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_date, 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 maxx, x; /* max column and current start column */
504 int lines, cols; /* copies of LINES and COLS */
505 int focussed; /* Only set on one parent or child view at a time. */
507 struct tog_view *parent;
508 struct tog_view *child;
511 * This flag is initially set on parent views when a new child view
512 * is created. It gets toggled when the 'Tab' key switches focus
513 * between parent and child.
514 * The flag indicates whether focus should be passed on to our child
515 * view if this parent view gets picked for focus after another parent
516 * view was closed. This prevents child views from losing focus in such
521 /* type-specific state */
522 enum tog_view_type type;
524 struct tog_diff_view_state diff;
525 struct tog_log_view_state log;
526 struct tog_blame_view_state blame;
527 struct tog_tree_view_state tree;
528 struct tog_ref_view_state ref;
531 const struct got_error *(*show)(struct tog_view *);
532 const struct got_error *(*input)(struct tog_view **,
533 struct tog_view *, int);
534 const struct got_error *(*close)(struct tog_view *);
536 const struct got_error *(*search_start)(struct tog_view *);
537 const struct got_error *(*search_next)(struct tog_view *);
540 #define TOG_SEARCH_FORWARD 1
541 #define TOG_SEARCH_BACKWARD 2
542 int search_next_done;
543 #define TOG_SEARCH_HAVE_MORE 1
544 #define TOG_SEARCH_NO_MORE 2
545 #define TOG_SEARCH_HAVE_NONE 3
550 static const struct got_error *open_diff_view(struct tog_view *,
551 struct got_object_id *, struct got_object_id *,
552 const char *, const char *, int, int, int, struct tog_view *,
553 struct got_repository *);
554 static const struct got_error *show_diff_view(struct tog_view *);
555 static const struct got_error *input_diff_view(struct tog_view **,
556 struct tog_view *, int);
557 static const struct got_error* close_diff_view(struct tog_view *);
558 static const struct got_error *search_start_diff_view(struct tog_view *);
559 static const struct got_error *search_next_diff_view(struct tog_view *);
561 static const struct got_error *open_log_view(struct tog_view *,
562 struct got_object_id *, struct got_repository *,
563 const char *, const char *, int);
564 static const struct got_error * show_log_view(struct tog_view *);
565 static const struct got_error *input_log_view(struct tog_view **,
566 struct tog_view *, int);
567 static const struct got_error *close_log_view(struct tog_view *);
568 static const struct got_error *search_start_log_view(struct tog_view *);
569 static const struct got_error *search_next_log_view(struct tog_view *);
571 static const struct got_error *open_blame_view(struct tog_view *, char *,
572 struct got_object_id *, struct got_repository *);
573 static const struct got_error *show_blame_view(struct tog_view *);
574 static const struct got_error *input_blame_view(struct tog_view **,
575 struct tog_view *, int);
576 static const struct got_error *close_blame_view(struct tog_view *);
577 static const struct got_error *search_start_blame_view(struct tog_view *);
578 static const struct got_error *search_next_blame_view(struct tog_view *);
580 static const struct got_error *open_tree_view(struct tog_view *,
581 struct got_object_id *, const char *, struct got_repository *);
582 static const struct got_error *show_tree_view(struct tog_view *);
583 static const struct got_error *input_tree_view(struct tog_view **,
584 struct tog_view *, int);
585 static const struct got_error *close_tree_view(struct tog_view *);
586 static const struct got_error *search_start_tree_view(struct tog_view *);
587 static const struct got_error *search_next_tree_view(struct tog_view *);
589 static const struct got_error *open_ref_view(struct tog_view *,
590 struct got_repository *);
591 static const struct got_error *show_ref_view(struct tog_view *);
592 static const struct got_error *input_ref_view(struct tog_view **,
593 struct tog_view *, int);
594 static const struct got_error *close_ref_view(struct tog_view *);
595 static const struct got_error *search_start_ref_view(struct tog_view *);
596 static const struct got_error *search_next_ref_view(struct tog_view *);
598 static volatile sig_atomic_t tog_sigwinch_received;
599 static volatile sig_atomic_t tog_sigpipe_received;
600 static volatile sig_atomic_t tog_sigcont_received;
601 static volatile sig_atomic_t tog_sigint_received;
602 static volatile sig_atomic_t tog_sigterm_received;
605 tog_sigwinch(int signo)
607 tog_sigwinch_received = 1;
611 tog_sigpipe(int signo)
613 tog_sigpipe_received = 1;
617 tog_sigcont(int signo)
619 tog_sigcont_received = 1;
623 tog_sigint(int signo)
625 tog_sigint_received = 1;
629 tog_sigterm(int signo)
631 tog_sigterm_received = 1;
635 tog_fatal_signal_received()
637 return (tog_sigpipe_received ||
638 tog_sigint_received || tog_sigint_received);
642 static const struct got_error *
643 view_close(struct tog_view *view)
645 const struct got_error *err = NULL;
648 view_close(view->child);
652 err = view->close(view);
654 del_panel(view->panel);
656 delwin(view->window);
661 static struct tog_view *
662 view_open(int nlines, int ncols, int begin_y, int begin_x,
663 enum tog_view_type type)
665 struct tog_view *view = calloc(1, sizeof(*view));
673 view->nlines = nlines ? nlines : LINES - begin_y;
674 view->ncols = ncols ? ncols : COLS - begin_x;
675 view->begin_y = begin_y;
676 view->begin_x = begin_x;
677 view->window = newwin(nlines, ncols, begin_y, begin_x);
678 if (view->window == NULL) {
682 view->panel = new_panel(view->window);
683 if (view->panel == NULL ||
684 set_panel_userptr(view->panel, view) != OK) {
689 keypad(view->window, TRUE);
694 view_split_begin_x(int begin_x)
696 if (begin_x > 0 || COLS < 120)
698 return (COLS - MAX(COLS / 2, 80));
701 static const struct got_error *view_resize(struct tog_view *);
703 static const struct got_error *
704 view_splitscreen(struct tog_view *view)
706 const struct got_error *err = NULL;
709 view->begin_x = view_split_begin_x(0);
710 view->nlines = LINES;
711 view->ncols = COLS - view->begin_x;
714 err = view_resize(view);
718 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
719 return got_error_from_errno("mvwin");
724 static const struct got_error *
725 view_fullscreen(struct tog_view *view)
727 const struct got_error *err = NULL;
731 view->nlines = LINES;
735 err = view_resize(view);
739 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
740 return got_error_from_errno("mvwin");
746 view_is_parent_view(struct tog_view *view)
748 return view->parent == NULL;
751 static const struct got_error *
752 view_resize(struct tog_view *view)
756 if (view->lines > LINES)
757 nlines = view->nlines - (view->lines - LINES);
759 nlines = view->nlines + (LINES - view->lines);
761 if (view->cols > COLS)
762 ncols = view->ncols - (view->cols - COLS);
764 ncols = view->ncols + (COLS - view->cols);
766 if (wresize(view->window, nlines, ncols) == ERR)
767 return got_error_from_errno("wresize");
768 if (replace_panel(view->panel, view->window) == ERR)
769 return got_error_from_errno("replace_panel");
770 wclear(view->window);
772 view->nlines = nlines;
778 view->child->begin_x = view_split_begin_x(view->begin_x);
779 if (view->child->begin_x == 0) {
780 view_fullscreen(view->child);
781 if (view->child->focussed)
782 show_panel(view->child->panel);
784 show_panel(view->panel);
786 view_splitscreen(view->child);
787 show_panel(view->child->panel);
794 static const struct got_error *
795 view_close_child(struct tog_view *view)
797 const struct got_error *err = NULL;
799 if (view->child == NULL)
802 err = view_close(view->child);
808 view_set_child(struct tog_view *view, struct tog_view *child)
811 child->parent = view;
815 view_is_splitscreen(struct tog_view *view)
817 return view->begin_x > 0;
826 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
827 cols = 80; /* Default */
833 resize_term(lines, cols);
836 static const struct got_error *
837 view_search_start(struct tog_view *view)
839 const struct got_error *err = NULL;
843 if (view->search_started) {
844 regfree(&view->regex);
846 memset(&view->regmatch, 0, sizeof(view->regmatch));
848 view->search_started = 0;
850 if (view->nlines < 1)
853 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
854 wclrtoeol(view->window);
858 ret = wgetnstr(view->window, pattern, sizeof(pattern));
864 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
865 err = view->search_start(view);
867 regfree(&view->regex);
870 view->search_started = 1;
871 view->searching = TOG_SEARCH_FORWARD;
872 view->search_next_done = 0;
873 view->search_next(view);
879 static const struct got_error *
880 view_input(struct tog_view **new, int *done, struct tog_view *view,
881 struct tog_view_list_head *views)
883 const struct got_error *err = NULL;
889 /* Clear "no matches" indicator. */
890 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
891 view->search_next_done == TOG_SEARCH_HAVE_NONE)
892 view->search_next_done = TOG_SEARCH_HAVE_MORE;
894 if (view->searching && !view->search_next_done) {
895 errcode = pthread_mutex_unlock(&tog_mutex);
897 return got_error_set_errno(errcode,
898 "pthread_mutex_unlock");
900 errcode = pthread_mutex_lock(&tog_mutex);
902 return got_error_set_errno(errcode,
903 "pthread_mutex_lock");
904 view->search_next(view);
908 nodelay(stdscr, FALSE);
909 /* Allow threads to make progress while we are waiting for input. */
910 errcode = pthread_mutex_unlock(&tog_mutex);
912 return got_error_set_errno(errcode, "pthread_mutex_unlock");
913 ch = wgetch(view->window);
914 errcode = pthread_mutex_lock(&tog_mutex);
916 return got_error_set_errno(errcode, "pthread_mutex_lock");
917 nodelay(stdscr, TRUE);
919 if (tog_sigwinch_received || tog_sigcont_received) {
921 tog_sigwinch_received = 0;
922 tog_sigcont_received = 0;
923 TAILQ_FOREACH(v, views, entry) {
924 err = view_resize(v);
927 err = v->input(new, v, KEY_RESIZE);
931 err = view_resize(v->child);
934 err = v->child->input(new, v->child,
946 view->child->focussed = 1;
947 view->focus_child = 1;
948 } else if (view->parent) {
950 view->parent->focussed = 1;
951 view->parent->focus_child = 0;
955 err = view->input(new, view, ch);
962 if (view_is_parent_view(view)) {
963 if (view->child == NULL)
965 if (view_is_splitscreen(view->child)) {
967 view->child->focussed = 1;
968 err = view_fullscreen(view->child);
970 err = view_splitscreen(view->child);
973 err = view->child->input(new, view->child,
976 if (view_is_splitscreen(view)) {
977 view->parent->focussed = 0;
979 err = view_fullscreen(view);
981 err = view_splitscreen(view);
985 err = view->input(new, view, KEY_RESIZE);
991 if (view->search_start)
992 view_search_start(view);
994 err = view->input(new, view, ch);
998 if (view->search_started && view->search_next) {
999 view->searching = (ch == 'n' ?
1000 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1001 view->search_next_done = 0;
1002 view->search_next(view);
1004 err = view->input(new, view, ch);
1007 err = view->input(new, view, ch);
1015 view_vborder(struct tog_view *view)
1018 const struct tog_view *view_above;
1021 return view_vborder(view->parent);
1023 panel = panel_above(view->panel);
1027 view_above = panel_userptr(panel);
1028 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1029 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1033 view_needs_focus_indication(struct tog_view *view)
1035 if (view_is_parent_view(view)) {
1036 if (view->child == NULL || view->child->focussed)
1038 if (!view_is_splitscreen(view->child))
1040 } else if (!view_is_splitscreen(view))
1043 return view->focussed;
1046 static const struct got_error *
1047 view_loop(struct tog_view *view)
1049 const struct got_error *err = NULL;
1050 struct tog_view_list_head views;
1051 struct tog_view *new_view;
1052 int fast_refresh = 10;
1053 int done = 0, errcode;
1055 errcode = pthread_mutex_lock(&tog_mutex);
1057 return got_error_set_errno(errcode, "pthread_mutex_lock");
1060 TAILQ_INSERT_HEAD(&views, view, entry);
1063 err = view->show(view);
1068 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1069 /* Refresh fast during initialization, then become slower. */
1070 if (fast_refresh && fast_refresh-- == 0)
1071 halfdelay(10); /* switch to once per second */
1073 err = view_input(&new_view, &done, view, &views);
1077 struct tog_view *v, *prev = NULL;
1079 if (view_is_parent_view(view))
1080 prev = TAILQ_PREV(view, tog_view_list_head,
1082 else if (view->parent)
1083 prev = view->parent;
1086 view->parent->child = NULL;
1087 view->parent->focus_child = 0;
1089 TAILQ_REMOVE(&views, view, entry);
1091 err = view_close(view);
1096 TAILQ_FOREACH(v, &views, entry) {
1100 if (view == NULL && new_view == NULL) {
1101 /* No view has focus. Try to pick one. */
1104 else if (!TAILQ_EMPTY(&views)) {
1105 view = TAILQ_LAST(&views,
1106 tog_view_list_head);
1109 if (view->focus_child) {
1110 view->child->focussed = 1;
1118 struct tog_view *v, *t;
1119 /* Only allow one parent view per type. */
1120 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1121 if (v->type != new_view->type)
1123 TAILQ_REMOVE(&views, v, entry);
1124 err = view_close(v);
1129 TAILQ_INSERT_TAIL(&views, new_view, entry);
1133 if (view_is_parent_view(view)) {
1134 if (view->child && view->child->focussed)
1137 if (view->parent && view->parent->focussed)
1138 view = view->parent;
1140 show_panel(view->panel);
1141 if (view->child && view_is_splitscreen(view->child))
1142 show_panel(view->child->panel);
1143 if (view->parent && view_is_splitscreen(view)) {
1144 err = view->parent->show(view->parent);
1148 err = view->show(view);
1152 err = view->child->show(view->child);
1161 while (!TAILQ_EMPTY(&views)) {
1162 view = TAILQ_FIRST(&views);
1163 TAILQ_REMOVE(&views, view, entry);
1167 errcode = pthread_mutex_unlock(&tog_mutex);
1168 if (errcode && err == NULL)
1169 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1179 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1184 /* Create newly allocated wide-character string equivalent to a byte string. */
1185 static const struct got_error *
1186 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1189 const struct got_error *err = NULL;
1192 *wlen = mbstowcs(NULL, s, 0);
1193 if (*wlen == (size_t)-1) {
1195 if (errno != EILSEQ)
1196 return got_error_from_errno("mbstowcs");
1198 /* byte string invalid in current encoding; try to "fix" it */
1199 err = got_mbsavis(&vis, &vislen, s);
1202 *wlen = mbstowcs(NULL, vis, 0);
1203 if (*wlen == (size_t)-1) {
1204 err = got_error_from_errno("mbstowcs"); /* give up */
1209 *ws = calloc(*wlen + 1, sizeof(**ws));
1211 err = got_error_from_errno("calloc");
1215 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1216 err = got_error_from_errno("mbstowcs");
1227 static const struct got_error *
1228 expand_tab(char **ptr, const char *src)
1231 size_t len, n, idx = 0, sz = 0;
1234 n = len = strlen(src);
1235 dst = malloc((n + 1) * sizeof(char));
1237 return got_error_from_errno("malloc");
1239 while (idx < len && src[idx]) {
1240 const char c = src[idx];
1243 size_t nb = TABSIZE - sz % TABSIZE;
1245 dst = reallocarray(dst, n, sizeof(char));
1247 return got_error_from_errno("reallocarray");
1248 memcpy(dst + sz, " ", nb);
1251 dst[sz++] = src[idx];
1260 /* Format a line for display, ensuring that it won't overflow a width limit. */
1261 static const struct got_error *
1262 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1263 int col_tab_align, int expand)
1265 const struct got_error *err = NULL;
1267 wchar_t *wline = NULL;
1276 err = expand_tab(&exstr, line);
1281 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1286 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1287 wline[wlen - 1] = L'\0';
1290 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1291 wline[wlen - 1] = L'\0';
1297 int width = wcwidth(wline[i]);
1304 if (width == 1 || width == 2) {
1305 if (cols + width > wlimit)
1309 } else if (width == -1) {
1310 if (wline[i] == L'\t') {
1312 ((cols + col_tab_align) % TABSIZE);
1317 if (cols + width > wlimit)
1322 err = got_error_from_errno("wcwidth");
1337 static const struct got_error*
1338 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1339 struct got_object_id *id, struct got_repository *repo)
1341 static const struct got_error *err = NULL;
1342 struct got_reflist_entry *re;
1348 TAILQ_FOREACH(re, refs, entry) {
1349 struct got_tag_object *tag = NULL;
1350 struct got_object_id *ref_id;
1353 name = got_ref_get_name(re->ref);
1354 if (strcmp(name, GOT_REF_HEAD) == 0)
1356 if (strncmp(name, "refs/", 5) == 0)
1358 if (strncmp(name, "got/", 4) == 0 &&
1359 strncmp(name, "got/backup/", 11) != 0)
1361 if (strncmp(name, "heads/", 6) == 0)
1363 if (strncmp(name, "remotes/", 8) == 0) {
1365 s = strstr(name, "/" GOT_REF_HEAD);
1366 if (s != NULL && s[strlen(s)] == '\0')
1369 err = got_ref_resolve(&ref_id, repo, re->ref);
1372 if (strncmp(name, "tags/", 5) == 0) {
1373 err = got_object_open_as_tag(&tag, repo, ref_id);
1375 if (err->code != GOT_ERR_OBJ_TYPE) {
1379 /* Ref points at something other than a tag. */
1384 cmp = got_object_id_cmp(tag ?
1385 got_object_tag_get_object_id(tag) : ref_id, id);
1388 got_object_tag_close(tag);
1392 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1393 s ? ", " : "", name) == -1) {
1394 err = got_error_from_errno("asprintf");
1405 static const struct got_error *
1406 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1411 smallerthan = strchr(author, '<');
1412 if (smallerthan && smallerthan[1] != '\0')
1413 author = smallerthan + 1;
1414 author[strcspn(author, "@>")] = '\0';
1415 return format_line(wauthor, author_width, author, limit, col_tab_align,
1419 static const struct got_error *
1420 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1421 struct got_object_id *id, const size_t date_display_cols,
1422 int author_display_cols)
1424 struct tog_log_view_state *s = &view->state.log;
1425 const struct got_error *err = NULL;
1426 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1427 char *logmsg0 = NULL, *logmsg = NULL;
1428 char *author = NULL;
1429 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1430 int author_width, logmsg_width;
1431 char *newline, *line = NULL;
1433 const int avail = view->ncols;
1435 time_t committer_time;
1436 struct tog_color *tc;
1438 committer_time = got_object_commit_get_committer_time(commit);
1439 if (gmtime_r(&committer_time, &tm) == NULL)
1440 return got_error_from_errno("gmtime_r");
1441 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1442 return got_error(GOT_ERR_NO_SPACE);
1444 if (avail <= date_display_cols)
1445 limit = MIN(sizeof(datebuf) - 1, avail);
1447 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1448 tc = get_color(&s->colors, TOG_COLOR_DATE);
1450 wattr_on(view->window,
1451 COLOR_PAIR(tc->colorpair), NULL);
1452 waddnstr(view->window, datebuf, limit);
1454 wattr_off(view->window,
1455 COLOR_PAIR(tc->colorpair), NULL);
1462 err = got_object_id_str(&id_str, id);
1465 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1467 wattr_on(view->window,
1468 COLOR_PAIR(tc->colorpair), NULL);
1469 wprintw(view->window, "%.8s ", id_str);
1471 wattr_off(view->window,
1472 COLOR_PAIR(tc->colorpair), NULL);
1479 author = strdup(got_object_commit_get_author(commit));
1480 if (author == NULL) {
1481 err = got_error_from_errno("strdup");
1484 err = format_author(&wauthor, &author_width, author, avail - col, col);
1487 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1489 wattr_on(view->window,
1490 COLOR_PAIR(tc->colorpair), NULL);
1491 waddwstr(view->window, wauthor);
1493 wattr_off(view->window,
1494 COLOR_PAIR(tc->colorpair), NULL);
1495 col += author_width;
1496 while (col < avail && author_width < author_display_cols + 2) {
1497 waddch(view->window, ' ');
1504 err = got_object_commit_get_logmsg(&logmsg0, commit);
1508 while (*logmsg == '\n')
1510 newline = strchr(logmsg, '\n');
1513 limit = view->x + avail - col;
1514 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col, 1);
1517 if (view->x < logmsg_width - 1)
1518 waddwstr(view->window, wlogmsg + view->x);
1521 col += MAX(logmsg_width - view->x, 0);
1522 while (col < avail) {
1523 waddch(view->window, ' ');
1535 static struct commit_queue_entry *
1536 alloc_commit_queue_entry(struct got_commit_object *commit,
1537 struct got_object_id *id)
1539 struct commit_queue_entry *entry;
1541 entry = calloc(1, sizeof(*entry));
1546 entry->commit = commit;
1551 pop_commit(struct commit_queue *commits)
1553 struct commit_queue_entry *entry;
1555 entry = TAILQ_FIRST(&commits->head);
1556 TAILQ_REMOVE(&commits->head, entry, entry);
1557 got_object_commit_close(entry->commit);
1558 commits->ncommits--;
1559 /* Don't free entry->id! It is owned by the commit graph. */
1564 free_commits(struct commit_queue *commits)
1566 while (!TAILQ_EMPTY(&commits->head))
1567 pop_commit(commits);
1570 static const struct got_error *
1571 match_commit(int *have_match, struct got_object_id *id,
1572 struct got_commit_object *commit, regex_t *regex)
1574 const struct got_error *err = NULL;
1575 regmatch_t regmatch;
1576 char *id_str = NULL, *logmsg = NULL;
1580 err = got_object_id_str(&id_str, id);
1584 err = got_object_commit_get_logmsg(&logmsg, commit);
1588 if (regexec(regex, got_object_commit_get_author(commit), 1,
1589 ®match, 0) == 0 ||
1590 regexec(regex, got_object_commit_get_committer(commit), 1,
1591 ®match, 0) == 0 ||
1592 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1593 regexec(regex, logmsg, 1, ®match, 0) == 0)
1601 static const struct got_error *
1602 queue_commits(struct tog_log_thread_args *a)
1604 const struct got_error *err = NULL;
1607 * We keep all commits open throughout the lifetime of the log
1608 * view in order to avoid having to re-fetch commits from disk
1609 * while updating the display.
1612 struct got_object_id *id;
1613 struct got_commit_object *commit;
1614 struct commit_queue_entry *entry;
1617 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1619 if (err || id == NULL)
1622 err = got_object_open_as_commit(&commit, a->repo, id);
1625 entry = alloc_commit_queue_entry(commit, id);
1626 if (entry == NULL) {
1627 err = got_error_from_errno("alloc_commit_queue_entry");
1631 errcode = pthread_mutex_lock(&tog_mutex);
1633 err = got_error_set_errno(errcode,
1634 "pthread_mutex_lock");
1638 entry->idx = a->commits->ncommits;
1639 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1640 a->commits->ncommits++;
1642 if (*a->searching == TOG_SEARCH_FORWARD &&
1643 !*a->search_next_done) {
1645 err = match_commit(&have_match, id, commit, a->regex);
1649 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1652 errcode = pthread_mutex_unlock(&tog_mutex);
1653 if (errcode && err == NULL)
1654 err = got_error_set_errno(errcode,
1655 "pthread_mutex_unlock");
1658 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1664 select_commit(struct tog_log_view_state *s)
1666 struct commit_queue_entry *entry;
1669 entry = s->first_displayed_entry;
1671 if (ncommits == s->selected) {
1672 s->selected_entry = entry;
1675 entry = TAILQ_NEXT(entry, entry);
1680 static const struct got_error *
1681 draw_commits(struct tog_view *view)
1683 const struct got_error *err = NULL;
1684 struct tog_log_view_state *s = &view->state.log;
1685 struct commit_queue_entry *entry = s->selected_entry;
1686 const int limit = view->nlines;
1688 int ncommits, author_cols = 4;
1689 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1690 char *refs_str = NULL;
1692 struct tog_color *tc;
1693 static const size_t date_display_cols = 12;
1695 if (s->selected_entry &&
1696 !(view->searching && view->search_next_done == 0)) {
1697 struct got_reflist_head *refs;
1698 err = got_object_id_str(&id_str, s->selected_entry->id);
1701 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1702 s->selected_entry->id);
1704 err = build_refs_str(&refs_str, refs,
1705 s->selected_entry->id, s->repo);
1711 if (s->thread_args.commits_needed == 0)
1712 halfdelay(10); /* disable fast refresh */
1714 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1715 if (asprintf(&ncommits_str, " [%d/%d] %s",
1716 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1717 (view->searching && !view->search_next_done) ?
1718 "searching..." : "loading...") == -1) {
1719 err = got_error_from_errno("asprintf");
1723 const char *search_str = NULL;
1725 if (view->searching) {
1726 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1727 search_str = "no more matches";
1728 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1729 search_str = "no matches found";
1730 else if (!view->search_next_done)
1731 search_str = "searching...";
1734 if (asprintf(&ncommits_str, " [%d/%d] %s",
1735 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1736 search_str ? search_str :
1737 (refs_str ? refs_str : "")) == -1) {
1738 err = got_error_from_errno("asprintf");
1743 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1744 if (asprintf(&header, "commit %s %s%s",
1745 id_str ? id_str : "........................................",
1746 s->in_repo_path, ncommits_str) == -1) {
1747 err = got_error_from_errno("asprintf");
1751 } else if (asprintf(&header, "commit %s%s",
1752 id_str ? id_str : "........................................",
1753 ncommits_str) == -1) {
1754 err = got_error_from_errno("asprintf");
1758 err = format_line(&wline, &width, header, view->ncols, 0, 0);
1762 werase(view->window);
1764 if (view_needs_focus_indication(view))
1765 wstandout(view->window);
1766 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1768 wattr_on(view->window,
1769 COLOR_PAIR(tc->colorpair), NULL);
1770 waddwstr(view->window, wline);
1772 wattr_off(view->window,
1773 COLOR_PAIR(tc->colorpair), NULL);
1774 while (width < view->ncols) {
1775 waddch(view->window, ' ');
1778 if (view_needs_focus_indication(view))
1779 wstandend(view->window);
1784 /* Grow author column size if necessary. */
1785 entry = s->first_displayed_entry;
1789 char *author, *eol, *msg, *msg0;
1792 if (ncommits >= limit - 1)
1794 author = strdup(got_object_commit_get_author(entry->commit));
1795 if (author == NULL) {
1796 err = got_error_from_errno("strdup");
1799 err = format_author(&wauthor, &width, author, COLS,
1801 if (author_cols < width)
1802 author_cols = width;
1805 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1809 while (*msg == '\n')
1811 if ((eol = strchr(msg, '\n')))
1812 view->maxx = MAX(view->maxx, eol - msg);
1814 view->maxx = MAX(view->maxx, strlen(msg));
1817 entry = TAILQ_NEXT(entry, entry);
1820 entry = s->first_displayed_entry;
1821 s->last_displayed_entry = s->first_displayed_entry;
1824 if (ncommits >= limit - 1)
1826 if (ncommits == s->selected)
1827 wstandout(view->window);
1828 err = draw_commit(view, entry->commit, entry->id,
1829 date_display_cols, author_cols);
1830 if (ncommits == s->selected)
1831 wstandend(view->window);
1835 s->last_displayed_entry = entry;
1836 entry = TAILQ_NEXT(entry, entry);
1849 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1851 struct commit_queue_entry *entry;
1854 entry = TAILQ_FIRST(&s->commits.head);
1855 if (s->first_displayed_entry == entry)
1858 entry = s->first_displayed_entry;
1859 while (entry && nscrolled < maxscroll) {
1860 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1862 s->first_displayed_entry = entry;
1868 static const struct got_error *
1869 trigger_log_thread(struct tog_view *view, int wait)
1871 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1874 halfdelay(1); /* fast refresh while loading commits */
1876 while (ta->commits_needed > 0 || ta->load_all) {
1877 if (ta->log_complete)
1880 /* Wake the log thread. */
1881 errcode = pthread_cond_signal(&ta->need_commits);
1883 return got_error_set_errno(errcode,
1884 "pthread_cond_signal");
1887 * The mutex will be released while the view loop waits
1888 * in wgetch(), at which time the log thread will run.
1893 /* Display progress update in log view. */
1894 show_log_view(view);
1898 /* Wait right here while next commit is being loaded. */
1899 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1901 return got_error_set_errno(errcode,
1902 "pthread_cond_wait");
1904 /* Display progress update in log view. */
1905 show_log_view(view);
1913 static const struct got_error *
1914 log_scroll_down(struct tog_view *view, int maxscroll)
1916 struct tog_log_view_state *s = &view->state.log;
1917 const struct got_error *err = NULL;
1918 struct commit_queue_entry *pentry;
1919 int nscrolled = 0, ncommits_needed;
1921 if (s->last_displayed_entry == NULL)
1924 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1925 if (s->commits.ncommits < ncommits_needed &&
1926 !s->thread_args.log_complete) {
1928 * Ask the log thread for required amount of commits.
1930 s->thread_args.commits_needed += maxscroll;
1931 err = trigger_log_thread(view, 1);
1937 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1941 s->last_displayed_entry = pentry;
1943 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1946 s->first_displayed_entry = pentry;
1947 } while (++nscrolled < maxscroll);
1952 static const struct got_error *
1953 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1954 struct got_commit_object *commit, struct got_object_id *commit_id,
1955 struct tog_view *log_view, struct got_repository *repo)
1957 const struct got_error *err;
1958 struct got_object_qid *parent_id;
1959 struct tog_view *diff_view;
1961 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1962 if (diff_view == NULL)
1963 return got_error_from_errno("view_open");
1965 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1966 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1967 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1969 *new_view = diff_view;
1973 static const struct got_error *
1974 tree_view_visit_subtree(struct tog_tree_view_state *s,
1975 struct got_tree_object *subtree)
1977 struct tog_parent_tree *parent;
1979 parent = calloc(1, sizeof(*parent));
1981 return got_error_from_errno("calloc");
1983 parent->tree = s->tree;
1984 parent->first_displayed_entry = s->first_displayed_entry;
1985 parent->selected_entry = s->selected_entry;
1986 parent->selected = s->selected;
1987 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1990 s->first_displayed_entry = NULL;
1994 static const struct got_error *
1995 tree_view_walk_path(struct tog_tree_view_state *s,
1996 struct got_commit_object *commit, const char *path)
1998 const struct got_error *err = NULL;
1999 struct got_tree_object *tree = NULL;
2001 char *slash, *subpath = NULL;
2003 /* Walk the path and open corresponding tree objects. */
2006 struct got_tree_entry *te;
2007 struct got_object_id *tree_id;
2013 /* Ensure the correct subtree entry is selected. */
2014 slash = strchr(p, '/');
2016 te_name = strdup(p);
2018 te_name = strndup(p, slash - p);
2019 if (te_name == NULL) {
2020 err = got_error_from_errno("strndup");
2023 te = got_object_tree_find_entry(s->tree, te_name);
2025 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2030 s->first_displayed_entry = s->selected_entry = te;
2032 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2033 break; /* jump to this file's entry */
2035 slash = strchr(p, '/');
2037 subpath = strndup(path, slash - path);
2039 subpath = strdup(path);
2040 if (subpath == NULL) {
2041 err = got_error_from_errno("strdup");
2045 err = got_object_id_by_path(&tree_id, s->repo, commit,
2050 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2055 err = tree_view_visit_subtree(s, tree);
2057 got_object_tree_close(tree);
2071 static const struct got_error *
2072 browse_commit_tree(struct tog_view **new_view, int begin_x,
2073 struct commit_queue_entry *entry, const char *path,
2074 const char *head_ref_name, struct got_repository *repo)
2076 const struct got_error *err = NULL;
2077 struct tog_tree_view_state *s;
2078 struct tog_view *tree_view;
2080 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2081 if (tree_view == NULL)
2082 return got_error_from_errno("view_open");
2084 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2087 s = &tree_view->state.tree;
2089 *new_view = tree_view;
2091 if (got_path_is_root_dir(path))
2094 return tree_view_walk_path(s, entry->commit, path);
2097 static const struct got_error *
2098 block_signals_used_by_main_thread(void)
2103 if (sigemptyset(&sigset) == -1)
2104 return got_error_from_errno("sigemptyset");
2106 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2107 if (sigaddset(&sigset, SIGWINCH) == -1)
2108 return got_error_from_errno("sigaddset");
2109 if (sigaddset(&sigset, SIGCONT) == -1)
2110 return got_error_from_errno("sigaddset");
2111 if (sigaddset(&sigset, SIGINT) == -1)
2112 return got_error_from_errno("sigaddset");
2113 if (sigaddset(&sigset, SIGTERM) == -1)
2114 return got_error_from_errno("sigaddset");
2116 /* ncurses handles SIGTSTP */
2117 if (sigaddset(&sigset, SIGTSTP) == -1)
2118 return got_error_from_errno("sigaddset");
2120 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2122 return got_error_set_errno(errcode, "pthread_sigmask");
2128 log_thread(void *arg)
2130 const struct got_error *err = NULL;
2132 struct tog_log_thread_args *a = arg;
2135 err = block_signals_used_by_main_thread();
2139 while (!done && !err && !tog_fatal_signal_received()) {
2140 err = queue_commits(a);
2142 if (err->code != GOT_ERR_ITER_COMPLETED)
2146 } else if (a->commits_needed > 0 && !a->load_all)
2147 a->commits_needed--;
2149 errcode = pthread_mutex_lock(&tog_mutex);
2151 err = got_error_set_errno(errcode,
2152 "pthread_mutex_lock");
2154 } else if (*a->quit)
2156 else if (*a->first_displayed_entry == NULL) {
2157 *a->first_displayed_entry =
2158 TAILQ_FIRST(&a->commits->head);
2159 *a->selected_entry = *a->first_displayed_entry;
2162 errcode = pthread_cond_signal(&a->commit_loaded);
2164 err = got_error_set_errno(errcode,
2165 "pthread_cond_signal");
2166 pthread_mutex_unlock(&tog_mutex);
2171 a->commits_needed = 0;
2173 if (a->commits_needed == 0 && !a->load_all) {
2174 errcode = pthread_cond_wait(&a->need_commits,
2177 err = got_error_set_errno(errcode,
2178 "pthread_cond_wait");
2184 errcode = pthread_mutex_unlock(&tog_mutex);
2185 if (errcode && err == NULL)
2186 err = got_error_set_errno(errcode,
2187 "pthread_mutex_unlock");
2189 a->log_complete = 1;
2193 static const struct got_error *
2194 stop_log_thread(struct tog_log_view_state *s)
2196 const struct got_error *err = NULL;
2201 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2203 return got_error_set_errno(errcode,
2204 "pthread_cond_signal");
2205 errcode = pthread_mutex_unlock(&tog_mutex);
2207 return got_error_set_errno(errcode,
2208 "pthread_mutex_unlock");
2209 errcode = pthread_join(s->thread, (void **)&err);
2211 return got_error_set_errno(errcode, "pthread_join");
2212 errcode = pthread_mutex_lock(&tog_mutex);
2214 return got_error_set_errno(errcode,
2215 "pthread_mutex_lock");
2219 if (s->thread_args.repo) {
2220 err = got_repo_close(s->thread_args.repo);
2221 s->thread_args.repo = NULL;
2224 if (s->thread_args.pack_fds) {
2225 const struct got_error *pack_err =
2226 got_repo_pack_fds_close(s->thread_args.pack_fds);
2229 s->thread_args.pack_fds = NULL;
2232 if (s->thread_args.graph) {
2233 got_commit_graph_close(s->thread_args.graph);
2234 s->thread_args.graph = NULL;
2240 static const struct got_error *
2241 close_log_view(struct tog_view *view)
2243 const struct got_error *err = NULL;
2244 struct tog_log_view_state *s = &view->state.log;
2247 err = stop_log_thread(s);
2249 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2250 if (errcode && err == NULL)
2251 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2253 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2254 if (errcode && err == NULL)
2255 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2257 free_commits(&s->commits);
2258 free(s->in_repo_path);
2259 s->in_repo_path = NULL;
2262 free(s->head_ref_name);
2263 s->head_ref_name = NULL;
2267 static const struct got_error *
2268 search_start_log_view(struct tog_view *view)
2270 struct tog_log_view_state *s = &view->state.log;
2272 s->matched_entry = NULL;
2273 s->search_entry = NULL;
2277 static const struct got_error *
2278 search_next_log_view(struct tog_view *view)
2280 const struct got_error *err = NULL;
2281 struct tog_log_view_state *s = &view->state.log;
2282 struct commit_queue_entry *entry;
2284 /* Display progress update in log view. */
2285 show_log_view(view);
2289 if (s->search_entry) {
2291 errcode = pthread_mutex_unlock(&tog_mutex);
2293 return got_error_set_errno(errcode,
2294 "pthread_mutex_unlock");
2295 ch = wgetch(view->window);
2296 errcode = pthread_mutex_lock(&tog_mutex);
2298 return got_error_set_errno(errcode,
2299 "pthread_mutex_lock");
2300 if (ch == KEY_BACKSPACE) {
2301 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2304 if (view->searching == TOG_SEARCH_FORWARD)
2305 entry = TAILQ_NEXT(s->search_entry, entry);
2307 entry = TAILQ_PREV(s->search_entry,
2308 commit_queue_head, entry);
2309 } else if (s->matched_entry) {
2310 if (view->searching == TOG_SEARCH_FORWARD)
2311 entry = TAILQ_NEXT(s->matched_entry, entry);
2313 entry = TAILQ_PREV(s->matched_entry,
2314 commit_queue_head, entry);
2316 entry = s->selected_entry;
2322 if (entry == NULL) {
2323 if (s->thread_args.log_complete ||
2324 view->searching == TOG_SEARCH_BACKWARD) {
2325 view->search_next_done =
2326 (s->matched_entry == NULL ?
2327 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2328 s->search_entry = NULL;
2332 * Poke the log thread for more commits and return,
2333 * allowing the main loop to make progress. Search
2334 * will resume at s->search_entry once we come back.
2336 s->thread_args.commits_needed++;
2337 return trigger_log_thread(view, 0);
2340 err = match_commit(&have_match, entry->id, entry->commit,
2345 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2346 s->matched_entry = entry;
2350 s->search_entry = entry;
2351 if (view->searching == TOG_SEARCH_FORWARD)
2352 entry = TAILQ_NEXT(entry, entry);
2354 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2357 if (s->matched_entry) {
2358 int cur = s->selected_entry->idx;
2359 while (cur < s->matched_entry->idx) {
2360 err = input_log_view(NULL, view, KEY_DOWN);
2365 while (cur > s->matched_entry->idx) {
2366 err = input_log_view(NULL, view, KEY_UP);
2373 s->search_entry = NULL;
2378 static const struct got_error *
2379 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2380 struct got_repository *repo, const char *head_ref_name,
2381 const char *in_repo_path, int log_branches)
2383 const struct got_error *err = NULL;
2384 struct tog_log_view_state *s = &view->state.log;
2385 struct got_repository *thread_repo = NULL;
2386 struct got_commit_graph *thread_graph = NULL;
2389 if (in_repo_path != s->in_repo_path) {
2390 free(s->in_repo_path);
2391 s->in_repo_path = strdup(in_repo_path);
2392 if (s->in_repo_path == NULL)
2393 return got_error_from_errno("strdup");
2396 /* The commit queue only contains commits being displayed. */
2397 TAILQ_INIT(&s->commits.head);
2398 s->commits.ncommits = 0;
2401 if (head_ref_name) {
2402 s->head_ref_name = strdup(head_ref_name);
2403 if (s->head_ref_name == NULL) {
2404 err = got_error_from_errno("strdup");
2408 s->start_id = got_object_id_dup(start_id);
2409 if (s->start_id == NULL) {
2410 err = got_error_from_errno("got_object_id_dup");
2413 s->log_branches = log_branches;
2415 STAILQ_INIT(&s->colors);
2416 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2417 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2418 get_color_value("TOG_COLOR_COMMIT"));
2421 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2422 get_color_value("TOG_COLOR_AUTHOR"));
2424 free_colors(&s->colors);
2427 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2428 get_color_value("TOG_COLOR_DATE"));
2430 free_colors(&s->colors);
2435 view->show = show_log_view;
2436 view->input = input_log_view;
2437 view->close = close_log_view;
2438 view->search_start = search_start_log_view;
2439 view->search_next = search_next_log_view;
2441 if (s->thread_args.pack_fds == NULL) {
2442 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2446 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2447 s->thread_args.pack_fds);
2450 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2454 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2455 s->repo, NULL, NULL);
2459 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2461 err = got_error_set_errno(errcode, "pthread_cond_init");
2464 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2466 err = got_error_set_errno(errcode, "pthread_cond_init");
2470 s->thread_args.commits_needed = view->nlines;
2471 s->thread_args.graph = thread_graph;
2472 s->thread_args.commits = &s->commits;
2473 s->thread_args.in_repo_path = s->in_repo_path;
2474 s->thread_args.start_id = s->start_id;
2475 s->thread_args.repo = thread_repo;
2476 s->thread_args.log_complete = 0;
2477 s->thread_args.quit = &s->quit;
2478 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2479 s->thread_args.selected_entry = &s->selected_entry;
2480 s->thread_args.searching = &view->searching;
2481 s->thread_args.search_next_done = &view->search_next_done;
2482 s->thread_args.regex = &view->regex;
2485 close_log_view(view);
2489 static const struct got_error *
2490 show_log_view(struct tog_view *view)
2492 const struct got_error *err;
2493 struct tog_log_view_state *s = &view->state.log;
2495 if (s->thread == NULL) {
2496 int errcode = pthread_create(&s->thread, NULL, log_thread,
2499 return got_error_set_errno(errcode, "pthread_create");
2500 if (s->thread_args.commits_needed > 0) {
2501 err = trigger_log_thread(view, 1);
2507 return draw_commits(view);
2510 static const struct got_error *
2511 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2513 const struct got_error *err = NULL;
2514 struct tog_log_view_state *s = &view->state.log;
2515 struct tog_view *diff_view = NULL, *tree_view = NULL;
2516 struct tog_view *ref_view = NULL;
2517 struct commit_queue_entry *entry;
2518 int begin_x = 0, n, nscroll = view->nlines - 1;
2520 if (s->thread_args.load_all) {
2521 if (ch == KEY_BACKSPACE)
2522 s->thread_args.load_all = 0;
2523 else if (s->thread_args.log_complete) {
2524 s->thread_args.load_all = 0;
2525 log_scroll_down(view, s->commits.ncommits);
2526 s->selected = MIN(view->nlines - 2,
2527 s->commits.ncommits - 1);
2541 view->x = MAX(view->maxx - view->ncols / 2, 0);
2545 if (view->x + view->ncols / 2 < view->maxx)
2546 view->x += 2; /* move two columns right */
2550 view->x -= MIN(view->x, 2); /* move two columns back */
2557 if (s->first_displayed_entry == NULL)
2559 if (s->selected > 0)
2562 log_scroll_up(s, 1);
2568 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2577 if (s->first_displayed_entry == NULL)
2579 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2580 s->selected = MAX(0, s->selected - nscroll - 1);
2582 log_scroll_up(s, nscroll);
2590 if (s->first_displayed_entry == NULL)
2592 if (s->selected < MIN(view->nlines - 2,
2593 s->commits.ncommits - 1))
2596 err = log_scroll_down(view, 1);
2604 /* We don't know yet how many commits, so we're forced to
2605 * traverse them all. */
2606 if (!s->thread_args.log_complete) {
2607 s->thread_args.load_all = 1;
2608 return trigger_log_thread(view, 0);
2612 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2613 for (n = 0; n < view->nlines - 1; n++) {
2616 s->first_displayed_entry = entry;
2617 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2620 s->selected = n - 1;
2630 struct commit_queue_entry *first;
2631 first = s->first_displayed_entry;
2634 err = log_scroll_down(view, nscroll);
2637 if (first == s->first_displayed_entry &&
2638 s->selected < MIN(view->nlines - 2,
2639 s->commits.ncommits - 1)) {
2640 /* can't scroll further down */
2641 s->selected += MIN(s->last_displayed_entry->idx -
2642 s->selected_entry->idx, nscroll + 1);
2648 if (s->selected > view->nlines - 2)
2649 s->selected = view->nlines - 2;
2650 if (s->selected > s->commits.ncommits - 1)
2651 s->selected = s->commits.ncommits - 1;
2653 if (s->commits.ncommits < view->nlines - 1 &&
2654 !s->thread_args.log_complete) {
2655 s->thread_args.commits_needed += (view->nlines - 1) -
2656 s->commits.ncommits;
2657 err = trigger_log_thread(view, 1);
2663 if (s->selected_entry == NULL)
2665 if (view_is_parent_view(view))
2666 begin_x = view_split_begin_x(view->begin_x);
2667 err = open_diff_view_for_commit(&diff_view, begin_x,
2668 s->selected_entry->commit, s->selected_entry->id,
2673 diff_view->focussed = 1;
2674 if (view_is_parent_view(view)) {
2675 err = view_close_child(view);
2678 view_set_child(view, diff_view);
2679 view->focus_child = 1;
2681 *new_view = diff_view;
2684 if (s->selected_entry == NULL)
2686 if (view_is_parent_view(view))
2687 begin_x = view_split_begin_x(view->begin_x);
2688 err = browse_commit_tree(&tree_view, begin_x,
2689 s->selected_entry, s->in_repo_path, s->head_ref_name,
2694 tree_view->focussed = 1;
2695 if (view_is_parent_view(view)) {
2696 err = view_close_child(view);
2699 view_set_child(view, tree_view);
2700 view->focus_child = 1;
2702 *new_view = tree_view;
2707 if (ch == KEY_BACKSPACE &&
2708 got_path_is_root_dir(s->in_repo_path))
2710 err = stop_log_thread(s);
2713 if (ch == KEY_BACKSPACE) {
2715 err = got_path_dirname(&parent_path, s->in_repo_path);
2718 free(s->in_repo_path);
2719 s->in_repo_path = parent_path;
2720 s->thread_args.in_repo_path = s->in_repo_path;
2721 } else if (ch == CTRL('l')) {
2722 struct got_object_id *start_id;
2723 err = got_repo_match_object_id(&start_id, NULL,
2724 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2725 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2729 s->start_id = start_id;
2730 s->thread_args.start_id = s->start_id;
2732 s->log_branches = !s->log_branches;
2734 err = got_repo_open(&s->thread_args.repo,
2735 got_repo_get_path(s->repo), NULL,
2736 s->thread_args.pack_fds);
2740 err = tog_load_refs(s->repo, 0);
2743 err = got_commit_graph_open(&s->thread_args.graph,
2744 s->in_repo_path, !s->log_branches);
2747 err = got_commit_graph_iter_start(s->thread_args.graph,
2748 s->start_id, s->repo, NULL, NULL);
2751 free_commits(&s->commits);
2752 s->first_displayed_entry = NULL;
2753 s->last_displayed_entry = NULL;
2754 s->selected_entry = NULL;
2756 s->thread_args.log_complete = 0;
2758 s->thread_args.commits_needed = view->nlines;
2761 if (view_is_parent_view(view))
2762 begin_x = view_split_begin_x(view->begin_x);
2763 ref_view = view_open(view->nlines, view->ncols,
2764 view->begin_y, begin_x, TOG_VIEW_REF);
2765 if (ref_view == NULL)
2766 return got_error_from_errno("view_open");
2767 err = open_ref_view(ref_view, s->repo);
2769 view_close(ref_view);
2773 ref_view->focussed = 1;
2774 if (view_is_parent_view(view)) {
2775 err = view_close_child(view);
2778 view_set_child(view, ref_view);
2779 view->focus_child = 1;
2781 *new_view = ref_view;
2790 static const struct got_error *
2791 apply_unveil(const char *repo_path, const char *worktree_path)
2793 const struct got_error *error;
2796 if (unveil("gmon.out", "rwc") != 0)
2797 return got_error_from_errno2("unveil", "gmon.out");
2799 if (repo_path && unveil(repo_path, "r") != 0)
2800 return got_error_from_errno2("unveil", repo_path);
2802 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2803 return got_error_from_errno2("unveil", worktree_path);
2805 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2806 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2808 error = got_privsep_unveil_exec_helpers();
2812 if (unveil(NULL, NULL) != 0)
2813 return got_error_from_errno("unveil");
2822 * Override default signal handlers before starting ncurses.
2823 * This should prevent ncurses from installing its own
2824 * broken cleanup() signal handler.
2826 signal(SIGWINCH, tog_sigwinch);
2827 signal(SIGPIPE, tog_sigpipe);
2828 signal(SIGCONT, tog_sigcont);
2829 signal(SIGINT, tog_sigint);
2830 signal(SIGTERM, tog_sigterm);
2834 halfdelay(1); /* Do fast refresh while initial view is loading. */
2837 intrflush(stdscr, FALSE);
2838 keypad(stdscr, TRUE);
2840 if (getenv("TOG_COLORS") != NULL) {
2842 use_default_colors();
2846 static const struct got_error *
2847 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2848 struct got_repository *repo, struct got_worktree *worktree)
2850 const struct got_error *err = NULL;
2853 *in_repo_path = strdup("/");
2854 if (*in_repo_path == NULL)
2855 return got_error_from_errno("strdup");
2860 const char *prefix = got_worktree_get_path_prefix(worktree);
2863 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2866 if (asprintf(in_repo_path, "%s%s%s", prefix,
2867 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2869 err = got_error_from_errno("asprintf");
2870 *in_repo_path = NULL;
2874 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2879 static const struct got_error *
2880 cmd_log(int argc, char *argv[])
2882 const struct got_error *error;
2883 struct got_repository *repo = NULL;
2884 struct got_worktree *worktree = NULL;
2885 struct got_object_id *start_id = NULL;
2886 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2887 char *start_commit = NULL, *label = NULL;
2888 struct got_reference *ref = NULL;
2889 const char *head_ref_name = NULL;
2890 int ch, log_branches = 0;
2891 struct tog_view *view;
2892 int *pack_fds = NULL;
2894 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2900 start_commit = optarg;
2903 repo_path = realpath(optarg, NULL);
2904 if (repo_path == NULL)
2905 return got_error_from_errno2("realpath",
2920 error = got_repo_pack_fds_open(&pack_fds);
2924 if (repo_path == NULL) {
2925 cwd = getcwd(NULL, 0);
2927 return got_error_from_errno("getcwd");
2928 error = got_worktree_open(&worktree, cwd);
2929 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2933 strdup(got_worktree_get_repo_path(worktree));
2935 repo_path = strdup(cwd);
2936 if (repo_path == NULL) {
2937 error = got_error_from_errno("strdup");
2942 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2946 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2953 error = apply_unveil(got_repo_get_path(repo),
2954 worktree ? got_worktree_get_root_path(worktree) : NULL);
2958 /* already loaded by tog_log_with_path()? */
2959 if (TAILQ_EMPTY(&tog_refs)) {
2960 error = tog_load_refs(repo, 0);
2965 if (start_commit == NULL) {
2966 error = got_repo_match_object_id(&start_id, &label,
2967 worktree ? got_worktree_get_head_ref_name(worktree) :
2968 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2971 head_ref_name = label;
2973 error = got_ref_open(&ref, repo, start_commit, 0);
2975 head_ref_name = got_ref_get_name(ref);
2976 else if (error->code != GOT_ERR_NOT_REF)
2978 error = got_repo_match_object_id(&start_id, NULL,
2979 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2984 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2986 error = got_error_from_errno("view_open");
2989 error = open_log_view(view, start_id, repo, head_ref_name,
2990 in_repo_path, log_branches);
2994 /* Release work tree lock. */
2995 got_worktree_close(worktree);
2998 error = view_loop(view);
3008 const struct got_error *close_err = got_repo_close(repo);
3013 got_worktree_close(worktree);
3015 const struct got_error *pack_err =
3016 got_repo_pack_fds_close(pack_fds);
3028 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3029 "[-w] object1 object2\n", getprogname());
3034 match_line(const char *line, regex_t *regex, size_t nmatch,
3035 regmatch_t *regmatch)
3037 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3041 match_color(struct tog_colors *colors, const char *line)
3043 struct tog_color *tc = NULL;
3045 STAILQ_FOREACH(tc, colors, entry) {
3046 if (match_line(line, &tc->regex, 0, NULL))
3053 static const struct got_error *
3054 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3055 WINDOW *window, int skip, regmatch_t *regmatch)
3057 const struct got_error *err = NULL;
3059 int rme, rms, n, width;
3062 rms = regmatch->rm_so;
3063 rme = regmatch->rm_eo;
3065 err = format_line(&wline, &width, line, wlimit + skip,
3070 /* draw up to matched token if we haven't scrolled past it */
3071 n = MAX(rms - skip, 0);
3073 waddnwstr(window, wline + skip, n);
3079 int len = rme - rms;
3083 len = MAX(len - n, 0);
3085 /* draw (visible part of) matched token (if scrolled into it) */
3087 wattron(window, A_STANDOUT);
3088 waddnwstr(window, wline + rms + n, len);
3089 wattroff(window, A_STANDOUT);
3095 if (wlimit > 0 && skip < width) { /* draw rest of line */
3098 n = MIN(skip - rme, width - rme);
3099 waddnwstr(window, wline + rme + n, wlimit);
3107 static const struct got_error *
3108 draw_file(struct tog_view *view, const char *header)
3110 struct tog_diff_view_state *s = &view->state.diff;
3111 regmatch_t *regmatch = &view->regmatch;
3112 const struct got_error *err;
3115 size_t linesize = 0;
3117 struct tog_color *tc;
3120 int max_lines = view->nlines;
3121 int nlines = s->nlines;
3124 line_offset = s->line_offsets[s->first_displayed_line - 1];
3125 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3126 return got_error_from_errno("fseek");
3128 werase(view->window);
3131 if (asprintf(&line, "[%d/%d] %s",
3132 s->first_displayed_line - 1 + s->selected_line, nlines,
3134 return got_error_from_errno("asprintf");
3135 err = format_line(&wline, &width, line, view->ncols, 0, 0);
3140 if (view_needs_focus_indication(view))
3141 wstandout(view->window);
3142 waddwstr(view->window, wline);
3145 if (view_needs_focus_indication(view))
3146 wstandend(view->window);
3147 if (width <= view->ncols - 1)
3148 waddch(view->window, '\n');
3158 while (max_lines > 0 && nprinted < max_lines) {
3159 linelen = getline(&line, &linesize, s->f);
3160 if (linelen == -1) {
3166 return got_ferror(s->f, GOT_ERR_IO);
3169 view->maxx = MAX(view->maxx, linelen);
3171 tc = match_color(&s->colors, line);
3173 wattr_on(view->window,
3174 COLOR_PAIR(tc->colorpair), NULL);
3175 if (s->first_displayed_line + nprinted == s->matched_line &&
3176 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3177 err = add_matched_line(&width, line, view->ncols, 0,
3178 view->window, view->x, regmatch);
3184 err = format_line(&wline, &width, line,
3185 view->x + view->ncols, 0, view->x ? 1 : 0);
3190 if (view->x < width - 1)
3191 waddwstr(view->window, wline + view->x);
3196 wattr_off(view->window,
3197 COLOR_PAIR(tc->colorpair), NULL);
3198 if (width - view->x <= view->ncols - 1)
3199 waddch(view->window, '\n');
3204 s->last_displayed_line = s->first_displayed_line +
3207 s->last_displayed_line = s->first_displayed_line;
3212 while (nprinted < view->nlines) {
3213 waddch(view->window, '\n');
3217 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols,
3223 wstandout(view->window);
3224 waddwstr(view->window, wline);
3227 wstandend(view->window);
3234 get_datestr(time_t *time, char *datebuf)
3236 struct tm mytm, *tm;
3239 tm = gmtime_r(time, &mytm);
3242 s = asctime_r(tm, datebuf);
3245 p = strchr(s, '\n');
3251 static const struct got_error *
3252 get_changed_paths(struct got_pathlist_head *paths,
3253 struct got_commit_object *commit, struct got_repository *repo)
3255 const struct got_error *err = NULL;
3256 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3257 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3258 struct got_object_qid *qid;
3260 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3262 struct got_commit_object *pcommit;
3263 err = got_object_open_as_commit(&pcommit, repo,
3268 tree_id1 = got_object_id_dup(
3269 got_object_commit_get_tree_id(pcommit));
3270 if (tree_id1 == NULL) {
3271 got_object_commit_close(pcommit);
3272 return got_error_from_errno("got_object_id_dup");
3274 got_object_commit_close(pcommit);
3279 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3284 tree_id2 = got_object_commit_get_tree_id(commit);
3285 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3289 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3290 got_diff_tree_collect_changed_paths, paths, 0);
3293 got_object_tree_close(tree1);
3295 got_object_tree_close(tree2);
3300 static const struct got_error *
3301 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3305 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3307 return got_error_from_errno("reallocarray");
3309 (*line_offsets)[*nlines] = off;
3314 static const struct got_error *
3315 write_commit_info(off_t **line_offsets, size_t *nlines,
3316 struct got_object_id *commit_id, struct got_reflist_head *refs,
3317 struct got_repository *repo, FILE *outfile)
3319 const struct got_error *err = NULL;
3320 char datebuf[26], *datestr;
3321 struct got_commit_object *commit;
3322 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3323 time_t committer_time;
3324 const char *author, *committer;
3325 char *refs_str = NULL;
3326 struct got_pathlist_head changed_paths;
3327 struct got_pathlist_entry *pe;
3331 TAILQ_INIT(&changed_paths);
3334 err = build_refs_str(&refs_str, refs, commit_id, repo);
3339 err = got_object_open_as_commit(&commit, repo, commit_id);
3343 err = got_object_id_str(&id_str, commit_id);
3345 err = got_error_from_errno("got_object_id_str");
3349 err = add_line_offset(line_offsets, nlines, 0);
3353 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3354 refs_str ? refs_str : "", refs_str ? ")" : "");
3356 err = got_error_from_errno("fprintf");
3360 err = add_line_offset(line_offsets, nlines, outoff);
3364 n = fprintf(outfile, "from: %s\n",
3365 got_object_commit_get_author(commit));
3367 err = got_error_from_errno("fprintf");
3371 err = add_line_offset(line_offsets, nlines, outoff);
3375 committer_time = got_object_commit_get_committer_time(commit);
3376 datestr = get_datestr(&committer_time, datebuf);
3378 n = fprintf(outfile, "date: %s UTC\n", datestr);
3380 err = got_error_from_errno("fprintf");
3384 err = add_line_offset(line_offsets, nlines, outoff);
3388 author = got_object_commit_get_author(commit);
3389 committer = got_object_commit_get_committer(commit);
3390 if (strcmp(author, committer) != 0) {
3391 n = fprintf(outfile, "via: %s\n", committer);
3393 err = got_error_from_errno("fprintf");
3397 err = add_line_offset(line_offsets, nlines, outoff);
3401 if (got_object_commit_get_nparents(commit) > 1) {
3402 const struct got_object_id_queue *parent_ids;
3403 struct got_object_qid *qid;
3405 parent_ids = got_object_commit_get_parent_ids(commit);
3406 STAILQ_FOREACH(qid, parent_ids, entry) {
3407 err = got_object_id_str(&id_str, &qid->id);
3410 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3412 err = got_error_from_errno("fprintf");
3416 err = add_line_offset(line_offsets, nlines, outoff);
3424 err = got_object_commit_get_logmsg(&logmsg, commit);
3428 while ((line = strsep(&s, "\n")) != NULL) {
3429 n = fprintf(outfile, "%s\n", line);
3431 err = got_error_from_errno("fprintf");
3435 err = add_line_offset(line_offsets, nlines, outoff);
3440 err = get_changed_paths(&changed_paths, commit, repo);
3443 TAILQ_FOREACH(pe, &changed_paths, entry) {
3444 struct got_diff_changed_path *cp = pe->data;
3445 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3447 err = got_error_from_errno("fprintf");
3451 err = add_line_offset(line_offsets, nlines, outoff);
3454 free((char *)pe->path);
3458 fputc('\n', outfile);
3460 err = add_line_offset(line_offsets, nlines, outoff);
3462 got_pathlist_free(&changed_paths);
3466 got_object_commit_close(commit);
3468 free(*line_offsets);
3469 *line_offsets = NULL;
3475 static const struct got_error *
3476 create_diff(struct tog_diff_view_state *s)
3478 const struct got_error *err = NULL;
3482 free(s->line_offsets);
3483 s->line_offsets = malloc(sizeof(off_t));
3484 if (s->line_offsets == NULL)
3485 return got_error_from_errno("malloc");
3490 err = got_error_from_errno("got_opentemp");
3493 if (s->f && fclose(s->f) == EOF) {
3494 err = got_error_from_errno("fclose");
3500 err = got_object_get_type(&obj_type, s->repo, s->id1);
3502 err = got_object_get_type(&obj_type, s->repo, s->id2);
3507 case GOT_OBJ_TYPE_BLOB:
3508 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3509 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3510 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3513 case GOT_OBJ_TYPE_TREE:
3514 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3515 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3516 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3518 case GOT_OBJ_TYPE_COMMIT: {
3519 const struct got_object_id_queue *parent_ids;
3520 struct got_object_qid *pid;
3521 struct got_commit_object *commit2;
3522 struct got_reflist_head *refs;
3524 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3527 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3528 /* Show commit info if we're diffing to a parent/root commit. */
3529 if (s->id1 == NULL) {
3530 err = write_commit_info(&s->line_offsets, &s->nlines,
3531 s->id2, refs, s->repo, s->f);
3535 parent_ids = got_object_commit_get_parent_ids(commit2);
3536 STAILQ_FOREACH(pid, parent_ids, entry) {
3537 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3538 err = write_commit_info(
3539 &s->line_offsets, &s->nlines,
3540 s->id2, refs, s->repo, s->f);
3547 got_object_commit_close(commit2);
3549 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3550 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3551 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3555 err = got_error(GOT_ERR_OBJ_TYPE);
3561 if (s->f && fflush(s->f) != 0 && err == NULL)
3562 err = got_error_from_errno("fflush");
3567 diff_view_indicate_progress(struct tog_view *view)
3569 mvwaddstr(view->window, 0, 0, "diffing...");
3574 static const struct got_error *
3575 search_start_diff_view(struct tog_view *view)
3577 struct tog_diff_view_state *s = &view->state.diff;
3579 s->matched_line = 0;
3583 static const struct got_error *
3584 search_next_diff_view(struct tog_view *view)
3586 struct tog_diff_view_state *s = &view->state.diff;
3587 const struct got_error *err = NULL;
3589 char *exstr = NULL, *line = NULL;
3590 size_t linesize = 0;
3593 if (!view->searching) {
3594 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3598 if (s->matched_line) {
3599 if (view->searching == TOG_SEARCH_FORWARD)
3600 lineno = s->matched_line + 1;
3602 lineno = s->matched_line - 1;
3604 lineno = s->first_displayed_line;
3609 if (lineno <= 0 || lineno > s->nlines) {
3610 if (s->matched_line == 0) {
3611 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3615 if (view->searching == TOG_SEARCH_FORWARD)
3621 offset = s->line_offsets[lineno - 1];
3622 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3624 return got_error_from_errno("fseeko");
3626 linelen = getline(&line, &linesize, s->f);
3627 err = expand_tab(&exstr, line);
3630 if (linelen != -1 &&
3631 match_line(exstr, &view->regex, 1, &view->regmatch)) {
3632 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3633 s->matched_line = lineno;
3638 if (view->searching == TOG_SEARCH_FORWARD)
3646 if (s->matched_line) {
3647 s->first_displayed_line = s->matched_line;
3648 s->selected_line = 1;
3654 static const struct got_error *
3655 close_diff_view(struct tog_view *view)
3657 const struct got_error *err = NULL;
3658 struct tog_diff_view_state *s = &view->state.diff;
3664 if (s->f && fclose(s->f) == EOF)
3665 err = got_error_from_errno("fclose");
3667 if (s->f1 && fclose(s->f1) == EOF)
3668 err = got_error_from_errno("fclose");
3670 if (s->f2 && fclose(s->f2) == EOF)
3671 err = got_error_from_errno("fclose");
3673 free_colors(&s->colors);
3674 free(s->line_offsets);
3675 s->line_offsets = NULL;
3680 static const struct got_error *
3681 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3682 struct got_object_id *id2, const char *label1, const char *label2,
3683 int diff_context, int ignore_whitespace, int force_text_diff,
3684 struct tog_view *log_view, struct got_repository *repo)
3686 const struct got_error *err;
3687 struct tog_diff_view_state *s = &view->state.diff;
3689 memset(s, 0, sizeof(*s));
3691 if (id1 != NULL && id2 != NULL) {
3693 err = got_object_get_type(&type1, repo, id1);
3696 err = got_object_get_type(&type2, repo, id2);
3701 return got_error(GOT_ERR_OBJ_TYPE);
3703 s->first_displayed_line = 1;
3704 s->last_displayed_line = view->nlines;
3705 s->selected_line = 1;
3713 s->id1 = got_object_id_dup(id1);
3715 return got_error_from_errno("got_object_id_dup");
3716 s->f1 = got_opentemp();
3717 if (s->f1 == NULL) {
3718 err = got_error_from_errno("got_opentemp");
3724 s->id2 = got_object_id_dup(id2);
3725 if (s->id2 == NULL) {
3726 err = got_error_from_errno("got_object_id_dup");
3730 s->f2 = got_opentemp();
3731 if (s->f2 == NULL) {
3732 err = got_error_from_errno("got_opentemp");
3736 s->first_displayed_line = 1;
3737 s->last_displayed_line = view->nlines;
3738 s->diff_context = diff_context;
3739 s->ignore_whitespace = ignore_whitespace;
3740 s->force_text_diff = force_text_diff;
3741 s->log_view = log_view;
3744 STAILQ_INIT(&s->colors);
3745 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3746 err = add_color(&s->colors,
3747 "^-", TOG_COLOR_DIFF_MINUS,
3748 get_color_value("TOG_COLOR_DIFF_MINUS"));
3751 err = add_color(&s->colors, "^\\+",
3752 TOG_COLOR_DIFF_PLUS,
3753 get_color_value("TOG_COLOR_DIFF_PLUS"));
3756 err = add_color(&s->colors,
3757 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3758 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3762 err = add_color(&s->colors,
3763 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3764 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3765 get_color_value("TOG_COLOR_DIFF_META"));
3769 err = add_color(&s->colors,
3770 "^(from|via): ", TOG_COLOR_AUTHOR,
3771 get_color_value("TOG_COLOR_AUTHOR"));
3775 err = add_color(&s->colors,
3776 "^date: ", TOG_COLOR_DATE,
3777 get_color_value("TOG_COLOR_DATE"));
3782 if (log_view && view_is_splitscreen(view))
3783 show_log_view(log_view); /* draw vborder */
3784 diff_view_indicate_progress(view);
3786 err = create_diff(s);
3788 view->show = show_diff_view;
3789 view->input = input_diff_view;
3790 view->close = close_diff_view;
3791 view->search_start = search_start_diff_view;
3792 view->search_next = search_next_diff_view;
3795 close_diff_view(view);
3799 static const struct got_error *
3800 show_diff_view(struct tog_view *view)
3802 const struct got_error *err;
3803 struct tog_diff_view_state *s = &view->state.diff;
3804 char *id_str1 = NULL, *id_str2, *header;
3805 const char *label1, *label2;
3808 err = got_object_id_str(&id_str1, s->id1);
3811 label1 = s->label1 ? : id_str1;
3813 label1 = "/dev/null";
3815 err = got_object_id_str(&id_str2, s->id2);
3818 label2 = s->label2 ? : id_str2;
3820 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3821 err = got_error_from_errno("asprintf");
3829 err = draw_file(view, header);
3834 static const struct got_error *
3835 set_selected_commit(struct tog_diff_view_state *s,
3836 struct commit_queue_entry *entry)
3838 const struct got_error *err;
3839 const struct got_object_id_queue *parent_ids;
3840 struct got_commit_object *selected_commit;
3841 struct got_object_qid *pid;
3844 s->id2 = got_object_id_dup(entry->id);
3846 return got_error_from_errno("got_object_id_dup");
3848 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3851 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3853 pid = STAILQ_FIRST(parent_ids);
3854 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3855 got_object_commit_close(selected_commit);
3859 static const struct got_error *
3860 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3862 const struct got_error *err = NULL;
3863 struct tog_diff_view_state *s = &view->state.diff;
3864 struct tog_log_view_state *ls;
3865 struct commit_queue_entry *old_selected_entry;
3867 size_t linesize = 0;
3869 int i, nscroll = view->nlines - 1;
3876 view->x = MAX(view->maxx - view->ncols / 3, 0);
3880 if (view->x + view->ncols / 3 < view->maxx)
3881 view->x += 2; /* move two columns right */
3885 view->x -= MIN(view->x, 2); /* move two columns back */
3890 s->force_text_diff = !s->force_text_diff;
3892 s->ignore_whitespace = !s->ignore_whitespace;
3893 wclear(view->window);
3894 s->first_displayed_line = 1;
3895 s->last_displayed_line = view->nlines;
3896 s->matched_line = 0;
3897 diff_view_indicate_progress(view);
3898 err = create_diff(s);
3902 s->first_displayed_line = 1;
3909 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3915 if (s->first_displayed_line > 1)
3916 s->first_displayed_line--;
3924 if (s->first_displayed_line == 1)
3927 while (i++ < nscroll && s->first_displayed_line > 1)
3928 s->first_displayed_line--;
3934 s->first_displayed_line++;
3946 while (!s->eof && i++ < nscroll) {
3947 linelen = getline(&line, &linesize, s->f);
3948 s->first_displayed_line++;
3949 if (linelen == -1) {
3953 err = got_ferror(s->f, GOT_ERR_IO);
3960 if (s->diff_context > 0) {
3962 s->matched_line = 0;
3963 diff_view_indicate_progress(view);
3964 err = create_diff(s);
3965 if (s->first_displayed_line + view->nlines - 1 >
3967 s->first_displayed_line = 1;
3968 s->last_displayed_line = view->nlines;
3973 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3975 s->matched_line = 0;
3976 diff_view_indicate_progress(view);
3977 err = create_diff(s);
3982 if (s->log_view == NULL)
3984 ls = &s->log_view->state.log;
3985 old_selected_entry = ls->selected_entry;
3987 err = input_log_view(NULL, s->log_view, KEY_UP);
3991 if (old_selected_entry == ls->selected_entry)
3994 err = set_selected_commit(s, ls->selected_entry);
3998 s->first_displayed_line = 1;
3999 s->last_displayed_line = view->nlines;
4000 s->matched_line = 0;
4003 diff_view_indicate_progress(view);
4004 err = create_diff(s);
4008 if (s->log_view == NULL)
4010 ls = &s->log_view->state.log;
4011 old_selected_entry = ls->selected_entry;
4013 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4017 if (old_selected_entry == ls->selected_entry)
4020 err = set_selected_commit(s, ls->selected_entry);
4024 s->first_displayed_line = 1;
4025 s->last_displayed_line = view->nlines;
4026 s->matched_line = 0;
4029 diff_view_indicate_progress(view);
4030 err = create_diff(s);
4039 static const struct got_error *
4040 cmd_diff(int argc, char *argv[])
4042 const struct got_error *error = NULL;
4043 struct got_repository *repo = NULL;
4044 struct got_worktree *worktree = NULL;
4045 struct got_object_id *id1 = NULL, *id2 = NULL;
4046 char *repo_path = NULL, *cwd = NULL;
4047 char *id_str1 = NULL, *id_str2 = NULL;
4048 char *label1 = NULL, *label2 = NULL;
4049 int diff_context = 3, ignore_whitespace = 0;
4050 int ch, force_text_diff = 0;
4052 struct tog_view *view;
4053 int *pack_fds = NULL;
4055 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4058 force_text_diff = 1;
4061 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4064 errx(1, "number of context lines is %s: %s",
4068 repo_path = realpath(optarg, NULL);
4069 if (repo_path == NULL)
4070 return got_error_from_errno2("realpath",
4072 got_path_strip_trailing_slashes(repo_path);
4075 ignore_whitespace = 1;
4087 usage_diff(); /* TODO show local worktree changes */
4088 } else if (argc == 2) {
4094 error = got_repo_pack_fds_open(&pack_fds);
4098 if (repo_path == NULL) {
4099 cwd = getcwd(NULL, 0);
4101 return got_error_from_errno("getcwd");
4102 error = got_worktree_open(&worktree, cwd);
4103 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4107 strdup(got_worktree_get_repo_path(worktree));
4109 repo_path = strdup(cwd);
4110 if (repo_path == NULL) {
4111 error = got_error_from_errno("strdup");
4116 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4122 error = apply_unveil(got_repo_get_path(repo), NULL);
4126 error = tog_load_refs(repo, 0);
4130 error = got_repo_match_object_id(&id1, &label1, id_str1,
4131 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4135 error = got_repo_match_object_id(&id2, &label2, id_str2,
4136 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4140 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4142 error = got_error_from_errno("view_open");
4145 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4146 ignore_whitespace, force_text_diff, NULL, repo);
4149 error = view_loop(view);
4156 const struct got_error *close_err = got_repo_close(repo);
4161 got_worktree_close(worktree);
4163 const struct got_error *pack_err =
4164 got_repo_pack_fds_close(pack_fds);
4176 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4181 struct tog_blame_line {
4183 struct got_object_id *id;
4186 static const struct got_error *
4187 draw_blame(struct tog_view *view)
4189 struct tog_blame_view_state *s = &view->state.blame;
4190 struct tog_blame *blame = &s->blame;
4191 regmatch_t *regmatch = &view->regmatch;
4192 const struct got_error *err;
4193 int lineno = 0, nprinted = 0;
4195 size_t linesize = 0;
4199 struct tog_blame_line *blame_line;
4200 struct got_object_id *prev_id = NULL;
4202 struct tog_color *tc;
4204 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4209 werase(view->window);
4211 if (asprintf(&line, "commit %s", id_str) == -1) {
4212 err = got_error_from_errno("asprintf");
4217 err = format_line(&wline, &width, line, view->ncols, 0, 0);
4222 if (view_needs_focus_indication(view))
4223 wstandout(view->window);
4224 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4226 wattr_on(view->window,
4227 COLOR_PAIR(tc->colorpair), NULL);
4228 waddwstr(view->window, wline);
4230 wattr_off(view->window,
4231 COLOR_PAIR(tc->colorpair), NULL);
4232 if (view_needs_focus_indication(view))
4233 wstandend(view->window);
4236 if (width < view->ncols - 1)
4237 waddch(view->window, '\n');
4239 if (asprintf(&line, "[%d/%d] %s%s",
4240 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4241 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4243 return got_error_from_errno("asprintf");
4246 err = format_line(&wline, &width, line, view->ncols, 0, 0);
4251 waddwstr(view->window, wline);
4254 if (width < view->ncols - 1)
4255 waddch(view->window, '\n');
4259 while (nprinted < view->nlines - 2) {
4260 linelen = getline(&line, &linesize, blame->f);
4261 if (linelen == -1) {
4262 if (feof(blame->f)) {
4267 return got_ferror(blame->f, GOT_ERR_IO);
4269 if (++lineno < s->first_displayed_line)
4272 view->maxx = MAX(view->maxx, linelen);
4274 if (view->focussed && nprinted == s->selected_line - 1)
4275 wstandout(view->window);
4277 if (blame->nlines > 0) {
4278 blame_line = &blame->lines[lineno - 1];
4279 if (blame_line->annotated && prev_id &&
4280 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4282 nprinted == s->selected_line - 1)) {
4283 waddstr(view->window, " ");
4284 } else if (blame_line->annotated) {
4286 err = got_object_id_str(&id_str, blame_line->id);
4291 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4293 wattr_on(view->window,
4294 COLOR_PAIR(tc->colorpair), NULL);
4295 wprintw(view->window, "%.8s", id_str);
4297 wattr_off(view->window,
4298 COLOR_PAIR(tc->colorpair), NULL);
4300 prev_id = blame_line->id;
4302 waddstr(view->window, "........");
4306 waddstr(view->window, "........");
4310 if (view->focussed && nprinted == s->selected_line - 1)
4311 wstandend(view->window);
4312 waddstr(view->window, " ");
4314 if (view->ncols <= 9) {
4316 wline = wcsdup(L"");
4317 if (wline == NULL) {
4318 err = got_error_from_errno("wcsdup");
4322 } else if (s->first_displayed_line + nprinted ==
4324 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4325 err = add_matched_line(&width, line, view->ncols - 9, 9,
4326 view->window, view->x, regmatch);
4333 err = format_line(&wline, &width, line,
4334 view->x + view->ncols - 9, 9, 1);
4335 if (!err && view->x < width - 1) {
4336 waddwstr(view->window, wline + view->x);
4345 if (width <= view->ncols - 1)
4346 waddch(view->window, '\n');
4347 if (++nprinted == 1)
4348 s->first_displayed_line = lineno;
4351 s->last_displayed_line = lineno;
4358 static const struct got_error *
4359 blame_cb(void *arg, int nlines, int lineno,
4360 struct got_commit_object *commit, struct got_object_id *id)
4362 const struct got_error *err = NULL;
4363 struct tog_blame_cb_args *a = arg;
4364 struct tog_blame_line *line;
4367 if (nlines != a->nlines ||
4368 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4369 return got_error(GOT_ERR_RANGE);
4371 errcode = pthread_mutex_lock(&tog_mutex);
4373 return got_error_set_errno(errcode, "pthread_mutex_lock");
4375 if (*a->quit) { /* user has quit the blame view */
4376 err = got_error(GOT_ERR_ITER_COMPLETED);
4381 goto done; /* no change in this commit */
4383 line = &a->lines[lineno - 1];
4384 if (line->annotated)
4387 line->id = got_object_id_dup(id);
4388 if (line->id == NULL) {
4389 err = got_error_from_errno("got_object_id_dup");
4392 line->annotated = 1;
4394 errcode = pthread_mutex_unlock(&tog_mutex);
4396 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4401 blame_thread(void *arg)
4403 const struct got_error *err, *close_err;
4404 struct tog_blame_thread_args *ta = arg;
4405 struct tog_blame_cb_args *a = ta->cb_args;
4408 err = block_signals_used_by_main_thread();
4412 err = got_blame(ta->path, a->commit_id, ta->repo,
4413 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4414 if (err && err->code == GOT_ERR_CANCELLED)
4417 errcode = pthread_mutex_lock(&tog_mutex);
4419 return (void *)got_error_set_errno(errcode,
4420 "pthread_mutex_lock");
4422 close_err = got_repo_close(ta->repo);
4428 errcode = pthread_mutex_unlock(&tog_mutex);
4429 if (errcode && err == NULL)
4430 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4435 static struct got_object_id *
4436 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4437 int first_displayed_line, int selected_line)
4439 struct tog_blame_line *line;
4444 line = &lines[first_displayed_line - 1 + selected_line - 1];
4445 if (!line->annotated)
4451 static const struct got_error *
4452 stop_blame(struct tog_blame *blame)
4454 const struct got_error *err = NULL;
4457 if (blame->thread) {
4459 errcode = pthread_mutex_unlock(&tog_mutex);
4461 return got_error_set_errno(errcode,
4462 "pthread_mutex_unlock");
4463 errcode = pthread_join(blame->thread, (void **)&err);
4465 return got_error_set_errno(errcode, "pthread_join");
4466 errcode = pthread_mutex_lock(&tog_mutex);
4468 return got_error_set_errno(errcode,
4469 "pthread_mutex_lock");
4470 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4472 blame->thread = NULL;
4474 if (blame->thread_args.repo) {
4475 const struct got_error *close_err;
4476 close_err = got_repo_close(blame->thread_args.repo);
4479 blame->thread_args.repo = NULL;
4482 if (fclose(blame->f) == EOF && err == NULL)
4483 err = got_error_from_errno("fclose");
4487 for (i = 0; i < blame->nlines; i++)
4488 free(blame->lines[i].id);
4490 blame->lines = NULL;
4492 free(blame->cb_args.commit_id);
4493 blame->cb_args.commit_id = NULL;
4494 if (blame->pack_fds) {
4495 const struct got_error *pack_err =
4496 got_repo_pack_fds_close(blame->pack_fds);
4499 blame->pack_fds = NULL;
4504 static const struct got_error *
4505 cancel_blame_view(void *arg)
4507 const struct got_error *err = NULL;
4511 errcode = pthread_mutex_lock(&tog_mutex);
4513 return got_error_set_errno(errcode,
4514 "pthread_mutex_unlock");
4517 err = got_error(GOT_ERR_CANCELLED);
4519 errcode = pthread_mutex_unlock(&tog_mutex);
4521 return got_error_set_errno(errcode,
4522 "pthread_mutex_lock");
4527 static const struct got_error *
4528 run_blame(struct tog_view *view)
4530 struct tog_blame_view_state *s = &view->state.blame;
4531 struct tog_blame *blame = &s->blame;
4532 const struct got_error *err = NULL;
4533 struct got_commit_object *commit = NULL;
4534 struct got_blob_object *blob = NULL;
4535 struct got_repository *thread_repo = NULL;
4536 struct got_object_id *obj_id = NULL;
4538 int *pack_fds = NULL;
4540 err = got_object_open_as_commit(&commit, s->repo,
4541 &s->blamed_commit->id);
4545 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4549 err = got_object_get_type(&obj_type, s->repo, obj_id);
4553 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4554 err = got_error(GOT_ERR_OBJ_TYPE);
4558 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4561 blame->f = got_opentemp();
4562 if (blame->f == NULL) {
4563 err = got_error_from_errno("got_opentemp");
4566 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4567 &blame->line_offsets, blame->f, blob);
4570 if (blame->nlines == 0) {
4571 s->blame_complete = 1;
4575 /* Don't include \n at EOF in the blame line count. */
4576 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4579 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4580 if (blame->lines == NULL) {
4581 err = got_error_from_errno("calloc");
4585 err = got_repo_pack_fds_open(&pack_fds);
4588 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4593 blame->pack_fds = pack_fds;
4594 blame->cb_args.view = view;
4595 blame->cb_args.lines = blame->lines;
4596 blame->cb_args.nlines = blame->nlines;
4597 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4598 if (blame->cb_args.commit_id == NULL) {
4599 err = got_error_from_errno("got_object_id_dup");
4602 blame->cb_args.quit = &s->done;
4604 blame->thread_args.path = s->path;
4605 blame->thread_args.repo = thread_repo;
4606 blame->thread_args.cb_args = &blame->cb_args;
4607 blame->thread_args.complete = &s->blame_complete;
4608 blame->thread_args.cancel_cb = cancel_blame_view;
4609 blame->thread_args.cancel_arg = &s->done;
4610 s->blame_complete = 0;
4612 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4613 s->first_displayed_line = 1;
4614 s->last_displayed_line = view->nlines;
4615 s->selected_line = 1;
4617 s->matched_line = 0;
4621 got_object_commit_close(commit);
4623 got_object_blob_close(blob);
4630 static const struct got_error *
4631 open_blame_view(struct tog_view *view, char *path,
4632 struct got_object_id *commit_id, struct got_repository *repo)
4634 const struct got_error *err = NULL;
4635 struct tog_blame_view_state *s = &view->state.blame;
4637 STAILQ_INIT(&s->blamed_commits);
4639 s->path = strdup(path);
4640 if (s->path == NULL)
4641 return got_error_from_errno("strdup");
4643 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4649 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4650 s->first_displayed_line = 1;
4651 s->last_displayed_line = view->nlines;
4652 s->selected_line = 1;
4653 s->blame_complete = 0;
4655 s->commit_id = commit_id;
4656 memset(&s->blame, 0, sizeof(s->blame));
4658 STAILQ_INIT(&s->colors);
4659 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4660 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4661 get_color_value("TOG_COLOR_COMMIT"));
4666 view->show = show_blame_view;
4667 view->input = input_blame_view;
4668 view->close = close_blame_view;
4669 view->search_start = search_start_blame_view;
4670 view->search_next = search_next_blame_view;
4672 return run_blame(view);
4675 static const struct got_error *
4676 close_blame_view(struct tog_view *view)
4678 const struct got_error *err = NULL;
4679 struct tog_blame_view_state *s = &view->state.blame;
4681 if (s->blame.thread)
4682 err = stop_blame(&s->blame);
4684 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4685 struct got_object_qid *blamed_commit;
4686 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4687 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4688 got_object_qid_free(blamed_commit);
4692 free_colors(&s->colors);
4696 static const struct got_error *
4697 search_start_blame_view(struct tog_view *view)
4699 struct tog_blame_view_state *s = &view->state.blame;
4701 s->matched_line = 0;
4705 static const struct got_error *
4706 search_next_blame_view(struct tog_view *view)
4708 struct tog_blame_view_state *s = &view->state.blame;
4709 const struct got_error *err = NULL;
4711 char *exstr = NULL, *line = NULL;
4712 size_t linesize = 0;
4715 if (!view->searching) {
4716 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4720 if (s->matched_line) {
4721 if (view->searching == TOG_SEARCH_FORWARD)
4722 lineno = s->matched_line + 1;
4724 lineno = s->matched_line - 1;
4726 lineno = s->first_displayed_line - 1 + s->selected_line;
4731 if (lineno <= 0 || lineno > s->blame.nlines) {
4732 if (s->matched_line == 0) {
4733 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4737 if (view->searching == TOG_SEARCH_FORWARD)
4740 lineno = s->blame.nlines;
4743 offset = s->blame.line_offsets[lineno - 1];
4744 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4746 return got_error_from_errno("fseeko");
4748 linelen = getline(&line, &linesize, s->blame.f);
4749 err = expand_tab(&exstr, line);
4752 if (linelen != -1 &&
4753 match_line(exstr, &view->regex, 1, &view->regmatch)) {
4754 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4755 s->matched_line = lineno;
4760 if (view->searching == TOG_SEARCH_FORWARD)
4768 if (s->matched_line) {
4769 s->first_displayed_line = s->matched_line;
4770 s->selected_line = 1;
4776 static const struct got_error *
4777 show_blame_view(struct tog_view *view)
4779 const struct got_error *err = NULL;
4780 struct tog_blame_view_state *s = &view->state.blame;
4783 if (s->blame.thread == NULL && !s->blame_complete) {
4784 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4785 &s->blame.thread_args);
4787 return got_error_set_errno(errcode, "pthread_create");
4789 halfdelay(1); /* fast refresh while annotating */
4792 if (s->blame_complete)
4793 halfdelay(10); /* disable fast refresh */
4795 err = draw_blame(view);
4801 static const struct got_error *
4802 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4804 const struct got_error *err = NULL, *thread_err = NULL;
4805 struct tog_view *diff_view;
4806 struct tog_blame_view_state *s = &view->state.blame;
4807 int begin_x = 0, nscroll = view->nlines - 2;
4814 view->x = MAX(view->maxx - view->ncols / 3, 0);
4818 if (view->x + view->ncols / 3 < view->maxx)
4819 view->x += 2; /* move two columns right */
4823 view->x -= MIN(view->x, 2); /* move two columns back */
4830 s->selected_line = 1;
4831 s->first_displayed_line = 1;
4835 if (s->blame.nlines < view->nlines - 2) {
4836 s->selected_line = s->blame.nlines;
4837 s->first_displayed_line = 1;
4839 s->selected_line = view->nlines - 2;
4840 s->first_displayed_line = s->blame.nlines -
4847 if (s->selected_line > 1)
4849 else if (s->selected_line == 1 &&
4850 s->first_displayed_line > 1)
4851 s->first_displayed_line--;
4859 if (s->first_displayed_line == 1) {
4860 s->selected_line = MAX(1, s->selected_line - nscroll);
4863 if (s->first_displayed_line > nscroll)
4864 s->first_displayed_line -= nscroll;
4866 s->first_displayed_line = 1;
4871 if (s->selected_line < view->nlines - 2 &&
4872 s->first_displayed_line +
4873 s->selected_line <= s->blame.nlines)
4875 else if (s->last_displayed_line <
4877 s->first_displayed_line++;
4881 struct got_object_id *id = NULL;
4882 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4883 s->first_displayed_line, s->selected_line);
4887 struct got_commit_object *commit, *pcommit;
4888 struct got_object_qid *pid;
4889 struct got_object_id *blob_id = NULL;
4891 err = got_object_open_as_commit(&commit,
4896 got_object_commit_get_parent_ids(commit));
4898 got_object_commit_close(commit);
4901 /* Check if path history ends here. */
4902 err = got_object_open_as_commit(&pcommit,
4906 err = got_object_id_by_path(&blob_id, s->repo,
4908 got_object_commit_close(pcommit);
4910 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4912 got_object_commit_close(commit);
4915 err = got_object_get_type(&obj_type, s->repo,
4918 /* Can't blame non-blob type objects. */
4919 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4920 got_object_commit_close(commit);
4923 err = got_object_qid_alloc(&s->blamed_commit,
4925 got_object_commit_close(commit);
4927 if (got_object_id_cmp(id,
4928 &s->blamed_commit->id) == 0)
4930 err = got_object_qid_alloc(&s->blamed_commit,
4936 thread_err = stop_blame(&s->blame);
4940 STAILQ_INSERT_HEAD(&s->blamed_commits,
4941 s->blamed_commit, entry);
4942 err = run_blame(view);
4948 struct got_object_qid *first;
4949 first = STAILQ_FIRST(&s->blamed_commits);
4950 if (!got_object_id_cmp(&first->id, s->commit_id))
4953 thread_err = stop_blame(&s->blame);
4957 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4958 got_object_qid_free(s->blamed_commit);
4960 STAILQ_FIRST(&s->blamed_commits);
4961 err = run_blame(view);
4968 struct got_object_id *id = NULL;
4969 struct got_object_qid *pid;
4970 struct got_commit_object *commit = NULL;
4971 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4972 s->first_displayed_line, s->selected_line);
4975 err = got_object_open_as_commit(&commit, s->repo, id);
4979 got_object_commit_get_parent_ids(commit));
4980 if (view_is_parent_view(view))
4981 begin_x = view_split_begin_x(view->begin_x);
4982 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4983 if (diff_view == NULL) {
4984 got_object_commit_close(commit);
4985 err = got_error_from_errno("view_open");
4988 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4989 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4990 got_object_commit_close(commit);
4992 view_close(diff_view);
4996 diff_view->focussed = 1;
4997 if (view_is_parent_view(view)) {
4998 err = view_close_child(view);
5001 view_set_child(view, diff_view);
5002 view->focus_child = 1;
5004 *new_view = diff_view;
5016 if (s->last_displayed_line >= s->blame.nlines &&
5017 s->selected_line >= MIN(s->blame.nlines,
5018 view->nlines - 2)) {
5021 if (s->last_displayed_line >= s->blame.nlines &&
5022 s->selected_line < view->nlines - 2) {
5024 MIN(nscroll, s->last_displayed_line -
5025 s->first_displayed_line - s->selected_line + 1);
5027 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5028 s->first_displayed_line += nscroll;
5030 s->first_displayed_line =
5031 s->blame.nlines - (view->nlines - 3);
5034 if (s->selected_line > view->nlines - 2) {
5035 s->selected_line = MIN(s->blame.nlines,
5042 return thread_err ? thread_err : err;
5045 static const struct got_error *
5046 cmd_blame(int argc, char *argv[])
5048 const struct got_error *error;
5049 struct got_repository *repo = NULL;
5050 struct got_worktree *worktree = NULL;
5051 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5052 char *link_target = NULL;
5053 struct got_object_id *commit_id = NULL;
5054 struct got_commit_object *commit = NULL;
5055 char *commit_id_str = NULL;
5057 struct tog_view *view;
5058 int *pack_fds = NULL;
5060 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5063 commit_id_str = optarg;
5066 repo_path = realpath(optarg, NULL);
5067 if (repo_path == NULL)
5068 return got_error_from_errno2("realpath",
5083 error = got_repo_pack_fds_open(&pack_fds);
5087 if (repo_path == NULL) {
5088 cwd = getcwd(NULL, 0);
5090 return got_error_from_errno("getcwd");
5091 error = got_worktree_open(&worktree, cwd);
5092 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5096 strdup(got_worktree_get_repo_path(worktree));
5098 repo_path = strdup(cwd);
5099 if (repo_path == NULL) {
5100 error = got_error_from_errno("strdup");
5105 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5109 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5116 error = apply_unveil(got_repo_get_path(repo), NULL);
5120 error = tog_load_refs(repo, 0);
5124 if (commit_id_str == NULL) {
5125 struct got_reference *head_ref;
5126 error = got_ref_open(&head_ref, repo, worktree ?
5127 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5130 error = got_ref_resolve(&commit_id, repo, head_ref);
5131 got_ref_close(head_ref);
5133 error = got_repo_match_object_id(&commit_id, NULL,
5134 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5139 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5141 error = got_error_from_errno("view_open");
5145 error = got_object_open_as_commit(&commit, repo, commit_id);
5149 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5154 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5159 /* Release work tree lock. */
5160 got_worktree_close(worktree);
5163 error = view_loop(view);
5171 got_object_commit_close(commit);
5173 got_worktree_close(worktree);
5175 const struct got_error *close_err = got_repo_close(repo);
5180 const struct got_error *pack_err =
5181 got_repo_pack_fds_close(pack_fds);
5189 static const struct got_error *
5190 draw_tree_entries(struct tog_view *view, const char *parent_path)
5192 struct tog_tree_view_state *s = &view->state.tree;
5193 const struct got_error *err = NULL;
5194 struct got_tree_entry *te;
5196 struct tog_color *tc;
5197 int width, n, i, nentries;
5198 int limit = view->nlines;
5202 werase(view->window);
5207 err = format_line(&wline, &width, s->tree_label, view->ncols, 0, 0);
5210 if (view_needs_focus_indication(view))
5211 wstandout(view->window);
5212 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5214 wattr_on(view->window,
5215 COLOR_PAIR(tc->colorpair), NULL);
5216 waddwstr(view->window, wline);
5218 wattr_off(view->window,
5219 COLOR_PAIR(tc->colorpair), NULL);
5220 if (view_needs_focus_indication(view))
5221 wstandend(view->window);
5224 if (width < view->ncols - 1)
5225 waddch(view->window, '\n');
5228 err = format_line(&wline, &width, parent_path, view->ncols, 0, 0);
5231 waddwstr(view->window, wline);
5234 if (width < view->ncols - 1)
5235 waddch(view->window, '\n');
5238 waddch(view->window, '\n');
5242 if (s->first_displayed_entry == NULL) {
5243 te = got_object_tree_get_first_entry(s->tree);
5244 if (s->selected == 0) {
5246 wstandout(view->window);
5247 s->selected_entry = NULL;
5249 waddstr(view->window, " ..\n"); /* parent directory */
5250 if (s->selected == 0 && view->focussed)
5251 wstandend(view->window);
5258 te = s->first_displayed_entry;
5261 nentries = got_object_tree_get_nentries(s->tree);
5262 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5263 char *line = NULL, *id_str = NULL, *link_target = NULL;
5264 const char *modestr = "";
5267 te = got_object_tree_get_entry(s->tree, i);
5268 mode = got_tree_entry_get_mode(te);
5271 err = got_object_id_str(&id_str,
5272 got_tree_entry_get_id(te));
5274 return got_error_from_errno(
5275 "got_object_id_str");
5277 if (got_object_tree_entry_is_submodule(te))
5279 else if (S_ISLNK(mode)) {
5282 err = got_tree_entry_get_symlink_target(&link_target,
5288 for (i = 0; i < strlen(link_target); i++) {
5289 if (!isprint((unsigned char)link_target[i]))
5290 link_target[i] = '?';
5294 else if (S_ISDIR(mode))
5296 else if (mode & S_IXUSR)
5298 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5299 got_tree_entry_get_name(te), modestr,
5300 link_target ? " -> ": "",
5301 link_target ? link_target : "") == -1) {
5304 return got_error_from_errno("asprintf");
5308 err = format_line(&wline, &width, line, view->ncols, 0, 0);
5313 if (n == s->selected) {
5315 wstandout(view->window);
5316 s->selected_entry = te;
5318 tc = match_color(&s->colors, line);
5320 wattr_on(view->window,
5321 COLOR_PAIR(tc->colorpair), NULL);
5322 waddwstr(view->window, wline);
5324 wattr_off(view->window,
5325 COLOR_PAIR(tc->colorpair), NULL);
5326 if (width < view->ncols - 1)
5327 waddch(view->window, '\n');
5328 if (n == s->selected && view->focussed)
5329 wstandend(view->window);
5335 s->last_displayed_entry = te;
5344 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5346 struct got_tree_entry *te;
5347 int isroot = s->tree == s->root;
5350 if (s->first_displayed_entry == NULL)
5353 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5354 while (i++ < maxscroll) {
5357 s->first_displayed_entry = NULL;
5360 s->first_displayed_entry = te;
5361 te = got_tree_entry_get_prev(s->tree, te);
5366 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5368 struct got_tree_entry *next, *last;
5371 if (s->first_displayed_entry)
5372 next = got_tree_entry_get_next(s->tree,
5373 s->first_displayed_entry);
5375 next = got_object_tree_get_first_entry(s->tree);
5377 last = s->last_displayed_entry;
5378 while (next && last && n++ < maxscroll) {
5379 last = got_tree_entry_get_next(s->tree, last);
5381 s->first_displayed_entry = next;
5382 next = got_tree_entry_get_next(s->tree, next);
5387 static const struct got_error *
5388 tree_entry_path(char **path, struct tog_parent_trees *parents,
5389 struct got_tree_entry *te)
5391 const struct got_error *err = NULL;
5392 struct tog_parent_tree *pt;
5393 size_t len = 2; /* for leading slash and NUL */
5395 TAILQ_FOREACH(pt, parents, entry)
5396 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5399 len += strlen(got_tree_entry_get_name(te));
5401 *path = calloc(1, len);
5403 return got_error_from_errno("calloc");
5406 pt = TAILQ_LAST(parents, tog_parent_trees);
5408 const char *name = got_tree_entry_get_name(pt->selected_entry);
5409 if (strlcat(*path, name, len) >= len) {
5410 err = got_error(GOT_ERR_NO_SPACE);
5413 if (strlcat(*path, "/", len) >= len) {
5414 err = got_error(GOT_ERR_NO_SPACE);
5417 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5420 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5421 err = got_error(GOT_ERR_NO_SPACE);
5433 static const struct got_error *
5434 blame_tree_entry(struct tog_view **new_view, int begin_x,
5435 struct got_tree_entry *te, struct tog_parent_trees *parents,
5436 struct got_object_id *commit_id, struct got_repository *repo)
5438 const struct got_error *err = NULL;
5440 struct tog_view *blame_view;
5444 err = tree_entry_path(&path, parents, te);
5448 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5449 if (blame_view == NULL) {
5450 err = got_error_from_errno("view_open");
5454 err = open_blame_view(blame_view, path, commit_id, repo);
5456 if (err->code == GOT_ERR_CANCELLED)
5458 view_close(blame_view);
5460 *new_view = blame_view;
5466 static const struct got_error *
5467 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5468 struct tog_tree_view_state *s)
5470 struct tog_view *log_view;
5471 const struct got_error *err = NULL;
5476 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5477 if (log_view == NULL)
5478 return got_error_from_errno("view_open");
5480 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5484 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5487 view_close(log_view);
5489 *new_view = log_view;
5494 static const struct got_error *
5495 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5496 const char *head_ref_name, struct got_repository *repo)
5498 const struct got_error *err = NULL;
5499 char *commit_id_str = NULL;
5500 struct tog_tree_view_state *s = &view->state.tree;
5501 struct got_commit_object *commit = NULL;
5503 TAILQ_INIT(&s->parents);
5504 STAILQ_INIT(&s->colors);
5506 s->commit_id = got_object_id_dup(commit_id);
5507 if (s->commit_id == NULL)
5508 return got_error_from_errno("got_object_id_dup");
5510 err = got_object_open_as_commit(&commit, repo, commit_id);
5515 * The root is opened here and will be closed when the view is closed.
5516 * Any visited subtrees and their path-wise parents are opened and
5519 err = got_object_open_as_tree(&s->root, repo,
5520 got_object_commit_get_tree_id(commit));
5525 err = got_object_id_str(&commit_id_str, commit_id);
5529 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5530 err = got_error_from_errno("asprintf");
5534 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5535 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5536 if (head_ref_name) {
5537 s->head_ref_name = strdup(head_ref_name);
5538 if (s->head_ref_name == NULL) {
5539 err = got_error_from_errno("strdup");
5545 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5546 err = add_color(&s->colors, "\\$$",
5547 TOG_COLOR_TREE_SUBMODULE,
5548 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5551 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5552 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5555 err = add_color(&s->colors, "/$",
5556 TOG_COLOR_TREE_DIRECTORY,
5557 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5561 err = add_color(&s->colors, "\\*$",
5562 TOG_COLOR_TREE_EXECUTABLE,
5563 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5567 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5568 get_color_value("TOG_COLOR_COMMIT"));
5573 view->show = show_tree_view;
5574 view->input = input_tree_view;
5575 view->close = close_tree_view;
5576 view->search_start = search_start_tree_view;
5577 view->search_next = search_next_tree_view;
5579 free(commit_id_str);
5581 got_object_commit_close(commit);
5583 close_tree_view(view);
5587 static const struct got_error *
5588 close_tree_view(struct tog_view *view)
5590 struct tog_tree_view_state *s = &view->state.tree;
5592 free_colors(&s->colors);
5593 free(s->tree_label);
5594 s->tree_label = NULL;
5596 s->commit_id = NULL;
5597 free(s->head_ref_name);
5598 s->head_ref_name = NULL;
5599 while (!TAILQ_EMPTY(&s->parents)) {
5600 struct tog_parent_tree *parent;
5601 parent = TAILQ_FIRST(&s->parents);
5602 TAILQ_REMOVE(&s->parents, parent, entry);
5603 if (parent->tree != s->root)
5604 got_object_tree_close(parent->tree);
5608 if (s->tree != NULL && s->tree != s->root)
5609 got_object_tree_close(s->tree);
5611 got_object_tree_close(s->root);
5615 static const struct got_error *
5616 search_start_tree_view(struct tog_view *view)
5618 struct tog_tree_view_state *s = &view->state.tree;
5620 s->matched_entry = NULL;
5625 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5627 regmatch_t regmatch;
5629 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5633 static const struct got_error *
5634 search_next_tree_view(struct tog_view *view)
5636 struct tog_tree_view_state *s = &view->state.tree;
5637 struct got_tree_entry *te = NULL;
5639 if (!view->searching) {
5640 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5644 if (s->matched_entry) {
5645 if (view->searching == TOG_SEARCH_FORWARD) {
5646 if (s->selected_entry)
5647 te = got_tree_entry_get_next(s->tree,
5650 te = got_object_tree_get_first_entry(s->tree);
5652 if (s->selected_entry == NULL)
5653 te = got_object_tree_get_last_entry(s->tree);
5655 te = got_tree_entry_get_prev(s->tree,
5659 if (s->selected_entry)
5660 te = s->selected_entry;
5661 else if (view->searching == TOG_SEARCH_FORWARD)
5662 te = got_object_tree_get_first_entry(s->tree);
5664 te = got_object_tree_get_last_entry(s->tree);
5669 if (s->matched_entry == NULL) {
5670 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5673 if (view->searching == TOG_SEARCH_FORWARD)
5674 te = got_object_tree_get_first_entry(s->tree);
5676 te = got_object_tree_get_last_entry(s->tree);
5679 if (match_tree_entry(te, &view->regex)) {
5680 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5681 s->matched_entry = te;
5685 if (view->searching == TOG_SEARCH_FORWARD)
5686 te = got_tree_entry_get_next(s->tree, te);
5688 te = got_tree_entry_get_prev(s->tree, te);
5691 if (s->matched_entry) {
5692 s->first_displayed_entry = s->matched_entry;
5699 static const struct got_error *
5700 show_tree_view(struct tog_view *view)
5702 const struct got_error *err = NULL;
5703 struct tog_tree_view_state *s = &view->state.tree;
5706 err = tree_entry_path(&parent_path, &s->parents, NULL);
5710 err = draw_tree_entries(view, parent_path);
5717 static const struct got_error *
5718 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5720 const struct got_error *err = NULL;
5721 struct tog_tree_view_state *s = &view->state.tree;
5722 struct tog_view *log_view, *ref_view;
5723 struct got_tree_entry *te;
5724 int begin_x = 0, n, nscroll = view->nlines - 3;
5728 s->show_ids = !s->show_ids;
5731 if (!s->selected_entry)
5733 if (view_is_parent_view(view))
5734 begin_x = view_split_begin_x(view->begin_x);
5735 err = log_selected_tree_entry(&log_view, begin_x, s);
5737 log_view->focussed = 1;
5738 if (view_is_parent_view(view)) {
5739 err = view_close_child(view);
5742 view_set_child(view, log_view);
5743 view->focus_child = 1;
5745 *new_view = log_view;
5748 if (view_is_parent_view(view))
5749 begin_x = view_split_begin_x(view->begin_x);
5750 ref_view = view_open(view->nlines, view->ncols,
5751 view->begin_y, begin_x, TOG_VIEW_REF);
5752 if (ref_view == NULL)
5753 return got_error_from_errno("view_open");
5754 err = open_ref_view(ref_view, s->repo);
5756 view_close(ref_view);
5760 ref_view->focussed = 1;
5761 if (view_is_parent_view(view)) {
5762 err = view_close_child(view);
5765 view_set_child(view, ref_view);
5766 view->focus_child = 1;
5768 *new_view = ref_view;
5773 if (s->tree == s->root)
5774 s->first_displayed_entry =
5775 got_object_tree_get_first_entry(s->tree);
5777 s->first_displayed_entry = NULL;
5782 te = got_object_tree_get_last_entry(s->tree);
5783 for (n = 0; n < view->nlines - 3; n++) {
5785 if(s->tree != s->root) {
5786 s->first_displayed_entry = NULL;
5791 s->first_displayed_entry = te;
5792 te = got_tree_entry_get_prev(s->tree, te);
5795 s->selected = n - 1;
5800 if (s->selected > 0) {
5804 tree_scroll_up(s, 1);
5812 if (s->tree == s->root) {
5813 if (got_object_tree_get_first_entry(s->tree) ==
5814 s->first_displayed_entry)
5815 s->selected -= MIN(s->selected, nscroll);
5817 if (s->first_displayed_entry == NULL)
5818 s->selected -= MIN(s->selected, nscroll);
5820 tree_scroll_up(s, MAX(0, nscroll));
5825 if (s->selected < s->ndisplayed - 1) {
5829 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5831 /* can't scroll any further */
5833 tree_scroll_down(s, 1);
5841 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5843 /* can't scroll any further; move cursor down */
5844 if (s->selected < s->ndisplayed - 1)
5845 s->selected += MIN(nscroll,
5846 s->ndisplayed - s->selected - 1);
5849 tree_scroll_down(s, nscroll);
5854 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5855 struct tog_parent_tree *parent;
5856 /* user selected '..' */
5857 if (s->tree == s->root)
5859 parent = TAILQ_FIRST(&s->parents);
5860 TAILQ_REMOVE(&s->parents, parent,
5862 got_object_tree_close(s->tree);
5863 s->tree = parent->tree;
5864 s->first_displayed_entry =
5865 parent->first_displayed_entry;
5867 parent->selected_entry;
5868 s->selected = parent->selected;
5870 } else if (S_ISDIR(got_tree_entry_get_mode(
5871 s->selected_entry))) {
5872 struct got_tree_object *subtree;
5873 err = got_object_open_as_tree(&subtree, s->repo,
5874 got_tree_entry_get_id(s->selected_entry));
5877 err = tree_view_visit_subtree(s, subtree);
5879 got_object_tree_close(subtree);
5882 } else if (S_ISREG(got_tree_entry_get_mode(
5883 s->selected_entry))) {
5884 struct tog_view *blame_view;
5885 int begin_x = view_is_parent_view(view) ?
5886 view_split_begin_x(view->begin_x) : 0;
5888 err = blame_tree_entry(&blame_view, begin_x,
5889 s->selected_entry, &s->parents,
5890 s->commit_id, s->repo);
5894 blame_view->focussed = 1;
5895 if (view_is_parent_view(view)) {
5896 err = view_close_child(view);
5899 view_set_child(view, blame_view);
5900 view->focus_child = 1;
5902 *new_view = blame_view;
5906 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5907 s->selected = view->nlines - 4;
5920 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5925 static const struct got_error *
5926 cmd_tree(int argc, char *argv[])
5928 const struct got_error *error;
5929 struct got_repository *repo = NULL;
5930 struct got_worktree *worktree = NULL;
5931 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5932 struct got_object_id *commit_id = NULL;
5933 struct got_commit_object *commit = NULL;
5934 const char *commit_id_arg = NULL;
5936 struct got_reference *ref = NULL;
5937 const char *head_ref_name = NULL;
5939 struct tog_view *view;
5940 int *pack_fds = NULL;
5942 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5945 commit_id_arg = optarg;
5948 repo_path = realpath(optarg, NULL);
5949 if (repo_path == NULL)
5950 return got_error_from_errno2("realpath",
5965 error = got_repo_pack_fds_open(&pack_fds);
5969 if (repo_path == NULL) {
5970 cwd = getcwd(NULL, 0);
5972 return got_error_from_errno("getcwd");
5973 error = got_worktree_open(&worktree, cwd);
5974 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5978 strdup(got_worktree_get_repo_path(worktree));
5980 repo_path = strdup(cwd);
5981 if (repo_path == NULL) {
5982 error = got_error_from_errno("strdup");
5987 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5991 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5998 error = apply_unveil(got_repo_get_path(repo), NULL);
6002 error = tog_load_refs(repo, 0);
6006 if (commit_id_arg == NULL) {
6007 error = got_repo_match_object_id(&commit_id, &label,
6008 worktree ? got_worktree_get_head_ref_name(worktree) :
6009 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6012 head_ref_name = label;
6014 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6016 head_ref_name = got_ref_get_name(ref);
6017 else if (error->code != GOT_ERR_NOT_REF)
6019 error = got_repo_match_object_id(&commit_id, NULL,
6020 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6025 error = got_object_open_as_commit(&commit, repo, commit_id);
6029 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6031 error = got_error_from_errno("view_open");
6034 error = open_tree_view(view, commit_id, head_ref_name, repo);
6037 if (!got_path_is_root_dir(in_repo_path)) {
6038 error = tree_view_walk_path(&view->state.tree, commit,
6045 /* Release work tree lock. */
6046 got_worktree_close(worktree);
6049 error = view_loop(view);
6058 const struct got_error *close_err = got_repo_close(repo);
6063 const struct got_error *pack_err =
6064 got_repo_pack_fds_close(pack_fds);
6072 static const struct got_error *
6073 ref_view_load_refs(struct tog_ref_view_state *s)
6075 struct got_reflist_entry *sre;
6076 struct tog_reflist_entry *re;
6079 TAILQ_FOREACH(sre, &tog_refs, entry) {
6080 if (strncmp(got_ref_get_name(sre->ref),
6081 "refs/got/", 9) == 0 &&
6082 strncmp(got_ref_get_name(sre->ref),
6083 "refs/got/backup/", 16) != 0)
6086 re = malloc(sizeof(*re));
6088 return got_error_from_errno("malloc");
6090 re->ref = got_ref_dup(sre->ref);
6091 if (re->ref == NULL)
6092 return got_error_from_errno("got_ref_dup");
6093 re->idx = s->nrefs++;
6094 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6097 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6102 ref_view_free_refs(struct tog_ref_view_state *s)
6104 struct tog_reflist_entry *re;
6106 while (!TAILQ_EMPTY(&s->refs)) {
6107 re = TAILQ_FIRST(&s->refs);
6108 TAILQ_REMOVE(&s->refs, re, entry);
6109 got_ref_close(re->ref);
6114 static const struct got_error *
6115 open_ref_view(struct tog_view *view, struct got_repository *repo)
6117 const struct got_error *err = NULL;
6118 struct tog_ref_view_state *s = &view->state.ref;
6120 s->selected_entry = 0;
6123 TAILQ_INIT(&s->refs);
6124 STAILQ_INIT(&s->colors);
6126 err = ref_view_load_refs(s);
6130 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6131 err = add_color(&s->colors, "^refs/heads/",
6132 TOG_COLOR_REFS_HEADS,
6133 get_color_value("TOG_COLOR_REFS_HEADS"));
6137 err = add_color(&s->colors, "^refs/tags/",
6138 TOG_COLOR_REFS_TAGS,
6139 get_color_value("TOG_COLOR_REFS_TAGS"));
6143 err = add_color(&s->colors, "^refs/remotes/",
6144 TOG_COLOR_REFS_REMOTES,
6145 get_color_value("TOG_COLOR_REFS_REMOTES"));
6149 err = add_color(&s->colors, "^refs/got/backup/",
6150 TOG_COLOR_REFS_BACKUP,
6151 get_color_value("TOG_COLOR_REFS_BACKUP"));
6156 view->show = show_ref_view;
6157 view->input = input_ref_view;
6158 view->close = close_ref_view;
6159 view->search_start = search_start_ref_view;
6160 view->search_next = search_next_ref_view;
6163 free_colors(&s->colors);
6167 static const struct got_error *
6168 close_ref_view(struct tog_view *view)
6170 struct tog_ref_view_state *s = &view->state.ref;
6172 ref_view_free_refs(s);
6173 free_colors(&s->colors);
6178 static const struct got_error *
6179 resolve_reflist_entry(struct got_object_id **commit_id,
6180 struct tog_reflist_entry *re, struct got_repository *repo)
6182 const struct got_error *err = NULL;
6183 struct got_object_id *obj_id;
6184 struct got_tag_object *tag = NULL;
6189 err = got_ref_resolve(&obj_id, repo, re->ref);
6193 err = got_object_get_type(&obj_type, repo, obj_id);
6198 case GOT_OBJ_TYPE_COMMIT:
6199 *commit_id = obj_id;
6201 case GOT_OBJ_TYPE_TAG:
6202 err = got_object_open_as_tag(&tag, repo, obj_id);
6206 err = got_object_get_type(&obj_type, repo,
6207 got_object_tag_get_object_id(tag));
6210 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6211 err = got_error(GOT_ERR_OBJ_TYPE);
6214 *commit_id = got_object_id_dup(
6215 got_object_tag_get_object_id(tag));
6216 if (*commit_id == NULL) {
6217 err = got_error_from_errno("got_object_id_dup");
6222 err = got_error(GOT_ERR_OBJ_TYPE);
6228 got_object_tag_close(tag);
6236 static const struct got_error *
6237 log_ref_entry(struct tog_view **new_view, int begin_x,
6238 struct tog_reflist_entry *re, struct got_repository *repo)
6240 struct tog_view *log_view;
6241 const struct got_error *err = NULL;
6242 struct got_object_id *commit_id = NULL;
6246 err = resolve_reflist_entry(&commit_id, re, repo);
6248 if (err->code != GOT_ERR_OBJ_TYPE)
6254 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6255 if (log_view == NULL) {
6256 err = got_error_from_errno("view_open");
6260 err = open_log_view(log_view, commit_id, repo,
6261 got_ref_get_name(re->ref), "", 0);
6264 view_close(log_view);
6266 *new_view = log_view;
6272 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6274 struct tog_reflist_entry *re;
6277 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6280 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6281 while (i++ < maxscroll) {
6284 s->first_displayed_entry = re;
6285 re = TAILQ_PREV(re, tog_reflist_head, entry);
6290 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6292 struct tog_reflist_entry *next, *last;
6295 if (s->first_displayed_entry)
6296 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6298 next = TAILQ_FIRST(&s->refs);
6300 last = s->last_displayed_entry;
6301 while (next && last && n++ < maxscroll) {
6302 last = TAILQ_NEXT(last, entry);
6304 s->first_displayed_entry = next;
6305 next = TAILQ_NEXT(next, entry);
6310 static const struct got_error *
6311 search_start_ref_view(struct tog_view *view)
6313 struct tog_ref_view_state *s = &view->state.ref;
6315 s->matched_entry = NULL;
6320 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6322 regmatch_t regmatch;
6324 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6328 static const struct got_error *
6329 search_next_ref_view(struct tog_view *view)
6331 struct tog_ref_view_state *s = &view->state.ref;
6332 struct tog_reflist_entry *re = NULL;
6334 if (!view->searching) {
6335 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6339 if (s->matched_entry) {
6340 if (view->searching == TOG_SEARCH_FORWARD) {
6341 if (s->selected_entry)
6342 re = TAILQ_NEXT(s->selected_entry, entry);
6344 re = TAILQ_PREV(s->selected_entry,
6345 tog_reflist_head, entry);
6347 if (s->selected_entry == NULL)
6348 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6350 re = TAILQ_PREV(s->selected_entry,
6351 tog_reflist_head, entry);
6354 if (s->selected_entry)
6355 re = s->selected_entry;
6356 else if (view->searching == TOG_SEARCH_FORWARD)
6357 re = TAILQ_FIRST(&s->refs);
6359 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6364 if (s->matched_entry == NULL) {
6365 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6368 if (view->searching == TOG_SEARCH_FORWARD)
6369 re = TAILQ_FIRST(&s->refs);
6371 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6374 if (match_reflist_entry(re, &view->regex)) {
6375 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6376 s->matched_entry = re;
6380 if (view->searching == TOG_SEARCH_FORWARD)
6381 re = TAILQ_NEXT(re, entry);
6383 re = TAILQ_PREV(re, tog_reflist_head, entry);
6386 if (s->matched_entry) {
6387 s->first_displayed_entry = s->matched_entry;
6394 static const struct got_error *
6395 show_ref_view(struct tog_view *view)
6397 const struct got_error *err = NULL;
6398 struct tog_ref_view_state *s = &view->state.ref;
6399 struct tog_reflist_entry *re;
6402 struct tog_color *tc;
6404 int limit = view->nlines;
6406 werase(view->window);
6413 re = s->first_displayed_entry;
6415 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6417 return got_error_from_errno("asprintf");
6419 err = format_line(&wline, &width, line, view->ncols, 0, 0);
6424 if (view_needs_focus_indication(view))
6425 wstandout(view->window);
6426 waddwstr(view->window, wline);
6427 if (view_needs_focus_indication(view))
6428 wstandend(view->window);
6433 if (width < view->ncols - 1)
6434 waddch(view->window, '\n');
6439 while (re && limit > 0) {
6441 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6444 struct got_commit_object *ci;
6445 struct got_tag_object *tag;
6446 struct got_object_id *id;
6450 err = got_ref_resolve(&id, s->repo, re->ref);
6453 err = got_object_open_as_tag(&tag, s->repo, id);
6455 if (err->code != GOT_ERR_OBJ_TYPE) {
6459 err = got_object_open_as_commit(&ci, s->repo,
6465 t = got_object_commit_get_committer_time(ci);
6466 got_object_commit_close(ci);
6468 t = got_object_tag_get_tagger_time(tag);
6469 got_object_tag_close(tag);
6472 if (gmtime_r(&t, &tm) == NULL)
6473 return got_error_from_errno("gmtime_r");
6474 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6475 return got_error(GOT_ERR_NO_SPACE);
6477 if (got_ref_is_symbolic(re->ref)) {
6478 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6479 ymd : "", got_ref_get_name(re->ref),
6480 got_ref_get_symref_target(re->ref)) == -1)
6481 return got_error_from_errno("asprintf");
6482 } else if (s->show_ids) {
6483 struct got_object_id *id;
6485 err = got_ref_resolve(&id, s->repo, re->ref);
6488 err = got_object_id_str(&id_str, id);
6493 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6494 got_ref_get_name(re->ref), id_str) == -1) {
6495 err = got_error_from_errno("asprintf");
6502 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6503 got_ref_get_name(re->ref)) == -1)
6504 return got_error_from_errno("asprintf");
6506 err = format_line(&wline, &width, line, view->ncols, 0, 0);
6511 if (n == s->selected) {
6513 wstandout(view->window);
6514 s->selected_entry = re;
6516 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6518 wattr_on(view->window,
6519 COLOR_PAIR(tc->colorpair), NULL);
6520 waddwstr(view->window, wline);
6522 wattr_off(view->window,
6523 COLOR_PAIR(tc->colorpair), NULL);
6524 if (width < view->ncols - 1)
6525 waddch(view->window, '\n');
6526 if (n == s->selected && view->focussed)
6527 wstandend(view->window);
6533 s->last_displayed_entry = re;
6536 re = TAILQ_NEXT(re, entry);
6543 static const struct got_error *
6544 browse_ref_tree(struct tog_view **new_view, int begin_x,
6545 struct tog_reflist_entry *re, struct got_repository *repo)
6547 const struct got_error *err = NULL;
6548 struct got_object_id *commit_id = NULL;
6549 struct tog_view *tree_view;
6553 err = resolve_reflist_entry(&commit_id, re, repo);
6555 if (err->code != GOT_ERR_OBJ_TYPE)
6562 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6563 if (tree_view == NULL) {
6564 err = got_error_from_errno("view_open");
6568 err = open_tree_view(tree_view, commit_id,
6569 got_ref_get_name(re->ref), repo);
6573 *new_view = tree_view;
6578 static const struct got_error *
6579 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6581 const struct got_error *err = NULL;
6582 struct tog_ref_view_state *s = &view->state.ref;
6583 struct tog_view *log_view, *tree_view;
6584 struct tog_reflist_entry *re;
6585 int begin_x = 0, n, nscroll = view->nlines - 1;
6589 s->show_ids = !s->show_ids;
6592 s->show_date = !s->show_date;
6595 s->sort_by_date = !s->sort_by_date;
6596 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6597 got_ref_cmp_by_commit_timestamp_descending :
6598 tog_ref_cmp_by_name, s->repo);
6601 got_reflist_object_id_map_free(tog_refs_idmap);
6602 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6603 &tog_refs, s->repo);
6606 ref_view_free_refs(s);
6607 err = ref_view_load_refs(s);
6611 if (!s->selected_entry)
6613 if (view_is_parent_view(view))
6614 begin_x = view_split_begin_x(view->begin_x);
6615 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6618 log_view->focussed = 1;
6619 if (view_is_parent_view(view)) {
6620 err = view_close_child(view);
6623 view_set_child(view, log_view);
6624 view->focus_child = 1;
6626 *new_view = log_view;
6629 if (!s->selected_entry)
6631 if (view_is_parent_view(view))
6632 begin_x = view_split_begin_x(view->begin_x);
6633 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6635 if (err || tree_view == NULL)
6638 tree_view->focussed = 1;
6639 if (view_is_parent_view(view)) {
6640 err = view_close_child(view);
6643 view_set_child(view, tree_view);
6644 view->focus_child = 1;
6646 *new_view = tree_view;
6651 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6656 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6657 for (n = 0; n < view->nlines - 1; n++) {
6660 s->first_displayed_entry = re;
6661 re = TAILQ_PREV(re, tog_reflist_head, entry);
6664 s->selected = n - 1;
6669 if (s->selected > 0) {
6673 ref_scroll_up(s, 1);
6681 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6682 s->selected -= MIN(nscroll, s->selected);
6683 ref_scroll_up(s, MAX(0, nscroll));
6688 if (s->selected < s->ndisplayed - 1) {
6692 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6693 /* can't scroll any further */
6695 ref_scroll_down(s, 1);
6703 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6704 /* can't scroll any further; move cursor down */
6705 if (s->selected < s->ndisplayed - 1)
6706 s->selected += MIN(nscroll,
6707 s->ndisplayed - s->selected - 1);
6710 ref_scroll_down(s, nscroll);
6714 err = tog_load_refs(s->repo, s->sort_by_date);
6717 ref_view_free_refs(s);
6718 err = ref_view_load_refs(s);
6721 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6722 s->selected = view->nlines - 2;
6735 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6740 static const struct got_error *
6741 cmd_ref(int argc, char *argv[])
6743 const struct got_error *error;
6744 struct got_repository *repo = NULL;
6745 struct got_worktree *worktree = NULL;
6746 char *cwd = NULL, *repo_path = NULL;
6748 struct tog_view *view;
6749 int *pack_fds = NULL;
6751 while ((ch = getopt(argc, argv, "r:")) != -1) {
6754 repo_path = realpath(optarg, NULL);
6755 if (repo_path == NULL)
6756 return got_error_from_errno2("realpath",
6771 error = got_repo_pack_fds_open(&pack_fds);
6775 if (repo_path == NULL) {
6776 cwd = getcwd(NULL, 0);
6778 return got_error_from_errno("getcwd");
6779 error = got_worktree_open(&worktree, cwd);
6780 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6784 strdup(got_worktree_get_repo_path(worktree));
6786 repo_path = strdup(cwd);
6787 if (repo_path == NULL) {
6788 error = got_error_from_errno("strdup");
6793 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6799 error = apply_unveil(got_repo_get_path(repo), NULL);
6803 error = tog_load_refs(repo, 0);
6807 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6809 error = got_error_from_errno("view_open");
6813 error = open_ref_view(view, repo);
6818 /* Release work tree lock. */
6819 got_worktree_close(worktree);
6822 error = view_loop(view);
6827 const struct got_error *close_err = got_repo_close(repo);
6832 const struct got_error *pack_err =
6833 got_repo_pack_fds_close(pack_fds);
6842 list_commands(FILE *fp)
6846 fprintf(fp, "commands:");
6847 for (i = 0; i < nitems(tog_commands); i++) {
6848 const struct tog_cmd *cmd = &tog_commands[i];
6849 fprintf(fp, " %s", cmd->name);
6855 usage(int hflag, int status)
6857 FILE *fp = (status == 0) ? stdout : stderr;
6859 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6862 fprintf(fp, "lazy usage: %s path\n", getprogname());
6869 make_argv(int argc, ...)
6877 argv = calloc(argc, sizeof(char *));
6880 for (i = 0; i < argc; i++) {
6881 argv[i] = strdup(va_arg(ap, char *));
6882 if (argv[i] == NULL)
6891 * Try to convert 'tog path' into a 'tog log path' command.
6892 * The user could simply have mistyped the command rather than knowingly
6893 * provided a path. So check whether argv[0] can in fact be resolved
6894 * to a path in the HEAD commit and print a special error if not.
6895 * This hack is for mpi@ <3
6897 static const struct got_error *
6898 tog_log_with_path(int argc, char *argv[])
6900 const struct got_error *error = NULL, *close_err;
6901 const struct tog_cmd *cmd = NULL;
6902 struct got_repository *repo = NULL;
6903 struct got_worktree *worktree = NULL;
6904 struct got_object_id *commit_id = NULL, *id = NULL;
6905 struct got_commit_object *commit = NULL;
6906 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6907 char *commit_id_str = NULL, **cmd_argv = NULL;
6908 int *pack_fds = NULL;
6910 cwd = getcwd(NULL, 0);
6912 return got_error_from_errno("getcwd");
6914 error = got_repo_pack_fds_open(&pack_fds);
6918 error = got_worktree_open(&worktree, cwd);
6919 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6923 repo_path = strdup(got_worktree_get_repo_path(worktree));
6925 repo_path = strdup(cwd);
6926 if (repo_path == NULL) {
6927 error = got_error_from_errno("strdup");
6931 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6935 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6940 error = tog_load_refs(repo, 0);
6943 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6944 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6945 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6950 got_worktree_close(worktree);
6954 error = got_object_open_as_commit(&commit, repo, commit_id);
6958 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6960 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6962 fprintf(stderr, "%s: '%s' is no known command or path\n",
6963 getprogname(), argv[0]);
6968 close_err = got_repo_close(repo);
6973 error = got_object_id_str(&commit_id_str, commit_id);
6977 cmd = &tog_commands[0]; /* log */
6979 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6980 error = cmd->cmd_main(argc, cmd_argv);
6983 close_err = got_repo_close(repo);
6988 got_object_commit_close(commit);
6990 got_worktree_close(worktree);
6992 const struct got_error *pack_err =
6993 got_repo_pack_fds_close(pack_fds);
6998 free(commit_id_str);
7005 for (i = 0; i < argc; i++)
7014 main(int argc, char *argv[])
7016 const struct got_error *error = NULL;
7017 const struct tog_cmd *cmd = NULL;
7018 int ch, hflag = 0, Vflag = 0;
7019 char **cmd_argv = NULL;
7020 static const struct option longopts[] = {
7021 { "version", no_argument, NULL, 'V' },
7025 setlocale(LC_CTYPE, "");
7027 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7047 got_version_print_str();
7052 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7060 /* Build an argument vector which runs a default command. */
7061 cmd = &tog_commands[0];
7063 cmd_argv = make_argv(argc, cmd->name);
7067 /* Did the user specify a command? */
7068 for (i = 0; i < nitems(tog_commands); i++) {
7069 if (strncmp(tog_commands[i].name, argv[0],
7070 strlen(argv[0])) == 0) {
7071 cmd = &tog_commands[i];
7080 /* No command specified; try log with a path */
7081 error = tog_log_with_path(argc, argv);
7086 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7093 for (i = 0; i < argc; i++)
7098 if (error && error->code != GOT_ERR_CANCELLED)
7099 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);