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 = SIMPLEQ_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_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; /* TAILQ */
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 SIMPLEQ_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,
1311 char *smallerthan, *at;
1313 smallerthan = strchr(author, '<');
1314 if (smallerthan && smallerthan[1] != '\0')
1315 author = smallerthan + 1;
1316 at = strchr(author, '@');
1319 return format_line(wauthor, author_width, author, limit, col_tab_align);
1322 static const struct got_error *
1323 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1324 struct got_object_id *id, const size_t date_display_cols,
1325 int author_display_cols)
1327 struct tog_log_view_state *s = &view->state.log;
1328 const struct got_error *err = NULL;
1329 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1330 char *logmsg0 = NULL, *logmsg = NULL;
1331 char *author = NULL;
1332 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1333 int author_width, logmsg_width;
1334 char *newline, *line = NULL;
1336 const int avail = view->ncols;
1338 time_t committer_time;
1339 struct tog_color *tc;
1341 committer_time = got_object_commit_get_committer_time(commit);
1342 if (localtime_r(&committer_time, &tm) == NULL)
1343 return got_error_from_errno("localtime_r");
1344 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1346 return got_error(GOT_ERR_NO_SPACE);
1348 if (avail <= date_display_cols)
1349 limit = MIN(sizeof(datebuf) - 1, avail);
1351 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1352 tc = get_color(&s->colors, TOG_COLOR_DATE);
1354 wattr_on(view->window,
1355 COLOR_PAIR(tc->colorpair), NULL);
1356 waddnstr(view->window, datebuf, limit);
1358 wattr_off(view->window,
1359 COLOR_PAIR(tc->colorpair), NULL);
1366 err = got_object_id_str(&id_str, id);
1369 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1371 wattr_on(view->window,
1372 COLOR_PAIR(tc->colorpair), NULL);
1373 wprintw(view->window, "%.8s ", id_str);
1375 wattr_off(view->window,
1376 COLOR_PAIR(tc->colorpair), NULL);
1383 author = strdup(got_object_commit_get_author(commit));
1384 if (author == NULL) {
1385 err = got_error_from_errno("strdup");
1388 err = format_author(&wauthor, &author_width, author, avail - col, col);
1391 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1393 wattr_on(view->window,
1394 COLOR_PAIR(tc->colorpair), NULL);
1395 waddwstr(view->window, wauthor);
1397 wattr_off(view->window,
1398 COLOR_PAIR(tc->colorpair), NULL);
1399 col += author_width;
1400 while (col < avail && author_width < author_display_cols + 2) {
1401 waddch(view->window, ' ');
1408 err = got_object_commit_get_logmsg(&logmsg0, commit);
1412 while (*logmsg == '\n')
1414 newline = strchr(logmsg, '\n');
1417 limit = avail - col;
1418 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1421 waddwstr(view->window, wlogmsg);
1422 col += logmsg_width;
1423 while (col < avail) {
1424 waddch(view->window, ' ');
1436 static struct commit_queue_entry *
1437 alloc_commit_queue_entry(struct got_commit_object *commit,
1438 struct got_object_id *id)
1440 struct commit_queue_entry *entry;
1442 entry = calloc(1, sizeof(*entry));
1447 entry->commit = commit;
1452 pop_commit(struct commit_queue *commits)
1454 struct commit_queue_entry *entry;
1456 entry = TAILQ_FIRST(&commits->head);
1457 TAILQ_REMOVE(&commits->head, entry, entry);
1458 got_object_commit_close(entry->commit);
1459 commits->ncommits--;
1460 /* Don't free entry->id! It is owned by the commit graph. */
1465 free_commits(struct commit_queue *commits)
1467 while (!TAILQ_EMPTY(&commits->head))
1468 pop_commit(commits);
1471 static const struct got_error *
1472 match_commit(int *have_match, struct got_object_id *id,
1473 struct got_commit_object *commit, regex_t *regex)
1475 const struct got_error *err = NULL;
1476 regmatch_t regmatch;
1477 char *id_str = NULL, *logmsg = NULL;
1481 err = got_object_id_str(&id_str, id);
1485 err = got_object_commit_get_logmsg(&logmsg, commit);
1489 if (regexec(regex, got_object_commit_get_author(commit), 1,
1490 ®match, 0) == 0 ||
1491 regexec(regex, got_object_commit_get_committer(commit), 1,
1492 ®match, 0) == 0 ||
1493 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1494 regexec(regex, logmsg, 1, ®match, 0) == 0)
1502 static const struct got_error *
1503 queue_commits(struct tog_log_thread_args *a)
1505 const struct got_error *err = NULL;
1508 * We keep all commits open throughout the lifetime of the log
1509 * view in order to avoid having to re-fetch commits from disk
1510 * while updating the display.
1513 struct got_object_id *id;
1514 struct got_commit_object *commit;
1515 struct commit_queue_entry *entry;
1518 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1520 if (err || id == NULL)
1523 err = got_object_open_as_commit(&commit, a->repo, id);
1526 entry = alloc_commit_queue_entry(commit, id);
1527 if (entry == NULL) {
1528 err = got_error_from_errno("alloc_commit_queue_entry");
1532 errcode = pthread_mutex_lock(&tog_mutex);
1534 err = got_error_set_errno(errcode,
1535 "pthread_mutex_lock");
1539 entry->idx = a->commits->ncommits;
1540 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1541 a->commits->ncommits++;
1543 if (*a->searching == TOG_SEARCH_FORWARD &&
1544 !*a->search_next_done) {
1546 err = match_commit(&have_match, id, commit, a->regex);
1550 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1553 errcode = pthread_mutex_unlock(&tog_mutex);
1554 if (errcode && err == NULL)
1555 err = got_error_set_errno(errcode,
1556 "pthread_mutex_unlock");
1559 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1565 select_commit(struct tog_log_view_state *s)
1567 struct commit_queue_entry *entry;
1570 entry = s->first_displayed_entry;
1572 if (ncommits == s->selected) {
1573 s->selected_entry = entry;
1576 entry = TAILQ_NEXT(entry, entry);
1581 static const struct got_error *
1582 draw_commits(struct tog_view *view)
1584 const struct got_error *err = NULL;
1585 struct tog_log_view_state *s = &view->state.log;
1586 struct commit_queue_entry *entry = s->selected_entry;
1587 const int limit = view->nlines;
1589 int ncommits, author_cols = 4;
1590 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1591 char *refs_str = NULL;
1593 struct tog_color *tc;
1594 static const size_t date_display_cols = 12;
1596 if (s->selected_entry &&
1597 !(view->searching && view->search_next_done == 0)) {
1598 struct got_reflist_head *refs;
1599 err = got_object_id_str(&id_str, s->selected_entry->id);
1602 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1603 s->selected_entry->id);
1605 err = build_refs_str(&refs_str, refs,
1606 s->selected_entry->id, s->repo);
1612 if (s->thread_args.commits_needed == 0)
1613 halfdelay(10); /* disable fast refresh */
1615 if (s->thread_args.commits_needed > 0) {
1616 if (asprintf(&ncommits_str, " [%d/%d] %s",
1617 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1618 (view->searching && !view->search_next_done) ?
1619 "searching..." : "loading...") == -1) {
1620 err = got_error_from_errno("asprintf");
1624 const char *search_str = NULL;
1626 if (view->searching) {
1627 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1628 search_str = "no more matches";
1629 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1630 search_str = "no matches found";
1631 else if (!view->search_next_done)
1632 search_str = "searching...";
1635 if (asprintf(&ncommits_str, " [%d/%d] %s",
1636 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1637 search_str ? search_str :
1638 (refs_str ? refs_str : "")) == -1) {
1639 err = got_error_from_errno("asprintf");
1644 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1645 if (asprintf(&header, "commit %s %s%s",
1646 id_str ? id_str : "........................................",
1647 s->in_repo_path, ncommits_str) == -1) {
1648 err = got_error_from_errno("asprintf");
1652 } else if (asprintf(&header, "commit %s%s",
1653 id_str ? id_str : "........................................",
1654 ncommits_str) == -1) {
1655 err = got_error_from_errno("asprintf");
1659 err = format_line(&wline, &width, header, view->ncols, 0);
1663 werase(view->window);
1665 if (view_needs_focus_indication(view))
1666 wstandout(view->window);
1667 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1669 wattr_on(view->window,
1670 COLOR_PAIR(tc->colorpair), NULL);
1671 waddwstr(view->window, wline);
1673 wattr_off(view->window,
1674 COLOR_PAIR(tc->colorpair), NULL);
1675 while (width < view->ncols) {
1676 waddch(view->window, ' ');
1679 if (view_needs_focus_indication(view))
1680 wstandend(view->window);
1685 /* Grow author column size if necessary. */
1686 entry = s->first_displayed_entry;
1692 if (ncommits >= limit - 1)
1694 author = strdup(got_object_commit_get_author(entry->commit));
1695 if (author == NULL) {
1696 err = got_error_from_errno("strdup");
1699 err = format_author(&wauthor, &width, author, COLS,
1701 if (author_cols < width)
1702 author_cols = width;
1706 entry = TAILQ_NEXT(entry, entry);
1709 entry = s->first_displayed_entry;
1710 s->last_displayed_entry = s->first_displayed_entry;
1713 if (ncommits >= limit - 1)
1715 if (ncommits == s->selected)
1716 wstandout(view->window);
1717 err = draw_commit(view, entry->commit, entry->id,
1718 date_display_cols, author_cols);
1719 if (ncommits == s->selected)
1720 wstandend(view->window);
1724 s->last_displayed_entry = entry;
1725 entry = TAILQ_NEXT(entry, entry);
1738 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1740 struct commit_queue_entry *entry;
1743 entry = TAILQ_FIRST(&s->commits.head);
1744 if (s->first_displayed_entry == entry)
1747 entry = s->first_displayed_entry;
1748 while (entry && nscrolled < maxscroll) {
1749 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1751 s->first_displayed_entry = entry;
1757 static const struct got_error *
1758 trigger_log_thread(struct tog_view *view, int wait)
1760 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1763 halfdelay(1); /* fast refresh while loading commits */
1765 while (ta->commits_needed > 0) {
1766 if (ta->log_complete)
1769 /* Wake the log thread. */
1770 errcode = pthread_cond_signal(&ta->need_commits);
1772 return got_error_set_errno(errcode,
1773 "pthread_cond_signal");
1776 * The mutex will be released while the view loop waits
1777 * in wgetch(), at which time the log thread will run.
1782 /* Display progress update in log view. */
1783 show_log_view(view);
1787 /* Wait right here while next commit is being loaded. */
1788 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1790 return got_error_set_errno(errcode,
1791 "pthread_cond_wait");
1793 /* Display progress update in log view. */
1794 show_log_view(view);
1802 static const struct got_error *
1803 log_scroll_down(struct tog_view *view, int maxscroll)
1805 struct tog_log_view_state *s = &view->state.log;
1806 const struct got_error *err = NULL;
1807 struct commit_queue_entry *pentry;
1808 int nscrolled = 0, ncommits_needed;
1810 if (s->last_displayed_entry == NULL)
1813 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1814 if (s->commits.ncommits < ncommits_needed &&
1815 !s->thread_args.log_complete) {
1817 * Ask the log thread for required amount of commits.
1819 s->thread_args.commits_needed += maxscroll;
1820 err = trigger_log_thread(view, 1);
1826 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1830 s->last_displayed_entry = pentry;
1832 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1835 s->first_displayed_entry = pentry;
1836 } while (++nscrolled < maxscroll);
1841 static const struct got_error *
1842 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1843 struct got_commit_object *commit, struct got_object_id *commit_id,
1844 struct tog_view *log_view, struct got_repository *repo)
1846 const struct got_error *err;
1847 struct got_object_qid *parent_id;
1848 struct tog_view *diff_view;
1850 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1851 if (diff_view == NULL)
1852 return got_error_from_errno("view_open");
1854 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1855 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1856 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1858 *new_view = diff_view;
1862 static const struct got_error *
1863 tree_view_visit_subtree(struct tog_tree_view_state *s,
1864 struct got_tree_object *subtree)
1866 struct tog_parent_tree *parent;
1868 parent = calloc(1, sizeof(*parent));
1870 return got_error_from_errno("calloc");
1872 parent->tree = s->tree;
1873 parent->first_displayed_entry = s->first_displayed_entry;
1874 parent->selected_entry = s->selected_entry;
1875 parent->selected = s->selected;
1876 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1879 s->first_displayed_entry = NULL;
1883 static const struct got_error *
1884 tree_view_walk_path(struct tog_tree_view_state *s,
1885 struct got_object_id *commit_id, const char *path)
1887 const struct got_error *err = NULL;
1888 struct got_tree_object *tree = NULL;
1890 char *slash, *subpath = NULL;
1892 /* Walk the path and open corresponding tree objects. */
1895 struct got_tree_entry *te;
1896 struct got_object_id *tree_id;
1902 /* Ensure the correct subtree entry is selected. */
1903 slash = strchr(p, '/');
1905 te_name = strdup(p);
1907 te_name = strndup(p, slash - p);
1908 if (te_name == NULL) {
1909 err = got_error_from_errno("strndup");
1912 te = got_object_tree_find_entry(s->tree, te_name);
1914 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1919 s->first_displayed_entry = s->selected_entry = te;
1921 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1922 break; /* jump to this file's entry */
1924 slash = strchr(p, '/');
1926 subpath = strndup(path, slash - path);
1928 subpath = strdup(path);
1929 if (subpath == NULL) {
1930 err = got_error_from_errno("strdup");
1934 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1939 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1944 err = tree_view_visit_subtree(s, tree);
1946 got_object_tree_close(tree);
1960 static const struct got_error *
1961 browse_commit_tree(struct tog_view **new_view, int begin_x,
1962 struct commit_queue_entry *entry, const char *path,
1963 const char *head_ref_name, struct got_repository *repo)
1965 const struct got_error *err = NULL;
1966 struct got_tree_object *tree;
1967 struct tog_tree_view_state *s;
1968 struct tog_view *tree_view;
1970 err = got_object_open_as_tree(&tree, repo,
1971 got_object_commit_get_tree_id(entry->commit));
1975 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1976 if (tree_view == NULL)
1977 return got_error_from_errno("view_open");
1979 err = open_tree_view(tree_view, tree, entry->id, head_ref_name, repo);
1981 got_object_tree_close(tree);
1984 s = &tree_view->state.tree;
1986 *new_view = tree_view;
1988 if (got_path_is_root_dir(path))
1991 return tree_view_walk_path(s, entry->id, path);
1994 static const struct got_error *
1995 block_signals_used_by_main_thread(void)
2000 if (sigemptyset(&sigset) == -1)
2001 return got_error_from_errno("sigemptyset");
2003 /* tog handles SIGWINCH and SIGCONT */
2004 if (sigaddset(&sigset, SIGWINCH) == -1)
2005 return got_error_from_errno("sigaddset");
2006 if (sigaddset(&sigset, SIGCONT) == -1)
2007 return got_error_from_errno("sigaddset");
2009 /* ncurses handles SIGTSTP */
2010 if (sigaddset(&sigset, SIGTSTP) == -1)
2011 return got_error_from_errno("sigaddset");
2013 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2015 return got_error_set_errno(errcode, "pthread_sigmask");
2021 log_thread(void *arg)
2023 const struct got_error *err = NULL;
2025 struct tog_log_thread_args *a = arg;
2028 err = block_signals_used_by_main_thread();
2032 while (!done && !err && !tog_sigpipe_received) {
2033 err = queue_commits(a);
2035 if (err->code != GOT_ERR_ITER_COMPLETED)
2039 } else if (a->commits_needed > 0)
2040 a->commits_needed--;
2042 errcode = pthread_mutex_lock(&tog_mutex);
2044 err = got_error_set_errno(errcode,
2045 "pthread_mutex_lock");
2047 } else if (*a->quit)
2049 else if (*a->first_displayed_entry == NULL) {
2050 *a->first_displayed_entry =
2051 TAILQ_FIRST(&a->commits->head);
2052 *a->selected_entry = *a->first_displayed_entry;
2055 errcode = pthread_cond_signal(&a->commit_loaded);
2057 err = got_error_set_errno(errcode,
2058 "pthread_cond_signal");
2059 pthread_mutex_unlock(&tog_mutex);
2064 a->commits_needed = 0;
2066 if (a->commits_needed == 0) {
2067 errcode = pthread_cond_wait(&a->need_commits,
2070 err = got_error_set_errno(errcode,
2071 "pthread_cond_wait");
2077 errcode = pthread_mutex_unlock(&tog_mutex);
2078 if (errcode && err == NULL)
2079 err = got_error_set_errno(errcode,
2080 "pthread_mutex_unlock");
2082 a->log_complete = 1;
2086 static const struct got_error *
2087 stop_log_thread(struct tog_log_view_state *s)
2089 const struct got_error *err = NULL;
2094 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2096 return got_error_set_errno(errcode,
2097 "pthread_cond_signal");
2098 errcode = pthread_mutex_unlock(&tog_mutex);
2100 return got_error_set_errno(errcode,
2101 "pthread_mutex_unlock");
2102 errcode = pthread_join(s->thread, (void **)&err);
2104 return got_error_set_errno(errcode, "pthread_join");
2105 errcode = pthread_mutex_lock(&tog_mutex);
2107 return got_error_set_errno(errcode,
2108 "pthread_mutex_lock");
2112 if (s->thread_args.repo) {
2113 got_repo_close(s->thread_args.repo);
2114 s->thread_args.repo = NULL;
2117 if (s->thread_args.graph) {
2118 got_commit_graph_close(s->thread_args.graph);
2119 s->thread_args.graph = NULL;
2125 static const struct got_error *
2126 close_log_view(struct tog_view *view)
2128 const struct got_error *err = NULL;
2129 struct tog_log_view_state *s = &view->state.log;
2132 err = stop_log_thread(s);
2134 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2135 if (errcode && err == NULL)
2136 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2138 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2139 if (errcode && err == NULL)
2140 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2142 free_commits(&s->commits);
2143 free(s->in_repo_path);
2144 s->in_repo_path = NULL;
2147 free(s->head_ref_name);
2148 s->head_ref_name = NULL;
2152 static const struct got_error *
2153 search_start_log_view(struct tog_view *view)
2155 struct tog_log_view_state *s = &view->state.log;
2157 s->matched_entry = NULL;
2158 s->search_entry = NULL;
2162 static const struct got_error *
2163 search_next_log_view(struct tog_view *view)
2165 const struct got_error *err = NULL;
2166 struct tog_log_view_state *s = &view->state.log;
2167 struct commit_queue_entry *entry;
2169 /* Display progress update in log view. */
2170 show_log_view(view);
2174 if (s->search_entry) {
2176 errcode = pthread_mutex_unlock(&tog_mutex);
2178 return got_error_set_errno(errcode,
2179 "pthread_mutex_unlock");
2180 ch = wgetch(view->window);
2181 errcode = pthread_mutex_lock(&tog_mutex);
2183 return got_error_set_errno(errcode,
2184 "pthread_mutex_lock");
2185 if (ch == KEY_BACKSPACE) {
2186 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2189 if (view->searching == TOG_SEARCH_FORWARD)
2190 entry = TAILQ_NEXT(s->search_entry, entry);
2192 entry = TAILQ_PREV(s->search_entry,
2193 commit_queue_head, entry);
2194 } else if (s->matched_entry) {
2195 if (view->searching == TOG_SEARCH_FORWARD)
2196 entry = TAILQ_NEXT(s->matched_entry, entry);
2198 entry = TAILQ_PREV(s->matched_entry,
2199 commit_queue_head, entry);
2201 if (view->searching == TOG_SEARCH_FORWARD)
2202 entry = TAILQ_FIRST(&s->commits.head);
2204 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2210 if (entry == NULL) {
2211 if (s->thread_args.log_complete ||
2212 view->searching == TOG_SEARCH_BACKWARD) {
2213 view->search_next_done =
2214 (s->matched_entry == NULL ?
2215 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2216 s->search_entry = NULL;
2220 * Poke the log thread for more commits and return,
2221 * allowing the main loop to make progress. Search
2222 * will resume at s->search_entry once we come back.
2224 s->thread_args.commits_needed++;
2225 return trigger_log_thread(view, 0);
2228 err = match_commit(&have_match, entry->id, entry->commit,
2233 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2234 s->matched_entry = entry;
2238 s->search_entry = entry;
2239 if (view->searching == TOG_SEARCH_FORWARD)
2240 entry = TAILQ_NEXT(entry, entry);
2242 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2245 if (s->matched_entry) {
2246 int cur = s->selected_entry->idx;
2247 while (cur < s->matched_entry->idx) {
2248 err = input_log_view(NULL, view, KEY_DOWN);
2253 while (cur > s->matched_entry->idx) {
2254 err = input_log_view(NULL, view, KEY_UP);
2261 s->search_entry = NULL;
2266 static const struct got_error *
2267 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2268 struct got_repository *repo, const char *head_ref_name,
2269 const char *in_repo_path, int log_branches)
2271 const struct got_error *err = NULL;
2272 struct tog_log_view_state *s = &view->state.log;
2273 struct got_repository *thread_repo = NULL;
2274 struct got_commit_graph *thread_graph = NULL;
2277 if (in_repo_path != s->in_repo_path) {
2278 free(s->in_repo_path);
2279 s->in_repo_path = strdup(in_repo_path);
2280 if (s->in_repo_path == NULL)
2281 return got_error_from_errno("strdup");
2284 /* The commit queue only contains commits being displayed. */
2285 TAILQ_INIT(&s->commits.head);
2286 s->commits.ncommits = 0;
2289 if (head_ref_name) {
2290 s->head_ref_name = strdup(head_ref_name);
2291 if (s->head_ref_name == NULL) {
2292 err = got_error_from_errno("strdup");
2296 s->start_id = got_object_id_dup(start_id);
2297 if (s->start_id == NULL) {
2298 err = got_error_from_errno("got_object_id_dup");
2301 s->log_branches = log_branches;
2303 SIMPLEQ_INIT(&s->colors);
2304 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2305 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2306 get_color_value("TOG_COLOR_COMMIT"));
2309 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2310 get_color_value("TOG_COLOR_AUTHOR"));
2312 free_colors(&s->colors);
2315 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2316 get_color_value("TOG_COLOR_DATE"));
2318 free_colors(&s->colors);
2323 view->show = show_log_view;
2324 view->input = input_log_view;
2325 view->close = close_log_view;
2326 view->search_start = search_start_log_view;
2327 view->search_next = search_next_log_view;
2329 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2332 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2336 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2337 s->repo, NULL, NULL);
2341 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2343 err = got_error_set_errno(errcode, "pthread_cond_init");
2346 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2348 err = got_error_set_errno(errcode, "pthread_cond_init");
2352 s->thread_args.commits_needed = view->nlines;
2353 s->thread_args.graph = thread_graph;
2354 s->thread_args.commits = &s->commits;
2355 s->thread_args.in_repo_path = s->in_repo_path;
2356 s->thread_args.start_id = s->start_id;
2357 s->thread_args.repo = thread_repo;
2358 s->thread_args.log_complete = 0;
2359 s->thread_args.quit = &s->quit;
2360 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2361 s->thread_args.selected_entry = &s->selected_entry;
2362 s->thread_args.searching = &view->searching;
2363 s->thread_args.search_next_done = &view->search_next_done;
2364 s->thread_args.regex = &view->regex;
2367 close_log_view(view);
2371 static const struct got_error *
2372 show_log_view(struct tog_view *view)
2374 const struct got_error *err;
2375 struct tog_log_view_state *s = &view->state.log;
2377 if (s->thread == NULL) {
2378 int errcode = pthread_create(&s->thread, NULL, log_thread,
2381 return got_error_set_errno(errcode, "pthread_create");
2382 if (s->thread_args.commits_needed > 0) {
2383 err = trigger_log_thread(view, 1);
2389 return draw_commits(view);
2392 static const struct got_error *
2393 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2395 const struct got_error *err = NULL;
2396 struct tog_log_view_state *s = &view->state.log;
2397 struct tog_view *diff_view = NULL, *tree_view = NULL;
2398 struct tog_view *ref_view = NULL;
2409 if (s->first_displayed_entry == NULL)
2411 if (s->selected > 0)
2414 log_scroll_up(s, 1);
2419 if (s->first_displayed_entry == NULL)
2421 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2424 log_scroll_up(s, view->nlines - 1);
2431 if (s->first_displayed_entry == NULL)
2433 if (s->selected < MIN(view->nlines - 2,
2434 s->commits.ncommits - 1))
2437 err = log_scroll_down(view, 1);
2445 struct commit_queue_entry *first;
2446 first = s->first_displayed_entry;
2449 err = log_scroll_down(view, view->nlines - 1);
2452 if (first == s->first_displayed_entry &&
2453 s->selected < MIN(view->nlines - 2,
2454 s->commits.ncommits - 1)) {
2455 /* can't scroll further down */
2456 s->selected = MIN(view->nlines - 2,
2457 s->commits.ncommits - 1);
2463 if (s->selected > view->nlines - 2)
2464 s->selected = view->nlines - 2;
2465 if (s->selected > s->commits.ncommits - 1)
2466 s->selected = s->commits.ncommits - 1;
2468 if (s->commits.ncommits < view->nlines - 1 &&
2469 !s->thread_args.log_complete) {
2470 s->thread_args.commits_needed += (view->nlines - 1) -
2471 s->commits.ncommits;
2472 err = trigger_log_thread(view, 1);
2478 if (s->selected_entry == NULL)
2480 if (view_is_parent_view(view))
2481 begin_x = view_split_begin_x(view->begin_x);
2482 err = open_diff_view_for_commit(&diff_view, begin_x,
2483 s->selected_entry->commit, s->selected_entry->id,
2488 diff_view->focussed = 1;
2489 if (view_is_parent_view(view)) {
2490 err = view_close_child(view);
2493 view_set_child(view, diff_view);
2494 view->focus_child = 1;
2496 *new_view = diff_view;
2499 if (s->selected_entry == NULL)
2501 if (view_is_parent_view(view))
2502 begin_x = view_split_begin_x(view->begin_x);
2503 err = browse_commit_tree(&tree_view, begin_x,
2504 s->selected_entry, s->in_repo_path, s->head_ref_name,
2509 tree_view->focussed = 1;
2510 if (view_is_parent_view(view)) {
2511 err = view_close_child(view);
2514 view_set_child(view, tree_view);
2515 view->focus_child = 1;
2517 *new_view = tree_view;
2522 if (ch == KEY_BACKSPACE &&
2523 got_path_is_root_dir(s->in_repo_path))
2525 err = stop_log_thread(s);
2528 if (ch == KEY_BACKSPACE) {
2530 err = got_path_dirname(&parent_path, s->in_repo_path);
2533 free(s->in_repo_path);
2534 s->in_repo_path = parent_path;
2535 s->thread_args.in_repo_path = s->in_repo_path;
2536 } else if (ch == CTRL('l')) {
2537 struct got_object_id *start_id;
2538 err = got_repo_match_object_id(&start_id, NULL,
2539 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2540 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2544 s->start_id = start_id;
2545 s->thread_args.start_id = s->start_id;
2547 s->log_branches = !s->log_branches;
2549 err = got_repo_open(&s->thread_args.repo,
2550 got_repo_get_path(s->repo), NULL);
2554 err = tog_load_refs(s->repo);
2557 err = got_commit_graph_open(&s->thread_args.graph,
2558 s->in_repo_path, !s->log_branches);
2561 err = got_commit_graph_iter_start(s->thread_args.graph,
2562 s->start_id, s->repo, NULL, NULL);
2565 free_commits(&s->commits);
2566 s->first_displayed_entry = NULL;
2567 s->last_displayed_entry = NULL;
2568 s->selected_entry = NULL;
2570 s->thread_args.log_complete = 0;
2572 s->thread_args.commits_needed = view->nlines;
2575 if (view_is_parent_view(view))
2576 begin_x = view_split_begin_x(view->begin_x);
2577 ref_view = view_open(view->nlines, view->ncols,
2578 view->begin_y, begin_x, TOG_VIEW_REF);
2579 if (ref_view == NULL)
2580 return got_error_from_errno("view_open");
2581 err = open_ref_view(ref_view, s->repo);
2583 view_close(ref_view);
2587 ref_view->focussed = 1;
2588 if (view_is_parent_view(view)) {
2589 err = view_close_child(view);
2592 view_set_child(view, ref_view);
2593 view->focus_child = 1;
2595 *new_view = ref_view;
2604 static const struct got_error *
2605 apply_unveil(const char *repo_path, const char *worktree_path)
2607 const struct got_error *error;
2610 if (unveil("gmon.out", "rwc") != 0)
2611 return got_error_from_errno2("unveil", "gmon.out");
2613 if (repo_path && unveil(repo_path, "r") != 0)
2614 return got_error_from_errno2("unveil", repo_path);
2616 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2617 return got_error_from_errno2("unveil", worktree_path);
2619 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2620 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2622 error = got_privsep_unveil_exec_helpers();
2626 if (unveil(NULL, NULL) != 0)
2627 return got_error_from_errno("unveil");
2637 halfdelay(1); /* Do fast refresh while initial view is loading. */
2640 intrflush(stdscr, FALSE);
2641 keypad(stdscr, TRUE);
2643 if (getenv("TOG_COLORS") != NULL) {
2645 use_default_colors();
2647 signal(SIGWINCH, tog_sigwinch);
2648 signal(SIGPIPE, tog_sigpipe);
2649 signal(SIGCONT, tog_sigcont);
2652 static const struct got_error *
2653 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2654 struct got_repository *repo, struct got_worktree *worktree)
2656 const struct got_error *err = NULL;
2659 *in_repo_path = strdup("/");
2660 if (*in_repo_path == NULL)
2661 return got_error_from_errno("strdup");
2666 const char *prefix = got_worktree_get_path_prefix(worktree);
2669 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2672 if (asprintf(in_repo_path, "%s%s%s", prefix,
2673 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2675 err = got_error_from_errno("asprintf");
2676 *in_repo_path = NULL;
2680 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2685 static const struct got_error *
2686 cmd_log(int argc, char *argv[])
2688 const struct got_error *error;
2689 struct got_repository *repo = NULL;
2690 struct got_worktree *worktree = NULL;
2691 struct got_object_id *start_id = NULL;
2692 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2693 char *start_commit = NULL, *label = NULL;
2694 struct got_reference *ref = NULL;
2695 const char *head_ref_name = NULL;
2696 int ch, log_branches = 0;
2697 struct tog_view *view;
2699 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2705 start_commit = optarg;
2708 repo_path = realpath(optarg, NULL);
2709 if (repo_path == NULL)
2710 return got_error_from_errno2("realpath",
2725 if (repo_path == NULL) {
2726 cwd = getcwd(NULL, 0);
2728 return got_error_from_errno("getcwd");
2729 error = got_worktree_open(&worktree, cwd);
2730 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2734 strdup(got_worktree_get_repo_path(worktree));
2736 repo_path = strdup(cwd);
2737 if (repo_path == NULL) {
2738 error = got_error_from_errno("strdup");
2743 error = got_repo_open(&repo, repo_path, NULL);
2747 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2754 error = apply_unveil(got_repo_get_path(repo),
2755 worktree ? got_worktree_get_root_path(worktree) : NULL);
2759 error = tog_load_refs(repo);
2763 if (start_commit == NULL) {
2764 error = got_repo_match_object_id(&start_id, &label,
2765 worktree ? got_worktree_get_head_ref_name(worktree) :
2766 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2769 head_ref_name = label;
2771 error = got_ref_open(&ref, repo, start_commit, 0);
2773 head_ref_name = got_ref_get_name(ref);
2774 else if (error->code != GOT_ERR_NOT_REF)
2776 error = got_repo_match_object_id(&start_id, NULL,
2777 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2782 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2784 error = got_error_from_errno("view_open");
2787 error = open_log_view(view, start_id, repo, head_ref_name,
2788 in_repo_path, log_branches);
2792 /* Release work tree lock. */
2793 got_worktree_close(worktree);
2796 error = view_loop(view);
2806 got_repo_close(repo);
2808 got_worktree_close(worktree);
2817 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2818 "[-w] object1 object2\n", getprogname());
2823 match_line(const char *line, regex_t *regex, size_t nmatch,
2824 regmatch_t *regmatch)
2826 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2830 match_color(struct tog_colors *colors, const char *line)
2832 struct tog_color *tc = NULL;
2834 SIMPLEQ_FOREACH(tc, colors, entry) {
2835 if (match_line(line, &tc->regex, 0, NULL))
2842 static const struct got_error *
2843 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2844 WINDOW *window, regmatch_t *regmatch)
2846 const struct got_error *err = NULL;
2853 s = strndup(line, regmatch->rm_so);
2855 return got_error_from_errno("strndup");
2857 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2862 waddwstr(window, wline);
2869 s = strndup(line + regmatch->rm_so,
2870 regmatch->rm_eo - regmatch->rm_so);
2872 err = got_error_from_errno("strndup");
2876 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2881 wattr_on(window, A_STANDOUT, NULL);
2882 waddwstr(window, wline);
2883 wattr_off(window, A_STANDOUT, NULL);
2890 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2891 err = format_line(&wline, &width,
2892 line + regmatch->rm_eo, wlimit, col_tab_align);
2895 waddwstr(window, wline);
2903 static const struct got_error *
2904 draw_file(struct tog_view *view, const char *header)
2906 struct tog_diff_view_state *s = &view->state.diff;
2907 regmatch_t *regmatch = &view->regmatch;
2908 const struct got_error *err;
2911 size_t linesize = 0;
2913 struct tog_color *tc;
2916 int max_lines = view->nlines;
2917 int nlines = s->nlines;
2920 line_offset = s->line_offsets[s->first_displayed_line - 1];
2921 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2922 return got_error_from_errno("fseek");
2924 werase(view->window);
2927 if (asprintf(&line, "[%d/%d] %s",
2928 s->first_displayed_line - 1 + s->selected_line, nlines,
2930 return got_error_from_errno("asprintf");
2931 err = format_line(&wline, &width, line, view->ncols, 0);
2936 if (view_needs_focus_indication(view))
2937 wstandout(view->window);
2938 waddwstr(view->window, wline);
2941 if (view_needs_focus_indication(view))
2942 wstandend(view->window);
2943 if (width <= view->ncols - 1)
2944 waddch(view->window, '\n');
2953 while (max_lines > 0 && nprinted < max_lines) {
2954 linelen = getline(&line, &linesize, s->f);
2955 if (linelen == -1) {
2961 return got_ferror(s->f, GOT_ERR_IO);
2964 tc = match_color(&s->colors, line);
2966 wattr_on(view->window,
2967 COLOR_PAIR(tc->colorpair), NULL);
2968 if (s->first_displayed_line + nprinted == s->matched_line &&
2969 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2970 err = add_matched_line(&width, line, view->ncols, 0,
2971 view->window, regmatch);
2977 err = format_line(&wline, &width, line, view->ncols, 0);
2982 waddwstr(view->window, wline);
2987 wattr_off(view->window,
2988 COLOR_PAIR(tc->colorpair), NULL);
2989 if (width <= view->ncols - 1)
2990 waddch(view->window, '\n');
2995 s->last_displayed_line = s->first_displayed_line +
2998 s->last_displayed_line = s->first_displayed_line;
3003 while (nprinted < view->nlines) {
3004 waddch(view->window, '\n');
3008 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3013 wstandout(view->window);
3014 waddwstr(view->window, wline);
3017 wstandend(view->window);
3024 get_datestr(time_t *time, char *datebuf)
3026 struct tm mytm, *tm;
3029 tm = gmtime_r(time, &mytm);
3032 s = asctime_r(tm, datebuf);
3035 p = strchr(s, '\n');
3041 static const struct got_error *
3042 get_changed_paths(struct got_pathlist_head *paths,
3043 struct got_commit_object *commit, struct got_repository *repo)
3045 const struct got_error *err = NULL;
3046 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3047 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3048 struct got_object_qid *qid;
3050 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3052 struct got_commit_object *pcommit;
3053 err = got_object_open_as_commit(&pcommit, repo,
3058 tree_id1 = got_object_commit_get_tree_id(pcommit);
3059 got_object_commit_close(pcommit);
3064 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3069 tree_id2 = got_object_commit_get_tree_id(commit);
3070 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3074 err = got_diff_tree(tree1, tree2, "", "", repo,
3075 got_diff_tree_collect_changed_paths, paths, 0);
3078 got_object_tree_close(tree1);
3080 got_object_tree_close(tree2);
3084 static const struct got_error *
3085 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3089 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3091 return got_error_from_errno("reallocarray");
3093 (*line_offsets)[*nlines] = off;
3098 static const struct got_error *
3099 write_commit_info(off_t **line_offsets, size_t *nlines,
3100 struct got_object_id *commit_id, struct got_reflist_head *refs,
3101 struct got_repository *repo, FILE *outfile)
3103 const struct got_error *err = NULL;
3104 char datebuf[26], *datestr;
3105 struct got_commit_object *commit;
3106 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3107 time_t committer_time;
3108 const char *author, *committer;
3109 char *refs_str = NULL;
3110 struct got_pathlist_head changed_paths;
3111 struct got_pathlist_entry *pe;
3115 TAILQ_INIT(&changed_paths);
3118 err = build_refs_str(&refs_str, refs, commit_id, repo);
3123 err = got_object_open_as_commit(&commit, repo, commit_id);
3127 err = got_object_id_str(&id_str, commit_id);
3129 err = got_error_from_errno("got_object_id_str");
3133 err = add_line_offset(line_offsets, nlines, 0);
3137 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3138 refs_str ? refs_str : "", refs_str ? ")" : "");
3140 err = got_error_from_errno("fprintf");
3144 err = add_line_offset(line_offsets, nlines, outoff);
3148 n = fprintf(outfile, "from: %s\n",
3149 got_object_commit_get_author(commit));
3151 err = got_error_from_errno("fprintf");
3155 err = add_line_offset(line_offsets, nlines, outoff);
3159 committer_time = got_object_commit_get_committer_time(commit);
3160 datestr = get_datestr(&committer_time, datebuf);
3162 n = fprintf(outfile, "date: %s UTC\n", datestr);
3164 err = got_error_from_errno("fprintf");
3168 err = add_line_offset(line_offsets, nlines, outoff);
3172 author = got_object_commit_get_author(commit);
3173 committer = got_object_commit_get_committer(commit);
3174 if (strcmp(author, committer) != 0) {
3175 n = fprintf(outfile, "via: %s\n", committer);
3177 err = got_error_from_errno("fprintf");
3181 err = add_line_offset(line_offsets, nlines, outoff);
3185 err = got_object_commit_get_logmsg(&logmsg, commit);
3189 while ((line = strsep(&s, "\n")) != NULL) {
3190 n = fprintf(outfile, "%s\n", line);
3192 err = got_error_from_errno("fprintf");
3196 err = add_line_offset(line_offsets, nlines, outoff);
3201 err = get_changed_paths(&changed_paths, commit, repo);
3204 TAILQ_FOREACH(pe, &changed_paths, entry) {
3205 struct got_diff_changed_path *cp = pe->data;
3206 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3208 err = got_error_from_errno("fprintf");
3212 err = add_line_offset(line_offsets, nlines, outoff);
3215 free((char *)pe->path);
3219 fputc('\n', outfile);
3221 err = add_line_offset(line_offsets, nlines, outoff);
3223 got_pathlist_free(&changed_paths);
3227 got_object_commit_close(commit);
3229 free(*line_offsets);
3230 *line_offsets = NULL;
3236 static const struct got_error *
3237 create_diff(struct tog_diff_view_state *s)
3239 const struct got_error *err = NULL;
3243 free(s->line_offsets);
3244 s->line_offsets = malloc(sizeof(off_t));
3245 if (s->line_offsets == NULL)
3246 return got_error_from_errno("malloc");
3251 err = got_error_from_errno("got_opentemp");
3254 if (s->f && fclose(s->f) != 0) {
3255 err = got_error_from_errno("fclose");
3261 err = got_object_get_type(&obj_type, s->repo, s->id1);
3263 err = got_object_get_type(&obj_type, s->repo, s->id2);
3268 case GOT_OBJ_TYPE_BLOB:
3269 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3270 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3271 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3273 case GOT_OBJ_TYPE_TREE:
3274 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3275 s->id1, s->id2, "", "", s->diff_context,
3276 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3278 case GOT_OBJ_TYPE_COMMIT: {
3279 const struct got_object_id_queue *parent_ids;
3280 struct got_object_qid *pid;
3281 struct got_commit_object *commit2;
3282 struct got_reflist_head *refs;
3284 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3287 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3288 /* Show commit info if we're diffing to a parent/root commit. */
3289 if (s->id1 == NULL) {
3290 err = write_commit_info(&s->line_offsets, &s->nlines,
3291 s->id2, refs, s->repo, s->f);
3295 parent_ids = got_object_commit_get_parent_ids(commit2);
3296 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3297 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3298 err = write_commit_info(
3299 &s->line_offsets, &s->nlines,
3300 s->id2, refs, s->repo, s->f);
3307 got_object_commit_close(commit2);
3309 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3310 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3311 s->force_text_diff, s->repo, s->f);
3315 err = got_error(GOT_ERR_OBJ_TYPE);
3321 if (s->f && fflush(s->f) != 0 && err == NULL)
3322 err = got_error_from_errno("fflush");
3327 diff_view_indicate_progress(struct tog_view *view)
3329 mvwaddstr(view->window, 0, 0, "diffing...");
3334 static const struct got_error *
3335 search_start_diff_view(struct tog_view *view)
3337 struct tog_diff_view_state *s = &view->state.diff;
3339 s->matched_line = 0;
3343 static const struct got_error *
3344 search_next_diff_view(struct tog_view *view)
3346 struct tog_diff_view_state *s = &view->state.diff;
3349 size_t linesize = 0;
3352 if (!view->searching) {
3353 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3357 if (s->matched_line) {
3358 if (view->searching == TOG_SEARCH_FORWARD)
3359 lineno = s->matched_line + 1;
3361 lineno = s->matched_line - 1;
3363 if (view->searching == TOG_SEARCH_FORWARD)
3372 if (lineno <= 0 || lineno > s->nlines) {
3373 if (s->matched_line == 0) {
3374 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3378 if (view->searching == TOG_SEARCH_FORWARD)
3384 offset = s->line_offsets[lineno - 1];
3385 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3387 return got_error_from_errno("fseeko");
3389 linelen = getline(&line, &linesize, s->f);
3390 if (linelen != -1 &&
3391 match_line(line, &view->regex, 1, &view->regmatch)) {
3392 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3393 s->matched_line = lineno;
3396 if (view->searching == TOG_SEARCH_FORWARD)
3403 if (s->matched_line) {
3404 s->first_displayed_line = s->matched_line;
3405 s->selected_line = 1;
3411 static const struct got_error *
3412 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3413 struct got_object_id *id2, const char *label1, const char *label2,
3414 int diff_context, int ignore_whitespace, int force_text_diff,
3415 struct tog_view *log_view, struct got_repository *repo)
3417 const struct got_error *err;
3418 struct tog_diff_view_state *s = &view->state.diff;
3420 if (id1 != NULL && id2 != NULL) {
3422 err = got_object_get_type(&type1, repo, id1);
3425 err = got_object_get_type(&type2, repo, id2);
3430 return got_error(GOT_ERR_OBJ_TYPE);
3432 s->first_displayed_line = 1;
3433 s->last_displayed_line = view->nlines;
3434 s->selected_line = 1;
3442 s->id1 = got_object_id_dup(id1);
3444 return got_error_from_errno("got_object_id_dup");
3448 s->id2 = got_object_id_dup(id2);
3449 if (s->id2 == NULL) {
3452 return got_error_from_errno("got_object_id_dup");
3455 s->first_displayed_line = 1;
3456 s->last_displayed_line = view->nlines;
3457 s->diff_context = diff_context;
3458 s->ignore_whitespace = ignore_whitespace;
3459 s->force_text_diff = force_text_diff;
3460 s->log_view = log_view;
3463 SIMPLEQ_INIT(&s->colors);
3464 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3465 err = add_color(&s->colors,
3466 "^-", TOG_COLOR_DIFF_MINUS,
3467 get_color_value("TOG_COLOR_DIFF_MINUS"));
3470 err = add_color(&s->colors, "^\\+",
3471 TOG_COLOR_DIFF_PLUS,
3472 get_color_value("TOG_COLOR_DIFF_PLUS"));
3474 free_colors(&s->colors);
3477 err = add_color(&s->colors,
3478 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3479 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3481 free_colors(&s->colors);
3485 err = add_color(&s->colors,
3486 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3487 TOG_COLOR_DIFF_META,
3488 get_color_value("TOG_COLOR_DIFF_META"));
3490 free_colors(&s->colors);
3494 err = add_color(&s->colors,
3495 "^(from|via): ", TOG_COLOR_AUTHOR,
3496 get_color_value("TOG_COLOR_AUTHOR"));
3498 free_colors(&s->colors);
3502 err = add_color(&s->colors,
3503 "^date: ", TOG_COLOR_DATE,
3504 get_color_value("TOG_COLOR_DATE"));
3506 free_colors(&s->colors);
3511 if (log_view && view_is_splitscreen(view))
3512 show_log_view(log_view); /* draw vborder */
3513 diff_view_indicate_progress(view);
3515 s->line_offsets = NULL;
3517 err = create_diff(s);
3523 free_colors(&s->colors);
3527 view->show = show_diff_view;
3528 view->input = input_diff_view;
3529 view->close = close_diff_view;
3530 view->search_start = search_start_diff_view;
3531 view->search_next = search_next_diff_view;
3536 static const struct got_error *
3537 close_diff_view(struct tog_view *view)
3539 const struct got_error *err = NULL;
3540 struct tog_diff_view_state *s = &view->state.diff;
3546 if (s->f && fclose(s->f) == EOF)
3547 err = got_error_from_errno("fclose");
3548 free_colors(&s->colors);
3549 free(s->line_offsets);
3550 s->line_offsets = NULL;
3555 static const struct got_error *
3556 show_diff_view(struct tog_view *view)
3558 const struct got_error *err;
3559 struct tog_diff_view_state *s = &view->state.diff;
3560 char *id_str1 = NULL, *id_str2, *header;
3561 const char *label1, *label2;
3564 err = got_object_id_str(&id_str1, s->id1);
3567 label1 = s->label1 ? : id_str1;
3569 label1 = "/dev/null";
3571 err = got_object_id_str(&id_str2, s->id2);
3574 label2 = s->label2 ? : id_str2;
3576 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3577 err = got_error_from_errno("asprintf");
3585 return draw_file(view, header);
3588 static const struct got_error *
3589 set_selected_commit(struct tog_diff_view_state *s,
3590 struct commit_queue_entry *entry)
3592 const struct got_error *err;
3593 const struct got_object_id_queue *parent_ids;
3594 struct got_commit_object *selected_commit;
3595 struct got_object_qid *pid;
3598 s->id2 = got_object_id_dup(entry->id);
3600 return got_error_from_errno("got_object_id_dup");
3602 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3605 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3607 pid = SIMPLEQ_FIRST(parent_ids);
3608 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3609 got_object_commit_close(selected_commit);
3613 static const struct got_error *
3614 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3616 const struct got_error *err = NULL;
3617 struct tog_diff_view_state *s = &view->state.diff;
3618 struct tog_log_view_state *ls;
3619 struct commit_queue_entry *old_selected_entry;
3621 size_t linesize = 0;
3629 s->force_text_diff = !s->force_text_diff;
3631 s->ignore_whitespace = !s->ignore_whitespace;
3632 wclear(view->window);
3633 s->first_displayed_line = 1;
3634 s->last_displayed_line = view->nlines;
3635 diff_view_indicate_progress(view);
3636 err = create_diff(s);
3640 if (s->first_displayed_line > 1)
3641 s->first_displayed_line--;
3645 if (s->first_displayed_line == 1)
3648 while (i++ < view->nlines - 1 &&
3649 s->first_displayed_line > 1)
3650 s->first_displayed_line--;
3655 s->first_displayed_line++;
3663 while (!s->eof && i++ < view->nlines - 1) {
3664 linelen = getline(&line, &linesize, s->f);
3665 s->first_displayed_line++;
3666 if (linelen == -1) {
3670 err = got_ferror(s->f, GOT_ERR_IO);
3677 if (s->diff_context > 0) {
3679 diff_view_indicate_progress(view);
3680 err = create_diff(s);
3681 if (s->first_displayed_line + view->nlines - 1 >
3683 s->first_displayed_line = 1;
3684 s->last_displayed_line = view->nlines;
3689 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3691 diff_view_indicate_progress(view);
3692 err = create_diff(s);
3697 if (s->log_view == NULL)
3699 ls = &s->log_view->state.log;
3700 old_selected_entry = ls->selected_entry;
3702 err = input_log_view(NULL, s->log_view, KEY_UP);
3706 if (old_selected_entry == ls->selected_entry)
3709 err = set_selected_commit(s, ls->selected_entry);
3713 s->first_displayed_line = 1;
3714 s->last_displayed_line = view->nlines;
3716 diff_view_indicate_progress(view);
3717 err = create_diff(s);
3721 if (s->log_view == NULL)
3723 ls = &s->log_view->state.log;
3724 old_selected_entry = ls->selected_entry;
3726 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3730 if (old_selected_entry == ls->selected_entry)
3733 err = set_selected_commit(s, ls->selected_entry);
3737 s->first_displayed_line = 1;
3738 s->last_displayed_line = view->nlines;
3740 diff_view_indicate_progress(view);
3741 err = create_diff(s);
3750 static const struct got_error *
3751 cmd_diff(int argc, char *argv[])
3753 const struct got_error *error = NULL;
3754 struct got_repository *repo = NULL;
3755 struct got_worktree *worktree = NULL;
3756 struct got_object_id *id1 = NULL, *id2 = NULL;
3757 char *repo_path = NULL, *cwd = NULL;
3758 char *id_str1 = NULL, *id_str2 = NULL;
3759 char *label1 = NULL, *label2 = NULL;
3760 int diff_context = 3, ignore_whitespace = 0;
3761 int ch, force_text_diff = 0;
3763 struct tog_view *view;
3765 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3768 force_text_diff = 1;
3771 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3774 err(1, "-C option %s", errstr);
3777 repo_path = realpath(optarg, NULL);
3778 if (repo_path == NULL)
3779 return got_error_from_errno2("realpath",
3781 got_path_strip_trailing_slashes(repo_path);
3784 ignore_whitespace = 1;
3796 usage_diff(); /* TODO show local worktree changes */
3797 } else if (argc == 2) {
3803 if (repo_path == NULL) {
3804 cwd = getcwd(NULL, 0);
3806 return got_error_from_errno("getcwd");
3807 error = got_worktree_open(&worktree, cwd);
3808 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3812 strdup(got_worktree_get_repo_path(worktree));
3814 repo_path = strdup(cwd);
3815 if (repo_path == NULL) {
3816 error = got_error_from_errno("strdup");
3821 error = got_repo_open(&repo, repo_path, NULL);
3827 error = apply_unveil(got_repo_get_path(repo), NULL);
3831 error = tog_load_refs(repo);
3835 error = got_repo_match_object_id(&id1, &label1, id_str1,
3836 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3840 error = got_repo_match_object_id(&id2, &label2, id_str2,
3841 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3845 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3847 error = got_error_from_errno("view_open");
3850 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3851 ignore_whitespace, force_text_diff, NULL, repo);
3854 error = view_loop(view);
3861 got_repo_close(repo);
3863 got_worktree_close(worktree);
3872 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3877 struct tog_blame_line {
3879 struct got_object_id *id;
3882 static const struct got_error *
3883 draw_blame(struct tog_view *view)
3885 struct tog_blame_view_state *s = &view->state.blame;
3886 struct tog_blame *blame = &s->blame;
3887 regmatch_t *regmatch = &view->regmatch;
3888 const struct got_error *err;
3889 int lineno = 0, nprinted = 0;
3891 size_t linesize = 0;
3895 struct tog_blame_line *blame_line;
3896 struct got_object_id *prev_id = NULL;
3898 struct tog_color *tc;
3900 err = got_object_id_str(&id_str, s->blamed_commit->id);
3905 werase(view->window);
3907 if (asprintf(&line, "commit %s", id_str) == -1) {
3908 err = got_error_from_errno("asprintf");
3913 err = format_line(&wline, &width, line, view->ncols, 0);
3918 if (view_needs_focus_indication(view))
3919 wstandout(view->window);
3920 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3922 wattr_on(view->window,
3923 COLOR_PAIR(tc->colorpair), NULL);
3924 waddwstr(view->window, wline);
3926 wattr_off(view->window,
3927 COLOR_PAIR(tc->colorpair), NULL);
3928 if (view_needs_focus_indication(view))
3929 wstandend(view->window);
3932 if (width < view->ncols - 1)
3933 waddch(view->window, '\n');
3935 if (asprintf(&line, "[%d/%d] %s%s",
3936 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3937 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3939 return got_error_from_errno("asprintf");
3942 err = format_line(&wline, &width, line, view->ncols, 0);
3947 waddwstr(view->window, wline);
3950 if (width < view->ncols - 1)
3951 waddch(view->window, '\n');
3954 while (nprinted < view->nlines - 2) {
3955 linelen = getline(&line, &linesize, blame->f);
3956 if (linelen == -1) {
3957 if (feof(blame->f)) {
3962 return got_ferror(blame->f, GOT_ERR_IO);
3964 if (++lineno < s->first_displayed_line)
3967 if (view->focussed && nprinted == s->selected_line - 1)
3968 wstandout(view->window);
3970 if (blame->nlines > 0) {
3971 blame_line = &blame->lines[lineno - 1];
3972 if (blame_line->annotated && prev_id &&
3973 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3975 nprinted == s->selected_line - 1)) {
3976 waddstr(view->window, " ");
3977 } else if (blame_line->annotated) {
3979 err = got_object_id_str(&id_str, blame_line->id);
3984 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3986 wattr_on(view->window,
3987 COLOR_PAIR(tc->colorpair), NULL);
3988 wprintw(view->window, "%.8s", id_str);
3990 wattr_off(view->window,
3991 COLOR_PAIR(tc->colorpair), NULL);
3993 prev_id = blame_line->id;
3995 waddstr(view->window, "........");
3999 waddstr(view->window, "........");
4003 if (view->focussed && nprinted == s->selected_line - 1)
4004 wstandend(view->window);
4005 waddstr(view->window, " ");
4007 if (view->ncols <= 9) {
4009 wline = wcsdup(L"");
4010 if (wline == NULL) {
4011 err = got_error_from_errno("wcsdup");
4015 } else if (s->first_displayed_line + nprinted ==
4017 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4018 err = add_matched_line(&width, line, view->ncols - 9, 9,
4019 view->window, regmatch);
4026 err = format_line(&wline, &width, line,
4027 view->ncols - 9, 9);
4028 waddwstr(view->window, wline);
4034 if (width <= view->ncols - 1)
4035 waddch(view->window, '\n');
4036 if (++nprinted == 1)
4037 s->first_displayed_line = lineno;
4040 s->last_displayed_line = lineno;
4047 static const struct got_error *
4048 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4050 const struct got_error *err = NULL;
4051 struct tog_blame_cb_args *a = arg;
4052 struct tog_blame_line *line;
4055 if (nlines != a->nlines ||
4056 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4057 return got_error(GOT_ERR_RANGE);
4059 errcode = pthread_mutex_lock(&tog_mutex);
4061 return got_error_set_errno(errcode, "pthread_mutex_lock");
4063 if (*a->quit) { /* user has quit the blame view */
4064 err = got_error(GOT_ERR_ITER_COMPLETED);
4069 goto done; /* no change in this commit */
4071 line = &a->lines[lineno - 1];
4072 if (line->annotated)
4075 line->id = got_object_id_dup(id);
4076 if (line->id == NULL) {
4077 err = got_error_from_errno("got_object_id_dup");
4080 line->annotated = 1;
4082 errcode = pthread_mutex_unlock(&tog_mutex);
4084 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4089 blame_thread(void *arg)
4091 const struct got_error *err;
4092 struct tog_blame_thread_args *ta = arg;
4093 struct tog_blame_cb_args *a = ta->cb_args;
4096 err = block_signals_used_by_main_thread();
4100 err = got_blame(ta->path, a->commit_id, ta->repo,
4101 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4102 if (err && err->code == GOT_ERR_CANCELLED)
4105 errcode = pthread_mutex_lock(&tog_mutex);
4107 return (void *)got_error_set_errno(errcode,
4108 "pthread_mutex_lock");
4110 got_repo_close(ta->repo);
4114 errcode = pthread_mutex_unlock(&tog_mutex);
4115 if (errcode && err == NULL)
4116 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4121 static struct got_object_id *
4122 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4123 int first_displayed_line, int selected_line)
4125 struct tog_blame_line *line;
4130 line = &lines[first_displayed_line - 1 + selected_line - 1];
4131 if (!line->annotated)
4137 static const struct got_error *
4138 stop_blame(struct tog_blame *blame)
4140 const struct got_error *err = NULL;
4143 if (blame->thread) {
4145 errcode = pthread_mutex_unlock(&tog_mutex);
4147 return got_error_set_errno(errcode,
4148 "pthread_mutex_unlock");
4149 errcode = pthread_join(blame->thread, (void **)&err);
4151 return got_error_set_errno(errcode, "pthread_join");
4152 errcode = pthread_mutex_lock(&tog_mutex);
4154 return got_error_set_errno(errcode,
4155 "pthread_mutex_lock");
4156 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4158 blame->thread = NULL;
4160 if (blame->thread_args.repo) {
4161 got_repo_close(blame->thread_args.repo);
4162 blame->thread_args.repo = NULL;
4165 if (fclose(blame->f) != 0 && err == NULL)
4166 err = got_error_from_errno("fclose");
4170 for (i = 0; i < blame->nlines; i++)
4171 free(blame->lines[i].id);
4173 blame->lines = NULL;
4175 free(blame->cb_args.commit_id);
4176 blame->cb_args.commit_id = NULL;
4181 static const struct got_error *
4182 cancel_blame_view(void *arg)
4184 const struct got_error *err = NULL;
4188 errcode = pthread_mutex_lock(&tog_mutex);
4190 return got_error_set_errno(errcode,
4191 "pthread_mutex_unlock");
4194 err = got_error(GOT_ERR_CANCELLED);
4196 errcode = pthread_mutex_unlock(&tog_mutex);
4198 return got_error_set_errno(errcode,
4199 "pthread_mutex_lock");
4204 static const struct got_error *
4205 run_blame(struct tog_view *view)
4207 struct tog_blame_view_state *s = &view->state.blame;
4208 struct tog_blame *blame = &s->blame;
4209 const struct got_error *err = NULL;
4210 struct got_blob_object *blob = NULL;
4211 struct got_repository *thread_repo = NULL;
4212 struct got_object_id *obj_id = NULL;
4215 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4220 err = got_object_get_type(&obj_type, s->repo, obj_id);
4224 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4225 err = got_error(GOT_ERR_OBJ_TYPE);
4229 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4232 blame->f = got_opentemp();
4233 if (blame->f == NULL) {
4234 err = got_error_from_errno("got_opentemp");
4237 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4238 &blame->line_offsets, blame->f, blob);
4239 if (err || blame->nlines == 0)
4242 /* Don't include \n at EOF in the blame line count. */
4243 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4246 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4247 if (blame->lines == NULL) {
4248 err = got_error_from_errno("calloc");
4252 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4256 blame->cb_args.view = view;
4257 blame->cb_args.lines = blame->lines;
4258 blame->cb_args.nlines = blame->nlines;
4259 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4260 if (blame->cb_args.commit_id == NULL) {
4261 err = got_error_from_errno("got_object_id_dup");
4264 blame->cb_args.quit = &s->done;
4266 blame->thread_args.path = s->path;
4267 blame->thread_args.repo = thread_repo;
4268 blame->thread_args.cb_args = &blame->cb_args;
4269 blame->thread_args.complete = &s->blame_complete;
4270 blame->thread_args.cancel_cb = cancel_blame_view;
4271 blame->thread_args.cancel_arg = &s->done;
4272 s->blame_complete = 0;
4274 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4275 s->first_displayed_line = 1;
4276 s->last_displayed_line = view->nlines;
4277 s->selected_line = 1;
4282 got_object_blob_close(blob);
4289 static const struct got_error *
4290 open_blame_view(struct tog_view *view, char *path,
4291 struct got_object_id *commit_id, struct got_repository *repo)
4293 const struct got_error *err = NULL;
4294 struct tog_blame_view_state *s = &view->state.blame;
4296 SIMPLEQ_INIT(&s->blamed_commits);
4298 s->path = strdup(path);
4299 if (s->path == NULL)
4300 return got_error_from_errno("strdup");
4302 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4308 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4309 s->first_displayed_line = 1;
4310 s->last_displayed_line = view->nlines;
4311 s->selected_line = 1;
4312 s->blame_complete = 0;
4314 s->commit_id = commit_id;
4315 memset(&s->blame, 0, sizeof(s->blame));
4317 SIMPLEQ_INIT(&s->colors);
4318 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4319 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4320 get_color_value("TOG_COLOR_COMMIT"));
4325 view->show = show_blame_view;
4326 view->input = input_blame_view;
4327 view->close = close_blame_view;
4328 view->search_start = search_start_blame_view;
4329 view->search_next = search_next_blame_view;
4331 return run_blame(view);
4334 static const struct got_error *
4335 close_blame_view(struct tog_view *view)
4337 const struct got_error *err = NULL;
4338 struct tog_blame_view_state *s = &view->state.blame;
4340 if (s->blame.thread)
4341 err = stop_blame(&s->blame);
4343 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4344 struct got_object_qid *blamed_commit;
4345 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4346 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4347 got_object_qid_free(blamed_commit);
4351 free_colors(&s->colors);
4356 static const struct got_error *
4357 search_start_blame_view(struct tog_view *view)
4359 struct tog_blame_view_state *s = &view->state.blame;
4361 s->matched_line = 0;
4365 static const struct got_error *
4366 search_next_blame_view(struct tog_view *view)
4368 struct tog_blame_view_state *s = &view->state.blame;
4371 size_t linesize = 0;
4374 if (!view->searching) {
4375 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4379 if (s->matched_line) {
4380 if (view->searching == TOG_SEARCH_FORWARD)
4381 lineno = s->matched_line + 1;
4383 lineno = s->matched_line - 1;
4385 if (view->searching == TOG_SEARCH_FORWARD)
4388 lineno = s->blame.nlines;
4394 if (lineno <= 0 || lineno > s->blame.nlines) {
4395 if (s->matched_line == 0) {
4396 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4400 if (view->searching == TOG_SEARCH_FORWARD)
4403 lineno = s->blame.nlines;
4406 offset = s->blame.line_offsets[lineno - 1];
4407 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4409 return got_error_from_errno("fseeko");
4411 linelen = getline(&line, &linesize, s->blame.f);
4412 if (linelen != -1 &&
4413 match_line(line, &view->regex, 1, &view->regmatch)) {
4414 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4415 s->matched_line = lineno;
4418 if (view->searching == TOG_SEARCH_FORWARD)
4425 if (s->matched_line) {
4426 s->first_displayed_line = s->matched_line;
4427 s->selected_line = 1;
4433 static const struct got_error *
4434 show_blame_view(struct tog_view *view)
4436 const struct got_error *err = NULL;
4437 struct tog_blame_view_state *s = &view->state.blame;
4440 if (s->blame.thread == NULL) {
4441 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4442 &s->blame.thread_args);
4444 return got_error_set_errno(errcode, "pthread_create");
4446 halfdelay(1); /* fast refresh while annotating */
4449 if (s->blame_complete)
4450 halfdelay(10); /* disable fast refresh */
4452 err = draw_blame(view);
4458 static const struct got_error *
4459 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4461 const struct got_error *err = NULL, *thread_err = NULL;
4462 struct tog_view *diff_view;
4463 struct tog_blame_view_state *s = &view->state.blame;
4472 if (s->selected_line > 1)
4474 else if (s->selected_line == 1 &&
4475 s->first_displayed_line > 1)
4476 s->first_displayed_line--;
4480 if (s->first_displayed_line == 1) {
4481 s->selected_line = 1;
4484 if (s->first_displayed_line > view->nlines - 2)
4485 s->first_displayed_line -=
4488 s->first_displayed_line = 1;
4492 if (s->selected_line < view->nlines - 2 &&
4493 s->first_displayed_line +
4494 s->selected_line <= s->blame.nlines)
4496 else if (s->last_displayed_line <
4498 s->first_displayed_line++;
4502 struct got_object_id *id = NULL;
4503 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4504 s->first_displayed_line, s->selected_line);
4508 struct got_commit_object *commit;
4509 struct got_object_qid *pid;
4510 struct got_object_id *blob_id = NULL;
4512 err = got_object_open_as_commit(&commit,
4516 pid = SIMPLEQ_FIRST(
4517 got_object_commit_get_parent_ids(commit));
4519 got_object_commit_close(commit);
4522 /* Check if path history ends here. */
4523 err = got_object_id_by_path(&blob_id, s->repo,
4526 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4528 got_object_commit_close(commit);
4531 err = got_object_get_type(&obj_type, s->repo,
4534 /* Can't blame non-blob type objects. */
4535 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4536 got_object_commit_close(commit);
4539 err = got_object_qid_alloc(&s->blamed_commit,
4541 got_object_commit_close(commit);
4543 if (got_object_id_cmp(id,
4544 s->blamed_commit->id) == 0)
4546 err = got_object_qid_alloc(&s->blamed_commit,
4552 thread_err = stop_blame(&s->blame);
4556 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4557 s->blamed_commit, entry);
4558 err = run_blame(view);
4564 struct got_object_qid *first;
4565 first = SIMPLEQ_FIRST(&s->blamed_commits);
4566 if (!got_object_id_cmp(first->id, s->commit_id))
4569 thread_err = stop_blame(&s->blame);
4573 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4574 got_object_qid_free(s->blamed_commit);
4576 SIMPLEQ_FIRST(&s->blamed_commits);
4577 err = run_blame(view);
4584 struct got_object_id *id = NULL;
4585 struct got_object_qid *pid;
4586 struct got_commit_object *commit = NULL;
4587 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4588 s->first_displayed_line, s->selected_line);
4591 err = got_object_open_as_commit(&commit, s->repo, id);
4594 pid = SIMPLEQ_FIRST(
4595 got_object_commit_get_parent_ids(commit));
4596 if (view_is_parent_view(view))
4597 begin_x = view_split_begin_x(view->begin_x);
4598 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4599 if (diff_view == NULL) {
4600 got_object_commit_close(commit);
4601 err = got_error_from_errno("view_open");
4604 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4605 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4606 got_object_commit_close(commit);
4608 view_close(diff_view);
4612 diff_view->focussed = 1;
4613 if (view_is_parent_view(view)) {
4614 err = view_close_child(view);
4617 view_set_child(view, diff_view);
4618 view->focus_child = 1;
4620 *new_view = diff_view;
4628 if (s->last_displayed_line >= s->blame.nlines &&
4629 s->selected_line >= MIN(s->blame.nlines,
4630 view->nlines - 2)) {
4633 if (s->last_displayed_line >= s->blame.nlines &&
4634 s->selected_line < view->nlines - 2) {
4635 s->selected_line = MIN(s->blame.nlines,
4639 if (s->last_displayed_line + view->nlines - 2
4641 s->first_displayed_line +=
4644 s->first_displayed_line =
4649 if (s->selected_line > view->nlines - 2) {
4650 s->selected_line = MIN(s->blame.nlines,
4657 return thread_err ? thread_err : err;
4660 static const struct got_error *
4661 cmd_blame(int argc, char *argv[])
4663 const struct got_error *error;
4664 struct got_repository *repo = NULL;
4665 struct got_worktree *worktree = NULL;
4666 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4667 char *link_target = NULL;
4668 struct got_object_id *commit_id = NULL;
4669 char *commit_id_str = NULL;
4671 struct tog_view *view;
4673 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4676 commit_id_str = optarg;
4679 repo_path = realpath(optarg, NULL);
4680 if (repo_path == NULL)
4681 return got_error_from_errno2("realpath",
4696 if (repo_path == NULL) {
4697 cwd = getcwd(NULL, 0);
4699 return got_error_from_errno("getcwd");
4700 error = got_worktree_open(&worktree, cwd);
4701 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4705 strdup(got_worktree_get_repo_path(worktree));
4707 repo_path = strdup(cwd);
4708 if (repo_path == NULL) {
4709 error = got_error_from_errno("strdup");
4714 error = got_repo_open(&repo, repo_path, NULL);
4718 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4725 error = apply_unveil(got_repo_get_path(repo), NULL);
4729 error = tog_load_refs(repo);
4733 if (commit_id_str == NULL) {
4734 struct got_reference *head_ref;
4735 error = got_ref_open(&head_ref, repo, worktree ?
4736 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4739 error = got_ref_resolve(&commit_id, repo, head_ref);
4740 got_ref_close(head_ref);
4742 error = got_repo_match_object_id(&commit_id, NULL,
4743 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4748 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4750 error = got_error_from_errno("view_open");
4754 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4759 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4764 /* Release work tree lock. */
4765 got_worktree_close(worktree);
4768 error = view_loop(view);
4776 got_worktree_close(worktree);
4778 got_repo_close(repo);
4783 static const struct got_error *
4784 draw_tree_entries(struct tog_view *view, const char *parent_path)
4786 struct tog_tree_view_state *s = &view->state.tree;
4787 const struct got_error *err = NULL;
4788 struct got_tree_entry *te;
4790 struct tog_color *tc;
4791 int width, n, i, nentries;
4792 int limit = view->nlines;
4796 werase(view->window);
4801 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4804 if (view_needs_focus_indication(view))
4805 wstandout(view->window);
4806 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4808 wattr_on(view->window,
4809 COLOR_PAIR(tc->colorpair), NULL);
4810 waddwstr(view->window, wline);
4812 wattr_off(view->window,
4813 COLOR_PAIR(tc->colorpair), NULL);
4814 if (view_needs_focus_indication(view))
4815 wstandend(view->window);
4818 if (width < view->ncols - 1)
4819 waddch(view->window, '\n');
4822 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4825 waddwstr(view->window, wline);
4828 if (width < view->ncols - 1)
4829 waddch(view->window, '\n');
4832 waddch(view->window, '\n');
4836 if (s->first_displayed_entry == NULL) {
4837 te = got_object_tree_get_first_entry(s->tree);
4838 if (s->selected == 0) {
4840 wstandout(view->window);
4841 s->selected_entry = NULL;
4843 waddstr(view->window, " ..\n"); /* parent directory */
4844 if (s->selected == 0 && view->focussed)
4845 wstandend(view->window);
4852 te = s->first_displayed_entry;
4855 nentries = got_object_tree_get_nentries(s->tree);
4856 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4857 char *line = NULL, *id_str = NULL, *link_target = NULL;
4858 const char *modestr = "";
4861 te = got_object_tree_get_entry(s->tree, i);
4862 mode = got_tree_entry_get_mode(te);
4865 err = got_object_id_str(&id_str,
4866 got_tree_entry_get_id(te));
4868 return got_error_from_errno(
4869 "got_object_id_str");
4871 if (got_object_tree_entry_is_submodule(te))
4873 else if (S_ISLNK(mode)) {
4876 err = got_tree_entry_get_symlink_target(&link_target,
4882 for (i = 0; i < strlen(link_target); i++) {
4883 if (!isprint((unsigned char)link_target[i]))
4884 link_target[i] = '?';
4888 else if (S_ISDIR(mode))
4890 else if (mode & S_IXUSR)
4892 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4893 got_tree_entry_get_name(te), modestr,
4894 link_target ? " -> ": "",
4895 link_target ? link_target : "") == -1) {
4898 return got_error_from_errno("asprintf");
4902 err = format_line(&wline, &width, line, view->ncols, 0);
4907 if (n == s->selected) {
4909 wstandout(view->window);
4910 s->selected_entry = te;
4912 tc = match_color(&s->colors, line);
4914 wattr_on(view->window,
4915 COLOR_PAIR(tc->colorpair), NULL);
4916 waddwstr(view->window, wline);
4918 wattr_off(view->window,
4919 COLOR_PAIR(tc->colorpair), NULL);
4920 if (width < view->ncols - 1)
4921 waddch(view->window, '\n');
4922 if (n == s->selected && view->focussed)
4923 wstandend(view->window);
4929 s->last_displayed_entry = te;
4938 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4940 struct got_tree_entry *te;
4941 int isroot = s->tree == s->root;
4944 if (s->first_displayed_entry == NULL)
4947 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4948 while (i++ < maxscroll) {
4951 s->first_displayed_entry = NULL;
4954 s->first_displayed_entry = te;
4955 te = got_tree_entry_get_prev(s->tree, te);
4960 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4962 struct got_tree_entry *next, *last;
4965 if (s->first_displayed_entry)
4966 next = got_tree_entry_get_next(s->tree,
4967 s->first_displayed_entry);
4969 next = got_object_tree_get_first_entry(s->tree);
4971 last = s->last_displayed_entry;
4972 while (next && last && n++ < maxscroll) {
4973 last = got_tree_entry_get_next(s->tree, last);
4975 s->first_displayed_entry = next;
4976 next = got_tree_entry_get_next(s->tree, next);
4981 static const struct got_error *
4982 tree_entry_path(char **path, struct tog_parent_trees *parents,
4983 struct got_tree_entry *te)
4985 const struct got_error *err = NULL;
4986 struct tog_parent_tree *pt;
4987 size_t len = 2; /* for leading slash and NUL */
4989 TAILQ_FOREACH(pt, parents, entry)
4990 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4993 len += strlen(got_tree_entry_get_name(te));
4995 *path = calloc(1, len);
4997 return got_error_from_errno("calloc");
5000 pt = TAILQ_LAST(parents, tog_parent_trees);
5002 const char *name = got_tree_entry_get_name(pt->selected_entry);
5003 if (strlcat(*path, name, len) >= len) {
5004 err = got_error(GOT_ERR_NO_SPACE);
5007 if (strlcat(*path, "/", len) >= len) {
5008 err = got_error(GOT_ERR_NO_SPACE);
5011 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5014 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5015 err = got_error(GOT_ERR_NO_SPACE);
5027 static const struct got_error *
5028 blame_tree_entry(struct tog_view **new_view, int begin_x,
5029 struct got_tree_entry *te, struct tog_parent_trees *parents,
5030 struct got_object_id *commit_id, struct got_repository *repo)
5032 const struct got_error *err = NULL;
5034 struct tog_view *blame_view;
5038 err = tree_entry_path(&path, parents, te);
5042 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5043 if (blame_view == NULL) {
5044 err = got_error_from_errno("view_open");
5048 err = open_blame_view(blame_view, path, commit_id, repo);
5050 if (err->code == GOT_ERR_CANCELLED)
5052 view_close(blame_view);
5054 *new_view = blame_view;
5060 static const struct got_error *
5061 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5062 struct tog_tree_view_state *s)
5064 struct tog_view *log_view;
5065 const struct got_error *err = NULL;
5070 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5071 if (log_view == NULL)
5072 return got_error_from_errno("view_open");
5074 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5078 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5081 view_close(log_view);
5083 *new_view = log_view;
5088 static const struct got_error *
5089 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5090 struct got_object_id *commit_id, const char *head_ref_name,
5091 struct got_repository *repo)
5093 const struct got_error *err = NULL;
5094 char *commit_id_str = NULL;
5095 struct tog_tree_view_state *s = &view->state.tree;
5097 TAILQ_INIT(&s->parents);
5099 err = got_object_id_str(&commit_id_str, commit_id);
5103 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5104 err = got_error_from_errno("asprintf");
5108 s->root = s->tree = root;
5109 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5110 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5111 s->commit_id = got_object_id_dup(commit_id);
5112 if (s->commit_id == NULL) {
5113 err = got_error_from_errno("got_object_id_dup");
5116 if (head_ref_name) {
5117 s->head_ref_name = strdup(head_ref_name);
5118 if (s->head_ref_name == NULL) {
5119 err = got_error_from_errno("strdup");
5125 SIMPLEQ_INIT(&s->colors);
5127 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5128 err = add_color(&s->colors, "\\$$",
5129 TOG_COLOR_TREE_SUBMODULE,
5130 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5133 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5134 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5136 free_colors(&s->colors);
5139 err = add_color(&s->colors, "/$",
5140 TOG_COLOR_TREE_DIRECTORY,
5141 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5143 free_colors(&s->colors);
5147 err = add_color(&s->colors, "\\*$",
5148 TOG_COLOR_TREE_EXECUTABLE,
5149 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5151 free_colors(&s->colors);
5155 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5156 get_color_value("TOG_COLOR_COMMIT"));
5158 free_colors(&s->colors);
5163 view->show = show_tree_view;
5164 view->input = input_tree_view;
5165 view->close = close_tree_view;
5166 view->search_start = search_start_tree_view;
5167 view->search_next = search_next_tree_view;
5169 free(commit_id_str);
5171 free(s->tree_label);
5172 s->tree_label = NULL;
5177 static const struct got_error *
5178 close_tree_view(struct tog_view *view)
5180 struct tog_tree_view_state *s = &view->state.tree;
5182 free_colors(&s->colors);
5183 free(s->tree_label);
5184 s->tree_label = NULL;
5186 s->commit_id = NULL;
5187 free(s->head_ref_name);
5188 s->head_ref_name = NULL;
5189 while (!TAILQ_EMPTY(&s->parents)) {
5190 struct tog_parent_tree *parent;
5191 parent = TAILQ_FIRST(&s->parents);
5192 TAILQ_REMOVE(&s->parents, parent, entry);
5196 if (s->tree != s->root)
5197 got_object_tree_close(s->tree);
5198 got_object_tree_close(s->root);
5202 static const struct got_error *
5203 search_start_tree_view(struct tog_view *view)
5205 struct tog_tree_view_state *s = &view->state.tree;
5207 s->matched_entry = NULL;
5212 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5214 regmatch_t regmatch;
5216 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5220 static const struct got_error *
5221 search_next_tree_view(struct tog_view *view)
5223 struct tog_tree_view_state *s = &view->state.tree;
5224 struct got_tree_entry *te = NULL;
5226 if (!view->searching) {
5227 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5231 if (s->matched_entry) {
5232 if (view->searching == TOG_SEARCH_FORWARD) {
5233 if (s->selected_entry)
5234 te = got_tree_entry_get_next(s->tree,
5237 te = got_object_tree_get_first_entry(s->tree);
5239 if (s->selected_entry == NULL)
5240 te = got_object_tree_get_last_entry(s->tree);
5242 te = got_tree_entry_get_prev(s->tree,
5246 if (view->searching == TOG_SEARCH_FORWARD)
5247 te = got_object_tree_get_first_entry(s->tree);
5249 te = got_object_tree_get_last_entry(s->tree);
5254 if (s->matched_entry == NULL) {
5255 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5258 if (view->searching == TOG_SEARCH_FORWARD)
5259 te = got_object_tree_get_first_entry(s->tree);
5261 te = got_object_tree_get_last_entry(s->tree);
5264 if (match_tree_entry(te, &view->regex)) {
5265 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5266 s->matched_entry = te;
5270 if (view->searching == TOG_SEARCH_FORWARD)
5271 te = got_tree_entry_get_next(s->tree, te);
5273 te = got_tree_entry_get_prev(s->tree, te);
5276 if (s->matched_entry) {
5277 s->first_displayed_entry = s->matched_entry;
5284 static const struct got_error *
5285 show_tree_view(struct tog_view *view)
5287 const struct got_error *err = NULL;
5288 struct tog_tree_view_state *s = &view->state.tree;
5291 err = tree_entry_path(&parent_path, &s->parents, NULL);
5295 err = draw_tree_entries(view, parent_path);
5302 static const struct got_error *
5303 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5305 const struct got_error *err = NULL;
5306 struct tog_tree_view_state *s = &view->state.tree;
5307 struct tog_view *log_view, *ref_view;
5312 s->show_ids = !s->show_ids;
5315 if (!s->selected_entry)
5317 if (view_is_parent_view(view))
5318 begin_x = view_split_begin_x(view->begin_x);
5319 err = log_selected_tree_entry(&log_view, begin_x, s);
5321 log_view->focussed = 1;
5322 if (view_is_parent_view(view)) {
5323 err = view_close_child(view);
5326 view_set_child(view, log_view);
5327 view->focus_child = 1;
5329 *new_view = log_view;
5332 if (view_is_parent_view(view))
5333 begin_x = view_split_begin_x(view->begin_x);
5334 ref_view = view_open(view->nlines, view->ncols,
5335 view->begin_y, begin_x, TOG_VIEW_REF);
5336 if (ref_view == NULL)
5337 return got_error_from_errno("view_open");
5338 err = open_ref_view(ref_view, s->repo);
5340 view_close(ref_view);
5344 ref_view->focussed = 1;
5345 if (view_is_parent_view(view)) {
5346 err = view_close_child(view);
5349 view_set_child(view, ref_view);
5350 view->focus_child = 1;
5352 *new_view = ref_view;
5356 if (s->selected > 0) {
5360 tree_scroll_up(s, 1);
5364 if (s->tree == s->root) {
5365 if (got_object_tree_get_first_entry(s->tree) ==
5366 s->first_displayed_entry)
5369 if (s->first_displayed_entry == NULL)
5372 tree_scroll_up(s, MAX(0, view->nlines - 3));
5376 if (s->selected < s->ndisplayed - 1) {
5380 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5382 /* can't scroll any further */
5384 tree_scroll_down(s, 1);
5388 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5390 /* can't scroll any further; move cursor down */
5391 if (s->selected < s->ndisplayed - 1)
5392 s->selected = s->ndisplayed - 1;
5395 tree_scroll_down(s, view->nlines - 3);
5400 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5401 struct tog_parent_tree *parent;
5402 /* user selected '..' */
5403 if (s->tree == s->root)
5405 parent = TAILQ_FIRST(&s->parents);
5406 TAILQ_REMOVE(&s->parents, parent,
5408 got_object_tree_close(s->tree);
5409 s->tree = parent->tree;
5410 s->first_displayed_entry =
5411 parent->first_displayed_entry;
5413 parent->selected_entry;
5414 s->selected = parent->selected;
5416 } else if (S_ISDIR(got_tree_entry_get_mode(
5417 s->selected_entry))) {
5418 struct got_tree_object *subtree;
5419 err = got_object_open_as_tree(&subtree, s->repo,
5420 got_tree_entry_get_id(s->selected_entry));
5423 err = tree_view_visit_subtree(s, subtree);
5425 got_object_tree_close(subtree);
5428 } else if (S_ISREG(got_tree_entry_get_mode(
5429 s->selected_entry))) {
5430 struct tog_view *blame_view;
5431 int begin_x = view_is_parent_view(view) ?
5432 view_split_begin_x(view->begin_x) : 0;
5434 err = blame_tree_entry(&blame_view, begin_x,
5435 s->selected_entry, &s->parents,
5436 s->commit_id, s->repo);
5440 blame_view->focussed = 1;
5441 if (view_is_parent_view(view)) {
5442 err = view_close_child(view);
5445 view_set_child(view, blame_view);
5446 view->focus_child = 1;
5448 *new_view = blame_view;
5452 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5453 s->selected = view->nlines - 4;
5466 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5471 static const struct got_error *
5472 cmd_tree(int argc, char *argv[])
5474 const struct got_error *error;
5475 struct got_repository *repo = NULL;
5476 struct got_worktree *worktree = NULL;
5477 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5478 struct got_object_id *commit_id = NULL;
5479 const char *commit_id_arg = NULL;
5481 struct got_commit_object *commit = NULL;
5482 struct got_tree_object *tree = NULL;
5483 struct got_reference *ref = NULL;
5484 const char *head_ref_name = NULL;
5486 struct tog_view *view;
5488 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5491 commit_id_arg = optarg;
5494 repo_path = realpath(optarg, NULL);
5495 if (repo_path == NULL)
5496 return got_error_from_errno2("realpath",
5511 if (repo_path == NULL) {
5512 cwd = getcwd(NULL, 0);
5514 return got_error_from_errno("getcwd");
5515 error = got_worktree_open(&worktree, cwd);
5516 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5520 strdup(got_worktree_get_repo_path(worktree));
5522 repo_path = strdup(cwd);
5523 if (repo_path == NULL) {
5524 error = got_error_from_errno("strdup");
5529 error = got_repo_open(&repo, repo_path, NULL);
5533 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5540 error = apply_unveil(got_repo_get_path(repo), NULL);
5544 error = tog_load_refs(repo);
5548 if (commit_id_arg == NULL) {
5549 error = got_repo_match_object_id(&commit_id, &label,
5550 worktree ? got_worktree_get_head_ref_name(worktree) :
5551 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5554 head_ref_name = label;
5556 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5558 head_ref_name = got_ref_get_name(ref);
5559 else if (error->code != GOT_ERR_NOT_REF)
5561 error = got_repo_match_object_id(&commit_id, NULL,
5562 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5567 error = got_object_open_as_commit(&commit, repo, commit_id);
5571 error = got_object_open_as_tree(&tree, repo,
5572 got_object_commit_get_tree_id(commit));
5576 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5578 error = got_error_from_errno("view_open");
5581 error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5584 if (!got_path_is_root_dir(in_repo_path)) {
5585 error = tree_view_walk_path(&view->state.tree, commit_id,
5592 /* Release work tree lock. */
5593 got_worktree_close(worktree);
5596 error = view_loop(view);
5605 got_object_commit_close(commit);
5607 got_object_tree_close(tree);
5609 got_repo_close(repo);
5614 static const struct got_error *
5615 ref_view_load_refs(struct tog_ref_view_state *s)
5617 struct got_reflist_entry *sre;
5618 struct tog_reflist_entry *re;
5621 SIMPLEQ_FOREACH(sre, &tog_refs, entry) {
5622 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5625 re = malloc(sizeof(*re));
5627 return got_error_from_errno("malloc");
5629 re->ref = got_ref_dup(sre->ref);
5630 if (re->ref == NULL)
5631 return got_error_from_errno("got_ref_dup");
5632 re->idx = s->nrefs++;
5633 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5636 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5641 ref_view_free_refs(struct tog_ref_view_state *s)
5643 struct tog_reflist_entry *re;
5645 while (!TAILQ_EMPTY(&s->refs)) {
5646 re = TAILQ_FIRST(&s->refs);
5647 TAILQ_REMOVE(&s->refs, re, entry);
5648 got_ref_close(re->ref);
5653 static const struct got_error *
5654 open_ref_view(struct tog_view *view, struct got_repository *repo)
5656 const struct got_error *err = NULL;
5657 struct tog_ref_view_state *s = &view->state.ref;
5659 s->selected_entry = 0;
5662 TAILQ_INIT(&s->refs);
5663 SIMPLEQ_INIT(&s->colors);
5665 err = ref_view_load_refs(s);
5669 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5670 err = add_color(&s->colors, "^refs/heads/",
5671 TOG_COLOR_REFS_HEADS,
5672 get_color_value("TOG_COLOR_REFS_HEADS"));
5676 err = add_color(&s->colors, "^refs/tags/",
5677 TOG_COLOR_REFS_TAGS,
5678 get_color_value("TOG_COLOR_REFS_TAGS"));
5682 err = add_color(&s->colors, "^refs/remotes/",
5683 TOG_COLOR_REFS_REMOTES,
5684 get_color_value("TOG_COLOR_REFS_REMOTES"));
5689 view->show = show_ref_view;
5690 view->input = input_ref_view;
5691 view->close = close_ref_view;
5692 view->search_start = search_start_ref_view;
5693 view->search_next = search_next_ref_view;
5696 free_colors(&s->colors);
5700 static const struct got_error *
5701 close_ref_view(struct tog_view *view)
5703 struct tog_ref_view_state *s = &view->state.ref;
5705 ref_view_free_refs(s);
5706 free_colors(&s->colors);
5711 static const struct got_error *
5712 resolve_reflist_entry(struct got_object_id **commit_id,
5713 struct tog_reflist_entry *re, struct got_repository *repo)
5715 const struct got_error *err = NULL;
5716 struct got_object_id *obj_id;
5717 struct got_tag_object *tag = NULL;
5722 err = got_ref_resolve(&obj_id, repo, re->ref);
5726 err = got_object_get_type(&obj_type, repo, obj_id);
5731 case GOT_OBJ_TYPE_COMMIT:
5732 *commit_id = obj_id;
5734 case GOT_OBJ_TYPE_TAG:
5735 err = got_object_open_as_tag(&tag, repo, obj_id);
5739 err = got_object_get_type(&obj_type, repo,
5740 got_object_tag_get_object_id(tag));
5743 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5744 err = got_error(GOT_ERR_OBJ_TYPE);
5747 *commit_id = got_object_id_dup(
5748 got_object_tag_get_object_id(tag));
5749 if (*commit_id == NULL) {
5750 err = got_error_from_errno("got_object_id_dup");
5755 err = got_error(GOT_ERR_OBJ_TYPE);
5761 got_object_tag_close(tag);
5769 static const struct got_error *
5770 log_ref_entry(struct tog_view **new_view, int begin_x,
5771 struct tog_reflist_entry *re, struct got_repository *repo)
5773 struct tog_view *log_view;
5774 const struct got_error *err = NULL;
5775 struct got_object_id *commit_id = NULL;
5779 err = resolve_reflist_entry(&commit_id, re, repo);
5781 if (err->code != GOT_ERR_OBJ_TYPE)
5787 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5788 if (log_view == NULL) {
5789 err = got_error_from_errno("view_open");
5793 err = open_log_view(log_view, commit_id, repo,
5794 got_ref_get_name(re->ref), "", 0);
5797 view_close(log_view);
5799 *new_view = log_view;
5805 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5807 struct tog_reflist_entry *re;
5810 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5813 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5814 while (i++ < maxscroll) {
5817 s->first_displayed_entry = re;
5818 re = TAILQ_PREV(re, tog_reflist_head, entry);
5823 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5825 struct tog_reflist_entry *next, *last;
5828 if (s->first_displayed_entry)
5829 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5831 next = TAILQ_FIRST(&s->refs);
5833 last = s->last_displayed_entry;
5834 while (next && last && n++ < maxscroll) {
5835 last = TAILQ_NEXT(last, entry);
5837 s->first_displayed_entry = next;
5838 next = TAILQ_NEXT(next, entry);
5843 static const struct got_error *
5844 search_start_ref_view(struct tog_view *view)
5846 struct tog_ref_view_state *s = &view->state.ref;
5848 s->matched_entry = NULL;
5853 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5855 regmatch_t regmatch;
5857 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5861 static const struct got_error *
5862 search_next_ref_view(struct tog_view *view)
5864 struct tog_ref_view_state *s = &view->state.ref;
5865 struct tog_reflist_entry *re = NULL;
5867 if (!view->searching) {
5868 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5872 if (s->matched_entry) {
5873 if (view->searching == TOG_SEARCH_FORWARD) {
5874 if (s->selected_entry)
5875 re = TAILQ_NEXT(s->selected_entry, entry);
5877 re = TAILQ_PREV(s->selected_entry,
5878 tog_reflist_head, entry);
5880 if (s->selected_entry == NULL)
5881 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5883 re = TAILQ_PREV(s->selected_entry,
5884 tog_reflist_head, entry);
5887 if (view->searching == TOG_SEARCH_FORWARD)
5888 re = TAILQ_FIRST(&s->refs);
5890 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5895 if (s->matched_entry == NULL) {
5896 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5899 if (view->searching == TOG_SEARCH_FORWARD)
5900 re = TAILQ_FIRST(&s->refs);
5902 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5905 if (match_reflist_entry(re, &view->regex)) {
5906 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5907 s->matched_entry = re;
5911 if (view->searching == TOG_SEARCH_FORWARD)
5912 re = TAILQ_NEXT(re, entry);
5914 re = TAILQ_PREV(re, tog_reflist_head, entry);
5917 if (s->matched_entry) {
5918 s->first_displayed_entry = s->matched_entry;
5925 static const struct got_error *
5926 show_ref_view(struct tog_view *view)
5928 const struct got_error *err = NULL;
5929 struct tog_ref_view_state *s = &view->state.ref;
5930 struct tog_reflist_entry *re;
5933 struct tog_color *tc;
5935 int limit = view->nlines;
5937 werase(view->window);
5944 re = s->first_displayed_entry;
5946 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5948 return got_error_from_errno("asprintf");
5950 err = format_line(&wline, &width, line, view->ncols, 0);
5955 if (view_needs_focus_indication(view))
5956 wstandout(view->window);
5957 waddwstr(view->window, wline);
5958 if (view_needs_focus_indication(view))
5959 wstandend(view->window);
5964 if (width < view->ncols - 1)
5965 waddch(view->window, '\n');
5970 while (re && limit > 0) {
5973 if (got_ref_is_symbolic(re->ref)) {
5974 if (asprintf(&line, "%s -> %s",
5975 got_ref_get_name(re->ref),
5976 got_ref_get_symref_target(re->ref)) == -1)
5977 return got_error_from_errno("asprintf");
5978 } else if (s->show_ids) {
5979 struct got_object_id *id;
5981 err = got_ref_resolve(&id, s->repo, re->ref);
5984 err = got_object_id_str(&id_str, id);
5989 if (asprintf(&line, "%s: %s",
5990 got_ref_get_name(re->ref), id_str) == -1) {
5991 err = got_error_from_errno("asprintf");
5999 line = strdup(got_ref_get_name(re->ref));
6001 return got_error_from_errno("strdup");
6004 err = format_line(&wline, &width, line, view->ncols, 0);
6009 if (n == s->selected) {
6011 wstandout(view->window);
6012 s->selected_entry = re;
6014 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6016 wattr_on(view->window,
6017 COLOR_PAIR(tc->colorpair), NULL);
6018 waddwstr(view->window, wline);
6020 wattr_off(view->window,
6021 COLOR_PAIR(tc->colorpair), NULL);
6022 if (width < view->ncols - 1)
6023 waddch(view->window, '\n');
6024 if (n == s->selected && view->focussed)
6025 wstandend(view->window);
6031 s->last_displayed_entry = re;
6034 re = TAILQ_NEXT(re, entry);
6041 static const struct got_error *
6042 browse_ref_tree(struct tog_view **new_view, int begin_x,
6043 struct tog_reflist_entry *re, struct got_repository *repo)
6045 const struct got_error *err = NULL;
6046 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6047 struct got_tree_object *tree = NULL;
6048 struct tog_view *tree_view;
6052 err = resolve_reflist_entry(&commit_id, re, repo);
6054 if (err->code != GOT_ERR_OBJ_TYPE)
6060 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6064 err = got_object_open_as_tree(&tree, repo, tree_id);
6068 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6069 if (tree_view == NULL) {
6070 err = got_error_from_errno("view_open");
6074 err = open_tree_view(tree_view, tree, commit_id,
6075 got_ref_get_name(re->ref), repo);
6079 *new_view = tree_view;
6085 got_object_tree_close(tree);
6089 static const struct got_error *
6090 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6092 const struct got_error *err = NULL;
6093 struct tog_ref_view_state *s = &view->state.ref;
6094 struct tog_view *log_view, *tree_view;
6099 s->show_ids = !s->show_ids;
6103 if (!s->selected_entry)
6105 if (view_is_parent_view(view))
6106 begin_x = view_split_begin_x(view->begin_x);
6107 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6110 log_view->focussed = 1;
6111 if (view_is_parent_view(view)) {
6112 err = view_close_child(view);
6115 view_set_child(view, log_view);
6116 view->focus_child = 1;
6118 *new_view = log_view;
6121 if (!s->selected_entry)
6123 if (view_is_parent_view(view))
6124 begin_x = view_split_begin_x(view->begin_x);
6125 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6127 if (err || tree_view == NULL)
6130 tree_view->focussed = 1;
6131 if (view_is_parent_view(view)) {
6132 err = view_close_child(view);
6135 view_set_child(view, tree_view);
6136 view->focus_child = 1;
6138 *new_view = tree_view;
6142 if (s->selected > 0) {
6146 ref_scroll_up(s, 1);
6150 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6152 ref_scroll_up(s, MAX(0, view->nlines - 1));
6156 if (s->selected < s->ndisplayed - 1) {
6160 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6161 /* can't scroll any further */
6163 ref_scroll_down(s, 1);
6167 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6168 /* can't scroll any further; move cursor down */
6169 if (s->selected < s->ndisplayed - 1)
6170 s->selected = s->ndisplayed - 1;
6173 ref_scroll_down(s, view->nlines - 1);
6177 err = tog_load_refs(s->repo);
6180 ref_view_free_refs(s);
6181 err = ref_view_load_refs(s);
6184 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6185 s->selected = view->nlines - 2;
6198 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6203 static const struct got_error *
6204 cmd_ref(int argc, char *argv[])
6206 const struct got_error *error;
6207 struct got_repository *repo = NULL;
6208 struct got_worktree *worktree = NULL;
6209 char *cwd = NULL, *repo_path = NULL;
6211 struct tog_view *view;
6213 while ((ch = getopt(argc, argv, "r:")) != -1) {
6216 repo_path = realpath(optarg, NULL);
6217 if (repo_path == NULL)
6218 return got_error_from_errno2("realpath",
6233 if (repo_path == NULL) {
6234 cwd = getcwd(NULL, 0);
6236 return got_error_from_errno("getcwd");
6237 error = got_worktree_open(&worktree, cwd);
6238 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6242 strdup(got_worktree_get_repo_path(worktree));
6244 repo_path = strdup(cwd);
6245 if (repo_path == NULL) {
6246 error = got_error_from_errno("strdup");
6251 error = got_repo_open(&repo, repo_path, NULL);
6257 error = apply_unveil(got_repo_get_path(repo), NULL);
6261 error = tog_load_refs(repo);
6265 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6267 error = got_error_from_errno("view_open");
6271 error = open_ref_view(view, repo);
6276 /* Release work tree lock. */
6277 got_worktree_close(worktree);
6280 error = view_loop(view);
6285 got_repo_close(repo);
6291 list_commands(FILE *fp)
6295 fprintf(fp, "commands:");
6296 for (i = 0; i < nitems(tog_commands); i++) {
6297 struct tog_cmd *cmd = &tog_commands[i];
6298 fprintf(fp, " %s", cmd->name);
6304 usage(int hflag, int status)
6306 FILE *fp = (status == 0) ? stdout : stderr;
6308 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6311 fprintf(fp, "lazy usage: %s path\n", getprogname());
6318 make_argv(int argc, ...)
6326 argv = calloc(argc, sizeof(char *));
6329 for (i = 0; i < argc; i++) {
6330 argv[i] = strdup(va_arg(ap, char *));
6331 if (argv[i] == NULL)
6340 * Try to convert 'tog path' into a 'tog log path' command.
6341 * The user could simply have mistyped the command rather than knowingly
6342 * provided a path. So check whether argv[0] can in fact be resolved
6343 * to a path in the HEAD commit and print a special error if not.
6344 * This hack is for mpi@ <3
6346 static const struct got_error *
6347 tog_log_with_path(int argc, char *argv[])
6349 const struct got_error *error = NULL;
6350 struct tog_cmd *cmd = NULL;
6351 struct got_repository *repo = NULL;
6352 struct got_worktree *worktree = NULL;
6353 struct got_object_id *commit_id = NULL, *id = NULL;
6354 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6355 char *commit_id_str = NULL, **cmd_argv = NULL;
6356 struct got_reflist_head refs;
6358 SIMPLEQ_INIT(&refs);
6360 cwd = getcwd(NULL, 0);
6362 return got_error_from_errno("getcwd");
6364 error = got_worktree_open(&worktree, cwd);
6365 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6369 repo_path = strdup(got_worktree_get_repo_path(worktree));
6371 repo_path = strdup(cwd);
6372 if (repo_path == NULL) {
6373 error = got_error_from_errno("strdup");
6377 error = got_repo_open(&repo, repo_path, NULL);
6381 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6386 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6389 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6390 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6391 GOT_OBJ_TYPE_COMMIT, &refs, repo);
6396 got_worktree_close(worktree);
6400 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6402 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6404 fprintf(stderr, "%s: '%s' is no known command or path\n",
6405 getprogname(), argv[0]);
6410 got_repo_close(repo);
6413 error = got_object_id_str(&commit_id_str, commit_id);
6417 cmd = &tog_commands[0]; /* log */
6419 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6420 error = cmd->cmd_main(argc, cmd_argv);
6423 got_repo_close(repo);
6425 got_worktree_close(worktree);
6427 free(commit_id_str);
6434 for (i = 0; i < argc; i++)
6438 got_ref_list_free(&refs);
6443 main(int argc, char *argv[])
6445 const struct got_error *error = NULL;
6446 struct tog_cmd *cmd = NULL;
6447 int ch, hflag = 0, Vflag = 0;
6448 char **cmd_argv = NULL;
6449 static struct option longopts[] = {
6450 { "version", no_argument, NULL, 'V' },
6454 setlocale(LC_CTYPE, "");
6456 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6476 got_version_print_str();
6481 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6489 /* Build an argument vector which runs a default command. */
6490 cmd = &tog_commands[0];
6492 cmd_argv = make_argv(argc, cmd->name);
6496 /* Did the user specify a command? */
6497 for (i = 0; i < nitems(tog_commands); i++) {
6498 if (strncmp(tog_commands[i].name, argv[0],
6499 strlen(argv[0])) == 0) {
6500 cmd = &tog_commands[i];
6509 /* No command specified; try log with a path */
6510 error = tog_log_with_path(argc, argv);
6515 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6522 for (i = 0; i < argc; i++)
6527 if (error && error->code != GOT_ERR_CANCELLED)
6528 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);