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);
2956 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2957 "[-w] object1 object2\n", getprogname());
2962 match_line(const char *line, regex_t *regex, size_t nmatch,
2963 regmatch_t *regmatch)
2965 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2969 match_color(struct tog_colors *colors, const char *line)
2971 struct tog_color *tc = NULL;
2973 STAILQ_FOREACH(tc, colors, entry) {
2974 if (match_line(line, &tc->regex, 0, NULL))
2981 static const struct got_error *
2982 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2983 WINDOW *window, regmatch_t *regmatch)
2985 const struct got_error *err = NULL;
2992 s = strndup(line, regmatch->rm_so);
2994 return got_error_from_errno("strndup");
2996 err = format_line(&wline, &width, s, wlimit, col_tab_align);
3001 waddwstr(window, wline);
3008 s = strndup(line + regmatch->rm_so,
3009 regmatch->rm_eo - regmatch->rm_so);
3011 err = got_error_from_errno("strndup");
3015 err = format_line(&wline, &width, s, wlimit, col_tab_align);
3020 wattr_on(window, A_STANDOUT, NULL);
3021 waddwstr(window, wline);
3022 wattr_off(window, A_STANDOUT, NULL);
3029 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
3030 err = format_line(&wline, &width,
3031 line + regmatch->rm_eo, wlimit, col_tab_align);
3034 waddwstr(window, wline);
3042 static const struct got_error *
3043 draw_file(struct tog_view *view, const char *header)
3045 struct tog_diff_view_state *s = &view->state.diff;
3046 regmatch_t *regmatch = &view->regmatch;
3047 const struct got_error *err;
3050 size_t linesize = 0;
3052 struct tog_color *tc;
3055 int max_lines = view->nlines;
3056 int nlines = s->nlines;
3059 line_offset = s->line_offsets[s->first_displayed_line - 1];
3060 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3061 return got_error_from_errno("fseek");
3063 werase(view->window);
3066 if (asprintf(&line, "[%d/%d] %s",
3067 s->first_displayed_line - 1 + s->selected_line, nlines,
3069 return got_error_from_errno("asprintf");
3070 err = format_line(&wline, &width, line, view->ncols, 0);
3075 if (view_needs_focus_indication(view))
3076 wstandout(view->window);
3077 waddwstr(view->window, wline);
3080 if (view_needs_focus_indication(view))
3081 wstandend(view->window);
3082 if (width <= view->ncols - 1)
3083 waddch(view->window, '\n');
3092 while (max_lines > 0 && nprinted < max_lines) {
3093 linelen = getline(&line, &linesize, s->f);
3094 if (linelen == -1) {
3100 return got_ferror(s->f, GOT_ERR_IO);
3103 tc = match_color(&s->colors, line);
3105 wattr_on(view->window,
3106 COLOR_PAIR(tc->colorpair), NULL);
3107 if (s->first_displayed_line + nprinted == s->matched_line &&
3108 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3109 err = add_matched_line(&width, line, view->ncols, 0,
3110 view->window, regmatch);
3116 err = format_line(&wline, &width, line, view->ncols, 0);
3121 waddwstr(view->window, wline);
3126 wattr_off(view->window,
3127 COLOR_PAIR(tc->colorpair), NULL);
3128 if (width <= view->ncols - 1)
3129 waddch(view->window, '\n');
3134 s->last_displayed_line = s->first_displayed_line +
3137 s->last_displayed_line = s->first_displayed_line;
3142 while (nprinted < view->nlines) {
3143 waddch(view->window, '\n');
3147 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3152 wstandout(view->window);
3153 waddwstr(view->window, wline);
3156 wstandend(view->window);
3163 get_datestr(time_t *time, char *datebuf)
3165 struct tm mytm, *tm;
3168 tm = gmtime_r(time, &mytm);
3171 s = asctime_r(tm, datebuf);
3174 p = strchr(s, '\n');
3180 static const struct got_error *
3181 get_changed_paths(struct got_pathlist_head *paths,
3182 struct got_commit_object *commit, struct got_repository *repo)
3184 const struct got_error *err = NULL;
3185 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3186 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3187 struct got_object_qid *qid;
3189 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3191 struct got_commit_object *pcommit;
3192 err = got_object_open_as_commit(&pcommit, repo,
3197 tree_id1 = got_object_id_dup(
3198 got_object_commit_get_tree_id(pcommit));
3199 if (tree_id1 == NULL) {
3200 got_object_commit_close(pcommit);
3201 return got_error_from_errno("got_object_id_dup");
3203 got_object_commit_close(pcommit);
3208 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3213 tree_id2 = got_object_commit_get_tree_id(commit);
3214 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3218 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3219 got_diff_tree_collect_changed_paths, paths, 0);
3222 got_object_tree_close(tree1);
3224 got_object_tree_close(tree2);
3229 static const struct got_error *
3230 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3234 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3236 return got_error_from_errno("reallocarray");
3238 (*line_offsets)[*nlines] = off;
3243 static const struct got_error *
3244 write_commit_info(off_t **line_offsets, size_t *nlines,
3245 struct got_object_id *commit_id, struct got_reflist_head *refs,
3246 struct got_repository *repo, FILE *outfile)
3248 const struct got_error *err = NULL;
3249 char datebuf[26], *datestr;
3250 struct got_commit_object *commit;
3251 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3252 time_t committer_time;
3253 const char *author, *committer;
3254 char *refs_str = NULL;
3255 struct got_pathlist_head changed_paths;
3256 struct got_pathlist_entry *pe;
3260 TAILQ_INIT(&changed_paths);
3263 err = build_refs_str(&refs_str, refs, commit_id, repo);
3268 err = got_object_open_as_commit(&commit, repo, commit_id);
3272 err = got_object_id_str(&id_str, commit_id);
3274 err = got_error_from_errno("got_object_id_str");
3278 err = add_line_offset(line_offsets, nlines, 0);
3282 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3283 refs_str ? refs_str : "", refs_str ? ")" : "");
3285 err = got_error_from_errno("fprintf");
3289 err = add_line_offset(line_offsets, nlines, outoff);
3293 n = fprintf(outfile, "from: %s\n",
3294 got_object_commit_get_author(commit));
3296 err = got_error_from_errno("fprintf");
3300 err = add_line_offset(line_offsets, nlines, outoff);
3304 committer_time = got_object_commit_get_committer_time(commit);
3305 datestr = get_datestr(&committer_time, datebuf);
3307 n = fprintf(outfile, "date: %s UTC\n", datestr);
3309 err = got_error_from_errno("fprintf");
3313 err = add_line_offset(line_offsets, nlines, outoff);
3317 author = got_object_commit_get_author(commit);
3318 committer = got_object_commit_get_committer(commit);
3319 if (strcmp(author, committer) != 0) {
3320 n = fprintf(outfile, "via: %s\n", committer);
3322 err = got_error_from_errno("fprintf");
3326 err = add_line_offset(line_offsets, nlines, outoff);
3330 if (got_object_commit_get_nparents(commit) > 1) {
3331 const struct got_object_id_queue *parent_ids;
3332 struct got_object_qid *qid;
3334 parent_ids = got_object_commit_get_parent_ids(commit);
3335 STAILQ_FOREACH(qid, parent_ids, entry) {
3336 err = got_object_id_str(&id_str, &qid->id);
3339 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3341 err = got_error_from_errno("fprintf");
3345 err = add_line_offset(line_offsets, nlines, outoff);
3353 err = got_object_commit_get_logmsg(&logmsg, commit);
3357 while ((line = strsep(&s, "\n")) != NULL) {
3358 n = fprintf(outfile, "%s\n", line);
3360 err = got_error_from_errno("fprintf");
3364 err = add_line_offset(line_offsets, nlines, outoff);
3369 err = get_changed_paths(&changed_paths, commit, repo);
3372 TAILQ_FOREACH(pe, &changed_paths, entry) {
3373 struct got_diff_changed_path *cp = pe->data;
3374 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3376 err = got_error_from_errno("fprintf");
3380 err = add_line_offset(line_offsets, nlines, outoff);
3383 free((char *)pe->path);
3387 fputc('\n', outfile);
3389 err = add_line_offset(line_offsets, nlines, outoff);
3391 got_pathlist_free(&changed_paths);
3395 got_object_commit_close(commit);
3397 free(*line_offsets);
3398 *line_offsets = NULL;
3404 static const struct got_error *
3405 create_diff(struct tog_diff_view_state *s)
3407 const struct got_error *err = NULL;
3411 free(s->line_offsets);
3412 s->line_offsets = malloc(sizeof(off_t));
3413 if (s->line_offsets == NULL)
3414 return got_error_from_errno("malloc");
3419 err = got_error_from_errno("got_opentemp");
3422 if (s->f && fclose(s->f) == EOF) {
3423 err = got_error_from_errno("fclose");
3429 err = got_object_get_type(&obj_type, s->repo, s->id1);
3431 err = got_object_get_type(&obj_type, s->repo, s->id2);
3436 case GOT_OBJ_TYPE_BLOB:
3437 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3438 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3439 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3442 case GOT_OBJ_TYPE_TREE:
3443 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3444 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3445 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3447 case GOT_OBJ_TYPE_COMMIT: {
3448 const struct got_object_id_queue *parent_ids;
3449 struct got_object_qid *pid;
3450 struct got_commit_object *commit2;
3451 struct got_reflist_head *refs;
3453 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3456 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3457 /* Show commit info if we're diffing to a parent/root commit. */
3458 if (s->id1 == NULL) {
3459 err = write_commit_info(&s->line_offsets, &s->nlines,
3460 s->id2, refs, s->repo, s->f);
3464 parent_ids = got_object_commit_get_parent_ids(commit2);
3465 STAILQ_FOREACH(pid, parent_ids, entry) {
3466 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3467 err = write_commit_info(
3468 &s->line_offsets, &s->nlines,
3469 s->id2, refs, s->repo, s->f);
3476 got_object_commit_close(commit2);
3478 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3479 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3480 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3484 err = got_error(GOT_ERR_OBJ_TYPE);
3490 if (s->f && fflush(s->f) != 0 && err == NULL)
3491 err = got_error_from_errno("fflush");
3496 diff_view_indicate_progress(struct tog_view *view)
3498 mvwaddstr(view->window, 0, 0, "diffing...");
3503 static const struct got_error *
3504 search_start_diff_view(struct tog_view *view)
3506 struct tog_diff_view_state *s = &view->state.diff;
3508 s->matched_line = 0;
3512 static const struct got_error *
3513 search_next_diff_view(struct tog_view *view)
3515 struct tog_diff_view_state *s = &view->state.diff;
3518 size_t linesize = 0;
3521 if (!view->searching) {
3522 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3526 if (s->matched_line) {
3527 if (view->searching == TOG_SEARCH_FORWARD)
3528 lineno = s->matched_line + 1;
3530 lineno = s->matched_line - 1;
3532 lineno = s->first_displayed_line;
3537 if (lineno <= 0 || lineno > s->nlines) {
3538 if (s->matched_line == 0) {
3539 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3543 if (view->searching == TOG_SEARCH_FORWARD)
3549 offset = s->line_offsets[lineno - 1];
3550 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3552 return got_error_from_errno("fseeko");
3554 linelen = getline(&line, &linesize, s->f);
3555 if (linelen != -1 &&
3556 match_line(line, &view->regex, 1, &view->regmatch)) {
3557 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3558 s->matched_line = lineno;
3561 if (view->searching == TOG_SEARCH_FORWARD)
3568 if (s->matched_line) {
3569 s->first_displayed_line = s->matched_line;
3570 s->selected_line = 1;
3576 static const struct got_error *
3577 close_diff_view(struct tog_view *view)
3579 const struct got_error *err = NULL;
3580 struct tog_diff_view_state *s = &view->state.diff;
3586 if (s->f && fclose(s->f) == EOF)
3587 err = got_error_from_errno("fclose");
3589 if (s->f1 && fclose(s->f1) == EOF)
3590 err = got_error_from_errno("fclose");
3592 if (s->f2 && fclose(s->f2) == EOF)
3593 err = got_error_from_errno("fclose");
3595 free_colors(&s->colors);
3596 free(s->line_offsets);
3597 s->line_offsets = NULL;
3602 static const struct got_error *
3603 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3604 struct got_object_id *id2, const char *label1, const char *label2,
3605 int diff_context, int ignore_whitespace, int force_text_diff,
3606 struct tog_view *log_view, struct got_repository *repo)
3608 const struct got_error *err;
3609 struct tog_diff_view_state *s = &view->state.diff;
3611 memset(s, 0, sizeof(*s));
3613 if (id1 != NULL && id2 != NULL) {
3615 err = got_object_get_type(&type1, repo, id1);
3618 err = got_object_get_type(&type2, repo, id2);
3623 return got_error(GOT_ERR_OBJ_TYPE);
3625 s->first_displayed_line = 1;
3626 s->last_displayed_line = view->nlines;
3627 s->selected_line = 1;
3635 s->id1 = got_object_id_dup(id1);
3637 return got_error_from_errno("got_object_id_dup");
3638 s->f1 = got_opentemp();
3639 if (s->f1 == NULL) {
3640 err = got_error_from_errno("got_opentemp");
3646 s->id2 = got_object_id_dup(id2);
3647 if (s->id2 == NULL) {
3648 err = got_error_from_errno("got_object_id_dup");
3652 s->f2 = got_opentemp();
3653 if (s->f2 == NULL) {
3654 err = got_error_from_errno("got_opentemp");
3658 s->first_displayed_line = 1;
3659 s->last_displayed_line = view->nlines;
3660 s->diff_context = diff_context;
3661 s->ignore_whitespace = ignore_whitespace;
3662 s->force_text_diff = force_text_diff;
3663 s->log_view = log_view;
3666 STAILQ_INIT(&s->colors);
3667 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3668 err = add_color(&s->colors,
3669 "^-", TOG_COLOR_DIFF_MINUS,
3670 get_color_value("TOG_COLOR_DIFF_MINUS"));
3673 err = add_color(&s->colors, "^\\+",
3674 TOG_COLOR_DIFF_PLUS,
3675 get_color_value("TOG_COLOR_DIFF_PLUS"));
3678 err = add_color(&s->colors,
3679 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3680 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3684 err = add_color(&s->colors,
3685 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3686 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3687 get_color_value("TOG_COLOR_DIFF_META"));
3691 err = add_color(&s->colors,
3692 "^(from|via): ", TOG_COLOR_AUTHOR,
3693 get_color_value("TOG_COLOR_AUTHOR"));
3697 err = add_color(&s->colors,
3698 "^date: ", TOG_COLOR_DATE,
3699 get_color_value("TOG_COLOR_DATE"));
3704 if (log_view && view_is_splitscreen(view))
3705 show_log_view(log_view); /* draw vborder */
3706 diff_view_indicate_progress(view);
3708 err = create_diff(s);
3710 view->show = show_diff_view;
3711 view->input = input_diff_view;
3712 view->close = close_diff_view;
3713 view->search_start = search_start_diff_view;
3714 view->search_next = search_next_diff_view;
3717 close_diff_view(view);
3721 static const struct got_error *
3722 show_diff_view(struct tog_view *view)
3724 const struct got_error *err;
3725 struct tog_diff_view_state *s = &view->state.diff;
3726 char *id_str1 = NULL, *id_str2, *header;
3727 const char *label1, *label2;
3730 err = got_object_id_str(&id_str1, s->id1);
3733 label1 = s->label1 ? : id_str1;
3735 label1 = "/dev/null";
3737 err = got_object_id_str(&id_str2, s->id2);
3740 label2 = s->label2 ? : id_str2;
3742 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3743 err = got_error_from_errno("asprintf");
3751 err = draw_file(view, header);
3756 static const struct got_error *
3757 set_selected_commit(struct tog_diff_view_state *s,
3758 struct commit_queue_entry *entry)
3760 const struct got_error *err;
3761 const struct got_object_id_queue *parent_ids;
3762 struct got_commit_object *selected_commit;
3763 struct got_object_qid *pid;
3766 s->id2 = got_object_id_dup(entry->id);
3768 return got_error_from_errno("got_object_id_dup");
3770 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3773 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3775 pid = STAILQ_FIRST(parent_ids);
3776 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3777 got_object_commit_close(selected_commit);
3781 static const struct got_error *
3782 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3784 const struct got_error *err = NULL;
3785 struct tog_diff_view_state *s = &view->state.diff;
3786 struct tog_log_view_state *ls;
3787 struct commit_queue_entry *old_selected_entry;
3789 size_t linesize = 0;
3791 int i, nscroll = view->nlines - 1;
3797 s->force_text_diff = !s->force_text_diff;
3799 s->ignore_whitespace = !s->ignore_whitespace;
3800 wclear(view->window);
3801 s->first_displayed_line = 1;
3802 s->last_displayed_line = view->nlines;
3803 s->matched_line = 0;
3804 diff_view_indicate_progress(view);
3805 err = create_diff(s);
3809 s->first_displayed_line = 1;
3816 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3822 if (s->first_displayed_line > 1)
3823 s->first_displayed_line--;
3831 if (s->first_displayed_line == 1)
3834 while (i++ < nscroll && s->first_displayed_line > 1)
3835 s->first_displayed_line--;
3841 s->first_displayed_line++;
3853 while (!s->eof && i++ < nscroll) {
3854 linelen = getline(&line, &linesize, s->f);
3855 s->first_displayed_line++;
3856 if (linelen == -1) {
3860 err = got_ferror(s->f, GOT_ERR_IO);
3867 if (s->diff_context > 0) {
3869 s->matched_line = 0;
3870 diff_view_indicate_progress(view);
3871 err = create_diff(s);
3872 if (s->first_displayed_line + view->nlines - 1 >
3874 s->first_displayed_line = 1;
3875 s->last_displayed_line = view->nlines;
3880 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3882 s->matched_line = 0;
3883 diff_view_indicate_progress(view);
3884 err = create_diff(s);
3889 if (s->log_view == NULL)
3891 ls = &s->log_view->state.log;
3892 old_selected_entry = ls->selected_entry;
3894 err = input_log_view(NULL, s->log_view, KEY_UP);
3898 if (old_selected_entry == ls->selected_entry)
3901 err = set_selected_commit(s, ls->selected_entry);
3905 s->first_displayed_line = 1;
3906 s->last_displayed_line = view->nlines;
3907 s->matched_line = 0;
3909 diff_view_indicate_progress(view);
3910 err = create_diff(s);
3914 if (s->log_view == NULL)
3916 ls = &s->log_view->state.log;
3917 old_selected_entry = ls->selected_entry;
3919 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3923 if (old_selected_entry == ls->selected_entry)
3926 err = set_selected_commit(s, ls->selected_entry);
3930 s->first_displayed_line = 1;
3931 s->last_displayed_line = view->nlines;
3932 s->matched_line = 0;
3934 diff_view_indicate_progress(view);
3935 err = create_diff(s);
3944 static const struct got_error *
3945 cmd_diff(int argc, char *argv[])
3947 const struct got_error *error = NULL;
3948 struct got_repository *repo = NULL;
3949 struct got_worktree *worktree = NULL;
3950 struct got_object_id *id1 = NULL, *id2 = NULL;
3951 char *repo_path = NULL, *cwd = NULL;
3952 char *id_str1 = NULL, *id_str2 = NULL;
3953 char *label1 = NULL, *label2 = NULL;
3954 int diff_context = 3, ignore_whitespace = 0;
3955 int ch, force_text_diff = 0;
3957 struct tog_view *view;
3958 int *pack_fds = NULL;
3960 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3963 force_text_diff = 1;
3966 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3969 errx(1, "number of context lines is %s: %s",
3973 repo_path = realpath(optarg, NULL);
3974 if (repo_path == NULL)
3975 return got_error_from_errno2("realpath",
3977 got_path_strip_trailing_slashes(repo_path);
3980 ignore_whitespace = 1;
3992 usage_diff(); /* TODO show local worktree changes */
3993 } else if (argc == 2) {
3999 error = got_repo_pack_fds_open(&pack_fds);
4003 if (repo_path == NULL) {
4004 cwd = getcwd(NULL, 0);
4006 return got_error_from_errno("getcwd");
4007 error = got_worktree_open(&worktree, cwd);
4008 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4012 strdup(got_worktree_get_repo_path(worktree));
4014 repo_path = strdup(cwd);
4015 if (repo_path == NULL) {
4016 error = got_error_from_errno("strdup");
4021 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4027 error = apply_unveil(got_repo_get_path(repo), NULL);
4031 error = tog_load_refs(repo, 0);
4035 error = got_repo_match_object_id(&id1, &label1, id_str1,
4036 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4040 error = got_repo_match_object_id(&id2, &label2, id_str2,
4041 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4045 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4047 error = got_error_from_errno("view_open");
4050 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4051 ignore_whitespace, force_text_diff, NULL, repo);
4054 error = view_loop(view);
4061 const struct got_error *close_err = got_repo_close(repo);
4066 got_worktree_close(worktree);
4068 const struct got_error *pack_err =
4069 got_repo_pack_fds_close(pack_fds);
4082 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4087 struct tog_blame_line {
4089 struct got_object_id *id;
4092 static const struct got_error *
4093 draw_blame(struct tog_view *view)
4095 struct tog_blame_view_state *s = &view->state.blame;
4096 struct tog_blame *blame = &s->blame;
4097 regmatch_t *regmatch = &view->regmatch;
4098 const struct got_error *err;
4099 int lineno = 0, nprinted = 0;
4101 size_t linesize = 0;
4105 struct tog_blame_line *blame_line;
4106 struct got_object_id *prev_id = NULL;
4108 struct tog_color *tc;
4110 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4115 werase(view->window);
4117 if (asprintf(&line, "commit %s", id_str) == -1) {
4118 err = got_error_from_errno("asprintf");
4123 err = format_line(&wline, &width, line, view->ncols, 0);
4128 if (view_needs_focus_indication(view))
4129 wstandout(view->window);
4130 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4132 wattr_on(view->window,
4133 COLOR_PAIR(tc->colorpair), NULL);
4134 waddwstr(view->window, wline);
4136 wattr_off(view->window,
4137 COLOR_PAIR(tc->colorpair), NULL);
4138 if (view_needs_focus_indication(view))
4139 wstandend(view->window);
4142 if (width < view->ncols - 1)
4143 waddch(view->window, '\n');
4145 if (asprintf(&line, "[%d/%d] %s%s",
4146 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4147 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4149 return got_error_from_errno("asprintf");
4152 err = format_line(&wline, &width, line, view->ncols, 0);
4157 waddwstr(view->window, wline);
4160 if (width < view->ncols - 1)
4161 waddch(view->window, '\n');
4164 while (nprinted < view->nlines - 2) {
4165 linelen = getline(&line, &linesize, blame->f);
4166 if (linelen == -1) {
4167 if (feof(blame->f)) {
4172 return got_ferror(blame->f, GOT_ERR_IO);
4174 if (++lineno < s->first_displayed_line)
4177 if (view->focussed && nprinted == s->selected_line - 1)
4178 wstandout(view->window);
4180 if (blame->nlines > 0) {
4181 blame_line = &blame->lines[lineno - 1];
4182 if (blame_line->annotated && prev_id &&
4183 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4185 nprinted == s->selected_line - 1)) {
4186 waddstr(view->window, " ");
4187 } else if (blame_line->annotated) {
4189 err = got_object_id_str(&id_str, blame_line->id);
4194 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4196 wattr_on(view->window,
4197 COLOR_PAIR(tc->colorpair), NULL);
4198 wprintw(view->window, "%.8s", id_str);
4200 wattr_off(view->window,
4201 COLOR_PAIR(tc->colorpair), NULL);
4203 prev_id = blame_line->id;
4205 waddstr(view->window, "........");
4209 waddstr(view->window, "........");
4213 if (view->focussed && nprinted == s->selected_line - 1)
4214 wstandend(view->window);
4215 waddstr(view->window, " ");
4217 if (view->ncols <= 9) {
4219 wline = wcsdup(L"");
4220 if (wline == NULL) {
4221 err = got_error_from_errno("wcsdup");
4225 } else if (s->first_displayed_line + nprinted ==
4227 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4228 err = add_matched_line(&width, line, view->ncols - 9, 9,
4229 view->window, regmatch);
4236 err = format_line(&wline, &width, line,
4237 view->ncols - 9, 9);
4238 waddwstr(view->window, wline);
4244 if (width <= view->ncols - 1)
4245 waddch(view->window, '\n');
4246 if (++nprinted == 1)
4247 s->first_displayed_line = lineno;
4250 s->last_displayed_line = lineno;
4257 static const struct got_error *
4258 blame_cb(void *arg, int nlines, int lineno,
4259 struct got_commit_object *commit, struct got_object_id *id)
4261 const struct got_error *err = NULL;
4262 struct tog_blame_cb_args *a = arg;
4263 struct tog_blame_line *line;
4266 if (nlines != a->nlines ||
4267 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4268 return got_error(GOT_ERR_RANGE);
4270 errcode = pthread_mutex_lock(&tog_mutex);
4272 return got_error_set_errno(errcode, "pthread_mutex_lock");
4274 if (*a->quit) { /* user has quit the blame view */
4275 err = got_error(GOT_ERR_ITER_COMPLETED);
4280 goto done; /* no change in this commit */
4282 line = &a->lines[lineno - 1];
4283 if (line->annotated)
4286 line->id = got_object_id_dup(id);
4287 if (line->id == NULL) {
4288 err = got_error_from_errno("got_object_id_dup");
4291 line->annotated = 1;
4293 errcode = pthread_mutex_unlock(&tog_mutex);
4295 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4300 blame_thread(void *arg)
4302 const struct got_error *err, *close_err;
4303 struct tog_blame_thread_args *ta = arg;
4304 struct tog_blame_cb_args *a = ta->cb_args;
4307 err = block_signals_used_by_main_thread();
4311 err = got_blame(ta->path, a->commit_id, ta->repo,
4312 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4313 if (err && err->code == GOT_ERR_CANCELLED)
4316 errcode = pthread_mutex_lock(&tog_mutex);
4318 return (void *)got_error_set_errno(errcode,
4319 "pthread_mutex_lock");
4321 close_err = got_repo_close(ta->repo);
4327 errcode = pthread_mutex_unlock(&tog_mutex);
4328 if (errcode && err == NULL)
4329 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4334 static struct got_object_id *
4335 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4336 int first_displayed_line, int selected_line)
4338 struct tog_blame_line *line;
4343 line = &lines[first_displayed_line - 1 + selected_line - 1];
4344 if (!line->annotated)
4350 static const struct got_error *
4351 stop_blame(struct tog_blame *blame)
4353 const struct got_error *err = NULL;
4356 if (blame->thread) {
4358 errcode = pthread_mutex_unlock(&tog_mutex);
4360 return got_error_set_errno(errcode,
4361 "pthread_mutex_unlock");
4362 errcode = pthread_join(blame->thread, (void **)&err);
4364 return got_error_set_errno(errcode, "pthread_join");
4365 errcode = pthread_mutex_lock(&tog_mutex);
4367 return got_error_set_errno(errcode,
4368 "pthread_mutex_lock");
4369 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4371 blame->thread = NULL;
4373 if (blame->thread_args.repo) {
4374 const struct got_error *close_err;
4375 close_err = got_repo_close(blame->thread_args.repo);
4378 blame->thread_args.repo = NULL;
4381 if (fclose(blame->f) == EOF && err == NULL)
4382 err = got_error_from_errno("fclose");
4386 for (i = 0; i < blame->nlines; i++)
4387 free(blame->lines[i].id);
4389 blame->lines = NULL;
4391 free(blame->cb_args.commit_id);
4392 blame->cb_args.commit_id = NULL;
4393 if (blame->pack_fds) {
4394 const struct got_error *pack_err =
4395 got_repo_pack_fds_close(blame->pack_fds);
4398 blame->pack_fds = NULL;
4403 static const struct got_error *
4404 cancel_blame_view(void *arg)
4406 const struct got_error *err = NULL;
4410 errcode = pthread_mutex_lock(&tog_mutex);
4412 return got_error_set_errno(errcode,
4413 "pthread_mutex_unlock");
4416 err = got_error(GOT_ERR_CANCELLED);
4418 errcode = pthread_mutex_unlock(&tog_mutex);
4420 return got_error_set_errno(errcode,
4421 "pthread_mutex_lock");
4426 static const struct got_error *
4427 run_blame(struct tog_view *view)
4429 struct tog_blame_view_state *s = &view->state.blame;
4430 struct tog_blame *blame = &s->blame;
4431 const struct got_error *err = NULL;
4432 struct got_commit_object *commit = NULL;
4433 struct got_blob_object *blob = NULL;
4434 struct got_repository *thread_repo = NULL;
4435 struct got_object_id *obj_id = NULL;
4437 int *pack_fds = NULL;
4439 err = got_object_open_as_commit(&commit, s->repo,
4440 &s->blamed_commit->id);
4444 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4448 err = got_object_get_type(&obj_type, s->repo, obj_id);
4452 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4453 err = got_error(GOT_ERR_OBJ_TYPE);
4457 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4460 blame->f = got_opentemp();
4461 if (blame->f == NULL) {
4462 err = got_error_from_errno("got_opentemp");
4465 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4466 &blame->line_offsets, blame->f, blob);
4469 if (blame->nlines == 0) {
4470 s->blame_complete = 1;
4474 /* Don't include \n at EOF in the blame line count. */
4475 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4478 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4479 if (blame->lines == NULL) {
4480 err = got_error_from_errno("calloc");
4484 err = got_repo_pack_fds_open(&pack_fds);
4487 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4492 blame->pack_fds = pack_fds;
4493 blame->cb_args.view = view;
4494 blame->cb_args.lines = blame->lines;
4495 blame->cb_args.nlines = blame->nlines;
4496 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4497 if (blame->cb_args.commit_id == NULL) {
4498 err = got_error_from_errno("got_object_id_dup");
4501 blame->cb_args.quit = &s->done;
4503 blame->thread_args.path = s->path;
4504 blame->thread_args.repo = thread_repo;
4505 blame->thread_args.cb_args = &blame->cb_args;
4506 blame->thread_args.complete = &s->blame_complete;
4507 blame->thread_args.cancel_cb = cancel_blame_view;
4508 blame->thread_args.cancel_arg = &s->done;
4509 s->blame_complete = 0;
4511 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4512 s->first_displayed_line = 1;
4513 s->last_displayed_line = view->nlines;
4514 s->selected_line = 1;
4516 s->matched_line = 0;
4520 got_object_commit_close(commit);
4522 got_object_blob_close(blob);
4529 static const struct got_error *
4530 open_blame_view(struct tog_view *view, char *path,
4531 struct got_object_id *commit_id, struct got_repository *repo)
4533 const struct got_error *err = NULL;
4534 struct tog_blame_view_state *s = &view->state.blame;
4536 STAILQ_INIT(&s->blamed_commits);
4538 s->path = strdup(path);
4539 if (s->path == NULL)
4540 return got_error_from_errno("strdup");
4542 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4548 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4549 s->first_displayed_line = 1;
4550 s->last_displayed_line = view->nlines;
4551 s->selected_line = 1;
4552 s->blame_complete = 0;
4554 s->commit_id = commit_id;
4555 memset(&s->blame, 0, sizeof(s->blame));
4557 STAILQ_INIT(&s->colors);
4558 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4559 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4560 get_color_value("TOG_COLOR_COMMIT"));
4565 view->show = show_blame_view;
4566 view->input = input_blame_view;
4567 view->close = close_blame_view;
4568 view->search_start = search_start_blame_view;
4569 view->search_next = search_next_blame_view;
4571 return run_blame(view);
4574 static const struct got_error *
4575 close_blame_view(struct tog_view *view)
4577 const struct got_error *err = NULL;
4578 struct tog_blame_view_state *s = &view->state.blame;
4580 if (s->blame.thread)
4581 err = stop_blame(&s->blame);
4583 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4584 struct got_object_qid *blamed_commit;
4585 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4586 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4587 got_object_qid_free(blamed_commit);
4591 free_colors(&s->colors);
4595 static const struct got_error *
4596 search_start_blame_view(struct tog_view *view)
4598 struct tog_blame_view_state *s = &view->state.blame;
4600 s->matched_line = 0;
4604 static const struct got_error *
4605 search_next_blame_view(struct tog_view *view)
4607 struct tog_blame_view_state *s = &view->state.blame;
4610 size_t linesize = 0;
4613 if (!view->searching) {
4614 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4618 if (s->matched_line) {
4619 if (view->searching == TOG_SEARCH_FORWARD)
4620 lineno = s->matched_line + 1;
4622 lineno = s->matched_line - 1;
4624 lineno = s->first_displayed_line - 1 + s->selected_line;
4629 if (lineno <= 0 || lineno > s->blame.nlines) {
4630 if (s->matched_line == 0) {
4631 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4635 if (view->searching == TOG_SEARCH_FORWARD)
4638 lineno = s->blame.nlines;
4641 offset = s->blame.line_offsets[lineno - 1];
4642 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4644 return got_error_from_errno("fseeko");
4646 linelen = getline(&line, &linesize, s->blame.f);
4647 if (linelen != -1 &&
4648 match_line(line, &view->regex, 1, &view->regmatch)) {
4649 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4650 s->matched_line = lineno;
4653 if (view->searching == TOG_SEARCH_FORWARD)
4660 if (s->matched_line) {
4661 s->first_displayed_line = s->matched_line;
4662 s->selected_line = 1;
4668 static const struct got_error *
4669 show_blame_view(struct tog_view *view)
4671 const struct got_error *err = NULL;
4672 struct tog_blame_view_state *s = &view->state.blame;
4675 if (s->blame.thread == NULL && !s->blame_complete) {
4676 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4677 &s->blame.thread_args);
4679 return got_error_set_errno(errcode, "pthread_create");
4681 halfdelay(1); /* fast refresh while annotating */
4684 if (s->blame_complete)
4685 halfdelay(10); /* disable fast refresh */
4687 err = draw_blame(view);
4693 static const struct got_error *
4694 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4696 const struct got_error *err = NULL, *thread_err = NULL;
4697 struct tog_view *diff_view;
4698 struct tog_blame_view_state *s = &view->state.blame;
4699 int begin_x = 0, nscroll = view->nlines - 2;
4707 s->selected_line = 1;
4708 s->first_displayed_line = 1;
4712 if (s->blame.nlines < view->nlines - 2) {
4713 s->selected_line = s->blame.nlines;
4714 s->first_displayed_line = 1;
4716 s->selected_line = view->nlines - 2;
4717 s->first_displayed_line = s->blame.nlines -
4724 if (s->selected_line > 1)
4726 else if (s->selected_line == 1 &&
4727 s->first_displayed_line > 1)
4728 s->first_displayed_line--;
4736 if (s->first_displayed_line == 1) {
4737 s->selected_line = MAX(1, s->selected_line - nscroll);
4740 if (s->first_displayed_line > nscroll)
4741 s->first_displayed_line -= nscroll;
4743 s->first_displayed_line = 1;
4748 if (s->selected_line < view->nlines - 2 &&
4749 s->first_displayed_line +
4750 s->selected_line <= s->blame.nlines)
4752 else if (s->last_displayed_line <
4754 s->first_displayed_line++;
4758 struct got_object_id *id = NULL;
4759 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4760 s->first_displayed_line, s->selected_line);
4764 struct got_commit_object *commit, *pcommit;
4765 struct got_object_qid *pid;
4766 struct got_object_id *blob_id = NULL;
4768 err = got_object_open_as_commit(&commit,
4773 got_object_commit_get_parent_ids(commit));
4775 got_object_commit_close(commit);
4778 /* Check if path history ends here. */
4779 err = got_object_open_as_commit(&pcommit,
4783 err = got_object_id_by_path(&blob_id, s->repo,
4785 got_object_commit_close(pcommit);
4787 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4789 got_object_commit_close(commit);
4792 err = got_object_get_type(&obj_type, s->repo,
4795 /* Can't blame non-blob type objects. */
4796 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4797 got_object_commit_close(commit);
4800 err = got_object_qid_alloc(&s->blamed_commit,
4802 got_object_commit_close(commit);
4804 if (got_object_id_cmp(id,
4805 &s->blamed_commit->id) == 0)
4807 err = got_object_qid_alloc(&s->blamed_commit,
4813 thread_err = stop_blame(&s->blame);
4817 STAILQ_INSERT_HEAD(&s->blamed_commits,
4818 s->blamed_commit, entry);
4819 err = run_blame(view);
4825 struct got_object_qid *first;
4826 first = STAILQ_FIRST(&s->blamed_commits);
4827 if (!got_object_id_cmp(&first->id, s->commit_id))
4830 thread_err = stop_blame(&s->blame);
4834 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4835 got_object_qid_free(s->blamed_commit);
4837 STAILQ_FIRST(&s->blamed_commits);
4838 err = run_blame(view);
4845 struct got_object_id *id = NULL;
4846 struct got_object_qid *pid;
4847 struct got_commit_object *commit = NULL;
4848 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4849 s->first_displayed_line, s->selected_line);
4852 err = got_object_open_as_commit(&commit, s->repo, id);
4856 got_object_commit_get_parent_ids(commit));
4857 if (view_is_parent_view(view))
4858 begin_x = view_split_begin_x(view->begin_x);
4859 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4860 if (diff_view == NULL) {
4861 got_object_commit_close(commit);
4862 err = got_error_from_errno("view_open");
4865 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4866 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4867 got_object_commit_close(commit);
4869 view_close(diff_view);
4873 diff_view->focussed = 1;
4874 if (view_is_parent_view(view)) {
4875 err = view_close_child(view);
4878 view_set_child(view, diff_view);
4879 view->focus_child = 1;
4881 *new_view = diff_view;
4893 if (s->last_displayed_line >= s->blame.nlines &&
4894 s->selected_line >= MIN(s->blame.nlines,
4895 view->nlines - 2)) {
4898 if (s->last_displayed_line >= s->blame.nlines &&
4899 s->selected_line < view->nlines - 2) {
4901 MIN(nscroll, s->last_displayed_line -
4902 s->first_displayed_line - s->selected_line + 1);
4904 if (s->last_displayed_line + nscroll <= s->blame.nlines)
4905 s->first_displayed_line += nscroll;
4907 s->first_displayed_line =
4908 s->blame.nlines - (view->nlines - 3);
4911 if (s->selected_line > view->nlines - 2) {
4912 s->selected_line = MIN(s->blame.nlines,
4919 return thread_err ? thread_err : err;
4922 static const struct got_error *
4923 cmd_blame(int argc, char *argv[])
4925 const struct got_error *error;
4926 struct got_repository *repo = NULL;
4927 struct got_worktree *worktree = NULL;
4928 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4929 char *link_target = NULL;
4930 struct got_object_id *commit_id = NULL;
4931 struct got_commit_object *commit = NULL;
4932 char *commit_id_str = NULL;
4934 struct tog_view *view;
4935 int *pack_fds = NULL;
4937 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4940 commit_id_str = optarg;
4943 repo_path = realpath(optarg, NULL);
4944 if (repo_path == NULL)
4945 return got_error_from_errno2("realpath",
4960 error = got_repo_pack_fds_open(&pack_fds);
4964 if (repo_path == NULL) {
4965 cwd = getcwd(NULL, 0);
4967 return got_error_from_errno("getcwd");
4968 error = got_worktree_open(&worktree, cwd);
4969 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4973 strdup(got_worktree_get_repo_path(worktree));
4975 repo_path = strdup(cwd);
4976 if (repo_path == NULL) {
4977 error = got_error_from_errno("strdup");
4982 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4986 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4993 error = apply_unveil(got_repo_get_path(repo), NULL);
4997 error = tog_load_refs(repo, 0);
5001 if (commit_id_str == NULL) {
5002 struct got_reference *head_ref;
5003 error = got_ref_open(&head_ref, repo, worktree ?
5004 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5007 error = got_ref_resolve(&commit_id, repo, head_ref);
5008 got_ref_close(head_ref);
5010 error = got_repo_match_object_id(&commit_id, NULL,
5011 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5016 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5018 error = got_error_from_errno("view_open");
5022 error = got_object_open_as_commit(&commit, repo, commit_id);
5026 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5031 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5036 /* Release work tree lock. */
5037 got_worktree_close(worktree);
5040 error = view_loop(view);
5048 got_object_commit_close(commit);
5050 got_worktree_close(worktree);
5052 const struct got_error *close_err = got_repo_close(repo);
5057 const struct got_error *pack_err =
5058 got_repo_pack_fds_close(pack_fds);
5067 static const struct got_error *
5068 draw_tree_entries(struct tog_view *view, const char *parent_path)
5070 struct tog_tree_view_state *s = &view->state.tree;
5071 const struct got_error *err = NULL;
5072 struct got_tree_entry *te;
5074 struct tog_color *tc;
5075 int width, n, i, nentries;
5076 int limit = view->nlines;
5080 werase(view->window);
5085 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
5088 if (view_needs_focus_indication(view))
5089 wstandout(view->window);
5090 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5092 wattr_on(view->window,
5093 COLOR_PAIR(tc->colorpair), NULL);
5094 waddwstr(view->window, wline);
5096 wattr_off(view->window,
5097 COLOR_PAIR(tc->colorpair), NULL);
5098 if (view_needs_focus_indication(view))
5099 wstandend(view->window);
5102 if (width < view->ncols - 1)
5103 waddch(view->window, '\n');
5106 err = format_line(&wline, &width, parent_path, view->ncols, 0);
5109 waddwstr(view->window, wline);
5112 if (width < view->ncols - 1)
5113 waddch(view->window, '\n');
5116 waddch(view->window, '\n');
5120 if (s->first_displayed_entry == NULL) {
5121 te = got_object_tree_get_first_entry(s->tree);
5122 if (s->selected == 0) {
5124 wstandout(view->window);
5125 s->selected_entry = NULL;
5127 waddstr(view->window, " ..\n"); /* parent directory */
5128 if (s->selected == 0 && view->focussed)
5129 wstandend(view->window);
5136 te = s->first_displayed_entry;
5139 nentries = got_object_tree_get_nentries(s->tree);
5140 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5141 char *line = NULL, *id_str = NULL, *link_target = NULL;
5142 const char *modestr = "";
5145 te = got_object_tree_get_entry(s->tree, i);
5146 mode = got_tree_entry_get_mode(te);
5149 err = got_object_id_str(&id_str,
5150 got_tree_entry_get_id(te));
5152 return got_error_from_errno(
5153 "got_object_id_str");
5155 if (got_object_tree_entry_is_submodule(te))
5157 else if (S_ISLNK(mode)) {
5160 err = got_tree_entry_get_symlink_target(&link_target,
5166 for (i = 0; i < strlen(link_target); i++) {
5167 if (!isprint((unsigned char)link_target[i]))
5168 link_target[i] = '?';
5172 else if (S_ISDIR(mode))
5174 else if (mode & S_IXUSR)
5176 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5177 got_tree_entry_get_name(te), modestr,
5178 link_target ? " -> ": "",
5179 link_target ? link_target : "") == -1) {
5182 return got_error_from_errno("asprintf");
5186 err = format_line(&wline, &width, line, view->ncols, 0);
5191 if (n == s->selected) {
5193 wstandout(view->window);
5194 s->selected_entry = te;
5196 tc = match_color(&s->colors, line);
5198 wattr_on(view->window,
5199 COLOR_PAIR(tc->colorpair), NULL);
5200 waddwstr(view->window, wline);
5202 wattr_off(view->window,
5203 COLOR_PAIR(tc->colorpair), NULL);
5204 if (width < view->ncols - 1)
5205 waddch(view->window, '\n');
5206 if (n == s->selected && view->focussed)
5207 wstandend(view->window);
5213 s->last_displayed_entry = te;
5222 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5224 struct got_tree_entry *te;
5225 int isroot = s->tree == s->root;
5228 if (s->first_displayed_entry == NULL)
5231 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5232 while (i++ < maxscroll) {
5235 s->first_displayed_entry = NULL;
5238 s->first_displayed_entry = te;
5239 te = got_tree_entry_get_prev(s->tree, te);
5244 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5246 struct got_tree_entry *next, *last;
5249 if (s->first_displayed_entry)
5250 next = got_tree_entry_get_next(s->tree,
5251 s->first_displayed_entry);
5253 next = got_object_tree_get_first_entry(s->tree);
5255 last = s->last_displayed_entry;
5256 while (next && last && n++ < maxscroll) {
5257 last = got_tree_entry_get_next(s->tree, last);
5259 s->first_displayed_entry = next;
5260 next = got_tree_entry_get_next(s->tree, next);
5265 static const struct got_error *
5266 tree_entry_path(char **path, struct tog_parent_trees *parents,
5267 struct got_tree_entry *te)
5269 const struct got_error *err = NULL;
5270 struct tog_parent_tree *pt;
5271 size_t len = 2; /* for leading slash and NUL */
5273 TAILQ_FOREACH(pt, parents, entry)
5274 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5277 len += strlen(got_tree_entry_get_name(te));
5279 *path = calloc(1, len);
5281 return got_error_from_errno("calloc");
5284 pt = TAILQ_LAST(parents, tog_parent_trees);
5286 const char *name = got_tree_entry_get_name(pt->selected_entry);
5287 if (strlcat(*path, name, len) >= len) {
5288 err = got_error(GOT_ERR_NO_SPACE);
5291 if (strlcat(*path, "/", len) >= len) {
5292 err = got_error(GOT_ERR_NO_SPACE);
5295 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5298 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5299 err = got_error(GOT_ERR_NO_SPACE);
5311 static const struct got_error *
5312 blame_tree_entry(struct tog_view **new_view, int begin_x,
5313 struct got_tree_entry *te, struct tog_parent_trees *parents,
5314 struct got_object_id *commit_id, struct got_repository *repo)
5316 const struct got_error *err = NULL;
5318 struct tog_view *blame_view;
5322 err = tree_entry_path(&path, parents, te);
5326 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5327 if (blame_view == NULL) {
5328 err = got_error_from_errno("view_open");
5332 err = open_blame_view(blame_view, path, commit_id, repo);
5334 if (err->code == GOT_ERR_CANCELLED)
5336 view_close(blame_view);
5338 *new_view = blame_view;
5344 static const struct got_error *
5345 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5346 struct tog_tree_view_state *s)
5348 struct tog_view *log_view;
5349 const struct got_error *err = NULL;
5354 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5355 if (log_view == NULL)
5356 return got_error_from_errno("view_open");
5358 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5362 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5365 view_close(log_view);
5367 *new_view = log_view;
5372 static const struct got_error *
5373 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5374 const char *head_ref_name, struct got_repository *repo)
5376 const struct got_error *err = NULL;
5377 char *commit_id_str = NULL;
5378 struct tog_tree_view_state *s = &view->state.tree;
5379 struct got_commit_object *commit = NULL;
5381 TAILQ_INIT(&s->parents);
5382 STAILQ_INIT(&s->colors);
5384 s->commit_id = got_object_id_dup(commit_id);
5385 if (s->commit_id == NULL)
5386 return got_error_from_errno("got_object_id_dup");
5388 err = got_object_open_as_commit(&commit, repo, commit_id);
5393 * The root is opened here and will be closed when the view is closed.
5394 * Any visited subtrees and their path-wise parents are opened and
5397 err = got_object_open_as_tree(&s->root, repo,
5398 got_object_commit_get_tree_id(commit));
5403 err = got_object_id_str(&commit_id_str, commit_id);
5407 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5408 err = got_error_from_errno("asprintf");
5412 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5413 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5414 if (head_ref_name) {
5415 s->head_ref_name = strdup(head_ref_name);
5416 if (s->head_ref_name == NULL) {
5417 err = got_error_from_errno("strdup");
5423 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5424 err = add_color(&s->colors, "\\$$",
5425 TOG_COLOR_TREE_SUBMODULE,
5426 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5429 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5430 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5433 err = add_color(&s->colors, "/$",
5434 TOG_COLOR_TREE_DIRECTORY,
5435 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5439 err = add_color(&s->colors, "\\*$",
5440 TOG_COLOR_TREE_EXECUTABLE,
5441 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5445 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5446 get_color_value("TOG_COLOR_COMMIT"));
5451 view->show = show_tree_view;
5452 view->input = input_tree_view;
5453 view->close = close_tree_view;
5454 view->search_start = search_start_tree_view;
5455 view->search_next = search_next_tree_view;
5457 free(commit_id_str);
5459 got_object_commit_close(commit);
5461 close_tree_view(view);
5465 static const struct got_error *
5466 close_tree_view(struct tog_view *view)
5468 struct tog_tree_view_state *s = &view->state.tree;
5470 free_colors(&s->colors);
5471 free(s->tree_label);
5472 s->tree_label = NULL;
5474 s->commit_id = NULL;
5475 free(s->head_ref_name);
5476 s->head_ref_name = NULL;
5477 while (!TAILQ_EMPTY(&s->parents)) {
5478 struct tog_parent_tree *parent;
5479 parent = TAILQ_FIRST(&s->parents);
5480 TAILQ_REMOVE(&s->parents, parent, entry);
5481 if (parent->tree != s->root)
5482 got_object_tree_close(parent->tree);
5486 if (s->tree != NULL && s->tree != s->root)
5487 got_object_tree_close(s->tree);
5489 got_object_tree_close(s->root);
5493 static const struct got_error *
5494 search_start_tree_view(struct tog_view *view)
5496 struct tog_tree_view_state *s = &view->state.tree;
5498 s->matched_entry = NULL;
5503 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5505 regmatch_t regmatch;
5507 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5511 static const struct got_error *
5512 search_next_tree_view(struct tog_view *view)
5514 struct tog_tree_view_state *s = &view->state.tree;
5515 struct got_tree_entry *te = NULL;
5517 if (!view->searching) {
5518 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5522 if (s->matched_entry) {
5523 if (view->searching == TOG_SEARCH_FORWARD) {
5524 if (s->selected_entry)
5525 te = got_tree_entry_get_next(s->tree,
5528 te = got_object_tree_get_first_entry(s->tree);
5530 if (s->selected_entry == NULL)
5531 te = got_object_tree_get_last_entry(s->tree);
5533 te = got_tree_entry_get_prev(s->tree,
5537 if (s->selected_entry)
5538 te = s->selected_entry;
5539 else if (view->searching == TOG_SEARCH_FORWARD)
5540 te = got_object_tree_get_first_entry(s->tree);
5542 te = got_object_tree_get_last_entry(s->tree);
5547 if (s->matched_entry == NULL) {
5548 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5551 if (view->searching == TOG_SEARCH_FORWARD)
5552 te = got_object_tree_get_first_entry(s->tree);
5554 te = got_object_tree_get_last_entry(s->tree);
5557 if (match_tree_entry(te, &view->regex)) {
5558 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5559 s->matched_entry = te;
5563 if (view->searching == TOG_SEARCH_FORWARD)
5564 te = got_tree_entry_get_next(s->tree, te);
5566 te = got_tree_entry_get_prev(s->tree, te);
5569 if (s->matched_entry) {
5570 s->first_displayed_entry = s->matched_entry;
5577 static const struct got_error *
5578 show_tree_view(struct tog_view *view)
5580 const struct got_error *err = NULL;
5581 struct tog_tree_view_state *s = &view->state.tree;
5584 err = tree_entry_path(&parent_path, &s->parents, NULL);
5588 err = draw_tree_entries(view, parent_path);
5595 static const struct got_error *
5596 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5598 const struct got_error *err = NULL;
5599 struct tog_tree_view_state *s = &view->state.tree;
5600 struct tog_view *log_view, *ref_view;
5601 struct got_tree_entry *te;
5602 int begin_x = 0, n, nscroll = view->nlines - 3;
5606 s->show_ids = !s->show_ids;
5609 if (!s->selected_entry)
5611 if (view_is_parent_view(view))
5612 begin_x = view_split_begin_x(view->begin_x);
5613 err = log_selected_tree_entry(&log_view, begin_x, s);
5615 log_view->focussed = 1;
5616 if (view_is_parent_view(view)) {
5617 err = view_close_child(view);
5620 view_set_child(view, log_view);
5621 view->focus_child = 1;
5623 *new_view = log_view;
5626 if (view_is_parent_view(view))
5627 begin_x = view_split_begin_x(view->begin_x);
5628 ref_view = view_open(view->nlines, view->ncols,
5629 view->begin_y, begin_x, TOG_VIEW_REF);
5630 if (ref_view == NULL)
5631 return got_error_from_errno("view_open");
5632 err = open_ref_view(ref_view, s->repo);
5634 view_close(ref_view);
5638 ref_view->focussed = 1;
5639 if (view_is_parent_view(view)) {
5640 err = view_close_child(view);
5643 view_set_child(view, ref_view);
5644 view->focus_child = 1;
5646 *new_view = ref_view;
5651 if (s->tree == s->root)
5652 s->first_displayed_entry =
5653 got_object_tree_get_first_entry(s->tree);
5655 s->first_displayed_entry = NULL;
5660 te = got_object_tree_get_last_entry(s->tree);
5661 for (n = 0; n < view->nlines - 3; n++) {
5663 if(s->tree != s->root) {
5664 s->first_displayed_entry = NULL;
5669 s->first_displayed_entry = te;
5670 te = got_tree_entry_get_prev(s->tree, te);
5673 s->selected = n - 1;
5678 if (s->selected > 0) {
5682 tree_scroll_up(s, 1);
5690 if (s->tree == s->root) {
5691 if (got_object_tree_get_first_entry(s->tree) ==
5692 s->first_displayed_entry)
5693 s->selected -= MIN(s->selected, nscroll);
5695 if (s->first_displayed_entry == NULL)
5696 s->selected -= MIN(s->selected, nscroll);
5698 tree_scroll_up(s, MAX(0, nscroll));
5703 if (s->selected < s->ndisplayed - 1) {
5707 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5709 /* can't scroll any further */
5711 tree_scroll_down(s, 1);
5719 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5721 /* can't scroll any further; move cursor down */
5722 if (s->selected < s->ndisplayed - 1)
5723 s->selected += MIN(nscroll,
5724 s->ndisplayed - s->selected - 1);
5727 tree_scroll_down(s, nscroll);
5732 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5733 struct tog_parent_tree *parent;
5734 /* user selected '..' */
5735 if (s->tree == s->root)
5737 parent = TAILQ_FIRST(&s->parents);
5738 TAILQ_REMOVE(&s->parents, parent,
5740 got_object_tree_close(s->tree);
5741 s->tree = parent->tree;
5742 s->first_displayed_entry =
5743 parent->first_displayed_entry;
5745 parent->selected_entry;
5746 s->selected = parent->selected;
5748 } else if (S_ISDIR(got_tree_entry_get_mode(
5749 s->selected_entry))) {
5750 struct got_tree_object *subtree;
5751 err = got_object_open_as_tree(&subtree, s->repo,
5752 got_tree_entry_get_id(s->selected_entry));
5755 err = tree_view_visit_subtree(s, subtree);
5757 got_object_tree_close(subtree);
5760 } else if (S_ISREG(got_tree_entry_get_mode(
5761 s->selected_entry))) {
5762 struct tog_view *blame_view;
5763 int begin_x = view_is_parent_view(view) ?
5764 view_split_begin_x(view->begin_x) : 0;
5766 err = blame_tree_entry(&blame_view, begin_x,
5767 s->selected_entry, &s->parents,
5768 s->commit_id, s->repo);
5772 blame_view->focussed = 1;
5773 if (view_is_parent_view(view)) {
5774 err = view_close_child(view);
5777 view_set_child(view, blame_view);
5778 view->focus_child = 1;
5780 *new_view = blame_view;
5784 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5785 s->selected = view->nlines - 4;
5798 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5803 static const struct got_error *
5804 cmd_tree(int argc, char *argv[])
5806 const struct got_error *error;
5807 struct got_repository *repo = NULL;
5808 struct got_worktree *worktree = NULL;
5809 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5810 struct got_object_id *commit_id = NULL;
5811 struct got_commit_object *commit = NULL;
5812 const char *commit_id_arg = NULL;
5814 struct got_reference *ref = NULL;
5815 const char *head_ref_name = NULL;
5817 struct tog_view *view;
5818 int *pack_fds = NULL;
5820 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5823 commit_id_arg = optarg;
5826 repo_path = realpath(optarg, NULL);
5827 if (repo_path == NULL)
5828 return got_error_from_errno2("realpath",
5843 error = got_repo_pack_fds_open(&pack_fds);
5847 if (repo_path == NULL) {
5848 cwd = getcwd(NULL, 0);
5850 return got_error_from_errno("getcwd");
5851 error = got_worktree_open(&worktree, cwd);
5852 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5856 strdup(got_worktree_get_repo_path(worktree));
5858 repo_path = strdup(cwd);
5859 if (repo_path == NULL) {
5860 error = got_error_from_errno("strdup");
5865 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5869 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5876 error = apply_unveil(got_repo_get_path(repo), NULL);
5880 error = tog_load_refs(repo, 0);
5884 if (commit_id_arg == NULL) {
5885 error = got_repo_match_object_id(&commit_id, &label,
5886 worktree ? got_worktree_get_head_ref_name(worktree) :
5887 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5890 head_ref_name = label;
5892 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5894 head_ref_name = got_ref_get_name(ref);
5895 else if (error->code != GOT_ERR_NOT_REF)
5897 error = got_repo_match_object_id(&commit_id, NULL,
5898 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5903 error = got_object_open_as_commit(&commit, repo, commit_id);
5907 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5909 error = got_error_from_errno("view_open");
5912 error = open_tree_view(view, commit_id, head_ref_name, repo);
5915 if (!got_path_is_root_dir(in_repo_path)) {
5916 error = tree_view_walk_path(&view->state.tree, commit,
5923 /* Release work tree lock. */
5924 got_worktree_close(worktree);
5927 error = view_loop(view);
5936 const struct got_error *close_err = got_repo_close(repo);
5941 const struct got_error *pack_err =
5942 got_repo_pack_fds_close(pack_fds);
5951 static const struct got_error *
5952 ref_view_load_refs(struct tog_ref_view_state *s)
5954 struct got_reflist_entry *sre;
5955 struct tog_reflist_entry *re;
5958 TAILQ_FOREACH(sre, &tog_refs, entry) {
5959 if (strncmp(got_ref_get_name(sre->ref),
5960 "refs/got/", 9) == 0 &&
5961 strncmp(got_ref_get_name(sre->ref),
5962 "refs/got/backup/", 16) != 0)
5965 re = malloc(sizeof(*re));
5967 return got_error_from_errno("malloc");
5969 re->ref = got_ref_dup(sre->ref);
5970 if (re->ref == NULL)
5971 return got_error_from_errno("got_ref_dup");
5972 re->idx = s->nrefs++;
5973 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5976 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5981 ref_view_free_refs(struct tog_ref_view_state *s)
5983 struct tog_reflist_entry *re;
5985 while (!TAILQ_EMPTY(&s->refs)) {
5986 re = TAILQ_FIRST(&s->refs);
5987 TAILQ_REMOVE(&s->refs, re, entry);
5988 got_ref_close(re->ref);
5993 static const struct got_error *
5994 open_ref_view(struct tog_view *view, struct got_repository *repo)
5996 const struct got_error *err = NULL;
5997 struct tog_ref_view_state *s = &view->state.ref;
5999 s->selected_entry = 0;
6002 TAILQ_INIT(&s->refs);
6003 STAILQ_INIT(&s->colors);
6005 err = ref_view_load_refs(s);
6009 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6010 err = add_color(&s->colors, "^refs/heads/",
6011 TOG_COLOR_REFS_HEADS,
6012 get_color_value("TOG_COLOR_REFS_HEADS"));
6016 err = add_color(&s->colors, "^refs/tags/",
6017 TOG_COLOR_REFS_TAGS,
6018 get_color_value("TOG_COLOR_REFS_TAGS"));
6022 err = add_color(&s->colors, "^refs/remotes/",
6023 TOG_COLOR_REFS_REMOTES,
6024 get_color_value("TOG_COLOR_REFS_REMOTES"));
6028 err = add_color(&s->colors, "^refs/got/backup/",
6029 TOG_COLOR_REFS_BACKUP,
6030 get_color_value("TOG_COLOR_REFS_BACKUP"));
6035 view->show = show_ref_view;
6036 view->input = input_ref_view;
6037 view->close = close_ref_view;
6038 view->search_start = search_start_ref_view;
6039 view->search_next = search_next_ref_view;
6042 free_colors(&s->colors);
6046 static const struct got_error *
6047 close_ref_view(struct tog_view *view)
6049 struct tog_ref_view_state *s = &view->state.ref;
6051 ref_view_free_refs(s);
6052 free_colors(&s->colors);
6057 static const struct got_error *
6058 resolve_reflist_entry(struct got_object_id **commit_id,
6059 struct tog_reflist_entry *re, struct got_repository *repo)
6061 const struct got_error *err = NULL;
6062 struct got_object_id *obj_id;
6063 struct got_tag_object *tag = NULL;
6068 err = got_ref_resolve(&obj_id, repo, re->ref);
6072 err = got_object_get_type(&obj_type, repo, obj_id);
6077 case GOT_OBJ_TYPE_COMMIT:
6078 *commit_id = obj_id;
6080 case GOT_OBJ_TYPE_TAG:
6081 err = got_object_open_as_tag(&tag, repo, obj_id);
6085 err = got_object_get_type(&obj_type, repo,
6086 got_object_tag_get_object_id(tag));
6089 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6090 err = got_error(GOT_ERR_OBJ_TYPE);
6093 *commit_id = got_object_id_dup(
6094 got_object_tag_get_object_id(tag));
6095 if (*commit_id == NULL) {
6096 err = got_error_from_errno("got_object_id_dup");
6101 err = got_error(GOT_ERR_OBJ_TYPE);
6107 got_object_tag_close(tag);
6115 static const struct got_error *
6116 log_ref_entry(struct tog_view **new_view, int begin_x,
6117 struct tog_reflist_entry *re, struct got_repository *repo)
6119 struct tog_view *log_view;
6120 const struct got_error *err = NULL;
6121 struct got_object_id *commit_id = NULL;
6125 err = resolve_reflist_entry(&commit_id, re, repo);
6127 if (err->code != GOT_ERR_OBJ_TYPE)
6133 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6134 if (log_view == NULL) {
6135 err = got_error_from_errno("view_open");
6139 err = open_log_view(log_view, commit_id, repo,
6140 got_ref_get_name(re->ref), "", 0);
6143 view_close(log_view);
6145 *new_view = log_view;
6151 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6153 struct tog_reflist_entry *re;
6156 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6159 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6160 while (i++ < maxscroll) {
6163 s->first_displayed_entry = re;
6164 re = TAILQ_PREV(re, tog_reflist_head, entry);
6169 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6171 struct tog_reflist_entry *next, *last;
6174 if (s->first_displayed_entry)
6175 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6177 next = TAILQ_FIRST(&s->refs);
6179 last = s->last_displayed_entry;
6180 while (next && last && n++ < maxscroll) {
6181 last = TAILQ_NEXT(last, entry);
6183 s->first_displayed_entry = next;
6184 next = TAILQ_NEXT(next, entry);
6189 static const struct got_error *
6190 search_start_ref_view(struct tog_view *view)
6192 struct tog_ref_view_state *s = &view->state.ref;
6194 s->matched_entry = NULL;
6199 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6201 regmatch_t regmatch;
6203 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6207 static const struct got_error *
6208 search_next_ref_view(struct tog_view *view)
6210 struct tog_ref_view_state *s = &view->state.ref;
6211 struct tog_reflist_entry *re = NULL;
6213 if (!view->searching) {
6214 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6218 if (s->matched_entry) {
6219 if (view->searching == TOG_SEARCH_FORWARD) {
6220 if (s->selected_entry)
6221 re = TAILQ_NEXT(s->selected_entry, entry);
6223 re = TAILQ_PREV(s->selected_entry,
6224 tog_reflist_head, entry);
6226 if (s->selected_entry == NULL)
6227 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6229 re = TAILQ_PREV(s->selected_entry,
6230 tog_reflist_head, entry);
6233 if (s->selected_entry)
6234 re = s->selected_entry;
6235 else if (view->searching == TOG_SEARCH_FORWARD)
6236 re = TAILQ_FIRST(&s->refs);
6238 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6243 if (s->matched_entry == NULL) {
6244 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6247 if (view->searching == TOG_SEARCH_FORWARD)
6248 re = TAILQ_FIRST(&s->refs);
6250 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6253 if (match_reflist_entry(re, &view->regex)) {
6254 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6255 s->matched_entry = re;
6259 if (view->searching == TOG_SEARCH_FORWARD)
6260 re = TAILQ_NEXT(re, entry);
6262 re = TAILQ_PREV(re, tog_reflist_head, entry);
6265 if (s->matched_entry) {
6266 s->first_displayed_entry = s->matched_entry;
6273 static const struct got_error *
6274 show_ref_view(struct tog_view *view)
6276 const struct got_error *err = NULL;
6277 struct tog_ref_view_state *s = &view->state.ref;
6278 struct tog_reflist_entry *re;
6281 struct tog_color *tc;
6283 int limit = view->nlines;
6285 werase(view->window);
6292 re = s->first_displayed_entry;
6294 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6296 return got_error_from_errno("asprintf");
6298 err = format_line(&wline, &width, line, view->ncols, 0);
6303 if (view_needs_focus_indication(view))
6304 wstandout(view->window);
6305 waddwstr(view->window, wline);
6306 if (view_needs_focus_indication(view))
6307 wstandend(view->window);
6312 if (width < view->ncols - 1)
6313 waddch(view->window, '\n');
6318 while (re && limit > 0) {
6321 if (got_ref_is_symbolic(re->ref)) {
6322 if (asprintf(&line, "%s -> %s",
6323 got_ref_get_name(re->ref),
6324 got_ref_get_symref_target(re->ref)) == -1)
6325 return got_error_from_errno("asprintf");
6326 } else if (s->show_ids) {
6327 struct got_object_id *id;
6329 err = got_ref_resolve(&id, s->repo, re->ref);
6332 err = got_object_id_str(&id_str, id);
6337 if (asprintf(&line, "%s: %s",
6338 got_ref_get_name(re->ref), id_str) == -1) {
6339 err = got_error_from_errno("asprintf");
6347 line = strdup(got_ref_get_name(re->ref));
6349 return got_error_from_errno("strdup");
6352 err = format_line(&wline, &width, line, view->ncols, 0);
6357 if (n == s->selected) {
6359 wstandout(view->window);
6360 s->selected_entry = re;
6362 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6364 wattr_on(view->window,
6365 COLOR_PAIR(tc->colorpair), NULL);
6366 waddwstr(view->window, wline);
6368 wattr_off(view->window,
6369 COLOR_PAIR(tc->colorpair), NULL);
6370 if (width < view->ncols - 1)
6371 waddch(view->window, '\n');
6372 if (n == s->selected && view->focussed)
6373 wstandend(view->window);
6379 s->last_displayed_entry = re;
6382 re = TAILQ_NEXT(re, entry);
6389 static const struct got_error *
6390 browse_ref_tree(struct tog_view **new_view, int begin_x,
6391 struct tog_reflist_entry *re, struct got_repository *repo)
6393 const struct got_error *err = NULL;
6394 struct got_object_id *commit_id = NULL;
6395 struct tog_view *tree_view;
6399 err = resolve_reflist_entry(&commit_id, re, repo);
6401 if (err->code != GOT_ERR_OBJ_TYPE)
6408 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6409 if (tree_view == NULL) {
6410 err = got_error_from_errno("view_open");
6414 err = open_tree_view(tree_view, commit_id,
6415 got_ref_get_name(re->ref), repo);
6419 *new_view = tree_view;
6424 static const struct got_error *
6425 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6427 const struct got_error *err = NULL;
6428 struct tog_ref_view_state *s = &view->state.ref;
6429 struct tog_view *log_view, *tree_view;
6430 struct tog_reflist_entry *re;
6431 int begin_x = 0, n, nscroll = view->nlines - 1;
6435 s->show_ids = !s->show_ids;
6438 s->sort_by_date = !s->sort_by_date;
6439 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6440 got_ref_cmp_by_commit_timestamp_descending :
6441 tog_ref_cmp_by_name, s->repo);
6444 got_reflist_object_id_map_free(tog_refs_idmap);
6445 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6446 &tog_refs, s->repo);
6449 ref_view_free_refs(s);
6450 err = ref_view_load_refs(s);
6454 if (!s->selected_entry)
6456 if (view_is_parent_view(view))
6457 begin_x = view_split_begin_x(view->begin_x);
6458 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6461 log_view->focussed = 1;
6462 if (view_is_parent_view(view)) {
6463 err = view_close_child(view);
6466 view_set_child(view, log_view);
6467 view->focus_child = 1;
6469 *new_view = log_view;
6472 if (!s->selected_entry)
6474 if (view_is_parent_view(view))
6475 begin_x = view_split_begin_x(view->begin_x);
6476 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6478 if (err || tree_view == NULL)
6481 tree_view->focussed = 1;
6482 if (view_is_parent_view(view)) {
6483 err = view_close_child(view);
6486 view_set_child(view, tree_view);
6487 view->focus_child = 1;
6489 *new_view = tree_view;
6494 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6499 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6500 for (n = 0; n < view->nlines - 1; n++) {
6503 s->first_displayed_entry = re;
6504 re = TAILQ_PREV(re, tog_reflist_head, entry);
6507 s->selected = n - 1;
6512 if (s->selected > 0) {
6516 ref_scroll_up(s, 1);
6524 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6525 s->selected -= MIN(nscroll, s->selected);
6526 ref_scroll_up(s, MAX(0, nscroll));
6531 if (s->selected < s->ndisplayed - 1) {
6535 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6536 /* can't scroll any further */
6538 ref_scroll_down(s, 1);
6546 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6547 /* can't scroll any further; move cursor down */
6548 if (s->selected < s->ndisplayed - 1)
6549 s->selected += MIN(nscroll,
6550 s->ndisplayed - s->selected - 1);
6553 ref_scroll_down(s, nscroll);
6557 err = tog_load_refs(s->repo, s->sort_by_date);
6560 ref_view_free_refs(s);
6561 err = ref_view_load_refs(s);
6564 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6565 s->selected = view->nlines - 2;
6578 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6583 static const struct got_error *
6584 cmd_ref(int argc, char *argv[])
6586 const struct got_error *error;
6587 struct got_repository *repo = NULL;
6588 struct got_worktree *worktree = NULL;
6589 char *cwd = NULL, *repo_path = NULL;
6591 struct tog_view *view;
6592 int *pack_fds = NULL;
6594 while ((ch = getopt(argc, argv, "r:")) != -1) {
6597 repo_path = realpath(optarg, NULL);
6598 if (repo_path == NULL)
6599 return got_error_from_errno2("realpath",
6614 error = got_repo_pack_fds_open(&pack_fds);
6618 if (repo_path == NULL) {
6619 cwd = getcwd(NULL, 0);
6621 return got_error_from_errno("getcwd");
6622 error = got_worktree_open(&worktree, cwd);
6623 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6627 strdup(got_worktree_get_repo_path(worktree));
6629 repo_path = strdup(cwd);
6630 if (repo_path == NULL) {
6631 error = got_error_from_errno("strdup");
6636 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6642 error = apply_unveil(got_repo_get_path(repo), NULL);
6646 error = tog_load_refs(repo, 0);
6650 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6652 error = got_error_from_errno("view_open");
6656 error = open_ref_view(view, repo);
6661 /* Release work tree lock. */
6662 got_worktree_close(worktree);
6665 error = view_loop(view);
6670 const struct got_error *close_err = got_repo_close(repo);
6675 const struct got_error *pack_err =
6676 got_repo_pack_fds_close(pack_fds);
6686 list_commands(FILE *fp)
6690 fprintf(fp, "commands:");
6691 for (i = 0; i < nitems(tog_commands); i++) {
6692 const struct tog_cmd *cmd = &tog_commands[i];
6693 fprintf(fp, " %s", cmd->name);
6699 usage(int hflag, int status)
6701 FILE *fp = (status == 0) ? stdout : stderr;
6703 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6706 fprintf(fp, "lazy usage: %s path\n", getprogname());
6713 make_argv(int argc, ...)
6721 argv = calloc(argc, sizeof(char *));
6724 for (i = 0; i < argc; i++) {
6725 argv[i] = strdup(va_arg(ap, char *));
6726 if (argv[i] == NULL)
6735 * Try to convert 'tog path' into a 'tog log path' command.
6736 * The user could simply have mistyped the command rather than knowingly
6737 * provided a path. So check whether argv[0] can in fact be resolved
6738 * to a path in the HEAD commit and print a special error if not.
6739 * This hack is for mpi@ <3
6741 static const struct got_error *
6742 tog_log_with_path(int argc, char *argv[])
6744 const struct got_error *error = NULL, *close_err;
6745 const struct tog_cmd *cmd = NULL;
6746 struct got_repository *repo = NULL;
6747 struct got_worktree *worktree = NULL;
6748 struct got_object_id *commit_id = NULL, *id = NULL;
6749 struct got_commit_object *commit = NULL;
6750 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6751 char *commit_id_str = NULL, **cmd_argv = NULL;
6752 int *pack_fds = NULL;
6754 cwd = getcwd(NULL, 0);
6756 return got_error_from_errno("getcwd");
6758 error = got_repo_pack_fds_open(&pack_fds);
6762 error = got_worktree_open(&worktree, cwd);
6763 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6767 repo_path = strdup(got_worktree_get_repo_path(worktree));
6769 repo_path = strdup(cwd);
6770 if (repo_path == NULL) {
6771 error = got_error_from_errno("strdup");
6775 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6779 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6784 error = tog_load_refs(repo, 0);
6787 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6788 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6789 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6794 got_worktree_close(worktree);
6798 error = got_object_open_as_commit(&commit, repo, commit_id);
6802 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6804 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6806 fprintf(stderr, "%s: '%s' is no known command or path\n",
6807 getprogname(), argv[0]);
6812 close_err = got_repo_close(repo);
6817 error = got_object_id_str(&commit_id_str, commit_id);
6821 cmd = &tog_commands[0]; /* log */
6823 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6824 error = cmd->cmd_main(argc, cmd_argv);
6827 close_err = got_repo_close(repo);
6832 got_object_commit_close(commit);
6834 got_worktree_close(worktree);
6836 const struct got_error *pack_err =
6837 got_repo_pack_fds_close(pack_fds);
6843 free(commit_id_str);
6850 for (i = 0; i < argc; i++)
6859 main(int argc, char *argv[])
6861 const struct got_error *error = NULL;
6862 const struct tog_cmd *cmd = NULL;
6863 int ch, hflag = 0, Vflag = 0;
6864 char **cmd_argv = NULL;
6865 static const struct option longopts[] = {
6866 { "version", no_argument, NULL, 'V' },
6870 setlocale(LC_CTYPE, "");
6872 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6892 got_version_print_str();
6897 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6905 /* Build an argument vector which runs a default command. */
6906 cmd = &tog_commands[0];
6908 cmd_argv = make_argv(argc, cmd->name);
6912 /* Did the user specify a command? */
6913 for (i = 0; i < nitems(tog_commands); i++) {
6914 if (strncmp(tog_commands[i].name, argv[0],
6915 strlen(argv[0])) == 0) {
6916 cmd = &tog_commands[i];
6925 /* No command specified; try log with a path */
6926 error = tog_log_with_path(argc, argv);
6931 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6938 for (i = 0; i < argc; i++)
6943 if (error && error->code != GOT_ERR_CANCELLED)
6944 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);