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_object_id *commit_id;/* commit which this tree belongs to */
415 struct got_tree_object *root; /* the commit's root tree entry */
416 struct got_tree_object *tree; /* currently displayed (sub-)tree */
417 struct got_tree_entry *first_displayed_entry;
418 struct got_tree_entry *last_displayed_entry;
419 struct got_tree_entry *selected_entry;
420 int ndisplayed, selected, show_ids;
421 struct tog_parent_trees parents; /* parent trees of current sub-tree */
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_object_id *, const char *, struct got_repository *);
549 static const struct got_error *show_tree_view(struct tog_view *);
550 static const struct got_error *input_tree_view(struct tog_view **,
551 struct tog_view *, int);
552 static const struct got_error *close_tree_view(struct tog_view *);
553 static const struct got_error *search_start_tree_view(struct tog_view *);
554 static const struct got_error *search_next_tree_view(struct tog_view *);
556 static const struct got_error *open_ref_view(struct tog_view *,
557 struct got_repository *);
558 static const struct got_error *show_ref_view(struct tog_view *);
559 static const struct got_error *input_ref_view(struct tog_view **,
560 struct tog_view *, int);
561 static const struct got_error *close_ref_view(struct tog_view *);
562 static const struct got_error *search_start_ref_view(struct tog_view *);
563 static const struct got_error *search_next_ref_view(struct tog_view *);
565 static volatile sig_atomic_t tog_sigwinch_received;
566 static volatile sig_atomic_t tog_sigpipe_received;
567 static volatile sig_atomic_t tog_sigcont_received;
570 tog_sigwinch(int signo)
572 tog_sigwinch_received = 1;
576 tog_sigpipe(int signo)
578 tog_sigpipe_received = 1;
582 tog_sigcont(int signo)
584 tog_sigcont_received = 1;
587 static const struct got_error *
588 view_close(struct tog_view *view)
590 const struct got_error *err = NULL;
593 view_close(view->child);
597 err = view->close(view);
599 del_panel(view->panel);
601 delwin(view->window);
606 static struct tog_view *
607 view_open(int nlines, int ncols, int begin_y, int begin_x,
608 enum tog_view_type type)
610 struct tog_view *view = calloc(1, sizeof(*view));
618 view->nlines = nlines ? nlines : LINES - begin_y;
619 view->ncols = ncols ? ncols : COLS - begin_x;
620 view->begin_y = begin_y;
621 view->begin_x = begin_x;
622 view->window = newwin(nlines, ncols, begin_y, begin_x);
623 if (view->window == NULL) {
627 view->panel = new_panel(view->window);
628 if (view->panel == NULL ||
629 set_panel_userptr(view->panel, view) != OK) {
634 keypad(view->window, TRUE);
639 view_split_begin_x(int begin_x)
641 if (begin_x > 0 || COLS < 120)
643 return (COLS - MAX(COLS / 2, 80));
646 static const struct got_error *view_resize(struct tog_view *);
648 static const struct got_error *
649 view_splitscreen(struct tog_view *view)
651 const struct got_error *err = NULL;
654 view->begin_x = view_split_begin_x(0);
655 view->nlines = LINES;
656 view->ncols = COLS - view->begin_x;
659 err = view_resize(view);
663 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
664 return got_error_from_errno("mvwin");
669 static const struct got_error *
670 view_fullscreen(struct tog_view *view)
672 const struct got_error *err = NULL;
676 view->nlines = LINES;
680 err = view_resize(view);
684 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
685 return got_error_from_errno("mvwin");
691 view_is_parent_view(struct tog_view *view)
693 return view->parent == NULL;
696 static const struct got_error *
697 view_resize(struct tog_view *view)
701 if (view->lines > LINES)
702 nlines = view->nlines - (view->lines - LINES);
704 nlines = view->nlines + (LINES - view->lines);
706 if (view->cols > COLS)
707 ncols = view->ncols - (view->cols - COLS);
709 ncols = view->ncols + (COLS - view->cols);
711 if (wresize(view->window, nlines, ncols) == ERR)
712 return got_error_from_errno("wresize");
713 if (replace_panel(view->panel, view->window) == ERR)
714 return got_error_from_errno("replace_panel");
715 wclear(view->window);
717 view->nlines = nlines;
723 view->child->begin_x = view_split_begin_x(view->begin_x);
724 if (view->child->begin_x == 0) {
725 view_fullscreen(view->child);
726 if (view->child->focussed)
727 show_panel(view->child->panel);
729 show_panel(view->panel);
731 view_splitscreen(view->child);
732 show_panel(view->child->panel);
739 static const struct got_error *
740 view_close_child(struct tog_view *view)
742 const struct got_error *err = NULL;
744 if (view->child == NULL)
747 err = view_close(view->child);
753 view_set_child(struct tog_view *view, struct tog_view *child)
756 child->parent = view;
760 view_is_splitscreen(struct tog_view *view)
762 return view->begin_x > 0;
771 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
772 cols = 80; /* Default */
778 resize_term(lines, cols);
781 static const struct got_error *
782 view_search_start(struct tog_view *view)
784 const struct got_error *err = NULL;
788 if (view->search_started) {
789 regfree(&view->regex);
791 memset(&view->regmatch, 0, sizeof(view->regmatch));
793 view->search_started = 0;
795 if (view->nlines < 1)
798 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
799 wclrtoeol(view->window);
803 ret = wgetnstr(view->window, pattern, sizeof(pattern));
809 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
810 err = view->search_start(view);
812 regfree(&view->regex);
815 view->search_started = 1;
816 view->searching = TOG_SEARCH_FORWARD;
817 view->search_next_done = 0;
818 view->search_next(view);
824 static const struct got_error *
825 view_input(struct tog_view **new, int *done, struct tog_view *view,
826 struct tog_view_list_head *views)
828 const struct got_error *err = NULL;
834 /* Clear "no matches" indicator. */
835 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
836 view->search_next_done == TOG_SEARCH_HAVE_NONE)
837 view->search_next_done = TOG_SEARCH_HAVE_MORE;
839 if (view->searching && !view->search_next_done) {
840 errcode = pthread_mutex_unlock(&tog_mutex);
842 return got_error_set_errno(errcode,
843 "pthread_mutex_unlock");
845 errcode = pthread_mutex_lock(&tog_mutex);
847 return got_error_set_errno(errcode,
848 "pthread_mutex_lock");
849 view->search_next(view);
853 nodelay(stdscr, FALSE);
854 /* Allow threads to make progress while we are waiting for input. */
855 errcode = pthread_mutex_unlock(&tog_mutex);
857 return got_error_set_errno(errcode, "pthread_mutex_unlock");
858 ch = wgetch(view->window);
859 errcode = pthread_mutex_lock(&tog_mutex);
861 return got_error_set_errno(errcode, "pthread_mutex_lock");
862 nodelay(stdscr, TRUE);
864 if (tog_sigwinch_received || tog_sigcont_received) {
866 tog_sigwinch_received = 0;
867 tog_sigcont_received = 0;
868 TAILQ_FOREACH(v, views, entry) {
869 err = view_resize(v);
872 err = v->input(new, v, KEY_RESIZE);
876 err = view_resize(v->child);
879 err = v->child->input(new, v->child,
893 view->child->focussed = 1;
894 view->focus_child = 1;
895 } else if (view->parent) {
897 view->parent->focussed = 1;
898 view->parent->focus_child = 0;
902 err = view->input(new, view, ch);
909 if (view_is_parent_view(view)) {
910 if (view->child == NULL)
912 if (view_is_splitscreen(view->child)) {
914 view->child->focussed = 1;
915 err = view_fullscreen(view->child);
917 err = view_splitscreen(view->child);
920 err = view->child->input(new, view->child,
923 if (view_is_splitscreen(view)) {
924 view->parent->focussed = 0;
926 err = view_fullscreen(view);
928 err = view_splitscreen(view);
932 err = view->input(new, view, KEY_RESIZE);
938 if (view->search_start)
939 view_search_start(view);
941 err = view->input(new, view, ch);
945 if (view->search_started && view->search_next) {
946 view->searching = (ch == 'n' ?
947 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
948 view->search_next_done = 0;
949 view->search_next(view);
951 err = view->input(new, view, ch);
954 err = view->input(new, view, ch);
962 view_vborder(struct tog_view *view)
965 const struct tog_view *view_above;
968 return view_vborder(view->parent);
970 panel = panel_above(view->panel);
974 view_above = panel_userptr(panel);
975 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
976 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
980 view_needs_focus_indication(struct tog_view *view)
982 if (view_is_parent_view(view)) {
983 if (view->child == NULL || view->child->focussed)
985 if (!view_is_splitscreen(view->child))
987 } else if (!view_is_splitscreen(view))
990 return view->focussed;
993 static const struct got_error *
994 view_loop(struct tog_view *view)
996 const struct got_error *err = NULL;
997 struct tog_view_list_head views;
998 struct tog_view *new_view;
999 int fast_refresh = 10;
1000 int done = 0, errcode;
1002 errcode = pthread_mutex_lock(&tog_mutex);
1004 return got_error_set_errno(errcode, "pthread_mutex_lock");
1007 TAILQ_INSERT_HEAD(&views, view, entry);
1010 err = view->show(view);
1015 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1016 /* Refresh fast during initialization, then become slower. */
1017 if (fast_refresh && fast_refresh-- == 0)
1018 halfdelay(10); /* switch to once per second */
1020 err = view_input(&new_view, &done, view, &views);
1024 struct tog_view *v, *prev = NULL;
1026 if (view_is_parent_view(view))
1027 prev = TAILQ_PREV(view, tog_view_list_head,
1029 else if (view->parent)
1030 prev = view->parent;
1033 view->parent->child = NULL;
1034 view->parent->focus_child = 0;
1036 TAILQ_REMOVE(&views, view, entry);
1038 err = view_close(view);
1043 TAILQ_FOREACH(v, &views, entry) {
1047 if (view == NULL && new_view == NULL) {
1048 /* No view has focus. Try to pick one. */
1051 else if (!TAILQ_EMPTY(&views)) {
1052 view = TAILQ_LAST(&views,
1053 tog_view_list_head);
1056 if (view->focus_child) {
1057 view->child->focussed = 1;
1065 struct tog_view *v, *t;
1066 /* Only allow one parent view per type. */
1067 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1068 if (v->type != new_view->type)
1070 TAILQ_REMOVE(&views, v, entry);
1071 err = view_close(v);
1076 TAILQ_INSERT_TAIL(&views, new_view, entry);
1080 if (view_is_parent_view(view)) {
1081 if (view->child && view->child->focussed)
1084 if (view->parent && view->parent->focussed)
1085 view = view->parent;
1087 show_panel(view->panel);
1088 if (view->child && view_is_splitscreen(view->child))
1089 show_panel(view->child->panel);
1090 if (view->parent && view_is_splitscreen(view)) {
1091 err = view->parent->show(view->parent);
1095 err = view->show(view);
1099 err = view->child->show(view->child);
1108 while (!TAILQ_EMPTY(&views)) {
1109 view = TAILQ_FIRST(&views);
1110 TAILQ_REMOVE(&views, view, entry);
1114 errcode = pthread_mutex_unlock(&tog_mutex);
1115 if (errcode && err == NULL)
1116 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1126 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1131 /* Create newly allocated wide-character string equivalent to a byte string. */
1132 static const struct got_error *
1133 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1136 const struct got_error *err = NULL;
1139 *wlen = mbstowcs(NULL, s, 0);
1140 if (*wlen == (size_t)-1) {
1142 if (errno != EILSEQ)
1143 return got_error_from_errno("mbstowcs");
1145 /* byte string invalid in current encoding; try to "fix" it */
1146 err = got_mbsavis(&vis, &vislen, s);
1149 *wlen = mbstowcs(NULL, vis, 0);
1150 if (*wlen == (size_t)-1) {
1151 err = got_error_from_errno("mbstowcs"); /* give up */
1156 *ws = calloc(*wlen + 1, sizeof(**ws));
1158 err = got_error_from_errno("calloc");
1162 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1163 err = got_error_from_errno("mbstowcs");
1174 /* Format a line for display, ensuring that it won't overflow a width limit. */
1175 static const struct got_error *
1176 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1179 const struct got_error *err = NULL;
1181 wchar_t *wline = NULL;
1188 err = mbs2ws(&wline, &wlen, line);
1192 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1193 wline[wlen - 1] = L'\0';
1196 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1197 wline[wlen - 1] = L'\0';
1203 int width = wcwidth(wline[i]);
1210 if (width == 1 || width == 2) {
1211 if (cols + width > wlimit)
1215 } else if (width == -1) {
1216 if (wline[i] == L'\t') {
1218 ((cols + col_tab_align) % TABSIZE);
1223 if (cols + width > wlimit)
1228 err = got_error_from_errno("wcwidth");
1243 static const struct got_error*
1244 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1245 struct got_object_id *id, struct got_repository *repo)
1247 static const struct got_error *err = NULL;
1248 struct got_reflist_entry *re;
1254 TAILQ_FOREACH(re, refs, entry) {
1255 struct got_tag_object *tag = NULL;
1256 struct got_object_id *ref_id;
1259 name = got_ref_get_name(re->ref);
1260 if (strcmp(name, GOT_REF_HEAD) == 0)
1262 if (strncmp(name, "refs/", 5) == 0)
1264 if (strncmp(name, "got/", 4) == 0)
1266 if (strncmp(name, "heads/", 6) == 0)
1268 if (strncmp(name, "remotes/", 8) == 0) {
1270 s = strstr(name, "/" GOT_REF_HEAD);
1271 if (s != NULL && s[strlen(s)] == '\0')
1274 err = got_ref_resolve(&ref_id, repo, re->ref);
1277 if (strncmp(name, "tags/", 5) == 0) {
1278 err = got_object_open_as_tag(&tag, repo, ref_id);
1280 if (err->code != GOT_ERR_OBJ_TYPE) {
1284 /* Ref points at something other than a tag. */
1289 cmp = got_object_id_cmp(tag ?
1290 got_object_tag_get_object_id(tag) : ref_id, id);
1293 got_object_tag_close(tag);
1297 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1298 s ? ", " : "", name) == -1) {
1299 err = got_error_from_errno("asprintf");
1310 static const struct got_error *
1311 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1316 smallerthan = strchr(author, '<');
1317 if (smallerthan && smallerthan[1] != '\0')
1318 author = smallerthan + 1;
1319 author[strcspn(author, "@>")] = '\0';
1320 return format_line(wauthor, author_width, author, limit, col_tab_align);
1323 static const struct got_error *
1324 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1325 struct got_object_id *id, const size_t date_display_cols,
1326 int author_display_cols)
1328 struct tog_log_view_state *s = &view->state.log;
1329 const struct got_error *err = NULL;
1330 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1331 char *logmsg0 = NULL, *logmsg = NULL;
1332 char *author = NULL;
1333 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1334 int author_width, logmsg_width;
1335 char *newline, *line = NULL;
1337 const int avail = view->ncols;
1339 time_t committer_time;
1340 struct tog_color *tc;
1342 committer_time = got_object_commit_get_committer_time(commit);
1343 if (localtime_r(&committer_time, &tm) == NULL)
1344 return got_error_from_errno("localtime_r");
1345 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1346 return got_error(GOT_ERR_NO_SPACE);
1348 if (avail <= date_display_cols)
1349 limit = MIN(sizeof(datebuf) - 1, avail);
1351 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1352 tc = get_color(&s->colors, TOG_COLOR_DATE);
1354 wattr_on(view->window,
1355 COLOR_PAIR(tc->colorpair), NULL);
1356 waddnstr(view->window, datebuf, limit);
1358 wattr_off(view->window,
1359 COLOR_PAIR(tc->colorpair), NULL);
1366 err = got_object_id_str(&id_str, id);
1369 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1371 wattr_on(view->window,
1372 COLOR_PAIR(tc->colorpair), NULL);
1373 wprintw(view->window, "%.8s ", id_str);
1375 wattr_off(view->window,
1376 COLOR_PAIR(tc->colorpair), NULL);
1383 author = strdup(got_object_commit_get_author(commit));
1384 if (author == NULL) {
1385 err = got_error_from_errno("strdup");
1388 err = format_author(&wauthor, &author_width, author, avail - col, col);
1391 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1393 wattr_on(view->window,
1394 COLOR_PAIR(tc->colorpair), NULL);
1395 waddwstr(view->window, wauthor);
1397 wattr_off(view->window,
1398 COLOR_PAIR(tc->colorpair), NULL);
1399 col += author_width;
1400 while (col < avail && author_width < author_display_cols + 2) {
1401 waddch(view->window, ' ');
1408 err = got_object_commit_get_logmsg(&logmsg0, commit);
1412 while (*logmsg == '\n')
1414 newline = strchr(logmsg, '\n');
1417 limit = avail - col;
1418 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1421 waddwstr(view->window, wlogmsg);
1422 col += logmsg_width;
1423 while (col < avail) {
1424 waddch(view->window, ' ');
1436 static struct commit_queue_entry *
1437 alloc_commit_queue_entry(struct got_commit_object *commit,
1438 struct got_object_id *id)
1440 struct commit_queue_entry *entry;
1442 entry = calloc(1, sizeof(*entry));
1447 entry->commit = commit;
1452 pop_commit(struct commit_queue *commits)
1454 struct commit_queue_entry *entry;
1456 entry = TAILQ_FIRST(&commits->head);
1457 TAILQ_REMOVE(&commits->head, entry, entry);
1458 got_object_commit_close(entry->commit);
1459 commits->ncommits--;
1460 /* Don't free entry->id! It is owned by the commit graph. */
1465 free_commits(struct commit_queue *commits)
1467 while (!TAILQ_EMPTY(&commits->head))
1468 pop_commit(commits);
1471 static const struct got_error *
1472 match_commit(int *have_match, struct got_object_id *id,
1473 struct got_commit_object *commit, regex_t *regex)
1475 const struct got_error *err = NULL;
1476 regmatch_t regmatch;
1477 char *id_str = NULL, *logmsg = NULL;
1481 err = got_object_id_str(&id_str, id);
1485 err = got_object_commit_get_logmsg(&logmsg, commit);
1489 if (regexec(regex, got_object_commit_get_author(commit), 1,
1490 ®match, 0) == 0 ||
1491 regexec(regex, got_object_commit_get_committer(commit), 1,
1492 ®match, 0) == 0 ||
1493 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1494 regexec(regex, logmsg, 1, ®match, 0) == 0)
1502 static const struct got_error *
1503 queue_commits(struct tog_log_thread_args *a)
1505 const struct got_error *err = NULL;
1508 * We keep all commits open throughout the lifetime of the log
1509 * view in order to avoid having to re-fetch commits from disk
1510 * while updating the display.
1513 struct got_object_id *id;
1514 struct got_commit_object *commit;
1515 struct commit_queue_entry *entry;
1518 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1520 if (err || id == NULL)
1523 err = got_object_open_as_commit(&commit, a->repo, id);
1526 entry = alloc_commit_queue_entry(commit, id);
1527 if (entry == NULL) {
1528 err = got_error_from_errno("alloc_commit_queue_entry");
1532 errcode = pthread_mutex_lock(&tog_mutex);
1534 err = got_error_set_errno(errcode,
1535 "pthread_mutex_lock");
1539 entry->idx = a->commits->ncommits;
1540 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1541 a->commits->ncommits++;
1543 if (*a->searching == TOG_SEARCH_FORWARD &&
1544 !*a->search_next_done) {
1546 err = match_commit(&have_match, id, commit, a->regex);
1550 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1553 errcode = pthread_mutex_unlock(&tog_mutex);
1554 if (errcode && err == NULL)
1555 err = got_error_set_errno(errcode,
1556 "pthread_mutex_unlock");
1559 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1565 select_commit(struct tog_log_view_state *s)
1567 struct commit_queue_entry *entry;
1570 entry = s->first_displayed_entry;
1572 if (ncommits == s->selected) {
1573 s->selected_entry = entry;
1576 entry = TAILQ_NEXT(entry, entry);
1581 static const struct got_error *
1582 draw_commits(struct tog_view *view)
1584 const struct got_error *err = NULL;
1585 struct tog_log_view_state *s = &view->state.log;
1586 struct commit_queue_entry *entry = s->selected_entry;
1587 const int limit = view->nlines;
1589 int ncommits, author_cols = 4;
1590 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1591 char *refs_str = NULL;
1593 struct tog_color *tc;
1594 static const size_t date_display_cols = 12;
1596 if (s->selected_entry &&
1597 !(view->searching && view->search_next_done == 0)) {
1598 struct got_reflist_head *refs;
1599 err = got_object_id_str(&id_str, s->selected_entry->id);
1602 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1603 s->selected_entry->id);
1605 err = build_refs_str(&refs_str, refs,
1606 s->selected_entry->id, s->repo);
1612 if (s->thread_args.commits_needed == 0)
1613 halfdelay(10); /* disable fast refresh */
1615 if (s->thread_args.commits_needed > 0) {
1616 if (asprintf(&ncommits_str, " [%d/%d] %s",
1617 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1618 (view->searching && !view->search_next_done) ?
1619 "searching..." : "loading...") == -1) {
1620 err = got_error_from_errno("asprintf");
1624 const char *search_str = NULL;
1626 if (view->searching) {
1627 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1628 search_str = "no more matches";
1629 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1630 search_str = "no matches found";
1631 else if (!view->search_next_done)
1632 search_str = "searching...";
1635 if (asprintf(&ncommits_str, " [%d/%d] %s",
1636 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1637 search_str ? search_str :
1638 (refs_str ? refs_str : "")) == -1) {
1639 err = got_error_from_errno("asprintf");
1644 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1645 if (asprintf(&header, "commit %s %s%s",
1646 id_str ? id_str : "........................................",
1647 s->in_repo_path, ncommits_str) == -1) {
1648 err = got_error_from_errno("asprintf");
1652 } else if (asprintf(&header, "commit %s%s",
1653 id_str ? id_str : "........................................",
1654 ncommits_str) == -1) {
1655 err = got_error_from_errno("asprintf");
1659 err = format_line(&wline, &width, header, view->ncols, 0);
1663 werase(view->window);
1665 if (view_needs_focus_indication(view))
1666 wstandout(view->window);
1667 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1669 wattr_on(view->window,
1670 COLOR_PAIR(tc->colorpair), NULL);
1671 waddwstr(view->window, wline);
1673 wattr_off(view->window,
1674 COLOR_PAIR(tc->colorpair), NULL);
1675 while (width < view->ncols) {
1676 waddch(view->window, ' ');
1679 if (view_needs_focus_indication(view))
1680 wstandend(view->window);
1685 /* Grow author column size if necessary. */
1686 entry = s->first_displayed_entry;
1692 if (ncommits >= limit - 1)
1694 author = strdup(got_object_commit_get_author(entry->commit));
1695 if (author == NULL) {
1696 err = got_error_from_errno("strdup");
1699 err = format_author(&wauthor, &width, author, COLS,
1701 if (author_cols < width)
1702 author_cols = width;
1706 entry = TAILQ_NEXT(entry, entry);
1709 entry = s->first_displayed_entry;
1710 s->last_displayed_entry = s->first_displayed_entry;
1713 if (ncommits >= limit - 1)
1715 if (ncommits == s->selected)
1716 wstandout(view->window);
1717 err = draw_commit(view, entry->commit, entry->id,
1718 date_display_cols, author_cols);
1719 if (ncommits == s->selected)
1720 wstandend(view->window);
1724 s->last_displayed_entry = entry;
1725 entry = TAILQ_NEXT(entry, entry);
1738 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1740 struct commit_queue_entry *entry;
1743 entry = TAILQ_FIRST(&s->commits.head);
1744 if (s->first_displayed_entry == entry)
1747 entry = s->first_displayed_entry;
1748 while (entry && nscrolled < maxscroll) {
1749 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1751 s->first_displayed_entry = entry;
1757 static const struct got_error *
1758 trigger_log_thread(struct tog_view *view, int wait)
1760 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1763 halfdelay(1); /* fast refresh while loading commits */
1765 while (ta->commits_needed > 0) {
1766 if (ta->log_complete)
1769 /* Wake the log thread. */
1770 errcode = pthread_cond_signal(&ta->need_commits);
1772 return got_error_set_errno(errcode,
1773 "pthread_cond_signal");
1776 * The mutex will be released while the view loop waits
1777 * in wgetch(), at which time the log thread will run.
1782 /* Display progress update in log view. */
1783 show_log_view(view);
1787 /* Wait right here while next commit is being loaded. */
1788 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1790 return got_error_set_errno(errcode,
1791 "pthread_cond_wait");
1793 /* Display progress update in log view. */
1794 show_log_view(view);
1802 static const struct got_error *
1803 log_scroll_down(struct tog_view *view, int maxscroll)
1805 struct tog_log_view_state *s = &view->state.log;
1806 const struct got_error *err = NULL;
1807 struct commit_queue_entry *pentry;
1808 int nscrolled = 0, ncommits_needed;
1810 if (s->last_displayed_entry == NULL)
1813 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1814 if (s->commits.ncommits < ncommits_needed &&
1815 !s->thread_args.log_complete) {
1817 * Ask the log thread for required amount of commits.
1819 s->thread_args.commits_needed += maxscroll;
1820 err = trigger_log_thread(view, 1);
1826 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1830 s->last_displayed_entry = pentry;
1832 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1835 s->first_displayed_entry = pentry;
1836 } while (++nscrolled < maxscroll);
1841 static const struct got_error *
1842 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1843 struct got_commit_object *commit, struct got_object_id *commit_id,
1844 struct tog_view *log_view, struct got_repository *repo)
1846 const struct got_error *err;
1847 struct got_object_qid *parent_id;
1848 struct tog_view *diff_view;
1850 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1851 if (diff_view == NULL)
1852 return got_error_from_errno("view_open");
1854 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1855 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1856 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1858 *new_view = diff_view;
1862 static const struct got_error *
1863 tree_view_visit_subtree(struct tog_tree_view_state *s,
1864 struct got_tree_object *subtree)
1866 struct tog_parent_tree *parent;
1868 parent = calloc(1, sizeof(*parent));
1870 return got_error_from_errno("calloc");
1872 parent->tree = s->tree;
1873 parent->first_displayed_entry = s->first_displayed_entry;
1874 parent->selected_entry = s->selected_entry;
1875 parent->selected = s->selected;
1876 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1879 s->first_displayed_entry = NULL;
1883 static const struct got_error *
1884 tree_view_walk_path(struct tog_tree_view_state *s,
1885 struct got_object_id *commit_id, const char *path)
1887 const struct got_error *err = NULL;
1888 struct got_tree_object *tree = NULL;
1890 char *slash, *subpath = NULL;
1892 /* Walk the path and open corresponding tree objects. */
1895 struct got_tree_entry *te;
1896 struct got_object_id *tree_id;
1902 /* Ensure the correct subtree entry is selected. */
1903 slash = strchr(p, '/');
1905 te_name = strdup(p);
1907 te_name = strndup(p, slash - p);
1908 if (te_name == NULL) {
1909 err = got_error_from_errno("strndup");
1912 te = got_object_tree_find_entry(s->tree, te_name);
1914 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1919 s->first_displayed_entry = s->selected_entry = te;
1921 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1922 break; /* jump to this file's entry */
1924 slash = strchr(p, '/');
1926 subpath = strndup(path, slash - path);
1928 subpath = strdup(path);
1929 if (subpath == NULL) {
1930 err = got_error_from_errno("strdup");
1934 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1939 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1944 err = tree_view_visit_subtree(s, tree);
1946 got_object_tree_close(tree);
1960 static const struct got_error *
1961 browse_commit_tree(struct tog_view **new_view, int begin_x,
1962 struct commit_queue_entry *entry, const char *path,
1963 const char *head_ref_name, struct got_repository *repo)
1965 const struct got_error *err = NULL;
1966 struct tog_tree_view_state *s;
1967 struct tog_view *tree_view;
1969 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1970 if (tree_view == NULL)
1971 return got_error_from_errno("view_open");
1973 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
1976 s = &tree_view->state.tree;
1978 *new_view = tree_view;
1980 if (got_path_is_root_dir(path))
1983 return tree_view_walk_path(s, entry->id, path);
1986 static const struct got_error *
1987 block_signals_used_by_main_thread(void)
1992 if (sigemptyset(&sigset) == -1)
1993 return got_error_from_errno("sigemptyset");
1995 /* tog handles SIGWINCH and SIGCONT */
1996 if (sigaddset(&sigset, SIGWINCH) == -1)
1997 return got_error_from_errno("sigaddset");
1998 if (sigaddset(&sigset, SIGCONT) == -1)
1999 return got_error_from_errno("sigaddset");
2001 /* ncurses handles SIGTSTP */
2002 if (sigaddset(&sigset, SIGTSTP) == -1)
2003 return got_error_from_errno("sigaddset");
2005 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2007 return got_error_set_errno(errcode, "pthread_sigmask");
2013 log_thread(void *arg)
2015 const struct got_error *err = NULL;
2017 struct tog_log_thread_args *a = arg;
2020 err = block_signals_used_by_main_thread();
2024 while (!done && !err && !tog_sigpipe_received) {
2025 err = queue_commits(a);
2027 if (err->code != GOT_ERR_ITER_COMPLETED)
2031 } else if (a->commits_needed > 0)
2032 a->commits_needed--;
2034 errcode = pthread_mutex_lock(&tog_mutex);
2036 err = got_error_set_errno(errcode,
2037 "pthread_mutex_lock");
2039 } else if (*a->quit)
2041 else if (*a->first_displayed_entry == NULL) {
2042 *a->first_displayed_entry =
2043 TAILQ_FIRST(&a->commits->head);
2044 *a->selected_entry = *a->first_displayed_entry;
2047 errcode = pthread_cond_signal(&a->commit_loaded);
2049 err = got_error_set_errno(errcode,
2050 "pthread_cond_signal");
2051 pthread_mutex_unlock(&tog_mutex);
2056 a->commits_needed = 0;
2058 if (a->commits_needed == 0) {
2059 errcode = pthread_cond_wait(&a->need_commits,
2062 err = got_error_set_errno(errcode,
2063 "pthread_cond_wait");
2069 errcode = pthread_mutex_unlock(&tog_mutex);
2070 if (errcode && err == NULL)
2071 err = got_error_set_errno(errcode,
2072 "pthread_mutex_unlock");
2074 a->log_complete = 1;
2078 static const struct got_error *
2079 stop_log_thread(struct tog_log_view_state *s)
2081 const struct got_error *err = NULL;
2086 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2088 return got_error_set_errno(errcode,
2089 "pthread_cond_signal");
2090 errcode = pthread_mutex_unlock(&tog_mutex);
2092 return got_error_set_errno(errcode,
2093 "pthread_mutex_unlock");
2094 errcode = pthread_join(s->thread, (void **)&err);
2096 return got_error_set_errno(errcode, "pthread_join");
2097 errcode = pthread_mutex_lock(&tog_mutex);
2099 return got_error_set_errno(errcode,
2100 "pthread_mutex_lock");
2104 if (s->thread_args.repo) {
2105 err = got_repo_close(s->thread_args.repo);
2106 s->thread_args.repo = NULL;
2109 if (s->thread_args.graph) {
2110 got_commit_graph_close(s->thread_args.graph);
2111 s->thread_args.graph = NULL;
2117 static const struct got_error *
2118 close_log_view(struct tog_view *view)
2120 const struct got_error *err = NULL;
2121 struct tog_log_view_state *s = &view->state.log;
2124 err = stop_log_thread(s);
2126 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2127 if (errcode && err == NULL)
2128 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2130 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2131 if (errcode && err == NULL)
2132 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2134 free_commits(&s->commits);
2135 free(s->in_repo_path);
2136 s->in_repo_path = NULL;
2139 free(s->head_ref_name);
2140 s->head_ref_name = NULL;
2144 static const struct got_error *
2145 search_start_log_view(struct tog_view *view)
2147 struct tog_log_view_state *s = &view->state.log;
2149 s->matched_entry = NULL;
2150 s->search_entry = NULL;
2154 static const struct got_error *
2155 search_next_log_view(struct tog_view *view)
2157 const struct got_error *err = NULL;
2158 struct tog_log_view_state *s = &view->state.log;
2159 struct commit_queue_entry *entry;
2161 /* Display progress update in log view. */
2162 show_log_view(view);
2166 if (s->search_entry) {
2168 errcode = pthread_mutex_unlock(&tog_mutex);
2170 return got_error_set_errno(errcode,
2171 "pthread_mutex_unlock");
2172 ch = wgetch(view->window);
2173 errcode = pthread_mutex_lock(&tog_mutex);
2175 return got_error_set_errno(errcode,
2176 "pthread_mutex_lock");
2177 if (ch == KEY_BACKSPACE) {
2178 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2181 if (view->searching == TOG_SEARCH_FORWARD)
2182 entry = TAILQ_NEXT(s->search_entry, entry);
2184 entry = TAILQ_PREV(s->search_entry,
2185 commit_queue_head, entry);
2186 } else if (s->matched_entry) {
2187 if (view->searching == TOG_SEARCH_FORWARD)
2188 entry = TAILQ_NEXT(s->matched_entry, entry);
2190 entry = TAILQ_PREV(s->matched_entry,
2191 commit_queue_head, entry);
2193 if (view->searching == TOG_SEARCH_FORWARD)
2194 entry = TAILQ_FIRST(&s->commits.head);
2196 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2202 if (entry == NULL) {
2203 if (s->thread_args.log_complete ||
2204 view->searching == TOG_SEARCH_BACKWARD) {
2205 view->search_next_done =
2206 (s->matched_entry == NULL ?
2207 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2208 s->search_entry = NULL;
2212 * Poke the log thread for more commits and return,
2213 * allowing the main loop to make progress. Search
2214 * will resume at s->search_entry once we come back.
2216 s->thread_args.commits_needed++;
2217 return trigger_log_thread(view, 0);
2220 err = match_commit(&have_match, entry->id, entry->commit,
2225 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2226 s->matched_entry = entry;
2230 s->search_entry = entry;
2231 if (view->searching == TOG_SEARCH_FORWARD)
2232 entry = TAILQ_NEXT(entry, entry);
2234 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2237 if (s->matched_entry) {
2238 int cur = s->selected_entry->idx;
2239 while (cur < s->matched_entry->idx) {
2240 err = input_log_view(NULL, view, KEY_DOWN);
2245 while (cur > s->matched_entry->idx) {
2246 err = input_log_view(NULL, view, KEY_UP);
2253 s->search_entry = NULL;
2258 static const struct got_error *
2259 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2260 struct got_repository *repo, const char *head_ref_name,
2261 const char *in_repo_path, int log_branches)
2263 const struct got_error *err = NULL;
2264 struct tog_log_view_state *s = &view->state.log;
2265 struct got_repository *thread_repo = NULL;
2266 struct got_commit_graph *thread_graph = NULL;
2269 if (in_repo_path != s->in_repo_path) {
2270 free(s->in_repo_path);
2271 s->in_repo_path = strdup(in_repo_path);
2272 if (s->in_repo_path == NULL)
2273 return got_error_from_errno("strdup");
2276 /* The commit queue only contains commits being displayed. */
2277 TAILQ_INIT(&s->commits.head);
2278 s->commits.ncommits = 0;
2281 if (head_ref_name) {
2282 s->head_ref_name = strdup(head_ref_name);
2283 if (s->head_ref_name == NULL) {
2284 err = got_error_from_errno("strdup");
2288 s->start_id = got_object_id_dup(start_id);
2289 if (s->start_id == NULL) {
2290 err = got_error_from_errno("got_object_id_dup");
2293 s->log_branches = log_branches;
2295 STAILQ_INIT(&s->colors);
2296 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2297 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2298 get_color_value("TOG_COLOR_COMMIT"));
2301 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2302 get_color_value("TOG_COLOR_AUTHOR"));
2304 free_colors(&s->colors);
2307 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2308 get_color_value("TOG_COLOR_DATE"));
2310 free_colors(&s->colors);
2315 view->show = show_log_view;
2316 view->input = input_log_view;
2317 view->close = close_log_view;
2318 view->search_start = search_start_log_view;
2319 view->search_next = search_next_log_view;
2321 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2324 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2328 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2329 s->repo, NULL, NULL);
2333 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2335 err = got_error_set_errno(errcode, "pthread_cond_init");
2338 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2340 err = got_error_set_errno(errcode, "pthread_cond_init");
2344 s->thread_args.commits_needed = view->nlines;
2345 s->thread_args.graph = thread_graph;
2346 s->thread_args.commits = &s->commits;
2347 s->thread_args.in_repo_path = s->in_repo_path;
2348 s->thread_args.start_id = s->start_id;
2349 s->thread_args.repo = thread_repo;
2350 s->thread_args.log_complete = 0;
2351 s->thread_args.quit = &s->quit;
2352 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2353 s->thread_args.selected_entry = &s->selected_entry;
2354 s->thread_args.searching = &view->searching;
2355 s->thread_args.search_next_done = &view->search_next_done;
2356 s->thread_args.regex = &view->regex;
2359 close_log_view(view);
2363 static const struct got_error *
2364 show_log_view(struct tog_view *view)
2366 const struct got_error *err;
2367 struct tog_log_view_state *s = &view->state.log;
2369 if (s->thread == NULL) {
2370 int errcode = pthread_create(&s->thread, NULL, log_thread,
2373 return got_error_set_errno(errcode, "pthread_create");
2374 if (s->thread_args.commits_needed > 0) {
2375 err = trigger_log_thread(view, 1);
2381 return draw_commits(view);
2384 static const struct got_error *
2385 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2387 const struct got_error *err = NULL;
2388 struct tog_log_view_state *s = &view->state.log;
2389 struct tog_view *diff_view = NULL, *tree_view = NULL;
2390 struct tog_view *ref_view = NULL;
2401 if (s->first_displayed_entry == NULL)
2403 if (s->selected > 0)
2406 log_scroll_up(s, 1);
2411 if (s->first_displayed_entry == NULL)
2413 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2416 log_scroll_up(s, view->nlines - 1);
2423 if (s->first_displayed_entry == NULL)
2425 if (s->selected < MIN(view->nlines - 2,
2426 s->commits.ncommits - 1))
2429 err = log_scroll_down(view, 1);
2437 struct commit_queue_entry *first;
2438 first = s->first_displayed_entry;
2441 err = log_scroll_down(view, view->nlines - 1);
2444 if (first == s->first_displayed_entry &&
2445 s->selected < MIN(view->nlines - 2,
2446 s->commits.ncommits - 1)) {
2447 /* can't scroll further down */
2448 s->selected = MIN(view->nlines - 2,
2449 s->commits.ncommits - 1);
2455 if (s->selected > view->nlines - 2)
2456 s->selected = view->nlines - 2;
2457 if (s->selected > s->commits.ncommits - 1)
2458 s->selected = s->commits.ncommits - 1;
2460 if (s->commits.ncommits < view->nlines - 1 &&
2461 !s->thread_args.log_complete) {
2462 s->thread_args.commits_needed += (view->nlines - 1) -
2463 s->commits.ncommits;
2464 err = trigger_log_thread(view, 1);
2470 if (s->selected_entry == NULL)
2472 if (view_is_parent_view(view))
2473 begin_x = view_split_begin_x(view->begin_x);
2474 err = open_diff_view_for_commit(&diff_view, begin_x,
2475 s->selected_entry->commit, s->selected_entry->id,
2480 diff_view->focussed = 1;
2481 if (view_is_parent_view(view)) {
2482 err = view_close_child(view);
2485 view_set_child(view, diff_view);
2486 view->focus_child = 1;
2488 *new_view = diff_view;
2491 if (s->selected_entry == NULL)
2493 if (view_is_parent_view(view))
2494 begin_x = view_split_begin_x(view->begin_x);
2495 err = browse_commit_tree(&tree_view, begin_x,
2496 s->selected_entry, s->in_repo_path, s->head_ref_name,
2501 tree_view->focussed = 1;
2502 if (view_is_parent_view(view)) {
2503 err = view_close_child(view);
2506 view_set_child(view, tree_view);
2507 view->focus_child = 1;
2509 *new_view = tree_view;
2514 if (ch == KEY_BACKSPACE &&
2515 got_path_is_root_dir(s->in_repo_path))
2517 err = stop_log_thread(s);
2520 if (ch == KEY_BACKSPACE) {
2522 err = got_path_dirname(&parent_path, s->in_repo_path);
2525 free(s->in_repo_path);
2526 s->in_repo_path = parent_path;
2527 s->thread_args.in_repo_path = s->in_repo_path;
2528 } else if (ch == CTRL('l')) {
2529 struct got_object_id *start_id;
2530 err = got_repo_match_object_id(&start_id, NULL,
2531 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2532 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2536 s->start_id = start_id;
2537 s->thread_args.start_id = s->start_id;
2539 s->log_branches = !s->log_branches;
2541 err = got_repo_open(&s->thread_args.repo,
2542 got_repo_get_path(s->repo), NULL);
2546 err = tog_load_refs(s->repo);
2549 err = got_commit_graph_open(&s->thread_args.graph,
2550 s->in_repo_path, !s->log_branches);
2553 err = got_commit_graph_iter_start(s->thread_args.graph,
2554 s->start_id, s->repo, NULL, NULL);
2557 free_commits(&s->commits);
2558 s->first_displayed_entry = NULL;
2559 s->last_displayed_entry = NULL;
2560 s->selected_entry = NULL;
2562 s->thread_args.log_complete = 0;
2564 s->thread_args.commits_needed = view->nlines;
2567 if (view_is_parent_view(view))
2568 begin_x = view_split_begin_x(view->begin_x);
2569 ref_view = view_open(view->nlines, view->ncols,
2570 view->begin_y, begin_x, TOG_VIEW_REF);
2571 if (ref_view == NULL)
2572 return got_error_from_errno("view_open");
2573 err = open_ref_view(ref_view, s->repo);
2575 view_close(ref_view);
2579 ref_view->focussed = 1;
2580 if (view_is_parent_view(view)) {
2581 err = view_close_child(view);
2584 view_set_child(view, ref_view);
2585 view->focus_child = 1;
2587 *new_view = ref_view;
2596 static const struct got_error *
2597 apply_unveil(const char *repo_path, const char *worktree_path)
2599 const struct got_error *error;
2602 if (unveil("gmon.out", "rwc") != 0)
2603 return got_error_from_errno2("unveil", "gmon.out");
2605 if (repo_path && unveil(repo_path, "r") != 0)
2606 return got_error_from_errno2("unveil", repo_path);
2608 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2609 return got_error_from_errno2("unveil", worktree_path);
2611 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2612 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2614 error = got_privsep_unveil_exec_helpers();
2618 if (unveil(NULL, NULL) != 0)
2619 return got_error_from_errno("unveil");
2629 halfdelay(1); /* Do fast refresh while initial view is loading. */
2632 intrflush(stdscr, FALSE);
2633 keypad(stdscr, TRUE);
2635 if (getenv("TOG_COLORS") != NULL) {
2637 use_default_colors();
2639 signal(SIGWINCH, tog_sigwinch);
2640 signal(SIGPIPE, tog_sigpipe);
2641 signal(SIGCONT, tog_sigcont);
2644 static const struct got_error *
2645 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2646 struct got_repository *repo, struct got_worktree *worktree)
2648 const struct got_error *err = NULL;
2651 *in_repo_path = strdup("/");
2652 if (*in_repo_path == NULL)
2653 return got_error_from_errno("strdup");
2658 const char *prefix = got_worktree_get_path_prefix(worktree);
2661 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2664 if (asprintf(in_repo_path, "%s%s%s", prefix,
2665 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2667 err = got_error_from_errno("asprintf");
2668 *in_repo_path = NULL;
2672 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2677 static const struct got_error *
2678 cmd_log(int argc, char *argv[])
2680 const struct got_error *error;
2681 struct got_repository *repo = NULL;
2682 struct got_worktree *worktree = NULL;
2683 struct got_object_id *start_id = NULL;
2684 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2685 char *start_commit = NULL, *label = NULL;
2686 struct got_reference *ref = NULL;
2687 const char *head_ref_name = NULL;
2688 int ch, log_branches = 0;
2689 struct tog_view *view;
2691 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2697 start_commit = optarg;
2700 repo_path = realpath(optarg, NULL);
2701 if (repo_path == NULL)
2702 return got_error_from_errno2("realpath",
2717 if (repo_path == NULL) {
2718 cwd = getcwd(NULL, 0);
2720 return got_error_from_errno("getcwd");
2721 error = got_worktree_open(&worktree, cwd);
2722 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2726 strdup(got_worktree_get_repo_path(worktree));
2728 repo_path = strdup(cwd);
2729 if (repo_path == NULL) {
2730 error = got_error_from_errno("strdup");
2735 error = got_repo_open(&repo, repo_path, NULL);
2739 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2746 error = apply_unveil(got_repo_get_path(repo),
2747 worktree ? got_worktree_get_root_path(worktree) : NULL);
2751 /* already loaded by tog_log_with_path()? */
2752 if (TAILQ_EMPTY(&tog_refs)) {
2753 error = tog_load_refs(repo);
2758 if (start_commit == NULL) {
2759 error = got_repo_match_object_id(&start_id, &label,
2760 worktree ? got_worktree_get_head_ref_name(worktree) :
2761 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2764 head_ref_name = label;
2766 error = got_ref_open(&ref, repo, start_commit, 0);
2768 head_ref_name = got_ref_get_name(ref);
2769 else if (error->code != GOT_ERR_NOT_REF)
2771 error = got_repo_match_object_id(&start_id, NULL,
2772 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2777 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2779 error = got_error_from_errno("view_open");
2782 error = open_log_view(view, start_id, repo, head_ref_name,
2783 in_repo_path, log_branches);
2787 /* Release work tree lock. */
2788 got_worktree_close(worktree);
2791 error = view_loop(view);
2801 const struct got_error *close_err = got_repo_close(repo);
2806 got_worktree_close(worktree);
2815 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2816 "[-w] object1 object2\n", getprogname());
2821 match_line(const char *line, regex_t *regex, size_t nmatch,
2822 regmatch_t *regmatch)
2824 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2828 match_color(struct tog_colors *colors, const char *line)
2830 struct tog_color *tc = NULL;
2832 STAILQ_FOREACH(tc, colors, entry) {
2833 if (match_line(line, &tc->regex, 0, NULL))
2840 static const struct got_error *
2841 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2842 WINDOW *window, regmatch_t *regmatch)
2844 const struct got_error *err = NULL;
2851 s = strndup(line, regmatch->rm_so);
2853 return got_error_from_errno("strndup");
2855 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2860 waddwstr(window, wline);
2867 s = strndup(line + regmatch->rm_so,
2868 regmatch->rm_eo - regmatch->rm_so);
2870 err = got_error_from_errno("strndup");
2874 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2879 wattr_on(window, A_STANDOUT, NULL);
2880 waddwstr(window, wline);
2881 wattr_off(window, A_STANDOUT, NULL);
2888 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2889 err = format_line(&wline, &width,
2890 line + regmatch->rm_eo, wlimit, col_tab_align);
2893 waddwstr(window, wline);
2901 static const struct got_error *
2902 draw_file(struct tog_view *view, const char *header)
2904 struct tog_diff_view_state *s = &view->state.diff;
2905 regmatch_t *regmatch = &view->regmatch;
2906 const struct got_error *err;
2909 size_t linesize = 0;
2911 struct tog_color *tc;
2914 int max_lines = view->nlines;
2915 int nlines = s->nlines;
2918 line_offset = s->line_offsets[s->first_displayed_line - 1];
2919 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2920 return got_error_from_errno("fseek");
2922 werase(view->window);
2925 if (asprintf(&line, "[%d/%d] %s",
2926 s->first_displayed_line - 1 + s->selected_line, nlines,
2928 return got_error_from_errno("asprintf");
2929 err = format_line(&wline, &width, line, view->ncols, 0);
2934 if (view_needs_focus_indication(view))
2935 wstandout(view->window);
2936 waddwstr(view->window, wline);
2939 if (view_needs_focus_indication(view))
2940 wstandend(view->window);
2941 if (width <= view->ncols - 1)
2942 waddch(view->window, '\n');
2951 while (max_lines > 0 && nprinted < max_lines) {
2952 linelen = getline(&line, &linesize, s->f);
2953 if (linelen == -1) {
2959 return got_ferror(s->f, GOT_ERR_IO);
2962 tc = match_color(&s->colors, line);
2964 wattr_on(view->window,
2965 COLOR_PAIR(tc->colorpair), NULL);
2966 if (s->first_displayed_line + nprinted == s->matched_line &&
2967 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2968 err = add_matched_line(&width, line, view->ncols, 0,
2969 view->window, regmatch);
2975 err = format_line(&wline, &width, line, view->ncols, 0);
2980 waddwstr(view->window, wline);
2985 wattr_off(view->window,
2986 COLOR_PAIR(tc->colorpair), NULL);
2987 if (width <= view->ncols - 1)
2988 waddch(view->window, '\n');
2993 s->last_displayed_line = s->first_displayed_line +
2996 s->last_displayed_line = s->first_displayed_line;
3001 while (nprinted < view->nlines) {
3002 waddch(view->window, '\n');
3006 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3011 wstandout(view->window);
3012 waddwstr(view->window, wline);
3015 wstandend(view->window);
3022 get_datestr(time_t *time, char *datebuf)
3024 struct tm mytm, *tm;
3027 tm = gmtime_r(time, &mytm);
3030 s = asctime_r(tm, datebuf);
3033 p = strchr(s, '\n');
3039 static const struct got_error *
3040 get_changed_paths(struct got_pathlist_head *paths,
3041 struct got_commit_object *commit, struct got_repository *repo)
3043 const struct got_error *err = NULL;
3044 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3045 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3046 struct got_object_qid *qid;
3048 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3050 struct got_commit_object *pcommit;
3051 err = got_object_open_as_commit(&pcommit, repo,
3056 tree_id1 = got_object_commit_get_tree_id(pcommit);
3057 got_object_commit_close(pcommit);
3062 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3067 tree_id2 = got_object_commit_get_tree_id(commit);
3068 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3072 err = got_diff_tree(tree1, tree2, "", "", repo,
3073 got_diff_tree_collect_changed_paths, paths, 0);
3076 got_object_tree_close(tree1);
3078 got_object_tree_close(tree2);
3082 static const struct got_error *
3083 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3087 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3089 return got_error_from_errno("reallocarray");
3091 (*line_offsets)[*nlines] = off;
3096 static const struct got_error *
3097 write_commit_info(off_t **line_offsets, size_t *nlines,
3098 struct got_object_id *commit_id, struct got_reflist_head *refs,
3099 struct got_repository *repo, FILE *outfile)
3101 const struct got_error *err = NULL;
3102 char datebuf[26], *datestr;
3103 struct got_commit_object *commit;
3104 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3105 time_t committer_time;
3106 const char *author, *committer;
3107 char *refs_str = NULL;
3108 struct got_pathlist_head changed_paths;
3109 struct got_pathlist_entry *pe;
3113 TAILQ_INIT(&changed_paths);
3116 err = build_refs_str(&refs_str, refs, commit_id, repo);
3121 err = got_object_open_as_commit(&commit, repo, commit_id);
3125 err = got_object_id_str(&id_str, commit_id);
3127 err = got_error_from_errno("got_object_id_str");
3131 err = add_line_offset(line_offsets, nlines, 0);
3135 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3136 refs_str ? refs_str : "", refs_str ? ")" : "");
3138 err = got_error_from_errno("fprintf");
3142 err = add_line_offset(line_offsets, nlines, outoff);
3146 n = fprintf(outfile, "from: %s\n",
3147 got_object_commit_get_author(commit));
3149 err = got_error_from_errno("fprintf");
3153 err = add_line_offset(line_offsets, nlines, outoff);
3157 committer_time = got_object_commit_get_committer_time(commit);
3158 datestr = get_datestr(&committer_time, datebuf);
3160 n = fprintf(outfile, "date: %s UTC\n", datestr);
3162 err = got_error_from_errno("fprintf");
3166 err = add_line_offset(line_offsets, nlines, outoff);
3170 author = got_object_commit_get_author(commit);
3171 committer = got_object_commit_get_committer(commit);
3172 if (strcmp(author, committer) != 0) {
3173 n = fprintf(outfile, "via: %s\n", committer);
3175 err = got_error_from_errno("fprintf");
3179 err = add_line_offset(line_offsets, nlines, outoff);
3183 err = got_object_commit_get_logmsg(&logmsg, commit);
3187 while ((line = strsep(&s, "\n")) != NULL) {
3188 n = fprintf(outfile, "%s\n", line);
3190 err = got_error_from_errno("fprintf");
3194 err = add_line_offset(line_offsets, nlines, outoff);
3199 err = get_changed_paths(&changed_paths, commit, repo);
3202 TAILQ_FOREACH(pe, &changed_paths, entry) {
3203 struct got_diff_changed_path *cp = pe->data;
3204 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3206 err = got_error_from_errno("fprintf");
3210 err = add_line_offset(line_offsets, nlines, outoff);
3213 free((char *)pe->path);
3217 fputc('\n', outfile);
3219 err = add_line_offset(line_offsets, nlines, outoff);
3221 got_pathlist_free(&changed_paths);
3225 got_object_commit_close(commit);
3227 free(*line_offsets);
3228 *line_offsets = NULL;
3234 static const struct got_error *
3235 create_diff(struct tog_diff_view_state *s)
3237 const struct got_error *err = NULL;
3241 free(s->line_offsets);
3242 s->line_offsets = malloc(sizeof(off_t));
3243 if (s->line_offsets == NULL)
3244 return got_error_from_errno("malloc");
3249 err = got_error_from_errno("got_opentemp");
3252 if (s->f && fclose(s->f) == EOF) {
3253 err = got_error_from_errno("fclose");
3259 err = got_object_get_type(&obj_type, s->repo, s->id1);
3261 err = got_object_get_type(&obj_type, s->repo, s->id2);
3266 case GOT_OBJ_TYPE_BLOB:
3267 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3268 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3269 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3271 case GOT_OBJ_TYPE_TREE:
3272 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3273 s->id1, s->id2, "", "", s->diff_context,
3274 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3276 case GOT_OBJ_TYPE_COMMIT: {
3277 const struct got_object_id_queue *parent_ids;
3278 struct got_object_qid *pid;
3279 struct got_commit_object *commit2;
3280 struct got_reflist_head *refs;
3282 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3285 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3286 /* Show commit info if we're diffing to a parent/root commit. */
3287 if (s->id1 == NULL) {
3288 err = write_commit_info(&s->line_offsets, &s->nlines,
3289 s->id2, refs, s->repo, s->f);
3293 parent_ids = got_object_commit_get_parent_ids(commit2);
3294 STAILQ_FOREACH(pid, parent_ids, entry) {
3295 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3296 err = write_commit_info(
3297 &s->line_offsets, &s->nlines,
3298 s->id2, refs, s->repo, s->f);
3305 got_object_commit_close(commit2);
3307 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3308 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3309 s->force_text_diff, s->repo, s->f);
3313 err = got_error(GOT_ERR_OBJ_TYPE);
3319 if (s->f && fflush(s->f) != 0 && err == NULL)
3320 err = got_error_from_errno("fflush");
3325 diff_view_indicate_progress(struct tog_view *view)
3327 mvwaddstr(view->window, 0, 0, "diffing...");
3332 static const struct got_error *
3333 search_start_diff_view(struct tog_view *view)
3335 struct tog_diff_view_state *s = &view->state.diff;
3337 s->matched_line = 0;
3341 static const struct got_error *
3342 search_next_diff_view(struct tog_view *view)
3344 struct tog_diff_view_state *s = &view->state.diff;
3347 size_t linesize = 0;
3350 if (!view->searching) {
3351 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3355 if (s->matched_line) {
3356 if (view->searching == TOG_SEARCH_FORWARD)
3357 lineno = s->matched_line + 1;
3359 lineno = s->matched_line - 1;
3361 if (view->searching == TOG_SEARCH_FORWARD)
3370 if (lineno <= 0 || lineno > s->nlines) {
3371 if (s->matched_line == 0) {
3372 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3376 if (view->searching == TOG_SEARCH_FORWARD)
3382 offset = s->line_offsets[lineno - 1];
3383 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3385 return got_error_from_errno("fseeko");
3387 linelen = getline(&line, &linesize, s->f);
3388 if (linelen != -1 &&
3389 match_line(line, &view->regex, 1, &view->regmatch)) {
3390 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3391 s->matched_line = lineno;
3394 if (view->searching == TOG_SEARCH_FORWARD)
3401 if (s->matched_line) {
3402 s->first_displayed_line = s->matched_line;
3403 s->selected_line = 1;
3409 static const struct got_error *
3410 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3411 struct got_object_id *id2, const char *label1, const char *label2,
3412 int diff_context, int ignore_whitespace, int force_text_diff,
3413 struct tog_view *log_view, struct got_repository *repo)
3415 const struct got_error *err;
3416 struct tog_diff_view_state *s = &view->state.diff;
3418 if (id1 != NULL && id2 != NULL) {
3420 err = got_object_get_type(&type1, repo, id1);
3423 err = got_object_get_type(&type2, repo, id2);
3428 return got_error(GOT_ERR_OBJ_TYPE);
3430 s->first_displayed_line = 1;
3431 s->last_displayed_line = view->nlines;
3432 s->selected_line = 1;
3440 s->id1 = got_object_id_dup(id1);
3442 return got_error_from_errno("got_object_id_dup");
3446 s->id2 = got_object_id_dup(id2);
3447 if (s->id2 == NULL) {
3450 return got_error_from_errno("got_object_id_dup");
3453 s->first_displayed_line = 1;
3454 s->last_displayed_line = view->nlines;
3455 s->diff_context = diff_context;
3456 s->ignore_whitespace = ignore_whitespace;
3457 s->force_text_diff = force_text_diff;
3458 s->log_view = log_view;
3461 STAILQ_INIT(&s->colors);
3462 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3463 err = add_color(&s->colors,
3464 "^-", TOG_COLOR_DIFF_MINUS,
3465 get_color_value("TOG_COLOR_DIFF_MINUS"));
3468 err = add_color(&s->colors, "^\\+",
3469 TOG_COLOR_DIFF_PLUS,
3470 get_color_value("TOG_COLOR_DIFF_PLUS"));
3472 free_colors(&s->colors);
3475 err = add_color(&s->colors,
3476 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3477 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3479 free_colors(&s->colors);
3483 err = add_color(&s->colors,
3484 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3485 TOG_COLOR_DIFF_META,
3486 get_color_value("TOG_COLOR_DIFF_META"));
3488 free_colors(&s->colors);
3492 err = add_color(&s->colors,
3493 "^(from|via): ", TOG_COLOR_AUTHOR,
3494 get_color_value("TOG_COLOR_AUTHOR"));
3496 free_colors(&s->colors);
3500 err = add_color(&s->colors,
3501 "^date: ", TOG_COLOR_DATE,
3502 get_color_value("TOG_COLOR_DATE"));
3504 free_colors(&s->colors);
3509 if (log_view && view_is_splitscreen(view))
3510 show_log_view(log_view); /* draw vborder */
3511 diff_view_indicate_progress(view);
3513 s->line_offsets = NULL;
3515 err = create_diff(s);
3521 free_colors(&s->colors);
3525 view->show = show_diff_view;
3526 view->input = input_diff_view;
3527 view->close = close_diff_view;
3528 view->search_start = search_start_diff_view;
3529 view->search_next = search_next_diff_view;
3534 static const struct got_error *
3535 close_diff_view(struct tog_view *view)
3537 const struct got_error *err = NULL;
3538 struct tog_diff_view_state *s = &view->state.diff;
3544 if (s->f && fclose(s->f) == EOF)
3545 err = got_error_from_errno("fclose");
3546 free_colors(&s->colors);
3547 free(s->line_offsets);
3548 s->line_offsets = NULL;
3553 static const struct got_error *
3554 show_diff_view(struct tog_view *view)
3556 const struct got_error *err;
3557 struct tog_diff_view_state *s = &view->state.diff;
3558 char *id_str1 = NULL, *id_str2, *header;
3559 const char *label1, *label2;
3562 err = got_object_id_str(&id_str1, s->id1);
3565 label1 = s->label1 ? : id_str1;
3567 label1 = "/dev/null";
3569 err = got_object_id_str(&id_str2, s->id2);
3572 label2 = s->label2 ? : id_str2;
3574 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3575 err = got_error_from_errno("asprintf");
3583 err = draw_file(view, header);
3588 static const struct got_error *
3589 set_selected_commit(struct tog_diff_view_state *s,
3590 struct commit_queue_entry *entry)
3592 const struct got_error *err;
3593 const struct got_object_id_queue *parent_ids;
3594 struct got_commit_object *selected_commit;
3595 struct got_object_qid *pid;
3598 s->id2 = got_object_id_dup(entry->id);
3600 return got_error_from_errno("got_object_id_dup");
3602 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3605 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3607 pid = STAILQ_FIRST(parent_ids);
3608 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3609 got_object_commit_close(selected_commit);
3613 static const struct got_error *
3614 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3616 const struct got_error *err = NULL;
3617 struct tog_diff_view_state *s = &view->state.diff;
3618 struct tog_log_view_state *ls;
3619 struct commit_queue_entry *old_selected_entry;
3621 size_t linesize = 0;
3629 s->force_text_diff = !s->force_text_diff;
3631 s->ignore_whitespace = !s->ignore_whitespace;
3632 wclear(view->window);
3633 s->first_displayed_line = 1;
3634 s->last_displayed_line = view->nlines;
3635 diff_view_indicate_progress(view);
3636 err = create_diff(s);
3640 if (s->first_displayed_line > 1)
3641 s->first_displayed_line--;
3645 if (s->first_displayed_line == 1)
3648 while (i++ < view->nlines - 1 &&
3649 s->first_displayed_line > 1)
3650 s->first_displayed_line--;
3655 s->first_displayed_line++;
3663 while (!s->eof && i++ < view->nlines - 1) {
3664 linelen = getline(&line, &linesize, s->f);
3665 s->first_displayed_line++;
3666 if (linelen == -1) {
3670 err = got_ferror(s->f, GOT_ERR_IO);
3677 if (s->diff_context > 0) {
3679 diff_view_indicate_progress(view);
3680 err = create_diff(s);
3681 if (s->first_displayed_line + view->nlines - 1 >
3683 s->first_displayed_line = 1;
3684 s->last_displayed_line = view->nlines;
3689 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3691 diff_view_indicate_progress(view);
3692 err = create_diff(s);
3697 if (s->log_view == NULL)
3699 ls = &s->log_view->state.log;
3700 old_selected_entry = ls->selected_entry;
3702 err = input_log_view(NULL, s->log_view, KEY_UP);
3706 if (old_selected_entry == ls->selected_entry)
3709 err = set_selected_commit(s, ls->selected_entry);
3713 s->first_displayed_line = 1;
3714 s->last_displayed_line = view->nlines;
3716 diff_view_indicate_progress(view);
3717 err = create_diff(s);
3721 if (s->log_view == NULL)
3723 ls = &s->log_view->state.log;
3724 old_selected_entry = ls->selected_entry;
3726 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3730 if (old_selected_entry == ls->selected_entry)
3733 err = set_selected_commit(s, ls->selected_entry);
3737 s->first_displayed_line = 1;
3738 s->last_displayed_line = view->nlines;
3740 diff_view_indicate_progress(view);
3741 err = create_diff(s);
3750 static const struct got_error *
3751 cmd_diff(int argc, char *argv[])
3753 const struct got_error *error = NULL;
3754 struct got_repository *repo = NULL;
3755 struct got_worktree *worktree = NULL;
3756 struct got_object_id *id1 = NULL, *id2 = NULL;
3757 char *repo_path = NULL, *cwd = NULL;
3758 char *id_str1 = NULL, *id_str2 = NULL;
3759 char *label1 = NULL, *label2 = NULL;
3760 int diff_context = 3, ignore_whitespace = 0;
3761 int ch, force_text_diff = 0;
3763 struct tog_view *view;
3765 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3768 force_text_diff = 1;
3771 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3774 err(1, "-C option %s", errstr);
3777 repo_path = realpath(optarg, NULL);
3778 if (repo_path == NULL)
3779 return got_error_from_errno2("realpath",
3781 got_path_strip_trailing_slashes(repo_path);
3784 ignore_whitespace = 1;
3796 usage_diff(); /* TODO show local worktree changes */
3797 } else if (argc == 2) {
3803 if (repo_path == NULL) {
3804 cwd = getcwd(NULL, 0);
3806 return got_error_from_errno("getcwd");
3807 error = got_worktree_open(&worktree, cwd);
3808 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3812 strdup(got_worktree_get_repo_path(worktree));
3814 repo_path = strdup(cwd);
3815 if (repo_path == NULL) {
3816 error = got_error_from_errno("strdup");
3821 error = got_repo_open(&repo, repo_path, NULL);
3827 error = apply_unveil(got_repo_get_path(repo), NULL);
3831 error = tog_load_refs(repo);
3835 error = got_repo_match_object_id(&id1, &label1, id_str1,
3836 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3840 error = got_repo_match_object_id(&id2, &label2, id_str2,
3841 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3845 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3847 error = got_error_from_errno("view_open");
3850 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3851 ignore_whitespace, force_text_diff, NULL, repo);
3854 error = view_loop(view);
3861 const struct got_error *close_err = got_repo_close(repo);
3866 got_worktree_close(worktree);
3875 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3880 struct tog_blame_line {
3882 struct got_object_id *id;
3885 static const struct got_error *
3886 draw_blame(struct tog_view *view)
3888 struct tog_blame_view_state *s = &view->state.blame;
3889 struct tog_blame *blame = &s->blame;
3890 regmatch_t *regmatch = &view->regmatch;
3891 const struct got_error *err;
3892 int lineno = 0, nprinted = 0;
3894 size_t linesize = 0;
3898 struct tog_blame_line *blame_line;
3899 struct got_object_id *prev_id = NULL;
3901 struct tog_color *tc;
3903 err = got_object_id_str(&id_str, s->blamed_commit->id);
3908 werase(view->window);
3910 if (asprintf(&line, "commit %s", id_str) == -1) {
3911 err = got_error_from_errno("asprintf");
3916 err = format_line(&wline, &width, line, view->ncols, 0);
3921 if (view_needs_focus_indication(view))
3922 wstandout(view->window);
3923 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3925 wattr_on(view->window,
3926 COLOR_PAIR(tc->colorpair), NULL);
3927 waddwstr(view->window, wline);
3929 wattr_off(view->window,
3930 COLOR_PAIR(tc->colorpair), NULL);
3931 if (view_needs_focus_indication(view))
3932 wstandend(view->window);
3935 if (width < view->ncols - 1)
3936 waddch(view->window, '\n');
3938 if (asprintf(&line, "[%d/%d] %s%s",
3939 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3940 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3942 return got_error_from_errno("asprintf");
3945 err = format_line(&wline, &width, line, view->ncols, 0);
3950 waddwstr(view->window, wline);
3953 if (width < view->ncols - 1)
3954 waddch(view->window, '\n');
3957 while (nprinted < view->nlines - 2) {
3958 linelen = getline(&line, &linesize, blame->f);
3959 if (linelen == -1) {
3960 if (feof(blame->f)) {
3965 return got_ferror(blame->f, GOT_ERR_IO);
3967 if (++lineno < s->first_displayed_line)
3970 if (view->focussed && nprinted == s->selected_line - 1)
3971 wstandout(view->window);
3973 if (blame->nlines > 0) {
3974 blame_line = &blame->lines[lineno - 1];
3975 if (blame_line->annotated && prev_id &&
3976 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3978 nprinted == s->selected_line - 1)) {
3979 waddstr(view->window, " ");
3980 } else if (blame_line->annotated) {
3982 err = got_object_id_str(&id_str, blame_line->id);
3987 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3989 wattr_on(view->window,
3990 COLOR_PAIR(tc->colorpair), NULL);
3991 wprintw(view->window, "%.8s", id_str);
3993 wattr_off(view->window,
3994 COLOR_PAIR(tc->colorpair), NULL);
3996 prev_id = blame_line->id;
3998 waddstr(view->window, "........");
4002 waddstr(view->window, "........");
4006 if (view->focussed && nprinted == s->selected_line - 1)
4007 wstandend(view->window);
4008 waddstr(view->window, " ");
4010 if (view->ncols <= 9) {
4012 wline = wcsdup(L"");
4013 if (wline == NULL) {
4014 err = got_error_from_errno("wcsdup");
4018 } else if (s->first_displayed_line + nprinted ==
4020 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4021 err = add_matched_line(&width, line, view->ncols - 9, 9,
4022 view->window, regmatch);
4029 err = format_line(&wline, &width, line,
4030 view->ncols - 9, 9);
4031 waddwstr(view->window, wline);
4037 if (width <= view->ncols - 1)
4038 waddch(view->window, '\n');
4039 if (++nprinted == 1)
4040 s->first_displayed_line = lineno;
4043 s->last_displayed_line = lineno;
4050 static const struct got_error *
4051 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4053 const struct got_error *err = NULL;
4054 struct tog_blame_cb_args *a = arg;
4055 struct tog_blame_line *line;
4058 if (nlines != a->nlines ||
4059 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4060 return got_error(GOT_ERR_RANGE);
4062 errcode = pthread_mutex_lock(&tog_mutex);
4064 return got_error_set_errno(errcode, "pthread_mutex_lock");
4066 if (*a->quit) { /* user has quit the blame view */
4067 err = got_error(GOT_ERR_ITER_COMPLETED);
4072 goto done; /* no change in this commit */
4074 line = &a->lines[lineno - 1];
4075 if (line->annotated)
4078 line->id = got_object_id_dup(id);
4079 if (line->id == NULL) {
4080 err = got_error_from_errno("got_object_id_dup");
4083 line->annotated = 1;
4085 errcode = pthread_mutex_unlock(&tog_mutex);
4087 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4092 blame_thread(void *arg)
4094 const struct got_error *err, *close_err;
4095 struct tog_blame_thread_args *ta = arg;
4096 struct tog_blame_cb_args *a = ta->cb_args;
4099 err = block_signals_used_by_main_thread();
4103 err = got_blame(ta->path, a->commit_id, ta->repo,
4104 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4105 if (err && err->code == GOT_ERR_CANCELLED)
4108 errcode = pthread_mutex_lock(&tog_mutex);
4110 return (void *)got_error_set_errno(errcode,
4111 "pthread_mutex_lock");
4113 close_err = got_repo_close(ta->repo);
4119 errcode = pthread_mutex_unlock(&tog_mutex);
4120 if (errcode && err == NULL)
4121 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4126 static struct got_object_id *
4127 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4128 int first_displayed_line, int selected_line)
4130 struct tog_blame_line *line;
4135 line = &lines[first_displayed_line - 1 + selected_line - 1];
4136 if (!line->annotated)
4142 static const struct got_error *
4143 stop_blame(struct tog_blame *blame)
4145 const struct got_error *err = NULL;
4148 if (blame->thread) {
4150 errcode = pthread_mutex_unlock(&tog_mutex);
4152 return got_error_set_errno(errcode,
4153 "pthread_mutex_unlock");
4154 errcode = pthread_join(blame->thread, (void **)&err);
4156 return got_error_set_errno(errcode, "pthread_join");
4157 errcode = pthread_mutex_lock(&tog_mutex);
4159 return got_error_set_errno(errcode,
4160 "pthread_mutex_lock");
4161 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4163 blame->thread = NULL;
4165 if (blame->thread_args.repo) {
4166 const struct got_error *close_err;
4167 close_err = got_repo_close(blame->thread_args.repo);
4170 blame->thread_args.repo = NULL;
4173 if (fclose(blame->f) == EOF && err == NULL)
4174 err = got_error_from_errno("fclose");
4178 for (i = 0; i < blame->nlines; i++)
4179 free(blame->lines[i].id);
4181 blame->lines = NULL;
4183 free(blame->cb_args.commit_id);
4184 blame->cb_args.commit_id = NULL;
4189 static const struct got_error *
4190 cancel_blame_view(void *arg)
4192 const struct got_error *err = NULL;
4196 errcode = pthread_mutex_lock(&tog_mutex);
4198 return got_error_set_errno(errcode,
4199 "pthread_mutex_unlock");
4202 err = got_error(GOT_ERR_CANCELLED);
4204 errcode = pthread_mutex_unlock(&tog_mutex);
4206 return got_error_set_errno(errcode,
4207 "pthread_mutex_lock");
4212 static const struct got_error *
4213 run_blame(struct tog_view *view)
4215 struct tog_blame_view_state *s = &view->state.blame;
4216 struct tog_blame *blame = &s->blame;
4217 const struct got_error *err = NULL;
4218 struct got_blob_object *blob = NULL;
4219 struct got_repository *thread_repo = NULL;
4220 struct got_object_id *obj_id = NULL;
4223 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4228 err = got_object_get_type(&obj_type, s->repo, obj_id);
4232 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4233 err = got_error(GOT_ERR_OBJ_TYPE);
4237 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4240 blame->f = got_opentemp();
4241 if (blame->f == NULL) {
4242 err = got_error_from_errno("got_opentemp");
4245 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4246 &blame->line_offsets, blame->f, blob);
4249 if (blame->nlines == 0) {
4250 s->blame_complete = 1;
4254 /* Don't include \n at EOF in the blame line count. */
4255 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4258 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4259 if (blame->lines == NULL) {
4260 err = got_error_from_errno("calloc");
4264 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4268 blame->cb_args.view = view;
4269 blame->cb_args.lines = blame->lines;
4270 blame->cb_args.nlines = blame->nlines;
4271 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4272 if (blame->cb_args.commit_id == NULL) {
4273 err = got_error_from_errno("got_object_id_dup");
4276 blame->cb_args.quit = &s->done;
4278 blame->thread_args.path = s->path;
4279 blame->thread_args.repo = thread_repo;
4280 blame->thread_args.cb_args = &blame->cb_args;
4281 blame->thread_args.complete = &s->blame_complete;
4282 blame->thread_args.cancel_cb = cancel_blame_view;
4283 blame->thread_args.cancel_arg = &s->done;
4284 s->blame_complete = 0;
4286 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4287 s->first_displayed_line = 1;
4288 s->last_displayed_line = view->nlines;
4289 s->selected_line = 1;
4294 got_object_blob_close(blob);
4301 static const struct got_error *
4302 open_blame_view(struct tog_view *view, char *path,
4303 struct got_object_id *commit_id, struct got_repository *repo)
4305 const struct got_error *err = NULL;
4306 struct tog_blame_view_state *s = &view->state.blame;
4308 STAILQ_INIT(&s->blamed_commits);
4310 s->path = strdup(path);
4311 if (s->path == NULL)
4312 return got_error_from_errno("strdup");
4314 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4320 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4321 s->first_displayed_line = 1;
4322 s->last_displayed_line = view->nlines;
4323 s->selected_line = 1;
4324 s->blame_complete = 0;
4326 s->commit_id = commit_id;
4327 memset(&s->blame, 0, sizeof(s->blame));
4329 STAILQ_INIT(&s->colors);
4330 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4331 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4332 get_color_value("TOG_COLOR_COMMIT"));
4337 view->show = show_blame_view;
4338 view->input = input_blame_view;
4339 view->close = close_blame_view;
4340 view->search_start = search_start_blame_view;
4341 view->search_next = search_next_blame_view;
4343 return run_blame(view);
4346 static const struct got_error *
4347 close_blame_view(struct tog_view *view)
4349 const struct got_error *err = NULL;
4350 struct tog_blame_view_state *s = &view->state.blame;
4352 if (s->blame.thread)
4353 err = stop_blame(&s->blame);
4355 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4356 struct got_object_qid *blamed_commit;
4357 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4358 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4359 got_object_qid_free(blamed_commit);
4363 free_colors(&s->colors);
4368 static const struct got_error *
4369 search_start_blame_view(struct tog_view *view)
4371 struct tog_blame_view_state *s = &view->state.blame;
4373 s->matched_line = 0;
4377 static const struct got_error *
4378 search_next_blame_view(struct tog_view *view)
4380 struct tog_blame_view_state *s = &view->state.blame;
4383 size_t linesize = 0;
4386 if (!view->searching) {
4387 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4391 if (s->matched_line) {
4392 if (view->searching == TOG_SEARCH_FORWARD)
4393 lineno = s->matched_line + 1;
4395 lineno = s->matched_line - 1;
4397 if (view->searching == TOG_SEARCH_FORWARD)
4400 lineno = s->blame.nlines;
4406 if (lineno <= 0 || lineno > s->blame.nlines) {
4407 if (s->matched_line == 0) {
4408 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4412 if (view->searching == TOG_SEARCH_FORWARD)
4415 lineno = s->blame.nlines;
4418 offset = s->blame.line_offsets[lineno - 1];
4419 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4421 return got_error_from_errno("fseeko");
4423 linelen = getline(&line, &linesize, s->blame.f);
4424 if (linelen != -1 &&
4425 match_line(line, &view->regex, 1, &view->regmatch)) {
4426 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4427 s->matched_line = lineno;
4430 if (view->searching == TOG_SEARCH_FORWARD)
4437 if (s->matched_line) {
4438 s->first_displayed_line = s->matched_line;
4439 s->selected_line = 1;
4445 static const struct got_error *
4446 show_blame_view(struct tog_view *view)
4448 const struct got_error *err = NULL;
4449 struct tog_blame_view_state *s = &view->state.blame;
4452 if (s->blame.thread == NULL && !s->blame_complete) {
4453 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4454 &s->blame.thread_args);
4456 return got_error_set_errno(errcode, "pthread_create");
4458 halfdelay(1); /* fast refresh while annotating */
4461 if (s->blame_complete)
4462 halfdelay(10); /* disable fast refresh */
4464 err = draw_blame(view);
4470 static const struct got_error *
4471 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4473 const struct got_error *err = NULL, *thread_err = NULL;
4474 struct tog_view *diff_view;
4475 struct tog_blame_view_state *s = &view->state.blame;
4484 if (s->selected_line > 1)
4486 else if (s->selected_line == 1 &&
4487 s->first_displayed_line > 1)
4488 s->first_displayed_line--;
4492 if (s->first_displayed_line == 1) {
4493 s->selected_line = 1;
4496 if (s->first_displayed_line > view->nlines - 2)
4497 s->first_displayed_line -=
4500 s->first_displayed_line = 1;
4504 if (s->selected_line < view->nlines - 2 &&
4505 s->first_displayed_line +
4506 s->selected_line <= s->blame.nlines)
4508 else if (s->last_displayed_line <
4510 s->first_displayed_line++;
4514 struct got_object_id *id = NULL;
4515 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4516 s->first_displayed_line, s->selected_line);
4520 struct got_commit_object *commit;
4521 struct got_object_qid *pid;
4522 struct got_object_id *blob_id = NULL;
4524 err = got_object_open_as_commit(&commit,
4529 got_object_commit_get_parent_ids(commit));
4531 got_object_commit_close(commit);
4534 /* Check if path history ends here. */
4535 err = got_object_id_by_path(&blob_id, s->repo,
4538 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4540 got_object_commit_close(commit);
4543 err = got_object_get_type(&obj_type, s->repo,
4546 /* Can't blame non-blob type objects. */
4547 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4548 got_object_commit_close(commit);
4551 err = got_object_qid_alloc(&s->blamed_commit,
4553 got_object_commit_close(commit);
4555 if (got_object_id_cmp(id,
4556 s->blamed_commit->id) == 0)
4558 err = got_object_qid_alloc(&s->blamed_commit,
4564 thread_err = stop_blame(&s->blame);
4568 STAILQ_INSERT_HEAD(&s->blamed_commits,
4569 s->blamed_commit, entry);
4570 err = run_blame(view);
4576 struct got_object_qid *first;
4577 first = STAILQ_FIRST(&s->blamed_commits);
4578 if (!got_object_id_cmp(first->id, s->commit_id))
4581 thread_err = stop_blame(&s->blame);
4585 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4586 got_object_qid_free(s->blamed_commit);
4588 STAILQ_FIRST(&s->blamed_commits);
4589 err = run_blame(view);
4596 struct got_object_id *id = NULL;
4597 struct got_object_qid *pid;
4598 struct got_commit_object *commit = NULL;
4599 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4600 s->first_displayed_line, s->selected_line);
4603 err = got_object_open_as_commit(&commit, s->repo, id);
4607 got_object_commit_get_parent_ids(commit));
4608 if (view_is_parent_view(view))
4609 begin_x = view_split_begin_x(view->begin_x);
4610 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4611 if (diff_view == NULL) {
4612 got_object_commit_close(commit);
4613 err = got_error_from_errno("view_open");
4616 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4617 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4618 got_object_commit_close(commit);
4620 view_close(diff_view);
4624 diff_view->focussed = 1;
4625 if (view_is_parent_view(view)) {
4626 err = view_close_child(view);
4629 view_set_child(view, diff_view);
4630 view->focus_child = 1;
4632 *new_view = diff_view;
4640 if (s->last_displayed_line >= s->blame.nlines &&
4641 s->selected_line >= MIN(s->blame.nlines,
4642 view->nlines - 2)) {
4645 if (s->last_displayed_line >= s->blame.nlines &&
4646 s->selected_line < view->nlines - 2) {
4647 s->selected_line = MIN(s->blame.nlines,
4651 if (s->last_displayed_line + view->nlines - 2
4653 s->first_displayed_line +=
4656 s->first_displayed_line =
4661 if (s->selected_line > view->nlines - 2) {
4662 s->selected_line = MIN(s->blame.nlines,
4669 return thread_err ? thread_err : err;
4672 static const struct got_error *
4673 cmd_blame(int argc, char *argv[])
4675 const struct got_error *error;
4676 struct got_repository *repo = NULL;
4677 struct got_worktree *worktree = NULL;
4678 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4679 char *link_target = NULL;
4680 struct got_object_id *commit_id = NULL;
4681 char *commit_id_str = NULL;
4683 struct tog_view *view;
4685 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4688 commit_id_str = optarg;
4691 repo_path = realpath(optarg, NULL);
4692 if (repo_path == NULL)
4693 return got_error_from_errno2("realpath",
4708 if (repo_path == NULL) {
4709 cwd = getcwd(NULL, 0);
4711 return got_error_from_errno("getcwd");
4712 error = got_worktree_open(&worktree, cwd);
4713 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4717 strdup(got_worktree_get_repo_path(worktree));
4719 repo_path = strdup(cwd);
4720 if (repo_path == NULL) {
4721 error = got_error_from_errno("strdup");
4726 error = got_repo_open(&repo, repo_path, NULL);
4730 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4737 error = apply_unveil(got_repo_get_path(repo), NULL);
4741 error = tog_load_refs(repo);
4745 if (commit_id_str == NULL) {
4746 struct got_reference *head_ref;
4747 error = got_ref_open(&head_ref, repo, worktree ?
4748 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4751 error = got_ref_resolve(&commit_id, repo, head_ref);
4752 got_ref_close(head_ref);
4754 error = got_repo_match_object_id(&commit_id, NULL,
4755 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4760 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4762 error = got_error_from_errno("view_open");
4766 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4771 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4776 /* Release work tree lock. */
4777 got_worktree_close(worktree);
4780 error = view_loop(view);
4788 got_worktree_close(worktree);
4790 const struct got_error *close_err = got_repo_close(repo);
4798 static const struct got_error *
4799 draw_tree_entries(struct tog_view *view, const char *parent_path)
4801 struct tog_tree_view_state *s = &view->state.tree;
4802 const struct got_error *err = NULL;
4803 struct got_tree_entry *te;
4805 struct tog_color *tc;
4806 int width, n, i, nentries;
4807 int limit = view->nlines;
4811 werase(view->window);
4816 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4819 if (view_needs_focus_indication(view))
4820 wstandout(view->window);
4821 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4823 wattr_on(view->window,
4824 COLOR_PAIR(tc->colorpair), NULL);
4825 waddwstr(view->window, wline);
4827 wattr_off(view->window,
4828 COLOR_PAIR(tc->colorpair), NULL);
4829 if (view_needs_focus_indication(view))
4830 wstandend(view->window);
4833 if (width < view->ncols - 1)
4834 waddch(view->window, '\n');
4837 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4840 waddwstr(view->window, wline);
4843 if (width < view->ncols - 1)
4844 waddch(view->window, '\n');
4847 waddch(view->window, '\n');
4851 if (s->first_displayed_entry == NULL) {
4852 te = got_object_tree_get_first_entry(s->tree);
4853 if (s->selected == 0) {
4855 wstandout(view->window);
4856 s->selected_entry = NULL;
4858 waddstr(view->window, " ..\n"); /* parent directory */
4859 if (s->selected == 0 && view->focussed)
4860 wstandend(view->window);
4867 te = s->first_displayed_entry;
4870 nentries = got_object_tree_get_nentries(s->tree);
4871 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4872 char *line = NULL, *id_str = NULL, *link_target = NULL;
4873 const char *modestr = "";
4876 te = got_object_tree_get_entry(s->tree, i);
4877 mode = got_tree_entry_get_mode(te);
4880 err = got_object_id_str(&id_str,
4881 got_tree_entry_get_id(te));
4883 return got_error_from_errno(
4884 "got_object_id_str");
4886 if (got_object_tree_entry_is_submodule(te))
4888 else if (S_ISLNK(mode)) {
4891 err = got_tree_entry_get_symlink_target(&link_target,
4897 for (i = 0; i < strlen(link_target); i++) {
4898 if (!isprint((unsigned char)link_target[i]))
4899 link_target[i] = '?';
4903 else if (S_ISDIR(mode))
4905 else if (mode & S_IXUSR)
4907 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4908 got_tree_entry_get_name(te), modestr,
4909 link_target ? " -> ": "",
4910 link_target ? link_target : "") == -1) {
4913 return got_error_from_errno("asprintf");
4917 err = format_line(&wline, &width, line, view->ncols, 0);
4922 if (n == s->selected) {
4924 wstandout(view->window);
4925 s->selected_entry = te;
4927 tc = match_color(&s->colors, line);
4929 wattr_on(view->window,
4930 COLOR_PAIR(tc->colorpair), NULL);
4931 waddwstr(view->window, wline);
4933 wattr_off(view->window,
4934 COLOR_PAIR(tc->colorpair), NULL);
4935 if (width < view->ncols - 1)
4936 waddch(view->window, '\n');
4937 if (n == s->selected && view->focussed)
4938 wstandend(view->window);
4944 s->last_displayed_entry = te;
4953 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4955 struct got_tree_entry *te;
4956 int isroot = s->tree == s->root;
4959 if (s->first_displayed_entry == NULL)
4962 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4963 while (i++ < maxscroll) {
4966 s->first_displayed_entry = NULL;
4969 s->first_displayed_entry = te;
4970 te = got_tree_entry_get_prev(s->tree, te);
4975 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4977 struct got_tree_entry *next, *last;
4980 if (s->first_displayed_entry)
4981 next = got_tree_entry_get_next(s->tree,
4982 s->first_displayed_entry);
4984 next = got_object_tree_get_first_entry(s->tree);
4986 last = s->last_displayed_entry;
4987 while (next && last && n++ < maxscroll) {
4988 last = got_tree_entry_get_next(s->tree, last);
4990 s->first_displayed_entry = next;
4991 next = got_tree_entry_get_next(s->tree, next);
4996 static const struct got_error *
4997 tree_entry_path(char **path, struct tog_parent_trees *parents,
4998 struct got_tree_entry *te)
5000 const struct got_error *err = NULL;
5001 struct tog_parent_tree *pt;
5002 size_t len = 2; /* for leading slash and NUL */
5004 TAILQ_FOREACH(pt, parents, entry)
5005 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5008 len += strlen(got_tree_entry_get_name(te));
5010 *path = calloc(1, len);
5012 return got_error_from_errno("calloc");
5015 pt = TAILQ_LAST(parents, tog_parent_trees);
5017 const char *name = got_tree_entry_get_name(pt->selected_entry);
5018 if (strlcat(*path, name, len) >= len) {
5019 err = got_error(GOT_ERR_NO_SPACE);
5022 if (strlcat(*path, "/", len) >= len) {
5023 err = got_error(GOT_ERR_NO_SPACE);
5026 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5029 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5030 err = got_error(GOT_ERR_NO_SPACE);
5042 static const struct got_error *
5043 blame_tree_entry(struct tog_view **new_view, int begin_x,
5044 struct got_tree_entry *te, struct tog_parent_trees *parents,
5045 struct got_object_id *commit_id, struct got_repository *repo)
5047 const struct got_error *err = NULL;
5049 struct tog_view *blame_view;
5053 err = tree_entry_path(&path, parents, te);
5057 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5058 if (blame_view == NULL) {
5059 err = got_error_from_errno("view_open");
5063 err = open_blame_view(blame_view, path, commit_id, repo);
5065 if (err->code == GOT_ERR_CANCELLED)
5067 view_close(blame_view);
5069 *new_view = blame_view;
5075 static const struct got_error *
5076 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5077 struct tog_tree_view_state *s)
5079 struct tog_view *log_view;
5080 const struct got_error *err = NULL;
5085 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5086 if (log_view == NULL)
5087 return got_error_from_errno("view_open");
5089 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5093 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5096 view_close(log_view);
5098 *new_view = log_view;
5103 static const struct got_error *
5104 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5105 const char *head_ref_name, struct got_repository *repo)
5107 const struct got_error *err = NULL;
5108 char *commit_id_str = NULL;
5109 struct tog_tree_view_state *s = &view->state.tree;
5110 struct got_commit_object *commit = NULL;
5112 TAILQ_INIT(&s->parents);
5113 STAILQ_INIT(&s->colors);
5115 s->commit_id = got_object_id_dup(commit_id);
5116 if (s->commit_id == NULL)
5117 return got_error_from_errno("got_object_id_dup");
5119 err = got_object_open_as_commit(&commit, repo, commit_id);
5124 * The root is opened here and will be closed when the view is closed.
5125 * Any visited subtrees and their path-wise parents are opened and
5128 err = got_object_open_as_tree(&s->root, repo,
5129 got_object_commit_get_tree_id(commit));
5134 err = got_object_id_str(&commit_id_str, commit_id);
5138 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5139 err = got_error_from_errno("asprintf");
5143 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5144 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5145 if (head_ref_name) {
5146 s->head_ref_name = strdup(head_ref_name);
5147 if (s->head_ref_name == NULL) {
5148 err = got_error_from_errno("strdup");
5154 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5155 err = add_color(&s->colors, "\\$$",
5156 TOG_COLOR_TREE_SUBMODULE,
5157 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5160 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5161 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5164 err = add_color(&s->colors, "/$",
5165 TOG_COLOR_TREE_DIRECTORY,
5166 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5170 err = add_color(&s->colors, "\\*$",
5171 TOG_COLOR_TREE_EXECUTABLE,
5172 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5176 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5177 get_color_value("TOG_COLOR_COMMIT"));
5182 view->show = show_tree_view;
5183 view->input = input_tree_view;
5184 view->close = close_tree_view;
5185 view->search_start = search_start_tree_view;
5186 view->search_next = search_next_tree_view;
5188 free(commit_id_str);
5190 got_object_commit_close(commit);
5192 close_tree_view(view);
5196 static const struct got_error *
5197 close_tree_view(struct tog_view *view)
5199 struct tog_tree_view_state *s = &view->state.tree;
5201 free_colors(&s->colors);
5202 free(s->tree_label);
5203 s->tree_label = NULL;
5205 s->commit_id = NULL;
5206 free(s->head_ref_name);
5207 s->head_ref_name = NULL;
5208 while (!TAILQ_EMPTY(&s->parents)) {
5209 struct tog_parent_tree *parent;
5210 parent = TAILQ_FIRST(&s->parents);
5211 TAILQ_REMOVE(&s->parents, parent, entry);
5212 if (parent->tree != s->root)
5213 got_object_tree_close(parent->tree);
5217 if (s->tree != NULL && s->tree != s->root)
5218 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_reference *ref = NULL;
5504 const char *head_ref_name = NULL;
5506 struct tog_view *view;
5508 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5511 commit_id_arg = optarg;
5514 repo_path = realpath(optarg, NULL);
5515 if (repo_path == NULL)
5516 return got_error_from_errno2("realpath",
5531 if (repo_path == NULL) {
5532 cwd = getcwd(NULL, 0);
5534 return got_error_from_errno("getcwd");
5535 error = got_worktree_open(&worktree, cwd);
5536 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5540 strdup(got_worktree_get_repo_path(worktree));
5542 repo_path = strdup(cwd);
5543 if (repo_path == NULL) {
5544 error = got_error_from_errno("strdup");
5549 error = got_repo_open(&repo, repo_path, NULL);
5553 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5560 error = apply_unveil(got_repo_get_path(repo), NULL);
5564 error = tog_load_refs(repo);
5568 if (commit_id_arg == NULL) {
5569 error = got_repo_match_object_id(&commit_id, &label,
5570 worktree ? got_worktree_get_head_ref_name(worktree) :
5571 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5574 head_ref_name = label;
5576 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5578 head_ref_name = got_ref_get_name(ref);
5579 else if (error->code != GOT_ERR_NOT_REF)
5581 error = got_repo_match_object_id(&commit_id, NULL,
5582 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5587 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5589 error = got_error_from_errno("view_open");
5592 error = open_tree_view(view, commit_id, head_ref_name, repo);
5595 if (!got_path_is_root_dir(in_repo_path)) {
5596 error = tree_view_walk_path(&view->state.tree, commit_id,
5603 /* Release work tree lock. */
5604 got_worktree_close(worktree);
5607 error = view_loop(view);
5616 const struct got_error *close_err = got_repo_close(repo);
5624 static const struct got_error *
5625 ref_view_load_refs(struct tog_ref_view_state *s)
5627 struct got_reflist_entry *sre;
5628 struct tog_reflist_entry *re;
5631 TAILQ_FOREACH(sre, &tog_refs, entry) {
5632 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5635 re = malloc(sizeof(*re));
5637 return got_error_from_errno("malloc");
5639 re->ref = got_ref_dup(sre->ref);
5640 if (re->ref == NULL)
5641 return got_error_from_errno("got_ref_dup");
5642 re->idx = s->nrefs++;
5643 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5646 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5651 ref_view_free_refs(struct tog_ref_view_state *s)
5653 struct tog_reflist_entry *re;
5655 while (!TAILQ_EMPTY(&s->refs)) {
5656 re = TAILQ_FIRST(&s->refs);
5657 TAILQ_REMOVE(&s->refs, re, entry);
5658 got_ref_close(re->ref);
5663 static const struct got_error *
5664 open_ref_view(struct tog_view *view, struct got_repository *repo)
5666 const struct got_error *err = NULL;
5667 struct tog_ref_view_state *s = &view->state.ref;
5669 s->selected_entry = 0;
5672 TAILQ_INIT(&s->refs);
5673 STAILQ_INIT(&s->colors);
5675 err = ref_view_load_refs(s);
5679 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5680 err = add_color(&s->colors, "^refs/heads/",
5681 TOG_COLOR_REFS_HEADS,
5682 get_color_value("TOG_COLOR_REFS_HEADS"));
5686 err = add_color(&s->colors, "^refs/tags/",
5687 TOG_COLOR_REFS_TAGS,
5688 get_color_value("TOG_COLOR_REFS_TAGS"));
5692 err = add_color(&s->colors, "^refs/remotes/",
5693 TOG_COLOR_REFS_REMOTES,
5694 get_color_value("TOG_COLOR_REFS_REMOTES"));
5699 view->show = show_ref_view;
5700 view->input = input_ref_view;
5701 view->close = close_ref_view;
5702 view->search_start = search_start_ref_view;
5703 view->search_next = search_next_ref_view;
5706 free_colors(&s->colors);
5710 static const struct got_error *
5711 close_ref_view(struct tog_view *view)
5713 struct tog_ref_view_state *s = &view->state.ref;
5715 ref_view_free_refs(s);
5716 free_colors(&s->colors);
5721 static const struct got_error *
5722 resolve_reflist_entry(struct got_object_id **commit_id,
5723 struct tog_reflist_entry *re, struct got_repository *repo)
5725 const struct got_error *err = NULL;
5726 struct got_object_id *obj_id;
5727 struct got_tag_object *tag = NULL;
5732 err = got_ref_resolve(&obj_id, repo, re->ref);
5736 err = got_object_get_type(&obj_type, repo, obj_id);
5741 case GOT_OBJ_TYPE_COMMIT:
5742 *commit_id = obj_id;
5744 case GOT_OBJ_TYPE_TAG:
5745 err = got_object_open_as_tag(&tag, repo, obj_id);
5749 err = got_object_get_type(&obj_type, repo,
5750 got_object_tag_get_object_id(tag));
5753 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5754 err = got_error(GOT_ERR_OBJ_TYPE);
5757 *commit_id = got_object_id_dup(
5758 got_object_tag_get_object_id(tag));
5759 if (*commit_id == NULL) {
5760 err = got_error_from_errno("got_object_id_dup");
5765 err = got_error(GOT_ERR_OBJ_TYPE);
5771 got_object_tag_close(tag);
5779 static const struct got_error *
5780 log_ref_entry(struct tog_view **new_view, int begin_x,
5781 struct tog_reflist_entry *re, struct got_repository *repo)
5783 struct tog_view *log_view;
5784 const struct got_error *err = NULL;
5785 struct got_object_id *commit_id = NULL;
5789 err = resolve_reflist_entry(&commit_id, re, repo);
5791 if (err->code != GOT_ERR_OBJ_TYPE)
5797 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5798 if (log_view == NULL) {
5799 err = got_error_from_errno("view_open");
5803 err = open_log_view(log_view, commit_id, repo,
5804 got_ref_get_name(re->ref), "", 0);
5807 view_close(log_view);
5809 *new_view = log_view;
5815 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5817 struct tog_reflist_entry *re;
5820 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5823 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5824 while (i++ < maxscroll) {
5827 s->first_displayed_entry = re;
5828 re = TAILQ_PREV(re, tog_reflist_head, entry);
5833 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5835 struct tog_reflist_entry *next, *last;
5838 if (s->first_displayed_entry)
5839 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5841 next = TAILQ_FIRST(&s->refs);
5843 last = s->last_displayed_entry;
5844 while (next && last && n++ < maxscroll) {
5845 last = TAILQ_NEXT(last, entry);
5847 s->first_displayed_entry = next;
5848 next = TAILQ_NEXT(next, entry);
5853 static const struct got_error *
5854 search_start_ref_view(struct tog_view *view)
5856 struct tog_ref_view_state *s = &view->state.ref;
5858 s->matched_entry = NULL;
5863 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5865 regmatch_t regmatch;
5867 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5871 static const struct got_error *
5872 search_next_ref_view(struct tog_view *view)
5874 struct tog_ref_view_state *s = &view->state.ref;
5875 struct tog_reflist_entry *re = NULL;
5877 if (!view->searching) {
5878 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5882 if (s->matched_entry) {
5883 if (view->searching == TOG_SEARCH_FORWARD) {
5884 if (s->selected_entry)
5885 re = TAILQ_NEXT(s->selected_entry, entry);
5887 re = TAILQ_PREV(s->selected_entry,
5888 tog_reflist_head, entry);
5890 if (s->selected_entry == NULL)
5891 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5893 re = TAILQ_PREV(s->selected_entry,
5894 tog_reflist_head, entry);
5897 if (view->searching == TOG_SEARCH_FORWARD)
5898 re = TAILQ_FIRST(&s->refs);
5900 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5905 if (s->matched_entry == NULL) {
5906 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5909 if (view->searching == TOG_SEARCH_FORWARD)
5910 re = TAILQ_FIRST(&s->refs);
5912 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5915 if (match_reflist_entry(re, &view->regex)) {
5916 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5917 s->matched_entry = re;
5921 if (view->searching == TOG_SEARCH_FORWARD)
5922 re = TAILQ_NEXT(re, entry);
5924 re = TAILQ_PREV(re, tog_reflist_head, entry);
5927 if (s->matched_entry) {
5928 s->first_displayed_entry = s->matched_entry;
5935 static const struct got_error *
5936 show_ref_view(struct tog_view *view)
5938 const struct got_error *err = NULL;
5939 struct tog_ref_view_state *s = &view->state.ref;
5940 struct tog_reflist_entry *re;
5943 struct tog_color *tc;
5945 int limit = view->nlines;
5947 werase(view->window);
5954 re = s->first_displayed_entry;
5956 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5958 return got_error_from_errno("asprintf");
5960 err = format_line(&wline, &width, line, view->ncols, 0);
5965 if (view_needs_focus_indication(view))
5966 wstandout(view->window);
5967 waddwstr(view->window, wline);
5968 if (view_needs_focus_indication(view))
5969 wstandend(view->window);
5974 if (width < view->ncols - 1)
5975 waddch(view->window, '\n');
5980 while (re && limit > 0) {
5983 if (got_ref_is_symbolic(re->ref)) {
5984 if (asprintf(&line, "%s -> %s",
5985 got_ref_get_name(re->ref),
5986 got_ref_get_symref_target(re->ref)) == -1)
5987 return got_error_from_errno("asprintf");
5988 } else if (s->show_ids) {
5989 struct got_object_id *id;
5991 err = got_ref_resolve(&id, s->repo, re->ref);
5994 err = got_object_id_str(&id_str, id);
5999 if (asprintf(&line, "%s: %s",
6000 got_ref_get_name(re->ref), id_str) == -1) {
6001 err = got_error_from_errno("asprintf");
6009 line = strdup(got_ref_get_name(re->ref));
6011 return got_error_from_errno("strdup");
6014 err = format_line(&wline, &width, line, view->ncols, 0);
6019 if (n == s->selected) {
6021 wstandout(view->window);
6022 s->selected_entry = re;
6024 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6026 wattr_on(view->window,
6027 COLOR_PAIR(tc->colorpair), NULL);
6028 waddwstr(view->window, wline);
6030 wattr_off(view->window,
6031 COLOR_PAIR(tc->colorpair), NULL);
6032 if (width < view->ncols - 1)
6033 waddch(view->window, '\n');
6034 if (n == s->selected && view->focussed)
6035 wstandend(view->window);
6041 s->last_displayed_entry = re;
6044 re = TAILQ_NEXT(re, entry);
6051 static const struct got_error *
6052 browse_ref_tree(struct tog_view **new_view, int begin_x,
6053 struct tog_reflist_entry *re, struct got_repository *repo)
6055 const struct got_error *err = NULL;
6056 struct got_object_id *commit_id = NULL;
6057 struct tog_view *tree_view;
6061 err = resolve_reflist_entry(&commit_id, re, repo);
6063 if (err->code != GOT_ERR_OBJ_TYPE)
6070 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6071 if (tree_view == NULL) {
6072 err = got_error_from_errno("view_open");
6076 err = open_tree_view(tree_view, commit_id,
6077 got_ref_get_name(re->ref), repo);
6081 *new_view = tree_view;
6086 static const struct got_error *
6087 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6089 const struct got_error *err = NULL;
6090 struct tog_ref_view_state *s = &view->state.ref;
6091 struct tog_view *log_view, *tree_view;
6096 s->show_ids = !s->show_ids;
6100 if (!s->selected_entry)
6102 if (view_is_parent_view(view))
6103 begin_x = view_split_begin_x(view->begin_x);
6104 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6107 log_view->focussed = 1;
6108 if (view_is_parent_view(view)) {
6109 err = view_close_child(view);
6112 view_set_child(view, log_view);
6113 view->focus_child = 1;
6115 *new_view = log_view;
6118 if (!s->selected_entry)
6120 if (view_is_parent_view(view))
6121 begin_x = view_split_begin_x(view->begin_x);
6122 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6124 if (err || tree_view == NULL)
6127 tree_view->focussed = 1;
6128 if (view_is_parent_view(view)) {
6129 err = view_close_child(view);
6132 view_set_child(view, tree_view);
6133 view->focus_child = 1;
6135 *new_view = tree_view;
6139 if (s->selected > 0) {
6143 ref_scroll_up(s, 1);
6147 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6149 ref_scroll_up(s, MAX(0, view->nlines - 1));
6153 if (s->selected < s->ndisplayed - 1) {
6157 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6158 /* can't scroll any further */
6160 ref_scroll_down(s, 1);
6164 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6165 /* can't scroll any further; move cursor down */
6166 if (s->selected < s->ndisplayed - 1)
6167 s->selected = s->ndisplayed - 1;
6170 ref_scroll_down(s, view->nlines - 1);
6174 err = tog_load_refs(s->repo);
6177 ref_view_free_refs(s);
6178 err = ref_view_load_refs(s);
6181 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6182 s->selected = view->nlines - 2;
6195 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6200 static const struct got_error *
6201 cmd_ref(int argc, char *argv[])
6203 const struct got_error *error;
6204 struct got_repository *repo = NULL;
6205 struct got_worktree *worktree = NULL;
6206 char *cwd = NULL, *repo_path = NULL;
6208 struct tog_view *view;
6210 while ((ch = getopt(argc, argv, "r:")) != -1) {
6213 repo_path = realpath(optarg, NULL);
6214 if (repo_path == NULL)
6215 return got_error_from_errno2("realpath",
6230 if (repo_path == NULL) {
6231 cwd = getcwd(NULL, 0);
6233 return got_error_from_errno("getcwd");
6234 error = got_worktree_open(&worktree, cwd);
6235 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6239 strdup(got_worktree_get_repo_path(worktree));
6241 repo_path = strdup(cwd);
6242 if (repo_path == NULL) {
6243 error = got_error_from_errno("strdup");
6248 error = got_repo_open(&repo, repo_path, NULL);
6254 error = apply_unveil(got_repo_get_path(repo), NULL);
6258 error = tog_load_refs(repo);
6262 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6264 error = got_error_from_errno("view_open");
6268 error = open_ref_view(view, repo);
6273 /* Release work tree lock. */
6274 got_worktree_close(worktree);
6277 error = view_loop(view);
6282 const struct got_error *close_err = got_repo_close(repo);
6291 list_commands(FILE *fp)
6295 fprintf(fp, "commands:");
6296 for (i = 0; i < nitems(tog_commands); i++) {
6297 struct tog_cmd *cmd = &tog_commands[i];
6298 fprintf(fp, " %s", cmd->name);
6304 usage(int hflag, int status)
6306 FILE *fp = (status == 0) ? stdout : stderr;
6308 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6311 fprintf(fp, "lazy usage: %s path\n", getprogname());
6318 make_argv(int argc, ...)
6326 argv = calloc(argc, sizeof(char *));
6329 for (i = 0; i < argc; i++) {
6330 argv[i] = strdup(va_arg(ap, char *));
6331 if (argv[i] == NULL)
6340 * Try to convert 'tog path' into a 'tog log path' command.
6341 * The user could simply have mistyped the command rather than knowingly
6342 * provided a path. So check whether argv[0] can in fact be resolved
6343 * to a path in the HEAD commit and print a special error if not.
6344 * This hack is for mpi@ <3
6346 static const struct got_error *
6347 tog_log_with_path(int argc, char *argv[])
6349 const struct got_error *error = NULL, *close_err;
6350 struct tog_cmd *cmd = NULL;
6351 struct got_repository *repo = NULL;
6352 struct got_worktree *worktree = NULL;
6353 struct got_object_id *commit_id = NULL, *id = NULL;
6354 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6355 char *commit_id_str = NULL, **cmd_argv = NULL;
6357 cwd = getcwd(NULL, 0);
6359 return got_error_from_errno("getcwd");
6361 error = got_worktree_open(&worktree, cwd);
6362 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6366 repo_path = strdup(got_worktree_get_repo_path(worktree));
6368 repo_path = strdup(cwd);
6369 if (repo_path == NULL) {
6370 error = got_error_from_errno("strdup");
6374 error = got_repo_open(&repo, repo_path, NULL);
6378 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6383 error = tog_load_refs(repo);
6386 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6387 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6388 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6393 got_worktree_close(worktree);
6397 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6399 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6401 fprintf(stderr, "%s: '%s' is no known command or path\n",
6402 getprogname(), argv[0]);
6407 close_err = got_repo_close(repo);
6412 error = got_object_id_str(&commit_id_str, commit_id);
6416 cmd = &tog_commands[0]; /* log */
6418 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6419 error = cmd->cmd_main(argc, cmd_argv);
6422 close_err = got_repo_close(repo);
6427 got_worktree_close(worktree);
6429 free(commit_id_str);
6436 for (i = 0; i < argc; i++)
6445 main(int argc, char *argv[])
6447 const struct got_error *error = NULL;
6448 struct tog_cmd *cmd = NULL;
6449 int ch, hflag = 0, Vflag = 0;
6450 char **cmd_argv = NULL;
6451 static struct option longopts[] = {
6452 { "version", no_argument, NULL, 'V' },
6456 setlocale(LC_CTYPE, "");
6458 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6478 got_version_print_str();
6483 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6491 /* Build an argument vector which runs a default command. */
6492 cmd = &tog_commands[0];
6494 cmd_argv = make_argv(argc, cmd->name);
6498 /* Did the user specify a command? */
6499 for (i = 0; i < nitems(tog_commands); i++) {
6500 if (strncmp(tog_commands[i].name, argv[0],
6501 strlen(argv[0])) == 0) {
6502 cmd = &tog_commands[i];
6511 /* No command specified; try log with a path */
6512 error = tog_log_with_path(argc, argv);
6517 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6524 for (i = 0; i < argc; i++)
6529 if (error && error->code != GOT_ERR_CANCELLED)
6530 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);