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.
18 #include <sys/ioctl.h>
22 #if defined(__FreeBSD__) || defined(__APPLE__)
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
44 #include "got_compat.h"
46 #include "got_version.h"
47 #include "got_error.h"
48 #include "got_object.h"
49 #include "got_reference.h"
50 #include "got_repository.h"
52 #include "got_opentemp.h"
54 #include "got_cancel.h"
55 #include "got_commit_graph.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
59 #include "got_worktree.h"
61 //#define update_panels() (0)
62 //#define doupdate() (0)
65 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
69 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
73 #define CTRL(x) ((x) & 0x1f)
77 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
82 const struct got_error *(*cmd_main)(int, char *[]);
83 void (*cmd_usage)(void);
86 __dead static void usage(int, int);
87 __dead static void usage_log(void);
88 __dead static void usage_diff(void);
89 __dead static void usage_blame(void);
90 __dead static void usage_tree(void);
91 __dead static void usage_ref(void);
93 static const struct got_error* cmd_log(int, char *[]);
94 static const struct got_error* cmd_diff(int, char *[]);
95 static const struct got_error* cmd_blame(int, char *[]);
96 static const struct got_error* cmd_tree(int, char *[]);
97 static const struct got_error* cmd_ref(int, char *[]);
99 static const struct tog_cmd tog_commands[] = {
100 { "log", cmd_log, usage_log },
101 { "diff", cmd_diff, usage_diff },
102 { "blame", cmd_blame, usage_blame },
103 { "tree", cmd_tree, usage_tree },
104 { "ref", cmd_ref, usage_ref },
115 #define TOG_EOF_STRING "(END)"
117 struct commit_queue_entry {
118 TAILQ_ENTRY(commit_queue_entry) entry;
119 struct got_object_id *id;
120 struct got_commit_object *commit;
123 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
124 struct commit_queue {
126 struct commit_queue_head head;
130 STAILQ_ENTRY(tog_color) entry;
134 STAILQ_HEAD(tog_colors, tog_color);
136 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
137 static struct got_reflist_object_id_map *tog_refs_idmap;
139 static const struct got_error *
140 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
141 struct got_reference* re2)
143 const char *name1 = got_ref_get_name(re1);
144 const char *name2 = got_ref_get_name(re2);
145 int isbackup1, isbackup2;
147 /* Sort backup refs towards the bottom of the list. */
148 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
149 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
150 if (!isbackup1 && isbackup2) {
153 } else if (isbackup1 && !isbackup2) {
158 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
162 static const struct got_error *
163 tog_load_refs(struct got_repository *repo, int sort_by_date)
165 const struct got_error *err;
167 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
168 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
173 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
180 if (tog_refs_idmap) {
181 got_reflist_object_id_map_free(tog_refs_idmap);
182 tog_refs_idmap = NULL;
184 got_ref_list_free(&tog_refs);
187 static const struct got_error *
188 add_color(struct tog_colors *colors, const char *pattern,
189 int idx, short color)
191 const struct got_error *err = NULL;
192 struct tog_color *tc;
195 if (idx < 1 || idx > COLOR_PAIRS - 1)
198 init_pair(idx, color, -1);
200 tc = calloc(1, sizeof(*tc));
202 return got_error_from_errno("calloc");
203 regerr = regcomp(&tc->regex, pattern,
204 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
206 static char regerr_msg[512];
207 static char err_msg[512];
208 regerror(regerr, &tc->regex, regerr_msg,
210 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
212 err = got_error_msg(GOT_ERR_REGEX, err_msg);
217 STAILQ_INSERT_HEAD(colors, tc, entry);
222 free_colors(struct tog_colors *colors)
224 struct tog_color *tc;
226 while (!STAILQ_EMPTY(colors)) {
227 tc = STAILQ_FIRST(colors);
228 STAILQ_REMOVE_HEAD(colors, entry);
235 get_color(struct tog_colors *colors, int colorpair)
237 struct tog_color *tc = NULL;
239 STAILQ_FOREACH(tc, colors, entry) {
240 if (tc->colorpair == colorpair)
248 default_color_value(const char *envvar)
250 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
251 return COLOR_MAGENTA;
252 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
254 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
256 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
258 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
259 return COLOR_MAGENTA;
260 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
261 return COLOR_MAGENTA;
262 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
264 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
266 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
268 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
270 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
272 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
274 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
275 return COLOR_MAGENTA;
276 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
278 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
285 get_color_value(const char *envvar)
287 const char *val = getenv(envvar);
290 return default_color_value(envvar);
292 if (strcasecmp(val, "black") == 0)
294 if (strcasecmp(val, "red") == 0)
296 if (strcasecmp(val, "green") == 0)
298 if (strcasecmp(val, "yellow") == 0)
300 if (strcasecmp(val, "blue") == 0)
302 if (strcasecmp(val, "magenta") == 0)
303 return COLOR_MAGENTA;
304 if (strcasecmp(val, "cyan") == 0)
306 if (strcasecmp(val, "white") == 0)
308 if (strcasecmp(val, "default") == 0)
311 return default_color_value(envvar);
315 struct tog_diff_view_state {
316 struct got_object_id *id1, *id2;
317 const char *label1, *label2;
319 int first_displayed_line;
320 int last_displayed_line;
323 int ignore_whitespace;
325 struct got_repository *repo;
326 struct tog_colors colors;
332 /* passed from log view; may be NULL */
333 struct tog_view *log_view;
336 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
338 struct tog_log_thread_args {
339 pthread_cond_t need_commits;
340 pthread_cond_t commit_loaded;
343 struct got_commit_graph *graph;
344 struct commit_queue *commits;
345 const char *in_repo_path;
346 struct got_object_id *start_id;
347 struct got_repository *repo;
351 struct commit_queue_entry **first_displayed_entry;
352 struct commit_queue_entry **selected_entry;
354 int *search_next_done;
358 struct tog_log_view_state {
359 struct commit_queue commits;
360 struct commit_queue_entry *first_displayed_entry;
361 struct commit_queue_entry *last_displayed_entry;
362 struct commit_queue_entry *selected_entry;
367 struct got_repository *repo;
368 struct got_object_id *start_id;
371 struct tog_log_thread_args thread_args;
372 struct commit_queue_entry *matched_entry;
373 struct commit_queue_entry *search_entry;
374 struct tog_colors colors;
377 #define TOG_COLOR_DIFF_MINUS 1
378 #define TOG_COLOR_DIFF_PLUS 2
379 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
380 #define TOG_COLOR_DIFF_META 4
381 #define TOG_COLOR_TREE_SUBMODULE 5
382 #define TOG_COLOR_TREE_SYMLINK 6
383 #define TOG_COLOR_TREE_DIRECTORY 7
384 #define TOG_COLOR_TREE_EXECUTABLE 8
385 #define TOG_COLOR_COMMIT 9
386 #define TOG_COLOR_AUTHOR 10
387 #define TOG_COLOR_DATE 11
388 #define TOG_COLOR_REFS_HEADS 12
389 #define TOG_COLOR_REFS_TAGS 13
390 #define TOG_COLOR_REFS_REMOTES 14
391 #define TOG_COLOR_REFS_BACKUP 15
393 struct tog_blame_cb_args {
394 struct tog_blame_line *lines; /* one per line */
397 struct tog_view *view;
398 struct got_object_id *commit_id;
402 struct tog_blame_thread_args {
404 struct got_repository *repo;
405 struct tog_blame_cb_args *cb_args;
407 got_cancel_cb cancel_cb;
414 struct tog_blame_line *lines;
418 struct tog_blame_thread_args thread_args;
419 struct tog_blame_cb_args cb_args;
424 struct tog_blame_view_state {
425 int first_displayed_line;
426 int last_displayed_line;
431 struct got_object_id_queue blamed_commits;
432 struct got_object_qid *blamed_commit;
434 struct got_repository *repo;
435 struct got_object_id *commit_id;
436 struct tog_blame blame;
438 struct tog_colors colors;
441 struct tog_parent_tree {
442 TAILQ_ENTRY(tog_parent_tree) entry;
443 struct got_tree_object *tree;
444 struct got_tree_entry *first_displayed_entry;
445 struct got_tree_entry *selected_entry;
449 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
451 struct tog_tree_view_state {
453 struct got_object_id *commit_id;/* commit which this tree belongs to */
454 struct got_tree_object *root; /* the commit's root tree entry */
455 struct got_tree_object *tree; /* currently displayed (sub-)tree */
456 struct got_tree_entry *first_displayed_entry;
457 struct got_tree_entry *last_displayed_entry;
458 struct got_tree_entry *selected_entry;
459 int ndisplayed, selected, show_ids;
460 struct tog_parent_trees parents; /* parent trees of current sub-tree */
462 struct got_repository *repo;
463 struct got_tree_entry *matched_entry;
464 struct tog_colors colors;
467 struct tog_reflist_entry {
468 TAILQ_ENTRY(tog_reflist_entry) entry;
469 struct got_reference *ref;
473 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
475 struct tog_ref_view_state {
476 struct tog_reflist_head refs;
477 struct tog_reflist_entry *first_displayed_entry;
478 struct tog_reflist_entry *last_displayed_entry;
479 struct tog_reflist_entry *selected_entry;
480 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
481 struct got_repository *repo;
482 struct tog_reflist_entry *matched_entry;
483 struct tog_colors colors;
487 * We implement two types of views: parent views and child views.
489 * The 'Tab' key switches focus between a parent view and its child view.
490 * Child views are shown side-by-side to their parent view, provided
491 * there is enough screen estate.
493 * When a new view is opened from within a parent view, this new view
494 * becomes a child view of the parent view, replacing any existing child.
496 * When a new view is opened from within a child view, this new view
497 * becomes a parent view which will obscure the views below until the
498 * user quits the new parent view by typing 'q'.
500 * This list of views contains parent views only.
501 * Child views are only pointed to by their parent view.
503 TAILQ_HEAD(tog_view_list_head, tog_view);
506 TAILQ_ENTRY(tog_view) entry;
509 int nlines, ncols, begin_y, begin_x;
510 int maxx, x; /* max column and current start column */
511 int lines, cols; /* copies of LINES and COLS */
512 int focussed; /* Only set on one parent or child view at a time. */
514 struct tog_view *parent;
515 struct tog_view *child;
518 * This flag is initially set on parent views when a new child view
519 * is created. It gets toggled when the 'Tab' key switches focus
520 * between parent and child.
521 * The flag indicates whether focus should be passed on to our child
522 * view if this parent view gets picked for focus after another parent
523 * view was closed. This prevents child views from losing focus in such
528 /* type-specific state */
529 enum tog_view_type type;
531 struct tog_diff_view_state diff;
532 struct tog_log_view_state log;
533 struct tog_blame_view_state blame;
534 struct tog_tree_view_state tree;
535 struct tog_ref_view_state ref;
538 const struct got_error *(*show)(struct tog_view *);
539 const struct got_error *(*input)(struct tog_view **,
540 struct tog_view *, int);
541 const struct got_error *(*close)(struct tog_view *);
543 const struct got_error *(*search_start)(struct tog_view *);
544 const struct got_error *(*search_next)(struct tog_view *);
547 #define TOG_SEARCH_FORWARD 1
548 #define TOG_SEARCH_BACKWARD 2
549 int search_next_done;
550 #define TOG_SEARCH_HAVE_MORE 1
551 #define TOG_SEARCH_NO_MORE 2
552 #define TOG_SEARCH_HAVE_NONE 3
557 static const struct got_error *open_diff_view(struct tog_view *,
558 struct got_object_id *, struct got_object_id *,
559 const char *, const char *, int, int, int, struct tog_view *,
560 struct got_repository *);
561 static const struct got_error *show_diff_view(struct tog_view *);
562 static const struct got_error *input_diff_view(struct tog_view **,
563 struct tog_view *, int);
564 static const struct got_error* close_diff_view(struct tog_view *);
565 static const struct got_error *search_start_diff_view(struct tog_view *);
566 static const struct got_error *search_next_diff_view(struct tog_view *);
568 static const struct got_error *open_log_view(struct tog_view *,
569 struct got_object_id *, struct got_repository *,
570 const char *, const char *, int);
571 static const struct got_error * show_log_view(struct tog_view *);
572 static const struct got_error *input_log_view(struct tog_view **,
573 struct tog_view *, int);
574 static const struct got_error *close_log_view(struct tog_view *);
575 static const struct got_error *search_start_log_view(struct tog_view *);
576 static const struct got_error *search_next_log_view(struct tog_view *);
578 static const struct got_error *open_blame_view(struct tog_view *, char *,
579 struct got_object_id *, struct got_repository *);
580 static const struct got_error *show_blame_view(struct tog_view *);
581 static const struct got_error *input_blame_view(struct tog_view **,
582 struct tog_view *, int);
583 static const struct got_error *close_blame_view(struct tog_view *);
584 static const struct got_error *search_start_blame_view(struct tog_view *);
585 static const struct got_error *search_next_blame_view(struct tog_view *);
587 static const struct got_error *open_tree_view(struct tog_view *,
588 struct got_object_id *, const char *, struct got_repository *);
589 static const struct got_error *show_tree_view(struct tog_view *);
590 static const struct got_error *input_tree_view(struct tog_view **,
591 struct tog_view *, int);
592 static const struct got_error *close_tree_view(struct tog_view *);
593 static const struct got_error *search_start_tree_view(struct tog_view *);
594 static const struct got_error *search_next_tree_view(struct tog_view *);
596 static const struct got_error *open_ref_view(struct tog_view *,
597 struct got_repository *);
598 static const struct got_error *show_ref_view(struct tog_view *);
599 static const struct got_error *input_ref_view(struct tog_view **,
600 struct tog_view *, int);
601 static const struct got_error *close_ref_view(struct tog_view *);
602 static const struct got_error *search_start_ref_view(struct tog_view *);
603 static const struct got_error *search_next_ref_view(struct tog_view *);
605 static volatile sig_atomic_t tog_sigwinch_received;
606 static volatile sig_atomic_t tog_sigpipe_received;
607 static volatile sig_atomic_t tog_sigcont_received;
608 static volatile sig_atomic_t tog_sigint_received;
609 static volatile sig_atomic_t tog_sigterm_received;
612 tog_sigwinch(int signo)
614 tog_sigwinch_received = 1;
618 tog_sigpipe(int signo)
620 tog_sigpipe_received = 1;
624 tog_sigcont(int signo)
626 tog_sigcont_received = 1;
630 tog_sigint(int signo)
632 tog_sigint_received = 1;
636 tog_sigterm(int signo)
638 tog_sigterm_received = 1;
642 tog_fatal_signal_received(void)
644 return (tog_sigpipe_received ||
645 tog_sigint_received || tog_sigint_received);
649 static const struct got_error *
650 view_close(struct tog_view *view)
652 const struct got_error *err = NULL;
655 view_close(view->child);
659 err = view->close(view);
661 del_panel(view->panel);
663 delwin(view->window);
668 static struct tog_view *
669 view_open(int nlines, int ncols, int begin_y, int begin_x,
670 enum tog_view_type type)
672 struct tog_view *view = calloc(1, sizeof(*view));
680 view->nlines = nlines ? nlines : LINES - begin_y;
681 view->ncols = ncols ? ncols : COLS - begin_x;
682 view->begin_y = begin_y;
683 view->begin_x = begin_x;
684 view->window = newwin(nlines, ncols, begin_y, begin_x);
685 if (view->window == NULL) {
689 view->panel = new_panel(view->window);
690 if (view->panel == NULL ||
691 set_panel_userptr(view->panel, view) != OK) {
696 keypad(view->window, TRUE);
701 view_split_begin_x(int begin_x)
703 if (begin_x > 0 || COLS < 120)
705 return (COLS - MAX(COLS / 2, 80));
708 static const struct got_error *view_resize(struct tog_view *);
710 static const struct got_error *
711 view_splitscreen(struct tog_view *view)
713 const struct got_error *err = NULL;
716 view->begin_x = view_split_begin_x(0);
717 view->nlines = LINES;
718 view->ncols = COLS - view->begin_x;
721 err = view_resize(view);
725 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
726 return got_error_from_errno("mvwin");
731 static const struct got_error *
732 view_fullscreen(struct tog_view *view)
734 const struct got_error *err = NULL;
738 view->nlines = LINES;
742 err = view_resize(view);
746 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
747 return got_error_from_errno("mvwin");
753 view_is_parent_view(struct tog_view *view)
755 return view->parent == NULL;
759 view_is_splitscreen(struct tog_view *view)
761 return view->begin_x > 0;
765 static const struct got_error *
766 view_resize(struct tog_view *view)
770 if (view->lines > LINES)
771 nlines = view->nlines - (view->lines - LINES);
773 nlines = view->nlines + (LINES - view->lines);
775 if (view->cols > COLS)
776 ncols = view->ncols - (view->cols - COLS);
778 ncols = view->ncols + (COLS - view->cols);
780 if (view->child && view_is_splitscreen(view->child)) {
781 view->child->begin_x = view_split_begin_x(view->begin_x);
782 if (view->child->begin_x == 0) {
785 view_fullscreen(view->child);
786 if (view->child->focussed)
787 show_panel(view->child->panel);
789 show_panel(view->panel);
791 ncols = view->child->begin_x;
793 view_splitscreen(view->child);
794 show_panel(view->child->panel);
796 } else if (view->parent == NULL)
799 if (wresize(view->window, nlines, ncols) == ERR)
800 return got_error_from_errno("wresize");
801 if (replace_panel(view->panel, view->window) == ERR)
802 return got_error_from_errno("replace_panel");
803 wclear(view->window);
805 view->nlines = nlines;
813 static const struct got_error *
814 view_close_child(struct tog_view *view)
816 const struct got_error *err = NULL;
818 if (view->child == NULL)
821 err = view_close(view->child);
826 static const struct got_error *
827 view_set_child(struct tog_view *view, struct tog_view *child)
830 child->parent = view;
832 return view_resize(view);
841 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
842 cols = 80; /* Default */
848 resize_term(lines, cols);
851 static const struct got_error *
852 view_search_start(struct tog_view *view)
854 const struct got_error *err = NULL;
858 if (view->search_started) {
859 regfree(&view->regex);
861 memset(&view->regmatch, 0, sizeof(view->regmatch));
863 view->search_started = 0;
865 if (view->nlines < 1)
868 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
869 wclrtoeol(view->window);
873 ret = wgetnstr(view->window, pattern, sizeof(pattern));
879 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
880 err = view->search_start(view);
882 regfree(&view->regex);
885 view->search_started = 1;
886 view->searching = TOG_SEARCH_FORWARD;
887 view->search_next_done = 0;
888 view->search_next(view);
894 static const struct got_error *
895 view_input(struct tog_view **new, int *done, struct tog_view *view,
896 struct tog_view_list_head *views)
898 const struct got_error *err = NULL;
904 /* Clear "no matches" indicator. */
905 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
906 view->search_next_done == TOG_SEARCH_HAVE_NONE)
907 view->search_next_done = TOG_SEARCH_HAVE_MORE;
909 if (view->searching && !view->search_next_done) {
910 errcode = pthread_mutex_unlock(&tog_mutex);
912 return got_error_set_errno(errcode,
913 "pthread_mutex_unlock");
915 errcode = pthread_mutex_lock(&tog_mutex);
917 return got_error_set_errno(errcode,
918 "pthread_mutex_lock");
919 view->search_next(view);
923 nodelay(stdscr, FALSE);
924 /* Allow threads to make progress while we are waiting for input. */
925 errcode = pthread_mutex_unlock(&tog_mutex);
927 return got_error_set_errno(errcode, "pthread_mutex_unlock");
928 ch = wgetch(view->window);
929 errcode = pthread_mutex_lock(&tog_mutex);
931 return got_error_set_errno(errcode, "pthread_mutex_lock");
932 nodelay(stdscr, TRUE);
934 if (tog_sigwinch_received || tog_sigcont_received) {
936 tog_sigwinch_received = 0;
937 tog_sigcont_received = 0;
938 TAILQ_FOREACH(v, views, entry) {
939 err = view_resize(v);
942 err = v->input(new, v, KEY_RESIZE);
946 err = view_resize(v->child);
949 err = v->child->input(new, v->child,
961 view->child->focussed = 1;
962 view->focus_child = 1;
963 } else if (view->parent) {
965 view->parent->focussed = 1;
966 view->parent->focus_child = 0;
967 if (!view_is_splitscreen(view))
968 err = view_fullscreen(view->parent);
972 err = view->input(new, view, ch);
979 if (view_is_parent_view(view)) {
980 if (view->child == NULL)
982 if (view_is_splitscreen(view->child)) {
984 view->child->focussed = 1;
985 err = view_fullscreen(view->child);
987 err = view_splitscreen(view->child);
990 err = view->child->input(new, view->child,
993 if (view_is_splitscreen(view)) {
994 view->parent->focussed = 0;
996 err = view_fullscreen(view);
998 err = view_splitscreen(view);
1000 err = view_resize(view->parent);
1004 err = view->input(new, view, KEY_RESIZE);
1010 if (view->search_start)
1011 view_search_start(view);
1013 err = view->input(new, view, ch);
1017 if (view->search_started && view->search_next) {
1018 view->searching = (ch == 'n' ?
1019 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1020 view->search_next_done = 0;
1021 view->search_next(view);
1023 err = view->input(new, view, ch);
1026 err = view->input(new, view, ch);
1034 view_vborder(struct tog_view *view)
1037 const struct tog_view *view_above;
1040 return view_vborder(view->parent);
1042 panel = panel_above(view->panel);
1046 view_above = panel_userptr(panel);
1047 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1048 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1052 view_needs_focus_indication(struct tog_view *view)
1054 if (view_is_parent_view(view)) {
1055 if (view->child == NULL || view->child->focussed)
1057 if (!view_is_splitscreen(view->child))
1059 } else if (!view_is_splitscreen(view))
1062 return view->focussed;
1065 static const struct got_error *
1066 view_loop(struct tog_view *view)
1068 const struct got_error *err = NULL;
1069 struct tog_view_list_head views;
1070 struct tog_view *new_view;
1071 int fast_refresh = 10;
1072 int done = 0, errcode;
1074 errcode = pthread_mutex_lock(&tog_mutex);
1076 return got_error_set_errno(errcode, "pthread_mutex_lock");
1079 TAILQ_INSERT_HEAD(&views, view, entry);
1082 err = view->show(view);
1087 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1088 /* Refresh fast during initialization, then become slower. */
1089 if (fast_refresh && fast_refresh-- == 0)
1090 halfdelay(10); /* switch to once per second */
1092 err = view_input(&new_view, &done, view, &views);
1096 struct tog_view *v, *prev = NULL;
1098 if (view_is_parent_view(view))
1099 prev = TAILQ_PREV(view, tog_view_list_head,
1101 else if (view->parent)
1102 prev = view->parent;
1105 view->parent->child = NULL;
1106 view->parent->focus_child = 0;
1108 err = view_resize(view->parent);
1112 TAILQ_REMOVE(&views, view, entry);
1114 err = view_close(view);
1119 TAILQ_FOREACH(v, &views, entry) {
1123 if (view == NULL && new_view == NULL) {
1124 /* No view has focus. Try to pick one. */
1127 else if (!TAILQ_EMPTY(&views)) {
1128 view = TAILQ_LAST(&views,
1129 tog_view_list_head);
1132 if (view->focus_child) {
1133 view->child->focussed = 1;
1141 struct tog_view *v, *t;
1142 /* Only allow one parent view per type. */
1143 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1144 if (v->type != new_view->type)
1146 TAILQ_REMOVE(&views, v, entry);
1147 err = view_close(v);
1152 TAILQ_INSERT_TAIL(&views, new_view, entry);
1156 if (view_is_parent_view(view)) {
1157 if (view->child && view->child->focussed)
1160 if (view->parent && view->parent->focussed)
1161 view = view->parent;
1163 show_panel(view->panel);
1164 if (view->child && view_is_splitscreen(view->child))
1165 show_panel(view->child->panel);
1166 if (view->parent && view_is_splitscreen(view)) {
1167 err = view->parent->show(view->parent);
1171 err = view->show(view);
1175 err = view->child->show(view->child);
1184 while (!TAILQ_EMPTY(&views)) {
1185 view = TAILQ_FIRST(&views);
1186 TAILQ_REMOVE(&views, view, entry);
1190 errcode = pthread_mutex_unlock(&tog_mutex);
1191 if (errcode && err == NULL)
1192 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1202 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1207 /* Create newly allocated wide-character string equivalent to a byte string. */
1208 static const struct got_error *
1209 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1212 const struct got_error *err = NULL;
1215 *wlen = mbstowcs(NULL, s, 0);
1216 if (*wlen == (size_t)-1) {
1218 if (errno != EILSEQ)
1219 return got_error_from_errno("mbstowcs");
1221 /* byte string invalid in current encoding; try to "fix" it */
1222 err = got_mbsavis(&vis, &vislen, s);
1225 *wlen = mbstowcs(NULL, vis, 0);
1226 if (*wlen == (size_t)-1) {
1227 err = got_error_from_errno("mbstowcs"); /* give up */
1232 *ws = calloc(*wlen + 1, sizeof(**ws));
1234 err = got_error_from_errno("calloc");
1238 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1239 err = got_error_from_errno("mbstowcs");
1250 static const struct got_error *
1251 expand_tab(char **ptr, const char *src)
1254 size_t len, n, idx = 0, sz = 0;
1257 n = len = strlen(src);
1258 dst = malloc(n + 1);
1260 return got_error_from_errno("malloc");
1262 while (idx < len && src[idx]) {
1263 const char c = src[idx];
1266 size_t nb = TABSIZE - sz % TABSIZE;
1269 p = realloc(dst, n + nb);
1272 return got_error_from_errno("realloc");
1277 memset(dst + sz, ' ', nb);
1280 dst[sz++] = src[idx];
1290 * Advance at most n columns from wline starting at offset off.
1291 * Return the index to the first character after the span operation.
1292 * Return the combined column width of all spanned wide character in
1296 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1298 int width, i, cols = 0;
1305 for (i = off; wline[i] != L'\0'; ++i) {
1306 if (wline[i] == L'\t')
1307 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1309 width = wcwidth(wline[i]);
1316 if (cols + width > n)
1326 * Format a line for display, ensuring that it won't overflow a width limit.
1327 * With scrolling, the width returned refers to the scrolled version of the
1328 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1330 static const struct got_error *
1331 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1332 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1334 const struct got_error *err = NULL;
1336 wchar_t *wline = NULL;
1345 err = expand_tab(&exstr, line);
1350 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1355 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1357 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1358 wline[wlen - 1] = L'\0';
1361 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1362 wline[wlen - 1] = L'\0';
1366 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1372 *scrollxp = scrollx;
1380 static const struct got_error*
1381 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1382 struct got_object_id *id, struct got_repository *repo)
1384 static const struct got_error *err = NULL;
1385 struct got_reflist_entry *re;
1391 TAILQ_FOREACH(re, refs, entry) {
1392 struct got_tag_object *tag = NULL;
1393 struct got_object_id *ref_id;
1396 name = got_ref_get_name(re->ref);
1397 if (strcmp(name, GOT_REF_HEAD) == 0)
1399 if (strncmp(name, "refs/", 5) == 0)
1401 if (strncmp(name, "got/", 4) == 0 &&
1402 strncmp(name, "got/backup/", 11) != 0)
1404 if (strncmp(name, "heads/", 6) == 0)
1406 if (strncmp(name, "remotes/", 8) == 0) {
1408 s = strstr(name, "/" GOT_REF_HEAD);
1409 if (s != NULL && s[strlen(s)] == '\0')
1412 err = got_ref_resolve(&ref_id, repo, re->ref);
1415 if (strncmp(name, "tags/", 5) == 0) {
1416 err = got_object_open_as_tag(&tag, repo, ref_id);
1418 if (err->code != GOT_ERR_OBJ_TYPE) {
1422 /* Ref points at something other than a tag. */
1427 cmp = got_object_id_cmp(tag ?
1428 got_object_tag_get_object_id(tag) : ref_id, id);
1431 got_object_tag_close(tag);
1435 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1436 s ? ", " : "", name) == -1) {
1437 err = got_error_from_errno("asprintf");
1448 static const struct got_error *
1449 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1454 smallerthan = strchr(author, '<');
1455 if (smallerthan && smallerthan[1] != '\0')
1456 author = smallerthan + 1;
1457 author[strcspn(author, "@>")] = '\0';
1458 return format_line(wauthor, author_width, NULL, author, 0, limit,
1462 static const struct got_error *
1463 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1464 struct got_object_id *id, const size_t date_display_cols,
1465 int author_display_cols)
1467 struct tog_log_view_state *s = &view->state.log;
1468 const struct got_error *err = NULL;
1469 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1470 char *logmsg0 = NULL, *logmsg = NULL;
1471 char *author = NULL;
1472 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1473 int author_width, logmsg_width;
1474 char *newline, *line = NULL;
1475 int col, limit, scrollx;
1476 const int avail = view->ncols;
1478 time_t committer_time;
1479 struct tog_color *tc;
1481 committer_time = got_object_commit_get_committer_time(commit);
1482 if (gmtime_r(&committer_time, &tm) == NULL)
1483 return got_error_from_errno("gmtime_r");
1484 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1485 return got_error(GOT_ERR_NO_SPACE);
1487 if (avail <= date_display_cols)
1488 limit = MIN(sizeof(datebuf) - 1, avail);
1490 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1491 tc = get_color(&s->colors, TOG_COLOR_DATE);
1493 wattr_on(view->window,
1494 COLOR_PAIR(tc->colorpair), NULL);
1495 waddnstr(view->window, datebuf, limit);
1497 wattr_off(view->window,
1498 COLOR_PAIR(tc->colorpair), NULL);
1505 err = got_object_id_str(&id_str, id);
1508 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1510 wattr_on(view->window,
1511 COLOR_PAIR(tc->colorpair), NULL);
1512 wprintw(view->window, "%.8s ", id_str);
1514 wattr_off(view->window,
1515 COLOR_PAIR(tc->colorpair), NULL);
1522 author = strdup(got_object_commit_get_author(commit));
1523 if (author == NULL) {
1524 err = got_error_from_errno("strdup");
1527 err = format_author(&wauthor, &author_width, author, avail - col, col);
1530 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1532 wattr_on(view->window,
1533 COLOR_PAIR(tc->colorpair), NULL);
1534 waddwstr(view->window, wauthor);
1536 wattr_off(view->window,
1537 COLOR_PAIR(tc->colorpair), NULL);
1538 col += author_width;
1539 while (col < avail && author_width < author_display_cols + 2) {
1540 waddch(view->window, ' ');
1547 err = got_object_commit_get_logmsg(&logmsg0, commit);
1551 while (*logmsg == '\n')
1553 newline = strchr(logmsg, '\n');
1556 limit = avail - col;
1557 if (view->child && view_is_splitscreen(view->child) && limit > 0)
1558 limit--; /* for the border */
1559 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1563 waddwstr(view->window, &wlogmsg[scrollx]);
1564 col += MAX(logmsg_width, 0);
1565 while (col < avail) {
1566 waddch(view->window, ' ');
1578 static struct commit_queue_entry *
1579 alloc_commit_queue_entry(struct got_commit_object *commit,
1580 struct got_object_id *id)
1582 struct commit_queue_entry *entry;
1584 entry = calloc(1, sizeof(*entry));
1589 entry->commit = commit;
1594 pop_commit(struct commit_queue *commits)
1596 struct commit_queue_entry *entry;
1598 entry = TAILQ_FIRST(&commits->head);
1599 TAILQ_REMOVE(&commits->head, entry, entry);
1600 got_object_commit_close(entry->commit);
1601 commits->ncommits--;
1602 /* Don't free entry->id! It is owned by the commit graph. */
1607 free_commits(struct commit_queue *commits)
1609 while (!TAILQ_EMPTY(&commits->head))
1610 pop_commit(commits);
1613 static const struct got_error *
1614 match_commit(int *have_match, struct got_object_id *id,
1615 struct got_commit_object *commit, regex_t *regex)
1617 const struct got_error *err = NULL;
1618 regmatch_t regmatch;
1619 char *id_str = NULL, *logmsg = NULL;
1623 err = got_object_id_str(&id_str, id);
1627 err = got_object_commit_get_logmsg(&logmsg, commit);
1631 if (regexec(regex, got_object_commit_get_author(commit), 1,
1632 ®match, 0) == 0 ||
1633 regexec(regex, got_object_commit_get_committer(commit), 1,
1634 ®match, 0) == 0 ||
1635 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1636 regexec(regex, logmsg, 1, ®match, 0) == 0)
1644 static const struct got_error *
1645 queue_commits(struct tog_log_thread_args *a)
1647 const struct got_error *err = NULL;
1650 * We keep all commits open throughout the lifetime of the log
1651 * view in order to avoid having to re-fetch commits from disk
1652 * while updating the display.
1655 struct got_object_id *id;
1656 struct got_commit_object *commit;
1657 struct commit_queue_entry *entry;
1660 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1662 if (err || id == NULL)
1665 err = got_object_open_as_commit(&commit, a->repo, id);
1668 entry = alloc_commit_queue_entry(commit, id);
1669 if (entry == NULL) {
1670 err = got_error_from_errno("alloc_commit_queue_entry");
1674 errcode = pthread_mutex_lock(&tog_mutex);
1676 err = got_error_set_errno(errcode,
1677 "pthread_mutex_lock");
1681 entry->idx = a->commits->ncommits;
1682 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1683 a->commits->ncommits++;
1685 if (*a->searching == TOG_SEARCH_FORWARD &&
1686 !*a->search_next_done) {
1688 err = match_commit(&have_match, id, commit, a->regex);
1692 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1695 errcode = pthread_mutex_unlock(&tog_mutex);
1696 if (errcode && err == NULL)
1697 err = got_error_set_errno(errcode,
1698 "pthread_mutex_unlock");
1701 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1707 select_commit(struct tog_log_view_state *s)
1709 struct commit_queue_entry *entry;
1712 entry = s->first_displayed_entry;
1714 if (ncommits == s->selected) {
1715 s->selected_entry = entry;
1718 entry = TAILQ_NEXT(entry, entry);
1723 static const struct got_error *
1724 draw_commits(struct tog_view *view)
1726 const struct got_error *err = NULL;
1727 struct tog_log_view_state *s = &view->state.log;
1728 struct commit_queue_entry *entry = s->selected_entry;
1729 const int limit = view->nlines;
1731 int ncommits, author_cols = 4;
1732 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1733 char *refs_str = NULL;
1735 struct tog_color *tc;
1736 static const size_t date_display_cols = 12;
1738 if (s->selected_entry &&
1739 !(view->searching && view->search_next_done == 0)) {
1740 struct got_reflist_head *refs;
1741 err = got_object_id_str(&id_str, s->selected_entry->id);
1744 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1745 s->selected_entry->id);
1747 err = build_refs_str(&refs_str, refs,
1748 s->selected_entry->id, s->repo);
1754 if (s->thread_args.commits_needed == 0)
1755 halfdelay(10); /* disable fast refresh */
1757 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1758 if (asprintf(&ncommits_str, " [%d/%d] %s",
1759 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1760 (view->searching && !view->search_next_done) ?
1761 "searching..." : "loading...") == -1) {
1762 err = got_error_from_errno("asprintf");
1766 const char *search_str = NULL;
1768 if (view->searching) {
1769 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1770 search_str = "no more matches";
1771 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1772 search_str = "no matches found";
1773 else if (!view->search_next_done)
1774 search_str = "searching...";
1777 if (asprintf(&ncommits_str, " [%d/%d] %s",
1778 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1779 search_str ? search_str :
1780 (refs_str ? refs_str : "")) == -1) {
1781 err = got_error_from_errno("asprintf");
1786 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1787 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1788 "........................................",
1789 s->in_repo_path, ncommits_str) == -1) {
1790 err = got_error_from_errno("asprintf");
1794 } else if (asprintf(&header, "commit %s%s",
1795 id_str ? id_str : "........................................",
1796 ncommits_str) == -1) {
1797 err = got_error_from_errno("asprintf");
1801 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1805 werase(view->window);
1807 if (view_needs_focus_indication(view))
1808 wstandout(view->window);
1809 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1811 wattr_on(view->window,
1812 COLOR_PAIR(tc->colorpair), NULL);
1813 waddwstr(view->window, wline);
1815 wattr_off(view->window,
1816 COLOR_PAIR(tc->colorpair), NULL);
1817 while (width < view->ncols) {
1818 waddch(view->window, ' ');
1821 if (view_needs_focus_indication(view))
1822 wstandend(view->window);
1827 /* Grow author column size if necessary, and set view->maxx. */
1828 entry = s->first_displayed_entry;
1832 char *author, *eol, *msg, *msg0;
1833 wchar_t *wauthor, *wmsg;
1835 if (ncommits >= limit - 1)
1837 author = strdup(got_object_commit_get_author(entry->commit));
1838 if (author == NULL) {
1839 err = got_error_from_errno("strdup");
1842 err = format_author(&wauthor, &width, author, COLS,
1844 if (author_cols < width)
1845 author_cols = width;
1848 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1852 while (*msg == '\n')
1854 if ((eol = strchr(msg, '\n')))
1856 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
1857 date_display_cols + author_cols, 0);
1860 view->maxx = MAX(view->maxx, width);
1864 entry = TAILQ_NEXT(entry, entry);
1867 entry = s->first_displayed_entry;
1868 s->last_displayed_entry = s->first_displayed_entry;
1871 if (ncommits >= limit - 1)
1873 if (ncommits == s->selected)
1874 wstandout(view->window);
1875 err = draw_commit(view, entry->commit, entry->id,
1876 date_display_cols, author_cols);
1877 if (ncommits == s->selected)
1878 wstandend(view->window);
1882 s->last_displayed_entry = entry;
1883 entry = TAILQ_NEXT(entry, entry);
1898 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1900 struct commit_queue_entry *entry;
1903 entry = TAILQ_FIRST(&s->commits.head);
1904 if (s->first_displayed_entry == entry)
1907 entry = s->first_displayed_entry;
1908 while (entry && nscrolled < maxscroll) {
1909 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1911 s->first_displayed_entry = entry;
1917 static const struct got_error *
1918 trigger_log_thread(struct tog_view *view, int wait)
1920 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1923 halfdelay(1); /* fast refresh while loading commits */
1925 while (ta->commits_needed > 0 || ta->load_all) {
1926 if (ta->log_complete)
1929 /* Wake the log thread. */
1930 errcode = pthread_cond_signal(&ta->need_commits);
1932 return got_error_set_errno(errcode,
1933 "pthread_cond_signal");
1936 * The mutex will be released while the view loop waits
1937 * in wgetch(), at which time the log thread will run.
1942 /* Display progress update in log view. */
1943 show_log_view(view);
1947 /* Wait right here while next commit is being loaded. */
1948 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1950 return got_error_set_errno(errcode,
1951 "pthread_cond_wait");
1953 /* Display progress update in log view. */
1954 show_log_view(view);
1962 static const struct got_error *
1963 log_scroll_down(struct tog_view *view, int maxscroll)
1965 struct tog_log_view_state *s = &view->state.log;
1966 const struct got_error *err = NULL;
1967 struct commit_queue_entry *pentry;
1968 int nscrolled = 0, ncommits_needed;
1970 if (s->last_displayed_entry == NULL)
1973 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1974 if (s->commits.ncommits < ncommits_needed &&
1975 !s->thread_args.log_complete) {
1977 * Ask the log thread for required amount of commits.
1979 s->thread_args.commits_needed += maxscroll;
1980 err = trigger_log_thread(view, 1);
1986 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1990 s->last_displayed_entry = pentry;
1992 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1995 s->first_displayed_entry = pentry;
1996 } while (++nscrolled < maxscroll);
2001 static const struct got_error *
2002 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
2003 struct got_commit_object *commit, struct got_object_id *commit_id,
2004 struct tog_view *log_view, struct got_repository *repo)
2006 const struct got_error *err;
2007 struct got_object_qid *parent_id;
2008 struct tog_view *diff_view;
2010 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2011 if (diff_view == NULL)
2012 return got_error_from_errno("view_open");
2014 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2015 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2016 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2018 *new_view = diff_view;
2022 static const struct got_error *
2023 tree_view_visit_subtree(struct tog_tree_view_state *s,
2024 struct got_tree_object *subtree)
2026 struct tog_parent_tree *parent;
2028 parent = calloc(1, sizeof(*parent));
2030 return got_error_from_errno("calloc");
2032 parent->tree = s->tree;
2033 parent->first_displayed_entry = s->first_displayed_entry;
2034 parent->selected_entry = s->selected_entry;
2035 parent->selected = s->selected;
2036 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2039 s->first_displayed_entry = NULL;
2043 static const struct got_error *
2044 tree_view_walk_path(struct tog_tree_view_state *s,
2045 struct got_commit_object *commit, const char *path)
2047 const struct got_error *err = NULL;
2048 struct got_tree_object *tree = NULL;
2050 char *slash, *subpath = NULL;
2052 /* Walk the path and open corresponding tree objects. */
2055 struct got_tree_entry *te;
2056 struct got_object_id *tree_id;
2062 /* Ensure the correct subtree entry is selected. */
2063 slash = strchr(p, '/');
2065 te_name = strdup(p);
2067 te_name = strndup(p, slash - p);
2068 if (te_name == NULL) {
2069 err = got_error_from_errno("strndup");
2072 te = got_object_tree_find_entry(s->tree, te_name);
2074 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2079 s->first_displayed_entry = s->selected_entry = te;
2081 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2082 break; /* jump to this file's entry */
2084 slash = strchr(p, '/');
2086 subpath = strndup(path, slash - path);
2088 subpath = strdup(path);
2089 if (subpath == NULL) {
2090 err = got_error_from_errno("strdup");
2094 err = got_object_id_by_path(&tree_id, s->repo, commit,
2099 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2104 err = tree_view_visit_subtree(s, tree);
2106 got_object_tree_close(tree);
2120 static const struct got_error *
2121 browse_commit_tree(struct tog_view **new_view, int begin_x,
2122 struct commit_queue_entry *entry, const char *path,
2123 const char *head_ref_name, struct got_repository *repo)
2125 const struct got_error *err = NULL;
2126 struct tog_tree_view_state *s;
2127 struct tog_view *tree_view;
2129 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2130 if (tree_view == NULL)
2131 return got_error_from_errno("view_open");
2133 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2136 s = &tree_view->state.tree;
2138 *new_view = tree_view;
2140 if (got_path_is_root_dir(path))
2143 return tree_view_walk_path(s, entry->commit, path);
2146 static const struct got_error *
2147 block_signals_used_by_main_thread(void)
2152 if (sigemptyset(&sigset) == -1)
2153 return got_error_from_errno("sigemptyset");
2155 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2156 if (sigaddset(&sigset, SIGWINCH) == -1)
2157 return got_error_from_errno("sigaddset");
2158 if (sigaddset(&sigset, SIGCONT) == -1)
2159 return got_error_from_errno("sigaddset");
2160 if (sigaddset(&sigset, SIGINT) == -1)
2161 return got_error_from_errno("sigaddset");
2162 if (sigaddset(&sigset, SIGTERM) == -1)
2163 return got_error_from_errno("sigaddset");
2165 /* ncurses handles SIGTSTP */
2166 if (sigaddset(&sigset, SIGTSTP) == -1)
2167 return got_error_from_errno("sigaddset");
2169 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2171 return got_error_set_errno(errcode, "pthread_sigmask");
2177 log_thread(void *arg)
2179 const struct got_error *err = NULL;
2181 struct tog_log_thread_args *a = arg;
2184 err = block_signals_used_by_main_thread();
2188 while (!done && !err && !tog_fatal_signal_received()) {
2189 err = queue_commits(a);
2191 if (err->code != GOT_ERR_ITER_COMPLETED)
2195 } else if (a->commits_needed > 0 && !a->load_all)
2196 a->commits_needed--;
2198 errcode = pthread_mutex_lock(&tog_mutex);
2200 err = got_error_set_errno(errcode,
2201 "pthread_mutex_lock");
2203 } else if (*a->quit)
2205 else if (*a->first_displayed_entry == NULL) {
2206 *a->first_displayed_entry =
2207 TAILQ_FIRST(&a->commits->head);
2208 *a->selected_entry = *a->first_displayed_entry;
2211 errcode = pthread_cond_signal(&a->commit_loaded);
2213 err = got_error_set_errno(errcode,
2214 "pthread_cond_signal");
2215 pthread_mutex_unlock(&tog_mutex);
2220 a->commits_needed = 0;
2222 if (a->commits_needed == 0 && !a->load_all) {
2223 errcode = pthread_cond_wait(&a->need_commits,
2226 err = got_error_set_errno(errcode,
2227 "pthread_cond_wait");
2233 errcode = pthread_mutex_unlock(&tog_mutex);
2234 if (errcode && err == NULL)
2235 err = got_error_set_errno(errcode,
2236 "pthread_mutex_unlock");
2238 a->log_complete = 1;
2242 static const struct got_error *
2243 stop_log_thread(struct tog_log_view_state *s)
2245 const struct got_error *err = NULL;
2250 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2252 return got_error_set_errno(errcode,
2253 "pthread_cond_signal");
2254 errcode = pthread_mutex_unlock(&tog_mutex);
2256 return got_error_set_errno(errcode,
2257 "pthread_mutex_unlock");
2258 errcode = pthread_join(s->thread, (void **)&err);
2260 return got_error_set_errno(errcode, "pthread_join");
2261 errcode = pthread_mutex_lock(&tog_mutex);
2263 return got_error_set_errno(errcode,
2264 "pthread_mutex_lock");
2265 s->thread = 0; //NULL;
2268 if (s->thread_args.repo) {
2269 err = got_repo_close(s->thread_args.repo);
2270 s->thread_args.repo = NULL;
2273 if (s->thread_args.pack_fds) {
2274 const struct got_error *pack_err =
2275 got_repo_pack_fds_close(s->thread_args.pack_fds);
2278 s->thread_args.pack_fds = NULL;
2281 if (s->thread_args.graph) {
2282 got_commit_graph_close(s->thread_args.graph);
2283 s->thread_args.graph = NULL;
2289 static const struct got_error *
2290 close_log_view(struct tog_view *view)
2292 const struct got_error *err = NULL;
2293 struct tog_log_view_state *s = &view->state.log;
2296 err = stop_log_thread(s);
2298 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2299 if (errcode && err == NULL)
2300 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2302 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2303 if (errcode && err == NULL)
2304 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2306 free_commits(&s->commits);
2307 free(s->in_repo_path);
2308 s->in_repo_path = NULL;
2311 free(s->head_ref_name);
2312 s->head_ref_name = NULL;
2316 static const struct got_error *
2317 search_start_log_view(struct tog_view *view)
2319 struct tog_log_view_state *s = &view->state.log;
2321 s->matched_entry = NULL;
2322 s->search_entry = NULL;
2326 static const struct got_error *
2327 search_next_log_view(struct tog_view *view)
2329 const struct got_error *err = NULL;
2330 struct tog_log_view_state *s = &view->state.log;
2331 struct commit_queue_entry *entry;
2333 /* Display progress update in log view. */
2334 show_log_view(view);
2338 if (s->search_entry) {
2340 errcode = pthread_mutex_unlock(&tog_mutex);
2342 return got_error_set_errno(errcode,
2343 "pthread_mutex_unlock");
2344 ch = wgetch(view->window);
2345 errcode = pthread_mutex_lock(&tog_mutex);
2347 return got_error_set_errno(errcode,
2348 "pthread_mutex_lock");
2349 if (ch == KEY_BACKSPACE) {
2350 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2353 if (view->searching == TOG_SEARCH_FORWARD)
2354 entry = TAILQ_NEXT(s->search_entry, entry);
2356 entry = TAILQ_PREV(s->search_entry,
2357 commit_queue_head, entry);
2358 } else if (s->matched_entry) {
2359 int matched_idx = s->matched_entry->idx;
2360 int selected_idx = s->selected_entry->idx;
2363 * If the user has moved the cursor after we hit a match,
2364 * the position from where we should continue searching
2365 * might have changed.
2367 if (view->searching == TOG_SEARCH_FORWARD) {
2368 if (matched_idx > selected_idx)
2369 entry = TAILQ_NEXT(s->selected_entry, entry);
2371 entry = TAILQ_NEXT(s->matched_entry, entry);
2373 if (matched_idx < selected_idx)
2374 entry = TAILQ_PREV(s->selected_entry,
2375 commit_queue_head, entry);
2377 entry = TAILQ_PREV(s->matched_entry,
2378 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 == 0) { //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);
2643 if (s->first_displayed_entry == NULL)
2645 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2646 s->selected = MAX(0, s->selected - nscroll - 1);
2648 log_scroll_up(s, nscroll);
2656 if (s->first_displayed_entry == NULL)
2658 if (s->selected < MIN(view->nlines - 2,
2659 s->commits.ncommits - 1))
2662 err = log_scroll_down(view, 1);
2670 /* We don't know yet how many commits, so we're forced to
2671 * traverse them all. */
2672 if (!s->thread_args.log_complete) {
2673 s->thread_args.load_all = 1;
2674 return trigger_log_thread(view, 0);
2678 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2679 for (n = 0; n < view->nlines - 1; n++) {
2682 s->first_displayed_entry = entry;
2683 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2686 s->selected = n - 1;
2698 struct commit_queue_entry *first;
2699 first = s->first_displayed_entry;
2702 err = log_scroll_down(view, nscroll);
2705 if (first == s->first_displayed_entry &&
2706 s->selected < MIN(view->nlines - 2,
2707 s->commits.ncommits - 1)) {
2708 /* can't scroll further down */
2709 s->selected += MIN(s->last_displayed_entry->idx -
2710 s->selected_entry->idx, nscroll + 1);
2716 if (s->selected > view->nlines - 2)
2717 s->selected = view->nlines - 2;
2718 if (s->selected > s->commits.ncommits - 1)
2719 s->selected = s->commits.ncommits - 1;
2721 if (s->commits.ncommits < view->nlines - 1 &&
2722 !s->thread_args.log_complete) {
2723 s->thread_args.commits_needed += (view->nlines - 1) -
2724 s->commits.ncommits;
2725 err = trigger_log_thread(view, 1);
2730 if (s->selected_entry == NULL)
2732 if (view_is_parent_view(view))
2733 begin_x = view_split_begin_x(view->begin_x);
2734 err = open_diff_view_for_commit(&diff_view, begin_x,
2735 s->selected_entry->commit, s->selected_entry->id,
2740 diff_view->focussed = 1;
2741 if (view_is_parent_view(view)) {
2742 err = view_close_child(view);
2745 err = view_set_child(view, diff_view);
2748 view->focus_child = 1;
2750 *new_view = diff_view;
2753 if (s->selected_entry == NULL)
2755 if (view_is_parent_view(view))
2756 begin_x = view_split_begin_x(view->begin_x);
2757 err = browse_commit_tree(&tree_view, begin_x,
2758 s->selected_entry, s->in_repo_path, s->head_ref_name,
2763 tree_view->focussed = 1;
2764 if (view_is_parent_view(view)) {
2765 err = view_close_child(view);
2768 err = view_set_child(view, tree_view);
2771 view->focus_child = 1;
2773 *new_view = tree_view;
2778 if (ch == KEY_BACKSPACE &&
2779 got_path_is_root_dir(s->in_repo_path))
2781 err = stop_log_thread(s);
2784 if (ch == KEY_BACKSPACE) {
2786 err = got_path_dirname(&parent_path, s->in_repo_path);
2789 free(s->in_repo_path);
2790 s->in_repo_path = parent_path;
2791 s->thread_args.in_repo_path = s->in_repo_path;
2792 } else if (ch == CTRL('l')) {
2793 struct got_object_id *start_id;
2794 err = got_repo_match_object_id(&start_id, NULL,
2795 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2796 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2800 s->start_id = start_id;
2801 s->thread_args.start_id = s->start_id;
2803 s->log_branches = !s->log_branches;
2805 if (s->thread_args.pack_fds == NULL) {
2806 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2810 err = got_repo_open(&s->thread_args.repo,
2811 got_repo_get_path(s->repo), NULL,
2812 s->thread_args.pack_fds);
2816 err = tog_load_refs(s->repo, 0);
2819 err = got_commit_graph_open(&s->thread_args.graph,
2820 s->in_repo_path, !s->log_branches);
2823 err = got_commit_graph_iter_start(s->thread_args.graph,
2824 s->start_id, s->repo, NULL, NULL);
2827 free_commits(&s->commits);
2828 s->first_displayed_entry = NULL;
2829 s->last_displayed_entry = NULL;
2830 s->selected_entry = NULL;
2832 s->thread_args.log_complete = 0;
2834 s->thread_args.commits_needed = view->nlines;
2835 s->matched_entry = NULL;
2836 s->search_entry = NULL;
2839 if (view_is_parent_view(view))
2840 begin_x = view_split_begin_x(view->begin_x);
2841 ref_view = view_open(view->nlines, view->ncols,
2842 view->begin_y, begin_x, TOG_VIEW_REF);
2843 if (ref_view == NULL)
2844 return got_error_from_errno("view_open");
2845 err = open_ref_view(ref_view, s->repo);
2847 view_close(ref_view);
2851 ref_view->focussed = 1;
2852 if (view_is_parent_view(view)) {
2853 err = view_close_child(view);
2856 err = view_set_child(view, ref_view);
2859 view->focus_child = 1;
2861 *new_view = ref_view;
2870 static const struct got_error *
2871 apply_unveil(const char *repo_path, const char *worktree_path)
2873 const struct got_error *error;
2876 if (unveil("gmon.out", "rwc") != 0)
2877 return got_error_from_errno2("unveil", "gmon.out");
2879 if (repo_path && unveil(repo_path, "r") != 0)
2880 return got_error_from_errno2("unveil", repo_path);
2882 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2883 return got_error_from_errno2("unveil", worktree_path);
2885 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2886 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2888 error = got_privsep_unveil_exec_helpers();
2892 if (unveil(NULL, NULL) != 0)
2893 return got_error_from_errno("unveil");
2902 * Override default signal handlers before starting ncurses.
2903 * This should prevent ncurses from installing its own
2904 * broken cleanup() signal handler.
2906 signal(SIGWINCH, tog_sigwinch);
2907 signal(SIGPIPE, tog_sigpipe);
2908 signal(SIGCONT, tog_sigcont);
2909 signal(SIGINT, tog_sigint);
2910 signal(SIGTERM, tog_sigterm);
2914 halfdelay(1); /* Do fast refresh while initial view is loading. */
2917 intrflush(stdscr, FALSE);
2918 keypad(stdscr, TRUE);
2920 if (getenv("TOG_COLORS") != NULL) {
2922 use_default_colors();
2926 static const struct got_error *
2927 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2928 struct got_repository *repo, struct got_worktree *worktree)
2930 const struct got_error *err = NULL;
2933 *in_repo_path = strdup("/");
2934 if (*in_repo_path == NULL)
2935 return got_error_from_errno("strdup");
2940 const char *prefix = got_worktree_get_path_prefix(worktree);
2943 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2946 if (asprintf(in_repo_path, "%s%s%s", prefix,
2947 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2949 err = got_error_from_errno("asprintf");
2950 *in_repo_path = NULL;
2954 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2959 static const struct got_error *
2960 cmd_log(int argc, char *argv[])
2962 const struct got_error *error;
2963 struct got_repository *repo = NULL;
2964 struct got_worktree *worktree = NULL;
2965 struct got_object_id *start_id = NULL;
2966 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2967 char *start_commit = NULL, *label = NULL;
2968 struct got_reference *ref = NULL;
2969 const char *head_ref_name = NULL;
2970 int ch, log_branches = 0;
2971 struct tog_view *view;
2972 int *pack_fds = NULL;
2974 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2980 start_commit = optarg;
2983 repo_path = realpath(optarg, NULL);
2984 if (repo_path == NULL)
2985 return got_error_from_errno2("realpath",
3000 error = got_repo_pack_fds_open(&pack_fds);
3004 if (repo_path == NULL) {
3005 cwd = getcwd(NULL, 0);
3007 return got_error_from_errno("getcwd");
3008 error = got_worktree_open(&worktree, cwd);
3009 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3013 strdup(got_worktree_get_repo_path(worktree));
3015 repo_path = strdup(cwd);
3016 if (repo_path == NULL) {
3017 error = got_error_from_errno("strdup");
3022 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3026 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3033 error = apply_unveil(got_repo_get_path(repo),
3034 worktree ? got_worktree_get_root_path(worktree) : NULL);
3038 /* already loaded by tog_log_with_path()? */
3039 if (TAILQ_EMPTY(&tog_refs)) {
3040 error = tog_load_refs(repo, 0);
3045 if (start_commit == NULL) {
3046 error = got_repo_match_object_id(&start_id, &label,
3047 worktree ? got_worktree_get_head_ref_name(worktree) :
3048 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3051 head_ref_name = label;
3053 error = got_ref_open(&ref, repo, start_commit, 0);
3055 head_ref_name = got_ref_get_name(ref);
3056 else if (error->code != GOT_ERR_NOT_REF)
3058 error = got_repo_match_object_id(&start_id, NULL,
3059 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3064 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3066 error = got_error_from_errno("view_open");
3069 error = open_log_view(view, start_id, repo, head_ref_name,
3070 in_repo_path, log_branches);
3074 /* Release work tree lock. */
3075 got_worktree_close(worktree);
3078 error = view_loop(view);
3088 const struct got_error *close_err = got_repo_close(repo);
3093 got_worktree_close(worktree);
3095 const struct got_error *pack_err =
3096 got_repo_pack_fds_close(pack_fds);
3108 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3109 "[-w] object1 object2\n", getprogname());
3114 match_line(const char *line, regex_t *regex, size_t nmatch,
3115 regmatch_t *regmatch)
3117 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3121 match_color(struct tog_colors *colors, const char *line)
3123 struct tog_color *tc = NULL;
3125 STAILQ_FOREACH(tc, colors, entry) {
3126 if (match_line(line, &tc->regex, 0, NULL))
3133 static const struct got_error *
3134 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3135 WINDOW *window, int skipcol, regmatch_t *regmatch)
3137 const struct got_error *err = NULL;
3139 wchar_t *wline = NULL;
3140 int rme, rms, n, width, scrollx;
3141 int width0 = 0, width1 = 0, width2 = 0;
3142 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3146 rms = regmatch->rm_so;
3147 rme = regmatch->rm_eo;
3149 err = expand_tab(&exstr, line);
3153 /* Split the line into 3 segments, according to match offsets. */
3154 seg0 = strndup(exstr, rms);
3156 err = got_error_from_errno("strndup");
3159 seg1 = strndup(exstr + rms, rme - rms);
3161 err = got_error_from_errno("strndup");
3164 seg2 = strdup(exstr + rme);
3166 err = got_error_from_errno("strndup");
3170 /* draw up to matched token if we haven't scrolled past it */
3171 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3175 n = MAX(width0 - skipcol, 0);
3178 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3179 wlimit, col_tab_align, 1);
3182 waddwstr(window, &wline[scrollx]);
3192 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3196 wlen = wcslen(wline);
3198 width = wcwidth(wline[i]);
3200 /* should not happen, tabs are expanded */
3201 err = got_error(GOT_ERR_RANGE);
3204 if (width0 + w + width > skipcol)
3209 /* draw (visible part of) matched token (if scrolled into it) */
3210 if (width1 - w > 0) {
3211 wattron(window, A_STANDOUT);
3212 waddwstr(window, &wline[i]);
3213 wattroff(window, A_STANDOUT);
3214 wlimit -= (width1 - w);
3215 *wtotal += (width1 - w);
3219 if (wlimit > 0) { /* draw rest of line */
3221 if (skipcol > width0 + width1) {
3222 err = format_line(&wline, &width2, &scrollx, seg2,
3223 skipcol - (width0 + width1), wlimit,
3227 waddwstr(window, &wline[scrollx]);
3229 err = format_line(&wline, &width2, NULL, seg2, 0,
3230 wlimit, col_tab_align, 1);
3233 waddwstr(window, wline);
3246 static const struct got_error *
3247 draw_file(struct tog_view *view, const char *header)
3249 struct tog_diff_view_state *s = &view->state.diff;
3250 regmatch_t *regmatch = &view->regmatch;
3251 const struct got_error *err;
3254 size_t linesize = 0;
3256 struct tog_color *tc;
3259 int max_lines = view->nlines;
3260 int nlines = s->nlines;
3263 line_offset = s->line_offsets[s->first_displayed_line - 1];
3264 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3265 return got_error_from_errno("fseek");
3267 werase(view->window);
3270 if (asprintf(&line, "[%d/%d] %s",
3271 s->first_displayed_line - 1 + s->selected_line, nlines,
3273 return got_error_from_errno("asprintf");
3274 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3280 if (view_needs_focus_indication(view))
3281 wstandout(view->window);
3282 waddwstr(view->window, wline);
3285 if (view_needs_focus_indication(view))
3286 wstandend(view->window);
3287 if (width <= view->ncols - 1)
3288 waddch(view->window, '\n');
3298 while (max_lines > 0 && nprinted < max_lines) {
3299 linelen = getline(&line, &linesize, s->f);
3300 if (linelen == -1) {
3306 return got_ferror(s->f, GOT_ERR_IO);
3309 /* Set view->maxx based on full line length. */
3310 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3316 view->maxx = MAX(view->maxx, width);
3320 tc = match_color(&s->colors, line);
3322 wattr_on(view->window,
3323 COLOR_PAIR(tc->colorpair), NULL);
3324 if (s->first_displayed_line + nprinted == s->matched_line &&
3325 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3326 err = add_matched_line(&width, line, view->ncols, 0,
3327 view->window, view->x, regmatch);
3334 err = format_line(&wline, &width, &skip, line,
3335 view->x, view->ncols, 0, view->x ? 1 : 0);
3340 waddwstr(view->window, &wline[skip]);
3345 wattr_off(view->window,
3346 COLOR_PAIR(tc->colorpair), NULL);
3347 if (width <= view->ncols - 1)
3348 waddch(view->window, '\n');
3353 s->last_displayed_line = s->first_displayed_line +
3356 s->last_displayed_line = s->first_displayed_line;
3361 while (nprinted < view->nlines) {
3362 waddch(view->window, '\n');
3366 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3372 wstandout(view->window);
3373 waddwstr(view->window, wline);
3376 wstandend(view->window);
3383 get_datestr(time_t *time, char *datebuf)
3385 struct tm mytm, *tm;
3388 tm = gmtime_r(time, &mytm);
3391 s = asctime_r(tm, datebuf);
3394 p = strchr(s, '\n');
3400 static const struct got_error *
3401 get_changed_paths(struct got_pathlist_head *paths,
3402 struct got_commit_object *commit, struct got_repository *repo)
3404 const struct got_error *err = NULL;
3405 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3406 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3407 struct got_object_qid *qid;
3409 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3411 struct got_commit_object *pcommit;
3412 err = got_object_open_as_commit(&pcommit, repo,
3417 tree_id1 = got_object_id_dup(
3418 got_object_commit_get_tree_id(pcommit));
3419 if (tree_id1 == NULL) {
3420 got_object_commit_close(pcommit);
3421 return got_error_from_errno("got_object_id_dup");
3423 got_object_commit_close(pcommit);
3428 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3433 tree_id2 = got_object_commit_get_tree_id(commit);
3434 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3438 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3439 got_diff_tree_collect_changed_paths, paths, 0);
3442 got_object_tree_close(tree1);
3444 got_object_tree_close(tree2);
3449 static const struct got_error *
3450 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3454 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3456 return got_error_from_errno("reallocarray");
3458 (*line_offsets)[*nlines] = off;
3463 static const struct got_error *
3464 write_commit_info(off_t **line_offsets, size_t *nlines,
3465 struct got_object_id *commit_id, struct got_reflist_head *refs,
3466 struct got_repository *repo, FILE *outfile)
3468 const struct got_error *err = NULL;
3469 char datebuf[26], *datestr;
3470 struct got_commit_object *commit;
3471 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3472 time_t committer_time;
3473 const char *author, *committer;
3474 char *refs_str = NULL;
3475 struct got_pathlist_head changed_paths;
3476 struct got_pathlist_entry *pe;
3480 TAILQ_INIT(&changed_paths);
3483 err = build_refs_str(&refs_str, refs, commit_id, repo);
3488 err = got_object_open_as_commit(&commit, repo, commit_id);
3492 err = got_object_id_str(&id_str, commit_id);
3494 err = got_error_from_errno("got_object_id_str");
3498 err = add_line_offset(line_offsets, nlines, 0);
3502 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3503 refs_str ? refs_str : "", refs_str ? ")" : "");
3505 err = got_error_from_errno("fprintf");
3509 err = add_line_offset(line_offsets, nlines, outoff);
3513 n = fprintf(outfile, "from: %s\n",
3514 got_object_commit_get_author(commit));
3516 err = got_error_from_errno("fprintf");
3520 err = add_line_offset(line_offsets, nlines, outoff);
3524 committer_time = got_object_commit_get_committer_time(commit);
3525 datestr = get_datestr(&committer_time, datebuf);
3527 n = fprintf(outfile, "date: %s UTC\n", datestr);
3529 err = got_error_from_errno("fprintf");
3533 err = add_line_offset(line_offsets, nlines, outoff);
3537 author = got_object_commit_get_author(commit);
3538 committer = got_object_commit_get_committer(commit);
3539 if (strcmp(author, committer) != 0) {
3540 n = fprintf(outfile, "via: %s\n", committer);
3542 err = got_error_from_errno("fprintf");
3546 err = add_line_offset(line_offsets, nlines, outoff);
3550 if (got_object_commit_get_nparents(commit) > 1) {
3551 const struct got_object_id_queue *parent_ids;
3552 struct got_object_qid *qid;
3554 parent_ids = got_object_commit_get_parent_ids(commit);
3555 STAILQ_FOREACH(qid, parent_ids, entry) {
3556 err = got_object_id_str(&id_str, &qid->id);
3559 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3561 err = got_error_from_errno("fprintf");
3565 err = add_line_offset(line_offsets, nlines, outoff);
3573 err = got_object_commit_get_logmsg(&logmsg, commit);
3577 while ((line = strsep(&s, "\n")) != NULL) {
3578 n = fprintf(outfile, "%s\n", line);
3580 err = got_error_from_errno("fprintf");
3584 err = add_line_offset(line_offsets, nlines, outoff);
3589 err = get_changed_paths(&changed_paths, commit, repo);
3592 TAILQ_FOREACH(pe, &changed_paths, entry) {
3593 struct got_diff_changed_path *cp = pe->data;
3594 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3596 err = got_error_from_errno("fprintf");
3600 err = add_line_offset(line_offsets, nlines, outoff);
3603 free((char *)pe->path);
3607 fputc('\n', outfile);
3609 err = add_line_offset(line_offsets, nlines, outoff);
3611 got_pathlist_free(&changed_paths);
3615 got_object_commit_close(commit);
3617 free(*line_offsets);
3618 *line_offsets = NULL;
3624 static const struct got_error *
3625 create_diff(struct tog_diff_view_state *s)
3627 const struct got_error *err = NULL;
3631 free(s->line_offsets);
3632 s->line_offsets = malloc(sizeof(off_t));
3633 if (s->line_offsets == NULL)
3634 return got_error_from_errno("malloc");
3639 err = got_error_from_errno("got_opentemp");
3642 if (s->f && fclose(s->f) == EOF) {
3643 err = got_error_from_errno("fclose");
3649 err = got_object_get_type(&obj_type, s->repo, s->id1);
3651 err = got_object_get_type(&obj_type, s->repo, s->id2);
3656 case GOT_OBJ_TYPE_BLOB:
3657 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3658 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3659 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3662 case GOT_OBJ_TYPE_TREE:
3663 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3664 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3665 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3667 case GOT_OBJ_TYPE_COMMIT: {
3668 const struct got_object_id_queue *parent_ids;
3669 struct got_object_qid *pid;
3670 struct got_commit_object *commit2;
3671 struct got_reflist_head *refs;
3673 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3676 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3677 /* Show commit info if we're diffing to a parent/root commit. */
3678 if (s->id1 == NULL) {
3679 err = write_commit_info(&s->line_offsets, &s->nlines,
3680 s->id2, refs, s->repo, s->f);
3684 parent_ids = got_object_commit_get_parent_ids(commit2);
3685 STAILQ_FOREACH(pid, parent_ids, entry) {
3686 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3687 err = write_commit_info(
3688 &s->line_offsets, &s->nlines,
3689 s->id2, refs, s->repo, s->f);
3696 got_object_commit_close(commit2);
3698 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3699 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3700 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3704 err = got_error(GOT_ERR_OBJ_TYPE);
3710 if (s->f && fflush(s->f) != 0 && err == NULL)
3711 err = got_error_from_errno("fflush");
3716 diff_view_indicate_progress(struct tog_view *view)
3718 mvwaddstr(view->window, 0, 0, "diffing...");
3723 static const struct got_error *
3724 search_start_diff_view(struct tog_view *view)
3726 struct tog_diff_view_state *s = &view->state.diff;
3728 s->matched_line = 0;
3732 static const struct got_error *
3733 search_next_diff_view(struct tog_view *view)
3735 struct tog_diff_view_state *s = &view->state.diff;
3736 const struct got_error *err = NULL;
3739 size_t linesize = 0;
3742 if (!view->searching) {
3743 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3747 if (s->matched_line) {
3748 if (view->searching == TOG_SEARCH_FORWARD)
3749 lineno = s->matched_line + 1;
3751 lineno = s->matched_line - 1;
3753 lineno = s->first_displayed_line;
3758 if (lineno <= 0 || lineno > s->nlines) {
3759 if (s->matched_line == 0) {
3760 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3764 if (view->searching == TOG_SEARCH_FORWARD)
3770 offset = s->line_offsets[lineno - 1];
3771 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3773 return got_error_from_errno("fseeko");
3775 linelen = getline(&line, &linesize, s->f);
3776 if (linelen != -1) {
3778 err = expand_tab(&exstr, line);
3781 if (match_line(exstr, &view->regex, 1,
3783 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3784 s->matched_line = lineno;
3790 if (view->searching == TOG_SEARCH_FORWARD)
3797 if (s->matched_line) {
3798 s->first_displayed_line = s->matched_line;
3799 s->selected_line = 1;
3805 static const struct got_error *
3806 close_diff_view(struct tog_view *view)
3808 const struct got_error *err = NULL;
3809 struct tog_diff_view_state *s = &view->state.diff;
3815 if (s->f && fclose(s->f) == EOF)
3816 err = got_error_from_errno("fclose");
3818 if (s->f1 && fclose(s->f1) == EOF)
3819 err = got_error_from_errno("fclose");
3821 if (s->f2 && fclose(s->f2) == EOF)
3822 err = got_error_from_errno("fclose");
3824 free_colors(&s->colors);
3825 free(s->line_offsets);
3826 s->line_offsets = NULL;
3831 static const struct got_error *
3832 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3833 struct got_object_id *id2, const char *label1, const char *label2,
3834 int diff_context, int ignore_whitespace, int force_text_diff,
3835 struct tog_view *log_view, struct got_repository *repo)
3837 const struct got_error *err;
3838 struct tog_diff_view_state *s = &view->state.diff;
3840 memset(s, 0, sizeof(*s));
3842 if (id1 != NULL && id2 != NULL) {
3844 err = got_object_get_type(&type1, repo, id1);
3847 err = got_object_get_type(&type2, repo, id2);
3852 return got_error(GOT_ERR_OBJ_TYPE);
3854 s->first_displayed_line = 1;
3855 s->last_displayed_line = view->nlines;
3856 s->selected_line = 1;
3864 s->id1 = got_object_id_dup(id1);
3866 return got_error_from_errno("got_object_id_dup");
3870 s->id2 = got_object_id_dup(id2);
3871 if (s->id2 == NULL) {
3872 err = got_error_from_errno("got_object_id_dup");
3876 s->f1 = got_opentemp();
3877 if (s->f1 == NULL) {
3878 err = got_error_from_errno("got_opentemp");
3882 s->f2 = got_opentemp();
3883 if (s->f2 == NULL) {
3884 err = got_error_from_errno("got_opentemp");
3888 s->first_displayed_line = 1;
3889 s->last_displayed_line = view->nlines;
3890 s->diff_context = diff_context;
3891 s->ignore_whitespace = ignore_whitespace;
3892 s->force_text_diff = force_text_diff;
3893 s->log_view = log_view;
3896 STAILQ_INIT(&s->colors);
3897 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3898 err = add_color(&s->colors,
3899 "^-", TOG_COLOR_DIFF_MINUS,
3900 get_color_value("TOG_COLOR_DIFF_MINUS"));
3903 err = add_color(&s->colors, "^\\+",
3904 TOG_COLOR_DIFF_PLUS,
3905 get_color_value("TOG_COLOR_DIFF_PLUS"));
3908 err = add_color(&s->colors,
3909 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3910 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3914 err = add_color(&s->colors,
3915 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3916 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3917 get_color_value("TOG_COLOR_DIFF_META"));
3921 err = add_color(&s->colors,
3922 "^(from|via): ", TOG_COLOR_AUTHOR,
3923 get_color_value("TOG_COLOR_AUTHOR"));
3927 err = add_color(&s->colors,
3928 "^date: ", TOG_COLOR_DATE,
3929 get_color_value("TOG_COLOR_DATE"));
3934 if (log_view && view_is_splitscreen(view))
3935 show_log_view(log_view); /* draw vborder */
3936 diff_view_indicate_progress(view);
3938 err = create_diff(s);
3940 view->show = show_diff_view;
3941 view->input = input_diff_view;
3942 view->close = close_diff_view;
3943 view->search_start = search_start_diff_view;
3944 view->search_next = search_next_diff_view;
3947 close_diff_view(view);
3951 static const struct got_error *
3952 show_diff_view(struct tog_view *view)
3954 const struct got_error *err;
3955 struct tog_diff_view_state *s = &view->state.diff;
3956 char *id_str1 = NULL, *id_str2, *header;
3957 const char *label1, *label2;
3960 err = got_object_id_str(&id_str1, s->id1);
3963 label1 = s->label1 ? : id_str1;
3965 label1 = "/dev/null";
3967 err = got_object_id_str(&id_str2, s->id2);
3970 label2 = s->label2 ? : id_str2;
3972 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3973 err = got_error_from_errno("asprintf");
3981 err = draw_file(view, header);
3986 static const struct got_error *
3987 set_selected_commit(struct tog_diff_view_state *s,
3988 struct commit_queue_entry *entry)
3990 const struct got_error *err;
3991 const struct got_object_id_queue *parent_ids;
3992 struct got_commit_object *selected_commit;
3993 struct got_object_qid *pid;
3996 s->id2 = got_object_id_dup(entry->id);
3998 return got_error_from_errno("got_object_id_dup");
4000 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4003 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4005 pid = STAILQ_FIRST(parent_ids);
4006 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4007 got_object_commit_close(selected_commit);
4011 static const struct got_error *
4012 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4014 const struct got_error *err = NULL;
4015 struct tog_diff_view_state *s = &view->state.diff;
4016 struct tog_log_view_state *ls;
4017 struct commit_queue_entry *old_selected_entry;
4019 size_t linesize = 0;
4021 int i, nscroll = view->nlines - 1;
4028 view->x = MAX(view->maxx - view->ncols / 3, 0);
4032 if (view->x + view->ncols / 3 < view->maxx)
4033 view->x += 2; /* move two columns right */
4037 view->x -= MIN(view->x, 2); /* move two columns back */
4042 s->force_text_diff = !s->force_text_diff;
4044 s->ignore_whitespace = !s->ignore_whitespace;
4045 wclear(view->window);
4046 s->first_displayed_line = 1;
4047 s->last_displayed_line = view->nlines;
4048 s->matched_line = 0;
4049 diff_view_indicate_progress(view);
4050 err = create_diff(s);
4054 s->first_displayed_line = 1;
4061 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4067 if (s->first_displayed_line > 1)
4068 s->first_displayed_line--;
4077 if (s->first_displayed_line == 1)
4080 while (i++ < nscroll && s->first_displayed_line > 1)
4081 s->first_displayed_line--;
4087 s->first_displayed_line++;
4100 while (!s->eof && i++ < nscroll) {
4101 linelen = getline(&line, &linesize, s->f);
4102 s->first_displayed_line++;
4103 if (linelen == -1) {
4107 err = got_ferror(s->f, GOT_ERR_IO);
4114 if (s->diff_context > 0) {
4116 s->matched_line = 0;
4117 diff_view_indicate_progress(view);
4118 err = create_diff(s);
4119 if (s->first_displayed_line + view->nlines - 1 >
4121 s->first_displayed_line = 1;
4122 s->last_displayed_line = view->nlines;
4127 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4129 s->matched_line = 0;
4130 diff_view_indicate_progress(view);
4131 err = create_diff(s);
4136 if (s->log_view == NULL)
4138 ls = &s->log_view->state.log;
4139 old_selected_entry = ls->selected_entry;
4141 err = input_log_view(NULL, s->log_view, KEY_UP);
4145 if (old_selected_entry == ls->selected_entry)
4148 err = set_selected_commit(s, ls->selected_entry);
4152 s->first_displayed_line = 1;
4153 s->last_displayed_line = view->nlines;
4154 s->matched_line = 0;
4157 diff_view_indicate_progress(view);
4158 err = create_diff(s);
4162 if (s->log_view == NULL)
4164 ls = &s->log_view->state.log;
4165 old_selected_entry = ls->selected_entry;
4167 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4171 if (old_selected_entry == ls->selected_entry)
4174 err = set_selected_commit(s, ls->selected_entry);
4178 s->first_displayed_line = 1;
4179 s->last_displayed_line = view->nlines;
4180 s->matched_line = 0;
4183 diff_view_indicate_progress(view);
4184 err = create_diff(s);
4193 static const struct got_error *
4194 cmd_diff(int argc, char *argv[])
4196 const struct got_error *error = NULL;
4197 struct got_repository *repo = NULL;
4198 struct got_worktree *worktree = NULL;
4199 struct got_object_id *id1 = NULL, *id2 = NULL;
4200 char *repo_path = NULL, *cwd = NULL;
4201 char *id_str1 = NULL, *id_str2 = NULL;
4202 char *label1 = NULL, *label2 = NULL;
4203 int diff_context = 3, ignore_whitespace = 0;
4204 int ch, force_text_diff = 0;
4206 struct tog_view *view;
4207 int *pack_fds = NULL;
4209 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4212 force_text_diff = 1;
4215 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4218 errx(1, "number of context lines is %s: %s",
4222 repo_path = realpath(optarg, NULL);
4223 if (repo_path == NULL)
4224 return got_error_from_errno2("realpath",
4226 got_path_strip_trailing_slashes(repo_path);
4229 ignore_whitespace = 1;
4241 usage_diff(); /* TODO show local worktree changes */
4242 } else if (argc == 2) {
4248 error = got_repo_pack_fds_open(&pack_fds);
4252 if (repo_path == NULL) {
4253 cwd = getcwd(NULL, 0);
4255 return got_error_from_errno("getcwd");
4256 error = got_worktree_open(&worktree, cwd);
4257 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4261 strdup(got_worktree_get_repo_path(worktree));
4263 repo_path = strdup(cwd);
4264 if (repo_path == NULL) {
4265 error = got_error_from_errno("strdup");
4270 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4276 error = apply_unveil(got_repo_get_path(repo), NULL);
4280 error = tog_load_refs(repo, 0);
4284 error = got_repo_match_object_id(&id1, &label1, id_str1,
4285 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4289 error = got_repo_match_object_id(&id2, &label2, id_str2,
4290 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4294 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4296 error = got_error_from_errno("view_open");
4299 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4300 ignore_whitespace, force_text_diff, NULL, repo);
4303 error = view_loop(view);
4310 const struct got_error *close_err = got_repo_close(repo);
4315 got_worktree_close(worktree);
4317 const struct got_error *pack_err =
4318 got_repo_pack_fds_close(pack_fds);
4331 "usage: %s blame [-c commit] [-r repository-path] path\n",
4336 struct tog_blame_line {
4338 struct got_object_id *id;
4341 static const struct got_error *
4342 draw_blame(struct tog_view *view)
4344 struct tog_blame_view_state *s = &view->state.blame;
4345 struct tog_blame *blame = &s->blame;
4346 regmatch_t *regmatch = &view->regmatch;
4347 const struct got_error *err;
4348 int lineno = 0, nprinted = 0;
4350 size_t linesize = 0;
4354 struct tog_blame_line *blame_line;
4355 struct got_object_id *prev_id = NULL;
4357 struct tog_color *tc;
4359 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4364 werase(view->window);
4366 if (asprintf(&line, "commit %s", id_str) == -1) {
4367 err = got_error_from_errno("asprintf");
4372 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4377 if (view_needs_focus_indication(view))
4378 wstandout(view->window);
4379 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4381 wattr_on(view->window,
4382 COLOR_PAIR(tc->colorpair), NULL);
4383 waddwstr(view->window, wline);
4385 wattr_off(view->window,
4386 COLOR_PAIR(tc->colorpair), NULL);
4387 if (view_needs_focus_indication(view))
4388 wstandend(view->window);
4391 if (width < view->ncols - 1)
4392 waddch(view->window, '\n');
4394 if (asprintf(&line, "[%d/%d] %s%s",
4395 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4396 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4398 return got_error_from_errno("asprintf");
4401 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4406 waddwstr(view->window, wline);
4409 if (width < view->ncols - 1)
4410 waddch(view->window, '\n');
4414 while (nprinted < view->nlines - 2) {
4415 linelen = getline(&line, &linesize, blame->f);
4416 if (linelen == -1) {
4417 if (feof(blame->f)) {
4422 return got_ferror(blame->f, GOT_ERR_IO);
4424 if (++lineno < s->first_displayed_line)
4427 /* Set view->maxx based on full line length. */
4428 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4435 view->maxx = MAX(view->maxx, width);
4437 if (view->focussed && nprinted == s->selected_line - 1)
4438 wstandout(view->window);
4440 if (blame->nlines > 0) {
4441 blame_line = &blame->lines[lineno - 1];
4442 if (blame_line->annotated && prev_id &&
4443 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4445 nprinted == s->selected_line - 1)) {
4446 waddstr(view->window, " ");
4447 } else if (blame_line->annotated) {
4449 err = got_object_id_str(&id_str,
4455 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4457 wattr_on(view->window,
4458 COLOR_PAIR(tc->colorpair), NULL);
4459 wprintw(view->window, "%.8s", id_str);
4461 wattr_off(view->window,
4462 COLOR_PAIR(tc->colorpair), NULL);
4464 prev_id = blame_line->id;
4466 waddstr(view->window, "........");
4470 waddstr(view->window, "........");
4474 if (view->focussed && nprinted == s->selected_line - 1)
4475 wstandend(view->window);
4476 waddstr(view->window, " ");
4478 if (view->ncols <= 9) {
4480 } else if (s->first_displayed_line + nprinted ==
4482 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4483 err = add_matched_line(&width, line, view->ncols - 9, 9,
4484 view->window, view->x, regmatch);
4492 err = format_line(&wline, &width, &skip, line,
4493 view->x, view->ncols - 9, 9, 1);
4498 waddwstr(view->window, &wline[skip]);
4504 if (width <= view->ncols - 1)
4505 waddch(view->window, '\n');
4506 if (++nprinted == 1)
4507 s->first_displayed_line = lineno;
4510 s->last_displayed_line = lineno;
4517 static const struct got_error *
4518 blame_cb(void *arg, int nlines, int lineno,
4519 struct got_commit_object *commit, struct got_object_id *id)
4521 const struct got_error *err = NULL;
4522 struct tog_blame_cb_args *a = arg;
4523 struct tog_blame_line *line;
4526 if (nlines != a->nlines ||
4527 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4528 return got_error(GOT_ERR_RANGE);
4530 errcode = pthread_mutex_lock(&tog_mutex);
4532 return got_error_set_errno(errcode, "pthread_mutex_lock");
4534 if (*a->quit) { /* user has quit the blame view */
4535 err = got_error(GOT_ERR_ITER_COMPLETED);
4540 goto done; /* no change in this commit */
4542 line = &a->lines[lineno - 1];
4543 if (line->annotated)
4546 line->id = got_object_id_dup(id);
4547 if (line->id == NULL) {
4548 err = got_error_from_errno("got_object_id_dup");
4551 line->annotated = 1;
4553 errcode = pthread_mutex_unlock(&tog_mutex);
4555 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4560 blame_thread(void *arg)
4562 const struct got_error *err, *close_err;
4563 struct tog_blame_thread_args *ta = arg;
4564 struct tog_blame_cb_args *a = ta->cb_args;
4567 err = block_signals_used_by_main_thread();
4571 err = got_blame(ta->path, a->commit_id, ta->repo,
4572 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4573 if (err && err->code == GOT_ERR_CANCELLED)
4576 errcode = pthread_mutex_lock(&tog_mutex);
4578 return (void *)got_error_set_errno(errcode,
4579 "pthread_mutex_lock");
4581 close_err = got_repo_close(ta->repo);
4587 errcode = pthread_mutex_unlock(&tog_mutex);
4588 if (errcode && err == NULL)
4589 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4594 static struct got_object_id *
4595 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4596 int first_displayed_line, int selected_line)
4598 struct tog_blame_line *line;
4603 line = &lines[first_displayed_line - 1 + selected_line - 1];
4604 if (!line->annotated)
4610 static const struct got_error *
4611 stop_blame(struct tog_blame *blame)
4613 const struct got_error *err = NULL;
4616 if (blame->thread) {
4618 errcode = pthread_mutex_unlock(&tog_mutex);
4620 return got_error_set_errno(errcode,
4621 "pthread_mutex_unlock");
4622 errcode = pthread_join(blame->thread, (void **)&err);
4624 return got_error_set_errno(errcode, "pthread_join");
4625 errcode = pthread_mutex_lock(&tog_mutex);
4627 return got_error_set_errno(errcode,
4628 "pthread_mutex_lock");
4629 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4631 blame->thread = 0; //NULL;
4633 if (blame->thread_args.repo) {
4634 const struct got_error *close_err;
4635 close_err = got_repo_close(blame->thread_args.repo);
4638 blame->thread_args.repo = NULL;
4641 if (fclose(blame->f) == EOF && err == NULL)
4642 err = got_error_from_errno("fclose");
4646 for (i = 0; i < blame->nlines; i++)
4647 free(blame->lines[i].id);
4649 blame->lines = NULL;
4651 free(blame->cb_args.commit_id);
4652 blame->cb_args.commit_id = NULL;
4653 if (blame->pack_fds) {
4654 const struct got_error *pack_err =
4655 got_repo_pack_fds_close(blame->pack_fds);
4658 blame->pack_fds = NULL;
4663 static const struct got_error *
4664 cancel_blame_view(void *arg)
4666 const struct got_error *err = NULL;
4670 errcode = pthread_mutex_lock(&tog_mutex);
4672 return got_error_set_errno(errcode,
4673 "pthread_mutex_unlock");
4676 err = got_error(GOT_ERR_CANCELLED);
4678 errcode = pthread_mutex_unlock(&tog_mutex);
4680 return got_error_set_errno(errcode,
4681 "pthread_mutex_lock");
4686 static const struct got_error *
4687 run_blame(struct tog_view *view)
4689 struct tog_blame_view_state *s = &view->state.blame;
4690 struct tog_blame *blame = &s->blame;
4691 const struct got_error *err = NULL;
4692 struct got_commit_object *commit = NULL;
4693 struct got_blob_object *blob = NULL;
4694 struct got_repository *thread_repo = NULL;
4695 struct got_object_id *obj_id = NULL;
4697 int *pack_fds = NULL;
4699 err = got_object_open_as_commit(&commit, s->repo,
4700 &s->blamed_commit->id);
4704 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4708 err = got_object_get_type(&obj_type, s->repo, obj_id);
4712 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4713 err = got_error(GOT_ERR_OBJ_TYPE);
4717 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4720 blame->f = got_opentemp();
4721 if (blame->f == NULL) {
4722 err = got_error_from_errno("got_opentemp");
4725 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4726 &blame->line_offsets, blame->f, blob);
4729 if (blame->nlines == 0) {
4730 s->blame_complete = 1;
4734 /* Don't include \n at EOF in the blame line count. */
4735 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4738 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4739 if (blame->lines == NULL) {
4740 err = got_error_from_errno("calloc");
4744 err = got_repo_pack_fds_open(&pack_fds);
4747 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4752 blame->pack_fds = pack_fds;
4753 blame->cb_args.view = view;
4754 blame->cb_args.lines = blame->lines;
4755 blame->cb_args.nlines = blame->nlines;
4756 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4757 if (blame->cb_args.commit_id == NULL) {
4758 err = got_error_from_errno("got_object_id_dup");
4761 blame->cb_args.quit = &s->done;
4763 blame->thread_args.path = s->path;
4764 blame->thread_args.repo = thread_repo;
4765 blame->thread_args.cb_args = &blame->cb_args;
4766 blame->thread_args.complete = &s->blame_complete;
4767 blame->thread_args.cancel_cb = cancel_blame_view;
4768 blame->thread_args.cancel_arg = &s->done;
4769 s->blame_complete = 0;
4771 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4772 s->first_displayed_line = 1;
4773 s->last_displayed_line = view->nlines;
4774 s->selected_line = 1;
4776 s->matched_line = 0;
4780 got_object_commit_close(commit);
4782 got_object_blob_close(blob);
4789 static const struct got_error *
4790 open_blame_view(struct tog_view *view, char *path,
4791 struct got_object_id *commit_id, struct got_repository *repo)
4793 const struct got_error *err = NULL;
4794 struct tog_blame_view_state *s = &view->state.blame;
4796 STAILQ_INIT(&s->blamed_commits);
4798 s->path = strdup(path);
4799 if (s->path == NULL)
4800 return got_error_from_errno("strdup");
4802 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4808 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4809 s->first_displayed_line = 1;
4810 s->last_displayed_line = view->nlines;
4811 s->selected_line = 1;
4812 s->blame_complete = 0;
4814 s->commit_id = commit_id;
4815 memset(&s->blame, 0, sizeof(s->blame));
4817 STAILQ_INIT(&s->colors);
4818 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4819 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4820 get_color_value("TOG_COLOR_COMMIT"));
4825 view->show = show_blame_view;
4826 view->input = input_blame_view;
4827 view->close = close_blame_view;
4828 view->search_start = search_start_blame_view;
4829 view->search_next = search_next_blame_view;
4831 return run_blame(view);
4834 static const struct got_error *
4835 close_blame_view(struct tog_view *view)
4837 const struct got_error *err = NULL;
4838 struct tog_blame_view_state *s = &view->state.blame;
4840 if (s->blame.thread)
4841 err = stop_blame(&s->blame);
4843 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4844 struct got_object_qid *blamed_commit;
4845 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4846 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4847 got_object_qid_free(blamed_commit);
4851 free_colors(&s->colors);
4855 static const struct got_error *
4856 search_start_blame_view(struct tog_view *view)
4858 struct tog_blame_view_state *s = &view->state.blame;
4860 s->matched_line = 0;
4864 static const struct got_error *
4865 search_next_blame_view(struct tog_view *view)
4867 struct tog_blame_view_state *s = &view->state.blame;
4868 const struct got_error *err = NULL;
4871 size_t linesize = 0;
4874 if (!view->searching) {
4875 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4879 if (s->matched_line) {
4880 if (view->searching == TOG_SEARCH_FORWARD)
4881 lineno = s->matched_line + 1;
4883 lineno = s->matched_line - 1;
4885 lineno = s->first_displayed_line - 1 + s->selected_line;
4890 if (lineno <= 0 || lineno > s->blame.nlines) {
4891 if (s->matched_line == 0) {
4892 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4896 if (view->searching == TOG_SEARCH_FORWARD)
4899 lineno = s->blame.nlines;
4902 offset = s->blame.line_offsets[lineno - 1];
4903 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4905 return got_error_from_errno("fseeko");
4907 linelen = getline(&line, &linesize, s->blame.f);
4908 if (linelen != -1) {
4910 err = expand_tab(&exstr, line);
4913 if (match_line(exstr, &view->regex, 1,
4915 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4916 s->matched_line = lineno;
4922 if (view->searching == TOG_SEARCH_FORWARD)
4929 if (s->matched_line) {
4930 s->first_displayed_line = s->matched_line;
4931 s->selected_line = 1;
4937 static const struct got_error *
4938 show_blame_view(struct tog_view *view)
4940 const struct got_error *err = NULL;
4941 struct tog_blame_view_state *s = &view->state.blame;
4944 if (s->blame.thread == 0 && !s->blame_complete) {
4945 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4946 &s->blame.thread_args);
4948 return got_error_set_errno(errcode, "pthread_create");
4950 halfdelay(1); /* fast refresh while annotating */
4953 if (s->blame_complete)
4954 halfdelay(10); /* disable fast refresh */
4956 err = draw_blame(view);
4962 static const struct got_error *
4963 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4965 const struct got_error *err = NULL, *thread_err = NULL;
4966 struct tog_view *diff_view;
4967 struct tog_blame_view_state *s = &view->state.blame;
4968 int begin_x = 0, nscroll = view->nlines - 2;
4975 view->x = MAX(view->maxx - view->ncols / 3, 0);
4979 if (view->x + view->ncols / 3 < view->maxx)
4980 view->x += 2; /* move two columns right */
4984 view->x -= MIN(view->x, 2); /* move two columns back */
4991 s->selected_line = 1;
4992 s->first_displayed_line = 1;
4996 if (s->blame.nlines < view->nlines - 2) {
4997 s->selected_line = s->blame.nlines;
4998 s->first_displayed_line = 1;
5000 s->selected_line = view->nlines - 2;
5001 s->first_displayed_line = s->blame.nlines -
5008 if (s->selected_line > 1)
5010 else if (s->selected_line == 1 &&
5011 s->first_displayed_line > 1)
5012 s->first_displayed_line--;
5021 if (s->first_displayed_line == 1) {
5022 s->selected_line = MAX(1, s->selected_line - nscroll);
5025 if (s->first_displayed_line > nscroll)
5026 s->first_displayed_line -= nscroll;
5028 s->first_displayed_line = 1;
5033 if (s->selected_line < view->nlines - 2 &&
5034 s->first_displayed_line +
5035 s->selected_line <= s->blame.nlines)
5037 else if (s->last_displayed_line <
5039 s->first_displayed_line++;
5043 struct got_object_id *id = NULL;
5044 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5045 s->first_displayed_line, s->selected_line);
5049 struct got_commit_object *commit, *pcommit;
5050 struct got_object_qid *pid;
5051 struct got_object_id *blob_id = NULL;
5053 err = got_object_open_as_commit(&commit,
5058 got_object_commit_get_parent_ids(commit));
5060 got_object_commit_close(commit);
5063 /* Check if path history ends here. */
5064 err = got_object_open_as_commit(&pcommit,
5068 err = got_object_id_by_path(&blob_id, s->repo,
5070 got_object_commit_close(pcommit);
5072 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5074 got_object_commit_close(commit);
5077 err = got_object_get_type(&obj_type, s->repo,
5080 /* Can't blame non-blob type objects. */
5081 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5082 got_object_commit_close(commit);
5085 err = got_object_qid_alloc(&s->blamed_commit,
5087 got_object_commit_close(commit);
5089 if (got_object_id_cmp(id,
5090 &s->blamed_commit->id) == 0)
5092 err = got_object_qid_alloc(&s->blamed_commit,
5098 thread_err = stop_blame(&s->blame);
5102 STAILQ_INSERT_HEAD(&s->blamed_commits,
5103 s->blamed_commit, entry);
5104 err = run_blame(view);
5110 struct got_object_qid *first;
5111 first = STAILQ_FIRST(&s->blamed_commits);
5112 if (!got_object_id_cmp(&first->id, s->commit_id))
5115 thread_err = stop_blame(&s->blame);
5119 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5120 got_object_qid_free(s->blamed_commit);
5122 STAILQ_FIRST(&s->blamed_commits);
5123 err = run_blame(view);
5130 struct got_object_id *id = NULL;
5131 struct got_object_qid *pid;
5132 struct got_commit_object *commit = NULL;
5133 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5134 s->first_displayed_line, s->selected_line);
5137 err = got_object_open_as_commit(&commit, s->repo, id);
5141 got_object_commit_get_parent_ids(commit));
5142 if (view_is_parent_view(view))
5143 begin_x = view_split_begin_x(view->begin_x);
5144 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5145 if (diff_view == NULL) {
5146 got_object_commit_close(commit);
5147 err = got_error_from_errno("view_open");
5150 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5151 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5152 got_object_commit_close(commit);
5154 view_close(diff_view);
5158 diff_view->focussed = 1;
5159 if (view_is_parent_view(view)) {
5160 err = view_close_child(view);
5163 err = view_set_child(view, diff_view);
5166 view->focus_child = 1;
5168 *new_view = diff_view;
5181 if (s->last_displayed_line >= s->blame.nlines &&
5182 s->selected_line >= MIN(s->blame.nlines,
5183 view->nlines - 2)) {
5186 if (s->last_displayed_line >= s->blame.nlines &&
5187 s->selected_line < view->nlines - 2) {
5189 MIN(nscroll, s->last_displayed_line -
5190 s->first_displayed_line - s->selected_line + 1);
5192 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5193 s->first_displayed_line += nscroll;
5195 s->first_displayed_line =
5196 s->blame.nlines - (view->nlines - 3);
5199 if (s->selected_line > view->nlines - 2) {
5200 s->selected_line = MIN(s->blame.nlines,
5207 return thread_err ? thread_err : err;
5210 static const struct got_error *
5211 cmd_blame(int argc, char *argv[])
5213 const struct got_error *error;
5214 struct got_repository *repo = NULL;
5215 struct got_worktree *worktree = NULL;
5216 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5217 char *link_target = NULL;
5218 struct got_object_id *commit_id = NULL;
5219 struct got_commit_object *commit = NULL;
5220 char *commit_id_str = NULL;
5222 struct tog_view *view;
5223 int *pack_fds = NULL;
5225 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5228 commit_id_str = optarg;
5231 repo_path = realpath(optarg, NULL);
5232 if (repo_path == NULL)
5233 return got_error_from_errno2("realpath",
5248 error = got_repo_pack_fds_open(&pack_fds);
5252 if (repo_path == NULL) {
5253 cwd = getcwd(NULL, 0);
5255 return got_error_from_errno("getcwd");
5256 error = got_worktree_open(&worktree, cwd);
5257 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5261 strdup(got_worktree_get_repo_path(worktree));
5263 repo_path = strdup(cwd);
5264 if (repo_path == NULL) {
5265 error = got_error_from_errno("strdup");
5270 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5274 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5281 error = apply_unveil(got_repo_get_path(repo), NULL);
5285 error = tog_load_refs(repo, 0);
5289 if (commit_id_str == NULL) {
5290 struct got_reference *head_ref;
5291 error = got_ref_open(&head_ref, repo, worktree ?
5292 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5295 error = got_ref_resolve(&commit_id, repo, head_ref);
5296 got_ref_close(head_ref);
5298 error = got_repo_match_object_id(&commit_id, NULL,
5299 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5304 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5306 error = got_error_from_errno("view_open");
5310 error = got_object_open_as_commit(&commit, repo, commit_id);
5314 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5319 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5324 /* Release work tree lock. */
5325 got_worktree_close(worktree);
5328 error = view_loop(view);
5336 got_object_commit_close(commit);
5338 got_worktree_close(worktree);
5340 const struct got_error *close_err = got_repo_close(repo);
5345 const struct got_error *pack_err =
5346 got_repo_pack_fds_close(pack_fds);
5354 static const struct got_error *
5355 draw_tree_entries(struct tog_view *view, const char *parent_path)
5357 struct tog_tree_view_state *s = &view->state.tree;
5358 const struct got_error *err = NULL;
5359 struct got_tree_entry *te;
5361 struct tog_color *tc;
5362 int width, n, i, nentries;
5363 int limit = view->nlines;
5367 werase(view->window);
5372 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5376 if (view_needs_focus_indication(view))
5377 wstandout(view->window);
5378 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5380 wattr_on(view->window,
5381 COLOR_PAIR(tc->colorpair), NULL);
5382 waddwstr(view->window, wline);
5384 wattr_off(view->window,
5385 COLOR_PAIR(tc->colorpair), NULL);
5386 if (view_needs_focus_indication(view))
5387 wstandend(view->window);
5390 if (width < view->ncols - 1)
5391 waddch(view->window, '\n');
5394 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5398 waddwstr(view->window, wline);
5401 if (width < view->ncols - 1)
5402 waddch(view->window, '\n');
5405 waddch(view->window, '\n');
5409 if (s->first_displayed_entry == NULL) {
5410 te = got_object_tree_get_first_entry(s->tree);
5411 if (s->selected == 0) {
5413 wstandout(view->window);
5414 s->selected_entry = NULL;
5416 waddstr(view->window, " ..\n"); /* parent directory */
5417 if (s->selected == 0 && view->focussed)
5418 wstandend(view->window);
5425 te = s->first_displayed_entry;
5428 nentries = got_object_tree_get_nentries(s->tree);
5429 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5430 char *line = NULL, *id_str = NULL, *link_target = NULL;
5431 const char *modestr = "";
5434 te = got_object_tree_get_entry(s->tree, i);
5435 mode = got_tree_entry_get_mode(te);
5438 err = got_object_id_str(&id_str,
5439 got_tree_entry_get_id(te));
5441 return got_error_from_errno(
5442 "got_object_id_str");
5444 if (got_object_tree_entry_is_submodule(te))
5446 else if (S_ISLNK(mode)) {
5449 err = got_tree_entry_get_symlink_target(&link_target,
5455 for (i = 0; i < strlen(link_target); i++) {
5456 if (!isprint((unsigned char)link_target[i]))
5457 link_target[i] = '?';
5461 else if (S_ISDIR(mode))
5463 else if (mode & S_IXUSR)
5465 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5466 got_tree_entry_get_name(te), modestr,
5467 link_target ? " -> ": "",
5468 link_target ? link_target : "") == -1) {
5471 return got_error_from_errno("asprintf");
5475 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5481 if (n == s->selected) {
5483 wstandout(view->window);
5484 s->selected_entry = te;
5486 tc = match_color(&s->colors, line);
5488 wattr_on(view->window,
5489 COLOR_PAIR(tc->colorpair), NULL);
5490 waddwstr(view->window, wline);
5492 wattr_off(view->window,
5493 COLOR_PAIR(tc->colorpair), NULL);
5494 if (width < view->ncols - 1)
5495 waddch(view->window, '\n');
5496 if (n == s->selected && view->focussed)
5497 wstandend(view->window);
5503 s->last_displayed_entry = te;
5512 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5514 struct got_tree_entry *te;
5515 int isroot = s->tree == s->root;
5518 if (s->first_displayed_entry == NULL)
5521 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5522 while (i++ < maxscroll) {
5525 s->first_displayed_entry = NULL;
5528 s->first_displayed_entry = te;
5529 te = got_tree_entry_get_prev(s->tree, te);
5534 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5536 struct got_tree_entry *next, *last;
5539 if (s->first_displayed_entry)
5540 next = got_tree_entry_get_next(s->tree,
5541 s->first_displayed_entry);
5543 next = got_object_tree_get_first_entry(s->tree);
5545 last = s->last_displayed_entry;
5546 while (next && last && n++ < maxscroll) {
5547 last = got_tree_entry_get_next(s->tree, last);
5549 s->first_displayed_entry = next;
5550 next = got_tree_entry_get_next(s->tree, next);
5555 static const struct got_error *
5556 tree_entry_path(char **path, struct tog_parent_trees *parents,
5557 struct got_tree_entry *te)
5559 const struct got_error *err = NULL;
5560 struct tog_parent_tree *pt;
5561 size_t len = 2; /* for leading slash and NUL */
5563 TAILQ_FOREACH(pt, parents, entry)
5564 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5567 len += strlen(got_tree_entry_get_name(te));
5569 *path = calloc(1, len);
5571 return got_error_from_errno("calloc");
5574 pt = TAILQ_LAST(parents, tog_parent_trees);
5576 const char *name = got_tree_entry_get_name(pt->selected_entry);
5577 if (strlcat(*path, name, len) >= len) {
5578 err = got_error(GOT_ERR_NO_SPACE);
5581 if (strlcat(*path, "/", len) >= len) {
5582 err = got_error(GOT_ERR_NO_SPACE);
5585 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5588 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5589 err = got_error(GOT_ERR_NO_SPACE);
5601 static const struct got_error *
5602 blame_tree_entry(struct tog_view **new_view, int begin_x,
5603 struct got_tree_entry *te, struct tog_parent_trees *parents,
5604 struct got_object_id *commit_id, struct got_repository *repo)
5606 const struct got_error *err = NULL;
5608 struct tog_view *blame_view;
5612 err = tree_entry_path(&path, parents, te);
5616 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5617 if (blame_view == NULL) {
5618 err = got_error_from_errno("view_open");
5622 err = open_blame_view(blame_view, path, commit_id, repo);
5624 if (err->code == GOT_ERR_CANCELLED)
5626 view_close(blame_view);
5628 *new_view = blame_view;
5634 static const struct got_error *
5635 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5636 struct tog_tree_view_state *s)
5638 struct tog_view *log_view;
5639 const struct got_error *err = NULL;
5644 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5645 if (log_view == NULL)
5646 return got_error_from_errno("view_open");
5648 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5652 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5655 view_close(log_view);
5657 *new_view = log_view;
5662 static const struct got_error *
5663 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5664 const char *head_ref_name, struct got_repository *repo)
5666 const struct got_error *err = NULL;
5667 char *commit_id_str = NULL;
5668 struct tog_tree_view_state *s = &view->state.tree;
5669 struct got_commit_object *commit = NULL;
5671 TAILQ_INIT(&s->parents);
5672 STAILQ_INIT(&s->colors);
5674 s->commit_id = got_object_id_dup(commit_id);
5675 if (s->commit_id == NULL)
5676 return got_error_from_errno("got_object_id_dup");
5678 err = got_object_open_as_commit(&commit, repo, commit_id);
5683 * The root is opened here and will be closed when the view is closed.
5684 * Any visited subtrees and their path-wise parents are opened and
5687 err = got_object_open_as_tree(&s->root, repo,
5688 got_object_commit_get_tree_id(commit));
5693 err = got_object_id_str(&commit_id_str, commit_id);
5697 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5698 err = got_error_from_errno("asprintf");
5702 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5703 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5704 if (head_ref_name) {
5705 s->head_ref_name = strdup(head_ref_name);
5706 if (s->head_ref_name == NULL) {
5707 err = got_error_from_errno("strdup");
5713 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5714 err = add_color(&s->colors, "\\$$",
5715 TOG_COLOR_TREE_SUBMODULE,
5716 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5719 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5720 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5723 err = add_color(&s->colors, "/$",
5724 TOG_COLOR_TREE_DIRECTORY,
5725 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5729 err = add_color(&s->colors, "\\*$",
5730 TOG_COLOR_TREE_EXECUTABLE,
5731 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5735 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5736 get_color_value("TOG_COLOR_COMMIT"));
5741 view->show = show_tree_view;
5742 view->input = input_tree_view;
5743 view->close = close_tree_view;
5744 view->search_start = search_start_tree_view;
5745 view->search_next = search_next_tree_view;
5747 free(commit_id_str);
5749 got_object_commit_close(commit);
5751 close_tree_view(view);
5755 static const struct got_error *
5756 close_tree_view(struct tog_view *view)
5758 struct tog_tree_view_state *s = &view->state.tree;
5760 free_colors(&s->colors);
5761 free(s->tree_label);
5762 s->tree_label = NULL;
5764 s->commit_id = NULL;
5765 free(s->head_ref_name);
5766 s->head_ref_name = NULL;
5767 while (!TAILQ_EMPTY(&s->parents)) {
5768 struct tog_parent_tree *parent;
5769 parent = TAILQ_FIRST(&s->parents);
5770 TAILQ_REMOVE(&s->parents, parent, entry);
5771 if (parent->tree != s->root)
5772 got_object_tree_close(parent->tree);
5776 if (s->tree != NULL && s->tree != s->root)
5777 got_object_tree_close(s->tree);
5779 got_object_tree_close(s->root);
5783 static const struct got_error *
5784 search_start_tree_view(struct tog_view *view)
5786 struct tog_tree_view_state *s = &view->state.tree;
5788 s->matched_entry = NULL;
5793 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5795 regmatch_t regmatch;
5797 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5801 static const struct got_error *
5802 search_next_tree_view(struct tog_view *view)
5804 struct tog_tree_view_state *s = &view->state.tree;
5805 struct got_tree_entry *te = NULL;
5807 if (!view->searching) {
5808 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5812 if (s->matched_entry) {
5813 if (view->searching == TOG_SEARCH_FORWARD) {
5814 if (s->selected_entry)
5815 te = got_tree_entry_get_next(s->tree,
5818 te = got_object_tree_get_first_entry(s->tree);
5820 if (s->selected_entry == NULL)
5821 te = got_object_tree_get_last_entry(s->tree);
5823 te = got_tree_entry_get_prev(s->tree,
5827 if (s->selected_entry)
5828 te = s->selected_entry;
5829 else if (view->searching == TOG_SEARCH_FORWARD)
5830 te = got_object_tree_get_first_entry(s->tree);
5832 te = got_object_tree_get_last_entry(s->tree);
5837 if (s->matched_entry == NULL) {
5838 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5841 if (view->searching == TOG_SEARCH_FORWARD)
5842 te = got_object_tree_get_first_entry(s->tree);
5844 te = got_object_tree_get_last_entry(s->tree);
5847 if (match_tree_entry(te, &view->regex)) {
5848 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5849 s->matched_entry = te;
5853 if (view->searching == TOG_SEARCH_FORWARD)
5854 te = got_tree_entry_get_next(s->tree, te);
5856 te = got_tree_entry_get_prev(s->tree, te);
5859 if (s->matched_entry) {
5860 s->first_displayed_entry = s->matched_entry;
5867 static const struct got_error *
5868 show_tree_view(struct tog_view *view)
5870 const struct got_error *err = NULL;
5871 struct tog_tree_view_state *s = &view->state.tree;
5874 err = tree_entry_path(&parent_path, &s->parents, NULL);
5878 err = draw_tree_entries(view, parent_path);
5885 static const struct got_error *
5886 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5888 const struct got_error *err = NULL;
5889 struct tog_tree_view_state *s = &view->state.tree;
5890 struct tog_view *log_view, *ref_view;
5891 struct got_tree_entry *te;
5892 int begin_x = 0, n, nscroll = view->nlines - 3;
5896 s->show_ids = !s->show_ids;
5899 if (!s->selected_entry)
5901 if (view_is_parent_view(view))
5902 begin_x = view_split_begin_x(view->begin_x);
5903 err = log_selected_tree_entry(&log_view, begin_x, s);
5905 log_view->focussed = 1;
5906 if (view_is_parent_view(view)) {
5907 err = view_close_child(view);
5910 err = view_set_child(view, log_view);
5913 view->focus_child = 1;
5915 *new_view = log_view;
5918 if (view_is_parent_view(view))
5919 begin_x = view_split_begin_x(view->begin_x);
5920 ref_view = view_open(view->nlines, view->ncols,
5921 view->begin_y, begin_x, TOG_VIEW_REF);
5922 if (ref_view == NULL)
5923 return got_error_from_errno("view_open");
5924 err = open_ref_view(ref_view, s->repo);
5926 view_close(ref_view);
5930 ref_view->focussed = 1;
5931 if (view_is_parent_view(view)) {
5932 err = view_close_child(view);
5935 err = view_set_child(view, ref_view);
5938 view->focus_child = 1;
5940 *new_view = ref_view;
5945 if (s->tree == s->root)
5946 s->first_displayed_entry =
5947 got_object_tree_get_first_entry(s->tree);
5949 s->first_displayed_entry = NULL;
5954 te = got_object_tree_get_last_entry(s->tree);
5955 for (n = 0; n < view->nlines - 3; n++) {
5957 if(s->tree != s->root) {
5958 s->first_displayed_entry = NULL;
5963 s->first_displayed_entry = te;
5964 te = got_tree_entry_get_prev(s->tree, te);
5967 s->selected = n - 1;
5972 if (s->selected > 0) {
5976 tree_scroll_up(s, 1);
5985 if (s->tree == s->root) {
5986 if (got_object_tree_get_first_entry(s->tree) ==
5987 s->first_displayed_entry)
5988 s->selected -= MIN(s->selected, nscroll);
5990 if (s->first_displayed_entry == NULL)
5991 s->selected -= MIN(s->selected, nscroll);
5993 tree_scroll_up(s, MAX(0, nscroll));
5998 if (s->selected < s->ndisplayed - 1) {
6002 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6004 /* can't scroll any further */
6006 tree_scroll_down(s, 1);
6016 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6018 /* can't scroll any further; move cursor down */
6019 if (s->selected < s->ndisplayed - 1)
6020 s->selected += MIN(nscroll,
6021 s->ndisplayed - s->selected - 1);
6024 tree_scroll_down(s, nscroll);
6029 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6030 struct tog_parent_tree *parent;
6031 /* user selected '..' */
6032 if (s->tree == s->root)
6034 parent = TAILQ_FIRST(&s->parents);
6035 TAILQ_REMOVE(&s->parents, parent,
6037 got_object_tree_close(s->tree);
6038 s->tree = parent->tree;
6039 s->first_displayed_entry =
6040 parent->first_displayed_entry;
6042 parent->selected_entry;
6043 s->selected = parent->selected;
6045 } else if (S_ISDIR(got_tree_entry_get_mode(
6046 s->selected_entry))) {
6047 struct got_tree_object *subtree;
6048 err = got_object_open_as_tree(&subtree, s->repo,
6049 got_tree_entry_get_id(s->selected_entry));
6052 err = tree_view_visit_subtree(s, subtree);
6054 got_object_tree_close(subtree);
6057 } else if (S_ISREG(got_tree_entry_get_mode(
6058 s->selected_entry))) {
6059 struct tog_view *blame_view;
6060 int begin_x = view_is_parent_view(view) ?
6061 view_split_begin_x(view->begin_x) : 0;
6063 err = blame_tree_entry(&blame_view, begin_x,
6064 s->selected_entry, &s->parents,
6065 s->commit_id, s->repo);
6069 blame_view->focussed = 1;
6070 if (view_is_parent_view(view)) {
6071 err = view_close_child(view);
6074 err = view_set_child(view, blame_view);
6077 view->focus_child = 1;
6079 *new_view = blame_view;
6083 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6084 s->selected = view->nlines - 4;
6098 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6103 static const struct got_error *
6104 cmd_tree(int argc, char *argv[])
6106 const struct got_error *error;
6107 struct got_repository *repo = NULL;
6108 struct got_worktree *worktree = NULL;
6109 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6110 struct got_object_id *commit_id = NULL;
6111 struct got_commit_object *commit = NULL;
6112 const char *commit_id_arg = NULL;
6114 struct got_reference *ref = NULL;
6115 const char *head_ref_name = NULL;
6117 struct tog_view *view;
6118 int *pack_fds = NULL;
6120 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6123 commit_id_arg = optarg;
6126 repo_path = realpath(optarg, NULL);
6127 if (repo_path == NULL)
6128 return got_error_from_errno2("realpath",
6143 error = got_repo_pack_fds_open(&pack_fds);
6147 if (repo_path == NULL) {
6148 cwd = getcwd(NULL, 0);
6150 return got_error_from_errno("getcwd");
6151 error = got_worktree_open(&worktree, cwd);
6152 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6156 strdup(got_worktree_get_repo_path(worktree));
6158 repo_path = strdup(cwd);
6159 if (repo_path == NULL) {
6160 error = got_error_from_errno("strdup");
6165 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6169 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6176 error = apply_unveil(got_repo_get_path(repo), NULL);
6180 error = tog_load_refs(repo, 0);
6184 if (commit_id_arg == NULL) {
6185 error = got_repo_match_object_id(&commit_id, &label,
6186 worktree ? got_worktree_get_head_ref_name(worktree) :
6187 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6190 head_ref_name = label;
6192 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6194 head_ref_name = got_ref_get_name(ref);
6195 else if (error->code != GOT_ERR_NOT_REF)
6197 error = got_repo_match_object_id(&commit_id, NULL,
6198 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6203 error = got_object_open_as_commit(&commit, repo, commit_id);
6207 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6209 error = got_error_from_errno("view_open");
6212 error = open_tree_view(view, commit_id, head_ref_name, repo);
6215 if (!got_path_is_root_dir(in_repo_path)) {
6216 error = tree_view_walk_path(&view->state.tree, commit,
6223 /* Release work tree lock. */
6224 got_worktree_close(worktree);
6227 error = view_loop(view);
6236 const struct got_error *close_err = got_repo_close(repo);
6241 const struct got_error *pack_err =
6242 got_repo_pack_fds_close(pack_fds);
6250 static const struct got_error *
6251 ref_view_load_refs(struct tog_ref_view_state *s)
6253 struct got_reflist_entry *sre;
6254 struct tog_reflist_entry *re;
6257 TAILQ_FOREACH(sre, &tog_refs, entry) {
6258 if (strncmp(got_ref_get_name(sre->ref),
6259 "refs/got/", 9) == 0 &&
6260 strncmp(got_ref_get_name(sre->ref),
6261 "refs/got/backup/", 16) != 0)
6264 re = malloc(sizeof(*re));
6266 return got_error_from_errno("malloc");
6268 re->ref = got_ref_dup(sre->ref);
6269 if (re->ref == NULL)
6270 return got_error_from_errno("got_ref_dup");
6271 re->idx = s->nrefs++;
6272 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6275 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6280 ref_view_free_refs(struct tog_ref_view_state *s)
6282 struct tog_reflist_entry *re;
6284 while (!TAILQ_EMPTY(&s->refs)) {
6285 re = TAILQ_FIRST(&s->refs);
6286 TAILQ_REMOVE(&s->refs, re, entry);
6287 got_ref_close(re->ref);
6292 static const struct got_error *
6293 open_ref_view(struct tog_view *view, struct got_repository *repo)
6295 const struct got_error *err = NULL;
6296 struct tog_ref_view_state *s = &view->state.ref;
6298 s->selected_entry = 0;
6301 TAILQ_INIT(&s->refs);
6302 STAILQ_INIT(&s->colors);
6304 err = ref_view_load_refs(s);
6308 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6309 err = add_color(&s->colors, "^refs/heads/",
6310 TOG_COLOR_REFS_HEADS,
6311 get_color_value("TOG_COLOR_REFS_HEADS"));
6315 err = add_color(&s->colors, "^refs/tags/",
6316 TOG_COLOR_REFS_TAGS,
6317 get_color_value("TOG_COLOR_REFS_TAGS"));
6321 err = add_color(&s->colors, "^refs/remotes/",
6322 TOG_COLOR_REFS_REMOTES,
6323 get_color_value("TOG_COLOR_REFS_REMOTES"));
6327 err = add_color(&s->colors, "^refs/got/backup/",
6328 TOG_COLOR_REFS_BACKUP,
6329 get_color_value("TOG_COLOR_REFS_BACKUP"));
6334 view->show = show_ref_view;
6335 view->input = input_ref_view;
6336 view->close = close_ref_view;
6337 view->search_start = search_start_ref_view;
6338 view->search_next = search_next_ref_view;
6341 free_colors(&s->colors);
6345 static const struct got_error *
6346 close_ref_view(struct tog_view *view)
6348 struct tog_ref_view_state *s = &view->state.ref;
6350 ref_view_free_refs(s);
6351 free_colors(&s->colors);
6356 static const struct got_error *
6357 resolve_reflist_entry(struct got_object_id **commit_id,
6358 struct tog_reflist_entry *re, struct got_repository *repo)
6360 const struct got_error *err = NULL;
6361 struct got_object_id *obj_id;
6362 struct got_tag_object *tag = NULL;
6367 err = got_ref_resolve(&obj_id, repo, re->ref);
6371 err = got_object_get_type(&obj_type, repo, obj_id);
6376 case GOT_OBJ_TYPE_COMMIT:
6377 *commit_id = obj_id;
6379 case GOT_OBJ_TYPE_TAG:
6380 err = got_object_open_as_tag(&tag, repo, obj_id);
6384 err = got_object_get_type(&obj_type, repo,
6385 got_object_tag_get_object_id(tag));
6388 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6389 err = got_error(GOT_ERR_OBJ_TYPE);
6392 *commit_id = got_object_id_dup(
6393 got_object_tag_get_object_id(tag));
6394 if (*commit_id == NULL) {
6395 err = got_error_from_errno("got_object_id_dup");
6400 err = got_error(GOT_ERR_OBJ_TYPE);
6406 got_object_tag_close(tag);
6414 static const struct got_error *
6415 log_ref_entry(struct tog_view **new_view, int begin_x,
6416 struct tog_reflist_entry *re, struct got_repository *repo)
6418 struct tog_view *log_view;
6419 const struct got_error *err = NULL;
6420 struct got_object_id *commit_id = NULL;
6424 err = resolve_reflist_entry(&commit_id, re, repo);
6426 if (err->code != GOT_ERR_OBJ_TYPE)
6432 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6433 if (log_view == NULL) {
6434 err = got_error_from_errno("view_open");
6438 err = open_log_view(log_view, commit_id, repo,
6439 got_ref_get_name(re->ref), "", 0);
6442 view_close(log_view);
6444 *new_view = log_view;
6450 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6452 struct tog_reflist_entry *re;
6455 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6458 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6459 while (i++ < maxscroll) {
6462 s->first_displayed_entry = re;
6463 re = TAILQ_PREV(re, tog_reflist_head, entry);
6468 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6470 struct tog_reflist_entry *next, *last;
6473 if (s->first_displayed_entry)
6474 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6476 next = TAILQ_FIRST(&s->refs);
6478 last = s->last_displayed_entry;
6479 while (next && last && n++ < maxscroll) {
6480 last = TAILQ_NEXT(last, entry);
6482 s->first_displayed_entry = next;
6483 next = TAILQ_NEXT(next, entry);
6488 static const struct got_error *
6489 search_start_ref_view(struct tog_view *view)
6491 struct tog_ref_view_state *s = &view->state.ref;
6493 s->matched_entry = NULL;
6498 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6500 regmatch_t regmatch;
6502 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6506 static const struct got_error *
6507 search_next_ref_view(struct tog_view *view)
6509 struct tog_ref_view_state *s = &view->state.ref;
6510 struct tog_reflist_entry *re = NULL;
6512 if (!view->searching) {
6513 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6517 if (s->matched_entry) {
6518 if (view->searching == TOG_SEARCH_FORWARD) {
6519 if (s->selected_entry)
6520 re = TAILQ_NEXT(s->selected_entry, entry);
6522 re = TAILQ_PREV(s->selected_entry,
6523 tog_reflist_head, entry);
6525 if (s->selected_entry == NULL)
6526 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6528 re = TAILQ_PREV(s->selected_entry,
6529 tog_reflist_head, entry);
6532 if (s->selected_entry)
6533 re = s->selected_entry;
6534 else if (view->searching == TOG_SEARCH_FORWARD)
6535 re = TAILQ_FIRST(&s->refs);
6537 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6542 if (s->matched_entry == NULL) {
6543 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6546 if (view->searching == TOG_SEARCH_FORWARD)
6547 re = TAILQ_FIRST(&s->refs);
6549 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6552 if (match_reflist_entry(re, &view->regex)) {
6553 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6554 s->matched_entry = re;
6558 if (view->searching == TOG_SEARCH_FORWARD)
6559 re = TAILQ_NEXT(re, entry);
6561 re = TAILQ_PREV(re, tog_reflist_head, entry);
6564 if (s->matched_entry) {
6565 s->first_displayed_entry = s->matched_entry;
6572 static const struct got_error *
6573 show_ref_view(struct tog_view *view)
6575 const struct got_error *err = NULL;
6576 struct tog_ref_view_state *s = &view->state.ref;
6577 struct tog_reflist_entry *re;
6580 struct tog_color *tc;
6582 int limit = view->nlines;
6584 werase(view->window);
6591 re = s->first_displayed_entry;
6593 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6595 return got_error_from_errno("asprintf");
6597 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6602 if (view_needs_focus_indication(view))
6603 wstandout(view->window);
6604 waddwstr(view->window, wline);
6605 if (view_needs_focus_indication(view))
6606 wstandend(view->window);
6611 if (width < view->ncols - 1)
6612 waddch(view->window, '\n');
6617 while (re && limit > 0) {
6619 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6622 struct got_commit_object *ci;
6623 struct got_tag_object *tag;
6624 struct got_object_id *id;
6628 err = got_ref_resolve(&id, s->repo, re->ref);
6631 err = got_object_open_as_tag(&tag, s->repo, id);
6633 if (err->code != GOT_ERR_OBJ_TYPE) {
6637 err = got_object_open_as_commit(&ci, s->repo,
6643 t = got_object_commit_get_committer_time(ci);
6644 got_object_commit_close(ci);
6646 t = got_object_tag_get_tagger_time(tag);
6647 got_object_tag_close(tag);
6650 if (gmtime_r(&t, &tm) == NULL)
6651 return got_error_from_errno("gmtime_r");
6652 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6653 return got_error(GOT_ERR_NO_SPACE);
6655 if (got_ref_is_symbolic(re->ref)) {
6656 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6657 ymd : "", got_ref_get_name(re->ref),
6658 got_ref_get_symref_target(re->ref)) == -1)
6659 return got_error_from_errno("asprintf");
6660 } else if (s->show_ids) {
6661 struct got_object_id *id;
6663 err = got_ref_resolve(&id, s->repo, re->ref);
6666 err = got_object_id_str(&id_str, id);
6671 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6672 got_ref_get_name(re->ref), id_str) == -1) {
6673 err = got_error_from_errno("asprintf");
6680 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6681 got_ref_get_name(re->ref)) == -1)
6682 return got_error_from_errno("asprintf");
6684 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6690 if (n == s->selected) {
6692 wstandout(view->window);
6693 s->selected_entry = re;
6695 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6697 wattr_on(view->window,
6698 COLOR_PAIR(tc->colorpair), NULL);
6699 waddwstr(view->window, wline);
6701 wattr_off(view->window,
6702 COLOR_PAIR(tc->colorpair), NULL);
6703 if (width < view->ncols - 1)
6704 waddch(view->window, '\n');
6705 if (n == s->selected && view->focussed)
6706 wstandend(view->window);
6712 s->last_displayed_entry = re;
6715 re = TAILQ_NEXT(re, entry);
6722 static const struct got_error *
6723 browse_ref_tree(struct tog_view **new_view, int begin_x,
6724 struct tog_reflist_entry *re, struct got_repository *repo)
6726 const struct got_error *err = NULL;
6727 struct got_object_id *commit_id = NULL;
6728 struct tog_view *tree_view;
6732 err = resolve_reflist_entry(&commit_id, re, repo);
6734 if (err->code != GOT_ERR_OBJ_TYPE)
6741 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6742 if (tree_view == NULL) {
6743 err = got_error_from_errno("view_open");
6747 err = open_tree_view(tree_view, commit_id,
6748 got_ref_get_name(re->ref), repo);
6752 *new_view = tree_view;
6757 static const struct got_error *
6758 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6760 const struct got_error *err = NULL;
6761 struct tog_ref_view_state *s = &view->state.ref;
6762 struct tog_view *log_view, *tree_view;
6763 struct tog_reflist_entry *re;
6764 int begin_x = 0, n, nscroll = view->nlines - 1;
6768 s->show_ids = !s->show_ids;
6771 s->show_date = !s->show_date;
6774 s->sort_by_date = !s->sort_by_date;
6775 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6776 got_ref_cmp_by_commit_timestamp_descending :
6777 tog_ref_cmp_by_name, s->repo);
6780 got_reflist_object_id_map_free(tog_refs_idmap);
6781 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6782 &tog_refs, s->repo);
6785 ref_view_free_refs(s);
6786 err = ref_view_load_refs(s);
6790 if (!s->selected_entry)
6792 if (view_is_parent_view(view))
6793 begin_x = view_split_begin_x(view->begin_x);
6794 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6797 log_view->focussed = 1;
6798 if (view_is_parent_view(view)) {
6799 err = view_close_child(view);
6802 err = view_set_child(view, log_view);
6805 view->focus_child = 1;
6807 *new_view = log_view;
6810 if (!s->selected_entry)
6812 if (view_is_parent_view(view))
6813 begin_x = view_split_begin_x(view->begin_x);
6814 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6816 if (err || tree_view == NULL)
6819 tree_view->focussed = 1;
6820 if (view_is_parent_view(view)) {
6821 err = view_close_child(view);
6824 err = view_set_child(view, tree_view);
6827 view->focus_child = 1;
6829 *new_view = tree_view;
6834 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6839 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6840 for (n = 0; n < view->nlines - 1; n++) {
6843 s->first_displayed_entry = re;
6844 re = TAILQ_PREV(re, tog_reflist_head, entry);
6847 s->selected = n - 1;
6852 if (s->selected > 0) {
6856 ref_scroll_up(s, 1);
6865 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6866 s->selected -= MIN(nscroll, s->selected);
6867 ref_scroll_up(s, MAX(0, nscroll));
6872 if (s->selected < s->ndisplayed - 1) {
6876 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6877 /* can't scroll any further */
6879 ref_scroll_down(s, 1);
6889 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6890 /* can't scroll any further; move cursor down */
6891 if (s->selected < s->ndisplayed - 1)
6892 s->selected += MIN(nscroll,
6893 s->ndisplayed - s->selected - 1);
6896 ref_scroll_down(s, nscroll);
6900 err = tog_load_refs(s->repo, s->sort_by_date);
6903 ref_view_free_refs(s);
6904 err = ref_view_load_refs(s);
6907 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6908 s->selected = view->nlines - 2;
6921 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6926 static const struct got_error *
6927 cmd_ref(int argc, char *argv[])
6929 const struct got_error *error;
6930 struct got_repository *repo = NULL;
6931 struct got_worktree *worktree = NULL;
6932 char *cwd = NULL, *repo_path = NULL;
6934 struct tog_view *view;
6935 int *pack_fds = NULL;
6937 while ((ch = getopt(argc, argv, "r:")) != -1) {
6940 repo_path = realpath(optarg, NULL);
6941 if (repo_path == NULL)
6942 return got_error_from_errno2("realpath",
6957 error = got_repo_pack_fds_open(&pack_fds);
6961 if (repo_path == NULL) {
6962 cwd = getcwd(NULL, 0);
6964 return got_error_from_errno("getcwd");
6965 error = got_worktree_open(&worktree, cwd);
6966 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6970 strdup(got_worktree_get_repo_path(worktree));
6972 repo_path = strdup(cwd);
6973 if (repo_path == NULL) {
6974 error = got_error_from_errno("strdup");
6979 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6985 error = apply_unveil(got_repo_get_path(repo), NULL);
6989 error = tog_load_refs(repo, 0);
6993 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6995 error = got_error_from_errno("view_open");
6999 error = open_ref_view(view, repo);
7004 /* Release work tree lock. */
7005 got_worktree_close(worktree);
7008 error = view_loop(view);
7013 const struct got_error *close_err = got_repo_close(repo);
7018 const struct got_error *pack_err =
7019 got_repo_pack_fds_close(pack_fds);
7028 list_commands(FILE *fp)
7032 fprintf(fp, "commands:");
7033 for (i = 0; i < nitems(tog_commands); i++) {
7034 const struct tog_cmd *cmd = &tog_commands[i];
7035 fprintf(fp, " %s", cmd->name);
7041 usage(int hflag, int status)
7043 FILE *fp = (status == 0) ? stdout : stderr;
7045 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7048 fprintf(fp, "lazy usage: %s path\n", getprogname());
7055 make_argv(int argc, ...)
7063 argv = calloc(argc, sizeof(char *));
7066 for (i = 0; i < argc; i++) {
7067 argv[i] = strdup(va_arg(ap, char *));
7068 if (argv[i] == NULL)
7077 * Try to convert 'tog path' into a 'tog log path' command.
7078 * The user could simply have mistyped the command rather than knowingly
7079 * provided a path. So check whether argv[0] can in fact be resolved
7080 * to a path in the HEAD commit and print a special error if not.
7081 * This hack is for mpi@ <3
7083 static const struct got_error *
7084 tog_log_with_path(int argc, char *argv[])
7086 const struct got_error *error = NULL, *close_err;
7087 const struct tog_cmd *cmd = NULL;
7088 struct got_repository *repo = NULL;
7089 struct got_worktree *worktree = NULL;
7090 struct got_object_id *commit_id = NULL, *id = NULL;
7091 struct got_commit_object *commit = NULL;
7092 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7093 char *commit_id_str = NULL, **cmd_argv = NULL;
7094 int *pack_fds = NULL;
7096 cwd = getcwd(NULL, 0);
7098 return got_error_from_errno("getcwd");
7100 error = got_repo_pack_fds_open(&pack_fds);
7104 error = got_worktree_open(&worktree, cwd);
7105 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7109 repo_path = strdup(got_worktree_get_repo_path(worktree));
7111 repo_path = strdup(cwd);
7112 if (repo_path == NULL) {
7113 error = got_error_from_errno("strdup");
7117 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7121 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7126 error = tog_load_refs(repo, 0);
7129 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7130 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7131 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7136 got_worktree_close(worktree);
7140 error = got_object_open_as_commit(&commit, repo, commit_id);
7144 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7146 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7148 fprintf(stderr, "%s: '%s' is no known command or path\n",
7149 getprogname(), argv[0]);
7154 close_err = got_repo_close(repo);
7159 error = got_object_id_str(&commit_id_str, commit_id);
7163 cmd = &tog_commands[0]; /* log */
7165 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7166 error = cmd->cmd_main(argc, cmd_argv);
7169 close_err = got_repo_close(repo);
7174 got_object_commit_close(commit);
7176 got_worktree_close(worktree);
7178 const struct got_error *pack_err =
7179 got_repo_pack_fds_close(pack_fds);
7184 free(commit_id_str);
7191 for (i = 0; i < argc; i++)
7200 main(int argc, char *argv[])
7202 const struct got_error *error = NULL;
7203 const struct tog_cmd *cmd = NULL;
7204 int ch, hflag = 0, Vflag = 0;
7205 char **cmd_argv = NULL;
7206 static const struct option longopts[] = {
7207 { "version", no_argument, NULL, 'V' },
7211 setlocale(LC_CTYPE, "");
7213 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7233 got_version_print_str();
7238 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7246 /* Build an argument vector which runs a default command. */
7247 cmd = &tog_commands[0];
7249 cmd_argv = make_argv(argc, cmd->name);
7253 /* Did the user specify a command? */
7254 for (i = 0; i < nitems(tog_commands); i++) {
7255 if (strncmp(tog_commands[i].name, argv[0],
7256 strlen(argv[0])) == 0) {
7257 cmd = &tog_commands[i];
7266 /* No command specified; try log with a path */
7267 error = tog_log_with_path(argc, argv);
7272 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7279 for (i = 0; i < argc; i++)
7284 if (error && error->code != GOT_ERR_CANCELLED)
7285 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);