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) != 0) {
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) != 0 && 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);
4240 if (err || blame->nlines == 0)
4243 /* Don't include \n at EOF in the blame line count. */
4244 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4247 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4248 if (blame->lines == NULL) {
4249 err = got_error_from_errno("calloc");
4253 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4257 blame->cb_args.view = view;
4258 blame->cb_args.lines = blame->lines;
4259 blame->cb_args.nlines = blame->nlines;
4260 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4261 if (blame->cb_args.commit_id == NULL) {
4262 err = got_error_from_errno("got_object_id_dup");
4265 blame->cb_args.quit = &s->done;
4267 blame->thread_args.path = s->path;
4268 blame->thread_args.repo = thread_repo;
4269 blame->thread_args.cb_args = &blame->cb_args;
4270 blame->thread_args.complete = &s->blame_complete;
4271 blame->thread_args.cancel_cb = cancel_blame_view;
4272 blame->thread_args.cancel_arg = &s->done;
4273 s->blame_complete = 0;
4275 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4276 s->first_displayed_line = 1;
4277 s->last_displayed_line = view->nlines;
4278 s->selected_line = 1;
4283 got_object_blob_close(blob);
4290 static const struct got_error *
4291 open_blame_view(struct tog_view *view, char *path,
4292 struct got_object_id *commit_id, struct got_repository *repo)
4294 const struct got_error *err = NULL;
4295 struct tog_blame_view_state *s = &view->state.blame;
4297 SIMPLEQ_INIT(&s->blamed_commits);
4299 s->path = strdup(path);
4300 if (s->path == NULL)
4301 return got_error_from_errno("strdup");
4303 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4309 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4310 s->first_displayed_line = 1;
4311 s->last_displayed_line = view->nlines;
4312 s->selected_line = 1;
4313 s->blame_complete = 0;
4315 s->commit_id = commit_id;
4316 memset(&s->blame, 0, sizeof(s->blame));
4318 SIMPLEQ_INIT(&s->colors);
4319 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4320 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4321 get_color_value("TOG_COLOR_COMMIT"));
4326 view->show = show_blame_view;
4327 view->input = input_blame_view;
4328 view->close = close_blame_view;
4329 view->search_start = search_start_blame_view;
4330 view->search_next = search_next_blame_view;
4332 return run_blame(view);
4335 static const struct got_error *
4336 close_blame_view(struct tog_view *view)
4338 const struct got_error *err = NULL;
4339 struct tog_blame_view_state *s = &view->state.blame;
4341 if (s->blame.thread)
4342 err = stop_blame(&s->blame);
4344 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4345 struct got_object_qid *blamed_commit;
4346 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4347 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4348 got_object_qid_free(blamed_commit);
4352 free_colors(&s->colors);
4357 static const struct got_error *
4358 search_start_blame_view(struct tog_view *view)
4360 struct tog_blame_view_state *s = &view->state.blame;
4362 s->matched_line = 0;
4366 static const struct got_error *
4367 search_next_blame_view(struct tog_view *view)
4369 struct tog_blame_view_state *s = &view->state.blame;
4372 size_t linesize = 0;
4375 if (!view->searching) {
4376 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4380 if (s->matched_line) {
4381 if (view->searching == TOG_SEARCH_FORWARD)
4382 lineno = s->matched_line + 1;
4384 lineno = s->matched_line - 1;
4386 if (view->searching == TOG_SEARCH_FORWARD)
4389 lineno = s->blame.nlines;
4395 if (lineno <= 0 || lineno > s->blame.nlines) {
4396 if (s->matched_line == 0) {
4397 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4401 if (view->searching == TOG_SEARCH_FORWARD)
4404 lineno = s->blame.nlines;
4407 offset = s->blame.line_offsets[lineno - 1];
4408 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4410 return got_error_from_errno("fseeko");
4412 linelen = getline(&line, &linesize, s->blame.f);
4413 if (linelen != -1 &&
4414 match_line(line, &view->regex, 1, &view->regmatch)) {
4415 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4416 s->matched_line = lineno;
4419 if (view->searching == TOG_SEARCH_FORWARD)
4426 if (s->matched_line) {
4427 s->first_displayed_line = s->matched_line;
4428 s->selected_line = 1;
4434 static const struct got_error *
4435 show_blame_view(struct tog_view *view)
4437 const struct got_error *err = NULL;
4438 struct tog_blame_view_state *s = &view->state.blame;
4441 if (s->blame.thread == NULL) {
4442 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4443 &s->blame.thread_args);
4445 return got_error_set_errno(errcode, "pthread_create");
4447 halfdelay(1); /* fast refresh while annotating */
4450 if (s->blame_complete)
4451 halfdelay(10); /* disable fast refresh */
4453 err = draw_blame(view);
4459 static const struct got_error *
4460 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4462 const struct got_error *err = NULL, *thread_err = NULL;
4463 struct tog_view *diff_view;
4464 struct tog_blame_view_state *s = &view->state.blame;
4473 if (s->selected_line > 1)
4475 else if (s->selected_line == 1 &&
4476 s->first_displayed_line > 1)
4477 s->first_displayed_line--;
4481 if (s->first_displayed_line == 1) {
4482 s->selected_line = 1;
4485 if (s->first_displayed_line > view->nlines - 2)
4486 s->first_displayed_line -=
4489 s->first_displayed_line = 1;
4493 if (s->selected_line < view->nlines - 2 &&
4494 s->first_displayed_line +
4495 s->selected_line <= s->blame.nlines)
4497 else if (s->last_displayed_line <
4499 s->first_displayed_line++;
4503 struct got_object_id *id = NULL;
4504 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4505 s->first_displayed_line, s->selected_line);
4509 struct got_commit_object *commit;
4510 struct got_object_qid *pid;
4511 struct got_object_id *blob_id = NULL;
4513 err = got_object_open_as_commit(&commit,
4517 pid = SIMPLEQ_FIRST(
4518 got_object_commit_get_parent_ids(commit));
4520 got_object_commit_close(commit);
4523 /* Check if path history ends here. */
4524 err = got_object_id_by_path(&blob_id, s->repo,
4527 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4529 got_object_commit_close(commit);
4532 err = got_object_get_type(&obj_type, s->repo,
4535 /* Can't blame non-blob type objects. */
4536 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4537 got_object_commit_close(commit);
4540 err = got_object_qid_alloc(&s->blamed_commit,
4542 got_object_commit_close(commit);
4544 if (got_object_id_cmp(id,
4545 s->blamed_commit->id) == 0)
4547 err = got_object_qid_alloc(&s->blamed_commit,
4553 thread_err = stop_blame(&s->blame);
4557 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4558 s->blamed_commit, entry);
4559 err = run_blame(view);
4565 struct got_object_qid *first;
4566 first = SIMPLEQ_FIRST(&s->blamed_commits);
4567 if (!got_object_id_cmp(first->id, s->commit_id))
4570 thread_err = stop_blame(&s->blame);
4574 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4575 got_object_qid_free(s->blamed_commit);
4577 SIMPLEQ_FIRST(&s->blamed_commits);
4578 err = run_blame(view);
4585 struct got_object_id *id = NULL;
4586 struct got_object_qid *pid;
4587 struct got_commit_object *commit = NULL;
4588 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4589 s->first_displayed_line, s->selected_line);
4592 err = got_object_open_as_commit(&commit, s->repo, id);
4595 pid = SIMPLEQ_FIRST(
4596 got_object_commit_get_parent_ids(commit));
4597 if (view_is_parent_view(view))
4598 begin_x = view_split_begin_x(view->begin_x);
4599 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4600 if (diff_view == NULL) {
4601 got_object_commit_close(commit);
4602 err = got_error_from_errno("view_open");
4605 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4606 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4607 got_object_commit_close(commit);
4609 view_close(diff_view);
4613 diff_view->focussed = 1;
4614 if (view_is_parent_view(view)) {
4615 err = view_close_child(view);
4618 view_set_child(view, diff_view);
4619 view->focus_child = 1;
4621 *new_view = diff_view;
4629 if (s->last_displayed_line >= s->blame.nlines &&
4630 s->selected_line >= MIN(s->blame.nlines,
4631 view->nlines - 2)) {
4634 if (s->last_displayed_line >= s->blame.nlines &&
4635 s->selected_line < view->nlines - 2) {
4636 s->selected_line = MIN(s->blame.nlines,
4640 if (s->last_displayed_line + view->nlines - 2
4642 s->first_displayed_line +=
4645 s->first_displayed_line =
4650 if (s->selected_line > view->nlines - 2) {
4651 s->selected_line = MIN(s->blame.nlines,
4658 return thread_err ? thread_err : err;
4661 static const struct got_error *
4662 cmd_blame(int argc, char *argv[])
4664 const struct got_error *error;
4665 struct got_repository *repo = NULL;
4666 struct got_worktree *worktree = NULL;
4667 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4668 char *link_target = NULL;
4669 struct got_object_id *commit_id = NULL;
4670 char *commit_id_str = NULL;
4672 struct tog_view *view;
4674 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4677 commit_id_str = optarg;
4680 repo_path = realpath(optarg, NULL);
4681 if (repo_path == NULL)
4682 return got_error_from_errno2("realpath",
4697 if (repo_path == NULL) {
4698 cwd = getcwd(NULL, 0);
4700 return got_error_from_errno("getcwd");
4701 error = got_worktree_open(&worktree, cwd);
4702 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4706 strdup(got_worktree_get_repo_path(worktree));
4708 repo_path = strdup(cwd);
4709 if (repo_path == NULL) {
4710 error = got_error_from_errno("strdup");
4715 error = got_repo_open(&repo, repo_path, NULL);
4719 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4726 error = apply_unveil(got_repo_get_path(repo), NULL);
4730 error = tog_load_refs(repo);
4734 if (commit_id_str == NULL) {
4735 struct got_reference *head_ref;
4736 error = got_ref_open(&head_ref, repo, worktree ?
4737 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4740 error = got_ref_resolve(&commit_id, repo, head_ref);
4741 got_ref_close(head_ref);
4743 error = got_repo_match_object_id(&commit_id, NULL,
4744 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4749 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4751 error = got_error_from_errno("view_open");
4755 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4760 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4765 /* Release work tree lock. */
4766 got_worktree_close(worktree);
4769 error = view_loop(view);
4777 got_worktree_close(worktree);
4779 got_repo_close(repo);
4784 static const struct got_error *
4785 draw_tree_entries(struct tog_view *view, const char *parent_path)
4787 struct tog_tree_view_state *s = &view->state.tree;
4788 const struct got_error *err = NULL;
4789 struct got_tree_entry *te;
4791 struct tog_color *tc;
4792 int width, n, i, nentries;
4793 int limit = view->nlines;
4797 werase(view->window);
4802 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4805 if (view_needs_focus_indication(view))
4806 wstandout(view->window);
4807 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4809 wattr_on(view->window,
4810 COLOR_PAIR(tc->colorpair), NULL);
4811 waddwstr(view->window, wline);
4813 wattr_off(view->window,
4814 COLOR_PAIR(tc->colorpair), NULL);
4815 if (view_needs_focus_indication(view))
4816 wstandend(view->window);
4819 if (width < view->ncols - 1)
4820 waddch(view->window, '\n');
4823 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4826 waddwstr(view->window, wline);
4829 if (width < view->ncols - 1)
4830 waddch(view->window, '\n');
4833 waddch(view->window, '\n');
4837 if (s->first_displayed_entry == NULL) {
4838 te = got_object_tree_get_first_entry(s->tree);
4839 if (s->selected == 0) {
4841 wstandout(view->window);
4842 s->selected_entry = NULL;
4844 waddstr(view->window, " ..\n"); /* parent directory */
4845 if (s->selected == 0 && view->focussed)
4846 wstandend(view->window);
4853 te = s->first_displayed_entry;
4856 nentries = got_object_tree_get_nentries(s->tree);
4857 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4858 char *line = NULL, *id_str = NULL, *link_target = NULL;
4859 const char *modestr = "";
4862 te = got_object_tree_get_entry(s->tree, i);
4863 mode = got_tree_entry_get_mode(te);
4866 err = got_object_id_str(&id_str,
4867 got_tree_entry_get_id(te));
4869 return got_error_from_errno(
4870 "got_object_id_str");
4872 if (got_object_tree_entry_is_submodule(te))
4874 else if (S_ISLNK(mode)) {
4877 err = got_tree_entry_get_symlink_target(&link_target,
4883 for (i = 0; i < strlen(link_target); i++) {
4884 if (!isprint((unsigned char)link_target[i]))
4885 link_target[i] = '?';
4889 else if (S_ISDIR(mode))
4891 else if (mode & S_IXUSR)
4893 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4894 got_tree_entry_get_name(te), modestr,
4895 link_target ? " -> ": "",
4896 link_target ? link_target : "") == -1) {
4899 return got_error_from_errno("asprintf");
4903 err = format_line(&wline, &width, line, view->ncols, 0);
4908 if (n == s->selected) {
4910 wstandout(view->window);
4911 s->selected_entry = te;
4913 tc = match_color(&s->colors, line);
4915 wattr_on(view->window,
4916 COLOR_PAIR(tc->colorpair), NULL);
4917 waddwstr(view->window, wline);
4919 wattr_off(view->window,
4920 COLOR_PAIR(tc->colorpair), NULL);
4921 if (width < view->ncols - 1)
4922 waddch(view->window, '\n');
4923 if (n == s->selected && view->focussed)
4924 wstandend(view->window);
4930 s->last_displayed_entry = te;
4939 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4941 struct got_tree_entry *te;
4942 int isroot = s->tree == s->root;
4945 if (s->first_displayed_entry == NULL)
4948 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4949 while (i++ < maxscroll) {
4952 s->first_displayed_entry = NULL;
4955 s->first_displayed_entry = te;
4956 te = got_tree_entry_get_prev(s->tree, te);
4961 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4963 struct got_tree_entry *next, *last;
4966 if (s->first_displayed_entry)
4967 next = got_tree_entry_get_next(s->tree,
4968 s->first_displayed_entry);
4970 next = got_object_tree_get_first_entry(s->tree);
4972 last = s->last_displayed_entry;
4973 while (next && last && n++ < maxscroll) {
4974 last = got_tree_entry_get_next(s->tree, last);
4976 s->first_displayed_entry = next;
4977 next = got_tree_entry_get_next(s->tree, next);
4982 static const struct got_error *
4983 tree_entry_path(char **path, struct tog_parent_trees *parents,
4984 struct got_tree_entry *te)
4986 const struct got_error *err = NULL;
4987 struct tog_parent_tree *pt;
4988 size_t len = 2; /* for leading slash and NUL */
4990 TAILQ_FOREACH(pt, parents, entry)
4991 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4994 len += strlen(got_tree_entry_get_name(te));
4996 *path = calloc(1, len);
4998 return got_error_from_errno("calloc");
5001 pt = TAILQ_LAST(parents, tog_parent_trees);
5003 const char *name = got_tree_entry_get_name(pt->selected_entry);
5004 if (strlcat(*path, name, len) >= len) {
5005 err = got_error(GOT_ERR_NO_SPACE);
5008 if (strlcat(*path, "/", len) >= len) {
5009 err = got_error(GOT_ERR_NO_SPACE);
5012 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5015 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5016 err = got_error(GOT_ERR_NO_SPACE);
5028 static const struct got_error *
5029 blame_tree_entry(struct tog_view **new_view, int begin_x,
5030 struct got_tree_entry *te, struct tog_parent_trees *parents,
5031 struct got_object_id *commit_id, struct got_repository *repo)
5033 const struct got_error *err = NULL;
5035 struct tog_view *blame_view;
5039 err = tree_entry_path(&path, parents, te);
5043 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5044 if (blame_view == NULL) {
5045 err = got_error_from_errno("view_open");
5049 err = open_blame_view(blame_view, path, commit_id, repo);
5051 if (err->code == GOT_ERR_CANCELLED)
5053 view_close(blame_view);
5055 *new_view = blame_view;
5061 static const struct got_error *
5062 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5063 struct tog_tree_view_state *s)
5065 struct tog_view *log_view;
5066 const struct got_error *err = NULL;
5071 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5072 if (log_view == NULL)
5073 return got_error_from_errno("view_open");
5075 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5079 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5082 view_close(log_view);
5084 *new_view = log_view;
5089 static const struct got_error *
5090 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5091 struct got_object_id *commit_id, const char *head_ref_name,
5092 struct got_repository *repo)
5094 const struct got_error *err = NULL;
5095 char *commit_id_str = NULL;
5096 struct tog_tree_view_state *s = &view->state.tree;
5098 TAILQ_INIT(&s->parents);
5100 err = got_object_id_str(&commit_id_str, commit_id);
5104 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5105 err = got_error_from_errno("asprintf");
5109 s->root = s->tree = root;
5110 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5111 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5112 s->commit_id = got_object_id_dup(commit_id);
5113 if (s->commit_id == NULL) {
5114 err = got_error_from_errno("got_object_id_dup");
5117 if (head_ref_name) {
5118 s->head_ref_name = strdup(head_ref_name);
5119 if (s->head_ref_name == NULL) {
5120 err = got_error_from_errno("strdup");
5126 SIMPLEQ_INIT(&s->colors);
5128 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5129 err = add_color(&s->colors, "\\$$",
5130 TOG_COLOR_TREE_SUBMODULE,
5131 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5134 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5135 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5137 free_colors(&s->colors);
5140 err = add_color(&s->colors, "/$",
5141 TOG_COLOR_TREE_DIRECTORY,
5142 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5144 free_colors(&s->colors);
5148 err = add_color(&s->colors, "\\*$",
5149 TOG_COLOR_TREE_EXECUTABLE,
5150 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5152 free_colors(&s->colors);
5156 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5157 get_color_value("TOG_COLOR_COMMIT"));
5159 free_colors(&s->colors);
5164 view->show = show_tree_view;
5165 view->input = input_tree_view;
5166 view->close = close_tree_view;
5167 view->search_start = search_start_tree_view;
5168 view->search_next = search_next_tree_view;
5170 free(commit_id_str);
5172 free(s->tree_label);
5173 s->tree_label = NULL;
5178 static const struct got_error *
5179 close_tree_view(struct tog_view *view)
5181 struct tog_tree_view_state *s = &view->state.tree;
5183 free_colors(&s->colors);
5184 free(s->tree_label);
5185 s->tree_label = NULL;
5187 s->commit_id = NULL;
5188 free(s->head_ref_name);
5189 s->head_ref_name = NULL;
5190 while (!TAILQ_EMPTY(&s->parents)) {
5191 struct tog_parent_tree *parent;
5192 parent = TAILQ_FIRST(&s->parents);
5193 TAILQ_REMOVE(&s->parents, parent, entry);
5197 if (s->tree != s->root)
5198 got_object_tree_close(s->tree);
5199 got_object_tree_close(s->root);
5203 static const struct got_error *
5204 search_start_tree_view(struct tog_view *view)
5206 struct tog_tree_view_state *s = &view->state.tree;
5208 s->matched_entry = NULL;
5213 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5215 regmatch_t regmatch;
5217 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5221 static const struct got_error *
5222 search_next_tree_view(struct tog_view *view)
5224 struct tog_tree_view_state *s = &view->state.tree;
5225 struct got_tree_entry *te = NULL;
5227 if (!view->searching) {
5228 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5232 if (s->matched_entry) {
5233 if (view->searching == TOG_SEARCH_FORWARD) {
5234 if (s->selected_entry)
5235 te = got_tree_entry_get_next(s->tree,
5238 te = got_object_tree_get_first_entry(s->tree);
5240 if (s->selected_entry == NULL)
5241 te = got_object_tree_get_last_entry(s->tree);
5243 te = got_tree_entry_get_prev(s->tree,
5247 if (view->searching == TOG_SEARCH_FORWARD)
5248 te = got_object_tree_get_first_entry(s->tree);
5250 te = got_object_tree_get_last_entry(s->tree);
5255 if (s->matched_entry == NULL) {
5256 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5259 if (view->searching == TOG_SEARCH_FORWARD)
5260 te = got_object_tree_get_first_entry(s->tree);
5262 te = got_object_tree_get_last_entry(s->tree);
5265 if (match_tree_entry(te, &view->regex)) {
5266 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5267 s->matched_entry = te;
5271 if (view->searching == TOG_SEARCH_FORWARD)
5272 te = got_tree_entry_get_next(s->tree, te);
5274 te = got_tree_entry_get_prev(s->tree, te);
5277 if (s->matched_entry) {
5278 s->first_displayed_entry = s->matched_entry;
5285 static const struct got_error *
5286 show_tree_view(struct tog_view *view)
5288 const struct got_error *err = NULL;
5289 struct tog_tree_view_state *s = &view->state.tree;
5292 err = tree_entry_path(&parent_path, &s->parents, NULL);
5296 err = draw_tree_entries(view, parent_path);
5303 static const struct got_error *
5304 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5306 const struct got_error *err = NULL;
5307 struct tog_tree_view_state *s = &view->state.tree;
5308 struct tog_view *log_view, *ref_view;
5313 s->show_ids = !s->show_ids;
5316 if (!s->selected_entry)
5318 if (view_is_parent_view(view))
5319 begin_x = view_split_begin_x(view->begin_x);
5320 err = log_selected_tree_entry(&log_view, begin_x, s);
5322 log_view->focussed = 1;
5323 if (view_is_parent_view(view)) {
5324 err = view_close_child(view);
5327 view_set_child(view, log_view);
5328 view->focus_child = 1;
5330 *new_view = log_view;
5333 if (view_is_parent_view(view))
5334 begin_x = view_split_begin_x(view->begin_x);
5335 ref_view = view_open(view->nlines, view->ncols,
5336 view->begin_y, begin_x, TOG_VIEW_REF);
5337 if (ref_view == NULL)
5338 return got_error_from_errno("view_open");
5339 err = open_ref_view(ref_view, s->repo);
5341 view_close(ref_view);
5345 ref_view->focussed = 1;
5346 if (view_is_parent_view(view)) {
5347 err = view_close_child(view);
5350 view_set_child(view, ref_view);
5351 view->focus_child = 1;
5353 *new_view = ref_view;
5357 if (s->selected > 0) {
5361 tree_scroll_up(s, 1);
5365 if (s->tree == s->root) {
5366 if (got_object_tree_get_first_entry(s->tree) ==
5367 s->first_displayed_entry)
5370 if (s->first_displayed_entry == NULL)
5373 tree_scroll_up(s, MAX(0, view->nlines - 3));
5377 if (s->selected < s->ndisplayed - 1) {
5381 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5383 /* can't scroll any further */
5385 tree_scroll_down(s, 1);
5389 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5391 /* can't scroll any further; move cursor down */
5392 if (s->selected < s->ndisplayed - 1)
5393 s->selected = s->ndisplayed - 1;
5396 tree_scroll_down(s, view->nlines - 3);
5401 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5402 struct tog_parent_tree *parent;
5403 /* user selected '..' */
5404 if (s->tree == s->root)
5406 parent = TAILQ_FIRST(&s->parents);
5407 TAILQ_REMOVE(&s->parents, parent,
5409 got_object_tree_close(s->tree);
5410 s->tree = parent->tree;
5411 s->first_displayed_entry =
5412 parent->first_displayed_entry;
5414 parent->selected_entry;
5415 s->selected = parent->selected;
5417 } else if (S_ISDIR(got_tree_entry_get_mode(
5418 s->selected_entry))) {
5419 struct got_tree_object *subtree;
5420 err = got_object_open_as_tree(&subtree, s->repo,
5421 got_tree_entry_get_id(s->selected_entry));
5424 err = tree_view_visit_subtree(s, subtree);
5426 got_object_tree_close(subtree);
5429 } else if (S_ISREG(got_tree_entry_get_mode(
5430 s->selected_entry))) {
5431 struct tog_view *blame_view;
5432 int begin_x = view_is_parent_view(view) ?
5433 view_split_begin_x(view->begin_x) : 0;
5435 err = blame_tree_entry(&blame_view, begin_x,
5436 s->selected_entry, &s->parents,
5437 s->commit_id, s->repo);
5441 blame_view->focussed = 1;
5442 if (view_is_parent_view(view)) {
5443 err = view_close_child(view);
5446 view_set_child(view, blame_view);
5447 view->focus_child = 1;
5449 *new_view = blame_view;
5453 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5454 s->selected = view->nlines - 4;
5467 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5472 static const struct got_error *
5473 cmd_tree(int argc, char *argv[])
5475 const struct got_error *error;
5476 struct got_repository *repo = NULL;
5477 struct got_worktree *worktree = NULL;
5478 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5479 struct got_object_id *commit_id = NULL;
5480 const char *commit_id_arg = NULL;
5482 struct got_commit_object *commit = NULL;
5483 struct got_tree_object *tree = NULL;
5484 struct got_reference *ref = NULL;
5485 const char *head_ref_name = NULL;
5487 struct tog_view *view;
5489 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5492 commit_id_arg = optarg;
5495 repo_path = realpath(optarg, NULL);
5496 if (repo_path == NULL)
5497 return got_error_from_errno2("realpath",
5512 if (repo_path == NULL) {
5513 cwd = getcwd(NULL, 0);
5515 return got_error_from_errno("getcwd");
5516 error = got_worktree_open(&worktree, cwd);
5517 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5521 strdup(got_worktree_get_repo_path(worktree));
5523 repo_path = strdup(cwd);
5524 if (repo_path == NULL) {
5525 error = got_error_from_errno("strdup");
5530 error = got_repo_open(&repo, repo_path, NULL);
5534 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5541 error = apply_unveil(got_repo_get_path(repo), NULL);
5545 error = tog_load_refs(repo);
5549 if (commit_id_arg == NULL) {
5550 error = got_repo_match_object_id(&commit_id, &label,
5551 worktree ? got_worktree_get_head_ref_name(worktree) :
5552 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5555 head_ref_name = label;
5557 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5559 head_ref_name = got_ref_get_name(ref);
5560 else if (error->code != GOT_ERR_NOT_REF)
5562 error = got_repo_match_object_id(&commit_id, NULL,
5563 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5568 error = got_object_open_as_commit(&commit, repo, commit_id);
5572 error = got_object_open_as_tree(&tree, repo,
5573 got_object_commit_get_tree_id(commit));
5577 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5579 error = got_error_from_errno("view_open");
5582 error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5585 if (!got_path_is_root_dir(in_repo_path)) {
5586 error = tree_view_walk_path(&view->state.tree, commit_id,
5593 /* Release work tree lock. */
5594 got_worktree_close(worktree);
5597 error = view_loop(view);
5606 got_object_commit_close(commit);
5608 got_object_tree_close(tree);
5610 got_repo_close(repo);
5615 static const struct got_error *
5616 ref_view_load_refs(struct tog_ref_view_state *s)
5618 struct got_reflist_entry *sre;
5619 struct tog_reflist_entry *re;
5622 TAILQ_FOREACH(sre, &tog_refs, entry) {
5623 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5626 re = malloc(sizeof(*re));
5628 return got_error_from_errno("malloc");
5630 re->ref = got_ref_dup(sre->ref);
5631 if (re->ref == NULL)
5632 return got_error_from_errno("got_ref_dup");
5633 re->idx = s->nrefs++;
5634 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5637 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5642 ref_view_free_refs(struct tog_ref_view_state *s)
5644 struct tog_reflist_entry *re;
5646 while (!TAILQ_EMPTY(&s->refs)) {
5647 re = TAILQ_FIRST(&s->refs);
5648 TAILQ_REMOVE(&s->refs, re, entry);
5649 got_ref_close(re->ref);
5654 static const struct got_error *
5655 open_ref_view(struct tog_view *view, struct got_repository *repo)
5657 const struct got_error *err = NULL;
5658 struct tog_ref_view_state *s = &view->state.ref;
5660 s->selected_entry = 0;
5663 TAILQ_INIT(&s->refs);
5664 SIMPLEQ_INIT(&s->colors);
5666 err = ref_view_load_refs(s);
5670 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5671 err = add_color(&s->colors, "^refs/heads/",
5672 TOG_COLOR_REFS_HEADS,
5673 get_color_value("TOG_COLOR_REFS_HEADS"));
5677 err = add_color(&s->colors, "^refs/tags/",
5678 TOG_COLOR_REFS_TAGS,
5679 get_color_value("TOG_COLOR_REFS_TAGS"));
5683 err = add_color(&s->colors, "^refs/remotes/",
5684 TOG_COLOR_REFS_REMOTES,
5685 get_color_value("TOG_COLOR_REFS_REMOTES"));
5690 view->show = show_ref_view;
5691 view->input = input_ref_view;
5692 view->close = close_ref_view;
5693 view->search_start = search_start_ref_view;
5694 view->search_next = search_next_ref_view;
5697 free_colors(&s->colors);
5701 static const struct got_error *
5702 close_ref_view(struct tog_view *view)
5704 struct tog_ref_view_state *s = &view->state.ref;
5706 ref_view_free_refs(s);
5707 free_colors(&s->colors);
5712 static const struct got_error *
5713 resolve_reflist_entry(struct got_object_id **commit_id,
5714 struct tog_reflist_entry *re, struct got_repository *repo)
5716 const struct got_error *err = NULL;
5717 struct got_object_id *obj_id;
5718 struct got_tag_object *tag = NULL;
5723 err = got_ref_resolve(&obj_id, repo, re->ref);
5727 err = got_object_get_type(&obj_type, repo, obj_id);
5732 case GOT_OBJ_TYPE_COMMIT:
5733 *commit_id = obj_id;
5735 case GOT_OBJ_TYPE_TAG:
5736 err = got_object_open_as_tag(&tag, repo, obj_id);
5740 err = got_object_get_type(&obj_type, repo,
5741 got_object_tag_get_object_id(tag));
5744 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5745 err = got_error(GOT_ERR_OBJ_TYPE);
5748 *commit_id = got_object_id_dup(
5749 got_object_tag_get_object_id(tag));
5750 if (*commit_id == NULL) {
5751 err = got_error_from_errno("got_object_id_dup");
5756 err = got_error(GOT_ERR_OBJ_TYPE);
5762 got_object_tag_close(tag);
5770 static const struct got_error *
5771 log_ref_entry(struct tog_view **new_view, int begin_x,
5772 struct tog_reflist_entry *re, struct got_repository *repo)
5774 struct tog_view *log_view;
5775 const struct got_error *err = NULL;
5776 struct got_object_id *commit_id = NULL;
5780 err = resolve_reflist_entry(&commit_id, re, repo);
5782 if (err->code != GOT_ERR_OBJ_TYPE)
5788 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5789 if (log_view == NULL) {
5790 err = got_error_from_errno("view_open");
5794 err = open_log_view(log_view, commit_id, repo,
5795 got_ref_get_name(re->ref), "", 0);
5798 view_close(log_view);
5800 *new_view = log_view;
5806 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5808 struct tog_reflist_entry *re;
5811 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5814 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5815 while (i++ < maxscroll) {
5818 s->first_displayed_entry = re;
5819 re = TAILQ_PREV(re, tog_reflist_head, entry);
5824 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5826 struct tog_reflist_entry *next, *last;
5829 if (s->first_displayed_entry)
5830 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5832 next = TAILQ_FIRST(&s->refs);
5834 last = s->last_displayed_entry;
5835 while (next && last && n++ < maxscroll) {
5836 last = TAILQ_NEXT(last, entry);
5838 s->first_displayed_entry = next;
5839 next = TAILQ_NEXT(next, entry);
5844 static const struct got_error *
5845 search_start_ref_view(struct tog_view *view)
5847 struct tog_ref_view_state *s = &view->state.ref;
5849 s->matched_entry = NULL;
5854 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5856 regmatch_t regmatch;
5858 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5862 static const struct got_error *
5863 search_next_ref_view(struct tog_view *view)
5865 struct tog_ref_view_state *s = &view->state.ref;
5866 struct tog_reflist_entry *re = NULL;
5868 if (!view->searching) {
5869 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5873 if (s->matched_entry) {
5874 if (view->searching == TOG_SEARCH_FORWARD) {
5875 if (s->selected_entry)
5876 re = TAILQ_NEXT(s->selected_entry, entry);
5878 re = TAILQ_PREV(s->selected_entry,
5879 tog_reflist_head, entry);
5881 if (s->selected_entry == NULL)
5882 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5884 re = TAILQ_PREV(s->selected_entry,
5885 tog_reflist_head, entry);
5888 if (view->searching == TOG_SEARCH_FORWARD)
5889 re = TAILQ_FIRST(&s->refs);
5891 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5896 if (s->matched_entry == NULL) {
5897 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5900 if (view->searching == TOG_SEARCH_FORWARD)
5901 re = TAILQ_FIRST(&s->refs);
5903 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5906 if (match_reflist_entry(re, &view->regex)) {
5907 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5908 s->matched_entry = re;
5912 if (view->searching == TOG_SEARCH_FORWARD)
5913 re = TAILQ_NEXT(re, entry);
5915 re = TAILQ_PREV(re, tog_reflist_head, entry);
5918 if (s->matched_entry) {
5919 s->first_displayed_entry = s->matched_entry;
5926 static const struct got_error *
5927 show_ref_view(struct tog_view *view)
5929 const struct got_error *err = NULL;
5930 struct tog_ref_view_state *s = &view->state.ref;
5931 struct tog_reflist_entry *re;
5934 struct tog_color *tc;
5936 int limit = view->nlines;
5938 werase(view->window);
5945 re = s->first_displayed_entry;
5947 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5949 return got_error_from_errno("asprintf");
5951 err = format_line(&wline, &width, line, view->ncols, 0);
5956 if (view_needs_focus_indication(view))
5957 wstandout(view->window);
5958 waddwstr(view->window, wline);
5959 if (view_needs_focus_indication(view))
5960 wstandend(view->window);
5965 if (width < view->ncols - 1)
5966 waddch(view->window, '\n');
5971 while (re && limit > 0) {
5974 if (got_ref_is_symbolic(re->ref)) {
5975 if (asprintf(&line, "%s -> %s",
5976 got_ref_get_name(re->ref),
5977 got_ref_get_symref_target(re->ref)) == -1)
5978 return got_error_from_errno("asprintf");
5979 } else if (s->show_ids) {
5980 struct got_object_id *id;
5982 err = got_ref_resolve(&id, s->repo, re->ref);
5985 err = got_object_id_str(&id_str, id);
5990 if (asprintf(&line, "%s: %s",
5991 got_ref_get_name(re->ref), id_str) == -1) {
5992 err = got_error_from_errno("asprintf");
6000 line = strdup(got_ref_get_name(re->ref));
6002 return got_error_from_errno("strdup");
6005 err = format_line(&wline, &width, line, view->ncols, 0);
6010 if (n == s->selected) {
6012 wstandout(view->window);
6013 s->selected_entry = re;
6015 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6017 wattr_on(view->window,
6018 COLOR_PAIR(tc->colorpair), NULL);
6019 waddwstr(view->window, wline);
6021 wattr_off(view->window,
6022 COLOR_PAIR(tc->colorpair), NULL);
6023 if (width < view->ncols - 1)
6024 waddch(view->window, '\n');
6025 if (n == s->selected && view->focussed)
6026 wstandend(view->window);
6032 s->last_displayed_entry = re;
6035 re = TAILQ_NEXT(re, entry);
6042 static const struct got_error *
6043 browse_ref_tree(struct tog_view **new_view, int begin_x,
6044 struct tog_reflist_entry *re, struct got_repository *repo)
6046 const struct got_error *err = NULL;
6047 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6048 struct got_tree_object *tree = NULL;
6049 struct tog_view *tree_view;
6053 err = resolve_reflist_entry(&commit_id, re, repo);
6055 if (err->code != GOT_ERR_OBJ_TYPE)
6061 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6065 err = got_object_open_as_tree(&tree, repo, tree_id);
6069 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6070 if (tree_view == NULL) {
6071 err = got_error_from_errno("view_open");
6075 err = open_tree_view(tree_view, tree, commit_id,
6076 got_ref_get_name(re->ref), repo);
6080 *new_view = tree_view;
6086 got_object_tree_close(tree);
6090 static const struct got_error *
6091 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6093 const struct got_error *err = NULL;
6094 struct tog_ref_view_state *s = &view->state.ref;
6095 struct tog_view *log_view, *tree_view;
6100 s->show_ids = !s->show_ids;
6104 if (!s->selected_entry)
6106 if (view_is_parent_view(view))
6107 begin_x = view_split_begin_x(view->begin_x);
6108 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6111 log_view->focussed = 1;
6112 if (view_is_parent_view(view)) {
6113 err = view_close_child(view);
6116 view_set_child(view, log_view);
6117 view->focus_child = 1;
6119 *new_view = log_view;
6122 if (!s->selected_entry)
6124 if (view_is_parent_view(view))
6125 begin_x = view_split_begin_x(view->begin_x);
6126 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6128 if (err || tree_view == NULL)
6131 tree_view->focussed = 1;
6132 if (view_is_parent_view(view)) {
6133 err = view_close_child(view);
6136 view_set_child(view, tree_view);
6137 view->focus_child = 1;
6139 *new_view = tree_view;
6143 if (s->selected > 0) {
6147 ref_scroll_up(s, 1);
6151 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6153 ref_scroll_up(s, MAX(0, view->nlines - 1));
6157 if (s->selected < s->ndisplayed - 1) {
6161 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6162 /* can't scroll any further */
6164 ref_scroll_down(s, 1);
6168 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6169 /* can't scroll any further; move cursor down */
6170 if (s->selected < s->ndisplayed - 1)
6171 s->selected = s->ndisplayed - 1;
6174 ref_scroll_down(s, view->nlines - 1);
6178 err = tog_load_refs(s->repo);
6181 ref_view_free_refs(s);
6182 err = ref_view_load_refs(s);
6185 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6186 s->selected = view->nlines - 2;
6199 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6204 static const struct got_error *
6205 cmd_ref(int argc, char *argv[])
6207 const struct got_error *error;
6208 struct got_repository *repo = NULL;
6209 struct got_worktree *worktree = NULL;
6210 char *cwd = NULL, *repo_path = NULL;
6212 struct tog_view *view;
6214 while ((ch = getopt(argc, argv, "r:")) != -1) {
6217 repo_path = realpath(optarg, NULL);
6218 if (repo_path == NULL)
6219 return got_error_from_errno2("realpath",
6234 if (repo_path == NULL) {
6235 cwd = getcwd(NULL, 0);
6237 return got_error_from_errno("getcwd");
6238 error = got_worktree_open(&worktree, cwd);
6239 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6243 strdup(got_worktree_get_repo_path(worktree));
6245 repo_path = strdup(cwd);
6246 if (repo_path == NULL) {
6247 error = got_error_from_errno("strdup");
6252 error = got_repo_open(&repo, repo_path, NULL);
6258 error = apply_unveil(got_repo_get_path(repo), NULL);
6262 error = tog_load_refs(repo);
6266 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6268 error = got_error_from_errno("view_open");
6272 error = open_ref_view(view, repo);
6277 /* Release work tree lock. */
6278 got_worktree_close(worktree);
6281 error = view_loop(view);
6286 got_repo_close(repo);
6292 list_commands(FILE *fp)
6296 fprintf(fp, "commands:");
6297 for (i = 0; i < nitems(tog_commands); i++) {
6298 struct tog_cmd *cmd = &tog_commands[i];
6299 fprintf(fp, " %s", cmd->name);
6305 usage(int hflag, int status)
6307 FILE *fp = (status == 0) ? stdout : stderr;
6309 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6312 fprintf(fp, "lazy usage: %s path\n", getprogname());
6319 make_argv(int argc, ...)
6327 argv = calloc(argc, sizeof(char *));
6330 for (i = 0; i < argc; i++) {
6331 argv[i] = strdup(va_arg(ap, char *));
6332 if (argv[i] == NULL)
6341 * Try to convert 'tog path' into a 'tog log path' command.
6342 * The user could simply have mistyped the command rather than knowingly
6343 * provided a path. So check whether argv[0] can in fact be resolved
6344 * to a path in the HEAD commit and print a special error if not.
6345 * This hack is for mpi@ <3
6347 static const struct got_error *
6348 tog_log_with_path(int argc, char *argv[])
6350 const struct got_error *error = NULL;
6351 struct tog_cmd *cmd = NULL;
6352 struct got_repository *repo = NULL;
6353 struct got_worktree *worktree = NULL;
6354 struct got_object_id *commit_id = NULL, *id = NULL;
6355 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6356 char *commit_id_str = NULL, **cmd_argv = NULL;
6358 cwd = getcwd(NULL, 0);
6360 return got_error_from_errno("getcwd");
6362 error = got_worktree_open(&worktree, cwd);
6363 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6367 repo_path = strdup(got_worktree_get_repo_path(worktree));
6369 repo_path = strdup(cwd);
6370 if (repo_path == NULL) {
6371 error = got_error_from_errno("strdup");
6375 error = got_repo_open(&repo, repo_path, NULL);
6379 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6384 error = tog_load_refs(repo);
6387 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6388 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6389 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6394 got_worktree_close(worktree);
6398 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6400 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6402 fprintf(stderr, "%s: '%s' is no known command or path\n",
6403 getprogname(), argv[0]);
6408 got_repo_close(repo);
6411 error = got_object_id_str(&commit_id_str, commit_id);
6415 cmd = &tog_commands[0]; /* log */
6417 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6418 error = cmd->cmd_main(argc, cmd_argv);
6421 got_repo_close(repo);
6423 got_worktree_close(worktree);
6425 free(commit_id_str);
6432 for (i = 0; i < argc; i++)
6441 main(int argc, char *argv[])
6443 const struct got_error *error = NULL;
6444 struct tog_cmd *cmd = NULL;
6445 int ch, hflag = 0, Vflag = 0;
6446 char **cmd_argv = NULL;
6447 static struct option longopts[] = {
6448 { "version", no_argument, NULL, 'V' },
6452 setlocale(LC_CTYPE, "");
6454 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6474 got_version_print_str();
6479 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6487 /* Build an argument vector which runs a default command. */
6488 cmd = &tog_commands[0];
6490 cmd_argv = make_argv(argc, cmd->name);
6494 /* Did the user specify a command? */
6495 for (i = 0; i < nitems(tog_commands); i++) {
6496 if (strncmp(tog_commands[i].name, argv[0],
6497 strlen(argv[0])) == 0) {
6498 cmd = &tog_commands[i];
6507 /* No command specified; try log with a path */
6508 error = tog_log_with_path(argc, argv);
6513 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6520 for (i = 0; i < argc; i++)
6525 if (error && error->code != GOT_ERR_CANCELLED)
6526 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);