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, 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 *,
472 struct got_reflist_head *, 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;
707 if (view->nlines < 1)
710 if (!view_is_parent_view(view))
711 begin_x = view_split_begin_x(view->begin_x);
712 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
714 wclrtoeol(view->window);
718 ret = wgetnstr(view->window, pattern, sizeof(pattern));
724 if (view->searching) {
725 regfree(&view->regex);
729 if (regcomp(&view->regex, pattern,
730 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
731 err = view->search_start(view);
733 regfree(&view->regex);
736 view->searching = TOG_SEARCH_FORWARD;
737 view->search_next_done = 0;
738 view->search_next(view);
744 static const struct got_error *
745 view_input(struct tog_view **new, struct tog_view **dead,
746 struct tog_view **focus, int *done, struct tog_view *view,
747 struct tog_view_list_head *views)
749 const struct got_error *err = NULL;
757 if (view->searching && !view->search_next_done) {
758 view->search_next(view);
762 nodelay(stdscr, FALSE);
763 /* Allow threads to make progress while we are waiting for input. */
764 errcode = pthread_mutex_unlock(&tog_mutex);
766 return got_error_set_errno(errcode, "pthread_mutex_unlock");
767 ch = wgetch(view->window);
768 errcode = pthread_mutex_lock(&tog_mutex);
770 return got_error_set_errno(errcode, "pthread_mutex_lock");
771 nodelay(stdscr, TRUE);
773 if (tog_sigwinch_received || tog_sigcont_received) {
775 tog_sigwinch_received = 0;
776 tog_sigcont_received = 0;
777 TAILQ_FOREACH(v, views, entry) {
778 err = view_resize(v);
781 err = v->input(new, dead, focus, v, KEY_RESIZE);
792 *focus = view->child;
793 view->child_focussed = 1;
794 } else if (view->parent) {
795 *focus = view->parent;
796 view->parent->child_focussed = 0;
800 err = view->input(new, dead, focus, view, ch);
807 if (view_is_parent_view(view)) {
808 if (view->child == NULL)
810 if (view_is_splitscreen(view->child)) {
811 *focus = view->child;
812 view->child_focussed = 1;
813 err = view_fullscreen(view->child);
815 err = view_splitscreen(view->child);
818 err = view->child->input(new, dead, focus,
819 view->child, KEY_RESIZE);
821 if (view_is_splitscreen(view)) {
823 view->parent->child_focussed = 1;
824 err = view_fullscreen(view);
826 err = view_splitscreen(view);
830 err = view->input(new, dead, focus, view,
837 if (view->search_start)
838 view_search_start(view);
840 err = view->input(new, dead, focus, view, ch);
844 if (view->search_next && view->searching) {
845 view->searching = (ch == 'n' ?
846 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
847 view->search_next_done = 0;
848 view->search_next(view);
850 err = view->input(new, dead, focus, view, ch);
853 err = view->input(new, dead, focus, view, ch);
861 view_vborder(struct tog_view *view)
864 struct tog_view *view_above;
867 return view_vborder(view->parent);
869 panel = panel_above(view->panel);
873 view_above = panel_userptr(panel);
874 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
875 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
879 view_needs_focus_indication(struct tog_view *view)
881 if (view_is_parent_view(view)) {
882 if (view->child == NULL || view->child_focussed)
884 if (!view_is_splitscreen(view->child))
886 } else if (!view_is_splitscreen(view))
889 return view->focussed;
892 static const struct got_error *
893 view_loop(struct tog_view *view)
895 const struct got_error *err = NULL;
896 struct tog_view_list_head views;
897 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
898 int fast_refresh = 10;
899 int done = 0, errcode;
901 errcode = pthread_mutex_lock(&tog_mutex);
903 return got_error_set_errno(errcode, "pthread_mutex_lock");
906 TAILQ_INSERT_HEAD(&views, view, entry);
910 err = view->show(view);
915 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
916 /* Refresh fast during initialization, then become slower. */
917 if (fast_refresh && fast_refresh-- == 0)
918 halfdelay(10); /* switch to once per second */
920 err = view_input(&new_view, &dead_view, &focus_view, &done,
925 struct tog_view *prev = NULL;
927 if (view_is_parent_view(dead_view))
928 prev = TAILQ_PREV(dead_view,
929 tog_view_list_head, entry);
930 else if (view->parent != dead_view)
933 if (dead_view->parent)
934 dead_view->parent->child = NULL;
936 TAILQ_REMOVE(&views, dead_view, entry);
938 err = view_close(dead_view);
939 if (err || (dead_view == main_view && new_view == NULL))
942 if (view == dead_view) {
947 else if (!TAILQ_EMPTY(&views))
948 view = TAILQ_LAST(&views,
953 if (view->child && view->child_focussed)
954 focus_view = view->child;
961 struct tog_view *v, *t;
962 /* Only allow one parent view per type. */
963 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
964 if (v->type != new_view->type)
966 TAILQ_REMOVE(&views, v, entry);
972 TAILQ_INSERT_TAIL(&views, new_view, entry);
974 if (focus_view == NULL)
975 focus_view = new_view;
978 show_panel(focus_view->panel);
981 focus_view->focussed = 1;
984 show_panel(new_view->panel);
985 if (view->child && view_is_splitscreen(view->child))
986 show_panel(view->child->panel);
989 if (focus_view == NULL) {
991 show_panel(view->panel);
992 if (view->child && view_is_splitscreen(view->child))
993 show_panel(view->child->panel);
997 err = view->parent->show(view->parent);
1001 err = view->show(view);
1005 err = view->child->show(view->child);
1014 while (!TAILQ_EMPTY(&views)) {
1015 view = TAILQ_FIRST(&views);
1016 TAILQ_REMOVE(&views, view, entry);
1020 errcode = pthread_mutex_unlock(&tog_mutex);
1021 if (errcode && err == NULL)
1022 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1032 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1037 /* Create newly allocated wide-character string equivalent to a byte string. */
1038 static const struct got_error *
1039 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1042 const struct got_error *err = NULL;
1045 *wlen = mbstowcs(NULL, s, 0);
1046 if (*wlen == (size_t)-1) {
1048 if (errno != EILSEQ)
1049 return got_error_from_errno("mbstowcs");
1051 /* byte string invalid in current encoding; try to "fix" it */
1052 err = got_mbsavis(&vis, &vislen, s);
1055 *wlen = mbstowcs(NULL, vis, 0);
1056 if (*wlen == (size_t)-1) {
1057 err = got_error_from_errno("mbstowcs"); /* give up */
1062 *ws = calloc(*wlen + 1, sizeof(**ws));
1064 err = got_error_from_errno("calloc");
1068 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1069 err = got_error_from_errno("mbstowcs");
1080 /* Format a line for display, ensuring that it won't overflow a width limit. */
1081 static const struct got_error *
1082 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1085 const struct got_error *err = NULL;
1087 wchar_t *wline = NULL;
1094 err = mbs2ws(&wline, &wlen, line);
1100 int width = wcwidth(wline[i]);
1107 if (width == 1 || width == 2) {
1108 if (cols + width > wlimit)
1112 } else if (width == -1) {
1113 if (wline[i] == L'\t') {
1115 ((cols + col_tab_align) % TABSIZE);
1116 if (cols + width > wlimit)
1122 err = got_error_from_errno("wcwidth");
1137 static const struct got_error*
1138 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1139 struct got_object_id *id, struct got_repository *repo)
1141 static const struct got_error *err = NULL;
1142 struct got_reflist_entry *re;
1148 SIMPLEQ_FOREACH(re, refs, entry) {
1149 struct got_tag_object *tag = NULL;
1152 name = got_ref_get_name(re->ref);
1153 if (strcmp(name, GOT_REF_HEAD) == 0)
1155 if (strncmp(name, "refs/", 5) == 0)
1157 if (strncmp(name, "got/", 4) == 0)
1159 if (strncmp(name, "heads/", 6) == 0)
1161 if (strncmp(name, "remotes/", 8) == 0)
1163 if (strncmp(name, "tags/", 5) == 0) {
1164 err = got_object_open_as_tag(&tag, repo, re->id);
1166 if (err->code != GOT_ERR_OBJ_TYPE)
1168 /* Ref points at something other than a tag. */
1173 cmp = got_object_id_cmp(tag ?
1174 got_object_tag_get_object_id(tag) : re->id, id);
1176 got_object_tag_close(tag);
1180 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1181 s ? ", " : "", name) == -1) {
1182 err = got_error_from_errno("asprintf");
1193 static const struct got_error *
1194 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1197 char *smallerthan, *at;
1199 smallerthan = strchr(author, '<');
1200 if (smallerthan && smallerthan[1] != '\0')
1201 author = smallerthan + 1;
1202 at = strchr(author, '@');
1205 return format_line(wauthor, author_width, author, limit, col_tab_align);
1208 static const struct got_error *
1209 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1210 struct got_object_id *id, struct got_reflist_head *refs,
1211 const size_t date_display_cols, int author_display_cols,
1212 struct tog_colors *colors)
1214 const struct got_error *err = NULL;
1215 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1216 char *logmsg0 = NULL, *logmsg = NULL;
1217 char *author = NULL;
1218 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1219 int author_width, logmsg_width;
1220 char *newline, *line = NULL;
1222 const int avail = view->ncols;
1224 time_t committer_time;
1225 struct tog_color *tc;
1227 committer_time = got_object_commit_get_committer_time(commit);
1228 if (localtime_r(&committer_time, &tm) == NULL)
1229 return got_error_from_errno("localtime_r");
1230 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1232 return got_error(GOT_ERR_NO_SPACE);
1234 if (avail <= date_display_cols)
1235 limit = MIN(sizeof(datebuf) - 1, avail);
1237 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1238 tc = get_color(colors, TOG_COLOR_DATE);
1240 wattr_on(view->window,
1241 COLOR_PAIR(tc->colorpair), NULL);
1242 waddnstr(view->window, datebuf, limit);
1244 wattr_off(view->window,
1245 COLOR_PAIR(tc->colorpair), NULL);
1252 err = got_object_id_str(&id_str, id);
1255 tc = get_color(colors, TOG_COLOR_COMMIT);
1257 wattr_on(view->window,
1258 COLOR_PAIR(tc->colorpair), NULL);
1259 wprintw(view->window, "%.8s ", id_str);
1261 wattr_off(view->window,
1262 COLOR_PAIR(tc->colorpair), NULL);
1269 author = strdup(got_object_commit_get_author(commit));
1270 if (author == NULL) {
1271 err = got_error_from_errno("strdup");
1274 err = format_author(&wauthor, &author_width, author, avail - col, col);
1277 tc = get_color(colors, TOG_COLOR_AUTHOR);
1279 wattr_on(view->window,
1280 COLOR_PAIR(tc->colorpair), NULL);
1281 waddwstr(view->window, wauthor);
1283 wattr_off(view->window,
1284 COLOR_PAIR(tc->colorpair), NULL);
1285 col += author_width;
1286 while (col < avail && author_width < author_display_cols + 2) {
1287 waddch(view->window, ' ');
1294 err = got_object_commit_get_logmsg(&logmsg0, commit);
1298 while (*logmsg == '\n')
1300 newline = strchr(logmsg, '\n');
1303 limit = avail - col;
1304 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1307 waddwstr(view->window, wlogmsg);
1308 col += logmsg_width;
1309 while (col < avail) {
1310 waddch(view->window, ' ');
1322 static struct commit_queue_entry *
1323 alloc_commit_queue_entry(struct got_commit_object *commit,
1324 struct got_object_id *id)
1326 struct commit_queue_entry *entry;
1328 entry = calloc(1, sizeof(*entry));
1333 entry->commit = commit;
1338 pop_commit(struct commit_queue *commits)
1340 struct commit_queue_entry *entry;
1342 entry = TAILQ_FIRST(&commits->head);
1343 TAILQ_REMOVE(&commits->head, entry, entry);
1344 got_object_commit_close(entry->commit);
1345 commits->ncommits--;
1346 /* Don't free entry->id! It is owned by the commit graph. */
1351 free_commits(struct commit_queue *commits)
1353 while (!TAILQ_EMPTY(&commits->head))
1354 pop_commit(commits);
1357 static const struct got_error *
1358 match_commit(int *have_match, struct got_object_id *id,
1359 struct got_commit_object *commit, regex_t *regex)
1361 const struct got_error *err = NULL;
1362 regmatch_t regmatch;
1363 char *id_str = NULL, *logmsg = NULL;
1367 err = got_object_id_str(&id_str, id);
1371 err = got_object_commit_get_logmsg(&logmsg, commit);
1375 if (regexec(regex, got_object_commit_get_author(commit), 1,
1376 ®match, 0) == 0 ||
1377 regexec(regex, got_object_commit_get_committer(commit), 1,
1378 ®match, 0) == 0 ||
1379 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1380 regexec(regex, logmsg, 1, ®match, 0) == 0)
1388 static const struct got_error *
1389 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1390 int minqueue, struct got_repository *repo, const char *path,
1391 int *searching, int *search_next_done, regex_t *regex)
1393 const struct got_error *err = NULL;
1394 int nqueued = 0, have_match = 0;
1397 * We keep all commits open throughout the lifetime of the log
1398 * view in order to avoid having to re-fetch commits from disk
1399 * while updating the display.
1401 while (nqueued < minqueue ||
1402 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1403 struct got_object_id *id;
1404 struct got_commit_object *commit;
1405 struct commit_queue_entry *entry;
1408 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1409 if (err || id == NULL)
1412 err = got_object_open_as_commit(&commit, repo, id);
1415 entry = alloc_commit_queue_entry(commit, id);
1416 if (entry == NULL) {
1417 err = got_error_from_errno("alloc_commit_queue_entry");
1421 errcode = pthread_mutex_lock(&tog_mutex);
1423 err = got_error_set_errno(errcode,
1424 "pthread_mutex_lock");
1428 entry->idx = commits->ncommits;
1429 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1431 commits->ncommits++;
1433 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1434 err = match_commit(&have_match, id, commit, regex);
1437 errcode = pthread_mutex_unlock(&tog_mutex);
1438 if (errcode && err == NULL)
1439 err = got_error_set_errno(errcode,
1440 "pthread_mutex_unlock");
1442 if (err || have_match)
1449 static const struct got_error *
1450 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1451 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1452 struct commit_queue *commits, int selected_idx, int limit,
1453 struct got_reflist_head *refs, const char *path, int commits_needed,
1454 struct tog_colors *colors)
1456 const struct got_error *err = NULL;
1457 struct tog_log_view_state *s = &view->state.log;
1458 struct commit_queue_entry *entry;
1460 int ncommits, author_cols = 4;
1461 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1462 char *refs_str = NULL;
1464 struct tog_color *tc;
1465 static const size_t date_display_cols = 12;
1470 if (ncommits == selected_idx) {
1474 entry = TAILQ_NEXT(entry, entry);
1478 if (*selected && !(view->searching && view->search_next_done == 0)) {
1479 err = got_object_id_str(&id_str, (*selected)->id);
1483 err = build_refs_str(&refs_str, refs, (*selected)->id,
1490 if (commits_needed == 0)
1491 halfdelay(10); /* disable fast refresh */
1493 if (asprintf(&ncommits_str, " [%d/%d] %s",
1494 entry ? entry->idx + 1 : 0, commits->ncommits,
1495 commits_needed > 0 ?
1496 (view->searching && view->search_next_done == 0
1497 ? "searching..." : "loading... ") :
1498 (refs_str ? refs_str : "")) == -1) {
1499 err = got_error_from_errno("asprintf");
1503 if (path && strcmp(path, "/") != 0) {
1504 if (asprintf(&header, "commit %s %s%s",
1505 id_str ? id_str : "........................................",
1506 path, ncommits_str) == -1) {
1507 err = got_error_from_errno("asprintf");
1511 } else if (asprintf(&header, "commit %s%s",
1512 id_str ? id_str : "........................................",
1513 ncommits_str) == -1) {
1514 err = got_error_from_errno("asprintf");
1518 err = format_line(&wline, &width, header, view->ncols, 0);
1522 werase(view->window);
1524 if (view_needs_focus_indication(view))
1525 wstandout(view->window);
1526 tc = get_color(colors, TOG_COLOR_COMMIT);
1528 wattr_on(view->window,
1529 COLOR_PAIR(tc->colorpair), NULL);
1530 waddwstr(view->window, wline);
1532 wattr_off(view->window,
1533 COLOR_PAIR(tc->colorpair), NULL);
1534 while (width < view->ncols) {
1535 waddch(view->window, ' ');
1538 if (view_needs_focus_indication(view))
1539 wstandend(view->window);
1544 /* Grow author column size if necessary. */
1551 if (ncommits >= limit - 1)
1553 author = strdup(got_object_commit_get_author(entry->commit));
1554 if (author == NULL) {
1555 err = got_error_from_errno("strdup");
1558 err = format_author(&wauthor, &width, author, COLS,
1560 if (author_cols < width)
1561 author_cols = width;
1565 entry = TAILQ_NEXT(entry, entry);
1572 if (ncommits >= limit - 1)
1574 if (ncommits == selected_idx)
1575 wstandout(view->window);
1576 err = draw_commit(view, entry->commit, entry->id, refs,
1577 date_display_cols, author_cols, colors);
1578 if (ncommits == selected_idx)
1579 wstandend(view->window);
1584 entry = TAILQ_NEXT(entry, entry);
1597 scroll_up(struct tog_view *view,
1598 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1599 struct commit_queue *commits)
1601 struct commit_queue_entry *entry;
1604 entry = TAILQ_FIRST(&commits->head);
1605 if (*first_displayed_entry == entry)
1608 entry = *first_displayed_entry;
1609 while (entry && nscrolled < maxscroll) {
1610 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1612 *first_displayed_entry = entry;
1618 static const struct got_error *
1619 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1620 pthread_cond_t *need_commits)
1625 halfdelay(1); /* fast refresh while loading commits */
1627 while (*commits_needed > 0) {
1631 /* Wake the log thread. */
1632 errcode = pthread_cond_signal(need_commits);
1634 return got_error_set_errno(errcode,
1635 "pthread_cond_signal");
1636 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1638 * Thread is not done yet; lose a key press
1639 * and let the user retry... this way the GUI
1640 * remains interactive while logging deep paths
1641 * with few commits in history.
1650 static const struct got_error *
1651 scroll_down(struct tog_view *view,
1652 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1653 struct commit_queue_entry **last_displayed_entry,
1654 struct commit_queue *commits, int *log_complete, int *commits_needed,
1655 pthread_cond_t *need_commits)
1657 const struct got_error *err = NULL;
1658 struct commit_queue_entry *pentry;
1661 if (*last_displayed_entry == NULL)
1664 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1665 if (pentry == NULL && !*log_complete) {
1667 * Ask the log thread for required amount of commits
1668 * plus some amount of pre-fetching.
1670 (*commits_needed) += maxscroll + 20;
1671 err = trigger_log_thread(0, commits_needed, log_complete,
1678 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1682 *last_displayed_entry = pentry;
1684 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1687 *first_displayed_entry = pentry;
1688 } while (++nscrolled < maxscroll);
1693 static const struct got_error *
1694 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1695 struct got_commit_object *commit, struct got_object_id *commit_id,
1696 struct tog_view *log_view, struct got_reflist_head *refs,
1697 struct got_repository *repo)
1699 const struct got_error *err;
1700 struct got_object_qid *parent_id;
1701 struct tog_view *diff_view;
1703 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1704 if (diff_view == NULL)
1705 return got_error_from_errno("view_open");
1707 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1708 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1709 commit_id, log_view, refs, repo);
1711 *new_view = diff_view;
1715 static const struct got_error *
1716 tree_view_visit_subtree(struct got_tree_object *subtree,
1717 struct tog_tree_view_state *s)
1719 struct tog_parent_tree *parent;
1721 parent = calloc(1, sizeof(*parent));
1723 return got_error_from_errno("calloc");
1725 parent->tree = s->tree;
1726 parent->first_displayed_entry = s->first_displayed_entry;
1727 parent->selected_entry = s->selected_entry;
1728 parent->selected = s->selected;
1729 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1732 s->first_displayed_entry = NULL;
1737 static const struct got_error *
1738 browse_commit_tree(struct tog_view **new_view, int begin_x,
1739 struct commit_queue_entry *entry, const char *path,
1740 struct got_reflist_head *refs, struct got_repository *repo)
1742 const struct got_error *err = NULL;
1743 struct got_tree_object *tree;
1744 struct tog_tree_view_state *s;
1745 struct tog_view *tree_view;
1746 char *slash, *subpath = NULL;
1749 err = got_object_open_as_tree(&tree, repo,
1750 got_object_commit_get_tree_id(entry->commit));
1754 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1755 if (tree_view == NULL)
1756 return got_error_from_errno("view_open");
1758 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1760 got_object_tree_close(tree);
1763 s = &tree_view->state.tree;
1765 *new_view = tree_view;
1767 if (got_path_is_root_dir(path))
1770 /* Walk the path and open corresponding tree objects. */
1773 struct got_tree_entry *te;
1774 struct got_object_id *tree_id;
1780 /* Ensure the correct subtree entry is selected. */
1781 slash = strchr(p, '/');
1783 te_name = strdup(p);
1785 te_name = strndup(p, slash - p);
1786 if (te_name == NULL) {
1787 err = got_error_from_errno("strndup");
1790 te = got_object_tree_find_entry(s->tree, te_name);
1792 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1797 s->selected_entry = te;
1798 s->selected = got_tree_entry_get_index(te);
1799 if (s->tree != s->root)
1800 s->selected++; /* skip '..' */
1802 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1803 /* Jump to this file's entry. */
1804 s->first_displayed_entry = s->selected_entry;
1809 slash = strchr(p, '/');
1811 subpath = strndup(path, slash - path);
1813 subpath = strdup(path);
1814 if (subpath == NULL) {
1815 err = got_error_from_errno("strdup");
1819 err = got_object_id_by_path(&tree_id, repo, entry->id,
1824 err = got_object_open_as_tree(&tree, repo, tree_id);
1829 err = tree_view_visit_subtree(tree, s);
1831 got_object_tree_close(tree);
1845 static const struct got_error *
1846 block_signals_used_by_main_thread(void)
1851 if (sigemptyset(&sigset) == -1)
1852 return got_error_from_errno("sigemptyset");
1854 /* tog handles SIGWINCH and SIGCONT */
1855 if (sigaddset(&sigset, SIGWINCH) == -1)
1856 return got_error_from_errno("sigaddset");
1857 if (sigaddset(&sigset, SIGCONT) == -1)
1858 return got_error_from_errno("sigaddset");
1860 /* ncurses handles SIGTSTP */
1861 if (sigaddset(&sigset, SIGTSTP) == -1)
1862 return got_error_from_errno("sigaddset");
1864 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1866 return got_error_set_errno(errcode, "pthread_sigmask");
1872 log_thread(void *arg)
1874 const struct got_error *err = NULL;
1876 struct tog_log_thread_args *a = arg;
1879 err = block_signals_used_by_main_thread();
1883 while (!done && !err && !tog_sigpipe_received) {
1884 err = queue_commits(a->graph, a->commits, 1, a->repo,
1885 a->in_repo_path, a->searching, a->search_next_done,
1888 if (err->code != GOT_ERR_ITER_COMPLETED)
1892 } else if (a->commits_needed > 0)
1893 a->commits_needed--;
1895 errcode = pthread_mutex_lock(&tog_mutex);
1897 err = got_error_set_errno(errcode,
1898 "pthread_mutex_lock");
1900 } else if (*a->quit)
1902 else if (*a->first_displayed_entry == NULL) {
1903 *a->first_displayed_entry =
1904 TAILQ_FIRST(&a->commits->head);
1905 *a->selected_entry = *a->first_displayed_entry;
1909 a->commits_needed = 0;
1910 else if (a->commits_needed == 0) {
1911 errcode = pthread_cond_wait(&a->need_commits,
1914 err = got_error_set_errno(errcode,
1915 "pthread_cond_wait");
1918 errcode = pthread_mutex_unlock(&tog_mutex);
1919 if (errcode && err == NULL)
1920 err = got_error_set_errno(errcode,
1921 "pthread_mutex_unlock");
1923 a->log_complete = 1;
1927 static const struct got_error *
1928 stop_log_thread(struct tog_log_view_state *s)
1930 const struct got_error *err = NULL;
1935 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1937 return got_error_set_errno(errcode,
1938 "pthread_cond_signal");
1939 errcode = pthread_mutex_unlock(&tog_mutex);
1941 return got_error_set_errno(errcode,
1942 "pthread_mutex_unlock");
1943 errcode = pthread_join(s->thread, (void **)&err);
1945 return got_error_set_errno(errcode, "pthread_join");
1946 errcode = pthread_mutex_lock(&tog_mutex);
1948 return got_error_set_errno(errcode,
1949 "pthread_mutex_lock");
1953 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1954 if (errcode && err == NULL)
1955 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1957 if (s->thread_args.repo) {
1958 got_repo_close(s->thread_args.repo);
1959 s->thread_args.repo = NULL;
1962 if (s->thread_args.graph) {
1963 got_commit_graph_close(s->thread_args.graph);
1964 s->thread_args.graph = NULL;
1970 static const struct got_error *
1971 close_log_view(struct tog_view *view)
1973 const struct got_error *err = NULL;
1974 struct tog_log_view_state *s = &view->state.log;
1976 err = stop_log_thread(s);
1977 free_commits(&s->commits);
1978 free(s->in_repo_path);
1979 s->in_repo_path = NULL;
1985 static const struct got_error *
1986 search_start_log_view(struct tog_view *view)
1988 struct tog_log_view_state *s = &view->state.log;
1990 s->matched_entry = NULL;
1991 s->search_entry = NULL;
1995 static const struct got_error *
1996 search_next_log_view(struct tog_view *view)
1998 const struct got_error *err = NULL;
1999 struct tog_log_view_state *s = &view->state.log;
2000 struct commit_queue_entry *entry;
2002 if (!view->searching) {
2003 view->search_next_done = 1;
2007 if (s->search_entry) {
2009 errcode = pthread_mutex_unlock(&tog_mutex);
2011 return got_error_set_errno(errcode,
2012 "pthread_mutex_unlock");
2013 ch = wgetch(view->window);
2014 errcode = pthread_mutex_lock(&tog_mutex);
2016 return got_error_set_errno(errcode,
2017 "pthread_mutex_lock");
2018 if (ch == KEY_BACKSPACE) {
2019 view->search_next_done = 1;
2022 if (view->searching == TOG_SEARCH_FORWARD)
2023 entry = TAILQ_NEXT(s->search_entry, entry);
2025 entry = TAILQ_PREV(s->search_entry,
2026 commit_queue_head, entry);
2027 } else if (s->matched_entry) {
2028 if (view->searching == TOG_SEARCH_FORWARD)
2029 entry = TAILQ_NEXT(s->selected_entry, entry);
2031 entry = TAILQ_PREV(s->selected_entry,
2032 commit_queue_head, entry);
2034 if (view->searching == TOG_SEARCH_FORWARD)
2035 entry = TAILQ_FIRST(&s->commits.head);
2037 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2043 if (entry == NULL) {
2044 if (s->thread_args.log_complete ||
2045 view->searching == TOG_SEARCH_BACKWARD) {
2046 view->search_next_done = 1;
2050 * Poke the log thread for more commits and return,
2051 * allowing the main loop to make progress. Search
2052 * will resume at s->search_entry once we come back.
2054 s->thread_args.commits_needed++;
2055 return trigger_log_thread(1,
2056 &s->thread_args.commits_needed,
2057 &s->thread_args.log_complete,
2058 &s->thread_args.need_commits);
2061 err = match_commit(&have_match, entry->id, entry->commit,
2066 view->search_next_done = 1;
2067 s->matched_entry = entry;
2071 s->search_entry = entry;
2072 if (view->searching == TOG_SEARCH_FORWARD)
2073 entry = TAILQ_NEXT(entry, entry);
2075 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2078 if (s->matched_entry) {
2079 int cur = s->selected_entry->idx;
2080 while (cur < s->matched_entry->idx) {
2081 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2086 while (cur > s->matched_entry->idx) {
2087 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2094 s->search_entry = NULL;
2099 static const struct got_error *
2100 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2101 struct got_reflist_head *refs, struct got_repository *repo,
2102 const char *head_ref_name, const char *path, int check_disk,
2105 const struct got_error *err = NULL;
2106 struct tog_log_view_state *s = &view->state.log;
2107 struct got_repository *thread_repo = NULL;
2108 struct got_commit_graph *thread_graph = NULL;
2111 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2115 /* The commit queue only contains commits being displayed. */
2116 TAILQ_INIT(&s->commits.head);
2117 s->commits.ncommits = 0;
2121 s->head_ref_name = head_ref_name;
2122 s->start_id = got_object_id_dup(start_id);
2123 if (s->start_id == NULL) {
2124 err = got_error_from_errno("got_object_id_dup");
2127 s->log_branches = log_branches;
2129 SIMPLEQ_INIT(&s->colors);
2130 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2131 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2132 get_color_value("TOG_COLOR_COMMIT"));
2135 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2136 get_color_value("TOG_COLOR_AUTHOR"));
2138 free_colors(&s->colors);
2141 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2142 get_color_value("TOG_COLOR_DATE"));
2144 free_colors(&s->colors);
2149 view->show = show_log_view;
2150 view->input = input_log_view;
2151 view->close = close_log_view;
2152 view->search_start = search_start_log_view;
2153 view->search_next = search_next_log_view;
2155 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2158 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2162 err = got_commit_graph_iter_start(thread_graph,
2163 s->start_id, s->repo, NULL, NULL);
2167 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2169 err = got_error_set_errno(errcode, "pthread_cond_init");
2173 s->thread_args.commits_needed = view->nlines;
2174 s->thread_args.graph = thread_graph;
2175 s->thread_args.commits = &s->commits;
2176 s->thread_args.in_repo_path = s->in_repo_path;
2177 s->thread_args.start_id = s->start_id;
2178 s->thread_args.repo = thread_repo;
2179 s->thread_args.log_complete = 0;
2180 s->thread_args.quit = &s->quit;
2181 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2182 s->thread_args.selected_entry = &s->selected_entry;
2183 s->thread_args.searching = &view->searching;
2184 s->thread_args.search_next_done = &view->search_next_done;
2185 s->thread_args.regex = &view->regex;
2188 close_log_view(view);
2192 static const struct got_error *
2193 show_log_view(struct tog_view *view)
2195 struct tog_log_view_state *s = &view->state.log;
2197 if (s->thread == NULL) {
2198 int errcode = pthread_create(&s->thread, NULL, log_thread,
2201 return got_error_set_errno(errcode, "pthread_create");
2204 return draw_commits(view, &s->last_displayed_entry,
2205 &s->selected_entry, s->first_displayed_entry,
2206 &s->commits, s->selected, view->nlines, s->refs,
2207 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2210 static const struct got_error *
2211 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2212 struct tog_view **focus_view, struct tog_view *view, int ch)
2214 const struct got_error *err = NULL;
2215 struct tog_log_view_state *s = &view->state.log;
2216 char *parent_path, *in_repo_path = NULL;
2217 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2219 struct got_object_id *start_id;
2229 if (s->first_displayed_entry == NULL)
2231 if (s->selected > 0)
2234 scroll_up(view, &s->first_displayed_entry, 1,
2239 if (s->first_displayed_entry == NULL)
2241 if (TAILQ_FIRST(&s->commits.head) ==
2242 s->first_displayed_entry) {
2246 scroll_up(view, &s->first_displayed_entry,
2247 view->nlines, &s->commits);
2253 if (s->first_displayed_entry == NULL)
2255 if (s->selected < MIN(view->nlines - 2,
2256 s->commits.ncommits - 1)) {
2260 err = scroll_down(view, &s->first_displayed_entry, 1,
2261 &s->last_displayed_entry, &s->commits,
2262 &s->thread_args.log_complete,
2263 &s->thread_args.commits_needed,
2264 &s->thread_args.need_commits);
2268 struct commit_queue_entry *first;
2269 first = s->first_displayed_entry;
2272 err = scroll_down(view, &s->first_displayed_entry,
2273 view->nlines, &s->last_displayed_entry,
2274 &s->commits, &s->thread_args.log_complete,
2275 &s->thread_args.commits_needed,
2276 &s->thread_args.need_commits);
2279 if (first == s->first_displayed_entry &&
2280 s->selected < MIN(view->nlines - 2,
2281 s->commits.ncommits - 1)) {
2282 /* can't scroll further down */
2283 s->selected = MIN(view->nlines - 2,
2284 s->commits.ncommits - 1);
2290 if (s->selected > view->nlines - 2)
2291 s->selected = view->nlines - 2;
2292 if (s->selected > s->commits.ncommits - 1)
2293 s->selected = s->commits.ncommits - 1;
2298 if (s->selected_entry == NULL)
2300 if (view_is_parent_view(view))
2301 begin_x = view_split_begin_x(view->begin_x);
2302 err = open_diff_view_for_commit(&diff_view, begin_x,
2303 s->selected_entry->commit, s->selected_entry->id,
2304 view, s->refs, s->repo);
2307 if (view_is_parent_view(view)) {
2308 err = view_close_child(view);
2311 err = view_set_child(view, diff_view);
2313 view_close(diff_view);
2316 *focus_view = diff_view;
2317 view->child_focussed = 1;
2319 *new_view = diff_view;
2322 if (s->selected_entry == NULL)
2324 if (view_is_parent_view(view))
2325 begin_x = view_split_begin_x(view->begin_x);
2326 err = browse_commit_tree(&tree_view, begin_x,
2327 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2330 if (view_is_parent_view(view)) {
2331 err = view_close_child(view);
2334 err = view_set_child(view, tree_view);
2336 view_close(tree_view);
2339 *focus_view = tree_view;
2340 view->child_focussed = 1;
2342 *new_view = tree_view;
2345 if (strcmp(s->in_repo_path, "/") == 0)
2347 parent_path = dirname(s->in_repo_path);
2348 if (parent_path && strcmp(parent_path, ".") != 0) {
2349 err = stop_log_thread(s);
2352 lv = view_open(view->nlines, view->ncols,
2353 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2355 return got_error_from_errno(
2357 err = open_log_view(lv, s->start_id, s->refs,
2358 s->repo, s->head_ref_name, parent_path, 0,
2362 if (view_is_parent_view(view))
2365 view_set_child(view->parent, lv);
2372 err = stop_log_thread(s);
2375 lv = view_open(view->nlines, view->ncols,
2376 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2378 return got_error_from_errno("view_open");
2379 err = got_repo_match_object_id(&start_id, NULL,
2380 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2381 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2386 in_repo_path = strdup(s->in_repo_path);
2387 if (in_repo_path == NULL) {
2390 return got_error_from_errno("strdup");
2392 got_ref_list_free(s->refs);
2393 err = got_ref_list(s->refs, s->repo, NULL,
2394 got_ref_cmp_by_name, NULL);
2400 err = open_log_view(lv, start_id, s->refs, s->repo,
2401 s->head_ref_name, in_repo_path, 0, s->log_branches);
2411 s->log_branches = !s->log_branches;
2412 err = stop_log_thread(s);
2415 lv = view_open(view->nlines, view->ncols,
2416 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2418 return got_error_from_errno("view_open");
2419 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2420 s->head_ref_name, s->in_repo_path, 0, s->log_branches);
2435 static const struct got_error *
2436 apply_unveil(const char *repo_path, const char *worktree_path)
2438 const struct got_error *error;
2441 if (unveil("gmon.out", "rwc") != 0)
2442 return got_error_from_errno2("unveil", "gmon.out");
2444 if (repo_path && unveil(repo_path, "r") != 0)
2445 return got_error_from_errno2("unveil", repo_path);
2447 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2448 return got_error_from_errno2("unveil", worktree_path);
2450 if (unveil("/tmp", "rwc") != 0)
2451 return got_error_from_errno2("unveil", "/tmp");
2453 error = got_privsep_unveil_exec_helpers();
2457 if (unveil(NULL, NULL) != 0)
2458 return got_error_from_errno("unveil");
2468 halfdelay(1); /* Do fast refresh while initial view is loading. */
2471 intrflush(stdscr, FALSE);
2472 keypad(stdscr, TRUE);
2474 if (getenv("TOG_COLORS") != NULL) {
2476 use_default_colors();
2478 signal(SIGWINCH, tog_sigwinch);
2479 signal(SIGPIPE, tog_sigpipe);
2480 signal(SIGCONT, tog_sigcont);
2483 static const struct got_error *
2484 cmd_log(int argc, char *argv[])
2486 const struct got_error *error;
2487 struct got_repository *repo = NULL;
2488 struct got_worktree *worktree = NULL;
2489 struct got_reflist_head refs;
2490 struct got_object_id *start_id = NULL;
2491 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2492 char *start_commit = NULL, *head_ref_name = NULL;
2493 int ch, log_branches = 0, check_disk = 1;
2494 struct tog_view *view;
2496 SIMPLEQ_INIT(&refs);
2499 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2504 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2510 start_commit = optarg;
2513 repo_path = realpath(optarg, NULL);
2514 if (repo_path == NULL)
2515 return got_error_from_errno2("realpath",
2527 cwd = getcwd(NULL, 0);
2529 error = got_error_from_errno("getcwd");
2532 error = got_worktree_open(&worktree, cwd);
2533 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2540 error = got_error_from_errno("strdup");
2543 } else if (argc == 1) {
2545 error = got_worktree_resolve_path(&path, worktree,
2550 path = strdup(argv[0]);
2552 error = got_error_from_errno("strdup");
2559 if (repo_path == NULL) {
2562 got_worktree_get_repo_path(worktree));
2564 repo_path = strdup(cwd);
2566 if (repo_path == NULL) {
2567 error = got_error_from_errno("strdup");
2573 error = got_repo_open(&repo, repo_path, NULL);
2577 error = apply_unveil(got_repo_get_path(repo),
2578 worktree ? got_worktree_get_root_path(worktree) : NULL);
2582 if (start_commit == NULL)
2583 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2584 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2585 GOT_OBJ_TYPE_COMMIT, 1, repo);
2587 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2588 GOT_OBJ_TYPE_COMMIT, 1, repo);
2592 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2596 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2598 error = got_error_from_errno("view_open");
2602 const char *prefix = got_worktree_get_path_prefix(worktree);
2604 if (asprintf(&p, "%s%s%s", prefix,
2605 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2606 error = got_error_from_errno("asprintf");
2613 head_ref_name = strdup(
2614 got_worktree_get_head_ref_name(worktree));
2615 if (head_ref_name == NULL) {
2616 error = got_error_from_errno("strdup");
2620 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2621 path, check_disk, log_branches);
2625 /* Release work tree lock. */
2626 got_worktree_close(worktree);
2629 error = view_loop(view);
2635 free(head_ref_name);
2637 got_repo_close(repo);
2639 got_worktree_close(worktree);
2640 got_ref_list_free(&refs);
2648 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2654 parse_next_line(FILE *f, size_t *len)
2659 const char delim[3] = { '\0', '\0', '\0'};
2661 line = fparseln(f, &linelen, &lineno, delim, 0);
2668 match_line(const char *line, regex_t *regex)
2670 regmatch_t regmatch;
2672 return regexec(regex, line, 1, ®match, 0) == 0;
2676 match_color(struct tog_colors *colors, const char *line)
2678 struct tog_color *tc = NULL;
2680 SIMPLEQ_FOREACH(tc, colors, entry) {
2681 if (match_line(line, &tc->regex))
2688 static const struct got_error *
2689 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line, int nlines,
2690 int selected_line, int max_lines, int *last_displayed_line, int *eof,
2691 char *header, struct tog_colors *colors)
2693 const struct got_error *err;
2694 int lineno = 0, nprinted = 0;
2696 struct tog_color *tc;
2702 werase(view->window);
2705 err = format_line(&wline, &width, header, view->ncols, 0);
2710 if (view_needs_focus_indication(view))
2711 wstandout(view->window);
2712 waddwstr(view->window, wline);
2713 if (view_needs_focus_indication(view))
2714 wstandend(view->window);
2715 if (width <= view->ncols - 1)
2716 waddch(view->window, '\n');
2724 while (nprinted < max_lines) {
2725 line = parse_next_line(f, &len);
2730 if (++lineno < *first_displayed_line) {
2735 err = format_line(&wline, &width, line, view->ncols, 0);
2741 tc = match_color(colors, line);
2743 wattr_on(view->window,
2744 COLOR_PAIR(tc->colorpair), NULL);
2745 waddwstr(view->window, wline);
2747 wattr_off(view->window,
2748 COLOR_PAIR(tc->colorpair), NULL);
2749 if (width <= view->ncols - 1)
2750 waddch(view->window, '\n');
2751 if (++nprinted == 1)
2752 *first_displayed_line = lineno;
2757 *last_displayed_line = lineno;
2762 while (nprinted < view->nlines) {
2763 waddch(view->window, '\n');
2767 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2772 wstandout(view->window);
2773 waddwstr(view->window, wline);
2774 wstandend(view->window);
2781 get_datestr(time_t *time, char *datebuf)
2783 struct tm mytm, *tm;
2786 tm = gmtime_r(time, &mytm);
2789 s = asctime_r(tm, datebuf);
2792 p = strchr(s, '\n');
2798 static const struct got_error *
2799 write_commit_info(struct got_object_id *commit_id,
2800 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2802 const struct got_error *err = NULL;
2803 char datebuf[26], *datestr;
2804 struct got_commit_object *commit;
2805 char *id_str = NULL, *logmsg = NULL;
2806 time_t committer_time;
2807 const char *author, *committer;
2808 char *refs_str = NULL;
2811 err = build_refs_str(&refs_str, refs, commit_id, repo);
2816 err = got_object_open_as_commit(&commit, repo, commit_id);
2820 err = got_object_id_str(&id_str, commit_id);
2822 err = got_error_from_errno("got_object_id_str");
2826 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2827 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2828 err = got_error_from_errno("fprintf");
2831 if (fprintf(outfile, "from: %s\n",
2832 got_object_commit_get_author(commit)) < 0) {
2833 err = got_error_from_errno("fprintf");
2836 committer_time = got_object_commit_get_committer_time(commit);
2837 datestr = get_datestr(&committer_time, datebuf);
2838 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2839 err = got_error_from_errno("fprintf");
2842 author = got_object_commit_get_author(commit);
2843 committer = got_object_commit_get_committer(commit);
2844 if (strcmp(author, committer) != 0 &&
2845 fprintf(outfile, "via: %s\n", committer) < 0) {
2846 err = got_error_from_errno("fprintf");
2849 err = got_object_commit_get_logmsg(&logmsg, commit);
2852 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2853 err = got_error_from_errno("fprintf");
2860 got_object_commit_close(commit);
2864 const struct got_error *
2865 get_filestream_info(size_t *filesize, int *nlines, off_t **line_offsets,
2871 size_t noffsets = 0;
2875 *line_offsets = NULL;
2881 if (fseek(infile, 0, SEEK_END) == -1)
2882 return got_error_from_errno("fseek");
2883 len = ftell(infile) + 1;
2885 return got_error_from_errno("ftell");
2886 if (fseek(infile, 0, SEEK_SET) == -1)
2887 return got_error_from_errno("fseek");
2891 if ((buf = calloc(len, sizeof(char *))) == NULL)
2892 return got_error_from_errno("calloc");
2894 fread(buf, 1, len, infile);
2896 return got_error_from_errno("fread");
2899 if (line_offsets && nlines) {
2900 if (*line_offsets == NULL) {
2901 /* Have some data but perhaps no '\n'. */
2904 *line_offsets = calloc(1, sizeof(**line_offsets));
2905 if (*line_offsets == NULL)
2906 return got_error_from_errno("calloc");
2907 /* Skip forward over end of first line. */
2914 /* Scan '\n' offsets in remaining chunk of data. */
2916 if (buf[i] != '\n') {
2921 if (noffsets < *nlines) {
2922 off_t *o = recallocarray(*line_offsets,
2924 sizeof(**line_offsets));
2926 free(*line_offsets);
2927 *line_offsets = NULL;
2928 return got_error_from_errno(
2935 (*line_offsets)[*nlines - 1] = off;
2940 if (fflush(infile) != 0)
2941 return got_error_from_errno("fflush");
2950 static const struct got_error *
2951 create_diff(struct tog_diff_view_state *s)
2953 const struct got_error *err = NULL;
2959 err = got_error_from_errno("got_opentemp");
2962 if (s->f && fclose(s->f) != 0) {
2963 err = got_error_from_errno("fclose");
2969 err = got_object_get_type(&obj_type, s->repo, s->id1);
2971 err = got_object_get_type(&obj_type, s->repo, s->id2);
2976 case GOT_OBJ_TYPE_BLOB:
2977 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2978 s->diff_context, 0, s->repo, s->f);
2980 case GOT_OBJ_TYPE_TREE:
2981 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2982 s->diff_context, 0, s->repo, s->f);
2984 case GOT_OBJ_TYPE_COMMIT: {
2985 const struct got_object_id_queue *parent_ids;
2986 struct got_object_qid *pid;
2987 struct got_commit_object *commit2;
2989 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2992 /* Show commit info if we're diffing to a parent/root commit. */
2993 if (s->id1 == NULL) {
2994 err =write_commit_info(s->id2, s->refs, s->repo, s->f);
2998 parent_ids = got_object_commit_get_parent_ids(commit2);
2999 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3000 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3001 err = write_commit_info(s->id2, s->refs,
3009 got_object_commit_close(commit2);
3011 err = got_diff_objects_as_commits(s->id1, s->id2,
3012 s->diff_context, 0, s->repo, s->f);
3016 err = got_error(GOT_ERR_OBJ_TYPE);
3021 err = get_filestream_info(&s->filesize, &s->nlines, &s->line_offsets,
3024 if (s->f && fflush(s->f) != 0 && err == NULL)
3025 err = got_error_from_errno("fflush");
3030 diff_view_indicate_progress(struct tog_view *view)
3032 mvwaddstr(view->window, 0, 0, "diffing...");
3037 static const struct got_error *
3038 search_start_diff_view(struct tog_view *view)
3040 struct tog_diff_view_state *s = &view->state.diff;
3042 s->matched_line = 0;
3046 static const struct got_error *
3047 search_next_diff_view(struct tog_view *view)
3049 struct tog_diff_view_state *s = &view->state.diff;
3052 if (!view->searching) {
3053 view->search_next_done = 1;
3057 if (s->matched_line) {
3058 if (view->searching == TOG_SEARCH_FORWARD)
3059 lineno = s->matched_line + 1;
3061 lineno = s->matched_line - 1;
3063 if (view->searching == TOG_SEARCH_FORWARD)
3074 if (lineno <= 0 || lineno > s->nlines) {
3075 if (s->matched_line == 0) {
3076 view->search_next_done = 1;
3081 if (view->searching == TOG_SEARCH_FORWARD)
3087 offset = s->line_offsets[lineno - 1];
3088 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3090 return got_error_from_errno("fseeko");
3093 line = parse_next_line(s->f, &len);
3094 if (line && match_line(line, &view->regex)) {
3095 view->search_next_done = 1;
3096 s->matched_line = lineno;
3101 if (view->searching == TOG_SEARCH_FORWARD)
3107 if (s->matched_line) {
3108 s->first_displayed_line = s->matched_line;
3109 s->selected_line = 1;
3115 static const struct got_error *
3116 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3117 struct got_object_id *id2, struct tog_view *log_view,
3118 struct got_reflist_head *refs, struct got_repository *repo)
3120 const struct got_error *err;
3121 struct tog_diff_view_state *s = &view->state.diff;
3123 if (id1 != NULL && id2 != NULL) {
3125 err = got_object_get_type(&type1, repo, id1);
3128 err = got_object_get_type(&type2, repo, id2);
3133 return got_error(GOT_ERR_OBJ_TYPE);
3135 s->first_displayed_line = 1;
3136 s->last_displayed_line = view->nlines;
3137 s->selected_line = 1;
3144 s->id1 = got_object_id_dup(id1);
3146 return got_error_from_errno("got_object_id_dup");
3150 s->id2 = got_object_id_dup(id2);
3151 if (s->id2 == NULL) {
3154 return got_error_from_errno("got_object_id_dup");
3157 s->first_displayed_line = 1;
3158 s->last_displayed_line = view->nlines;
3159 s->diff_context = 3;
3160 s->log_view = log_view;
3164 SIMPLEQ_INIT(&s->colors);
3165 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3166 err = add_color(&s->colors,
3167 "^-", TOG_COLOR_DIFF_MINUS,
3168 get_color_value("TOG_COLOR_DIFF_MINUS"));
3171 err = add_color(&s->colors, "^\\+",
3172 TOG_COLOR_DIFF_PLUS,
3173 get_color_value("TOG_COLOR_DIFF_PLUS"));
3175 free_colors(&s->colors);
3178 err = add_color(&s->colors,
3179 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3180 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3182 free_colors(&s->colors);
3186 err = add_color(&s->colors,
3187 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
3188 get_color_value("TOG_COLOR_DIFF_META"));
3190 free_colors(&s->colors);
3194 err = add_color(&s->colors,
3195 "^(from|via): ", TOG_COLOR_AUTHOR,
3196 get_color_value("TOG_COLOR_AUTHOR"));
3198 free_colors(&s->colors);
3202 err = add_color(&s->colors,
3203 "^date: ", TOG_COLOR_DATE,
3204 get_color_value("TOG_COLOR_DATE"));
3206 free_colors(&s->colors);
3211 if (log_view && view_is_splitscreen(view))
3212 show_log_view(log_view); /* draw vborder */
3213 diff_view_indicate_progress(view);
3215 err = create_diff(s);
3224 view->show = show_diff_view;
3225 view->input = input_diff_view;
3226 view->close = close_diff_view;
3227 view->search_start = search_start_diff_view;
3228 view->search_next = search_next_diff_view;
3233 static const struct got_error *
3234 close_diff_view(struct tog_view *view)
3236 const struct got_error *err = NULL;
3237 struct tog_diff_view_state *s = &view->state.diff;
3243 if (s->f && fclose(s->f) == EOF)
3244 err = got_error_from_errno("fclose");
3245 free_colors(&s->colors);
3246 free(s->line_offsets);
3250 static const struct got_error *
3251 show_diff_view(struct tog_view *view)
3253 const struct got_error *err;
3254 struct tog_diff_view_state *s = &view->state.diff;
3255 char *id_str1 = NULL, *id_str2, *header;
3258 err = got_object_id_str(&id_str1, s->id1);
3262 err = got_object_id_str(&id_str2, s->id2);
3266 if (asprintf(&header, "diff %s %s",
3267 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3268 err = got_error_from_errno("asprintf");
3276 return draw_file(view, s->f, &s->first_displayed_line, s->nlines,
3277 s->selected_line, view->nlines, &s->last_displayed_line, &s->eof,
3278 header, &s->colors);
3281 static const struct got_error *
3282 set_selected_commit(struct tog_diff_view_state *s,
3283 struct commit_queue_entry *entry)
3285 const struct got_error *err;
3286 const struct got_object_id_queue *parent_ids;
3287 struct got_commit_object *selected_commit;
3288 struct got_object_qid *pid;
3291 s->id2 = got_object_id_dup(entry->id);
3293 return got_error_from_errno("got_object_id_dup");
3295 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3298 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3300 pid = SIMPLEQ_FIRST(parent_ids);
3301 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3302 got_object_commit_close(selected_commit);
3306 static const struct got_error *
3307 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3308 struct tog_view **focus_view, struct tog_view *view, int ch)
3310 const struct got_error *err = NULL;
3311 struct tog_diff_view_state *s = &view->state.diff;
3312 struct tog_log_view_state *ls;
3313 struct commit_queue_entry *entry;
3319 if (s->first_displayed_line > 1)
3320 s->first_displayed_line--;
3324 if (s->first_displayed_line == 1)
3327 while (i++ < view->nlines - 1 &&
3328 s->first_displayed_line > 1)
3329 s->first_displayed_line--;
3334 s->first_displayed_line++;
3342 while (!s->eof && i++ < view->nlines - 1) {
3344 line = parse_next_line(s->f, NULL);
3345 s->first_displayed_line++;
3351 if (s->diff_context > 0) {
3353 diff_view_indicate_progress(view);
3354 err = create_diff(s);
3358 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3360 diff_view_indicate_progress(view);
3361 err = create_diff(s);
3366 if (s->log_view == NULL)
3368 ls = &s->log_view->state.log;
3369 entry = TAILQ_PREV(ls->selected_entry,
3370 commit_queue_head, entry);
3374 err = input_log_view(NULL, NULL, NULL, s->log_view,
3379 err = set_selected_commit(s, entry);
3383 s->first_displayed_line = 1;
3384 s->last_displayed_line = view->nlines;
3386 diff_view_indicate_progress(view);
3387 err = create_diff(s);
3391 if (s->log_view == NULL)
3393 ls = &s->log_view->state.log;
3395 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3396 ls->thread_args.commits_needed++;
3398 /* Display "loading..." in log view. */
3399 show_log_view(s->log_view);
3403 err = trigger_log_thread(1 /* load_all */,
3404 &ls->thread_args.commits_needed,
3405 &ls->thread_args.log_complete,
3406 &ls->thread_args.need_commits);
3410 err = input_log_view(NULL, NULL, NULL, s->log_view,
3415 entry = TAILQ_NEXT(ls->selected_entry, entry);
3419 err = set_selected_commit(s, entry);
3423 s->first_displayed_line = 1;
3424 s->last_displayed_line = view->nlines;
3426 diff_view_indicate_progress(view);
3427 err = create_diff(s);
3436 static const struct got_error *
3437 cmd_diff(int argc, char *argv[])
3439 const struct got_error *error = NULL;
3440 struct got_repository *repo = NULL;
3441 struct got_reflist_head refs;
3442 struct got_object_id *id1 = NULL, *id2 = NULL;
3443 char *repo_path = NULL;
3444 char *id_str1 = NULL, *id_str2 = NULL;
3446 struct tog_view *view;
3448 SIMPLEQ_INIT(&refs);
3451 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3456 while ((ch = getopt(argc, argv, "")) != -1) {
3468 usage_diff(); /* TODO show local worktree changes */
3469 } else if (argc == 2) {
3470 repo_path = getcwd(NULL, 0);
3471 if (repo_path == NULL)
3472 return got_error_from_errno("getcwd");
3475 } else if (argc == 3) {
3476 repo_path = realpath(argv[0], NULL);
3477 if (repo_path == NULL)
3478 return got_error_from_errno2("realpath", argv[0]);
3486 error = got_repo_open(&repo, repo_path, NULL);
3490 error = apply_unveil(got_repo_get_path(repo), NULL);
3494 error = got_repo_match_object_id_prefix(&id1, id_str1,
3495 GOT_OBJ_TYPE_ANY, repo);
3499 error = got_repo_match_object_id_prefix(&id2, id_str2,
3500 GOT_OBJ_TYPE_ANY, repo);
3504 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3508 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3510 error = got_error_from_errno("view_open");
3513 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3516 error = view_loop(view);
3520 got_repo_close(repo);
3521 got_ref_list_free(&refs);
3529 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3534 struct tog_blame_line {
3536 struct got_object_id *id;
3539 static const struct got_error *
3540 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3541 const char *path, struct tog_blame_line *lines, int nlines,
3542 int blame_complete, int selected_line, int *first_displayed_line,
3543 int *last_displayed_line, int *eof, int max_lines,
3544 struct tog_colors *colors)
3546 const struct got_error *err;
3547 int lineno = 0, nprinted = 0;
3552 struct tog_blame_line *blame_line;
3553 struct got_object_id *prev_id = NULL;
3555 struct tog_color *tc;
3557 err = got_object_id_str(&id_str, id);
3562 werase(view->window);
3564 if (asprintf(&line, "commit %s", id_str) == -1) {
3565 err = got_error_from_errno("asprintf");
3570 err = format_line(&wline, &width, line, view->ncols, 0);
3575 if (view_needs_focus_indication(view))
3576 wstandout(view->window);
3577 tc = get_color(colors, TOG_COLOR_COMMIT);
3579 wattr_on(view->window,
3580 COLOR_PAIR(tc->colorpair), NULL);
3581 waddwstr(view->window, wline);
3583 wattr_off(view->window,
3584 COLOR_PAIR(tc->colorpair), NULL);
3585 if (view_needs_focus_indication(view))
3586 wstandend(view->window);
3589 if (width < view->ncols - 1)
3590 waddch(view->window, '\n');
3592 if (asprintf(&line, "[%d/%d] %s%s",
3593 *first_displayed_line - 1 + selected_line, nlines,
3594 blame_complete ? "" : "annotating... ", path) == -1) {
3596 return got_error_from_errno("asprintf");
3599 err = format_line(&wline, &width, line, view->ncols, 0);
3604 waddwstr(view->window, wline);
3607 if (width < view->ncols - 1)
3608 waddch(view->window, '\n');
3611 while (nprinted < max_lines - 2) {
3612 line = parse_next_line(f, &len);
3617 if (++lineno < *first_displayed_line) {
3622 if (view->ncols <= 9) {
3624 wline = wcsdup(L"");
3626 err = got_error_from_errno("wcsdup");
3628 err = format_line(&wline, &width, line,
3629 view->ncols - 9, 9);
3637 if (view->focussed && nprinted == selected_line - 1)
3638 wstandout(view->window);
3641 blame_line = &lines[lineno - 1];
3642 if (blame_line->annotated && prev_id &&
3643 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3645 nprinted == selected_line - 1)) {
3646 waddstr(view->window, " ");
3647 } else if (blame_line->annotated) {
3649 err = got_object_id_str(&id_str, blame_line->id);
3655 tc = get_color(colors, TOG_COLOR_COMMIT);
3657 wattr_on(view->window,
3658 COLOR_PAIR(tc->colorpair), NULL);
3659 wprintw(view->window, "%.8s", id_str);
3661 wattr_off(view->window,
3662 COLOR_PAIR(tc->colorpair), NULL);
3664 prev_id = blame_line->id;
3666 waddstr(view->window, "........");
3670 waddstr(view->window, "........");
3674 if (view->focussed && nprinted == selected_line - 1)
3675 wstandend(view->window);
3676 waddstr(view->window, " ");
3678 waddwstr(view->window, wline);
3679 if (width <= view->ncols - 1)
3680 waddch(view->window, '\n');
3681 if (++nprinted == 1)
3682 *first_displayed_line = lineno;
3687 *last_displayed_line = lineno;
3694 static const struct got_error *
3695 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3697 const struct got_error *err = NULL;
3698 struct tog_blame_cb_args *a = arg;
3699 struct tog_blame_line *line;
3702 if (nlines != a->nlines ||
3703 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3704 return got_error(GOT_ERR_RANGE);
3706 errcode = pthread_mutex_lock(&tog_mutex);
3708 return got_error_set_errno(errcode, "pthread_mutex_lock");
3710 if (*a->quit) { /* user has quit the blame view */
3711 err = got_error(GOT_ERR_ITER_COMPLETED);
3716 goto done; /* no change in this commit */
3718 line = &a->lines[lineno - 1];
3719 if (line->annotated)
3722 line->id = got_object_id_dup(id);
3723 if (line->id == NULL) {
3724 err = got_error_from_errno("got_object_id_dup");
3727 line->annotated = 1;
3729 errcode = pthread_mutex_unlock(&tog_mutex);
3731 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3736 blame_thread(void *arg)
3738 const struct got_error *err;
3739 struct tog_blame_thread_args *ta = arg;
3740 struct tog_blame_cb_args *a = ta->cb_args;
3743 err = block_signals_used_by_main_thread();
3747 err = got_blame(ta->path, a->commit_id, ta->repo,
3748 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3749 if (err && err->code == GOT_ERR_CANCELLED)
3752 errcode = pthread_mutex_lock(&tog_mutex);
3754 return (void *)got_error_set_errno(errcode,
3755 "pthread_mutex_lock");
3757 got_repo_close(ta->repo);
3761 errcode = pthread_mutex_unlock(&tog_mutex);
3762 if (errcode && err == NULL)
3763 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3768 static struct got_object_id *
3769 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3770 int first_displayed_line, int selected_line)
3772 struct tog_blame_line *line;
3777 line = &lines[first_displayed_line - 1 + selected_line - 1];
3778 if (!line->annotated)
3784 static const struct got_error *
3785 stop_blame(struct tog_blame *blame)
3787 const struct got_error *err = NULL;
3790 if (blame->thread) {
3792 errcode = pthread_mutex_unlock(&tog_mutex);
3794 return got_error_set_errno(errcode,
3795 "pthread_mutex_unlock");
3796 errcode = pthread_join(blame->thread, (void **)&err);
3798 return got_error_set_errno(errcode, "pthread_join");
3799 errcode = pthread_mutex_lock(&tog_mutex);
3801 return got_error_set_errno(errcode,
3802 "pthread_mutex_lock");
3803 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3805 blame->thread = NULL;
3807 if (blame->thread_args.repo) {
3808 got_repo_close(blame->thread_args.repo);
3809 blame->thread_args.repo = NULL;
3812 if (fclose(blame->f) != 0 && err == NULL)
3813 err = got_error_from_errno("fclose");
3817 for (i = 0; i < blame->nlines; i++)
3818 free(blame->lines[i].id);
3820 blame->lines = NULL;
3822 free(blame->cb_args.commit_id);
3823 blame->cb_args.commit_id = NULL;
3828 static const struct got_error *
3829 cancel_blame_view(void *arg)
3831 const struct got_error *err = NULL;
3835 errcode = pthread_mutex_lock(&tog_mutex);
3837 return got_error_set_errno(errcode,
3838 "pthread_mutex_unlock");
3841 err = got_error(GOT_ERR_CANCELLED);
3843 errcode = pthread_mutex_unlock(&tog_mutex);
3845 return got_error_set_errno(errcode,
3846 "pthread_mutex_lock");
3851 static const struct got_error *
3852 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3853 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3854 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3855 struct got_repository *repo)
3857 const struct got_error *err = NULL;
3858 struct got_blob_object *blob = NULL;
3859 struct got_repository *thread_repo = NULL;
3860 struct got_object_id *obj_id = NULL;
3863 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3867 err = got_object_get_type(&obj_type, repo, obj_id);
3871 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3872 err = got_error(GOT_ERR_OBJ_TYPE);
3876 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3879 blame->f = got_opentemp();
3880 if (blame->f == NULL) {
3881 err = got_error_from_errno("got_opentemp");
3884 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3885 &blame->line_offsets, blame->f, blob);
3886 if (err || blame->nlines == 0)
3889 /* Don't include \n at EOF in the blame line count. */
3890 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3893 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3894 if (blame->lines == NULL) {
3895 err = got_error_from_errno("calloc");
3899 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3903 blame->cb_args.view = view;
3904 blame->cb_args.lines = blame->lines;
3905 blame->cb_args.nlines = blame->nlines;
3906 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3907 if (blame->cb_args.commit_id == NULL) {
3908 err = got_error_from_errno("got_object_id_dup");
3911 blame->cb_args.quit = done;
3913 blame->thread_args.path = path;
3914 blame->thread_args.repo = thread_repo;
3915 blame->thread_args.cb_args = &blame->cb_args;
3916 blame->thread_args.complete = blame_complete;
3917 blame->thread_args.cancel_cb = cancel_blame_view;
3918 blame->thread_args.cancel_arg = done;
3919 *blame_complete = 0;
3923 got_object_blob_close(blob);
3930 static const struct got_error *
3931 open_blame_view(struct tog_view *view, char *path,
3932 struct got_object_id *commit_id, struct got_reflist_head *refs,
3933 struct got_repository *repo)
3935 const struct got_error *err = NULL;
3936 struct tog_blame_view_state *s = &view->state.blame;
3938 SIMPLEQ_INIT(&s->blamed_commits);
3940 s->path = strdup(path);
3941 if (s->path == NULL)
3942 return got_error_from_errno("strdup");
3944 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3950 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3951 s->first_displayed_line = 1;
3952 s->last_displayed_line = view->nlines;
3953 s->selected_line = 1;
3954 s->blame_complete = 0;
3957 s->commit_id = commit_id;
3958 memset(&s->blame, 0, sizeof(s->blame));
3960 SIMPLEQ_INIT(&s->colors);
3961 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3962 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3963 get_color_value("TOG_COLOR_COMMIT"));
3968 view->show = show_blame_view;
3969 view->input = input_blame_view;
3970 view->close = close_blame_view;
3971 view->search_start = search_start_blame_view;
3972 view->search_next = search_next_blame_view;
3974 return run_blame(&s->blame, view, &s->blame_complete,
3975 &s->first_displayed_line, &s->last_displayed_line,
3976 &s->selected_line, &s->done, &s->eof, s->path,
3977 s->blamed_commit->id, s->repo);
3980 static const struct got_error *
3981 close_blame_view(struct tog_view *view)
3983 const struct got_error *err = NULL;
3984 struct tog_blame_view_state *s = &view->state.blame;
3986 if (s->blame.thread)
3987 err = stop_blame(&s->blame);
3989 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3990 struct got_object_qid *blamed_commit;
3991 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3992 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3993 got_object_qid_free(blamed_commit);
3997 free_colors(&s->colors);
4002 static const struct got_error *
4003 search_start_blame_view(struct tog_view *view)
4005 struct tog_blame_view_state *s = &view->state.blame;
4007 s->matched_line = 0;
4011 static const struct got_error *
4012 search_next_blame_view(struct tog_view *view)
4014 struct tog_blame_view_state *s = &view->state.blame;
4017 if (!view->searching) {
4018 view->search_next_done = 1;
4022 if (s->matched_line) {
4023 if (view->searching == TOG_SEARCH_FORWARD)
4024 lineno = s->matched_line + 1;
4026 lineno = s->matched_line - 1;
4028 if (view->searching == TOG_SEARCH_FORWARD)
4031 lineno = s->blame.nlines;
4039 if (lineno <= 0 || lineno > s->blame.nlines) {
4040 if (s->matched_line == 0) {
4041 view->search_next_done = 1;
4046 if (view->searching == TOG_SEARCH_FORWARD)
4049 lineno = s->blame.nlines;
4052 offset = s->blame.line_offsets[lineno - 1];
4053 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4055 return got_error_from_errno("fseeko");
4058 line = parse_next_line(s->blame.f, &len);
4059 if (line && match_line(line, &view->regex)) {
4060 view->search_next_done = 1;
4061 s->matched_line = lineno;
4066 if (view->searching == TOG_SEARCH_FORWARD)
4072 if (s->matched_line) {
4073 s->first_displayed_line = s->matched_line;
4074 s->selected_line = 1;
4080 static const struct got_error *
4081 show_blame_view(struct tog_view *view)
4083 const struct got_error *err = NULL;
4084 struct tog_blame_view_state *s = &view->state.blame;
4087 if (s->blame.thread == NULL) {
4088 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4089 &s->blame.thread_args);
4091 return got_error_set_errno(errcode, "pthread_create");
4093 halfdelay(1); /* fast refresh while annotating */
4096 if (s->blame_complete)
4097 halfdelay(10); /* disable fast refresh */
4099 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4100 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4101 s->selected_line, &s->first_displayed_line,
4102 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
4108 static const struct got_error *
4109 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4110 struct tog_view **focus_view, struct tog_view *view, int ch)
4112 const struct got_error *err = NULL, *thread_err = NULL;
4113 struct tog_view *diff_view;
4114 struct tog_blame_view_state *s = &view->state.blame;
4123 if (s->selected_line > 1)
4125 else if (s->selected_line == 1 &&
4126 s->first_displayed_line > 1)
4127 s->first_displayed_line--;
4130 if (s->first_displayed_line == 1) {
4131 s->selected_line = 1;
4134 if (s->first_displayed_line > view->nlines - 2)
4135 s->first_displayed_line -=
4138 s->first_displayed_line = 1;
4142 if (s->selected_line < view->nlines - 2 &&
4143 s->first_displayed_line +
4144 s->selected_line <= s->blame.nlines)
4146 else if (s->last_displayed_line <
4148 s->first_displayed_line++;
4152 struct got_object_id *id = NULL;
4153 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4154 s->first_displayed_line, s->selected_line);
4158 struct got_commit_object *commit;
4159 struct got_object_qid *pid;
4160 struct got_object_id *blob_id = NULL;
4162 err = got_object_open_as_commit(&commit,
4166 pid = SIMPLEQ_FIRST(
4167 got_object_commit_get_parent_ids(commit));
4169 got_object_commit_close(commit);
4172 /* Check if path history ends here. */
4173 err = got_object_id_by_path(&blob_id, s->repo,
4176 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4178 got_object_commit_close(commit);
4181 err = got_object_get_type(&obj_type, s->repo,
4184 /* Can't blame non-blob type objects. */
4185 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4186 got_object_commit_close(commit);
4189 err = got_object_qid_alloc(&s->blamed_commit,
4191 got_object_commit_close(commit);
4193 if (got_object_id_cmp(id,
4194 s->blamed_commit->id) == 0)
4196 err = got_object_qid_alloc(&s->blamed_commit,
4202 thread_err = stop_blame(&s->blame);
4206 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4207 s->blamed_commit, entry);
4208 err = run_blame(&s->blame, view, &s->blame_complete,
4209 &s->first_displayed_line, &s->last_displayed_line,
4210 &s->selected_line, &s->done, &s->eof,
4211 s->path, s->blamed_commit->id, s->repo);
4217 struct got_object_qid *first;
4218 first = SIMPLEQ_FIRST(&s->blamed_commits);
4219 if (!got_object_id_cmp(first->id, s->commit_id))
4222 thread_err = stop_blame(&s->blame);
4226 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4227 got_object_qid_free(s->blamed_commit);
4229 SIMPLEQ_FIRST(&s->blamed_commits);
4230 err = run_blame(&s->blame, view, &s->blame_complete,
4231 &s->first_displayed_line, &s->last_displayed_line,
4232 &s->selected_line, &s->done, &s->eof, s->path,
4233 s->blamed_commit->id, s->repo);
4240 struct got_object_id *id = NULL;
4241 struct got_object_qid *pid;
4242 struct got_commit_object *commit = NULL;
4243 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4244 s->first_displayed_line, s->selected_line);
4247 err = got_object_open_as_commit(&commit, s->repo, id);
4250 pid = SIMPLEQ_FIRST(
4251 got_object_commit_get_parent_ids(commit));
4252 if (view_is_parent_view(view))
4253 begin_x = view_split_begin_x(view->begin_x);
4254 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4255 if (diff_view == NULL) {
4256 got_object_commit_close(commit);
4257 err = got_error_from_errno("view_open");
4260 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4261 id, NULL, s->refs, s->repo);
4262 got_object_commit_close(commit);
4264 view_close(diff_view);
4267 if (view_is_parent_view(view)) {
4268 err = view_close_child(view);
4271 err = view_set_child(view, diff_view);
4273 view_close(diff_view);
4276 *focus_view = diff_view;
4277 view->child_focussed = 1;
4279 *new_view = diff_view;
4286 if (s->last_displayed_line >= s->blame.nlines &&
4287 s->selected_line >= MIN(s->blame.nlines,
4288 view->nlines - 2)) {
4291 if (s->last_displayed_line >= s->blame.nlines &&
4292 s->selected_line < view->nlines - 2) {
4293 s->selected_line = MIN(s->blame.nlines,
4297 if (s->last_displayed_line + view->nlines - 2
4299 s->first_displayed_line +=
4302 s->first_displayed_line =
4307 if (s->selected_line > view->nlines - 2) {
4308 s->selected_line = MIN(s->blame.nlines,
4315 return thread_err ? thread_err : err;
4318 static const struct got_error *
4319 cmd_blame(int argc, char *argv[])
4321 const struct got_error *error;
4322 struct got_repository *repo = NULL;
4323 struct got_reflist_head refs;
4324 struct got_worktree *worktree = NULL;
4325 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4326 struct got_object_id *commit_id = NULL;
4327 char *commit_id_str = NULL;
4329 struct tog_view *view;
4331 SIMPLEQ_INIT(&refs);
4334 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4339 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4342 commit_id_str = optarg;
4345 repo_path = realpath(optarg, NULL);
4346 if (repo_path == NULL)
4347 return got_error_from_errno2("realpath",
4364 cwd = getcwd(NULL, 0);
4366 error = got_error_from_errno("getcwd");
4369 if (repo_path == NULL) {
4370 error = got_worktree_open(&worktree, cwd);
4371 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4377 strdup(got_worktree_get_repo_path(worktree));
4378 if (repo_path == NULL)
4379 error = got_error_from_errno("strdup");
4383 repo_path = strdup(cwd);
4384 if (repo_path == NULL) {
4385 error = got_error_from_errno("strdup");
4393 error = got_repo_open(&repo, repo_path, NULL);
4397 error = apply_unveil(got_repo_get_path(repo), NULL);
4402 const char *prefix = got_worktree_get_path_prefix(worktree);
4403 char *p, *worktree_subdir = cwd +
4404 strlen(got_worktree_get_root_path(worktree));
4405 if (asprintf(&p, "%s%s%s%s%s",
4406 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4407 worktree_subdir, worktree_subdir[0] ? "/" : "",
4409 error = got_error_from_errno("asprintf");
4412 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4415 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4420 if (commit_id_str == NULL) {
4421 struct got_reference *head_ref;
4422 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4425 error = got_ref_resolve(&commit_id, repo, head_ref);
4426 got_ref_close(head_ref);
4428 error = got_repo_match_object_id(&commit_id, NULL,
4429 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4434 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4438 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4440 error = got_error_from_errno("view_open");
4443 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4447 /* Release work tree lock. */
4448 got_worktree_close(worktree);
4451 error = view_loop(view);
4457 got_worktree_close(worktree);
4459 got_repo_close(repo);
4460 got_ref_list_free(&refs);
4464 static const struct got_error *
4465 draw_tree_entries(struct tog_view *view,
4466 struct got_tree_entry **first_displayed_entry,
4467 struct got_tree_entry **last_displayed_entry,
4468 struct got_tree_entry **selected_entry, int *ndisplayed,
4469 const char *label, int show_ids, const char *parent_path,
4470 struct got_tree_object *tree, int selected, int limit,
4471 int isroot, struct tog_colors *colors)
4473 const struct got_error *err = NULL;
4474 struct got_tree_entry *te;
4476 struct tog_color *tc;
4477 int width, n, i, nentries;
4481 werase(view->window);
4486 err = format_line(&wline, &width, label, view->ncols, 0);
4489 if (view_needs_focus_indication(view))
4490 wstandout(view->window);
4491 tc = get_color(colors, TOG_COLOR_COMMIT);
4493 wattr_on(view->window,
4494 COLOR_PAIR(tc->colorpair), NULL);
4495 waddwstr(view->window, wline);
4497 wattr_off(view->window,
4498 COLOR_PAIR(tc->colorpair), NULL);
4499 if (view_needs_focus_indication(view))
4500 wstandend(view->window);
4503 if (width < view->ncols - 1)
4504 waddch(view->window, '\n');
4507 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4510 waddwstr(view->window, wline);
4513 if (width < view->ncols - 1)
4514 waddch(view->window, '\n');
4517 waddch(view->window, '\n');
4521 if (*first_displayed_entry == NULL) {
4522 te = got_object_tree_get_first_entry(tree);
4523 if (selected == 0) {
4525 wstandout(view->window);
4526 *selected_entry = NULL;
4528 waddstr(view->window, " ..\n"); /* parent directory */
4529 if (selected == 0 && view->focussed)
4530 wstandend(view->window);
4537 te = *first_displayed_entry;
4540 nentries = got_object_tree_get_nentries(tree);
4541 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4542 char *line = NULL, *id_str = NULL;
4543 const char *modestr = "";
4546 te = got_object_tree_get_entry(tree, i);
4547 mode = got_tree_entry_get_mode(te);
4550 err = got_object_id_str(&id_str,
4551 got_tree_entry_get_id(te));
4553 return got_error_from_errno(
4554 "got_object_id_str");
4556 if (got_object_tree_entry_is_submodule(te))
4558 else if (S_ISLNK(mode))
4560 else if (S_ISDIR(mode))
4562 else if (mode & S_IXUSR)
4564 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4565 got_tree_entry_get_name(te), modestr) == -1) {
4567 return got_error_from_errno("asprintf");
4570 err = format_line(&wline, &width, line, view->ncols, 0);
4575 if (n == selected) {
4577 wstandout(view->window);
4578 *selected_entry = te;
4580 tc = match_color(colors, line);
4582 wattr_on(view->window,
4583 COLOR_PAIR(tc->colorpair), NULL);
4584 waddwstr(view->window, wline);
4586 wattr_off(view->window,
4587 COLOR_PAIR(tc->colorpair), NULL);
4588 if (width < view->ncols - 1)
4589 waddch(view->window, '\n');
4590 if (n == selected && view->focussed)
4591 wstandend(view->window);
4597 *last_displayed_entry = te;
4606 tree_scroll_up(struct tog_view *view,
4607 struct got_tree_entry **first_displayed_entry, int maxscroll,
4608 struct got_tree_object *tree, int isroot)
4610 struct got_tree_entry *te;
4613 if (*first_displayed_entry == NULL)
4616 te = got_object_tree_get_entry(tree, 0);
4617 if (*first_displayed_entry == te) {
4619 *first_displayed_entry = NULL;
4624 while (*first_displayed_entry && i < maxscroll) {
4625 *first_displayed_entry = got_tree_entry_get_prev(tree,
4626 *first_displayed_entry);
4629 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4630 *first_displayed_entry = NULL;
4634 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4635 struct got_tree_entry *last_displayed_entry,
4636 struct got_tree_object *tree)
4638 struct got_tree_entry *next, *last;
4641 if (*first_displayed_entry)
4642 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4644 next = got_object_tree_get_first_entry(tree);
4646 last = last_displayed_entry;
4647 while (next && last && n++ < maxscroll) {
4648 last = got_tree_entry_get_next(tree, last);
4650 *first_displayed_entry = next;
4651 next = got_tree_entry_get_next(tree, next);
4657 static const struct got_error *
4658 tree_entry_path(char **path, struct tog_parent_trees *parents,
4659 struct got_tree_entry *te)
4661 const struct got_error *err = NULL;
4662 struct tog_parent_tree *pt;
4663 size_t len = 2; /* for leading slash and NUL */
4665 TAILQ_FOREACH(pt, parents, entry)
4666 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4669 len += strlen(got_tree_entry_get_name(te));
4671 *path = calloc(1, len);
4673 return got_error_from_errno("calloc");
4676 pt = TAILQ_LAST(parents, tog_parent_trees);
4678 const char *name = got_tree_entry_get_name(pt->selected_entry);
4679 if (strlcat(*path, name, len) >= len) {
4680 err = got_error(GOT_ERR_NO_SPACE);
4683 if (strlcat(*path, "/", len) >= len) {
4684 err = got_error(GOT_ERR_NO_SPACE);
4687 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4690 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4691 err = got_error(GOT_ERR_NO_SPACE);
4703 static const struct got_error *
4704 blame_tree_entry(struct tog_view **new_view, int begin_x,
4705 struct got_tree_entry *te, struct tog_parent_trees *parents,
4706 struct got_object_id *commit_id, struct got_reflist_head *refs,
4707 struct got_repository *repo)
4709 const struct got_error *err = NULL;
4711 struct tog_view *blame_view;
4715 err = tree_entry_path(&path, parents, te);
4719 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4720 if (blame_view == NULL) {
4721 err = got_error_from_errno("view_open");
4725 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4727 if (err->code == GOT_ERR_CANCELLED)
4729 view_close(blame_view);
4731 *new_view = blame_view;
4737 static const struct got_error *
4738 log_tree_entry(struct tog_view **new_view, int begin_x,
4739 struct got_tree_entry *te, struct tog_parent_trees *parents,
4740 struct got_object_id *commit_id, struct got_reflist_head *refs,
4741 struct got_repository *repo)
4743 struct tog_view *log_view;
4744 const struct got_error *err = NULL;
4749 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4750 if (log_view == NULL)
4751 return got_error_from_errno("view_open");
4753 err = tree_entry_path(&path, parents, te);
4757 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0, 0);
4759 view_close(log_view);
4761 *new_view = log_view;
4766 static const struct got_error *
4767 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4768 struct got_object_id *commit_id, struct got_reflist_head *refs,
4769 struct got_repository *repo)
4771 const struct got_error *err = NULL;
4772 char *commit_id_str = NULL;
4773 struct tog_tree_view_state *s = &view->state.tree;
4775 TAILQ_INIT(&s->parents);
4777 err = got_object_id_str(&commit_id_str, commit_id);
4781 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4782 err = got_error_from_errno("asprintf");
4786 s->root = s->tree = root;
4787 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4788 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4789 s->commit_id = got_object_id_dup(commit_id);
4790 if (s->commit_id == NULL) {
4791 err = got_error_from_errno("got_object_id_dup");
4797 SIMPLEQ_INIT(&s->colors);
4799 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4800 err = add_color(&s->colors, "\\$$",
4801 TOG_COLOR_TREE_SUBMODULE,
4802 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4805 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4806 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4808 free_colors(&s->colors);
4811 err = add_color(&s->colors, "/$",
4812 TOG_COLOR_TREE_DIRECTORY,
4813 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4815 free_colors(&s->colors);
4819 err = add_color(&s->colors, "\\*$",
4820 TOG_COLOR_TREE_EXECUTABLE,
4821 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4823 free_colors(&s->colors);
4827 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4828 get_color_value("TOG_COLOR_COMMIT"));
4830 free_colors(&s->colors);
4835 view->show = show_tree_view;
4836 view->input = input_tree_view;
4837 view->close = close_tree_view;
4838 view->search_start = search_start_tree_view;
4839 view->search_next = search_next_tree_view;
4841 free(commit_id_str);
4843 free(s->tree_label);
4844 s->tree_label = NULL;
4849 static const struct got_error *
4850 close_tree_view(struct tog_view *view)
4852 struct tog_tree_view_state *s = &view->state.tree;
4854 free_colors(&s->colors);
4855 free(s->tree_label);
4856 s->tree_label = NULL;
4858 s->commit_id = NULL;
4859 while (!TAILQ_EMPTY(&s->parents)) {
4860 struct tog_parent_tree *parent;
4861 parent = TAILQ_FIRST(&s->parents);
4862 TAILQ_REMOVE(&s->parents, parent, entry);
4866 if (s->tree != s->root)
4867 got_object_tree_close(s->tree);
4868 got_object_tree_close(s->root);
4873 static const struct got_error *
4874 search_start_tree_view(struct tog_view *view)
4876 struct tog_tree_view_state *s = &view->state.tree;
4878 s->matched_entry = NULL;
4883 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4885 regmatch_t regmatch;
4887 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
4891 static const struct got_error *
4892 search_next_tree_view(struct tog_view *view)
4894 struct tog_tree_view_state *s = &view->state.tree;
4895 struct got_tree_entry *te = NULL;
4897 if (!view->searching) {
4898 view->search_next_done = 1;
4902 if (s->matched_entry) {
4903 if (view->searching == TOG_SEARCH_FORWARD) {
4904 if (s->selected_entry)
4905 te = got_tree_entry_get_next(s->tree,
4908 te = got_object_tree_get_first_entry(s->tree);
4910 if (s->selected_entry == NULL)
4911 te = got_object_tree_get_last_entry(s->tree);
4913 te = got_tree_entry_get_prev(s->tree,
4917 if (view->searching == TOG_SEARCH_FORWARD)
4918 te = got_object_tree_get_first_entry(s->tree);
4920 te = got_object_tree_get_last_entry(s->tree);
4925 if (s->matched_entry == NULL) {
4926 view->search_next_done = 1;
4929 if (view->searching == TOG_SEARCH_FORWARD)
4930 te = got_object_tree_get_first_entry(s->tree);
4932 te = got_object_tree_get_last_entry(s->tree);
4935 if (match_tree_entry(te, &view->regex)) {
4936 view->search_next_done = 1;
4937 s->matched_entry = te;
4941 if (view->searching == TOG_SEARCH_FORWARD)
4942 te = got_tree_entry_get_next(s->tree, te);
4944 te = got_tree_entry_get_prev(s->tree, te);
4947 if (s->matched_entry) {
4948 s->first_displayed_entry = s->matched_entry;
4955 static const struct got_error *
4956 show_tree_view(struct tog_view *view)
4958 const struct got_error *err = NULL;
4959 struct tog_tree_view_state *s = &view->state.tree;
4962 err = tree_entry_path(&parent_path, &s->parents, NULL);
4966 err = draw_tree_entries(view, &s->first_displayed_entry,
4967 &s->last_displayed_entry, &s->selected_entry,
4968 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4969 s->tree, s->selected, view->nlines, s->tree == s->root,
4977 static const struct got_error *
4978 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4979 struct tog_view **focus_view, struct tog_view *view, int ch)
4981 const struct got_error *err = NULL;
4982 struct tog_tree_view_state *s = &view->state.tree;
4983 struct tog_view *log_view;
4984 int begin_x = 0, nscrolled;
4988 s->show_ids = !s->show_ids;
4991 if (!s->selected_entry)
4993 if (view_is_parent_view(view))
4994 begin_x = view_split_begin_x(view->begin_x);
4995 err = log_tree_entry(&log_view, begin_x,
4996 s->selected_entry, &s->parents,
4997 s->commit_id, s->refs, s->repo);
4998 if (view_is_parent_view(view)) {
4999 err = view_close_child(view);
5002 err = view_set_child(view, log_view);
5004 view_close(log_view);
5007 *focus_view = log_view;
5008 view->child_focussed = 1;
5010 *new_view = log_view;
5014 if (s->selected > 0) {
5016 if (s->selected == 0)
5019 if (s->selected > 0)
5021 tree_scroll_up(view, &s->first_displayed_entry, 1,
5022 s->tree, s->tree == s->root);
5025 tree_scroll_up(view, &s->first_displayed_entry,
5026 MAX(0, view->nlines - 4 - s->selected), s->tree,
5027 s->tree == s->root);
5029 if (got_object_tree_get_first_entry(s->tree) ==
5030 s->first_displayed_entry && s->tree != s->root)
5031 s->first_displayed_entry = NULL;
5035 if (s->selected < s->ndisplayed - 1) {
5039 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5041 /* can't scroll any further */
5043 tree_scroll_down(&s->first_displayed_entry, 1,
5044 s->last_displayed_entry, s->tree);
5047 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5049 /* can't scroll any further; move cursor down */
5050 if (s->selected < s->ndisplayed - 1)
5051 s->selected = s->ndisplayed - 1;
5054 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5055 view->nlines, s->last_displayed_entry, s->tree);
5056 if (nscrolled < view->nlines) {
5058 struct got_tree_entry *te;
5059 te = s->first_displayed_entry;
5062 te = got_tree_entry_get_next(s->tree, te);
5064 s->selected = ndisplayed - 1;
5070 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5071 struct tog_parent_tree *parent;
5072 /* user selected '..' */
5073 if (s->tree == s->root)
5075 parent = TAILQ_FIRST(&s->parents);
5076 TAILQ_REMOVE(&s->parents, parent,
5078 got_object_tree_close(s->tree);
5079 s->tree = parent->tree;
5080 s->first_displayed_entry =
5081 parent->first_displayed_entry;
5083 parent->selected_entry;
5084 s->selected = parent->selected;
5086 } else if (S_ISDIR(got_tree_entry_get_mode(
5087 s->selected_entry))) {
5088 struct got_tree_object *subtree;
5089 err = got_object_open_as_tree(&subtree, s->repo,
5090 got_tree_entry_get_id(s->selected_entry));
5093 err = tree_view_visit_subtree(subtree, s);
5095 got_object_tree_close(subtree);
5098 } else if (S_ISREG(got_tree_entry_get_mode(
5099 s->selected_entry))) {
5100 struct tog_view *blame_view;
5101 int begin_x = view_is_parent_view(view) ?
5102 view_split_begin_x(view->begin_x) : 0;
5104 err = blame_tree_entry(&blame_view, begin_x,
5105 s->selected_entry, &s->parents,
5106 s->commit_id, s->refs, s->repo);
5109 if (view_is_parent_view(view)) {
5110 err = view_close_child(view);
5113 err = view_set_child(view, blame_view);
5115 view_close(blame_view);
5118 *focus_view = blame_view;
5119 view->child_focussed = 1;
5121 *new_view = blame_view;
5125 if (s->selected > view->nlines)
5126 s->selected = s->ndisplayed - 1;
5139 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
5144 static const struct got_error *
5145 cmd_tree(int argc, char *argv[])
5147 const struct got_error *error;
5148 struct got_repository *repo = NULL;
5149 struct got_reflist_head refs;
5150 char *repo_path = NULL;
5151 struct got_object_id *commit_id = NULL;
5152 char *commit_id_arg = NULL;
5153 struct got_commit_object *commit = NULL;
5154 struct got_tree_object *tree = NULL;
5156 struct tog_view *view;
5158 SIMPLEQ_INIT(&refs);
5161 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5166 while ((ch = getopt(argc, argv, "c:")) != -1) {
5169 commit_id_arg = optarg;
5181 struct got_worktree *worktree;
5182 char *cwd = getcwd(NULL, 0);
5184 return got_error_from_errno("getcwd");
5185 error = got_worktree_open(&worktree, cwd);
5186 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5191 strdup(got_worktree_get_repo_path(worktree));
5192 got_worktree_close(worktree);
5195 if (repo_path == NULL) {
5196 error = got_error_from_errno("strdup");
5199 } else if (argc == 1) {
5200 repo_path = realpath(argv[0], NULL);
5201 if (repo_path == NULL)
5202 return got_error_from_errno2("realpath", argv[0]);
5208 error = got_repo_open(&repo, repo_path, NULL);
5212 error = apply_unveil(got_repo_get_path(repo), NULL);
5216 error = got_repo_match_object_id(&commit_id, NULL,
5217 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5218 GOT_OBJ_TYPE_COMMIT, 1, repo);
5222 error = got_object_open_as_commit(&commit, repo, commit_id);
5226 error = got_object_open_as_tree(&tree, repo,
5227 got_object_commit_get_tree_id(commit));
5231 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5235 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5237 error = got_error_from_errno("view_open");
5240 error = open_tree_view(view, tree, commit_id, &refs, repo);
5243 error = view_loop(view);
5248 got_object_commit_close(commit);
5250 got_object_tree_close(tree);
5252 got_repo_close(repo);
5253 got_ref_list_free(&refs);
5262 fprintf(stderr, "commands:");
5263 for (i = 0; i < nitems(tog_commands); i++) {
5264 struct tog_cmd *cmd = &tog_commands[i];
5265 fprintf(stderr, " %s", cmd->name);
5267 fputc('\n', stderr);
5273 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5281 make_argv(const char *arg0, const char *arg1)
5284 int argc = (arg1 == NULL ? 1 : 2);
5286 argv = calloc(argc, sizeof(char *));
5289 argv[0] = strdup(arg0);
5290 if (argv[0] == NULL)
5293 argv[1] = strdup(arg1);
5294 if (argv[1] == NULL)
5302 main(int argc, char *argv[])
5304 const struct got_error *error = NULL;
5305 struct tog_cmd *cmd = NULL;
5306 int ch, hflag = 0, Vflag = 0;
5307 char **cmd_argv = NULL;
5308 static struct option longopts[] = {
5309 { "version", no_argument, NULL, 'V' },
5313 setlocale(LC_CTYPE, "");
5315 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5335 got_version_print_str();
5342 /* Build an argument vector which runs a default command. */
5343 cmd = &tog_commands[0];
5344 cmd_argv = make_argv(cmd->name, NULL);
5349 /* Did the user specific a command? */
5350 for (i = 0; i < nitems(tog_commands); i++) {
5351 if (strncmp(tog_commands[i].name, argv[0],
5352 strlen(argv[0])) == 0) {
5353 cmd = &tog_commands[i];
5359 fprintf(stderr, "%s: unknown command '%s'\n",
5360 getprogname(), argv[0]);
5369 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5373 if (error && error->code != GOT_ERR_CANCELLED)
5374 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);