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 const 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_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
133 struct got_reference* re2)
135 const char *name1 = got_ref_get_name(re1);
136 const char *name2 = got_ref_get_name(re2);
137 int isbackup1, isbackup2;
139 /* Sort backup refs towards the bottom of the list. */
140 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
141 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
142 if (!isbackup1 && isbackup2) {
145 } else if (isbackup1 && !isbackup2) {
150 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
154 static const struct got_error *
155 tog_load_refs(struct got_repository *repo, int sort_by_date)
157 const struct got_error *err;
159 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
160 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
165 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
172 if (tog_refs_idmap) {
173 got_reflist_object_id_map_free(tog_refs_idmap);
174 tog_refs_idmap = NULL;
176 got_ref_list_free(&tog_refs);
179 static const struct got_error *
180 add_color(struct tog_colors *colors, const char *pattern,
181 int idx, short color)
183 const struct got_error *err = NULL;
184 struct tog_color *tc;
187 if (idx < 1 || idx > COLOR_PAIRS - 1)
190 init_pair(idx, color, -1);
192 tc = calloc(1, sizeof(*tc));
194 return got_error_from_errno("calloc");
195 regerr = regcomp(&tc->regex, pattern,
196 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
198 static char regerr_msg[512];
199 static char err_msg[512];
200 regerror(regerr, &tc->regex, regerr_msg,
202 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
204 err = got_error_msg(GOT_ERR_REGEX, err_msg);
209 STAILQ_INSERT_HEAD(colors, tc, entry);
214 free_colors(struct tog_colors *colors)
216 struct tog_color *tc;
218 while (!STAILQ_EMPTY(colors)) {
219 tc = STAILQ_FIRST(colors);
220 STAILQ_REMOVE_HEAD(colors, entry);
227 get_color(struct tog_colors *colors, int colorpair)
229 struct tog_color *tc = NULL;
231 STAILQ_FOREACH(tc, colors, entry) {
232 if (tc->colorpair == colorpair)
240 default_color_value(const char *envvar)
242 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
243 return COLOR_MAGENTA;
244 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
246 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
248 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
250 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
251 return COLOR_MAGENTA;
252 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
253 return COLOR_MAGENTA;
254 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
256 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
258 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
260 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
262 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
264 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
266 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
267 return COLOR_MAGENTA;
268 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
270 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
277 get_color_value(const char *envvar)
279 const char *val = getenv(envvar);
282 return default_color_value(envvar);
284 if (strcasecmp(val, "black") == 0)
286 if (strcasecmp(val, "red") == 0)
288 if (strcasecmp(val, "green") == 0)
290 if (strcasecmp(val, "yellow") == 0)
292 if (strcasecmp(val, "blue") == 0)
294 if (strcasecmp(val, "magenta") == 0)
295 return COLOR_MAGENTA;
296 if (strcasecmp(val, "cyan") == 0)
298 if (strcasecmp(val, "white") == 0)
300 if (strcasecmp(val, "default") == 0)
303 return default_color_value(envvar);
307 struct tog_diff_view_state {
308 struct got_object_id *id1, *id2;
309 const char *label1, *label2;
311 int first_displayed_line;
312 int last_displayed_line;
315 int ignore_whitespace;
317 struct got_repository *repo;
318 struct tog_colors colors;
324 /* passed from log view; may be NULL */
325 struct tog_view *log_view;
328 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
330 struct tog_log_thread_args {
331 pthread_cond_t need_commits;
332 pthread_cond_t commit_loaded;
335 struct got_commit_graph *graph;
336 struct commit_queue *commits;
337 const char *in_repo_path;
338 struct got_object_id *start_id;
339 struct got_repository *repo;
342 struct commit_queue_entry **first_displayed_entry;
343 struct commit_queue_entry **selected_entry;
345 int *search_next_done;
349 struct tog_log_view_state {
350 struct commit_queue commits;
351 struct commit_queue_entry *first_displayed_entry;
352 struct commit_queue_entry *last_displayed_entry;
353 struct commit_queue_entry *selected_entry;
358 struct got_repository *repo;
359 struct got_object_id *start_id;
362 struct tog_log_thread_args thread_args;
363 struct commit_queue_entry *matched_entry;
364 struct commit_queue_entry *search_entry;
365 struct tog_colors colors;
368 #define TOG_COLOR_DIFF_MINUS 1
369 #define TOG_COLOR_DIFF_PLUS 2
370 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
371 #define TOG_COLOR_DIFF_META 4
372 #define TOG_COLOR_TREE_SUBMODULE 5
373 #define TOG_COLOR_TREE_SYMLINK 6
374 #define TOG_COLOR_TREE_DIRECTORY 7
375 #define TOG_COLOR_TREE_EXECUTABLE 8
376 #define TOG_COLOR_COMMIT 9
377 #define TOG_COLOR_AUTHOR 10
378 #define TOG_COLOR_DATE 11
379 #define TOG_COLOR_REFS_HEADS 12
380 #define TOG_COLOR_REFS_TAGS 13
381 #define TOG_COLOR_REFS_REMOTES 14
382 #define TOG_COLOR_REFS_BACKUP 15
384 struct tog_blame_cb_args {
385 struct tog_blame_line *lines; /* one per line */
388 struct tog_view *view;
389 struct got_object_id *commit_id;
393 struct tog_blame_thread_args {
395 struct got_repository *repo;
396 struct tog_blame_cb_args *cb_args;
398 got_cancel_cb cancel_cb;
405 struct tog_blame_line *lines;
409 struct tog_blame_thread_args thread_args;
410 struct tog_blame_cb_args cb_args;
414 struct tog_blame_view_state {
415 int first_displayed_line;
416 int last_displayed_line;
421 struct got_object_id_queue blamed_commits;
422 struct got_object_qid *blamed_commit;
424 struct got_repository *repo;
425 struct got_object_id *commit_id;
426 struct tog_blame blame;
428 struct tog_colors colors;
431 struct tog_parent_tree {
432 TAILQ_ENTRY(tog_parent_tree) entry;
433 struct got_tree_object *tree;
434 struct got_tree_entry *first_displayed_entry;
435 struct got_tree_entry *selected_entry;
439 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
441 struct tog_tree_view_state {
443 struct got_object_id *commit_id;/* commit which this tree belongs to */
444 struct got_tree_object *root; /* the commit's root tree entry */
445 struct got_tree_object *tree; /* currently displayed (sub-)tree */
446 struct got_tree_entry *first_displayed_entry;
447 struct got_tree_entry *last_displayed_entry;
448 struct got_tree_entry *selected_entry;
449 int ndisplayed, selected, show_ids;
450 struct tog_parent_trees parents; /* parent trees of current sub-tree */
452 struct got_repository *repo;
453 struct got_tree_entry *matched_entry;
454 struct tog_colors colors;
457 struct tog_reflist_entry {
458 TAILQ_ENTRY(tog_reflist_entry) entry;
459 struct got_reference *ref;
463 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
465 struct tog_ref_view_state {
466 struct tog_reflist_head refs;
467 struct tog_reflist_entry *first_displayed_entry;
468 struct tog_reflist_entry *last_displayed_entry;
469 struct tog_reflist_entry *selected_entry;
470 int nrefs, ndisplayed, selected, show_ids, sort_by_date;
471 struct got_repository *repo;
472 struct tog_reflist_entry *matched_entry;
473 struct tog_colors colors;
477 * We implement two types of views: parent views and child views.
479 * The 'Tab' key switches focus between a parent view and its child view.
480 * Child views are shown side-by-side to their parent view, provided
481 * there is enough screen estate.
483 * When a new view is opened from within a parent view, this new view
484 * becomes a child view of the parent view, replacing any existing child.
486 * When a new view is opened from within a child view, this new view
487 * becomes a parent view which will obscure the views below until the
488 * user quits the new parent view by typing 'q'.
490 * This list of views contains parent views only.
491 * Child views are only pointed to by their parent view.
493 TAILQ_HEAD(tog_view_list_head, tog_view);
496 TAILQ_ENTRY(tog_view) entry;
499 int nlines, ncols, begin_y, begin_x;
500 int lines, cols; /* copies of LINES and COLS */
501 int focussed; /* Only set on one parent or child view at a time. */
503 struct tog_view *parent;
504 struct tog_view *child;
507 * This flag is initially set on parent views when a new child view
508 * is created. It gets toggled when the 'Tab' key switches focus
509 * between parent and child.
510 * The flag indicates whether focus should be passed on to our child
511 * view if this parent view gets picked for focus after another parent
512 * view was closed. This prevents child views from losing focus in such
517 /* type-specific state */
518 enum tog_view_type type;
520 struct tog_diff_view_state diff;
521 struct tog_log_view_state log;
522 struct tog_blame_view_state blame;
523 struct tog_tree_view_state tree;
524 struct tog_ref_view_state ref;
527 const struct got_error *(*show)(struct tog_view *);
528 const struct got_error *(*input)(struct tog_view **,
529 struct tog_view *, int);
530 const struct got_error *(*close)(struct tog_view *);
532 const struct got_error *(*search_start)(struct tog_view *);
533 const struct got_error *(*search_next)(struct tog_view *);
536 #define TOG_SEARCH_FORWARD 1
537 #define TOG_SEARCH_BACKWARD 2
538 int search_next_done;
539 #define TOG_SEARCH_HAVE_MORE 1
540 #define TOG_SEARCH_NO_MORE 2
541 #define TOG_SEARCH_HAVE_NONE 3
546 static const struct got_error *open_diff_view(struct tog_view *,
547 struct got_object_id *, struct got_object_id *,
548 const char *, const char *, int, int, int, struct tog_view *,
549 struct got_repository *);
550 static const struct got_error *show_diff_view(struct tog_view *);
551 static const struct got_error *input_diff_view(struct tog_view **,
552 struct tog_view *, int);
553 static const struct got_error* close_diff_view(struct tog_view *);
554 static const struct got_error *search_start_diff_view(struct tog_view *);
555 static const struct got_error *search_next_diff_view(struct tog_view *);
557 static const struct got_error *open_log_view(struct tog_view *,
558 struct got_object_id *, struct got_repository *,
559 const char *, const char *, int);
560 static const struct got_error * show_log_view(struct tog_view *);
561 static const struct got_error *input_log_view(struct tog_view **,
562 struct tog_view *, int);
563 static const struct got_error *close_log_view(struct tog_view *);
564 static const struct got_error *search_start_log_view(struct tog_view *);
565 static const struct got_error *search_next_log_view(struct tog_view *);
567 static const struct got_error *open_blame_view(struct tog_view *, char *,
568 struct got_object_id *, struct got_repository *);
569 static const struct got_error *show_blame_view(struct tog_view *);
570 static const struct got_error *input_blame_view(struct tog_view **,
571 struct tog_view *, int);
572 static const struct got_error *close_blame_view(struct tog_view *);
573 static const struct got_error *search_start_blame_view(struct tog_view *);
574 static const struct got_error *search_next_blame_view(struct tog_view *);
576 static const struct got_error *open_tree_view(struct tog_view *,
577 struct got_object_id *, const char *, struct got_repository *);
578 static const struct got_error *show_tree_view(struct tog_view *);
579 static const struct got_error *input_tree_view(struct tog_view **,
580 struct tog_view *, int);
581 static const struct got_error *close_tree_view(struct tog_view *);
582 static const struct got_error *search_start_tree_view(struct tog_view *);
583 static const struct got_error *search_next_tree_view(struct tog_view *);
585 static const struct got_error *open_ref_view(struct tog_view *,
586 struct got_repository *);
587 static const struct got_error *show_ref_view(struct tog_view *);
588 static const struct got_error *input_ref_view(struct tog_view **,
589 struct tog_view *, int);
590 static const struct got_error *close_ref_view(struct tog_view *);
591 static const struct got_error *search_start_ref_view(struct tog_view *);
592 static const struct got_error *search_next_ref_view(struct tog_view *);
594 static volatile sig_atomic_t tog_sigwinch_received;
595 static volatile sig_atomic_t tog_sigpipe_received;
596 static volatile sig_atomic_t tog_sigcont_received;
599 tog_sigwinch(int signo)
601 tog_sigwinch_received = 1;
605 tog_sigpipe(int signo)
607 tog_sigpipe_received = 1;
611 tog_sigcont(int signo)
613 tog_sigcont_received = 1;
616 static const struct got_error *
617 view_close(struct tog_view *view)
619 const struct got_error *err = NULL;
622 view_close(view->child);
626 err = view->close(view);
628 del_panel(view->panel);
630 delwin(view->window);
635 static struct tog_view *
636 view_open(int nlines, int ncols, int begin_y, int begin_x,
637 enum tog_view_type type)
639 struct tog_view *view = calloc(1, sizeof(*view));
647 view->nlines = nlines ? nlines : LINES - begin_y;
648 view->ncols = ncols ? ncols : COLS - begin_x;
649 view->begin_y = begin_y;
650 view->begin_x = begin_x;
651 view->window = newwin(nlines, ncols, begin_y, begin_x);
652 if (view->window == NULL) {
656 view->panel = new_panel(view->window);
657 if (view->panel == NULL ||
658 set_panel_userptr(view->panel, view) != OK) {
663 keypad(view->window, TRUE);
668 view_split_begin_x(int begin_x)
670 if (begin_x > 0 || COLS < 120)
672 return (COLS - MAX(COLS / 2, 80));
675 static const struct got_error *view_resize(struct tog_view *);
677 static const struct got_error *
678 view_splitscreen(struct tog_view *view)
680 const struct got_error *err = NULL;
683 view->begin_x = view_split_begin_x(0);
684 view->nlines = LINES;
685 view->ncols = COLS - view->begin_x;
688 err = view_resize(view);
692 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
693 return got_error_from_errno("mvwin");
698 static const struct got_error *
699 view_fullscreen(struct tog_view *view)
701 const struct got_error *err = NULL;
705 view->nlines = LINES;
709 err = view_resize(view);
713 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
714 return got_error_from_errno("mvwin");
720 view_is_parent_view(struct tog_view *view)
722 return view->parent == NULL;
725 static const struct got_error *
726 view_resize(struct tog_view *view)
730 if (view->lines > LINES)
731 nlines = view->nlines - (view->lines - LINES);
733 nlines = view->nlines + (LINES - view->lines);
735 if (view->cols > COLS)
736 ncols = view->ncols - (view->cols - COLS);
738 ncols = view->ncols + (COLS - view->cols);
740 if (wresize(view->window, nlines, ncols) == ERR)
741 return got_error_from_errno("wresize");
742 if (replace_panel(view->panel, view->window) == ERR)
743 return got_error_from_errno("replace_panel");
744 wclear(view->window);
746 view->nlines = nlines;
752 view->child->begin_x = view_split_begin_x(view->begin_x);
753 if (view->child->begin_x == 0) {
754 view_fullscreen(view->child);
755 if (view->child->focussed)
756 show_panel(view->child->panel);
758 show_panel(view->panel);
760 view_splitscreen(view->child);
761 show_panel(view->child->panel);
768 static const struct got_error *
769 view_close_child(struct tog_view *view)
771 const struct got_error *err = NULL;
773 if (view->child == NULL)
776 err = view_close(view->child);
782 view_set_child(struct tog_view *view, struct tog_view *child)
785 child->parent = view;
789 view_is_splitscreen(struct tog_view *view)
791 return view->begin_x > 0;
800 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
801 cols = 80; /* Default */
807 resize_term(lines, cols);
810 static const struct got_error *
811 view_search_start(struct tog_view *view)
813 const struct got_error *err = NULL;
817 if (view->search_started) {
818 regfree(&view->regex);
820 memset(&view->regmatch, 0, sizeof(view->regmatch));
822 view->search_started = 0;
824 if (view->nlines < 1)
827 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
828 wclrtoeol(view->window);
832 ret = wgetnstr(view->window, pattern, sizeof(pattern));
838 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
839 err = view->search_start(view);
841 regfree(&view->regex);
844 view->search_started = 1;
845 view->searching = TOG_SEARCH_FORWARD;
846 view->search_next_done = 0;
847 view->search_next(view);
853 static const struct got_error *
854 view_input(struct tog_view **new, int *done, struct tog_view *view,
855 struct tog_view_list_head *views)
857 const struct got_error *err = NULL;
863 /* Clear "no matches" indicator. */
864 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
865 view->search_next_done == TOG_SEARCH_HAVE_NONE)
866 view->search_next_done = TOG_SEARCH_HAVE_MORE;
868 if (view->searching && !view->search_next_done) {
869 errcode = pthread_mutex_unlock(&tog_mutex);
871 return got_error_set_errno(errcode,
872 "pthread_mutex_unlock");
874 errcode = pthread_mutex_lock(&tog_mutex);
876 return got_error_set_errno(errcode,
877 "pthread_mutex_lock");
878 view->search_next(view);
882 nodelay(stdscr, FALSE);
883 /* Allow threads to make progress while we are waiting for input. */
884 errcode = pthread_mutex_unlock(&tog_mutex);
886 return got_error_set_errno(errcode, "pthread_mutex_unlock");
887 ch = wgetch(view->window);
888 errcode = pthread_mutex_lock(&tog_mutex);
890 return got_error_set_errno(errcode, "pthread_mutex_lock");
891 nodelay(stdscr, TRUE);
893 if (tog_sigwinch_received || tog_sigcont_received) {
895 tog_sigwinch_received = 0;
896 tog_sigcont_received = 0;
897 TAILQ_FOREACH(v, views, entry) {
898 err = view_resize(v);
901 err = v->input(new, v, KEY_RESIZE);
905 err = view_resize(v->child);
908 err = v->child->input(new, v->child,
920 view->child->focussed = 1;
921 view->focus_child = 1;
922 } else if (view->parent) {
924 view->parent->focussed = 1;
925 view->parent->focus_child = 0;
929 err = view->input(new, view, ch);
936 if (view_is_parent_view(view)) {
937 if (view->child == NULL)
939 if (view_is_splitscreen(view->child)) {
941 view->child->focussed = 1;
942 err = view_fullscreen(view->child);
944 err = view_splitscreen(view->child);
947 err = view->child->input(new, view->child,
950 if (view_is_splitscreen(view)) {
951 view->parent->focussed = 0;
953 err = view_fullscreen(view);
955 err = view_splitscreen(view);
959 err = view->input(new, view, KEY_RESIZE);
965 if (view->search_start)
966 view_search_start(view);
968 err = view->input(new, view, ch);
972 if (view->search_started && view->search_next) {
973 view->searching = (ch == 'n' ?
974 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
975 view->search_next_done = 0;
976 view->search_next(view);
978 err = view->input(new, view, ch);
981 err = view->input(new, view, ch);
989 view_vborder(struct tog_view *view)
992 const struct tog_view *view_above;
995 return view_vborder(view->parent);
997 panel = panel_above(view->panel);
1001 view_above = panel_userptr(panel);
1002 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1003 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1007 view_needs_focus_indication(struct tog_view *view)
1009 if (view_is_parent_view(view)) {
1010 if (view->child == NULL || view->child->focussed)
1012 if (!view_is_splitscreen(view->child))
1014 } else if (!view_is_splitscreen(view))
1017 return view->focussed;
1020 static const struct got_error *
1021 view_loop(struct tog_view *view)
1023 const struct got_error *err = NULL;
1024 struct tog_view_list_head views;
1025 struct tog_view *new_view;
1026 int fast_refresh = 10;
1027 int done = 0, errcode;
1029 errcode = pthread_mutex_lock(&tog_mutex);
1031 return got_error_set_errno(errcode, "pthread_mutex_lock");
1034 TAILQ_INSERT_HEAD(&views, view, entry);
1037 err = view->show(view);
1042 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1043 /* Refresh fast during initialization, then become slower. */
1044 if (fast_refresh && fast_refresh-- == 0)
1045 halfdelay(10); /* switch to once per second */
1047 err = view_input(&new_view, &done, view, &views);
1051 struct tog_view *v, *prev = NULL;
1053 if (view_is_parent_view(view))
1054 prev = TAILQ_PREV(view, tog_view_list_head,
1056 else if (view->parent)
1057 prev = view->parent;
1060 view->parent->child = NULL;
1061 view->parent->focus_child = 0;
1063 TAILQ_REMOVE(&views, view, entry);
1065 err = view_close(view);
1070 TAILQ_FOREACH(v, &views, entry) {
1074 if (view == NULL && new_view == NULL) {
1075 /* No view has focus. Try to pick one. */
1078 else if (!TAILQ_EMPTY(&views)) {
1079 view = TAILQ_LAST(&views,
1080 tog_view_list_head);
1083 if (view->focus_child) {
1084 view->child->focussed = 1;
1092 struct tog_view *v, *t;
1093 /* Only allow one parent view per type. */
1094 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1095 if (v->type != new_view->type)
1097 TAILQ_REMOVE(&views, v, entry);
1098 err = view_close(v);
1103 TAILQ_INSERT_TAIL(&views, new_view, entry);
1107 if (view_is_parent_view(view)) {
1108 if (view->child && view->child->focussed)
1111 if (view->parent && view->parent->focussed)
1112 view = view->parent;
1114 show_panel(view->panel);
1115 if (view->child && view_is_splitscreen(view->child))
1116 show_panel(view->child->panel);
1117 if (view->parent && view_is_splitscreen(view)) {
1118 err = view->parent->show(view->parent);
1122 err = view->show(view);
1126 err = view->child->show(view->child);
1135 while (!TAILQ_EMPTY(&views)) {
1136 view = TAILQ_FIRST(&views);
1137 TAILQ_REMOVE(&views, view, entry);
1141 errcode = pthread_mutex_unlock(&tog_mutex);
1142 if (errcode && err == NULL)
1143 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1153 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1158 /* Create newly allocated wide-character string equivalent to a byte string. */
1159 static const struct got_error *
1160 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1163 const struct got_error *err = NULL;
1166 *wlen = mbstowcs(NULL, s, 0);
1167 if (*wlen == (size_t)-1) {
1169 if (errno != EILSEQ)
1170 return got_error_from_errno("mbstowcs");
1172 /* byte string invalid in current encoding; try to "fix" it */
1173 err = got_mbsavis(&vis, &vislen, s);
1176 *wlen = mbstowcs(NULL, vis, 0);
1177 if (*wlen == (size_t)-1) {
1178 err = got_error_from_errno("mbstowcs"); /* give up */
1183 *ws = calloc(*wlen + 1, sizeof(**ws));
1185 err = got_error_from_errno("calloc");
1189 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1190 err = got_error_from_errno("mbstowcs");
1201 /* Format a line for display, ensuring that it won't overflow a width limit. */
1202 static const struct got_error *
1203 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1206 const struct got_error *err = NULL;
1208 wchar_t *wline = NULL;
1215 err = mbs2ws(&wline, &wlen, line);
1219 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1220 wline[wlen - 1] = L'\0';
1223 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1224 wline[wlen - 1] = L'\0';
1230 int width = wcwidth(wline[i]);
1237 if (width == 1 || width == 2) {
1238 if (cols + width > wlimit)
1242 } else if (width == -1) {
1243 if (wline[i] == L'\t') {
1245 ((cols + col_tab_align) % TABSIZE);
1250 if (cols + width > wlimit)
1255 err = got_error_from_errno("wcwidth");
1270 static const struct got_error*
1271 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1272 struct got_object_id *id, struct got_repository *repo)
1274 static const struct got_error *err = NULL;
1275 struct got_reflist_entry *re;
1281 TAILQ_FOREACH(re, refs, entry) {
1282 struct got_tag_object *tag = NULL;
1283 struct got_object_id *ref_id;
1286 name = got_ref_get_name(re->ref);
1287 if (strcmp(name, GOT_REF_HEAD) == 0)
1289 if (strncmp(name, "refs/", 5) == 0)
1291 if (strncmp(name, "got/", 4) == 0 &&
1292 strncmp(name, "got/backup/", 11) != 0)
1294 if (strncmp(name, "heads/", 6) == 0)
1296 if (strncmp(name, "remotes/", 8) == 0) {
1298 s = strstr(name, "/" GOT_REF_HEAD);
1299 if (s != NULL && s[strlen(s)] == '\0')
1302 err = got_ref_resolve(&ref_id, repo, re->ref);
1305 if (strncmp(name, "tags/", 5) == 0) {
1306 err = got_object_open_as_tag(&tag, repo, ref_id);
1308 if (err->code != GOT_ERR_OBJ_TYPE) {
1312 /* Ref points at something other than a tag. */
1317 cmp = got_object_id_cmp(tag ?
1318 got_object_tag_get_object_id(tag) : ref_id, id);
1321 got_object_tag_close(tag);
1325 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1326 s ? ", " : "", name) == -1) {
1327 err = got_error_from_errno("asprintf");
1338 static const struct got_error *
1339 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1344 smallerthan = strchr(author, '<');
1345 if (smallerthan && smallerthan[1] != '\0')
1346 author = smallerthan + 1;
1347 author[strcspn(author, "@>")] = '\0';
1348 return format_line(wauthor, author_width, author, limit, col_tab_align);
1351 static const struct got_error *
1352 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1353 struct got_object_id *id, const size_t date_display_cols,
1354 int author_display_cols)
1356 struct tog_log_view_state *s = &view->state.log;
1357 const struct got_error *err = NULL;
1358 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1359 char *logmsg0 = NULL, *logmsg = NULL;
1360 char *author = NULL;
1361 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1362 int author_width, logmsg_width;
1363 char *newline, *line = NULL;
1365 const int avail = view->ncols;
1367 time_t committer_time;
1368 struct tog_color *tc;
1370 committer_time = got_object_commit_get_committer_time(commit);
1371 if (gmtime_r(&committer_time, &tm) == NULL)
1372 return got_error_from_errno("gmtime_r");
1373 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1374 return got_error(GOT_ERR_NO_SPACE);
1376 if (avail <= date_display_cols)
1377 limit = MIN(sizeof(datebuf) - 1, avail);
1379 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1380 tc = get_color(&s->colors, TOG_COLOR_DATE);
1382 wattr_on(view->window,
1383 COLOR_PAIR(tc->colorpair), NULL);
1384 waddnstr(view->window, datebuf, limit);
1386 wattr_off(view->window,
1387 COLOR_PAIR(tc->colorpair), NULL);
1394 err = got_object_id_str(&id_str, id);
1397 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1399 wattr_on(view->window,
1400 COLOR_PAIR(tc->colorpair), NULL);
1401 wprintw(view->window, "%.8s ", id_str);
1403 wattr_off(view->window,
1404 COLOR_PAIR(tc->colorpair), NULL);
1411 author = strdup(got_object_commit_get_author(commit));
1412 if (author == NULL) {
1413 err = got_error_from_errno("strdup");
1416 err = format_author(&wauthor, &author_width, author, avail - col, col);
1419 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1421 wattr_on(view->window,
1422 COLOR_PAIR(tc->colorpair), NULL);
1423 waddwstr(view->window, wauthor);
1425 wattr_off(view->window,
1426 COLOR_PAIR(tc->colorpair), NULL);
1427 col += author_width;
1428 while (col < avail && author_width < author_display_cols + 2) {
1429 waddch(view->window, ' ');
1436 err = got_object_commit_get_logmsg(&logmsg0, commit);
1440 while (*logmsg == '\n')
1442 newline = strchr(logmsg, '\n');
1445 limit = avail - col;
1446 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1449 waddwstr(view->window, wlogmsg);
1450 col += logmsg_width;
1451 while (col < avail) {
1452 waddch(view->window, ' ');
1464 static struct commit_queue_entry *
1465 alloc_commit_queue_entry(struct got_commit_object *commit,
1466 struct got_object_id *id)
1468 struct commit_queue_entry *entry;
1470 entry = calloc(1, sizeof(*entry));
1475 entry->commit = commit;
1480 pop_commit(struct commit_queue *commits)
1482 struct commit_queue_entry *entry;
1484 entry = TAILQ_FIRST(&commits->head);
1485 TAILQ_REMOVE(&commits->head, entry, entry);
1486 got_object_commit_close(entry->commit);
1487 commits->ncommits--;
1488 /* Don't free entry->id! It is owned by the commit graph. */
1493 free_commits(struct commit_queue *commits)
1495 while (!TAILQ_EMPTY(&commits->head))
1496 pop_commit(commits);
1499 static const struct got_error *
1500 match_commit(int *have_match, struct got_object_id *id,
1501 struct got_commit_object *commit, regex_t *regex)
1503 const struct got_error *err = NULL;
1504 regmatch_t regmatch;
1505 char *id_str = NULL, *logmsg = NULL;
1509 err = got_object_id_str(&id_str, id);
1513 err = got_object_commit_get_logmsg(&logmsg, commit);
1517 if (regexec(regex, got_object_commit_get_author(commit), 1,
1518 ®match, 0) == 0 ||
1519 regexec(regex, got_object_commit_get_committer(commit), 1,
1520 ®match, 0) == 0 ||
1521 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1522 regexec(regex, logmsg, 1, ®match, 0) == 0)
1530 static const struct got_error *
1531 queue_commits(struct tog_log_thread_args *a)
1533 const struct got_error *err = NULL;
1536 * We keep all commits open throughout the lifetime of the log
1537 * view in order to avoid having to re-fetch commits from disk
1538 * while updating the display.
1541 struct got_object_id *id;
1542 struct got_commit_object *commit;
1543 struct commit_queue_entry *entry;
1546 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1548 if (err || id == NULL)
1551 err = got_object_open_as_commit(&commit, a->repo, id);
1554 entry = alloc_commit_queue_entry(commit, id);
1555 if (entry == NULL) {
1556 err = got_error_from_errno("alloc_commit_queue_entry");
1560 errcode = pthread_mutex_lock(&tog_mutex);
1562 err = got_error_set_errno(errcode,
1563 "pthread_mutex_lock");
1567 entry->idx = a->commits->ncommits;
1568 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1569 a->commits->ncommits++;
1571 if (*a->searching == TOG_SEARCH_FORWARD &&
1572 !*a->search_next_done) {
1574 err = match_commit(&have_match, id, commit, a->regex);
1578 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1581 errcode = pthread_mutex_unlock(&tog_mutex);
1582 if (errcode && err == NULL)
1583 err = got_error_set_errno(errcode,
1584 "pthread_mutex_unlock");
1587 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1593 select_commit(struct tog_log_view_state *s)
1595 struct commit_queue_entry *entry;
1598 entry = s->first_displayed_entry;
1600 if (ncommits == s->selected) {
1601 s->selected_entry = entry;
1604 entry = TAILQ_NEXT(entry, entry);
1609 static const struct got_error *
1610 draw_commits(struct tog_view *view)
1612 const struct got_error *err = NULL;
1613 struct tog_log_view_state *s = &view->state.log;
1614 struct commit_queue_entry *entry = s->selected_entry;
1615 const int limit = view->nlines;
1617 int ncommits, author_cols = 4;
1618 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1619 char *refs_str = NULL;
1621 struct tog_color *tc;
1622 static const size_t date_display_cols = 12;
1624 if (s->selected_entry &&
1625 !(view->searching && view->search_next_done == 0)) {
1626 struct got_reflist_head *refs;
1627 err = got_object_id_str(&id_str, s->selected_entry->id);
1630 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1631 s->selected_entry->id);
1633 err = build_refs_str(&refs_str, refs,
1634 s->selected_entry->id, s->repo);
1640 if (s->thread_args.commits_needed == 0)
1641 halfdelay(10); /* disable fast refresh */
1643 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1644 if (asprintf(&ncommits_str, " [%d/%d] %s",
1645 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1646 (view->searching && !view->search_next_done) ?
1647 "searching..." : "loading...") == -1) {
1648 err = got_error_from_errno("asprintf");
1652 const char *search_str = NULL;
1654 if (view->searching) {
1655 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1656 search_str = "no more matches";
1657 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1658 search_str = "no matches found";
1659 else if (!view->search_next_done)
1660 search_str = "searching...";
1663 if (asprintf(&ncommits_str, " [%d/%d] %s",
1664 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1665 search_str ? search_str :
1666 (refs_str ? refs_str : "")) == -1) {
1667 err = got_error_from_errno("asprintf");
1672 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1673 if (asprintf(&header, "commit %s %s%s",
1674 id_str ? id_str : "........................................",
1675 s->in_repo_path, ncommits_str) == -1) {
1676 err = got_error_from_errno("asprintf");
1680 } else if (asprintf(&header, "commit %s%s",
1681 id_str ? id_str : "........................................",
1682 ncommits_str) == -1) {
1683 err = got_error_from_errno("asprintf");
1687 err = format_line(&wline, &width, header, view->ncols, 0);
1691 werase(view->window);
1693 if (view_needs_focus_indication(view))
1694 wstandout(view->window);
1695 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1697 wattr_on(view->window,
1698 COLOR_PAIR(tc->colorpair), NULL);
1699 waddwstr(view->window, wline);
1701 wattr_off(view->window,
1702 COLOR_PAIR(tc->colorpair), NULL);
1703 while (width < view->ncols) {
1704 waddch(view->window, ' ');
1707 if (view_needs_focus_indication(view))
1708 wstandend(view->window);
1713 /* Grow author column size if necessary. */
1714 entry = s->first_displayed_entry;
1720 if (ncommits >= limit - 1)
1722 author = strdup(got_object_commit_get_author(entry->commit));
1723 if (author == NULL) {
1724 err = got_error_from_errno("strdup");
1727 err = format_author(&wauthor, &width, author, COLS,
1729 if (author_cols < width)
1730 author_cols = width;
1734 entry = TAILQ_NEXT(entry, entry);
1737 entry = s->first_displayed_entry;
1738 s->last_displayed_entry = s->first_displayed_entry;
1741 if (ncommits >= limit - 1)
1743 if (ncommits == s->selected)
1744 wstandout(view->window);
1745 err = draw_commit(view, entry->commit, entry->id,
1746 date_display_cols, author_cols);
1747 if (ncommits == s->selected)
1748 wstandend(view->window);
1752 s->last_displayed_entry = entry;
1753 entry = TAILQ_NEXT(entry, entry);
1766 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1768 struct commit_queue_entry *entry;
1771 entry = TAILQ_FIRST(&s->commits.head);
1772 if (s->first_displayed_entry == entry)
1775 entry = s->first_displayed_entry;
1776 while (entry && nscrolled < maxscroll) {
1777 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1779 s->first_displayed_entry = entry;
1785 static const struct got_error *
1786 trigger_log_thread(struct tog_view *view, int wait)
1788 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1791 halfdelay(1); /* fast refresh while loading commits */
1793 while (ta->commits_needed > 0 || ta->load_all) {
1794 if (ta->log_complete)
1797 /* Wake the log thread. */
1798 errcode = pthread_cond_signal(&ta->need_commits);
1800 return got_error_set_errno(errcode,
1801 "pthread_cond_signal");
1804 * The mutex will be released while the view loop waits
1805 * in wgetch(), at which time the log thread will run.
1810 /* Display progress update in log view. */
1811 show_log_view(view);
1815 /* Wait right here while next commit is being loaded. */
1816 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1818 return got_error_set_errno(errcode,
1819 "pthread_cond_wait");
1821 /* Display progress update in log view. */
1822 show_log_view(view);
1830 static const struct got_error *
1831 log_scroll_down(struct tog_view *view, int maxscroll)
1833 struct tog_log_view_state *s = &view->state.log;
1834 const struct got_error *err = NULL;
1835 struct commit_queue_entry *pentry;
1836 int nscrolled = 0, ncommits_needed;
1838 if (s->last_displayed_entry == NULL)
1841 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1842 if (s->commits.ncommits < ncommits_needed &&
1843 !s->thread_args.log_complete) {
1845 * Ask the log thread for required amount of commits.
1847 s->thread_args.commits_needed += maxscroll;
1848 err = trigger_log_thread(view, 1);
1854 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1858 s->last_displayed_entry = pentry;
1860 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1863 s->first_displayed_entry = pentry;
1864 } while (++nscrolled < maxscroll);
1869 static const struct got_error *
1870 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1871 struct got_commit_object *commit, struct got_object_id *commit_id,
1872 struct tog_view *log_view, struct got_repository *repo)
1874 const struct got_error *err;
1875 struct got_object_qid *parent_id;
1876 struct tog_view *diff_view;
1878 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1879 if (diff_view == NULL)
1880 return got_error_from_errno("view_open");
1882 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1883 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1884 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1886 *new_view = diff_view;
1890 static const struct got_error *
1891 tree_view_visit_subtree(struct tog_tree_view_state *s,
1892 struct got_tree_object *subtree)
1894 struct tog_parent_tree *parent;
1896 parent = calloc(1, sizeof(*parent));
1898 return got_error_from_errno("calloc");
1900 parent->tree = s->tree;
1901 parent->first_displayed_entry = s->first_displayed_entry;
1902 parent->selected_entry = s->selected_entry;
1903 parent->selected = s->selected;
1904 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1907 s->first_displayed_entry = NULL;
1911 static const struct got_error *
1912 tree_view_walk_path(struct tog_tree_view_state *s,
1913 struct got_object_id *commit_id, const char *path)
1915 const struct got_error *err = NULL;
1916 struct got_tree_object *tree = NULL;
1918 char *slash, *subpath = NULL;
1920 /* Walk the path and open corresponding tree objects. */
1923 struct got_tree_entry *te;
1924 struct got_object_id *tree_id;
1930 /* Ensure the correct subtree entry is selected. */
1931 slash = strchr(p, '/');
1933 te_name = strdup(p);
1935 te_name = strndup(p, slash - p);
1936 if (te_name == NULL) {
1937 err = got_error_from_errno("strndup");
1940 te = got_object_tree_find_entry(s->tree, te_name);
1942 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1947 s->first_displayed_entry = s->selected_entry = te;
1949 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1950 break; /* jump to this file's entry */
1952 slash = strchr(p, '/');
1954 subpath = strndup(path, slash - path);
1956 subpath = strdup(path);
1957 if (subpath == NULL) {
1958 err = got_error_from_errno("strdup");
1962 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1967 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1972 err = tree_view_visit_subtree(s, tree);
1974 got_object_tree_close(tree);
1988 static const struct got_error *
1989 browse_commit_tree(struct tog_view **new_view, int begin_x,
1990 struct commit_queue_entry *entry, const char *path,
1991 const char *head_ref_name, struct got_repository *repo)
1993 const struct got_error *err = NULL;
1994 struct tog_tree_view_state *s;
1995 struct tog_view *tree_view;
1997 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1998 if (tree_view == NULL)
1999 return got_error_from_errno("view_open");
2001 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2004 s = &tree_view->state.tree;
2006 *new_view = tree_view;
2008 if (got_path_is_root_dir(path))
2011 return tree_view_walk_path(s, entry->id, path);
2014 static const struct got_error *
2015 block_signals_used_by_main_thread(void)
2020 if (sigemptyset(&sigset) == -1)
2021 return got_error_from_errno("sigemptyset");
2023 /* tog handles SIGWINCH and SIGCONT */
2024 if (sigaddset(&sigset, SIGWINCH) == -1)
2025 return got_error_from_errno("sigaddset");
2026 if (sigaddset(&sigset, SIGCONT) == -1)
2027 return got_error_from_errno("sigaddset");
2029 /* ncurses handles SIGTSTP */
2030 if (sigaddset(&sigset, SIGTSTP) == -1)
2031 return got_error_from_errno("sigaddset");
2033 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2035 return got_error_set_errno(errcode, "pthread_sigmask");
2041 log_thread(void *arg)
2043 const struct got_error *err = NULL;
2045 struct tog_log_thread_args *a = arg;
2048 err = block_signals_used_by_main_thread();
2052 while (!done && !err && !tog_sigpipe_received) {
2053 err = queue_commits(a);
2055 if (err->code != GOT_ERR_ITER_COMPLETED)
2059 } else if (a->commits_needed > 0 && !a->load_all)
2060 a->commits_needed--;
2062 errcode = pthread_mutex_lock(&tog_mutex);
2064 err = got_error_set_errno(errcode,
2065 "pthread_mutex_lock");
2067 } else if (*a->quit)
2069 else if (*a->first_displayed_entry == NULL) {
2070 *a->first_displayed_entry =
2071 TAILQ_FIRST(&a->commits->head);
2072 *a->selected_entry = *a->first_displayed_entry;
2075 errcode = pthread_cond_signal(&a->commit_loaded);
2077 err = got_error_set_errno(errcode,
2078 "pthread_cond_signal");
2079 pthread_mutex_unlock(&tog_mutex);
2084 a->commits_needed = 0;
2086 if (a->commits_needed == 0 && !a->load_all) {
2087 errcode = pthread_cond_wait(&a->need_commits,
2090 err = got_error_set_errno(errcode,
2091 "pthread_cond_wait");
2097 errcode = pthread_mutex_unlock(&tog_mutex);
2098 if (errcode && err == NULL)
2099 err = got_error_set_errno(errcode,
2100 "pthread_mutex_unlock");
2102 a->log_complete = 1;
2106 static const struct got_error *
2107 stop_log_thread(struct tog_log_view_state *s)
2109 const struct got_error *err = NULL;
2114 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2116 return got_error_set_errno(errcode,
2117 "pthread_cond_signal");
2118 errcode = pthread_mutex_unlock(&tog_mutex);
2120 return got_error_set_errno(errcode,
2121 "pthread_mutex_unlock");
2122 errcode = pthread_join(s->thread, (void **)&err);
2124 return got_error_set_errno(errcode, "pthread_join");
2125 errcode = pthread_mutex_lock(&tog_mutex);
2127 return got_error_set_errno(errcode,
2128 "pthread_mutex_lock");
2132 if (s->thread_args.repo) {
2133 err = got_repo_close(s->thread_args.repo);
2134 s->thread_args.repo = NULL;
2137 if (s->thread_args.graph) {
2138 got_commit_graph_close(s->thread_args.graph);
2139 s->thread_args.graph = NULL;
2145 static const struct got_error *
2146 close_log_view(struct tog_view *view)
2148 const struct got_error *err = NULL;
2149 struct tog_log_view_state *s = &view->state.log;
2152 err = stop_log_thread(s);
2154 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2155 if (errcode && err == NULL)
2156 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2158 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2159 if (errcode && err == NULL)
2160 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2162 free_commits(&s->commits);
2163 free(s->in_repo_path);
2164 s->in_repo_path = NULL;
2167 free(s->head_ref_name);
2168 s->head_ref_name = NULL;
2172 static const struct got_error *
2173 search_start_log_view(struct tog_view *view)
2175 struct tog_log_view_state *s = &view->state.log;
2177 s->matched_entry = NULL;
2178 s->search_entry = NULL;
2182 static const struct got_error *
2183 search_next_log_view(struct tog_view *view)
2185 const struct got_error *err = NULL;
2186 struct tog_log_view_state *s = &view->state.log;
2187 struct commit_queue_entry *entry;
2189 /* Display progress update in log view. */
2190 show_log_view(view);
2194 if (s->search_entry) {
2196 errcode = pthread_mutex_unlock(&tog_mutex);
2198 return got_error_set_errno(errcode,
2199 "pthread_mutex_unlock");
2200 ch = wgetch(view->window);
2201 errcode = pthread_mutex_lock(&tog_mutex);
2203 return got_error_set_errno(errcode,
2204 "pthread_mutex_lock");
2205 if (ch == KEY_BACKSPACE) {
2206 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2209 if (view->searching == TOG_SEARCH_FORWARD)
2210 entry = TAILQ_NEXT(s->search_entry, entry);
2212 entry = TAILQ_PREV(s->search_entry,
2213 commit_queue_head, entry);
2214 } else if (s->matched_entry) {
2215 if (view->searching == TOG_SEARCH_FORWARD)
2216 entry = TAILQ_NEXT(s->matched_entry, entry);
2218 entry = TAILQ_PREV(s->matched_entry,
2219 commit_queue_head, entry);
2221 entry = s->selected_entry;
2227 if (entry == NULL) {
2228 if (s->thread_args.log_complete ||
2229 view->searching == TOG_SEARCH_BACKWARD) {
2230 view->search_next_done =
2231 (s->matched_entry == NULL ?
2232 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2233 s->search_entry = NULL;
2237 * Poke the log thread for more commits and return,
2238 * allowing the main loop to make progress. Search
2239 * will resume at s->search_entry once we come back.
2241 s->thread_args.commits_needed++;
2242 return trigger_log_thread(view, 0);
2245 err = match_commit(&have_match, entry->id, entry->commit,
2250 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2251 s->matched_entry = entry;
2255 s->search_entry = entry;
2256 if (view->searching == TOG_SEARCH_FORWARD)
2257 entry = TAILQ_NEXT(entry, entry);
2259 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2262 if (s->matched_entry) {
2263 int cur = s->selected_entry->idx;
2264 while (cur < s->matched_entry->idx) {
2265 err = input_log_view(NULL, view, KEY_DOWN);
2270 while (cur > s->matched_entry->idx) {
2271 err = input_log_view(NULL, view, KEY_UP);
2278 s->search_entry = NULL;
2283 static const struct got_error *
2284 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2285 struct got_repository *repo, const char *head_ref_name,
2286 const char *in_repo_path, int log_branches)
2288 const struct got_error *err = NULL;
2289 struct tog_log_view_state *s = &view->state.log;
2290 struct got_repository *thread_repo = NULL;
2291 struct got_commit_graph *thread_graph = NULL;
2294 if (in_repo_path != s->in_repo_path) {
2295 free(s->in_repo_path);
2296 s->in_repo_path = strdup(in_repo_path);
2297 if (s->in_repo_path == NULL)
2298 return got_error_from_errno("strdup");
2301 /* The commit queue only contains commits being displayed. */
2302 TAILQ_INIT(&s->commits.head);
2303 s->commits.ncommits = 0;
2306 if (head_ref_name) {
2307 s->head_ref_name = strdup(head_ref_name);
2308 if (s->head_ref_name == NULL) {
2309 err = got_error_from_errno("strdup");
2313 s->start_id = got_object_id_dup(start_id);
2314 if (s->start_id == NULL) {
2315 err = got_error_from_errno("got_object_id_dup");
2318 s->log_branches = log_branches;
2320 STAILQ_INIT(&s->colors);
2321 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2322 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2323 get_color_value("TOG_COLOR_COMMIT"));
2326 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2327 get_color_value("TOG_COLOR_AUTHOR"));
2329 free_colors(&s->colors);
2332 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2333 get_color_value("TOG_COLOR_DATE"));
2335 free_colors(&s->colors);
2340 view->show = show_log_view;
2341 view->input = input_log_view;
2342 view->close = close_log_view;
2343 view->search_start = search_start_log_view;
2344 view->search_next = search_next_log_view;
2346 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2349 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2353 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2354 s->repo, NULL, NULL);
2358 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2360 err = got_error_set_errno(errcode, "pthread_cond_init");
2363 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2365 err = got_error_set_errno(errcode, "pthread_cond_init");
2369 s->thread_args.commits_needed = view->nlines;
2370 s->thread_args.graph = thread_graph;
2371 s->thread_args.commits = &s->commits;
2372 s->thread_args.in_repo_path = s->in_repo_path;
2373 s->thread_args.start_id = s->start_id;
2374 s->thread_args.repo = thread_repo;
2375 s->thread_args.log_complete = 0;
2376 s->thread_args.quit = &s->quit;
2377 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2378 s->thread_args.selected_entry = &s->selected_entry;
2379 s->thread_args.searching = &view->searching;
2380 s->thread_args.search_next_done = &view->search_next_done;
2381 s->thread_args.regex = &view->regex;
2384 close_log_view(view);
2388 static const struct got_error *
2389 show_log_view(struct tog_view *view)
2391 const struct got_error *err;
2392 struct tog_log_view_state *s = &view->state.log;
2394 if (s->thread == NULL) {
2395 int errcode = pthread_create(&s->thread, NULL, log_thread,
2398 return got_error_set_errno(errcode, "pthread_create");
2399 if (s->thread_args.commits_needed > 0) {
2400 err = trigger_log_thread(view, 1);
2406 return draw_commits(view);
2409 static const struct got_error *
2410 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2412 const struct got_error *err = NULL;
2413 struct tog_log_view_state *s = &view->state.log;
2414 struct tog_view *diff_view = NULL, *tree_view = NULL;
2415 struct tog_view *ref_view = NULL;
2416 struct commit_queue_entry *entry;
2419 if (s->thread_args.load_all) {
2420 if (ch == KEY_BACKSPACE)
2421 s->thread_args.load_all = 0;
2422 else if (s->thread_args.log_complete) {
2423 s->thread_args.load_all = 0;
2424 log_scroll_down(view, s->commits.ncommits);
2425 s->selected = MIN(view->nlines - 2,
2426 s->commits.ncommits - 1);
2441 if (s->first_displayed_entry == NULL)
2443 if (s->selected > 0)
2446 log_scroll_up(s, 1);
2452 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2457 if (s->first_displayed_entry == NULL)
2459 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2462 log_scroll_up(s, view->nlines - 1);
2470 if (s->first_displayed_entry == NULL)
2472 if (s->selected < MIN(view->nlines - 2,
2473 s->commits.ncommits - 1))
2476 err = log_scroll_down(view, 1);
2484 /* We don't know yet how many commits, so we're forced to
2485 * traverse them all. */
2486 if (!s->thread_args.log_complete) {
2487 s->thread_args.load_all = 1;
2488 return trigger_log_thread(view, 0);
2492 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2493 for (n = 0; n < view->nlines - 1; n++) {
2496 s->first_displayed_entry = entry;
2497 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2500 s->selected = n - 1;
2506 struct commit_queue_entry *first;
2507 first = s->first_displayed_entry;
2510 err = log_scroll_down(view, view->nlines - 1);
2513 if (first == s->first_displayed_entry &&
2514 s->selected < MIN(view->nlines - 2,
2515 s->commits.ncommits - 1)) {
2516 /* can't scroll further down */
2517 s->selected = MIN(view->nlines - 2,
2518 s->commits.ncommits - 1);
2524 if (s->selected > view->nlines - 2)
2525 s->selected = view->nlines - 2;
2526 if (s->selected > s->commits.ncommits - 1)
2527 s->selected = s->commits.ncommits - 1;
2529 if (s->commits.ncommits < view->nlines - 1 &&
2530 !s->thread_args.log_complete) {
2531 s->thread_args.commits_needed += (view->nlines - 1) -
2532 s->commits.ncommits;
2533 err = trigger_log_thread(view, 1);
2539 if (s->selected_entry == NULL)
2541 if (view_is_parent_view(view))
2542 begin_x = view_split_begin_x(view->begin_x);
2543 err = open_diff_view_for_commit(&diff_view, begin_x,
2544 s->selected_entry->commit, s->selected_entry->id,
2549 diff_view->focussed = 1;
2550 if (view_is_parent_view(view)) {
2551 err = view_close_child(view);
2554 view_set_child(view, diff_view);
2555 view->focus_child = 1;
2557 *new_view = diff_view;
2560 if (s->selected_entry == NULL)
2562 if (view_is_parent_view(view))
2563 begin_x = view_split_begin_x(view->begin_x);
2564 err = browse_commit_tree(&tree_view, begin_x,
2565 s->selected_entry, s->in_repo_path, s->head_ref_name,
2570 tree_view->focussed = 1;
2571 if (view_is_parent_view(view)) {
2572 err = view_close_child(view);
2575 view_set_child(view, tree_view);
2576 view->focus_child = 1;
2578 *new_view = tree_view;
2583 if (ch == KEY_BACKSPACE &&
2584 got_path_is_root_dir(s->in_repo_path))
2586 err = stop_log_thread(s);
2589 if (ch == KEY_BACKSPACE) {
2591 err = got_path_dirname(&parent_path, s->in_repo_path);
2594 free(s->in_repo_path);
2595 s->in_repo_path = parent_path;
2596 s->thread_args.in_repo_path = s->in_repo_path;
2597 } else if (ch == CTRL('l')) {
2598 struct got_object_id *start_id;
2599 err = got_repo_match_object_id(&start_id, NULL,
2600 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2601 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2605 s->start_id = start_id;
2606 s->thread_args.start_id = s->start_id;
2608 s->log_branches = !s->log_branches;
2610 err = got_repo_open(&s->thread_args.repo,
2611 got_repo_get_path(s->repo), NULL);
2615 err = tog_load_refs(s->repo, 0);
2618 err = got_commit_graph_open(&s->thread_args.graph,
2619 s->in_repo_path, !s->log_branches);
2622 err = got_commit_graph_iter_start(s->thread_args.graph,
2623 s->start_id, s->repo, NULL, NULL);
2626 free_commits(&s->commits);
2627 s->first_displayed_entry = NULL;
2628 s->last_displayed_entry = NULL;
2629 s->selected_entry = NULL;
2631 s->thread_args.log_complete = 0;
2633 s->thread_args.commits_needed = view->nlines;
2636 if (view_is_parent_view(view))
2637 begin_x = view_split_begin_x(view->begin_x);
2638 ref_view = view_open(view->nlines, view->ncols,
2639 view->begin_y, begin_x, TOG_VIEW_REF);
2640 if (ref_view == NULL)
2641 return got_error_from_errno("view_open");
2642 err = open_ref_view(ref_view, s->repo);
2644 view_close(ref_view);
2648 ref_view->focussed = 1;
2649 if (view_is_parent_view(view)) {
2650 err = view_close_child(view);
2653 view_set_child(view, ref_view);
2654 view->focus_child = 1;
2656 *new_view = ref_view;
2665 static const struct got_error *
2666 apply_unveil(const char *repo_path, const char *worktree_path)
2668 const struct got_error *error;
2671 if (unveil("gmon.out", "rwc") != 0)
2672 return got_error_from_errno2("unveil", "gmon.out");
2674 if (repo_path && unveil(repo_path, "r") != 0)
2675 return got_error_from_errno2("unveil", repo_path);
2677 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2678 return got_error_from_errno2("unveil", worktree_path);
2680 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2681 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2683 error = got_privsep_unveil_exec_helpers();
2687 if (unveil(NULL, NULL) != 0)
2688 return got_error_from_errno("unveil");
2698 halfdelay(1); /* Do fast refresh while initial view is loading. */
2701 intrflush(stdscr, FALSE);
2702 keypad(stdscr, TRUE);
2704 if (getenv("TOG_COLORS") != NULL) {
2706 use_default_colors();
2708 signal(SIGWINCH, tog_sigwinch);
2709 signal(SIGPIPE, tog_sigpipe);
2710 signal(SIGCONT, tog_sigcont);
2713 static const struct got_error *
2714 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2715 struct got_repository *repo, struct got_worktree *worktree)
2717 const struct got_error *err = NULL;
2720 *in_repo_path = strdup("/");
2721 if (*in_repo_path == NULL)
2722 return got_error_from_errno("strdup");
2727 const char *prefix = got_worktree_get_path_prefix(worktree);
2730 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2733 if (asprintf(in_repo_path, "%s%s%s", prefix,
2734 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2736 err = got_error_from_errno("asprintf");
2737 *in_repo_path = NULL;
2741 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2746 static const struct got_error *
2747 cmd_log(int argc, char *argv[])
2749 const struct got_error *error;
2750 struct got_repository *repo = NULL;
2751 struct got_worktree *worktree = NULL;
2752 struct got_object_id *start_id = NULL;
2753 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2754 char *start_commit = NULL, *label = NULL;
2755 struct got_reference *ref = NULL;
2756 const char *head_ref_name = NULL;
2757 int ch, log_branches = 0;
2758 struct tog_view *view;
2760 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2766 start_commit = optarg;
2769 repo_path = realpath(optarg, NULL);
2770 if (repo_path == NULL)
2771 return got_error_from_errno2("realpath",
2786 if (repo_path == NULL) {
2787 cwd = getcwd(NULL, 0);
2789 return got_error_from_errno("getcwd");
2790 error = got_worktree_open(&worktree, cwd);
2791 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2795 strdup(got_worktree_get_repo_path(worktree));
2797 repo_path = strdup(cwd);
2798 if (repo_path == NULL) {
2799 error = got_error_from_errno("strdup");
2804 error = got_repo_open(&repo, repo_path, NULL);
2808 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2815 error = apply_unveil(got_repo_get_path(repo),
2816 worktree ? got_worktree_get_root_path(worktree) : NULL);
2820 /* already loaded by tog_log_with_path()? */
2821 if (TAILQ_EMPTY(&tog_refs)) {
2822 error = tog_load_refs(repo, 0);
2827 if (start_commit == NULL) {
2828 error = got_repo_match_object_id(&start_id, &label,
2829 worktree ? got_worktree_get_head_ref_name(worktree) :
2830 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2833 head_ref_name = label;
2835 error = got_ref_open(&ref, repo, start_commit, 0);
2837 head_ref_name = got_ref_get_name(ref);
2838 else if (error->code != GOT_ERR_NOT_REF)
2840 error = got_repo_match_object_id(&start_id, NULL,
2841 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2846 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2848 error = got_error_from_errno("view_open");
2851 error = open_log_view(view, start_id, repo, head_ref_name,
2852 in_repo_path, log_branches);
2856 /* Release work tree lock. */
2857 got_worktree_close(worktree);
2860 error = view_loop(view);
2870 const struct got_error *close_err = got_repo_close(repo);
2875 got_worktree_close(worktree);
2884 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2885 "[-w] object1 object2\n", getprogname());
2890 match_line(const char *line, regex_t *regex, size_t nmatch,
2891 regmatch_t *regmatch)
2893 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2897 match_color(struct tog_colors *colors, const char *line)
2899 struct tog_color *tc = NULL;
2901 STAILQ_FOREACH(tc, colors, entry) {
2902 if (match_line(line, &tc->regex, 0, NULL))
2909 static const struct got_error *
2910 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2911 WINDOW *window, regmatch_t *regmatch)
2913 const struct got_error *err = NULL;
2920 s = strndup(line, regmatch->rm_so);
2922 return got_error_from_errno("strndup");
2924 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2929 waddwstr(window, wline);
2936 s = strndup(line + regmatch->rm_so,
2937 regmatch->rm_eo - regmatch->rm_so);
2939 err = got_error_from_errno("strndup");
2943 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2948 wattr_on(window, A_STANDOUT, NULL);
2949 waddwstr(window, wline);
2950 wattr_off(window, A_STANDOUT, NULL);
2957 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2958 err = format_line(&wline, &width,
2959 line + regmatch->rm_eo, wlimit, col_tab_align);
2962 waddwstr(window, wline);
2970 static const struct got_error *
2971 draw_file(struct tog_view *view, const char *header)
2973 struct tog_diff_view_state *s = &view->state.diff;
2974 regmatch_t *regmatch = &view->regmatch;
2975 const struct got_error *err;
2978 size_t linesize = 0;
2980 struct tog_color *tc;
2983 int max_lines = view->nlines;
2984 int nlines = s->nlines;
2987 line_offset = s->line_offsets[s->first_displayed_line - 1];
2988 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2989 return got_error_from_errno("fseek");
2991 werase(view->window);
2994 if (asprintf(&line, "[%d/%d] %s",
2995 s->first_displayed_line - 1 + s->selected_line, nlines,
2997 return got_error_from_errno("asprintf");
2998 err = format_line(&wline, &width, line, view->ncols, 0);
3003 if (view_needs_focus_indication(view))
3004 wstandout(view->window);
3005 waddwstr(view->window, wline);
3008 if (view_needs_focus_indication(view))
3009 wstandend(view->window);
3010 if (width <= view->ncols - 1)
3011 waddch(view->window, '\n');
3020 while (max_lines > 0 && nprinted < max_lines) {
3021 linelen = getline(&line, &linesize, s->f);
3022 if (linelen == -1) {
3028 return got_ferror(s->f, GOT_ERR_IO);
3031 tc = match_color(&s->colors, line);
3033 wattr_on(view->window,
3034 COLOR_PAIR(tc->colorpair), NULL);
3035 if (s->first_displayed_line + nprinted == s->matched_line &&
3036 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3037 err = add_matched_line(&width, line, view->ncols, 0,
3038 view->window, regmatch);
3044 err = format_line(&wline, &width, line, view->ncols, 0);
3049 waddwstr(view->window, wline);
3054 wattr_off(view->window,
3055 COLOR_PAIR(tc->colorpair), NULL);
3056 if (width <= view->ncols - 1)
3057 waddch(view->window, '\n');
3062 s->last_displayed_line = s->first_displayed_line +
3065 s->last_displayed_line = s->first_displayed_line;
3070 while (nprinted < view->nlines) {
3071 waddch(view->window, '\n');
3075 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3080 wstandout(view->window);
3081 waddwstr(view->window, wline);
3084 wstandend(view->window);
3091 get_datestr(time_t *time, char *datebuf)
3093 struct tm mytm, *tm;
3096 tm = gmtime_r(time, &mytm);
3099 s = asctime_r(tm, datebuf);
3102 p = strchr(s, '\n');
3108 static const struct got_error *
3109 get_changed_paths(struct got_pathlist_head *paths,
3110 struct got_commit_object *commit, struct got_repository *repo)
3112 const struct got_error *err = NULL;
3113 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3114 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3115 struct got_object_qid *qid;
3117 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3119 struct got_commit_object *pcommit;
3120 err = got_object_open_as_commit(&pcommit, repo,
3125 tree_id1 = got_object_id_dup(
3126 got_object_commit_get_tree_id(pcommit));
3127 if (tree_id1 == NULL) {
3128 got_object_commit_close(pcommit);
3129 return got_error_from_errno("got_object_id_dup");
3131 got_object_commit_close(pcommit);
3136 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3141 tree_id2 = got_object_commit_get_tree_id(commit);
3142 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3146 err = got_diff_tree(tree1, tree2, "", "", repo,
3147 got_diff_tree_collect_changed_paths, paths, 0);
3150 got_object_tree_close(tree1);
3152 got_object_tree_close(tree2);
3157 static const struct got_error *
3158 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3162 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3164 return got_error_from_errno("reallocarray");
3166 (*line_offsets)[*nlines] = off;
3171 static const struct got_error *
3172 write_commit_info(off_t **line_offsets, size_t *nlines,
3173 struct got_object_id *commit_id, struct got_reflist_head *refs,
3174 struct got_repository *repo, FILE *outfile)
3176 const struct got_error *err = NULL;
3177 char datebuf[26], *datestr;
3178 struct got_commit_object *commit;
3179 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3180 time_t committer_time;
3181 const char *author, *committer;
3182 char *refs_str = NULL;
3183 struct got_pathlist_head changed_paths;
3184 struct got_pathlist_entry *pe;
3188 TAILQ_INIT(&changed_paths);
3191 err = build_refs_str(&refs_str, refs, commit_id, repo);
3196 err = got_object_open_as_commit(&commit, repo, commit_id);
3200 err = got_object_id_str(&id_str, commit_id);
3202 err = got_error_from_errno("got_object_id_str");
3206 err = add_line_offset(line_offsets, nlines, 0);
3210 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3211 refs_str ? refs_str : "", refs_str ? ")" : "");
3213 err = got_error_from_errno("fprintf");
3217 err = add_line_offset(line_offsets, nlines, outoff);
3221 n = fprintf(outfile, "from: %s\n",
3222 got_object_commit_get_author(commit));
3224 err = got_error_from_errno("fprintf");
3228 err = add_line_offset(line_offsets, nlines, outoff);
3232 committer_time = got_object_commit_get_committer_time(commit);
3233 datestr = get_datestr(&committer_time, datebuf);
3235 n = fprintf(outfile, "date: %s UTC\n", datestr);
3237 err = got_error_from_errno("fprintf");
3241 err = add_line_offset(line_offsets, nlines, outoff);
3245 author = got_object_commit_get_author(commit);
3246 committer = got_object_commit_get_committer(commit);
3247 if (strcmp(author, committer) != 0) {
3248 n = fprintf(outfile, "via: %s\n", committer);
3250 err = got_error_from_errno("fprintf");
3254 err = add_line_offset(line_offsets, nlines, outoff);
3258 if (got_object_commit_get_nparents(commit) > 1) {
3259 const struct got_object_id_queue *parent_ids;
3260 struct got_object_qid *qid;
3262 parent_ids = got_object_commit_get_parent_ids(commit);
3263 STAILQ_FOREACH(qid, parent_ids, entry) {
3264 err = got_object_id_str(&id_str, qid->id);
3267 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3269 err = got_error_from_errno("fprintf");
3273 err = add_line_offset(line_offsets, nlines, outoff);
3281 err = got_object_commit_get_logmsg(&logmsg, commit);
3285 while ((line = strsep(&s, "\n")) != NULL) {
3286 n = fprintf(outfile, "%s\n", line);
3288 err = got_error_from_errno("fprintf");
3292 err = add_line_offset(line_offsets, nlines, outoff);
3297 err = get_changed_paths(&changed_paths, commit, repo);
3300 TAILQ_FOREACH(pe, &changed_paths, entry) {
3301 struct got_diff_changed_path *cp = pe->data;
3302 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3304 err = got_error_from_errno("fprintf");
3308 err = add_line_offset(line_offsets, nlines, outoff);
3311 free((char *)pe->path);
3315 fputc('\n', outfile);
3317 err = add_line_offset(line_offsets, nlines, outoff);
3319 got_pathlist_free(&changed_paths);
3323 got_object_commit_close(commit);
3325 free(*line_offsets);
3326 *line_offsets = NULL;
3332 static const struct got_error *
3333 create_diff(struct tog_diff_view_state *s)
3335 const struct got_error *err = NULL;
3339 free(s->line_offsets);
3340 s->line_offsets = malloc(sizeof(off_t));
3341 if (s->line_offsets == NULL)
3342 return got_error_from_errno("malloc");
3347 err = got_error_from_errno("got_opentemp");
3350 if (s->f && fclose(s->f) == EOF) {
3351 err = got_error_from_errno("fclose");
3357 err = got_object_get_type(&obj_type, s->repo, s->id1);
3359 err = got_object_get_type(&obj_type, s->repo, s->id2);
3364 case GOT_OBJ_TYPE_BLOB:
3365 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3366 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3367 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3369 case GOT_OBJ_TYPE_TREE:
3370 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3371 s->id1, s->id2, NULL, "", "", s->diff_context,
3372 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3374 case GOT_OBJ_TYPE_COMMIT: {
3375 const struct got_object_id_queue *parent_ids;
3376 struct got_object_qid *pid;
3377 struct got_commit_object *commit2;
3378 struct got_reflist_head *refs;
3380 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3383 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3384 /* Show commit info if we're diffing to a parent/root commit. */
3385 if (s->id1 == NULL) {
3386 err = write_commit_info(&s->line_offsets, &s->nlines,
3387 s->id2, refs, s->repo, s->f);
3391 parent_ids = got_object_commit_get_parent_ids(commit2);
3392 STAILQ_FOREACH(pid, parent_ids, entry) {
3393 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3394 err = write_commit_info(
3395 &s->line_offsets, &s->nlines,
3396 s->id2, refs, s->repo, s->f);
3403 got_object_commit_close(commit2);
3405 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3406 s->id1, s->id2, NULL, s->diff_context, s->ignore_whitespace,
3407 s->force_text_diff, s->repo, s->f);
3411 err = got_error(GOT_ERR_OBJ_TYPE);
3417 if (s->f && fflush(s->f) != 0 && err == NULL)
3418 err = got_error_from_errno("fflush");
3423 diff_view_indicate_progress(struct tog_view *view)
3425 mvwaddstr(view->window, 0, 0, "diffing...");
3430 static const struct got_error *
3431 search_start_diff_view(struct tog_view *view)
3433 struct tog_diff_view_state *s = &view->state.diff;
3435 s->matched_line = 0;
3439 static const struct got_error *
3440 search_next_diff_view(struct tog_view *view)
3442 struct tog_diff_view_state *s = &view->state.diff;
3445 size_t linesize = 0;
3448 if (!view->searching) {
3449 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3453 if (s->matched_line) {
3454 if (view->searching == TOG_SEARCH_FORWARD)
3455 lineno = s->matched_line + 1;
3457 lineno = s->matched_line - 1;
3459 lineno = s->first_displayed_line;
3464 if (lineno <= 0 || lineno > s->nlines) {
3465 if (s->matched_line == 0) {
3466 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3470 if (view->searching == TOG_SEARCH_FORWARD)
3476 offset = s->line_offsets[lineno - 1];
3477 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3479 return got_error_from_errno("fseeko");
3481 linelen = getline(&line, &linesize, s->f);
3482 if (linelen != -1 &&
3483 match_line(line, &view->regex, 1, &view->regmatch)) {
3484 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3485 s->matched_line = lineno;
3488 if (view->searching == TOG_SEARCH_FORWARD)
3495 if (s->matched_line) {
3496 s->first_displayed_line = s->matched_line;
3497 s->selected_line = 1;
3503 static const struct got_error *
3504 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3505 struct got_object_id *id2, const char *label1, const char *label2,
3506 int diff_context, int ignore_whitespace, int force_text_diff,
3507 struct tog_view *log_view, struct got_repository *repo)
3509 const struct got_error *err;
3510 struct tog_diff_view_state *s = &view->state.diff;
3512 if (id1 != NULL && id2 != NULL) {
3514 err = got_object_get_type(&type1, repo, id1);
3517 err = got_object_get_type(&type2, repo, id2);
3522 return got_error(GOT_ERR_OBJ_TYPE);
3524 s->first_displayed_line = 1;
3525 s->last_displayed_line = view->nlines;
3526 s->selected_line = 1;
3534 s->id1 = got_object_id_dup(id1);
3536 return got_error_from_errno("got_object_id_dup");
3540 s->id2 = got_object_id_dup(id2);
3541 if (s->id2 == NULL) {
3544 return got_error_from_errno("got_object_id_dup");
3547 s->first_displayed_line = 1;
3548 s->last_displayed_line = view->nlines;
3549 s->diff_context = diff_context;
3550 s->ignore_whitespace = ignore_whitespace;
3551 s->force_text_diff = force_text_diff;
3552 s->log_view = log_view;
3555 STAILQ_INIT(&s->colors);
3556 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3557 err = add_color(&s->colors,
3558 "^-", TOG_COLOR_DIFF_MINUS,
3559 get_color_value("TOG_COLOR_DIFF_MINUS"));
3562 err = add_color(&s->colors, "^\\+",
3563 TOG_COLOR_DIFF_PLUS,
3564 get_color_value("TOG_COLOR_DIFF_PLUS"));
3566 free_colors(&s->colors);
3569 err = add_color(&s->colors,
3570 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3571 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3573 free_colors(&s->colors);
3577 err = add_color(&s->colors,
3578 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3579 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3580 get_color_value("TOG_COLOR_DIFF_META"));
3582 free_colors(&s->colors);
3586 err = add_color(&s->colors,
3587 "^(from|via): ", TOG_COLOR_AUTHOR,
3588 get_color_value("TOG_COLOR_AUTHOR"));
3590 free_colors(&s->colors);
3594 err = add_color(&s->colors,
3595 "^date: ", TOG_COLOR_DATE,
3596 get_color_value("TOG_COLOR_DATE"));
3598 free_colors(&s->colors);
3603 if (log_view && view_is_splitscreen(view))
3604 show_log_view(log_view); /* draw vborder */
3605 diff_view_indicate_progress(view);
3607 s->line_offsets = NULL;
3609 err = create_diff(s);
3615 free_colors(&s->colors);
3619 view->show = show_diff_view;
3620 view->input = input_diff_view;
3621 view->close = close_diff_view;
3622 view->search_start = search_start_diff_view;
3623 view->search_next = search_next_diff_view;
3628 static const struct got_error *
3629 close_diff_view(struct tog_view *view)
3631 const struct got_error *err = NULL;
3632 struct tog_diff_view_state *s = &view->state.diff;
3638 if (s->f && fclose(s->f) == EOF)
3639 err = got_error_from_errno("fclose");
3640 free_colors(&s->colors);
3641 free(s->line_offsets);
3642 s->line_offsets = NULL;
3647 static const struct got_error *
3648 show_diff_view(struct tog_view *view)
3650 const struct got_error *err;
3651 struct tog_diff_view_state *s = &view->state.diff;
3652 char *id_str1 = NULL, *id_str2, *header;
3653 const char *label1, *label2;
3656 err = got_object_id_str(&id_str1, s->id1);
3659 label1 = s->label1 ? : id_str1;
3661 label1 = "/dev/null";
3663 err = got_object_id_str(&id_str2, s->id2);
3666 label2 = s->label2 ? : id_str2;
3668 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3669 err = got_error_from_errno("asprintf");
3677 err = draw_file(view, header);
3682 static const struct got_error *
3683 set_selected_commit(struct tog_diff_view_state *s,
3684 struct commit_queue_entry *entry)
3686 const struct got_error *err;
3687 const struct got_object_id_queue *parent_ids;
3688 struct got_commit_object *selected_commit;
3689 struct got_object_qid *pid;
3692 s->id2 = got_object_id_dup(entry->id);
3694 return got_error_from_errno("got_object_id_dup");
3696 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3699 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3701 pid = STAILQ_FIRST(parent_ids);
3702 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3703 got_object_commit_close(selected_commit);
3707 static const struct got_error *
3708 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3710 const struct got_error *err = NULL;
3711 struct tog_diff_view_state *s = &view->state.diff;
3712 struct tog_log_view_state *ls;
3713 struct commit_queue_entry *old_selected_entry;
3715 size_t linesize = 0;
3723 s->force_text_diff = !s->force_text_diff;
3725 s->ignore_whitespace = !s->ignore_whitespace;
3726 wclear(view->window);
3727 s->first_displayed_line = 1;
3728 s->last_displayed_line = view->nlines;
3729 s->matched_line = 0;
3730 diff_view_indicate_progress(view);
3731 err = create_diff(s);
3735 s->first_displayed_line = 1;
3742 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3748 if (s->first_displayed_line > 1)
3749 s->first_displayed_line--;
3753 if (s->first_displayed_line == 1)
3756 while (i++ < view->nlines - 1 &&
3757 s->first_displayed_line > 1)
3758 s->first_displayed_line--;
3764 s->first_displayed_line++;
3772 while (!s->eof && i++ < view->nlines - 1) {
3773 linelen = getline(&line, &linesize, s->f);
3774 s->first_displayed_line++;
3775 if (linelen == -1) {
3779 err = got_ferror(s->f, GOT_ERR_IO);
3786 if (s->diff_context > 0) {
3788 s->matched_line = 0;
3789 diff_view_indicate_progress(view);
3790 err = create_diff(s);
3791 if (s->first_displayed_line + view->nlines - 1 >
3793 s->first_displayed_line = 1;
3794 s->last_displayed_line = view->nlines;
3799 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3801 s->matched_line = 0;
3802 diff_view_indicate_progress(view);
3803 err = create_diff(s);
3808 if (s->log_view == NULL)
3810 ls = &s->log_view->state.log;
3811 old_selected_entry = ls->selected_entry;
3813 err = input_log_view(NULL, s->log_view, KEY_UP);
3817 if (old_selected_entry == ls->selected_entry)
3820 err = set_selected_commit(s, ls->selected_entry);
3824 s->first_displayed_line = 1;
3825 s->last_displayed_line = view->nlines;
3826 s->matched_line = 0;
3828 diff_view_indicate_progress(view);
3829 err = create_diff(s);
3833 if (s->log_view == NULL)
3835 ls = &s->log_view->state.log;
3836 old_selected_entry = ls->selected_entry;
3838 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3842 if (old_selected_entry == ls->selected_entry)
3845 err = set_selected_commit(s, ls->selected_entry);
3849 s->first_displayed_line = 1;
3850 s->last_displayed_line = view->nlines;
3851 s->matched_line = 0;
3853 diff_view_indicate_progress(view);
3854 err = create_diff(s);
3863 static const struct got_error *
3864 cmd_diff(int argc, char *argv[])
3866 const struct got_error *error = NULL;
3867 struct got_repository *repo = NULL;
3868 struct got_worktree *worktree = NULL;
3869 struct got_object_id *id1 = NULL, *id2 = NULL;
3870 char *repo_path = NULL, *cwd = NULL;
3871 char *id_str1 = NULL, *id_str2 = NULL;
3872 char *label1 = NULL, *label2 = NULL;
3873 int diff_context = 3, ignore_whitespace = 0;
3874 int ch, force_text_diff = 0;
3876 struct tog_view *view;
3878 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3881 force_text_diff = 1;
3884 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3887 errx(1, "number of context lines is %s: %s",
3891 repo_path = realpath(optarg, NULL);
3892 if (repo_path == NULL)
3893 return got_error_from_errno2("realpath",
3895 got_path_strip_trailing_slashes(repo_path);
3898 ignore_whitespace = 1;
3910 usage_diff(); /* TODO show local worktree changes */
3911 } else if (argc == 2) {
3917 if (repo_path == NULL) {
3918 cwd = getcwd(NULL, 0);
3920 return got_error_from_errno("getcwd");
3921 error = got_worktree_open(&worktree, cwd);
3922 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3926 strdup(got_worktree_get_repo_path(worktree));
3928 repo_path = strdup(cwd);
3929 if (repo_path == NULL) {
3930 error = got_error_from_errno("strdup");
3935 error = got_repo_open(&repo, repo_path, NULL);
3941 error = apply_unveil(got_repo_get_path(repo), NULL);
3945 error = tog_load_refs(repo, 0);
3949 error = got_repo_match_object_id(&id1, &label1, id_str1,
3950 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3954 error = got_repo_match_object_id(&id2, &label2, id_str2,
3955 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3959 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3961 error = got_error_from_errno("view_open");
3964 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3965 ignore_whitespace, force_text_diff, NULL, repo);
3968 error = view_loop(view);
3975 const struct got_error *close_err = got_repo_close(repo);
3980 got_worktree_close(worktree);
3989 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3994 struct tog_blame_line {
3996 struct got_object_id *id;
3999 static const struct got_error *
4000 draw_blame(struct tog_view *view)
4002 struct tog_blame_view_state *s = &view->state.blame;
4003 struct tog_blame *blame = &s->blame;
4004 regmatch_t *regmatch = &view->regmatch;
4005 const struct got_error *err;
4006 int lineno = 0, nprinted = 0;
4008 size_t linesize = 0;
4012 struct tog_blame_line *blame_line;
4013 struct got_object_id *prev_id = NULL;
4015 struct tog_color *tc;
4017 err = got_object_id_str(&id_str, s->blamed_commit->id);
4022 werase(view->window);
4024 if (asprintf(&line, "commit %s", id_str) == -1) {
4025 err = got_error_from_errno("asprintf");
4030 err = format_line(&wline, &width, line, view->ncols, 0);
4035 if (view_needs_focus_indication(view))
4036 wstandout(view->window);
4037 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4039 wattr_on(view->window,
4040 COLOR_PAIR(tc->colorpair), NULL);
4041 waddwstr(view->window, wline);
4043 wattr_off(view->window,
4044 COLOR_PAIR(tc->colorpair), NULL);
4045 if (view_needs_focus_indication(view))
4046 wstandend(view->window);
4049 if (width < view->ncols - 1)
4050 waddch(view->window, '\n');
4052 if (asprintf(&line, "[%d/%d] %s%s",
4053 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4054 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4056 return got_error_from_errno("asprintf");
4059 err = format_line(&wline, &width, line, view->ncols, 0);
4064 waddwstr(view->window, wline);
4067 if (width < view->ncols - 1)
4068 waddch(view->window, '\n');
4071 while (nprinted < view->nlines - 2) {
4072 linelen = getline(&line, &linesize, blame->f);
4073 if (linelen == -1) {
4074 if (feof(blame->f)) {
4079 return got_ferror(blame->f, GOT_ERR_IO);
4081 if (++lineno < s->first_displayed_line)
4084 if (view->focussed && nprinted == s->selected_line - 1)
4085 wstandout(view->window);
4087 if (blame->nlines > 0) {
4088 blame_line = &blame->lines[lineno - 1];
4089 if (blame_line->annotated && prev_id &&
4090 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4092 nprinted == s->selected_line - 1)) {
4093 waddstr(view->window, " ");
4094 } else if (blame_line->annotated) {
4096 err = got_object_id_str(&id_str, blame_line->id);
4101 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4103 wattr_on(view->window,
4104 COLOR_PAIR(tc->colorpair), NULL);
4105 wprintw(view->window, "%.8s", id_str);
4107 wattr_off(view->window,
4108 COLOR_PAIR(tc->colorpair), NULL);
4110 prev_id = blame_line->id;
4112 waddstr(view->window, "........");
4116 waddstr(view->window, "........");
4120 if (view->focussed && nprinted == s->selected_line - 1)
4121 wstandend(view->window);
4122 waddstr(view->window, " ");
4124 if (view->ncols <= 9) {
4126 wline = wcsdup(L"");
4127 if (wline == NULL) {
4128 err = got_error_from_errno("wcsdup");
4132 } else if (s->first_displayed_line + nprinted ==
4134 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4135 err = add_matched_line(&width, line, view->ncols - 9, 9,
4136 view->window, regmatch);
4143 err = format_line(&wline, &width, line,
4144 view->ncols - 9, 9);
4145 waddwstr(view->window, wline);
4151 if (width <= view->ncols - 1)
4152 waddch(view->window, '\n');
4153 if (++nprinted == 1)
4154 s->first_displayed_line = lineno;
4157 s->last_displayed_line = lineno;
4164 static const struct got_error *
4165 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4167 const struct got_error *err = NULL;
4168 struct tog_blame_cb_args *a = arg;
4169 struct tog_blame_line *line;
4172 if (nlines != a->nlines ||
4173 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4174 return got_error(GOT_ERR_RANGE);
4176 errcode = pthread_mutex_lock(&tog_mutex);
4178 return got_error_set_errno(errcode, "pthread_mutex_lock");
4180 if (*a->quit) { /* user has quit the blame view */
4181 err = got_error(GOT_ERR_ITER_COMPLETED);
4186 goto done; /* no change in this commit */
4188 line = &a->lines[lineno - 1];
4189 if (line->annotated)
4192 line->id = got_object_id_dup(id);
4193 if (line->id == NULL) {
4194 err = got_error_from_errno("got_object_id_dup");
4197 line->annotated = 1;
4199 errcode = pthread_mutex_unlock(&tog_mutex);
4201 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4206 blame_thread(void *arg)
4208 const struct got_error *err, *close_err;
4209 struct tog_blame_thread_args *ta = arg;
4210 struct tog_blame_cb_args *a = ta->cb_args;
4213 err = block_signals_used_by_main_thread();
4217 err = got_blame(ta->path, a->commit_id, ta->repo,
4218 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4219 if (err && err->code == GOT_ERR_CANCELLED)
4222 errcode = pthread_mutex_lock(&tog_mutex);
4224 return (void *)got_error_set_errno(errcode,
4225 "pthread_mutex_lock");
4227 close_err = got_repo_close(ta->repo);
4233 errcode = pthread_mutex_unlock(&tog_mutex);
4234 if (errcode && err == NULL)
4235 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4240 static struct got_object_id *
4241 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4242 int first_displayed_line, int selected_line)
4244 struct tog_blame_line *line;
4249 line = &lines[first_displayed_line - 1 + selected_line - 1];
4250 if (!line->annotated)
4256 static const struct got_error *
4257 stop_blame(struct tog_blame *blame)
4259 const struct got_error *err = NULL;
4262 if (blame->thread) {
4264 errcode = pthread_mutex_unlock(&tog_mutex);
4266 return got_error_set_errno(errcode,
4267 "pthread_mutex_unlock");
4268 errcode = pthread_join(blame->thread, (void **)&err);
4270 return got_error_set_errno(errcode, "pthread_join");
4271 errcode = pthread_mutex_lock(&tog_mutex);
4273 return got_error_set_errno(errcode,
4274 "pthread_mutex_lock");
4275 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4277 blame->thread = NULL;
4279 if (blame->thread_args.repo) {
4280 const struct got_error *close_err;
4281 close_err = got_repo_close(blame->thread_args.repo);
4284 blame->thread_args.repo = NULL;
4287 if (fclose(blame->f) == EOF && err == NULL)
4288 err = got_error_from_errno("fclose");
4292 for (i = 0; i < blame->nlines; i++)
4293 free(blame->lines[i].id);
4295 blame->lines = NULL;
4297 free(blame->cb_args.commit_id);
4298 blame->cb_args.commit_id = NULL;
4303 static const struct got_error *
4304 cancel_blame_view(void *arg)
4306 const struct got_error *err = NULL;
4310 errcode = pthread_mutex_lock(&tog_mutex);
4312 return got_error_set_errno(errcode,
4313 "pthread_mutex_unlock");
4316 err = got_error(GOT_ERR_CANCELLED);
4318 errcode = pthread_mutex_unlock(&tog_mutex);
4320 return got_error_set_errno(errcode,
4321 "pthread_mutex_lock");
4326 static const struct got_error *
4327 run_blame(struct tog_view *view)
4329 struct tog_blame_view_state *s = &view->state.blame;
4330 struct tog_blame *blame = &s->blame;
4331 const struct got_error *err = NULL;
4332 struct got_blob_object *blob = NULL;
4333 struct got_repository *thread_repo = NULL;
4334 struct got_object_id *obj_id = NULL;
4337 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4342 err = got_object_get_type(&obj_type, s->repo, obj_id);
4346 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4347 err = got_error(GOT_ERR_OBJ_TYPE);
4351 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4354 blame->f = got_opentemp();
4355 if (blame->f == NULL) {
4356 err = got_error_from_errno("got_opentemp");
4359 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4360 &blame->line_offsets, blame->f, blob);
4363 if (blame->nlines == 0) {
4364 s->blame_complete = 1;
4368 /* Don't include \n at EOF in the blame line count. */
4369 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4372 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4373 if (blame->lines == NULL) {
4374 err = got_error_from_errno("calloc");
4378 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4382 blame->cb_args.view = view;
4383 blame->cb_args.lines = blame->lines;
4384 blame->cb_args.nlines = blame->nlines;
4385 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4386 if (blame->cb_args.commit_id == NULL) {
4387 err = got_error_from_errno("got_object_id_dup");
4390 blame->cb_args.quit = &s->done;
4392 blame->thread_args.path = s->path;
4393 blame->thread_args.repo = thread_repo;
4394 blame->thread_args.cb_args = &blame->cb_args;
4395 blame->thread_args.complete = &s->blame_complete;
4396 blame->thread_args.cancel_cb = cancel_blame_view;
4397 blame->thread_args.cancel_arg = &s->done;
4398 s->blame_complete = 0;
4400 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4401 s->first_displayed_line = 1;
4402 s->last_displayed_line = view->nlines;
4403 s->selected_line = 1;
4405 s->matched_line = 0;
4409 got_object_blob_close(blob);
4416 static const struct got_error *
4417 open_blame_view(struct tog_view *view, char *path,
4418 struct got_object_id *commit_id, struct got_repository *repo)
4420 const struct got_error *err = NULL;
4421 struct tog_blame_view_state *s = &view->state.blame;
4423 STAILQ_INIT(&s->blamed_commits);
4425 s->path = strdup(path);
4426 if (s->path == NULL)
4427 return got_error_from_errno("strdup");
4429 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4435 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4436 s->first_displayed_line = 1;
4437 s->last_displayed_line = view->nlines;
4438 s->selected_line = 1;
4439 s->blame_complete = 0;
4441 s->commit_id = commit_id;
4442 memset(&s->blame, 0, sizeof(s->blame));
4444 STAILQ_INIT(&s->colors);
4445 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4446 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4447 get_color_value("TOG_COLOR_COMMIT"));
4452 view->show = show_blame_view;
4453 view->input = input_blame_view;
4454 view->close = close_blame_view;
4455 view->search_start = search_start_blame_view;
4456 view->search_next = search_next_blame_view;
4458 return run_blame(view);
4461 static const struct got_error *
4462 close_blame_view(struct tog_view *view)
4464 const struct got_error *err = NULL;
4465 struct tog_blame_view_state *s = &view->state.blame;
4467 if (s->blame.thread)
4468 err = stop_blame(&s->blame);
4470 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4471 struct got_object_qid *blamed_commit;
4472 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4473 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4474 got_object_qid_free(blamed_commit);
4478 free_colors(&s->colors);
4483 static const struct got_error *
4484 search_start_blame_view(struct tog_view *view)
4486 struct tog_blame_view_state *s = &view->state.blame;
4488 s->matched_line = 0;
4492 static const struct got_error *
4493 search_next_blame_view(struct tog_view *view)
4495 struct tog_blame_view_state *s = &view->state.blame;
4498 size_t linesize = 0;
4501 if (!view->searching) {
4502 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4506 if (s->matched_line) {
4507 if (view->searching == TOG_SEARCH_FORWARD)
4508 lineno = s->matched_line + 1;
4510 lineno = s->matched_line - 1;
4512 lineno = s->first_displayed_line - 1 + s->selected_line;
4517 if (lineno <= 0 || lineno > s->blame.nlines) {
4518 if (s->matched_line == 0) {
4519 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4523 if (view->searching == TOG_SEARCH_FORWARD)
4526 lineno = s->blame.nlines;
4529 offset = s->blame.line_offsets[lineno - 1];
4530 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4532 return got_error_from_errno("fseeko");
4534 linelen = getline(&line, &linesize, s->blame.f);
4535 if (linelen != -1 &&
4536 match_line(line, &view->regex, 1, &view->regmatch)) {
4537 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4538 s->matched_line = lineno;
4541 if (view->searching == TOG_SEARCH_FORWARD)
4548 if (s->matched_line) {
4549 s->first_displayed_line = s->matched_line;
4550 s->selected_line = 1;
4556 static const struct got_error *
4557 show_blame_view(struct tog_view *view)
4559 const struct got_error *err = NULL;
4560 struct tog_blame_view_state *s = &view->state.blame;
4563 if (s->blame.thread == NULL && !s->blame_complete) {
4564 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4565 &s->blame.thread_args);
4567 return got_error_set_errno(errcode, "pthread_create");
4569 halfdelay(1); /* fast refresh while annotating */
4572 if (s->blame_complete)
4573 halfdelay(10); /* disable fast refresh */
4575 err = draw_blame(view);
4581 static const struct got_error *
4582 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4584 const struct got_error *err = NULL, *thread_err = NULL;
4585 struct tog_view *diff_view;
4586 struct tog_blame_view_state *s = &view->state.blame;
4595 s->selected_line = 1;
4596 s->first_displayed_line = 1;
4600 if (s->blame.nlines < view->nlines - 2) {
4601 s->selected_line = s->blame.nlines;
4602 s->first_displayed_line = 1;
4604 s->selected_line = view->nlines - 2;
4605 s->first_displayed_line = s->blame.nlines -
4612 if (s->selected_line > 1)
4614 else if (s->selected_line == 1 &&
4615 s->first_displayed_line > 1)
4616 s->first_displayed_line--;
4620 if (s->first_displayed_line == 1) {
4621 s->selected_line = 1;
4624 if (s->first_displayed_line > view->nlines - 2)
4625 s->first_displayed_line -=
4628 s->first_displayed_line = 1;
4633 if (s->selected_line < view->nlines - 2 &&
4634 s->first_displayed_line +
4635 s->selected_line <= s->blame.nlines)
4637 else if (s->last_displayed_line <
4639 s->first_displayed_line++;
4643 struct got_object_id *id = NULL;
4644 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4645 s->first_displayed_line, s->selected_line);
4649 struct got_commit_object *commit;
4650 struct got_object_qid *pid;
4651 struct got_object_id *blob_id = NULL;
4653 err = got_object_open_as_commit(&commit,
4658 got_object_commit_get_parent_ids(commit));
4660 got_object_commit_close(commit);
4663 /* Check if path history ends here. */
4664 err = got_object_id_by_path(&blob_id, s->repo,
4667 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4669 got_object_commit_close(commit);
4672 err = got_object_get_type(&obj_type, s->repo,
4675 /* Can't blame non-blob type objects. */
4676 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4677 got_object_commit_close(commit);
4680 err = got_object_qid_alloc(&s->blamed_commit,
4682 got_object_commit_close(commit);
4684 if (got_object_id_cmp(id,
4685 s->blamed_commit->id) == 0)
4687 err = got_object_qid_alloc(&s->blamed_commit,
4693 thread_err = stop_blame(&s->blame);
4697 STAILQ_INSERT_HEAD(&s->blamed_commits,
4698 s->blamed_commit, entry);
4699 err = run_blame(view);
4705 struct got_object_qid *first;
4706 first = STAILQ_FIRST(&s->blamed_commits);
4707 if (!got_object_id_cmp(first->id, s->commit_id))
4710 thread_err = stop_blame(&s->blame);
4714 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4715 got_object_qid_free(s->blamed_commit);
4717 STAILQ_FIRST(&s->blamed_commits);
4718 err = run_blame(view);
4725 struct got_object_id *id = NULL;
4726 struct got_object_qid *pid;
4727 struct got_commit_object *commit = NULL;
4728 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4729 s->first_displayed_line, s->selected_line);
4732 err = got_object_open_as_commit(&commit, s->repo, id);
4736 got_object_commit_get_parent_ids(commit));
4737 if (view_is_parent_view(view))
4738 begin_x = view_split_begin_x(view->begin_x);
4739 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4740 if (diff_view == NULL) {
4741 got_object_commit_close(commit);
4742 err = got_error_from_errno("view_open");
4745 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4746 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4747 got_object_commit_close(commit);
4749 view_close(diff_view);
4753 diff_view->focussed = 1;
4754 if (view_is_parent_view(view)) {
4755 err = view_close_child(view);
4758 view_set_child(view, diff_view);
4759 view->focus_child = 1;
4761 *new_view = diff_view;
4769 if (s->last_displayed_line >= s->blame.nlines &&
4770 s->selected_line >= MIN(s->blame.nlines,
4771 view->nlines - 2)) {
4774 if (s->last_displayed_line >= s->blame.nlines &&
4775 s->selected_line < view->nlines - 2) {
4776 s->selected_line = MIN(s->blame.nlines,
4780 if (s->last_displayed_line + view->nlines - 2
4782 s->first_displayed_line +=
4785 s->first_displayed_line =
4790 if (s->selected_line > view->nlines - 2) {
4791 s->selected_line = MIN(s->blame.nlines,
4798 return thread_err ? thread_err : err;
4801 static const struct got_error *
4802 cmd_blame(int argc, char *argv[])
4804 const struct got_error *error;
4805 struct got_repository *repo = NULL;
4806 struct got_worktree *worktree = NULL;
4807 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4808 char *link_target = NULL;
4809 struct got_object_id *commit_id = NULL;
4810 char *commit_id_str = NULL;
4812 struct tog_view *view;
4814 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4817 commit_id_str = optarg;
4820 repo_path = realpath(optarg, NULL);
4821 if (repo_path == NULL)
4822 return got_error_from_errno2("realpath",
4837 if (repo_path == NULL) {
4838 cwd = getcwd(NULL, 0);
4840 return got_error_from_errno("getcwd");
4841 error = got_worktree_open(&worktree, cwd);
4842 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4846 strdup(got_worktree_get_repo_path(worktree));
4848 repo_path = strdup(cwd);
4849 if (repo_path == NULL) {
4850 error = got_error_from_errno("strdup");
4855 error = got_repo_open(&repo, repo_path, NULL);
4859 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4866 error = apply_unveil(got_repo_get_path(repo), NULL);
4870 error = tog_load_refs(repo, 0);
4874 if (commit_id_str == NULL) {
4875 struct got_reference *head_ref;
4876 error = got_ref_open(&head_ref, repo, worktree ?
4877 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4880 error = got_ref_resolve(&commit_id, repo, head_ref);
4881 got_ref_close(head_ref);
4883 error = got_repo_match_object_id(&commit_id, NULL,
4884 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4889 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4891 error = got_error_from_errno("view_open");
4895 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4900 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4905 /* Release work tree lock. */
4906 got_worktree_close(worktree);
4909 error = view_loop(view);
4917 got_worktree_close(worktree);
4919 const struct got_error *close_err = got_repo_close(repo);
4927 static const struct got_error *
4928 draw_tree_entries(struct tog_view *view, const char *parent_path)
4930 struct tog_tree_view_state *s = &view->state.tree;
4931 const struct got_error *err = NULL;
4932 struct got_tree_entry *te;
4934 struct tog_color *tc;
4935 int width, n, i, nentries;
4936 int limit = view->nlines;
4940 werase(view->window);
4945 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4948 if (view_needs_focus_indication(view))
4949 wstandout(view->window);
4950 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4952 wattr_on(view->window,
4953 COLOR_PAIR(tc->colorpair), NULL);
4954 waddwstr(view->window, wline);
4956 wattr_off(view->window,
4957 COLOR_PAIR(tc->colorpair), NULL);
4958 if (view_needs_focus_indication(view))
4959 wstandend(view->window);
4962 if (width < view->ncols - 1)
4963 waddch(view->window, '\n');
4966 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4969 waddwstr(view->window, wline);
4972 if (width < view->ncols - 1)
4973 waddch(view->window, '\n');
4976 waddch(view->window, '\n');
4980 if (s->first_displayed_entry == NULL) {
4981 te = got_object_tree_get_first_entry(s->tree);
4982 if (s->selected == 0) {
4984 wstandout(view->window);
4985 s->selected_entry = NULL;
4987 waddstr(view->window, " ..\n"); /* parent directory */
4988 if (s->selected == 0 && view->focussed)
4989 wstandend(view->window);
4996 te = s->first_displayed_entry;
4999 nentries = got_object_tree_get_nentries(s->tree);
5000 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5001 char *line = NULL, *id_str = NULL, *link_target = NULL;
5002 const char *modestr = "";
5005 te = got_object_tree_get_entry(s->tree, i);
5006 mode = got_tree_entry_get_mode(te);
5009 err = got_object_id_str(&id_str,
5010 got_tree_entry_get_id(te));
5012 return got_error_from_errno(
5013 "got_object_id_str");
5015 if (got_object_tree_entry_is_submodule(te))
5017 else if (S_ISLNK(mode)) {
5020 err = got_tree_entry_get_symlink_target(&link_target,
5026 for (i = 0; i < strlen(link_target); i++) {
5027 if (!isprint((unsigned char)link_target[i]))
5028 link_target[i] = '?';
5032 else if (S_ISDIR(mode))
5034 else if (mode & S_IXUSR)
5036 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5037 got_tree_entry_get_name(te), modestr,
5038 link_target ? " -> ": "",
5039 link_target ? link_target : "") == -1) {
5042 return got_error_from_errno("asprintf");
5046 err = format_line(&wline, &width, line, view->ncols, 0);
5051 if (n == s->selected) {
5053 wstandout(view->window);
5054 s->selected_entry = te;
5056 tc = match_color(&s->colors, line);
5058 wattr_on(view->window,
5059 COLOR_PAIR(tc->colorpair), NULL);
5060 waddwstr(view->window, wline);
5062 wattr_off(view->window,
5063 COLOR_PAIR(tc->colorpair), NULL);
5064 if (width < view->ncols - 1)
5065 waddch(view->window, '\n');
5066 if (n == s->selected && view->focussed)
5067 wstandend(view->window);
5073 s->last_displayed_entry = te;
5082 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5084 struct got_tree_entry *te;
5085 int isroot = s->tree == s->root;
5088 if (s->first_displayed_entry == NULL)
5091 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5092 while (i++ < maxscroll) {
5095 s->first_displayed_entry = NULL;
5098 s->first_displayed_entry = te;
5099 te = got_tree_entry_get_prev(s->tree, te);
5104 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5106 struct got_tree_entry *next, *last;
5109 if (s->first_displayed_entry)
5110 next = got_tree_entry_get_next(s->tree,
5111 s->first_displayed_entry);
5113 next = got_object_tree_get_first_entry(s->tree);
5115 last = s->last_displayed_entry;
5116 while (next && last && n++ < maxscroll) {
5117 last = got_tree_entry_get_next(s->tree, last);
5119 s->first_displayed_entry = next;
5120 next = got_tree_entry_get_next(s->tree, next);
5125 static const struct got_error *
5126 tree_entry_path(char **path, struct tog_parent_trees *parents,
5127 struct got_tree_entry *te)
5129 const struct got_error *err = NULL;
5130 struct tog_parent_tree *pt;
5131 size_t len = 2; /* for leading slash and NUL */
5133 TAILQ_FOREACH(pt, parents, entry)
5134 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5137 len += strlen(got_tree_entry_get_name(te));
5139 *path = calloc(1, len);
5141 return got_error_from_errno("calloc");
5144 pt = TAILQ_LAST(parents, tog_parent_trees);
5146 const char *name = got_tree_entry_get_name(pt->selected_entry);
5147 if (strlcat(*path, name, len) >= len) {
5148 err = got_error(GOT_ERR_NO_SPACE);
5151 if (strlcat(*path, "/", len) >= len) {
5152 err = got_error(GOT_ERR_NO_SPACE);
5155 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5158 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5159 err = got_error(GOT_ERR_NO_SPACE);
5171 static const struct got_error *
5172 blame_tree_entry(struct tog_view **new_view, int begin_x,
5173 struct got_tree_entry *te, struct tog_parent_trees *parents,
5174 struct got_object_id *commit_id, struct got_repository *repo)
5176 const struct got_error *err = NULL;
5178 struct tog_view *blame_view;
5182 err = tree_entry_path(&path, parents, te);
5186 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5187 if (blame_view == NULL) {
5188 err = got_error_from_errno("view_open");
5192 err = open_blame_view(blame_view, path, commit_id, repo);
5194 if (err->code == GOT_ERR_CANCELLED)
5196 view_close(blame_view);
5198 *new_view = blame_view;
5204 static const struct got_error *
5205 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5206 struct tog_tree_view_state *s)
5208 struct tog_view *log_view;
5209 const struct got_error *err = NULL;
5214 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5215 if (log_view == NULL)
5216 return got_error_from_errno("view_open");
5218 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5222 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5225 view_close(log_view);
5227 *new_view = log_view;
5232 static const struct got_error *
5233 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5234 const char *head_ref_name, struct got_repository *repo)
5236 const struct got_error *err = NULL;
5237 char *commit_id_str = NULL;
5238 struct tog_tree_view_state *s = &view->state.tree;
5239 struct got_commit_object *commit = NULL;
5241 TAILQ_INIT(&s->parents);
5242 STAILQ_INIT(&s->colors);
5244 s->commit_id = got_object_id_dup(commit_id);
5245 if (s->commit_id == NULL)
5246 return got_error_from_errno("got_object_id_dup");
5248 err = got_object_open_as_commit(&commit, repo, commit_id);
5253 * The root is opened here and will be closed when the view is closed.
5254 * Any visited subtrees and their path-wise parents are opened and
5257 err = got_object_open_as_tree(&s->root, repo,
5258 got_object_commit_get_tree_id(commit));
5263 err = got_object_id_str(&commit_id_str, commit_id);
5267 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5268 err = got_error_from_errno("asprintf");
5272 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5273 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5274 if (head_ref_name) {
5275 s->head_ref_name = strdup(head_ref_name);
5276 if (s->head_ref_name == NULL) {
5277 err = got_error_from_errno("strdup");
5283 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5284 err = add_color(&s->colors, "\\$$",
5285 TOG_COLOR_TREE_SUBMODULE,
5286 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5289 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5290 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5293 err = add_color(&s->colors, "/$",
5294 TOG_COLOR_TREE_DIRECTORY,
5295 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5299 err = add_color(&s->colors, "\\*$",
5300 TOG_COLOR_TREE_EXECUTABLE,
5301 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5305 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5306 get_color_value("TOG_COLOR_COMMIT"));
5311 view->show = show_tree_view;
5312 view->input = input_tree_view;
5313 view->close = close_tree_view;
5314 view->search_start = search_start_tree_view;
5315 view->search_next = search_next_tree_view;
5317 free(commit_id_str);
5319 got_object_commit_close(commit);
5321 close_tree_view(view);
5325 static const struct got_error *
5326 close_tree_view(struct tog_view *view)
5328 struct tog_tree_view_state *s = &view->state.tree;
5330 free_colors(&s->colors);
5331 free(s->tree_label);
5332 s->tree_label = NULL;
5334 s->commit_id = NULL;
5335 free(s->head_ref_name);
5336 s->head_ref_name = NULL;
5337 while (!TAILQ_EMPTY(&s->parents)) {
5338 struct tog_parent_tree *parent;
5339 parent = TAILQ_FIRST(&s->parents);
5340 TAILQ_REMOVE(&s->parents, parent, entry);
5341 if (parent->tree != s->root)
5342 got_object_tree_close(parent->tree);
5346 if (s->tree != NULL && s->tree != s->root)
5347 got_object_tree_close(s->tree);
5349 got_object_tree_close(s->root);
5353 static const struct got_error *
5354 search_start_tree_view(struct tog_view *view)
5356 struct tog_tree_view_state *s = &view->state.tree;
5358 s->matched_entry = NULL;
5363 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5365 regmatch_t regmatch;
5367 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5371 static const struct got_error *
5372 search_next_tree_view(struct tog_view *view)
5374 struct tog_tree_view_state *s = &view->state.tree;
5375 struct got_tree_entry *te = NULL;
5377 if (!view->searching) {
5378 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5382 if (s->matched_entry) {
5383 if (view->searching == TOG_SEARCH_FORWARD) {
5384 if (s->selected_entry)
5385 te = got_tree_entry_get_next(s->tree,
5388 te = got_object_tree_get_first_entry(s->tree);
5390 if (s->selected_entry == NULL)
5391 te = got_object_tree_get_last_entry(s->tree);
5393 te = got_tree_entry_get_prev(s->tree,
5397 if (s->selected_entry)
5398 te = s->selected_entry;
5399 else if (view->searching == TOG_SEARCH_FORWARD)
5400 te = got_object_tree_get_first_entry(s->tree);
5402 te = got_object_tree_get_last_entry(s->tree);
5407 if (s->matched_entry == NULL) {
5408 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5411 if (view->searching == TOG_SEARCH_FORWARD)
5412 te = got_object_tree_get_first_entry(s->tree);
5414 te = got_object_tree_get_last_entry(s->tree);
5417 if (match_tree_entry(te, &view->regex)) {
5418 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5419 s->matched_entry = te;
5423 if (view->searching == TOG_SEARCH_FORWARD)
5424 te = got_tree_entry_get_next(s->tree, te);
5426 te = got_tree_entry_get_prev(s->tree, te);
5429 if (s->matched_entry) {
5430 s->first_displayed_entry = s->matched_entry;
5437 static const struct got_error *
5438 show_tree_view(struct tog_view *view)
5440 const struct got_error *err = NULL;
5441 struct tog_tree_view_state *s = &view->state.tree;
5444 err = tree_entry_path(&parent_path, &s->parents, NULL);
5448 err = draw_tree_entries(view, parent_path);
5455 static const struct got_error *
5456 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5458 const struct got_error *err = NULL;
5459 struct tog_tree_view_state *s = &view->state.tree;
5460 struct tog_view *log_view, *ref_view;
5461 struct got_tree_entry *te;
5466 s->show_ids = !s->show_ids;
5469 if (!s->selected_entry)
5471 if (view_is_parent_view(view))
5472 begin_x = view_split_begin_x(view->begin_x);
5473 err = log_selected_tree_entry(&log_view, begin_x, s);
5475 log_view->focussed = 1;
5476 if (view_is_parent_view(view)) {
5477 err = view_close_child(view);
5480 view_set_child(view, log_view);
5481 view->focus_child = 1;
5483 *new_view = log_view;
5486 if (view_is_parent_view(view))
5487 begin_x = view_split_begin_x(view->begin_x);
5488 ref_view = view_open(view->nlines, view->ncols,
5489 view->begin_y, begin_x, TOG_VIEW_REF);
5490 if (ref_view == NULL)
5491 return got_error_from_errno("view_open");
5492 err = open_ref_view(ref_view, s->repo);
5494 view_close(ref_view);
5498 ref_view->focussed = 1;
5499 if (view_is_parent_view(view)) {
5500 err = view_close_child(view);
5503 view_set_child(view, ref_view);
5504 view->focus_child = 1;
5506 *new_view = ref_view;
5511 if (s->tree == s->root)
5512 s->first_displayed_entry =
5513 got_object_tree_get_first_entry(s->tree);
5515 s->first_displayed_entry = NULL;
5520 te = got_object_tree_get_last_entry(s->tree);
5521 for (n = 0; n < view->nlines - 3; n++) {
5523 if(s->tree != s->root) {
5524 s->first_displayed_entry = NULL;
5529 s->first_displayed_entry = te;
5530 te = got_tree_entry_get_prev(s->tree, te);
5533 s->selected = n - 1;
5538 if (s->selected > 0) {
5542 tree_scroll_up(s, 1);
5546 if (s->tree == s->root) {
5547 if (got_object_tree_get_first_entry(s->tree) ==
5548 s->first_displayed_entry)
5551 if (s->first_displayed_entry == NULL)
5554 tree_scroll_up(s, MAX(0, view->nlines - 3));
5559 if (s->selected < s->ndisplayed - 1) {
5563 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5565 /* can't scroll any further */
5567 tree_scroll_down(s, 1);
5571 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5573 /* can't scroll any further; move cursor down */
5574 if (s->selected < s->ndisplayed - 1)
5575 s->selected = s->ndisplayed - 1;
5578 tree_scroll_down(s, view->nlines - 3);
5583 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5584 struct tog_parent_tree *parent;
5585 /* user selected '..' */
5586 if (s->tree == s->root)
5588 parent = TAILQ_FIRST(&s->parents);
5589 TAILQ_REMOVE(&s->parents, parent,
5591 got_object_tree_close(s->tree);
5592 s->tree = parent->tree;
5593 s->first_displayed_entry =
5594 parent->first_displayed_entry;
5596 parent->selected_entry;
5597 s->selected = parent->selected;
5599 } else if (S_ISDIR(got_tree_entry_get_mode(
5600 s->selected_entry))) {
5601 struct got_tree_object *subtree;
5602 err = got_object_open_as_tree(&subtree, s->repo,
5603 got_tree_entry_get_id(s->selected_entry));
5606 err = tree_view_visit_subtree(s, subtree);
5608 got_object_tree_close(subtree);
5611 } else if (S_ISREG(got_tree_entry_get_mode(
5612 s->selected_entry))) {
5613 struct tog_view *blame_view;
5614 int begin_x = view_is_parent_view(view) ?
5615 view_split_begin_x(view->begin_x) : 0;
5617 err = blame_tree_entry(&blame_view, begin_x,
5618 s->selected_entry, &s->parents,
5619 s->commit_id, s->repo);
5623 blame_view->focussed = 1;
5624 if (view_is_parent_view(view)) {
5625 err = view_close_child(view);
5628 view_set_child(view, blame_view);
5629 view->focus_child = 1;
5631 *new_view = blame_view;
5635 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5636 s->selected = view->nlines - 4;
5649 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5654 static const struct got_error *
5655 cmd_tree(int argc, char *argv[])
5657 const struct got_error *error;
5658 struct got_repository *repo = NULL;
5659 struct got_worktree *worktree = NULL;
5660 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5661 struct got_object_id *commit_id = NULL;
5662 const char *commit_id_arg = NULL;
5664 struct got_reference *ref = NULL;
5665 const char *head_ref_name = NULL;
5667 struct tog_view *view;
5669 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5672 commit_id_arg = optarg;
5675 repo_path = realpath(optarg, NULL);
5676 if (repo_path == NULL)
5677 return got_error_from_errno2("realpath",
5692 if (repo_path == NULL) {
5693 cwd = getcwd(NULL, 0);
5695 return got_error_from_errno("getcwd");
5696 error = got_worktree_open(&worktree, cwd);
5697 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5701 strdup(got_worktree_get_repo_path(worktree));
5703 repo_path = strdup(cwd);
5704 if (repo_path == NULL) {
5705 error = got_error_from_errno("strdup");
5710 error = got_repo_open(&repo, repo_path, NULL);
5714 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5721 error = apply_unveil(got_repo_get_path(repo), NULL);
5725 error = tog_load_refs(repo, 0);
5729 if (commit_id_arg == NULL) {
5730 error = got_repo_match_object_id(&commit_id, &label,
5731 worktree ? got_worktree_get_head_ref_name(worktree) :
5732 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5735 head_ref_name = label;
5737 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5739 head_ref_name = got_ref_get_name(ref);
5740 else if (error->code != GOT_ERR_NOT_REF)
5742 error = got_repo_match_object_id(&commit_id, NULL,
5743 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5748 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5750 error = got_error_from_errno("view_open");
5753 error = open_tree_view(view, commit_id, head_ref_name, repo);
5756 if (!got_path_is_root_dir(in_repo_path)) {
5757 error = tree_view_walk_path(&view->state.tree, commit_id,
5764 /* Release work tree lock. */
5765 got_worktree_close(worktree);
5768 error = view_loop(view);
5777 const struct got_error *close_err = got_repo_close(repo);
5785 static const struct got_error *
5786 ref_view_load_refs(struct tog_ref_view_state *s)
5788 struct got_reflist_entry *sre;
5789 struct tog_reflist_entry *re;
5792 TAILQ_FOREACH(sre, &tog_refs, entry) {
5793 if (strncmp(got_ref_get_name(sre->ref),
5794 "refs/got/", 9) == 0 &&
5795 strncmp(got_ref_get_name(sre->ref),
5796 "refs/got/backup/", 16) != 0)
5799 re = malloc(sizeof(*re));
5801 return got_error_from_errno("malloc");
5803 re->ref = got_ref_dup(sre->ref);
5804 if (re->ref == NULL)
5805 return got_error_from_errno("got_ref_dup");
5806 re->idx = s->nrefs++;
5807 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5810 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5815 ref_view_free_refs(struct tog_ref_view_state *s)
5817 struct tog_reflist_entry *re;
5819 while (!TAILQ_EMPTY(&s->refs)) {
5820 re = TAILQ_FIRST(&s->refs);
5821 TAILQ_REMOVE(&s->refs, re, entry);
5822 got_ref_close(re->ref);
5827 static const struct got_error *
5828 open_ref_view(struct tog_view *view, struct got_repository *repo)
5830 const struct got_error *err = NULL;
5831 struct tog_ref_view_state *s = &view->state.ref;
5833 s->selected_entry = 0;
5836 TAILQ_INIT(&s->refs);
5837 STAILQ_INIT(&s->colors);
5839 err = ref_view_load_refs(s);
5843 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5844 err = add_color(&s->colors, "^refs/heads/",
5845 TOG_COLOR_REFS_HEADS,
5846 get_color_value("TOG_COLOR_REFS_HEADS"));
5850 err = add_color(&s->colors, "^refs/tags/",
5851 TOG_COLOR_REFS_TAGS,
5852 get_color_value("TOG_COLOR_REFS_TAGS"));
5856 err = add_color(&s->colors, "^refs/remotes/",
5857 TOG_COLOR_REFS_REMOTES,
5858 get_color_value("TOG_COLOR_REFS_REMOTES"));
5862 err = add_color(&s->colors, "^refs/got/backup/",
5863 TOG_COLOR_REFS_BACKUP,
5864 get_color_value("TOG_COLOR_REFS_BACKUP"));
5869 view->show = show_ref_view;
5870 view->input = input_ref_view;
5871 view->close = close_ref_view;
5872 view->search_start = search_start_ref_view;
5873 view->search_next = search_next_ref_view;
5876 free_colors(&s->colors);
5880 static const struct got_error *
5881 close_ref_view(struct tog_view *view)
5883 struct tog_ref_view_state *s = &view->state.ref;
5885 ref_view_free_refs(s);
5886 free_colors(&s->colors);
5891 static const struct got_error *
5892 resolve_reflist_entry(struct got_object_id **commit_id,
5893 struct tog_reflist_entry *re, struct got_repository *repo)
5895 const struct got_error *err = NULL;
5896 struct got_object_id *obj_id;
5897 struct got_tag_object *tag = NULL;
5902 err = got_ref_resolve(&obj_id, repo, re->ref);
5906 err = got_object_get_type(&obj_type, repo, obj_id);
5911 case GOT_OBJ_TYPE_COMMIT:
5912 *commit_id = obj_id;
5914 case GOT_OBJ_TYPE_TAG:
5915 err = got_object_open_as_tag(&tag, repo, obj_id);
5919 err = got_object_get_type(&obj_type, repo,
5920 got_object_tag_get_object_id(tag));
5923 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5924 err = got_error(GOT_ERR_OBJ_TYPE);
5927 *commit_id = got_object_id_dup(
5928 got_object_tag_get_object_id(tag));
5929 if (*commit_id == NULL) {
5930 err = got_error_from_errno("got_object_id_dup");
5935 err = got_error(GOT_ERR_OBJ_TYPE);
5941 got_object_tag_close(tag);
5949 static const struct got_error *
5950 log_ref_entry(struct tog_view **new_view, int begin_x,
5951 struct tog_reflist_entry *re, struct got_repository *repo)
5953 struct tog_view *log_view;
5954 const struct got_error *err = NULL;
5955 struct got_object_id *commit_id = NULL;
5959 err = resolve_reflist_entry(&commit_id, re, repo);
5961 if (err->code != GOT_ERR_OBJ_TYPE)
5967 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5968 if (log_view == NULL) {
5969 err = got_error_from_errno("view_open");
5973 err = open_log_view(log_view, commit_id, repo,
5974 got_ref_get_name(re->ref), "", 0);
5977 view_close(log_view);
5979 *new_view = log_view;
5985 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5987 struct tog_reflist_entry *re;
5990 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5993 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5994 while (i++ < maxscroll) {
5997 s->first_displayed_entry = re;
5998 re = TAILQ_PREV(re, tog_reflist_head, entry);
6003 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6005 struct tog_reflist_entry *next, *last;
6008 if (s->first_displayed_entry)
6009 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6011 next = TAILQ_FIRST(&s->refs);
6013 last = s->last_displayed_entry;
6014 while (next && last && n++ < maxscroll) {
6015 last = TAILQ_NEXT(last, entry);
6017 s->first_displayed_entry = next;
6018 next = TAILQ_NEXT(next, entry);
6023 static const struct got_error *
6024 search_start_ref_view(struct tog_view *view)
6026 struct tog_ref_view_state *s = &view->state.ref;
6028 s->matched_entry = NULL;
6033 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6035 regmatch_t regmatch;
6037 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6041 static const struct got_error *
6042 search_next_ref_view(struct tog_view *view)
6044 struct tog_ref_view_state *s = &view->state.ref;
6045 struct tog_reflist_entry *re = NULL;
6047 if (!view->searching) {
6048 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6052 if (s->matched_entry) {
6053 if (view->searching == TOG_SEARCH_FORWARD) {
6054 if (s->selected_entry)
6055 re = TAILQ_NEXT(s->selected_entry, entry);
6057 re = TAILQ_PREV(s->selected_entry,
6058 tog_reflist_head, entry);
6060 if (s->selected_entry == NULL)
6061 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6063 re = TAILQ_PREV(s->selected_entry,
6064 tog_reflist_head, entry);
6067 if (s->selected_entry)
6068 re = s->selected_entry;
6069 else if (view->searching == TOG_SEARCH_FORWARD)
6070 re = TAILQ_FIRST(&s->refs);
6072 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6077 if (s->matched_entry == NULL) {
6078 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6081 if (view->searching == TOG_SEARCH_FORWARD)
6082 re = TAILQ_FIRST(&s->refs);
6084 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6087 if (match_reflist_entry(re, &view->regex)) {
6088 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6089 s->matched_entry = re;
6093 if (view->searching == TOG_SEARCH_FORWARD)
6094 re = TAILQ_NEXT(re, entry);
6096 re = TAILQ_PREV(re, tog_reflist_head, entry);
6099 if (s->matched_entry) {
6100 s->first_displayed_entry = s->matched_entry;
6107 static const struct got_error *
6108 show_ref_view(struct tog_view *view)
6110 const struct got_error *err = NULL;
6111 struct tog_ref_view_state *s = &view->state.ref;
6112 struct tog_reflist_entry *re;
6115 struct tog_color *tc;
6117 int limit = view->nlines;
6119 werase(view->window);
6126 re = s->first_displayed_entry;
6128 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6130 return got_error_from_errno("asprintf");
6132 err = format_line(&wline, &width, line, view->ncols, 0);
6137 if (view_needs_focus_indication(view))
6138 wstandout(view->window);
6139 waddwstr(view->window, wline);
6140 if (view_needs_focus_indication(view))
6141 wstandend(view->window);
6146 if (width < view->ncols - 1)
6147 waddch(view->window, '\n');
6152 while (re && limit > 0) {
6155 if (got_ref_is_symbolic(re->ref)) {
6156 if (asprintf(&line, "%s -> %s",
6157 got_ref_get_name(re->ref),
6158 got_ref_get_symref_target(re->ref)) == -1)
6159 return got_error_from_errno("asprintf");
6160 } else if (s->show_ids) {
6161 struct got_object_id *id;
6163 err = got_ref_resolve(&id, s->repo, re->ref);
6166 err = got_object_id_str(&id_str, id);
6171 if (asprintf(&line, "%s: %s",
6172 got_ref_get_name(re->ref), id_str) == -1) {
6173 err = got_error_from_errno("asprintf");
6181 line = strdup(got_ref_get_name(re->ref));
6183 return got_error_from_errno("strdup");
6186 err = format_line(&wline, &width, line, view->ncols, 0);
6191 if (n == s->selected) {
6193 wstandout(view->window);
6194 s->selected_entry = re;
6196 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6198 wattr_on(view->window,
6199 COLOR_PAIR(tc->colorpair), NULL);
6200 waddwstr(view->window, wline);
6202 wattr_off(view->window,
6203 COLOR_PAIR(tc->colorpair), NULL);
6204 if (width < view->ncols - 1)
6205 waddch(view->window, '\n');
6206 if (n == s->selected && view->focussed)
6207 wstandend(view->window);
6213 s->last_displayed_entry = re;
6216 re = TAILQ_NEXT(re, entry);
6223 static const struct got_error *
6224 browse_ref_tree(struct tog_view **new_view, int begin_x,
6225 struct tog_reflist_entry *re, struct got_repository *repo)
6227 const struct got_error *err = NULL;
6228 struct got_object_id *commit_id = NULL;
6229 struct tog_view *tree_view;
6233 err = resolve_reflist_entry(&commit_id, re, repo);
6235 if (err->code != GOT_ERR_OBJ_TYPE)
6242 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6243 if (tree_view == NULL) {
6244 err = got_error_from_errno("view_open");
6248 err = open_tree_view(tree_view, commit_id,
6249 got_ref_get_name(re->ref), repo);
6253 *new_view = tree_view;
6258 static const struct got_error *
6259 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6261 const struct got_error *err = NULL;
6262 struct tog_ref_view_state *s = &view->state.ref;
6263 struct tog_view *log_view, *tree_view;
6264 struct tog_reflist_entry *re;
6269 s->show_ids = !s->show_ids;
6272 s->sort_by_date = !s->sort_by_date;
6273 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6274 got_ref_cmp_by_commit_timestamp_descending :
6275 tog_ref_cmp_by_name, s->repo);
6278 got_reflist_object_id_map_free(tog_refs_idmap);
6279 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6280 &tog_refs, s->repo);
6283 ref_view_free_refs(s);
6284 err = ref_view_load_refs(s);
6288 if (!s->selected_entry)
6290 if (view_is_parent_view(view))
6291 begin_x = view_split_begin_x(view->begin_x);
6292 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6295 log_view->focussed = 1;
6296 if (view_is_parent_view(view)) {
6297 err = view_close_child(view);
6300 view_set_child(view, log_view);
6301 view->focus_child = 1;
6303 *new_view = log_view;
6306 if (!s->selected_entry)
6308 if (view_is_parent_view(view))
6309 begin_x = view_split_begin_x(view->begin_x);
6310 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6312 if (err || tree_view == NULL)
6315 tree_view->focussed = 1;
6316 if (view_is_parent_view(view)) {
6317 err = view_close_child(view);
6320 view_set_child(view, tree_view);
6321 view->focus_child = 1;
6323 *new_view = tree_view;
6328 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6333 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6334 for (n = 0; n < view->nlines - 1; n++) {
6337 s->first_displayed_entry = re;
6338 re = TAILQ_PREV(re, tog_reflist_head, entry);
6341 s->selected = n - 1;
6346 if (s->selected > 0) {
6350 ref_scroll_up(s, 1);
6354 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6356 ref_scroll_up(s, MAX(0, view->nlines - 1));
6361 if (s->selected < s->ndisplayed - 1) {
6365 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6366 /* can't scroll any further */
6368 ref_scroll_down(s, 1);
6372 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6373 /* can't scroll any further; move cursor down */
6374 if (s->selected < s->ndisplayed - 1)
6375 s->selected = s->ndisplayed - 1;
6378 ref_scroll_down(s, view->nlines - 1);
6382 err = tog_load_refs(s->repo, s->sort_by_date);
6385 ref_view_free_refs(s);
6386 err = ref_view_load_refs(s);
6389 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6390 s->selected = view->nlines - 2;
6403 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6408 static const struct got_error *
6409 cmd_ref(int argc, char *argv[])
6411 const struct got_error *error;
6412 struct got_repository *repo = NULL;
6413 struct got_worktree *worktree = NULL;
6414 char *cwd = NULL, *repo_path = NULL;
6416 struct tog_view *view;
6418 while ((ch = getopt(argc, argv, "r:")) != -1) {
6421 repo_path = realpath(optarg, NULL);
6422 if (repo_path == NULL)
6423 return got_error_from_errno2("realpath",
6438 if (repo_path == NULL) {
6439 cwd = getcwd(NULL, 0);
6441 return got_error_from_errno("getcwd");
6442 error = got_worktree_open(&worktree, cwd);
6443 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6447 strdup(got_worktree_get_repo_path(worktree));
6449 repo_path = strdup(cwd);
6450 if (repo_path == NULL) {
6451 error = got_error_from_errno("strdup");
6456 error = got_repo_open(&repo, repo_path, NULL);
6462 error = apply_unveil(got_repo_get_path(repo), NULL);
6466 error = tog_load_refs(repo, 0);
6470 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6472 error = got_error_from_errno("view_open");
6476 error = open_ref_view(view, repo);
6481 /* Release work tree lock. */
6482 got_worktree_close(worktree);
6485 error = view_loop(view);
6490 const struct got_error *close_err = got_repo_close(repo);
6499 list_commands(FILE *fp)
6503 fprintf(fp, "commands:");
6504 for (i = 0; i < nitems(tog_commands); i++) {
6505 const struct tog_cmd *cmd = &tog_commands[i];
6506 fprintf(fp, " %s", cmd->name);
6512 usage(int hflag, int status)
6514 FILE *fp = (status == 0) ? stdout : stderr;
6516 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6519 fprintf(fp, "lazy usage: %s path\n", getprogname());
6526 make_argv(int argc, ...)
6534 argv = calloc(argc, sizeof(char *));
6537 for (i = 0; i < argc; i++) {
6538 argv[i] = strdup(va_arg(ap, char *));
6539 if (argv[i] == NULL)
6548 * Try to convert 'tog path' into a 'tog log path' command.
6549 * The user could simply have mistyped the command rather than knowingly
6550 * provided a path. So check whether argv[0] can in fact be resolved
6551 * to a path in the HEAD commit and print a special error if not.
6552 * This hack is for mpi@ <3
6554 static const struct got_error *
6555 tog_log_with_path(int argc, char *argv[])
6557 const struct got_error *error = NULL, *close_err;
6558 const struct tog_cmd *cmd = NULL;
6559 struct got_repository *repo = NULL;
6560 struct got_worktree *worktree = NULL;
6561 struct got_object_id *commit_id = NULL, *id = NULL;
6562 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6563 char *commit_id_str = NULL, **cmd_argv = NULL;
6565 cwd = getcwd(NULL, 0);
6567 return got_error_from_errno("getcwd");
6569 error = got_worktree_open(&worktree, cwd);
6570 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6574 repo_path = strdup(got_worktree_get_repo_path(worktree));
6576 repo_path = strdup(cwd);
6577 if (repo_path == NULL) {
6578 error = got_error_from_errno("strdup");
6582 error = got_repo_open(&repo, repo_path, NULL);
6586 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6591 error = tog_load_refs(repo, 0);
6594 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6595 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6596 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6601 got_worktree_close(worktree);
6605 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6607 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6609 fprintf(stderr, "%s: '%s' is no known command or path\n",
6610 getprogname(), argv[0]);
6615 close_err = got_repo_close(repo);
6620 error = got_object_id_str(&commit_id_str, commit_id);
6624 cmd = &tog_commands[0]; /* log */
6626 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6627 error = cmd->cmd_main(argc, cmd_argv);
6630 close_err = got_repo_close(repo);
6635 got_worktree_close(worktree);
6637 free(commit_id_str);
6644 for (i = 0; i < argc; i++)
6653 main(int argc, char *argv[])
6655 const struct got_error *error = NULL;
6656 const struct tog_cmd *cmd = NULL;
6657 int ch, hflag = 0, Vflag = 0;
6658 char **cmd_argv = NULL;
6659 static const struct option longopts[] = {
6660 { "version", no_argument, NULL, 'V' },
6664 setlocale(LC_CTYPE, "");
6666 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6686 got_version_print_str();
6691 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6699 /* Build an argument vector which runs a default command. */
6700 cmd = &tog_commands[0];
6702 cmd_argv = make_argv(argc, cmd->name);
6706 /* Did the user specify a command? */
6707 for (i = 0; i < nitems(tog_commands); i++) {
6708 if (strncmp(tog_commands[i].name, argv[0],
6709 strlen(argv[0])) == 0) {
6710 cmd = &tog_commands[i];
6719 /* No command specified; try log with a path */
6720 error = tog_log_with_path(argc, argv);
6725 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6732 for (i = 0; i < argc; i++)
6737 if (error && error->code != GOT_ERR_CANCELLED)
6738 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);