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 *);
506 #define TOG_SEARCH_FORWARD 1
507 #define TOG_SEARCH_BACKWARD 2
508 int search_next_done;
509 #define TOG_SEARCH_HAVE_MORE 1
510 #define TOG_SEARCH_NO_MORE 2
511 #define TOG_SEARCH_HAVE_NONE 3
516 static const struct got_error *open_diff_view(struct tog_view *,
517 struct got_object_id *, struct got_object_id *,
518 const char *, const char *, int, int, int, struct tog_view *,
519 struct got_repository *);
520 static const struct got_error *show_diff_view(struct tog_view *);
521 static const struct got_error *input_diff_view(struct tog_view **,
522 struct tog_view *, int);
523 static const struct got_error* close_diff_view(struct tog_view *);
524 static const struct got_error *search_start_diff_view(struct tog_view *);
525 static const struct got_error *search_next_diff_view(struct tog_view *);
527 static const struct got_error *open_log_view(struct tog_view *,
528 struct got_object_id *, struct got_repository *,
529 const char *, const char *, int);
530 static const struct got_error * show_log_view(struct tog_view *);
531 static const struct got_error *input_log_view(struct tog_view **,
532 struct tog_view *, int);
533 static const struct got_error *close_log_view(struct tog_view *);
534 static const struct got_error *search_start_log_view(struct tog_view *);
535 static const struct got_error *search_next_log_view(struct tog_view *);
537 static const struct got_error *open_blame_view(struct tog_view *, char *,
538 struct got_object_id *, struct got_repository *);
539 static const struct got_error *show_blame_view(struct tog_view *);
540 static const struct got_error *input_blame_view(struct tog_view **,
541 struct tog_view *, int);
542 static const struct got_error *close_blame_view(struct tog_view *);
543 static const struct got_error *search_start_blame_view(struct tog_view *);
544 static const struct got_error *search_next_blame_view(struct tog_view *);
546 static const struct got_error *open_tree_view(struct tog_view *,
547 struct got_tree_object *, struct got_object_id *, const char *,
548 struct got_repository *);
549 static const struct got_error *show_tree_view(struct tog_view *);
550 static const struct got_error *input_tree_view(struct tog_view **,
551 struct tog_view *, int);
552 static const struct got_error *close_tree_view(struct tog_view *);
553 static const struct got_error *search_start_tree_view(struct tog_view *);
554 static const struct got_error *search_next_tree_view(struct tog_view *);
556 static const struct got_error *open_ref_view(struct tog_view *,
557 struct got_repository *);
558 static const struct got_error *show_ref_view(struct tog_view *);
559 static const struct got_error *input_ref_view(struct tog_view **,
560 struct tog_view *, int);
561 static const struct got_error *close_ref_view(struct tog_view *);
562 static const struct got_error *search_start_ref_view(struct tog_view *);
563 static const struct got_error *search_next_ref_view(struct tog_view *);
565 static volatile sig_atomic_t tog_sigwinch_received;
566 static volatile sig_atomic_t tog_sigpipe_received;
567 static volatile sig_atomic_t tog_sigcont_received;
570 tog_sigwinch(int signo)
572 tog_sigwinch_received = 1;
576 tog_sigpipe(int signo)
578 tog_sigpipe_received = 1;
582 tog_sigcont(int signo)
584 tog_sigcont_received = 1;
587 static const struct got_error *
588 view_close(struct tog_view *view)
590 const struct got_error *err = NULL;
593 view_close(view->child);
597 err = view->close(view);
599 del_panel(view->panel);
601 delwin(view->window);
606 static struct tog_view *
607 view_open(int nlines, int ncols, int begin_y, int begin_x,
608 enum tog_view_type type)
610 struct tog_view *view = calloc(1, sizeof(*view));
618 view->nlines = nlines ? nlines : LINES - begin_y;
619 view->ncols = ncols ? ncols : COLS - begin_x;
620 view->begin_y = begin_y;
621 view->begin_x = begin_x;
622 view->window = newwin(nlines, ncols, begin_y, begin_x);
623 if (view->window == NULL) {
627 view->panel = new_panel(view->window);
628 if (view->panel == NULL ||
629 set_panel_userptr(view->panel, view) != OK) {
634 keypad(view->window, TRUE);
639 view_split_begin_x(int begin_x)
641 if (begin_x > 0 || COLS < 120)
643 return (COLS - MAX(COLS / 2, 80));
646 static const struct got_error *view_resize(struct tog_view *);
648 static const struct got_error *
649 view_splitscreen(struct tog_view *view)
651 const struct got_error *err = NULL;
654 view->begin_x = view_split_begin_x(0);
655 view->nlines = LINES;
656 view->ncols = COLS - view->begin_x;
659 err = view_resize(view);
663 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
664 return got_error_from_errno("mvwin");
669 static const struct got_error *
670 view_fullscreen(struct tog_view *view)
672 const struct got_error *err = NULL;
676 view->nlines = LINES;
680 err = view_resize(view);
684 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
685 return got_error_from_errno("mvwin");
691 view_is_parent_view(struct tog_view *view)
693 return view->parent == NULL;
696 static const struct got_error *
697 view_resize(struct tog_view *view)
701 if (view->lines > LINES)
702 nlines = view->nlines - (view->lines - LINES);
704 nlines = view->nlines + (LINES - view->lines);
706 if (view->cols > COLS)
707 ncols = view->ncols - (view->cols - COLS);
709 ncols = view->ncols + (COLS - view->cols);
711 if (wresize(view->window, nlines, ncols) == ERR)
712 return got_error_from_errno("wresize");
713 if (replace_panel(view->panel, view->window) == ERR)
714 return got_error_from_errno("replace_panel");
715 wclear(view->window);
717 view->nlines = nlines;
723 view->child->begin_x = view_split_begin_x(view->begin_x);
724 if (view->child->begin_x == 0) {
725 view_fullscreen(view->child);
726 if (view->child->focussed)
727 show_panel(view->child->panel);
729 show_panel(view->panel);
731 view_splitscreen(view->child);
732 show_panel(view->child->panel);
739 static const struct got_error *
740 view_close_child(struct tog_view *view)
742 const struct got_error *err = NULL;
744 if (view->child == NULL)
747 err = view_close(view->child);
753 view_set_child(struct tog_view *view, struct tog_view *child)
756 child->parent = view;
760 view_is_splitscreen(struct tog_view *view)
762 return view->begin_x > 0;
771 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
772 cols = 80; /* Default */
778 resize_term(lines, cols);
781 static const struct got_error *
782 view_search_start(struct tog_view *view)
784 const struct got_error *err = NULL;
788 if (view->nlines < 1)
791 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
792 wclrtoeol(view->window);
796 ret = wgetnstr(view->window, pattern, sizeof(pattern));
802 if (view->searching) {
803 regfree(&view->regex);
807 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
808 err = view->search_start(view);
810 regfree(&view->regex);
813 view->searching = TOG_SEARCH_FORWARD;
814 view->search_next_done = 0;
815 view->search_next(view);
821 static const struct got_error *
822 view_input(struct tog_view **new, int *done, struct tog_view *view,
823 struct tog_view_list_head *views)
825 const struct got_error *err = NULL;
831 /* Clear "no matches" indicator. */
832 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
833 view->search_next_done == TOG_SEARCH_HAVE_NONE)
834 view->search_next_done = TOG_SEARCH_HAVE_MORE;
836 if (view->searching && !view->search_next_done) {
837 errcode = pthread_mutex_unlock(&tog_mutex);
839 return got_error_set_errno(errcode,
840 "pthread_mutex_unlock");
842 errcode = pthread_mutex_lock(&tog_mutex);
844 return got_error_set_errno(errcode,
845 "pthread_mutex_lock");
846 view->search_next(view);
850 nodelay(stdscr, FALSE);
851 /* Allow threads to make progress while we are waiting for input. */
852 errcode = pthread_mutex_unlock(&tog_mutex);
854 return got_error_set_errno(errcode, "pthread_mutex_unlock");
855 ch = wgetch(view->window);
856 errcode = pthread_mutex_lock(&tog_mutex);
858 return got_error_set_errno(errcode, "pthread_mutex_lock");
859 nodelay(stdscr, TRUE);
861 if (tog_sigwinch_received || tog_sigcont_received) {
863 tog_sigwinch_received = 0;
864 tog_sigcont_received = 0;
865 TAILQ_FOREACH(v, views, entry) {
866 err = view_resize(v);
869 err = v->input(new, v, KEY_RESIZE);
873 err = view_resize(v->child);
876 err = v->child->input(new, v->child,
890 view->child->focussed = 1;
891 view->focus_child = 1;
892 } else if (view->parent) {
894 view->parent->focussed = 1;
895 view->parent->focus_child = 0;
899 err = view->input(new, view, ch);
906 if (view_is_parent_view(view)) {
907 if (view->child == NULL)
909 if (view_is_splitscreen(view->child)) {
911 view->child->focussed = 1;
912 err = view_fullscreen(view->child);
914 err = view_splitscreen(view->child);
917 err = view->child->input(new, view->child,
920 if (view_is_splitscreen(view)) {
921 view->parent->focussed = 0;
923 err = view_fullscreen(view);
925 err = view_splitscreen(view);
929 err = view->input(new, view, KEY_RESIZE);
935 if (view->search_start)
936 view_search_start(view);
938 err = view->input(new, view, ch);
942 if (view->search_next) {
943 view->searching = (ch == 'n' ?
944 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
945 view->search_next_done = 0;
946 view->search_next(view);
948 err = view->input(new, view, ch);
951 err = view->input(new, view, ch);
959 view_vborder(struct tog_view *view)
962 const struct tog_view *view_above;
965 return view_vborder(view->parent);
967 panel = panel_above(view->panel);
971 view_above = panel_userptr(panel);
972 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
973 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
977 view_needs_focus_indication(struct tog_view *view)
979 if (view_is_parent_view(view)) {
980 if (view->child == NULL || view->child->focussed)
982 if (!view_is_splitscreen(view->child))
984 } else if (!view_is_splitscreen(view))
987 return view->focussed;
990 static const struct got_error *
991 view_loop(struct tog_view *view)
993 const struct got_error *err = NULL;
994 struct tog_view_list_head views;
995 struct tog_view *new_view;
996 int fast_refresh = 10;
997 int done = 0, errcode;
999 errcode = pthread_mutex_lock(&tog_mutex);
1001 return got_error_set_errno(errcode, "pthread_mutex_lock");
1004 TAILQ_INSERT_HEAD(&views, view, entry);
1007 err = view->show(view);
1012 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1013 /* Refresh fast during initialization, then become slower. */
1014 if (fast_refresh && fast_refresh-- == 0)
1015 halfdelay(10); /* switch to once per second */
1017 err = view_input(&new_view, &done, view, &views);
1021 struct tog_view *v, *prev = NULL;
1023 if (view_is_parent_view(view))
1024 prev = TAILQ_PREV(view, tog_view_list_head,
1026 else if (view->parent)
1027 prev = view->parent;
1030 view->parent->child = NULL;
1031 view->parent->focus_child = 0;
1033 TAILQ_REMOVE(&views, view, entry);
1035 err = view_close(view);
1040 TAILQ_FOREACH(v, &views, entry) {
1044 if (view == NULL && new_view == NULL) {
1045 /* No view has focus. Try to pick one. */
1048 else if (!TAILQ_EMPTY(&views)) {
1049 view = TAILQ_LAST(&views,
1050 tog_view_list_head);
1053 if (view->focus_child) {
1054 view->child->focussed = 1;
1062 struct tog_view *v, *t;
1063 /* Only allow one parent view per type. */
1064 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1065 if (v->type != new_view->type)
1067 TAILQ_REMOVE(&views, v, entry);
1068 err = view_close(v);
1073 TAILQ_INSERT_TAIL(&views, new_view, entry);
1077 if (view_is_parent_view(view)) {
1078 if (view->child && view->child->focussed)
1081 if (view->parent && view->parent->focussed)
1082 view = view->parent;
1084 show_panel(view->panel);
1085 if (view->child && view_is_splitscreen(view->child))
1086 show_panel(view->child->panel);
1087 if (view->parent && view_is_splitscreen(view)) {
1088 err = view->parent->show(view->parent);
1092 err = view->show(view);
1096 err = view->child->show(view->child);
1105 while (!TAILQ_EMPTY(&views)) {
1106 view = TAILQ_FIRST(&views);
1107 TAILQ_REMOVE(&views, view, entry);
1111 errcode = pthread_mutex_unlock(&tog_mutex);
1112 if (errcode && err == NULL)
1113 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1123 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1128 /* Create newly allocated wide-character string equivalent to a byte string. */
1129 static const struct got_error *
1130 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1133 const struct got_error *err = NULL;
1136 *wlen = mbstowcs(NULL, s, 0);
1137 if (*wlen == (size_t)-1) {
1139 if (errno != EILSEQ)
1140 return got_error_from_errno("mbstowcs");
1142 /* byte string invalid in current encoding; try to "fix" it */
1143 err = got_mbsavis(&vis, &vislen, s);
1146 *wlen = mbstowcs(NULL, vis, 0);
1147 if (*wlen == (size_t)-1) {
1148 err = got_error_from_errno("mbstowcs"); /* give up */
1153 *ws = calloc(*wlen + 1, sizeof(**ws));
1155 err = got_error_from_errno("calloc");
1159 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1160 err = got_error_from_errno("mbstowcs");
1171 /* Format a line for display, ensuring that it won't overflow a width limit. */
1172 static const struct got_error *
1173 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1176 const struct got_error *err = NULL;
1178 wchar_t *wline = NULL;
1185 err = mbs2ws(&wline, &wlen, line);
1189 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1190 wline[wlen - 1] = L'\0';
1193 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1194 wline[wlen - 1] = L'\0';
1200 int width = wcwidth(wline[i]);
1207 if (width == 1 || width == 2) {
1208 if (cols + width > wlimit)
1212 } else if (width == -1) {
1213 if (wline[i] == L'\t') {
1215 ((cols + col_tab_align) % TABSIZE);
1220 if (cols + width > wlimit)
1225 err = got_error_from_errno("wcwidth");
1240 static const struct got_error*
1241 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1242 struct got_object_id *id, struct got_repository *repo)
1244 static const struct got_error *err = NULL;
1245 struct got_reflist_entry *re;
1251 TAILQ_FOREACH(re, refs, entry) {
1252 struct got_tag_object *tag = NULL;
1253 struct got_object_id *ref_id;
1256 name = got_ref_get_name(re->ref);
1257 if (strcmp(name, GOT_REF_HEAD) == 0)
1259 if (strncmp(name, "refs/", 5) == 0)
1261 if (strncmp(name, "got/", 4) == 0)
1263 if (strncmp(name, "heads/", 6) == 0)
1265 if (strncmp(name, "remotes/", 8) == 0) {
1267 s = strstr(name, "/" GOT_REF_HEAD);
1268 if (s != NULL && s[strlen(s)] == '\0')
1271 err = got_ref_resolve(&ref_id, repo, re->ref);
1274 if (strncmp(name, "tags/", 5) == 0) {
1275 err = got_object_open_as_tag(&tag, repo, ref_id);
1277 if (err->code != GOT_ERR_OBJ_TYPE) {
1281 /* Ref points at something other than a tag. */
1286 cmp = got_object_id_cmp(tag ?
1287 got_object_tag_get_object_id(tag) : ref_id, id);
1290 got_object_tag_close(tag);
1294 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1295 s ? ", " : "", name) == -1) {
1296 err = got_error_from_errno("asprintf");
1307 static const struct got_error *
1308 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1313 smallerthan = strchr(author, '<');
1314 if (smallerthan && smallerthan[1] != '\0')
1315 author = smallerthan + 1;
1316 author[strcspn(author, "@>")] = '\0';
1317 return format_line(wauthor, author_width, author, limit, col_tab_align);
1320 static const struct got_error *
1321 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1322 struct got_object_id *id, const size_t date_display_cols,
1323 int author_display_cols)
1325 struct tog_log_view_state *s = &view->state.log;
1326 const struct got_error *err = NULL;
1327 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1328 char *logmsg0 = NULL, *logmsg = NULL;
1329 char *author = NULL;
1330 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1331 int author_width, logmsg_width;
1332 char *newline, *line = NULL;
1334 const int avail = view->ncols;
1336 time_t committer_time;
1337 struct tog_color *tc;
1339 committer_time = got_object_commit_get_committer_time(commit);
1340 if (localtime_r(&committer_time, &tm) == NULL)
1341 return got_error_from_errno("localtime_r");
1342 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1344 return got_error(GOT_ERR_NO_SPACE);
1346 if (avail <= date_display_cols)
1347 limit = MIN(sizeof(datebuf) - 1, avail);
1349 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1350 tc = get_color(&s->colors, TOG_COLOR_DATE);
1352 wattr_on(view->window,
1353 COLOR_PAIR(tc->colorpair), NULL);
1354 waddnstr(view->window, datebuf, limit);
1356 wattr_off(view->window,
1357 COLOR_PAIR(tc->colorpair), NULL);
1364 err = got_object_id_str(&id_str, id);
1367 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1369 wattr_on(view->window,
1370 COLOR_PAIR(tc->colorpair), NULL);
1371 wprintw(view->window, "%.8s ", id_str);
1373 wattr_off(view->window,
1374 COLOR_PAIR(tc->colorpair), NULL);
1381 author = strdup(got_object_commit_get_author(commit));
1382 if (author == NULL) {
1383 err = got_error_from_errno("strdup");
1386 err = format_author(&wauthor, &author_width, author, avail - col, col);
1389 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1391 wattr_on(view->window,
1392 COLOR_PAIR(tc->colorpair), NULL);
1393 waddwstr(view->window, wauthor);
1395 wattr_off(view->window,
1396 COLOR_PAIR(tc->colorpair), NULL);
1397 col += author_width;
1398 while (col < avail && author_width < author_display_cols + 2) {
1399 waddch(view->window, ' ');
1406 err = got_object_commit_get_logmsg(&logmsg0, commit);
1410 while (*logmsg == '\n')
1412 newline = strchr(logmsg, '\n');
1415 limit = avail - col;
1416 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1419 waddwstr(view->window, wlogmsg);
1420 col += logmsg_width;
1421 while (col < avail) {
1422 waddch(view->window, ' ');
1434 static struct commit_queue_entry *
1435 alloc_commit_queue_entry(struct got_commit_object *commit,
1436 struct got_object_id *id)
1438 struct commit_queue_entry *entry;
1440 entry = calloc(1, sizeof(*entry));
1445 entry->commit = commit;
1450 pop_commit(struct commit_queue *commits)
1452 struct commit_queue_entry *entry;
1454 entry = TAILQ_FIRST(&commits->head);
1455 TAILQ_REMOVE(&commits->head, entry, entry);
1456 got_object_commit_close(entry->commit);
1457 commits->ncommits--;
1458 /* Don't free entry->id! It is owned by the commit graph. */
1463 free_commits(struct commit_queue *commits)
1465 while (!TAILQ_EMPTY(&commits->head))
1466 pop_commit(commits);
1469 static const struct got_error *
1470 match_commit(int *have_match, struct got_object_id *id,
1471 struct got_commit_object *commit, regex_t *regex)
1473 const struct got_error *err = NULL;
1474 regmatch_t regmatch;
1475 char *id_str = NULL, *logmsg = NULL;
1479 err = got_object_id_str(&id_str, id);
1483 err = got_object_commit_get_logmsg(&logmsg, commit);
1487 if (regexec(regex, got_object_commit_get_author(commit), 1,
1488 ®match, 0) == 0 ||
1489 regexec(regex, got_object_commit_get_committer(commit), 1,
1490 ®match, 0) == 0 ||
1491 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1492 regexec(regex, logmsg, 1, ®match, 0) == 0)
1500 static const struct got_error *
1501 queue_commits(struct tog_log_thread_args *a)
1503 const struct got_error *err = NULL;
1506 * We keep all commits open throughout the lifetime of the log
1507 * view in order to avoid having to re-fetch commits from disk
1508 * while updating the display.
1511 struct got_object_id *id;
1512 struct got_commit_object *commit;
1513 struct commit_queue_entry *entry;
1516 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1518 if (err || id == NULL)
1521 err = got_object_open_as_commit(&commit, a->repo, id);
1524 entry = alloc_commit_queue_entry(commit, id);
1525 if (entry == NULL) {
1526 err = got_error_from_errno("alloc_commit_queue_entry");
1530 errcode = pthread_mutex_lock(&tog_mutex);
1532 err = got_error_set_errno(errcode,
1533 "pthread_mutex_lock");
1537 entry->idx = a->commits->ncommits;
1538 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1539 a->commits->ncommits++;
1541 if (*a->searching == TOG_SEARCH_FORWARD &&
1542 !*a->search_next_done) {
1544 err = match_commit(&have_match, id, commit, a->regex);
1548 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1551 errcode = pthread_mutex_unlock(&tog_mutex);
1552 if (errcode && err == NULL)
1553 err = got_error_set_errno(errcode,
1554 "pthread_mutex_unlock");
1557 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1563 select_commit(struct tog_log_view_state *s)
1565 struct commit_queue_entry *entry;
1568 entry = s->first_displayed_entry;
1570 if (ncommits == s->selected) {
1571 s->selected_entry = entry;
1574 entry = TAILQ_NEXT(entry, entry);
1579 static const struct got_error *
1580 draw_commits(struct tog_view *view)
1582 const struct got_error *err = NULL;
1583 struct tog_log_view_state *s = &view->state.log;
1584 struct commit_queue_entry *entry = s->selected_entry;
1585 const int limit = view->nlines;
1587 int ncommits, author_cols = 4;
1588 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1589 char *refs_str = NULL;
1591 struct tog_color *tc;
1592 static const size_t date_display_cols = 12;
1594 if (s->selected_entry &&
1595 !(view->searching && view->search_next_done == 0)) {
1596 struct got_reflist_head *refs;
1597 err = got_object_id_str(&id_str, s->selected_entry->id);
1600 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1601 s->selected_entry->id);
1603 err = build_refs_str(&refs_str, refs,
1604 s->selected_entry->id, s->repo);
1610 if (s->thread_args.commits_needed == 0)
1611 halfdelay(10); /* disable fast refresh */
1613 if (s->thread_args.commits_needed > 0) {
1614 if (asprintf(&ncommits_str, " [%d/%d] %s",
1615 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1616 (view->searching && !view->search_next_done) ?
1617 "searching..." : "loading...") == -1) {
1618 err = got_error_from_errno("asprintf");
1622 const char *search_str = NULL;
1624 if (view->searching) {
1625 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1626 search_str = "no more matches";
1627 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1628 search_str = "no matches found";
1629 else if (!view->search_next_done)
1630 search_str = "searching...";
1633 if (asprintf(&ncommits_str, " [%d/%d] %s",
1634 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1635 search_str ? search_str :
1636 (refs_str ? refs_str : "")) == -1) {
1637 err = got_error_from_errno("asprintf");
1642 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1643 if (asprintf(&header, "commit %s %s%s",
1644 id_str ? id_str : "........................................",
1645 s->in_repo_path, ncommits_str) == -1) {
1646 err = got_error_from_errno("asprintf");
1650 } else if (asprintf(&header, "commit %s%s",
1651 id_str ? id_str : "........................................",
1652 ncommits_str) == -1) {
1653 err = got_error_from_errno("asprintf");
1657 err = format_line(&wline, &width, header, view->ncols, 0);
1661 werase(view->window);
1663 if (view_needs_focus_indication(view))
1664 wstandout(view->window);
1665 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1667 wattr_on(view->window,
1668 COLOR_PAIR(tc->colorpair), NULL);
1669 waddwstr(view->window, wline);
1671 wattr_off(view->window,
1672 COLOR_PAIR(tc->colorpair), NULL);
1673 while (width < view->ncols) {
1674 waddch(view->window, ' ');
1677 if (view_needs_focus_indication(view))
1678 wstandend(view->window);
1683 /* Grow author column size if necessary. */
1684 entry = s->first_displayed_entry;
1690 if (ncommits >= limit - 1)
1692 author = strdup(got_object_commit_get_author(entry->commit));
1693 if (author == NULL) {
1694 err = got_error_from_errno("strdup");
1697 err = format_author(&wauthor, &width, author, COLS,
1699 if (author_cols < width)
1700 author_cols = width;
1704 entry = TAILQ_NEXT(entry, entry);
1707 entry = s->first_displayed_entry;
1708 s->last_displayed_entry = s->first_displayed_entry;
1711 if (ncommits >= limit - 1)
1713 if (ncommits == s->selected)
1714 wstandout(view->window);
1715 err = draw_commit(view, entry->commit, entry->id,
1716 date_display_cols, author_cols);
1717 if (ncommits == s->selected)
1718 wstandend(view->window);
1722 s->last_displayed_entry = entry;
1723 entry = TAILQ_NEXT(entry, entry);
1736 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1738 struct commit_queue_entry *entry;
1741 entry = TAILQ_FIRST(&s->commits.head);
1742 if (s->first_displayed_entry == entry)
1745 entry = s->first_displayed_entry;
1746 while (entry && nscrolled < maxscroll) {
1747 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1749 s->first_displayed_entry = entry;
1755 static const struct got_error *
1756 trigger_log_thread(struct tog_view *view, int wait)
1758 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1761 halfdelay(1); /* fast refresh while loading commits */
1763 while (ta->commits_needed > 0) {
1764 if (ta->log_complete)
1767 /* Wake the log thread. */
1768 errcode = pthread_cond_signal(&ta->need_commits);
1770 return got_error_set_errno(errcode,
1771 "pthread_cond_signal");
1774 * The mutex will be released while the view loop waits
1775 * in wgetch(), at which time the log thread will run.
1780 /* Display progress update in log view. */
1781 show_log_view(view);
1785 /* Wait right here while next commit is being loaded. */
1786 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1788 return got_error_set_errno(errcode,
1789 "pthread_cond_wait");
1791 /* Display progress update in log view. */
1792 show_log_view(view);
1800 static const struct got_error *
1801 log_scroll_down(struct tog_view *view, int maxscroll)
1803 struct tog_log_view_state *s = &view->state.log;
1804 const struct got_error *err = NULL;
1805 struct commit_queue_entry *pentry;
1806 int nscrolled = 0, ncommits_needed;
1808 if (s->last_displayed_entry == NULL)
1811 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1812 if (s->commits.ncommits < ncommits_needed &&
1813 !s->thread_args.log_complete) {
1815 * Ask the log thread for required amount of commits.
1817 s->thread_args.commits_needed += maxscroll;
1818 err = trigger_log_thread(view, 1);
1824 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1828 s->last_displayed_entry = pentry;
1830 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1833 s->first_displayed_entry = pentry;
1834 } while (++nscrolled < maxscroll);
1839 static const struct got_error *
1840 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1841 struct got_commit_object *commit, struct got_object_id *commit_id,
1842 struct tog_view *log_view, struct got_repository *repo)
1844 const struct got_error *err;
1845 struct got_object_qid *parent_id;
1846 struct tog_view *diff_view;
1848 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1849 if (diff_view == NULL)
1850 return got_error_from_errno("view_open");
1852 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1853 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1854 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1856 *new_view = diff_view;
1860 static const struct got_error *
1861 tree_view_visit_subtree(struct tog_tree_view_state *s,
1862 struct got_tree_object *subtree)
1864 struct tog_parent_tree *parent;
1866 parent = calloc(1, sizeof(*parent));
1868 return got_error_from_errno("calloc");
1870 parent->tree = s->tree;
1871 parent->first_displayed_entry = s->first_displayed_entry;
1872 parent->selected_entry = s->selected_entry;
1873 parent->selected = s->selected;
1874 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1877 s->first_displayed_entry = NULL;
1881 static const struct got_error *
1882 tree_view_walk_path(struct tog_tree_view_state *s,
1883 struct got_object_id *commit_id, const char *path)
1885 const struct got_error *err = NULL;
1886 struct got_tree_object *tree = NULL;
1888 char *slash, *subpath = NULL;
1890 /* Walk the path and open corresponding tree objects. */
1893 struct got_tree_entry *te;
1894 struct got_object_id *tree_id;
1900 /* Ensure the correct subtree entry is selected. */
1901 slash = strchr(p, '/');
1903 te_name = strdup(p);
1905 te_name = strndup(p, slash - p);
1906 if (te_name == NULL) {
1907 err = got_error_from_errno("strndup");
1910 te = got_object_tree_find_entry(s->tree, te_name);
1912 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1917 s->first_displayed_entry = s->selected_entry = te;
1919 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1920 break; /* jump to this file's entry */
1922 slash = strchr(p, '/');
1924 subpath = strndup(path, slash - path);
1926 subpath = strdup(path);
1927 if (subpath == NULL) {
1928 err = got_error_from_errno("strdup");
1932 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1937 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1942 err = tree_view_visit_subtree(s, tree);
1944 got_object_tree_close(tree);
1958 static const struct got_error *
1959 browse_commit_tree(struct tog_view **new_view, int begin_x,
1960 struct commit_queue_entry *entry, const char *path,
1961 const char *head_ref_name, struct got_repository *repo)
1963 const struct got_error *err = NULL;
1964 struct got_tree_object *tree;
1965 struct tog_tree_view_state *s;
1966 struct tog_view *tree_view;
1968 err = got_object_open_as_tree(&tree, repo,
1969 got_object_commit_get_tree_id(entry->commit));
1973 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1974 if (tree_view == NULL)
1975 return got_error_from_errno("view_open");
1977 err = open_tree_view(tree_view, tree, entry->id, head_ref_name, repo);
1979 got_object_tree_close(tree);
1982 s = &tree_view->state.tree;
1984 *new_view = tree_view;
1986 if (got_path_is_root_dir(path))
1989 return tree_view_walk_path(s, entry->id, path);
1992 static const struct got_error *
1993 block_signals_used_by_main_thread(void)
1998 if (sigemptyset(&sigset) == -1)
1999 return got_error_from_errno("sigemptyset");
2001 /* tog handles SIGWINCH and SIGCONT */
2002 if (sigaddset(&sigset, SIGWINCH) == -1)
2003 return got_error_from_errno("sigaddset");
2004 if (sigaddset(&sigset, SIGCONT) == -1)
2005 return got_error_from_errno("sigaddset");
2007 /* ncurses handles SIGTSTP */
2008 if (sigaddset(&sigset, SIGTSTP) == -1)
2009 return got_error_from_errno("sigaddset");
2011 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2013 return got_error_set_errno(errcode, "pthread_sigmask");
2019 log_thread(void *arg)
2021 const struct got_error *err = NULL;
2023 struct tog_log_thread_args *a = arg;
2026 err = block_signals_used_by_main_thread();
2030 while (!done && !err && !tog_sigpipe_received) {
2031 err = queue_commits(a);
2033 if (err->code != GOT_ERR_ITER_COMPLETED)
2037 } else if (a->commits_needed > 0)
2038 a->commits_needed--;
2040 errcode = pthread_mutex_lock(&tog_mutex);
2042 err = got_error_set_errno(errcode,
2043 "pthread_mutex_lock");
2045 } else if (*a->quit)
2047 else if (*a->first_displayed_entry == NULL) {
2048 *a->first_displayed_entry =
2049 TAILQ_FIRST(&a->commits->head);
2050 *a->selected_entry = *a->first_displayed_entry;
2053 errcode = pthread_cond_signal(&a->commit_loaded);
2055 err = got_error_set_errno(errcode,
2056 "pthread_cond_signal");
2057 pthread_mutex_unlock(&tog_mutex);
2062 a->commits_needed = 0;
2064 if (a->commits_needed == 0) {
2065 errcode = pthread_cond_wait(&a->need_commits,
2068 err = got_error_set_errno(errcode,
2069 "pthread_cond_wait");
2075 errcode = pthread_mutex_unlock(&tog_mutex);
2076 if (errcode && err == NULL)
2077 err = got_error_set_errno(errcode,
2078 "pthread_mutex_unlock");
2080 a->log_complete = 1;
2084 static const struct got_error *
2085 stop_log_thread(struct tog_log_view_state *s)
2087 const struct got_error *err = NULL;
2092 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2094 return got_error_set_errno(errcode,
2095 "pthread_cond_signal");
2096 errcode = pthread_mutex_unlock(&tog_mutex);
2098 return got_error_set_errno(errcode,
2099 "pthread_mutex_unlock");
2100 errcode = pthread_join(s->thread, (void **)&err);
2102 return got_error_set_errno(errcode, "pthread_join");
2103 errcode = pthread_mutex_lock(&tog_mutex);
2105 return got_error_set_errno(errcode,
2106 "pthread_mutex_lock");
2110 if (s->thread_args.repo) {
2111 got_repo_close(s->thread_args.repo);
2112 s->thread_args.repo = NULL;
2115 if (s->thread_args.graph) {
2116 got_commit_graph_close(s->thread_args.graph);
2117 s->thread_args.graph = NULL;
2123 static const struct got_error *
2124 close_log_view(struct tog_view *view)
2126 const struct got_error *err = NULL;
2127 struct tog_log_view_state *s = &view->state.log;
2130 err = stop_log_thread(s);
2132 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2133 if (errcode && err == NULL)
2134 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2136 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2137 if (errcode && err == NULL)
2138 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2140 free_commits(&s->commits);
2141 free(s->in_repo_path);
2142 s->in_repo_path = NULL;
2145 free(s->head_ref_name);
2146 s->head_ref_name = NULL;
2150 static const struct got_error *
2151 search_start_log_view(struct tog_view *view)
2153 struct tog_log_view_state *s = &view->state.log;
2155 s->matched_entry = NULL;
2156 s->search_entry = NULL;
2160 static const struct got_error *
2161 search_next_log_view(struct tog_view *view)
2163 const struct got_error *err = NULL;
2164 struct tog_log_view_state *s = &view->state.log;
2165 struct commit_queue_entry *entry;
2167 /* Display progress update in log view. */
2168 show_log_view(view);
2172 if (s->search_entry) {
2174 errcode = pthread_mutex_unlock(&tog_mutex);
2176 return got_error_set_errno(errcode,
2177 "pthread_mutex_unlock");
2178 ch = wgetch(view->window);
2179 errcode = pthread_mutex_lock(&tog_mutex);
2181 return got_error_set_errno(errcode,
2182 "pthread_mutex_lock");
2183 if (ch == KEY_BACKSPACE) {
2184 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2187 if (view->searching == TOG_SEARCH_FORWARD)
2188 entry = TAILQ_NEXT(s->search_entry, entry);
2190 entry = TAILQ_PREV(s->search_entry,
2191 commit_queue_head, entry);
2192 } else if (s->matched_entry) {
2193 if (view->searching == TOG_SEARCH_FORWARD)
2194 entry = TAILQ_NEXT(s->matched_entry, entry);
2196 entry = TAILQ_PREV(s->matched_entry,
2197 commit_queue_head, entry);
2199 if (view->searching == TOG_SEARCH_FORWARD)
2200 entry = TAILQ_FIRST(&s->commits.head);
2202 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2208 if (entry == NULL) {
2209 if (s->thread_args.log_complete ||
2210 view->searching == TOG_SEARCH_BACKWARD) {
2211 view->search_next_done =
2212 (s->matched_entry == NULL ?
2213 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2214 s->search_entry = NULL;
2218 * Poke the log thread for more commits and return,
2219 * allowing the main loop to make progress. Search
2220 * will resume at s->search_entry once we come back.
2222 s->thread_args.commits_needed++;
2223 return trigger_log_thread(view, 0);
2226 err = match_commit(&have_match, entry->id, entry->commit,
2231 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2232 s->matched_entry = entry;
2236 s->search_entry = entry;
2237 if (view->searching == TOG_SEARCH_FORWARD)
2238 entry = TAILQ_NEXT(entry, entry);
2240 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2243 if (s->matched_entry) {
2244 int cur = s->selected_entry->idx;
2245 while (cur < s->matched_entry->idx) {
2246 err = input_log_view(NULL, view, KEY_DOWN);
2251 while (cur > s->matched_entry->idx) {
2252 err = input_log_view(NULL, view, KEY_UP);
2259 s->search_entry = NULL;
2264 static const struct got_error *
2265 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2266 struct got_repository *repo, const char *head_ref_name,
2267 const char *in_repo_path, int log_branches)
2269 const struct got_error *err = NULL;
2270 struct tog_log_view_state *s = &view->state.log;
2271 struct got_repository *thread_repo = NULL;
2272 struct got_commit_graph *thread_graph = NULL;
2275 if (in_repo_path != s->in_repo_path) {
2276 free(s->in_repo_path);
2277 s->in_repo_path = strdup(in_repo_path);
2278 if (s->in_repo_path == NULL)
2279 return got_error_from_errno("strdup");
2282 /* The commit queue only contains commits being displayed. */
2283 TAILQ_INIT(&s->commits.head);
2284 s->commits.ncommits = 0;
2287 if (head_ref_name) {
2288 s->head_ref_name = strdup(head_ref_name);
2289 if (s->head_ref_name == NULL) {
2290 err = got_error_from_errno("strdup");
2294 s->start_id = got_object_id_dup(start_id);
2295 if (s->start_id == NULL) {
2296 err = got_error_from_errno("got_object_id_dup");
2299 s->log_branches = log_branches;
2301 SIMPLEQ_INIT(&s->colors);
2302 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2303 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2304 get_color_value("TOG_COLOR_COMMIT"));
2307 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2308 get_color_value("TOG_COLOR_AUTHOR"));
2310 free_colors(&s->colors);
2313 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2314 get_color_value("TOG_COLOR_DATE"));
2316 free_colors(&s->colors);
2321 view->show = show_log_view;
2322 view->input = input_log_view;
2323 view->close = close_log_view;
2324 view->search_start = search_start_log_view;
2325 view->search_next = search_next_log_view;
2327 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2330 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2334 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2335 s->repo, NULL, NULL);
2339 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2341 err = got_error_set_errno(errcode, "pthread_cond_init");
2344 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2346 err = got_error_set_errno(errcode, "pthread_cond_init");
2350 s->thread_args.commits_needed = view->nlines;
2351 s->thread_args.graph = thread_graph;
2352 s->thread_args.commits = &s->commits;
2353 s->thread_args.in_repo_path = s->in_repo_path;
2354 s->thread_args.start_id = s->start_id;
2355 s->thread_args.repo = thread_repo;
2356 s->thread_args.log_complete = 0;
2357 s->thread_args.quit = &s->quit;
2358 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2359 s->thread_args.selected_entry = &s->selected_entry;
2360 s->thread_args.searching = &view->searching;
2361 s->thread_args.search_next_done = &view->search_next_done;
2362 s->thread_args.regex = &view->regex;
2365 close_log_view(view);
2369 static const struct got_error *
2370 show_log_view(struct tog_view *view)
2372 const struct got_error *err;
2373 struct tog_log_view_state *s = &view->state.log;
2375 if (s->thread == NULL) {
2376 int errcode = pthread_create(&s->thread, NULL, log_thread,
2379 return got_error_set_errno(errcode, "pthread_create");
2380 if (s->thread_args.commits_needed > 0) {
2381 err = trigger_log_thread(view, 1);
2387 return draw_commits(view);
2390 static const struct got_error *
2391 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2393 const struct got_error *err = NULL;
2394 struct tog_log_view_state *s = &view->state.log;
2395 struct tog_view *diff_view = NULL, *tree_view = NULL;
2396 struct tog_view *ref_view = NULL;
2407 if (s->first_displayed_entry == NULL)
2409 if (s->selected > 0)
2412 log_scroll_up(s, 1);
2417 if (s->first_displayed_entry == NULL)
2419 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2422 log_scroll_up(s, view->nlines - 1);
2429 if (s->first_displayed_entry == NULL)
2431 if (s->selected < MIN(view->nlines - 2,
2432 s->commits.ncommits - 1))
2435 err = log_scroll_down(view, 1);
2443 struct commit_queue_entry *first;
2444 first = s->first_displayed_entry;
2447 err = log_scroll_down(view, view->nlines - 1);
2450 if (first == s->first_displayed_entry &&
2451 s->selected < MIN(view->nlines - 2,
2452 s->commits.ncommits - 1)) {
2453 /* can't scroll further down */
2454 s->selected = MIN(view->nlines - 2,
2455 s->commits.ncommits - 1);
2461 if (s->selected > view->nlines - 2)
2462 s->selected = view->nlines - 2;
2463 if (s->selected > s->commits.ncommits - 1)
2464 s->selected = s->commits.ncommits - 1;
2466 if (s->commits.ncommits < view->nlines - 1 &&
2467 !s->thread_args.log_complete) {
2468 s->thread_args.commits_needed += (view->nlines - 1) -
2469 s->commits.ncommits;
2470 err = trigger_log_thread(view, 1);
2476 if (s->selected_entry == NULL)
2478 if (view_is_parent_view(view))
2479 begin_x = view_split_begin_x(view->begin_x);
2480 err = open_diff_view_for_commit(&diff_view, begin_x,
2481 s->selected_entry->commit, s->selected_entry->id,
2486 diff_view->focussed = 1;
2487 if (view_is_parent_view(view)) {
2488 err = view_close_child(view);
2491 view_set_child(view, diff_view);
2492 view->focus_child = 1;
2494 *new_view = diff_view;
2497 if (s->selected_entry == NULL)
2499 if (view_is_parent_view(view))
2500 begin_x = view_split_begin_x(view->begin_x);
2501 err = browse_commit_tree(&tree_view, begin_x,
2502 s->selected_entry, s->in_repo_path, s->head_ref_name,
2507 tree_view->focussed = 1;
2508 if (view_is_parent_view(view)) {
2509 err = view_close_child(view);
2512 view_set_child(view, tree_view);
2513 view->focus_child = 1;
2515 *new_view = tree_view;
2520 if (ch == KEY_BACKSPACE &&
2521 got_path_is_root_dir(s->in_repo_path))
2523 err = stop_log_thread(s);
2526 if (ch == KEY_BACKSPACE) {
2528 err = got_path_dirname(&parent_path, s->in_repo_path);
2531 free(s->in_repo_path);
2532 s->in_repo_path = parent_path;
2533 s->thread_args.in_repo_path = s->in_repo_path;
2534 } else if (ch == CTRL('l')) {
2535 struct got_object_id *start_id;
2536 err = got_repo_match_object_id(&start_id, NULL,
2537 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2538 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2542 s->start_id = start_id;
2543 s->thread_args.start_id = s->start_id;
2545 s->log_branches = !s->log_branches;
2547 err = got_repo_open(&s->thread_args.repo,
2548 got_repo_get_path(s->repo), NULL);
2552 err = tog_load_refs(s->repo);
2555 err = got_commit_graph_open(&s->thread_args.graph,
2556 s->in_repo_path, !s->log_branches);
2559 err = got_commit_graph_iter_start(s->thread_args.graph,
2560 s->start_id, s->repo, NULL, NULL);
2563 free_commits(&s->commits);
2564 s->first_displayed_entry = NULL;
2565 s->last_displayed_entry = NULL;
2566 s->selected_entry = NULL;
2568 s->thread_args.log_complete = 0;
2570 s->thread_args.commits_needed = view->nlines;
2573 if (view_is_parent_view(view))
2574 begin_x = view_split_begin_x(view->begin_x);
2575 ref_view = view_open(view->nlines, view->ncols,
2576 view->begin_y, begin_x, TOG_VIEW_REF);
2577 if (ref_view == NULL)
2578 return got_error_from_errno("view_open");
2579 err = open_ref_view(ref_view, s->repo);
2581 view_close(ref_view);
2585 ref_view->focussed = 1;
2586 if (view_is_parent_view(view)) {
2587 err = view_close_child(view);
2590 view_set_child(view, ref_view);
2591 view->focus_child = 1;
2593 *new_view = ref_view;
2602 static const struct got_error *
2603 apply_unveil(const char *repo_path, const char *worktree_path)
2605 const struct got_error *error;
2608 if (unveil("gmon.out", "rwc") != 0)
2609 return got_error_from_errno2("unveil", "gmon.out");
2611 if (repo_path && unveil(repo_path, "r") != 0)
2612 return got_error_from_errno2("unveil", repo_path);
2614 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2615 return got_error_from_errno2("unveil", worktree_path);
2617 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2618 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2620 error = got_privsep_unveil_exec_helpers();
2624 if (unveil(NULL, NULL) != 0)
2625 return got_error_from_errno("unveil");
2635 halfdelay(1); /* Do fast refresh while initial view is loading. */
2638 intrflush(stdscr, FALSE);
2639 keypad(stdscr, TRUE);
2641 if (getenv("TOG_COLORS") != NULL) {
2643 use_default_colors();
2645 signal(SIGWINCH, tog_sigwinch);
2646 signal(SIGPIPE, tog_sigpipe);
2647 signal(SIGCONT, tog_sigcont);
2650 static const struct got_error *
2651 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2652 struct got_repository *repo, struct got_worktree *worktree)
2654 const struct got_error *err = NULL;
2657 *in_repo_path = strdup("/");
2658 if (*in_repo_path == NULL)
2659 return got_error_from_errno("strdup");
2664 const char *prefix = got_worktree_get_path_prefix(worktree);
2667 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2670 if (asprintf(in_repo_path, "%s%s%s", prefix,
2671 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2673 err = got_error_from_errno("asprintf");
2674 *in_repo_path = NULL;
2678 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2683 static const struct got_error *
2684 cmd_log(int argc, char *argv[])
2686 const struct got_error *error;
2687 struct got_repository *repo = NULL;
2688 struct got_worktree *worktree = NULL;
2689 struct got_object_id *start_id = NULL;
2690 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2691 char *start_commit = NULL, *label = NULL;
2692 struct got_reference *ref = NULL;
2693 const char *head_ref_name = NULL;
2694 int ch, log_branches = 0;
2695 struct tog_view *view;
2697 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2703 start_commit = optarg;
2706 repo_path = realpath(optarg, NULL);
2707 if (repo_path == NULL)
2708 return got_error_from_errno2("realpath",
2723 if (repo_path == NULL) {
2724 cwd = getcwd(NULL, 0);
2726 return got_error_from_errno("getcwd");
2727 error = got_worktree_open(&worktree, cwd);
2728 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2732 strdup(got_worktree_get_repo_path(worktree));
2734 repo_path = strdup(cwd);
2735 if (repo_path == NULL) {
2736 error = got_error_from_errno("strdup");
2741 error = got_repo_open(&repo, repo_path, NULL);
2745 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2752 error = apply_unveil(got_repo_get_path(repo),
2753 worktree ? got_worktree_get_root_path(worktree) : NULL);
2757 /* already loaded by tog_log_with_path()? */
2758 if (TAILQ_EMPTY(&tog_refs)) {
2759 error = tog_load_refs(repo);
2764 if (start_commit == NULL) {
2765 error = got_repo_match_object_id(&start_id, &label,
2766 worktree ? got_worktree_get_head_ref_name(worktree) :
2767 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2770 head_ref_name = label;
2772 error = got_ref_open(&ref, repo, start_commit, 0);
2774 head_ref_name = got_ref_get_name(ref);
2775 else if (error->code != GOT_ERR_NOT_REF)
2777 error = got_repo_match_object_id(&start_id, NULL,
2778 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2783 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2785 error = got_error_from_errno("view_open");
2788 error = open_log_view(view, start_id, repo, head_ref_name,
2789 in_repo_path, log_branches);
2793 /* Release work tree lock. */
2794 got_worktree_close(worktree);
2797 error = view_loop(view);
2807 got_repo_close(repo);
2809 got_worktree_close(worktree);
2818 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2819 "[-w] object1 object2\n", getprogname());
2824 match_line(const char *line, regex_t *regex, size_t nmatch,
2825 regmatch_t *regmatch)
2827 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2831 match_color(struct tog_colors *colors, const char *line)
2833 struct tog_color *tc = NULL;
2835 SIMPLEQ_FOREACH(tc, colors, entry) {
2836 if (match_line(line, &tc->regex, 0, NULL))
2843 static const struct got_error *
2844 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2845 WINDOW *window, regmatch_t *regmatch)
2847 const struct got_error *err = NULL;
2854 s = strndup(line, regmatch->rm_so);
2856 return got_error_from_errno("strndup");
2858 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2863 waddwstr(window, wline);
2870 s = strndup(line + regmatch->rm_so,
2871 regmatch->rm_eo - regmatch->rm_so);
2873 err = got_error_from_errno("strndup");
2877 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2882 wattr_on(window, A_STANDOUT, NULL);
2883 waddwstr(window, wline);
2884 wattr_off(window, A_STANDOUT, NULL);
2891 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2892 err = format_line(&wline, &width,
2893 line + regmatch->rm_eo, wlimit, col_tab_align);
2896 waddwstr(window, wline);
2904 static const struct got_error *
2905 draw_file(struct tog_view *view, const char *header)
2907 struct tog_diff_view_state *s = &view->state.diff;
2908 regmatch_t *regmatch = &view->regmatch;
2909 const struct got_error *err;
2912 size_t linesize = 0;
2914 struct tog_color *tc;
2917 int max_lines = view->nlines;
2918 int nlines = s->nlines;
2921 line_offset = s->line_offsets[s->first_displayed_line - 1];
2922 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2923 return got_error_from_errno("fseek");
2925 werase(view->window);
2928 if (asprintf(&line, "[%d/%d] %s",
2929 s->first_displayed_line - 1 + s->selected_line, nlines,
2931 return got_error_from_errno("asprintf");
2932 err = format_line(&wline, &width, line, view->ncols, 0);
2937 if (view_needs_focus_indication(view))
2938 wstandout(view->window);
2939 waddwstr(view->window, wline);
2942 if (view_needs_focus_indication(view))
2943 wstandend(view->window);
2944 if (width <= view->ncols - 1)
2945 waddch(view->window, '\n');
2954 while (max_lines > 0 && nprinted < max_lines) {
2955 linelen = getline(&line, &linesize, s->f);
2956 if (linelen == -1) {
2962 return got_ferror(s->f, GOT_ERR_IO);
2965 tc = match_color(&s->colors, line);
2967 wattr_on(view->window,
2968 COLOR_PAIR(tc->colorpair), NULL);
2969 if (s->first_displayed_line + nprinted == s->matched_line &&
2970 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2971 err = add_matched_line(&width, line, view->ncols, 0,
2972 view->window, regmatch);
2978 err = format_line(&wline, &width, line, view->ncols, 0);
2983 waddwstr(view->window, wline);
2988 wattr_off(view->window,
2989 COLOR_PAIR(tc->colorpair), NULL);
2990 if (width <= view->ncols - 1)
2991 waddch(view->window, '\n');
2996 s->last_displayed_line = s->first_displayed_line +
2999 s->last_displayed_line = s->first_displayed_line;
3004 while (nprinted < view->nlines) {
3005 waddch(view->window, '\n');
3009 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3014 wstandout(view->window);
3015 waddwstr(view->window, wline);
3018 wstandend(view->window);
3025 get_datestr(time_t *time, char *datebuf)
3027 struct tm mytm, *tm;
3030 tm = gmtime_r(time, &mytm);
3033 s = asctime_r(tm, datebuf);
3036 p = strchr(s, '\n');
3042 static const struct got_error *
3043 get_changed_paths(struct got_pathlist_head *paths,
3044 struct got_commit_object *commit, struct got_repository *repo)
3046 const struct got_error *err = NULL;
3047 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3048 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3049 struct got_object_qid *qid;
3051 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3053 struct got_commit_object *pcommit;
3054 err = got_object_open_as_commit(&pcommit, repo,
3059 tree_id1 = got_object_commit_get_tree_id(pcommit);
3060 got_object_commit_close(pcommit);
3065 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3070 tree_id2 = got_object_commit_get_tree_id(commit);
3071 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3075 err = got_diff_tree(tree1, tree2, "", "", repo,
3076 got_diff_tree_collect_changed_paths, paths, 0);
3079 got_object_tree_close(tree1);
3081 got_object_tree_close(tree2);
3085 static const struct got_error *
3086 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3090 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3092 return got_error_from_errno("reallocarray");
3094 (*line_offsets)[*nlines] = off;
3099 static const struct got_error *
3100 write_commit_info(off_t **line_offsets, size_t *nlines,
3101 struct got_object_id *commit_id, struct got_reflist_head *refs,
3102 struct got_repository *repo, FILE *outfile)
3104 const struct got_error *err = NULL;
3105 char datebuf[26], *datestr;
3106 struct got_commit_object *commit;
3107 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3108 time_t committer_time;
3109 const char *author, *committer;
3110 char *refs_str = NULL;
3111 struct got_pathlist_head changed_paths;
3112 struct got_pathlist_entry *pe;
3116 TAILQ_INIT(&changed_paths);
3119 err = build_refs_str(&refs_str, refs, commit_id, repo);
3124 err = got_object_open_as_commit(&commit, repo, commit_id);
3128 err = got_object_id_str(&id_str, commit_id);
3130 err = got_error_from_errno("got_object_id_str");
3134 err = add_line_offset(line_offsets, nlines, 0);
3138 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3139 refs_str ? refs_str : "", refs_str ? ")" : "");
3141 err = got_error_from_errno("fprintf");
3145 err = add_line_offset(line_offsets, nlines, outoff);
3149 n = fprintf(outfile, "from: %s\n",
3150 got_object_commit_get_author(commit));
3152 err = got_error_from_errno("fprintf");
3156 err = add_line_offset(line_offsets, nlines, outoff);
3160 committer_time = got_object_commit_get_committer_time(commit);
3161 datestr = get_datestr(&committer_time, datebuf);
3163 n = fprintf(outfile, "date: %s UTC\n", datestr);
3165 err = got_error_from_errno("fprintf");
3169 err = add_line_offset(line_offsets, nlines, outoff);
3173 author = got_object_commit_get_author(commit);
3174 committer = got_object_commit_get_committer(commit);
3175 if (strcmp(author, committer) != 0) {
3176 n = fprintf(outfile, "via: %s\n", committer);
3178 err = got_error_from_errno("fprintf");
3182 err = add_line_offset(line_offsets, nlines, outoff);
3186 err = got_object_commit_get_logmsg(&logmsg, commit);
3190 while ((line = strsep(&s, "\n")) != NULL) {
3191 n = fprintf(outfile, "%s\n", line);
3193 err = got_error_from_errno("fprintf");
3197 err = add_line_offset(line_offsets, nlines, outoff);
3202 err = get_changed_paths(&changed_paths, commit, repo);
3205 TAILQ_FOREACH(pe, &changed_paths, entry) {
3206 struct got_diff_changed_path *cp = pe->data;
3207 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3209 err = got_error_from_errno("fprintf");
3213 err = add_line_offset(line_offsets, nlines, outoff);
3216 free((char *)pe->path);
3220 fputc('\n', outfile);
3222 err = add_line_offset(line_offsets, nlines, outoff);
3224 got_pathlist_free(&changed_paths);
3228 got_object_commit_close(commit);
3230 free(*line_offsets);
3231 *line_offsets = NULL;
3237 static const struct got_error *
3238 create_diff(struct tog_diff_view_state *s)
3240 const struct got_error *err = NULL;
3244 free(s->line_offsets);
3245 s->line_offsets = malloc(sizeof(off_t));
3246 if (s->line_offsets == NULL)
3247 return got_error_from_errno("malloc");
3252 err = got_error_from_errno("got_opentemp");
3255 if (s->f && fclose(s->f) == EOF) {
3256 err = got_error_from_errno("fclose");
3262 err = got_object_get_type(&obj_type, s->repo, s->id1);
3264 err = got_object_get_type(&obj_type, s->repo, s->id2);
3269 case GOT_OBJ_TYPE_BLOB:
3270 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3271 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3272 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3274 case GOT_OBJ_TYPE_TREE:
3275 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3276 s->id1, s->id2, "", "", s->diff_context,
3277 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3279 case GOT_OBJ_TYPE_COMMIT: {
3280 const struct got_object_id_queue *parent_ids;
3281 struct got_object_qid *pid;
3282 struct got_commit_object *commit2;
3283 struct got_reflist_head *refs;
3285 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3288 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3289 /* Show commit info if we're diffing to a parent/root commit. */
3290 if (s->id1 == NULL) {
3291 err = write_commit_info(&s->line_offsets, &s->nlines,
3292 s->id2, refs, s->repo, s->f);
3296 parent_ids = got_object_commit_get_parent_ids(commit2);
3297 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3298 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3299 err = write_commit_info(
3300 &s->line_offsets, &s->nlines,
3301 s->id2, refs, s->repo, s->f);
3308 got_object_commit_close(commit2);
3310 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3311 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3312 s->force_text_diff, s->repo, s->f);
3316 err = got_error(GOT_ERR_OBJ_TYPE);
3322 if (s->f && fflush(s->f) != 0 && err == NULL)
3323 err = got_error_from_errno("fflush");
3328 diff_view_indicate_progress(struct tog_view *view)
3330 mvwaddstr(view->window, 0, 0, "diffing...");
3335 static const struct got_error *
3336 search_start_diff_view(struct tog_view *view)
3338 struct tog_diff_view_state *s = &view->state.diff;
3340 s->matched_line = 0;
3344 static const struct got_error *
3345 search_next_diff_view(struct tog_view *view)
3347 struct tog_diff_view_state *s = &view->state.diff;
3350 size_t linesize = 0;
3353 if (!view->searching) {
3354 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3358 if (s->matched_line) {
3359 if (view->searching == TOG_SEARCH_FORWARD)
3360 lineno = s->matched_line + 1;
3362 lineno = s->matched_line - 1;
3364 if (view->searching == TOG_SEARCH_FORWARD)
3373 if (lineno <= 0 || lineno > s->nlines) {
3374 if (s->matched_line == 0) {
3375 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3379 if (view->searching == TOG_SEARCH_FORWARD)
3385 offset = s->line_offsets[lineno - 1];
3386 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3388 return got_error_from_errno("fseeko");
3390 linelen = getline(&line, &linesize, s->f);
3391 if (linelen != -1 &&
3392 match_line(line, &view->regex, 1, &view->regmatch)) {
3393 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3394 s->matched_line = lineno;
3397 if (view->searching == TOG_SEARCH_FORWARD)
3404 if (s->matched_line) {
3405 s->first_displayed_line = s->matched_line;
3406 s->selected_line = 1;
3412 static const struct got_error *
3413 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3414 struct got_object_id *id2, const char *label1, const char *label2,
3415 int diff_context, int ignore_whitespace, int force_text_diff,
3416 struct tog_view *log_view, struct got_repository *repo)
3418 const struct got_error *err;
3419 struct tog_diff_view_state *s = &view->state.diff;
3421 if (id1 != NULL && id2 != NULL) {
3423 err = got_object_get_type(&type1, repo, id1);
3426 err = got_object_get_type(&type2, repo, id2);
3431 return got_error(GOT_ERR_OBJ_TYPE);
3433 s->first_displayed_line = 1;
3434 s->last_displayed_line = view->nlines;
3435 s->selected_line = 1;
3443 s->id1 = got_object_id_dup(id1);
3445 return got_error_from_errno("got_object_id_dup");
3449 s->id2 = got_object_id_dup(id2);
3450 if (s->id2 == NULL) {
3453 return got_error_from_errno("got_object_id_dup");
3456 s->first_displayed_line = 1;
3457 s->last_displayed_line = view->nlines;
3458 s->diff_context = diff_context;
3459 s->ignore_whitespace = ignore_whitespace;
3460 s->force_text_diff = force_text_diff;
3461 s->log_view = log_view;
3464 SIMPLEQ_INIT(&s->colors);
3465 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3466 err = add_color(&s->colors,
3467 "^-", TOG_COLOR_DIFF_MINUS,
3468 get_color_value("TOG_COLOR_DIFF_MINUS"));
3471 err = add_color(&s->colors, "^\\+",
3472 TOG_COLOR_DIFF_PLUS,
3473 get_color_value("TOG_COLOR_DIFF_PLUS"));
3475 free_colors(&s->colors);
3478 err = add_color(&s->colors,
3479 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3480 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3482 free_colors(&s->colors);
3486 err = add_color(&s->colors,
3487 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3488 TOG_COLOR_DIFF_META,
3489 get_color_value("TOG_COLOR_DIFF_META"));
3491 free_colors(&s->colors);
3495 err = add_color(&s->colors,
3496 "^(from|via): ", TOG_COLOR_AUTHOR,
3497 get_color_value("TOG_COLOR_AUTHOR"));
3499 free_colors(&s->colors);
3503 err = add_color(&s->colors,
3504 "^date: ", TOG_COLOR_DATE,
3505 get_color_value("TOG_COLOR_DATE"));
3507 free_colors(&s->colors);
3512 if (log_view && view_is_splitscreen(view))
3513 show_log_view(log_view); /* draw vborder */
3514 diff_view_indicate_progress(view);
3516 s->line_offsets = NULL;
3518 err = create_diff(s);
3524 free_colors(&s->colors);
3528 view->show = show_diff_view;
3529 view->input = input_diff_view;
3530 view->close = close_diff_view;
3531 view->search_start = search_start_diff_view;
3532 view->search_next = search_next_diff_view;
3537 static const struct got_error *
3538 close_diff_view(struct tog_view *view)
3540 const struct got_error *err = NULL;
3541 struct tog_diff_view_state *s = &view->state.diff;
3547 if (s->f && fclose(s->f) == EOF)
3548 err = got_error_from_errno("fclose");
3549 free_colors(&s->colors);
3550 free(s->line_offsets);
3551 s->line_offsets = NULL;
3556 static const struct got_error *
3557 show_diff_view(struct tog_view *view)
3559 const struct got_error *err;
3560 struct tog_diff_view_state *s = &view->state.diff;
3561 char *id_str1 = NULL, *id_str2, *header;
3562 const char *label1, *label2;
3565 err = got_object_id_str(&id_str1, s->id1);
3568 label1 = s->label1 ? : id_str1;
3570 label1 = "/dev/null";
3572 err = got_object_id_str(&id_str2, s->id2);
3575 label2 = s->label2 ? : id_str2;
3577 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3578 err = got_error_from_errno("asprintf");
3586 return draw_file(view, header);
3589 static const struct got_error *
3590 set_selected_commit(struct tog_diff_view_state *s,
3591 struct commit_queue_entry *entry)
3593 const struct got_error *err;
3594 const struct got_object_id_queue *parent_ids;
3595 struct got_commit_object *selected_commit;
3596 struct got_object_qid *pid;
3599 s->id2 = got_object_id_dup(entry->id);
3601 return got_error_from_errno("got_object_id_dup");
3603 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3606 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3608 pid = SIMPLEQ_FIRST(parent_ids);
3609 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3610 got_object_commit_close(selected_commit);
3614 static const struct got_error *
3615 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3617 const struct got_error *err = NULL;
3618 struct tog_diff_view_state *s = &view->state.diff;
3619 struct tog_log_view_state *ls;
3620 struct commit_queue_entry *old_selected_entry;
3622 size_t linesize = 0;
3630 s->force_text_diff = !s->force_text_diff;
3632 s->ignore_whitespace = !s->ignore_whitespace;
3633 wclear(view->window);
3634 s->first_displayed_line = 1;
3635 s->last_displayed_line = view->nlines;
3636 diff_view_indicate_progress(view);
3637 err = create_diff(s);
3641 if (s->first_displayed_line > 1)
3642 s->first_displayed_line--;
3646 if (s->first_displayed_line == 1)
3649 while (i++ < view->nlines - 1 &&
3650 s->first_displayed_line > 1)
3651 s->first_displayed_line--;
3656 s->first_displayed_line++;
3664 while (!s->eof && i++ < view->nlines - 1) {
3665 linelen = getline(&line, &linesize, s->f);
3666 s->first_displayed_line++;
3667 if (linelen == -1) {
3671 err = got_ferror(s->f, GOT_ERR_IO);
3678 if (s->diff_context > 0) {
3680 diff_view_indicate_progress(view);
3681 err = create_diff(s);
3682 if (s->first_displayed_line + view->nlines - 1 >
3684 s->first_displayed_line = 1;
3685 s->last_displayed_line = view->nlines;
3690 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3692 diff_view_indicate_progress(view);
3693 err = create_diff(s);
3698 if (s->log_view == NULL)
3700 ls = &s->log_view->state.log;
3701 old_selected_entry = ls->selected_entry;
3703 err = input_log_view(NULL, s->log_view, KEY_UP);
3707 if (old_selected_entry == ls->selected_entry)
3710 err = set_selected_commit(s, ls->selected_entry);
3714 s->first_displayed_line = 1;
3715 s->last_displayed_line = view->nlines;
3717 diff_view_indicate_progress(view);
3718 err = create_diff(s);
3722 if (s->log_view == NULL)
3724 ls = &s->log_view->state.log;
3725 old_selected_entry = ls->selected_entry;
3727 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3731 if (old_selected_entry == ls->selected_entry)
3734 err = set_selected_commit(s, ls->selected_entry);
3738 s->first_displayed_line = 1;
3739 s->last_displayed_line = view->nlines;
3741 diff_view_indicate_progress(view);
3742 err = create_diff(s);
3751 static const struct got_error *
3752 cmd_diff(int argc, char *argv[])
3754 const struct got_error *error = NULL;
3755 struct got_repository *repo = NULL;
3756 struct got_worktree *worktree = NULL;
3757 struct got_object_id *id1 = NULL, *id2 = NULL;
3758 char *repo_path = NULL, *cwd = NULL;
3759 char *id_str1 = NULL, *id_str2 = NULL;
3760 char *label1 = NULL, *label2 = NULL;
3761 int diff_context = 3, ignore_whitespace = 0;
3762 int ch, force_text_diff = 0;
3764 struct tog_view *view;
3766 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3769 force_text_diff = 1;
3772 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3775 err(1, "-C option %s", errstr);
3778 repo_path = realpath(optarg, NULL);
3779 if (repo_path == NULL)
3780 return got_error_from_errno2("realpath",
3782 got_path_strip_trailing_slashes(repo_path);
3785 ignore_whitespace = 1;
3797 usage_diff(); /* TODO show local worktree changes */
3798 } else if (argc == 2) {
3804 if (repo_path == NULL) {
3805 cwd = getcwd(NULL, 0);
3807 return got_error_from_errno("getcwd");
3808 error = got_worktree_open(&worktree, cwd);
3809 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3813 strdup(got_worktree_get_repo_path(worktree));
3815 repo_path = strdup(cwd);
3816 if (repo_path == NULL) {
3817 error = got_error_from_errno("strdup");
3822 error = got_repo_open(&repo, repo_path, NULL);
3828 error = apply_unveil(got_repo_get_path(repo), NULL);
3832 error = tog_load_refs(repo);
3836 error = got_repo_match_object_id(&id1, &label1, id_str1,
3837 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3841 error = got_repo_match_object_id(&id2, &label2, id_str2,
3842 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3846 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3848 error = got_error_from_errno("view_open");
3851 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3852 ignore_whitespace, force_text_diff, NULL, repo);
3855 error = view_loop(view);
3862 got_repo_close(repo);
3864 got_worktree_close(worktree);
3873 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3878 struct tog_blame_line {
3880 struct got_object_id *id;
3883 static const struct got_error *
3884 draw_blame(struct tog_view *view)
3886 struct tog_blame_view_state *s = &view->state.blame;
3887 struct tog_blame *blame = &s->blame;
3888 regmatch_t *regmatch = &view->regmatch;
3889 const struct got_error *err;
3890 int lineno = 0, nprinted = 0;
3892 size_t linesize = 0;
3896 struct tog_blame_line *blame_line;
3897 struct got_object_id *prev_id = NULL;
3899 struct tog_color *tc;
3901 err = got_object_id_str(&id_str, s->blamed_commit->id);
3906 werase(view->window);
3908 if (asprintf(&line, "commit %s", id_str) == -1) {
3909 err = got_error_from_errno("asprintf");
3914 err = format_line(&wline, &width, line, view->ncols, 0);
3919 if (view_needs_focus_indication(view))
3920 wstandout(view->window);
3921 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3923 wattr_on(view->window,
3924 COLOR_PAIR(tc->colorpair), NULL);
3925 waddwstr(view->window, wline);
3927 wattr_off(view->window,
3928 COLOR_PAIR(tc->colorpair), NULL);
3929 if (view_needs_focus_indication(view))
3930 wstandend(view->window);
3933 if (width < view->ncols - 1)
3934 waddch(view->window, '\n');
3936 if (asprintf(&line, "[%d/%d] %s%s",
3937 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3938 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3940 return got_error_from_errno("asprintf");
3943 err = format_line(&wline, &width, line, view->ncols, 0);
3948 waddwstr(view->window, wline);
3951 if (width < view->ncols - 1)
3952 waddch(view->window, '\n');
3955 while (nprinted < view->nlines - 2) {
3956 linelen = getline(&line, &linesize, blame->f);
3957 if (linelen == -1) {
3958 if (feof(blame->f)) {
3963 return got_ferror(blame->f, GOT_ERR_IO);
3965 if (++lineno < s->first_displayed_line)
3968 if (view->focussed && nprinted == s->selected_line - 1)
3969 wstandout(view->window);
3971 if (blame->nlines > 0) {
3972 blame_line = &blame->lines[lineno - 1];
3973 if (blame_line->annotated && prev_id &&
3974 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3976 nprinted == s->selected_line - 1)) {
3977 waddstr(view->window, " ");
3978 } else if (blame_line->annotated) {
3980 err = got_object_id_str(&id_str, blame_line->id);
3985 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3987 wattr_on(view->window,
3988 COLOR_PAIR(tc->colorpair), NULL);
3989 wprintw(view->window, "%.8s", id_str);
3991 wattr_off(view->window,
3992 COLOR_PAIR(tc->colorpair), NULL);
3994 prev_id = blame_line->id;
3996 waddstr(view->window, "........");
4000 waddstr(view->window, "........");
4004 if (view->focussed && nprinted == s->selected_line - 1)
4005 wstandend(view->window);
4006 waddstr(view->window, " ");
4008 if (view->ncols <= 9) {
4010 wline = wcsdup(L"");
4011 if (wline == NULL) {
4012 err = got_error_from_errno("wcsdup");
4016 } else if (s->first_displayed_line + nprinted ==
4018 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4019 err = add_matched_line(&width, line, view->ncols - 9, 9,
4020 view->window, regmatch);
4027 err = format_line(&wline, &width, line,
4028 view->ncols - 9, 9);
4029 waddwstr(view->window, wline);
4035 if (width <= view->ncols - 1)
4036 waddch(view->window, '\n');
4037 if (++nprinted == 1)
4038 s->first_displayed_line = lineno;
4041 s->last_displayed_line = lineno;
4048 static const struct got_error *
4049 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4051 const struct got_error *err = NULL;
4052 struct tog_blame_cb_args *a = arg;
4053 struct tog_blame_line *line;
4056 if (nlines != a->nlines ||
4057 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4058 return got_error(GOT_ERR_RANGE);
4060 errcode = pthread_mutex_lock(&tog_mutex);
4062 return got_error_set_errno(errcode, "pthread_mutex_lock");
4064 if (*a->quit) { /* user has quit the blame view */
4065 err = got_error(GOT_ERR_ITER_COMPLETED);
4070 goto done; /* no change in this commit */
4072 line = &a->lines[lineno - 1];
4073 if (line->annotated)
4076 line->id = got_object_id_dup(id);
4077 if (line->id == NULL) {
4078 err = got_error_from_errno("got_object_id_dup");
4081 line->annotated = 1;
4083 errcode = pthread_mutex_unlock(&tog_mutex);
4085 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4090 blame_thread(void *arg)
4092 const struct got_error *err;
4093 struct tog_blame_thread_args *ta = arg;
4094 struct tog_blame_cb_args *a = ta->cb_args;
4097 err = block_signals_used_by_main_thread();
4101 err = got_blame(ta->path, a->commit_id, ta->repo,
4102 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4103 if (err && err->code == GOT_ERR_CANCELLED)
4106 errcode = pthread_mutex_lock(&tog_mutex);
4108 return (void *)got_error_set_errno(errcode,
4109 "pthread_mutex_lock");
4111 got_repo_close(ta->repo);
4115 errcode = pthread_mutex_unlock(&tog_mutex);
4116 if (errcode && err == NULL)
4117 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4122 static struct got_object_id *
4123 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4124 int first_displayed_line, int selected_line)
4126 struct tog_blame_line *line;
4131 line = &lines[first_displayed_line - 1 + selected_line - 1];
4132 if (!line->annotated)
4138 static const struct got_error *
4139 stop_blame(struct tog_blame *blame)
4141 const struct got_error *err = NULL;
4144 if (blame->thread) {
4146 errcode = pthread_mutex_unlock(&tog_mutex);
4148 return got_error_set_errno(errcode,
4149 "pthread_mutex_unlock");
4150 errcode = pthread_join(blame->thread, (void **)&err);
4152 return got_error_set_errno(errcode, "pthread_join");
4153 errcode = pthread_mutex_lock(&tog_mutex);
4155 return got_error_set_errno(errcode,
4156 "pthread_mutex_lock");
4157 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4159 blame->thread = NULL;
4161 if (blame->thread_args.repo) {
4162 got_repo_close(blame->thread_args.repo);
4163 blame->thread_args.repo = NULL;
4166 if (fclose(blame->f) == EOF && err == NULL)
4167 err = got_error_from_errno("fclose");
4171 for (i = 0; i < blame->nlines; i++)
4172 free(blame->lines[i].id);
4174 blame->lines = NULL;
4176 free(blame->cb_args.commit_id);
4177 blame->cb_args.commit_id = NULL;
4182 static const struct got_error *
4183 cancel_blame_view(void *arg)
4185 const struct got_error *err = NULL;
4189 errcode = pthread_mutex_lock(&tog_mutex);
4191 return got_error_set_errno(errcode,
4192 "pthread_mutex_unlock");
4195 err = got_error(GOT_ERR_CANCELLED);
4197 errcode = pthread_mutex_unlock(&tog_mutex);
4199 return got_error_set_errno(errcode,
4200 "pthread_mutex_lock");
4205 static const struct got_error *
4206 run_blame(struct tog_view *view)
4208 struct tog_blame_view_state *s = &view->state.blame;
4209 struct tog_blame *blame = &s->blame;
4210 const struct got_error *err = NULL;
4211 struct got_blob_object *blob = NULL;
4212 struct got_repository *thread_repo = NULL;
4213 struct got_object_id *obj_id = NULL;
4216 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4221 err = got_object_get_type(&obj_type, s->repo, obj_id);
4225 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4226 err = got_error(GOT_ERR_OBJ_TYPE);
4230 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4233 blame->f = got_opentemp();
4234 if (blame->f == NULL) {
4235 err = got_error_from_errno("got_opentemp");
4238 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4239 &blame->line_offsets, blame->f, blob);
4242 if (blame->nlines == 0) {
4243 s->blame_complete = 1;
4247 /* Don't include \n at EOF in the blame line count. */
4248 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4251 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4252 if (blame->lines == NULL) {
4253 err = got_error_from_errno("calloc");
4257 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4261 blame->cb_args.view = view;
4262 blame->cb_args.lines = blame->lines;
4263 blame->cb_args.nlines = blame->nlines;
4264 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4265 if (blame->cb_args.commit_id == NULL) {
4266 err = got_error_from_errno("got_object_id_dup");
4269 blame->cb_args.quit = &s->done;
4271 blame->thread_args.path = s->path;
4272 blame->thread_args.repo = thread_repo;
4273 blame->thread_args.cb_args = &blame->cb_args;
4274 blame->thread_args.complete = &s->blame_complete;
4275 blame->thread_args.cancel_cb = cancel_blame_view;
4276 blame->thread_args.cancel_arg = &s->done;
4277 s->blame_complete = 0;
4279 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4280 s->first_displayed_line = 1;
4281 s->last_displayed_line = view->nlines;
4282 s->selected_line = 1;
4287 got_object_blob_close(blob);
4294 static const struct got_error *
4295 open_blame_view(struct tog_view *view, char *path,
4296 struct got_object_id *commit_id, struct got_repository *repo)
4298 const struct got_error *err = NULL;
4299 struct tog_blame_view_state *s = &view->state.blame;
4301 SIMPLEQ_INIT(&s->blamed_commits);
4303 s->path = strdup(path);
4304 if (s->path == NULL)
4305 return got_error_from_errno("strdup");
4307 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4313 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4314 s->first_displayed_line = 1;
4315 s->last_displayed_line = view->nlines;
4316 s->selected_line = 1;
4317 s->blame_complete = 0;
4319 s->commit_id = commit_id;
4320 memset(&s->blame, 0, sizeof(s->blame));
4322 SIMPLEQ_INIT(&s->colors);
4323 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4324 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4325 get_color_value("TOG_COLOR_COMMIT"));
4330 view->show = show_blame_view;
4331 view->input = input_blame_view;
4332 view->close = close_blame_view;
4333 view->search_start = search_start_blame_view;
4334 view->search_next = search_next_blame_view;
4336 return run_blame(view);
4339 static const struct got_error *
4340 close_blame_view(struct tog_view *view)
4342 const struct got_error *err = NULL;
4343 struct tog_blame_view_state *s = &view->state.blame;
4345 if (s->blame.thread)
4346 err = stop_blame(&s->blame);
4348 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4349 struct got_object_qid *blamed_commit;
4350 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4351 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4352 got_object_qid_free(blamed_commit);
4356 free_colors(&s->colors);
4361 static const struct got_error *
4362 search_start_blame_view(struct tog_view *view)
4364 struct tog_blame_view_state *s = &view->state.blame;
4366 s->matched_line = 0;
4370 static const struct got_error *
4371 search_next_blame_view(struct tog_view *view)
4373 struct tog_blame_view_state *s = &view->state.blame;
4376 size_t linesize = 0;
4379 if (!view->searching) {
4380 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4384 if (s->matched_line) {
4385 if (view->searching == TOG_SEARCH_FORWARD)
4386 lineno = s->matched_line + 1;
4388 lineno = s->matched_line - 1;
4390 if (view->searching == TOG_SEARCH_FORWARD)
4393 lineno = s->blame.nlines;
4399 if (lineno <= 0 || lineno > s->blame.nlines) {
4400 if (s->matched_line == 0) {
4401 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4405 if (view->searching == TOG_SEARCH_FORWARD)
4408 lineno = s->blame.nlines;
4411 offset = s->blame.line_offsets[lineno - 1];
4412 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4414 return got_error_from_errno("fseeko");
4416 linelen = getline(&line, &linesize, s->blame.f);
4417 if (linelen != -1 &&
4418 match_line(line, &view->regex, 1, &view->regmatch)) {
4419 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4420 s->matched_line = lineno;
4423 if (view->searching == TOG_SEARCH_FORWARD)
4430 if (s->matched_line) {
4431 s->first_displayed_line = s->matched_line;
4432 s->selected_line = 1;
4438 static const struct got_error *
4439 show_blame_view(struct tog_view *view)
4441 const struct got_error *err = NULL;
4442 struct tog_blame_view_state *s = &view->state.blame;
4445 if (s->blame.thread == NULL && !s->blame_complete) {
4446 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4447 &s->blame.thread_args);
4449 return got_error_set_errno(errcode, "pthread_create");
4451 halfdelay(1); /* fast refresh while annotating */
4454 if (s->blame_complete)
4455 halfdelay(10); /* disable fast refresh */
4457 err = draw_blame(view);
4463 static const struct got_error *
4464 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4466 const struct got_error *err = NULL, *thread_err = NULL;
4467 struct tog_view *diff_view;
4468 struct tog_blame_view_state *s = &view->state.blame;
4477 if (s->selected_line > 1)
4479 else if (s->selected_line == 1 &&
4480 s->first_displayed_line > 1)
4481 s->first_displayed_line--;
4485 if (s->first_displayed_line == 1) {
4486 s->selected_line = 1;
4489 if (s->first_displayed_line > view->nlines - 2)
4490 s->first_displayed_line -=
4493 s->first_displayed_line = 1;
4497 if (s->selected_line < view->nlines - 2 &&
4498 s->first_displayed_line +
4499 s->selected_line <= s->blame.nlines)
4501 else if (s->last_displayed_line <
4503 s->first_displayed_line++;
4507 struct got_object_id *id = NULL;
4508 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4509 s->first_displayed_line, s->selected_line);
4513 struct got_commit_object *commit;
4514 struct got_object_qid *pid;
4515 struct got_object_id *blob_id = NULL;
4517 err = got_object_open_as_commit(&commit,
4521 pid = SIMPLEQ_FIRST(
4522 got_object_commit_get_parent_ids(commit));
4524 got_object_commit_close(commit);
4527 /* Check if path history ends here. */
4528 err = got_object_id_by_path(&blob_id, s->repo,
4531 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4533 got_object_commit_close(commit);
4536 err = got_object_get_type(&obj_type, s->repo,
4539 /* Can't blame non-blob type objects. */
4540 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4541 got_object_commit_close(commit);
4544 err = got_object_qid_alloc(&s->blamed_commit,
4546 got_object_commit_close(commit);
4548 if (got_object_id_cmp(id,
4549 s->blamed_commit->id) == 0)
4551 err = got_object_qid_alloc(&s->blamed_commit,
4557 thread_err = stop_blame(&s->blame);
4561 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4562 s->blamed_commit, entry);
4563 err = run_blame(view);
4569 struct got_object_qid *first;
4570 first = SIMPLEQ_FIRST(&s->blamed_commits);
4571 if (!got_object_id_cmp(first->id, s->commit_id))
4574 thread_err = stop_blame(&s->blame);
4578 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4579 got_object_qid_free(s->blamed_commit);
4581 SIMPLEQ_FIRST(&s->blamed_commits);
4582 err = run_blame(view);
4589 struct got_object_id *id = NULL;
4590 struct got_object_qid *pid;
4591 struct got_commit_object *commit = NULL;
4592 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4593 s->first_displayed_line, s->selected_line);
4596 err = got_object_open_as_commit(&commit, s->repo, id);
4599 pid = SIMPLEQ_FIRST(
4600 got_object_commit_get_parent_ids(commit));
4601 if (view_is_parent_view(view))
4602 begin_x = view_split_begin_x(view->begin_x);
4603 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4604 if (diff_view == NULL) {
4605 got_object_commit_close(commit);
4606 err = got_error_from_errno("view_open");
4609 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4610 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4611 got_object_commit_close(commit);
4613 view_close(diff_view);
4617 diff_view->focussed = 1;
4618 if (view_is_parent_view(view)) {
4619 err = view_close_child(view);
4622 view_set_child(view, diff_view);
4623 view->focus_child = 1;
4625 *new_view = diff_view;
4633 if (s->last_displayed_line >= s->blame.nlines &&
4634 s->selected_line >= MIN(s->blame.nlines,
4635 view->nlines - 2)) {
4638 if (s->last_displayed_line >= s->blame.nlines &&
4639 s->selected_line < view->nlines - 2) {
4640 s->selected_line = MIN(s->blame.nlines,
4644 if (s->last_displayed_line + view->nlines - 2
4646 s->first_displayed_line +=
4649 s->first_displayed_line =
4654 if (s->selected_line > view->nlines - 2) {
4655 s->selected_line = MIN(s->blame.nlines,
4662 return thread_err ? thread_err : err;
4665 static const struct got_error *
4666 cmd_blame(int argc, char *argv[])
4668 const struct got_error *error;
4669 struct got_repository *repo = NULL;
4670 struct got_worktree *worktree = NULL;
4671 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4672 char *link_target = NULL;
4673 struct got_object_id *commit_id = NULL;
4674 char *commit_id_str = NULL;
4676 struct tog_view *view;
4678 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4681 commit_id_str = optarg;
4684 repo_path = realpath(optarg, NULL);
4685 if (repo_path == NULL)
4686 return got_error_from_errno2("realpath",
4701 if (repo_path == NULL) {
4702 cwd = getcwd(NULL, 0);
4704 return got_error_from_errno("getcwd");
4705 error = got_worktree_open(&worktree, cwd);
4706 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4710 strdup(got_worktree_get_repo_path(worktree));
4712 repo_path = strdup(cwd);
4713 if (repo_path == NULL) {
4714 error = got_error_from_errno("strdup");
4719 error = got_repo_open(&repo, repo_path, NULL);
4723 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4730 error = apply_unveil(got_repo_get_path(repo), NULL);
4734 error = tog_load_refs(repo);
4738 if (commit_id_str == NULL) {
4739 struct got_reference *head_ref;
4740 error = got_ref_open(&head_ref, repo, worktree ?
4741 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4744 error = got_ref_resolve(&commit_id, repo, head_ref);
4745 got_ref_close(head_ref);
4747 error = got_repo_match_object_id(&commit_id, NULL,
4748 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4753 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4755 error = got_error_from_errno("view_open");
4759 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4764 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4769 /* Release work tree lock. */
4770 got_worktree_close(worktree);
4773 error = view_loop(view);
4781 got_worktree_close(worktree);
4783 got_repo_close(repo);
4788 static const struct got_error *
4789 draw_tree_entries(struct tog_view *view, const char *parent_path)
4791 struct tog_tree_view_state *s = &view->state.tree;
4792 const struct got_error *err = NULL;
4793 struct got_tree_entry *te;
4795 struct tog_color *tc;
4796 int width, n, i, nentries;
4797 int limit = view->nlines;
4801 werase(view->window);
4806 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4809 if (view_needs_focus_indication(view))
4810 wstandout(view->window);
4811 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4813 wattr_on(view->window,
4814 COLOR_PAIR(tc->colorpair), NULL);
4815 waddwstr(view->window, wline);
4817 wattr_off(view->window,
4818 COLOR_PAIR(tc->colorpair), NULL);
4819 if (view_needs_focus_indication(view))
4820 wstandend(view->window);
4823 if (width < view->ncols - 1)
4824 waddch(view->window, '\n');
4827 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4830 waddwstr(view->window, wline);
4833 if (width < view->ncols - 1)
4834 waddch(view->window, '\n');
4837 waddch(view->window, '\n');
4841 if (s->first_displayed_entry == NULL) {
4842 te = got_object_tree_get_first_entry(s->tree);
4843 if (s->selected == 0) {
4845 wstandout(view->window);
4846 s->selected_entry = NULL;
4848 waddstr(view->window, " ..\n"); /* parent directory */
4849 if (s->selected == 0 && view->focussed)
4850 wstandend(view->window);
4857 te = s->first_displayed_entry;
4860 nentries = got_object_tree_get_nentries(s->tree);
4861 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4862 char *line = NULL, *id_str = NULL, *link_target = NULL;
4863 const char *modestr = "";
4866 te = got_object_tree_get_entry(s->tree, i);
4867 mode = got_tree_entry_get_mode(te);
4870 err = got_object_id_str(&id_str,
4871 got_tree_entry_get_id(te));
4873 return got_error_from_errno(
4874 "got_object_id_str");
4876 if (got_object_tree_entry_is_submodule(te))
4878 else if (S_ISLNK(mode)) {
4881 err = got_tree_entry_get_symlink_target(&link_target,
4887 for (i = 0; i < strlen(link_target); i++) {
4888 if (!isprint((unsigned char)link_target[i]))
4889 link_target[i] = '?';
4893 else if (S_ISDIR(mode))
4895 else if (mode & S_IXUSR)
4897 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4898 got_tree_entry_get_name(te), modestr,
4899 link_target ? " -> ": "",
4900 link_target ? link_target : "") == -1) {
4903 return got_error_from_errno("asprintf");
4907 err = format_line(&wline, &width, line, view->ncols, 0);
4912 if (n == s->selected) {
4914 wstandout(view->window);
4915 s->selected_entry = te;
4917 tc = match_color(&s->colors, line);
4919 wattr_on(view->window,
4920 COLOR_PAIR(tc->colorpair), NULL);
4921 waddwstr(view->window, wline);
4923 wattr_off(view->window,
4924 COLOR_PAIR(tc->colorpair), NULL);
4925 if (width < view->ncols - 1)
4926 waddch(view->window, '\n');
4927 if (n == s->selected && view->focussed)
4928 wstandend(view->window);
4934 s->last_displayed_entry = te;
4943 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4945 struct got_tree_entry *te;
4946 int isroot = s->tree == s->root;
4949 if (s->first_displayed_entry == NULL)
4952 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4953 while (i++ < maxscroll) {
4956 s->first_displayed_entry = NULL;
4959 s->first_displayed_entry = te;
4960 te = got_tree_entry_get_prev(s->tree, te);
4965 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4967 struct got_tree_entry *next, *last;
4970 if (s->first_displayed_entry)
4971 next = got_tree_entry_get_next(s->tree,
4972 s->first_displayed_entry);
4974 next = got_object_tree_get_first_entry(s->tree);
4976 last = s->last_displayed_entry;
4977 while (next && last && n++ < maxscroll) {
4978 last = got_tree_entry_get_next(s->tree, last);
4980 s->first_displayed_entry = next;
4981 next = got_tree_entry_get_next(s->tree, next);
4986 static const struct got_error *
4987 tree_entry_path(char **path, struct tog_parent_trees *parents,
4988 struct got_tree_entry *te)
4990 const struct got_error *err = NULL;
4991 struct tog_parent_tree *pt;
4992 size_t len = 2; /* for leading slash and NUL */
4994 TAILQ_FOREACH(pt, parents, entry)
4995 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4998 len += strlen(got_tree_entry_get_name(te));
5000 *path = calloc(1, len);
5002 return got_error_from_errno("calloc");
5005 pt = TAILQ_LAST(parents, tog_parent_trees);
5007 const char *name = got_tree_entry_get_name(pt->selected_entry);
5008 if (strlcat(*path, name, len) >= len) {
5009 err = got_error(GOT_ERR_NO_SPACE);
5012 if (strlcat(*path, "/", len) >= len) {
5013 err = got_error(GOT_ERR_NO_SPACE);
5016 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5019 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5020 err = got_error(GOT_ERR_NO_SPACE);
5032 static const struct got_error *
5033 blame_tree_entry(struct tog_view **new_view, int begin_x,
5034 struct got_tree_entry *te, struct tog_parent_trees *parents,
5035 struct got_object_id *commit_id, struct got_repository *repo)
5037 const struct got_error *err = NULL;
5039 struct tog_view *blame_view;
5043 err = tree_entry_path(&path, parents, te);
5047 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5048 if (blame_view == NULL) {
5049 err = got_error_from_errno("view_open");
5053 err = open_blame_view(blame_view, path, commit_id, repo);
5055 if (err->code == GOT_ERR_CANCELLED)
5057 view_close(blame_view);
5059 *new_view = blame_view;
5065 static const struct got_error *
5066 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5067 struct tog_tree_view_state *s)
5069 struct tog_view *log_view;
5070 const struct got_error *err = NULL;
5075 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5076 if (log_view == NULL)
5077 return got_error_from_errno("view_open");
5079 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5083 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5086 view_close(log_view);
5088 *new_view = log_view;
5093 static const struct got_error *
5094 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5095 struct got_object_id *commit_id, const char *head_ref_name,
5096 struct got_repository *repo)
5098 const struct got_error *err = NULL;
5099 char *commit_id_str = NULL;
5100 struct tog_tree_view_state *s = &view->state.tree;
5102 TAILQ_INIT(&s->parents);
5104 err = got_object_id_str(&commit_id_str, commit_id);
5108 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5109 err = got_error_from_errno("asprintf");
5113 s->root = s->tree = root;
5114 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5115 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5116 s->commit_id = got_object_id_dup(commit_id);
5117 if (s->commit_id == NULL) {
5118 err = got_error_from_errno("got_object_id_dup");
5121 if (head_ref_name) {
5122 s->head_ref_name = strdup(head_ref_name);
5123 if (s->head_ref_name == NULL) {
5124 err = got_error_from_errno("strdup");
5130 SIMPLEQ_INIT(&s->colors);
5132 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5133 err = add_color(&s->colors, "\\$$",
5134 TOG_COLOR_TREE_SUBMODULE,
5135 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5138 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5139 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5141 free_colors(&s->colors);
5144 err = add_color(&s->colors, "/$",
5145 TOG_COLOR_TREE_DIRECTORY,
5146 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5148 free_colors(&s->colors);
5152 err = add_color(&s->colors, "\\*$",
5153 TOG_COLOR_TREE_EXECUTABLE,
5154 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5156 free_colors(&s->colors);
5160 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5161 get_color_value("TOG_COLOR_COMMIT"));
5163 free_colors(&s->colors);
5168 view->show = show_tree_view;
5169 view->input = input_tree_view;
5170 view->close = close_tree_view;
5171 view->search_start = search_start_tree_view;
5172 view->search_next = search_next_tree_view;
5174 free(commit_id_str);
5176 free(s->tree_label);
5177 s->tree_label = NULL;
5182 static const struct got_error *
5183 close_tree_view(struct tog_view *view)
5185 struct tog_tree_view_state *s = &view->state.tree;
5187 free_colors(&s->colors);
5188 free(s->tree_label);
5189 s->tree_label = NULL;
5191 s->commit_id = NULL;
5192 free(s->head_ref_name);
5193 s->head_ref_name = NULL;
5194 while (!TAILQ_EMPTY(&s->parents)) {
5195 struct tog_parent_tree *parent;
5196 parent = TAILQ_FIRST(&s->parents);
5197 TAILQ_REMOVE(&s->parents, parent, entry);
5201 if (s->tree != s->root)
5202 got_object_tree_close(s->tree);
5203 got_object_tree_close(s->root);
5207 static const struct got_error *
5208 search_start_tree_view(struct tog_view *view)
5210 struct tog_tree_view_state *s = &view->state.tree;
5212 s->matched_entry = NULL;
5217 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5219 regmatch_t regmatch;
5221 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5225 static const struct got_error *
5226 search_next_tree_view(struct tog_view *view)
5228 struct tog_tree_view_state *s = &view->state.tree;
5229 struct got_tree_entry *te = NULL;
5231 if (!view->searching) {
5232 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5236 if (s->matched_entry) {
5237 if (view->searching == TOG_SEARCH_FORWARD) {
5238 if (s->selected_entry)
5239 te = got_tree_entry_get_next(s->tree,
5242 te = got_object_tree_get_first_entry(s->tree);
5244 if (s->selected_entry == NULL)
5245 te = got_object_tree_get_last_entry(s->tree);
5247 te = got_tree_entry_get_prev(s->tree,
5251 if (view->searching == TOG_SEARCH_FORWARD)
5252 te = got_object_tree_get_first_entry(s->tree);
5254 te = got_object_tree_get_last_entry(s->tree);
5259 if (s->matched_entry == NULL) {
5260 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5263 if (view->searching == TOG_SEARCH_FORWARD)
5264 te = got_object_tree_get_first_entry(s->tree);
5266 te = got_object_tree_get_last_entry(s->tree);
5269 if (match_tree_entry(te, &view->regex)) {
5270 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5271 s->matched_entry = te;
5275 if (view->searching == TOG_SEARCH_FORWARD)
5276 te = got_tree_entry_get_next(s->tree, te);
5278 te = got_tree_entry_get_prev(s->tree, te);
5281 if (s->matched_entry) {
5282 s->first_displayed_entry = s->matched_entry;
5289 static const struct got_error *
5290 show_tree_view(struct tog_view *view)
5292 const struct got_error *err = NULL;
5293 struct tog_tree_view_state *s = &view->state.tree;
5296 err = tree_entry_path(&parent_path, &s->parents, NULL);
5300 err = draw_tree_entries(view, parent_path);
5307 static const struct got_error *
5308 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5310 const struct got_error *err = NULL;
5311 struct tog_tree_view_state *s = &view->state.tree;
5312 struct tog_view *log_view, *ref_view;
5317 s->show_ids = !s->show_ids;
5320 if (!s->selected_entry)
5322 if (view_is_parent_view(view))
5323 begin_x = view_split_begin_x(view->begin_x);
5324 err = log_selected_tree_entry(&log_view, begin_x, s);
5326 log_view->focussed = 1;
5327 if (view_is_parent_view(view)) {
5328 err = view_close_child(view);
5331 view_set_child(view, log_view);
5332 view->focus_child = 1;
5334 *new_view = log_view;
5337 if (view_is_parent_view(view))
5338 begin_x = view_split_begin_x(view->begin_x);
5339 ref_view = view_open(view->nlines, view->ncols,
5340 view->begin_y, begin_x, TOG_VIEW_REF);
5341 if (ref_view == NULL)
5342 return got_error_from_errno("view_open");
5343 err = open_ref_view(ref_view, s->repo);
5345 view_close(ref_view);
5349 ref_view->focussed = 1;
5350 if (view_is_parent_view(view)) {
5351 err = view_close_child(view);
5354 view_set_child(view, ref_view);
5355 view->focus_child = 1;
5357 *new_view = ref_view;
5361 if (s->selected > 0) {
5365 tree_scroll_up(s, 1);
5369 if (s->tree == s->root) {
5370 if (got_object_tree_get_first_entry(s->tree) ==
5371 s->first_displayed_entry)
5374 if (s->first_displayed_entry == NULL)
5377 tree_scroll_up(s, MAX(0, view->nlines - 3));
5381 if (s->selected < s->ndisplayed - 1) {
5385 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5387 /* can't scroll any further */
5389 tree_scroll_down(s, 1);
5393 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5395 /* can't scroll any further; move cursor down */
5396 if (s->selected < s->ndisplayed - 1)
5397 s->selected = s->ndisplayed - 1;
5400 tree_scroll_down(s, view->nlines - 3);
5405 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5406 struct tog_parent_tree *parent;
5407 /* user selected '..' */
5408 if (s->tree == s->root)
5410 parent = TAILQ_FIRST(&s->parents);
5411 TAILQ_REMOVE(&s->parents, parent,
5413 got_object_tree_close(s->tree);
5414 s->tree = parent->tree;
5415 s->first_displayed_entry =
5416 parent->first_displayed_entry;
5418 parent->selected_entry;
5419 s->selected = parent->selected;
5421 } else if (S_ISDIR(got_tree_entry_get_mode(
5422 s->selected_entry))) {
5423 struct got_tree_object *subtree;
5424 err = got_object_open_as_tree(&subtree, s->repo,
5425 got_tree_entry_get_id(s->selected_entry));
5428 err = tree_view_visit_subtree(s, subtree);
5430 got_object_tree_close(subtree);
5433 } else if (S_ISREG(got_tree_entry_get_mode(
5434 s->selected_entry))) {
5435 struct tog_view *blame_view;
5436 int begin_x = view_is_parent_view(view) ?
5437 view_split_begin_x(view->begin_x) : 0;
5439 err = blame_tree_entry(&blame_view, begin_x,
5440 s->selected_entry, &s->parents,
5441 s->commit_id, s->repo);
5445 blame_view->focussed = 1;
5446 if (view_is_parent_view(view)) {
5447 err = view_close_child(view);
5450 view_set_child(view, blame_view);
5451 view->focus_child = 1;
5453 *new_view = blame_view;
5457 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5458 s->selected = view->nlines - 4;
5471 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5476 static const struct got_error *
5477 cmd_tree(int argc, char *argv[])
5479 const struct got_error *error;
5480 struct got_repository *repo = NULL;
5481 struct got_worktree *worktree = NULL;
5482 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5483 struct got_object_id *commit_id = NULL;
5484 const char *commit_id_arg = NULL;
5486 struct got_commit_object *commit = NULL;
5487 struct got_tree_object *tree = NULL;
5488 struct got_reference *ref = NULL;
5489 const char *head_ref_name = NULL;
5491 struct tog_view *view;
5493 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5496 commit_id_arg = optarg;
5499 repo_path = realpath(optarg, NULL);
5500 if (repo_path == NULL)
5501 return got_error_from_errno2("realpath",
5516 if (repo_path == NULL) {
5517 cwd = getcwd(NULL, 0);
5519 return got_error_from_errno("getcwd");
5520 error = got_worktree_open(&worktree, cwd);
5521 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5525 strdup(got_worktree_get_repo_path(worktree));
5527 repo_path = strdup(cwd);
5528 if (repo_path == NULL) {
5529 error = got_error_from_errno("strdup");
5534 error = got_repo_open(&repo, repo_path, NULL);
5538 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5545 error = apply_unveil(got_repo_get_path(repo), NULL);
5549 error = tog_load_refs(repo);
5553 if (commit_id_arg == NULL) {
5554 error = got_repo_match_object_id(&commit_id, &label,
5555 worktree ? got_worktree_get_head_ref_name(worktree) :
5556 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5559 head_ref_name = label;
5561 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5563 head_ref_name = got_ref_get_name(ref);
5564 else if (error->code != GOT_ERR_NOT_REF)
5566 error = got_repo_match_object_id(&commit_id, NULL,
5567 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5572 error = got_object_open_as_commit(&commit, repo, commit_id);
5576 error = got_object_open_as_tree(&tree, repo,
5577 got_object_commit_get_tree_id(commit));
5581 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5583 error = got_error_from_errno("view_open");
5586 error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5589 if (!got_path_is_root_dir(in_repo_path)) {
5590 error = tree_view_walk_path(&view->state.tree, commit_id,
5597 /* Release work tree lock. */
5598 got_worktree_close(worktree);
5601 error = view_loop(view);
5610 got_object_commit_close(commit);
5612 got_object_tree_close(tree);
5614 got_repo_close(repo);
5619 static const struct got_error *
5620 ref_view_load_refs(struct tog_ref_view_state *s)
5622 struct got_reflist_entry *sre;
5623 struct tog_reflist_entry *re;
5626 TAILQ_FOREACH(sre, &tog_refs, entry) {
5627 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5630 re = malloc(sizeof(*re));
5632 return got_error_from_errno("malloc");
5634 re->ref = got_ref_dup(sre->ref);
5635 if (re->ref == NULL)
5636 return got_error_from_errno("got_ref_dup");
5637 re->idx = s->nrefs++;
5638 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5641 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5646 ref_view_free_refs(struct tog_ref_view_state *s)
5648 struct tog_reflist_entry *re;
5650 while (!TAILQ_EMPTY(&s->refs)) {
5651 re = TAILQ_FIRST(&s->refs);
5652 TAILQ_REMOVE(&s->refs, re, entry);
5653 got_ref_close(re->ref);
5658 static const struct got_error *
5659 open_ref_view(struct tog_view *view, struct got_repository *repo)
5661 const struct got_error *err = NULL;
5662 struct tog_ref_view_state *s = &view->state.ref;
5664 s->selected_entry = 0;
5667 TAILQ_INIT(&s->refs);
5668 SIMPLEQ_INIT(&s->colors);
5670 err = ref_view_load_refs(s);
5674 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5675 err = add_color(&s->colors, "^refs/heads/",
5676 TOG_COLOR_REFS_HEADS,
5677 get_color_value("TOG_COLOR_REFS_HEADS"));
5681 err = add_color(&s->colors, "^refs/tags/",
5682 TOG_COLOR_REFS_TAGS,
5683 get_color_value("TOG_COLOR_REFS_TAGS"));
5687 err = add_color(&s->colors, "^refs/remotes/",
5688 TOG_COLOR_REFS_REMOTES,
5689 get_color_value("TOG_COLOR_REFS_REMOTES"));
5694 view->show = show_ref_view;
5695 view->input = input_ref_view;
5696 view->close = close_ref_view;
5697 view->search_start = search_start_ref_view;
5698 view->search_next = search_next_ref_view;
5701 free_colors(&s->colors);
5705 static const struct got_error *
5706 close_ref_view(struct tog_view *view)
5708 struct tog_ref_view_state *s = &view->state.ref;
5710 ref_view_free_refs(s);
5711 free_colors(&s->colors);
5716 static const struct got_error *
5717 resolve_reflist_entry(struct got_object_id **commit_id,
5718 struct tog_reflist_entry *re, struct got_repository *repo)
5720 const struct got_error *err = NULL;
5721 struct got_object_id *obj_id;
5722 struct got_tag_object *tag = NULL;
5727 err = got_ref_resolve(&obj_id, repo, re->ref);
5731 err = got_object_get_type(&obj_type, repo, obj_id);
5736 case GOT_OBJ_TYPE_COMMIT:
5737 *commit_id = obj_id;
5739 case GOT_OBJ_TYPE_TAG:
5740 err = got_object_open_as_tag(&tag, repo, obj_id);
5744 err = got_object_get_type(&obj_type, repo,
5745 got_object_tag_get_object_id(tag));
5748 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5749 err = got_error(GOT_ERR_OBJ_TYPE);
5752 *commit_id = got_object_id_dup(
5753 got_object_tag_get_object_id(tag));
5754 if (*commit_id == NULL) {
5755 err = got_error_from_errno("got_object_id_dup");
5760 err = got_error(GOT_ERR_OBJ_TYPE);
5766 got_object_tag_close(tag);
5774 static const struct got_error *
5775 log_ref_entry(struct tog_view **new_view, int begin_x,
5776 struct tog_reflist_entry *re, struct got_repository *repo)
5778 struct tog_view *log_view;
5779 const struct got_error *err = NULL;
5780 struct got_object_id *commit_id = NULL;
5784 err = resolve_reflist_entry(&commit_id, re, repo);
5786 if (err->code != GOT_ERR_OBJ_TYPE)
5792 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5793 if (log_view == NULL) {
5794 err = got_error_from_errno("view_open");
5798 err = open_log_view(log_view, commit_id, repo,
5799 got_ref_get_name(re->ref), "", 0);
5802 view_close(log_view);
5804 *new_view = log_view;
5810 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5812 struct tog_reflist_entry *re;
5815 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5818 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5819 while (i++ < maxscroll) {
5822 s->first_displayed_entry = re;
5823 re = TAILQ_PREV(re, tog_reflist_head, entry);
5828 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5830 struct tog_reflist_entry *next, *last;
5833 if (s->first_displayed_entry)
5834 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5836 next = TAILQ_FIRST(&s->refs);
5838 last = s->last_displayed_entry;
5839 while (next && last && n++ < maxscroll) {
5840 last = TAILQ_NEXT(last, entry);
5842 s->first_displayed_entry = next;
5843 next = TAILQ_NEXT(next, entry);
5848 static const struct got_error *
5849 search_start_ref_view(struct tog_view *view)
5851 struct tog_ref_view_state *s = &view->state.ref;
5853 s->matched_entry = NULL;
5858 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5860 regmatch_t regmatch;
5862 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5866 static const struct got_error *
5867 search_next_ref_view(struct tog_view *view)
5869 struct tog_ref_view_state *s = &view->state.ref;
5870 struct tog_reflist_entry *re = NULL;
5872 if (!view->searching) {
5873 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5877 if (s->matched_entry) {
5878 if (view->searching == TOG_SEARCH_FORWARD) {
5879 if (s->selected_entry)
5880 re = TAILQ_NEXT(s->selected_entry, entry);
5882 re = TAILQ_PREV(s->selected_entry,
5883 tog_reflist_head, entry);
5885 if (s->selected_entry == NULL)
5886 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5888 re = TAILQ_PREV(s->selected_entry,
5889 tog_reflist_head, entry);
5892 if (view->searching == TOG_SEARCH_FORWARD)
5893 re = TAILQ_FIRST(&s->refs);
5895 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5900 if (s->matched_entry == NULL) {
5901 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5904 if (view->searching == TOG_SEARCH_FORWARD)
5905 re = TAILQ_FIRST(&s->refs);
5907 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5910 if (match_reflist_entry(re, &view->regex)) {
5911 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5912 s->matched_entry = re;
5916 if (view->searching == TOG_SEARCH_FORWARD)
5917 re = TAILQ_NEXT(re, entry);
5919 re = TAILQ_PREV(re, tog_reflist_head, entry);
5922 if (s->matched_entry) {
5923 s->first_displayed_entry = s->matched_entry;
5930 static const struct got_error *
5931 show_ref_view(struct tog_view *view)
5933 const struct got_error *err = NULL;
5934 struct tog_ref_view_state *s = &view->state.ref;
5935 struct tog_reflist_entry *re;
5938 struct tog_color *tc;
5940 int limit = view->nlines;
5942 werase(view->window);
5949 re = s->first_displayed_entry;
5951 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5953 return got_error_from_errno("asprintf");
5955 err = format_line(&wline, &width, line, view->ncols, 0);
5960 if (view_needs_focus_indication(view))
5961 wstandout(view->window);
5962 waddwstr(view->window, wline);
5963 if (view_needs_focus_indication(view))
5964 wstandend(view->window);
5969 if (width < view->ncols - 1)
5970 waddch(view->window, '\n');
5975 while (re && limit > 0) {
5978 if (got_ref_is_symbolic(re->ref)) {
5979 if (asprintf(&line, "%s -> %s",
5980 got_ref_get_name(re->ref),
5981 got_ref_get_symref_target(re->ref)) == -1)
5982 return got_error_from_errno("asprintf");
5983 } else if (s->show_ids) {
5984 struct got_object_id *id;
5986 err = got_ref_resolve(&id, s->repo, re->ref);
5989 err = got_object_id_str(&id_str, id);
5994 if (asprintf(&line, "%s: %s",
5995 got_ref_get_name(re->ref), id_str) == -1) {
5996 err = got_error_from_errno("asprintf");
6004 line = strdup(got_ref_get_name(re->ref));
6006 return got_error_from_errno("strdup");
6009 err = format_line(&wline, &width, line, view->ncols, 0);
6014 if (n == s->selected) {
6016 wstandout(view->window);
6017 s->selected_entry = re;
6019 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6021 wattr_on(view->window,
6022 COLOR_PAIR(tc->colorpair), NULL);
6023 waddwstr(view->window, wline);
6025 wattr_off(view->window,
6026 COLOR_PAIR(tc->colorpair), NULL);
6027 if (width < view->ncols - 1)
6028 waddch(view->window, '\n');
6029 if (n == s->selected && view->focussed)
6030 wstandend(view->window);
6036 s->last_displayed_entry = re;
6039 re = TAILQ_NEXT(re, entry);
6046 static const struct got_error *
6047 browse_ref_tree(struct tog_view **new_view, int begin_x,
6048 struct tog_reflist_entry *re, struct got_repository *repo)
6050 const struct got_error *err = NULL;
6051 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6052 struct got_tree_object *tree = NULL;
6053 struct tog_view *tree_view;
6057 err = resolve_reflist_entry(&commit_id, re, repo);
6059 if (err->code != GOT_ERR_OBJ_TYPE)
6065 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6069 err = got_object_open_as_tree(&tree, repo, tree_id);
6073 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6074 if (tree_view == NULL) {
6075 err = got_error_from_errno("view_open");
6079 err = open_tree_view(tree_view, tree, commit_id,
6080 got_ref_get_name(re->ref), repo);
6084 *new_view = tree_view;
6090 got_object_tree_close(tree);
6094 static const struct got_error *
6095 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6097 const struct got_error *err = NULL;
6098 struct tog_ref_view_state *s = &view->state.ref;
6099 struct tog_view *log_view, *tree_view;
6104 s->show_ids = !s->show_ids;
6108 if (!s->selected_entry)
6110 if (view_is_parent_view(view))
6111 begin_x = view_split_begin_x(view->begin_x);
6112 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6115 log_view->focussed = 1;
6116 if (view_is_parent_view(view)) {
6117 err = view_close_child(view);
6120 view_set_child(view, log_view);
6121 view->focus_child = 1;
6123 *new_view = log_view;
6126 if (!s->selected_entry)
6128 if (view_is_parent_view(view))
6129 begin_x = view_split_begin_x(view->begin_x);
6130 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6132 if (err || tree_view == NULL)
6135 tree_view->focussed = 1;
6136 if (view_is_parent_view(view)) {
6137 err = view_close_child(view);
6140 view_set_child(view, tree_view);
6141 view->focus_child = 1;
6143 *new_view = tree_view;
6147 if (s->selected > 0) {
6151 ref_scroll_up(s, 1);
6155 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6157 ref_scroll_up(s, MAX(0, view->nlines - 1));
6161 if (s->selected < s->ndisplayed - 1) {
6165 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6166 /* can't scroll any further */
6168 ref_scroll_down(s, 1);
6172 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6173 /* can't scroll any further; move cursor down */
6174 if (s->selected < s->ndisplayed - 1)
6175 s->selected = s->ndisplayed - 1;
6178 ref_scroll_down(s, view->nlines - 1);
6182 err = tog_load_refs(s->repo);
6185 ref_view_free_refs(s);
6186 err = ref_view_load_refs(s);
6189 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6190 s->selected = view->nlines - 2;
6203 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6208 static const struct got_error *
6209 cmd_ref(int argc, char *argv[])
6211 const struct got_error *error;
6212 struct got_repository *repo = NULL;
6213 struct got_worktree *worktree = NULL;
6214 char *cwd = NULL, *repo_path = NULL;
6216 struct tog_view *view;
6218 while ((ch = getopt(argc, argv, "r:")) != -1) {
6221 repo_path = realpath(optarg, NULL);
6222 if (repo_path == NULL)
6223 return got_error_from_errno2("realpath",
6238 if (repo_path == NULL) {
6239 cwd = getcwd(NULL, 0);
6241 return got_error_from_errno("getcwd");
6242 error = got_worktree_open(&worktree, cwd);
6243 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6247 strdup(got_worktree_get_repo_path(worktree));
6249 repo_path = strdup(cwd);
6250 if (repo_path == NULL) {
6251 error = got_error_from_errno("strdup");
6256 error = got_repo_open(&repo, repo_path, NULL);
6262 error = apply_unveil(got_repo_get_path(repo), NULL);
6266 error = tog_load_refs(repo);
6270 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6272 error = got_error_from_errno("view_open");
6276 error = open_ref_view(view, repo);
6281 /* Release work tree lock. */
6282 got_worktree_close(worktree);
6285 error = view_loop(view);
6290 got_repo_close(repo);
6296 list_commands(FILE *fp)
6300 fprintf(fp, "commands:");
6301 for (i = 0; i < nitems(tog_commands); i++) {
6302 struct tog_cmd *cmd = &tog_commands[i];
6303 fprintf(fp, " %s", cmd->name);
6309 usage(int hflag, int status)
6311 FILE *fp = (status == 0) ? stdout : stderr;
6313 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6316 fprintf(fp, "lazy usage: %s path\n", getprogname());
6323 make_argv(int argc, ...)
6331 argv = calloc(argc, sizeof(char *));
6334 for (i = 0; i < argc; i++) {
6335 argv[i] = strdup(va_arg(ap, char *));
6336 if (argv[i] == NULL)
6345 * Try to convert 'tog path' into a 'tog log path' command.
6346 * The user could simply have mistyped the command rather than knowingly
6347 * provided a path. So check whether argv[0] can in fact be resolved
6348 * to a path in the HEAD commit and print a special error if not.
6349 * This hack is for mpi@ <3
6351 static const struct got_error *
6352 tog_log_with_path(int argc, char *argv[])
6354 const struct got_error *error = NULL;
6355 struct tog_cmd *cmd = NULL;
6356 struct got_repository *repo = NULL;
6357 struct got_worktree *worktree = NULL;
6358 struct got_object_id *commit_id = NULL, *id = NULL;
6359 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6360 char *commit_id_str = NULL, **cmd_argv = NULL;
6362 cwd = getcwd(NULL, 0);
6364 return got_error_from_errno("getcwd");
6366 error = got_worktree_open(&worktree, cwd);
6367 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6371 repo_path = strdup(got_worktree_get_repo_path(worktree));
6373 repo_path = strdup(cwd);
6374 if (repo_path == NULL) {
6375 error = got_error_from_errno("strdup");
6379 error = got_repo_open(&repo, repo_path, NULL);
6383 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6388 error = tog_load_refs(repo);
6391 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6392 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6393 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6398 got_worktree_close(worktree);
6402 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6404 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6406 fprintf(stderr, "%s: '%s' is no known command or path\n",
6407 getprogname(), argv[0]);
6412 got_repo_close(repo);
6415 error = got_object_id_str(&commit_id_str, commit_id);
6419 cmd = &tog_commands[0]; /* log */
6421 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6422 error = cmd->cmd_main(argc, cmd_argv);
6425 got_repo_close(repo);
6427 got_worktree_close(worktree);
6429 free(commit_id_str);
6436 for (i = 0; i < argc; i++)
6445 main(int argc, char *argv[])
6447 const struct got_error *error = NULL;
6448 struct tog_cmd *cmd = NULL;
6449 int ch, hflag = 0, Vflag = 0;
6450 char **cmd_argv = NULL;
6451 static struct option longopts[] = {
6452 { "version", no_argument, NULL, 'V' },
6456 setlocale(LC_CTYPE, "");
6458 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6478 got_version_print_str();
6483 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6491 /* Build an argument vector which runs a default command. */
6492 cmd = &tog_commands[0];
6494 cmd_argv = make_argv(argc, cmd->name);
6498 /* Did the user specify a command? */
6499 for (i = 0; i < nitems(tog_commands); i++) {
6500 if (strncmp(tog_commands[i].name, argv[0],
6501 strlen(argv[0])) == 0) {
6502 cmd = &tog_commands[i];
6511 /* No command specified; try log with a path */
6512 error = tog_log_with_path(argc, argv);
6517 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6524 for (i = 0; i < argc; i++)
6529 if (error && error->code != GOT_ERR_CANCELLED)
6530 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);