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 lineno = s->first_displayed_line;
3437 if (lineno <= 0 || lineno > s->nlines) {
3438 if (s->matched_line == 0) {
3439 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3443 if (view->searching == TOG_SEARCH_FORWARD)
3449 offset = s->line_offsets[lineno - 1];
3450 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3452 return got_error_from_errno("fseeko");
3454 linelen = getline(&line, &linesize, s->f);
3455 if (linelen != -1 &&
3456 match_line(line, &view->regex, 1, &view->regmatch)) {
3457 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3458 s->matched_line = lineno;
3461 if (view->searching == TOG_SEARCH_FORWARD)
3468 if (s->matched_line) {
3469 s->first_displayed_line = s->matched_line;
3470 s->selected_line = 1;
3476 static const struct got_error *
3477 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3478 struct got_object_id *id2, const char *label1, const char *label2,
3479 int diff_context, int ignore_whitespace, int force_text_diff,
3480 struct tog_view *log_view, struct got_repository *repo)
3482 const struct got_error *err;
3483 struct tog_diff_view_state *s = &view->state.diff;
3485 if (id1 != NULL && id2 != NULL) {
3487 err = got_object_get_type(&type1, repo, id1);
3490 err = got_object_get_type(&type2, repo, id2);
3495 return got_error(GOT_ERR_OBJ_TYPE);
3497 s->first_displayed_line = 1;
3498 s->last_displayed_line = view->nlines;
3499 s->selected_line = 1;
3507 s->id1 = got_object_id_dup(id1);
3509 return got_error_from_errno("got_object_id_dup");
3513 s->id2 = got_object_id_dup(id2);
3514 if (s->id2 == NULL) {
3517 return got_error_from_errno("got_object_id_dup");
3520 s->first_displayed_line = 1;
3521 s->last_displayed_line = view->nlines;
3522 s->diff_context = diff_context;
3523 s->ignore_whitespace = ignore_whitespace;
3524 s->force_text_diff = force_text_diff;
3525 s->log_view = log_view;
3528 STAILQ_INIT(&s->colors);
3529 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3530 err = add_color(&s->colors,
3531 "^-", TOG_COLOR_DIFF_MINUS,
3532 get_color_value("TOG_COLOR_DIFF_MINUS"));
3535 err = add_color(&s->colors, "^\\+",
3536 TOG_COLOR_DIFF_PLUS,
3537 get_color_value("TOG_COLOR_DIFF_PLUS"));
3539 free_colors(&s->colors);
3542 err = add_color(&s->colors,
3543 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3544 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3546 free_colors(&s->colors);
3550 err = add_color(&s->colors,
3551 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3552 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3553 get_color_value("TOG_COLOR_DIFF_META"));
3555 free_colors(&s->colors);
3559 err = add_color(&s->colors,
3560 "^(from|via): ", TOG_COLOR_AUTHOR,
3561 get_color_value("TOG_COLOR_AUTHOR"));
3563 free_colors(&s->colors);
3567 err = add_color(&s->colors,
3568 "^date: ", TOG_COLOR_DATE,
3569 get_color_value("TOG_COLOR_DATE"));
3571 free_colors(&s->colors);
3576 if (log_view && view_is_splitscreen(view))
3577 show_log_view(log_view); /* draw vborder */
3578 diff_view_indicate_progress(view);
3580 s->line_offsets = NULL;
3582 err = create_diff(s);
3588 free_colors(&s->colors);
3592 view->show = show_diff_view;
3593 view->input = input_diff_view;
3594 view->close = close_diff_view;
3595 view->search_start = search_start_diff_view;
3596 view->search_next = search_next_diff_view;
3601 static const struct got_error *
3602 close_diff_view(struct tog_view *view)
3604 const struct got_error *err = NULL;
3605 struct tog_diff_view_state *s = &view->state.diff;
3611 if (s->f && fclose(s->f) == EOF)
3612 err = got_error_from_errno("fclose");
3613 free_colors(&s->colors);
3614 free(s->line_offsets);
3615 s->line_offsets = NULL;
3620 static const struct got_error *
3621 show_diff_view(struct tog_view *view)
3623 const struct got_error *err;
3624 struct tog_diff_view_state *s = &view->state.diff;
3625 char *id_str1 = NULL, *id_str2, *header;
3626 const char *label1, *label2;
3629 err = got_object_id_str(&id_str1, s->id1);
3632 label1 = s->label1 ? : id_str1;
3634 label1 = "/dev/null";
3636 err = got_object_id_str(&id_str2, s->id2);
3639 label2 = s->label2 ? : id_str2;
3641 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3642 err = got_error_from_errno("asprintf");
3650 err = draw_file(view, header);
3655 static const struct got_error *
3656 set_selected_commit(struct tog_diff_view_state *s,
3657 struct commit_queue_entry *entry)
3659 const struct got_error *err;
3660 const struct got_object_id_queue *parent_ids;
3661 struct got_commit_object *selected_commit;
3662 struct got_object_qid *pid;
3665 s->id2 = got_object_id_dup(entry->id);
3667 return got_error_from_errno("got_object_id_dup");
3669 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3672 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3674 pid = STAILQ_FIRST(parent_ids);
3675 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3676 got_object_commit_close(selected_commit);
3680 static const struct got_error *
3681 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3683 const struct got_error *err = NULL;
3684 struct tog_diff_view_state *s = &view->state.diff;
3685 struct tog_log_view_state *ls;
3686 struct commit_queue_entry *old_selected_entry;
3688 size_t linesize = 0;
3696 s->force_text_diff = !s->force_text_diff;
3698 s->ignore_whitespace = !s->ignore_whitespace;
3699 wclear(view->window);
3700 s->first_displayed_line = 1;
3701 s->last_displayed_line = view->nlines;
3702 s->matched_line = 0;
3703 diff_view_indicate_progress(view);
3704 err = create_diff(s);
3708 s->first_displayed_line = 1;
3715 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3721 if (s->first_displayed_line > 1)
3722 s->first_displayed_line--;
3726 if (s->first_displayed_line == 1)
3729 while (i++ < view->nlines - 1 &&
3730 s->first_displayed_line > 1)
3731 s->first_displayed_line--;
3737 s->first_displayed_line++;
3745 while (!s->eof && i++ < view->nlines - 1) {
3746 linelen = getline(&line, &linesize, s->f);
3747 s->first_displayed_line++;
3748 if (linelen == -1) {
3752 err = got_ferror(s->f, GOT_ERR_IO);
3759 if (s->diff_context > 0) {
3761 s->matched_line = 0;
3762 diff_view_indicate_progress(view);
3763 err = create_diff(s);
3764 if (s->first_displayed_line + view->nlines - 1 >
3766 s->first_displayed_line = 1;
3767 s->last_displayed_line = view->nlines;
3772 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3774 s->matched_line = 0;
3775 diff_view_indicate_progress(view);
3776 err = create_diff(s);
3781 if (s->log_view == NULL)
3783 ls = &s->log_view->state.log;
3784 old_selected_entry = ls->selected_entry;
3786 err = input_log_view(NULL, s->log_view, KEY_UP);
3790 if (old_selected_entry == ls->selected_entry)
3793 err = set_selected_commit(s, ls->selected_entry);
3797 s->first_displayed_line = 1;
3798 s->last_displayed_line = view->nlines;
3799 s->matched_line = 0;
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;
3824 s->matched_line = 0;
3826 diff_view_indicate_progress(view);
3827 err = create_diff(s);
3836 static const struct got_error *
3837 cmd_diff(int argc, char *argv[])
3839 const struct got_error *error = NULL;
3840 struct got_repository *repo = NULL;
3841 struct got_worktree *worktree = NULL;
3842 struct got_object_id *id1 = NULL, *id2 = NULL;
3843 char *repo_path = NULL, *cwd = NULL;
3844 char *id_str1 = NULL, *id_str2 = NULL;
3845 char *label1 = NULL, *label2 = NULL;
3846 int diff_context = 3, ignore_whitespace = 0;
3847 int ch, force_text_diff = 0;
3849 struct tog_view *view;
3851 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3854 force_text_diff = 1;
3857 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3860 err(1, "-C option %s", errstr);
3863 repo_path = realpath(optarg, NULL);
3864 if (repo_path == NULL)
3865 return got_error_from_errno2("realpath",
3867 got_path_strip_trailing_slashes(repo_path);
3870 ignore_whitespace = 1;
3882 usage_diff(); /* TODO show local worktree changes */
3883 } else if (argc == 2) {
3889 if (repo_path == NULL) {
3890 cwd = getcwd(NULL, 0);
3892 return got_error_from_errno("getcwd");
3893 error = got_worktree_open(&worktree, cwd);
3894 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3898 strdup(got_worktree_get_repo_path(worktree));
3900 repo_path = strdup(cwd);
3901 if (repo_path == NULL) {
3902 error = got_error_from_errno("strdup");
3907 error = got_repo_open(&repo, repo_path, NULL);
3913 error = apply_unveil(got_repo_get_path(repo), NULL);
3917 error = tog_load_refs(repo, 0);
3921 error = got_repo_match_object_id(&id1, &label1, id_str1,
3922 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3926 error = got_repo_match_object_id(&id2, &label2, id_str2,
3927 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3931 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3933 error = got_error_from_errno("view_open");
3936 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3937 ignore_whitespace, force_text_diff, NULL, repo);
3940 error = view_loop(view);
3947 const struct got_error *close_err = got_repo_close(repo);
3952 got_worktree_close(worktree);
3961 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3966 struct tog_blame_line {
3968 struct got_object_id *id;
3971 static const struct got_error *
3972 draw_blame(struct tog_view *view)
3974 struct tog_blame_view_state *s = &view->state.blame;
3975 struct tog_blame *blame = &s->blame;
3976 regmatch_t *regmatch = &view->regmatch;
3977 const struct got_error *err;
3978 int lineno = 0, nprinted = 0;
3980 size_t linesize = 0;
3984 struct tog_blame_line *blame_line;
3985 struct got_object_id *prev_id = NULL;
3987 struct tog_color *tc;
3989 err = got_object_id_str(&id_str, s->blamed_commit->id);
3994 werase(view->window);
3996 if (asprintf(&line, "commit %s", id_str) == -1) {
3997 err = got_error_from_errno("asprintf");
4002 err = format_line(&wline, &width, line, view->ncols, 0);
4007 if (view_needs_focus_indication(view))
4008 wstandout(view->window);
4009 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4011 wattr_on(view->window,
4012 COLOR_PAIR(tc->colorpair), NULL);
4013 waddwstr(view->window, wline);
4015 wattr_off(view->window,
4016 COLOR_PAIR(tc->colorpair), NULL);
4017 if (view_needs_focus_indication(view))
4018 wstandend(view->window);
4021 if (width < view->ncols - 1)
4022 waddch(view->window, '\n');
4024 if (asprintf(&line, "[%d/%d] %s%s",
4025 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4026 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4028 return got_error_from_errno("asprintf");
4031 err = format_line(&wline, &width, line, view->ncols, 0);
4036 waddwstr(view->window, wline);
4039 if (width < view->ncols - 1)
4040 waddch(view->window, '\n');
4043 while (nprinted < view->nlines - 2) {
4044 linelen = getline(&line, &linesize, blame->f);
4045 if (linelen == -1) {
4046 if (feof(blame->f)) {
4051 return got_ferror(blame->f, GOT_ERR_IO);
4053 if (++lineno < s->first_displayed_line)
4056 if (view->focussed && nprinted == s->selected_line - 1)
4057 wstandout(view->window);
4059 if (blame->nlines > 0) {
4060 blame_line = &blame->lines[lineno - 1];
4061 if (blame_line->annotated && prev_id &&
4062 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4064 nprinted == s->selected_line - 1)) {
4065 waddstr(view->window, " ");
4066 } else if (blame_line->annotated) {
4068 err = got_object_id_str(&id_str, blame_line->id);
4073 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4075 wattr_on(view->window,
4076 COLOR_PAIR(tc->colorpair), NULL);
4077 wprintw(view->window, "%.8s", id_str);
4079 wattr_off(view->window,
4080 COLOR_PAIR(tc->colorpair), NULL);
4082 prev_id = blame_line->id;
4084 waddstr(view->window, "........");
4088 waddstr(view->window, "........");
4092 if (view->focussed && nprinted == s->selected_line - 1)
4093 wstandend(view->window);
4094 waddstr(view->window, " ");
4096 if (view->ncols <= 9) {
4098 wline = wcsdup(L"");
4099 if (wline == NULL) {
4100 err = got_error_from_errno("wcsdup");
4104 } else if (s->first_displayed_line + nprinted ==
4106 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4107 err = add_matched_line(&width, line, view->ncols - 9, 9,
4108 view->window, regmatch);
4115 err = format_line(&wline, &width, line,
4116 view->ncols - 9, 9);
4117 waddwstr(view->window, wline);
4123 if (width <= view->ncols - 1)
4124 waddch(view->window, '\n');
4125 if (++nprinted == 1)
4126 s->first_displayed_line = lineno;
4129 s->last_displayed_line = lineno;
4136 static const struct got_error *
4137 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4139 const struct got_error *err = NULL;
4140 struct tog_blame_cb_args *a = arg;
4141 struct tog_blame_line *line;
4144 if (nlines != a->nlines ||
4145 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4146 return got_error(GOT_ERR_RANGE);
4148 errcode = pthread_mutex_lock(&tog_mutex);
4150 return got_error_set_errno(errcode, "pthread_mutex_lock");
4152 if (*a->quit) { /* user has quit the blame view */
4153 err = got_error(GOT_ERR_ITER_COMPLETED);
4158 goto done; /* no change in this commit */
4160 line = &a->lines[lineno - 1];
4161 if (line->annotated)
4164 line->id = got_object_id_dup(id);
4165 if (line->id == NULL) {
4166 err = got_error_from_errno("got_object_id_dup");
4169 line->annotated = 1;
4171 errcode = pthread_mutex_unlock(&tog_mutex);
4173 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4178 blame_thread(void *arg)
4180 const struct got_error *err, *close_err;
4181 struct tog_blame_thread_args *ta = arg;
4182 struct tog_blame_cb_args *a = ta->cb_args;
4185 err = block_signals_used_by_main_thread();
4189 err = got_blame(ta->path, a->commit_id, ta->repo,
4190 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4191 if (err && err->code == GOT_ERR_CANCELLED)
4194 errcode = pthread_mutex_lock(&tog_mutex);
4196 return (void *)got_error_set_errno(errcode,
4197 "pthread_mutex_lock");
4199 close_err = got_repo_close(ta->repo);
4205 errcode = pthread_mutex_unlock(&tog_mutex);
4206 if (errcode && err == NULL)
4207 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4212 static struct got_object_id *
4213 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4214 int first_displayed_line, int selected_line)
4216 struct tog_blame_line *line;
4221 line = &lines[first_displayed_line - 1 + selected_line - 1];
4222 if (!line->annotated)
4228 static const struct got_error *
4229 stop_blame(struct tog_blame *blame)
4231 const struct got_error *err = NULL;
4234 if (blame->thread) {
4236 errcode = pthread_mutex_unlock(&tog_mutex);
4238 return got_error_set_errno(errcode,
4239 "pthread_mutex_unlock");
4240 errcode = pthread_join(blame->thread, (void **)&err);
4242 return got_error_set_errno(errcode, "pthread_join");
4243 errcode = pthread_mutex_lock(&tog_mutex);
4245 return got_error_set_errno(errcode,
4246 "pthread_mutex_lock");
4247 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4249 blame->thread = NULL;
4251 if (blame->thread_args.repo) {
4252 const struct got_error *close_err;
4253 close_err = got_repo_close(blame->thread_args.repo);
4256 blame->thread_args.repo = NULL;
4259 if (fclose(blame->f) == EOF && err == NULL)
4260 err = got_error_from_errno("fclose");
4264 for (i = 0; i < blame->nlines; i++)
4265 free(blame->lines[i].id);
4267 blame->lines = NULL;
4269 free(blame->cb_args.commit_id);
4270 blame->cb_args.commit_id = NULL;
4275 static const struct got_error *
4276 cancel_blame_view(void *arg)
4278 const struct got_error *err = NULL;
4282 errcode = pthread_mutex_lock(&tog_mutex);
4284 return got_error_set_errno(errcode,
4285 "pthread_mutex_unlock");
4288 err = got_error(GOT_ERR_CANCELLED);
4290 errcode = pthread_mutex_unlock(&tog_mutex);
4292 return got_error_set_errno(errcode,
4293 "pthread_mutex_lock");
4298 static const struct got_error *
4299 run_blame(struct tog_view *view)
4301 struct tog_blame_view_state *s = &view->state.blame;
4302 struct tog_blame *blame = &s->blame;
4303 const struct got_error *err = NULL;
4304 struct got_blob_object *blob = NULL;
4305 struct got_repository *thread_repo = NULL;
4306 struct got_object_id *obj_id = NULL;
4309 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4314 err = got_object_get_type(&obj_type, s->repo, obj_id);
4318 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4319 err = got_error(GOT_ERR_OBJ_TYPE);
4323 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4326 blame->f = got_opentemp();
4327 if (blame->f == NULL) {
4328 err = got_error_from_errno("got_opentemp");
4331 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4332 &blame->line_offsets, blame->f, blob);
4335 if (blame->nlines == 0) {
4336 s->blame_complete = 1;
4340 /* Don't include \n at EOF in the blame line count. */
4341 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4344 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4345 if (blame->lines == NULL) {
4346 err = got_error_from_errno("calloc");
4350 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4354 blame->cb_args.view = view;
4355 blame->cb_args.lines = blame->lines;
4356 blame->cb_args.nlines = blame->nlines;
4357 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4358 if (blame->cb_args.commit_id == NULL) {
4359 err = got_error_from_errno("got_object_id_dup");
4362 blame->cb_args.quit = &s->done;
4364 blame->thread_args.path = s->path;
4365 blame->thread_args.repo = thread_repo;
4366 blame->thread_args.cb_args = &blame->cb_args;
4367 blame->thread_args.complete = &s->blame_complete;
4368 blame->thread_args.cancel_cb = cancel_blame_view;
4369 blame->thread_args.cancel_arg = &s->done;
4370 s->blame_complete = 0;
4372 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4373 s->first_displayed_line = 1;
4374 s->last_displayed_line = view->nlines;
4375 s->selected_line = 1;
4377 s->matched_line = 0;
4381 got_object_blob_close(blob);
4388 static const struct got_error *
4389 open_blame_view(struct tog_view *view, char *path,
4390 struct got_object_id *commit_id, struct got_repository *repo)
4392 const struct got_error *err = NULL;
4393 struct tog_blame_view_state *s = &view->state.blame;
4395 STAILQ_INIT(&s->blamed_commits);
4397 s->path = strdup(path);
4398 if (s->path == NULL)
4399 return got_error_from_errno("strdup");
4401 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4407 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4408 s->first_displayed_line = 1;
4409 s->last_displayed_line = view->nlines;
4410 s->selected_line = 1;
4411 s->blame_complete = 0;
4413 s->commit_id = commit_id;
4414 memset(&s->blame, 0, sizeof(s->blame));
4416 STAILQ_INIT(&s->colors);
4417 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4418 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4419 get_color_value("TOG_COLOR_COMMIT"));
4424 view->show = show_blame_view;
4425 view->input = input_blame_view;
4426 view->close = close_blame_view;
4427 view->search_start = search_start_blame_view;
4428 view->search_next = search_next_blame_view;
4430 return run_blame(view);
4433 static const struct got_error *
4434 close_blame_view(struct tog_view *view)
4436 const struct got_error *err = NULL;
4437 struct tog_blame_view_state *s = &view->state.blame;
4439 if (s->blame.thread)
4440 err = stop_blame(&s->blame);
4442 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4443 struct got_object_qid *blamed_commit;
4444 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4445 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4446 got_object_qid_free(blamed_commit);
4450 free_colors(&s->colors);
4455 static const struct got_error *
4456 search_start_blame_view(struct tog_view *view)
4458 struct tog_blame_view_state *s = &view->state.blame;
4460 s->matched_line = 0;
4464 static const struct got_error *
4465 search_next_blame_view(struct tog_view *view)
4467 struct tog_blame_view_state *s = &view->state.blame;
4470 size_t linesize = 0;
4473 if (!view->searching) {
4474 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4478 if (s->matched_line) {
4479 if (view->searching == TOG_SEARCH_FORWARD)
4480 lineno = s->matched_line + 1;
4482 lineno = s->matched_line - 1;
4484 lineno = s->first_displayed_line - 1 + s->selected_line;
4489 if (lineno <= 0 || lineno > s->blame.nlines) {
4490 if (s->matched_line == 0) {
4491 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4495 if (view->searching == TOG_SEARCH_FORWARD)
4498 lineno = s->blame.nlines;
4501 offset = s->blame.line_offsets[lineno - 1];
4502 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4504 return got_error_from_errno("fseeko");
4506 linelen = getline(&line, &linesize, s->blame.f);
4507 if (linelen != -1 &&
4508 match_line(line, &view->regex, 1, &view->regmatch)) {
4509 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4510 s->matched_line = lineno;
4513 if (view->searching == TOG_SEARCH_FORWARD)
4520 if (s->matched_line) {
4521 s->first_displayed_line = s->matched_line;
4522 s->selected_line = 1;
4528 static const struct got_error *
4529 show_blame_view(struct tog_view *view)
4531 const struct got_error *err = NULL;
4532 struct tog_blame_view_state *s = &view->state.blame;
4535 if (s->blame.thread == NULL && !s->blame_complete) {
4536 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4537 &s->blame.thread_args);
4539 return got_error_set_errno(errcode, "pthread_create");
4541 halfdelay(1); /* fast refresh while annotating */
4544 if (s->blame_complete)
4545 halfdelay(10); /* disable fast refresh */
4547 err = draw_blame(view);
4553 static const struct got_error *
4554 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4556 const struct got_error *err = NULL, *thread_err = NULL;
4557 struct tog_view *diff_view;
4558 struct tog_blame_view_state *s = &view->state.blame;
4567 s->selected_line = 1;
4568 s->first_displayed_line = 1;
4572 if (s->blame.nlines < view->nlines - 2) {
4573 s->selected_line = s->blame.nlines;
4574 s->first_displayed_line = 1;
4576 s->selected_line = view->nlines - 2;
4577 s->first_displayed_line = s->blame.nlines -
4584 if (s->selected_line > 1)
4586 else if (s->selected_line == 1 &&
4587 s->first_displayed_line > 1)
4588 s->first_displayed_line--;
4592 if (s->first_displayed_line == 1) {
4593 s->selected_line = 1;
4596 if (s->first_displayed_line > view->nlines - 2)
4597 s->first_displayed_line -=
4600 s->first_displayed_line = 1;
4605 if (s->selected_line < view->nlines - 2 &&
4606 s->first_displayed_line +
4607 s->selected_line <= s->blame.nlines)
4609 else if (s->last_displayed_line <
4611 s->first_displayed_line++;
4615 struct got_object_id *id = NULL;
4616 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4617 s->first_displayed_line, s->selected_line);
4621 struct got_commit_object *commit;
4622 struct got_object_qid *pid;
4623 struct got_object_id *blob_id = NULL;
4625 err = got_object_open_as_commit(&commit,
4630 got_object_commit_get_parent_ids(commit));
4632 got_object_commit_close(commit);
4635 /* Check if path history ends here. */
4636 err = got_object_id_by_path(&blob_id, s->repo,
4639 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4641 got_object_commit_close(commit);
4644 err = got_object_get_type(&obj_type, s->repo,
4647 /* Can't blame non-blob type objects. */
4648 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4649 got_object_commit_close(commit);
4652 err = got_object_qid_alloc(&s->blamed_commit,
4654 got_object_commit_close(commit);
4656 if (got_object_id_cmp(id,
4657 s->blamed_commit->id) == 0)
4659 err = got_object_qid_alloc(&s->blamed_commit,
4665 thread_err = stop_blame(&s->blame);
4669 STAILQ_INSERT_HEAD(&s->blamed_commits,
4670 s->blamed_commit, entry);
4671 err = run_blame(view);
4677 struct got_object_qid *first;
4678 first = STAILQ_FIRST(&s->blamed_commits);
4679 if (!got_object_id_cmp(first->id, s->commit_id))
4682 thread_err = stop_blame(&s->blame);
4686 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4687 got_object_qid_free(s->blamed_commit);
4689 STAILQ_FIRST(&s->blamed_commits);
4690 err = run_blame(view);
4697 struct got_object_id *id = NULL;
4698 struct got_object_qid *pid;
4699 struct got_commit_object *commit = NULL;
4700 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4701 s->first_displayed_line, s->selected_line);
4704 err = got_object_open_as_commit(&commit, s->repo, id);
4708 got_object_commit_get_parent_ids(commit));
4709 if (view_is_parent_view(view))
4710 begin_x = view_split_begin_x(view->begin_x);
4711 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4712 if (diff_view == NULL) {
4713 got_object_commit_close(commit);
4714 err = got_error_from_errno("view_open");
4717 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4718 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4719 got_object_commit_close(commit);
4721 view_close(diff_view);
4725 diff_view->focussed = 1;
4726 if (view_is_parent_view(view)) {
4727 err = view_close_child(view);
4730 view_set_child(view, diff_view);
4731 view->focus_child = 1;
4733 *new_view = diff_view;
4741 if (s->last_displayed_line >= s->blame.nlines &&
4742 s->selected_line >= MIN(s->blame.nlines,
4743 view->nlines - 2)) {
4746 if (s->last_displayed_line >= s->blame.nlines &&
4747 s->selected_line < view->nlines - 2) {
4748 s->selected_line = MIN(s->blame.nlines,
4752 if (s->last_displayed_line + view->nlines - 2
4754 s->first_displayed_line +=
4757 s->first_displayed_line =
4762 if (s->selected_line > view->nlines - 2) {
4763 s->selected_line = MIN(s->blame.nlines,
4770 return thread_err ? thread_err : err;
4773 static const struct got_error *
4774 cmd_blame(int argc, char *argv[])
4776 const struct got_error *error;
4777 struct got_repository *repo = NULL;
4778 struct got_worktree *worktree = NULL;
4779 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4780 char *link_target = NULL;
4781 struct got_object_id *commit_id = NULL;
4782 char *commit_id_str = NULL;
4784 struct tog_view *view;
4786 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4789 commit_id_str = optarg;
4792 repo_path = realpath(optarg, NULL);
4793 if (repo_path == NULL)
4794 return got_error_from_errno2("realpath",
4809 if (repo_path == NULL) {
4810 cwd = getcwd(NULL, 0);
4812 return got_error_from_errno("getcwd");
4813 error = got_worktree_open(&worktree, cwd);
4814 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4818 strdup(got_worktree_get_repo_path(worktree));
4820 repo_path = strdup(cwd);
4821 if (repo_path == NULL) {
4822 error = got_error_from_errno("strdup");
4827 error = got_repo_open(&repo, repo_path, NULL);
4831 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4838 error = apply_unveil(got_repo_get_path(repo), NULL);
4842 error = tog_load_refs(repo, 0);
4846 if (commit_id_str == NULL) {
4847 struct got_reference *head_ref;
4848 error = got_ref_open(&head_ref, repo, worktree ?
4849 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4852 error = got_ref_resolve(&commit_id, repo, head_ref);
4853 got_ref_close(head_ref);
4855 error = got_repo_match_object_id(&commit_id, NULL,
4856 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4861 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4863 error = got_error_from_errno("view_open");
4867 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4872 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4877 /* Release work tree lock. */
4878 got_worktree_close(worktree);
4881 error = view_loop(view);
4889 got_worktree_close(worktree);
4891 const struct got_error *close_err = got_repo_close(repo);
4899 static const struct got_error *
4900 draw_tree_entries(struct tog_view *view, const char *parent_path)
4902 struct tog_tree_view_state *s = &view->state.tree;
4903 const struct got_error *err = NULL;
4904 struct got_tree_entry *te;
4906 struct tog_color *tc;
4907 int width, n, i, nentries;
4908 int limit = view->nlines;
4912 werase(view->window);
4917 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4920 if (view_needs_focus_indication(view))
4921 wstandout(view->window);
4922 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4924 wattr_on(view->window,
4925 COLOR_PAIR(tc->colorpair), NULL);
4926 waddwstr(view->window, wline);
4928 wattr_off(view->window,
4929 COLOR_PAIR(tc->colorpair), NULL);
4930 if (view_needs_focus_indication(view))
4931 wstandend(view->window);
4934 if (width < view->ncols - 1)
4935 waddch(view->window, '\n');
4938 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4941 waddwstr(view->window, wline);
4944 if (width < view->ncols - 1)
4945 waddch(view->window, '\n');
4948 waddch(view->window, '\n');
4952 if (s->first_displayed_entry == NULL) {
4953 te = got_object_tree_get_first_entry(s->tree);
4954 if (s->selected == 0) {
4956 wstandout(view->window);
4957 s->selected_entry = NULL;
4959 waddstr(view->window, " ..\n"); /* parent directory */
4960 if (s->selected == 0 && view->focussed)
4961 wstandend(view->window);
4968 te = s->first_displayed_entry;
4971 nentries = got_object_tree_get_nentries(s->tree);
4972 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4973 char *line = NULL, *id_str = NULL, *link_target = NULL;
4974 const char *modestr = "";
4977 te = got_object_tree_get_entry(s->tree, i);
4978 mode = got_tree_entry_get_mode(te);
4981 err = got_object_id_str(&id_str,
4982 got_tree_entry_get_id(te));
4984 return got_error_from_errno(
4985 "got_object_id_str");
4987 if (got_object_tree_entry_is_submodule(te))
4989 else if (S_ISLNK(mode)) {
4992 err = got_tree_entry_get_symlink_target(&link_target,
4998 for (i = 0; i < strlen(link_target); i++) {
4999 if (!isprint((unsigned char)link_target[i]))
5000 link_target[i] = '?';
5004 else if (S_ISDIR(mode))
5006 else if (mode & S_IXUSR)
5008 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5009 got_tree_entry_get_name(te), modestr,
5010 link_target ? " -> ": "",
5011 link_target ? link_target : "") == -1) {
5014 return got_error_from_errno("asprintf");
5018 err = format_line(&wline, &width, line, view->ncols, 0);
5023 if (n == s->selected) {
5025 wstandout(view->window);
5026 s->selected_entry = te;
5028 tc = match_color(&s->colors, line);
5030 wattr_on(view->window,
5031 COLOR_PAIR(tc->colorpair), NULL);
5032 waddwstr(view->window, wline);
5034 wattr_off(view->window,
5035 COLOR_PAIR(tc->colorpair), NULL);
5036 if (width < view->ncols - 1)
5037 waddch(view->window, '\n');
5038 if (n == s->selected && view->focussed)
5039 wstandend(view->window);
5045 s->last_displayed_entry = te;
5054 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5056 struct got_tree_entry *te;
5057 int isroot = s->tree == s->root;
5060 if (s->first_displayed_entry == NULL)
5063 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5064 while (i++ < maxscroll) {
5067 s->first_displayed_entry = NULL;
5070 s->first_displayed_entry = te;
5071 te = got_tree_entry_get_prev(s->tree, te);
5076 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5078 struct got_tree_entry *next, *last;
5081 if (s->first_displayed_entry)
5082 next = got_tree_entry_get_next(s->tree,
5083 s->first_displayed_entry);
5085 next = got_object_tree_get_first_entry(s->tree);
5087 last = s->last_displayed_entry;
5088 while (next && last && n++ < maxscroll) {
5089 last = got_tree_entry_get_next(s->tree, last);
5091 s->first_displayed_entry = next;
5092 next = got_tree_entry_get_next(s->tree, next);
5097 static const struct got_error *
5098 tree_entry_path(char **path, struct tog_parent_trees *parents,
5099 struct got_tree_entry *te)
5101 const struct got_error *err = NULL;
5102 struct tog_parent_tree *pt;
5103 size_t len = 2; /* for leading slash and NUL */
5105 TAILQ_FOREACH(pt, parents, entry)
5106 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5109 len += strlen(got_tree_entry_get_name(te));
5111 *path = calloc(1, len);
5113 return got_error_from_errno("calloc");
5116 pt = TAILQ_LAST(parents, tog_parent_trees);
5118 const char *name = got_tree_entry_get_name(pt->selected_entry);
5119 if (strlcat(*path, name, len) >= len) {
5120 err = got_error(GOT_ERR_NO_SPACE);
5123 if (strlcat(*path, "/", len) >= len) {
5124 err = got_error(GOT_ERR_NO_SPACE);
5127 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5130 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5131 err = got_error(GOT_ERR_NO_SPACE);
5143 static const struct got_error *
5144 blame_tree_entry(struct tog_view **new_view, int begin_x,
5145 struct got_tree_entry *te, struct tog_parent_trees *parents,
5146 struct got_object_id *commit_id, struct got_repository *repo)
5148 const struct got_error *err = NULL;
5150 struct tog_view *blame_view;
5154 err = tree_entry_path(&path, parents, te);
5158 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5159 if (blame_view == NULL) {
5160 err = got_error_from_errno("view_open");
5164 err = open_blame_view(blame_view, path, commit_id, repo);
5166 if (err->code == GOT_ERR_CANCELLED)
5168 view_close(blame_view);
5170 *new_view = blame_view;
5176 static const struct got_error *
5177 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5178 struct tog_tree_view_state *s)
5180 struct tog_view *log_view;
5181 const struct got_error *err = NULL;
5186 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5187 if (log_view == NULL)
5188 return got_error_from_errno("view_open");
5190 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5194 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5197 view_close(log_view);
5199 *new_view = log_view;
5204 static const struct got_error *
5205 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5206 const char *head_ref_name, struct got_repository *repo)
5208 const struct got_error *err = NULL;
5209 char *commit_id_str = NULL;
5210 struct tog_tree_view_state *s = &view->state.tree;
5211 struct got_commit_object *commit = NULL;
5213 TAILQ_INIT(&s->parents);
5214 STAILQ_INIT(&s->colors);
5216 s->commit_id = got_object_id_dup(commit_id);
5217 if (s->commit_id == NULL)
5218 return got_error_from_errno("got_object_id_dup");
5220 err = got_object_open_as_commit(&commit, repo, commit_id);
5225 * The root is opened here and will be closed when the view is closed.
5226 * Any visited subtrees and their path-wise parents are opened and
5229 err = got_object_open_as_tree(&s->root, repo,
5230 got_object_commit_get_tree_id(commit));
5235 err = got_object_id_str(&commit_id_str, commit_id);
5239 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5240 err = got_error_from_errno("asprintf");
5244 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5245 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5246 if (head_ref_name) {
5247 s->head_ref_name = strdup(head_ref_name);
5248 if (s->head_ref_name == NULL) {
5249 err = got_error_from_errno("strdup");
5255 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5256 err = add_color(&s->colors, "\\$$",
5257 TOG_COLOR_TREE_SUBMODULE,
5258 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5261 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5262 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5265 err = add_color(&s->colors, "/$",
5266 TOG_COLOR_TREE_DIRECTORY,
5267 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5271 err = add_color(&s->colors, "\\*$",
5272 TOG_COLOR_TREE_EXECUTABLE,
5273 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5277 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5278 get_color_value("TOG_COLOR_COMMIT"));
5283 view->show = show_tree_view;
5284 view->input = input_tree_view;
5285 view->close = close_tree_view;
5286 view->search_start = search_start_tree_view;
5287 view->search_next = search_next_tree_view;
5289 free(commit_id_str);
5291 got_object_commit_close(commit);
5293 close_tree_view(view);
5297 static const struct got_error *
5298 close_tree_view(struct tog_view *view)
5300 struct tog_tree_view_state *s = &view->state.tree;
5302 free_colors(&s->colors);
5303 free(s->tree_label);
5304 s->tree_label = NULL;
5306 s->commit_id = NULL;
5307 free(s->head_ref_name);
5308 s->head_ref_name = NULL;
5309 while (!TAILQ_EMPTY(&s->parents)) {
5310 struct tog_parent_tree *parent;
5311 parent = TAILQ_FIRST(&s->parents);
5312 TAILQ_REMOVE(&s->parents, parent, entry);
5313 if (parent->tree != s->root)
5314 got_object_tree_close(parent->tree);
5318 if (s->tree != NULL && s->tree != s->root)
5319 got_object_tree_close(s->tree);
5321 got_object_tree_close(s->root);
5325 static const struct got_error *
5326 search_start_tree_view(struct tog_view *view)
5328 struct tog_tree_view_state *s = &view->state.tree;
5330 s->matched_entry = NULL;
5335 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5337 regmatch_t regmatch;
5339 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5343 static const struct got_error *
5344 search_next_tree_view(struct tog_view *view)
5346 struct tog_tree_view_state *s = &view->state.tree;
5347 struct got_tree_entry *te = NULL;
5349 if (!view->searching) {
5350 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5354 if (s->matched_entry) {
5355 if (view->searching == TOG_SEARCH_FORWARD) {
5356 if (s->selected_entry)
5357 te = got_tree_entry_get_next(s->tree,
5360 te = got_object_tree_get_first_entry(s->tree);
5362 if (s->selected_entry == NULL)
5363 te = got_object_tree_get_last_entry(s->tree);
5365 te = got_tree_entry_get_prev(s->tree,
5369 if (s->selected_entry)
5370 te = s->selected_entry;
5371 else 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 (s->selected_entry)
6031 re = s->selected_entry;
6032 else if (view->searching == TOG_SEARCH_FORWARD)
6033 re = TAILQ_FIRST(&s->refs);
6035 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6040 if (s->matched_entry == NULL) {
6041 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6044 if (view->searching == TOG_SEARCH_FORWARD)
6045 re = TAILQ_FIRST(&s->refs);
6047 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6050 if (match_reflist_entry(re, &view->regex)) {
6051 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6052 s->matched_entry = re;
6056 if (view->searching == TOG_SEARCH_FORWARD)
6057 re = TAILQ_NEXT(re, entry);
6059 re = TAILQ_PREV(re, tog_reflist_head, entry);
6062 if (s->matched_entry) {
6063 s->first_displayed_entry = s->matched_entry;
6070 static const struct got_error *
6071 show_ref_view(struct tog_view *view)
6073 const struct got_error *err = NULL;
6074 struct tog_ref_view_state *s = &view->state.ref;
6075 struct tog_reflist_entry *re;
6078 struct tog_color *tc;
6080 int limit = view->nlines;
6082 werase(view->window);
6089 re = s->first_displayed_entry;
6091 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6093 return got_error_from_errno("asprintf");
6095 err = format_line(&wline, &width, line, view->ncols, 0);
6100 if (view_needs_focus_indication(view))
6101 wstandout(view->window);
6102 waddwstr(view->window, wline);
6103 if (view_needs_focus_indication(view))
6104 wstandend(view->window);
6109 if (width < view->ncols - 1)
6110 waddch(view->window, '\n');
6115 while (re && limit > 0) {
6118 if (got_ref_is_symbolic(re->ref)) {
6119 if (asprintf(&line, "%s -> %s",
6120 got_ref_get_name(re->ref),
6121 got_ref_get_symref_target(re->ref)) == -1)
6122 return got_error_from_errno("asprintf");
6123 } else if (s->show_ids) {
6124 struct got_object_id *id;
6126 err = got_ref_resolve(&id, s->repo, re->ref);
6129 err = got_object_id_str(&id_str, id);
6134 if (asprintf(&line, "%s: %s",
6135 got_ref_get_name(re->ref), id_str) == -1) {
6136 err = got_error_from_errno("asprintf");
6144 line = strdup(got_ref_get_name(re->ref));
6146 return got_error_from_errno("strdup");
6149 err = format_line(&wline, &width, line, view->ncols, 0);
6154 if (n == s->selected) {
6156 wstandout(view->window);
6157 s->selected_entry = re;
6159 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6161 wattr_on(view->window,
6162 COLOR_PAIR(tc->colorpair), NULL);
6163 waddwstr(view->window, wline);
6165 wattr_off(view->window,
6166 COLOR_PAIR(tc->colorpair), NULL);
6167 if (width < view->ncols - 1)
6168 waddch(view->window, '\n');
6169 if (n == s->selected && view->focussed)
6170 wstandend(view->window);
6176 s->last_displayed_entry = re;
6179 re = TAILQ_NEXT(re, entry);
6186 static const struct got_error *
6187 browse_ref_tree(struct tog_view **new_view, int begin_x,
6188 struct tog_reflist_entry *re, struct got_repository *repo)
6190 const struct got_error *err = NULL;
6191 struct got_object_id *commit_id = NULL;
6192 struct tog_view *tree_view;
6196 err = resolve_reflist_entry(&commit_id, re, repo);
6198 if (err->code != GOT_ERR_OBJ_TYPE)
6205 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6206 if (tree_view == NULL) {
6207 err = got_error_from_errno("view_open");
6211 err = open_tree_view(tree_view, commit_id,
6212 got_ref_get_name(re->ref), repo);
6216 *new_view = tree_view;
6221 static const struct got_error *
6222 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6224 const struct got_error *err = NULL;
6225 struct tog_ref_view_state *s = &view->state.ref;
6226 struct tog_view *log_view, *tree_view;
6227 struct tog_reflist_entry *re;
6232 s->show_ids = !s->show_ids;
6235 s->sort_by_date = !s->sort_by_date;
6236 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6237 got_ref_cmp_by_commit_timestamp_descending :
6238 got_ref_cmp_by_name, s->repo);
6241 got_reflist_object_id_map_free(tog_refs_idmap);
6242 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6243 &tog_refs, s->repo);
6246 ref_view_free_refs(s);
6247 err = ref_view_load_refs(s);
6251 if (!s->selected_entry)
6253 if (view_is_parent_view(view))
6254 begin_x = view_split_begin_x(view->begin_x);
6255 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6258 log_view->focussed = 1;
6259 if (view_is_parent_view(view)) {
6260 err = view_close_child(view);
6263 view_set_child(view, log_view);
6264 view->focus_child = 1;
6266 *new_view = log_view;
6269 if (!s->selected_entry)
6271 if (view_is_parent_view(view))
6272 begin_x = view_split_begin_x(view->begin_x);
6273 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6275 if (err || tree_view == NULL)
6278 tree_view->focussed = 1;
6279 if (view_is_parent_view(view)) {
6280 err = view_close_child(view);
6283 view_set_child(view, tree_view);
6284 view->focus_child = 1;
6286 *new_view = tree_view;
6291 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6296 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6297 for (n = 0; n < view->nlines - 1; n++) {
6300 s->first_displayed_entry = re;
6301 re = TAILQ_PREV(re, tog_reflist_head, entry);
6304 s->selected = n - 1;
6309 if (s->selected > 0) {
6313 ref_scroll_up(s, 1);
6317 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6319 ref_scroll_up(s, MAX(0, view->nlines - 1));
6324 if (s->selected < s->ndisplayed - 1) {
6328 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6329 /* can't scroll any further */
6331 ref_scroll_down(s, 1);
6335 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6336 /* can't scroll any further; move cursor down */
6337 if (s->selected < s->ndisplayed - 1)
6338 s->selected = s->ndisplayed - 1;
6341 ref_scroll_down(s, view->nlines - 1);
6345 err = tog_load_refs(s->repo, s->sort_by_date);
6348 ref_view_free_refs(s);
6349 err = ref_view_load_refs(s);
6352 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6353 s->selected = view->nlines - 2;
6366 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6371 static const struct got_error *
6372 cmd_ref(int argc, char *argv[])
6374 const struct got_error *error;
6375 struct got_repository *repo = NULL;
6376 struct got_worktree *worktree = NULL;
6377 char *cwd = NULL, *repo_path = NULL;
6379 struct tog_view *view;
6381 while ((ch = getopt(argc, argv, "r:")) != -1) {
6384 repo_path = realpath(optarg, NULL);
6385 if (repo_path == NULL)
6386 return got_error_from_errno2("realpath",
6401 if (repo_path == NULL) {
6402 cwd = getcwd(NULL, 0);
6404 return got_error_from_errno("getcwd");
6405 error = got_worktree_open(&worktree, cwd);
6406 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6410 strdup(got_worktree_get_repo_path(worktree));
6412 repo_path = strdup(cwd);
6413 if (repo_path == NULL) {
6414 error = got_error_from_errno("strdup");
6419 error = got_repo_open(&repo, repo_path, NULL);
6425 error = apply_unveil(got_repo_get_path(repo), NULL);
6429 error = tog_load_refs(repo, 0);
6433 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6435 error = got_error_from_errno("view_open");
6439 error = open_ref_view(view, repo);
6444 /* Release work tree lock. */
6445 got_worktree_close(worktree);
6448 error = view_loop(view);
6453 const struct got_error *close_err = got_repo_close(repo);
6462 list_commands(FILE *fp)
6466 fprintf(fp, "commands:");
6467 for (i = 0; i < nitems(tog_commands); i++) {
6468 struct tog_cmd *cmd = &tog_commands[i];
6469 fprintf(fp, " %s", cmd->name);
6475 usage(int hflag, int status)
6477 FILE *fp = (status == 0) ? stdout : stderr;
6479 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6482 fprintf(fp, "lazy usage: %s path\n", getprogname());
6489 make_argv(int argc, ...)
6497 argv = calloc(argc, sizeof(char *));
6500 for (i = 0; i < argc; i++) {
6501 argv[i] = strdup(va_arg(ap, char *));
6502 if (argv[i] == NULL)
6511 * Try to convert 'tog path' into a 'tog log path' command.
6512 * The user could simply have mistyped the command rather than knowingly
6513 * provided a path. So check whether argv[0] can in fact be resolved
6514 * to a path in the HEAD commit and print a special error if not.
6515 * This hack is for mpi@ <3
6517 static const struct got_error *
6518 tog_log_with_path(int argc, char *argv[])
6520 const struct got_error *error = NULL, *close_err;
6521 struct tog_cmd *cmd = NULL;
6522 struct got_repository *repo = NULL;
6523 struct got_worktree *worktree = NULL;
6524 struct got_object_id *commit_id = NULL, *id = NULL;
6525 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6526 char *commit_id_str = NULL, **cmd_argv = NULL;
6528 cwd = getcwd(NULL, 0);
6530 return got_error_from_errno("getcwd");
6532 error = got_worktree_open(&worktree, cwd);
6533 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6537 repo_path = strdup(got_worktree_get_repo_path(worktree));
6539 repo_path = strdup(cwd);
6540 if (repo_path == NULL) {
6541 error = got_error_from_errno("strdup");
6545 error = got_repo_open(&repo, repo_path, NULL);
6549 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6554 error = tog_load_refs(repo, 0);
6557 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6558 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6559 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6564 got_worktree_close(worktree);
6568 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6570 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6572 fprintf(stderr, "%s: '%s' is no known command or path\n",
6573 getprogname(), argv[0]);
6578 close_err = got_repo_close(repo);
6583 error = got_object_id_str(&commit_id_str, commit_id);
6587 cmd = &tog_commands[0]; /* log */
6589 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6590 error = cmd->cmd_main(argc, cmd_argv);
6593 close_err = got_repo_close(repo);
6598 got_worktree_close(worktree);
6600 free(commit_id_str);
6607 for (i = 0; i < argc; i++)
6616 main(int argc, char *argv[])
6618 const struct got_error *error = NULL;
6619 struct tog_cmd *cmd = NULL;
6620 int ch, hflag = 0, Vflag = 0;
6621 char **cmd_argv = NULL;
6622 static struct option longopts[] = {
6623 { "version", no_argument, NULL, 'V' },
6627 setlocale(LC_CTYPE, "");
6629 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6649 got_version_print_str();
6654 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6662 /* Build an argument vector which runs a default command. */
6663 cmd = &tog_commands[0];
6665 cmd_argv = make_argv(argc, cmd->name);
6669 /* Did the user specify a command? */
6670 for (i = 0; i < nitems(tog_commands); i++) {
6671 if (strncmp(tog_commands[i].name, argv[0],
6672 strlen(argv[0])) == 0) {
6673 cmd = &tog_commands[i];
6682 /* No command specified; try log with a path */
6683 error = tog_log_with_path(argc, argv);
6688 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6695 for (i = 0; i < argc; i++)
6700 if (error && error->code != GOT_ERR_CANCELLED)
6701 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);