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 if (repo_path == NULL) {
2706 cwd = getcwd(NULL, 0);
2708 return got_error_from_errno("getcwd");
2709 error = got_worktree_open(&worktree, cwd);
2710 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2714 strdup(got_worktree_get_repo_path(worktree));
2716 repo_path = strdup(cwd);
2717 if (repo_path == NULL) {
2718 error = got_error_from_errno("strdup");
2723 error = got_repo_open(&repo, repo_path, NULL);
2727 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2734 error = apply_unveil(got_repo_get_path(repo),
2735 worktree ? got_worktree_get_root_path(worktree) : NULL);
2739 if (start_commit == NULL) {
2740 error = got_repo_match_object_id(&start_id, &label,
2741 worktree ? got_worktree_get_head_ref_name(worktree) :
2742 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
2745 head_ref_name = label;
2747 error = got_ref_open(&ref, repo, start_commit, 0);
2749 head_ref_name = got_ref_get_name(ref);
2750 else if (error->code != GOT_ERR_NOT_REF)
2752 error = got_repo_match_object_id(&start_id, NULL,
2753 start_commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2758 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2760 error = got_error_from_errno("view_open");
2763 error = open_log_view(view, start_id, repo, head_ref_name,
2764 in_repo_path, log_branches);
2768 /* Release work tree lock. */
2769 got_worktree_close(worktree);
2772 error = view_loop(view);
2782 got_repo_close(repo);
2784 got_worktree_close(worktree);
2792 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2793 "[-w] object1 object2\n", getprogname());
2798 match_line(const char *line, regex_t *regex, size_t nmatch,
2799 regmatch_t *regmatch)
2801 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2805 match_color(struct tog_colors *colors, const char *line)
2807 struct tog_color *tc = NULL;
2809 SIMPLEQ_FOREACH(tc, colors, entry) {
2810 if (match_line(line, &tc->regex, 0, NULL))
2817 static const struct got_error *
2818 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2819 WINDOW *window, regmatch_t *regmatch)
2821 const struct got_error *err = NULL;
2828 s = strndup(line, regmatch->rm_so);
2830 return got_error_from_errno("strndup");
2832 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2837 waddwstr(window, wline);
2844 s = strndup(line + regmatch->rm_so,
2845 regmatch->rm_eo - regmatch->rm_so);
2847 err = got_error_from_errno("strndup");
2851 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2856 wattr_on(window, A_STANDOUT, NULL);
2857 waddwstr(window, wline);
2858 wattr_off(window, A_STANDOUT, NULL);
2865 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2866 err = format_line(&wline, &width,
2867 line + regmatch->rm_eo, wlimit, col_tab_align);
2870 waddwstr(window, wline);
2878 static const struct got_error *
2879 draw_file(struct tog_view *view, const char *header)
2881 struct tog_diff_view_state *s = &view->state.diff;
2882 regmatch_t *regmatch = &view->regmatch;
2883 const struct got_error *err;
2886 size_t linesize = 0;
2888 struct tog_color *tc;
2891 int max_lines = view->nlines;
2892 int nlines = s->nlines;
2895 line_offset = s->line_offsets[s->first_displayed_line - 1];
2896 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2897 return got_error_from_errno("fseek");
2899 werase(view->window);
2902 if (asprintf(&line, "[%d/%d] %s",
2903 s->first_displayed_line - 1 + s->selected_line, nlines,
2905 return got_error_from_errno("asprintf");
2906 err = format_line(&wline, &width, line, view->ncols, 0);
2911 if (view_needs_focus_indication(view))
2912 wstandout(view->window);
2913 waddwstr(view->window, wline);
2916 if (view_needs_focus_indication(view))
2917 wstandend(view->window);
2918 if (width <= view->ncols - 1)
2919 waddch(view->window, '\n');
2928 while (max_lines > 0 && nprinted < max_lines) {
2929 linelen = getline(&line, &linesize, s->f);
2930 if (linelen == -1) {
2936 return got_ferror(s->f, GOT_ERR_IO);
2939 tc = match_color(&s->colors, line);
2941 wattr_on(view->window,
2942 COLOR_PAIR(tc->colorpair), NULL);
2943 if (s->first_displayed_line + nprinted == s->matched_line &&
2944 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2945 err = add_matched_line(&width, line, view->ncols, 0,
2946 view->window, regmatch);
2952 err = format_line(&wline, &width, line, view->ncols, 0);
2957 waddwstr(view->window, wline);
2962 wattr_off(view->window,
2963 COLOR_PAIR(tc->colorpair), NULL);
2964 if (width <= view->ncols - 1)
2965 waddch(view->window, '\n');
2970 s->last_displayed_line = s->first_displayed_line +
2973 s->last_displayed_line = s->first_displayed_line;
2978 while (nprinted < view->nlines) {
2979 waddch(view->window, '\n');
2983 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2988 wstandout(view->window);
2989 waddwstr(view->window, wline);
2992 wstandend(view->window);
2999 get_datestr(time_t *time, char *datebuf)
3001 struct tm mytm, *tm;
3004 tm = gmtime_r(time, &mytm);
3007 s = asctime_r(tm, datebuf);
3010 p = strchr(s, '\n');
3016 static const struct got_error *
3017 get_changed_paths(struct got_pathlist_head *paths,
3018 struct got_commit_object *commit, struct got_repository *repo)
3020 const struct got_error *err = NULL;
3021 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3022 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3023 struct got_object_qid *qid;
3025 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3027 struct got_commit_object *pcommit;
3028 err = got_object_open_as_commit(&pcommit, repo,
3033 tree_id1 = got_object_commit_get_tree_id(pcommit);
3034 got_object_commit_close(pcommit);
3039 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3044 tree_id2 = got_object_commit_get_tree_id(commit);
3045 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3049 err = got_diff_tree(tree1, tree2, "", "", repo,
3050 got_diff_tree_collect_changed_paths, paths, 0);
3053 got_object_tree_close(tree1);
3055 got_object_tree_close(tree2);
3059 static const struct got_error *
3060 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3064 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3066 return got_error_from_errno("reallocarray");
3068 (*line_offsets)[*nlines] = off;
3073 static const struct got_error *
3074 write_commit_info(off_t **line_offsets, size_t *nlines,
3075 struct got_object_id *commit_id, struct got_reflist_head *refs,
3076 struct got_repository *repo, FILE *outfile)
3078 const struct got_error *err = NULL;
3079 char datebuf[26], *datestr;
3080 struct got_commit_object *commit;
3081 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3082 time_t committer_time;
3083 const char *author, *committer;
3084 char *refs_str = NULL;
3085 struct got_pathlist_head changed_paths;
3086 struct got_pathlist_entry *pe;
3090 TAILQ_INIT(&changed_paths);
3093 err = build_refs_str(&refs_str, refs, commit_id, repo);
3098 err = got_object_open_as_commit(&commit, repo, commit_id);
3102 err = got_object_id_str(&id_str, commit_id);
3104 err = got_error_from_errno("got_object_id_str");
3108 err = add_line_offset(line_offsets, nlines, 0);
3112 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3113 refs_str ? refs_str : "", refs_str ? ")" : "");
3115 err = got_error_from_errno("fprintf");
3119 err = add_line_offset(line_offsets, nlines, outoff);
3123 n = fprintf(outfile, "from: %s\n",
3124 got_object_commit_get_author(commit));
3126 err = got_error_from_errno("fprintf");
3130 err = add_line_offset(line_offsets, nlines, outoff);
3134 committer_time = got_object_commit_get_committer_time(commit);
3135 datestr = get_datestr(&committer_time, datebuf);
3137 n = fprintf(outfile, "date: %s UTC\n", datestr);
3139 err = got_error_from_errno("fprintf");
3143 err = add_line_offset(line_offsets, nlines, outoff);
3147 author = got_object_commit_get_author(commit);
3148 committer = got_object_commit_get_committer(commit);
3149 if (strcmp(author, committer) != 0) {
3150 n = fprintf(outfile, "via: %s\n", committer);
3152 err = got_error_from_errno("fprintf");
3156 err = add_line_offset(line_offsets, nlines, outoff);
3160 err = got_object_commit_get_logmsg(&logmsg, commit);
3164 while ((line = strsep(&s, "\n")) != NULL) {
3165 n = fprintf(outfile, "%s\n", line);
3167 err = got_error_from_errno("fprintf");
3171 err = add_line_offset(line_offsets, nlines, outoff);
3176 err = get_changed_paths(&changed_paths, commit, repo);
3179 TAILQ_FOREACH(pe, &changed_paths, entry) {
3180 struct got_diff_changed_path *cp = pe->data;
3181 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3183 err = got_error_from_errno("fprintf");
3187 err = add_line_offset(line_offsets, nlines, outoff);
3190 free((char *)pe->path);
3194 fputc('\n', outfile);
3196 err = add_line_offset(line_offsets, nlines, outoff);
3198 got_pathlist_free(&changed_paths);
3202 got_object_commit_close(commit);
3204 free(*line_offsets);
3205 *line_offsets = NULL;
3211 static const struct got_error *
3212 create_diff(struct tog_diff_view_state *s)
3214 const struct got_error *err = NULL;
3218 free(s->line_offsets);
3219 s->line_offsets = malloc(sizeof(off_t));
3220 if (s->line_offsets == NULL)
3221 return got_error_from_errno("malloc");
3226 err = got_error_from_errno("got_opentemp");
3229 if (s->f && fclose(s->f) != 0) {
3230 err = got_error_from_errno("fclose");
3236 err = got_object_get_type(&obj_type, s->repo, s->id1);
3238 err = got_object_get_type(&obj_type, s->repo, s->id2);
3243 case GOT_OBJ_TYPE_BLOB:
3244 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3245 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3246 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3248 case GOT_OBJ_TYPE_TREE:
3249 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3250 s->id1, s->id2, "", "", s->diff_context,
3251 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3253 case GOT_OBJ_TYPE_COMMIT: {
3254 const struct got_object_id_queue *parent_ids;
3255 struct got_object_qid *pid;
3256 struct got_commit_object *commit2;
3258 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3261 /* Show commit info if we're diffing to a parent/root commit. */
3262 if (s->id1 == NULL) {
3263 err = write_commit_info(&s->line_offsets, &s->nlines,
3264 s->id2, &s->refs, s->repo, s->f);
3268 parent_ids = got_object_commit_get_parent_ids(commit2);
3269 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3270 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3271 err = write_commit_info(
3272 &s->line_offsets, &s->nlines,
3273 s->id2, &s->refs, s->repo, s->f);
3280 got_object_commit_close(commit2);
3282 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3283 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3284 s->force_text_diff, s->repo, s->f);
3288 err = got_error(GOT_ERR_OBJ_TYPE);
3294 if (s->f && fflush(s->f) != 0 && err == NULL)
3295 err = got_error_from_errno("fflush");
3300 diff_view_indicate_progress(struct tog_view *view)
3302 mvwaddstr(view->window, 0, 0, "diffing...");
3307 static const struct got_error *
3308 search_start_diff_view(struct tog_view *view)
3310 struct tog_diff_view_state *s = &view->state.diff;
3312 s->matched_line = 0;
3316 static const struct got_error *
3317 search_next_diff_view(struct tog_view *view)
3319 struct tog_diff_view_state *s = &view->state.diff;
3322 size_t linesize = 0;
3325 if (!view->searching) {
3326 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3330 if (s->matched_line) {
3331 if (view->searching == TOG_SEARCH_FORWARD)
3332 lineno = s->matched_line + 1;
3334 lineno = s->matched_line - 1;
3336 if (view->searching == TOG_SEARCH_FORWARD)
3345 if (lineno <= 0 || lineno > s->nlines) {
3346 if (s->matched_line == 0) {
3347 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3351 if (view->searching == TOG_SEARCH_FORWARD)
3357 offset = s->line_offsets[lineno - 1];
3358 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3360 return got_error_from_errno("fseeko");
3362 linelen = getline(&line, &linesize, s->f);
3363 if (linelen != -1 &&
3364 match_line(line, &view->regex, 1, &view->regmatch)) {
3365 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3366 s->matched_line = lineno;
3369 if (view->searching == TOG_SEARCH_FORWARD)
3376 if (s->matched_line) {
3377 s->first_displayed_line = s->matched_line;
3378 s->selected_line = 1;
3384 static const struct got_error *
3385 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3386 struct got_object_id *id2, const char *label1, const char *label2,
3387 int diff_context, int ignore_whitespace, int force_text_diff,
3388 struct tog_view *log_view, struct got_repository *repo)
3390 const struct got_error *err;
3391 struct tog_diff_view_state *s = &view->state.diff;
3393 SIMPLEQ_INIT(&s->refs);
3395 if (id1 != NULL && id2 != NULL) {
3397 err = got_object_get_type(&type1, repo, id1);
3400 err = got_object_get_type(&type2, repo, id2);
3405 return got_error(GOT_ERR_OBJ_TYPE);
3407 s->first_displayed_line = 1;
3408 s->last_displayed_line = view->nlines;
3409 s->selected_line = 1;
3417 s->id1 = got_object_id_dup(id1);
3419 return got_error_from_errno("got_object_id_dup");
3423 s->id2 = got_object_id_dup(id2);
3424 if (s->id2 == NULL) {
3427 return got_error_from_errno("got_object_id_dup");
3430 s->first_displayed_line = 1;
3431 s->last_displayed_line = view->nlines;
3432 s->diff_context = diff_context;
3433 s->ignore_whitespace = ignore_whitespace;
3434 s->force_text_diff = force_text_diff;
3435 s->log_view = log_view;
3438 SIMPLEQ_INIT(&s->colors);
3439 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3440 err = add_color(&s->colors,
3441 "^-", TOG_COLOR_DIFF_MINUS,
3442 get_color_value("TOG_COLOR_DIFF_MINUS"));
3445 err = add_color(&s->colors, "^\\+",
3446 TOG_COLOR_DIFF_PLUS,
3447 get_color_value("TOG_COLOR_DIFF_PLUS"));
3449 free_colors(&s->colors);
3452 err = add_color(&s->colors,
3453 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3454 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3456 free_colors(&s->colors);
3460 err = add_color(&s->colors,
3461 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3462 TOG_COLOR_DIFF_META,
3463 get_color_value("TOG_COLOR_DIFF_META"));
3465 free_colors(&s->colors);
3469 err = add_color(&s->colors,
3470 "^(from|via): ", TOG_COLOR_AUTHOR,
3471 get_color_value("TOG_COLOR_AUTHOR"));
3473 free_colors(&s->colors);
3477 err = add_color(&s->colors,
3478 "^date: ", TOG_COLOR_DATE,
3479 get_color_value("TOG_COLOR_DATE"));
3481 free_colors(&s->colors);
3486 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3492 free_colors(&s->colors);
3496 if (log_view && view_is_splitscreen(view))
3497 show_log_view(log_view); /* draw vborder */
3498 diff_view_indicate_progress(view);
3500 s->line_offsets = NULL;
3502 err = create_diff(s);
3508 free_colors(&s->colors);
3509 got_ref_list_free(&s->refs);
3513 view->show = show_diff_view;
3514 view->input = input_diff_view;
3515 view->close = close_diff_view;
3516 view->search_start = search_start_diff_view;
3517 view->search_next = search_next_diff_view;
3522 static const struct got_error *
3523 close_diff_view(struct tog_view *view)
3525 const struct got_error *err = NULL;
3526 struct tog_diff_view_state *s = &view->state.diff;
3532 if (s->f && fclose(s->f) == EOF)
3533 err = got_error_from_errno("fclose");
3534 free_colors(&s->colors);
3535 free(s->line_offsets);
3536 s->line_offsets = NULL;
3538 got_ref_list_free(&s->refs);
3542 static const struct got_error *
3543 show_diff_view(struct tog_view *view)
3545 const struct got_error *err;
3546 struct tog_diff_view_state *s = &view->state.diff;
3547 char *id_str1 = NULL, *id_str2, *header;
3548 const char *label1, *label2;
3551 err = got_object_id_str(&id_str1, s->id1);
3554 label1 = s->label1 ? : id_str1;
3556 label1 = "/dev/null";
3558 err = got_object_id_str(&id_str2, s->id2);
3561 label2 = s->label2 ? : id_str2;
3563 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3564 err = got_error_from_errno("asprintf");
3572 return draw_file(view, header);
3575 static const struct got_error *
3576 set_selected_commit(struct tog_diff_view_state *s,
3577 struct commit_queue_entry *entry)
3579 const struct got_error *err;
3580 const struct got_object_id_queue *parent_ids;
3581 struct got_commit_object *selected_commit;
3582 struct got_object_qid *pid;
3585 s->id2 = got_object_id_dup(entry->id);
3587 return got_error_from_errno("got_object_id_dup");
3589 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3592 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3594 pid = SIMPLEQ_FIRST(parent_ids);
3595 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3596 got_object_commit_close(selected_commit);
3600 static const struct got_error *
3601 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3603 const struct got_error *err = NULL;
3604 struct tog_diff_view_state *s = &view->state.diff;
3605 struct tog_log_view_state *ls;
3606 struct commit_queue_entry *old_selected_entry;
3608 size_t linesize = 0;
3616 s->force_text_diff = !s->force_text_diff;
3618 s->ignore_whitespace = !s->ignore_whitespace;
3619 wclear(view->window);
3620 s->first_displayed_line = 1;
3621 s->last_displayed_line = view->nlines;
3622 diff_view_indicate_progress(view);
3623 err = create_diff(s);
3627 if (s->first_displayed_line > 1)
3628 s->first_displayed_line--;
3632 if (s->first_displayed_line == 1)
3635 while (i++ < view->nlines - 1 &&
3636 s->first_displayed_line > 1)
3637 s->first_displayed_line--;
3642 s->first_displayed_line++;
3650 while (!s->eof && i++ < view->nlines - 1) {
3651 linelen = getline(&line, &linesize, s->f);
3652 s->first_displayed_line++;
3653 if (linelen == -1) {
3657 err = got_ferror(s->f, GOT_ERR_IO);
3664 if (s->diff_context > 0) {
3666 diff_view_indicate_progress(view);
3667 err = create_diff(s);
3668 if (s->first_displayed_line + view->nlines - 1 >
3670 s->first_displayed_line = 1;
3671 s->last_displayed_line = view->nlines;
3676 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3678 diff_view_indicate_progress(view);
3679 err = create_diff(s);
3684 if (s->log_view == NULL)
3686 ls = &s->log_view->state.log;
3687 old_selected_entry = ls->selected_entry;
3689 err = input_log_view(NULL, s->log_view, KEY_UP);
3693 if (old_selected_entry == ls->selected_entry)
3696 err = set_selected_commit(s, ls->selected_entry);
3700 s->first_displayed_line = 1;
3701 s->last_displayed_line = view->nlines;
3703 diff_view_indicate_progress(view);
3704 err = create_diff(s);
3708 if (s->log_view == NULL)
3710 ls = &s->log_view->state.log;
3711 old_selected_entry = ls->selected_entry;
3713 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3717 if (old_selected_entry == ls->selected_entry)
3720 err = set_selected_commit(s, ls->selected_entry);
3724 s->first_displayed_line = 1;
3725 s->last_displayed_line = view->nlines;
3727 diff_view_indicate_progress(view);
3728 err = create_diff(s);
3737 static const struct got_error *
3738 cmd_diff(int argc, char *argv[])
3740 const struct got_error *error = NULL;
3741 struct got_repository *repo = NULL;
3742 struct got_worktree *worktree = NULL;
3743 struct got_object_id *id1 = NULL, *id2 = NULL;
3744 char *repo_path = NULL, *cwd = NULL;
3745 char *id_str1 = NULL, *id_str2 = NULL;
3746 char *label1 = NULL, *label2 = NULL;
3747 int diff_context = 3, ignore_whitespace = 0;
3748 int ch, force_text_diff = 0;
3750 struct tog_view *view;
3752 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3755 force_text_diff = 1;
3758 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3761 err(1, "-C option %s", errstr);
3764 repo_path = realpath(optarg, NULL);
3765 if (repo_path == NULL)
3766 return got_error_from_errno2("realpath",
3768 got_path_strip_trailing_slashes(repo_path);
3771 ignore_whitespace = 1;
3783 usage_diff(); /* TODO show local worktree changes */
3784 } else if (argc == 2) {
3790 if (repo_path == NULL) {
3791 cwd = getcwd(NULL, 0);
3793 return got_error_from_errno("getcwd");
3794 error = got_worktree_open(&worktree, cwd);
3795 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3799 strdup(got_worktree_get_repo_path(worktree));
3801 repo_path = strdup(cwd);
3802 if (repo_path == NULL) {
3803 error = got_error_from_errno("strdup");
3808 error = got_repo_open(&repo, repo_path, NULL);
3814 error = apply_unveil(got_repo_get_path(repo), NULL);
3818 error = got_repo_match_object_id(&id1, &label1, id_str1,
3819 GOT_OBJ_TYPE_ANY, 1, repo);
3823 error = got_repo_match_object_id(&id2, &label2, id_str2,
3824 GOT_OBJ_TYPE_ANY, 1, repo);
3828 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3830 error = got_error_from_errno("view_open");
3833 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3834 ignore_whitespace, force_text_diff, NULL, repo);
3837 error = view_loop(view);
3844 got_repo_close(repo);
3846 got_worktree_close(worktree);
3854 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3859 struct tog_blame_line {
3861 struct got_object_id *id;
3864 static const struct got_error *
3865 draw_blame(struct tog_view *view)
3867 struct tog_blame_view_state *s = &view->state.blame;
3868 struct tog_blame *blame = &s->blame;
3869 regmatch_t *regmatch = &view->regmatch;
3870 const struct got_error *err;
3871 int lineno = 0, nprinted = 0;
3873 size_t linesize = 0;
3877 struct tog_blame_line *blame_line;
3878 struct got_object_id *prev_id = NULL;
3880 struct tog_color *tc;
3882 err = got_object_id_str(&id_str, s->blamed_commit->id);
3887 werase(view->window);
3889 if (asprintf(&line, "commit %s", id_str) == -1) {
3890 err = got_error_from_errno("asprintf");
3895 err = format_line(&wline, &width, line, view->ncols, 0);
3900 if (view_needs_focus_indication(view))
3901 wstandout(view->window);
3902 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3904 wattr_on(view->window,
3905 COLOR_PAIR(tc->colorpair), NULL);
3906 waddwstr(view->window, wline);
3908 wattr_off(view->window,
3909 COLOR_PAIR(tc->colorpair), NULL);
3910 if (view_needs_focus_indication(view))
3911 wstandend(view->window);
3914 if (width < view->ncols - 1)
3915 waddch(view->window, '\n');
3917 if (asprintf(&line, "[%d/%d] %s%s",
3918 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3919 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3921 return got_error_from_errno("asprintf");
3924 err = format_line(&wline, &width, line, view->ncols, 0);
3929 waddwstr(view->window, wline);
3932 if (width < view->ncols - 1)
3933 waddch(view->window, '\n');
3936 while (nprinted < view->nlines - 2) {
3937 linelen = getline(&line, &linesize, blame->f);
3938 if (linelen == -1) {
3939 if (feof(blame->f)) {
3944 return got_ferror(blame->f, GOT_ERR_IO);
3946 if (++lineno < s->first_displayed_line)
3949 if (view->focussed && nprinted == s->selected_line - 1)
3950 wstandout(view->window);
3952 if (blame->nlines > 0) {
3953 blame_line = &blame->lines[lineno - 1];
3954 if (blame_line->annotated && prev_id &&
3955 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3957 nprinted == s->selected_line - 1)) {
3958 waddstr(view->window, " ");
3959 } else if (blame_line->annotated) {
3961 err = got_object_id_str(&id_str, blame_line->id);
3966 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3968 wattr_on(view->window,
3969 COLOR_PAIR(tc->colorpair), NULL);
3970 wprintw(view->window, "%.8s", id_str);
3972 wattr_off(view->window,
3973 COLOR_PAIR(tc->colorpair), NULL);
3975 prev_id = blame_line->id;
3977 waddstr(view->window, "........");
3981 waddstr(view->window, "........");
3985 if (view->focussed && nprinted == s->selected_line - 1)
3986 wstandend(view->window);
3987 waddstr(view->window, " ");
3989 if (view->ncols <= 9) {
3991 wline = wcsdup(L"");
3992 if (wline == NULL) {
3993 err = got_error_from_errno("wcsdup");
3997 } else if (s->first_displayed_line + nprinted ==
3999 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4000 err = add_matched_line(&width, line, view->ncols - 9, 9,
4001 view->window, regmatch);
4008 err = format_line(&wline, &width, line,
4009 view->ncols - 9, 9);
4010 waddwstr(view->window, wline);
4016 if (width <= view->ncols - 1)
4017 waddch(view->window, '\n');
4018 if (++nprinted == 1)
4019 s->first_displayed_line = lineno;
4022 s->last_displayed_line = lineno;
4029 static const struct got_error *
4030 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4032 const struct got_error *err = NULL;
4033 struct tog_blame_cb_args *a = arg;
4034 struct tog_blame_line *line;
4037 if (nlines != a->nlines ||
4038 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4039 return got_error(GOT_ERR_RANGE);
4041 errcode = pthread_mutex_lock(&tog_mutex);
4043 return got_error_set_errno(errcode, "pthread_mutex_lock");
4045 if (*a->quit) { /* user has quit the blame view */
4046 err = got_error(GOT_ERR_ITER_COMPLETED);
4051 goto done; /* no change in this commit */
4053 line = &a->lines[lineno - 1];
4054 if (line->annotated)
4057 line->id = got_object_id_dup(id);
4058 if (line->id == NULL) {
4059 err = got_error_from_errno("got_object_id_dup");
4062 line->annotated = 1;
4064 errcode = pthread_mutex_unlock(&tog_mutex);
4066 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4071 blame_thread(void *arg)
4073 const struct got_error *err;
4074 struct tog_blame_thread_args *ta = arg;
4075 struct tog_blame_cb_args *a = ta->cb_args;
4078 err = block_signals_used_by_main_thread();
4082 err = got_blame(ta->path, a->commit_id, ta->repo,
4083 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4084 if (err && err->code == GOT_ERR_CANCELLED)
4087 errcode = pthread_mutex_lock(&tog_mutex);
4089 return (void *)got_error_set_errno(errcode,
4090 "pthread_mutex_lock");
4092 got_repo_close(ta->repo);
4096 errcode = pthread_mutex_unlock(&tog_mutex);
4097 if (errcode && err == NULL)
4098 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4103 static struct got_object_id *
4104 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4105 int first_displayed_line, int selected_line)
4107 struct tog_blame_line *line;
4112 line = &lines[first_displayed_line - 1 + selected_line - 1];
4113 if (!line->annotated)
4119 static const struct got_error *
4120 stop_blame(struct tog_blame *blame)
4122 const struct got_error *err = NULL;
4125 if (blame->thread) {
4127 errcode = pthread_mutex_unlock(&tog_mutex);
4129 return got_error_set_errno(errcode,
4130 "pthread_mutex_unlock");
4131 errcode = pthread_join(blame->thread, (void **)&err);
4133 return got_error_set_errno(errcode, "pthread_join");
4134 errcode = pthread_mutex_lock(&tog_mutex);
4136 return got_error_set_errno(errcode,
4137 "pthread_mutex_lock");
4138 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4140 blame->thread = NULL;
4142 if (blame->thread_args.repo) {
4143 got_repo_close(blame->thread_args.repo);
4144 blame->thread_args.repo = NULL;
4147 if (fclose(blame->f) != 0 && err == NULL)
4148 err = got_error_from_errno("fclose");
4152 for (i = 0; i < blame->nlines; i++)
4153 free(blame->lines[i].id);
4155 blame->lines = NULL;
4157 free(blame->cb_args.commit_id);
4158 blame->cb_args.commit_id = NULL;
4163 static const struct got_error *
4164 cancel_blame_view(void *arg)
4166 const struct got_error *err = NULL;
4170 errcode = pthread_mutex_lock(&tog_mutex);
4172 return got_error_set_errno(errcode,
4173 "pthread_mutex_unlock");
4176 err = got_error(GOT_ERR_CANCELLED);
4178 errcode = pthread_mutex_unlock(&tog_mutex);
4180 return got_error_set_errno(errcode,
4181 "pthread_mutex_lock");
4186 static const struct got_error *
4187 run_blame(struct tog_view *view)
4189 struct tog_blame_view_state *s = &view->state.blame;
4190 struct tog_blame *blame = &s->blame;
4191 const struct got_error *err = NULL;
4192 struct got_blob_object *blob = NULL;
4193 struct got_repository *thread_repo = NULL;
4194 struct got_object_id *obj_id = NULL;
4197 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4202 err = got_object_get_type(&obj_type, s->repo, obj_id);
4206 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4207 err = got_error(GOT_ERR_OBJ_TYPE);
4211 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4214 blame->f = got_opentemp();
4215 if (blame->f == NULL) {
4216 err = got_error_from_errno("got_opentemp");
4219 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4220 &blame->line_offsets, blame->f, blob);
4221 if (err || blame->nlines == 0)
4224 /* Don't include \n at EOF in the blame line count. */
4225 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4228 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4229 if (blame->lines == NULL) {
4230 err = got_error_from_errno("calloc");
4234 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4238 blame->cb_args.view = view;
4239 blame->cb_args.lines = blame->lines;
4240 blame->cb_args.nlines = blame->nlines;
4241 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4242 if (blame->cb_args.commit_id == NULL) {
4243 err = got_error_from_errno("got_object_id_dup");
4246 blame->cb_args.quit = &s->done;
4248 blame->thread_args.path = s->path;
4249 blame->thread_args.repo = thread_repo;
4250 blame->thread_args.cb_args = &blame->cb_args;
4251 blame->thread_args.complete = &s->blame_complete;
4252 blame->thread_args.cancel_cb = cancel_blame_view;
4253 blame->thread_args.cancel_arg = &s->done;
4254 s->blame_complete = 0;
4256 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4257 s->first_displayed_line = 1;
4258 s->last_displayed_line = view->nlines;
4259 s->selected_line = 1;
4264 got_object_blob_close(blob);
4271 static const struct got_error *
4272 open_blame_view(struct tog_view *view, char *path,
4273 struct got_object_id *commit_id, struct got_repository *repo)
4275 const struct got_error *err = NULL;
4276 struct tog_blame_view_state *s = &view->state.blame;
4278 SIMPLEQ_INIT(&s->blamed_commits);
4280 s->path = strdup(path);
4281 if (s->path == NULL)
4282 return got_error_from_errno("strdup");
4284 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4290 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4291 s->first_displayed_line = 1;
4292 s->last_displayed_line = view->nlines;
4293 s->selected_line = 1;
4294 s->blame_complete = 0;
4296 s->commit_id = commit_id;
4297 memset(&s->blame, 0, sizeof(s->blame));
4299 SIMPLEQ_INIT(&s->colors);
4300 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4301 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4302 get_color_value("TOG_COLOR_COMMIT"));
4307 view->show = show_blame_view;
4308 view->input = input_blame_view;
4309 view->close = close_blame_view;
4310 view->search_start = search_start_blame_view;
4311 view->search_next = search_next_blame_view;
4313 return run_blame(view);
4316 static const struct got_error *
4317 close_blame_view(struct tog_view *view)
4319 const struct got_error *err = NULL;
4320 struct tog_blame_view_state *s = &view->state.blame;
4322 if (s->blame.thread)
4323 err = stop_blame(&s->blame);
4325 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4326 struct got_object_qid *blamed_commit;
4327 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4328 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4329 got_object_qid_free(blamed_commit);
4333 free_colors(&s->colors);
4338 static const struct got_error *
4339 search_start_blame_view(struct tog_view *view)
4341 struct tog_blame_view_state *s = &view->state.blame;
4343 s->matched_line = 0;
4347 static const struct got_error *
4348 search_next_blame_view(struct tog_view *view)
4350 struct tog_blame_view_state *s = &view->state.blame;
4353 size_t linesize = 0;
4356 if (!view->searching) {
4357 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4361 if (s->matched_line) {
4362 if (view->searching == TOG_SEARCH_FORWARD)
4363 lineno = s->matched_line + 1;
4365 lineno = s->matched_line - 1;
4367 if (view->searching == TOG_SEARCH_FORWARD)
4370 lineno = s->blame.nlines;
4376 if (lineno <= 0 || lineno > s->blame.nlines) {
4377 if (s->matched_line == 0) {
4378 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4382 if (view->searching == TOG_SEARCH_FORWARD)
4385 lineno = s->blame.nlines;
4388 offset = s->blame.line_offsets[lineno - 1];
4389 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4391 return got_error_from_errno("fseeko");
4393 linelen = getline(&line, &linesize, s->blame.f);
4394 if (linelen != -1 &&
4395 match_line(line, &view->regex, 1, &view->regmatch)) {
4396 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4397 s->matched_line = lineno;
4400 if (view->searching == TOG_SEARCH_FORWARD)
4407 if (s->matched_line) {
4408 s->first_displayed_line = s->matched_line;
4409 s->selected_line = 1;
4415 static const struct got_error *
4416 show_blame_view(struct tog_view *view)
4418 const struct got_error *err = NULL;
4419 struct tog_blame_view_state *s = &view->state.blame;
4422 if (s->blame.thread == NULL) {
4423 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4424 &s->blame.thread_args);
4426 return got_error_set_errno(errcode, "pthread_create");
4428 halfdelay(1); /* fast refresh while annotating */
4431 if (s->blame_complete)
4432 halfdelay(10); /* disable fast refresh */
4434 err = draw_blame(view);
4440 static const struct got_error *
4441 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4443 const struct got_error *err = NULL, *thread_err = NULL;
4444 struct tog_view *diff_view;
4445 struct tog_blame_view_state *s = &view->state.blame;
4454 if (s->selected_line > 1)
4456 else if (s->selected_line == 1 &&
4457 s->first_displayed_line > 1)
4458 s->first_displayed_line--;
4462 if (s->first_displayed_line == 1) {
4463 s->selected_line = 1;
4466 if (s->first_displayed_line > view->nlines - 2)
4467 s->first_displayed_line -=
4470 s->first_displayed_line = 1;
4474 if (s->selected_line < view->nlines - 2 &&
4475 s->first_displayed_line +
4476 s->selected_line <= s->blame.nlines)
4478 else if (s->last_displayed_line <
4480 s->first_displayed_line++;
4484 struct got_object_id *id = NULL;
4485 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4486 s->first_displayed_line, s->selected_line);
4490 struct got_commit_object *commit;
4491 struct got_object_qid *pid;
4492 struct got_object_id *blob_id = NULL;
4494 err = got_object_open_as_commit(&commit,
4498 pid = SIMPLEQ_FIRST(
4499 got_object_commit_get_parent_ids(commit));
4501 got_object_commit_close(commit);
4504 /* Check if path history ends here. */
4505 err = got_object_id_by_path(&blob_id, s->repo,
4508 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4510 got_object_commit_close(commit);
4513 err = got_object_get_type(&obj_type, s->repo,
4516 /* Can't blame non-blob type objects. */
4517 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4518 got_object_commit_close(commit);
4521 err = got_object_qid_alloc(&s->blamed_commit,
4523 got_object_commit_close(commit);
4525 if (got_object_id_cmp(id,
4526 s->blamed_commit->id) == 0)
4528 err = got_object_qid_alloc(&s->blamed_commit,
4534 thread_err = stop_blame(&s->blame);
4538 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4539 s->blamed_commit, entry);
4540 err = run_blame(view);
4546 struct got_object_qid *first;
4547 first = SIMPLEQ_FIRST(&s->blamed_commits);
4548 if (!got_object_id_cmp(first->id, s->commit_id))
4551 thread_err = stop_blame(&s->blame);
4555 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4556 got_object_qid_free(s->blamed_commit);
4558 SIMPLEQ_FIRST(&s->blamed_commits);
4559 err = run_blame(view);
4566 struct got_object_id *id = NULL;
4567 struct got_object_qid *pid;
4568 struct got_commit_object *commit = NULL;
4569 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4570 s->first_displayed_line, s->selected_line);
4573 err = got_object_open_as_commit(&commit, s->repo, id);
4576 pid = SIMPLEQ_FIRST(
4577 got_object_commit_get_parent_ids(commit));
4578 if (view_is_parent_view(view))
4579 begin_x = view_split_begin_x(view->begin_x);
4580 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4581 if (diff_view == NULL) {
4582 got_object_commit_close(commit);
4583 err = got_error_from_errno("view_open");
4586 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4587 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4588 got_object_commit_close(commit);
4590 view_close(diff_view);
4594 diff_view->focussed = 1;
4595 if (view_is_parent_view(view)) {
4596 err = view_close_child(view);
4599 view_set_child(view, diff_view);
4600 view->focus_child = 1;
4602 *new_view = diff_view;
4610 if (s->last_displayed_line >= s->blame.nlines &&
4611 s->selected_line >= MIN(s->blame.nlines,
4612 view->nlines - 2)) {
4615 if (s->last_displayed_line >= s->blame.nlines &&
4616 s->selected_line < view->nlines - 2) {
4617 s->selected_line = MIN(s->blame.nlines,
4621 if (s->last_displayed_line + view->nlines - 2
4623 s->first_displayed_line +=
4626 s->first_displayed_line =
4631 if (s->selected_line > view->nlines - 2) {
4632 s->selected_line = MIN(s->blame.nlines,
4639 return thread_err ? thread_err : err;
4642 static const struct got_error *
4643 cmd_blame(int argc, char *argv[])
4645 const struct got_error *error;
4646 struct got_repository *repo = NULL;
4647 struct got_worktree *worktree = NULL;
4648 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4649 char *link_target = NULL;
4650 struct got_object_id *commit_id = NULL;
4651 char *commit_id_str = NULL;
4653 struct tog_view *view;
4655 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4658 commit_id_str = optarg;
4661 repo_path = realpath(optarg, NULL);
4662 if (repo_path == NULL)
4663 return got_error_from_errno2("realpath",
4678 if (repo_path == NULL) {
4679 cwd = getcwd(NULL, 0);
4681 return got_error_from_errno("getcwd");
4682 error = got_worktree_open(&worktree, cwd);
4683 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4687 strdup(got_worktree_get_repo_path(worktree));
4689 repo_path = strdup(cwd);
4690 if (repo_path == NULL) {
4691 error = got_error_from_errno("strdup");
4696 error = got_repo_open(&repo, repo_path, NULL);
4700 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4707 error = apply_unveil(got_repo_get_path(repo), NULL);
4711 if (commit_id_str == NULL) {
4712 struct got_reference *head_ref;
4713 error = got_ref_open(&head_ref, repo, worktree ?
4714 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4717 error = got_ref_resolve(&commit_id, repo, head_ref);
4718 got_ref_close(head_ref);
4720 error = got_repo_match_object_id(&commit_id, NULL,
4721 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4726 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4728 error = got_error_from_errno("view_open");
4732 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4737 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4742 /* Release work tree lock. */
4743 got_worktree_close(worktree);
4746 error = view_loop(view);
4754 got_worktree_close(worktree);
4756 got_repo_close(repo);
4760 static const struct got_error *
4761 draw_tree_entries(struct tog_view *view, const char *parent_path)
4763 struct tog_tree_view_state *s = &view->state.tree;
4764 const struct got_error *err = NULL;
4765 struct got_tree_entry *te;
4767 struct tog_color *tc;
4768 int width, n, i, nentries;
4769 int limit = view->nlines;
4773 werase(view->window);
4778 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4781 if (view_needs_focus_indication(view))
4782 wstandout(view->window);
4783 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4785 wattr_on(view->window,
4786 COLOR_PAIR(tc->colorpair), NULL);
4787 waddwstr(view->window, wline);
4789 wattr_off(view->window,
4790 COLOR_PAIR(tc->colorpair), NULL);
4791 if (view_needs_focus_indication(view))
4792 wstandend(view->window);
4795 if (width < view->ncols - 1)
4796 waddch(view->window, '\n');
4799 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4802 waddwstr(view->window, wline);
4805 if (width < view->ncols - 1)
4806 waddch(view->window, '\n');
4809 waddch(view->window, '\n');
4813 if (s->first_displayed_entry == NULL) {
4814 te = got_object_tree_get_first_entry(s->tree);
4815 if (s->selected == 0) {
4817 wstandout(view->window);
4818 s->selected_entry = NULL;
4820 waddstr(view->window, " ..\n"); /* parent directory */
4821 if (s->selected == 0 && view->focussed)
4822 wstandend(view->window);
4829 te = s->first_displayed_entry;
4832 nentries = got_object_tree_get_nentries(s->tree);
4833 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4834 char *line = NULL, *id_str = NULL, *link_target = NULL;
4835 const char *modestr = "";
4838 te = got_object_tree_get_entry(s->tree, i);
4839 mode = got_tree_entry_get_mode(te);
4842 err = got_object_id_str(&id_str,
4843 got_tree_entry_get_id(te));
4845 return got_error_from_errno(
4846 "got_object_id_str");
4848 if (got_object_tree_entry_is_submodule(te))
4850 else if (S_ISLNK(mode)) {
4853 err = got_tree_entry_get_symlink_target(&link_target,
4859 for (i = 0; i < strlen(link_target); i++) {
4860 if (!isprint((unsigned char)link_target[i]))
4861 link_target[i] = '?';
4865 else if (S_ISDIR(mode))
4867 else if (mode & S_IXUSR)
4869 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4870 got_tree_entry_get_name(te), modestr,
4871 link_target ? " -> ": "",
4872 link_target ? link_target : "") == -1) {
4875 return got_error_from_errno("asprintf");
4879 err = format_line(&wline, &width, line, view->ncols, 0);
4884 if (n == s->selected) {
4886 wstandout(view->window);
4887 s->selected_entry = te;
4889 tc = match_color(&s->colors, line);
4891 wattr_on(view->window,
4892 COLOR_PAIR(tc->colorpair), NULL);
4893 waddwstr(view->window, wline);
4895 wattr_off(view->window,
4896 COLOR_PAIR(tc->colorpair), NULL);
4897 if (width < view->ncols - 1)
4898 waddch(view->window, '\n');
4899 if (n == s->selected && view->focussed)
4900 wstandend(view->window);
4906 s->last_displayed_entry = te;
4915 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4917 struct got_tree_entry *te;
4918 int isroot = s->tree == s->root;
4921 if (s->first_displayed_entry == NULL)
4924 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4925 while (i++ < maxscroll) {
4928 s->first_displayed_entry = NULL;
4931 s->first_displayed_entry = te;
4932 te = got_tree_entry_get_prev(s->tree, te);
4937 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4939 struct got_tree_entry *next, *last;
4942 if (s->first_displayed_entry)
4943 next = got_tree_entry_get_next(s->tree,
4944 s->first_displayed_entry);
4946 next = got_object_tree_get_first_entry(s->tree);
4948 last = s->last_displayed_entry;
4949 while (next && last && n++ < maxscroll) {
4950 last = got_tree_entry_get_next(s->tree, last);
4952 s->first_displayed_entry = next;
4953 next = got_tree_entry_get_next(s->tree, next);
4958 static const struct got_error *
4959 tree_entry_path(char **path, struct tog_parent_trees *parents,
4960 struct got_tree_entry *te)
4962 const struct got_error *err = NULL;
4963 struct tog_parent_tree *pt;
4964 size_t len = 2; /* for leading slash and NUL */
4966 TAILQ_FOREACH(pt, parents, entry)
4967 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4970 len += strlen(got_tree_entry_get_name(te));
4972 *path = calloc(1, len);
4974 return got_error_from_errno("calloc");
4977 pt = TAILQ_LAST(parents, tog_parent_trees);
4979 const char *name = got_tree_entry_get_name(pt->selected_entry);
4980 if (strlcat(*path, name, len) >= len) {
4981 err = got_error(GOT_ERR_NO_SPACE);
4984 if (strlcat(*path, "/", len) >= len) {
4985 err = got_error(GOT_ERR_NO_SPACE);
4988 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4991 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4992 err = got_error(GOT_ERR_NO_SPACE);
5004 static const struct got_error *
5005 blame_tree_entry(struct tog_view **new_view, int begin_x,
5006 struct got_tree_entry *te, struct tog_parent_trees *parents,
5007 struct got_object_id *commit_id, struct got_repository *repo)
5009 const struct got_error *err = NULL;
5011 struct tog_view *blame_view;
5015 err = tree_entry_path(&path, parents, te);
5019 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5020 if (blame_view == NULL) {
5021 err = got_error_from_errno("view_open");
5025 err = open_blame_view(blame_view, path, commit_id, repo);
5027 if (err->code == GOT_ERR_CANCELLED)
5029 view_close(blame_view);
5031 *new_view = blame_view;
5037 static const struct got_error *
5038 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5039 struct tog_tree_view_state *s)
5041 struct tog_view *log_view;
5042 const struct got_error *err = NULL;
5047 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5048 if (log_view == NULL)
5049 return got_error_from_errno("view_open");
5051 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5055 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5058 view_close(log_view);
5060 *new_view = log_view;
5065 static const struct got_error *
5066 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5067 struct got_object_id *commit_id, const char *head_ref_name,
5068 struct got_repository *repo)
5070 const struct got_error *err = NULL;
5071 char *commit_id_str = NULL;
5072 struct tog_tree_view_state *s = &view->state.tree;
5074 TAILQ_INIT(&s->parents);
5076 err = got_object_id_str(&commit_id_str, commit_id);
5080 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5081 err = got_error_from_errno("asprintf");
5085 s->root = s->tree = root;
5086 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5087 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5088 s->commit_id = got_object_id_dup(commit_id);
5089 if (s->commit_id == NULL) {
5090 err = got_error_from_errno("got_object_id_dup");
5093 if (head_ref_name) {
5094 s->head_ref_name = strdup(head_ref_name);
5095 if (s->head_ref_name == NULL) {
5096 err = got_error_from_errno("strdup");
5102 SIMPLEQ_INIT(&s->colors);
5104 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5105 err = add_color(&s->colors, "\\$$",
5106 TOG_COLOR_TREE_SUBMODULE,
5107 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5110 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5111 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5113 free_colors(&s->colors);
5116 err = add_color(&s->colors, "/$",
5117 TOG_COLOR_TREE_DIRECTORY,
5118 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5120 free_colors(&s->colors);
5124 err = add_color(&s->colors, "\\*$",
5125 TOG_COLOR_TREE_EXECUTABLE,
5126 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5128 free_colors(&s->colors);
5132 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5133 get_color_value("TOG_COLOR_COMMIT"));
5135 free_colors(&s->colors);
5140 view->show = show_tree_view;
5141 view->input = input_tree_view;
5142 view->close = close_tree_view;
5143 view->search_start = search_start_tree_view;
5144 view->search_next = search_next_tree_view;
5146 free(commit_id_str);
5148 free(s->tree_label);
5149 s->tree_label = NULL;
5154 static const struct got_error *
5155 close_tree_view(struct tog_view *view)
5157 struct tog_tree_view_state *s = &view->state.tree;
5159 free_colors(&s->colors);
5160 free(s->tree_label);
5161 s->tree_label = NULL;
5163 s->commit_id = NULL;
5164 free(s->head_ref_name);
5165 s->head_ref_name = NULL;
5166 while (!TAILQ_EMPTY(&s->parents)) {
5167 struct tog_parent_tree *parent;
5168 parent = TAILQ_FIRST(&s->parents);
5169 TAILQ_REMOVE(&s->parents, parent, entry);
5173 if (s->tree != s->root)
5174 got_object_tree_close(s->tree);
5175 got_object_tree_close(s->root);
5179 static const struct got_error *
5180 search_start_tree_view(struct tog_view *view)
5182 struct tog_tree_view_state *s = &view->state.tree;
5184 s->matched_entry = NULL;
5189 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5191 regmatch_t regmatch;
5193 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5197 static const struct got_error *
5198 search_next_tree_view(struct tog_view *view)
5200 struct tog_tree_view_state *s = &view->state.tree;
5201 struct got_tree_entry *te = NULL;
5203 if (!view->searching) {
5204 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5208 if (s->matched_entry) {
5209 if (view->searching == TOG_SEARCH_FORWARD) {
5210 if (s->selected_entry)
5211 te = got_tree_entry_get_next(s->tree,
5214 te = got_object_tree_get_first_entry(s->tree);
5216 if (s->selected_entry == NULL)
5217 te = got_object_tree_get_last_entry(s->tree);
5219 te = got_tree_entry_get_prev(s->tree,
5223 if (view->searching == TOG_SEARCH_FORWARD)
5224 te = got_object_tree_get_first_entry(s->tree);
5226 te = got_object_tree_get_last_entry(s->tree);
5231 if (s->matched_entry == NULL) {
5232 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5235 if (view->searching == TOG_SEARCH_FORWARD)
5236 te = got_object_tree_get_first_entry(s->tree);
5238 te = got_object_tree_get_last_entry(s->tree);
5241 if (match_tree_entry(te, &view->regex)) {
5242 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5243 s->matched_entry = te;
5247 if (view->searching == TOG_SEARCH_FORWARD)
5248 te = got_tree_entry_get_next(s->tree, te);
5250 te = got_tree_entry_get_prev(s->tree, te);
5253 if (s->matched_entry) {
5254 s->first_displayed_entry = s->matched_entry;
5261 static const struct got_error *
5262 show_tree_view(struct tog_view *view)
5264 const struct got_error *err = NULL;
5265 struct tog_tree_view_state *s = &view->state.tree;
5268 err = tree_entry_path(&parent_path, &s->parents, NULL);
5272 err = draw_tree_entries(view, parent_path);
5279 static const struct got_error *
5280 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5282 const struct got_error *err = NULL;
5283 struct tog_tree_view_state *s = &view->state.tree;
5284 struct tog_view *log_view, *ref_view;
5289 s->show_ids = !s->show_ids;
5292 if (!s->selected_entry)
5294 if (view_is_parent_view(view))
5295 begin_x = view_split_begin_x(view->begin_x);
5296 err = log_selected_tree_entry(&log_view, begin_x, s);
5298 log_view->focussed = 1;
5299 if (view_is_parent_view(view)) {
5300 err = view_close_child(view);
5303 view_set_child(view, log_view);
5304 view->focus_child = 1;
5306 *new_view = log_view;
5309 if (view_is_parent_view(view))
5310 begin_x = view_split_begin_x(view->begin_x);
5311 ref_view = view_open(view->nlines, view->ncols,
5312 view->begin_y, begin_x, TOG_VIEW_REF);
5313 if (ref_view == NULL)
5314 return got_error_from_errno("view_open");
5315 err = open_ref_view(ref_view, s->repo);
5317 view_close(ref_view);
5321 ref_view->focussed = 1;
5322 if (view_is_parent_view(view)) {
5323 err = view_close_child(view);
5326 view_set_child(view, ref_view);
5327 view->focus_child = 1;
5329 *new_view = ref_view;
5333 if (s->selected > 0) {
5337 tree_scroll_up(s, 1);
5341 if (s->tree == s->root) {
5342 if (got_object_tree_get_first_entry(s->tree) ==
5343 s->first_displayed_entry)
5346 if (s->first_displayed_entry == NULL)
5349 tree_scroll_up(s, MAX(0, view->nlines - 3));
5353 if (s->selected < s->ndisplayed - 1) {
5357 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5359 /* can't scroll any further */
5361 tree_scroll_down(s, 1);
5365 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5367 /* can't scroll any further; move cursor down */
5368 if (s->selected < s->ndisplayed - 1)
5369 s->selected = s->ndisplayed - 1;
5372 tree_scroll_down(s, view->nlines - 3);
5377 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5378 struct tog_parent_tree *parent;
5379 /* user selected '..' */
5380 if (s->tree == s->root)
5382 parent = TAILQ_FIRST(&s->parents);
5383 TAILQ_REMOVE(&s->parents, parent,
5385 got_object_tree_close(s->tree);
5386 s->tree = parent->tree;
5387 s->first_displayed_entry =
5388 parent->first_displayed_entry;
5390 parent->selected_entry;
5391 s->selected = parent->selected;
5393 } else if (S_ISDIR(got_tree_entry_get_mode(
5394 s->selected_entry))) {
5395 struct got_tree_object *subtree;
5396 err = got_object_open_as_tree(&subtree, s->repo,
5397 got_tree_entry_get_id(s->selected_entry));
5400 err = tree_view_visit_subtree(s, subtree);
5402 got_object_tree_close(subtree);
5405 } else if (S_ISREG(got_tree_entry_get_mode(
5406 s->selected_entry))) {
5407 struct tog_view *blame_view;
5408 int begin_x = view_is_parent_view(view) ?
5409 view_split_begin_x(view->begin_x) : 0;
5411 err = blame_tree_entry(&blame_view, begin_x,
5412 s->selected_entry, &s->parents,
5413 s->commit_id, s->repo);
5417 blame_view->focussed = 1;
5418 if (view_is_parent_view(view)) {
5419 err = view_close_child(view);
5422 view_set_child(view, blame_view);
5423 view->focus_child = 1;
5425 *new_view = blame_view;
5429 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5430 s->selected = view->nlines - 4;
5443 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5448 static const struct got_error *
5449 cmd_tree(int argc, char *argv[])
5451 const struct got_error *error;
5452 struct got_repository *repo = NULL;
5453 struct got_worktree *worktree = NULL;
5454 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5455 struct got_object_id *commit_id = NULL;
5456 const char *commit_id_arg = NULL;
5458 struct got_commit_object *commit = NULL;
5459 struct got_tree_object *tree = NULL;
5460 struct got_reference *ref = NULL;
5461 const char *head_ref_name = NULL;
5463 struct tog_view *view;
5465 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5468 commit_id_arg = optarg;
5471 repo_path = realpath(optarg, NULL);
5472 if (repo_path == NULL)
5473 return got_error_from_errno2("realpath",
5488 if (repo_path == NULL) {
5489 cwd = getcwd(NULL, 0);
5491 return got_error_from_errno("getcwd");
5492 error = got_worktree_open(&worktree, cwd);
5493 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5497 strdup(got_worktree_get_repo_path(worktree));
5499 repo_path = strdup(cwd);
5500 if (repo_path == NULL) {
5501 error = got_error_from_errno("strdup");
5506 error = got_repo_open(&repo, repo_path, NULL);
5510 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5517 error = apply_unveil(got_repo_get_path(repo), NULL);
5521 if (commit_id_arg == NULL) {
5522 error = got_repo_match_object_id(&commit_id, &label,
5523 worktree ? got_worktree_get_head_ref_name(worktree) :
5524 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
5527 head_ref_name = label;
5529 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5531 head_ref_name = got_ref_get_name(ref);
5532 else if (error->code != GOT_ERR_NOT_REF)
5534 error = got_repo_match_object_id(&commit_id, NULL,
5535 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5540 error = got_object_open_as_commit(&commit, repo, commit_id);
5544 error = got_object_open_as_tree(&tree, repo,
5545 got_object_commit_get_tree_id(commit));
5549 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5551 error = got_error_from_errno("view_open");
5554 error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5557 if (!got_path_is_root_dir(in_repo_path)) {
5558 error = tree_view_walk_path(&view->state.tree, commit_id,
5565 /* Release work tree lock. */
5566 got_worktree_close(worktree);
5569 error = view_loop(view);
5578 got_object_commit_close(commit);
5580 got_object_tree_close(tree);
5582 got_repo_close(repo);
5586 static const struct got_error *
5587 ref_view_load_refs(struct tog_ref_view_state *s)
5589 const struct got_error *err;
5590 struct got_reflist_entry *sre;
5591 struct tog_reflist_entry *re;
5593 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5594 got_ref_cmp_by_name, NULL);
5599 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5600 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5603 re = malloc(sizeof(*re));
5605 return got_error_from_errno("malloc");
5608 re->idx = s->nrefs++;
5609 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5616 ref_view_free_refs(struct tog_ref_view_state *s)
5618 struct tog_reflist_entry *re;
5620 while (!TAILQ_EMPTY(&s->refs)) {
5621 re = TAILQ_FIRST(&s->refs);
5622 TAILQ_REMOVE(&s->refs, re, entry);
5625 got_ref_list_free(&s->simplerefs);
5628 static const struct got_error *
5629 open_ref_view(struct tog_view *view, struct got_repository *repo)
5631 const struct got_error *err = NULL;
5632 struct tog_ref_view_state *s = &view->state.ref;
5634 s->selected_entry = 0;
5637 SIMPLEQ_INIT(&s->simplerefs);
5638 TAILQ_INIT(&s->refs);
5639 SIMPLEQ_INIT(&s->colors);
5641 err = ref_view_load_refs(s);
5645 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5647 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5648 err = add_color(&s->colors, "^refs/heads/",
5649 TOG_COLOR_REFS_HEADS,
5650 get_color_value("TOG_COLOR_REFS_HEADS"));
5654 err = add_color(&s->colors, "^refs/tags/",
5655 TOG_COLOR_REFS_TAGS,
5656 get_color_value("TOG_COLOR_REFS_TAGS"));
5660 err = add_color(&s->colors, "^refs/remotes/",
5661 TOG_COLOR_REFS_REMOTES,
5662 get_color_value("TOG_COLOR_REFS_REMOTES"));
5667 view->show = show_ref_view;
5668 view->input = input_ref_view;
5669 view->close = close_ref_view;
5670 view->search_start = search_start_ref_view;
5671 view->search_next = search_next_ref_view;
5674 free_colors(&s->colors);
5678 static const struct got_error *
5679 close_ref_view(struct tog_view *view)
5681 struct tog_ref_view_state *s = &view->state.ref;
5683 ref_view_free_refs(s);
5684 free_colors(&s->colors);
5689 static const struct got_error *
5690 resolve_reflist_entry(struct got_object_id **commit_id,
5691 struct tog_reflist_entry *re, struct got_repository *repo)
5693 const struct got_error *err = NULL;
5694 struct got_object_id *obj_id;
5695 struct got_tag_object *tag = NULL;
5700 err = got_ref_resolve(&obj_id, repo, re->ref);
5704 err = got_object_get_type(&obj_type, repo, obj_id);
5709 case GOT_OBJ_TYPE_COMMIT:
5710 *commit_id = obj_id;
5712 case GOT_OBJ_TYPE_TAG:
5713 err = got_object_open_as_tag(&tag, repo, obj_id);
5717 err = got_object_get_type(&obj_type, repo,
5718 got_object_tag_get_object_id(tag));
5721 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5722 err = got_error(GOT_ERR_OBJ_TYPE);
5725 *commit_id = got_object_id_dup(
5726 got_object_tag_get_object_id(tag));
5727 if (*commit_id == NULL) {
5728 err = got_error_from_errno("got_object_id_dup");
5733 err = got_error(GOT_ERR_OBJ_TYPE);
5739 got_object_tag_close(tag);
5747 static const struct got_error *
5748 log_ref_entry(struct tog_view **new_view, int begin_x,
5749 struct tog_reflist_entry *re, struct got_repository *repo)
5751 struct tog_view *log_view;
5752 const struct got_error *err = NULL;
5753 struct got_object_id *commit_id = NULL;
5757 err = resolve_reflist_entry(&commit_id, re, repo);
5759 if (err->code != GOT_ERR_OBJ_TYPE)
5765 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5766 if (log_view == NULL) {
5767 err = got_error_from_errno("view_open");
5771 err = open_log_view(log_view, commit_id, repo,
5772 got_ref_get_name(re->ref), "", 0);
5775 view_close(log_view);
5777 *new_view = log_view;
5783 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5785 struct tog_reflist_entry *re;
5788 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5791 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5792 while (i++ < maxscroll) {
5795 s->first_displayed_entry = re;
5796 re = TAILQ_PREV(re, tog_reflist_head, entry);
5801 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5803 struct tog_reflist_entry *next, *last;
5806 if (s->first_displayed_entry)
5807 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5809 next = TAILQ_FIRST(&s->refs);
5811 last = s->last_displayed_entry;
5812 while (next && last && n++ < maxscroll) {
5813 last = TAILQ_NEXT(last, entry);
5815 s->first_displayed_entry = next;
5816 next = TAILQ_NEXT(next, entry);
5821 static const struct got_error *
5822 search_start_ref_view(struct tog_view *view)
5824 struct tog_ref_view_state *s = &view->state.ref;
5826 s->matched_entry = NULL;
5831 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5833 regmatch_t regmatch;
5835 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5839 static const struct got_error *
5840 search_next_ref_view(struct tog_view *view)
5842 struct tog_ref_view_state *s = &view->state.ref;
5843 struct tog_reflist_entry *re = NULL;
5845 if (!view->searching) {
5846 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5850 if (s->matched_entry) {
5851 if (view->searching == TOG_SEARCH_FORWARD) {
5852 if (s->selected_entry)
5853 re = TAILQ_NEXT(s->selected_entry, entry);
5855 re = TAILQ_PREV(s->selected_entry,
5856 tog_reflist_head, entry);
5858 if (s->selected_entry == NULL)
5859 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5861 re = TAILQ_PREV(s->selected_entry,
5862 tog_reflist_head, entry);
5865 if (view->searching == TOG_SEARCH_FORWARD)
5866 re = TAILQ_FIRST(&s->refs);
5868 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5873 if (s->matched_entry == NULL) {
5874 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5877 if (view->searching == TOG_SEARCH_FORWARD)
5878 re = TAILQ_FIRST(&s->refs);
5880 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5883 if (match_reflist_entry(re, &view->regex)) {
5884 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5885 s->matched_entry = re;
5889 if (view->searching == TOG_SEARCH_FORWARD)
5890 re = TAILQ_NEXT(re, entry);
5892 re = TAILQ_PREV(re, tog_reflist_head, entry);
5895 if (s->matched_entry) {
5896 s->first_displayed_entry = s->matched_entry;
5903 static const struct got_error *
5904 show_ref_view(struct tog_view *view)
5906 const struct got_error *err = NULL;
5907 struct tog_ref_view_state *s = &view->state.ref;
5908 struct tog_reflist_entry *re;
5911 struct tog_color *tc;
5913 int limit = view->nlines;
5915 werase(view->window);
5922 re = s->first_displayed_entry;
5924 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5926 return got_error_from_errno("asprintf");
5928 err = format_line(&wline, &width, line, view->ncols, 0);
5933 if (view_needs_focus_indication(view))
5934 wstandout(view->window);
5935 waddwstr(view->window, wline);
5936 if (view_needs_focus_indication(view))
5937 wstandend(view->window);
5942 if (width < view->ncols - 1)
5943 waddch(view->window, '\n');
5948 while (re && limit > 0) {
5951 if (got_ref_is_symbolic(re->ref)) {
5952 if (asprintf(&line, "%s -> %s",
5953 got_ref_get_name(re->ref),
5954 got_ref_get_symref_target(re->ref)) == -1)
5955 return got_error_from_errno("asprintf");
5956 } else if (s->show_ids) {
5957 struct got_object_id *id;
5959 err = got_ref_resolve(&id, s->repo, re->ref);
5962 err = got_object_id_str(&id_str, id);
5967 if (asprintf(&line, "%s: %s",
5968 got_ref_get_name(re->ref), id_str) == -1) {
5969 err = got_error_from_errno("asprintf");
5977 line = strdup(got_ref_get_name(re->ref));
5979 return got_error_from_errno("strdup");
5982 err = format_line(&wline, &width, line, view->ncols, 0);
5987 if (n == s->selected) {
5989 wstandout(view->window);
5990 s->selected_entry = re;
5992 tc = match_color(&s->colors, got_ref_get_name(re->ref));
5994 wattr_on(view->window,
5995 COLOR_PAIR(tc->colorpair), NULL);
5996 waddwstr(view->window, wline);
5998 wattr_off(view->window,
5999 COLOR_PAIR(tc->colorpair), NULL);
6000 if (width < view->ncols - 1)
6001 waddch(view->window, '\n');
6002 if (n == s->selected && view->focussed)
6003 wstandend(view->window);
6009 s->last_displayed_entry = re;
6012 re = TAILQ_NEXT(re, entry);
6019 static const struct got_error *
6020 browse_ref_tree(struct tog_view **new_view, int begin_x,
6021 struct tog_reflist_entry *re, struct got_repository *repo)
6023 const struct got_error *err = NULL;
6024 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6025 struct got_tree_object *tree = NULL;
6026 struct tog_view *tree_view;
6030 err = resolve_reflist_entry(&commit_id, re, repo);
6032 if (err->code != GOT_ERR_OBJ_TYPE)
6038 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6042 err = got_object_open_as_tree(&tree, repo, tree_id);
6046 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6047 if (tree_view == NULL) {
6048 err = got_error_from_errno("view_open");
6052 err = open_tree_view(tree_view, tree, commit_id,
6053 got_ref_get_name(re->ref), repo);
6057 *new_view = tree_view;
6063 got_object_tree_close(tree);
6067 static const struct got_error *
6068 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6070 const struct got_error *err = NULL;
6071 struct tog_ref_view_state *s = &view->state.ref;
6072 struct tog_view *log_view, *tree_view;
6077 s->show_ids = !s->show_ids;
6081 if (!s->selected_entry)
6083 if (view_is_parent_view(view))
6084 begin_x = view_split_begin_x(view->begin_x);
6085 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6088 log_view->focussed = 1;
6089 if (view_is_parent_view(view)) {
6090 err = view_close_child(view);
6093 view_set_child(view, log_view);
6094 view->focus_child = 1;
6096 *new_view = log_view;
6099 if (!s->selected_entry)
6101 if (view_is_parent_view(view))
6102 begin_x = view_split_begin_x(view->begin_x);
6103 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6105 if (err || tree_view == NULL)
6108 tree_view->focussed = 1;
6109 if (view_is_parent_view(view)) {
6110 err = view_close_child(view);
6113 view_set_child(view, tree_view);
6114 view->focus_child = 1;
6116 *new_view = tree_view;
6120 if (s->selected > 0) {
6124 ref_scroll_up(s, 1);
6128 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6130 ref_scroll_up(s, MAX(0, view->nlines - 1));
6134 if (s->selected < s->ndisplayed - 1) {
6138 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6139 /* can't scroll any further */
6141 ref_scroll_down(s, 1);
6145 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6146 /* can't scroll any further; move cursor down */
6147 if (s->selected < s->ndisplayed - 1)
6148 s->selected = s->ndisplayed - 1;
6151 ref_scroll_down(s, view->nlines - 1);
6154 ref_view_free_refs(s);
6155 err = ref_view_load_refs(s);
6158 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6159 s->selected = view->nlines - 2;
6172 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6177 static const struct got_error *
6178 cmd_ref(int argc, char *argv[])
6180 const struct got_error *error;
6181 struct got_repository *repo = NULL;
6182 struct got_worktree *worktree = NULL;
6183 char *cwd = NULL, *repo_path = NULL;
6185 struct tog_view *view;
6187 while ((ch = getopt(argc, argv, "r:")) != -1) {
6190 repo_path = realpath(optarg, NULL);
6191 if (repo_path == NULL)
6192 return got_error_from_errno2("realpath",
6207 if (repo_path == NULL) {
6208 cwd = getcwd(NULL, 0);
6210 return got_error_from_errno("getcwd");
6211 error = got_worktree_open(&worktree, cwd);
6212 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6216 strdup(got_worktree_get_repo_path(worktree));
6218 repo_path = strdup(cwd);
6219 if (repo_path == NULL) {
6220 error = got_error_from_errno("strdup");
6225 error = got_repo_open(&repo, repo_path, NULL);
6231 error = apply_unveil(got_repo_get_path(repo), NULL);
6235 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6237 error = got_error_from_errno("view_open");
6241 error = open_ref_view(view, repo);
6246 /* Release work tree lock. */
6247 got_worktree_close(worktree);
6250 error = view_loop(view);
6255 got_repo_close(repo);
6260 list_commands(FILE *fp)
6264 fprintf(fp, "commands:");
6265 for (i = 0; i < nitems(tog_commands); i++) {
6266 struct tog_cmd *cmd = &tog_commands[i];
6267 fprintf(fp, " %s", cmd->name);
6273 usage(int hflag, int status)
6275 FILE *fp = (status == 0) ? stdout : stderr;
6277 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6280 fprintf(fp, "lazy usage: %s path\n", getprogname());
6287 make_argv(int argc, ...)
6295 argv = calloc(argc, sizeof(char *));
6298 for (i = 0; i < argc; i++) {
6299 argv[i] = strdup(va_arg(ap, char *));
6300 if (argv[i] == NULL)
6309 * Try to convert 'tog path' into a 'tog log path' command.
6310 * The user could simply have mistyped the command rather than knowingly
6311 * provided a path. So check whether argv[0] can in fact be resolved
6312 * to a path in the HEAD commit and print a special error if not.
6313 * This hack is for mpi@ <3
6315 static const struct got_error *
6316 tog_log_with_path(int argc, char *argv[])
6318 const struct got_error *error = NULL;
6319 struct tog_cmd *cmd = NULL;
6320 struct got_repository *repo = NULL;
6321 struct got_worktree *worktree = NULL;
6322 struct got_object_id *commit_id = NULL, *id = NULL;
6323 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6324 char *commit_id_str = NULL, **cmd_argv = NULL;
6326 cwd = getcwd(NULL, 0);
6328 return got_error_from_errno("getcwd");
6330 error = got_worktree_open(&worktree, cwd);
6331 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6335 repo_path = strdup(got_worktree_get_repo_path(worktree));
6337 repo_path = strdup(cwd);
6338 if (repo_path == NULL) {
6339 error = got_error_from_errno("strdup");
6343 error = got_repo_open(&repo, repo_path, NULL);
6347 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6352 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6353 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6354 GOT_OBJ_TYPE_COMMIT, 1, repo);
6359 got_worktree_close(worktree);
6363 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6365 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6367 fprintf(stderr, "%s: '%s' is no known command or path\n",
6368 getprogname(), argv[0]);
6373 got_repo_close(repo);
6376 error = got_object_id_str(&commit_id_str, commit_id);
6380 cmd = &tog_commands[0]; /* log */
6382 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6383 error = cmd->cmd_main(argc, cmd_argv);
6386 got_repo_close(repo);
6388 got_worktree_close(worktree);
6390 free(commit_id_str);
6397 for (i = 0; i < argc; i++)
6405 main(int argc, char *argv[])
6407 const struct got_error *error = NULL;
6408 struct tog_cmd *cmd = NULL;
6409 int ch, hflag = 0, Vflag = 0;
6410 char **cmd_argv = NULL;
6411 static struct option longopts[] = {
6412 { "version", no_argument, NULL, 'V' },
6416 setlocale(LC_CTYPE, "");
6418 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6438 got_version_print_str();
6443 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6451 /* Build an argument vector which runs a default command. */
6452 cmd = &tog_commands[0];
6454 cmd_argv = make_argv(argc, cmd->name);
6458 /* Did the user specify a command? */
6459 for (i = 0; i < nitems(tog_commands); i++) {
6460 if (strncmp(tog_commands[i].name, argv[0],
6461 strlen(argv[0])) == 0) {
6462 cmd = &tog_commands[i];
6471 /* No command specified; try log with a path */
6472 error = tog_log_with_path(argc, argv);
6477 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6484 for (i = 0; i < argc; i++)
6489 if (error && error->code != GOT_ERR_CANCELLED)
6490 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);