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 ch, count; /* current keymap and count prefix */
506 int focussed; /* Only set on one parent or child view at a time. */
508 struct tog_view *parent;
509 struct tog_view *child;
512 * This flag is initially set on parent views when a new child view
513 * is created. It gets toggled when the 'Tab' key switches focus
514 * between parent and child.
515 * The flag indicates whether focus should be passed on to our child
516 * view if this parent view gets picked for focus after another parent
517 * view was closed. This prevents child views from losing focus in such
522 /* type-specific state */
523 enum tog_view_type type;
525 struct tog_diff_view_state diff;
526 struct tog_log_view_state log;
527 struct tog_blame_view_state blame;
528 struct tog_tree_view_state tree;
529 struct tog_ref_view_state ref;
532 const struct got_error *(*show)(struct tog_view *);
533 const struct got_error *(*input)(struct tog_view **,
534 struct tog_view *, int);
535 const struct got_error *(*close)(struct tog_view *);
537 const struct got_error *(*search_start)(struct tog_view *);
538 const struct got_error *(*search_next)(struct tog_view *);
541 #define TOG_SEARCH_FORWARD 1
542 #define TOG_SEARCH_BACKWARD 2
543 int search_next_done;
544 #define TOG_SEARCH_HAVE_MORE 1
545 #define TOG_SEARCH_NO_MORE 2
546 #define TOG_SEARCH_HAVE_NONE 3
551 static const struct got_error *open_diff_view(struct tog_view *,
552 struct got_object_id *, struct got_object_id *,
553 const char *, const char *, int, int, int, struct tog_view *,
554 struct got_repository *);
555 static const struct got_error *show_diff_view(struct tog_view *);
556 static const struct got_error *input_diff_view(struct tog_view **,
557 struct tog_view *, int);
558 static const struct got_error* close_diff_view(struct tog_view *);
559 static const struct got_error *search_start_diff_view(struct tog_view *);
560 static const struct got_error *search_next_diff_view(struct tog_view *);
562 static const struct got_error *open_log_view(struct tog_view *,
563 struct got_object_id *, struct got_repository *,
564 const char *, const char *, int);
565 static const struct got_error * show_log_view(struct tog_view *);
566 static const struct got_error *input_log_view(struct tog_view **,
567 struct tog_view *, int);
568 static const struct got_error *close_log_view(struct tog_view *);
569 static const struct got_error *search_start_log_view(struct tog_view *);
570 static const struct got_error *search_next_log_view(struct tog_view *);
572 static const struct got_error *open_blame_view(struct tog_view *, char *,
573 struct got_object_id *, struct got_repository *);
574 static const struct got_error *show_blame_view(struct tog_view *);
575 static const struct got_error *input_blame_view(struct tog_view **,
576 struct tog_view *, int);
577 static const struct got_error *close_blame_view(struct tog_view *);
578 static const struct got_error *search_start_blame_view(struct tog_view *);
579 static const struct got_error *search_next_blame_view(struct tog_view *);
581 static const struct got_error *open_tree_view(struct tog_view *,
582 struct got_object_id *, const char *, struct got_repository *);
583 static const struct got_error *show_tree_view(struct tog_view *);
584 static const struct got_error *input_tree_view(struct tog_view **,
585 struct tog_view *, int);
586 static const struct got_error *close_tree_view(struct tog_view *);
587 static const struct got_error *search_start_tree_view(struct tog_view *);
588 static const struct got_error *search_next_tree_view(struct tog_view *);
590 static const struct got_error *open_ref_view(struct tog_view *,
591 struct got_repository *);
592 static const struct got_error *show_ref_view(struct tog_view *);
593 static const struct got_error *input_ref_view(struct tog_view **,
594 struct tog_view *, int);
595 static const struct got_error *close_ref_view(struct tog_view *);
596 static const struct got_error *search_start_ref_view(struct tog_view *);
597 static const struct got_error *search_next_ref_view(struct tog_view *);
599 static volatile sig_atomic_t tog_sigwinch_received;
600 static volatile sig_atomic_t tog_sigpipe_received;
601 static volatile sig_atomic_t tog_sigcont_received;
602 static volatile sig_atomic_t tog_sigint_received;
603 static volatile sig_atomic_t tog_sigterm_received;
606 tog_sigwinch(int signo)
608 tog_sigwinch_received = 1;
612 tog_sigpipe(int signo)
614 tog_sigpipe_received = 1;
618 tog_sigcont(int signo)
620 tog_sigcont_received = 1;
624 tog_sigint(int signo)
626 tog_sigint_received = 1;
630 tog_sigterm(int signo)
632 tog_sigterm_received = 1;
636 tog_fatal_signal_received(void)
638 return (tog_sigpipe_received ||
639 tog_sigint_received || tog_sigint_received);
643 static const struct got_error *
644 view_close(struct tog_view *view)
646 const struct got_error *err = NULL;
649 view_close(view->child);
653 err = view->close(view);
655 del_panel(view->panel);
657 delwin(view->window);
662 static struct tog_view *
663 view_open(int nlines, int ncols, int begin_y, int begin_x,
664 enum tog_view_type type)
666 struct tog_view *view = calloc(1, sizeof(*view));
676 view->nlines = nlines ? nlines : LINES - begin_y;
677 view->ncols = ncols ? ncols : COLS - begin_x;
678 view->begin_y = begin_y;
679 view->begin_x = begin_x;
680 view->window = newwin(nlines, ncols, begin_y, begin_x);
681 if (view->window == NULL) {
685 view->panel = new_panel(view->window);
686 if (view->panel == NULL ||
687 set_panel_userptr(view->panel, view) != OK) {
692 keypad(view->window, TRUE);
697 view_split_begin_x(int begin_x)
699 if (begin_x > 0 || COLS < 120)
701 return (COLS - MAX(COLS / 2, 80));
704 static const struct got_error *view_resize(struct tog_view *);
706 static const struct got_error *
707 view_splitscreen(struct tog_view *view)
709 const struct got_error *err = NULL;
712 view->begin_x = view_split_begin_x(0);
713 view->nlines = LINES;
714 view->ncols = COLS - view->begin_x;
717 err = view_resize(view);
721 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
722 return got_error_from_errno("mvwin");
727 static const struct got_error *
728 view_fullscreen(struct tog_view *view)
730 const struct got_error *err = NULL;
734 view->nlines = LINES;
738 err = view_resize(view);
742 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
743 return got_error_from_errno("mvwin");
749 view_is_parent_view(struct tog_view *view)
751 return view->parent == NULL;
755 view_is_splitscreen(struct tog_view *view)
757 return view->begin_x > 0;
761 static const struct got_error *
762 view_resize(struct tog_view *view)
766 if (view->lines > LINES)
767 nlines = view->nlines - (view->lines - LINES);
769 nlines = view->nlines + (LINES - view->lines);
771 if (view->cols > COLS)
772 ncols = view->ncols - (view->cols - COLS);
774 ncols = view->ncols + (COLS - view->cols);
776 if (view->child && view_is_splitscreen(view->child)) {
777 view->child->begin_x = view_split_begin_x(view->begin_x);
778 if (view->child->begin_x == 0) {
781 view_fullscreen(view->child);
782 if (view->child->focussed)
783 show_panel(view->child->panel);
785 show_panel(view->panel);
787 ncols = view->child->begin_x;
789 view_splitscreen(view->child);
790 show_panel(view->child->panel);
792 } else if (view->parent == NULL)
795 if (wresize(view->window, nlines, ncols) == ERR)
796 return got_error_from_errno("wresize");
797 if (replace_panel(view->panel, view->window) == ERR)
798 return got_error_from_errno("replace_panel");
799 wclear(view->window);
801 view->nlines = nlines;
809 static const struct got_error *
810 view_close_child(struct tog_view *view)
812 const struct got_error *err = NULL;
814 if (view->child == NULL)
817 err = view_close(view->child);
822 static const struct got_error *
823 view_set_child(struct tog_view *view, struct tog_view *child)
826 child->parent = view;
828 return view_resize(view);
837 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
838 cols = 80; /* Default */
844 resize_term(lines, cols);
847 static const struct got_error *
848 view_search_start(struct tog_view *view)
850 const struct got_error *err = NULL;
854 if (view->search_started) {
855 regfree(&view->regex);
857 memset(&view->regmatch, 0, sizeof(view->regmatch));
859 view->search_started = 0;
861 if (view->nlines < 1)
864 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
865 wclrtoeol(view->window);
869 ret = wgetnstr(view->window, pattern, sizeof(pattern));
875 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
876 err = view->search_start(view);
878 regfree(&view->regex);
881 view->search_started = 1;
882 view->searching = TOG_SEARCH_FORWARD;
883 view->search_next_done = 0;
884 view->search_next(view);
891 * Compute view->count from numeric user input. User has five-tenths of a
892 * second to follow each numeric keypress with another number to form count.
893 * Return first non-numeric input or ERR and assign total to view->count.
894 * XXX Should we add support for user-defined timeout?
897 get_compound_key(struct tog_view *view, int c)
902 halfdelay(5); /* block for half a second */
906 * Don't overflow. Max valid request should be the greatest
907 * between the longest and total lines; cap at 10 million.
912 n = n * 10 + (c - '0');
913 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
915 /* Massage excessive or inapplicable values at the input handler. */
918 cbreak(); /* return to blocking */
922 static const struct got_error *
923 view_input(struct tog_view **new, int *done, struct tog_view *view,
924 struct tog_view_list_head *views)
926 const struct got_error *err = NULL;
932 /* Clear "no matches" indicator. */
933 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
934 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
935 view->search_next_done = TOG_SEARCH_HAVE_MORE;
939 if (view->searching && !view->search_next_done) {
940 errcode = pthread_mutex_unlock(&tog_mutex);
942 return got_error_set_errno(errcode,
943 "pthread_mutex_unlock");
945 errcode = pthread_mutex_lock(&tog_mutex);
947 return got_error_set_errno(errcode,
948 "pthread_mutex_lock");
949 view->search_next(view);
953 nodelay(stdscr, FALSE);
954 /* Allow threads to make progress while we are waiting for input. */
955 errcode = pthread_mutex_unlock(&tog_mutex);
957 return got_error_set_errno(errcode, "pthread_mutex_unlock");
958 /* If we have an unfinished count, don't get a new key map. */
960 if ((view->count && --view->count == 0) || !view->count) {
961 ch = wgetch(view->window);
962 if (ch >= '1' && ch <= '9')
963 view->ch = ch = get_compound_key(view, ch);
965 errcode = pthread_mutex_lock(&tog_mutex);
967 return got_error_set_errno(errcode, "pthread_mutex_lock");
968 nodelay(stdscr, TRUE);
970 if (tog_sigwinch_received || tog_sigcont_received) {
972 tog_sigwinch_received = 0;
973 tog_sigcont_received = 0;
974 TAILQ_FOREACH(v, views, entry) {
975 err = view_resize(v);
978 err = v->input(new, v, KEY_RESIZE);
982 err = view_resize(v->child);
985 err = v->child->input(new, v->child,
998 view->child->focussed = 1;
999 view->focus_child = 1;
1000 } else if (view->parent) {
1002 view->parent->focussed = 1;
1003 view->parent->focus_child = 0;
1004 if (!view_is_splitscreen(view))
1005 err = view_fullscreen(view->parent);
1009 err = view->input(new, view, ch);
1017 if (view_is_parent_view(view)) {
1018 if (view->child == NULL)
1020 if (view_is_splitscreen(view->child)) {
1022 view->child->focussed = 1;
1023 err = view_fullscreen(view->child);
1025 err = view_splitscreen(view->child);
1028 err = view->child->input(new, view->child,
1031 if (view_is_splitscreen(view)) {
1032 view->parent->focussed = 0;
1034 err = view_fullscreen(view);
1036 err = view_splitscreen(view);
1038 err = view_resize(view->parent);
1042 err = view->input(new, view, KEY_RESIZE);
1049 if (view->search_start)
1050 view_search_start(view);
1052 err = view->input(new, view, ch);
1056 if (view->search_started && view->search_next) {
1057 view->searching = (ch == 'n' ?
1058 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1059 view->search_next_done = 0;
1060 view->search_next(view);
1062 err = view->input(new, view, ch);
1065 err = view->input(new, view, ch);
1073 view_vborder(struct tog_view *view)
1076 const struct tog_view *view_above;
1079 return view_vborder(view->parent);
1081 panel = panel_above(view->panel);
1085 view_above = panel_userptr(panel);
1086 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1087 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1091 view_needs_focus_indication(struct tog_view *view)
1093 if (view_is_parent_view(view)) {
1094 if (view->child == NULL || view->child->focussed)
1096 if (!view_is_splitscreen(view->child))
1098 } else if (!view_is_splitscreen(view))
1101 return view->focussed;
1104 static const struct got_error *
1105 view_loop(struct tog_view *view)
1107 const struct got_error *err = NULL;
1108 struct tog_view_list_head views;
1109 struct tog_view *new_view;
1110 int fast_refresh = 10;
1111 int done = 0, errcode;
1113 errcode = pthread_mutex_lock(&tog_mutex);
1115 return got_error_set_errno(errcode, "pthread_mutex_lock");
1118 TAILQ_INSERT_HEAD(&views, view, entry);
1121 err = view->show(view);
1126 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1127 /* Refresh fast during initialization, then become slower. */
1128 if (fast_refresh && fast_refresh-- == 0)
1129 halfdelay(10); /* switch to once per second */
1131 err = view_input(&new_view, &done, view, &views);
1135 struct tog_view *v, *prev = NULL;
1137 if (view_is_parent_view(view))
1138 prev = TAILQ_PREV(view, tog_view_list_head,
1140 else if (view->parent)
1141 prev = view->parent;
1144 view->parent->child = NULL;
1145 view->parent->focus_child = 0;
1147 err = view_resize(view->parent);
1151 TAILQ_REMOVE(&views, view, entry);
1153 err = view_close(view);
1158 TAILQ_FOREACH(v, &views, entry) {
1162 if (view == NULL && new_view == NULL) {
1163 /* No view has focus. Try to pick one. */
1166 else if (!TAILQ_EMPTY(&views)) {
1167 view = TAILQ_LAST(&views,
1168 tog_view_list_head);
1171 if (view->focus_child) {
1172 view->child->focussed = 1;
1180 struct tog_view *v, *t;
1181 /* Only allow one parent view per type. */
1182 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1183 if (v->type != new_view->type)
1185 TAILQ_REMOVE(&views, v, entry);
1186 err = view_close(v);
1191 TAILQ_INSERT_TAIL(&views, new_view, entry);
1195 if (view_is_parent_view(view)) {
1196 if (view->child && view->child->focussed)
1199 if (view->parent && view->parent->focussed)
1200 view = view->parent;
1202 show_panel(view->panel);
1203 if (view->child && view_is_splitscreen(view->child))
1204 show_panel(view->child->panel);
1205 if (view->parent && view_is_splitscreen(view)) {
1206 err = view->parent->show(view->parent);
1210 err = view->show(view);
1214 err = view->child->show(view->child);
1223 while (!TAILQ_EMPTY(&views)) {
1224 view = TAILQ_FIRST(&views);
1225 TAILQ_REMOVE(&views, view, entry);
1229 errcode = pthread_mutex_unlock(&tog_mutex);
1230 if (errcode && err == NULL)
1231 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1241 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1246 /* Create newly allocated wide-character string equivalent to a byte string. */
1247 static const struct got_error *
1248 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1251 const struct got_error *err = NULL;
1254 *wlen = mbstowcs(NULL, s, 0);
1255 if (*wlen == (size_t)-1) {
1257 if (errno != EILSEQ)
1258 return got_error_from_errno("mbstowcs");
1260 /* byte string invalid in current encoding; try to "fix" it */
1261 err = got_mbsavis(&vis, &vislen, s);
1264 *wlen = mbstowcs(NULL, vis, 0);
1265 if (*wlen == (size_t)-1) {
1266 err = got_error_from_errno("mbstowcs"); /* give up */
1271 *ws = calloc(*wlen + 1, sizeof(**ws));
1273 err = got_error_from_errno("calloc");
1277 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1278 err = got_error_from_errno("mbstowcs");
1289 static const struct got_error *
1290 expand_tab(char **ptr, const char *src)
1293 size_t len, n, idx = 0, sz = 0;
1296 n = len = strlen(src);
1297 dst = malloc(n + 1);
1299 return got_error_from_errno("malloc");
1301 while (idx < len && src[idx]) {
1302 const char c = src[idx];
1305 size_t nb = TABSIZE - sz % TABSIZE;
1308 p = realloc(dst, n + nb);
1311 return got_error_from_errno("realloc");
1316 memset(dst + sz, ' ', nb);
1319 dst[sz++] = src[idx];
1329 * Advance at most n columns from wline starting at offset off.
1330 * Return the index to the first character after the span operation.
1331 * Return the combined column width of all spanned wide character in
1335 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1337 int width, i, cols = 0;
1344 for (i = off; wline[i] != L'\0'; ++i) {
1345 if (wline[i] == L'\t')
1346 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1348 width = wcwidth(wline[i]);
1355 if (cols + width > n)
1365 * Format a line for display, ensuring that it won't overflow a width limit.
1366 * With scrolling, the width returned refers to the scrolled version of the
1367 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1369 static const struct got_error *
1370 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1371 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1373 const struct got_error *err = NULL;
1375 wchar_t *wline = NULL;
1384 err = expand_tab(&exstr, line);
1389 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1394 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1396 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1397 wline[wlen - 1] = L'\0';
1400 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1401 wline[wlen - 1] = L'\0';
1405 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1411 *scrollxp = scrollx;
1419 static const struct got_error*
1420 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1421 struct got_object_id *id, struct got_repository *repo)
1423 static const struct got_error *err = NULL;
1424 struct got_reflist_entry *re;
1430 TAILQ_FOREACH(re, refs, entry) {
1431 struct got_tag_object *tag = NULL;
1432 struct got_object_id *ref_id;
1435 name = got_ref_get_name(re->ref);
1436 if (strcmp(name, GOT_REF_HEAD) == 0)
1438 if (strncmp(name, "refs/", 5) == 0)
1440 if (strncmp(name, "got/", 4) == 0 &&
1441 strncmp(name, "got/backup/", 11) != 0)
1443 if (strncmp(name, "heads/", 6) == 0)
1445 if (strncmp(name, "remotes/", 8) == 0) {
1447 s = strstr(name, "/" GOT_REF_HEAD);
1448 if (s != NULL && s[strlen(s)] == '\0')
1451 err = got_ref_resolve(&ref_id, repo, re->ref);
1454 if (strncmp(name, "tags/", 5) == 0) {
1455 err = got_object_open_as_tag(&tag, repo, ref_id);
1457 if (err->code != GOT_ERR_OBJ_TYPE) {
1461 /* Ref points at something other than a tag. */
1466 cmp = got_object_id_cmp(tag ?
1467 got_object_tag_get_object_id(tag) : ref_id, id);
1470 got_object_tag_close(tag);
1474 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1475 s ? ", " : "", name) == -1) {
1476 err = got_error_from_errno("asprintf");
1487 static const struct got_error *
1488 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1493 smallerthan = strchr(author, '<');
1494 if (smallerthan && smallerthan[1] != '\0')
1495 author = smallerthan + 1;
1496 author[strcspn(author, "@>")] = '\0';
1497 return format_line(wauthor, author_width, NULL, author, 0, limit,
1501 static const struct got_error *
1502 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1503 struct got_object_id *id, const size_t date_display_cols,
1504 int author_display_cols)
1506 struct tog_log_view_state *s = &view->state.log;
1507 const struct got_error *err = NULL;
1508 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1509 char *logmsg0 = NULL, *logmsg = NULL;
1510 char *author = NULL;
1511 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1512 int author_width, logmsg_width;
1513 char *newline, *line = NULL;
1514 int col, limit, scrollx;
1515 const int avail = view->ncols;
1517 time_t committer_time;
1518 struct tog_color *tc;
1520 committer_time = got_object_commit_get_committer_time(commit);
1521 if (gmtime_r(&committer_time, &tm) == NULL)
1522 return got_error_from_errno("gmtime_r");
1523 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1524 return got_error(GOT_ERR_NO_SPACE);
1526 if (avail <= date_display_cols)
1527 limit = MIN(sizeof(datebuf) - 1, avail);
1529 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1530 tc = get_color(&s->colors, TOG_COLOR_DATE);
1532 wattr_on(view->window,
1533 COLOR_PAIR(tc->colorpair), NULL);
1534 waddnstr(view->window, datebuf, limit);
1536 wattr_off(view->window,
1537 COLOR_PAIR(tc->colorpair), NULL);
1544 err = got_object_id_str(&id_str, id);
1547 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1549 wattr_on(view->window,
1550 COLOR_PAIR(tc->colorpair), NULL);
1551 wprintw(view->window, "%.8s ", id_str);
1553 wattr_off(view->window,
1554 COLOR_PAIR(tc->colorpair), NULL);
1561 author = strdup(got_object_commit_get_author(commit));
1562 if (author == NULL) {
1563 err = got_error_from_errno("strdup");
1566 err = format_author(&wauthor, &author_width, author, avail - col, col);
1569 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1571 wattr_on(view->window,
1572 COLOR_PAIR(tc->colorpair), NULL);
1573 waddwstr(view->window, wauthor);
1575 wattr_off(view->window,
1576 COLOR_PAIR(tc->colorpair), NULL);
1577 col += author_width;
1578 while (col < avail && author_width < author_display_cols + 2) {
1579 waddch(view->window, ' ');
1586 err = got_object_commit_get_logmsg(&logmsg0, commit);
1590 while (*logmsg == '\n')
1592 newline = strchr(logmsg, '\n');
1595 limit = avail - col;
1596 if (view->child && view_is_splitscreen(view->child) && limit > 0)
1597 limit--; /* for the border */
1598 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1602 waddwstr(view->window, &wlogmsg[scrollx]);
1603 col += MAX(logmsg_width, 0);
1604 while (col < avail) {
1605 waddch(view->window, ' ');
1617 static struct commit_queue_entry *
1618 alloc_commit_queue_entry(struct got_commit_object *commit,
1619 struct got_object_id *id)
1621 struct commit_queue_entry *entry;
1623 entry = calloc(1, sizeof(*entry));
1628 entry->commit = commit;
1633 pop_commit(struct commit_queue *commits)
1635 struct commit_queue_entry *entry;
1637 entry = TAILQ_FIRST(&commits->head);
1638 TAILQ_REMOVE(&commits->head, entry, entry);
1639 got_object_commit_close(entry->commit);
1640 commits->ncommits--;
1641 /* Don't free entry->id! It is owned by the commit graph. */
1646 free_commits(struct commit_queue *commits)
1648 while (!TAILQ_EMPTY(&commits->head))
1649 pop_commit(commits);
1652 static const struct got_error *
1653 match_commit(int *have_match, struct got_object_id *id,
1654 struct got_commit_object *commit, regex_t *regex)
1656 const struct got_error *err = NULL;
1657 regmatch_t regmatch;
1658 char *id_str = NULL, *logmsg = NULL;
1662 err = got_object_id_str(&id_str, id);
1666 err = got_object_commit_get_logmsg(&logmsg, commit);
1670 if (regexec(regex, got_object_commit_get_author(commit), 1,
1671 ®match, 0) == 0 ||
1672 regexec(regex, got_object_commit_get_committer(commit), 1,
1673 ®match, 0) == 0 ||
1674 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1675 regexec(regex, logmsg, 1, ®match, 0) == 0)
1683 static const struct got_error *
1684 queue_commits(struct tog_log_thread_args *a)
1686 const struct got_error *err = NULL;
1689 * We keep all commits open throughout the lifetime of the log
1690 * view in order to avoid having to re-fetch commits from disk
1691 * while updating the display.
1694 struct got_object_id *id;
1695 struct got_commit_object *commit;
1696 struct commit_queue_entry *entry;
1699 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1701 if (err || id == NULL)
1704 err = got_object_open_as_commit(&commit, a->repo, id);
1707 entry = alloc_commit_queue_entry(commit, id);
1708 if (entry == NULL) {
1709 err = got_error_from_errno("alloc_commit_queue_entry");
1713 errcode = pthread_mutex_lock(&tog_mutex);
1715 err = got_error_set_errno(errcode,
1716 "pthread_mutex_lock");
1720 entry->idx = a->commits->ncommits;
1721 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1722 a->commits->ncommits++;
1724 if (*a->searching == TOG_SEARCH_FORWARD &&
1725 !*a->search_next_done) {
1727 err = match_commit(&have_match, id, commit, a->regex);
1731 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1734 errcode = pthread_mutex_unlock(&tog_mutex);
1735 if (errcode && err == NULL)
1736 err = got_error_set_errno(errcode,
1737 "pthread_mutex_unlock");
1740 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1746 select_commit(struct tog_log_view_state *s)
1748 struct commit_queue_entry *entry;
1751 entry = s->first_displayed_entry;
1753 if (ncommits == s->selected) {
1754 s->selected_entry = entry;
1757 entry = TAILQ_NEXT(entry, entry);
1762 static const struct got_error *
1763 draw_commits(struct tog_view *view)
1765 const struct got_error *err = NULL;
1766 struct tog_log_view_state *s = &view->state.log;
1767 struct commit_queue_entry *entry = s->selected_entry;
1768 const int limit = view->nlines;
1770 int ncommits, author_cols = 4;
1771 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1772 char *refs_str = NULL;
1774 struct tog_color *tc;
1775 static const size_t date_display_cols = 12;
1777 if (s->selected_entry &&
1778 !(view->searching && view->search_next_done == 0)) {
1779 struct got_reflist_head *refs;
1780 err = got_object_id_str(&id_str, s->selected_entry->id);
1783 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1784 s->selected_entry->id);
1786 err = build_refs_str(&refs_str, refs,
1787 s->selected_entry->id, s->repo);
1793 if (s->thread_args.commits_needed == 0)
1794 halfdelay(10); /* disable fast refresh */
1796 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1797 if (asprintf(&ncommits_str, " [%d/%d] %s",
1798 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1799 (view->searching && !view->search_next_done) ?
1800 "searching..." : "loading...") == -1) {
1801 err = got_error_from_errno("asprintf");
1805 const char *search_str = NULL;
1807 if (view->searching) {
1808 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1809 search_str = "no more matches";
1810 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1811 search_str = "no matches found";
1812 else if (!view->search_next_done)
1813 search_str = "searching...";
1816 if (asprintf(&ncommits_str, " [%d/%d] %s",
1817 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1818 search_str ? search_str :
1819 (refs_str ? refs_str : "")) == -1) {
1820 err = got_error_from_errno("asprintf");
1825 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1826 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1827 "........................................",
1828 s->in_repo_path, ncommits_str) == -1) {
1829 err = got_error_from_errno("asprintf");
1833 } else if (asprintf(&header, "commit %s%s",
1834 id_str ? id_str : "........................................",
1835 ncommits_str) == -1) {
1836 err = got_error_from_errno("asprintf");
1840 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1844 werase(view->window);
1846 if (view_needs_focus_indication(view))
1847 wstandout(view->window);
1848 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1850 wattr_on(view->window,
1851 COLOR_PAIR(tc->colorpair), NULL);
1852 waddwstr(view->window, wline);
1854 wattr_off(view->window,
1855 COLOR_PAIR(tc->colorpair), NULL);
1856 while (width < view->ncols) {
1857 waddch(view->window, ' ');
1860 if (view_needs_focus_indication(view))
1861 wstandend(view->window);
1866 /* Grow author column size if necessary, and set view->maxx. */
1867 entry = s->first_displayed_entry;
1871 char *author, *eol, *msg, *msg0;
1872 wchar_t *wauthor, *wmsg;
1874 if (ncommits >= limit - 1)
1876 author = strdup(got_object_commit_get_author(entry->commit));
1877 if (author == NULL) {
1878 err = got_error_from_errno("strdup");
1881 err = format_author(&wauthor, &width, author, COLS,
1883 if (author_cols < width)
1884 author_cols = width;
1887 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1891 while (*msg == '\n')
1893 if ((eol = strchr(msg, '\n')))
1895 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
1896 date_display_cols + author_cols, 0);
1899 view->maxx = MAX(view->maxx, width);
1903 entry = TAILQ_NEXT(entry, entry);
1906 entry = s->first_displayed_entry;
1907 s->last_displayed_entry = s->first_displayed_entry;
1910 if (ncommits >= limit - 1)
1912 if (ncommits == s->selected)
1913 wstandout(view->window);
1914 err = draw_commit(view, entry->commit, entry->id,
1915 date_display_cols, author_cols);
1916 if (ncommits == s->selected)
1917 wstandend(view->window);
1921 s->last_displayed_entry = entry;
1922 entry = TAILQ_NEXT(entry, entry);
1935 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1937 struct commit_queue_entry *entry;
1940 entry = TAILQ_FIRST(&s->commits.head);
1941 if (s->first_displayed_entry == entry)
1944 entry = s->first_displayed_entry;
1945 while (entry && nscrolled < maxscroll) {
1946 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1948 s->first_displayed_entry = entry;
1954 static const struct got_error *
1955 trigger_log_thread(struct tog_view *view, int wait)
1957 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1960 halfdelay(1); /* fast refresh while loading commits */
1962 while (ta->commits_needed > 0 || ta->load_all) {
1963 if (ta->log_complete)
1966 /* Wake the log thread. */
1967 errcode = pthread_cond_signal(&ta->need_commits);
1969 return got_error_set_errno(errcode,
1970 "pthread_cond_signal");
1973 * The mutex will be released while the view loop waits
1974 * in wgetch(), at which time the log thread will run.
1979 /* Display progress update in log view. */
1980 show_log_view(view);
1984 /* Wait right here while next commit is being loaded. */
1985 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1987 return got_error_set_errno(errcode,
1988 "pthread_cond_wait");
1990 /* Display progress update in log view. */
1991 show_log_view(view);
1999 static const struct got_error *
2000 log_scroll_down(struct tog_view *view, int maxscroll)
2002 struct tog_log_view_state *s = &view->state.log;
2003 const struct got_error *err = NULL;
2004 struct commit_queue_entry *pentry;
2005 int nscrolled = 0, ncommits_needed;
2007 if (s->last_displayed_entry == NULL)
2010 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2011 if (s->commits.ncommits < ncommits_needed &&
2012 !s->thread_args.log_complete) {
2014 * Ask the log thread for required amount of commits.
2016 s->thread_args.commits_needed += maxscroll;
2017 err = trigger_log_thread(view, 1);
2023 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2027 s->last_displayed_entry = pentry;
2029 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2032 s->first_displayed_entry = pentry;
2033 } while (++nscrolled < maxscroll);
2038 static const struct got_error *
2039 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
2040 struct got_commit_object *commit, struct got_object_id *commit_id,
2041 struct tog_view *log_view, struct got_repository *repo)
2043 const struct got_error *err;
2044 struct got_object_qid *parent_id;
2045 struct tog_view *diff_view;
2047 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2048 if (diff_view == NULL)
2049 return got_error_from_errno("view_open");
2051 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2052 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2053 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2055 *new_view = diff_view;
2059 static const struct got_error *
2060 tree_view_visit_subtree(struct tog_tree_view_state *s,
2061 struct got_tree_object *subtree)
2063 struct tog_parent_tree *parent;
2065 parent = calloc(1, sizeof(*parent));
2067 return got_error_from_errno("calloc");
2069 parent->tree = s->tree;
2070 parent->first_displayed_entry = s->first_displayed_entry;
2071 parent->selected_entry = s->selected_entry;
2072 parent->selected = s->selected;
2073 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2076 s->first_displayed_entry = NULL;
2080 static const struct got_error *
2081 tree_view_walk_path(struct tog_tree_view_state *s,
2082 struct got_commit_object *commit, const char *path)
2084 const struct got_error *err = NULL;
2085 struct got_tree_object *tree = NULL;
2087 char *slash, *subpath = NULL;
2089 /* Walk the path and open corresponding tree objects. */
2092 struct got_tree_entry *te;
2093 struct got_object_id *tree_id;
2099 /* Ensure the correct subtree entry is selected. */
2100 slash = strchr(p, '/');
2102 te_name = strdup(p);
2104 te_name = strndup(p, slash - p);
2105 if (te_name == NULL) {
2106 err = got_error_from_errno("strndup");
2109 te = got_object_tree_find_entry(s->tree, te_name);
2111 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2116 s->first_displayed_entry = s->selected_entry = te;
2118 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2119 break; /* jump to this file's entry */
2121 slash = strchr(p, '/');
2123 subpath = strndup(path, slash - path);
2125 subpath = strdup(path);
2126 if (subpath == NULL) {
2127 err = got_error_from_errno("strdup");
2131 err = got_object_id_by_path(&tree_id, s->repo, commit,
2136 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2141 err = tree_view_visit_subtree(s, tree);
2143 got_object_tree_close(tree);
2157 static const struct got_error *
2158 browse_commit_tree(struct tog_view **new_view, int begin_x,
2159 struct commit_queue_entry *entry, const char *path,
2160 const char *head_ref_name, struct got_repository *repo)
2162 const struct got_error *err = NULL;
2163 struct tog_tree_view_state *s;
2164 struct tog_view *tree_view;
2166 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2167 if (tree_view == NULL)
2168 return got_error_from_errno("view_open");
2170 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2173 s = &tree_view->state.tree;
2175 *new_view = tree_view;
2177 if (got_path_is_root_dir(path))
2180 return tree_view_walk_path(s, entry->commit, path);
2183 static const struct got_error *
2184 block_signals_used_by_main_thread(void)
2189 if (sigemptyset(&sigset) == -1)
2190 return got_error_from_errno("sigemptyset");
2192 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2193 if (sigaddset(&sigset, SIGWINCH) == -1)
2194 return got_error_from_errno("sigaddset");
2195 if (sigaddset(&sigset, SIGCONT) == -1)
2196 return got_error_from_errno("sigaddset");
2197 if (sigaddset(&sigset, SIGINT) == -1)
2198 return got_error_from_errno("sigaddset");
2199 if (sigaddset(&sigset, SIGTERM) == -1)
2200 return got_error_from_errno("sigaddset");
2202 /* ncurses handles SIGTSTP */
2203 if (sigaddset(&sigset, SIGTSTP) == -1)
2204 return got_error_from_errno("sigaddset");
2206 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2208 return got_error_set_errno(errcode, "pthread_sigmask");
2214 log_thread(void *arg)
2216 const struct got_error *err = NULL;
2218 struct tog_log_thread_args *a = arg;
2221 err = block_signals_used_by_main_thread();
2225 while (!done && !err && !tog_fatal_signal_received()) {
2226 err = queue_commits(a);
2228 if (err->code != GOT_ERR_ITER_COMPLETED)
2232 } else if (a->commits_needed > 0 && !a->load_all)
2233 a->commits_needed--;
2235 errcode = pthread_mutex_lock(&tog_mutex);
2237 err = got_error_set_errno(errcode,
2238 "pthread_mutex_lock");
2240 } else if (*a->quit)
2242 else if (*a->first_displayed_entry == NULL) {
2243 *a->first_displayed_entry =
2244 TAILQ_FIRST(&a->commits->head);
2245 *a->selected_entry = *a->first_displayed_entry;
2248 errcode = pthread_cond_signal(&a->commit_loaded);
2250 err = got_error_set_errno(errcode,
2251 "pthread_cond_signal");
2252 pthread_mutex_unlock(&tog_mutex);
2257 a->commits_needed = 0;
2259 if (a->commits_needed == 0 && !a->load_all) {
2260 errcode = pthread_cond_wait(&a->need_commits,
2263 err = got_error_set_errno(errcode,
2264 "pthread_cond_wait");
2270 errcode = pthread_mutex_unlock(&tog_mutex);
2271 if (errcode && err == NULL)
2272 err = got_error_set_errno(errcode,
2273 "pthread_mutex_unlock");
2275 a->log_complete = 1;
2279 static const struct got_error *
2280 stop_log_thread(struct tog_log_view_state *s)
2282 const struct got_error *err = NULL;
2287 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2289 return got_error_set_errno(errcode,
2290 "pthread_cond_signal");
2291 errcode = pthread_mutex_unlock(&tog_mutex);
2293 return got_error_set_errno(errcode,
2294 "pthread_mutex_unlock");
2295 errcode = pthread_join(s->thread, (void **)&err);
2297 return got_error_set_errno(errcode, "pthread_join");
2298 errcode = pthread_mutex_lock(&tog_mutex);
2300 return got_error_set_errno(errcode,
2301 "pthread_mutex_lock");
2305 if (s->thread_args.repo) {
2306 err = got_repo_close(s->thread_args.repo);
2307 s->thread_args.repo = NULL;
2310 if (s->thread_args.pack_fds) {
2311 const struct got_error *pack_err =
2312 got_repo_pack_fds_close(s->thread_args.pack_fds);
2315 s->thread_args.pack_fds = NULL;
2318 if (s->thread_args.graph) {
2319 got_commit_graph_close(s->thread_args.graph);
2320 s->thread_args.graph = NULL;
2326 static const struct got_error *
2327 close_log_view(struct tog_view *view)
2329 const struct got_error *err = NULL;
2330 struct tog_log_view_state *s = &view->state.log;
2333 err = stop_log_thread(s);
2335 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2336 if (errcode && err == NULL)
2337 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2339 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2340 if (errcode && err == NULL)
2341 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2343 free_commits(&s->commits);
2344 free(s->in_repo_path);
2345 s->in_repo_path = NULL;
2348 free(s->head_ref_name);
2349 s->head_ref_name = NULL;
2353 static const struct got_error *
2354 search_start_log_view(struct tog_view *view)
2356 struct tog_log_view_state *s = &view->state.log;
2358 s->matched_entry = NULL;
2359 s->search_entry = NULL;
2363 static const struct got_error *
2364 search_next_log_view(struct tog_view *view)
2366 const struct got_error *err = NULL;
2367 struct tog_log_view_state *s = &view->state.log;
2368 struct commit_queue_entry *entry;
2370 /* Display progress update in log view. */
2371 show_log_view(view);
2375 if (s->search_entry) {
2377 errcode = pthread_mutex_unlock(&tog_mutex);
2379 return got_error_set_errno(errcode,
2380 "pthread_mutex_unlock");
2381 ch = wgetch(view->window);
2382 errcode = pthread_mutex_lock(&tog_mutex);
2384 return got_error_set_errno(errcode,
2385 "pthread_mutex_lock");
2386 if (ch == KEY_BACKSPACE) {
2387 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2390 if (view->searching == TOG_SEARCH_FORWARD)
2391 entry = TAILQ_NEXT(s->search_entry, entry);
2393 entry = TAILQ_PREV(s->search_entry,
2394 commit_queue_head, entry);
2395 } else if (s->matched_entry) {
2396 int matched_idx = s->matched_entry->idx;
2397 int selected_idx = s->selected_entry->idx;
2400 * If the user has moved the cursor after we hit a match,
2401 * the position from where we should continue searching
2402 * might have changed.
2404 if (view->searching == TOG_SEARCH_FORWARD) {
2405 if (matched_idx > selected_idx)
2406 entry = TAILQ_NEXT(s->selected_entry, entry);
2408 entry = TAILQ_NEXT(s->matched_entry, entry);
2410 if (matched_idx < selected_idx)
2411 entry = TAILQ_PREV(s->selected_entry,
2412 commit_queue_head, entry);
2414 entry = TAILQ_PREV(s->matched_entry,
2415 commit_queue_head, entry);
2418 entry = s->selected_entry;
2424 if (entry == NULL) {
2425 if (s->thread_args.log_complete ||
2426 view->searching == TOG_SEARCH_BACKWARD) {
2427 view->search_next_done =
2428 (s->matched_entry == NULL ?
2429 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2430 s->search_entry = NULL;
2434 * Poke the log thread for more commits and return,
2435 * allowing the main loop to make progress. Search
2436 * will resume at s->search_entry once we come back.
2438 s->thread_args.commits_needed++;
2439 return trigger_log_thread(view, 0);
2442 err = match_commit(&have_match, entry->id, entry->commit,
2447 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2448 s->matched_entry = entry;
2452 s->search_entry = entry;
2453 if (view->searching == TOG_SEARCH_FORWARD)
2454 entry = TAILQ_NEXT(entry, entry);
2456 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2459 if (s->matched_entry) {
2460 int cur = s->selected_entry->idx;
2461 while (cur < s->matched_entry->idx) {
2462 err = input_log_view(NULL, view, KEY_DOWN);
2467 while (cur > s->matched_entry->idx) {
2468 err = input_log_view(NULL, view, KEY_UP);
2475 s->search_entry = NULL;
2480 static const struct got_error *
2481 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2482 struct got_repository *repo, const char *head_ref_name,
2483 const char *in_repo_path, int log_branches)
2485 const struct got_error *err = NULL;
2486 struct tog_log_view_state *s = &view->state.log;
2487 struct got_repository *thread_repo = NULL;
2488 struct got_commit_graph *thread_graph = NULL;
2491 if (in_repo_path != s->in_repo_path) {
2492 free(s->in_repo_path);
2493 s->in_repo_path = strdup(in_repo_path);
2494 if (s->in_repo_path == NULL)
2495 return got_error_from_errno("strdup");
2498 /* The commit queue only contains commits being displayed. */
2499 TAILQ_INIT(&s->commits.head);
2500 s->commits.ncommits = 0;
2503 if (head_ref_name) {
2504 s->head_ref_name = strdup(head_ref_name);
2505 if (s->head_ref_name == NULL) {
2506 err = got_error_from_errno("strdup");
2510 s->start_id = got_object_id_dup(start_id);
2511 if (s->start_id == NULL) {
2512 err = got_error_from_errno("got_object_id_dup");
2515 s->log_branches = log_branches;
2517 STAILQ_INIT(&s->colors);
2518 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2519 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2520 get_color_value("TOG_COLOR_COMMIT"));
2523 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2524 get_color_value("TOG_COLOR_AUTHOR"));
2526 free_colors(&s->colors);
2529 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2530 get_color_value("TOG_COLOR_DATE"));
2532 free_colors(&s->colors);
2537 view->show = show_log_view;
2538 view->input = input_log_view;
2539 view->close = close_log_view;
2540 view->search_start = search_start_log_view;
2541 view->search_next = search_next_log_view;
2543 if (s->thread_args.pack_fds == NULL) {
2544 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2548 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2549 s->thread_args.pack_fds);
2552 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2556 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2557 s->repo, NULL, NULL);
2561 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2563 err = got_error_set_errno(errcode, "pthread_cond_init");
2566 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2568 err = got_error_set_errno(errcode, "pthread_cond_init");
2572 s->thread_args.commits_needed = view->nlines;
2573 s->thread_args.graph = thread_graph;
2574 s->thread_args.commits = &s->commits;
2575 s->thread_args.in_repo_path = s->in_repo_path;
2576 s->thread_args.start_id = s->start_id;
2577 s->thread_args.repo = thread_repo;
2578 s->thread_args.log_complete = 0;
2579 s->thread_args.quit = &s->quit;
2580 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2581 s->thread_args.selected_entry = &s->selected_entry;
2582 s->thread_args.searching = &view->searching;
2583 s->thread_args.search_next_done = &view->search_next_done;
2584 s->thread_args.regex = &view->regex;
2587 close_log_view(view);
2591 static const struct got_error *
2592 show_log_view(struct tog_view *view)
2594 const struct got_error *err;
2595 struct tog_log_view_state *s = &view->state.log;
2597 if (s->thread == NULL) {
2598 int errcode = pthread_create(&s->thread, NULL, log_thread,
2601 return got_error_set_errno(errcode, "pthread_create");
2602 if (s->thread_args.commits_needed > 0) {
2603 err = trigger_log_thread(view, 1);
2609 return draw_commits(view);
2612 static const struct got_error *
2613 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2615 const struct got_error *err = NULL;
2616 struct tog_log_view_state *s = &view->state.log;
2617 struct tog_view *diff_view = NULL, *tree_view = NULL;
2618 struct tog_view *ref_view = NULL;
2619 struct commit_queue_entry *entry;
2620 int begin_x = 0, n, nscroll = view->nlines - 1;
2622 if (s->thread_args.load_all) {
2623 if (ch == KEY_BACKSPACE)
2624 s->thread_args.load_all = 0;
2625 else if (s->thread_args.log_complete) {
2626 s->thread_args.load_all = 0;
2627 log_scroll_down(view, s->commits.ncommits);
2628 s->selected = MIN(view->nlines - 2,
2629 s->commits.ncommits - 1);
2643 view->x = MAX(view->maxx - view->ncols / 2, 0);
2648 if (view->x + view->ncols / 2 < view->maxx)
2649 view->x += 2; /* move two columns right */
2655 view->x -= MIN(view->x, 2); /* move two columns back */
2664 if (s->selected_entry->idx == 0)
2666 if (s->first_displayed_entry == NULL)
2668 if (s->selected > 0)
2671 log_scroll_up(s, 1);
2677 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2688 if (s->first_displayed_entry == NULL)
2690 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2691 s->selected = MAX(0, s->selected - nscroll - 1);
2693 log_scroll_up(s, nscroll);
2695 if (s->selected_entry->idx == 0)
2703 if (s->first_displayed_entry == NULL)
2705 if (s->selected < MIN(view->nlines - 2,
2706 s->commits.ncommits - 1))
2709 err = log_scroll_down(view, 1);
2714 if (s->thread_args.log_complete &&
2715 s->selected_entry->idx == s->commits.ncommits - 1)
2720 /* We don't know yet how many commits, so we're forced to
2721 * traverse them all. */
2723 if (!s->thread_args.log_complete) {
2724 s->thread_args.load_all = 1;
2725 return trigger_log_thread(view, 0);
2729 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2730 for (n = 0; n < view->nlines - 1; n++) {
2733 s->first_displayed_entry = entry;
2734 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2737 s->selected = n - 1;
2749 struct commit_queue_entry *first;
2750 first = s->first_displayed_entry;
2751 if (first == NULL) {
2755 err = log_scroll_down(view, nscroll);
2758 if (first == s->first_displayed_entry &&
2759 s->selected < MIN(view->nlines - 2,
2760 s->commits.ncommits - 1)) {
2761 /* can't scroll further down */
2762 s->selected += MIN(s->last_displayed_entry->idx -
2763 s->selected_entry->idx, nscroll + 1);
2766 if (s->thread_args.log_complete &&
2767 s->selected_entry->idx == s->commits.ncommits - 1)
2772 if (s->selected > view->nlines - 2)
2773 s->selected = view->nlines - 2;
2774 if (s->selected > s->commits.ncommits - 1)
2775 s->selected = s->commits.ncommits - 1;
2777 if (s->commits.ncommits < view->nlines - 1 &&
2778 !s->thread_args.log_complete) {
2779 s->thread_args.commits_needed += (view->nlines - 1) -
2780 s->commits.ncommits;
2781 err = trigger_log_thread(view, 1);
2787 if (s->selected_entry == NULL)
2789 if (view_is_parent_view(view))
2790 begin_x = view_split_begin_x(view->begin_x);
2791 err = open_diff_view_for_commit(&diff_view, begin_x,
2792 s->selected_entry->commit, s->selected_entry->id,
2797 diff_view->focussed = 1;
2798 if (view_is_parent_view(view)) {
2799 err = view_close_child(view);
2802 err = view_set_child(view, diff_view);
2805 view->focus_child = 1;
2807 *new_view = diff_view;
2811 if (s->selected_entry == NULL)
2813 if (view_is_parent_view(view))
2814 begin_x = view_split_begin_x(view->begin_x);
2815 err = browse_commit_tree(&tree_view, begin_x,
2816 s->selected_entry, s->in_repo_path, s->head_ref_name,
2821 tree_view->focussed = 1;
2822 if (view_is_parent_view(view)) {
2823 err = view_close_child(view);
2826 err = view_set_child(view, tree_view);
2829 view->focus_child = 1;
2831 *new_view = tree_view;
2837 if (ch == KEY_BACKSPACE &&
2838 got_path_is_root_dir(s->in_repo_path))
2840 err = stop_log_thread(s);
2843 if (ch == KEY_BACKSPACE) {
2845 err = got_path_dirname(&parent_path, s->in_repo_path);
2848 free(s->in_repo_path);
2849 s->in_repo_path = parent_path;
2850 s->thread_args.in_repo_path = s->in_repo_path;
2851 } else if (ch == CTRL('l')) {
2852 struct got_object_id *start_id;
2853 err = got_repo_match_object_id(&start_id, NULL,
2854 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2855 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2859 s->start_id = start_id;
2860 s->thread_args.start_id = s->start_id;
2862 s->log_branches = !s->log_branches;
2864 if (s->thread_args.pack_fds == NULL) {
2865 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2869 err = got_repo_open(&s->thread_args.repo,
2870 got_repo_get_path(s->repo), NULL,
2871 s->thread_args.pack_fds);
2875 err = tog_load_refs(s->repo, 0);
2878 err = got_commit_graph_open(&s->thread_args.graph,
2879 s->in_repo_path, !s->log_branches);
2882 err = got_commit_graph_iter_start(s->thread_args.graph,
2883 s->start_id, s->repo, NULL, NULL);
2886 free_commits(&s->commits);
2887 s->first_displayed_entry = NULL;
2888 s->last_displayed_entry = NULL;
2889 s->selected_entry = NULL;
2891 s->thread_args.log_complete = 0;
2893 s->thread_args.commits_needed = view->nlines;
2894 s->matched_entry = NULL;
2895 s->search_entry = NULL;
2899 if (view_is_parent_view(view))
2900 begin_x = view_split_begin_x(view->begin_x);
2901 ref_view = view_open(view->nlines, view->ncols,
2902 view->begin_y, begin_x, TOG_VIEW_REF);
2903 if (ref_view == NULL)
2904 return got_error_from_errno("view_open");
2905 err = open_ref_view(ref_view, s->repo);
2907 view_close(ref_view);
2911 ref_view->focussed = 1;
2912 if (view_is_parent_view(view)) {
2913 err = view_close_child(view);
2916 err = view_set_child(view, ref_view);
2919 view->focus_child = 1;
2921 *new_view = ref_view;
2931 static const struct got_error *
2932 apply_unveil(const char *repo_path, const char *worktree_path)
2934 const struct got_error *error;
2937 if (unveil("gmon.out", "rwc") != 0)
2938 return got_error_from_errno2("unveil", "gmon.out");
2940 if (repo_path && unveil(repo_path, "r") != 0)
2941 return got_error_from_errno2("unveil", repo_path);
2943 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2944 return got_error_from_errno2("unveil", worktree_path);
2946 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2947 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2949 error = got_privsep_unveil_exec_helpers();
2953 if (unveil(NULL, NULL) != 0)
2954 return got_error_from_errno("unveil");
2963 * Override default signal handlers before starting ncurses.
2964 * This should prevent ncurses from installing its own
2965 * broken cleanup() signal handler.
2967 signal(SIGWINCH, tog_sigwinch);
2968 signal(SIGPIPE, tog_sigpipe);
2969 signal(SIGCONT, tog_sigcont);
2970 signal(SIGINT, tog_sigint);
2971 signal(SIGTERM, tog_sigterm);
2975 halfdelay(1); /* Do fast refresh while initial view is loading. */
2978 intrflush(stdscr, FALSE);
2979 keypad(stdscr, TRUE);
2981 if (getenv("TOG_COLORS") != NULL) {
2983 use_default_colors();
2987 static const struct got_error *
2988 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2989 struct got_repository *repo, struct got_worktree *worktree)
2991 const struct got_error *err = NULL;
2994 *in_repo_path = strdup("/");
2995 if (*in_repo_path == NULL)
2996 return got_error_from_errno("strdup");
3001 const char *prefix = got_worktree_get_path_prefix(worktree);
3004 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3007 if (asprintf(in_repo_path, "%s%s%s", prefix,
3008 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3010 err = got_error_from_errno("asprintf");
3011 *in_repo_path = NULL;
3015 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3020 static const struct got_error *
3021 cmd_log(int argc, char *argv[])
3023 const struct got_error *error;
3024 struct got_repository *repo = NULL;
3025 struct got_worktree *worktree = NULL;
3026 struct got_object_id *start_id = NULL;
3027 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3028 char *start_commit = NULL, *label = NULL;
3029 struct got_reference *ref = NULL;
3030 const char *head_ref_name = NULL;
3031 int ch, log_branches = 0;
3032 struct tog_view *view;
3033 int *pack_fds = NULL;
3035 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3041 start_commit = optarg;
3044 repo_path = realpath(optarg, NULL);
3045 if (repo_path == NULL)
3046 return got_error_from_errno2("realpath",
3061 error = got_repo_pack_fds_open(&pack_fds);
3065 if (repo_path == NULL) {
3066 cwd = getcwd(NULL, 0);
3068 return got_error_from_errno("getcwd");
3069 error = got_worktree_open(&worktree, cwd);
3070 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3074 strdup(got_worktree_get_repo_path(worktree));
3076 repo_path = strdup(cwd);
3077 if (repo_path == NULL) {
3078 error = got_error_from_errno("strdup");
3083 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3087 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3094 error = apply_unveil(got_repo_get_path(repo),
3095 worktree ? got_worktree_get_root_path(worktree) : NULL);
3099 /* already loaded by tog_log_with_path()? */
3100 if (TAILQ_EMPTY(&tog_refs)) {
3101 error = tog_load_refs(repo, 0);
3106 if (start_commit == NULL) {
3107 error = got_repo_match_object_id(&start_id, &label,
3108 worktree ? got_worktree_get_head_ref_name(worktree) :
3109 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3112 head_ref_name = label;
3114 error = got_ref_open(&ref, repo, start_commit, 0);
3116 head_ref_name = got_ref_get_name(ref);
3117 else if (error->code != GOT_ERR_NOT_REF)
3119 error = got_repo_match_object_id(&start_id, NULL,
3120 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3125 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3127 error = got_error_from_errno("view_open");
3130 error = open_log_view(view, start_id, repo, head_ref_name,
3131 in_repo_path, log_branches);
3135 /* Release work tree lock. */
3136 got_worktree_close(worktree);
3139 error = view_loop(view);
3149 const struct got_error *close_err = got_repo_close(repo);
3154 got_worktree_close(worktree);
3156 const struct got_error *pack_err =
3157 got_repo_pack_fds_close(pack_fds);
3169 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3170 "[-w] object1 object2\n", getprogname());
3175 match_line(const char *line, regex_t *regex, size_t nmatch,
3176 regmatch_t *regmatch)
3178 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3182 match_color(struct tog_colors *colors, const char *line)
3184 struct tog_color *tc = NULL;
3186 STAILQ_FOREACH(tc, colors, entry) {
3187 if (match_line(line, &tc->regex, 0, NULL))
3194 static const struct got_error *
3195 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3196 WINDOW *window, int skipcol, regmatch_t *regmatch)
3198 const struct got_error *err = NULL;
3200 wchar_t *wline = NULL;
3201 int rme, rms, n, width, scrollx;
3202 int width0 = 0, width1 = 0, width2 = 0;
3203 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3207 rms = regmatch->rm_so;
3208 rme = regmatch->rm_eo;
3210 err = expand_tab(&exstr, line);
3214 /* Split the line into 3 segments, according to match offsets. */
3215 seg0 = strndup(exstr, rms);
3217 err = got_error_from_errno("strndup");
3220 seg1 = strndup(exstr + rms, rme - rms);
3222 err = got_error_from_errno("strndup");
3225 seg2 = strdup(exstr + rme);
3227 err = got_error_from_errno("strndup");
3231 /* draw up to matched token if we haven't scrolled past it */
3232 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3236 n = MAX(width0 - skipcol, 0);
3239 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3240 wlimit, col_tab_align, 1);
3243 waddwstr(window, &wline[scrollx]);
3253 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3257 wlen = wcslen(wline);
3259 width = wcwidth(wline[i]);
3261 /* should not happen, tabs are expanded */
3262 err = got_error(GOT_ERR_RANGE);
3265 if (width0 + w + width > skipcol)
3270 /* draw (visible part of) matched token (if scrolled into it) */
3271 if (width1 - w > 0) {
3272 wattron(window, A_STANDOUT);
3273 waddwstr(window, &wline[i]);
3274 wattroff(window, A_STANDOUT);
3275 wlimit -= (width1 - w);
3276 *wtotal += (width1 - w);
3280 if (wlimit > 0) { /* draw rest of line */
3282 if (skipcol > width0 + width1) {
3283 err = format_line(&wline, &width2, &scrollx, seg2,
3284 skipcol - (width0 + width1), wlimit,
3288 waddwstr(window, &wline[scrollx]);
3290 err = format_line(&wline, &width2, NULL, seg2, 0,
3291 wlimit, col_tab_align, 1);
3294 waddwstr(window, wline);
3307 static const struct got_error *
3308 draw_file(struct tog_view *view, const char *header)
3310 struct tog_diff_view_state *s = &view->state.diff;
3311 regmatch_t *regmatch = &view->regmatch;
3312 const struct got_error *err;
3315 size_t linesize = 0;
3317 struct tog_color *tc;
3320 int max_lines = view->nlines;
3321 int nlines = s->nlines;
3324 line_offset = s->line_offsets[s->first_displayed_line - 1];
3325 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3326 return got_error_from_errno("fseek");
3328 werase(view->window);
3331 if (asprintf(&line, "[%d/%d] %s",
3332 s->first_displayed_line - 1 + s->selected_line, nlines,
3334 return got_error_from_errno("asprintf");
3335 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3341 if (view_needs_focus_indication(view))
3342 wstandout(view->window);
3343 waddwstr(view->window, wline);
3346 if (view_needs_focus_indication(view))
3347 wstandend(view->window);
3348 if (width <= view->ncols - 1)
3349 waddch(view->window, '\n');
3359 while (max_lines > 0 && nprinted < max_lines) {
3360 linelen = getline(&line, &linesize, s->f);
3361 if (linelen == -1) {
3367 return got_ferror(s->f, GOT_ERR_IO);
3370 /* Set view->maxx based on full line length. */
3371 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3377 view->maxx = MAX(view->maxx, width);
3381 tc = match_color(&s->colors, line);
3383 wattr_on(view->window,
3384 COLOR_PAIR(tc->colorpair), NULL);
3385 if (s->first_displayed_line + nprinted == s->matched_line &&
3386 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3387 err = add_matched_line(&width, line, view->ncols, 0,
3388 view->window, view->x, regmatch);
3395 err = format_line(&wline, &width, &skip, line,
3396 view->x, view->ncols, 0, view->x ? 1 : 0);
3401 waddwstr(view->window, &wline[skip]);
3406 wattr_off(view->window,
3407 COLOR_PAIR(tc->colorpair), NULL);
3408 if (width <= view->ncols - 1)
3409 waddch(view->window, '\n');
3414 s->last_displayed_line = s->first_displayed_line +
3417 s->last_displayed_line = s->first_displayed_line;
3422 while (nprinted < view->nlines) {
3423 waddch(view->window, '\n');
3427 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3433 wstandout(view->window);
3434 waddwstr(view->window, wline);
3437 wstandend(view->window);
3444 get_datestr(time_t *time, char *datebuf)
3446 struct tm mytm, *tm;
3449 tm = gmtime_r(time, &mytm);
3452 s = asctime_r(tm, datebuf);
3455 p = strchr(s, '\n');
3461 static const struct got_error *
3462 get_changed_paths(struct got_pathlist_head *paths,
3463 struct got_commit_object *commit, struct got_repository *repo)
3465 const struct got_error *err = NULL;
3466 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3467 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3468 struct got_object_qid *qid;
3470 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3472 struct got_commit_object *pcommit;
3473 err = got_object_open_as_commit(&pcommit, repo,
3478 tree_id1 = got_object_id_dup(
3479 got_object_commit_get_tree_id(pcommit));
3480 if (tree_id1 == NULL) {
3481 got_object_commit_close(pcommit);
3482 return got_error_from_errno("got_object_id_dup");
3484 got_object_commit_close(pcommit);
3489 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3494 tree_id2 = got_object_commit_get_tree_id(commit);
3495 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3499 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3500 got_diff_tree_collect_changed_paths, paths, 0);
3503 got_object_tree_close(tree1);
3505 got_object_tree_close(tree2);
3510 static const struct got_error *
3511 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3515 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3517 return got_error_from_errno("reallocarray");
3519 (*line_offsets)[*nlines] = off;
3524 static const struct got_error *
3525 write_commit_info(off_t **line_offsets, size_t *nlines,
3526 struct got_object_id *commit_id, struct got_reflist_head *refs,
3527 struct got_repository *repo, FILE *outfile)
3529 const struct got_error *err = NULL;
3530 char datebuf[26], *datestr;
3531 struct got_commit_object *commit;
3532 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3533 time_t committer_time;
3534 const char *author, *committer;
3535 char *refs_str = NULL;
3536 struct got_pathlist_head changed_paths;
3537 struct got_pathlist_entry *pe;
3541 TAILQ_INIT(&changed_paths);
3544 err = build_refs_str(&refs_str, refs, commit_id, repo);
3549 err = got_object_open_as_commit(&commit, repo, commit_id);
3553 err = got_object_id_str(&id_str, commit_id);
3555 err = got_error_from_errno("got_object_id_str");
3559 err = add_line_offset(line_offsets, nlines, 0);
3563 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3564 refs_str ? refs_str : "", refs_str ? ")" : "");
3566 err = got_error_from_errno("fprintf");
3570 err = add_line_offset(line_offsets, nlines, outoff);
3574 n = fprintf(outfile, "from: %s\n",
3575 got_object_commit_get_author(commit));
3577 err = got_error_from_errno("fprintf");
3581 err = add_line_offset(line_offsets, nlines, outoff);
3585 committer_time = got_object_commit_get_committer_time(commit);
3586 datestr = get_datestr(&committer_time, datebuf);
3588 n = fprintf(outfile, "date: %s UTC\n", datestr);
3590 err = got_error_from_errno("fprintf");
3594 err = add_line_offset(line_offsets, nlines, outoff);
3598 author = got_object_commit_get_author(commit);
3599 committer = got_object_commit_get_committer(commit);
3600 if (strcmp(author, committer) != 0) {
3601 n = fprintf(outfile, "via: %s\n", committer);
3603 err = got_error_from_errno("fprintf");
3607 err = add_line_offset(line_offsets, nlines, outoff);
3611 if (got_object_commit_get_nparents(commit) > 1) {
3612 const struct got_object_id_queue *parent_ids;
3613 struct got_object_qid *qid;
3615 parent_ids = got_object_commit_get_parent_ids(commit);
3616 STAILQ_FOREACH(qid, parent_ids, entry) {
3617 err = got_object_id_str(&id_str, &qid->id);
3620 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3622 err = got_error_from_errno("fprintf");
3626 err = add_line_offset(line_offsets, nlines, outoff);
3634 err = got_object_commit_get_logmsg(&logmsg, commit);
3638 while ((line = strsep(&s, "\n")) != NULL) {
3639 n = fprintf(outfile, "%s\n", line);
3641 err = got_error_from_errno("fprintf");
3645 err = add_line_offset(line_offsets, nlines, outoff);
3650 err = get_changed_paths(&changed_paths, commit, repo);
3653 TAILQ_FOREACH(pe, &changed_paths, entry) {
3654 struct got_diff_changed_path *cp = pe->data;
3655 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3657 err = got_error_from_errno("fprintf");
3661 err = add_line_offset(line_offsets, nlines, outoff);
3664 free((char *)pe->path);
3668 fputc('\n', outfile);
3670 err = add_line_offset(line_offsets, nlines, outoff);
3672 got_pathlist_free(&changed_paths);
3676 got_object_commit_close(commit);
3678 free(*line_offsets);
3679 *line_offsets = NULL;
3685 static const struct got_error *
3686 create_diff(struct tog_diff_view_state *s)
3688 const struct got_error *err = NULL;
3692 free(s->line_offsets);
3693 s->line_offsets = malloc(sizeof(off_t));
3694 if (s->line_offsets == NULL)
3695 return got_error_from_errno("malloc");
3700 err = got_error_from_errno("got_opentemp");
3703 if (s->f && fclose(s->f) == EOF) {
3704 err = got_error_from_errno("fclose");
3710 err = got_object_get_type(&obj_type, s->repo, s->id1);
3712 err = got_object_get_type(&obj_type, s->repo, s->id2);
3717 case GOT_OBJ_TYPE_BLOB:
3718 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3719 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3720 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3723 case GOT_OBJ_TYPE_TREE:
3724 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3725 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3726 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3728 case GOT_OBJ_TYPE_COMMIT: {
3729 const struct got_object_id_queue *parent_ids;
3730 struct got_object_qid *pid;
3731 struct got_commit_object *commit2;
3732 struct got_reflist_head *refs;
3734 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3737 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3738 /* Show commit info if we're diffing to a parent/root commit. */
3739 if (s->id1 == NULL) {
3740 err = write_commit_info(&s->line_offsets, &s->nlines,
3741 s->id2, refs, s->repo, s->f);
3745 parent_ids = got_object_commit_get_parent_ids(commit2);
3746 STAILQ_FOREACH(pid, parent_ids, entry) {
3747 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3748 err = write_commit_info(
3749 &s->line_offsets, &s->nlines,
3750 s->id2, refs, s->repo, s->f);
3757 got_object_commit_close(commit2);
3759 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3760 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3761 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3765 err = got_error(GOT_ERR_OBJ_TYPE);
3771 if (s->f && fflush(s->f) != 0 && err == NULL)
3772 err = got_error_from_errno("fflush");
3777 diff_view_indicate_progress(struct tog_view *view)
3779 mvwaddstr(view->window, 0, 0, "diffing...");
3784 static const struct got_error *
3785 search_start_diff_view(struct tog_view *view)
3787 struct tog_diff_view_state *s = &view->state.diff;
3789 s->matched_line = 0;
3793 static const struct got_error *
3794 search_next_diff_view(struct tog_view *view)
3796 struct tog_diff_view_state *s = &view->state.diff;
3797 const struct got_error *err = NULL;
3800 size_t linesize = 0;
3803 if (!view->searching) {
3804 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3808 if (s->matched_line) {
3809 if (view->searching == TOG_SEARCH_FORWARD)
3810 lineno = s->matched_line + 1;
3812 lineno = s->matched_line - 1;
3814 lineno = s->first_displayed_line;
3819 if (lineno <= 0 || lineno > s->nlines) {
3820 if (s->matched_line == 0) {
3821 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3825 if (view->searching == TOG_SEARCH_FORWARD)
3831 offset = s->line_offsets[lineno - 1];
3832 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3834 return got_error_from_errno("fseeko");
3836 linelen = getline(&line, &linesize, s->f);
3837 if (linelen != -1) {
3839 err = expand_tab(&exstr, line);
3842 if (match_line(exstr, &view->regex, 1,
3844 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3845 s->matched_line = lineno;
3851 if (view->searching == TOG_SEARCH_FORWARD)
3858 if (s->matched_line) {
3859 s->first_displayed_line = s->matched_line;
3860 s->selected_line = 1;
3866 static const struct got_error *
3867 close_diff_view(struct tog_view *view)
3869 const struct got_error *err = NULL;
3870 struct tog_diff_view_state *s = &view->state.diff;
3876 if (s->f && fclose(s->f) == EOF)
3877 err = got_error_from_errno("fclose");
3879 if (s->f1 && fclose(s->f1) == EOF)
3880 err = got_error_from_errno("fclose");
3882 if (s->f2 && fclose(s->f2) == EOF)
3883 err = got_error_from_errno("fclose");
3885 free_colors(&s->colors);
3886 free(s->line_offsets);
3887 s->line_offsets = NULL;
3892 static const struct got_error *
3893 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3894 struct got_object_id *id2, const char *label1, const char *label2,
3895 int diff_context, int ignore_whitespace, int force_text_diff,
3896 struct tog_view *log_view, struct got_repository *repo)
3898 const struct got_error *err;
3899 struct tog_diff_view_state *s = &view->state.diff;
3901 memset(s, 0, sizeof(*s));
3903 if (id1 != NULL && id2 != NULL) {
3905 err = got_object_get_type(&type1, repo, id1);
3908 err = got_object_get_type(&type2, repo, id2);
3913 return got_error(GOT_ERR_OBJ_TYPE);
3915 s->first_displayed_line = 1;
3916 s->last_displayed_line = view->nlines;
3917 s->selected_line = 1;
3925 s->id1 = got_object_id_dup(id1);
3927 return got_error_from_errno("got_object_id_dup");
3931 s->id2 = got_object_id_dup(id2);
3932 if (s->id2 == NULL) {
3933 err = got_error_from_errno("got_object_id_dup");
3937 s->f1 = got_opentemp();
3938 if (s->f1 == NULL) {
3939 err = got_error_from_errno("got_opentemp");
3943 s->f2 = got_opentemp();
3944 if (s->f2 == NULL) {
3945 err = got_error_from_errno("got_opentemp");
3949 s->first_displayed_line = 1;
3950 s->last_displayed_line = view->nlines;
3951 s->diff_context = diff_context;
3952 s->ignore_whitespace = ignore_whitespace;
3953 s->force_text_diff = force_text_diff;
3954 s->log_view = log_view;
3957 STAILQ_INIT(&s->colors);
3958 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3959 err = add_color(&s->colors,
3960 "^-", TOG_COLOR_DIFF_MINUS,
3961 get_color_value("TOG_COLOR_DIFF_MINUS"));
3964 err = add_color(&s->colors, "^\\+",
3965 TOG_COLOR_DIFF_PLUS,
3966 get_color_value("TOG_COLOR_DIFF_PLUS"));
3969 err = add_color(&s->colors,
3970 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3971 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3975 err = add_color(&s->colors,
3976 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3977 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3978 get_color_value("TOG_COLOR_DIFF_META"));
3982 err = add_color(&s->colors,
3983 "^(from|via): ", TOG_COLOR_AUTHOR,
3984 get_color_value("TOG_COLOR_AUTHOR"));
3988 err = add_color(&s->colors,
3989 "^date: ", TOG_COLOR_DATE,
3990 get_color_value("TOG_COLOR_DATE"));
3995 if (log_view && view_is_splitscreen(view))
3996 show_log_view(log_view); /* draw vborder */
3997 diff_view_indicate_progress(view);
3999 err = create_diff(s);
4001 view->show = show_diff_view;
4002 view->input = input_diff_view;
4003 view->close = close_diff_view;
4004 view->search_start = search_start_diff_view;
4005 view->search_next = search_next_diff_view;
4008 close_diff_view(view);
4012 static const struct got_error *
4013 show_diff_view(struct tog_view *view)
4015 const struct got_error *err;
4016 struct tog_diff_view_state *s = &view->state.diff;
4017 char *id_str1 = NULL, *id_str2, *header;
4018 const char *label1, *label2;
4021 err = got_object_id_str(&id_str1, s->id1);
4024 label1 = s->label1 ? : id_str1;
4026 label1 = "/dev/null";
4028 err = got_object_id_str(&id_str2, s->id2);
4031 label2 = s->label2 ? : id_str2;
4033 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4034 err = got_error_from_errno("asprintf");
4042 err = draw_file(view, header);
4047 static const struct got_error *
4048 set_selected_commit(struct tog_diff_view_state *s,
4049 struct commit_queue_entry *entry)
4051 const struct got_error *err;
4052 const struct got_object_id_queue *parent_ids;
4053 struct got_commit_object *selected_commit;
4054 struct got_object_qid *pid;
4057 s->id2 = got_object_id_dup(entry->id);
4059 return got_error_from_errno("got_object_id_dup");
4061 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4064 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4066 pid = STAILQ_FIRST(parent_ids);
4067 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4068 got_object_commit_close(selected_commit);
4072 static const struct got_error *
4073 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4075 const struct got_error *err = NULL;
4076 struct tog_diff_view_state *s = &view->state.diff;
4077 struct tog_log_view_state *ls;
4078 struct commit_queue_entry *old_selected_entry;
4080 size_t linesize = 0;
4082 int i, nscroll = view->nlines - 1;
4089 view->x = MAX(view->maxx - view->ncols / 3, 0);
4094 if (view->x + view->ncols / 3 < view->maxx)
4095 view->x += 2; /* move two columns right */
4101 view->x -= MIN(view->x, 2); /* move two columns back */
4108 s->force_text_diff = !s->force_text_diff;
4110 s->ignore_whitespace = !s->ignore_whitespace;
4111 wclear(view->window);
4112 s->first_displayed_line = 1;
4113 s->last_displayed_line = view->nlines;
4114 s->matched_line = 0;
4115 diff_view_indicate_progress(view);
4116 err = create_diff(s);
4121 s->first_displayed_line = 1;
4130 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4136 if (s->first_displayed_line > 1)
4137 s->first_displayed_line--;
4148 if (s->first_displayed_line == 1) {
4153 while (i++ < nscroll && s->first_displayed_line > 1)
4154 s->first_displayed_line--;
4160 s->first_displayed_line++;
4177 while (!s->eof && i++ < nscroll) {
4178 linelen = getline(&line, &linesize, s->f);
4179 s->first_displayed_line++;
4180 if (linelen == -1) {
4184 err = got_ferror(s->f, GOT_ERR_IO);
4191 if (s->diff_context > 0) {
4193 s->matched_line = 0;
4194 diff_view_indicate_progress(view);
4195 err = create_diff(s);
4196 if (s->first_displayed_line + view->nlines - 1 >
4198 s->first_displayed_line = 1;
4199 s->last_displayed_line = view->nlines;
4205 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4207 s->matched_line = 0;
4208 diff_view_indicate_progress(view);
4209 err = create_diff(s);
4215 if (s->log_view == NULL) {
4219 ls = &s->log_view->state.log;
4220 old_selected_entry = ls->selected_entry;
4222 /* view->count handled in input_log_view() */
4223 err = input_log_view(NULL, s->log_view, KEY_UP);
4227 if (old_selected_entry == ls->selected_entry)
4230 err = set_selected_commit(s, ls->selected_entry);
4234 s->first_displayed_line = 1;
4235 s->last_displayed_line = view->nlines;
4236 s->matched_line = 0;
4239 diff_view_indicate_progress(view);
4240 err = create_diff(s);
4244 if (s->log_view == NULL) {
4248 ls = &s->log_view->state.log;
4249 old_selected_entry = ls->selected_entry;
4251 /* view->count handled in input_log_view() */
4252 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4256 if (old_selected_entry == ls->selected_entry)
4259 err = set_selected_commit(s, ls->selected_entry);
4263 s->first_displayed_line = 1;
4264 s->last_displayed_line = view->nlines;
4265 s->matched_line = 0;
4268 diff_view_indicate_progress(view);
4269 err = create_diff(s);
4279 static const struct got_error *
4280 cmd_diff(int argc, char *argv[])
4282 const struct got_error *error = NULL;
4283 struct got_repository *repo = NULL;
4284 struct got_worktree *worktree = NULL;
4285 struct got_object_id *id1 = NULL, *id2 = NULL;
4286 char *repo_path = NULL, *cwd = NULL;
4287 char *id_str1 = NULL, *id_str2 = NULL;
4288 char *label1 = NULL, *label2 = NULL;
4289 int diff_context = 3, ignore_whitespace = 0;
4290 int ch, force_text_diff = 0;
4292 struct tog_view *view;
4293 int *pack_fds = NULL;
4295 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4298 force_text_diff = 1;
4301 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4304 errx(1, "number of context lines is %s: %s",
4308 repo_path = realpath(optarg, NULL);
4309 if (repo_path == NULL)
4310 return got_error_from_errno2("realpath",
4312 got_path_strip_trailing_slashes(repo_path);
4315 ignore_whitespace = 1;
4327 usage_diff(); /* TODO show local worktree changes */
4328 } else if (argc == 2) {
4334 error = got_repo_pack_fds_open(&pack_fds);
4338 if (repo_path == NULL) {
4339 cwd = getcwd(NULL, 0);
4341 return got_error_from_errno("getcwd");
4342 error = got_worktree_open(&worktree, cwd);
4343 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4347 strdup(got_worktree_get_repo_path(worktree));
4349 repo_path = strdup(cwd);
4350 if (repo_path == NULL) {
4351 error = got_error_from_errno("strdup");
4356 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4362 error = apply_unveil(got_repo_get_path(repo), NULL);
4366 error = tog_load_refs(repo, 0);
4370 error = got_repo_match_object_id(&id1, &label1, id_str1,
4371 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4375 error = got_repo_match_object_id(&id2, &label2, id_str2,
4376 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4380 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4382 error = got_error_from_errno("view_open");
4385 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4386 ignore_whitespace, force_text_diff, NULL, repo);
4389 error = view_loop(view);
4396 const struct got_error *close_err = got_repo_close(repo);
4401 got_worktree_close(worktree);
4403 const struct got_error *pack_err =
4404 got_repo_pack_fds_close(pack_fds);
4417 "usage: %s blame [-c commit] [-r repository-path] path\n",
4422 struct tog_blame_line {
4424 struct got_object_id *id;
4427 static const struct got_error *
4428 draw_blame(struct tog_view *view)
4430 struct tog_blame_view_state *s = &view->state.blame;
4431 struct tog_blame *blame = &s->blame;
4432 regmatch_t *regmatch = &view->regmatch;
4433 const struct got_error *err;
4434 int lineno = 0, nprinted = 0;
4436 size_t linesize = 0;
4440 struct tog_blame_line *blame_line;
4441 struct got_object_id *prev_id = NULL;
4443 struct tog_color *tc;
4445 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4450 werase(view->window);
4452 if (asprintf(&line, "commit %s", id_str) == -1) {
4453 err = got_error_from_errno("asprintf");
4458 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4463 if (view_needs_focus_indication(view))
4464 wstandout(view->window);
4465 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4467 wattr_on(view->window,
4468 COLOR_PAIR(tc->colorpair), NULL);
4469 waddwstr(view->window, wline);
4471 wattr_off(view->window,
4472 COLOR_PAIR(tc->colorpair), NULL);
4473 if (view_needs_focus_indication(view))
4474 wstandend(view->window);
4477 if (width < view->ncols - 1)
4478 waddch(view->window, '\n');
4480 if (asprintf(&line, "[%d/%d] %s%s",
4481 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4482 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4484 return got_error_from_errno("asprintf");
4487 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4492 waddwstr(view->window, wline);
4495 if (width < view->ncols - 1)
4496 waddch(view->window, '\n');
4500 while (nprinted < view->nlines - 2) {
4501 linelen = getline(&line, &linesize, blame->f);
4502 if (linelen == -1) {
4503 if (feof(blame->f)) {
4508 return got_ferror(blame->f, GOT_ERR_IO);
4510 if (++lineno < s->first_displayed_line)
4513 /* Set view->maxx based on full line length. */
4514 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4521 view->maxx = MAX(view->maxx, width);
4523 if (view->focussed && nprinted == s->selected_line - 1)
4524 wstandout(view->window);
4526 if (blame->nlines > 0) {
4527 blame_line = &blame->lines[lineno - 1];
4528 if (blame_line->annotated && prev_id &&
4529 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4531 nprinted == s->selected_line - 1)) {
4532 waddstr(view->window, " ");
4533 } else if (blame_line->annotated) {
4535 err = got_object_id_str(&id_str,
4541 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4543 wattr_on(view->window,
4544 COLOR_PAIR(tc->colorpair), NULL);
4545 wprintw(view->window, "%.8s", id_str);
4547 wattr_off(view->window,
4548 COLOR_PAIR(tc->colorpair), NULL);
4550 prev_id = blame_line->id;
4552 waddstr(view->window, "........");
4556 waddstr(view->window, "........");
4560 if (view->focussed && nprinted == s->selected_line - 1)
4561 wstandend(view->window);
4562 waddstr(view->window, " ");
4564 if (view->ncols <= 9) {
4566 } else if (s->first_displayed_line + nprinted ==
4568 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4569 err = add_matched_line(&width, line, view->ncols - 9, 9,
4570 view->window, view->x, regmatch);
4578 err = format_line(&wline, &width, &skip, line,
4579 view->x, view->ncols - 9, 9, 1);
4584 waddwstr(view->window, &wline[skip]);
4590 if (width <= view->ncols - 1)
4591 waddch(view->window, '\n');
4592 if (++nprinted == 1)
4593 s->first_displayed_line = lineno;
4596 s->last_displayed_line = lineno;
4603 static const struct got_error *
4604 blame_cb(void *arg, int nlines, int lineno,
4605 struct got_commit_object *commit, struct got_object_id *id)
4607 const struct got_error *err = NULL;
4608 struct tog_blame_cb_args *a = arg;
4609 struct tog_blame_line *line;
4612 if (nlines != a->nlines ||
4613 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4614 return got_error(GOT_ERR_RANGE);
4616 errcode = pthread_mutex_lock(&tog_mutex);
4618 return got_error_set_errno(errcode, "pthread_mutex_lock");
4620 if (*a->quit) { /* user has quit the blame view */
4621 err = got_error(GOT_ERR_ITER_COMPLETED);
4626 goto done; /* no change in this commit */
4628 line = &a->lines[lineno - 1];
4629 if (line->annotated)
4632 line->id = got_object_id_dup(id);
4633 if (line->id == NULL) {
4634 err = got_error_from_errno("got_object_id_dup");
4637 line->annotated = 1;
4639 errcode = pthread_mutex_unlock(&tog_mutex);
4641 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4646 blame_thread(void *arg)
4648 const struct got_error *err, *close_err;
4649 struct tog_blame_thread_args *ta = arg;
4650 struct tog_blame_cb_args *a = ta->cb_args;
4653 err = block_signals_used_by_main_thread();
4657 err = got_blame(ta->path, a->commit_id, ta->repo,
4658 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4659 if (err && err->code == GOT_ERR_CANCELLED)
4662 errcode = pthread_mutex_lock(&tog_mutex);
4664 return (void *)got_error_set_errno(errcode,
4665 "pthread_mutex_lock");
4667 close_err = got_repo_close(ta->repo);
4673 errcode = pthread_mutex_unlock(&tog_mutex);
4674 if (errcode && err == NULL)
4675 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4680 static struct got_object_id *
4681 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4682 int first_displayed_line, int selected_line)
4684 struct tog_blame_line *line;
4689 line = &lines[first_displayed_line - 1 + selected_line - 1];
4690 if (!line->annotated)
4696 static const struct got_error *
4697 stop_blame(struct tog_blame *blame)
4699 const struct got_error *err = NULL;
4702 if (blame->thread) {
4704 errcode = pthread_mutex_unlock(&tog_mutex);
4706 return got_error_set_errno(errcode,
4707 "pthread_mutex_unlock");
4708 errcode = pthread_join(blame->thread, (void **)&err);
4710 return got_error_set_errno(errcode, "pthread_join");
4711 errcode = pthread_mutex_lock(&tog_mutex);
4713 return got_error_set_errno(errcode,
4714 "pthread_mutex_lock");
4715 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4717 blame->thread = NULL;
4719 if (blame->thread_args.repo) {
4720 const struct got_error *close_err;
4721 close_err = got_repo_close(blame->thread_args.repo);
4724 blame->thread_args.repo = NULL;
4727 if (fclose(blame->f) == EOF && err == NULL)
4728 err = got_error_from_errno("fclose");
4732 for (i = 0; i < blame->nlines; i++)
4733 free(blame->lines[i].id);
4735 blame->lines = NULL;
4737 free(blame->cb_args.commit_id);
4738 blame->cb_args.commit_id = NULL;
4739 if (blame->pack_fds) {
4740 const struct got_error *pack_err =
4741 got_repo_pack_fds_close(blame->pack_fds);
4744 blame->pack_fds = NULL;
4749 static const struct got_error *
4750 cancel_blame_view(void *arg)
4752 const struct got_error *err = NULL;
4756 errcode = pthread_mutex_lock(&tog_mutex);
4758 return got_error_set_errno(errcode,
4759 "pthread_mutex_unlock");
4762 err = got_error(GOT_ERR_CANCELLED);
4764 errcode = pthread_mutex_unlock(&tog_mutex);
4766 return got_error_set_errno(errcode,
4767 "pthread_mutex_lock");
4772 static const struct got_error *
4773 run_blame(struct tog_view *view)
4775 struct tog_blame_view_state *s = &view->state.blame;
4776 struct tog_blame *blame = &s->blame;
4777 const struct got_error *err = NULL;
4778 struct got_commit_object *commit = NULL;
4779 struct got_blob_object *blob = NULL;
4780 struct got_repository *thread_repo = NULL;
4781 struct got_object_id *obj_id = NULL;
4783 int *pack_fds = NULL;
4785 err = got_object_open_as_commit(&commit, s->repo,
4786 &s->blamed_commit->id);
4790 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4794 err = got_object_get_type(&obj_type, s->repo, obj_id);
4798 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4799 err = got_error(GOT_ERR_OBJ_TYPE);
4803 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4806 blame->f = got_opentemp();
4807 if (blame->f == NULL) {
4808 err = got_error_from_errno("got_opentemp");
4811 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4812 &blame->line_offsets, blame->f, blob);
4815 if (blame->nlines == 0) {
4816 s->blame_complete = 1;
4820 /* Don't include \n at EOF in the blame line count. */
4821 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4824 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4825 if (blame->lines == NULL) {
4826 err = got_error_from_errno("calloc");
4830 err = got_repo_pack_fds_open(&pack_fds);
4833 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4838 blame->pack_fds = pack_fds;
4839 blame->cb_args.view = view;
4840 blame->cb_args.lines = blame->lines;
4841 blame->cb_args.nlines = blame->nlines;
4842 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4843 if (blame->cb_args.commit_id == NULL) {
4844 err = got_error_from_errno("got_object_id_dup");
4847 blame->cb_args.quit = &s->done;
4849 blame->thread_args.path = s->path;
4850 blame->thread_args.repo = thread_repo;
4851 blame->thread_args.cb_args = &blame->cb_args;
4852 blame->thread_args.complete = &s->blame_complete;
4853 blame->thread_args.cancel_cb = cancel_blame_view;
4854 blame->thread_args.cancel_arg = &s->done;
4855 s->blame_complete = 0;
4857 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4858 s->first_displayed_line = 1;
4859 s->last_displayed_line = view->nlines;
4860 s->selected_line = 1;
4862 s->matched_line = 0;
4866 got_object_commit_close(commit);
4868 got_object_blob_close(blob);
4875 static const struct got_error *
4876 open_blame_view(struct tog_view *view, char *path,
4877 struct got_object_id *commit_id, struct got_repository *repo)
4879 const struct got_error *err = NULL;
4880 struct tog_blame_view_state *s = &view->state.blame;
4882 STAILQ_INIT(&s->blamed_commits);
4884 s->path = strdup(path);
4885 if (s->path == NULL)
4886 return got_error_from_errno("strdup");
4888 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4894 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4895 s->first_displayed_line = 1;
4896 s->last_displayed_line = view->nlines;
4897 s->selected_line = 1;
4898 s->blame_complete = 0;
4900 s->commit_id = commit_id;
4901 memset(&s->blame, 0, sizeof(s->blame));
4903 STAILQ_INIT(&s->colors);
4904 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4905 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4906 get_color_value("TOG_COLOR_COMMIT"));
4911 view->show = show_blame_view;
4912 view->input = input_blame_view;
4913 view->close = close_blame_view;
4914 view->search_start = search_start_blame_view;
4915 view->search_next = search_next_blame_view;
4917 return run_blame(view);
4920 static const struct got_error *
4921 close_blame_view(struct tog_view *view)
4923 const struct got_error *err = NULL;
4924 struct tog_blame_view_state *s = &view->state.blame;
4926 if (s->blame.thread)
4927 err = stop_blame(&s->blame);
4929 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4930 struct got_object_qid *blamed_commit;
4931 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4932 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4933 got_object_qid_free(blamed_commit);
4937 free_colors(&s->colors);
4941 static const struct got_error *
4942 search_start_blame_view(struct tog_view *view)
4944 struct tog_blame_view_state *s = &view->state.blame;
4946 s->matched_line = 0;
4950 static const struct got_error *
4951 search_next_blame_view(struct tog_view *view)
4953 struct tog_blame_view_state *s = &view->state.blame;
4954 const struct got_error *err = NULL;
4957 size_t linesize = 0;
4960 if (!view->searching) {
4961 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4965 if (s->matched_line) {
4966 if (view->searching == TOG_SEARCH_FORWARD)
4967 lineno = s->matched_line + 1;
4969 lineno = s->matched_line - 1;
4971 lineno = s->first_displayed_line - 1 + s->selected_line;
4976 if (lineno <= 0 || lineno > s->blame.nlines) {
4977 if (s->matched_line == 0) {
4978 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4982 if (view->searching == TOG_SEARCH_FORWARD)
4985 lineno = s->blame.nlines;
4988 offset = s->blame.line_offsets[lineno - 1];
4989 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4991 return got_error_from_errno("fseeko");
4993 linelen = getline(&line, &linesize, s->blame.f);
4994 if (linelen != -1) {
4996 err = expand_tab(&exstr, line);
4999 if (match_line(exstr, &view->regex, 1,
5001 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5002 s->matched_line = lineno;
5008 if (view->searching == TOG_SEARCH_FORWARD)
5015 if (s->matched_line) {
5016 s->first_displayed_line = s->matched_line;
5017 s->selected_line = 1;
5023 static const struct got_error *
5024 show_blame_view(struct tog_view *view)
5026 const struct got_error *err = NULL;
5027 struct tog_blame_view_state *s = &view->state.blame;
5030 if (s->blame.thread == NULL && !s->blame_complete) {
5031 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5032 &s->blame.thread_args);
5034 return got_error_set_errno(errcode, "pthread_create");
5036 halfdelay(1); /* fast refresh while annotating */
5039 if (s->blame_complete)
5040 halfdelay(10); /* disable fast refresh */
5042 err = draw_blame(view);
5048 static const struct got_error *
5049 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5051 const struct got_error *err = NULL, *thread_err = NULL;
5052 struct tog_view *diff_view;
5053 struct tog_blame_view_state *s = &view->state.blame;
5054 int begin_x = 0, nscroll = view->nlines - 2;
5061 view->x = MAX(view->maxx - view->ncols / 3, 0);
5066 if (view->x + view->ncols / 3 < view->maxx)
5067 view->x += 2; /* move two columns right */
5073 view->x -= MIN(view->x, 2); /* move two columns back */
5082 s->selected_line = 1;
5083 s->first_displayed_line = 1;
5088 if (s->blame.nlines < view->nlines - 2) {
5089 s->selected_line = s->blame.nlines;
5090 s->first_displayed_line = 1;
5092 s->selected_line = view->nlines - 2;
5093 s->first_displayed_line = s->blame.nlines -
5101 if (s->selected_line > 1)
5103 else if (s->selected_line == 1 &&
5104 s->first_displayed_line > 1)
5105 s->first_displayed_line--;
5116 if (s->first_displayed_line == 1) {
5117 if (view->count > 1)
5119 s->selected_line = MAX(1, s->selected_line - nscroll);
5123 if (s->first_displayed_line > nscroll)
5124 s->first_displayed_line -= nscroll;
5126 s->first_displayed_line = 1;
5131 if (s->selected_line < view->nlines - 2 &&
5132 s->first_displayed_line +
5133 s->selected_line <= s->blame.nlines)
5135 else if (s->last_displayed_line <
5137 s->first_displayed_line++;
5143 struct got_object_id *id = NULL;
5146 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5147 s->first_displayed_line, s->selected_line);
5151 struct got_commit_object *commit, *pcommit;
5152 struct got_object_qid *pid;
5153 struct got_object_id *blob_id = NULL;
5155 err = got_object_open_as_commit(&commit,
5160 got_object_commit_get_parent_ids(commit));
5162 got_object_commit_close(commit);
5165 /* Check if path history ends here. */
5166 err = got_object_open_as_commit(&pcommit,
5170 err = got_object_id_by_path(&blob_id, s->repo,
5172 got_object_commit_close(pcommit);
5174 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5176 got_object_commit_close(commit);
5179 err = got_object_get_type(&obj_type, s->repo,
5182 /* Can't blame non-blob type objects. */
5183 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5184 got_object_commit_close(commit);
5187 err = got_object_qid_alloc(&s->blamed_commit,
5189 got_object_commit_close(commit);
5191 if (got_object_id_cmp(id,
5192 &s->blamed_commit->id) == 0)
5194 err = got_object_qid_alloc(&s->blamed_commit,
5200 thread_err = stop_blame(&s->blame);
5204 STAILQ_INSERT_HEAD(&s->blamed_commits,
5205 s->blamed_commit, entry);
5206 err = run_blame(view);
5212 struct got_object_qid *first;
5215 first = STAILQ_FIRST(&s->blamed_commits);
5216 if (!got_object_id_cmp(&first->id, s->commit_id))
5219 thread_err = stop_blame(&s->blame);
5223 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5224 got_object_qid_free(s->blamed_commit);
5226 STAILQ_FIRST(&s->blamed_commits);
5227 err = run_blame(view);
5234 struct got_object_id *id = NULL;
5235 struct got_object_qid *pid;
5236 struct got_commit_object *commit = NULL;
5239 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5240 s->first_displayed_line, s->selected_line);
5243 err = got_object_open_as_commit(&commit, s->repo, id);
5247 got_object_commit_get_parent_ids(commit));
5248 if (view_is_parent_view(view))
5249 begin_x = view_split_begin_x(view->begin_x);
5250 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5251 if (diff_view == NULL) {
5252 got_object_commit_close(commit);
5253 err = got_error_from_errno("view_open");
5256 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5257 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5258 got_object_commit_close(commit);
5260 view_close(diff_view);
5264 diff_view->focussed = 1;
5265 if (view_is_parent_view(view)) {
5266 err = view_close_child(view);
5269 err = view_set_child(view, diff_view);
5272 view->focus_child = 1;
5274 *new_view = diff_view;
5287 if (s->last_displayed_line >= s->blame.nlines &&
5288 s->selected_line >= MIN(s->blame.nlines,
5289 view->nlines - 2)) {
5293 if (s->last_displayed_line >= s->blame.nlines &&
5294 s->selected_line < view->nlines - 2) {
5296 MIN(nscroll, s->last_displayed_line -
5297 s->first_displayed_line - s->selected_line + 1);
5299 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5300 s->first_displayed_line += nscroll;
5302 s->first_displayed_line =
5303 s->blame.nlines - (view->nlines - 3);
5306 if (s->selected_line > view->nlines - 2) {
5307 s->selected_line = MIN(s->blame.nlines,
5315 return thread_err ? thread_err : err;
5318 static const struct got_error *
5319 cmd_blame(int argc, char *argv[])
5321 const struct got_error *error;
5322 struct got_repository *repo = NULL;
5323 struct got_worktree *worktree = NULL;
5324 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5325 char *link_target = NULL;
5326 struct got_object_id *commit_id = NULL;
5327 struct got_commit_object *commit = NULL;
5328 char *commit_id_str = NULL;
5330 struct tog_view *view;
5331 int *pack_fds = NULL;
5333 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5336 commit_id_str = optarg;
5339 repo_path = realpath(optarg, NULL);
5340 if (repo_path == NULL)
5341 return got_error_from_errno2("realpath",
5356 error = got_repo_pack_fds_open(&pack_fds);
5360 if (repo_path == NULL) {
5361 cwd = getcwd(NULL, 0);
5363 return got_error_from_errno("getcwd");
5364 error = got_worktree_open(&worktree, cwd);
5365 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5369 strdup(got_worktree_get_repo_path(worktree));
5371 repo_path = strdup(cwd);
5372 if (repo_path == NULL) {
5373 error = got_error_from_errno("strdup");
5378 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5382 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5389 error = apply_unveil(got_repo_get_path(repo), NULL);
5393 error = tog_load_refs(repo, 0);
5397 if (commit_id_str == NULL) {
5398 struct got_reference *head_ref;
5399 error = got_ref_open(&head_ref, repo, worktree ?
5400 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5403 error = got_ref_resolve(&commit_id, repo, head_ref);
5404 got_ref_close(head_ref);
5406 error = got_repo_match_object_id(&commit_id, NULL,
5407 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5412 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5414 error = got_error_from_errno("view_open");
5418 error = got_object_open_as_commit(&commit, repo, commit_id);
5422 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5427 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5432 /* Release work tree lock. */
5433 got_worktree_close(worktree);
5436 error = view_loop(view);
5444 got_object_commit_close(commit);
5446 got_worktree_close(worktree);
5448 const struct got_error *close_err = got_repo_close(repo);
5453 const struct got_error *pack_err =
5454 got_repo_pack_fds_close(pack_fds);
5462 static const struct got_error *
5463 draw_tree_entries(struct tog_view *view, const char *parent_path)
5465 struct tog_tree_view_state *s = &view->state.tree;
5466 const struct got_error *err = NULL;
5467 struct got_tree_entry *te;
5469 struct tog_color *tc;
5470 int width, n, i, nentries;
5471 int limit = view->nlines;
5475 werase(view->window);
5480 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5484 if (view_needs_focus_indication(view))
5485 wstandout(view->window);
5486 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5488 wattr_on(view->window,
5489 COLOR_PAIR(tc->colorpair), NULL);
5490 waddwstr(view->window, wline);
5492 wattr_off(view->window,
5493 COLOR_PAIR(tc->colorpair), NULL);
5494 if (view_needs_focus_indication(view))
5495 wstandend(view->window);
5498 if (width < view->ncols - 1)
5499 waddch(view->window, '\n');
5502 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5506 waddwstr(view->window, wline);
5509 if (width < view->ncols - 1)
5510 waddch(view->window, '\n');
5513 waddch(view->window, '\n');
5517 if (s->first_displayed_entry == NULL) {
5518 te = got_object_tree_get_first_entry(s->tree);
5519 if (s->selected == 0) {
5521 wstandout(view->window);
5522 s->selected_entry = NULL;
5524 waddstr(view->window, " ..\n"); /* parent directory */
5525 if (s->selected == 0 && view->focussed)
5526 wstandend(view->window);
5533 te = s->first_displayed_entry;
5536 nentries = got_object_tree_get_nentries(s->tree);
5537 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5538 char *line = NULL, *id_str = NULL, *link_target = NULL;
5539 const char *modestr = "";
5542 te = got_object_tree_get_entry(s->tree, i);
5543 mode = got_tree_entry_get_mode(te);
5546 err = got_object_id_str(&id_str,
5547 got_tree_entry_get_id(te));
5549 return got_error_from_errno(
5550 "got_object_id_str");
5552 if (got_object_tree_entry_is_submodule(te))
5554 else if (S_ISLNK(mode)) {
5557 err = got_tree_entry_get_symlink_target(&link_target,
5563 for (i = 0; i < strlen(link_target); i++) {
5564 if (!isprint((unsigned char)link_target[i]))
5565 link_target[i] = '?';
5569 else if (S_ISDIR(mode))
5571 else if (mode & S_IXUSR)
5573 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5574 got_tree_entry_get_name(te), modestr,
5575 link_target ? " -> ": "",
5576 link_target ? link_target : "") == -1) {
5579 return got_error_from_errno("asprintf");
5583 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5589 if (n == s->selected) {
5591 wstandout(view->window);
5592 s->selected_entry = te;
5594 tc = match_color(&s->colors, line);
5596 wattr_on(view->window,
5597 COLOR_PAIR(tc->colorpair), NULL);
5598 waddwstr(view->window, wline);
5600 wattr_off(view->window,
5601 COLOR_PAIR(tc->colorpair), NULL);
5602 if (width < view->ncols - 1)
5603 waddch(view->window, '\n');
5604 if (n == s->selected && view->focussed)
5605 wstandend(view->window);
5611 s->last_displayed_entry = te;
5620 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5622 struct got_tree_entry *te;
5623 int isroot = s->tree == s->root;
5626 if (s->first_displayed_entry == NULL)
5629 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5630 while (i++ < maxscroll) {
5633 s->first_displayed_entry = NULL;
5636 s->first_displayed_entry = te;
5637 te = got_tree_entry_get_prev(s->tree, te);
5642 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5644 struct got_tree_entry *next, *last;
5647 if (s->first_displayed_entry)
5648 next = got_tree_entry_get_next(s->tree,
5649 s->first_displayed_entry);
5651 next = got_object_tree_get_first_entry(s->tree);
5653 last = s->last_displayed_entry;
5654 while (next && last && n++ < maxscroll) {
5655 last = got_tree_entry_get_next(s->tree, last);
5657 s->first_displayed_entry = next;
5658 next = got_tree_entry_get_next(s->tree, next);
5663 static const struct got_error *
5664 tree_entry_path(char **path, struct tog_parent_trees *parents,
5665 struct got_tree_entry *te)
5667 const struct got_error *err = NULL;
5668 struct tog_parent_tree *pt;
5669 size_t len = 2; /* for leading slash and NUL */
5671 TAILQ_FOREACH(pt, parents, entry)
5672 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5675 len += strlen(got_tree_entry_get_name(te));
5677 *path = calloc(1, len);
5679 return got_error_from_errno("calloc");
5682 pt = TAILQ_LAST(parents, tog_parent_trees);
5684 const char *name = got_tree_entry_get_name(pt->selected_entry);
5685 if (strlcat(*path, name, len) >= len) {
5686 err = got_error(GOT_ERR_NO_SPACE);
5689 if (strlcat(*path, "/", len) >= len) {
5690 err = got_error(GOT_ERR_NO_SPACE);
5693 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5696 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5697 err = got_error(GOT_ERR_NO_SPACE);
5709 static const struct got_error *
5710 blame_tree_entry(struct tog_view **new_view, int begin_x,
5711 struct got_tree_entry *te, struct tog_parent_trees *parents,
5712 struct got_object_id *commit_id, struct got_repository *repo)
5714 const struct got_error *err = NULL;
5716 struct tog_view *blame_view;
5720 err = tree_entry_path(&path, parents, te);
5724 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5725 if (blame_view == NULL) {
5726 err = got_error_from_errno("view_open");
5730 err = open_blame_view(blame_view, path, commit_id, repo);
5732 if (err->code == GOT_ERR_CANCELLED)
5734 view_close(blame_view);
5736 *new_view = blame_view;
5742 static const struct got_error *
5743 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5744 struct tog_tree_view_state *s)
5746 struct tog_view *log_view;
5747 const struct got_error *err = NULL;
5752 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5753 if (log_view == NULL)
5754 return got_error_from_errno("view_open");
5756 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5760 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5763 view_close(log_view);
5765 *new_view = log_view;
5770 static const struct got_error *
5771 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5772 const char *head_ref_name, struct got_repository *repo)
5774 const struct got_error *err = NULL;
5775 char *commit_id_str = NULL;
5776 struct tog_tree_view_state *s = &view->state.tree;
5777 struct got_commit_object *commit = NULL;
5779 TAILQ_INIT(&s->parents);
5780 STAILQ_INIT(&s->colors);
5782 s->commit_id = got_object_id_dup(commit_id);
5783 if (s->commit_id == NULL)
5784 return got_error_from_errno("got_object_id_dup");
5786 err = got_object_open_as_commit(&commit, repo, commit_id);
5791 * The root is opened here and will be closed when the view is closed.
5792 * Any visited subtrees and their path-wise parents are opened and
5795 err = got_object_open_as_tree(&s->root, repo,
5796 got_object_commit_get_tree_id(commit));
5801 err = got_object_id_str(&commit_id_str, commit_id);
5805 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5806 err = got_error_from_errno("asprintf");
5810 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5811 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5812 if (head_ref_name) {
5813 s->head_ref_name = strdup(head_ref_name);
5814 if (s->head_ref_name == NULL) {
5815 err = got_error_from_errno("strdup");
5821 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5822 err = add_color(&s->colors, "\\$$",
5823 TOG_COLOR_TREE_SUBMODULE,
5824 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5827 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5828 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5831 err = add_color(&s->colors, "/$",
5832 TOG_COLOR_TREE_DIRECTORY,
5833 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5837 err = add_color(&s->colors, "\\*$",
5838 TOG_COLOR_TREE_EXECUTABLE,
5839 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5843 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5844 get_color_value("TOG_COLOR_COMMIT"));
5849 view->show = show_tree_view;
5850 view->input = input_tree_view;
5851 view->close = close_tree_view;
5852 view->search_start = search_start_tree_view;
5853 view->search_next = search_next_tree_view;
5855 free(commit_id_str);
5857 got_object_commit_close(commit);
5859 close_tree_view(view);
5863 static const struct got_error *
5864 close_tree_view(struct tog_view *view)
5866 struct tog_tree_view_state *s = &view->state.tree;
5868 free_colors(&s->colors);
5869 free(s->tree_label);
5870 s->tree_label = NULL;
5872 s->commit_id = NULL;
5873 free(s->head_ref_name);
5874 s->head_ref_name = NULL;
5875 while (!TAILQ_EMPTY(&s->parents)) {
5876 struct tog_parent_tree *parent;
5877 parent = TAILQ_FIRST(&s->parents);
5878 TAILQ_REMOVE(&s->parents, parent, entry);
5879 if (parent->tree != s->root)
5880 got_object_tree_close(parent->tree);
5884 if (s->tree != NULL && s->tree != s->root)
5885 got_object_tree_close(s->tree);
5887 got_object_tree_close(s->root);
5891 static const struct got_error *
5892 search_start_tree_view(struct tog_view *view)
5894 struct tog_tree_view_state *s = &view->state.tree;
5896 s->matched_entry = NULL;
5901 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5903 regmatch_t regmatch;
5905 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5909 static const struct got_error *
5910 search_next_tree_view(struct tog_view *view)
5912 struct tog_tree_view_state *s = &view->state.tree;
5913 struct got_tree_entry *te = NULL;
5915 if (!view->searching) {
5916 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5920 if (s->matched_entry) {
5921 if (view->searching == TOG_SEARCH_FORWARD) {
5922 if (s->selected_entry)
5923 te = got_tree_entry_get_next(s->tree,
5926 te = got_object_tree_get_first_entry(s->tree);
5928 if (s->selected_entry == NULL)
5929 te = got_object_tree_get_last_entry(s->tree);
5931 te = got_tree_entry_get_prev(s->tree,
5935 if (s->selected_entry)
5936 te = s->selected_entry;
5937 else if (view->searching == TOG_SEARCH_FORWARD)
5938 te = got_object_tree_get_first_entry(s->tree);
5940 te = got_object_tree_get_last_entry(s->tree);
5945 if (s->matched_entry == NULL) {
5946 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5949 if (view->searching == TOG_SEARCH_FORWARD)
5950 te = got_object_tree_get_first_entry(s->tree);
5952 te = got_object_tree_get_last_entry(s->tree);
5955 if (match_tree_entry(te, &view->regex)) {
5956 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5957 s->matched_entry = te;
5961 if (view->searching == TOG_SEARCH_FORWARD)
5962 te = got_tree_entry_get_next(s->tree, te);
5964 te = got_tree_entry_get_prev(s->tree, te);
5967 if (s->matched_entry) {
5968 s->first_displayed_entry = s->matched_entry;
5975 static const struct got_error *
5976 show_tree_view(struct tog_view *view)
5978 const struct got_error *err = NULL;
5979 struct tog_tree_view_state *s = &view->state.tree;
5982 err = tree_entry_path(&parent_path, &s->parents, NULL);
5986 err = draw_tree_entries(view, parent_path);
5993 static const struct got_error *
5994 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5996 const struct got_error *err = NULL;
5997 struct tog_tree_view_state *s = &view->state.tree;
5998 struct tog_view *log_view, *ref_view;
5999 struct got_tree_entry *te;
6000 int begin_x = 0, n, nscroll = view->nlines - 3;
6004 s->show_ids = !s->show_ids;
6009 if (!s->selected_entry)
6011 if (view_is_parent_view(view))
6012 begin_x = view_split_begin_x(view->begin_x);
6013 err = log_selected_tree_entry(&log_view, begin_x, s);
6015 log_view->focussed = 1;
6016 if (view_is_parent_view(view)) {
6017 err = view_close_child(view);
6020 err = view_set_child(view, log_view);
6023 view->focus_child = 1;
6025 *new_view = log_view;
6029 if (view_is_parent_view(view))
6030 begin_x = view_split_begin_x(view->begin_x);
6031 ref_view = view_open(view->nlines, view->ncols,
6032 view->begin_y, begin_x, TOG_VIEW_REF);
6033 if (ref_view == NULL)
6034 return got_error_from_errno("view_open");
6035 err = open_ref_view(ref_view, s->repo);
6037 view_close(ref_view);
6041 ref_view->focussed = 1;
6042 if (view_is_parent_view(view)) {
6043 err = view_close_child(view);
6046 err = view_set_child(view, ref_view);
6049 view->focus_child = 1;
6051 *new_view = ref_view;
6057 if (s->tree == s->root)
6058 s->first_displayed_entry =
6059 got_object_tree_get_first_entry(s->tree);
6061 s->first_displayed_entry = NULL;
6067 te = got_object_tree_get_last_entry(s->tree);
6068 for (n = 0; n < view->nlines - 3; n++) {
6070 if(s->tree != s->root) {
6071 s->first_displayed_entry = NULL;
6076 s->first_displayed_entry = te;
6077 te = got_tree_entry_get_prev(s->tree, te);
6080 s->selected = n - 1;
6085 if (s->selected > 0) {
6089 tree_scroll_up(s, 1);
6090 if (s->selected_entry == NULL ||
6091 (s->tree == s->root && s->selected_entry ==
6092 got_object_tree_get_first_entry(s->tree)))
6102 if (s->tree == s->root) {
6103 if (got_object_tree_get_first_entry(s->tree) ==
6104 s->first_displayed_entry)
6105 s->selected -= MIN(s->selected, nscroll);
6107 if (s->first_displayed_entry == NULL)
6108 s->selected -= MIN(s->selected, nscroll);
6110 tree_scroll_up(s, MAX(0, nscroll));
6111 if (s->selected_entry == NULL ||
6112 (s->tree == s->root && s->selected_entry ==
6113 got_object_tree_get_first_entry(s->tree)))
6119 if (s->selected < s->ndisplayed - 1) {
6123 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6125 /* can't scroll any further */
6129 tree_scroll_down(s, 1);
6139 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6141 /* can't scroll any further; move cursor down */
6142 if (s->selected < s->ndisplayed - 1)
6143 s->selected += MIN(nscroll,
6144 s->ndisplayed - s->selected - 1);
6149 tree_scroll_down(s, nscroll);
6154 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6155 struct tog_parent_tree *parent;
6156 /* user selected '..' */
6157 if (s->tree == s->root) {
6161 parent = TAILQ_FIRST(&s->parents);
6162 TAILQ_REMOVE(&s->parents, parent,
6164 got_object_tree_close(s->tree);
6165 s->tree = parent->tree;
6166 s->first_displayed_entry =
6167 parent->first_displayed_entry;
6169 parent->selected_entry;
6170 s->selected = parent->selected;
6172 } else if (S_ISDIR(got_tree_entry_get_mode(
6173 s->selected_entry))) {
6174 struct got_tree_object *subtree;
6176 err = got_object_open_as_tree(&subtree, s->repo,
6177 got_tree_entry_get_id(s->selected_entry));
6180 err = tree_view_visit_subtree(s, subtree);
6182 got_object_tree_close(subtree);
6185 } else if (S_ISREG(got_tree_entry_get_mode(
6186 s->selected_entry))) {
6187 struct tog_view *blame_view;
6188 int begin_x = view_is_parent_view(view) ?
6189 view_split_begin_x(view->begin_x) : 0;
6191 err = blame_tree_entry(&blame_view, begin_x,
6192 s->selected_entry, &s->parents,
6193 s->commit_id, s->repo);
6198 blame_view->focussed = 1;
6199 if (view_is_parent_view(view)) {
6200 err = view_close_child(view);
6203 err = view_set_child(view, blame_view);
6206 view->focus_child = 1;
6208 *new_view = blame_view;
6212 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6213 s->selected = view->nlines - 4;
6229 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6234 static const struct got_error *
6235 cmd_tree(int argc, char *argv[])
6237 const struct got_error *error;
6238 struct got_repository *repo = NULL;
6239 struct got_worktree *worktree = NULL;
6240 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6241 struct got_object_id *commit_id = NULL;
6242 struct got_commit_object *commit = NULL;
6243 const char *commit_id_arg = NULL;
6245 struct got_reference *ref = NULL;
6246 const char *head_ref_name = NULL;
6248 struct tog_view *view;
6249 int *pack_fds = NULL;
6251 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6254 commit_id_arg = optarg;
6257 repo_path = realpath(optarg, NULL);
6258 if (repo_path == NULL)
6259 return got_error_from_errno2("realpath",
6274 error = got_repo_pack_fds_open(&pack_fds);
6278 if (repo_path == NULL) {
6279 cwd = getcwd(NULL, 0);
6281 return got_error_from_errno("getcwd");
6282 error = got_worktree_open(&worktree, cwd);
6283 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6287 strdup(got_worktree_get_repo_path(worktree));
6289 repo_path = strdup(cwd);
6290 if (repo_path == NULL) {
6291 error = got_error_from_errno("strdup");
6296 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6300 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6307 error = apply_unveil(got_repo_get_path(repo), NULL);
6311 error = tog_load_refs(repo, 0);
6315 if (commit_id_arg == NULL) {
6316 error = got_repo_match_object_id(&commit_id, &label,
6317 worktree ? got_worktree_get_head_ref_name(worktree) :
6318 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6321 head_ref_name = label;
6323 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6325 head_ref_name = got_ref_get_name(ref);
6326 else if (error->code != GOT_ERR_NOT_REF)
6328 error = got_repo_match_object_id(&commit_id, NULL,
6329 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6334 error = got_object_open_as_commit(&commit, repo, commit_id);
6338 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6340 error = got_error_from_errno("view_open");
6343 error = open_tree_view(view, commit_id, head_ref_name, repo);
6346 if (!got_path_is_root_dir(in_repo_path)) {
6347 error = tree_view_walk_path(&view->state.tree, commit,
6354 /* Release work tree lock. */
6355 got_worktree_close(worktree);
6358 error = view_loop(view);
6367 const struct got_error *close_err = got_repo_close(repo);
6372 const struct got_error *pack_err =
6373 got_repo_pack_fds_close(pack_fds);
6381 static const struct got_error *
6382 ref_view_load_refs(struct tog_ref_view_state *s)
6384 struct got_reflist_entry *sre;
6385 struct tog_reflist_entry *re;
6388 TAILQ_FOREACH(sre, &tog_refs, entry) {
6389 if (strncmp(got_ref_get_name(sre->ref),
6390 "refs/got/", 9) == 0 &&
6391 strncmp(got_ref_get_name(sre->ref),
6392 "refs/got/backup/", 16) != 0)
6395 re = malloc(sizeof(*re));
6397 return got_error_from_errno("malloc");
6399 re->ref = got_ref_dup(sre->ref);
6400 if (re->ref == NULL)
6401 return got_error_from_errno("got_ref_dup");
6402 re->idx = s->nrefs++;
6403 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6406 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6411 ref_view_free_refs(struct tog_ref_view_state *s)
6413 struct tog_reflist_entry *re;
6415 while (!TAILQ_EMPTY(&s->refs)) {
6416 re = TAILQ_FIRST(&s->refs);
6417 TAILQ_REMOVE(&s->refs, re, entry);
6418 got_ref_close(re->ref);
6423 static const struct got_error *
6424 open_ref_view(struct tog_view *view, struct got_repository *repo)
6426 const struct got_error *err = NULL;
6427 struct tog_ref_view_state *s = &view->state.ref;
6429 s->selected_entry = 0;
6432 TAILQ_INIT(&s->refs);
6433 STAILQ_INIT(&s->colors);
6435 err = ref_view_load_refs(s);
6439 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6440 err = add_color(&s->colors, "^refs/heads/",
6441 TOG_COLOR_REFS_HEADS,
6442 get_color_value("TOG_COLOR_REFS_HEADS"));
6446 err = add_color(&s->colors, "^refs/tags/",
6447 TOG_COLOR_REFS_TAGS,
6448 get_color_value("TOG_COLOR_REFS_TAGS"));
6452 err = add_color(&s->colors, "^refs/remotes/",
6453 TOG_COLOR_REFS_REMOTES,
6454 get_color_value("TOG_COLOR_REFS_REMOTES"));
6458 err = add_color(&s->colors, "^refs/got/backup/",
6459 TOG_COLOR_REFS_BACKUP,
6460 get_color_value("TOG_COLOR_REFS_BACKUP"));
6465 view->show = show_ref_view;
6466 view->input = input_ref_view;
6467 view->close = close_ref_view;
6468 view->search_start = search_start_ref_view;
6469 view->search_next = search_next_ref_view;
6472 free_colors(&s->colors);
6476 static const struct got_error *
6477 close_ref_view(struct tog_view *view)
6479 struct tog_ref_view_state *s = &view->state.ref;
6481 ref_view_free_refs(s);
6482 free_colors(&s->colors);
6487 static const struct got_error *
6488 resolve_reflist_entry(struct got_object_id **commit_id,
6489 struct tog_reflist_entry *re, struct got_repository *repo)
6491 const struct got_error *err = NULL;
6492 struct got_object_id *obj_id;
6493 struct got_tag_object *tag = NULL;
6498 err = got_ref_resolve(&obj_id, repo, re->ref);
6502 err = got_object_get_type(&obj_type, repo, obj_id);
6507 case GOT_OBJ_TYPE_COMMIT:
6508 *commit_id = obj_id;
6510 case GOT_OBJ_TYPE_TAG:
6511 err = got_object_open_as_tag(&tag, repo, obj_id);
6515 err = got_object_get_type(&obj_type, repo,
6516 got_object_tag_get_object_id(tag));
6519 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6520 err = got_error(GOT_ERR_OBJ_TYPE);
6523 *commit_id = got_object_id_dup(
6524 got_object_tag_get_object_id(tag));
6525 if (*commit_id == NULL) {
6526 err = got_error_from_errno("got_object_id_dup");
6531 err = got_error(GOT_ERR_OBJ_TYPE);
6537 got_object_tag_close(tag);
6545 static const struct got_error *
6546 log_ref_entry(struct tog_view **new_view, int begin_x,
6547 struct tog_reflist_entry *re, struct got_repository *repo)
6549 struct tog_view *log_view;
6550 const struct got_error *err = NULL;
6551 struct got_object_id *commit_id = NULL;
6555 err = resolve_reflist_entry(&commit_id, re, repo);
6557 if (err->code != GOT_ERR_OBJ_TYPE)
6563 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6564 if (log_view == NULL) {
6565 err = got_error_from_errno("view_open");
6569 err = open_log_view(log_view, commit_id, repo,
6570 got_ref_get_name(re->ref), "", 0);
6573 view_close(log_view);
6575 *new_view = log_view;
6581 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6583 struct tog_reflist_entry *re;
6586 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6589 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6590 while (i++ < maxscroll) {
6593 s->first_displayed_entry = re;
6594 re = TAILQ_PREV(re, tog_reflist_head, entry);
6599 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6601 struct tog_reflist_entry *next, *last;
6604 if (s->first_displayed_entry)
6605 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6607 next = TAILQ_FIRST(&s->refs);
6609 last = s->last_displayed_entry;
6610 while (next && last && n++ < maxscroll) {
6611 last = TAILQ_NEXT(last, entry);
6613 s->first_displayed_entry = next;
6614 next = TAILQ_NEXT(next, entry);
6619 static const struct got_error *
6620 search_start_ref_view(struct tog_view *view)
6622 struct tog_ref_view_state *s = &view->state.ref;
6624 s->matched_entry = NULL;
6629 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6631 regmatch_t regmatch;
6633 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6637 static const struct got_error *
6638 search_next_ref_view(struct tog_view *view)
6640 struct tog_ref_view_state *s = &view->state.ref;
6641 struct tog_reflist_entry *re = NULL;
6643 if (!view->searching) {
6644 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6648 if (s->matched_entry) {
6649 if (view->searching == TOG_SEARCH_FORWARD) {
6650 if (s->selected_entry)
6651 re = TAILQ_NEXT(s->selected_entry, entry);
6653 re = TAILQ_PREV(s->selected_entry,
6654 tog_reflist_head, entry);
6656 if (s->selected_entry == NULL)
6657 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6659 re = TAILQ_PREV(s->selected_entry,
6660 tog_reflist_head, entry);
6663 if (s->selected_entry)
6664 re = s->selected_entry;
6665 else if (view->searching == TOG_SEARCH_FORWARD)
6666 re = TAILQ_FIRST(&s->refs);
6668 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6673 if (s->matched_entry == NULL) {
6674 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6677 if (view->searching == TOG_SEARCH_FORWARD)
6678 re = TAILQ_FIRST(&s->refs);
6680 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6683 if (match_reflist_entry(re, &view->regex)) {
6684 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6685 s->matched_entry = re;
6689 if (view->searching == TOG_SEARCH_FORWARD)
6690 re = TAILQ_NEXT(re, entry);
6692 re = TAILQ_PREV(re, tog_reflist_head, entry);
6695 if (s->matched_entry) {
6696 s->first_displayed_entry = s->matched_entry;
6703 static const struct got_error *
6704 show_ref_view(struct tog_view *view)
6706 const struct got_error *err = NULL;
6707 struct tog_ref_view_state *s = &view->state.ref;
6708 struct tog_reflist_entry *re;
6711 struct tog_color *tc;
6713 int limit = view->nlines;
6715 werase(view->window);
6722 re = s->first_displayed_entry;
6724 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6726 return got_error_from_errno("asprintf");
6728 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6733 if (view_needs_focus_indication(view))
6734 wstandout(view->window);
6735 waddwstr(view->window, wline);
6736 if (view_needs_focus_indication(view))
6737 wstandend(view->window);
6742 if (width < view->ncols - 1)
6743 waddch(view->window, '\n');
6748 while (re && limit > 0) {
6750 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6753 struct got_commit_object *ci;
6754 struct got_tag_object *tag;
6755 struct got_object_id *id;
6759 err = got_ref_resolve(&id, s->repo, re->ref);
6762 err = got_object_open_as_tag(&tag, s->repo, id);
6764 if (err->code != GOT_ERR_OBJ_TYPE) {
6768 err = got_object_open_as_commit(&ci, s->repo,
6774 t = got_object_commit_get_committer_time(ci);
6775 got_object_commit_close(ci);
6777 t = got_object_tag_get_tagger_time(tag);
6778 got_object_tag_close(tag);
6781 if (gmtime_r(&t, &tm) == NULL)
6782 return got_error_from_errno("gmtime_r");
6783 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6784 return got_error(GOT_ERR_NO_SPACE);
6786 if (got_ref_is_symbolic(re->ref)) {
6787 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6788 ymd : "", got_ref_get_name(re->ref),
6789 got_ref_get_symref_target(re->ref)) == -1)
6790 return got_error_from_errno("asprintf");
6791 } else if (s->show_ids) {
6792 struct got_object_id *id;
6794 err = got_ref_resolve(&id, s->repo, re->ref);
6797 err = got_object_id_str(&id_str, id);
6802 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6803 got_ref_get_name(re->ref), id_str) == -1) {
6804 err = got_error_from_errno("asprintf");
6811 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6812 got_ref_get_name(re->ref)) == -1)
6813 return got_error_from_errno("asprintf");
6815 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6821 if (n == s->selected) {
6823 wstandout(view->window);
6824 s->selected_entry = re;
6826 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6828 wattr_on(view->window,
6829 COLOR_PAIR(tc->colorpair), NULL);
6830 waddwstr(view->window, wline);
6832 wattr_off(view->window,
6833 COLOR_PAIR(tc->colorpair), NULL);
6834 if (width < view->ncols - 1)
6835 waddch(view->window, '\n');
6836 if (n == s->selected && view->focussed)
6837 wstandend(view->window);
6843 s->last_displayed_entry = re;
6846 re = TAILQ_NEXT(re, entry);
6853 static const struct got_error *
6854 browse_ref_tree(struct tog_view **new_view, int begin_x,
6855 struct tog_reflist_entry *re, struct got_repository *repo)
6857 const struct got_error *err = NULL;
6858 struct got_object_id *commit_id = NULL;
6859 struct tog_view *tree_view;
6863 err = resolve_reflist_entry(&commit_id, re, repo);
6865 if (err->code != GOT_ERR_OBJ_TYPE)
6872 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6873 if (tree_view == NULL) {
6874 err = got_error_from_errno("view_open");
6878 err = open_tree_view(tree_view, commit_id,
6879 got_ref_get_name(re->ref), repo);
6883 *new_view = tree_view;
6888 static const struct got_error *
6889 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6891 const struct got_error *err = NULL;
6892 struct tog_ref_view_state *s = &view->state.ref;
6893 struct tog_view *log_view, *tree_view;
6894 struct tog_reflist_entry *re;
6895 int begin_x = 0, n, nscroll = view->nlines - 1;
6899 s->show_ids = !s->show_ids;
6903 s->show_date = !s->show_date;
6907 s->sort_by_date = !s->sort_by_date;
6909 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6910 got_ref_cmp_by_commit_timestamp_descending :
6911 tog_ref_cmp_by_name, s->repo);
6914 got_reflist_object_id_map_free(tog_refs_idmap);
6915 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6916 &tog_refs, s->repo);
6919 ref_view_free_refs(s);
6920 err = ref_view_load_refs(s);
6925 if (!s->selected_entry)
6927 if (view_is_parent_view(view))
6928 begin_x = view_split_begin_x(view->begin_x);
6929 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6932 log_view->focussed = 1;
6933 if (view_is_parent_view(view)) {
6934 err = view_close_child(view);
6937 err = view_set_child(view, log_view);
6940 view->focus_child = 1;
6942 *new_view = log_view;
6946 if (!s->selected_entry)
6948 if (view_is_parent_view(view))
6949 begin_x = view_split_begin_x(view->begin_x);
6950 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6952 if (err || tree_view == NULL)
6955 tree_view->focussed = 1;
6956 if (view_is_parent_view(view)) {
6957 err = view_close_child(view);
6960 err = view_set_child(view, tree_view);
6963 view->focus_child = 1;
6965 *new_view = tree_view;
6971 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6977 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6978 for (n = 0; n < view->nlines - 1; n++) {
6981 s->first_displayed_entry = re;
6982 re = TAILQ_PREV(re, tog_reflist_head, entry);
6985 s->selected = n - 1;
6990 if (s->selected > 0) {
6994 ref_scroll_up(s, 1);
6995 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7005 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7006 s->selected -= MIN(nscroll, s->selected);
7007 ref_scroll_up(s, MAX(0, nscroll));
7008 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7014 if (s->selected < s->ndisplayed - 1) {
7018 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7019 /* can't scroll any further */
7023 ref_scroll_down(s, 1);
7033 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7034 /* can't scroll any further; move cursor down */
7035 if (s->selected < s->ndisplayed - 1)
7036 s->selected += MIN(nscroll,
7037 s->ndisplayed - s->selected - 1);
7038 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7039 s->selected += s->ndisplayed - s->selected - 1;
7043 ref_scroll_down(s, nscroll);
7048 err = tog_load_refs(s->repo, s->sort_by_date);
7051 ref_view_free_refs(s);
7052 err = ref_view_load_refs(s);
7055 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7056 s->selected = view->nlines - 2;
7070 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7075 static const struct got_error *
7076 cmd_ref(int argc, char *argv[])
7078 const struct got_error *error;
7079 struct got_repository *repo = NULL;
7080 struct got_worktree *worktree = NULL;
7081 char *cwd = NULL, *repo_path = NULL;
7083 struct tog_view *view;
7084 int *pack_fds = NULL;
7086 while ((ch = getopt(argc, argv, "r:")) != -1) {
7089 repo_path = realpath(optarg, NULL);
7090 if (repo_path == NULL)
7091 return got_error_from_errno2("realpath",
7106 error = got_repo_pack_fds_open(&pack_fds);
7110 if (repo_path == NULL) {
7111 cwd = getcwd(NULL, 0);
7113 return got_error_from_errno("getcwd");
7114 error = got_worktree_open(&worktree, cwd);
7115 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7119 strdup(got_worktree_get_repo_path(worktree));
7121 repo_path = strdup(cwd);
7122 if (repo_path == NULL) {
7123 error = got_error_from_errno("strdup");
7128 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7134 error = apply_unveil(got_repo_get_path(repo), NULL);
7138 error = tog_load_refs(repo, 0);
7142 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7144 error = got_error_from_errno("view_open");
7148 error = open_ref_view(view, repo);
7153 /* Release work tree lock. */
7154 got_worktree_close(worktree);
7157 error = view_loop(view);
7162 const struct got_error *close_err = got_repo_close(repo);
7167 const struct got_error *pack_err =
7168 got_repo_pack_fds_close(pack_fds);
7177 list_commands(FILE *fp)
7181 fprintf(fp, "commands:");
7182 for (i = 0; i < nitems(tog_commands); i++) {
7183 const struct tog_cmd *cmd = &tog_commands[i];
7184 fprintf(fp, " %s", cmd->name);
7190 usage(int hflag, int status)
7192 FILE *fp = (status == 0) ? stdout : stderr;
7194 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7197 fprintf(fp, "lazy usage: %s path\n", getprogname());
7204 make_argv(int argc, ...)
7212 argv = calloc(argc, sizeof(char *));
7215 for (i = 0; i < argc; i++) {
7216 argv[i] = strdup(va_arg(ap, char *));
7217 if (argv[i] == NULL)
7226 * Try to convert 'tog path' into a 'tog log path' command.
7227 * The user could simply have mistyped the command rather than knowingly
7228 * provided a path. So check whether argv[0] can in fact be resolved
7229 * to a path in the HEAD commit and print a special error if not.
7230 * This hack is for mpi@ <3
7232 static const struct got_error *
7233 tog_log_with_path(int argc, char *argv[])
7235 const struct got_error *error = NULL, *close_err;
7236 const struct tog_cmd *cmd = NULL;
7237 struct got_repository *repo = NULL;
7238 struct got_worktree *worktree = NULL;
7239 struct got_object_id *commit_id = NULL, *id = NULL;
7240 struct got_commit_object *commit = NULL;
7241 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7242 char *commit_id_str = NULL, **cmd_argv = NULL;
7243 int *pack_fds = NULL;
7245 cwd = getcwd(NULL, 0);
7247 return got_error_from_errno("getcwd");
7249 error = got_repo_pack_fds_open(&pack_fds);
7253 error = got_worktree_open(&worktree, cwd);
7254 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7258 repo_path = strdup(got_worktree_get_repo_path(worktree));
7260 repo_path = strdup(cwd);
7261 if (repo_path == NULL) {
7262 error = got_error_from_errno("strdup");
7266 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7270 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7275 error = tog_load_refs(repo, 0);
7278 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7279 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7280 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7285 got_worktree_close(worktree);
7289 error = got_object_open_as_commit(&commit, repo, commit_id);
7293 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7295 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7297 fprintf(stderr, "%s: '%s' is no known command or path\n",
7298 getprogname(), argv[0]);
7303 close_err = got_repo_close(repo);
7308 error = got_object_id_str(&commit_id_str, commit_id);
7312 cmd = &tog_commands[0]; /* log */
7314 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7315 error = cmd->cmd_main(argc, cmd_argv);
7318 close_err = got_repo_close(repo);
7323 got_object_commit_close(commit);
7325 got_worktree_close(worktree);
7327 const struct got_error *pack_err =
7328 got_repo_pack_fds_close(pack_fds);
7333 free(commit_id_str);
7340 for (i = 0; i < argc; i++)
7349 main(int argc, char *argv[])
7351 const struct got_error *error = NULL;
7352 const struct tog_cmd *cmd = NULL;
7353 int ch, hflag = 0, Vflag = 0;
7354 char **cmd_argv = NULL;
7355 static const struct option longopts[] = {
7356 { "version", no_argument, NULL, 'V' },
7360 setlocale(LC_CTYPE, "");
7362 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7382 got_version_print_str();
7387 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7395 /* Build an argument vector which runs a default command. */
7396 cmd = &tog_commands[0];
7398 cmd_argv = make_argv(argc, cmd->name);
7402 /* Did the user specify a command? */
7403 for (i = 0; i < nitems(tog_commands); i++) {
7404 if (strncmp(tog_commands[i].name, argv[0],
7405 strlen(argv[0])) == 0) {
7406 cmd = &tog_commands[i];
7415 /* No command specified; try log with a path */
7416 error = tog_log_with_path(argc, argv);
7421 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7428 for (i = 0; i < argc; i++)
7433 if (error && error->code != GOT_ERR_CANCELLED)
7434 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);