2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED
25 #undef _XOPEN_SOURCE_EXTENDED
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
49 #include "got_opentemp.h"
51 #include "got_cancel.h"
52 #include "got_commit_graph.h"
53 #include "got_blame.h"
54 #include "got_privsep.h"
56 #include "got_worktree.h"
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
66 #define CTRL(x) ((x) & 0x1f)
69 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
78 __dead static void usage(int, int);
79 __dead static void usage_log(void);
80 __dead static void usage_diff(void);
81 __dead static void usage_blame(void);
82 __dead static void usage_tree(void);
83 __dead static void usage_ref(void);
85 static const struct got_error* cmd_log(int, char *[]);
86 static const struct got_error* cmd_diff(int, char *[]);
87 static const struct got_error* cmd_blame(int, char *[]);
88 static const struct got_error* cmd_tree(int, char *[]);
89 static const struct got_error* cmd_ref(int, char *[]);
91 static struct tog_cmd tog_commands[] = {
92 { "log", cmd_log, usage_log },
93 { "diff", cmd_diff, usage_diff },
94 { "blame", cmd_blame, usage_blame },
95 { "tree", cmd_tree, usage_tree },
96 { "ref", cmd_ref, usage_ref },
107 #define TOG_EOF_STRING "(END)"
109 struct commit_queue_entry {
110 TAILQ_ENTRY(commit_queue_entry) entry;
111 struct got_object_id *id;
112 struct got_commit_object *commit;
115 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
116 struct commit_queue {
118 struct commit_queue_head head;
122 SIMPLEQ_ENTRY(tog_color) entry;
126 SIMPLEQ_HEAD(tog_colors, tog_color);
128 static const struct got_error *
129 add_color(struct tog_colors *colors, const char *pattern,
130 int idx, short color)
132 const struct got_error *err = NULL;
133 struct tog_color *tc;
136 if (idx < 1 || idx > COLOR_PAIRS - 1)
139 init_pair(idx, color, -1);
141 tc = calloc(1, sizeof(*tc));
143 return got_error_from_errno("calloc");
144 regerr = regcomp(&tc->regex, pattern,
145 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
147 static char regerr_msg[512];
148 static char err_msg[512];
149 regerror(regerr, &tc->regex, regerr_msg,
151 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
153 err = got_error_msg(GOT_ERR_REGEX, err_msg);
158 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
163 free_colors(struct tog_colors *colors)
165 struct tog_color *tc;
167 while (!SIMPLEQ_EMPTY(colors)) {
168 tc = SIMPLEQ_FIRST(colors);
169 SIMPLEQ_REMOVE_HEAD(colors, entry);
176 get_color(struct tog_colors *colors, int colorpair)
178 struct tog_color *tc = NULL;
180 SIMPLEQ_FOREACH(tc, colors, entry) {
181 if (tc->colorpair == colorpair)
189 default_color_value(const char *envvar)
191 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
192 return COLOR_MAGENTA;
193 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
195 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
197 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
199 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
200 return COLOR_MAGENTA;
201 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
202 return COLOR_MAGENTA;
203 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
205 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
207 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
209 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
211 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
213 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
215 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
216 return COLOR_MAGENTA;
217 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
224 get_color_value(const char *envvar)
226 const char *val = getenv(envvar);
229 return default_color_value(envvar);
231 if (strcasecmp(val, "black") == 0)
233 if (strcasecmp(val, "red") == 0)
235 if (strcasecmp(val, "green") == 0)
237 if (strcasecmp(val, "yellow") == 0)
239 if (strcasecmp(val, "blue") == 0)
241 if (strcasecmp(val, "magenta") == 0)
242 return COLOR_MAGENTA;
243 if (strcasecmp(val, "cyan") == 0)
245 if (strcasecmp(val, "white") == 0)
247 if (strcasecmp(val, "default") == 0)
250 return default_color_value(envvar);
254 struct tog_diff_view_state {
255 struct got_object_id *id1, *id2;
256 const char *label1, *label2;
258 int first_displayed_line;
259 int last_displayed_line;
262 int ignore_whitespace;
264 struct got_repository *repo;
265 struct got_reflist_head refs;
266 struct tog_colors colors;
272 /* passed from log view; may be NULL */
273 struct tog_view *log_view;
276 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
278 struct tog_log_thread_args {
279 pthread_cond_t need_commits;
280 pthread_cond_t commit_loaded;
282 struct got_commit_graph *graph;
283 struct commit_queue *commits;
284 const char *in_repo_path;
285 struct got_object_id *start_id;
286 struct got_repository *repo;
289 struct commit_queue_entry **first_displayed_entry;
290 struct commit_queue_entry **selected_entry;
292 int *search_next_done;
296 struct tog_log_view_state {
297 struct commit_queue commits;
298 struct commit_queue_entry *first_displayed_entry;
299 struct commit_queue_entry *last_displayed_entry;
300 struct commit_queue_entry *selected_entry;
305 struct got_repository *repo;
306 struct got_reflist_head refs;
307 struct got_object_id *start_id;
310 struct tog_log_thread_args thread_args;
311 struct commit_queue_entry *matched_entry;
312 struct commit_queue_entry *search_entry;
313 struct tog_colors colors;
316 #define TOG_COLOR_DIFF_MINUS 1
317 #define TOG_COLOR_DIFF_PLUS 2
318 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
319 #define TOG_COLOR_DIFF_META 4
320 #define TOG_COLOR_TREE_SUBMODULE 5
321 #define TOG_COLOR_TREE_SYMLINK 6
322 #define TOG_COLOR_TREE_DIRECTORY 7
323 #define TOG_COLOR_TREE_EXECUTABLE 8
324 #define TOG_COLOR_COMMIT 9
325 #define TOG_COLOR_AUTHOR 10
326 #define TOG_COLOR_DATE 11
327 #define TOG_COLOR_REFS_HEADS 12
328 #define TOG_COLOR_REFS_TAGS 13
329 #define TOG_COLOR_REFS_REMOTES 14
331 struct tog_blame_cb_args {
332 struct tog_blame_line *lines; /* one per line */
335 struct tog_view *view;
336 struct got_object_id *commit_id;
340 struct tog_blame_thread_args {
342 struct got_repository *repo;
343 struct tog_blame_cb_args *cb_args;
345 got_cancel_cb cancel_cb;
352 struct tog_blame_line *lines;
356 struct tog_blame_thread_args thread_args;
357 struct tog_blame_cb_args cb_args;
361 struct tog_blame_view_state {
362 int first_displayed_line;
363 int last_displayed_line;
368 struct got_object_id_queue blamed_commits;
369 struct got_object_qid *blamed_commit;
371 struct got_repository *repo;
372 struct got_object_id *commit_id;
373 struct tog_blame blame;
375 struct tog_colors colors;
378 struct tog_parent_tree {
379 TAILQ_ENTRY(tog_parent_tree) entry;
380 struct got_tree_object *tree;
381 struct got_tree_entry *first_displayed_entry;
382 struct got_tree_entry *selected_entry;
386 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
388 struct tog_tree_view_state {
390 struct got_tree_object *root;
391 struct got_tree_object *tree;
392 struct got_tree_entry *first_displayed_entry;
393 struct got_tree_entry *last_displayed_entry;
394 struct got_tree_entry *selected_entry;
395 int ndisplayed, selected, show_ids;
396 struct tog_parent_trees parents;
397 struct got_object_id *commit_id;
399 struct got_repository *repo;
400 struct got_tree_entry *matched_entry;
401 struct tog_colors colors;
404 struct tog_reflist_entry {
405 TAILQ_ENTRY(tog_reflist_entry) entry;
406 struct got_reference *ref;
410 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
412 struct tog_ref_view_state {
413 struct got_reflist_head simplerefs; /* SIMPLEQ */
414 struct tog_reflist_head refs; /* TAILQ */
415 struct tog_reflist_entry *first_displayed_entry;
416 struct tog_reflist_entry *last_displayed_entry;
417 struct tog_reflist_entry *selected_entry;
418 int nrefs, ndisplayed, selected, show_ids;
419 struct got_repository *repo;
420 struct tog_reflist_entry *matched_entry;
421 struct tog_colors colors;
425 * We implement two types of views: parent views and child views.
427 * The 'Tab' key switches focus between a parent view and its child view.
428 * Child views are shown side-by-side to their parent view, provided
429 * there is enough screen estate.
431 * When a new view is opened from within a parent view, this new view
432 * becomes a child view of the parent view, replacing any existing child.
434 * When a new view is opened from within a child view, this new view
435 * becomes a parent view which will obscure the views below until the
436 * user quits the new parent view by typing 'q'.
438 * This list of views contains parent views only.
439 * Child views are only pointed to by their parent view.
441 TAILQ_HEAD(tog_view_list_head, tog_view);
444 TAILQ_ENTRY(tog_view) entry;
447 int nlines, ncols, begin_y, begin_x;
448 int lines, cols; /* copies of LINES and COLS */
449 int focussed; /* Only set on one parent or child view at a time. */
451 struct tog_view *parent;
452 struct tog_view *child;
455 * This flag is initially set on parent views when a new child view
456 * is created. It gets toggled when the 'Tab' key switches focus
457 * between parent and child.
458 * The flag indicates whether focus should be passed on to our child
459 * view if this parent view gets picked for focus after another parent
460 * view was closed. This prevents child views from losing focus in such
465 /* type-specific state */
466 enum tog_view_type type;
468 struct tog_diff_view_state diff;
469 struct tog_log_view_state log;
470 struct tog_blame_view_state blame;
471 struct tog_tree_view_state tree;
472 struct tog_ref_view_state ref;
475 const struct got_error *(*show)(struct tog_view *);
476 const struct got_error *(*input)(struct tog_view **,
477 struct tog_view *, int);
478 const struct got_error *(*close)(struct tog_view *);
480 const struct got_error *(*search_start)(struct tog_view *);
481 const struct got_error *(*search_next)(struct tog_view *);
483 #define TOG_SEARCH_FORWARD 1
484 #define TOG_SEARCH_BACKWARD 2
485 int search_next_done;
486 #define TOG_SEARCH_HAVE_MORE 1
487 #define TOG_SEARCH_NO_MORE 2
488 #define TOG_SEARCH_HAVE_NONE 3
493 static const struct got_error *open_diff_view(struct tog_view *,
494 struct got_object_id *, struct got_object_id *,
495 const char *, const char *, int, int, int, struct tog_view *,
496 struct got_repository *);
497 static const struct got_error *show_diff_view(struct tog_view *);
498 static const struct got_error *input_diff_view(struct tog_view **,
499 struct tog_view *, int);
500 static const struct got_error* close_diff_view(struct tog_view *);
501 static const struct got_error *search_start_diff_view(struct tog_view *);
502 static const struct got_error *search_next_diff_view(struct tog_view *);
504 static const struct got_error *open_log_view(struct tog_view *,
505 struct got_object_id *, struct got_repository *,
506 const char *, const char *, int);
507 static const struct got_error * show_log_view(struct tog_view *);
508 static const struct got_error *input_log_view(struct tog_view **,
509 struct tog_view *, int);
510 static const struct got_error *close_log_view(struct tog_view *);
511 static const struct got_error *search_start_log_view(struct tog_view *);
512 static const struct got_error *search_next_log_view(struct tog_view *);
514 static const struct got_error *open_blame_view(struct tog_view *, char *,
515 struct got_object_id *, struct got_repository *);
516 static const struct got_error *show_blame_view(struct tog_view *);
517 static const struct got_error *input_blame_view(struct tog_view **,
518 struct tog_view *, int);
519 static const struct got_error *close_blame_view(struct tog_view *);
520 static const struct got_error *search_start_blame_view(struct tog_view *);
521 static const struct got_error *search_next_blame_view(struct tog_view *);
523 static const struct got_error *open_tree_view(struct tog_view *,
524 struct got_tree_object *, struct got_object_id *, const char *,
525 struct got_repository *);
526 static const struct got_error *show_tree_view(struct tog_view *);
527 static const struct got_error *input_tree_view(struct tog_view **,
528 struct tog_view *, int);
529 static const struct got_error *close_tree_view(struct tog_view *);
530 static const struct got_error *search_start_tree_view(struct tog_view *);
531 static const struct got_error *search_next_tree_view(struct tog_view *);
533 static const struct got_error *open_ref_view(struct tog_view *,
534 struct got_repository *);
535 static const struct got_error *show_ref_view(struct tog_view *);
536 static const struct got_error *input_ref_view(struct tog_view **,
537 struct tog_view *, int);
538 static const struct got_error *close_ref_view(struct tog_view *);
539 static const struct got_error *search_start_ref_view(struct tog_view *);
540 static const struct got_error *search_next_ref_view(struct tog_view *);
542 static volatile sig_atomic_t tog_sigwinch_received;
543 static volatile sig_atomic_t tog_sigpipe_received;
544 static volatile sig_atomic_t tog_sigcont_received;
547 tog_sigwinch(int signo)
549 tog_sigwinch_received = 1;
553 tog_sigpipe(int signo)
555 tog_sigpipe_received = 1;
559 tog_sigcont(int signo)
561 tog_sigcont_received = 1;
564 static const struct got_error *
565 view_close(struct tog_view *view)
567 const struct got_error *err = NULL;
570 view_close(view->child);
574 err = view->close(view);
576 del_panel(view->panel);
578 delwin(view->window);
583 static struct tog_view *
584 view_open(int nlines, int ncols, int begin_y, int begin_x,
585 enum tog_view_type type)
587 struct tog_view *view = calloc(1, sizeof(*view));
595 view->nlines = nlines ? nlines : LINES - begin_y;
596 view->ncols = ncols ? ncols : COLS - begin_x;
597 view->begin_y = begin_y;
598 view->begin_x = begin_x;
599 view->window = newwin(nlines, ncols, begin_y, begin_x);
600 if (view->window == NULL) {
604 view->panel = new_panel(view->window);
605 if (view->panel == NULL ||
606 set_panel_userptr(view->panel, view) != OK) {
611 keypad(view->window, TRUE);
616 view_split_begin_x(int begin_x)
618 if (begin_x > 0 || COLS < 120)
620 return (COLS - MAX(COLS / 2, 80));
623 static const struct got_error *view_resize(struct tog_view *);
625 static const struct got_error *
626 view_splitscreen(struct tog_view *view)
628 const struct got_error *err = NULL;
631 view->begin_x = view_split_begin_x(0);
632 view->nlines = LINES;
633 view->ncols = COLS - view->begin_x;
636 err = view_resize(view);
640 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
641 return got_error_from_errno("mvwin");
646 static const struct got_error *
647 view_fullscreen(struct tog_view *view)
649 const struct got_error *err = NULL;
653 view->nlines = LINES;
657 err = view_resize(view);
661 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
662 return got_error_from_errno("mvwin");
668 view_is_parent_view(struct tog_view *view)
670 return view->parent == NULL;
673 static const struct got_error *
674 view_resize(struct tog_view *view)
678 if (view->lines > LINES)
679 nlines = view->nlines - (view->lines - LINES);
681 nlines = view->nlines + (LINES - view->lines);
683 if (view->cols > COLS)
684 ncols = view->ncols - (view->cols - COLS);
686 ncols = view->ncols + (COLS - view->cols);
688 if (wresize(view->window, nlines, ncols) == ERR)
689 return got_error_from_errno("wresize");
690 if (replace_panel(view->panel, view->window) == ERR)
691 return got_error_from_errno("replace_panel");
692 wclear(view->window);
694 view->nlines = nlines;
700 view->child->begin_x = view_split_begin_x(view->begin_x);
701 if (view->child->begin_x == 0) {
702 view_fullscreen(view->child);
703 if (view->child->focussed)
704 show_panel(view->child->panel);
706 show_panel(view->panel);
708 view_splitscreen(view->child);
709 show_panel(view->child->panel);
716 static const struct got_error *
717 view_close_child(struct tog_view *view)
719 const struct got_error *err = NULL;
721 if (view->child == NULL)
724 err = view_close(view->child);
730 view_set_child(struct tog_view *view, struct tog_view *child)
733 child->parent = view;
737 view_is_splitscreen(struct tog_view *view)
739 return view->begin_x > 0;
748 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
749 cols = 80; /* Default */
755 resize_term(lines, cols);
758 static const struct got_error *
759 view_search_start(struct tog_view *view)
761 const struct got_error *err = NULL;
765 if (view->nlines < 1)
768 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
769 wclrtoeol(view->window);
773 ret = wgetnstr(view->window, pattern, sizeof(pattern));
779 if (view->searching) {
780 regfree(&view->regex);
784 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
785 err = view->search_start(view);
787 regfree(&view->regex);
790 view->searching = TOG_SEARCH_FORWARD;
791 view->search_next_done = 0;
792 view->search_next(view);
798 static const struct got_error *
799 view_input(struct tog_view **new, int *done, struct tog_view *view,
800 struct tog_view_list_head *views)
802 const struct got_error *err = NULL;
808 /* Clear "no matches" indicator. */
809 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
810 view->search_next_done == TOG_SEARCH_HAVE_NONE)
811 view->search_next_done = TOG_SEARCH_HAVE_MORE;
813 if (view->searching && !view->search_next_done) {
814 errcode = pthread_mutex_unlock(&tog_mutex);
816 return got_error_set_errno(errcode,
817 "pthread_mutex_unlock");
819 errcode = pthread_mutex_lock(&tog_mutex);
821 return got_error_set_errno(errcode,
822 "pthread_mutex_lock");
823 view->search_next(view);
827 nodelay(stdscr, FALSE);
828 /* Allow threads to make progress while we are waiting for input. */
829 errcode = pthread_mutex_unlock(&tog_mutex);
831 return got_error_set_errno(errcode, "pthread_mutex_unlock");
832 ch = wgetch(view->window);
833 errcode = pthread_mutex_lock(&tog_mutex);
835 return got_error_set_errno(errcode, "pthread_mutex_lock");
836 nodelay(stdscr, TRUE);
838 if (tog_sigwinch_received || tog_sigcont_received) {
840 tog_sigwinch_received = 0;
841 tog_sigcont_received = 0;
842 TAILQ_FOREACH(v, views, entry) {
843 err = view_resize(v);
846 err = v->input(new, v, KEY_RESIZE);
850 err = view_resize(v->child);
853 err = v->child->input(new, v->child,
867 view->child->focussed = 1;
868 view->focus_child = 1;
869 } else if (view->parent) {
871 view->parent->focussed = 1;
872 view->parent->focus_child = 0;
876 err = view->input(new, view, ch);
883 if (view_is_parent_view(view)) {
884 if (view->child == NULL)
886 if (view_is_splitscreen(view->child)) {
888 view->child->focussed = 1;
889 err = view_fullscreen(view->child);
891 err = view_splitscreen(view->child);
894 err = view->child->input(new, view->child,
897 if (view_is_splitscreen(view)) {
898 view->parent->focussed = 0;
900 err = view_fullscreen(view);
902 err = view_splitscreen(view);
906 err = view->input(new, view, KEY_RESIZE);
912 if (view->search_start)
913 view_search_start(view);
915 err = view->input(new, view, ch);
919 if (view->search_next) {
920 view->searching = (ch == 'n' ?
921 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
922 view->search_next_done = 0;
923 view->search_next(view);
925 err = view->input(new, view, ch);
928 err = view->input(new, view, ch);
936 view_vborder(struct tog_view *view)
939 const struct tog_view *view_above;
942 return view_vborder(view->parent);
944 panel = panel_above(view->panel);
948 view_above = panel_userptr(panel);
949 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
950 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
954 view_needs_focus_indication(struct tog_view *view)
956 if (view_is_parent_view(view)) {
957 if (view->child == NULL || view->child->focussed)
959 if (!view_is_splitscreen(view->child))
961 } else if (!view_is_splitscreen(view))
964 return view->focussed;
967 static const struct got_error *
968 view_loop(struct tog_view *view)
970 const struct got_error *err = NULL;
971 struct tog_view_list_head views;
972 struct tog_view *new_view;
973 int fast_refresh = 10;
974 int done = 0, errcode;
976 errcode = pthread_mutex_lock(&tog_mutex);
978 return got_error_set_errno(errcode, "pthread_mutex_lock");
981 TAILQ_INSERT_HEAD(&views, view, entry);
984 err = view->show(view);
989 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
990 /* Refresh fast during initialization, then become slower. */
991 if (fast_refresh && fast_refresh-- == 0)
992 halfdelay(10); /* switch to once per second */
994 err = view_input(&new_view, &done, view, &views);
998 struct tog_view *v, *prev = NULL;
1000 if (view_is_parent_view(view))
1001 prev = TAILQ_PREV(view, tog_view_list_head,
1003 else if (view->parent)
1004 prev = view->parent;
1007 view->parent->child = NULL;
1008 view->parent->focus_child = 0;
1010 TAILQ_REMOVE(&views, view, entry);
1012 err = view_close(view);
1017 TAILQ_FOREACH(v, &views, entry) {
1021 if (view == NULL && new_view == NULL) {
1022 /* No view has focus. Try to pick one. */
1025 else if (!TAILQ_EMPTY(&views)) {
1026 view = TAILQ_LAST(&views,
1027 tog_view_list_head);
1030 if (view->focus_child) {
1031 view->child->focussed = 1;
1039 struct tog_view *v, *t;
1040 /* Only allow one parent view per type. */
1041 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1042 if (v->type != new_view->type)
1044 TAILQ_REMOVE(&views, v, entry);
1045 err = view_close(v);
1050 TAILQ_INSERT_TAIL(&views, new_view, entry);
1054 if (view_is_parent_view(view)) {
1055 if (view->child && view->child->focussed)
1058 if (view->parent && view->parent->focussed)
1059 view = view->parent;
1061 show_panel(view->panel);
1062 if (view->child && view_is_splitscreen(view->child))
1063 show_panel(view->child->panel);
1064 if (view->parent && view_is_splitscreen(view)) {
1065 err = view->parent->show(view->parent);
1069 err = view->show(view);
1073 err = view->child->show(view->child);
1082 while (!TAILQ_EMPTY(&views)) {
1083 view = TAILQ_FIRST(&views);
1084 TAILQ_REMOVE(&views, view, entry);
1088 errcode = pthread_mutex_unlock(&tog_mutex);
1089 if (errcode && err == NULL)
1090 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1100 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1105 /* Create newly allocated wide-character string equivalent to a byte string. */
1106 static const struct got_error *
1107 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1110 const struct got_error *err = NULL;
1113 *wlen = mbstowcs(NULL, s, 0);
1114 if (*wlen == (size_t)-1) {
1116 if (errno != EILSEQ)
1117 return got_error_from_errno("mbstowcs");
1119 /* byte string invalid in current encoding; try to "fix" it */
1120 err = got_mbsavis(&vis, &vislen, s);
1123 *wlen = mbstowcs(NULL, vis, 0);
1124 if (*wlen == (size_t)-1) {
1125 err = got_error_from_errno("mbstowcs"); /* give up */
1130 *ws = calloc(*wlen + 1, sizeof(**ws));
1132 err = got_error_from_errno("calloc");
1136 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1137 err = got_error_from_errno("mbstowcs");
1148 /* Format a line for display, ensuring that it won't overflow a width limit. */
1149 static const struct got_error *
1150 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1153 const struct got_error *err = NULL;
1155 wchar_t *wline = NULL;
1162 err = mbs2ws(&wline, &wlen, line);
1166 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1167 wline[wlen - 1] = L'\0';
1170 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1171 wline[wlen - 1] = L'\0';
1177 int width = wcwidth(wline[i]);
1184 if (width == 1 || width == 2) {
1185 if (cols + width > wlimit)
1189 } else if (width == -1) {
1190 if (wline[i] == L'\t') {
1192 ((cols + col_tab_align) % TABSIZE);
1193 if (cols + width > wlimit)
1199 err = got_error_from_errno("wcwidth");
1214 static const struct got_error*
1215 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1216 struct got_object_id *id, struct got_repository *repo)
1218 static const struct got_error *err = NULL;
1219 struct got_reflist_entry *re;
1225 SIMPLEQ_FOREACH(re, refs, entry) {
1226 struct got_tag_object *tag = NULL;
1227 struct got_object_id *ref_id;
1230 name = got_ref_get_name(re->ref);
1231 if (strcmp(name, GOT_REF_HEAD) == 0)
1233 if (strncmp(name, "refs/", 5) == 0)
1235 if (strncmp(name, "got/", 4) == 0)
1237 if (strncmp(name, "heads/", 6) == 0)
1239 if (strncmp(name, "remotes/", 8) == 0) {
1241 s = strstr(name, "/" GOT_REF_HEAD);
1242 if (s != NULL && s[strlen(s)] == '\0')
1245 err = got_ref_resolve(&ref_id, repo, re->ref);
1248 if (strncmp(name, "tags/", 5) == 0) {
1249 err = got_object_open_as_tag(&tag, repo, ref_id);
1251 if (err->code != GOT_ERR_OBJ_TYPE) {
1255 /* Ref points at something other than a tag. */
1260 cmp = got_object_id_cmp(tag ?
1261 got_object_tag_get_object_id(tag) : ref_id, id);
1264 got_object_tag_close(tag);
1268 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1269 s ? ", " : "", name) == -1) {
1270 err = got_error_from_errno("asprintf");
1281 static const struct got_error *
1282 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1285 char *smallerthan, *at;
1287 smallerthan = strchr(author, '<');
1288 if (smallerthan && smallerthan[1] != '\0')
1289 author = smallerthan + 1;
1290 at = strchr(author, '@');
1293 return format_line(wauthor, author_width, author, limit, col_tab_align);
1296 static const struct got_error *
1297 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1298 struct got_object_id *id, const size_t date_display_cols,
1299 int author_display_cols)
1301 struct tog_log_view_state *s = &view->state.log;
1302 const struct got_error *err = NULL;
1303 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1304 char *logmsg0 = NULL, *logmsg = NULL;
1305 char *author = NULL;
1306 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1307 int author_width, logmsg_width;
1308 char *newline, *line = NULL;
1310 const int avail = view->ncols;
1312 time_t committer_time;
1313 struct tog_color *tc;
1315 committer_time = got_object_commit_get_committer_time(commit);
1316 if (localtime_r(&committer_time, &tm) == NULL)
1317 return got_error_from_errno("localtime_r");
1318 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1320 return got_error(GOT_ERR_NO_SPACE);
1322 if (avail <= date_display_cols)
1323 limit = MIN(sizeof(datebuf) - 1, avail);
1325 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1326 tc = get_color(&s->colors, TOG_COLOR_DATE);
1328 wattr_on(view->window,
1329 COLOR_PAIR(tc->colorpair), NULL);
1330 waddnstr(view->window, datebuf, limit);
1332 wattr_off(view->window,
1333 COLOR_PAIR(tc->colorpair), NULL);
1340 err = got_object_id_str(&id_str, id);
1343 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1345 wattr_on(view->window,
1346 COLOR_PAIR(tc->colorpair), NULL);
1347 wprintw(view->window, "%.8s ", id_str);
1349 wattr_off(view->window,
1350 COLOR_PAIR(tc->colorpair), NULL);
1357 author = strdup(got_object_commit_get_author(commit));
1358 if (author == NULL) {
1359 err = got_error_from_errno("strdup");
1362 err = format_author(&wauthor, &author_width, author, avail - col, col);
1365 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1367 wattr_on(view->window,
1368 COLOR_PAIR(tc->colorpair), NULL);
1369 waddwstr(view->window, wauthor);
1371 wattr_off(view->window,
1372 COLOR_PAIR(tc->colorpair), NULL);
1373 col += author_width;
1374 while (col < avail && author_width < author_display_cols + 2) {
1375 waddch(view->window, ' ');
1382 err = got_object_commit_get_logmsg(&logmsg0, commit);
1386 while (*logmsg == '\n')
1388 newline = strchr(logmsg, '\n');
1391 limit = avail - col;
1392 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1395 waddwstr(view->window, wlogmsg);
1396 col += logmsg_width;
1397 while (col < avail) {
1398 waddch(view->window, ' ');
1410 static struct commit_queue_entry *
1411 alloc_commit_queue_entry(struct got_commit_object *commit,
1412 struct got_object_id *id)
1414 struct commit_queue_entry *entry;
1416 entry = calloc(1, sizeof(*entry));
1421 entry->commit = commit;
1426 pop_commit(struct commit_queue *commits)
1428 struct commit_queue_entry *entry;
1430 entry = TAILQ_FIRST(&commits->head);
1431 TAILQ_REMOVE(&commits->head, entry, entry);
1432 got_object_commit_close(entry->commit);
1433 commits->ncommits--;
1434 /* Don't free entry->id! It is owned by the commit graph. */
1439 free_commits(struct commit_queue *commits)
1441 while (!TAILQ_EMPTY(&commits->head))
1442 pop_commit(commits);
1445 static const struct got_error *
1446 match_commit(int *have_match, struct got_object_id *id,
1447 struct got_commit_object *commit, regex_t *regex)
1449 const struct got_error *err = NULL;
1450 regmatch_t regmatch;
1451 char *id_str = NULL, *logmsg = NULL;
1455 err = got_object_id_str(&id_str, id);
1459 err = got_object_commit_get_logmsg(&logmsg, commit);
1463 if (regexec(regex, got_object_commit_get_author(commit), 1,
1464 ®match, 0) == 0 ||
1465 regexec(regex, got_object_commit_get_committer(commit), 1,
1466 ®match, 0) == 0 ||
1467 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1468 regexec(regex, logmsg, 1, ®match, 0) == 0)
1476 static const struct got_error *
1477 queue_commits(struct tog_log_thread_args *a)
1479 const struct got_error *err = NULL;
1482 * We keep all commits open throughout the lifetime of the log
1483 * view in order to avoid having to re-fetch commits from disk
1484 * while updating the display.
1487 struct got_object_id *id;
1488 struct got_commit_object *commit;
1489 struct commit_queue_entry *entry;
1492 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1494 if (err || id == NULL)
1497 err = got_object_open_as_commit(&commit, a->repo, id);
1500 entry = alloc_commit_queue_entry(commit, id);
1501 if (entry == NULL) {
1502 err = got_error_from_errno("alloc_commit_queue_entry");
1506 errcode = pthread_mutex_lock(&tog_mutex);
1508 err = got_error_set_errno(errcode,
1509 "pthread_mutex_lock");
1513 entry->idx = a->commits->ncommits;
1514 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1515 a->commits->ncommits++;
1517 if (*a->searching == TOG_SEARCH_FORWARD &&
1518 !*a->search_next_done) {
1520 err = match_commit(&have_match, id, commit, a->regex);
1524 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1527 errcode = pthread_mutex_unlock(&tog_mutex);
1528 if (errcode && err == NULL)
1529 err = got_error_set_errno(errcode,
1530 "pthread_mutex_unlock");
1533 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1539 select_commit(struct tog_log_view_state *s)
1541 struct commit_queue_entry *entry;
1544 entry = s->first_displayed_entry;
1546 if (ncommits == s->selected) {
1547 s->selected_entry = entry;
1550 entry = TAILQ_NEXT(entry, entry);
1555 static const struct got_error *
1556 draw_commits(struct tog_view *view)
1558 const struct got_error *err = NULL;
1559 struct tog_log_view_state *s = &view->state.log;
1560 struct commit_queue_entry *entry = s->selected_entry;
1561 const int limit = view->nlines;
1563 int ncommits, author_cols = 4;
1564 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1565 char *refs_str = NULL;
1567 struct tog_color *tc;
1568 static const size_t date_display_cols = 12;
1570 if (s->selected_entry &&
1571 !(view->searching && view->search_next_done == 0)) {
1572 err = got_object_id_str(&id_str, s->selected_entry->id);
1575 err = build_refs_str(&refs_str, &s->refs,
1576 s->selected_entry->id, s->repo);
1581 if (s->thread_args.commits_needed == 0)
1582 halfdelay(10); /* disable fast refresh */
1584 if (s->thread_args.commits_needed > 0) {
1585 if (asprintf(&ncommits_str, " [%d/%d] %s",
1586 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1587 (view->searching && !view->search_next_done) ?
1588 "searching..." : "loading...") == -1) {
1589 err = got_error_from_errno("asprintf");
1593 const char *search_str = NULL;
1595 if (view->searching) {
1596 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1597 search_str = "no more matches";
1598 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1599 search_str = "no matches found";
1600 else if (!view->search_next_done)
1601 search_str = "searching...";
1604 if (asprintf(&ncommits_str, " [%d/%d] %s",
1605 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1606 search_str ? search_str :
1607 (refs_str ? refs_str : "")) == -1) {
1608 err = got_error_from_errno("asprintf");
1613 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1614 if (asprintf(&header, "commit %s %s%s",
1615 id_str ? id_str : "........................................",
1616 s->in_repo_path, ncommits_str) == -1) {
1617 err = got_error_from_errno("asprintf");
1621 } else if (asprintf(&header, "commit %s%s",
1622 id_str ? id_str : "........................................",
1623 ncommits_str) == -1) {
1624 err = got_error_from_errno("asprintf");
1628 err = format_line(&wline, &width, header, view->ncols, 0);
1632 werase(view->window);
1634 if (view_needs_focus_indication(view))
1635 wstandout(view->window);
1636 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1638 wattr_on(view->window,
1639 COLOR_PAIR(tc->colorpair), NULL);
1640 waddwstr(view->window, wline);
1642 wattr_off(view->window,
1643 COLOR_PAIR(tc->colorpair), NULL);
1644 while (width < view->ncols) {
1645 waddch(view->window, ' ');
1648 if (view_needs_focus_indication(view))
1649 wstandend(view->window);
1654 /* Grow author column size if necessary. */
1655 entry = s->first_displayed_entry;
1661 if (ncommits >= limit - 1)
1663 author = strdup(got_object_commit_get_author(entry->commit));
1664 if (author == NULL) {
1665 err = got_error_from_errno("strdup");
1668 err = format_author(&wauthor, &width, author, COLS,
1670 if (author_cols < width)
1671 author_cols = width;
1675 entry = TAILQ_NEXT(entry, entry);
1678 entry = s->first_displayed_entry;
1679 s->last_displayed_entry = s->first_displayed_entry;
1682 if (ncommits >= limit - 1)
1684 if (ncommits == s->selected)
1685 wstandout(view->window);
1686 err = draw_commit(view, entry->commit, entry->id,
1687 date_display_cols, author_cols);
1688 if (ncommits == s->selected)
1689 wstandend(view->window);
1693 s->last_displayed_entry = entry;
1694 entry = TAILQ_NEXT(entry, entry);
1707 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1709 struct commit_queue_entry *entry;
1712 entry = TAILQ_FIRST(&s->commits.head);
1713 if (s->first_displayed_entry == entry)
1716 entry = s->first_displayed_entry;
1717 while (entry && nscrolled < maxscroll) {
1718 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1720 s->first_displayed_entry = entry;
1726 static const struct got_error *
1727 trigger_log_thread(struct tog_view *view, int wait)
1729 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1732 halfdelay(1); /* fast refresh while loading commits */
1734 while (ta->commits_needed > 0) {
1735 if (ta->log_complete)
1738 /* Wake the log thread. */
1739 errcode = pthread_cond_signal(&ta->need_commits);
1741 return got_error_set_errno(errcode,
1742 "pthread_cond_signal");
1745 * The mutex will be released while the view loop waits
1746 * in wgetch(), at which time the log thread will run.
1751 /* Display progress update in log view. */
1752 show_log_view(view);
1756 /* Wait right here while next commit is being loaded. */
1757 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1759 return got_error_set_errno(errcode,
1760 "pthread_cond_wait");
1762 /* Display progress update in log view. */
1763 show_log_view(view);
1771 static const struct got_error *
1772 log_scroll_down(struct tog_view *view, int maxscroll)
1774 struct tog_log_view_state *s = &view->state.log;
1775 const struct got_error *err = NULL;
1776 struct commit_queue_entry *pentry;
1777 int nscrolled = 0, ncommits_needed;
1779 if (s->last_displayed_entry == NULL)
1782 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1783 if (s->commits.ncommits < ncommits_needed &&
1784 !s->thread_args.log_complete) {
1786 * Ask the log thread for required amount of commits.
1788 s->thread_args.commits_needed += maxscroll;
1789 err = trigger_log_thread(view, 1);
1795 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1799 s->last_displayed_entry = pentry;
1801 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1804 s->first_displayed_entry = pentry;
1805 } while (++nscrolled < maxscroll);
1810 static const struct got_error *
1811 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1812 struct got_commit_object *commit, struct got_object_id *commit_id,
1813 struct tog_view *log_view, struct got_repository *repo)
1815 const struct got_error *err;
1816 struct got_object_qid *parent_id;
1817 struct tog_view *diff_view;
1819 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1820 if (diff_view == NULL)
1821 return got_error_from_errno("view_open");
1823 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1824 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1825 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1827 *new_view = diff_view;
1831 static const struct got_error *
1832 tree_view_visit_subtree(struct tog_tree_view_state *s,
1833 struct got_tree_object *subtree)
1835 struct tog_parent_tree *parent;
1837 parent = calloc(1, sizeof(*parent));
1839 return got_error_from_errno("calloc");
1841 parent->tree = s->tree;
1842 parent->first_displayed_entry = s->first_displayed_entry;
1843 parent->selected_entry = s->selected_entry;
1844 parent->selected = s->selected;
1845 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1848 s->first_displayed_entry = NULL;
1852 static const struct got_error *
1853 tree_view_walk_path(struct tog_tree_view_state *s,
1854 struct got_object_id *commit_id, const char *path)
1856 const struct got_error *err = NULL;
1857 struct got_tree_object *tree = NULL;
1859 char *slash, *subpath = NULL;
1861 /* Walk the path and open corresponding tree objects. */
1864 struct got_tree_entry *te;
1865 struct got_object_id *tree_id;
1871 /* Ensure the correct subtree entry is selected. */
1872 slash = strchr(p, '/');
1874 te_name = strdup(p);
1876 te_name = strndup(p, slash - p);
1877 if (te_name == NULL) {
1878 err = got_error_from_errno("strndup");
1881 te = got_object_tree_find_entry(s->tree, te_name);
1883 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1888 s->first_displayed_entry = s->selected_entry = te;
1890 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1891 break; /* jump to this file's entry */
1893 slash = strchr(p, '/');
1895 subpath = strndup(path, slash - path);
1897 subpath = strdup(path);
1898 if (subpath == NULL) {
1899 err = got_error_from_errno("strdup");
1903 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1908 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1913 err = tree_view_visit_subtree(s, tree);
1915 got_object_tree_close(tree);
1929 static const struct got_error *
1930 browse_commit_tree(struct tog_view **new_view, int begin_x,
1931 struct commit_queue_entry *entry, const char *path,
1932 const char *head_ref_name, struct got_repository *repo)
1934 const struct got_error *err = NULL;
1935 struct got_tree_object *tree;
1936 struct tog_tree_view_state *s;
1937 struct tog_view *tree_view;
1939 err = got_object_open_as_tree(&tree, repo,
1940 got_object_commit_get_tree_id(entry->commit));
1944 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1945 if (tree_view == NULL)
1946 return got_error_from_errno("view_open");
1948 err = open_tree_view(tree_view, tree, entry->id, head_ref_name, repo);
1950 got_object_tree_close(tree);
1953 s = &tree_view->state.tree;
1955 *new_view = tree_view;
1957 if (got_path_is_root_dir(path))
1960 return tree_view_walk_path(s, entry->id, path);
1963 static const struct got_error *
1964 block_signals_used_by_main_thread(void)
1969 if (sigemptyset(&sigset) == -1)
1970 return got_error_from_errno("sigemptyset");
1972 /* tog handles SIGWINCH and SIGCONT */
1973 if (sigaddset(&sigset, SIGWINCH) == -1)
1974 return got_error_from_errno("sigaddset");
1975 if (sigaddset(&sigset, SIGCONT) == -1)
1976 return got_error_from_errno("sigaddset");
1978 /* ncurses handles SIGTSTP */
1979 if (sigaddset(&sigset, SIGTSTP) == -1)
1980 return got_error_from_errno("sigaddset");
1982 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1984 return got_error_set_errno(errcode, "pthread_sigmask");
1990 log_thread(void *arg)
1992 const struct got_error *err = NULL;
1994 struct tog_log_thread_args *a = arg;
1997 err = block_signals_used_by_main_thread();
2001 while (!done && !err && !tog_sigpipe_received) {
2002 err = queue_commits(a);
2004 if (err->code != GOT_ERR_ITER_COMPLETED)
2008 } else if (a->commits_needed > 0)
2009 a->commits_needed--;
2011 errcode = pthread_mutex_lock(&tog_mutex);
2013 err = got_error_set_errno(errcode,
2014 "pthread_mutex_lock");
2016 } else if (*a->quit)
2018 else if (*a->first_displayed_entry == NULL) {
2019 *a->first_displayed_entry =
2020 TAILQ_FIRST(&a->commits->head);
2021 *a->selected_entry = *a->first_displayed_entry;
2024 errcode = pthread_cond_signal(&a->commit_loaded);
2026 err = got_error_set_errno(errcode,
2027 "pthread_cond_signal");
2028 pthread_mutex_unlock(&tog_mutex);
2033 a->commits_needed = 0;
2035 if (a->commits_needed == 0) {
2036 errcode = pthread_cond_wait(&a->need_commits,
2039 err = got_error_set_errno(errcode,
2040 "pthread_cond_wait");
2046 errcode = pthread_mutex_unlock(&tog_mutex);
2047 if (errcode && err == NULL)
2048 err = got_error_set_errno(errcode,
2049 "pthread_mutex_unlock");
2051 a->log_complete = 1;
2055 static const struct got_error *
2056 stop_log_thread(struct tog_log_view_state *s)
2058 const struct got_error *err = NULL;
2063 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2065 return got_error_set_errno(errcode,
2066 "pthread_cond_signal");
2067 errcode = pthread_mutex_unlock(&tog_mutex);
2069 return got_error_set_errno(errcode,
2070 "pthread_mutex_unlock");
2071 errcode = pthread_join(s->thread, (void **)&err);
2073 return got_error_set_errno(errcode, "pthread_join");
2074 errcode = pthread_mutex_lock(&tog_mutex);
2076 return got_error_set_errno(errcode,
2077 "pthread_mutex_lock");
2081 if (s->thread_args.repo) {
2082 got_repo_close(s->thread_args.repo);
2083 s->thread_args.repo = NULL;
2086 if (s->thread_args.graph) {
2087 got_commit_graph_close(s->thread_args.graph);
2088 s->thread_args.graph = NULL;
2094 static const struct got_error *
2095 close_log_view(struct tog_view *view)
2097 const struct got_error *err = NULL;
2098 struct tog_log_view_state *s = &view->state.log;
2101 err = stop_log_thread(s);
2103 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2104 if (errcode && err == NULL)
2105 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2107 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2108 if (errcode && err == NULL)
2109 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2111 free_commits(&s->commits);
2112 free(s->in_repo_path);
2113 s->in_repo_path = NULL;
2116 free(s->head_ref_name);
2117 s->head_ref_name = NULL;
2118 got_ref_list_free(&s->refs);
2122 static const struct got_error *
2123 search_start_log_view(struct tog_view *view)
2125 struct tog_log_view_state *s = &view->state.log;
2127 s->matched_entry = NULL;
2128 s->search_entry = NULL;
2132 static const struct got_error *
2133 search_next_log_view(struct tog_view *view)
2135 const struct got_error *err = NULL;
2136 struct tog_log_view_state *s = &view->state.log;
2137 struct commit_queue_entry *entry;
2139 /* Display progress update in log view. */
2140 show_log_view(view);
2144 if (s->search_entry) {
2146 errcode = pthread_mutex_unlock(&tog_mutex);
2148 return got_error_set_errno(errcode,
2149 "pthread_mutex_unlock");
2150 ch = wgetch(view->window);
2151 errcode = pthread_mutex_lock(&tog_mutex);
2153 return got_error_set_errno(errcode,
2154 "pthread_mutex_lock");
2155 if (ch == KEY_BACKSPACE) {
2156 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2159 if (view->searching == TOG_SEARCH_FORWARD)
2160 entry = TAILQ_NEXT(s->search_entry, entry);
2162 entry = TAILQ_PREV(s->search_entry,
2163 commit_queue_head, entry);
2164 } else if (s->matched_entry) {
2165 if (view->searching == TOG_SEARCH_FORWARD)
2166 entry = TAILQ_NEXT(s->matched_entry, entry);
2168 entry = TAILQ_PREV(s->matched_entry,
2169 commit_queue_head, entry);
2171 if (view->searching == TOG_SEARCH_FORWARD)
2172 entry = TAILQ_FIRST(&s->commits.head);
2174 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2180 if (entry == NULL) {
2181 if (s->thread_args.log_complete ||
2182 view->searching == TOG_SEARCH_BACKWARD) {
2183 view->search_next_done =
2184 (s->matched_entry == NULL ?
2185 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2186 s->search_entry = NULL;
2190 * Poke the log thread for more commits and return,
2191 * allowing the main loop to make progress. Search
2192 * will resume at s->search_entry once we come back.
2194 s->thread_args.commits_needed++;
2195 return trigger_log_thread(view, 0);
2198 err = match_commit(&have_match, entry->id, entry->commit,
2203 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2204 s->matched_entry = entry;
2208 s->search_entry = entry;
2209 if (view->searching == TOG_SEARCH_FORWARD)
2210 entry = TAILQ_NEXT(entry, entry);
2212 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2215 if (s->matched_entry) {
2216 int cur = s->selected_entry->idx;
2217 while (cur < s->matched_entry->idx) {
2218 err = input_log_view(NULL, view, KEY_DOWN);
2223 while (cur > s->matched_entry->idx) {
2224 err = input_log_view(NULL, view, KEY_UP);
2231 s->search_entry = NULL;
2236 static const struct got_error *
2237 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2238 struct got_repository *repo, const char *head_ref_name,
2239 const char *in_repo_path, int log_branches)
2241 const struct got_error *err = NULL;
2242 struct tog_log_view_state *s = &view->state.log;
2243 struct got_repository *thread_repo = NULL;
2244 struct got_commit_graph *thread_graph = NULL;
2247 SIMPLEQ_INIT(&s->refs);
2249 if (in_repo_path != s->in_repo_path) {
2250 free(s->in_repo_path);
2251 s->in_repo_path = strdup(in_repo_path);
2252 if (s->in_repo_path == NULL)
2253 return got_error_from_errno("strdup");
2256 /* The commit queue only contains commits being displayed. */
2257 TAILQ_INIT(&s->commits.head);
2258 s->commits.ncommits = 0;
2260 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2265 if (head_ref_name) {
2266 s->head_ref_name = strdup(head_ref_name);
2267 if (s->head_ref_name == NULL) {
2268 err = got_error_from_errno("strdup");
2272 s->start_id = got_object_id_dup(start_id);
2273 if (s->start_id == NULL) {
2274 err = got_error_from_errno("got_object_id_dup");
2277 s->log_branches = log_branches;
2279 SIMPLEQ_INIT(&s->colors);
2280 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2281 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2282 get_color_value("TOG_COLOR_COMMIT"));
2285 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2286 get_color_value("TOG_COLOR_AUTHOR"));
2288 free_colors(&s->colors);
2291 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2292 get_color_value("TOG_COLOR_DATE"));
2294 free_colors(&s->colors);
2299 view->show = show_log_view;
2300 view->input = input_log_view;
2301 view->close = close_log_view;
2302 view->search_start = search_start_log_view;
2303 view->search_next = search_next_log_view;
2305 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2308 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2312 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2313 s->repo, NULL, NULL);
2317 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2319 err = got_error_set_errno(errcode, "pthread_cond_init");
2322 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2324 err = got_error_set_errno(errcode, "pthread_cond_init");
2328 s->thread_args.commits_needed = view->nlines;
2329 s->thread_args.graph = thread_graph;
2330 s->thread_args.commits = &s->commits;
2331 s->thread_args.in_repo_path = s->in_repo_path;
2332 s->thread_args.start_id = s->start_id;
2333 s->thread_args.repo = thread_repo;
2334 s->thread_args.log_complete = 0;
2335 s->thread_args.quit = &s->quit;
2336 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2337 s->thread_args.selected_entry = &s->selected_entry;
2338 s->thread_args.searching = &view->searching;
2339 s->thread_args.search_next_done = &view->search_next_done;
2340 s->thread_args.regex = &view->regex;
2343 close_log_view(view);
2347 static const struct got_error *
2348 show_log_view(struct tog_view *view)
2350 const struct got_error *err;
2351 struct tog_log_view_state *s = &view->state.log;
2353 if (s->thread == NULL) {
2354 int errcode = pthread_create(&s->thread, NULL, log_thread,
2357 return got_error_set_errno(errcode, "pthread_create");
2358 if (s->thread_args.commits_needed > 0) {
2359 err = trigger_log_thread(view, 1);
2365 return draw_commits(view);
2368 static const struct got_error *
2369 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2371 const struct got_error *err = NULL;
2372 struct tog_log_view_state *s = &view->state.log;
2373 struct tog_view *diff_view = NULL, *tree_view = NULL;
2374 struct tog_view *ref_view = NULL;
2385 if (s->first_displayed_entry == NULL)
2387 if (s->selected > 0)
2390 log_scroll_up(s, 1);
2395 if (s->first_displayed_entry == NULL)
2397 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2400 log_scroll_up(s, view->nlines - 1);
2407 if (s->first_displayed_entry == NULL)
2409 if (s->selected < MIN(view->nlines - 2,
2410 s->commits.ncommits - 1))
2413 err = log_scroll_down(view, 1);
2421 struct commit_queue_entry *first;
2422 first = s->first_displayed_entry;
2425 err = log_scroll_down(view, view->nlines - 1);
2428 if (first == s->first_displayed_entry &&
2429 s->selected < MIN(view->nlines - 2,
2430 s->commits.ncommits - 1)) {
2431 /* can't scroll further down */
2432 s->selected = MIN(view->nlines - 2,
2433 s->commits.ncommits - 1);
2439 if (s->selected > view->nlines - 2)
2440 s->selected = view->nlines - 2;
2441 if (s->selected > s->commits.ncommits - 1)
2442 s->selected = s->commits.ncommits - 1;
2444 if (s->commits.ncommits < view->nlines - 1 &&
2445 !s->thread_args.log_complete) {
2446 s->thread_args.commits_needed += (view->nlines - 1) -
2447 s->commits.ncommits;
2448 err = trigger_log_thread(view, 1);
2454 if (s->selected_entry == NULL)
2456 if (view_is_parent_view(view))
2457 begin_x = view_split_begin_x(view->begin_x);
2458 err = open_diff_view_for_commit(&diff_view, begin_x,
2459 s->selected_entry->commit, s->selected_entry->id,
2464 diff_view->focussed = 1;
2465 if (view_is_parent_view(view)) {
2466 err = view_close_child(view);
2469 view_set_child(view, diff_view);
2470 view->focus_child = 1;
2472 *new_view = diff_view;
2475 if (s->selected_entry == NULL)
2477 if (view_is_parent_view(view))
2478 begin_x = view_split_begin_x(view->begin_x);
2479 err = browse_commit_tree(&tree_view, begin_x,
2480 s->selected_entry, s->in_repo_path, s->head_ref_name,
2485 tree_view->focussed = 1;
2486 if (view_is_parent_view(view)) {
2487 err = view_close_child(view);
2490 view_set_child(view, tree_view);
2491 view->focus_child = 1;
2493 *new_view = tree_view;
2498 if (ch == KEY_BACKSPACE &&
2499 got_path_is_root_dir(s->in_repo_path))
2501 err = stop_log_thread(s);
2504 if (ch == KEY_BACKSPACE) {
2506 err = got_path_dirname(&parent_path, s->in_repo_path);
2509 free(s->in_repo_path);
2510 s->in_repo_path = parent_path;
2511 s->thread_args.in_repo_path = s->in_repo_path;
2512 } else if (ch == CTRL('l')) {
2513 struct got_object_id *start_id;
2514 err = got_repo_match_object_id(&start_id, NULL,
2515 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2516 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2520 s->start_id = start_id;
2521 s->thread_args.start_id = s->start_id;
2523 s->log_branches = !s->log_branches;
2525 err = got_repo_open(&s->thread_args.repo,
2526 got_repo_get_path(s->repo), NULL);
2529 got_ref_list_free(&s->refs);
2530 err = got_ref_list(&s->refs, s->repo, NULL,
2531 got_ref_cmp_by_name, NULL);
2534 err = got_commit_graph_open(&s->thread_args.graph,
2535 s->in_repo_path, !s->log_branches);
2538 err = got_commit_graph_iter_start(s->thread_args.graph,
2539 s->start_id, s->repo, NULL, NULL);
2542 free_commits(&s->commits);
2543 s->first_displayed_entry = NULL;
2544 s->last_displayed_entry = NULL;
2545 s->selected_entry = NULL;
2547 s->thread_args.log_complete = 0;
2549 s->thread_args.commits_needed = view->nlines;
2552 if (view_is_parent_view(view))
2553 begin_x = view_split_begin_x(view->begin_x);
2554 ref_view = view_open(view->nlines, view->ncols,
2555 view->begin_y, begin_x, TOG_VIEW_REF);
2556 if (ref_view == NULL)
2557 return got_error_from_errno("view_open");
2558 err = open_ref_view(ref_view, s->repo);
2560 view_close(ref_view);
2564 ref_view->focussed = 1;
2565 if (view_is_parent_view(view)) {
2566 err = view_close_child(view);
2569 view_set_child(view, ref_view);
2570 view->focus_child = 1;
2572 *new_view = ref_view;
2581 static const struct got_error *
2582 apply_unveil(const char *repo_path, const char *worktree_path)
2584 const struct got_error *error;
2587 if (unveil("gmon.out", "rwc") != 0)
2588 return got_error_from_errno2("unveil", "gmon.out");
2590 if (repo_path && unveil(repo_path, "r") != 0)
2591 return got_error_from_errno2("unveil", repo_path);
2593 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2594 return got_error_from_errno2("unveil", worktree_path);
2596 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2597 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2599 error = got_privsep_unveil_exec_helpers();
2603 if (unveil(NULL, NULL) != 0)
2604 return got_error_from_errno("unveil");
2614 halfdelay(1); /* Do fast refresh while initial view is loading. */
2617 intrflush(stdscr, FALSE);
2618 keypad(stdscr, TRUE);
2620 if (getenv("TOG_COLORS") != NULL) {
2622 use_default_colors();
2624 signal(SIGWINCH, tog_sigwinch);
2625 signal(SIGPIPE, tog_sigpipe);
2626 signal(SIGCONT, tog_sigcont);
2629 static const struct got_error *
2630 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2631 struct got_repository *repo, struct got_worktree *worktree)
2633 const struct got_error *err = NULL;
2636 *in_repo_path = strdup("/");
2637 if (*in_repo_path == NULL)
2638 return got_error_from_errno("strdup");
2643 const char *prefix = got_worktree_get_path_prefix(worktree);
2646 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2649 if (asprintf(in_repo_path, "%s%s%s", prefix,
2650 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2652 err = got_error_from_errno("asprintf");
2653 *in_repo_path = NULL;
2657 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2662 static const struct got_error *
2663 cmd_log(int argc, char *argv[])
2665 const struct got_error *error;
2666 struct got_repository *repo = NULL;
2667 struct got_worktree *worktree = NULL;
2668 struct got_object_id *start_id = NULL;
2669 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2670 char *start_commit = NULL, *label = NULL;
2671 struct got_reference *ref = NULL;
2672 const char *head_ref_name = NULL;
2673 int ch, log_branches = 0;
2674 struct tog_view *view;
2676 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2682 start_commit = optarg;
2685 repo_path = realpath(optarg, NULL);
2686 if (repo_path == NULL)
2687 return got_error_from_errno2("realpath",
2702 cwd = getcwd(NULL, 0);
2704 return got_error_from_errno("getcwd");
2706 error = got_worktree_open(&worktree, cwd);
2707 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2710 if (repo_path == NULL) {
2713 strdup(got_worktree_get_repo_path(worktree));
2715 repo_path = strdup(cwd);
2717 if (repo_path == NULL) {
2718 error = got_error_from_errno("strdup");
2722 error = got_repo_open(&repo, repo_path, NULL);
2726 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2733 error = apply_unveil(got_repo_get_path(repo),
2734 worktree ? got_worktree_get_root_path(worktree) : NULL);
2738 if (start_commit == NULL) {
2739 error = got_repo_match_object_id(&start_id, &label,
2740 worktree ? got_worktree_get_head_ref_name(worktree) :
2741 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
2744 head_ref_name = label;
2746 error = got_ref_open(&ref, repo, start_commit, 0);
2748 head_ref_name = got_ref_get_name(ref);
2749 else if (error->code != GOT_ERR_NOT_REF)
2751 error = got_repo_match_object_id(&start_id, NULL,
2752 start_commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2757 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2759 error = got_error_from_errno("view_open");
2762 error = open_log_view(view, start_id, repo, head_ref_name,
2763 in_repo_path, log_branches);
2767 /* Release work tree lock. */
2768 got_worktree_close(worktree);
2771 error = view_loop(view);
2781 got_repo_close(repo);
2783 got_worktree_close(worktree);
2791 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2792 "[-w] object1 object2\n", getprogname());
2797 match_line(const char *line, regex_t *regex, size_t nmatch,
2798 regmatch_t *regmatch)
2800 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2804 match_color(struct tog_colors *colors, const char *line)
2806 struct tog_color *tc = NULL;
2808 SIMPLEQ_FOREACH(tc, colors, entry) {
2809 if (match_line(line, &tc->regex, 0, NULL))
2816 static const struct got_error *
2817 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2818 WINDOW *window, regmatch_t *regmatch)
2820 const struct got_error *err = NULL;
2827 s = strndup(line, regmatch->rm_so);
2829 return got_error_from_errno("strndup");
2831 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2836 waddwstr(window, wline);
2843 s = strndup(line + regmatch->rm_so,
2844 regmatch->rm_eo - regmatch->rm_so);
2846 err = got_error_from_errno("strndup");
2850 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2855 wattr_on(window, A_STANDOUT, NULL);
2856 waddwstr(window, wline);
2857 wattr_off(window, A_STANDOUT, NULL);
2864 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2865 err = format_line(&wline, &width,
2866 line + regmatch->rm_eo, wlimit, col_tab_align);
2869 waddwstr(window, wline);
2877 static const struct got_error *
2878 draw_file(struct tog_view *view, const char *header)
2880 struct tog_diff_view_state *s = &view->state.diff;
2881 regmatch_t *regmatch = &view->regmatch;
2882 const struct got_error *err;
2885 size_t linesize = 0;
2887 struct tog_color *tc;
2890 int max_lines = view->nlines;
2891 int nlines = s->nlines;
2894 line_offset = s->line_offsets[s->first_displayed_line - 1];
2895 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2896 return got_error_from_errno("fseek");
2898 werase(view->window);
2901 if (asprintf(&line, "[%d/%d] %s",
2902 s->first_displayed_line - 1 + s->selected_line, nlines,
2904 return got_error_from_errno("asprintf");
2905 err = format_line(&wline, &width, line, view->ncols, 0);
2910 if (view_needs_focus_indication(view))
2911 wstandout(view->window);
2912 waddwstr(view->window, wline);
2915 if (view_needs_focus_indication(view))
2916 wstandend(view->window);
2917 if (width <= view->ncols - 1)
2918 waddch(view->window, '\n');
2927 while (max_lines > 0 && nprinted < max_lines) {
2928 linelen = getline(&line, &linesize, s->f);
2929 if (linelen == -1) {
2935 return got_ferror(s->f, GOT_ERR_IO);
2938 tc = match_color(&s->colors, line);
2940 wattr_on(view->window,
2941 COLOR_PAIR(tc->colorpair), NULL);
2942 if (s->first_displayed_line + nprinted == s->matched_line &&
2943 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2944 err = add_matched_line(&width, line, view->ncols, 0,
2945 view->window, regmatch);
2951 err = format_line(&wline, &width, line, view->ncols, 0);
2956 waddwstr(view->window, wline);
2961 wattr_off(view->window,
2962 COLOR_PAIR(tc->colorpair), NULL);
2963 if (width <= view->ncols - 1)
2964 waddch(view->window, '\n');
2969 s->last_displayed_line = s->first_displayed_line +
2972 s->last_displayed_line = s->first_displayed_line;
2977 while (nprinted < view->nlines) {
2978 waddch(view->window, '\n');
2982 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2987 wstandout(view->window);
2988 waddwstr(view->window, wline);
2991 wstandend(view->window);
2998 get_datestr(time_t *time, char *datebuf)
3000 struct tm mytm, *tm;
3003 tm = gmtime_r(time, &mytm);
3006 s = asctime_r(tm, datebuf);
3009 p = strchr(s, '\n');
3015 static const struct got_error *
3016 get_changed_paths(struct got_pathlist_head *paths,
3017 struct got_commit_object *commit, struct got_repository *repo)
3019 const struct got_error *err = NULL;
3020 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3021 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3022 struct got_object_qid *qid;
3024 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3026 struct got_commit_object *pcommit;
3027 err = got_object_open_as_commit(&pcommit, repo,
3032 tree_id1 = got_object_commit_get_tree_id(pcommit);
3033 got_object_commit_close(pcommit);
3038 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3043 tree_id2 = got_object_commit_get_tree_id(commit);
3044 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3048 err = got_diff_tree(tree1, tree2, "", "", repo,
3049 got_diff_tree_collect_changed_paths, paths, 0);
3052 got_object_tree_close(tree1);
3054 got_object_tree_close(tree2);
3058 static const struct got_error *
3059 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3063 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3065 return got_error_from_errno("reallocarray");
3067 (*line_offsets)[*nlines] = off;
3072 static const struct got_error *
3073 write_commit_info(off_t **line_offsets, size_t *nlines,
3074 struct got_object_id *commit_id, struct got_reflist_head *refs,
3075 struct got_repository *repo, FILE *outfile)
3077 const struct got_error *err = NULL;
3078 char datebuf[26], *datestr;
3079 struct got_commit_object *commit;
3080 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3081 time_t committer_time;
3082 const char *author, *committer;
3083 char *refs_str = NULL;
3084 struct got_pathlist_head changed_paths;
3085 struct got_pathlist_entry *pe;
3089 TAILQ_INIT(&changed_paths);
3092 err = build_refs_str(&refs_str, refs, commit_id, repo);
3097 err = got_object_open_as_commit(&commit, repo, commit_id);
3101 err = got_object_id_str(&id_str, commit_id);
3103 err = got_error_from_errno("got_object_id_str");
3107 err = add_line_offset(line_offsets, nlines, 0);
3111 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3112 refs_str ? refs_str : "", refs_str ? ")" : "");
3114 err = got_error_from_errno("fprintf");
3118 err = add_line_offset(line_offsets, nlines, outoff);
3122 n = fprintf(outfile, "from: %s\n",
3123 got_object_commit_get_author(commit));
3125 err = got_error_from_errno("fprintf");
3129 err = add_line_offset(line_offsets, nlines, outoff);
3133 committer_time = got_object_commit_get_committer_time(commit);
3134 datestr = get_datestr(&committer_time, datebuf);
3136 n = fprintf(outfile, "date: %s UTC\n", datestr);
3138 err = got_error_from_errno("fprintf");
3142 err = add_line_offset(line_offsets, nlines, outoff);
3146 author = got_object_commit_get_author(commit);
3147 committer = got_object_commit_get_committer(commit);
3148 if (strcmp(author, committer) != 0) {
3149 n = fprintf(outfile, "via: %s\n", committer);
3151 err = got_error_from_errno("fprintf");
3155 err = add_line_offset(line_offsets, nlines, outoff);
3159 err = got_object_commit_get_logmsg(&logmsg, commit);
3163 while ((line = strsep(&s, "\n")) != NULL) {
3164 n = fprintf(outfile, "%s\n", line);
3166 err = got_error_from_errno("fprintf");
3170 err = add_line_offset(line_offsets, nlines, outoff);
3175 err = get_changed_paths(&changed_paths, commit, repo);
3178 TAILQ_FOREACH(pe, &changed_paths, entry) {
3179 struct got_diff_changed_path *cp = pe->data;
3180 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3182 err = got_error_from_errno("fprintf");
3186 err = add_line_offset(line_offsets, nlines, outoff);
3189 free((char *)pe->path);
3193 fputc('\n', outfile);
3195 err = add_line_offset(line_offsets, nlines, outoff);
3197 got_pathlist_free(&changed_paths);
3201 got_object_commit_close(commit);
3203 free(*line_offsets);
3204 *line_offsets = NULL;
3210 static const struct got_error *
3211 create_diff(struct tog_diff_view_state *s)
3213 const struct got_error *err = NULL;
3217 free(s->line_offsets);
3218 s->line_offsets = malloc(sizeof(off_t));
3219 if (s->line_offsets == NULL)
3220 return got_error_from_errno("malloc");
3225 err = got_error_from_errno("got_opentemp");
3228 if (s->f && fclose(s->f) != 0) {
3229 err = got_error_from_errno("fclose");
3235 err = got_object_get_type(&obj_type, s->repo, s->id1);
3237 err = got_object_get_type(&obj_type, s->repo, s->id2);
3242 case GOT_OBJ_TYPE_BLOB:
3243 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3244 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3245 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3247 case GOT_OBJ_TYPE_TREE:
3248 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3249 s->id1, s->id2, "", "", s->diff_context,
3250 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3252 case GOT_OBJ_TYPE_COMMIT: {
3253 const struct got_object_id_queue *parent_ids;
3254 struct got_object_qid *pid;
3255 struct got_commit_object *commit2;
3257 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3260 /* Show commit info if we're diffing to a parent/root commit. */
3261 if (s->id1 == NULL) {
3262 err = write_commit_info(&s->line_offsets, &s->nlines,
3263 s->id2, &s->refs, s->repo, s->f);
3267 parent_ids = got_object_commit_get_parent_ids(commit2);
3268 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3269 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3270 err = write_commit_info(
3271 &s->line_offsets, &s->nlines,
3272 s->id2, &s->refs, s->repo, s->f);
3279 got_object_commit_close(commit2);
3281 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3282 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3283 s->force_text_diff, s->repo, s->f);
3287 err = got_error(GOT_ERR_OBJ_TYPE);
3293 if (s->f && fflush(s->f) != 0 && err == NULL)
3294 err = got_error_from_errno("fflush");
3299 diff_view_indicate_progress(struct tog_view *view)
3301 mvwaddstr(view->window, 0, 0, "diffing...");
3306 static const struct got_error *
3307 search_start_diff_view(struct tog_view *view)
3309 struct tog_diff_view_state *s = &view->state.diff;
3311 s->matched_line = 0;
3315 static const struct got_error *
3316 search_next_diff_view(struct tog_view *view)
3318 struct tog_diff_view_state *s = &view->state.diff;
3321 size_t linesize = 0;
3324 if (!view->searching) {
3325 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3329 if (s->matched_line) {
3330 if (view->searching == TOG_SEARCH_FORWARD)
3331 lineno = s->matched_line + 1;
3333 lineno = s->matched_line - 1;
3335 if (view->searching == TOG_SEARCH_FORWARD)
3344 if (lineno <= 0 || lineno > s->nlines) {
3345 if (s->matched_line == 0) {
3346 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3350 if (view->searching == TOG_SEARCH_FORWARD)
3356 offset = s->line_offsets[lineno - 1];
3357 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3359 return got_error_from_errno("fseeko");
3361 linelen = getline(&line, &linesize, s->f);
3362 if (linelen != -1 &&
3363 match_line(line, &view->regex, 1, &view->regmatch)) {
3364 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3365 s->matched_line = lineno;
3368 if (view->searching == TOG_SEARCH_FORWARD)
3375 if (s->matched_line) {
3376 s->first_displayed_line = s->matched_line;
3377 s->selected_line = 1;
3383 static const struct got_error *
3384 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3385 struct got_object_id *id2, const char *label1, const char *label2,
3386 int diff_context, int ignore_whitespace, int force_text_diff,
3387 struct tog_view *log_view, struct got_repository *repo)
3389 const struct got_error *err;
3390 struct tog_diff_view_state *s = &view->state.diff;
3392 SIMPLEQ_INIT(&s->refs);
3394 if (id1 != NULL && id2 != NULL) {
3396 err = got_object_get_type(&type1, repo, id1);
3399 err = got_object_get_type(&type2, repo, id2);
3404 return got_error(GOT_ERR_OBJ_TYPE);
3406 s->first_displayed_line = 1;
3407 s->last_displayed_line = view->nlines;
3408 s->selected_line = 1;
3416 s->id1 = got_object_id_dup(id1);
3418 return got_error_from_errno("got_object_id_dup");
3422 s->id2 = got_object_id_dup(id2);
3423 if (s->id2 == NULL) {
3426 return got_error_from_errno("got_object_id_dup");
3429 s->first_displayed_line = 1;
3430 s->last_displayed_line = view->nlines;
3431 s->diff_context = diff_context;
3432 s->ignore_whitespace = ignore_whitespace;
3433 s->force_text_diff = force_text_diff;
3434 s->log_view = log_view;
3437 SIMPLEQ_INIT(&s->colors);
3438 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3439 err = add_color(&s->colors,
3440 "^-", TOG_COLOR_DIFF_MINUS,
3441 get_color_value("TOG_COLOR_DIFF_MINUS"));
3444 err = add_color(&s->colors, "^\\+",
3445 TOG_COLOR_DIFF_PLUS,
3446 get_color_value("TOG_COLOR_DIFF_PLUS"));
3448 free_colors(&s->colors);
3451 err = add_color(&s->colors,
3452 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3453 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3455 free_colors(&s->colors);
3459 err = add_color(&s->colors,
3460 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3461 TOG_COLOR_DIFF_META,
3462 get_color_value("TOG_COLOR_DIFF_META"));
3464 free_colors(&s->colors);
3468 err = add_color(&s->colors,
3469 "^(from|via): ", TOG_COLOR_AUTHOR,
3470 get_color_value("TOG_COLOR_AUTHOR"));
3472 free_colors(&s->colors);
3476 err = add_color(&s->colors,
3477 "^date: ", TOG_COLOR_DATE,
3478 get_color_value("TOG_COLOR_DATE"));
3480 free_colors(&s->colors);
3485 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3491 free_colors(&s->colors);
3495 if (log_view && view_is_splitscreen(view))
3496 show_log_view(log_view); /* draw vborder */
3497 diff_view_indicate_progress(view);
3499 s->line_offsets = NULL;
3501 err = create_diff(s);
3507 free_colors(&s->colors);
3508 got_ref_list_free(&s->refs);
3512 view->show = show_diff_view;
3513 view->input = input_diff_view;
3514 view->close = close_diff_view;
3515 view->search_start = search_start_diff_view;
3516 view->search_next = search_next_diff_view;
3521 static const struct got_error *
3522 close_diff_view(struct tog_view *view)
3524 const struct got_error *err = NULL;
3525 struct tog_diff_view_state *s = &view->state.diff;
3531 if (s->f && fclose(s->f) == EOF)
3532 err = got_error_from_errno("fclose");
3533 free_colors(&s->colors);
3534 free(s->line_offsets);
3535 s->line_offsets = NULL;
3537 got_ref_list_free(&s->refs);
3541 static const struct got_error *
3542 show_diff_view(struct tog_view *view)
3544 const struct got_error *err;
3545 struct tog_diff_view_state *s = &view->state.diff;
3546 char *id_str1 = NULL, *id_str2, *header;
3547 const char *label1, *label2;
3550 err = got_object_id_str(&id_str1, s->id1);
3553 label1 = s->label1 ? : id_str1;
3555 label1 = "/dev/null";
3557 err = got_object_id_str(&id_str2, s->id2);
3560 label2 = s->label2 ? : id_str2;
3562 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3563 err = got_error_from_errno("asprintf");
3571 return draw_file(view, header);
3574 static const struct got_error *
3575 set_selected_commit(struct tog_diff_view_state *s,
3576 struct commit_queue_entry *entry)
3578 const struct got_error *err;
3579 const struct got_object_id_queue *parent_ids;
3580 struct got_commit_object *selected_commit;
3581 struct got_object_qid *pid;
3584 s->id2 = got_object_id_dup(entry->id);
3586 return got_error_from_errno("got_object_id_dup");
3588 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3591 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3593 pid = SIMPLEQ_FIRST(parent_ids);
3594 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3595 got_object_commit_close(selected_commit);
3599 static const struct got_error *
3600 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3602 const struct got_error *err = NULL;
3603 struct tog_diff_view_state *s = &view->state.diff;
3604 struct tog_log_view_state *ls;
3605 struct commit_queue_entry *old_selected_entry;
3607 size_t linesize = 0;
3615 s->force_text_diff = !s->force_text_diff;
3617 s->ignore_whitespace = !s->ignore_whitespace;
3618 wclear(view->window);
3619 s->first_displayed_line = 1;
3620 s->last_displayed_line = view->nlines;
3621 diff_view_indicate_progress(view);
3622 err = create_diff(s);
3626 if (s->first_displayed_line > 1)
3627 s->first_displayed_line--;
3631 if (s->first_displayed_line == 1)
3634 while (i++ < view->nlines - 1 &&
3635 s->first_displayed_line > 1)
3636 s->first_displayed_line--;
3641 s->first_displayed_line++;
3649 while (!s->eof && i++ < view->nlines - 1) {
3650 linelen = getline(&line, &linesize, s->f);
3651 s->first_displayed_line++;
3652 if (linelen == -1) {
3656 err = got_ferror(s->f, GOT_ERR_IO);
3663 if (s->diff_context > 0) {
3665 diff_view_indicate_progress(view);
3666 err = create_diff(s);
3667 if (s->first_displayed_line + view->nlines - 1 >
3669 s->first_displayed_line = 1;
3670 s->last_displayed_line = view->nlines;
3675 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3677 diff_view_indicate_progress(view);
3678 err = create_diff(s);
3683 if (s->log_view == NULL)
3685 ls = &s->log_view->state.log;
3686 old_selected_entry = ls->selected_entry;
3688 err = input_log_view(NULL, s->log_view, KEY_UP);
3692 if (old_selected_entry == ls->selected_entry)
3695 err = set_selected_commit(s, ls->selected_entry);
3699 s->first_displayed_line = 1;
3700 s->last_displayed_line = view->nlines;
3702 diff_view_indicate_progress(view);
3703 err = create_diff(s);
3707 if (s->log_view == NULL)
3709 ls = &s->log_view->state.log;
3710 old_selected_entry = ls->selected_entry;
3712 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3716 if (old_selected_entry == ls->selected_entry)
3719 err = set_selected_commit(s, ls->selected_entry);
3723 s->first_displayed_line = 1;
3724 s->last_displayed_line = view->nlines;
3726 diff_view_indicate_progress(view);
3727 err = create_diff(s);
3736 static const struct got_error *
3737 cmd_diff(int argc, char *argv[])
3739 const struct got_error *error = NULL;
3740 struct got_repository *repo = NULL;
3741 struct got_worktree *worktree = NULL;
3742 struct got_object_id *id1 = NULL, *id2 = NULL;
3743 char *repo_path = NULL, *cwd = NULL;
3744 char *id_str1 = NULL, *id_str2 = NULL;
3745 char *label1 = NULL, *label2 = NULL;
3746 int diff_context = 3, ignore_whitespace = 0;
3747 int ch, force_text_diff = 0;
3749 struct tog_view *view;
3751 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3754 force_text_diff = 1;
3757 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3760 err(1, "-C option %s", errstr);
3763 repo_path = realpath(optarg, NULL);
3764 if (repo_path == NULL)
3765 return got_error_from_errno2("realpath",
3767 got_path_strip_trailing_slashes(repo_path);
3770 ignore_whitespace = 1;
3782 usage_diff(); /* TODO show local worktree changes */
3783 } else if (argc == 2) {
3789 cwd = getcwd(NULL, 0);
3791 return got_error_from_errno("getcwd");
3793 error = got_worktree_open(&worktree, cwd);
3794 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3797 if (repo_path == NULL) {
3800 strdup(got_worktree_get_repo_path(worktree));
3802 repo_path = strdup(cwd);
3804 if (repo_path == NULL) {
3805 error = got_error_from_errno("strdup");
3809 error = got_repo_open(&repo, repo_path, NULL);
3815 error = apply_unveil(got_repo_get_path(repo), NULL);
3819 error = got_repo_match_object_id(&id1, &label1, id_str1,
3820 GOT_OBJ_TYPE_ANY, 1, repo);
3824 error = got_repo_match_object_id(&id2, &label2, id_str2,
3825 GOT_OBJ_TYPE_ANY, 1, repo);
3829 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3831 error = got_error_from_errno("view_open");
3834 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3835 ignore_whitespace, force_text_diff, NULL, repo);
3838 error = view_loop(view);
3845 got_repo_close(repo);
3847 got_worktree_close(worktree);
3855 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3860 struct tog_blame_line {
3862 struct got_object_id *id;
3865 static const struct got_error *
3866 draw_blame(struct tog_view *view)
3868 struct tog_blame_view_state *s = &view->state.blame;
3869 struct tog_blame *blame = &s->blame;
3870 regmatch_t *regmatch = &view->regmatch;
3871 const struct got_error *err;
3872 int lineno = 0, nprinted = 0;
3874 size_t linesize = 0;
3878 struct tog_blame_line *blame_line;
3879 struct got_object_id *prev_id = NULL;
3881 struct tog_color *tc;
3883 err = got_object_id_str(&id_str, s->blamed_commit->id);
3888 werase(view->window);
3890 if (asprintf(&line, "commit %s", id_str) == -1) {
3891 err = got_error_from_errno("asprintf");
3896 err = format_line(&wline, &width, line, view->ncols, 0);
3901 if (view_needs_focus_indication(view))
3902 wstandout(view->window);
3903 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3905 wattr_on(view->window,
3906 COLOR_PAIR(tc->colorpair), NULL);
3907 waddwstr(view->window, wline);
3909 wattr_off(view->window,
3910 COLOR_PAIR(tc->colorpair), NULL);
3911 if (view_needs_focus_indication(view))
3912 wstandend(view->window);
3915 if (width < view->ncols - 1)
3916 waddch(view->window, '\n');
3918 if (asprintf(&line, "[%d/%d] %s%s",
3919 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3920 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3922 return got_error_from_errno("asprintf");
3925 err = format_line(&wline, &width, line, view->ncols, 0);
3930 waddwstr(view->window, wline);
3933 if (width < view->ncols - 1)
3934 waddch(view->window, '\n');
3937 while (nprinted < view->nlines - 2) {
3938 linelen = getline(&line, &linesize, blame->f);
3939 if (linelen == -1) {
3940 if (feof(blame->f)) {
3945 return got_ferror(blame->f, GOT_ERR_IO);
3947 if (++lineno < s->first_displayed_line)
3950 if (view->focussed && nprinted == s->selected_line - 1)
3951 wstandout(view->window);
3953 if (blame->nlines > 0) {
3954 blame_line = &blame->lines[lineno - 1];
3955 if (blame_line->annotated && prev_id &&
3956 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3958 nprinted == s->selected_line - 1)) {
3959 waddstr(view->window, " ");
3960 } else if (blame_line->annotated) {
3962 err = got_object_id_str(&id_str, blame_line->id);
3967 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3969 wattr_on(view->window,
3970 COLOR_PAIR(tc->colorpair), NULL);
3971 wprintw(view->window, "%.8s", id_str);
3973 wattr_off(view->window,
3974 COLOR_PAIR(tc->colorpair), NULL);
3976 prev_id = blame_line->id;
3978 waddstr(view->window, "........");
3982 waddstr(view->window, "........");
3986 if (view->focussed && nprinted == s->selected_line - 1)
3987 wstandend(view->window);
3988 waddstr(view->window, " ");
3990 if (view->ncols <= 9) {
3992 wline = wcsdup(L"");
3993 if (wline == NULL) {
3994 err = got_error_from_errno("wcsdup");
3998 } else if (s->first_displayed_line + nprinted ==
4000 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4001 err = add_matched_line(&width, line, view->ncols - 9, 9,
4002 view->window, regmatch);
4009 err = format_line(&wline, &width, line,
4010 view->ncols - 9, 9);
4011 waddwstr(view->window, wline);
4017 if (width <= view->ncols - 1)
4018 waddch(view->window, '\n');
4019 if (++nprinted == 1)
4020 s->first_displayed_line = lineno;
4023 s->last_displayed_line = lineno;
4030 static const struct got_error *
4031 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4033 const struct got_error *err = NULL;
4034 struct tog_blame_cb_args *a = arg;
4035 struct tog_blame_line *line;
4038 if (nlines != a->nlines ||
4039 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4040 return got_error(GOT_ERR_RANGE);
4042 errcode = pthread_mutex_lock(&tog_mutex);
4044 return got_error_set_errno(errcode, "pthread_mutex_lock");
4046 if (*a->quit) { /* user has quit the blame view */
4047 err = got_error(GOT_ERR_ITER_COMPLETED);
4052 goto done; /* no change in this commit */
4054 line = &a->lines[lineno - 1];
4055 if (line->annotated)
4058 line->id = got_object_id_dup(id);
4059 if (line->id == NULL) {
4060 err = got_error_from_errno("got_object_id_dup");
4063 line->annotated = 1;
4065 errcode = pthread_mutex_unlock(&tog_mutex);
4067 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4072 blame_thread(void *arg)
4074 const struct got_error *err;
4075 struct tog_blame_thread_args *ta = arg;
4076 struct tog_blame_cb_args *a = ta->cb_args;
4079 err = block_signals_used_by_main_thread();
4083 err = got_blame(ta->path, a->commit_id, ta->repo,
4084 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4085 if (err && err->code == GOT_ERR_CANCELLED)
4088 errcode = pthread_mutex_lock(&tog_mutex);
4090 return (void *)got_error_set_errno(errcode,
4091 "pthread_mutex_lock");
4093 got_repo_close(ta->repo);
4097 errcode = pthread_mutex_unlock(&tog_mutex);
4098 if (errcode && err == NULL)
4099 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4104 static struct got_object_id *
4105 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4106 int first_displayed_line, int selected_line)
4108 struct tog_blame_line *line;
4113 line = &lines[first_displayed_line - 1 + selected_line - 1];
4114 if (!line->annotated)
4120 static const struct got_error *
4121 stop_blame(struct tog_blame *blame)
4123 const struct got_error *err = NULL;
4126 if (blame->thread) {
4128 errcode = pthread_mutex_unlock(&tog_mutex);
4130 return got_error_set_errno(errcode,
4131 "pthread_mutex_unlock");
4132 errcode = pthread_join(blame->thread, (void **)&err);
4134 return got_error_set_errno(errcode, "pthread_join");
4135 errcode = pthread_mutex_lock(&tog_mutex);
4137 return got_error_set_errno(errcode,
4138 "pthread_mutex_lock");
4139 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4141 blame->thread = NULL;
4143 if (blame->thread_args.repo) {
4144 got_repo_close(blame->thread_args.repo);
4145 blame->thread_args.repo = NULL;
4148 if (fclose(blame->f) != 0 && err == NULL)
4149 err = got_error_from_errno("fclose");
4153 for (i = 0; i < blame->nlines; i++)
4154 free(blame->lines[i].id);
4156 blame->lines = NULL;
4158 free(blame->cb_args.commit_id);
4159 blame->cb_args.commit_id = NULL;
4164 static const struct got_error *
4165 cancel_blame_view(void *arg)
4167 const struct got_error *err = NULL;
4171 errcode = pthread_mutex_lock(&tog_mutex);
4173 return got_error_set_errno(errcode,
4174 "pthread_mutex_unlock");
4177 err = got_error(GOT_ERR_CANCELLED);
4179 errcode = pthread_mutex_unlock(&tog_mutex);
4181 return got_error_set_errno(errcode,
4182 "pthread_mutex_lock");
4187 static const struct got_error *
4188 run_blame(struct tog_view *view)
4190 struct tog_blame_view_state *s = &view->state.blame;
4191 struct tog_blame *blame = &s->blame;
4192 const struct got_error *err = NULL;
4193 struct got_blob_object *blob = NULL;
4194 struct got_repository *thread_repo = NULL;
4195 struct got_object_id *obj_id = NULL;
4198 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4203 err = got_object_get_type(&obj_type, s->repo, obj_id);
4207 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4208 err = got_error(GOT_ERR_OBJ_TYPE);
4212 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4215 blame->f = got_opentemp();
4216 if (blame->f == NULL) {
4217 err = got_error_from_errno("got_opentemp");
4220 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4221 &blame->line_offsets, blame->f, blob);
4222 if (err || blame->nlines == 0)
4225 /* Don't include \n at EOF in the blame line count. */
4226 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4229 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4230 if (blame->lines == NULL) {
4231 err = got_error_from_errno("calloc");
4235 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4239 blame->cb_args.view = view;
4240 blame->cb_args.lines = blame->lines;
4241 blame->cb_args.nlines = blame->nlines;
4242 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4243 if (blame->cb_args.commit_id == NULL) {
4244 err = got_error_from_errno("got_object_id_dup");
4247 blame->cb_args.quit = &s->done;
4249 blame->thread_args.path = s->path;
4250 blame->thread_args.repo = thread_repo;
4251 blame->thread_args.cb_args = &blame->cb_args;
4252 blame->thread_args.complete = &s->blame_complete;
4253 blame->thread_args.cancel_cb = cancel_blame_view;
4254 blame->thread_args.cancel_arg = &s->done;
4255 s->blame_complete = 0;
4259 got_object_blob_close(blob);
4266 static const struct got_error *
4267 open_blame_view(struct tog_view *view, char *path,
4268 struct got_object_id *commit_id, struct got_repository *repo)
4270 const struct got_error *err = NULL;
4271 struct tog_blame_view_state *s = &view->state.blame;
4273 SIMPLEQ_INIT(&s->blamed_commits);
4275 s->path = strdup(path);
4276 if (s->path == NULL)
4277 return got_error_from_errno("strdup");
4279 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4285 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4286 s->first_displayed_line = 1;
4287 s->last_displayed_line = view->nlines;
4288 s->selected_line = 1;
4289 s->blame_complete = 0;
4291 s->commit_id = commit_id;
4292 memset(&s->blame, 0, sizeof(s->blame));
4294 SIMPLEQ_INIT(&s->colors);
4295 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4296 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4297 get_color_value("TOG_COLOR_COMMIT"));
4302 view->show = show_blame_view;
4303 view->input = input_blame_view;
4304 view->close = close_blame_view;
4305 view->search_start = search_start_blame_view;
4306 view->search_next = search_next_blame_view;
4308 return run_blame(view);
4311 static const struct got_error *
4312 close_blame_view(struct tog_view *view)
4314 const struct got_error *err = NULL;
4315 struct tog_blame_view_state *s = &view->state.blame;
4317 if (s->blame.thread)
4318 err = stop_blame(&s->blame);
4320 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4321 struct got_object_qid *blamed_commit;
4322 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4323 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4324 got_object_qid_free(blamed_commit);
4328 free_colors(&s->colors);
4333 static const struct got_error *
4334 search_start_blame_view(struct tog_view *view)
4336 struct tog_blame_view_state *s = &view->state.blame;
4338 s->matched_line = 0;
4342 static const struct got_error *
4343 search_next_blame_view(struct tog_view *view)
4345 struct tog_blame_view_state *s = &view->state.blame;
4348 size_t linesize = 0;
4351 if (!view->searching) {
4352 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4356 if (s->matched_line) {
4357 if (view->searching == TOG_SEARCH_FORWARD)
4358 lineno = s->matched_line + 1;
4360 lineno = s->matched_line - 1;
4362 if (view->searching == TOG_SEARCH_FORWARD)
4365 lineno = s->blame.nlines;
4371 if (lineno <= 0 || lineno > s->blame.nlines) {
4372 if (s->matched_line == 0) {
4373 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4377 if (view->searching == TOG_SEARCH_FORWARD)
4380 lineno = s->blame.nlines;
4383 offset = s->blame.line_offsets[lineno - 1];
4384 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4386 return got_error_from_errno("fseeko");
4388 linelen = getline(&line, &linesize, s->blame.f);
4389 if (linelen != -1 &&
4390 match_line(line, &view->regex, 1, &view->regmatch)) {
4391 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4392 s->matched_line = lineno;
4395 if (view->searching == TOG_SEARCH_FORWARD)
4402 if (s->matched_line) {
4403 s->first_displayed_line = s->matched_line;
4404 s->selected_line = 1;
4410 static const struct got_error *
4411 show_blame_view(struct tog_view *view)
4413 const struct got_error *err = NULL;
4414 struct tog_blame_view_state *s = &view->state.blame;
4417 if (s->blame.thread == NULL) {
4418 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4419 &s->blame.thread_args);
4421 return got_error_set_errno(errcode, "pthread_create");
4423 halfdelay(1); /* fast refresh while annotating */
4426 if (s->blame_complete)
4427 halfdelay(10); /* disable fast refresh */
4429 err = draw_blame(view);
4435 static const struct got_error *
4436 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4438 const struct got_error *err = NULL, *thread_err = NULL;
4439 struct tog_view *diff_view;
4440 struct tog_blame_view_state *s = &view->state.blame;
4449 if (s->selected_line > 1)
4451 else if (s->selected_line == 1 &&
4452 s->first_displayed_line > 1)
4453 s->first_displayed_line--;
4457 if (s->first_displayed_line == 1) {
4458 s->selected_line = 1;
4461 if (s->first_displayed_line > view->nlines - 2)
4462 s->first_displayed_line -=
4465 s->first_displayed_line = 1;
4469 if (s->selected_line < view->nlines - 2 &&
4470 s->first_displayed_line +
4471 s->selected_line <= s->blame.nlines)
4473 else if (s->last_displayed_line <
4475 s->first_displayed_line++;
4479 struct got_object_id *id = NULL;
4480 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4481 s->first_displayed_line, s->selected_line);
4485 struct got_commit_object *commit;
4486 struct got_object_qid *pid;
4487 struct got_object_id *blob_id = NULL;
4489 err = got_object_open_as_commit(&commit,
4493 pid = SIMPLEQ_FIRST(
4494 got_object_commit_get_parent_ids(commit));
4496 got_object_commit_close(commit);
4499 /* Check if path history ends here. */
4500 err = got_object_id_by_path(&blob_id, s->repo,
4503 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4505 got_object_commit_close(commit);
4508 err = got_object_get_type(&obj_type, s->repo,
4511 /* Can't blame non-blob type objects. */
4512 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4513 got_object_commit_close(commit);
4516 err = got_object_qid_alloc(&s->blamed_commit,
4518 got_object_commit_close(commit);
4520 if (got_object_id_cmp(id,
4521 s->blamed_commit->id) == 0)
4523 err = got_object_qid_alloc(&s->blamed_commit,
4529 thread_err = stop_blame(&s->blame);
4533 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4534 s->blamed_commit, entry);
4535 err = run_blame(view);
4541 struct got_object_qid *first;
4542 first = SIMPLEQ_FIRST(&s->blamed_commits);
4543 if (!got_object_id_cmp(first->id, s->commit_id))
4546 thread_err = stop_blame(&s->blame);
4550 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4551 got_object_qid_free(s->blamed_commit);
4553 SIMPLEQ_FIRST(&s->blamed_commits);
4554 err = run_blame(view);
4561 struct got_object_id *id = NULL;
4562 struct got_object_qid *pid;
4563 struct got_commit_object *commit = NULL;
4564 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4565 s->first_displayed_line, s->selected_line);
4568 err = got_object_open_as_commit(&commit, s->repo, id);
4571 pid = SIMPLEQ_FIRST(
4572 got_object_commit_get_parent_ids(commit));
4573 if (view_is_parent_view(view))
4574 begin_x = view_split_begin_x(view->begin_x);
4575 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4576 if (diff_view == NULL) {
4577 got_object_commit_close(commit);
4578 err = got_error_from_errno("view_open");
4581 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4582 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4583 got_object_commit_close(commit);
4585 view_close(diff_view);
4589 diff_view->focussed = 1;
4590 if (view_is_parent_view(view)) {
4591 err = view_close_child(view);
4594 view_set_child(view, diff_view);
4595 view->focus_child = 1;
4597 *new_view = diff_view;
4605 if (s->last_displayed_line >= s->blame.nlines &&
4606 s->selected_line >= MIN(s->blame.nlines,
4607 view->nlines - 2)) {
4610 if (s->last_displayed_line >= s->blame.nlines &&
4611 s->selected_line < view->nlines - 2) {
4612 s->selected_line = MIN(s->blame.nlines,
4616 if (s->last_displayed_line + view->nlines - 2
4618 s->first_displayed_line +=
4621 s->first_displayed_line =
4626 if (s->selected_line > view->nlines - 2) {
4627 s->selected_line = MIN(s->blame.nlines,
4634 return thread_err ? thread_err : err;
4637 static const struct got_error *
4638 cmd_blame(int argc, char *argv[])
4640 const struct got_error *error;
4641 struct got_repository *repo = NULL;
4642 struct got_worktree *worktree = NULL;
4643 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4644 char *link_target = NULL;
4645 struct got_object_id *commit_id = NULL;
4646 char *commit_id_str = NULL;
4648 struct tog_view *view;
4650 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4653 commit_id_str = optarg;
4656 repo_path = realpath(optarg, NULL);
4657 if (repo_path == NULL)
4658 return got_error_from_errno2("realpath",
4673 cwd = getcwd(NULL, 0);
4675 return got_error_from_errno("getcwd");
4677 error = got_worktree_open(&worktree, cwd);
4678 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4681 if (repo_path == NULL) {
4684 strdup(got_worktree_get_repo_path(worktree));
4686 repo_path = strdup(cwd);
4688 if (repo_path == NULL) {
4689 error = got_error_from_errno("strdup");
4693 error = got_repo_open(&repo, repo_path, NULL);
4697 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4704 error = apply_unveil(got_repo_get_path(repo), NULL);
4708 if (commit_id_str == NULL) {
4709 struct got_reference *head_ref;
4710 error = got_ref_open(&head_ref, repo, worktree ?
4711 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4714 error = got_ref_resolve(&commit_id, repo, head_ref);
4715 got_ref_close(head_ref);
4717 error = got_repo_match_object_id(&commit_id, NULL,
4718 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4723 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4725 error = got_error_from_errno("view_open");
4729 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4734 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4739 /* Release work tree lock. */
4740 got_worktree_close(worktree);
4743 error = view_loop(view);
4751 got_worktree_close(worktree);
4753 got_repo_close(repo);
4757 static const struct got_error *
4758 draw_tree_entries(struct tog_view *view, const char *parent_path)
4760 struct tog_tree_view_state *s = &view->state.tree;
4761 const struct got_error *err = NULL;
4762 struct got_tree_entry *te;
4764 struct tog_color *tc;
4765 int width, n, i, nentries;
4766 int limit = view->nlines;
4770 werase(view->window);
4775 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4778 if (view_needs_focus_indication(view))
4779 wstandout(view->window);
4780 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4782 wattr_on(view->window,
4783 COLOR_PAIR(tc->colorpair), NULL);
4784 waddwstr(view->window, wline);
4786 wattr_off(view->window,
4787 COLOR_PAIR(tc->colorpair), NULL);
4788 if (view_needs_focus_indication(view))
4789 wstandend(view->window);
4792 if (width < view->ncols - 1)
4793 waddch(view->window, '\n');
4796 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4799 waddwstr(view->window, wline);
4802 if (width < view->ncols - 1)
4803 waddch(view->window, '\n');
4806 waddch(view->window, '\n');
4810 if (s->first_displayed_entry == NULL) {
4811 te = got_object_tree_get_first_entry(s->tree);
4812 if (s->selected == 0) {
4814 wstandout(view->window);
4815 s->selected_entry = NULL;
4817 waddstr(view->window, " ..\n"); /* parent directory */
4818 if (s->selected == 0 && view->focussed)
4819 wstandend(view->window);
4826 te = s->first_displayed_entry;
4829 nentries = got_object_tree_get_nentries(s->tree);
4830 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4831 char *line = NULL, *id_str = NULL, *link_target = NULL;
4832 const char *modestr = "";
4835 te = got_object_tree_get_entry(s->tree, i);
4836 mode = got_tree_entry_get_mode(te);
4839 err = got_object_id_str(&id_str,
4840 got_tree_entry_get_id(te));
4842 return got_error_from_errno(
4843 "got_object_id_str");
4845 if (got_object_tree_entry_is_submodule(te))
4847 else if (S_ISLNK(mode)) {
4850 err = got_tree_entry_get_symlink_target(&link_target,
4856 for (i = 0; i < strlen(link_target); i++) {
4857 if (!isprint((unsigned char)link_target[i]))
4858 link_target[i] = '?';
4862 else if (S_ISDIR(mode))
4864 else if (mode & S_IXUSR)
4866 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4867 got_tree_entry_get_name(te), modestr,
4868 link_target ? " -> ": "",
4869 link_target ? link_target : "") == -1) {
4872 return got_error_from_errno("asprintf");
4876 err = format_line(&wline, &width, line, view->ncols, 0);
4881 if (n == s->selected) {
4883 wstandout(view->window);
4884 s->selected_entry = te;
4886 tc = match_color(&s->colors, line);
4888 wattr_on(view->window,
4889 COLOR_PAIR(tc->colorpair), NULL);
4890 waddwstr(view->window, wline);
4892 wattr_off(view->window,
4893 COLOR_PAIR(tc->colorpair), NULL);
4894 if (width < view->ncols - 1)
4895 waddch(view->window, '\n');
4896 if (n == s->selected && view->focussed)
4897 wstandend(view->window);
4903 s->last_displayed_entry = te;
4912 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4914 struct got_tree_entry *te;
4915 int isroot = s->tree == s->root;
4918 if (s->first_displayed_entry == NULL)
4921 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4922 while (i++ < maxscroll) {
4925 s->first_displayed_entry = NULL;
4928 s->first_displayed_entry = te;
4929 te = got_tree_entry_get_prev(s->tree, te);
4934 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4936 struct got_tree_entry *next, *last;
4939 if (s->first_displayed_entry)
4940 next = got_tree_entry_get_next(s->tree,
4941 s->first_displayed_entry);
4943 next = got_object_tree_get_first_entry(s->tree);
4945 last = s->last_displayed_entry;
4946 while (next && last && n++ < maxscroll) {
4947 last = got_tree_entry_get_next(s->tree, last);
4949 s->first_displayed_entry = next;
4950 next = got_tree_entry_get_next(s->tree, next);
4955 static const struct got_error *
4956 tree_entry_path(char **path, struct tog_parent_trees *parents,
4957 struct got_tree_entry *te)
4959 const struct got_error *err = NULL;
4960 struct tog_parent_tree *pt;
4961 size_t len = 2; /* for leading slash and NUL */
4963 TAILQ_FOREACH(pt, parents, entry)
4964 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4967 len += strlen(got_tree_entry_get_name(te));
4969 *path = calloc(1, len);
4971 return got_error_from_errno("calloc");
4974 pt = TAILQ_LAST(parents, tog_parent_trees);
4976 const char *name = got_tree_entry_get_name(pt->selected_entry);
4977 if (strlcat(*path, name, len) >= len) {
4978 err = got_error(GOT_ERR_NO_SPACE);
4981 if (strlcat(*path, "/", len) >= len) {
4982 err = got_error(GOT_ERR_NO_SPACE);
4985 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4988 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4989 err = got_error(GOT_ERR_NO_SPACE);
5001 static const struct got_error *
5002 blame_tree_entry(struct tog_view **new_view, int begin_x,
5003 struct got_tree_entry *te, struct tog_parent_trees *parents,
5004 struct got_object_id *commit_id, struct got_repository *repo)
5006 const struct got_error *err = NULL;
5008 struct tog_view *blame_view;
5012 err = tree_entry_path(&path, parents, te);
5016 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5017 if (blame_view == NULL) {
5018 err = got_error_from_errno("view_open");
5022 err = open_blame_view(blame_view, path, commit_id, repo);
5024 if (err->code == GOT_ERR_CANCELLED)
5026 view_close(blame_view);
5028 *new_view = blame_view;
5034 static const struct got_error *
5035 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5036 struct tog_tree_view_state *s)
5038 struct tog_view *log_view;
5039 const struct got_error *err = NULL;
5044 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5045 if (log_view == NULL)
5046 return got_error_from_errno("view_open");
5048 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5052 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5055 view_close(log_view);
5057 *new_view = log_view;
5062 static const struct got_error *
5063 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5064 struct got_object_id *commit_id, const char *head_ref_name,
5065 struct got_repository *repo)
5067 const struct got_error *err = NULL;
5068 char *commit_id_str = NULL;
5069 struct tog_tree_view_state *s = &view->state.tree;
5071 TAILQ_INIT(&s->parents);
5073 err = got_object_id_str(&commit_id_str, commit_id);
5077 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5078 err = got_error_from_errno("asprintf");
5082 s->root = s->tree = root;
5083 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5084 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5085 s->commit_id = got_object_id_dup(commit_id);
5086 if (s->commit_id == NULL) {
5087 err = got_error_from_errno("got_object_id_dup");
5090 if (head_ref_name) {
5091 s->head_ref_name = strdup(head_ref_name);
5092 if (s->head_ref_name == NULL) {
5093 err = got_error_from_errno("strdup");
5099 SIMPLEQ_INIT(&s->colors);
5101 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5102 err = add_color(&s->colors, "\\$$",
5103 TOG_COLOR_TREE_SUBMODULE,
5104 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5107 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5108 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5110 free_colors(&s->colors);
5113 err = add_color(&s->colors, "/$",
5114 TOG_COLOR_TREE_DIRECTORY,
5115 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5117 free_colors(&s->colors);
5121 err = add_color(&s->colors, "\\*$",
5122 TOG_COLOR_TREE_EXECUTABLE,
5123 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5125 free_colors(&s->colors);
5129 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5130 get_color_value("TOG_COLOR_COMMIT"));
5132 free_colors(&s->colors);
5137 view->show = show_tree_view;
5138 view->input = input_tree_view;
5139 view->close = close_tree_view;
5140 view->search_start = search_start_tree_view;
5141 view->search_next = search_next_tree_view;
5143 free(commit_id_str);
5145 free(s->tree_label);
5146 s->tree_label = NULL;
5151 static const struct got_error *
5152 close_tree_view(struct tog_view *view)
5154 struct tog_tree_view_state *s = &view->state.tree;
5156 free_colors(&s->colors);
5157 free(s->tree_label);
5158 s->tree_label = NULL;
5160 s->commit_id = NULL;
5161 free(s->head_ref_name);
5162 s->head_ref_name = NULL;
5163 while (!TAILQ_EMPTY(&s->parents)) {
5164 struct tog_parent_tree *parent;
5165 parent = TAILQ_FIRST(&s->parents);
5166 TAILQ_REMOVE(&s->parents, parent, entry);
5170 if (s->tree != s->root)
5171 got_object_tree_close(s->tree);
5172 got_object_tree_close(s->root);
5176 static const struct got_error *
5177 search_start_tree_view(struct tog_view *view)
5179 struct tog_tree_view_state *s = &view->state.tree;
5181 s->matched_entry = NULL;
5186 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5188 regmatch_t regmatch;
5190 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5194 static const struct got_error *
5195 search_next_tree_view(struct tog_view *view)
5197 struct tog_tree_view_state *s = &view->state.tree;
5198 struct got_tree_entry *te = NULL;
5200 if (!view->searching) {
5201 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5205 if (s->matched_entry) {
5206 if (view->searching == TOG_SEARCH_FORWARD) {
5207 if (s->selected_entry)
5208 te = got_tree_entry_get_next(s->tree,
5211 te = got_object_tree_get_first_entry(s->tree);
5213 if (s->selected_entry == NULL)
5214 te = got_object_tree_get_last_entry(s->tree);
5216 te = got_tree_entry_get_prev(s->tree,
5220 if (view->searching == TOG_SEARCH_FORWARD)
5221 te = got_object_tree_get_first_entry(s->tree);
5223 te = got_object_tree_get_last_entry(s->tree);
5228 if (s->matched_entry == NULL) {
5229 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5232 if (view->searching == TOG_SEARCH_FORWARD)
5233 te = got_object_tree_get_first_entry(s->tree);
5235 te = got_object_tree_get_last_entry(s->tree);
5238 if (match_tree_entry(te, &view->regex)) {
5239 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5240 s->matched_entry = te;
5244 if (view->searching == TOG_SEARCH_FORWARD)
5245 te = got_tree_entry_get_next(s->tree, te);
5247 te = got_tree_entry_get_prev(s->tree, te);
5250 if (s->matched_entry) {
5251 s->first_displayed_entry = s->matched_entry;
5258 static const struct got_error *
5259 show_tree_view(struct tog_view *view)
5261 const struct got_error *err = NULL;
5262 struct tog_tree_view_state *s = &view->state.tree;
5265 err = tree_entry_path(&parent_path, &s->parents, NULL);
5269 err = draw_tree_entries(view, parent_path);
5276 static const struct got_error *
5277 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5279 const struct got_error *err = NULL;
5280 struct tog_tree_view_state *s = &view->state.tree;
5281 struct tog_view *log_view, *ref_view;
5286 s->show_ids = !s->show_ids;
5289 if (!s->selected_entry)
5291 if (view_is_parent_view(view))
5292 begin_x = view_split_begin_x(view->begin_x);
5293 err = log_selected_tree_entry(&log_view, begin_x, s);
5295 log_view->focussed = 1;
5296 if (view_is_parent_view(view)) {
5297 err = view_close_child(view);
5300 view_set_child(view, log_view);
5301 view->focus_child = 1;
5303 *new_view = log_view;
5306 if (view_is_parent_view(view))
5307 begin_x = view_split_begin_x(view->begin_x);
5308 ref_view = view_open(view->nlines, view->ncols,
5309 view->begin_y, begin_x, TOG_VIEW_REF);
5310 if (ref_view == NULL)
5311 return got_error_from_errno("view_open");
5312 err = open_ref_view(ref_view, s->repo);
5314 view_close(ref_view);
5318 ref_view->focussed = 1;
5319 if (view_is_parent_view(view)) {
5320 err = view_close_child(view);
5323 view_set_child(view, ref_view);
5324 view->focus_child = 1;
5326 *new_view = ref_view;
5330 if (s->selected > 0) {
5334 tree_scroll_up(s, 1);
5338 if (s->tree == s->root) {
5339 if (got_object_tree_get_first_entry(s->tree) ==
5340 s->first_displayed_entry)
5343 if (s->first_displayed_entry == NULL)
5346 tree_scroll_up(s, MAX(0, view->nlines - 3));
5350 if (s->selected < s->ndisplayed - 1) {
5354 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5356 /* can't scroll any further */
5358 tree_scroll_down(s, 1);
5362 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5364 /* can't scroll any further; move cursor down */
5365 if (s->selected < s->ndisplayed - 1)
5366 s->selected = s->ndisplayed - 1;
5369 tree_scroll_down(s, view->nlines - 3);
5374 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5375 struct tog_parent_tree *parent;
5376 /* user selected '..' */
5377 if (s->tree == s->root)
5379 parent = TAILQ_FIRST(&s->parents);
5380 TAILQ_REMOVE(&s->parents, parent,
5382 got_object_tree_close(s->tree);
5383 s->tree = parent->tree;
5384 s->first_displayed_entry =
5385 parent->first_displayed_entry;
5387 parent->selected_entry;
5388 s->selected = parent->selected;
5390 } else if (S_ISDIR(got_tree_entry_get_mode(
5391 s->selected_entry))) {
5392 struct got_tree_object *subtree;
5393 err = got_object_open_as_tree(&subtree, s->repo,
5394 got_tree_entry_get_id(s->selected_entry));
5397 err = tree_view_visit_subtree(s, subtree);
5399 got_object_tree_close(subtree);
5402 } else if (S_ISREG(got_tree_entry_get_mode(
5403 s->selected_entry))) {
5404 struct tog_view *blame_view;
5405 int begin_x = view_is_parent_view(view) ?
5406 view_split_begin_x(view->begin_x) : 0;
5408 err = blame_tree_entry(&blame_view, begin_x,
5409 s->selected_entry, &s->parents,
5410 s->commit_id, s->repo);
5414 blame_view->focussed = 1;
5415 if (view_is_parent_view(view)) {
5416 err = view_close_child(view);
5419 view_set_child(view, blame_view);
5420 view->focus_child = 1;
5422 *new_view = blame_view;
5426 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5427 s->selected = view->nlines - 4;
5440 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5445 static const struct got_error *
5446 cmd_tree(int argc, char *argv[])
5448 const struct got_error *error;
5449 struct got_repository *repo = NULL;
5450 struct got_worktree *worktree = NULL;
5451 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5452 struct got_object_id *commit_id = NULL;
5453 const char *commit_id_arg = NULL;
5455 struct got_commit_object *commit = NULL;
5456 struct got_tree_object *tree = NULL;
5457 struct got_reference *ref = NULL;
5458 const char *head_ref_name = NULL;
5460 struct tog_view *view;
5462 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5465 commit_id_arg = optarg;
5468 repo_path = realpath(optarg, NULL);
5469 if (repo_path == NULL)
5470 return got_error_from_errno2("realpath",
5485 cwd = getcwd(NULL, 0);
5487 return got_error_from_errno("getcwd");
5489 error = got_worktree_open(&worktree, cwd);
5490 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5493 if (repo_path == NULL) {
5496 strdup(got_worktree_get_repo_path(worktree));
5498 repo_path = strdup(cwd);
5500 if (repo_path == NULL) {
5501 error = got_error_from_errno("strdup");
5505 error = got_repo_open(&repo, repo_path, NULL);
5509 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5516 error = apply_unveil(got_repo_get_path(repo), NULL);
5520 if (commit_id_arg == NULL) {
5521 error = got_repo_match_object_id(&commit_id, &label,
5522 worktree ? got_worktree_get_head_ref_name(worktree) :
5523 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
5526 head_ref_name = label;
5528 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5530 head_ref_name = got_ref_get_name(ref);
5531 else if (error->code != GOT_ERR_NOT_REF)
5533 error = got_repo_match_object_id(&commit_id, NULL,
5534 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5539 error = got_object_open_as_commit(&commit, repo, commit_id);
5543 error = got_object_open_as_tree(&tree, repo,
5544 got_object_commit_get_tree_id(commit));
5548 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5550 error = got_error_from_errno("view_open");
5553 error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5556 if (!got_path_is_root_dir(in_repo_path)) {
5557 error = tree_view_walk_path(&view->state.tree, commit_id,
5564 /* Release work tree lock. */
5565 got_worktree_close(worktree);
5568 error = view_loop(view);
5577 got_object_commit_close(commit);
5579 got_object_tree_close(tree);
5581 got_repo_close(repo);
5585 static const struct got_error *
5586 ref_view_load_refs(struct tog_ref_view_state *s)
5588 const struct got_error *err;
5589 struct got_reflist_entry *sre;
5590 struct tog_reflist_entry *re;
5592 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5593 got_ref_cmp_by_name, NULL);
5598 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5599 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5602 re = malloc(sizeof(*re));
5604 return got_error_from_errno("malloc");
5607 re->idx = s->nrefs++;
5608 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5615 ref_view_free_refs(struct tog_ref_view_state *s)
5617 struct tog_reflist_entry *re;
5619 while (!TAILQ_EMPTY(&s->refs)) {
5620 re = TAILQ_FIRST(&s->refs);
5621 TAILQ_REMOVE(&s->refs, re, entry);
5624 got_ref_list_free(&s->simplerefs);
5627 static const struct got_error *
5628 open_ref_view(struct tog_view *view, struct got_repository *repo)
5630 const struct got_error *err = NULL;
5631 struct tog_ref_view_state *s = &view->state.ref;
5633 s->selected_entry = 0;
5636 SIMPLEQ_INIT(&s->simplerefs);
5637 TAILQ_INIT(&s->refs);
5638 SIMPLEQ_INIT(&s->colors);
5640 err = ref_view_load_refs(s);
5644 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5646 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5647 err = add_color(&s->colors, "^refs/heads/",
5648 TOG_COLOR_REFS_HEADS,
5649 get_color_value("TOG_COLOR_REFS_HEADS"));
5653 err = add_color(&s->colors, "^refs/tags/",
5654 TOG_COLOR_REFS_TAGS,
5655 get_color_value("TOG_COLOR_REFS_TAGS"));
5659 err = add_color(&s->colors, "^refs/remotes/",
5660 TOG_COLOR_REFS_REMOTES,
5661 get_color_value("TOG_COLOR_REFS_REMOTES"));
5666 view->show = show_ref_view;
5667 view->input = input_ref_view;
5668 view->close = close_ref_view;
5669 view->search_start = search_start_ref_view;
5670 view->search_next = search_next_ref_view;
5673 free_colors(&s->colors);
5677 static const struct got_error *
5678 close_ref_view(struct tog_view *view)
5680 struct tog_ref_view_state *s = &view->state.ref;
5682 ref_view_free_refs(s);
5683 free_colors(&s->colors);
5688 static const struct got_error *
5689 resolve_reflist_entry(struct got_object_id **commit_id,
5690 struct tog_reflist_entry *re, struct got_repository *repo)
5692 const struct got_error *err = NULL;
5693 struct got_object_id *obj_id;
5694 struct got_tag_object *tag = NULL;
5699 err = got_ref_resolve(&obj_id, repo, re->ref);
5703 err = got_object_get_type(&obj_type, repo, obj_id);
5708 case GOT_OBJ_TYPE_COMMIT:
5709 *commit_id = obj_id;
5711 case GOT_OBJ_TYPE_TAG:
5712 err = got_object_open_as_tag(&tag, repo, obj_id);
5716 err = got_object_get_type(&obj_type, repo,
5717 got_object_tag_get_object_id(tag));
5720 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5721 err = got_error(GOT_ERR_OBJ_TYPE);
5724 *commit_id = got_object_id_dup(
5725 got_object_tag_get_object_id(tag));
5726 if (*commit_id == NULL) {
5727 err = got_error_from_errno("got_object_id_dup");
5732 err = got_error(GOT_ERR_OBJ_TYPE);
5738 got_object_tag_close(tag);
5746 static const struct got_error *
5747 log_ref_entry(struct tog_view **new_view, int begin_x,
5748 struct tog_reflist_entry *re, struct got_repository *repo)
5750 struct tog_view *log_view;
5751 const struct got_error *err = NULL;
5752 struct got_object_id *commit_id = NULL;
5756 err = resolve_reflist_entry(&commit_id, re, repo);
5758 if (err->code != GOT_ERR_OBJ_TYPE)
5764 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5765 if (log_view == NULL) {
5766 err = got_error_from_errno("view_open");
5770 err = open_log_view(log_view, commit_id, repo,
5771 got_ref_get_name(re->ref), "", 0);
5774 view_close(log_view);
5776 *new_view = log_view;
5782 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5784 struct tog_reflist_entry *re;
5787 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5790 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5791 while (i++ < maxscroll) {
5794 s->first_displayed_entry = re;
5795 re = TAILQ_PREV(re, tog_reflist_head, entry);
5800 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5802 struct tog_reflist_entry *next, *last;
5805 if (s->first_displayed_entry)
5806 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5808 next = TAILQ_FIRST(&s->refs);
5810 last = s->last_displayed_entry;
5811 while (next && last && n++ < maxscroll) {
5812 last = TAILQ_NEXT(last, entry);
5814 s->first_displayed_entry = next;
5815 next = TAILQ_NEXT(next, entry);
5820 static const struct got_error *
5821 search_start_ref_view(struct tog_view *view)
5823 struct tog_ref_view_state *s = &view->state.ref;
5825 s->matched_entry = NULL;
5830 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5832 regmatch_t regmatch;
5834 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5838 static const struct got_error *
5839 search_next_ref_view(struct tog_view *view)
5841 struct tog_ref_view_state *s = &view->state.ref;
5842 struct tog_reflist_entry *re = NULL;
5844 if (!view->searching) {
5845 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5849 if (s->matched_entry) {
5850 if (view->searching == TOG_SEARCH_FORWARD) {
5851 if (s->selected_entry)
5852 re = TAILQ_NEXT(s->selected_entry, entry);
5854 re = TAILQ_PREV(s->selected_entry,
5855 tog_reflist_head, entry);
5857 if (s->selected_entry == NULL)
5858 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5860 re = TAILQ_PREV(s->selected_entry,
5861 tog_reflist_head, entry);
5864 if (view->searching == TOG_SEARCH_FORWARD)
5865 re = TAILQ_FIRST(&s->refs);
5867 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5872 if (s->matched_entry == NULL) {
5873 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5876 if (view->searching == TOG_SEARCH_FORWARD)
5877 re = TAILQ_FIRST(&s->refs);
5879 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5882 if (match_reflist_entry(re, &view->regex)) {
5883 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5884 s->matched_entry = re;
5888 if (view->searching == TOG_SEARCH_FORWARD)
5889 re = TAILQ_NEXT(re, entry);
5891 re = TAILQ_PREV(re, tog_reflist_head, entry);
5894 if (s->matched_entry) {
5895 s->first_displayed_entry = s->matched_entry;
5902 static const struct got_error *
5903 show_ref_view(struct tog_view *view)
5905 const struct got_error *err = NULL;
5906 struct tog_ref_view_state *s = &view->state.ref;
5907 struct tog_reflist_entry *re;
5910 struct tog_color *tc;
5912 int limit = view->nlines;
5914 werase(view->window);
5921 re = s->first_displayed_entry;
5923 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5925 return got_error_from_errno("asprintf");
5927 err = format_line(&wline, &width, line, view->ncols, 0);
5932 if (view_needs_focus_indication(view))
5933 wstandout(view->window);
5934 waddwstr(view->window, wline);
5935 if (view_needs_focus_indication(view))
5936 wstandend(view->window);
5941 if (width < view->ncols - 1)
5942 waddch(view->window, '\n');
5947 while (re && limit > 0) {
5950 if (got_ref_is_symbolic(re->ref)) {
5951 if (asprintf(&line, "%s -> %s",
5952 got_ref_get_name(re->ref),
5953 got_ref_get_symref_target(re->ref)) == -1)
5954 return got_error_from_errno("asprintf");
5955 } else if (s->show_ids) {
5956 struct got_object_id *id;
5958 err = got_ref_resolve(&id, s->repo, re->ref);
5961 err = got_object_id_str(&id_str, id);
5966 if (asprintf(&line, "%s: %s",
5967 got_ref_get_name(re->ref), id_str) == -1) {
5968 err = got_error_from_errno("asprintf");
5976 line = strdup(got_ref_get_name(re->ref));
5978 return got_error_from_errno("strdup");
5981 err = format_line(&wline, &width, line, view->ncols, 0);
5986 if (n == s->selected) {
5988 wstandout(view->window);
5989 s->selected_entry = re;
5991 tc = match_color(&s->colors, got_ref_get_name(re->ref));
5993 wattr_on(view->window,
5994 COLOR_PAIR(tc->colorpair), NULL);
5995 waddwstr(view->window, wline);
5997 wattr_off(view->window,
5998 COLOR_PAIR(tc->colorpair), NULL);
5999 if (width < view->ncols - 1)
6000 waddch(view->window, '\n');
6001 if (n == s->selected && view->focussed)
6002 wstandend(view->window);
6008 s->last_displayed_entry = re;
6011 re = TAILQ_NEXT(re, entry);
6018 static const struct got_error *
6019 browse_ref_tree(struct tog_view **new_view, int begin_x,
6020 struct tog_reflist_entry *re, struct got_repository *repo)
6022 const struct got_error *err = NULL;
6023 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6024 struct got_tree_object *tree = NULL;
6025 struct tog_view *tree_view;
6029 err = resolve_reflist_entry(&commit_id, re, repo);
6031 if (err->code != GOT_ERR_OBJ_TYPE)
6037 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6041 err = got_object_open_as_tree(&tree, repo, tree_id);
6045 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6046 if (tree_view == NULL) {
6047 err = got_error_from_errno("view_open");
6051 err = open_tree_view(tree_view, tree, commit_id,
6052 got_ref_get_name(re->ref), repo);
6056 *new_view = tree_view;
6062 got_object_tree_close(tree);
6066 static const struct got_error *
6067 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6069 const struct got_error *err = NULL;
6070 struct tog_ref_view_state *s = &view->state.ref;
6071 struct tog_view *log_view, *tree_view;
6076 s->show_ids = !s->show_ids;
6080 if (!s->selected_entry)
6082 if (view_is_parent_view(view))
6083 begin_x = view_split_begin_x(view->begin_x);
6084 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6087 log_view->focussed = 1;
6088 if (view_is_parent_view(view)) {
6089 err = view_close_child(view);
6092 view_set_child(view, log_view);
6093 view->focus_child = 1;
6095 *new_view = log_view;
6098 if (!s->selected_entry)
6100 if (view_is_parent_view(view))
6101 begin_x = view_split_begin_x(view->begin_x);
6102 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6104 if (err || tree_view == NULL)
6107 tree_view->focussed = 1;
6108 if (view_is_parent_view(view)) {
6109 err = view_close_child(view);
6112 view_set_child(view, tree_view);
6113 view->focus_child = 1;
6115 *new_view = tree_view;
6119 if (s->selected > 0) {
6123 ref_scroll_up(s, 1);
6127 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6129 ref_scroll_up(s, MAX(0, view->nlines - 1));
6133 if (s->selected < s->ndisplayed - 1) {
6137 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6138 /* can't scroll any further */
6140 ref_scroll_down(s, 1);
6144 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6145 /* can't scroll any further; move cursor down */
6146 if (s->selected < s->ndisplayed - 1)
6147 s->selected = s->ndisplayed - 1;
6150 ref_scroll_down(s, view->nlines - 1);
6153 ref_view_free_refs(s);
6154 err = ref_view_load_refs(s);
6157 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6158 s->selected = view->nlines - 2;
6171 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6176 static const struct got_error *
6177 cmd_ref(int argc, char *argv[])
6179 const struct got_error *error;
6180 struct got_repository *repo = NULL;
6181 struct got_worktree *worktree = NULL;
6182 char *cwd = NULL, *repo_path = NULL;
6184 struct tog_view *view;
6186 while ((ch = getopt(argc, argv, "r:")) != -1) {
6189 repo_path = realpath(optarg, NULL);
6190 if (repo_path == NULL)
6191 return got_error_from_errno2("realpath",
6206 cwd = getcwd(NULL, 0);
6208 return got_error_from_errno("getcwd");
6210 error = got_worktree_open(&worktree, cwd);
6211 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6214 if (repo_path == NULL) {
6217 strdup(got_worktree_get_repo_path(worktree));
6219 repo_path = strdup(cwd);
6221 if (repo_path == NULL) {
6222 error = got_error_from_errno("strdup");
6226 error = got_repo_open(&repo, repo_path, NULL);
6232 error = apply_unveil(got_repo_get_path(repo), NULL);
6236 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6238 error = got_error_from_errno("view_open");
6242 error = open_ref_view(view, repo);
6247 /* Release work tree lock. */
6248 got_worktree_close(worktree);
6251 error = view_loop(view);
6256 got_repo_close(repo);
6261 list_commands(FILE *fp)
6265 fprintf(fp, "commands:");
6266 for (i = 0; i < nitems(tog_commands); i++) {
6267 struct tog_cmd *cmd = &tog_commands[i];
6268 fprintf(fp, " %s", cmd->name);
6274 usage(int hflag, int status)
6276 FILE *fp = (status == 0) ? stdout : stderr;
6278 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6281 fprintf(fp, "lazy usage: %s path\n", getprogname());
6288 make_argv(int argc, ...)
6296 argv = calloc(argc, sizeof(char *));
6299 for (i = 0; i < argc; i++) {
6300 argv[i] = strdup(va_arg(ap, char *));
6301 if (argv[i] == NULL)
6310 * Try to convert 'tog path' into a 'tog log path' command.
6311 * The user could simply have mistyped the command rather than knowingly
6312 * provided a path. So check whether argv[0] can in fact be resolved
6313 * to a path in the HEAD commit and print a special error if not.
6314 * This hack is for mpi@ <3
6316 static const struct got_error *
6317 tog_log_with_path(int argc, char *argv[])
6319 const struct got_error *error = NULL;
6320 struct tog_cmd *cmd = NULL;
6321 struct got_repository *repo = NULL;
6322 struct got_worktree *worktree = NULL;
6323 struct got_object_id *commit_id = NULL, *id = NULL;
6324 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6325 char *commit_id_str = NULL, **cmd_argv = NULL;
6327 cwd = getcwd(NULL, 0);
6329 return got_error_from_errno("getcwd");
6331 error = got_worktree_open(&worktree, cwd);
6332 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6336 repo_path = strdup(got_worktree_get_repo_path(worktree));
6338 repo_path = strdup(cwd);
6339 if (repo_path == NULL) {
6340 error = got_error_from_errno("strdup");
6344 error = got_repo_open(&repo, repo_path, NULL);
6348 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6353 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6354 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6355 GOT_OBJ_TYPE_COMMIT, 1, repo);
6360 got_worktree_close(worktree);
6364 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6366 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6368 fprintf(stderr, "%s: '%s' is no known command or path\n",
6369 getprogname(), argv[0]);
6374 got_repo_close(repo);
6377 error = got_object_id_str(&commit_id_str, commit_id);
6381 cmd = &tog_commands[0]; /* log */
6383 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6384 error = cmd->cmd_main(argc, cmd_argv);
6387 got_repo_close(repo);
6389 got_worktree_close(worktree);
6391 free(commit_id_str);
6398 for (i = 0; i < argc; i++)
6406 main(int argc, char *argv[])
6408 const struct got_error *error = NULL;
6409 struct tog_cmd *cmd = NULL;
6410 int ch, hflag = 0, Vflag = 0;
6411 char **cmd_argv = NULL;
6412 static struct option longopts[] = {
6413 { "version", no_argument, NULL, 'V' },
6417 setlocale(LC_CTYPE, "");
6419 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6439 got_version_print_str();
6444 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6452 /* Build an argument vector which runs a default command. */
6453 cmd = &tog_commands[0];
6455 cmd_argv = make_argv(argc, cmd->name);
6459 /* Did the user specify a command? */
6460 for (i = 0; i < nitems(tog_commands); i++) {
6461 if (strncmp(tog_commands[i].name, argv[0],
6462 strlen(argv[0])) == 0) {
6463 cmd = &tog_commands[i];
6472 /* No command specified; try log with a path */
6473 error = tog_log_with_path(argc, argv);
6478 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6485 for (i = 0; i < argc; i++)
6490 if (error && error->code != GOT_ERR_CANCELLED)
6491 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);