2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
49 #include "got_opentemp.h"
51 #include "got_cancel.h"
52 #include "got_commit_graph.h"
53 #include "got_blame.h"
54 #include "got_privsep.h"
56 #include "got_worktree.h"
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
66 #define CTRL(x) ((x) & 0x1f)
69 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
78 __dead static void usage(int, int);
79 __dead static void usage_log(void);
80 __dead static void usage_diff(void);
81 __dead static void usage_blame(void);
82 __dead static void usage_tree(void);
83 __dead static void usage_ref(void);
85 static const struct got_error* cmd_log(int, char *[]);
86 static const struct got_error* cmd_diff(int, char *[]);
87 static const struct got_error* cmd_blame(int, char *[]);
88 static const struct got_error* cmd_tree(int, char *[]);
89 static const struct got_error* cmd_ref(int, char *[]);
91 static struct tog_cmd tog_commands[] = {
92 { "log", cmd_log, usage_log },
93 { "diff", cmd_diff, usage_diff },
94 { "blame", cmd_blame, usage_blame },
95 { "tree", cmd_tree, usage_tree },
96 { "ref", cmd_ref, usage_ref },
107 #define TOG_EOF_STRING "(END)"
109 struct commit_queue_entry {
110 TAILQ_ENTRY(commit_queue_entry) entry;
111 struct got_object_id *id;
112 struct got_commit_object *commit;
115 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
116 struct commit_queue {
118 struct commit_queue_head head;
122 STAILQ_ENTRY(tog_color) entry;
126 STAILQ_HEAD(tog_colors, tog_color);
128 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
129 static struct got_reflist_object_id_map *tog_refs_idmap;
131 static const struct got_error *
132 tog_load_refs(struct got_repository *repo, int sort_by_date)
134 const struct got_error *err;
136 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
137 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
142 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
149 if (tog_refs_idmap) {
150 got_reflist_object_id_map_free(tog_refs_idmap);
151 tog_refs_idmap = NULL;
153 got_ref_list_free(&tog_refs);
156 static const struct got_error *
157 add_color(struct tog_colors *colors, const char *pattern,
158 int idx, short color)
160 const struct got_error *err = NULL;
161 struct tog_color *tc;
164 if (idx < 1 || idx > COLOR_PAIRS - 1)
167 init_pair(idx, color, -1);
169 tc = calloc(1, sizeof(*tc));
171 return got_error_from_errno("calloc");
172 regerr = regcomp(&tc->regex, pattern,
173 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
175 static char regerr_msg[512];
176 static char err_msg[512];
177 regerror(regerr, &tc->regex, regerr_msg,
179 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
181 err = got_error_msg(GOT_ERR_REGEX, err_msg);
186 STAILQ_INSERT_HEAD(colors, tc, entry);
191 free_colors(struct tog_colors *colors)
193 struct tog_color *tc;
195 while (!STAILQ_EMPTY(colors)) {
196 tc = STAILQ_FIRST(colors);
197 STAILQ_REMOVE_HEAD(colors, entry);
204 get_color(struct tog_colors *colors, int colorpair)
206 struct tog_color *tc = NULL;
208 STAILQ_FOREACH(tc, colors, entry) {
209 if (tc->colorpair == colorpair)
217 default_color_value(const char *envvar)
219 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
220 return COLOR_MAGENTA;
221 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
223 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
225 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
227 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
228 return COLOR_MAGENTA;
229 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
230 return COLOR_MAGENTA;
231 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
233 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
235 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
237 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
239 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
241 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
243 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
244 return COLOR_MAGENTA;
245 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
252 get_color_value(const char *envvar)
254 const char *val = getenv(envvar);
257 return default_color_value(envvar);
259 if (strcasecmp(val, "black") == 0)
261 if (strcasecmp(val, "red") == 0)
263 if (strcasecmp(val, "green") == 0)
265 if (strcasecmp(val, "yellow") == 0)
267 if (strcasecmp(val, "blue") == 0)
269 if (strcasecmp(val, "magenta") == 0)
270 return COLOR_MAGENTA;
271 if (strcasecmp(val, "cyan") == 0)
273 if (strcasecmp(val, "white") == 0)
275 if (strcasecmp(val, "default") == 0)
278 return default_color_value(envvar);
282 struct tog_diff_view_state {
283 struct got_object_id *id1, *id2;
284 const char *label1, *label2;
286 int first_displayed_line;
287 int last_displayed_line;
290 int ignore_whitespace;
292 struct got_repository *repo;
293 struct tog_colors colors;
299 /* passed from log view; may be NULL */
300 struct tog_view *log_view;
303 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
305 struct tog_log_thread_args {
306 pthread_cond_t need_commits;
307 pthread_cond_t commit_loaded;
310 struct got_commit_graph *graph;
311 struct commit_queue *commits;
312 const char *in_repo_path;
313 struct got_object_id *start_id;
314 struct got_repository *repo;
317 struct commit_queue_entry **first_displayed_entry;
318 struct commit_queue_entry **selected_entry;
320 int *search_next_done;
324 struct tog_log_view_state {
325 struct commit_queue commits;
326 struct commit_queue_entry *first_displayed_entry;
327 struct commit_queue_entry *last_displayed_entry;
328 struct commit_queue_entry *selected_entry;
333 struct got_repository *repo;
334 struct got_object_id *start_id;
337 struct tog_log_thread_args thread_args;
338 struct commit_queue_entry *matched_entry;
339 struct commit_queue_entry *search_entry;
340 struct tog_colors colors;
343 #define TOG_COLOR_DIFF_MINUS 1
344 #define TOG_COLOR_DIFF_PLUS 2
345 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
346 #define TOG_COLOR_DIFF_META 4
347 #define TOG_COLOR_TREE_SUBMODULE 5
348 #define TOG_COLOR_TREE_SYMLINK 6
349 #define TOG_COLOR_TREE_DIRECTORY 7
350 #define TOG_COLOR_TREE_EXECUTABLE 8
351 #define TOG_COLOR_COMMIT 9
352 #define TOG_COLOR_AUTHOR 10
353 #define TOG_COLOR_DATE 11
354 #define TOG_COLOR_REFS_HEADS 12
355 #define TOG_COLOR_REFS_TAGS 13
356 #define TOG_COLOR_REFS_REMOTES 14
358 struct tog_blame_cb_args {
359 struct tog_blame_line *lines; /* one per line */
362 struct tog_view *view;
363 struct got_object_id *commit_id;
367 struct tog_blame_thread_args {
369 struct got_repository *repo;
370 struct tog_blame_cb_args *cb_args;
372 got_cancel_cb cancel_cb;
379 struct tog_blame_line *lines;
383 struct tog_blame_thread_args thread_args;
384 struct tog_blame_cb_args cb_args;
388 struct tog_blame_view_state {
389 int first_displayed_line;
390 int last_displayed_line;
395 struct got_object_id_queue blamed_commits;
396 struct got_object_qid *blamed_commit;
398 struct got_repository *repo;
399 struct got_object_id *commit_id;
400 struct tog_blame blame;
402 struct tog_colors colors;
405 struct tog_parent_tree {
406 TAILQ_ENTRY(tog_parent_tree) entry;
407 struct got_tree_object *tree;
408 struct got_tree_entry *first_displayed_entry;
409 struct got_tree_entry *selected_entry;
413 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
415 struct tog_tree_view_state {
417 struct got_object_id *commit_id;/* commit which this tree belongs to */
418 struct got_tree_object *root; /* the commit's root tree entry */
419 struct got_tree_object *tree; /* currently displayed (sub-)tree */
420 struct got_tree_entry *first_displayed_entry;
421 struct got_tree_entry *last_displayed_entry;
422 struct got_tree_entry *selected_entry;
423 int ndisplayed, selected, show_ids;
424 struct tog_parent_trees parents; /* parent trees of current sub-tree */
426 struct got_repository *repo;
427 struct got_tree_entry *matched_entry;
428 struct tog_colors colors;
431 struct tog_reflist_entry {
432 TAILQ_ENTRY(tog_reflist_entry) entry;
433 struct got_reference *ref;
437 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
439 struct tog_ref_view_state {
440 struct tog_reflist_head refs;
441 struct tog_reflist_entry *first_displayed_entry;
442 struct tog_reflist_entry *last_displayed_entry;
443 struct tog_reflist_entry *selected_entry;
444 int nrefs, ndisplayed, selected, show_ids, sort_by_date;
445 struct got_repository *repo;
446 struct tog_reflist_entry *matched_entry;
447 struct tog_colors colors;
451 * We implement two types of views: parent views and child views.
453 * The 'Tab' key switches focus between a parent view and its child view.
454 * Child views are shown side-by-side to their parent view, provided
455 * there is enough screen estate.
457 * When a new view is opened from within a parent view, this new view
458 * becomes a child view of the parent view, replacing any existing child.
460 * When a new view is opened from within a child view, this new view
461 * becomes a parent view which will obscure the views below until the
462 * user quits the new parent view by typing 'q'.
464 * This list of views contains parent views only.
465 * Child views are only pointed to by their parent view.
467 TAILQ_HEAD(tog_view_list_head, tog_view);
470 TAILQ_ENTRY(tog_view) entry;
473 int nlines, ncols, begin_y, begin_x;
474 int lines, cols; /* copies of LINES and COLS */
475 int focussed; /* Only set on one parent or child view at a time. */
477 struct tog_view *parent;
478 struct tog_view *child;
481 * This flag is initially set on parent views when a new child view
482 * is created. It gets toggled when the 'Tab' key switches focus
483 * between parent and child.
484 * The flag indicates whether focus should be passed on to our child
485 * view if this parent view gets picked for focus after another parent
486 * view was closed. This prevents child views from losing focus in such
491 /* type-specific state */
492 enum tog_view_type type;
494 struct tog_diff_view_state diff;
495 struct tog_log_view_state log;
496 struct tog_blame_view_state blame;
497 struct tog_tree_view_state tree;
498 struct tog_ref_view_state ref;
501 const struct got_error *(*show)(struct tog_view *);
502 const struct got_error *(*input)(struct tog_view **,
503 struct tog_view *, int);
504 const struct got_error *(*close)(struct tog_view *);
506 const struct got_error *(*search_start)(struct tog_view *);
507 const struct got_error *(*search_next)(struct tog_view *);
510 #define TOG_SEARCH_FORWARD 1
511 #define TOG_SEARCH_BACKWARD 2
512 int search_next_done;
513 #define TOG_SEARCH_HAVE_MORE 1
514 #define TOG_SEARCH_NO_MORE 2
515 #define TOG_SEARCH_HAVE_NONE 3
520 static const struct got_error *open_diff_view(struct tog_view *,
521 struct got_object_id *, struct got_object_id *,
522 const char *, const char *, int, int, int, struct tog_view *,
523 struct got_repository *);
524 static const struct got_error *show_diff_view(struct tog_view *);
525 static const struct got_error *input_diff_view(struct tog_view **,
526 struct tog_view *, int);
527 static const struct got_error* close_diff_view(struct tog_view *);
528 static const struct got_error *search_start_diff_view(struct tog_view *);
529 static const struct got_error *search_next_diff_view(struct tog_view *);
531 static const struct got_error *open_log_view(struct tog_view *,
532 struct got_object_id *, struct got_repository *,
533 const char *, const char *, int);
534 static const struct got_error * show_log_view(struct tog_view *);
535 static const struct got_error *input_log_view(struct tog_view **,
536 struct tog_view *, int);
537 static const struct got_error *close_log_view(struct tog_view *);
538 static const struct got_error *search_start_log_view(struct tog_view *);
539 static const struct got_error *search_next_log_view(struct tog_view *);
541 static const struct got_error *open_blame_view(struct tog_view *, char *,
542 struct got_object_id *, struct got_repository *);
543 static const struct got_error *show_blame_view(struct tog_view *);
544 static const struct got_error *input_blame_view(struct tog_view **,
545 struct tog_view *, int);
546 static const struct got_error *close_blame_view(struct tog_view *);
547 static const struct got_error *search_start_blame_view(struct tog_view *);
548 static const struct got_error *search_next_blame_view(struct tog_view *);
550 static const struct got_error *open_tree_view(struct tog_view *,
551 struct got_object_id *, const char *, struct got_repository *);
552 static const struct got_error *show_tree_view(struct tog_view *);
553 static const struct got_error *input_tree_view(struct tog_view **,
554 struct tog_view *, int);
555 static const struct got_error *close_tree_view(struct tog_view *);
556 static const struct got_error *search_start_tree_view(struct tog_view *);
557 static const struct got_error *search_next_tree_view(struct tog_view *);
559 static const struct got_error *open_ref_view(struct tog_view *,
560 struct got_repository *);
561 static const struct got_error *show_ref_view(struct tog_view *);
562 static const struct got_error *input_ref_view(struct tog_view **,
563 struct tog_view *, int);
564 static const struct got_error *close_ref_view(struct tog_view *);
565 static const struct got_error *search_start_ref_view(struct tog_view *);
566 static const struct got_error *search_next_ref_view(struct tog_view *);
568 static volatile sig_atomic_t tog_sigwinch_received;
569 static volatile sig_atomic_t tog_sigpipe_received;
570 static volatile sig_atomic_t tog_sigcont_received;
573 tog_sigwinch(int signo)
575 tog_sigwinch_received = 1;
579 tog_sigpipe(int signo)
581 tog_sigpipe_received = 1;
585 tog_sigcont(int signo)
587 tog_sigcont_received = 1;
590 static const struct got_error *
591 view_close(struct tog_view *view)
593 const struct got_error *err = NULL;
596 view_close(view->child);
600 err = view->close(view);
602 del_panel(view->panel);
604 delwin(view->window);
609 static struct tog_view *
610 view_open(int nlines, int ncols, int begin_y, int begin_x,
611 enum tog_view_type type)
613 struct tog_view *view = calloc(1, sizeof(*view));
621 view->nlines = nlines ? nlines : LINES - begin_y;
622 view->ncols = ncols ? ncols : COLS - begin_x;
623 view->begin_y = begin_y;
624 view->begin_x = begin_x;
625 view->window = newwin(nlines, ncols, begin_y, begin_x);
626 if (view->window == NULL) {
630 view->panel = new_panel(view->window);
631 if (view->panel == NULL ||
632 set_panel_userptr(view->panel, view) != OK) {
637 keypad(view->window, TRUE);
642 view_split_begin_x(int begin_x)
644 if (begin_x > 0 || COLS < 120)
646 return (COLS - MAX(COLS / 2, 80));
649 static const struct got_error *view_resize(struct tog_view *);
651 static const struct got_error *
652 view_splitscreen(struct tog_view *view)
654 const struct got_error *err = NULL;
657 view->begin_x = view_split_begin_x(0);
658 view->nlines = LINES;
659 view->ncols = COLS - view->begin_x;
662 err = view_resize(view);
666 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
667 return got_error_from_errno("mvwin");
672 static const struct got_error *
673 view_fullscreen(struct tog_view *view)
675 const struct got_error *err = NULL;
679 view->nlines = LINES;
683 err = view_resize(view);
687 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
688 return got_error_from_errno("mvwin");
694 view_is_parent_view(struct tog_view *view)
696 return view->parent == NULL;
699 static const struct got_error *
700 view_resize(struct tog_view *view)
704 if (view->lines > LINES)
705 nlines = view->nlines - (view->lines - LINES);
707 nlines = view->nlines + (LINES - view->lines);
709 if (view->cols > COLS)
710 ncols = view->ncols - (view->cols - COLS);
712 ncols = view->ncols + (COLS - view->cols);
714 if (wresize(view->window, nlines, ncols) == ERR)
715 return got_error_from_errno("wresize");
716 if (replace_panel(view->panel, view->window) == ERR)
717 return got_error_from_errno("replace_panel");
718 wclear(view->window);
720 view->nlines = nlines;
726 view->child->begin_x = view_split_begin_x(view->begin_x);
727 if (view->child->begin_x == 0) {
728 view_fullscreen(view->child);
729 if (view->child->focussed)
730 show_panel(view->child->panel);
732 show_panel(view->panel);
734 view_splitscreen(view->child);
735 show_panel(view->child->panel);
742 static const struct got_error *
743 view_close_child(struct tog_view *view)
745 const struct got_error *err = NULL;
747 if (view->child == NULL)
750 err = view_close(view->child);
756 view_set_child(struct tog_view *view, struct tog_view *child)
759 child->parent = view;
763 view_is_splitscreen(struct tog_view *view)
765 return view->begin_x > 0;
774 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
775 cols = 80; /* Default */
781 resize_term(lines, cols);
784 static const struct got_error *
785 view_search_start(struct tog_view *view)
787 const struct got_error *err = NULL;
791 if (view->search_started) {
792 regfree(&view->regex);
794 memset(&view->regmatch, 0, sizeof(view->regmatch));
796 view->search_started = 0;
798 if (view->nlines < 1)
801 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
802 wclrtoeol(view->window);
806 ret = wgetnstr(view->window, pattern, sizeof(pattern));
812 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
813 err = view->search_start(view);
815 regfree(&view->regex);
818 view->search_started = 1;
819 view->searching = TOG_SEARCH_FORWARD;
820 view->search_next_done = 0;
821 view->search_next(view);
827 static const struct got_error *
828 view_input(struct tog_view **new, int *done, struct tog_view *view,
829 struct tog_view_list_head *views)
831 const struct got_error *err = NULL;
837 /* Clear "no matches" indicator. */
838 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
839 view->search_next_done == TOG_SEARCH_HAVE_NONE)
840 view->search_next_done = TOG_SEARCH_HAVE_MORE;
842 if (view->searching && !view->search_next_done) {
843 errcode = pthread_mutex_unlock(&tog_mutex);
845 return got_error_set_errno(errcode,
846 "pthread_mutex_unlock");
848 errcode = pthread_mutex_lock(&tog_mutex);
850 return got_error_set_errno(errcode,
851 "pthread_mutex_lock");
852 view->search_next(view);
856 nodelay(stdscr, FALSE);
857 /* Allow threads to make progress while we are waiting for input. */
858 errcode = pthread_mutex_unlock(&tog_mutex);
860 return got_error_set_errno(errcode, "pthread_mutex_unlock");
861 ch = wgetch(view->window);
862 errcode = pthread_mutex_lock(&tog_mutex);
864 return got_error_set_errno(errcode, "pthread_mutex_lock");
865 nodelay(stdscr, TRUE);
867 if (tog_sigwinch_received || tog_sigcont_received) {
869 tog_sigwinch_received = 0;
870 tog_sigcont_received = 0;
871 TAILQ_FOREACH(v, views, entry) {
872 err = view_resize(v);
875 err = v->input(new, v, KEY_RESIZE);
879 err = view_resize(v->child);
882 err = v->child->input(new, v->child,
894 view->child->focussed = 1;
895 view->focus_child = 1;
896 } else if (view->parent) {
898 view->parent->focussed = 1;
899 view->parent->focus_child = 0;
903 err = view->input(new, view, ch);
910 if (view_is_parent_view(view)) {
911 if (view->child == NULL)
913 if (view_is_splitscreen(view->child)) {
915 view->child->focussed = 1;
916 err = view_fullscreen(view->child);
918 err = view_splitscreen(view->child);
921 err = view->child->input(new, view->child,
924 if (view_is_splitscreen(view)) {
925 view->parent->focussed = 0;
927 err = view_fullscreen(view);
929 err = view_splitscreen(view);
933 err = view->input(new, view, KEY_RESIZE);
939 if (view->search_start)
940 view_search_start(view);
942 err = view->input(new, view, ch);
946 if (view->search_started && view->search_next) {
947 view->searching = (ch == 'n' ?
948 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
949 view->search_next_done = 0;
950 view->search_next(view);
952 err = view->input(new, view, ch);
955 err = view->input(new, view, ch);
963 view_vborder(struct tog_view *view)
966 const struct tog_view *view_above;
969 return view_vborder(view->parent);
971 panel = panel_above(view->panel);
975 view_above = panel_userptr(panel);
976 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
977 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
981 view_needs_focus_indication(struct tog_view *view)
983 if (view_is_parent_view(view)) {
984 if (view->child == NULL || view->child->focussed)
986 if (!view_is_splitscreen(view->child))
988 } else if (!view_is_splitscreen(view))
991 return view->focussed;
994 static const struct got_error *
995 view_loop(struct tog_view *view)
997 const struct got_error *err = NULL;
998 struct tog_view_list_head views;
999 struct tog_view *new_view;
1000 int fast_refresh = 10;
1001 int done = 0, errcode;
1003 errcode = pthread_mutex_lock(&tog_mutex);
1005 return got_error_set_errno(errcode, "pthread_mutex_lock");
1008 TAILQ_INSERT_HEAD(&views, view, entry);
1011 err = view->show(view);
1016 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1017 /* Refresh fast during initialization, then become slower. */
1018 if (fast_refresh && fast_refresh-- == 0)
1019 halfdelay(10); /* switch to once per second */
1021 err = view_input(&new_view, &done, view, &views);
1025 struct tog_view *v, *prev = NULL;
1027 if (view_is_parent_view(view))
1028 prev = TAILQ_PREV(view, tog_view_list_head,
1030 else if (view->parent)
1031 prev = view->parent;
1034 view->parent->child = NULL;
1035 view->parent->focus_child = 0;
1037 TAILQ_REMOVE(&views, view, entry);
1039 err = view_close(view);
1044 TAILQ_FOREACH(v, &views, entry) {
1048 if (view == NULL && new_view == NULL) {
1049 /* No view has focus. Try to pick one. */
1052 else if (!TAILQ_EMPTY(&views)) {
1053 view = TAILQ_LAST(&views,
1054 tog_view_list_head);
1057 if (view->focus_child) {
1058 view->child->focussed = 1;
1066 struct tog_view *v, *t;
1067 /* Only allow one parent view per type. */
1068 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1069 if (v->type != new_view->type)
1071 TAILQ_REMOVE(&views, v, entry);
1072 err = view_close(v);
1077 TAILQ_INSERT_TAIL(&views, new_view, entry);
1081 if (view_is_parent_view(view)) {
1082 if (view->child && view->child->focussed)
1085 if (view->parent && view->parent->focussed)
1086 view = view->parent;
1088 show_panel(view->panel);
1089 if (view->child && view_is_splitscreen(view->child))
1090 show_panel(view->child->panel);
1091 if (view->parent && view_is_splitscreen(view)) {
1092 err = view->parent->show(view->parent);
1096 err = view->show(view);
1100 err = view->child->show(view->child);
1109 while (!TAILQ_EMPTY(&views)) {
1110 view = TAILQ_FIRST(&views);
1111 TAILQ_REMOVE(&views, view, entry);
1115 errcode = pthread_mutex_unlock(&tog_mutex);
1116 if (errcode && err == NULL)
1117 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1127 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1132 /* Create newly allocated wide-character string equivalent to a byte string. */
1133 static const struct got_error *
1134 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1137 const struct got_error *err = NULL;
1140 *wlen = mbstowcs(NULL, s, 0);
1141 if (*wlen == (size_t)-1) {
1143 if (errno != EILSEQ)
1144 return got_error_from_errno("mbstowcs");
1146 /* byte string invalid in current encoding; try to "fix" it */
1147 err = got_mbsavis(&vis, &vislen, s);
1150 *wlen = mbstowcs(NULL, vis, 0);
1151 if (*wlen == (size_t)-1) {
1152 err = got_error_from_errno("mbstowcs"); /* give up */
1157 *ws = calloc(*wlen + 1, sizeof(**ws));
1159 err = got_error_from_errno("calloc");
1163 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1164 err = got_error_from_errno("mbstowcs");
1175 /* Format a line for display, ensuring that it won't overflow a width limit. */
1176 static const struct got_error *
1177 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1180 const struct got_error *err = NULL;
1182 wchar_t *wline = NULL;
1189 err = mbs2ws(&wline, &wlen, line);
1193 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1194 wline[wlen - 1] = L'\0';
1197 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1198 wline[wlen - 1] = L'\0';
1204 int width = wcwidth(wline[i]);
1211 if (width == 1 || width == 2) {
1212 if (cols + width > wlimit)
1216 } else if (width == -1) {
1217 if (wline[i] == L'\t') {
1219 ((cols + col_tab_align) % TABSIZE);
1224 if (cols + width > wlimit)
1229 err = got_error_from_errno("wcwidth");
1244 static const struct got_error*
1245 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1246 struct got_object_id *id, struct got_repository *repo)
1248 static const struct got_error *err = NULL;
1249 struct got_reflist_entry *re;
1255 TAILQ_FOREACH(re, refs, entry) {
1256 struct got_tag_object *tag = NULL;
1257 struct got_object_id *ref_id;
1260 name = got_ref_get_name(re->ref);
1261 if (strcmp(name, GOT_REF_HEAD) == 0)
1263 if (strncmp(name, "refs/", 5) == 0)
1265 if (strncmp(name, "got/", 4) == 0)
1267 if (strncmp(name, "heads/", 6) == 0)
1269 if (strncmp(name, "remotes/", 8) == 0) {
1271 s = strstr(name, "/" GOT_REF_HEAD);
1272 if (s != NULL && s[strlen(s)] == '\0')
1275 err = got_ref_resolve(&ref_id, repo, re->ref);
1278 if (strncmp(name, "tags/", 5) == 0) {
1279 err = got_object_open_as_tag(&tag, repo, ref_id);
1281 if (err->code != GOT_ERR_OBJ_TYPE) {
1285 /* Ref points at something other than a tag. */
1290 cmp = got_object_id_cmp(tag ?
1291 got_object_tag_get_object_id(tag) : ref_id, id);
1294 got_object_tag_close(tag);
1298 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1299 s ? ", " : "", name) == -1) {
1300 err = got_error_from_errno("asprintf");
1311 static const struct got_error *
1312 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1317 smallerthan = strchr(author, '<');
1318 if (smallerthan && smallerthan[1] != '\0')
1319 author = smallerthan + 1;
1320 author[strcspn(author, "@>")] = '\0';
1321 return format_line(wauthor, author_width, author, limit, col_tab_align);
1324 static const struct got_error *
1325 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1326 struct got_object_id *id, const size_t date_display_cols,
1327 int author_display_cols)
1329 struct tog_log_view_state *s = &view->state.log;
1330 const struct got_error *err = NULL;
1331 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1332 char *logmsg0 = NULL, *logmsg = NULL;
1333 char *author = NULL;
1334 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1335 int author_width, logmsg_width;
1336 char *newline, *line = NULL;
1338 const int avail = view->ncols;
1340 time_t committer_time;
1341 struct tog_color *tc;
1343 committer_time = got_object_commit_get_committer_time(commit);
1344 if (gmtime_r(&committer_time, &tm) == NULL)
1345 return got_error_from_errno("gmtime_r");
1346 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1347 return got_error(GOT_ERR_NO_SPACE);
1349 if (avail <= date_display_cols)
1350 limit = MIN(sizeof(datebuf) - 1, avail);
1352 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1353 tc = get_color(&s->colors, TOG_COLOR_DATE);
1355 wattr_on(view->window,
1356 COLOR_PAIR(tc->colorpair), NULL);
1357 waddnstr(view->window, datebuf, limit);
1359 wattr_off(view->window,
1360 COLOR_PAIR(tc->colorpair), NULL);
1367 err = got_object_id_str(&id_str, id);
1370 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1372 wattr_on(view->window,
1373 COLOR_PAIR(tc->colorpair), NULL);
1374 wprintw(view->window, "%.8s ", id_str);
1376 wattr_off(view->window,
1377 COLOR_PAIR(tc->colorpair), NULL);
1384 author = strdup(got_object_commit_get_author(commit));
1385 if (author == NULL) {
1386 err = got_error_from_errno("strdup");
1389 err = format_author(&wauthor, &author_width, author, avail - col, col);
1392 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1394 wattr_on(view->window,
1395 COLOR_PAIR(tc->colorpair), NULL);
1396 waddwstr(view->window, wauthor);
1398 wattr_off(view->window,
1399 COLOR_PAIR(tc->colorpair), NULL);
1400 col += author_width;
1401 while (col < avail && author_width < author_display_cols + 2) {
1402 waddch(view->window, ' ');
1409 err = got_object_commit_get_logmsg(&logmsg0, commit);
1413 while (*logmsg == '\n')
1415 newline = strchr(logmsg, '\n');
1418 limit = avail - col;
1419 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1422 waddwstr(view->window, wlogmsg);
1423 col += logmsg_width;
1424 while (col < avail) {
1425 waddch(view->window, ' ');
1437 static struct commit_queue_entry *
1438 alloc_commit_queue_entry(struct got_commit_object *commit,
1439 struct got_object_id *id)
1441 struct commit_queue_entry *entry;
1443 entry = calloc(1, sizeof(*entry));
1448 entry->commit = commit;
1453 pop_commit(struct commit_queue *commits)
1455 struct commit_queue_entry *entry;
1457 entry = TAILQ_FIRST(&commits->head);
1458 TAILQ_REMOVE(&commits->head, entry, entry);
1459 got_object_commit_close(entry->commit);
1460 commits->ncommits--;
1461 /* Don't free entry->id! It is owned by the commit graph. */
1466 free_commits(struct commit_queue *commits)
1468 while (!TAILQ_EMPTY(&commits->head))
1469 pop_commit(commits);
1472 static const struct got_error *
1473 match_commit(int *have_match, struct got_object_id *id,
1474 struct got_commit_object *commit, regex_t *regex)
1476 const struct got_error *err = NULL;
1477 regmatch_t regmatch;
1478 char *id_str = NULL, *logmsg = NULL;
1482 err = got_object_id_str(&id_str, id);
1486 err = got_object_commit_get_logmsg(&logmsg, commit);
1490 if (regexec(regex, got_object_commit_get_author(commit), 1,
1491 ®match, 0) == 0 ||
1492 regexec(regex, got_object_commit_get_committer(commit), 1,
1493 ®match, 0) == 0 ||
1494 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1495 regexec(regex, logmsg, 1, ®match, 0) == 0)
1503 static const struct got_error *
1504 queue_commits(struct tog_log_thread_args *a)
1506 const struct got_error *err = NULL;
1509 * We keep all commits open throughout the lifetime of the log
1510 * view in order to avoid having to re-fetch commits from disk
1511 * while updating the display.
1514 struct got_object_id *id;
1515 struct got_commit_object *commit;
1516 struct commit_queue_entry *entry;
1519 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1521 if (err || id == NULL)
1524 err = got_object_open_as_commit(&commit, a->repo, id);
1527 entry = alloc_commit_queue_entry(commit, id);
1528 if (entry == NULL) {
1529 err = got_error_from_errno("alloc_commit_queue_entry");
1533 errcode = pthread_mutex_lock(&tog_mutex);
1535 err = got_error_set_errno(errcode,
1536 "pthread_mutex_lock");
1540 entry->idx = a->commits->ncommits;
1541 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1542 a->commits->ncommits++;
1544 if (*a->searching == TOG_SEARCH_FORWARD &&
1545 !*a->search_next_done) {
1547 err = match_commit(&have_match, id, commit, a->regex);
1551 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1554 errcode = pthread_mutex_unlock(&tog_mutex);
1555 if (errcode && err == NULL)
1556 err = got_error_set_errno(errcode,
1557 "pthread_mutex_unlock");
1560 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1566 select_commit(struct tog_log_view_state *s)
1568 struct commit_queue_entry *entry;
1571 entry = s->first_displayed_entry;
1573 if (ncommits == s->selected) {
1574 s->selected_entry = entry;
1577 entry = TAILQ_NEXT(entry, entry);
1582 static const struct got_error *
1583 draw_commits(struct tog_view *view)
1585 const struct got_error *err = NULL;
1586 struct tog_log_view_state *s = &view->state.log;
1587 struct commit_queue_entry *entry = s->selected_entry;
1588 const int limit = view->nlines;
1590 int ncommits, author_cols = 4;
1591 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1592 char *refs_str = NULL;
1594 struct tog_color *tc;
1595 static const size_t date_display_cols = 12;
1597 if (s->selected_entry &&
1598 !(view->searching && view->search_next_done == 0)) {
1599 struct got_reflist_head *refs;
1600 err = got_object_id_str(&id_str, s->selected_entry->id);
1603 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1604 s->selected_entry->id);
1606 err = build_refs_str(&refs_str, refs,
1607 s->selected_entry->id, s->repo);
1613 if (s->thread_args.commits_needed == 0)
1614 halfdelay(10); /* disable fast refresh */
1616 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1617 if (asprintf(&ncommits_str, " [%d/%d] %s",
1618 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1619 (view->searching && !view->search_next_done) ?
1620 "searching..." : "loading...") == -1) {
1621 err = got_error_from_errno("asprintf");
1625 const char *search_str = NULL;
1627 if (view->searching) {
1628 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1629 search_str = "no more matches";
1630 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1631 search_str = "no matches found";
1632 else if (!view->search_next_done)
1633 search_str = "searching...";
1636 if (asprintf(&ncommits_str, " [%d/%d] %s",
1637 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1638 search_str ? search_str :
1639 (refs_str ? refs_str : "")) == -1) {
1640 err = got_error_from_errno("asprintf");
1645 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1646 if (asprintf(&header, "commit %s %s%s",
1647 id_str ? id_str : "........................................",
1648 s->in_repo_path, ncommits_str) == -1) {
1649 err = got_error_from_errno("asprintf");
1653 } else if (asprintf(&header, "commit %s%s",
1654 id_str ? id_str : "........................................",
1655 ncommits_str) == -1) {
1656 err = got_error_from_errno("asprintf");
1660 err = format_line(&wline, &width, header, view->ncols, 0);
1664 werase(view->window);
1666 if (view_needs_focus_indication(view))
1667 wstandout(view->window);
1668 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1670 wattr_on(view->window,
1671 COLOR_PAIR(tc->colorpair), NULL);
1672 waddwstr(view->window, wline);
1674 wattr_off(view->window,
1675 COLOR_PAIR(tc->colorpair), NULL);
1676 while (width < view->ncols) {
1677 waddch(view->window, ' ');
1680 if (view_needs_focus_indication(view))
1681 wstandend(view->window);
1686 /* Grow author column size if necessary. */
1687 entry = s->first_displayed_entry;
1693 if (ncommits >= limit - 1)
1695 author = strdup(got_object_commit_get_author(entry->commit));
1696 if (author == NULL) {
1697 err = got_error_from_errno("strdup");
1700 err = format_author(&wauthor, &width, author, COLS,
1702 if (author_cols < width)
1703 author_cols = width;
1707 entry = TAILQ_NEXT(entry, entry);
1710 entry = s->first_displayed_entry;
1711 s->last_displayed_entry = s->first_displayed_entry;
1714 if (ncommits >= limit - 1)
1716 if (ncommits == s->selected)
1717 wstandout(view->window);
1718 err = draw_commit(view, entry->commit, entry->id,
1719 date_display_cols, author_cols);
1720 if (ncommits == s->selected)
1721 wstandend(view->window);
1725 s->last_displayed_entry = entry;
1726 entry = TAILQ_NEXT(entry, entry);
1739 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1741 struct commit_queue_entry *entry;
1744 entry = TAILQ_FIRST(&s->commits.head);
1745 if (s->first_displayed_entry == entry)
1748 entry = s->first_displayed_entry;
1749 while (entry && nscrolled < maxscroll) {
1750 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1752 s->first_displayed_entry = entry;
1758 static const struct got_error *
1759 trigger_log_thread(struct tog_view *view, int wait)
1761 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1764 halfdelay(1); /* fast refresh while loading commits */
1766 while (ta->commits_needed > 0 || ta->load_all) {
1767 if (ta->log_complete)
1770 /* Wake the log thread. */
1771 errcode = pthread_cond_signal(&ta->need_commits);
1773 return got_error_set_errno(errcode,
1774 "pthread_cond_signal");
1777 * The mutex will be released while the view loop waits
1778 * in wgetch(), at which time the log thread will run.
1783 /* Display progress update in log view. */
1784 show_log_view(view);
1788 /* Wait right here while next commit is being loaded. */
1789 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1791 return got_error_set_errno(errcode,
1792 "pthread_cond_wait");
1794 /* Display progress update in log view. */
1795 show_log_view(view);
1803 static const struct got_error *
1804 log_scroll_down(struct tog_view *view, int maxscroll)
1806 struct tog_log_view_state *s = &view->state.log;
1807 const struct got_error *err = NULL;
1808 struct commit_queue_entry *pentry;
1809 int nscrolled = 0, ncommits_needed;
1811 if (s->last_displayed_entry == NULL)
1814 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1815 if (s->commits.ncommits < ncommits_needed &&
1816 !s->thread_args.log_complete) {
1818 * Ask the log thread for required amount of commits.
1820 s->thread_args.commits_needed += maxscroll;
1821 err = trigger_log_thread(view, 1);
1827 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1831 s->last_displayed_entry = pentry;
1833 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1836 s->first_displayed_entry = pentry;
1837 } while (++nscrolled < maxscroll);
1842 static const struct got_error *
1843 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1844 struct got_commit_object *commit, struct got_object_id *commit_id,
1845 struct tog_view *log_view, struct got_repository *repo)
1847 const struct got_error *err;
1848 struct got_object_qid *parent_id;
1849 struct tog_view *diff_view;
1851 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1852 if (diff_view == NULL)
1853 return got_error_from_errno("view_open");
1855 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1856 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1857 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1859 *new_view = diff_view;
1863 static const struct got_error *
1864 tree_view_visit_subtree(struct tog_tree_view_state *s,
1865 struct got_tree_object *subtree)
1867 struct tog_parent_tree *parent;
1869 parent = calloc(1, sizeof(*parent));
1871 return got_error_from_errno("calloc");
1873 parent->tree = s->tree;
1874 parent->first_displayed_entry = s->first_displayed_entry;
1875 parent->selected_entry = s->selected_entry;
1876 parent->selected = s->selected;
1877 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1880 s->first_displayed_entry = NULL;
1884 static const struct got_error *
1885 tree_view_walk_path(struct tog_tree_view_state *s,
1886 struct got_object_id *commit_id, const char *path)
1888 const struct got_error *err = NULL;
1889 struct got_tree_object *tree = NULL;
1891 char *slash, *subpath = NULL;
1893 /* Walk the path and open corresponding tree objects. */
1896 struct got_tree_entry *te;
1897 struct got_object_id *tree_id;
1903 /* Ensure the correct subtree entry is selected. */
1904 slash = strchr(p, '/');
1906 te_name = strdup(p);
1908 te_name = strndup(p, slash - p);
1909 if (te_name == NULL) {
1910 err = got_error_from_errno("strndup");
1913 te = got_object_tree_find_entry(s->tree, te_name);
1915 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1920 s->first_displayed_entry = s->selected_entry = te;
1922 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1923 break; /* jump to this file's entry */
1925 slash = strchr(p, '/');
1927 subpath = strndup(path, slash - path);
1929 subpath = strdup(path);
1930 if (subpath == NULL) {
1931 err = got_error_from_errno("strdup");
1935 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1940 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1945 err = tree_view_visit_subtree(s, tree);
1947 got_object_tree_close(tree);
1961 static const struct got_error *
1962 browse_commit_tree(struct tog_view **new_view, int begin_x,
1963 struct commit_queue_entry *entry, const char *path,
1964 const char *head_ref_name, struct got_repository *repo)
1966 const struct got_error *err = NULL;
1967 struct tog_tree_view_state *s;
1968 struct tog_view *tree_view;
1970 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1971 if (tree_view == NULL)
1972 return got_error_from_errno("view_open");
1974 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
1977 s = &tree_view->state.tree;
1979 *new_view = tree_view;
1981 if (got_path_is_root_dir(path))
1984 return tree_view_walk_path(s, entry->id, path);
1987 static const struct got_error *
1988 block_signals_used_by_main_thread(void)
1993 if (sigemptyset(&sigset) == -1)
1994 return got_error_from_errno("sigemptyset");
1996 /* tog handles SIGWINCH and SIGCONT */
1997 if (sigaddset(&sigset, SIGWINCH) == -1)
1998 return got_error_from_errno("sigaddset");
1999 if (sigaddset(&sigset, SIGCONT) == -1)
2000 return got_error_from_errno("sigaddset");
2002 /* ncurses handles SIGTSTP */
2003 if (sigaddset(&sigset, SIGTSTP) == -1)
2004 return got_error_from_errno("sigaddset");
2006 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2008 return got_error_set_errno(errcode, "pthread_sigmask");
2014 log_thread(void *arg)
2016 const struct got_error *err = NULL;
2018 struct tog_log_thread_args *a = arg;
2021 err = block_signals_used_by_main_thread();
2025 while (!done && !err && !tog_sigpipe_received) {
2026 err = queue_commits(a);
2028 if (err->code != GOT_ERR_ITER_COMPLETED)
2032 } else if (a->commits_needed > 0 && !a->load_all)
2033 a->commits_needed--;
2035 errcode = pthread_mutex_lock(&tog_mutex);
2037 err = got_error_set_errno(errcode,
2038 "pthread_mutex_lock");
2040 } else if (*a->quit)
2042 else if (*a->first_displayed_entry == NULL) {
2043 *a->first_displayed_entry =
2044 TAILQ_FIRST(&a->commits->head);
2045 *a->selected_entry = *a->first_displayed_entry;
2048 errcode = pthread_cond_signal(&a->commit_loaded);
2050 err = got_error_set_errno(errcode,
2051 "pthread_cond_signal");
2052 pthread_mutex_unlock(&tog_mutex);
2057 a->commits_needed = 0;
2059 if (a->commits_needed == 0 && !a->load_all) {
2060 errcode = pthread_cond_wait(&a->need_commits,
2063 err = got_error_set_errno(errcode,
2064 "pthread_cond_wait");
2070 errcode = pthread_mutex_unlock(&tog_mutex);
2071 if (errcode && err == NULL)
2072 err = got_error_set_errno(errcode,
2073 "pthread_mutex_unlock");
2075 a->log_complete = 1;
2079 static const struct got_error *
2080 stop_log_thread(struct tog_log_view_state *s)
2082 const struct got_error *err = NULL;
2087 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2089 return got_error_set_errno(errcode,
2090 "pthread_cond_signal");
2091 errcode = pthread_mutex_unlock(&tog_mutex);
2093 return got_error_set_errno(errcode,
2094 "pthread_mutex_unlock");
2095 errcode = pthread_join(s->thread, (void **)&err);
2097 return got_error_set_errno(errcode, "pthread_join");
2098 errcode = pthread_mutex_lock(&tog_mutex);
2100 return got_error_set_errno(errcode,
2101 "pthread_mutex_lock");
2105 if (s->thread_args.repo) {
2106 err = got_repo_close(s->thread_args.repo);
2107 s->thread_args.repo = NULL;
2110 if (s->thread_args.graph) {
2111 got_commit_graph_close(s->thread_args.graph);
2112 s->thread_args.graph = NULL;
2118 static const struct got_error *
2119 close_log_view(struct tog_view *view)
2121 const struct got_error *err = NULL;
2122 struct tog_log_view_state *s = &view->state.log;
2125 err = stop_log_thread(s);
2127 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2128 if (errcode && err == NULL)
2129 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2131 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2132 if (errcode && err == NULL)
2133 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2135 free_commits(&s->commits);
2136 free(s->in_repo_path);
2137 s->in_repo_path = NULL;
2140 free(s->head_ref_name);
2141 s->head_ref_name = NULL;
2145 static const struct got_error *
2146 search_start_log_view(struct tog_view *view)
2148 struct tog_log_view_state *s = &view->state.log;
2150 s->matched_entry = NULL;
2151 s->search_entry = NULL;
2155 static const struct got_error *
2156 search_next_log_view(struct tog_view *view)
2158 const struct got_error *err = NULL;
2159 struct tog_log_view_state *s = &view->state.log;
2160 struct commit_queue_entry *entry;
2162 /* Display progress update in log view. */
2163 show_log_view(view);
2167 if (s->search_entry) {
2169 errcode = pthread_mutex_unlock(&tog_mutex);
2171 return got_error_set_errno(errcode,
2172 "pthread_mutex_unlock");
2173 ch = wgetch(view->window);
2174 errcode = pthread_mutex_lock(&tog_mutex);
2176 return got_error_set_errno(errcode,
2177 "pthread_mutex_lock");
2178 if (ch == KEY_BACKSPACE) {
2179 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2182 if (view->searching == TOG_SEARCH_FORWARD)
2183 entry = TAILQ_NEXT(s->search_entry, entry);
2185 entry = TAILQ_PREV(s->search_entry,
2186 commit_queue_head, entry);
2187 } else if (s->matched_entry) {
2188 if (view->searching == TOG_SEARCH_FORWARD)
2189 entry = TAILQ_NEXT(s->matched_entry, entry);
2191 entry = TAILQ_PREV(s->matched_entry,
2192 commit_queue_head, entry);
2194 entry = s->selected_entry;
2200 if (entry == NULL) {
2201 if (s->thread_args.log_complete ||
2202 view->searching == TOG_SEARCH_BACKWARD) {
2203 view->search_next_done =
2204 (s->matched_entry == NULL ?
2205 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2206 s->search_entry = NULL;
2210 * Poke the log thread for more commits and return,
2211 * allowing the main loop to make progress. Search
2212 * will resume at s->search_entry once we come back.
2214 s->thread_args.commits_needed++;
2215 return trigger_log_thread(view, 0);
2218 err = match_commit(&have_match, entry->id, entry->commit,
2223 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2224 s->matched_entry = entry;
2228 s->search_entry = entry;
2229 if (view->searching == TOG_SEARCH_FORWARD)
2230 entry = TAILQ_NEXT(entry, entry);
2232 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2235 if (s->matched_entry) {
2236 int cur = s->selected_entry->idx;
2237 while (cur < s->matched_entry->idx) {
2238 err = input_log_view(NULL, view, KEY_DOWN);
2243 while (cur > s->matched_entry->idx) {
2244 err = input_log_view(NULL, view, KEY_UP);
2251 s->search_entry = NULL;
2256 static const struct got_error *
2257 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2258 struct got_repository *repo, const char *head_ref_name,
2259 const char *in_repo_path, int log_branches)
2261 const struct got_error *err = NULL;
2262 struct tog_log_view_state *s = &view->state.log;
2263 struct got_repository *thread_repo = NULL;
2264 struct got_commit_graph *thread_graph = NULL;
2267 if (in_repo_path != s->in_repo_path) {
2268 free(s->in_repo_path);
2269 s->in_repo_path = strdup(in_repo_path);
2270 if (s->in_repo_path == NULL)
2271 return got_error_from_errno("strdup");
2274 /* The commit queue only contains commits being displayed. */
2275 TAILQ_INIT(&s->commits.head);
2276 s->commits.ncommits = 0;
2279 if (head_ref_name) {
2280 s->head_ref_name = strdup(head_ref_name);
2281 if (s->head_ref_name == NULL) {
2282 err = got_error_from_errno("strdup");
2286 s->start_id = got_object_id_dup(start_id);
2287 if (s->start_id == NULL) {
2288 err = got_error_from_errno("got_object_id_dup");
2291 s->log_branches = log_branches;
2293 STAILQ_INIT(&s->colors);
2294 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2295 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2296 get_color_value("TOG_COLOR_COMMIT"));
2299 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2300 get_color_value("TOG_COLOR_AUTHOR"));
2302 free_colors(&s->colors);
2305 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2306 get_color_value("TOG_COLOR_DATE"));
2308 free_colors(&s->colors);
2313 view->show = show_log_view;
2314 view->input = input_log_view;
2315 view->close = close_log_view;
2316 view->search_start = search_start_log_view;
2317 view->search_next = search_next_log_view;
2319 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2322 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2326 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2327 s->repo, NULL, NULL);
2331 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2333 err = got_error_set_errno(errcode, "pthread_cond_init");
2336 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2338 err = got_error_set_errno(errcode, "pthread_cond_init");
2342 s->thread_args.commits_needed = view->nlines;
2343 s->thread_args.graph = thread_graph;
2344 s->thread_args.commits = &s->commits;
2345 s->thread_args.in_repo_path = s->in_repo_path;
2346 s->thread_args.start_id = s->start_id;
2347 s->thread_args.repo = thread_repo;
2348 s->thread_args.log_complete = 0;
2349 s->thread_args.quit = &s->quit;
2350 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2351 s->thread_args.selected_entry = &s->selected_entry;
2352 s->thread_args.searching = &view->searching;
2353 s->thread_args.search_next_done = &view->search_next_done;
2354 s->thread_args.regex = &view->regex;
2357 close_log_view(view);
2361 static const struct got_error *
2362 show_log_view(struct tog_view *view)
2364 const struct got_error *err;
2365 struct tog_log_view_state *s = &view->state.log;
2367 if (s->thread == NULL) {
2368 int errcode = pthread_create(&s->thread, NULL, log_thread,
2371 return got_error_set_errno(errcode, "pthread_create");
2372 if (s->thread_args.commits_needed > 0) {
2373 err = trigger_log_thread(view, 1);
2379 return draw_commits(view);
2382 static const struct got_error *
2383 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2385 const struct got_error *err = NULL;
2386 struct tog_log_view_state *s = &view->state.log;
2387 struct tog_view *diff_view = NULL, *tree_view = NULL;
2388 struct tog_view *ref_view = NULL;
2389 struct commit_queue_entry *entry;
2392 if (s->thread_args.load_all) {
2393 if (ch == KEY_BACKSPACE)
2394 s->thread_args.load_all = 0;
2395 else if (s->thread_args.log_complete) {
2396 s->thread_args.load_all = 0;
2397 log_scroll_down(view, s->commits.ncommits);
2398 s->selected = MIN(view->nlines - 2,
2399 s->commits.ncommits - 1);
2414 if (s->first_displayed_entry == NULL)
2416 if (s->selected > 0)
2419 log_scroll_up(s, 1);
2425 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2430 if (s->first_displayed_entry == NULL)
2432 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2435 log_scroll_up(s, view->nlines - 1);
2443 if (s->first_displayed_entry == NULL)
2445 if (s->selected < MIN(view->nlines - 2,
2446 s->commits.ncommits - 1))
2449 err = log_scroll_down(view, 1);
2457 /* We don't know yet how many commits, so we're forced to
2458 * traverse them all. */
2459 if (!s->thread_args.log_complete) {
2460 s->thread_args.load_all = 1;
2461 return trigger_log_thread(view, 0);
2465 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2466 for (n = 0; n < view->nlines - 1; n++) {
2469 s->first_displayed_entry = entry;
2470 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2473 s->selected = n - 1;
2479 struct commit_queue_entry *first;
2480 first = s->first_displayed_entry;
2483 err = log_scroll_down(view, view->nlines - 1);
2486 if (first == s->first_displayed_entry &&
2487 s->selected < MIN(view->nlines - 2,
2488 s->commits.ncommits - 1)) {
2489 /* can't scroll further down */
2490 s->selected = MIN(view->nlines - 2,
2491 s->commits.ncommits - 1);
2497 if (s->selected > view->nlines - 2)
2498 s->selected = view->nlines - 2;
2499 if (s->selected > s->commits.ncommits - 1)
2500 s->selected = s->commits.ncommits - 1;
2502 if (s->commits.ncommits < view->nlines - 1 &&
2503 !s->thread_args.log_complete) {
2504 s->thread_args.commits_needed += (view->nlines - 1) -
2505 s->commits.ncommits;
2506 err = trigger_log_thread(view, 1);
2512 if (s->selected_entry == NULL)
2514 if (view_is_parent_view(view))
2515 begin_x = view_split_begin_x(view->begin_x);
2516 err = open_diff_view_for_commit(&diff_view, begin_x,
2517 s->selected_entry->commit, s->selected_entry->id,
2522 diff_view->focussed = 1;
2523 if (view_is_parent_view(view)) {
2524 err = view_close_child(view);
2527 view_set_child(view, diff_view);
2528 view->focus_child = 1;
2530 *new_view = diff_view;
2533 if (s->selected_entry == NULL)
2535 if (view_is_parent_view(view))
2536 begin_x = view_split_begin_x(view->begin_x);
2537 err = browse_commit_tree(&tree_view, begin_x,
2538 s->selected_entry, s->in_repo_path, s->head_ref_name,
2543 tree_view->focussed = 1;
2544 if (view_is_parent_view(view)) {
2545 err = view_close_child(view);
2548 view_set_child(view, tree_view);
2549 view->focus_child = 1;
2551 *new_view = tree_view;
2556 if (ch == KEY_BACKSPACE &&
2557 got_path_is_root_dir(s->in_repo_path))
2559 err = stop_log_thread(s);
2562 if (ch == KEY_BACKSPACE) {
2564 err = got_path_dirname(&parent_path, s->in_repo_path);
2567 free(s->in_repo_path);
2568 s->in_repo_path = parent_path;
2569 s->thread_args.in_repo_path = s->in_repo_path;
2570 } else if (ch == CTRL('l')) {
2571 struct got_object_id *start_id;
2572 err = got_repo_match_object_id(&start_id, NULL,
2573 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2574 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2578 s->start_id = start_id;
2579 s->thread_args.start_id = s->start_id;
2581 s->log_branches = !s->log_branches;
2583 err = got_repo_open(&s->thread_args.repo,
2584 got_repo_get_path(s->repo), NULL);
2588 err = tog_load_refs(s->repo, 0);
2591 err = got_commit_graph_open(&s->thread_args.graph,
2592 s->in_repo_path, !s->log_branches);
2595 err = got_commit_graph_iter_start(s->thread_args.graph,
2596 s->start_id, s->repo, NULL, NULL);
2599 free_commits(&s->commits);
2600 s->first_displayed_entry = NULL;
2601 s->last_displayed_entry = NULL;
2602 s->selected_entry = NULL;
2604 s->thread_args.log_complete = 0;
2606 s->thread_args.commits_needed = view->nlines;
2609 if (view_is_parent_view(view))
2610 begin_x = view_split_begin_x(view->begin_x);
2611 ref_view = view_open(view->nlines, view->ncols,
2612 view->begin_y, begin_x, TOG_VIEW_REF);
2613 if (ref_view == NULL)
2614 return got_error_from_errno("view_open");
2615 err = open_ref_view(ref_view, s->repo);
2617 view_close(ref_view);
2621 ref_view->focussed = 1;
2622 if (view_is_parent_view(view)) {
2623 err = view_close_child(view);
2626 view_set_child(view, ref_view);
2627 view->focus_child = 1;
2629 *new_view = ref_view;
2638 static const struct got_error *
2639 apply_unveil(const char *repo_path, const char *worktree_path)
2641 const struct got_error *error;
2644 if (unveil("gmon.out", "rwc") != 0)
2645 return got_error_from_errno2("unveil", "gmon.out");
2647 if (repo_path && unveil(repo_path, "r") != 0)
2648 return got_error_from_errno2("unveil", repo_path);
2650 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2651 return got_error_from_errno2("unveil", worktree_path);
2653 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2654 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2656 error = got_privsep_unveil_exec_helpers();
2660 if (unveil(NULL, NULL) != 0)
2661 return got_error_from_errno("unveil");
2671 halfdelay(1); /* Do fast refresh while initial view is loading. */
2674 intrflush(stdscr, FALSE);
2675 keypad(stdscr, TRUE);
2677 if (getenv("TOG_COLORS") != NULL) {
2679 use_default_colors();
2681 signal(SIGWINCH, tog_sigwinch);
2682 signal(SIGPIPE, tog_sigpipe);
2683 signal(SIGCONT, tog_sigcont);
2686 static const struct got_error *
2687 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2688 struct got_repository *repo, struct got_worktree *worktree)
2690 const struct got_error *err = NULL;
2693 *in_repo_path = strdup("/");
2694 if (*in_repo_path == NULL)
2695 return got_error_from_errno("strdup");
2700 const char *prefix = got_worktree_get_path_prefix(worktree);
2703 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2706 if (asprintf(in_repo_path, "%s%s%s", prefix,
2707 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2709 err = got_error_from_errno("asprintf");
2710 *in_repo_path = NULL;
2714 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2719 static const struct got_error *
2720 cmd_log(int argc, char *argv[])
2722 const struct got_error *error;
2723 struct got_repository *repo = NULL;
2724 struct got_worktree *worktree = NULL;
2725 struct got_object_id *start_id = NULL;
2726 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2727 char *start_commit = NULL, *label = NULL;
2728 struct got_reference *ref = NULL;
2729 const char *head_ref_name = NULL;
2730 int ch, log_branches = 0;
2731 struct tog_view *view;
2733 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2739 start_commit = optarg;
2742 repo_path = realpath(optarg, NULL);
2743 if (repo_path == NULL)
2744 return got_error_from_errno2("realpath",
2759 if (repo_path == NULL) {
2760 cwd = getcwd(NULL, 0);
2762 return got_error_from_errno("getcwd");
2763 error = got_worktree_open(&worktree, cwd);
2764 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2768 strdup(got_worktree_get_repo_path(worktree));
2770 repo_path = strdup(cwd);
2771 if (repo_path == NULL) {
2772 error = got_error_from_errno("strdup");
2777 error = got_repo_open(&repo, repo_path, NULL);
2781 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2788 error = apply_unveil(got_repo_get_path(repo),
2789 worktree ? got_worktree_get_root_path(worktree) : NULL);
2793 /* already loaded by tog_log_with_path()? */
2794 if (TAILQ_EMPTY(&tog_refs)) {
2795 error = tog_load_refs(repo, 0);
2800 if (start_commit == NULL) {
2801 error = got_repo_match_object_id(&start_id, &label,
2802 worktree ? got_worktree_get_head_ref_name(worktree) :
2803 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2806 head_ref_name = label;
2808 error = got_ref_open(&ref, repo, start_commit, 0);
2810 head_ref_name = got_ref_get_name(ref);
2811 else if (error->code != GOT_ERR_NOT_REF)
2813 error = got_repo_match_object_id(&start_id, NULL,
2814 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2819 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2821 error = got_error_from_errno("view_open");
2824 error = open_log_view(view, start_id, repo, head_ref_name,
2825 in_repo_path, log_branches);
2829 /* Release work tree lock. */
2830 got_worktree_close(worktree);
2833 error = view_loop(view);
2843 const struct got_error *close_err = got_repo_close(repo);
2848 got_worktree_close(worktree);
2857 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2858 "[-w] object1 object2\n", getprogname());
2863 match_line(const char *line, regex_t *regex, size_t nmatch,
2864 regmatch_t *regmatch)
2866 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2870 match_color(struct tog_colors *colors, const char *line)
2872 struct tog_color *tc = NULL;
2874 STAILQ_FOREACH(tc, colors, entry) {
2875 if (match_line(line, &tc->regex, 0, NULL))
2882 static const struct got_error *
2883 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2884 WINDOW *window, regmatch_t *regmatch)
2886 const struct got_error *err = NULL;
2893 s = strndup(line, regmatch->rm_so);
2895 return got_error_from_errno("strndup");
2897 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2902 waddwstr(window, wline);
2909 s = strndup(line + regmatch->rm_so,
2910 regmatch->rm_eo - regmatch->rm_so);
2912 err = got_error_from_errno("strndup");
2916 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2921 wattr_on(window, A_STANDOUT, NULL);
2922 waddwstr(window, wline);
2923 wattr_off(window, A_STANDOUT, NULL);
2930 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2931 err = format_line(&wline, &width,
2932 line + regmatch->rm_eo, wlimit, col_tab_align);
2935 waddwstr(window, wline);
2943 static const struct got_error *
2944 draw_file(struct tog_view *view, const char *header)
2946 struct tog_diff_view_state *s = &view->state.diff;
2947 regmatch_t *regmatch = &view->regmatch;
2948 const struct got_error *err;
2951 size_t linesize = 0;
2953 struct tog_color *tc;
2956 int max_lines = view->nlines;
2957 int nlines = s->nlines;
2960 line_offset = s->line_offsets[s->first_displayed_line - 1];
2961 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2962 return got_error_from_errno("fseek");
2964 werase(view->window);
2967 if (asprintf(&line, "[%d/%d] %s",
2968 s->first_displayed_line - 1 + s->selected_line, nlines,
2970 return got_error_from_errno("asprintf");
2971 err = format_line(&wline, &width, line, view->ncols, 0);
2976 if (view_needs_focus_indication(view))
2977 wstandout(view->window);
2978 waddwstr(view->window, wline);
2981 if (view_needs_focus_indication(view))
2982 wstandend(view->window);
2983 if (width <= view->ncols - 1)
2984 waddch(view->window, '\n');
2993 while (max_lines > 0 && nprinted < max_lines) {
2994 linelen = getline(&line, &linesize, s->f);
2995 if (linelen == -1) {
3001 return got_ferror(s->f, GOT_ERR_IO);
3004 tc = match_color(&s->colors, line);
3006 wattr_on(view->window,
3007 COLOR_PAIR(tc->colorpair), NULL);
3008 if (s->first_displayed_line + nprinted == s->matched_line &&
3009 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3010 err = add_matched_line(&width, line, view->ncols, 0,
3011 view->window, regmatch);
3017 err = format_line(&wline, &width, line, view->ncols, 0);
3022 waddwstr(view->window, wline);
3027 wattr_off(view->window,
3028 COLOR_PAIR(tc->colorpair), NULL);
3029 if (width <= view->ncols - 1)
3030 waddch(view->window, '\n');
3035 s->last_displayed_line = s->first_displayed_line +
3038 s->last_displayed_line = s->first_displayed_line;
3043 while (nprinted < view->nlines) {
3044 waddch(view->window, '\n');
3048 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3053 wstandout(view->window);
3054 waddwstr(view->window, wline);
3057 wstandend(view->window);
3064 get_datestr(time_t *time, char *datebuf)
3066 struct tm mytm, *tm;
3069 tm = gmtime_r(time, &mytm);
3072 s = asctime_r(tm, datebuf);
3075 p = strchr(s, '\n');
3081 static const struct got_error *
3082 get_changed_paths(struct got_pathlist_head *paths,
3083 struct got_commit_object *commit, struct got_repository *repo)
3085 const struct got_error *err = NULL;
3086 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3087 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3088 struct got_object_qid *qid;
3090 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3092 struct got_commit_object *pcommit;
3093 err = got_object_open_as_commit(&pcommit, repo,
3098 tree_id1 = got_object_id_dup(
3099 got_object_commit_get_tree_id(pcommit));
3100 if (tree_id1 == NULL) {
3101 got_object_commit_close(pcommit);
3102 return got_error_from_errno("got_object_id_dup");
3104 got_object_commit_close(pcommit);
3109 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3114 tree_id2 = got_object_commit_get_tree_id(commit);
3115 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3119 err = got_diff_tree(tree1, tree2, "", "", repo,
3120 got_diff_tree_collect_changed_paths, paths, 0);
3123 got_object_tree_close(tree1);
3125 got_object_tree_close(tree2);
3130 static const struct got_error *
3131 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3135 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3137 return got_error_from_errno("reallocarray");
3139 (*line_offsets)[*nlines] = off;
3144 static const struct got_error *
3145 write_commit_info(off_t **line_offsets, size_t *nlines,
3146 struct got_object_id *commit_id, struct got_reflist_head *refs,
3147 struct got_repository *repo, FILE *outfile)
3149 const struct got_error *err = NULL;
3150 char datebuf[26], *datestr;
3151 struct got_commit_object *commit;
3152 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3153 time_t committer_time;
3154 const char *author, *committer;
3155 char *refs_str = NULL;
3156 struct got_pathlist_head changed_paths;
3157 struct got_pathlist_entry *pe;
3161 TAILQ_INIT(&changed_paths);
3164 err = build_refs_str(&refs_str, refs, commit_id, repo);
3169 err = got_object_open_as_commit(&commit, repo, commit_id);
3173 err = got_object_id_str(&id_str, commit_id);
3175 err = got_error_from_errno("got_object_id_str");
3179 err = add_line_offset(line_offsets, nlines, 0);
3183 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3184 refs_str ? refs_str : "", refs_str ? ")" : "");
3186 err = got_error_from_errno("fprintf");
3190 err = add_line_offset(line_offsets, nlines, outoff);
3194 n = fprintf(outfile, "from: %s\n",
3195 got_object_commit_get_author(commit));
3197 err = got_error_from_errno("fprintf");
3201 err = add_line_offset(line_offsets, nlines, outoff);
3205 committer_time = got_object_commit_get_committer_time(commit);
3206 datestr = get_datestr(&committer_time, datebuf);
3208 n = fprintf(outfile, "date: %s UTC\n", datestr);
3210 err = got_error_from_errno("fprintf");
3214 err = add_line_offset(line_offsets, nlines, outoff);
3218 author = got_object_commit_get_author(commit);
3219 committer = got_object_commit_get_committer(commit);
3220 if (strcmp(author, committer) != 0) {
3221 n = fprintf(outfile, "via: %s\n", committer);
3223 err = got_error_from_errno("fprintf");
3227 err = add_line_offset(line_offsets, nlines, outoff);
3231 if (got_object_commit_get_nparents(commit) > 1) {
3232 const struct got_object_id_queue *parent_ids;
3233 struct got_object_qid *qid;
3235 parent_ids = got_object_commit_get_parent_ids(commit);
3236 STAILQ_FOREACH(qid, parent_ids, entry) {
3237 err = got_object_id_str(&id_str, qid->id);
3240 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3242 err = got_error_from_errno("fprintf");
3246 err = add_line_offset(line_offsets, nlines, outoff);
3254 err = got_object_commit_get_logmsg(&logmsg, commit);
3258 while ((line = strsep(&s, "\n")) != NULL) {
3259 n = fprintf(outfile, "%s\n", line);
3261 err = got_error_from_errno("fprintf");
3265 err = add_line_offset(line_offsets, nlines, outoff);
3270 err = get_changed_paths(&changed_paths, commit, repo);
3273 TAILQ_FOREACH(pe, &changed_paths, entry) {
3274 struct got_diff_changed_path *cp = pe->data;
3275 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3277 err = got_error_from_errno("fprintf");
3281 err = add_line_offset(line_offsets, nlines, outoff);
3284 free((char *)pe->path);
3288 fputc('\n', outfile);
3290 err = add_line_offset(line_offsets, nlines, outoff);
3292 got_pathlist_free(&changed_paths);
3296 got_object_commit_close(commit);
3298 free(*line_offsets);
3299 *line_offsets = NULL;
3305 static const struct got_error *
3306 create_diff(struct tog_diff_view_state *s)
3308 const struct got_error *err = NULL;
3312 free(s->line_offsets);
3313 s->line_offsets = malloc(sizeof(off_t));
3314 if (s->line_offsets == NULL)
3315 return got_error_from_errno("malloc");
3320 err = got_error_from_errno("got_opentemp");
3323 if (s->f && fclose(s->f) == EOF) {
3324 err = got_error_from_errno("fclose");
3330 err = got_object_get_type(&obj_type, s->repo, s->id1);
3332 err = got_object_get_type(&obj_type, s->repo, s->id2);
3337 case GOT_OBJ_TYPE_BLOB:
3338 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3339 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3340 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3342 case GOT_OBJ_TYPE_TREE:
3343 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3344 s->id1, s->id2, NULL, "", "", s->diff_context,
3345 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3347 case GOT_OBJ_TYPE_COMMIT: {
3348 const struct got_object_id_queue *parent_ids;
3349 struct got_object_qid *pid;
3350 struct got_commit_object *commit2;
3351 struct got_reflist_head *refs;
3353 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3356 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3357 /* Show commit info if we're diffing to a parent/root commit. */
3358 if (s->id1 == NULL) {
3359 err = write_commit_info(&s->line_offsets, &s->nlines,
3360 s->id2, refs, s->repo, s->f);
3364 parent_ids = got_object_commit_get_parent_ids(commit2);
3365 STAILQ_FOREACH(pid, parent_ids, entry) {
3366 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3367 err = write_commit_info(
3368 &s->line_offsets, &s->nlines,
3369 s->id2, refs, s->repo, s->f);
3376 got_object_commit_close(commit2);
3378 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3379 s->id1, s->id2, NULL, s->diff_context, s->ignore_whitespace,
3380 s->force_text_diff, s->repo, s->f);
3384 err = got_error(GOT_ERR_OBJ_TYPE);
3390 if (s->f && fflush(s->f) != 0 && err == NULL)
3391 err = got_error_from_errno("fflush");
3396 diff_view_indicate_progress(struct tog_view *view)
3398 mvwaddstr(view->window, 0, 0, "diffing...");
3403 static const struct got_error *
3404 search_start_diff_view(struct tog_view *view)
3406 struct tog_diff_view_state *s = &view->state.diff;
3408 s->matched_line = 0;
3412 static const struct got_error *
3413 search_next_diff_view(struct tog_view *view)
3415 struct tog_diff_view_state *s = &view->state.diff;
3418 size_t linesize = 0;
3421 if (!view->searching) {
3422 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3426 if (s->matched_line) {
3427 if (view->searching == TOG_SEARCH_FORWARD)
3428 lineno = s->matched_line + 1;
3430 lineno = s->matched_line - 1;
3432 if (view->searching == TOG_SEARCH_FORWARD)
3441 if (lineno <= 0 || lineno > s->nlines) {
3442 if (s->matched_line == 0) {
3443 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3447 if (view->searching == TOG_SEARCH_FORWARD)
3453 offset = s->line_offsets[lineno - 1];
3454 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3456 return got_error_from_errno("fseeko");
3458 linelen = getline(&line, &linesize, s->f);
3459 if (linelen != -1 &&
3460 match_line(line, &view->regex, 1, &view->regmatch)) {
3461 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3462 s->matched_line = lineno;
3465 if (view->searching == TOG_SEARCH_FORWARD)
3472 if (s->matched_line) {
3473 s->first_displayed_line = s->matched_line;
3474 s->selected_line = 1;
3480 static const struct got_error *
3481 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3482 struct got_object_id *id2, const char *label1, const char *label2,
3483 int diff_context, int ignore_whitespace, int force_text_diff,
3484 struct tog_view *log_view, struct got_repository *repo)
3486 const struct got_error *err;
3487 struct tog_diff_view_state *s = &view->state.diff;
3489 if (id1 != NULL && id2 != NULL) {
3491 err = got_object_get_type(&type1, repo, id1);
3494 err = got_object_get_type(&type2, repo, id2);
3499 return got_error(GOT_ERR_OBJ_TYPE);
3501 s->first_displayed_line = 1;
3502 s->last_displayed_line = view->nlines;
3503 s->selected_line = 1;
3511 s->id1 = got_object_id_dup(id1);
3513 return got_error_from_errno("got_object_id_dup");
3517 s->id2 = got_object_id_dup(id2);
3518 if (s->id2 == NULL) {
3521 return got_error_from_errno("got_object_id_dup");
3524 s->first_displayed_line = 1;
3525 s->last_displayed_line = view->nlines;
3526 s->diff_context = diff_context;
3527 s->ignore_whitespace = ignore_whitespace;
3528 s->force_text_diff = force_text_diff;
3529 s->log_view = log_view;
3532 STAILQ_INIT(&s->colors);
3533 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3534 err = add_color(&s->colors,
3535 "^-", TOG_COLOR_DIFF_MINUS,
3536 get_color_value("TOG_COLOR_DIFF_MINUS"));
3539 err = add_color(&s->colors, "^\\+",
3540 TOG_COLOR_DIFF_PLUS,
3541 get_color_value("TOG_COLOR_DIFF_PLUS"));
3543 free_colors(&s->colors);
3546 err = add_color(&s->colors,
3547 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3548 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3550 free_colors(&s->colors);
3554 err = add_color(&s->colors,
3555 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3556 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3557 get_color_value("TOG_COLOR_DIFF_META"));
3559 free_colors(&s->colors);
3563 err = add_color(&s->colors,
3564 "^(from|via): ", TOG_COLOR_AUTHOR,
3565 get_color_value("TOG_COLOR_AUTHOR"));
3567 free_colors(&s->colors);
3571 err = add_color(&s->colors,
3572 "^date: ", TOG_COLOR_DATE,
3573 get_color_value("TOG_COLOR_DATE"));
3575 free_colors(&s->colors);
3580 if (log_view && view_is_splitscreen(view))
3581 show_log_view(log_view); /* draw vborder */
3582 diff_view_indicate_progress(view);
3584 s->line_offsets = NULL;
3586 err = create_diff(s);
3592 free_colors(&s->colors);
3596 view->show = show_diff_view;
3597 view->input = input_diff_view;
3598 view->close = close_diff_view;
3599 view->search_start = search_start_diff_view;
3600 view->search_next = search_next_diff_view;
3605 static const struct got_error *
3606 close_diff_view(struct tog_view *view)
3608 const struct got_error *err = NULL;
3609 struct tog_diff_view_state *s = &view->state.diff;
3615 if (s->f && fclose(s->f) == EOF)
3616 err = got_error_from_errno("fclose");
3617 free_colors(&s->colors);
3618 free(s->line_offsets);
3619 s->line_offsets = NULL;
3624 static const struct got_error *
3625 show_diff_view(struct tog_view *view)
3627 const struct got_error *err;
3628 struct tog_diff_view_state *s = &view->state.diff;
3629 char *id_str1 = NULL, *id_str2, *header;
3630 const char *label1, *label2;
3633 err = got_object_id_str(&id_str1, s->id1);
3636 label1 = s->label1 ? : id_str1;
3638 label1 = "/dev/null";
3640 err = got_object_id_str(&id_str2, s->id2);
3643 label2 = s->label2 ? : id_str2;
3645 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3646 err = got_error_from_errno("asprintf");
3654 err = draw_file(view, header);
3659 static const struct got_error *
3660 set_selected_commit(struct tog_diff_view_state *s,
3661 struct commit_queue_entry *entry)
3663 const struct got_error *err;
3664 const struct got_object_id_queue *parent_ids;
3665 struct got_commit_object *selected_commit;
3666 struct got_object_qid *pid;
3669 s->id2 = got_object_id_dup(entry->id);
3671 return got_error_from_errno("got_object_id_dup");
3673 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3676 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3678 pid = STAILQ_FIRST(parent_ids);
3679 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3680 got_object_commit_close(selected_commit);
3684 static const struct got_error *
3685 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3687 const struct got_error *err = NULL;
3688 struct tog_diff_view_state *s = &view->state.diff;
3689 struct tog_log_view_state *ls;
3690 struct commit_queue_entry *old_selected_entry;
3692 size_t linesize = 0;
3700 s->force_text_diff = !s->force_text_diff;
3702 s->ignore_whitespace = !s->ignore_whitespace;
3703 wclear(view->window);
3704 s->first_displayed_line = 1;
3705 s->last_displayed_line = view->nlines;
3706 diff_view_indicate_progress(view);
3707 err = create_diff(s);
3711 s->first_displayed_line = 1;
3718 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3724 if (s->first_displayed_line > 1)
3725 s->first_displayed_line--;
3729 if (s->first_displayed_line == 1)
3732 while (i++ < view->nlines - 1 &&
3733 s->first_displayed_line > 1)
3734 s->first_displayed_line--;
3740 s->first_displayed_line++;
3748 while (!s->eof && i++ < view->nlines - 1) {
3749 linelen = getline(&line, &linesize, s->f);
3750 s->first_displayed_line++;
3751 if (linelen == -1) {
3755 err = got_ferror(s->f, GOT_ERR_IO);
3762 if (s->diff_context > 0) {
3764 diff_view_indicate_progress(view);
3765 err = create_diff(s);
3766 if (s->first_displayed_line + view->nlines - 1 >
3768 s->first_displayed_line = 1;
3769 s->last_displayed_line = view->nlines;
3774 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3776 diff_view_indicate_progress(view);
3777 err = create_diff(s);
3782 if (s->log_view == NULL)
3784 ls = &s->log_view->state.log;
3785 old_selected_entry = ls->selected_entry;
3787 err = input_log_view(NULL, s->log_view, KEY_UP);
3791 if (old_selected_entry == ls->selected_entry)
3794 err = set_selected_commit(s, ls->selected_entry);
3798 s->first_displayed_line = 1;
3799 s->last_displayed_line = view->nlines;
3801 diff_view_indicate_progress(view);
3802 err = create_diff(s);
3806 if (s->log_view == NULL)
3808 ls = &s->log_view->state.log;
3809 old_selected_entry = ls->selected_entry;
3811 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3815 if (old_selected_entry == ls->selected_entry)
3818 err = set_selected_commit(s, ls->selected_entry);
3822 s->first_displayed_line = 1;
3823 s->last_displayed_line = view->nlines;
3825 diff_view_indicate_progress(view);
3826 err = create_diff(s);
3835 static const struct got_error *
3836 cmd_diff(int argc, char *argv[])
3838 const struct got_error *error = NULL;
3839 struct got_repository *repo = NULL;
3840 struct got_worktree *worktree = NULL;
3841 struct got_object_id *id1 = NULL, *id2 = NULL;
3842 char *repo_path = NULL, *cwd = NULL;
3843 char *id_str1 = NULL, *id_str2 = NULL;
3844 char *label1 = NULL, *label2 = NULL;
3845 int diff_context = 3, ignore_whitespace = 0;
3846 int ch, force_text_diff = 0;
3848 struct tog_view *view;
3850 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3853 force_text_diff = 1;
3856 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3859 err(1, "-C option %s", errstr);
3862 repo_path = realpath(optarg, NULL);
3863 if (repo_path == NULL)
3864 return got_error_from_errno2("realpath",
3866 got_path_strip_trailing_slashes(repo_path);
3869 ignore_whitespace = 1;
3881 usage_diff(); /* TODO show local worktree changes */
3882 } else if (argc == 2) {
3888 if (repo_path == NULL) {
3889 cwd = getcwd(NULL, 0);
3891 return got_error_from_errno("getcwd");
3892 error = got_worktree_open(&worktree, cwd);
3893 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3897 strdup(got_worktree_get_repo_path(worktree));
3899 repo_path = strdup(cwd);
3900 if (repo_path == NULL) {
3901 error = got_error_from_errno("strdup");
3906 error = got_repo_open(&repo, repo_path, NULL);
3912 error = apply_unveil(got_repo_get_path(repo), NULL);
3916 error = tog_load_refs(repo, 0);
3920 error = got_repo_match_object_id(&id1, &label1, id_str1,
3921 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3925 error = got_repo_match_object_id(&id2, &label2, id_str2,
3926 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3930 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3932 error = got_error_from_errno("view_open");
3935 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3936 ignore_whitespace, force_text_diff, NULL, repo);
3939 error = view_loop(view);
3946 const struct got_error *close_err = got_repo_close(repo);
3951 got_worktree_close(worktree);
3960 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3965 struct tog_blame_line {
3967 struct got_object_id *id;
3970 static const struct got_error *
3971 draw_blame(struct tog_view *view)
3973 struct tog_blame_view_state *s = &view->state.blame;
3974 struct tog_blame *blame = &s->blame;
3975 regmatch_t *regmatch = &view->regmatch;
3976 const struct got_error *err;
3977 int lineno = 0, nprinted = 0;
3979 size_t linesize = 0;
3983 struct tog_blame_line *blame_line;
3984 struct got_object_id *prev_id = NULL;
3986 struct tog_color *tc;
3988 err = got_object_id_str(&id_str, s->blamed_commit->id);
3993 werase(view->window);
3995 if (asprintf(&line, "commit %s", id_str) == -1) {
3996 err = got_error_from_errno("asprintf");
4001 err = format_line(&wline, &width, line, view->ncols, 0);
4006 if (view_needs_focus_indication(view))
4007 wstandout(view->window);
4008 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4010 wattr_on(view->window,
4011 COLOR_PAIR(tc->colorpair), NULL);
4012 waddwstr(view->window, wline);
4014 wattr_off(view->window,
4015 COLOR_PAIR(tc->colorpair), NULL);
4016 if (view_needs_focus_indication(view))
4017 wstandend(view->window);
4020 if (width < view->ncols - 1)
4021 waddch(view->window, '\n');
4023 if (asprintf(&line, "[%d/%d] %s%s",
4024 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4025 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4027 return got_error_from_errno("asprintf");
4030 err = format_line(&wline, &width, line, view->ncols, 0);
4035 waddwstr(view->window, wline);
4038 if (width < view->ncols - 1)
4039 waddch(view->window, '\n');
4042 while (nprinted < view->nlines - 2) {
4043 linelen = getline(&line, &linesize, blame->f);
4044 if (linelen == -1) {
4045 if (feof(blame->f)) {
4050 return got_ferror(blame->f, GOT_ERR_IO);
4052 if (++lineno < s->first_displayed_line)
4055 if (view->focussed && nprinted == s->selected_line - 1)
4056 wstandout(view->window);
4058 if (blame->nlines > 0) {
4059 blame_line = &blame->lines[lineno - 1];
4060 if (blame_line->annotated && prev_id &&
4061 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4063 nprinted == s->selected_line - 1)) {
4064 waddstr(view->window, " ");
4065 } else if (blame_line->annotated) {
4067 err = got_object_id_str(&id_str, blame_line->id);
4072 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4074 wattr_on(view->window,
4075 COLOR_PAIR(tc->colorpair), NULL);
4076 wprintw(view->window, "%.8s", id_str);
4078 wattr_off(view->window,
4079 COLOR_PAIR(tc->colorpair), NULL);
4081 prev_id = blame_line->id;
4083 waddstr(view->window, "........");
4087 waddstr(view->window, "........");
4091 if (view->focussed && nprinted == s->selected_line - 1)
4092 wstandend(view->window);
4093 waddstr(view->window, " ");
4095 if (view->ncols <= 9) {
4097 wline = wcsdup(L"");
4098 if (wline == NULL) {
4099 err = got_error_from_errno("wcsdup");
4103 } else if (s->first_displayed_line + nprinted ==
4105 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4106 err = add_matched_line(&width, line, view->ncols - 9, 9,
4107 view->window, regmatch);
4114 err = format_line(&wline, &width, line,
4115 view->ncols - 9, 9);
4116 waddwstr(view->window, wline);
4122 if (width <= view->ncols - 1)
4123 waddch(view->window, '\n');
4124 if (++nprinted == 1)
4125 s->first_displayed_line = lineno;
4128 s->last_displayed_line = lineno;
4135 static const struct got_error *
4136 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4138 const struct got_error *err = NULL;
4139 struct tog_blame_cb_args *a = arg;
4140 struct tog_blame_line *line;
4143 if (nlines != a->nlines ||
4144 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4145 return got_error(GOT_ERR_RANGE);
4147 errcode = pthread_mutex_lock(&tog_mutex);
4149 return got_error_set_errno(errcode, "pthread_mutex_lock");
4151 if (*a->quit) { /* user has quit the blame view */
4152 err = got_error(GOT_ERR_ITER_COMPLETED);
4157 goto done; /* no change in this commit */
4159 line = &a->lines[lineno - 1];
4160 if (line->annotated)
4163 line->id = got_object_id_dup(id);
4164 if (line->id == NULL) {
4165 err = got_error_from_errno("got_object_id_dup");
4168 line->annotated = 1;
4170 errcode = pthread_mutex_unlock(&tog_mutex);
4172 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4177 blame_thread(void *arg)
4179 const struct got_error *err, *close_err;
4180 struct tog_blame_thread_args *ta = arg;
4181 struct tog_blame_cb_args *a = ta->cb_args;
4184 err = block_signals_used_by_main_thread();
4188 err = got_blame(ta->path, a->commit_id, ta->repo,
4189 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4190 if (err && err->code == GOT_ERR_CANCELLED)
4193 errcode = pthread_mutex_lock(&tog_mutex);
4195 return (void *)got_error_set_errno(errcode,
4196 "pthread_mutex_lock");
4198 close_err = got_repo_close(ta->repo);
4204 errcode = pthread_mutex_unlock(&tog_mutex);
4205 if (errcode && err == NULL)
4206 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4211 static struct got_object_id *
4212 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4213 int first_displayed_line, int selected_line)
4215 struct tog_blame_line *line;
4220 line = &lines[first_displayed_line - 1 + selected_line - 1];
4221 if (!line->annotated)
4227 static const struct got_error *
4228 stop_blame(struct tog_blame *blame)
4230 const struct got_error *err = NULL;
4233 if (blame->thread) {
4235 errcode = pthread_mutex_unlock(&tog_mutex);
4237 return got_error_set_errno(errcode,
4238 "pthread_mutex_unlock");
4239 errcode = pthread_join(blame->thread, (void **)&err);
4241 return got_error_set_errno(errcode, "pthread_join");
4242 errcode = pthread_mutex_lock(&tog_mutex);
4244 return got_error_set_errno(errcode,
4245 "pthread_mutex_lock");
4246 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4248 blame->thread = NULL;
4250 if (blame->thread_args.repo) {
4251 const struct got_error *close_err;
4252 close_err = got_repo_close(blame->thread_args.repo);
4255 blame->thread_args.repo = NULL;
4258 if (fclose(blame->f) == EOF && err == NULL)
4259 err = got_error_from_errno("fclose");
4263 for (i = 0; i < blame->nlines; i++)
4264 free(blame->lines[i].id);
4266 blame->lines = NULL;
4268 free(blame->cb_args.commit_id);
4269 blame->cb_args.commit_id = NULL;
4274 static const struct got_error *
4275 cancel_blame_view(void *arg)
4277 const struct got_error *err = NULL;
4281 errcode = pthread_mutex_lock(&tog_mutex);
4283 return got_error_set_errno(errcode,
4284 "pthread_mutex_unlock");
4287 err = got_error(GOT_ERR_CANCELLED);
4289 errcode = pthread_mutex_unlock(&tog_mutex);
4291 return got_error_set_errno(errcode,
4292 "pthread_mutex_lock");
4297 static const struct got_error *
4298 run_blame(struct tog_view *view)
4300 struct tog_blame_view_state *s = &view->state.blame;
4301 struct tog_blame *blame = &s->blame;
4302 const struct got_error *err = NULL;
4303 struct got_blob_object *blob = NULL;
4304 struct got_repository *thread_repo = NULL;
4305 struct got_object_id *obj_id = NULL;
4308 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4313 err = got_object_get_type(&obj_type, s->repo, obj_id);
4317 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4318 err = got_error(GOT_ERR_OBJ_TYPE);
4322 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4325 blame->f = got_opentemp();
4326 if (blame->f == NULL) {
4327 err = got_error_from_errno("got_opentemp");
4330 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4331 &blame->line_offsets, blame->f, blob);
4334 if (blame->nlines == 0) {
4335 s->blame_complete = 1;
4339 /* Don't include \n at EOF in the blame line count. */
4340 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4343 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4344 if (blame->lines == NULL) {
4345 err = got_error_from_errno("calloc");
4349 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4353 blame->cb_args.view = view;
4354 blame->cb_args.lines = blame->lines;
4355 blame->cb_args.nlines = blame->nlines;
4356 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4357 if (blame->cb_args.commit_id == NULL) {
4358 err = got_error_from_errno("got_object_id_dup");
4361 blame->cb_args.quit = &s->done;
4363 blame->thread_args.path = s->path;
4364 blame->thread_args.repo = thread_repo;
4365 blame->thread_args.cb_args = &blame->cb_args;
4366 blame->thread_args.complete = &s->blame_complete;
4367 blame->thread_args.cancel_cb = cancel_blame_view;
4368 blame->thread_args.cancel_arg = &s->done;
4369 s->blame_complete = 0;
4371 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4372 s->first_displayed_line = 1;
4373 s->last_displayed_line = view->nlines;
4374 s->selected_line = 1;
4379 got_object_blob_close(blob);
4386 static const struct got_error *
4387 open_blame_view(struct tog_view *view, char *path,
4388 struct got_object_id *commit_id, struct got_repository *repo)
4390 const struct got_error *err = NULL;
4391 struct tog_blame_view_state *s = &view->state.blame;
4393 STAILQ_INIT(&s->blamed_commits);
4395 s->path = strdup(path);
4396 if (s->path == NULL)
4397 return got_error_from_errno("strdup");
4399 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4405 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4406 s->first_displayed_line = 1;
4407 s->last_displayed_line = view->nlines;
4408 s->selected_line = 1;
4409 s->blame_complete = 0;
4411 s->commit_id = commit_id;
4412 memset(&s->blame, 0, sizeof(s->blame));
4414 STAILQ_INIT(&s->colors);
4415 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4416 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4417 get_color_value("TOG_COLOR_COMMIT"));
4422 view->show = show_blame_view;
4423 view->input = input_blame_view;
4424 view->close = close_blame_view;
4425 view->search_start = search_start_blame_view;
4426 view->search_next = search_next_blame_view;
4428 return run_blame(view);
4431 static const struct got_error *
4432 close_blame_view(struct tog_view *view)
4434 const struct got_error *err = NULL;
4435 struct tog_blame_view_state *s = &view->state.blame;
4437 if (s->blame.thread)
4438 err = stop_blame(&s->blame);
4440 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4441 struct got_object_qid *blamed_commit;
4442 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4443 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4444 got_object_qid_free(blamed_commit);
4448 free_colors(&s->colors);
4453 static const struct got_error *
4454 search_start_blame_view(struct tog_view *view)
4456 struct tog_blame_view_state *s = &view->state.blame;
4458 s->matched_line = 0;
4462 static const struct got_error *
4463 search_next_blame_view(struct tog_view *view)
4465 struct tog_blame_view_state *s = &view->state.blame;
4468 size_t linesize = 0;
4471 if (!view->searching) {
4472 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4476 if (s->matched_line) {
4477 if (view->searching == TOG_SEARCH_FORWARD)
4478 lineno = s->matched_line + 1;
4480 lineno = s->matched_line - 1;
4482 if (view->searching == TOG_SEARCH_FORWARD)
4485 lineno = s->blame.nlines;
4491 if (lineno <= 0 || lineno > s->blame.nlines) {
4492 if (s->matched_line == 0) {
4493 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4497 if (view->searching == TOG_SEARCH_FORWARD)
4500 lineno = s->blame.nlines;
4503 offset = s->blame.line_offsets[lineno - 1];
4504 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4506 return got_error_from_errno("fseeko");
4508 linelen = getline(&line, &linesize, s->blame.f);
4509 if (linelen != -1 &&
4510 match_line(line, &view->regex, 1, &view->regmatch)) {
4511 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4512 s->matched_line = lineno;
4515 if (view->searching == TOG_SEARCH_FORWARD)
4522 if (s->matched_line) {
4523 s->first_displayed_line = s->matched_line;
4524 s->selected_line = 1;
4530 static const struct got_error *
4531 show_blame_view(struct tog_view *view)
4533 const struct got_error *err = NULL;
4534 struct tog_blame_view_state *s = &view->state.blame;
4537 if (s->blame.thread == NULL && !s->blame_complete) {
4538 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4539 &s->blame.thread_args);
4541 return got_error_set_errno(errcode, "pthread_create");
4543 halfdelay(1); /* fast refresh while annotating */
4546 if (s->blame_complete)
4547 halfdelay(10); /* disable fast refresh */
4549 err = draw_blame(view);
4555 static const struct got_error *
4556 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4558 const struct got_error *err = NULL, *thread_err = NULL;
4559 struct tog_view *diff_view;
4560 struct tog_blame_view_state *s = &view->state.blame;
4569 s->selected_line = 1;
4570 s->first_displayed_line = 1;
4574 if (s->blame.nlines < view->nlines - 2) {
4575 s->selected_line = s->blame.nlines;
4576 s->first_displayed_line = 1;
4578 s->selected_line = view->nlines - 2;
4579 s->first_displayed_line = s->blame.nlines -
4586 if (s->selected_line > 1)
4588 else if (s->selected_line == 1 &&
4589 s->first_displayed_line > 1)
4590 s->first_displayed_line--;
4594 if (s->first_displayed_line == 1) {
4595 s->selected_line = 1;
4598 if (s->first_displayed_line > view->nlines - 2)
4599 s->first_displayed_line -=
4602 s->first_displayed_line = 1;
4607 if (s->selected_line < view->nlines - 2 &&
4608 s->first_displayed_line +
4609 s->selected_line <= s->blame.nlines)
4611 else if (s->last_displayed_line <
4613 s->first_displayed_line++;
4617 struct got_object_id *id = NULL;
4618 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4619 s->first_displayed_line, s->selected_line);
4623 struct got_commit_object *commit;
4624 struct got_object_qid *pid;
4625 struct got_object_id *blob_id = NULL;
4627 err = got_object_open_as_commit(&commit,
4632 got_object_commit_get_parent_ids(commit));
4634 got_object_commit_close(commit);
4637 /* Check if path history ends here. */
4638 err = got_object_id_by_path(&blob_id, s->repo,
4641 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4643 got_object_commit_close(commit);
4646 err = got_object_get_type(&obj_type, s->repo,
4649 /* Can't blame non-blob type objects. */
4650 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4651 got_object_commit_close(commit);
4654 err = got_object_qid_alloc(&s->blamed_commit,
4656 got_object_commit_close(commit);
4658 if (got_object_id_cmp(id,
4659 s->blamed_commit->id) == 0)
4661 err = got_object_qid_alloc(&s->blamed_commit,
4667 thread_err = stop_blame(&s->blame);
4671 STAILQ_INSERT_HEAD(&s->blamed_commits,
4672 s->blamed_commit, entry);
4673 err = run_blame(view);
4679 struct got_object_qid *first;
4680 first = STAILQ_FIRST(&s->blamed_commits);
4681 if (!got_object_id_cmp(first->id, s->commit_id))
4684 thread_err = stop_blame(&s->blame);
4688 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4689 got_object_qid_free(s->blamed_commit);
4691 STAILQ_FIRST(&s->blamed_commits);
4692 err = run_blame(view);
4699 struct got_object_id *id = NULL;
4700 struct got_object_qid *pid;
4701 struct got_commit_object *commit = NULL;
4702 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4703 s->first_displayed_line, s->selected_line);
4706 err = got_object_open_as_commit(&commit, s->repo, id);
4710 got_object_commit_get_parent_ids(commit));
4711 if (view_is_parent_view(view))
4712 begin_x = view_split_begin_x(view->begin_x);
4713 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4714 if (diff_view == NULL) {
4715 got_object_commit_close(commit);
4716 err = got_error_from_errno("view_open");
4719 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4720 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4721 got_object_commit_close(commit);
4723 view_close(diff_view);
4727 diff_view->focussed = 1;
4728 if (view_is_parent_view(view)) {
4729 err = view_close_child(view);
4732 view_set_child(view, diff_view);
4733 view->focus_child = 1;
4735 *new_view = diff_view;
4743 if (s->last_displayed_line >= s->blame.nlines &&
4744 s->selected_line >= MIN(s->blame.nlines,
4745 view->nlines - 2)) {
4748 if (s->last_displayed_line >= s->blame.nlines &&
4749 s->selected_line < view->nlines - 2) {
4750 s->selected_line = MIN(s->blame.nlines,
4754 if (s->last_displayed_line + view->nlines - 2
4756 s->first_displayed_line +=
4759 s->first_displayed_line =
4764 if (s->selected_line > view->nlines - 2) {
4765 s->selected_line = MIN(s->blame.nlines,
4772 return thread_err ? thread_err : err;
4775 static const struct got_error *
4776 cmd_blame(int argc, char *argv[])
4778 const struct got_error *error;
4779 struct got_repository *repo = NULL;
4780 struct got_worktree *worktree = NULL;
4781 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4782 char *link_target = NULL;
4783 struct got_object_id *commit_id = NULL;
4784 char *commit_id_str = NULL;
4786 struct tog_view *view;
4788 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4791 commit_id_str = optarg;
4794 repo_path = realpath(optarg, NULL);
4795 if (repo_path == NULL)
4796 return got_error_from_errno2("realpath",
4811 if (repo_path == NULL) {
4812 cwd = getcwd(NULL, 0);
4814 return got_error_from_errno("getcwd");
4815 error = got_worktree_open(&worktree, cwd);
4816 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4820 strdup(got_worktree_get_repo_path(worktree));
4822 repo_path = strdup(cwd);
4823 if (repo_path == NULL) {
4824 error = got_error_from_errno("strdup");
4829 error = got_repo_open(&repo, repo_path, NULL);
4833 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4840 error = apply_unveil(got_repo_get_path(repo), NULL);
4844 error = tog_load_refs(repo, 0);
4848 if (commit_id_str == NULL) {
4849 struct got_reference *head_ref;
4850 error = got_ref_open(&head_ref, repo, worktree ?
4851 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4854 error = got_ref_resolve(&commit_id, repo, head_ref);
4855 got_ref_close(head_ref);
4857 error = got_repo_match_object_id(&commit_id, NULL,
4858 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4863 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4865 error = got_error_from_errno("view_open");
4869 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4874 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4879 /* Release work tree lock. */
4880 got_worktree_close(worktree);
4883 error = view_loop(view);
4891 got_worktree_close(worktree);
4893 const struct got_error *close_err = got_repo_close(repo);
4901 static const struct got_error *
4902 draw_tree_entries(struct tog_view *view, const char *parent_path)
4904 struct tog_tree_view_state *s = &view->state.tree;
4905 const struct got_error *err = NULL;
4906 struct got_tree_entry *te;
4908 struct tog_color *tc;
4909 int width, n, i, nentries;
4910 int limit = view->nlines;
4914 werase(view->window);
4919 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4922 if (view_needs_focus_indication(view))
4923 wstandout(view->window);
4924 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4926 wattr_on(view->window,
4927 COLOR_PAIR(tc->colorpair), NULL);
4928 waddwstr(view->window, wline);
4930 wattr_off(view->window,
4931 COLOR_PAIR(tc->colorpair), NULL);
4932 if (view_needs_focus_indication(view))
4933 wstandend(view->window);
4936 if (width < view->ncols - 1)
4937 waddch(view->window, '\n');
4940 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4943 waddwstr(view->window, wline);
4946 if (width < view->ncols - 1)
4947 waddch(view->window, '\n');
4950 waddch(view->window, '\n');
4954 if (s->first_displayed_entry == NULL) {
4955 te = got_object_tree_get_first_entry(s->tree);
4956 if (s->selected == 0) {
4958 wstandout(view->window);
4959 s->selected_entry = NULL;
4961 waddstr(view->window, " ..\n"); /* parent directory */
4962 if (s->selected == 0 && view->focussed)
4963 wstandend(view->window);
4970 te = s->first_displayed_entry;
4973 nentries = got_object_tree_get_nentries(s->tree);
4974 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4975 char *line = NULL, *id_str = NULL, *link_target = NULL;
4976 const char *modestr = "";
4979 te = got_object_tree_get_entry(s->tree, i);
4980 mode = got_tree_entry_get_mode(te);
4983 err = got_object_id_str(&id_str,
4984 got_tree_entry_get_id(te));
4986 return got_error_from_errno(
4987 "got_object_id_str");
4989 if (got_object_tree_entry_is_submodule(te))
4991 else if (S_ISLNK(mode)) {
4994 err = got_tree_entry_get_symlink_target(&link_target,
5000 for (i = 0; i < strlen(link_target); i++) {
5001 if (!isprint((unsigned char)link_target[i]))
5002 link_target[i] = '?';
5006 else if (S_ISDIR(mode))
5008 else if (mode & S_IXUSR)
5010 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5011 got_tree_entry_get_name(te), modestr,
5012 link_target ? " -> ": "",
5013 link_target ? link_target : "") == -1) {
5016 return got_error_from_errno("asprintf");
5020 err = format_line(&wline, &width, line, view->ncols, 0);
5025 if (n == s->selected) {
5027 wstandout(view->window);
5028 s->selected_entry = te;
5030 tc = match_color(&s->colors, line);
5032 wattr_on(view->window,
5033 COLOR_PAIR(tc->colorpair), NULL);
5034 waddwstr(view->window, wline);
5036 wattr_off(view->window,
5037 COLOR_PAIR(tc->colorpair), NULL);
5038 if (width < view->ncols - 1)
5039 waddch(view->window, '\n');
5040 if (n == s->selected && view->focussed)
5041 wstandend(view->window);
5047 s->last_displayed_entry = te;
5056 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5058 struct got_tree_entry *te;
5059 int isroot = s->tree == s->root;
5062 if (s->first_displayed_entry == NULL)
5065 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5066 while (i++ < maxscroll) {
5069 s->first_displayed_entry = NULL;
5072 s->first_displayed_entry = te;
5073 te = got_tree_entry_get_prev(s->tree, te);
5078 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5080 struct got_tree_entry *next, *last;
5083 if (s->first_displayed_entry)
5084 next = got_tree_entry_get_next(s->tree,
5085 s->first_displayed_entry);
5087 next = got_object_tree_get_first_entry(s->tree);
5089 last = s->last_displayed_entry;
5090 while (next && last && n++ < maxscroll) {
5091 last = got_tree_entry_get_next(s->tree, last);
5093 s->first_displayed_entry = next;
5094 next = got_tree_entry_get_next(s->tree, next);
5099 static const struct got_error *
5100 tree_entry_path(char **path, struct tog_parent_trees *parents,
5101 struct got_tree_entry *te)
5103 const struct got_error *err = NULL;
5104 struct tog_parent_tree *pt;
5105 size_t len = 2; /* for leading slash and NUL */
5107 TAILQ_FOREACH(pt, parents, entry)
5108 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5111 len += strlen(got_tree_entry_get_name(te));
5113 *path = calloc(1, len);
5115 return got_error_from_errno("calloc");
5118 pt = TAILQ_LAST(parents, tog_parent_trees);
5120 const char *name = got_tree_entry_get_name(pt->selected_entry);
5121 if (strlcat(*path, name, len) >= len) {
5122 err = got_error(GOT_ERR_NO_SPACE);
5125 if (strlcat(*path, "/", len) >= len) {
5126 err = got_error(GOT_ERR_NO_SPACE);
5129 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5132 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5133 err = got_error(GOT_ERR_NO_SPACE);
5145 static const struct got_error *
5146 blame_tree_entry(struct tog_view **new_view, int begin_x,
5147 struct got_tree_entry *te, struct tog_parent_trees *parents,
5148 struct got_object_id *commit_id, struct got_repository *repo)
5150 const struct got_error *err = NULL;
5152 struct tog_view *blame_view;
5156 err = tree_entry_path(&path, parents, te);
5160 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5161 if (blame_view == NULL) {
5162 err = got_error_from_errno("view_open");
5166 err = open_blame_view(blame_view, path, commit_id, repo);
5168 if (err->code == GOT_ERR_CANCELLED)
5170 view_close(blame_view);
5172 *new_view = blame_view;
5178 static const struct got_error *
5179 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5180 struct tog_tree_view_state *s)
5182 struct tog_view *log_view;
5183 const struct got_error *err = NULL;
5188 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5189 if (log_view == NULL)
5190 return got_error_from_errno("view_open");
5192 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5196 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5199 view_close(log_view);
5201 *new_view = log_view;
5206 static const struct got_error *
5207 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5208 const char *head_ref_name, struct got_repository *repo)
5210 const struct got_error *err = NULL;
5211 char *commit_id_str = NULL;
5212 struct tog_tree_view_state *s = &view->state.tree;
5213 struct got_commit_object *commit = NULL;
5215 TAILQ_INIT(&s->parents);
5216 STAILQ_INIT(&s->colors);
5218 s->commit_id = got_object_id_dup(commit_id);
5219 if (s->commit_id == NULL)
5220 return got_error_from_errno("got_object_id_dup");
5222 err = got_object_open_as_commit(&commit, repo, commit_id);
5227 * The root is opened here and will be closed when the view is closed.
5228 * Any visited subtrees and their path-wise parents are opened and
5231 err = got_object_open_as_tree(&s->root, repo,
5232 got_object_commit_get_tree_id(commit));
5237 err = got_object_id_str(&commit_id_str, commit_id);
5241 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5242 err = got_error_from_errno("asprintf");
5246 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5247 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5248 if (head_ref_name) {
5249 s->head_ref_name = strdup(head_ref_name);
5250 if (s->head_ref_name == NULL) {
5251 err = got_error_from_errno("strdup");
5257 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5258 err = add_color(&s->colors, "\\$$",
5259 TOG_COLOR_TREE_SUBMODULE,
5260 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5263 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5264 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5267 err = add_color(&s->colors, "/$",
5268 TOG_COLOR_TREE_DIRECTORY,
5269 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5273 err = add_color(&s->colors, "\\*$",
5274 TOG_COLOR_TREE_EXECUTABLE,
5275 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5279 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5280 get_color_value("TOG_COLOR_COMMIT"));
5285 view->show = show_tree_view;
5286 view->input = input_tree_view;
5287 view->close = close_tree_view;
5288 view->search_start = search_start_tree_view;
5289 view->search_next = search_next_tree_view;
5291 free(commit_id_str);
5293 got_object_commit_close(commit);
5295 close_tree_view(view);
5299 static const struct got_error *
5300 close_tree_view(struct tog_view *view)
5302 struct tog_tree_view_state *s = &view->state.tree;
5304 free_colors(&s->colors);
5305 free(s->tree_label);
5306 s->tree_label = NULL;
5308 s->commit_id = NULL;
5309 free(s->head_ref_name);
5310 s->head_ref_name = NULL;
5311 while (!TAILQ_EMPTY(&s->parents)) {
5312 struct tog_parent_tree *parent;
5313 parent = TAILQ_FIRST(&s->parents);
5314 TAILQ_REMOVE(&s->parents, parent, entry);
5315 if (parent->tree != s->root)
5316 got_object_tree_close(parent->tree);
5320 if (s->tree != NULL && s->tree != s->root)
5321 got_object_tree_close(s->tree);
5323 got_object_tree_close(s->root);
5327 static const struct got_error *
5328 search_start_tree_view(struct tog_view *view)
5330 struct tog_tree_view_state *s = &view->state.tree;
5332 s->matched_entry = NULL;
5337 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5339 regmatch_t regmatch;
5341 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5345 static const struct got_error *
5346 search_next_tree_view(struct tog_view *view)
5348 struct tog_tree_view_state *s = &view->state.tree;
5349 struct got_tree_entry *te = NULL;
5351 if (!view->searching) {
5352 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5356 if (s->matched_entry) {
5357 if (view->searching == TOG_SEARCH_FORWARD) {
5358 if (s->selected_entry)
5359 te = got_tree_entry_get_next(s->tree,
5362 te = got_object_tree_get_first_entry(s->tree);
5364 if (s->selected_entry == NULL)
5365 te = got_object_tree_get_last_entry(s->tree);
5367 te = got_tree_entry_get_prev(s->tree,
5371 if (view->searching == TOG_SEARCH_FORWARD)
5372 te = got_object_tree_get_first_entry(s->tree);
5374 te = got_object_tree_get_last_entry(s->tree);
5379 if (s->matched_entry == NULL) {
5380 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5383 if (view->searching == TOG_SEARCH_FORWARD)
5384 te = got_object_tree_get_first_entry(s->tree);
5386 te = got_object_tree_get_last_entry(s->tree);
5389 if (match_tree_entry(te, &view->regex)) {
5390 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5391 s->matched_entry = te;
5395 if (view->searching == TOG_SEARCH_FORWARD)
5396 te = got_tree_entry_get_next(s->tree, te);
5398 te = got_tree_entry_get_prev(s->tree, te);
5401 if (s->matched_entry) {
5402 s->first_displayed_entry = s->matched_entry;
5409 static const struct got_error *
5410 show_tree_view(struct tog_view *view)
5412 const struct got_error *err = NULL;
5413 struct tog_tree_view_state *s = &view->state.tree;
5416 err = tree_entry_path(&parent_path, &s->parents, NULL);
5420 err = draw_tree_entries(view, parent_path);
5427 static const struct got_error *
5428 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5430 const struct got_error *err = NULL;
5431 struct tog_tree_view_state *s = &view->state.tree;
5432 struct tog_view *log_view, *ref_view;
5433 struct got_tree_entry *te;
5438 s->show_ids = !s->show_ids;
5441 if (!s->selected_entry)
5443 if (view_is_parent_view(view))
5444 begin_x = view_split_begin_x(view->begin_x);
5445 err = log_selected_tree_entry(&log_view, begin_x, s);
5447 log_view->focussed = 1;
5448 if (view_is_parent_view(view)) {
5449 err = view_close_child(view);
5452 view_set_child(view, log_view);
5453 view->focus_child = 1;
5455 *new_view = log_view;
5458 if (view_is_parent_view(view))
5459 begin_x = view_split_begin_x(view->begin_x);
5460 ref_view = view_open(view->nlines, view->ncols,
5461 view->begin_y, begin_x, TOG_VIEW_REF);
5462 if (ref_view == NULL)
5463 return got_error_from_errno("view_open");
5464 err = open_ref_view(ref_view, s->repo);
5466 view_close(ref_view);
5470 ref_view->focussed = 1;
5471 if (view_is_parent_view(view)) {
5472 err = view_close_child(view);
5475 view_set_child(view, ref_view);
5476 view->focus_child = 1;
5478 *new_view = ref_view;
5483 if (s->tree == s->root)
5484 s->first_displayed_entry =
5485 got_object_tree_get_first_entry(s->tree);
5487 s->first_displayed_entry = NULL;
5492 te = got_object_tree_get_last_entry(s->tree);
5493 for (n = 0; n < view->nlines - 3; n++) {
5495 if(s->tree != s->root) {
5496 s->first_displayed_entry = NULL;
5501 s->first_displayed_entry = te;
5502 te = got_tree_entry_get_prev(s->tree, te);
5505 s->selected = n - 1;
5510 if (s->selected > 0) {
5514 tree_scroll_up(s, 1);
5518 if (s->tree == s->root) {
5519 if (got_object_tree_get_first_entry(s->tree) ==
5520 s->first_displayed_entry)
5523 if (s->first_displayed_entry == NULL)
5526 tree_scroll_up(s, MAX(0, view->nlines - 3));
5531 if (s->selected < s->ndisplayed - 1) {
5535 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5537 /* can't scroll any further */
5539 tree_scroll_down(s, 1);
5543 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5545 /* can't scroll any further; move cursor down */
5546 if (s->selected < s->ndisplayed - 1)
5547 s->selected = s->ndisplayed - 1;
5550 tree_scroll_down(s, view->nlines - 3);
5555 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5556 struct tog_parent_tree *parent;
5557 /* user selected '..' */
5558 if (s->tree == s->root)
5560 parent = TAILQ_FIRST(&s->parents);
5561 TAILQ_REMOVE(&s->parents, parent,
5563 got_object_tree_close(s->tree);
5564 s->tree = parent->tree;
5565 s->first_displayed_entry =
5566 parent->first_displayed_entry;
5568 parent->selected_entry;
5569 s->selected = parent->selected;
5571 } else if (S_ISDIR(got_tree_entry_get_mode(
5572 s->selected_entry))) {
5573 struct got_tree_object *subtree;
5574 err = got_object_open_as_tree(&subtree, s->repo,
5575 got_tree_entry_get_id(s->selected_entry));
5578 err = tree_view_visit_subtree(s, subtree);
5580 got_object_tree_close(subtree);
5583 } else if (S_ISREG(got_tree_entry_get_mode(
5584 s->selected_entry))) {
5585 struct tog_view *blame_view;
5586 int begin_x = view_is_parent_view(view) ?
5587 view_split_begin_x(view->begin_x) : 0;
5589 err = blame_tree_entry(&blame_view, begin_x,
5590 s->selected_entry, &s->parents,
5591 s->commit_id, s->repo);
5595 blame_view->focussed = 1;
5596 if (view_is_parent_view(view)) {
5597 err = view_close_child(view);
5600 view_set_child(view, blame_view);
5601 view->focus_child = 1;
5603 *new_view = blame_view;
5607 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5608 s->selected = view->nlines - 4;
5621 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5626 static const struct got_error *
5627 cmd_tree(int argc, char *argv[])
5629 const struct got_error *error;
5630 struct got_repository *repo = NULL;
5631 struct got_worktree *worktree = NULL;
5632 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5633 struct got_object_id *commit_id = NULL;
5634 const char *commit_id_arg = NULL;
5636 struct got_reference *ref = NULL;
5637 const char *head_ref_name = NULL;
5639 struct tog_view *view;
5641 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5644 commit_id_arg = optarg;
5647 repo_path = realpath(optarg, NULL);
5648 if (repo_path == NULL)
5649 return got_error_from_errno2("realpath",
5664 if (repo_path == NULL) {
5665 cwd = getcwd(NULL, 0);
5667 return got_error_from_errno("getcwd");
5668 error = got_worktree_open(&worktree, cwd);
5669 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5673 strdup(got_worktree_get_repo_path(worktree));
5675 repo_path = strdup(cwd);
5676 if (repo_path == NULL) {
5677 error = got_error_from_errno("strdup");
5682 error = got_repo_open(&repo, repo_path, NULL);
5686 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5693 error = apply_unveil(got_repo_get_path(repo), NULL);
5697 error = tog_load_refs(repo, 0);
5701 if (commit_id_arg == NULL) {
5702 error = got_repo_match_object_id(&commit_id, &label,
5703 worktree ? got_worktree_get_head_ref_name(worktree) :
5704 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5707 head_ref_name = label;
5709 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5711 head_ref_name = got_ref_get_name(ref);
5712 else if (error->code != GOT_ERR_NOT_REF)
5714 error = got_repo_match_object_id(&commit_id, NULL,
5715 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5720 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5722 error = got_error_from_errno("view_open");
5725 error = open_tree_view(view, commit_id, head_ref_name, repo);
5728 if (!got_path_is_root_dir(in_repo_path)) {
5729 error = tree_view_walk_path(&view->state.tree, commit_id,
5736 /* Release work tree lock. */
5737 got_worktree_close(worktree);
5740 error = view_loop(view);
5749 const struct got_error *close_err = got_repo_close(repo);
5757 static const struct got_error *
5758 ref_view_load_refs(struct tog_ref_view_state *s)
5760 struct got_reflist_entry *sre;
5761 struct tog_reflist_entry *re;
5764 TAILQ_FOREACH(sre, &tog_refs, entry) {
5765 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5768 re = malloc(sizeof(*re));
5770 return got_error_from_errno("malloc");
5772 re->ref = got_ref_dup(sre->ref);
5773 if (re->ref == NULL)
5774 return got_error_from_errno("got_ref_dup");
5775 re->idx = s->nrefs++;
5776 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5779 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5784 ref_view_free_refs(struct tog_ref_view_state *s)
5786 struct tog_reflist_entry *re;
5788 while (!TAILQ_EMPTY(&s->refs)) {
5789 re = TAILQ_FIRST(&s->refs);
5790 TAILQ_REMOVE(&s->refs, re, entry);
5791 got_ref_close(re->ref);
5796 static const struct got_error *
5797 open_ref_view(struct tog_view *view, struct got_repository *repo)
5799 const struct got_error *err = NULL;
5800 struct tog_ref_view_state *s = &view->state.ref;
5802 s->selected_entry = 0;
5805 TAILQ_INIT(&s->refs);
5806 STAILQ_INIT(&s->colors);
5808 err = ref_view_load_refs(s);
5812 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5813 err = add_color(&s->colors, "^refs/heads/",
5814 TOG_COLOR_REFS_HEADS,
5815 get_color_value("TOG_COLOR_REFS_HEADS"));
5819 err = add_color(&s->colors, "^refs/tags/",
5820 TOG_COLOR_REFS_TAGS,
5821 get_color_value("TOG_COLOR_REFS_TAGS"));
5825 err = add_color(&s->colors, "^refs/remotes/",
5826 TOG_COLOR_REFS_REMOTES,
5827 get_color_value("TOG_COLOR_REFS_REMOTES"));
5832 view->show = show_ref_view;
5833 view->input = input_ref_view;
5834 view->close = close_ref_view;
5835 view->search_start = search_start_ref_view;
5836 view->search_next = search_next_ref_view;
5839 free_colors(&s->colors);
5843 static const struct got_error *
5844 close_ref_view(struct tog_view *view)
5846 struct tog_ref_view_state *s = &view->state.ref;
5848 ref_view_free_refs(s);
5849 free_colors(&s->colors);
5854 static const struct got_error *
5855 resolve_reflist_entry(struct got_object_id **commit_id,
5856 struct tog_reflist_entry *re, struct got_repository *repo)
5858 const struct got_error *err = NULL;
5859 struct got_object_id *obj_id;
5860 struct got_tag_object *tag = NULL;
5865 err = got_ref_resolve(&obj_id, repo, re->ref);
5869 err = got_object_get_type(&obj_type, repo, obj_id);
5874 case GOT_OBJ_TYPE_COMMIT:
5875 *commit_id = obj_id;
5877 case GOT_OBJ_TYPE_TAG:
5878 err = got_object_open_as_tag(&tag, repo, obj_id);
5882 err = got_object_get_type(&obj_type, repo,
5883 got_object_tag_get_object_id(tag));
5886 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5887 err = got_error(GOT_ERR_OBJ_TYPE);
5890 *commit_id = got_object_id_dup(
5891 got_object_tag_get_object_id(tag));
5892 if (*commit_id == NULL) {
5893 err = got_error_from_errno("got_object_id_dup");
5898 err = got_error(GOT_ERR_OBJ_TYPE);
5904 got_object_tag_close(tag);
5912 static const struct got_error *
5913 log_ref_entry(struct tog_view **new_view, int begin_x,
5914 struct tog_reflist_entry *re, struct got_repository *repo)
5916 struct tog_view *log_view;
5917 const struct got_error *err = NULL;
5918 struct got_object_id *commit_id = NULL;
5922 err = resolve_reflist_entry(&commit_id, re, repo);
5924 if (err->code != GOT_ERR_OBJ_TYPE)
5930 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5931 if (log_view == NULL) {
5932 err = got_error_from_errno("view_open");
5936 err = open_log_view(log_view, commit_id, repo,
5937 got_ref_get_name(re->ref), "", 0);
5940 view_close(log_view);
5942 *new_view = log_view;
5948 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5950 struct tog_reflist_entry *re;
5953 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5956 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5957 while (i++ < maxscroll) {
5960 s->first_displayed_entry = re;
5961 re = TAILQ_PREV(re, tog_reflist_head, entry);
5966 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5968 struct tog_reflist_entry *next, *last;
5971 if (s->first_displayed_entry)
5972 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5974 next = TAILQ_FIRST(&s->refs);
5976 last = s->last_displayed_entry;
5977 while (next && last && n++ < maxscroll) {
5978 last = TAILQ_NEXT(last, entry);
5980 s->first_displayed_entry = next;
5981 next = TAILQ_NEXT(next, entry);
5986 static const struct got_error *
5987 search_start_ref_view(struct tog_view *view)
5989 struct tog_ref_view_state *s = &view->state.ref;
5991 s->matched_entry = NULL;
5996 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5998 regmatch_t regmatch;
6000 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6004 static const struct got_error *
6005 search_next_ref_view(struct tog_view *view)
6007 struct tog_ref_view_state *s = &view->state.ref;
6008 struct tog_reflist_entry *re = NULL;
6010 if (!view->searching) {
6011 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6015 if (s->matched_entry) {
6016 if (view->searching == TOG_SEARCH_FORWARD) {
6017 if (s->selected_entry)
6018 re = TAILQ_NEXT(s->selected_entry, entry);
6020 re = TAILQ_PREV(s->selected_entry,
6021 tog_reflist_head, entry);
6023 if (s->selected_entry == NULL)
6024 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6026 re = TAILQ_PREV(s->selected_entry,
6027 tog_reflist_head, entry);
6030 if (view->searching == TOG_SEARCH_FORWARD)
6031 re = TAILQ_FIRST(&s->refs);
6033 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6038 if (s->matched_entry == NULL) {
6039 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6042 if (view->searching == TOG_SEARCH_FORWARD)
6043 re = TAILQ_FIRST(&s->refs);
6045 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6048 if (match_reflist_entry(re, &view->regex)) {
6049 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6050 s->matched_entry = re;
6054 if (view->searching == TOG_SEARCH_FORWARD)
6055 re = TAILQ_NEXT(re, entry);
6057 re = TAILQ_PREV(re, tog_reflist_head, entry);
6060 if (s->matched_entry) {
6061 s->first_displayed_entry = s->matched_entry;
6068 static const struct got_error *
6069 show_ref_view(struct tog_view *view)
6071 const struct got_error *err = NULL;
6072 struct tog_ref_view_state *s = &view->state.ref;
6073 struct tog_reflist_entry *re;
6076 struct tog_color *tc;
6078 int limit = view->nlines;
6080 werase(view->window);
6087 re = s->first_displayed_entry;
6089 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6091 return got_error_from_errno("asprintf");
6093 err = format_line(&wline, &width, line, view->ncols, 0);
6098 if (view_needs_focus_indication(view))
6099 wstandout(view->window);
6100 waddwstr(view->window, wline);
6101 if (view_needs_focus_indication(view))
6102 wstandend(view->window);
6107 if (width < view->ncols - 1)
6108 waddch(view->window, '\n');
6113 while (re && limit > 0) {
6116 if (got_ref_is_symbolic(re->ref)) {
6117 if (asprintf(&line, "%s -> %s",
6118 got_ref_get_name(re->ref),
6119 got_ref_get_symref_target(re->ref)) == -1)
6120 return got_error_from_errno("asprintf");
6121 } else if (s->show_ids) {
6122 struct got_object_id *id;
6124 err = got_ref_resolve(&id, s->repo, re->ref);
6127 err = got_object_id_str(&id_str, id);
6132 if (asprintf(&line, "%s: %s",
6133 got_ref_get_name(re->ref), id_str) == -1) {
6134 err = got_error_from_errno("asprintf");
6142 line = strdup(got_ref_get_name(re->ref));
6144 return got_error_from_errno("strdup");
6147 err = format_line(&wline, &width, line, view->ncols, 0);
6152 if (n == s->selected) {
6154 wstandout(view->window);
6155 s->selected_entry = re;
6157 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6159 wattr_on(view->window,
6160 COLOR_PAIR(tc->colorpair), NULL);
6161 waddwstr(view->window, wline);
6163 wattr_off(view->window,
6164 COLOR_PAIR(tc->colorpair), NULL);
6165 if (width < view->ncols - 1)
6166 waddch(view->window, '\n');
6167 if (n == s->selected && view->focussed)
6168 wstandend(view->window);
6174 s->last_displayed_entry = re;
6177 re = TAILQ_NEXT(re, entry);
6184 static const struct got_error *
6185 browse_ref_tree(struct tog_view **new_view, int begin_x,
6186 struct tog_reflist_entry *re, struct got_repository *repo)
6188 const struct got_error *err = NULL;
6189 struct got_object_id *commit_id = NULL;
6190 struct tog_view *tree_view;
6194 err = resolve_reflist_entry(&commit_id, re, repo);
6196 if (err->code != GOT_ERR_OBJ_TYPE)
6203 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6204 if (tree_view == NULL) {
6205 err = got_error_from_errno("view_open");
6209 err = open_tree_view(tree_view, commit_id,
6210 got_ref_get_name(re->ref), repo);
6214 *new_view = tree_view;
6219 static const struct got_error *
6220 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6222 const struct got_error *err = NULL;
6223 struct tog_ref_view_state *s = &view->state.ref;
6224 struct tog_view *log_view, *tree_view;
6225 struct tog_reflist_entry *re;
6230 s->show_ids = !s->show_ids;
6233 s->sort_by_date = !s->sort_by_date;
6234 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6235 got_ref_cmp_by_commit_timestamp_descending :
6236 got_ref_cmp_by_name, s->repo);
6239 got_reflist_object_id_map_free(tog_refs_idmap);
6240 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6241 &tog_refs, s->repo);
6244 ref_view_free_refs(s);
6245 err = ref_view_load_refs(s);
6249 if (!s->selected_entry)
6251 if (view_is_parent_view(view))
6252 begin_x = view_split_begin_x(view->begin_x);
6253 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6256 log_view->focussed = 1;
6257 if (view_is_parent_view(view)) {
6258 err = view_close_child(view);
6261 view_set_child(view, log_view);
6262 view->focus_child = 1;
6264 *new_view = log_view;
6267 if (!s->selected_entry)
6269 if (view_is_parent_view(view))
6270 begin_x = view_split_begin_x(view->begin_x);
6271 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6273 if (err || tree_view == NULL)
6276 tree_view->focussed = 1;
6277 if (view_is_parent_view(view)) {
6278 err = view_close_child(view);
6281 view_set_child(view, tree_view);
6282 view->focus_child = 1;
6284 *new_view = tree_view;
6289 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6294 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6295 for (n = 0; n < view->nlines - 1; n++) {
6298 s->first_displayed_entry = re;
6299 re = TAILQ_PREV(re, tog_reflist_head, entry);
6302 s->selected = n - 1;
6307 if (s->selected > 0) {
6311 ref_scroll_up(s, 1);
6315 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6317 ref_scroll_up(s, MAX(0, view->nlines - 1));
6322 if (s->selected < s->ndisplayed - 1) {
6326 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6327 /* can't scroll any further */
6329 ref_scroll_down(s, 1);
6333 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6334 /* can't scroll any further; move cursor down */
6335 if (s->selected < s->ndisplayed - 1)
6336 s->selected = s->ndisplayed - 1;
6339 ref_scroll_down(s, view->nlines - 1);
6343 err = tog_load_refs(s->repo, s->sort_by_date);
6346 ref_view_free_refs(s);
6347 err = ref_view_load_refs(s);
6350 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6351 s->selected = view->nlines - 2;
6364 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6369 static const struct got_error *
6370 cmd_ref(int argc, char *argv[])
6372 const struct got_error *error;
6373 struct got_repository *repo = NULL;
6374 struct got_worktree *worktree = NULL;
6375 char *cwd = NULL, *repo_path = NULL;
6377 struct tog_view *view;
6379 while ((ch = getopt(argc, argv, "r:")) != -1) {
6382 repo_path = realpath(optarg, NULL);
6383 if (repo_path == NULL)
6384 return got_error_from_errno2("realpath",
6399 if (repo_path == NULL) {
6400 cwd = getcwd(NULL, 0);
6402 return got_error_from_errno("getcwd");
6403 error = got_worktree_open(&worktree, cwd);
6404 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6408 strdup(got_worktree_get_repo_path(worktree));
6410 repo_path = strdup(cwd);
6411 if (repo_path == NULL) {
6412 error = got_error_from_errno("strdup");
6417 error = got_repo_open(&repo, repo_path, NULL);
6423 error = apply_unveil(got_repo_get_path(repo), NULL);
6427 error = tog_load_refs(repo, 0);
6431 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6433 error = got_error_from_errno("view_open");
6437 error = open_ref_view(view, repo);
6442 /* Release work tree lock. */
6443 got_worktree_close(worktree);
6446 error = view_loop(view);
6451 const struct got_error *close_err = got_repo_close(repo);
6460 list_commands(FILE *fp)
6464 fprintf(fp, "commands:");
6465 for (i = 0; i < nitems(tog_commands); i++) {
6466 struct tog_cmd *cmd = &tog_commands[i];
6467 fprintf(fp, " %s", cmd->name);
6473 usage(int hflag, int status)
6475 FILE *fp = (status == 0) ? stdout : stderr;
6477 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6480 fprintf(fp, "lazy usage: %s path\n", getprogname());
6487 make_argv(int argc, ...)
6495 argv = calloc(argc, sizeof(char *));
6498 for (i = 0; i < argc; i++) {
6499 argv[i] = strdup(va_arg(ap, char *));
6500 if (argv[i] == NULL)
6509 * Try to convert 'tog path' into a 'tog log path' command.
6510 * The user could simply have mistyped the command rather than knowingly
6511 * provided a path. So check whether argv[0] can in fact be resolved
6512 * to a path in the HEAD commit and print a special error if not.
6513 * This hack is for mpi@ <3
6515 static const struct got_error *
6516 tog_log_with_path(int argc, char *argv[])
6518 const struct got_error *error = NULL, *close_err;
6519 struct tog_cmd *cmd = NULL;
6520 struct got_repository *repo = NULL;
6521 struct got_worktree *worktree = NULL;
6522 struct got_object_id *commit_id = NULL, *id = NULL;
6523 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6524 char *commit_id_str = NULL, **cmd_argv = NULL;
6526 cwd = getcwd(NULL, 0);
6528 return got_error_from_errno("getcwd");
6530 error = got_worktree_open(&worktree, cwd);
6531 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6535 repo_path = strdup(got_worktree_get_repo_path(worktree));
6537 repo_path = strdup(cwd);
6538 if (repo_path == NULL) {
6539 error = got_error_from_errno("strdup");
6543 error = got_repo_open(&repo, repo_path, NULL);
6547 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6552 error = tog_load_refs(repo, 0);
6555 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6556 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6557 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6562 got_worktree_close(worktree);
6566 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6568 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6570 fprintf(stderr, "%s: '%s' is no known command or path\n",
6571 getprogname(), argv[0]);
6576 close_err = got_repo_close(repo);
6581 error = got_object_id_str(&commit_id_str, commit_id);
6585 cmd = &tog_commands[0]; /* log */
6587 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6588 error = cmd->cmd_main(argc, cmd_argv);
6591 close_err = got_repo_close(repo);
6596 got_worktree_close(worktree);
6598 free(commit_id_str);
6605 for (i = 0; i < argc; i++)
6614 main(int argc, char *argv[])
6616 const struct got_error *error = NULL;
6617 struct tog_cmd *cmd = NULL;
6618 int ch, hflag = 0, Vflag = 0;
6619 char **cmd_argv = NULL;
6620 static struct option longopts[] = {
6621 { "version", no_argument, NULL, 'V' },
6625 setlocale(LC_CTYPE, "");
6627 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6647 got_version_print_str();
6652 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6660 /* Build an argument vector which runs a default command. */
6661 cmd = &tog_commands[0];
6663 cmd_argv = make_argv(argc, cmd->name);
6667 /* Did the user specify a command? */
6668 for (i = 0; i < nitems(tog_commands); i++) {
6669 if (strncmp(tog_commands[i].name, argv[0],
6670 strlen(argv[0])) == 0) {
6671 cmd = &tog_commands[i];
6680 /* No command specified; try log with a path */
6681 error = tog_log_with_path(argc, argv);
6686 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6693 for (i = 0; i < argc; i++)
6698 if (error && error->code != GOT_ERR_CANCELLED)
6699 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);