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 SIMPLEQ_ENTRY(tog_color) entry;
126 SIMPLEQ_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 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
189 free_colors(struct tog_colors *colors)
191 struct tog_color *tc;
193 while (!SIMPLEQ_EMPTY(colors)) {
194 tc = SIMPLEQ_FIRST(colors);
195 SIMPLEQ_REMOVE_HEAD(colors, entry);
202 get_color(struct tog_colors *colors, int colorpair)
204 struct tog_color *tc = NULL;
206 SIMPLEQ_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;
307 struct got_commit_graph *graph;
308 struct commit_queue *commits;
309 const char *in_repo_path;
310 struct got_object_id *start_id;
311 struct got_repository *repo;
314 struct commit_queue_entry **first_displayed_entry;
315 struct commit_queue_entry **selected_entry;
317 int *search_next_done;
321 struct tog_log_view_state {
322 struct commit_queue commits;
323 struct commit_queue_entry *first_displayed_entry;
324 struct commit_queue_entry *last_displayed_entry;
325 struct commit_queue_entry *selected_entry;
330 struct got_repository *repo;
331 struct got_object_id *start_id;
334 struct tog_log_thread_args thread_args;
335 struct commit_queue_entry *matched_entry;
336 struct commit_queue_entry *search_entry;
337 struct tog_colors colors;
340 #define TOG_COLOR_DIFF_MINUS 1
341 #define TOG_COLOR_DIFF_PLUS 2
342 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
343 #define TOG_COLOR_DIFF_META 4
344 #define TOG_COLOR_TREE_SUBMODULE 5
345 #define TOG_COLOR_TREE_SYMLINK 6
346 #define TOG_COLOR_TREE_DIRECTORY 7
347 #define TOG_COLOR_TREE_EXECUTABLE 8
348 #define TOG_COLOR_COMMIT 9
349 #define TOG_COLOR_AUTHOR 10
350 #define TOG_COLOR_DATE 11
351 #define TOG_COLOR_REFS_HEADS 12
352 #define TOG_COLOR_REFS_TAGS 13
353 #define TOG_COLOR_REFS_REMOTES 14
355 struct tog_blame_cb_args {
356 struct tog_blame_line *lines; /* one per line */
359 struct tog_view *view;
360 struct got_object_id *commit_id;
364 struct tog_blame_thread_args {
366 struct got_repository *repo;
367 struct tog_blame_cb_args *cb_args;
369 got_cancel_cb cancel_cb;
376 struct tog_blame_line *lines;
380 struct tog_blame_thread_args thread_args;
381 struct tog_blame_cb_args cb_args;
385 struct tog_blame_view_state {
386 int first_displayed_line;
387 int last_displayed_line;
392 struct got_object_id_queue blamed_commits;
393 struct got_object_qid *blamed_commit;
395 struct got_repository *repo;
396 struct got_object_id *commit_id;
397 struct tog_blame blame;
399 struct tog_colors colors;
402 struct tog_parent_tree {
403 TAILQ_ENTRY(tog_parent_tree) entry;
404 struct got_tree_object *tree;
405 struct got_tree_entry *first_displayed_entry;
406 struct got_tree_entry *selected_entry;
410 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
412 struct tog_tree_view_state {
414 struct got_tree_object *root;
415 struct got_tree_object *tree;
416 struct got_tree_entry *first_displayed_entry;
417 struct got_tree_entry *last_displayed_entry;
418 struct got_tree_entry *selected_entry;
419 int ndisplayed, selected, show_ids;
420 struct tog_parent_trees parents;
421 struct got_object_id *commit_id;
423 struct got_repository *repo;
424 struct got_tree_entry *matched_entry;
425 struct tog_colors colors;
428 struct tog_reflist_entry {
429 TAILQ_ENTRY(tog_reflist_entry) entry;
430 struct got_reference *ref;
434 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
436 struct tog_ref_view_state {
437 struct tog_reflist_head refs;
438 struct tog_reflist_entry *first_displayed_entry;
439 struct tog_reflist_entry *last_displayed_entry;
440 struct tog_reflist_entry *selected_entry;
441 int nrefs, ndisplayed, selected, show_ids;
442 struct got_repository *repo;
443 struct tog_reflist_entry *matched_entry;
444 struct tog_colors colors;
448 * We implement two types of views: parent views and child views.
450 * The 'Tab' key switches focus between a parent view and its child view.
451 * Child views are shown side-by-side to their parent view, provided
452 * there is enough screen estate.
454 * When a new view is opened from within a parent view, this new view
455 * becomes a child view of the parent view, replacing any existing child.
457 * When a new view is opened from within a child view, this new view
458 * becomes a parent view which will obscure the views below until the
459 * user quits the new parent view by typing 'q'.
461 * This list of views contains parent views only.
462 * Child views are only pointed to by their parent view.
464 TAILQ_HEAD(tog_view_list_head, tog_view);
467 TAILQ_ENTRY(tog_view) entry;
470 int nlines, ncols, begin_y, begin_x;
471 int lines, cols; /* copies of LINES and COLS */
472 int focussed; /* Only set on one parent or child view at a time. */
474 struct tog_view *parent;
475 struct tog_view *child;
478 * This flag is initially set on parent views when a new child view
479 * is created. It gets toggled when the 'Tab' key switches focus
480 * between parent and child.
481 * The flag indicates whether focus should be passed on to our child
482 * view if this parent view gets picked for focus after another parent
483 * view was closed. This prevents child views from losing focus in such
488 /* type-specific state */
489 enum tog_view_type type;
491 struct tog_diff_view_state diff;
492 struct tog_log_view_state log;
493 struct tog_blame_view_state blame;
494 struct tog_tree_view_state tree;
495 struct tog_ref_view_state ref;
498 const struct got_error *(*show)(struct tog_view *);
499 const struct got_error *(*input)(struct tog_view **,
500 struct tog_view *, int);
501 const struct got_error *(*close)(struct tog_view *);
503 const struct got_error *(*search_start)(struct tog_view *);
504 const struct got_error *(*search_next)(struct tog_view *);
507 #define TOG_SEARCH_FORWARD 1
508 #define TOG_SEARCH_BACKWARD 2
509 int search_next_done;
510 #define TOG_SEARCH_HAVE_MORE 1
511 #define TOG_SEARCH_NO_MORE 2
512 #define TOG_SEARCH_HAVE_NONE 3
517 static const struct got_error *open_diff_view(struct tog_view *,
518 struct got_object_id *, struct got_object_id *,
519 const char *, const char *, int, int, int, struct tog_view *,
520 struct got_repository *);
521 static const struct got_error *show_diff_view(struct tog_view *);
522 static const struct got_error *input_diff_view(struct tog_view **,
523 struct tog_view *, int);
524 static const struct got_error* close_diff_view(struct tog_view *);
525 static const struct got_error *search_start_diff_view(struct tog_view *);
526 static const struct got_error *search_next_diff_view(struct tog_view *);
528 static const struct got_error *open_log_view(struct tog_view *,
529 struct got_object_id *, struct got_repository *,
530 const char *, const char *, int);
531 static const struct got_error * show_log_view(struct tog_view *);
532 static const struct got_error *input_log_view(struct tog_view **,
533 struct tog_view *, int);
534 static const struct got_error *close_log_view(struct tog_view *);
535 static const struct got_error *search_start_log_view(struct tog_view *);
536 static const struct got_error *search_next_log_view(struct tog_view *);
538 static const struct got_error *open_blame_view(struct tog_view *, char *,
539 struct got_object_id *, struct got_repository *);
540 static const struct got_error *show_blame_view(struct tog_view *);
541 static const struct got_error *input_blame_view(struct tog_view **,
542 struct tog_view *, int);
543 static const struct got_error *close_blame_view(struct tog_view *);
544 static const struct got_error *search_start_blame_view(struct tog_view *);
545 static const struct got_error *search_next_blame_view(struct tog_view *);
547 static const struct got_error *open_tree_view(struct tog_view *,
548 struct got_tree_object *, struct got_object_id *, const char *,
549 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,
894 view->child->focussed = 1;
895 view->focus_child = 1;
896 } else if (view->parent) {
898 view->parent->focussed = 1;
899 view->parent->focus_child = 0;
903 err = view->input(new, view, ch);
910 if (view_is_parent_view(view)) {
911 if (view->child == NULL)
913 if (view_is_splitscreen(view->child)) {
915 view->child->focussed = 1;
916 err = view_fullscreen(view->child);
918 err = view_splitscreen(view->child);
921 err = view->child->input(new, view->child,
924 if (view_is_splitscreen(view)) {
925 view->parent->focussed = 0;
927 err = view_fullscreen(view);
929 err = view_splitscreen(view);
933 err = view->input(new, view, KEY_RESIZE);
939 if (view->search_start)
940 view_search_start(view);
942 err = view->input(new, view, ch);
946 if (view->search_started && view->search_next) {
947 view->searching = (ch == 'n' ?
948 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
949 view->search_next_done = 0;
950 view->search_next(view);
952 err = view->input(new, view, ch);
955 err = view->input(new, view, ch);
963 view_vborder(struct tog_view *view)
966 const struct tog_view *view_above;
969 return view_vborder(view->parent);
971 panel = panel_above(view->panel);
975 view_above = panel_userptr(panel);
976 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
977 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
981 view_needs_focus_indication(struct tog_view *view)
983 if (view_is_parent_view(view)) {
984 if (view->child == NULL || view->child->focussed)
986 if (!view_is_splitscreen(view->child))
988 } else if (!view_is_splitscreen(view))
991 return view->focussed;
994 static const struct got_error *
995 view_loop(struct tog_view *view)
997 const struct got_error *err = NULL;
998 struct tog_view_list_head views;
999 struct tog_view *new_view;
1000 int fast_refresh = 10;
1001 int done = 0, errcode;
1003 errcode = pthread_mutex_lock(&tog_mutex);
1005 return got_error_set_errno(errcode, "pthread_mutex_lock");
1008 TAILQ_INSERT_HEAD(&views, view, entry);
1011 err = view->show(view);
1016 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1017 /* Refresh fast during initialization, then become slower. */
1018 if (fast_refresh && fast_refresh-- == 0)
1019 halfdelay(10); /* switch to once per second */
1021 err = view_input(&new_view, &done, view, &views);
1025 struct tog_view *v, *prev = NULL;
1027 if (view_is_parent_view(view))
1028 prev = TAILQ_PREV(view, tog_view_list_head,
1030 else if (view->parent)
1031 prev = view->parent;
1034 view->parent->child = NULL;
1035 view->parent->focus_child = 0;
1037 TAILQ_REMOVE(&views, view, entry);
1039 err = view_close(view);
1044 TAILQ_FOREACH(v, &views, entry) {
1048 if (view == NULL && new_view == NULL) {
1049 /* No view has focus. Try to pick one. */
1052 else if (!TAILQ_EMPTY(&views)) {
1053 view = TAILQ_LAST(&views,
1054 tog_view_list_head);
1057 if (view->focus_child) {
1058 view->child->focussed = 1;
1066 struct tog_view *v, *t;
1067 /* Only allow one parent view per type. */
1068 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1069 if (v->type != new_view->type)
1071 TAILQ_REMOVE(&views, v, entry);
1072 err = view_close(v);
1077 TAILQ_INSERT_TAIL(&views, new_view, entry);
1081 if (view_is_parent_view(view)) {
1082 if (view->child && view->child->focussed)
1085 if (view->parent && view->parent->focussed)
1086 view = view->parent;
1088 show_panel(view->panel);
1089 if (view->child && view_is_splitscreen(view->child))
1090 show_panel(view->child->panel);
1091 if (view->parent && view_is_splitscreen(view)) {
1092 err = view->parent->show(view->parent);
1096 err = view->show(view);
1100 err = view->child->show(view->child);
1109 while (!TAILQ_EMPTY(&views)) {
1110 view = TAILQ_FIRST(&views);
1111 TAILQ_REMOVE(&views, view, entry);
1115 errcode = pthread_mutex_unlock(&tog_mutex);
1116 if (errcode && err == NULL)
1117 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1127 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1132 /* Create newly allocated wide-character string equivalent to a byte string. */
1133 static const struct got_error *
1134 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1137 const struct got_error *err = NULL;
1140 *wlen = mbstowcs(NULL, s, 0);
1141 if (*wlen == (size_t)-1) {
1143 if (errno != EILSEQ)
1144 return got_error_from_errno("mbstowcs");
1146 /* byte string invalid in current encoding; try to "fix" it */
1147 err = got_mbsavis(&vis, &vislen, s);
1150 *wlen = mbstowcs(NULL, vis, 0);
1151 if (*wlen == (size_t)-1) {
1152 err = got_error_from_errno("mbstowcs"); /* give up */
1157 *ws = calloc(*wlen + 1, sizeof(**ws));
1159 err = got_error_from_errno("calloc");
1163 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1164 err = got_error_from_errno("mbstowcs");
1175 /* Format a line for display, ensuring that it won't overflow a width limit. */
1176 static const struct got_error *
1177 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1180 const struct got_error *err = NULL;
1182 wchar_t *wline = NULL;
1189 err = mbs2ws(&wline, &wlen, line);
1193 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1194 wline[wlen - 1] = L'\0';
1197 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1198 wline[wlen - 1] = L'\0';
1204 int width = wcwidth(wline[i]);
1211 if (width == 1 || width == 2) {
1212 if (cols + width > wlimit)
1216 } else if (width == -1) {
1217 if (wline[i] == L'\t') {
1219 ((cols + col_tab_align) % TABSIZE);
1224 if (cols + width > wlimit)
1229 err = got_error_from_errno("wcwidth");
1244 static const struct got_error*
1245 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1246 struct got_object_id *id, struct got_repository *repo)
1248 static const struct got_error *err = NULL;
1249 struct got_reflist_entry *re;
1255 TAILQ_FOREACH(re, refs, entry) {
1256 struct got_tag_object *tag = NULL;
1257 struct got_object_id *ref_id;
1260 name = got_ref_get_name(re->ref);
1261 if (strcmp(name, GOT_REF_HEAD) == 0)
1263 if (strncmp(name, "refs/", 5) == 0)
1265 if (strncmp(name, "got/", 4) == 0)
1267 if (strncmp(name, "heads/", 6) == 0)
1269 if (strncmp(name, "remotes/", 8) == 0) {
1271 s = strstr(name, "/" GOT_REF_HEAD);
1272 if (s != NULL && s[strlen(s)] == '\0')
1275 err = got_ref_resolve(&ref_id, repo, re->ref);
1278 if (strncmp(name, "tags/", 5) == 0) {
1279 err = got_object_open_as_tag(&tag, repo, ref_id);
1281 if (err->code != GOT_ERR_OBJ_TYPE) {
1285 /* Ref points at something other than a tag. */
1290 cmp = got_object_id_cmp(tag ?
1291 got_object_tag_get_object_id(tag) : ref_id, id);
1294 got_object_tag_close(tag);
1298 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1299 s ? ", " : "", name) == -1) {
1300 err = got_error_from_errno("asprintf");
1311 static const struct got_error *
1312 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1317 smallerthan = strchr(author, '<');
1318 if (smallerthan && smallerthan[1] != '\0')
1319 author = smallerthan + 1;
1320 author[strcspn(author, "@>")] = '\0';
1321 return format_line(wauthor, author_width, author, limit, col_tab_align);
1324 static const struct got_error *
1325 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1326 struct got_object_id *id, const size_t date_display_cols,
1327 int author_display_cols)
1329 struct tog_log_view_state *s = &view->state.log;
1330 const struct got_error *err = NULL;
1331 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1332 char *logmsg0 = NULL, *logmsg = NULL;
1333 char *author = NULL;
1334 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1335 int author_width, logmsg_width;
1336 char *newline, *line = NULL;
1338 const int avail = view->ncols;
1340 time_t committer_time;
1341 struct tog_color *tc;
1343 committer_time = got_object_commit_get_committer_time(commit);
1344 if (localtime_r(&committer_time, &tm) == NULL)
1345 return got_error_from_errno("localtime_r");
1346 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1348 return got_error(GOT_ERR_NO_SPACE);
1350 if (avail <= date_display_cols)
1351 limit = MIN(sizeof(datebuf) - 1, avail);
1353 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1354 tc = get_color(&s->colors, TOG_COLOR_DATE);
1356 wattr_on(view->window,
1357 COLOR_PAIR(tc->colorpair), NULL);
1358 waddnstr(view->window, datebuf, limit);
1360 wattr_off(view->window,
1361 COLOR_PAIR(tc->colorpair), NULL);
1368 err = got_object_id_str(&id_str, id);
1371 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1373 wattr_on(view->window,
1374 COLOR_PAIR(tc->colorpair), NULL);
1375 wprintw(view->window, "%.8s ", id_str);
1377 wattr_off(view->window,
1378 COLOR_PAIR(tc->colorpair), NULL);
1385 author = strdup(got_object_commit_get_author(commit));
1386 if (author == NULL) {
1387 err = got_error_from_errno("strdup");
1390 err = format_author(&wauthor, &author_width, author, avail - col, col);
1393 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1395 wattr_on(view->window,
1396 COLOR_PAIR(tc->colorpair), NULL);
1397 waddwstr(view->window, wauthor);
1399 wattr_off(view->window,
1400 COLOR_PAIR(tc->colorpair), NULL);
1401 col += author_width;
1402 while (col < avail && author_width < author_display_cols + 2) {
1403 waddch(view->window, ' ');
1410 err = got_object_commit_get_logmsg(&logmsg0, commit);
1414 while (*logmsg == '\n')
1416 newline = strchr(logmsg, '\n');
1419 limit = avail - col;
1420 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1423 waddwstr(view->window, wlogmsg);
1424 col += logmsg_width;
1425 while (col < avail) {
1426 waddch(view->window, ' ');
1438 static struct commit_queue_entry *
1439 alloc_commit_queue_entry(struct got_commit_object *commit,
1440 struct got_object_id *id)
1442 struct commit_queue_entry *entry;
1444 entry = calloc(1, sizeof(*entry));
1449 entry->commit = commit;
1454 pop_commit(struct commit_queue *commits)
1456 struct commit_queue_entry *entry;
1458 entry = TAILQ_FIRST(&commits->head);
1459 TAILQ_REMOVE(&commits->head, entry, entry);
1460 got_object_commit_close(entry->commit);
1461 commits->ncommits--;
1462 /* Don't free entry->id! It is owned by the commit graph. */
1467 free_commits(struct commit_queue *commits)
1469 while (!TAILQ_EMPTY(&commits->head))
1470 pop_commit(commits);
1473 static const struct got_error *
1474 match_commit(int *have_match, struct got_object_id *id,
1475 struct got_commit_object *commit, regex_t *regex)
1477 const struct got_error *err = NULL;
1478 regmatch_t regmatch;
1479 char *id_str = NULL, *logmsg = NULL;
1483 err = got_object_id_str(&id_str, id);
1487 err = got_object_commit_get_logmsg(&logmsg, commit);
1491 if (regexec(regex, got_object_commit_get_author(commit), 1,
1492 ®match, 0) == 0 ||
1493 regexec(regex, got_object_commit_get_committer(commit), 1,
1494 ®match, 0) == 0 ||
1495 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1496 regexec(regex, logmsg, 1, ®match, 0) == 0)
1504 static const struct got_error *
1505 queue_commits(struct tog_log_thread_args *a)
1507 const struct got_error *err = NULL;
1510 * We keep all commits open throughout the lifetime of the log
1511 * view in order to avoid having to re-fetch commits from disk
1512 * while updating the display.
1515 struct got_object_id *id;
1516 struct got_commit_object *commit;
1517 struct commit_queue_entry *entry;
1520 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1522 if (err || id == NULL)
1525 err = got_object_open_as_commit(&commit, a->repo, id);
1528 entry = alloc_commit_queue_entry(commit, id);
1529 if (entry == NULL) {
1530 err = got_error_from_errno("alloc_commit_queue_entry");
1534 errcode = pthread_mutex_lock(&tog_mutex);
1536 err = got_error_set_errno(errcode,
1537 "pthread_mutex_lock");
1541 entry->idx = a->commits->ncommits;
1542 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1543 a->commits->ncommits++;
1545 if (*a->searching == TOG_SEARCH_FORWARD &&
1546 !*a->search_next_done) {
1548 err = match_commit(&have_match, id, commit, a->regex);
1552 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1555 errcode = pthread_mutex_unlock(&tog_mutex);
1556 if (errcode && err == NULL)
1557 err = got_error_set_errno(errcode,
1558 "pthread_mutex_unlock");
1561 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1567 select_commit(struct tog_log_view_state *s)
1569 struct commit_queue_entry *entry;
1572 entry = s->first_displayed_entry;
1574 if (ncommits == s->selected) {
1575 s->selected_entry = entry;
1578 entry = TAILQ_NEXT(entry, entry);
1583 static const struct got_error *
1584 draw_commits(struct tog_view *view)
1586 const struct got_error *err = NULL;
1587 struct tog_log_view_state *s = &view->state.log;
1588 struct commit_queue_entry *entry = s->selected_entry;
1589 const int limit = view->nlines;
1591 int ncommits, author_cols = 4;
1592 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1593 char *refs_str = NULL;
1595 struct tog_color *tc;
1596 static const size_t date_display_cols = 12;
1598 if (s->selected_entry &&
1599 !(view->searching && view->search_next_done == 0)) {
1600 struct got_reflist_head *refs;
1601 err = got_object_id_str(&id_str, s->selected_entry->id);
1604 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1605 s->selected_entry->id);
1607 err = build_refs_str(&refs_str, refs,
1608 s->selected_entry->id, s->repo);
1614 if (s->thread_args.commits_needed == 0)
1615 halfdelay(10); /* disable fast refresh */
1617 if (s->thread_args.commits_needed > 0) {
1618 if (asprintf(&ncommits_str, " [%d/%d] %s",
1619 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1620 (view->searching && !view->search_next_done) ?
1621 "searching..." : "loading...") == -1) {
1622 err = got_error_from_errno("asprintf");
1626 const char *search_str = NULL;
1628 if (view->searching) {
1629 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1630 search_str = "no more matches";
1631 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1632 search_str = "no matches found";
1633 else if (!view->search_next_done)
1634 search_str = "searching...";
1637 if (asprintf(&ncommits_str, " [%d/%d] %s",
1638 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1639 search_str ? search_str :
1640 (refs_str ? refs_str : "")) == -1) {
1641 err = got_error_from_errno("asprintf");
1646 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1647 if (asprintf(&header, "commit %s %s%s",
1648 id_str ? id_str : "........................................",
1649 s->in_repo_path, ncommits_str) == -1) {
1650 err = got_error_from_errno("asprintf");
1654 } else if (asprintf(&header, "commit %s%s",
1655 id_str ? id_str : "........................................",
1656 ncommits_str) == -1) {
1657 err = got_error_from_errno("asprintf");
1661 err = format_line(&wline, &width, header, view->ncols, 0);
1665 werase(view->window);
1667 if (view_needs_focus_indication(view))
1668 wstandout(view->window);
1669 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1671 wattr_on(view->window,
1672 COLOR_PAIR(tc->colorpair), NULL);
1673 waddwstr(view->window, wline);
1675 wattr_off(view->window,
1676 COLOR_PAIR(tc->colorpair), NULL);
1677 while (width < view->ncols) {
1678 waddch(view->window, ' ');
1681 if (view_needs_focus_indication(view))
1682 wstandend(view->window);
1687 /* Grow author column size if necessary. */
1688 entry = s->first_displayed_entry;
1694 if (ncommits >= limit - 1)
1696 author = strdup(got_object_commit_get_author(entry->commit));
1697 if (author == NULL) {
1698 err = got_error_from_errno("strdup");
1701 err = format_author(&wauthor, &width, author, COLS,
1703 if (author_cols < width)
1704 author_cols = width;
1708 entry = TAILQ_NEXT(entry, entry);
1711 entry = s->first_displayed_entry;
1712 s->last_displayed_entry = s->first_displayed_entry;
1715 if (ncommits >= limit - 1)
1717 if (ncommits == s->selected)
1718 wstandout(view->window);
1719 err = draw_commit(view, entry->commit, entry->id,
1720 date_display_cols, author_cols);
1721 if (ncommits == s->selected)
1722 wstandend(view->window);
1726 s->last_displayed_entry = entry;
1727 entry = TAILQ_NEXT(entry, entry);
1740 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1742 struct commit_queue_entry *entry;
1745 entry = TAILQ_FIRST(&s->commits.head);
1746 if (s->first_displayed_entry == entry)
1749 entry = s->first_displayed_entry;
1750 while (entry && nscrolled < maxscroll) {
1751 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1753 s->first_displayed_entry = entry;
1759 static const struct got_error *
1760 trigger_log_thread(struct tog_view *view, int wait)
1762 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1765 halfdelay(1); /* fast refresh while loading commits */
1767 while (ta->commits_needed > 0) {
1768 if (ta->log_complete)
1771 /* Wake the log thread. */
1772 errcode = pthread_cond_signal(&ta->need_commits);
1774 return got_error_set_errno(errcode,
1775 "pthread_cond_signal");
1778 * The mutex will be released while the view loop waits
1779 * in wgetch(), at which time the log thread will run.
1784 /* Display progress update in log view. */
1785 show_log_view(view);
1789 /* Wait right here while next commit is being loaded. */
1790 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1792 return got_error_set_errno(errcode,
1793 "pthread_cond_wait");
1795 /* Display progress update in log view. */
1796 show_log_view(view);
1804 static const struct got_error *
1805 log_scroll_down(struct tog_view *view, int maxscroll)
1807 struct tog_log_view_state *s = &view->state.log;
1808 const struct got_error *err = NULL;
1809 struct commit_queue_entry *pentry;
1810 int nscrolled = 0, ncommits_needed;
1812 if (s->last_displayed_entry == NULL)
1815 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1816 if (s->commits.ncommits < ncommits_needed &&
1817 !s->thread_args.log_complete) {
1819 * Ask the log thread for required amount of commits.
1821 s->thread_args.commits_needed += maxscroll;
1822 err = trigger_log_thread(view, 1);
1828 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1832 s->last_displayed_entry = pentry;
1834 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1837 s->first_displayed_entry = pentry;
1838 } while (++nscrolled < maxscroll);
1843 static const struct got_error *
1844 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1845 struct got_commit_object *commit, struct got_object_id *commit_id,
1846 struct tog_view *log_view, struct got_repository *repo)
1848 const struct got_error *err;
1849 struct got_object_qid *parent_id;
1850 struct tog_view *diff_view;
1852 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1853 if (diff_view == NULL)
1854 return got_error_from_errno("view_open");
1856 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1857 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1858 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1860 *new_view = diff_view;
1864 static const struct got_error *
1865 tree_view_visit_subtree(struct tog_tree_view_state *s,
1866 struct got_tree_object *subtree)
1868 struct tog_parent_tree *parent;
1870 parent = calloc(1, sizeof(*parent));
1872 return got_error_from_errno("calloc");
1874 parent->tree = s->tree;
1875 parent->first_displayed_entry = s->first_displayed_entry;
1876 parent->selected_entry = s->selected_entry;
1877 parent->selected = s->selected;
1878 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1881 s->first_displayed_entry = NULL;
1885 static const struct got_error *
1886 tree_view_walk_path(struct tog_tree_view_state *s,
1887 struct got_object_id *commit_id, const char *path)
1889 const struct got_error *err = NULL;
1890 struct got_tree_object *tree = NULL;
1892 char *slash, *subpath = NULL;
1894 /* Walk the path and open corresponding tree objects. */
1897 struct got_tree_entry *te;
1898 struct got_object_id *tree_id;
1904 /* Ensure the correct subtree entry is selected. */
1905 slash = strchr(p, '/');
1907 te_name = strdup(p);
1909 te_name = strndup(p, slash - p);
1910 if (te_name == NULL) {
1911 err = got_error_from_errno("strndup");
1914 te = got_object_tree_find_entry(s->tree, te_name);
1916 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1921 s->first_displayed_entry = s->selected_entry = te;
1923 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1924 break; /* jump to this file's entry */
1926 slash = strchr(p, '/');
1928 subpath = strndup(path, slash - path);
1930 subpath = strdup(path);
1931 if (subpath == NULL) {
1932 err = got_error_from_errno("strdup");
1936 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1941 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1946 err = tree_view_visit_subtree(s, tree);
1948 got_object_tree_close(tree);
1962 static const struct got_error *
1963 browse_commit_tree(struct tog_view **new_view, int begin_x,
1964 struct commit_queue_entry *entry, const char *path,
1965 const char *head_ref_name, struct got_repository *repo)
1967 const struct got_error *err = NULL;
1968 struct got_tree_object *tree;
1969 struct tog_tree_view_state *s;
1970 struct tog_view *tree_view;
1972 err = got_object_open_as_tree(&tree, repo,
1973 got_object_commit_get_tree_id(entry->commit));
1977 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1978 if (tree_view == NULL)
1979 return got_error_from_errno("view_open");
1981 err = open_tree_view(tree_view, tree, entry->id, head_ref_name, repo);
1983 got_object_tree_close(tree);
1986 s = &tree_view->state.tree;
1988 *new_view = tree_view;
1990 if (got_path_is_root_dir(path))
1993 return tree_view_walk_path(s, entry->id, path);
1996 static const struct got_error *
1997 block_signals_used_by_main_thread(void)
2002 if (sigemptyset(&sigset) == -1)
2003 return got_error_from_errno("sigemptyset");
2005 /* tog handles SIGWINCH and SIGCONT */
2006 if (sigaddset(&sigset, SIGWINCH) == -1)
2007 return got_error_from_errno("sigaddset");
2008 if (sigaddset(&sigset, SIGCONT) == -1)
2009 return got_error_from_errno("sigaddset");
2011 /* ncurses handles SIGTSTP */
2012 if (sigaddset(&sigset, SIGTSTP) == -1)
2013 return got_error_from_errno("sigaddset");
2015 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2017 return got_error_set_errno(errcode, "pthread_sigmask");
2023 log_thread(void *arg)
2025 const struct got_error *err = NULL;
2027 struct tog_log_thread_args *a = arg;
2030 err = block_signals_used_by_main_thread();
2034 while (!done && !err && !tog_sigpipe_received) {
2035 err = queue_commits(a);
2037 if (err->code != GOT_ERR_ITER_COMPLETED)
2041 } else if (a->commits_needed > 0)
2042 a->commits_needed--;
2044 errcode = pthread_mutex_lock(&tog_mutex);
2046 err = got_error_set_errno(errcode,
2047 "pthread_mutex_lock");
2049 } else if (*a->quit)
2051 else if (*a->first_displayed_entry == NULL) {
2052 *a->first_displayed_entry =
2053 TAILQ_FIRST(&a->commits->head);
2054 *a->selected_entry = *a->first_displayed_entry;
2057 errcode = pthread_cond_signal(&a->commit_loaded);
2059 err = got_error_set_errno(errcode,
2060 "pthread_cond_signal");
2061 pthread_mutex_unlock(&tog_mutex);
2066 a->commits_needed = 0;
2068 if (a->commits_needed == 0) {
2069 errcode = pthread_cond_wait(&a->need_commits,
2072 err = got_error_set_errno(errcode,
2073 "pthread_cond_wait");
2079 errcode = pthread_mutex_unlock(&tog_mutex);
2080 if (errcode && err == NULL)
2081 err = got_error_set_errno(errcode,
2082 "pthread_mutex_unlock");
2084 a->log_complete = 1;
2088 static const struct got_error *
2089 stop_log_thread(struct tog_log_view_state *s)
2091 const struct got_error *err = NULL;
2096 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2098 return got_error_set_errno(errcode,
2099 "pthread_cond_signal");
2100 errcode = pthread_mutex_unlock(&tog_mutex);
2102 return got_error_set_errno(errcode,
2103 "pthread_mutex_unlock");
2104 errcode = pthread_join(s->thread, (void **)&err);
2106 return got_error_set_errno(errcode, "pthread_join");
2107 errcode = pthread_mutex_lock(&tog_mutex);
2109 return got_error_set_errno(errcode,
2110 "pthread_mutex_lock");
2114 if (s->thread_args.repo) {
2115 got_repo_close(s->thread_args.repo);
2116 s->thread_args.repo = NULL;
2119 if (s->thread_args.graph) {
2120 got_commit_graph_close(s->thread_args.graph);
2121 s->thread_args.graph = NULL;
2127 static const struct got_error *
2128 close_log_view(struct tog_view *view)
2130 const struct got_error *err = NULL;
2131 struct tog_log_view_state *s = &view->state.log;
2134 err = stop_log_thread(s);
2136 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2137 if (errcode && err == NULL)
2138 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2140 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2141 if (errcode && err == NULL)
2142 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2144 free_commits(&s->commits);
2145 free(s->in_repo_path);
2146 s->in_repo_path = NULL;
2149 free(s->head_ref_name);
2150 s->head_ref_name = NULL;
2154 static const struct got_error *
2155 search_start_log_view(struct tog_view *view)
2157 struct tog_log_view_state *s = &view->state.log;
2159 s->matched_entry = NULL;
2160 s->search_entry = NULL;
2164 static const struct got_error *
2165 search_next_log_view(struct tog_view *view)
2167 const struct got_error *err = NULL;
2168 struct tog_log_view_state *s = &view->state.log;
2169 struct commit_queue_entry *entry;
2171 /* Display progress update in log view. */
2172 show_log_view(view);
2176 if (s->search_entry) {
2178 errcode = pthread_mutex_unlock(&tog_mutex);
2180 return got_error_set_errno(errcode,
2181 "pthread_mutex_unlock");
2182 ch = wgetch(view->window);
2183 errcode = pthread_mutex_lock(&tog_mutex);
2185 return got_error_set_errno(errcode,
2186 "pthread_mutex_lock");
2187 if (ch == KEY_BACKSPACE) {
2188 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2191 if (view->searching == TOG_SEARCH_FORWARD)
2192 entry = TAILQ_NEXT(s->search_entry, entry);
2194 entry = TAILQ_PREV(s->search_entry,
2195 commit_queue_head, entry);
2196 } else if (s->matched_entry) {
2197 if (view->searching == TOG_SEARCH_FORWARD)
2198 entry = TAILQ_NEXT(s->matched_entry, entry);
2200 entry = TAILQ_PREV(s->matched_entry,
2201 commit_queue_head, entry);
2203 if (view->searching == TOG_SEARCH_FORWARD)
2204 entry = TAILQ_FIRST(&s->commits.head);
2206 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2212 if (entry == NULL) {
2213 if (s->thread_args.log_complete ||
2214 view->searching == TOG_SEARCH_BACKWARD) {
2215 view->search_next_done =
2216 (s->matched_entry == NULL ?
2217 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2218 s->search_entry = NULL;
2222 * Poke the log thread for more commits and return,
2223 * allowing the main loop to make progress. Search
2224 * will resume at s->search_entry once we come back.
2226 s->thread_args.commits_needed++;
2227 return trigger_log_thread(view, 0);
2230 err = match_commit(&have_match, entry->id, entry->commit,
2235 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2236 s->matched_entry = entry;
2240 s->search_entry = entry;
2241 if (view->searching == TOG_SEARCH_FORWARD)
2242 entry = TAILQ_NEXT(entry, entry);
2244 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2247 if (s->matched_entry) {
2248 int cur = s->selected_entry->idx;
2249 while (cur < s->matched_entry->idx) {
2250 err = input_log_view(NULL, view, KEY_DOWN);
2255 while (cur > s->matched_entry->idx) {
2256 err = input_log_view(NULL, view, KEY_UP);
2263 s->search_entry = NULL;
2268 static const struct got_error *
2269 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2270 struct got_repository *repo, const char *head_ref_name,
2271 const char *in_repo_path, int log_branches)
2273 const struct got_error *err = NULL;
2274 struct tog_log_view_state *s = &view->state.log;
2275 struct got_repository *thread_repo = NULL;
2276 struct got_commit_graph *thread_graph = NULL;
2279 if (in_repo_path != s->in_repo_path) {
2280 free(s->in_repo_path);
2281 s->in_repo_path = strdup(in_repo_path);
2282 if (s->in_repo_path == NULL)
2283 return got_error_from_errno("strdup");
2286 /* The commit queue only contains commits being displayed. */
2287 TAILQ_INIT(&s->commits.head);
2288 s->commits.ncommits = 0;
2291 if (head_ref_name) {
2292 s->head_ref_name = strdup(head_ref_name);
2293 if (s->head_ref_name == NULL) {
2294 err = got_error_from_errno("strdup");
2298 s->start_id = got_object_id_dup(start_id);
2299 if (s->start_id == NULL) {
2300 err = got_error_from_errno("got_object_id_dup");
2303 s->log_branches = log_branches;
2305 SIMPLEQ_INIT(&s->colors);
2306 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2307 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2308 get_color_value("TOG_COLOR_COMMIT"));
2311 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2312 get_color_value("TOG_COLOR_AUTHOR"));
2314 free_colors(&s->colors);
2317 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2318 get_color_value("TOG_COLOR_DATE"));
2320 free_colors(&s->colors);
2325 view->show = show_log_view;
2326 view->input = input_log_view;
2327 view->close = close_log_view;
2328 view->search_start = search_start_log_view;
2329 view->search_next = search_next_log_view;
2331 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2334 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2338 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2339 s->repo, NULL, NULL);
2343 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2345 err = got_error_set_errno(errcode, "pthread_cond_init");
2348 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2350 err = got_error_set_errno(errcode, "pthread_cond_init");
2354 s->thread_args.commits_needed = view->nlines;
2355 s->thread_args.graph = thread_graph;
2356 s->thread_args.commits = &s->commits;
2357 s->thread_args.in_repo_path = s->in_repo_path;
2358 s->thread_args.start_id = s->start_id;
2359 s->thread_args.repo = thread_repo;
2360 s->thread_args.log_complete = 0;
2361 s->thread_args.quit = &s->quit;
2362 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2363 s->thread_args.selected_entry = &s->selected_entry;
2364 s->thread_args.searching = &view->searching;
2365 s->thread_args.search_next_done = &view->search_next_done;
2366 s->thread_args.regex = &view->regex;
2369 close_log_view(view);
2373 static const struct got_error *
2374 show_log_view(struct tog_view *view)
2376 const struct got_error *err;
2377 struct tog_log_view_state *s = &view->state.log;
2379 if (s->thread == NULL) {
2380 int errcode = pthread_create(&s->thread, NULL, log_thread,
2383 return got_error_set_errno(errcode, "pthread_create");
2384 if (s->thread_args.commits_needed > 0) {
2385 err = trigger_log_thread(view, 1);
2391 return draw_commits(view);
2394 static const struct got_error *
2395 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2397 const struct got_error *err = NULL;
2398 struct tog_log_view_state *s = &view->state.log;
2399 struct tog_view *diff_view = NULL, *tree_view = NULL;
2400 struct tog_view *ref_view = NULL;
2411 if (s->first_displayed_entry == NULL)
2413 if (s->selected > 0)
2416 log_scroll_up(s, 1);
2421 if (s->first_displayed_entry == NULL)
2423 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2426 log_scroll_up(s, view->nlines - 1);
2433 if (s->first_displayed_entry == NULL)
2435 if (s->selected < MIN(view->nlines - 2,
2436 s->commits.ncommits - 1))
2439 err = log_scroll_down(view, 1);
2447 struct commit_queue_entry *first;
2448 first = s->first_displayed_entry;
2451 err = log_scroll_down(view, view->nlines - 1);
2454 if (first == s->first_displayed_entry &&
2455 s->selected < MIN(view->nlines - 2,
2456 s->commits.ncommits - 1)) {
2457 /* can't scroll further down */
2458 s->selected = MIN(view->nlines - 2,
2459 s->commits.ncommits - 1);
2465 if (s->selected > view->nlines - 2)
2466 s->selected = view->nlines - 2;
2467 if (s->selected > s->commits.ncommits - 1)
2468 s->selected = s->commits.ncommits - 1;
2470 if (s->commits.ncommits < view->nlines - 1 &&
2471 !s->thread_args.log_complete) {
2472 s->thread_args.commits_needed += (view->nlines - 1) -
2473 s->commits.ncommits;
2474 err = trigger_log_thread(view, 1);
2480 if (s->selected_entry == NULL)
2482 if (view_is_parent_view(view))
2483 begin_x = view_split_begin_x(view->begin_x);
2484 err = open_diff_view_for_commit(&diff_view, begin_x,
2485 s->selected_entry->commit, s->selected_entry->id,
2490 diff_view->focussed = 1;
2491 if (view_is_parent_view(view)) {
2492 err = view_close_child(view);
2495 view_set_child(view, diff_view);
2496 view->focus_child = 1;
2498 *new_view = diff_view;
2501 if (s->selected_entry == NULL)
2503 if (view_is_parent_view(view))
2504 begin_x = view_split_begin_x(view->begin_x);
2505 err = browse_commit_tree(&tree_view, begin_x,
2506 s->selected_entry, s->in_repo_path, s->head_ref_name,
2511 tree_view->focussed = 1;
2512 if (view_is_parent_view(view)) {
2513 err = view_close_child(view);
2516 view_set_child(view, tree_view);
2517 view->focus_child = 1;
2519 *new_view = tree_view;
2524 if (ch == KEY_BACKSPACE &&
2525 got_path_is_root_dir(s->in_repo_path))
2527 err = stop_log_thread(s);
2530 if (ch == KEY_BACKSPACE) {
2532 err = got_path_dirname(&parent_path, s->in_repo_path);
2535 free(s->in_repo_path);
2536 s->in_repo_path = parent_path;
2537 s->thread_args.in_repo_path = s->in_repo_path;
2538 } else if (ch == CTRL('l')) {
2539 struct got_object_id *start_id;
2540 err = got_repo_match_object_id(&start_id, NULL,
2541 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2542 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2546 s->start_id = start_id;
2547 s->thread_args.start_id = s->start_id;
2549 s->log_branches = !s->log_branches;
2551 err = got_repo_open(&s->thread_args.repo,
2552 got_repo_get_path(s->repo), NULL);
2556 err = tog_load_refs(s->repo);
2559 err = got_commit_graph_open(&s->thread_args.graph,
2560 s->in_repo_path, !s->log_branches);
2563 err = got_commit_graph_iter_start(s->thread_args.graph,
2564 s->start_id, s->repo, NULL, NULL);
2567 free_commits(&s->commits);
2568 s->first_displayed_entry = NULL;
2569 s->last_displayed_entry = NULL;
2570 s->selected_entry = NULL;
2572 s->thread_args.log_complete = 0;
2574 s->thread_args.commits_needed = view->nlines;
2577 if (view_is_parent_view(view))
2578 begin_x = view_split_begin_x(view->begin_x);
2579 ref_view = view_open(view->nlines, view->ncols,
2580 view->begin_y, begin_x, TOG_VIEW_REF);
2581 if (ref_view == NULL)
2582 return got_error_from_errno("view_open");
2583 err = open_ref_view(ref_view, s->repo);
2585 view_close(ref_view);
2589 ref_view->focussed = 1;
2590 if (view_is_parent_view(view)) {
2591 err = view_close_child(view);
2594 view_set_child(view, ref_view);
2595 view->focus_child = 1;
2597 *new_view = ref_view;
2606 static const struct got_error *
2607 apply_unveil(const char *repo_path, const char *worktree_path)
2609 const struct got_error *error;
2612 if (unveil("gmon.out", "rwc") != 0)
2613 return got_error_from_errno2("unveil", "gmon.out");
2615 if (repo_path && unveil(repo_path, "r") != 0)
2616 return got_error_from_errno2("unveil", repo_path);
2618 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2619 return got_error_from_errno2("unveil", worktree_path);
2621 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2622 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2624 error = got_privsep_unveil_exec_helpers();
2628 if (unveil(NULL, NULL) != 0)
2629 return got_error_from_errno("unveil");
2639 halfdelay(1); /* Do fast refresh while initial view is loading. */
2642 intrflush(stdscr, FALSE);
2643 keypad(stdscr, TRUE);
2645 if (getenv("TOG_COLORS") != NULL) {
2647 use_default_colors();
2649 signal(SIGWINCH, tog_sigwinch);
2650 signal(SIGPIPE, tog_sigpipe);
2651 signal(SIGCONT, tog_sigcont);
2654 static const struct got_error *
2655 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2656 struct got_repository *repo, struct got_worktree *worktree)
2658 const struct got_error *err = NULL;
2661 *in_repo_path = strdup("/");
2662 if (*in_repo_path == NULL)
2663 return got_error_from_errno("strdup");
2668 const char *prefix = got_worktree_get_path_prefix(worktree);
2671 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2674 if (asprintf(in_repo_path, "%s%s%s", prefix,
2675 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2677 err = got_error_from_errno("asprintf");
2678 *in_repo_path = NULL;
2682 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2687 static const struct got_error *
2688 cmd_log(int argc, char *argv[])
2690 const struct got_error *error;
2691 struct got_repository *repo = NULL;
2692 struct got_worktree *worktree = NULL;
2693 struct got_object_id *start_id = NULL;
2694 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2695 char *start_commit = NULL, *label = NULL;
2696 struct got_reference *ref = NULL;
2697 const char *head_ref_name = NULL;
2698 int ch, log_branches = 0;
2699 struct tog_view *view;
2701 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2707 start_commit = optarg;
2710 repo_path = realpath(optarg, NULL);
2711 if (repo_path == NULL)
2712 return got_error_from_errno2("realpath",
2727 if (repo_path == NULL) {
2728 cwd = getcwd(NULL, 0);
2730 return got_error_from_errno("getcwd");
2731 error = got_worktree_open(&worktree, cwd);
2732 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2736 strdup(got_worktree_get_repo_path(worktree));
2738 repo_path = strdup(cwd);
2739 if (repo_path == NULL) {
2740 error = got_error_from_errno("strdup");
2745 error = got_repo_open(&repo, repo_path, NULL);
2749 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2756 error = apply_unveil(got_repo_get_path(repo),
2757 worktree ? got_worktree_get_root_path(worktree) : NULL);
2761 /* already loaded by tog_log_with_path()? */
2762 if (TAILQ_EMPTY(&tog_refs)) {
2763 error = tog_load_refs(repo);
2768 if (start_commit == NULL) {
2769 error = got_repo_match_object_id(&start_id, &label,
2770 worktree ? got_worktree_get_head_ref_name(worktree) :
2771 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2774 head_ref_name = label;
2776 error = got_ref_open(&ref, repo, start_commit, 0);
2778 head_ref_name = got_ref_get_name(ref);
2779 else if (error->code != GOT_ERR_NOT_REF)
2781 error = got_repo_match_object_id(&start_id, NULL,
2782 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2787 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2789 error = got_error_from_errno("view_open");
2792 error = open_log_view(view, start_id, repo, head_ref_name,
2793 in_repo_path, log_branches);
2797 /* Release work tree lock. */
2798 got_worktree_close(worktree);
2801 error = view_loop(view);
2811 got_repo_close(repo);
2813 got_worktree_close(worktree);
2822 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2823 "[-w] object1 object2\n", getprogname());
2828 match_line(const char *line, regex_t *regex, size_t nmatch,
2829 regmatch_t *regmatch)
2831 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2835 match_color(struct tog_colors *colors, const char *line)
2837 struct tog_color *tc = NULL;
2839 SIMPLEQ_FOREACH(tc, colors, entry) {
2840 if (match_line(line, &tc->regex, 0, NULL))
2847 static const struct got_error *
2848 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2849 WINDOW *window, regmatch_t *regmatch)
2851 const struct got_error *err = NULL;
2858 s = strndup(line, regmatch->rm_so);
2860 return got_error_from_errno("strndup");
2862 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2867 waddwstr(window, wline);
2874 s = strndup(line + regmatch->rm_so,
2875 regmatch->rm_eo - regmatch->rm_so);
2877 err = got_error_from_errno("strndup");
2881 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2886 wattr_on(window, A_STANDOUT, NULL);
2887 waddwstr(window, wline);
2888 wattr_off(window, A_STANDOUT, NULL);
2895 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2896 err = format_line(&wline, &width,
2897 line + regmatch->rm_eo, wlimit, col_tab_align);
2900 waddwstr(window, wline);
2908 static const struct got_error *
2909 draw_file(struct tog_view *view, const char *header)
2911 struct tog_diff_view_state *s = &view->state.diff;
2912 regmatch_t *regmatch = &view->regmatch;
2913 const struct got_error *err;
2916 size_t linesize = 0;
2918 struct tog_color *tc;
2921 int max_lines = view->nlines;
2922 int nlines = s->nlines;
2925 line_offset = s->line_offsets[s->first_displayed_line - 1];
2926 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2927 return got_error_from_errno("fseek");
2929 werase(view->window);
2932 if (asprintf(&line, "[%d/%d] %s",
2933 s->first_displayed_line - 1 + s->selected_line, nlines,
2935 return got_error_from_errno("asprintf");
2936 err = format_line(&wline, &width, line, view->ncols, 0);
2941 if (view_needs_focus_indication(view))
2942 wstandout(view->window);
2943 waddwstr(view->window, wline);
2946 if (view_needs_focus_indication(view))
2947 wstandend(view->window);
2948 if (width <= view->ncols - 1)
2949 waddch(view->window, '\n');
2958 while (max_lines > 0 && nprinted < max_lines) {
2959 linelen = getline(&line, &linesize, s->f);
2960 if (linelen == -1) {
2966 return got_ferror(s->f, GOT_ERR_IO);
2969 tc = match_color(&s->colors, line);
2971 wattr_on(view->window,
2972 COLOR_PAIR(tc->colorpair), NULL);
2973 if (s->first_displayed_line + nprinted == s->matched_line &&
2974 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2975 err = add_matched_line(&width, line, view->ncols, 0,
2976 view->window, regmatch);
2982 err = format_line(&wline, &width, line, view->ncols, 0);
2987 waddwstr(view->window, wline);
2992 wattr_off(view->window,
2993 COLOR_PAIR(tc->colorpair), NULL);
2994 if (width <= view->ncols - 1)
2995 waddch(view->window, '\n');
3000 s->last_displayed_line = s->first_displayed_line +
3003 s->last_displayed_line = s->first_displayed_line;
3008 while (nprinted < view->nlines) {
3009 waddch(view->window, '\n');
3013 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3018 wstandout(view->window);
3019 waddwstr(view->window, wline);
3022 wstandend(view->window);
3029 get_datestr(time_t *time, char *datebuf)
3031 struct tm mytm, *tm;
3034 tm = gmtime_r(time, &mytm);
3037 s = asctime_r(tm, datebuf);
3040 p = strchr(s, '\n');
3046 static const struct got_error *
3047 get_changed_paths(struct got_pathlist_head *paths,
3048 struct got_commit_object *commit, struct got_repository *repo)
3050 const struct got_error *err = NULL;
3051 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3052 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3053 struct got_object_qid *qid;
3055 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3057 struct got_commit_object *pcommit;
3058 err = got_object_open_as_commit(&pcommit, repo,
3063 tree_id1 = got_object_commit_get_tree_id(pcommit);
3064 got_object_commit_close(pcommit);
3069 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3074 tree_id2 = got_object_commit_get_tree_id(commit);
3075 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3079 err = got_diff_tree(tree1, tree2, "", "", repo,
3080 got_diff_tree_collect_changed_paths, paths, 0);
3083 got_object_tree_close(tree1);
3085 got_object_tree_close(tree2);
3089 static const struct got_error *
3090 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3094 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3096 return got_error_from_errno("reallocarray");
3098 (*line_offsets)[*nlines] = off;
3103 static const struct got_error *
3104 write_commit_info(off_t **line_offsets, size_t *nlines,
3105 struct got_object_id *commit_id, struct got_reflist_head *refs,
3106 struct got_repository *repo, FILE *outfile)
3108 const struct got_error *err = NULL;
3109 char datebuf[26], *datestr;
3110 struct got_commit_object *commit;
3111 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3112 time_t committer_time;
3113 const char *author, *committer;
3114 char *refs_str = NULL;
3115 struct got_pathlist_head changed_paths;
3116 struct got_pathlist_entry *pe;
3120 TAILQ_INIT(&changed_paths);
3123 err = build_refs_str(&refs_str, refs, commit_id, repo);
3128 err = got_object_open_as_commit(&commit, repo, commit_id);
3132 err = got_object_id_str(&id_str, commit_id);
3134 err = got_error_from_errno("got_object_id_str");
3138 err = add_line_offset(line_offsets, nlines, 0);
3142 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3143 refs_str ? refs_str : "", refs_str ? ")" : "");
3145 err = got_error_from_errno("fprintf");
3149 err = add_line_offset(line_offsets, nlines, outoff);
3153 n = fprintf(outfile, "from: %s\n",
3154 got_object_commit_get_author(commit));
3156 err = got_error_from_errno("fprintf");
3160 err = add_line_offset(line_offsets, nlines, outoff);
3164 committer_time = got_object_commit_get_committer_time(commit);
3165 datestr = get_datestr(&committer_time, datebuf);
3167 n = fprintf(outfile, "date: %s UTC\n", datestr);
3169 err = got_error_from_errno("fprintf");
3173 err = add_line_offset(line_offsets, nlines, outoff);
3177 author = got_object_commit_get_author(commit);
3178 committer = got_object_commit_get_committer(commit);
3179 if (strcmp(author, committer) != 0) {
3180 n = fprintf(outfile, "via: %s\n", committer);
3182 err = got_error_from_errno("fprintf");
3186 err = add_line_offset(line_offsets, nlines, outoff);
3190 err = got_object_commit_get_logmsg(&logmsg, commit);
3194 while ((line = strsep(&s, "\n")) != NULL) {
3195 n = fprintf(outfile, "%s\n", line);
3197 err = got_error_from_errno("fprintf");
3201 err = add_line_offset(line_offsets, nlines, outoff);
3206 err = get_changed_paths(&changed_paths, commit, repo);
3209 TAILQ_FOREACH(pe, &changed_paths, entry) {
3210 struct got_diff_changed_path *cp = pe->data;
3211 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3213 err = got_error_from_errno("fprintf");
3217 err = add_line_offset(line_offsets, nlines, outoff);
3220 free((char *)pe->path);
3224 fputc('\n', outfile);
3226 err = add_line_offset(line_offsets, nlines, outoff);
3228 got_pathlist_free(&changed_paths);
3232 got_object_commit_close(commit);
3234 free(*line_offsets);
3235 *line_offsets = NULL;
3241 static const struct got_error *
3242 create_diff(struct tog_diff_view_state *s)
3244 const struct got_error *err = NULL;
3248 free(s->line_offsets);
3249 s->line_offsets = malloc(sizeof(off_t));
3250 if (s->line_offsets == NULL)
3251 return got_error_from_errno("malloc");
3256 err = got_error_from_errno("got_opentemp");
3259 if (s->f && fclose(s->f) == EOF) {
3260 err = got_error_from_errno("fclose");
3266 err = got_object_get_type(&obj_type, s->repo, s->id1);
3268 err = got_object_get_type(&obj_type, s->repo, s->id2);
3273 case GOT_OBJ_TYPE_BLOB:
3274 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3275 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3276 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3278 case GOT_OBJ_TYPE_TREE:
3279 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3280 s->id1, s->id2, "", "", s->diff_context,
3281 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3283 case GOT_OBJ_TYPE_COMMIT: {
3284 const struct got_object_id_queue *parent_ids;
3285 struct got_object_qid *pid;
3286 struct got_commit_object *commit2;
3287 struct got_reflist_head *refs;
3289 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3292 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3293 /* Show commit info if we're diffing to a parent/root commit. */
3294 if (s->id1 == NULL) {
3295 err = write_commit_info(&s->line_offsets, &s->nlines,
3296 s->id2, refs, s->repo, s->f);
3300 parent_ids = got_object_commit_get_parent_ids(commit2);
3301 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3302 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3303 err = write_commit_info(
3304 &s->line_offsets, &s->nlines,
3305 s->id2, refs, s->repo, s->f);
3312 got_object_commit_close(commit2);
3314 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3315 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3316 s->force_text_diff, s->repo, s->f);
3320 err = got_error(GOT_ERR_OBJ_TYPE);
3326 if (s->f && fflush(s->f) != 0 && err == NULL)
3327 err = got_error_from_errno("fflush");
3332 diff_view_indicate_progress(struct tog_view *view)
3334 mvwaddstr(view->window, 0, 0, "diffing...");
3339 static const struct got_error *
3340 search_start_diff_view(struct tog_view *view)
3342 struct tog_diff_view_state *s = &view->state.diff;
3344 s->matched_line = 0;
3348 static const struct got_error *
3349 search_next_diff_view(struct tog_view *view)
3351 struct tog_diff_view_state *s = &view->state.diff;
3354 size_t linesize = 0;
3357 if (!view->searching) {
3358 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3362 if (s->matched_line) {
3363 if (view->searching == TOG_SEARCH_FORWARD)
3364 lineno = s->matched_line + 1;
3366 lineno = s->matched_line - 1;
3368 if (view->searching == TOG_SEARCH_FORWARD)
3377 if (lineno <= 0 || lineno > s->nlines) {
3378 if (s->matched_line == 0) {
3379 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3383 if (view->searching == TOG_SEARCH_FORWARD)
3389 offset = s->line_offsets[lineno - 1];
3390 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3392 return got_error_from_errno("fseeko");
3394 linelen = getline(&line, &linesize, s->f);
3395 if (linelen != -1 &&
3396 match_line(line, &view->regex, 1, &view->regmatch)) {
3397 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3398 s->matched_line = lineno;
3401 if (view->searching == TOG_SEARCH_FORWARD)
3408 if (s->matched_line) {
3409 s->first_displayed_line = s->matched_line;
3410 s->selected_line = 1;
3416 static const struct got_error *
3417 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3418 struct got_object_id *id2, const char *label1, const char *label2,
3419 int diff_context, int ignore_whitespace, int force_text_diff,
3420 struct tog_view *log_view, struct got_repository *repo)
3422 const struct got_error *err;
3423 struct tog_diff_view_state *s = &view->state.diff;
3425 if (id1 != NULL && id2 != NULL) {
3427 err = got_object_get_type(&type1, repo, id1);
3430 err = got_object_get_type(&type2, repo, id2);
3435 return got_error(GOT_ERR_OBJ_TYPE);
3437 s->first_displayed_line = 1;
3438 s->last_displayed_line = view->nlines;
3439 s->selected_line = 1;
3447 s->id1 = got_object_id_dup(id1);
3449 return got_error_from_errno("got_object_id_dup");
3453 s->id2 = got_object_id_dup(id2);
3454 if (s->id2 == NULL) {
3457 return got_error_from_errno("got_object_id_dup");
3460 s->first_displayed_line = 1;
3461 s->last_displayed_line = view->nlines;
3462 s->diff_context = diff_context;
3463 s->ignore_whitespace = ignore_whitespace;
3464 s->force_text_diff = force_text_diff;
3465 s->log_view = log_view;
3468 SIMPLEQ_INIT(&s->colors);
3469 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3470 err = add_color(&s->colors,
3471 "^-", TOG_COLOR_DIFF_MINUS,
3472 get_color_value("TOG_COLOR_DIFF_MINUS"));
3475 err = add_color(&s->colors, "^\\+",
3476 TOG_COLOR_DIFF_PLUS,
3477 get_color_value("TOG_COLOR_DIFF_PLUS"));
3479 free_colors(&s->colors);
3482 err = add_color(&s->colors,
3483 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3484 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3486 free_colors(&s->colors);
3490 err = add_color(&s->colors,
3491 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3492 TOG_COLOR_DIFF_META,
3493 get_color_value("TOG_COLOR_DIFF_META"));
3495 free_colors(&s->colors);
3499 err = add_color(&s->colors,
3500 "^(from|via): ", TOG_COLOR_AUTHOR,
3501 get_color_value("TOG_COLOR_AUTHOR"));
3503 free_colors(&s->colors);
3507 err = add_color(&s->colors,
3508 "^date: ", TOG_COLOR_DATE,
3509 get_color_value("TOG_COLOR_DATE"));
3511 free_colors(&s->colors);
3516 if (log_view && view_is_splitscreen(view))
3517 show_log_view(log_view); /* draw vborder */
3518 diff_view_indicate_progress(view);
3520 s->line_offsets = NULL;
3522 err = create_diff(s);
3528 free_colors(&s->colors);
3532 view->show = show_diff_view;
3533 view->input = input_diff_view;
3534 view->close = close_diff_view;
3535 view->search_start = search_start_diff_view;
3536 view->search_next = search_next_diff_view;
3541 static const struct got_error *
3542 close_diff_view(struct tog_view *view)
3544 const struct got_error *err = NULL;
3545 struct tog_diff_view_state *s = &view->state.diff;
3551 if (s->f && fclose(s->f) == EOF)
3552 err = got_error_from_errno("fclose");
3553 free_colors(&s->colors);
3554 free(s->line_offsets);
3555 s->line_offsets = NULL;
3560 static const struct got_error *
3561 show_diff_view(struct tog_view *view)
3563 const struct got_error *err;
3564 struct tog_diff_view_state *s = &view->state.diff;
3565 char *id_str1 = NULL, *id_str2, *header;
3566 const char *label1, *label2;
3569 err = got_object_id_str(&id_str1, s->id1);
3572 label1 = s->label1 ? : id_str1;
3574 label1 = "/dev/null";
3576 err = got_object_id_str(&id_str2, s->id2);
3579 label2 = s->label2 ? : id_str2;
3581 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3582 err = got_error_from_errno("asprintf");
3590 return draw_file(view, header);
3593 static const struct got_error *
3594 set_selected_commit(struct tog_diff_view_state *s,
3595 struct commit_queue_entry *entry)
3597 const struct got_error *err;
3598 const struct got_object_id_queue *parent_ids;
3599 struct got_commit_object *selected_commit;
3600 struct got_object_qid *pid;
3603 s->id2 = got_object_id_dup(entry->id);
3605 return got_error_from_errno("got_object_id_dup");
3607 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3610 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3612 pid = SIMPLEQ_FIRST(parent_ids);
3613 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3614 got_object_commit_close(selected_commit);
3618 static const struct got_error *
3619 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3621 const struct got_error *err = NULL;
3622 struct tog_diff_view_state *s = &view->state.diff;
3623 struct tog_log_view_state *ls;
3624 struct commit_queue_entry *old_selected_entry;
3626 size_t linesize = 0;
3634 s->force_text_diff = !s->force_text_diff;
3636 s->ignore_whitespace = !s->ignore_whitespace;
3637 wclear(view->window);
3638 s->first_displayed_line = 1;
3639 s->last_displayed_line = view->nlines;
3640 diff_view_indicate_progress(view);
3641 err = create_diff(s);
3645 if (s->first_displayed_line > 1)
3646 s->first_displayed_line--;
3650 if (s->first_displayed_line == 1)
3653 while (i++ < view->nlines - 1 &&
3654 s->first_displayed_line > 1)
3655 s->first_displayed_line--;
3660 s->first_displayed_line++;
3668 while (!s->eof && i++ < view->nlines - 1) {
3669 linelen = getline(&line, &linesize, s->f);
3670 s->first_displayed_line++;
3671 if (linelen == -1) {
3675 err = got_ferror(s->f, GOT_ERR_IO);
3682 if (s->diff_context > 0) {
3684 diff_view_indicate_progress(view);
3685 err = create_diff(s);
3686 if (s->first_displayed_line + view->nlines - 1 >
3688 s->first_displayed_line = 1;
3689 s->last_displayed_line = view->nlines;
3694 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3696 diff_view_indicate_progress(view);
3697 err = create_diff(s);
3702 if (s->log_view == NULL)
3704 ls = &s->log_view->state.log;
3705 old_selected_entry = ls->selected_entry;
3707 err = input_log_view(NULL, s->log_view, KEY_UP);
3711 if (old_selected_entry == ls->selected_entry)
3714 err = set_selected_commit(s, ls->selected_entry);
3718 s->first_displayed_line = 1;
3719 s->last_displayed_line = view->nlines;
3721 diff_view_indicate_progress(view);
3722 err = create_diff(s);
3726 if (s->log_view == NULL)
3728 ls = &s->log_view->state.log;
3729 old_selected_entry = ls->selected_entry;
3731 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3735 if (old_selected_entry == ls->selected_entry)
3738 err = set_selected_commit(s, ls->selected_entry);
3742 s->first_displayed_line = 1;
3743 s->last_displayed_line = view->nlines;
3745 diff_view_indicate_progress(view);
3746 err = create_diff(s);
3755 static const struct got_error *
3756 cmd_diff(int argc, char *argv[])
3758 const struct got_error *error = NULL;
3759 struct got_repository *repo = NULL;
3760 struct got_worktree *worktree = NULL;
3761 struct got_object_id *id1 = NULL, *id2 = NULL;
3762 char *repo_path = NULL, *cwd = NULL;
3763 char *id_str1 = NULL, *id_str2 = NULL;
3764 char *label1 = NULL, *label2 = NULL;
3765 int diff_context = 3, ignore_whitespace = 0;
3766 int ch, force_text_diff = 0;
3768 struct tog_view *view;
3770 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3773 force_text_diff = 1;
3776 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3779 err(1, "-C option %s", errstr);
3782 repo_path = realpath(optarg, NULL);
3783 if (repo_path == NULL)
3784 return got_error_from_errno2("realpath",
3786 got_path_strip_trailing_slashes(repo_path);
3789 ignore_whitespace = 1;
3801 usage_diff(); /* TODO show local worktree changes */
3802 } else if (argc == 2) {
3808 if (repo_path == NULL) {
3809 cwd = getcwd(NULL, 0);
3811 return got_error_from_errno("getcwd");
3812 error = got_worktree_open(&worktree, cwd);
3813 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3817 strdup(got_worktree_get_repo_path(worktree));
3819 repo_path = strdup(cwd);
3820 if (repo_path == NULL) {
3821 error = got_error_from_errno("strdup");
3826 error = got_repo_open(&repo, repo_path, NULL);
3832 error = apply_unveil(got_repo_get_path(repo), NULL);
3836 error = tog_load_refs(repo);
3840 error = got_repo_match_object_id(&id1, &label1, id_str1,
3841 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3845 error = got_repo_match_object_id(&id2, &label2, id_str2,
3846 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3850 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3852 error = got_error_from_errno("view_open");
3855 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3856 ignore_whitespace, force_text_diff, NULL, repo);
3859 error = view_loop(view);
3866 got_repo_close(repo);
3868 got_worktree_close(worktree);
3877 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3882 struct tog_blame_line {
3884 struct got_object_id *id;
3887 static const struct got_error *
3888 draw_blame(struct tog_view *view)
3890 struct tog_blame_view_state *s = &view->state.blame;
3891 struct tog_blame *blame = &s->blame;
3892 regmatch_t *regmatch = &view->regmatch;
3893 const struct got_error *err;
3894 int lineno = 0, nprinted = 0;
3896 size_t linesize = 0;
3900 struct tog_blame_line *blame_line;
3901 struct got_object_id *prev_id = NULL;
3903 struct tog_color *tc;
3905 err = got_object_id_str(&id_str, s->blamed_commit->id);
3910 werase(view->window);
3912 if (asprintf(&line, "commit %s", id_str) == -1) {
3913 err = got_error_from_errno("asprintf");
3918 err = format_line(&wline, &width, line, view->ncols, 0);
3923 if (view_needs_focus_indication(view))
3924 wstandout(view->window);
3925 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3927 wattr_on(view->window,
3928 COLOR_PAIR(tc->colorpair), NULL);
3929 waddwstr(view->window, wline);
3931 wattr_off(view->window,
3932 COLOR_PAIR(tc->colorpair), NULL);
3933 if (view_needs_focus_indication(view))
3934 wstandend(view->window);
3937 if (width < view->ncols - 1)
3938 waddch(view->window, '\n');
3940 if (asprintf(&line, "[%d/%d] %s%s",
3941 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3942 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3944 return got_error_from_errno("asprintf");
3947 err = format_line(&wline, &width, line, view->ncols, 0);
3952 waddwstr(view->window, wline);
3955 if (width < view->ncols - 1)
3956 waddch(view->window, '\n');
3959 while (nprinted < view->nlines - 2) {
3960 linelen = getline(&line, &linesize, blame->f);
3961 if (linelen == -1) {
3962 if (feof(blame->f)) {
3967 return got_ferror(blame->f, GOT_ERR_IO);
3969 if (++lineno < s->first_displayed_line)
3972 if (view->focussed && nprinted == s->selected_line - 1)
3973 wstandout(view->window);
3975 if (blame->nlines > 0) {
3976 blame_line = &blame->lines[lineno - 1];
3977 if (blame_line->annotated && prev_id &&
3978 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3980 nprinted == s->selected_line - 1)) {
3981 waddstr(view->window, " ");
3982 } else if (blame_line->annotated) {
3984 err = got_object_id_str(&id_str, blame_line->id);
3989 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3991 wattr_on(view->window,
3992 COLOR_PAIR(tc->colorpair), NULL);
3993 wprintw(view->window, "%.8s", id_str);
3995 wattr_off(view->window,
3996 COLOR_PAIR(tc->colorpair), NULL);
3998 prev_id = blame_line->id;
4000 waddstr(view->window, "........");
4004 waddstr(view->window, "........");
4008 if (view->focussed && nprinted == s->selected_line - 1)
4009 wstandend(view->window);
4010 waddstr(view->window, " ");
4012 if (view->ncols <= 9) {
4014 wline = wcsdup(L"");
4015 if (wline == NULL) {
4016 err = got_error_from_errno("wcsdup");
4020 } else if (s->first_displayed_line + nprinted ==
4022 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4023 err = add_matched_line(&width, line, view->ncols - 9, 9,
4024 view->window, regmatch);
4031 err = format_line(&wline, &width, line,
4032 view->ncols - 9, 9);
4033 waddwstr(view->window, wline);
4039 if (width <= view->ncols - 1)
4040 waddch(view->window, '\n');
4041 if (++nprinted == 1)
4042 s->first_displayed_line = lineno;
4045 s->last_displayed_line = lineno;
4052 static const struct got_error *
4053 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4055 const struct got_error *err = NULL;
4056 struct tog_blame_cb_args *a = arg;
4057 struct tog_blame_line *line;
4060 if (nlines != a->nlines ||
4061 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4062 return got_error(GOT_ERR_RANGE);
4064 errcode = pthread_mutex_lock(&tog_mutex);
4066 return got_error_set_errno(errcode, "pthread_mutex_lock");
4068 if (*a->quit) { /* user has quit the blame view */
4069 err = got_error(GOT_ERR_ITER_COMPLETED);
4074 goto done; /* no change in this commit */
4076 line = &a->lines[lineno - 1];
4077 if (line->annotated)
4080 line->id = got_object_id_dup(id);
4081 if (line->id == NULL) {
4082 err = got_error_from_errno("got_object_id_dup");
4085 line->annotated = 1;
4087 errcode = pthread_mutex_unlock(&tog_mutex);
4089 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4094 blame_thread(void *arg)
4096 const struct got_error *err;
4097 struct tog_blame_thread_args *ta = arg;
4098 struct tog_blame_cb_args *a = ta->cb_args;
4101 err = block_signals_used_by_main_thread();
4105 err = got_blame(ta->path, a->commit_id, ta->repo,
4106 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4107 if (err && err->code == GOT_ERR_CANCELLED)
4110 errcode = pthread_mutex_lock(&tog_mutex);
4112 return (void *)got_error_set_errno(errcode,
4113 "pthread_mutex_lock");
4115 got_repo_close(ta->repo);
4119 errcode = pthread_mutex_unlock(&tog_mutex);
4120 if (errcode && err == NULL)
4121 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4126 static struct got_object_id *
4127 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4128 int first_displayed_line, int selected_line)
4130 struct tog_blame_line *line;
4135 line = &lines[first_displayed_line - 1 + selected_line - 1];
4136 if (!line->annotated)
4142 static const struct got_error *
4143 stop_blame(struct tog_blame *blame)
4145 const struct got_error *err = NULL;
4148 if (blame->thread) {
4150 errcode = pthread_mutex_unlock(&tog_mutex);
4152 return got_error_set_errno(errcode,
4153 "pthread_mutex_unlock");
4154 errcode = pthread_join(blame->thread, (void **)&err);
4156 return got_error_set_errno(errcode, "pthread_join");
4157 errcode = pthread_mutex_lock(&tog_mutex);
4159 return got_error_set_errno(errcode,
4160 "pthread_mutex_lock");
4161 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4163 blame->thread = NULL;
4165 if (blame->thread_args.repo) {
4166 got_repo_close(blame->thread_args.repo);
4167 blame->thread_args.repo = NULL;
4170 if (fclose(blame->f) == EOF && err == NULL)
4171 err = got_error_from_errno("fclose");
4175 for (i = 0; i < blame->nlines; i++)
4176 free(blame->lines[i].id);
4178 blame->lines = NULL;
4180 free(blame->cb_args.commit_id);
4181 blame->cb_args.commit_id = NULL;
4186 static const struct got_error *
4187 cancel_blame_view(void *arg)
4189 const struct got_error *err = NULL;
4193 errcode = pthread_mutex_lock(&tog_mutex);
4195 return got_error_set_errno(errcode,
4196 "pthread_mutex_unlock");
4199 err = got_error(GOT_ERR_CANCELLED);
4201 errcode = pthread_mutex_unlock(&tog_mutex);
4203 return got_error_set_errno(errcode,
4204 "pthread_mutex_lock");
4209 static const struct got_error *
4210 run_blame(struct tog_view *view)
4212 struct tog_blame_view_state *s = &view->state.blame;
4213 struct tog_blame *blame = &s->blame;
4214 const struct got_error *err = NULL;
4215 struct got_blob_object *blob = NULL;
4216 struct got_repository *thread_repo = NULL;
4217 struct got_object_id *obj_id = NULL;
4220 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4225 err = got_object_get_type(&obj_type, s->repo, obj_id);
4229 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4230 err = got_error(GOT_ERR_OBJ_TYPE);
4234 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4237 blame->f = got_opentemp();
4238 if (blame->f == NULL) {
4239 err = got_error_from_errno("got_opentemp");
4242 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4243 &blame->line_offsets, blame->f, blob);
4246 if (blame->nlines == 0) {
4247 s->blame_complete = 1;
4251 /* Don't include \n at EOF in the blame line count. */
4252 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4255 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4256 if (blame->lines == NULL) {
4257 err = got_error_from_errno("calloc");
4261 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4265 blame->cb_args.view = view;
4266 blame->cb_args.lines = blame->lines;
4267 blame->cb_args.nlines = blame->nlines;
4268 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4269 if (blame->cb_args.commit_id == NULL) {
4270 err = got_error_from_errno("got_object_id_dup");
4273 blame->cb_args.quit = &s->done;
4275 blame->thread_args.path = s->path;
4276 blame->thread_args.repo = thread_repo;
4277 blame->thread_args.cb_args = &blame->cb_args;
4278 blame->thread_args.complete = &s->blame_complete;
4279 blame->thread_args.cancel_cb = cancel_blame_view;
4280 blame->thread_args.cancel_arg = &s->done;
4281 s->blame_complete = 0;
4283 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4284 s->first_displayed_line = 1;
4285 s->last_displayed_line = view->nlines;
4286 s->selected_line = 1;
4291 got_object_blob_close(blob);
4298 static const struct got_error *
4299 open_blame_view(struct tog_view *view, char *path,
4300 struct got_object_id *commit_id, struct got_repository *repo)
4302 const struct got_error *err = NULL;
4303 struct tog_blame_view_state *s = &view->state.blame;
4305 SIMPLEQ_INIT(&s->blamed_commits);
4307 s->path = strdup(path);
4308 if (s->path == NULL)
4309 return got_error_from_errno("strdup");
4311 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4317 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4318 s->first_displayed_line = 1;
4319 s->last_displayed_line = view->nlines;
4320 s->selected_line = 1;
4321 s->blame_complete = 0;
4323 s->commit_id = commit_id;
4324 memset(&s->blame, 0, sizeof(s->blame));
4326 SIMPLEQ_INIT(&s->colors);
4327 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4328 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4329 get_color_value("TOG_COLOR_COMMIT"));
4334 view->show = show_blame_view;
4335 view->input = input_blame_view;
4336 view->close = close_blame_view;
4337 view->search_start = search_start_blame_view;
4338 view->search_next = search_next_blame_view;
4340 return run_blame(view);
4343 static const struct got_error *
4344 close_blame_view(struct tog_view *view)
4346 const struct got_error *err = NULL;
4347 struct tog_blame_view_state *s = &view->state.blame;
4349 if (s->blame.thread)
4350 err = stop_blame(&s->blame);
4352 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4353 struct got_object_qid *blamed_commit;
4354 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4355 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4356 got_object_qid_free(blamed_commit);
4360 free_colors(&s->colors);
4365 static const struct got_error *
4366 search_start_blame_view(struct tog_view *view)
4368 struct tog_blame_view_state *s = &view->state.blame;
4370 s->matched_line = 0;
4374 static const struct got_error *
4375 search_next_blame_view(struct tog_view *view)
4377 struct tog_blame_view_state *s = &view->state.blame;
4380 size_t linesize = 0;
4383 if (!view->searching) {
4384 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4388 if (s->matched_line) {
4389 if (view->searching == TOG_SEARCH_FORWARD)
4390 lineno = s->matched_line + 1;
4392 lineno = s->matched_line - 1;
4394 if (view->searching == TOG_SEARCH_FORWARD)
4397 lineno = s->blame.nlines;
4403 if (lineno <= 0 || lineno > s->blame.nlines) {
4404 if (s->matched_line == 0) {
4405 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4409 if (view->searching == TOG_SEARCH_FORWARD)
4412 lineno = s->blame.nlines;
4415 offset = s->blame.line_offsets[lineno - 1];
4416 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4418 return got_error_from_errno("fseeko");
4420 linelen = getline(&line, &linesize, s->blame.f);
4421 if (linelen != -1 &&
4422 match_line(line, &view->regex, 1, &view->regmatch)) {
4423 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4424 s->matched_line = lineno;
4427 if (view->searching == TOG_SEARCH_FORWARD)
4434 if (s->matched_line) {
4435 s->first_displayed_line = s->matched_line;
4436 s->selected_line = 1;
4442 static const struct got_error *
4443 show_blame_view(struct tog_view *view)
4445 const struct got_error *err = NULL;
4446 struct tog_blame_view_state *s = &view->state.blame;
4449 if (s->blame.thread == NULL && !s->blame_complete) {
4450 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4451 &s->blame.thread_args);
4453 return got_error_set_errno(errcode, "pthread_create");
4455 halfdelay(1); /* fast refresh while annotating */
4458 if (s->blame_complete)
4459 halfdelay(10); /* disable fast refresh */
4461 err = draw_blame(view);
4467 static const struct got_error *
4468 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4470 const struct got_error *err = NULL, *thread_err = NULL;
4471 struct tog_view *diff_view;
4472 struct tog_blame_view_state *s = &view->state.blame;
4481 if (s->selected_line > 1)
4483 else if (s->selected_line == 1 &&
4484 s->first_displayed_line > 1)
4485 s->first_displayed_line--;
4489 if (s->first_displayed_line == 1) {
4490 s->selected_line = 1;
4493 if (s->first_displayed_line > view->nlines - 2)
4494 s->first_displayed_line -=
4497 s->first_displayed_line = 1;
4501 if (s->selected_line < view->nlines - 2 &&
4502 s->first_displayed_line +
4503 s->selected_line <= s->blame.nlines)
4505 else if (s->last_displayed_line <
4507 s->first_displayed_line++;
4511 struct got_object_id *id = NULL;
4512 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4513 s->first_displayed_line, s->selected_line);
4517 struct got_commit_object *commit;
4518 struct got_object_qid *pid;
4519 struct got_object_id *blob_id = NULL;
4521 err = got_object_open_as_commit(&commit,
4525 pid = SIMPLEQ_FIRST(
4526 got_object_commit_get_parent_ids(commit));
4528 got_object_commit_close(commit);
4531 /* Check if path history ends here. */
4532 err = got_object_id_by_path(&blob_id, s->repo,
4535 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4537 got_object_commit_close(commit);
4540 err = got_object_get_type(&obj_type, s->repo,
4543 /* Can't blame non-blob type objects. */
4544 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4545 got_object_commit_close(commit);
4548 err = got_object_qid_alloc(&s->blamed_commit,
4550 got_object_commit_close(commit);
4552 if (got_object_id_cmp(id,
4553 s->blamed_commit->id) == 0)
4555 err = got_object_qid_alloc(&s->blamed_commit,
4561 thread_err = stop_blame(&s->blame);
4565 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4566 s->blamed_commit, entry);
4567 err = run_blame(view);
4573 struct got_object_qid *first;
4574 first = SIMPLEQ_FIRST(&s->blamed_commits);
4575 if (!got_object_id_cmp(first->id, s->commit_id))
4578 thread_err = stop_blame(&s->blame);
4582 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4583 got_object_qid_free(s->blamed_commit);
4585 SIMPLEQ_FIRST(&s->blamed_commits);
4586 err = run_blame(view);
4593 struct got_object_id *id = NULL;
4594 struct got_object_qid *pid;
4595 struct got_commit_object *commit = NULL;
4596 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4597 s->first_displayed_line, s->selected_line);
4600 err = got_object_open_as_commit(&commit, s->repo, id);
4603 pid = SIMPLEQ_FIRST(
4604 got_object_commit_get_parent_ids(commit));
4605 if (view_is_parent_view(view))
4606 begin_x = view_split_begin_x(view->begin_x);
4607 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4608 if (diff_view == NULL) {
4609 got_object_commit_close(commit);
4610 err = got_error_from_errno("view_open");
4613 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4614 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4615 got_object_commit_close(commit);
4617 view_close(diff_view);
4621 diff_view->focussed = 1;
4622 if (view_is_parent_view(view)) {
4623 err = view_close_child(view);
4626 view_set_child(view, diff_view);
4627 view->focus_child = 1;
4629 *new_view = diff_view;
4637 if (s->last_displayed_line >= s->blame.nlines &&
4638 s->selected_line >= MIN(s->blame.nlines,
4639 view->nlines - 2)) {
4642 if (s->last_displayed_line >= s->blame.nlines &&
4643 s->selected_line < view->nlines - 2) {
4644 s->selected_line = MIN(s->blame.nlines,
4648 if (s->last_displayed_line + view->nlines - 2
4650 s->first_displayed_line +=
4653 s->first_displayed_line =
4658 if (s->selected_line > view->nlines - 2) {
4659 s->selected_line = MIN(s->blame.nlines,
4666 return thread_err ? thread_err : err;
4669 static const struct got_error *
4670 cmd_blame(int argc, char *argv[])
4672 const struct got_error *error;
4673 struct got_repository *repo = NULL;
4674 struct got_worktree *worktree = NULL;
4675 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4676 char *link_target = NULL;
4677 struct got_object_id *commit_id = NULL;
4678 char *commit_id_str = NULL;
4680 struct tog_view *view;
4682 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4685 commit_id_str = optarg;
4688 repo_path = realpath(optarg, NULL);
4689 if (repo_path == NULL)
4690 return got_error_from_errno2("realpath",
4705 if (repo_path == NULL) {
4706 cwd = getcwd(NULL, 0);
4708 return got_error_from_errno("getcwd");
4709 error = got_worktree_open(&worktree, cwd);
4710 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4714 strdup(got_worktree_get_repo_path(worktree));
4716 repo_path = strdup(cwd);
4717 if (repo_path == NULL) {
4718 error = got_error_from_errno("strdup");
4723 error = got_repo_open(&repo, repo_path, NULL);
4727 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4734 error = apply_unveil(got_repo_get_path(repo), NULL);
4738 error = tog_load_refs(repo);
4742 if (commit_id_str == NULL) {
4743 struct got_reference *head_ref;
4744 error = got_ref_open(&head_ref, repo, worktree ?
4745 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4748 error = got_ref_resolve(&commit_id, repo, head_ref);
4749 got_ref_close(head_ref);
4751 error = got_repo_match_object_id(&commit_id, NULL,
4752 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4757 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4759 error = got_error_from_errno("view_open");
4763 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4768 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4773 /* Release work tree lock. */
4774 got_worktree_close(worktree);
4777 error = view_loop(view);
4785 got_worktree_close(worktree);
4787 got_repo_close(repo);
4792 static const struct got_error *
4793 draw_tree_entries(struct tog_view *view, const char *parent_path)
4795 struct tog_tree_view_state *s = &view->state.tree;
4796 const struct got_error *err = NULL;
4797 struct got_tree_entry *te;
4799 struct tog_color *tc;
4800 int width, n, i, nentries;
4801 int limit = view->nlines;
4805 werase(view->window);
4810 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4813 if (view_needs_focus_indication(view))
4814 wstandout(view->window);
4815 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4817 wattr_on(view->window,
4818 COLOR_PAIR(tc->colorpair), NULL);
4819 waddwstr(view->window, wline);
4821 wattr_off(view->window,
4822 COLOR_PAIR(tc->colorpair), NULL);
4823 if (view_needs_focus_indication(view))
4824 wstandend(view->window);
4827 if (width < view->ncols - 1)
4828 waddch(view->window, '\n');
4831 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4834 waddwstr(view->window, wline);
4837 if (width < view->ncols - 1)
4838 waddch(view->window, '\n');
4841 waddch(view->window, '\n');
4845 if (s->first_displayed_entry == NULL) {
4846 te = got_object_tree_get_first_entry(s->tree);
4847 if (s->selected == 0) {
4849 wstandout(view->window);
4850 s->selected_entry = NULL;
4852 waddstr(view->window, " ..\n"); /* parent directory */
4853 if (s->selected == 0 && view->focussed)
4854 wstandend(view->window);
4861 te = s->first_displayed_entry;
4864 nentries = got_object_tree_get_nentries(s->tree);
4865 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4866 char *line = NULL, *id_str = NULL, *link_target = NULL;
4867 const char *modestr = "";
4870 te = got_object_tree_get_entry(s->tree, i);
4871 mode = got_tree_entry_get_mode(te);
4874 err = got_object_id_str(&id_str,
4875 got_tree_entry_get_id(te));
4877 return got_error_from_errno(
4878 "got_object_id_str");
4880 if (got_object_tree_entry_is_submodule(te))
4882 else if (S_ISLNK(mode)) {
4885 err = got_tree_entry_get_symlink_target(&link_target,
4891 for (i = 0; i < strlen(link_target); i++) {
4892 if (!isprint((unsigned char)link_target[i]))
4893 link_target[i] = '?';
4897 else if (S_ISDIR(mode))
4899 else if (mode & S_IXUSR)
4901 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4902 got_tree_entry_get_name(te), modestr,
4903 link_target ? " -> ": "",
4904 link_target ? link_target : "") == -1) {
4907 return got_error_from_errno("asprintf");
4911 err = format_line(&wline, &width, line, view->ncols, 0);
4916 if (n == s->selected) {
4918 wstandout(view->window);
4919 s->selected_entry = te;
4921 tc = match_color(&s->colors, line);
4923 wattr_on(view->window,
4924 COLOR_PAIR(tc->colorpair), NULL);
4925 waddwstr(view->window, wline);
4927 wattr_off(view->window,
4928 COLOR_PAIR(tc->colorpair), NULL);
4929 if (width < view->ncols - 1)
4930 waddch(view->window, '\n');
4931 if (n == s->selected && view->focussed)
4932 wstandend(view->window);
4938 s->last_displayed_entry = te;
4947 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4949 struct got_tree_entry *te;
4950 int isroot = s->tree == s->root;
4953 if (s->first_displayed_entry == NULL)
4956 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4957 while (i++ < maxscroll) {
4960 s->first_displayed_entry = NULL;
4963 s->first_displayed_entry = te;
4964 te = got_tree_entry_get_prev(s->tree, te);
4969 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4971 struct got_tree_entry *next, *last;
4974 if (s->first_displayed_entry)
4975 next = got_tree_entry_get_next(s->tree,
4976 s->first_displayed_entry);
4978 next = got_object_tree_get_first_entry(s->tree);
4980 last = s->last_displayed_entry;
4981 while (next && last && n++ < maxscroll) {
4982 last = got_tree_entry_get_next(s->tree, last);
4984 s->first_displayed_entry = next;
4985 next = got_tree_entry_get_next(s->tree, next);
4990 static const struct got_error *
4991 tree_entry_path(char **path, struct tog_parent_trees *parents,
4992 struct got_tree_entry *te)
4994 const struct got_error *err = NULL;
4995 struct tog_parent_tree *pt;
4996 size_t len = 2; /* for leading slash and NUL */
4998 TAILQ_FOREACH(pt, parents, entry)
4999 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5002 len += strlen(got_tree_entry_get_name(te));
5004 *path = calloc(1, len);
5006 return got_error_from_errno("calloc");
5009 pt = TAILQ_LAST(parents, tog_parent_trees);
5011 const char *name = got_tree_entry_get_name(pt->selected_entry);
5012 if (strlcat(*path, name, len) >= len) {
5013 err = got_error(GOT_ERR_NO_SPACE);
5016 if (strlcat(*path, "/", len) >= len) {
5017 err = got_error(GOT_ERR_NO_SPACE);
5020 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5023 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5024 err = got_error(GOT_ERR_NO_SPACE);
5036 static const struct got_error *
5037 blame_tree_entry(struct tog_view **new_view, int begin_x,
5038 struct got_tree_entry *te, struct tog_parent_trees *parents,
5039 struct got_object_id *commit_id, struct got_repository *repo)
5041 const struct got_error *err = NULL;
5043 struct tog_view *blame_view;
5047 err = tree_entry_path(&path, parents, te);
5051 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5052 if (blame_view == NULL) {
5053 err = got_error_from_errno("view_open");
5057 err = open_blame_view(blame_view, path, commit_id, repo);
5059 if (err->code == GOT_ERR_CANCELLED)
5061 view_close(blame_view);
5063 *new_view = blame_view;
5069 static const struct got_error *
5070 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5071 struct tog_tree_view_state *s)
5073 struct tog_view *log_view;
5074 const struct got_error *err = NULL;
5079 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5080 if (log_view == NULL)
5081 return got_error_from_errno("view_open");
5083 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5087 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5090 view_close(log_view);
5092 *new_view = log_view;
5097 static const struct got_error *
5098 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5099 struct got_object_id *commit_id, const char *head_ref_name,
5100 struct got_repository *repo)
5102 const struct got_error *err = NULL;
5103 char *commit_id_str = NULL;
5104 struct tog_tree_view_state *s = &view->state.tree;
5106 TAILQ_INIT(&s->parents);
5108 err = got_object_id_str(&commit_id_str, commit_id);
5112 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5113 err = got_error_from_errno("asprintf");
5117 s->root = s->tree = root;
5118 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5119 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5120 s->commit_id = got_object_id_dup(commit_id);
5121 if (s->commit_id == NULL) {
5122 err = got_error_from_errno("got_object_id_dup");
5125 if (head_ref_name) {
5126 s->head_ref_name = strdup(head_ref_name);
5127 if (s->head_ref_name == NULL) {
5128 err = got_error_from_errno("strdup");
5134 SIMPLEQ_INIT(&s->colors);
5136 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5137 err = add_color(&s->colors, "\\$$",
5138 TOG_COLOR_TREE_SUBMODULE,
5139 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5142 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5143 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5145 free_colors(&s->colors);
5148 err = add_color(&s->colors, "/$",
5149 TOG_COLOR_TREE_DIRECTORY,
5150 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5152 free_colors(&s->colors);
5156 err = add_color(&s->colors, "\\*$",
5157 TOG_COLOR_TREE_EXECUTABLE,
5158 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5160 free_colors(&s->colors);
5164 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5165 get_color_value("TOG_COLOR_COMMIT"));
5167 free_colors(&s->colors);
5172 view->show = show_tree_view;
5173 view->input = input_tree_view;
5174 view->close = close_tree_view;
5175 view->search_start = search_start_tree_view;
5176 view->search_next = search_next_tree_view;
5178 free(commit_id_str);
5180 free(s->tree_label);
5181 s->tree_label = NULL;
5186 static const struct got_error *
5187 close_tree_view(struct tog_view *view)
5189 struct tog_tree_view_state *s = &view->state.tree;
5191 free_colors(&s->colors);
5192 free(s->tree_label);
5193 s->tree_label = NULL;
5195 s->commit_id = NULL;
5196 free(s->head_ref_name);
5197 s->head_ref_name = NULL;
5198 while (!TAILQ_EMPTY(&s->parents)) {
5199 struct tog_parent_tree *parent;
5200 parent = TAILQ_FIRST(&s->parents);
5201 TAILQ_REMOVE(&s->parents, parent, entry);
5205 if (s->tree != s->root)
5206 got_object_tree_close(s->tree);
5207 got_object_tree_close(s->root);
5211 static const struct got_error *
5212 search_start_tree_view(struct tog_view *view)
5214 struct tog_tree_view_state *s = &view->state.tree;
5216 s->matched_entry = NULL;
5221 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5223 regmatch_t regmatch;
5225 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5229 static const struct got_error *
5230 search_next_tree_view(struct tog_view *view)
5232 struct tog_tree_view_state *s = &view->state.tree;
5233 struct got_tree_entry *te = NULL;
5235 if (!view->searching) {
5236 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5240 if (s->matched_entry) {
5241 if (view->searching == TOG_SEARCH_FORWARD) {
5242 if (s->selected_entry)
5243 te = got_tree_entry_get_next(s->tree,
5246 te = got_object_tree_get_first_entry(s->tree);
5248 if (s->selected_entry == NULL)
5249 te = got_object_tree_get_last_entry(s->tree);
5251 te = got_tree_entry_get_prev(s->tree,
5255 if (view->searching == TOG_SEARCH_FORWARD)
5256 te = got_object_tree_get_first_entry(s->tree);
5258 te = got_object_tree_get_last_entry(s->tree);
5263 if (s->matched_entry == NULL) {
5264 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5267 if (view->searching == TOG_SEARCH_FORWARD)
5268 te = got_object_tree_get_first_entry(s->tree);
5270 te = got_object_tree_get_last_entry(s->tree);
5273 if (match_tree_entry(te, &view->regex)) {
5274 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5275 s->matched_entry = te;
5279 if (view->searching == TOG_SEARCH_FORWARD)
5280 te = got_tree_entry_get_next(s->tree, te);
5282 te = got_tree_entry_get_prev(s->tree, te);
5285 if (s->matched_entry) {
5286 s->first_displayed_entry = s->matched_entry;
5293 static const struct got_error *
5294 show_tree_view(struct tog_view *view)
5296 const struct got_error *err = NULL;
5297 struct tog_tree_view_state *s = &view->state.tree;
5300 err = tree_entry_path(&parent_path, &s->parents, NULL);
5304 err = draw_tree_entries(view, parent_path);
5311 static const struct got_error *
5312 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5314 const struct got_error *err = NULL;
5315 struct tog_tree_view_state *s = &view->state.tree;
5316 struct tog_view *log_view, *ref_view;
5321 s->show_ids = !s->show_ids;
5324 if (!s->selected_entry)
5326 if (view_is_parent_view(view))
5327 begin_x = view_split_begin_x(view->begin_x);
5328 err = log_selected_tree_entry(&log_view, begin_x, s);
5330 log_view->focussed = 1;
5331 if (view_is_parent_view(view)) {
5332 err = view_close_child(view);
5335 view_set_child(view, log_view);
5336 view->focus_child = 1;
5338 *new_view = log_view;
5341 if (view_is_parent_view(view))
5342 begin_x = view_split_begin_x(view->begin_x);
5343 ref_view = view_open(view->nlines, view->ncols,
5344 view->begin_y, begin_x, TOG_VIEW_REF);
5345 if (ref_view == NULL)
5346 return got_error_from_errno("view_open");
5347 err = open_ref_view(ref_view, s->repo);
5349 view_close(ref_view);
5353 ref_view->focussed = 1;
5354 if (view_is_parent_view(view)) {
5355 err = view_close_child(view);
5358 view_set_child(view, ref_view);
5359 view->focus_child = 1;
5361 *new_view = ref_view;
5365 if (s->selected > 0) {
5369 tree_scroll_up(s, 1);
5373 if (s->tree == s->root) {
5374 if (got_object_tree_get_first_entry(s->tree) ==
5375 s->first_displayed_entry)
5378 if (s->first_displayed_entry == NULL)
5381 tree_scroll_up(s, MAX(0, view->nlines - 3));
5385 if (s->selected < s->ndisplayed - 1) {
5389 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5391 /* can't scroll any further */
5393 tree_scroll_down(s, 1);
5397 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5399 /* can't scroll any further; move cursor down */
5400 if (s->selected < s->ndisplayed - 1)
5401 s->selected = s->ndisplayed - 1;
5404 tree_scroll_down(s, view->nlines - 3);
5409 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5410 struct tog_parent_tree *parent;
5411 /* user selected '..' */
5412 if (s->tree == s->root)
5414 parent = TAILQ_FIRST(&s->parents);
5415 TAILQ_REMOVE(&s->parents, parent,
5417 got_object_tree_close(s->tree);
5418 s->tree = parent->tree;
5419 s->first_displayed_entry =
5420 parent->first_displayed_entry;
5422 parent->selected_entry;
5423 s->selected = parent->selected;
5425 } else if (S_ISDIR(got_tree_entry_get_mode(
5426 s->selected_entry))) {
5427 struct got_tree_object *subtree;
5428 err = got_object_open_as_tree(&subtree, s->repo,
5429 got_tree_entry_get_id(s->selected_entry));
5432 err = tree_view_visit_subtree(s, subtree);
5434 got_object_tree_close(subtree);
5437 } else if (S_ISREG(got_tree_entry_get_mode(
5438 s->selected_entry))) {
5439 struct tog_view *blame_view;
5440 int begin_x = view_is_parent_view(view) ?
5441 view_split_begin_x(view->begin_x) : 0;
5443 err = blame_tree_entry(&blame_view, begin_x,
5444 s->selected_entry, &s->parents,
5445 s->commit_id, s->repo);
5449 blame_view->focussed = 1;
5450 if (view_is_parent_view(view)) {
5451 err = view_close_child(view);
5454 view_set_child(view, blame_view);
5455 view->focus_child = 1;
5457 *new_view = blame_view;
5461 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5462 s->selected = view->nlines - 4;
5475 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5480 static const struct got_error *
5481 cmd_tree(int argc, char *argv[])
5483 const struct got_error *error;
5484 struct got_repository *repo = NULL;
5485 struct got_worktree *worktree = NULL;
5486 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5487 struct got_object_id *commit_id = NULL;
5488 const char *commit_id_arg = NULL;
5490 struct got_commit_object *commit = NULL;
5491 struct got_tree_object *tree = NULL;
5492 struct got_reference *ref = NULL;
5493 const char *head_ref_name = NULL;
5495 struct tog_view *view;
5497 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5500 commit_id_arg = optarg;
5503 repo_path = realpath(optarg, NULL);
5504 if (repo_path == NULL)
5505 return got_error_from_errno2("realpath",
5520 if (repo_path == NULL) {
5521 cwd = getcwd(NULL, 0);
5523 return got_error_from_errno("getcwd");
5524 error = got_worktree_open(&worktree, cwd);
5525 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5529 strdup(got_worktree_get_repo_path(worktree));
5531 repo_path = strdup(cwd);
5532 if (repo_path == NULL) {
5533 error = got_error_from_errno("strdup");
5538 error = got_repo_open(&repo, repo_path, NULL);
5542 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5549 error = apply_unveil(got_repo_get_path(repo), NULL);
5553 error = tog_load_refs(repo);
5557 if (commit_id_arg == NULL) {
5558 error = got_repo_match_object_id(&commit_id, &label,
5559 worktree ? got_worktree_get_head_ref_name(worktree) :
5560 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5563 head_ref_name = label;
5565 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5567 head_ref_name = got_ref_get_name(ref);
5568 else if (error->code != GOT_ERR_NOT_REF)
5570 error = got_repo_match_object_id(&commit_id, NULL,
5571 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5576 error = got_object_open_as_commit(&commit, repo, commit_id);
5580 error = got_object_open_as_tree(&tree, repo,
5581 got_object_commit_get_tree_id(commit));
5585 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5587 error = got_error_from_errno("view_open");
5590 error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5593 if (!got_path_is_root_dir(in_repo_path)) {
5594 error = tree_view_walk_path(&view->state.tree, commit_id,
5601 /* Release work tree lock. */
5602 got_worktree_close(worktree);
5605 error = view_loop(view);
5614 got_object_commit_close(commit);
5616 got_object_tree_close(tree);
5618 got_repo_close(repo);
5623 static const struct got_error *
5624 ref_view_load_refs(struct tog_ref_view_state *s)
5626 struct got_reflist_entry *sre;
5627 struct tog_reflist_entry *re;
5630 TAILQ_FOREACH(sre, &tog_refs, entry) {
5631 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5634 re = malloc(sizeof(*re));
5636 return got_error_from_errno("malloc");
5638 re->ref = got_ref_dup(sre->ref);
5639 if (re->ref == NULL)
5640 return got_error_from_errno("got_ref_dup");
5641 re->idx = s->nrefs++;
5642 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5645 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5650 ref_view_free_refs(struct tog_ref_view_state *s)
5652 struct tog_reflist_entry *re;
5654 while (!TAILQ_EMPTY(&s->refs)) {
5655 re = TAILQ_FIRST(&s->refs);
5656 TAILQ_REMOVE(&s->refs, re, entry);
5657 got_ref_close(re->ref);
5662 static const struct got_error *
5663 open_ref_view(struct tog_view *view, struct got_repository *repo)
5665 const struct got_error *err = NULL;
5666 struct tog_ref_view_state *s = &view->state.ref;
5668 s->selected_entry = 0;
5671 TAILQ_INIT(&s->refs);
5672 SIMPLEQ_INIT(&s->colors);
5674 err = ref_view_load_refs(s);
5678 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5679 err = add_color(&s->colors, "^refs/heads/",
5680 TOG_COLOR_REFS_HEADS,
5681 get_color_value("TOG_COLOR_REFS_HEADS"));
5685 err = add_color(&s->colors, "^refs/tags/",
5686 TOG_COLOR_REFS_TAGS,
5687 get_color_value("TOG_COLOR_REFS_TAGS"));
5691 err = add_color(&s->colors, "^refs/remotes/",
5692 TOG_COLOR_REFS_REMOTES,
5693 get_color_value("TOG_COLOR_REFS_REMOTES"));
5698 view->show = show_ref_view;
5699 view->input = input_ref_view;
5700 view->close = close_ref_view;
5701 view->search_start = search_start_ref_view;
5702 view->search_next = search_next_ref_view;
5705 free_colors(&s->colors);
5709 static const struct got_error *
5710 close_ref_view(struct tog_view *view)
5712 struct tog_ref_view_state *s = &view->state.ref;
5714 ref_view_free_refs(s);
5715 free_colors(&s->colors);
5720 static const struct got_error *
5721 resolve_reflist_entry(struct got_object_id **commit_id,
5722 struct tog_reflist_entry *re, struct got_repository *repo)
5724 const struct got_error *err = NULL;
5725 struct got_object_id *obj_id;
5726 struct got_tag_object *tag = NULL;
5731 err = got_ref_resolve(&obj_id, repo, re->ref);
5735 err = got_object_get_type(&obj_type, repo, obj_id);
5740 case GOT_OBJ_TYPE_COMMIT:
5741 *commit_id = obj_id;
5743 case GOT_OBJ_TYPE_TAG:
5744 err = got_object_open_as_tag(&tag, repo, obj_id);
5748 err = got_object_get_type(&obj_type, repo,
5749 got_object_tag_get_object_id(tag));
5752 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5753 err = got_error(GOT_ERR_OBJ_TYPE);
5756 *commit_id = got_object_id_dup(
5757 got_object_tag_get_object_id(tag));
5758 if (*commit_id == NULL) {
5759 err = got_error_from_errno("got_object_id_dup");
5764 err = got_error(GOT_ERR_OBJ_TYPE);
5770 got_object_tag_close(tag);
5778 static const struct got_error *
5779 log_ref_entry(struct tog_view **new_view, int begin_x,
5780 struct tog_reflist_entry *re, struct got_repository *repo)
5782 struct tog_view *log_view;
5783 const struct got_error *err = NULL;
5784 struct got_object_id *commit_id = NULL;
5788 err = resolve_reflist_entry(&commit_id, re, repo);
5790 if (err->code != GOT_ERR_OBJ_TYPE)
5796 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5797 if (log_view == NULL) {
5798 err = got_error_from_errno("view_open");
5802 err = open_log_view(log_view, commit_id, repo,
5803 got_ref_get_name(re->ref), "", 0);
5806 view_close(log_view);
5808 *new_view = log_view;
5814 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5816 struct tog_reflist_entry *re;
5819 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5822 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5823 while (i++ < maxscroll) {
5826 s->first_displayed_entry = re;
5827 re = TAILQ_PREV(re, tog_reflist_head, entry);
5832 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5834 struct tog_reflist_entry *next, *last;
5837 if (s->first_displayed_entry)
5838 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5840 next = TAILQ_FIRST(&s->refs);
5842 last = s->last_displayed_entry;
5843 while (next && last && n++ < maxscroll) {
5844 last = TAILQ_NEXT(last, entry);
5846 s->first_displayed_entry = next;
5847 next = TAILQ_NEXT(next, entry);
5852 static const struct got_error *
5853 search_start_ref_view(struct tog_view *view)
5855 struct tog_ref_view_state *s = &view->state.ref;
5857 s->matched_entry = NULL;
5862 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5864 regmatch_t regmatch;
5866 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5870 static const struct got_error *
5871 search_next_ref_view(struct tog_view *view)
5873 struct tog_ref_view_state *s = &view->state.ref;
5874 struct tog_reflist_entry *re = NULL;
5876 if (!view->searching) {
5877 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5881 if (s->matched_entry) {
5882 if (view->searching == TOG_SEARCH_FORWARD) {
5883 if (s->selected_entry)
5884 re = TAILQ_NEXT(s->selected_entry, entry);
5886 re = TAILQ_PREV(s->selected_entry,
5887 tog_reflist_head, entry);
5889 if (s->selected_entry == NULL)
5890 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5892 re = TAILQ_PREV(s->selected_entry,
5893 tog_reflist_head, entry);
5896 if (view->searching == TOG_SEARCH_FORWARD)
5897 re = TAILQ_FIRST(&s->refs);
5899 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5904 if (s->matched_entry == NULL) {
5905 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5908 if (view->searching == TOG_SEARCH_FORWARD)
5909 re = TAILQ_FIRST(&s->refs);
5911 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5914 if (match_reflist_entry(re, &view->regex)) {
5915 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5916 s->matched_entry = re;
5920 if (view->searching == TOG_SEARCH_FORWARD)
5921 re = TAILQ_NEXT(re, entry);
5923 re = TAILQ_PREV(re, tog_reflist_head, entry);
5926 if (s->matched_entry) {
5927 s->first_displayed_entry = s->matched_entry;
5934 static const struct got_error *
5935 show_ref_view(struct tog_view *view)
5937 const struct got_error *err = NULL;
5938 struct tog_ref_view_state *s = &view->state.ref;
5939 struct tog_reflist_entry *re;
5942 struct tog_color *tc;
5944 int limit = view->nlines;
5946 werase(view->window);
5953 re = s->first_displayed_entry;
5955 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5957 return got_error_from_errno("asprintf");
5959 err = format_line(&wline, &width, line, view->ncols, 0);
5964 if (view_needs_focus_indication(view))
5965 wstandout(view->window);
5966 waddwstr(view->window, wline);
5967 if (view_needs_focus_indication(view))
5968 wstandend(view->window);
5973 if (width < view->ncols - 1)
5974 waddch(view->window, '\n');
5979 while (re && limit > 0) {
5982 if (got_ref_is_symbolic(re->ref)) {
5983 if (asprintf(&line, "%s -> %s",
5984 got_ref_get_name(re->ref),
5985 got_ref_get_symref_target(re->ref)) == -1)
5986 return got_error_from_errno("asprintf");
5987 } else if (s->show_ids) {
5988 struct got_object_id *id;
5990 err = got_ref_resolve(&id, s->repo, re->ref);
5993 err = got_object_id_str(&id_str, id);
5998 if (asprintf(&line, "%s: %s",
5999 got_ref_get_name(re->ref), id_str) == -1) {
6000 err = got_error_from_errno("asprintf");
6008 line = strdup(got_ref_get_name(re->ref));
6010 return got_error_from_errno("strdup");
6013 err = format_line(&wline, &width, line, view->ncols, 0);
6018 if (n == s->selected) {
6020 wstandout(view->window);
6021 s->selected_entry = re;
6023 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6025 wattr_on(view->window,
6026 COLOR_PAIR(tc->colorpair), NULL);
6027 waddwstr(view->window, wline);
6029 wattr_off(view->window,
6030 COLOR_PAIR(tc->colorpair), NULL);
6031 if (width < view->ncols - 1)
6032 waddch(view->window, '\n');
6033 if (n == s->selected && view->focussed)
6034 wstandend(view->window);
6040 s->last_displayed_entry = re;
6043 re = TAILQ_NEXT(re, entry);
6050 static const struct got_error *
6051 browse_ref_tree(struct tog_view **new_view, int begin_x,
6052 struct tog_reflist_entry *re, struct got_repository *repo)
6054 const struct got_error *err = NULL;
6055 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6056 struct got_tree_object *tree = NULL;
6057 struct tog_view *tree_view;
6061 err = resolve_reflist_entry(&commit_id, re, repo);
6063 if (err->code != GOT_ERR_OBJ_TYPE)
6069 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6073 err = got_object_open_as_tree(&tree, repo, tree_id);
6077 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6078 if (tree_view == NULL) {
6079 err = got_error_from_errno("view_open");
6083 err = open_tree_view(tree_view, tree, commit_id,
6084 got_ref_get_name(re->ref), repo);
6088 *new_view = tree_view;
6094 got_object_tree_close(tree);
6098 static const struct got_error *
6099 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6101 const struct got_error *err = NULL;
6102 struct tog_ref_view_state *s = &view->state.ref;
6103 struct tog_view *log_view, *tree_view;
6108 s->show_ids = !s->show_ids;
6112 if (!s->selected_entry)
6114 if (view_is_parent_view(view))
6115 begin_x = view_split_begin_x(view->begin_x);
6116 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6119 log_view->focussed = 1;
6120 if (view_is_parent_view(view)) {
6121 err = view_close_child(view);
6124 view_set_child(view, log_view);
6125 view->focus_child = 1;
6127 *new_view = log_view;
6130 if (!s->selected_entry)
6132 if (view_is_parent_view(view))
6133 begin_x = view_split_begin_x(view->begin_x);
6134 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6136 if (err || tree_view == NULL)
6139 tree_view->focussed = 1;
6140 if (view_is_parent_view(view)) {
6141 err = view_close_child(view);
6144 view_set_child(view, tree_view);
6145 view->focus_child = 1;
6147 *new_view = tree_view;
6151 if (s->selected > 0) {
6155 ref_scroll_up(s, 1);
6159 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6161 ref_scroll_up(s, MAX(0, view->nlines - 1));
6165 if (s->selected < s->ndisplayed - 1) {
6169 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6170 /* can't scroll any further */
6172 ref_scroll_down(s, 1);
6176 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6177 /* can't scroll any further; move cursor down */
6178 if (s->selected < s->ndisplayed - 1)
6179 s->selected = s->ndisplayed - 1;
6182 ref_scroll_down(s, view->nlines - 1);
6186 err = tog_load_refs(s->repo);
6189 ref_view_free_refs(s);
6190 err = ref_view_load_refs(s);
6193 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6194 s->selected = view->nlines - 2;
6207 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6212 static const struct got_error *
6213 cmd_ref(int argc, char *argv[])
6215 const struct got_error *error;
6216 struct got_repository *repo = NULL;
6217 struct got_worktree *worktree = NULL;
6218 char *cwd = NULL, *repo_path = NULL;
6220 struct tog_view *view;
6222 while ((ch = getopt(argc, argv, "r:")) != -1) {
6225 repo_path = realpath(optarg, NULL);
6226 if (repo_path == NULL)
6227 return got_error_from_errno2("realpath",
6242 if (repo_path == NULL) {
6243 cwd = getcwd(NULL, 0);
6245 return got_error_from_errno("getcwd");
6246 error = got_worktree_open(&worktree, cwd);
6247 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6251 strdup(got_worktree_get_repo_path(worktree));
6253 repo_path = strdup(cwd);
6254 if (repo_path == NULL) {
6255 error = got_error_from_errno("strdup");
6260 error = got_repo_open(&repo, repo_path, NULL);
6266 error = apply_unveil(got_repo_get_path(repo), NULL);
6270 error = tog_load_refs(repo);
6274 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6276 error = got_error_from_errno("view_open");
6280 error = open_ref_view(view, repo);
6285 /* Release work tree lock. */
6286 got_worktree_close(worktree);
6289 error = view_loop(view);
6294 got_repo_close(repo);
6300 list_commands(FILE *fp)
6304 fprintf(fp, "commands:");
6305 for (i = 0; i < nitems(tog_commands); i++) {
6306 struct tog_cmd *cmd = &tog_commands[i];
6307 fprintf(fp, " %s", cmd->name);
6313 usage(int hflag, int status)
6315 FILE *fp = (status == 0) ? stdout : stderr;
6317 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6320 fprintf(fp, "lazy usage: %s path\n", getprogname());
6327 make_argv(int argc, ...)
6335 argv = calloc(argc, sizeof(char *));
6338 for (i = 0; i < argc; i++) {
6339 argv[i] = strdup(va_arg(ap, char *));
6340 if (argv[i] == NULL)
6349 * Try to convert 'tog path' into a 'tog log path' command.
6350 * The user could simply have mistyped the command rather than knowingly
6351 * provided a path. So check whether argv[0] can in fact be resolved
6352 * to a path in the HEAD commit and print a special error if not.
6353 * This hack is for mpi@ <3
6355 static const struct got_error *
6356 tog_log_with_path(int argc, char *argv[])
6358 const struct got_error *error = NULL;
6359 struct tog_cmd *cmd = NULL;
6360 struct got_repository *repo = NULL;
6361 struct got_worktree *worktree = NULL;
6362 struct got_object_id *commit_id = NULL, *id = NULL;
6363 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6364 char *commit_id_str = NULL, **cmd_argv = NULL;
6366 cwd = getcwd(NULL, 0);
6368 return got_error_from_errno("getcwd");
6370 error = got_worktree_open(&worktree, cwd);
6371 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6375 repo_path = strdup(got_worktree_get_repo_path(worktree));
6377 repo_path = strdup(cwd);
6378 if (repo_path == NULL) {
6379 error = got_error_from_errno("strdup");
6383 error = got_repo_open(&repo, repo_path, NULL);
6387 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6392 error = tog_load_refs(repo);
6395 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6396 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6397 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6402 got_worktree_close(worktree);
6406 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6408 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6410 fprintf(stderr, "%s: '%s' is no known command or path\n",
6411 getprogname(), argv[0]);
6416 got_repo_close(repo);
6419 error = got_object_id_str(&commit_id_str, commit_id);
6423 cmd = &tog_commands[0]; /* log */
6425 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6426 error = cmd->cmd_main(argc, cmd_argv);
6429 got_repo_close(repo);
6431 got_worktree_close(worktree);
6433 free(commit_id_str);
6440 for (i = 0; i < argc; i++)
6449 main(int argc, char *argv[])
6451 const struct got_error *error = NULL;
6452 struct tog_cmd *cmd = NULL;
6453 int ch, hflag = 0, Vflag = 0;
6454 char **cmd_argv = NULL;
6455 static struct option longopts[] = {
6456 { "version", no_argument, NULL, 'V' },
6460 setlocale(LC_CTYPE, "");
6462 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6482 got_version_print_str();
6487 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6495 /* Build an argument vector which runs a default command. */
6496 cmd = &tog_commands[0];
6498 cmd_argv = make_argv(argc, cmd->name);
6502 /* Did the user specify a command? */
6503 for (i = 0; i < nitems(tog_commands); i++) {
6504 if (strncmp(tog_commands[i].name, argv[0],
6505 strlen(argv[0])) == 0) {
6506 cmd = &tog_commands[i];
6515 /* No command specified; try log with a path */
6516 error = tog_log_with_path(argc, argv);
6521 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6528 for (i = 0; i < argc; i++)
6533 if (error && error->code != GOT_ERR_CANCELLED)
6534 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);