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>
22 #define _XOPEN_SOURCE_EXTENDED
24 #undef _XOPEN_SOURCE_EXTENDED
42 #include "got_version.h"
43 #include "got_error.h"
44 #include "got_object.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
48 #include "got_opentemp.h"
50 #include "got_cancel.h"
51 #include "got_commit_graph.h"
52 #include "got_blame.h"
53 #include "got_privsep.h"
55 #include "got_worktree.h"
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 #define CTRL(x) ((x) & 0x1f)
68 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
73 const struct got_error *(*cmd_main)(int, char *[]);
74 void (*cmd_usage)(void);
77 __dead static void usage(int);
78 __dead static void usage_log(void);
79 __dead static void usage_diff(void);
80 __dead static void usage_blame(void);
81 __dead static void usage_tree(void);
83 static const struct got_error* cmd_log(int, char *[]);
84 static const struct got_error* cmd_diff(int, char *[]);
85 static const struct got_error* cmd_blame(int, char *[]);
86 static const struct got_error* cmd_tree(int, char *[]);
88 static struct tog_cmd tog_commands[] = {
89 { "log", cmd_log, usage_log },
90 { "diff", cmd_diff, usage_diff },
91 { "blame", cmd_blame, usage_blame },
92 { "tree", cmd_tree, usage_tree },
102 #define TOG_EOF_STRING "(END)"
104 struct commit_queue_entry {
105 TAILQ_ENTRY(commit_queue_entry) entry;
106 struct got_object_id *id;
107 struct got_commit_object *commit;
110 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
111 struct commit_queue {
113 struct commit_queue_head head;
117 SIMPLEQ_ENTRY(tog_color) entry;
121 SIMPLEQ_HEAD(tog_colors, tog_color);
123 static const struct got_error *
124 add_color(struct tog_colors *colors, const char *pattern,
125 int idx, short color)
127 const struct got_error *err = NULL;
128 struct tog_color *tc;
131 if (idx < 1 || idx > COLOR_PAIRS - 1)
134 init_pair(idx, color, -1);
136 tc = calloc(1, sizeof(*tc));
138 return got_error_from_errno("calloc");
139 regerr = regcomp(&tc->regex, pattern,
140 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
142 static char regerr_msg[512];
143 static char err_msg[512];
144 regerror(regerr, &tc->regex, regerr_msg,
146 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
148 err = got_error_msg(GOT_ERR_REGEX, err_msg);
153 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
158 free_colors(struct tog_colors *colors)
160 struct tog_color *tc;
162 while (!SIMPLEQ_EMPTY(colors)) {
163 tc = SIMPLEQ_FIRST(colors);
164 SIMPLEQ_REMOVE_HEAD(colors, entry);
171 get_color(struct tog_colors *colors, int colorpair)
173 struct tog_color *tc = NULL;
175 SIMPLEQ_FOREACH(tc, colors, entry) {
176 if (tc->colorpair == colorpair)
184 default_color_value(const char *envvar)
186 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
187 return COLOR_MAGENTA;
188 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
190 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
192 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
194 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
195 return COLOR_MAGENTA;
196 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
197 return COLOR_MAGENTA;
198 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
200 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
202 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
204 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
206 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
213 get_color_value(const char *envvar)
215 const char *val = getenv(envvar);
218 return default_color_value(envvar);
220 if (strcasecmp(val, "black") == 0)
222 if (strcasecmp(val, "red") == 0)
224 if (strcasecmp(val, "green") == 0)
226 if (strcasecmp(val, "yellow") == 0)
228 if (strcasecmp(val, "blue") == 0)
230 if (strcasecmp(val, "magenta") == 0)
231 return COLOR_MAGENTA;
232 if (strcasecmp(val, "cyan") == 0)
234 if (strcasecmp(val, "white") == 0)
236 if (strcasecmp(val, "default") == 0)
239 return default_color_value(envvar);
243 struct tog_diff_view_state {
244 struct got_object_id *id1, *id2;
246 int first_displayed_line;
247 int last_displayed_line;
250 struct got_repository *repo;
251 struct got_reflist_head *refs;
252 struct tog_colors colors;
259 /* passed from log view; may be NULL */
260 struct tog_view *log_view;
263 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
265 struct tog_log_thread_args {
266 pthread_cond_t need_commits;
268 struct got_commit_graph *graph;
269 struct commit_queue *commits;
270 const char *in_repo_path;
271 struct got_object_id *start_id;
272 struct got_repository *repo;
275 struct commit_queue_entry **first_displayed_entry;
276 struct commit_queue_entry **selected_entry;
278 int *search_next_done;
282 struct tog_log_view_state {
283 struct commit_queue commits;
284 struct commit_queue_entry *first_displayed_entry;
285 struct commit_queue_entry *last_displayed_entry;
286 struct commit_queue_entry *selected_entry;
289 const char *head_ref_name;
291 struct got_repository *repo;
292 struct got_reflist_head *refs;
293 struct got_object_id *start_id;
296 struct tog_log_thread_args thread_args;
297 struct commit_queue_entry *matched_entry;
298 struct commit_queue_entry *search_entry;
299 struct tog_colors colors;
302 #define TOG_COLOR_DIFF_MINUS 1
303 #define TOG_COLOR_DIFF_PLUS 2
304 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
305 #define TOG_COLOR_DIFF_META 4
306 #define TOG_COLOR_TREE_SUBMODULE 5
307 #define TOG_COLOR_TREE_SYMLINK 6
308 #define TOG_COLOR_TREE_DIRECTORY 7
309 #define TOG_COLOR_TREE_EXECUTABLE 8
310 #define TOG_COLOR_COMMIT 9
311 #define TOG_COLOR_AUTHOR 10
312 #define TOG_COLOR_DATE 11
314 struct tog_blame_cb_args {
315 struct tog_blame_line *lines; /* one per line */
318 struct tog_view *view;
319 struct got_object_id *commit_id;
323 struct tog_blame_thread_args {
325 struct got_repository *repo;
326 struct tog_blame_cb_args *cb_args;
328 got_cancel_cb cancel_cb;
335 struct tog_blame_line *lines;
339 struct tog_blame_thread_args thread_args;
340 struct tog_blame_cb_args cb_args;
344 struct tog_blame_view_state {
345 int first_displayed_line;
346 int last_displayed_line;
351 struct got_object_id_queue blamed_commits;
352 struct got_object_qid *blamed_commit;
354 struct got_repository *repo;
355 struct got_reflist_head *refs;
356 struct got_object_id *commit_id;
357 struct tog_blame blame;
359 struct tog_colors colors;
362 struct tog_parent_tree {
363 TAILQ_ENTRY(tog_parent_tree) entry;
364 struct got_tree_object *tree;
365 struct got_tree_entry *first_displayed_entry;
366 struct got_tree_entry *selected_entry;
370 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
372 struct tog_tree_view_state {
374 struct got_tree_object *root;
375 struct got_tree_object *tree;
376 struct got_tree_entry *first_displayed_entry;
377 struct got_tree_entry *last_displayed_entry;
378 struct got_tree_entry *selected_entry;
379 int ndisplayed, selected, show_ids;
380 struct tog_parent_trees parents;
381 struct got_object_id *commit_id;
382 struct got_repository *repo;
383 struct got_reflist_head *refs;
384 struct got_tree_entry *matched_entry;
385 struct tog_colors colors;
389 * We implement two types of views: parent views and child views.
391 * The 'Tab' key switches between a parent view and its child view.
392 * Child views are shown side-by-side to their parent view, provided
393 * there is enough screen estate.
395 * When a new view is opened from within a parent view, this new view
396 * becomes a child view of the parent view, replacing any existing child.
398 * When a new view is opened from within a child view, this new view
399 * becomes a parent view which will obscure the views below until the
400 * user quits the new parent view by typing 'q'.
402 * This list of views contains parent views only.
403 * Child views are only pointed to by their parent view.
405 TAILQ_HEAD(tog_view_list_head, tog_view);
408 TAILQ_ENTRY(tog_view) entry;
411 int nlines, ncols, begin_y, begin_x;
412 int lines, cols; /* copies of LINES and COLS */
414 struct tog_view *parent;
415 struct tog_view *child;
418 /* type-specific state */
419 enum tog_view_type type;
421 struct tog_diff_view_state diff;
422 struct tog_log_view_state log;
423 struct tog_blame_view_state blame;
424 struct tog_tree_view_state tree;
427 const struct got_error *(*show)(struct tog_view *);
428 const struct got_error *(*input)(struct tog_view **,
429 struct tog_view **, struct tog_view**, struct tog_view *, int);
430 const struct got_error *(*close)(struct tog_view *);
432 const struct got_error *(*search_start)(struct tog_view *);
433 const struct got_error *(*search_next)(struct tog_view *);
435 #define TOG_SEARCH_FORWARD 1
436 #define TOG_SEARCH_BACKWARD 2
437 int search_next_done;
441 static const struct got_error *open_diff_view(struct tog_view *,
442 struct got_object_id *, struct got_object_id *, struct tog_view *,
443 struct got_reflist_head *, struct got_repository *);
444 static const struct got_error *show_diff_view(struct tog_view *);
445 static const struct got_error *input_diff_view(struct tog_view **,
446 struct tog_view **, struct tog_view **, struct tog_view *, int);
447 static const struct got_error* close_diff_view(struct tog_view *);
448 static const struct got_error *search_start_diff_view(struct tog_view *);
449 static const struct got_error *search_next_diff_view(struct tog_view *);
451 static const struct got_error *open_log_view(struct tog_view *,
452 struct got_object_id *, struct got_reflist_head *,
453 struct got_repository *, const char *, const char *, int);
454 static const struct got_error * show_log_view(struct tog_view *);
455 static const struct got_error *input_log_view(struct tog_view **,
456 struct tog_view **, struct tog_view **, struct tog_view *, int);
457 static const struct got_error *close_log_view(struct tog_view *);
458 static const struct got_error *search_start_log_view(struct tog_view *);
459 static const struct got_error *search_next_log_view(struct tog_view *);
461 static const struct got_error *open_blame_view(struct tog_view *, char *,
462 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
463 static const struct got_error *show_blame_view(struct tog_view *);
464 static const struct got_error *input_blame_view(struct tog_view **,
465 struct tog_view **, struct tog_view **, struct tog_view *, int);
466 static const struct got_error *close_blame_view(struct tog_view *);
467 static const struct got_error *search_start_blame_view(struct tog_view *);
468 static const struct got_error *search_next_blame_view(struct tog_view *);
470 static const struct got_error *open_tree_view(struct tog_view *,
471 struct got_tree_object *, struct got_object_id *, struct got_reflist_head *,
472 struct got_repository *);
473 static const struct got_error *show_tree_view(struct tog_view *);
474 static const struct got_error *input_tree_view(struct tog_view **,
475 struct tog_view **, struct tog_view **, struct tog_view *, int);
476 static const struct got_error *close_tree_view(struct tog_view *);
477 static const struct got_error *search_start_tree_view(struct tog_view *);
478 static const struct got_error *search_next_tree_view(struct tog_view *);
480 static volatile sig_atomic_t tog_sigwinch_received;
481 static volatile sig_atomic_t tog_sigpipe_received;
482 static volatile sig_atomic_t tog_sigcont_received;
485 tog_sigwinch(int signo)
487 tog_sigwinch_received = 1;
491 tog_sigpipe(int signo)
493 tog_sigpipe_received = 1;
497 tog_sigcont(int signo)
499 tog_sigcont_received = 1;
502 static const struct got_error *
503 view_close(struct tog_view *view)
505 const struct got_error *err = NULL;
508 view_close(view->child);
512 err = view->close(view);
514 del_panel(view->panel);
516 delwin(view->window);
521 static struct tog_view *
522 view_open(int nlines, int ncols, int begin_y, int begin_x,
523 enum tog_view_type type)
525 struct tog_view *view = calloc(1, sizeof(*view));
533 view->nlines = nlines ? nlines : LINES - begin_y;
534 view->ncols = ncols ? ncols : COLS - begin_x;
535 view->begin_y = begin_y;
536 view->begin_x = begin_x;
537 view->window = newwin(nlines, ncols, begin_y, begin_x);
538 if (view->window == NULL) {
542 view->panel = new_panel(view->window);
543 if (view->panel == NULL ||
544 set_panel_userptr(view->panel, view) != OK) {
549 keypad(view->window, TRUE);
554 view_split_begin_x(int begin_x)
556 if (begin_x > 0 || COLS < 120)
558 return (COLS - MAX(COLS / 2, 80));
561 static const struct got_error *view_resize(struct tog_view *);
563 static const struct got_error *
564 view_splitscreen(struct tog_view *view)
566 const struct got_error *err = NULL;
569 view->begin_x = view_split_begin_x(0);
570 view->nlines = LINES;
571 view->ncols = COLS - view->begin_x;
574 err = view_resize(view);
578 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
579 return got_error_from_errno("mvwin");
584 static const struct got_error *
585 view_fullscreen(struct tog_view *view)
587 const struct got_error *err = NULL;
591 view->nlines = LINES;
595 err = view_resize(view);
599 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
600 return got_error_from_errno("mvwin");
606 view_is_parent_view(struct tog_view *view)
608 return view->parent == NULL;
611 static const struct got_error *
612 view_resize(struct tog_view *view)
616 if (view->lines > LINES)
617 nlines = view->nlines - (view->lines - LINES);
619 nlines = view->nlines + (LINES - view->lines);
621 if (view->cols > COLS)
622 ncols = view->ncols - (view->cols - COLS);
624 ncols = view->ncols + (COLS - view->cols);
626 if (wresize(view->window, nlines, ncols) == ERR)
627 return got_error_from_errno("wresize");
628 if (replace_panel(view->panel, view->window) == ERR)
629 return got_error_from_errno("replace_panel");
630 wclear(view->window);
632 view->nlines = nlines;
638 view->child->begin_x = view_split_begin_x(view->begin_x);
639 if (view->child->begin_x == 0) {
640 view_fullscreen(view->child);
641 if (view->child->focussed)
642 show_panel(view->child->panel);
644 show_panel(view->panel);
646 view_splitscreen(view->child);
647 show_panel(view->child->panel);
654 static const struct got_error *
655 view_close_child(struct tog_view *view)
657 const struct got_error *err = NULL;
659 if (view->child == NULL)
662 err = view_close(view->child);
667 static const struct got_error *
668 view_set_child(struct tog_view *view, struct tog_view *child)
670 const struct got_error *err = NULL;
673 child->parent = view;
678 view_is_splitscreen(struct tog_view *view)
680 return view->begin_x > 0;
689 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
690 cols = 80; /* Default */
696 resize_term(lines, cols);
699 static const struct got_error *
700 view_search_start(struct tog_view *view)
702 const struct got_error *err = NULL;
706 if (view->nlines < 1)
709 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
710 wclrtoeol(view->window);
714 ret = wgetnstr(view->window, pattern, sizeof(pattern));
720 if (view->searching) {
721 regfree(&view->regex);
725 if (regcomp(&view->regex, pattern,
726 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
727 err = view->search_start(view);
729 regfree(&view->regex);
732 view->searching = TOG_SEARCH_FORWARD;
733 view->search_next_done = 0;
734 view->search_next(view);
740 static const struct got_error *
741 view_input(struct tog_view **new, struct tog_view **dead,
742 struct tog_view **focus, int *done, struct tog_view *view,
743 struct tog_view_list_head *views)
745 const struct got_error *err = NULL;
753 if (view->searching && !view->search_next_done) {
754 errcode = pthread_mutex_unlock(&tog_mutex);
756 return got_error_set_errno(errcode,
757 "pthread_mutex_unlock");
759 errcode = pthread_mutex_lock(&tog_mutex);
761 return got_error_set_errno(errcode,
762 "pthread_mutex_lock");
763 view->search_next(view);
767 nodelay(stdscr, FALSE);
768 /* Allow threads to make progress while we are waiting for input. */
769 errcode = pthread_mutex_unlock(&tog_mutex);
771 return got_error_set_errno(errcode, "pthread_mutex_unlock");
772 ch = wgetch(view->window);
773 errcode = pthread_mutex_lock(&tog_mutex);
775 return got_error_set_errno(errcode, "pthread_mutex_lock");
776 nodelay(stdscr, TRUE);
778 if (tog_sigwinch_received || tog_sigcont_received) {
780 tog_sigwinch_received = 0;
781 tog_sigcont_received = 0;
782 TAILQ_FOREACH(v, views, entry) {
783 err = view_resize(v);
786 err = v->input(new, dead, focus, v, KEY_RESIZE);
797 *focus = view->child;
798 view->child_focussed = 1;
799 } else if (view->parent) {
800 *focus = view->parent;
801 view->parent->child_focussed = 0;
805 err = view->input(new, dead, focus, view, ch);
812 if (view_is_parent_view(view)) {
813 if (view->child == NULL)
815 if (view_is_splitscreen(view->child)) {
816 *focus = view->child;
817 view->child_focussed = 1;
818 err = view_fullscreen(view->child);
820 err = view_splitscreen(view->child);
823 err = view->child->input(new, dead, focus,
824 view->child, KEY_RESIZE);
826 if (view_is_splitscreen(view)) {
828 view->parent->child_focussed = 1;
829 err = view_fullscreen(view);
831 err = view_splitscreen(view);
835 err = view->input(new, dead, focus, view,
842 if (view->search_start)
843 view_search_start(view);
845 err = view->input(new, dead, focus, view, ch);
849 if (view->search_next && view->searching) {
850 view->searching = (ch == 'n' ?
851 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
852 view->search_next_done = 0;
853 view->search_next(view);
855 err = view->input(new, dead, focus, view, ch);
858 err = view->input(new, dead, focus, view, ch);
866 view_vborder(struct tog_view *view)
869 struct tog_view *view_above;
872 return view_vborder(view->parent);
874 panel = panel_above(view->panel);
878 view_above = panel_userptr(panel);
879 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
880 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
884 view_needs_focus_indication(struct tog_view *view)
886 if (view_is_parent_view(view)) {
887 if (view->child == NULL || view->child_focussed)
889 if (!view_is_splitscreen(view->child))
891 } else if (!view_is_splitscreen(view))
894 return view->focussed;
897 static const struct got_error *
898 view_loop(struct tog_view *view)
900 const struct got_error *err = NULL;
901 struct tog_view_list_head views;
902 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
903 int fast_refresh = 10;
904 int done = 0, errcode;
906 errcode = pthread_mutex_lock(&tog_mutex);
908 return got_error_set_errno(errcode, "pthread_mutex_lock");
911 TAILQ_INSERT_HEAD(&views, view, entry);
915 err = view->show(view);
920 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
921 /* Refresh fast during initialization, then become slower. */
922 if (fast_refresh && fast_refresh-- == 0)
923 halfdelay(10); /* switch to once per second */
925 err = view_input(&new_view, &dead_view, &focus_view, &done,
930 struct tog_view *prev = NULL;
932 if (view_is_parent_view(dead_view))
933 prev = TAILQ_PREV(dead_view,
934 tog_view_list_head, entry);
935 else if (view->parent != dead_view)
938 if (dead_view->parent)
939 dead_view->parent->child = NULL;
941 TAILQ_REMOVE(&views, dead_view, entry);
943 err = view_close(dead_view);
944 if (err || (dead_view == main_view && new_view == NULL))
947 if (view == dead_view) {
952 else if (!TAILQ_EMPTY(&views))
953 view = TAILQ_LAST(&views,
958 if (view->child && view->child_focussed)
959 focus_view = view->child;
966 struct tog_view *v, *t;
967 /* Only allow one parent view per type. */
968 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
969 if (v->type != new_view->type)
971 TAILQ_REMOVE(&views, v, entry);
977 TAILQ_INSERT_TAIL(&views, new_view, entry);
979 if (focus_view == NULL)
980 focus_view = new_view;
983 show_panel(focus_view->panel);
986 focus_view->focussed = 1;
989 show_panel(new_view->panel);
990 if (view->child && view_is_splitscreen(view->child))
991 show_panel(view->child->panel);
994 if (focus_view == NULL) {
996 show_panel(view->panel);
997 if (view->child && view_is_splitscreen(view->child))
998 show_panel(view->child->panel);
1002 err = view->parent->show(view->parent);
1006 err = view->show(view);
1010 err = view->child->show(view->child);
1019 while (!TAILQ_EMPTY(&views)) {
1020 view = TAILQ_FIRST(&views);
1021 TAILQ_REMOVE(&views, view, entry);
1025 errcode = pthread_mutex_unlock(&tog_mutex);
1026 if (errcode && err == NULL)
1027 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1037 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1042 /* Create newly allocated wide-character string equivalent to a byte string. */
1043 static const struct got_error *
1044 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1047 const struct got_error *err = NULL;
1050 *wlen = mbstowcs(NULL, s, 0);
1051 if (*wlen == (size_t)-1) {
1053 if (errno != EILSEQ)
1054 return got_error_from_errno("mbstowcs");
1056 /* byte string invalid in current encoding; try to "fix" it */
1057 err = got_mbsavis(&vis, &vislen, s);
1060 *wlen = mbstowcs(NULL, vis, 0);
1061 if (*wlen == (size_t)-1) {
1062 err = got_error_from_errno("mbstowcs"); /* give up */
1067 *ws = calloc(*wlen + 1, sizeof(**ws));
1069 err = got_error_from_errno("calloc");
1073 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1074 err = got_error_from_errno("mbstowcs");
1085 /* Format a line for display, ensuring that it won't overflow a width limit. */
1086 static const struct got_error *
1087 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1090 const struct got_error *err = NULL;
1092 wchar_t *wline = NULL;
1099 err = mbs2ws(&wline, &wlen, line);
1105 int width = wcwidth(wline[i]);
1112 if (width == 1 || width == 2) {
1113 if (cols + width > wlimit)
1117 } else if (width == -1) {
1118 if (wline[i] == L'\t') {
1120 ((cols + col_tab_align) % TABSIZE);
1121 if (cols + width > wlimit)
1127 err = got_error_from_errno("wcwidth");
1142 static const struct got_error*
1143 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1144 struct got_object_id *id, struct got_repository *repo)
1146 static const struct got_error *err = NULL;
1147 struct got_reflist_entry *re;
1153 SIMPLEQ_FOREACH(re, refs, entry) {
1154 struct got_tag_object *tag = NULL;
1157 name = got_ref_get_name(re->ref);
1158 if (strcmp(name, GOT_REF_HEAD) == 0)
1160 if (strncmp(name, "refs/", 5) == 0)
1162 if (strncmp(name, "got/", 4) == 0)
1164 if (strncmp(name, "heads/", 6) == 0)
1166 if (strncmp(name, "remotes/", 8) == 0)
1168 if (strncmp(name, "tags/", 5) == 0) {
1169 err = got_object_open_as_tag(&tag, repo, re->id);
1171 if (err->code != GOT_ERR_OBJ_TYPE)
1173 /* Ref points at something other than a tag. */
1178 cmp = got_object_id_cmp(tag ?
1179 got_object_tag_get_object_id(tag) : re->id, id);
1181 got_object_tag_close(tag);
1185 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1186 s ? ", " : "", name) == -1) {
1187 err = got_error_from_errno("asprintf");
1198 static const struct got_error *
1199 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1202 char *smallerthan, *at;
1204 smallerthan = strchr(author, '<');
1205 if (smallerthan && smallerthan[1] != '\0')
1206 author = smallerthan + 1;
1207 at = strchr(author, '@');
1210 return format_line(wauthor, author_width, author, limit, col_tab_align);
1213 static const struct got_error *
1214 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1215 struct got_object_id *id, struct got_reflist_head *refs,
1216 const size_t date_display_cols, int author_display_cols,
1217 struct tog_colors *colors)
1219 const struct got_error *err = NULL;
1220 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1221 char *logmsg0 = NULL, *logmsg = NULL;
1222 char *author = NULL;
1223 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1224 int author_width, logmsg_width;
1225 char *newline, *line = NULL;
1227 const int avail = view->ncols;
1229 time_t committer_time;
1230 struct tog_color *tc;
1232 committer_time = got_object_commit_get_committer_time(commit);
1233 if (localtime_r(&committer_time, &tm) == NULL)
1234 return got_error_from_errno("localtime_r");
1235 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1237 return got_error(GOT_ERR_NO_SPACE);
1239 if (avail <= date_display_cols)
1240 limit = MIN(sizeof(datebuf) - 1, avail);
1242 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1243 tc = get_color(colors, TOG_COLOR_DATE);
1245 wattr_on(view->window,
1246 COLOR_PAIR(tc->colorpair), NULL);
1247 waddnstr(view->window, datebuf, limit);
1249 wattr_off(view->window,
1250 COLOR_PAIR(tc->colorpair), NULL);
1257 err = got_object_id_str(&id_str, id);
1260 tc = get_color(colors, TOG_COLOR_COMMIT);
1262 wattr_on(view->window,
1263 COLOR_PAIR(tc->colorpair), NULL);
1264 wprintw(view->window, "%.8s ", id_str);
1266 wattr_off(view->window,
1267 COLOR_PAIR(tc->colorpair), NULL);
1274 author = strdup(got_object_commit_get_author(commit));
1275 if (author == NULL) {
1276 err = got_error_from_errno("strdup");
1279 err = format_author(&wauthor, &author_width, author, avail - col, col);
1282 tc = get_color(colors, TOG_COLOR_AUTHOR);
1284 wattr_on(view->window,
1285 COLOR_PAIR(tc->colorpair), NULL);
1286 waddwstr(view->window, wauthor);
1288 wattr_off(view->window,
1289 COLOR_PAIR(tc->colorpair), NULL);
1290 col += author_width;
1291 while (col < avail && author_width < author_display_cols + 2) {
1292 waddch(view->window, ' ');
1299 err = got_object_commit_get_logmsg(&logmsg0, commit);
1303 while (*logmsg == '\n')
1305 newline = strchr(logmsg, '\n');
1308 limit = avail - col;
1309 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1312 waddwstr(view->window, wlogmsg);
1313 col += logmsg_width;
1314 while (col < avail) {
1315 waddch(view->window, ' ');
1327 static struct commit_queue_entry *
1328 alloc_commit_queue_entry(struct got_commit_object *commit,
1329 struct got_object_id *id)
1331 struct commit_queue_entry *entry;
1333 entry = calloc(1, sizeof(*entry));
1338 entry->commit = commit;
1343 pop_commit(struct commit_queue *commits)
1345 struct commit_queue_entry *entry;
1347 entry = TAILQ_FIRST(&commits->head);
1348 TAILQ_REMOVE(&commits->head, entry, entry);
1349 got_object_commit_close(entry->commit);
1350 commits->ncommits--;
1351 /* Don't free entry->id! It is owned by the commit graph. */
1356 free_commits(struct commit_queue *commits)
1358 while (!TAILQ_EMPTY(&commits->head))
1359 pop_commit(commits);
1362 static const struct got_error *
1363 match_commit(int *have_match, struct got_object_id *id,
1364 struct got_commit_object *commit, regex_t *regex)
1366 const struct got_error *err = NULL;
1367 regmatch_t regmatch;
1368 char *id_str = NULL, *logmsg = NULL;
1372 err = got_object_id_str(&id_str, id);
1376 err = got_object_commit_get_logmsg(&logmsg, commit);
1380 if (regexec(regex, got_object_commit_get_author(commit), 1,
1381 ®match, 0) == 0 ||
1382 regexec(regex, got_object_commit_get_committer(commit), 1,
1383 ®match, 0) == 0 ||
1384 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1385 regexec(regex, logmsg, 1, ®match, 0) == 0)
1393 static const struct got_error *
1394 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1395 int minqueue, struct got_repository *repo, const char *path,
1396 int *searching, int *search_next_done, regex_t *regex)
1398 const struct got_error *err = NULL;
1399 int nqueued = 0, have_match = 0;
1402 * We keep all commits open throughout the lifetime of the log
1403 * view in order to avoid having to re-fetch commits from disk
1404 * while updating the display.
1406 while (nqueued < minqueue ||
1407 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1408 struct got_object_id *id;
1409 struct got_commit_object *commit;
1410 struct commit_queue_entry *entry;
1413 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1414 if (err || id == NULL)
1417 err = got_object_open_as_commit(&commit, repo, id);
1420 entry = alloc_commit_queue_entry(commit, id);
1421 if (entry == NULL) {
1422 err = got_error_from_errno("alloc_commit_queue_entry");
1426 errcode = pthread_mutex_lock(&tog_mutex);
1428 err = got_error_set_errno(errcode,
1429 "pthread_mutex_lock");
1433 entry->idx = commits->ncommits;
1434 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1436 commits->ncommits++;
1438 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1439 err = match_commit(&have_match, id, commit, regex);
1442 errcode = pthread_mutex_unlock(&tog_mutex);
1443 if (errcode && err == NULL)
1444 err = got_error_set_errno(errcode,
1445 "pthread_mutex_unlock");
1447 if (err || have_match)
1454 static const struct got_error *
1455 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1456 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1457 struct commit_queue *commits, int selected_idx, int limit,
1458 struct got_reflist_head *refs, const char *path, int commits_needed,
1459 struct tog_colors *colors)
1461 const struct got_error *err = NULL;
1462 struct tog_log_view_state *s = &view->state.log;
1463 struct commit_queue_entry *entry;
1465 int ncommits, author_cols = 4;
1466 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1467 char *refs_str = NULL;
1469 struct tog_color *tc;
1470 static const size_t date_display_cols = 12;
1475 if (ncommits == selected_idx) {
1479 entry = TAILQ_NEXT(entry, entry);
1483 if (*selected && !(view->searching && view->search_next_done == 0)) {
1484 err = got_object_id_str(&id_str, (*selected)->id);
1488 err = build_refs_str(&refs_str, refs, (*selected)->id,
1495 if (commits_needed == 0)
1496 halfdelay(10); /* disable fast refresh */
1498 if (asprintf(&ncommits_str, " [%d/%d] %s",
1499 entry ? entry->idx + 1 : 0, commits->ncommits,
1500 commits_needed > 0 ?
1501 (view->searching && view->search_next_done == 0
1502 ? "searching..." : "loading... ") :
1503 (refs_str ? refs_str : "")) == -1) {
1504 err = got_error_from_errno("asprintf");
1508 if (path && strcmp(path, "/") != 0) {
1509 if (asprintf(&header, "commit %s %s%s",
1510 id_str ? id_str : "........................................",
1511 path, ncommits_str) == -1) {
1512 err = got_error_from_errno("asprintf");
1516 } else if (asprintf(&header, "commit %s%s",
1517 id_str ? id_str : "........................................",
1518 ncommits_str) == -1) {
1519 err = got_error_from_errno("asprintf");
1523 err = format_line(&wline, &width, header, view->ncols, 0);
1527 werase(view->window);
1529 if (view_needs_focus_indication(view))
1530 wstandout(view->window);
1531 tc = get_color(colors, TOG_COLOR_COMMIT);
1533 wattr_on(view->window,
1534 COLOR_PAIR(tc->colorpair), NULL);
1535 waddwstr(view->window, wline);
1537 wattr_off(view->window,
1538 COLOR_PAIR(tc->colorpair), NULL);
1539 while (width < view->ncols) {
1540 waddch(view->window, ' ');
1543 if (view_needs_focus_indication(view))
1544 wstandend(view->window);
1549 /* Grow author column size if necessary. */
1556 if (ncommits >= limit - 1)
1558 author = strdup(got_object_commit_get_author(entry->commit));
1559 if (author == NULL) {
1560 err = got_error_from_errno("strdup");
1563 err = format_author(&wauthor, &width, author, COLS,
1565 if (author_cols < width)
1566 author_cols = width;
1570 entry = TAILQ_NEXT(entry, entry);
1577 if (ncommits >= limit - 1)
1579 if (ncommits == selected_idx)
1580 wstandout(view->window);
1581 err = draw_commit(view, entry->commit, entry->id, refs,
1582 date_display_cols, author_cols, colors);
1583 if (ncommits == selected_idx)
1584 wstandend(view->window);
1589 entry = TAILQ_NEXT(entry, entry);
1602 scroll_up(struct tog_view *view,
1603 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1604 struct commit_queue *commits)
1606 struct commit_queue_entry *entry;
1609 entry = TAILQ_FIRST(&commits->head);
1610 if (*first_displayed_entry == entry)
1613 entry = *first_displayed_entry;
1614 while (entry && nscrolled < maxscroll) {
1615 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1617 *first_displayed_entry = entry;
1623 static const struct got_error *
1624 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1625 pthread_cond_t *need_commits)
1630 halfdelay(1); /* fast refresh while loading commits */
1632 while (*commits_needed > 0) {
1636 /* Wake the log thread. */
1637 errcode = pthread_cond_signal(need_commits);
1639 return got_error_set_errno(errcode,
1640 "pthread_cond_signal");
1641 errcode = pthread_mutex_unlock(&tog_mutex);
1643 return got_error_set_errno(errcode,
1644 "pthread_mutex_unlock");
1646 errcode = pthread_mutex_lock(&tog_mutex);
1648 return got_error_set_errno(errcode,
1649 "pthread_mutex_lock");
1651 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1653 * Thread is not done yet; lose a key press
1654 * and let the user retry... this way the GUI
1655 * remains interactive while logging deep paths
1656 * with few commits in history.
1665 static const struct got_error *
1666 scroll_down(struct tog_view *view,
1667 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1668 struct commit_queue_entry **last_displayed_entry,
1669 struct commit_queue *commits, int *log_complete, int *commits_needed,
1670 pthread_cond_t *need_commits)
1672 const struct got_error *err = NULL;
1673 struct commit_queue_entry *pentry;
1676 if (*last_displayed_entry == NULL)
1679 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1680 if (pentry == NULL && !*log_complete) {
1682 * Ask the log thread for required amount of commits
1683 * plus some amount of pre-fetching.
1685 (*commits_needed) += maxscroll + 20;
1686 err = trigger_log_thread(0, commits_needed, log_complete,
1693 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1697 *last_displayed_entry = pentry;
1699 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1702 *first_displayed_entry = pentry;
1703 } while (++nscrolled < maxscroll);
1708 static const struct got_error *
1709 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1710 struct got_commit_object *commit, struct got_object_id *commit_id,
1711 struct tog_view *log_view, struct got_reflist_head *refs,
1712 struct got_repository *repo)
1714 const struct got_error *err;
1715 struct got_object_qid *parent_id;
1716 struct tog_view *diff_view;
1718 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1719 if (diff_view == NULL)
1720 return got_error_from_errno("view_open");
1722 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1723 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1724 commit_id, log_view, refs, repo);
1726 *new_view = diff_view;
1730 static const struct got_error *
1731 tree_view_visit_subtree(struct got_tree_object *subtree,
1732 struct tog_tree_view_state *s)
1734 struct tog_parent_tree *parent;
1736 parent = calloc(1, sizeof(*parent));
1738 return got_error_from_errno("calloc");
1740 parent->tree = s->tree;
1741 parent->first_displayed_entry = s->first_displayed_entry;
1742 parent->selected_entry = s->selected_entry;
1743 parent->selected = s->selected;
1744 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1747 s->first_displayed_entry = NULL;
1751 static const struct got_error *
1752 tree_view_walk_path(struct tog_tree_view_state *s,
1753 struct got_object_id *commit_id,
1754 const char *path, struct got_repository *repo)
1756 const struct got_error *err = NULL;
1757 struct got_tree_object *tree = NULL;
1759 char *slash, *subpath = NULL;
1761 /* Walk the path and open corresponding tree objects. */
1764 struct got_tree_entry *te;
1765 struct got_object_id *tree_id;
1771 /* Ensure the correct subtree entry is selected. */
1772 slash = strchr(p, '/');
1774 te_name = strdup(p);
1776 te_name = strndup(p, slash - p);
1777 if (te_name == NULL) {
1778 err = got_error_from_errno("strndup");
1781 te = got_object_tree_find_entry(s->tree, te_name);
1783 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1788 s->selected_entry = te;
1789 s->selected = got_tree_entry_get_index(te);
1790 if (s->tree != s->root)
1791 s->selected++; /* skip '..' */
1793 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1794 /* Jump to this file's entry. */
1795 s->first_displayed_entry = s->selected_entry;
1800 slash = strchr(p, '/');
1802 subpath = strndup(path, slash - path);
1804 subpath = strdup(path);
1805 if (subpath == NULL) {
1806 err = got_error_from_errno("strdup");
1810 err = got_object_id_by_path(&tree_id, repo, commit_id,
1815 err = got_object_open_as_tree(&tree, repo, tree_id);
1820 err = tree_view_visit_subtree(tree, s);
1822 got_object_tree_close(tree);
1836 static const struct got_error *
1837 browse_commit_tree(struct tog_view **new_view, int begin_x,
1838 struct commit_queue_entry *entry, const char *path,
1839 struct got_reflist_head *refs, struct got_repository *repo)
1841 const struct got_error *err = NULL;
1842 struct got_tree_object *tree;
1843 struct tog_tree_view_state *s;
1844 struct tog_view *tree_view;
1846 err = got_object_open_as_tree(&tree, repo,
1847 got_object_commit_get_tree_id(entry->commit));
1851 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1852 if (tree_view == NULL)
1853 return got_error_from_errno("view_open");
1855 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1857 got_object_tree_close(tree);
1860 s = &tree_view->state.tree;
1862 *new_view = tree_view;
1864 if (got_path_is_root_dir(path))
1867 return tree_view_walk_path(s, entry->id, path, repo);
1870 static const struct got_error *
1871 block_signals_used_by_main_thread(void)
1876 if (sigemptyset(&sigset) == -1)
1877 return got_error_from_errno("sigemptyset");
1879 /* tog handles SIGWINCH and SIGCONT */
1880 if (sigaddset(&sigset, SIGWINCH) == -1)
1881 return got_error_from_errno("sigaddset");
1882 if (sigaddset(&sigset, SIGCONT) == -1)
1883 return got_error_from_errno("sigaddset");
1885 /* ncurses handles SIGTSTP */
1886 if (sigaddset(&sigset, SIGTSTP) == -1)
1887 return got_error_from_errno("sigaddset");
1889 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1891 return got_error_set_errno(errcode, "pthread_sigmask");
1897 log_thread(void *arg)
1899 const struct got_error *err = NULL;
1901 struct tog_log_thread_args *a = arg;
1904 err = block_signals_used_by_main_thread();
1908 while (!done && !err && !tog_sigpipe_received) {
1909 err = queue_commits(a->graph, a->commits, 1, a->repo,
1910 a->in_repo_path, a->searching, a->search_next_done,
1913 if (err->code != GOT_ERR_ITER_COMPLETED)
1917 } else if (a->commits_needed > 0)
1918 a->commits_needed--;
1920 errcode = pthread_mutex_lock(&tog_mutex);
1922 err = got_error_set_errno(errcode,
1923 "pthread_mutex_lock");
1925 } else if (*a->quit)
1927 else if (*a->first_displayed_entry == NULL) {
1928 *a->first_displayed_entry =
1929 TAILQ_FIRST(&a->commits->head);
1930 *a->selected_entry = *a->first_displayed_entry;
1934 a->commits_needed = 0;
1935 else if (a->commits_needed == 0) {
1936 errcode = pthread_cond_wait(&a->need_commits,
1939 err = got_error_set_errno(errcode,
1940 "pthread_cond_wait");
1943 errcode = pthread_mutex_unlock(&tog_mutex);
1944 if (errcode && err == NULL)
1945 err = got_error_set_errno(errcode,
1946 "pthread_mutex_unlock");
1948 a->log_complete = 1;
1952 static const struct got_error *
1953 stop_log_thread(struct tog_log_view_state *s)
1955 const struct got_error *err = NULL;
1960 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1962 return got_error_set_errno(errcode,
1963 "pthread_cond_signal");
1964 errcode = pthread_mutex_unlock(&tog_mutex);
1966 return got_error_set_errno(errcode,
1967 "pthread_mutex_unlock");
1968 errcode = pthread_join(s->thread, (void **)&err);
1970 return got_error_set_errno(errcode, "pthread_join");
1971 errcode = pthread_mutex_lock(&tog_mutex);
1973 return got_error_set_errno(errcode,
1974 "pthread_mutex_lock");
1978 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1979 if (errcode && err == NULL)
1980 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1982 if (s->thread_args.repo) {
1983 got_repo_close(s->thread_args.repo);
1984 s->thread_args.repo = NULL;
1987 if (s->thread_args.graph) {
1988 got_commit_graph_close(s->thread_args.graph);
1989 s->thread_args.graph = NULL;
1995 static const struct got_error *
1996 close_log_view(struct tog_view *view)
1998 const struct got_error *err = NULL;
1999 struct tog_log_view_state *s = &view->state.log;
2001 err = stop_log_thread(s);
2002 free_commits(&s->commits);
2003 free(s->in_repo_path);
2004 s->in_repo_path = NULL;
2010 static const struct got_error *
2011 search_start_log_view(struct tog_view *view)
2013 struct tog_log_view_state *s = &view->state.log;
2015 s->matched_entry = NULL;
2016 s->search_entry = NULL;
2020 static const struct got_error *
2021 search_next_log_view(struct tog_view *view)
2023 const struct got_error *err = NULL;
2024 struct tog_log_view_state *s = &view->state.log;
2025 struct commit_queue_entry *entry;
2027 if (!view->searching) {
2028 view->search_next_done = 1;
2032 if (s->search_entry) {
2034 errcode = pthread_mutex_unlock(&tog_mutex);
2036 return got_error_set_errno(errcode,
2037 "pthread_mutex_unlock");
2038 ch = wgetch(view->window);
2039 errcode = pthread_mutex_lock(&tog_mutex);
2041 return got_error_set_errno(errcode,
2042 "pthread_mutex_lock");
2043 if (ch == KEY_BACKSPACE) {
2044 view->search_next_done = 1;
2047 if (view->searching == TOG_SEARCH_FORWARD)
2048 entry = TAILQ_NEXT(s->search_entry, entry);
2050 entry = TAILQ_PREV(s->search_entry,
2051 commit_queue_head, entry);
2052 } else if (s->matched_entry) {
2053 if (view->searching == TOG_SEARCH_FORWARD)
2054 entry = TAILQ_NEXT(s->selected_entry, entry);
2056 entry = TAILQ_PREV(s->selected_entry,
2057 commit_queue_head, entry);
2059 if (view->searching == TOG_SEARCH_FORWARD)
2060 entry = TAILQ_FIRST(&s->commits.head);
2062 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2068 if (entry == NULL) {
2069 if (s->thread_args.log_complete ||
2070 view->searching == TOG_SEARCH_BACKWARD) {
2071 view->search_next_done = 1;
2075 * Poke the log thread for more commits and return,
2076 * allowing the main loop to make progress. Search
2077 * will resume at s->search_entry once we come back.
2079 s->thread_args.commits_needed++;
2080 return trigger_log_thread(1,
2081 &s->thread_args.commits_needed,
2082 &s->thread_args.log_complete,
2083 &s->thread_args.need_commits);
2086 err = match_commit(&have_match, entry->id, entry->commit,
2091 view->search_next_done = 1;
2092 s->matched_entry = entry;
2096 s->search_entry = entry;
2097 if (view->searching == TOG_SEARCH_FORWARD)
2098 entry = TAILQ_NEXT(entry, entry);
2100 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2103 if (s->matched_entry) {
2104 int cur = s->selected_entry->idx;
2105 while (cur < s->matched_entry->idx) {
2106 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2111 while (cur > s->matched_entry->idx) {
2112 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2119 s->search_entry = NULL;
2124 static const struct got_error *
2125 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2126 struct got_reflist_head *refs, struct got_repository *repo,
2127 const char *head_ref_name, const char *in_repo_path,
2130 const struct got_error *err = NULL;
2131 struct tog_log_view_state *s = &view->state.log;
2132 struct got_repository *thread_repo = NULL;
2133 struct got_commit_graph *thread_graph = NULL;
2136 if (in_repo_path != s->in_repo_path) {
2137 free(s->in_repo_path);
2138 s->in_repo_path = strdup(in_repo_path);
2139 if (s->in_repo_path == NULL)
2140 return got_error_from_errno("strdup");
2143 /* The commit queue only contains commits being displayed. */
2144 TAILQ_INIT(&s->commits.head);
2145 s->commits.ncommits = 0;
2149 s->head_ref_name = head_ref_name;
2150 s->start_id = got_object_id_dup(start_id);
2151 if (s->start_id == NULL) {
2152 err = got_error_from_errno("got_object_id_dup");
2155 s->log_branches = log_branches;
2157 SIMPLEQ_INIT(&s->colors);
2158 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2159 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2160 get_color_value("TOG_COLOR_COMMIT"));
2163 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2164 get_color_value("TOG_COLOR_AUTHOR"));
2166 free_colors(&s->colors);
2169 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2170 get_color_value("TOG_COLOR_DATE"));
2172 free_colors(&s->colors);
2177 view->show = show_log_view;
2178 view->input = input_log_view;
2179 view->close = close_log_view;
2180 view->search_start = search_start_log_view;
2181 view->search_next = search_next_log_view;
2183 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2186 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2190 err = got_commit_graph_iter_start(thread_graph,
2191 s->start_id, s->repo, NULL, NULL);
2195 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2197 err = got_error_set_errno(errcode, "pthread_cond_init");
2201 s->thread_args.commits_needed = view->nlines;
2202 s->thread_args.graph = thread_graph;
2203 s->thread_args.commits = &s->commits;
2204 s->thread_args.in_repo_path = s->in_repo_path;
2205 s->thread_args.start_id = s->start_id;
2206 s->thread_args.repo = thread_repo;
2207 s->thread_args.log_complete = 0;
2208 s->thread_args.quit = &s->quit;
2209 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2210 s->thread_args.selected_entry = &s->selected_entry;
2211 s->thread_args.searching = &view->searching;
2212 s->thread_args.search_next_done = &view->search_next_done;
2213 s->thread_args.regex = &view->regex;
2216 close_log_view(view);
2220 static const struct got_error *
2221 show_log_view(struct tog_view *view)
2223 struct tog_log_view_state *s = &view->state.log;
2225 if (s->thread == NULL) {
2226 int errcode = pthread_create(&s->thread, NULL, log_thread,
2229 return got_error_set_errno(errcode, "pthread_create");
2232 return draw_commits(view, &s->last_displayed_entry,
2233 &s->selected_entry, s->first_displayed_entry,
2234 &s->commits, s->selected, view->nlines, s->refs,
2235 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2238 static const struct got_error *
2239 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2240 struct tog_view **focus_view, struct tog_view *view, int ch)
2242 const struct got_error *err = NULL;
2243 struct tog_log_view_state *s = &view->state.log;
2244 char *parent_path, *in_repo_path = NULL;
2245 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2247 struct got_object_id *start_id;
2257 if (s->first_displayed_entry == NULL)
2259 if (s->selected > 0)
2262 scroll_up(view, &s->first_displayed_entry, 1,
2267 if (s->first_displayed_entry == NULL)
2269 if (TAILQ_FIRST(&s->commits.head) ==
2270 s->first_displayed_entry) {
2274 scroll_up(view, &s->first_displayed_entry,
2275 view->nlines, &s->commits);
2281 if (s->first_displayed_entry == NULL)
2283 if (s->selected < MIN(view->nlines - 2,
2284 s->commits.ncommits - 1)) {
2288 err = scroll_down(view, &s->first_displayed_entry, 1,
2289 &s->last_displayed_entry, &s->commits,
2290 &s->thread_args.log_complete,
2291 &s->thread_args.commits_needed,
2292 &s->thread_args.need_commits);
2296 struct commit_queue_entry *first;
2297 first = s->first_displayed_entry;
2300 err = scroll_down(view, &s->first_displayed_entry,
2301 view->nlines, &s->last_displayed_entry,
2302 &s->commits, &s->thread_args.log_complete,
2303 &s->thread_args.commits_needed,
2304 &s->thread_args.need_commits);
2307 if (first == s->first_displayed_entry &&
2308 s->selected < MIN(view->nlines - 2,
2309 s->commits.ncommits - 1)) {
2310 /* can't scroll further down */
2311 s->selected = MIN(view->nlines - 2,
2312 s->commits.ncommits - 1);
2318 if (s->selected > view->nlines - 2)
2319 s->selected = view->nlines - 2;
2320 if (s->selected > s->commits.ncommits - 1)
2321 s->selected = s->commits.ncommits - 1;
2326 if (s->selected_entry == NULL)
2328 if (view_is_parent_view(view))
2329 begin_x = view_split_begin_x(view->begin_x);
2330 err = open_diff_view_for_commit(&diff_view, begin_x,
2331 s->selected_entry->commit, s->selected_entry->id,
2332 view, s->refs, s->repo);
2335 if (view_is_parent_view(view)) {
2336 err = view_close_child(view);
2339 err = view_set_child(view, diff_view);
2341 view_close(diff_view);
2344 *focus_view = diff_view;
2345 view->child_focussed = 1;
2347 *new_view = diff_view;
2350 if (s->selected_entry == NULL)
2352 if (view_is_parent_view(view))
2353 begin_x = view_split_begin_x(view->begin_x);
2354 err = browse_commit_tree(&tree_view, begin_x,
2355 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2358 if (view_is_parent_view(view)) {
2359 err = view_close_child(view);
2362 err = view_set_child(view, tree_view);
2364 view_close(tree_view);
2367 *focus_view = tree_view;
2368 view->child_focussed = 1;
2370 *new_view = tree_view;
2373 if (strcmp(s->in_repo_path, "/") == 0)
2375 parent_path = dirname(s->in_repo_path);
2376 if (parent_path && strcmp(parent_path, ".") != 0) {
2377 err = stop_log_thread(s);
2380 lv = view_open(view->nlines, view->ncols,
2381 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2383 return got_error_from_errno(
2385 err = open_log_view(lv, s->start_id, s->refs,
2386 s->repo, s->head_ref_name, parent_path,
2390 if (view_is_parent_view(view))
2393 view_set_child(view->parent, lv);
2400 err = stop_log_thread(s);
2403 lv = view_open(view->nlines, view->ncols,
2404 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2406 return got_error_from_errno("view_open");
2407 err = got_repo_match_object_id(&start_id, NULL,
2408 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2409 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2414 in_repo_path = strdup(s->in_repo_path);
2415 if (in_repo_path == NULL) {
2418 return got_error_from_errno("strdup");
2420 got_ref_list_free(s->refs);
2421 err = got_ref_list(s->refs, s->repo, NULL,
2422 got_ref_cmp_by_name, NULL);
2428 err = open_log_view(lv, start_id, s->refs, s->repo,
2429 s->head_ref_name, in_repo_path, s->log_branches);
2439 s->log_branches = !s->log_branches;
2440 err = stop_log_thread(s);
2443 lv = view_open(view->nlines, view->ncols,
2444 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2446 return got_error_from_errno("view_open");
2447 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2448 s->head_ref_name, s->in_repo_path, s->log_branches);
2463 static const struct got_error *
2464 apply_unveil(const char *repo_path, const char *worktree_path)
2466 const struct got_error *error;
2469 if (unveil("gmon.out", "rwc") != 0)
2470 return got_error_from_errno2("unveil", "gmon.out");
2472 if (repo_path && unveil(repo_path, "r") != 0)
2473 return got_error_from_errno2("unveil", repo_path);
2475 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2476 return got_error_from_errno2("unveil", worktree_path);
2478 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2479 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2481 error = got_privsep_unveil_exec_helpers();
2485 if (unveil(NULL, NULL) != 0)
2486 return got_error_from_errno("unveil");
2496 halfdelay(1); /* Do fast refresh while initial view is loading. */
2499 intrflush(stdscr, FALSE);
2500 keypad(stdscr, TRUE);
2502 if (getenv("TOG_COLORS") != NULL) {
2504 use_default_colors();
2506 signal(SIGWINCH, tog_sigwinch);
2507 signal(SIGPIPE, tog_sigpipe);
2508 signal(SIGCONT, tog_sigcont);
2511 static const struct got_error *
2512 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2513 struct got_repository *repo, struct got_worktree *worktree)
2515 const struct got_error *err = NULL;
2518 *in_repo_path = strdup("/");
2519 if (*in_repo_path == NULL)
2520 return got_error_from_errno("strdup");
2525 const char *prefix = got_worktree_get_path_prefix(worktree);
2528 err = got_worktree_resolve_path(&wt_path, worktree, argv[0]);
2532 if (asprintf(&p, "%s%s%s", prefix,
2533 (strcmp(prefix, "/") != 0) ? "/" : "", wt_path) == -1) {
2534 err = got_error_from_errno("asprintf");
2538 err = got_repo_map_path(in_repo_path, repo, p, 0);
2542 err = got_repo_map_path(in_repo_path, repo, argv[0], 1);
2547 static const struct got_error *
2548 cmd_log(int argc, char *argv[])
2550 const struct got_error *error;
2551 struct got_repository *repo = NULL;
2552 struct got_worktree *worktree = NULL;
2553 struct got_reflist_head refs;
2554 struct got_object_id *start_id = NULL;
2555 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2556 char *start_commit = NULL, *head_ref_name = NULL;
2557 int ch, log_branches = 0;
2558 struct tog_view *view;
2560 SIMPLEQ_INIT(&refs);
2563 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2568 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2574 start_commit = optarg;
2577 repo_path = realpath(optarg, NULL);
2578 if (repo_path == NULL)
2579 return got_error_from_errno2("realpath",
2594 cwd = getcwd(NULL, 0);
2596 return got_error_from_errno("getcwd");
2598 error = got_worktree_open(&worktree, cwd);
2599 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2602 if (repo_path == NULL) {
2605 strdup(got_worktree_get_repo_path(worktree));
2607 repo_path = strdup(cwd);
2609 if (repo_path == NULL) {
2610 error = got_error_from_errno("strdup");
2614 error = got_repo_open(&repo, repo_path, NULL);
2618 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2625 error = apply_unveil(got_repo_get_path(repo),
2626 worktree ? got_worktree_get_root_path(worktree) : NULL);
2630 if (start_commit == NULL)
2631 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2632 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2633 GOT_OBJ_TYPE_COMMIT, 1, repo);
2635 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2636 GOT_OBJ_TYPE_COMMIT, 1, repo);
2640 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2644 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2646 error = got_error_from_errno("view_open");
2650 head_ref_name = strdup(
2651 got_worktree_get_head_ref_name(worktree));
2652 if (head_ref_name == NULL) {
2653 error = got_error_from_errno("strdup");
2657 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2658 in_repo_path, log_branches);
2662 /* Release work tree lock. */
2663 got_worktree_close(worktree);
2666 error = view_loop(view);
2672 free(head_ref_name);
2674 got_repo_close(repo);
2676 got_worktree_close(worktree);
2677 got_ref_list_free(&refs);
2685 fprintf(stderr, "usage: %s diff [-r repository-path] object1 object2\n",
2691 parse_next_line(FILE *f, size_t *len)
2696 const char delim[3] = { '\0', '\0', '\0'};
2698 line = fparseln(f, &linelen, &lineno, delim, 0);
2705 match_line(const char *line, regex_t *regex)
2707 regmatch_t regmatch;
2709 return regexec(regex, line, 1, ®match, 0) == 0;
2713 match_color(struct tog_colors *colors, const char *line)
2715 struct tog_color *tc = NULL;
2717 SIMPLEQ_FOREACH(tc, colors, entry) {
2718 if (match_line(line, &tc->regex))
2725 static const struct got_error *
2726 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line, int nlines,
2727 int selected_line, int max_lines, int *last_displayed_line, int *eof,
2728 char *header, struct tog_colors *colors)
2730 const struct got_error *err;
2731 int lineno = 0, nprinted = 0;
2733 struct tog_color *tc;
2739 werase(view->window);
2742 err = format_line(&wline, &width, header, view->ncols, 0);
2747 if (view_needs_focus_indication(view))
2748 wstandout(view->window);
2749 waddwstr(view->window, wline);
2750 if (view_needs_focus_indication(view))
2751 wstandend(view->window);
2752 if (width <= view->ncols - 1)
2753 waddch(view->window, '\n');
2761 while (nprinted < max_lines) {
2762 line = parse_next_line(f, &len);
2767 if (++lineno < *first_displayed_line) {
2772 err = format_line(&wline, &width, line, view->ncols, 0);
2778 tc = match_color(colors, line);
2780 wattr_on(view->window,
2781 COLOR_PAIR(tc->colorpair), NULL);
2782 waddwstr(view->window, wline);
2784 wattr_off(view->window,
2785 COLOR_PAIR(tc->colorpair), NULL);
2786 if (width <= view->ncols - 1)
2787 waddch(view->window, '\n');
2788 if (++nprinted == 1)
2789 *first_displayed_line = lineno;
2794 *last_displayed_line = lineno;
2799 while (nprinted < view->nlines) {
2800 waddch(view->window, '\n');
2804 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2809 wstandout(view->window);
2810 waddwstr(view->window, wline);
2811 wstandend(view->window);
2818 get_datestr(time_t *time, char *datebuf)
2820 struct tm mytm, *tm;
2823 tm = gmtime_r(time, &mytm);
2826 s = asctime_r(tm, datebuf);
2829 p = strchr(s, '\n');
2835 static const struct got_error *
2836 write_commit_info(struct got_object_id *commit_id,
2837 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2839 const struct got_error *err = NULL;
2840 char datebuf[26], *datestr;
2841 struct got_commit_object *commit;
2842 char *id_str = NULL, *logmsg = NULL;
2843 time_t committer_time;
2844 const char *author, *committer;
2845 char *refs_str = NULL;
2848 err = build_refs_str(&refs_str, refs, commit_id, repo);
2853 err = got_object_open_as_commit(&commit, repo, commit_id);
2857 err = got_object_id_str(&id_str, commit_id);
2859 err = got_error_from_errno("got_object_id_str");
2863 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2864 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2865 err = got_error_from_errno("fprintf");
2868 if (fprintf(outfile, "from: %s\n",
2869 got_object_commit_get_author(commit)) < 0) {
2870 err = got_error_from_errno("fprintf");
2873 committer_time = got_object_commit_get_committer_time(commit);
2874 datestr = get_datestr(&committer_time, datebuf);
2875 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2876 err = got_error_from_errno("fprintf");
2879 author = got_object_commit_get_author(commit);
2880 committer = got_object_commit_get_committer(commit);
2881 if (strcmp(author, committer) != 0 &&
2882 fprintf(outfile, "via: %s\n", committer) < 0) {
2883 err = got_error_from_errno("fprintf");
2886 err = got_object_commit_get_logmsg(&logmsg, commit);
2889 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2890 err = got_error_from_errno("fprintf");
2897 got_object_commit_close(commit);
2901 const struct got_error *
2902 get_filestream_info(size_t *filesize, int *nlines, off_t **line_offsets,
2908 size_t noffsets = 0;
2912 *line_offsets = NULL;
2918 if (fseek(infile, 0, SEEK_END) == -1)
2919 return got_error_from_errno("fseek");
2920 len = ftell(infile) + 1;
2922 return got_error_from_errno("ftell");
2923 if (fseek(infile, 0, SEEK_SET) == -1)
2924 return got_error_from_errno("fseek");
2928 if ((buf = calloc(len, sizeof(char *))) == NULL)
2929 return got_error_from_errno("calloc");
2931 fread(buf, 1, len, infile);
2933 return got_error_from_errno("fread");
2936 if (line_offsets && nlines) {
2937 if (*line_offsets == NULL) {
2938 /* Have some data but perhaps no '\n'. */
2941 *line_offsets = calloc(1, sizeof(**line_offsets));
2942 if (*line_offsets == NULL)
2943 return got_error_from_errno("calloc");
2944 /* Skip forward over end of first line. */
2951 /* Scan '\n' offsets in remaining chunk of data. */
2953 if (buf[i] != '\n') {
2958 if (noffsets < *nlines) {
2959 off_t *o = recallocarray(*line_offsets,
2961 sizeof(**line_offsets));
2963 free(*line_offsets);
2964 *line_offsets = NULL;
2965 return got_error_from_errno(
2972 (*line_offsets)[*nlines - 1] = off;
2977 if (fflush(infile) != 0)
2978 return got_error_from_errno("fflush");
2987 static const struct got_error *
2988 create_diff(struct tog_diff_view_state *s)
2990 const struct got_error *err = NULL;
2996 err = got_error_from_errno("got_opentemp");
2999 if (s->f && fclose(s->f) != 0) {
3000 err = got_error_from_errno("fclose");
3006 err = got_object_get_type(&obj_type, s->repo, s->id1);
3008 err = got_object_get_type(&obj_type, s->repo, s->id2);
3013 case GOT_OBJ_TYPE_BLOB:
3014 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
3015 s->diff_context, 0, s->repo, s->f);
3017 case GOT_OBJ_TYPE_TREE:
3018 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
3019 s->diff_context, 0, s->repo, s->f);
3021 case GOT_OBJ_TYPE_COMMIT: {
3022 const struct got_object_id_queue *parent_ids;
3023 struct got_object_qid *pid;
3024 struct got_commit_object *commit2;
3026 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3029 /* Show commit info if we're diffing to a parent/root commit. */
3030 if (s->id1 == NULL) {
3031 err =write_commit_info(s->id2, s->refs, s->repo, s->f);
3035 parent_ids = got_object_commit_get_parent_ids(commit2);
3036 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3037 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3038 err = write_commit_info(s->id2, s->refs,
3046 got_object_commit_close(commit2);
3048 err = got_diff_objects_as_commits(s->id1, s->id2,
3049 s->diff_context, 0, s->repo, s->f);
3053 err = got_error(GOT_ERR_OBJ_TYPE);
3058 err = get_filestream_info(&s->filesize, &s->nlines, &s->line_offsets,
3061 if (s->f && fflush(s->f) != 0 && err == NULL)
3062 err = got_error_from_errno("fflush");
3067 diff_view_indicate_progress(struct tog_view *view)
3069 mvwaddstr(view->window, 0, 0, "diffing...");
3074 static const struct got_error *
3075 search_start_diff_view(struct tog_view *view)
3077 struct tog_diff_view_state *s = &view->state.diff;
3079 s->matched_line = 0;
3083 static const struct got_error *
3084 search_next_diff_view(struct tog_view *view)
3086 struct tog_diff_view_state *s = &view->state.diff;
3089 if (!view->searching) {
3090 view->search_next_done = 1;
3094 if (s->matched_line) {
3095 if (view->searching == TOG_SEARCH_FORWARD)
3096 lineno = s->matched_line + 1;
3098 lineno = s->matched_line - 1;
3100 if (view->searching == TOG_SEARCH_FORWARD)
3111 if (lineno <= 0 || lineno > s->nlines) {
3112 if (s->matched_line == 0) {
3113 view->search_next_done = 1;
3118 if (view->searching == TOG_SEARCH_FORWARD)
3124 offset = s->line_offsets[lineno - 1];
3125 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3127 return got_error_from_errno("fseeko");
3130 line = parse_next_line(s->f, &len);
3131 if (line && match_line(line, &view->regex)) {
3132 view->search_next_done = 1;
3133 s->matched_line = lineno;
3138 if (view->searching == TOG_SEARCH_FORWARD)
3144 if (s->matched_line) {
3145 s->first_displayed_line = s->matched_line;
3146 s->selected_line = 1;
3152 static const struct got_error *
3153 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3154 struct got_object_id *id2, struct tog_view *log_view,
3155 struct got_reflist_head *refs, struct got_repository *repo)
3157 const struct got_error *err;
3158 struct tog_diff_view_state *s = &view->state.diff;
3160 if (id1 != NULL && id2 != NULL) {
3162 err = got_object_get_type(&type1, repo, id1);
3165 err = got_object_get_type(&type2, repo, id2);
3170 return got_error(GOT_ERR_OBJ_TYPE);
3172 s->first_displayed_line = 1;
3173 s->last_displayed_line = view->nlines;
3174 s->selected_line = 1;
3181 s->id1 = got_object_id_dup(id1);
3183 return got_error_from_errno("got_object_id_dup");
3187 s->id2 = got_object_id_dup(id2);
3188 if (s->id2 == NULL) {
3191 return got_error_from_errno("got_object_id_dup");
3194 s->first_displayed_line = 1;
3195 s->last_displayed_line = view->nlines;
3196 s->diff_context = 3;
3197 s->log_view = log_view;
3201 SIMPLEQ_INIT(&s->colors);
3202 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3203 err = add_color(&s->colors,
3204 "^-", TOG_COLOR_DIFF_MINUS,
3205 get_color_value("TOG_COLOR_DIFF_MINUS"));
3208 err = add_color(&s->colors, "^\\+",
3209 TOG_COLOR_DIFF_PLUS,
3210 get_color_value("TOG_COLOR_DIFF_PLUS"));
3212 free_colors(&s->colors);
3215 err = add_color(&s->colors,
3216 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3217 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3219 free_colors(&s->colors);
3223 err = add_color(&s->colors,
3224 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
3225 get_color_value("TOG_COLOR_DIFF_META"));
3227 free_colors(&s->colors);
3231 err = add_color(&s->colors,
3232 "^(from|via): ", TOG_COLOR_AUTHOR,
3233 get_color_value("TOG_COLOR_AUTHOR"));
3235 free_colors(&s->colors);
3239 err = add_color(&s->colors,
3240 "^date: ", TOG_COLOR_DATE,
3241 get_color_value("TOG_COLOR_DATE"));
3243 free_colors(&s->colors);
3248 if (log_view && view_is_splitscreen(view))
3249 show_log_view(log_view); /* draw vborder */
3250 diff_view_indicate_progress(view);
3252 err = create_diff(s);
3261 view->show = show_diff_view;
3262 view->input = input_diff_view;
3263 view->close = close_diff_view;
3264 view->search_start = search_start_diff_view;
3265 view->search_next = search_next_diff_view;
3270 static const struct got_error *
3271 close_diff_view(struct tog_view *view)
3273 const struct got_error *err = NULL;
3274 struct tog_diff_view_state *s = &view->state.diff;
3280 if (s->f && fclose(s->f) == EOF)
3281 err = got_error_from_errno("fclose");
3282 free_colors(&s->colors);
3283 free(s->line_offsets);
3287 static const struct got_error *
3288 show_diff_view(struct tog_view *view)
3290 const struct got_error *err;
3291 struct tog_diff_view_state *s = &view->state.diff;
3292 char *id_str1 = NULL, *id_str2, *header;
3295 err = got_object_id_str(&id_str1, s->id1);
3299 err = got_object_id_str(&id_str2, s->id2);
3303 if (asprintf(&header, "diff %s %s",
3304 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3305 err = got_error_from_errno("asprintf");
3313 return draw_file(view, s->f, &s->first_displayed_line, s->nlines,
3314 s->selected_line, view->nlines, &s->last_displayed_line, &s->eof,
3315 header, &s->colors);
3318 static const struct got_error *
3319 set_selected_commit(struct tog_diff_view_state *s,
3320 struct commit_queue_entry *entry)
3322 const struct got_error *err;
3323 const struct got_object_id_queue *parent_ids;
3324 struct got_commit_object *selected_commit;
3325 struct got_object_qid *pid;
3328 s->id2 = got_object_id_dup(entry->id);
3330 return got_error_from_errno("got_object_id_dup");
3332 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3335 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3337 pid = SIMPLEQ_FIRST(parent_ids);
3338 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3339 got_object_commit_close(selected_commit);
3343 static const struct got_error *
3344 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3345 struct tog_view **focus_view, struct tog_view *view, int ch)
3347 const struct got_error *err = NULL;
3348 struct tog_diff_view_state *s = &view->state.diff;
3349 struct tog_log_view_state *ls;
3350 struct commit_queue_entry *entry;
3356 if (s->first_displayed_line > 1)
3357 s->first_displayed_line--;
3361 if (s->first_displayed_line == 1)
3364 while (i++ < view->nlines - 1 &&
3365 s->first_displayed_line > 1)
3366 s->first_displayed_line--;
3371 s->first_displayed_line++;
3379 while (!s->eof && i++ < view->nlines - 1) {
3381 line = parse_next_line(s->f, NULL);
3382 s->first_displayed_line++;
3388 if (s->diff_context > 0) {
3390 diff_view_indicate_progress(view);
3391 err = create_diff(s);
3395 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3397 diff_view_indicate_progress(view);
3398 err = create_diff(s);
3403 if (s->log_view == NULL)
3405 ls = &s->log_view->state.log;
3406 entry = TAILQ_PREV(ls->selected_entry,
3407 commit_queue_head, entry);
3411 err = input_log_view(NULL, NULL, NULL, s->log_view,
3416 err = set_selected_commit(s, entry);
3420 s->first_displayed_line = 1;
3421 s->last_displayed_line = view->nlines;
3423 diff_view_indicate_progress(view);
3424 err = create_diff(s);
3428 if (s->log_view == NULL)
3430 ls = &s->log_view->state.log;
3432 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3433 ls->thread_args.commits_needed++;
3435 /* Display "loading..." in log view. */
3436 show_log_view(s->log_view);
3440 err = trigger_log_thread(1 /* load_all */,
3441 &ls->thread_args.commits_needed,
3442 &ls->thread_args.log_complete,
3443 &ls->thread_args.need_commits);
3447 err = input_log_view(NULL, NULL, NULL, s->log_view,
3452 entry = TAILQ_NEXT(ls->selected_entry, entry);
3456 err = set_selected_commit(s, entry);
3460 s->first_displayed_line = 1;
3461 s->last_displayed_line = view->nlines;
3463 diff_view_indicate_progress(view);
3464 err = create_diff(s);
3473 static const struct got_error *
3474 cmd_diff(int argc, char *argv[])
3476 const struct got_error *error = NULL;
3477 struct got_repository *repo = NULL;
3478 struct got_worktree *worktree = NULL;
3479 struct got_reflist_head refs;
3480 struct got_object_id *id1 = NULL, *id2 = NULL;
3481 char *repo_path = NULL, *cwd = NULL;
3482 char *id_str1 = NULL, *id_str2 = NULL;
3484 struct tog_view *view;
3486 SIMPLEQ_INIT(&refs);
3489 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3494 while ((ch = getopt(argc, argv, "r:")) != -1) {
3497 repo_path = realpath(optarg, NULL);
3498 if (repo_path == NULL)
3499 return got_error_from_errno2("realpath",
3512 usage_diff(); /* TODO show local worktree changes */
3513 } else if (argc == 2) {
3519 cwd = getcwd(NULL, 0);
3521 return got_error_from_errno("getcwd");
3523 error = got_worktree_open(&worktree, cwd);
3524 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3527 if (repo_path == NULL) {
3530 strdup(got_worktree_get_repo_path(worktree));
3532 repo_path = strdup(cwd);
3534 if (repo_path == NULL) {
3535 error = got_error_from_errno("strdup");
3539 error = got_repo_open(&repo, repo_path, NULL);
3545 error = apply_unveil(got_repo_get_path(repo), NULL);
3549 error = got_repo_match_object_id_prefix(&id1, id_str1,
3550 GOT_OBJ_TYPE_ANY, repo);
3554 error = got_repo_match_object_id_prefix(&id2, id_str2,
3555 GOT_OBJ_TYPE_ANY, repo);
3559 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3563 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3565 error = got_error_from_errno("view_open");
3568 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3571 error = view_loop(view);
3576 got_repo_close(repo);
3578 got_worktree_close(worktree);
3579 got_ref_list_free(&refs);
3587 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3592 struct tog_blame_line {
3594 struct got_object_id *id;
3597 static const struct got_error *
3598 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3599 const char *path, struct tog_blame_line *lines, int nlines,
3600 int blame_complete, int selected_line, int *first_displayed_line,
3601 int *last_displayed_line, int *eof, int max_lines,
3602 struct tog_colors *colors)
3604 const struct got_error *err;
3605 int lineno = 0, nprinted = 0;
3610 struct tog_blame_line *blame_line;
3611 struct got_object_id *prev_id = NULL;
3613 struct tog_color *tc;
3615 err = got_object_id_str(&id_str, id);
3620 werase(view->window);
3622 if (asprintf(&line, "commit %s", id_str) == -1) {
3623 err = got_error_from_errno("asprintf");
3628 err = format_line(&wline, &width, line, view->ncols, 0);
3633 if (view_needs_focus_indication(view))
3634 wstandout(view->window);
3635 tc = get_color(colors, TOG_COLOR_COMMIT);
3637 wattr_on(view->window,
3638 COLOR_PAIR(tc->colorpair), NULL);
3639 waddwstr(view->window, wline);
3641 wattr_off(view->window,
3642 COLOR_PAIR(tc->colorpair), NULL);
3643 if (view_needs_focus_indication(view))
3644 wstandend(view->window);
3647 if (width < view->ncols - 1)
3648 waddch(view->window, '\n');
3650 if (asprintf(&line, "[%d/%d] %s%s",
3651 *first_displayed_line - 1 + selected_line, nlines,
3652 blame_complete ? "" : "annotating... ", path) == -1) {
3654 return got_error_from_errno("asprintf");
3657 err = format_line(&wline, &width, line, view->ncols, 0);
3662 waddwstr(view->window, wline);
3665 if (width < view->ncols - 1)
3666 waddch(view->window, '\n');
3669 while (nprinted < max_lines - 2) {
3670 line = parse_next_line(f, &len);
3675 if (++lineno < *first_displayed_line) {
3680 if (view->ncols <= 9) {
3682 wline = wcsdup(L"");
3684 err = got_error_from_errno("wcsdup");
3686 err = format_line(&wline, &width, line,
3687 view->ncols - 9, 9);
3695 if (view->focussed && nprinted == selected_line - 1)
3696 wstandout(view->window);
3699 blame_line = &lines[lineno - 1];
3700 if (blame_line->annotated && prev_id &&
3701 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3703 nprinted == selected_line - 1)) {
3704 waddstr(view->window, " ");
3705 } else if (blame_line->annotated) {
3707 err = got_object_id_str(&id_str, blame_line->id);
3713 tc = get_color(colors, TOG_COLOR_COMMIT);
3715 wattr_on(view->window,
3716 COLOR_PAIR(tc->colorpair), NULL);
3717 wprintw(view->window, "%.8s", id_str);
3719 wattr_off(view->window,
3720 COLOR_PAIR(tc->colorpair), NULL);
3722 prev_id = blame_line->id;
3724 waddstr(view->window, "........");
3728 waddstr(view->window, "........");
3732 if (view->focussed && nprinted == selected_line - 1)
3733 wstandend(view->window);
3734 waddstr(view->window, " ");
3736 waddwstr(view->window, wline);
3737 if (width <= view->ncols - 1)
3738 waddch(view->window, '\n');
3739 if (++nprinted == 1)
3740 *first_displayed_line = lineno;
3745 *last_displayed_line = lineno;
3752 static const struct got_error *
3753 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3755 const struct got_error *err = NULL;
3756 struct tog_blame_cb_args *a = arg;
3757 struct tog_blame_line *line;
3760 if (nlines != a->nlines ||
3761 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3762 return got_error(GOT_ERR_RANGE);
3764 errcode = pthread_mutex_lock(&tog_mutex);
3766 return got_error_set_errno(errcode, "pthread_mutex_lock");
3768 if (*a->quit) { /* user has quit the blame view */
3769 err = got_error(GOT_ERR_ITER_COMPLETED);
3774 goto done; /* no change in this commit */
3776 line = &a->lines[lineno - 1];
3777 if (line->annotated)
3780 line->id = got_object_id_dup(id);
3781 if (line->id == NULL) {
3782 err = got_error_from_errno("got_object_id_dup");
3785 line->annotated = 1;
3787 errcode = pthread_mutex_unlock(&tog_mutex);
3789 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3794 blame_thread(void *arg)
3796 const struct got_error *err;
3797 struct tog_blame_thread_args *ta = arg;
3798 struct tog_blame_cb_args *a = ta->cb_args;
3801 err = block_signals_used_by_main_thread();
3805 err = got_blame(ta->path, a->commit_id, ta->repo,
3806 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3807 if (err && err->code == GOT_ERR_CANCELLED)
3810 errcode = pthread_mutex_lock(&tog_mutex);
3812 return (void *)got_error_set_errno(errcode,
3813 "pthread_mutex_lock");
3815 got_repo_close(ta->repo);
3819 errcode = pthread_mutex_unlock(&tog_mutex);
3820 if (errcode && err == NULL)
3821 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3826 static struct got_object_id *
3827 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3828 int first_displayed_line, int selected_line)
3830 struct tog_blame_line *line;
3835 line = &lines[first_displayed_line - 1 + selected_line - 1];
3836 if (!line->annotated)
3842 static const struct got_error *
3843 stop_blame(struct tog_blame *blame)
3845 const struct got_error *err = NULL;
3848 if (blame->thread) {
3850 errcode = pthread_mutex_unlock(&tog_mutex);
3852 return got_error_set_errno(errcode,
3853 "pthread_mutex_unlock");
3854 errcode = pthread_join(blame->thread, (void **)&err);
3856 return got_error_set_errno(errcode, "pthread_join");
3857 errcode = pthread_mutex_lock(&tog_mutex);
3859 return got_error_set_errno(errcode,
3860 "pthread_mutex_lock");
3861 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3863 blame->thread = NULL;
3865 if (blame->thread_args.repo) {
3866 got_repo_close(blame->thread_args.repo);
3867 blame->thread_args.repo = NULL;
3870 if (fclose(blame->f) != 0 && err == NULL)
3871 err = got_error_from_errno("fclose");
3875 for (i = 0; i < blame->nlines; i++)
3876 free(blame->lines[i].id);
3878 blame->lines = NULL;
3880 free(blame->cb_args.commit_id);
3881 blame->cb_args.commit_id = NULL;
3886 static const struct got_error *
3887 cancel_blame_view(void *arg)
3889 const struct got_error *err = NULL;
3893 errcode = pthread_mutex_lock(&tog_mutex);
3895 return got_error_set_errno(errcode,
3896 "pthread_mutex_unlock");
3899 err = got_error(GOT_ERR_CANCELLED);
3901 errcode = pthread_mutex_unlock(&tog_mutex);
3903 return got_error_set_errno(errcode,
3904 "pthread_mutex_lock");
3909 static const struct got_error *
3910 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3911 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3912 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3913 struct got_repository *repo)
3915 const struct got_error *err = NULL;
3916 struct got_blob_object *blob = NULL;
3917 struct got_repository *thread_repo = NULL;
3918 struct got_object_id *obj_id = NULL;
3921 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3925 err = got_object_get_type(&obj_type, repo, obj_id);
3929 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3930 err = got_error(GOT_ERR_OBJ_TYPE);
3934 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3937 blame->f = got_opentemp();
3938 if (blame->f == NULL) {
3939 err = got_error_from_errno("got_opentemp");
3942 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3943 &blame->line_offsets, blame->f, blob);
3944 if (err || blame->nlines == 0)
3947 /* Don't include \n at EOF in the blame line count. */
3948 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3951 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3952 if (blame->lines == NULL) {
3953 err = got_error_from_errno("calloc");
3957 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3961 blame->cb_args.view = view;
3962 blame->cb_args.lines = blame->lines;
3963 blame->cb_args.nlines = blame->nlines;
3964 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3965 if (blame->cb_args.commit_id == NULL) {
3966 err = got_error_from_errno("got_object_id_dup");
3969 blame->cb_args.quit = done;
3971 blame->thread_args.path = path;
3972 blame->thread_args.repo = thread_repo;
3973 blame->thread_args.cb_args = &blame->cb_args;
3974 blame->thread_args.complete = blame_complete;
3975 blame->thread_args.cancel_cb = cancel_blame_view;
3976 blame->thread_args.cancel_arg = done;
3977 *blame_complete = 0;
3981 got_object_blob_close(blob);
3988 static const struct got_error *
3989 open_blame_view(struct tog_view *view, char *path,
3990 struct got_object_id *commit_id, struct got_reflist_head *refs,
3991 struct got_repository *repo)
3993 const struct got_error *err = NULL;
3994 struct tog_blame_view_state *s = &view->state.blame;
3996 SIMPLEQ_INIT(&s->blamed_commits);
3998 s->path = strdup(path);
3999 if (s->path == NULL)
4000 return got_error_from_errno("strdup");
4002 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4008 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4009 s->first_displayed_line = 1;
4010 s->last_displayed_line = view->nlines;
4011 s->selected_line = 1;
4012 s->blame_complete = 0;
4015 s->commit_id = commit_id;
4016 memset(&s->blame, 0, sizeof(s->blame));
4018 SIMPLEQ_INIT(&s->colors);
4019 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4020 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4021 get_color_value("TOG_COLOR_COMMIT"));
4026 view->show = show_blame_view;
4027 view->input = input_blame_view;
4028 view->close = close_blame_view;
4029 view->search_start = search_start_blame_view;
4030 view->search_next = search_next_blame_view;
4032 return run_blame(&s->blame, view, &s->blame_complete,
4033 &s->first_displayed_line, &s->last_displayed_line,
4034 &s->selected_line, &s->done, &s->eof, s->path,
4035 s->blamed_commit->id, s->repo);
4038 static const struct got_error *
4039 close_blame_view(struct tog_view *view)
4041 const struct got_error *err = NULL;
4042 struct tog_blame_view_state *s = &view->state.blame;
4044 if (s->blame.thread)
4045 err = stop_blame(&s->blame);
4047 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4048 struct got_object_qid *blamed_commit;
4049 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4050 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4051 got_object_qid_free(blamed_commit);
4055 free_colors(&s->colors);
4060 static const struct got_error *
4061 search_start_blame_view(struct tog_view *view)
4063 struct tog_blame_view_state *s = &view->state.blame;
4065 s->matched_line = 0;
4069 static const struct got_error *
4070 search_next_blame_view(struct tog_view *view)
4072 struct tog_blame_view_state *s = &view->state.blame;
4075 if (!view->searching) {
4076 view->search_next_done = 1;
4080 if (s->matched_line) {
4081 if (view->searching == TOG_SEARCH_FORWARD)
4082 lineno = s->matched_line + 1;
4084 lineno = s->matched_line - 1;
4086 if (view->searching == TOG_SEARCH_FORWARD)
4089 lineno = s->blame.nlines;
4097 if (lineno <= 0 || lineno > s->blame.nlines) {
4098 if (s->matched_line == 0) {
4099 view->search_next_done = 1;
4104 if (view->searching == TOG_SEARCH_FORWARD)
4107 lineno = s->blame.nlines;
4110 offset = s->blame.line_offsets[lineno - 1];
4111 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4113 return got_error_from_errno("fseeko");
4116 line = parse_next_line(s->blame.f, &len);
4117 if (line && match_line(line, &view->regex)) {
4118 view->search_next_done = 1;
4119 s->matched_line = lineno;
4124 if (view->searching == TOG_SEARCH_FORWARD)
4130 if (s->matched_line) {
4131 s->first_displayed_line = s->matched_line;
4132 s->selected_line = 1;
4138 static const struct got_error *
4139 show_blame_view(struct tog_view *view)
4141 const struct got_error *err = NULL;
4142 struct tog_blame_view_state *s = &view->state.blame;
4145 if (s->blame.thread == NULL) {
4146 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4147 &s->blame.thread_args);
4149 return got_error_set_errno(errcode, "pthread_create");
4151 halfdelay(1); /* fast refresh while annotating */
4154 if (s->blame_complete)
4155 halfdelay(10); /* disable fast refresh */
4157 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4158 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4159 s->selected_line, &s->first_displayed_line,
4160 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
4166 static const struct got_error *
4167 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4168 struct tog_view **focus_view, struct tog_view *view, int ch)
4170 const struct got_error *err = NULL, *thread_err = NULL;
4171 struct tog_view *diff_view;
4172 struct tog_blame_view_state *s = &view->state.blame;
4181 if (s->selected_line > 1)
4183 else if (s->selected_line == 1 &&
4184 s->first_displayed_line > 1)
4185 s->first_displayed_line--;
4189 if (s->first_displayed_line == 1) {
4190 s->selected_line = 1;
4193 if (s->first_displayed_line > view->nlines - 2)
4194 s->first_displayed_line -=
4197 s->first_displayed_line = 1;
4201 if (s->selected_line < view->nlines - 2 &&
4202 s->first_displayed_line +
4203 s->selected_line <= s->blame.nlines)
4205 else if (s->last_displayed_line <
4207 s->first_displayed_line++;
4211 struct got_object_id *id = NULL;
4212 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4213 s->first_displayed_line, s->selected_line);
4217 struct got_commit_object *commit;
4218 struct got_object_qid *pid;
4219 struct got_object_id *blob_id = NULL;
4221 err = got_object_open_as_commit(&commit,
4225 pid = SIMPLEQ_FIRST(
4226 got_object_commit_get_parent_ids(commit));
4228 got_object_commit_close(commit);
4231 /* Check if path history ends here. */
4232 err = got_object_id_by_path(&blob_id, s->repo,
4235 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4237 got_object_commit_close(commit);
4240 err = got_object_get_type(&obj_type, s->repo,
4243 /* Can't blame non-blob type objects. */
4244 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4245 got_object_commit_close(commit);
4248 err = got_object_qid_alloc(&s->blamed_commit,
4250 got_object_commit_close(commit);
4252 if (got_object_id_cmp(id,
4253 s->blamed_commit->id) == 0)
4255 err = got_object_qid_alloc(&s->blamed_commit,
4261 thread_err = stop_blame(&s->blame);
4265 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4266 s->blamed_commit, entry);
4267 err = run_blame(&s->blame, view, &s->blame_complete,
4268 &s->first_displayed_line, &s->last_displayed_line,
4269 &s->selected_line, &s->done, &s->eof,
4270 s->path, s->blamed_commit->id, s->repo);
4276 struct got_object_qid *first;
4277 first = SIMPLEQ_FIRST(&s->blamed_commits);
4278 if (!got_object_id_cmp(first->id, s->commit_id))
4281 thread_err = stop_blame(&s->blame);
4285 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4286 got_object_qid_free(s->blamed_commit);
4288 SIMPLEQ_FIRST(&s->blamed_commits);
4289 err = run_blame(&s->blame, view, &s->blame_complete,
4290 &s->first_displayed_line, &s->last_displayed_line,
4291 &s->selected_line, &s->done, &s->eof, s->path,
4292 s->blamed_commit->id, s->repo);
4299 struct got_object_id *id = NULL;
4300 struct got_object_qid *pid;
4301 struct got_commit_object *commit = NULL;
4302 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4303 s->first_displayed_line, s->selected_line);
4306 err = got_object_open_as_commit(&commit, s->repo, id);
4309 pid = SIMPLEQ_FIRST(
4310 got_object_commit_get_parent_ids(commit));
4311 if (view_is_parent_view(view))
4312 begin_x = view_split_begin_x(view->begin_x);
4313 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4314 if (diff_view == NULL) {
4315 got_object_commit_close(commit);
4316 err = got_error_from_errno("view_open");
4319 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4320 id, NULL, s->refs, s->repo);
4321 got_object_commit_close(commit);
4323 view_close(diff_view);
4326 if (view_is_parent_view(view)) {
4327 err = view_close_child(view);
4330 err = view_set_child(view, diff_view);
4332 view_close(diff_view);
4335 *focus_view = diff_view;
4336 view->child_focussed = 1;
4338 *new_view = diff_view;
4346 if (s->last_displayed_line >= s->blame.nlines &&
4347 s->selected_line >= MIN(s->blame.nlines,
4348 view->nlines - 2)) {
4351 if (s->last_displayed_line >= s->blame.nlines &&
4352 s->selected_line < view->nlines - 2) {
4353 s->selected_line = MIN(s->blame.nlines,
4357 if (s->last_displayed_line + view->nlines - 2
4359 s->first_displayed_line +=
4362 s->first_displayed_line =
4367 if (s->selected_line > view->nlines - 2) {
4368 s->selected_line = MIN(s->blame.nlines,
4375 return thread_err ? thread_err : err;
4378 static const struct got_error *
4379 cmd_blame(int argc, char *argv[])
4381 const struct got_error *error;
4382 struct got_repository *repo = NULL;
4383 struct got_reflist_head refs;
4384 struct got_worktree *worktree = NULL;
4385 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4386 struct got_object_id *commit_id = NULL;
4387 char *commit_id_str = NULL;
4389 struct tog_view *view;
4391 SIMPLEQ_INIT(&refs);
4394 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4399 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4402 commit_id_str = optarg;
4405 repo_path = realpath(optarg, NULL);
4406 if (repo_path == NULL)
4407 return got_error_from_errno2("realpath",
4422 cwd = getcwd(NULL, 0);
4424 return got_error_from_errno("getcwd");
4426 error = got_worktree_open(&worktree, cwd);
4427 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4430 if (repo_path == NULL) {
4433 strdup(got_worktree_get_repo_path(worktree));
4435 repo_path = strdup(cwd);
4437 if (repo_path == NULL) {
4438 error = got_error_from_errno("strdup");
4442 error = got_repo_open(&repo, repo_path, NULL);
4446 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4453 error = apply_unveil(got_repo_get_path(repo), NULL);
4457 if (commit_id_str == NULL) {
4458 struct got_reference *head_ref;
4459 error = got_ref_open(&head_ref, repo, worktree ?
4460 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4463 error = got_ref_resolve(&commit_id, repo, head_ref);
4464 got_ref_close(head_ref);
4466 error = got_repo_match_object_id(&commit_id, NULL,
4467 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4472 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4476 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4478 error = got_error_from_errno("view_open");
4481 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4485 /* Release work tree lock. */
4486 got_worktree_close(worktree);
4489 error = view_loop(view);
4496 got_worktree_close(worktree);
4498 got_repo_close(repo);
4499 got_ref_list_free(&refs);
4503 static const struct got_error *
4504 draw_tree_entries(struct tog_view *view,
4505 struct got_tree_entry **first_displayed_entry,
4506 struct got_tree_entry **last_displayed_entry,
4507 struct got_tree_entry **selected_entry, int *ndisplayed,
4508 const char *label, int show_ids, const char *parent_path,
4509 struct got_tree_object *tree, int selected, int limit,
4510 int isroot, struct tog_colors *colors)
4512 const struct got_error *err = NULL;
4513 struct got_tree_entry *te;
4515 struct tog_color *tc;
4516 int width, n, i, nentries;
4520 werase(view->window);
4525 err = format_line(&wline, &width, label, view->ncols, 0);
4528 if (view_needs_focus_indication(view))
4529 wstandout(view->window);
4530 tc = get_color(colors, TOG_COLOR_COMMIT);
4532 wattr_on(view->window,
4533 COLOR_PAIR(tc->colorpair), NULL);
4534 waddwstr(view->window, wline);
4536 wattr_off(view->window,
4537 COLOR_PAIR(tc->colorpair), NULL);
4538 if (view_needs_focus_indication(view))
4539 wstandend(view->window);
4542 if (width < view->ncols - 1)
4543 waddch(view->window, '\n');
4546 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4549 waddwstr(view->window, wline);
4552 if (width < view->ncols - 1)
4553 waddch(view->window, '\n');
4556 waddch(view->window, '\n');
4560 if (*first_displayed_entry == NULL) {
4561 te = got_object_tree_get_first_entry(tree);
4562 if (selected == 0) {
4564 wstandout(view->window);
4565 *selected_entry = NULL;
4567 waddstr(view->window, " ..\n"); /* parent directory */
4568 if (selected == 0 && view->focussed)
4569 wstandend(view->window);
4576 te = *first_displayed_entry;
4579 nentries = got_object_tree_get_nentries(tree);
4580 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4581 char *line = NULL, *id_str = NULL;
4582 const char *modestr = "";
4585 te = got_object_tree_get_entry(tree, i);
4586 mode = got_tree_entry_get_mode(te);
4589 err = got_object_id_str(&id_str,
4590 got_tree_entry_get_id(te));
4592 return got_error_from_errno(
4593 "got_object_id_str");
4595 if (got_object_tree_entry_is_submodule(te))
4597 else if (S_ISLNK(mode))
4599 else if (S_ISDIR(mode))
4601 else if (mode & S_IXUSR)
4603 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4604 got_tree_entry_get_name(te), modestr) == -1) {
4606 return got_error_from_errno("asprintf");
4609 err = format_line(&wline, &width, line, view->ncols, 0);
4614 if (n == selected) {
4616 wstandout(view->window);
4617 *selected_entry = te;
4619 tc = match_color(colors, line);
4621 wattr_on(view->window,
4622 COLOR_PAIR(tc->colorpair), NULL);
4623 waddwstr(view->window, wline);
4625 wattr_off(view->window,
4626 COLOR_PAIR(tc->colorpair), NULL);
4627 if (width < view->ncols - 1)
4628 waddch(view->window, '\n');
4629 if (n == selected && view->focussed)
4630 wstandend(view->window);
4636 *last_displayed_entry = te;
4645 tree_scroll_up(struct tog_view *view,
4646 struct got_tree_entry **first_displayed_entry, int maxscroll,
4647 struct got_tree_object *tree, int isroot)
4649 struct got_tree_entry *te;
4652 if (*first_displayed_entry == NULL)
4655 te = got_object_tree_get_entry(tree, 0);
4656 if (*first_displayed_entry == te) {
4658 *first_displayed_entry = NULL;
4663 while (*first_displayed_entry && i < maxscroll) {
4664 *first_displayed_entry = got_tree_entry_get_prev(tree,
4665 *first_displayed_entry);
4668 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4669 *first_displayed_entry = NULL;
4673 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4674 struct got_tree_entry *last_displayed_entry,
4675 struct got_tree_object *tree)
4677 struct got_tree_entry *next, *last;
4680 if (*first_displayed_entry)
4681 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4683 next = got_object_tree_get_first_entry(tree);
4685 last = last_displayed_entry;
4686 while (next && last && n++ < maxscroll) {
4687 last = got_tree_entry_get_next(tree, last);
4689 *first_displayed_entry = next;
4690 next = got_tree_entry_get_next(tree, next);
4696 static const struct got_error *
4697 tree_entry_path(char **path, struct tog_parent_trees *parents,
4698 struct got_tree_entry *te)
4700 const struct got_error *err = NULL;
4701 struct tog_parent_tree *pt;
4702 size_t len = 2; /* for leading slash and NUL */
4704 TAILQ_FOREACH(pt, parents, entry)
4705 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4708 len += strlen(got_tree_entry_get_name(te));
4710 *path = calloc(1, len);
4712 return got_error_from_errno("calloc");
4715 pt = TAILQ_LAST(parents, tog_parent_trees);
4717 const char *name = got_tree_entry_get_name(pt->selected_entry);
4718 if (strlcat(*path, name, len) >= len) {
4719 err = got_error(GOT_ERR_NO_SPACE);
4722 if (strlcat(*path, "/", len) >= len) {
4723 err = got_error(GOT_ERR_NO_SPACE);
4726 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4729 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4730 err = got_error(GOT_ERR_NO_SPACE);
4742 static const struct got_error *
4743 blame_tree_entry(struct tog_view **new_view, int begin_x,
4744 struct got_tree_entry *te, struct tog_parent_trees *parents,
4745 struct got_object_id *commit_id, struct got_reflist_head *refs,
4746 struct got_repository *repo)
4748 const struct got_error *err = NULL;
4750 struct tog_view *blame_view;
4754 err = tree_entry_path(&path, parents, te);
4758 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4759 if (blame_view == NULL) {
4760 err = got_error_from_errno("view_open");
4764 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4766 if (err->code == GOT_ERR_CANCELLED)
4768 view_close(blame_view);
4770 *new_view = blame_view;
4776 static const struct got_error *
4777 log_tree_entry(struct tog_view **new_view, int begin_x,
4778 struct got_tree_entry *te, struct tog_parent_trees *parents,
4779 struct got_object_id *commit_id, struct got_reflist_head *refs,
4780 struct got_repository *repo)
4782 struct tog_view *log_view;
4783 const struct got_error *err = NULL;
4788 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4789 if (log_view == NULL)
4790 return got_error_from_errno("view_open");
4792 err = tree_entry_path(&path, parents, te);
4796 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4798 view_close(log_view);
4800 *new_view = log_view;
4805 static const struct got_error *
4806 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4807 struct got_object_id *commit_id, struct got_reflist_head *refs,
4808 struct got_repository *repo)
4810 const struct got_error *err = NULL;
4811 char *commit_id_str = NULL;
4812 struct tog_tree_view_state *s = &view->state.tree;
4814 TAILQ_INIT(&s->parents);
4816 err = got_object_id_str(&commit_id_str, commit_id);
4820 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4821 err = got_error_from_errno("asprintf");
4825 s->root = s->tree = root;
4826 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4827 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4828 s->commit_id = got_object_id_dup(commit_id);
4829 if (s->commit_id == NULL) {
4830 err = got_error_from_errno("got_object_id_dup");
4836 SIMPLEQ_INIT(&s->colors);
4838 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4839 err = add_color(&s->colors, "\\$$",
4840 TOG_COLOR_TREE_SUBMODULE,
4841 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4844 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4845 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4847 free_colors(&s->colors);
4850 err = add_color(&s->colors, "/$",
4851 TOG_COLOR_TREE_DIRECTORY,
4852 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4854 free_colors(&s->colors);
4858 err = add_color(&s->colors, "\\*$",
4859 TOG_COLOR_TREE_EXECUTABLE,
4860 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4862 free_colors(&s->colors);
4866 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4867 get_color_value("TOG_COLOR_COMMIT"));
4869 free_colors(&s->colors);
4874 view->show = show_tree_view;
4875 view->input = input_tree_view;
4876 view->close = close_tree_view;
4877 view->search_start = search_start_tree_view;
4878 view->search_next = search_next_tree_view;
4880 free(commit_id_str);
4882 free(s->tree_label);
4883 s->tree_label = NULL;
4888 static const struct got_error *
4889 close_tree_view(struct tog_view *view)
4891 struct tog_tree_view_state *s = &view->state.tree;
4893 free_colors(&s->colors);
4894 free(s->tree_label);
4895 s->tree_label = NULL;
4897 s->commit_id = NULL;
4898 while (!TAILQ_EMPTY(&s->parents)) {
4899 struct tog_parent_tree *parent;
4900 parent = TAILQ_FIRST(&s->parents);
4901 TAILQ_REMOVE(&s->parents, parent, entry);
4905 if (s->tree != s->root)
4906 got_object_tree_close(s->tree);
4907 got_object_tree_close(s->root);
4912 static const struct got_error *
4913 search_start_tree_view(struct tog_view *view)
4915 struct tog_tree_view_state *s = &view->state.tree;
4917 s->matched_entry = NULL;
4922 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4924 regmatch_t regmatch;
4926 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
4930 static const struct got_error *
4931 search_next_tree_view(struct tog_view *view)
4933 struct tog_tree_view_state *s = &view->state.tree;
4934 struct got_tree_entry *te = NULL;
4936 if (!view->searching) {
4937 view->search_next_done = 1;
4941 if (s->matched_entry) {
4942 if (view->searching == TOG_SEARCH_FORWARD) {
4943 if (s->selected_entry)
4944 te = got_tree_entry_get_next(s->tree,
4947 te = got_object_tree_get_first_entry(s->tree);
4949 if (s->selected_entry == NULL)
4950 te = got_object_tree_get_last_entry(s->tree);
4952 te = got_tree_entry_get_prev(s->tree,
4956 if (view->searching == TOG_SEARCH_FORWARD)
4957 te = got_object_tree_get_first_entry(s->tree);
4959 te = got_object_tree_get_last_entry(s->tree);
4964 if (s->matched_entry == NULL) {
4965 view->search_next_done = 1;
4968 if (view->searching == TOG_SEARCH_FORWARD)
4969 te = got_object_tree_get_first_entry(s->tree);
4971 te = got_object_tree_get_last_entry(s->tree);
4974 if (match_tree_entry(te, &view->regex)) {
4975 view->search_next_done = 1;
4976 s->matched_entry = te;
4980 if (view->searching == TOG_SEARCH_FORWARD)
4981 te = got_tree_entry_get_next(s->tree, te);
4983 te = got_tree_entry_get_prev(s->tree, te);
4986 if (s->matched_entry) {
4987 s->first_displayed_entry = s->matched_entry;
4994 static const struct got_error *
4995 show_tree_view(struct tog_view *view)
4997 const struct got_error *err = NULL;
4998 struct tog_tree_view_state *s = &view->state.tree;
5001 err = tree_entry_path(&parent_path, &s->parents, NULL);
5005 err = draw_tree_entries(view, &s->first_displayed_entry,
5006 &s->last_displayed_entry, &s->selected_entry,
5007 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5008 s->tree, s->selected, view->nlines, s->tree == s->root,
5016 static const struct got_error *
5017 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5018 struct tog_view **focus_view, struct tog_view *view, int ch)
5020 const struct got_error *err = NULL;
5021 struct tog_tree_view_state *s = &view->state.tree;
5022 struct tog_view *log_view;
5023 int begin_x = 0, nscrolled;
5027 s->show_ids = !s->show_ids;
5030 if (!s->selected_entry)
5032 if (view_is_parent_view(view))
5033 begin_x = view_split_begin_x(view->begin_x);
5034 err = log_tree_entry(&log_view, begin_x,
5035 s->selected_entry, &s->parents,
5036 s->commit_id, s->refs, s->repo);
5037 if (view_is_parent_view(view)) {
5038 err = view_close_child(view);
5041 err = view_set_child(view, log_view);
5043 view_close(log_view);
5046 *focus_view = log_view;
5047 view->child_focussed = 1;
5049 *new_view = log_view;
5053 if (s->selected > 0) {
5055 if (s->selected == 0)
5058 if (s->selected > 0)
5060 tree_scroll_up(view, &s->first_displayed_entry, 1,
5061 s->tree, s->tree == s->root);
5065 tree_scroll_up(view, &s->first_displayed_entry,
5066 MAX(0, view->nlines - 4 - s->selected), s->tree,
5067 s->tree == s->root);
5069 if (got_object_tree_get_first_entry(s->tree) ==
5070 s->first_displayed_entry && s->tree != s->root)
5071 s->first_displayed_entry = NULL;
5075 if (s->selected < s->ndisplayed - 1) {
5079 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5081 /* can't scroll any further */
5083 tree_scroll_down(&s->first_displayed_entry, 1,
5084 s->last_displayed_entry, s->tree);
5088 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5090 /* can't scroll any further; move cursor down */
5091 if (s->selected < s->ndisplayed - 1)
5092 s->selected = s->ndisplayed - 1;
5095 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5096 view->nlines, s->last_displayed_entry, s->tree);
5097 if (nscrolled < view->nlines) {
5099 struct got_tree_entry *te;
5100 te = s->first_displayed_entry;
5103 te = got_tree_entry_get_next(s->tree, te);
5105 s->selected = ndisplayed - 1;
5111 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5112 struct tog_parent_tree *parent;
5113 /* user selected '..' */
5114 if (s->tree == s->root)
5116 parent = TAILQ_FIRST(&s->parents);
5117 TAILQ_REMOVE(&s->parents, parent,
5119 got_object_tree_close(s->tree);
5120 s->tree = parent->tree;
5121 s->first_displayed_entry =
5122 parent->first_displayed_entry;
5124 parent->selected_entry;
5125 s->selected = parent->selected;
5127 } else if (S_ISDIR(got_tree_entry_get_mode(
5128 s->selected_entry))) {
5129 struct got_tree_object *subtree;
5130 err = got_object_open_as_tree(&subtree, s->repo,
5131 got_tree_entry_get_id(s->selected_entry));
5134 err = tree_view_visit_subtree(subtree, s);
5136 got_object_tree_close(subtree);
5139 } else if (S_ISREG(got_tree_entry_get_mode(
5140 s->selected_entry))) {
5141 struct tog_view *blame_view;
5142 int begin_x = view_is_parent_view(view) ?
5143 view_split_begin_x(view->begin_x) : 0;
5145 err = blame_tree_entry(&blame_view, begin_x,
5146 s->selected_entry, &s->parents,
5147 s->commit_id, s->refs, s->repo);
5150 if (view_is_parent_view(view)) {
5151 err = view_close_child(view);
5154 err = view_set_child(view, blame_view);
5156 view_close(blame_view);
5159 *focus_view = blame_view;
5160 view->child_focussed = 1;
5162 *new_view = blame_view;
5166 if (s->selected > view->nlines)
5167 s->selected = s->ndisplayed - 1;
5180 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5185 static const struct got_error *
5186 cmd_tree(int argc, char *argv[])
5188 const struct got_error *error;
5189 struct got_repository *repo = NULL;
5190 struct got_worktree *worktree = NULL;
5191 struct got_reflist_head refs;
5192 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5193 struct got_object_id *commit_id = NULL;
5194 char *commit_id_arg = NULL;
5195 struct got_commit_object *commit = NULL;
5196 struct got_tree_object *tree = NULL;
5198 struct tog_view *view;
5200 SIMPLEQ_INIT(&refs);
5203 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5208 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5211 commit_id_arg = optarg;
5214 repo_path = realpath(optarg, NULL);
5215 if (repo_path == NULL)
5216 return got_error_from_errno2("realpath",
5231 cwd = getcwd(NULL, 0);
5233 return got_error_from_errno("getcwd");
5235 error = got_worktree_open(&worktree, cwd);
5236 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5239 if (repo_path == NULL) {
5242 strdup(got_worktree_get_repo_path(worktree));
5244 repo_path = strdup(cwd);
5246 if (repo_path == NULL) {
5247 error = got_error_from_errno("strdup");
5251 error = got_repo_open(&repo, repo_path, NULL);
5255 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5262 error = apply_unveil(got_repo_get_path(repo), NULL);
5266 error = got_repo_match_object_id(&commit_id, NULL,
5267 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5268 GOT_OBJ_TYPE_COMMIT, 1, repo);
5272 error = got_object_open_as_commit(&commit, repo, commit_id);
5276 error = got_object_open_as_tree(&tree, repo,
5277 got_object_commit_get_tree_id(commit));
5281 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5285 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5287 error = got_error_from_errno("view_open");
5290 error = open_tree_view(view, tree, commit_id, &refs, repo);
5293 if (!got_path_is_root_dir(in_repo_path)) {
5294 error = tree_view_walk_path(&view->state.tree, commit_id,
5295 in_repo_path, repo);
5301 /* Release work tree lock. */
5302 got_worktree_close(worktree);
5305 error = view_loop(view);
5311 got_object_commit_close(commit);
5313 got_object_tree_close(tree);
5315 got_repo_close(repo);
5316 got_ref_list_free(&refs);
5325 fprintf(stderr, "commands:");
5326 for (i = 0; i < nitems(tog_commands); i++) {
5327 struct tog_cmd *cmd = &tog_commands[i];
5328 fprintf(stderr, " %s", cmd->name);
5330 fputc('\n', stderr);
5336 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5344 make_argv(const char *arg0, const char *arg1)
5347 int argc = (arg1 == NULL ? 1 : 2);
5349 argv = calloc(argc, sizeof(char *));
5352 argv[0] = strdup(arg0);
5353 if (argv[0] == NULL)
5356 argv[1] = strdup(arg1);
5357 if (argv[1] == NULL)
5365 main(int argc, char *argv[])
5367 const struct got_error *error = NULL;
5368 struct tog_cmd *cmd = NULL;
5369 int ch, hflag = 0, Vflag = 0;
5370 char **cmd_argv = NULL;
5371 static struct option longopts[] = {
5372 { "version", no_argument, NULL, 'V' },
5376 setlocale(LC_CTYPE, "");
5378 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5398 got_version_print_str();
5405 /* Build an argument vector which runs a default command. */
5406 cmd = &tog_commands[0];
5407 cmd_argv = make_argv(cmd->name, NULL);
5412 /* Did the user specific a command? */
5413 for (i = 0; i < nitems(tog_commands); i++) {
5414 if (strncmp(tog_commands[i].name, argv[0],
5415 strlen(argv[0])) == 0) {
5416 cmd = &tog_commands[i];
5422 fprintf(stderr, "%s: unknown command '%s'\n",
5423 getprogname(), argv[0]);
5432 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5436 if (error && error->code != GOT_ERR_CANCELLED)
5437 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);