2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/ioctl.h>
22 #if defined(__FreeBSD__) || defined(__APPLE__)
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
45 #include "got_compat.h"
47 #include "got_version.h"
48 #include "got_error.h"
49 #include "got_object.h"
50 #include "got_reference.h"
51 #include "got_repository.h"
53 #include "got_opentemp.h"
55 #include "got_cancel.h"
56 #include "got_commit_graph.h"
57 #include "got_blame.h"
58 #include "got_privsep.h"
60 #include "got_worktree.h"
62 //#define update_panels() (0)
63 //#define doupdate() (0)
66 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
70 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
74 #define CTRL(x) ((x) & 0x1f)
78 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
83 const struct got_error *(*cmd_main)(int, char *[]);
84 void (*cmd_usage)(void);
87 __dead static void usage(int, int);
88 __dead static void usage_log(void);
89 __dead static void usage_diff(void);
90 __dead static void usage_blame(void);
91 __dead static void usage_tree(void);
92 __dead static void usage_ref(void);
94 static const struct got_error* cmd_log(int, char *[]);
95 static const struct got_error* cmd_diff(int, char *[]);
96 static const struct got_error* cmd_blame(int, char *[]);
97 static const struct got_error* cmd_tree(int, char *[]);
98 static const struct got_error* cmd_ref(int, char *[]);
100 static const struct tog_cmd tog_commands[] = {
101 { "log", cmd_log, usage_log },
102 { "diff", cmd_diff, usage_diff },
103 { "blame", cmd_blame, usage_blame },
104 { "tree", cmd_tree, usage_tree },
105 { "ref", cmd_ref, usage_ref },
116 #define TOG_EOF_STRING "(END)"
118 struct commit_queue_entry {
119 TAILQ_ENTRY(commit_queue_entry) entry;
120 struct got_object_id *id;
121 struct got_commit_object *commit;
124 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
125 struct commit_queue {
127 struct commit_queue_head head;
131 STAILQ_ENTRY(tog_color) entry;
135 STAILQ_HEAD(tog_colors, tog_color);
137 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
138 static struct got_reflist_object_id_map *tog_refs_idmap;
140 static const struct got_error *
141 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
142 struct got_reference* re2)
144 const char *name1 = got_ref_get_name(re1);
145 const char *name2 = got_ref_get_name(re2);
146 int isbackup1, isbackup2;
148 /* Sort backup refs towards the bottom of the list. */
149 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
150 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
151 if (!isbackup1 && isbackup2) {
154 } else if (isbackup1 && !isbackup2) {
159 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
163 static const struct got_error *
164 tog_load_refs(struct got_repository *repo, int sort_by_date)
166 const struct got_error *err;
168 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
169 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
174 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
181 if (tog_refs_idmap) {
182 got_reflist_object_id_map_free(tog_refs_idmap);
183 tog_refs_idmap = NULL;
185 got_ref_list_free(&tog_refs);
188 static const struct got_error *
189 add_color(struct tog_colors *colors, const char *pattern,
190 int idx, short color)
192 const struct got_error *err = NULL;
193 struct tog_color *tc;
196 if (idx < 1 || idx > COLOR_PAIRS - 1)
199 init_pair(idx, color, -1);
201 tc = calloc(1, sizeof(*tc));
203 return got_error_from_errno("calloc");
204 regerr = regcomp(&tc->regex, pattern,
205 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
207 static char regerr_msg[512];
208 static char err_msg[512];
209 regerror(regerr, &tc->regex, regerr_msg,
211 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
213 err = got_error_msg(GOT_ERR_REGEX, err_msg);
218 STAILQ_INSERT_HEAD(colors, tc, entry);
223 free_colors(struct tog_colors *colors)
225 struct tog_color *tc;
227 while (!STAILQ_EMPTY(colors)) {
228 tc = STAILQ_FIRST(colors);
229 STAILQ_REMOVE_HEAD(colors, entry);
236 get_color(struct tog_colors *colors, int colorpair)
238 struct tog_color *tc = NULL;
240 STAILQ_FOREACH(tc, colors, entry) {
241 if (tc->colorpair == colorpair)
249 default_color_value(const char *envvar)
251 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
252 return COLOR_MAGENTA;
253 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
255 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
257 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
259 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
260 return COLOR_MAGENTA;
261 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
262 return COLOR_MAGENTA;
263 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
265 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
267 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
269 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
271 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
273 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
275 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
276 return COLOR_MAGENTA;
277 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
279 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
286 get_color_value(const char *envvar)
288 const char *val = getenv(envvar);
291 return default_color_value(envvar);
293 if (strcasecmp(val, "black") == 0)
295 if (strcasecmp(val, "red") == 0)
297 if (strcasecmp(val, "green") == 0)
299 if (strcasecmp(val, "yellow") == 0)
301 if (strcasecmp(val, "blue") == 0)
303 if (strcasecmp(val, "magenta") == 0)
304 return COLOR_MAGENTA;
305 if (strcasecmp(val, "cyan") == 0)
307 if (strcasecmp(val, "white") == 0)
309 if (strcasecmp(val, "default") == 0)
312 return default_color_value(envvar);
316 struct tog_diff_view_state {
317 struct got_object_id *id1, *id2;
318 const char *label1, *label2;
320 int first_displayed_line;
321 int last_displayed_line;
324 int ignore_whitespace;
326 struct got_repository *repo;
327 struct tog_colors colors;
333 /* passed from log view; may be NULL */
334 struct tog_view *log_view;
337 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
339 struct tog_log_thread_args {
340 pthread_cond_t need_commits;
341 pthread_cond_t commit_loaded;
344 struct got_commit_graph *graph;
345 struct commit_queue *commits;
346 const char *in_repo_path;
347 struct got_object_id *start_id;
348 struct got_repository *repo;
351 struct commit_queue_entry **first_displayed_entry;
352 struct commit_queue_entry **selected_entry;
354 int *search_next_done;
358 struct tog_log_view_state {
359 struct commit_queue commits;
360 struct commit_queue_entry *first_displayed_entry;
361 struct commit_queue_entry *last_displayed_entry;
362 struct commit_queue_entry *selected_entry;
367 struct got_repository *repo;
368 struct got_object_id *start_id;
371 struct tog_log_thread_args thread_args;
372 struct commit_queue_entry *matched_entry;
373 struct commit_queue_entry *search_entry;
374 struct tog_colors colors;
377 #define TOG_COLOR_DIFF_MINUS 1
378 #define TOG_COLOR_DIFF_PLUS 2
379 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
380 #define TOG_COLOR_DIFF_META 4
381 #define TOG_COLOR_TREE_SUBMODULE 5
382 #define TOG_COLOR_TREE_SYMLINK 6
383 #define TOG_COLOR_TREE_DIRECTORY 7
384 #define TOG_COLOR_TREE_EXECUTABLE 8
385 #define TOG_COLOR_COMMIT 9
386 #define TOG_COLOR_AUTHOR 10
387 #define TOG_COLOR_DATE 11
388 #define TOG_COLOR_REFS_HEADS 12
389 #define TOG_COLOR_REFS_TAGS 13
390 #define TOG_COLOR_REFS_REMOTES 14
391 #define TOG_COLOR_REFS_BACKUP 15
393 struct tog_blame_cb_args {
394 struct tog_blame_line *lines; /* one per line */
397 struct tog_view *view;
398 struct got_object_id *commit_id;
402 struct tog_blame_thread_args {
404 struct got_repository *repo;
405 struct tog_blame_cb_args *cb_args;
407 got_cancel_cb cancel_cb;
414 struct tog_blame_line *lines;
418 struct tog_blame_thread_args thread_args;
419 struct tog_blame_cb_args cb_args;
423 struct tog_blame_view_state {
424 int first_displayed_line;
425 int last_displayed_line;
430 struct got_object_id_queue blamed_commits;
431 struct got_object_qid *blamed_commit;
433 struct got_repository *repo;
434 struct got_object_id *commit_id;
435 struct tog_blame blame;
437 struct tog_colors colors;
440 struct tog_parent_tree {
441 TAILQ_ENTRY(tog_parent_tree) entry;
442 struct got_tree_object *tree;
443 struct got_tree_entry *first_displayed_entry;
444 struct got_tree_entry *selected_entry;
448 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
450 struct tog_tree_view_state {
452 struct got_object_id *commit_id;/* commit which this tree belongs to */
453 struct got_tree_object *root; /* the commit's root tree entry */
454 struct got_tree_object *tree; /* currently displayed (sub-)tree */
455 struct got_tree_entry *first_displayed_entry;
456 struct got_tree_entry *last_displayed_entry;
457 struct got_tree_entry *selected_entry;
458 int ndisplayed, selected, show_ids;
459 struct tog_parent_trees parents; /* parent trees of current sub-tree */
461 struct got_repository *repo;
462 struct got_tree_entry *matched_entry;
463 struct tog_colors colors;
466 struct tog_reflist_entry {
467 TAILQ_ENTRY(tog_reflist_entry) entry;
468 struct got_reference *ref;
472 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
474 struct tog_ref_view_state {
475 struct tog_reflist_head refs;
476 struct tog_reflist_entry *first_displayed_entry;
477 struct tog_reflist_entry *last_displayed_entry;
478 struct tog_reflist_entry *selected_entry;
479 int nrefs, ndisplayed, selected, show_ids, sort_by_date;
480 struct got_repository *repo;
481 struct tog_reflist_entry *matched_entry;
482 struct tog_colors colors;
486 * We implement two types of views: parent views and child views.
488 * The 'Tab' key switches focus between a parent view and its child view.
489 * Child views are shown side-by-side to their parent view, provided
490 * there is enough screen estate.
492 * When a new view is opened from within a parent view, this new view
493 * becomes a child view of the parent view, replacing any existing child.
495 * When a new view is opened from within a child view, this new view
496 * becomes a parent view which will obscure the views below until the
497 * user quits the new parent view by typing 'q'.
499 * This list of views contains parent views only.
500 * Child views are only pointed to by their parent view.
502 TAILQ_HEAD(tog_view_list_head, tog_view);
505 TAILQ_ENTRY(tog_view) entry;
508 int nlines, ncols, begin_y, begin_x;
509 int lines, cols; /* copies of LINES and COLS */
510 int focussed; /* Only set on one parent or child view at a time. */
512 struct tog_view *parent;
513 struct tog_view *child;
516 * This flag is initially set on parent views when a new child view
517 * is created. It gets toggled when the 'Tab' key switches focus
518 * between parent and child.
519 * The flag indicates whether focus should be passed on to our child
520 * view if this parent view gets picked for focus after another parent
521 * view was closed. This prevents child views from losing focus in such
526 /* type-specific state */
527 enum tog_view_type type;
529 struct tog_diff_view_state diff;
530 struct tog_log_view_state log;
531 struct tog_blame_view_state blame;
532 struct tog_tree_view_state tree;
533 struct tog_ref_view_state ref;
536 const struct got_error *(*show)(struct tog_view *);
537 const struct got_error *(*input)(struct tog_view **,
538 struct tog_view *, int);
539 const struct got_error *(*close)(struct tog_view *);
541 const struct got_error *(*search_start)(struct tog_view *);
542 const struct got_error *(*search_next)(struct tog_view *);
545 #define TOG_SEARCH_FORWARD 1
546 #define TOG_SEARCH_BACKWARD 2
547 int search_next_done;
548 #define TOG_SEARCH_HAVE_MORE 1
549 #define TOG_SEARCH_NO_MORE 2
550 #define TOG_SEARCH_HAVE_NONE 3
555 static const struct got_error *open_diff_view(struct tog_view *,
556 struct got_object_id *, struct got_object_id *,
557 const char *, const char *, int, int, int, struct tog_view *,
558 struct got_repository *);
559 static const struct got_error *show_diff_view(struct tog_view *);
560 static const struct got_error *input_diff_view(struct tog_view **,
561 struct tog_view *, int);
562 static const struct got_error* close_diff_view(struct tog_view *);
563 static const struct got_error *search_start_diff_view(struct tog_view *);
564 static const struct got_error *search_next_diff_view(struct tog_view *);
566 static const struct got_error *open_log_view(struct tog_view *,
567 struct got_object_id *, struct got_repository *,
568 const char *, const char *, int);
569 static const struct got_error * show_log_view(struct tog_view *);
570 static const struct got_error *input_log_view(struct tog_view **,
571 struct tog_view *, int);
572 static const struct got_error *close_log_view(struct tog_view *);
573 static const struct got_error *search_start_log_view(struct tog_view *);
574 static const struct got_error *search_next_log_view(struct tog_view *);
576 static const struct got_error *open_blame_view(struct tog_view *, char *,
577 struct got_object_id *, struct got_repository *);
578 static const struct got_error *show_blame_view(struct tog_view *);
579 static const struct got_error *input_blame_view(struct tog_view **,
580 struct tog_view *, int);
581 static const struct got_error *close_blame_view(struct tog_view *);
582 static const struct got_error *search_start_blame_view(struct tog_view *);
583 static const struct got_error *search_next_blame_view(struct tog_view *);
585 static const struct got_error *open_tree_view(struct tog_view *,
586 struct got_object_id *, const char *, struct got_repository *);
587 static const struct got_error *show_tree_view(struct tog_view *);
588 static const struct got_error *input_tree_view(struct tog_view **,
589 struct tog_view *, int);
590 static const struct got_error *close_tree_view(struct tog_view *);
591 static const struct got_error *search_start_tree_view(struct tog_view *);
592 static const struct got_error *search_next_tree_view(struct tog_view *);
594 static const struct got_error *open_ref_view(struct tog_view *,
595 struct got_repository *);
596 static const struct got_error *show_ref_view(struct tog_view *);
597 static const struct got_error *input_ref_view(struct tog_view **,
598 struct tog_view *, int);
599 static const struct got_error *close_ref_view(struct tog_view *);
600 static const struct got_error *search_start_ref_view(struct tog_view *);
601 static const struct got_error *search_next_ref_view(struct tog_view *);
603 static volatile sig_atomic_t tog_sigwinch_received;
604 static volatile sig_atomic_t tog_sigpipe_received;
605 static volatile sig_atomic_t tog_sigcont_received;
608 tog_sigwinch(int signo)
610 tog_sigwinch_received = 1;
614 tog_sigpipe(int signo)
616 tog_sigpipe_received = 1;
620 tog_sigcont(int signo)
622 tog_sigcont_received = 1;
625 static const struct got_error *
626 view_close(struct tog_view *view)
628 const struct got_error *err = NULL;
631 view_close(view->child);
635 err = view->close(view);
637 del_panel(view->panel);
639 delwin(view->window);
644 static struct tog_view *
645 view_open(int nlines, int ncols, int begin_y, int begin_x,
646 enum tog_view_type type)
648 struct tog_view *view = calloc(1, sizeof(*view));
656 view->nlines = nlines ? nlines : LINES - begin_y;
657 view->ncols = ncols ? ncols : COLS - begin_x;
658 view->begin_y = begin_y;
659 view->begin_x = begin_x;
660 view->window = newwin(nlines, ncols, begin_y, begin_x);
661 if (view->window == NULL) {
665 view->panel = new_panel(view->window);
666 if (view->panel == NULL ||
667 set_panel_userptr(view->panel, view) != OK) {
672 keypad(view->window, TRUE);
677 view_split_begin_x(int begin_x)
679 if (begin_x > 0 || COLS < 120)
681 return (COLS - MAX(COLS / 2, 80));
684 static const struct got_error *view_resize(struct tog_view *);
686 static const struct got_error *
687 view_splitscreen(struct tog_view *view)
689 const struct got_error *err = NULL;
692 view->begin_x = view_split_begin_x(0);
693 view->nlines = LINES;
694 view->ncols = COLS - view->begin_x;
697 err = view_resize(view);
701 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
702 return got_error_from_errno("mvwin");
707 static const struct got_error *
708 view_fullscreen(struct tog_view *view)
710 const struct got_error *err = NULL;
714 view->nlines = LINES;
718 err = view_resize(view);
722 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
723 return got_error_from_errno("mvwin");
729 view_is_parent_view(struct tog_view *view)
731 return view->parent == NULL;
734 static const struct got_error *
735 view_resize(struct tog_view *view)
739 if (view->lines > LINES)
740 nlines = view->nlines - (view->lines - LINES);
742 nlines = view->nlines + (LINES - view->lines);
744 if (view->cols > COLS)
745 ncols = view->ncols - (view->cols - COLS);
747 ncols = view->ncols + (COLS - view->cols);
749 if (wresize(view->window, nlines, ncols) == ERR)
750 return got_error_from_errno("wresize");
751 if (replace_panel(view->panel, view->window) == ERR)
752 return got_error_from_errno("replace_panel");
753 wclear(view->window);
755 view->nlines = nlines;
761 view->child->begin_x = view_split_begin_x(view->begin_x);
762 if (view->child->begin_x == 0) {
763 view_fullscreen(view->child);
764 if (view->child->focussed)
765 show_panel(view->child->panel);
767 show_panel(view->panel);
769 view_splitscreen(view->child);
770 show_panel(view->child->panel);
777 static const struct got_error *
778 view_close_child(struct tog_view *view)
780 const struct got_error *err = NULL;
782 if (view->child == NULL)
785 err = view_close(view->child);
791 view_set_child(struct tog_view *view, struct tog_view *child)
794 child->parent = view;
798 view_is_splitscreen(struct tog_view *view)
800 return view->begin_x > 0;
809 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
810 cols = 80; /* Default */
816 resize_term(lines, cols);
819 static const struct got_error *
820 view_search_start(struct tog_view *view)
822 const struct got_error *err = NULL;
826 if (view->search_started) {
827 regfree(&view->regex);
829 memset(&view->regmatch, 0, sizeof(view->regmatch));
831 view->search_started = 0;
833 if (view->nlines < 1)
836 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
837 wclrtoeol(view->window);
841 ret = wgetnstr(view->window, pattern, sizeof(pattern));
847 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
848 err = view->search_start(view);
850 regfree(&view->regex);
853 view->search_started = 1;
854 view->searching = TOG_SEARCH_FORWARD;
855 view->search_next_done = 0;
856 view->search_next(view);
862 static const struct got_error *
863 view_input(struct tog_view **new, int *done, struct tog_view *view,
864 struct tog_view_list_head *views)
866 const struct got_error *err = NULL;
872 /* Clear "no matches" indicator. */
873 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
874 view->search_next_done == TOG_SEARCH_HAVE_NONE)
875 view->search_next_done = TOG_SEARCH_HAVE_MORE;
877 if (view->searching && !view->search_next_done) {
878 errcode = pthread_mutex_unlock(&tog_mutex);
880 return got_error_set_errno(errcode,
881 "pthread_mutex_unlock");
883 errcode = pthread_mutex_lock(&tog_mutex);
885 return got_error_set_errno(errcode,
886 "pthread_mutex_lock");
887 view->search_next(view);
891 nodelay(stdscr, FALSE);
892 /* Allow threads to make progress while we are waiting for input. */
893 errcode = pthread_mutex_unlock(&tog_mutex);
895 return got_error_set_errno(errcode, "pthread_mutex_unlock");
896 ch = wgetch(view->window);
897 errcode = pthread_mutex_lock(&tog_mutex);
899 return got_error_set_errno(errcode, "pthread_mutex_lock");
900 nodelay(stdscr, TRUE);
902 if (tog_sigwinch_received || tog_sigcont_received) {
904 tog_sigwinch_received = 0;
905 tog_sigcont_received = 0;
906 TAILQ_FOREACH(v, views, entry) {
907 err = view_resize(v);
910 err = v->input(new, v, KEY_RESIZE);
914 err = view_resize(v->child);
917 err = v->child->input(new, v->child,
929 view->child->focussed = 1;
930 view->focus_child = 1;
931 } else if (view->parent) {
933 view->parent->focussed = 1;
934 view->parent->focus_child = 0;
938 err = view->input(new, view, ch);
945 if (view_is_parent_view(view)) {
946 if (view->child == NULL)
948 if (view_is_splitscreen(view->child)) {
950 view->child->focussed = 1;
951 err = view_fullscreen(view->child);
953 err = view_splitscreen(view->child);
956 err = view->child->input(new, view->child,
959 if (view_is_splitscreen(view)) {
960 view->parent->focussed = 0;
962 err = view_fullscreen(view);
964 err = view_splitscreen(view);
968 err = view->input(new, view, KEY_RESIZE);
974 if (view->search_start)
975 view_search_start(view);
977 err = view->input(new, view, ch);
981 if (view->search_started && view->search_next) {
982 view->searching = (ch == 'n' ?
983 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
984 view->search_next_done = 0;
985 view->search_next(view);
987 err = view->input(new, view, ch);
990 err = view->input(new, view, ch);
998 view_vborder(struct tog_view *view)
1001 const struct tog_view *view_above;
1004 return view_vborder(view->parent);
1006 panel = panel_above(view->panel);
1010 view_above = panel_userptr(panel);
1011 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1012 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1016 view_needs_focus_indication(struct tog_view *view)
1018 if (view_is_parent_view(view)) {
1019 if (view->child == NULL || view->child->focussed)
1021 if (!view_is_splitscreen(view->child))
1023 } else if (!view_is_splitscreen(view))
1026 return view->focussed;
1029 static const struct got_error *
1030 view_loop(struct tog_view *view)
1032 const struct got_error *err = NULL;
1033 struct tog_view_list_head views;
1034 struct tog_view *new_view;
1035 int fast_refresh = 10;
1036 int done = 0, errcode;
1038 errcode = pthread_mutex_lock(&tog_mutex);
1040 return got_error_set_errno(errcode, "pthread_mutex_lock");
1043 TAILQ_INSERT_HEAD(&views, view, entry);
1046 err = view->show(view);
1051 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1052 /* Refresh fast during initialization, then become slower. */
1053 if (fast_refresh && fast_refresh-- == 0)
1054 halfdelay(10); /* switch to once per second */
1056 err = view_input(&new_view, &done, view, &views);
1060 struct tog_view *v, *prev = NULL;
1062 if (view_is_parent_view(view))
1063 prev = TAILQ_PREV(view, tog_view_list_head,
1065 else if (view->parent)
1066 prev = view->parent;
1069 view->parent->child = NULL;
1070 view->parent->focus_child = 0;
1072 TAILQ_REMOVE(&views, view, entry);
1074 err = view_close(view);
1079 TAILQ_FOREACH(v, &views, entry) {
1083 if (view == NULL && new_view == NULL) {
1084 /* No view has focus. Try to pick one. */
1087 else if (!TAILQ_EMPTY(&views)) {
1088 view = TAILQ_LAST(&views,
1089 tog_view_list_head);
1092 if (view->focus_child) {
1093 view->child->focussed = 1;
1101 struct tog_view *v, *t;
1102 /* Only allow one parent view per type. */
1103 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1104 if (v->type != new_view->type)
1106 TAILQ_REMOVE(&views, v, entry);
1107 err = view_close(v);
1112 TAILQ_INSERT_TAIL(&views, new_view, entry);
1116 if (view_is_parent_view(view)) {
1117 if (view->child && view->child->focussed)
1120 if (view->parent && view->parent->focussed)
1121 view = view->parent;
1123 show_panel(view->panel);
1124 if (view->child && view_is_splitscreen(view->child))
1125 show_panel(view->child->panel);
1126 if (view->parent && view_is_splitscreen(view)) {
1127 err = view->parent->show(view->parent);
1131 err = view->show(view);
1135 err = view->child->show(view->child);
1144 while (!TAILQ_EMPTY(&views)) {
1145 view = TAILQ_FIRST(&views);
1146 TAILQ_REMOVE(&views, view, entry);
1150 errcode = pthread_mutex_unlock(&tog_mutex);
1151 if (errcode && err == NULL)
1152 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1162 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1167 /* Create newly allocated wide-character string equivalent to a byte string. */
1168 static const struct got_error *
1169 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1172 const struct got_error *err = NULL;
1175 *wlen = mbstowcs(NULL, s, 0);
1176 if (*wlen == (size_t)-1) {
1178 if (errno != EILSEQ)
1179 return got_error_from_errno("mbstowcs");
1181 /* byte string invalid in current encoding; try to "fix" it */
1182 err = got_mbsavis(&vis, &vislen, s);
1185 *wlen = mbstowcs(NULL, vis, 0);
1186 if (*wlen == (size_t)-1) {
1187 err = got_error_from_errno("mbstowcs"); /* give up */
1192 *ws = calloc(*wlen + 1, sizeof(**ws));
1194 err = got_error_from_errno("calloc");
1198 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1199 err = got_error_from_errno("mbstowcs");
1210 /* Format a line for display, ensuring that it won't overflow a width limit. */
1211 static const struct got_error *
1212 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1215 const struct got_error *err = NULL;
1217 wchar_t *wline = NULL;
1224 err = mbs2ws(&wline, &wlen, line);
1228 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1229 wline[wlen - 1] = L'\0';
1232 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1233 wline[wlen - 1] = L'\0';
1239 int width = wcwidth(wline[i]);
1246 if (width == 1 || width == 2) {
1247 if (cols + width > wlimit)
1251 } else if (width == -1) {
1252 if (wline[i] == L'\t') {
1254 ((cols + col_tab_align) % TABSIZE);
1259 if (cols + width > wlimit)
1264 err = got_error_from_errno("wcwidth");
1279 static const struct got_error*
1280 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1281 struct got_object_id *id, struct got_repository *repo)
1283 static const struct got_error *err = NULL;
1284 struct got_reflist_entry *re;
1290 TAILQ_FOREACH(re, refs, entry) {
1291 struct got_tag_object *tag = NULL;
1292 struct got_object_id *ref_id;
1295 name = got_ref_get_name(re->ref);
1296 if (strcmp(name, GOT_REF_HEAD) == 0)
1298 if (strncmp(name, "refs/", 5) == 0)
1300 if (strncmp(name, "got/", 4) == 0 &&
1301 strncmp(name, "got/backup/", 11) != 0)
1303 if (strncmp(name, "heads/", 6) == 0)
1305 if (strncmp(name, "remotes/", 8) == 0) {
1307 s = strstr(name, "/" GOT_REF_HEAD);
1308 if (s != NULL && s[strlen(s)] == '\0')
1311 err = got_ref_resolve(&ref_id, repo, re->ref);
1314 if (strncmp(name, "tags/", 5) == 0) {
1315 err = got_object_open_as_tag(&tag, repo, ref_id);
1317 if (err->code != GOT_ERR_OBJ_TYPE) {
1321 /* Ref points at something other than a tag. */
1326 cmp = got_object_id_cmp(tag ?
1327 got_object_tag_get_object_id(tag) : ref_id, id);
1330 got_object_tag_close(tag);
1334 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1335 s ? ", " : "", name) == -1) {
1336 err = got_error_from_errno("asprintf");
1347 static const struct got_error *
1348 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1353 smallerthan = strchr(author, '<');
1354 if (smallerthan && smallerthan[1] != '\0')
1355 author = smallerthan + 1;
1356 author[strcspn(author, "@>")] = '\0';
1357 return format_line(wauthor, author_width, author, limit, col_tab_align);
1360 static const struct got_error *
1361 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1362 struct got_object_id *id, const size_t date_display_cols,
1363 int author_display_cols)
1365 struct tog_log_view_state *s = &view->state.log;
1366 const struct got_error *err = NULL;
1367 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1368 char *logmsg0 = NULL, *logmsg = NULL;
1369 char *author = NULL;
1370 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1371 int author_width, logmsg_width;
1372 char *newline, *line = NULL;
1374 const int avail = view->ncols;
1376 time_t committer_time;
1377 struct tog_color *tc;
1379 committer_time = got_object_commit_get_committer_time(commit);
1380 if (gmtime_r(&committer_time, &tm) == NULL)
1381 return got_error_from_errno("gmtime_r");
1382 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1383 return got_error(GOT_ERR_NO_SPACE);
1385 if (avail <= date_display_cols)
1386 limit = MIN(sizeof(datebuf) - 1, avail);
1388 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1389 tc = get_color(&s->colors, TOG_COLOR_DATE);
1391 wattr_on(view->window,
1392 COLOR_PAIR(tc->colorpair), NULL);
1393 waddnstr(view->window, datebuf, limit);
1395 wattr_off(view->window,
1396 COLOR_PAIR(tc->colorpair), NULL);
1403 err = got_object_id_str(&id_str, id);
1406 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1408 wattr_on(view->window,
1409 COLOR_PAIR(tc->colorpair), NULL);
1410 wprintw(view->window, "%.8s ", id_str);
1412 wattr_off(view->window,
1413 COLOR_PAIR(tc->colorpair), NULL);
1420 author = strdup(got_object_commit_get_author(commit));
1421 if (author == NULL) {
1422 err = got_error_from_errno("strdup");
1425 err = format_author(&wauthor, &author_width, author, avail - col, col);
1428 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1430 wattr_on(view->window,
1431 COLOR_PAIR(tc->colorpair), NULL);
1432 waddwstr(view->window, wauthor);
1434 wattr_off(view->window,
1435 COLOR_PAIR(tc->colorpair), NULL);
1436 col += author_width;
1437 while (col < avail && author_width < author_display_cols + 2) {
1438 waddch(view->window, ' ');
1445 err = got_object_commit_get_logmsg(&logmsg0, commit);
1449 while (*logmsg == '\n')
1451 newline = strchr(logmsg, '\n');
1454 limit = avail - col;
1455 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1458 waddwstr(view->window, wlogmsg);
1459 col += logmsg_width;
1460 while (col < avail) {
1461 waddch(view->window, ' ');
1473 static struct commit_queue_entry *
1474 alloc_commit_queue_entry(struct got_commit_object *commit,
1475 struct got_object_id *id)
1477 struct commit_queue_entry *entry;
1479 entry = calloc(1, sizeof(*entry));
1484 entry->commit = commit;
1489 pop_commit(struct commit_queue *commits)
1491 struct commit_queue_entry *entry;
1493 entry = TAILQ_FIRST(&commits->head);
1494 TAILQ_REMOVE(&commits->head, entry, entry);
1495 got_object_commit_close(entry->commit);
1496 commits->ncommits--;
1497 /* Don't free entry->id! It is owned by the commit graph. */
1502 free_commits(struct commit_queue *commits)
1504 while (!TAILQ_EMPTY(&commits->head))
1505 pop_commit(commits);
1508 static const struct got_error *
1509 match_commit(int *have_match, struct got_object_id *id,
1510 struct got_commit_object *commit, regex_t *regex)
1512 const struct got_error *err = NULL;
1513 regmatch_t regmatch;
1514 char *id_str = NULL, *logmsg = NULL;
1518 err = got_object_id_str(&id_str, id);
1522 err = got_object_commit_get_logmsg(&logmsg, commit);
1526 if (regexec(regex, got_object_commit_get_author(commit), 1,
1527 ®match, 0) == 0 ||
1528 regexec(regex, got_object_commit_get_committer(commit), 1,
1529 ®match, 0) == 0 ||
1530 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1531 regexec(regex, logmsg, 1, ®match, 0) == 0)
1539 static const struct got_error *
1540 queue_commits(struct tog_log_thread_args *a)
1542 const struct got_error *err = NULL;
1545 * We keep all commits open throughout the lifetime of the log
1546 * view in order to avoid having to re-fetch commits from disk
1547 * while updating the display.
1550 struct got_object_id *id;
1551 struct got_commit_object *commit;
1552 struct commit_queue_entry *entry;
1555 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1557 if (err || id == NULL)
1560 err = got_object_open_as_commit(&commit, a->repo, id);
1563 entry = alloc_commit_queue_entry(commit, id);
1564 if (entry == NULL) {
1565 err = got_error_from_errno("alloc_commit_queue_entry");
1569 errcode = pthread_mutex_lock(&tog_mutex);
1571 err = got_error_set_errno(errcode,
1572 "pthread_mutex_lock");
1576 entry->idx = a->commits->ncommits;
1577 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1578 a->commits->ncommits++;
1580 if (*a->searching == TOG_SEARCH_FORWARD &&
1581 !*a->search_next_done) {
1583 err = match_commit(&have_match, id, commit, a->regex);
1587 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1590 errcode = pthread_mutex_unlock(&tog_mutex);
1591 if (errcode && err == NULL)
1592 err = got_error_set_errno(errcode,
1593 "pthread_mutex_unlock");
1596 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1602 select_commit(struct tog_log_view_state *s)
1604 struct commit_queue_entry *entry;
1607 entry = s->first_displayed_entry;
1609 if (ncommits == s->selected) {
1610 s->selected_entry = entry;
1613 entry = TAILQ_NEXT(entry, entry);
1618 static const struct got_error *
1619 draw_commits(struct tog_view *view)
1621 const struct got_error *err = NULL;
1622 struct tog_log_view_state *s = &view->state.log;
1623 struct commit_queue_entry *entry = s->selected_entry;
1624 const int limit = view->nlines;
1626 int ncommits, author_cols = 4;
1627 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1628 char *refs_str = NULL;
1630 struct tog_color *tc;
1631 static const size_t date_display_cols = 12;
1633 if (s->selected_entry &&
1634 !(view->searching && view->search_next_done == 0)) {
1635 struct got_reflist_head *refs;
1636 err = got_object_id_str(&id_str, s->selected_entry->id);
1639 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1640 s->selected_entry->id);
1642 err = build_refs_str(&refs_str, refs,
1643 s->selected_entry->id, s->repo);
1649 if (s->thread_args.commits_needed == 0)
1650 halfdelay(10); /* disable fast refresh */
1652 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1653 if (asprintf(&ncommits_str, " [%d/%d] %s",
1654 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1655 (view->searching && !view->search_next_done) ?
1656 "searching..." : "loading...") == -1) {
1657 err = got_error_from_errno("asprintf");
1661 const char *search_str = NULL;
1663 if (view->searching) {
1664 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1665 search_str = "no more matches";
1666 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1667 search_str = "no matches found";
1668 else if (!view->search_next_done)
1669 search_str = "searching...";
1672 if (asprintf(&ncommits_str, " [%d/%d] %s",
1673 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1674 search_str ? search_str :
1675 (refs_str ? refs_str : "")) == -1) {
1676 err = got_error_from_errno("asprintf");
1681 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1682 if (asprintf(&header, "commit %s %s%s",
1683 id_str ? id_str : "........................................",
1684 s->in_repo_path, ncommits_str) == -1) {
1685 err = got_error_from_errno("asprintf");
1689 } else if (asprintf(&header, "commit %s%s",
1690 id_str ? id_str : "........................................",
1691 ncommits_str) == -1) {
1692 err = got_error_from_errno("asprintf");
1696 err = format_line(&wline, &width, header, view->ncols, 0);
1700 werase(view->window);
1702 if (view_needs_focus_indication(view))
1703 wstandout(view->window);
1704 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1706 wattr_on(view->window,
1707 COLOR_PAIR(tc->colorpair), NULL);
1708 waddwstr(view->window, wline);
1710 wattr_off(view->window,
1711 COLOR_PAIR(tc->colorpair), NULL);
1712 while (width < view->ncols) {
1713 waddch(view->window, ' ');
1716 if (view_needs_focus_indication(view))
1717 wstandend(view->window);
1722 /* Grow author column size if necessary. */
1723 entry = s->first_displayed_entry;
1729 if (ncommits >= limit - 1)
1731 author = strdup(got_object_commit_get_author(entry->commit));
1732 if (author == NULL) {
1733 err = got_error_from_errno("strdup");
1736 err = format_author(&wauthor, &width, author, COLS,
1738 if (author_cols < width)
1739 author_cols = width;
1743 entry = TAILQ_NEXT(entry, entry);
1746 entry = s->first_displayed_entry;
1747 s->last_displayed_entry = s->first_displayed_entry;
1750 if (ncommits >= limit - 1)
1752 if (ncommits == s->selected)
1753 wstandout(view->window);
1754 err = draw_commit(view, entry->commit, entry->id,
1755 date_display_cols, author_cols);
1756 if (ncommits == s->selected)
1757 wstandend(view->window);
1761 s->last_displayed_entry = entry;
1762 entry = TAILQ_NEXT(entry, entry);
1777 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1779 struct commit_queue_entry *entry;
1782 entry = TAILQ_FIRST(&s->commits.head);
1783 if (s->first_displayed_entry == entry)
1786 entry = s->first_displayed_entry;
1787 while (entry && nscrolled < maxscroll) {
1788 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1790 s->first_displayed_entry = entry;
1796 static const struct got_error *
1797 trigger_log_thread(struct tog_view *view, int wait)
1799 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1802 halfdelay(1); /* fast refresh while loading commits */
1804 while (ta->commits_needed > 0 || ta->load_all) {
1805 if (ta->log_complete)
1808 /* Wake the log thread. */
1809 errcode = pthread_cond_signal(&ta->need_commits);
1811 return got_error_set_errno(errcode,
1812 "pthread_cond_signal");
1815 * The mutex will be released while the view loop waits
1816 * in wgetch(), at which time the log thread will run.
1821 /* Display progress update in log view. */
1822 show_log_view(view);
1826 /* Wait right here while next commit is being loaded. */
1827 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1829 return got_error_set_errno(errcode,
1830 "pthread_cond_wait");
1832 /* Display progress update in log view. */
1833 show_log_view(view);
1841 static const struct got_error *
1842 log_scroll_down(struct tog_view *view, int maxscroll)
1844 struct tog_log_view_state *s = &view->state.log;
1845 const struct got_error *err = NULL;
1846 struct commit_queue_entry *pentry;
1847 int nscrolled = 0, ncommits_needed;
1849 if (s->last_displayed_entry == NULL)
1852 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1853 if (s->commits.ncommits < ncommits_needed &&
1854 !s->thread_args.log_complete) {
1856 * Ask the log thread for required amount of commits.
1858 s->thread_args.commits_needed += maxscroll;
1859 err = trigger_log_thread(view, 1);
1865 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1869 s->last_displayed_entry = pentry;
1871 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1874 s->first_displayed_entry = pentry;
1875 } while (++nscrolled < maxscroll);
1880 static const struct got_error *
1881 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1882 struct got_commit_object *commit, struct got_object_id *commit_id,
1883 struct tog_view *log_view, struct got_repository *repo)
1885 const struct got_error *err;
1886 struct got_object_qid *parent_id;
1887 struct tog_view *diff_view;
1889 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1890 if (diff_view == NULL)
1891 return got_error_from_errno("view_open");
1893 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1894 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1895 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1897 *new_view = diff_view;
1901 static const struct got_error *
1902 tree_view_visit_subtree(struct tog_tree_view_state *s,
1903 struct got_tree_object *subtree)
1905 struct tog_parent_tree *parent;
1907 parent = calloc(1, sizeof(*parent));
1909 return got_error_from_errno("calloc");
1911 parent->tree = s->tree;
1912 parent->first_displayed_entry = s->first_displayed_entry;
1913 parent->selected_entry = s->selected_entry;
1914 parent->selected = s->selected;
1915 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1918 s->first_displayed_entry = NULL;
1922 static const struct got_error *
1923 tree_view_walk_path(struct tog_tree_view_state *s,
1924 struct got_commit_object *commit, const char *path)
1926 const struct got_error *err = NULL;
1927 struct got_tree_object *tree = NULL;
1929 char *slash, *subpath = NULL;
1931 /* Walk the path and open corresponding tree objects. */
1934 struct got_tree_entry *te;
1935 struct got_object_id *tree_id;
1941 /* Ensure the correct subtree entry is selected. */
1942 slash = strchr(p, '/');
1944 te_name = strdup(p);
1946 te_name = strndup(p, slash - p);
1947 if (te_name == NULL) {
1948 err = got_error_from_errno("strndup");
1951 te = got_object_tree_find_entry(s->tree, te_name);
1953 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1958 s->first_displayed_entry = s->selected_entry = te;
1960 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1961 break; /* jump to this file's entry */
1963 slash = strchr(p, '/');
1965 subpath = strndup(path, slash - path);
1967 subpath = strdup(path);
1968 if (subpath == NULL) {
1969 err = got_error_from_errno("strdup");
1973 err = got_object_id_by_path(&tree_id, s->repo, commit,
1978 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1983 err = tree_view_visit_subtree(s, tree);
1985 got_object_tree_close(tree);
1999 static const struct got_error *
2000 browse_commit_tree(struct tog_view **new_view, int begin_x,
2001 struct commit_queue_entry *entry, const char *path,
2002 const char *head_ref_name, struct got_repository *repo)
2004 const struct got_error *err = NULL;
2005 struct tog_tree_view_state *s;
2006 struct tog_view *tree_view;
2008 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2009 if (tree_view == NULL)
2010 return got_error_from_errno("view_open");
2012 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2015 s = &tree_view->state.tree;
2017 *new_view = tree_view;
2019 if (got_path_is_root_dir(path))
2022 return tree_view_walk_path(s, entry->commit, path);
2025 static const struct got_error *
2026 block_signals_used_by_main_thread(void)
2031 if (sigemptyset(&sigset) == -1)
2032 return got_error_from_errno("sigemptyset");
2034 /* tog handles SIGWINCH and SIGCONT */
2035 if (sigaddset(&sigset, SIGWINCH) == -1)
2036 return got_error_from_errno("sigaddset");
2037 if (sigaddset(&sigset, SIGCONT) == -1)
2038 return got_error_from_errno("sigaddset");
2040 /* ncurses handles SIGTSTP */
2041 if (sigaddset(&sigset, SIGTSTP) == -1)
2042 return got_error_from_errno("sigaddset");
2044 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2046 return got_error_set_errno(errcode, "pthread_sigmask");
2052 log_thread(void *arg)
2054 const struct got_error *err = NULL;
2056 struct tog_log_thread_args *a = arg;
2059 err = block_signals_used_by_main_thread();
2063 while (!done && !err && !tog_sigpipe_received) {
2064 err = queue_commits(a);
2066 if (err->code != GOT_ERR_ITER_COMPLETED)
2070 } else if (a->commits_needed > 0 && !a->load_all)
2071 a->commits_needed--;
2073 errcode = pthread_mutex_lock(&tog_mutex);
2075 err = got_error_set_errno(errcode,
2076 "pthread_mutex_lock");
2078 } else if (*a->quit)
2080 else if (*a->first_displayed_entry == NULL) {
2081 *a->first_displayed_entry =
2082 TAILQ_FIRST(&a->commits->head);
2083 *a->selected_entry = *a->first_displayed_entry;
2086 errcode = pthread_cond_signal(&a->commit_loaded);
2088 err = got_error_set_errno(errcode,
2089 "pthread_cond_signal");
2090 pthread_mutex_unlock(&tog_mutex);
2095 a->commits_needed = 0;
2097 if (a->commits_needed == 0 && !a->load_all) {
2098 errcode = pthread_cond_wait(&a->need_commits,
2101 err = got_error_set_errno(errcode,
2102 "pthread_cond_wait");
2108 errcode = pthread_mutex_unlock(&tog_mutex);
2109 if (errcode && err == NULL)
2110 err = got_error_set_errno(errcode,
2111 "pthread_mutex_unlock");
2113 a->log_complete = 1;
2117 static const struct got_error *
2118 stop_log_thread(struct tog_log_view_state *s)
2120 const struct got_error *err = NULL;
2125 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2127 return got_error_set_errno(errcode,
2128 "pthread_cond_signal");
2129 errcode = pthread_mutex_unlock(&tog_mutex);
2131 return got_error_set_errno(errcode,
2132 "pthread_mutex_unlock");
2133 errcode = pthread_join(s->thread, (void **)&err);
2135 return got_error_set_errno(errcode, "pthread_join");
2136 errcode = pthread_mutex_lock(&tog_mutex);
2138 return got_error_set_errno(errcode,
2139 "pthread_mutex_lock");
2140 s->thread = 0; //NULL;
2143 if (s->thread_args.repo) {
2144 err = got_repo_close(s->thread_args.repo);
2145 s->thread_args.repo = NULL;
2148 if (s->thread_args.graph) {
2149 got_commit_graph_close(s->thread_args.graph);
2150 s->thread_args.graph = NULL;
2156 static const struct got_error *
2157 close_log_view(struct tog_view *view)
2159 const struct got_error *err = NULL;
2160 struct tog_log_view_state *s = &view->state.log;
2163 err = stop_log_thread(s);
2165 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2166 if (errcode && err == NULL)
2167 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2169 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2170 if (errcode && err == NULL)
2171 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2173 free_commits(&s->commits);
2174 free(s->in_repo_path);
2175 s->in_repo_path = NULL;
2178 free(s->head_ref_name);
2179 s->head_ref_name = NULL;
2183 static const struct got_error *
2184 search_start_log_view(struct tog_view *view)
2186 struct tog_log_view_state *s = &view->state.log;
2188 s->matched_entry = NULL;
2189 s->search_entry = NULL;
2193 static const struct got_error *
2194 search_next_log_view(struct tog_view *view)
2196 const struct got_error *err = NULL;
2197 struct tog_log_view_state *s = &view->state.log;
2198 struct commit_queue_entry *entry;
2200 /* Display progress update in log view. */
2201 show_log_view(view);
2205 if (s->search_entry) {
2207 errcode = pthread_mutex_unlock(&tog_mutex);
2209 return got_error_set_errno(errcode,
2210 "pthread_mutex_unlock");
2211 ch = wgetch(view->window);
2212 errcode = pthread_mutex_lock(&tog_mutex);
2214 return got_error_set_errno(errcode,
2215 "pthread_mutex_lock");
2216 if (ch == KEY_BACKSPACE) {
2217 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2220 if (view->searching == TOG_SEARCH_FORWARD)
2221 entry = TAILQ_NEXT(s->search_entry, entry);
2223 entry = TAILQ_PREV(s->search_entry,
2224 commit_queue_head, entry);
2225 } else if (s->matched_entry) {
2226 if (view->searching == TOG_SEARCH_FORWARD)
2227 entry = TAILQ_NEXT(s->matched_entry, entry);
2229 entry = TAILQ_PREV(s->matched_entry,
2230 commit_queue_head, entry);
2232 entry = s->selected_entry;
2238 if (entry == NULL) {
2239 if (s->thread_args.log_complete ||
2240 view->searching == TOG_SEARCH_BACKWARD) {
2241 view->search_next_done =
2242 (s->matched_entry == NULL ?
2243 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2244 s->search_entry = NULL;
2248 * Poke the log thread for more commits and return,
2249 * allowing the main loop to make progress. Search
2250 * will resume at s->search_entry once we come back.
2252 s->thread_args.commits_needed++;
2253 return trigger_log_thread(view, 0);
2256 err = match_commit(&have_match, entry->id, entry->commit,
2261 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2262 s->matched_entry = entry;
2266 s->search_entry = entry;
2267 if (view->searching == TOG_SEARCH_FORWARD)
2268 entry = TAILQ_NEXT(entry, entry);
2270 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2273 if (s->matched_entry) {
2274 int cur = s->selected_entry->idx;
2275 while (cur < s->matched_entry->idx) {
2276 err = input_log_view(NULL, view, KEY_DOWN);
2281 while (cur > s->matched_entry->idx) {
2282 err = input_log_view(NULL, view, KEY_UP);
2289 s->search_entry = NULL;
2294 static const struct got_error *
2295 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2296 struct got_repository *repo, const char *head_ref_name,
2297 const char *in_repo_path, int log_branches)
2299 const struct got_error *err = NULL;
2300 struct tog_log_view_state *s = &view->state.log;
2301 struct got_repository *thread_repo = NULL;
2302 struct got_commit_graph *thread_graph = NULL;
2305 if (in_repo_path != s->in_repo_path) {
2306 free(s->in_repo_path);
2307 s->in_repo_path = strdup(in_repo_path);
2308 if (s->in_repo_path == NULL)
2309 return got_error_from_errno("strdup");
2312 /* The commit queue only contains commits being displayed. */
2313 TAILQ_INIT(&s->commits.head);
2314 s->commits.ncommits = 0;
2317 if (head_ref_name) {
2318 s->head_ref_name = strdup(head_ref_name);
2319 if (s->head_ref_name == NULL) {
2320 err = got_error_from_errno("strdup");
2324 s->start_id = got_object_id_dup(start_id);
2325 if (s->start_id == NULL) {
2326 err = got_error_from_errno("got_object_id_dup");
2329 s->log_branches = log_branches;
2331 STAILQ_INIT(&s->colors);
2332 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2333 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2334 get_color_value("TOG_COLOR_COMMIT"));
2337 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2338 get_color_value("TOG_COLOR_AUTHOR"));
2340 free_colors(&s->colors);
2343 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2344 get_color_value("TOG_COLOR_DATE"));
2346 free_colors(&s->colors);
2351 view->show = show_log_view;
2352 view->input = input_log_view;
2353 view->close = close_log_view;
2354 view->search_start = search_start_log_view;
2355 view->search_next = search_next_log_view;
2357 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2360 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2364 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2365 s->repo, NULL, NULL);
2369 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2371 err = got_error_set_errno(errcode, "pthread_cond_init");
2374 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2376 err = got_error_set_errno(errcode, "pthread_cond_init");
2380 s->thread_args.commits_needed = view->nlines;
2381 s->thread_args.graph = thread_graph;
2382 s->thread_args.commits = &s->commits;
2383 s->thread_args.in_repo_path = s->in_repo_path;
2384 s->thread_args.start_id = s->start_id;
2385 s->thread_args.repo = thread_repo;
2386 s->thread_args.log_complete = 0;
2387 s->thread_args.quit = &s->quit;
2388 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2389 s->thread_args.selected_entry = &s->selected_entry;
2390 s->thread_args.searching = &view->searching;
2391 s->thread_args.search_next_done = &view->search_next_done;
2392 s->thread_args.regex = &view->regex;
2395 close_log_view(view);
2399 static const struct got_error *
2400 show_log_view(struct tog_view *view)
2402 const struct got_error *err;
2403 struct tog_log_view_state *s = &view->state.log;
2405 if (s->thread == 0) { //NULL) {
2406 int errcode = pthread_create(&s->thread, NULL, log_thread,
2409 return got_error_set_errno(errcode, "pthread_create");
2410 if (s->thread_args.commits_needed > 0) {
2411 err = trigger_log_thread(view, 1);
2417 return draw_commits(view);
2420 static const struct got_error *
2421 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2423 const struct got_error *err = NULL;
2424 struct tog_log_view_state *s = &view->state.log;
2425 struct tog_view *diff_view = NULL, *tree_view = NULL;
2426 struct tog_view *ref_view = NULL;
2427 struct commit_queue_entry *entry;
2430 if (s->thread_args.load_all) {
2431 if (ch == KEY_BACKSPACE)
2432 s->thread_args.load_all = 0;
2433 else if (s->thread_args.log_complete) {
2434 s->thread_args.load_all = 0;
2435 log_scroll_down(view, s->commits.ncommits);
2436 s->selected = MIN(view->nlines - 2,
2437 s->commits.ncommits - 1);
2452 if (s->first_displayed_entry == NULL)
2454 if (s->selected > 0)
2457 log_scroll_up(s, 1);
2463 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2468 if (s->first_displayed_entry == NULL)
2470 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2473 log_scroll_up(s, view->nlines - 1);
2481 if (s->first_displayed_entry == NULL)
2483 if (s->selected < MIN(view->nlines - 2,
2484 s->commits.ncommits - 1))
2487 err = log_scroll_down(view, 1);
2495 /* We don't know yet how many commits, so we're forced to
2496 * traverse them all. */
2497 if (!s->thread_args.log_complete) {
2498 s->thread_args.load_all = 1;
2499 return trigger_log_thread(view, 0);
2503 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2504 for (n = 0; n < view->nlines - 1; n++) {
2507 s->first_displayed_entry = entry;
2508 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2511 s->selected = n - 1;
2517 struct commit_queue_entry *first;
2518 first = s->first_displayed_entry;
2521 err = log_scroll_down(view, view->nlines - 1);
2524 if (first == s->first_displayed_entry &&
2525 s->selected < MIN(view->nlines - 2,
2526 s->commits.ncommits - 1)) {
2527 /* can't scroll further down */
2528 s->selected = MIN(view->nlines - 2,
2529 s->commits.ncommits - 1);
2535 if (s->selected > view->nlines - 2)
2536 s->selected = view->nlines - 2;
2537 if (s->selected > s->commits.ncommits - 1)
2538 s->selected = s->commits.ncommits - 1;
2540 if (s->commits.ncommits < view->nlines - 1 &&
2541 !s->thread_args.log_complete) {
2542 s->thread_args.commits_needed += (view->nlines - 1) -
2543 s->commits.ncommits;
2544 err = trigger_log_thread(view, 1);
2550 if (s->selected_entry == NULL)
2552 if (view_is_parent_view(view))
2553 begin_x = view_split_begin_x(view->begin_x);
2554 err = open_diff_view_for_commit(&diff_view, begin_x,
2555 s->selected_entry->commit, s->selected_entry->id,
2560 diff_view->focussed = 1;
2561 if (view_is_parent_view(view)) {
2562 err = view_close_child(view);
2565 view_set_child(view, diff_view);
2566 view->focus_child = 1;
2568 *new_view = diff_view;
2571 if (s->selected_entry == NULL)
2573 if (view_is_parent_view(view))
2574 begin_x = view_split_begin_x(view->begin_x);
2575 err = browse_commit_tree(&tree_view, begin_x,
2576 s->selected_entry, s->in_repo_path, s->head_ref_name,
2581 tree_view->focussed = 1;
2582 if (view_is_parent_view(view)) {
2583 err = view_close_child(view);
2586 view_set_child(view, tree_view);
2587 view->focus_child = 1;
2589 *new_view = tree_view;
2594 if (ch == KEY_BACKSPACE &&
2595 got_path_is_root_dir(s->in_repo_path))
2597 err = stop_log_thread(s);
2600 if (ch == KEY_BACKSPACE) {
2602 err = got_path_dirname(&parent_path, s->in_repo_path);
2605 free(s->in_repo_path);
2606 s->in_repo_path = parent_path;
2607 s->thread_args.in_repo_path = s->in_repo_path;
2608 } else if (ch == CTRL('l')) {
2609 struct got_object_id *start_id;
2610 err = got_repo_match_object_id(&start_id, NULL,
2611 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2612 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2616 s->start_id = start_id;
2617 s->thread_args.start_id = s->start_id;
2619 s->log_branches = !s->log_branches;
2621 err = got_repo_open(&s->thread_args.repo,
2622 got_repo_get_path(s->repo), NULL);
2626 err = tog_load_refs(s->repo, 0);
2629 err = got_commit_graph_open(&s->thread_args.graph,
2630 s->in_repo_path, !s->log_branches);
2633 err = got_commit_graph_iter_start(s->thread_args.graph,
2634 s->start_id, s->repo, NULL, NULL);
2637 free_commits(&s->commits);
2638 s->first_displayed_entry = NULL;
2639 s->last_displayed_entry = NULL;
2640 s->selected_entry = NULL;
2642 s->thread_args.log_complete = 0;
2644 s->thread_args.commits_needed = view->nlines;
2647 if (view_is_parent_view(view))
2648 begin_x = view_split_begin_x(view->begin_x);
2649 ref_view = view_open(view->nlines, view->ncols,
2650 view->begin_y, begin_x, TOG_VIEW_REF);
2651 if (ref_view == NULL)
2652 return got_error_from_errno("view_open");
2653 err = open_ref_view(ref_view, s->repo);
2655 view_close(ref_view);
2659 ref_view->focussed = 1;
2660 if (view_is_parent_view(view)) {
2661 err = view_close_child(view);
2664 view_set_child(view, ref_view);
2665 view->focus_child = 1;
2667 *new_view = ref_view;
2676 static const struct got_error *
2677 apply_unveil(const char *repo_path, const char *worktree_path)
2679 const struct got_error *error;
2682 if (unveil("gmon.out", "rwc") != 0)
2683 return got_error_from_errno2("unveil", "gmon.out");
2685 if (repo_path && unveil(repo_path, "r") != 0)
2686 return got_error_from_errno2("unveil", repo_path);
2688 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2689 return got_error_from_errno2("unveil", worktree_path);
2691 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2692 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2694 error = got_privsep_unveil_exec_helpers();
2698 if (unveil(NULL, NULL) != 0)
2699 return got_error_from_errno("unveil");
2709 halfdelay(1); /* Do fast refresh while initial view is loading. */
2712 intrflush(stdscr, FALSE);
2713 keypad(stdscr, TRUE);
2715 if (getenv("TOG_COLORS") != NULL) {
2717 use_default_colors();
2719 signal(SIGWINCH, tog_sigwinch);
2720 signal(SIGPIPE, tog_sigpipe);
2721 signal(SIGCONT, tog_sigcont);
2724 static const struct got_error *
2725 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2726 struct got_repository *repo, struct got_worktree *worktree)
2728 const struct got_error *err = NULL;
2731 *in_repo_path = strdup("/");
2732 if (*in_repo_path == NULL)
2733 return got_error_from_errno("strdup");
2738 const char *prefix = got_worktree_get_path_prefix(worktree);
2741 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2744 if (asprintf(in_repo_path, "%s%s%s", prefix,
2745 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2747 err = got_error_from_errno("asprintf");
2748 *in_repo_path = NULL;
2752 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2757 static const struct got_error *
2758 cmd_log(int argc, char *argv[])
2760 const struct got_error *error;
2761 struct got_repository *repo = NULL;
2762 struct got_worktree *worktree = NULL;
2763 struct got_object_id *start_id = NULL;
2764 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2765 char *start_commit = NULL, *label = NULL;
2766 struct got_reference *ref = NULL;
2767 const char *head_ref_name = NULL;
2768 int ch, log_branches = 0;
2769 struct tog_view *view;
2771 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2777 start_commit = optarg;
2780 repo_path = realpath(optarg, NULL);
2781 if (repo_path == NULL)
2782 return got_error_from_errno2("realpath",
2797 if (repo_path == NULL) {
2798 cwd = getcwd(NULL, 0);
2800 return got_error_from_errno("getcwd");
2801 error = got_worktree_open(&worktree, cwd);
2802 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2806 strdup(got_worktree_get_repo_path(worktree));
2808 repo_path = strdup(cwd);
2809 if (repo_path == NULL) {
2810 error = got_error_from_errno("strdup");
2815 error = got_repo_open(&repo, repo_path, NULL);
2819 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2826 error = apply_unveil(got_repo_get_path(repo),
2827 worktree ? got_worktree_get_root_path(worktree) : NULL);
2831 /* already loaded by tog_log_with_path()? */
2832 if (TAILQ_EMPTY(&tog_refs)) {
2833 error = tog_load_refs(repo, 0);
2838 if (start_commit == NULL) {
2839 error = got_repo_match_object_id(&start_id, &label,
2840 worktree ? got_worktree_get_head_ref_name(worktree) :
2841 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2844 head_ref_name = label;
2846 error = got_ref_open(&ref, repo, start_commit, 0);
2848 head_ref_name = got_ref_get_name(ref);
2849 else if (error->code != GOT_ERR_NOT_REF)
2851 error = got_repo_match_object_id(&start_id, NULL,
2852 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2857 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2859 error = got_error_from_errno("view_open");
2862 error = open_log_view(view, start_id, repo, head_ref_name,
2863 in_repo_path, log_branches);
2867 /* Release work tree lock. */
2868 got_worktree_close(worktree);
2871 error = view_loop(view);
2881 const struct got_error *close_err = got_repo_close(repo);
2886 got_worktree_close(worktree);
2895 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2896 "[-w] object1 object2\n", getprogname());
2901 match_line(const char *line, regex_t *regex, size_t nmatch,
2902 regmatch_t *regmatch)
2904 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2908 match_color(struct tog_colors *colors, const char *line)
2910 struct tog_color *tc = NULL;
2912 STAILQ_FOREACH(tc, colors, entry) {
2913 if (match_line(line, &tc->regex, 0, NULL))
2920 static const struct got_error *
2921 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2922 WINDOW *window, regmatch_t *regmatch)
2924 const struct got_error *err = NULL;
2931 s = strndup(line, regmatch->rm_so);
2933 return got_error_from_errno("strndup");
2935 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2940 waddwstr(window, wline);
2947 s = strndup(line + regmatch->rm_so,
2948 regmatch->rm_eo - regmatch->rm_so);
2950 err = got_error_from_errno("strndup");
2954 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2959 wattr_on(window, A_STANDOUT, NULL);
2960 waddwstr(window, wline);
2961 wattr_off(window, A_STANDOUT, NULL);
2968 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2969 err = format_line(&wline, &width,
2970 line + regmatch->rm_eo, wlimit, col_tab_align);
2973 waddwstr(window, wline);
2981 static const struct got_error *
2982 draw_file(struct tog_view *view, const char *header)
2984 struct tog_diff_view_state *s = &view->state.diff;
2985 regmatch_t *regmatch = &view->regmatch;
2986 const struct got_error *err;
2989 size_t linesize = 0;
2991 struct tog_color *tc;
2994 int max_lines = view->nlines;
2995 int nlines = s->nlines;
2998 line_offset = s->line_offsets[s->first_displayed_line - 1];
2999 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3000 return got_error_from_errno("fseek");
3002 werase(view->window);
3005 if (asprintf(&line, "[%d/%d] %s",
3006 s->first_displayed_line - 1 + s->selected_line, nlines,
3008 return got_error_from_errno("asprintf");
3009 err = format_line(&wline, &width, line, view->ncols, 0);
3014 if (view_needs_focus_indication(view))
3015 wstandout(view->window);
3016 waddwstr(view->window, wline);
3019 if (view_needs_focus_indication(view))
3020 wstandend(view->window);
3021 if (width <= view->ncols - 1)
3022 waddch(view->window, '\n');
3031 while (max_lines > 0 && nprinted < max_lines) {
3032 linelen = getline(&line, &linesize, s->f);
3033 if (linelen == -1) {
3039 return got_ferror(s->f, GOT_ERR_IO);
3042 tc = match_color(&s->colors, line);
3044 wattr_on(view->window,
3045 COLOR_PAIR(tc->colorpair), NULL);
3046 if (s->first_displayed_line + nprinted == s->matched_line &&
3047 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3048 err = add_matched_line(&width, line, view->ncols, 0,
3049 view->window, regmatch);
3055 err = format_line(&wline, &width, line, view->ncols, 0);
3060 waddwstr(view->window, wline);
3065 wattr_off(view->window,
3066 COLOR_PAIR(tc->colorpair), NULL);
3067 if (width <= view->ncols - 1)
3068 waddch(view->window, '\n');
3073 s->last_displayed_line = s->first_displayed_line +
3076 s->last_displayed_line = s->first_displayed_line;
3081 while (nprinted < view->nlines) {
3082 waddch(view->window, '\n');
3086 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3091 wstandout(view->window);
3092 waddwstr(view->window, wline);
3095 wstandend(view->window);
3102 get_datestr(time_t *time, char *datebuf)
3104 struct tm mytm, *tm;
3107 tm = gmtime_r(time, &mytm);
3110 s = asctime_r(tm, datebuf);
3113 p = strchr(s, '\n');
3119 static const struct got_error *
3120 get_changed_paths(struct got_pathlist_head *paths,
3121 struct got_commit_object *commit, struct got_repository *repo)
3123 const struct got_error *err = NULL;
3124 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3125 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3126 struct got_object_qid *qid;
3128 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3130 struct got_commit_object *pcommit;
3131 err = got_object_open_as_commit(&pcommit, repo,
3136 tree_id1 = got_object_id_dup(
3137 got_object_commit_get_tree_id(pcommit));
3138 if (tree_id1 == NULL) {
3139 got_object_commit_close(pcommit);
3140 return got_error_from_errno("got_object_id_dup");
3142 got_object_commit_close(pcommit);
3147 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3152 tree_id2 = got_object_commit_get_tree_id(commit);
3153 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3157 err = got_diff_tree(tree1, tree2, "", "", repo,
3158 got_diff_tree_collect_changed_paths, paths, 0);
3161 got_object_tree_close(tree1);
3163 got_object_tree_close(tree2);
3168 static const struct got_error *
3169 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3173 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3175 return got_error_from_errno("reallocarray");
3177 (*line_offsets)[*nlines] = off;
3182 static const struct got_error *
3183 write_commit_info(off_t **line_offsets, size_t *nlines,
3184 struct got_object_id *commit_id, struct got_reflist_head *refs,
3185 struct got_repository *repo, FILE *outfile)
3187 const struct got_error *err = NULL;
3188 char datebuf[26], *datestr;
3189 struct got_commit_object *commit;
3190 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3191 time_t committer_time;
3192 const char *author, *committer;
3193 char *refs_str = NULL;
3194 struct got_pathlist_head changed_paths;
3195 struct got_pathlist_entry *pe;
3199 TAILQ_INIT(&changed_paths);
3202 err = build_refs_str(&refs_str, refs, commit_id, repo);
3207 err = got_object_open_as_commit(&commit, repo, commit_id);
3211 err = got_object_id_str(&id_str, commit_id);
3213 err = got_error_from_errno("got_object_id_str");
3217 err = add_line_offset(line_offsets, nlines, 0);
3221 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3222 refs_str ? refs_str : "", refs_str ? ")" : "");
3224 err = got_error_from_errno("fprintf");
3228 err = add_line_offset(line_offsets, nlines, outoff);
3232 n = fprintf(outfile, "from: %s\n",
3233 got_object_commit_get_author(commit));
3235 err = got_error_from_errno("fprintf");
3239 err = add_line_offset(line_offsets, nlines, outoff);
3243 committer_time = got_object_commit_get_committer_time(commit);
3244 datestr = get_datestr(&committer_time, datebuf);
3246 n = fprintf(outfile, "date: %s UTC\n", datestr);
3248 err = got_error_from_errno("fprintf");
3252 err = add_line_offset(line_offsets, nlines, outoff);
3256 author = got_object_commit_get_author(commit);
3257 committer = got_object_commit_get_committer(commit);
3258 if (strcmp(author, committer) != 0) {
3259 n = fprintf(outfile, "via: %s\n", committer);
3261 err = got_error_from_errno("fprintf");
3265 err = add_line_offset(line_offsets, nlines, outoff);
3269 if (got_object_commit_get_nparents(commit) > 1) {
3270 const struct got_object_id_queue *parent_ids;
3271 struct got_object_qid *qid;
3273 parent_ids = got_object_commit_get_parent_ids(commit);
3274 STAILQ_FOREACH(qid, parent_ids, entry) {
3275 err = got_object_id_str(&id_str, &qid->id);
3278 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3280 err = got_error_from_errno("fprintf");
3284 err = add_line_offset(line_offsets, nlines, outoff);
3292 err = got_object_commit_get_logmsg(&logmsg, commit);
3296 while ((line = strsep(&s, "\n")) != NULL) {
3297 n = fprintf(outfile, "%s\n", line);
3299 err = got_error_from_errno("fprintf");
3303 err = add_line_offset(line_offsets, nlines, outoff);
3308 err = get_changed_paths(&changed_paths, commit, repo);
3311 TAILQ_FOREACH(pe, &changed_paths, entry) {
3312 struct got_diff_changed_path *cp = pe->data;
3313 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3315 err = got_error_from_errno("fprintf");
3319 err = add_line_offset(line_offsets, nlines, outoff);
3322 free((char *)pe->path);
3326 fputc('\n', outfile);
3328 err = add_line_offset(line_offsets, nlines, outoff);
3330 got_pathlist_free(&changed_paths);
3334 got_object_commit_close(commit);
3336 free(*line_offsets);
3337 *line_offsets = NULL;
3343 static const struct got_error *
3344 create_diff(struct tog_diff_view_state *s)
3346 const struct got_error *err = NULL;
3350 free(s->line_offsets);
3351 s->line_offsets = malloc(sizeof(off_t));
3352 if (s->line_offsets == NULL)
3353 return got_error_from_errno("malloc");
3358 err = got_error_from_errno("got_opentemp");
3361 if (s->f && fclose(s->f) == EOF) {
3362 err = got_error_from_errno("fclose");
3368 err = got_object_get_type(&obj_type, s->repo, s->id1);
3370 err = got_object_get_type(&obj_type, s->repo, s->id2);
3375 case GOT_OBJ_TYPE_BLOB:
3376 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3377 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3378 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3380 case GOT_OBJ_TYPE_TREE:
3381 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3382 s->id1, s->id2, NULL, "", "", s->diff_context,
3383 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3385 case GOT_OBJ_TYPE_COMMIT: {
3386 const struct got_object_id_queue *parent_ids;
3387 struct got_object_qid *pid;
3388 struct got_commit_object *commit2;
3389 struct got_reflist_head *refs;
3391 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3394 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3395 /* Show commit info if we're diffing to a parent/root commit. */
3396 if (s->id1 == NULL) {
3397 err = write_commit_info(&s->line_offsets, &s->nlines,
3398 s->id2, refs, s->repo, s->f);
3402 parent_ids = got_object_commit_get_parent_ids(commit2);
3403 STAILQ_FOREACH(pid, parent_ids, entry) {
3404 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3405 err = write_commit_info(
3406 &s->line_offsets, &s->nlines,
3407 s->id2, refs, s->repo, s->f);
3414 got_object_commit_close(commit2);
3416 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3417 s->id1, s->id2, NULL, s->diff_context, s->ignore_whitespace,
3418 s->force_text_diff, s->repo, s->f);
3422 err = got_error(GOT_ERR_OBJ_TYPE);
3428 if (s->f && fflush(s->f) != 0 && err == NULL)
3429 err = got_error_from_errno("fflush");
3434 diff_view_indicate_progress(struct tog_view *view)
3436 mvwaddstr(view->window, 0, 0, "diffing...");
3441 static const struct got_error *
3442 search_start_diff_view(struct tog_view *view)
3444 struct tog_diff_view_state *s = &view->state.diff;
3446 s->matched_line = 0;
3450 static const struct got_error *
3451 search_next_diff_view(struct tog_view *view)
3453 struct tog_diff_view_state *s = &view->state.diff;
3456 size_t linesize = 0;
3459 if (!view->searching) {
3460 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3464 if (s->matched_line) {
3465 if (view->searching == TOG_SEARCH_FORWARD)
3466 lineno = s->matched_line + 1;
3468 lineno = s->matched_line - 1;
3470 lineno = s->first_displayed_line;
3475 if (lineno <= 0 || lineno > s->nlines) {
3476 if (s->matched_line == 0) {
3477 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3481 if (view->searching == TOG_SEARCH_FORWARD)
3487 offset = s->line_offsets[lineno - 1];
3488 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3490 return got_error_from_errno("fseeko");
3492 linelen = getline(&line, &linesize, s->f);
3493 if (linelen != -1 &&
3494 match_line(line, &view->regex, 1, &view->regmatch)) {
3495 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3496 s->matched_line = lineno;
3499 if (view->searching == TOG_SEARCH_FORWARD)
3506 if (s->matched_line) {
3507 s->first_displayed_line = s->matched_line;
3508 s->selected_line = 1;
3514 static const struct got_error *
3515 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3516 struct got_object_id *id2, const char *label1, const char *label2,
3517 int diff_context, int ignore_whitespace, int force_text_diff,
3518 struct tog_view *log_view, struct got_repository *repo)
3520 const struct got_error *err;
3521 struct tog_diff_view_state *s = &view->state.diff;
3523 if (id1 != NULL && id2 != NULL) {
3525 err = got_object_get_type(&type1, repo, id1);
3528 err = got_object_get_type(&type2, repo, id2);
3533 return got_error(GOT_ERR_OBJ_TYPE);
3535 s->first_displayed_line = 1;
3536 s->last_displayed_line = view->nlines;
3537 s->selected_line = 1;
3545 s->id1 = got_object_id_dup(id1);
3547 return got_error_from_errno("got_object_id_dup");
3551 s->id2 = got_object_id_dup(id2);
3552 if (s->id2 == NULL) {
3555 return got_error_from_errno("got_object_id_dup");
3558 s->first_displayed_line = 1;
3559 s->last_displayed_line = view->nlines;
3560 s->diff_context = diff_context;
3561 s->ignore_whitespace = ignore_whitespace;
3562 s->force_text_diff = force_text_diff;
3563 s->log_view = log_view;
3566 STAILQ_INIT(&s->colors);
3567 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3568 err = add_color(&s->colors,
3569 "^-", TOG_COLOR_DIFF_MINUS,
3570 get_color_value("TOG_COLOR_DIFF_MINUS"));
3573 err = add_color(&s->colors, "^\\+",
3574 TOG_COLOR_DIFF_PLUS,
3575 get_color_value("TOG_COLOR_DIFF_PLUS"));
3577 free_colors(&s->colors);
3580 err = add_color(&s->colors,
3581 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3582 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3584 free_colors(&s->colors);
3588 err = add_color(&s->colors,
3589 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3590 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3591 get_color_value("TOG_COLOR_DIFF_META"));
3593 free_colors(&s->colors);
3597 err = add_color(&s->colors,
3598 "^(from|via): ", TOG_COLOR_AUTHOR,
3599 get_color_value("TOG_COLOR_AUTHOR"));
3601 free_colors(&s->colors);
3605 err = add_color(&s->colors,
3606 "^date: ", TOG_COLOR_DATE,
3607 get_color_value("TOG_COLOR_DATE"));
3609 free_colors(&s->colors);
3614 if (log_view && view_is_splitscreen(view))
3615 show_log_view(log_view); /* draw vborder */
3616 diff_view_indicate_progress(view);
3618 s->line_offsets = NULL;
3620 err = create_diff(s);
3626 free_colors(&s->colors);
3630 view->show = show_diff_view;
3631 view->input = input_diff_view;
3632 view->close = close_diff_view;
3633 view->search_start = search_start_diff_view;
3634 view->search_next = search_next_diff_view;
3639 static const struct got_error *
3640 close_diff_view(struct tog_view *view)
3642 const struct got_error *err = NULL;
3643 struct tog_diff_view_state *s = &view->state.diff;
3649 if (s->f && fclose(s->f) == EOF)
3650 err = got_error_from_errno("fclose");
3651 free_colors(&s->colors);
3652 free(s->line_offsets);
3653 s->line_offsets = NULL;
3658 static const struct got_error *
3659 show_diff_view(struct tog_view *view)
3661 const struct got_error *err;
3662 struct tog_diff_view_state *s = &view->state.diff;
3663 char *id_str1 = NULL, *id_str2, *header;
3664 const char *label1, *label2;
3667 err = got_object_id_str(&id_str1, s->id1);
3670 label1 = s->label1 ? : id_str1;
3672 label1 = "/dev/null";
3674 err = got_object_id_str(&id_str2, s->id2);
3677 label2 = s->label2 ? : id_str2;
3679 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3680 err = got_error_from_errno("asprintf");
3688 err = draw_file(view, header);
3693 static const struct got_error *
3694 set_selected_commit(struct tog_diff_view_state *s,
3695 struct commit_queue_entry *entry)
3697 const struct got_error *err;
3698 const struct got_object_id_queue *parent_ids;
3699 struct got_commit_object *selected_commit;
3700 struct got_object_qid *pid;
3703 s->id2 = got_object_id_dup(entry->id);
3705 return got_error_from_errno("got_object_id_dup");
3707 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3710 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3712 pid = STAILQ_FIRST(parent_ids);
3713 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3714 got_object_commit_close(selected_commit);
3718 static const struct got_error *
3719 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3721 const struct got_error *err = NULL;
3722 struct tog_diff_view_state *s = &view->state.diff;
3723 struct tog_log_view_state *ls;
3724 struct commit_queue_entry *old_selected_entry;
3726 size_t linesize = 0;
3734 s->force_text_diff = !s->force_text_diff;
3736 s->ignore_whitespace = !s->ignore_whitespace;
3737 wclear(view->window);
3738 s->first_displayed_line = 1;
3739 s->last_displayed_line = view->nlines;
3740 s->matched_line = 0;
3741 diff_view_indicate_progress(view);
3742 err = create_diff(s);
3746 s->first_displayed_line = 1;
3753 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3759 if (s->first_displayed_line > 1)
3760 s->first_displayed_line--;
3764 if (s->first_displayed_line == 1)
3767 while (i++ < view->nlines - 1 &&
3768 s->first_displayed_line > 1)
3769 s->first_displayed_line--;
3775 s->first_displayed_line++;
3783 while (!s->eof && i++ < view->nlines - 1) {
3784 linelen = getline(&line, &linesize, s->f);
3785 s->first_displayed_line++;
3786 if (linelen == -1) {
3790 err = got_ferror(s->f, GOT_ERR_IO);
3797 if (s->diff_context > 0) {
3799 s->matched_line = 0;
3800 diff_view_indicate_progress(view);
3801 err = create_diff(s);
3802 if (s->first_displayed_line + view->nlines - 1 >
3804 s->first_displayed_line = 1;
3805 s->last_displayed_line = view->nlines;
3810 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3812 s->matched_line = 0;
3813 diff_view_indicate_progress(view);
3814 err = create_diff(s);
3819 if (s->log_view == NULL)
3821 ls = &s->log_view->state.log;
3822 old_selected_entry = ls->selected_entry;
3824 err = input_log_view(NULL, s->log_view, KEY_UP);
3828 if (old_selected_entry == ls->selected_entry)
3831 err = set_selected_commit(s, ls->selected_entry);
3835 s->first_displayed_line = 1;
3836 s->last_displayed_line = view->nlines;
3837 s->matched_line = 0;
3839 diff_view_indicate_progress(view);
3840 err = create_diff(s);
3844 if (s->log_view == NULL)
3846 ls = &s->log_view->state.log;
3847 old_selected_entry = ls->selected_entry;
3849 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3853 if (old_selected_entry == ls->selected_entry)
3856 err = set_selected_commit(s, ls->selected_entry);
3860 s->first_displayed_line = 1;
3861 s->last_displayed_line = view->nlines;
3862 s->matched_line = 0;
3864 diff_view_indicate_progress(view);
3865 err = create_diff(s);
3874 static const struct got_error *
3875 cmd_diff(int argc, char *argv[])
3877 const struct got_error *error = NULL;
3878 struct got_repository *repo = NULL;
3879 struct got_worktree *worktree = NULL;
3880 struct got_object_id *id1 = NULL, *id2 = NULL;
3881 char *repo_path = NULL, *cwd = NULL;
3882 char *id_str1 = NULL, *id_str2 = NULL;
3883 char *label1 = NULL, *label2 = NULL;
3884 int diff_context = 3, ignore_whitespace = 0;
3885 int ch, force_text_diff = 0;
3887 struct tog_view *view;
3889 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3892 force_text_diff = 1;
3895 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3898 errx(1, "number of context lines is %s: %s",
3902 repo_path = realpath(optarg, NULL);
3903 if (repo_path == NULL)
3904 return got_error_from_errno2("realpath",
3906 got_path_strip_trailing_slashes(repo_path);
3909 ignore_whitespace = 1;
3921 usage_diff(); /* TODO show local worktree changes */
3922 } else if (argc == 2) {
3928 if (repo_path == NULL) {
3929 cwd = getcwd(NULL, 0);
3931 return got_error_from_errno("getcwd");
3932 error = got_worktree_open(&worktree, cwd);
3933 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3937 strdup(got_worktree_get_repo_path(worktree));
3939 repo_path = strdup(cwd);
3940 if (repo_path == NULL) {
3941 error = got_error_from_errno("strdup");
3946 error = got_repo_open(&repo, repo_path, NULL);
3952 error = apply_unveil(got_repo_get_path(repo), NULL);
3956 error = tog_load_refs(repo, 0);
3960 error = got_repo_match_object_id(&id1, &label1, id_str1,
3961 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3965 error = got_repo_match_object_id(&id2, &label2, id_str2,
3966 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3970 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3972 error = got_error_from_errno("view_open");
3975 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3976 ignore_whitespace, force_text_diff, NULL, repo);
3979 error = view_loop(view);
3986 const struct got_error *close_err = got_repo_close(repo);
3991 got_worktree_close(worktree);
4000 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4005 struct tog_blame_line {
4007 struct got_object_id *id;
4010 static const struct got_error *
4011 draw_blame(struct tog_view *view)
4013 struct tog_blame_view_state *s = &view->state.blame;
4014 struct tog_blame *blame = &s->blame;
4015 regmatch_t *regmatch = &view->regmatch;
4016 const struct got_error *err;
4017 int lineno = 0, nprinted = 0;
4019 size_t linesize = 0;
4023 struct tog_blame_line *blame_line;
4024 struct got_object_id *prev_id = NULL;
4026 struct tog_color *tc;
4028 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4033 werase(view->window);
4035 if (asprintf(&line, "commit %s", id_str) == -1) {
4036 err = got_error_from_errno("asprintf");
4041 err = format_line(&wline, &width, line, view->ncols, 0);
4046 if (view_needs_focus_indication(view))
4047 wstandout(view->window);
4048 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4050 wattr_on(view->window,
4051 COLOR_PAIR(tc->colorpair), NULL);
4052 waddwstr(view->window, wline);
4054 wattr_off(view->window,
4055 COLOR_PAIR(tc->colorpair), NULL);
4056 if (view_needs_focus_indication(view))
4057 wstandend(view->window);
4060 if (width < view->ncols - 1)
4061 waddch(view->window, '\n');
4063 if (asprintf(&line, "[%d/%d] %s%s",
4064 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4065 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4067 return got_error_from_errno("asprintf");
4070 err = format_line(&wline, &width, line, view->ncols, 0);
4075 waddwstr(view->window, wline);
4078 if (width < view->ncols - 1)
4079 waddch(view->window, '\n');
4082 while (nprinted < view->nlines - 2) {
4083 linelen = getline(&line, &linesize, blame->f);
4084 if (linelen == -1) {
4085 if (feof(blame->f)) {
4090 return got_ferror(blame->f, GOT_ERR_IO);
4092 if (++lineno < s->first_displayed_line)
4095 if (view->focussed && nprinted == s->selected_line - 1)
4096 wstandout(view->window);
4098 if (blame->nlines > 0) {
4099 blame_line = &blame->lines[lineno - 1];
4100 if (blame_line->annotated && prev_id &&
4101 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4103 nprinted == s->selected_line - 1)) {
4104 waddstr(view->window, " ");
4105 } else if (blame_line->annotated) {
4107 err = got_object_id_str(&id_str, blame_line->id);
4112 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4114 wattr_on(view->window,
4115 COLOR_PAIR(tc->colorpair), NULL);
4116 wprintw(view->window, "%.8s", id_str);
4118 wattr_off(view->window,
4119 COLOR_PAIR(tc->colorpair), NULL);
4121 prev_id = blame_line->id;
4123 waddstr(view->window, "........");
4127 waddstr(view->window, "........");
4131 if (view->focussed && nprinted == s->selected_line - 1)
4132 wstandend(view->window);
4133 waddstr(view->window, " ");
4135 if (view->ncols <= 9) {
4137 wline = wcsdup(L"");
4138 if (wline == NULL) {
4139 err = got_error_from_errno("wcsdup");
4143 } else if (s->first_displayed_line + nprinted ==
4145 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4146 err = add_matched_line(&width, line, view->ncols - 9, 9,
4147 view->window, regmatch);
4154 err = format_line(&wline, &width, line,
4155 view->ncols - 9, 9);
4156 waddwstr(view->window, wline);
4162 if (width <= view->ncols - 1)
4163 waddch(view->window, '\n');
4164 if (++nprinted == 1)
4165 s->first_displayed_line = lineno;
4168 s->last_displayed_line = lineno;
4175 static const struct got_error *
4176 blame_cb(void *arg, int nlines, int lineno,
4177 struct got_commit_object *commit, struct got_object_id *id)
4179 const struct got_error *err = NULL;
4180 struct tog_blame_cb_args *a = arg;
4181 struct tog_blame_line *line;
4184 if (nlines != a->nlines ||
4185 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4186 return got_error(GOT_ERR_RANGE);
4188 errcode = pthread_mutex_lock(&tog_mutex);
4190 return got_error_set_errno(errcode, "pthread_mutex_lock");
4192 if (*a->quit) { /* user has quit the blame view */
4193 err = got_error(GOT_ERR_ITER_COMPLETED);
4198 goto done; /* no change in this commit */
4200 line = &a->lines[lineno - 1];
4201 if (line->annotated)
4204 line->id = got_object_id_dup(id);
4205 if (line->id == NULL) {
4206 err = got_error_from_errno("got_object_id_dup");
4209 line->annotated = 1;
4211 errcode = pthread_mutex_unlock(&tog_mutex);
4213 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4218 blame_thread(void *arg)
4220 const struct got_error *err, *close_err;
4221 struct tog_blame_thread_args *ta = arg;
4222 struct tog_blame_cb_args *a = ta->cb_args;
4225 err = block_signals_used_by_main_thread();
4229 err = got_blame(ta->path, a->commit_id, ta->repo,
4230 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4231 if (err && err->code == GOT_ERR_CANCELLED)
4234 errcode = pthread_mutex_lock(&tog_mutex);
4236 return (void *)got_error_set_errno(errcode,
4237 "pthread_mutex_lock");
4239 close_err = got_repo_close(ta->repo);
4245 errcode = pthread_mutex_unlock(&tog_mutex);
4246 if (errcode && err == NULL)
4247 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4252 static struct got_object_id *
4253 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4254 int first_displayed_line, int selected_line)
4256 struct tog_blame_line *line;
4261 line = &lines[first_displayed_line - 1 + selected_line - 1];
4262 if (!line->annotated)
4268 static const struct got_error *
4269 stop_blame(struct tog_blame *blame)
4271 const struct got_error *err = NULL;
4274 if (blame->thread) {
4276 errcode = pthread_mutex_unlock(&tog_mutex);
4278 return got_error_set_errno(errcode,
4279 "pthread_mutex_unlock");
4280 errcode = pthread_join(blame->thread, (void **)&err);
4282 return got_error_set_errno(errcode, "pthread_join");
4283 errcode = pthread_mutex_lock(&tog_mutex);
4285 return got_error_set_errno(errcode,
4286 "pthread_mutex_lock");
4287 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4289 blame->thread = 0; //NULL;
4291 if (blame->thread_args.repo) {
4292 const struct got_error *close_err;
4293 close_err = got_repo_close(blame->thread_args.repo);
4296 blame->thread_args.repo = NULL;
4299 if (fclose(blame->f) == EOF && err == NULL)
4300 err = got_error_from_errno("fclose");
4304 for (i = 0; i < blame->nlines; i++)
4305 free(blame->lines[i].id);
4307 blame->lines = NULL;
4309 free(blame->cb_args.commit_id);
4310 blame->cb_args.commit_id = NULL;
4315 static const struct got_error *
4316 cancel_blame_view(void *arg)
4318 const struct got_error *err = NULL;
4322 errcode = pthread_mutex_lock(&tog_mutex);
4324 return got_error_set_errno(errcode,
4325 "pthread_mutex_unlock");
4328 err = got_error(GOT_ERR_CANCELLED);
4330 errcode = pthread_mutex_unlock(&tog_mutex);
4332 return got_error_set_errno(errcode,
4333 "pthread_mutex_lock");
4338 static const struct got_error *
4339 run_blame(struct tog_view *view)
4341 struct tog_blame_view_state *s = &view->state.blame;
4342 struct tog_blame *blame = &s->blame;
4343 const struct got_error *err = NULL;
4344 struct got_commit_object *commit = NULL;
4345 struct got_blob_object *blob = NULL;
4346 struct got_repository *thread_repo = NULL;
4347 struct got_object_id *obj_id = NULL;
4350 err = got_object_open_as_commit(&commit, s->repo,
4351 &s->blamed_commit->id);
4355 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4359 err = got_object_get_type(&obj_type, s->repo, obj_id);
4363 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4364 err = got_error(GOT_ERR_OBJ_TYPE);
4368 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4371 blame->f = got_opentemp();
4372 if (blame->f == NULL) {
4373 err = got_error_from_errno("got_opentemp");
4376 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4377 &blame->line_offsets, blame->f, blob);
4380 if (blame->nlines == 0) {
4381 s->blame_complete = 1;
4385 /* Don't include \n at EOF in the blame line count. */
4386 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4389 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4390 if (blame->lines == NULL) {
4391 err = got_error_from_errno("calloc");
4395 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4399 blame->cb_args.view = view;
4400 blame->cb_args.lines = blame->lines;
4401 blame->cb_args.nlines = blame->nlines;
4402 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4403 if (blame->cb_args.commit_id == NULL) {
4404 err = got_error_from_errno("got_object_id_dup");
4407 blame->cb_args.quit = &s->done;
4409 blame->thread_args.path = s->path;
4410 blame->thread_args.repo = thread_repo;
4411 blame->thread_args.cb_args = &blame->cb_args;
4412 blame->thread_args.complete = &s->blame_complete;
4413 blame->thread_args.cancel_cb = cancel_blame_view;
4414 blame->thread_args.cancel_arg = &s->done;
4415 s->blame_complete = 0;
4417 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4418 s->first_displayed_line = 1;
4419 s->last_displayed_line = view->nlines;
4420 s->selected_line = 1;
4422 s->matched_line = 0;
4426 got_object_commit_close(commit);
4428 got_object_blob_close(blob);
4435 static const struct got_error *
4436 open_blame_view(struct tog_view *view, char *path,
4437 struct got_object_id *commit_id, struct got_repository *repo)
4439 const struct got_error *err = NULL;
4440 struct tog_blame_view_state *s = &view->state.blame;
4442 STAILQ_INIT(&s->blamed_commits);
4444 s->path = strdup(path);
4445 if (s->path == NULL)
4446 return got_error_from_errno("strdup");
4448 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4454 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4455 s->first_displayed_line = 1;
4456 s->last_displayed_line = view->nlines;
4457 s->selected_line = 1;
4458 s->blame_complete = 0;
4460 s->commit_id = commit_id;
4461 memset(&s->blame, 0, sizeof(s->blame));
4463 STAILQ_INIT(&s->colors);
4464 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4465 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4466 get_color_value("TOG_COLOR_COMMIT"));
4471 view->show = show_blame_view;
4472 view->input = input_blame_view;
4473 view->close = close_blame_view;
4474 view->search_start = search_start_blame_view;
4475 view->search_next = search_next_blame_view;
4477 return run_blame(view);
4480 static const struct got_error *
4481 close_blame_view(struct tog_view *view)
4483 const struct got_error *err = NULL;
4484 struct tog_blame_view_state *s = &view->state.blame;
4486 if (s->blame.thread)
4487 err = stop_blame(&s->blame);
4489 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4490 struct got_object_qid *blamed_commit;
4491 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4492 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4493 got_object_qid_free(blamed_commit);
4497 free_colors(&s->colors);
4502 static const struct got_error *
4503 search_start_blame_view(struct tog_view *view)
4505 struct tog_blame_view_state *s = &view->state.blame;
4507 s->matched_line = 0;
4511 static const struct got_error *
4512 search_next_blame_view(struct tog_view *view)
4514 struct tog_blame_view_state *s = &view->state.blame;
4517 size_t linesize = 0;
4520 if (!view->searching) {
4521 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4525 if (s->matched_line) {
4526 if (view->searching == TOG_SEARCH_FORWARD)
4527 lineno = s->matched_line + 1;
4529 lineno = s->matched_line - 1;
4531 lineno = s->first_displayed_line - 1 + s->selected_line;
4536 if (lineno <= 0 || lineno > s->blame.nlines) {
4537 if (s->matched_line == 0) {
4538 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4542 if (view->searching == TOG_SEARCH_FORWARD)
4545 lineno = s->blame.nlines;
4548 offset = s->blame.line_offsets[lineno - 1];
4549 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4551 return got_error_from_errno("fseeko");
4553 linelen = getline(&line, &linesize, s->blame.f);
4554 if (linelen != -1 &&
4555 match_line(line, &view->regex, 1, &view->regmatch)) {
4556 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4557 s->matched_line = lineno;
4560 if (view->searching == TOG_SEARCH_FORWARD)
4567 if (s->matched_line) {
4568 s->first_displayed_line = s->matched_line;
4569 s->selected_line = 1;
4575 static const struct got_error *
4576 show_blame_view(struct tog_view *view)
4578 const struct got_error *err = NULL;
4579 struct tog_blame_view_state *s = &view->state.blame;
4582 if (s->blame.thread == 0 && !s->blame_complete) {
4583 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4584 &s->blame.thread_args);
4586 return got_error_set_errno(errcode, "pthread_create");
4588 halfdelay(1); /* fast refresh while annotating */
4591 if (s->blame_complete)
4592 halfdelay(10); /* disable fast refresh */
4594 err = draw_blame(view);
4600 static const struct got_error *
4601 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4603 const struct got_error *err = NULL, *thread_err = NULL;
4604 struct tog_view *diff_view;
4605 struct tog_blame_view_state *s = &view->state.blame;
4614 s->selected_line = 1;
4615 s->first_displayed_line = 1;
4619 if (s->blame.nlines < view->nlines - 2) {
4620 s->selected_line = s->blame.nlines;
4621 s->first_displayed_line = 1;
4623 s->selected_line = view->nlines - 2;
4624 s->first_displayed_line = s->blame.nlines -
4631 if (s->selected_line > 1)
4633 else if (s->selected_line == 1 &&
4634 s->first_displayed_line > 1)
4635 s->first_displayed_line--;
4639 if (s->first_displayed_line == 1) {
4640 s->selected_line = 1;
4643 if (s->first_displayed_line > view->nlines - 2)
4644 s->first_displayed_line -=
4647 s->first_displayed_line = 1;
4652 if (s->selected_line < view->nlines - 2 &&
4653 s->first_displayed_line +
4654 s->selected_line <= s->blame.nlines)
4656 else if (s->last_displayed_line <
4658 s->first_displayed_line++;
4662 struct got_object_id *id = NULL;
4663 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4664 s->first_displayed_line, s->selected_line);
4668 struct got_commit_object *commit, *pcommit;
4669 struct got_object_qid *pid;
4670 struct got_object_id *blob_id = NULL;
4672 err = got_object_open_as_commit(&commit,
4677 got_object_commit_get_parent_ids(commit));
4679 got_object_commit_close(commit);
4682 /* Check if path history ends here. */
4683 err = got_object_open_as_commit(&pcommit,
4687 err = got_object_id_by_path(&blob_id, s->repo,
4689 got_object_commit_close(pcommit);
4691 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4693 got_object_commit_close(commit);
4696 err = got_object_get_type(&obj_type, s->repo,
4699 /* Can't blame non-blob type objects. */
4700 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4701 got_object_commit_close(commit);
4704 err = got_object_qid_alloc(&s->blamed_commit,
4706 got_object_commit_close(commit);
4708 if (got_object_id_cmp(id,
4709 &s->blamed_commit->id) == 0)
4711 err = got_object_qid_alloc(&s->blamed_commit,
4717 thread_err = stop_blame(&s->blame);
4721 STAILQ_INSERT_HEAD(&s->blamed_commits,
4722 s->blamed_commit, entry);
4723 err = run_blame(view);
4729 struct got_object_qid *first;
4730 first = STAILQ_FIRST(&s->blamed_commits);
4731 if (!got_object_id_cmp(&first->id, s->commit_id))
4734 thread_err = stop_blame(&s->blame);
4738 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4739 got_object_qid_free(s->blamed_commit);
4741 STAILQ_FIRST(&s->blamed_commits);
4742 err = run_blame(view);
4749 struct got_object_id *id = NULL;
4750 struct got_object_qid *pid;
4751 struct got_commit_object *commit = NULL;
4752 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4753 s->first_displayed_line, s->selected_line);
4756 err = got_object_open_as_commit(&commit, s->repo, id);
4760 got_object_commit_get_parent_ids(commit));
4761 if (view_is_parent_view(view))
4762 begin_x = view_split_begin_x(view->begin_x);
4763 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4764 if (diff_view == NULL) {
4765 got_object_commit_close(commit);
4766 err = got_error_from_errno("view_open");
4769 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4770 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4771 got_object_commit_close(commit);
4773 view_close(diff_view);
4777 diff_view->focussed = 1;
4778 if (view_is_parent_view(view)) {
4779 err = view_close_child(view);
4782 view_set_child(view, diff_view);
4783 view->focus_child = 1;
4785 *new_view = diff_view;
4793 if (s->last_displayed_line >= s->blame.nlines &&
4794 s->selected_line >= MIN(s->blame.nlines,
4795 view->nlines - 2)) {
4798 if (s->last_displayed_line >= s->blame.nlines &&
4799 s->selected_line < view->nlines - 2) {
4800 s->selected_line = MIN(s->blame.nlines,
4804 if (s->last_displayed_line + view->nlines - 2
4806 s->first_displayed_line +=
4809 s->first_displayed_line =
4814 if (s->selected_line > view->nlines - 2) {
4815 s->selected_line = MIN(s->blame.nlines,
4822 return thread_err ? thread_err : err;
4825 static const struct got_error *
4826 cmd_blame(int argc, char *argv[])
4828 const struct got_error *error;
4829 struct got_repository *repo = NULL;
4830 struct got_worktree *worktree = NULL;
4831 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4832 char *link_target = NULL;
4833 struct got_object_id *commit_id = NULL;
4834 struct got_commit_object *commit = NULL;
4835 char *commit_id_str = NULL;
4837 struct tog_view *view;
4839 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4842 commit_id_str = optarg;
4845 repo_path = realpath(optarg, NULL);
4846 if (repo_path == NULL)
4847 return got_error_from_errno2("realpath",
4862 if (repo_path == NULL) {
4863 cwd = getcwd(NULL, 0);
4865 return got_error_from_errno("getcwd");
4866 error = got_worktree_open(&worktree, cwd);
4867 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4871 strdup(got_worktree_get_repo_path(worktree));
4873 repo_path = strdup(cwd);
4874 if (repo_path == NULL) {
4875 error = got_error_from_errno("strdup");
4880 error = got_repo_open(&repo, repo_path, NULL);
4884 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4891 error = apply_unveil(got_repo_get_path(repo), NULL);
4895 error = tog_load_refs(repo, 0);
4899 if (commit_id_str == NULL) {
4900 struct got_reference *head_ref;
4901 error = got_ref_open(&head_ref, repo, worktree ?
4902 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4905 error = got_ref_resolve(&commit_id, repo, head_ref);
4906 got_ref_close(head_ref);
4908 error = got_repo_match_object_id(&commit_id, NULL,
4909 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4914 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4916 error = got_error_from_errno("view_open");
4920 error = got_object_open_as_commit(&commit, repo, commit_id);
4924 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4929 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4934 /* Release work tree lock. */
4935 got_worktree_close(worktree);
4938 error = view_loop(view);
4946 got_object_commit_close(commit);
4948 got_worktree_close(worktree);
4950 const struct got_error *close_err = got_repo_close(repo);
4958 static const struct got_error *
4959 draw_tree_entries(struct tog_view *view, const char *parent_path)
4961 struct tog_tree_view_state *s = &view->state.tree;
4962 const struct got_error *err = NULL;
4963 struct got_tree_entry *te;
4965 struct tog_color *tc;
4966 int width, n, i, nentries;
4967 int limit = view->nlines;
4971 werase(view->window);
4976 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4979 if (view_needs_focus_indication(view))
4980 wstandout(view->window);
4981 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4983 wattr_on(view->window,
4984 COLOR_PAIR(tc->colorpair), NULL);
4985 waddwstr(view->window, wline);
4987 wattr_off(view->window,
4988 COLOR_PAIR(tc->colorpair), NULL);
4989 if (view_needs_focus_indication(view))
4990 wstandend(view->window);
4993 if (width < view->ncols - 1)
4994 waddch(view->window, '\n');
4997 err = format_line(&wline, &width, parent_path, view->ncols, 0);
5000 waddwstr(view->window, wline);
5003 if (width < view->ncols - 1)
5004 waddch(view->window, '\n');
5007 waddch(view->window, '\n');
5011 if (s->first_displayed_entry == NULL) {
5012 te = got_object_tree_get_first_entry(s->tree);
5013 if (s->selected == 0) {
5015 wstandout(view->window);
5016 s->selected_entry = NULL;
5018 waddstr(view->window, " ..\n"); /* parent directory */
5019 if (s->selected == 0 && view->focussed)
5020 wstandend(view->window);
5027 te = s->first_displayed_entry;
5030 nentries = got_object_tree_get_nentries(s->tree);
5031 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5032 char *line = NULL, *id_str = NULL, *link_target = NULL;
5033 const char *modestr = "";
5036 te = got_object_tree_get_entry(s->tree, i);
5037 mode = got_tree_entry_get_mode(te);
5040 err = got_object_id_str(&id_str,
5041 got_tree_entry_get_id(te));
5043 return got_error_from_errno(
5044 "got_object_id_str");
5046 if (got_object_tree_entry_is_submodule(te))
5048 else if (S_ISLNK(mode)) {
5051 err = got_tree_entry_get_symlink_target(&link_target,
5057 for (i = 0; i < strlen(link_target); i++) {
5058 if (!isprint((unsigned char)link_target[i]))
5059 link_target[i] = '?';
5063 else if (S_ISDIR(mode))
5065 else if (mode & S_IXUSR)
5067 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5068 got_tree_entry_get_name(te), modestr,
5069 link_target ? " -> ": "",
5070 link_target ? link_target : "") == -1) {
5073 return got_error_from_errno("asprintf");
5077 err = format_line(&wline, &width, line, view->ncols, 0);
5082 if (n == s->selected) {
5084 wstandout(view->window);
5085 s->selected_entry = te;
5087 tc = match_color(&s->colors, line);
5089 wattr_on(view->window,
5090 COLOR_PAIR(tc->colorpair), NULL);
5091 waddwstr(view->window, wline);
5093 wattr_off(view->window,
5094 COLOR_PAIR(tc->colorpair), NULL);
5095 if (width < view->ncols - 1)
5096 waddch(view->window, '\n');
5097 if (n == s->selected && view->focussed)
5098 wstandend(view->window);
5104 s->last_displayed_entry = te;
5113 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5115 struct got_tree_entry *te;
5116 int isroot = s->tree == s->root;
5119 if (s->first_displayed_entry == NULL)
5122 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5123 while (i++ < maxscroll) {
5126 s->first_displayed_entry = NULL;
5129 s->first_displayed_entry = te;
5130 te = got_tree_entry_get_prev(s->tree, te);
5135 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5137 struct got_tree_entry *next, *last;
5140 if (s->first_displayed_entry)
5141 next = got_tree_entry_get_next(s->tree,
5142 s->first_displayed_entry);
5144 next = got_object_tree_get_first_entry(s->tree);
5146 last = s->last_displayed_entry;
5147 while (next && last && n++ < maxscroll) {
5148 last = got_tree_entry_get_next(s->tree, last);
5150 s->first_displayed_entry = next;
5151 next = got_tree_entry_get_next(s->tree, next);
5156 static const struct got_error *
5157 tree_entry_path(char **path, struct tog_parent_trees *parents,
5158 struct got_tree_entry *te)
5160 const struct got_error *err = NULL;
5161 struct tog_parent_tree *pt;
5162 size_t len = 2; /* for leading slash and NUL */
5164 TAILQ_FOREACH(pt, parents, entry)
5165 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5168 len += strlen(got_tree_entry_get_name(te));
5170 *path = calloc(1, len);
5172 return got_error_from_errno("calloc");
5175 pt = TAILQ_LAST(parents, tog_parent_trees);
5177 const char *name = got_tree_entry_get_name(pt->selected_entry);
5178 if (strlcat(*path, name, len) >= len) {
5179 err = got_error(GOT_ERR_NO_SPACE);
5182 if (strlcat(*path, "/", len) >= len) {
5183 err = got_error(GOT_ERR_NO_SPACE);
5186 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5189 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5190 err = got_error(GOT_ERR_NO_SPACE);
5202 static const struct got_error *
5203 blame_tree_entry(struct tog_view **new_view, int begin_x,
5204 struct got_tree_entry *te, struct tog_parent_trees *parents,
5205 struct got_object_id *commit_id, struct got_repository *repo)
5207 const struct got_error *err = NULL;
5209 struct tog_view *blame_view;
5213 err = tree_entry_path(&path, parents, te);
5217 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5218 if (blame_view == NULL) {
5219 err = got_error_from_errno("view_open");
5223 err = open_blame_view(blame_view, path, commit_id, repo);
5225 if (err->code == GOT_ERR_CANCELLED)
5227 view_close(blame_view);
5229 *new_view = blame_view;
5235 static const struct got_error *
5236 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5237 struct tog_tree_view_state *s)
5239 struct tog_view *log_view;
5240 const struct got_error *err = NULL;
5245 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5246 if (log_view == NULL)
5247 return got_error_from_errno("view_open");
5249 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5253 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5256 view_close(log_view);
5258 *new_view = log_view;
5263 static const struct got_error *
5264 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5265 const char *head_ref_name, struct got_repository *repo)
5267 const struct got_error *err = NULL;
5268 char *commit_id_str = NULL;
5269 struct tog_tree_view_state *s = &view->state.tree;
5270 struct got_commit_object *commit = NULL;
5272 TAILQ_INIT(&s->parents);
5273 STAILQ_INIT(&s->colors);
5275 s->commit_id = got_object_id_dup(commit_id);
5276 if (s->commit_id == NULL)
5277 return got_error_from_errno("got_object_id_dup");
5279 err = got_object_open_as_commit(&commit, repo, commit_id);
5284 * The root is opened here and will be closed when the view is closed.
5285 * Any visited subtrees and their path-wise parents are opened and
5288 err = got_object_open_as_tree(&s->root, repo,
5289 got_object_commit_get_tree_id(commit));
5294 err = got_object_id_str(&commit_id_str, commit_id);
5298 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5299 err = got_error_from_errno("asprintf");
5303 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5304 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5305 if (head_ref_name) {
5306 s->head_ref_name = strdup(head_ref_name);
5307 if (s->head_ref_name == NULL) {
5308 err = got_error_from_errno("strdup");
5314 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5315 err = add_color(&s->colors, "\\$$",
5316 TOG_COLOR_TREE_SUBMODULE,
5317 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5320 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5321 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5324 err = add_color(&s->colors, "/$",
5325 TOG_COLOR_TREE_DIRECTORY,
5326 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5330 err = add_color(&s->colors, "\\*$",
5331 TOG_COLOR_TREE_EXECUTABLE,
5332 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5336 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5337 get_color_value("TOG_COLOR_COMMIT"));
5342 view->show = show_tree_view;
5343 view->input = input_tree_view;
5344 view->close = close_tree_view;
5345 view->search_start = search_start_tree_view;
5346 view->search_next = search_next_tree_view;
5348 free(commit_id_str);
5350 got_object_commit_close(commit);
5352 close_tree_view(view);
5356 static const struct got_error *
5357 close_tree_view(struct tog_view *view)
5359 struct tog_tree_view_state *s = &view->state.tree;
5361 free_colors(&s->colors);
5362 free(s->tree_label);
5363 s->tree_label = NULL;
5365 s->commit_id = NULL;
5366 free(s->head_ref_name);
5367 s->head_ref_name = NULL;
5368 while (!TAILQ_EMPTY(&s->parents)) {
5369 struct tog_parent_tree *parent;
5370 parent = TAILQ_FIRST(&s->parents);
5371 TAILQ_REMOVE(&s->parents, parent, entry);
5372 if (parent->tree != s->root)
5373 got_object_tree_close(parent->tree);
5377 if (s->tree != NULL && s->tree != s->root)
5378 got_object_tree_close(s->tree);
5380 got_object_tree_close(s->root);
5384 static const struct got_error *
5385 search_start_tree_view(struct tog_view *view)
5387 struct tog_tree_view_state *s = &view->state.tree;
5389 s->matched_entry = NULL;
5394 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5396 regmatch_t regmatch;
5398 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5402 static const struct got_error *
5403 search_next_tree_view(struct tog_view *view)
5405 struct tog_tree_view_state *s = &view->state.tree;
5406 struct got_tree_entry *te = NULL;
5408 if (!view->searching) {
5409 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5413 if (s->matched_entry) {
5414 if (view->searching == TOG_SEARCH_FORWARD) {
5415 if (s->selected_entry)
5416 te = got_tree_entry_get_next(s->tree,
5419 te = got_object_tree_get_first_entry(s->tree);
5421 if (s->selected_entry == NULL)
5422 te = got_object_tree_get_last_entry(s->tree);
5424 te = got_tree_entry_get_prev(s->tree,
5428 if (s->selected_entry)
5429 te = s->selected_entry;
5430 else if (view->searching == TOG_SEARCH_FORWARD)
5431 te = got_object_tree_get_first_entry(s->tree);
5433 te = got_object_tree_get_last_entry(s->tree);
5438 if (s->matched_entry == NULL) {
5439 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5442 if (view->searching == TOG_SEARCH_FORWARD)
5443 te = got_object_tree_get_first_entry(s->tree);
5445 te = got_object_tree_get_last_entry(s->tree);
5448 if (match_tree_entry(te, &view->regex)) {
5449 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5450 s->matched_entry = te;
5454 if (view->searching == TOG_SEARCH_FORWARD)
5455 te = got_tree_entry_get_next(s->tree, te);
5457 te = got_tree_entry_get_prev(s->tree, te);
5460 if (s->matched_entry) {
5461 s->first_displayed_entry = s->matched_entry;
5468 static const struct got_error *
5469 show_tree_view(struct tog_view *view)
5471 const struct got_error *err = NULL;
5472 struct tog_tree_view_state *s = &view->state.tree;
5475 err = tree_entry_path(&parent_path, &s->parents, NULL);
5479 err = draw_tree_entries(view, parent_path);
5486 static const struct got_error *
5487 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5489 const struct got_error *err = NULL;
5490 struct tog_tree_view_state *s = &view->state.tree;
5491 struct tog_view *log_view, *ref_view;
5492 struct got_tree_entry *te;
5497 s->show_ids = !s->show_ids;
5500 if (!s->selected_entry)
5502 if (view_is_parent_view(view))
5503 begin_x = view_split_begin_x(view->begin_x);
5504 err = log_selected_tree_entry(&log_view, begin_x, s);
5506 log_view->focussed = 1;
5507 if (view_is_parent_view(view)) {
5508 err = view_close_child(view);
5511 view_set_child(view, log_view);
5512 view->focus_child = 1;
5514 *new_view = log_view;
5517 if (view_is_parent_view(view))
5518 begin_x = view_split_begin_x(view->begin_x);
5519 ref_view = view_open(view->nlines, view->ncols,
5520 view->begin_y, begin_x, TOG_VIEW_REF);
5521 if (ref_view == NULL)
5522 return got_error_from_errno("view_open");
5523 err = open_ref_view(ref_view, s->repo);
5525 view_close(ref_view);
5529 ref_view->focussed = 1;
5530 if (view_is_parent_view(view)) {
5531 err = view_close_child(view);
5534 view_set_child(view, ref_view);
5535 view->focus_child = 1;
5537 *new_view = ref_view;
5542 if (s->tree == s->root)
5543 s->first_displayed_entry =
5544 got_object_tree_get_first_entry(s->tree);
5546 s->first_displayed_entry = NULL;
5551 te = got_object_tree_get_last_entry(s->tree);
5552 for (n = 0; n < view->nlines - 3; n++) {
5554 if(s->tree != s->root) {
5555 s->first_displayed_entry = NULL;
5560 s->first_displayed_entry = te;
5561 te = got_tree_entry_get_prev(s->tree, te);
5564 s->selected = n - 1;
5569 if (s->selected > 0) {
5573 tree_scroll_up(s, 1);
5577 if (s->tree == s->root) {
5578 if (got_object_tree_get_first_entry(s->tree) ==
5579 s->first_displayed_entry)
5582 if (s->first_displayed_entry == NULL)
5585 tree_scroll_up(s, MAX(0, view->nlines - 3));
5590 if (s->selected < s->ndisplayed - 1) {
5594 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5596 /* can't scroll any further */
5598 tree_scroll_down(s, 1);
5602 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5604 /* can't scroll any further; move cursor down */
5605 if (s->selected < s->ndisplayed - 1)
5606 s->selected = s->ndisplayed - 1;
5609 tree_scroll_down(s, view->nlines - 3);
5614 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5615 struct tog_parent_tree *parent;
5616 /* user selected '..' */
5617 if (s->tree == s->root)
5619 parent = TAILQ_FIRST(&s->parents);
5620 TAILQ_REMOVE(&s->parents, parent,
5622 got_object_tree_close(s->tree);
5623 s->tree = parent->tree;
5624 s->first_displayed_entry =
5625 parent->first_displayed_entry;
5627 parent->selected_entry;
5628 s->selected = parent->selected;
5630 } else if (S_ISDIR(got_tree_entry_get_mode(
5631 s->selected_entry))) {
5632 struct got_tree_object *subtree;
5633 err = got_object_open_as_tree(&subtree, s->repo,
5634 got_tree_entry_get_id(s->selected_entry));
5637 err = tree_view_visit_subtree(s, subtree);
5639 got_object_tree_close(subtree);
5642 } else if (S_ISREG(got_tree_entry_get_mode(
5643 s->selected_entry))) {
5644 struct tog_view *blame_view;
5645 int begin_x = view_is_parent_view(view) ?
5646 view_split_begin_x(view->begin_x) : 0;
5648 err = blame_tree_entry(&blame_view, begin_x,
5649 s->selected_entry, &s->parents,
5650 s->commit_id, s->repo);
5654 blame_view->focussed = 1;
5655 if (view_is_parent_view(view)) {
5656 err = view_close_child(view);
5659 view_set_child(view, blame_view);
5660 view->focus_child = 1;
5662 *new_view = blame_view;
5666 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5667 s->selected = view->nlines - 4;
5680 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5685 static const struct got_error *
5686 cmd_tree(int argc, char *argv[])
5688 const struct got_error *error;
5689 struct got_repository *repo = NULL;
5690 struct got_worktree *worktree = NULL;
5691 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5692 struct got_object_id *commit_id = NULL;
5693 struct got_commit_object *commit = NULL;
5694 const char *commit_id_arg = NULL;
5696 struct got_reference *ref = NULL;
5697 const char *head_ref_name = NULL;
5699 struct tog_view *view;
5701 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5704 commit_id_arg = optarg;
5707 repo_path = realpath(optarg, NULL);
5708 if (repo_path == NULL)
5709 return got_error_from_errno2("realpath",
5724 if (repo_path == NULL) {
5725 cwd = getcwd(NULL, 0);
5727 return got_error_from_errno("getcwd");
5728 error = got_worktree_open(&worktree, cwd);
5729 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5733 strdup(got_worktree_get_repo_path(worktree));
5735 repo_path = strdup(cwd);
5736 if (repo_path == NULL) {
5737 error = got_error_from_errno("strdup");
5742 error = got_repo_open(&repo, repo_path, NULL);
5746 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5753 error = apply_unveil(got_repo_get_path(repo), NULL);
5757 error = tog_load_refs(repo, 0);
5761 if (commit_id_arg == NULL) {
5762 error = got_repo_match_object_id(&commit_id, &label,
5763 worktree ? got_worktree_get_head_ref_name(worktree) :
5764 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5767 head_ref_name = label;
5769 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5771 head_ref_name = got_ref_get_name(ref);
5772 else if (error->code != GOT_ERR_NOT_REF)
5774 error = got_repo_match_object_id(&commit_id, NULL,
5775 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5780 error = got_object_open_as_commit(&commit, repo, commit_id);
5784 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5786 error = got_error_from_errno("view_open");
5789 error = open_tree_view(view, commit_id, head_ref_name, repo);
5792 if (!got_path_is_root_dir(in_repo_path)) {
5793 error = tree_view_walk_path(&view->state.tree, commit,
5800 /* Release work tree lock. */
5801 got_worktree_close(worktree);
5804 error = view_loop(view);
5813 const struct got_error *close_err = got_repo_close(repo);
5821 static const struct got_error *
5822 ref_view_load_refs(struct tog_ref_view_state *s)
5824 struct got_reflist_entry *sre;
5825 struct tog_reflist_entry *re;
5828 TAILQ_FOREACH(sre, &tog_refs, entry) {
5829 if (strncmp(got_ref_get_name(sre->ref),
5830 "refs/got/", 9) == 0 &&
5831 strncmp(got_ref_get_name(sre->ref),
5832 "refs/got/backup/", 16) != 0)
5835 re = malloc(sizeof(*re));
5837 return got_error_from_errno("malloc");
5839 re->ref = got_ref_dup(sre->ref);
5840 if (re->ref == NULL)
5841 return got_error_from_errno("got_ref_dup");
5842 re->idx = s->nrefs++;
5843 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5846 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5851 ref_view_free_refs(struct tog_ref_view_state *s)
5853 struct tog_reflist_entry *re;
5855 while (!TAILQ_EMPTY(&s->refs)) {
5856 re = TAILQ_FIRST(&s->refs);
5857 TAILQ_REMOVE(&s->refs, re, entry);
5858 got_ref_close(re->ref);
5863 static const struct got_error *
5864 open_ref_view(struct tog_view *view, struct got_repository *repo)
5866 const struct got_error *err = NULL;
5867 struct tog_ref_view_state *s = &view->state.ref;
5869 s->selected_entry = 0;
5872 TAILQ_INIT(&s->refs);
5873 STAILQ_INIT(&s->colors);
5875 err = ref_view_load_refs(s);
5879 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5880 err = add_color(&s->colors, "^refs/heads/",
5881 TOG_COLOR_REFS_HEADS,
5882 get_color_value("TOG_COLOR_REFS_HEADS"));
5886 err = add_color(&s->colors, "^refs/tags/",
5887 TOG_COLOR_REFS_TAGS,
5888 get_color_value("TOG_COLOR_REFS_TAGS"));
5892 err = add_color(&s->colors, "^refs/remotes/",
5893 TOG_COLOR_REFS_REMOTES,
5894 get_color_value("TOG_COLOR_REFS_REMOTES"));
5898 err = add_color(&s->colors, "^refs/got/backup/",
5899 TOG_COLOR_REFS_BACKUP,
5900 get_color_value("TOG_COLOR_REFS_BACKUP"));
5905 view->show = show_ref_view;
5906 view->input = input_ref_view;
5907 view->close = close_ref_view;
5908 view->search_start = search_start_ref_view;
5909 view->search_next = search_next_ref_view;
5912 free_colors(&s->colors);
5916 static const struct got_error *
5917 close_ref_view(struct tog_view *view)
5919 struct tog_ref_view_state *s = &view->state.ref;
5921 ref_view_free_refs(s);
5922 free_colors(&s->colors);
5927 static const struct got_error *
5928 resolve_reflist_entry(struct got_object_id **commit_id,
5929 struct tog_reflist_entry *re, struct got_repository *repo)
5931 const struct got_error *err = NULL;
5932 struct got_object_id *obj_id;
5933 struct got_tag_object *tag = NULL;
5938 err = got_ref_resolve(&obj_id, repo, re->ref);
5942 err = got_object_get_type(&obj_type, repo, obj_id);
5947 case GOT_OBJ_TYPE_COMMIT:
5948 *commit_id = obj_id;
5950 case GOT_OBJ_TYPE_TAG:
5951 err = got_object_open_as_tag(&tag, repo, obj_id);
5955 err = got_object_get_type(&obj_type, repo,
5956 got_object_tag_get_object_id(tag));
5959 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5960 err = got_error(GOT_ERR_OBJ_TYPE);
5963 *commit_id = got_object_id_dup(
5964 got_object_tag_get_object_id(tag));
5965 if (*commit_id == NULL) {
5966 err = got_error_from_errno("got_object_id_dup");
5971 err = got_error(GOT_ERR_OBJ_TYPE);
5977 got_object_tag_close(tag);
5985 static const struct got_error *
5986 log_ref_entry(struct tog_view **new_view, int begin_x,
5987 struct tog_reflist_entry *re, struct got_repository *repo)
5989 struct tog_view *log_view;
5990 const struct got_error *err = NULL;
5991 struct got_object_id *commit_id = NULL;
5995 err = resolve_reflist_entry(&commit_id, re, repo);
5997 if (err->code != GOT_ERR_OBJ_TYPE)
6003 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6004 if (log_view == NULL) {
6005 err = got_error_from_errno("view_open");
6009 err = open_log_view(log_view, commit_id, repo,
6010 got_ref_get_name(re->ref), "", 0);
6013 view_close(log_view);
6015 *new_view = log_view;
6021 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6023 struct tog_reflist_entry *re;
6026 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6029 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6030 while (i++ < maxscroll) {
6033 s->first_displayed_entry = re;
6034 re = TAILQ_PREV(re, tog_reflist_head, entry);
6039 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6041 struct tog_reflist_entry *next, *last;
6044 if (s->first_displayed_entry)
6045 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6047 next = TAILQ_FIRST(&s->refs);
6049 last = s->last_displayed_entry;
6050 while (next && last && n++ < maxscroll) {
6051 last = TAILQ_NEXT(last, entry);
6053 s->first_displayed_entry = next;
6054 next = TAILQ_NEXT(next, entry);
6059 static const struct got_error *
6060 search_start_ref_view(struct tog_view *view)
6062 struct tog_ref_view_state *s = &view->state.ref;
6064 s->matched_entry = NULL;
6069 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6071 regmatch_t regmatch;
6073 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6077 static const struct got_error *
6078 search_next_ref_view(struct tog_view *view)
6080 struct tog_ref_view_state *s = &view->state.ref;
6081 struct tog_reflist_entry *re = NULL;
6083 if (!view->searching) {
6084 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6088 if (s->matched_entry) {
6089 if (view->searching == TOG_SEARCH_FORWARD) {
6090 if (s->selected_entry)
6091 re = TAILQ_NEXT(s->selected_entry, entry);
6093 re = TAILQ_PREV(s->selected_entry,
6094 tog_reflist_head, entry);
6096 if (s->selected_entry == NULL)
6097 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6099 re = TAILQ_PREV(s->selected_entry,
6100 tog_reflist_head, entry);
6103 if (s->selected_entry)
6104 re = s->selected_entry;
6105 else if (view->searching == TOG_SEARCH_FORWARD)
6106 re = TAILQ_FIRST(&s->refs);
6108 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6113 if (s->matched_entry == NULL) {
6114 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6117 if (view->searching == TOG_SEARCH_FORWARD)
6118 re = TAILQ_FIRST(&s->refs);
6120 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6123 if (match_reflist_entry(re, &view->regex)) {
6124 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6125 s->matched_entry = re;
6129 if (view->searching == TOG_SEARCH_FORWARD)
6130 re = TAILQ_NEXT(re, entry);
6132 re = TAILQ_PREV(re, tog_reflist_head, entry);
6135 if (s->matched_entry) {
6136 s->first_displayed_entry = s->matched_entry;
6143 static const struct got_error *
6144 show_ref_view(struct tog_view *view)
6146 const struct got_error *err = NULL;
6147 struct tog_ref_view_state *s = &view->state.ref;
6148 struct tog_reflist_entry *re;
6151 struct tog_color *tc;
6153 int limit = view->nlines;
6155 werase(view->window);
6162 re = s->first_displayed_entry;
6164 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6166 return got_error_from_errno("asprintf");
6168 err = format_line(&wline, &width, line, view->ncols, 0);
6173 if (view_needs_focus_indication(view))
6174 wstandout(view->window);
6175 waddwstr(view->window, wline);
6176 if (view_needs_focus_indication(view))
6177 wstandend(view->window);
6182 if (width < view->ncols - 1)
6183 waddch(view->window, '\n');
6188 while (re && limit > 0) {
6191 if (got_ref_is_symbolic(re->ref)) {
6192 if (asprintf(&line, "%s -> %s",
6193 got_ref_get_name(re->ref),
6194 got_ref_get_symref_target(re->ref)) == -1)
6195 return got_error_from_errno("asprintf");
6196 } else if (s->show_ids) {
6197 struct got_object_id *id;
6199 err = got_ref_resolve(&id, s->repo, re->ref);
6202 err = got_object_id_str(&id_str, id);
6207 if (asprintf(&line, "%s: %s",
6208 got_ref_get_name(re->ref), id_str) == -1) {
6209 err = got_error_from_errno("asprintf");
6217 line = strdup(got_ref_get_name(re->ref));
6219 return got_error_from_errno("strdup");
6222 err = format_line(&wline, &width, line, view->ncols, 0);
6227 if (n == s->selected) {
6229 wstandout(view->window);
6230 s->selected_entry = re;
6232 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6234 wattr_on(view->window,
6235 COLOR_PAIR(tc->colorpair), NULL);
6236 waddwstr(view->window, wline);
6238 wattr_off(view->window,
6239 COLOR_PAIR(tc->colorpair), NULL);
6240 if (width < view->ncols - 1)
6241 waddch(view->window, '\n');
6242 if (n == s->selected && view->focussed)
6243 wstandend(view->window);
6249 s->last_displayed_entry = re;
6252 re = TAILQ_NEXT(re, entry);
6259 static const struct got_error *
6260 browse_ref_tree(struct tog_view **new_view, int begin_x,
6261 struct tog_reflist_entry *re, struct got_repository *repo)
6263 const struct got_error *err = NULL;
6264 struct got_object_id *commit_id = NULL;
6265 struct tog_view *tree_view;
6269 err = resolve_reflist_entry(&commit_id, re, repo);
6271 if (err->code != GOT_ERR_OBJ_TYPE)
6278 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6279 if (tree_view == NULL) {
6280 err = got_error_from_errno("view_open");
6284 err = open_tree_view(tree_view, commit_id,
6285 got_ref_get_name(re->ref), repo);
6289 *new_view = tree_view;
6294 static const struct got_error *
6295 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6297 const struct got_error *err = NULL;
6298 struct tog_ref_view_state *s = &view->state.ref;
6299 struct tog_view *log_view, *tree_view;
6300 struct tog_reflist_entry *re;
6305 s->show_ids = !s->show_ids;
6308 s->sort_by_date = !s->sort_by_date;
6309 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6310 got_ref_cmp_by_commit_timestamp_descending :
6311 tog_ref_cmp_by_name, s->repo);
6314 got_reflist_object_id_map_free(tog_refs_idmap);
6315 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6316 &tog_refs, s->repo);
6319 ref_view_free_refs(s);
6320 err = ref_view_load_refs(s);
6324 if (!s->selected_entry)
6326 if (view_is_parent_view(view))
6327 begin_x = view_split_begin_x(view->begin_x);
6328 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6331 log_view->focussed = 1;
6332 if (view_is_parent_view(view)) {
6333 err = view_close_child(view);
6336 view_set_child(view, log_view);
6337 view->focus_child = 1;
6339 *new_view = log_view;
6342 if (!s->selected_entry)
6344 if (view_is_parent_view(view))
6345 begin_x = view_split_begin_x(view->begin_x);
6346 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6348 if (err || tree_view == NULL)
6351 tree_view->focussed = 1;
6352 if (view_is_parent_view(view)) {
6353 err = view_close_child(view);
6356 view_set_child(view, tree_view);
6357 view->focus_child = 1;
6359 *new_view = tree_view;
6364 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6369 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6370 for (n = 0; n < view->nlines - 1; n++) {
6373 s->first_displayed_entry = re;
6374 re = TAILQ_PREV(re, tog_reflist_head, entry);
6377 s->selected = n - 1;
6382 if (s->selected > 0) {
6386 ref_scroll_up(s, 1);
6390 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6392 ref_scroll_up(s, MAX(0, view->nlines - 1));
6397 if (s->selected < s->ndisplayed - 1) {
6401 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6402 /* can't scroll any further */
6404 ref_scroll_down(s, 1);
6408 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6409 /* can't scroll any further; move cursor down */
6410 if (s->selected < s->ndisplayed - 1)
6411 s->selected = s->ndisplayed - 1;
6414 ref_scroll_down(s, view->nlines - 1);
6418 err = tog_load_refs(s->repo, s->sort_by_date);
6421 ref_view_free_refs(s);
6422 err = ref_view_load_refs(s);
6425 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6426 s->selected = view->nlines - 2;
6439 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6444 static const struct got_error *
6445 cmd_ref(int argc, char *argv[])
6447 const struct got_error *error;
6448 struct got_repository *repo = NULL;
6449 struct got_worktree *worktree = NULL;
6450 char *cwd = NULL, *repo_path = NULL;
6452 struct tog_view *view;
6454 while ((ch = getopt(argc, argv, "r:")) != -1) {
6457 repo_path = realpath(optarg, NULL);
6458 if (repo_path == NULL)
6459 return got_error_from_errno2("realpath",
6474 if (repo_path == NULL) {
6475 cwd = getcwd(NULL, 0);
6477 return got_error_from_errno("getcwd");
6478 error = got_worktree_open(&worktree, cwd);
6479 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6483 strdup(got_worktree_get_repo_path(worktree));
6485 repo_path = strdup(cwd);
6486 if (repo_path == NULL) {
6487 error = got_error_from_errno("strdup");
6492 error = got_repo_open(&repo, repo_path, NULL);
6498 error = apply_unveil(got_repo_get_path(repo), NULL);
6502 error = tog_load_refs(repo, 0);
6506 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6508 error = got_error_from_errno("view_open");
6512 error = open_ref_view(view, repo);
6517 /* Release work tree lock. */
6518 got_worktree_close(worktree);
6521 error = view_loop(view);
6526 const struct got_error *close_err = got_repo_close(repo);
6535 list_commands(FILE *fp)
6539 fprintf(fp, "commands:");
6540 for (i = 0; i < nitems(tog_commands); i++) {
6541 const struct tog_cmd *cmd = &tog_commands[i];
6542 fprintf(fp, " %s", cmd->name);
6548 usage(int hflag, int status)
6550 FILE *fp = (status == 0) ? stdout : stderr;
6552 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6555 fprintf(fp, "lazy usage: %s path\n", getprogname());
6562 make_argv(int argc, ...)
6570 argv = calloc(argc, sizeof(char *));
6573 for (i = 0; i < argc; i++) {
6574 argv[i] = strdup(va_arg(ap, char *));
6575 if (argv[i] == NULL)
6584 * Try to convert 'tog path' into a 'tog log path' command.
6585 * The user could simply have mistyped the command rather than knowingly
6586 * provided a path. So check whether argv[0] can in fact be resolved
6587 * to a path in the HEAD commit and print a special error if not.
6588 * This hack is for mpi@ <3
6590 static const struct got_error *
6591 tog_log_with_path(int argc, char *argv[])
6593 const struct got_error *error = NULL, *close_err;
6594 const struct tog_cmd *cmd = NULL;
6595 struct got_repository *repo = NULL;
6596 struct got_worktree *worktree = NULL;
6597 struct got_object_id *commit_id = NULL, *id = NULL;
6598 struct got_commit_object *commit = NULL;
6599 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6600 char *commit_id_str = NULL, **cmd_argv = NULL;
6602 cwd = getcwd(NULL, 0);
6604 return got_error_from_errno("getcwd");
6606 error = got_worktree_open(&worktree, cwd);
6607 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6611 repo_path = strdup(got_worktree_get_repo_path(worktree));
6613 repo_path = strdup(cwd);
6614 if (repo_path == NULL) {
6615 error = got_error_from_errno("strdup");
6619 error = got_repo_open(&repo, repo_path, NULL);
6623 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6628 error = tog_load_refs(repo, 0);
6631 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6632 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6633 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6638 got_worktree_close(worktree);
6642 error = got_object_open_as_commit(&commit, repo, commit_id);
6646 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6648 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6650 fprintf(stderr, "%s: '%s' is no known command or path\n",
6651 getprogname(), argv[0]);
6656 close_err = got_repo_close(repo);
6661 error = got_object_id_str(&commit_id_str, commit_id);
6665 cmd = &tog_commands[0]; /* log */
6667 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6668 error = cmd->cmd_main(argc, cmd_argv);
6671 close_err = got_repo_close(repo);
6676 got_object_commit_close(commit);
6678 got_worktree_close(worktree);
6680 free(commit_id_str);
6687 for (i = 0; i < argc; i++)
6696 main(int argc, char *argv[])
6698 const struct got_error *error = NULL;
6699 const struct tog_cmd *cmd = NULL;
6700 int ch, hflag = 0, Vflag = 0;
6701 char **cmd_argv = NULL;
6702 static const struct option longopts[] = {
6703 { "version", no_argument, NULL, 'V' },
6707 setlocale(LC_CTYPE, "");
6709 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6729 got_version_print_str();
6734 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6742 /* Build an argument vector which runs a default command. */
6743 cmd = &tog_commands[0];
6745 cmd_argv = make_argv(argc, cmd->name);
6749 /* Did the user specify a command? */
6750 for (i = 0; i < nitems(tog_commands); i++) {
6751 if (strncmp(tog_commands[i].name, argv[0],
6752 strlen(argv[0])) == 0) {
6753 cmd = &tog_commands[i];
6762 /* No command specified; try log with a path */
6763 error = tog_log_with_path(argc, argv);
6768 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6775 for (i = 0; i < argc; i++)
6780 if (error && error->code != GOT_ERR_CANCELLED)
6781 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);