2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_opentemp.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
57 #include "got_worktree.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 #define CTRL(x) ((x) & 0x1f)
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static const struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
108 #define TOG_EOF_STRING "(END)"
110 struct commit_queue_entry {
111 TAILQ_ENTRY(commit_queue_entry) entry;
112 struct got_object_id *id;
113 struct got_commit_object *commit;
116 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 struct commit_queue {
119 struct commit_queue_head head;
123 STAILQ_ENTRY(tog_color) entry;
127 STAILQ_HEAD(tog_colors, tog_color);
129 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
130 static struct got_reflist_object_id_map *tog_refs_idmap;
132 static const struct got_error *
133 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
134 struct got_reference* re2)
136 const char *name1 = got_ref_get_name(re1);
137 const char *name2 = got_ref_get_name(re2);
138 int isbackup1, isbackup2;
140 /* Sort backup refs towards the bottom of the list. */
141 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
142 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
143 if (!isbackup1 && isbackup2) {
146 } else if (isbackup1 && !isbackup2) {
151 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
155 static const struct got_error *
156 tog_load_refs(struct got_repository *repo, int sort_by_date)
158 const struct got_error *err;
160 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
161 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
166 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
173 if (tog_refs_idmap) {
174 got_reflist_object_id_map_free(tog_refs_idmap);
175 tog_refs_idmap = NULL;
177 got_ref_list_free(&tog_refs);
180 static const struct got_error *
181 add_color(struct tog_colors *colors, const char *pattern,
182 int idx, short color)
184 const struct got_error *err = NULL;
185 struct tog_color *tc;
188 if (idx < 1 || idx > COLOR_PAIRS - 1)
191 init_pair(idx, color, -1);
193 tc = calloc(1, sizeof(*tc));
195 return got_error_from_errno("calloc");
196 regerr = regcomp(&tc->regex, pattern,
197 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
199 static char regerr_msg[512];
200 static char err_msg[512];
201 regerror(regerr, &tc->regex, regerr_msg,
203 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
205 err = got_error_msg(GOT_ERR_REGEX, err_msg);
210 STAILQ_INSERT_HEAD(colors, tc, entry);
215 free_colors(struct tog_colors *colors)
217 struct tog_color *tc;
219 while (!STAILQ_EMPTY(colors)) {
220 tc = STAILQ_FIRST(colors);
221 STAILQ_REMOVE_HEAD(colors, entry);
228 get_color(struct tog_colors *colors, int colorpair)
230 struct tog_color *tc = NULL;
232 STAILQ_FOREACH(tc, colors, entry) {
233 if (tc->colorpair == colorpair)
241 default_color_value(const char *envvar)
243 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
244 return COLOR_MAGENTA;
245 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
247 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
249 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
251 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
252 return COLOR_MAGENTA;
253 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
254 return COLOR_MAGENTA;
255 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
257 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
259 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
261 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
263 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
265 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
267 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
268 return COLOR_MAGENTA;
269 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
271 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
278 get_color_value(const char *envvar)
280 const char *val = getenv(envvar);
283 return default_color_value(envvar);
285 if (strcasecmp(val, "black") == 0)
287 if (strcasecmp(val, "red") == 0)
289 if (strcasecmp(val, "green") == 0)
291 if (strcasecmp(val, "yellow") == 0)
293 if (strcasecmp(val, "blue") == 0)
295 if (strcasecmp(val, "magenta") == 0)
296 return COLOR_MAGENTA;
297 if (strcasecmp(val, "cyan") == 0)
299 if (strcasecmp(val, "white") == 0)
301 if (strcasecmp(val, "default") == 0)
304 return default_color_value(envvar);
308 struct tog_diff_view_state {
309 struct got_object_id *id1, *id2;
310 const char *label1, *label2;
312 int first_displayed_line;
313 int last_displayed_line;
316 int ignore_whitespace;
318 struct got_repository *repo;
319 struct tog_colors colors;
325 /* passed from log view; may be NULL */
326 struct tog_view *log_view;
329 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
331 struct tog_log_thread_args {
332 pthread_cond_t need_commits;
333 pthread_cond_t commit_loaded;
336 struct got_commit_graph *graph;
337 struct commit_queue *commits;
338 const char *in_repo_path;
339 struct got_object_id *start_id;
340 struct got_repository *repo;
344 struct commit_queue_entry **first_displayed_entry;
345 struct commit_queue_entry **selected_entry;
347 int *search_next_done;
351 struct tog_log_view_state {
352 struct commit_queue commits;
353 struct commit_queue_entry *first_displayed_entry;
354 struct commit_queue_entry *last_displayed_entry;
355 struct commit_queue_entry *selected_entry;
360 struct got_repository *repo;
361 struct got_object_id *start_id;
364 struct tog_log_thread_args thread_args;
365 struct commit_queue_entry *matched_entry;
366 struct commit_queue_entry *search_entry;
367 struct tog_colors colors;
370 #define TOG_COLOR_DIFF_MINUS 1
371 #define TOG_COLOR_DIFF_PLUS 2
372 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
373 #define TOG_COLOR_DIFF_META 4
374 #define TOG_COLOR_TREE_SUBMODULE 5
375 #define TOG_COLOR_TREE_SYMLINK 6
376 #define TOG_COLOR_TREE_DIRECTORY 7
377 #define TOG_COLOR_TREE_EXECUTABLE 8
378 #define TOG_COLOR_COMMIT 9
379 #define TOG_COLOR_AUTHOR 10
380 #define TOG_COLOR_DATE 11
381 #define TOG_COLOR_REFS_HEADS 12
382 #define TOG_COLOR_REFS_TAGS 13
383 #define TOG_COLOR_REFS_REMOTES 14
384 #define TOG_COLOR_REFS_BACKUP 15
386 struct tog_blame_cb_args {
387 struct tog_blame_line *lines; /* one per line */
390 struct tog_view *view;
391 struct got_object_id *commit_id;
395 struct tog_blame_thread_args {
397 struct got_repository *repo;
398 struct tog_blame_cb_args *cb_args;
400 got_cancel_cb cancel_cb;
407 struct tog_blame_line *lines;
411 struct tog_blame_thread_args thread_args;
412 struct tog_blame_cb_args cb_args;
417 struct tog_blame_view_state {
418 int first_displayed_line;
419 int last_displayed_line;
424 struct got_object_id_queue blamed_commits;
425 struct got_object_qid *blamed_commit;
427 struct got_repository *repo;
428 struct got_object_id *commit_id;
429 struct tog_blame blame;
431 struct tog_colors colors;
434 struct tog_parent_tree {
435 TAILQ_ENTRY(tog_parent_tree) entry;
436 struct got_tree_object *tree;
437 struct got_tree_entry *first_displayed_entry;
438 struct got_tree_entry *selected_entry;
442 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
444 struct tog_tree_view_state {
446 struct got_object_id *commit_id;/* commit which this tree belongs to */
447 struct got_tree_object *root; /* the commit's root tree entry */
448 struct got_tree_object *tree; /* currently displayed (sub-)tree */
449 struct got_tree_entry *first_displayed_entry;
450 struct got_tree_entry *last_displayed_entry;
451 struct got_tree_entry *selected_entry;
452 int ndisplayed, selected, show_ids;
453 struct tog_parent_trees parents; /* parent trees of current sub-tree */
455 struct got_repository *repo;
456 struct got_tree_entry *matched_entry;
457 struct tog_colors colors;
460 struct tog_reflist_entry {
461 TAILQ_ENTRY(tog_reflist_entry) entry;
462 struct got_reference *ref;
466 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
468 struct tog_ref_view_state {
469 struct tog_reflist_head refs;
470 struct tog_reflist_entry *first_displayed_entry;
471 struct tog_reflist_entry *last_displayed_entry;
472 struct tog_reflist_entry *selected_entry;
473 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
474 struct got_repository *repo;
475 struct tog_reflist_entry *matched_entry;
476 struct tog_colors colors;
480 * We implement two types of views: parent views and child views.
482 * The 'Tab' key switches focus between a parent view and its child view.
483 * Child views are shown side-by-side to their parent view, provided
484 * there is enough screen estate.
486 * When a new view is opened from within a parent view, this new view
487 * becomes a child view of the parent view, replacing any existing child.
489 * When a new view is opened from within a child view, this new view
490 * becomes a parent view which will obscure the views below until the
491 * user quits the new parent view by typing 'q'.
493 * This list of views contains parent views only.
494 * Child views are only pointed to by their parent view.
496 TAILQ_HEAD(tog_view_list_head, tog_view);
499 TAILQ_ENTRY(tog_view) entry;
502 int nlines, ncols, begin_y, begin_x;
503 int maxx, x; /* max column and current start column */
504 int lines, cols; /* copies of LINES and COLS */
505 int focussed; /* Only set on one parent or child view at a time. */
507 struct tog_view *parent;
508 struct tog_view *child;
511 * This flag is initially set on parent views when a new child view
512 * is created. It gets toggled when the 'Tab' key switches focus
513 * between parent and child.
514 * The flag indicates whether focus should be passed on to our child
515 * view if this parent view gets picked for focus after another parent
516 * view was closed. This prevents child views from losing focus in such
521 /* type-specific state */
522 enum tog_view_type type;
524 struct tog_diff_view_state diff;
525 struct tog_log_view_state log;
526 struct tog_blame_view_state blame;
527 struct tog_tree_view_state tree;
528 struct tog_ref_view_state ref;
531 const struct got_error *(*show)(struct tog_view *);
532 const struct got_error *(*input)(struct tog_view **,
533 struct tog_view *, int);
534 const struct got_error *(*close)(struct tog_view *);
536 const struct got_error *(*search_start)(struct tog_view *);
537 const struct got_error *(*search_next)(struct tog_view *);
540 #define TOG_SEARCH_FORWARD 1
541 #define TOG_SEARCH_BACKWARD 2
542 int search_next_done;
543 #define TOG_SEARCH_HAVE_MORE 1
544 #define TOG_SEARCH_NO_MORE 2
545 #define TOG_SEARCH_HAVE_NONE 3
550 static const struct got_error *open_diff_view(struct tog_view *,
551 struct got_object_id *, struct got_object_id *,
552 const char *, const char *, int, int, int, struct tog_view *,
553 struct got_repository *);
554 static const struct got_error *show_diff_view(struct tog_view *);
555 static const struct got_error *input_diff_view(struct tog_view **,
556 struct tog_view *, int);
557 static const struct got_error* close_diff_view(struct tog_view *);
558 static const struct got_error *search_start_diff_view(struct tog_view *);
559 static const struct got_error *search_next_diff_view(struct tog_view *);
561 static const struct got_error *open_log_view(struct tog_view *,
562 struct got_object_id *, struct got_repository *,
563 const char *, const char *, int);
564 static const struct got_error * show_log_view(struct tog_view *);
565 static const struct got_error *input_log_view(struct tog_view **,
566 struct tog_view *, int);
567 static const struct got_error *close_log_view(struct tog_view *);
568 static const struct got_error *search_start_log_view(struct tog_view *);
569 static const struct got_error *search_next_log_view(struct tog_view *);
571 static const struct got_error *open_blame_view(struct tog_view *, char *,
572 struct got_object_id *, struct got_repository *);
573 static const struct got_error *show_blame_view(struct tog_view *);
574 static const struct got_error *input_blame_view(struct tog_view **,
575 struct tog_view *, int);
576 static const struct got_error *close_blame_view(struct tog_view *);
577 static const struct got_error *search_start_blame_view(struct tog_view *);
578 static const struct got_error *search_next_blame_view(struct tog_view *);
580 static const struct got_error *open_tree_view(struct tog_view *,
581 struct got_object_id *, const char *, struct got_repository *);
582 static const struct got_error *show_tree_view(struct tog_view *);
583 static const struct got_error *input_tree_view(struct tog_view **,
584 struct tog_view *, int);
585 static const struct got_error *close_tree_view(struct tog_view *);
586 static const struct got_error *search_start_tree_view(struct tog_view *);
587 static const struct got_error *search_next_tree_view(struct tog_view *);
589 static const struct got_error *open_ref_view(struct tog_view *,
590 struct got_repository *);
591 static const struct got_error *show_ref_view(struct tog_view *);
592 static const struct got_error *input_ref_view(struct tog_view **,
593 struct tog_view *, int);
594 static const struct got_error *close_ref_view(struct tog_view *);
595 static const struct got_error *search_start_ref_view(struct tog_view *);
596 static const struct got_error *search_next_ref_view(struct tog_view *);
598 static volatile sig_atomic_t tog_sigwinch_received;
599 static volatile sig_atomic_t tog_sigpipe_received;
600 static volatile sig_atomic_t tog_sigcont_received;
601 static volatile sig_atomic_t tog_sigint_received;
602 static volatile sig_atomic_t tog_sigterm_received;
605 tog_sigwinch(int signo)
607 tog_sigwinch_received = 1;
611 tog_sigpipe(int signo)
613 tog_sigpipe_received = 1;
617 tog_sigcont(int signo)
619 tog_sigcont_received = 1;
623 tog_sigint(int signo)
625 tog_sigint_received = 1;
629 tog_sigterm(int signo)
631 tog_sigterm_received = 1;
635 tog_fatal_signal_received()
637 return (tog_sigpipe_received ||
638 tog_sigint_received || tog_sigint_received);
642 static const struct got_error *
643 view_close(struct tog_view *view)
645 const struct got_error *err = NULL;
648 view_close(view->child);
652 err = view->close(view);
654 del_panel(view->panel);
656 delwin(view->window);
661 static struct tog_view *
662 view_open(int nlines, int ncols, int begin_y, int begin_x,
663 enum tog_view_type type)
665 struct tog_view *view = calloc(1, sizeof(*view));
673 view->nlines = nlines ? nlines : LINES - begin_y;
674 view->ncols = ncols ? ncols : COLS - begin_x;
675 view->begin_y = begin_y;
676 view->begin_x = begin_x;
677 view->window = newwin(nlines, ncols, begin_y, begin_x);
678 if (view->window == NULL) {
682 view->panel = new_panel(view->window);
683 if (view->panel == NULL ||
684 set_panel_userptr(view->panel, view) != OK) {
689 keypad(view->window, TRUE);
694 view_split_begin_x(int begin_x)
696 if (begin_x > 0 || COLS < 120)
698 return (COLS - MAX(COLS / 2, 80));
701 static const struct got_error *view_resize(struct tog_view *);
703 static const struct got_error *
704 view_splitscreen(struct tog_view *view)
706 const struct got_error *err = NULL;
709 view->begin_x = view_split_begin_x(0);
710 view->nlines = LINES;
711 view->ncols = COLS - view->begin_x;
714 err = view_resize(view);
718 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
719 return got_error_from_errno("mvwin");
724 static const struct got_error *
725 view_fullscreen(struct tog_view *view)
727 const struct got_error *err = NULL;
731 view->nlines = LINES;
735 err = view_resize(view);
739 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
740 return got_error_from_errno("mvwin");
746 view_is_parent_view(struct tog_view *view)
748 return view->parent == NULL;
751 static const struct got_error *
752 view_resize(struct tog_view *view)
756 if (view->lines > LINES)
757 nlines = view->nlines - (view->lines - LINES);
759 nlines = view->nlines + (LINES - view->lines);
761 if (view->cols > COLS)
762 ncols = view->ncols - (view->cols - COLS);
764 ncols = view->ncols + (COLS - view->cols);
766 if (wresize(view->window, nlines, ncols) == ERR)
767 return got_error_from_errno("wresize");
768 if (replace_panel(view->panel, view->window) == ERR)
769 return got_error_from_errno("replace_panel");
770 wclear(view->window);
772 view->nlines = nlines;
778 view->child->begin_x = view_split_begin_x(view->begin_x);
779 if (view->child->begin_x == 0) {
780 view_fullscreen(view->child);
781 if (view->child->focussed)
782 show_panel(view->child->panel);
784 show_panel(view->panel);
786 view_splitscreen(view->child);
787 show_panel(view->child->panel);
794 static const struct got_error *
795 view_close_child(struct tog_view *view)
797 const struct got_error *err = NULL;
799 if (view->child == NULL)
802 err = view_close(view->child);
808 view_set_child(struct tog_view *view, struct tog_view *child)
811 child->parent = view;
815 view_is_splitscreen(struct tog_view *view)
817 return view->begin_x > 0;
826 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
827 cols = 80; /* Default */
833 resize_term(lines, cols);
836 static const struct got_error *
837 view_search_start(struct tog_view *view)
839 const struct got_error *err = NULL;
843 if (view->search_started) {
844 regfree(&view->regex);
846 memset(&view->regmatch, 0, sizeof(view->regmatch));
848 view->search_started = 0;
850 if (view->nlines < 1)
853 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
854 wclrtoeol(view->window);
858 ret = wgetnstr(view->window, pattern, sizeof(pattern));
864 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
865 err = view->search_start(view);
867 regfree(&view->regex);
870 view->search_started = 1;
871 view->searching = TOG_SEARCH_FORWARD;
872 view->search_next_done = 0;
873 view->search_next(view);
879 static const struct got_error *
880 view_input(struct tog_view **new, int *done, struct tog_view *view,
881 struct tog_view_list_head *views)
883 const struct got_error *err = NULL;
889 /* Clear "no matches" indicator. */
890 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
891 view->search_next_done == TOG_SEARCH_HAVE_NONE)
892 view->search_next_done = TOG_SEARCH_HAVE_MORE;
894 if (view->searching && !view->search_next_done) {
895 errcode = pthread_mutex_unlock(&tog_mutex);
897 return got_error_set_errno(errcode,
898 "pthread_mutex_unlock");
900 errcode = pthread_mutex_lock(&tog_mutex);
902 return got_error_set_errno(errcode,
903 "pthread_mutex_lock");
904 view->search_next(view);
908 nodelay(stdscr, FALSE);
909 /* Allow threads to make progress while we are waiting for input. */
910 errcode = pthread_mutex_unlock(&tog_mutex);
912 return got_error_set_errno(errcode, "pthread_mutex_unlock");
913 ch = wgetch(view->window);
914 errcode = pthread_mutex_lock(&tog_mutex);
916 return got_error_set_errno(errcode, "pthread_mutex_lock");
917 nodelay(stdscr, TRUE);
919 if (tog_sigwinch_received || tog_sigcont_received) {
921 tog_sigwinch_received = 0;
922 tog_sigcont_received = 0;
923 TAILQ_FOREACH(v, views, entry) {
924 err = view_resize(v);
927 err = v->input(new, v, KEY_RESIZE);
931 err = view_resize(v->child);
934 err = v->child->input(new, v->child,
946 view->child->focussed = 1;
947 view->focus_child = 1;
948 } else if (view->parent) {
950 view->parent->focussed = 1;
951 view->parent->focus_child = 0;
955 err = view->input(new, view, ch);
962 if (view_is_parent_view(view)) {
963 if (view->child == NULL)
965 if (view_is_splitscreen(view->child)) {
967 view->child->focussed = 1;
968 err = view_fullscreen(view->child);
970 err = view_splitscreen(view->child);
973 err = view->child->input(new, view->child,
976 if (view_is_splitscreen(view)) {
977 view->parent->focussed = 0;
979 err = view_fullscreen(view);
981 err = view_splitscreen(view);
985 err = view->input(new, view, KEY_RESIZE);
991 if (view->search_start)
992 view_search_start(view);
994 err = view->input(new, view, ch);
998 if (view->search_started && view->search_next) {
999 view->searching = (ch == 'n' ?
1000 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1001 view->search_next_done = 0;
1002 view->search_next(view);
1004 err = view->input(new, view, ch);
1007 err = view->input(new, view, ch);
1015 view_vborder(struct tog_view *view)
1018 const struct tog_view *view_above;
1021 return view_vborder(view->parent);
1023 panel = panel_above(view->panel);
1027 view_above = panel_userptr(panel);
1028 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1029 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1033 view_needs_focus_indication(struct tog_view *view)
1035 if (view_is_parent_view(view)) {
1036 if (view->child == NULL || view->child->focussed)
1038 if (!view_is_splitscreen(view->child))
1040 } else if (!view_is_splitscreen(view))
1043 return view->focussed;
1046 static const struct got_error *
1047 view_loop(struct tog_view *view)
1049 const struct got_error *err = NULL;
1050 struct tog_view_list_head views;
1051 struct tog_view *new_view;
1052 int fast_refresh = 10;
1053 int done = 0, errcode;
1055 errcode = pthread_mutex_lock(&tog_mutex);
1057 return got_error_set_errno(errcode, "pthread_mutex_lock");
1060 TAILQ_INSERT_HEAD(&views, view, entry);
1063 err = view->show(view);
1068 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1069 /* Refresh fast during initialization, then become slower. */
1070 if (fast_refresh && fast_refresh-- == 0)
1071 halfdelay(10); /* switch to once per second */
1073 err = view_input(&new_view, &done, view, &views);
1077 struct tog_view *v, *prev = NULL;
1079 if (view_is_parent_view(view))
1080 prev = TAILQ_PREV(view, tog_view_list_head,
1082 else if (view->parent)
1083 prev = view->parent;
1086 view->parent->child = NULL;
1087 view->parent->focus_child = 0;
1089 TAILQ_REMOVE(&views, view, entry);
1091 err = view_close(view);
1096 TAILQ_FOREACH(v, &views, entry) {
1100 if (view == NULL && new_view == NULL) {
1101 /* No view has focus. Try to pick one. */
1104 else if (!TAILQ_EMPTY(&views)) {
1105 view = TAILQ_LAST(&views,
1106 tog_view_list_head);
1109 if (view->focus_child) {
1110 view->child->focussed = 1;
1118 struct tog_view *v, *t;
1119 /* Only allow one parent view per type. */
1120 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1121 if (v->type != new_view->type)
1123 TAILQ_REMOVE(&views, v, entry);
1124 err = view_close(v);
1129 TAILQ_INSERT_TAIL(&views, new_view, entry);
1133 if (view_is_parent_view(view)) {
1134 if (view->child && view->child->focussed)
1137 if (view->parent && view->parent->focussed)
1138 view = view->parent;
1140 show_panel(view->panel);
1141 if (view->child && view_is_splitscreen(view->child))
1142 show_panel(view->child->panel);
1143 if (view->parent && view_is_splitscreen(view)) {
1144 err = view->parent->show(view->parent);
1148 err = view->show(view);
1152 err = view->child->show(view->child);
1161 while (!TAILQ_EMPTY(&views)) {
1162 view = TAILQ_FIRST(&views);
1163 TAILQ_REMOVE(&views, view, entry);
1167 errcode = pthread_mutex_unlock(&tog_mutex);
1168 if (errcode && err == NULL)
1169 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1179 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1184 /* Create newly allocated wide-character string equivalent to a byte string. */
1185 static const struct got_error *
1186 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1189 const struct got_error *err = NULL;
1192 *wlen = mbstowcs(NULL, s, 0);
1193 if (*wlen == (size_t)-1) {
1195 if (errno != EILSEQ)
1196 return got_error_from_errno("mbstowcs");
1198 /* byte string invalid in current encoding; try to "fix" it */
1199 err = got_mbsavis(&vis, &vislen, s);
1202 *wlen = mbstowcs(NULL, vis, 0);
1203 if (*wlen == (size_t)-1) {
1204 err = got_error_from_errno("mbstowcs"); /* give up */
1209 *ws = calloc(*wlen + 1, sizeof(**ws));
1211 err = got_error_from_errno("calloc");
1215 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1216 err = got_error_from_errno("mbstowcs");
1227 static const struct got_error *
1228 expand_tab(char **ptr, const char *src)
1231 size_t len, n, idx = 0, sz = 0;
1234 n = len = strlen(src);
1235 dst = malloc(n + 1);
1237 return got_error_from_errno("malloc");
1239 while (idx < len && src[idx]) {
1240 const char c = src[idx];
1243 size_t nb = TABSIZE - sz % TABSIZE;
1246 p = realloc(dst, n + nb);
1249 return got_error_from_errno("realloc");
1254 memset(dst + sz, ' ', nb);
1257 dst[sz++] = src[idx];
1267 * Skip leading nscroll columns of a wide character string.
1268 * Returns the index to the first character of the scrolled string.
1270 static const struct got_error *
1271 scroll_wline(int *scrollx , wchar_t *wline, int nscroll,
1275 size_t wlen = wcslen(wline);
1280 while (i < wlen && cols < nscroll) {
1281 int width = wcwidth(wline[i]);
1288 if (width == 1 || width == 2) {
1289 if (cols + width > nscroll)
1293 } else if (width == -1) {
1294 if (wline[i] == L'\t') {
1296 ((cols + col_tab_align) % TABSIZE);
1301 if (cols + width > nscroll)
1306 return got_error_from_errno("wcwidth");
1314 * Format a line for display, ensuring that it won't overflow a width limit.
1315 * With scrolling, the width returned refers to the scrolled version of the
1316 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1318 static const struct got_error *
1319 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1320 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1322 const struct got_error *err = NULL;
1324 wchar_t *wline = NULL;
1333 err = expand_tab(&exstr, line);
1338 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1343 err = scroll_wline(&scrollx, wline, nscroll, col_tab_align);
1347 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1348 wline[wlen - 1] = L'\0';
1351 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1352 wline[wlen - 1] = L'\0';
1358 int width = wcwidth(wline[i]);
1365 if (width == 1 || width == 2) {
1366 if (cols + width > wlimit)
1370 } else if (width == -1) {
1371 if (wline[i] == L'\t') {
1373 ((cols + col_tab_align) % TABSIZE);
1378 if (cols + width > wlimit)
1383 err = got_error_from_errno("wcwidth");
1391 *scrollxp = scrollx;
1400 static const struct got_error*
1401 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1402 struct got_object_id *id, struct got_repository *repo)
1404 static const struct got_error *err = NULL;
1405 struct got_reflist_entry *re;
1411 TAILQ_FOREACH(re, refs, entry) {
1412 struct got_tag_object *tag = NULL;
1413 struct got_object_id *ref_id;
1416 name = got_ref_get_name(re->ref);
1417 if (strcmp(name, GOT_REF_HEAD) == 0)
1419 if (strncmp(name, "refs/", 5) == 0)
1421 if (strncmp(name, "got/", 4) == 0 &&
1422 strncmp(name, "got/backup/", 11) != 0)
1424 if (strncmp(name, "heads/", 6) == 0)
1426 if (strncmp(name, "remotes/", 8) == 0) {
1428 s = strstr(name, "/" GOT_REF_HEAD);
1429 if (s != NULL && s[strlen(s)] == '\0')
1432 err = got_ref_resolve(&ref_id, repo, re->ref);
1435 if (strncmp(name, "tags/", 5) == 0) {
1436 err = got_object_open_as_tag(&tag, repo, ref_id);
1438 if (err->code != GOT_ERR_OBJ_TYPE) {
1442 /* Ref points at something other than a tag. */
1447 cmp = got_object_id_cmp(tag ?
1448 got_object_tag_get_object_id(tag) : ref_id, id);
1451 got_object_tag_close(tag);
1455 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1456 s ? ", " : "", name) == -1) {
1457 err = got_error_from_errno("asprintf");
1468 static const struct got_error *
1469 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1474 smallerthan = strchr(author, '<');
1475 if (smallerthan && smallerthan[1] != '\0')
1476 author = smallerthan + 1;
1477 author[strcspn(author, "@>")] = '\0';
1478 return format_line(wauthor, author_width, NULL, author, 0, limit,
1482 static const struct got_error *
1483 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1484 struct got_object_id *id, const size_t date_display_cols,
1485 int author_display_cols)
1487 struct tog_log_view_state *s = &view->state.log;
1488 const struct got_error *err = NULL;
1489 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1490 char *logmsg0 = NULL, *logmsg = NULL;
1491 char *author = NULL;
1492 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1493 int author_width, logmsg_width;
1494 char *newline, *line = NULL;
1495 int col, limit, scrollx;
1496 const int avail = view->ncols;
1498 time_t committer_time;
1499 struct tog_color *tc;
1501 committer_time = got_object_commit_get_committer_time(commit);
1502 if (gmtime_r(&committer_time, &tm) == NULL)
1503 return got_error_from_errno("gmtime_r");
1504 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1505 return got_error(GOT_ERR_NO_SPACE);
1507 if (avail <= date_display_cols)
1508 limit = MIN(sizeof(datebuf) - 1, avail);
1510 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1511 tc = get_color(&s->colors, TOG_COLOR_DATE);
1513 wattr_on(view->window,
1514 COLOR_PAIR(tc->colorpair), NULL);
1515 waddnstr(view->window, datebuf, limit);
1517 wattr_off(view->window,
1518 COLOR_PAIR(tc->colorpair), NULL);
1525 err = got_object_id_str(&id_str, id);
1528 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1530 wattr_on(view->window,
1531 COLOR_PAIR(tc->colorpair), NULL);
1532 wprintw(view->window, "%.8s ", id_str);
1534 wattr_off(view->window,
1535 COLOR_PAIR(tc->colorpair), NULL);
1542 author = strdup(got_object_commit_get_author(commit));
1543 if (author == NULL) {
1544 err = got_error_from_errno("strdup");
1547 err = format_author(&wauthor, &author_width, author, avail - col, col);
1550 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1552 wattr_on(view->window,
1553 COLOR_PAIR(tc->colorpair), NULL);
1554 waddwstr(view->window, wauthor);
1556 wattr_off(view->window,
1557 COLOR_PAIR(tc->colorpair), NULL);
1558 col += author_width;
1559 while (col < avail && author_width < author_display_cols + 2) {
1560 waddch(view->window, ' ');
1567 err = got_object_commit_get_logmsg(&logmsg0, commit);
1571 while (*logmsg == '\n')
1573 newline = strchr(logmsg, '\n');
1576 limit = avail - col;
1577 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1581 waddwstr(view->window, &wlogmsg[scrollx]);
1582 col += MAX(logmsg_width, 0);
1583 while (col < avail) {
1584 waddch(view->window, ' ');
1596 static struct commit_queue_entry *
1597 alloc_commit_queue_entry(struct got_commit_object *commit,
1598 struct got_object_id *id)
1600 struct commit_queue_entry *entry;
1602 entry = calloc(1, sizeof(*entry));
1607 entry->commit = commit;
1612 pop_commit(struct commit_queue *commits)
1614 struct commit_queue_entry *entry;
1616 entry = TAILQ_FIRST(&commits->head);
1617 TAILQ_REMOVE(&commits->head, entry, entry);
1618 got_object_commit_close(entry->commit);
1619 commits->ncommits--;
1620 /* Don't free entry->id! It is owned by the commit graph. */
1625 free_commits(struct commit_queue *commits)
1627 while (!TAILQ_EMPTY(&commits->head))
1628 pop_commit(commits);
1631 static const struct got_error *
1632 match_commit(int *have_match, struct got_object_id *id,
1633 struct got_commit_object *commit, regex_t *regex)
1635 const struct got_error *err = NULL;
1636 regmatch_t regmatch;
1637 char *id_str = NULL, *logmsg = NULL;
1641 err = got_object_id_str(&id_str, id);
1645 err = got_object_commit_get_logmsg(&logmsg, commit);
1649 if (regexec(regex, got_object_commit_get_author(commit), 1,
1650 ®match, 0) == 0 ||
1651 regexec(regex, got_object_commit_get_committer(commit), 1,
1652 ®match, 0) == 0 ||
1653 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1654 regexec(regex, logmsg, 1, ®match, 0) == 0)
1662 static const struct got_error *
1663 queue_commits(struct tog_log_thread_args *a)
1665 const struct got_error *err = NULL;
1668 * We keep all commits open throughout the lifetime of the log
1669 * view in order to avoid having to re-fetch commits from disk
1670 * while updating the display.
1673 struct got_object_id *id;
1674 struct got_commit_object *commit;
1675 struct commit_queue_entry *entry;
1678 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1680 if (err || id == NULL)
1683 err = got_object_open_as_commit(&commit, a->repo, id);
1686 entry = alloc_commit_queue_entry(commit, id);
1687 if (entry == NULL) {
1688 err = got_error_from_errno("alloc_commit_queue_entry");
1692 errcode = pthread_mutex_lock(&tog_mutex);
1694 err = got_error_set_errno(errcode,
1695 "pthread_mutex_lock");
1699 entry->idx = a->commits->ncommits;
1700 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1701 a->commits->ncommits++;
1703 if (*a->searching == TOG_SEARCH_FORWARD &&
1704 !*a->search_next_done) {
1706 err = match_commit(&have_match, id, commit, a->regex);
1710 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1713 errcode = pthread_mutex_unlock(&tog_mutex);
1714 if (errcode && err == NULL)
1715 err = got_error_set_errno(errcode,
1716 "pthread_mutex_unlock");
1719 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1725 select_commit(struct tog_log_view_state *s)
1727 struct commit_queue_entry *entry;
1730 entry = s->first_displayed_entry;
1732 if (ncommits == s->selected) {
1733 s->selected_entry = entry;
1736 entry = TAILQ_NEXT(entry, entry);
1741 static const struct got_error *
1742 draw_commits(struct tog_view *view)
1744 const struct got_error *err = NULL;
1745 struct tog_log_view_state *s = &view->state.log;
1746 struct commit_queue_entry *entry = s->selected_entry;
1747 const int limit = view->nlines;
1749 int ncommits, author_cols = 4;
1750 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1751 char *refs_str = NULL;
1753 struct tog_color *tc;
1754 static const size_t date_display_cols = 12;
1756 if (s->selected_entry &&
1757 !(view->searching && view->search_next_done == 0)) {
1758 struct got_reflist_head *refs;
1759 err = got_object_id_str(&id_str, s->selected_entry->id);
1762 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1763 s->selected_entry->id);
1765 err = build_refs_str(&refs_str, refs,
1766 s->selected_entry->id, s->repo);
1772 if (s->thread_args.commits_needed == 0)
1773 halfdelay(10); /* disable fast refresh */
1775 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1776 if (asprintf(&ncommits_str, " [%d/%d] %s",
1777 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1778 (view->searching && !view->search_next_done) ?
1779 "searching..." : "loading...") == -1) {
1780 err = got_error_from_errno("asprintf");
1784 const char *search_str = NULL;
1786 if (view->searching) {
1787 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1788 search_str = "no more matches";
1789 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1790 search_str = "no matches found";
1791 else if (!view->search_next_done)
1792 search_str = "searching...";
1795 if (asprintf(&ncommits_str, " [%d/%d] %s",
1796 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1797 search_str ? search_str :
1798 (refs_str ? refs_str : "")) == -1) {
1799 err = got_error_from_errno("asprintf");
1804 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1805 if (asprintf(&header, "commit %s %s%s",
1806 id_str ? id_str : "........................................",
1807 s->in_repo_path, ncommits_str) == -1) {
1808 err = got_error_from_errno("asprintf");
1812 } else if (asprintf(&header, "commit %s%s",
1813 id_str ? id_str : "........................................",
1814 ncommits_str) == -1) {
1815 err = got_error_from_errno("asprintf");
1819 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1823 werase(view->window);
1825 if (view_needs_focus_indication(view))
1826 wstandout(view->window);
1827 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1829 wattr_on(view->window,
1830 COLOR_PAIR(tc->colorpair), NULL);
1831 waddwstr(view->window, wline);
1833 wattr_off(view->window,
1834 COLOR_PAIR(tc->colorpair), NULL);
1835 while (width < view->ncols) {
1836 waddch(view->window, ' ');
1839 if (view_needs_focus_indication(view))
1840 wstandend(view->window);
1845 /* Grow author column size if necessary, and set view->maxx. */
1846 entry = s->first_displayed_entry;
1850 char *author, *eol, *msg, *msg0;
1851 wchar_t *wauthor, *wmsg;
1853 if (ncommits >= limit - 1)
1855 author = strdup(got_object_commit_get_author(entry->commit));
1856 if (author == NULL) {
1857 err = got_error_from_errno("strdup");
1860 err = format_author(&wauthor, &width, author, COLS,
1862 if (author_cols < width)
1863 author_cols = width;
1866 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1870 while (*msg == '\n')
1872 if ((eol = strchr(msg, '\n')))
1874 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
1875 date_display_cols + author_cols, 0);
1878 view->maxx = MAX(view->maxx, width);
1882 entry = TAILQ_NEXT(entry, entry);
1885 entry = s->first_displayed_entry;
1886 s->last_displayed_entry = s->first_displayed_entry;
1889 if (ncommits >= limit - 1)
1891 if (ncommits == s->selected)
1892 wstandout(view->window);
1893 err = draw_commit(view, entry->commit, entry->id,
1894 date_display_cols, author_cols);
1895 if (ncommits == s->selected)
1896 wstandend(view->window);
1900 s->last_displayed_entry = entry;
1901 entry = TAILQ_NEXT(entry, entry);
1914 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1916 struct commit_queue_entry *entry;
1919 entry = TAILQ_FIRST(&s->commits.head);
1920 if (s->first_displayed_entry == entry)
1923 entry = s->first_displayed_entry;
1924 while (entry && nscrolled < maxscroll) {
1925 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1927 s->first_displayed_entry = entry;
1933 static const struct got_error *
1934 trigger_log_thread(struct tog_view *view, int wait)
1936 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1939 halfdelay(1); /* fast refresh while loading commits */
1941 while (ta->commits_needed > 0 || ta->load_all) {
1942 if (ta->log_complete)
1945 /* Wake the log thread. */
1946 errcode = pthread_cond_signal(&ta->need_commits);
1948 return got_error_set_errno(errcode,
1949 "pthread_cond_signal");
1952 * The mutex will be released while the view loop waits
1953 * in wgetch(), at which time the log thread will run.
1958 /* Display progress update in log view. */
1959 show_log_view(view);
1963 /* Wait right here while next commit is being loaded. */
1964 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1966 return got_error_set_errno(errcode,
1967 "pthread_cond_wait");
1969 /* Display progress update in log view. */
1970 show_log_view(view);
1978 static const struct got_error *
1979 log_scroll_down(struct tog_view *view, int maxscroll)
1981 struct tog_log_view_state *s = &view->state.log;
1982 const struct got_error *err = NULL;
1983 struct commit_queue_entry *pentry;
1984 int nscrolled = 0, ncommits_needed;
1986 if (s->last_displayed_entry == NULL)
1989 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1990 if (s->commits.ncommits < ncommits_needed &&
1991 !s->thread_args.log_complete) {
1993 * Ask the log thread for required amount of commits.
1995 s->thread_args.commits_needed += maxscroll;
1996 err = trigger_log_thread(view, 1);
2002 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2006 s->last_displayed_entry = pentry;
2008 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2011 s->first_displayed_entry = pentry;
2012 } while (++nscrolled < maxscroll);
2017 static const struct got_error *
2018 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
2019 struct got_commit_object *commit, struct got_object_id *commit_id,
2020 struct tog_view *log_view, struct got_repository *repo)
2022 const struct got_error *err;
2023 struct got_object_qid *parent_id;
2024 struct tog_view *diff_view;
2026 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2027 if (diff_view == NULL)
2028 return got_error_from_errno("view_open");
2030 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2031 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2032 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2034 *new_view = diff_view;
2038 static const struct got_error *
2039 tree_view_visit_subtree(struct tog_tree_view_state *s,
2040 struct got_tree_object *subtree)
2042 struct tog_parent_tree *parent;
2044 parent = calloc(1, sizeof(*parent));
2046 return got_error_from_errno("calloc");
2048 parent->tree = s->tree;
2049 parent->first_displayed_entry = s->first_displayed_entry;
2050 parent->selected_entry = s->selected_entry;
2051 parent->selected = s->selected;
2052 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2055 s->first_displayed_entry = NULL;
2059 static const struct got_error *
2060 tree_view_walk_path(struct tog_tree_view_state *s,
2061 struct got_commit_object *commit, const char *path)
2063 const struct got_error *err = NULL;
2064 struct got_tree_object *tree = NULL;
2066 char *slash, *subpath = NULL;
2068 /* Walk the path and open corresponding tree objects. */
2071 struct got_tree_entry *te;
2072 struct got_object_id *tree_id;
2078 /* Ensure the correct subtree entry is selected. */
2079 slash = strchr(p, '/');
2081 te_name = strdup(p);
2083 te_name = strndup(p, slash - p);
2084 if (te_name == NULL) {
2085 err = got_error_from_errno("strndup");
2088 te = got_object_tree_find_entry(s->tree, te_name);
2090 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2095 s->first_displayed_entry = s->selected_entry = te;
2097 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2098 break; /* jump to this file's entry */
2100 slash = strchr(p, '/');
2102 subpath = strndup(path, slash - path);
2104 subpath = strdup(path);
2105 if (subpath == NULL) {
2106 err = got_error_from_errno("strdup");
2110 err = got_object_id_by_path(&tree_id, s->repo, commit,
2115 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2120 err = tree_view_visit_subtree(s, tree);
2122 got_object_tree_close(tree);
2136 static const struct got_error *
2137 browse_commit_tree(struct tog_view **new_view, int begin_x,
2138 struct commit_queue_entry *entry, const char *path,
2139 const char *head_ref_name, struct got_repository *repo)
2141 const struct got_error *err = NULL;
2142 struct tog_tree_view_state *s;
2143 struct tog_view *tree_view;
2145 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2146 if (tree_view == NULL)
2147 return got_error_from_errno("view_open");
2149 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2152 s = &tree_view->state.tree;
2154 *new_view = tree_view;
2156 if (got_path_is_root_dir(path))
2159 return tree_view_walk_path(s, entry->commit, path);
2162 static const struct got_error *
2163 block_signals_used_by_main_thread(void)
2168 if (sigemptyset(&sigset) == -1)
2169 return got_error_from_errno("sigemptyset");
2171 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2172 if (sigaddset(&sigset, SIGWINCH) == -1)
2173 return got_error_from_errno("sigaddset");
2174 if (sigaddset(&sigset, SIGCONT) == -1)
2175 return got_error_from_errno("sigaddset");
2176 if (sigaddset(&sigset, SIGINT) == -1)
2177 return got_error_from_errno("sigaddset");
2178 if (sigaddset(&sigset, SIGTERM) == -1)
2179 return got_error_from_errno("sigaddset");
2181 /* ncurses handles SIGTSTP */
2182 if (sigaddset(&sigset, SIGTSTP) == -1)
2183 return got_error_from_errno("sigaddset");
2185 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2187 return got_error_set_errno(errcode, "pthread_sigmask");
2193 log_thread(void *arg)
2195 const struct got_error *err = NULL;
2197 struct tog_log_thread_args *a = arg;
2200 err = block_signals_used_by_main_thread();
2204 while (!done && !err && !tog_fatal_signal_received()) {
2205 err = queue_commits(a);
2207 if (err->code != GOT_ERR_ITER_COMPLETED)
2211 } else if (a->commits_needed > 0 && !a->load_all)
2212 a->commits_needed--;
2214 errcode = pthread_mutex_lock(&tog_mutex);
2216 err = got_error_set_errno(errcode,
2217 "pthread_mutex_lock");
2219 } else if (*a->quit)
2221 else if (*a->first_displayed_entry == NULL) {
2222 *a->first_displayed_entry =
2223 TAILQ_FIRST(&a->commits->head);
2224 *a->selected_entry = *a->first_displayed_entry;
2227 errcode = pthread_cond_signal(&a->commit_loaded);
2229 err = got_error_set_errno(errcode,
2230 "pthread_cond_signal");
2231 pthread_mutex_unlock(&tog_mutex);
2236 a->commits_needed = 0;
2238 if (a->commits_needed == 0 && !a->load_all) {
2239 errcode = pthread_cond_wait(&a->need_commits,
2242 err = got_error_set_errno(errcode,
2243 "pthread_cond_wait");
2249 errcode = pthread_mutex_unlock(&tog_mutex);
2250 if (errcode && err == NULL)
2251 err = got_error_set_errno(errcode,
2252 "pthread_mutex_unlock");
2254 a->log_complete = 1;
2258 static const struct got_error *
2259 stop_log_thread(struct tog_log_view_state *s)
2261 const struct got_error *err = NULL;
2266 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2268 return got_error_set_errno(errcode,
2269 "pthread_cond_signal");
2270 errcode = pthread_mutex_unlock(&tog_mutex);
2272 return got_error_set_errno(errcode,
2273 "pthread_mutex_unlock");
2274 errcode = pthread_join(s->thread, (void **)&err);
2276 return got_error_set_errno(errcode, "pthread_join");
2277 errcode = pthread_mutex_lock(&tog_mutex);
2279 return got_error_set_errno(errcode,
2280 "pthread_mutex_lock");
2284 if (s->thread_args.repo) {
2285 err = got_repo_close(s->thread_args.repo);
2286 s->thread_args.repo = NULL;
2289 if (s->thread_args.pack_fds) {
2290 const struct got_error *pack_err =
2291 got_repo_pack_fds_close(s->thread_args.pack_fds);
2294 s->thread_args.pack_fds = NULL;
2297 if (s->thread_args.graph) {
2298 got_commit_graph_close(s->thread_args.graph);
2299 s->thread_args.graph = NULL;
2305 static const struct got_error *
2306 close_log_view(struct tog_view *view)
2308 const struct got_error *err = NULL;
2309 struct tog_log_view_state *s = &view->state.log;
2312 err = stop_log_thread(s);
2314 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2315 if (errcode && err == NULL)
2316 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2318 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2319 if (errcode && err == NULL)
2320 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2322 free_commits(&s->commits);
2323 free(s->in_repo_path);
2324 s->in_repo_path = NULL;
2327 free(s->head_ref_name);
2328 s->head_ref_name = NULL;
2332 static const struct got_error *
2333 search_start_log_view(struct tog_view *view)
2335 struct tog_log_view_state *s = &view->state.log;
2337 s->matched_entry = NULL;
2338 s->search_entry = NULL;
2342 static const struct got_error *
2343 search_next_log_view(struct tog_view *view)
2345 const struct got_error *err = NULL;
2346 struct tog_log_view_state *s = &view->state.log;
2347 struct commit_queue_entry *entry;
2349 /* Display progress update in log view. */
2350 show_log_view(view);
2354 if (s->search_entry) {
2356 errcode = pthread_mutex_unlock(&tog_mutex);
2358 return got_error_set_errno(errcode,
2359 "pthread_mutex_unlock");
2360 ch = wgetch(view->window);
2361 errcode = pthread_mutex_lock(&tog_mutex);
2363 return got_error_set_errno(errcode,
2364 "pthread_mutex_lock");
2365 if (ch == KEY_BACKSPACE) {
2366 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2369 if (view->searching == TOG_SEARCH_FORWARD)
2370 entry = TAILQ_NEXT(s->search_entry, entry);
2372 entry = TAILQ_PREV(s->search_entry,
2373 commit_queue_head, entry);
2374 } else if (s->matched_entry) {
2375 if (view->searching == TOG_SEARCH_FORWARD)
2376 entry = TAILQ_NEXT(s->matched_entry, entry);
2378 entry = TAILQ_PREV(s->matched_entry,
2379 commit_queue_head, entry);
2381 entry = s->selected_entry;
2387 if (entry == NULL) {
2388 if (s->thread_args.log_complete ||
2389 view->searching == TOG_SEARCH_BACKWARD) {
2390 view->search_next_done =
2391 (s->matched_entry == NULL ?
2392 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2393 s->search_entry = NULL;
2397 * Poke the log thread for more commits and return,
2398 * allowing the main loop to make progress. Search
2399 * will resume at s->search_entry once we come back.
2401 s->thread_args.commits_needed++;
2402 return trigger_log_thread(view, 0);
2405 err = match_commit(&have_match, entry->id, entry->commit,
2410 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2411 s->matched_entry = entry;
2415 s->search_entry = entry;
2416 if (view->searching == TOG_SEARCH_FORWARD)
2417 entry = TAILQ_NEXT(entry, entry);
2419 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2422 if (s->matched_entry) {
2423 int cur = s->selected_entry->idx;
2424 while (cur < s->matched_entry->idx) {
2425 err = input_log_view(NULL, view, KEY_DOWN);
2430 while (cur > s->matched_entry->idx) {
2431 err = input_log_view(NULL, view, KEY_UP);
2438 s->search_entry = NULL;
2443 static const struct got_error *
2444 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2445 struct got_repository *repo, const char *head_ref_name,
2446 const char *in_repo_path, int log_branches)
2448 const struct got_error *err = NULL;
2449 struct tog_log_view_state *s = &view->state.log;
2450 struct got_repository *thread_repo = NULL;
2451 struct got_commit_graph *thread_graph = NULL;
2454 if (in_repo_path != s->in_repo_path) {
2455 free(s->in_repo_path);
2456 s->in_repo_path = strdup(in_repo_path);
2457 if (s->in_repo_path == NULL)
2458 return got_error_from_errno("strdup");
2461 /* The commit queue only contains commits being displayed. */
2462 TAILQ_INIT(&s->commits.head);
2463 s->commits.ncommits = 0;
2466 if (head_ref_name) {
2467 s->head_ref_name = strdup(head_ref_name);
2468 if (s->head_ref_name == NULL) {
2469 err = got_error_from_errno("strdup");
2473 s->start_id = got_object_id_dup(start_id);
2474 if (s->start_id == NULL) {
2475 err = got_error_from_errno("got_object_id_dup");
2478 s->log_branches = log_branches;
2480 STAILQ_INIT(&s->colors);
2481 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2482 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2483 get_color_value("TOG_COLOR_COMMIT"));
2486 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2487 get_color_value("TOG_COLOR_AUTHOR"));
2489 free_colors(&s->colors);
2492 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2493 get_color_value("TOG_COLOR_DATE"));
2495 free_colors(&s->colors);
2500 view->show = show_log_view;
2501 view->input = input_log_view;
2502 view->close = close_log_view;
2503 view->search_start = search_start_log_view;
2504 view->search_next = search_next_log_view;
2506 if (s->thread_args.pack_fds == NULL) {
2507 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2511 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2512 s->thread_args.pack_fds);
2515 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2519 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2520 s->repo, NULL, NULL);
2524 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2526 err = got_error_set_errno(errcode, "pthread_cond_init");
2529 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2531 err = got_error_set_errno(errcode, "pthread_cond_init");
2535 s->thread_args.commits_needed = view->nlines;
2536 s->thread_args.graph = thread_graph;
2537 s->thread_args.commits = &s->commits;
2538 s->thread_args.in_repo_path = s->in_repo_path;
2539 s->thread_args.start_id = s->start_id;
2540 s->thread_args.repo = thread_repo;
2541 s->thread_args.log_complete = 0;
2542 s->thread_args.quit = &s->quit;
2543 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2544 s->thread_args.selected_entry = &s->selected_entry;
2545 s->thread_args.searching = &view->searching;
2546 s->thread_args.search_next_done = &view->search_next_done;
2547 s->thread_args.regex = &view->regex;
2550 close_log_view(view);
2554 static const struct got_error *
2555 show_log_view(struct tog_view *view)
2557 const struct got_error *err;
2558 struct tog_log_view_state *s = &view->state.log;
2560 if (s->thread == NULL) {
2561 int errcode = pthread_create(&s->thread, NULL, log_thread,
2564 return got_error_set_errno(errcode, "pthread_create");
2565 if (s->thread_args.commits_needed > 0) {
2566 err = trigger_log_thread(view, 1);
2572 return draw_commits(view);
2575 static const struct got_error *
2576 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2578 const struct got_error *err = NULL;
2579 struct tog_log_view_state *s = &view->state.log;
2580 struct tog_view *diff_view = NULL, *tree_view = NULL;
2581 struct tog_view *ref_view = NULL;
2582 struct commit_queue_entry *entry;
2583 int begin_x = 0, n, nscroll = view->nlines - 1;
2585 if (s->thread_args.load_all) {
2586 if (ch == KEY_BACKSPACE)
2587 s->thread_args.load_all = 0;
2588 else if (s->thread_args.log_complete) {
2589 s->thread_args.load_all = 0;
2590 log_scroll_down(view, s->commits.ncommits);
2591 s->selected = MIN(view->nlines - 2,
2592 s->commits.ncommits - 1);
2606 view->x = MAX(view->maxx - view->ncols / 2, 0);
2610 if (view->x + view->ncols / 2 < view->maxx)
2611 view->x += 2; /* move two columns right */
2615 view->x -= MIN(view->x, 2); /* move two columns back */
2622 if (s->first_displayed_entry == NULL)
2624 if (s->selected > 0)
2627 log_scroll_up(s, 1);
2633 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2642 if (s->first_displayed_entry == NULL)
2644 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2645 s->selected = MAX(0, s->selected - nscroll - 1);
2647 log_scroll_up(s, nscroll);
2655 if (s->first_displayed_entry == NULL)
2657 if (s->selected < MIN(view->nlines - 2,
2658 s->commits.ncommits - 1))
2661 err = log_scroll_down(view, 1);
2669 /* We don't know yet how many commits, so we're forced to
2670 * traverse them all. */
2671 if (!s->thread_args.log_complete) {
2672 s->thread_args.load_all = 1;
2673 return trigger_log_thread(view, 0);
2677 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2678 for (n = 0; n < view->nlines - 1; n++) {
2681 s->first_displayed_entry = entry;
2682 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2685 s->selected = n - 1;
2695 struct commit_queue_entry *first;
2696 first = s->first_displayed_entry;
2699 err = log_scroll_down(view, nscroll);
2702 if (first == s->first_displayed_entry &&
2703 s->selected < MIN(view->nlines - 2,
2704 s->commits.ncommits - 1)) {
2705 /* can't scroll further down */
2706 s->selected += MIN(s->last_displayed_entry->idx -
2707 s->selected_entry->idx, nscroll + 1);
2713 if (s->selected > view->nlines - 2)
2714 s->selected = view->nlines - 2;
2715 if (s->selected > s->commits.ncommits - 1)
2716 s->selected = s->commits.ncommits - 1;
2718 if (s->commits.ncommits < view->nlines - 1 &&
2719 !s->thread_args.log_complete) {
2720 s->thread_args.commits_needed += (view->nlines - 1) -
2721 s->commits.ncommits;
2722 err = trigger_log_thread(view, 1);
2728 if (s->selected_entry == NULL)
2730 if (view_is_parent_view(view))
2731 begin_x = view_split_begin_x(view->begin_x);
2732 err = open_diff_view_for_commit(&diff_view, begin_x,
2733 s->selected_entry->commit, s->selected_entry->id,
2738 diff_view->focussed = 1;
2739 if (view_is_parent_view(view)) {
2740 err = view_close_child(view);
2743 view_set_child(view, diff_view);
2744 view->focus_child = 1;
2746 *new_view = diff_view;
2749 if (s->selected_entry == NULL)
2751 if (view_is_parent_view(view))
2752 begin_x = view_split_begin_x(view->begin_x);
2753 err = browse_commit_tree(&tree_view, begin_x,
2754 s->selected_entry, s->in_repo_path, s->head_ref_name,
2759 tree_view->focussed = 1;
2760 if (view_is_parent_view(view)) {
2761 err = view_close_child(view);
2764 view_set_child(view, tree_view);
2765 view->focus_child = 1;
2767 *new_view = tree_view;
2772 if (ch == KEY_BACKSPACE &&
2773 got_path_is_root_dir(s->in_repo_path))
2775 err = stop_log_thread(s);
2778 if (ch == KEY_BACKSPACE) {
2780 err = got_path_dirname(&parent_path, s->in_repo_path);
2783 free(s->in_repo_path);
2784 s->in_repo_path = parent_path;
2785 s->thread_args.in_repo_path = s->in_repo_path;
2786 } else if (ch == CTRL('l')) {
2787 struct got_object_id *start_id;
2788 err = got_repo_match_object_id(&start_id, NULL,
2789 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2790 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2794 s->start_id = start_id;
2795 s->thread_args.start_id = s->start_id;
2797 s->log_branches = !s->log_branches;
2799 if (s->thread_args.pack_fds == NULL) {
2800 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2804 err = got_repo_open(&s->thread_args.repo,
2805 got_repo_get_path(s->repo), NULL,
2806 s->thread_args.pack_fds);
2810 err = tog_load_refs(s->repo, 0);
2813 err = got_commit_graph_open(&s->thread_args.graph,
2814 s->in_repo_path, !s->log_branches);
2817 err = got_commit_graph_iter_start(s->thread_args.graph,
2818 s->start_id, s->repo, NULL, NULL);
2821 free_commits(&s->commits);
2822 s->first_displayed_entry = NULL;
2823 s->last_displayed_entry = NULL;
2824 s->selected_entry = NULL;
2826 s->thread_args.log_complete = 0;
2828 s->thread_args.commits_needed = view->nlines;
2831 if (view_is_parent_view(view))
2832 begin_x = view_split_begin_x(view->begin_x);
2833 ref_view = view_open(view->nlines, view->ncols,
2834 view->begin_y, begin_x, TOG_VIEW_REF);
2835 if (ref_view == NULL)
2836 return got_error_from_errno("view_open");
2837 err = open_ref_view(ref_view, s->repo);
2839 view_close(ref_view);
2843 ref_view->focussed = 1;
2844 if (view_is_parent_view(view)) {
2845 err = view_close_child(view);
2848 view_set_child(view, ref_view);
2849 view->focus_child = 1;
2851 *new_view = ref_view;
2860 static const struct got_error *
2861 apply_unveil(const char *repo_path, const char *worktree_path)
2863 const struct got_error *error;
2866 if (unveil("gmon.out", "rwc") != 0)
2867 return got_error_from_errno2("unveil", "gmon.out");
2869 if (repo_path && unveil(repo_path, "r") != 0)
2870 return got_error_from_errno2("unveil", repo_path);
2872 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2873 return got_error_from_errno2("unveil", worktree_path);
2875 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2876 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2878 error = got_privsep_unveil_exec_helpers();
2882 if (unveil(NULL, NULL) != 0)
2883 return got_error_from_errno("unveil");
2892 * Override default signal handlers before starting ncurses.
2893 * This should prevent ncurses from installing its own
2894 * broken cleanup() signal handler.
2896 signal(SIGWINCH, tog_sigwinch);
2897 signal(SIGPIPE, tog_sigpipe);
2898 signal(SIGCONT, tog_sigcont);
2899 signal(SIGINT, tog_sigint);
2900 signal(SIGTERM, tog_sigterm);
2904 halfdelay(1); /* Do fast refresh while initial view is loading. */
2907 intrflush(stdscr, FALSE);
2908 keypad(stdscr, TRUE);
2910 if (getenv("TOG_COLORS") != NULL) {
2912 use_default_colors();
2916 static const struct got_error *
2917 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2918 struct got_repository *repo, struct got_worktree *worktree)
2920 const struct got_error *err = NULL;
2923 *in_repo_path = strdup("/");
2924 if (*in_repo_path == NULL)
2925 return got_error_from_errno("strdup");
2930 const char *prefix = got_worktree_get_path_prefix(worktree);
2933 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2936 if (asprintf(in_repo_path, "%s%s%s", prefix,
2937 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2939 err = got_error_from_errno("asprintf");
2940 *in_repo_path = NULL;
2944 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2949 static const struct got_error *
2950 cmd_log(int argc, char *argv[])
2952 const struct got_error *error;
2953 struct got_repository *repo = NULL;
2954 struct got_worktree *worktree = NULL;
2955 struct got_object_id *start_id = NULL;
2956 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2957 char *start_commit = NULL, *label = NULL;
2958 struct got_reference *ref = NULL;
2959 const char *head_ref_name = NULL;
2960 int ch, log_branches = 0;
2961 struct tog_view *view;
2962 int *pack_fds = NULL;
2964 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2970 start_commit = optarg;
2973 repo_path = realpath(optarg, NULL);
2974 if (repo_path == NULL)
2975 return got_error_from_errno2("realpath",
2990 error = got_repo_pack_fds_open(&pack_fds);
2994 if (repo_path == NULL) {
2995 cwd = getcwd(NULL, 0);
2997 return got_error_from_errno("getcwd");
2998 error = got_worktree_open(&worktree, cwd);
2999 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3003 strdup(got_worktree_get_repo_path(worktree));
3005 repo_path = strdup(cwd);
3006 if (repo_path == NULL) {
3007 error = got_error_from_errno("strdup");
3012 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3016 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3023 error = apply_unveil(got_repo_get_path(repo),
3024 worktree ? got_worktree_get_root_path(worktree) : NULL);
3028 /* already loaded by tog_log_with_path()? */
3029 if (TAILQ_EMPTY(&tog_refs)) {
3030 error = tog_load_refs(repo, 0);
3035 if (start_commit == NULL) {
3036 error = got_repo_match_object_id(&start_id, &label,
3037 worktree ? got_worktree_get_head_ref_name(worktree) :
3038 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3041 head_ref_name = label;
3043 error = got_ref_open(&ref, repo, start_commit, 0);
3045 head_ref_name = got_ref_get_name(ref);
3046 else if (error->code != GOT_ERR_NOT_REF)
3048 error = got_repo_match_object_id(&start_id, NULL,
3049 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3054 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3056 error = got_error_from_errno("view_open");
3059 error = open_log_view(view, start_id, repo, head_ref_name,
3060 in_repo_path, log_branches);
3064 /* Release work tree lock. */
3065 got_worktree_close(worktree);
3068 error = view_loop(view);
3078 const struct got_error *close_err = got_repo_close(repo);
3083 got_worktree_close(worktree);
3085 const struct got_error *pack_err =
3086 got_repo_pack_fds_close(pack_fds);
3098 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3099 "[-w] object1 object2\n", getprogname());
3104 match_line(const char *line, regex_t *regex, size_t nmatch,
3105 regmatch_t *regmatch)
3107 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3111 match_color(struct tog_colors *colors, const char *line)
3113 struct tog_color *tc = NULL;
3115 STAILQ_FOREACH(tc, colors, entry) {
3116 if (match_line(line, &tc->regex, 0, NULL))
3123 static const struct got_error *
3124 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3125 WINDOW *window, int skip, regmatch_t *regmatch)
3127 const struct got_error *err = NULL;
3129 int rme, rms, n, width;
3132 rms = regmatch->rm_so;
3133 rme = regmatch->rm_eo;
3135 err = format_line(&wline, &width, NULL, line, 0, wlimit + skip,
3140 /* draw up to matched token if we haven't scrolled past it */
3141 n = MAX(rms - skip, 0);
3143 waddnwstr(window, wline + skip, n);
3149 int len = rme - rms;
3153 len = MAX(len - n, 0);
3155 /* draw (visible part of) matched token (if scrolled into it) */
3157 wattron(window, A_STANDOUT);
3158 waddnwstr(window, wline + rms + n, len);
3159 wattroff(window, A_STANDOUT);
3165 if (wlimit > 0 && skip < width) { /* draw rest of line */
3168 n = MIN(skip - rme, width - rme);
3169 waddnwstr(window, wline + rme + n, wlimit);
3177 static const struct got_error *
3178 draw_file(struct tog_view *view, const char *header)
3180 struct tog_diff_view_state *s = &view->state.diff;
3181 regmatch_t *regmatch = &view->regmatch;
3182 const struct got_error *err;
3185 size_t linesize = 0;
3187 struct tog_color *tc;
3190 int max_lines = view->nlines;
3191 int nlines = s->nlines;
3194 line_offset = s->line_offsets[s->first_displayed_line - 1];
3195 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3196 return got_error_from_errno("fseek");
3198 werase(view->window);
3201 if (asprintf(&line, "[%d/%d] %s",
3202 s->first_displayed_line - 1 + s->selected_line, nlines,
3204 return got_error_from_errno("asprintf");
3205 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3211 if (view_needs_focus_indication(view))
3212 wstandout(view->window);
3213 waddwstr(view->window, wline);
3216 if (view_needs_focus_indication(view))
3217 wstandend(view->window);
3218 if (width <= view->ncols - 1)
3219 waddch(view->window, '\n');
3229 while (max_lines > 0 && nprinted < max_lines) {
3230 linelen = getline(&line, &linesize, s->f);
3231 if (linelen == -1) {
3237 return got_ferror(s->f, GOT_ERR_IO);
3240 view->maxx = MAX(view->maxx, linelen);
3242 tc = match_color(&s->colors, line);
3244 wattr_on(view->window,
3245 COLOR_PAIR(tc->colorpair), NULL);
3246 if (s->first_displayed_line + nprinted == s->matched_line &&
3247 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3248 err = add_matched_line(&width, line, view->ncols, 0,
3249 view->window, view->x, regmatch);
3255 err = format_line(&wline, &width, NULL, line, 0,
3256 view->x + view->ncols, 0, view->x ? 1 : 0);
3261 if (view->x < width)
3262 waddwstr(view->window, wline + view->x);
3267 wattr_off(view->window,
3268 COLOR_PAIR(tc->colorpair), NULL);
3269 if (width - view->x <= view->ncols - 1)
3270 waddch(view->window, '\n');
3275 s->last_displayed_line = s->first_displayed_line +
3278 s->last_displayed_line = s->first_displayed_line;
3283 while (nprinted < view->nlines) {
3284 waddch(view->window, '\n');
3288 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3294 wstandout(view->window);
3295 waddwstr(view->window, wline);
3298 wstandend(view->window);
3305 get_datestr(time_t *time, char *datebuf)
3307 struct tm mytm, *tm;
3310 tm = gmtime_r(time, &mytm);
3313 s = asctime_r(tm, datebuf);
3316 p = strchr(s, '\n');
3322 static const struct got_error *
3323 get_changed_paths(struct got_pathlist_head *paths,
3324 struct got_commit_object *commit, struct got_repository *repo)
3326 const struct got_error *err = NULL;
3327 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3328 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3329 struct got_object_qid *qid;
3331 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3333 struct got_commit_object *pcommit;
3334 err = got_object_open_as_commit(&pcommit, repo,
3339 tree_id1 = got_object_id_dup(
3340 got_object_commit_get_tree_id(pcommit));
3341 if (tree_id1 == NULL) {
3342 got_object_commit_close(pcommit);
3343 return got_error_from_errno("got_object_id_dup");
3345 got_object_commit_close(pcommit);
3350 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3355 tree_id2 = got_object_commit_get_tree_id(commit);
3356 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3360 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3361 got_diff_tree_collect_changed_paths, paths, 0);
3364 got_object_tree_close(tree1);
3366 got_object_tree_close(tree2);
3371 static const struct got_error *
3372 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3376 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3378 return got_error_from_errno("reallocarray");
3380 (*line_offsets)[*nlines] = off;
3385 static const struct got_error *
3386 write_commit_info(off_t **line_offsets, size_t *nlines,
3387 struct got_object_id *commit_id, struct got_reflist_head *refs,
3388 struct got_repository *repo, FILE *outfile)
3390 const struct got_error *err = NULL;
3391 char datebuf[26], *datestr;
3392 struct got_commit_object *commit;
3393 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3394 time_t committer_time;
3395 const char *author, *committer;
3396 char *refs_str = NULL;
3397 struct got_pathlist_head changed_paths;
3398 struct got_pathlist_entry *pe;
3402 TAILQ_INIT(&changed_paths);
3405 err = build_refs_str(&refs_str, refs, commit_id, repo);
3410 err = got_object_open_as_commit(&commit, repo, commit_id);
3414 err = got_object_id_str(&id_str, commit_id);
3416 err = got_error_from_errno("got_object_id_str");
3420 err = add_line_offset(line_offsets, nlines, 0);
3424 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3425 refs_str ? refs_str : "", refs_str ? ")" : "");
3427 err = got_error_from_errno("fprintf");
3431 err = add_line_offset(line_offsets, nlines, outoff);
3435 n = fprintf(outfile, "from: %s\n",
3436 got_object_commit_get_author(commit));
3438 err = got_error_from_errno("fprintf");
3442 err = add_line_offset(line_offsets, nlines, outoff);
3446 committer_time = got_object_commit_get_committer_time(commit);
3447 datestr = get_datestr(&committer_time, datebuf);
3449 n = fprintf(outfile, "date: %s UTC\n", datestr);
3451 err = got_error_from_errno("fprintf");
3455 err = add_line_offset(line_offsets, nlines, outoff);
3459 author = got_object_commit_get_author(commit);
3460 committer = got_object_commit_get_committer(commit);
3461 if (strcmp(author, committer) != 0) {
3462 n = fprintf(outfile, "via: %s\n", committer);
3464 err = got_error_from_errno("fprintf");
3468 err = add_line_offset(line_offsets, nlines, outoff);
3472 if (got_object_commit_get_nparents(commit) > 1) {
3473 const struct got_object_id_queue *parent_ids;
3474 struct got_object_qid *qid;
3476 parent_ids = got_object_commit_get_parent_ids(commit);
3477 STAILQ_FOREACH(qid, parent_ids, entry) {
3478 err = got_object_id_str(&id_str, &qid->id);
3481 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3483 err = got_error_from_errno("fprintf");
3487 err = add_line_offset(line_offsets, nlines, outoff);
3495 err = got_object_commit_get_logmsg(&logmsg, commit);
3499 while ((line = strsep(&s, "\n")) != NULL) {
3500 n = fprintf(outfile, "%s\n", line);
3502 err = got_error_from_errno("fprintf");
3506 err = add_line_offset(line_offsets, nlines, outoff);
3511 err = get_changed_paths(&changed_paths, commit, repo);
3514 TAILQ_FOREACH(pe, &changed_paths, entry) {
3515 struct got_diff_changed_path *cp = pe->data;
3516 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3518 err = got_error_from_errno("fprintf");
3522 err = add_line_offset(line_offsets, nlines, outoff);
3525 free((char *)pe->path);
3529 fputc('\n', outfile);
3531 err = add_line_offset(line_offsets, nlines, outoff);
3533 got_pathlist_free(&changed_paths);
3537 got_object_commit_close(commit);
3539 free(*line_offsets);
3540 *line_offsets = NULL;
3546 static const struct got_error *
3547 create_diff(struct tog_diff_view_state *s)
3549 const struct got_error *err = NULL;
3553 free(s->line_offsets);
3554 s->line_offsets = malloc(sizeof(off_t));
3555 if (s->line_offsets == NULL)
3556 return got_error_from_errno("malloc");
3561 err = got_error_from_errno("got_opentemp");
3564 if (s->f && fclose(s->f) == EOF) {
3565 err = got_error_from_errno("fclose");
3571 err = got_object_get_type(&obj_type, s->repo, s->id1);
3573 err = got_object_get_type(&obj_type, s->repo, s->id2);
3578 case GOT_OBJ_TYPE_BLOB:
3579 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3580 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3581 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3584 case GOT_OBJ_TYPE_TREE:
3585 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3586 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3587 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3589 case GOT_OBJ_TYPE_COMMIT: {
3590 const struct got_object_id_queue *parent_ids;
3591 struct got_object_qid *pid;
3592 struct got_commit_object *commit2;
3593 struct got_reflist_head *refs;
3595 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3598 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3599 /* Show commit info if we're diffing to a parent/root commit. */
3600 if (s->id1 == NULL) {
3601 err = write_commit_info(&s->line_offsets, &s->nlines,
3602 s->id2, refs, s->repo, s->f);
3606 parent_ids = got_object_commit_get_parent_ids(commit2);
3607 STAILQ_FOREACH(pid, parent_ids, entry) {
3608 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3609 err = write_commit_info(
3610 &s->line_offsets, &s->nlines,
3611 s->id2, refs, s->repo, s->f);
3618 got_object_commit_close(commit2);
3620 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3621 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3622 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3626 err = got_error(GOT_ERR_OBJ_TYPE);
3632 if (s->f && fflush(s->f) != 0 && err == NULL)
3633 err = got_error_from_errno("fflush");
3638 diff_view_indicate_progress(struct tog_view *view)
3640 mvwaddstr(view->window, 0, 0, "diffing...");
3645 static const struct got_error *
3646 search_start_diff_view(struct tog_view *view)
3648 struct tog_diff_view_state *s = &view->state.diff;
3650 s->matched_line = 0;
3654 static const struct got_error *
3655 search_next_diff_view(struct tog_view *view)
3657 struct tog_diff_view_state *s = &view->state.diff;
3658 const struct got_error *err = NULL;
3660 char *exstr = NULL, *line = NULL;
3661 size_t linesize = 0;
3664 if (!view->searching) {
3665 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3669 if (s->matched_line) {
3670 if (view->searching == TOG_SEARCH_FORWARD)
3671 lineno = s->matched_line + 1;
3673 lineno = s->matched_line - 1;
3675 lineno = s->first_displayed_line;
3680 if (lineno <= 0 || lineno > s->nlines) {
3681 if (s->matched_line == 0) {
3682 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3686 if (view->searching == TOG_SEARCH_FORWARD)
3692 offset = s->line_offsets[lineno - 1];
3693 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3695 return got_error_from_errno("fseeko");
3697 linelen = getline(&line, &linesize, s->f);
3698 err = expand_tab(&exstr, line);
3701 if (linelen != -1 &&
3702 match_line(exstr, &view->regex, 1, &view->regmatch)) {
3703 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3704 s->matched_line = lineno;
3709 if (view->searching == TOG_SEARCH_FORWARD)
3717 if (s->matched_line) {
3718 s->first_displayed_line = s->matched_line;
3719 s->selected_line = 1;
3725 static const struct got_error *
3726 close_diff_view(struct tog_view *view)
3728 const struct got_error *err = NULL;
3729 struct tog_diff_view_state *s = &view->state.diff;
3735 if (s->f && fclose(s->f) == EOF)
3736 err = got_error_from_errno("fclose");
3738 if (s->f1 && fclose(s->f1) == EOF)
3739 err = got_error_from_errno("fclose");
3741 if (s->f2 && fclose(s->f2) == EOF)
3742 err = got_error_from_errno("fclose");
3744 free_colors(&s->colors);
3745 free(s->line_offsets);
3746 s->line_offsets = NULL;
3751 static const struct got_error *
3752 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3753 struct got_object_id *id2, const char *label1, const char *label2,
3754 int diff_context, int ignore_whitespace, int force_text_diff,
3755 struct tog_view *log_view, struct got_repository *repo)
3757 const struct got_error *err;
3758 struct tog_diff_view_state *s = &view->state.diff;
3760 memset(s, 0, sizeof(*s));
3762 if (id1 != NULL && id2 != NULL) {
3764 err = got_object_get_type(&type1, repo, id1);
3767 err = got_object_get_type(&type2, repo, id2);
3772 return got_error(GOT_ERR_OBJ_TYPE);
3774 s->first_displayed_line = 1;
3775 s->last_displayed_line = view->nlines;
3776 s->selected_line = 1;
3784 s->id1 = got_object_id_dup(id1);
3786 return got_error_from_errno("got_object_id_dup");
3787 s->f1 = got_opentemp();
3788 if (s->f1 == NULL) {
3789 err = got_error_from_errno("got_opentemp");
3795 s->id2 = got_object_id_dup(id2);
3796 if (s->id2 == NULL) {
3797 err = got_error_from_errno("got_object_id_dup");
3801 s->f2 = got_opentemp();
3802 if (s->f2 == NULL) {
3803 err = got_error_from_errno("got_opentemp");
3807 s->first_displayed_line = 1;
3808 s->last_displayed_line = view->nlines;
3809 s->diff_context = diff_context;
3810 s->ignore_whitespace = ignore_whitespace;
3811 s->force_text_diff = force_text_diff;
3812 s->log_view = log_view;
3815 STAILQ_INIT(&s->colors);
3816 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3817 err = add_color(&s->colors,
3818 "^-", TOG_COLOR_DIFF_MINUS,
3819 get_color_value("TOG_COLOR_DIFF_MINUS"));
3822 err = add_color(&s->colors, "^\\+",
3823 TOG_COLOR_DIFF_PLUS,
3824 get_color_value("TOG_COLOR_DIFF_PLUS"));
3827 err = add_color(&s->colors,
3828 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3829 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3833 err = add_color(&s->colors,
3834 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3835 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3836 get_color_value("TOG_COLOR_DIFF_META"));
3840 err = add_color(&s->colors,
3841 "^(from|via): ", TOG_COLOR_AUTHOR,
3842 get_color_value("TOG_COLOR_AUTHOR"));
3846 err = add_color(&s->colors,
3847 "^date: ", TOG_COLOR_DATE,
3848 get_color_value("TOG_COLOR_DATE"));
3853 if (log_view && view_is_splitscreen(view))
3854 show_log_view(log_view); /* draw vborder */
3855 diff_view_indicate_progress(view);
3857 err = create_diff(s);
3859 view->show = show_diff_view;
3860 view->input = input_diff_view;
3861 view->close = close_diff_view;
3862 view->search_start = search_start_diff_view;
3863 view->search_next = search_next_diff_view;
3866 close_diff_view(view);
3870 static const struct got_error *
3871 show_diff_view(struct tog_view *view)
3873 const struct got_error *err;
3874 struct tog_diff_view_state *s = &view->state.diff;
3875 char *id_str1 = NULL, *id_str2, *header;
3876 const char *label1, *label2;
3879 err = got_object_id_str(&id_str1, s->id1);
3882 label1 = s->label1 ? : id_str1;
3884 label1 = "/dev/null";
3886 err = got_object_id_str(&id_str2, s->id2);
3889 label2 = s->label2 ? : id_str2;
3891 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3892 err = got_error_from_errno("asprintf");
3900 err = draw_file(view, header);
3905 static const struct got_error *
3906 set_selected_commit(struct tog_diff_view_state *s,
3907 struct commit_queue_entry *entry)
3909 const struct got_error *err;
3910 const struct got_object_id_queue *parent_ids;
3911 struct got_commit_object *selected_commit;
3912 struct got_object_qid *pid;
3915 s->id2 = got_object_id_dup(entry->id);
3917 return got_error_from_errno("got_object_id_dup");
3919 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3922 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3924 pid = STAILQ_FIRST(parent_ids);
3925 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3926 got_object_commit_close(selected_commit);
3930 static const struct got_error *
3931 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3933 const struct got_error *err = NULL;
3934 struct tog_diff_view_state *s = &view->state.diff;
3935 struct tog_log_view_state *ls;
3936 struct commit_queue_entry *old_selected_entry;
3938 size_t linesize = 0;
3940 int i, nscroll = view->nlines - 1;
3947 view->x = MAX(view->maxx - view->ncols / 3, 0);
3951 if (view->x + view->ncols / 3 < view->maxx)
3952 view->x += 2; /* move two columns right */
3956 view->x -= MIN(view->x, 2); /* move two columns back */
3961 s->force_text_diff = !s->force_text_diff;
3963 s->ignore_whitespace = !s->ignore_whitespace;
3964 wclear(view->window);
3965 s->first_displayed_line = 1;
3966 s->last_displayed_line = view->nlines;
3967 s->matched_line = 0;
3968 diff_view_indicate_progress(view);
3969 err = create_diff(s);
3973 s->first_displayed_line = 1;
3980 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3986 if (s->first_displayed_line > 1)
3987 s->first_displayed_line--;
3995 if (s->first_displayed_line == 1)
3998 while (i++ < nscroll && s->first_displayed_line > 1)
3999 s->first_displayed_line--;
4005 s->first_displayed_line++;
4017 while (!s->eof && i++ < nscroll) {
4018 linelen = getline(&line, &linesize, s->f);
4019 s->first_displayed_line++;
4020 if (linelen == -1) {
4024 err = got_ferror(s->f, GOT_ERR_IO);
4031 if (s->diff_context > 0) {
4033 s->matched_line = 0;
4034 diff_view_indicate_progress(view);
4035 err = create_diff(s);
4036 if (s->first_displayed_line + view->nlines - 1 >
4038 s->first_displayed_line = 1;
4039 s->last_displayed_line = view->nlines;
4044 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4046 s->matched_line = 0;
4047 diff_view_indicate_progress(view);
4048 err = create_diff(s);
4053 if (s->log_view == NULL)
4055 ls = &s->log_view->state.log;
4056 old_selected_entry = ls->selected_entry;
4058 err = input_log_view(NULL, s->log_view, KEY_UP);
4062 if (old_selected_entry == ls->selected_entry)
4065 err = set_selected_commit(s, ls->selected_entry);
4069 s->first_displayed_line = 1;
4070 s->last_displayed_line = view->nlines;
4071 s->matched_line = 0;
4074 diff_view_indicate_progress(view);
4075 err = create_diff(s);
4079 if (s->log_view == NULL)
4081 ls = &s->log_view->state.log;
4082 old_selected_entry = ls->selected_entry;
4084 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4088 if (old_selected_entry == ls->selected_entry)
4091 err = set_selected_commit(s, ls->selected_entry);
4095 s->first_displayed_line = 1;
4096 s->last_displayed_line = view->nlines;
4097 s->matched_line = 0;
4100 diff_view_indicate_progress(view);
4101 err = create_diff(s);
4110 static const struct got_error *
4111 cmd_diff(int argc, char *argv[])
4113 const struct got_error *error = NULL;
4114 struct got_repository *repo = NULL;
4115 struct got_worktree *worktree = NULL;
4116 struct got_object_id *id1 = NULL, *id2 = NULL;
4117 char *repo_path = NULL, *cwd = NULL;
4118 char *id_str1 = NULL, *id_str2 = NULL;
4119 char *label1 = NULL, *label2 = NULL;
4120 int diff_context = 3, ignore_whitespace = 0;
4121 int ch, force_text_diff = 0;
4123 struct tog_view *view;
4124 int *pack_fds = NULL;
4126 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4129 force_text_diff = 1;
4132 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4135 errx(1, "number of context lines is %s: %s",
4139 repo_path = realpath(optarg, NULL);
4140 if (repo_path == NULL)
4141 return got_error_from_errno2("realpath",
4143 got_path_strip_trailing_slashes(repo_path);
4146 ignore_whitespace = 1;
4158 usage_diff(); /* TODO show local worktree changes */
4159 } else if (argc == 2) {
4165 error = got_repo_pack_fds_open(&pack_fds);
4169 if (repo_path == NULL) {
4170 cwd = getcwd(NULL, 0);
4172 return got_error_from_errno("getcwd");
4173 error = got_worktree_open(&worktree, cwd);
4174 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4178 strdup(got_worktree_get_repo_path(worktree));
4180 repo_path = strdup(cwd);
4181 if (repo_path == NULL) {
4182 error = got_error_from_errno("strdup");
4187 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4193 error = apply_unveil(got_repo_get_path(repo), NULL);
4197 error = tog_load_refs(repo, 0);
4201 error = got_repo_match_object_id(&id1, &label1, id_str1,
4202 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4206 error = got_repo_match_object_id(&id2, &label2, id_str2,
4207 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4211 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4213 error = got_error_from_errno("view_open");
4216 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4217 ignore_whitespace, force_text_diff, NULL, repo);
4220 error = view_loop(view);
4227 const struct got_error *close_err = got_repo_close(repo);
4232 got_worktree_close(worktree);
4234 const struct got_error *pack_err =
4235 got_repo_pack_fds_close(pack_fds);
4247 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4252 struct tog_blame_line {
4254 struct got_object_id *id;
4257 static const struct got_error *
4258 draw_blame(struct tog_view *view)
4260 struct tog_blame_view_state *s = &view->state.blame;
4261 struct tog_blame *blame = &s->blame;
4262 regmatch_t *regmatch = &view->regmatch;
4263 const struct got_error *err;
4264 int lineno = 0, nprinted = 0, i;
4266 size_t linesize = 0;
4270 struct tog_blame_line *blame_line;
4271 struct got_object_id *prev_id = NULL;
4273 struct tog_color *tc;
4275 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4280 werase(view->window);
4282 if (asprintf(&line, "commit %s", id_str) == -1) {
4283 err = got_error_from_errno("asprintf");
4288 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4293 if (view_needs_focus_indication(view))
4294 wstandout(view->window);
4295 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4297 wattr_on(view->window,
4298 COLOR_PAIR(tc->colorpair), NULL);
4299 waddwstr(view->window, wline);
4301 wattr_off(view->window,
4302 COLOR_PAIR(tc->colorpair), NULL);
4303 if (view_needs_focus_indication(view))
4304 wstandend(view->window);
4307 if (width < view->ncols - 1)
4308 waddch(view->window, '\n');
4310 if (asprintf(&line, "[%d/%d] %s%s",
4311 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4312 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4314 return got_error_from_errno("asprintf");
4317 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4322 waddwstr(view->window, wline);
4325 if (width < view->ncols - 1)
4326 waddch(view->window, '\n');
4330 while (nprinted < view->nlines - 2) {
4331 linelen = getline(&line, &linesize, blame->f);
4332 if (linelen == -1) {
4333 if (feof(blame->f)) {
4338 return got_ferror(blame->f, GOT_ERR_IO);
4340 if (++lineno < s->first_displayed_line)
4343 view->maxx = MAX(view->maxx, linelen);
4345 if (view->focussed && nprinted == s->selected_line - 1)
4346 wstandout(view->window);
4348 if (blame->nlines > 0) {
4349 blame_line = &blame->lines[lineno - 1];
4350 if (blame_line->annotated && prev_id &&
4351 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4353 nprinted == s->selected_line - 1)) {
4354 waddstr(view->window, " ");
4355 } else if (blame_line->annotated) {
4357 err = got_object_id_str(&id_str, blame_line->id);
4362 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4364 wattr_on(view->window,
4365 COLOR_PAIR(tc->colorpair), NULL);
4366 wprintw(view->window, "%.8s", id_str);
4368 wattr_off(view->window,
4369 COLOR_PAIR(tc->colorpair), NULL);
4371 prev_id = blame_line->id;
4373 waddstr(view->window, "........");
4377 waddstr(view->window, "........");
4381 if (view->focussed && nprinted == s->selected_line - 1)
4382 wstandend(view->window);
4383 waddstr(view->window, " ");
4385 if (view->ncols <= 9) {
4387 wline = wcsdup(L"");
4388 if (wline == NULL) {
4389 err = got_error_from_errno("wcsdup");
4393 } else if (s->first_displayed_line + nprinted ==
4395 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4396 err = add_matched_line(&width, line, view->ncols - 9, 9,
4397 view->window, view->x, regmatch);
4404 err = format_line(&wline, &width, NULL, line, 0,
4405 view->x + view->ncols - 9, 9, 1);
4410 if (view->x < width) {
4411 waddwstr(view->window, wline + view->x);
4412 for (i = 0; i < view->x; i++)
4413 width -= wcwidth(wline[i]);
4420 if (width <= view->ncols - 1)
4421 waddch(view->window, '\n');
4422 if (++nprinted == 1)
4423 s->first_displayed_line = lineno;
4426 s->last_displayed_line = lineno;
4433 static const struct got_error *
4434 blame_cb(void *arg, int nlines, int lineno,
4435 struct got_commit_object *commit, struct got_object_id *id)
4437 const struct got_error *err = NULL;
4438 struct tog_blame_cb_args *a = arg;
4439 struct tog_blame_line *line;
4442 if (nlines != a->nlines ||
4443 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4444 return got_error(GOT_ERR_RANGE);
4446 errcode = pthread_mutex_lock(&tog_mutex);
4448 return got_error_set_errno(errcode, "pthread_mutex_lock");
4450 if (*a->quit) { /* user has quit the blame view */
4451 err = got_error(GOT_ERR_ITER_COMPLETED);
4456 goto done; /* no change in this commit */
4458 line = &a->lines[lineno - 1];
4459 if (line->annotated)
4462 line->id = got_object_id_dup(id);
4463 if (line->id == NULL) {
4464 err = got_error_from_errno("got_object_id_dup");
4467 line->annotated = 1;
4469 errcode = pthread_mutex_unlock(&tog_mutex);
4471 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4476 blame_thread(void *arg)
4478 const struct got_error *err, *close_err;
4479 struct tog_blame_thread_args *ta = arg;
4480 struct tog_blame_cb_args *a = ta->cb_args;
4483 err = block_signals_used_by_main_thread();
4487 err = got_blame(ta->path, a->commit_id, ta->repo,
4488 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4489 if (err && err->code == GOT_ERR_CANCELLED)
4492 errcode = pthread_mutex_lock(&tog_mutex);
4494 return (void *)got_error_set_errno(errcode,
4495 "pthread_mutex_lock");
4497 close_err = got_repo_close(ta->repo);
4503 errcode = pthread_mutex_unlock(&tog_mutex);
4504 if (errcode && err == NULL)
4505 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4510 static struct got_object_id *
4511 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4512 int first_displayed_line, int selected_line)
4514 struct tog_blame_line *line;
4519 line = &lines[first_displayed_line - 1 + selected_line - 1];
4520 if (!line->annotated)
4526 static const struct got_error *
4527 stop_blame(struct tog_blame *blame)
4529 const struct got_error *err = NULL;
4532 if (blame->thread) {
4534 errcode = pthread_mutex_unlock(&tog_mutex);
4536 return got_error_set_errno(errcode,
4537 "pthread_mutex_unlock");
4538 errcode = pthread_join(blame->thread, (void **)&err);
4540 return got_error_set_errno(errcode, "pthread_join");
4541 errcode = pthread_mutex_lock(&tog_mutex);
4543 return got_error_set_errno(errcode,
4544 "pthread_mutex_lock");
4545 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4547 blame->thread = NULL;
4549 if (blame->thread_args.repo) {
4550 const struct got_error *close_err;
4551 close_err = got_repo_close(blame->thread_args.repo);
4554 blame->thread_args.repo = NULL;
4557 if (fclose(blame->f) == EOF && err == NULL)
4558 err = got_error_from_errno("fclose");
4562 for (i = 0; i < blame->nlines; i++)
4563 free(blame->lines[i].id);
4565 blame->lines = NULL;
4567 free(blame->cb_args.commit_id);
4568 blame->cb_args.commit_id = NULL;
4569 if (blame->pack_fds) {
4570 const struct got_error *pack_err =
4571 got_repo_pack_fds_close(blame->pack_fds);
4574 blame->pack_fds = NULL;
4579 static const struct got_error *
4580 cancel_blame_view(void *arg)
4582 const struct got_error *err = NULL;
4586 errcode = pthread_mutex_lock(&tog_mutex);
4588 return got_error_set_errno(errcode,
4589 "pthread_mutex_unlock");
4592 err = got_error(GOT_ERR_CANCELLED);
4594 errcode = pthread_mutex_unlock(&tog_mutex);
4596 return got_error_set_errno(errcode,
4597 "pthread_mutex_lock");
4602 static const struct got_error *
4603 run_blame(struct tog_view *view)
4605 struct tog_blame_view_state *s = &view->state.blame;
4606 struct tog_blame *blame = &s->blame;
4607 const struct got_error *err = NULL;
4608 struct got_commit_object *commit = NULL;
4609 struct got_blob_object *blob = NULL;
4610 struct got_repository *thread_repo = NULL;
4611 struct got_object_id *obj_id = NULL;
4613 int *pack_fds = NULL;
4615 err = got_object_open_as_commit(&commit, s->repo,
4616 &s->blamed_commit->id);
4620 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4624 err = got_object_get_type(&obj_type, s->repo, obj_id);
4628 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4629 err = got_error(GOT_ERR_OBJ_TYPE);
4633 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4636 blame->f = got_opentemp();
4637 if (blame->f == NULL) {
4638 err = got_error_from_errno("got_opentemp");
4641 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4642 &blame->line_offsets, blame->f, blob);
4645 if (blame->nlines == 0) {
4646 s->blame_complete = 1;
4650 /* Don't include \n at EOF in the blame line count. */
4651 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4654 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4655 if (blame->lines == NULL) {
4656 err = got_error_from_errno("calloc");
4660 err = got_repo_pack_fds_open(&pack_fds);
4663 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4668 blame->pack_fds = pack_fds;
4669 blame->cb_args.view = view;
4670 blame->cb_args.lines = blame->lines;
4671 blame->cb_args.nlines = blame->nlines;
4672 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4673 if (blame->cb_args.commit_id == NULL) {
4674 err = got_error_from_errno("got_object_id_dup");
4677 blame->cb_args.quit = &s->done;
4679 blame->thread_args.path = s->path;
4680 blame->thread_args.repo = thread_repo;
4681 blame->thread_args.cb_args = &blame->cb_args;
4682 blame->thread_args.complete = &s->blame_complete;
4683 blame->thread_args.cancel_cb = cancel_blame_view;
4684 blame->thread_args.cancel_arg = &s->done;
4685 s->blame_complete = 0;
4687 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4688 s->first_displayed_line = 1;
4689 s->last_displayed_line = view->nlines;
4690 s->selected_line = 1;
4692 s->matched_line = 0;
4696 got_object_commit_close(commit);
4698 got_object_blob_close(blob);
4705 static const struct got_error *
4706 open_blame_view(struct tog_view *view, char *path,
4707 struct got_object_id *commit_id, struct got_repository *repo)
4709 const struct got_error *err = NULL;
4710 struct tog_blame_view_state *s = &view->state.blame;
4712 STAILQ_INIT(&s->blamed_commits);
4714 s->path = strdup(path);
4715 if (s->path == NULL)
4716 return got_error_from_errno("strdup");
4718 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4724 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4725 s->first_displayed_line = 1;
4726 s->last_displayed_line = view->nlines;
4727 s->selected_line = 1;
4728 s->blame_complete = 0;
4730 s->commit_id = commit_id;
4731 memset(&s->blame, 0, sizeof(s->blame));
4733 STAILQ_INIT(&s->colors);
4734 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4735 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4736 get_color_value("TOG_COLOR_COMMIT"));
4741 view->show = show_blame_view;
4742 view->input = input_blame_view;
4743 view->close = close_blame_view;
4744 view->search_start = search_start_blame_view;
4745 view->search_next = search_next_blame_view;
4747 return run_blame(view);
4750 static const struct got_error *
4751 close_blame_view(struct tog_view *view)
4753 const struct got_error *err = NULL;
4754 struct tog_blame_view_state *s = &view->state.blame;
4756 if (s->blame.thread)
4757 err = stop_blame(&s->blame);
4759 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4760 struct got_object_qid *blamed_commit;
4761 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4762 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4763 got_object_qid_free(blamed_commit);
4767 free_colors(&s->colors);
4771 static const struct got_error *
4772 search_start_blame_view(struct tog_view *view)
4774 struct tog_blame_view_state *s = &view->state.blame;
4776 s->matched_line = 0;
4780 static const struct got_error *
4781 search_next_blame_view(struct tog_view *view)
4783 struct tog_blame_view_state *s = &view->state.blame;
4784 const struct got_error *err = NULL;
4786 char *exstr = NULL, *line = NULL;
4787 size_t linesize = 0;
4790 if (!view->searching) {
4791 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4795 if (s->matched_line) {
4796 if (view->searching == TOG_SEARCH_FORWARD)
4797 lineno = s->matched_line + 1;
4799 lineno = s->matched_line - 1;
4801 lineno = s->first_displayed_line - 1 + s->selected_line;
4806 if (lineno <= 0 || lineno > s->blame.nlines) {
4807 if (s->matched_line == 0) {
4808 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4812 if (view->searching == TOG_SEARCH_FORWARD)
4815 lineno = s->blame.nlines;
4818 offset = s->blame.line_offsets[lineno - 1];
4819 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4821 return got_error_from_errno("fseeko");
4823 linelen = getline(&line, &linesize, s->blame.f);
4824 err = expand_tab(&exstr, line);
4827 if (linelen != -1 &&
4828 match_line(exstr, &view->regex, 1, &view->regmatch)) {
4829 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4830 s->matched_line = lineno;
4835 if (view->searching == TOG_SEARCH_FORWARD)
4843 if (s->matched_line) {
4844 s->first_displayed_line = s->matched_line;
4845 s->selected_line = 1;
4851 static const struct got_error *
4852 show_blame_view(struct tog_view *view)
4854 const struct got_error *err = NULL;
4855 struct tog_blame_view_state *s = &view->state.blame;
4858 if (s->blame.thread == NULL && !s->blame_complete) {
4859 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4860 &s->blame.thread_args);
4862 return got_error_set_errno(errcode, "pthread_create");
4864 halfdelay(1); /* fast refresh while annotating */
4867 if (s->blame_complete)
4868 halfdelay(10); /* disable fast refresh */
4870 err = draw_blame(view);
4876 static const struct got_error *
4877 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4879 const struct got_error *err = NULL, *thread_err = NULL;
4880 struct tog_view *diff_view;
4881 struct tog_blame_view_state *s = &view->state.blame;
4882 int begin_x = 0, nscroll = view->nlines - 2;
4889 view->x = MAX(view->maxx - view->ncols / 3, 0);
4893 if (view->x + view->ncols / 3 < view->maxx)
4894 view->x += 2; /* move two columns right */
4898 view->x -= MIN(view->x, 2); /* move two columns back */
4905 s->selected_line = 1;
4906 s->first_displayed_line = 1;
4910 if (s->blame.nlines < view->nlines - 2) {
4911 s->selected_line = s->blame.nlines;
4912 s->first_displayed_line = 1;
4914 s->selected_line = view->nlines - 2;
4915 s->first_displayed_line = s->blame.nlines -
4922 if (s->selected_line > 1)
4924 else if (s->selected_line == 1 &&
4925 s->first_displayed_line > 1)
4926 s->first_displayed_line--;
4934 if (s->first_displayed_line == 1) {
4935 s->selected_line = MAX(1, s->selected_line - nscroll);
4938 if (s->first_displayed_line > nscroll)
4939 s->first_displayed_line -= nscroll;
4941 s->first_displayed_line = 1;
4946 if (s->selected_line < view->nlines - 2 &&
4947 s->first_displayed_line +
4948 s->selected_line <= s->blame.nlines)
4950 else if (s->last_displayed_line <
4952 s->first_displayed_line++;
4956 struct got_object_id *id = NULL;
4957 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4958 s->first_displayed_line, s->selected_line);
4962 struct got_commit_object *commit, *pcommit;
4963 struct got_object_qid *pid;
4964 struct got_object_id *blob_id = NULL;
4966 err = got_object_open_as_commit(&commit,
4971 got_object_commit_get_parent_ids(commit));
4973 got_object_commit_close(commit);
4976 /* Check if path history ends here. */
4977 err = got_object_open_as_commit(&pcommit,
4981 err = got_object_id_by_path(&blob_id, s->repo,
4983 got_object_commit_close(pcommit);
4985 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4987 got_object_commit_close(commit);
4990 err = got_object_get_type(&obj_type, s->repo,
4993 /* Can't blame non-blob type objects. */
4994 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4995 got_object_commit_close(commit);
4998 err = got_object_qid_alloc(&s->blamed_commit,
5000 got_object_commit_close(commit);
5002 if (got_object_id_cmp(id,
5003 &s->blamed_commit->id) == 0)
5005 err = got_object_qid_alloc(&s->blamed_commit,
5011 thread_err = stop_blame(&s->blame);
5015 STAILQ_INSERT_HEAD(&s->blamed_commits,
5016 s->blamed_commit, entry);
5017 err = run_blame(view);
5023 struct got_object_qid *first;
5024 first = STAILQ_FIRST(&s->blamed_commits);
5025 if (!got_object_id_cmp(&first->id, s->commit_id))
5028 thread_err = stop_blame(&s->blame);
5032 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5033 got_object_qid_free(s->blamed_commit);
5035 STAILQ_FIRST(&s->blamed_commits);
5036 err = run_blame(view);
5043 struct got_object_id *id = NULL;
5044 struct got_object_qid *pid;
5045 struct got_commit_object *commit = NULL;
5046 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5047 s->first_displayed_line, s->selected_line);
5050 err = got_object_open_as_commit(&commit, s->repo, id);
5054 got_object_commit_get_parent_ids(commit));
5055 if (view_is_parent_view(view))
5056 begin_x = view_split_begin_x(view->begin_x);
5057 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5058 if (diff_view == NULL) {
5059 got_object_commit_close(commit);
5060 err = got_error_from_errno("view_open");
5063 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5064 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5065 got_object_commit_close(commit);
5067 view_close(diff_view);
5071 diff_view->focussed = 1;
5072 if (view_is_parent_view(view)) {
5073 err = view_close_child(view);
5076 view_set_child(view, diff_view);
5077 view->focus_child = 1;
5079 *new_view = diff_view;
5091 if (s->last_displayed_line >= s->blame.nlines &&
5092 s->selected_line >= MIN(s->blame.nlines,
5093 view->nlines - 2)) {
5096 if (s->last_displayed_line >= s->blame.nlines &&
5097 s->selected_line < view->nlines - 2) {
5099 MIN(nscroll, s->last_displayed_line -
5100 s->first_displayed_line - s->selected_line + 1);
5102 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5103 s->first_displayed_line += nscroll;
5105 s->first_displayed_line =
5106 s->blame.nlines - (view->nlines - 3);
5109 if (s->selected_line > view->nlines - 2) {
5110 s->selected_line = MIN(s->blame.nlines,
5117 return thread_err ? thread_err : err;
5120 static const struct got_error *
5121 cmd_blame(int argc, char *argv[])
5123 const struct got_error *error;
5124 struct got_repository *repo = NULL;
5125 struct got_worktree *worktree = NULL;
5126 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5127 char *link_target = NULL;
5128 struct got_object_id *commit_id = NULL;
5129 struct got_commit_object *commit = NULL;
5130 char *commit_id_str = NULL;
5132 struct tog_view *view;
5133 int *pack_fds = NULL;
5135 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5138 commit_id_str = optarg;
5141 repo_path = realpath(optarg, NULL);
5142 if (repo_path == NULL)
5143 return got_error_from_errno2("realpath",
5158 error = got_repo_pack_fds_open(&pack_fds);
5162 if (repo_path == NULL) {
5163 cwd = getcwd(NULL, 0);
5165 return got_error_from_errno("getcwd");
5166 error = got_worktree_open(&worktree, cwd);
5167 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5171 strdup(got_worktree_get_repo_path(worktree));
5173 repo_path = strdup(cwd);
5174 if (repo_path == NULL) {
5175 error = got_error_from_errno("strdup");
5180 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5184 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5191 error = apply_unveil(got_repo_get_path(repo), NULL);
5195 error = tog_load_refs(repo, 0);
5199 if (commit_id_str == NULL) {
5200 struct got_reference *head_ref;
5201 error = got_ref_open(&head_ref, repo, worktree ?
5202 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5205 error = got_ref_resolve(&commit_id, repo, head_ref);
5206 got_ref_close(head_ref);
5208 error = got_repo_match_object_id(&commit_id, NULL,
5209 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5214 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5216 error = got_error_from_errno("view_open");
5220 error = got_object_open_as_commit(&commit, repo, commit_id);
5224 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5229 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5234 /* Release work tree lock. */
5235 got_worktree_close(worktree);
5238 error = view_loop(view);
5246 got_object_commit_close(commit);
5248 got_worktree_close(worktree);
5250 const struct got_error *close_err = got_repo_close(repo);
5255 const struct got_error *pack_err =
5256 got_repo_pack_fds_close(pack_fds);
5264 static const struct got_error *
5265 draw_tree_entries(struct tog_view *view, const char *parent_path)
5267 struct tog_tree_view_state *s = &view->state.tree;
5268 const struct got_error *err = NULL;
5269 struct got_tree_entry *te;
5271 struct tog_color *tc;
5272 int width, n, i, nentries;
5273 int limit = view->nlines;
5277 werase(view->window);
5282 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5286 if (view_needs_focus_indication(view))
5287 wstandout(view->window);
5288 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5290 wattr_on(view->window,
5291 COLOR_PAIR(tc->colorpair), NULL);
5292 waddwstr(view->window, wline);
5294 wattr_off(view->window,
5295 COLOR_PAIR(tc->colorpair), NULL);
5296 if (view_needs_focus_indication(view))
5297 wstandend(view->window);
5300 if (width < view->ncols - 1)
5301 waddch(view->window, '\n');
5304 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5308 waddwstr(view->window, wline);
5311 if (width < view->ncols - 1)
5312 waddch(view->window, '\n');
5315 waddch(view->window, '\n');
5319 if (s->first_displayed_entry == NULL) {
5320 te = got_object_tree_get_first_entry(s->tree);
5321 if (s->selected == 0) {
5323 wstandout(view->window);
5324 s->selected_entry = NULL;
5326 waddstr(view->window, " ..\n"); /* parent directory */
5327 if (s->selected == 0 && view->focussed)
5328 wstandend(view->window);
5335 te = s->first_displayed_entry;
5338 nentries = got_object_tree_get_nentries(s->tree);
5339 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5340 char *line = NULL, *id_str = NULL, *link_target = NULL;
5341 const char *modestr = "";
5344 te = got_object_tree_get_entry(s->tree, i);
5345 mode = got_tree_entry_get_mode(te);
5348 err = got_object_id_str(&id_str,
5349 got_tree_entry_get_id(te));
5351 return got_error_from_errno(
5352 "got_object_id_str");
5354 if (got_object_tree_entry_is_submodule(te))
5356 else if (S_ISLNK(mode)) {
5359 err = got_tree_entry_get_symlink_target(&link_target,
5365 for (i = 0; i < strlen(link_target); i++) {
5366 if (!isprint((unsigned char)link_target[i]))
5367 link_target[i] = '?';
5371 else if (S_ISDIR(mode))
5373 else if (mode & S_IXUSR)
5375 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5376 got_tree_entry_get_name(te), modestr,
5377 link_target ? " -> ": "",
5378 link_target ? link_target : "") == -1) {
5381 return got_error_from_errno("asprintf");
5385 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5391 if (n == s->selected) {
5393 wstandout(view->window);
5394 s->selected_entry = te;
5396 tc = match_color(&s->colors, line);
5398 wattr_on(view->window,
5399 COLOR_PAIR(tc->colorpair), NULL);
5400 waddwstr(view->window, wline);
5402 wattr_off(view->window,
5403 COLOR_PAIR(tc->colorpair), NULL);
5404 if (width < view->ncols - 1)
5405 waddch(view->window, '\n');
5406 if (n == s->selected && view->focussed)
5407 wstandend(view->window);
5413 s->last_displayed_entry = te;
5422 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5424 struct got_tree_entry *te;
5425 int isroot = s->tree == s->root;
5428 if (s->first_displayed_entry == NULL)
5431 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5432 while (i++ < maxscroll) {
5435 s->first_displayed_entry = NULL;
5438 s->first_displayed_entry = te;
5439 te = got_tree_entry_get_prev(s->tree, te);
5444 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5446 struct got_tree_entry *next, *last;
5449 if (s->first_displayed_entry)
5450 next = got_tree_entry_get_next(s->tree,
5451 s->first_displayed_entry);
5453 next = got_object_tree_get_first_entry(s->tree);
5455 last = s->last_displayed_entry;
5456 while (next && last && n++ < maxscroll) {
5457 last = got_tree_entry_get_next(s->tree, last);
5459 s->first_displayed_entry = next;
5460 next = got_tree_entry_get_next(s->tree, next);
5465 static const struct got_error *
5466 tree_entry_path(char **path, struct tog_parent_trees *parents,
5467 struct got_tree_entry *te)
5469 const struct got_error *err = NULL;
5470 struct tog_parent_tree *pt;
5471 size_t len = 2; /* for leading slash and NUL */
5473 TAILQ_FOREACH(pt, parents, entry)
5474 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5477 len += strlen(got_tree_entry_get_name(te));
5479 *path = calloc(1, len);
5481 return got_error_from_errno("calloc");
5484 pt = TAILQ_LAST(parents, tog_parent_trees);
5486 const char *name = got_tree_entry_get_name(pt->selected_entry);
5487 if (strlcat(*path, name, len) >= len) {
5488 err = got_error(GOT_ERR_NO_SPACE);
5491 if (strlcat(*path, "/", len) >= len) {
5492 err = got_error(GOT_ERR_NO_SPACE);
5495 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5498 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5499 err = got_error(GOT_ERR_NO_SPACE);
5511 static const struct got_error *
5512 blame_tree_entry(struct tog_view **new_view, int begin_x,
5513 struct got_tree_entry *te, struct tog_parent_trees *parents,
5514 struct got_object_id *commit_id, struct got_repository *repo)
5516 const struct got_error *err = NULL;
5518 struct tog_view *blame_view;
5522 err = tree_entry_path(&path, parents, te);
5526 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5527 if (blame_view == NULL) {
5528 err = got_error_from_errno("view_open");
5532 err = open_blame_view(blame_view, path, commit_id, repo);
5534 if (err->code == GOT_ERR_CANCELLED)
5536 view_close(blame_view);
5538 *new_view = blame_view;
5544 static const struct got_error *
5545 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5546 struct tog_tree_view_state *s)
5548 struct tog_view *log_view;
5549 const struct got_error *err = NULL;
5554 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5555 if (log_view == NULL)
5556 return got_error_from_errno("view_open");
5558 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5562 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5565 view_close(log_view);
5567 *new_view = log_view;
5572 static const struct got_error *
5573 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5574 const char *head_ref_name, struct got_repository *repo)
5576 const struct got_error *err = NULL;
5577 char *commit_id_str = NULL;
5578 struct tog_tree_view_state *s = &view->state.tree;
5579 struct got_commit_object *commit = NULL;
5581 TAILQ_INIT(&s->parents);
5582 STAILQ_INIT(&s->colors);
5584 s->commit_id = got_object_id_dup(commit_id);
5585 if (s->commit_id == NULL)
5586 return got_error_from_errno("got_object_id_dup");
5588 err = got_object_open_as_commit(&commit, repo, commit_id);
5593 * The root is opened here and will be closed when the view is closed.
5594 * Any visited subtrees and their path-wise parents are opened and
5597 err = got_object_open_as_tree(&s->root, repo,
5598 got_object_commit_get_tree_id(commit));
5603 err = got_object_id_str(&commit_id_str, commit_id);
5607 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5608 err = got_error_from_errno("asprintf");
5612 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5613 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5614 if (head_ref_name) {
5615 s->head_ref_name = strdup(head_ref_name);
5616 if (s->head_ref_name == NULL) {
5617 err = got_error_from_errno("strdup");
5623 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5624 err = add_color(&s->colors, "\\$$",
5625 TOG_COLOR_TREE_SUBMODULE,
5626 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5629 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5630 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5633 err = add_color(&s->colors, "/$",
5634 TOG_COLOR_TREE_DIRECTORY,
5635 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5639 err = add_color(&s->colors, "\\*$",
5640 TOG_COLOR_TREE_EXECUTABLE,
5641 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5645 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5646 get_color_value("TOG_COLOR_COMMIT"));
5651 view->show = show_tree_view;
5652 view->input = input_tree_view;
5653 view->close = close_tree_view;
5654 view->search_start = search_start_tree_view;
5655 view->search_next = search_next_tree_view;
5657 free(commit_id_str);
5659 got_object_commit_close(commit);
5661 close_tree_view(view);
5665 static const struct got_error *
5666 close_tree_view(struct tog_view *view)
5668 struct tog_tree_view_state *s = &view->state.tree;
5670 free_colors(&s->colors);
5671 free(s->tree_label);
5672 s->tree_label = NULL;
5674 s->commit_id = NULL;
5675 free(s->head_ref_name);
5676 s->head_ref_name = NULL;
5677 while (!TAILQ_EMPTY(&s->parents)) {
5678 struct tog_parent_tree *parent;
5679 parent = TAILQ_FIRST(&s->parents);
5680 TAILQ_REMOVE(&s->parents, parent, entry);
5681 if (parent->tree != s->root)
5682 got_object_tree_close(parent->tree);
5686 if (s->tree != NULL && s->tree != s->root)
5687 got_object_tree_close(s->tree);
5689 got_object_tree_close(s->root);
5693 static const struct got_error *
5694 search_start_tree_view(struct tog_view *view)
5696 struct tog_tree_view_state *s = &view->state.tree;
5698 s->matched_entry = NULL;
5703 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5705 regmatch_t regmatch;
5707 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5711 static const struct got_error *
5712 search_next_tree_view(struct tog_view *view)
5714 struct tog_tree_view_state *s = &view->state.tree;
5715 struct got_tree_entry *te = NULL;
5717 if (!view->searching) {
5718 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5722 if (s->matched_entry) {
5723 if (view->searching == TOG_SEARCH_FORWARD) {
5724 if (s->selected_entry)
5725 te = got_tree_entry_get_next(s->tree,
5728 te = got_object_tree_get_first_entry(s->tree);
5730 if (s->selected_entry == NULL)
5731 te = got_object_tree_get_last_entry(s->tree);
5733 te = got_tree_entry_get_prev(s->tree,
5737 if (s->selected_entry)
5738 te = s->selected_entry;
5739 else if (view->searching == TOG_SEARCH_FORWARD)
5740 te = got_object_tree_get_first_entry(s->tree);
5742 te = got_object_tree_get_last_entry(s->tree);
5747 if (s->matched_entry == NULL) {
5748 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5751 if (view->searching == TOG_SEARCH_FORWARD)
5752 te = got_object_tree_get_first_entry(s->tree);
5754 te = got_object_tree_get_last_entry(s->tree);
5757 if (match_tree_entry(te, &view->regex)) {
5758 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5759 s->matched_entry = te;
5763 if (view->searching == TOG_SEARCH_FORWARD)
5764 te = got_tree_entry_get_next(s->tree, te);
5766 te = got_tree_entry_get_prev(s->tree, te);
5769 if (s->matched_entry) {
5770 s->first_displayed_entry = s->matched_entry;
5777 static const struct got_error *
5778 show_tree_view(struct tog_view *view)
5780 const struct got_error *err = NULL;
5781 struct tog_tree_view_state *s = &view->state.tree;
5784 err = tree_entry_path(&parent_path, &s->parents, NULL);
5788 err = draw_tree_entries(view, parent_path);
5795 static const struct got_error *
5796 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5798 const struct got_error *err = NULL;
5799 struct tog_tree_view_state *s = &view->state.tree;
5800 struct tog_view *log_view, *ref_view;
5801 struct got_tree_entry *te;
5802 int begin_x = 0, n, nscroll = view->nlines - 3;
5806 s->show_ids = !s->show_ids;
5809 if (!s->selected_entry)
5811 if (view_is_parent_view(view))
5812 begin_x = view_split_begin_x(view->begin_x);
5813 err = log_selected_tree_entry(&log_view, begin_x, s);
5815 log_view->focussed = 1;
5816 if (view_is_parent_view(view)) {
5817 err = view_close_child(view);
5820 view_set_child(view, log_view);
5821 view->focus_child = 1;
5823 *new_view = log_view;
5826 if (view_is_parent_view(view))
5827 begin_x = view_split_begin_x(view->begin_x);
5828 ref_view = view_open(view->nlines, view->ncols,
5829 view->begin_y, begin_x, TOG_VIEW_REF);
5830 if (ref_view == NULL)
5831 return got_error_from_errno("view_open");
5832 err = open_ref_view(ref_view, s->repo);
5834 view_close(ref_view);
5838 ref_view->focussed = 1;
5839 if (view_is_parent_view(view)) {
5840 err = view_close_child(view);
5843 view_set_child(view, ref_view);
5844 view->focus_child = 1;
5846 *new_view = ref_view;
5851 if (s->tree == s->root)
5852 s->first_displayed_entry =
5853 got_object_tree_get_first_entry(s->tree);
5855 s->first_displayed_entry = NULL;
5860 te = got_object_tree_get_last_entry(s->tree);
5861 for (n = 0; n < view->nlines - 3; n++) {
5863 if(s->tree != s->root) {
5864 s->first_displayed_entry = NULL;
5869 s->first_displayed_entry = te;
5870 te = got_tree_entry_get_prev(s->tree, te);
5873 s->selected = n - 1;
5878 if (s->selected > 0) {
5882 tree_scroll_up(s, 1);
5890 if (s->tree == s->root) {
5891 if (got_object_tree_get_first_entry(s->tree) ==
5892 s->first_displayed_entry)
5893 s->selected -= MIN(s->selected, nscroll);
5895 if (s->first_displayed_entry == NULL)
5896 s->selected -= MIN(s->selected, nscroll);
5898 tree_scroll_up(s, MAX(0, nscroll));
5903 if (s->selected < s->ndisplayed - 1) {
5907 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5909 /* can't scroll any further */
5911 tree_scroll_down(s, 1);
5919 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5921 /* can't scroll any further; move cursor down */
5922 if (s->selected < s->ndisplayed - 1)
5923 s->selected += MIN(nscroll,
5924 s->ndisplayed - s->selected - 1);
5927 tree_scroll_down(s, nscroll);
5932 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5933 struct tog_parent_tree *parent;
5934 /* user selected '..' */
5935 if (s->tree == s->root)
5937 parent = TAILQ_FIRST(&s->parents);
5938 TAILQ_REMOVE(&s->parents, parent,
5940 got_object_tree_close(s->tree);
5941 s->tree = parent->tree;
5942 s->first_displayed_entry =
5943 parent->first_displayed_entry;
5945 parent->selected_entry;
5946 s->selected = parent->selected;
5948 } else if (S_ISDIR(got_tree_entry_get_mode(
5949 s->selected_entry))) {
5950 struct got_tree_object *subtree;
5951 err = got_object_open_as_tree(&subtree, s->repo,
5952 got_tree_entry_get_id(s->selected_entry));
5955 err = tree_view_visit_subtree(s, subtree);
5957 got_object_tree_close(subtree);
5960 } else if (S_ISREG(got_tree_entry_get_mode(
5961 s->selected_entry))) {
5962 struct tog_view *blame_view;
5963 int begin_x = view_is_parent_view(view) ?
5964 view_split_begin_x(view->begin_x) : 0;
5966 err = blame_tree_entry(&blame_view, begin_x,
5967 s->selected_entry, &s->parents,
5968 s->commit_id, s->repo);
5972 blame_view->focussed = 1;
5973 if (view_is_parent_view(view)) {
5974 err = view_close_child(view);
5977 view_set_child(view, blame_view);
5978 view->focus_child = 1;
5980 *new_view = blame_view;
5984 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5985 s->selected = view->nlines - 4;
5998 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6003 static const struct got_error *
6004 cmd_tree(int argc, char *argv[])
6006 const struct got_error *error;
6007 struct got_repository *repo = NULL;
6008 struct got_worktree *worktree = NULL;
6009 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6010 struct got_object_id *commit_id = NULL;
6011 struct got_commit_object *commit = NULL;
6012 const char *commit_id_arg = NULL;
6014 struct got_reference *ref = NULL;
6015 const char *head_ref_name = NULL;
6017 struct tog_view *view;
6018 int *pack_fds = NULL;
6020 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6023 commit_id_arg = optarg;
6026 repo_path = realpath(optarg, NULL);
6027 if (repo_path == NULL)
6028 return got_error_from_errno2("realpath",
6043 error = got_repo_pack_fds_open(&pack_fds);
6047 if (repo_path == NULL) {
6048 cwd = getcwd(NULL, 0);
6050 return got_error_from_errno("getcwd");
6051 error = got_worktree_open(&worktree, cwd);
6052 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6056 strdup(got_worktree_get_repo_path(worktree));
6058 repo_path = strdup(cwd);
6059 if (repo_path == NULL) {
6060 error = got_error_from_errno("strdup");
6065 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6069 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6076 error = apply_unveil(got_repo_get_path(repo), NULL);
6080 error = tog_load_refs(repo, 0);
6084 if (commit_id_arg == NULL) {
6085 error = got_repo_match_object_id(&commit_id, &label,
6086 worktree ? got_worktree_get_head_ref_name(worktree) :
6087 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6090 head_ref_name = label;
6092 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6094 head_ref_name = got_ref_get_name(ref);
6095 else if (error->code != GOT_ERR_NOT_REF)
6097 error = got_repo_match_object_id(&commit_id, NULL,
6098 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6103 error = got_object_open_as_commit(&commit, repo, commit_id);
6107 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6109 error = got_error_from_errno("view_open");
6112 error = open_tree_view(view, commit_id, head_ref_name, repo);
6115 if (!got_path_is_root_dir(in_repo_path)) {
6116 error = tree_view_walk_path(&view->state.tree, commit,
6123 /* Release work tree lock. */
6124 got_worktree_close(worktree);
6127 error = view_loop(view);
6136 const struct got_error *close_err = got_repo_close(repo);
6141 const struct got_error *pack_err =
6142 got_repo_pack_fds_close(pack_fds);
6150 static const struct got_error *
6151 ref_view_load_refs(struct tog_ref_view_state *s)
6153 struct got_reflist_entry *sre;
6154 struct tog_reflist_entry *re;
6157 TAILQ_FOREACH(sre, &tog_refs, entry) {
6158 if (strncmp(got_ref_get_name(sre->ref),
6159 "refs/got/", 9) == 0 &&
6160 strncmp(got_ref_get_name(sre->ref),
6161 "refs/got/backup/", 16) != 0)
6164 re = malloc(sizeof(*re));
6166 return got_error_from_errno("malloc");
6168 re->ref = got_ref_dup(sre->ref);
6169 if (re->ref == NULL)
6170 return got_error_from_errno("got_ref_dup");
6171 re->idx = s->nrefs++;
6172 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6175 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6180 ref_view_free_refs(struct tog_ref_view_state *s)
6182 struct tog_reflist_entry *re;
6184 while (!TAILQ_EMPTY(&s->refs)) {
6185 re = TAILQ_FIRST(&s->refs);
6186 TAILQ_REMOVE(&s->refs, re, entry);
6187 got_ref_close(re->ref);
6192 static const struct got_error *
6193 open_ref_view(struct tog_view *view, struct got_repository *repo)
6195 const struct got_error *err = NULL;
6196 struct tog_ref_view_state *s = &view->state.ref;
6198 s->selected_entry = 0;
6201 TAILQ_INIT(&s->refs);
6202 STAILQ_INIT(&s->colors);
6204 err = ref_view_load_refs(s);
6208 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6209 err = add_color(&s->colors, "^refs/heads/",
6210 TOG_COLOR_REFS_HEADS,
6211 get_color_value("TOG_COLOR_REFS_HEADS"));
6215 err = add_color(&s->colors, "^refs/tags/",
6216 TOG_COLOR_REFS_TAGS,
6217 get_color_value("TOG_COLOR_REFS_TAGS"));
6221 err = add_color(&s->colors, "^refs/remotes/",
6222 TOG_COLOR_REFS_REMOTES,
6223 get_color_value("TOG_COLOR_REFS_REMOTES"));
6227 err = add_color(&s->colors, "^refs/got/backup/",
6228 TOG_COLOR_REFS_BACKUP,
6229 get_color_value("TOG_COLOR_REFS_BACKUP"));
6234 view->show = show_ref_view;
6235 view->input = input_ref_view;
6236 view->close = close_ref_view;
6237 view->search_start = search_start_ref_view;
6238 view->search_next = search_next_ref_view;
6241 free_colors(&s->colors);
6245 static const struct got_error *
6246 close_ref_view(struct tog_view *view)
6248 struct tog_ref_view_state *s = &view->state.ref;
6250 ref_view_free_refs(s);
6251 free_colors(&s->colors);
6256 static const struct got_error *
6257 resolve_reflist_entry(struct got_object_id **commit_id,
6258 struct tog_reflist_entry *re, struct got_repository *repo)
6260 const struct got_error *err = NULL;
6261 struct got_object_id *obj_id;
6262 struct got_tag_object *tag = NULL;
6267 err = got_ref_resolve(&obj_id, repo, re->ref);
6271 err = got_object_get_type(&obj_type, repo, obj_id);
6276 case GOT_OBJ_TYPE_COMMIT:
6277 *commit_id = obj_id;
6279 case GOT_OBJ_TYPE_TAG:
6280 err = got_object_open_as_tag(&tag, repo, obj_id);
6284 err = got_object_get_type(&obj_type, repo,
6285 got_object_tag_get_object_id(tag));
6288 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6289 err = got_error(GOT_ERR_OBJ_TYPE);
6292 *commit_id = got_object_id_dup(
6293 got_object_tag_get_object_id(tag));
6294 if (*commit_id == NULL) {
6295 err = got_error_from_errno("got_object_id_dup");
6300 err = got_error(GOT_ERR_OBJ_TYPE);
6306 got_object_tag_close(tag);
6314 static const struct got_error *
6315 log_ref_entry(struct tog_view **new_view, int begin_x,
6316 struct tog_reflist_entry *re, struct got_repository *repo)
6318 struct tog_view *log_view;
6319 const struct got_error *err = NULL;
6320 struct got_object_id *commit_id = NULL;
6324 err = resolve_reflist_entry(&commit_id, re, repo);
6326 if (err->code != GOT_ERR_OBJ_TYPE)
6332 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6333 if (log_view == NULL) {
6334 err = got_error_from_errno("view_open");
6338 err = open_log_view(log_view, commit_id, repo,
6339 got_ref_get_name(re->ref), "", 0);
6342 view_close(log_view);
6344 *new_view = log_view;
6350 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6352 struct tog_reflist_entry *re;
6355 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6358 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6359 while (i++ < maxscroll) {
6362 s->first_displayed_entry = re;
6363 re = TAILQ_PREV(re, tog_reflist_head, entry);
6368 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6370 struct tog_reflist_entry *next, *last;
6373 if (s->first_displayed_entry)
6374 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6376 next = TAILQ_FIRST(&s->refs);
6378 last = s->last_displayed_entry;
6379 while (next && last && n++ < maxscroll) {
6380 last = TAILQ_NEXT(last, entry);
6382 s->first_displayed_entry = next;
6383 next = TAILQ_NEXT(next, entry);
6388 static const struct got_error *
6389 search_start_ref_view(struct tog_view *view)
6391 struct tog_ref_view_state *s = &view->state.ref;
6393 s->matched_entry = NULL;
6398 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6400 regmatch_t regmatch;
6402 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6406 static const struct got_error *
6407 search_next_ref_view(struct tog_view *view)
6409 struct tog_ref_view_state *s = &view->state.ref;
6410 struct tog_reflist_entry *re = NULL;
6412 if (!view->searching) {
6413 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6417 if (s->matched_entry) {
6418 if (view->searching == TOG_SEARCH_FORWARD) {
6419 if (s->selected_entry)
6420 re = TAILQ_NEXT(s->selected_entry, entry);
6422 re = TAILQ_PREV(s->selected_entry,
6423 tog_reflist_head, entry);
6425 if (s->selected_entry == NULL)
6426 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6428 re = TAILQ_PREV(s->selected_entry,
6429 tog_reflist_head, entry);
6432 if (s->selected_entry)
6433 re = s->selected_entry;
6434 else if (view->searching == TOG_SEARCH_FORWARD)
6435 re = TAILQ_FIRST(&s->refs);
6437 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6442 if (s->matched_entry == NULL) {
6443 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6446 if (view->searching == TOG_SEARCH_FORWARD)
6447 re = TAILQ_FIRST(&s->refs);
6449 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6452 if (match_reflist_entry(re, &view->regex)) {
6453 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6454 s->matched_entry = re;
6458 if (view->searching == TOG_SEARCH_FORWARD)
6459 re = TAILQ_NEXT(re, entry);
6461 re = TAILQ_PREV(re, tog_reflist_head, entry);
6464 if (s->matched_entry) {
6465 s->first_displayed_entry = s->matched_entry;
6472 static const struct got_error *
6473 show_ref_view(struct tog_view *view)
6475 const struct got_error *err = NULL;
6476 struct tog_ref_view_state *s = &view->state.ref;
6477 struct tog_reflist_entry *re;
6480 struct tog_color *tc;
6482 int limit = view->nlines;
6484 werase(view->window);
6491 re = s->first_displayed_entry;
6493 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6495 return got_error_from_errno("asprintf");
6497 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6502 if (view_needs_focus_indication(view))
6503 wstandout(view->window);
6504 waddwstr(view->window, wline);
6505 if (view_needs_focus_indication(view))
6506 wstandend(view->window);
6511 if (width < view->ncols - 1)
6512 waddch(view->window, '\n');
6517 while (re && limit > 0) {
6519 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6522 struct got_commit_object *ci;
6523 struct got_tag_object *tag;
6524 struct got_object_id *id;
6528 err = got_ref_resolve(&id, s->repo, re->ref);
6531 err = got_object_open_as_tag(&tag, s->repo, id);
6533 if (err->code != GOT_ERR_OBJ_TYPE) {
6537 err = got_object_open_as_commit(&ci, s->repo,
6543 t = got_object_commit_get_committer_time(ci);
6544 got_object_commit_close(ci);
6546 t = got_object_tag_get_tagger_time(tag);
6547 got_object_tag_close(tag);
6550 if (gmtime_r(&t, &tm) == NULL)
6551 return got_error_from_errno("gmtime_r");
6552 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6553 return got_error(GOT_ERR_NO_SPACE);
6555 if (got_ref_is_symbolic(re->ref)) {
6556 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6557 ymd : "", got_ref_get_name(re->ref),
6558 got_ref_get_symref_target(re->ref)) == -1)
6559 return got_error_from_errno("asprintf");
6560 } else if (s->show_ids) {
6561 struct got_object_id *id;
6563 err = got_ref_resolve(&id, s->repo, re->ref);
6566 err = got_object_id_str(&id_str, id);
6571 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6572 got_ref_get_name(re->ref), id_str) == -1) {
6573 err = got_error_from_errno("asprintf");
6580 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6581 got_ref_get_name(re->ref)) == -1)
6582 return got_error_from_errno("asprintf");
6584 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6590 if (n == s->selected) {
6592 wstandout(view->window);
6593 s->selected_entry = re;
6595 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6597 wattr_on(view->window,
6598 COLOR_PAIR(tc->colorpair), NULL);
6599 waddwstr(view->window, wline);
6601 wattr_off(view->window,
6602 COLOR_PAIR(tc->colorpair), NULL);
6603 if (width < view->ncols - 1)
6604 waddch(view->window, '\n');
6605 if (n == s->selected && view->focussed)
6606 wstandend(view->window);
6612 s->last_displayed_entry = re;
6615 re = TAILQ_NEXT(re, entry);
6622 static const struct got_error *
6623 browse_ref_tree(struct tog_view **new_view, int begin_x,
6624 struct tog_reflist_entry *re, struct got_repository *repo)
6626 const struct got_error *err = NULL;
6627 struct got_object_id *commit_id = NULL;
6628 struct tog_view *tree_view;
6632 err = resolve_reflist_entry(&commit_id, re, repo);
6634 if (err->code != GOT_ERR_OBJ_TYPE)
6641 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6642 if (tree_view == NULL) {
6643 err = got_error_from_errno("view_open");
6647 err = open_tree_view(tree_view, commit_id,
6648 got_ref_get_name(re->ref), repo);
6652 *new_view = tree_view;
6657 static const struct got_error *
6658 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6660 const struct got_error *err = NULL;
6661 struct tog_ref_view_state *s = &view->state.ref;
6662 struct tog_view *log_view, *tree_view;
6663 struct tog_reflist_entry *re;
6664 int begin_x = 0, n, nscroll = view->nlines - 1;
6668 s->show_ids = !s->show_ids;
6671 s->show_date = !s->show_date;
6674 s->sort_by_date = !s->sort_by_date;
6675 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6676 got_ref_cmp_by_commit_timestamp_descending :
6677 tog_ref_cmp_by_name, s->repo);
6680 got_reflist_object_id_map_free(tog_refs_idmap);
6681 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6682 &tog_refs, s->repo);
6685 ref_view_free_refs(s);
6686 err = ref_view_load_refs(s);
6690 if (!s->selected_entry)
6692 if (view_is_parent_view(view))
6693 begin_x = view_split_begin_x(view->begin_x);
6694 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6697 log_view->focussed = 1;
6698 if (view_is_parent_view(view)) {
6699 err = view_close_child(view);
6702 view_set_child(view, log_view);
6703 view->focus_child = 1;
6705 *new_view = log_view;
6708 if (!s->selected_entry)
6710 if (view_is_parent_view(view))
6711 begin_x = view_split_begin_x(view->begin_x);
6712 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6714 if (err || tree_view == NULL)
6717 tree_view->focussed = 1;
6718 if (view_is_parent_view(view)) {
6719 err = view_close_child(view);
6722 view_set_child(view, tree_view);
6723 view->focus_child = 1;
6725 *new_view = tree_view;
6730 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6735 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6736 for (n = 0; n < view->nlines - 1; n++) {
6739 s->first_displayed_entry = re;
6740 re = TAILQ_PREV(re, tog_reflist_head, entry);
6743 s->selected = n - 1;
6748 if (s->selected > 0) {
6752 ref_scroll_up(s, 1);
6760 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6761 s->selected -= MIN(nscroll, s->selected);
6762 ref_scroll_up(s, MAX(0, nscroll));
6767 if (s->selected < s->ndisplayed - 1) {
6771 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6772 /* can't scroll any further */
6774 ref_scroll_down(s, 1);
6782 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6783 /* can't scroll any further; move cursor down */
6784 if (s->selected < s->ndisplayed - 1)
6785 s->selected += MIN(nscroll,
6786 s->ndisplayed - s->selected - 1);
6789 ref_scroll_down(s, nscroll);
6793 err = tog_load_refs(s->repo, s->sort_by_date);
6796 ref_view_free_refs(s);
6797 err = ref_view_load_refs(s);
6800 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6801 s->selected = view->nlines - 2;
6814 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6819 static const struct got_error *
6820 cmd_ref(int argc, char *argv[])
6822 const struct got_error *error;
6823 struct got_repository *repo = NULL;
6824 struct got_worktree *worktree = NULL;
6825 char *cwd = NULL, *repo_path = NULL;
6827 struct tog_view *view;
6828 int *pack_fds = NULL;
6830 while ((ch = getopt(argc, argv, "r:")) != -1) {
6833 repo_path = realpath(optarg, NULL);
6834 if (repo_path == NULL)
6835 return got_error_from_errno2("realpath",
6850 error = got_repo_pack_fds_open(&pack_fds);
6854 if (repo_path == NULL) {
6855 cwd = getcwd(NULL, 0);
6857 return got_error_from_errno("getcwd");
6858 error = got_worktree_open(&worktree, cwd);
6859 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6863 strdup(got_worktree_get_repo_path(worktree));
6865 repo_path = strdup(cwd);
6866 if (repo_path == NULL) {
6867 error = got_error_from_errno("strdup");
6872 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6878 error = apply_unveil(got_repo_get_path(repo), NULL);
6882 error = tog_load_refs(repo, 0);
6886 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6888 error = got_error_from_errno("view_open");
6892 error = open_ref_view(view, repo);
6897 /* Release work tree lock. */
6898 got_worktree_close(worktree);
6901 error = view_loop(view);
6906 const struct got_error *close_err = got_repo_close(repo);
6911 const struct got_error *pack_err =
6912 got_repo_pack_fds_close(pack_fds);
6921 list_commands(FILE *fp)
6925 fprintf(fp, "commands:");
6926 for (i = 0; i < nitems(tog_commands); i++) {
6927 const struct tog_cmd *cmd = &tog_commands[i];
6928 fprintf(fp, " %s", cmd->name);
6934 usage(int hflag, int status)
6936 FILE *fp = (status == 0) ? stdout : stderr;
6938 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6941 fprintf(fp, "lazy usage: %s path\n", getprogname());
6948 make_argv(int argc, ...)
6956 argv = calloc(argc, sizeof(char *));
6959 for (i = 0; i < argc; i++) {
6960 argv[i] = strdup(va_arg(ap, char *));
6961 if (argv[i] == NULL)
6970 * Try to convert 'tog path' into a 'tog log path' command.
6971 * The user could simply have mistyped the command rather than knowingly
6972 * provided a path. So check whether argv[0] can in fact be resolved
6973 * to a path in the HEAD commit and print a special error if not.
6974 * This hack is for mpi@ <3
6976 static const struct got_error *
6977 tog_log_with_path(int argc, char *argv[])
6979 const struct got_error *error = NULL, *close_err;
6980 const struct tog_cmd *cmd = NULL;
6981 struct got_repository *repo = NULL;
6982 struct got_worktree *worktree = NULL;
6983 struct got_object_id *commit_id = NULL, *id = NULL;
6984 struct got_commit_object *commit = NULL;
6985 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6986 char *commit_id_str = NULL, **cmd_argv = NULL;
6987 int *pack_fds = NULL;
6989 cwd = getcwd(NULL, 0);
6991 return got_error_from_errno("getcwd");
6993 error = got_repo_pack_fds_open(&pack_fds);
6997 error = got_worktree_open(&worktree, cwd);
6998 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7002 repo_path = strdup(got_worktree_get_repo_path(worktree));
7004 repo_path = strdup(cwd);
7005 if (repo_path == NULL) {
7006 error = got_error_from_errno("strdup");
7010 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7014 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7019 error = tog_load_refs(repo, 0);
7022 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7023 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7024 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7029 got_worktree_close(worktree);
7033 error = got_object_open_as_commit(&commit, repo, commit_id);
7037 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7039 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7041 fprintf(stderr, "%s: '%s' is no known command or path\n",
7042 getprogname(), argv[0]);
7047 close_err = got_repo_close(repo);
7052 error = got_object_id_str(&commit_id_str, commit_id);
7056 cmd = &tog_commands[0]; /* log */
7058 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7059 error = cmd->cmd_main(argc, cmd_argv);
7062 close_err = got_repo_close(repo);
7067 got_object_commit_close(commit);
7069 got_worktree_close(worktree);
7071 const struct got_error *pack_err =
7072 got_repo_pack_fds_close(pack_fds);
7077 free(commit_id_str);
7084 for (i = 0; i < argc; i++)
7093 main(int argc, char *argv[])
7095 const struct got_error *error = NULL;
7096 const struct tog_cmd *cmd = NULL;
7097 int ch, hflag = 0, Vflag = 0;
7098 char **cmd_argv = NULL;
7099 static const struct option longopts[] = {
7100 { "version", no_argument, NULL, 'V' },
7104 setlocale(LC_CTYPE, "");
7106 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7126 got_version_print_str();
7131 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7139 /* Build an argument vector which runs a default command. */
7140 cmd = &tog_commands[0];
7142 cmd_argv = make_argv(argc, cmd->name);
7146 /* Did the user specify a command? */
7147 for (i = 0; i < nitems(tog_commands); i++) {
7148 if (strncmp(tog_commands[i].name, argv[0],
7149 strlen(argv[0])) == 0) {
7150 cmd = &tog_commands[i];
7159 /* No command specified; try log with a path */
7160 error = tog_log_with_path(argc, argv);
7165 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7172 for (i = 0; i < argc; i++)
7177 if (error && error->code != GOT_ERR_CANCELLED)
7178 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);