2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED
25 #undef _XOPEN_SOURCE_EXTENDED
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
49 #include "got_opentemp.h"
51 #include "got_cancel.h"
52 #include "got_commit_graph.h"
53 #include "got_blame.h"
54 #include "got_privsep.h"
56 #include "got_worktree.h"
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
66 #define CTRL(x) ((x) & 0x1f)
69 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
78 __dead static void usage(int, int);
79 __dead static void usage_log(void);
80 __dead static void usage_diff(void);
81 __dead static void usage_blame(void);
82 __dead static void usage_tree(void);
83 __dead static void usage_ref(void);
85 static const struct got_error* cmd_log(int, char *[]);
86 static const struct got_error* cmd_diff(int, char *[]);
87 static const struct got_error* cmd_blame(int, char *[]);
88 static const struct got_error* cmd_tree(int, char *[]);
89 static const struct got_error* cmd_ref(int, char *[]);
91 static struct tog_cmd tog_commands[] = {
92 { "log", cmd_log, usage_log },
93 { "diff", cmd_diff, usage_diff },
94 { "blame", cmd_blame, usage_blame },
95 { "tree", cmd_tree, usage_tree },
96 { "ref", cmd_ref, usage_ref },
107 #define TOG_EOF_STRING "(END)"
109 struct commit_queue_entry {
110 TAILQ_ENTRY(commit_queue_entry) entry;
111 struct got_object_id *id;
112 struct got_commit_object *commit;
115 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
116 struct commit_queue {
118 struct commit_queue_head head;
122 STAILQ_ENTRY(tog_color) entry;
126 STAILQ_HEAD(tog_colors, tog_color);
128 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
129 static struct got_reflist_object_id_map *tog_refs_idmap;
131 static const struct got_error *
132 tog_load_refs(struct got_repository *repo)
134 const struct got_error *err;
136 err = got_ref_list(&tog_refs, repo, NULL, got_ref_cmp_by_name, NULL);
140 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
147 if (tog_refs_idmap) {
148 got_reflist_object_id_map_free(tog_refs_idmap);
149 tog_refs_idmap = NULL;
151 got_ref_list_free(&tog_refs);
154 static const struct got_error *
155 add_color(struct tog_colors *colors, const char *pattern,
156 int idx, short color)
158 const struct got_error *err = NULL;
159 struct tog_color *tc;
162 if (idx < 1 || idx > COLOR_PAIRS - 1)
165 init_pair(idx, color, -1);
167 tc = calloc(1, sizeof(*tc));
169 return got_error_from_errno("calloc");
170 regerr = regcomp(&tc->regex, pattern,
171 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
173 static char regerr_msg[512];
174 static char err_msg[512];
175 regerror(regerr, &tc->regex, regerr_msg,
177 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
179 err = got_error_msg(GOT_ERR_REGEX, err_msg);
184 STAILQ_INSERT_HEAD(colors, tc, entry);
189 free_colors(struct tog_colors *colors)
191 struct tog_color *tc;
193 while (!STAILQ_EMPTY(colors)) {
194 tc = STAILQ_FIRST(colors);
195 STAILQ_REMOVE_HEAD(colors, entry);
202 get_color(struct tog_colors *colors, int colorpair)
204 struct tog_color *tc = NULL;
206 STAILQ_FOREACH(tc, colors, entry) {
207 if (tc->colorpair == colorpair)
215 default_color_value(const char *envvar)
217 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
218 return COLOR_MAGENTA;
219 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
221 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
223 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
225 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
226 return COLOR_MAGENTA;
227 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
228 return COLOR_MAGENTA;
229 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
231 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
233 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
235 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
237 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
239 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
241 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
242 return COLOR_MAGENTA;
243 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
250 get_color_value(const char *envvar)
252 const char *val = getenv(envvar);
255 return default_color_value(envvar);
257 if (strcasecmp(val, "black") == 0)
259 if (strcasecmp(val, "red") == 0)
261 if (strcasecmp(val, "green") == 0)
263 if (strcasecmp(val, "yellow") == 0)
265 if (strcasecmp(val, "blue") == 0)
267 if (strcasecmp(val, "magenta") == 0)
268 return COLOR_MAGENTA;
269 if (strcasecmp(val, "cyan") == 0)
271 if (strcasecmp(val, "white") == 0)
273 if (strcasecmp(val, "default") == 0)
276 return default_color_value(envvar);
280 struct tog_diff_view_state {
281 struct got_object_id *id1, *id2;
282 const char *label1, *label2;
284 int first_displayed_line;
285 int last_displayed_line;
288 int ignore_whitespace;
290 struct got_repository *repo;
291 struct tog_colors colors;
297 /* passed from log view; may be NULL */
298 struct tog_view *log_view;
301 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
303 struct tog_log_thread_args {
304 pthread_cond_t need_commits;
305 pthread_cond_t commit_loaded;
308 struct got_commit_graph *graph;
309 struct commit_queue *commits;
310 const char *in_repo_path;
311 struct got_object_id *start_id;
312 struct got_repository *repo;
315 struct commit_queue_entry **first_displayed_entry;
316 struct commit_queue_entry **selected_entry;
318 int *search_next_done;
322 struct tog_log_view_state {
323 struct commit_queue commits;
324 struct commit_queue_entry *first_displayed_entry;
325 struct commit_queue_entry *last_displayed_entry;
326 struct commit_queue_entry *selected_entry;
331 struct got_repository *repo;
332 struct got_object_id *start_id;
335 struct tog_log_thread_args thread_args;
336 struct commit_queue_entry *matched_entry;
337 struct commit_queue_entry *search_entry;
338 struct tog_colors colors;
341 #define TOG_COLOR_DIFF_MINUS 1
342 #define TOG_COLOR_DIFF_PLUS 2
343 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
344 #define TOG_COLOR_DIFF_META 4
345 #define TOG_COLOR_TREE_SUBMODULE 5
346 #define TOG_COLOR_TREE_SYMLINK 6
347 #define TOG_COLOR_TREE_DIRECTORY 7
348 #define TOG_COLOR_TREE_EXECUTABLE 8
349 #define TOG_COLOR_COMMIT 9
350 #define TOG_COLOR_AUTHOR 10
351 #define TOG_COLOR_DATE 11
352 #define TOG_COLOR_REFS_HEADS 12
353 #define TOG_COLOR_REFS_TAGS 13
354 #define TOG_COLOR_REFS_REMOTES 14
356 struct tog_blame_cb_args {
357 struct tog_blame_line *lines; /* one per line */
360 struct tog_view *view;
361 struct got_object_id *commit_id;
365 struct tog_blame_thread_args {
367 struct got_repository *repo;
368 struct tog_blame_cb_args *cb_args;
370 got_cancel_cb cancel_cb;
377 struct tog_blame_line *lines;
381 struct tog_blame_thread_args thread_args;
382 struct tog_blame_cb_args cb_args;
386 struct tog_blame_view_state {
387 int first_displayed_line;
388 int last_displayed_line;
393 struct got_object_id_queue blamed_commits;
394 struct got_object_qid *blamed_commit;
396 struct got_repository *repo;
397 struct got_object_id *commit_id;
398 struct tog_blame blame;
400 struct tog_colors colors;
403 struct tog_parent_tree {
404 TAILQ_ENTRY(tog_parent_tree) entry;
405 struct got_tree_object *tree;
406 struct got_tree_entry *first_displayed_entry;
407 struct got_tree_entry *selected_entry;
411 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
413 struct tog_tree_view_state {
415 struct got_object_id *commit_id;/* commit which this tree belongs to */
416 struct got_tree_object *root; /* the commit's root tree entry */
417 struct got_tree_object *tree; /* currently displayed (sub-)tree */
418 struct got_tree_entry *first_displayed_entry;
419 struct got_tree_entry *last_displayed_entry;
420 struct got_tree_entry *selected_entry;
421 int ndisplayed, selected, show_ids;
422 struct tog_parent_trees parents; /* parent trees of current sub-tree */
424 struct got_repository *repo;
425 struct got_tree_entry *matched_entry;
426 struct tog_colors colors;
429 struct tog_reflist_entry {
430 TAILQ_ENTRY(tog_reflist_entry) entry;
431 struct got_reference *ref;
435 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
437 struct tog_ref_view_state {
438 struct tog_reflist_head refs;
439 struct tog_reflist_entry *first_displayed_entry;
440 struct tog_reflist_entry *last_displayed_entry;
441 struct tog_reflist_entry *selected_entry;
442 int nrefs, ndisplayed, selected, show_ids;
443 struct got_repository *repo;
444 struct tog_reflist_entry *matched_entry;
445 struct tog_colors colors;
449 * We implement two types of views: parent views and child views.
451 * The 'Tab' key switches focus between a parent view and its child view.
452 * Child views are shown side-by-side to their parent view, provided
453 * there is enough screen estate.
455 * When a new view is opened from within a parent view, this new view
456 * becomes a child view of the parent view, replacing any existing child.
458 * When a new view is opened from within a child view, this new view
459 * becomes a parent view which will obscure the views below until the
460 * user quits the new parent view by typing 'q'.
462 * This list of views contains parent views only.
463 * Child views are only pointed to by their parent view.
465 TAILQ_HEAD(tog_view_list_head, tog_view);
468 TAILQ_ENTRY(tog_view) entry;
471 int nlines, ncols, begin_y, begin_x;
472 int lines, cols; /* copies of LINES and COLS */
473 int focussed; /* Only set on one parent or child view at a time. */
475 struct tog_view *parent;
476 struct tog_view *child;
479 * This flag is initially set on parent views when a new child view
480 * is created. It gets toggled when the 'Tab' key switches focus
481 * between parent and child.
482 * The flag indicates whether focus should be passed on to our child
483 * view if this parent view gets picked for focus after another parent
484 * view was closed. This prevents child views from losing focus in such
489 /* type-specific state */
490 enum tog_view_type type;
492 struct tog_diff_view_state diff;
493 struct tog_log_view_state log;
494 struct tog_blame_view_state blame;
495 struct tog_tree_view_state tree;
496 struct tog_ref_view_state ref;
499 const struct got_error *(*show)(struct tog_view *);
500 const struct got_error *(*input)(struct tog_view **,
501 struct tog_view *, int);
502 const struct got_error *(*close)(struct tog_view *);
504 const struct got_error *(*search_start)(struct tog_view *);
505 const struct got_error *(*search_next)(struct tog_view *);
508 #define TOG_SEARCH_FORWARD 1
509 #define TOG_SEARCH_BACKWARD 2
510 int search_next_done;
511 #define TOG_SEARCH_HAVE_MORE 1
512 #define TOG_SEARCH_NO_MORE 2
513 #define TOG_SEARCH_HAVE_NONE 3
518 static const struct got_error *open_diff_view(struct tog_view *,
519 struct got_object_id *, struct got_object_id *,
520 const char *, const char *, int, int, int, struct tog_view *,
521 struct got_repository *);
522 static const struct got_error *show_diff_view(struct tog_view *);
523 static const struct got_error *input_diff_view(struct tog_view **,
524 struct tog_view *, int);
525 static const struct got_error* close_diff_view(struct tog_view *);
526 static const struct got_error *search_start_diff_view(struct tog_view *);
527 static const struct got_error *search_next_diff_view(struct tog_view *);
529 static const struct got_error *open_log_view(struct tog_view *,
530 struct got_object_id *, struct got_repository *,
531 const char *, const char *, int);
532 static const struct got_error * show_log_view(struct tog_view *);
533 static const struct got_error *input_log_view(struct tog_view **,
534 struct tog_view *, int);
535 static const struct got_error *close_log_view(struct tog_view *);
536 static const struct got_error *search_start_log_view(struct tog_view *);
537 static const struct got_error *search_next_log_view(struct tog_view *);
539 static const struct got_error *open_blame_view(struct tog_view *, char *,
540 struct got_object_id *, struct got_repository *);
541 static const struct got_error *show_blame_view(struct tog_view *);
542 static const struct got_error *input_blame_view(struct tog_view **,
543 struct tog_view *, int);
544 static const struct got_error *close_blame_view(struct tog_view *);
545 static const struct got_error *search_start_blame_view(struct tog_view *);
546 static const struct got_error *search_next_blame_view(struct tog_view *);
548 static const struct got_error *open_tree_view(struct tog_view *,
549 struct got_object_id *, const char *, struct got_repository *);
550 static const struct got_error *show_tree_view(struct tog_view *);
551 static const struct got_error *input_tree_view(struct tog_view **,
552 struct tog_view *, int);
553 static const struct got_error *close_tree_view(struct tog_view *);
554 static const struct got_error *search_start_tree_view(struct tog_view *);
555 static const struct got_error *search_next_tree_view(struct tog_view *);
557 static const struct got_error *open_ref_view(struct tog_view *,
558 struct got_repository *);
559 static const struct got_error *show_ref_view(struct tog_view *);
560 static const struct got_error *input_ref_view(struct tog_view **,
561 struct tog_view *, int);
562 static const struct got_error *close_ref_view(struct tog_view *);
563 static const struct got_error *search_start_ref_view(struct tog_view *);
564 static const struct got_error *search_next_ref_view(struct tog_view *);
566 static volatile sig_atomic_t tog_sigwinch_received;
567 static volatile sig_atomic_t tog_sigpipe_received;
568 static volatile sig_atomic_t tog_sigcont_received;
571 tog_sigwinch(int signo)
573 tog_sigwinch_received = 1;
577 tog_sigpipe(int signo)
579 tog_sigpipe_received = 1;
583 tog_sigcont(int signo)
585 tog_sigcont_received = 1;
588 static const struct got_error *
589 view_close(struct tog_view *view)
591 const struct got_error *err = NULL;
594 view_close(view->child);
598 err = view->close(view);
600 del_panel(view->panel);
602 delwin(view->window);
607 static struct tog_view *
608 view_open(int nlines, int ncols, int begin_y, int begin_x,
609 enum tog_view_type type)
611 struct tog_view *view = calloc(1, sizeof(*view));
619 view->nlines = nlines ? nlines : LINES - begin_y;
620 view->ncols = ncols ? ncols : COLS - begin_x;
621 view->begin_y = begin_y;
622 view->begin_x = begin_x;
623 view->window = newwin(nlines, ncols, begin_y, begin_x);
624 if (view->window == NULL) {
628 view->panel = new_panel(view->window);
629 if (view->panel == NULL ||
630 set_panel_userptr(view->panel, view) != OK) {
635 keypad(view->window, TRUE);
640 view_split_begin_x(int begin_x)
642 if (begin_x > 0 || COLS < 120)
644 return (COLS - MAX(COLS / 2, 80));
647 static const struct got_error *view_resize(struct tog_view *);
649 static const struct got_error *
650 view_splitscreen(struct tog_view *view)
652 const struct got_error *err = NULL;
655 view->begin_x = view_split_begin_x(0);
656 view->nlines = LINES;
657 view->ncols = COLS - view->begin_x;
660 err = view_resize(view);
664 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
665 return got_error_from_errno("mvwin");
670 static const struct got_error *
671 view_fullscreen(struct tog_view *view)
673 const struct got_error *err = NULL;
677 view->nlines = LINES;
681 err = view_resize(view);
685 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
686 return got_error_from_errno("mvwin");
692 view_is_parent_view(struct tog_view *view)
694 return view->parent == NULL;
697 static const struct got_error *
698 view_resize(struct tog_view *view)
702 if (view->lines > LINES)
703 nlines = view->nlines - (view->lines - LINES);
705 nlines = view->nlines + (LINES - view->lines);
707 if (view->cols > COLS)
708 ncols = view->ncols - (view->cols - COLS);
710 ncols = view->ncols + (COLS - view->cols);
712 if (wresize(view->window, nlines, ncols) == ERR)
713 return got_error_from_errno("wresize");
714 if (replace_panel(view->panel, view->window) == ERR)
715 return got_error_from_errno("replace_panel");
716 wclear(view->window);
718 view->nlines = nlines;
724 view->child->begin_x = view_split_begin_x(view->begin_x);
725 if (view->child->begin_x == 0) {
726 view_fullscreen(view->child);
727 if (view->child->focussed)
728 show_panel(view->child->panel);
730 show_panel(view->panel);
732 view_splitscreen(view->child);
733 show_panel(view->child->panel);
740 static const struct got_error *
741 view_close_child(struct tog_view *view)
743 const struct got_error *err = NULL;
745 if (view->child == NULL)
748 err = view_close(view->child);
754 view_set_child(struct tog_view *view, struct tog_view *child)
757 child->parent = view;
761 view_is_splitscreen(struct tog_view *view)
763 return view->begin_x > 0;
772 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
773 cols = 80; /* Default */
779 resize_term(lines, cols);
782 static const struct got_error *
783 view_search_start(struct tog_view *view)
785 const struct got_error *err = NULL;
789 if (view->search_started) {
790 regfree(&view->regex);
792 memset(&view->regmatch, 0, sizeof(view->regmatch));
794 view->search_started = 0;
796 if (view->nlines < 1)
799 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
800 wclrtoeol(view->window);
804 ret = wgetnstr(view->window, pattern, sizeof(pattern));
810 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
811 err = view->search_start(view);
813 regfree(&view->regex);
816 view->search_started = 1;
817 view->searching = TOG_SEARCH_FORWARD;
818 view->search_next_done = 0;
819 view->search_next(view);
825 static const struct got_error *
826 view_input(struct tog_view **new, int *done, struct tog_view *view,
827 struct tog_view_list_head *views)
829 const struct got_error *err = NULL;
835 /* Clear "no matches" indicator. */
836 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
837 view->search_next_done == TOG_SEARCH_HAVE_NONE)
838 view->search_next_done = TOG_SEARCH_HAVE_MORE;
840 if (view->searching && !view->search_next_done) {
841 errcode = pthread_mutex_unlock(&tog_mutex);
843 return got_error_set_errno(errcode,
844 "pthread_mutex_unlock");
846 errcode = pthread_mutex_lock(&tog_mutex);
848 return got_error_set_errno(errcode,
849 "pthread_mutex_lock");
850 view->search_next(view);
854 nodelay(stdscr, FALSE);
855 /* Allow threads to make progress while we are waiting for input. */
856 errcode = pthread_mutex_unlock(&tog_mutex);
858 return got_error_set_errno(errcode, "pthread_mutex_unlock");
859 ch = wgetch(view->window);
860 errcode = pthread_mutex_lock(&tog_mutex);
862 return got_error_set_errno(errcode, "pthread_mutex_lock");
863 nodelay(stdscr, TRUE);
865 if (tog_sigwinch_received || tog_sigcont_received) {
867 tog_sigwinch_received = 0;
868 tog_sigcont_received = 0;
869 TAILQ_FOREACH(v, views, entry) {
870 err = view_resize(v);
873 err = v->input(new, v, KEY_RESIZE);
877 err = view_resize(v->child);
880 err = v->child->input(new, v->child,
892 view->child->focussed = 1;
893 view->focus_child = 1;
894 } else if (view->parent) {
896 view->parent->focussed = 1;
897 view->parent->focus_child = 0;
901 err = view->input(new, view, ch);
908 if (view_is_parent_view(view)) {
909 if (view->child == NULL)
911 if (view_is_splitscreen(view->child)) {
913 view->child->focussed = 1;
914 err = view_fullscreen(view->child);
916 err = view_splitscreen(view->child);
919 err = view->child->input(new, view->child,
922 if (view_is_splitscreen(view)) {
923 view->parent->focussed = 0;
925 err = view_fullscreen(view);
927 err = view_splitscreen(view);
931 err = view->input(new, view, KEY_RESIZE);
937 if (view->search_start)
938 view_search_start(view);
940 err = view->input(new, view, ch);
944 if (view->search_started && view->search_next) {
945 view->searching = (ch == 'n' ?
946 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
947 view->search_next_done = 0;
948 view->search_next(view);
950 err = view->input(new, view, ch);
953 err = view->input(new, view, ch);
961 view_vborder(struct tog_view *view)
964 const struct tog_view *view_above;
967 return view_vborder(view->parent);
969 panel = panel_above(view->panel);
973 view_above = panel_userptr(panel);
974 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
975 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
979 view_needs_focus_indication(struct tog_view *view)
981 if (view_is_parent_view(view)) {
982 if (view->child == NULL || view->child->focussed)
984 if (!view_is_splitscreen(view->child))
986 } else if (!view_is_splitscreen(view))
989 return view->focussed;
992 static const struct got_error *
993 view_loop(struct tog_view *view)
995 const struct got_error *err = NULL;
996 struct tog_view_list_head views;
997 struct tog_view *new_view;
998 int fast_refresh = 10;
999 int done = 0, errcode;
1001 errcode = pthread_mutex_lock(&tog_mutex);
1003 return got_error_set_errno(errcode, "pthread_mutex_lock");
1006 TAILQ_INSERT_HEAD(&views, view, entry);
1009 err = view->show(view);
1014 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1015 /* Refresh fast during initialization, then become slower. */
1016 if (fast_refresh && fast_refresh-- == 0)
1017 halfdelay(10); /* switch to once per second */
1019 err = view_input(&new_view, &done, view, &views);
1023 struct tog_view *v, *prev = NULL;
1025 if (view_is_parent_view(view))
1026 prev = TAILQ_PREV(view, tog_view_list_head,
1028 else if (view->parent)
1029 prev = view->parent;
1032 view->parent->child = NULL;
1033 view->parent->focus_child = 0;
1035 TAILQ_REMOVE(&views, view, entry);
1037 err = view_close(view);
1042 TAILQ_FOREACH(v, &views, entry) {
1046 if (view == NULL && new_view == NULL) {
1047 /* No view has focus. Try to pick one. */
1050 else if (!TAILQ_EMPTY(&views)) {
1051 view = TAILQ_LAST(&views,
1052 tog_view_list_head);
1055 if (view->focus_child) {
1056 view->child->focussed = 1;
1064 struct tog_view *v, *t;
1065 /* Only allow one parent view per type. */
1066 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1067 if (v->type != new_view->type)
1069 TAILQ_REMOVE(&views, v, entry);
1070 err = view_close(v);
1075 TAILQ_INSERT_TAIL(&views, new_view, entry);
1079 if (view_is_parent_view(view)) {
1080 if (view->child && view->child->focussed)
1083 if (view->parent && view->parent->focussed)
1084 view = view->parent;
1086 show_panel(view->panel);
1087 if (view->child && view_is_splitscreen(view->child))
1088 show_panel(view->child->panel);
1089 if (view->parent && view_is_splitscreen(view)) {
1090 err = view->parent->show(view->parent);
1094 err = view->show(view);
1098 err = view->child->show(view->child);
1107 while (!TAILQ_EMPTY(&views)) {
1108 view = TAILQ_FIRST(&views);
1109 TAILQ_REMOVE(&views, view, entry);
1113 errcode = pthread_mutex_unlock(&tog_mutex);
1114 if (errcode && err == NULL)
1115 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1125 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1130 /* Create newly allocated wide-character string equivalent to a byte string. */
1131 static const struct got_error *
1132 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1135 const struct got_error *err = NULL;
1138 *wlen = mbstowcs(NULL, s, 0);
1139 if (*wlen == (size_t)-1) {
1141 if (errno != EILSEQ)
1142 return got_error_from_errno("mbstowcs");
1144 /* byte string invalid in current encoding; try to "fix" it */
1145 err = got_mbsavis(&vis, &vislen, s);
1148 *wlen = mbstowcs(NULL, vis, 0);
1149 if (*wlen == (size_t)-1) {
1150 err = got_error_from_errno("mbstowcs"); /* give up */
1155 *ws = calloc(*wlen + 1, sizeof(**ws));
1157 err = got_error_from_errno("calloc");
1161 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1162 err = got_error_from_errno("mbstowcs");
1173 /* Format a line for display, ensuring that it won't overflow a width limit. */
1174 static const struct got_error *
1175 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1178 const struct got_error *err = NULL;
1180 wchar_t *wline = NULL;
1187 err = mbs2ws(&wline, &wlen, line);
1191 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1192 wline[wlen - 1] = L'\0';
1195 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1196 wline[wlen - 1] = L'\0';
1202 int width = wcwidth(wline[i]);
1209 if (width == 1 || width == 2) {
1210 if (cols + width > wlimit)
1214 } else if (width == -1) {
1215 if (wline[i] == L'\t') {
1217 ((cols + col_tab_align) % TABSIZE);
1222 if (cols + width > wlimit)
1227 err = got_error_from_errno("wcwidth");
1242 static const struct got_error*
1243 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1244 struct got_object_id *id, struct got_repository *repo)
1246 static const struct got_error *err = NULL;
1247 struct got_reflist_entry *re;
1253 TAILQ_FOREACH(re, refs, entry) {
1254 struct got_tag_object *tag = NULL;
1255 struct got_object_id *ref_id;
1258 name = got_ref_get_name(re->ref);
1259 if (strcmp(name, GOT_REF_HEAD) == 0)
1261 if (strncmp(name, "refs/", 5) == 0)
1263 if (strncmp(name, "got/", 4) == 0)
1265 if (strncmp(name, "heads/", 6) == 0)
1267 if (strncmp(name, "remotes/", 8) == 0) {
1269 s = strstr(name, "/" GOT_REF_HEAD);
1270 if (s != NULL && s[strlen(s)] == '\0')
1273 err = got_ref_resolve(&ref_id, repo, re->ref);
1276 if (strncmp(name, "tags/", 5) == 0) {
1277 err = got_object_open_as_tag(&tag, repo, ref_id);
1279 if (err->code != GOT_ERR_OBJ_TYPE) {
1283 /* Ref points at something other than a tag. */
1288 cmp = got_object_id_cmp(tag ?
1289 got_object_tag_get_object_id(tag) : ref_id, id);
1292 got_object_tag_close(tag);
1296 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1297 s ? ", " : "", name) == -1) {
1298 err = got_error_from_errno("asprintf");
1309 static const struct got_error *
1310 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1315 smallerthan = strchr(author, '<');
1316 if (smallerthan && smallerthan[1] != '\0')
1317 author = smallerthan + 1;
1318 author[strcspn(author, "@>")] = '\0';
1319 return format_line(wauthor, author_width, author, limit, col_tab_align);
1322 static const struct got_error *
1323 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1324 struct got_object_id *id, const size_t date_display_cols,
1325 int author_display_cols)
1327 struct tog_log_view_state *s = &view->state.log;
1328 const struct got_error *err = NULL;
1329 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1330 char *logmsg0 = NULL, *logmsg = NULL;
1331 char *author = NULL;
1332 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1333 int author_width, logmsg_width;
1334 char *newline, *line = NULL;
1336 const int avail = view->ncols;
1338 time_t committer_time;
1339 struct tog_color *tc;
1341 committer_time = got_object_commit_get_committer_time(commit);
1342 if (gmtime_r(&committer_time, &tm) == NULL)
1343 return got_error_from_errno("gmtime_r");
1344 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1345 return got_error(GOT_ERR_NO_SPACE);
1347 if (avail <= date_display_cols)
1348 limit = MIN(sizeof(datebuf) - 1, avail);
1350 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1351 tc = get_color(&s->colors, TOG_COLOR_DATE);
1353 wattr_on(view->window,
1354 COLOR_PAIR(tc->colorpair), NULL);
1355 waddnstr(view->window, datebuf, limit);
1357 wattr_off(view->window,
1358 COLOR_PAIR(tc->colorpair), NULL);
1365 err = got_object_id_str(&id_str, id);
1368 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1370 wattr_on(view->window,
1371 COLOR_PAIR(tc->colorpair), NULL);
1372 wprintw(view->window, "%.8s ", id_str);
1374 wattr_off(view->window,
1375 COLOR_PAIR(tc->colorpair), NULL);
1382 author = strdup(got_object_commit_get_author(commit));
1383 if (author == NULL) {
1384 err = got_error_from_errno("strdup");
1387 err = format_author(&wauthor, &author_width, author, avail - col, col);
1390 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1392 wattr_on(view->window,
1393 COLOR_PAIR(tc->colorpair), NULL);
1394 waddwstr(view->window, wauthor);
1396 wattr_off(view->window,
1397 COLOR_PAIR(tc->colorpair), NULL);
1398 col += author_width;
1399 while (col < avail && author_width < author_display_cols + 2) {
1400 waddch(view->window, ' ');
1407 err = got_object_commit_get_logmsg(&logmsg0, commit);
1411 while (*logmsg == '\n')
1413 newline = strchr(logmsg, '\n');
1416 limit = avail - col;
1417 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1420 waddwstr(view->window, wlogmsg);
1421 col += logmsg_width;
1422 while (col < avail) {
1423 waddch(view->window, ' ');
1435 static struct commit_queue_entry *
1436 alloc_commit_queue_entry(struct got_commit_object *commit,
1437 struct got_object_id *id)
1439 struct commit_queue_entry *entry;
1441 entry = calloc(1, sizeof(*entry));
1446 entry->commit = commit;
1451 pop_commit(struct commit_queue *commits)
1453 struct commit_queue_entry *entry;
1455 entry = TAILQ_FIRST(&commits->head);
1456 TAILQ_REMOVE(&commits->head, entry, entry);
1457 got_object_commit_close(entry->commit);
1458 commits->ncommits--;
1459 /* Don't free entry->id! It is owned by the commit graph. */
1464 free_commits(struct commit_queue *commits)
1466 while (!TAILQ_EMPTY(&commits->head))
1467 pop_commit(commits);
1470 static const struct got_error *
1471 match_commit(int *have_match, struct got_object_id *id,
1472 struct got_commit_object *commit, regex_t *regex)
1474 const struct got_error *err = NULL;
1475 regmatch_t regmatch;
1476 char *id_str = NULL, *logmsg = NULL;
1480 err = got_object_id_str(&id_str, id);
1484 err = got_object_commit_get_logmsg(&logmsg, commit);
1488 if (regexec(regex, got_object_commit_get_author(commit), 1,
1489 ®match, 0) == 0 ||
1490 regexec(regex, got_object_commit_get_committer(commit), 1,
1491 ®match, 0) == 0 ||
1492 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1493 regexec(regex, logmsg, 1, ®match, 0) == 0)
1501 static const struct got_error *
1502 queue_commits(struct tog_log_thread_args *a)
1504 const struct got_error *err = NULL;
1507 * We keep all commits open throughout the lifetime of the log
1508 * view in order to avoid having to re-fetch commits from disk
1509 * while updating the display.
1512 struct got_object_id *id;
1513 struct got_commit_object *commit;
1514 struct commit_queue_entry *entry;
1517 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1519 if (err || id == NULL)
1522 err = got_object_open_as_commit(&commit, a->repo, id);
1525 entry = alloc_commit_queue_entry(commit, id);
1526 if (entry == NULL) {
1527 err = got_error_from_errno("alloc_commit_queue_entry");
1531 errcode = pthread_mutex_lock(&tog_mutex);
1533 err = got_error_set_errno(errcode,
1534 "pthread_mutex_lock");
1538 entry->idx = a->commits->ncommits;
1539 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1540 a->commits->ncommits++;
1542 if (*a->searching == TOG_SEARCH_FORWARD &&
1543 !*a->search_next_done) {
1545 err = match_commit(&have_match, id, commit, a->regex);
1549 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1552 errcode = pthread_mutex_unlock(&tog_mutex);
1553 if (errcode && err == NULL)
1554 err = got_error_set_errno(errcode,
1555 "pthread_mutex_unlock");
1558 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1564 select_commit(struct tog_log_view_state *s)
1566 struct commit_queue_entry *entry;
1569 entry = s->first_displayed_entry;
1571 if (ncommits == s->selected) {
1572 s->selected_entry = entry;
1575 entry = TAILQ_NEXT(entry, entry);
1580 static const struct got_error *
1581 draw_commits(struct tog_view *view)
1583 const struct got_error *err = NULL;
1584 struct tog_log_view_state *s = &view->state.log;
1585 struct commit_queue_entry *entry = s->selected_entry;
1586 const int limit = view->nlines;
1588 int ncommits, author_cols = 4;
1589 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1590 char *refs_str = NULL;
1592 struct tog_color *tc;
1593 static const size_t date_display_cols = 12;
1595 if (s->selected_entry &&
1596 !(view->searching && view->search_next_done == 0)) {
1597 struct got_reflist_head *refs;
1598 err = got_object_id_str(&id_str, s->selected_entry->id);
1601 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1602 s->selected_entry->id);
1604 err = build_refs_str(&refs_str, refs,
1605 s->selected_entry->id, s->repo);
1611 if (s->thread_args.commits_needed == 0)
1612 halfdelay(10); /* disable fast refresh */
1614 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1615 if (asprintf(&ncommits_str, " [%d/%d] %s",
1616 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1617 (view->searching && !view->search_next_done) ?
1618 "searching..." : "loading...") == -1) {
1619 err = got_error_from_errno("asprintf");
1623 const char *search_str = NULL;
1625 if (view->searching) {
1626 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1627 search_str = "no more matches";
1628 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1629 search_str = "no matches found";
1630 else if (!view->search_next_done)
1631 search_str = "searching...";
1634 if (asprintf(&ncommits_str, " [%d/%d] %s",
1635 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1636 search_str ? search_str :
1637 (refs_str ? refs_str : "")) == -1) {
1638 err = got_error_from_errno("asprintf");
1643 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1644 if (asprintf(&header, "commit %s %s%s",
1645 id_str ? id_str : "........................................",
1646 s->in_repo_path, ncommits_str) == -1) {
1647 err = got_error_from_errno("asprintf");
1651 } else if (asprintf(&header, "commit %s%s",
1652 id_str ? id_str : "........................................",
1653 ncommits_str) == -1) {
1654 err = got_error_from_errno("asprintf");
1658 err = format_line(&wline, &width, header, view->ncols, 0);
1662 werase(view->window);
1664 if (view_needs_focus_indication(view))
1665 wstandout(view->window);
1666 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1668 wattr_on(view->window,
1669 COLOR_PAIR(tc->colorpair), NULL);
1670 waddwstr(view->window, wline);
1672 wattr_off(view->window,
1673 COLOR_PAIR(tc->colorpair), NULL);
1674 while (width < view->ncols) {
1675 waddch(view->window, ' ');
1678 if (view_needs_focus_indication(view))
1679 wstandend(view->window);
1684 /* Grow author column size if necessary. */
1685 entry = s->first_displayed_entry;
1691 if (ncommits >= limit - 1)
1693 author = strdup(got_object_commit_get_author(entry->commit));
1694 if (author == NULL) {
1695 err = got_error_from_errno("strdup");
1698 err = format_author(&wauthor, &width, author, COLS,
1700 if (author_cols < width)
1701 author_cols = width;
1705 entry = TAILQ_NEXT(entry, entry);
1708 entry = s->first_displayed_entry;
1709 s->last_displayed_entry = s->first_displayed_entry;
1712 if (ncommits >= limit - 1)
1714 if (ncommits == s->selected)
1715 wstandout(view->window);
1716 err = draw_commit(view, entry->commit, entry->id,
1717 date_display_cols, author_cols);
1718 if (ncommits == s->selected)
1719 wstandend(view->window);
1723 s->last_displayed_entry = entry;
1724 entry = TAILQ_NEXT(entry, entry);
1737 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1739 struct commit_queue_entry *entry;
1742 entry = TAILQ_FIRST(&s->commits.head);
1743 if (s->first_displayed_entry == entry)
1746 entry = s->first_displayed_entry;
1747 while (entry && nscrolled < maxscroll) {
1748 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1750 s->first_displayed_entry = entry;
1756 static const struct got_error *
1757 trigger_log_thread(struct tog_view *view, int wait)
1759 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1762 halfdelay(1); /* fast refresh while loading commits */
1764 while (ta->commits_needed > 0 || ta->load_all) {
1765 if (ta->log_complete)
1768 /* Wake the log thread. */
1769 errcode = pthread_cond_signal(&ta->need_commits);
1771 return got_error_set_errno(errcode,
1772 "pthread_cond_signal");
1775 * The mutex will be released while the view loop waits
1776 * in wgetch(), at which time the log thread will run.
1781 /* Display progress update in log view. */
1782 show_log_view(view);
1786 /* Wait right here while next commit is being loaded. */
1787 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1789 return got_error_set_errno(errcode,
1790 "pthread_cond_wait");
1792 /* Display progress update in log view. */
1793 show_log_view(view);
1801 static const struct got_error *
1802 log_scroll_down(struct tog_view *view, int maxscroll)
1804 struct tog_log_view_state *s = &view->state.log;
1805 const struct got_error *err = NULL;
1806 struct commit_queue_entry *pentry;
1807 int nscrolled = 0, ncommits_needed;
1809 if (s->last_displayed_entry == NULL)
1812 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1813 if (s->commits.ncommits < ncommits_needed &&
1814 !s->thread_args.log_complete) {
1816 * Ask the log thread for required amount of commits.
1818 s->thread_args.commits_needed += maxscroll;
1819 err = trigger_log_thread(view, 1);
1825 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1829 s->last_displayed_entry = pentry;
1831 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1834 s->first_displayed_entry = pentry;
1835 } while (++nscrolled < maxscroll);
1840 static const struct got_error *
1841 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1842 struct got_commit_object *commit, struct got_object_id *commit_id,
1843 struct tog_view *log_view, struct got_repository *repo)
1845 const struct got_error *err;
1846 struct got_object_qid *parent_id;
1847 struct tog_view *diff_view;
1849 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1850 if (diff_view == NULL)
1851 return got_error_from_errno("view_open");
1853 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1854 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1855 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1857 *new_view = diff_view;
1861 static const struct got_error *
1862 tree_view_visit_subtree(struct tog_tree_view_state *s,
1863 struct got_tree_object *subtree)
1865 struct tog_parent_tree *parent;
1867 parent = calloc(1, sizeof(*parent));
1869 return got_error_from_errno("calloc");
1871 parent->tree = s->tree;
1872 parent->first_displayed_entry = s->first_displayed_entry;
1873 parent->selected_entry = s->selected_entry;
1874 parent->selected = s->selected;
1875 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1878 s->first_displayed_entry = NULL;
1882 static const struct got_error *
1883 tree_view_walk_path(struct tog_tree_view_state *s,
1884 struct got_object_id *commit_id, const char *path)
1886 const struct got_error *err = NULL;
1887 struct got_tree_object *tree = NULL;
1889 char *slash, *subpath = NULL;
1891 /* Walk the path and open corresponding tree objects. */
1894 struct got_tree_entry *te;
1895 struct got_object_id *tree_id;
1901 /* Ensure the correct subtree entry is selected. */
1902 slash = strchr(p, '/');
1904 te_name = strdup(p);
1906 te_name = strndup(p, slash - p);
1907 if (te_name == NULL) {
1908 err = got_error_from_errno("strndup");
1911 te = got_object_tree_find_entry(s->tree, te_name);
1913 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1918 s->first_displayed_entry = s->selected_entry = te;
1920 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1921 break; /* jump to this file's entry */
1923 slash = strchr(p, '/');
1925 subpath = strndup(path, slash - path);
1927 subpath = strdup(path);
1928 if (subpath == NULL) {
1929 err = got_error_from_errno("strdup");
1933 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1938 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1943 err = tree_view_visit_subtree(s, tree);
1945 got_object_tree_close(tree);
1959 static const struct got_error *
1960 browse_commit_tree(struct tog_view **new_view, int begin_x,
1961 struct commit_queue_entry *entry, const char *path,
1962 const char *head_ref_name, struct got_repository *repo)
1964 const struct got_error *err = NULL;
1965 struct tog_tree_view_state *s;
1966 struct tog_view *tree_view;
1968 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1969 if (tree_view == NULL)
1970 return got_error_from_errno("view_open");
1972 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
1975 s = &tree_view->state.tree;
1977 *new_view = tree_view;
1979 if (got_path_is_root_dir(path))
1982 return tree_view_walk_path(s, entry->id, path);
1985 static const struct got_error *
1986 block_signals_used_by_main_thread(void)
1991 if (sigemptyset(&sigset) == -1)
1992 return got_error_from_errno("sigemptyset");
1994 /* tog handles SIGWINCH and SIGCONT */
1995 if (sigaddset(&sigset, SIGWINCH) == -1)
1996 return got_error_from_errno("sigaddset");
1997 if (sigaddset(&sigset, SIGCONT) == -1)
1998 return got_error_from_errno("sigaddset");
2000 /* ncurses handles SIGTSTP */
2001 if (sigaddset(&sigset, SIGTSTP) == -1)
2002 return got_error_from_errno("sigaddset");
2004 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2006 return got_error_set_errno(errcode, "pthread_sigmask");
2012 log_thread(void *arg)
2014 const struct got_error *err = NULL;
2016 struct tog_log_thread_args *a = arg;
2019 err = block_signals_used_by_main_thread();
2023 while (!done && !err && !tog_sigpipe_received) {
2024 err = queue_commits(a);
2026 if (err->code != GOT_ERR_ITER_COMPLETED)
2030 } else if (a->commits_needed > 0 && !a->load_all)
2031 a->commits_needed--;
2033 errcode = pthread_mutex_lock(&tog_mutex);
2035 err = got_error_set_errno(errcode,
2036 "pthread_mutex_lock");
2038 } else if (*a->quit)
2040 else if (*a->first_displayed_entry == NULL) {
2041 *a->first_displayed_entry =
2042 TAILQ_FIRST(&a->commits->head);
2043 *a->selected_entry = *a->first_displayed_entry;
2046 errcode = pthread_cond_signal(&a->commit_loaded);
2048 err = got_error_set_errno(errcode,
2049 "pthread_cond_signal");
2050 pthread_mutex_unlock(&tog_mutex);
2055 a->commits_needed = 0;
2057 if (a->commits_needed == 0 && !a->load_all) {
2058 errcode = pthread_cond_wait(&a->need_commits,
2061 err = got_error_set_errno(errcode,
2062 "pthread_cond_wait");
2068 errcode = pthread_mutex_unlock(&tog_mutex);
2069 if (errcode && err == NULL)
2070 err = got_error_set_errno(errcode,
2071 "pthread_mutex_unlock");
2073 a->log_complete = 1;
2077 static const struct got_error *
2078 stop_log_thread(struct tog_log_view_state *s)
2080 const struct got_error *err = NULL;
2085 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2087 return got_error_set_errno(errcode,
2088 "pthread_cond_signal");
2089 errcode = pthread_mutex_unlock(&tog_mutex);
2091 return got_error_set_errno(errcode,
2092 "pthread_mutex_unlock");
2093 errcode = pthread_join(s->thread, (void **)&err);
2095 return got_error_set_errno(errcode, "pthread_join");
2096 errcode = pthread_mutex_lock(&tog_mutex);
2098 return got_error_set_errno(errcode,
2099 "pthread_mutex_lock");
2103 if (s->thread_args.repo) {
2104 err = got_repo_close(s->thread_args.repo);
2105 s->thread_args.repo = NULL;
2108 if (s->thread_args.graph) {
2109 got_commit_graph_close(s->thread_args.graph);
2110 s->thread_args.graph = NULL;
2116 static const struct got_error *
2117 close_log_view(struct tog_view *view)
2119 const struct got_error *err = NULL;
2120 struct tog_log_view_state *s = &view->state.log;
2123 err = stop_log_thread(s);
2125 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2126 if (errcode && err == NULL)
2127 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2129 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2130 if (errcode && err == NULL)
2131 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2133 free_commits(&s->commits);
2134 free(s->in_repo_path);
2135 s->in_repo_path = NULL;
2138 free(s->head_ref_name);
2139 s->head_ref_name = NULL;
2143 static const struct got_error *
2144 search_start_log_view(struct tog_view *view)
2146 struct tog_log_view_state *s = &view->state.log;
2148 s->matched_entry = NULL;
2149 s->search_entry = NULL;
2153 static const struct got_error *
2154 search_next_log_view(struct tog_view *view)
2156 const struct got_error *err = NULL;
2157 struct tog_log_view_state *s = &view->state.log;
2158 struct commit_queue_entry *entry;
2160 /* Display progress update in log view. */
2161 show_log_view(view);
2165 if (s->search_entry) {
2167 errcode = pthread_mutex_unlock(&tog_mutex);
2169 return got_error_set_errno(errcode,
2170 "pthread_mutex_unlock");
2171 ch = wgetch(view->window);
2172 errcode = pthread_mutex_lock(&tog_mutex);
2174 return got_error_set_errno(errcode,
2175 "pthread_mutex_lock");
2176 if (ch == KEY_BACKSPACE) {
2177 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2180 if (view->searching == TOG_SEARCH_FORWARD)
2181 entry = TAILQ_NEXT(s->search_entry, entry);
2183 entry = TAILQ_PREV(s->search_entry,
2184 commit_queue_head, entry);
2185 } else if (s->matched_entry) {
2186 if (view->searching == TOG_SEARCH_FORWARD)
2187 entry = TAILQ_NEXT(s->matched_entry, entry);
2189 entry = TAILQ_PREV(s->matched_entry,
2190 commit_queue_head, entry);
2192 if (view->searching == TOG_SEARCH_FORWARD)
2193 entry = TAILQ_FIRST(&s->commits.head);
2195 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2201 if (entry == NULL) {
2202 if (s->thread_args.log_complete ||
2203 view->searching == TOG_SEARCH_BACKWARD) {
2204 view->search_next_done =
2205 (s->matched_entry == NULL ?
2206 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2207 s->search_entry = NULL;
2211 * Poke the log thread for more commits and return,
2212 * allowing the main loop to make progress. Search
2213 * will resume at s->search_entry once we come back.
2215 s->thread_args.commits_needed++;
2216 return trigger_log_thread(view, 0);
2219 err = match_commit(&have_match, entry->id, entry->commit,
2224 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2225 s->matched_entry = entry;
2229 s->search_entry = entry;
2230 if (view->searching == TOG_SEARCH_FORWARD)
2231 entry = TAILQ_NEXT(entry, entry);
2233 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2236 if (s->matched_entry) {
2237 int cur = s->selected_entry->idx;
2238 while (cur < s->matched_entry->idx) {
2239 err = input_log_view(NULL, view, KEY_DOWN);
2244 while (cur > s->matched_entry->idx) {
2245 err = input_log_view(NULL, view, KEY_UP);
2252 s->search_entry = NULL;
2257 static const struct got_error *
2258 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2259 struct got_repository *repo, const char *head_ref_name,
2260 const char *in_repo_path, int log_branches)
2262 const struct got_error *err = NULL;
2263 struct tog_log_view_state *s = &view->state.log;
2264 struct got_repository *thread_repo = NULL;
2265 struct got_commit_graph *thread_graph = NULL;
2268 if (in_repo_path != s->in_repo_path) {
2269 free(s->in_repo_path);
2270 s->in_repo_path = strdup(in_repo_path);
2271 if (s->in_repo_path == NULL)
2272 return got_error_from_errno("strdup");
2275 /* The commit queue only contains commits being displayed. */
2276 TAILQ_INIT(&s->commits.head);
2277 s->commits.ncommits = 0;
2280 if (head_ref_name) {
2281 s->head_ref_name = strdup(head_ref_name);
2282 if (s->head_ref_name == NULL) {
2283 err = got_error_from_errno("strdup");
2287 s->start_id = got_object_id_dup(start_id);
2288 if (s->start_id == NULL) {
2289 err = got_error_from_errno("got_object_id_dup");
2292 s->log_branches = log_branches;
2294 STAILQ_INIT(&s->colors);
2295 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2296 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2297 get_color_value("TOG_COLOR_COMMIT"));
2300 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2301 get_color_value("TOG_COLOR_AUTHOR"));
2303 free_colors(&s->colors);
2306 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2307 get_color_value("TOG_COLOR_DATE"));
2309 free_colors(&s->colors);
2314 view->show = show_log_view;
2315 view->input = input_log_view;
2316 view->close = close_log_view;
2317 view->search_start = search_start_log_view;
2318 view->search_next = search_next_log_view;
2320 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2323 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2327 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2328 s->repo, NULL, NULL);
2332 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2334 err = got_error_set_errno(errcode, "pthread_cond_init");
2337 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2339 err = got_error_set_errno(errcode, "pthread_cond_init");
2343 s->thread_args.commits_needed = view->nlines;
2344 s->thread_args.graph = thread_graph;
2345 s->thread_args.commits = &s->commits;
2346 s->thread_args.in_repo_path = s->in_repo_path;
2347 s->thread_args.start_id = s->start_id;
2348 s->thread_args.repo = thread_repo;
2349 s->thread_args.log_complete = 0;
2350 s->thread_args.quit = &s->quit;
2351 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2352 s->thread_args.selected_entry = &s->selected_entry;
2353 s->thread_args.searching = &view->searching;
2354 s->thread_args.search_next_done = &view->search_next_done;
2355 s->thread_args.regex = &view->regex;
2358 close_log_view(view);
2362 static const struct got_error *
2363 show_log_view(struct tog_view *view)
2365 const struct got_error *err;
2366 struct tog_log_view_state *s = &view->state.log;
2368 if (s->thread == NULL) {
2369 int errcode = pthread_create(&s->thread, NULL, log_thread,
2372 return got_error_set_errno(errcode, "pthread_create");
2373 if (s->thread_args.commits_needed > 0) {
2374 err = trigger_log_thread(view, 1);
2380 return draw_commits(view);
2383 static const struct got_error *
2384 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2386 const struct got_error *err = NULL;
2387 struct tog_log_view_state *s = &view->state.log;
2388 struct tog_view *diff_view = NULL, *tree_view = NULL;
2389 struct tog_view *ref_view = NULL;
2392 if (s->thread_args.load_all) {
2393 if (ch == KEY_BACKSPACE)
2394 s->thread_args.load_all = 0;
2395 else if (s->thread_args.log_complete) {
2396 s->thread_args.load_all = 0;
2397 log_scroll_down(view, s->commits.ncommits);
2398 s->selected = MIN(view->nlines - 2,
2399 s->commits.ncommits - 1);
2413 if (s->first_displayed_entry == NULL)
2415 if (s->selected > 0)
2418 log_scroll_up(s, 1);
2424 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2429 if (s->first_displayed_entry == NULL)
2431 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2434 log_scroll_up(s, view->nlines - 1);
2441 if (s->first_displayed_entry == NULL)
2443 if (s->selected < MIN(view->nlines - 2,
2444 s->commits.ncommits - 1))
2447 err = log_scroll_down(view, 1);
2455 /* We don't know yet how many commits, so we're forced to
2456 * traverse them all. */
2457 if (!s->thread_args.log_complete) {
2458 s->thread_args.load_all = 1;
2459 return trigger_log_thread(view, 0);
2462 log_scroll_down(view, s->commits.ncommits);
2463 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
2469 struct commit_queue_entry *first;
2470 first = s->first_displayed_entry;
2473 err = log_scroll_down(view, view->nlines - 1);
2476 if (first == s->first_displayed_entry &&
2477 s->selected < MIN(view->nlines - 2,
2478 s->commits.ncommits - 1)) {
2479 /* can't scroll further down */
2480 s->selected = MIN(view->nlines - 2,
2481 s->commits.ncommits - 1);
2487 if (s->selected > view->nlines - 2)
2488 s->selected = view->nlines - 2;
2489 if (s->selected > s->commits.ncommits - 1)
2490 s->selected = s->commits.ncommits - 1;
2492 if (s->commits.ncommits < view->nlines - 1 &&
2493 !s->thread_args.log_complete) {
2494 s->thread_args.commits_needed += (view->nlines - 1) -
2495 s->commits.ncommits;
2496 err = trigger_log_thread(view, 1);
2502 if (s->selected_entry == NULL)
2504 if (view_is_parent_view(view))
2505 begin_x = view_split_begin_x(view->begin_x);
2506 err = open_diff_view_for_commit(&diff_view, begin_x,
2507 s->selected_entry->commit, s->selected_entry->id,
2512 diff_view->focussed = 1;
2513 if (view_is_parent_view(view)) {
2514 err = view_close_child(view);
2517 view_set_child(view, diff_view);
2518 view->focus_child = 1;
2520 *new_view = diff_view;
2523 if (s->selected_entry == NULL)
2525 if (view_is_parent_view(view))
2526 begin_x = view_split_begin_x(view->begin_x);
2527 err = browse_commit_tree(&tree_view, begin_x,
2528 s->selected_entry, s->in_repo_path, s->head_ref_name,
2533 tree_view->focussed = 1;
2534 if (view_is_parent_view(view)) {
2535 err = view_close_child(view);
2538 view_set_child(view, tree_view);
2539 view->focus_child = 1;
2541 *new_view = tree_view;
2546 if (ch == KEY_BACKSPACE &&
2547 got_path_is_root_dir(s->in_repo_path))
2549 err = stop_log_thread(s);
2552 if (ch == KEY_BACKSPACE) {
2554 err = got_path_dirname(&parent_path, s->in_repo_path);
2557 free(s->in_repo_path);
2558 s->in_repo_path = parent_path;
2559 s->thread_args.in_repo_path = s->in_repo_path;
2560 } else if (ch == CTRL('l')) {
2561 struct got_object_id *start_id;
2562 err = got_repo_match_object_id(&start_id, NULL,
2563 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2564 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2568 s->start_id = start_id;
2569 s->thread_args.start_id = s->start_id;
2571 s->log_branches = !s->log_branches;
2573 err = got_repo_open(&s->thread_args.repo,
2574 got_repo_get_path(s->repo), NULL);
2578 err = tog_load_refs(s->repo);
2581 err = got_commit_graph_open(&s->thread_args.graph,
2582 s->in_repo_path, !s->log_branches);
2585 err = got_commit_graph_iter_start(s->thread_args.graph,
2586 s->start_id, s->repo, NULL, NULL);
2589 free_commits(&s->commits);
2590 s->first_displayed_entry = NULL;
2591 s->last_displayed_entry = NULL;
2592 s->selected_entry = NULL;
2594 s->thread_args.log_complete = 0;
2596 s->thread_args.commits_needed = view->nlines;
2599 if (view_is_parent_view(view))
2600 begin_x = view_split_begin_x(view->begin_x);
2601 ref_view = view_open(view->nlines, view->ncols,
2602 view->begin_y, begin_x, TOG_VIEW_REF);
2603 if (ref_view == NULL)
2604 return got_error_from_errno("view_open");
2605 err = open_ref_view(ref_view, s->repo);
2607 view_close(ref_view);
2611 ref_view->focussed = 1;
2612 if (view_is_parent_view(view)) {
2613 err = view_close_child(view);
2616 view_set_child(view, ref_view);
2617 view->focus_child = 1;
2619 *new_view = ref_view;
2628 static const struct got_error *
2629 apply_unveil(const char *repo_path, const char *worktree_path)
2631 const struct got_error *error;
2634 if (unveil("gmon.out", "rwc") != 0)
2635 return got_error_from_errno2("unveil", "gmon.out");
2637 if (repo_path && unveil(repo_path, "r") != 0)
2638 return got_error_from_errno2("unveil", repo_path);
2640 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2641 return got_error_from_errno2("unveil", worktree_path);
2643 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2644 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2646 error = got_privsep_unveil_exec_helpers();
2650 if (unveil(NULL, NULL) != 0)
2651 return got_error_from_errno("unveil");
2661 halfdelay(1); /* Do fast refresh while initial view is loading. */
2664 intrflush(stdscr, FALSE);
2665 keypad(stdscr, TRUE);
2667 if (getenv("TOG_COLORS") != NULL) {
2669 use_default_colors();
2671 signal(SIGWINCH, tog_sigwinch);
2672 signal(SIGPIPE, tog_sigpipe);
2673 signal(SIGCONT, tog_sigcont);
2676 static const struct got_error *
2677 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2678 struct got_repository *repo, struct got_worktree *worktree)
2680 const struct got_error *err = NULL;
2683 *in_repo_path = strdup("/");
2684 if (*in_repo_path == NULL)
2685 return got_error_from_errno("strdup");
2690 const char *prefix = got_worktree_get_path_prefix(worktree);
2693 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2696 if (asprintf(in_repo_path, "%s%s%s", prefix,
2697 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2699 err = got_error_from_errno("asprintf");
2700 *in_repo_path = NULL;
2704 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2709 static const struct got_error *
2710 cmd_log(int argc, char *argv[])
2712 const struct got_error *error;
2713 struct got_repository *repo = NULL;
2714 struct got_worktree *worktree = NULL;
2715 struct got_object_id *start_id = NULL;
2716 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2717 char *start_commit = NULL, *label = NULL;
2718 struct got_reference *ref = NULL;
2719 const char *head_ref_name = NULL;
2720 int ch, log_branches = 0;
2721 struct tog_view *view;
2723 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2729 start_commit = optarg;
2732 repo_path = realpath(optarg, NULL);
2733 if (repo_path == NULL)
2734 return got_error_from_errno2("realpath",
2749 if (repo_path == NULL) {
2750 cwd = getcwd(NULL, 0);
2752 return got_error_from_errno("getcwd");
2753 error = got_worktree_open(&worktree, cwd);
2754 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2758 strdup(got_worktree_get_repo_path(worktree));
2760 repo_path = strdup(cwd);
2761 if (repo_path == NULL) {
2762 error = got_error_from_errno("strdup");
2767 error = got_repo_open(&repo, repo_path, NULL);
2771 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2778 error = apply_unveil(got_repo_get_path(repo),
2779 worktree ? got_worktree_get_root_path(worktree) : NULL);
2783 /* already loaded by tog_log_with_path()? */
2784 if (TAILQ_EMPTY(&tog_refs)) {
2785 error = tog_load_refs(repo);
2790 if (start_commit == NULL) {
2791 error = got_repo_match_object_id(&start_id, &label,
2792 worktree ? got_worktree_get_head_ref_name(worktree) :
2793 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2796 head_ref_name = label;
2798 error = got_ref_open(&ref, repo, start_commit, 0);
2800 head_ref_name = got_ref_get_name(ref);
2801 else if (error->code != GOT_ERR_NOT_REF)
2803 error = got_repo_match_object_id(&start_id, NULL,
2804 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2809 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2811 error = got_error_from_errno("view_open");
2814 error = open_log_view(view, start_id, repo, head_ref_name,
2815 in_repo_path, log_branches);
2819 /* Release work tree lock. */
2820 got_worktree_close(worktree);
2823 error = view_loop(view);
2833 const struct got_error *close_err = got_repo_close(repo);
2838 got_worktree_close(worktree);
2847 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2848 "[-w] object1 object2\n", getprogname());
2853 match_line(const char *line, regex_t *regex, size_t nmatch,
2854 regmatch_t *regmatch)
2856 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2860 match_color(struct tog_colors *colors, const char *line)
2862 struct tog_color *tc = NULL;
2864 STAILQ_FOREACH(tc, colors, entry) {
2865 if (match_line(line, &tc->regex, 0, NULL))
2872 static const struct got_error *
2873 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2874 WINDOW *window, regmatch_t *regmatch)
2876 const struct got_error *err = NULL;
2883 s = strndup(line, regmatch->rm_so);
2885 return got_error_from_errno("strndup");
2887 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2892 waddwstr(window, wline);
2899 s = strndup(line + regmatch->rm_so,
2900 regmatch->rm_eo - regmatch->rm_so);
2902 err = got_error_from_errno("strndup");
2906 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2911 wattr_on(window, A_STANDOUT, NULL);
2912 waddwstr(window, wline);
2913 wattr_off(window, A_STANDOUT, NULL);
2920 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2921 err = format_line(&wline, &width,
2922 line + regmatch->rm_eo, wlimit, col_tab_align);
2925 waddwstr(window, wline);
2933 static const struct got_error *
2934 draw_file(struct tog_view *view, const char *header)
2936 struct tog_diff_view_state *s = &view->state.diff;
2937 regmatch_t *regmatch = &view->regmatch;
2938 const struct got_error *err;
2941 size_t linesize = 0;
2943 struct tog_color *tc;
2946 int max_lines = view->nlines;
2947 int nlines = s->nlines;
2950 line_offset = s->line_offsets[s->first_displayed_line - 1];
2951 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2952 return got_error_from_errno("fseek");
2954 werase(view->window);
2957 if (asprintf(&line, "[%d/%d] %s",
2958 s->first_displayed_line - 1 + s->selected_line, nlines,
2960 return got_error_from_errno("asprintf");
2961 err = format_line(&wline, &width, line, view->ncols, 0);
2966 if (view_needs_focus_indication(view))
2967 wstandout(view->window);
2968 waddwstr(view->window, wline);
2971 if (view_needs_focus_indication(view))
2972 wstandend(view->window);
2973 if (width <= view->ncols - 1)
2974 waddch(view->window, '\n');
2983 while (max_lines > 0 && nprinted < max_lines) {
2984 linelen = getline(&line, &linesize, s->f);
2985 if (linelen == -1) {
2991 return got_ferror(s->f, GOT_ERR_IO);
2994 tc = match_color(&s->colors, line);
2996 wattr_on(view->window,
2997 COLOR_PAIR(tc->colorpair), NULL);
2998 if (s->first_displayed_line + nprinted == s->matched_line &&
2999 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3000 err = add_matched_line(&width, line, view->ncols, 0,
3001 view->window, regmatch);
3007 err = format_line(&wline, &width, line, view->ncols, 0);
3012 waddwstr(view->window, wline);
3017 wattr_off(view->window,
3018 COLOR_PAIR(tc->colorpair), NULL);
3019 if (width <= view->ncols - 1)
3020 waddch(view->window, '\n');
3025 s->last_displayed_line = s->first_displayed_line +
3028 s->last_displayed_line = s->first_displayed_line;
3033 while (nprinted < view->nlines) {
3034 waddch(view->window, '\n');
3038 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3043 wstandout(view->window);
3044 waddwstr(view->window, wline);
3047 wstandend(view->window);
3054 get_datestr(time_t *time, char *datebuf)
3056 struct tm mytm, *tm;
3059 tm = gmtime_r(time, &mytm);
3062 s = asctime_r(tm, datebuf);
3065 p = strchr(s, '\n');
3071 static const struct got_error *
3072 get_changed_paths(struct got_pathlist_head *paths,
3073 struct got_commit_object *commit, struct got_repository *repo)
3075 const struct got_error *err = NULL;
3076 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3077 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3078 struct got_object_qid *qid;
3080 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3082 struct got_commit_object *pcommit;
3083 err = got_object_open_as_commit(&pcommit, repo,
3088 tree_id1 = got_object_id_dup(
3089 got_object_commit_get_tree_id(pcommit));
3090 if (tree_id1 == NULL) {
3091 got_object_commit_close(pcommit);
3092 return got_error_from_errno("got_object_id_dup");
3094 got_object_commit_close(pcommit);
3099 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3104 tree_id2 = got_object_commit_get_tree_id(commit);
3105 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3109 err = got_diff_tree(tree1, tree2, "", "", repo,
3110 got_diff_tree_collect_changed_paths, paths, 0);
3113 got_object_tree_close(tree1);
3115 got_object_tree_close(tree2);
3120 static const struct got_error *
3121 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3125 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3127 return got_error_from_errno("reallocarray");
3129 (*line_offsets)[*nlines] = off;
3134 static const struct got_error *
3135 write_commit_info(off_t **line_offsets, size_t *nlines,
3136 struct got_object_id *commit_id, struct got_reflist_head *refs,
3137 struct got_repository *repo, FILE *outfile)
3139 const struct got_error *err = NULL;
3140 char datebuf[26], *datestr;
3141 struct got_commit_object *commit;
3142 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3143 time_t committer_time;
3144 const char *author, *committer;
3145 char *refs_str = NULL;
3146 struct got_pathlist_head changed_paths;
3147 struct got_pathlist_entry *pe;
3151 TAILQ_INIT(&changed_paths);
3154 err = build_refs_str(&refs_str, refs, commit_id, repo);
3159 err = got_object_open_as_commit(&commit, repo, commit_id);
3163 err = got_object_id_str(&id_str, commit_id);
3165 err = got_error_from_errno("got_object_id_str");
3169 err = add_line_offset(line_offsets, nlines, 0);
3173 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3174 refs_str ? refs_str : "", refs_str ? ")" : "");
3176 err = got_error_from_errno("fprintf");
3180 err = add_line_offset(line_offsets, nlines, outoff);
3184 n = fprintf(outfile, "from: %s\n",
3185 got_object_commit_get_author(commit));
3187 err = got_error_from_errno("fprintf");
3191 err = add_line_offset(line_offsets, nlines, outoff);
3195 committer_time = got_object_commit_get_committer_time(commit);
3196 datestr = get_datestr(&committer_time, datebuf);
3198 n = fprintf(outfile, "date: %s UTC\n", datestr);
3200 err = got_error_from_errno("fprintf");
3204 err = add_line_offset(line_offsets, nlines, outoff);
3208 author = got_object_commit_get_author(commit);
3209 committer = got_object_commit_get_committer(commit);
3210 if (strcmp(author, committer) != 0) {
3211 n = fprintf(outfile, "via: %s\n", committer);
3213 err = got_error_from_errno("fprintf");
3217 err = add_line_offset(line_offsets, nlines, outoff);
3221 err = got_object_commit_get_logmsg(&logmsg, commit);
3225 while ((line = strsep(&s, "\n")) != NULL) {
3226 n = fprintf(outfile, "%s\n", line);
3228 err = got_error_from_errno("fprintf");
3232 err = add_line_offset(line_offsets, nlines, outoff);
3237 err = get_changed_paths(&changed_paths, commit, repo);
3240 TAILQ_FOREACH(pe, &changed_paths, entry) {
3241 struct got_diff_changed_path *cp = pe->data;
3242 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3244 err = got_error_from_errno("fprintf");
3248 err = add_line_offset(line_offsets, nlines, outoff);
3251 free((char *)pe->path);
3255 fputc('\n', outfile);
3257 err = add_line_offset(line_offsets, nlines, outoff);
3259 got_pathlist_free(&changed_paths);
3263 got_object_commit_close(commit);
3265 free(*line_offsets);
3266 *line_offsets = NULL;
3272 static const struct got_error *
3273 create_diff(struct tog_diff_view_state *s)
3275 const struct got_error *err = NULL;
3279 free(s->line_offsets);
3280 s->line_offsets = malloc(sizeof(off_t));
3281 if (s->line_offsets == NULL)
3282 return got_error_from_errno("malloc");
3287 err = got_error_from_errno("got_opentemp");
3290 if (s->f && fclose(s->f) == EOF) {
3291 err = got_error_from_errno("fclose");
3297 err = got_object_get_type(&obj_type, s->repo, s->id1);
3299 err = got_object_get_type(&obj_type, s->repo, s->id2);
3304 case GOT_OBJ_TYPE_BLOB:
3305 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3306 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3307 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3309 case GOT_OBJ_TYPE_TREE:
3310 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3311 s->id1, s->id2, "", "", s->diff_context,
3312 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3314 case GOT_OBJ_TYPE_COMMIT: {
3315 const struct got_object_id_queue *parent_ids;
3316 struct got_object_qid *pid;
3317 struct got_commit_object *commit2;
3318 struct got_reflist_head *refs;
3320 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3323 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3324 /* Show commit info if we're diffing to a parent/root commit. */
3325 if (s->id1 == NULL) {
3326 err = write_commit_info(&s->line_offsets, &s->nlines,
3327 s->id2, refs, s->repo, s->f);
3331 parent_ids = got_object_commit_get_parent_ids(commit2);
3332 STAILQ_FOREACH(pid, parent_ids, entry) {
3333 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3334 err = write_commit_info(
3335 &s->line_offsets, &s->nlines,
3336 s->id2, refs, s->repo, s->f);
3343 got_object_commit_close(commit2);
3345 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3346 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3347 s->force_text_diff, s->repo, s->f);
3351 err = got_error(GOT_ERR_OBJ_TYPE);
3357 if (s->f && fflush(s->f) != 0 && err == NULL)
3358 err = got_error_from_errno("fflush");
3363 diff_view_indicate_progress(struct tog_view *view)
3365 mvwaddstr(view->window, 0, 0, "diffing...");
3370 static const struct got_error *
3371 search_start_diff_view(struct tog_view *view)
3373 struct tog_diff_view_state *s = &view->state.diff;
3375 s->matched_line = 0;
3379 static const struct got_error *
3380 search_next_diff_view(struct tog_view *view)
3382 struct tog_diff_view_state *s = &view->state.diff;
3385 size_t linesize = 0;
3388 if (!view->searching) {
3389 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3393 if (s->matched_line) {
3394 if (view->searching == TOG_SEARCH_FORWARD)
3395 lineno = s->matched_line + 1;
3397 lineno = s->matched_line - 1;
3399 if (view->searching == TOG_SEARCH_FORWARD)
3408 if (lineno <= 0 || lineno > s->nlines) {
3409 if (s->matched_line == 0) {
3410 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3414 if (view->searching == TOG_SEARCH_FORWARD)
3420 offset = s->line_offsets[lineno - 1];
3421 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3423 return got_error_from_errno("fseeko");
3425 linelen = getline(&line, &linesize, s->f);
3426 if (linelen != -1 &&
3427 match_line(line, &view->regex, 1, &view->regmatch)) {
3428 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3429 s->matched_line = lineno;
3432 if (view->searching == TOG_SEARCH_FORWARD)
3439 if (s->matched_line) {
3440 s->first_displayed_line = s->matched_line;
3441 s->selected_line = 1;
3447 static const struct got_error *
3448 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3449 struct got_object_id *id2, const char *label1, const char *label2,
3450 int diff_context, int ignore_whitespace, int force_text_diff,
3451 struct tog_view *log_view, struct got_repository *repo)
3453 const struct got_error *err;
3454 struct tog_diff_view_state *s = &view->state.diff;
3456 if (id1 != NULL && id2 != NULL) {
3458 err = got_object_get_type(&type1, repo, id1);
3461 err = got_object_get_type(&type2, repo, id2);
3466 return got_error(GOT_ERR_OBJ_TYPE);
3468 s->first_displayed_line = 1;
3469 s->last_displayed_line = view->nlines;
3470 s->selected_line = 1;
3478 s->id1 = got_object_id_dup(id1);
3480 return got_error_from_errno("got_object_id_dup");
3484 s->id2 = got_object_id_dup(id2);
3485 if (s->id2 == NULL) {
3488 return got_error_from_errno("got_object_id_dup");
3491 s->first_displayed_line = 1;
3492 s->last_displayed_line = view->nlines;
3493 s->diff_context = diff_context;
3494 s->ignore_whitespace = ignore_whitespace;
3495 s->force_text_diff = force_text_diff;
3496 s->log_view = log_view;
3499 STAILQ_INIT(&s->colors);
3500 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3501 err = add_color(&s->colors,
3502 "^-", TOG_COLOR_DIFF_MINUS,
3503 get_color_value("TOG_COLOR_DIFF_MINUS"));
3506 err = add_color(&s->colors, "^\\+",
3507 TOG_COLOR_DIFF_PLUS,
3508 get_color_value("TOG_COLOR_DIFF_PLUS"));
3510 free_colors(&s->colors);
3513 err = add_color(&s->colors,
3514 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3515 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3517 free_colors(&s->colors);
3521 err = add_color(&s->colors,
3522 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3523 TOG_COLOR_DIFF_META,
3524 get_color_value("TOG_COLOR_DIFF_META"));
3526 free_colors(&s->colors);
3530 err = add_color(&s->colors,
3531 "^(from|via): ", TOG_COLOR_AUTHOR,
3532 get_color_value("TOG_COLOR_AUTHOR"));
3534 free_colors(&s->colors);
3538 err = add_color(&s->colors,
3539 "^date: ", TOG_COLOR_DATE,
3540 get_color_value("TOG_COLOR_DATE"));
3542 free_colors(&s->colors);
3547 if (log_view && view_is_splitscreen(view))
3548 show_log_view(log_view); /* draw vborder */
3549 diff_view_indicate_progress(view);
3551 s->line_offsets = NULL;
3553 err = create_diff(s);
3559 free_colors(&s->colors);
3563 view->show = show_diff_view;
3564 view->input = input_diff_view;
3565 view->close = close_diff_view;
3566 view->search_start = search_start_diff_view;
3567 view->search_next = search_next_diff_view;
3572 static const struct got_error *
3573 close_diff_view(struct tog_view *view)
3575 const struct got_error *err = NULL;
3576 struct tog_diff_view_state *s = &view->state.diff;
3582 if (s->f && fclose(s->f) == EOF)
3583 err = got_error_from_errno("fclose");
3584 free_colors(&s->colors);
3585 free(s->line_offsets);
3586 s->line_offsets = NULL;
3591 static const struct got_error *
3592 show_diff_view(struct tog_view *view)
3594 const struct got_error *err;
3595 struct tog_diff_view_state *s = &view->state.diff;
3596 char *id_str1 = NULL, *id_str2, *header;
3597 const char *label1, *label2;
3600 err = got_object_id_str(&id_str1, s->id1);
3603 label1 = s->label1 ? : id_str1;
3605 label1 = "/dev/null";
3607 err = got_object_id_str(&id_str2, s->id2);
3610 label2 = s->label2 ? : id_str2;
3612 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3613 err = got_error_from_errno("asprintf");
3621 err = draw_file(view, header);
3626 static const struct got_error *
3627 set_selected_commit(struct tog_diff_view_state *s,
3628 struct commit_queue_entry *entry)
3630 const struct got_error *err;
3631 const struct got_object_id_queue *parent_ids;
3632 struct got_commit_object *selected_commit;
3633 struct got_object_qid *pid;
3636 s->id2 = got_object_id_dup(entry->id);
3638 return got_error_from_errno("got_object_id_dup");
3640 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3643 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3645 pid = STAILQ_FIRST(parent_ids);
3646 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3647 got_object_commit_close(selected_commit);
3651 static const struct got_error *
3652 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3654 const struct got_error *err = NULL;
3655 struct tog_diff_view_state *s = &view->state.diff;
3656 struct tog_log_view_state *ls;
3657 struct commit_queue_entry *old_selected_entry;
3659 size_t linesize = 0;
3667 s->force_text_diff = !s->force_text_diff;
3669 s->ignore_whitespace = !s->ignore_whitespace;
3670 wclear(view->window);
3671 s->first_displayed_line = 1;
3672 s->last_displayed_line = view->nlines;
3673 diff_view_indicate_progress(view);
3674 err = create_diff(s);
3678 s->first_displayed_line = 1;
3685 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3690 if (s->first_displayed_line > 1)
3691 s->first_displayed_line--;
3695 if (s->first_displayed_line == 1)
3698 while (i++ < view->nlines - 1 &&
3699 s->first_displayed_line > 1)
3700 s->first_displayed_line--;
3705 s->first_displayed_line++;
3713 while (!s->eof && i++ < view->nlines - 1) {
3714 linelen = getline(&line, &linesize, s->f);
3715 s->first_displayed_line++;
3716 if (linelen == -1) {
3720 err = got_ferror(s->f, GOT_ERR_IO);
3727 if (s->diff_context > 0) {
3729 diff_view_indicate_progress(view);
3730 err = create_diff(s);
3731 if (s->first_displayed_line + view->nlines - 1 >
3733 s->first_displayed_line = 1;
3734 s->last_displayed_line = view->nlines;
3739 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3741 diff_view_indicate_progress(view);
3742 err = create_diff(s);
3747 if (s->log_view == NULL)
3749 ls = &s->log_view->state.log;
3750 old_selected_entry = ls->selected_entry;
3752 err = input_log_view(NULL, s->log_view, KEY_UP);
3756 if (old_selected_entry == ls->selected_entry)
3759 err = set_selected_commit(s, ls->selected_entry);
3763 s->first_displayed_line = 1;
3764 s->last_displayed_line = view->nlines;
3766 diff_view_indicate_progress(view);
3767 err = create_diff(s);
3771 if (s->log_view == NULL)
3773 ls = &s->log_view->state.log;
3774 old_selected_entry = ls->selected_entry;
3776 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3780 if (old_selected_entry == ls->selected_entry)
3783 err = set_selected_commit(s, ls->selected_entry);
3787 s->first_displayed_line = 1;
3788 s->last_displayed_line = view->nlines;
3790 diff_view_indicate_progress(view);
3791 err = create_diff(s);
3800 static const struct got_error *
3801 cmd_diff(int argc, char *argv[])
3803 const struct got_error *error = NULL;
3804 struct got_repository *repo = NULL;
3805 struct got_worktree *worktree = NULL;
3806 struct got_object_id *id1 = NULL, *id2 = NULL;
3807 char *repo_path = NULL, *cwd = NULL;
3808 char *id_str1 = NULL, *id_str2 = NULL;
3809 char *label1 = NULL, *label2 = NULL;
3810 int diff_context = 3, ignore_whitespace = 0;
3811 int ch, force_text_diff = 0;
3813 struct tog_view *view;
3815 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3818 force_text_diff = 1;
3821 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3824 err(1, "-C option %s", errstr);
3827 repo_path = realpath(optarg, NULL);
3828 if (repo_path == NULL)
3829 return got_error_from_errno2("realpath",
3831 got_path_strip_trailing_slashes(repo_path);
3834 ignore_whitespace = 1;
3846 usage_diff(); /* TODO show local worktree changes */
3847 } else if (argc == 2) {
3853 if (repo_path == NULL) {
3854 cwd = getcwd(NULL, 0);
3856 return got_error_from_errno("getcwd");
3857 error = got_worktree_open(&worktree, cwd);
3858 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3862 strdup(got_worktree_get_repo_path(worktree));
3864 repo_path = strdup(cwd);
3865 if (repo_path == NULL) {
3866 error = got_error_from_errno("strdup");
3871 error = got_repo_open(&repo, repo_path, NULL);
3877 error = apply_unveil(got_repo_get_path(repo), NULL);
3881 error = tog_load_refs(repo);
3885 error = got_repo_match_object_id(&id1, &label1, id_str1,
3886 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3890 error = got_repo_match_object_id(&id2, &label2, id_str2,
3891 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3895 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3897 error = got_error_from_errno("view_open");
3900 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3901 ignore_whitespace, force_text_diff, NULL, repo);
3904 error = view_loop(view);
3911 const struct got_error *close_err = got_repo_close(repo);
3916 got_worktree_close(worktree);
3925 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3930 struct tog_blame_line {
3932 struct got_object_id *id;
3935 static const struct got_error *
3936 draw_blame(struct tog_view *view)
3938 struct tog_blame_view_state *s = &view->state.blame;
3939 struct tog_blame *blame = &s->blame;
3940 regmatch_t *regmatch = &view->regmatch;
3941 const struct got_error *err;
3942 int lineno = 0, nprinted = 0;
3944 size_t linesize = 0;
3948 struct tog_blame_line *blame_line;
3949 struct got_object_id *prev_id = NULL;
3951 struct tog_color *tc;
3953 err = got_object_id_str(&id_str, s->blamed_commit->id);
3958 werase(view->window);
3960 if (asprintf(&line, "commit %s", id_str) == -1) {
3961 err = got_error_from_errno("asprintf");
3966 err = format_line(&wline, &width, line, view->ncols, 0);
3971 if (view_needs_focus_indication(view))
3972 wstandout(view->window);
3973 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3975 wattr_on(view->window,
3976 COLOR_PAIR(tc->colorpair), NULL);
3977 waddwstr(view->window, wline);
3979 wattr_off(view->window,
3980 COLOR_PAIR(tc->colorpair), NULL);
3981 if (view_needs_focus_indication(view))
3982 wstandend(view->window);
3985 if (width < view->ncols - 1)
3986 waddch(view->window, '\n');
3988 if (asprintf(&line, "[%d/%d] %s%s",
3989 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3990 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3992 return got_error_from_errno("asprintf");
3995 err = format_line(&wline, &width, line, view->ncols, 0);
4000 waddwstr(view->window, wline);
4003 if (width < view->ncols - 1)
4004 waddch(view->window, '\n');
4007 while (nprinted < view->nlines - 2) {
4008 linelen = getline(&line, &linesize, blame->f);
4009 if (linelen == -1) {
4010 if (feof(blame->f)) {
4015 return got_ferror(blame->f, GOT_ERR_IO);
4017 if (++lineno < s->first_displayed_line)
4020 if (view->focussed && nprinted == s->selected_line - 1)
4021 wstandout(view->window);
4023 if (blame->nlines > 0) {
4024 blame_line = &blame->lines[lineno - 1];
4025 if (blame_line->annotated && prev_id &&
4026 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4028 nprinted == s->selected_line - 1)) {
4029 waddstr(view->window, " ");
4030 } else if (blame_line->annotated) {
4032 err = got_object_id_str(&id_str, blame_line->id);
4037 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4039 wattr_on(view->window,
4040 COLOR_PAIR(tc->colorpair), NULL);
4041 wprintw(view->window, "%.8s", id_str);
4043 wattr_off(view->window,
4044 COLOR_PAIR(tc->colorpair), NULL);
4046 prev_id = blame_line->id;
4048 waddstr(view->window, "........");
4052 waddstr(view->window, "........");
4056 if (view->focussed && nprinted == s->selected_line - 1)
4057 wstandend(view->window);
4058 waddstr(view->window, " ");
4060 if (view->ncols <= 9) {
4062 wline = wcsdup(L"");
4063 if (wline == NULL) {
4064 err = got_error_from_errno("wcsdup");
4068 } else if (s->first_displayed_line + nprinted ==
4070 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4071 err = add_matched_line(&width, line, view->ncols - 9, 9,
4072 view->window, regmatch);
4079 err = format_line(&wline, &width, line,
4080 view->ncols - 9, 9);
4081 waddwstr(view->window, wline);
4087 if (width <= view->ncols - 1)
4088 waddch(view->window, '\n');
4089 if (++nprinted == 1)
4090 s->first_displayed_line = lineno;
4093 s->last_displayed_line = lineno;
4100 static const struct got_error *
4101 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4103 const struct got_error *err = NULL;
4104 struct tog_blame_cb_args *a = arg;
4105 struct tog_blame_line *line;
4108 if (nlines != a->nlines ||
4109 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4110 return got_error(GOT_ERR_RANGE);
4112 errcode = pthread_mutex_lock(&tog_mutex);
4114 return got_error_set_errno(errcode, "pthread_mutex_lock");
4116 if (*a->quit) { /* user has quit the blame view */
4117 err = got_error(GOT_ERR_ITER_COMPLETED);
4122 goto done; /* no change in this commit */
4124 line = &a->lines[lineno - 1];
4125 if (line->annotated)
4128 line->id = got_object_id_dup(id);
4129 if (line->id == NULL) {
4130 err = got_error_from_errno("got_object_id_dup");
4133 line->annotated = 1;
4135 errcode = pthread_mutex_unlock(&tog_mutex);
4137 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4142 blame_thread(void *arg)
4144 const struct got_error *err, *close_err;
4145 struct tog_blame_thread_args *ta = arg;
4146 struct tog_blame_cb_args *a = ta->cb_args;
4149 err = block_signals_used_by_main_thread();
4153 err = got_blame(ta->path, a->commit_id, ta->repo,
4154 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4155 if (err && err->code == GOT_ERR_CANCELLED)
4158 errcode = pthread_mutex_lock(&tog_mutex);
4160 return (void *)got_error_set_errno(errcode,
4161 "pthread_mutex_lock");
4163 close_err = got_repo_close(ta->repo);
4169 errcode = pthread_mutex_unlock(&tog_mutex);
4170 if (errcode && err == NULL)
4171 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4176 static struct got_object_id *
4177 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4178 int first_displayed_line, int selected_line)
4180 struct tog_blame_line *line;
4185 line = &lines[first_displayed_line - 1 + selected_line - 1];
4186 if (!line->annotated)
4192 static const struct got_error *
4193 stop_blame(struct tog_blame *blame)
4195 const struct got_error *err = NULL;
4198 if (blame->thread) {
4200 errcode = pthread_mutex_unlock(&tog_mutex);
4202 return got_error_set_errno(errcode,
4203 "pthread_mutex_unlock");
4204 errcode = pthread_join(blame->thread, (void **)&err);
4206 return got_error_set_errno(errcode, "pthread_join");
4207 errcode = pthread_mutex_lock(&tog_mutex);
4209 return got_error_set_errno(errcode,
4210 "pthread_mutex_lock");
4211 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4213 blame->thread = NULL;
4215 if (blame->thread_args.repo) {
4216 const struct got_error *close_err;
4217 close_err = got_repo_close(blame->thread_args.repo);
4220 blame->thread_args.repo = NULL;
4223 if (fclose(blame->f) == EOF && err == NULL)
4224 err = got_error_from_errno("fclose");
4228 for (i = 0; i < blame->nlines; i++)
4229 free(blame->lines[i].id);
4231 blame->lines = NULL;
4233 free(blame->cb_args.commit_id);
4234 blame->cb_args.commit_id = NULL;
4239 static const struct got_error *
4240 cancel_blame_view(void *arg)
4242 const struct got_error *err = NULL;
4246 errcode = pthread_mutex_lock(&tog_mutex);
4248 return got_error_set_errno(errcode,
4249 "pthread_mutex_unlock");
4252 err = got_error(GOT_ERR_CANCELLED);
4254 errcode = pthread_mutex_unlock(&tog_mutex);
4256 return got_error_set_errno(errcode,
4257 "pthread_mutex_lock");
4262 static const struct got_error *
4263 run_blame(struct tog_view *view)
4265 struct tog_blame_view_state *s = &view->state.blame;
4266 struct tog_blame *blame = &s->blame;
4267 const struct got_error *err = NULL;
4268 struct got_blob_object *blob = NULL;
4269 struct got_repository *thread_repo = NULL;
4270 struct got_object_id *obj_id = NULL;
4273 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4278 err = got_object_get_type(&obj_type, s->repo, obj_id);
4282 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4283 err = got_error(GOT_ERR_OBJ_TYPE);
4287 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4290 blame->f = got_opentemp();
4291 if (blame->f == NULL) {
4292 err = got_error_from_errno("got_opentemp");
4295 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4296 &blame->line_offsets, blame->f, blob);
4299 if (blame->nlines == 0) {
4300 s->blame_complete = 1;
4304 /* Don't include \n at EOF in the blame line count. */
4305 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4308 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4309 if (blame->lines == NULL) {
4310 err = got_error_from_errno("calloc");
4314 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4318 blame->cb_args.view = view;
4319 blame->cb_args.lines = blame->lines;
4320 blame->cb_args.nlines = blame->nlines;
4321 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4322 if (blame->cb_args.commit_id == NULL) {
4323 err = got_error_from_errno("got_object_id_dup");
4326 blame->cb_args.quit = &s->done;
4328 blame->thread_args.path = s->path;
4329 blame->thread_args.repo = thread_repo;
4330 blame->thread_args.cb_args = &blame->cb_args;
4331 blame->thread_args.complete = &s->blame_complete;
4332 blame->thread_args.cancel_cb = cancel_blame_view;
4333 blame->thread_args.cancel_arg = &s->done;
4334 s->blame_complete = 0;
4336 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4337 s->first_displayed_line = 1;
4338 s->last_displayed_line = view->nlines;
4339 s->selected_line = 1;
4344 got_object_blob_close(blob);
4351 static const struct got_error *
4352 open_blame_view(struct tog_view *view, char *path,
4353 struct got_object_id *commit_id, struct got_repository *repo)
4355 const struct got_error *err = NULL;
4356 struct tog_blame_view_state *s = &view->state.blame;
4358 STAILQ_INIT(&s->blamed_commits);
4360 s->path = strdup(path);
4361 if (s->path == NULL)
4362 return got_error_from_errno("strdup");
4364 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4370 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4371 s->first_displayed_line = 1;
4372 s->last_displayed_line = view->nlines;
4373 s->selected_line = 1;
4374 s->blame_complete = 0;
4376 s->commit_id = commit_id;
4377 memset(&s->blame, 0, sizeof(s->blame));
4379 STAILQ_INIT(&s->colors);
4380 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4381 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4382 get_color_value("TOG_COLOR_COMMIT"));
4387 view->show = show_blame_view;
4388 view->input = input_blame_view;
4389 view->close = close_blame_view;
4390 view->search_start = search_start_blame_view;
4391 view->search_next = search_next_blame_view;
4393 return run_blame(view);
4396 static const struct got_error *
4397 close_blame_view(struct tog_view *view)
4399 const struct got_error *err = NULL;
4400 struct tog_blame_view_state *s = &view->state.blame;
4402 if (s->blame.thread)
4403 err = stop_blame(&s->blame);
4405 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4406 struct got_object_qid *blamed_commit;
4407 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4408 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4409 got_object_qid_free(blamed_commit);
4413 free_colors(&s->colors);
4418 static const struct got_error *
4419 search_start_blame_view(struct tog_view *view)
4421 struct tog_blame_view_state *s = &view->state.blame;
4423 s->matched_line = 0;
4427 static const struct got_error *
4428 search_next_blame_view(struct tog_view *view)
4430 struct tog_blame_view_state *s = &view->state.blame;
4433 size_t linesize = 0;
4436 if (!view->searching) {
4437 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4441 if (s->matched_line) {
4442 if (view->searching == TOG_SEARCH_FORWARD)
4443 lineno = s->matched_line + 1;
4445 lineno = s->matched_line - 1;
4447 if (view->searching == TOG_SEARCH_FORWARD)
4450 lineno = s->blame.nlines;
4456 if (lineno <= 0 || lineno > s->blame.nlines) {
4457 if (s->matched_line == 0) {
4458 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4462 if (view->searching == TOG_SEARCH_FORWARD)
4465 lineno = s->blame.nlines;
4468 offset = s->blame.line_offsets[lineno - 1];
4469 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4471 return got_error_from_errno("fseeko");
4473 linelen = getline(&line, &linesize, s->blame.f);
4474 if (linelen != -1 &&
4475 match_line(line, &view->regex, 1, &view->regmatch)) {
4476 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4477 s->matched_line = lineno;
4480 if (view->searching == TOG_SEARCH_FORWARD)
4487 if (s->matched_line) {
4488 s->first_displayed_line = s->matched_line;
4489 s->selected_line = 1;
4495 static const struct got_error *
4496 show_blame_view(struct tog_view *view)
4498 const struct got_error *err = NULL;
4499 struct tog_blame_view_state *s = &view->state.blame;
4502 if (s->blame.thread == NULL && !s->blame_complete) {
4503 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4504 &s->blame.thread_args);
4506 return got_error_set_errno(errcode, "pthread_create");
4508 halfdelay(1); /* fast refresh while annotating */
4511 if (s->blame_complete)
4512 halfdelay(10); /* disable fast refresh */
4514 err = draw_blame(view);
4520 static const struct got_error *
4521 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4523 const struct got_error *err = NULL, *thread_err = NULL;
4524 struct tog_view *diff_view;
4525 struct tog_blame_view_state *s = &view->state.blame;
4534 s->selected_line = 1;
4535 s->first_displayed_line = 1;
4539 if (s->blame.nlines < view->nlines - 2) {
4540 s->selected_line = s->blame.nlines;
4541 s->first_displayed_line = 1;
4543 s->selected_line = view->nlines - 2;
4544 s->first_displayed_line = s->blame.nlines -
4550 if (s->selected_line > 1)
4552 else if (s->selected_line == 1 &&
4553 s->first_displayed_line > 1)
4554 s->first_displayed_line--;
4558 if (s->first_displayed_line == 1) {
4559 s->selected_line = 1;
4562 if (s->first_displayed_line > view->nlines - 2)
4563 s->first_displayed_line -=
4566 s->first_displayed_line = 1;
4570 if (s->selected_line < view->nlines - 2 &&
4571 s->first_displayed_line +
4572 s->selected_line <= s->blame.nlines)
4574 else if (s->last_displayed_line <
4576 s->first_displayed_line++;
4580 struct got_object_id *id = NULL;
4581 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4582 s->first_displayed_line, s->selected_line);
4586 struct got_commit_object *commit;
4587 struct got_object_qid *pid;
4588 struct got_object_id *blob_id = NULL;
4590 err = got_object_open_as_commit(&commit,
4595 got_object_commit_get_parent_ids(commit));
4597 got_object_commit_close(commit);
4600 /* Check if path history ends here. */
4601 err = got_object_id_by_path(&blob_id, s->repo,
4604 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4606 got_object_commit_close(commit);
4609 err = got_object_get_type(&obj_type, s->repo,
4612 /* Can't blame non-blob type objects. */
4613 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4614 got_object_commit_close(commit);
4617 err = got_object_qid_alloc(&s->blamed_commit,
4619 got_object_commit_close(commit);
4621 if (got_object_id_cmp(id,
4622 s->blamed_commit->id) == 0)
4624 err = got_object_qid_alloc(&s->blamed_commit,
4630 thread_err = stop_blame(&s->blame);
4634 STAILQ_INSERT_HEAD(&s->blamed_commits,
4635 s->blamed_commit, entry);
4636 err = run_blame(view);
4642 struct got_object_qid *first;
4643 first = STAILQ_FIRST(&s->blamed_commits);
4644 if (!got_object_id_cmp(first->id, s->commit_id))
4647 thread_err = stop_blame(&s->blame);
4651 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4652 got_object_qid_free(s->blamed_commit);
4654 STAILQ_FIRST(&s->blamed_commits);
4655 err = run_blame(view);
4662 struct got_object_id *id = NULL;
4663 struct got_object_qid *pid;
4664 struct got_commit_object *commit = NULL;
4665 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4666 s->first_displayed_line, s->selected_line);
4669 err = got_object_open_as_commit(&commit, s->repo, id);
4673 got_object_commit_get_parent_ids(commit));
4674 if (view_is_parent_view(view))
4675 begin_x = view_split_begin_x(view->begin_x);
4676 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4677 if (diff_view == NULL) {
4678 got_object_commit_close(commit);
4679 err = got_error_from_errno("view_open");
4682 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4683 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4684 got_object_commit_close(commit);
4686 view_close(diff_view);
4690 diff_view->focussed = 1;
4691 if (view_is_parent_view(view)) {
4692 err = view_close_child(view);
4695 view_set_child(view, diff_view);
4696 view->focus_child = 1;
4698 *new_view = diff_view;
4706 if (s->last_displayed_line >= s->blame.nlines &&
4707 s->selected_line >= MIN(s->blame.nlines,
4708 view->nlines - 2)) {
4711 if (s->last_displayed_line >= s->blame.nlines &&
4712 s->selected_line < view->nlines - 2) {
4713 s->selected_line = MIN(s->blame.nlines,
4717 if (s->last_displayed_line + view->nlines - 2
4719 s->first_displayed_line +=
4722 s->first_displayed_line =
4727 if (s->selected_line > view->nlines - 2) {
4728 s->selected_line = MIN(s->blame.nlines,
4735 return thread_err ? thread_err : err;
4738 static const struct got_error *
4739 cmd_blame(int argc, char *argv[])
4741 const struct got_error *error;
4742 struct got_repository *repo = NULL;
4743 struct got_worktree *worktree = NULL;
4744 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4745 char *link_target = NULL;
4746 struct got_object_id *commit_id = NULL;
4747 char *commit_id_str = NULL;
4749 struct tog_view *view;
4751 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4754 commit_id_str = optarg;
4757 repo_path = realpath(optarg, NULL);
4758 if (repo_path == NULL)
4759 return got_error_from_errno2("realpath",
4774 if (repo_path == NULL) {
4775 cwd = getcwd(NULL, 0);
4777 return got_error_from_errno("getcwd");
4778 error = got_worktree_open(&worktree, cwd);
4779 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4783 strdup(got_worktree_get_repo_path(worktree));
4785 repo_path = strdup(cwd);
4786 if (repo_path == NULL) {
4787 error = got_error_from_errno("strdup");
4792 error = got_repo_open(&repo, repo_path, NULL);
4796 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4803 error = apply_unveil(got_repo_get_path(repo), NULL);
4807 error = tog_load_refs(repo);
4811 if (commit_id_str == NULL) {
4812 struct got_reference *head_ref;
4813 error = got_ref_open(&head_ref, repo, worktree ?
4814 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4817 error = got_ref_resolve(&commit_id, repo, head_ref);
4818 got_ref_close(head_ref);
4820 error = got_repo_match_object_id(&commit_id, NULL,
4821 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4826 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4828 error = got_error_from_errno("view_open");
4832 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4837 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4842 /* Release work tree lock. */
4843 got_worktree_close(worktree);
4846 error = view_loop(view);
4854 got_worktree_close(worktree);
4856 const struct got_error *close_err = got_repo_close(repo);
4864 static const struct got_error *
4865 draw_tree_entries(struct tog_view *view, const char *parent_path)
4867 struct tog_tree_view_state *s = &view->state.tree;
4868 const struct got_error *err = NULL;
4869 struct got_tree_entry *te;
4871 struct tog_color *tc;
4872 int width, n, i, nentries;
4873 int limit = view->nlines;
4877 werase(view->window);
4882 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4885 if (view_needs_focus_indication(view))
4886 wstandout(view->window);
4887 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4889 wattr_on(view->window,
4890 COLOR_PAIR(tc->colorpair), NULL);
4891 waddwstr(view->window, wline);
4893 wattr_off(view->window,
4894 COLOR_PAIR(tc->colorpair), NULL);
4895 if (view_needs_focus_indication(view))
4896 wstandend(view->window);
4899 if (width < view->ncols - 1)
4900 waddch(view->window, '\n');
4903 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4906 waddwstr(view->window, wline);
4909 if (width < view->ncols - 1)
4910 waddch(view->window, '\n');
4913 waddch(view->window, '\n');
4917 if (s->first_displayed_entry == NULL) {
4918 te = got_object_tree_get_first_entry(s->tree);
4919 if (s->selected == 0) {
4921 wstandout(view->window);
4922 s->selected_entry = NULL;
4924 waddstr(view->window, " ..\n"); /* parent directory */
4925 if (s->selected == 0 && view->focussed)
4926 wstandend(view->window);
4933 te = s->first_displayed_entry;
4936 nentries = got_object_tree_get_nentries(s->tree);
4937 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4938 char *line = NULL, *id_str = NULL, *link_target = NULL;
4939 const char *modestr = "";
4942 te = got_object_tree_get_entry(s->tree, i);
4943 mode = got_tree_entry_get_mode(te);
4946 err = got_object_id_str(&id_str,
4947 got_tree_entry_get_id(te));
4949 return got_error_from_errno(
4950 "got_object_id_str");
4952 if (got_object_tree_entry_is_submodule(te))
4954 else if (S_ISLNK(mode)) {
4957 err = got_tree_entry_get_symlink_target(&link_target,
4963 for (i = 0; i < strlen(link_target); i++) {
4964 if (!isprint((unsigned char)link_target[i]))
4965 link_target[i] = '?';
4969 else if (S_ISDIR(mode))
4971 else if (mode & S_IXUSR)
4973 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4974 got_tree_entry_get_name(te), modestr,
4975 link_target ? " -> ": "",
4976 link_target ? link_target : "") == -1) {
4979 return got_error_from_errno("asprintf");
4983 err = format_line(&wline, &width, line, view->ncols, 0);
4988 if (n == s->selected) {
4990 wstandout(view->window);
4991 s->selected_entry = te;
4993 tc = match_color(&s->colors, line);
4995 wattr_on(view->window,
4996 COLOR_PAIR(tc->colorpair), NULL);
4997 waddwstr(view->window, wline);
4999 wattr_off(view->window,
5000 COLOR_PAIR(tc->colorpair), NULL);
5001 if (width < view->ncols - 1)
5002 waddch(view->window, '\n');
5003 if (n == s->selected && view->focussed)
5004 wstandend(view->window);
5010 s->last_displayed_entry = te;
5019 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5021 struct got_tree_entry *te;
5022 int isroot = s->tree == s->root;
5025 if (s->first_displayed_entry == NULL)
5028 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5029 while (i++ < maxscroll) {
5032 s->first_displayed_entry = NULL;
5035 s->first_displayed_entry = te;
5036 te = got_tree_entry_get_prev(s->tree, te);
5041 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5043 struct got_tree_entry *next, *last;
5046 if (s->first_displayed_entry)
5047 next = got_tree_entry_get_next(s->tree,
5048 s->first_displayed_entry);
5050 next = got_object_tree_get_first_entry(s->tree);
5052 last = s->last_displayed_entry;
5053 while (next && last && n++ < maxscroll) {
5054 last = got_tree_entry_get_next(s->tree, last);
5056 s->first_displayed_entry = next;
5057 next = got_tree_entry_get_next(s->tree, next);
5062 static const struct got_error *
5063 tree_entry_path(char **path, struct tog_parent_trees *parents,
5064 struct got_tree_entry *te)
5066 const struct got_error *err = NULL;
5067 struct tog_parent_tree *pt;
5068 size_t len = 2; /* for leading slash and NUL */
5070 TAILQ_FOREACH(pt, parents, entry)
5071 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5074 len += strlen(got_tree_entry_get_name(te));
5076 *path = calloc(1, len);
5078 return got_error_from_errno("calloc");
5081 pt = TAILQ_LAST(parents, tog_parent_trees);
5083 const char *name = got_tree_entry_get_name(pt->selected_entry);
5084 if (strlcat(*path, name, len) >= len) {
5085 err = got_error(GOT_ERR_NO_SPACE);
5088 if (strlcat(*path, "/", len) >= len) {
5089 err = got_error(GOT_ERR_NO_SPACE);
5092 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5095 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5096 err = got_error(GOT_ERR_NO_SPACE);
5108 static const struct got_error *
5109 blame_tree_entry(struct tog_view **new_view, int begin_x,
5110 struct got_tree_entry *te, struct tog_parent_trees *parents,
5111 struct got_object_id *commit_id, struct got_repository *repo)
5113 const struct got_error *err = NULL;
5115 struct tog_view *blame_view;
5119 err = tree_entry_path(&path, parents, te);
5123 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5124 if (blame_view == NULL) {
5125 err = got_error_from_errno("view_open");
5129 err = open_blame_view(blame_view, path, commit_id, repo);
5131 if (err->code == GOT_ERR_CANCELLED)
5133 view_close(blame_view);
5135 *new_view = blame_view;
5141 static const struct got_error *
5142 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5143 struct tog_tree_view_state *s)
5145 struct tog_view *log_view;
5146 const struct got_error *err = NULL;
5151 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5152 if (log_view == NULL)
5153 return got_error_from_errno("view_open");
5155 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5159 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5162 view_close(log_view);
5164 *new_view = log_view;
5169 static const struct got_error *
5170 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5171 const char *head_ref_name, struct got_repository *repo)
5173 const struct got_error *err = NULL;
5174 char *commit_id_str = NULL;
5175 struct tog_tree_view_state *s = &view->state.tree;
5176 struct got_commit_object *commit = NULL;
5178 TAILQ_INIT(&s->parents);
5179 STAILQ_INIT(&s->colors);
5181 s->commit_id = got_object_id_dup(commit_id);
5182 if (s->commit_id == NULL)
5183 return got_error_from_errno("got_object_id_dup");
5185 err = got_object_open_as_commit(&commit, repo, commit_id);
5190 * The root is opened here and will be closed when the view is closed.
5191 * Any visited subtrees and their path-wise parents are opened and
5194 err = got_object_open_as_tree(&s->root, repo,
5195 got_object_commit_get_tree_id(commit));
5200 err = got_object_id_str(&commit_id_str, commit_id);
5204 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5205 err = got_error_from_errno("asprintf");
5209 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5210 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5211 if (head_ref_name) {
5212 s->head_ref_name = strdup(head_ref_name);
5213 if (s->head_ref_name == NULL) {
5214 err = got_error_from_errno("strdup");
5220 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5221 err = add_color(&s->colors, "\\$$",
5222 TOG_COLOR_TREE_SUBMODULE,
5223 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5226 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5227 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5230 err = add_color(&s->colors, "/$",
5231 TOG_COLOR_TREE_DIRECTORY,
5232 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5236 err = add_color(&s->colors, "\\*$",
5237 TOG_COLOR_TREE_EXECUTABLE,
5238 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5242 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5243 get_color_value("TOG_COLOR_COMMIT"));
5248 view->show = show_tree_view;
5249 view->input = input_tree_view;
5250 view->close = close_tree_view;
5251 view->search_start = search_start_tree_view;
5252 view->search_next = search_next_tree_view;
5254 free(commit_id_str);
5256 got_object_commit_close(commit);
5258 close_tree_view(view);
5262 static const struct got_error *
5263 close_tree_view(struct tog_view *view)
5265 struct tog_tree_view_state *s = &view->state.tree;
5267 free_colors(&s->colors);
5268 free(s->tree_label);
5269 s->tree_label = NULL;
5271 s->commit_id = NULL;
5272 free(s->head_ref_name);
5273 s->head_ref_name = NULL;
5274 while (!TAILQ_EMPTY(&s->parents)) {
5275 struct tog_parent_tree *parent;
5276 parent = TAILQ_FIRST(&s->parents);
5277 TAILQ_REMOVE(&s->parents, parent, entry);
5278 if (parent->tree != s->root)
5279 got_object_tree_close(parent->tree);
5283 if (s->tree != NULL && s->tree != s->root)
5284 got_object_tree_close(s->tree);
5286 got_object_tree_close(s->root);
5290 static const struct got_error *
5291 search_start_tree_view(struct tog_view *view)
5293 struct tog_tree_view_state *s = &view->state.tree;
5295 s->matched_entry = NULL;
5300 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5302 regmatch_t regmatch;
5304 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5308 static const struct got_error *
5309 search_next_tree_view(struct tog_view *view)
5311 struct tog_tree_view_state *s = &view->state.tree;
5312 struct got_tree_entry *te = NULL;
5314 if (!view->searching) {
5315 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5319 if (s->matched_entry) {
5320 if (view->searching == TOG_SEARCH_FORWARD) {
5321 if (s->selected_entry)
5322 te = got_tree_entry_get_next(s->tree,
5325 te = got_object_tree_get_first_entry(s->tree);
5327 if (s->selected_entry == NULL)
5328 te = got_object_tree_get_last_entry(s->tree);
5330 te = got_tree_entry_get_prev(s->tree,
5334 if (view->searching == TOG_SEARCH_FORWARD)
5335 te = got_object_tree_get_first_entry(s->tree);
5337 te = got_object_tree_get_last_entry(s->tree);
5342 if (s->matched_entry == NULL) {
5343 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5346 if (view->searching == TOG_SEARCH_FORWARD)
5347 te = got_object_tree_get_first_entry(s->tree);
5349 te = got_object_tree_get_last_entry(s->tree);
5352 if (match_tree_entry(te, &view->regex)) {
5353 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5354 s->matched_entry = te;
5358 if (view->searching == TOG_SEARCH_FORWARD)
5359 te = got_tree_entry_get_next(s->tree, te);
5361 te = got_tree_entry_get_prev(s->tree, te);
5364 if (s->matched_entry) {
5365 s->first_displayed_entry = s->matched_entry;
5372 static const struct got_error *
5373 show_tree_view(struct tog_view *view)
5375 const struct got_error *err = NULL;
5376 struct tog_tree_view_state *s = &view->state.tree;
5379 err = tree_entry_path(&parent_path, &s->parents, NULL);
5383 err = draw_tree_entries(view, parent_path);
5390 static const struct got_error *
5391 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5393 const struct got_error *err = NULL;
5394 struct tog_tree_view_state *s = &view->state.tree;
5395 struct tog_view *log_view, *ref_view;
5396 struct got_tree_entry *te;
5401 s->show_ids = !s->show_ids;
5404 if (!s->selected_entry)
5406 if (view_is_parent_view(view))
5407 begin_x = view_split_begin_x(view->begin_x);
5408 err = log_selected_tree_entry(&log_view, begin_x, s);
5410 log_view->focussed = 1;
5411 if (view_is_parent_view(view)) {
5412 err = view_close_child(view);
5415 view_set_child(view, log_view);
5416 view->focus_child = 1;
5418 *new_view = log_view;
5421 if (view_is_parent_view(view))
5422 begin_x = view_split_begin_x(view->begin_x);
5423 ref_view = view_open(view->nlines, view->ncols,
5424 view->begin_y, begin_x, TOG_VIEW_REF);
5425 if (ref_view == NULL)
5426 return got_error_from_errno("view_open");
5427 err = open_ref_view(ref_view, s->repo);
5429 view_close(ref_view);
5433 ref_view->focussed = 1;
5434 if (view_is_parent_view(view)) {
5435 err = view_close_child(view);
5438 view_set_child(view, ref_view);
5439 view->focus_child = 1;
5441 *new_view = ref_view;
5446 if (s->tree == s->root)
5447 s->first_displayed_entry =
5448 got_object_tree_get_first_entry(s->tree);
5450 s->first_displayed_entry = NULL;
5455 te = got_object_tree_get_last_entry(s->tree);
5456 for (n = 0; n < view->nlines - 3; n++) {
5458 if(s->tree != s->root) {
5459 s->first_displayed_entry = NULL;
5464 s->first_displayed_entry = te;
5465 te = got_tree_entry_get_prev(s->tree, te);
5468 s->selected = n - 1;
5472 if (s->selected > 0) {
5476 tree_scroll_up(s, 1);
5480 if (s->tree == s->root) {
5481 if (got_object_tree_get_first_entry(s->tree) ==
5482 s->first_displayed_entry)
5485 if (s->first_displayed_entry == NULL)
5488 tree_scroll_up(s, MAX(0, view->nlines - 3));
5492 if (s->selected < s->ndisplayed - 1) {
5496 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5498 /* can't scroll any further */
5500 tree_scroll_down(s, 1);
5504 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5506 /* can't scroll any further; move cursor down */
5507 if (s->selected < s->ndisplayed - 1)
5508 s->selected = s->ndisplayed - 1;
5511 tree_scroll_down(s, view->nlines - 3);
5516 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5517 struct tog_parent_tree *parent;
5518 /* user selected '..' */
5519 if (s->tree == s->root)
5521 parent = TAILQ_FIRST(&s->parents);
5522 TAILQ_REMOVE(&s->parents, parent,
5524 got_object_tree_close(s->tree);
5525 s->tree = parent->tree;
5526 s->first_displayed_entry =
5527 parent->first_displayed_entry;
5529 parent->selected_entry;
5530 s->selected = parent->selected;
5532 } else if (S_ISDIR(got_tree_entry_get_mode(
5533 s->selected_entry))) {
5534 struct got_tree_object *subtree;
5535 err = got_object_open_as_tree(&subtree, s->repo,
5536 got_tree_entry_get_id(s->selected_entry));
5539 err = tree_view_visit_subtree(s, subtree);
5541 got_object_tree_close(subtree);
5544 } else if (S_ISREG(got_tree_entry_get_mode(
5545 s->selected_entry))) {
5546 struct tog_view *blame_view;
5547 int begin_x = view_is_parent_view(view) ?
5548 view_split_begin_x(view->begin_x) : 0;
5550 err = blame_tree_entry(&blame_view, begin_x,
5551 s->selected_entry, &s->parents,
5552 s->commit_id, s->repo);
5556 blame_view->focussed = 1;
5557 if (view_is_parent_view(view)) {
5558 err = view_close_child(view);
5561 view_set_child(view, blame_view);
5562 view->focus_child = 1;
5564 *new_view = blame_view;
5568 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5569 s->selected = view->nlines - 4;
5582 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5587 static const struct got_error *
5588 cmd_tree(int argc, char *argv[])
5590 const struct got_error *error;
5591 struct got_repository *repo = NULL;
5592 struct got_worktree *worktree = NULL;
5593 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5594 struct got_object_id *commit_id = NULL;
5595 const char *commit_id_arg = NULL;
5597 struct got_reference *ref = NULL;
5598 const char *head_ref_name = NULL;
5600 struct tog_view *view;
5602 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5605 commit_id_arg = optarg;
5608 repo_path = realpath(optarg, NULL);
5609 if (repo_path == NULL)
5610 return got_error_from_errno2("realpath",
5625 if (repo_path == NULL) {
5626 cwd = getcwd(NULL, 0);
5628 return got_error_from_errno("getcwd");
5629 error = got_worktree_open(&worktree, cwd);
5630 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5634 strdup(got_worktree_get_repo_path(worktree));
5636 repo_path = strdup(cwd);
5637 if (repo_path == NULL) {
5638 error = got_error_from_errno("strdup");
5643 error = got_repo_open(&repo, repo_path, NULL);
5647 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5654 error = apply_unveil(got_repo_get_path(repo), NULL);
5658 error = tog_load_refs(repo);
5662 if (commit_id_arg == NULL) {
5663 error = got_repo_match_object_id(&commit_id, &label,
5664 worktree ? got_worktree_get_head_ref_name(worktree) :
5665 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5668 head_ref_name = label;
5670 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5672 head_ref_name = got_ref_get_name(ref);
5673 else if (error->code != GOT_ERR_NOT_REF)
5675 error = got_repo_match_object_id(&commit_id, NULL,
5676 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5681 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5683 error = got_error_from_errno("view_open");
5686 error = open_tree_view(view, commit_id, head_ref_name, repo);
5689 if (!got_path_is_root_dir(in_repo_path)) {
5690 error = tree_view_walk_path(&view->state.tree, commit_id,
5697 /* Release work tree lock. */
5698 got_worktree_close(worktree);
5701 error = view_loop(view);
5710 const struct got_error *close_err = got_repo_close(repo);
5718 static const struct got_error *
5719 ref_view_load_refs(struct tog_ref_view_state *s)
5721 struct got_reflist_entry *sre;
5722 struct tog_reflist_entry *re;
5725 TAILQ_FOREACH(sre, &tog_refs, entry) {
5726 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5729 re = malloc(sizeof(*re));
5731 return got_error_from_errno("malloc");
5733 re->ref = got_ref_dup(sre->ref);
5734 if (re->ref == NULL)
5735 return got_error_from_errno("got_ref_dup");
5736 re->idx = s->nrefs++;
5737 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5740 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5745 ref_view_free_refs(struct tog_ref_view_state *s)
5747 struct tog_reflist_entry *re;
5749 while (!TAILQ_EMPTY(&s->refs)) {
5750 re = TAILQ_FIRST(&s->refs);
5751 TAILQ_REMOVE(&s->refs, re, entry);
5752 got_ref_close(re->ref);
5757 static const struct got_error *
5758 open_ref_view(struct tog_view *view, struct got_repository *repo)
5760 const struct got_error *err = NULL;
5761 struct tog_ref_view_state *s = &view->state.ref;
5763 s->selected_entry = 0;
5766 TAILQ_INIT(&s->refs);
5767 STAILQ_INIT(&s->colors);
5769 err = ref_view_load_refs(s);
5773 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5774 err = add_color(&s->colors, "^refs/heads/",
5775 TOG_COLOR_REFS_HEADS,
5776 get_color_value("TOG_COLOR_REFS_HEADS"));
5780 err = add_color(&s->colors, "^refs/tags/",
5781 TOG_COLOR_REFS_TAGS,
5782 get_color_value("TOG_COLOR_REFS_TAGS"));
5786 err = add_color(&s->colors, "^refs/remotes/",
5787 TOG_COLOR_REFS_REMOTES,
5788 get_color_value("TOG_COLOR_REFS_REMOTES"));
5793 view->show = show_ref_view;
5794 view->input = input_ref_view;
5795 view->close = close_ref_view;
5796 view->search_start = search_start_ref_view;
5797 view->search_next = search_next_ref_view;
5800 free_colors(&s->colors);
5804 static const struct got_error *
5805 close_ref_view(struct tog_view *view)
5807 struct tog_ref_view_state *s = &view->state.ref;
5809 ref_view_free_refs(s);
5810 free_colors(&s->colors);
5815 static const struct got_error *
5816 resolve_reflist_entry(struct got_object_id **commit_id,
5817 struct tog_reflist_entry *re, struct got_repository *repo)
5819 const struct got_error *err = NULL;
5820 struct got_object_id *obj_id;
5821 struct got_tag_object *tag = NULL;
5826 err = got_ref_resolve(&obj_id, repo, re->ref);
5830 err = got_object_get_type(&obj_type, repo, obj_id);
5835 case GOT_OBJ_TYPE_COMMIT:
5836 *commit_id = obj_id;
5838 case GOT_OBJ_TYPE_TAG:
5839 err = got_object_open_as_tag(&tag, repo, obj_id);
5843 err = got_object_get_type(&obj_type, repo,
5844 got_object_tag_get_object_id(tag));
5847 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5848 err = got_error(GOT_ERR_OBJ_TYPE);
5851 *commit_id = got_object_id_dup(
5852 got_object_tag_get_object_id(tag));
5853 if (*commit_id == NULL) {
5854 err = got_error_from_errno("got_object_id_dup");
5859 err = got_error(GOT_ERR_OBJ_TYPE);
5865 got_object_tag_close(tag);
5873 static const struct got_error *
5874 log_ref_entry(struct tog_view **new_view, int begin_x,
5875 struct tog_reflist_entry *re, struct got_repository *repo)
5877 struct tog_view *log_view;
5878 const struct got_error *err = NULL;
5879 struct got_object_id *commit_id = NULL;
5883 err = resolve_reflist_entry(&commit_id, re, repo);
5885 if (err->code != GOT_ERR_OBJ_TYPE)
5891 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5892 if (log_view == NULL) {
5893 err = got_error_from_errno("view_open");
5897 err = open_log_view(log_view, commit_id, repo,
5898 got_ref_get_name(re->ref), "", 0);
5901 view_close(log_view);
5903 *new_view = log_view;
5909 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5911 struct tog_reflist_entry *re;
5914 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5917 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5918 while (i++ < maxscroll) {
5921 s->first_displayed_entry = re;
5922 re = TAILQ_PREV(re, tog_reflist_head, entry);
5927 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5929 struct tog_reflist_entry *next, *last;
5932 if (s->first_displayed_entry)
5933 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5935 next = TAILQ_FIRST(&s->refs);
5937 last = s->last_displayed_entry;
5938 while (next && last && n++ < maxscroll) {
5939 last = TAILQ_NEXT(last, entry);
5941 s->first_displayed_entry = next;
5942 next = TAILQ_NEXT(next, entry);
5947 static const struct got_error *
5948 search_start_ref_view(struct tog_view *view)
5950 struct tog_ref_view_state *s = &view->state.ref;
5952 s->matched_entry = NULL;
5957 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5959 regmatch_t regmatch;
5961 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5965 static const struct got_error *
5966 search_next_ref_view(struct tog_view *view)
5968 struct tog_ref_view_state *s = &view->state.ref;
5969 struct tog_reflist_entry *re = NULL;
5971 if (!view->searching) {
5972 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5976 if (s->matched_entry) {
5977 if (view->searching == TOG_SEARCH_FORWARD) {
5978 if (s->selected_entry)
5979 re = TAILQ_NEXT(s->selected_entry, entry);
5981 re = TAILQ_PREV(s->selected_entry,
5982 tog_reflist_head, entry);
5984 if (s->selected_entry == NULL)
5985 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5987 re = TAILQ_PREV(s->selected_entry,
5988 tog_reflist_head, entry);
5991 if (view->searching == TOG_SEARCH_FORWARD)
5992 re = TAILQ_FIRST(&s->refs);
5994 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5999 if (s->matched_entry == NULL) {
6000 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6003 if (view->searching == TOG_SEARCH_FORWARD)
6004 re = TAILQ_FIRST(&s->refs);
6006 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6009 if (match_reflist_entry(re, &view->regex)) {
6010 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6011 s->matched_entry = re;
6015 if (view->searching == TOG_SEARCH_FORWARD)
6016 re = TAILQ_NEXT(re, entry);
6018 re = TAILQ_PREV(re, tog_reflist_head, entry);
6021 if (s->matched_entry) {
6022 s->first_displayed_entry = s->matched_entry;
6029 static const struct got_error *
6030 show_ref_view(struct tog_view *view)
6032 const struct got_error *err = NULL;
6033 struct tog_ref_view_state *s = &view->state.ref;
6034 struct tog_reflist_entry *re;
6037 struct tog_color *tc;
6039 int limit = view->nlines;
6041 werase(view->window);
6048 re = s->first_displayed_entry;
6050 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6052 return got_error_from_errno("asprintf");
6054 err = format_line(&wline, &width, line, view->ncols, 0);
6059 if (view_needs_focus_indication(view))
6060 wstandout(view->window);
6061 waddwstr(view->window, wline);
6062 if (view_needs_focus_indication(view))
6063 wstandend(view->window);
6068 if (width < view->ncols - 1)
6069 waddch(view->window, '\n');
6074 while (re && limit > 0) {
6077 if (got_ref_is_symbolic(re->ref)) {
6078 if (asprintf(&line, "%s -> %s",
6079 got_ref_get_name(re->ref),
6080 got_ref_get_symref_target(re->ref)) == -1)
6081 return got_error_from_errno("asprintf");
6082 } else if (s->show_ids) {
6083 struct got_object_id *id;
6085 err = got_ref_resolve(&id, s->repo, re->ref);
6088 err = got_object_id_str(&id_str, id);
6093 if (asprintf(&line, "%s: %s",
6094 got_ref_get_name(re->ref), id_str) == -1) {
6095 err = got_error_from_errno("asprintf");
6103 line = strdup(got_ref_get_name(re->ref));
6105 return got_error_from_errno("strdup");
6108 err = format_line(&wline, &width, line, view->ncols, 0);
6113 if (n == s->selected) {
6115 wstandout(view->window);
6116 s->selected_entry = re;
6118 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6120 wattr_on(view->window,
6121 COLOR_PAIR(tc->colorpair), NULL);
6122 waddwstr(view->window, wline);
6124 wattr_off(view->window,
6125 COLOR_PAIR(tc->colorpair), NULL);
6126 if (width < view->ncols - 1)
6127 waddch(view->window, '\n');
6128 if (n == s->selected && view->focussed)
6129 wstandend(view->window);
6135 s->last_displayed_entry = re;
6138 re = TAILQ_NEXT(re, entry);
6145 static const struct got_error *
6146 browse_ref_tree(struct tog_view **new_view, int begin_x,
6147 struct tog_reflist_entry *re, struct got_repository *repo)
6149 const struct got_error *err = NULL;
6150 struct got_object_id *commit_id = NULL;
6151 struct tog_view *tree_view;
6155 err = resolve_reflist_entry(&commit_id, re, repo);
6157 if (err->code != GOT_ERR_OBJ_TYPE)
6164 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6165 if (tree_view == NULL) {
6166 err = got_error_from_errno("view_open");
6170 err = open_tree_view(tree_view, commit_id,
6171 got_ref_get_name(re->ref), repo);
6175 *new_view = tree_view;
6180 static const struct got_error *
6181 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6183 const struct got_error *err = NULL;
6184 struct tog_ref_view_state *s = &view->state.ref;
6185 struct tog_view *log_view, *tree_view;
6186 struct tog_reflist_entry *re;
6191 s->show_ids = !s->show_ids;
6195 if (!s->selected_entry)
6197 if (view_is_parent_view(view))
6198 begin_x = view_split_begin_x(view->begin_x);
6199 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6202 log_view->focussed = 1;
6203 if (view_is_parent_view(view)) {
6204 err = view_close_child(view);
6207 view_set_child(view, log_view);
6208 view->focus_child = 1;
6210 *new_view = log_view;
6213 if (!s->selected_entry)
6215 if (view_is_parent_view(view))
6216 begin_x = view_split_begin_x(view->begin_x);
6217 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6219 if (err || tree_view == NULL)
6222 tree_view->focussed = 1;
6223 if (view_is_parent_view(view)) {
6224 err = view_close_child(view);
6227 view_set_child(view, tree_view);
6228 view->focus_child = 1;
6230 *new_view = tree_view;
6235 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6240 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6241 for (n = 0; n < view->nlines - 1; n++) {
6244 s->first_displayed_entry = re;
6245 re = TAILQ_PREV(re, tog_reflist_head, entry);
6248 s->selected = n - 1;
6252 if (s->selected > 0) {
6256 ref_scroll_up(s, 1);
6260 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6262 ref_scroll_up(s, MAX(0, view->nlines - 1));
6266 if (s->selected < s->ndisplayed - 1) {
6270 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6271 /* can't scroll any further */
6273 ref_scroll_down(s, 1);
6277 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6278 /* can't scroll any further; move cursor down */
6279 if (s->selected < s->ndisplayed - 1)
6280 s->selected = s->ndisplayed - 1;
6283 ref_scroll_down(s, view->nlines - 1);
6287 err = tog_load_refs(s->repo);
6290 ref_view_free_refs(s);
6291 err = ref_view_load_refs(s);
6294 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6295 s->selected = view->nlines - 2;
6308 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6313 static const struct got_error *
6314 cmd_ref(int argc, char *argv[])
6316 const struct got_error *error;
6317 struct got_repository *repo = NULL;
6318 struct got_worktree *worktree = NULL;
6319 char *cwd = NULL, *repo_path = NULL;
6321 struct tog_view *view;
6323 while ((ch = getopt(argc, argv, "r:")) != -1) {
6326 repo_path = realpath(optarg, NULL);
6327 if (repo_path == NULL)
6328 return got_error_from_errno2("realpath",
6343 if (repo_path == NULL) {
6344 cwd = getcwd(NULL, 0);
6346 return got_error_from_errno("getcwd");
6347 error = got_worktree_open(&worktree, cwd);
6348 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6352 strdup(got_worktree_get_repo_path(worktree));
6354 repo_path = strdup(cwd);
6355 if (repo_path == NULL) {
6356 error = got_error_from_errno("strdup");
6361 error = got_repo_open(&repo, repo_path, NULL);
6367 error = apply_unveil(got_repo_get_path(repo), NULL);
6371 error = tog_load_refs(repo);
6375 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6377 error = got_error_from_errno("view_open");
6381 error = open_ref_view(view, repo);
6386 /* Release work tree lock. */
6387 got_worktree_close(worktree);
6390 error = view_loop(view);
6395 const struct got_error *close_err = got_repo_close(repo);
6404 list_commands(FILE *fp)
6408 fprintf(fp, "commands:");
6409 for (i = 0; i < nitems(tog_commands); i++) {
6410 struct tog_cmd *cmd = &tog_commands[i];
6411 fprintf(fp, " %s", cmd->name);
6417 usage(int hflag, int status)
6419 FILE *fp = (status == 0) ? stdout : stderr;
6421 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6424 fprintf(fp, "lazy usage: %s path\n", getprogname());
6431 make_argv(int argc, ...)
6439 argv = calloc(argc, sizeof(char *));
6442 for (i = 0; i < argc; i++) {
6443 argv[i] = strdup(va_arg(ap, char *));
6444 if (argv[i] == NULL)
6453 * Try to convert 'tog path' into a 'tog log path' command.
6454 * The user could simply have mistyped the command rather than knowingly
6455 * provided a path. So check whether argv[0] can in fact be resolved
6456 * to a path in the HEAD commit and print a special error if not.
6457 * This hack is for mpi@ <3
6459 static const struct got_error *
6460 tog_log_with_path(int argc, char *argv[])
6462 const struct got_error *error = NULL, *close_err;
6463 struct tog_cmd *cmd = NULL;
6464 struct got_repository *repo = NULL;
6465 struct got_worktree *worktree = NULL;
6466 struct got_object_id *commit_id = NULL, *id = NULL;
6467 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6468 char *commit_id_str = NULL, **cmd_argv = NULL;
6470 cwd = getcwd(NULL, 0);
6472 return got_error_from_errno("getcwd");
6474 error = got_worktree_open(&worktree, cwd);
6475 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6479 repo_path = strdup(got_worktree_get_repo_path(worktree));
6481 repo_path = strdup(cwd);
6482 if (repo_path == NULL) {
6483 error = got_error_from_errno("strdup");
6487 error = got_repo_open(&repo, repo_path, NULL);
6491 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6496 error = tog_load_refs(repo);
6499 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6500 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6501 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6506 got_worktree_close(worktree);
6510 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6512 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6514 fprintf(stderr, "%s: '%s' is no known command or path\n",
6515 getprogname(), argv[0]);
6520 close_err = got_repo_close(repo);
6525 error = got_object_id_str(&commit_id_str, commit_id);
6529 cmd = &tog_commands[0]; /* log */
6531 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6532 error = cmd->cmd_main(argc, cmd_argv);
6535 close_err = got_repo_close(repo);
6540 got_worktree_close(worktree);
6542 free(commit_id_str);
6549 for (i = 0; i < argc; i++)
6558 main(int argc, char *argv[])
6560 const struct got_error *error = NULL;
6561 struct tog_cmd *cmd = NULL;
6562 int ch, hflag = 0, Vflag = 0;
6563 char **cmd_argv = NULL;
6564 static struct option longopts[] = {
6565 { "version", no_argument, NULL, 'V' },
6569 setlocale(LC_CTYPE, "");
6571 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6591 got_version_print_str();
6596 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6604 /* Build an argument vector which runs a default command. */
6605 cmd = &tog_commands[0];
6607 cmd_argv = make_argv(argc, cmd->name);
6611 /* Did the user specify a command? */
6612 for (i = 0; i < nitems(tog_commands); i++) {
6613 if (strncmp(tog_commands[i].name, argv[0],
6614 strlen(argv[0])) == 0) {
6615 cmd = &tog_commands[i];
6624 /* No command specified; try log with a path */
6625 error = tog_log_with_path(argc, argv);
6630 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6637 for (i = 0; i < argc; i++)
6642 if (error && error->code != GOT_ERR_CANCELLED)
6643 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);