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);
1197 if (cols + width > wlimit)
1202 err = got_error_from_errno("wcwidth");
1217 static const struct got_error*
1218 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1219 struct got_object_id *id, struct got_repository *repo)
1221 static const struct got_error *err = NULL;
1222 struct got_reflist_entry *re;
1228 SIMPLEQ_FOREACH(re, refs, entry) {
1229 struct got_tag_object *tag = NULL;
1230 struct got_object_id *ref_id;
1233 name = got_ref_get_name(re->ref);
1234 if (strcmp(name, GOT_REF_HEAD) == 0)
1236 if (strncmp(name, "refs/", 5) == 0)
1238 if (strncmp(name, "got/", 4) == 0)
1240 if (strncmp(name, "heads/", 6) == 0)
1242 if (strncmp(name, "remotes/", 8) == 0) {
1244 s = strstr(name, "/" GOT_REF_HEAD);
1245 if (s != NULL && s[strlen(s)] == '\0')
1248 err = got_ref_resolve(&ref_id, repo, re->ref);
1251 if (strncmp(name, "tags/", 5) == 0) {
1252 err = got_object_open_as_tag(&tag, repo, ref_id);
1254 if (err->code != GOT_ERR_OBJ_TYPE) {
1258 /* Ref points at something other than a tag. */
1263 cmp = got_object_id_cmp(tag ?
1264 got_object_tag_get_object_id(tag) : ref_id, id);
1267 got_object_tag_close(tag);
1271 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1272 s ? ", " : "", name) == -1) {
1273 err = got_error_from_errno("asprintf");
1284 static const struct got_error *
1285 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1288 char *smallerthan, *at;
1290 smallerthan = strchr(author, '<');
1291 if (smallerthan && smallerthan[1] != '\0')
1292 author = smallerthan + 1;
1293 at = strchr(author, '@');
1296 return format_line(wauthor, author_width, author, limit, col_tab_align);
1299 static const struct got_error *
1300 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1301 struct got_object_id *id, const size_t date_display_cols,
1302 int author_display_cols)
1304 struct tog_log_view_state *s = &view->state.log;
1305 const struct got_error *err = NULL;
1306 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1307 char *logmsg0 = NULL, *logmsg = NULL;
1308 char *author = NULL;
1309 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1310 int author_width, logmsg_width;
1311 char *newline, *line = NULL;
1313 const int avail = view->ncols;
1315 time_t committer_time;
1316 struct tog_color *tc;
1318 committer_time = got_object_commit_get_committer_time(commit);
1319 if (localtime_r(&committer_time, &tm) == NULL)
1320 return got_error_from_errno("localtime_r");
1321 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1323 return got_error(GOT_ERR_NO_SPACE);
1325 if (avail <= date_display_cols)
1326 limit = MIN(sizeof(datebuf) - 1, avail);
1328 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1329 tc = get_color(&s->colors, TOG_COLOR_DATE);
1331 wattr_on(view->window,
1332 COLOR_PAIR(tc->colorpair), NULL);
1333 waddnstr(view->window, datebuf, limit);
1335 wattr_off(view->window,
1336 COLOR_PAIR(tc->colorpair), NULL);
1343 err = got_object_id_str(&id_str, id);
1346 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1348 wattr_on(view->window,
1349 COLOR_PAIR(tc->colorpair), NULL);
1350 wprintw(view->window, "%.8s ", id_str);
1352 wattr_off(view->window,
1353 COLOR_PAIR(tc->colorpair), NULL);
1360 author = strdup(got_object_commit_get_author(commit));
1361 if (author == NULL) {
1362 err = got_error_from_errno("strdup");
1365 err = format_author(&wauthor, &author_width, author, avail - col, col);
1368 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1370 wattr_on(view->window,
1371 COLOR_PAIR(tc->colorpair), NULL);
1372 waddwstr(view->window, wauthor);
1374 wattr_off(view->window,
1375 COLOR_PAIR(tc->colorpair), NULL);
1376 col += author_width;
1377 while (col < avail && author_width < author_display_cols + 2) {
1378 waddch(view->window, ' ');
1385 err = got_object_commit_get_logmsg(&logmsg0, commit);
1389 while (*logmsg == '\n')
1391 newline = strchr(logmsg, '\n');
1394 limit = avail - col;
1395 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1398 waddwstr(view->window, wlogmsg);
1399 col += logmsg_width;
1400 while (col < avail) {
1401 waddch(view->window, ' ');
1413 static struct commit_queue_entry *
1414 alloc_commit_queue_entry(struct got_commit_object *commit,
1415 struct got_object_id *id)
1417 struct commit_queue_entry *entry;
1419 entry = calloc(1, sizeof(*entry));
1424 entry->commit = commit;
1429 pop_commit(struct commit_queue *commits)
1431 struct commit_queue_entry *entry;
1433 entry = TAILQ_FIRST(&commits->head);
1434 TAILQ_REMOVE(&commits->head, entry, entry);
1435 got_object_commit_close(entry->commit);
1436 commits->ncommits--;
1437 /* Don't free entry->id! It is owned by the commit graph. */
1442 free_commits(struct commit_queue *commits)
1444 while (!TAILQ_EMPTY(&commits->head))
1445 pop_commit(commits);
1448 static const struct got_error *
1449 match_commit(int *have_match, struct got_object_id *id,
1450 struct got_commit_object *commit, regex_t *regex)
1452 const struct got_error *err = NULL;
1453 regmatch_t regmatch;
1454 char *id_str = NULL, *logmsg = NULL;
1458 err = got_object_id_str(&id_str, id);
1462 err = got_object_commit_get_logmsg(&logmsg, commit);
1466 if (regexec(regex, got_object_commit_get_author(commit), 1,
1467 ®match, 0) == 0 ||
1468 regexec(regex, got_object_commit_get_committer(commit), 1,
1469 ®match, 0) == 0 ||
1470 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1471 regexec(regex, logmsg, 1, ®match, 0) == 0)
1479 static const struct got_error *
1480 queue_commits(struct tog_log_thread_args *a)
1482 const struct got_error *err = NULL;
1485 * We keep all commits open throughout the lifetime of the log
1486 * view in order to avoid having to re-fetch commits from disk
1487 * while updating the display.
1490 struct got_object_id *id;
1491 struct got_commit_object *commit;
1492 struct commit_queue_entry *entry;
1495 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1497 if (err || id == NULL)
1500 err = got_object_open_as_commit(&commit, a->repo, id);
1503 entry = alloc_commit_queue_entry(commit, id);
1504 if (entry == NULL) {
1505 err = got_error_from_errno("alloc_commit_queue_entry");
1509 errcode = pthread_mutex_lock(&tog_mutex);
1511 err = got_error_set_errno(errcode,
1512 "pthread_mutex_lock");
1516 entry->idx = a->commits->ncommits;
1517 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1518 a->commits->ncommits++;
1520 if (*a->searching == TOG_SEARCH_FORWARD &&
1521 !*a->search_next_done) {
1523 err = match_commit(&have_match, id, commit, a->regex);
1527 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1530 errcode = pthread_mutex_unlock(&tog_mutex);
1531 if (errcode && err == NULL)
1532 err = got_error_set_errno(errcode,
1533 "pthread_mutex_unlock");
1536 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1542 select_commit(struct tog_log_view_state *s)
1544 struct commit_queue_entry *entry;
1547 entry = s->first_displayed_entry;
1549 if (ncommits == s->selected) {
1550 s->selected_entry = entry;
1553 entry = TAILQ_NEXT(entry, entry);
1558 static const struct got_error *
1559 draw_commits(struct tog_view *view)
1561 const struct got_error *err = NULL;
1562 struct tog_log_view_state *s = &view->state.log;
1563 struct commit_queue_entry *entry = s->selected_entry;
1564 const int limit = view->nlines;
1566 int ncommits, author_cols = 4;
1567 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1568 char *refs_str = NULL;
1570 struct tog_color *tc;
1571 static const size_t date_display_cols = 12;
1573 if (s->selected_entry &&
1574 !(view->searching && view->search_next_done == 0)) {
1575 err = got_object_id_str(&id_str, s->selected_entry->id);
1578 err = build_refs_str(&refs_str, &s->refs,
1579 s->selected_entry->id, s->repo);
1584 if (s->thread_args.commits_needed == 0)
1585 halfdelay(10); /* disable fast refresh */
1587 if (s->thread_args.commits_needed > 0) {
1588 if (asprintf(&ncommits_str, " [%d/%d] %s",
1589 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1590 (view->searching && !view->search_next_done) ?
1591 "searching..." : "loading...") == -1) {
1592 err = got_error_from_errno("asprintf");
1596 const char *search_str = NULL;
1598 if (view->searching) {
1599 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1600 search_str = "no more matches";
1601 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1602 search_str = "no matches found";
1603 else if (!view->search_next_done)
1604 search_str = "searching...";
1607 if (asprintf(&ncommits_str, " [%d/%d] %s",
1608 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1609 search_str ? search_str :
1610 (refs_str ? refs_str : "")) == -1) {
1611 err = got_error_from_errno("asprintf");
1616 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1617 if (asprintf(&header, "commit %s %s%s",
1618 id_str ? id_str : "........................................",
1619 s->in_repo_path, ncommits_str) == -1) {
1620 err = got_error_from_errno("asprintf");
1624 } else if (asprintf(&header, "commit %s%s",
1625 id_str ? id_str : "........................................",
1626 ncommits_str) == -1) {
1627 err = got_error_from_errno("asprintf");
1631 err = format_line(&wline, &width, header, view->ncols, 0);
1635 werase(view->window);
1637 if (view_needs_focus_indication(view))
1638 wstandout(view->window);
1639 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1641 wattr_on(view->window,
1642 COLOR_PAIR(tc->colorpair), NULL);
1643 waddwstr(view->window, wline);
1645 wattr_off(view->window,
1646 COLOR_PAIR(tc->colorpair), NULL);
1647 while (width < view->ncols) {
1648 waddch(view->window, ' ');
1651 if (view_needs_focus_indication(view))
1652 wstandend(view->window);
1657 /* Grow author column size if necessary. */
1658 entry = s->first_displayed_entry;
1664 if (ncommits >= limit - 1)
1666 author = strdup(got_object_commit_get_author(entry->commit));
1667 if (author == NULL) {
1668 err = got_error_from_errno("strdup");
1671 err = format_author(&wauthor, &width, author, COLS,
1673 if (author_cols < width)
1674 author_cols = width;
1678 entry = TAILQ_NEXT(entry, entry);
1681 entry = s->first_displayed_entry;
1682 s->last_displayed_entry = s->first_displayed_entry;
1685 if (ncommits >= limit - 1)
1687 if (ncommits == s->selected)
1688 wstandout(view->window);
1689 err = draw_commit(view, entry->commit, entry->id,
1690 date_display_cols, author_cols);
1691 if (ncommits == s->selected)
1692 wstandend(view->window);
1696 s->last_displayed_entry = entry;
1697 entry = TAILQ_NEXT(entry, entry);
1710 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1712 struct commit_queue_entry *entry;
1715 entry = TAILQ_FIRST(&s->commits.head);
1716 if (s->first_displayed_entry == entry)
1719 entry = s->first_displayed_entry;
1720 while (entry && nscrolled < maxscroll) {
1721 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1723 s->first_displayed_entry = entry;
1729 static const struct got_error *
1730 trigger_log_thread(struct tog_view *view, int wait)
1732 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1735 halfdelay(1); /* fast refresh while loading commits */
1737 while (ta->commits_needed > 0) {
1738 if (ta->log_complete)
1741 /* Wake the log thread. */
1742 errcode = pthread_cond_signal(&ta->need_commits);
1744 return got_error_set_errno(errcode,
1745 "pthread_cond_signal");
1748 * The mutex will be released while the view loop waits
1749 * in wgetch(), at which time the log thread will run.
1754 /* Display progress update in log view. */
1755 show_log_view(view);
1759 /* Wait right here while next commit is being loaded. */
1760 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1762 return got_error_set_errno(errcode,
1763 "pthread_cond_wait");
1765 /* Display progress update in log view. */
1766 show_log_view(view);
1774 static const struct got_error *
1775 log_scroll_down(struct tog_view *view, int maxscroll)
1777 struct tog_log_view_state *s = &view->state.log;
1778 const struct got_error *err = NULL;
1779 struct commit_queue_entry *pentry;
1780 int nscrolled = 0, ncommits_needed;
1782 if (s->last_displayed_entry == NULL)
1785 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1786 if (s->commits.ncommits < ncommits_needed &&
1787 !s->thread_args.log_complete) {
1789 * Ask the log thread for required amount of commits.
1791 s->thread_args.commits_needed += maxscroll;
1792 err = trigger_log_thread(view, 1);
1798 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1802 s->last_displayed_entry = pentry;
1804 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1807 s->first_displayed_entry = pentry;
1808 } while (++nscrolled < maxscroll);
1813 static const struct got_error *
1814 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1815 struct got_commit_object *commit, struct got_object_id *commit_id,
1816 struct tog_view *log_view, struct got_repository *repo)
1818 const struct got_error *err;
1819 struct got_object_qid *parent_id;
1820 struct tog_view *diff_view;
1822 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1823 if (diff_view == NULL)
1824 return got_error_from_errno("view_open");
1826 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1827 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1828 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1830 *new_view = diff_view;
1834 static const struct got_error *
1835 tree_view_visit_subtree(struct tog_tree_view_state *s,
1836 struct got_tree_object *subtree)
1838 struct tog_parent_tree *parent;
1840 parent = calloc(1, sizeof(*parent));
1842 return got_error_from_errno("calloc");
1844 parent->tree = s->tree;
1845 parent->first_displayed_entry = s->first_displayed_entry;
1846 parent->selected_entry = s->selected_entry;
1847 parent->selected = s->selected;
1848 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1851 s->first_displayed_entry = NULL;
1855 static const struct got_error *
1856 tree_view_walk_path(struct tog_tree_view_state *s,
1857 struct got_object_id *commit_id, const char *path)
1859 const struct got_error *err = NULL;
1860 struct got_tree_object *tree = NULL;
1862 char *slash, *subpath = NULL;
1864 /* Walk the path and open corresponding tree objects. */
1867 struct got_tree_entry *te;
1868 struct got_object_id *tree_id;
1874 /* Ensure the correct subtree entry is selected. */
1875 slash = strchr(p, '/');
1877 te_name = strdup(p);
1879 te_name = strndup(p, slash - p);
1880 if (te_name == NULL) {
1881 err = got_error_from_errno("strndup");
1884 te = got_object_tree_find_entry(s->tree, te_name);
1886 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1891 s->first_displayed_entry = s->selected_entry = te;
1893 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1894 break; /* jump to this file's entry */
1896 slash = strchr(p, '/');
1898 subpath = strndup(path, slash - path);
1900 subpath = strdup(path);
1901 if (subpath == NULL) {
1902 err = got_error_from_errno("strdup");
1906 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1911 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1916 err = tree_view_visit_subtree(s, tree);
1918 got_object_tree_close(tree);
1932 static const struct got_error *
1933 browse_commit_tree(struct tog_view **new_view, int begin_x,
1934 struct commit_queue_entry *entry, const char *path,
1935 const char *head_ref_name, struct got_repository *repo)
1937 const struct got_error *err = NULL;
1938 struct got_tree_object *tree;
1939 struct tog_tree_view_state *s;
1940 struct tog_view *tree_view;
1942 err = got_object_open_as_tree(&tree, repo,
1943 got_object_commit_get_tree_id(entry->commit));
1947 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1948 if (tree_view == NULL)
1949 return got_error_from_errno("view_open");
1951 err = open_tree_view(tree_view, tree, entry->id, head_ref_name, repo);
1953 got_object_tree_close(tree);
1956 s = &tree_view->state.tree;
1958 *new_view = tree_view;
1960 if (got_path_is_root_dir(path))
1963 return tree_view_walk_path(s, entry->id, path);
1966 static const struct got_error *
1967 block_signals_used_by_main_thread(void)
1972 if (sigemptyset(&sigset) == -1)
1973 return got_error_from_errno("sigemptyset");
1975 /* tog handles SIGWINCH and SIGCONT */
1976 if (sigaddset(&sigset, SIGWINCH) == -1)
1977 return got_error_from_errno("sigaddset");
1978 if (sigaddset(&sigset, SIGCONT) == -1)
1979 return got_error_from_errno("sigaddset");
1981 /* ncurses handles SIGTSTP */
1982 if (sigaddset(&sigset, SIGTSTP) == -1)
1983 return got_error_from_errno("sigaddset");
1985 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1987 return got_error_set_errno(errcode, "pthread_sigmask");
1993 log_thread(void *arg)
1995 const struct got_error *err = NULL;
1997 struct tog_log_thread_args *a = arg;
2000 err = block_signals_used_by_main_thread();
2004 while (!done && !err && !tog_sigpipe_received) {
2005 err = queue_commits(a);
2007 if (err->code != GOT_ERR_ITER_COMPLETED)
2011 } else if (a->commits_needed > 0)
2012 a->commits_needed--;
2014 errcode = pthread_mutex_lock(&tog_mutex);
2016 err = got_error_set_errno(errcode,
2017 "pthread_mutex_lock");
2019 } else if (*a->quit)
2021 else if (*a->first_displayed_entry == NULL) {
2022 *a->first_displayed_entry =
2023 TAILQ_FIRST(&a->commits->head);
2024 *a->selected_entry = *a->first_displayed_entry;
2027 errcode = pthread_cond_signal(&a->commit_loaded);
2029 err = got_error_set_errno(errcode,
2030 "pthread_cond_signal");
2031 pthread_mutex_unlock(&tog_mutex);
2036 a->commits_needed = 0;
2038 if (a->commits_needed == 0) {
2039 errcode = pthread_cond_wait(&a->need_commits,
2042 err = got_error_set_errno(errcode,
2043 "pthread_cond_wait");
2049 errcode = pthread_mutex_unlock(&tog_mutex);
2050 if (errcode && err == NULL)
2051 err = got_error_set_errno(errcode,
2052 "pthread_mutex_unlock");
2054 a->log_complete = 1;
2058 static const struct got_error *
2059 stop_log_thread(struct tog_log_view_state *s)
2061 const struct got_error *err = NULL;
2066 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2068 return got_error_set_errno(errcode,
2069 "pthread_cond_signal");
2070 errcode = pthread_mutex_unlock(&tog_mutex);
2072 return got_error_set_errno(errcode,
2073 "pthread_mutex_unlock");
2074 errcode = pthread_join(s->thread, (void **)&err);
2076 return got_error_set_errno(errcode, "pthread_join");
2077 errcode = pthread_mutex_lock(&tog_mutex);
2079 return got_error_set_errno(errcode,
2080 "pthread_mutex_lock");
2084 if (s->thread_args.repo) {
2085 got_repo_close(s->thread_args.repo);
2086 s->thread_args.repo = NULL;
2089 if (s->thread_args.graph) {
2090 got_commit_graph_close(s->thread_args.graph);
2091 s->thread_args.graph = NULL;
2097 static const struct got_error *
2098 close_log_view(struct tog_view *view)
2100 const struct got_error *err = NULL;
2101 struct tog_log_view_state *s = &view->state.log;
2104 err = stop_log_thread(s);
2106 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2107 if (errcode && err == NULL)
2108 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2110 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2111 if (errcode && err == NULL)
2112 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2114 free_commits(&s->commits);
2115 free(s->in_repo_path);
2116 s->in_repo_path = NULL;
2119 free(s->head_ref_name);
2120 s->head_ref_name = NULL;
2121 got_ref_list_free(&s->refs);
2125 static const struct got_error *
2126 search_start_log_view(struct tog_view *view)
2128 struct tog_log_view_state *s = &view->state.log;
2130 s->matched_entry = NULL;
2131 s->search_entry = NULL;
2135 static const struct got_error *
2136 search_next_log_view(struct tog_view *view)
2138 const struct got_error *err = NULL;
2139 struct tog_log_view_state *s = &view->state.log;
2140 struct commit_queue_entry *entry;
2142 /* Display progress update in log view. */
2143 show_log_view(view);
2147 if (s->search_entry) {
2149 errcode = pthread_mutex_unlock(&tog_mutex);
2151 return got_error_set_errno(errcode,
2152 "pthread_mutex_unlock");
2153 ch = wgetch(view->window);
2154 errcode = pthread_mutex_lock(&tog_mutex);
2156 return got_error_set_errno(errcode,
2157 "pthread_mutex_lock");
2158 if (ch == KEY_BACKSPACE) {
2159 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2162 if (view->searching == TOG_SEARCH_FORWARD)
2163 entry = TAILQ_NEXT(s->search_entry, entry);
2165 entry = TAILQ_PREV(s->search_entry,
2166 commit_queue_head, entry);
2167 } else if (s->matched_entry) {
2168 if (view->searching == TOG_SEARCH_FORWARD)
2169 entry = TAILQ_NEXT(s->matched_entry, entry);
2171 entry = TAILQ_PREV(s->matched_entry,
2172 commit_queue_head, entry);
2174 if (view->searching == TOG_SEARCH_FORWARD)
2175 entry = TAILQ_FIRST(&s->commits.head);
2177 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2183 if (entry == NULL) {
2184 if (s->thread_args.log_complete ||
2185 view->searching == TOG_SEARCH_BACKWARD) {
2186 view->search_next_done =
2187 (s->matched_entry == NULL ?
2188 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2189 s->search_entry = NULL;
2193 * Poke the log thread for more commits and return,
2194 * allowing the main loop to make progress. Search
2195 * will resume at s->search_entry once we come back.
2197 s->thread_args.commits_needed++;
2198 return trigger_log_thread(view, 0);
2201 err = match_commit(&have_match, entry->id, entry->commit,
2206 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2207 s->matched_entry = entry;
2211 s->search_entry = entry;
2212 if (view->searching == TOG_SEARCH_FORWARD)
2213 entry = TAILQ_NEXT(entry, entry);
2215 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2218 if (s->matched_entry) {
2219 int cur = s->selected_entry->idx;
2220 while (cur < s->matched_entry->idx) {
2221 err = input_log_view(NULL, view, KEY_DOWN);
2226 while (cur > s->matched_entry->idx) {
2227 err = input_log_view(NULL, view, KEY_UP);
2234 s->search_entry = NULL;
2239 static const struct got_error *
2240 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2241 struct got_repository *repo, const char *head_ref_name,
2242 const char *in_repo_path, int log_branches)
2244 const struct got_error *err = NULL;
2245 struct tog_log_view_state *s = &view->state.log;
2246 struct got_repository *thread_repo = NULL;
2247 struct got_commit_graph *thread_graph = NULL;
2250 SIMPLEQ_INIT(&s->refs);
2252 if (in_repo_path != s->in_repo_path) {
2253 free(s->in_repo_path);
2254 s->in_repo_path = strdup(in_repo_path);
2255 if (s->in_repo_path == NULL)
2256 return got_error_from_errno("strdup");
2259 /* The commit queue only contains commits being displayed. */
2260 TAILQ_INIT(&s->commits.head);
2261 s->commits.ncommits = 0;
2263 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2268 if (head_ref_name) {
2269 s->head_ref_name = strdup(head_ref_name);
2270 if (s->head_ref_name == NULL) {
2271 err = got_error_from_errno("strdup");
2275 s->start_id = got_object_id_dup(start_id);
2276 if (s->start_id == NULL) {
2277 err = got_error_from_errno("got_object_id_dup");
2280 s->log_branches = log_branches;
2282 SIMPLEQ_INIT(&s->colors);
2283 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2284 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2285 get_color_value("TOG_COLOR_COMMIT"));
2288 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2289 get_color_value("TOG_COLOR_AUTHOR"));
2291 free_colors(&s->colors);
2294 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2295 get_color_value("TOG_COLOR_DATE"));
2297 free_colors(&s->colors);
2302 view->show = show_log_view;
2303 view->input = input_log_view;
2304 view->close = close_log_view;
2305 view->search_start = search_start_log_view;
2306 view->search_next = search_next_log_view;
2308 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2311 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2315 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2316 s->repo, NULL, NULL);
2320 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2322 err = got_error_set_errno(errcode, "pthread_cond_init");
2325 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2327 err = got_error_set_errno(errcode, "pthread_cond_init");
2331 s->thread_args.commits_needed = view->nlines;
2332 s->thread_args.graph = thread_graph;
2333 s->thread_args.commits = &s->commits;
2334 s->thread_args.in_repo_path = s->in_repo_path;
2335 s->thread_args.start_id = s->start_id;
2336 s->thread_args.repo = thread_repo;
2337 s->thread_args.log_complete = 0;
2338 s->thread_args.quit = &s->quit;
2339 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2340 s->thread_args.selected_entry = &s->selected_entry;
2341 s->thread_args.searching = &view->searching;
2342 s->thread_args.search_next_done = &view->search_next_done;
2343 s->thread_args.regex = &view->regex;
2346 close_log_view(view);
2350 static const struct got_error *
2351 show_log_view(struct tog_view *view)
2353 const struct got_error *err;
2354 struct tog_log_view_state *s = &view->state.log;
2356 if (s->thread == NULL) {
2357 int errcode = pthread_create(&s->thread, NULL, log_thread,
2360 return got_error_set_errno(errcode, "pthread_create");
2361 if (s->thread_args.commits_needed > 0) {
2362 err = trigger_log_thread(view, 1);
2368 return draw_commits(view);
2371 static const struct got_error *
2372 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2374 const struct got_error *err = NULL;
2375 struct tog_log_view_state *s = &view->state.log;
2376 struct tog_view *diff_view = NULL, *tree_view = NULL;
2377 struct tog_view *ref_view = NULL;
2388 if (s->first_displayed_entry == NULL)
2390 if (s->selected > 0)
2393 log_scroll_up(s, 1);
2398 if (s->first_displayed_entry == NULL)
2400 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2403 log_scroll_up(s, view->nlines - 1);
2410 if (s->first_displayed_entry == NULL)
2412 if (s->selected < MIN(view->nlines - 2,
2413 s->commits.ncommits - 1))
2416 err = log_scroll_down(view, 1);
2424 struct commit_queue_entry *first;
2425 first = s->first_displayed_entry;
2428 err = log_scroll_down(view, view->nlines - 1);
2431 if (first == s->first_displayed_entry &&
2432 s->selected < MIN(view->nlines - 2,
2433 s->commits.ncommits - 1)) {
2434 /* can't scroll further down */
2435 s->selected = MIN(view->nlines - 2,
2436 s->commits.ncommits - 1);
2442 if (s->selected > view->nlines - 2)
2443 s->selected = view->nlines - 2;
2444 if (s->selected > s->commits.ncommits - 1)
2445 s->selected = s->commits.ncommits - 1;
2447 if (s->commits.ncommits < view->nlines - 1 &&
2448 !s->thread_args.log_complete) {
2449 s->thread_args.commits_needed += (view->nlines - 1) -
2450 s->commits.ncommits;
2451 err = trigger_log_thread(view, 1);
2457 if (s->selected_entry == NULL)
2459 if (view_is_parent_view(view))
2460 begin_x = view_split_begin_x(view->begin_x);
2461 err = open_diff_view_for_commit(&diff_view, begin_x,
2462 s->selected_entry->commit, s->selected_entry->id,
2467 diff_view->focussed = 1;
2468 if (view_is_parent_view(view)) {
2469 err = view_close_child(view);
2472 view_set_child(view, diff_view);
2473 view->focus_child = 1;
2475 *new_view = diff_view;
2478 if (s->selected_entry == NULL)
2480 if (view_is_parent_view(view))
2481 begin_x = view_split_begin_x(view->begin_x);
2482 err = browse_commit_tree(&tree_view, begin_x,
2483 s->selected_entry, s->in_repo_path, s->head_ref_name,
2488 tree_view->focussed = 1;
2489 if (view_is_parent_view(view)) {
2490 err = view_close_child(view);
2493 view_set_child(view, tree_view);
2494 view->focus_child = 1;
2496 *new_view = tree_view;
2501 if (ch == KEY_BACKSPACE &&
2502 got_path_is_root_dir(s->in_repo_path))
2504 err = stop_log_thread(s);
2507 if (ch == KEY_BACKSPACE) {
2509 err = got_path_dirname(&parent_path, s->in_repo_path);
2512 free(s->in_repo_path);
2513 s->in_repo_path = parent_path;
2514 s->thread_args.in_repo_path = s->in_repo_path;
2515 } else if (ch == CTRL('l')) {
2516 struct got_object_id *start_id;
2517 err = got_repo_match_object_id(&start_id, NULL,
2518 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2519 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2523 s->start_id = start_id;
2524 s->thread_args.start_id = s->start_id;
2526 s->log_branches = !s->log_branches;
2528 err = got_repo_open(&s->thread_args.repo,
2529 got_repo_get_path(s->repo), NULL);
2532 got_ref_list_free(&s->refs);
2533 err = got_ref_list(&s->refs, s->repo, NULL,
2534 got_ref_cmp_by_name, NULL);
2537 err = got_commit_graph_open(&s->thread_args.graph,
2538 s->in_repo_path, !s->log_branches);
2541 err = got_commit_graph_iter_start(s->thread_args.graph,
2542 s->start_id, s->repo, NULL, NULL);
2545 free_commits(&s->commits);
2546 s->first_displayed_entry = NULL;
2547 s->last_displayed_entry = NULL;
2548 s->selected_entry = NULL;
2550 s->thread_args.log_complete = 0;
2552 s->thread_args.commits_needed = view->nlines;
2555 if (view_is_parent_view(view))
2556 begin_x = view_split_begin_x(view->begin_x);
2557 ref_view = view_open(view->nlines, view->ncols,
2558 view->begin_y, begin_x, TOG_VIEW_REF);
2559 if (ref_view == NULL)
2560 return got_error_from_errno("view_open");
2561 err = open_ref_view(ref_view, s->repo);
2563 view_close(ref_view);
2567 ref_view->focussed = 1;
2568 if (view_is_parent_view(view)) {
2569 err = view_close_child(view);
2572 view_set_child(view, ref_view);
2573 view->focus_child = 1;
2575 *new_view = ref_view;
2584 static const struct got_error *
2585 apply_unveil(const char *repo_path, const char *worktree_path)
2587 const struct got_error *error;
2590 if (unveil("gmon.out", "rwc") != 0)
2591 return got_error_from_errno2("unveil", "gmon.out");
2593 if (repo_path && unveil(repo_path, "r") != 0)
2594 return got_error_from_errno2("unveil", repo_path);
2596 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2597 return got_error_from_errno2("unveil", worktree_path);
2599 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2600 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2602 error = got_privsep_unveil_exec_helpers();
2606 if (unveil(NULL, NULL) != 0)
2607 return got_error_from_errno("unveil");
2617 halfdelay(1); /* Do fast refresh while initial view is loading. */
2620 intrflush(stdscr, FALSE);
2621 keypad(stdscr, TRUE);
2623 if (getenv("TOG_COLORS") != NULL) {
2625 use_default_colors();
2627 signal(SIGWINCH, tog_sigwinch);
2628 signal(SIGPIPE, tog_sigpipe);
2629 signal(SIGCONT, tog_sigcont);
2632 static const struct got_error *
2633 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2634 struct got_repository *repo, struct got_worktree *worktree)
2636 const struct got_error *err = NULL;
2639 *in_repo_path = strdup("/");
2640 if (*in_repo_path == NULL)
2641 return got_error_from_errno("strdup");
2646 const char *prefix = got_worktree_get_path_prefix(worktree);
2649 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2652 if (asprintf(in_repo_path, "%s%s%s", prefix,
2653 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2655 err = got_error_from_errno("asprintf");
2656 *in_repo_path = NULL;
2660 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2665 static const struct got_error *
2666 cmd_log(int argc, char *argv[])
2668 const struct got_error *error;
2669 struct got_repository *repo = NULL;
2670 struct got_worktree *worktree = NULL;
2671 struct got_object_id *start_id = NULL;
2672 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2673 char *start_commit = NULL, *label = NULL;
2674 struct got_reference *ref = NULL;
2675 const char *head_ref_name = NULL;
2676 int ch, log_branches = 0;
2677 struct tog_view *view;
2679 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2685 start_commit = optarg;
2688 repo_path = realpath(optarg, NULL);
2689 if (repo_path == NULL)
2690 return got_error_from_errno2("realpath",
2705 cwd = getcwd(NULL, 0);
2707 return got_error_from_errno("getcwd");
2709 error = got_worktree_open(&worktree, cwd);
2710 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2713 if (repo_path == NULL) {
2716 strdup(got_worktree_get_repo_path(worktree));
2718 repo_path = strdup(cwd);
2720 if (repo_path == NULL) {
2721 error = got_error_from_errno("strdup");
2725 error = got_repo_open(&repo, repo_path, NULL);
2729 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2736 error = apply_unveil(got_repo_get_path(repo),
2737 worktree ? got_worktree_get_root_path(worktree) : NULL);
2741 if (start_commit == NULL) {
2742 error = got_repo_match_object_id(&start_id, &label,
2743 worktree ? got_worktree_get_head_ref_name(worktree) :
2744 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
2747 head_ref_name = label;
2749 error = got_ref_open(&ref, repo, start_commit, 0);
2751 head_ref_name = got_ref_get_name(ref);
2752 else if (error->code != GOT_ERR_NOT_REF)
2754 error = got_repo_match_object_id(&start_id, NULL,
2755 start_commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2760 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2762 error = got_error_from_errno("view_open");
2765 error = open_log_view(view, start_id, repo, head_ref_name,
2766 in_repo_path, log_branches);
2770 /* Release work tree lock. */
2771 got_worktree_close(worktree);
2774 error = view_loop(view);
2784 got_repo_close(repo);
2786 got_worktree_close(worktree);
2794 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2795 "[-w] object1 object2\n", getprogname());
2800 match_line(const char *line, regex_t *regex, size_t nmatch,
2801 regmatch_t *regmatch)
2803 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2807 match_color(struct tog_colors *colors, const char *line)
2809 struct tog_color *tc = NULL;
2811 SIMPLEQ_FOREACH(tc, colors, entry) {
2812 if (match_line(line, &tc->regex, 0, NULL))
2819 static const struct got_error *
2820 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2821 WINDOW *window, regmatch_t *regmatch)
2823 const struct got_error *err = NULL;
2830 s = strndup(line, regmatch->rm_so);
2832 return got_error_from_errno("strndup");
2834 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2839 waddwstr(window, wline);
2846 s = strndup(line + regmatch->rm_so,
2847 regmatch->rm_eo - regmatch->rm_so);
2849 err = got_error_from_errno("strndup");
2853 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2858 wattr_on(window, A_STANDOUT, NULL);
2859 waddwstr(window, wline);
2860 wattr_off(window, A_STANDOUT, NULL);
2867 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2868 err = format_line(&wline, &width,
2869 line + regmatch->rm_eo, wlimit, col_tab_align);
2872 waddwstr(window, wline);
2880 static const struct got_error *
2881 draw_file(struct tog_view *view, const char *header)
2883 struct tog_diff_view_state *s = &view->state.diff;
2884 regmatch_t *regmatch = &view->regmatch;
2885 const struct got_error *err;
2888 size_t linesize = 0;
2890 struct tog_color *tc;
2893 int max_lines = view->nlines;
2894 int nlines = s->nlines;
2897 line_offset = s->line_offsets[s->first_displayed_line - 1];
2898 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2899 return got_error_from_errno("fseek");
2901 werase(view->window);
2904 if (asprintf(&line, "[%d/%d] %s",
2905 s->first_displayed_line - 1 + s->selected_line, nlines,
2907 return got_error_from_errno("asprintf");
2908 err = format_line(&wline, &width, line, view->ncols, 0);
2913 if (view_needs_focus_indication(view))
2914 wstandout(view->window);
2915 waddwstr(view->window, wline);
2918 if (view_needs_focus_indication(view))
2919 wstandend(view->window);
2920 if (width <= view->ncols - 1)
2921 waddch(view->window, '\n');
2930 while (max_lines > 0 && nprinted < max_lines) {
2931 linelen = getline(&line, &linesize, s->f);
2932 if (linelen == -1) {
2938 return got_ferror(s->f, GOT_ERR_IO);
2941 tc = match_color(&s->colors, line);
2943 wattr_on(view->window,
2944 COLOR_PAIR(tc->colorpair), NULL);
2945 if (s->first_displayed_line + nprinted == s->matched_line &&
2946 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2947 err = add_matched_line(&width, line, view->ncols, 0,
2948 view->window, regmatch);
2954 err = format_line(&wline, &width, line, view->ncols, 0);
2959 waddwstr(view->window, wline);
2964 wattr_off(view->window,
2965 COLOR_PAIR(tc->colorpair), NULL);
2966 if (width <= view->ncols - 1)
2967 waddch(view->window, '\n');
2972 s->last_displayed_line = s->first_displayed_line +
2975 s->last_displayed_line = s->first_displayed_line;
2980 while (nprinted < view->nlines) {
2981 waddch(view->window, '\n');
2985 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2990 wstandout(view->window);
2991 waddwstr(view->window, wline);
2994 wstandend(view->window);
3001 get_datestr(time_t *time, char *datebuf)
3003 struct tm mytm, *tm;
3006 tm = gmtime_r(time, &mytm);
3009 s = asctime_r(tm, datebuf);
3012 p = strchr(s, '\n');
3018 static const struct got_error *
3019 get_changed_paths(struct got_pathlist_head *paths,
3020 struct got_commit_object *commit, struct got_repository *repo)
3022 const struct got_error *err = NULL;
3023 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3024 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3025 struct got_object_qid *qid;
3027 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3029 struct got_commit_object *pcommit;
3030 err = got_object_open_as_commit(&pcommit, repo,
3035 tree_id1 = got_object_commit_get_tree_id(pcommit);
3036 got_object_commit_close(pcommit);
3041 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3046 tree_id2 = got_object_commit_get_tree_id(commit);
3047 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3051 err = got_diff_tree(tree1, tree2, "", "", repo,
3052 got_diff_tree_collect_changed_paths, paths, 0);
3055 got_object_tree_close(tree1);
3057 got_object_tree_close(tree2);
3061 static const struct got_error *
3062 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3066 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3068 return got_error_from_errno("reallocarray");
3070 (*line_offsets)[*nlines] = off;
3075 static const struct got_error *
3076 write_commit_info(off_t **line_offsets, size_t *nlines,
3077 struct got_object_id *commit_id, struct got_reflist_head *refs,
3078 struct got_repository *repo, FILE *outfile)
3080 const struct got_error *err = NULL;
3081 char datebuf[26], *datestr;
3082 struct got_commit_object *commit;
3083 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3084 time_t committer_time;
3085 const char *author, *committer;
3086 char *refs_str = NULL;
3087 struct got_pathlist_head changed_paths;
3088 struct got_pathlist_entry *pe;
3092 TAILQ_INIT(&changed_paths);
3095 err = build_refs_str(&refs_str, refs, commit_id, repo);
3100 err = got_object_open_as_commit(&commit, repo, commit_id);
3104 err = got_object_id_str(&id_str, commit_id);
3106 err = got_error_from_errno("got_object_id_str");
3110 err = add_line_offset(line_offsets, nlines, 0);
3114 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3115 refs_str ? refs_str : "", refs_str ? ")" : "");
3117 err = got_error_from_errno("fprintf");
3121 err = add_line_offset(line_offsets, nlines, outoff);
3125 n = fprintf(outfile, "from: %s\n",
3126 got_object_commit_get_author(commit));
3128 err = got_error_from_errno("fprintf");
3132 err = add_line_offset(line_offsets, nlines, outoff);
3136 committer_time = got_object_commit_get_committer_time(commit);
3137 datestr = get_datestr(&committer_time, datebuf);
3139 n = fprintf(outfile, "date: %s UTC\n", datestr);
3141 err = got_error_from_errno("fprintf");
3145 err = add_line_offset(line_offsets, nlines, outoff);
3149 author = got_object_commit_get_author(commit);
3150 committer = got_object_commit_get_committer(commit);
3151 if (strcmp(author, committer) != 0) {
3152 n = fprintf(outfile, "via: %s\n", committer);
3154 err = got_error_from_errno("fprintf");
3158 err = add_line_offset(line_offsets, nlines, outoff);
3162 err = got_object_commit_get_logmsg(&logmsg, commit);
3166 while ((line = strsep(&s, "\n")) != NULL) {
3167 n = fprintf(outfile, "%s\n", line);
3169 err = got_error_from_errno("fprintf");
3173 err = add_line_offset(line_offsets, nlines, outoff);
3178 err = get_changed_paths(&changed_paths, commit, repo);
3181 TAILQ_FOREACH(pe, &changed_paths, entry) {
3182 struct got_diff_changed_path *cp = pe->data;
3183 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3185 err = got_error_from_errno("fprintf");
3189 err = add_line_offset(line_offsets, nlines, outoff);
3192 free((char *)pe->path);
3196 fputc('\n', outfile);
3198 err = add_line_offset(line_offsets, nlines, outoff);
3200 got_pathlist_free(&changed_paths);
3204 got_object_commit_close(commit);
3206 free(*line_offsets);
3207 *line_offsets = NULL;
3213 static const struct got_error *
3214 create_diff(struct tog_diff_view_state *s)
3216 const struct got_error *err = NULL;
3220 free(s->line_offsets);
3221 s->line_offsets = malloc(sizeof(off_t));
3222 if (s->line_offsets == NULL)
3223 return got_error_from_errno("malloc");
3228 err = got_error_from_errno("got_opentemp");
3231 if (s->f && fclose(s->f) != 0) {
3232 err = got_error_from_errno("fclose");
3238 err = got_object_get_type(&obj_type, s->repo, s->id1);
3240 err = got_object_get_type(&obj_type, s->repo, s->id2);
3245 case GOT_OBJ_TYPE_BLOB:
3246 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3247 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3248 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3250 case GOT_OBJ_TYPE_TREE:
3251 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3252 s->id1, s->id2, "", "", s->diff_context,
3253 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3255 case GOT_OBJ_TYPE_COMMIT: {
3256 const struct got_object_id_queue *parent_ids;
3257 struct got_object_qid *pid;
3258 struct got_commit_object *commit2;
3260 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3263 /* Show commit info if we're diffing to a parent/root commit. */
3264 if (s->id1 == NULL) {
3265 err = write_commit_info(&s->line_offsets, &s->nlines,
3266 s->id2, &s->refs, s->repo, s->f);
3270 parent_ids = got_object_commit_get_parent_ids(commit2);
3271 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3272 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3273 err = write_commit_info(
3274 &s->line_offsets, &s->nlines,
3275 s->id2, &s->refs, s->repo, s->f);
3282 got_object_commit_close(commit2);
3284 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3285 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3286 s->force_text_diff, s->repo, s->f);
3290 err = got_error(GOT_ERR_OBJ_TYPE);
3296 if (s->f && fflush(s->f) != 0 && err == NULL)
3297 err = got_error_from_errno("fflush");
3302 diff_view_indicate_progress(struct tog_view *view)
3304 mvwaddstr(view->window, 0, 0, "diffing...");
3309 static const struct got_error *
3310 search_start_diff_view(struct tog_view *view)
3312 struct tog_diff_view_state *s = &view->state.diff;
3314 s->matched_line = 0;
3318 static const struct got_error *
3319 search_next_diff_view(struct tog_view *view)
3321 struct tog_diff_view_state *s = &view->state.diff;
3324 size_t linesize = 0;
3327 if (!view->searching) {
3328 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3332 if (s->matched_line) {
3333 if (view->searching == TOG_SEARCH_FORWARD)
3334 lineno = s->matched_line + 1;
3336 lineno = s->matched_line - 1;
3338 if (view->searching == TOG_SEARCH_FORWARD)
3347 if (lineno <= 0 || lineno > s->nlines) {
3348 if (s->matched_line == 0) {
3349 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3353 if (view->searching == TOG_SEARCH_FORWARD)
3359 offset = s->line_offsets[lineno - 1];
3360 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3362 return got_error_from_errno("fseeko");
3364 linelen = getline(&line, &linesize, s->f);
3365 if (linelen != -1 &&
3366 match_line(line, &view->regex, 1, &view->regmatch)) {
3367 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3368 s->matched_line = lineno;
3371 if (view->searching == TOG_SEARCH_FORWARD)
3378 if (s->matched_line) {
3379 s->first_displayed_line = s->matched_line;
3380 s->selected_line = 1;
3386 static const struct got_error *
3387 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3388 struct got_object_id *id2, const char *label1, const char *label2,
3389 int diff_context, int ignore_whitespace, int force_text_diff,
3390 struct tog_view *log_view, struct got_repository *repo)
3392 const struct got_error *err;
3393 struct tog_diff_view_state *s = &view->state.diff;
3395 SIMPLEQ_INIT(&s->refs);
3397 if (id1 != NULL && id2 != NULL) {
3399 err = got_object_get_type(&type1, repo, id1);
3402 err = got_object_get_type(&type2, repo, id2);
3407 return got_error(GOT_ERR_OBJ_TYPE);
3409 s->first_displayed_line = 1;
3410 s->last_displayed_line = view->nlines;
3411 s->selected_line = 1;
3419 s->id1 = got_object_id_dup(id1);
3421 return got_error_from_errno("got_object_id_dup");
3425 s->id2 = got_object_id_dup(id2);
3426 if (s->id2 == NULL) {
3429 return got_error_from_errno("got_object_id_dup");
3432 s->first_displayed_line = 1;
3433 s->last_displayed_line = view->nlines;
3434 s->diff_context = diff_context;
3435 s->ignore_whitespace = ignore_whitespace;
3436 s->force_text_diff = force_text_diff;
3437 s->log_view = log_view;
3440 SIMPLEQ_INIT(&s->colors);
3441 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3442 err = add_color(&s->colors,
3443 "^-", TOG_COLOR_DIFF_MINUS,
3444 get_color_value("TOG_COLOR_DIFF_MINUS"));
3447 err = add_color(&s->colors, "^\\+",
3448 TOG_COLOR_DIFF_PLUS,
3449 get_color_value("TOG_COLOR_DIFF_PLUS"));
3451 free_colors(&s->colors);
3454 err = add_color(&s->colors,
3455 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3456 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3458 free_colors(&s->colors);
3462 err = add_color(&s->colors,
3463 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3464 TOG_COLOR_DIFF_META,
3465 get_color_value("TOG_COLOR_DIFF_META"));
3467 free_colors(&s->colors);
3471 err = add_color(&s->colors,
3472 "^(from|via): ", TOG_COLOR_AUTHOR,
3473 get_color_value("TOG_COLOR_AUTHOR"));
3475 free_colors(&s->colors);
3479 err = add_color(&s->colors,
3480 "^date: ", TOG_COLOR_DATE,
3481 get_color_value("TOG_COLOR_DATE"));
3483 free_colors(&s->colors);
3488 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3494 free_colors(&s->colors);
3498 if (log_view && view_is_splitscreen(view))
3499 show_log_view(log_view); /* draw vborder */
3500 diff_view_indicate_progress(view);
3502 s->line_offsets = NULL;
3504 err = create_diff(s);
3510 free_colors(&s->colors);
3511 got_ref_list_free(&s->refs);
3515 view->show = show_diff_view;
3516 view->input = input_diff_view;
3517 view->close = close_diff_view;
3518 view->search_start = search_start_diff_view;
3519 view->search_next = search_next_diff_view;
3524 static const struct got_error *
3525 close_diff_view(struct tog_view *view)
3527 const struct got_error *err = NULL;
3528 struct tog_diff_view_state *s = &view->state.diff;
3534 if (s->f && fclose(s->f) == EOF)
3535 err = got_error_from_errno("fclose");
3536 free_colors(&s->colors);
3537 free(s->line_offsets);
3538 s->line_offsets = NULL;
3540 got_ref_list_free(&s->refs);
3544 static const struct got_error *
3545 show_diff_view(struct tog_view *view)
3547 const struct got_error *err;
3548 struct tog_diff_view_state *s = &view->state.diff;
3549 char *id_str1 = NULL, *id_str2, *header;
3550 const char *label1, *label2;
3553 err = got_object_id_str(&id_str1, s->id1);
3556 label1 = s->label1 ? : id_str1;
3558 label1 = "/dev/null";
3560 err = got_object_id_str(&id_str2, s->id2);
3563 label2 = s->label2 ? : id_str2;
3565 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3566 err = got_error_from_errno("asprintf");
3574 return draw_file(view, header);
3577 static const struct got_error *
3578 set_selected_commit(struct tog_diff_view_state *s,
3579 struct commit_queue_entry *entry)
3581 const struct got_error *err;
3582 const struct got_object_id_queue *parent_ids;
3583 struct got_commit_object *selected_commit;
3584 struct got_object_qid *pid;
3587 s->id2 = got_object_id_dup(entry->id);
3589 return got_error_from_errno("got_object_id_dup");
3591 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3594 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3596 pid = SIMPLEQ_FIRST(parent_ids);
3597 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3598 got_object_commit_close(selected_commit);
3602 static const struct got_error *
3603 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3605 const struct got_error *err = NULL;
3606 struct tog_diff_view_state *s = &view->state.diff;
3607 struct tog_log_view_state *ls;
3608 struct commit_queue_entry *old_selected_entry;
3610 size_t linesize = 0;
3618 s->force_text_diff = !s->force_text_diff;
3620 s->ignore_whitespace = !s->ignore_whitespace;
3621 wclear(view->window);
3622 s->first_displayed_line = 1;
3623 s->last_displayed_line = view->nlines;
3624 diff_view_indicate_progress(view);
3625 err = create_diff(s);
3629 if (s->first_displayed_line > 1)
3630 s->first_displayed_line--;
3634 if (s->first_displayed_line == 1)
3637 while (i++ < view->nlines - 1 &&
3638 s->first_displayed_line > 1)
3639 s->first_displayed_line--;
3644 s->first_displayed_line++;
3652 while (!s->eof && i++ < view->nlines - 1) {
3653 linelen = getline(&line, &linesize, s->f);
3654 s->first_displayed_line++;
3655 if (linelen == -1) {
3659 err = got_ferror(s->f, GOT_ERR_IO);
3666 if (s->diff_context > 0) {
3668 diff_view_indicate_progress(view);
3669 err = create_diff(s);
3670 if (s->first_displayed_line + view->nlines - 1 >
3672 s->first_displayed_line = 1;
3673 s->last_displayed_line = view->nlines;
3678 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3680 diff_view_indicate_progress(view);
3681 err = create_diff(s);
3686 if (s->log_view == NULL)
3688 ls = &s->log_view->state.log;
3689 old_selected_entry = ls->selected_entry;
3691 err = input_log_view(NULL, s->log_view, KEY_UP);
3695 if (old_selected_entry == ls->selected_entry)
3698 err = set_selected_commit(s, ls->selected_entry);
3702 s->first_displayed_line = 1;
3703 s->last_displayed_line = view->nlines;
3705 diff_view_indicate_progress(view);
3706 err = create_diff(s);
3710 if (s->log_view == NULL)
3712 ls = &s->log_view->state.log;
3713 old_selected_entry = ls->selected_entry;
3715 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3719 if (old_selected_entry == ls->selected_entry)
3722 err = set_selected_commit(s, ls->selected_entry);
3726 s->first_displayed_line = 1;
3727 s->last_displayed_line = view->nlines;
3729 diff_view_indicate_progress(view);
3730 err = create_diff(s);
3739 static const struct got_error *
3740 cmd_diff(int argc, char *argv[])
3742 const struct got_error *error = NULL;
3743 struct got_repository *repo = NULL;
3744 struct got_worktree *worktree = NULL;
3745 struct got_object_id *id1 = NULL, *id2 = NULL;
3746 char *repo_path = NULL, *cwd = NULL;
3747 char *id_str1 = NULL, *id_str2 = NULL;
3748 char *label1 = NULL, *label2 = NULL;
3749 int diff_context = 3, ignore_whitespace = 0;
3750 int ch, force_text_diff = 0;
3752 struct tog_view *view;
3754 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3757 force_text_diff = 1;
3760 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3763 err(1, "-C option %s", errstr);
3766 repo_path = realpath(optarg, NULL);
3767 if (repo_path == NULL)
3768 return got_error_from_errno2("realpath",
3770 got_path_strip_trailing_slashes(repo_path);
3773 ignore_whitespace = 1;
3785 usage_diff(); /* TODO show local worktree changes */
3786 } else if (argc == 2) {
3792 cwd = getcwd(NULL, 0);
3794 return got_error_from_errno("getcwd");
3796 error = got_worktree_open(&worktree, cwd);
3797 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3800 if (repo_path == NULL) {
3803 strdup(got_worktree_get_repo_path(worktree));
3805 repo_path = strdup(cwd);
3807 if (repo_path == NULL) {
3808 error = got_error_from_errno("strdup");
3812 error = got_repo_open(&repo, repo_path, NULL);
3818 error = apply_unveil(got_repo_get_path(repo), NULL);
3822 error = got_repo_match_object_id(&id1, &label1, id_str1,
3823 GOT_OBJ_TYPE_ANY, 1, repo);
3827 error = got_repo_match_object_id(&id2, &label2, id_str2,
3828 GOT_OBJ_TYPE_ANY, 1, repo);
3832 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3834 error = got_error_from_errno("view_open");
3837 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3838 ignore_whitespace, force_text_diff, NULL, repo);
3841 error = view_loop(view);
3848 got_repo_close(repo);
3850 got_worktree_close(worktree);
3858 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3863 struct tog_blame_line {
3865 struct got_object_id *id;
3868 static const struct got_error *
3869 draw_blame(struct tog_view *view)
3871 struct tog_blame_view_state *s = &view->state.blame;
3872 struct tog_blame *blame = &s->blame;
3873 regmatch_t *regmatch = &view->regmatch;
3874 const struct got_error *err;
3875 int lineno = 0, nprinted = 0;
3877 size_t linesize = 0;
3881 struct tog_blame_line *blame_line;
3882 struct got_object_id *prev_id = NULL;
3884 struct tog_color *tc;
3886 err = got_object_id_str(&id_str, s->blamed_commit->id);
3891 werase(view->window);
3893 if (asprintf(&line, "commit %s", id_str) == -1) {
3894 err = got_error_from_errno("asprintf");
3899 err = format_line(&wline, &width, line, view->ncols, 0);
3904 if (view_needs_focus_indication(view))
3905 wstandout(view->window);
3906 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3908 wattr_on(view->window,
3909 COLOR_PAIR(tc->colorpair), NULL);
3910 waddwstr(view->window, wline);
3912 wattr_off(view->window,
3913 COLOR_PAIR(tc->colorpair), NULL);
3914 if (view_needs_focus_indication(view))
3915 wstandend(view->window);
3918 if (width < view->ncols - 1)
3919 waddch(view->window, '\n');
3921 if (asprintf(&line, "[%d/%d] %s%s",
3922 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3923 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3925 return got_error_from_errno("asprintf");
3928 err = format_line(&wline, &width, line, view->ncols, 0);
3933 waddwstr(view->window, wline);
3936 if (width < view->ncols - 1)
3937 waddch(view->window, '\n');
3940 while (nprinted < view->nlines - 2) {
3941 linelen = getline(&line, &linesize, blame->f);
3942 if (linelen == -1) {
3943 if (feof(blame->f)) {
3948 return got_ferror(blame->f, GOT_ERR_IO);
3950 if (++lineno < s->first_displayed_line)
3953 if (view->focussed && nprinted == s->selected_line - 1)
3954 wstandout(view->window);
3956 if (blame->nlines > 0) {
3957 blame_line = &blame->lines[lineno - 1];
3958 if (blame_line->annotated && prev_id &&
3959 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3961 nprinted == s->selected_line - 1)) {
3962 waddstr(view->window, " ");
3963 } else if (blame_line->annotated) {
3965 err = got_object_id_str(&id_str, blame_line->id);
3970 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3972 wattr_on(view->window,
3973 COLOR_PAIR(tc->colorpair), NULL);
3974 wprintw(view->window, "%.8s", id_str);
3976 wattr_off(view->window,
3977 COLOR_PAIR(tc->colorpair), NULL);
3979 prev_id = blame_line->id;
3981 waddstr(view->window, "........");
3985 waddstr(view->window, "........");
3989 if (view->focussed && nprinted == s->selected_line - 1)
3990 wstandend(view->window);
3991 waddstr(view->window, " ");
3993 if (view->ncols <= 9) {
3995 wline = wcsdup(L"");
3996 if (wline == NULL) {
3997 err = got_error_from_errno("wcsdup");
4001 } else if (s->first_displayed_line + nprinted ==
4003 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4004 err = add_matched_line(&width, line, view->ncols - 9, 9,
4005 view->window, regmatch);
4012 err = format_line(&wline, &width, line,
4013 view->ncols - 9, 9);
4014 waddwstr(view->window, wline);
4020 if (width <= view->ncols - 1)
4021 waddch(view->window, '\n');
4022 if (++nprinted == 1)
4023 s->first_displayed_line = lineno;
4026 s->last_displayed_line = lineno;
4033 static const struct got_error *
4034 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4036 const struct got_error *err = NULL;
4037 struct tog_blame_cb_args *a = arg;
4038 struct tog_blame_line *line;
4041 if (nlines != a->nlines ||
4042 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4043 return got_error(GOT_ERR_RANGE);
4045 errcode = pthread_mutex_lock(&tog_mutex);
4047 return got_error_set_errno(errcode, "pthread_mutex_lock");
4049 if (*a->quit) { /* user has quit the blame view */
4050 err = got_error(GOT_ERR_ITER_COMPLETED);
4055 goto done; /* no change in this commit */
4057 line = &a->lines[lineno - 1];
4058 if (line->annotated)
4061 line->id = got_object_id_dup(id);
4062 if (line->id == NULL) {
4063 err = got_error_from_errno("got_object_id_dup");
4066 line->annotated = 1;
4068 errcode = pthread_mutex_unlock(&tog_mutex);
4070 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4075 blame_thread(void *arg)
4077 const struct got_error *err;
4078 struct tog_blame_thread_args *ta = arg;
4079 struct tog_blame_cb_args *a = ta->cb_args;
4082 err = block_signals_used_by_main_thread();
4086 err = got_blame(ta->path, a->commit_id, ta->repo,
4087 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4088 if (err && err->code == GOT_ERR_CANCELLED)
4091 errcode = pthread_mutex_lock(&tog_mutex);
4093 return (void *)got_error_set_errno(errcode,
4094 "pthread_mutex_lock");
4096 got_repo_close(ta->repo);
4100 errcode = pthread_mutex_unlock(&tog_mutex);
4101 if (errcode && err == NULL)
4102 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4107 static struct got_object_id *
4108 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4109 int first_displayed_line, int selected_line)
4111 struct tog_blame_line *line;
4116 line = &lines[first_displayed_line - 1 + selected_line - 1];
4117 if (!line->annotated)
4123 static const struct got_error *
4124 stop_blame(struct tog_blame *blame)
4126 const struct got_error *err = NULL;
4129 if (blame->thread) {
4131 errcode = pthread_mutex_unlock(&tog_mutex);
4133 return got_error_set_errno(errcode,
4134 "pthread_mutex_unlock");
4135 errcode = pthread_join(blame->thread, (void **)&err);
4137 return got_error_set_errno(errcode, "pthread_join");
4138 errcode = pthread_mutex_lock(&tog_mutex);
4140 return got_error_set_errno(errcode,
4141 "pthread_mutex_lock");
4142 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4144 blame->thread = NULL;
4146 if (blame->thread_args.repo) {
4147 got_repo_close(blame->thread_args.repo);
4148 blame->thread_args.repo = NULL;
4151 if (fclose(blame->f) != 0 && err == NULL)
4152 err = got_error_from_errno("fclose");
4156 for (i = 0; i < blame->nlines; i++)
4157 free(blame->lines[i].id);
4159 blame->lines = NULL;
4161 free(blame->cb_args.commit_id);
4162 blame->cb_args.commit_id = NULL;
4167 static const struct got_error *
4168 cancel_blame_view(void *arg)
4170 const struct got_error *err = NULL;
4174 errcode = pthread_mutex_lock(&tog_mutex);
4176 return got_error_set_errno(errcode,
4177 "pthread_mutex_unlock");
4180 err = got_error(GOT_ERR_CANCELLED);
4182 errcode = pthread_mutex_unlock(&tog_mutex);
4184 return got_error_set_errno(errcode,
4185 "pthread_mutex_lock");
4190 static const struct got_error *
4191 run_blame(struct tog_view *view)
4193 struct tog_blame_view_state *s = &view->state.blame;
4194 struct tog_blame *blame = &s->blame;
4195 const struct got_error *err = NULL;
4196 struct got_blob_object *blob = NULL;
4197 struct got_repository *thread_repo = NULL;
4198 struct got_object_id *obj_id = NULL;
4201 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4206 err = got_object_get_type(&obj_type, s->repo, obj_id);
4210 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4211 err = got_error(GOT_ERR_OBJ_TYPE);
4215 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4218 blame->f = got_opentemp();
4219 if (blame->f == NULL) {
4220 err = got_error_from_errno("got_opentemp");
4223 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4224 &blame->line_offsets, blame->f, blob);
4225 if (err || blame->nlines == 0)
4228 /* Don't include \n at EOF in the blame line count. */
4229 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4232 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4233 if (blame->lines == NULL) {
4234 err = got_error_from_errno("calloc");
4238 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4242 blame->cb_args.view = view;
4243 blame->cb_args.lines = blame->lines;
4244 blame->cb_args.nlines = blame->nlines;
4245 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4246 if (blame->cb_args.commit_id == NULL) {
4247 err = got_error_from_errno("got_object_id_dup");
4250 blame->cb_args.quit = &s->done;
4252 blame->thread_args.path = s->path;
4253 blame->thread_args.repo = thread_repo;
4254 blame->thread_args.cb_args = &blame->cb_args;
4255 blame->thread_args.complete = &s->blame_complete;
4256 blame->thread_args.cancel_cb = cancel_blame_view;
4257 blame->thread_args.cancel_arg = &s->done;
4258 s->blame_complete = 0;
4260 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4261 s->first_displayed_line = 1;
4262 s->last_displayed_line = view->nlines;
4263 s->selected_line = 1;
4268 got_object_blob_close(blob);
4275 static const struct got_error *
4276 open_blame_view(struct tog_view *view, char *path,
4277 struct got_object_id *commit_id, struct got_repository *repo)
4279 const struct got_error *err = NULL;
4280 struct tog_blame_view_state *s = &view->state.blame;
4282 SIMPLEQ_INIT(&s->blamed_commits);
4284 s->path = strdup(path);
4285 if (s->path == NULL)
4286 return got_error_from_errno("strdup");
4288 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4294 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4295 s->first_displayed_line = 1;
4296 s->last_displayed_line = view->nlines;
4297 s->selected_line = 1;
4298 s->blame_complete = 0;
4300 s->commit_id = commit_id;
4301 memset(&s->blame, 0, sizeof(s->blame));
4303 SIMPLEQ_INIT(&s->colors);
4304 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4305 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4306 get_color_value("TOG_COLOR_COMMIT"));
4311 view->show = show_blame_view;
4312 view->input = input_blame_view;
4313 view->close = close_blame_view;
4314 view->search_start = search_start_blame_view;
4315 view->search_next = search_next_blame_view;
4317 return run_blame(view);
4320 static const struct got_error *
4321 close_blame_view(struct tog_view *view)
4323 const struct got_error *err = NULL;
4324 struct tog_blame_view_state *s = &view->state.blame;
4326 if (s->blame.thread)
4327 err = stop_blame(&s->blame);
4329 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4330 struct got_object_qid *blamed_commit;
4331 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4332 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4333 got_object_qid_free(blamed_commit);
4337 free_colors(&s->colors);
4342 static const struct got_error *
4343 search_start_blame_view(struct tog_view *view)
4345 struct tog_blame_view_state *s = &view->state.blame;
4347 s->matched_line = 0;
4351 static const struct got_error *
4352 search_next_blame_view(struct tog_view *view)
4354 struct tog_blame_view_state *s = &view->state.blame;
4357 size_t linesize = 0;
4360 if (!view->searching) {
4361 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4365 if (s->matched_line) {
4366 if (view->searching == TOG_SEARCH_FORWARD)
4367 lineno = s->matched_line + 1;
4369 lineno = s->matched_line - 1;
4371 if (view->searching == TOG_SEARCH_FORWARD)
4374 lineno = s->blame.nlines;
4380 if (lineno <= 0 || lineno > s->blame.nlines) {
4381 if (s->matched_line == 0) {
4382 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4386 if (view->searching == TOG_SEARCH_FORWARD)
4389 lineno = s->blame.nlines;
4392 offset = s->blame.line_offsets[lineno - 1];
4393 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4395 return got_error_from_errno("fseeko");
4397 linelen = getline(&line, &linesize, s->blame.f);
4398 if (linelen != -1 &&
4399 match_line(line, &view->regex, 1, &view->regmatch)) {
4400 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4401 s->matched_line = lineno;
4404 if (view->searching == TOG_SEARCH_FORWARD)
4411 if (s->matched_line) {
4412 s->first_displayed_line = s->matched_line;
4413 s->selected_line = 1;
4419 static const struct got_error *
4420 show_blame_view(struct tog_view *view)
4422 const struct got_error *err = NULL;
4423 struct tog_blame_view_state *s = &view->state.blame;
4426 if (s->blame.thread == NULL) {
4427 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4428 &s->blame.thread_args);
4430 return got_error_set_errno(errcode, "pthread_create");
4432 halfdelay(1); /* fast refresh while annotating */
4435 if (s->blame_complete)
4436 halfdelay(10); /* disable fast refresh */
4438 err = draw_blame(view);
4444 static const struct got_error *
4445 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4447 const struct got_error *err = NULL, *thread_err = NULL;
4448 struct tog_view *diff_view;
4449 struct tog_blame_view_state *s = &view->state.blame;
4458 if (s->selected_line > 1)
4460 else if (s->selected_line == 1 &&
4461 s->first_displayed_line > 1)
4462 s->first_displayed_line--;
4466 if (s->first_displayed_line == 1) {
4467 s->selected_line = 1;
4470 if (s->first_displayed_line > view->nlines - 2)
4471 s->first_displayed_line -=
4474 s->first_displayed_line = 1;
4478 if (s->selected_line < view->nlines - 2 &&
4479 s->first_displayed_line +
4480 s->selected_line <= s->blame.nlines)
4482 else if (s->last_displayed_line <
4484 s->first_displayed_line++;
4488 struct got_object_id *id = NULL;
4489 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4490 s->first_displayed_line, s->selected_line);
4494 struct got_commit_object *commit;
4495 struct got_object_qid *pid;
4496 struct got_object_id *blob_id = NULL;
4498 err = got_object_open_as_commit(&commit,
4502 pid = SIMPLEQ_FIRST(
4503 got_object_commit_get_parent_ids(commit));
4505 got_object_commit_close(commit);
4508 /* Check if path history ends here. */
4509 err = got_object_id_by_path(&blob_id, s->repo,
4512 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4514 got_object_commit_close(commit);
4517 err = got_object_get_type(&obj_type, s->repo,
4520 /* Can't blame non-blob type objects. */
4521 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4522 got_object_commit_close(commit);
4525 err = got_object_qid_alloc(&s->blamed_commit,
4527 got_object_commit_close(commit);
4529 if (got_object_id_cmp(id,
4530 s->blamed_commit->id) == 0)
4532 err = got_object_qid_alloc(&s->blamed_commit,
4538 thread_err = stop_blame(&s->blame);
4542 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4543 s->blamed_commit, entry);
4544 err = run_blame(view);
4550 struct got_object_qid *first;
4551 first = SIMPLEQ_FIRST(&s->blamed_commits);
4552 if (!got_object_id_cmp(first->id, s->commit_id))
4555 thread_err = stop_blame(&s->blame);
4559 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4560 got_object_qid_free(s->blamed_commit);
4562 SIMPLEQ_FIRST(&s->blamed_commits);
4563 err = run_blame(view);
4570 struct got_object_id *id = NULL;
4571 struct got_object_qid *pid;
4572 struct got_commit_object *commit = NULL;
4573 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4574 s->first_displayed_line, s->selected_line);
4577 err = got_object_open_as_commit(&commit, s->repo, id);
4580 pid = SIMPLEQ_FIRST(
4581 got_object_commit_get_parent_ids(commit));
4582 if (view_is_parent_view(view))
4583 begin_x = view_split_begin_x(view->begin_x);
4584 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4585 if (diff_view == NULL) {
4586 got_object_commit_close(commit);
4587 err = got_error_from_errno("view_open");
4590 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4591 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4592 got_object_commit_close(commit);
4594 view_close(diff_view);
4598 diff_view->focussed = 1;
4599 if (view_is_parent_view(view)) {
4600 err = view_close_child(view);
4603 view_set_child(view, diff_view);
4604 view->focus_child = 1;
4606 *new_view = diff_view;
4614 if (s->last_displayed_line >= s->blame.nlines &&
4615 s->selected_line >= MIN(s->blame.nlines,
4616 view->nlines - 2)) {
4619 if (s->last_displayed_line >= s->blame.nlines &&
4620 s->selected_line < view->nlines - 2) {
4621 s->selected_line = MIN(s->blame.nlines,
4625 if (s->last_displayed_line + view->nlines - 2
4627 s->first_displayed_line +=
4630 s->first_displayed_line =
4635 if (s->selected_line > view->nlines - 2) {
4636 s->selected_line = MIN(s->blame.nlines,
4643 return thread_err ? thread_err : err;
4646 static const struct got_error *
4647 cmd_blame(int argc, char *argv[])
4649 const struct got_error *error;
4650 struct got_repository *repo = NULL;
4651 struct got_worktree *worktree = NULL;
4652 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4653 char *link_target = NULL;
4654 struct got_object_id *commit_id = NULL;
4655 char *commit_id_str = NULL;
4657 struct tog_view *view;
4659 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4662 commit_id_str = optarg;
4665 repo_path = realpath(optarg, NULL);
4666 if (repo_path == NULL)
4667 return got_error_from_errno2("realpath",
4682 cwd = getcwd(NULL, 0);
4684 return got_error_from_errno("getcwd");
4686 error = got_worktree_open(&worktree, cwd);
4687 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4690 if (repo_path == NULL) {
4693 strdup(got_worktree_get_repo_path(worktree));
4695 repo_path = strdup(cwd);
4697 if (repo_path == NULL) {
4698 error = got_error_from_errno("strdup");
4702 error = got_repo_open(&repo, repo_path, NULL);
4706 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4713 error = apply_unveil(got_repo_get_path(repo), NULL);
4717 if (commit_id_str == NULL) {
4718 struct got_reference *head_ref;
4719 error = got_ref_open(&head_ref, repo, worktree ?
4720 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4723 error = got_ref_resolve(&commit_id, repo, head_ref);
4724 got_ref_close(head_ref);
4726 error = got_repo_match_object_id(&commit_id, NULL,
4727 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4732 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4734 error = got_error_from_errno("view_open");
4738 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4743 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4748 /* Release work tree lock. */
4749 got_worktree_close(worktree);
4752 error = view_loop(view);
4760 got_worktree_close(worktree);
4762 got_repo_close(repo);
4766 static const struct got_error *
4767 draw_tree_entries(struct tog_view *view, const char *parent_path)
4769 struct tog_tree_view_state *s = &view->state.tree;
4770 const struct got_error *err = NULL;
4771 struct got_tree_entry *te;
4773 struct tog_color *tc;
4774 int width, n, i, nentries;
4775 int limit = view->nlines;
4779 werase(view->window);
4784 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4787 if (view_needs_focus_indication(view))
4788 wstandout(view->window);
4789 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4791 wattr_on(view->window,
4792 COLOR_PAIR(tc->colorpair), NULL);
4793 waddwstr(view->window, wline);
4795 wattr_off(view->window,
4796 COLOR_PAIR(tc->colorpair), NULL);
4797 if (view_needs_focus_indication(view))
4798 wstandend(view->window);
4801 if (width < view->ncols - 1)
4802 waddch(view->window, '\n');
4805 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4808 waddwstr(view->window, wline);
4811 if (width < view->ncols - 1)
4812 waddch(view->window, '\n');
4815 waddch(view->window, '\n');
4819 if (s->first_displayed_entry == NULL) {
4820 te = got_object_tree_get_first_entry(s->tree);
4821 if (s->selected == 0) {
4823 wstandout(view->window);
4824 s->selected_entry = NULL;
4826 waddstr(view->window, " ..\n"); /* parent directory */
4827 if (s->selected == 0 && view->focussed)
4828 wstandend(view->window);
4835 te = s->first_displayed_entry;
4838 nentries = got_object_tree_get_nentries(s->tree);
4839 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4840 char *line = NULL, *id_str = NULL, *link_target = NULL;
4841 const char *modestr = "";
4844 te = got_object_tree_get_entry(s->tree, i);
4845 mode = got_tree_entry_get_mode(te);
4848 err = got_object_id_str(&id_str,
4849 got_tree_entry_get_id(te));
4851 return got_error_from_errno(
4852 "got_object_id_str");
4854 if (got_object_tree_entry_is_submodule(te))
4856 else if (S_ISLNK(mode)) {
4859 err = got_tree_entry_get_symlink_target(&link_target,
4865 for (i = 0; i < strlen(link_target); i++) {
4866 if (!isprint((unsigned char)link_target[i]))
4867 link_target[i] = '?';
4871 else if (S_ISDIR(mode))
4873 else if (mode & S_IXUSR)
4875 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4876 got_tree_entry_get_name(te), modestr,
4877 link_target ? " -> ": "",
4878 link_target ? link_target : "") == -1) {
4881 return got_error_from_errno("asprintf");
4885 err = format_line(&wline, &width, line, view->ncols, 0);
4890 if (n == s->selected) {
4892 wstandout(view->window);
4893 s->selected_entry = te;
4895 tc = match_color(&s->colors, line);
4897 wattr_on(view->window,
4898 COLOR_PAIR(tc->colorpair), NULL);
4899 waddwstr(view->window, wline);
4901 wattr_off(view->window,
4902 COLOR_PAIR(tc->colorpair), NULL);
4903 if (width < view->ncols - 1)
4904 waddch(view->window, '\n');
4905 if (n == s->selected && view->focussed)
4906 wstandend(view->window);
4912 s->last_displayed_entry = te;
4921 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4923 struct got_tree_entry *te;
4924 int isroot = s->tree == s->root;
4927 if (s->first_displayed_entry == NULL)
4930 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4931 while (i++ < maxscroll) {
4934 s->first_displayed_entry = NULL;
4937 s->first_displayed_entry = te;
4938 te = got_tree_entry_get_prev(s->tree, te);
4943 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4945 struct got_tree_entry *next, *last;
4948 if (s->first_displayed_entry)
4949 next = got_tree_entry_get_next(s->tree,
4950 s->first_displayed_entry);
4952 next = got_object_tree_get_first_entry(s->tree);
4954 last = s->last_displayed_entry;
4955 while (next && last && n++ < maxscroll) {
4956 last = got_tree_entry_get_next(s->tree, last);
4958 s->first_displayed_entry = next;
4959 next = got_tree_entry_get_next(s->tree, next);
4964 static const struct got_error *
4965 tree_entry_path(char **path, struct tog_parent_trees *parents,
4966 struct got_tree_entry *te)
4968 const struct got_error *err = NULL;
4969 struct tog_parent_tree *pt;
4970 size_t len = 2; /* for leading slash and NUL */
4972 TAILQ_FOREACH(pt, parents, entry)
4973 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4976 len += strlen(got_tree_entry_get_name(te));
4978 *path = calloc(1, len);
4980 return got_error_from_errno("calloc");
4983 pt = TAILQ_LAST(parents, tog_parent_trees);
4985 const char *name = got_tree_entry_get_name(pt->selected_entry);
4986 if (strlcat(*path, name, len) >= len) {
4987 err = got_error(GOT_ERR_NO_SPACE);
4990 if (strlcat(*path, "/", len) >= len) {
4991 err = got_error(GOT_ERR_NO_SPACE);
4994 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4997 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4998 err = got_error(GOT_ERR_NO_SPACE);
5010 static const struct got_error *
5011 blame_tree_entry(struct tog_view **new_view, int begin_x,
5012 struct got_tree_entry *te, struct tog_parent_trees *parents,
5013 struct got_object_id *commit_id, struct got_repository *repo)
5015 const struct got_error *err = NULL;
5017 struct tog_view *blame_view;
5021 err = tree_entry_path(&path, parents, te);
5025 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5026 if (blame_view == NULL) {
5027 err = got_error_from_errno("view_open");
5031 err = open_blame_view(blame_view, path, commit_id, repo);
5033 if (err->code == GOT_ERR_CANCELLED)
5035 view_close(blame_view);
5037 *new_view = blame_view;
5043 static const struct got_error *
5044 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5045 struct tog_tree_view_state *s)
5047 struct tog_view *log_view;
5048 const struct got_error *err = NULL;
5053 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5054 if (log_view == NULL)
5055 return got_error_from_errno("view_open");
5057 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5061 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5064 view_close(log_view);
5066 *new_view = log_view;
5071 static const struct got_error *
5072 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5073 struct got_object_id *commit_id, const char *head_ref_name,
5074 struct got_repository *repo)
5076 const struct got_error *err = NULL;
5077 char *commit_id_str = NULL;
5078 struct tog_tree_view_state *s = &view->state.tree;
5080 TAILQ_INIT(&s->parents);
5082 err = got_object_id_str(&commit_id_str, commit_id);
5086 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5087 err = got_error_from_errno("asprintf");
5091 s->root = s->tree = root;
5092 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5093 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5094 s->commit_id = got_object_id_dup(commit_id);
5095 if (s->commit_id == NULL) {
5096 err = got_error_from_errno("got_object_id_dup");
5099 if (head_ref_name) {
5100 s->head_ref_name = strdup(head_ref_name);
5101 if (s->head_ref_name == NULL) {
5102 err = got_error_from_errno("strdup");
5108 SIMPLEQ_INIT(&s->colors);
5110 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5111 err = add_color(&s->colors, "\\$$",
5112 TOG_COLOR_TREE_SUBMODULE,
5113 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5116 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5117 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5119 free_colors(&s->colors);
5122 err = add_color(&s->colors, "/$",
5123 TOG_COLOR_TREE_DIRECTORY,
5124 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5126 free_colors(&s->colors);
5130 err = add_color(&s->colors, "\\*$",
5131 TOG_COLOR_TREE_EXECUTABLE,
5132 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5134 free_colors(&s->colors);
5138 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5139 get_color_value("TOG_COLOR_COMMIT"));
5141 free_colors(&s->colors);
5146 view->show = show_tree_view;
5147 view->input = input_tree_view;
5148 view->close = close_tree_view;
5149 view->search_start = search_start_tree_view;
5150 view->search_next = search_next_tree_view;
5152 free(commit_id_str);
5154 free(s->tree_label);
5155 s->tree_label = NULL;
5160 static const struct got_error *
5161 close_tree_view(struct tog_view *view)
5163 struct tog_tree_view_state *s = &view->state.tree;
5165 free_colors(&s->colors);
5166 free(s->tree_label);
5167 s->tree_label = NULL;
5169 s->commit_id = NULL;
5170 free(s->head_ref_name);
5171 s->head_ref_name = NULL;
5172 while (!TAILQ_EMPTY(&s->parents)) {
5173 struct tog_parent_tree *parent;
5174 parent = TAILQ_FIRST(&s->parents);
5175 TAILQ_REMOVE(&s->parents, parent, entry);
5179 if (s->tree != s->root)
5180 got_object_tree_close(s->tree);
5181 got_object_tree_close(s->root);
5185 static const struct got_error *
5186 search_start_tree_view(struct tog_view *view)
5188 struct tog_tree_view_state *s = &view->state.tree;
5190 s->matched_entry = NULL;
5195 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5197 regmatch_t regmatch;
5199 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5203 static const struct got_error *
5204 search_next_tree_view(struct tog_view *view)
5206 struct tog_tree_view_state *s = &view->state.tree;
5207 struct got_tree_entry *te = NULL;
5209 if (!view->searching) {
5210 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5214 if (s->matched_entry) {
5215 if (view->searching == TOG_SEARCH_FORWARD) {
5216 if (s->selected_entry)
5217 te = got_tree_entry_get_next(s->tree,
5220 te = got_object_tree_get_first_entry(s->tree);
5222 if (s->selected_entry == NULL)
5223 te = got_object_tree_get_last_entry(s->tree);
5225 te = got_tree_entry_get_prev(s->tree,
5229 if (view->searching == TOG_SEARCH_FORWARD)
5230 te = got_object_tree_get_first_entry(s->tree);
5232 te = got_object_tree_get_last_entry(s->tree);
5237 if (s->matched_entry == NULL) {
5238 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5241 if (view->searching == TOG_SEARCH_FORWARD)
5242 te = got_object_tree_get_first_entry(s->tree);
5244 te = got_object_tree_get_last_entry(s->tree);
5247 if (match_tree_entry(te, &view->regex)) {
5248 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5249 s->matched_entry = te;
5253 if (view->searching == TOG_SEARCH_FORWARD)
5254 te = got_tree_entry_get_next(s->tree, te);
5256 te = got_tree_entry_get_prev(s->tree, te);
5259 if (s->matched_entry) {
5260 s->first_displayed_entry = s->matched_entry;
5267 static const struct got_error *
5268 show_tree_view(struct tog_view *view)
5270 const struct got_error *err = NULL;
5271 struct tog_tree_view_state *s = &view->state.tree;
5274 err = tree_entry_path(&parent_path, &s->parents, NULL);
5278 err = draw_tree_entries(view, parent_path);
5285 static const struct got_error *
5286 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5288 const struct got_error *err = NULL;
5289 struct tog_tree_view_state *s = &view->state.tree;
5290 struct tog_view *log_view, *ref_view;
5295 s->show_ids = !s->show_ids;
5298 if (!s->selected_entry)
5300 if (view_is_parent_view(view))
5301 begin_x = view_split_begin_x(view->begin_x);
5302 err = log_selected_tree_entry(&log_view, begin_x, s);
5304 log_view->focussed = 1;
5305 if (view_is_parent_view(view)) {
5306 err = view_close_child(view);
5309 view_set_child(view, log_view);
5310 view->focus_child = 1;
5312 *new_view = log_view;
5315 if (view_is_parent_view(view))
5316 begin_x = view_split_begin_x(view->begin_x);
5317 ref_view = view_open(view->nlines, view->ncols,
5318 view->begin_y, begin_x, TOG_VIEW_REF);
5319 if (ref_view == NULL)
5320 return got_error_from_errno("view_open");
5321 err = open_ref_view(ref_view, s->repo);
5323 view_close(ref_view);
5327 ref_view->focussed = 1;
5328 if (view_is_parent_view(view)) {
5329 err = view_close_child(view);
5332 view_set_child(view, ref_view);
5333 view->focus_child = 1;
5335 *new_view = ref_view;
5339 if (s->selected > 0) {
5343 tree_scroll_up(s, 1);
5347 if (s->tree == s->root) {
5348 if (got_object_tree_get_first_entry(s->tree) ==
5349 s->first_displayed_entry)
5352 if (s->first_displayed_entry == NULL)
5355 tree_scroll_up(s, MAX(0, view->nlines - 3));
5359 if (s->selected < s->ndisplayed - 1) {
5363 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5365 /* can't scroll any further */
5367 tree_scroll_down(s, 1);
5371 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5373 /* can't scroll any further; move cursor down */
5374 if (s->selected < s->ndisplayed - 1)
5375 s->selected = s->ndisplayed - 1;
5378 tree_scroll_down(s, view->nlines - 3);
5383 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5384 struct tog_parent_tree *parent;
5385 /* user selected '..' */
5386 if (s->tree == s->root)
5388 parent = TAILQ_FIRST(&s->parents);
5389 TAILQ_REMOVE(&s->parents, parent,
5391 got_object_tree_close(s->tree);
5392 s->tree = parent->tree;
5393 s->first_displayed_entry =
5394 parent->first_displayed_entry;
5396 parent->selected_entry;
5397 s->selected = parent->selected;
5399 } else if (S_ISDIR(got_tree_entry_get_mode(
5400 s->selected_entry))) {
5401 struct got_tree_object *subtree;
5402 err = got_object_open_as_tree(&subtree, s->repo,
5403 got_tree_entry_get_id(s->selected_entry));
5406 err = tree_view_visit_subtree(s, subtree);
5408 got_object_tree_close(subtree);
5411 } else if (S_ISREG(got_tree_entry_get_mode(
5412 s->selected_entry))) {
5413 struct tog_view *blame_view;
5414 int begin_x = view_is_parent_view(view) ?
5415 view_split_begin_x(view->begin_x) : 0;
5417 err = blame_tree_entry(&blame_view, begin_x,
5418 s->selected_entry, &s->parents,
5419 s->commit_id, s->repo);
5423 blame_view->focussed = 1;
5424 if (view_is_parent_view(view)) {
5425 err = view_close_child(view);
5428 view_set_child(view, blame_view);
5429 view->focus_child = 1;
5431 *new_view = blame_view;
5435 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5436 s->selected = view->nlines - 4;
5449 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5454 static const struct got_error *
5455 cmd_tree(int argc, char *argv[])
5457 const struct got_error *error;
5458 struct got_repository *repo = NULL;
5459 struct got_worktree *worktree = NULL;
5460 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5461 struct got_object_id *commit_id = NULL;
5462 const char *commit_id_arg = NULL;
5464 struct got_commit_object *commit = NULL;
5465 struct got_tree_object *tree = NULL;
5466 struct got_reference *ref = NULL;
5467 const char *head_ref_name = NULL;
5469 struct tog_view *view;
5471 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5474 commit_id_arg = optarg;
5477 repo_path = realpath(optarg, NULL);
5478 if (repo_path == NULL)
5479 return got_error_from_errno2("realpath",
5494 cwd = getcwd(NULL, 0);
5496 return got_error_from_errno("getcwd");
5498 error = got_worktree_open(&worktree, cwd);
5499 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5502 if (repo_path == NULL) {
5505 strdup(got_worktree_get_repo_path(worktree));
5507 repo_path = strdup(cwd);
5509 if (repo_path == NULL) {
5510 error = got_error_from_errno("strdup");
5514 error = got_repo_open(&repo, repo_path, NULL);
5518 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5525 error = apply_unveil(got_repo_get_path(repo), NULL);
5529 if (commit_id_arg == NULL) {
5530 error = got_repo_match_object_id(&commit_id, &label,
5531 worktree ? got_worktree_get_head_ref_name(worktree) :
5532 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
5535 head_ref_name = label;
5537 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5539 head_ref_name = got_ref_get_name(ref);
5540 else if (error->code != GOT_ERR_NOT_REF)
5542 error = got_repo_match_object_id(&commit_id, NULL,
5543 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5548 error = got_object_open_as_commit(&commit, repo, commit_id);
5552 error = got_object_open_as_tree(&tree, repo,
5553 got_object_commit_get_tree_id(commit));
5557 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5559 error = got_error_from_errno("view_open");
5562 error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5565 if (!got_path_is_root_dir(in_repo_path)) {
5566 error = tree_view_walk_path(&view->state.tree, commit_id,
5573 /* Release work tree lock. */
5574 got_worktree_close(worktree);
5577 error = view_loop(view);
5586 got_object_commit_close(commit);
5588 got_object_tree_close(tree);
5590 got_repo_close(repo);
5594 static const struct got_error *
5595 ref_view_load_refs(struct tog_ref_view_state *s)
5597 const struct got_error *err;
5598 struct got_reflist_entry *sre;
5599 struct tog_reflist_entry *re;
5601 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5602 got_ref_cmp_by_name, NULL);
5607 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5608 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5611 re = malloc(sizeof(*re));
5613 return got_error_from_errno("malloc");
5616 re->idx = s->nrefs++;
5617 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5624 ref_view_free_refs(struct tog_ref_view_state *s)
5626 struct tog_reflist_entry *re;
5628 while (!TAILQ_EMPTY(&s->refs)) {
5629 re = TAILQ_FIRST(&s->refs);
5630 TAILQ_REMOVE(&s->refs, re, entry);
5633 got_ref_list_free(&s->simplerefs);
5636 static const struct got_error *
5637 open_ref_view(struct tog_view *view, struct got_repository *repo)
5639 const struct got_error *err = NULL;
5640 struct tog_ref_view_state *s = &view->state.ref;
5642 s->selected_entry = 0;
5645 SIMPLEQ_INIT(&s->simplerefs);
5646 TAILQ_INIT(&s->refs);
5647 SIMPLEQ_INIT(&s->colors);
5649 err = ref_view_load_refs(s);
5653 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5655 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5656 err = add_color(&s->colors, "^refs/heads/",
5657 TOG_COLOR_REFS_HEADS,
5658 get_color_value("TOG_COLOR_REFS_HEADS"));
5662 err = add_color(&s->colors, "^refs/tags/",
5663 TOG_COLOR_REFS_TAGS,
5664 get_color_value("TOG_COLOR_REFS_TAGS"));
5668 err = add_color(&s->colors, "^refs/remotes/",
5669 TOG_COLOR_REFS_REMOTES,
5670 get_color_value("TOG_COLOR_REFS_REMOTES"));
5675 view->show = show_ref_view;
5676 view->input = input_ref_view;
5677 view->close = close_ref_view;
5678 view->search_start = search_start_ref_view;
5679 view->search_next = search_next_ref_view;
5682 free_colors(&s->colors);
5686 static const struct got_error *
5687 close_ref_view(struct tog_view *view)
5689 struct tog_ref_view_state *s = &view->state.ref;
5691 ref_view_free_refs(s);
5692 free_colors(&s->colors);
5697 static const struct got_error *
5698 resolve_reflist_entry(struct got_object_id **commit_id,
5699 struct tog_reflist_entry *re, struct got_repository *repo)
5701 const struct got_error *err = NULL;
5702 struct got_object_id *obj_id;
5703 struct got_tag_object *tag = NULL;
5708 err = got_ref_resolve(&obj_id, repo, re->ref);
5712 err = got_object_get_type(&obj_type, repo, obj_id);
5717 case GOT_OBJ_TYPE_COMMIT:
5718 *commit_id = obj_id;
5720 case GOT_OBJ_TYPE_TAG:
5721 err = got_object_open_as_tag(&tag, repo, obj_id);
5725 err = got_object_get_type(&obj_type, repo,
5726 got_object_tag_get_object_id(tag));
5729 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5730 err = got_error(GOT_ERR_OBJ_TYPE);
5733 *commit_id = got_object_id_dup(
5734 got_object_tag_get_object_id(tag));
5735 if (*commit_id == NULL) {
5736 err = got_error_from_errno("got_object_id_dup");
5741 err = got_error(GOT_ERR_OBJ_TYPE);
5747 got_object_tag_close(tag);
5755 static const struct got_error *
5756 log_ref_entry(struct tog_view **new_view, int begin_x,
5757 struct tog_reflist_entry *re, struct got_repository *repo)
5759 struct tog_view *log_view;
5760 const struct got_error *err = NULL;
5761 struct got_object_id *commit_id = NULL;
5765 err = resolve_reflist_entry(&commit_id, re, repo);
5767 if (err->code != GOT_ERR_OBJ_TYPE)
5773 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5774 if (log_view == NULL) {
5775 err = got_error_from_errno("view_open");
5779 err = open_log_view(log_view, commit_id, repo,
5780 got_ref_get_name(re->ref), "", 0);
5783 view_close(log_view);
5785 *new_view = log_view;
5791 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5793 struct tog_reflist_entry *re;
5796 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5799 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5800 while (i++ < maxscroll) {
5803 s->first_displayed_entry = re;
5804 re = TAILQ_PREV(re, tog_reflist_head, entry);
5809 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5811 struct tog_reflist_entry *next, *last;
5814 if (s->first_displayed_entry)
5815 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5817 next = TAILQ_FIRST(&s->refs);
5819 last = s->last_displayed_entry;
5820 while (next && last && n++ < maxscroll) {
5821 last = TAILQ_NEXT(last, entry);
5823 s->first_displayed_entry = next;
5824 next = TAILQ_NEXT(next, entry);
5829 static const struct got_error *
5830 search_start_ref_view(struct tog_view *view)
5832 struct tog_ref_view_state *s = &view->state.ref;
5834 s->matched_entry = NULL;
5839 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5841 regmatch_t regmatch;
5843 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5847 static const struct got_error *
5848 search_next_ref_view(struct tog_view *view)
5850 struct tog_ref_view_state *s = &view->state.ref;
5851 struct tog_reflist_entry *re = NULL;
5853 if (!view->searching) {
5854 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5858 if (s->matched_entry) {
5859 if (view->searching == TOG_SEARCH_FORWARD) {
5860 if (s->selected_entry)
5861 re = TAILQ_NEXT(s->selected_entry, entry);
5863 re = TAILQ_PREV(s->selected_entry,
5864 tog_reflist_head, entry);
5866 if (s->selected_entry == NULL)
5867 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5869 re = TAILQ_PREV(s->selected_entry,
5870 tog_reflist_head, entry);
5873 if (view->searching == TOG_SEARCH_FORWARD)
5874 re = TAILQ_FIRST(&s->refs);
5876 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5881 if (s->matched_entry == NULL) {
5882 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5885 if (view->searching == TOG_SEARCH_FORWARD)
5886 re = TAILQ_FIRST(&s->refs);
5888 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5891 if (match_reflist_entry(re, &view->regex)) {
5892 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5893 s->matched_entry = re;
5897 if (view->searching == TOG_SEARCH_FORWARD)
5898 re = TAILQ_NEXT(re, entry);
5900 re = TAILQ_PREV(re, tog_reflist_head, entry);
5903 if (s->matched_entry) {
5904 s->first_displayed_entry = s->matched_entry;
5911 static const struct got_error *
5912 show_ref_view(struct tog_view *view)
5914 const struct got_error *err = NULL;
5915 struct tog_ref_view_state *s = &view->state.ref;
5916 struct tog_reflist_entry *re;
5919 struct tog_color *tc;
5921 int limit = view->nlines;
5923 werase(view->window);
5930 re = s->first_displayed_entry;
5932 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5934 return got_error_from_errno("asprintf");
5936 err = format_line(&wline, &width, line, view->ncols, 0);
5941 if (view_needs_focus_indication(view))
5942 wstandout(view->window);
5943 waddwstr(view->window, wline);
5944 if (view_needs_focus_indication(view))
5945 wstandend(view->window);
5950 if (width < view->ncols - 1)
5951 waddch(view->window, '\n');
5956 while (re && limit > 0) {
5959 if (got_ref_is_symbolic(re->ref)) {
5960 if (asprintf(&line, "%s -> %s",
5961 got_ref_get_name(re->ref),
5962 got_ref_get_symref_target(re->ref)) == -1)
5963 return got_error_from_errno("asprintf");
5964 } else if (s->show_ids) {
5965 struct got_object_id *id;
5967 err = got_ref_resolve(&id, s->repo, re->ref);
5970 err = got_object_id_str(&id_str, id);
5975 if (asprintf(&line, "%s: %s",
5976 got_ref_get_name(re->ref), id_str) == -1) {
5977 err = got_error_from_errno("asprintf");
5985 line = strdup(got_ref_get_name(re->ref));
5987 return got_error_from_errno("strdup");
5990 err = format_line(&wline, &width, line, view->ncols, 0);
5995 if (n == s->selected) {
5997 wstandout(view->window);
5998 s->selected_entry = re;
6000 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6002 wattr_on(view->window,
6003 COLOR_PAIR(tc->colorpair), NULL);
6004 waddwstr(view->window, wline);
6006 wattr_off(view->window,
6007 COLOR_PAIR(tc->colorpair), NULL);
6008 if (width < view->ncols - 1)
6009 waddch(view->window, '\n');
6010 if (n == s->selected && view->focussed)
6011 wstandend(view->window);
6017 s->last_displayed_entry = re;
6020 re = TAILQ_NEXT(re, entry);
6027 static const struct got_error *
6028 browse_ref_tree(struct tog_view **new_view, int begin_x,
6029 struct tog_reflist_entry *re, struct got_repository *repo)
6031 const struct got_error *err = NULL;
6032 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6033 struct got_tree_object *tree = NULL;
6034 struct tog_view *tree_view;
6038 err = resolve_reflist_entry(&commit_id, re, repo);
6040 if (err->code != GOT_ERR_OBJ_TYPE)
6046 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6050 err = got_object_open_as_tree(&tree, repo, tree_id);
6054 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6055 if (tree_view == NULL) {
6056 err = got_error_from_errno("view_open");
6060 err = open_tree_view(tree_view, tree, commit_id,
6061 got_ref_get_name(re->ref), repo);
6065 *new_view = tree_view;
6071 got_object_tree_close(tree);
6075 static const struct got_error *
6076 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6078 const struct got_error *err = NULL;
6079 struct tog_ref_view_state *s = &view->state.ref;
6080 struct tog_view *log_view, *tree_view;
6085 s->show_ids = !s->show_ids;
6089 if (!s->selected_entry)
6091 if (view_is_parent_view(view))
6092 begin_x = view_split_begin_x(view->begin_x);
6093 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6096 log_view->focussed = 1;
6097 if (view_is_parent_view(view)) {
6098 err = view_close_child(view);
6101 view_set_child(view, log_view);
6102 view->focus_child = 1;
6104 *new_view = log_view;
6107 if (!s->selected_entry)
6109 if (view_is_parent_view(view))
6110 begin_x = view_split_begin_x(view->begin_x);
6111 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6113 if (err || tree_view == NULL)
6116 tree_view->focussed = 1;
6117 if (view_is_parent_view(view)) {
6118 err = view_close_child(view);
6121 view_set_child(view, tree_view);
6122 view->focus_child = 1;
6124 *new_view = tree_view;
6128 if (s->selected > 0) {
6132 ref_scroll_up(s, 1);
6136 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6138 ref_scroll_up(s, MAX(0, view->nlines - 1));
6142 if (s->selected < s->ndisplayed - 1) {
6146 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6147 /* can't scroll any further */
6149 ref_scroll_down(s, 1);
6153 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6154 /* can't scroll any further; move cursor down */
6155 if (s->selected < s->ndisplayed - 1)
6156 s->selected = s->ndisplayed - 1;
6159 ref_scroll_down(s, view->nlines - 1);
6162 ref_view_free_refs(s);
6163 err = ref_view_load_refs(s);
6166 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6167 s->selected = view->nlines - 2;
6180 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6185 static const struct got_error *
6186 cmd_ref(int argc, char *argv[])
6188 const struct got_error *error;
6189 struct got_repository *repo = NULL;
6190 struct got_worktree *worktree = NULL;
6191 char *cwd = NULL, *repo_path = NULL;
6193 struct tog_view *view;
6195 while ((ch = getopt(argc, argv, "r:")) != -1) {
6198 repo_path = realpath(optarg, NULL);
6199 if (repo_path == NULL)
6200 return got_error_from_errno2("realpath",
6215 cwd = getcwd(NULL, 0);
6217 return got_error_from_errno("getcwd");
6219 error = got_worktree_open(&worktree, cwd);
6220 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6223 if (repo_path == NULL) {
6226 strdup(got_worktree_get_repo_path(worktree));
6228 repo_path = strdup(cwd);
6230 if (repo_path == NULL) {
6231 error = got_error_from_errno("strdup");
6235 error = got_repo_open(&repo, repo_path, NULL);
6241 error = apply_unveil(got_repo_get_path(repo), NULL);
6245 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6247 error = got_error_from_errno("view_open");
6251 error = open_ref_view(view, repo);
6256 /* Release work tree lock. */
6257 got_worktree_close(worktree);
6260 error = view_loop(view);
6265 got_repo_close(repo);
6270 list_commands(FILE *fp)
6274 fprintf(fp, "commands:");
6275 for (i = 0; i < nitems(tog_commands); i++) {
6276 struct tog_cmd *cmd = &tog_commands[i];
6277 fprintf(fp, " %s", cmd->name);
6283 usage(int hflag, int status)
6285 FILE *fp = (status == 0) ? stdout : stderr;
6287 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6290 fprintf(fp, "lazy usage: %s path\n", getprogname());
6297 make_argv(int argc, ...)
6305 argv = calloc(argc, sizeof(char *));
6308 for (i = 0; i < argc; i++) {
6309 argv[i] = strdup(va_arg(ap, char *));
6310 if (argv[i] == NULL)
6319 * Try to convert 'tog path' into a 'tog log path' command.
6320 * The user could simply have mistyped the command rather than knowingly
6321 * provided a path. So check whether argv[0] can in fact be resolved
6322 * to a path in the HEAD commit and print a special error if not.
6323 * This hack is for mpi@ <3
6325 static const struct got_error *
6326 tog_log_with_path(int argc, char *argv[])
6328 const struct got_error *error = NULL;
6329 struct tog_cmd *cmd = NULL;
6330 struct got_repository *repo = NULL;
6331 struct got_worktree *worktree = NULL;
6332 struct got_object_id *commit_id = NULL, *id = NULL;
6333 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6334 char *commit_id_str = NULL, **cmd_argv = NULL;
6336 cwd = getcwd(NULL, 0);
6338 return got_error_from_errno("getcwd");
6340 error = got_worktree_open(&worktree, cwd);
6341 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6345 repo_path = strdup(got_worktree_get_repo_path(worktree));
6347 repo_path = strdup(cwd);
6348 if (repo_path == NULL) {
6349 error = got_error_from_errno("strdup");
6353 error = got_repo_open(&repo, repo_path, NULL);
6357 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6362 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6363 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6364 GOT_OBJ_TYPE_COMMIT, 1, repo);
6369 got_worktree_close(worktree);
6373 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6375 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6377 fprintf(stderr, "%s: '%s' is no known command or path\n",
6378 getprogname(), argv[0]);
6383 got_repo_close(repo);
6386 error = got_object_id_str(&commit_id_str, commit_id);
6390 cmd = &tog_commands[0]; /* log */
6392 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6393 error = cmd->cmd_main(argc, cmd_argv);
6396 got_repo_close(repo);
6398 got_worktree_close(worktree);
6400 free(commit_id_str);
6407 for (i = 0; i < argc; i++)
6415 main(int argc, char *argv[])
6417 const struct got_error *error = NULL;
6418 struct tog_cmd *cmd = NULL;
6419 int ch, hflag = 0, Vflag = 0;
6420 char **cmd_argv = NULL;
6421 static struct option longopts[] = {
6422 { "version", no_argument, NULL, 'V' },
6426 setlocale(LC_CTYPE, "");
6428 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6448 got_version_print_str();
6453 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6461 /* Build an argument vector which runs a default command. */
6462 cmd = &tog_commands[0];
6464 cmd_argv = make_argv(argc, cmd->name);
6468 /* Did the user specify a command? */
6469 for (i = 0; i < nitems(tog_commands); i++) {
6470 if (strncmp(tog_commands[i].name, argv[0],
6471 strlen(argv[0])) == 0) {
6472 cmd = &tog_commands[i];
6481 /* No command specified; try log with a path */
6482 error = tog_log_with_path(argc, argv);
6487 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6494 for (i = 0; i < argc; i++)
6499 if (error && error->code != GOT_ERR_CANCELLED)
6500 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);