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;
350 struct commit_queue_entry **first_displayed_entry;
351 struct commit_queue_entry **selected_entry;
353 int *search_next_done;
357 struct tog_log_view_state {
358 struct commit_queue commits;
359 struct commit_queue_entry *first_displayed_entry;
360 struct commit_queue_entry *last_displayed_entry;
361 struct commit_queue_entry *selected_entry;
366 struct got_repository *repo;
367 struct got_object_id *start_id;
370 struct tog_log_thread_args thread_args;
371 struct commit_queue_entry *matched_entry;
372 struct commit_queue_entry *search_entry;
373 struct tog_colors colors;
376 #define TOG_COLOR_DIFF_MINUS 1
377 #define TOG_COLOR_DIFF_PLUS 2
378 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
379 #define TOG_COLOR_DIFF_META 4
380 #define TOG_COLOR_TREE_SUBMODULE 5
381 #define TOG_COLOR_TREE_SYMLINK 6
382 #define TOG_COLOR_TREE_DIRECTORY 7
383 #define TOG_COLOR_TREE_EXECUTABLE 8
384 #define TOG_COLOR_COMMIT 9
385 #define TOG_COLOR_AUTHOR 10
386 #define TOG_COLOR_DATE 11
387 #define TOG_COLOR_REFS_HEADS 12
388 #define TOG_COLOR_REFS_TAGS 13
389 #define TOG_COLOR_REFS_REMOTES 14
390 #define TOG_COLOR_REFS_BACKUP 15
392 struct tog_blame_cb_args {
393 struct tog_blame_line *lines; /* one per line */
396 struct tog_view *view;
397 struct got_object_id *commit_id;
401 struct tog_blame_thread_args {
403 struct got_repository *repo;
404 struct tog_blame_cb_args *cb_args;
406 got_cancel_cb cancel_cb;
413 struct tog_blame_line *lines;
417 struct tog_blame_thread_args thread_args;
418 struct tog_blame_cb_args cb_args;
422 struct tog_blame_view_state {
423 int first_displayed_line;
424 int last_displayed_line;
429 struct got_object_id_queue blamed_commits;
430 struct got_object_qid *blamed_commit;
432 struct got_repository *repo;
433 struct got_object_id *commit_id;
434 struct tog_blame blame;
436 struct tog_colors colors;
439 struct tog_parent_tree {
440 TAILQ_ENTRY(tog_parent_tree) entry;
441 struct got_tree_object *tree;
442 struct got_tree_entry *first_displayed_entry;
443 struct got_tree_entry *selected_entry;
447 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
449 struct tog_tree_view_state {
451 struct got_object_id *commit_id;/* commit which this tree belongs to */
452 struct got_tree_object *root; /* the commit's root tree entry */
453 struct got_tree_object *tree; /* currently displayed (sub-)tree */
454 struct got_tree_entry *first_displayed_entry;
455 struct got_tree_entry *last_displayed_entry;
456 struct got_tree_entry *selected_entry;
457 int ndisplayed, selected, show_ids;
458 struct tog_parent_trees parents; /* parent trees of current sub-tree */
460 struct got_repository *repo;
461 struct got_tree_entry *matched_entry;
462 struct tog_colors colors;
465 struct tog_reflist_entry {
466 TAILQ_ENTRY(tog_reflist_entry) entry;
467 struct got_reference *ref;
471 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
473 struct tog_ref_view_state {
474 struct tog_reflist_head refs;
475 struct tog_reflist_entry *first_displayed_entry;
476 struct tog_reflist_entry *last_displayed_entry;
477 struct tog_reflist_entry *selected_entry;
478 int nrefs, ndisplayed, selected, show_ids, sort_by_date;
479 struct got_repository *repo;
480 struct tog_reflist_entry *matched_entry;
481 struct tog_colors colors;
485 * We implement two types of views: parent views and child views.
487 * The 'Tab' key switches focus between a parent view and its child view.
488 * Child views are shown side-by-side to their parent view, provided
489 * there is enough screen estate.
491 * When a new view is opened from within a parent view, this new view
492 * becomes a child view of the parent view, replacing any existing child.
494 * When a new view is opened from within a child view, this new view
495 * becomes a parent view which will obscure the views below until the
496 * user quits the new parent view by typing 'q'.
498 * This list of views contains parent views only.
499 * Child views are only pointed to by their parent view.
501 TAILQ_HEAD(tog_view_list_head, tog_view);
504 TAILQ_ENTRY(tog_view) entry;
507 int nlines, ncols, begin_y, begin_x;
508 int lines, cols; /* copies of LINES and COLS */
509 int focussed; /* Only set on one parent or child view at a time. */
511 struct tog_view *parent;
512 struct tog_view *child;
515 * This flag is initially set on parent views when a new child view
516 * is created. It gets toggled when the 'Tab' key switches focus
517 * between parent and child.
518 * The flag indicates whether focus should be passed on to our child
519 * view if this parent view gets picked for focus after another parent
520 * view was closed. This prevents child views from losing focus in such
525 /* type-specific state */
526 enum tog_view_type type;
528 struct tog_diff_view_state diff;
529 struct tog_log_view_state log;
530 struct tog_blame_view_state blame;
531 struct tog_tree_view_state tree;
532 struct tog_ref_view_state ref;
535 const struct got_error *(*show)(struct tog_view *);
536 const struct got_error *(*input)(struct tog_view **,
537 struct tog_view *, int);
538 const struct got_error *(*close)(struct tog_view *);
540 const struct got_error *(*search_start)(struct tog_view *);
541 const struct got_error *(*search_next)(struct tog_view *);
544 #define TOG_SEARCH_FORWARD 1
545 #define TOG_SEARCH_BACKWARD 2
546 int search_next_done;
547 #define TOG_SEARCH_HAVE_MORE 1
548 #define TOG_SEARCH_NO_MORE 2
549 #define TOG_SEARCH_HAVE_NONE 3
554 static const struct got_error *open_diff_view(struct tog_view *,
555 struct got_object_id *, struct got_object_id *,
556 const char *, const char *, int, int, int, struct tog_view *,
557 struct got_repository *);
558 static const struct got_error *show_diff_view(struct tog_view *);
559 static const struct got_error *input_diff_view(struct tog_view **,
560 struct tog_view *, int);
561 static const struct got_error* close_diff_view(struct tog_view *);
562 static const struct got_error *search_start_diff_view(struct tog_view *);
563 static const struct got_error *search_next_diff_view(struct tog_view *);
565 static const struct got_error *open_log_view(struct tog_view *,
566 struct got_object_id *, struct got_repository *,
567 const char *, const char *, int);
568 static const struct got_error * show_log_view(struct tog_view *);
569 static const struct got_error *input_log_view(struct tog_view **,
570 struct tog_view *, int);
571 static const struct got_error *close_log_view(struct tog_view *);
572 static const struct got_error *search_start_log_view(struct tog_view *);
573 static const struct got_error *search_next_log_view(struct tog_view *);
575 static const struct got_error *open_blame_view(struct tog_view *, char *,
576 struct got_object_id *, struct got_repository *);
577 static const struct got_error *show_blame_view(struct tog_view *);
578 static const struct got_error *input_blame_view(struct tog_view **,
579 struct tog_view *, int);
580 static const struct got_error *close_blame_view(struct tog_view *);
581 static const struct got_error *search_start_blame_view(struct tog_view *);
582 static const struct got_error *search_next_blame_view(struct tog_view *);
584 static const struct got_error *open_tree_view(struct tog_view *,
585 struct got_object_id *, const char *, struct got_repository *);
586 static const struct got_error *show_tree_view(struct tog_view *);
587 static const struct got_error *input_tree_view(struct tog_view **,
588 struct tog_view *, int);
589 static const struct got_error *close_tree_view(struct tog_view *);
590 static const struct got_error *search_start_tree_view(struct tog_view *);
591 static const struct got_error *search_next_tree_view(struct tog_view *);
593 static const struct got_error *open_ref_view(struct tog_view *,
594 struct got_repository *);
595 static const struct got_error *show_ref_view(struct tog_view *);
596 static const struct got_error *input_ref_view(struct tog_view **,
597 struct tog_view *, int);
598 static const struct got_error *close_ref_view(struct tog_view *);
599 static const struct got_error *search_start_ref_view(struct tog_view *);
600 static const struct got_error *search_next_ref_view(struct tog_view *);
602 static volatile sig_atomic_t tog_sigwinch_received;
603 static volatile sig_atomic_t tog_sigpipe_received;
604 static volatile sig_atomic_t tog_sigcont_received;
607 tog_sigwinch(int signo)
609 tog_sigwinch_received = 1;
613 tog_sigpipe(int signo)
615 tog_sigpipe_received = 1;
619 tog_sigcont(int signo)
621 tog_sigcont_received = 1;
624 static const struct got_error *
625 view_close(struct tog_view *view)
627 const struct got_error *err = NULL;
630 view_close(view->child);
634 err = view->close(view);
636 del_panel(view->panel);
638 delwin(view->window);
643 static struct tog_view *
644 view_open(int nlines, int ncols, int begin_y, int begin_x,
645 enum tog_view_type type)
647 struct tog_view *view = calloc(1, sizeof(*view));
655 view->nlines = nlines ? nlines : LINES - begin_y;
656 view->ncols = ncols ? ncols : COLS - begin_x;
657 view->begin_y = begin_y;
658 view->begin_x = begin_x;
659 view->window = newwin(nlines, ncols, begin_y, begin_x);
660 if (view->window == NULL) {
664 view->panel = new_panel(view->window);
665 if (view->panel == NULL ||
666 set_panel_userptr(view->panel, view) != OK) {
671 keypad(view->window, TRUE);
676 view_split_begin_x(int begin_x)
678 if (begin_x > 0 || COLS < 120)
680 return (COLS - MAX(COLS / 2, 80));
683 static const struct got_error *view_resize(struct tog_view *);
685 static const struct got_error *
686 view_splitscreen(struct tog_view *view)
688 const struct got_error *err = NULL;
691 view->begin_x = view_split_begin_x(0);
692 view->nlines = LINES;
693 view->ncols = COLS - view->begin_x;
696 err = view_resize(view);
700 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
701 return got_error_from_errno("mvwin");
706 static const struct got_error *
707 view_fullscreen(struct tog_view *view)
709 const struct got_error *err = NULL;
713 view->nlines = LINES;
717 err = view_resize(view);
721 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
722 return got_error_from_errno("mvwin");
728 view_is_parent_view(struct tog_view *view)
730 return view->parent == NULL;
733 static const struct got_error *
734 view_resize(struct tog_view *view)
738 if (view->lines > LINES)
739 nlines = view->nlines - (view->lines - LINES);
741 nlines = view->nlines + (LINES - view->lines);
743 if (view->cols > COLS)
744 ncols = view->ncols - (view->cols - COLS);
746 ncols = view->ncols + (COLS - view->cols);
748 if (wresize(view->window, nlines, ncols) == ERR)
749 return got_error_from_errno("wresize");
750 if (replace_panel(view->panel, view->window) == ERR)
751 return got_error_from_errno("replace_panel");
752 wclear(view->window);
754 view->nlines = nlines;
760 view->child->begin_x = view_split_begin_x(view->begin_x);
761 if (view->child->begin_x == 0) {
762 view_fullscreen(view->child);
763 if (view->child->focussed)
764 show_panel(view->child->panel);
766 show_panel(view->panel);
768 view_splitscreen(view->child);
769 show_panel(view->child->panel);
776 static const struct got_error *
777 view_close_child(struct tog_view *view)
779 const struct got_error *err = NULL;
781 if (view->child == NULL)
784 err = view_close(view->child);
790 view_set_child(struct tog_view *view, struct tog_view *child)
793 child->parent = view;
797 view_is_splitscreen(struct tog_view *view)
799 return view->begin_x > 0;
808 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
809 cols = 80; /* Default */
815 resize_term(lines, cols);
818 static const struct got_error *
819 view_search_start(struct tog_view *view)
821 const struct got_error *err = NULL;
825 if (view->search_started) {
826 regfree(&view->regex);
828 memset(&view->regmatch, 0, sizeof(view->regmatch));
830 view->search_started = 0;
832 if (view->nlines < 1)
835 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
836 wclrtoeol(view->window);
840 ret = wgetnstr(view->window, pattern, sizeof(pattern));
846 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
847 err = view->search_start(view);
849 regfree(&view->regex);
852 view->search_started = 1;
853 view->searching = TOG_SEARCH_FORWARD;
854 view->search_next_done = 0;
855 view->search_next(view);
861 static const struct got_error *
862 view_input(struct tog_view **new, int *done, struct tog_view *view,
863 struct tog_view_list_head *views)
865 const struct got_error *err = NULL;
871 /* Clear "no matches" indicator. */
872 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
873 view->search_next_done == TOG_SEARCH_HAVE_NONE)
874 view->search_next_done = TOG_SEARCH_HAVE_MORE;
876 if (view->searching && !view->search_next_done) {
877 errcode = pthread_mutex_unlock(&tog_mutex);
879 return got_error_set_errno(errcode,
880 "pthread_mutex_unlock");
882 errcode = pthread_mutex_lock(&tog_mutex);
884 return got_error_set_errno(errcode,
885 "pthread_mutex_lock");
886 view->search_next(view);
890 nodelay(stdscr, FALSE);
891 /* Allow threads to make progress while we are waiting for input. */
892 errcode = pthread_mutex_unlock(&tog_mutex);
894 return got_error_set_errno(errcode, "pthread_mutex_unlock");
895 ch = wgetch(view->window);
896 errcode = pthread_mutex_lock(&tog_mutex);
898 return got_error_set_errno(errcode, "pthread_mutex_lock");
899 nodelay(stdscr, TRUE);
901 if (tog_sigwinch_received || tog_sigcont_received) {
903 tog_sigwinch_received = 0;
904 tog_sigcont_received = 0;
905 TAILQ_FOREACH(v, views, entry) {
906 err = view_resize(v);
909 err = v->input(new, v, KEY_RESIZE);
913 err = view_resize(v->child);
916 err = v->child->input(new, v->child,
928 view->child->focussed = 1;
929 view->focus_child = 1;
930 } else if (view->parent) {
932 view->parent->focussed = 1;
933 view->parent->focus_child = 0;
937 err = view->input(new, view, ch);
944 if (view_is_parent_view(view)) {
945 if (view->child == NULL)
947 if (view_is_splitscreen(view->child)) {
949 view->child->focussed = 1;
950 err = view_fullscreen(view->child);
952 err = view_splitscreen(view->child);
955 err = view->child->input(new, view->child,
958 if (view_is_splitscreen(view)) {
959 view->parent->focussed = 0;
961 err = view_fullscreen(view);
963 err = view_splitscreen(view);
967 err = view->input(new, view, KEY_RESIZE);
973 if (view->search_start)
974 view_search_start(view);
976 err = view->input(new, view, ch);
980 if (view->search_started && view->search_next) {
981 view->searching = (ch == 'n' ?
982 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
983 view->search_next_done = 0;
984 view->search_next(view);
986 err = view->input(new, view, ch);
989 err = view->input(new, view, ch);
997 view_vborder(struct tog_view *view)
1000 const struct tog_view *view_above;
1003 return view_vborder(view->parent);
1005 panel = panel_above(view->panel);
1009 view_above = panel_userptr(panel);
1010 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1011 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1015 view_needs_focus_indication(struct tog_view *view)
1017 if (view_is_parent_view(view)) {
1018 if (view->child == NULL || view->child->focussed)
1020 if (!view_is_splitscreen(view->child))
1022 } else if (!view_is_splitscreen(view))
1025 return view->focussed;
1028 static const struct got_error *
1029 view_loop(struct tog_view *view)
1031 const struct got_error *err = NULL;
1032 struct tog_view_list_head views;
1033 struct tog_view *new_view;
1034 int fast_refresh = 10;
1035 int done = 0, errcode;
1037 errcode = pthread_mutex_lock(&tog_mutex);
1039 return got_error_set_errno(errcode, "pthread_mutex_lock");
1042 TAILQ_INSERT_HEAD(&views, view, entry);
1045 err = view->show(view);
1050 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1051 /* Refresh fast during initialization, then become slower. */
1052 if (fast_refresh && fast_refresh-- == 0)
1053 halfdelay(10); /* switch to once per second */
1055 err = view_input(&new_view, &done, view, &views);
1059 struct tog_view *v, *prev = NULL;
1061 if (view_is_parent_view(view))
1062 prev = TAILQ_PREV(view, tog_view_list_head,
1064 else if (view->parent)
1065 prev = view->parent;
1068 view->parent->child = NULL;
1069 view->parent->focus_child = 0;
1071 TAILQ_REMOVE(&views, view, entry);
1073 err = view_close(view);
1078 TAILQ_FOREACH(v, &views, entry) {
1082 if (view == NULL && new_view == NULL) {
1083 /* No view has focus. Try to pick one. */
1086 else if (!TAILQ_EMPTY(&views)) {
1087 view = TAILQ_LAST(&views,
1088 tog_view_list_head);
1091 if (view->focus_child) {
1092 view->child->focussed = 1;
1100 struct tog_view *v, *t;
1101 /* Only allow one parent view per type. */
1102 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1103 if (v->type != new_view->type)
1105 TAILQ_REMOVE(&views, v, entry);
1106 err = view_close(v);
1111 TAILQ_INSERT_TAIL(&views, new_view, entry);
1115 if (view_is_parent_view(view)) {
1116 if (view->child && view->child->focussed)
1119 if (view->parent && view->parent->focussed)
1120 view = view->parent;
1122 show_panel(view->panel);
1123 if (view->child && view_is_splitscreen(view->child))
1124 show_panel(view->child->panel);
1125 if (view->parent && view_is_splitscreen(view)) {
1126 err = view->parent->show(view->parent);
1130 err = view->show(view);
1134 err = view->child->show(view->child);
1143 while (!TAILQ_EMPTY(&views)) {
1144 view = TAILQ_FIRST(&views);
1145 TAILQ_REMOVE(&views, view, entry);
1149 errcode = pthread_mutex_unlock(&tog_mutex);
1150 if (errcode && err == NULL)
1151 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1161 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1166 /* Create newly allocated wide-character string equivalent to a byte string. */
1167 static const struct got_error *
1168 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1171 const struct got_error *err = NULL;
1174 *wlen = mbstowcs(NULL, s, 0);
1175 if (*wlen == (size_t)-1) {
1177 if (errno != EILSEQ)
1178 return got_error_from_errno("mbstowcs");
1180 /* byte string invalid in current encoding; try to "fix" it */
1181 err = got_mbsavis(&vis, &vislen, s);
1184 *wlen = mbstowcs(NULL, vis, 0);
1185 if (*wlen == (size_t)-1) {
1186 err = got_error_from_errno("mbstowcs"); /* give up */
1191 *ws = calloc(*wlen + 1, sizeof(**ws));
1193 err = got_error_from_errno("calloc");
1197 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1198 err = got_error_from_errno("mbstowcs");
1209 /* Format a line for display, ensuring that it won't overflow a width limit. */
1210 static const struct got_error *
1211 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1214 const struct got_error *err = NULL;
1216 wchar_t *wline = NULL;
1223 err = mbs2ws(&wline, &wlen, line);
1227 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1228 wline[wlen - 1] = L'\0';
1231 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1232 wline[wlen - 1] = L'\0';
1238 int width = wcwidth(wline[i]);
1245 if (width == 1 || width == 2) {
1246 if (cols + width > wlimit)
1250 } else if (width == -1) {
1251 if (wline[i] == L'\t') {
1253 ((cols + col_tab_align) % TABSIZE);
1258 if (cols + width > wlimit)
1263 err = got_error_from_errno("wcwidth");
1278 static const struct got_error*
1279 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1280 struct got_object_id *id, struct got_repository *repo)
1282 static const struct got_error *err = NULL;
1283 struct got_reflist_entry *re;
1289 TAILQ_FOREACH(re, refs, entry) {
1290 struct got_tag_object *tag = NULL;
1291 struct got_object_id *ref_id;
1294 name = got_ref_get_name(re->ref);
1295 if (strcmp(name, GOT_REF_HEAD) == 0)
1297 if (strncmp(name, "refs/", 5) == 0)
1299 if (strncmp(name, "got/", 4) == 0 &&
1300 strncmp(name, "got/backup/", 11) != 0)
1302 if (strncmp(name, "heads/", 6) == 0)
1304 if (strncmp(name, "remotes/", 8) == 0) {
1306 s = strstr(name, "/" GOT_REF_HEAD);
1307 if (s != NULL && s[strlen(s)] == '\0')
1310 err = got_ref_resolve(&ref_id, repo, re->ref);
1313 if (strncmp(name, "tags/", 5) == 0) {
1314 err = got_object_open_as_tag(&tag, repo, ref_id);
1316 if (err->code != GOT_ERR_OBJ_TYPE) {
1320 /* Ref points at something other than a tag. */
1325 cmp = got_object_id_cmp(tag ?
1326 got_object_tag_get_object_id(tag) : ref_id, id);
1329 got_object_tag_close(tag);
1333 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1334 s ? ", " : "", name) == -1) {
1335 err = got_error_from_errno("asprintf");
1346 static const struct got_error *
1347 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1352 smallerthan = strchr(author, '<');
1353 if (smallerthan && smallerthan[1] != '\0')
1354 author = smallerthan + 1;
1355 author[strcspn(author, "@>")] = '\0';
1356 return format_line(wauthor, author_width, author, limit, col_tab_align);
1359 static const struct got_error *
1360 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1361 struct got_object_id *id, const size_t date_display_cols,
1362 int author_display_cols)
1364 struct tog_log_view_state *s = &view->state.log;
1365 const struct got_error *err = NULL;
1366 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1367 char *logmsg0 = NULL, *logmsg = NULL;
1368 char *author = NULL;
1369 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1370 int author_width, logmsg_width;
1371 char *newline, *line = NULL;
1373 const int avail = view->ncols;
1375 time_t committer_time;
1376 struct tog_color *tc;
1378 committer_time = got_object_commit_get_committer_time(commit);
1379 if (gmtime_r(&committer_time, &tm) == NULL)
1380 return got_error_from_errno("gmtime_r");
1381 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1382 return got_error(GOT_ERR_NO_SPACE);
1384 if (avail <= date_display_cols)
1385 limit = MIN(sizeof(datebuf) - 1, avail);
1387 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1388 tc = get_color(&s->colors, TOG_COLOR_DATE);
1390 wattr_on(view->window,
1391 COLOR_PAIR(tc->colorpair), NULL);
1392 waddnstr(view->window, datebuf, limit);
1394 wattr_off(view->window,
1395 COLOR_PAIR(tc->colorpair), NULL);
1402 err = got_object_id_str(&id_str, id);
1405 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1407 wattr_on(view->window,
1408 COLOR_PAIR(tc->colorpair), NULL);
1409 wprintw(view->window, "%.8s ", id_str);
1411 wattr_off(view->window,
1412 COLOR_PAIR(tc->colorpair), NULL);
1419 author = strdup(got_object_commit_get_author(commit));
1420 if (author == NULL) {
1421 err = got_error_from_errno("strdup");
1424 err = format_author(&wauthor, &author_width, author, avail - col, col);
1427 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1429 wattr_on(view->window,
1430 COLOR_PAIR(tc->colorpair), NULL);
1431 waddwstr(view->window, wauthor);
1433 wattr_off(view->window,
1434 COLOR_PAIR(tc->colorpair), NULL);
1435 col += author_width;
1436 while (col < avail && author_width < author_display_cols + 2) {
1437 waddch(view->window, ' ');
1444 err = got_object_commit_get_logmsg(&logmsg0, commit);
1448 while (*logmsg == '\n')
1450 newline = strchr(logmsg, '\n');
1453 limit = avail - col;
1454 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1457 waddwstr(view->window, wlogmsg);
1458 col += logmsg_width;
1459 while (col < avail) {
1460 waddch(view->window, ' ');
1472 static struct commit_queue_entry *
1473 alloc_commit_queue_entry(struct got_commit_object *commit,
1474 struct got_object_id *id)
1476 struct commit_queue_entry *entry;
1478 entry = calloc(1, sizeof(*entry));
1483 entry->commit = commit;
1488 pop_commit(struct commit_queue *commits)
1490 struct commit_queue_entry *entry;
1492 entry = TAILQ_FIRST(&commits->head);
1493 TAILQ_REMOVE(&commits->head, entry, entry);
1494 got_object_commit_close(entry->commit);
1495 commits->ncommits--;
1496 /* Don't free entry->id! It is owned by the commit graph. */
1501 free_commits(struct commit_queue *commits)
1503 while (!TAILQ_EMPTY(&commits->head))
1504 pop_commit(commits);
1507 static const struct got_error *
1508 match_commit(int *have_match, struct got_object_id *id,
1509 struct got_commit_object *commit, regex_t *regex)
1511 const struct got_error *err = NULL;
1512 regmatch_t regmatch;
1513 char *id_str = NULL, *logmsg = NULL;
1517 err = got_object_id_str(&id_str, id);
1521 err = got_object_commit_get_logmsg(&logmsg, commit);
1525 if (regexec(regex, got_object_commit_get_author(commit), 1,
1526 ®match, 0) == 0 ||
1527 regexec(regex, got_object_commit_get_committer(commit), 1,
1528 ®match, 0) == 0 ||
1529 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1530 regexec(regex, logmsg, 1, ®match, 0) == 0)
1538 static const struct got_error *
1539 queue_commits(struct tog_log_thread_args *a)
1541 const struct got_error *err = NULL;
1544 * We keep all commits open throughout the lifetime of the log
1545 * view in order to avoid having to re-fetch commits from disk
1546 * while updating the display.
1549 struct got_object_id *id;
1550 struct got_commit_object *commit;
1551 struct commit_queue_entry *entry;
1554 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1556 if (err || id == NULL)
1559 err = got_object_open_as_commit(&commit, a->repo, id);
1562 entry = alloc_commit_queue_entry(commit, id);
1563 if (entry == NULL) {
1564 err = got_error_from_errno("alloc_commit_queue_entry");
1568 errcode = pthread_mutex_lock(&tog_mutex);
1570 err = got_error_set_errno(errcode,
1571 "pthread_mutex_lock");
1575 entry->idx = a->commits->ncommits;
1576 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1577 a->commits->ncommits++;
1579 if (*a->searching == TOG_SEARCH_FORWARD &&
1580 !*a->search_next_done) {
1582 err = match_commit(&have_match, id, commit, a->regex);
1586 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1589 errcode = pthread_mutex_unlock(&tog_mutex);
1590 if (errcode && err == NULL)
1591 err = got_error_set_errno(errcode,
1592 "pthread_mutex_unlock");
1595 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1601 select_commit(struct tog_log_view_state *s)
1603 struct commit_queue_entry *entry;
1606 entry = s->first_displayed_entry;
1608 if (ncommits == s->selected) {
1609 s->selected_entry = entry;
1612 entry = TAILQ_NEXT(entry, entry);
1617 static const struct got_error *
1618 draw_commits(struct tog_view *view)
1620 const struct got_error *err = NULL;
1621 struct tog_log_view_state *s = &view->state.log;
1622 struct commit_queue_entry *entry = s->selected_entry;
1623 const int limit = view->nlines;
1625 int ncommits, author_cols = 4;
1626 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1627 char *refs_str = NULL;
1629 struct tog_color *tc;
1630 static const size_t date_display_cols = 12;
1632 if (s->selected_entry &&
1633 !(view->searching && view->search_next_done == 0)) {
1634 struct got_reflist_head *refs;
1635 err = got_object_id_str(&id_str, s->selected_entry->id);
1638 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1639 s->selected_entry->id);
1641 err = build_refs_str(&refs_str, refs,
1642 s->selected_entry->id, s->repo);
1648 if (s->thread_args.commits_needed == 0)
1649 halfdelay(10); /* disable fast refresh */
1651 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1652 if (asprintf(&ncommits_str, " [%d/%d] %s",
1653 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1654 (view->searching && !view->search_next_done) ?
1655 "searching..." : "loading...") == -1) {
1656 err = got_error_from_errno("asprintf");
1660 const char *search_str = NULL;
1662 if (view->searching) {
1663 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1664 search_str = "no more matches";
1665 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1666 search_str = "no matches found";
1667 else if (!view->search_next_done)
1668 search_str = "searching...";
1671 if (asprintf(&ncommits_str, " [%d/%d] %s",
1672 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1673 search_str ? search_str :
1674 (refs_str ? refs_str : "")) == -1) {
1675 err = got_error_from_errno("asprintf");
1680 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1681 if (asprintf(&header, "commit %s %s%s",
1682 id_str ? id_str : "........................................",
1683 s->in_repo_path, ncommits_str) == -1) {
1684 err = got_error_from_errno("asprintf");
1688 } else if (asprintf(&header, "commit %s%s",
1689 id_str ? id_str : "........................................",
1690 ncommits_str) == -1) {
1691 err = got_error_from_errno("asprintf");
1695 err = format_line(&wline, &width, header, view->ncols, 0);
1699 werase(view->window);
1701 if (view_needs_focus_indication(view))
1702 wstandout(view->window);
1703 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1705 wattr_on(view->window,
1706 COLOR_PAIR(tc->colorpair), NULL);
1707 waddwstr(view->window, wline);
1709 wattr_off(view->window,
1710 COLOR_PAIR(tc->colorpair), NULL);
1711 while (width < view->ncols) {
1712 waddch(view->window, ' ');
1715 if (view_needs_focus_indication(view))
1716 wstandend(view->window);
1721 /* Grow author column size if necessary. */
1722 entry = s->first_displayed_entry;
1728 if (ncommits >= limit - 1)
1730 author = strdup(got_object_commit_get_author(entry->commit));
1731 if (author == NULL) {
1732 err = got_error_from_errno("strdup");
1735 err = format_author(&wauthor, &width, author, COLS,
1737 if (author_cols < width)
1738 author_cols = width;
1742 entry = TAILQ_NEXT(entry, entry);
1745 entry = s->first_displayed_entry;
1746 s->last_displayed_entry = s->first_displayed_entry;
1749 if (ncommits >= limit - 1)
1751 if (ncommits == s->selected)
1752 wstandout(view->window);
1753 err = draw_commit(view, entry->commit, entry->id,
1754 date_display_cols, author_cols);
1755 if (ncommits == s->selected)
1756 wstandend(view->window);
1760 s->last_displayed_entry = entry;
1761 entry = TAILQ_NEXT(entry, entry);
1776 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1778 struct commit_queue_entry *entry;
1781 entry = TAILQ_FIRST(&s->commits.head);
1782 if (s->first_displayed_entry == entry)
1785 entry = s->first_displayed_entry;
1786 while (entry && nscrolled < maxscroll) {
1787 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1789 s->first_displayed_entry = entry;
1795 static const struct got_error *
1796 trigger_log_thread(struct tog_view *view, int wait)
1798 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1801 halfdelay(1); /* fast refresh while loading commits */
1803 while (ta->commits_needed > 0 || ta->load_all) {
1804 if (ta->log_complete)
1807 /* Wake the log thread. */
1808 errcode = pthread_cond_signal(&ta->need_commits);
1810 return got_error_set_errno(errcode,
1811 "pthread_cond_signal");
1814 * The mutex will be released while the view loop waits
1815 * in wgetch(), at which time the log thread will run.
1820 /* Display progress update in log view. */
1821 show_log_view(view);
1825 /* Wait right here while next commit is being loaded. */
1826 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1828 return got_error_set_errno(errcode,
1829 "pthread_cond_wait");
1831 /* Display progress update in log view. */
1832 show_log_view(view);
1840 static const struct got_error *
1841 log_scroll_down(struct tog_view *view, int maxscroll)
1843 struct tog_log_view_state *s = &view->state.log;
1844 const struct got_error *err = NULL;
1845 struct commit_queue_entry *pentry;
1846 int nscrolled = 0, ncommits_needed;
1848 if (s->last_displayed_entry == NULL)
1851 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1852 if (s->commits.ncommits < ncommits_needed &&
1853 !s->thread_args.log_complete) {
1855 * Ask the log thread for required amount of commits.
1857 s->thread_args.commits_needed += maxscroll;
1858 err = trigger_log_thread(view, 1);
1864 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1868 s->last_displayed_entry = pentry;
1870 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1873 s->first_displayed_entry = pentry;
1874 } while (++nscrolled < maxscroll);
1879 static const struct got_error *
1880 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1881 struct got_commit_object *commit, struct got_object_id *commit_id,
1882 struct tog_view *log_view, struct got_repository *repo)
1884 const struct got_error *err;
1885 struct got_object_qid *parent_id;
1886 struct tog_view *diff_view;
1888 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1889 if (diff_view == NULL)
1890 return got_error_from_errno("view_open");
1892 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1893 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1894 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1896 *new_view = diff_view;
1900 static const struct got_error *
1901 tree_view_visit_subtree(struct tog_tree_view_state *s,
1902 struct got_tree_object *subtree)
1904 struct tog_parent_tree *parent;
1906 parent = calloc(1, sizeof(*parent));
1908 return got_error_from_errno("calloc");
1910 parent->tree = s->tree;
1911 parent->first_displayed_entry = s->first_displayed_entry;
1912 parent->selected_entry = s->selected_entry;
1913 parent->selected = s->selected;
1914 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1917 s->first_displayed_entry = NULL;
1921 static const struct got_error *
1922 tree_view_walk_path(struct tog_tree_view_state *s,
1923 struct got_object_id *commit_id, const char *path)
1925 const struct got_error *err = NULL;
1926 struct got_tree_object *tree = NULL;
1928 char *slash, *subpath = NULL;
1930 /* Walk the path and open corresponding tree objects. */
1933 struct got_tree_entry *te;
1934 struct got_object_id *tree_id;
1940 /* Ensure the correct subtree entry is selected. */
1941 slash = strchr(p, '/');
1943 te_name = strdup(p);
1945 te_name = strndup(p, slash - p);
1946 if (te_name == NULL) {
1947 err = got_error_from_errno("strndup");
1950 te = got_object_tree_find_entry(s->tree, te_name);
1952 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1957 s->first_displayed_entry = s->selected_entry = te;
1959 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1960 break; /* jump to this file's entry */
1962 slash = strchr(p, '/');
1964 subpath = strndup(path, slash - path);
1966 subpath = strdup(path);
1967 if (subpath == NULL) {
1968 err = got_error_from_errno("strdup");
1972 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1977 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1982 err = tree_view_visit_subtree(s, tree);
1984 got_object_tree_close(tree);
1998 static const struct got_error *
1999 browse_commit_tree(struct tog_view **new_view, int begin_x,
2000 struct commit_queue_entry *entry, const char *path,
2001 const char *head_ref_name, struct got_repository *repo)
2003 const struct got_error *err = NULL;
2004 struct tog_tree_view_state *s;
2005 struct tog_view *tree_view;
2007 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2008 if (tree_view == NULL)
2009 return got_error_from_errno("view_open");
2011 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2014 s = &tree_view->state.tree;
2016 *new_view = tree_view;
2018 if (got_path_is_root_dir(path))
2021 return tree_view_walk_path(s, entry->id, path);
2024 static const struct got_error *
2025 block_signals_used_by_main_thread(void)
2030 if (sigemptyset(&sigset) == -1)
2031 return got_error_from_errno("sigemptyset");
2033 /* tog handles SIGWINCH and SIGCONT */
2034 if (sigaddset(&sigset, SIGWINCH) == -1)
2035 return got_error_from_errno("sigaddset");
2036 if (sigaddset(&sigset, SIGCONT) == -1)
2037 return got_error_from_errno("sigaddset");
2039 /* ncurses handles SIGTSTP */
2040 if (sigaddset(&sigset, SIGTSTP) == -1)
2041 return got_error_from_errno("sigaddset");
2043 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2045 return got_error_set_errno(errcode, "pthread_sigmask");
2051 log_thread(void *arg)
2053 const struct got_error *err = NULL;
2055 struct tog_log_thread_args *a = arg;
2058 err = block_signals_used_by_main_thread();
2062 while (!done && !err && !tog_sigpipe_received) {
2063 err = queue_commits(a);
2065 if (err->code != GOT_ERR_ITER_COMPLETED)
2069 } else if (a->commits_needed > 0 && !a->load_all)
2070 a->commits_needed--;
2072 errcode = pthread_mutex_lock(&tog_mutex);
2074 err = got_error_set_errno(errcode,
2075 "pthread_mutex_lock");
2077 } else if (*a->quit)
2079 else if (*a->first_displayed_entry == NULL) {
2080 *a->first_displayed_entry =
2081 TAILQ_FIRST(&a->commits->head);
2082 *a->selected_entry = *a->first_displayed_entry;
2085 errcode = pthread_cond_signal(&a->commit_loaded);
2087 err = got_error_set_errno(errcode,
2088 "pthread_cond_signal");
2089 pthread_mutex_unlock(&tog_mutex);
2094 a->commits_needed = 0;
2096 if (a->commits_needed == 0 && !a->load_all) {
2097 errcode = pthread_cond_wait(&a->need_commits,
2100 err = got_error_set_errno(errcode,
2101 "pthread_cond_wait");
2107 errcode = pthread_mutex_unlock(&tog_mutex);
2108 if (errcode && err == NULL)
2109 err = got_error_set_errno(errcode,
2110 "pthread_mutex_unlock");
2112 a->log_complete = 1;
2116 static const struct got_error *
2117 stop_log_thread(struct tog_log_view_state *s)
2119 const struct got_error *err = NULL;
2124 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2126 return got_error_set_errno(errcode,
2127 "pthread_cond_signal");
2128 errcode = pthread_mutex_unlock(&tog_mutex);
2130 return got_error_set_errno(errcode,
2131 "pthread_mutex_unlock");
2132 errcode = pthread_join(s->thread, (void **)&err);
2134 return got_error_set_errno(errcode, "pthread_join");
2135 errcode = pthread_mutex_lock(&tog_mutex);
2137 return got_error_set_errno(errcode,
2138 "pthread_mutex_lock");
2139 s->thread = 0; //NULL;
2142 if (s->thread_args.repo) {
2143 err = got_repo_close(s->thread_args.repo);
2144 s->thread_args.repo = NULL;
2147 if (s->thread_args.graph) {
2148 got_commit_graph_close(s->thread_args.graph);
2149 s->thread_args.graph = NULL;
2155 static const struct got_error *
2156 close_log_view(struct tog_view *view)
2158 const struct got_error *err = NULL;
2159 struct tog_log_view_state *s = &view->state.log;
2162 err = stop_log_thread(s);
2164 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2165 if (errcode && err == NULL)
2166 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2168 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2169 if (errcode && err == NULL)
2170 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2172 free_commits(&s->commits);
2173 free(s->in_repo_path);
2174 s->in_repo_path = NULL;
2177 free(s->head_ref_name);
2178 s->head_ref_name = NULL;
2182 static const struct got_error *
2183 search_start_log_view(struct tog_view *view)
2185 struct tog_log_view_state *s = &view->state.log;
2187 s->matched_entry = NULL;
2188 s->search_entry = NULL;
2192 static const struct got_error *
2193 search_next_log_view(struct tog_view *view)
2195 const struct got_error *err = NULL;
2196 struct tog_log_view_state *s = &view->state.log;
2197 struct commit_queue_entry *entry;
2199 /* Display progress update in log view. */
2200 show_log_view(view);
2204 if (s->search_entry) {
2206 errcode = pthread_mutex_unlock(&tog_mutex);
2208 return got_error_set_errno(errcode,
2209 "pthread_mutex_unlock");
2210 ch = wgetch(view->window);
2211 errcode = pthread_mutex_lock(&tog_mutex);
2213 return got_error_set_errno(errcode,
2214 "pthread_mutex_lock");
2215 if (ch == KEY_BACKSPACE) {
2216 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2219 if (view->searching == TOG_SEARCH_FORWARD)
2220 entry = TAILQ_NEXT(s->search_entry, entry);
2222 entry = TAILQ_PREV(s->search_entry,
2223 commit_queue_head, entry);
2224 } else if (s->matched_entry) {
2225 if (view->searching == TOG_SEARCH_FORWARD)
2226 entry = TAILQ_NEXT(s->matched_entry, entry);
2228 entry = TAILQ_PREV(s->matched_entry,
2229 commit_queue_head, entry);
2231 entry = s->selected_entry;
2237 if (entry == NULL) {
2238 if (s->thread_args.log_complete ||
2239 view->searching == TOG_SEARCH_BACKWARD) {
2240 view->search_next_done =
2241 (s->matched_entry == NULL ?
2242 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2243 s->search_entry = NULL;
2247 * Poke the log thread for more commits and return,
2248 * allowing the main loop to make progress. Search
2249 * will resume at s->search_entry once we come back.
2251 s->thread_args.commits_needed++;
2252 return trigger_log_thread(view, 0);
2255 err = match_commit(&have_match, entry->id, entry->commit,
2260 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2261 s->matched_entry = entry;
2265 s->search_entry = entry;
2266 if (view->searching == TOG_SEARCH_FORWARD)
2267 entry = TAILQ_NEXT(entry, entry);
2269 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2272 if (s->matched_entry) {
2273 int cur = s->selected_entry->idx;
2274 while (cur < s->matched_entry->idx) {
2275 err = input_log_view(NULL, view, KEY_DOWN);
2280 while (cur > s->matched_entry->idx) {
2281 err = input_log_view(NULL, view, KEY_UP);
2288 s->search_entry = NULL;
2293 static const struct got_error *
2294 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2295 struct got_repository *repo, const char *head_ref_name,
2296 const char *in_repo_path, int log_branches)
2298 const struct got_error *err = NULL;
2299 struct tog_log_view_state *s = &view->state.log;
2300 struct got_repository *thread_repo = NULL;
2301 struct got_commit_graph *thread_graph = NULL;
2304 if (in_repo_path != s->in_repo_path) {
2305 free(s->in_repo_path);
2306 s->in_repo_path = strdup(in_repo_path);
2307 if (s->in_repo_path == NULL)
2308 return got_error_from_errno("strdup");
2311 /* The commit queue only contains commits being displayed. */
2312 TAILQ_INIT(&s->commits.head);
2313 s->commits.ncommits = 0;
2316 if (head_ref_name) {
2317 s->head_ref_name = strdup(head_ref_name);
2318 if (s->head_ref_name == NULL) {
2319 err = got_error_from_errno("strdup");
2323 s->start_id = got_object_id_dup(start_id);
2324 if (s->start_id == NULL) {
2325 err = got_error_from_errno("got_object_id_dup");
2328 s->log_branches = log_branches;
2330 STAILQ_INIT(&s->colors);
2331 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2332 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2333 get_color_value("TOG_COLOR_COMMIT"));
2336 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2337 get_color_value("TOG_COLOR_AUTHOR"));
2339 free_colors(&s->colors);
2342 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2343 get_color_value("TOG_COLOR_DATE"));
2345 free_colors(&s->colors);
2350 view->show = show_log_view;
2351 view->input = input_log_view;
2352 view->close = close_log_view;
2353 view->search_start = search_start_log_view;
2354 view->search_next = search_next_log_view;
2356 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2359 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2363 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2364 s->repo, NULL, NULL);
2368 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2370 err = got_error_set_errno(errcode, "pthread_cond_init");
2373 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2375 err = got_error_set_errno(errcode, "pthread_cond_init");
2379 s->thread_args.commits_needed = view->nlines;
2380 s->thread_args.graph = thread_graph;
2381 s->thread_args.commits = &s->commits;
2382 s->thread_args.in_repo_path = s->in_repo_path;
2383 s->thread_args.start_id = s->start_id;
2384 s->thread_args.repo = thread_repo;
2385 s->thread_args.log_complete = 0;
2386 s->thread_args.quit = &s->quit;
2387 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2388 s->thread_args.selected_entry = &s->selected_entry;
2389 s->thread_args.searching = &view->searching;
2390 s->thread_args.search_next_done = &view->search_next_done;
2391 s->thread_args.regex = &view->regex;
2394 close_log_view(view);
2398 static const struct got_error *
2399 show_log_view(struct tog_view *view)
2401 const struct got_error *err;
2402 struct tog_log_view_state *s = &view->state.log;
2404 if (s->thread == 0) { //NULL) {
2405 int errcode = pthread_create(&s->thread, NULL, log_thread,
2408 return got_error_set_errno(errcode, "pthread_create");
2409 if (s->thread_args.commits_needed > 0) {
2410 err = trigger_log_thread(view, 1);
2416 return draw_commits(view);
2419 static const struct got_error *
2420 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2422 const struct got_error *err = NULL;
2423 struct tog_log_view_state *s = &view->state.log;
2424 struct tog_view *diff_view = NULL, *tree_view = NULL;
2425 struct tog_view *ref_view = NULL;
2426 struct commit_queue_entry *entry;
2429 if (s->thread_args.load_all) {
2430 if (ch == KEY_BACKSPACE)
2431 s->thread_args.load_all = 0;
2432 else if (s->thread_args.log_complete) {
2433 s->thread_args.load_all = 0;
2434 log_scroll_down(view, s->commits.ncommits);
2435 s->selected = MIN(view->nlines - 2,
2436 s->commits.ncommits - 1);
2451 if (s->first_displayed_entry == NULL)
2453 if (s->selected > 0)
2456 log_scroll_up(s, 1);
2462 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2467 if (s->first_displayed_entry == NULL)
2469 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2472 log_scroll_up(s, view->nlines - 1);
2480 if (s->first_displayed_entry == NULL)
2482 if (s->selected < MIN(view->nlines - 2,
2483 s->commits.ncommits - 1))
2486 err = log_scroll_down(view, 1);
2494 /* We don't know yet how many commits, so we're forced to
2495 * traverse them all. */
2496 if (!s->thread_args.log_complete) {
2497 s->thread_args.load_all = 1;
2498 return trigger_log_thread(view, 0);
2502 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2503 for (n = 0; n < view->nlines - 1; n++) {
2506 s->first_displayed_entry = entry;
2507 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2510 s->selected = n - 1;
2516 struct commit_queue_entry *first;
2517 first = s->first_displayed_entry;
2520 err = log_scroll_down(view, view->nlines - 1);
2523 if (first == s->first_displayed_entry &&
2524 s->selected < MIN(view->nlines - 2,
2525 s->commits.ncommits - 1)) {
2526 /* can't scroll further down */
2527 s->selected = MIN(view->nlines - 2,
2528 s->commits.ncommits - 1);
2534 if (s->selected > view->nlines - 2)
2535 s->selected = view->nlines - 2;
2536 if (s->selected > s->commits.ncommits - 1)
2537 s->selected = s->commits.ncommits - 1;
2539 if (s->commits.ncommits < view->nlines - 1 &&
2540 !s->thread_args.log_complete) {
2541 s->thread_args.commits_needed += (view->nlines - 1) -
2542 s->commits.ncommits;
2543 err = trigger_log_thread(view, 1);
2549 if (s->selected_entry == NULL)
2551 if (view_is_parent_view(view))
2552 begin_x = view_split_begin_x(view->begin_x);
2553 err = open_diff_view_for_commit(&diff_view, begin_x,
2554 s->selected_entry->commit, s->selected_entry->id,
2559 diff_view->focussed = 1;
2560 if (view_is_parent_view(view)) {
2561 err = view_close_child(view);
2564 view_set_child(view, diff_view);
2565 view->focus_child = 1;
2567 *new_view = diff_view;
2570 if (s->selected_entry == NULL)
2572 if (view_is_parent_view(view))
2573 begin_x = view_split_begin_x(view->begin_x);
2574 err = browse_commit_tree(&tree_view, begin_x,
2575 s->selected_entry, s->in_repo_path, s->head_ref_name,
2580 tree_view->focussed = 1;
2581 if (view_is_parent_view(view)) {
2582 err = view_close_child(view);
2585 view_set_child(view, tree_view);
2586 view->focus_child = 1;
2588 *new_view = tree_view;
2593 if (ch == KEY_BACKSPACE &&
2594 got_path_is_root_dir(s->in_repo_path))
2596 err = stop_log_thread(s);
2599 if (ch == KEY_BACKSPACE) {
2601 err = got_path_dirname(&parent_path, s->in_repo_path);
2604 free(s->in_repo_path);
2605 s->in_repo_path = parent_path;
2606 s->thread_args.in_repo_path = s->in_repo_path;
2607 } else if (ch == CTRL('l')) {
2608 struct got_object_id *start_id;
2609 err = got_repo_match_object_id(&start_id, NULL,
2610 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2611 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2615 s->start_id = start_id;
2616 s->thread_args.start_id = s->start_id;
2618 s->log_branches = !s->log_branches;
2620 err = got_repo_open(&s->thread_args.repo,
2621 got_repo_get_path(s->repo), NULL);
2625 err = tog_load_refs(s->repo, 0);
2628 err = got_commit_graph_open(&s->thread_args.graph,
2629 s->in_repo_path, !s->log_branches);
2632 err = got_commit_graph_iter_start(s->thread_args.graph,
2633 s->start_id, s->repo, NULL, NULL);
2636 free_commits(&s->commits);
2637 s->first_displayed_entry = NULL;
2638 s->last_displayed_entry = NULL;
2639 s->selected_entry = NULL;
2641 s->thread_args.log_complete = 0;
2643 s->thread_args.commits_needed = view->nlines;
2646 if (view_is_parent_view(view))
2647 begin_x = view_split_begin_x(view->begin_x);
2648 ref_view = view_open(view->nlines, view->ncols,
2649 view->begin_y, begin_x, TOG_VIEW_REF);
2650 if (ref_view == NULL)
2651 return got_error_from_errno("view_open");
2652 err = open_ref_view(ref_view, s->repo);
2654 view_close(ref_view);
2658 ref_view->focussed = 1;
2659 if (view_is_parent_view(view)) {
2660 err = view_close_child(view);
2663 view_set_child(view, ref_view);
2664 view->focus_child = 1;
2666 *new_view = ref_view;
2675 static const struct got_error *
2676 apply_unveil(const char *repo_path, const char *worktree_path)
2678 const struct got_error *error;
2681 if (unveil("gmon.out", "rwc") != 0)
2682 return got_error_from_errno2("unveil", "gmon.out");
2684 if (repo_path && unveil(repo_path, "r") != 0)
2685 return got_error_from_errno2("unveil", repo_path);
2687 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2688 return got_error_from_errno2("unveil", worktree_path);
2690 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2691 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2693 error = got_privsep_unveil_exec_helpers();
2697 if (unveil(NULL, NULL) != 0)
2698 return got_error_from_errno("unveil");
2708 halfdelay(1); /* Do fast refresh while initial view is loading. */
2711 intrflush(stdscr, FALSE);
2712 keypad(stdscr, TRUE);
2714 if (getenv("TOG_COLORS") != NULL) {
2716 use_default_colors();
2718 signal(SIGWINCH, tog_sigwinch);
2719 signal(SIGPIPE, tog_sigpipe);
2720 signal(SIGCONT, tog_sigcont);
2723 static const struct got_error *
2724 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2725 struct got_repository *repo, struct got_worktree *worktree)
2727 const struct got_error *err = NULL;
2730 *in_repo_path = strdup("/");
2731 if (*in_repo_path == NULL)
2732 return got_error_from_errno("strdup");
2737 const char *prefix = got_worktree_get_path_prefix(worktree);
2740 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2743 if (asprintf(in_repo_path, "%s%s%s", prefix,
2744 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2746 err = got_error_from_errno("asprintf");
2747 *in_repo_path = NULL;
2751 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2756 static const struct got_error *
2757 cmd_log(int argc, char *argv[])
2759 const struct got_error *error;
2760 struct got_repository *repo = NULL;
2761 struct got_worktree *worktree = NULL;
2762 struct got_object_id *start_id = NULL;
2763 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2764 char *start_commit = NULL, *label = NULL;
2765 struct got_reference *ref = NULL;
2766 const char *head_ref_name = NULL;
2767 int ch, log_branches = 0;
2768 struct tog_view *view;
2770 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2776 start_commit = optarg;
2779 repo_path = realpath(optarg, NULL);
2780 if (repo_path == NULL)
2781 return got_error_from_errno2("realpath",
2796 if (repo_path == NULL) {
2797 cwd = getcwd(NULL, 0);
2799 return got_error_from_errno("getcwd");
2800 error = got_worktree_open(&worktree, cwd);
2801 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2805 strdup(got_worktree_get_repo_path(worktree));
2807 repo_path = strdup(cwd);
2808 if (repo_path == NULL) {
2809 error = got_error_from_errno("strdup");
2814 error = got_repo_open(&repo, repo_path, NULL);
2818 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2825 error = apply_unveil(got_repo_get_path(repo),
2826 worktree ? got_worktree_get_root_path(worktree) : NULL);
2830 /* already loaded by tog_log_with_path()? */
2831 if (TAILQ_EMPTY(&tog_refs)) {
2832 error = tog_load_refs(repo, 0);
2837 if (start_commit == NULL) {
2838 error = got_repo_match_object_id(&start_id, &label,
2839 worktree ? got_worktree_get_head_ref_name(worktree) :
2840 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2843 head_ref_name = label;
2845 error = got_ref_open(&ref, repo, start_commit, 0);
2847 head_ref_name = got_ref_get_name(ref);
2848 else if (error->code != GOT_ERR_NOT_REF)
2850 error = got_repo_match_object_id(&start_id, NULL,
2851 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2856 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2858 error = got_error_from_errno("view_open");
2861 error = open_log_view(view, start_id, repo, head_ref_name,
2862 in_repo_path, log_branches);
2866 /* Release work tree lock. */
2867 got_worktree_close(worktree);
2870 error = view_loop(view);
2880 const struct got_error *close_err = got_repo_close(repo);
2885 got_worktree_close(worktree);
2894 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2895 "[-w] object1 object2\n", getprogname());
2900 match_line(const char *line, regex_t *regex, size_t nmatch,
2901 regmatch_t *regmatch)
2903 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2907 match_color(struct tog_colors *colors, const char *line)
2909 struct tog_color *tc = NULL;
2911 STAILQ_FOREACH(tc, colors, entry) {
2912 if (match_line(line, &tc->regex, 0, NULL))
2919 static const struct got_error *
2920 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2921 WINDOW *window, regmatch_t *regmatch)
2923 const struct got_error *err = NULL;
2930 s = strndup(line, regmatch->rm_so);
2932 return got_error_from_errno("strndup");
2934 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2939 waddwstr(window, wline);
2946 s = strndup(line + regmatch->rm_so,
2947 regmatch->rm_eo - regmatch->rm_so);
2949 err = got_error_from_errno("strndup");
2953 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2958 wattr_on(window, A_STANDOUT, NULL);
2959 waddwstr(window, wline);
2960 wattr_off(window, A_STANDOUT, NULL);
2967 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2968 err = format_line(&wline, &width,
2969 line + regmatch->rm_eo, wlimit, col_tab_align);
2972 waddwstr(window, wline);
2980 static const struct got_error *
2981 draw_file(struct tog_view *view, const char *header)
2983 struct tog_diff_view_state *s = &view->state.diff;
2984 regmatch_t *regmatch = &view->regmatch;
2985 const struct got_error *err;
2988 size_t linesize = 0;
2990 struct tog_color *tc;
2993 int max_lines = view->nlines;
2994 int nlines = s->nlines;
2997 line_offset = s->line_offsets[s->first_displayed_line - 1];
2998 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2999 return got_error_from_errno("fseek");
3001 werase(view->window);
3004 if (asprintf(&line, "[%d/%d] %s",
3005 s->first_displayed_line - 1 + s->selected_line, nlines,
3007 return got_error_from_errno("asprintf");
3008 err = format_line(&wline, &width, line, view->ncols, 0);
3013 if (view_needs_focus_indication(view))
3014 wstandout(view->window);
3015 waddwstr(view->window, wline);
3018 if (view_needs_focus_indication(view))
3019 wstandend(view->window);
3020 if (width <= view->ncols - 1)
3021 waddch(view->window, '\n');
3030 while (max_lines > 0 && nprinted < max_lines) {
3031 linelen = getline(&line, &linesize, s->f);
3032 if (linelen == -1) {
3038 return got_ferror(s->f, GOT_ERR_IO);
3041 tc = match_color(&s->colors, line);
3043 wattr_on(view->window,
3044 COLOR_PAIR(tc->colorpair), NULL);
3045 if (s->first_displayed_line + nprinted == s->matched_line &&
3046 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3047 err = add_matched_line(&width, line, view->ncols, 0,
3048 view->window, regmatch);
3054 err = format_line(&wline, &width, line, view->ncols, 0);
3059 waddwstr(view->window, wline);
3064 wattr_off(view->window,
3065 COLOR_PAIR(tc->colorpair), NULL);
3066 if (width <= view->ncols - 1)
3067 waddch(view->window, '\n');
3072 s->last_displayed_line = s->first_displayed_line +
3075 s->last_displayed_line = s->first_displayed_line;
3080 while (nprinted < view->nlines) {
3081 waddch(view->window, '\n');
3085 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3090 wstandout(view->window);
3091 waddwstr(view->window, wline);
3094 wstandend(view->window);
3101 get_datestr(time_t *time, char *datebuf)
3103 struct tm mytm, *tm;
3106 tm = gmtime_r(time, &mytm);
3109 s = asctime_r(tm, datebuf);
3112 p = strchr(s, '\n');
3118 static const struct got_error *
3119 get_changed_paths(struct got_pathlist_head *paths,
3120 struct got_commit_object *commit, struct got_repository *repo)
3122 const struct got_error *err = NULL;
3123 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3124 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3125 struct got_object_qid *qid;
3127 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3129 struct got_commit_object *pcommit;
3130 err = got_object_open_as_commit(&pcommit, repo,
3135 tree_id1 = got_object_id_dup(
3136 got_object_commit_get_tree_id(pcommit));
3137 if (tree_id1 == NULL) {
3138 got_object_commit_close(pcommit);
3139 return got_error_from_errno("got_object_id_dup");
3141 got_object_commit_close(pcommit);
3146 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3151 tree_id2 = got_object_commit_get_tree_id(commit);
3152 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3156 err = got_diff_tree(tree1, tree2, "", "", repo,
3157 got_diff_tree_collect_changed_paths, paths, 0);
3160 got_object_tree_close(tree1);
3162 got_object_tree_close(tree2);
3167 static const struct got_error *
3168 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3172 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3174 return got_error_from_errno("reallocarray");
3176 (*line_offsets)[*nlines] = off;
3181 static const struct got_error *
3182 write_commit_info(off_t **line_offsets, size_t *nlines,
3183 struct got_object_id *commit_id, struct got_reflist_head *refs,
3184 struct got_repository *repo, FILE *outfile)
3186 const struct got_error *err = NULL;
3187 char datebuf[26], *datestr;
3188 struct got_commit_object *commit;
3189 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3190 time_t committer_time;
3191 const char *author, *committer;
3192 char *refs_str = NULL;
3193 struct got_pathlist_head changed_paths;
3194 struct got_pathlist_entry *pe;
3198 TAILQ_INIT(&changed_paths);
3201 err = build_refs_str(&refs_str, refs, commit_id, repo);
3206 err = got_object_open_as_commit(&commit, repo, commit_id);
3210 err = got_object_id_str(&id_str, commit_id);
3212 err = got_error_from_errno("got_object_id_str");
3216 err = add_line_offset(line_offsets, nlines, 0);
3220 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3221 refs_str ? refs_str : "", refs_str ? ")" : "");
3223 err = got_error_from_errno("fprintf");
3227 err = add_line_offset(line_offsets, nlines, outoff);
3231 n = fprintf(outfile, "from: %s\n",
3232 got_object_commit_get_author(commit));
3234 err = got_error_from_errno("fprintf");
3238 err = add_line_offset(line_offsets, nlines, outoff);
3242 committer_time = got_object_commit_get_committer_time(commit);
3243 datestr = get_datestr(&committer_time, datebuf);
3245 n = fprintf(outfile, "date: %s UTC\n", datestr);
3247 err = got_error_from_errno("fprintf");
3251 err = add_line_offset(line_offsets, nlines, outoff);
3255 author = got_object_commit_get_author(commit);
3256 committer = got_object_commit_get_committer(commit);
3257 if (strcmp(author, committer) != 0) {
3258 n = fprintf(outfile, "via: %s\n", committer);
3260 err = got_error_from_errno("fprintf");
3264 err = add_line_offset(line_offsets, nlines, outoff);
3268 if (got_object_commit_get_nparents(commit) > 1) {
3269 const struct got_object_id_queue *parent_ids;
3270 struct got_object_qid *qid;
3272 parent_ids = got_object_commit_get_parent_ids(commit);
3273 STAILQ_FOREACH(qid, parent_ids, entry) {
3274 err = got_object_id_str(&id_str, qid->id);
3277 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3279 err = got_error_from_errno("fprintf");
3283 err = add_line_offset(line_offsets, nlines, outoff);
3291 err = got_object_commit_get_logmsg(&logmsg, commit);
3295 while ((line = strsep(&s, "\n")) != NULL) {
3296 n = fprintf(outfile, "%s\n", line);
3298 err = got_error_from_errno("fprintf");
3302 err = add_line_offset(line_offsets, nlines, outoff);
3307 err = get_changed_paths(&changed_paths, commit, repo);
3310 TAILQ_FOREACH(pe, &changed_paths, entry) {
3311 struct got_diff_changed_path *cp = pe->data;
3312 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3314 err = got_error_from_errno("fprintf");
3318 err = add_line_offset(line_offsets, nlines, outoff);
3321 free((char *)pe->path);
3325 fputc('\n', outfile);
3327 err = add_line_offset(line_offsets, nlines, outoff);
3329 got_pathlist_free(&changed_paths);
3333 got_object_commit_close(commit);
3335 free(*line_offsets);
3336 *line_offsets = NULL;
3342 static const struct got_error *
3343 create_diff(struct tog_diff_view_state *s)
3345 const struct got_error *err = NULL;
3349 free(s->line_offsets);
3350 s->line_offsets = malloc(sizeof(off_t));
3351 if (s->line_offsets == NULL)
3352 return got_error_from_errno("malloc");
3357 err = got_error_from_errno("got_opentemp");
3360 if (s->f && fclose(s->f) == EOF) {
3361 err = got_error_from_errno("fclose");
3367 err = got_object_get_type(&obj_type, s->repo, s->id1);
3369 err = got_object_get_type(&obj_type, s->repo, s->id2);
3374 case GOT_OBJ_TYPE_BLOB:
3375 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3376 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3377 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3379 case GOT_OBJ_TYPE_TREE:
3380 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3381 s->id1, s->id2, NULL, "", "", s->diff_context,
3382 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3384 case GOT_OBJ_TYPE_COMMIT: {
3385 const struct got_object_id_queue *parent_ids;
3386 struct got_object_qid *pid;
3387 struct got_commit_object *commit2;
3388 struct got_reflist_head *refs;
3390 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3393 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3394 /* Show commit info if we're diffing to a parent/root commit. */
3395 if (s->id1 == NULL) {
3396 err = write_commit_info(&s->line_offsets, &s->nlines,
3397 s->id2, refs, s->repo, s->f);
3401 parent_ids = got_object_commit_get_parent_ids(commit2);
3402 STAILQ_FOREACH(pid, parent_ids, entry) {
3403 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3404 err = write_commit_info(
3405 &s->line_offsets, &s->nlines,
3406 s->id2, refs, s->repo, s->f);
3413 got_object_commit_close(commit2);
3415 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3416 s->id1, s->id2, NULL, s->diff_context, s->ignore_whitespace,
3417 s->force_text_diff, s->repo, s->f);
3421 err = got_error(GOT_ERR_OBJ_TYPE);
3427 if (s->f && fflush(s->f) != 0 && err == NULL)
3428 err = got_error_from_errno("fflush");
3433 diff_view_indicate_progress(struct tog_view *view)
3435 mvwaddstr(view->window, 0, 0, "diffing...");
3440 static const struct got_error *
3441 search_start_diff_view(struct tog_view *view)
3443 struct tog_diff_view_state *s = &view->state.diff;
3445 s->matched_line = 0;
3449 static const struct got_error *
3450 search_next_diff_view(struct tog_view *view)
3452 struct tog_diff_view_state *s = &view->state.diff;
3455 size_t linesize = 0;
3458 if (!view->searching) {
3459 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3463 if (s->matched_line) {
3464 if (view->searching == TOG_SEARCH_FORWARD)
3465 lineno = s->matched_line + 1;
3467 lineno = s->matched_line - 1;
3469 lineno = s->first_displayed_line;
3474 if (lineno <= 0 || lineno > s->nlines) {
3475 if (s->matched_line == 0) {
3476 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3480 if (view->searching == TOG_SEARCH_FORWARD)
3486 offset = s->line_offsets[lineno - 1];
3487 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3489 return got_error_from_errno("fseeko");
3491 linelen = getline(&line, &linesize, s->f);
3492 if (linelen != -1 &&
3493 match_line(line, &view->regex, 1, &view->regmatch)) {
3494 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3495 s->matched_line = lineno;
3498 if (view->searching == TOG_SEARCH_FORWARD)
3505 if (s->matched_line) {
3506 s->first_displayed_line = s->matched_line;
3507 s->selected_line = 1;
3513 static const struct got_error *
3514 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3515 struct got_object_id *id2, const char *label1, const char *label2,
3516 int diff_context, int ignore_whitespace, int force_text_diff,
3517 struct tog_view *log_view, struct got_repository *repo)
3519 const struct got_error *err;
3520 struct tog_diff_view_state *s = &view->state.diff;
3522 if (id1 != NULL && id2 != NULL) {
3524 err = got_object_get_type(&type1, repo, id1);
3527 err = got_object_get_type(&type2, repo, id2);
3532 return got_error(GOT_ERR_OBJ_TYPE);
3534 s->first_displayed_line = 1;
3535 s->last_displayed_line = view->nlines;
3536 s->selected_line = 1;
3544 s->id1 = got_object_id_dup(id1);
3546 return got_error_from_errno("got_object_id_dup");
3550 s->id2 = got_object_id_dup(id2);
3551 if (s->id2 == NULL) {
3554 return got_error_from_errno("got_object_id_dup");
3557 s->first_displayed_line = 1;
3558 s->last_displayed_line = view->nlines;
3559 s->diff_context = diff_context;
3560 s->ignore_whitespace = ignore_whitespace;
3561 s->force_text_diff = force_text_diff;
3562 s->log_view = log_view;
3565 STAILQ_INIT(&s->colors);
3566 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3567 err = add_color(&s->colors,
3568 "^-", TOG_COLOR_DIFF_MINUS,
3569 get_color_value("TOG_COLOR_DIFF_MINUS"));
3572 err = add_color(&s->colors, "^\\+",
3573 TOG_COLOR_DIFF_PLUS,
3574 get_color_value("TOG_COLOR_DIFF_PLUS"));
3576 free_colors(&s->colors);
3579 err = add_color(&s->colors,
3580 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3581 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3583 free_colors(&s->colors);
3587 err = add_color(&s->colors,
3588 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3589 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3590 get_color_value("TOG_COLOR_DIFF_META"));
3592 free_colors(&s->colors);
3596 err = add_color(&s->colors,
3597 "^(from|via): ", TOG_COLOR_AUTHOR,
3598 get_color_value("TOG_COLOR_AUTHOR"));
3600 free_colors(&s->colors);
3604 err = add_color(&s->colors,
3605 "^date: ", TOG_COLOR_DATE,
3606 get_color_value("TOG_COLOR_DATE"));
3608 free_colors(&s->colors);
3613 if (log_view && view_is_splitscreen(view))
3614 show_log_view(log_view); /* draw vborder */
3615 diff_view_indicate_progress(view);
3617 s->line_offsets = NULL;
3619 err = create_diff(s);
3625 free_colors(&s->colors);
3629 view->show = show_diff_view;
3630 view->input = input_diff_view;
3631 view->close = close_diff_view;
3632 view->search_start = search_start_diff_view;
3633 view->search_next = search_next_diff_view;
3638 static const struct got_error *
3639 close_diff_view(struct tog_view *view)
3641 const struct got_error *err = NULL;
3642 struct tog_diff_view_state *s = &view->state.diff;
3648 if (s->f && fclose(s->f) == EOF)
3649 err = got_error_from_errno("fclose");
3650 free_colors(&s->colors);
3651 free(s->line_offsets);
3652 s->line_offsets = NULL;
3657 static const struct got_error *
3658 show_diff_view(struct tog_view *view)
3660 const struct got_error *err;
3661 struct tog_diff_view_state *s = &view->state.diff;
3662 char *id_str1 = NULL, *id_str2, *header;
3663 const char *label1, *label2;
3666 err = got_object_id_str(&id_str1, s->id1);
3669 label1 = s->label1 ? : id_str1;
3671 label1 = "/dev/null";
3673 err = got_object_id_str(&id_str2, s->id2);
3676 label2 = s->label2 ? : id_str2;
3678 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3679 err = got_error_from_errno("asprintf");
3687 err = draw_file(view, header);
3692 static const struct got_error *
3693 set_selected_commit(struct tog_diff_view_state *s,
3694 struct commit_queue_entry *entry)
3696 const struct got_error *err;
3697 const struct got_object_id_queue *parent_ids;
3698 struct got_commit_object *selected_commit;
3699 struct got_object_qid *pid;
3702 s->id2 = got_object_id_dup(entry->id);
3704 return got_error_from_errno("got_object_id_dup");
3706 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3709 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3711 pid = STAILQ_FIRST(parent_ids);
3712 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3713 got_object_commit_close(selected_commit);
3717 static const struct got_error *
3718 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3720 const struct got_error *err = NULL;
3721 struct tog_diff_view_state *s = &view->state.diff;
3722 struct tog_log_view_state *ls;
3723 struct commit_queue_entry *old_selected_entry;
3725 size_t linesize = 0;
3733 s->force_text_diff = !s->force_text_diff;
3735 s->ignore_whitespace = !s->ignore_whitespace;
3736 wclear(view->window);
3737 s->first_displayed_line = 1;
3738 s->last_displayed_line = view->nlines;
3739 s->matched_line = 0;
3740 diff_view_indicate_progress(view);
3741 err = create_diff(s);
3745 s->first_displayed_line = 1;
3752 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3758 if (s->first_displayed_line > 1)
3759 s->first_displayed_line--;
3763 if (s->first_displayed_line == 1)
3766 while (i++ < view->nlines - 1 &&
3767 s->first_displayed_line > 1)
3768 s->first_displayed_line--;
3774 s->first_displayed_line++;
3782 while (!s->eof && i++ < view->nlines - 1) {
3783 linelen = getline(&line, &linesize, s->f);
3784 s->first_displayed_line++;
3785 if (linelen == -1) {
3789 err = got_ferror(s->f, GOT_ERR_IO);
3796 if (s->diff_context > 0) {
3798 s->matched_line = 0;
3799 diff_view_indicate_progress(view);
3800 err = create_diff(s);
3801 if (s->first_displayed_line + view->nlines - 1 >
3803 s->first_displayed_line = 1;
3804 s->last_displayed_line = view->nlines;
3809 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3811 s->matched_line = 0;
3812 diff_view_indicate_progress(view);
3813 err = create_diff(s);
3818 if (s->log_view == NULL)
3820 ls = &s->log_view->state.log;
3821 old_selected_entry = ls->selected_entry;
3823 err = input_log_view(NULL, s->log_view, KEY_UP);
3827 if (old_selected_entry == ls->selected_entry)
3830 err = set_selected_commit(s, ls->selected_entry);
3834 s->first_displayed_line = 1;
3835 s->last_displayed_line = view->nlines;
3836 s->matched_line = 0;
3838 diff_view_indicate_progress(view);
3839 err = create_diff(s);
3843 if (s->log_view == NULL)
3845 ls = &s->log_view->state.log;
3846 old_selected_entry = ls->selected_entry;
3848 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3852 if (old_selected_entry == ls->selected_entry)
3855 err = set_selected_commit(s, ls->selected_entry);
3859 s->first_displayed_line = 1;
3860 s->last_displayed_line = view->nlines;
3861 s->matched_line = 0;
3863 diff_view_indicate_progress(view);
3864 err = create_diff(s);
3873 static const struct got_error *
3874 cmd_diff(int argc, char *argv[])
3876 const struct got_error *error = NULL;
3877 struct got_repository *repo = NULL;
3878 struct got_worktree *worktree = NULL;
3879 struct got_object_id *id1 = NULL, *id2 = NULL;
3880 char *repo_path = NULL, *cwd = NULL;
3881 char *id_str1 = NULL, *id_str2 = NULL;
3882 char *label1 = NULL, *label2 = NULL;
3883 int diff_context = 3, ignore_whitespace = 0;
3884 int ch, force_text_diff = 0;
3886 struct tog_view *view;
3888 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3891 force_text_diff = 1;
3894 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3897 errx(1, "number of context lines is %s: %s",
3901 repo_path = realpath(optarg, NULL);
3902 if (repo_path == NULL)
3903 return got_error_from_errno2("realpath",
3905 got_path_strip_trailing_slashes(repo_path);
3908 ignore_whitespace = 1;
3920 usage_diff(); /* TODO show local worktree changes */
3921 } else if (argc == 2) {
3927 if (repo_path == NULL) {
3928 cwd = getcwd(NULL, 0);
3930 return got_error_from_errno("getcwd");
3931 error = got_worktree_open(&worktree, cwd);
3932 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3936 strdup(got_worktree_get_repo_path(worktree));
3938 repo_path = strdup(cwd);
3939 if (repo_path == NULL) {
3940 error = got_error_from_errno("strdup");
3945 error = got_repo_open(&repo, repo_path, NULL);
3951 error = apply_unveil(got_repo_get_path(repo), NULL);
3955 error = tog_load_refs(repo, 0);
3959 error = got_repo_match_object_id(&id1, &label1, id_str1,
3960 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3964 error = got_repo_match_object_id(&id2, &label2, id_str2,
3965 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3969 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3971 error = got_error_from_errno("view_open");
3974 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3975 ignore_whitespace, force_text_diff, NULL, repo);
3978 error = view_loop(view);
3985 const struct got_error *close_err = got_repo_close(repo);
3990 got_worktree_close(worktree);
3999 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4004 struct tog_blame_line {
4006 struct got_object_id *id;
4009 static const struct got_error *
4010 draw_blame(struct tog_view *view)
4012 struct tog_blame_view_state *s = &view->state.blame;
4013 struct tog_blame *blame = &s->blame;
4014 regmatch_t *regmatch = &view->regmatch;
4015 const struct got_error *err;
4016 int lineno = 0, nprinted = 0;
4018 size_t linesize = 0;
4022 struct tog_blame_line *blame_line;
4023 struct got_object_id *prev_id = NULL;
4025 struct tog_color *tc;
4027 err = got_object_id_str(&id_str, s->blamed_commit->id);
4032 werase(view->window);
4034 if (asprintf(&line, "commit %s", id_str) == -1) {
4035 err = got_error_from_errno("asprintf");
4040 err = format_line(&wline, &width, line, view->ncols, 0);
4045 if (view_needs_focus_indication(view))
4046 wstandout(view->window);
4047 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4049 wattr_on(view->window,
4050 COLOR_PAIR(tc->colorpair), NULL);
4051 waddwstr(view->window, wline);
4053 wattr_off(view->window,
4054 COLOR_PAIR(tc->colorpair), NULL);
4055 if (view_needs_focus_indication(view))
4056 wstandend(view->window);
4059 if (width < view->ncols - 1)
4060 waddch(view->window, '\n');
4062 if (asprintf(&line, "[%d/%d] %s%s",
4063 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4064 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4066 return got_error_from_errno("asprintf");
4069 err = format_line(&wline, &width, line, view->ncols, 0);
4074 waddwstr(view->window, wline);
4077 if (width < view->ncols - 1)
4078 waddch(view->window, '\n');
4081 while (nprinted < view->nlines - 2) {
4082 linelen = getline(&line, &linesize, blame->f);
4083 if (linelen == -1) {
4084 if (feof(blame->f)) {
4089 return got_ferror(blame->f, GOT_ERR_IO);
4091 if (++lineno < s->first_displayed_line)
4094 if (view->focussed && nprinted == s->selected_line - 1)
4095 wstandout(view->window);
4097 if (blame->nlines > 0) {
4098 blame_line = &blame->lines[lineno - 1];
4099 if (blame_line->annotated && prev_id &&
4100 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4102 nprinted == s->selected_line - 1)) {
4103 waddstr(view->window, " ");
4104 } else if (blame_line->annotated) {
4106 err = got_object_id_str(&id_str, blame_line->id);
4111 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4113 wattr_on(view->window,
4114 COLOR_PAIR(tc->colorpair), NULL);
4115 wprintw(view->window, "%.8s", id_str);
4117 wattr_off(view->window,
4118 COLOR_PAIR(tc->colorpair), NULL);
4120 prev_id = blame_line->id;
4122 waddstr(view->window, "........");
4126 waddstr(view->window, "........");
4130 if (view->focussed && nprinted == s->selected_line - 1)
4131 wstandend(view->window);
4132 waddstr(view->window, " ");
4134 if (view->ncols <= 9) {
4136 wline = wcsdup(L"");
4137 if (wline == NULL) {
4138 err = got_error_from_errno("wcsdup");
4142 } else if (s->first_displayed_line + nprinted ==
4144 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4145 err = add_matched_line(&width, line, view->ncols - 9, 9,
4146 view->window, regmatch);
4153 err = format_line(&wline, &width, line,
4154 view->ncols - 9, 9);
4155 waddwstr(view->window, wline);
4161 if (width <= view->ncols - 1)
4162 waddch(view->window, '\n');
4163 if (++nprinted == 1)
4164 s->first_displayed_line = lineno;
4167 s->last_displayed_line = lineno;
4174 static const struct got_error *
4175 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4177 const struct got_error *err = NULL;
4178 struct tog_blame_cb_args *a = arg;
4179 struct tog_blame_line *line;
4182 if (nlines != a->nlines ||
4183 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4184 return got_error(GOT_ERR_RANGE);
4186 errcode = pthread_mutex_lock(&tog_mutex);
4188 return got_error_set_errno(errcode, "pthread_mutex_lock");
4190 if (*a->quit) { /* user has quit the blame view */
4191 err = got_error(GOT_ERR_ITER_COMPLETED);
4196 goto done; /* no change in this commit */
4198 line = &a->lines[lineno - 1];
4199 if (line->annotated)
4202 line->id = got_object_id_dup(id);
4203 if (line->id == NULL) {
4204 err = got_error_from_errno("got_object_id_dup");
4207 line->annotated = 1;
4209 errcode = pthread_mutex_unlock(&tog_mutex);
4211 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4216 blame_thread(void *arg)
4218 const struct got_error *err, *close_err;
4219 struct tog_blame_thread_args *ta = arg;
4220 struct tog_blame_cb_args *a = ta->cb_args;
4223 err = block_signals_used_by_main_thread();
4227 err = got_blame(ta->path, a->commit_id, ta->repo,
4228 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4229 if (err && err->code == GOT_ERR_CANCELLED)
4232 errcode = pthread_mutex_lock(&tog_mutex);
4234 return (void *)got_error_set_errno(errcode,
4235 "pthread_mutex_lock");
4237 close_err = got_repo_close(ta->repo);
4243 errcode = pthread_mutex_unlock(&tog_mutex);
4244 if (errcode && err == NULL)
4245 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4250 static struct got_object_id *
4251 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4252 int first_displayed_line, int selected_line)
4254 struct tog_blame_line *line;
4259 line = &lines[first_displayed_line - 1 + selected_line - 1];
4260 if (!line->annotated)
4266 static const struct got_error *
4267 stop_blame(struct tog_blame *blame)
4269 const struct got_error *err = NULL;
4272 if (blame->thread) {
4274 errcode = pthread_mutex_unlock(&tog_mutex);
4276 return got_error_set_errno(errcode,
4277 "pthread_mutex_unlock");
4278 errcode = pthread_join(blame->thread, (void **)&err);
4280 return got_error_set_errno(errcode, "pthread_join");
4281 errcode = pthread_mutex_lock(&tog_mutex);
4283 return got_error_set_errno(errcode,
4284 "pthread_mutex_lock");
4285 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4287 blame->thread = 0; //NULL;
4289 if (blame->thread_args.repo) {
4290 const struct got_error *close_err;
4291 close_err = got_repo_close(blame->thread_args.repo);
4294 blame->thread_args.repo = NULL;
4297 if (fclose(blame->f) == EOF && err == NULL)
4298 err = got_error_from_errno("fclose");
4302 for (i = 0; i < blame->nlines; i++)
4303 free(blame->lines[i].id);
4305 blame->lines = NULL;
4307 free(blame->cb_args.commit_id);
4308 blame->cb_args.commit_id = NULL;
4313 static const struct got_error *
4314 cancel_blame_view(void *arg)
4316 const struct got_error *err = NULL;
4320 errcode = pthread_mutex_lock(&tog_mutex);
4322 return got_error_set_errno(errcode,
4323 "pthread_mutex_unlock");
4326 err = got_error(GOT_ERR_CANCELLED);
4328 errcode = pthread_mutex_unlock(&tog_mutex);
4330 return got_error_set_errno(errcode,
4331 "pthread_mutex_lock");
4336 static const struct got_error *
4337 run_blame(struct tog_view *view)
4339 struct tog_blame_view_state *s = &view->state.blame;
4340 struct tog_blame *blame = &s->blame;
4341 const struct got_error *err = NULL;
4342 struct got_blob_object *blob = NULL;
4343 struct got_repository *thread_repo = NULL;
4344 struct got_object_id *obj_id = NULL;
4347 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4352 err = got_object_get_type(&obj_type, s->repo, obj_id);
4356 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4357 err = got_error(GOT_ERR_OBJ_TYPE);
4361 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4364 blame->f = got_opentemp();
4365 if (blame->f == NULL) {
4366 err = got_error_from_errno("got_opentemp");
4369 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4370 &blame->line_offsets, blame->f, blob);
4373 if (blame->nlines == 0) {
4374 s->blame_complete = 1;
4378 /* Don't include \n at EOF in the blame line count. */
4379 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4382 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4383 if (blame->lines == NULL) {
4384 err = got_error_from_errno("calloc");
4388 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4392 blame->cb_args.view = view;
4393 blame->cb_args.lines = blame->lines;
4394 blame->cb_args.nlines = blame->nlines;
4395 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4396 if (blame->cb_args.commit_id == NULL) {
4397 err = got_error_from_errno("got_object_id_dup");
4400 blame->cb_args.quit = &s->done;
4402 blame->thread_args.path = s->path;
4403 blame->thread_args.repo = thread_repo;
4404 blame->thread_args.cb_args = &blame->cb_args;
4405 blame->thread_args.complete = &s->blame_complete;
4406 blame->thread_args.cancel_cb = cancel_blame_view;
4407 blame->thread_args.cancel_arg = &s->done;
4408 s->blame_complete = 0;
4410 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4411 s->first_displayed_line = 1;
4412 s->last_displayed_line = view->nlines;
4413 s->selected_line = 1;
4415 s->matched_line = 0;
4419 got_object_blob_close(blob);
4426 static const struct got_error *
4427 open_blame_view(struct tog_view *view, char *path,
4428 struct got_object_id *commit_id, struct got_repository *repo)
4430 const struct got_error *err = NULL;
4431 struct tog_blame_view_state *s = &view->state.blame;
4433 STAILQ_INIT(&s->blamed_commits);
4435 s->path = strdup(path);
4436 if (s->path == NULL)
4437 return got_error_from_errno("strdup");
4439 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4445 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4446 s->first_displayed_line = 1;
4447 s->last_displayed_line = view->nlines;
4448 s->selected_line = 1;
4449 s->blame_complete = 0;
4451 s->commit_id = commit_id;
4452 memset(&s->blame, 0, sizeof(s->blame));
4454 STAILQ_INIT(&s->colors);
4455 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4456 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4457 get_color_value("TOG_COLOR_COMMIT"));
4462 view->show = show_blame_view;
4463 view->input = input_blame_view;
4464 view->close = close_blame_view;
4465 view->search_start = search_start_blame_view;
4466 view->search_next = search_next_blame_view;
4468 return run_blame(view);
4471 static const struct got_error *
4472 close_blame_view(struct tog_view *view)
4474 const struct got_error *err = NULL;
4475 struct tog_blame_view_state *s = &view->state.blame;
4477 if (s->blame.thread)
4478 err = stop_blame(&s->blame);
4480 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4481 struct got_object_qid *blamed_commit;
4482 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4483 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4484 got_object_qid_free(blamed_commit);
4488 free_colors(&s->colors);
4493 static const struct got_error *
4494 search_start_blame_view(struct tog_view *view)
4496 struct tog_blame_view_state *s = &view->state.blame;
4498 s->matched_line = 0;
4502 static const struct got_error *
4503 search_next_blame_view(struct tog_view *view)
4505 struct tog_blame_view_state *s = &view->state.blame;
4508 size_t linesize = 0;
4511 if (!view->searching) {
4512 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4516 if (s->matched_line) {
4517 if (view->searching == TOG_SEARCH_FORWARD)
4518 lineno = s->matched_line + 1;
4520 lineno = s->matched_line - 1;
4522 lineno = s->first_displayed_line - 1 + s->selected_line;
4527 if (lineno <= 0 || lineno > s->blame.nlines) {
4528 if (s->matched_line == 0) {
4529 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4533 if (view->searching == TOG_SEARCH_FORWARD)
4536 lineno = s->blame.nlines;
4539 offset = s->blame.line_offsets[lineno - 1];
4540 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4542 return got_error_from_errno("fseeko");
4544 linelen = getline(&line, &linesize, s->blame.f);
4545 if (linelen != -1 &&
4546 match_line(line, &view->regex, 1, &view->regmatch)) {
4547 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4548 s->matched_line = lineno;
4551 if (view->searching == TOG_SEARCH_FORWARD)
4558 if (s->matched_line) {
4559 s->first_displayed_line = s->matched_line;
4560 s->selected_line = 1;
4566 static const struct got_error *
4567 show_blame_view(struct tog_view *view)
4569 const struct got_error *err = NULL;
4570 struct tog_blame_view_state *s = &view->state.blame;
4573 if (s->blame.thread == 0 && !s->blame_complete) {
4574 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4575 &s->blame.thread_args);
4577 return got_error_set_errno(errcode, "pthread_create");
4579 halfdelay(1); /* fast refresh while annotating */
4582 if (s->blame_complete)
4583 halfdelay(10); /* disable fast refresh */
4585 err = draw_blame(view);
4591 static const struct got_error *
4592 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4594 const struct got_error *err = NULL, *thread_err = NULL;
4595 struct tog_view *diff_view;
4596 struct tog_blame_view_state *s = &view->state.blame;
4605 s->selected_line = 1;
4606 s->first_displayed_line = 1;
4610 if (s->blame.nlines < view->nlines - 2) {
4611 s->selected_line = s->blame.nlines;
4612 s->first_displayed_line = 1;
4614 s->selected_line = view->nlines - 2;
4615 s->first_displayed_line = s->blame.nlines -
4622 if (s->selected_line > 1)
4624 else if (s->selected_line == 1 &&
4625 s->first_displayed_line > 1)
4626 s->first_displayed_line--;
4630 if (s->first_displayed_line == 1) {
4631 s->selected_line = 1;
4634 if (s->first_displayed_line > view->nlines - 2)
4635 s->first_displayed_line -=
4638 s->first_displayed_line = 1;
4643 if (s->selected_line < view->nlines - 2 &&
4644 s->first_displayed_line +
4645 s->selected_line <= s->blame.nlines)
4647 else if (s->last_displayed_line <
4649 s->first_displayed_line++;
4653 struct got_object_id *id = NULL;
4654 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4655 s->first_displayed_line, s->selected_line);
4659 struct got_commit_object *commit;
4660 struct got_object_qid *pid;
4661 struct got_object_id *blob_id = NULL;
4663 err = got_object_open_as_commit(&commit,
4668 got_object_commit_get_parent_ids(commit));
4670 got_object_commit_close(commit);
4673 /* Check if path history ends here. */
4674 err = got_object_id_by_path(&blob_id, s->repo,
4677 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4679 got_object_commit_close(commit);
4682 err = got_object_get_type(&obj_type, s->repo,
4685 /* Can't blame non-blob type objects. */
4686 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4687 got_object_commit_close(commit);
4690 err = got_object_qid_alloc(&s->blamed_commit,
4692 got_object_commit_close(commit);
4694 if (got_object_id_cmp(id,
4695 s->blamed_commit->id) == 0)
4697 err = got_object_qid_alloc(&s->blamed_commit,
4703 thread_err = stop_blame(&s->blame);
4707 STAILQ_INSERT_HEAD(&s->blamed_commits,
4708 s->blamed_commit, entry);
4709 err = run_blame(view);
4715 struct got_object_qid *first;
4716 first = STAILQ_FIRST(&s->blamed_commits);
4717 if (!got_object_id_cmp(first->id, s->commit_id))
4720 thread_err = stop_blame(&s->blame);
4724 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4725 got_object_qid_free(s->blamed_commit);
4727 STAILQ_FIRST(&s->blamed_commits);
4728 err = run_blame(view);
4735 struct got_object_id *id = NULL;
4736 struct got_object_qid *pid;
4737 struct got_commit_object *commit = NULL;
4738 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4739 s->first_displayed_line, s->selected_line);
4742 err = got_object_open_as_commit(&commit, s->repo, id);
4746 got_object_commit_get_parent_ids(commit));
4747 if (view_is_parent_view(view))
4748 begin_x = view_split_begin_x(view->begin_x);
4749 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4750 if (diff_view == NULL) {
4751 got_object_commit_close(commit);
4752 err = got_error_from_errno("view_open");
4755 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4756 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4757 got_object_commit_close(commit);
4759 view_close(diff_view);
4763 diff_view->focussed = 1;
4764 if (view_is_parent_view(view)) {
4765 err = view_close_child(view);
4768 view_set_child(view, diff_view);
4769 view->focus_child = 1;
4771 *new_view = diff_view;
4779 if (s->last_displayed_line >= s->blame.nlines &&
4780 s->selected_line >= MIN(s->blame.nlines,
4781 view->nlines - 2)) {
4784 if (s->last_displayed_line >= s->blame.nlines &&
4785 s->selected_line < view->nlines - 2) {
4786 s->selected_line = MIN(s->blame.nlines,
4790 if (s->last_displayed_line + view->nlines - 2
4792 s->first_displayed_line +=
4795 s->first_displayed_line =
4800 if (s->selected_line > view->nlines - 2) {
4801 s->selected_line = MIN(s->blame.nlines,
4808 return thread_err ? thread_err : err;
4811 static const struct got_error *
4812 cmd_blame(int argc, char *argv[])
4814 const struct got_error *error;
4815 struct got_repository *repo = NULL;
4816 struct got_worktree *worktree = NULL;
4817 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4818 char *link_target = NULL;
4819 struct got_object_id *commit_id = NULL;
4820 char *commit_id_str = NULL;
4822 struct tog_view *view;
4824 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4827 commit_id_str = optarg;
4830 repo_path = realpath(optarg, NULL);
4831 if (repo_path == NULL)
4832 return got_error_from_errno2("realpath",
4847 if (repo_path == NULL) {
4848 cwd = getcwd(NULL, 0);
4850 return got_error_from_errno("getcwd");
4851 error = got_worktree_open(&worktree, cwd);
4852 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4856 strdup(got_worktree_get_repo_path(worktree));
4858 repo_path = strdup(cwd);
4859 if (repo_path == NULL) {
4860 error = got_error_from_errno("strdup");
4865 error = got_repo_open(&repo, repo_path, NULL);
4869 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4876 error = apply_unveil(got_repo_get_path(repo), NULL);
4880 error = tog_load_refs(repo, 0);
4884 if (commit_id_str == NULL) {
4885 struct got_reference *head_ref;
4886 error = got_ref_open(&head_ref, repo, worktree ?
4887 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4890 error = got_ref_resolve(&commit_id, repo, head_ref);
4891 got_ref_close(head_ref);
4893 error = got_repo_match_object_id(&commit_id, NULL,
4894 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4899 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4901 error = got_error_from_errno("view_open");
4905 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4910 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4915 /* Release work tree lock. */
4916 got_worktree_close(worktree);
4919 error = view_loop(view);
4927 got_worktree_close(worktree);
4929 const struct got_error *close_err = got_repo_close(repo);
4937 static const struct got_error *
4938 draw_tree_entries(struct tog_view *view, const char *parent_path)
4940 struct tog_tree_view_state *s = &view->state.tree;
4941 const struct got_error *err = NULL;
4942 struct got_tree_entry *te;
4944 struct tog_color *tc;
4945 int width, n, i, nentries;
4946 int limit = view->nlines;
4950 werase(view->window);
4955 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4958 if (view_needs_focus_indication(view))
4959 wstandout(view->window);
4960 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4962 wattr_on(view->window,
4963 COLOR_PAIR(tc->colorpair), NULL);
4964 waddwstr(view->window, wline);
4966 wattr_off(view->window,
4967 COLOR_PAIR(tc->colorpair), NULL);
4968 if (view_needs_focus_indication(view))
4969 wstandend(view->window);
4972 if (width < view->ncols - 1)
4973 waddch(view->window, '\n');
4976 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4979 waddwstr(view->window, wline);
4982 if (width < view->ncols - 1)
4983 waddch(view->window, '\n');
4986 waddch(view->window, '\n');
4990 if (s->first_displayed_entry == NULL) {
4991 te = got_object_tree_get_first_entry(s->tree);
4992 if (s->selected == 0) {
4994 wstandout(view->window);
4995 s->selected_entry = NULL;
4997 waddstr(view->window, " ..\n"); /* parent directory */
4998 if (s->selected == 0 && view->focussed)
4999 wstandend(view->window);
5006 te = s->first_displayed_entry;
5009 nentries = got_object_tree_get_nentries(s->tree);
5010 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5011 char *line = NULL, *id_str = NULL, *link_target = NULL;
5012 const char *modestr = "";
5015 te = got_object_tree_get_entry(s->tree, i);
5016 mode = got_tree_entry_get_mode(te);
5019 err = got_object_id_str(&id_str,
5020 got_tree_entry_get_id(te));
5022 return got_error_from_errno(
5023 "got_object_id_str");
5025 if (got_object_tree_entry_is_submodule(te))
5027 else if (S_ISLNK(mode)) {
5030 err = got_tree_entry_get_symlink_target(&link_target,
5036 for (i = 0; i < strlen(link_target); i++) {
5037 if (!isprint((unsigned char)link_target[i]))
5038 link_target[i] = '?';
5042 else if (S_ISDIR(mode))
5044 else if (mode & S_IXUSR)
5046 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5047 got_tree_entry_get_name(te), modestr,
5048 link_target ? " -> ": "",
5049 link_target ? link_target : "") == -1) {
5052 return got_error_from_errno("asprintf");
5056 err = format_line(&wline, &width, line, view->ncols, 0);
5061 if (n == s->selected) {
5063 wstandout(view->window);
5064 s->selected_entry = te;
5066 tc = match_color(&s->colors, line);
5068 wattr_on(view->window,
5069 COLOR_PAIR(tc->colorpair), NULL);
5070 waddwstr(view->window, wline);
5072 wattr_off(view->window,
5073 COLOR_PAIR(tc->colorpair), NULL);
5074 if (width < view->ncols - 1)
5075 waddch(view->window, '\n');
5076 if (n == s->selected && view->focussed)
5077 wstandend(view->window);
5083 s->last_displayed_entry = te;
5092 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5094 struct got_tree_entry *te;
5095 int isroot = s->tree == s->root;
5098 if (s->first_displayed_entry == NULL)
5101 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5102 while (i++ < maxscroll) {
5105 s->first_displayed_entry = NULL;
5108 s->first_displayed_entry = te;
5109 te = got_tree_entry_get_prev(s->tree, te);
5114 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5116 struct got_tree_entry *next, *last;
5119 if (s->first_displayed_entry)
5120 next = got_tree_entry_get_next(s->tree,
5121 s->first_displayed_entry);
5123 next = got_object_tree_get_first_entry(s->tree);
5125 last = s->last_displayed_entry;
5126 while (next && last && n++ < maxscroll) {
5127 last = got_tree_entry_get_next(s->tree, last);
5129 s->first_displayed_entry = next;
5130 next = got_tree_entry_get_next(s->tree, next);
5135 static const struct got_error *
5136 tree_entry_path(char **path, struct tog_parent_trees *parents,
5137 struct got_tree_entry *te)
5139 const struct got_error *err = NULL;
5140 struct tog_parent_tree *pt;
5141 size_t len = 2; /* for leading slash and NUL */
5143 TAILQ_FOREACH(pt, parents, entry)
5144 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5147 len += strlen(got_tree_entry_get_name(te));
5149 *path = calloc(1, len);
5151 return got_error_from_errno("calloc");
5154 pt = TAILQ_LAST(parents, tog_parent_trees);
5156 const char *name = got_tree_entry_get_name(pt->selected_entry);
5157 if (strlcat(*path, name, len) >= len) {
5158 err = got_error(GOT_ERR_NO_SPACE);
5161 if (strlcat(*path, "/", len) >= len) {
5162 err = got_error(GOT_ERR_NO_SPACE);
5165 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5168 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5169 err = got_error(GOT_ERR_NO_SPACE);
5181 static const struct got_error *
5182 blame_tree_entry(struct tog_view **new_view, int begin_x,
5183 struct got_tree_entry *te, struct tog_parent_trees *parents,
5184 struct got_object_id *commit_id, struct got_repository *repo)
5186 const struct got_error *err = NULL;
5188 struct tog_view *blame_view;
5192 err = tree_entry_path(&path, parents, te);
5196 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5197 if (blame_view == NULL) {
5198 err = got_error_from_errno("view_open");
5202 err = open_blame_view(blame_view, path, commit_id, repo);
5204 if (err->code == GOT_ERR_CANCELLED)
5206 view_close(blame_view);
5208 *new_view = blame_view;
5214 static const struct got_error *
5215 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5216 struct tog_tree_view_state *s)
5218 struct tog_view *log_view;
5219 const struct got_error *err = NULL;
5224 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5225 if (log_view == NULL)
5226 return got_error_from_errno("view_open");
5228 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5232 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5235 view_close(log_view);
5237 *new_view = log_view;
5242 static const struct got_error *
5243 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5244 const char *head_ref_name, struct got_repository *repo)
5246 const struct got_error *err = NULL;
5247 char *commit_id_str = NULL;
5248 struct tog_tree_view_state *s = &view->state.tree;
5249 struct got_commit_object *commit = NULL;
5251 TAILQ_INIT(&s->parents);
5252 STAILQ_INIT(&s->colors);
5254 s->commit_id = got_object_id_dup(commit_id);
5255 if (s->commit_id == NULL)
5256 return got_error_from_errno("got_object_id_dup");
5258 err = got_object_open_as_commit(&commit, repo, commit_id);
5263 * The root is opened here and will be closed when the view is closed.
5264 * Any visited subtrees and their path-wise parents are opened and
5267 err = got_object_open_as_tree(&s->root, repo,
5268 got_object_commit_get_tree_id(commit));
5273 err = got_object_id_str(&commit_id_str, commit_id);
5277 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5278 err = got_error_from_errno("asprintf");
5282 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5283 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5284 if (head_ref_name) {
5285 s->head_ref_name = strdup(head_ref_name);
5286 if (s->head_ref_name == NULL) {
5287 err = got_error_from_errno("strdup");
5293 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5294 err = add_color(&s->colors, "\\$$",
5295 TOG_COLOR_TREE_SUBMODULE,
5296 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5299 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5300 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5303 err = add_color(&s->colors, "/$",
5304 TOG_COLOR_TREE_DIRECTORY,
5305 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5309 err = add_color(&s->colors, "\\*$",
5310 TOG_COLOR_TREE_EXECUTABLE,
5311 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5315 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5316 get_color_value("TOG_COLOR_COMMIT"));
5321 view->show = show_tree_view;
5322 view->input = input_tree_view;
5323 view->close = close_tree_view;
5324 view->search_start = search_start_tree_view;
5325 view->search_next = search_next_tree_view;
5327 free(commit_id_str);
5329 got_object_commit_close(commit);
5331 close_tree_view(view);
5335 static const struct got_error *
5336 close_tree_view(struct tog_view *view)
5338 struct tog_tree_view_state *s = &view->state.tree;
5340 free_colors(&s->colors);
5341 free(s->tree_label);
5342 s->tree_label = NULL;
5344 s->commit_id = NULL;
5345 free(s->head_ref_name);
5346 s->head_ref_name = NULL;
5347 while (!TAILQ_EMPTY(&s->parents)) {
5348 struct tog_parent_tree *parent;
5349 parent = TAILQ_FIRST(&s->parents);
5350 TAILQ_REMOVE(&s->parents, parent, entry);
5351 if (parent->tree != s->root)
5352 got_object_tree_close(parent->tree);
5356 if (s->tree != NULL && s->tree != s->root)
5357 got_object_tree_close(s->tree);
5359 got_object_tree_close(s->root);
5363 static const struct got_error *
5364 search_start_tree_view(struct tog_view *view)
5366 struct tog_tree_view_state *s = &view->state.tree;
5368 s->matched_entry = NULL;
5373 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5375 regmatch_t regmatch;
5377 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5381 static const struct got_error *
5382 search_next_tree_view(struct tog_view *view)
5384 struct tog_tree_view_state *s = &view->state.tree;
5385 struct got_tree_entry *te = NULL;
5387 if (!view->searching) {
5388 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5392 if (s->matched_entry) {
5393 if (view->searching == TOG_SEARCH_FORWARD) {
5394 if (s->selected_entry)
5395 te = got_tree_entry_get_next(s->tree,
5398 te = got_object_tree_get_first_entry(s->tree);
5400 if (s->selected_entry == NULL)
5401 te = got_object_tree_get_last_entry(s->tree);
5403 te = got_tree_entry_get_prev(s->tree,
5407 if (s->selected_entry)
5408 te = s->selected_entry;
5409 else if (view->searching == TOG_SEARCH_FORWARD)
5410 te = got_object_tree_get_first_entry(s->tree);
5412 te = got_object_tree_get_last_entry(s->tree);
5417 if (s->matched_entry == NULL) {
5418 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5421 if (view->searching == TOG_SEARCH_FORWARD)
5422 te = got_object_tree_get_first_entry(s->tree);
5424 te = got_object_tree_get_last_entry(s->tree);
5427 if (match_tree_entry(te, &view->regex)) {
5428 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5429 s->matched_entry = te;
5433 if (view->searching == TOG_SEARCH_FORWARD)
5434 te = got_tree_entry_get_next(s->tree, te);
5436 te = got_tree_entry_get_prev(s->tree, te);
5439 if (s->matched_entry) {
5440 s->first_displayed_entry = s->matched_entry;
5447 static const struct got_error *
5448 show_tree_view(struct tog_view *view)
5450 const struct got_error *err = NULL;
5451 struct tog_tree_view_state *s = &view->state.tree;
5454 err = tree_entry_path(&parent_path, &s->parents, NULL);
5458 err = draw_tree_entries(view, parent_path);
5465 static const struct got_error *
5466 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5468 const struct got_error *err = NULL;
5469 struct tog_tree_view_state *s = &view->state.tree;
5470 struct tog_view *log_view, *ref_view;
5471 struct got_tree_entry *te;
5476 s->show_ids = !s->show_ids;
5479 if (!s->selected_entry)
5481 if (view_is_parent_view(view))
5482 begin_x = view_split_begin_x(view->begin_x);
5483 err = log_selected_tree_entry(&log_view, begin_x, s);
5485 log_view->focussed = 1;
5486 if (view_is_parent_view(view)) {
5487 err = view_close_child(view);
5490 view_set_child(view, log_view);
5491 view->focus_child = 1;
5493 *new_view = log_view;
5496 if (view_is_parent_view(view))
5497 begin_x = view_split_begin_x(view->begin_x);
5498 ref_view = view_open(view->nlines, view->ncols,
5499 view->begin_y, begin_x, TOG_VIEW_REF);
5500 if (ref_view == NULL)
5501 return got_error_from_errno("view_open");
5502 err = open_ref_view(ref_view, s->repo);
5504 view_close(ref_view);
5508 ref_view->focussed = 1;
5509 if (view_is_parent_view(view)) {
5510 err = view_close_child(view);
5513 view_set_child(view, ref_view);
5514 view->focus_child = 1;
5516 *new_view = ref_view;
5521 if (s->tree == s->root)
5522 s->first_displayed_entry =
5523 got_object_tree_get_first_entry(s->tree);
5525 s->first_displayed_entry = NULL;
5530 te = got_object_tree_get_last_entry(s->tree);
5531 for (n = 0; n < view->nlines - 3; n++) {
5533 if(s->tree != s->root) {
5534 s->first_displayed_entry = NULL;
5539 s->first_displayed_entry = te;
5540 te = got_tree_entry_get_prev(s->tree, te);
5543 s->selected = n - 1;
5548 if (s->selected > 0) {
5552 tree_scroll_up(s, 1);
5556 if (s->tree == s->root) {
5557 if (got_object_tree_get_first_entry(s->tree) ==
5558 s->first_displayed_entry)
5561 if (s->first_displayed_entry == NULL)
5564 tree_scroll_up(s, MAX(0, view->nlines - 3));
5569 if (s->selected < s->ndisplayed - 1) {
5573 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5575 /* can't scroll any further */
5577 tree_scroll_down(s, 1);
5581 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5583 /* can't scroll any further; move cursor down */
5584 if (s->selected < s->ndisplayed - 1)
5585 s->selected = s->ndisplayed - 1;
5588 tree_scroll_down(s, view->nlines - 3);
5593 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5594 struct tog_parent_tree *parent;
5595 /* user selected '..' */
5596 if (s->tree == s->root)
5598 parent = TAILQ_FIRST(&s->parents);
5599 TAILQ_REMOVE(&s->parents, parent,
5601 got_object_tree_close(s->tree);
5602 s->tree = parent->tree;
5603 s->first_displayed_entry =
5604 parent->first_displayed_entry;
5606 parent->selected_entry;
5607 s->selected = parent->selected;
5609 } else if (S_ISDIR(got_tree_entry_get_mode(
5610 s->selected_entry))) {
5611 struct got_tree_object *subtree;
5612 err = got_object_open_as_tree(&subtree, s->repo,
5613 got_tree_entry_get_id(s->selected_entry));
5616 err = tree_view_visit_subtree(s, subtree);
5618 got_object_tree_close(subtree);
5621 } else if (S_ISREG(got_tree_entry_get_mode(
5622 s->selected_entry))) {
5623 struct tog_view *blame_view;
5624 int begin_x = view_is_parent_view(view) ?
5625 view_split_begin_x(view->begin_x) : 0;
5627 err = blame_tree_entry(&blame_view, begin_x,
5628 s->selected_entry, &s->parents,
5629 s->commit_id, s->repo);
5633 blame_view->focussed = 1;
5634 if (view_is_parent_view(view)) {
5635 err = view_close_child(view);
5638 view_set_child(view, blame_view);
5639 view->focus_child = 1;
5641 *new_view = blame_view;
5645 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5646 s->selected = view->nlines - 4;
5659 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5664 static const struct got_error *
5665 cmd_tree(int argc, char *argv[])
5667 const struct got_error *error;
5668 struct got_repository *repo = NULL;
5669 struct got_worktree *worktree = NULL;
5670 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5671 struct got_object_id *commit_id = NULL;
5672 const char *commit_id_arg = NULL;
5674 struct got_reference *ref = NULL;
5675 const char *head_ref_name = NULL;
5677 struct tog_view *view;
5679 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5682 commit_id_arg = optarg;
5685 repo_path = realpath(optarg, NULL);
5686 if (repo_path == NULL)
5687 return got_error_from_errno2("realpath",
5702 if (repo_path == NULL) {
5703 cwd = getcwd(NULL, 0);
5705 return got_error_from_errno("getcwd");
5706 error = got_worktree_open(&worktree, cwd);
5707 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5711 strdup(got_worktree_get_repo_path(worktree));
5713 repo_path = strdup(cwd);
5714 if (repo_path == NULL) {
5715 error = got_error_from_errno("strdup");
5720 error = got_repo_open(&repo, repo_path, NULL);
5724 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5731 error = apply_unveil(got_repo_get_path(repo), NULL);
5735 error = tog_load_refs(repo, 0);
5739 if (commit_id_arg == NULL) {
5740 error = got_repo_match_object_id(&commit_id, &label,
5741 worktree ? got_worktree_get_head_ref_name(worktree) :
5742 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5745 head_ref_name = label;
5747 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5749 head_ref_name = got_ref_get_name(ref);
5750 else if (error->code != GOT_ERR_NOT_REF)
5752 error = got_repo_match_object_id(&commit_id, NULL,
5753 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5758 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5760 error = got_error_from_errno("view_open");
5763 error = open_tree_view(view, commit_id, head_ref_name, repo);
5766 if (!got_path_is_root_dir(in_repo_path)) {
5767 error = tree_view_walk_path(&view->state.tree, commit_id,
5774 /* Release work tree lock. */
5775 got_worktree_close(worktree);
5778 error = view_loop(view);
5787 const struct got_error *close_err = got_repo_close(repo);
5795 static const struct got_error *
5796 ref_view_load_refs(struct tog_ref_view_state *s)
5798 struct got_reflist_entry *sre;
5799 struct tog_reflist_entry *re;
5802 TAILQ_FOREACH(sre, &tog_refs, entry) {
5803 if (strncmp(got_ref_get_name(sre->ref),
5804 "refs/got/", 9) == 0 &&
5805 strncmp(got_ref_get_name(sre->ref),
5806 "refs/got/backup/", 16) != 0)
5809 re = malloc(sizeof(*re));
5811 return got_error_from_errno("malloc");
5813 re->ref = got_ref_dup(sre->ref);
5814 if (re->ref == NULL)
5815 return got_error_from_errno("got_ref_dup");
5816 re->idx = s->nrefs++;
5817 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5820 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5825 ref_view_free_refs(struct tog_ref_view_state *s)
5827 struct tog_reflist_entry *re;
5829 while (!TAILQ_EMPTY(&s->refs)) {
5830 re = TAILQ_FIRST(&s->refs);
5831 TAILQ_REMOVE(&s->refs, re, entry);
5832 got_ref_close(re->ref);
5837 static const struct got_error *
5838 open_ref_view(struct tog_view *view, struct got_repository *repo)
5840 const struct got_error *err = NULL;
5841 struct tog_ref_view_state *s = &view->state.ref;
5843 s->selected_entry = 0;
5846 TAILQ_INIT(&s->refs);
5847 STAILQ_INIT(&s->colors);
5849 err = ref_view_load_refs(s);
5853 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5854 err = add_color(&s->colors, "^refs/heads/",
5855 TOG_COLOR_REFS_HEADS,
5856 get_color_value("TOG_COLOR_REFS_HEADS"));
5860 err = add_color(&s->colors, "^refs/tags/",
5861 TOG_COLOR_REFS_TAGS,
5862 get_color_value("TOG_COLOR_REFS_TAGS"));
5866 err = add_color(&s->colors, "^refs/remotes/",
5867 TOG_COLOR_REFS_REMOTES,
5868 get_color_value("TOG_COLOR_REFS_REMOTES"));
5872 err = add_color(&s->colors, "^refs/got/backup/",
5873 TOG_COLOR_REFS_BACKUP,
5874 get_color_value("TOG_COLOR_REFS_BACKUP"));
5879 view->show = show_ref_view;
5880 view->input = input_ref_view;
5881 view->close = close_ref_view;
5882 view->search_start = search_start_ref_view;
5883 view->search_next = search_next_ref_view;
5886 free_colors(&s->colors);
5890 static const struct got_error *
5891 close_ref_view(struct tog_view *view)
5893 struct tog_ref_view_state *s = &view->state.ref;
5895 ref_view_free_refs(s);
5896 free_colors(&s->colors);
5901 static const struct got_error *
5902 resolve_reflist_entry(struct got_object_id **commit_id,
5903 struct tog_reflist_entry *re, struct got_repository *repo)
5905 const struct got_error *err = NULL;
5906 struct got_object_id *obj_id;
5907 struct got_tag_object *tag = NULL;
5912 err = got_ref_resolve(&obj_id, repo, re->ref);
5916 err = got_object_get_type(&obj_type, repo, obj_id);
5921 case GOT_OBJ_TYPE_COMMIT:
5922 *commit_id = obj_id;
5924 case GOT_OBJ_TYPE_TAG:
5925 err = got_object_open_as_tag(&tag, repo, obj_id);
5929 err = got_object_get_type(&obj_type, repo,
5930 got_object_tag_get_object_id(tag));
5933 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5934 err = got_error(GOT_ERR_OBJ_TYPE);
5937 *commit_id = got_object_id_dup(
5938 got_object_tag_get_object_id(tag));
5939 if (*commit_id == NULL) {
5940 err = got_error_from_errno("got_object_id_dup");
5945 err = got_error(GOT_ERR_OBJ_TYPE);
5951 got_object_tag_close(tag);
5959 static const struct got_error *
5960 log_ref_entry(struct tog_view **new_view, int begin_x,
5961 struct tog_reflist_entry *re, struct got_repository *repo)
5963 struct tog_view *log_view;
5964 const struct got_error *err = NULL;
5965 struct got_object_id *commit_id = NULL;
5969 err = resolve_reflist_entry(&commit_id, re, repo);
5971 if (err->code != GOT_ERR_OBJ_TYPE)
5977 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5978 if (log_view == NULL) {
5979 err = got_error_from_errno("view_open");
5983 err = open_log_view(log_view, commit_id, repo,
5984 got_ref_get_name(re->ref), "", 0);
5987 view_close(log_view);
5989 *new_view = log_view;
5995 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5997 struct tog_reflist_entry *re;
6000 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6003 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6004 while (i++ < maxscroll) {
6007 s->first_displayed_entry = re;
6008 re = TAILQ_PREV(re, tog_reflist_head, entry);
6013 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6015 struct tog_reflist_entry *next, *last;
6018 if (s->first_displayed_entry)
6019 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6021 next = TAILQ_FIRST(&s->refs);
6023 last = s->last_displayed_entry;
6024 while (next && last && n++ < maxscroll) {
6025 last = TAILQ_NEXT(last, entry);
6027 s->first_displayed_entry = next;
6028 next = TAILQ_NEXT(next, entry);
6033 static const struct got_error *
6034 search_start_ref_view(struct tog_view *view)
6036 struct tog_ref_view_state *s = &view->state.ref;
6038 s->matched_entry = NULL;
6043 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6045 regmatch_t regmatch;
6047 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6051 static const struct got_error *
6052 search_next_ref_view(struct tog_view *view)
6054 struct tog_ref_view_state *s = &view->state.ref;
6055 struct tog_reflist_entry *re = NULL;
6057 if (!view->searching) {
6058 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6062 if (s->matched_entry) {
6063 if (view->searching == TOG_SEARCH_FORWARD) {
6064 if (s->selected_entry)
6065 re = TAILQ_NEXT(s->selected_entry, entry);
6067 re = TAILQ_PREV(s->selected_entry,
6068 tog_reflist_head, entry);
6070 if (s->selected_entry == NULL)
6071 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6073 re = TAILQ_PREV(s->selected_entry,
6074 tog_reflist_head, entry);
6077 if (s->selected_entry)
6078 re = s->selected_entry;
6079 else if (view->searching == TOG_SEARCH_FORWARD)
6080 re = TAILQ_FIRST(&s->refs);
6082 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6087 if (s->matched_entry == NULL) {
6088 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6091 if (view->searching == TOG_SEARCH_FORWARD)
6092 re = TAILQ_FIRST(&s->refs);
6094 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6097 if (match_reflist_entry(re, &view->regex)) {
6098 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6099 s->matched_entry = re;
6103 if (view->searching == TOG_SEARCH_FORWARD)
6104 re = TAILQ_NEXT(re, entry);
6106 re = TAILQ_PREV(re, tog_reflist_head, entry);
6109 if (s->matched_entry) {
6110 s->first_displayed_entry = s->matched_entry;
6117 static const struct got_error *
6118 show_ref_view(struct tog_view *view)
6120 const struct got_error *err = NULL;
6121 struct tog_ref_view_state *s = &view->state.ref;
6122 struct tog_reflist_entry *re;
6125 struct tog_color *tc;
6127 int limit = view->nlines;
6129 werase(view->window);
6136 re = s->first_displayed_entry;
6138 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6140 return got_error_from_errno("asprintf");
6142 err = format_line(&wline, &width, line, view->ncols, 0);
6147 if (view_needs_focus_indication(view))
6148 wstandout(view->window);
6149 waddwstr(view->window, wline);
6150 if (view_needs_focus_indication(view))
6151 wstandend(view->window);
6156 if (width < view->ncols - 1)
6157 waddch(view->window, '\n');
6162 while (re && limit > 0) {
6165 if (got_ref_is_symbolic(re->ref)) {
6166 if (asprintf(&line, "%s -> %s",
6167 got_ref_get_name(re->ref),
6168 got_ref_get_symref_target(re->ref)) == -1)
6169 return got_error_from_errno("asprintf");
6170 } else if (s->show_ids) {
6171 struct got_object_id *id;
6173 err = got_ref_resolve(&id, s->repo, re->ref);
6176 err = got_object_id_str(&id_str, id);
6181 if (asprintf(&line, "%s: %s",
6182 got_ref_get_name(re->ref), id_str) == -1) {
6183 err = got_error_from_errno("asprintf");
6191 line = strdup(got_ref_get_name(re->ref));
6193 return got_error_from_errno("strdup");
6196 err = format_line(&wline, &width, line, view->ncols, 0);
6201 if (n == s->selected) {
6203 wstandout(view->window);
6204 s->selected_entry = re;
6206 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6208 wattr_on(view->window,
6209 COLOR_PAIR(tc->colorpair), NULL);
6210 waddwstr(view->window, wline);
6212 wattr_off(view->window,
6213 COLOR_PAIR(tc->colorpair), NULL);
6214 if (width < view->ncols - 1)
6215 waddch(view->window, '\n');
6216 if (n == s->selected && view->focussed)
6217 wstandend(view->window);
6223 s->last_displayed_entry = re;
6226 re = TAILQ_NEXT(re, entry);
6233 static const struct got_error *
6234 browse_ref_tree(struct tog_view **new_view, int begin_x,
6235 struct tog_reflist_entry *re, struct got_repository *repo)
6237 const struct got_error *err = NULL;
6238 struct got_object_id *commit_id = NULL;
6239 struct tog_view *tree_view;
6243 err = resolve_reflist_entry(&commit_id, re, repo);
6245 if (err->code != GOT_ERR_OBJ_TYPE)
6252 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6253 if (tree_view == NULL) {
6254 err = got_error_from_errno("view_open");
6258 err = open_tree_view(tree_view, commit_id,
6259 got_ref_get_name(re->ref), repo);
6263 *new_view = tree_view;
6268 static const struct got_error *
6269 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6271 const struct got_error *err = NULL;
6272 struct tog_ref_view_state *s = &view->state.ref;
6273 struct tog_view *log_view, *tree_view;
6274 struct tog_reflist_entry *re;
6279 s->show_ids = !s->show_ids;
6282 s->sort_by_date = !s->sort_by_date;
6283 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6284 got_ref_cmp_by_commit_timestamp_descending :
6285 tog_ref_cmp_by_name, s->repo);
6288 got_reflist_object_id_map_free(tog_refs_idmap);
6289 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6290 &tog_refs, s->repo);
6293 ref_view_free_refs(s);
6294 err = ref_view_load_refs(s);
6298 if (!s->selected_entry)
6300 if (view_is_parent_view(view))
6301 begin_x = view_split_begin_x(view->begin_x);
6302 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6305 log_view->focussed = 1;
6306 if (view_is_parent_view(view)) {
6307 err = view_close_child(view);
6310 view_set_child(view, log_view);
6311 view->focus_child = 1;
6313 *new_view = log_view;
6316 if (!s->selected_entry)
6318 if (view_is_parent_view(view))
6319 begin_x = view_split_begin_x(view->begin_x);
6320 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6322 if (err || tree_view == NULL)
6325 tree_view->focussed = 1;
6326 if (view_is_parent_view(view)) {
6327 err = view_close_child(view);
6330 view_set_child(view, tree_view);
6331 view->focus_child = 1;
6333 *new_view = tree_view;
6338 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6343 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6344 for (n = 0; n < view->nlines - 1; n++) {
6347 s->first_displayed_entry = re;
6348 re = TAILQ_PREV(re, tog_reflist_head, entry);
6351 s->selected = n - 1;
6356 if (s->selected > 0) {
6360 ref_scroll_up(s, 1);
6364 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6366 ref_scroll_up(s, MAX(0, view->nlines - 1));
6371 if (s->selected < s->ndisplayed - 1) {
6375 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6376 /* can't scroll any further */
6378 ref_scroll_down(s, 1);
6382 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6383 /* can't scroll any further; move cursor down */
6384 if (s->selected < s->ndisplayed - 1)
6385 s->selected = s->ndisplayed - 1;
6388 ref_scroll_down(s, view->nlines - 1);
6392 err = tog_load_refs(s->repo, s->sort_by_date);
6395 ref_view_free_refs(s);
6396 err = ref_view_load_refs(s);
6399 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6400 s->selected = view->nlines - 2;
6413 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6418 static const struct got_error *
6419 cmd_ref(int argc, char *argv[])
6421 const struct got_error *error;
6422 struct got_repository *repo = NULL;
6423 struct got_worktree *worktree = NULL;
6424 char *cwd = NULL, *repo_path = NULL;
6426 struct tog_view *view;
6428 while ((ch = getopt(argc, argv, "r:")) != -1) {
6431 repo_path = realpath(optarg, NULL);
6432 if (repo_path == NULL)
6433 return got_error_from_errno2("realpath",
6448 if (repo_path == NULL) {
6449 cwd = getcwd(NULL, 0);
6451 return got_error_from_errno("getcwd");
6452 error = got_worktree_open(&worktree, cwd);
6453 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6457 strdup(got_worktree_get_repo_path(worktree));
6459 repo_path = strdup(cwd);
6460 if (repo_path == NULL) {
6461 error = got_error_from_errno("strdup");
6466 error = got_repo_open(&repo, repo_path, NULL);
6472 error = apply_unveil(got_repo_get_path(repo), NULL);
6476 error = tog_load_refs(repo, 0);
6480 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6482 error = got_error_from_errno("view_open");
6486 error = open_ref_view(view, repo);
6491 /* Release work tree lock. */
6492 got_worktree_close(worktree);
6495 error = view_loop(view);
6500 const struct got_error *close_err = got_repo_close(repo);
6509 list_commands(FILE *fp)
6513 fprintf(fp, "commands:");
6514 for (i = 0; i < nitems(tog_commands); i++) {
6515 const struct tog_cmd *cmd = &tog_commands[i];
6516 fprintf(fp, " %s", cmd->name);
6522 usage(int hflag, int status)
6524 FILE *fp = (status == 0) ? stdout : stderr;
6526 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6529 fprintf(fp, "lazy usage: %s path\n", getprogname());
6536 make_argv(int argc, ...)
6544 argv = calloc(argc, sizeof(char *));
6547 for (i = 0; i < argc; i++) {
6548 argv[i] = strdup(va_arg(ap, char *));
6549 if (argv[i] == NULL)
6558 * Try to convert 'tog path' into a 'tog log path' command.
6559 * The user could simply have mistyped the command rather than knowingly
6560 * provided a path. So check whether argv[0] can in fact be resolved
6561 * to a path in the HEAD commit and print a special error if not.
6562 * This hack is for mpi@ <3
6564 static const struct got_error *
6565 tog_log_with_path(int argc, char *argv[])
6567 const struct got_error *error = NULL, *close_err;
6568 const struct tog_cmd *cmd = NULL;
6569 struct got_repository *repo = NULL;
6570 struct got_worktree *worktree = NULL;
6571 struct got_object_id *commit_id = NULL, *id = NULL;
6572 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6573 char *commit_id_str = NULL, **cmd_argv = NULL;
6575 cwd = getcwd(NULL, 0);
6577 return got_error_from_errno("getcwd");
6579 error = got_worktree_open(&worktree, cwd);
6580 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6584 repo_path = strdup(got_worktree_get_repo_path(worktree));
6586 repo_path = strdup(cwd);
6587 if (repo_path == NULL) {
6588 error = got_error_from_errno("strdup");
6592 error = got_repo_open(&repo, repo_path, NULL);
6596 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6601 error = tog_load_refs(repo, 0);
6604 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6605 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6606 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6611 got_worktree_close(worktree);
6615 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6617 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6619 fprintf(stderr, "%s: '%s' is no known command or path\n",
6620 getprogname(), argv[0]);
6625 close_err = got_repo_close(repo);
6630 error = got_object_id_str(&commit_id_str, commit_id);
6634 cmd = &tog_commands[0]; /* log */
6636 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6637 error = cmd->cmd_main(argc, cmd_argv);
6640 close_err = got_repo_close(repo);
6645 got_worktree_close(worktree);
6647 free(commit_id_str);
6654 for (i = 0; i < argc; i++)
6663 main(int argc, char *argv[])
6665 const struct got_error *error = NULL;
6666 const struct tog_cmd *cmd = NULL;
6667 int ch, hflag = 0, Vflag = 0;
6668 char **cmd_argv = NULL;
6669 static const struct option longopts[] = {
6670 { "version", no_argument, NULL, 'V' },
6674 setlocale(LC_CTYPE, "");
6676 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6696 got_version_print_str();
6701 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6709 /* Build an argument vector which runs a default command. */
6710 cmd = &tog_commands[0];
6712 cmd_argv = make_argv(argc, cmd->name);
6716 /* Did the user specify a command? */
6717 for (i = 0; i < nitems(tog_commands); i++) {
6718 if (strncmp(tog_commands[i].name, argv[0],
6719 strlen(argv[0])) == 0) {
6720 cmd = &tog_commands[i];
6729 /* No command specified; try log with a path */
6730 error = tog_log_with_path(argc, argv);
6735 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6742 for (i = 0; i < argc; i++)
6747 if (error && error->code != GOT_ERR_CANCELLED)
6748 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);