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()
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;
758 static const struct got_error *
759 view_resize(struct tog_view *view)
763 if (view->lines > LINES)
764 nlines = view->nlines - (view->lines - LINES);
766 nlines = view->nlines + (LINES - view->lines);
768 if (view->cols > COLS)
769 ncols = view->ncols - (view->cols - COLS);
771 ncols = view->ncols + (COLS - view->cols);
773 if (wresize(view->window, nlines, ncols) == ERR)
774 return got_error_from_errno("wresize");
775 if (replace_panel(view->panel, view->window) == ERR)
776 return got_error_from_errno("replace_panel");
777 wclear(view->window);
779 view->nlines = nlines;
785 view->child->begin_x = view_split_begin_x(view->begin_x);
786 if (view->child->begin_x == 0) {
787 view_fullscreen(view->child);
788 if (view->child->focussed)
789 show_panel(view->child->panel);
791 show_panel(view->panel);
793 view_splitscreen(view->child);
794 show_panel(view->child->panel);
801 static const struct got_error *
802 view_close_child(struct tog_view *view)
804 const struct got_error *err = NULL;
806 if (view->child == NULL)
809 err = view_close(view->child);
815 view_set_child(struct tog_view *view, struct tog_view *child)
818 child->parent = view;
822 view_is_splitscreen(struct tog_view *view)
824 return view->begin_x > 0;
833 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
834 cols = 80; /* Default */
840 resize_term(lines, cols);
843 static const struct got_error *
844 view_search_start(struct tog_view *view)
846 const struct got_error *err = NULL;
850 if (view->search_started) {
851 regfree(&view->regex);
853 memset(&view->regmatch, 0, sizeof(view->regmatch));
855 view->search_started = 0;
857 if (view->nlines < 1)
860 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
861 wclrtoeol(view->window);
865 ret = wgetnstr(view->window, pattern, sizeof(pattern));
871 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
872 err = view->search_start(view);
874 regfree(&view->regex);
877 view->search_started = 1;
878 view->searching = TOG_SEARCH_FORWARD;
879 view->search_next_done = 0;
880 view->search_next(view);
886 static const struct got_error *
887 view_input(struct tog_view **new, int *done, struct tog_view *view,
888 struct tog_view_list_head *views)
890 const struct got_error *err = NULL;
896 /* Clear "no matches" indicator. */
897 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
898 view->search_next_done == TOG_SEARCH_HAVE_NONE)
899 view->search_next_done = TOG_SEARCH_HAVE_MORE;
901 if (view->searching && !view->search_next_done) {
902 errcode = pthread_mutex_unlock(&tog_mutex);
904 return got_error_set_errno(errcode,
905 "pthread_mutex_unlock");
907 errcode = pthread_mutex_lock(&tog_mutex);
909 return got_error_set_errno(errcode,
910 "pthread_mutex_lock");
911 view->search_next(view);
915 nodelay(stdscr, FALSE);
916 /* Allow threads to make progress while we are waiting for input. */
917 errcode = pthread_mutex_unlock(&tog_mutex);
919 return got_error_set_errno(errcode, "pthread_mutex_unlock");
920 ch = wgetch(view->window);
921 errcode = pthread_mutex_lock(&tog_mutex);
923 return got_error_set_errno(errcode, "pthread_mutex_lock");
924 nodelay(stdscr, TRUE);
926 if (tog_sigwinch_received || tog_sigcont_received) {
928 tog_sigwinch_received = 0;
929 tog_sigcont_received = 0;
930 TAILQ_FOREACH(v, views, entry) {
931 err = view_resize(v);
934 err = v->input(new, v, KEY_RESIZE);
938 err = view_resize(v->child);
941 err = v->child->input(new, v->child,
953 view->child->focussed = 1;
954 view->focus_child = 1;
955 } else if (view->parent) {
957 view->parent->focussed = 1;
958 view->parent->focus_child = 0;
962 err = view->input(new, view, ch);
969 if (view_is_parent_view(view)) {
970 if (view->child == NULL)
972 if (view_is_splitscreen(view->child)) {
974 view->child->focussed = 1;
975 err = view_fullscreen(view->child);
977 err = view_splitscreen(view->child);
980 err = view->child->input(new, view->child,
983 if (view_is_splitscreen(view)) {
984 view->parent->focussed = 0;
986 err = view_fullscreen(view);
988 err = view_splitscreen(view);
992 err = view->input(new, view, KEY_RESIZE);
998 if (view->search_start)
999 view_search_start(view);
1001 err = view->input(new, view, ch);
1005 if (view->search_started && view->search_next) {
1006 view->searching = (ch == 'n' ?
1007 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1008 view->search_next_done = 0;
1009 view->search_next(view);
1011 err = view->input(new, view, ch);
1014 err = view->input(new, view, ch);
1022 view_vborder(struct tog_view *view)
1025 const struct tog_view *view_above;
1028 return view_vborder(view->parent);
1030 panel = panel_above(view->panel);
1034 view_above = panel_userptr(panel);
1035 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1036 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1040 view_needs_focus_indication(struct tog_view *view)
1042 if (view_is_parent_view(view)) {
1043 if (view->child == NULL || view->child->focussed)
1045 if (!view_is_splitscreen(view->child))
1047 } else if (!view_is_splitscreen(view))
1050 return view->focussed;
1053 static const struct got_error *
1054 view_loop(struct tog_view *view)
1056 const struct got_error *err = NULL;
1057 struct tog_view_list_head views;
1058 struct tog_view *new_view;
1059 int fast_refresh = 10;
1060 int done = 0, errcode;
1062 errcode = pthread_mutex_lock(&tog_mutex);
1064 return got_error_set_errno(errcode, "pthread_mutex_lock");
1067 TAILQ_INSERT_HEAD(&views, view, entry);
1070 err = view->show(view);
1075 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1076 /* Refresh fast during initialization, then become slower. */
1077 if (fast_refresh && fast_refresh-- == 0)
1078 halfdelay(10); /* switch to once per second */
1080 err = view_input(&new_view, &done, view, &views);
1084 struct tog_view *v, *prev = NULL;
1086 if (view_is_parent_view(view))
1087 prev = TAILQ_PREV(view, tog_view_list_head,
1089 else if (view->parent)
1090 prev = view->parent;
1093 view->parent->child = NULL;
1094 view->parent->focus_child = 0;
1096 TAILQ_REMOVE(&views, view, entry);
1098 err = view_close(view);
1103 TAILQ_FOREACH(v, &views, entry) {
1107 if (view == NULL && new_view == NULL) {
1108 /* No view has focus. Try to pick one. */
1111 else if (!TAILQ_EMPTY(&views)) {
1112 view = TAILQ_LAST(&views,
1113 tog_view_list_head);
1116 if (view->focus_child) {
1117 view->child->focussed = 1;
1125 struct tog_view *v, *t;
1126 /* Only allow one parent view per type. */
1127 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1128 if (v->type != new_view->type)
1130 TAILQ_REMOVE(&views, v, entry);
1131 err = view_close(v);
1136 TAILQ_INSERT_TAIL(&views, new_view, entry);
1140 if (view_is_parent_view(view)) {
1141 if (view->child && view->child->focussed)
1144 if (view->parent && view->parent->focussed)
1145 view = view->parent;
1147 show_panel(view->panel);
1148 if (view->child && view_is_splitscreen(view->child))
1149 show_panel(view->child->panel);
1150 if (view->parent && view_is_splitscreen(view)) {
1151 err = view->parent->show(view->parent);
1155 err = view->show(view);
1159 err = view->child->show(view->child);
1168 while (!TAILQ_EMPTY(&views)) {
1169 view = TAILQ_FIRST(&views);
1170 TAILQ_REMOVE(&views, view, entry);
1174 errcode = pthread_mutex_unlock(&tog_mutex);
1175 if (errcode && err == NULL)
1176 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1186 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1191 /* Create newly allocated wide-character string equivalent to a byte string. */
1192 static const struct got_error *
1193 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1196 const struct got_error *err = NULL;
1199 *wlen = mbstowcs(NULL, s, 0);
1200 if (*wlen == (size_t)-1) {
1202 if (errno != EILSEQ)
1203 return got_error_from_errno("mbstowcs");
1205 /* byte string invalid in current encoding; try to "fix" it */
1206 err = got_mbsavis(&vis, &vislen, s);
1209 *wlen = mbstowcs(NULL, vis, 0);
1210 if (*wlen == (size_t)-1) {
1211 err = got_error_from_errno("mbstowcs"); /* give up */
1216 *ws = calloc(*wlen + 1, sizeof(**ws));
1218 err = got_error_from_errno("calloc");
1222 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1223 err = got_error_from_errno("mbstowcs");
1234 static const struct got_error *
1235 expand_tab(char **ptr, const char *src)
1238 size_t len, n, idx = 0, sz = 0;
1241 n = len = strlen(src);
1242 dst = malloc((n + 1) * sizeof(char));
1244 return got_error_from_errno("malloc");
1246 while (idx < len && src[idx]) {
1247 const char c = src[idx];
1250 size_t nb = TABSIZE - sz % TABSIZE;
1252 dst = reallocarray(dst, n, sizeof(char));
1254 return got_error_from_errno("reallocarray");
1255 memcpy(dst + sz, " ", nb);
1258 dst[sz++] = src[idx];
1267 /* Format a line for display, ensuring that it won't overflow a width limit. */
1268 static const struct got_error *
1269 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1270 int col_tab_align, int expand)
1272 const struct got_error *err = NULL;
1274 wchar_t *wline = NULL;
1283 err = expand_tab(&exstr, line);
1288 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1293 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1294 wline[wlen - 1] = L'\0';
1297 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1298 wline[wlen - 1] = L'\0';
1304 int width = wcwidth(wline[i]);
1311 if (width == 1 || width == 2) {
1312 if (cols + width > wlimit)
1316 } else if (width == -1) {
1317 if (wline[i] == L'\t') {
1319 ((cols + col_tab_align) % TABSIZE);
1324 if (cols + width > wlimit)
1329 err = got_error_from_errno("wcwidth");
1344 /* Skip the leading nscroll columns of a wide character string. */
1345 const struct got_error *
1346 scroll_wline(wchar_t **wlinep, wchar_t *wline, int nscroll,
1350 size_t wlen = wcslen(wline);
1355 while (i < wlen && cols < nscroll) {
1356 int width = wcwidth(wline[i]);
1363 if (width == 1 || width == 2) {
1364 if (cols + width > nscroll)
1368 } else if (width == -1) {
1369 if (wline[i] == L'\t') {
1371 ((cols + col_tab_align) % TABSIZE);
1376 if (cols + width > nscroll)
1381 return got_error_from_errno("wcwidth");
1385 *wlinep = &wline[j];
1389 static const struct got_error*
1390 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1391 struct got_object_id *id, struct got_repository *repo)
1393 static const struct got_error *err = NULL;
1394 struct got_reflist_entry *re;
1400 TAILQ_FOREACH(re, refs, entry) {
1401 struct got_tag_object *tag = NULL;
1402 struct got_object_id *ref_id;
1405 name = got_ref_get_name(re->ref);
1406 if (strcmp(name, GOT_REF_HEAD) == 0)
1408 if (strncmp(name, "refs/", 5) == 0)
1410 if (strncmp(name, "got/", 4) == 0 &&
1411 strncmp(name, "got/backup/", 11) != 0)
1413 if (strncmp(name, "heads/", 6) == 0)
1415 if (strncmp(name, "remotes/", 8) == 0) {
1417 s = strstr(name, "/" GOT_REF_HEAD);
1418 if (s != NULL && s[strlen(s)] == '\0')
1421 err = got_ref_resolve(&ref_id, repo, re->ref);
1424 if (strncmp(name, "tags/", 5) == 0) {
1425 err = got_object_open_as_tag(&tag, repo, ref_id);
1427 if (err->code != GOT_ERR_OBJ_TYPE) {
1431 /* Ref points at something other than a tag. */
1436 cmp = got_object_id_cmp(tag ?
1437 got_object_tag_get_object_id(tag) : ref_id, id);
1440 got_object_tag_close(tag);
1444 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1445 s ? ", " : "", name) == -1) {
1446 err = got_error_from_errno("asprintf");
1457 static const struct got_error *
1458 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1463 smallerthan = strchr(author, '<');
1464 if (smallerthan && smallerthan[1] != '\0')
1465 author = smallerthan + 1;
1466 author[strcspn(author, "@>")] = '\0';
1467 return format_line(wauthor, author_width, author, limit, col_tab_align,
1471 static const struct got_error *
1472 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1473 struct got_object_id *id, const size_t date_display_cols,
1474 int author_display_cols)
1476 struct tog_log_view_state *s = &view->state.log;
1477 const struct got_error *err = NULL;
1478 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1479 char *logmsg0 = NULL, *logmsg = NULL;
1480 char *author = NULL;
1481 wchar_t *wlogmsg = NULL, *wauthor = NULL, *scrolled_wline;
1482 int author_width, logmsg_width;
1483 char *newline, *line = NULL;
1485 const int avail = view->ncols;
1487 time_t committer_time;
1488 struct tog_color *tc;
1490 committer_time = got_object_commit_get_committer_time(commit);
1491 if (gmtime_r(&committer_time, &tm) == NULL)
1492 return got_error_from_errno("gmtime_r");
1493 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1494 return got_error(GOT_ERR_NO_SPACE);
1496 if (avail <= date_display_cols)
1497 limit = MIN(sizeof(datebuf) - 1, avail);
1499 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1500 tc = get_color(&s->colors, TOG_COLOR_DATE);
1502 wattr_on(view->window,
1503 COLOR_PAIR(tc->colorpair), NULL);
1504 waddnstr(view->window, datebuf, limit);
1506 wattr_off(view->window,
1507 COLOR_PAIR(tc->colorpair), NULL);
1514 err = got_object_id_str(&id_str, id);
1517 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1519 wattr_on(view->window,
1520 COLOR_PAIR(tc->colorpair), NULL);
1521 wprintw(view->window, "%.8s ", id_str);
1523 wattr_off(view->window,
1524 COLOR_PAIR(tc->colorpair), NULL);
1531 author = strdup(got_object_commit_get_author(commit));
1532 if (author == NULL) {
1533 err = got_error_from_errno("strdup");
1536 err = format_author(&wauthor, &author_width, author, avail - col, col);
1539 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1541 wattr_on(view->window,
1542 COLOR_PAIR(tc->colorpair), NULL);
1543 waddwstr(view->window, wauthor);
1545 wattr_off(view->window,
1546 COLOR_PAIR(tc->colorpair), NULL);
1547 col += author_width;
1548 while (col < avail && author_width < author_display_cols + 2) {
1549 waddch(view->window, ' ');
1556 err = got_object_commit_get_logmsg(&logmsg0, commit);
1560 while (*logmsg == '\n')
1562 newline = strchr(logmsg, '\n');
1565 limit = view->x + avail - col;
1566 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col, 1);
1569 err = scroll_wline(&scrolled_wline, wlogmsg, view->x, col);
1572 waddwstr(view->window, scrolled_wline);
1573 logmsg_width = wcswidth(scrolled_wline, wcslen(scrolled_wline));
1574 col += MAX(logmsg_width, 0);
1575 while (col < avail) {
1576 waddch(view->window, ' ');
1588 static struct commit_queue_entry *
1589 alloc_commit_queue_entry(struct got_commit_object *commit,
1590 struct got_object_id *id)
1592 struct commit_queue_entry *entry;
1594 entry = calloc(1, sizeof(*entry));
1599 entry->commit = commit;
1604 pop_commit(struct commit_queue *commits)
1606 struct commit_queue_entry *entry;
1608 entry = TAILQ_FIRST(&commits->head);
1609 TAILQ_REMOVE(&commits->head, entry, entry);
1610 got_object_commit_close(entry->commit);
1611 commits->ncommits--;
1612 /* Don't free entry->id! It is owned by the commit graph. */
1617 free_commits(struct commit_queue *commits)
1619 while (!TAILQ_EMPTY(&commits->head))
1620 pop_commit(commits);
1623 static const struct got_error *
1624 match_commit(int *have_match, struct got_object_id *id,
1625 struct got_commit_object *commit, regex_t *regex)
1627 const struct got_error *err = NULL;
1628 regmatch_t regmatch;
1629 char *id_str = NULL, *logmsg = NULL;
1633 err = got_object_id_str(&id_str, id);
1637 err = got_object_commit_get_logmsg(&logmsg, commit);
1641 if (regexec(regex, got_object_commit_get_author(commit), 1,
1642 ®match, 0) == 0 ||
1643 regexec(regex, got_object_commit_get_committer(commit), 1,
1644 ®match, 0) == 0 ||
1645 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1646 regexec(regex, logmsg, 1, ®match, 0) == 0)
1654 static const struct got_error *
1655 queue_commits(struct tog_log_thread_args *a)
1657 const struct got_error *err = NULL;
1660 * We keep all commits open throughout the lifetime of the log
1661 * view in order to avoid having to re-fetch commits from disk
1662 * while updating the display.
1665 struct got_object_id *id;
1666 struct got_commit_object *commit;
1667 struct commit_queue_entry *entry;
1670 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1672 if (err || id == NULL)
1675 err = got_object_open_as_commit(&commit, a->repo, id);
1678 entry = alloc_commit_queue_entry(commit, id);
1679 if (entry == NULL) {
1680 err = got_error_from_errno("alloc_commit_queue_entry");
1684 errcode = pthread_mutex_lock(&tog_mutex);
1686 err = got_error_set_errno(errcode,
1687 "pthread_mutex_lock");
1691 entry->idx = a->commits->ncommits;
1692 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1693 a->commits->ncommits++;
1695 if (*a->searching == TOG_SEARCH_FORWARD &&
1696 !*a->search_next_done) {
1698 err = match_commit(&have_match, id, commit, a->regex);
1702 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1705 errcode = pthread_mutex_unlock(&tog_mutex);
1706 if (errcode && err == NULL)
1707 err = got_error_set_errno(errcode,
1708 "pthread_mutex_unlock");
1711 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1717 select_commit(struct tog_log_view_state *s)
1719 struct commit_queue_entry *entry;
1722 entry = s->first_displayed_entry;
1724 if (ncommits == s->selected) {
1725 s->selected_entry = entry;
1728 entry = TAILQ_NEXT(entry, entry);
1733 static const struct got_error *
1734 draw_commits(struct tog_view *view)
1736 const struct got_error *err = NULL;
1737 struct tog_log_view_state *s = &view->state.log;
1738 struct commit_queue_entry *entry = s->selected_entry;
1739 const int limit = view->nlines;
1741 int ncommits, author_cols = 4;
1742 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1743 char *refs_str = NULL;
1745 struct tog_color *tc;
1746 static const size_t date_display_cols = 12;
1748 if (s->selected_entry &&
1749 !(view->searching && view->search_next_done == 0)) {
1750 struct got_reflist_head *refs;
1751 err = got_object_id_str(&id_str, s->selected_entry->id);
1754 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1755 s->selected_entry->id);
1757 err = build_refs_str(&refs_str, refs,
1758 s->selected_entry->id, s->repo);
1764 if (s->thread_args.commits_needed == 0)
1765 halfdelay(10); /* disable fast refresh */
1767 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1768 if (asprintf(&ncommits_str, " [%d/%d] %s",
1769 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1770 (view->searching && !view->search_next_done) ?
1771 "searching..." : "loading...") == -1) {
1772 err = got_error_from_errno("asprintf");
1776 const char *search_str = NULL;
1778 if (view->searching) {
1779 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1780 search_str = "no more matches";
1781 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1782 search_str = "no matches found";
1783 else if (!view->search_next_done)
1784 search_str = "searching...";
1787 if (asprintf(&ncommits_str, " [%d/%d] %s",
1788 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1789 search_str ? search_str :
1790 (refs_str ? refs_str : "")) == -1) {
1791 err = got_error_from_errno("asprintf");
1796 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1797 if (asprintf(&header, "commit %s %s%s",
1798 id_str ? id_str : "........................................",
1799 s->in_repo_path, ncommits_str) == -1) {
1800 err = got_error_from_errno("asprintf");
1804 } else if (asprintf(&header, "commit %s%s",
1805 id_str ? id_str : "........................................",
1806 ncommits_str) == -1) {
1807 err = got_error_from_errno("asprintf");
1811 err = format_line(&wline, &width, header, view->ncols, 0, 0);
1815 werase(view->window);
1817 if (view_needs_focus_indication(view))
1818 wstandout(view->window);
1819 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1821 wattr_on(view->window,
1822 COLOR_PAIR(tc->colorpair), NULL);
1823 waddwstr(view->window, wline);
1825 wattr_off(view->window,
1826 COLOR_PAIR(tc->colorpair), NULL);
1827 while (width < view->ncols) {
1828 waddch(view->window, ' ');
1831 if (view_needs_focus_indication(view))
1832 wstandend(view->window);
1837 /* Grow author column size if necessary, and set view->maxx. */
1838 entry = s->first_displayed_entry;
1842 char *author, *eol, *msg, *msg0;
1843 wchar_t *wauthor, *wmsg;
1845 if (ncommits >= limit - 1)
1847 author = strdup(got_object_commit_get_author(entry->commit));
1848 if (author == NULL) {
1849 err = got_error_from_errno("strdup");
1852 err = format_author(&wauthor, &width, author, COLS,
1854 if (author_cols < width)
1855 author_cols = width;
1858 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1862 while (*msg == '\n')
1864 if ((eol = strchr(msg, '\n')))
1866 err = format_line(&wmsg, &width, msg, INT_MAX,
1867 date_display_cols + author_cols, 0);
1870 view->maxx = MAX(view->maxx, width);
1874 entry = TAILQ_NEXT(entry, entry);
1877 entry = s->first_displayed_entry;
1878 s->last_displayed_entry = s->first_displayed_entry;
1881 if (ncommits >= limit - 1)
1883 if (ncommits == s->selected)
1884 wstandout(view->window);
1885 err = draw_commit(view, entry->commit, entry->id,
1886 date_display_cols, author_cols);
1887 if (ncommits == s->selected)
1888 wstandend(view->window);
1892 s->last_displayed_entry = entry;
1893 entry = TAILQ_NEXT(entry, entry);
1908 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1910 struct commit_queue_entry *entry;
1913 entry = TAILQ_FIRST(&s->commits.head);
1914 if (s->first_displayed_entry == entry)
1917 entry = s->first_displayed_entry;
1918 while (entry && nscrolled < maxscroll) {
1919 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1921 s->first_displayed_entry = entry;
1927 static const struct got_error *
1928 trigger_log_thread(struct tog_view *view, int wait)
1930 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1933 halfdelay(1); /* fast refresh while loading commits */
1935 while (ta->commits_needed > 0 || ta->load_all) {
1936 if (ta->log_complete)
1939 /* Wake the log thread. */
1940 errcode = pthread_cond_signal(&ta->need_commits);
1942 return got_error_set_errno(errcode,
1943 "pthread_cond_signal");
1946 * The mutex will be released while the view loop waits
1947 * in wgetch(), at which time the log thread will run.
1952 /* Display progress update in log view. */
1953 show_log_view(view);
1957 /* Wait right here while next commit is being loaded. */
1958 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1960 return got_error_set_errno(errcode,
1961 "pthread_cond_wait");
1963 /* Display progress update in log view. */
1964 show_log_view(view);
1972 static const struct got_error *
1973 log_scroll_down(struct tog_view *view, int maxscroll)
1975 struct tog_log_view_state *s = &view->state.log;
1976 const struct got_error *err = NULL;
1977 struct commit_queue_entry *pentry;
1978 int nscrolled = 0, ncommits_needed;
1980 if (s->last_displayed_entry == NULL)
1983 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1984 if (s->commits.ncommits < ncommits_needed &&
1985 !s->thread_args.log_complete) {
1987 * Ask the log thread for required amount of commits.
1989 s->thread_args.commits_needed += maxscroll;
1990 err = trigger_log_thread(view, 1);
1996 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2000 s->last_displayed_entry = pentry;
2002 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2005 s->first_displayed_entry = pentry;
2006 } while (++nscrolled < maxscroll);
2011 static const struct got_error *
2012 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
2013 struct got_commit_object *commit, struct got_object_id *commit_id,
2014 struct tog_view *log_view, struct got_repository *repo)
2016 const struct got_error *err;
2017 struct got_object_qid *parent_id;
2018 struct tog_view *diff_view;
2020 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2021 if (diff_view == NULL)
2022 return got_error_from_errno("view_open");
2024 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2025 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2026 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2028 *new_view = diff_view;
2032 static const struct got_error *
2033 tree_view_visit_subtree(struct tog_tree_view_state *s,
2034 struct got_tree_object *subtree)
2036 struct tog_parent_tree *parent;
2038 parent = calloc(1, sizeof(*parent));
2040 return got_error_from_errno("calloc");
2042 parent->tree = s->tree;
2043 parent->first_displayed_entry = s->first_displayed_entry;
2044 parent->selected_entry = s->selected_entry;
2045 parent->selected = s->selected;
2046 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2049 s->first_displayed_entry = NULL;
2053 static const struct got_error *
2054 tree_view_walk_path(struct tog_tree_view_state *s,
2055 struct got_commit_object *commit, const char *path)
2057 const struct got_error *err = NULL;
2058 struct got_tree_object *tree = NULL;
2060 char *slash, *subpath = NULL;
2062 /* Walk the path and open corresponding tree objects. */
2065 struct got_tree_entry *te;
2066 struct got_object_id *tree_id;
2072 /* Ensure the correct subtree entry is selected. */
2073 slash = strchr(p, '/');
2075 te_name = strdup(p);
2077 te_name = strndup(p, slash - p);
2078 if (te_name == NULL) {
2079 err = got_error_from_errno("strndup");
2082 te = got_object_tree_find_entry(s->tree, te_name);
2084 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2089 s->first_displayed_entry = s->selected_entry = te;
2091 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2092 break; /* jump to this file's entry */
2094 slash = strchr(p, '/');
2096 subpath = strndup(path, slash - path);
2098 subpath = strdup(path);
2099 if (subpath == NULL) {
2100 err = got_error_from_errno("strdup");
2104 err = got_object_id_by_path(&tree_id, s->repo, commit,
2109 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2114 err = tree_view_visit_subtree(s, tree);
2116 got_object_tree_close(tree);
2130 static const struct got_error *
2131 browse_commit_tree(struct tog_view **new_view, int begin_x,
2132 struct commit_queue_entry *entry, const char *path,
2133 const char *head_ref_name, struct got_repository *repo)
2135 const struct got_error *err = NULL;
2136 struct tog_tree_view_state *s;
2137 struct tog_view *tree_view;
2139 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2140 if (tree_view == NULL)
2141 return got_error_from_errno("view_open");
2143 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2146 s = &tree_view->state.tree;
2148 *new_view = tree_view;
2150 if (got_path_is_root_dir(path))
2153 return tree_view_walk_path(s, entry->commit, path);
2156 static const struct got_error *
2157 block_signals_used_by_main_thread(void)
2162 if (sigemptyset(&sigset) == -1)
2163 return got_error_from_errno("sigemptyset");
2165 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2166 if (sigaddset(&sigset, SIGWINCH) == -1)
2167 return got_error_from_errno("sigaddset");
2168 if (sigaddset(&sigset, SIGCONT) == -1)
2169 return got_error_from_errno("sigaddset");
2170 if (sigaddset(&sigset, SIGINT) == -1)
2171 return got_error_from_errno("sigaddset");
2172 if (sigaddset(&sigset, SIGTERM) == -1)
2173 return got_error_from_errno("sigaddset");
2175 /* ncurses handles SIGTSTP */
2176 if (sigaddset(&sigset, SIGTSTP) == -1)
2177 return got_error_from_errno("sigaddset");
2179 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2181 return got_error_set_errno(errcode, "pthread_sigmask");
2187 log_thread(void *arg)
2189 const struct got_error *err = NULL;
2191 struct tog_log_thread_args *a = arg;
2194 err = block_signals_used_by_main_thread();
2198 while (!done && !err && !tog_fatal_signal_received()) {
2199 err = queue_commits(a);
2201 if (err->code != GOT_ERR_ITER_COMPLETED)
2205 } else if (a->commits_needed > 0 && !a->load_all)
2206 a->commits_needed--;
2208 errcode = pthread_mutex_lock(&tog_mutex);
2210 err = got_error_set_errno(errcode,
2211 "pthread_mutex_lock");
2213 } else if (*a->quit)
2215 else if (*a->first_displayed_entry == NULL) {
2216 *a->first_displayed_entry =
2217 TAILQ_FIRST(&a->commits->head);
2218 *a->selected_entry = *a->first_displayed_entry;
2221 errcode = pthread_cond_signal(&a->commit_loaded);
2223 err = got_error_set_errno(errcode,
2224 "pthread_cond_signal");
2225 pthread_mutex_unlock(&tog_mutex);
2230 a->commits_needed = 0;
2232 if (a->commits_needed == 0 && !a->load_all) {
2233 errcode = pthread_cond_wait(&a->need_commits,
2236 err = got_error_set_errno(errcode,
2237 "pthread_cond_wait");
2243 errcode = pthread_mutex_unlock(&tog_mutex);
2244 if (errcode && err == NULL)
2245 err = got_error_set_errno(errcode,
2246 "pthread_mutex_unlock");
2248 a->log_complete = 1;
2252 static const struct got_error *
2253 stop_log_thread(struct tog_log_view_state *s)
2255 const struct got_error *err = NULL;
2260 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2262 return got_error_set_errno(errcode,
2263 "pthread_cond_signal");
2264 errcode = pthread_mutex_unlock(&tog_mutex);
2266 return got_error_set_errno(errcode,
2267 "pthread_mutex_unlock");
2268 errcode = pthread_join(s->thread, (void **)&err);
2270 return got_error_set_errno(errcode, "pthread_join");
2271 errcode = pthread_mutex_lock(&tog_mutex);
2273 return got_error_set_errno(errcode,
2274 "pthread_mutex_lock");
2275 s->thread = 0; //NULL;
2278 if (s->thread_args.repo) {
2279 err = got_repo_close(s->thread_args.repo);
2280 s->thread_args.repo = NULL;
2283 if (s->thread_args.pack_fds) {
2284 const struct got_error *pack_err =
2285 got_repo_pack_fds_close(s->thread_args.pack_fds);
2288 s->thread_args.pack_fds = NULL;
2291 if (s->thread_args.graph) {
2292 got_commit_graph_close(s->thread_args.graph);
2293 s->thread_args.graph = NULL;
2299 static const struct got_error *
2300 close_log_view(struct tog_view *view)
2302 const struct got_error *err = NULL;
2303 struct tog_log_view_state *s = &view->state.log;
2306 err = stop_log_thread(s);
2308 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2309 if (errcode && err == NULL)
2310 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2312 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2313 if (errcode && err == NULL)
2314 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2316 free_commits(&s->commits);
2317 free(s->in_repo_path);
2318 s->in_repo_path = NULL;
2321 free(s->head_ref_name);
2322 s->head_ref_name = NULL;
2326 static const struct got_error *
2327 search_start_log_view(struct tog_view *view)
2329 struct tog_log_view_state *s = &view->state.log;
2331 s->matched_entry = NULL;
2332 s->search_entry = NULL;
2336 static const struct got_error *
2337 search_next_log_view(struct tog_view *view)
2339 const struct got_error *err = NULL;
2340 struct tog_log_view_state *s = &view->state.log;
2341 struct commit_queue_entry *entry;
2343 /* Display progress update in log view. */
2344 show_log_view(view);
2348 if (s->search_entry) {
2350 errcode = pthread_mutex_unlock(&tog_mutex);
2352 return got_error_set_errno(errcode,
2353 "pthread_mutex_unlock");
2354 ch = wgetch(view->window);
2355 errcode = pthread_mutex_lock(&tog_mutex);
2357 return got_error_set_errno(errcode,
2358 "pthread_mutex_lock");
2359 if (ch == KEY_BACKSPACE) {
2360 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2363 if (view->searching == TOG_SEARCH_FORWARD)
2364 entry = TAILQ_NEXT(s->search_entry, entry);
2366 entry = TAILQ_PREV(s->search_entry,
2367 commit_queue_head, entry);
2368 } else if (s->matched_entry) {
2369 if (view->searching == TOG_SEARCH_FORWARD)
2370 entry = TAILQ_NEXT(s->matched_entry, entry);
2372 entry = TAILQ_PREV(s->matched_entry,
2373 commit_queue_head, entry);
2375 entry = s->selected_entry;
2381 if (entry == NULL) {
2382 if (s->thread_args.log_complete ||
2383 view->searching == TOG_SEARCH_BACKWARD) {
2384 view->search_next_done =
2385 (s->matched_entry == NULL ?
2386 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2387 s->search_entry = NULL;
2391 * Poke the log thread for more commits and return,
2392 * allowing the main loop to make progress. Search
2393 * will resume at s->search_entry once we come back.
2395 s->thread_args.commits_needed++;
2396 return trigger_log_thread(view, 0);
2399 err = match_commit(&have_match, entry->id, entry->commit,
2404 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2405 s->matched_entry = entry;
2409 s->search_entry = entry;
2410 if (view->searching == TOG_SEARCH_FORWARD)
2411 entry = TAILQ_NEXT(entry, entry);
2413 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2416 if (s->matched_entry) {
2417 int cur = s->selected_entry->idx;
2418 while (cur < s->matched_entry->idx) {
2419 err = input_log_view(NULL, view, KEY_DOWN);
2424 while (cur > s->matched_entry->idx) {
2425 err = input_log_view(NULL, view, KEY_UP);
2432 s->search_entry = NULL;
2437 static const struct got_error *
2438 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2439 struct got_repository *repo, const char *head_ref_name,
2440 const char *in_repo_path, int log_branches)
2442 const struct got_error *err = NULL;
2443 struct tog_log_view_state *s = &view->state.log;
2444 struct got_repository *thread_repo = NULL;
2445 struct got_commit_graph *thread_graph = NULL;
2448 if (in_repo_path != s->in_repo_path) {
2449 free(s->in_repo_path);
2450 s->in_repo_path = strdup(in_repo_path);
2451 if (s->in_repo_path == NULL)
2452 return got_error_from_errno("strdup");
2455 /* The commit queue only contains commits being displayed. */
2456 TAILQ_INIT(&s->commits.head);
2457 s->commits.ncommits = 0;
2460 if (head_ref_name) {
2461 s->head_ref_name = strdup(head_ref_name);
2462 if (s->head_ref_name == NULL) {
2463 err = got_error_from_errno("strdup");
2467 s->start_id = got_object_id_dup(start_id);
2468 if (s->start_id == NULL) {
2469 err = got_error_from_errno("got_object_id_dup");
2472 s->log_branches = log_branches;
2474 STAILQ_INIT(&s->colors);
2475 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2476 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2477 get_color_value("TOG_COLOR_COMMIT"));
2480 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2481 get_color_value("TOG_COLOR_AUTHOR"));
2483 free_colors(&s->colors);
2486 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2487 get_color_value("TOG_COLOR_DATE"));
2489 free_colors(&s->colors);
2494 view->show = show_log_view;
2495 view->input = input_log_view;
2496 view->close = close_log_view;
2497 view->search_start = search_start_log_view;
2498 view->search_next = search_next_log_view;
2500 if (s->thread_args.pack_fds == NULL) {
2501 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2505 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2506 s->thread_args.pack_fds);
2509 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2513 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2514 s->repo, NULL, NULL);
2518 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2520 err = got_error_set_errno(errcode, "pthread_cond_init");
2523 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2525 err = got_error_set_errno(errcode, "pthread_cond_init");
2529 s->thread_args.commits_needed = view->nlines;
2530 s->thread_args.graph = thread_graph;
2531 s->thread_args.commits = &s->commits;
2532 s->thread_args.in_repo_path = s->in_repo_path;
2533 s->thread_args.start_id = s->start_id;
2534 s->thread_args.repo = thread_repo;
2535 s->thread_args.log_complete = 0;
2536 s->thread_args.quit = &s->quit;
2537 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2538 s->thread_args.selected_entry = &s->selected_entry;
2539 s->thread_args.searching = &view->searching;
2540 s->thread_args.search_next_done = &view->search_next_done;
2541 s->thread_args.regex = &view->regex;
2544 close_log_view(view);
2548 static const struct got_error *
2549 show_log_view(struct tog_view *view)
2551 const struct got_error *err;
2552 struct tog_log_view_state *s = &view->state.log;
2554 if (s->thread == 0) { //NULL) {
2555 int errcode = pthread_create(&s->thread, NULL, log_thread,
2558 return got_error_set_errno(errcode, "pthread_create");
2559 if (s->thread_args.commits_needed > 0) {
2560 err = trigger_log_thread(view, 1);
2566 return draw_commits(view);
2569 static const struct got_error *
2570 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2572 const struct got_error *err = NULL;
2573 struct tog_log_view_state *s = &view->state.log;
2574 struct tog_view *diff_view = NULL, *tree_view = NULL;
2575 struct tog_view *ref_view = NULL;
2576 struct commit_queue_entry *entry;
2577 int begin_x = 0, n, nscroll = view->nlines - 1;
2579 if (s->thread_args.load_all) {
2580 if (ch == KEY_BACKSPACE)
2581 s->thread_args.load_all = 0;
2582 else if (s->thread_args.log_complete) {
2583 s->thread_args.load_all = 0;
2584 log_scroll_down(view, s->commits.ncommits);
2585 s->selected = MIN(view->nlines - 2,
2586 s->commits.ncommits - 1);
2600 view->x = MAX(view->maxx - view->ncols / 2, 0);
2604 if (view->x + view->ncols / 2 < view->maxx)
2605 view->x += 2; /* move two columns right */
2609 view->x -= MIN(view->x, 2); /* move two columns back */
2616 if (s->first_displayed_entry == NULL)
2618 if (s->selected > 0)
2621 log_scroll_up(s, 1);
2627 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2636 if (s->first_displayed_entry == NULL)
2638 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2639 s->selected = MAX(0, s->selected - nscroll - 1);
2641 log_scroll_up(s, nscroll);
2649 if (s->first_displayed_entry == NULL)
2651 if (s->selected < MIN(view->nlines - 2,
2652 s->commits.ncommits - 1))
2655 err = log_scroll_down(view, 1);
2663 /* We don't know yet how many commits, so we're forced to
2664 * traverse them all. */
2665 if (!s->thread_args.log_complete) {
2666 s->thread_args.load_all = 1;
2667 return trigger_log_thread(view, 0);
2671 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2672 for (n = 0; n < view->nlines - 1; n++) {
2675 s->first_displayed_entry = entry;
2676 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2679 s->selected = n - 1;
2689 struct commit_queue_entry *first;
2690 first = s->first_displayed_entry;
2693 err = log_scroll_down(view, nscroll);
2696 if (first == s->first_displayed_entry &&
2697 s->selected < MIN(view->nlines - 2,
2698 s->commits.ncommits - 1)) {
2699 /* can't scroll further down */
2700 s->selected += MIN(s->last_displayed_entry->idx -
2701 s->selected_entry->idx, nscroll + 1);
2707 if (s->selected > view->nlines - 2)
2708 s->selected = view->nlines - 2;
2709 if (s->selected > s->commits.ncommits - 1)
2710 s->selected = s->commits.ncommits - 1;
2712 if (s->commits.ncommits < view->nlines - 1 &&
2713 !s->thread_args.log_complete) {
2714 s->thread_args.commits_needed += (view->nlines - 1) -
2715 s->commits.ncommits;
2716 err = trigger_log_thread(view, 1);
2722 if (s->selected_entry == NULL)
2724 if (view_is_parent_view(view))
2725 begin_x = view_split_begin_x(view->begin_x);
2726 err = open_diff_view_for_commit(&diff_view, begin_x,
2727 s->selected_entry->commit, s->selected_entry->id,
2732 diff_view->focussed = 1;
2733 if (view_is_parent_view(view)) {
2734 err = view_close_child(view);
2737 view_set_child(view, diff_view);
2738 view->focus_child = 1;
2740 *new_view = diff_view;
2743 if (s->selected_entry == NULL)
2745 if (view_is_parent_view(view))
2746 begin_x = view_split_begin_x(view->begin_x);
2747 err = browse_commit_tree(&tree_view, begin_x,
2748 s->selected_entry, s->in_repo_path, s->head_ref_name,
2753 tree_view->focussed = 1;
2754 if (view_is_parent_view(view)) {
2755 err = view_close_child(view);
2758 view_set_child(view, tree_view);
2759 view->focus_child = 1;
2761 *new_view = tree_view;
2766 if (ch == KEY_BACKSPACE &&
2767 got_path_is_root_dir(s->in_repo_path))
2769 err = stop_log_thread(s);
2772 if (ch == KEY_BACKSPACE) {
2774 err = got_path_dirname(&parent_path, s->in_repo_path);
2777 free(s->in_repo_path);
2778 s->in_repo_path = parent_path;
2779 s->thread_args.in_repo_path = s->in_repo_path;
2780 } else if (ch == CTRL('l')) {
2781 struct got_object_id *start_id;
2782 err = got_repo_match_object_id(&start_id, NULL,
2783 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2784 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2788 s->start_id = start_id;
2789 s->thread_args.start_id = s->start_id;
2791 s->log_branches = !s->log_branches;
2793 err = got_repo_open(&s->thread_args.repo,
2794 got_repo_get_path(s->repo), NULL,
2795 s->thread_args.pack_fds);
2799 err = tog_load_refs(s->repo, 0);
2802 err = got_commit_graph_open(&s->thread_args.graph,
2803 s->in_repo_path, !s->log_branches);
2806 err = got_commit_graph_iter_start(s->thread_args.graph,
2807 s->start_id, s->repo, NULL, NULL);
2810 free_commits(&s->commits);
2811 s->first_displayed_entry = NULL;
2812 s->last_displayed_entry = NULL;
2813 s->selected_entry = NULL;
2815 s->thread_args.log_complete = 0;
2817 s->thread_args.commits_needed = view->nlines;
2820 if (view_is_parent_view(view))
2821 begin_x = view_split_begin_x(view->begin_x);
2822 ref_view = view_open(view->nlines, view->ncols,
2823 view->begin_y, begin_x, TOG_VIEW_REF);
2824 if (ref_view == NULL)
2825 return got_error_from_errno("view_open");
2826 err = open_ref_view(ref_view, s->repo);
2828 view_close(ref_view);
2832 ref_view->focussed = 1;
2833 if (view_is_parent_view(view)) {
2834 err = view_close_child(view);
2837 view_set_child(view, ref_view);
2838 view->focus_child = 1;
2840 *new_view = ref_view;
2849 static const struct got_error *
2850 apply_unveil(const char *repo_path, const char *worktree_path)
2852 const struct got_error *error;
2855 if (unveil("gmon.out", "rwc") != 0)
2856 return got_error_from_errno2("unveil", "gmon.out");
2858 if (repo_path && unveil(repo_path, "r") != 0)
2859 return got_error_from_errno2("unveil", repo_path);
2861 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2862 return got_error_from_errno2("unveil", worktree_path);
2864 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2865 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2867 error = got_privsep_unveil_exec_helpers();
2871 if (unveil(NULL, NULL) != 0)
2872 return got_error_from_errno("unveil");
2881 * Override default signal handlers before starting ncurses.
2882 * This should prevent ncurses from installing its own
2883 * broken cleanup() signal handler.
2885 signal(SIGWINCH, tog_sigwinch);
2886 signal(SIGPIPE, tog_sigpipe);
2887 signal(SIGCONT, tog_sigcont);
2888 signal(SIGINT, tog_sigint);
2889 signal(SIGTERM, tog_sigterm);
2893 halfdelay(1); /* Do fast refresh while initial view is loading. */
2896 intrflush(stdscr, FALSE);
2897 keypad(stdscr, TRUE);
2899 if (getenv("TOG_COLORS") != NULL) {
2901 use_default_colors();
2905 static const struct got_error *
2906 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2907 struct got_repository *repo, struct got_worktree *worktree)
2909 const struct got_error *err = NULL;
2912 *in_repo_path = strdup("/");
2913 if (*in_repo_path == NULL)
2914 return got_error_from_errno("strdup");
2919 const char *prefix = got_worktree_get_path_prefix(worktree);
2922 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2925 if (asprintf(in_repo_path, "%s%s%s", prefix,
2926 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2928 err = got_error_from_errno("asprintf");
2929 *in_repo_path = NULL;
2933 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2938 static const struct got_error *
2939 cmd_log(int argc, char *argv[])
2941 const struct got_error *error;
2942 struct got_repository *repo = NULL;
2943 struct got_worktree *worktree = NULL;
2944 struct got_object_id *start_id = NULL;
2945 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2946 char *start_commit = NULL, *label = NULL;
2947 struct got_reference *ref = NULL;
2948 const char *head_ref_name = NULL;
2949 int ch, log_branches = 0;
2950 struct tog_view *view;
2951 int *pack_fds = NULL;
2953 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2959 start_commit = optarg;
2962 repo_path = realpath(optarg, NULL);
2963 if (repo_path == NULL)
2964 return got_error_from_errno2("realpath",
2979 error = got_repo_pack_fds_open(&pack_fds);
2983 if (repo_path == NULL) {
2984 cwd = getcwd(NULL, 0);
2986 return got_error_from_errno("getcwd");
2987 error = got_worktree_open(&worktree, cwd);
2988 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2992 strdup(got_worktree_get_repo_path(worktree));
2994 repo_path = strdup(cwd);
2995 if (repo_path == NULL) {
2996 error = got_error_from_errno("strdup");
3001 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3005 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3012 error = apply_unveil(got_repo_get_path(repo),
3013 worktree ? got_worktree_get_root_path(worktree) : NULL);
3017 /* already loaded by tog_log_with_path()? */
3018 if (TAILQ_EMPTY(&tog_refs)) {
3019 error = tog_load_refs(repo, 0);
3024 if (start_commit == NULL) {
3025 error = got_repo_match_object_id(&start_id, &label,
3026 worktree ? got_worktree_get_head_ref_name(worktree) :
3027 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3030 head_ref_name = label;
3032 error = got_ref_open(&ref, repo, start_commit, 0);
3034 head_ref_name = got_ref_get_name(ref);
3035 else if (error->code != GOT_ERR_NOT_REF)
3037 error = got_repo_match_object_id(&start_id, NULL,
3038 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3043 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3045 error = got_error_from_errno("view_open");
3048 error = open_log_view(view, start_id, repo, head_ref_name,
3049 in_repo_path, log_branches);
3053 /* Release work tree lock. */
3054 got_worktree_close(worktree);
3057 error = view_loop(view);
3067 const struct got_error *close_err = got_repo_close(repo);
3072 got_worktree_close(worktree);
3074 const struct got_error *pack_err =
3075 got_repo_pack_fds_close(pack_fds);
3087 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3088 "[-w] object1 object2\n", getprogname());
3093 match_line(const char *line, regex_t *regex, size_t nmatch,
3094 regmatch_t *regmatch)
3096 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3100 match_color(struct tog_colors *colors, const char *line)
3102 struct tog_color *tc = NULL;
3104 STAILQ_FOREACH(tc, colors, entry) {
3105 if (match_line(line, &tc->regex, 0, NULL))
3112 static const struct got_error *
3113 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3114 WINDOW *window, int skip, regmatch_t *regmatch)
3116 const struct got_error *err = NULL;
3118 int rme, rms, n, width;
3121 rms = regmatch->rm_so;
3122 rme = regmatch->rm_eo;
3124 err = format_line(&wline, &width, line, wlimit + skip,
3129 /* draw up to matched token if we haven't scrolled past it */
3130 n = MAX(rms - skip, 0);
3132 waddnwstr(window, wline + skip, n);
3138 int len = rme - rms;
3142 len = MAX(len - n, 0);
3144 /* draw (visible part of) matched token (if scrolled into it) */
3146 wattron(window, A_STANDOUT);
3147 waddnwstr(window, wline + rms + n, len);
3148 wattroff(window, A_STANDOUT);
3154 if (wlimit > 0 && skip < width) { /* draw rest of line */
3157 n = MIN(skip - rme, width - rme);
3158 waddnwstr(window, wline + rme + n, wlimit);
3166 static const struct got_error *
3167 draw_file(struct tog_view *view, const char *header)
3169 struct tog_diff_view_state *s = &view->state.diff;
3170 regmatch_t *regmatch = &view->regmatch;
3171 const struct got_error *err;
3174 size_t linesize = 0;
3176 struct tog_color *tc;
3179 int max_lines = view->nlines;
3180 int nlines = s->nlines;
3183 line_offset = s->line_offsets[s->first_displayed_line - 1];
3184 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3185 return got_error_from_errno("fseek");
3187 werase(view->window);
3190 if (asprintf(&line, "[%d/%d] %s",
3191 s->first_displayed_line - 1 + s->selected_line, nlines,
3193 return got_error_from_errno("asprintf");
3194 err = format_line(&wline, &width, line, view->ncols, 0, 0);
3199 if (view_needs_focus_indication(view))
3200 wstandout(view->window);
3201 waddwstr(view->window, wline);
3204 if (view_needs_focus_indication(view))
3205 wstandend(view->window);
3206 if (width <= view->ncols - 1)
3207 waddch(view->window, '\n');
3217 while (max_lines > 0 && nprinted < max_lines) {
3218 linelen = getline(&line, &linesize, s->f);
3219 if (linelen == -1) {
3225 return got_ferror(s->f, GOT_ERR_IO);
3228 view->maxx = MAX(view->maxx, linelen);
3230 tc = match_color(&s->colors, line);
3232 wattr_on(view->window,
3233 COLOR_PAIR(tc->colorpair), NULL);
3234 if (s->first_displayed_line + nprinted == s->matched_line &&
3235 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3236 err = add_matched_line(&width, line, view->ncols, 0,
3237 view->window, view->x, regmatch);
3243 err = format_line(&wline, &width, line,
3244 view->x + view->ncols, 0, view->x ? 1 : 0);
3249 if (view->x < width - 1)
3250 waddwstr(view->window, wline + view->x);
3255 wattr_off(view->window,
3256 COLOR_PAIR(tc->colorpair), NULL);
3257 if (width - view->x <= view->ncols - 1)
3258 waddch(view->window, '\n');
3263 s->last_displayed_line = s->first_displayed_line +
3266 s->last_displayed_line = s->first_displayed_line;
3271 while (nprinted < view->nlines) {
3272 waddch(view->window, '\n');
3276 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols,
3282 wstandout(view->window);
3283 waddwstr(view->window, wline);
3286 wstandend(view->window);
3293 get_datestr(time_t *time, char *datebuf)
3295 struct tm mytm, *tm;
3298 tm = gmtime_r(time, &mytm);
3301 s = asctime_r(tm, datebuf);
3304 p = strchr(s, '\n');
3310 static const struct got_error *
3311 get_changed_paths(struct got_pathlist_head *paths,
3312 struct got_commit_object *commit, struct got_repository *repo)
3314 const struct got_error *err = NULL;
3315 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3316 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3317 struct got_object_qid *qid;
3319 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3321 struct got_commit_object *pcommit;
3322 err = got_object_open_as_commit(&pcommit, repo,
3327 tree_id1 = got_object_id_dup(
3328 got_object_commit_get_tree_id(pcommit));
3329 if (tree_id1 == NULL) {
3330 got_object_commit_close(pcommit);
3331 return got_error_from_errno("got_object_id_dup");
3333 got_object_commit_close(pcommit);
3338 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3343 tree_id2 = got_object_commit_get_tree_id(commit);
3344 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3348 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3349 got_diff_tree_collect_changed_paths, paths, 0);
3352 got_object_tree_close(tree1);
3354 got_object_tree_close(tree2);
3359 static const struct got_error *
3360 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3364 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3366 return got_error_from_errno("reallocarray");
3368 (*line_offsets)[*nlines] = off;
3373 static const struct got_error *
3374 write_commit_info(off_t **line_offsets, size_t *nlines,
3375 struct got_object_id *commit_id, struct got_reflist_head *refs,
3376 struct got_repository *repo, FILE *outfile)
3378 const struct got_error *err = NULL;
3379 char datebuf[26], *datestr;
3380 struct got_commit_object *commit;
3381 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3382 time_t committer_time;
3383 const char *author, *committer;
3384 char *refs_str = NULL;
3385 struct got_pathlist_head changed_paths;
3386 struct got_pathlist_entry *pe;
3390 TAILQ_INIT(&changed_paths);
3393 err = build_refs_str(&refs_str, refs, commit_id, repo);
3398 err = got_object_open_as_commit(&commit, repo, commit_id);
3402 err = got_object_id_str(&id_str, commit_id);
3404 err = got_error_from_errno("got_object_id_str");
3408 err = add_line_offset(line_offsets, nlines, 0);
3412 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3413 refs_str ? refs_str : "", refs_str ? ")" : "");
3415 err = got_error_from_errno("fprintf");
3419 err = add_line_offset(line_offsets, nlines, outoff);
3423 n = fprintf(outfile, "from: %s\n",
3424 got_object_commit_get_author(commit));
3426 err = got_error_from_errno("fprintf");
3430 err = add_line_offset(line_offsets, nlines, outoff);
3434 committer_time = got_object_commit_get_committer_time(commit);
3435 datestr = get_datestr(&committer_time, datebuf);
3437 n = fprintf(outfile, "date: %s UTC\n", datestr);
3439 err = got_error_from_errno("fprintf");
3443 err = add_line_offset(line_offsets, nlines, outoff);
3447 author = got_object_commit_get_author(commit);
3448 committer = got_object_commit_get_committer(commit);
3449 if (strcmp(author, committer) != 0) {
3450 n = fprintf(outfile, "via: %s\n", committer);
3452 err = got_error_from_errno("fprintf");
3456 err = add_line_offset(line_offsets, nlines, outoff);
3460 if (got_object_commit_get_nparents(commit) > 1) {
3461 const struct got_object_id_queue *parent_ids;
3462 struct got_object_qid *qid;
3464 parent_ids = got_object_commit_get_parent_ids(commit);
3465 STAILQ_FOREACH(qid, parent_ids, entry) {
3466 err = got_object_id_str(&id_str, &qid->id);
3469 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3471 err = got_error_from_errno("fprintf");
3475 err = add_line_offset(line_offsets, nlines, outoff);
3483 err = got_object_commit_get_logmsg(&logmsg, commit);
3487 while ((line = strsep(&s, "\n")) != NULL) {
3488 n = fprintf(outfile, "%s\n", line);
3490 err = got_error_from_errno("fprintf");
3494 err = add_line_offset(line_offsets, nlines, outoff);
3499 err = get_changed_paths(&changed_paths, commit, repo);
3502 TAILQ_FOREACH(pe, &changed_paths, entry) {
3503 struct got_diff_changed_path *cp = pe->data;
3504 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3506 err = got_error_from_errno("fprintf");
3510 err = add_line_offset(line_offsets, nlines, outoff);
3513 free((char *)pe->path);
3517 fputc('\n', outfile);
3519 err = add_line_offset(line_offsets, nlines, outoff);
3521 got_pathlist_free(&changed_paths);
3525 got_object_commit_close(commit);
3527 free(*line_offsets);
3528 *line_offsets = NULL;
3534 static const struct got_error *
3535 create_diff(struct tog_diff_view_state *s)
3537 const struct got_error *err = NULL;
3541 free(s->line_offsets);
3542 s->line_offsets = malloc(sizeof(off_t));
3543 if (s->line_offsets == NULL)
3544 return got_error_from_errno("malloc");
3549 err = got_error_from_errno("got_opentemp");
3552 if (s->f && fclose(s->f) == EOF) {
3553 err = got_error_from_errno("fclose");
3559 err = got_object_get_type(&obj_type, s->repo, s->id1);
3561 err = got_object_get_type(&obj_type, s->repo, s->id2);
3566 case GOT_OBJ_TYPE_BLOB:
3567 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3568 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3569 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3572 case GOT_OBJ_TYPE_TREE:
3573 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3574 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3575 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3577 case GOT_OBJ_TYPE_COMMIT: {
3578 const struct got_object_id_queue *parent_ids;
3579 struct got_object_qid *pid;
3580 struct got_commit_object *commit2;
3581 struct got_reflist_head *refs;
3583 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3586 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3587 /* Show commit info if we're diffing to a parent/root commit. */
3588 if (s->id1 == NULL) {
3589 err = write_commit_info(&s->line_offsets, &s->nlines,
3590 s->id2, refs, s->repo, s->f);
3594 parent_ids = got_object_commit_get_parent_ids(commit2);
3595 STAILQ_FOREACH(pid, parent_ids, entry) {
3596 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3597 err = write_commit_info(
3598 &s->line_offsets, &s->nlines,
3599 s->id2, refs, s->repo, s->f);
3606 got_object_commit_close(commit2);
3608 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3609 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3610 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3614 err = got_error(GOT_ERR_OBJ_TYPE);
3620 if (s->f && fflush(s->f) != 0 && err == NULL)
3621 err = got_error_from_errno("fflush");
3626 diff_view_indicate_progress(struct tog_view *view)
3628 mvwaddstr(view->window, 0, 0, "diffing...");
3633 static const struct got_error *
3634 search_start_diff_view(struct tog_view *view)
3636 struct tog_diff_view_state *s = &view->state.diff;
3638 s->matched_line = 0;
3642 static const struct got_error *
3643 search_next_diff_view(struct tog_view *view)
3645 struct tog_diff_view_state *s = &view->state.diff;
3646 const struct got_error *err = NULL;
3648 char *exstr = NULL, *line = NULL;
3649 size_t linesize = 0;
3652 if (!view->searching) {
3653 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3657 if (s->matched_line) {
3658 if (view->searching == TOG_SEARCH_FORWARD)
3659 lineno = s->matched_line + 1;
3661 lineno = s->matched_line - 1;
3663 lineno = s->first_displayed_line;
3668 if (lineno <= 0 || lineno > s->nlines) {
3669 if (s->matched_line == 0) {
3670 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3674 if (view->searching == TOG_SEARCH_FORWARD)
3680 offset = s->line_offsets[lineno - 1];
3681 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3683 return got_error_from_errno("fseeko");
3685 linelen = getline(&line, &linesize, s->f);
3686 err = expand_tab(&exstr, line);
3689 if (linelen != -1 &&
3690 match_line(exstr, &view->regex, 1, &view->regmatch)) {
3691 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3692 s->matched_line = lineno;
3697 if (view->searching == TOG_SEARCH_FORWARD)
3705 if (s->matched_line) {
3706 s->first_displayed_line = s->matched_line;
3707 s->selected_line = 1;
3713 static const struct got_error *
3714 close_diff_view(struct tog_view *view)
3716 const struct got_error *err = NULL;
3717 struct tog_diff_view_state *s = &view->state.diff;
3723 if (s->f && fclose(s->f) == EOF)
3724 err = got_error_from_errno("fclose");
3726 if (s->f1 && fclose(s->f1) == EOF)
3727 err = got_error_from_errno("fclose");
3729 if (s->f2 && fclose(s->f2) == EOF)
3730 err = got_error_from_errno("fclose");
3732 free_colors(&s->colors);
3733 free(s->line_offsets);
3734 s->line_offsets = NULL;
3739 static const struct got_error *
3740 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3741 struct got_object_id *id2, const char *label1, const char *label2,
3742 int diff_context, int ignore_whitespace, int force_text_diff,
3743 struct tog_view *log_view, struct got_repository *repo)
3745 const struct got_error *err;
3746 struct tog_diff_view_state *s = &view->state.diff;
3748 memset(s, 0, sizeof(*s));
3750 if (id1 != NULL && id2 != NULL) {
3752 err = got_object_get_type(&type1, repo, id1);
3755 err = got_object_get_type(&type2, repo, id2);
3760 return got_error(GOT_ERR_OBJ_TYPE);
3762 s->first_displayed_line = 1;
3763 s->last_displayed_line = view->nlines;
3764 s->selected_line = 1;
3772 s->id1 = got_object_id_dup(id1);
3774 return got_error_from_errno("got_object_id_dup");
3775 s->f1 = got_opentemp();
3776 if (s->f1 == NULL) {
3777 err = got_error_from_errno("got_opentemp");
3783 s->id2 = got_object_id_dup(id2);
3784 if (s->id2 == NULL) {
3785 err = got_error_from_errno("got_object_id_dup");
3789 s->f2 = got_opentemp();
3790 if (s->f2 == NULL) {
3791 err = got_error_from_errno("got_opentemp");
3795 s->first_displayed_line = 1;
3796 s->last_displayed_line = view->nlines;
3797 s->diff_context = diff_context;
3798 s->ignore_whitespace = ignore_whitespace;
3799 s->force_text_diff = force_text_diff;
3800 s->log_view = log_view;
3803 STAILQ_INIT(&s->colors);
3804 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3805 err = add_color(&s->colors,
3806 "^-", TOG_COLOR_DIFF_MINUS,
3807 get_color_value("TOG_COLOR_DIFF_MINUS"));
3810 err = add_color(&s->colors, "^\\+",
3811 TOG_COLOR_DIFF_PLUS,
3812 get_color_value("TOG_COLOR_DIFF_PLUS"));
3815 err = add_color(&s->colors,
3816 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3817 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3821 err = add_color(&s->colors,
3822 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3823 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3824 get_color_value("TOG_COLOR_DIFF_META"));
3828 err = add_color(&s->colors,
3829 "^(from|via): ", TOG_COLOR_AUTHOR,
3830 get_color_value("TOG_COLOR_AUTHOR"));
3834 err = add_color(&s->colors,
3835 "^date: ", TOG_COLOR_DATE,
3836 get_color_value("TOG_COLOR_DATE"));
3841 if (log_view && view_is_splitscreen(view))
3842 show_log_view(log_view); /* draw vborder */
3843 diff_view_indicate_progress(view);
3845 err = create_diff(s);
3847 view->show = show_diff_view;
3848 view->input = input_diff_view;
3849 view->close = close_diff_view;
3850 view->search_start = search_start_diff_view;
3851 view->search_next = search_next_diff_view;
3854 close_diff_view(view);
3858 static const struct got_error *
3859 show_diff_view(struct tog_view *view)
3861 const struct got_error *err;
3862 struct tog_diff_view_state *s = &view->state.diff;
3863 char *id_str1 = NULL, *id_str2, *header;
3864 const char *label1, *label2;
3867 err = got_object_id_str(&id_str1, s->id1);
3870 label1 = s->label1 ? : id_str1;
3872 label1 = "/dev/null";
3874 err = got_object_id_str(&id_str2, s->id2);
3877 label2 = s->label2 ? : id_str2;
3879 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3880 err = got_error_from_errno("asprintf");
3888 err = draw_file(view, header);
3893 static const struct got_error *
3894 set_selected_commit(struct tog_diff_view_state *s,
3895 struct commit_queue_entry *entry)
3897 const struct got_error *err;
3898 const struct got_object_id_queue *parent_ids;
3899 struct got_commit_object *selected_commit;
3900 struct got_object_qid *pid;
3903 s->id2 = got_object_id_dup(entry->id);
3905 return got_error_from_errno("got_object_id_dup");
3907 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3910 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3912 pid = STAILQ_FIRST(parent_ids);
3913 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3914 got_object_commit_close(selected_commit);
3918 static const struct got_error *
3919 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3921 const struct got_error *err = NULL;
3922 struct tog_diff_view_state *s = &view->state.diff;
3923 struct tog_log_view_state *ls;
3924 struct commit_queue_entry *old_selected_entry;
3926 size_t linesize = 0;
3928 int i, nscroll = view->nlines - 1;
3935 view->x = MAX(view->maxx - view->ncols / 3, 0);
3939 if (view->x + view->ncols / 3 < view->maxx)
3940 view->x += 2; /* move two columns right */
3944 view->x -= MIN(view->x, 2); /* move two columns back */
3949 s->force_text_diff = !s->force_text_diff;
3951 s->ignore_whitespace = !s->ignore_whitespace;
3952 wclear(view->window);
3953 s->first_displayed_line = 1;
3954 s->last_displayed_line = view->nlines;
3955 s->matched_line = 0;
3956 diff_view_indicate_progress(view);
3957 err = create_diff(s);
3961 s->first_displayed_line = 1;
3968 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3974 if (s->first_displayed_line > 1)
3975 s->first_displayed_line--;
3983 if (s->first_displayed_line == 1)
3986 while (i++ < nscroll && s->first_displayed_line > 1)
3987 s->first_displayed_line--;
3993 s->first_displayed_line++;
4005 while (!s->eof && i++ < nscroll) {
4006 linelen = getline(&line, &linesize, s->f);
4007 s->first_displayed_line++;
4008 if (linelen == -1) {
4012 err = got_ferror(s->f, GOT_ERR_IO);
4019 if (s->diff_context > 0) {
4021 s->matched_line = 0;
4022 diff_view_indicate_progress(view);
4023 err = create_diff(s);
4024 if (s->first_displayed_line + view->nlines - 1 >
4026 s->first_displayed_line = 1;
4027 s->last_displayed_line = view->nlines;
4032 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4034 s->matched_line = 0;
4035 diff_view_indicate_progress(view);
4036 err = create_diff(s);
4041 if (s->log_view == NULL)
4043 ls = &s->log_view->state.log;
4044 old_selected_entry = ls->selected_entry;
4046 err = input_log_view(NULL, s->log_view, KEY_UP);
4050 if (old_selected_entry == ls->selected_entry)
4053 err = set_selected_commit(s, ls->selected_entry);
4057 s->first_displayed_line = 1;
4058 s->last_displayed_line = view->nlines;
4059 s->matched_line = 0;
4062 diff_view_indicate_progress(view);
4063 err = create_diff(s);
4067 if (s->log_view == NULL)
4069 ls = &s->log_view->state.log;
4070 old_selected_entry = ls->selected_entry;
4072 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4076 if (old_selected_entry == ls->selected_entry)
4079 err = set_selected_commit(s, ls->selected_entry);
4083 s->first_displayed_line = 1;
4084 s->last_displayed_line = view->nlines;
4085 s->matched_line = 0;
4088 diff_view_indicate_progress(view);
4089 err = create_diff(s);
4098 static const struct got_error *
4099 cmd_diff(int argc, char *argv[])
4101 const struct got_error *error = NULL;
4102 struct got_repository *repo = NULL;
4103 struct got_worktree *worktree = NULL;
4104 struct got_object_id *id1 = NULL, *id2 = NULL;
4105 char *repo_path = NULL, *cwd = NULL;
4106 char *id_str1 = NULL, *id_str2 = NULL;
4107 char *label1 = NULL, *label2 = NULL;
4108 int diff_context = 3, ignore_whitespace = 0;
4109 int ch, force_text_diff = 0;
4111 struct tog_view *view;
4112 int *pack_fds = NULL;
4114 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4117 force_text_diff = 1;
4120 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4123 errx(1, "number of context lines is %s: %s",
4127 repo_path = realpath(optarg, NULL);
4128 if (repo_path == NULL)
4129 return got_error_from_errno2("realpath",
4131 got_path_strip_trailing_slashes(repo_path);
4134 ignore_whitespace = 1;
4146 usage_diff(); /* TODO show local worktree changes */
4147 } else if (argc == 2) {
4153 error = got_repo_pack_fds_open(&pack_fds);
4157 if (repo_path == NULL) {
4158 cwd = getcwd(NULL, 0);
4160 return got_error_from_errno("getcwd");
4161 error = got_worktree_open(&worktree, cwd);
4162 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4166 strdup(got_worktree_get_repo_path(worktree));
4168 repo_path = strdup(cwd);
4169 if (repo_path == NULL) {
4170 error = got_error_from_errno("strdup");
4175 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4181 error = apply_unveil(got_repo_get_path(repo), NULL);
4185 error = tog_load_refs(repo, 0);
4189 error = got_repo_match_object_id(&id1, &label1, id_str1,
4190 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4194 error = got_repo_match_object_id(&id2, &label2, id_str2,
4195 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4199 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4201 error = got_error_from_errno("view_open");
4204 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4205 ignore_whitespace, force_text_diff, NULL, repo);
4208 error = view_loop(view);
4215 const struct got_error *close_err = got_repo_close(repo);
4220 got_worktree_close(worktree);
4222 const struct got_error *pack_err =
4223 got_repo_pack_fds_close(pack_fds);
4235 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4240 struct tog_blame_line {
4242 struct got_object_id *id;
4245 static const struct got_error *
4246 draw_blame(struct tog_view *view)
4248 struct tog_blame_view_state *s = &view->state.blame;
4249 struct tog_blame *blame = &s->blame;
4250 regmatch_t *regmatch = &view->regmatch;
4251 const struct got_error *err;
4252 int lineno = 0, nprinted = 0, i;
4254 size_t linesize = 0;
4258 struct tog_blame_line *blame_line;
4259 struct got_object_id *prev_id = NULL;
4261 struct tog_color *tc;
4263 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4268 werase(view->window);
4270 if (asprintf(&line, "commit %s", id_str) == -1) {
4271 err = got_error_from_errno("asprintf");
4276 err = format_line(&wline, &width, line, view->ncols, 0, 0);
4281 if (view_needs_focus_indication(view))
4282 wstandout(view->window);
4283 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4285 wattr_on(view->window,
4286 COLOR_PAIR(tc->colorpair), NULL);
4287 waddwstr(view->window, wline);
4289 wattr_off(view->window,
4290 COLOR_PAIR(tc->colorpair), NULL);
4291 if (view_needs_focus_indication(view))
4292 wstandend(view->window);
4295 if (width < view->ncols - 1)
4296 waddch(view->window, '\n');
4298 if (asprintf(&line, "[%d/%d] %s%s",
4299 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4300 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4302 return got_error_from_errno("asprintf");
4305 err = format_line(&wline, &width, line, view->ncols, 0, 0);
4310 waddwstr(view->window, wline);
4313 if (width < view->ncols - 1)
4314 waddch(view->window, '\n');
4318 while (nprinted < view->nlines - 2) {
4319 linelen = getline(&line, &linesize, blame->f);
4320 if (linelen == -1) {
4321 if (feof(blame->f)) {
4326 return got_ferror(blame->f, GOT_ERR_IO);
4328 if (++lineno < s->first_displayed_line)
4331 view->maxx = MAX(view->maxx, linelen);
4333 if (view->focussed && nprinted == s->selected_line - 1)
4334 wstandout(view->window);
4336 if (blame->nlines > 0) {
4337 blame_line = &blame->lines[lineno - 1];
4338 if (blame_line->annotated && prev_id &&
4339 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4341 nprinted == s->selected_line - 1)) {
4342 waddstr(view->window, " ");
4343 } else if (blame_line->annotated) {
4345 err = got_object_id_str(&id_str, blame_line->id);
4350 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4352 wattr_on(view->window,
4353 COLOR_PAIR(tc->colorpair), NULL);
4354 wprintw(view->window, "%.8s", id_str);
4356 wattr_off(view->window,
4357 COLOR_PAIR(tc->colorpair), NULL);
4359 prev_id = blame_line->id;
4361 waddstr(view->window, "........");
4365 waddstr(view->window, "........");
4369 if (view->focussed && nprinted == s->selected_line - 1)
4370 wstandend(view->window);
4371 waddstr(view->window, " ");
4373 if (view->ncols <= 9) {
4375 wline = wcsdup(L"");
4376 if (wline == NULL) {
4377 err = got_error_from_errno("wcsdup");
4381 } else if (s->first_displayed_line + nprinted ==
4383 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4384 err = add_matched_line(&width, line, view->ncols - 9, 9,
4385 view->window, view->x, regmatch);
4392 err = format_line(&wline, &width, line,
4393 view->x + view->ncols - 9, 9, 1);
4398 if (view->x < width) {
4399 waddwstr(view->window, wline + view->x);
4400 for (i = 0; i < view->x; i++)
4401 width -= wcwidth(wline[i]);
4408 if (width <= view->ncols - 1)
4409 waddch(view->window, '\n');
4410 if (++nprinted == 1)
4411 s->first_displayed_line = lineno;
4414 s->last_displayed_line = lineno;
4421 static const struct got_error *
4422 blame_cb(void *arg, int nlines, int lineno,
4423 struct got_commit_object *commit, struct got_object_id *id)
4425 const struct got_error *err = NULL;
4426 struct tog_blame_cb_args *a = arg;
4427 struct tog_blame_line *line;
4430 if (nlines != a->nlines ||
4431 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4432 return got_error(GOT_ERR_RANGE);
4434 errcode = pthread_mutex_lock(&tog_mutex);
4436 return got_error_set_errno(errcode, "pthread_mutex_lock");
4438 if (*a->quit) { /* user has quit the blame view */
4439 err = got_error(GOT_ERR_ITER_COMPLETED);
4444 goto done; /* no change in this commit */
4446 line = &a->lines[lineno - 1];
4447 if (line->annotated)
4450 line->id = got_object_id_dup(id);
4451 if (line->id == NULL) {
4452 err = got_error_from_errno("got_object_id_dup");
4455 line->annotated = 1;
4457 errcode = pthread_mutex_unlock(&tog_mutex);
4459 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4464 blame_thread(void *arg)
4466 const struct got_error *err, *close_err;
4467 struct tog_blame_thread_args *ta = arg;
4468 struct tog_blame_cb_args *a = ta->cb_args;
4471 err = block_signals_used_by_main_thread();
4475 err = got_blame(ta->path, a->commit_id, ta->repo,
4476 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4477 if (err && err->code == GOT_ERR_CANCELLED)
4480 errcode = pthread_mutex_lock(&tog_mutex);
4482 return (void *)got_error_set_errno(errcode,
4483 "pthread_mutex_lock");
4485 close_err = got_repo_close(ta->repo);
4491 errcode = pthread_mutex_unlock(&tog_mutex);
4492 if (errcode && err == NULL)
4493 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4498 static struct got_object_id *
4499 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4500 int first_displayed_line, int selected_line)
4502 struct tog_blame_line *line;
4507 line = &lines[first_displayed_line - 1 + selected_line - 1];
4508 if (!line->annotated)
4514 static const struct got_error *
4515 stop_blame(struct tog_blame *blame)
4517 const struct got_error *err = NULL;
4520 if (blame->thread) {
4522 errcode = pthread_mutex_unlock(&tog_mutex);
4524 return got_error_set_errno(errcode,
4525 "pthread_mutex_unlock");
4526 errcode = pthread_join(blame->thread, (void **)&err);
4528 return got_error_set_errno(errcode, "pthread_join");
4529 errcode = pthread_mutex_lock(&tog_mutex);
4531 return got_error_set_errno(errcode,
4532 "pthread_mutex_lock");
4533 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4535 blame->thread = 0; //NULL;
4537 if (blame->thread_args.repo) {
4538 const struct got_error *close_err;
4539 close_err = got_repo_close(blame->thread_args.repo);
4542 blame->thread_args.repo = NULL;
4545 if (fclose(blame->f) == EOF && err == NULL)
4546 err = got_error_from_errno("fclose");
4550 for (i = 0; i < blame->nlines; i++)
4551 free(blame->lines[i].id);
4553 blame->lines = NULL;
4555 free(blame->cb_args.commit_id);
4556 blame->cb_args.commit_id = NULL;
4557 if (blame->pack_fds) {
4558 const struct got_error *pack_err =
4559 got_repo_pack_fds_close(blame->pack_fds);
4562 blame->pack_fds = NULL;
4567 static const struct got_error *
4568 cancel_blame_view(void *arg)
4570 const struct got_error *err = NULL;
4574 errcode = pthread_mutex_lock(&tog_mutex);
4576 return got_error_set_errno(errcode,
4577 "pthread_mutex_unlock");
4580 err = got_error(GOT_ERR_CANCELLED);
4582 errcode = pthread_mutex_unlock(&tog_mutex);
4584 return got_error_set_errno(errcode,
4585 "pthread_mutex_lock");
4590 static const struct got_error *
4591 run_blame(struct tog_view *view)
4593 struct tog_blame_view_state *s = &view->state.blame;
4594 struct tog_blame *blame = &s->blame;
4595 const struct got_error *err = NULL;
4596 struct got_commit_object *commit = NULL;
4597 struct got_blob_object *blob = NULL;
4598 struct got_repository *thread_repo = NULL;
4599 struct got_object_id *obj_id = NULL;
4601 int *pack_fds = NULL;
4603 err = got_object_open_as_commit(&commit, s->repo,
4604 &s->blamed_commit->id);
4608 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4612 err = got_object_get_type(&obj_type, s->repo, obj_id);
4616 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4617 err = got_error(GOT_ERR_OBJ_TYPE);
4621 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4624 blame->f = got_opentemp();
4625 if (blame->f == NULL) {
4626 err = got_error_from_errno("got_opentemp");
4629 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4630 &blame->line_offsets, blame->f, blob);
4633 if (blame->nlines == 0) {
4634 s->blame_complete = 1;
4638 /* Don't include \n at EOF in the blame line count. */
4639 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4642 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4643 if (blame->lines == NULL) {
4644 err = got_error_from_errno("calloc");
4648 err = got_repo_pack_fds_open(&pack_fds);
4651 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4656 blame->pack_fds = pack_fds;
4657 blame->cb_args.view = view;
4658 blame->cb_args.lines = blame->lines;
4659 blame->cb_args.nlines = blame->nlines;
4660 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4661 if (blame->cb_args.commit_id == NULL) {
4662 err = got_error_from_errno("got_object_id_dup");
4665 blame->cb_args.quit = &s->done;
4667 blame->thread_args.path = s->path;
4668 blame->thread_args.repo = thread_repo;
4669 blame->thread_args.cb_args = &blame->cb_args;
4670 blame->thread_args.complete = &s->blame_complete;
4671 blame->thread_args.cancel_cb = cancel_blame_view;
4672 blame->thread_args.cancel_arg = &s->done;
4673 s->blame_complete = 0;
4675 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4676 s->first_displayed_line = 1;
4677 s->last_displayed_line = view->nlines;
4678 s->selected_line = 1;
4680 s->matched_line = 0;
4684 got_object_commit_close(commit);
4686 got_object_blob_close(blob);
4693 static const struct got_error *
4694 open_blame_view(struct tog_view *view, char *path,
4695 struct got_object_id *commit_id, struct got_repository *repo)
4697 const struct got_error *err = NULL;
4698 struct tog_blame_view_state *s = &view->state.blame;
4700 STAILQ_INIT(&s->blamed_commits);
4702 s->path = strdup(path);
4703 if (s->path == NULL)
4704 return got_error_from_errno("strdup");
4706 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4712 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4713 s->first_displayed_line = 1;
4714 s->last_displayed_line = view->nlines;
4715 s->selected_line = 1;
4716 s->blame_complete = 0;
4718 s->commit_id = commit_id;
4719 memset(&s->blame, 0, sizeof(s->blame));
4721 STAILQ_INIT(&s->colors);
4722 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4723 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4724 get_color_value("TOG_COLOR_COMMIT"));
4729 view->show = show_blame_view;
4730 view->input = input_blame_view;
4731 view->close = close_blame_view;
4732 view->search_start = search_start_blame_view;
4733 view->search_next = search_next_blame_view;
4735 return run_blame(view);
4738 static const struct got_error *
4739 close_blame_view(struct tog_view *view)
4741 const struct got_error *err = NULL;
4742 struct tog_blame_view_state *s = &view->state.blame;
4744 if (s->blame.thread)
4745 err = stop_blame(&s->blame);
4747 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4748 struct got_object_qid *blamed_commit;
4749 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4750 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4751 got_object_qid_free(blamed_commit);
4755 free_colors(&s->colors);
4759 static const struct got_error *
4760 search_start_blame_view(struct tog_view *view)
4762 struct tog_blame_view_state *s = &view->state.blame;
4764 s->matched_line = 0;
4768 static const struct got_error *
4769 search_next_blame_view(struct tog_view *view)
4771 struct tog_blame_view_state *s = &view->state.blame;
4772 const struct got_error *err = NULL;
4774 char *exstr = NULL, *line = NULL;
4775 size_t linesize = 0;
4778 if (!view->searching) {
4779 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4783 if (s->matched_line) {
4784 if (view->searching == TOG_SEARCH_FORWARD)
4785 lineno = s->matched_line + 1;
4787 lineno = s->matched_line - 1;
4789 lineno = s->first_displayed_line - 1 + s->selected_line;
4794 if (lineno <= 0 || lineno > s->blame.nlines) {
4795 if (s->matched_line == 0) {
4796 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4800 if (view->searching == TOG_SEARCH_FORWARD)
4803 lineno = s->blame.nlines;
4806 offset = s->blame.line_offsets[lineno - 1];
4807 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4809 return got_error_from_errno("fseeko");
4811 linelen = getline(&line, &linesize, s->blame.f);
4812 err = expand_tab(&exstr, line);
4815 if (linelen != -1 &&
4816 match_line(exstr, &view->regex, 1, &view->regmatch)) {
4817 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4818 s->matched_line = lineno;
4823 if (view->searching == TOG_SEARCH_FORWARD)
4831 if (s->matched_line) {
4832 s->first_displayed_line = s->matched_line;
4833 s->selected_line = 1;
4839 static const struct got_error *
4840 show_blame_view(struct tog_view *view)
4842 const struct got_error *err = NULL;
4843 struct tog_blame_view_state *s = &view->state.blame;
4846 if (s->blame.thread == 0 && !s->blame_complete) {
4847 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4848 &s->blame.thread_args);
4850 return got_error_set_errno(errcode, "pthread_create");
4852 halfdelay(1); /* fast refresh while annotating */
4855 if (s->blame_complete)
4856 halfdelay(10); /* disable fast refresh */
4858 err = draw_blame(view);
4864 static const struct got_error *
4865 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4867 const struct got_error *err = NULL, *thread_err = NULL;
4868 struct tog_view *diff_view;
4869 struct tog_blame_view_state *s = &view->state.blame;
4870 int begin_x = 0, nscroll = view->nlines - 2;
4877 view->x = MAX(view->maxx - view->ncols / 3, 0);
4881 if (view->x + view->ncols / 3 < view->maxx)
4882 view->x += 2; /* move two columns right */
4886 view->x -= MIN(view->x, 2); /* move two columns back */
4893 s->selected_line = 1;
4894 s->first_displayed_line = 1;
4898 if (s->blame.nlines < view->nlines - 2) {
4899 s->selected_line = s->blame.nlines;
4900 s->first_displayed_line = 1;
4902 s->selected_line = view->nlines - 2;
4903 s->first_displayed_line = s->blame.nlines -
4910 if (s->selected_line > 1)
4912 else if (s->selected_line == 1 &&
4913 s->first_displayed_line > 1)
4914 s->first_displayed_line--;
4922 if (s->first_displayed_line == 1) {
4923 s->selected_line = MAX(1, s->selected_line - nscroll);
4926 if (s->first_displayed_line > nscroll)
4927 s->first_displayed_line -= nscroll;
4929 s->first_displayed_line = 1;
4934 if (s->selected_line < view->nlines - 2 &&
4935 s->first_displayed_line +
4936 s->selected_line <= s->blame.nlines)
4938 else if (s->last_displayed_line <
4940 s->first_displayed_line++;
4944 struct got_object_id *id = NULL;
4945 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4946 s->first_displayed_line, s->selected_line);
4950 struct got_commit_object *commit, *pcommit;
4951 struct got_object_qid *pid;
4952 struct got_object_id *blob_id = NULL;
4954 err = got_object_open_as_commit(&commit,
4959 got_object_commit_get_parent_ids(commit));
4961 got_object_commit_close(commit);
4964 /* Check if path history ends here. */
4965 err = got_object_open_as_commit(&pcommit,
4969 err = got_object_id_by_path(&blob_id, s->repo,
4971 got_object_commit_close(pcommit);
4973 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4975 got_object_commit_close(commit);
4978 err = got_object_get_type(&obj_type, s->repo,
4981 /* Can't blame non-blob type objects. */
4982 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4983 got_object_commit_close(commit);
4986 err = got_object_qid_alloc(&s->blamed_commit,
4988 got_object_commit_close(commit);
4990 if (got_object_id_cmp(id,
4991 &s->blamed_commit->id) == 0)
4993 err = got_object_qid_alloc(&s->blamed_commit,
4999 thread_err = stop_blame(&s->blame);
5003 STAILQ_INSERT_HEAD(&s->blamed_commits,
5004 s->blamed_commit, entry);
5005 err = run_blame(view);
5011 struct got_object_qid *first;
5012 first = STAILQ_FIRST(&s->blamed_commits);
5013 if (!got_object_id_cmp(&first->id, s->commit_id))
5016 thread_err = stop_blame(&s->blame);
5020 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5021 got_object_qid_free(s->blamed_commit);
5023 STAILQ_FIRST(&s->blamed_commits);
5024 err = run_blame(view);
5031 struct got_object_id *id = NULL;
5032 struct got_object_qid *pid;
5033 struct got_commit_object *commit = NULL;
5034 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5035 s->first_displayed_line, s->selected_line);
5038 err = got_object_open_as_commit(&commit, s->repo, id);
5042 got_object_commit_get_parent_ids(commit));
5043 if (view_is_parent_view(view))
5044 begin_x = view_split_begin_x(view->begin_x);
5045 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5046 if (diff_view == NULL) {
5047 got_object_commit_close(commit);
5048 err = got_error_from_errno("view_open");
5051 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5052 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5053 got_object_commit_close(commit);
5055 view_close(diff_view);
5059 diff_view->focussed = 1;
5060 if (view_is_parent_view(view)) {
5061 err = view_close_child(view);
5064 view_set_child(view, diff_view);
5065 view->focus_child = 1;
5067 *new_view = diff_view;
5079 if (s->last_displayed_line >= s->blame.nlines &&
5080 s->selected_line >= MIN(s->blame.nlines,
5081 view->nlines - 2)) {
5084 if (s->last_displayed_line >= s->blame.nlines &&
5085 s->selected_line < view->nlines - 2) {
5087 MIN(nscroll, s->last_displayed_line -
5088 s->first_displayed_line - s->selected_line + 1);
5090 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5091 s->first_displayed_line += nscroll;
5093 s->first_displayed_line =
5094 s->blame.nlines - (view->nlines - 3);
5097 if (s->selected_line > view->nlines - 2) {
5098 s->selected_line = MIN(s->blame.nlines,
5105 return thread_err ? thread_err : err;
5108 static const struct got_error *
5109 cmd_blame(int argc, char *argv[])
5111 const struct got_error *error;
5112 struct got_repository *repo = NULL;
5113 struct got_worktree *worktree = NULL;
5114 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5115 char *link_target = NULL;
5116 struct got_object_id *commit_id = NULL;
5117 struct got_commit_object *commit = NULL;
5118 char *commit_id_str = NULL;
5120 struct tog_view *view;
5121 int *pack_fds = NULL;
5123 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5126 commit_id_str = optarg;
5129 repo_path = realpath(optarg, NULL);
5130 if (repo_path == NULL)
5131 return got_error_from_errno2("realpath",
5146 error = got_repo_pack_fds_open(&pack_fds);
5150 if (repo_path == NULL) {
5151 cwd = getcwd(NULL, 0);
5153 return got_error_from_errno("getcwd");
5154 error = got_worktree_open(&worktree, cwd);
5155 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5159 strdup(got_worktree_get_repo_path(worktree));
5161 repo_path = strdup(cwd);
5162 if (repo_path == NULL) {
5163 error = got_error_from_errno("strdup");
5168 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5172 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5179 error = apply_unveil(got_repo_get_path(repo), NULL);
5183 error = tog_load_refs(repo, 0);
5187 if (commit_id_str == NULL) {
5188 struct got_reference *head_ref;
5189 error = got_ref_open(&head_ref, repo, worktree ?
5190 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5193 error = got_ref_resolve(&commit_id, repo, head_ref);
5194 got_ref_close(head_ref);
5196 error = got_repo_match_object_id(&commit_id, NULL,
5197 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5202 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5204 error = got_error_from_errno("view_open");
5208 error = got_object_open_as_commit(&commit, repo, commit_id);
5212 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5217 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5222 /* Release work tree lock. */
5223 got_worktree_close(worktree);
5226 error = view_loop(view);
5234 got_object_commit_close(commit);
5236 got_worktree_close(worktree);
5238 const struct got_error *close_err = got_repo_close(repo);
5243 const struct got_error *pack_err =
5244 got_repo_pack_fds_close(pack_fds);
5252 static const struct got_error *
5253 draw_tree_entries(struct tog_view *view, const char *parent_path)
5255 struct tog_tree_view_state *s = &view->state.tree;
5256 const struct got_error *err = NULL;
5257 struct got_tree_entry *te;
5259 struct tog_color *tc;
5260 int width, n, i, nentries;
5261 int limit = view->nlines;
5265 werase(view->window);
5270 err = format_line(&wline, &width, s->tree_label, view->ncols, 0, 0);
5273 if (view_needs_focus_indication(view))
5274 wstandout(view->window);
5275 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5277 wattr_on(view->window,
5278 COLOR_PAIR(tc->colorpair), NULL);
5279 waddwstr(view->window, wline);
5281 wattr_off(view->window,
5282 COLOR_PAIR(tc->colorpair), NULL);
5283 if (view_needs_focus_indication(view))
5284 wstandend(view->window);
5287 if (width < view->ncols - 1)
5288 waddch(view->window, '\n');
5291 err = format_line(&wline, &width, parent_path, view->ncols, 0, 0);
5294 waddwstr(view->window, wline);
5297 if (width < view->ncols - 1)
5298 waddch(view->window, '\n');
5301 waddch(view->window, '\n');
5305 if (s->first_displayed_entry == NULL) {
5306 te = got_object_tree_get_first_entry(s->tree);
5307 if (s->selected == 0) {
5309 wstandout(view->window);
5310 s->selected_entry = NULL;
5312 waddstr(view->window, " ..\n"); /* parent directory */
5313 if (s->selected == 0 && view->focussed)
5314 wstandend(view->window);
5321 te = s->first_displayed_entry;
5324 nentries = got_object_tree_get_nentries(s->tree);
5325 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5326 char *line = NULL, *id_str = NULL, *link_target = NULL;
5327 const char *modestr = "";
5330 te = got_object_tree_get_entry(s->tree, i);
5331 mode = got_tree_entry_get_mode(te);
5334 err = got_object_id_str(&id_str,
5335 got_tree_entry_get_id(te));
5337 return got_error_from_errno(
5338 "got_object_id_str");
5340 if (got_object_tree_entry_is_submodule(te))
5342 else if (S_ISLNK(mode)) {
5345 err = got_tree_entry_get_symlink_target(&link_target,
5351 for (i = 0; i < strlen(link_target); i++) {
5352 if (!isprint((unsigned char)link_target[i]))
5353 link_target[i] = '?';
5357 else if (S_ISDIR(mode))
5359 else if (mode & S_IXUSR)
5361 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5362 got_tree_entry_get_name(te), modestr,
5363 link_target ? " -> ": "",
5364 link_target ? link_target : "") == -1) {
5367 return got_error_from_errno("asprintf");
5371 err = format_line(&wline, &width, line, view->ncols, 0, 0);
5376 if (n == s->selected) {
5378 wstandout(view->window);
5379 s->selected_entry = te;
5381 tc = match_color(&s->colors, line);
5383 wattr_on(view->window,
5384 COLOR_PAIR(tc->colorpair), NULL);
5385 waddwstr(view->window, wline);
5387 wattr_off(view->window,
5388 COLOR_PAIR(tc->colorpair), NULL);
5389 if (width < view->ncols - 1)
5390 waddch(view->window, '\n');
5391 if (n == s->selected && view->focussed)
5392 wstandend(view->window);
5398 s->last_displayed_entry = te;
5407 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5409 struct got_tree_entry *te;
5410 int isroot = s->tree == s->root;
5413 if (s->first_displayed_entry == NULL)
5416 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5417 while (i++ < maxscroll) {
5420 s->first_displayed_entry = NULL;
5423 s->first_displayed_entry = te;
5424 te = got_tree_entry_get_prev(s->tree, te);
5429 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5431 struct got_tree_entry *next, *last;
5434 if (s->first_displayed_entry)
5435 next = got_tree_entry_get_next(s->tree,
5436 s->first_displayed_entry);
5438 next = got_object_tree_get_first_entry(s->tree);
5440 last = s->last_displayed_entry;
5441 while (next && last && n++ < maxscroll) {
5442 last = got_tree_entry_get_next(s->tree, last);
5444 s->first_displayed_entry = next;
5445 next = got_tree_entry_get_next(s->tree, next);
5450 static const struct got_error *
5451 tree_entry_path(char **path, struct tog_parent_trees *parents,
5452 struct got_tree_entry *te)
5454 const struct got_error *err = NULL;
5455 struct tog_parent_tree *pt;
5456 size_t len = 2; /* for leading slash and NUL */
5458 TAILQ_FOREACH(pt, parents, entry)
5459 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5462 len += strlen(got_tree_entry_get_name(te));
5464 *path = calloc(1, len);
5466 return got_error_from_errno("calloc");
5469 pt = TAILQ_LAST(parents, tog_parent_trees);
5471 const char *name = got_tree_entry_get_name(pt->selected_entry);
5472 if (strlcat(*path, name, len) >= len) {
5473 err = got_error(GOT_ERR_NO_SPACE);
5476 if (strlcat(*path, "/", len) >= len) {
5477 err = got_error(GOT_ERR_NO_SPACE);
5480 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5483 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5484 err = got_error(GOT_ERR_NO_SPACE);
5496 static const struct got_error *
5497 blame_tree_entry(struct tog_view **new_view, int begin_x,
5498 struct got_tree_entry *te, struct tog_parent_trees *parents,
5499 struct got_object_id *commit_id, struct got_repository *repo)
5501 const struct got_error *err = NULL;
5503 struct tog_view *blame_view;
5507 err = tree_entry_path(&path, parents, te);
5511 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5512 if (blame_view == NULL) {
5513 err = got_error_from_errno("view_open");
5517 err = open_blame_view(blame_view, path, commit_id, repo);
5519 if (err->code == GOT_ERR_CANCELLED)
5521 view_close(blame_view);
5523 *new_view = blame_view;
5529 static const struct got_error *
5530 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5531 struct tog_tree_view_state *s)
5533 struct tog_view *log_view;
5534 const struct got_error *err = NULL;
5539 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5540 if (log_view == NULL)
5541 return got_error_from_errno("view_open");
5543 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5547 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5550 view_close(log_view);
5552 *new_view = log_view;
5557 static const struct got_error *
5558 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5559 const char *head_ref_name, struct got_repository *repo)
5561 const struct got_error *err = NULL;
5562 char *commit_id_str = NULL;
5563 struct tog_tree_view_state *s = &view->state.tree;
5564 struct got_commit_object *commit = NULL;
5566 TAILQ_INIT(&s->parents);
5567 STAILQ_INIT(&s->colors);
5569 s->commit_id = got_object_id_dup(commit_id);
5570 if (s->commit_id == NULL)
5571 return got_error_from_errno("got_object_id_dup");
5573 err = got_object_open_as_commit(&commit, repo, commit_id);
5578 * The root is opened here and will be closed when the view is closed.
5579 * Any visited subtrees and their path-wise parents are opened and
5582 err = got_object_open_as_tree(&s->root, repo,
5583 got_object_commit_get_tree_id(commit));
5588 err = got_object_id_str(&commit_id_str, commit_id);
5592 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5593 err = got_error_from_errno("asprintf");
5597 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5598 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5599 if (head_ref_name) {
5600 s->head_ref_name = strdup(head_ref_name);
5601 if (s->head_ref_name == NULL) {
5602 err = got_error_from_errno("strdup");
5608 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5609 err = add_color(&s->colors, "\\$$",
5610 TOG_COLOR_TREE_SUBMODULE,
5611 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5614 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5615 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5618 err = add_color(&s->colors, "/$",
5619 TOG_COLOR_TREE_DIRECTORY,
5620 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5624 err = add_color(&s->colors, "\\*$",
5625 TOG_COLOR_TREE_EXECUTABLE,
5626 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5630 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5631 get_color_value("TOG_COLOR_COMMIT"));
5636 view->show = show_tree_view;
5637 view->input = input_tree_view;
5638 view->close = close_tree_view;
5639 view->search_start = search_start_tree_view;
5640 view->search_next = search_next_tree_view;
5642 free(commit_id_str);
5644 got_object_commit_close(commit);
5646 close_tree_view(view);
5650 static const struct got_error *
5651 close_tree_view(struct tog_view *view)
5653 struct tog_tree_view_state *s = &view->state.tree;
5655 free_colors(&s->colors);
5656 free(s->tree_label);
5657 s->tree_label = NULL;
5659 s->commit_id = NULL;
5660 free(s->head_ref_name);
5661 s->head_ref_name = NULL;
5662 while (!TAILQ_EMPTY(&s->parents)) {
5663 struct tog_parent_tree *parent;
5664 parent = TAILQ_FIRST(&s->parents);
5665 TAILQ_REMOVE(&s->parents, parent, entry);
5666 if (parent->tree != s->root)
5667 got_object_tree_close(parent->tree);
5671 if (s->tree != NULL && s->tree != s->root)
5672 got_object_tree_close(s->tree);
5674 got_object_tree_close(s->root);
5678 static const struct got_error *
5679 search_start_tree_view(struct tog_view *view)
5681 struct tog_tree_view_state *s = &view->state.tree;
5683 s->matched_entry = NULL;
5688 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5690 regmatch_t regmatch;
5692 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5696 static const struct got_error *
5697 search_next_tree_view(struct tog_view *view)
5699 struct tog_tree_view_state *s = &view->state.tree;
5700 struct got_tree_entry *te = NULL;
5702 if (!view->searching) {
5703 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5707 if (s->matched_entry) {
5708 if (view->searching == TOG_SEARCH_FORWARD) {
5709 if (s->selected_entry)
5710 te = got_tree_entry_get_next(s->tree,
5713 te = got_object_tree_get_first_entry(s->tree);
5715 if (s->selected_entry == NULL)
5716 te = got_object_tree_get_last_entry(s->tree);
5718 te = got_tree_entry_get_prev(s->tree,
5722 if (s->selected_entry)
5723 te = s->selected_entry;
5724 else if (view->searching == TOG_SEARCH_FORWARD)
5725 te = got_object_tree_get_first_entry(s->tree);
5727 te = got_object_tree_get_last_entry(s->tree);
5732 if (s->matched_entry == NULL) {
5733 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5736 if (view->searching == TOG_SEARCH_FORWARD)
5737 te = got_object_tree_get_first_entry(s->tree);
5739 te = got_object_tree_get_last_entry(s->tree);
5742 if (match_tree_entry(te, &view->regex)) {
5743 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5744 s->matched_entry = te;
5748 if (view->searching == TOG_SEARCH_FORWARD)
5749 te = got_tree_entry_get_next(s->tree, te);
5751 te = got_tree_entry_get_prev(s->tree, te);
5754 if (s->matched_entry) {
5755 s->first_displayed_entry = s->matched_entry;
5762 static const struct got_error *
5763 show_tree_view(struct tog_view *view)
5765 const struct got_error *err = NULL;
5766 struct tog_tree_view_state *s = &view->state.tree;
5769 err = tree_entry_path(&parent_path, &s->parents, NULL);
5773 err = draw_tree_entries(view, parent_path);
5780 static const struct got_error *
5781 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5783 const struct got_error *err = NULL;
5784 struct tog_tree_view_state *s = &view->state.tree;
5785 struct tog_view *log_view, *ref_view;
5786 struct got_tree_entry *te;
5787 int begin_x = 0, n, nscroll = view->nlines - 3;
5791 s->show_ids = !s->show_ids;
5794 if (!s->selected_entry)
5796 if (view_is_parent_view(view))
5797 begin_x = view_split_begin_x(view->begin_x);
5798 err = log_selected_tree_entry(&log_view, begin_x, s);
5800 log_view->focussed = 1;
5801 if (view_is_parent_view(view)) {
5802 err = view_close_child(view);
5805 view_set_child(view, log_view);
5806 view->focus_child = 1;
5808 *new_view = log_view;
5811 if (view_is_parent_view(view))
5812 begin_x = view_split_begin_x(view->begin_x);
5813 ref_view = view_open(view->nlines, view->ncols,
5814 view->begin_y, begin_x, TOG_VIEW_REF);
5815 if (ref_view == NULL)
5816 return got_error_from_errno("view_open");
5817 err = open_ref_view(ref_view, s->repo);
5819 view_close(ref_view);
5823 ref_view->focussed = 1;
5824 if (view_is_parent_view(view)) {
5825 err = view_close_child(view);
5828 view_set_child(view, ref_view);
5829 view->focus_child = 1;
5831 *new_view = ref_view;
5836 if (s->tree == s->root)
5837 s->first_displayed_entry =
5838 got_object_tree_get_first_entry(s->tree);
5840 s->first_displayed_entry = NULL;
5845 te = got_object_tree_get_last_entry(s->tree);
5846 for (n = 0; n < view->nlines - 3; n++) {
5848 if(s->tree != s->root) {
5849 s->first_displayed_entry = NULL;
5854 s->first_displayed_entry = te;
5855 te = got_tree_entry_get_prev(s->tree, te);
5858 s->selected = n - 1;
5863 if (s->selected > 0) {
5867 tree_scroll_up(s, 1);
5875 if (s->tree == s->root) {
5876 if (got_object_tree_get_first_entry(s->tree) ==
5877 s->first_displayed_entry)
5878 s->selected -= MIN(s->selected, nscroll);
5880 if (s->first_displayed_entry == NULL)
5881 s->selected -= MIN(s->selected, nscroll);
5883 tree_scroll_up(s, MAX(0, nscroll));
5888 if (s->selected < s->ndisplayed - 1) {
5892 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5894 /* can't scroll any further */
5896 tree_scroll_down(s, 1);
5904 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5906 /* can't scroll any further; move cursor down */
5907 if (s->selected < s->ndisplayed - 1)
5908 s->selected += MIN(nscroll,
5909 s->ndisplayed - s->selected - 1);
5912 tree_scroll_down(s, nscroll);
5917 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5918 struct tog_parent_tree *parent;
5919 /* user selected '..' */
5920 if (s->tree == s->root)
5922 parent = TAILQ_FIRST(&s->parents);
5923 TAILQ_REMOVE(&s->parents, parent,
5925 got_object_tree_close(s->tree);
5926 s->tree = parent->tree;
5927 s->first_displayed_entry =
5928 parent->first_displayed_entry;
5930 parent->selected_entry;
5931 s->selected = parent->selected;
5933 } else if (S_ISDIR(got_tree_entry_get_mode(
5934 s->selected_entry))) {
5935 struct got_tree_object *subtree;
5936 err = got_object_open_as_tree(&subtree, s->repo,
5937 got_tree_entry_get_id(s->selected_entry));
5940 err = tree_view_visit_subtree(s, subtree);
5942 got_object_tree_close(subtree);
5945 } else if (S_ISREG(got_tree_entry_get_mode(
5946 s->selected_entry))) {
5947 struct tog_view *blame_view;
5948 int begin_x = view_is_parent_view(view) ?
5949 view_split_begin_x(view->begin_x) : 0;
5951 err = blame_tree_entry(&blame_view, begin_x,
5952 s->selected_entry, &s->parents,
5953 s->commit_id, s->repo);
5957 blame_view->focussed = 1;
5958 if (view_is_parent_view(view)) {
5959 err = view_close_child(view);
5962 view_set_child(view, blame_view);
5963 view->focus_child = 1;
5965 *new_view = blame_view;
5969 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5970 s->selected = view->nlines - 4;
5983 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5988 static const struct got_error *
5989 cmd_tree(int argc, char *argv[])
5991 const struct got_error *error;
5992 struct got_repository *repo = NULL;
5993 struct got_worktree *worktree = NULL;
5994 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5995 struct got_object_id *commit_id = NULL;
5996 struct got_commit_object *commit = NULL;
5997 const char *commit_id_arg = NULL;
5999 struct got_reference *ref = NULL;
6000 const char *head_ref_name = NULL;
6002 struct tog_view *view;
6003 int *pack_fds = NULL;
6005 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6008 commit_id_arg = optarg;
6011 repo_path = realpath(optarg, NULL);
6012 if (repo_path == NULL)
6013 return got_error_from_errno2("realpath",
6028 error = got_repo_pack_fds_open(&pack_fds);
6032 if (repo_path == NULL) {
6033 cwd = getcwd(NULL, 0);
6035 return got_error_from_errno("getcwd");
6036 error = got_worktree_open(&worktree, cwd);
6037 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6041 strdup(got_worktree_get_repo_path(worktree));
6043 repo_path = strdup(cwd);
6044 if (repo_path == NULL) {
6045 error = got_error_from_errno("strdup");
6050 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6054 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6061 error = apply_unveil(got_repo_get_path(repo), NULL);
6065 error = tog_load_refs(repo, 0);
6069 if (commit_id_arg == NULL) {
6070 error = got_repo_match_object_id(&commit_id, &label,
6071 worktree ? got_worktree_get_head_ref_name(worktree) :
6072 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6075 head_ref_name = label;
6077 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6079 head_ref_name = got_ref_get_name(ref);
6080 else if (error->code != GOT_ERR_NOT_REF)
6082 error = got_repo_match_object_id(&commit_id, NULL,
6083 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6088 error = got_object_open_as_commit(&commit, repo, commit_id);
6092 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6094 error = got_error_from_errno("view_open");
6097 error = open_tree_view(view, commit_id, head_ref_name, repo);
6100 if (!got_path_is_root_dir(in_repo_path)) {
6101 error = tree_view_walk_path(&view->state.tree, commit,
6108 /* Release work tree lock. */
6109 got_worktree_close(worktree);
6112 error = view_loop(view);
6121 const struct got_error *close_err = got_repo_close(repo);
6126 const struct got_error *pack_err =
6127 got_repo_pack_fds_close(pack_fds);
6135 static const struct got_error *
6136 ref_view_load_refs(struct tog_ref_view_state *s)
6138 struct got_reflist_entry *sre;
6139 struct tog_reflist_entry *re;
6142 TAILQ_FOREACH(sre, &tog_refs, entry) {
6143 if (strncmp(got_ref_get_name(sre->ref),
6144 "refs/got/", 9) == 0 &&
6145 strncmp(got_ref_get_name(sre->ref),
6146 "refs/got/backup/", 16) != 0)
6149 re = malloc(sizeof(*re));
6151 return got_error_from_errno("malloc");
6153 re->ref = got_ref_dup(sre->ref);
6154 if (re->ref == NULL)
6155 return got_error_from_errno("got_ref_dup");
6156 re->idx = s->nrefs++;
6157 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6160 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6165 ref_view_free_refs(struct tog_ref_view_state *s)
6167 struct tog_reflist_entry *re;
6169 while (!TAILQ_EMPTY(&s->refs)) {
6170 re = TAILQ_FIRST(&s->refs);
6171 TAILQ_REMOVE(&s->refs, re, entry);
6172 got_ref_close(re->ref);
6177 static const struct got_error *
6178 open_ref_view(struct tog_view *view, struct got_repository *repo)
6180 const struct got_error *err = NULL;
6181 struct tog_ref_view_state *s = &view->state.ref;
6183 s->selected_entry = 0;
6186 TAILQ_INIT(&s->refs);
6187 STAILQ_INIT(&s->colors);
6189 err = ref_view_load_refs(s);
6193 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6194 err = add_color(&s->colors, "^refs/heads/",
6195 TOG_COLOR_REFS_HEADS,
6196 get_color_value("TOG_COLOR_REFS_HEADS"));
6200 err = add_color(&s->colors, "^refs/tags/",
6201 TOG_COLOR_REFS_TAGS,
6202 get_color_value("TOG_COLOR_REFS_TAGS"));
6206 err = add_color(&s->colors, "^refs/remotes/",
6207 TOG_COLOR_REFS_REMOTES,
6208 get_color_value("TOG_COLOR_REFS_REMOTES"));
6212 err = add_color(&s->colors, "^refs/got/backup/",
6213 TOG_COLOR_REFS_BACKUP,
6214 get_color_value("TOG_COLOR_REFS_BACKUP"));
6219 view->show = show_ref_view;
6220 view->input = input_ref_view;
6221 view->close = close_ref_view;
6222 view->search_start = search_start_ref_view;
6223 view->search_next = search_next_ref_view;
6226 free_colors(&s->colors);
6230 static const struct got_error *
6231 close_ref_view(struct tog_view *view)
6233 struct tog_ref_view_state *s = &view->state.ref;
6235 ref_view_free_refs(s);
6236 free_colors(&s->colors);
6241 static const struct got_error *
6242 resolve_reflist_entry(struct got_object_id **commit_id,
6243 struct tog_reflist_entry *re, struct got_repository *repo)
6245 const struct got_error *err = NULL;
6246 struct got_object_id *obj_id;
6247 struct got_tag_object *tag = NULL;
6252 err = got_ref_resolve(&obj_id, repo, re->ref);
6256 err = got_object_get_type(&obj_type, repo, obj_id);
6261 case GOT_OBJ_TYPE_COMMIT:
6262 *commit_id = obj_id;
6264 case GOT_OBJ_TYPE_TAG:
6265 err = got_object_open_as_tag(&tag, repo, obj_id);
6269 err = got_object_get_type(&obj_type, repo,
6270 got_object_tag_get_object_id(tag));
6273 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6274 err = got_error(GOT_ERR_OBJ_TYPE);
6277 *commit_id = got_object_id_dup(
6278 got_object_tag_get_object_id(tag));
6279 if (*commit_id == NULL) {
6280 err = got_error_from_errno("got_object_id_dup");
6285 err = got_error(GOT_ERR_OBJ_TYPE);
6291 got_object_tag_close(tag);
6299 static const struct got_error *
6300 log_ref_entry(struct tog_view **new_view, int begin_x,
6301 struct tog_reflist_entry *re, struct got_repository *repo)
6303 struct tog_view *log_view;
6304 const struct got_error *err = NULL;
6305 struct got_object_id *commit_id = NULL;
6309 err = resolve_reflist_entry(&commit_id, re, repo);
6311 if (err->code != GOT_ERR_OBJ_TYPE)
6317 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6318 if (log_view == NULL) {
6319 err = got_error_from_errno("view_open");
6323 err = open_log_view(log_view, commit_id, repo,
6324 got_ref_get_name(re->ref), "", 0);
6327 view_close(log_view);
6329 *new_view = log_view;
6335 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6337 struct tog_reflist_entry *re;
6340 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6343 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6344 while (i++ < maxscroll) {
6347 s->first_displayed_entry = re;
6348 re = TAILQ_PREV(re, tog_reflist_head, entry);
6353 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6355 struct tog_reflist_entry *next, *last;
6358 if (s->first_displayed_entry)
6359 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6361 next = TAILQ_FIRST(&s->refs);
6363 last = s->last_displayed_entry;
6364 while (next && last && n++ < maxscroll) {
6365 last = TAILQ_NEXT(last, entry);
6367 s->first_displayed_entry = next;
6368 next = TAILQ_NEXT(next, entry);
6373 static const struct got_error *
6374 search_start_ref_view(struct tog_view *view)
6376 struct tog_ref_view_state *s = &view->state.ref;
6378 s->matched_entry = NULL;
6383 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6385 regmatch_t regmatch;
6387 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6391 static const struct got_error *
6392 search_next_ref_view(struct tog_view *view)
6394 struct tog_ref_view_state *s = &view->state.ref;
6395 struct tog_reflist_entry *re = NULL;
6397 if (!view->searching) {
6398 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6402 if (s->matched_entry) {
6403 if (view->searching == TOG_SEARCH_FORWARD) {
6404 if (s->selected_entry)
6405 re = TAILQ_NEXT(s->selected_entry, entry);
6407 re = TAILQ_PREV(s->selected_entry,
6408 tog_reflist_head, entry);
6410 if (s->selected_entry == NULL)
6411 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6413 re = TAILQ_PREV(s->selected_entry,
6414 tog_reflist_head, entry);
6417 if (s->selected_entry)
6418 re = s->selected_entry;
6419 else if (view->searching == TOG_SEARCH_FORWARD)
6420 re = TAILQ_FIRST(&s->refs);
6422 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6427 if (s->matched_entry == NULL) {
6428 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6431 if (view->searching == TOG_SEARCH_FORWARD)
6432 re = TAILQ_FIRST(&s->refs);
6434 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6437 if (match_reflist_entry(re, &view->regex)) {
6438 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6439 s->matched_entry = re;
6443 if (view->searching == TOG_SEARCH_FORWARD)
6444 re = TAILQ_NEXT(re, entry);
6446 re = TAILQ_PREV(re, tog_reflist_head, entry);
6449 if (s->matched_entry) {
6450 s->first_displayed_entry = s->matched_entry;
6457 static const struct got_error *
6458 show_ref_view(struct tog_view *view)
6460 const struct got_error *err = NULL;
6461 struct tog_ref_view_state *s = &view->state.ref;
6462 struct tog_reflist_entry *re;
6465 struct tog_color *tc;
6467 int limit = view->nlines;
6469 werase(view->window);
6476 re = s->first_displayed_entry;
6478 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6480 return got_error_from_errno("asprintf");
6482 err = format_line(&wline, &width, line, view->ncols, 0, 0);
6487 if (view_needs_focus_indication(view))
6488 wstandout(view->window);
6489 waddwstr(view->window, wline);
6490 if (view_needs_focus_indication(view))
6491 wstandend(view->window);
6496 if (width < view->ncols - 1)
6497 waddch(view->window, '\n');
6502 while (re && limit > 0) {
6504 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6507 struct got_commit_object *ci;
6508 struct got_tag_object *tag;
6509 struct got_object_id *id;
6513 err = got_ref_resolve(&id, s->repo, re->ref);
6516 err = got_object_open_as_tag(&tag, s->repo, id);
6518 if (err->code != GOT_ERR_OBJ_TYPE) {
6522 err = got_object_open_as_commit(&ci, s->repo,
6528 t = got_object_commit_get_committer_time(ci);
6529 got_object_commit_close(ci);
6531 t = got_object_tag_get_tagger_time(tag);
6532 got_object_tag_close(tag);
6535 if (gmtime_r(&t, &tm) == NULL)
6536 return got_error_from_errno("gmtime_r");
6537 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6538 return got_error(GOT_ERR_NO_SPACE);
6540 if (got_ref_is_symbolic(re->ref)) {
6541 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6542 ymd : "", got_ref_get_name(re->ref),
6543 got_ref_get_symref_target(re->ref)) == -1)
6544 return got_error_from_errno("asprintf");
6545 } else if (s->show_ids) {
6546 struct got_object_id *id;
6548 err = got_ref_resolve(&id, s->repo, re->ref);
6551 err = got_object_id_str(&id_str, id);
6556 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6557 got_ref_get_name(re->ref), id_str) == -1) {
6558 err = got_error_from_errno("asprintf");
6565 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6566 got_ref_get_name(re->ref)) == -1)
6567 return got_error_from_errno("asprintf");
6569 err = format_line(&wline, &width, line, view->ncols, 0, 0);
6574 if (n == s->selected) {
6576 wstandout(view->window);
6577 s->selected_entry = re;
6579 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6581 wattr_on(view->window,
6582 COLOR_PAIR(tc->colorpair), NULL);
6583 waddwstr(view->window, wline);
6585 wattr_off(view->window,
6586 COLOR_PAIR(tc->colorpair), NULL);
6587 if (width < view->ncols - 1)
6588 waddch(view->window, '\n');
6589 if (n == s->selected && view->focussed)
6590 wstandend(view->window);
6596 s->last_displayed_entry = re;
6599 re = TAILQ_NEXT(re, entry);
6606 static const struct got_error *
6607 browse_ref_tree(struct tog_view **new_view, int begin_x,
6608 struct tog_reflist_entry *re, struct got_repository *repo)
6610 const struct got_error *err = NULL;
6611 struct got_object_id *commit_id = NULL;
6612 struct tog_view *tree_view;
6616 err = resolve_reflist_entry(&commit_id, re, repo);
6618 if (err->code != GOT_ERR_OBJ_TYPE)
6625 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6626 if (tree_view == NULL) {
6627 err = got_error_from_errno("view_open");
6631 err = open_tree_view(tree_view, commit_id,
6632 got_ref_get_name(re->ref), repo);
6636 *new_view = tree_view;
6641 static const struct got_error *
6642 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6644 const struct got_error *err = NULL;
6645 struct tog_ref_view_state *s = &view->state.ref;
6646 struct tog_view *log_view, *tree_view;
6647 struct tog_reflist_entry *re;
6648 int begin_x = 0, n, nscroll = view->nlines - 1;
6652 s->show_ids = !s->show_ids;
6655 s->show_date = !s->show_date;
6658 s->sort_by_date = !s->sort_by_date;
6659 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6660 got_ref_cmp_by_commit_timestamp_descending :
6661 tog_ref_cmp_by_name, s->repo);
6664 got_reflist_object_id_map_free(tog_refs_idmap);
6665 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6666 &tog_refs, s->repo);
6669 ref_view_free_refs(s);
6670 err = ref_view_load_refs(s);
6674 if (!s->selected_entry)
6676 if (view_is_parent_view(view))
6677 begin_x = view_split_begin_x(view->begin_x);
6678 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6681 log_view->focussed = 1;
6682 if (view_is_parent_view(view)) {
6683 err = view_close_child(view);
6686 view_set_child(view, log_view);
6687 view->focus_child = 1;
6689 *new_view = log_view;
6692 if (!s->selected_entry)
6694 if (view_is_parent_view(view))
6695 begin_x = view_split_begin_x(view->begin_x);
6696 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6698 if (err || tree_view == NULL)
6701 tree_view->focussed = 1;
6702 if (view_is_parent_view(view)) {
6703 err = view_close_child(view);
6706 view_set_child(view, tree_view);
6707 view->focus_child = 1;
6709 *new_view = tree_view;
6714 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6719 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6720 for (n = 0; n < view->nlines - 1; n++) {
6723 s->first_displayed_entry = re;
6724 re = TAILQ_PREV(re, tog_reflist_head, entry);
6727 s->selected = n - 1;
6732 if (s->selected > 0) {
6736 ref_scroll_up(s, 1);
6744 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6745 s->selected -= MIN(nscroll, s->selected);
6746 ref_scroll_up(s, MAX(0, nscroll));
6751 if (s->selected < s->ndisplayed - 1) {
6755 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6756 /* can't scroll any further */
6758 ref_scroll_down(s, 1);
6766 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6767 /* can't scroll any further; move cursor down */
6768 if (s->selected < s->ndisplayed - 1)
6769 s->selected += MIN(nscroll,
6770 s->ndisplayed - s->selected - 1);
6773 ref_scroll_down(s, nscroll);
6777 err = tog_load_refs(s->repo, s->sort_by_date);
6780 ref_view_free_refs(s);
6781 err = ref_view_load_refs(s);
6784 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6785 s->selected = view->nlines - 2;
6798 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6803 static const struct got_error *
6804 cmd_ref(int argc, char *argv[])
6806 const struct got_error *error;
6807 struct got_repository *repo = NULL;
6808 struct got_worktree *worktree = NULL;
6809 char *cwd = NULL, *repo_path = NULL;
6811 struct tog_view *view;
6812 int *pack_fds = NULL;
6814 while ((ch = getopt(argc, argv, "r:")) != -1) {
6817 repo_path = realpath(optarg, NULL);
6818 if (repo_path == NULL)
6819 return got_error_from_errno2("realpath",
6834 error = got_repo_pack_fds_open(&pack_fds);
6838 if (repo_path == NULL) {
6839 cwd = getcwd(NULL, 0);
6841 return got_error_from_errno("getcwd");
6842 error = got_worktree_open(&worktree, cwd);
6843 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6847 strdup(got_worktree_get_repo_path(worktree));
6849 repo_path = strdup(cwd);
6850 if (repo_path == NULL) {
6851 error = got_error_from_errno("strdup");
6856 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6862 error = apply_unveil(got_repo_get_path(repo), NULL);
6866 error = tog_load_refs(repo, 0);
6870 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6872 error = got_error_from_errno("view_open");
6876 error = open_ref_view(view, repo);
6881 /* Release work tree lock. */
6882 got_worktree_close(worktree);
6885 error = view_loop(view);
6890 const struct got_error *close_err = got_repo_close(repo);
6895 const struct got_error *pack_err =
6896 got_repo_pack_fds_close(pack_fds);
6905 list_commands(FILE *fp)
6909 fprintf(fp, "commands:");
6910 for (i = 0; i < nitems(tog_commands); i++) {
6911 const struct tog_cmd *cmd = &tog_commands[i];
6912 fprintf(fp, " %s", cmd->name);
6918 usage(int hflag, int status)
6920 FILE *fp = (status == 0) ? stdout : stderr;
6922 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6925 fprintf(fp, "lazy usage: %s path\n", getprogname());
6932 make_argv(int argc, ...)
6940 argv = calloc(argc, sizeof(char *));
6943 for (i = 0; i < argc; i++) {
6944 argv[i] = strdup(va_arg(ap, char *));
6945 if (argv[i] == NULL)
6954 * Try to convert 'tog path' into a 'tog log path' command.
6955 * The user could simply have mistyped the command rather than knowingly
6956 * provided a path. So check whether argv[0] can in fact be resolved
6957 * to a path in the HEAD commit and print a special error if not.
6958 * This hack is for mpi@ <3
6960 static const struct got_error *
6961 tog_log_with_path(int argc, char *argv[])
6963 const struct got_error *error = NULL, *close_err;
6964 const struct tog_cmd *cmd = NULL;
6965 struct got_repository *repo = NULL;
6966 struct got_worktree *worktree = NULL;
6967 struct got_object_id *commit_id = NULL, *id = NULL;
6968 struct got_commit_object *commit = NULL;
6969 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6970 char *commit_id_str = NULL, **cmd_argv = NULL;
6971 int *pack_fds = NULL;
6973 cwd = getcwd(NULL, 0);
6975 return got_error_from_errno("getcwd");
6977 error = got_repo_pack_fds_open(&pack_fds);
6981 error = got_worktree_open(&worktree, cwd);
6982 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6986 repo_path = strdup(got_worktree_get_repo_path(worktree));
6988 repo_path = strdup(cwd);
6989 if (repo_path == NULL) {
6990 error = got_error_from_errno("strdup");
6994 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6998 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7003 error = tog_load_refs(repo, 0);
7006 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7007 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7008 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7013 got_worktree_close(worktree);
7017 error = got_object_open_as_commit(&commit, repo, commit_id);
7021 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7023 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7025 fprintf(stderr, "%s: '%s' is no known command or path\n",
7026 getprogname(), argv[0]);
7031 close_err = got_repo_close(repo);
7036 error = got_object_id_str(&commit_id_str, commit_id);
7040 cmd = &tog_commands[0]; /* log */
7042 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7043 error = cmd->cmd_main(argc, cmd_argv);
7046 close_err = got_repo_close(repo);
7051 got_object_commit_close(commit);
7053 got_worktree_close(worktree);
7055 const struct got_error *pack_err =
7056 got_repo_pack_fds_close(pack_fds);
7061 free(commit_id_str);
7068 for (i = 0; i < argc; i++)
7077 main(int argc, char *argv[])
7079 const struct got_error *error = NULL;
7080 const struct tog_cmd *cmd = NULL;
7081 int ch, hflag = 0, Vflag = 0;
7082 char **cmd_argv = NULL;
7083 static const struct option longopts[] = {
7084 { "version", no_argument, NULL, 'V' },
7088 setlocale(LC_CTYPE, "");
7090 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7110 got_version_print_str();
7115 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7123 /* Build an argument vector which runs a default command. */
7124 cmd = &tog_commands[0];
7126 cmd_argv = make_argv(argc, cmd->name);
7130 /* Did the user specify a command? */
7131 for (i = 0; i < nitems(tog_commands); i++) {
7132 if (strncmp(tog_commands[i].name, argv[0],
7133 strlen(argv[0])) == 0) {
7134 cmd = &tog_commands[i];
7143 /* No command specified; try log with a path */
7144 error = tog_log_with_path(argc, argv);
7149 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7156 for (i = 0; i < argc; i++)
7161 if (error && error->code != GOT_ERR_CANCELLED)
7162 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);