2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED
25 #undef _XOPEN_SOURCE_EXTENDED
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
49 #include "got_opentemp.h"
51 #include "got_cancel.h"
52 #include "got_commit_graph.h"
53 #include "got_blame.h"
54 #include "got_privsep.h"
56 #include "got_worktree.h"
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
66 #define CTRL(x) ((x) & 0x1f)
69 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
78 __dead static void usage(int, int);
79 __dead static void usage_log(void);
80 __dead static void usage_diff(void);
81 __dead static void usage_blame(void);
82 __dead static void usage_tree(void);
83 __dead static void usage_ref(void);
85 static const struct got_error* cmd_log(int, char *[]);
86 static const struct got_error* cmd_diff(int, char *[]);
87 static const struct got_error* cmd_blame(int, char *[]);
88 static const struct got_error* cmd_tree(int, char *[]);
89 static const struct got_error* cmd_ref(int, char *[]);
91 static struct tog_cmd tog_commands[] = {
92 { "log", cmd_log, usage_log },
93 { "diff", cmd_diff, usage_diff },
94 { "blame", cmd_blame, usage_blame },
95 { "tree", cmd_tree, usage_tree },
96 { "ref", cmd_ref, usage_ref },
107 #define TOG_EOF_STRING "(END)"
109 struct commit_queue_entry {
110 TAILQ_ENTRY(commit_queue_entry) entry;
111 struct got_object_id *id;
112 struct got_commit_object *commit;
115 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
116 struct commit_queue {
118 struct commit_queue_head head;
122 STAILQ_ENTRY(tog_color) entry;
126 STAILQ_HEAD(tog_colors, tog_color);
128 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
129 static struct got_reflist_object_id_map *tog_refs_idmap;
131 static const struct got_error *
132 tog_load_refs(struct got_repository *repo)
134 const struct got_error *err;
136 err = got_ref_list(&tog_refs, repo, NULL, got_ref_cmp_by_name, NULL);
140 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
147 if (tog_refs_idmap) {
148 got_reflist_object_id_map_free(tog_refs_idmap);
149 tog_refs_idmap = NULL;
151 got_ref_list_free(&tog_refs);
154 static const struct got_error *
155 add_color(struct tog_colors *colors, const char *pattern,
156 int idx, short color)
158 const struct got_error *err = NULL;
159 struct tog_color *tc;
162 if (idx < 1 || idx > COLOR_PAIRS - 1)
165 init_pair(idx, color, -1);
167 tc = calloc(1, sizeof(*tc));
169 return got_error_from_errno("calloc");
170 regerr = regcomp(&tc->regex, pattern,
171 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
173 static char regerr_msg[512];
174 static char err_msg[512];
175 regerror(regerr, &tc->regex, regerr_msg,
177 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
179 err = got_error_msg(GOT_ERR_REGEX, err_msg);
184 STAILQ_INSERT_HEAD(colors, tc, entry);
189 free_colors(struct tog_colors *colors)
191 struct tog_color *tc;
193 while (!STAILQ_EMPTY(colors)) {
194 tc = STAILQ_FIRST(colors);
195 STAILQ_REMOVE_HEAD(colors, entry);
202 get_color(struct tog_colors *colors, int colorpair)
204 struct tog_color *tc = NULL;
206 STAILQ_FOREACH(tc, colors, entry) {
207 if (tc->colorpair == colorpair)
215 default_color_value(const char *envvar)
217 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
218 return COLOR_MAGENTA;
219 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
221 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
223 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
225 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
226 return COLOR_MAGENTA;
227 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
228 return COLOR_MAGENTA;
229 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
231 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
233 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
235 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
237 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
239 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
241 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
242 return COLOR_MAGENTA;
243 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
250 get_color_value(const char *envvar)
252 const char *val = getenv(envvar);
255 return default_color_value(envvar);
257 if (strcasecmp(val, "black") == 0)
259 if (strcasecmp(val, "red") == 0)
261 if (strcasecmp(val, "green") == 0)
263 if (strcasecmp(val, "yellow") == 0)
265 if (strcasecmp(val, "blue") == 0)
267 if (strcasecmp(val, "magenta") == 0)
268 return COLOR_MAGENTA;
269 if (strcasecmp(val, "cyan") == 0)
271 if (strcasecmp(val, "white") == 0)
273 if (strcasecmp(val, "default") == 0)
276 return default_color_value(envvar);
280 struct tog_diff_view_state {
281 struct got_object_id *id1, *id2;
282 const char *label1, *label2;
284 int first_displayed_line;
285 int last_displayed_line;
288 int ignore_whitespace;
290 struct got_repository *repo;
291 struct tog_colors colors;
297 /* passed from log view; may be NULL */
298 struct tog_view *log_view;
301 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
303 struct tog_log_thread_args {
304 pthread_cond_t need_commits;
305 pthread_cond_t commit_loaded;
307 struct got_commit_graph *graph;
308 struct commit_queue *commits;
309 const char *in_repo_path;
310 struct got_object_id *start_id;
311 struct got_repository *repo;
314 struct commit_queue_entry **first_displayed_entry;
315 struct commit_queue_entry **selected_entry;
317 int *search_next_done;
321 struct tog_log_view_state {
322 struct commit_queue commits;
323 struct commit_queue_entry *first_displayed_entry;
324 struct commit_queue_entry *last_displayed_entry;
325 struct commit_queue_entry *selected_entry;
330 struct got_repository *repo;
331 struct got_object_id *start_id;
334 struct tog_log_thread_args thread_args;
335 struct commit_queue_entry *matched_entry;
336 struct commit_queue_entry *search_entry;
337 struct tog_colors colors;
340 #define TOG_COLOR_DIFF_MINUS 1
341 #define TOG_COLOR_DIFF_PLUS 2
342 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
343 #define TOG_COLOR_DIFF_META 4
344 #define TOG_COLOR_TREE_SUBMODULE 5
345 #define TOG_COLOR_TREE_SYMLINK 6
346 #define TOG_COLOR_TREE_DIRECTORY 7
347 #define TOG_COLOR_TREE_EXECUTABLE 8
348 #define TOG_COLOR_COMMIT 9
349 #define TOG_COLOR_AUTHOR 10
350 #define TOG_COLOR_DATE 11
351 #define TOG_COLOR_REFS_HEADS 12
352 #define TOG_COLOR_REFS_TAGS 13
353 #define TOG_COLOR_REFS_REMOTES 14
355 struct tog_blame_cb_args {
356 struct tog_blame_line *lines; /* one per line */
359 struct tog_view *view;
360 struct got_object_id *commit_id;
364 struct tog_blame_thread_args {
366 struct got_repository *repo;
367 struct tog_blame_cb_args *cb_args;
369 got_cancel_cb cancel_cb;
376 struct tog_blame_line *lines;
380 struct tog_blame_thread_args thread_args;
381 struct tog_blame_cb_args cb_args;
385 struct tog_blame_view_state {
386 int first_displayed_line;
387 int last_displayed_line;
392 struct got_object_id_queue blamed_commits;
393 struct got_object_qid *blamed_commit;
395 struct got_repository *repo;
396 struct got_object_id *commit_id;
397 struct tog_blame blame;
399 struct tog_colors colors;
402 struct tog_parent_tree {
403 TAILQ_ENTRY(tog_parent_tree) entry;
404 struct got_tree_object *tree;
405 struct got_tree_entry *first_displayed_entry;
406 struct got_tree_entry *selected_entry;
410 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
412 struct tog_tree_view_state {
414 struct got_tree_object *root;
415 struct got_tree_object *tree;
416 struct got_tree_entry *first_displayed_entry;
417 struct got_tree_entry *last_displayed_entry;
418 struct got_tree_entry *selected_entry;
419 int ndisplayed, selected, show_ids;
420 struct tog_parent_trees parents;
421 struct got_object_id *commit_id;
423 struct got_repository *repo;
424 struct got_tree_entry *matched_entry;
425 struct tog_colors colors;
428 struct tog_reflist_entry {
429 TAILQ_ENTRY(tog_reflist_entry) entry;
430 struct got_reference *ref;
434 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
436 struct tog_ref_view_state {
437 struct tog_reflist_head refs;
438 struct tog_reflist_entry *first_displayed_entry;
439 struct tog_reflist_entry *last_displayed_entry;
440 struct tog_reflist_entry *selected_entry;
441 int nrefs, ndisplayed, selected, show_ids;
442 struct got_repository *repo;
443 struct tog_reflist_entry *matched_entry;
444 struct tog_colors colors;
448 * We implement two types of views: parent views and child views.
450 * The 'Tab' key switches focus between a parent view and its child view.
451 * Child views are shown side-by-side to their parent view, provided
452 * there is enough screen estate.
454 * When a new view is opened from within a parent view, this new view
455 * becomes a child view of the parent view, replacing any existing child.
457 * When a new view is opened from within a child view, this new view
458 * becomes a parent view which will obscure the views below until the
459 * user quits the new parent view by typing 'q'.
461 * This list of views contains parent views only.
462 * Child views are only pointed to by their parent view.
464 TAILQ_HEAD(tog_view_list_head, tog_view);
467 TAILQ_ENTRY(tog_view) entry;
470 int nlines, ncols, begin_y, begin_x;
471 int lines, cols; /* copies of LINES and COLS */
472 int focussed; /* Only set on one parent or child view at a time. */
474 struct tog_view *parent;
475 struct tog_view *child;
478 * This flag is initially set on parent views when a new child view
479 * is created. It gets toggled when the 'Tab' key switches focus
480 * between parent and child.
481 * The flag indicates whether focus should be passed on to our child
482 * view if this parent view gets picked for focus after another parent
483 * view was closed. This prevents child views from losing focus in such
488 /* type-specific state */
489 enum tog_view_type type;
491 struct tog_diff_view_state diff;
492 struct tog_log_view_state log;
493 struct tog_blame_view_state blame;
494 struct tog_tree_view_state tree;
495 struct tog_ref_view_state ref;
498 const struct got_error *(*show)(struct tog_view *);
499 const struct got_error *(*input)(struct tog_view **,
500 struct tog_view *, int);
501 const struct got_error *(*close)(struct tog_view *);
503 const struct got_error *(*search_start)(struct tog_view *);
504 const struct got_error *(*search_next)(struct tog_view *);
507 #define TOG_SEARCH_FORWARD 1
508 #define TOG_SEARCH_BACKWARD 2
509 int search_next_done;
510 #define TOG_SEARCH_HAVE_MORE 1
511 #define TOG_SEARCH_NO_MORE 2
512 #define TOG_SEARCH_HAVE_NONE 3
517 static const struct got_error *open_diff_view(struct tog_view *,
518 struct got_object_id *, struct got_object_id *,
519 const char *, const char *, int, int, int, struct tog_view *,
520 struct got_repository *);
521 static const struct got_error *show_diff_view(struct tog_view *);
522 static const struct got_error *input_diff_view(struct tog_view **,
523 struct tog_view *, int);
524 static const struct got_error* close_diff_view(struct tog_view *);
525 static const struct got_error *search_start_diff_view(struct tog_view *);
526 static const struct got_error *search_next_diff_view(struct tog_view *);
528 static const struct got_error *open_log_view(struct tog_view *,
529 struct got_object_id *, struct got_repository *,
530 const char *, const char *, int);
531 static const struct got_error * show_log_view(struct tog_view *);
532 static const struct got_error *input_log_view(struct tog_view **,
533 struct tog_view *, int);
534 static const struct got_error *close_log_view(struct tog_view *);
535 static const struct got_error *search_start_log_view(struct tog_view *);
536 static const struct got_error *search_next_log_view(struct tog_view *);
538 static const struct got_error *open_blame_view(struct tog_view *, char *,
539 struct got_object_id *, struct got_repository *);
540 static const struct got_error *show_blame_view(struct tog_view *);
541 static const struct got_error *input_blame_view(struct tog_view **,
542 struct tog_view *, int);
543 static const struct got_error *close_blame_view(struct tog_view *);
544 static const struct got_error *search_start_blame_view(struct tog_view *);
545 static const struct got_error *search_next_blame_view(struct tog_view *);
547 static const struct got_error *open_tree_view(struct tog_view *,
548 struct got_tree_object *, struct got_object_id *, const char *,
549 struct got_repository *);
550 static const struct got_error *show_tree_view(struct tog_view *);
551 static const struct got_error *input_tree_view(struct tog_view **,
552 struct tog_view *, int);
553 static const struct got_error *close_tree_view(struct tog_view *);
554 static const struct got_error *search_start_tree_view(struct tog_view *);
555 static const struct got_error *search_next_tree_view(struct tog_view *);
557 static const struct got_error *open_ref_view(struct tog_view *,
558 struct got_repository *);
559 static const struct got_error *show_ref_view(struct tog_view *);
560 static const struct got_error *input_ref_view(struct tog_view **,
561 struct tog_view *, int);
562 static const struct got_error *close_ref_view(struct tog_view *);
563 static const struct got_error *search_start_ref_view(struct tog_view *);
564 static const struct got_error *search_next_ref_view(struct tog_view *);
566 static volatile sig_atomic_t tog_sigwinch_received;
567 static volatile sig_atomic_t tog_sigpipe_received;
568 static volatile sig_atomic_t tog_sigcont_received;
571 tog_sigwinch(int signo)
573 tog_sigwinch_received = 1;
577 tog_sigpipe(int signo)
579 tog_sigpipe_received = 1;
583 tog_sigcont(int signo)
585 tog_sigcont_received = 1;
588 static const struct got_error *
589 view_close(struct tog_view *view)
591 const struct got_error *err = NULL;
594 view_close(view->child);
598 err = view->close(view);
600 del_panel(view->panel);
602 delwin(view->window);
607 static struct tog_view *
608 view_open(int nlines, int ncols, int begin_y, int begin_x,
609 enum tog_view_type type)
611 struct tog_view *view = calloc(1, sizeof(*view));
619 view->nlines = nlines ? nlines : LINES - begin_y;
620 view->ncols = ncols ? ncols : COLS - begin_x;
621 view->begin_y = begin_y;
622 view->begin_x = begin_x;
623 view->window = newwin(nlines, ncols, begin_y, begin_x);
624 if (view->window == NULL) {
628 view->panel = new_panel(view->window);
629 if (view->panel == NULL ||
630 set_panel_userptr(view->panel, view) != OK) {
635 keypad(view->window, TRUE);
640 view_split_begin_x(int begin_x)
642 if (begin_x > 0 || COLS < 120)
644 return (COLS - MAX(COLS / 2, 80));
647 static const struct got_error *view_resize(struct tog_view *);
649 static const struct got_error *
650 view_splitscreen(struct tog_view *view)
652 const struct got_error *err = NULL;
655 view->begin_x = view_split_begin_x(0);
656 view->nlines = LINES;
657 view->ncols = COLS - view->begin_x;
660 err = view_resize(view);
664 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
665 return got_error_from_errno("mvwin");
670 static const struct got_error *
671 view_fullscreen(struct tog_view *view)
673 const struct got_error *err = NULL;
677 view->nlines = LINES;
681 err = view_resize(view);
685 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
686 return got_error_from_errno("mvwin");
692 view_is_parent_view(struct tog_view *view)
694 return view->parent == NULL;
697 static const struct got_error *
698 view_resize(struct tog_view *view)
702 if (view->lines > LINES)
703 nlines = view->nlines - (view->lines - LINES);
705 nlines = view->nlines + (LINES - view->lines);
707 if (view->cols > COLS)
708 ncols = view->ncols - (view->cols - COLS);
710 ncols = view->ncols + (COLS - view->cols);
712 if (wresize(view->window, nlines, ncols) == ERR)
713 return got_error_from_errno("wresize");
714 if (replace_panel(view->panel, view->window) == ERR)
715 return got_error_from_errno("replace_panel");
716 wclear(view->window);
718 view->nlines = nlines;
724 view->child->begin_x = view_split_begin_x(view->begin_x);
725 if (view->child->begin_x == 0) {
726 view_fullscreen(view->child);
727 if (view->child->focussed)
728 show_panel(view->child->panel);
730 show_panel(view->panel);
732 view_splitscreen(view->child);
733 show_panel(view->child->panel);
740 static const struct got_error *
741 view_close_child(struct tog_view *view)
743 const struct got_error *err = NULL;
745 if (view->child == NULL)
748 err = view_close(view->child);
754 view_set_child(struct tog_view *view, struct tog_view *child)
757 child->parent = view;
761 view_is_splitscreen(struct tog_view *view)
763 return view->begin_x > 0;
772 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
773 cols = 80; /* Default */
779 resize_term(lines, cols);
782 static const struct got_error *
783 view_search_start(struct tog_view *view)
785 const struct got_error *err = NULL;
789 if (view->search_started) {
790 regfree(&view->regex);
792 memset(&view->regmatch, 0, sizeof(view->regmatch));
794 view->search_started = 0;
796 if (view->nlines < 1)
799 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
800 wclrtoeol(view->window);
804 ret = wgetnstr(view->window, pattern, sizeof(pattern));
810 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
811 err = view->search_start(view);
813 regfree(&view->regex);
816 view->search_started = 1;
817 view->searching = TOG_SEARCH_FORWARD;
818 view->search_next_done = 0;
819 view->search_next(view);
825 static const struct got_error *
826 view_input(struct tog_view **new, int *done, struct tog_view *view,
827 struct tog_view_list_head *views)
829 const struct got_error *err = NULL;
835 /* Clear "no matches" indicator. */
836 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
837 view->search_next_done == TOG_SEARCH_HAVE_NONE)
838 view->search_next_done = TOG_SEARCH_HAVE_MORE;
840 if (view->searching && !view->search_next_done) {
841 errcode = pthread_mutex_unlock(&tog_mutex);
843 return got_error_set_errno(errcode,
844 "pthread_mutex_unlock");
846 errcode = pthread_mutex_lock(&tog_mutex);
848 return got_error_set_errno(errcode,
849 "pthread_mutex_lock");
850 view->search_next(view);
854 nodelay(stdscr, FALSE);
855 /* Allow threads to make progress while we are waiting for input. */
856 errcode = pthread_mutex_unlock(&tog_mutex);
858 return got_error_set_errno(errcode, "pthread_mutex_unlock");
859 ch = wgetch(view->window);
860 errcode = pthread_mutex_lock(&tog_mutex);
862 return got_error_set_errno(errcode, "pthread_mutex_lock");
863 nodelay(stdscr, TRUE);
865 if (tog_sigwinch_received || tog_sigcont_received) {
867 tog_sigwinch_received = 0;
868 tog_sigcont_received = 0;
869 TAILQ_FOREACH(v, views, entry) {
870 err = view_resize(v);
873 err = v->input(new, v, KEY_RESIZE);
877 err = view_resize(v->child);
880 err = v->child->input(new, v->child,
894 view->child->focussed = 1;
895 view->focus_child = 1;
896 } else if (view->parent) {
898 view->parent->focussed = 1;
899 view->parent->focus_child = 0;
903 err = view->input(new, view, ch);
910 if (view_is_parent_view(view)) {
911 if (view->child == NULL)
913 if (view_is_splitscreen(view->child)) {
915 view->child->focussed = 1;
916 err = view_fullscreen(view->child);
918 err = view_splitscreen(view->child);
921 err = view->child->input(new, view->child,
924 if (view_is_splitscreen(view)) {
925 view->parent->focussed = 0;
927 err = view_fullscreen(view);
929 err = view_splitscreen(view);
933 err = view->input(new, view, KEY_RESIZE);
939 if (view->search_start)
940 view_search_start(view);
942 err = view->input(new, view, ch);
946 if (view->search_started && view->search_next) {
947 view->searching = (ch == 'n' ?
948 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
949 view->search_next_done = 0;
950 view->search_next(view);
952 err = view->input(new, view, ch);
955 err = view->input(new, view, ch);
963 view_vborder(struct tog_view *view)
966 const struct tog_view *view_above;
969 return view_vborder(view->parent);
971 panel = panel_above(view->panel);
975 view_above = panel_userptr(panel);
976 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
977 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
981 view_needs_focus_indication(struct tog_view *view)
983 if (view_is_parent_view(view)) {
984 if (view->child == NULL || view->child->focussed)
986 if (!view_is_splitscreen(view->child))
988 } else if (!view_is_splitscreen(view))
991 return view->focussed;
994 static const struct got_error *
995 view_loop(struct tog_view *view)
997 const struct got_error *err = NULL;
998 struct tog_view_list_head views;
999 struct tog_view *new_view;
1000 int fast_refresh = 10;
1001 int done = 0, errcode;
1003 errcode = pthread_mutex_lock(&tog_mutex);
1005 return got_error_set_errno(errcode, "pthread_mutex_lock");
1008 TAILQ_INSERT_HEAD(&views, view, entry);
1011 err = view->show(view);
1016 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1017 /* Refresh fast during initialization, then become slower. */
1018 if (fast_refresh && fast_refresh-- == 0)
1019 halfdelay(10); /* switch to once per second */
1021 err = view_input(&new_view, &done, view, &views);
1025 struct tog_view *v, *prev = NULL;
1027 if (view_is_parent_view(view))
1028 prev = TAILQ_PREV(view, tog_view_list_head,
1030 else if (view->parent)
1031 prev = view->parent;
1034 view->parent->child = NULL;
1035 view->parent->focus_child = 0;
1037 TAILQ_REMOVE(&views, view, entry);
1039 err = view_close(view);
1044 TAILQ_FOREACH(v, &views, entry) {
1048 if (view == NULL && new_view == NULL) {
1049 /* No view has focus. Try to pick one. */
1052 else if (!TAILQ_EMPTY(&views)) {
1053 view = TAILQ_LAST(&views,
1054 tog_view_list_head);
1057 if (view->focus_child) {
1058 view->child->focussed = 1;
1066 struct tog_view *v, *t;
1067 /* Only allow one parent view per type. */
1068 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1069 if (v->type != new_view->type)
1071 TAILQ_REMOVE(&views, v, entry);
1072 err = view_close(v);
1077 TAILQ_INSERT_TAIL(&views, new_view, entry);
1081 if (view_is_parent_view(view)) {
1082 if (view->child && view->child->focussed)
1085 if (view->parent && view->parent->focussed)
1086 view = view->parent;
1088 show_panel(view->panel);
1089 if (view->child && view_is_splitscreen(view->child))
1090 show_panel(view->child->panel);
1091 if (view->parent && view_is_splitscreen(view)) {
1092 err = view->parent->show(view->parent);
1096 err = view->show(view);
1100 err = view->child->show(view->child);
1109 while (!TAILQ_EMPTY(&views)) {
1110 view = TAILQ_FIRST(&views);
1111 TAILQ_REMOVE(&views, view, entry);
1115 errcode = pthread_mutex_unlock(&tog_mutex);
1116 if (errcode && err == NULL)
1117 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1127 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1132 /* Create newly allocated wide-character string equivalent to a byte string. */
1133 static const struct got_error *
1134 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1137 const struct got_error *err = NULL;
1140 *wlen = mbstowcs(NULL, s, 0);
1141 if (*wlen == (size_t)-1) {
1143 if (errno != EILSEQ)
1144 return got_error_from_errno("mbstowcs");
1146 /* byte string invalid in current encoding; try to "fix" it */
1147 err = got_mbsavis(&vis, &vislen, s);
1150 *wlen = mbstowcs(NULL, vis, 0);
1151 if (*wlen == (size_t)-1) {
1152 err = got_error_from_errno("mbstowcs"); /* give up */
1157 *ws = calloc(*wlen + 1, sizeof(**ws));
1159 err = got_error_from_errno("calloc");
1163 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1164 err = got_error_from_errno("mbstowcs");
1175 /* Format a line for display, ensuring that it won't overflow a width limit. */
1176 static const struct got_error *
1177 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1180 const struct got_error *err = NULL;
1182 wchar_t *wline = NULL;
1189 err = mbs2ws(&wline, &wlen, line);
1193 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1194 wline[wlen - 1] = L'\0';
1197 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1198 wline[wlen - 1] = L'\0';
1204 int width = wcwidth(wline[i]);
1211 if (width == 1 || width == 2) {
1212 if (cols + width > wlimit)
1216 } else if (width == -1) {
1217 if (wline[i] == L'\t') {
1219 ((cols + col_tab_align) % TABSIZE);
1224 if (cols + width > wlimit)
1229 err = got_error_from_errno("wcwidth");
1244 static const struct got_error*
1245 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1246 struct got_object_id *id, struct got_repository *repo)
1248 static const struct got_error *err = NULL;
1249 struct got_reflist_entry *re;
1255 TAILQ_FOREACH(re, refs, entry) {
1256 struct got_tag_object *tag = NULL;
1257 struct got_object_id *ref_id;
1260 name = got_ref_get_name(re->ref);
1261 if (strcmp(name, GOT_REF_HEAD) == 0)
1263 if (strncmp(name, "refs/", 5) == 0)
1265 if (strncmp(name, "got/", 4) == 0)
1267 if (strncmp(name, "heads/", 6) == 0)
1269 if (strncmp(name, "remotes/", 8) == 0) {
1271 s = strstr(name, "/" GOT_REF_HEAD);
1272 if (s != NULL && s[strlen(s)] == '\0')
1275 err = got_ref_resolve(&ref_id, repo, re->ref);
1278 if (strncmp(name, "tags/", 5) == 0) {
1279 err = got_object_open_as_tag(&tag, repo, ref_id);
1281 if (err->code != GOT_ERR_OBJ_TYPE) {
1285 /* Ref points at something other than a tag. */
1290 cmp = got_object_id_cmp(tag ?
1291 got_object_tag_get_object_id(tag) : ref_id, id);
1294 got_object_tag_close(tag);
1298 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1299 s ? ", " : "", name) == -1) {
1300 err = got_error_from_errno("asprintf");
1311 static const struct got_error *
1312 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1317 smallerthan = strchr(author, '<');
1318 if (smallerthan && smallerthan[1] != '\0')
1319 author = smallerthan + 1;
1320 author[strcspn(author, "@>")] = '\0';
1321 return format_line(wauthor, author_width, author, limit, col_tab_align);
1324 static const struct got_error *
1325 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1326 struct got_object_id *id, const size_t date_display_cols,
1327 int author_display_cols)
1329 struct tog_log_view_state *s = &view->state.log;
1330 const struct got_error *err = NULL;
1331 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1332 char *logmsg0 = NULL, *logmsg = NULL;
1333 char *author = NULL;
1334 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1335 int author_width, logmsg_width;
1336 char *newline, *line = NULL;
1338 const int avail = view->ncols;
1340 time_t committer_time;
1341 struct tog_color *tc;
1343 committer_time = got_object_commit_get_committer_time(commit);
1344 if (localtime_r(&committer_time, &tm) == NULL)
1345 return got_error_from_errno("localtime_r");
1346 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1347 return got_error(GOT_ERR_NO_SPACE);
1349 if (avail <= date_display_cols)
1350 limit = MIN(sizeof(datebuf) - 1, avail);
1352 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1353 tc = get_color(&s->colors, TOG_COLOR_DATE);
1355 wattr_on(view->window,
1356 COLOR_PAIR(tc->colorpair), NULL);
1357 waddnstr(view->window, datebuf, limit);
1359 wattr_off(view->window,
1360 COLOR_PAIR(tc->colorpair), NULL);
1367 err = got_object_id_str(&id_str, id);
1370 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1372 wattr_on(view->window,
1373 COLOR_PAIR(tc->colorpair), NULL);
1374 wprintw(view->window, "%.8s ", id_str);
1376 wattr_off(view->window,
1377 COLOR_PAIR(tc->colorpair), NULL);
1384 author = strdup(got_object_commit_get_author(commit));
1385 if (author == NULL) {
1386 err = got_error_from_errno("strdup");
1389 err = format_author(&wauthor, &author_width, author, avail - col, col);
1392 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1394 wattr_on(view->window,
1395 COLOR_PAIR(tc->colorpair), NULL);
1396 waddwstr(view->window, wauthor);
1398 wattr_off(view->window,
1399 COLOR_PAIR(tc->colorpair), NULL);
1400 col += author_width;
1401 while (col < avail && author_width < author_display_cols + 2) {
1402 waddch(view->window, ' ');
1409 err = got_object_commit_get_logmsg(&logmsg0, commit);
1413 while (*logmsg == '\n')
1415 newline = strchr(logmsg, '\n');
1418 limit = avail - col;
1419 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1422 waddwstr(view->window, wlogmsg);
1423 col += logmsg_width;
1424 while (col < avail) {
1425 waddch(view->window, ' ');
1437 static struct commit_queue_entry *
1438 alloc_commit_queue_entry(struct got_commit_object *commit,
1439 struct got_object_id *id)
1441 struct commit_queue_entry *entry;
1443 entry = calloc(1, sizeof(*entry));
1448 entry->commit = commit;
1453 pop_commit(struct commit_queue *commits)
1455 struct commit_queue_entry *entry;
1457 entry = TAILQ_FIRST(&commits->head);
1458 TAILQ_REMOVE(&commits->head, entry, entry);
1459 got_object_commit_close(entry->commit);
1460 commits->ncommits--;
1461 /* Don't free entry->id! It is owned by the commit graph. */
1466 free_commits(struct commit_queue *commits)
1468 while (!TAILQ_EMPTY(&commits->head))
1469 pop_commit(commits);
1472 static const struct got_error *
1473 match_commit(int *have_match, struct got_object_id *id,
1474 struct got_commit_object *commit, regex_t *regex)
1476 const struct got_error *err = NULL;
1477 regmatch_t regmatch;
1478 char *id_str = NULL, *logmsg = NULL;
1482 err = got_object_id_str(&id_str, id);
1486 err = got_object_commit_get_logmsg(&logmsg, commit);
1490 if (regexec(regex, got_object_commit_get_author(commit), 1,
1491 ®match, 0) == 0 ||
1492 regexec(regex, got_object_commit_get_committer(commit), 1,
1493 ®match, 0) == 0 ||
1494 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1495 regexec(regex, logmsg, 1, ®match, 0) == 0)
1503 static const struct got_error *
1504 queue_commits(struct tog_log_thread_args *a)
1506 const struct got_error *err = NULL;
1509 * We keep all commits open throughout the lifetime of the log
1510 * view in order to avoid having to re-fetch commits from disk
1511 * while updating the display.
1514 struct got_object_id *id;
1515 struct got_commit_object *commit;
1516 struct commit_queue_entry *entry;
1519 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1521 if (err || id == NULL)
1524 err = got_object_open_as_commit(&commit, a->repo, id);
1527 entry = alloc_commit_queue_entry(commit, id);
1528 if (entry == NULL) {
1529 err = got_error_from_errno("alloc_commit_queue_entry");
1533 errcode = pthread_mutex_lock(&tog_mutex);
1535 err = got_error_set_errno(errcode,
1536 "pthread_mutex_lock");
1540 entry->idx = a->commits->ncommits;
1541 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1542 a->commits->ncommits++;
1544 if (*a->searching == TOG_SEARCH_FORWARD &&
1545 !*a->search_next_done) {
1547 err = match_commit(&have_match, id, commit, a->regex);
1551 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1554 errcode = pthread_mutex_unlock(&tog_mutex);
1555 if (errcode && err == NULL)
1556 err = got_error_set_errno(errcode,
1557 "pthread_mutex_unlock");
1560 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1566 select_commit(struct tog_log_view_state *s)
1568 struct commit_queue_entry *entry;
1571 entry = s->first_displayed_entry;
1573 if (ncommits == s->selected) {
1574 s->selected_entry = entry;
1577 entry = TAILQ_NEXT(entry, entry);
1582 static const struct got_error *
1583 draw_commits(struct tog_view *view)
1585 const struct got_error *err = NULL;
1586 struct tog_log_view_state *s = &view->state.log;
1587 struct commit_queue_entry *entry = s->selected_entry;
1588 const int limit = view->nlines;
1590 int ncommits, author_cols = 4;
1591 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1592 char *refs_str = NULL;
1594 struct tog_color *tc;
1595 static const size_t date_display_cols = 12;
1597 if (s->selected_entry &&
1598 !(view->searching && view->search_next_done == 0)) {
1599 struct got_reflist_head *refs;
1600 err = got_object_id_str(&id_str, s->selected_entry->id);
1603 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1604 s->selected_entry->id);
1606 err = build_refs_str(&refs_str, refs,
1607 s->selected_entry->id, s->repo);
1613 if (s->thread_args.commits_needed == 0)
1614 halfdelay(10); /* disable fast refresh */
1616 if (s->thread_args.commits_needed > 0) {
1617 if (asprintf(&ncommits_str, " [%d/%d] %s",
1618 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1619 (view->searching && !view->search_next_done) ?
1620 "searching..." : "loading...") == -1) {
1621 err = got_error_from_errno("asprintf");
1625 const char *search_str = NULL;
1627 if (view->searching) {
1628 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1629 search_str = "no more matches";
1630 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1631 search_str = "no matches found";
1632 else if (!view->search_next_done)
1633 search_str = "searching...";
1636 if (asprintf(&ncommits_str, " [%d/%d] %s",
1637 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1638 search_str ? search_str :
1639 (refs_str ? refs_str : "")) == -1) {
1640 err = got_error_from_errno("asprintf");
1645 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1646 if (asprintf(&header, "commit %s %s%s",
1647 id_str ? id_str : "........................................",
1648 s->in_repo_path, ncommits_str) == -1) {
1649 err = got_error_from_errno("asprintf");
1653 } else if (asprintf(&header, "commit %s%s",
1654 id_str ? id_str : "........................................",
1655 ncommits_str) == -1) {
1656 err = got_error_from_errno("asprintf");
1660 err = format_line(&wline, &width, header, view->ncols, 0);
1664 werase(view->window);
1666 if (view_needs_focus_indication(view))
1667 wstandout(view->window);
1668 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1670 wattr_on(view->window,
1671 COLOR_PAIR(tc->colorpair), NULL);
1672 waddwstr(view->window, wline);
1674 wattr_off(view->window,
1675 COLOR_PAIR(tc->colorpair), NULL);
1676 while (width < view->ncols) {
1677 waddch(view->window, ' ');
1680 if (view_needs_focus_indication(view))
1681 wstandend(view->window);
1686 /* Grow author column size if necessary. */
1687 entry = s->first_displayed_entry;
1693 if (ncommits >= limit - 1)
1695 author = strdup(got_object_commit_get_author(entry->commit));
1696 if (author == NULL) {
1697 err = got_error_from_errno("strdup");
1700 err = format_author(&wauthor, &width, author, COLS,
1702 if (author_cols < width)
1703 author_cols = width;
1707 entry = TAILQ_NEXT(entry, entry);
1710 entry = s->first_displayed_entry;
1711 s->last_displayed_entry = s->first_displayed_entry;
1714 if (ncommits >= limit - 1)
1716 if (ncommits == s->selected)
1717 wstandout(view->window);
1718 err = draw_commit(view, entry->commit, entry->id,
1719 date_display_cols, author_cols);
1720 if (ncommits == s->selected)
1721 wstandend(view->window);
1725 s->last_displayed_entry = entry;
1726 entry = TAILQ_NEXT(entry, entry);
1739 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1741 struct commit_queue_entry *entry;
1744 entry = TAILQ_FIRST(&s->commits.head);
1745 if (s->first_displayed_entry == entry)
1748 entry = s->first_displayed_entry;
1749 while (entry && nscrolled < maxscroll) {
1750 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1752 s->first_displayed_entry = entry;
1758 static const struct got_error *
1759 trigger_log_thread(struct tog_view *view, int wait)
1761 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1764 halfdelay(1); /* fast refresh while loading commits */
1766 while (ta->commits_needed > 0) {
1767 if (ta->log_complete)
1770 /* Wake the log thread. */
1771 errcode = pthread_cond_signal(&ta->need_commits);
1773 return got_error_set_errno(errcode,
1774 "pthread_cond_signal");
1777 * The mutex will be released while the view loop waits
1778 * in wgetch(), at which time the log thread will run.
1783 /* Display progress update in log view. */
1784 show_log_view(view);
1788 /* Wait right here while next commit is being loaded. */
1789 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1791 return got_error_set_errno(errcode,
1792 "pthread_cond_wait");
1794 /* Display progress update in log view. */
1795 show_log_view(view);
1803 static const struct got_error *
1804 log_scroll_down(struct tog_view *view, int maxscroll)
1806 struct tog_log_view_state *s = &view->state.log;
1807 const struct got_error *err = NULL;
1808 struct commit_queue_entry *pentry;
1809 int nscrolled = 0, ncommits_needed;
1811 if (s->last_displayed_entry == NULL)
1814 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1815 if (s->commits.ncommits < ncommits_needed &&
1816 !s->thread_args.log_complete) {
1818 * Ask the log thread for required amount of commits.
1820 s->thread_args.commits_needed += maxscroll;
1821 err = trigger_log_thread(view, 1);
1827 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1831 s->last_displayed_entry = pentry;
1833 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1836 s->first_displayed_entry = pentry;
1837 } while (++nscrolled < maxscroll);
1842 static const struct got_error *
1843 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1844 struct got_commit_object *commit, struct got_object_id *commit_id,
1845 struct tog_view *log_view, struct got_repository *repo)
1847 const struct got_error *err;
1848 struct got_object_qid *parent_id;
1849 struct tog_view *diff_view;
1851 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1852 if (diff_view == NULL)
1853 return got_error_from_errno("view_open");
1855 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1856 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1857 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1859 *new_view = diff_view;
1863 static const struct got_error *
1864 tree_view_visit_subtree(struct tog_tree_view_state *s,
1865 struct got_tree_object *subtree)
1867 struct tog_parent_tree *parent;
1869 parent = calloc(1, sizeof(*parent));
1871 return got_error_from_errno("calloc");
1873 parent->tree = s->tree;
1874 parent->first_displayed_entry = s->first_displayed_entry;
1875 parent->selected_entry = s->selected_entry;
1876 parent->selected = s->selected;
1877 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1880 s->first_displayed_entry = NULL;
1884 static const struct got_error *
1885 tree_view_walk_path(struct tog_tree_view_state *s,
1886 struct got_object_id *commit_id, const char *path)
1888 const struct got_error *err = NULL;
1889 struct got_tree_object *tree = NULL;
1891 char *slash, *subpath = NULL;
1893 /* Walk the path and open corresponding tree objects. */
1896 struct got_tree_entry *te;
1897 struct got_object_id *tree_id;
1903 /* Ensure the correct subtree entry is selected. */
1904 slash = strchr(p, '/');
1906 te_name = strdup(p);
1908 te_name = strndup(p, slash - p);
1909 if (te_name == NULL) {
1910 err = got_error_from_errno("strndup");
1913 te = got_object_tree_find_entry(s->tree, te_name);
1915 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1920 s->first_displayed_entry = s->selected_entry = te;
1922 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1923 break; /* jump to this file's entry */
1925 slash = strchr(p, '/');
1927 subpath = strndup(path, slash - path);
1929 subpath = strdup(path);
1930 if (subpath == NULL) {
1931 err = got_error_from_errno("strdup");
1935 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1940 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1945 err = tree_view_visit_subtree(s, tree);
1947 got_object_tree_close(tree);
1961 static const struct got_error *
1962 browse_commit_tree(struct tog_view **new_view, int begin_x,
1963 struct commit_queue_entry *entry, const char *path,
1964 const char *head_ref_name, struct got_repository *repo)
1966 const struct got_error *err = NULL;
1967 struct got_tree_object *tree;
1968 struct tog_tree_view_state *s;
1969 struct tog_view *tree_view;
1971 err = got_object_open_as_tree(&tree, repo,
1972 got_object_commit_get_tree_id(entry->commit));
1976 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1977 if (tree_view == NULL)
1978 return got_error_from_errno("view_open");
1980 err = open_tree_view(tree_view, tree, entry->id, head_ref_name, repo);
1982 got_object_tree_close(tree);
1985 s = &tree_view->state.tree;
1987 *new_view = tree_view;
1989 if (got_path_is_root_dir(path))
1992 return tree_view_walk_path(s, entry->id, path);
1995 static const struct got_error *
1996 block_signals_used_by_main_thread(void)
2001 if (sigemptyset(&sigset) == -1)
2002 return got_error_from_errno("sigemptyset");
2004 /* tog handles SIGWINCH and SIGCONT */
2005 if (sigaddset(&sigset, SIGWINCH) == -1)
2006 return got_error_from_errno("sigaddset");
2007 if (sigaddset(&sigset, SIGCONT) == -1)
2008 return got_error_from_errno("sigaddset");
2010 /* ncurses handles SIGTSTP */
2011 if (sigaddset(&sigset, SIGTSTP) == -1)
2012 return got_error_from_errno("sigaddset");
2014 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2016 return got_error_set_errno(errcode, "pthread_sigmask");
2022 log_thread(void *arg)
2024 const struct got_error *err = NULL;
2026 struct tog_log_thread_args *a = arg;
2029 err = block_signals_used_by_main_thread();
2033 while (!done && !err && !tog_sigpipe_received) {
2034 err = queue_commits(a);
2036 if (err->code != GOT_ERR_ITER_COMPLETED)
2040 } else if (a->commits_needed > 0)
2041 a->commits_needed--;
2043 errcode = pthread_mutex_lock(&tog_mutex);
2045 err = got_error_set_errno(errcode,
2046 "pthread_mutex_lock");
2048 } else if (*a->quit)
2050 else if (*a->first_displayed_entry == NULL) {
2051 *a->first_displayed_entry =
2052 TAILQ_FIRST(&a->commits->head);
2053 *a->selected_entry = *a->first_displayed_entry;
2056 errcode = pthread_cond_signal(&a->commit_loaded);
2058 err = got_error_set_errno(errcode,
2059 "pthread_cond_signal");
2060 pthread_mutex_unlock(&tog_mutex);
2065 a->commits_needed = 0;
2067 if (a->commits_needed == 0) {
2068 errcode = pthread_cond_wait(&a->need_commits,
2071 err = got_error_set_errno(errcode,
2072 "pthread_cond_wait");
2078 errcode = pthread_mutex_unlock(&tog_mutex);
2079 if (errcode && err == NULL)
2080 err = got_error_set_errno(errcode,
2081 "pthread_mutex_unlock");
2083 a->log_complete = 1;
2087 static const struct got_error *
2088 stop_log_thread(struct tog_log_view_state *s)
2090 const struct got_error *err = NULL;
2095 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2097 return got_error_set_errno(errcode,
2098 "pthread_cond_signal");
2099 errcode = pthread_mutex_unlock(&tog_mutex);
2101 return got_error_set_errno(errcode,
2102 "pthread_mutex_unlock");
2103 errcode = pthread_join(s->thread, (void **)&err);
2105 return got_error_set_errno(errcode, "pthread_join");
2106 errcode = pthread_mutex_lock(&tog_mutex);
2108 return got_error_set_errno(errcode,
2109 "pthread_mutex_lock");
2113 if (s->thread_args.repo) {
2114 err = got_repo_close(s->thread_args.repo);
2115 s->thread_args.repo = NULL;
2118 if (s->thread_args.graph) {
2119 got_commit_graph_close(s->thread_args.graph);
2120 s->thread_args.graph = NULL;
2126 static const struct got_error *
2127 close_log_view(struct tog_view *view)
2129 const struct got_error *err = NULL;
2130 struct tog_log_view_state *s = &view->state.log;
2133 err = stop_log_thread(s);
2135 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2136 if (errcode && err == NULL)
2137 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2139 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2140 if (errcode && err == NULL)
2141 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2143 free_commits(&s->commits);
2144 free(s->in_repo_path);
2145 s->in_repo_path = NULL;
2148 free(s->head_ref_name);
2149 s->head_ref_name = NULL;
2153 static const struct got_error *
2154 search_start_log_view(struct tog_view *view)
2156 struct tog_log_view_state *s = &view->state.log;
2158 s->matched_entry = NULL;
2159 s->search_entry = NULL;
2163 static const struct got_error *
2164 search_next_log_view(struct tog_view *view)
2166 const struct got_error *err = NULL;
2167 struct tog_log_view_state *s = &view->state.log;
2168 struct commit_queue_entry *entry;
2170 /* Display progress update in log view. */
2171 show_log_view(view);
2175 if (s->search_entry) {
2177 errcode = pthread_mutex_unlock(&tog_mutex);
2179 return got_error_set_errno(errcode,
2180 "pthread_mutex_unlock");
2181 ch = wgetch(view->window);
2182 errcode = pthread_mutex_lock(&tog_mutex);
2184 return got_error_set_errno(errcode,
2185 "pthread_mutex_lock");
2186 if (ch == KEY_BACKSPACE) {
2187 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2190 if (view->searching == TOG_SEARCH_FORWARD)
2191 entry = TAILQ_NEXT(s->search_entry, entry);
2193 entry = TAILQ_PREV(s->search_entry,
2194 commit_queue_head, entry);
2195 } else if (s->matched_entry) {
2196 if (view->searching == TOG_SEARCH_FORWARD)
2197 entry = TAILQ_NEXT(s->matched_entry, entry);
2199 entry = TAILQ_PREV(s->matched_entry,
2200 commit_queue_head, entry);
2202 if (view->searching == TOG_SEARCH_FORWARD)
2203 entry = TAILQ_FIRST(&s->commits.head);
2205 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2211 if (entry == NULL) {
2212 if (s->thread_args.log_complete ||
2213 view->searching == TOG_SEARCH_BACKWARD) {
2214 view->search_next_done =
2215 (s->matched_entry == NULL ?
2216 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2217 s->search_entry = NULL;
2221 * Poke the log thread for more commits and return,
2222 * allowing the main loop to make progress. Search
2223 * will resume at s->search_entry once we come back.
2225 s->thread_args.commits_needed++;
2226 return trigger_log_thread(view, 0);
2229 err = match_commit(&have_match, entry->id, entry->commit,
2234 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2235 s->matched_entry = entry;
2239 s->search_entry = entry;
2240 if (view->searching == TOG_SEARCH_FORWARD)
2241 entry = TAILQ_NEXT(entry, entry);
2243 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2246 if (s->matched_entry) {
2247 int cur = s->selected_entry->idx;
2248 while (cur < s->matched_entry->idx) {
2249 err = input_log_view(NULL, view, KEY_DOWN);
2254 while (cur > s->matched_entry->idx) {
2255 err = input_log_view(NULL, view, KEY_UP);
2262 s->search_entry = NULL;
2267 static const struct got_error *
2268 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2269 struct got_repository *repo, const char *head_ref_name,
2270 const char *in_repo_path, int log_branches)
2272 const struct got_error *err = NULL;
2273 struct tog_log_view_state *s = &view->state.log;
2274 struct got_repository *thread_repo = NULL;
2275 struct got_commit_graph *thread_graph = NULL;
2278 if (in_repo_path != s->in_repo_path) {
2279 free(s->in_repo_path);
2280 s->in_repo_path = strdup(in_repo_path);
2281 if (s->in_repo_path == NULL)
2282 return got_error_from_errno("strdup");
2285 /* The commit queue only contains commits being displayed. */
2286 TAILQ_INIT(&s->commits.head);
2287 s->commits.ncommits = 0;
2290 if (head_ref_name) {
2291 s->head_ref_name = strdup(head_ref_name);
2292 if (s->head_ref_name == NULL) {
2293 err = got_error_from_errno("strdup");
2297 s->start_id = got_object_id_dup(start_id);
2298 if (s->start_id == NULL) {
2299 err = got_error_from_errno("got_object_id_dup");
2302 s->log_branches = log_branches;
2304 STAILQ_INIT(&s->colors);
2305 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2306 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2307 get_color_value("TOG_COLOR_COMMIT"));
2310 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2311 get_color_value("TOG_COLOR_AUTHOR"));
2313 free_colors(&s->colors);
2316 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2317 get_color_value("TOG_COLOR_DATE"));
2319 free_colors(&s->colors);
2324 view->show = show_log_view;
2325 view->input = input_log_view;
2326 view->close = close_log_view;
2327 view->search_start = search_start_log_view;
2328 view->search_next = search_next_log_view;
2330 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2333 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2337 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2338 s->repo, NULL, NULL);
2342 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2344 err = got_error_set_errno(errcode, "pthread_cond_init");
2347 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2349 err = got_error_set_errno(errcode, "pthread_cond_init");
2353 s->thread_args.commits_needed = view->nlines;
2354 s->thread_args.graph = thread_graph;
2355 s->thread_args.commits = &s->commits;
2356 s->thread_args.in_repo_path = s->in_repo_path;
2357 s->thread_args.start_id = s->start_id;
2358 s->thread_args.repo = thread_repo;
2359 s->thread_args.log_complete = 0;
2360 s->thread_args.quit = &s->quit;
2361 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2362 s->thread_args.selected_entry = &s->selected_entry;
2363 s->thread_args.searching = &view->searching;
2364 s->thread_args.search_next_done = &view->search_next_done;
2365 s->thread_args.regex = &view->regex;
2368 close_log_view(view);
2372 static const struct got_error *
2373 show_log_view(struct tog_view *view)
2375 const struct got_error *err;
2376 struct tog_log_view_state *s = &view->state.log;
2378 if (s->thread == NULL) {
2379 int errcode = pthread_create(&s->thread, NULL, log_thread,
2382 return got_error_set_errno(errcode, "pthread_create");
2383 if (s->thread_args.commits_needed > 0) {
2384 err = trigger_log_thread(view, 1);
2390 return draw_commits(view);
2393 static const struct got_error *
2394 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2396 const struct got_error *err = NULL;
2397 struct tog_log_view_state *s = &view->state.log;
2398 struct tog_view *diff_view = NULL, *tree_view = NULL;
2399 struct tog_view *ref_view = NULL;
2410 if (s->first_displayed_entry == NULL)
2412 if (s->selected > 0)
2415 log_scroll_up(s, 1);
2420 if (s->first_displayed_entry == NULL)
2422 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2425 log_scroll_up(s, view->nlines - 1);
2432 if (s->first_displayed_entry == NULL)
2434 if (s->selected < MIN(view->nlines - 2,
2435 s->commits.ncommits - 1))
2438 err = log_scroll_down(view, 1);
2446 struct commit_queue_entry *first;
2447 first = s->first_displayed_entry;
2450 err = log_scroll_down(view, view->nlines - 1);
2453 if (first == s->first_displayed_entry &&
2454 s->selected < MIN(view->nlines - 2,
2455 s->commits.ncommits - 1)) {
2456 /* can't scroll further down */
2457 s->selected = MIN(view->nlines - 2,
2458 s->commits.ncommits - 1);
2464 if (s->selected > view->nlines - 2)
2465 s->selected = view->nlines - 2;
2466 if (s->selected > s->commits.ncommits - 1)
2467 s->selected = s->commits.ncommits - 1;
2469 if (s->commits.ncommits < view->nlines - 1 &&
2470 !s->thread_args.log_complete) {
2471 s->thread_args.commits_needed += (view->nlines - 1) -
2472 s->commits.ncommits;
2473 err = trigger_log_thread(view, 1);
2479 if (s->selected_entry == NULL)
2481 if (view_is_parent_view(view))
2482 begin_x = view_split_begin_x(view->begin_x);
2483 err = open_diff_view_for_commit(&diff_view, begin_x,
2484 s->selected_entry->commit, s->selected_entry->id,
2489 diff_view->focussed = 1;
2490 if (view_is_parent_view(view)) {
2491 err = view_close_child(view);
2494 view_set_child(view, diff_view);
2495 view->focus_child = 1;
2497 *new_view = diff_view;
2500 if (s->selected_entry == NULL)
2502 if (view_is_parent_view(view))
2503 begin_x = view_split_begin_x(view->begin_x);
2504 err = browse_commit_tree(&tree_view, begin_x,
2505 s->selected_entry, s->in_repo_path, s->head_ref_name,
2510 tree_view->focussed = 1;
2511 if (view_is_parent_view(view)) {
2512 err = view_close_child(view);
2515 view_set_child(view, tree_view);
2516 view->focus_child = 1;
2518 *new_view = tree_view;
2523 if (ch == KEY_BACKSPACE &&
2524 got_path_is_root_dir(s->in_repo_path))
2526 err = stop_log_thread(s);
2529 if (ch == KEY_BACKSPACE) {
2531 err = got_path_dirname(&parent_path, s->in_repo_path);
2534 free(s->in_repo_path);
2535 s->in_repo_path = parent_path;
2536 s->thread_args.in_repo_path = s->in_repo_path;
2537 } else if (ch == CTRL('l')) {
2538 struct got_object_id *start_id;
2539 err = got_repo_match_object_id(&start_id, NULL,
2540 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2541 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2545 s->start_id = start_id;
2546 s->thread_args.start_id = s->start_id;
2548 s->log_branches = !s->log_branches;
2550 err = got_repo_open(&s->thread_args.repo,
2551 got_repo_get_path(s->repo), NULL);
2555 err = tog_load_refs(s->repo);
2558 err = got_commit_graph_open(&s->thread_args.graph,
2559 s->in_repo_path, !s->log_branches);
2562 err = got_commit_graph_iter_start(s->thread_args.graph,
2563 s->start_id, s->repo, NULL, NULL);
2566 free_commits(&s->commits);
2567 s->first_displayed_entry = NULL;
2568 s->last_displayed_entry = NULL;
2569 s->selected_entry = NULL;
2571 s->thread_args.log_complete = 0;
2573 s->thread_args.commits_needed = view->nlines;
2576 if (view_is_parent_view(view))
2577 begin_x = view_split_begin_x(view->begin_x);
2578 ref_view = view_open(view->nlines, view->ncols,
2579 view->begin_y, begin_x, TOG_VIEW_REF);
2580 if (ref_view == NULL)
2581 return got_error_from_errno("view_open");
2582 err = open_ref_view(ref_view, s->repo);
2584 view_close(ref_view);
2588 ref_view->focussed = 1;
2589 if (view_is_parent_view(view)) {
2590 err = view_close_child(view);
2593 view_set_child(view, ref_view);
2594 view->focus_child = 1;
2596 *new_view = ref_view;
2605 static const struct got_error *
2606 apply_unveil(const char *repo_path, const char *worktree_path)
2608 const struct got_error *error;
2611 if (unveil("gmon.out", "rwc") != 0)
2612 return got_error_from_errno2("unveil", "gmon.out");
2614 if (repo_path && unveil(repo_path, "r") != 0)
2615 return got_error_from_errno2("unveil", repo_path);
2617 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2618 return got_error_from_errno2("unveil", worktree_path);
2620 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2621 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2623 error = got_privsep_unveil_exec_helpers();
2627 if (unveil(NULL, NULL) != 0)
2628 return got_error_from_errno("unveil");
2638 halfdelay(1); /* Do fast refresh while initial view is loading. */
2641 intrflush(stdscr, FALSE);
2642 keypad(stdscr, TRUE);
2644 if (getenv("TOG_COLORS") != NULL) {
2646 use_default_colors();
2648 signal(SIGWINCH, tog_sigwinch);
2649 signal(SIGPIPE, tog_sigpipe);
2650 signal(SIGCONT, tog_sigcont);
2653 static const struct got_error *
2654 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2655 struct got_repository *repo, struct got_worktree *worktree)
2657 const struct got_error *err = NULL;
2660 *in_repo_path = strdup("/");
2661 if (*in_repo_path == NULL)
2662 return got_error_from_errno("strdup");
2667 const char *prefix = got_worktree_get_path_prefix(worktree);
2670 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2673 if (asprintf(in_repo_path, "%s%s%s", prefix,
2674 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2676 err = got_error_from_errno("asprintf");
2677 *in_repo_path = NULL;
2681 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2686 static const struct got_error *
2687 cmd_log(int argc, char *argv[])
2689 const struct got_error *error;
2690 struct got_repository *repo = NULL;
2691 struct got_worktree *worktree = NULL;
2692 struct got_object_id *start_id = NULL;
2693 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2694 char *start_commit = NULL, *label = NULL;
2695 struct got_reference *ref = NULL;
2696 const char *head_ref_name = NULL;
2697 int ch, log_branches = 0;
2698 struct tog_view *view;
2700 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2706 start_commit = optarg;
2709 repo_path = realpath(optarg, NULL);
2710 if (repo_path == NULL)
2711 return got_error_from_errno2("realpath",
2726 if (repo_path == NULL) {
2727 cwd = getcwd(NULL, 0);
2729 return got_error_from_errno("getcwd");
2730 error = got_worktree_open(&worktree, cwd);
2731 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2735 strdup(got_worktree_get_repo_path(worktree));
2737 repo_path = strdup(cwd);
2738 if (repo_path == NULL) {
2739 error = got_error_from_errno("strdup");
2744 error = got_repo_open(&repo, repo_path, NULL);
2748 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2755 error = apply_unveil(got_repo_get_path(repo),
2756 worktree ? got_worktree_get_root_path(worktree) : NULL);
2760 /* already loaded by tog_log_with_path()? */
2761 if (TAILQ_EMPTY(&tog_refs)) {
2762 error = tog_load_refs(repo);
2767 if (start_commit == NULL) {
2768 error = got_repo_match_object_id(&start_id, &label,
2769 worktree ? got_worktree_get_head_ref_name(worktree) :
2770 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2773 head_ref_name = label;
2775 error = got_ref_open(&ref, repo, start_commit, 0);
2777 head_ref_name = got_ref_get_name(ref);
2778 else if (error->code != GOT_ERR_NOT_REF)
2780 error = got_repo_match_object_id(&start_id, NULL,
2781 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2786 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2788 error = got_error_from_errno("view_open");
2791 error = open_log_view(view, start_id, repo, head_ref_name,
2792 in_repo_path, log_branches);
2796 /* Release work tree lock. */
2797 got_worktree_close(worktree);
2800 error = view_loop(view);
2810 const struct got_error *close_err = got_repo_close(repo);
2815 got_worktree_close(worktree);
2824 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2825 "[-w] object1 object2\n", getprogname());
2830 match_line(const char *line, regex_t *regex, size_t nmatch,
2831 regmatch_t *regmatch)
2833 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2837 match_color(struct tog_colors *colors, const char *line)
2839 struct tog_color *tc = NULL;
2841 STAILQ_FOREACH(tc, colors, entry) {
2842 if (match_line(line, &tc->regex, 0, NULL))
2849 static const struct got_error *
2850 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2851 WINDOW *window, regmatch_t *regmatch)
2853 const struct got_error *err = NULL;
2860 s = strndup(line, regmatch->rm_so);
2862 return got_error_from_errno("strndup");
2864 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2869 waddwstr(window, wline);
2876 s = strndup(line + regmatch->rm_so,
2877 regmatch->rm_eo - regmatch->rm_so);
2879 err = got_error_from_errno("strndup");
2883 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2888 wattr_on(window, A_STANDOUT, NULL);
2889 waddwstr(window, wline);
2890 wattr_off(window, A_STANDOUT, NULL);
2897 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2898 err = format_line(&wline, &width,
2899 line + regmatch->rm_eo, wlimit, col_tab_align);
2902 waddwstr(window, wline);
2910 static const struct got_error *
2911 draw_file(struct tog_view *view, const char *header)
2913 struct tog_diff_view_state *s = &view->state.diff;
2914 regmatch_t *regmatch = &view->regmatch;
2915 const struct got_error *err;
2918 size_t linesize = 0;
2920 struct tog_color *tc;
2923 int max_lines = view->nlines;
2924 int nlines = s->nlines;
2927 line_offset = s->line_offsets[s->first_displayed_line - 1];
2928 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2929 return got_error_from_errno("fseek");
2931 werase(view->window);
2934 if (asprintf(&line, "[%d/%d] %s",
2935 s->first_displayed_line - 1 + s->selected_line, nlines,
2937 return got_error_from_errno("asprintf");
2938 err = format_line(&wline, &width, line, view->ncols, 0);
2943 if (view_needs_focus_indication(view))
2944 wstandout(view->window);
2945 waddwstr(view->window, wline);
2948 if (view_needs_focus_indication(view))
2949 wstandend(view->window);
2950 if (width <= view->ncols - 1)
2951 waddch(view->window, '\n');
2960 while (max_lines > 0 && nprinted < max_lines) {
2961 linelen = getline(&line, &linesize, s->f);
2962 if (linelen == -1) {
2968 return got_ferror(s->f, GOT_ERR_IO);
2971 tc = match_color(&s->colors, line);
2973 wattr_on(view->window,
2974 COLOR_PAIR(tc->colorpair), NULL);
2975 if (s->first_displayed_line + nprinted == s->matched_line &&
2976 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2977 err = add_matched_line(&width, line, view->ncols, 0,
2978 view->window, regmatch);
2984 err = format_line(&wline, &width, line, view->ncols, 0);
2989 waddwstr(view->window, wline);
2994 wattr_off(view->window,
2995 COLOR_PAIR(tc->colorpair), NULL);
2996 if (width <= view->ncols - 1)
2997 waddch(view->window, '\n');
3002 s->last_displayed_line = s->first_displayed_line +
3005 s->last_displayed_line = s->first_displayed_line;
3010 while (nprinted < view->nlines) {
3011 waddch(view->window, '\n');
3015 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3020 wstandout(view->window);
3021 waddwstr(view->window, wline);
3024 wstandend(view->window);
3031 get_datestr(time_t *time, char *datebuf)
3033 struct tm mytm, *tm;
3036 tm = gmtime_r(time, &mytm);
3039 s = asctime_r(tm, datebuf);
3042 p = strchr(s, '\n');
3048 static const struct got_error *
3049 get_changed_paths(struct got_pathlist_head *paths,
3050 struct got_commit_object *commit, struct got_repository *repo)
3052 const struct got_error *err = NULL;
3053 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3054 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3055 struct got_object_qid *qid;
3057 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3059 struct got_commit_object *pcommit;
3060 err = got_object_open_as_commit(&pcommit, repo,
3065 tree_id1 = got_object_commit_get_tree_id(pcommit);
3066 got_object_commit_close(pcommit);
3071 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3076 tree_id2 = got_object_commit_get_tree_id(commit);
3077 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3081 err = got_diff_tree(tree1, tree2, "", "", repo,
3082 got_diff_tree_collect_changed_paths, paths, 0);
3085 got_object_tree_close(tree1);
3087 got_object_tree_close(tree2);
3091 static const struct got_error *
3092 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3096 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3098 return got_error_from_errno("reallocarray");
3100 (*line_offsets)[*nlines] = off;
3105 static const struct got_error *
3106 write_commit_info(off_t **line_offsets, size_t *nlines,
3107 struct got_object_id *commit_id, struct got_reflist_head *refs,
3108 struct got_repository *repo, FILE *outfile)
3110 const struct got_error *err = NULL;
3111 char datebuf[26], *datestr;
3112 struct got_commit_object *commit;
3113 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3114 time_t committer_time;
3115 const char *author, *committer;
3116 char *refs_str = NULL;
3117 struct got_pathlist_head changed_paths;
3118 struct got_pathlist_entry *pe;
3122 TAILQ_INIT(&changed_paths);
3125 err = build_refs_str(&refs_str, refs, commit_id, repo);
3130 err = got_object_open_as_commit(&commit, repo, commit_id);
3134 err = got_object_id_str(&id_str, commit_id);
3136 err = got_error_from_errno("got_object_id_str");
3140 err = add_line_offset(line_offsets, nlines, 0);
3144 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3145 refs_str ? refs_str : "", refs_str ? ")" : "");
3147 err = got_error_from_errno("fprintf");
3151 err = add_line_offset(line_offsets, nlines, outoff);
3155 n = fprintf(outfile, "from: %s\n",
3156 got_object_commit_get_author(commit));
3158 err = got_error_from_errno("fprintf");
3162 err = add_line_offset(line_offsets, nlines, outoff);
3166 committer_time = got_object_commit_get_committer_time(commit);
3167 datestr = get_datestr(&committer_time, datebuf);
3169 n = fprintf(outfile, "date: %s UTC\n", datestr);
3171 err = got_error_from_errno("fprintf");
3175 err = add_line_offset(line_offsets, nlines, outoff);
3179 author = got_object_commit_get_author(commit);
3180 committer = got_object_commit_get_committer(commit);
3181 if (strcmp(author, committer) != 0) {
3182 n = fprintf(outfile, "via: %s\n", committer);
3184 err = got_error_from_errno("fprintf");
3188 err = add_line_offset(line_offsets, nlines, outoff);
3192 err = got_object_commit_get_logmsg(&logmsg, commit);
3196 while ((line = strsep(&s, "\n")) != NULL) {
3197 n = fprintf(outfile, "%s\n", line);
3199 err = got_error_from_errno("fprintf");
3203 err = add_line_offset(line_offsets, nlines, outoff);
3208 err = get_changed_paths(&changed_paths, commit, repo);
3211 TAILQ_FOREACH(pe, &changed_paths, entry) {
3212 struct got_diff_changed_path *cp = pe->data;
3213 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3215 err = got_error_from_errno("fprintf");
3219 err = add_line_offset(line_offsets, nlines, outoff);
3222 free((char *)pe->path);
3226 fputc('\n', outfile);
3228 err = add_line_offset(line_offsets, nlines, outoff);
3230 got_pathlist_free(&changed_paths);
3234 got_object_commit_close(commit);
3236 free(*line_offsets);
3237 *line_offsets = NULL;
3243 static const struct got_error *
3244 create_diff(struct tog_diff_view_state *s)
3246 const struct got_error *err = NULL;
3250 free(s->line_offsets);
3251 s->line_offsets = malloc(sizeof(off_t));
3252 if (s->line_offsets == NULL)
3253 return got_error_from_errno("malloc");
3258 err = got_error_from_errno("got_opentemp");
3261 if (s->f && fclose(s->f) == EOF) {
3262 err = got_error_from_errno("fclose");
3268 err = got_object_get_type(&obj_type, s->repo, s->id1);
3270 err = got_object_get_type(&obj_type, s->repo, s->id2);
3275 case GOT_OBJ_TYPE_BLOB:
3276 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3277 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3278 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3280 case GOT_OBJ_TYPE_TREE:
3281 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3282 s->id1, s->id2, "", "", s->diff_context,
3283 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3285 case GOT_OBJ_TYPE_COMMIT: {
3286 const struct got_object_id_queue *parent_ids;
3287 struct got_object_qid *pid;
3288 struct got_commit_object *commit2;
3289 struct got_reflist_head *refs;
3291 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3294 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3295 /* Show commit info if we're diffing to a parent/root commit. */
3296 if (s->id1 == NULL) {
3297 err = write_commit_info(&s->line_offsets, &s->nlines,
3298 s->id2, refs, s->repo, s->f);
3302 parent_ids = got_object_commit_get_parent_ids(commit2);
3303 STAILQ_FOREACH(pid, parent_ids, entry) {
3304 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3305 err = write_commit_info(
3306 &s->line_offsets, &s->nlines,
3307 s->id2, refs, s->repo, s->f);
3314 got_object_commit_close(commit2);
3316 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3317 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3318 s->force_text_diff, s->repo, s->f);
3322 err = got_error(GOT_ERR_OBJ_TYPE);
3328 if (s->f && fflush(s->f) != 0 && err == NULL)
3329 err = got_error_from_errno("fflush");
3334 diff_view_indicate_progress(struct tog_view *view)
3336 mvwaddstr(view->window, 0, 0, "diffing...");
3341 static const struct got_error *
3342 search_start_diff_view(struct tog_view *view)
3344 struct tog_diff_view_state *s = &view->state.diff;
3346 s->matched_line = 0;
3350 static const struct got_error *
3351 search_next_diff_view(struct tog_view *view)
3353 struct tog_diff_view_state *s = &view->state.diff;
3356 size_t linesize = 0;
3359 if (!view->searching) {
3360 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3364 if (s->matched_line) {
3365 if (view->searching == TOG_SEARCH_FORWARD)
3366 lineno = s->matched_line + 1;
3368 lineno = s->matched_line - 1;
3370 if (view->searching == TOG_SEARCH_FORWARD)
3379 if (lineno <= 0 || lineno > s->nlines) {
3380 if (s->matched_line == 0) {
3381 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3385 if (view->searching == TOG_SEARCH_FORWARD)
3391 offset = s->line_offsets[lineno - 1];
3392 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3394 return got_error_from_errno("fseeko");
3396 linelen = getline(&line, &linesize, s->f);
3397 if (linelen != -1 &&
3398 match_line(line, &view->regex, 1, &view->regmatch)) {
3399 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3400 s->matched_line = lineno;
3403 if (view->searching == TOG_SEARCH_FORWARD)
3410 if (s->matched_line) {
3411 s->first_displayed_line = s->matched_line;
3412 s->selected_line = 1;
3418 static const struct got_error *
3419 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3420 struct got_object_id *id2, const char *label1, const char *label2,
3421 int diff_context, int ignore_whitespace, int force_text_diff,
3422 struct tog_view *log_view, struct got_repository *repo)
3424 const struct got_error *err;
3425 struct tog_diff_view_state *s = &view->state.diff;
3427 if (id1 != NULL && id2 != NULL) {
3429 err = got_object_get_type(&type1, repo, id1);
3432 err = got_object_get_type(&type2, repo, id2);
3437 return got_error(GOT_ERR_OBJ_TYPE);
3439 s->first_displayed_line = 1;
3440 s->last_displayed_line = view->nlines;
3441 s->selected_line = 1;
3449 s->id1 = got_object_id_dup(id1);
3451 return got_error_from_errno("got_object_id_dup");
3455 s->id2 = got_object_id_dup(id2);
3456 if (s->id2 == NULL) {
3459 return got_error_from_errno("got_object_id_dup");
3462 s->first_displayed_line = 1;
3463 s->last_displayed_line = view->nlines;
3464 s->diff_context = diff_context;
3465 s->ignore_whitespace = ignore_whitespace;
3466 s->force_text_diff = force_text_diff;
3467 s->log_view = log_view;
3470 STAILQ_INIT(&s->colors);
3471 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3472 err = add_color(&s->colors,
3473 "^-", TOG_COLOR_DIFF_MINUS,
3474 get_color_value("TOG_COLOR_DIFF_MINUS"));
3477 err = add_color(&s->colors, "^\\+",
3478 TOG_COLOR_DIFF_PLUS,
3479 get_color_value("TOG_COLOR_DIFF_PLUS"));
3481 free_colors(&s->colors);
3484 err = add_color(&s->colors,
3485 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3486 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3488 free_colors(&s->colors);
3492 err = add_color(&s->colors,
3493 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3494 TOG_COLOR_DIFF_META,
3495 get_color_value("TOG_COLOR_DIFF_META"));
3497 free_colors(&s->colors);
3501 err = add_color(&s->colors,
3502 "^(from|via): ", TOG_COLOR_AUTHOR,
3503 get_color_value("TOG_COLOR_AUTHOR"));
3505 free_colors(&s->colors);
3509 err = add_color(&s->colors,
3510 "^date: ", TOG_COLOR_DATE,
3511 get_color_value("TOG_COLOR_DATE"));
3513 free_colors(&s->colors);
3518 if (log_view && view_is_splitscreen(view))
3519 show_log_view(log_view); /* draw vborder */
3520 diff_view_indicate_progress(view);
3522 s->line_offsets = NULL;
3524 err = create_diff(s);
3530 free_colors(&s->colors);
3534 view->show = show_diff_view;
3535 view->input = input_diff_view;
3536 view->close = close_diff_view;
3537 view->search_start = search_start_diff_view;
3538 view->search_next = search_next_diff_view;
3543 static const struct got_error *
3544 close_diff_view(struct tog_view *view)
3546 const struct got_error *err = NULL;
3547 struct tog_diff_view_state *s = &view->state.diff;
3553 if (s->f && fclose(s->f) == EOF)
3554 err = got_error_from_errno("fclose");
3555 free_colors(&s->colors);
3556 free(s->line_offsets);
3557 s->line_offsets = NULL;
3562 static const struct got_error *
3563 show_diff_view(struct tog_view *view)
3565 const struct got_error *err;
3566 struct tog_diff_view_state *s = &view->state.diff;
3567 char *id_str1 = NULL, *id_str2, *header;
3568 const char *label1, *label2;
3571 err = got_object_id_str(&id_str1, s->id1);
3574 label1 = s->label1 ? : id_str1;
3576 label1 = "/dev/null";
3578 err = got_object_id_str(&id_str2, s->id2);
3581 label2 = s->label2 ? : id_str2;
3583 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3584 err = got_error_from_errno("asprintf");
3592 return draw_file(view, header);
3595 static const struct got_error *
3596 set_selected_commit(struct tog_diff_view_state *s,
3597 struct commit_queue_entry *entry)
3599 const struct got_error *err;
3600 const struct got_object_id_queue *parent_ids;
3601 struct got_commit_object *selected_commit;
3602 struct got_object_qid *pid;
3605 s->id2 = got_object_id_dup(entry->id);
3607 return got_error_from_errno("got_object_id_dup");
3609 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3612 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3614 pid = STAILQ_FIRST(parent_ids);
3615 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3616 got_object_commit_close(selected_commit);
3620 static const struct got_error *
3621 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3623 const struct got_error *err = NULL;
3624 struct tog_diff_view_state *s = &view->state.diff;
3625 struct tog_log_view_state *ls;
3626 struct commit_queue_entry *old_selected_entry;
3628 size_t linesize = 0;
3636 s->force_text_diff = !s->force_text_diff;
3638 s->ignore_whitespace = !s->ignore_whitespace;
3639 wclear(view->window);
3640 s->first_displayed_line = 1;
3641 s->last_displayed_line = view->nlines;
3642 diff_view_indicate_progress(view);
3643 err = create_diff(s);
3647 if (s->first_displayed_line > 1)
3648 s->first_displayed_line--;
3652 if (s->first_displayed_line == 1)
3655 while (i++ < view->nlines - 1 &&
3656 s->first_displayed_line > 1)
3657 s->first_displayed_line--;
3662 s->first_displayed_line++;
3670 while (!s->eof && i++ < view->nlines - 1) {
3671 linelen = getline(&line, &linesize, s->f);
3672 s->first_displayed_line++;
3673 if (linelen == -1) {
3677 err = got_ferror(s->f, GOT_ERR_IO);
3684 if (s->diff_context > 0) {
3686 diff_view_indicate_progress(view);
3687 err = create_diff(s);
3688 if (s->first_displayed_line + view->nlines - 1 >
3690 s->first_displayed_line = 1;
3691 s->last_displayed_line = view->nlines;
3696 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3698 diff_view_indicate_progress(view);
3699 err = create_diff(s);
3704 if (s->log_view == NULL)
3706 ls = &s->log_view->state.log;
3707 old_selected_entry = ls->selected_entry;
3709 err = input_log_view(NULL, s->log_view, KEY_UP);
3713 if (old_selected_entry == ls->selected_entry)
3716 err = set_selected_commit(s, ls->selected_entry);
3720 s->first_displayed_line = 1;
3721 s->last_displayed_line = view->nlines;
3723 diff_view_indicate_progress(view);
3724 err = create_diff(s);
3728 if (s->log_view == NULL)
3730 ls = &s->log_view->state.log;
3731 old_selected_entry = ls->selected_entry;
3733 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3737 if (old_selected_entry == ls->selected_entry)
3740 err = set_selected_commit(s, ls->selected_entry);
3744 s->first_displayed_line = 1;
3745 s->last_displayed_line = view->nlines;
3747 diff_view_indicate_progress(view);
3748 err = create_diff(s);
3757 static const struct got_error *
3758 cmd_diff(int argc, char *argv[])
3760 const struct got_error *error = NULL;
3761 struct got_repository *repo = NULL;
3762 struct got_worktree *worktree = NULL;
3763 struct got_object_id *id1 = NULL, *id2 = NULL;
3764 char *repo_path = NULL, *cwd = NULL;
3765 char *id_str1 = NULL, *id_str2 = NULL;
3766 char *label1 = NULL, *label2 = NULL;
3767 int diff_context = 3, ignore_whitespace = 0;
3768 int ch, force_text_diff = 0;
3770 struct tog_view *view;
3772 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3775 force_text_diff = 1;
3778 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3781 err(1, "-C option %s", errstr);
3784 repo_path = realpath(optarg, NULL);
3785 if (repo_path == NULL)
3786 return got_error_from_errno2("realpath",
3788 got_path_strip_trailing_slashes(repo_path);
3791 ignore_whitespace = 1;
3803 usage_diff(); /* TODO show local worktree changes */
3804 } else if (argc == 2) {
3810 if (repo_path == NULL) {
3811 cwd = getcwd(NULL, 0);
3813 return got_error_from_errno("getcwd");
3814 error = got_worktree_open(&worktree, cwd);
3815 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3819 strdup(got_worktree_get_repo_path(worktree));
3821 repo_path = strdup(cwd);
3822 if (repo_path == NULL) {
3823 error = got_error_from_errno("strdup");
3828 error = got_repo_open(&repo, repo_path, NULL);
3834 error = apply_unveil(got_repo_get_path(repo), NULL);
3838 error = tog_load_refs(repo);
3842 error = got_repo_match_object_id(&id1, &label1, id_str1,
3843 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3847 error = got_repo_match_object_id(&id2, &label2, id_str2,
3848 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3852 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3854 error = got_error_from_errno("view_open");
3857 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3858 ignore_whitespace, force_text_diff, NULL, repo);
3861 error = view_loop(view);
3868 const struct got_error *close_err = got_repo_close(repo);
3873 got_worktree_close(worktree);
3882 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3887 struct tog_blame_line {
3889 struct got_object_id *id;
3892 static const struct got_error *
3893 draw_blame(struct tog_view *view)
3895 struct tog_blame_view_state *s = &view->state.blame;
3896 struct tog_blame *blame = &s->blame;
3897 regmatch_t *regmatch = &view->regmatch;
3898 const struct got_error *err;
3899 int lineno = 0, nprinted = 0;
3901 size_t linesize = 0;
3905 struct tog_blame_line *blame_line;
3906 struct got_object_id *prev_id = NULL;
3908 struct tog_color *tc;
3910 err = got_object_id_str(&id_str, s->blamed_commit->id);
3915 werase(view->window);
3917 if (asprintf(&line, "commit %s", id_str) == -1) {
3918 err = got_error_from_errno("asprintf");
3923 err = format_line(&wline, &width, line, view->ncols, 0);
3928 if (view_needs_focus_indication(view))
3929 wstandout(view->window);
3930 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3932 wattr_on(view->window,
3933 COLOR_PAIR(tc->colorpair), NULL);
3934 waddwstr(view->window, wline);
3936 wattr_off(view->window,
3937 COLOR_PAIR(tc->colorpair), NULL);
3938 if (view_needs_focus_indication(view))
3939 wstandend(view->window);
3942 if (width < view->ncols - 1)
3943 waddch(view->window, '\n');
3945 if (asprintf(&line, "[%d/%d] %s%s",
3946 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3947 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3949 return got_error_from_errno("asprintf");
3952 err = format_line(&wline, &width, line, view->ncols, 0);
3957 waddwstr(view->window, wline);
3960 if (width < view->ncols - 1)
3961 waddch(view->window, '\n');
3964 while (nprinted < view->nlines - 2) {
3965 linelen = getline(&line, &linesize, blame->f);
3966 if (linelen == -1) {
3967 if (feof(blame->f)) {
3972 return got_ferror(blame->f, GOT_ERR_IO);
3974 if (++lineno < s->first_displayed_line)
3977 if (view->focussed && nprinted == s->selected_line - 1)
3978 wstandout(view->window);
3980 if (blame->nlines > 0) {
3981 blame_line = &blame->lines[lineno - 1];
3982 if (blame_line->annotated && prev_id &&
3983 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3985 nprinted == s->selected_line - 1)) {
3986 waddstr(view->window, " ");
3987 } else if (blame_line->annotated) {
3989 err = got_object_id_str(&id_str, blame_line->id);
3994 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3996 wattr_on(view->window,
3997 COLOR_PAIR(tc->colorpair), NULL);
3998 wprintw(view->window, "%.8s", id_str);
4000 wattr_off(view->window,
4001 COLOR_PAIR(tc->colorpair), NULL);
4003 prev_id = blame_line->id;
4005 waddstr(view->window, "........");
4009 waddstr(view->window, "........");
4013 if (view->focussed && nprinted == s->selected_line - 1)
4014 wstandend(view->window);
4015 waddstr(view->window, " ");
4017 if (view->ncols <= 9) {
4019 wline = wcsdup(L"");
4020 if (wline == NULL) {
4021 err = got_error_from_errno("wcsdup");
4025 } else if (s->first_displayed_line + nprinted ==
4027 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4028 err = add_matched_line(&width, line, view->ncols - 9, 9,
4029 view->window, regmatch);
4036 err = format_line(&wline, &width, line,
4037 view->ncols - 9, 9);
4038 waddwstr(view->window, wline);
4044 if (width <= view->ncols - 1)
4045 waddch(view->window, '\n');
4046 if (++nprinted == 1)
4047 s->first_displayed_line = lineno;
4050 s->last_displayed_line = lineno;
4057 static const struct got_error *
4058 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4060 const struct got_error *err = NULL;
4061 struct tog_blame_cb_args *a = arg;
4062 struct tog_blame_line *line;
4065 if (nlines != a->nlines ||
4066 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4067 return got_error(GOT_ERR_RANGE);
4069 errcode = pthread_mutex_lock(&tog_mutex);
4071 return got_error_set_errno(errcode, "pthread_mutex_lock");
4073 if (*a->quit) { /* user has quit the blame view */
4074 err = got_error(GOT_ERR_ITER_COMPLETED);
4079 goto done; /* no change in this commit */
4081 line = &a->lines[lineno - 1];
4082 if (line->annotated)
4085 line->id = got_object_id_dup(id);
4086 if (line->id == NULL) {
4087 err = got_error_from_errno("got_object_id_dup");
4090 line->annotated = 1;
4092 errcode = pthread_mutex_unlock(&tog_mutex);
4094 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4099 blame_thread(void *arg)
4101 const struct got_error *err, *close_err;
4102 struct tog_blame_thread_args *ta = arg;
4103 struct tog_blame_cb_args *a = ta->cb_args;
4106 err = block_signals_used_by_main_thread();
4110 err = got_blame(ta->path, a->commit_id, ta->repo,
4111 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4112 if (err && err->code == GOT_ERR_CANCELLED)
4115 errcode = pthread_mutex_lock(&tog_mutex);
4117 return (void *)got_error_set_errno(errcode,
4118 "pthread_mutex_lock");
4120 close_err = got_repo_close(ta->repo);
4126 errcode = pthread_mutex_unlock(&tog_mutex);
4127 if (errcode && err == NULL)
4128 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4133 static struct got_object_id *
4134 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4135 int first_displayed_line, int selected_line)
4137 struct tog_blame_line *line;
4142 line = &lines[first_displayed_line - 1 + selected_line - 1];
4143 if (!line->annotated)
4149 static const struct got_error *
4150 stop_blame(struct tog_blame *blame)
4152 const struct got_error *err = NULL;
4155 if (blame->thread) {
4157 errcode = pthread_mutex_unlock(&tog_mutex);
4159 return got_error_set_errno(errcode,
4160 "pthread_mutex_unlock");
4161 errcode = pthread_join(blame->thread, (void **)&err);
4163 return got_error_set_errno(errcode, "pthread_join");
4164 errcode = pthread_mutex_lock(&tog_mutex);
4166 return got_error_set_errno(errcode,
4167 "pthread_mutex_lock");
4168 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4170 blame->thread = NULL;
4172 if (blame->thread_args.repo) {
4173 const struct got_error *close_err;
4174 close_err = got_repo_close(blame->thread_args.repo);
4177 blame->thread_args.repo = NULL;
4180 if (fclose(blame->f) == EOF && err == NULL)
4181 err = got_error_from_errno("fclose");
4185 for (i = 0; i < blame->nlines; i++)
4186 free(blame->lines[i].id);
4188 blame->lines = NULL;
4190 free(blame->cb_args.commit_id);
4191 blame->cb_args.commit_id = NULL;
4196 static const struct got_error *
4197 cancel_blame_view(void *arg)
4199 const struct got_error *err = NULL;
4203 errcode = pthread_mutex_lock(&tog_mutex);
4205 return got_error_set_errno(errcode,
4206 "pthread_mutex_unlock");
4209 err = got_error(GOT_ERR_CANCELLED);
4211 errcode = pthread_mutex_unlock(&tog_mutex);
4213 return got_error_set_errno(errcode,
4214 "pthread_mutex_lock");
4219 static const struct got_error *
4220 run_blame(struct tog_view *view)
4222 struct tog_blame_view_state *s = &view->state.blame;
4223 struct tog_blame *blame = &s->blame;
4224 const struct got_error *err = NULL;
4225 struct got_blob_object *blob = NULL;
4226 struct got_repository *thread_repo = NULL;
4227 struct got_object_id *obj_id = NULL;
4230 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4235 err = got_object_get_type(&obj_type, s->repo, obj_id);
4239 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4240 err = got_error(GOT_ERR_OBJ_TYPE);
4244 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4247 blame->f = got_opentemp();
4248 if (blame->f == NULL) {
4249 err = got_error_from_errno("got_opentemp");
4252 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4253 &blame->line_offsets, blame->f, blob);
4256 if (blame->nlines == 0) {
4257 s->blame_complete = 1;
4261 /* Don't include \n at EOF in the blame line count. */
4262 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4265 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4266 if (blame->lines == NULL) {
4267 err = got_error_from_errno("calloc");
4271 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4275 blame->cb_args.view = view;
4276 blame->cb_args.lines = blame->lines;
4277 blame->cb_args.nlines = blame->nlines;
4278 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4279 if (blame->cb_args.commit_id == NULL) {
4280 err = got_error_from_errno("got_object_id_dup");
4283 blame->cb_args.quit = &s->done;
4285 blame->thread_args.path = s->path;
4286 blame->thread_args.repo = thread_repo;
4287 blame->thread_args.cb_args = &blame->cb_args;
4288 blame->thread_args.complete = &s->blame_complete;
4289 blame->thread_args.cancel_cb = cancel_blame_view;
4290 blame->thread_args.cancel_arg = &s->done;
4291 s->blame_complete = 0;
4293 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4294 s->first_displayed_line = 1;
4295 s->last_displayed_line = view->nlines;
4296 s->selected_line = 1;
4301 got_object_blob_close(blob);
4308 static const struct got_error *
4309 open_blame_view(struct tog_view *view, char *path,
4310 struct got_object_id *commit_id, struct got_repository *repo)
4312 const struct got_error *err = NULL;
4313 struct tog_blame_view_state *s = &view->state.blame;
4315 STAILQ_INIT(&s->blamed_commits);
4317 s->path = strdup(path);
4318 if (s->path == NULL)
4319 return got_error_from_errno("strdup");
4321 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4327 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4328 s->first_displayed_line = 1;
4329 s->last_displayed_line = view->nlines;
4330 s->selected_line = 1;
4331 s->blame_complete = 0;
4333 s->commit_id = commit_id;
4334 memset(&s->blame, 0, sizeof(s->blame));
4336 STAILQ_INIT(&s->colors);
4337 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4338 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4339 get_color_value("TOG_COLOR_COMMIT"));
4344 view->show = show_blame_view;
4345 view->input = input_blame_view;
4346 view->close = close_blame_view;
4347 view->search_start = search_start_blame_view;
4348 view->search_next = search_next_blame_view;
4350 return run_blame(view);
4353 static const struct got_error *
4354 close_blame_view(struct tog_view *view)
4356 const struct got_error *err = NULL;
4357 struct tog_blame_view_state *s = &view->state.blame;
4359 if (s->blame.thread)
4360 err = stop_blame(&s->blame);
4362 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4363 struct got_object_qid *blamed_commit;
4364 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4365 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4366 got_object_qid_free(blamed_commit);
4370 free_colors(&s->colors);
4375 static const struct got_error *
4376 search_start_blame_view(struct tog_view *view)
4378 struct tog_blame_view_state *s = &view->state.blame;
4380 s->matched_line = 0;
4384 static const struct got_error *
4385 search_next_blame_view(struct tog_view *view)
4387 struct tog_blame_view_state *s = &view->state.blame;
4390 size_t linesize = 0;
4393 if (!view->searching) {
4394 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4398 if (s->matched_line) {
4399 if (view->searching == TOG_SEARCH_FORWARD)
4400 lineno = s->matched_line + 1;
4402 lineno = s->matched_line - 1;
4404 if (view->searching == TOG_SEARCH_FORWARD)
4407 lineno = s->blame.nlines;
4413 if (lineno <= 0 || lineno > s->blame.nlines) {
4414 if (s->matched_line == 0) {
4415 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4419 if (view->searching == TOG_SEARCH_FORWARD)
4422 lineno = s->blame.nlines;
4425 offset = s->blame.line_offsets[lineno - 1];
4426 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4428 return got_error_from_errno("fseeko");
4430 linelen = getline(&line, &linesize, s->blame.f);
4431 if (linelen != -1 &&
4432 match_line(line, &view->regex, 1, &view->regmatch)) {
4433 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4434 s->matched_line = lineno;
4437 if (view->searching == TOG_SEARCH_FORWARD)
4444 if (s->matched_line) {
4445 s->first_displayed_line = s->matched_line;
4446 s->selected_line = 1;
4452 static const struct got_error *
4453 show_blame_view(struct tog_view *view)
4455 const struct got_error *err = NULL;
4456 struct tog_blame_view_state *s = &view->state.blame;
4459 if (s->blame.thread == NULL && !s->blame_complete) {
4460 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4461 &s->blame.thread_args);
4463 return got_error_set_errno(errcode, "pthread_create");
4465 halfdelay(1); /* fast refresh while annotating */
4468 if (s->blame_complete)
4469 halfdelay(10); /* disable fast refresh */
4471 err = draw_blame(view);
4477 static const struct got_error *
4478 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4480 const struct got_error *err = NULL, *thread_err = NULL;
4481 struct tog_view *diff_view;
4482 struct tog_blame_view_state *s = &view->state.blame;
4491 if (s->selected_line > 1)
4493 else if (s->selected_line == 1 &&
4494 s->first_displayed_line > 1)
4495 s->first_displayed_line--;
4499 if (s->first_displayed_line == 1) {
4500 s->selected_line = 1;
4503 if (s->first_displayed_line > view->nlines - 2)
4504 s->first_displayed_line -=
4507 s->first_displayed_line = 1;
4511 if (s->selected_line < view->nlines - 2 &&
4512 s->first_displayed_line +
4513 s->selected_line <= s->blame.nlines)
4515 else if (s->last_displayed_line <
4517 s->first_displayed_line++;
4521 struct got_object_id *id = NULL;
4522 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4523 s->first_displayed_line, s->selected_line);
4527 struct got_commit_object *commit;
4528 struct got_object_qid *pid;
4529 struct got_object_id *blob_id = NULL;
4531 err = got_object_open_as_commit(&commit,
4536 got_object_commit_get_parent_ids(commit));
4538 got_object_commit_close(commit);
4541 /* Check if path history ends here. */
4542 err = got_object_id_by_path(&blob_id, s->repo,
4545 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4547 got_object_commit_close(commit);
4550 err = got_object_get_type(&obj_type, s->repo,
4553 /* Can't blame non-blob type objects. */
4554 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4555 got_object_commit_close(commit);
4558 err = got_object_qid_alloc(&s->blamed_commit,
4560 got_object_commit_close(commit);
4562 if (got_object_id_cmp(id,
4563 s->blamed_commit->id) == 0)
4565 err = got_object_qid_alloc(&s->blamed_commit,
4571 thread_err = stop_blame(&s->blame);
4575 STAILQ_INSERT_HEAD(&s->blamed_commits,
4576 s->blamed_commit, entry);
4577 err = run_blame(view);
4583 struct got_object_qid *first;
4584 first = STAILQ_FIRST(&s->blamed_commits);
4585 if (!got_object_id_cmp(first->id, s->commit_id))
4588 thread_err = stop_blame(&s->blame);
4592 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4593 got_object_qid_free(s->blamed_commit);
4595 STAILQ_FIRST(&s->blamed_commits);
4596 err = run_blame(view);
4603 struct got_object_id *id = NULL;
4604 struct got_object_qid *pid;
4605 struct got_commit_object *commit = NULL;
4606 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4607 s->first_displayed_line, s->selected_line);
4610 err = got_object_open_as_commit(&commit, s->repo, id);
4614 got_object_commit_get_parent_ids(commit));
4615 if (view_is_parent_view(view))
4616 begin_x = view_split_begin_x(view->begin_x);
4617 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4618 if (diff_view == NULL) {
4619 got_object_commit_close(commit);
4620 err = got_error_from_errno("view_open");
4623 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4624 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4625 got_object_commit_close(commit);
4627 view_close(diff_view);
4631 diff_view->focussed = 1;
4632 if (view_is_parent_view(view)) {
4633 err = view_close_child(view);
4636 view_set_child(view, diff_view);
4637 view->focus_child = 1;
4639 *new_view = diff_view;
4647 if (s->last_displayed_line >= s->blame.nlines &&
4648 s->selected_line >= MIN(s->blame.nlines,
4649 view->nlines - 2)) {
4652 if (s->last_displayed_line >= s->blame.nlines &&
4653 s->selected_line < view->nlines - 2) {
4654 s->selected_line = MIN(s->blame.nlines,
4658 if (s->last_displayed_line + view->nlines - 2
4660 s->first_displayed_line +=
4663 s->first_displayed_line =
4668 if (s->selected_line > view->nlines - 2) {
4669 s->selected_line = MIN(s->blame.nlines,
4676 return thread_err ? thread_err : err;
4679 static const struct got_error *
4680 cmd_blame(int argc, char *argv[])
4682 const struct got_error *error;
4683 struct got_repository *repo = NULL;
4684 struct got_worktree *worktree = NULL;
4685 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4686 char *link_target = NULL;
4687 struct got_object_id *commit_id = NULL;
4688 char *commit_id_str = NULL;
4690 struct tog_view *view;
4692 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4695 commit_id_str = optarg;
4698 repo_path = realpath(optarg, NULL);
4699 if (repo_path == NULL)
4700 return got_error_from_errno2("realpath",
4715 if (repo_path == NULL) {
4716 cwd = getcwd(NULL, 0);
4718 return got_error_from_errno("getcwd");
4719 error = got_worktree_open(&worktree, cwd);
4720 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4724 strdup(got_worktree_get_repo_path(worktree));
4726 repo_path = strdup(cwd);
4727 if (repo_path == NULL) {
4728 error = got_error_from_errno("strdup");
4733 error = got_repo_open(&repo, repo_path, NULL);
4737 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4744 error = apply_unveil(got_repo_get_path(repo), NULL);
4748 error = tog_load_refs(repo);
4752 if (commit_id_str == NULL) {
4753 struct got_reference *head_ref;
4754 error = got_ref_open(&head_ref, repo, worktree ?
4755 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4758 error = got_ref_resolve(&commit_id, repo, head_ref);
4759 got_ref_close(head_ref);
4761 error = got_repo_match_object_id(&commit_id, NULL,
4762 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4767 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4769 error = got_error_from_errno("view_open");
4773 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4778 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4783 /* Release work tree lock. */
4784 got_worktree_close(worktree);
4787 error = view_loop(view);
4795 got_worktree_close(worktree);
4797 const struct got_error *close_err = got_repo_close(repo);
4805 static const struct got_error *
4806 draw_tree_entries(struct tog_view *view, const char *parent_path)
4808 struct tog_tree_view_state *s = &view->state.tree;
4809 const struct got_error *err = NULL;
4810 struct got_tree_entry *te;
4812 struct tog_color *tc;
4813 int width, n, i, nentries;
4814 int limit = view->nlines;
4818 werase(view->window);
4823 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4826 if (view_needs_focus_indication(view))
4827 wstandout(view->window);
4828 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4830 wattr_on(view->window,
4831 COLOR_PAIR(tc->colorpair), NULL);
4832 waddwstr(view->window, wline);
4834 wattr_off(view->window,
4835 COLOR_PAIR(tc->colorpair), NULL);
4836 if (view_needs_focus_indication(view))
4837 wstandend(view->window);
4840 if (width < view->ncols - 1)
4841 waddch(view->window, '\n');
4844 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4847 waddwstr(view->window, wline);
4850 if (width < view->ncols - 1)
4851 waddch(view->window, '\n');
4854 waddch(view->window, '\n');
4858 if (s->first_displayed_entry == NULL) {
4859 te = got_object_tree_get_first_entry(s->tree);
4860 if (s->selected == 0) {
4862 wstandout(view->window);
4863 s->selected_entry = NULL;
4865 waddstr(view->window, " ..\n"); /* parent directory */
4866 if (s->selected == 0 && view->focussed)
4867 wstandend(view->window);
4874 te = s->first_displayed_entry;
4877 nentries = got_object_tree_get_nentries(s->tree);
4878 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4879 char *line = NULL, *id_str = NULL, *link_target = NULL;
4880 const char *modestr = "";
4883 te = got_object_tree_get_entry(s->tree, i);
4884 mode = got_tree_entry_get_mode(te);
4887 err = got_object_id_str(&id_str,
4888 got_tree_entry_get_id(te));
4890 return got_error_from_errno(
4891 "got_object_id_str");
4893 if (got_object_tree_entry_is_submodule(te))
4895 else if (S_ISLNK(mode)) {
4898 err = got_tree_entry_get_symlink_target(&link_target,
4904 for (i = 0; i < strlen(link_target); i++) {
4905 if (!isprint((unsigned char)link_target[i]))
4906 link_target[i] = '?';
4910 else if (S_ISDIR(mode))
4912 else if (mode & S_IXUSR)
4914 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4915 got_tree_entry_get_name(te), modestr,
4916 link_target ? " -> ": "",
4917 link_target ? link_target : "") == -1) {
4920 return got_error_from_errno("asprintf");
4924 err = format_line(&wline, &width, line, view->ncols, 0);
4929 if (n == s->selected) {
4931 wstandout(view->window);
4932 s->selected_entry = te;
4934 tc = match_color(&s->colors, line);
4936 wattr_on(view->window,
4937 COLOR_PAIR(tc->colorpair), NULL);
4938 waddwstr(view->window, wline);
4940 wattr_off(view->window,
4941 COLOR_PAIR(tc->colorpair), NULL);
4942 if (width < view->ncols - 1)
4943 waddch(view->window, '\n');
4944 if (n == s->selected && view->focussed)
4945 wstandend(view->window);
4951 s->last_displayed_entry = te;
4960 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4962 struct got_tree_entry *te;
4963 int isroot = s->tree == s->root;
4966 if (s->first_displayed_entry == NULL)
4969 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4970 while (i++ < maxscroll) {
4973 s->first_displayed_entry = NULL;
4976 s->first_displayed_entry = te;
4977 te = got_tree_entry_get_prev(s->tree, te);
4982 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4984 struct got_tree_entry *next, *last;
4987 if (s->first_displayed_entry)
4988 next = got_tree_entry_get_next(s->tree,
4989 s->first_displayed_entry);
4991 next = got_object_tree_get_first_entry(s->tree);
4993 last = s->last_displayed_entry;
4994 while (next && last && n++ < maxscroll) {
4995 last = got_tree_entry_get_next(s->tree, last);
4997 s->first_displayed_entry = next;
4998 next = got_tree_entry_get_next(s->tree, next);
5003 static const struct got_error *
5004 tree_entry_path(char **path, struct tog_parent_trees *parents,
5005 struct got_tree_entry *te)
5007 const struct got_error *err = NULL;
5008 struct tog_parent_tree *pt;
5009 size_t len = 2; /* for leading slash and NUL */
5011 TAILQ_FOREACH(pt, parents, entry)
5012 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5015 len += strlen(got_tree_entry_get_name(te));
5017 *path = calloc(1, len);
5019 return got_error_from_errno("calloc");
5022 pt = TAILQ_LAST(parents, tog_parent_trees);
5024 const char *name = got_tree_entry_get_name(pt->selected_entry);
5025 if (strlcat(*path, name, len) >= len) {
5026 err = got_error(GOT_ERR_NO_SPACE);
5029 if (strlcat(*path, "/", len) >= len) {
5030 err = got_error(GOT_ERR_NO_SPACE);
5033 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5036 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5037 err = got_error(GOT_ERR_NO_SPACE);
5049 static const struct got_error *
5050 blame_tree_entry(struct tog_view **new_view, int begin_x,
5051 struct got_tree_entry *te, struct tog_parent_trees *parents,
5052 struct got_object_id *commit_id, struct got_repository *repo)
5054 const struct got_error *err = NULL;
5056 struct tog_view *blame_view;
5060 err = tree_entry_path(&path, parents, te);
5064 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5065 if (blame_view == NULL) {
5066 err = got_error_from_errno("view_open");
5070 err = open_blame_view(blame_view, path, commit_id, repo);
5072 if (err->code == GOT_ERR_CANCELLED)
5074 view_close(blame_view);
5076 *new_view = blame_view;
5082 static const struct got_error *
5083 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5084 struct tog_tree_view_state *s)
5086 struct tog_view *log_view;
5087 const struct got_error *err = NULL;
5092 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5093 if (log_view == NULL)
5094 return got_error_from_errno("view_open");
5096 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5100 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5103 view_close(log_view);
5105 *new_view = log_view;
5110 static const struct got_error *
5111 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5112 struct got_object_id *commit_id, const char *head_ref_name,
5113 struct got_repository *repo)
5115 const struct got_error *err = NULL;
5116 char *commit_id_str = NULL;
5117 struct tog_tree_view_state *s = &view->state.tree;
5119 TAILQ_INIT(&s->parents);
5121 err = got_object_id_str(&commit_id_str, commit_id);
5125 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5126 err = got_error_from_errno("asprintf");
5130 s->root = s->tree = root;
5131 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5132 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5133 s->commit_id = got_object_id_dup(commit_id);
5134 if (s->commit_id == NULL) {
5135 err = got_error_from_errno("got_object_id_dup");
5138 if (head_ref_name) {
5139 s->head_ref_name = strdup(head_ref_name);
5140 if (s->head_ref_name == NULL) {
5141 err = got_error_from_errno("strdup");
5147 STAILQ_INIT(&s->colors);
5149 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5150 err = add_color(&s->colors, "\\$$",
5151 TOG_COLOR_TREE_SUBMODULE,
5152 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5155 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5156 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5158 free_colors(&s->colors);
5161 err = add_color(&s->colors, "/$",
5162 TOG_COLOR_TREE_DIRECTORY,
5163 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5165 free_colors(&s->colors);
5169 err = add_color(&s->colors, "\\*$",
5170 TOG_COLOR_TREE_EXECUTABLE,
5171 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5173 free_colors(&s->colors);
5177 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5178 get_color_value("TOG_COLOR_COMMIT"));
5180 free_colors(&s->colors);
5185 view->show = show_tree_view;
5186 view->input = input_tree_view;
5187 view->close = close_tree_view;
5188 view->search_start = search_start_tree_view;
5189 view->search_next = search_next_tree_view;
5191 free(commit_id_str);
5193 free(s->tree_label);
5194 s->tree_label = NULL;
5199 static const struct got_error *
5200 close_tree_view(struct tog_view *view)
5202 struct tog_tree_view_state *s = &view->state.tree;
5204 free_colors(&s->colors);
5205 free(s->tree_label);
5206 s->tree_label = NULL;
5208 s->commit_id = NULL;
5209 free(s->head_ref_name);
5210 s->head_ref_name = NULL;
5211 while (!TAILQ_EMPTY(&s->parents)) {
5212 struct tog_parent_tree *parent;
5213 parent = TAILQ_FIRST(&s->parents);
5214 TAILQ_REMOVE(&s->parents, parent, entry);
5218 if (s->tree != s->root)
5219 got_object_tree_close(s->tree);
5220 got_object_tree_close(s->root);
5224 static const struct got_error *
5225 search_start_tree_view(struct tog_view *view)
5227 struct tog_tree_view_state *s = &view->state.tree;
5229 s->matched_entry = NULL;
5234 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5236 regmatch_t regmatch;
5238 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5242 static const struct got_error *
5243 search_next_tree_view(struct tog_view *view)
5245 struct tog_tree_view_state *s = &view->state.tree;
5246 struct got_tree_entry *te = NULL;
5248 if (!view->searching) {
5249 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5253 if (s->matched_entry) {
5254 if (view->searching == TOG_SEARCH_FORWARD) {
5255 if (s->selected_entry)
5256 te = got_tree_entry_get_next(s->tree,
5259 te = got_object_tree_get_first_entry(s->tree);
5261 if (s->selected_entry == NULL)
5262 te = got_object_tree_get_last_entry(s->tree);
5264 te = got_tree_entry_get_prev(s->tree,
5268 if (view->searching == TOG_SEARCH_FORWARD)
5269 te = got_object_tree_get_first_entry(s->tree);
5271 te = got_object_tree_get_last_entry(s->tree);
5276 if (s->matched_entry == NULL) {
5277 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5280 if (view->searching == TOG_SEARCH_FORWARD)
5281 te = got_object_tree_get_first_entry(s->tree);
5283 te = got_object_tree_get_last_entry(s->tree);
5286 if (match_tree_entry(te, &view->regex)) {
5287 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5288 s->matched_entry = te;
5292 if (view->searching == TOG_SEARCH_FORWARD)
5293 te = got_tree_entry_get_next(s->tree, te);
5295 te = got_tree_entry_get_prev(s->tree, te);
5298 if (s->matched_entry) {
5299 s->first_displayed_entry = s->matched_entry;
5306 static const struct got_error *
5307 show_tree_view(struct tog_view *view)
5309 const struct got_error *err = NULL;
5310 struct tog_tree_view_state *s = &view->state.tree;
5313 err = tree_entry_path(&parent_path, &s->parents, NULL);
5317 err = draw_tree_entries(view, parent_path);
5324 static const struct got_error *
5325 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5327 const struct got_error *err = NULL;
5328 struct tog_tree_view_state *s = &view->state.tree;
5329 struct tog_view *log_view, *ref_view;
5334 s->show_ids = !s->show_ids;
5337 if (!s->selected_entry)
5339 if (view_is_parent_view(view))
5340 begin_x = view_split_begin_x(view->begin_x);
5341 err = log_selected_tree_entry(&log_view, begin_x, s);
5343 log_view->focussed = 1;
5344 if (view_is_parent_view(view)) {
5345 err = view_close_child(view);
5348 view_set_child(view, log_view);
5349 view->focus_child = 1;
5351 *new_view = log_view;
5354 if (view_is_parent_view(view))
5355 begin_x = view_split_begin_x(view->begin_x);
5356 ref_view = view_open(view->nlines, view->ncols,
5357 view->begin_y, begin_x, TOG_VIEW_REF);
5358 if (ref_view == NULL)
5359 return got_error_from_errno("view_open");
5360 err = open_ref_view(ref_view, s->repo);
5362 view_close(ref_view);
5366 ref_view->focussed = 1;
5367 if (view_is_parent_view(view)) {
5368 err = view_close_child(view);
5371 view_set_child(view, ref_view);
5372 view->focus_child = 1;
5374 *new_view = ref_view;
5378 if (s->selected > 0) {
5382 tree_scroll_up(s, 1);
5386 if (s->tree == s->root) {
5387 if (got_object_tree_get_first_entry(s->tree) ==
5388 s->first_displayed_entry)
5391 if (s->first_displayed_entry == NULL)
5394 tree_scroll_up(s, MAX(0, view->nlines - 3));
5398 if (s->selected < s->ndisplayed - 1) {
5402 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5404 /* can't scroll any further */
5406 tree_scroll_down(s, 1);
5410 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5412 /* can't scroll any further; move cursor down */
5413 if (s->selected < s->ndisplayed - 1)
5414 s->selected = s->ndisplayed - 1;
5417 tree_scroll_down(s, view->nlines - 3);
5422 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5423 struct tog_parent_tree *parent;
5424 /* user selected '..' */
5425 if (s->tree == s->root)
5427 parent = TAILQ_FIRST(&s->parents);
5428 TAILQ_REMOVE(&s->parents, parent,
5430 got_object_tree_close(s->tree);
5431 s->tree = parent->tree;
5432 s->first_displayed_entry =
5433 parent->first_displayed_entry;
5435 parent->selected_entry;
5436 s->selected = parent->selected;
5438 } else if (S_ISDIR(got_tree_entry_get_mode(
5439 s->selected_entry))) {
5440 struct got_tree_object *subtree;
5441 err = got_object_open_as_tree(&subtree, s->repo,
5442 got_tree_entry_get_id(s->selected_entry));
5445 err = tree_view_visit_subtree(s, subtree);
5447 got_object_tree_close(subtree);
5450 } else if (S_ISREG(got_tree_entry_get_mode(
5451 s->selected_entry))) {
5452 struct tog_view *blame_view;
5453 int begin_x = view_is_parent_view(view) ?
5454 view_split_begin_x(view->begin_x) : 0;
5456 err = blame_tree_entry(&blame_view, begin_x,
5457 s->selected_entry, &s->parents,
5458 s->commit_id, s->repo);
5462 blame_view->focussed = 1;
5463 if (view_is_parent_view(view)) {
5464 err = view_close_child(view);
5467 view_set_child(view, blame_view);
5468 view->focus_child = 1;
5470 *new_view = blame_view;
5474 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5475 s->selected = view->nlines - 4;
5488 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5493 static const struct got_error *
5494 cmd_tree(int argc, char *argv[])
5496 const struct got_error *error;
5497 struct got_repository *repo = NULL;
5498 struct got_worktree *worktree = NULL;
5499 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5500 struct got_object_id *commit_id = NULL;
5501 const char *commit_id_arg = NULL;
5503 struct got_commit_object *commit = NULL;
5504 struct got_tree_object *tree = NULL;
5505 struct got_reference *ref = NULL;
5506 const char *head_ref_name = NULL;
5508 struct tog_view *view;
5510 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5513 commit_id_arg = optarg;
5516 repo_path = realpath(optarg, NULL);
5517 if (repo_path == NULL)
5518 return got_error_from_errno2("realpath",
5533 if (repo_path == NULL) {
5534 cwd = getcwd(NULL, 0);
5536 return got_error_from_errno("getcwd");
5537 error = got_worktree_open(&worktree, cwd);
5538 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5542 strdup(got_worktree_get_repo_path(worktree));
5544 repo_path = strdup(cwd);
5545 if (repo_path == NULL) {
5546 error = got_error_from_errno("strdup");
5551 error = got_repo_open(&repo, repo_path, NULL);
5555 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5562 error = apply_unveil(got_repo_get_path(repo), NULL);
5566 error = tog_load_refs(repo);
5570 if (commit_id_arg == NULL) {
5571 error = got_repo_match_object_id(&commit_id, &label,
5572 worktree ? got_worktree_get_head_ref_name(worktree) :
5573 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5576 head_ref_name = label;
5578 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5580 head_ref_name = got_ref_get_name(ref);
5581 else if (error->code != GOT_ERR_NOT_REF)
5583 error = got_repo_match_object_id(&commit_id, NULL,
5584 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5589 error = got_object_open_as_commit(&commit, repo, commit_id);
5593 error = got_object_open_as_tree(&tree, repo,
5594 got_object_commit_get_tree_id(commit));
5598 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5600 error = got_error_from_errno("view_open");
5603 error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5606 if (!got_path_is_root_dir(in_repo_path)) {
5607 error = tree_view_walk_path(&view->state.tree, commit_id,
5614 /* Release work tree lock. */
5615 got_worktree_close(worktree);
5618 error = view_loop(view);
5627 got_object_commit_close(commit);
5629 got_object_tree_close(tree);
5631 const struct got_error *close_err = got_repo_close(repo);
5639 static const struct got_error *
5640 ref_view_load_refs(struct tog_ref_view_state *s)
5642 struct got_reflist_entry *sre;
5643 struct tog_reflist_entry *re;
5646 TAILQ_FOREACH(sre, &tog_refs, entry) {
5647 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5650 re = malloc(sizeof(*re));
5652 return got_error_from_errno("malloc");
5654 re->ref = got_ref_dup(sre->ref);
5655 if (re->ref == NULL)
5656 return got_error_from_errno("got_ref_dup");
5657 re->idx = s->nrefs++;
5658 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5661 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5666 ref_view_free_refs(struct tog_ref_view_state *s)
5668 struct tog_reflist_entry *re;
5670 while (!TAILQ_EMPTY(&s->refs)) {
5671 re = TAILQ_FIRST(&s->refs);
5672 TAILQ_REMOVE(&s->refs, re, entry);
5673 got_ref_close(re->ref);
5678 static const struct got_error *
5679 open_ref_view(struct tog_view *view, struct got_repository *repo)
5681 const struct got_error *err = NULL;
5682 struct tog_ref_view_state *s = &view->state.ref;
5684 s->selected_entry = 0;
5687 TAILQ_INIT(&s->refs);
5688 STAILQ_INIT(&s->colors);
5690 err = ref_view_load_refs(s);
5694 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5695 err = add_color(&s->colors, "^refs/heads/",
5696 TOG_COLOR_REFS_HEADS,
5697 get_color_value("TOG_COLOR_REFS_HEADS"));
5701 err = add_color(&s->colors, "^refs/tags/",
5702 TOG_COLOR_REFS_TAGS,
5703 get_color_value("TOG_COLOR_REFS_TAGS"));
5707 err = add_color(&s->colors, "^refs/remotes/",
5708 TOG_COLOR_REFS_REMOTES,
5709 get_color_value("TOG_COLOR_REFS_REMOTES"));
5714 view->show = show_ref_view;
5715 view->input = input_ref_view;
5716 view->close = close_ref_view;
5717 view->search_start = search_start_ref_view;
5718 view->search_next = search_next_ref_view;
5721 free_colors(&s->colors);
5725 static const struct got_error *
5726 close_ref_view(struct tog_view *view)
5728 struct tog_ref_view_state *s = &view->state.ref;
5730 ref_view_free_refs(s);
5731 free_colors(&s->colors);
5736 static const struct got_error *
5737 resolve_reflist_entry(struct got_object_id **commit_id,
5738 struct tog_reflist_entry *re, struct got_repository *repo)
5740 const struct got_error *err = NULL;
5741 struct got_object_id *obj_id;
5742 struct got_tag_object *tag = NULL;
5747 err = got_ref_resolve(&obj_id, repo, re->ref);
5751 err = got_object_get_type(&obj_type, repo, obj_id);
5756 case GOT_OBJ_TYPE_COMMIT:
5757 *commit_id = obj_id;
5759 case GOT_OBJ_TYPE_TAG:
5760 err = got_object_open_as_tag(&tag, repo, obj_id);
5764 err = got_object_get_type(&obj_type, repo,
5765 got_object_tag_get_object_id(tag));
5768 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5769 err = got_error(GOT_ERR_OBJ_TYPE);
5772 *commit_id = got_object_id_dup(
5773 got_object_tag_get_object_id(tag));
5774 if (*commit_id == NULL) {
5775 err = got_error_from_errno("got_object_id_dup");
5780 err = got_error(GOT_ERR_OBJ_TYPE);
5786 got_object_tag_close(tag);
5794 static const struct got_error *
5795 log_ref_entry(struct tog_view **new_view, int begin_x,
5796 struct tog_reflist_entry *re, struct got_repository *repo)
5798 struct tog_view *log_view;
5799 const struct got_error *err = NULL;
5800 struct got_object_id *commit_id = NULL;
5804 err = resolve_reflist_entry(&commit_id, re, repo);
5806 if (err->code != GOT_ERR_OBJ_TYPE)
5812 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5813 if (log_view == NULL) {
5814 err = got_error_from_errno("view_open");
5818 err = open_log_view(log_view, commit_id, repo,
5819 got_ref_get_name(re->ref), "", 0);
5822 view_close(log_view);
5824 *new_view = log_view;
5830 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5832 struct tog_reflist_entry *re;
5835 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5838 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5839 while (i++ < maxscroll) {
5842 s->first_displayed_entry = re;
5843 re = TAILQ_PREV(re, tog_reflist_head, entry);
5848 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5850 struct tog_reflist_entry *next, *last;
5853 if (s->first_displayed_entry)
5854 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5856 next = TAILQ_FIRST(&s->refs);
5858 last = s->last_displayed_entry;
5859 while (next && last && n++ < maxscroll) {
5860 last = TAILQ_NEXT(last, entry);
5862 s->first_displayed_entry = next;
5863 next = TAILQ_NEXT(next, entry);
5868 static const struct got_error *
5869 search_start_ref_view(struct tog_view *view)
5871 struct tog_ref_view_state *s = &view->state.ref;
5873 s->matched_entry = NULL;
5878 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5880 regmatch_t regmatch;
5882 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5886 static const struct got_error *
5887 search_next_ref_view(struct tog_view *view)
5889 struct tog_ref_view_state *s = &view->state.ref;
5890 struct tog_reflist_entry *re = NULL;
5892 if (!view->searching) {
5893 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5897 if (s->matched_entry) {
5898 if (view->searching == TOG_SEARCH_FORWARD) {
5899 if (s->selected_entry)
5900 re = TAILQ_NEXT(s->selected_entry, entry);
5902 re = TAILQ_PREV(s->selected_entry,
5903 tog_reflist_head, entry);
5905 if (s->selected_entry == NULL)
5906 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5908 re = TAILQ_PREV(s->selected_entry,
5909 tog_reflist_head, entry);
5912 if (view->searching == TOG_SEARCH_FORWARD)
5913 re = TAILQ_FIRST(&s->refs);
5915 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5920 if (s->matched_entry == NULL) {
5921 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5924 if (view->searching == TOG_SEARCH_FORWARD)
5925 re = TAILQ_FIRST(&s->refs);
5927 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5930 if (match_reflist_entry(re, &view->regex)) {
5931 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5932 s->matched_entry = re;
5936 if (view->searching == TOG_SEARCH_FORWARD)
5937 re = TAILQ_NEXT(re, entry);
5939 re = TAILQ_PREV(re, tog_reflist_head, entry);
5942 if (s->matched_entry) {
5943 s->first_displayed_entry = s->matched_entry;
5950 static const struct got_error *
5951 show_ref_view(struct tog_view *view)
5953 const struct got_error *err = NULL;
5954 struct tog_ref_view_state *s = &view->state.ref;
5955 struct tog_reflist_entry *re;
5958 struct tog_color *tc;
5960 int limit = view->nlines;
5962 werase(view->window);
5969 re = s->first_displayed_entry;
5971 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5973 return got_error_from_errno("asprintf");
5975 err = format_line(&wline, &width, line, view->ncols, 0);
5980 if (view_needs_focus_indication(view))
5981 wstandout(view->window);
5982 waddwstr(view->window, wline);
5983 if (view_needs_focus_indication(view))
5984 wstandend(view->window);
5989 if (width < view->ncols - 1)
5990 waddch(view->window, '\n');
5995 while (re && limit > 0) {
5998 if (got_ref_is_symbolic(re->ref)) {
5999 if (asprintf(&line, "%s -> %s",
6000 got_ref_get_name(re->ref),
6001 got_ref_get_symref_target(re->ref)) == -1)
6002 return got_error_from_errno("asprintf");
6003 } else if (s->show_ids) {
6004 struct got_object_id *id;
6006 err = got_ref_resolve(&id, s->repo, re->ref);
6009 err = got_object_id_str(&id_str, id);
6014 if (asprintf(&line, "%s: %s",
6015 got_ref_get_name(re->ref), id_str) == -1) {
6016 err = got_error_from_errno("asprintf");
6024 line = strdup(got_ref_get_name(re->ref));
6026 return got_error_from_errno("strdup");
6029 err = format_line(&wline, &width, line, view->ncols, 0);
6034 if (n == s->selected) {
6036 wstandout(view->window);
6037 s->selected_entry = re;
6039 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6041 wattr_on(view->window,
6042 COLOR_PAIR(tc->colorpair), NULL);
6043 waddwstr(view->window, wline);
6045 wattr_off(view->window,
6046 COLOR_PAIR(tc->colorpair), NULL);
6047 if (width < view->ncols - 1)
6048 waddch(view->window, '\n');
6049 if (n == s->selected && view->focussed)
6050 wstandend(view->window);
6056 s->last_displayed_entry = re;
6059 re = TAILQ_NEXT(re, entry);
6066 static const struct got_error *
6067 browse_ref_tree(struct tog_view **new_view, int begin_x,
6068 struct tog_reflist_entry *re, struct got_repository *repo)
6070 const struct got_error *err = NULL;
6071 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6072 struct got_tree_object *tree = NULL;
6073 struct tog_view *tree_view;
6077 err = resolve_reflist_entry(&commit_id, re, repo);
6079 if (err->code != GOT_ERR_OBJ_TYPE)
6085 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6089 err = got_object_open_as_tree(&tree, repo, tree_id);
6093 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6094 if (tree_view == NULL) {
6095 err = got_error_from_errno("view_open");
6099 err = open_tree_view(tree_view, tree, commit_id,
6100 got_ref_get_name(re->ref), repo);
6104 *new_view = tree_view;
6110 got_object_tree_close(tree);
6114 static const struct got_error *
6115 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6117 const struct got_error *err = NULL;
6118 struct tog_ref_view_state *s = &view->state.ref;
6119 struct tog_view *log_view, *tree_view;
6124 s->show_ids = !s->show_ids;
6128 if (!s->selected_entry)
6130 if (view_is_parent_view(view))
6131 begin_x = view_split_begin_x(view->begin_x);
6132 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6135 log_view->focussed = 1;
6136 if (view_is_parent_view(view)) {
6137 err = view_close_child(view);
6140 view_set_child(view, log_view);
6141 view->focus_child = 1;
6143 *new_view = log_view;
6146 if (!s->selected_entry)
6148 if (view_is_parent_view(view))
6149 begin_x = view_split_begin_x(view->begin_x);
6150 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6152 if (err || tree_view == NULL)
6155 tree_view->focussed = 1;
6156 if (view_is_parent_view(view)) {
6157 err = view_close_child(view);
6160 view_set_child(view, tree_view);
6161 view->focus_child = 1;
6163 *new_view = tree_view;
6167 if (s->selected > 0) {
6171 ref_scroll_up(s, 1);
6175 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6177 ref_scroll_up(s, MAX(0, view->nlines - 1));
6181 if (s->selected < s->ndisplayed - 1) {
6185 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6186 /* can't scroll any further */
6188 ref_scroll_down(s, 1);
6192 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6193 /* can't scroll any further; move cursor down */
6194 if (s->selected < s->ndisplayed - 1)
6195 s->selected = s->ndisplayed - 1;
6198 ref_scroll_down(s, view->nlines - 1);
6202 err = tog_load_refs(s->repo);
6205 ref_view_free_refs(s);
6206 err = ref_view_load_refs(s);
6209 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6210 s->selected = view->nlines - 2;
6223 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6228 static const struct got_error *
6229 cmd_ref(int argc, char *argv[])
6231 const struct got_error *error;
6232 struct got_repository *repo = NULL;
6233 struct got_worktree *worktree = NULL;
6234 char *cwd = NULL, *repo_path = NULL;
6236 struct tog_view *view;
6238 while ((ch = getopt(argc, argv, "r:")) != -1) {
6241 repo_path = realpath(optarg, NULL);
6242 if (repo_path == NULL)
6243 return got_error_from_errno2("realpath",
6258 if (repo_path == NULL) {
6259 cwd = getcwd(NULL, 0);
6261 return got_error_from_errno("getcwd");
6262 error = got_worktree_open(&worktree, cwd);
6263 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6267 strdup(got_worktree_get_repo_path(worktree));
6269 repo_path = strdup(cwd);
6270 if (repo_path == NULL) {
6271 error = got_error_from_errno("strdup");
6276 error = got_repo_open(&repo, repo_path, NULL);
6282 error = apply_unveil(got_repo_get_path(repo), NULL);
6286 error = tog_load_refs(repo);
6290 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6292 error = got_error_from_errno("view_open");
6296 error = open_ref_view(view, repo);
6301 /* Release work tree lock. */
6302 got_worktree_close(worktree);
6305 error = view_loop(view);
6310 const struct got_error *close_err = got_repo_close(repo);
6319 list_commands(FILE *fp)
6323 fprintf(fp, "commands:");
6324 for (i = 0; i < nitems(tog_commands); i++) {
6325 struct tog_cmd *cmd = &tog_commands[i];
6326 fprintf(fp, " %s", cmd->name);
6332 usage(int hflag, int status)
6334 FILE *fp = (status == 0) ? stdout : stderr;
6336 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6339 fprintf(fp, "lazy usage: %s path\n", getprogname());
6346 make_argv(int argc, ...)
6354 argv = calloc(argc, sizeof(char *));
6357 for (i = 0; i < argc; i++) {
6358 argv[i] = strdup(va_arg(ap, char *));
6359 if (argv[i] == NULL)
6368 * Try to convert 'tog path' into a 'tog log path' command.
6369 * The user could simply have mistyped the command rather than knowingly
6370 * provided a path. So check whether argv[0] can in fact be resolved
6371 * to a path in the HEAD commit and print a special error if not.
6372 * This hack is for mpi@ <3
6374 static const struct got_error *
6375 tog_log_with_path(int argc, char *argv[])
6377 const struct got_error *error = NULL, *close_err;
6378 struct tog_cmd *cmd = NULL;
6379 struct got_repository *repo = NULL;
6380 struct got_worktree *worktree = NULL;
6381 struct got_object_id *commit_id = NULL, *id = NULL;
6382 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6383 char *commit_id_str = NULL, **cmd_argv = NULL;
6385 cwd = getcwd(NULL, 0);
6387 return got_error_from_errno("getcwd");
6389 error = got_worktree_open(&worktree, cwd);
6390 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6394 repo_path = strdup(got_worktree_get_repo_path(worktree));
6396 repo_path = strdup(cwd);
6397 if (repo_path == NULL) {
6398 error = got_error_from_errno("strdup");
6402 error = got_repo_open(&repo, repo_path, NULL);
6406 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6411 error = tog_load_refs(repo);
6414 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6415 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6416 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6421 got_worktree_close(worktree);
6425 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6427 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6429 fprintf(stderr, "%s: '%s' is no known command or path\n",
6430 getprogname(), argv[0]);
6435 close_err = got_repo_close(repo);
6440 error = got_object_id_str(&commit_id_str, commit_id);
6444 cmd = &tog_commands[0]; /* log */
6446 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6447 error = cmd->cmd_main(argc, cmd_argv);
6450 close_err = got_repo_close(repo);
6455 got_worktree_close(worktree);
6457 free(commit_id_str);
6464 for (i = 0; i < argc; i++)
6473 main(int argc, char *argv[])
6475 const struct got_error *error = NULL;
6476 struct tog_cmd *cmd = NULL;
6477 int ch, hflag = 0, Vflag = 0;
6478 char **cmd_argv = NULL;
6479 static struct option longopts[] = {
6480 { "version", no_argument, NULL, 'V' },
6484 setlocale(LC_CTYPE, "");
6486 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6506 got_version_print_str();
6511 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6519 /* Build an argument vector which runs a default command. */
6520 cmd = &tog_commands[0];
6522 cmd_argv = make_argv(argc, cmd->name);
6526 /* Did the user specify a command? */
6527 for (i = 0; i < nitems(tog_commands); i++) {
6528 if (strncmp(tog_commands[i].name, argv[0],
6529 strlen(argv[0])) == 0) {
6530 cmd = &tog_commands[i];
6539 /* No command specified; try log with a path */
6540 error = tog_log_with_path(argc, argv);
6545 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6552 for (i = 0; i < argc; i++)
6557 if (error && error->code != GOT_ERR_CANCELLED)
6558 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);