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);
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;
1253 p = realloc(dst, n + nb);
1256 return got_error_from_errno("realloc");
1261 memset(dst + sz, ' ', nb);
1264 dst[sz++] = src[idx];
1274 * Skip leading nscroll columns of a wide character string.
1275 * Returns the index to the first character of the scrolled string.
1277 static const struct got_error *
1278 scroll_wline(int *scrollx , wchar_t *wline, int nscroll,
1282 size_t wlen = wcslen(wline);
1287 while (i < wlen && cols < nscroll) {
1288 int width = wcwidth(wline[i]);
1295 if (width == 1 || width == 2) {
1296 if (cols + width > nscroll)
1300 } else if (width == -1) {
1301 if (wline[i] == L'\t') {
1303 ((cols + col_tab_align) % TABSIZE);
1308 if (cols + width > nscroll)
1313 return got_error_from_errno("wcwidth");
1321 * Format a line for display, ensuring that it won't overflow a width limit.
1322 * With scrolling, the width returned refers to the scrolled version of the
1323 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1325 static const struct got_error *
1326 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1327 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1329 const struct got_error *err = NULL;
1331 wchar_t *wline = NULL;
1340 err = expand_tab(&exstr, line);
1345 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1350 err = scroll_wline(&scrollx, wline, nscroll, col_tab_align);
1354 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1355 wline[wlen - 1] = L'\0';
1358 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1359 wline[wlen - 1] = L'\0';
1365 int width = wcwidth(wline[i]);
1372 if (width == 1 || width == 2) {
1373 if (cols + width > wlimit)
1377 } else if (width == -1) {
1378 if (wline[i] == L'\t') {
1380 ((cols + col_tab_align) % TABSIZE);
1385 if (cols + width > wlimit)
1390 err = got_error_from_errno("wcwidth");
1398 *scrollxp = scrollx;
1407 static const struct got_error*
1408 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1409 struct got_object_id *id, struct got_repository *repo)
1411 static const struct got_error *err = NULL;
1412 struct got_reflist_entry *re;
1418 TAILQ_FOREACH(re, refs, entry) {
1419 struct got_tag_object *tag = NULL;
1420 struct got_object_id *ref_id;
1423 name = got_ref_get_name(re->ref);
1424 if (strcmp(name, GOT_REF_HEAD) == 0)
1426 if (strncmp(name, "refs/", 5) == 0)
1428 if (strncmp(name, "got/", 4) == 0 &&
1429 strncmp(name, "got/backup/", 11) != 0)
1431 if (strncmp(name, "heads/", 6) == 0)
1433 if (strncmp(name, "remotes/", 8) == 0) {
1435 s = strstr(name, "/" GOT_REF_HEAD);
1436 if (s != NULL && s[strlen(s)] == '\0')
1439 err = got_ref_resolve(&ref_id, repo, re->ref);
1442 if (strncmp(name, "tags/", 5) == 0) {
1443 err = got_object_open_as_tag(&tag, repo, ref_id);
1445 if (err->code != GOT_ERR_OBJ_TYPE) {
1449 /* Ref points at something other than a tag. */
1454 cmp = got_object_id_cmp(tag ?
1455 got_object_tag_get_object_id(tag) : ref_id, id);
1458 got_object_tag_close(tag);
1462 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1463 s ? ", " : "", name) == -1) {
1464 err = got_error_from_errno("asprintf");
1475 static const struct got_error *
1476 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1481 smallerthan = strchr(author, '<');
1482 if (smallerthan && smallerthan[1] != '\0')
1483 author = smallerthan + 1;
1484 author[strcspn(author, "@>")] = '\0';
1485 return format_line(wauthor, author_width, NULL, author, 0, limit,
1489 static const struct got_error *
1490 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1491 struct got_object_id *id, const size_t date_display_cols,
1492 int author_display_cols)
1494 struct tog_log_view_state *s = &view->state.log;
1495 const struct got_error *err = NULL;
1496 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1497 char *logmsg0 = NULL, *logmsg = NULL;
1498 char *author = NULL;
1499 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1500 int author_width, logmsg_width;
1501 char *newline, *line = NULL;
1502 int col, limit, scrollx;
1503 const int avail = view->ncols;
1505 time_t committer_time;
1506 struct tog_color *tc;
1508 committer_time = got_object_commit_get_committer_time(commit);
1509 if (gmtime_r(&committer_time, &tm) == NULL)
1510 return got_error_from_errno("gmtime_r");
1511 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1512 return got_error(GOT_ERR_NO_SPACE);
1514 if (avail <= date_display_cols)
1515 limit = MIN(sizeof(datebuf) - 1, avail);
1517 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1518 tc = get_color(&s->colors, TOG_COLOR_DATE);
1520 wattr_on(view->window,
1521 COLOR_PAIR(tc->colorpair), NULL);
1522 waddnstr(view->window, datebuf, limit);
1524 wattr_off(view->window,
1525 COLOR_PAIR(tc->colorpair), NULL);
1532 err = got_object_id_str(&id_str, id);
1535 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1537 wattr_on(view->window,
1538 COLOR_PAIR(tc->colorpair), NULL);
1539 wprintw(view->window, "%.8s ", id_str);
1541 wattr_off(view->window,
1542 COLOR_PAIR(tc->colorpair), NULL);
1549 author = strdup(got_object_commit_get_author(commit));
1550 if (author == NULL) {
1551 err = got_error_from_errno("strdup");
1554 err = format_author(&wauthor, &author_width, author, avail - col, col);
1557 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1559 wattr_on(view->window,
1560 COLOR_PAIR(tc->colorpair), NULL);
1561 waddwstr(view->window, wauthor);
1563 wattr_off(view->window,
1564 COLOR_PAIR(tc->colorpair), NULL);
1565 col += author_width;
1566 while (col < avail && author_width < author_display_cols + 2) {
1567 waddch(view->window, ' ');
1574 err = got_object_commit_get_logmsg(&logmsg0, commit);
1578 while (*logmsg == '\n')
1580 newline = strchr(logmsg, '\n');
1583 limit = avail - col;
1584 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1588 waddwstr(view->window, &wlogmsg[scrollx]);
1589 col += MAX(logmsg_width, 0);
1590 while (col < avail) {
1591 waddch(view->window, ' ');
1603 static struct commit_queue_entry *
1604 alloc_commit_queue_entry(struct got_commit_object *commit,
1605 struct got_object_id *id)
1607 struct commit_queue_entry *entry;
1609 entry = calloc(1, sizeof(*entry));
1614 entry->commit = commit;
1619 pop_commit(struct commit_queue *commits)
1621 struct commit_queue_entry *entry;
1623 entry = TAILQ_FIRST(&commits->head);
1624 TAILQ_REMOVE(&commits->head, entry, entry);
1625 got_object_commit_close(entry->commit);
1626 commits->ncommits--;
1627 /* Don't free entry->id! It is owned by the commit graph. */
1632 free_commits(struct commit_queue *commits)
1634 while (!TAILQ_EMPTY(&commits->head))
1635 pop_commit(commits);
1638 static const struct got_error *
1639 match_commit(int *have_match, struct got_object_id *id,
1640 struct got_commit_object *commit, regex_t *regex)
1642 const struct got_error *err = NULL;
1643 regmatch_t regmatch;
1644 char *id_str = NULL, *logmsg = NULL;
1648 err = got_object_id_str(&id_str, id);
1652 err = got_object_commit_get_logmsg(&logmsg, commit);
1656 if (regexec(regex, got_object_commit_get_author(commit), 1,
1657 ®match, 0) == 0 ||
1658 regexec(regex, got_object_commit_get_committer(commit), 1,
1659 ®match, 0) == 0 ||
1660 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1661 regexec(regex, logmsg, 1, ®match, 0) == 0)
1669 static const struct got_error *
1670 queue_commits(struct tog_log_thread_args *a)
1672 const struct got_error *err = NULL;
1675 * We keep all commits open throughout the lifetime of the log
1676 * view in order to avoid having to re-fetch commits from disk
1677 * while updating the display.
1680 struct got_object_id *id;
1681 struct got_commit_object *commit;
1682 struct commit_queue_entry *entry;
1685 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1687 if (err || id == NULL)
1690 err = got_object_open_as_commit(&commit, a->repo, id);
1693 entry = alloc_commit_queue_entry(commit, id);
1694 if (entry == NULL) {
1695 err = got_error_from_errno("alloc_commit_queue_entry");
1699 errcode = pthread_mutex_lock(&tog_mutex);
1701 err = got_error_set_errno(errcode,
1702 "pthread_mutex_lock");
1706 entry->idx = a->commits->ncommits;
1707 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1708 a->commits->ncommits++;
1710 if (*a->searching == TOG_SEARCH_FORWARD &&
1711 !*a->search_next_done) {
1713 err = match_commit(&have_match, id, commit, a->regex);
1717 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1720 errcode = pthread_mutex_unlock(&tog_mutex);
1721 if (errcode && err == NULL)
1722 err = got_error_set_errno(errcode,
1723 "pthread_mutex_unlock");
1726 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1732 select_commit(struct tog_log_view_state *s)
1734 struct commit_queue_entry *entry;
1737 entry = s->first_displayed_entry;
1739 if (ncommits == s->selected) {
1740 s->selected_entry = entry;
1743 entry = TAILQ_NEXT(entry, entry);
1748 static const struct got_error *
1749 draw_commits(struct tog_view *view)
1751 const struct got_error *err = NULL;
1752 struct tog_log_view_state *s = &view->state.log;
1753 struct commit_queue_entry *entry = s->selected_entry;
1754 const int limit = view->nlines;
1756 int ncommits, author_cols = 4;
1757 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1758 char *refs_str = NULL;
1760 struct tog_color *tc;
1761 static const size_t date_display_cols = 12;
1763 if (s->selected_entry &&
1764 !(view->searching && view->search_next_done == 0)) {
1765 struct got_reflist_head *refs;
1766 err = got_object_id_str(&id_str, s->selected_entry->id);
1769 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1770 s->selected_entry->id);
1772 err = build_refs_str(&refs_str, refs,
1773 s->selected_entry->id, s->repo);
1779 if (s->thread_args.commits_needed == 0)
1780 halfdelay(10); /* disable fast refresh */
1782 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1783 if (asprintf(&ncommits_str, " [%d/%d] %s",
1784 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1785 (view->searching && !view->search_next_done) ?
1786 "searching..." : "loading...") == -1) {
1787 err = got_error_from_errno("asprintf");
1791 const char *search_str = NULL;
1793 if (view->searching) {
1794 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1795 search_str = "no more matches";
1796 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1797 search_str = "no matches found";
1798 else if (!view->search_next_done)
1799 search_str = "searching...";
1802 if (asprintf(&ncommits_str, " [%d/%d] %s",
1803 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1804 search_str ? search_str :
1805 (refs_str ? refs_str : "")) == -1) {
1806 err = got_error_from_errno("asprintf");
1811 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1812 if (asprintf(&header, "commit %s %s%s",
1813 id_str ? id_str : "........................................",
1814 s->in_repo_path, ncommits_str) == -1) {
1815 err = got_error_from_errno("asprintf");
1819 } else if (asprintf(&header, "commit %s%s",
1820 id_str ? id_str : "........................................",
1821 ncommits_str) == -1) {
1822 err = got_error_from_errno("asprintf");
1826 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1830 werase(view->window);
1832 if (view_needs_focus_indication(view))
1833 wstandout(view->window);
1834 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1836 wattr_on(view->window,
1837 COLOR_PAIR(tc->colorpair), NULL);
1838 waddwstr(view->window, wline);
1840 wattr_off(view->window,
1841 COLOR_PAIR(tc->colorpair), NULL);
1842 while (width < view->ncols) {
1843 waddch(view->window, ' ');
1846 if (view_needs_focus_indication(view))
1847 wstandend(view->window);
1852 /* Grow author column size if necessary, and set view->maxx. */
1853 entry = s->first_displayed_entry;
1857 char *author, *eol, *msg, *msg0;
1858 wchar_t *wauthor, *wmsg;
1860 if (ncommits >= limit - 1)
1862 author = strdup(got_object_commit_get_author(entry->commit));
1863 if (author == NULL) {
1864 err = got_error_from_errno("strdup");
1867 err = format_author(&wauthor, &width, author, COLS,
1869 if (author_cols < width)
1870 author_cols = width;
1873 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1877 while (*msg == '\n')
1879 if ((eol = strchr(msg, '\n')))
1881 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
1882 date_display_cols + author_cols, 0);
1885 view->maxx = MAX(view->maxx, width);
1889 entry = TAILQ_NEXT(entry, entry);
1892 entry = s->first_displayed_entry;
1893 s->last_displayed_entry = s->first_displayed_entry;
1896 if (ncommits >= limit - 1)
1898 if (ncommits == s->selected)
1899 wstandout(view->window);
1900 err = draw_commit(view, entry->commit, entry->id,
1901 date_display_cols, author_cols);
1902 if (ncommits == s->selected)
1903 wstandend(view->window);
1907 s->last_displayed_entry = entry;
1908 entry = TAILQ_NEXT(entry, entry);
1923 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1925 struct commit_queue_entry *entry;
1928 entry = TAILQ_FIRST(&s->commits.head);
1929 if (s->first_displayed_entry == entry)
1932 entry = s->first_displayed_entry;
1933 while (entry && nscrolled < maxscroll) {
1934 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1936 s->first_displayed_entry = entry;
1942 static const struct got_error *
1943 trigger_log_thread(struct tog_view *view, int wait)
1945 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1948 halfdelay(1); /* fast refresh while loading commits */
1950 while (ta->commits_needed > 0 || ta->load_all) {
1951 if (ta->log_complete)
1954 /* Wake the log thread. */
1955 errcode = pthread_cond_signal(&ta->need_commits);
1957 return got_error_set_errno(errcode,
1958 "pthread_cond_signal");
1961 * The mutex will be released while the view loop waits
1962 * in wgetch(), at which time the log thread will run.
1967 /* Display progress update in log view. */
1968 show_log_view(view);
1972 /* Wait right here while next commit is being loaded. */
1973 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1975 return got_error_set_errno(errcode,
1976 "pthread_cond_wait");
1978 /* Display progress update in log view. */
1979 show_log_view(view);
1987 static const struct got_error *
1988 log_scroll_down(struct tog_view *view, int maxscroll)
1990 struct tog_log_view_state *s = &view->state.log;
1991 const struct got_error *err = NULL;
1992 struct commit_queue_entry *pentry;
1993 int nscrolled = 0, ncommits_needed;
1995 if (s->last_displayed_entry == NULL)
1998 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1999 if (s->commits.ncommits < ncommits_needed &&
2000 !s->thread_args.log_complete) {
2002 * Ask the log thread for required amount of commits.
2004 s->thread_args.commits_needed += maxscroll;
2005 err = trigger_log_thread(view, 1);
2011 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2015 s->last_displayed_entry = pentry;
2017 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2020 s->first_displayed_entry = pentry;
2021 } while (++nscrolled < maxscroll);
2026 static const struct got_error *
2027 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
2028 struct got_commit_object *commit, struct got_object_id *commit_id,
2029 struct tog_view *log_view, struct got_repository *repo)
2031 const struct got_error *err;
2032 struct got_object_qid *parent_id;
2033 struct tog_view *diff_view;
2035 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2036 if (diff_view == NULL)
2037 return got_error_from_errno("view_open");
2039 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2040 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2041 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2043 *new_view = diff_view;
2047 static const struct got_error *
2048 tree_view_visit_subtree(struct tog_tree_view_state *s,
2049 struct got_tree_object *subtree)
2051 struct tog_parent_tree *parent;
2053 parent = calloc(1, sizeof(*parent));
2055 return got_error_from_errno("calloc");
2057 parent->tree = s->tree;
2058 parent->first_displayed_entry = s->first_displayed_entry;
2059 parent->selected_entry = s->selected_entry;
2060 parent->selected = s->selected;
2061 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2064 s->first_displayed_entry = NULL;
2068 static const struct got_error *
2069 tree_view_walk_path(struct tog_tree_view_state *s,
2070 struct got_commit_object *commit, const char *path)
2072 const struct got_error *err = NULL;
2073 struct got_tree_object *tree = NULL;
2075 char *slash, *subpath = NULL;
2077 /* Walk the path and open corresponding tree objects. */
2080 struct got_tree_entry *te;
2081 struct got_object_id *tree_id;
2087 /* Ensure the correct subtree entry is selected. */
2088 slash = strchr(p, '/');
2090 te_name = strdup(p);
2092 te_name = strndup(p, slash - p);
2093 if (te_name == NULL) {
2094 err = got_error_from_errno("strndup");
2097 te = got_object_tree_find_entry(s->tree, te_name);
2099 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2104 s->first_displayed_entry = s->selected_entry = te;
2106 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2107 break; /* jump to this file's entry */
2109 slash = strchr(p, '/');
2111 subpath = strndup(path, slash - path);
2113 subpath = strdup(path);
2114 if (subpath == NULL) {
2115 err = got_error_from_errno("strdup");
2119 err = got_object_id_by_path(&tree_id, s->repo, commit,
2124 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2129 err = tree_view_visit_subtree(s, tree);
2131 got_object_tree_close(tree);
2145 static const struct got_error *
2146 browse_commit_tree(struct tog_view **new_view, int begin_x,
2147 struct commit_queue_entry *entry, const char *path,
2148 const char *head_ref_name, struct got_repository *repo)
2150 const struct got_error *err = NULL;
2151 struct tog_tree_view_state *s;
2152 struct tog_view *tree_view;
2154 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2155 if (tree_view == NULL)
2156 return got_error_from_errno("view_open");
2158 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2161 s = &tree_view->state.tree;
2163 *new_view = tree_view;
2165 if (got_path_is_root_dir(path))
2168 return tree_view_walk_path(s, entry->commit, path);
2171 static const struct got_error *
2172 block_signals_used_by_main_thread(void)
2177 if (sigemptyset(&sigset) == -1)
2178 return got_error_from_errno("sigemptyset");
2180 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2181 if (sigaddset(&sigset, SIGWINCH) == -1)
2182 return got_error_from_errno("sigaddset");
2183 if (sigaddset(&sigset, SIGCONT) == -1)
2184 return got_error_from_errno("sigaddset");
2185 if (sigaddset(&sigset, SIGINT) == -1)
2186 return got_error_from_errno("sigaddset");
2187 if (sigaddset(&sigset, SIGTERM) == -1)
2188 return got_error_from_errno("sigaddset");
2190 /* ncurses handles SIGTSTP */
2191 if (sigaddset(&sigset, SIGTSTP) == -1)
2192 return got_error_from_errno("sigaddset");
2194 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2196 return got_error_set_errno(errcode, "pthread_sigmask");
2202 log_thread(void *arg)
2204 const struct got_error *err = NULL;
2206 struct tog_log_thread_args *a = arg;
2209 err = block_signals_used_by_main_thread();
2213 while (!done && !err && !tog_fatal_signal_received()) {
2214 err = queue_commits(a);
2216 if (err->code != GOT_ERR_ITER_COMPLETED)
2220 } else if (a->commits_needed > 0 && !a->load_all)
2221 a->commits_needed--;
2223 errcode = pthread_mutex_lock(&tog_mutex);
2225 err = got_error_set_errno(errcode,
2226 "pthread_mutex_lock");
2228 } else if (*a->quit)
2230 else if (*a->first_displayed_entry == NULL) {
2231 *a->first_displayed_entry =
2232 TAILQ_FIRST(&a->commits->head);
2233 *a->selected_entry = *a->first_displayed_entry;
2236 errcode = pthread_cond_signal(&a->commit_loaded);
2238 err = got_error_set_errno(errcode,
2239 "pthread_cond_signal");
2240 pthread_mutex_unlock(&tog_mutex);
2245 a->commits_needed = 0;
2247 if (a->commits_needed == 0 && !a->load_all) {
2248 errcode = pthread_cond_wait(&a->need_commits,
2251 err = got_error_set_errno(errcode,
2252 "pthread_cond_wait");
2258 errcode = pthread_mutex_unlock(&tog_mutex);
2259 if (errcode && err == NULL)
2260 err = got_error_set_errno(errcode,
2261 "pthread_mutex_unlock");
2263 a->log_complete = 1;
2267 static const struct got_error *
2268 stop_log_thread(struct tog_log_view_state *s)
2270 const struct got_error *err = NULL;
2275 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2277 return got_error_set_errno(errcode,
2278 "pthread_cond_signal");
2279 errcode = pthread_mutex_unlock(&tog_mutex);
2281 return got_error_set_errno(errcode,
2282 "pthread_mutex_unlock");
2283 errcode = pthread_join(s->thread, (void **)&err);
2285 return got_error_set_errno(errcode, "pthread_join");
2286 errcode = pthread_mutex_lock(&tog_mutex);
2288 return got_error_set_errno(errcode,
2289 "pthread_mutex_lock");
2290 s->thread = 0; //NULL;
2293 if (s->thread_args.repo) {
2294 err = got_repo_close(s->thread_args.repo);
2295 s->thread_args.repo = NULL;
2298 if (s->thread_args.pack_fds) {
2299 const struct got_error *pack_err =
2300 got_repo_pack_fds_close(s->thread_args.pack_fds);
2303 s->thread_args.pack_fds = NULL;
2306 if (s->thread_args.graph) {
2307 got_commit_graph_close(s->thread_args.graph);
2308 s->thread_args.graph = NULL;
2314 static const struct got_error *
2315 close_log_view(struct tog_view *view)
2317 const struct got_error *err = NULL;
2318 struct tog_log_view_state *s = &view->state.log;
2321 err = stop_log_thread(s);
2323 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2324 if (errcode && err == NULL)
2325 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2327 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2328 if (errcode && err == NULL)
2329 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2331 free_commits(&s->commits);
2332 free(s->in_repo_path);
2333 s->in_repo_path = NULL;
2336 free(s->head_ref_name);
2337 s->head_ref_name = NULL;
2341 static const struct got_error *
2342 search_start_log_view(struct tog_view *view)
2344 struct tog_log_view_state *s = &view->state.log;
2346 s->matched_entry = NULL;
2347 s->search_entry = NULL;
2351 static const struct got_error *
2352 search_next_log_view(struct tog_view *view)
2354 const struct got_error *err = NULL;
2355 struct tog_log_view_state *s = &view->state.log;
2356 struct commit_queue_entry *entry;
2358 /* Display progress update in log view. */
2359 show_log_view(view);
2363 if (s->search_entry) {
2365 errcode = pthread_mutex_unlock(&tog_mutex);
2367 return got_error_set_errno(errcode,
2368 "pthread_mutex_unlock");
2369 ch = wgetch(view->window);
2370 errcode = pthread_mutex_lock(&tog_mutex);
2372 return got_error_set_errno(errcode,
2373 "pthread_mutex_lock");
2374 if (ch == KEY_BACKSPACE) {
2375 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2378 if (view->searching == TOG_SEARCH_FORWARD)
2379 entry = TAILQ_NEXT(s->search_entry, entry);
2381 entry = TAILQ_PREV(s->search_entry,
2382 commit_queue_head, entry);
2383 } else if (s->matched_entry) {
2384 if (view->searching == TOG_SEARCH_FORWARD)
2385 entry = TAILQ_NEXT(s->matched_entry, entry);
2387 entry = TAILQ_PREV(s->matched_entry,
2388 commit_queue_head, entry);
2390 entry = s->selected_entry;
2396 if (entry == NULL) {
2397 if (s->thread_args.log_complete ||
2398 view->searching == TOG_SEARCH_BACKWARD) {
2399 view->search_next_done =
2400 (s->matched_entry == NULL ?
2401 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2402 s->search_entry = NULL;
2406 * Poke the log thread for more commits and return,
2407 * allowing the main loop to make progress. Search
2408 * will resume at s->search_entry once we come back.
2410 s->thread_args.commits_needed++;
2411 return trigger_log_thread(view, 0);
2414 err = match_commit(&have_match, entry->id, entry->commit,
2419 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2420 s->matched_entry = entry;
2424 s->search_entry = entry;
2425 if (view->searching == TOG_SEARCH_FORWARD)
2426 entry = TAILQ_NEXT(entry, entry);
2428 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2431 if (s->matched_entry) {
2432 int cur = s->selected_entry->idx;
2433 while (cur < s->matched_entry->idx) {
2434 err = input_log_view(NULL, view, KEY_DOWN);
2439 while (cur > s->matched_entry->idx) {
2440 err = input_log_view(NULL, view, KEY_UP);
2447 s->search_entry = NULL;
2452 static const struct got_error *
2453 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2454 struct got_repository *repo, const char *head_ref_name,
2455 const char *in_repo_path, int log_branches)
2457 const struct got_error *err = NULL;
2458 struct tog_log_view_state *s = &view->state.log;
2459 struct got_repository *thread_repo = NULL;
2460 struct got_commit_graph *thread_graph = NULL;
2463 if (in_repo_path != s->in_repo_path) {
2464 free(s->in_repo_path);
2465 s->in_repo_path = strdup(in_repo_path);
2466 if (s->in_repo_path == NULL)
2467 return got_error_from_errno("strdup");
2470 /* The commit queue only contains commits being displayed. */
2471 TAILQ_INIT(&s->commits.head);
2472 s->commits.ncommits = 0;
2475 if (head_ref_name) {
2476 s->head_ref_name = strdup(head_ref_name);
2477 if (s->head_ref_name == NULL) {
2478 err = got_error_from_errno("strdup");
2482 s->start_id = got_object_id_dup(start_id);
2483 if (s->start_id == NULL) {
2484 err = got_error_from_errno("got_object_id_dup");
2487 s->log_branches = log_branches;
2489 STAILQ_INIT(&s->colors);
2490 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2491 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2492 get_color_value("TOG_COLOR_COMMIT"));
2495 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2496 get_color_value("TOG_COLOR_AUTHOR"));
2498 free_colors(&s->colors);
2501 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2502 get_color_value("TOG_COLOR_DATE"));
2504 free_colors(&s->colors);
2509 view->show = show_log_view;
2510 view->input = input_log_view;
2511 view->close = close_log_view;
2512 view->search_start = search_start_log_view;
2513 view->search_next = search_next_log_view;
2515 if (s->thread_args.pack_fds == NULL) {
2516 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2520 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2521 s->thread_args.pack_fds);
2524 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2528 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2529 s->repo, NULL, NULL);
2533 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2535 err = got_error_set_errno(errcode, "pthread_cond_init");
2538 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2540 err = got_error_set_errno(errcode, "pthread_cond_init");
2544 s->thread_args.commits_needed = view->nlines;
2545 s->thread_args.graph = thread_graph;
2546 s->thread_args.commits = &s->commits;
2547 s->thread_args.in_repo_path = s->in_repo_path;
2548 s->thread_args.start_id = s->start_id;
2549 s->thread_args.repo = thread_repo;
2550 s->thread_args.log_complete = 0;
2551 s->thread_args.quit = &s->quit;
2552 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2553 s->thread_args.selected_entry = &s->selected_entry;
2554 s->thread_args.searching = &view->searching;
2555 s->thread_args.search_next_done = &view->search_next_done;
2556 s->thread_args.regex = &view->regex;
2559 close_log_view(view);
2563 static const struct got_error *
2564 show_log_view(struct tog_view *view)
2566 const struct got_error *err;
2567 struct tog_log_view_state *s = &view->state.log;
2569 if (s->thread == 0) { //NULL) {
2570 int errcode = pthread_create(&s->thread, NULL, log_thread,
2573 return got_error_set_errno(errcode, "pthread_create");
2574 if (s->thread_args.commits_needed > 0) {
2575 err = trigger_log_thread(view, 1);
2581 return draw_commits(view);
2584 static const struct got_error *
2585 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2587 const struct got_error *err = NULL;
2588 struct tog_log_view_state *s = &view->state.log;
2589 struct tog_view *diff_view = NULL, *tree_view = NULL;
2590 struct tog_view *ref_view = NULL;
2591 struct commit_queue_entry *entry;
2592 int begin_x = 0, n, nscroll = view->nlines - 1;
2594 if (s->thread_args.load_all) {
2595 if (ch == KEY_BACKSPACE)
2596 s->thread_args.load_all = 0;
2597 else if (s->thread_args.log_complete) {
2598 s->thread_args.load_all = 0;
2599 log_scroll_down(view, s->commits.ncommits);
2600 s->selected = MIN(view->nlines - 2,
2601 s->commits.ncommits - 1);
2615 view->x = MAX(view->maxx - view->ncols / 2, 0);
2619 if (view->x + view->ncols / 2 < view->maxx)
2620 view->x += 2; /* move two columns right */
2624 view->x -= MIN(view->x, 2); /* move two columns back */
2631 if (s->first_displayed_entry == NULL)
2633 if (s->selected > 0)
2636 log_scroll_up(s, 1);
2642 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2651 if (s->first_displayed_entry == NULL)
2653 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2654 s->selected = MAX(0, s->selected - nscroll - 1);
2656 log_scroll_up(s, nscroll);
2664 if (s->first_displayed_entry == NULL)
2666 if (s->selected < MIN(view->nlines - 2,
2667 s->commits.ncommits - 1))
2670 err = log_scroll_down(view, 1);
2678 /* We don't know yet how many commits, so we're forced to
2679 * traverse them all. */
2680 if (!s->thread_args.log_complete) {
2681 s->thread_args.load_all = 1;
2682 return trigger_log_thread(view, 0);
2686 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2687 for (n = 0; n < view->nlines - 1; n++) {
2690 s->first_displayed_entry = entry;
2691 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2694 s->selected = n - 1;
2704 struct commit_queue_entry *first;
2705 first = s->first_displayed_entry;
2708 err = log_scroll_down(view, nscroll);
2711 if (first == s->first_displayed_entry &&
2712 s->selected < MIN(view->nlines - 2,
2713 s->commits.ncommits - 1)) {
2714 /* can't scroll further down */
2715 s->selected += MIN(s->last_displayed_entry->idx -
2716 s->selected_entry->idx, nscroll + 1);
2722 if (s->selected > view->nlines - 2)
2723 s->selected = view->nlines - 2;
2724 if (s->selected > s->commits.ncommits - 1)
2725 s->selected = s->commits.ncommits - 1;
2727 if (s->commits.ncommits < view->nlines - 1 &&
2728 !s->thread_args.log_complete) {
2729 s->thread_args.commits_needed += (view->nlines - 1) -
2730 s->commits.ncommits;
2731 err = trigger_log_thread(view, 1);
2737 if (s->selected_entry == NULL)
2739 if (view_is_parent_view(view))
2740 begin_x = view_split_begin_x(view->begin_x);
2741 err = open_diff_view_for_commit(&diff_view, begin_x,
2742 s->selected_entry->commit, s->selected_entry->id,
2747 diff_view->focussed = 1;
2748 if (view_is_parent_view(view)) {
2749 err = view_close_child(view);
2752 view_set_child(view, diff_view);
2753 view->focus_child = 1;
2755 *new_view = diff_view;
2758 if (s->selected_entry == NULL)
2760 if (view_is_parent_view(view))
2761 begin_x = view_split_begin_x(view->begin_x);
2762 err = browse_commit_tree(&tree_view, begin_x,
2763 s->selected_entry, s->in_repo_path, s->head_ref_name,
2768 tree_view->focussed = 1;
2769 if (view_is_parent_view(view)) {
2770 err = view_close_child(view);
2773 view_set_child(view, tree_view);
2774 view->focus_child = 1;
2776 *new_view = tree_view;
2781 if (ch == KEY_BACKSPACE &&
2782 got_path_is_root_dir(s->in_repo_path))
2784 err = stop_log_thread(s);
2787 if (ch == KEY_BACKSPACE) {
2789 err = got_path_dirname(&parent_path, s->in_repo_path);
2792 free(s->in_repo_path);
2793 s->in_repo_path = parent_path;
2794 s->thread_args.in_repo_path = s->in_repo_path;
2795 } else if (ch == CTRL('l')) {
2796 struct got_object_id *start_id;
2797 err = got_repo_match_object_id(&start_id, NULL,
2798 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2799 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2803 s->start_id = start_id;
2804 s->thread_args.start_id = s->start_id;
2806 s->log_branches = !s->log_branches;
2808 if (s->thread_args.pack_fds == NULL) {
2809 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2813 err = got_repo_open(&s->thread_args.repo,
2814 got_repo_get_path(s->repo), NULL,
2815 s->thread_args.pack_fds);
2819 err = tog_load_refs(s->repo, 0);
2822 err = got_commit_graph_open(&s->thread_args.graph,
2823 s->in_repo_path, !s->log_branches);
2826 err = got_commit_graph_iter_start(s->thread_args.graph,
2827 s->start_id, s->repo, NULL, NULL);
2830 free_commits(&s->commits);
2831 s->first_displayed_entry = NULL;
2832 s->last_displayed_entry = NULL;
2833 s->selected_entry = NULL;
2835 s->thread_args.log_complete = 0;
2837 s->thread_args.commits_needed = view->nlines;
2840 if (view_is_parent_view(view))
2841 begin_x = view_split_begin_x(view->begin_x);
2842 ref_view = view_open(view->nlines, view->ncols,
2843 view->begin_y, begin_x, TOG_VIEW_REF);
2844 if (ref_view == NULL)
2845 return got_error_from_errno("view_open");
2846 err = open_ref_view(ref_view, s->repo);
2848 view_close(ref_view);
2852 ref_view->focussed = 1;
2853 if (view_is_parent_view(view)) {
2854 err = view_close_child(view);
2857 view_set_child(view, ref_view);
2858 view->focus_child = 1;
2860 *new_view = ref_view;
2869 static const struct got_error *
2870 apply_unveil(const char *repo_path, const char *worktree_path)
2872 const struct got_error *error;
2875 if (unveil("gmon.out", "rwc") != 0)
2876 return got_error_from_errno2("unveil", "gmon.out");
2878 if (repo_path && unveil(repo_path, "r") != 0)
2879 return got_error_from_errno2("unveil", repo_path);
2881 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2882 return got_error_from_errno2("unveil", worktree_path);
2884 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2885 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2887 error = got_privsep_unveil_exec_helpers();
2891 if (unveil(NULL, NULL) != 0)
2892 return got_error_from_errno("unveil");
2901 * Override default signal handlers before starting ncurses.
2902 * This should prevent ncurses from installing its own
2903 * broken cleanup() signal handler.
2905 signal(SIGWINCH, tog_sigwinch);
2906 signal(SIGPIPE, tog_sigpipe);
2907 signal(SIGCONT, tog_sigcont);
2908 signal(SIGINT, tog_sigint);
2909 signal(SIGTERM, tog_sigterm);
2913 halfdelay(1); /* Do fast refresh while initial view is loading. */
2916 intrflush(stdscr, FALSE);
2917 keypad(stdscr, TRUE);
2919 if (getenv("TOG_COLORS") != NULL) {
2921 use_default_colors();
2925 static const struct got_error *
2926 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2927 struct got_repository *repo, struct got_worktree *worktree)
2929 const struct got_error *err = NULL;
2932 *in_repo_path = strdup("/");
2933 if (*in_repo_path == NULL)
2934 return got_error_from_errno("strdup");
2939 const char *prefix = got_worktree_get_path_prefix(worktree);
2942 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2945 if (asprintf(in_repo_path, "%s%s%s", prefix,
2946 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2948 err = got_error_from_errno("asprintf");
2949 *in_repo_path = NULL;
2953 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2958 static const struct got_error *
2959 cmd_log(int argc, char *argv[])
2961 const struct got_error *error;
2962 struct got_repository *repo = NULL;
2963 struct got_worktree *worktree = NULL;
2964 struct got_object_id *start_id = NULL;
2965 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2966 char *start_commit = NULL, *label = NULL;
2967 struct got_reference *ref = NULL;
2968 const char *head_ref_name = NULL;
2969 int ch, log_branches = 0;
2970 struct tog_view *view;
2971 int *pack_fds = NULL;
2973 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2979 start_commit = optarg;
2982 repo_path = realpath(optarg, NULL);
2983 if (repo_path == NULL)
2984 return got_error_from_errno2("realpath",
2999 error = got_repo_pack_fds_open(&pack_fds);
3003 if (repo_path == NULL) {
3004 cwd = getcwd(NULL, 0);
3006 return got_error_from_errno("getcwd");
3007 error = got_worktree_open(&worktree, cwd);
3008 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3012 strdup(got_worktree_get_repo_path(worktree));
3014 repo_path = strdup(cwd);
3015 if (repo_path == NULL) {
3016 error = got_error_from_errno("strdup");
3021 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3025 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3032 error = apply_unveil(got_repo_get_path(repo),
3033 worktree ? got_worktree_get_root_path(worktree) : NULL);
3037 /* already loaded by tog_log_with_path()? */
3038 if (TAILQ_EMPTY(&tog_refs)) {
3039 error = tog_load_refs(repo, 0);
3044 if (start_commit == NULL) {
3045 error = got_repo_match_object_id(&start_id, &label,
3046 worktree ? got_worktree_get_head_ref_name(worktree) :
3047 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3050 head_ref_name = label;
3052 error = got_ref_open(&ref, repo, start_commit, 0);
3054 head_ref_name = got_ref_get_name(ref);
3055 else if (error->code != GOT_ERR_NOT_REF)
3057 error = got_repo_match_object_id(&start_id, NULL,
3058 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3063 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3065 error = got_error_from_errno("view_open");
3068 error = open_log_view(view, start_id, repo, head_ref_name,
3069 in_repo_path, log_branches);
3073 /* Release work tree lock. */
3074 got_worktree_close(worktree);
3077 error = view_loop(view);
3087 const struct got_error *close_err = got_repo_close(repo);
3092 got_worktree_close(worktree);
3094 const struct got_error *pack_err =
3095 got_repo_pack_fds_close(pack_fds);
3107 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3108 "[-w] object1 object2\n", getprogname());
3113 match_line(const char *line, regex_t *regex, size_t nmatch,
3114 regmatch_t *regmatch)
3116 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3120 match_color(struct tog_colors *colors, const char *line)
3122 struct tog_color *tc = NULL;
3124 STAILQ_FOREACH(tc, colors, entry) {
3125 if (match_line(line, &tc->regex, 0, NULL))
3132 static const struct got_error *
3133 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3134 WINDOW *window, int skip, regmatch_t *regmatch)
3136 const struct got_error *err = NULL;
3138 int rme, rms, n, width;
3141 rms = regmatch->rm_so;
3142 rme = regmatch->rm_eo;
3144 err = format_line(&wline, &width, NULL, line, 0, wlimit + skip,
3149 /* draw up to matched token if we haven't scrolled past it */
3150 n = MAX(rms - skip, 0);
3152 waddnwstr(window, wline + skip, n);
3158 int len = rme - rms;
3162 len = MAX(len - n, 0);
3164 /* draw (visible part of) matched token (if scrolled into it) */
3166 wattron(window, A_STANDOUT);
3167 waddnwstr(window, wline + rms + n, len);
3168 wattroff(window, A_STANDOUT);
3174 if (wlimit > 0 && skip < width) { /* draw rest of line */
3177 n = MIN(skip - rme, width - rme);
3178 waddnwstr(window, wline + rme + n, wlimit);
3186 static const struct got_error *
3187 draw_file(struct tog_view *view, const char *header)
3189 struct tog_diff_view_state *s = &view->state.diff;
3190 regmatch_t *regmatch = &view->regmatch;
3191 const struct got_error *err;
3194 size_t linesize = 0;
3196 struct tog_color *tc;
3199 int max_lines = view->nlines;
3200 int nlines = s->nlines;
3203 line_offset = s->line_offsets[s->first_displayed_line - 1];
3204 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3205 return got_error_from_errno("fseek");
3207 werase(view->window);
3210 if (asprintf(&line, "[%d/%d] %s",
3211 s->first_displayed_line - 1 + s->selected_line, nlines,
3213 return got_error_from_errno("asprintf");
3214 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3220 if (view_needs_focus_indication(view))
3221 wstandout(view->window);
3222 waddwstr(view->window, wline);
3225 if (view_needs_focus_indication(view))
3226 wstandend(view->window);
3227 if (width <= view->ncols - 1)
3228 waddch(view->window, '\n');
3238 while (max_lines > 0 && nprinted < max_lines) {
3239 linelen = getline(&line, &linesize, s->f);
3240 if (linelen == -1) {
3246 return got_ferror(s->f, GOT_ERR_IO);
3249 view->maxx = MAX(view->maxx, linelen);
3251 tc = match_color(&s->colors, line);
3253 wattr_on(view->window,
3254 COLOR_PAIR(tc->colorpair), NULL);
3255 if (s->first_displayed_line + nprinted == s->matched_line &&
3256 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3257 err = add_matched_line(&width, line, view->ncols, 0,
3258 view->window, view->x, regmatch);
3264 err = format_line(&wline, &width, NULL, line, 0,
3265 view->x + view->ncols, 0, view->x ? 1 : 0);
3270 if (view->x < width)
3271 waddwstr(view->window, wline + view->x);
3276 wattr_off(view->window,
3277 COLOR_PAIR(tc->colorpair), NULL);
3278 if (width - view->x <= view->ncols - 1)
3279 waddch(view->window, '\n');
3284 s->last_displayed_line = s->first_displayed_line +
3287 s->last_displayed_line = s->first_displayed_line;
3292 while (nprinted < view->nlines) {
3293 waddch(view->window, '\n');
3297 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3303 wstandout(view->window);
3304 waddwstr(view->window, wline);
3307 wstandend(view->window);
3314 get_datestr(time_t *time, char *datebuf)
3316 struct tm mytm, *tm;
3319 tm = gmtime_r(time, &mytm);
3322 s = asctime_r(tm, datebuf);
3325 p = strchr(s, '\n');
3331 static const struct got_error *
3332 get_changed_paths(struct got_pathlist_head *paths,
3333 struct got_commit_object *commit, struct got_repository *repo)
3335 const struct got_error *err = NULL;
3336 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3337 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3338 struct got_object_qid *qid;
3340 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3342 struct got_commit_object *pcommit;
3343 err = got_object_open_as_commit(&pcommit, repo,
3348 tree_id1 = got_object_id_dup(
3349 got_object_commit_get_tree_id(pcommit));
3350 if (tree_id1 == NULL) {
3351 got_object_commit_close(pcommit);
3352 return got_error_from_errno("got_object_id_dup");
3354 got_object_commit_close(pcommit);
3359 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3364 tree_id2 = got_object_commit_get_tree_id(commit);
3365 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3369 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3370 got_diff_tree_collect_changed_paths, paths, 0);
3373 got_object_tree_close(tree1);
3375 got_object_tree_close(tree2);
3380 static const struct got_error *
3381 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3385 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3387 return got_error_from_errno("reallocarray");
3389 (*line_offsets)[*nlines] = off;
3394 static const struct got_error *
3395 write_commit_info(off_t **line_offsets, size_t *nlines,
3396 struct got_object_id *commit_id, struct got_reflist_head *refs,
3397 struct got_repository *repo, FILE *outfile)
3399 const struct got_error *err = NULL;
3400 char datebuf[26], *datestr;
3401 struct got_commit_object *commit;
3402 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3403 time_t committer_time;
3404 const char *author, *committer;
3405 char *refs_str = NULL;
3406 struct got_pathlist_head changed_paths;
3407 struct got_pathlist_entry *pe;
3411 TAILQ_INIT(&changed_paths);
3414 err = build_refs_str(&refs_str, refs, commit_id, repo);
3419 err = got_object_open_as_commit(&commit, repo, commit_id);
3423 err = got_object_id_str(&id_str, commit_id);
3425 err = got_error_from_errno("got_object_id_str");
3429 err = add_line_offset(line_offsets, nlines, 0);
3433 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3434 refs_str ? refs_str : "", refs_str ? ")" : "");
3436 err = got_error_from_errno("fprintf");
3440 err = add_line_offset(line_offsets, nlines, outoff);
3444 n = fprintf(outfile, "from: %s\n",
3445 got_object_commit_get_author(commit));
3447 err = got_error_from_errno("fprintf");
3451 err = add_line_offset(line_offsets, nlines, outoff);
3455 committer_time = got_object_commit_get_committer_time(commit);
3456 datestr = get_datestr(&committer_time, datebuf);
3458 n = fprintf(outfile, "date: %s UTC\n", datestr);
3460 err = got_error_from_errno("fprintf");
3464 err = add_line_offset(line_offsets, nlines, outoff);
3468 author = got_object_commit_get_author(commit);
3469 committer = got_object_commit_get_committer(commit);
3470 if (strcmp(author, committer) != 0) {
3471 n = fprintf(outfile, "via: %s\n", committer);
3473 err = got_error_from_errno("fprintf");
3477 err = add_line_offset(line_offsets, nlines, outoff);
3481 if (got_object_commit_get_nparents(commit) > 1) {
3482 const struct got_object_id_queue *parent_ids;
3483 struct got_object_qid *qid;
3485 parent_ids = got_object_commit_get_parent_ids(commit);
3486 STAILQ_FOREACH(qid, parent_ids, entry) {
3487 err = got_object_id_str(&id_str, &qid->id);
3490 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3492 err = got_error_from_errno("fprintf");
3496 err = add_line_offset(line_offsets, nlines, outoff);
3504 err = got_object_commit_get_logmsg(&logmsg, commit);
3508 while ((line = strsep(&s, "\n")) != NULL) {
3509 n = fprintf(outfile, "%s\n", line);
3511 err = got_error_from_errno("fprintf");
3515 err = add_line_offset(line_offsets, nlines, outoff);
3520 err = get_changed_paths(&changed_paths, commit, repo);
3523 TAILQ_FOREACH(pe, &changed_paths, entry) {
3524 struct got_diff_changed_path *cp = pe->data;
3525 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3527 err = got_error_from_errno("fprintf");
3531 err = add_line_offset(line_offsets, nlines, outoff);
3534 free((char *)pe->path);
3538 fputc('\n', outfile);
3540 err = add_line_offset(line_offsets, nlines, outoff);
3542 got_pathlist_free(&changed_paths);
3546 got_object_commit_close(commit);
3548 free(*line_offsets);
3549 *line_offsets = NULL;
3555 static const struct got_error *
3556 create_diff(struct tog_diff_view_state *s)
3558 const struct got_error *err = NULL;
3562 free(s->line_offsets);
3563 s->line_offsets = malloc(sizeof(off_t));
3564 if (s->line_offsets == NULL)
3565 return got_error_from_errno("malloc");
3570 err = got_error_from_errno("got_opentemp");
3573 if (s->f && fclose(s->f) == EOF) {
3574 err = got_error_from_errno("fclose");
3580 err = got_object_get_type(&obj_type, s->repo, s->id1);
3582 err = got_object_get_type(&obj_type, s->repo, s->id2);
3587 case GOT_OBJ_TYPE_BLOB:
3588 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3589 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3590 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3593 case GOT_OBJ_TYPE_TREE:
3594 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3595 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3596 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3598 case GOT_OBJ_TYPE_COMMIT: {
3599 const struct got_object_id_queue *parent_ids;
3600 struct got_object_qid *pid;
3601 struct got_commit_object *commit2;
3602 struct got_reflist_head *refs;
3604 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3607 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3608 /* Show commit info if we're diffing to a parent/root commit. */
3609 if (s->id1 == NULL) {
3610 err = write_commit_info(&s->line_offsets, &s->nlines,
3611 s->id2, refs, s->repo, s->f);
3615 parent_ids = got_object_commit_get_parent_ids(commit2);
3616 STAILQ_FOREACH(pid, parent_ids, entry) {
3617 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3618 err = write_commit_info(
3619 &s->line_offsets, &s->nlines,
3620 s->id2, refs, s->repo, s->f);
3627 got_object_commit_close(commit2);
3629 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3630 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3631 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3635 err = got_error(GOT_ERR_OBJ_TYPE);
3641 if (s->f && fflush(s->f) != 0 && err == NULL)
3642 err = got_error_from_errno("fflush");
3647 diff_view_indicate_progress(struct tog_view *view)
3649 mvwaddstr(view->window, 0, 0, "diffing...");
3654 static const struct got_error *
3655 search_start_diff_view(struct tog_view *view)
3657 struct tog_diff_view_state *s = &view->state.diff;
3659 s->matched_line = 0;
3663 static const struct got_error *
3664 search_next_diff_view(struct tog_view *view)
3666 struct tog_diff_view_state *s = &view->state.diff;
3667 const struct got_error *err = NULL;
3669 char *exstr = NULL, *line = NULL;
3670 size_t linesize = 0;
3673 if (!view->searching) {
3674 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3678 if (s->matched_line) {
3679 if (view->searching == TOG_SEARCH_FORWARD)
3680 lineno = s->matched_line + 1;
3682 lineno = s->matched_line - 1;
3684 lineno = s->first_displayed_line;
3689 if (lineno <= 0 || lineno > s->nlines) {
3690 if (s->matched_line == 0) {
3691 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3695 if (view->searching == TOG_SEARCH_FORWARD)
3701 offset = s->line_offsets[lineno - 1];
3702 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3704 return got_error_from_errno("fseeko");
3706 linelen = getline(&line, &linesize, s->f);
3707 err = expand_tab(&exstr, line);
3710 if (linelen != -1 &&
3711 match_line(exstr, &view->regex, 1, &view->regmatch)) {
3712 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3713 s->matched_line = lineno;
3718 if (view->searching == TOG_SEARCH_FORWARD)
3726 if (s->matched_line) {
3727 s->first_displayed_line = s->matched_line;
3728 s->selected_line = 1;
3734 static const struct got_error *
3735 close_diff_view(struct tog_view *view)
3737 const struct got_error *err = NULL;
3738 struct tog_diff_view_state *s = &view->state.diff;
3744 if (s->f && fclose(s->f) == EOF)
3745 err = got_error_from_errno("fclose");
3747 if (s->f1 && fclose(s->f1) == EOF)
3748 err = got_error_from_errno("fclose");
3750 if (s->f2 && fclose(s->f2) == EOF)
3751 err = got_error_from_errno("fclose");
3753 free_colors(&s->colors);
3754 free(s->line_offsets);
3755 s->line_offsets = NULL;
3760 static const struct got_error *
3761 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3762 struct got_object_id *id2, const char *label1, const char *label2,
3763 int diff_context, int ignore_whitespace, int force_text_diff,
3764 struct tog_view *log_view, struct got_repository *repo)
3766 const struct got_error *err;
3767 struct tog_diff_view_state *s = &view->state.diff;
3769 memset(s, 0, sizeof(*s));
3771 if (id1 != NULL && id2 != NULL) {
3773 err = got_object_get_type(&type1, repo, id1);
3776 err = got_object_get_type(&type2, repo, id2);
3781 return got_error(GOT_ERR_OBJ_TYPE);
3783 s->first_displayed_line = 1;
3784 s->last_displayed_line = view->nlines;
3785 s->selected_line = 1;
3793 s->id1 = got_object_id_dup(id1);
3795 return got_error_from_errno("got_object_id_dup");
3796 s->f1 = got_opentemp();
3797 if (s->f1 == NULL) {
3798 err = got_error_from_errno("got_opentemp");
3804 s->id2 = got_object_id_dup(id2);
3805 if (s->id2 == NULL) {
3806 err = got_error_from_errno("got_object_id_dup");
3810 s->f2 = got_opentemp();
3811 if (s->f2 == NULL) {
3812 err = got_error_from_errno("got_opentemp");
3816 s->first_displayed_line = 1;
3817 s->last_displayed_line = view->nlines;
3818 s->diff_context = diff_context;
3819 s->ignore_whitespace = ignore_whitespace;
3820 s->force_text_diff = force_text_diff;
3821 s->log_view = log_view;
3824 STAILQ_INIT(&s->colors);
3825 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3826 err = add_color(&s->colors,
3827 "^-", TOG_COLOR_DIFF_MINUS,
3828 get_color_value("TOG_COLOR_DIFF_MINUS"));
3831 err = add_color(&s->colors, "^\\+",
3832 TOG_COLOR_DIFF_PLUS,
3833 get_color_value("TOG_COLOR_DIFF_PLUS"));
3836 err = add_color(&s->colors,
3837 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3838 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3842 err = add_color(&s->colors,
3843 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3844 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3845 get_color_value("TOG_COLOR_DIFF_META"));
3849 err = add_color(&s->colors,
3850 "^(from|via): ", TOG_COLOR_AUTHOR,
3851 get_color_value("TOG_COLOR_AUTHOR"));
3855 err = add_color(&s->colors,
3856 "^date: ", TOG_COLOR_DATE,
3857 get_color_value("TOG_COLOR_DATE"));
3862 if (log_view && view_is_splitscreen(view))
3863 show_log_view(log_view); /* draw vborder */
3864 diff_view_indicate_progress(view);
3866 err = create_diff(s);
3868 view->show = show_diff_view;
3869 view->input = input_diff_view;
3870 view->close = close_diff_view;
3871 view->search_start = search_start_diff_view;
3872 view->search_next = search_next_diff_view;
3875 close_diff_view(view);
3879 static const struct got_error *
3880 show_diff_view(struct tog_view *view)
3882 const struct got_error *err;
3883 struct tog_diff_view_state *s = &view->state.diff;
3884 char *id_str1 = NULL, *id_str2, *header;
3885 const char *label1, *label2;
3888 err = got_object_id_str(&id_str1, s->id1);
3891 label1 = s->label1 ? : id_str1;
3893 label1 = "/dev/null";
3895 err = got_object_id_str(&id_str2, s->id2);
3898 label2 = s->label2 ? : id_str2;
3900 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3901 err = got_error_from_errno("asprintf");
3909 err = draw_file(view, header);
3914 static const struct got_error *
3915 set_selected_commit(struct tog_diff_view_state *s,
3916 struct commit_queue_entry *entry)
3918 const struct got_error *err;
3919 const struct got_object_id_queue *parent_ids;
3920 struct got_commit_object *selected_commit;
3921 struct got_object_qid *pid;
3924 s->id2 = got_object_id_dup(entry->id);
3926 return got_error_from_errno("got_object_id_dup");
3928 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3931 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3933 pid = STAILQ_FIRST(parent_ids);
3934 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3935 got_object_commit_close(selected_commit);
3939 static const struct got_error *
3940 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3942 const struct got_error *err = NULL;
3943 struct tog_diff_view_state *s = &view->state.diff;
3944 struct tog_log_view_state *ls;
3945 struct commit_queue_entry *old_selected_entry;
3947 size_t linesize = 0;
3949 int i, nscroll = view->nlines - 1;
3956 view->x = MAX(view->maxx - view->ncols / 3, 0);
3960 if (view->x + view->ncols / 3 < view->maxx)
3961 view->x += 2; /* move two columns right */
3965 view->x -= MIN(view->x, 2); /* move two columns back */
3970 s->force_text_diff = !s->force_text_diff;
3972 s->ignore_whitespace = !s->ignore_whitespace;
3973 wclear(view->window);
3974 s->first_displayed_line = 1;
3975 s->last_displayed_line = view->nlines;
3976 s->matched_line = 0;
3977 diff_view_indicate_progress(view);
3978 err = create_diff(s);
3982 s->first_displayed_line = 1;
3989 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3995 if (s->first_displayed_line > 1)
3996 s->first_displayed_line--;
4004 if (s->first_displayed_line == 1)
4007 while (i++ < nscroll && s->first_displayed_line > 1)
4008 s->first_displayed_line--;
4014 s->first_displayed_line++;
4026 while (!s->eof && i++ < nscroll) {
4027 linelen = getline(&line, &linesize, s->f);
4028 s->first_displayed_line++;
4029 if (linelen == -1) {
4033 err = got_ferror(s->f, GOT_ERR_IO);
4040 if (s->diff_context > 0) {
4042 s->matched_line = 0;
4043 diff_view_indicate_progress(view);
4044 err = create_diff(s);
4045 if (s->first_displayed_line + view->nlines - 1 >
4047 s->first_displayed_line = 1;
4048 s->last_displayed_line = view->nlines;
4053 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4055 s->matched_line = 0;
4056 diff_view_indicate_progress(view);
4057 err = create_diff(s);
4062 if (s->log_view == NULL)
4064 ls = &s->log_view->state.log;
4065 old_selected_entry = ls->selected_entry;
4067 err = input_log_view(NULL, s->log_view, KEY_UP);
4071 if (old_selected_entry == ls->selected_entry)
4074 err = set_selected_commit(s, ls->selected_entry);
4078 s->first_displayed_line = 1;
4079 s->last_displayed_line = view->nlines;
4080 s->matched_line = 0;
4083 diff_view_indicate_progress(view);
4084 err = create_diff(s);
4088 if (s->log_view == NULL)
4090 ls = &s->log_view->state.log;
4091 old_selected_entry = ls->selected_entry;
4093 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4097 if (old_selected_entry == ls->selected_entry)
4100 err = set_selected_commit(s, ls->selected_entry);
4104 s->first_displayed_line = 1;
4105 s->last_displayed_line = view->nlines;
4106 s->matched_line = 0;
4109 diff_view_indicate_progress(view);
4110 err = create_diff(s);
4119 static const struct got_error *
4120 cmd_diff(int argc, char *argv[])
4122 const struct got_error *error = NULL;
4123 struct got_repository *repo = NULL;
4124 struct got_worktree *worktree = NULL;
4125 struct got_object_id *id1 = NULL, *id2 = NULL;
4126 char *repo_path = NULL, *cwd = NULL;
4127 char *id_str1 = NULL, *id_str2 = NULL;
4128 char *label1 = NULL, *label2 = NULL;
4129 int diff_context = 3, ignore_whitespace = 0;
4130 int ch, force_text_diff = 0;
4132 struct tog_view *view;
4133 int *pack_fds = NULL;
4135 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4138 force_text_diff = 1;
4141 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4144 errx(1, "number of context lines is %s: %s",
4148 repo_path = realpath(optarg, NULL);
4149 if (repo_path == NULL)
4150 return got_error_from_errno2("realpath",
4152 got_path_strip_trailing_slashes(repo_path);
4155 ignore_whitespace = 1;
4167 usage_diff(); /* TODO show local worktree changes */
4168 } else if (argc == 2) {
4174 error = got_repo_pack_fds_open(&pack_fds);
4178 if (repo_path == NULL) {
4179 cwd = getcwd(NULL, 0);
4181 return got_error_from_errno("getcwd");
4182 error = got_worktree_open(&worktree, cwd);
4183 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4187 strdup(got_worktree_get_repo_path(worktree));
4189 repo_path = strdup(cwd);
4190 if (repo_path == NULL) {
4191 error = got_error_from_errno("strdup");
4196 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4202 error = apply_unveil(got_repo_get_path(repo), NULL);
4206 error = tog_load_refs(repo, 0);
4210 error = got_repo_match_object_id(&id1, &label1, id_str1,
4211 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4215 error = got_repo_match_object_id(&id2, &label2, id_str2,
4216 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4220 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4222 error = got_error_from_errno("view_open");
4225 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4226 ignore_whitespace, force_text_diff, NULL, repo);
4229 error = view_loop(view);
4236 const struct got_error *close_err = got_repo_close(repo);
4241 got_worktree_close(worktree);
4243 const struct got_error *pack_err =
4244 got_repo_pack_fds_close(pack_fds);
4256 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4261 struct tog_blame_line {
4263 struct got_object_id *id;
4266 static const struct got_error *
4267 draw_blame(struct tog_view *view)
4269 struct tog_blame_view_state *s = &view->state.blame;
4270 struct tog_blame *blame = &s->blame;
4271 regmatch_t *regmatch = &view->regmatch;
4272 const struct got_error *err;
4273 int lineno = 0, nprinted = 0, i;
4275 size_t linesize = 0;
4279 struct tog_blame_line *blame_line;
4280 struct got_object_id *prev_id = NULL;
4282 struct tog_color *tc;
4284 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4289 werase(view->window);
4291 if (asprintf(&line, "commit %s", id_str) == -1) {
4292 err = got_error_from_errno("asprintf");
4297 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4302 if (view_needs_focus_indication(view))
4303 wstandout(view->window);
4304 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4306 wattr_on(view->window,
4307 COLOR_PAIR(tc->colorpair), NULL);
4308 waddwstr(view->window, wline);
4310 wattr_off(view->window,
4311 COLOR_PAIR(tc->colorpair), NULL);
4312 if (view_needs_focus_indication(view))
4313 wstandend(view->window);
4316 if (width < view->ncols - 1)
4317 waddch(view->window, '\n');
4319 if (asprintf(&line, "[%d/%d] %s%s",
4320 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4321 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4323 return got_error_from_errno("asprintf");
4326 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4331 waddwstr(view->window, wline);
4334 if (width < view->ncols - 1)
4335 waddch(view->window, '\n');
4339 while (nprinted < view->nlines - 2) {
4340 linelen = getline(&line, &linesize, blame->f);
4341 if (linelen == -1) {
4342 if (feof(blame->f)) {
4347 return got_ferror(blame->f, GOT_ERR_IO);
4349 if (++lineno < s->first_displayed_line)
4352 view->maxx = MAX(view->maxx, linelen);
4354 if (view->focussed && nprinted == s->selected_line - 1)
4355 wstandout(view->window);
4357 if (blame->nlines > 0) {
4358 blame_line = &blame->lines[lineno - 1];
4359 if (blame_line->annotated && prev_id &&
4360 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4362 nprinted == s->selected_line - 1)) {
4363 waddstr(view->window, " ");
4364 } else if (blame_line->annotated) {
4366 err = got_object_id_str(&id_str, blame_line->id);
4371 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4373 wattr_on(view->window,
4374 COLOR_PAIR(tc->colorpair), NULL);
4375 wprintw(view->window, "%.8s", id_str);
4377 wattr_off(view->window,
4378 COLOR_PAIR(tc->colorpair), NULL);
4380 prev_id = blame_line->id;
4382 waddstr(view->window, "........");
4386 waddstr(view->window, "........");
4390 if (view->focussed && nprinted == s->selected_line - 1)
4391 wstandend(view->window);
4392 waddstr(view->window, " ");
4394 if (view->ncols <= 9) {
4396 wline = wcsdup(L"");
4397 if (wline == NULL) {
4398 err = got_error_from_errno("wcsdup");
4402 } else if (s->first_displayed_line + nprinted ==
4404 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4405 err = add_matched_line(&width, line, view->ncols - 9, 9,
4406 view->window, view->x, regmatch);
4413 err = format_line(&wline, &width, NULL, line, 0,
4414 view->x + view->ncols - 9, 9, 1);
4419 if (view->x < width) {
4420 waddwstr(view->window, wline + view->x);
4421 for (i = 0; i < view->x; i++)
4422 width -= wcwidth(wline[i]);
4429 if (width <= view->ncols - 1)
4430 waddch(view->window, '\n');
4431 if (++nprinted == 1)
4432 s->first_displayed_line = lineno;
4435 s->last_displayed_line = lineno;
4442 static const struct got_error *
4443 blame_cb(void *arg, int nlines, int lineno,
4444 struct got_commit_object *commit, struct got_object_id *id)
4446 const struct got_error *err = NULL;
4447 struct tog_blame_cb_args *a = arg;
4448 struct tog_blame_line *line;
4451 if (nlines != a->nlines ||
4452 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4453 return got_error(GOT_ERR_RANGE);
4455 errcode = pthread_mutex_lock(&tog_mutex);
4457 return got_error_set_errno(errcode, "pthread_mutex_lock");
4459 if (*a->quit) { /* user has quit the blame view */
4460 err = got_error(GOT_ERR_ITER_COMPLETED);
4465 goto done; /* no change in this commit */
4467 line = &a->lines[lineno - 1];
4468 if (line->annotated)
4471 line->id = got_object_id_dup(id);
4472 if (line->id == NULL) {
4473 err = got_error_from_errno("got_object_id_dup");
4476 line->annotated = 1;
4478 errcode = pthread_mutex_unlock(&tog_mutex);
4480 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4485 blame_thread(void *arg)
4487 const struct got_error *err, *close_err;
4488 struct tog_blame_thread_args *ta = arg;
4489 struct tog_blame_cb_args *a = ta->cb_args;
4492 err = block_signals_used_by_main_thread();
4496 err = got_blame(ta->path, a->commit_id, ta->repo,
4497 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4498 if (err && err->code == GOT_ERR_CANCELLED)
4501 errcode = pthread_mutex_lock(&tog_mutex);
4503 return (void *)got_error_set_errno(errcode,
4504 "pthread_mutex_lock");
4506 close_err = got_repo_close(ta->repo);
4512 errcode = pthread_mutex_unlock(&tog_mutex);
4513 if (errcode && err == NULL)
4514 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4519 static struct got_object_id *
4520 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4521 int first_displayed_line, int selected_line)
4523 struct tog_blame_line *line;
4528 line = &lines[first_displayed_line - 1 + selected_line - 1];
4529 if (!line->annotated)
4535 static const struct got_error *
4536 stop_blame(struct tog_blame *blame)
4538 const struct got_error *err = NULL;
4541 if (blame->thread) {
4543 errcode = pthread_mutex_unlock(&tog_mutex);
4545 return got_error_set_errno(errcode,
4546 "pthread_mutex_unlock");
4547 errcode = pthread_join(blame->thread, (void **)&err);
4549 return got_error_set_errno(errcode, "pthread_join");
4550 errcode = pthread_mutex_lock(&tog_mutex);
4552 return got_error_set_errno(errcode,
4553 "pthread_mutex_lock");
4554 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4556 blame->thread = 0; //NULL;
4558 if (blame->thread_args.repo) {
4559 const struct got_error *close_err;
4560 close_err = got_repo_close(blame->thread_args.repo);
4563 blame->thread_args.repo = NULL;
4566 if (fclose(blame->f) == EOF && err == NULL)
4567 err = got_error_from_errno("fclose");
4571 for (i = 0; i < blame->nlines; i++)
4572 free(blame->lines[i].id);
4574 blame->lines = NULL;
4576 free(blame->cb_args.commit_id);
4577 blame->cb_args.commit_id = NULL;
4578 if (blame->pack_fds) {
4579 const struct got_error *pack_err =
4580 got_repo_pack_fds_close(blame->pack_fds);
4583 blame->pack_fds = NULL;
4588 static const struct got_error *
4589 cancel_blame_view(void *arg)
4591 const struct got_error *err = NULL;
4595 errcode = pthread_mutex_lock(&tog_mutex);
4597 return got_error_set_errno(errcode,
4598 "pthread_mutex_unlock");
4601 err = got_error(GOT_ERR_CANCELLED);
4603 errcode = pthread_mutex_unlock(&tog_mutex);
4605 return got_error_set_errno(errcode,
4606 "pthread_mutex_lock");
4611 static const struct got_error *
4612 run_blame(struct tog_view *view)
4614 struct tog_blame_view_state *s = &view->state.blame;
4615 struct tog_blame *blame = &s->blame;
4616 const struct got_error *err = NULL;
4617 struct got_commit_object *commit = NULL;
4618 struct got_blob_object *blob = NULL;
4619 struct got_repository *thread_repo = NULL;
4620 struct got_object_id *obj_id = NULL;
4622 int *pack_fds = NULL;
4624 err = got_object_open_as_commit(&commit, s->repo,
4625 &s->blamed_commit->id);
4629 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4633 err = got_object_get_type(&obj_type, s->repo, obj_id);
4637 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4638 err = got_error(GOT_ERR_OBJ_TYPE);
4642 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4645 blame->f = got_opentemp();
4646 if (blame->f == NULL) {
4647 err = got_error_from_errno("got_opentemp");
4650 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4651 &blame->line_offsets, blame->f, blob);
4654 if (blame->nlines == 0) {
4655 s->blame_complete = 1;
4659 /* Don't include \n at EOF in the blame line count. */
4660 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4663 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4664 if (blame->lines == NULL) {
4665 err = got_error_from_errno("calloc");
4669 err = got_repo_pack_fds_open(&pack_fds);
4672 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4677 blame->pack_fds = pack_fds;
4678 blame->cb_args.view = view;
4679 blame->cb_args.lines = blame->lines;
4680 blame->cb_args.nlines = blame->nlines;
4681 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4682 if (blame->cb_args.commit_id == NULL) {
4683 err = got_error_from_errno("got_object_id_dup");
4686 blame->cb_args.quit = &s->done;
4688 blame->thread_args.path = s->path;
4689 blame->thread_args.repo = thread_repo;
4690 blame->thread_args.cb_args = &blame->cb_args;
4691 blame->thread_args.complete = &s->blame_complete;
4692 blame->thread_args.cancel_cb = cancel_blame_view;
4693 blame->thread_args.cancel_arg = &s->done;
4694 s->blame_complete = 0;
4696 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4697 s->first_displayed_line = 1;
4698 s->last_displayed_line = view->nlines;
4699 s->selected_line = 1;
4701 s->matched_line = 0;
4705 got_object_commit_close(commit);
4707 got_object_blob_close(blob);
4714 static const struct got_error *
4715 open_blame_view(struct tog_view *view, char *path,
4716 struct got_object_id *commit_id, struct got_repository *repo)
4718 const struct got_error *err = NULL;
4719 struct tog_blame_view_state *s = &view->state.blame;
4721 STAILQ_INIT(&s->blamed_commits);
4723 s->path = strdup(path);
4724 if (s->path == NULL)
4725 return got_error_from_errno("strdup");
4727 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4733 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4734 s->first_displayed_line = 1;
4735 s->last_displayed_line = view->nlines;
4736 s->selected_line = 1;
4737 s->blame_complete = 0;
4739 s->commit_id = commit_id;
4740 memset(&s->blame, 0, sizeof(s->blame));
4742 STAILQ_INIT(&s->colors);
4743 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4744 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4745 get_color_value("TOG_COLOR_COMMIT"));
4750 view->show = show_blame_view;
4751 view->input = input_blame_view;
4752 view->close = close_blame_view;
4753 view->search_start = search_start_blame_view;
4754 view->search_next = search_next_blame_view;
4756 return run_blame(view);
4759 static const struct got_error *
4760 close_blame_view(struct tog_view *view)
4762 const struct got_error *err = NULL;
4763 struct tog_blame_view_state *s = &view->state.blame;
4765 if (s->blame.thread)
4766 err = stop_blame(&s->blame);
4768 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4769 struct got_object_qid *blamed_commit;
4770 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4771 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4772 got_object_qid_free(blamed_commit);
4776 free_colors(&s->colors);
4780 static const struct got_error *
4781 search_start_blame_view(struct tog_view *view)
4783 struct tog_blame_view_state *s = &view->state.blame;
4785 s->matched_line = 0;
4789 static const struct got_error *
4790 search_next_blame_view(struct tog_view *view)
4792 struct tog_blame_view_state *s = &view->state.blame;
4793 const struct got_error *err = NULL;
4795 char *exstr = NULL, *line = NULL;
4796 size_t linesize = 0;
4799 if (!view->searching) {
4800 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4804 if (s->matched_line) {
4805 if (view->searching == TOG_SEARCH_FORWARD)
4806 lineno = s->matched_line + 1;
4808 lineno = s->matched_line - 1;
4810 lineno = s->first_displayed_line - 1 + s->selected_line;
4815 if (lineno <= 0 || lineno > s->blame.nlines) {
4816 if (s->matched_line == 0) {
4817 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4821 if (view->searching == TOG_SEARCH_FORWARD)
4824 lineno = s->blame.nlines;
4827 offset = s->blame.line_offsets[lineno - 1];
4828 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4830 return got_error_from_errno("fseeko");
4832 linelen = getline(&line, &linesize, s->blame.f);
4833 err = expand_tab(&exstr, line);
4836 if (linelen != -1 &&
4837 match_line(exstr, &view->regex, 1, &view->regmatch)) {
4838 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4839 s->matched_line = lineno;
4844 if (view->searching == TOG_SEARCH_FORWARD)
4852 if (s->matched_line) {
4853 s->first_displayed_line = s->matched_line;
4854 s->selected_line = 1;
4860 static const struct got_error *
4861 show_blame_view(struct tog_view *view)
4863 const struct got_error *err = NULL;
4864 struct tog_blame_view_state *s = &view->state.blame;
4867 if (s->blame.thread == 0 && !s->blame_complete) {
4868 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4869 &s->blame.thread_args);
4871 return got_error_set_errno(errcode, "pthread_create");
4873 halfdelay(1); /* fast refresh while annotating */
4876 if (s->blame_complete)
4877 halfdelay(10); /* disable fast refresh */
4879 err = draw_blame(view);
4885 static const struct got_error *
4886 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4888 const struct got_error *err = NULL, *thread_err = NULL;
4889 struct tog_view *diff_view;
4890 struct tog_blame_view_state *s = &view->state.blame;
4891 int begin_x = 0, nscroll = view->nlines - 2;
4898 view->x = MAX(view->maxx - view->ncols / 3, 0);
4902 if (view->x + view->ncols / 3 < view->maxx)
4903 view->x += 2; /* move two columns right */
4907 view->x -= MIN(view->x, 2); /* move two columns back */
4914 s->selected_line = 1;
4915 s->first_displayed_line = 1;
4919 if (s->blame.nlines < view->nlines - 2) {
4920 s->selected_line = s->blame.nlines;
4921 s->first_displayed_line = 1;
4923 s->selected_line = view->nlines - 2;
4924 s->first_displayed_line = s->blame.nlines -
4931 if (s->selected_line > 1)
4933 else if (s->selected_line == 1 &&
4934 s->first_displayed_line > 1)
4935 s->first_displayed_line--;
4943 if (s->first_displayed_line == 1) {
4944 s->selected_line = MAX(1, s->selected_line - nscroll);
4947 if (s->first_displayed_line > nscroll)
4948 s->first_displayed_line -= nscroll;
4950 s->first_displayed_line = 1;
4955 if (s->selected_line < view->nlines - 2 &&
4956 s->first_displayed_line +
4957 s->selected_line <= s->blame.nlines)
4959 else if (s->last_displayed_line <
4961 s->first_displayed_line++;
4965 struct got_object_id *id = NULL;
4966 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4967 s->first_displayed_line, s->selected_line);
4971 struct got_commit_object *commit, *pcommit;
4972 struct got_object_qid *pid;
4973 struct got_object_id *blob_id = NULL;
4975 err = got_object_open_as_commit(&commit,
4980 got_object_commit_get_parent_ids(commit));
4982 got_object_commit_close(commit);
4985 /* Check if path history ends here. */
4986 err = got_object_open_as_commit(&pcommit,
4990 err = got_object_id_by_path(&blob_id, s->repo,
4992 got_object_commit_close(pcommit);
4994 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4996 got_object_commit_close(commit);
4999 err = got_object_get_type(&obj_type, s->repo,
5002 /* Can't blame non-blob type objects. */
5003 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5004 got_object_commit_close(commit);
5007 err = got_object_qid_alloc(&s->blamed_commit,
5009 got_object_commit_close(commit);
5011 if (got_object_id_cmp(id,
5012 &s->blamed_commit->id) == 0)
5014 err = got_object_qid_alloc(&s->blamed_commit,
5020 thread_err = stop_blame(&s->blame);
5024 STAILQ_INSERT_HEAD(&s->blamed_commits,
5025 s->blamed_commit, entry);
5026 err = run_blame(view);
5032 struct got_object_qid *first;
5033 first = STAILQ_FIRST(&s->blamed_commits);
5034 if (!got_object_id_cmp(&first->id, s->commit_id))
5037 thread_err = stop_blame(&s->blame);
5041 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5042 got_object_qid_free(s->blamed_commit);
5044 STAILQ_FIRST(&s->blamed_commits);
5045 err = run_blame(view);
5052 struct got_object_id *id = NULL;
5053 struct got_object_qid *pid;
5054 struct got_commit_object *commit = NULL;
5055 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5056 s->first_displayed_line, s->selected_line);
5059 err = got_object_open_as_commit(&commit, s->repo, id);
5063 got_object_commit_get_parent_ids(commit));
5064 if (view_is_parent_view(view))
5065 begin_x = view_split_begin_x(view->begin_x);
5066 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5067 if (diff_view == NULL) {
5068 got_object_commit_close(commit);
5069 err = got_error_from_errno("view_open");
5072 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5073 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5074 got_object_commit_close(commit);
5076 view_close(diff_view);
5080 diff_view->focussed = 1;
5081 if (view_is_parent_view(view)) {
5082 err = view_close_child(view);
5085 view_set_child(view, diff_view);
5086 view->focus_child = 1;
5088 *new_view = diff_view;
5100 if (s->last_displayed_line >= s->blame.nlines &&
5101 s->selected_line >= MIN(s->blame.nlines,
5102 view->nlines - 2)) {
5105 if (s->last_displayed_line >= s->blame.nlines &&
5106 s->selected_line < view->nlines - 2) {
5108 MIN(nscroll, s->last_displayed_line -
5109 s->first_displayed_line - s->selected_line + 1);
5111 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5112 s->first_displayed_line += nscroll;
5114 s->first_displayed_line =
5115 s->blame.nlines - (view->nlines - 3);
5118 if (s->selected_line > view->nlines - 2) {
5119 s->selected_line = MIN(s->blame.nlines,
5126 return thread_err ? thread_err : err;
5129 static const struct got_error *
5130 cmd_blame(int argc, char *argv[])
5132 const struct got_error *error;
5133 struct got_repository *repo = NULL;
5134 struct got_worktree *worktree = NULL;
5135 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5136 char *link_target = NULL;
5137 struct got_object_id *commit_id = NULL;
5138 struct got_commit_object *commit = NULL;
5139 char *commit_id_str = NULL;
5141 struct tog_view *view;
5142 int *pack_fds = NULL;
5144 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5147 commit_id_str = optarg;
5150 repo_path = realpath(optarg, NULL);
5151 if (repo_path == NULL)
5152 return got_error_from_errno2("realpath",
5167 error = got_repo_pack_fds_open(&pack_fds);
5171 if (repo_path == NULL) {
5172 cwd = getcwd(NULL, 0);
5174 return got_error_from_errno("getcwd");
5175 error = got_worktree_open(&worktree, cwd);
5176 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5180 strdup(got_worktree_get_repo_path(worktree));
5182 repo_path = strdup(cwd);
5183 if (repo_path == NULL) {
5184 error = got_error_from_errno("strdup");
5189 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5193 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5200 error = apply_unveil(got_repo_get_path(repo), NULL);
5204 error = tog_load_refs(repo, 0);
5208 if (commit_id_str == NULL) {
5209 struct got_reference *head_ref;
5210 error = got_ref_open(&head_ref, repo, worktree ?
5211 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5214 error = got_ref_resolve(&commit_id, repo, head_ref);
5215 got_ref_close(head_ref);
5217 error = got_repo_match_object_id(&commit_id, NULL,
5218 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5223 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5225 error = got_error_from_errno("view_open");
5229 error = got_object_open_as_commit(&commit, repo, commit_id);
5233 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5238 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5243 /* Release work tree lock. */
5244 got_worktree_close(worktree);
5247 error = view_loop(view);
5255 got_object_commit_close(commit);
5257 got_worktree_close(worktree);
5259 const struct got_error *close_err = got_repo_close(repo);
5264 const struct got_error *pack_err =
5265 got_repo_pack_fds_close(pack_fds);
5273 static const struct got_error *
5274 draw_tree_entries(struct tog_view *view, const char *parent_path)
5276 struct tog_tree_view_state *s = &view->state.tree;
5277 const struct got_error *err = NULL;
5278 struct got_tree_entry *te;
5280 struct tog_color *tc;
5281 int width, n, i, nentries;
5282 int limit = view->nlines;
5286 werase(view->window);
5291 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5295 if (view_needs_focus_indication(view))
5296 wstandout(view->window);
5297 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5299 wattr_on(view->window,
5300 COLOR_PAIR(tc->colorpair), NULL);
5301 waddwstr(view->window, wline);
5303 wattr_off(view->window,
5304 COLOR_PAIR(tc->colorpair), NULL);
5305 if (view_needs_focus_indication(view))
5306 wstandend(view->window);
5309 if (width < view->ncols - 1)
5310 waddch(view->window, '\n');
5313 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5317 waddwstr(view->window, wline);
5320 if (width < view->ncols - 1)
5321 waddch(view->window, '\n');
5324 waddch(view->window, '\n');
5328 if (s->first_displayed_entry == NULL) {
5329 te = got_object_tree_get_first_entry(s->tree);
5330 if (s->selected == 0) {
5332 wstandout(view->window);
5333 s->selected_entry = NULL;
5335 waddstr(view->window, " ..\n"); /* parent directory */
5336 if (s->selected == 0 && view->focussed)
5337 wstandend(view->window);
5344 te = s->first_displayed_entry;
5347 nentries = got_object_tree_get_nentries(s->tree);
5348 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5349 char *line = NULL, *id_str = NULL, *link_target = NULL;
5350 const char *modestr = "";
5353 te = got_object_tree_get_entry(s->tree, i);
5354 mode = got_tree_entry_get_mode(te);
5357 err = got_object_id_str(&id_str,
5358 got_tree_entry_get_id(te));
5360 return got_error_from_errno(
5361 "got_object_id_str");
5363 if (got_object_tree_entry_is_submodule(te))
5365 else if (S_ISLNK(mode)) {
5368 err = got_tree_entry_get_symlink_target(&link_target,
5374 for (i = 0; i < strlen(link_target); i++) {
5375 if (!isprint((unsigned char)link_target[i]))
5376 link_target[i] = '?';
5380 else if (S_ISDIR(mode))
5382 else if (mode & S_IXUSR)
5384 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5385 got_tree_entry_get_name(te), modestr,
5386 link_target ? " -> ": "",
5387 link_target ? link_target : "") == -1) {
5390 return got_error_from_errno("asprintf");
5394 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5400 if (n == s->selected) {
5402 wstandout(view->window);
5403 s->selected_entry = te;
5405 tc = match_color(&s->colors, line);
5407 wattr_on(view->window,
5408 COLOR_PAIR(tc->colorpair), NULL);
5409 waddwstr(view->window, wline);
5411 wattr_off(view->window,
5412 COLOR_PAIR(tc->colorpair), NULL);
5413 if (width < view->ncols - 1)
5414 waddch(view->window, '\n');
5415 if (n == s->selected && view->focussed)
5416 wstandend(view->window);
5422 s->last_displayed_entry = te;
5431 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5433 struct got_tree_entry *te;
5434 int isroot = s->tree == s->root;
5437 if (s->first_displayed_entry == NULL)
5440 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5441 while (i++ < maxscroll) {
5444 s->first_displayed_entry = NULL;
5447 s->first_displayed_entry = te;
5448 te = got_tree_entry_get_prev(s->tree, te);
5453 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5455 struct got_tree_entry *next, *last;
5458 if (s->first_displayed_entry)
5459 next = got_tree_entry_get_next(s->tree,
5460 s->first_displayed_entry);
5462 next = got_object_tree_get_first_entry(s->tree);
5464 last = s->last_displayed_entry;
5465 while (next && last && n++ < maxscroll) {
5466 last = got_tree_entry_get_next(s->tree, last);
5468 s->first_displayed_entry = next;
5469 next = got_tree_entry_get_next(s->tree, next);
5474 static const struct got_error *
5475 tree_entry_path(char **path, struct tog_parent_trees *parents,
5476 struct got_tree_entry *te)
5478 const struct got_error *err = NULL;
5479 struct tog_parent_tree *pt;
5480 size_t len = 2; /* for leading slash and NUL */
5482 TAILQ_FOREACH(pt, parents, entry)
5483 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5486 len += strlen(got_tree_entry_get_name(te));
5488 *path = calloc(1, len);
5490 return got_error_from_errno("calloc");
5493 pt = TAILQ_LAST(parents, tog_parent_trees);
5495 const char *name = got_tree_entry_get_name(pt->selected_entry);
5496 if (strlcat(*path, name, len) >= len) {
5497 err = got_error(GOT_ERR_NO_SPACE);
5500 if (strlcat(*path, "/", len) >= len) {
5501 err = got_error(GOT_ERR_NO_SPACE);
5504 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5507 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5508 err = got_error(GOT_ERR_NO_SPACE);
5520 static const struct got_error *
5521 blame_tree_entry(struct tog_view **new_view, int begin_x,
5522 struct got_tree_entry *te, struct tog_parent_trees *parents,
5523 struct got_object_id *commit_id, struct got_repository *repo)
5525 const struct got_error *err = NULL;
5527 struct tog_view *blame_view;
5531 err = tree_entry_path(&path, parents, te);
5535 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5536 if (blame_view == NULL) {
5537 err = got_error_from_errno("view_open");
5541 err = open_blame_view(blame_view, path, commit_id, repo);
5543 if (err->code == GOT_ERR_CANCELLED)
5545 view_close(blame_view);
5547 *new_view = blame_view;
5553 static const struct got_error *
5554 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5555 struct tog_tree_view_state *s)
5557 struct tog_view *log_view;
5558 const struct got_error *err = NULL;
5563 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5564 if (log_view == NULL)
5565 return got_error_from_errno("view_open");
5567 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5571 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5574 view_close(log_view);
5576 *new_view = log_view;
5581 static const struct got_error *
5582 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5583 const char *head_ref_name, struct got_repository *repo)
5585 const struct got_error *err = NULL;
5586 char *commit_id_str = NULL;
5587 struct tog_tree_view_state *s = &view->state.tree;
5588 struct got_commit_object *commit = NULL;
5590 TAILQ_INIT(&s->parents);
5591 STAILQ_INIT(&s->colors);
5593 s->commit_id = got_object_id_dup(commit_id);
5594 if (s->commit_id == NULL)
5595 return got_error_from_errno("got_object_id_dup");
5597 err = got_object_open_as_commit(&commit, repo, commit_id);
5602 * The root is opened here and will be closed when the view is closed.
5603 * Any visited subtrees and their path-wise parents are opened and
5606 err = got_object_open_as_tree(&s->root, repo,
5607 got_object_commit_get_tree_id(commit));
5612 err = got_object_id_str(&commit_id_str, commit_id);
5616 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5617 err = got_error_from_errno("asprintf");
5621 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5622 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5623 if (head_ref_name) {
5624 s->head_ref_name = strdup(head_ref_name);
5625 if (s->head_ref_name == NULL) {
5626 err = got_error_from_errno("strdup");
5632 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5633 err = add_color(&s->colors, "\\$$",
5634 TOG_COLOR_TREE_SUBMODULE,
5635 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5638 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5639 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5642 err = add_color(&s->colors, "/$",
5643 TOG_COLOR_TREE_DIRECTORY,
5644 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5648 err = add_color(&s->colors, "\\*$",
5649 TOG_COLOR_TREE_EXECUTABLE,
5650 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5654 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5655 get_color_value("TOG_COLOR_COMMIT"));
5660 view->show = show_tree_view;
5661 view->input = input_tree_view;
5662 view->close = close_tree_view;
5663 view->search_start = search_start_tree_view;
5664 view->search_next = search_next_tree_view;
5666 free(commit_id_str);
5668 got_object_commit_close(commit);
5670 close_tree_view(view);
5674 static const struct got_error *
5675 close_tree_view(struct tog_view *view)
5677 struct tog_tree_view_state *s = &view->state.tree;
5679 free_colors(&s->colors);
5680 free(s->tree_label);
5681 s->tree_label = NULL;
5683 s->commit_id = NULL;
5684 free(s->head_ref_name);
5685 s->head_ref_name = NULL;
5686 while (!TAILQ_EMPTY(&s->parents)) {
5687 struct tog_parent_tree *parent;
5688 parent = TAILQ_FIRST(&s->parents);
5689 TAILQ_REMOVE(&s->parents, parent, entry);
5690 if (parent->tree != s->root)
5691 got_object_tree_close(parent->tree);
5695 if (s->tree != NULL && s->tree != s->root)
5696 got_object_tree_close(s->tree);
5698 got_object_tree_close(s->root);
5702 static const struct got_error *
5703 search_start_tree_view(struct tog_view *view)
5705 struct tog_tree_view_state *s = &view->state.tree;
5707 s->matched_entry = NULL;
5712 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5714 regmatch_t regmatch;
5716 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5720 static const struct got_error *
5721 search_next_tree_view(struct tog_view *view)
5723 struct tog_tree_view_state *s = &view->state.tree;
5724 struct got_tree_entry *te = NULL;
5726 if (!view->searching) {
5727 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5731 if (s->matched_entry) {
5732 if (view->searching == TOG_SEARCH_FORWARD) {
5733 if (s->selected_entry)
5734 te = got_tree_entry_get_next(s->tree,
5737 te = got_object_tree_get_first_entry(s->tree);
5739 if (s->selected_entry == NULL)
5740 te = got_object_tree_get_last_entry(s->tree);
5742 te = got_tree_entry_get_prev(s->tree,
5746 if (s->selected_entry)
5747 te = s->selected_entry;
5748 else if (view->searching == TOG_SEARCH_FORWARD)
5749 te = got_object_tree_get_first_entry(s->tree);
5751 te = got_object_tree_get_last_entry(s->tree);
5756 if (s->matched_entry == NULL) {
5757 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5760 if (view->searching == TOG_SEARCH_FORWARD)
5761 te = got_object_tree_get_first_entry(s->tree);
5763 te = got_object_tree_get_last_entry(s->tree);
5766 if (match_tree_entry(te, &view->regex)) {
5767 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5768 s->matched_entry = te;
5772 if (view->searching == TOG_SEARCH_FORWARD)
5773 te = got_tree_entry_get_next(s->tree, te);
5775 te = got_tree_entry_get_prev(s->tree, te);
5778 if (s->matched_entry) {
5779 s->first_displayed_entry = s->matched_entry;
5786 static const struct got_error *
5787 show_tree_view(struct tog_view *view)
5789 const struct got_error *err = NULL;
5790 struct tog_tree_view_state *s = &view->state.tree;
5793 err = tree_entry_path(&parent_path, &s->parents, NULL);
5797 err = draw_tree_entries(view, parent_path);
5804 static const struct got_error *
5805 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5807 const struct got_error *err = NULL;
5808 struct tog_tree_view_state *s = &view->state.tree;
5809 struct tog_view *log_view, *ref_view;
5810 struct got_tree_entry *te;
5811 int begin_x = 0, n, nscroll = view->nlines - 3;
5815 s->show_ids = !s->show_ids;
5818 if (!s->selected_entry)
5820 if (view_is_parent_view(view))
5821 begin_x = view_split_begin_x(view->begin_x);
5822 err = log_selected_tree_entry(&log_view, begin_x, s);
5824 log_view->focussed = 1;
5825 if (view_is_parent_view(view)) {
5826 err = view_close_child(view);
5829 view_set_child(view, log_view);
5830 view->focus_child = 1;
5832 *new_view = log_view;
5835 if (view_is_parent_view(view))
5836 begin_x = view_split_begin_x(view->begin_x);
5837 ref_view = view_open(view->nlines, view->ncols,
5838 view->begin_y, begin_x, TOG_VIEW_REF);
5839 if (ref_view == NULL)
5840 return got_error_from_errno("view_open");
5841 err = open_ref_view(ref_view, s->repo);
5843 view_close(ref_view);
5847 ref_view->focussed = 1;
5848 if (view_is_parent_view(view)) {
5849 err = view_close_child(view);
5852 view_set_child(view, ref_view);
5853 view->focus_child = 1;
5855 *new_view = ref_view;
5860 if (s->tree == s->root)
5861 s->first_displayed_entry =
5862 got_object_tree_get_first_entry(s->tree);
5864 s->first_displayed_entry = NULL;
5869 te = got_object_tree_get_last_entry(s->tree);
5870 for (n = 0; n < view->nlines - 3; n++) {
5872 if(s->tree != s->root) {
5873 s->first_displayed_entry = NULL;
5878 s->first_displayed_entry = te;
5879 te = got_tree_entry_get_prev(s->tree, te);
5882 s->selected = n - 1;
5887 if (s->selected > 0) {
5891 tree_scroll_up(s, 1);
5899 if (s->tree == s->root) {
5900 if (got_object_tree_get_first_entry(s->tree) ==
5901 s->first_displayed_entry)
5902 s->selected -= MIN(s->selected, nscroll);
5904 if (s->first_displayed_entry == NULL)
5905 s->selected -= MIN(s->selected, nscroll);
5907 tree_scroll_up(s, MAX(0, nscroll));
5912 if (s->selected < s->ndisplayed - 1) {
5916 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5918 /* can't scroll any further */
5920 tree_scroll_down(s, 1);
5928 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5930 /* can't scroll any further; move cursor down */
5931 if (s->selected < s->ndisplayed - 1)
5932 s->selected += MIN(nscroll,
5933 s->ndisplayed - s->selected - 1);
5936 tree_scroll_down(s, nscroll);
5941 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5942 struct tog_parent_tree *parent;
5943 /* user selected '..' */
5944 if (s->tree == s->root)
5946 parent = TAILQ_FIRST(&s->parents);
5947 TAILQ_REMOVE(&s->parents, parent,
5949 got_object_tree_close(s->tree);
5950 s->tree = parent->tree;
5951 s->first_displayed_entry =
5952 parent->first_displayed_entry;
5954 parent->selected_entry;
5955 s->selected = parent->selected;
5957 } else if (S_ISDIR(got_tree_entry_get_mode(
5958 s->selected_entry))) {
5959 struct got_tree_object *subtree;
5960 err = got_object_open_as_tree(&subtree, s->repo,
5961 got_tree_entry_get_id(s->selected_entry));
5964 err = tree_view_visit_subtree(s, subtree);
5966 got_object_tree_close(subtree);
5969 } else if (S_ISREG(got_tree_entry_get_mode(
5970 s->selected_entry))) {
5971 struct tog_view *blame_view;
5972 int begin_x = view_is_parent_view(view) ?
5973 view_split_begin_x(view->begin_x) : 0;
5975 err = blame_tree_entry(&blame_view, begin_x,
5976 s->selected_entry, &s->parents,
5977 s->commit_id, s->repo);
5981 blame_view->focussed = 1;
5982 if (view_is_parent_view(view)) {
5983 err = view_close_child(view);
5986 view_set_child(view, blame_view);
5987 view->focus_child = 1;
5989 *new_view = blame_view;
5993 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5994 s->selected = view->nlines - 4;
6007 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6012 static const struct got_error *
6013 cmd_tree(int argc, char *argv[])
6015 const struct got_error *error;
6016 struct got_repository *repo = NULL;
6017 struct got_worktree *worktree = NULL;
6018 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6019 struct got_object_id *commit_id = NULL;
6020 struct got_commit_object *commit = NULL;
6021 const char *commit_id_arg = NULL;
6023 struct got_reference *ref = NULL;
6024 const char *head_ref_name = NULL;
6026 struct tog_view *view;
6027 int *pack_fds = NULL;
6029 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6032 commit_id_arg = optarg;
6035 repo_path = realpath(optarg, NULL);
6036 if (repo_path == NULL)
6037 return got_error_from_errno2("realpath",
6052 error = got_repo_pack_fds_open(&pack_fds);
6056 if (repo_path == NULL) {
6057 cwd = getcwd(NULL, 0);
6059 return got_error_from_errno("getcwd");
6060 error = got_worktree_open(&worktree, cwd);
6061 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6065 strdup(got_worktree_get_repo_path(worktree));
6067 repo_path = strdup(cwd);
6068 if (repo_path == NULL) {
6069 error = got_error_from_errno("strdup");
6074 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6078 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6085 error = apply_unveil(got_repo_get_path(repo), NULL);
6089 error = tog_load_refs(repo, 0);
6093 if (commit_id_arg == NULL) {
6094 error = got_repo_match_object_id(&commit_id, &label,
6095 worktree ? got_worktree_get_head_ref_name(worktree) :
6096 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6099 head_ref_name = label;
6101 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6103 head_ref_name = got_ref_get_name(ref);
6104 else if (error->code != GOT_ERR_NOT_REF)
6106 error = got_repo_match_object_id(&commit_id, NULL,
6107 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6112 error = got_object_open_as_commit(&commit, repo, commit_id);
6116 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6118 error = got_error_from_errno("view_open");
6121 error = open_tree_view(view, commit_id, head_ref_name, repo);
6124 if (!got_path_is_root_dir(in_repo_path)) {
6125 error = tree_view_walk_path(&view->state.tree, commit,
6132 /* Release work tree lock. */
6133 got_worktree_close(worktree);
6136 error = view_loop(view);
6145 const struct got_error *close_err = got_repo_close(repo);
6150 const struct got_error *pack_err =
6151 got_repo_pack_fds_close(pack_fds);
6159 static const struct got_error *
6160 ref_view_load_refs(struct tog_ref_view_state *s)
6162 struct got_reflist_entry *sre;
6163 struct tog_reflist_entry *re;
6166 TAILQ_FOREACH(sre, &tog_refs, entry) {
6167 if (strncmp(got_ref_get_name(sre->ref),
6168 "refs/got/", 9) == 0 &&
6169 strncmp(got_ref_get_name(sre->ref),
6170 "refs/got/backup/", 16) != 0)
6173 re = malloc(sizeof(*re));
6175 return got_error_from_errno("malloc");
6177 re->ref = got_ref_dup(sre->ref);
6178 if (re->ref == NULL)
6179 return got_error_from_errno("got_ref_dup");
6180 re->idx = s->nrefs++;
6181 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6184 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6189 ref_view_free_refs(struct tog_ref_view_state *s)
6191 struct tog_reflist_entry *re;
6193 while (!TAILQ_EMPTY(&s->refs)) {
6194 re = TAILQ_FIRST(&s->refs);
6195 TAILQ_REMOVE(&s->refs, re, entry);
6196 got_ref_close(re->ref);
6201 static const struct got_error *
6202 open_ref_view(struct tog_view *view, struct got_repository *repo)
6204 const struct got_error *err = NULL;
6205 struct tog_ref_view_state *s = &view->state.ref;
6207 s->selected_entry = 0;
6210 TAILQ_INIT(&s->refs);
6211 STAILQ_INIT(&s->colors);
6213 err = ref_view_load_refs(s);
6217 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6218 err = add_color(&s->colors, "^refs/heads/",
6219 TOG_COLOR_REFS_HEADS,
6220 get_color_value("TOG_COLOR_REFS_HEADS"));
6224 err = add_color(&s->colors, "^refs/tags/",
6225 TOG_COLOR_REFS_TAGS,
6226 get_color_value("TOG_COLOR_REFS_TAGS"));
6230 err = add_color(&s->colors, "^refs/remotes/",
6231 TOG_COLOR_REFS_REMOTES,
6232 get_color_value("TOG_COLOR_REFS_REMOTES"));
6236 err = add_color(&s->colors, "^refs/got/backup/",
6237 TOG_COLOR_REFS_BACKUP,
6238 get_color_value("TOG_COLOR_REFS_BACKUP"));
6243 view->show = show_ref_view;
6244 view->input = input_ref_view;
6245 view->close = close_ref_view;
6246 view->search_start = search_start_ref_view;
6247 view->search_next = search_next_ref_view;
6250 free_colors(&s->colors);
6254 static const struct got_error *
6255 close_ref_view(struct tog_view *view)
6257 struct tog_ref_view_state *s = &view->state.ref;
6259 ref_view_free_refs(s);
6260 free_colors(&s->colors);
6265 static const struct got_error *
6266 resolve_reflist_entry(struct got_object_id **commit_id,
6267 struct tog_reflist_entry *re, struct got_repository *repo)
6269 const struct got_error *err = NULL;
6270 struct got_object_id *obj_id;
6271 struct got_tag_object *tag = NULL;
6276 err = got_ref_resolve(&obj_id, repo, re->ref);
6280 err = got_object_get_type(&obj_type, repo, obj_id);
6285 case GOT_OBJ_TYPE_COMMIT:
6286 *commit_id = obj_id;
6288 case GOT_OBJ_TYPE_TAG:
6289 err = got_object_open_as_tag(&tag, repo, obj_id);
6293 err = got_object_get_type(&obj_type, repo,
6294 got_object_tag_get_object_id(tag));
6297 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6298 err = got_error(GOT_ERR_OBJ_TYPE);
6301 *commit_id = got_object_id_dup(
6302 got_object_tag_get_object_id(tag));
6303 if (*commit_id == NULL) {
6304 err = got_error_from_errno("got_object_id_dup");
6309 err = got_error(GOT_ERR_OBJ_TYPE);
6315 got_object_tag_close(tag);
6323 static const struct got_error *
6324 log_ref_entry(struct tog_view **new_view, int begin_x,
6325 struct tog_reflist_entry *re, struct got_repository *repo)
6327 struct tog_view *log_view;
6328 const struct got_error *err = NULL;
6329 struct got_object_id *commit_id = NULL;
6333 err = resolve_reflist_entry(&commit_id, re, repo);
6335 if (err->code != GOT_ERR_OBJ_TYPE)
6341 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6342 if (log_view == NULL) {
6343 err = got_error_from_errno("view_open");
6347 err = open_log_view(log_view, commit_id, repo,
6348 got_ref_get_name(re->ref), "", 0);
6351 view_close(log_view);
6353 *new_view = log_view;
6359 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6361 struct tog_reflist_entry *re;
6364 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6367 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6368 while (i++ < maxscroll) {
6371 s->first_displayed_entry = re;
6372 re = TAILQ_PREV(re, tog_reflist_head, entry);
6377 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6379 struct tog_reflist_entry *next, *last;
6382 if (s->first_displayed_entry)
6383 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6385 next = TAILQ_FIRST(&s->refs);
6387 last = s->last_displayed_entry;
6388 while (next && last && n++ < maxscroll) {
6389 last = TAILQ_NEXT(last, entry);
6391 s->first_displayed_entry = next;
6392 next = TAILQ_NEXT(next, entry);
6397 static const struct got_error *
6398 search_start_ref_view(struct tog_view *view)
6400 struct tog_ref_view_state *s = &view->state.ref;
6402 s->matched_entry = NULL;
6407 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6409 regmatch_t regmatch;
6411 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6415 static const struct got_error *
6416 search_next_ref_view(struct tog_view *view)
6418 struct tog_ref_view_state *s = &view->state.ref;
6419 struct tog_reflist_entry *re = NULL;
6421 if (!view->searching) {
6422 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6426 if (s->matched_entry) {
6427 if (view->searching == TOG_SEARCH_FORWARD) {
6428 if (s->selected_entry)
6429 re = TAILQ_NEXT(s->selected_entry, entry);
6431 re = TAILQ_PREV(s->selected_entry,
6432 tog_reflist_head, entry);
6434 if (s->selected_entry == NULL)
6435 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6437 re = TAILQ_PREV(s->selected_entry,
6438 tog_reflist_head, entry);
6441 if (s->selected_entry)
6442 re = s->selected_entry;
6443 else if (view->searching == TOG_SEARCH_FORWARD)
6444 re = TAILQ_FIRST(&s->refs);
6446 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6451 if (s->matched_entry == NULL) {
6452 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6455 if (view->searching == TOG_SEARCH_FORWARD)
6456 re = TAILQ_FIRST(&s->refs);
6458 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6461 if (match_reflist_entry(re, &view->regex)) {
6462 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6463 s->matched_entry = re;
6467 if (view->searching == TOG_SEARCH_FORWARD)
6468 re = TAILQ_NEXT(re, entry);
6470 re = TAILQ_PREV(re, tog_reflist_head, entry);
6473 if (s->matched_entry) {
6474 s->first_displayed_entry = s->matched_entry;
6481 static const struct got_error *
6482 show_ref_view(struct tog_view *view)
6484 const struct got_error *err = NULL;
6485 struct tog_ref_view_state *s = &view->state.ref;
6486 struct tog_reflist_entry *re;
6489 struct tog_color *tc;
6491 int limit = view->nlines;
6493 werase(view->window);
6500 re = s->first_displayed_entry;
6502 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6504 return got_error_from_errno("asprintf");
6506 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6511 if (view_needs_focus_indication(view))
6512 wstandout(view->window);
6513 waddwstr(view->window, wline);
6514 if (view_needs_focus_indication(view))
6515 wstandend(view->window);
6520 if (width < view->ncols - 1)
6521 waddch(view->window, '\n');
6526 while (re && limit > 0) {
6528 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6531 struct got_commit_object *ci;
6532 struct got_tag_object *tag;
6533 struct got_object_id *id;
6537 err = got_ref_resolve(&id, s->repo, re->ref);
6540 err = got_object_open_as_tag(&tag, s->repo, id);
6542 if (err->code != GOT_ERR_OBJ_TYPE) {
6546 err = got_object_open_as_commit(&ci, s->repo,
6552 t = got_object_commit_get_committer_time(ci);
6553 got_object_commit_close(ci);
6555 t = got_object_tag_get_tagger_time(tag);
6556 got_object_tag_close(tag);
6559 if (gmtime_r(&t, &tm) == NULL)
6560 return got_error_from_errno("gmtime_r");
6561 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6562 return got_error(GOT_ERR_NO_SPACE);
6564 if (got_ref_is_symbolic(re->ref)) {
6565 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6566 ymd : "", got_ref_get_name(re->ref),
6567 got_ref_get_symref_target(re->ref)) == -1)
6568 return got_error_from_errno("asprintf");
6569 } else if (s->show_ids) {
6570 struct got_object_id *id;
6572 err = got_ref_resolve(&id, s->repo, re->ref);
6575 err = got_object_id_str(&id_str, id);
6580 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6581 got_ref_get_name(re->ref), id_str) == -1) {
6582 err = got_error_from_errno("asprintf");
6589 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6590 got_ref_get_name(re->ref)) == -1)
6591 return got_error_from_errno("asprintf");
6593 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6599 if (n == s->selected) {
6601 wstandout(view->window);
6602 s->selected_entry = re;
6604 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6606 wattr_on(view->window,
6607 COLOR_PAIR(tc->colorpair), NULL);
6608 waddwstr(view->window, wline);
6610 wattr_off(view->window,
6611 COLOR_PAIR(tc->colorpair), NULL);
6612 if (width < view->ncols - 1)
6613 waddch(view->window, '\n');
6614 if (n == s->selected && view->focussed)
6615 wstandend(view->window);
6621 s->last_displayed_entry = re;
6624 re = TAILQ_NEXT(re, entry);
6631 static const struct got_error *
6632 browse_ref_tree(struct tog_view **new_view, int begin_x,
6633 struct tog_reflist_entry *re, struct got_repository *repo)
6635 const struct got_error *err = NULL;
6636 struct got_object_id *commit_id = NULL;
6637 struct tog_view *tree_view;
6641 err = resolve_reflist_entry(&commit_id, re, repo);
6643 if (err->code != GOT_ERR_OBJ_TYPE)
6650 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6651 if (tree_view == NULL) {
6652 err = got_error_from_errno("view_open");
6656 err = open_tree_view(tree_view, commit_id,
6657 got_ref_get_name(re->ref), repo);
6661 *new_view = tree_view;
6666 static const struct got_error *
6667 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6669 const struct got_error *err = NULL;
6670 struct tog_ref_view_state *s = &view->state.ref;
6671 struct tog_view *log_view, *tree_view;
6672 struct tog_reflist_entry *re;
6673 int begin_x = 0, n, nscroll = view->nlines - 1;
6677 s->show_ids = !s->show_ids;
6680 s->show_date = !s->show_date;
6683 s->sort_by_date = !s->sort_by_date;
6684 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6685 got_ref_cmp_by_commit_timestamp_descending :
6686 tog_ref_cmp_by_name, s->repo);
6689 got_reflist_object_id_map_free(tog_refs_idmap);
6690 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6691 &tog_refs, s->repo);
6694 ref_view_free_refs(s);
6695 err = ref_view_load_refs(s);
6699 if (!s->selected_entry)
6701 if (view_is_parent_view(view))
6702 begin_x = view_split_begin_x(view->begin_x);
6703 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6706 log_view->focussed = 1;
6707 if (view_is_parent_view(view)) {
6708 err = view_close_child(view);
6711 view_set_child(view, log_view);
6712 view->focus_child = 1;
6714 *new_view = log_view;
6717 if (!s->selected_entry)
6719 if (view_is_parent_view(view))
6720 begin_x = view_split_begin_x(view->begin_x);
6721 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6723 if (err || tree_view == NULL)
6726 tree_view->focussed = 1;
6727 if (view_is_parent_view(view)) {
6728 err = view_close_child(view);
6731 view_set_child(view, tree_view);
6732 view->focus_child = 1;
6734 *new_view = tree_view;
6739 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6744 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6745 for (n = 0; n < view->nlines - 1; n++) {
6748 s->first_displayed_entry = re;
6749 re = TAILQ_PREV(re, tog_reflist_head, entry);
6752 s->selected = n - 1;
6757 if (s->selected > 0) {
6761 ref_scroll_up(s, 1);
6769 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6770 s->selected -= MIN(nscroll, s->selected);
6771 ref_scroll_up(s, MAX(0, nscroll));
6776 if (s->selected < s->ndisplayed - 1) {
6780 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6781 /* can't scroll any further */
6783 ref_scroll_down(s, 1);
6791 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6792 /* can't scroll any further; move cursor down */
6793 if (s->selected < s->ndisplayed - 1)
6794 s->selected += MIN(nscroll,
6795 s->ndisplayed - s->selected - 1);
6798 ref_scroll_down(s, nscroll);
6802 err = tog_load_refs(s->repo, s->sort_by_date);
6805 ref_view_free_refs(s);
6806 err = ref_view_load_refs(s);
6809 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6810 s->selected = view->nlines - 2;
6823 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6828 static const struct got_error *
6829 cmd_ref(int argc, char *argv[])
6831 const struct got_error *error;
6832 struct got_repository *repo = NULL;
6833 struct got_worktree *worktree = NULL;
6834 char *cwd = NULL, *repo_path = NULL;
6836 struct tog_view *view;
6837 int *pack_fds = NULL;
6839 while ((ch = getopt(argc, argv, "r:")) != -1) {
6842 repo_path = realpath(optarg, NULL);
6843 if (repo_path == NULL)
6844 return got_error_from_errno2("realpath",
6859 error = got_repo_pack_fds_open(&pack_fds);
6863 if (repo_path == NULL) {
6864 cwd = getcwd(NULL, 0);
6866 return got_error_from_errno("getcwd");
6867 error = got_worktree_open(&worktree, cwd);
6868 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6872 strdup(got_worktree_get_repo_path(worktree));
6874 repo_path = strdup(cwd);
6875 if (repo_path == NULL) {
6876 error = got_error_from_errno("strdup");
6881 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6887 error = apply_unveil(got_repo_get_path(repo), NULL);
6891 error = tog_load_refs(repo, 0);
6895 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6897 error = got_error_from_errno("view_open");
6901 error = open_ref_view(view, repo);
6906 /* Release work tree lock. */
6907 got_worktree_close(worktree);
6910 error = view_loop(view);
6915 const struct got_error *close_err = got_repo_close(repo);
6920 const struct got_error *pack_err =
6921 got_repo_pack_fds_close(pack_fds);
6930 list_commands(FILE *fp)
6934 fprintf(fp, "commands:");
6935 for (i = 0; i < nitems(tog_commands); i++) {
6936 const struct tog_cmd *cmd = &tog_commands[i];
6937 fprintf(fp, " %s", cmd->name);
6943 usage(int hflag, int status)
6945 FILE *fp = (status == 0) ? stdout : stderr;
6947 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6950 fprintf(fp, "lazy usage: %s path\n", getprogname());
6957 make_argv(int argc, ...)
6965 argv = calloc(argc, sizeof(char *));
6968 for (i = 0; i < argc; i++) {
6969 argv[i] = strdup(va_arg(ap, char *));
6970 if (argv[i] == NULL)
6979 * Try to convert 'tog path' into a 'tog log path' command.
6980 * The user could simply have mistyped the command rather than knowingly
6981 * provided a path. So check whether argv[0] can in fact be resolved
6982 * to a path in the HEAD commit and print a special error if not.
6983 * This hack is for mpi@ <3
6985 static const struct got_error *
6986 tog_log_with_path(int argc, char *argv[])
6988 const struct got_error *error = NULL, *close_err;
6989 const struct tog_cmd *cmd = NULL;
6990 struct got_repository *repo = NULL;
6991 struct got_worktree *worktree = NULL;
6992 struct got_object_id *commit_id = NULL, *id = NULL;
6993 struct got_commit_object *commit = NULL;
6994 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6995 char *commit_id_str = NULL, **cmd_argv = NULL;
6996 int *pack_fds = NULL;
6998 cwd = getcwd(NULL, 0);
7000 return got_error_from_errno("getcwd");
7002 error = got_repo_pack_fds_open(&pack_fds);
7006 error = got_worktree_open(&worktree, cwd);
7007 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7011 repo_path = strdup(got_worktree_get_repo_path(worktree));
7013 repo_path = strdup(cwd);
7014 if (repo_path == NULL) {
7015 error = got_error_from_errno("strdup");
7019 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7023 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7028 error = tog_load_refs(repo, 0);
7031 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7032 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7033 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7038 got_worktree_close(worktree);
7042 error = got_object_open_as_commit(&commit, repo, commit_id);
7046 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7048 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7050 fprintf(stderr, "%s: '%s' is no known command or path\n",
7051 getprogname(), argv[0]);
7056 close_err = got_repo_close(repo);
7061 error = got_object_id_str(&commit_id_str, commit_id);
7065 cmd = &tog_commands[0]; /* log */
7067 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7068 error = cmd->cmd_main(argc, cmd_argv);
7071 close_err = got_repo_close(repo);
7076 got_object_commit_close(commit);
7078 got_worktree_close(worktree);
7080 const struct got_error *pack_err =
7081 got_repo_pack_fds_close(pack_fds);
7086 free(commit_id_str);
7093 for (i = 0; i < argc; i++)
7102 main(int argc, char *argv[])
7104 const struct got_error *error = NULL;
7105 const struct tog_cmd *cmd = NULL;
7106 int ch, hflag = 0, Vflag = 0;
7107 char **cmd_argv = NULL;
7108 static const struct option longopts[] = {
7109 { "version", no_argument, NULL, 'V' },
7113 setlocale(LC_CTYPE, "");
7115 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7135 got_version_print_str();
7140 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7148 /* Build an argument vector which runs a default command. */
7149 cmd = &tog_commands[0];
7151 cmd_argv = make_argv(argc, cmd->name);
7155 /* Did the user specify a command? */
7156 for (i = 0; i < nitems(tog_commands); i++) {
7157 if (strncmp(tog_commands[i].name, argv[0],
7158 strlen(argv[0])) == 0) {
7159 cmd = &tog_commands[i];
7168 /* No command specified; try log with a path */
7169 error = tog_log_with_path(argc, argv);
7174 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7181 for (i = 0; i < argc; i++)
7186 if (error && error->code != GOT_ERR_CANCELLED)
7187 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);