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.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_opentemp.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
57 #include "got_worktree.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 #define CTRL(x) ((x) & 0x1f)
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static const struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
108 #define TOG_EOF_STRING "(END)"
110 struct commit_queue_entry {
111 TAILQ_ENTRY(commit_queue_entry) entry;
112 struct got_object_id *id;
113 struct got_commit_object *commit;
116 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 struct commit_queue {
119 struct commit_queue_head head;
123 STAILQ_ENTRY(tog_color) entry;
127 STAILQ_HEAD(tog_colors, tog_color);
129 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
130 static struct got_reflist_object_id_map *tog_refs_idmap;
132 static const struct got_error *
133 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
134 struct got_reference* re2)
136 const char *name1 = got_ref_get_name(re1);
137 const char *name2 = got_ref_get_name(re2);
138 int isbackup1, isbackup2;
140 /* Sort backup refs towards the bottom of the list. */
141 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
142 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
143 if (!isbackup1 && isbackup2) {
146 } else if (isbackup1 && !isbackup2) {
151 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
155 static const struct got_error *
156 tog_load_refs(struct got_repository *repo, int sort_by_date)
158 const struct got_error *err;
160 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
161 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
166 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
173 if (tog_refs_idmap) {
174 got_reflist_object_id_map_free(tog_refs_idmap);
175 tog_refs_idmap = NULL;
177 got_ref_list_free(&tog_refs);
180 static const struct got_error *
181 add_color(struct tog_colors *colors, const char *pattern,
182 int idx, short color)
184 const struct got_error *err = NULL;
185 struct tog_color *tc;
188 if (idx < 1 || idx > COLOR_PAIRS - 1)
191 init_pair(idx, color, -1);
193 tc = calloc(1, sizeof(*tc));
195 return got_error_from_errno("calloc");
196 regerr = regcomp(&tc->regex, pattern,
197 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
199 static char regerr_msg[512];
200 static char err_msg[512];
201 regerror(regerr, &tc->regex, regerr_msg,
203 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
205 err = got_error_msg(GOT_ERR_REGEX, err_msg);
210 STAILQ_INSERT_HEAD(colors, tc, entry);
215 free_colors(struct tog_colors *colors)
217 struct tog_color *tc;
219 while (!STAILQ_EMPTY(colors)) {
220 tc = STAILQ_FIRST(colors);
221 STAILQ_REMOVE_HEAD(colors, entry);
228 get_color(struct tog_colors *colors, int colorpair)
230 struct tog_color *tc = NULL;
232 STAILQ_FOREACH(tc, colors, entry) {
233 if (tc->colorpair == colorpair)
241 default_color_value(const char *envvar)
243 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
244 return COLOR_MAGENTA;
245 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
247 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
249 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
251 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
252 return COLOR_MAGENTA;
253 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
254 return COLOR_MAGENTA;
255 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
257 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
259 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
261 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
263 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
265 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
267 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
268 return COLOR_MAGENTA;
269 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
271 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
278 get_color_value(const char *envvar)
280 const char *val = getenv(envvar);
283 return default_color_value(envvar);
285 if (strcasecmp(val, "black") == 0)
287 if (strcasecmp(val, "red") == 0)
289 if (strcasecmp(val, "green") == 0)
291 if (strcasecmp(val, "yellow") == 0)
293 if (strcasecmp(val, "blue") == 0)
295 if (strcasecmp(val, "magenta") == 0)
296 return COLOR_MAGENTA;
297 if (strcasecmp(val, "cyan") == 0)
299 if (strcasecmp(val, "white") == 0)
301 if (strcasecmp(val, "default") == 0)
304 return default_color_value(envvar);
308 struct tog_diff_view_state {
309 struct got_object_id *id1, *id2;
310 const char *label1, *label2;
312 int first_displayed_line;
313 int last_displayed_line;
316 int ignore_whitespace;
318 struct got_repository *repo;
319 struct tog_colors colors;
325 /* passed from log view; may be NULL */
326 struct tog_view *log_view;
329 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
331 struct tog_log_thread_args {
332 pthread_cond_t need_commits;
333 pthread_cond_t commit_loaded;
336 struct got_commit_graph *graph;
337 struct commit_queue *commits;
338 const char *in_repo_path;
339 struct got_object_id *start_id;
340 struct got_repository *repo;
344 struct commit_queue_entry **first_displayed_entry;
345 struct commit_queue_entry **selected_entry;
347 int *search_next_done;
351 struct tog_log_view_state {
352 struct commit_queue commits;
353 struct commit_queue_entry *first_displayed_entry;
354 struct commit_queue_entry *last_displayed_entry;
355 struct commit_queue_entry *selected_entry;
360 struct got_repository *repo;
361 struct got_object_id *start_id;
364 struct tog_log_thread_args thread_args;
365 struct commit_queue_entry *matched_entry;
366 struct commit_queue_entry *search_entry;
367 struct tog_colors colors;
370 #define TOG_COLOR_DIFF_MINUS 1
371 #define TOG_COLOR_DIFF_PLUS 2
372 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
373 #define TOG_COLOR_DIFF_META 4
374 #define TOG_COLOR_TREE_SUBMODULE 5
375 #define TOG_COLOR_TREE_SYMLINK 6
376 #define TOG_COLOR_TREE_DIRECTORY 7
377 #define TOG_COLOR_TREE_EXECUTABLE 8
378 #define TOG_COLOR_COMMIT 9
379 #define TOG_COLOR_AUTHOR 10
380 #define TOG_COLOR_DATE 11
381 #define TOG_COLOR_REFS_HEADS 12
382 #define TOG_COLOR_REFS_TAGS 13
383 #define TOG_COLOR_REFS_REMOTES 14
384 #define TOG_COLOR_REFS_BACKUP 15
386 struct tog_blame_cb_args {
387 struct tog_blame_line *lines; /* one per line */
390 struct tog_view *view;
391 struct got_object_id *commit_id;
395 struct tog_blame_thread_args {
397 struct got_repository *repo;
398 struct tog_blame_cb_args *cb_args;
400 got_cancel_cb cancel_cb;
407 struct tog_blame_line *lines;
411 struct tog_blame_thread_args thread_args;
412 struct tog_blame_cb_args cb_args;
417 struct tog_blame_view_state {
418 int first_displayed_line;
419 int last_displayed_line;
424 struct got_object_id_queue blamed_commits;
425 struct got_object_qid *blamed_commit;
427 struct got_repository *repo;
428 struct got_object_id *commit_id;
429 struct tog_blame blame;
431 struct tog_colors colors;
434 struct tog_parent_tree {
435 TAILQ_ENTRY(tog_parent_tree) entry;
436 struct got_tree_object *tree;
437 struct got_tree_entry *first_displayed_entry;
438 struct got_tree_entry *selected_entry;
442 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
444 struct tog_tree_view_state {
446 struct got_object_id *commit_id;/* commit which this tree belongs to */
447 struct got_tree_object *root; /* the commit's root tree entry */
448 struct got_tree_object *tree; /* currently displayed (sub-)tree */
449 struct got_tree_entry *first_displayed_entry;
450 struct got_tree_entry *last_displayed_entry;
451 struct got_tree_entry *selected_entry;
452 int ndisplayed, selected, show_ids;
453 struct tog_parent_trees parents; /* parent trees of current sub-tree */
455 struct got_repository *repo;
456 struct got_tree_entry *matched_entry;
457 struct tog_colors colors;
460 struct tog_reflist_entry {
461 TAILQ_ENTRY(tog_reflist_entry) entry;
462 struct got_reference *ref;
466 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
468 struct tog_ref_view_state {
469 struct tog_reflist_head refs;
470 struct tog_reflist_entry *first_displayed_entry;
471 struct tog_reflist_entry *last_displayed_entry;
472 struct tog_reflist_entry *selected_entry;
473 int nrefs, ndisplayed, selected, show_ids, sort_by_date;
474 struct got_repository *repo;
475 struct tog_reflist_entry *matched_entry;
476 struct tog_colors colors;
480 * We implement two types of views: parent views and child views.
482 * The 'Tab' key switches focus between a parent view and its child view.
483 * Child views are shown side-by-side to their parent view, provided
484 * there is enough screen estate.
486 * When a new view is opened from within a parent view, this new view
487 * becomes a child view of the parent view, replacing any existing child.
489 * When a new view is opened from within a child view, this new view
490 * becomes a parent view which will obscure the views below until the
491 * user quits the new parent view by typing 'q'.
493 * This list of views contains parent views only.
494 * Child views are only pointed to by their parent view.
496 TAILQ_HEAD(tog_view_list_head, tog_view);
499 TAILQ_ENTRY(tog_view) entry;
502 int nlines, ncols, begin_y, begin_x;
503 int lines, cols; /* copies of LINES and COLS */
504 int focussed; /* Only set on one parent or child view at a time. */
506 struct tog_view *parent;
507 struct tog_view *child;
510 * This flag is initially set on parent views when a new child view
511 * is created. It gets toggled when the 'Tab' key switches focus
512 * between parent and child.
513 * The flag indicates whether focus should be passed on to our child
514 * view if this parent view gets picked for focus after another parent
515 * view was closed. This prevents child views from losing focus in such
520 /* type-specific state */
521 enum tog_view_type type;
523 struct tog_diff_view_state diff;
524 struct tog_log_view_state log;
525 struct tog_blame_view_state blame;
526 struct tog_tree_view_state tree;
527 struct tog_ref_view_state ref;
530 const struct got_error *(*show)(struct tog_view *);
531 const struct got_error *(*input)(struct tog_view **,
532 struct tog_view *, int);
533 const struct got_error *(*close)(struct tog_view *);
535 const struct got_error *(*search_start)(struct tog_view *);
536 const struct got_error *(*search_next)(struct tog_view *);
539 #define TOG_SEARCH_FORWARD 1
540 #define TOG_SEARCH_BACKWARD 2
541 int search_next_done;
542 #define TOG_SEARCH_HAVE_MORE 1
543 #define TOG_SEARCH_NO_MORE 2
544 #define TOG_SEARCH_HAVE_NONE 3
549 static const struct got_error *open_diff_view(struct tog_view *,
550 struct got_object_id *, struct got_object_id *,
551 const char *, const char *, int, int, int, struct tog_view *,
552 struct got_repository *);
553 static const struct got_error *show_diff_view(struct tog_view *);
554 static const struct got_error *input_diff_view(struct tog_view **,
555 struct tog_view *, int);
556 static const struct got_error* close_diff_view(struct tog_view *);
557 static const struct got_error *search_start_diff_view(struct tog_view *);
558 static const struct got_error *search_next_diff_view(struct tog_view *);
560 static const struct got_error *open_log_view(struct tog_view *,
561 struct got_object_id *, struct got_repository *,
562 const char *, const char *, int);
563 static const struct got_error * show_log_view(struct tog_view *);
564 static const struct got_error *input_log_view(struct tog_view **,
565 struct tog_view *, int);
566 static const struct got_error *close_log_view(struct tog_view *);
567 static const struct got_error *search_start_log_view(struct tog_view *);
568 static const struct got_error *search_next_log_view(struct tog_view *);
570 static const struct got_error *open_blame_view(struct tog_view *, char *,
571 struct got_object_id *, struct got_repository *);
572 static const struct got_error *show_blame_view(struct tog_view *);
573 static const struct got_error *input_blame_view(struct tog_view **,
574 struct tog_view *, int);
575 static const struct got_error *close_blame_view(struct tog_view *);
576 static const struct got_error *search_start_blame_view(struct tog_view *);
577 static const struct got_error *search_next_blame_view(struct tog_view *);
579 static const struct got_error *open_tree_view(struct tog_view *,
580 struct got_object_id *, const char *, struct got_repository *);
581 static const struct got_error *show_tree_view(struct tog_view *);
582 static const struct got_error *input_tree_view(struct tog_view **,
583 struct tog_view *, int);
584 static const struct got_error *close_tree_view(struct tog_view *);
585 static const struct got_error *search_start_tree_view(struct tog_view *);
586 static const struct got_error *search_next_tree_view(struct tog_view *);
588 static const struct got_error *open_ref_view(struct tog_view *,
589 struct got_repository *);
590 static const struct got_error *show_ref_view(struct tog_view *);
591 static const struct got_error *input_ref_view(struct tog_view **,
592 struct tog_view *, int);
593 static const struct got_error *close_ref_view(struct tog_view *);
594 static const struct got_error *search_start_ref_view(struct tog_view *);
595 static const struct got_error *search_next_ref_view(struct tog_view *);
597 static volatile sig_atomic_t tog_sigwinch_received;
598 static volatile sig_atomic_t tog_sigpipe_received;
599 static volatile sig_atomic_t tog_sigcont_received;
600 static volatile sig_atomic_t tog_sigint_received;
601 static volatile sig_atomic_t tog_sigterm_received;
604 tog_sigwinch(int signo)
606 tog_sigwinch_received = 1;
610 tog_sigpipe(int signo)
612 tog_sigpipe_received = 1;
616 tog_sigcont(int signo)
618 tog_sigcont_received = 1;
622 tog_sigint(int signo)
624 tog_sigint_received = 1;
628 tog_sigterm(int signo)
630 tog_sigterm_received = 1;
634 tog_fatal_signal_received()
636 return (tog_sigpipe_received ||
637 tog_sigint_received || tog_sigint_received);
641 static const struct got_error *
642 view_close(struct tog_view *view)
644 const struct got_error *err = NULL;
647 view_close(view->child);
651 err = view->close(view);
653 del_panel(view->panel);
655 delwin(view->window);
660 static struct tog_view *
661 view_open(int nlines, int ncols, int begin_y, int begin_x,
662 enum tog_view_type type)
664 struct tog_view *view = calloc(1, sizeof(*view));
672 view->nlines = nlines ? nlines : LINES - begin_y;
673 view->ncols = ncols ? ncols : COLS - begin_x;
674 view->begin_y = begin_y;
675 view->begin_x = begin_x;
676 view->window = newwin(nlines, ncols, begin_y, begin_x);
677 if (view->window == NULL) {
681 view->panel = new_panel(view->window);
682 if (view->panel == NULL ||
683 set_panel_userptr(view->panel, view) != OK) {
688 keypad(view->window, TRUE);
693 view_split_begin_x(int begin_x)
695 if (begin_x > 0 || COLS < 120)
697 return (COLS - MAX(COLS / 2, 80));
700 static const struct got_error *view_resize(struct tog_view *);
702 static const struct got_error *
703 view_splitscreen(struct tog_view *view)
705 const struct got_error *err = NULL;
708 view->begin_x = view_split_begin_x(0);
709 view->nlines = LINES;
710 view->ncols = COLS - view->begin_x;
713 err = view_resize(view);
717 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
718 return got_error_from_errno("mvwin");
723 static const struct got_error *
724 view_fullscreen(struct tog_view *view)
726 const struct got_error *err = NULL;
730 view->nlines = LINES;
734 err = view_resize(view);
738 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
739 return got_error_from_errno("mvwin");
745 view_is_parent_view(struct tog_view *view)
747 return view->parent == NULL;
750 static const struct got_error *
751 view_resize(struct tog_view *view)
755 if (view->lines > LINES)
756 nlines = view->nlines - (view->lines - LINES);
758 nlines = view->nlines + (LINES - view->lines);
760 if (view->cols > COLS)
761 ncols = view->ncols - (view->cols - COLS);
763 ncols = view->ncols + (COLS - view->cols);
765 if (wresize(view->window, nlines, ncols) == ERR)
766 return got_error_from_errno("wresize");
767 if (replace_panel(view->panel, view->window) == ERR)
768 return got_error_from_errno("replace_panel");
769 wclear(view->window);
771 view->nlines = nlines;
777 view->child->begin_x = view_split_begin_x(view->begin_x);
778 if (view->child->begin_x == 0) {
779 view_fullscreen(view->child);
780 if (view->child->focussed)
781 show_panel(view->child->panel);
783 show_panel(view->panel);
785 view_splitscreen(view->child);
786 show_panel(view->child->panel);
793 static const struct got_error *
794 view_close_child(struct tog_view *view)
796 const struct got_error *err = NULL;
798 if (view->child == NULL)
801 err = view_close(view->child);
807 view_set_child(struct tog_view *view, struct tog_view *child)
810 child->parent = view;
814 view_is_splitscreen(struct tog_view *view)
816 return view->begin_x > 0;
825 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
826 cols = 80; /* Default */
832 resize_term(lines, cols);
835 static const struct got_error *
836 view_search_start(struct tog_view *view)
838 const struct got_error *err = NULL;
842 if (view->search_started) {
843 regfree(&view->regex);
845 memset(&view->regmatch, 0, sizeof(view->regmatch));
847 view->search_started = 0;
849 if (view->nlines < 1)
852 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
853 wclrtoeol(view->window);
857 ret = wgetnstr(view->window, pattern, sizeof(pattern));
863 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
864 err = view->search_start(view);
866 regfree(&view->regex);
869 view->search_started = 1;
870 view->searching = TOG_SEARCH_FORWARD;
871 view->search_next_done = 0;
872 view->search_next(view);
878 static const struct got_error *
879 view_input(struct tog_view **new, int *done, struct tog_view *view,
880 struct tog_view_list_head *views)
882 const struct got_error *err = NULL;
888 /* Clear "no matches" indicator. */
889 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
890 view->search_next_done == TOG_SEARCH_HAVE_NONE)
891 view->search_next_done = TOG_SEARCH_HAVE_MORE;
893 if (view->searching && !view->search_next_done) {
894 errcode = pthread_mutex_unlock(&tog_mutex);
896 return got_error_set_errno(errcode,
897 "pthread_mutex_unlock");
899 errcode = pthread_mutex_lock(&tog_mutex);
901 return got_error_set_errno(errcode,
902 "pthread_mutex_lock");
903 view->search_next(view);
907 nodelay(stdscr, FALSE);
908 /* Allow threads to make progress while we are waiting for input. */
909 errcode = pthread_mutex_unlock(&tog_mutex);
911 return got_error_set_errno(errcode, "pthread_mutex_unlock");
912 ch = wgetch(view->window);
913 errcode = pthread_mutex_lock(&tog_mutex);
915 return got_error_set_errno(errcode, "pthread_mutex_lock");
916 nodelay(stdscr, TRUE);
918 if (tog_sigwinch_received || tog_sigcont_received) {
920 tog_sigwinch_received = 0;
921 tog_sigcont_received = 0;
922 TAILQ_FOREACH(v, views, entry) {
923 err = view_resize(v);
926 err = v->input(new, v, KEY_RESIZE);
930 err = view_resize(v->child);
933 err = v->child->input(new, v->child,
945 view->child->focussed = 1;
946 view->focus_child = 1;
947 } else if (view->parent) {
949 view->parent->focussed = 1;
950 view->parent->focus_child = 0;
954 err = view->input(new, view, ch);
961 if (view_is_parent_view(view)) {
962 if (view->child == NULL)
964 if (view_is_splitscreen(view->child)) {
966 view->child->focussed = 1;
967 err = view_fullscreen(view->child);
969 err = view_splitscreen(view->child);
972 err = view->child->input(new, view->child,
975 if (view_is_splitscreen(view)) {
976 view->parent->focussed = 0;
978 err = view_fullscreen(view);
980 err = view_splitscreen(view);
984 err = view->input(new, view, KEY_RESIZE);
990 if (view->search_start)
991 view_search_start(view);
993 err = view->input(new, view, ch);
997 if (view->search_started && view->search_next) {
998 view->searching = (ch == 'n' ?
999 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1000 view->search_next_done = 0;
1001 view->search_next(view);
1003 err = view->input(new, view, ch);
1006 err = view->input(new, view, ch);
1014 view_vborder(struct tog_view *view)
1017 const struct tog_view *view_above;
1020 return view_vborder(view->parent);
1022 panel = panel_above(view->panel);
1026 view_above = panel_userptr(panel);
1027 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1028 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1032 view_needs_focus_indication(struct tog_view *view)
1034 if (view_is_parent_view(view)) {
1035 if (view->child == NULL || view->child->focussed)
1037 if (!view_is_splitscreen(view->child))
1039 } else if (!view_is_splitscreen(view))
1042 return view->focussed;
1045 static const struct got_error *
1046 view_loop(struct tog_view *view)
1048 const struct got_error *err = NULL;
1049 struct tog_view_list_head views;
1050 struct tog_view *new_view;
1051 int fast_refresh = 10;
1052 int done = 0, errcode;
1054 errcode = pthread_mutex_lock(&tog_mutex);
1056 return got_error_set_errno(errcode, "pthread_mutex_lock");
1059 TAILQ_INSERT_HEAD(&views, view, entry);
1062 err = view->show(view);
1067 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1068 /* Refresh fast during initialization, then become slower. */
1069 if (fast_refresh && fast_refresh-- == 0)
1070 halfdelay(10); /* switch to once per second */
1072 err = view_input(&new_view, &done, view, &views);
1076 struct tog_view *v, *prev = NULL;
1078 if (view_is_parent_view(view))
1079 prev = TAILQ_PREV(view, tog_view_list_head,
1081 else if (view->parent)
1082 prev = view->parent;
1085 view->parent->child = NULL;
1086 view->parent->focus_child = 0;
1088 TAILQ_REMOVE(&views, view, entry);
1090 err = view_close(view);
1095 TAILQ_FOREACH(v, &views, entry) {
1099 if (view == NULL && new_view == NULL) {
1100 /* No view has focus. Try to pick one. */
1103 else if (!TAILQ_EMPTY(&views)) {
1104 view = TAILQ_LAST(&views,
1105 tog_view_list_head);
1108 if (view->focus_child) {
1109 view->child->focussed = 1;
1117 struct tog_view *v, *t;
1118 /* Only allow one parent view per type. */
1119 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1120 if (v->type != new_view->type)
1122 TAILQ_REMOVE(&views, v, entry);
1123 err = view_close(v);
1128 TAILQ_INSERT_TAIL(&views, new_view, entry);
1132 if (view_is_parent_view(view)) {
1133 if (view->child && view->child->focussed)
1136 if (view->parent && view->parent->focussed)
1137 view = view->parent;
1139 show_panel(view->panel);
1140 if (view->child && view_is_splitscreen(view->child))
1141 show_panel(view->child->panel);
1142 if (view->parent && view_is_splitscreen(view)) {
1143 err = view->parent->show(view->parent);
1147 err = view->show(view);
1151 err = view->child->show(view->child);
1160 while (!TAILQ_EMPTY(&views)) {
1161 view = TAILQ_FIRST(&views);
1162 TAILQ_REMOVE(&views, view, entry);
1166 errcode = pthread_mutex_unlock(&tog_mutex);
1167 if (errcode && err == NULL)
1168 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1178 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1183 /* Create newly allocated wide-character string equivalent to a byte string. */
1184 static const struct got_error *
1185 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1188 const struct got_error *err = NULL;
1191 *wlen = mbstowcs(NULL, s, 0);
1192 if (*wlen == (size_t)-1) {
1194 if (errno != EILSEQ)
1195 return got_error_from_errno("mbstowcs");
1197 /* byte string invalid in current encoding; try to "fix" it */
1198 err = got_mbsavis(&vis, &vislen, s);
1201 *wlen = mbstowcs(NULL, vis, 0);
1202 if (*wlen == (size_t)-1) {
1203 err = got_error_from_errno("mbstowcs"); /* give up */
1208 *ws = calloc(*wlen + 1, sizeof(**ws));
1210 err = got_error_from_errno("calloc");
1214 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1215 err = got_error_from_errno("mbstowcs");
1226 /* Format a line for display, ensuring that it won't overflow a width limit. */
1227 static const struct got_error *
1228 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1231 const struct got_error *err = NULL;
1233 wchar_t *wline = NULL;
1240 err = mbs2ws(&wline, &wlen, line);
1244 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1245 wline[wlen - 1] = L'\0';
1248 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1249 wline[wlen - 1] = L'\0';
1255 int width = wcwidth(wline[i]);
1262 if (width == 1 || width == 2) {
1263 if (cols + width > wlimit)
1267 } else if (width == -1) {
1268 if (wline[i] == L'\t') {
1270 ((cols + col_tab_align) % TABSIZE);
1275 if (cols + width > wlimit)
1280 err = got_error_from_errno("wcwidth");
1295 static const struct got_error*
1296 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1297 struct got_object_id *id, struct got_repository *repo)
1299 static const struct got_error *err = NULL;
1300 struct got_reflist_entry *re;
1306 TAILQ_FOREACH(re, refs, entry) {
1307 struct got_tag_object *tag = NULL;
1308 struct got_object_id *ref_id;
1311 name = got_ref_get_name(re->ref);
1312 if (strcmp(name, GOT_REF_HEAD) == 0)
1314 if (strncmp(name, "refs/", 5) == 0)
1316 if (strncmp(name, "got/", 4) == 0 &&
1317 strncmp(name, "got/backup/", 11) != 0)
1319 if (strncmp(name, "heads/", 6) == 0)
1321 if (strncmp(name, "remotes/", 8) == 0) {
1323 s = strstr(name, "/" GOT_REF_HEAD);
1324 if (s != NULL && s[strlen(s)] == '\0')
1327 err = got_ref_resolve(&ref_id, repo, re->ref);
1330 if (strncmp(name, "tags/", 5) == 0) {
1331 err = got_object_open_as_tag(&tag, repo, ref_id);
1333 if (err->code != GOT_ERR_OBJ_TYPE) {
1337 /* Ref points at something other than a tag. */
1342 cmp = got_object_id_cmp(tag ?
1343 got_object_tag_get_object_id(tag) : ref_id, id);
1346 got_object_tag_close(tag);
1350 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1351 s ? ", " : "", name) == -1) {
1352 err = got_error_from_errno("asprintf");
1363 static const struct got_error *
1364 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1369 smallerthan = strchr(author, '<');
1370 if (smallerthan && smallerthan[1] != '\0')
1371 author = smallerthan + 1;
1372 author[strcspn(author, "@>")] = '\0';
1373 return format_line(wauthor, author_width, author, limit, col_tab_align);
1376 static const struct got_error *
1377 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1378 struct got_object_id *id, const size_t date_display_cols,
1379 int author_display_cols)
1381 struct tog_log_view_state *s = &view->state.log;
1382 const struct got_error *err = NULL;
1383 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1384 char *logmsg0 = NULL, *logmsg = NULL;
1385 char *author = NULL;
1386 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1387 int author_width, logmsg_width;
1388 char *newline, *line = NULL;
1390 const int avail = view->ncols;
1392 time_t committer_time;
1393 struct tog_color *tc;
1395 committer_time = got_object_commit_get_committer_time(commit);
1396 if (gmtime_r(&committer_time, &tm) == NULL)
1397 return got_error_from_errno("gmtime_r");
1398 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1399 return got_error(GOT_ERR_NO_SPACE);
1401 if (avail <= date_display_cols)
1402 limit = MIN(sizeof(datebuf) - 1, avail);
1404 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1405 tc = get_color(&s->colors, TOG_COLOR_DATE);
1407 wattr_on(view->window,
1408 COLOR_PAIR(tc->colorpair), NULL);
1409 waddnstr(view->window, datebuf, limit);
1411 wattr_off(view->window,
1412 COLOR_PAIR(tc->colorpair), NULL);
1419 err = got_object_id_str(&id_str, id);
1422 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1424 wattr_on(view->window,
1425 COLOR_PAIR(tc->colorpair), NULL);
1426 wprintw(view->window, "%.8s ", id_str);
1428 wattr_off(view->window,
1429 COLOR_PAIR(tc->colorpair), NULL);
1436 author = strdup(got_object_commit_get_author(commit));
1437 if (author == NULL) {
1438 err = got_error_from_errno("strdup");
1441 err = format_author(&wauthor, &author_width, author, avail - col, col);
1444 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1446 wattr_on(view->window,
1447 COLOR_PAIR(tc->colorpair), NULL);
1448 waddwstr(view->window, wauthor);
1450 wattr_off(view->window,
1451 COLOR_PAIR(tc->colorpair), NULL);
1452 col += author_width;
1453 while (col < avail && author_width < author_display_cols + 2) {
1454 waddch(view->window, ' ');
1461 err = got_object_commit_get_logmsg(&logmsg0, commit);
1465 while (*logmsg == '\n')
1467 newline = strchr(logmsg, '\n');
1470 limit = avail - col;
1471 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1474 waddwstr(view->window, wlogmsg);
1475 col += logmsg_width;
1476 while (col < avail) {
1477 waddch(view->window, ' ');
1489 static struct commit_queue_entry *
1490 alloc_commit_queue_entry(struct got_commit_object *commit,
1491 struct got_object_id *id)
1493 struct commit_queue_entry *entry;
1495 entry = calloc(1, sizeof(*entry));
1500 entry->commit = commit;
1505 pop_commit(struct commit_queue *commits)
1507 struct commit_queue_entry *entry;
1509 entry = TAILQ_FIRST(&commits->head);
1510 TAILQ_REMOVE(&commits->head, entry, entry);
1511 got_object_commit_close(entry->commit);
1512 commits->ncommits--;
1513 /* Don't free entry->id! It is owned by the commit graph. */
1518 free_commits(struct commit_queue *commits)
1520 while (!TAILQ_EMPTY(&commits->head))
1521 pop_commit(commits);
1524 static const struct got_error *
1525 match_commit(int *have_match, struct got_object_id *id,
1526 struct got_commit_object *commit, regex_t *regex)
1528 const struct got_error *err = NULL;
1529 regmatch_t regmatch;
1530 char *id_str = NULL, *logmsg = NULL;
1534 err = got_object_id_str(&id_str, id);
1538 err = got_object_commit_get_logmsg(&logmsg, commit);
1542 if (regexec(regex, got_object_commit_get_author(commit), 1,
1543 ®match, 0) == 0 ||
1544 regexec(regex, got_object_commit_get_committer(commit), 1,
1545 ®match, 0) == 0 ||
1546 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1547 regexec(regex, logmsg, 1, ®match, 0) == 0)
1555 static const struct got_error *
1556 queue_commits(struct tog_log_thread_args *a)
1558 const struct got_error *err = NULL;
1561 * We keep all commits open throughout the lifetime of the log
1562 * view in order to avoid having to re-fetch commits from disk
1563 * while updating the display.
1566 struct got_object_id *id;
1567 struct got_commit_object *commit;
1568 struct commit_queue_entry *entry;
1571 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1573 if (err || id == NULL)
1576 err = got_object_open_as_commit(&commit, a->repo, id);
1579 entry = alloc_commit_queue_entry(commit, id);
1580 if (entry == NULL) {
1581 err = got_error_from_errno("alloc_commit_queue_entry");
1585 errcode = pthread_mutex_lock(&tog_mutex);
1587 err = got_error_set_errno(errcode,
1588 "pthread_mutex_lock");
1592 entry->idx = a->commits->ncommits;
1593 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1594 a->commits->ncommits++;
1596 if (*a->searching == TOG_SEARCH_FORWARD &&
1597 !*a->search_next_done) {
1599 err = match_commit(&have_match, id, commit, a->regex);
1603 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1606 errcode = pthread_mutex_unlock(&tog_mutex);
1607 if (errcode && err == NULL)
1608 err = got_error_set_errno(errcode,
1609 "pthread_mutex_unlock");
1612 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1618 select_commit(struct tog_log_view_state *s)
1620 struct commit_queue_entry *entry;
1623 entry = s->first_displayed_entry;
1625 if (ncommits == s->selected) {
1626 s->selected_entry = entry;
1629 entry = TAILQ_NEXT(entry, entry);
1634 static const struct got_error *
1635 draw_commits(struct tog_view *view)
1637 const struct got_error *err = NULL;
1638 struct tog_log_view_state *s = &view->state.log;
1639 struct commit_queue_entry *entry = s->selected_entry;
1640 const int limit = view->nlines;
1642 int ncommits, author_cols = 4;
1643 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1644 char *refs_str = NULL;
1646 struct tog_color *tc;
1647 static const size_t date_display_cols = 12;
1649 if (s->selected_entry &&
1650 !(view->searching && view->search_next_done == 0)) {
1651 struct got_reflist_head *refs;
1652 err = got_object_id_str(&id_str, s->selected_entry->id);
1655 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1656 s->selected_entry->id);
1658 err = build_refs_str(&refs_str, refs,
1659 s->selected_entry->id, s->repo);
1665 if (s->thread_args.commits_needed == 0)
1666 halfdelay(10); /* disable fast refresh */
1668 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1669 if (asprintf(&ncommits_str, " [%d/%d] %s",
1670 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1671 (view->searching && !view->search_next_done) ?
1672 "searching..." : "loading...") == -1) {
1673 err = got_error_from_errno("asprintf");
1677 const char *search_str = NULL;
1679 if (view->searching) {
1680 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1681 search_str = "no more matches";
1682 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1683 search_str = "no matches found";
1684 else if (!view->search_next_done)
1685 search_str = "searching...";
1688 if (asprintf(&ncommits_str, " [%d/%d] %s",
1689 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1690 search_str ? search_str :
1691 (refs_str ? refs_str : "")) == -1) {
1692 err = got_error_from_errno("asprintf");
1697 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1698 if (asprintf(&header, "commit %s %s%s",
1699 id_str ? id_str : "........................................",
1700 s->in_repo_path, ncommits_str) == -1) {
1701 err = got_error_from_errno("asprintf");
1705 } else if (asprintf(&header, "commit %s%s",
1706 id_str ? id_str : "........................................",
1707 ncommits_str) == -1) {
1708 err = got_error_from_errno("asprintf");
1712 err = format_line(&wline, &width, header, view->ncols, 0);
1716 werase(view->window);
1718 if (view_needs_focus_indication(view))
1719 wstandout(view->window);
1720 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1722 wattr_on(view->window,
1723 COLOR_PAIR(tc->colorpair), NULL);
1724 waddwstr(view->window, wline);
1726 wattr_off(view->window,
1727 COLOR_PAIR(tc->colorpair), NULL);
1728 while (width < view->ncols) {
1729 waddch(view->window, ' ');
1732 if (view_needs_focus_indication(view))
1733 wstandend(view->window);
1738 /* Grow author column size if necessary. */
1739 entry = s->first_displayed_entry;
1745 if (ncommits >= limit - 1)
1747 author = strdup(got_object_commit_get_author(entry->commit));
1748 if (author == NULL) {
1749 err = got_error_from_errno("strdup");
1752 err = format_author(&wauthor, &width, author, COLS,
1754 if (author_cols < width)
1755 author_cols = width;
1759 entry = TAILQ_NEXT(entry, entry);
1762 entry = s->first_displayed_entry;
1763 s->last_displayed_entry = s->first_displayed_entry;
1766 if (ncommits >= limit - 1)
1768 if (ncommits == s->selected)
1769 wstandout(view->window);
1770 err = draw_commit(view, entry->commit, entry->id,
1771 date_display_cols, author_cols);
1772 if (ncommits == s->selected)
1773 wstandend(view->window);
1777 s->last_displayed_entry = entry;
1778 entry = TAILQ_NEXT(entry, entry);
1791 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1793 struct commit_queue_entry *entry;
1796 entry = TAILQ_FIRST(&s->commits.head);
1797 if (s->first_displayed_entry == entry)
1800 entry = s->first_displayed_entry;
1801 while (entry && nscrolled < maxscroll) {
1802 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1804 s->first_displayed_entry = entry;
1810 static const struct got_error *
1811 trigger_log_thread(struct tog_view *view, int wait)
1813 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1816 halfdelay(1); /* fast refresh while loading commits */
1818 while (ta->commits_needed > 0 || ta->load_all) {
1819 if (ta->log_complete)
1822 /* Wake the log thread. */
1823 errcode = pthread_cond_signal(&ta->need_commits);
1825 return got_error_set_errno(errcode,
1826 "pthread_cond_signal");
1829 * The mutex will be released while the view loop waits
1830 * in wgetch(), at which time the log thread will run.
1835 /* Display progress update in log view. */
1836 show_log_view(view);
1840 /* Wait right here while next commit is being loaded. */
1841 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1843 return got_error_set_errno(errcode,
1844 "pthread_cond_wait");
1846 /* Display progress update in log view. */
1847 show_log_view(view);
1855 static const struct got_error *
1856 log_scroll_down(struct tog_view *view, int maxscroll)
1858 struct tog_log_view_state *s = &view->state.log;
1859 const struct got_error *err = NULL;
1860 struct commit_queue_entry *pentry;
1861 int nscrolled = 0, ncommits_needed;
1863 if (s->last_displayed_entry == NULL)
1866 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1867 if (s->commits.ncommits < ncommits_needed &&
1868 !s->thread_args.log_complete) {
1870 * Ask the log thread for required amount of commits.
1872 s->thread_args.commits_needed += maxscroll;
1873 err = trigger_log_thread(view, 1);
1879 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1883 s->last_displayed_entry = pentry;
1885 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1888 s->first_displayed_entry = pentry;
1889 } while (++nscrolled < maxscroll);
1894 static const struct got_error *
1895 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1896 struct got_commit_object *commit, struct got_object_id *commit_id,
1897 struct tog_view *log_view, struct got_repository *repo)
1899 const struct got_error *err;
1900 struct got_object_qid *parent_id;
1901 struct tog_view *diff_view;
1903 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1904 if (diff_view == NULL)
1905 return got_error_from_errno("view_open");
1907 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1908 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1909 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1911 *new_view = diff_view;
1915 static const struct got_error *
1916 tree_view_visit_subtree(struct tog_tree_view_state *s,
1917 struct got_tree_object *subtree)
1919 struct tog_parent_tree *parent;
1921 parent = calloc(1, sizeof(*parent));
1923 return got_error_from_errno("calloc");
1925 parent->tree = s->tree;
1926 parent->first_displayed_entry = s->first_displayed_entry;
1927 parent->selected_entry = s->selected_entry;
1928 parent->selected = s->selected;
1929 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1932 s->first_displayed_entry = NULL;
1936 static const struct got_error *
1937 tree_view_walk_path(struct tog_tree_view_state *s,
1938 struct got_commit_object *commit, const char *path)
1940 const struct got_error *err = NULL;
1941 struct got_tree_object *tree = NULL;
1943 char *slash, *subpath = NULL;
1945 /* Walk the path and open corresponding tree objects. */
1948 struct got_tree_entry *te;
1949 struct got_object_id *tree_id;
1955 /* Ensure the correct subtree entry is selected. */
1956 slash = strchr(p, '/');
1958 te_name = strdup(p);
1960 te_name = strndup(p, slash - p);
1961 if (te_name == NULL) {
1962 err = got_error_from_errno("strndup");
1965 te = got_object_tree_find_entry(s->tree, te_name);
1967 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1972 s->first_displayed_entry = s->selected_entry = te;
1974 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1975 break; /* jump to this file's entry */
1977 slash = strchr(p, '/');
1979 subpath = strndup(path, slash - path);
1981 subpath = strdup(path);
1982 if (subpath == NULL) {
1983 err = got_error_from_errno("strdup");
1987 err = got_object_id_by_path(&tree_id, s->repo, commit,
1992 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1997 err = tree_view_visit_subtree(s, tree);
1999 got_object_tree_close(tree);
2013 static const struct got_error *
2014 browse_commit_tree(struct tog_view **new_view, int begin_x,
2015 struct commit_queue_entry *entry, const char *path,
2016 const char *head_ref_name, struct got_repository *repo)
2018 const struct got_error *err = NULL;
2019 struct tog_tree_view_state *s;
2020 struct tog_view *tree_view;
2022 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2023 if (tree_view == NULL)
2024 return got_error_from_errno("view_open");
2026 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2029 s = &tree_view->state.tree;
2031 *new_view = tree_view;
2033 if (got_path_is_root_dir(path))
2036 return tree_view_walk_path(s, entry->commit, path);
2039 static const struct got_error *
2040 block_signals_used_by_main_thread(void)
2045 if (sigemptyset(&sigset) == -1)
2046 return got_error_from_errno("sigemptyset");
2048 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2049 if (sigaddset(&sigset, SIGWINCH) == -1)
2050 return got_error_from_errno("sigaddset");
2051 if (sigaddset(&sigset, SIGCONT) == -1)
2052 return got_error_from_errno("sigaddset");
2053 if (sigaddset(&sigset, SIGINT) == -1)
2054 return got_error_from_errno("sigaddset");
2055 if (sigaddset(&sigset, SIGTERM) == -1)
2056 return got_error_from_errno("sigaddset");
2058 /* ncurses handles SIGTSTP */
2059 if (sigaddset(&sigset, SIGTSTP) == -1)
2060 return got_error_from_errno("sigaddset");
2062 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2064 return got_error_set_errno(errcode, "pthread_sigmask");
2070 log_thread(void *arg)
2072 const struct got_error *err = NULL;
2074 struct tog_log_thread_args *a = arg;
2077 err = block_signals_used_by_main_thread();
2081 while (!done && !err && !tog_fatal_signal_received()) {
2082 err = queue_commits(a);
2084 if (err->code != GOT_ERR_ITER_COMPLETED)
2088 } else if (a->commits_needed > 0 && !a->load_all)
2089 a->commits_needed--;
2091 errcode = pthread_mutex_lock(&tog_mutex);
2093 err = got_error_set_errno(errcode,
2094 "pthread_mutex_lock");
2096 } else if (*a->quit)
2098 else if (*a->first_displayed_entry == NULL) {
2099 *a->first_displayed_entry =
2100 TAILQ_FIRST(&a->commits->head);
2101 *a->selected_entry = *a->first_displayed_entry;
2104 errcode = pthread_cond_signal(&a->commit_loaded);
2106 err = got_error_set_errno(errcode,
2107 "pthread_cond_signal");
2108 pthread_mutex_unlock(&tog_mutex);
2113 a->commits_needed = 0;
2115 if (a->commits_needed == 0 && !a->load_all) {
2116 errcode = pthread_cond_wait(&a->need_commits,
2119 err = got_error_set_errno(errcode,
2120 "pthread_cond_wait");
2126 errcode = pthread_mutex_unlock(&tog_mutex);
2127 if (errcode && err == NULL)
2128 err = got_error_set_errno(errcode,
2129 "pthread_mutex_unlock");
2131 a->log_complete = 1;
2135 static const struct got_error *
2136 stop_log_thread(struct tog_log_view_state *s)
2138 const struct got_error *err = NULL;
2143 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2145 return got_error_set_errno(errcode,
2146 "pthread_cond_signal");
2147 errcode = pthread_mutex_unlock(&tog_mutex);
2149 return got_error_set_errno(errcode,
2150 "pthread_mutex_unlock");
2151 errcode = pthread_join(s->thread, (void **)&err);
2153 return got_error_set_errno(errcode, "pthread_join");
2154 errcode = pthread_mutex_lock(&tog_mutex);
2156 return got_error_set_errno(errcode,
2157 "pthread_mutex_lock");
2161 if (s->thread_args.repo) {
2162 err = got_repo_close(s->thread_args.repo);
2163 s->thread_args.repo = NULL;
2166 if (s->thread_args.pack_fds) {
2167 const struct got_error *pack_err =
2168 got_repo_pack_fds_close(s->thread_args.pack_fds);
2171 s->thread_args.pack_fds = NULL;
2174 if (s->thread_args.graph) {
2175 got_commit_graph_close(s->thread_args.graph);
2176 s->thread_args.graph = NULL;
2182 static const struct got_error *
2183 close_log_view(struct tog_view *view)
2185 const struct got_error *err = NULL;
2186 struct tog_log_view_state *s = &view->state.log;
2189 err = stop_log_thread(s);
2191 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2192 if (errcode && err == NULL)
2193 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2195 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2196 if (errcode && err == NULL)
2197 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2199 free_commits(&s->commits);
2200 free(s->in_repo_path);
2201 s->in_repo_path = NULL;
2204 free(s->head_ref_name);
2205 s->head_ref_name = NULL;
2209 static const struct got_error *
2210 search_start_log_view(struct tog_view *view)
2212 struct tog_log_view_state *s = &view->state.log;
2214 s->matched_entry = NULL;
2215 s->search_entry = NULL;
2219 static const struct got_error *
2220 search_next_log_view(struct tog_view *view)
2222 const struct got_error *err = NULL;
2223 struct tog_log_view_state *s = &view->state.log;
2224 struct commit_queue_entry *entry;
2226 /* Display progress update in log view. */
2227 show_log_view(view);
2231 if (s->search_entry) {
2233 errcode = pthread_mutex_unlock(&tog_mutex);
2235 return got_error_set_errno(errcode,
2236 "pthread_mutex_unlock");
2237 ch = wgetch(view->window);
2238 errcode = pthread_mutex_lock(&tog_mutex);
2240 return got_error_set_errno(errcode,
2241 "pthread_mutex_lock");
2242 if (ch == KEY_BACKSPACE) {
2243 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2246 if (view->searching == TOG_SEARCH_FORWARD)
2247 entry = TAILQ_NEXT(s->search_entry, entry);
2249 entry = TAILQ_PREV(s->search_entry,
2250 commit_queue_head, entry);
2251 } else if (s->matched_entry) {
2252 if (view->searching == TOG_SEARCH_FORWARD)
2253 entry = TAILQ_NEXT(s->matched_entry, entry);
2255 entry = TAILQ_PREV(s->matched_entry,
2256 commit_queue_head, entry);
2258 entry = s->selected_entry;
2264 if (entry == NULL) {
2265 if (s->thread_args.log_complete ||
2266 view->searching == TOG_SEARCH_BACKWARD) {
2267 view->search_next_done =
2268 (s->matched_entry == NULL ?
2269 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2270 s->search_entry = NULL;
2274 * Poke the log thread for more commits and return,
2275 * allowing the main loop to make progress. Search
2276 * will resume at s->search_entry once we come back.
2278 s->thread_args.commits_needed++;
2279 return trigger_log_thread(view, 0);
2282 err = match_commit(&have_match, entry->id, entry->commit,
2287 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2288 s->matched_entry = entry;
2292 s->search_entry = entry;
2293 if (view->searching == TOG_SEARCH_FORWARD)
2294 entry = TAILQ_NEXT(entry, entry);
2296 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2299 if (s->matched_entry) {
2300 int cur = s->selected_entry->idx;
2301 while (cur < s->matched_entry->idx) {
2302 err = input_log_view(NULL, view, KEY_DOWN);
2307 while (cur > s->matched_entry->idx) {
2308 err = input_log_view(NULL, view, KEY_UP);
2315 s->search_entry = NULL;
2320 static const struct got_error *
2321 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2322 struct got_repository *repo, const char *head_ref_name,
2323 const char *in_repo_path, int log_branches)
2325 const struct got_error *err = NULL;
2326 struct tog_log_view_state *s = &view->state.log;
2327 struct got_repository *thread_repo = NULL;
2328 struct got_commit_graph *thread_graph = NULL;
2331 if (in_repo_path != s->in_repo_path) {
2332 free(s->in_repo_path);
2333 s->in_repo_path = strdup(in_repo_path);
2334 if (s->in_repo_path == NULL)
2335 return got_error_from_errno("strdup");
2338 /* The commit queue only contains commits being displayed. */
2339 TAILQ_INIT(&s->commits.head);
2340 s->commits.ncommits = 0;
2343 if (head_ref_name) {
2344 s->head_ref_name = strdup(head_ref_name);
2345 if (s->head_ref_name == NULL) {
2346 err = got_error_from_errno("strdup");
2350 s->start_id = got_object_id_dup(start_id);
2351 if (s->start_id == NULL) {
2352 err = got_error_from_errno("got_object_id_dup");
2355 s->log_branches = log_branches;
2357 STAILQ_INIT(&s->colors);
2358 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2359 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2360 get_color_value("TOG_COLOR_COMMIT"));
2363 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2364 get_color_value("TOG_COLOR_AUTHOR"));
2366 free_colors(&s->colors);
2369 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2370 get_color_value("TOG_COLOR_DATE"));
2372 free_colors(&s->colors);
2377 view->show = show_log_view;
2378 view->input = input_log_view;
2379 view->close = close_log_view;
2380 view->search_start = search_start_log_view;
2381 view->search_next = search_next_log_view;
2383 if (s->thread_args.pack_fds == NULL) {
2384 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2388 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2389 s->thread_args.pack_fds);
2392 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2396 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2397 s->repo, NULL, NULL);
2401 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2403 err = got_error_set_errno(errcode, "pthread_cond_init");
2406 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2408 err = got_error_set_errno(errcode, "pthread_cond_init");
2412 s->thread_args.commits_needed = view->nlines;
2413 s->thread_args.graph = thread_graph;
2414 s->thread_args.commits = &s->commits;
2415 s->thread_args.in_repo_path = s->in_repo_path;
2416 s->thread_args.start_id = s->start_id;
2417 s->thread_args.repo = thread_repo;
2418 s->thread_args.log_complete = 0;
2419 s->thread_args.quit = &s->quit;
2420 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2421 s->thread_args.selected_entry = &s->selected_entry;
2422 s->thread_args.searching = &view->searching;
2423 s->thread_args.search_next_done = &view->search_next_done;
2424 s->thread_args.regex = &view->regex;
2427 close_log_view(view);
2431 static const struct got_error *
2432 show_log_view(struct tog_view *view)
2434 const struct got_error *err;
2435 struct tog_log_view_state *s = &view->state.log;
2437 if (s->thread == NULL) {
2438 int errcode = pthread_create(&s->thread, NULL, log_thread,
2441 return got_error_set_errno(errcode, "pthread_create");
2442 if (s->thread_args.commits_needed > 0) {
2443 err = trigger_log_thread(view, 1);
2449 return draw_commits(view);
2452 static const struct got_error *
2453 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2455 const struct got_error *err = NULL;
2456 struct tog_log_view_state *s = &view->state.log;
2457 struct tog_view *diff_view = NULL, *tree_view = NULL;
2458 struct tog_view *ref_view = NULL;
2459 struct commit_queue_entry *entry;
2460 int begin_x = 0, n, nscroll = view->nlines - 1;
2462 if (s->thread_args.load_all) {
2463 if (ch == KEY_BACKSPACE)
2464 s->thread_args.load_all = 0;
2465 else if (s->thread_args.log_complete) {
2466 s->thread_args.load_all = 0;
2467 log_scroll_down(view, s->commits.ncommits);
2468 s->selected = MIN(view->nlines - 2,
2469 s->commits.ncommits - 1);
2484 if (s->first_displayed_entry == NULL)
2486 if (s->selected > 0)
2489 log_scroll_up(s, 1);
2495 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2504 if (s->first_displayed_entry == NULL)
2506 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2507 s->selected = MAX(0, s->selected - nscroll - 1);
2509 log_scroll_up(s, nscroll);
2517 if (s->first_displayed_entry == NULL)
2519 if (s->selected < MIN(view->nlines - 2,
2520 s->commits.ncommits - 1))
2523 err = log_scroll_down(view, 1);
2531 /* We don't know yet how many commits, so we're forced to
2532 * traverse them all. */
2533 if (!s->thread_args.log_complete) {
2534 s->thread_args.load_all = 1;
2535 return trigger_log_thread(view, 0);
2539 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2540 for (n = 0; n < view->nlines - 1; n++) {
2543 s->first_displayed_entry = entry;
2544 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2547 s->selected = n - 1;
2557 struct commit_queue_entry *first;
2558 first = s->first_displayed_entry;
2561 err = log_scroll_down(view, nscroll);
2564 if (first == s->first_displayed_entry &&
2565 s->selected < MIN(view->nlines - 2,
2566 s->commits.ncommits - 1)) {
2567 /* can't scroll further down */
2568 s->selected += MIN(s->last_displayed_entry->idx -
2569 s->selected_entry->idx, nscroll + 1);
2575 if (s->selected > view->nlines - 2)
2576 s->selected = view->nlines - 2;
2577 if (s->selected > s->commits.ncommits - 1)
2578 s->selected = s->commits.ncommits - 1;
2580 if (s->commits.ncommits < view->nlines - 1 &&
2581 !s->thread_args.log_complete) {
2582 s->thread_args.commits_needed += (view->nlines - 1) -
2583 s->commits.ncommits;
2584 err = trigger_log_thread(view, 1);
2590 if (s->selected_entry == NULL)
2592 if (view_is_parent_view(view))
2593 begin_x = view_split_begin_x(view->begin_x);
2594 err = open_diff_view_for_commit(&diff_view, begin_x,
2595 s->selected_entry->commit, s->selected_entry->id,
2600 diff_view->focussed = 1;
2601 if (view_is_parent_view(view)) {
2602 err = view_close_child(view);
2605 view_set_child(view, diff_view);
2606 view->focus_child = 1;
2608 *new_view = diff_view;
2611 if (s->selected_entry == NULL)
2613 if (view_is_parent_view(view))
2614 begin_x = view_split_begin_x(view->begin_x);
2615 err = browse_commit_tree(&tree_view, begin_x,
2616 s->selected_entry, s->in_repo_path, s->head_ref_name,
2621 tree_view->focussed = 1;
2622 if (view_is_parent_view(view)) {
2623 err = view_close_child(view);
2626 view_set_child(view, tree_view);
2627 view->focus_child = 1;
2629 *new_view = tree_view;
2634 if (ch == KEY_BACKSPACE &&
2635 got_path_is_root_dir(s->in_repo_path))
2637 err = stop_log_thread(s);
2640 if (ch == KEY_BACKSPACE) {
2642 err = got_path_dirname(&parent_path, s->in_repo_path);
2645 free(s->in_repo_path);
2646 s->in_repo_path = parent_path;
2647 s->thread_args.in_repo_path = s->in_repo_path;
2648 } else if (ch == CTRL('l')) {
2649 struct got_object_id *start_id;
2650 err = got_repo_match_object_id(&start_id, NULL,
2651 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2652 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2656 s->start_id = start_id;
2657 s->thread_args.start_id = s->start_id;
2659 s->log_branches = !s->log_branches;
2661 err = got_repo_open(&s->thread_args.repo,
2662 got_repo_get_path(s->repo), NULL,
2663 s->thread_args.pack_fds);
2667 err = tog_load_refs(s->repo, 0);
2670 err = got_commit_graph_open(&s->thread_args.graph,
2671 s->in_repo_path, !s->log_branches);
2674 err = got_commit_graph_iter_start(s->thread_args.graph,
2675 s->start_id, s->repo, NULL, NULL);
2678 free_commits(&s->commits);
2679 s->first_displayed_entry = NULL;
2680 s->last_displayed_entry = NULL;
2681 s->selected_entry = NULL;
2683 s->thread_args.log_complete = 0;
2685 s->thread_args.commits_needed = view->nlines;
2688 if (view_is_parent_view(view))
2689 begin_x = view_split_begin_x(view->begin_x);
2690 ref_view = view_open(view->nlines, view->ncols,
2691 view->begin_y, begin_x, TOG_VIEW_REF);
2692 if (ref_view == NULL)
2693 return got_error_from_errno("view_open");
2694 err = open_ref_view(ref_view, s->repo);
2696 view_close(ref_view);
2700 ref_view->focussed = 1;
2701 if (view_is_parent_view(view)) {
2702 err = view_close_child(view);
2705 view_set_child(view, ref_view);
2706 view->focus_child = 1;
2708 *new_view = ref_view;
2717 static const struct got_error *
2718 apply_unveil(const char *repo_path, const char *worktree_path)
2720 const struct got_error *error;
2723 if (unveil("gmon.out", "rwc") != 0)
2724 return got_error_from_errno2("unveil", "gmon.out");
2726 if (repo_path && unveil(repo_path, "r") != 0)
2727 return got_error_from_errno2("unveil", repo_path);
2729 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2730 return got_error_from_errno2("unveil", worktree_path);
2732 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2733 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2735 error = got_privsep_unveil_exec_helpers();
2739 if (unveil(NULL, NULL) != 0)
2740 return got_error_from_errno("unveil");
2749 * Override default signal handlers before starting ncurses.
2750 * This should prevent ncurses from installing its own
2751 * broken cleanup() signal handler.
2753 signal(SIGWINCH, tog_sigwinch);
2754 signal(SIGPIPE, tog_sigpipe);
2755 signal(SIGCONT, tog_sigcont);
2756 signal(SIGINT, tog_sigint);
2757 signal(SIGTERM, tog_sigterm);
2761 halfdelay(1); /* Do fast refresh while initial view is loading. */
2764 intrflush(stdscr, FALSE);
2765 keypad(stdscr, TRUE);
2767 if (getenv("TOG_COLORS") != NULL) {
2769 use_default_colors();
2773 static const struct got_error *
2774 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2775 struct got_repository *repo, struct got_worktree *worktree)
2777 const struct got_error *err = NULL;
2780 *in_repo_path = strdup("/");
2781 if (*in_repo_path == NULL)
2782 return got_error_from_errno("strdup");
2787 const char *prefix = got_worktree_get_path_prefix(worktree);
2790 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2793 if (asprintf(in_repo_path, "%s%s%s", prefix,
2794 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2796 err = got_error_from_errno("asprintf");
2797 *in_repo_path = NULL;
2801 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2806 static const struct got_error *
2807 cmd_log(int argc, char *argv[])
2809 const struct got_error *error;
2810 struct got_repository *repo = NULL;
2811 struct got_worktree *worktree = NULL;
2812 struct got_object_id *start_id = NULL;
2813 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2814 char *start_commit = NULL, *label = NULL;
2815 struct got_reference *ref = NULL;
2816 const char *head_ref_name = NULL;
2817 int ch, log_branches = 0;
2818 struct tog_view *view;
2819 int *pack_fds = NULL;
2821 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2827 start_commit = optarg;
2830 repo_path = realpath(optarg, NULL);
2831 if (repo_path == NULL)
2832 return got_error_from_errno2("realpath",
2847 error = got_repo_pack_fds_open(&pack_fds);
2851 if (repo_path == NULL) {
2852 cwd = getcwd(NULL, 0);
2854 return got_error_from_errno("getcwd");
2855 error = got_worktree_open(&worktree, cwd);
2856 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2860 strdup(got_worktree_get_repo_path(worktree));
2862 repo_path = strdup(cwd);
2863 if (repo_path == NULL) {
2864 error = got_error_from_errno("strdup");
2869 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2873 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2880 error = apply_unveil(got_repo_get_path(repo),
2881 worktree ? got_worktree_get_root_path(worktree) : NULL);
2885 /* already loaded by tog_log_with_path()? */
2886 if (TAILQ_EMPTY(&tog_refs)) {
2887 error = tog_load_refs(repo, 0);
2892 if (start_commit == NULL) {
2893 error = got_repo_match_object_id(&start_id, &label,
2894 worktree ? got_worktree_get_head_ref_name(worktree) :
2895 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2898 head_ref_name = label;
2900 error = got_ref_open(&ref, repo, start_commit, 0);
2902 head_ref_name = got_ref_get_name(ref);
2903 else if (error->code != GOT_ERR_NOT_REF)
2905 error = got_repo_match_object_id(&start_id, NULL,
2906 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2911 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2913 error = got_error_from_errno("view_open");
2916 error = open_log_view(view, start_id, repo, head_ref_name,
2917 in_repo_path, log_branches);
2921 /* Release work tree lock. */
2922 got_worktree_close(worktree);
2925 error = view_loop(view);
2935 const struct got_error *close_err = got_repo_close(repo);
2940 got_worktree_close(worktree);
2942 const struct got_error *pack_err =
2943 got_repo_pack_fds_close(pack_fds);
2955 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2956 "[-w] object1 object2\n", getprogname());
2961 match_line(const char *line, regex_t *regex, size_t nmatch,
2962 regmatch_t *regmatch)
2964 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2968 match_color(struct tog_colors *colors, const char *line)
2970 struct tog_color *tc = NULL;
2972 STAILQ_FOREACH(tc, colors, entry) {
2973 if (match_line(line, &tc->regex, 0, NULL))
2980 static const struct got_error *
2981 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2982 WINDOW *window, regmatch_t *regmatch)
2984 const struct got_error *err = NULL;
2991 s = strndup(line, regmatch->rm_so);
2993 return got_error_from_errno("strndup");
2995 err = format_line(&wline, &width, s, wlimit, col_tab_align);
3000 waddwstr(window, wline);
3007 s = strndup(line + regmatch->rm_so,
3008 regmatch->rm_eo - regmatch->rm_so);
3010 err = got_error_from_errno("strndup");
3014 err = format_line(&wline, &width, s, wlimit, col_tab_align);
3019 wattr_on(window, A_STANDOUT, NULL);
3020 waddwstr(window, wline);
3021 wattr_off(window, A_STANDOUT, NULL);
3028 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
3029 err = format_line(&wline, &width,
3030 line + regmatch->rm_eo, wlimit, col_tab_align);
3033 waddwstr(window, wline);
3041 static const struct got_error *
3042 draw_file(struct tog_view *view, const char *header)
3044 struct tog_diff_view_state *s = &view->state.diff;
3045 regmatch_t *regmatch = &view->regmatch;
3046 const struct got_error *err;
3049 size_t linesize = 0;
3051 struct tog_color *tc;
3054 int max_lines = view->nlines;
3055 int nlines = s->nlines;
3058 line_offset = s->line_offsets[s->first_displayed_line - 1];
3059 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3060 return got_error_from_errno("fseek");
3062 werase(view->window);
3065 if (asprintf(&line, "[%d/%d] %s",
3066 s->first_displayed_line - 1 + s->selected_line, nlines,
3068 return got_error_from_errno("asprintf");
3069 err = format_line(&wline, &width, line, view->ncols, 0);
3074 if (view_needs_focus_indication(view))
3075 wstandout(view->window);
3076 waddwstr(view->window, wline);
3079 if (view_needs_focus_indication(view))
3080 wstandend(view->window);
3081 if (width <= view->ncols - 1)
3082 waddch(view->window, '\n');
3091 while (max_lines > 0 && nprinted < max_lines) {
3092 linelen = getline(&line, &linesize, s->f);
3093 if (linelen == -1) {
3099 return got_ferror(s->f, GOT_ERR_IO);
3102 tc = match_color(&s->colors, line);
3104 wattr_on(view->window,
3105 COLOR_PAIR(tc->colorpair), NULL);
3106 if (s->first_displayed_line + nprinted == s->matched_line &&
3107 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3108 err = add_matched_line(&width, line, view->ncols, 0,
3109 view->window, regmatch);
3115 err = format_line(&wline, &width, line, view->ncols, 0);
3120 waddwstr(view->window, wline);
3125 wattr_off(view->window,
3126 COLOR_PAIR(tc->colorpair), NULL);
3127 if (width <= view->ncols - 1)
3128 waddch(view->window, '\n');
3133 s->last_displayed_line = s->first_displayed_line +
3136 s->last_displayed_line = s->first_displayed_line;
3141 while (nprinted < view->nlines) {
3142 waddch(view->window, '\n');
3146 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3151 wstandout(view->window);
3152 waddwstr(view->window, wline);
3155 wstandend(view->window);
3162 get_datestr(time_t *time, char *datebuf)
3164 struct tm mytm, *tm;
3167 tm = gmtime_r(time, &mytm);
3170 s = asctime_r(tm, datebuf);
3173 p = strchr(s, '\n');
3179 static const struct got_error *
3180 get_changed_paths(struct got_pathlist_head *paths,
3181 struct got_commit_object *commit, struct got_repository *repo)
3183 const struct got_error *err = NULL;
3184 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3185 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3186 struct got_object_qid *qid;
3188 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3190 struct got_commit_object *pcommit;
3191 err = got_object_open_as_commit(&pcommit, repo,
3196 tree_id1 = got_object_id_dup(
3197 got_object_commit_get_tree_id(pcommit));
3198 if (tree_id1 == NULL) {
3199 got_object_commit_close(pcommit);
3200 return got_error_from_errno("got_object_id_dup");
3202 got_object_commit_close(pcommit);
3207 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3212 tree_id2 = got_object_commit_get_tree_id(commit);
3213 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3217 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3218 got_diff_tree_collect_changed_paths, paths, 0);
3221 got_object_tree_close(tree1);
3223 got_object_tree_close(tree2);
3228 static const struct got_error *
3229 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3233 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3235 return got_error_from_errno("reallocarray");
3237 (*line_offsets)[*nlines] = off;
3242 static const struct got_error *
3243 write_commit_info(off_t **line_offsets, size_t *nlines,
3244 struct got_object_id *commit_id, struct got_reflist_head *refs,
3245 struct got_repository *repo, FILE *outfile)
3247 const struct got_error *err = NULL;
3248 char datebuf[26], *datestr;
3249 struct got_commit_object *commit;
3250 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3251 time_t committer_time;
3252 const char *author, *committer;
3253 char *refs_str = NULL;
3254 struct got_pathlist_head changed_paths;
3255 struct got_pathlist_entry *pe;
3259 TAILQ_INIT(&changed_paths);
3262 err = build_refs_str(&refs_str, refs, commit_id, repo);
3267 err = got_object_open_as_commit(&commit, repo, commit_id);
3271 err = got_object_id_str(&id_str, commit_id);
3273 err = got_error_from_errno("got_object_id_str");
3277 err = add_line_offset(line_offsets, nlines, 0);
3281 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3282 refs_str ? refs_str : "", refs_str ? ")" : "");
3284 err = got_error_from_errno("fprintf");
3288 err = add_line_offset(line_offsets, nlines, outoff);
3292 n = fprintf(outfile, "from: %s\n",
3293 got_object_commit_get_author(commit));
3295 err = got_error_from_errno("fprintf");
3299 err = add_line_offset(line_offsets, nlines, outoff);
3303 committer_time = got_object_commit_get_committer_time(commit);
3304 datestr = get_datestr(&committer_time, datebuf);
3306 n = fprintf(outfile, "date: %s UTC\n", datestr);
3308 err = got_error_from_errno("fprintf");
3312 err = add_line_offset(line_offsets, nlines, outoff);
3316 author = got_object_commit_get_author(commit);
3317 committer = got_object_commit_get_committer(commit);
3318 if (strcmp(author, committer) != 0) {
3319 n = fprintf(outfile, "via: %s\n", committer);
3321 err = got_error_from_errno("fprintf");
3325 err = add_line_offset(line_offsets, nlines, outoff);
3329 if (got_object_commit_get_nparents(commit) > 1) {
3330 const struct got_object_id_queue *parent_ids;
3331 struct got_object_qid *qid;
3333 parent_ids = got_object_commit_get_parent_ids(commit);
3334 STAILQ_FOREACH(qid, parent_ids, entry) {
3335 err = got_object_id_str(&id_str, &qid->id);
3338 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3340 err = got_error_from_errno("fprintf");
3344 err = add_line_offset(line_offsets, nlines, outoff);
3352 err = got_object_commit_get_logmsg(&logmsg, commit);
3356 while ((line = strsep(&s, "\n")) != NULL) {
3357 n = fprintf(outfile, "%s\n", line);
3359 err = got_error_from_errno("fprintf");
3363 err = add_line_offset(line_offsets, nlines, outoff);
3368 err = get_changed_paths(&changed_paths, commit, repo);
3371 TAILQ_FOREACH(pe, &changed_paths, entry) {
3372 struct got_diff_changed_path *cp = pe->data;
3373 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3375 err = got_error_from_errno("fprintf");
3379 err = add_line_offset(line_offsets, nlines, outoff);
3382 free((char *)pe->path);
3386 fputc('\n', outfile);
3388 err = add_line_offset(line_offsets, nlines, outoff);
3390 got_pathlist_free(&changed_paths);
3394 got_object_commit_close(commit);
3396 free(*line_offsets);
3397 *line_offsets = NULL;
3403 static const struct got_error *
3404 create_diff(struct tog_diff_view_state *s)
3406 const struct got_error *err = NULL;
3410 free(s->line_offsets);
3411 s->line_offsets = malloc(sizeof(off_t));
3412 if (s->line_offsets == NULL)
3413 return got_error_from_errno("malloc");
3418 err = got_error_from_errno("got_opentemp");
3421 if (s->f && fclose(s->f) == EOF) {
3422 err = got_error_from_errno("fclose");
3428 err = got_object_get_type(&obj_type, s->repo, s->id1);
3430 err = got_object_get_type(&obj_type, s->repo, s->id2);
3435 case GOT_OBJ_TYPE_BLOB:
3436 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3437 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3438 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3441 case GOT_OBJ_TYPE_TREE:
3442 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3443 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3444 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3446 case GOT_OBJ_TYPE_COMMIT: {
3447 const struct got_object_id_queue *parent_ids;
3448 struct got_object_qid *pid;
3449 struct got_commit_object *commit2;
3450 struct got_reflist_head *refs;
3452 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3455 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3456 /* Show commit info if we're diffing to a parent/root commit. */
3457 if (s->id1 == NULL) {
3458 err = write_commit_info(&s->line_offsets, &s->nlines,
3459 s->id2, refs, s->repo, s->f);
3463 parent_ids = got_object_commit_get_parent_ids(commit2);
3464 STAILQ_FOREACH(pid, parent_ids, entry) {
3465 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3466 err = write_commit_info(
3467 &s->line_offsets, &s->nlines,
3468 s->id2, refs, s->repo, s->f);
3475 got_object_commit_close(commit2);
3477 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3478 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3479 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3483 err = got_error(GOT_ERR_OBJ_TYPE);
3489 if (s->f && fflush(s->f) != 0 && err == NULL)
3490 err = got_error_from_errno("fflush");
3495 diff_view_indicate_progress(struct tog_view *view)
3497 mvwaddstr(view->window, 0, 0, "diffing...");
3502 static const struct got_error *
3503 search_start_diff_view(struct tog_view *view)
3505 struct tog_diff_view_state *s = &view->state.diff;
3507 s->matched_line = 0;
3511 static const struct got_error *
3512 search_next_diff_view(struct tog_view *view)
3514 struct tog_diff_view_state *s = &view->state.diff;
3517 size_t linesize = 0;
3520 if (!view->searching) {
3521 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3525 if (s->matched_line) {
3526 if (view->searching == TOG_SEARCH_FORWARD)
3527 lineno = s->matched_line + 1;
3529 lineno = s->matched_line - 1;
3531 lineno = s->first_displayed_line;
3536 if (lineno <= 0 || lineno > s->nlines) {
3537 if (s->matched_line == 0) {
3538 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3542 if (view->searching == TOG_SEARCH_FORWARD)
3548 offset = s->line_offsets[lineno - 1];
3549 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3551 return got_error_from_errno("fseeko");
3553 linelen = getline(&line, &linesize, s->f);
3554 if (linelen != -1 &&
3555 match_line(line, &view->regex, 1, &view->regmatch)) {
3556 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3557 s->matched_line = lineno;
3560 if (view->searching == TOG_SEARCH_FORWARD)
3567 if (s->matched_line) {
3568 s->first_displayed_line = s->matched_line;
3569 s->selected_line = 1;
3575 static const struct got_error *
3576 close_diff_view(struct tog_view *view)
3578 const struct got_error *err = NULL;
3579 struct tog_diff_view_state *s = &view->state.diff;
3585 if (s->f && fclose(s->f) == EOF)
3586 err = got_error_from_errno("fclose");
3588 if (s->f1 && fclose(s->f1) == EOF)
3589 err = got_error_from_errno("fclose");
3591 if (s->f2 && fclose(s->f2) == EOF)
3592 err = got_error_from_errno("fclose");
3594 free_colors(&s->colors);
3595 free(s->line_offsets);
3596 s->line_offsets = NULL;
3601 static const struct got_error *
3602 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3603 struct got_object_id *id2, const char *label1, const char *label2,
3604 int diff_context, int ignore_whitespace, int force_text_diff,
3605 struct tog_view *log_view, struct got_repository *repo)
3607 const struct got_error *err;
3608 struct tog_diff_view_state *s = &view->state.diff;
3610 memset(s, 0, sizeof(*s));
3612 if (id1 != NULL && id2 != NULL) {
3614 err = got_object_get_type(&type1, repo, id1);
3617 err = got_object_get_type(&type2, repo, id2);
3622 return got_error(GOT_ERR_OBJ_TYPE);
3624 s->first_displayed_line = 1;
3625 s->last_displayed_line = view->nlines;
3626 s->selected_line = 1;
3634 s->id1 = got_object_id_dup(id1);
3636 return got_error_from_errno("got_object_id_dup");
3637 s->f1 = got_opentemp();
3638 if (s->f1 == NULL) {
3639 err = got_error_from_errno("got_opentemp");
3645 s->id2 = got_object_id_dup(id2);
3646 if (s->id2 == NULL) {
3647 err = got_error_from_errno("got_object_id_dup");
3651 s->f2 = got_opentemp();
3652 if (s->f2 == NULL) {
3653 err = got_error_from_errno("got_opentemp");
3657 s->first_displayed_line = 1;
3658 s->last_displayed_line = view->nlines;
3659 s->diff_context = diff_context;
3660 s->ignore_whitespace = ignore_whitespace;
3661 s->force_text_diff = force_text_diff;
3662 s->log_view = log_view;
3665 STAILQ_INIT(&s->colors);
3666 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3667 err = add_color(&s->colors,
3668 "^-", TOG_COLOR_DIFF_MINUS,
3669 get_color_value("TOG_COLOR_DIFF_MINUS"));
3672 err = add_color(&s->colors, "^\\+",
3673 TOG_COLOR_DIFF_PLUS,
3674 get_color_value("TOG_COLOR_DIFF_PLUS"));
3677 err = add_color(&s->colors,
3678 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3679 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3683 err = add_color(&s->colors,
3684 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3685 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3686 get_color_value("TOG_COLOR_DIFF_META"));
3690 err = add_color(&s->colors,
3691 "^(from|via): ", TOG_COLOR_AUTHOR,
3692 get_color_value("TOG_COLOR_AUTHOR"));
3696 err = add_color(&s->colors,
3697 "^date: ", TOG_COLOR_DATE,
3698 get_color_value("TOG_COLOR_DATE"));
3703 if (log_view && view_is_splitscreen(view))
3704 show_log_view(log_view); /* draw vborder */
3705 diff_view_indicate_progress(view);
3707 err = create_diff(s);
3709 view->show = show_diff_view;
3710 view->input = input_diff_view;
3711 view->close = close_diff_view;
3712 view->search_start = search_start_diff_view;
3713 view->search_next = search_next_diff_view;
3716 close_diff_view(view);
3720 static const struct got_error *
3721 show_diff_view(struct tog_view *view)
3723 const struct got_error *err;
3724 struct tog_diff_view_state *s = &view->state.diff;
3725 char *id_str1 = NULL, *id_str2, *header;
3726 const char *label1, *label2;
3729 err = got_object_id_str(&id_str1, s->id1);
3732 label1 = s->label1 ? : id_str1;
3734 label1 = "/dev/null";
3736 err = got_object_id_str(&id_str2, s->id2);
3739 label2 = s->label2 ? : id_str2;
3741 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3742 err = got_error_from_errno("asprintf");
3750 err = draw_file(view, header);
3755 static const struct got_error *
3756 set_selected_commit(struct tog_diff_view_state *s,
3757 struct commit_queue_entry *entry)
3759 const struct got_error *err;
3760 const struct got_object_id_queue *parent_ids;
3761 struct got_commit_object *selected_commit;
3762 struct got_object_qid *pid;
3765 s->id2 = got_object_id_dup(entry->id);
3767 return got_error_from_errno("got_object_id_dup");
3769 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3772 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3774 pid = STAILQ_FIRST(parent_ids);
3775 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3776 got_object_commit_close(selected_commit);
3780 static const struct got_error *
3781 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3783 const struct got_error *err = NULL;
3784 struct tog_diff_view_state *s = &view->state.diff;
3785 struct tog_log_view_state *ls;
3786 struct commit_queue_entry *old_selected_entry;
3788 size_t linesize = 0;
3790 int i, nscroll = view->nlines - 1;
3796 s->force_text_diff = !s->force_text_diff;
3798 s->ignore_whitespace = !s->ignore_whitespace;
3799 wclear(view->window);
3800 s->first_displayed_line = 1;
3801 s->last_displayed_line = view->nlines;
3802 s->matched_line = 0;
3803 diff_view_indicate_progress(view);
3804 err = create_diff(s);
3808 s->first_displayed_line = 1;
3815 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3821 if (s->first_displayed_line > 1)
3822 s->first_displayed_line--;
3830 if (s->first_displayed_line == 1)
3833 while (i++ < nscroll && s->first_displayed_line > 1)
3834 s->first_displayed_line--;
3840 s->first_displayed_line++;
3852 while (!s->eof && i++ < nscroll) {
3853 linelen = getline(&line, &linesize, s->f);
3854 s->first_displayed_line++;
3855 if (linelen == -1) {
3859 err = got_ferror(s->f, GOT_ERR_IO);
3866 if (s->diff_context > 0) {
3868 s->matched_line = 0;
3869 diff_view_indicate_progress(view);
3870 err = create_diff(s);
3871 if (s->first_displayed_line + view->nlines - 1 >
3873 s->first_displayed_line = 1;
3874 s->last_displayed_line = view->nlines;
3879 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3881 s->matched_line = 0;
3882 diff_view_indicate_progress(view);
3883 err = create_diff(s);
3888 if (s->log_view == NULL)
3890 ls = &s->log_view->state.log;
3891 old_selected_entry = ls->selected_entry;
3893 err = input_log_view(NULL, s->log_view, KEY_UP);
3897 if (old_selected_entry == ls->selected_entry)
3900 err = set_selected_commit(s, ls->selected_entry);
3904 s->first_displayed_line = 1;
3905 s->last_displayed_line = view->nlines;
3906 s->matched_line = 0;
3908 diff_view_indicate_progress(view);
3909 err = create_diff(s);
3913 if (s->log_view == NULL)
3915 ls = &s->log_view->state.log;
3916 old_selected_entry = ls->selected_entry;
3918 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3922 if (old_selected_entry == ls->selected_entry)
3925 err = set_selected_commit(s, ls->selected_entry);
3929 s->first_displayed_line = 1;
3930 s->last_displayed_line = view->nlines;
3931 s->matched_line = 0;
3933 diff_view_indicate_progress(view);
3934 err = create_diff(s);
3943 static const struct got_error *
3944 cmd_diff(int argc, char *argv[])
3946 const struct got_error *error = NULL;
3947 struct got_repository *repo = NULL;
3948 struct got_worktree *worktree = NULL;
3949 struct got_object_id *id1 = NULL, *id2 = NULL;
3950 char *repo_path = NULL, *cwd = NULL;
3951 char *id_str1 = NULL, *id_str2 = NULL;
3952 char *label1 = NULL, *label2 = NULL;
3953 int diff_context = 3, ignore_whitespace = 0;
3954 int ch, force_text_diff = 0;
3956 struct tog_view *view;
3957 int *pack_fds = NULL;
3959 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3962 force_text_diff = 1;
3965 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3968 errx(1, "number of context lines is %s: %s",
3972 repo_path = realpath(optarg, NULL);
3973 if (repo_path == NULL)
3974 return got_error_from_errno2("realpath",
3976 got_path_strip_trailing_slashes(repo_path);
3979 ignore_whitespace = 1;
3991 usage_diff(); /* TODO show local worktree changes */
3992 } else if (argc == 2) {
3998 error = got_repo_pack_fds_open(&pack_fds);
4002 if (repo_path == NULL) {
4003 cwd = getcwd(NULL, 0);
4005 return got_error_from_errno("getcwd");
4006 error = got_worktree_open(&worktree, cwd);
4007 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4011 strdup(got_worktree_get_repo_path(worktree));
4013 repo_path = strdup(cwd);
4014 if (repo_path == NULL) {
4015 error = got_error_from_errno("strdup");
4020 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4026 error = apply_unveil(got_repo_get_path(repo), NULL);
4030 error = tog_load_refs(repo, 0);
4034 error = got_repo_match_object_id(&id1, &label1, id_str1,
4035 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4039 error = got_repo_match_object_id(&id2, &label2, id_str2,
4040 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4044 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4046 error = got_error_from_errno("view_open");
4049 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4050 ignore_whitespace, force_text_diff, NULL, repo);
4053 error = view_loop(view);
4060 const struct got_error *close_err = got_repo_close(repo);
4065 got_worktree_close(worktree);
4067 const struct got_error *pack_err =
4068 got_repo_pack_fds_close(pack_fds);
4080 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4085 struct tog_blame_line {
4087 struct got_object_id *id;
4090 static const struct got_error *
4091 draw_blame(struct tog_view *view)
4093 struct tog_blame_view_state *s = &view->state.blame;
4094 struct tog_blame *blame = &s->blame;
4095 regmatch_t *regmatch = &view->regmatch;
4096 const struct got_error *err;
4097 int lineno = 0, nprinted = 0;
4099 size_t linesize = 0;
4103 struct tog_blame_line *blame_line;
4104 struct got_object_id *prev_id = NULL;
4106 struct tog_color *tc;
4108 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4113 werase(view->window);
4115 if (asprintf(&line, "commit %s", id_str) == -1) {
4116 err = got_error_from_errno("asprintf");
4121 err = format_line(&wline, &width, line, view->ncols, 0);
4126 if (view_needs_focus_indication(view))
4127 wstandout(view->window);
4128 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4130 wattr_on(view->window,
4131 COLOR_PAIR(tc->colorpair), NULL);
4132 waddwstr(view->window, wline);
4134 wattr_off(view->window,
4135 COLOR_PAIR(tc->colorpair), NULL);
4136 if (view_needs_focus_indication(view))
4137 wstandend(view->window);
4140 if (width < view->ncols - 1)
4141 waddch(view->window, '\n');
4143 if (asprintf(&line, "[%d/%d] %s%s",
4144 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4145 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4147 return got_error_from_errno("asprintf");
4150 err = format_line(&wline, &width, line, view->ncols, 0);
4155 waddwstr(view->window, wline);
4158 if (width < view->ncols - 1)
4159 waddch(view->window, '\n');
4162 while (nprinted < view->nlines - 2) {
4163 linelen = getline(&line, &linesize, blame->f);
4164 if (linelen == -1) {
4165 if (feof(blame->f)) {
4170 return got_ferror(blame->f, GOT_ERR_IO);
4172 if (++lineno < s->first_displayed_line)
4175 if (view->focussed && nprinted == s->selected_line - 1)
4176 wstandout(view->window);
4178 if (blame->nlines > 0) {
4179 blame_line = &blame->lines[lineno - 1];
4180 if (blame_line->annotated && prev_id &&
4181 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4183 nprinted == s->selected_line - 1)) {
4184 waddstr(view->window, " ");
4185 } else if (blame_line->annotated) {
4187 err = got_object_id_str(&id_str, blame_line->id);
4192 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4194 wattr_on(view->window,
4195 COLOR_PAIR(tc->colorpair), NULL);
4196 wprintw(view->window, "%.8s", id_str);
4198 wattr_off(view->window,
4199 COLOR_PAIR(tc->colorpair), NULL);
4201 prev_id = blame_line->id;
4203 waddstr(view->window, "........");
4207 waddstr(view->window, "........");
4211 if (view->focussed && nprinted == s->selected_line - 1)
4212 wstandend(view->window);
4213 waddstr(view->window, " ");
4215 if (view->ncols <= 9) {
4217 wline = wcsdup(L"");
4218 if (wline == NULL) {
4219 err = got_error_from_errno("wcsdup");
4223 } else if (s->first_displayed_line + nprinted ==
4225 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4226 err = add_matched_line(&width, line, view->ncols - 9, 9,
4227 view->window, regmatch);
4234 err = format_line(&wline, &width, line,
4235 view->ncols - 9, 9);
4236 waddwstr(view->window, wline);
4242 if (width <= view->ncols - 1)
4243 waddch(view->window, '\n');
4244 if (++nprinted == 1)
4245 s->first_displayed_line = lineno;
4248 s->last_displayed_line = lineno;
4255 static const struct got_error *
4256 blame_cb(void *arg, int nlines, int lineno,
4257 struct got_commit_object *commit, struct got_object_id *id)
4259 const struct got_error *err = NULL;
4260 struct tog_blame_cb_args *a = arg;
4261 struct tog_blame_line *line;
4264 if (nlines != a->nlines ||
4265 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4266 return got_error(GOT_ERR_RANGE);
4268 errcode = pthread_mutex_lock(&tog_mutex);
4270 return got_error_set_errno(errcode, "pthread_mutex_lock");
4272 if (*a->quit) { /* user has quit the blame view */
4273 err = got_error(GOT_ERR_ITER_COMPLETED);
4278 goto done; /* no change in this commit */
4280 line = &a->lines[lineno - 1];
4281 if (line->annotated)
4284 line->id = got_object_id_dup(id);
4285 if (line->id == NULL) {
4286 err = got_error_from_errno("got_object_id_dup");
4289 line->annotated = 1;
4291 errcode = pthread_mutex_unlock(&tog_mutex);
4293 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4298 blame_thread(void *arg)
4300 const struct got_error *err, *close_err;
4301 struct tog_blame_thread_args *ta = arg;
4302 struct tog_blame_cb_args *a = ta->cb_args;
4305 err = block_signals_used_by_main_thread();
4309 err = got_blame(ta->path, a->commit_id, ta->repo,
4310 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4311 if (err && err->code == GOT_ERR_CANCELLED)
4314 errcode = pthread_mutex_lock(&tog_mutex);
4316 return (void *)got_error_set_errno(errcode,
4317 "pthread_mutex_lock");
4319 close_err = got_repo_close(ta->repo);
4325 errcode = pthread_mutex_unlock(&tog_mutex);
4326 if (errcode && err == NULL)
4327 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4332 static struct got_object_id *
4333 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4334 int first_displayed_line, int selected_line)
4336 struct tog_blame_line *line;
4341 line = &lines[first_displayed_line - 1 + selected_line - 1];
4342 if (!line->annotated)
4348 static const struct got_error *
4349 stop_blame(struct tog_blame *blame)
4351 const struct got_error *err = NULL;
4354 if (blame->thread) {
4356 errcode = pthread_mutex_unlock(&tog_mutex);
4358 return got_error_set_errno(errcode,
4359 "pthread_mutex_unlock");
4360 errcode = pthread_join(blame->thread, (void **)&err);
4362 return got_error_set_errno(errcode, "pthread_join");
4363 errcode = pthread_mutex_lock(&tog_mutex);
4365 return got_error_set_errno(errcode,
4366 "pthread_mutex_lock");
4367 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4369 blame->thread = NULL;
4371 if (blame->thread_args.repo) {
4372 const struct got_error *close_err;
4373 close_err = got_repo_close(blame->thread_args.repo);
4376 blame->thread_args.repo = NULL;
4379 if (fclose(blame->f) == EOF && err == NULL)
4380 err = got_error_from_errno("fclose");
4384 for (i = 0; i < blame->nlines; i++)
4385 free(blame->lines[i].id);
4387 blame->lines = NULL;
4389 free(blame->cb_args.commit_id);
4390 blame->cb_args.commit_id = NULL;
4391 if (blame->pack_fds) {
4392 const struct got_error *pack_err =
4393 got_repo_pack_fds_close(blame->pack_fds);
4396 blame->pack_fds = NULL;
4401 static const struct got_error *
4402 cancel_blame_view(void *arg)
4404 const struct got_error *err = NULL;
4408 errcode = pthread_mutex_lock(&tog_mutex);
4410 return got_error_set_errno(errcode,
4411 "pthread_mutex_unlock");
4414 err = got_error(GOT_ERR_CANCELLED);
4416 errcode = pthread_mutex_unlock(&tog_mutex);
4418 return got_error_set_errno(errcode,
4419 "pthread_mutex_lock");
4424 static const struct got_error *
4425 run_blame(struct tog_view *view)
4427 struct tog_blame_view_state *s = &view->state.blame;
4428 struct tog_blame *blame = &s->blame;
4429 const struct got_error *err = NULL;
4430 struct got_commit_object *commit = NULL;
4431 struct got_blob_object *blob = NULL;
4432 struct got_repository *thread_repo = NULL;
4433 struct got_object_id *obj_id = NULL;
4435 int *pack_fds = NULL;
4437 err = got_object_open_as_commit(&commit, s->repo,
4438 &s->blamed_commit->id);
4442 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4446 err = got_object_get_type(&obj_type, s->repo, obj_id);
4450 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4451 err = got_error(GOT_ERR_OBJ_TYPE);
4455 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4458 blame->f = got_opentemp();
4459 if (blame->f == NULL) {
4460 err = got_error_from_errno("got_opentemp");
4463 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4464 &blame->line_offsets, blame->f, blob);
4467 if (blame->nlines == 0) {
4468 s->blame_complete = 1;
4472 /* Don't include \n at EOF in the blame line count. */
4473 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4476 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4477 if (blame->lines == NULL) {
4478 err = got_error_from_errno("calloc");
4482 err = got_repo_pack_fds_open(&pack_fds);
4485 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4490 blame->pack_fds = pack_fds;
4491 blame->cb_args.view = view;
4492 blame->cb_args.lines = blame->lines;
4493 blame->cb_args.nlines = blame->nlines;
4494 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4495 if (blame->cb_args.commit_id == NULL) {
4496 err = got_error_from_errno("got_object_id_dup");
4499 blame->cb_args.quit = &s->done;
4501 blame->thread_args.path = s->path;
4502 blame->thread_args.repo = thread_repo;
4503 blame->thread_args.cb_args = &blame->cb_args;
4504 blame->thread_args.complete = &s->blame_complete;
4505 blame->thread_args.cancel_cb = cancel_blame_view;
4506 blame->thread_args.cancel_arg = &s->done;
4507 s->blame_complete = 0;
4509 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4510 s->first_displayed_line = 1;
4511 s->last_displayed_line = view->nlines;
4512 s->selected_line = 1;
4514 s->matched_line = 0;
4518 got_object_commit_close(commit);
4520 got_object_blob_close(blob);
4527 static const struct got_error *
4528 open_blame_view(struct tog_view *view, char *path,
4529 struct got_object_id *commit_id, struct got_repository *repo)
4531 const struct got_error *err = NULL;
4532 struct tog_blame_view_state *s = &view->state.blame;
4534 STAILQ_INIT(&s->blamed_commits);
4536 s->path = strdup(path);
4537 if (s->path == NULL)
4538 return got_error_from_errno("strdup");
4540 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4546 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4547 s->first_displayed_line = 1;
4548 s->last_displayed_line = view->nlines;
4549 s->selected_line = 1;
4550 s->blame_complete = 0;
4552 s->commit_id = commit_id;
4553 memset(&s->blame, 0, sizeof(s->blame));
4555 STAILQ_INIT(&s->colors);
4556 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4557 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4558 get_color_value("TOG_COLOR_COMMIT"));
4563 view->show = show_blame_view;
4564 view->input = input_blame_view;
4565 view->close = close_blame_view;
4566 view->search_start = search_start_blame_view;
4567 view->search_next = search_next_blame_view;
4569 return run_blame(view);
4572 static const struct got_error *
4573 close_blame_view(struct tog_view *view)
4575 const struct got_error *err = NULL;
4576 struct tog_blame_view_state *s = &view->state.blame;
4578 if (s->blame.thread)
4579 err = stop_blame(&s->blame);
4581 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4582 struct got_object_qid *blamed_commit;
4583 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4584 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4585 got_object_qid_free(blamed_commit);
4589 free_colors(&s->colors);
4593 static const struct got_error *
4594 search_start_blame_view(struct tog_view *view)
4596 struct tog_blame_view_state *s = &view->state.blame;
4598 s->matched_line = 0;
4602 static const struct got_error *
4603 search_next_blame_view(struct tog_view *view)
4605 struct tog_blame_view_state *s = &view->state.blame;
4608 size_t linesize = 0;
4611 if (!view->searching) {
4612 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4616 if (s->matched_line) {
4617 if (view->searching == TOG_SEARCH_FORWARD)
4618 lineno = s->matched_line + 1;
4620 lineno = s->matched_line - 1;
4622 lineno = s->first_displayed_line - 1 + s->selected_line;
4627 if (lineno <= 0 || lineno > s->blame.nlines) {
4628 if (s->matched_line == 0) {
4629 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4633 if (view->searching == TOG_SEARCH_FORWARD)
4636 lineno = s->blame.nlines;
4639 offset = s->blame.line_offsets[lineno - 1];
4640 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4642 return got_error_from_errno("fseeko");
4644 linelen = getline(&line, &linesize, s->blame.f);
4645 if (linelen != -1 &&
4646 match_line(line, &view->regex, 1, &view->regmatch)) {
4647 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4648 s->matched_line = lineno;
4651 if (view->searching == TOG_SEARCH_FORWARD)
4658 if (s->matched_line) {
4659 s->first_displayed_line = s->matched_line;
4660 s->selected_line = 1;
4666 static const struct got_error *
4667 show_blame_view(struct tog_view *view)
4669 const struct got_error *err = NULL;
4670 struct tog_blame_view_state *s = &view->state.blame;
4673 if (s->blame.thread == NULL && !s->blame_complete) {
4674 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4675 &s->blame.thread_args);
4677 return got_error_set_errno(errcode, "pthread_create");
4679 halfdelay(1); /* fast refresh while annotating */
4682 if (s->blame_complete)
4683 halfdelay(10); /* disable fast refresh */
4685 err = draw_blame(view);
4691 static const struct got_error *
4692 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4694 const struct got_error *err = NULL, *thread_err = NULL;
4695 struct tog_view *diff_view;
4696 struct tog_blame_view_state *s = &view->state.blame;
4697 int begin_x = 0, nscroll = view->nlines - 2;
4705 s->selected_line = 1;
4706 s->first_displayed_line = 1;
4710 if (s->blame.nlines < view->nlines - 2) {
4711 s->selected_line = s->blame.nlines;
4712 s->first_displayed_line = 1;
4714 s->selected_line = view->nlines - 2;
4715 s->first_displayed_line = s->blame.nlines -
4722 if (s->selected_line > 1)
4724 else if (s->selected_line == 1 &&
4725 s->first_displayed_line > 1)
4726 s->first_displayed_line--;
4734 if (s->first_displayed_line == 1) {
4735 s->selected_line = MAX(1, s->selected_line - nscroll);
4738 if (s->first_displayed_line > nscroll)
4739 s->first_displayed_line -= nscroll;
4741 s->first_displayed_line = 1;
4746 if (s->selected_line < view->nlines - 2 &&
4747 s->first_displayed_line +
4748 s->selected_line <= s->blame.nlines)
4750 else if (s->last_displayed_line <
4752 s->first_displayed_line++;
4756 struct got_object_id *id = NULL;
4757 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4758 s->first_displayed_line, s->selected_line);
4762 struct got_commit_object *commit, *pcommit;
4763 struct got_object_qid *pid;
4764 struct got_object_id *blob_id = NULL;
4766 err = got_object_open_as_commit(&commit,
4771 got_object_commit_get_parent_ids(commit));
4773 got_object_commit_close(commit);
4776 /* Check if path history ends here. */
4777 err = got_object_open_as_commit(&pcommit,
4781 err = got_object_id_by_path(&blob_id, s->repo,
4783 got_object_commit_close(pcommit);
4785 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4787 got_object_commit_close(commit);
4790 err = got_object_get_type(&obj_type, s->repo,
4793 /* Can't blame non-blob type objects. */
4794 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4795 got_object_commit_close(commit);
4798 err = got_object_qid_alloc(&s->blamed_commit,
4800 got_object_commit_close(commit);
4802 if (got_object_id_cmp(id,
4803 &s->blamed_commit->id) == 0)
4805 err = got_object_qid_alloc(&s->blamed_commit,
4811 thread_err = stop_blame(&s->blame);
4815 STAILQ_INSERT_HEAD(&s->blamed_commits,
4816 s->blamed_commit, entry);
4817 err = run_blame(view);
4823 struct got_object_qid *first;
4824 first = STAILQ_FIRST(&s->blamed_commits);
4825 if (!got_object_id_cmp(&first->id, s->commit_id))
4828 thread_err = stop_blame(&s->blame);
4832 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4833 got_object_qid_free(s->blamed_commit);
4835 STAILQ_FIRST(&s->blamed_commits);
4836 err = run_blame(view);
4843 struct got_object_id *id = NULL;
4844 struct got_object_qid *pid;
4845 struct got_commit_object *commit = NULL;
4846 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4847 s->first_displayed_line, s->selected_line);
4850 err = got_object_open_as_commit(&commit, s->repo, id);
4854 got_object_commit_get_parent_ids(commit));
4855 if (view_is_parent_view(view))
4856 begin_x = view_split_begin_x(view->begin_x);
4857 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4858 if (diff_view == NULL) {
4859 got_object_commit_close(commit);
4860 err = got_error_from_errno("view_open");
4863 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4864 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4865 got_object_commit_close(commit);
4867 view_close(diff_view);
4871 diff_view->focussed = 1;
4872 if (view_is_parent_view(view)) {
4873 err = view_close_child(view);
4876 view_set_child(view, diff_view);
4877 view->focus_child = 1;
4879 *new_view = diff_view;
4891 if (s->last_displayed_line >= s->blame.nlines &&
4892 s->selected_line >= MIN(s->blame.nlines,
4893 view->nlines - 2)) {
4896 if (s->last_displayed_line >= s->blame.nlines &&
4897 s->selected_line < view->nlines - 2) {
4899 MIN(nscroll, s->last_displayed_line -
4900 s->first_displayed_line - s->selected_line + 1);
4902 if (s->last_displayed_line + nscroll <= s->blame.nlines)
4903 s->first_displayed_line += nscroll;
4905 s->first_displayed_line =
4906 s->blame.nlines - (view->nlines - 3);
4909 if (s->selected_line > view->nlines - 2) {
4910 s->selected_line = MIN(s->blame.nlines,
4917 return thread_err ? thread_err : err;
4920 static const struct got_error *
4921 cmd_blame(int argc, char *argv[])
4923 const struct got_error *error;
4924 struct got_repository *repo = NULL;
4925 struct got_worktree *worktree = NULL;
4926 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4927 char *link_target = NULL;
4928 struct got_object_id *commit_id = NULL;
4929 struct got_commit_object *commit = NULL;
4930 char *commit_id_str = NULL;
4932 struct tog_view *view;
4933 int *pack_fds = NULL;
4935 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4938 commit_id_str = optarg;
4941 repo_path = realpath(optarg, NULL);
4942 if (repo_path == NULL)
4943 return got_error_from_errno2("realpath",
4958 error = got_repo_pack_fds_open(&pack_fds);
4962 if (repo_path == NULL) {
4963 cwd = getcwd(NULL, 0);
4965 return got_error_from_errno("getcwd");
4966 error = got_worktree_open(&worktree, cwd);
4967 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4971 strdup(got_worktree_get_repo_path(worktree));
4973 repo_path = strdup(cwd);
4974 if (repo_path == NULL) {
4975 error = got_error_from_errno("strdup");
4980 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4984 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4991 error = apply_unveil(got_repo_get_path(repo), NULL);
4995 error = tog_load_refs(repo, 0);
4999 if (commit_id_str == NULL) {
5000 struct got_reference *head_ref;
5001 error = got_ref_open(&head_ref, repo, worktree ?
5002 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5005 error = got_ref_resolve(&commit_id, repo, head_ref);
5006 got_ref_close(head_ref);
5008 error = got_repo_match_object_id(&commit_id, NULL,
5009 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5014 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5016 error = got_error_from_errno("view_open");
5020 error = got_object_open_as_commit(&commit, repo, commit_id);
5024 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5029 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5034 /* Release work tree lock. */
5035 got_worktree_close(worktree);
5038 error = view_loop(view);
5046 got_object_commit_close(commit);
5048 got_worktree_close(worktree);
5050 const struct got_error *close_err = got_repo_close(repo);
5055 const struct got_error *pack_err =
5056 got_repo_pack_fds_close(pack_fds);
5064 static const struct got_error *
5065 draw_tree_entries(struct tog_view *view, const char *parent_path)
5067 struct tog_tree_view_state *s = &view->state.tree;
5068 const struct got_error *err = NULL;
5069 struct got_tree_entry *te;
5071 struct tog_color *tc;
5072 int width, n, i, nentries;
5073 int limit = view->nlines;
5077 werase(view->window);
5082 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
5085 if (view_needs_focus_indication(view))
5086 wstandout(view->window);
5087 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5089 wattr_on(view->window,
5090 COLOR_PAIR(tc->colorpair), NULL);
5091 waddwstr(view->window, wline);
5093 wattr_off(view->window,
5094 COLOR_PAIR(tc->colorpair), NULL);
5095 if (view_needs_focus_indication(view))
5096 wstandend(view->window);
5099 if (width < view->ncols - 1)
5100 waddch(view->window, '\n');
5103 err = format_line(&wline, &width, parent_path, view->ncols, 0);
5106 waddwstr(view->window, wline);
5109 if (width < view->ncols - 1)
5110 waddch(view->window, '\n');
5113 waddch(view->window, '\n');
5117 if (s->first_displayed_entry == NULL) {
5118 te = got_object_tree_get_first_entry(s->tree);
5119 if (s->selected == 0) {
5121 wstandout(view->window);
5122 s->selected_entry = NULL;
5124 waddstr(view->window, " ..\n"); /* parent directory */
5125 if (s->selected == 0 && view->focussed)
5126 wstandend(view->window);
5133 te = s->first_displayed_entry;
5136 nentries = got_object_tree_get_nentries(s->tree);
5137 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5138 char *line = NULL, *id_str = NULL, *link_target = NULL;
5139 const char *modestr = "";
5142 te = got_object_tree_get_entry(s->tree, i);
5143 mode = got_tree_entry_get_mode(te);
5146 err = got_object_id_str(&id_str,
5147 got_tree_entry_get_id(te));
5149 return got_error_from_errno(
5150 "got_object_id_str");
5152 if (got_object_tree_entry_is_submodule(te))
5154 else if (S_ISLNK(mode)) {
5157 err = got_tree_entry_get_symlink_target(&link_target,
5163 for (i = 0; i < strlen(link_target); i++) {
5164 if (!isprint((unsigned char)link_target[i]))
5165 link_target[i] = '?';
5169 else if (S_ISDIR(mode))
5171 else if (mode & S_IXUSR)
5173 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5174 got_tree_entry_get_name(te), modestr,
5175 link_target ? " -> ": "",
5176 link_target ? link_target : "") == -1) {
5179 return got_error_from_errno("asprintf");
5183 err = format_line(&wline, &width, line, view->ncols, 0);
5188 if (n == s->selected) {
5190 wstandout(view->window);
5191 s->selected_entry = te;
5193 tc = match_color(&s->colors, line);
5195 wattr_on(view->window,
5196 COLOR_PAIR(tc->colorpair), NULL);
5197 waddwstr(view->window, wline);
5199 wattr_off(view->window,
5200 COLOR_PAIR(tc->colorpair), NULL);
5201 if (width < view->ncols - 1)
5202 waddch(view->window, '\n');
5203 if (n == s->selected && view->focussed)
5204 wstandend(view->window);
5210 s->last_displayed_entry = te;
5219 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5221 struct got_tree_entry *te;
5222 int isroot = s->tree == s->root;
5225 if (s->first_displayed_entry == NULL)
5228 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5229 while (i++ < maxscroll) {
5232 s->first_displayed_entry = NULL;
5235 s->first_displayed_entry = te;
5236 te = got_tree_entry_get_prev(s->tree, te);
5241 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5243 struct got_tree_entry *next, *last;
5246 if (s->first_displayed_entry)
5247 next = got_tree_entry_get_next(s->tree,
5248 s->first_displayed_entry);
5250 next = got_object_tree_get_first_entry(s->tree);
5252 last = s->last_displayed_entry;
5253 while (next && last && n++ < maxscroll) {
5254 last = got_tree_entry_get_next(s->tree, last);
5256 s->first_displayed_entry = next;
5257 next = got_tree_entry_get_next(s->tree, next);
5262 static const struct got_error *
5263 tree_entry_path(char **path, struct tog_parent_trees *parents,
5264 struct got_tree_entry *te)
5266 const struct got_error *err = NULL;
5267 struct tog_parent_tree *pt;
5268 size_t len = 2; /* for leading slash and NUL */
5270 TAILQ_FOREACH(pt, parents, entry)
5271 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5274 len += strlen(got_tree_entry_get_name(te));
5276 *path = calloc(1, len);
5278 return got_error_from_errno("calloc");
5281 pt = TAILQ_LAST(parents, tog_parent_trees);
5283 const char *name = got_tree_entry_get_name(pt->selected_entry);
5284 if (strlcat(*path, name, len) >= len) {
5285 err = got_error(GOT_ERR_NO_SPACE);
5288 if (strlcat(*path, "/", len) >= len) {
5289 err = got_error(GOT_ERR_NO_SPACE);
5292 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5295 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5296 err = got_error(GOT_ERR_NO_SPACE);
5308 static const struct got_error *
5309 blame_tree_entry(struct tog_view **new_view, int begin_x,
5310 struct got_tree_entry *te, struct tog_parent_trees *parents,
5311 struct got_object_id *commit_id, struct got_repository *repo)
5313 const struct got_error *err = NULL;
5315 struct tog_view *blame_view;
5319 err = tree_entry_path(&path, parents, te);
5323 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5324 if (blame_view == NULL) {
5325 err = got_error_from_errno("view_open");
5329 err = open_blame_view(blame_view, path, commit_id, repo);
5331 if (err->code == GOT_ERR_CANCELLED)
5333 view_close(blame_view);
5335 *new_view = blame_view;
5341 static const struct got_error *
5342 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5343 struct tog_tree_view_state *s)
5345 struct tog_view *log_view;
5346 const struct got_error *err = NULL;
5351 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5352 if (log_view == NULL)
5353 return got_error_from_errno("view_open");
5355 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5359 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5362 view_close(log_view);
5364 *new_view = log_view;
5369 static const struct got_error *
5370 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5371 const char *head_ref_name, struct got_repository *repo)
5373 const struct got_error *err = NULL;
5374 char *commit_id_str = NULL;
5375 struct tog_tree_view_state *s = &view->state.tree;
5376 struct got_commit_object *commit = NULL;
5378 TAILQ_INIT(&s->parents);
5379 STAILQ_INIT(&s->colors);
5381 s->commit_id = got_object_id_dup(commit_id);
5382 if (s->commit_id == NULL)
5383 return got_error_from_errno("got_object_id_dup");
5385 err = got_object_open_as_commit(&commit, repo, commit_id);
5390 * The root is opened here and will be closed when the view is closed.
5391 * Any visited subtrees and their path-wise parents are opened and
5394 err = got_object_open_as_tree(&s->root, repo,
5395 got_object_commit_get_tree_id(commit));
5400 err = got_object_id_str(&commit_id_str, commit_id);
5404 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5405 err = got_error_from_errno("asprintf");
5409 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5410 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5411 if (head_ref_name) {
5412 s->head_ref_name = strdup(head_ref_name);
5413 if (s->head_ref_name == NULL) {
5414 err = got_error_from_errno("strdup");
5420 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5421 err = add_color(&s->colors, "\\$$",
5422 TOG_COLOR_TREE_SUBMODULE,
5423 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5426 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5427 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5430 err = add_color(&s->colors, "/$",
5431 TOG_COLOR_TREE_DIRECTORY,
5432 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5436 err = add_color(&s->colors, "\\*$",
5437 TOG_COLOR_TREE_EXECUTABLE,
5438 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5442 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5443 get_color_value("TOG_COLOR_COMMIT"));
5448 view->show = show_tree_view;
5449 view->input = input_tree_view;
5450 view->close = close_tree_view;
5451 view->search_start = search_start_tree_view;
5452 view->search_next = search_next_tree_view;
5454 free(commit_id_str);
5456 got_object_commit_close(commit);
5458 close_tree_view(view);
5462 static const struct got_error *
5463 close_tree_view(struct tog_view *view)
5465 struct tog_tree_view_state *s = &view->state.tree;
5467 free_colors(&s->colors);
5468 free(s->tree_label);
5469 s->tree_label = NULL;
5471 s->commit_id = NULL;
5472 free(s->head_ref_name);
5473 s->head_ref_name = NULL;
5474 while (!TAILQ_EMPTY(&s->parents)) {
5475 struct tog_parent_tree *parent;
5476 parent = TAILQ_FIRST(&s->parents);
5477 TAILQ_REMOVE(&s->parents, parent, entry);
5478 if (parent->tree != s->root)
5479 got_object_tree_close(parent->tree);
5483 if (s->tree != NULL && s->tree != s->root)
5484 got_object_tree_close(s->tree);
5486 got_object_tree_close(s->root);
5490 static const struct got_error *
5491 search_start_tree_view(struct tog_view *view)
5493 struct tog_tree_view_state *s = &view->state.tree;
5495 s->matched_entry = NULL;
5500 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5502 regmatch_t regmatch;
5504 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5508 static const struct got_error *
5509 search_next_tree_view(struct tog_view *view)
5511 struct tog_tree_view_state *s = &view->state.tree;
5512 struct got_tree_entry *te = NULL;
5514 if (!view->searching) {
5515 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5519 if (s->matched_entry) {
5520 if (view->searching == TOG_SEARCH_FORWARD) {
5521 if (s->selected_entry)
5522 te = got_tree_entry_get_next(s->tree,
5525 te = got_object_tree_get_first_entry(s->tree);
5527 if (s->selected_entry == NULL)
5528 te = got_object_tree_get_last_entry(s->tree);
5530 te = got_tree_entry_get_prev(s->tree,
5534 if (s->selected_entry)
5535 te = s->selected_entry;
5536 else if (view->searching == TOG_SEARCH_FORWARD)
5537 te = got_object_tree_get_first_entry(s->tree);
5539 te = got_object_tree_get_last_entry(s->tree);
5544 if (s->matched_entry == NULL) {
5545 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5548 if (view->searching == TOG_SEARCH_FORWARD)
5549 te = got_object_tree_get_first_entry(s->tree);
5551 te = got_object_tree_get_last_entry(s->tree);
5554 if (match_tree_entry(te, &view->regex)) {
5555 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5556 s->matched_entry = te;
5560 if (view->searching == TOG_SEARCH_FORWARD)
5561 te = got_tree_entry_get_next(s->tree, te);
5563 te = got_tree_entry_get_prev(s->tree, te);
5566 if (s->matched_entry) {
5567 s->first_displayed_entry = s->matched_entry;
5574 static const struct got_error *
5575 show_tree_view(struct tog_view *view)
5577 const struct got_error *err = NULL;
5578 struct tog_tree_view_state *s = &view->state.tree;
5581 err = tree_entry_path(&parent_path, &s->parents, NULL);
5585 err = draw_tree_entries(view, parent_path);
5592 static const struct got_error *
5593 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5595 const struct got_error *err = NULL;
5596 struct tog_tree_view_state *s = &view->state.tree;
5597 struct tog_view *log_view, *ref_view;
5598 struct got_tree_entry *te;
5599 int begin_x = 0, n, nscroll = view->nlines - 3;
5603 s->show_ids = !s->show_ids;
5606 if (!s->selected_entry)
5608 if (view_is_parent_view(view))
5609 begin_x = view_split_begin_x(view->begin_x);
5610 err = log_selected_tree_entry(&log_view, begin_x, s);
5612 log_view->focussed = 1;
5613 if (view_is_parent_view(view)) {
5614 err = view_close_child(view);
5617 view_set_child(view, log_view);
5618 view->focus_child = 1;
5620 *new_view = log_view;
5623 if (view_is_parent_view(view))
5624 begin_x = view_split_begin_x(view->begin_x);
5625 ref_view = view_open(view->nlines, view->ncols,
5626 view->begin_y, begin_x, TOG_VIEW_REF);
5627 if (ref_view == NULL)
5628 return got_error_from_errno("view_open");
5629 err = open_ref_view(ref_view, s->repo);
5631 view_close(ref_view);
5635 ref_view->focussed = 1;
5636 if (view_is_parent_view(view)) {
5637 err = view_close_child(view);
5640 view_set_child(view, ref_view);
5641 view->focus_child = 1;
5643 *new_view = ref_view;
5648 if (s->tree == s->root)
5649 s->first_displayed_entry =
5650 got_object_tree_get_first_entry(s->tree);
5652 s->first_displayed_entry = NULL;
5657 te = got_object_tree_get_last_entry(s->tree);
5658 for (n = 0; n < view->nlines - 3; n++) {
5660 if(s->tree != s->root) {
5661 s->first_displayed_entry = NULL;
5666 s->first_displayed_entry = te;
5667 te = got_tree_entry_get_prev(s->tree, te);
5670 s->selected = n - 1;
5675 if (s->selected > 0) {
5679 tree_scroll_up(s, 1);
5687 if (s->tree == s->root) {
5688 if (got_object_tree_get_first_entry(s->tree) ==
5689 s->first_displayed_entry)
5690 s->selected -= MIN(s->selected, nscroll);
5692 if (s->first_displayed_entry == NULL)
5693 s->selected -= MIN(s->selected, nscroll);
5695 tree_scroll_up(s, MAX(0, nscroll));
5700 if (s->selected < s->ndisplayed - 1) {
5704 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5706 /* can't scroll any further */
5708 tree_scroll_down(s, 1);
5716 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5718 /* can't scroll any further; move cursor down */
5719 if (s->selected < s->ndisplayed - 1)
5720 s->selected += MIN(nscroll,
5721 s->ndisplayed - s->selected - 1);
5724 tree_scroll_down(s, nscroll);
5729 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5730 struct tog_parent_tree *parent;
5731 /* user selected '..' */
5732 if (s->tree == s->root)
5734 parent = TAILQ_FIRST(&s->parents);
5735 TAILQ_REMOVE(&s->parents, parent,
5737 got_object_tree_close(s->tree);
5738 s->tree = parent->tree;
5739 s->first_displayed_entry =
5740 parent->first_displayed_entry;
5742 parent->selected_entry;
5743 s->selected = parent->selected;
5745 } else if (S_ISDIR(got_tree_entry_get_mode(
5746 s->selected_entry))) {
5747 struct got_tree_object *subtree;
5748 err = got_object_open_as_tree(&subtree, s->repo,
5749 got_tree_entry_get_id(s->selected_entry));
5752 err = tree_view_visit_subtree(s, subtree);
5754 got_object_tree_close(subtree);
5757 } else if (S_ISREG(got_tree_entry_get_mode(
5758 s->selected_entry))) {
5759 struct tog_view *blame_view;
5760 int begin_x = view_is_parent_view(view) ?
5761 view_split_begin_x(view->begin_x) : 0;
5763 err = blame_tree_entry(&blame_view, begin_x,
5764 s->selected_entry, &s->parents,
5765 s->commit_id, s->repo);
5769 blame_view->focussed = 1;
5770 if (view_is_parent_view(view)) {
5771 err = view_close_child(view);
5774 view_set_child(view, blame_view);
5775 view->focus_child = 1;
5777 *new_view = blame_view;
5781 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5782 s->selected = view->nlines - 4;
5795 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5800 static const struct got_error *
5801 cmd_tree(int argc, char *argv[])
5803 const struct got_error *error;
5804 struct got_repository *repo = NULL;
5805 struct got_worktree *worktree = NULL;
5806 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5807 struct got_object_id *commit_id = NULL;
5808 struct got_commit_object *commit = NULL;
5809 const char *commit_id_arg = NULL;
5811 struct got_reference *ref = NULL;
5812 const char *head_ref_name = NULL;
5814 struct tog_view *view;
5815 int *pack_fds = NULL;
5817 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5820 commit_id_arg = optarg;
5823 repo_path = realpath(optarg, NULL);
5824 if (repo_path == NULL)
5825 return got_error_from_errno2("realpath",
5840 error = got_repo_pack_fds_open(&pack_fds);
5844 if (repo_path == NULL) {
5845 cwd = getcwd(NULL, 0);
5847 return got_error_from_errno("getcwd");
5848 error = got_worktree_open(&worktree, cwd);
5849 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5853 strdup(got_worktree_get_repo_path(worktree));
5855 repo_path = strdup(cwd);
5856 if (repo_path == NULL) {
5857 error = got_error_from_errno("strdup");
5862 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5866 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5873 error = apply_unveil(got_repo_get_path(repo), NULL);
5877 error = tog_load_refs(repo, 0);
5881 if (commit_id_arg == NULL) {
5882 error = got_repo_match_object_id(&commit_id, &label,
5883 worktree ? got_worktree_get_head_ref_name(worktree) :
5884 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5887 head_ref_name = label;
5889 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5891 head_ref_name = got_ref_get_name(ref);
5892 else if (error->code != GOT_ERR_NOT_REF)
5894 error = got_repo_match_object_id(&commit_id, NULL,
5895 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5900 error = got_object_open_as_commit(&commit, repo, commit_id);
5904 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5906 error = got_error_from_errno("view_open");
5909 error = open_tree_view(view, commit_id, head_ref_name, repo);
5912 if (!got_path_is_root_dir(in_repo_path)) {
5913 error = tree_view_walk_path(&view->state.tree, commit,
5920 /* Release work tree lock. */
5921 got_worktree_close(worktree);
5924 error = view_loop(view);
5933 const struct got_error *close_err = got_repo_close(repo);
5938 const struct got_error *pack_err =
5939 got_repo_pack_fds_close(pack_fds);
5947 static const struct got_error *
5948 ref_view_load_refs(struct tog_ref_view_state *s)
5950 struct got_reflist_entry *sre;
5951 struct tog_reflist_entry *re;
5954 TAILQ_FOREACH(sre, &tog_refs, entry) {
5955 if (strncmp(got_ref_get_name(sre->ref),
5956 "refs/got/", 9) == 0 &&
5957 strncmp(got_ref_get_name(sre->ref),
5958 "refs/got/backup/", 16) != 0)
5961 re = malloc(sizeof(*re));
5963 return got_error_from_errno("malloc");
5965 re->ref = got_ref_dup(sre->ref);
5966 if (re->ref == NULL)
5967 return got_error_from_errno("got_ref_dup");
5968 re->idx = s->nrefs++;
5969 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5972 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5977 ref_view_free_refs(struct tog_ref_view_state *s)
5979 struct tog_reflist_entry *re;
5981 while (!TAILQ_EMPTY(&s->refs)) {
5982 re = TAILQ_FIRST(&s->refs);
5983 TAILQ_REMOVE(&s->refs, re, entry);
5984 got_ref_close(re->ref);
5989 static const struct got_error *
5990 open_ref_view(struct tog_view *view, struct got_repository *repo)
5992 const struct got_error *err = NULL;
5993 struct tog_ref_view_state *s = &view->state.ref;
5995 s->selected_entry = 0;
5998 TAILQ_INIT(&s->refs);
5999 STAILQ_INIT(&s->colors);
6001 err = ref_view_load_refs(s);
6005 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6006 err = add_color(&s->colors, "^refs/heads/",
6007 TOG_COLOR_REFS_HEADS,
6008 get_color_value("TOG_COLOR_REFS_HEADS"));
6012 err = add_color(&s->colors, "^refs/tags/",
6013 TOG_COLOR_REFS_TAGS,
6014 get_color_value("TOG_COLOR_REFS_TAGS"));
6018 err = add_color(&s->colors, "^refs/remotes/",
6019 TOG_COLOR_REFS_REMOTES,
6020 get_color_value("TOG_COLOR_REFS_REMOTES"));
6024 err = add_color(&s->colors, "^refs/got/backup/",
6025 TOG_COLOR_REFS_BACKUP,
6026 get_color_value("TOG_COLOR_REFS_BACKUP"));
6031 view->show = show_ref_view;
6032 view->input = input_ref_view;
6033 view->close = close_ref_view;
6034 view->search_start = search_start_ref_view;
6035 view->search_next = search_next_ref_view;
6038 free_colors(&s->colors);
6042 static const struct got_error *
6043 close_ref_view(struct tog_view *view)
6045 struct tog_ref_view_state *s = &view->state.ref;
6047 ref_view_free_refs(s);
6048 free_colors(&s->colors);
6053 static const struct got_error *
6054 resolve_reflist_entry(struct got_object_id **commit_id,
6055 struct tog_reflist_entry *re, struct got_repository *repo)
6057 const struct got_error *err = NULL;
6058 struct got_object_id *obj_id;
6059 struct got_tag_object *tag = NULL;
6064 err = got_ref_resolve(&obj_id, repo, re->ref);
6068 err = got_object_get_type(&obj_type, repo, obj_id);
6073 case GOT_OBJ_TYPE_COMMIT:
6074 *commit_id = obj_id;
6076 case GOT_OBJ_TYPE_TAG:
6077 err = got_object_open_as_tag(&tag, repo, obj_id);
6081 err = got_object_get_type(&obj_type, repo,
6082 got_object_tag_get_object_id(tag));
6085 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6086 err = got_error(GOT_ERR_OBJ_TYPE);
6089 *commit_id = got_object_id_dup(
6090 got_object_tag_get_object_id(tag));
6091 if (*commit_id == NULL) {
6092 err = got_error_from_errno("got_object_id_dup");
6097 err = got_error(GOT_ERR_OBJ_TYPE);
6103 got_object_tag_close(tag);
6111 static const struct got_error *
6112 log_ref_entry(struct tog_view **new_view, int begin_x,
6113 struct tog_reflist_entry *re, struct got_repository *repo)
6115 struct tog_view *log_view;
6116 const struct got_error *err = NULL;
6117 struct got_object_id *commit_id = NULL;
6121 err = resolve_reflist_entry(&commit_id, re, repo);
6123 if (err->code != GOT_ERR_OBJ_TYPE)
6129 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6130 if (log_view == NULL) {
6131 err = got_error_from_errno("view_open");
6135 err = open_log_view(log_view, commit_id, repo,
6136 got_ref_get_name(re->ref), "", 0);
6139 view_close(log_view);
6141 *new_view = log_view;
6147 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6149 struct tog_reflist_entry *re;
6152 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6155 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6156 while (i++ < maxscroll) {
6159 s->first_displayed_entry = re;
6160 re = TAILQ_PREV(re, tog_reflist_head, entry);
6165 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6167 struct tog_reflist_entry *next, *last;
6170 if (s->first_displayed_entry)
6171 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6173 next = TAILQ_FIRST(&s->refs);
6175 last = s->last_displayed_entry;
6176 while (next && last && n++ < maxscroll) {
6177 last = TAILQ_NEXT(last, entry);
6179 s->first_displayed_entry = next;
6180 next = TAILQ_NEXT(next, entry);
6185 static const struct got_error *
6186 search_start_ref_view(struct tog_view *view)
6188 struct tog_ref_view_state *s = &view->state.ref;
6190 s->matched_entry = NULL;
6195 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6197 regmatch_t regmatch;
6199 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6203 static const struct got_error *
6204 search_next_ref_view(struct tog_view *view)
6206 struct tog_ref_view_state *s = &view->state.ref;
6207 struct tog_reflist_entry *re = NULL;
6209 if (!view->searching) {
6210 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6214 if (s->matched_entry) {
6215 if (view->searching == TOG_SEARCH_FORWARD) {
6216 if (s->selected_entry)
6217 re = TAILQ_NEXT(s->selected_entry, entry);
6219 re = TAILQ_PREV(s->selected_entry,
6220 tog_reflist_head, entry);
6222 if (s->selected_entry == NULL)
6223 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6225 re = TAILQ_PREV(s->selected_entry,
6226 tog_reflist_head, entry);
6229 if (s->selected_entry)
6230 re = s->selected_entry;
6231 else if (view->searching == TOG_SEARCH_FORWARD)
6232 re = TAILQ_FIRST(&s->refs);
6234 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6239 if (s->matched_entry == NULL) {
6240 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6243 if (view->searching == TOG_SEARCH_FORWARD)
6244 re = TAILQ_FIRST(&s->refs);
6246 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6249 if (match_reflist_entry(re, &view->regex)) {
6250 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6251 s->matched_entry = re;
6255 if (view->searching == TOG_SEARCH_FORWARD)
6256 re = TAILQ_NEXT(re, entry);
6258 re = TAILQ_PREV(re, tog_reflist_head, entry);
6261 if (s->matched_entry) {
6262 s->first_displayed_entry = s->matched_entry;
6269 static const struct got_error *
6270 show_ref_view(struct tog_view *view)
6272 const struct got_error *err = NULL;
6273 struct tog_ref_view_state *s = &view->state.ref;
6274 struct tog_reflist_entry *re;
6277 struct tog_color *tc;
6279 int limit = view->nlines;
6281 werase(view->window);
6288 re = s->first_displayed_entry;
6290 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6292 return got_error_from_errno("asprintf");
6294 err = format_line(&wline, &width, line, view->ncols, 0);
6299 if (view_needs_focus_indication(view))
6300 wstandout(view->window);
6301 waddwstr(view->window, wline);
6302 if (view_needs_focus_indication(view))
6303 wstandend(view->window);
6308 if (width < view->ncols - 1)
6309 waddch(view->window, '\n');
6314 while (re && limit > 0) {
6317 if (got_ref_is_symbolic(re->ref)) {
6318 if (asprintf(&line, "%s -> %s",
6319 got_ref_get_name(re->ref),
6320 got_ref_get_symref_target(re->ref)) == -1)
6321 return got_error_from_errno("asprintf");
6322 } else if (s->show_ids) {
6323 struct got_object_id *id;
6325 err = got_ref_resolve(&id, s->repo, re->ref);
6328 err = got_object_id_str(&id_str, id);
6333 if (asprintf(&line, "%s: %s",
6334 got_ref_get_name(re->ref), id_str) == -1) {
6335 err = got_error_from_errno("asprintf");
6343 line = strdup(got_ref_get_name(re->ref));
6345 return got_error_from_errno("strdup");
6348 err = format_line(&wline, &width, line, view->ncols, 0);
6353 if (n == s->selected) {
6355 wstandout(view->window);
6356 s->selected_entry = re;
6358 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6360 wattr_on(view->window,
6361 COLOR_PAIR(tc->colorpair), NULL);
6362 waddwstr(view->window, wline);
6364 wattr_off(view->window,
6365 COLOR_PAIR(tc->colorpair), NULL);
6366 if (width < view->ncols - 1)
6367 waddch(view->window, '\n');
6368 if (n == s->selected && view->focussed)
6369 wstandend(view->window);
6375 s->last_displayed_entry = re;
6378 re = TAILQ_NEXT(re, entry);
6385 static const struct got_error *
6386 browse_ref_tree(struct tog_view **new_view, int begin_x,
6387 struct tog_reflist_entry *re, struct got_repository *repo)
6389 const struct got_error *err = NULL;
6390 struct got_object_id *commit_id = NULL;
6391 struct tog_view *tree_view;
6395 err = resolve_reflist_entry(&commit_id, re, repo);
6397 if (err->code != GOT_ERR_OBJ_TYPE)
6404 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6405 if (tree_view == NULL) {
6406 err = got_error_from_errno("view_open");
6410 err = open_tree_view(tree_view, commit_id,
6411 got_ref_get_name(re->ref), repo);
6415 *new_view = tree_view;
6420 static const struct got_error *
6421 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6423 const struct got_error *err = NULL;
6424 struct tog_ref_view_state *s = &view->state.ref;
6425 struct tog_view *log_view, *tree_view;
6426 struct tog_reflist_entry *re;
6427 int begin_x = 0, n, nscroll = view->nlines - 1;
6431 s->show_ids = !s->show_ids;
6434 s->sort_by_date = !s->sort_by_date;
6435 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6436 got_ref_cmp_by_commit_timestamp_descending :
6437 tog_ref_cmp_by_name, s->repo);
6440 got_reflist_object_id_map_free(tog_refs_idmap);
6441 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6442 &tog_refs, s->repo);
6445 ref_view_free_refs(s);
6446 err = ref_view_load_refs(s);
6450 if (!s->selected_entry)
6452 if (view_is_parent_view(view))
6453 begin_x = view_split_begin_x(view->begin_x);
6454 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6457 log_view->focussed = 1;
6458 if (view_is_parent_view(view)) {
6459 err = view_close_child(view);
6462 view_set_child(view, log_view);
6463 view->focus_child = 1;
6465 *new_view = log_view;
6468 if (!s->selected_entry)
6470 if (view_is_parent_view(view))
6471 begin_x = view_split_begin_x(view->begin_x);
6472 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6474 if (err || tree_view == NULL)
6477 tree_view->focussed = 1;
6478 if (view_is_parent_view(view)) {
6479 err = view_close_child(view);
6482 view_set_child(view, tree_view);
6483 view->focus_child = 1;
6485 *new_view = tree_view;
6490 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6495 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6496 for (n = 0; n < view->nlines - 1; n++) {
6499 s->first_displayed_entry = re;
6500 re = TAILQ_PREV(re, tog_reflist_head, entry);
6503 s->selected = n - 1;
6508 if (s->selected > 0) {
6512 ref_scroll_up(s, 1);
6520 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6521 s->selected -= MIN(nscroll, s->selected);
6522 ref_scroll_up(s, MAX(0, nscroll));
6527 if (s->selected < s->ndisplayed - 1) {
6531 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6532 /* can't scroll any further */
6534 ref_scroll_down(s, 1);
6542 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6543 /* can't scroll any further; move cursor down */
6544 if (s->selected < s->ndisplayed - 1)
6545 s->selected += MIN(nscroll,
6546 s->ndisplayed - s->selected - 1);
6549 ref_scroll_down(s, nscroll);
6553 err = tog_load_refs(s->repo, s->sort_by_date);
6556 ref_view_free_refs(s);
6557 err = ref_view_load_refs(s);
6560 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6561 s->selected = view->nlines - 2;
6574 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6579 static const struct got_error *
6580 cmd_ref(int argc, char *argv[])
6582 const struct got_error *error;
6583 struct got_repository *repo = NULL;
6584 struct got_worktree *worktree = NULL;
6585 char *cwd = NULL, *repo_path = NULL;
6587 struct tog_view *view;
6588 int *pack_fds = NULL;
6590 while ((ch = getopt(argc, argv, "r:")) != -1) {
6593 repo_path = realpath(optarg, NULL);
6594 if (repo_path == NULL)
6595 return got_error_from_errno2("realpath",
6610 error = got_repo_pack_fds_open(&pack_fds);
6614 if (repo_path == NULL) {
6615 cwd = getcwd(NULL, 0);
6617 return got_error_from_errno("getcwd");
6618 error = got_worktree_open(&worktree, cwd);
6619 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6623 strdup(got_worktree_get_repo_path(worktree));
6625 repo_path = strdup(cwd);
6626 if (repo_path == NULL) {
6627 error = got_error_from_errno("strdup");
6632 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6638 error = apply_unveil(got_repo_get_path(repo), NULL);
6642 error = tog_load_refs(repo, 0);
6646 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6648 error = got_error_from_errno("view_open");
6652 error = open_ref_view(view, repo);
6657 /* Release work tree lock. */
6658 got_worktree_close(worktree);
6661 error = view_loop(view);
6666 const struct got_error *close_err = got_repo_close(repo);
6671 const struct got_error *pack_err =
6672 got_repo_pack_fds_close(pack_fds);
6681 list_commands(FILE *fp)
6685 fprintf(fp, "commands:");
6686 for (i = 0; i < nitems(tog_commands); i++) {
6687 const struct tog_cmd *cmd = &tog_commands[i];
6688 fprintf(fp, " %s", cmd->name);
6694 usage(int hflag, int status)
6696 FILE *fp = (status == 0) ? stdout : stderr;
6698 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6701 fprintf(fp, "lazy usage: %s path\n", getprogname());
6708 make_argv(int argc, ...)
6716 argv = calloc(argc, sizeof(char *));
6719 for (i = 0; i < argc; i++) {
6720 argv[i] = strdup(va_arg(ap, char *));
6721 if (argv[i] == NULL)
6730 * Try to convert 'tog path' into a 'tog log path' command.
6731 * The user could simply have mistyped the command rather than knowingly
6732 * provided a path. So check whether argv[0] can in fact be resolved
6733 * to a path in the HEAD commit and print a special error if not.
6734 * This hack is for mpi@ <3
6736 static const struct got_error *
6737 tog_log_with_path(int argc, char *argv[])
6739 const struct got_error *error = NULL, *close_err;
6740 const struct tog_cmd *cmd = NULL;
6741 struct got_repository *repo = NULL;
6742 struct got_worktree *worktree = NULL;
6743 struct got_object_id *commit_id = NULL, *id = NULL;
6744 struct got_commit_object *commit = NULL;
6745 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6746 char *commit_id_str = NULL, **cmd_argv = NULL;
6747 int *pack_fds = NULL;
6749 cwd = getcwd(NULL, 0);
6751 return got_error_from_errno("getcwd");
6753 error = got_repo_pack_fds_open(&pack_fds);
6757 error = got_worktree_open(&worktree, cwd);
6758 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6762 repo_path = strdup(got_worktree_get_repo_path(worktree));
6764 repo_path = strdup(cwd);
6765 if (repo_path == NULL) {
6766 error = got_error_from_errno("strdup");
6770 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6774 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6779 error = tog_load_refs(repo, 0);
6782 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6783 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6784 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6789 got_worktree_close(worktree);
6793 error = got_object_open_as_commit(&commit, repo, commit_id);
6797 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6799 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6801 fprintf(stderr, "%s: '%s' is no known command or path\n",
6802 getprogname(), argv[0]);
6807 close_err = got_repo_close(repo);
6812 error = got_object_id_str(&commit_id_str, commit_id);
6816 cmd = &tog_commands[0]; /* log */
6818 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6819 error = cmd->cmd_main(argc, cmd_argv);
6822 close_err = got_repo_close(repo);
6827 got_object_commit_close(commit);
6829 got_worktree_close(worktree);
6831 const struct got_error *pack_err =
6832 got_repo_pack_fds_close(pack_fds);
6837 free(commit_id_str);
6844 for (i = 0; i < argc; i++)
6853 main(int argc, char *argv[])
6855 const struct got_error *error = NULL;
6856 const struct tog_cmd *cmd = NULL;
6857 int ch, hflag = 0, Vflag = 0;
6858 char **cmd_argv = NULL;
6859 static const struct option longopts[] = {
6860 { "version", no_argument, NULL, 'V' },
6864 setlocale(LC_CTYPE, "");
6866 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6886 got_version_print_str();
6891 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6899 /* Build an argument vector which runs a default command. */
6900 cmd = &tog_commands[0];
6902 cmd_argv = make_argv(argc, cmd->name);
6906 /* Did the user specify a command? */
6907 for (i = 0; i < nitems(tog_commands); i++) {
6908 if (strncmp(tog_commands[i].name, argv[0],
6909 strlen(argv[0])) == 0) {
6910 cmd = &tog_commands[i];
6919 /* No command specified; try log with a path */
6920 error = tog_log_with_path(argc, argv);
6925 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6932 for (i = 0; i < argc; i++)
6937 if (error && error->code != GOT_ERR_CANCELLED)
6938 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);