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
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
49 #include "got_opentemp.h"
51 #include "got_cancel.h"
52 #include "got_commit_graph.h"
53 #include "got_blame.h"
54 #include "got_privsep.h"
56 #include "got_worktree.h"
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
66 #define CTRL(x) ((x) & 0x1f)
69 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
78 __dead static void usage(int);
79 __dead static void usage_log(void);
80 __dead static void usage_diff(void);
81 __dead static void usage_blame(void);
82 __dead static void usage_tree(void);
84 static const struct got_error* cmd_log(int, char *[]);
85 static const struct got_error* cmd_diff(int, char *[]);
86 static const struct got_error* cmd_blame(int, char *[]);
87 static const struct got_error* cmd_tree(int, char *[]);
89 static struct tog_cmd tog_commands[] = {
90 { "log", cmd_log, usage_log },
91 { "diff", cmd_diff, usage_diff },
92 { "blame", cmd_blame, usage_blame },
93 { "tree", cmd_tree, usage_tree },
103 #define TOG_EOF_STRING "(END)"
105 struct commit_queue_entry {
106 TAILQ_ENTRY(commit_queue_entry) entry;
107 struct got_object_id *id;
108 struct got_commit_object *commit;
111 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
112 struct commit_queue {
114 struct commit_queue_head head;
118 SIMPLEQ_ENTRY(tog_color) entry;
122 SIMPLEQ_HEAD(tog_colors, tog_color);
124 static const struct got_error *
125 add_color(struct tog_colors *colors, const char *pattern,
126 int idx, short color)
128 const struct got_error *err = NULL;
129 struct tog_color *tc;
132 if (idx < 1 || idx > COLOR_PAIRS - 1)
135 init_pair(idx, color, -1);
137 tc = calloc(1, sizeof(*tc));
139 return got_error_from_errno("calloc");
140 regerr = regcomp(&tc->regex, pattern,
141 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
143 static char regerr_msg[512];
144 static char err_msg[512];
145 regerror(regerr, &tc->regex, regerr_msg,
147 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
149 err = got_error_msg(GOT_ERR_REGEX, err_msg);
154 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
159 free_colors(struct tog_colors *colors)
161 struct tog_color *tc;
163 while (!SIMPLEQ_EMPTY(colors)) {
164 tc = SIMPLEQ_FIRST(colors);
165 SIMPLEQ_REMOVE_HEAD(colors, entry);
172 get_color(struct tog_colors *colors, int colorpair)
174 struct tog_color *tc = NULL;
176 SIMPLEQ_FOREACH(tc, colors, entry) {
177 if (tc->colorpair == colorpair)
185 default_color_value(const char *envvar)
187 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
188 return COLOR_MAGENTA;
189 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
191 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
193 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
195 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
196 return COLOR_MAGENTA;
197 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
198 return COLOR_MAGENTA;
199 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
201 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
203 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
205 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
207 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
214 get_color_value(const char *envvar)
216 const char *val = getenv(envvar);
219 return default_color_value(envvar);
221 if (strcasecmp(val, "black") == 0)
223 if (strcasecmp(val, "red") == 0)
225 if (strcasecmp(val, "green") == 0)
227 if (strcasecmp(val, "yellow") == 0)
229 if (strcasecmp(val, "blue") == 0)
231 if (strcasecmp(val, "magenta") == 0)
232 return COLOR_MAGENTA;
233 if (strcasecmp(val, "cyan") == 0)
235 if (strcasecmp(val, "white") == 0)
237 if (strcasecmp(val, "default") == 0)
240 return default_color_value(envvar);
244 struct tog_diff_view_state {
245 struct got_object_id *id1, *id2;
247 int first_displayed_line;
248 int last_displayed_line;
251 struct got_repository *repo;
252 struct got_reflist_head *refs;
253 struct tog_colors colors;
260 /* passed from log view; may be NULL */
261 struct tog_view *log_view;
264 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
266 struct tog_log_thread_args {
267 pthread_cond_t need_commits;
268 pthread_cond_t commit_loaded;
270 struct got_commit_graph *graph;
271 struct commit_queue *commits;
272 const char *in_repo_path;
273 struct got_object_id *start_id;
274 struct got_repository *repo;
277 struct commit_queue_entry **first_displayed_entry;
278 struct commit_queue_entry **selected_entry;
280 int *search_next_done;
284 struct tog_log_view_state {
285 struct commit_queue commits;
286 struct commit_queue_entry *first_displayed_entry;
287 struct commit_queue_entry *last_displayed_entry;
288 struct commit_queue_entry *selected_entry;
291 const char *head_ref_name;
293 struct got_repository *repo;
294 struct got_reflist_head *refs;
295 struct got_object_id *start_id;
298 struct tog_log_thread_args thread_args;
299 struct commit_queue_entry *matched_entry;
300 struct commit_queue_entry *search_entry;
301 struct tog_colors colors;
304 #define TOG_COLOR_DIFF_MINUS 1
305 #define TOG_COLOR_DIFF_PLUS 2
306 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
307 #define TOG_COLOR_DIFF_META 4
308 #define TOG_COLOR_TREE_SUBMODULE 5
309 #define TOG_COLOR_TREE_SYMLINK 6
310 #define TOG_COLOR_TREE_DIRECTORY 7
311 #define TOG_COLOR_TREE_EXECUTABLE 8
312 #define TOG_COLOR_COMMIT 9
313 #define TOG_COLOR_AUTHOR 10
314 #define TOG_COLOR_DATE 11
316 struct tog_blame_cb_args {
317 struct tog_blame_line *lines; /* one per line */
320 struct tog_view *view;
321 struct got_object_id *commit_id;
325 struct tog_blame_thread_args {
327 struct got_repository *repo;
328 struct tog_blame_cb_args *cb_args;
330 got_cancel_cb cancel_cb;
337 struct tog_blame_line *lines;
341 struct tog_blame_thread_args thread_args;
342 struct tog_blame_cb_args cb_args;
346 struct tog_blame_view_state {
347 int first_displayed_line;
348 int last_displayed_line;
353 struct got_object_id_queue blamed_commits;
354 struct got_object_qid *blamed_commit;
356 struct got_repository *repo;
357 struct got_reflist_head *refs;
358 struct got_object_id *commit_id;
359 struct tog_blame blame;
361 struct tog_colors colors;
364 struct tog_parent_tree {
365 TAILQ_ENTRY(tog_parent_tree) entry;
366 struct got_tree_object *tree;
367 struct got_tree_entry *first_displayed_entry;
368 struct got_tree_entry *selected_entry;
372 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
374 struct tog_tree_view_state {
376 struct got_tree_object *root;
377 struct got_tree_object *tree;
378 struct got_tree_entry *first_displayed_entry;
379 struct got_tree_entry *last_displayed_entry;
380 struct got_tree_entry *selected_entry;
381 int ndisplayed, selected, show_ids;
382 struct tog_parent_trees parents;
383 struct got_object_id *commit_id;
384 struct got_repository *repo;
385 struct got_reflist_head *refs;
386 struct got_tree_entry *matched_entry;
387 struct tog_colors colors;
391 * We implement two types of views: parent views and child views.
393 * The 'Tab' key switches between a parent view and its child view.
394 * Child views are shown side-by-side to their parent view, provided
395 * there is enough screen estate.
397 * When a new view is opened from within a parent view, this new view
398 * becomes a child view of the parent view, replacing any existing child.
400 * When a new view is opened from within a child view, this new view
401 * becomes a parent view which will obscure the views below until the
402 * user quits the new parent view by typing 'q'.
404 * This list of views contains parent views only.
405 * Child views are only pointed to by their parent view.
407 TAILQ_HEAD(tog_view_list_head, tog_view);
410 TAILQ_ENTRY(tog_view) entry;
413 int nlines, ncols, begin_y, begin_x;
414 int lines, cols; /* copies of LINES and COLS */
416 struct tog_view *parent;
417 struct tog_view *child;
420 /* type-specific state */
421 enum tog_view_type type;
423 struct tog_diff_view_state diff;
424 struct tog_log_view_state log;
425 struct tog_blame_view_state blame;
426 struct tog_tree_view_state tree;
429 const struct got_error *(*show)(struct tog_view *);
430 const struct got_error *(*input)(struct tog_view **,
431 struct tog_view **, struct tog_view**, struct tog_view *, int);
432 const struct got_error *(*close)(struct tog_view *);
434 const struct got_error *(*search_start)(struct tog_view *);
435 const struct got_error *(*search_next)(struct tog_view *);
437 #define TOG_SEARCH_FORWARD 1
438 #define TOG_SEARCH_BACKWARD 2
439 int search_next_done;
440 #define TOG_SEARCH_HAVE_MORE 1
441 #define TOG_SEARCH_NO_MORE 2
445 static const struct got_error *open_diff_view(struct tog_view *,
446 struct got_object_id *, struct got_object_id *, struct tog_view *,
447 struct got_reflist_head *, struct got_repository *);
448 static const struct got_error *show_diff_view(struct tog_view *);
449 static const struct got_error *input_diff_view(struct tog_view **,
450 struct tog_view **, struct tog_view **, struct tog_view *, int);
451 static const struct got_error* close_diff_view(struct tog_view *);
452 static const struct got_error *search_start_diff_view(struct tog_view *);
453 static const struct got_error *search_next_diff_view(struct tog_view *);
455 static const struct got_error *open_log_view(struct tog_view *,
456 struct got_object_id *, struct got_reflist_head *,
457 struct got_repository *, const char *, const char *, int);
458 static const struct got_error * show_log_view(struct tog_view *);
459 static const struct got_error *input_log_view(struct tog_view **,
460 struct tog_view **, struct tog_view **, struct tog_view *, int);
461 static const struct got_error *close_log_view(struct tog_view *);
462 static const struct got_error *search_start_log_view(struct tog_view *);
463 static const struct got_error *search_next_log_view(struct tog_view *);
465 static const struct got_error *open_blame_view(struct tog_view *, char *,
466 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
467 static const struct got_error *show_blame_view(struct tog_view *);
468 static const struct got_error *input_blame_view(struct tog_view **,
469 struct tog_view **, struct tog_view **, struct tog_view *, int);
470 static const struct got_error *close_blame_view(struct tog_view *);
471 static const struct got_error *search_start_blame_view(struct tog_view *);
472 static const struct got_error *search_next_blame_view(struct tog_view *);
474 static const struct got_error *open_tree_view(struct tog_view *,
475 struct got_tree_object *, struct got_object_id *, struct got_reflist_head *,
476 struct got_repository *);
477 static const struct got_error *show_tree_view(struct tog_view *);
478 static const struct got_error *input_tree_view(struct tog_view **,
479 struct tog_view **, struct tog_view **, struct tog_view *, int);
480 static const struct got_error *close_tree_view(struct tog_view *);
481 static const struct got_error *search_start_tree_view(struct tog_view *);
482 static const struct got_error *search_next_tree_view(struct tog_view *);
484 static volatile sig_atomic_t tog_sigwinch_received;
485 static volatile sig_atomic_t tog_sigpipe_received;
486 static volatile sig_atomic_t tog_sigcont_received;
489 tog_sigwinch(int signo)
491 tog_sigwinch_received = 1;
495 tog_sigpipe(int signo)
497 tog_sigpipe_received = 1;
501 tog_sigcont(int signo)
503 tog_sigcont_received = 1;
506 static const struct got_error *
507 view_close(struct tog_view *view)
509 const struct got_error *err = NULL;
512 view_close(view->child);
516 err = view->close(view);
518 del_panel(view->panel);
520 delwin(view->window);
525 static struct tog_view *
526 view_open(int nlines, int ncols, int begin_y, int begin_x,
527 enum tog_view_type type)
529 struct tog_view *view = calloc(1, sizeof(*view));
537 view->nlines = nlines ? nlines : LINES - begin_y;
538 view->ncols = ncols ? ncols : COLS - begin_x;
539 view->begin_y = begin_y;
540 view->begin_x = begin_x;
541 view->window = newwin(nlines, ncols, begin_y, begin_x);
542 if (view->window == NULL) {
546 view->panel = new_panel(view->window);
547 if (view->panel == NULL ||
548 set_panel_userptr(view->panel, view) != OK) {
553 keypad(view->window, TRUE);
558 view_split_begin_x(int begin_x)
560 if (begin_x > 0 || COLS < 120)
562 return (COLS - MAX(COLS / 2, 80));
565 static const struct got_error *view_resize(struct tog_view *);
567 static const struct got_error *
568 view_splitscreen(struct tog_view *view)
570 const struct got_error *err = NULL;
573 view->begin_x = view_split_begin_x(0);
574 view->nlines = LINES;
575 view->ncols = COLS - view->begin_x;
578 err = view_resize(view);
582 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
583 return got_error_from_errno("mvwin");
588 static const struct got_error *
589 view_fullscreen(struct tog_view *view)
591 const struct got_error *err = NULL;
595 view->nlines = LINES;
599 err = view_resize(view);
603 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
604 return got_error_from_errno("mvwin");
610 view_is_parent_view(struct tog_view *view)
612 return view->parent == NULL;
615 static const struct got_error *
616 view_resize(struct tog_view *view)
620 if (view->lines > LINES)
621 nlines = view->nlines - (view->lines - LINES);
623 nlines = view->nlines + (LINES - view->lines);
625 if (view->cols > COLS)
626 ncols = view->ncols - (view->cols - COLS);
628 ncols = view->ncols + (COLS - view->cols);
630 if (wresize(view->window, nlines, ncols) == ERR)
631 return got_error_from_errno("wresize");
632 if (replace_panel(view->panel, view->window) == ERR)
633 return got_error_from_errno("replace_panel");
634 wclear(view->window);
636 view->nlines = nlines;
642 view->child->begin_x = view_split_begin_x(view->begin_x);
643 if (view->child->begin_x == 0) {
644 view_fullscreen(view->child);
645 if (view->child->focussed)
646 show_panel(view->child->panel);
648 show_panel(view->panel);
650 view_splitscreen(view->child);
651 show_panel(view->child->panel);
658 static const struct got_error *
659 view_close_child(struct tog_view *view)
661 const struct got_error *err = NULL;
663 if (view->child == NULL)
666 err = view_close(view->child);
671 static const struct got_error *
672 view_set_child(struct tog_view *view, struct tog_view *child)
674 const struct got_error *err = NULL;
677 child->parent = view;
682 view_is_splitscreen(struct tog_view *view)
684 return view->begin_x > 0;
693 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
694 cols = 80; /* Default */
700 resize_term(lines, cols);
703 static const struct got_error *
704 view_search_start(struct tog_view *view)
706 const struct got_error *err = NULL;
710 if (view->nlines < 1)
713 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
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 /* Clear "no more matches" indicator. */
758 if (view->search_next_done == TOG_SEARCH_NO_MORE)
759 view->search_next_done = TOG_SEARCH_HAVE_MORE;
761 if (view->searching && !view->search_next_done) {
762 errcode = pthread_mutex_unlock(&tog_mutex);
764 return got_error_set_errno(errcode,
765 "pthread_mutex_unlock");
767 errcode = pthread_mutex_lock(&tog_mutex);
769 return got_error_set_errno(errcode,
770 "pthread_mutex_lock");
771 view->search_next(view);
775 nodelay(stdscr, FALSE);
776 /* Allow threads to make progress while we are waiting for input. */
777 errcode = pthread_mutex_unlock(&tog_mutex);
779 return got_error_set_errno(errcode, "pthread_mutex_unlock");
780 ch = wgetch(view->window);
781 errcode = pthread_mutex_lock(&tog_mutex);
783 return got_error_set_errno(errcode, "pthread_mutex_lock");
784 nodelay(stdscr, TRUE);
786 if (tog_sigwinch_received || tog_sigcont_received) {
788 tog_sigwinch_received = 0;
789 tog_sigcont_received = 0;
790 TAILQ_FOREACH(v, views, entry) {
791 err = view_resize(v);
794 err = v->input(new, dead, focus, v, KEY_RESIZE);
805 *focus = view->child;
806 view->child_focussed = 1;
807 } else if (view->parent) {
808 *focus = view->parent;
809 view->parent->child_focussed = 0;
813 err = view->input(new, dead, focus, view, ch);
820 if (view_is_parent_view(view)) {
821 if (view->child == NULL)
823 if (view_is_splitscreen(view->child)) {
824 *focus = view->child;
825 view->child_focussed = 1;
826 err = view_fullscreen(view->child);
828 err = view_splitscreen(view->child);
831 err = view->child->input(new, dead, focus,
832 view->child, KEY_RESIZE);
834 if (view_is_splitscreen(view)) {
836 view->parent->child_focussed = 1;
837 err = view_fullscreen(view);
839 err = view_splitscreen(view);
843 err = view->input(new, dead, focus, view,
850 if (view->search_start)
851 view_search_start(view);
853 err = view->input(new, dead, focus, view, ch);
857 if (view->search_next) {
858 view->searching = (ch == 'n' ?
859 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
860 view->search_next_done = 0;
861 view->search_next(view);
863 err = view->input(new, dead, focus, view, ch);
866 err = view->input(new, dead, focus, view, ch);
874 view_vborder(struct tog_view *view)
877 struct tog_view *view_above;
880 return view_vborder(view->parent);
882 panel = panel_above(view->panel);
886 view_above = panel_userptr(panel);
887 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
888 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
892 view_needs_focus_indication(struct tog_view *view)
894 if (view_is_parent_view(view)) {
895 if (view->child == NULL || view->child_focussed)
897 if (!view_is_splitscreen(view->child))
899 } else if (!view_is_splitscreen(view))
902 return view->focussed;
905 static const struct got_error *
906 view_loop(struct tog_view *view)
908 const struct got_error *err = NULL;
909 struct tog_view_list_head views;
910 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
911 int fast_refresh = 10;
912 int done = 0, errcode;
914 errcode = pthread_mutex_lock(&tog_mutex);
916 return got_error_set_errno(errcode, "pthread_mutex_lock");
919 TAILQ_INSERT_HEAD(&views, view, entry);
923 err = view->show(view);
928 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
929 /* Refresh fast during initialization, then become slower. */
930 if (fast_refresh && fast_refresh-- == 0)
931 halfdelay(10); /* switch to once per second */
933 err = view_input(&new_view, &dead_view, &focus_view, &done,
938 struct tog_view *prev = NULL;
940 if (view_is_parent_view(dead_view))
941 prev = TAILQ_PREV(dead_view,
942 tog_view_list_head, entry);
943 else if (view->parent != dead_view)
946 if (dead_view->parent)
947 dead_view->parent->child = NULL;
949 TAILQ_REMOVE(&views, dead_view, entry);
951 err = view_close(dead_view);
952 if (err || (dead_view == main_view && new_view == NULL))
955 if (view == dead_view) {
960 else if (!TAILQ_EMPTY(&views))
961 view = TAILQ_LAST(&views,
966 if (view->child && view->child_focussed)
967 focus_view = view->child;
974 struct tog_view *v, *t;
975 /* Only allow one parent view per type. */
976 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
977 if (v->type != new_view->type)
979 TAILQ_REMOVE(&views, v, entry);
985 TAILQ_INSERT_TAIL(&views, new_view, entry);
987 if (focus_view == NULL)
988 focus_view = new_view;
991 show_panel(focus_view->panel);
994 focus_view->focussed = 1;
997 show_panel(new_view->panel);
998 if (view->child && view_is_splitscreen(view->child))
999 show_panel(view->child->panel);
1002 if (focus_view == NULL) {
1004 show_panel(view->panel);
1005 if (view->child && view_is_splitscreen(view->child))
1006 show_panel(view->child->panel);
1010 err = view->parent->show(view->parent);
1014 err = view->show(view);
1018 err = view->child->show(view->child);
1027 while (!TAILQ_EMPTY(&views)) {
1028 view = TAILQ_FIRST(&views);
1029 TAILQ_REMOVE(&views, view, entry);
1033 errcode = pthread_mutex_unlock(&tog_mutex);
1034 if (errcode && err == NULL)
1035 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1045 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1050 /* Create newly allocated wide-character string equivalent to a byte string. */
1051 static const struct got_error *
1052 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1055 const struct got_error *err = NULL;
1058 *wlen = mbstowcs(NULL, s, 0);
1059 if (*wlen == (size_t)-1) {
1061 if (errno != EILSEQ)
1062 return got_error_from_errno("mbstowcs");
1064 /* byte string invalid in current encoding; try to "fix" it */
1065 err = got_mbsavis(&vis, &vislen, s);
1068 *wlen = mbstowcs(NULL, vis, 0);
1069 if (*wlen == (size_t)-1) {
1070 err = got_error_from_errno("mbstowcs"); /* give up */
1075 *ws = calloc(*wlen + 1, sizeof(**ws));
1077 err = got_error_from_errno("calloc");
1081 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1082 err = got_error_from_errno("mbstowcs");
1093 /* Format a line for display, ensuring that it won't overflow a width limit. */
1094 static const struct got_error *
1095 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1098 const struct got_error *err = NULL;
1100 wchar_t *wline = NULL;
1107 err = mbs2ws(&wline, &wlen, line);
1113 int width = wcwidth(wline[i]);
1120 if (width == 1 || width == 2) {
1121 if (cols + width > wlimit)
1125 } else if (width == -1) {
1126 if (wline[i] == L'\t') {
1128 ((cols + col_tab_align) % TABSIZE);
1129 if (cols + width > wlimit)
1135 err = got_error_from_errno("wcwidth");
1150 static const struct got_error*
1151 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1152 struct got_object_id *id, struct got_repository *repo)
1154 static const struct got_error *err = NULL;
1155 struct got_reflist_entry *re;
1161 SIMPLEQ_FOREACH(re, refs, entry) {
1162 struct got_tag_object *tag = NULL;
1165 name = got_ref_get_name(re->ref);
1166 if (strcmp(name, GOT_REF_HEAD) == 0)
1168 if (strncmp(name, "refs/", 5) == 0)
1170 if (strncmp(name, "got/", 4) == 0)
1172 if (strncmp(name, "heads/", 6) == 0)
1174 if (strncmp(name, "remotes/", 8) == 0)
1176 if (strncmp(name, "tags/", 5) == 0) {
1177 err = got_object_open_as_tag(&tag, repo, re->id);
1179 if (err->code != GOT_ERR_OBJ_TYPE)
1181 /* Ref points at something other than a tag. */
1186 cmp = got_object_id_cmp(tag ?
1187 got_object_tag_get_object_id(tag) : re->id, id);
1189 got_object_tag_close(tag);
1193 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1194 s ? ", " : "", name) == -1) {
1195 err = got_error_from_errno("asprintf");
1206 static const struct got_error *
1207 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1210 char *smallerthan, *at;
1212 smallerthan = strchr(author, '<');
1213 if (smallerthan && smallerthan[1] != '\0')
1214 author = smallerthan + 1;
1215 at = strchr(author, '@');
1218 return format_line(wauthor, author_width, author, limit, col_tab_align);
1221 static const struct got_error *
1222 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1223 struct got_object_id *id, struct got_reflist_head *refs,
1224 const size_t date_display_cols, int author_display_cols,
1225 struct tog_colors *colors)
1227 const struct got_error *err = NULL;
1228 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1229 char *logmsg0 = NULL, *logmsg = NULL;
1230 char *author = NULL;
1231 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1232 int author_width, logmsg_width;
1233 char *newline, *line = NULL;
1235 const int avail = view->ncols;
1237 time_t committer_time;
1238 struct tog_color *tc;
1240 committer_time = got_object_commit_get_committer_time(commit);
1241 if (localtime_r(&committer_time, &tm) == NULL)
1242 return got_error_from_errno("localtime_r");
1243 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1245 return got_error(GOT_ERR_NO_SPACE);
1247 if (avail <= date_display_cols)
1248 limit = MIN(sizeof(datebuf) - 1, avail);
1250 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1251 tc = get_color(colors, TOG_COLOR_DATE);
1253 wattr_on(view->window,
1254 COLOR_PAIR(tc->colorpair), NULL);
1255 waddnstr(view->window, datebuf, limit);
1257 wattr_off(view->window,
1258 COLOR_PAIR(tc->colorpair), NULL);
1265 err = got_object_id_str(&id_str, id);
1268 tc = get_color(colors, TOG_COLOR_COMMIT);
1270 wattr_on(view->window,
1271 COLOR_PAIR(tc->colorpair), NULL);
1272 wprintw(view->window, "%.8s ", id_str);
1274 wattr_off(view->window,
1275 COLOR_PAIR(tc->colorpair), NULL);
1282 author = strdup(got_object_commit_get_author(commit));
1283 if (author == NULL) {
1284 err = got_error_from_errno("strdup");
1287 err = format_author(&wauthor, &author_width, author, avail - col, col);
1290 tc = get_color(colors, TOG_COLOR_AUTHOR);
1292 wattr_on(view->window,
1293 COLOR_PAIR(tc->colorpair), NULL);
1294 waddwstr(view->window, wauthor);
1296 wattr_off(view->window,
1297 COLOR_PAIR(tc->colorpair), NULL);
1298 col += author_width;
1299 while (col < avail && author_width < author_display_cols + 2) {
1300 waddch(view->window, ' ');
1307 err = got_object_commit_get_logmsg(&logmsg0, commit);
1311 while (*logmsg == '\n')
1313 newline = strchr(logmsg, '\n');
1316 limit = avail - col;
1317 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1320 waddwstr(view->window, wlogmsg);
1321 col += logmsg_width;
1322 while (col < avail) {
1323 waddch(view->window, ' ');
1335 static struct commit_queue_entry *
1336 alloc_commit_queue_entry(struct got_commit_object *commit,
1337 struct got_object_id *id)
1339 struct commit_queue_entry *entry;
1341 entry = calloc(1, sizeof(*entry));
1346 entry->commit = commit;
1351 pop_commit(struct commit_queue *commits)
1353 struct commit_queue_entry *entry;
1355 entry = TAILQ_FIRST(&commits->head);
1356 TAILQ_REMOVE(&commits->head, entry, entry);
1357 got_object_commit_close(entry->commit);
1358 commits->ncommits--;
1359 /* Don't free entry->id! It is owned by the commit graph. */
1364 free_commits(struct commit_queue *commits)
1366 while (!TAILQ_EMPTY(&commits->head))
1367 pop_commit(commits);
1370 static const struct got_error *
1371 match_commit(int *have_match, struct got_object_id *id,
1372 struct got_commit_object *commit, regex_t *regex)
1374 const struct got_error *err = NULL;
1375 regmatch_t regmatch;
1376 char *id_str = NULL, *logmsg = NULL;
1380 err = got_object_id_str(&id_str, id);
1384 err = got_object_commit_get_logmsg(&logmsg, commit);
1388 if (regexec(regex, got_object_commit_get_author(commit), 1,
1389 ®match, 0) == 0 ||
1390 regexec(regex, got_object_commit_get_committer(commit), 1,
1391 ®match, 0) == 0 ||
1392 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1393 regexec(regex, logmsg, 1, ®match, 0) == 0)
1401 static const struct got_error *
1402 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1403 int minqueue, struct got_repository *repo, const char *path,
1404 int *searching, int *search_next_done, regex_t *regex)
1406 const struct got_error *err = NULL;
1410 * We keep all commits open throughout the lifetime of the log
1411 * view in order to avoid having to re-fetch commits from disk
1412 * while updating the display.
1414 while (nqueued < minqueue ||
1415 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1416 struct got_object_id *id;
1417 struct got_commit_object *commit;
1418 struct commit_queue_entry *entry;
1421 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1422 if (err || id == NULL)
1425 err = got_object_open_as_commit(&commit, repo, id);
1428 entry = alloc_commit_queue_entry(commit, id);
1429 if (entry == NULL) {
1430 err = got_error_from_errno("alloc_commit_queue_entry");
1434 errcode = pthread_mutex_lock(&tog_mutex);
1436 err = got_error_set_errno(errcode,
1437 "pthread_mutex_lock");
1441 entry->idx = commits->ncommits;
1442 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1444 commits->ncommits++;
1446 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1448 err = match_commit(&have_match, id, commit, regex);
1452 *search_next_done = TOG_SEARCH_HAVE_MORE;
1455 errcode = pthread_mutex_unlock(&tog_mutex);
1456 if (errcode && err == NULL)
1457 err = got_error_set_errno(errcode,
1458 "pthread_mutex_unlock");
1466 static const struct got_error *
1467 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1468 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1469 struct commit_queue *commits, int selected_idx, int limit,
1470 struct got_reflist_head *refs, const char *path, int commits_needed,
1471 struct tog_colors *colors)
1473 const struct got_error *err = NULL;
1474 struct tog_log_view_state *s = &view->state.log;
1475 struct commit_queue_entry *entry;
1477 int ncommits, author_cols = 4;
1478 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1479 char *refs_str = NULL;
1481 struct tog_color *tc;
1482 static const size_t date_display_cols = 12;
1487 if (ncommits == selected_idx) {
1491 entry = TAILQ_NEXT(entry, entry);
1495 if (*selected && !(view->searching && view->search_next_done == 0)) {
1496 err = got_object_id_str(&id_str, (*selected)->id);
1500 err = build_refs_str(&refs_str, refs, (*selected)->id,
1507 if (commits_needed == 0)
1508 halfdelay(10); /* disable fast refresh */
1510 if (commits_needed > 0) {
1511 if (asprintf(&ncommits_str, " [%d/%d] %s",
1512 entry ? entry->idx + 1 : 0, commits->ncommits,
1513 view->searching ? "searching..." : "loading...") == -1) {
1514 err = got_error_from_errno("asprintf");
1518 if (asprintf(&ncommits_str, " [%d/%d] %s",
1519 entry ? entry->idx + 1 : 0, commits->ncommits,
1520 view->search_next_done == TOG_SEARCH_NO_MORE ?
1521 "no more matches" : (refs_str ? refs_str : "")) == -1) {
1522 err = got_error_from_errno("asprintf");
1527 if (path && strcmp(path, "/") != 0) {
1528 if (asprintf(&header, "commit %s %s%s",
1529 id_str ? id_str : "........................................",
1530 path, ncommits_str) == -1) {
1531 err = got_error_from_errno("asprintf");
1535 } else if (asprintf(&header, "commit %s%s",
1536 id_str ? id_str : "........................................",
1537 ncommits_str) == -1) {
1538 err = got_error_from_errno("asprintf");
1542 err = format_line(&wline, &width, header, view->ncols, 0);
1546 werase(view->window);
1548 if (view_needs_focus_indication(view))
1549 wstandout(view->window);
1550 tc = get_color(colors, TOG_COLOR_COMMIT);
1552 wattr_on(view->window,
1553 COLOR_PAIR(tc->colorpair), NULL);
1554 waddwstr(view->window, wline);
1556 wattr_off(view->window,
1557 COLOR_PAIR(tc->colorpair), NULL);
1558 while (width < view->ncols) {
1559 waddch(view->window, ' ');
1562 if (view_needs_focus_indication(view))
1563 wstandend(view->window);
1568 /* Grow author column size if necessary. */
1575 if (ncommits >= limit - 1)
1577 author = strdup(got_object_commit_get_author(entry->commit));
1578 if (author == NULL) {
1579 err = got_error_from_errno("strdup");
1582 err = format_author(&wauthor, &width, author, COLS,
1584 if (author_cols < width)
1585 author_cols = width;
1589 entry = TAILQ_NEXT(entry, entry);
1596 if (ncommits >= limit - 1)
1598 if (ncommits == selected_idx)
1599 wstandout(view->window);
1600 err = draw_commit(view, entry->commit, entry->id, refs,
1601 date_display_cols, author_cols, colors);
1602 if (ncommits == selected_idx)
1603 wstandend(view->window);
1608 entry = TAILQ_NEXT(entry, entry);
1621 scroll_up(struct tog_view *view,
1622 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1623 struct commit_queue *commits)
1625 struct commit_queue_entry *entry;
1628 entry = TAILQ_FIRST(&commits->head);
1629 if (*first_displayed_entry == entry)
1632 entry = *first_displayed_entry;
1633 while (entry && nscrolled < maxscroll) {
1634 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1636 *first_displayed_entry = entry;
1642 static const struct got_error *
1643 trigger_log_thread(struct tog_view *log_view, int wait,
1644 int *commits_needed, int *log_complete,
1645 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1649 halfdelay(1); /* fast refresh while loading commits */
1651 while (*commits_needed > 0) {
1655 /* Wake the log thread. */
1656 errcode = pthread_cond_signal(need_commits);
1658 return got_error_set_errno(errcode,
1659 "pthread_cond_signal");
1662 * The mutex will be released while the view loop waits
1663 * in wgetch(), at which time the log thread will run.
1668 /* Display progress update in log view. */
1669 show_log_view(log_view);
1673 /* Wait right here while next commit is being loaded. */
1674 errcode = pthread_cond_wait(commit_loaded, &tog_mutex);
1676 return got_error_set_errno(errcode,
1677 "pthread_cond_wait");
1679 /* Display progress update in log view. */
1680 show_log_view(log_view);
1688 static const struct got_error *
1689 scroll_down(struct tog_view *log_view,
1690 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1691 struct commit_queue_entry **last_displayed_entry,
1692 struct commit_queue *commits, int *log_complete, int *commits_needed,
1693 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1695 const struct got_error *err = NULL;
1696 struct commit_queue_entry *pentry;
1697 int nscrolled = 0, ncommits_needed;
1699 if (*last_displayed_entry == NULL)
1702 ncommits_needed = (*last_displayed_entry)->idx + 1 + maxscroll;
1703 if (commits->ncommits < ncommits_needed && !*log_complete) {
1705 * Ask the log thread for required amount of commits.
1707 (*commits_needed) += maxscroll;
1708 err = trigger_log_thread(log_view, 1, commits_needed,
1709 log_complete, need_commits, commit_loaded);
1715 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1719 *last_displayed_entry = pentry;
1721 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1724 *first_displayed_entry = pentry;
1725 } while (++nscrolled < maxscroll);
1730 static const struct got_error *
1731 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1732 struct got_commit_object *commit, struct got_object_id *commit_id,
1733 struct tog_view *log_view, struct got_reflist_head *refs,
1734 struct got_repository *repo)
1736 const struct got_error *err;
1737 struct got_object_qid *parent_id;
1738 struct tog_view *diff_view;
1740 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1741 if (diff_view == NULL)
1742 return got_error_from_errno("view_open");
1744 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1745 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1746 commit_id, log_view, refs, repo);
1748 *new_view = diff_view;
1752 static const struct got_error *
1753 tree_view_visit_subtree(struct got_tree_object *subtree,
1754 struct tog_tree_view_state *s)
1756 struct tog_parent_tree *parent;
1758 parent = calloc(1, sizeof(*parent));
1760 return got_error_from_errno("calloc");
1762 parent->tree = s->tree;
1763 parent->first_displayed_entry = s->first_displayed_entry;
1764 parent->selected_entry = s->selected_entry;
1765 parent->selected = s->selected;
1766 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1769 s->first_displayed_entry = NULL;
1773 static const struct got_error *
1774 tree_view_walk_path(struct tog_tree_view_state *s,
1775 struct got_object_id *commit_id,
1776 const char *path, struct got_repository *repo)
1778 const struct got_error *err = NULL;
1779 struct got_tree_object *tree = NULL;
1781 char *slash, *subpath = NULL;
1783 /* Walk the path and open corresponding tree objects. */
1786 struct got_tree_entry *te;
1787 struct got_object_id *tree_id;
1793 /* Ensure the correct subtree entry is selected. */
1794 slash = strchr(p, '/');
1796 te_name = strdup(p);
1798 te_name = strndup(p, slash - p);
1799 if (te_name == NULL) {
1800 err = got_error_from_errno("strndup");
1803 te = got_object_tree_find_entry(s->tree, te_name);
1805 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1810 s->selected_entry = te;
1811 s->selected = got_tree_entry_get_index(te);
1812 if (s->tree != s->root)
1813 s->selected++; /* skip '..' */
1815 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1816 /* Jump to this file's entry. */
1817 s->first_displayed_entry = s->selected_entry;
1822 slash = strchr(p, '/');
1824 subpath = strndup(path, slash - path);
1826 subpath = strdup(path);
1827 if (subpath == NULL) {
1828 err = got_error_from_errno("strdup");
1832 err = got_object_id_by_path(&tree_id, repo, commit_id,
1837 err = got_object_open_as_tree(&tree, repo, tree_id);
1842 err = tree_view_visit_subtree(tree, s);
1844 got_object_tree_close(tree);
1858 static const struct got_error *
1859 browse_commit_tree(struct tog_view **new_view, int begin_x,
1860 struct commit_queue_entry *entry, const char *path,
1861 struct got_reflist_head *refs, struct got_repository *repo)
1863 const struct got_error *err = NULL;
1864 struct got_tree_object *tree;
1865 struct tog_tree_view_state *s;
1866 struct tog_view *tree_view;
1868 err = got_object_open_as_tree(&tree, repo,
1869 got_object_commit_get_tree_id(entry->commit));
1873 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1874 if (tree_view == NULL)
1875 return got_error_from_errno("view_open");
1877 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1879 got_object_tree_close(tree);
1882 s = &tree_view->state.tree;
1884 *new_view = tree_view;
1886 if (got_path_is_root_dir(path))
1889 return tree_view_walk_path(s, entry->id, path, repo);
1892 static const struct got_error *
1893 block_signals_used_by_main_thread(void)
1898 if (sigemptyset(&sigset) == -1)
1899 return got_error_from_errno("sigemptyset");
1901 /* tog handles SIGWINCH and SIGCONT */
1902 if (sigaddset(&sigset, SIGWINCH) == -1)
1903 return got_error_from_errno("sigaddset");
1904 if (sigaddset(&sigset, SIGCONT) == -1)
1905 return got_error_from_errno("sigaddset");
1907 /* ncurses handles SIGTSTP */
1908 if (sigaddset(&sigset, SIGTSTP) == -1)
1909 return got_error_from_errno("sigaddset");
1911 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1913 return got_error_set_errno(errcode, "pthread_sigmask");
1919 log_thread(void *arg)
1921 const struct got_error *err = NULL;
1923 struct tog_log_thread_args *a = arg;
1926 err = block_signals_used_by_main_thread();
1930 while (!done && !err && !tog_sigpipe_received) {
1931 err = queue_commits(a->graph, a->commits, 1, a->repo,
1932 a->in_repo_path, a->searching, a->search_next_done,
1935 if (err->code != GOT_ERR_ITER_COMPLETED)
1939 } else if (a->commits_needed > 0)
1940 a->commits_needed--;
1942 errcode = pthread_mutex_lock(&tog_mutex);
1944 err = got_error_set_errno(errcode,
1945 "pthread_mutex_lock");
1947 } else if (*a->quit)
1949 else if (*a->first_displayed_entry == NULL) {
1950 *a->first_displayed_entry =
1951 TAILQ_FIRST(&a->commits->head);
1952 *a->selected_entry = *a->first_displayed_entry;
1955 errcode = pthread_cond_signal(&a->commit_loaded);
1957 err = got_error_set_errno(errcode,
1958 "pthread_cond_signal");
1959 pthread_mutex_unlock(&tog_mutex);
1964 a->commits_needed = 0;
1966 if (a->commits_needed == 0) {
1967 errcode = pthread_cond_wait(&a->need_commits,
1970 err = got_error_set_errno(errcode,
1971 "pthread_cond_wait");
1975 errcode = pthread_mutex_unlock(&tog_mutex);
1976 if (errcode && err == NULL)
1977 err = got_error_set_errno(errcode,
1978 "pthread_mutex_unlock");
1980 a->log_complete = 1;
1984 static const struct got_error *
1985 stop_log_thread(struct tog_log_view_state *s)
1987 const struct got_error *err = NULL;
1992 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1994 return got_error_set_errno(errcode,
1995 "pthread_cond_signal");
1996 errcode = pthread_mutex_unlock(&tog_mutex);
1998 return got_error_set_errno(errcode,
1999 "pthread_mutex_unlock");
2000 errcode = pthread_join(s->thread, (void **)&err);
2002 return got_error_set_errno(errcode, "pthread_join");
2003 errcode = pthread_mutex_lock(&tog_mutex);
2005 return got_error_set_errno(errcode,
2006 "pthread_mutex_lock");
2010 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2011 if (errcode && err == NULL)
2012 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2014 if (s->thread_args.repo) {
2015 got_repo_close(s->thread_args.repo);
2016 s->thread_args.repo = NULL;
2019 if (s->thread_args.graph) {
2020 got_commit_graph_close(s->thread_args.graph);
2021 s->thread_args.graph = NULL;
2027 static const struct got_error *
2028 close_log_view(struct tog_view *view)
2030 const struct got_error *err = NULL;
2031 struct tog_log_view_state *s = &view->state.log;
2033 err = stop_log_thread(s);
2034 free_commits(&s->commits);
2035 free(s->in_repo_path);
2036 s->in_repo_path = NULL;
2042 static const struct got_error *
2043 search_start_log_view(struct tog_view *view)
2045 struct tog_log_view_state *s = &view->state.log;
2047 s->matched_entry = NULL;
2048 s->search_entry = NULL;
2052 static const struct got_error *
2053 search_next_log_view(struct tog_view *view)
2055 const struct got_error *err = NULL;
2056 struct tog_log_view_state *s = &view->state.log;
2057 struct commit_queue_entry *entry;
2059 if (s->search_entry) {
2061 errcode = pthread_mutex_unlock(&tog_mutex);
2063 return got_error_set_errno(errcode,
2064 "pthread_mutex_unlock");
2065 ch = wgetch(view->window);
2066 errcode = pthread_mutex_lock(&tog_mutex);
2068 return got_error_set_errno(errcode,
2069 "pthread_mutex_lock");
2070 if (ch == KEY_BACKSPACE) {
2071 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2074 if (view->searching == TOG_SEARCH_FORWARD)
2075 entry = TAILQ_NEXT(s->search_entry, entry);
2077 entry = TAILQ_PREV(s->search_entry,
2078 commit_queue_head, entry);
2079 } else if (s->matched_entry) {
2080 if (view->searching == TOG_SEARCH_FORWARD)
2081 entry = TAILQ_NEXT(s->matched_entry, entry);
2083 entry = TAILQ_PREV(s->matched_entry,
2084 commit_queue_head, entry);
2086 if (view->searching == TOG_SEARCH_FORWARD)
2087 entry = TAILQ_FIRST(&s->commits.head);
2089 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2095 if (entry == NULL) {
2096 if (s->thread_args.log_complete ||
2097 view->searching == TOG_SEARCH_BACKWARD) {
2098 view->search_next_done = TOG_SEARCH_NO_MORE;
2099 s->search_entry = NULL;
2103 * Poke the log thread for more commits and return,
2104 * allowing the main loop to make progress. Search
2105 * will resume at s->search_entry once we come back.
2107 s->thread_args.commits_needed++;
2108 return trigger_log_thread(view, 0,
2109 &s->thread_args.commits_needed,
2110 &s->thread_args.log_complete,
2111 &s->thread_args.need_commits,
2112 &s->thread_args.commit_loaded);
2115 err = match_commit(&have_match, entry->id, entry->commit,
2120 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2121 s->matched_entry = entry;
2125 s->search_entry = entry;
2126 if (view->searching == TOG_SEARCH_FORWARD)
2127 entry = TAILQ_NEXT(entry, entry);
2129 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2132 if (s->matched_entry) {
2133 int cur = s->selected_entry->idx;
2134 while (cur < s->matched_entry->idx) {
2135 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2140 while (cur > s->matched_entry->idx) {
2141 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2148 s->search_entry = NULL;
2153 static const struct got_error *
2154 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2155 struct got_reflist_head *refs, struct got_repository *repo,
2156 const char *head_ref_name, const char *in_repo_path,
2159 const struct got_error *err = NULL;
2160 struct tog_log_view_state *s = &view->state.log;
2161 struct got_repository *thread_repo = NULL;
2162 struct got_commit_graph *thread_graph = NULL;
2165 if (in_repo_path != s->in_repo_path) {
2166 free(s->in_repo_path);
2167 s->in_repo_path = strdup(in_repo_path);
2168 if (s->in_repo_path == NULL)
2169 return got_error_from_errno("strdup");
2172 /* The commit queue only contains commits being displayed. */
2173 TAILQ_INIT(&s->commits.head);
2174 s->commits.ncommits = 0;
2178 s->head_ref_name = head_ref_name;
2179 s->start_id = got_object_id_dup(start_id);
2180 if (s->start_id == NULL) {
2181 err = got_error_from_errno("got_object_id_dup");
2184 s->log_branches = log_branches;
2186 SIMPLEQ_INIT(&s->colors);
2187 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2188 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2189 get_color_value("TOG_COLOR_COMMIT"));
2192 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2193 get_color_value("TOG_COLOR_AUTHOR"));
2195 free_colors(&s->colors);
2198 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2199 get_color_value("TOG_COLOR_DATE"));
2201 free_colors(&s->colors);
2206 view->show = show_log_view;
2207 view->input = input_log_view;
2208 view->close = close_log_view;
2209 view->search_start = search_start_log_view;
2210 view->search_next = search_next_log_view;
2212 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2215 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2219 err = got_commit_graph_iter_start(thread_graph,
2220 s->start_id, s->repo, NULL, NULL);
2224 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2226 err = got_error_set_errno(errcode, "pthread_cond_init");
2229 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2231 err = got_error_set_errno(errcode, "pthread_cond_init");
2235 s->thread_args.commits_needed = view->nlines;
2236 s->thread_args.graph = thread_graph;
2237 s->thread_args.commits = &s->commits;
2238 s->thread_args.in_repo_path = s->in_repo_path;
2239 s->thread_args.start_id = s->start_id;
2240 s->thread_args.repo = thread_repo;
2241 s->thread_args.log_complete = 0;
2242 s->thread_args.quit = &s->quit;
2243 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2244 s->thread_args.selected_entry = &s->selected_entry;
2245 s->thread_args.searching = &view->searching;
2246 s->thread_args.search_next_done = &view->search_next_done;
2247 s->thread_args.regex = &view->regex;
2250 close_log_view(view);
2254 static const struct got_error *
2255 show_log_view(struct tog_view *view)
2257 struct tog_log_view_state *s = &view->state.log;
2259 if (s->thread == NULL) {
2260 int errcode = pthread_create(&s->thread, NULL, log_thread,
2263 return got_error_set_errno(errcode, "pthread_create");
2266 return draw_commits(view, &s->last_displayed_entry,
2267 &s->selected_entry, s->first_displayed_entry,
2268 &s->commits, s->selected, view->nlines, s->refs,
2269 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2272 static const struct got_error *
2273 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2274 struct tog_view **focus_view, struct tog_view *view, int ch)
2276 const struct got_error *err = NULL;
2277 struct tog_log_view_state *s = &view->state.log;
2278 char *parent_path, *in_repo_path = NULL;
2279 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2281 struct got_object_id *start_id;
2291 if (s->first_displayed_entry == NULL)
2293 if (s->selected > 0)
2296 scroll_up(view, &s->first_displayed_entry, 1,
2301 if (s->first_displayed_entry == NULL)
2303 if (TAILQ_FIRST(&s->commits.head) ==
2304 s->first_displayed_entry) {
2308 scroll_up(view, &s->first_displayed_entry,
2309 view->nlines - 1, &s->commits);
2315 if (s->first_displayed_entry == NULL)
2317 if (s->selected < MIN(view->nlines - 2,
2318 s->commits.ncommits - 1)) {
2322 err = scroll_down(view, &s->first_displayed_entry, 1,
2323 &s->last_displayed_entry, &s->commits,
2324 &s->thread_args.log_complete,
2325 &s->thread_args.commits_needed,
2326 &s->thread_args.need_commits,
2327 &s->thread_args.commit_loaded);
2331 struct commit_queue_entry *first;
2332 first = s->first_displayed_entry;
2335 err = scroll_down(view, &s->first_displayed_entry,
2336 view->nlines - 1, &s->last_displayed_entry,
2337 &s->commits, &s->thread_args.log_complete,
2338 &s->thread_args.commits_needed,
2339 &s->thread_args.need_commits,
2340 &s->thread_args.commit_loaded);
2343 if (first == s->first_displayed_entry &&
2344 s->selected < MIN(view->nlines - 2,
2345 s->commits.ncommits - 1)) {
2346 /* can't scroll further down */
2347 s->selected = MIN(view->nlines - 2,
2348 s->commits.ncommits - 1);
2354 if (s->selected > view->nlines - 2)
2355 s->selected = view->nlines - 2;
2356 if (s->selected > s->commits.ncommits - 1)
2357 s->selected = s->commits.ncommits - 1;
2362 if (s->selected_entry == NULL)
2364 if (view_is_parent_view(view))
2365 begin_x = view_split_begin_x(view->begin_x);
2366 err = open_diff_view_for_commit(&diff_view, begin_x,
2367 s->selected_entry->commit, s->selected_entry->id,
2368 view, s->refs, s->repo);
2371 if (view_is_parent_view(view)) {
2372 err = view_close_child(view);
2375 err = view_set_child(view, diff_view);
2377 view_close(diff_view);
2380 *focus_view = diff_view;
2381 view->child_focussed = 1;
2383 *new_view = diff_view;
2386 if (s->selected_entry == NULL)
2388 if (view_is_parent_view(view))
2389 begin_x = view_split_begin_x(view->begin_x);
2390 err = browse_commit_tree(&tree_view, begin_x,
2391 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2394 if (view_is_parent_view(view)) {
2395 err = view_close_child(view);
2398 err = view_set_child(view, tree_view);
2400 view_close(tree_view);
2403 *focus_view = tree_view;
2404 view->child_focussed = 1;
2406 *new_view = tree_view;
2409 if (strcmp(s->in_repo_path, "/") == 0)
2411 parent_path = dirname(s->in_repo_path);
2412 if (parent_path && strcmp(parent_path, ".") != 0) {
2413 err = stop_log_thread(s);
2416 lv = view_open(view->nlines, view->ncols,
2417 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2419 return got_error_from_errno(
2421 err = open_log_view(lv, s->start_id, s->refs,
2422 s->repo, s->head_ref_name, parent_path,
2426 if (view_is_parent_view(view))
2429 view_set_child(view->parent, lv);
2436 err = stop_log_thread(s);
2439 lv = view_open(view->nlines, view->ncols,
2440 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2442 return got_error_from_errno("view_open");
2443 err = got_repo_match_object_id(&start_id, NULL,
2444 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2445 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2450 in_repo_path = strdup(s->in_repo_path);
2451 if (in_repo_path == NULL) {
2454 return got_error_from_errno("strdup");
2456 got_ref_list_free(s->refs);
2457 err = got_ref_list(s->refs, s->repo, NULL,
2458 got_ref_cmp_by_name, NULL);
2464 err = open_log_view(lv, start_id, s->refs, s->repo,
2465 s->head_ref_name, in_repo_path, s->log_branches);
2475 s->log_branches = !s->log_branches;
2476 err = stop_log_thread(s);
2479 lv = view_open(view->nlines, view->ncols,
2480 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2482 return got_error_from_errno("view_open");
2483 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2484 s->head_ref_name, s->in_repo_path, s->log_branches);
2499 static const struct got_error *
2500 apply_unveil(const char *repo_path, const char *worktree_path)
2502 const struct got_error *error;
2505 if (unveil("gmon.out", "rwc") != 0)
2506 return got_error_from_errno2("unveil", "gmon.out");
2508 if (repo_path && unveil(repo_path, "r") != 0)
2509 return got_error_from_errno2("unveil", repo_path);
2511 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2512 return got_error_from_errno2("unveil", worktree_path);
2514 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2515 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2517 error = got_privsep_unveil_exec_helpers();
2521 if (unveil(NULL, NULL) != 0)
2522 return got_error_from_errno("unveil");
2532 halfdelay(1); /* Do fast refresh while initial view is loading. */
2535 intrflush(stdscr, FALSE);
2536 keypad(stdscr, TRUE);
2538 if (getenv("TOG_COLORS") != NULL) {
2540 use_default_colors();
2542 signal(SIGWINCH, tog_sigwinch);
2543 signal(SIGPIPE, tog_sigpipe);
2544 signal(SIGCONT, tog_sigcont);
2547 static const struct got_error *
2548 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2549 struct got_repository *repo, struct got_worktree *worktree)
2551 const struct got_error *err = NULL;
2554 *in_repo_path = strdup("/");
2555 if (*in_repo_path == NULL)
2556 return got_error_from_errno("strdup");
2561 const char *prefix = got_worktree_get_path_prefix(worktree);
2564 err = got_worktree_resolve_path(&wt_path, worktree, argv[0]);
2568 if (asprintf(&p, "%s%s%s", prefix,
2569 (strcmp(prefix, "/") != 0) ? "/" : "", wt_path) == -1) {
2570 err = got_error_from_errno("asprintf");
2574 err = got_repo_map_path(in_repo_path, repo, p, 0);
2578 err = got_repo_map_path(in_repo_path, repo, argv[0], 1);
2583 static const struct got_error *
2584 cmd_log(int argc, char *argv[])
2586 const struct got_error *error;
2587 struct got_repository *repo = NULL;
2588 struct got_worktree *worktree = NULL;
2589 struct got_reflist_head refs;
2590 struct got_object_id *start_id = NULL;
2591 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2592 char *start_commit = NULL, *head_ref_name = NULL;
2593 int ch, log_branches = 0;
2594 struct tog_view *view;
2596 SIMPLEQ_INIT(&refs);
2599 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2604 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2610 start_commit = optarg;
2613 repo_path = realpath(optarg, NULL);
2614 if (repo_path == NULL)
2615 return got_error_from_errno2("realpath",
2630 cwd = getcwd(NULL, 0);
2632 return got_error_from_errno("getcwd");
2634 error = got_worktree_open(&worktree, cwd);
2635 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2638 if (repo_path == NULL) {
2641 strdup(got_worktree_get_repo_path(worktree));
2643 repo_path = strdup(cwd);
2645 if (repo_path == NULL) {
2646 error = got_error_from_errno("strdup");
2650 error = got_repo_open(&repo, repo_path, NULL);
2654 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2661 error = apply_unveil(got_repo_get_path(repo),
2662 worktree ? got_worktree_get_root_path(worktree) : NULL);
2666 if (start_commit == NULL)
2667 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2668 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2669 GOT_OBJ_TYPE_COMMIT, 1, repo);
2671 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2672 GOT_OBJ_TYPE_COMMIT, 1, repo);
2676 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2680 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2682 error = got_error_from_errno("view_open");
2686 head_ref_name = strdup(
2687 got_worktree_get_head_ref_name(worktree));
2688 if (head_ref_name == NULL) {
2689 error = got_error_from_errno("strdup");
2693 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2694 in_repo_path, log_branches);
2698 /* Release work tree lock. */
2699 got_worktree_close(worktree);
2702 error = view_loop(view);
2708 free(head_ref_name);
2710 got_repo_close(repo);
2712 got_worktree_close(worktree);
2713 got_ref_list_free(&refs);
2721 fprintf(stderr, "usage: %s diff [-r repository-path] object1 object2\n",
2727 parse_next_line(FILE *f, size_t *len)
2732 const char delim[3] = { '\0', '\0', '\0'};
2734 line = fparseln(f, &linelen, &lineno, delim, 0);
2741 match_line(const char *line, regex_t *regex)
2743 regmatch_t regmatch;
2745 return regexec(regex, line, 1, ®match, 0) == 0;
2749 match_color(struct tog_colors *colors, const char *line)
2751 struct tog_color *tc = NULL;
2753 SIMPLEQ_FOREACH(tc, colors, entry) {
2754 if (match_line(line, &tc->regex))
2761 static const struct got_error *
2762 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line, int nlines,
2763 int selected_line, int max_lines, int *last_displayed_line, int *eof,
2764 char *header, struct tog_colors *colors)
2766 const struct got_error *err;
2767 int lineno = 0, nprinted = 0;
2769 struct tog_color *tc;
2775 werase(view->window);
2778 err = format_line(&wline, &width, header, view->ncols, 0);
2783 if (view_needs_focus_indication(view))
2784 wstandout(view->window);
2785 waddwstr(view->window, wline);
2786 if (view_needs_focus_indication(view))
2787 wstandend(view->window);
2788 if (width <= view->ncols - 1)
2789 waddch(view->window, '\n');
2797 while (nprinted < max_lines) {
2798 line = parse_next_line(f, &len);
2803 if (++lineno < *first_displayed_line) {
2808 err = format_line(&wline, &width, line, view->ncols, 0);
2814 tc = match_color(colors, line);
2816 wattr_on(view->window,
2817 COLOR_PAIR(tc->colorpair), NULL);
2818 waddwstr(view->window, wline);
2820 wattr_off(view->window,
2821 COLOR_PAIR(tc->colorpair), NULL);
2822 if (width <= view->ncols - 1)
2823 waddch(view->window, '\n');
2824 if (++nprinted == 1)
2825 *first_displayed_line = lineno;
2830 *last_displayed_line = lineno;
2835 while (nprinted < view->nlines) {
2836 waddch(view->window, '\n');
2840 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2845 wstandout(view->window);
2846 waddwstr(view->window, wline);
2847 wstandend(view->window);
2854 get_datestr(time_t *time, char *datebuf)
2856 struct tm mytm, *tm;
2859 tm = gmtime_r(time, &mytm);
2862 s = asctime_r(tm, datebuf);
2865 p = strchr(s, '\n');
2871 static const struct got_error *
2872 write_commit_info(struct got_object_id *commit_id,
2873 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2875 const struct got_error *err = NULL;
2876 char datebuf[26], *datestr;
2877 struct got_commit_object *commit;
2878 char *id_str = NULL, *logmsg = NULL;
2879 time_t committer_time;
2880 const char *author, *committer;
2881 char *refs_str = NULL;
2884 err = build_refs_str(&refs_str, refs, commit_id, repo);
2889 err = got_object_open_as_commit(&commit, repo, commit_id);
2893 err = got_object_id_str(&id_str, commit_id);
2895 err = got_error_from_errno("got_object_id_str");
2899 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2900 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2901 err = got_error_from_errno("fprintf");
2904 if (fprintf(outfile, "from: %s\n",
2905 got_object_commit_get_author(commit)) < 0) {
2906 err = got_error_from_errno("fprintf");
2909 committer_time = got_object_commit_get_committer_time(commit);
2910 datestr = get_datestr(&committer_time, datebuf);
2911 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2912 err = got_error_from_errno("fprintf");
2915 author = got_object_commit_get_author(commit);
2916 committer = got_object_commit_get_committer(commit);
2917 if (strcmp(author, committer) != 0 &&
2918 fprintf(outfile, "via: %s\n", committer) < 0) {
2919 err = got_error_from_errno("fprintf");
2922 err = got_object_commit_get_logmsg(&logmsg, commit);
2925 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2926 err = got_error_from_errno("fprintf");
2933 got_object_commit_close(commit);
2937 const struct got_error *
2938 get_filestream_info(size_t *filesize, int *nlines, off_t **line_offsets,
2944 size_t noffsets = 0;
2948 *line_offsets = NULL;
2954 if (fseek(infile, 0, SEEK_END) == -1)
2955 return got_error_from_errno("fseek");
2956 len = ftell(infile) + 1;
2958 return got_error_from_errno("ftell");
2959 if (fseek(infile, 0, SEEK_SET) == -1)
2960 return got_error_from_errno("fseek");
2964 if ((buf = calloc(len, sizeof(char *))) == NULL)
2965 return got_error_from_errno("calloc");
2967 fread(buf, 1, len, infile);
2969 return got_error_from_errno("fread");
2972 if (line_offsets && nlines) {
2973 if (*line_offsets == NULL) {
2974 /* Have some data but perhaps no '\n'. */
2977 *line_offsets = calloc(1, sizeof(**line_offsets));
2978 if (*line_offsets == NULL)
2979 return got_error_from_errno("calloc");
2980 /* Skip forward over end of first line. */
2987 /* Scan '\n' offsets in remaining chunk of data. */
2989 if (buf[i] != '\n') {
2994 if (noffsets < *nlines) {
2995 off_t *o = recallocarray(*line_offsets,
2997 sizeof(**line_offsets));
2999 free(*line_offsets);
3000 *line_offsets = NULL;
3001 return got_error_from_errno(
3008 (*line_offsets)[*nlines - 1] = off;
3013 if (fflush(infile) != 0)
3014 return got_error_from_errno("fflush");
3023 static const struct got_error *
3024 create_diff(struct tog_diff_view_state *s)
3026 const struct got_error *err = NULL;
3032 err = got_error_from_errno("got_opentemp");
3035 if (s->f && fclose(s->f) != 0) {
3036 err = got_error_from_errno("fclose");
3042 err = got_object_get_type(&obj_type, s->repo, s->id1);
3044 err = got_object_get_type(&obj_type, s->repo, s->id2);
3049 case GOT_OBJ_TYPE_BLOB:
3050 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
3051 s->diff_context, 0, s->repo, s->f);
3053 case GOT_OBJ_TYPE_TREE:
3054 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
3055 s->diff_context, 0, s->repo, s->f);
3057 case GOT_OBJ_TYPE_COMMIT: {
3058 const struct got_object_id_queue *parent_ids;
3059 struct got_object_qid *pid;
3060 struct got_commit_object *commit2;
3062 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3065 /* Show commit info if we're diffing to a parent/root commit. */
3066 if (s->id1 == NULL) {
3067 err =write_commit_info(s->id2, s->refs, s->repo, s->f);
3071 parent_ids = got_object_commit_get_parent_ids(commit2);
3072 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3073 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3074 err = write_commit_info(s->id2, s->refs,
3082 got_object_commit_close(commit2);
3084 err = got_diff_objects_as_commits(s->id1, s->id2,
3085 s->diff_context, 0, s->repo, s->f);
3089 err = got_error(GOT_ERR_OBJ_TYPE);
3094 err = get_filestream_info(&s->filesize, &s->nlines, &s->line_offsets,
3097 if (s->f && fflush(s->f) != 0 && err == NULL)
3098 err = got_error_from_errno("fflush");
3103 diff_view_indicate_progress(struct tog_view *view)
3105 mvwaddstr(view->window, 0, 0, "diffing...");
3110 static const struct got_error *
3111 search_start_diff_view(struct tog_view *view)
3113 struct tog_diff_view_state *s = &view->state.diff;
3115 s->matched_line = 0;
3119 static const struct got_error *
3120 search_next_diff_view(struct tog_view *view)
3122 struct tog_diff_view_state *s = &view->state.diff;
3125 if (!view->searching) {
3126 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3130 if (s->matched_line) {
3131 if (view->searching == TOG_SEARCH_FORWARD)
3132 lineno = s->matched_line + 1;
3134 lineno = s->matched_line - 1;
3136 if (view->searching == TOG_SEARCH_FORWARD)
3147 if (lineno <= 0 || lineno > s->nlines) {
3148 if (s->matched_line == 0) {
3149 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3154 if (view->searching == TOG_SEARCH_FORWARD)
3160 offset = s->line_offsets[lineno - 1];
3161 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3163 return got_error_from_errno("fseeko");
3166 line = parse_next_line(s->f, &len);
3167 if (line && match_line(line, &view->regex)) {
3168 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3169 s->matched_line = lineno;
3174 if (view->searching == TOG_SEARCH_FORWARD)
3180 if (s->matched_line) {
3181 s->first_displayed_line = s->matched_line;
3182 s->selected_line = 1;
3188 static const struct got_error *
3189 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3190 struct got_object_id *id2, struct tog_view *log_view,
3191 struct got_reflist_head *refs, struct got_repository *repo)
3193 const struct got_error *err;
3194 struct tog_diff_view_state *s = &view->state.diff;
3196 if (id1 != NULL && id2 != NULL) {
3198 err = got_object_get_type(&type1, repo, id1);
3201 err = got_object_get_type(&type2, repo, id2);
3206 return got_error(GOT_ERR_OBJ_TYPE);
3208 s->first_displayed_line = 1;
3209 s->last_displayed_line = view->nlines;
3210 s->selected_line = 1;
3217 s->id1 = got_object_id_dup(id1);
3219 return got_error_from_errno("got_object_id_dup");
3223 s->id2 = got_object_id_dup(id2);
3224 if (s->id2 == NULL) {
3227 return got_error_from_errno("got_object_id_dup");
3230 s->first_displayed_line = 1;
3231 s->last_displayed_line = view->nlines;
3232 s->diff_context = 3;
3233 s->log_view = log_view;
3237 SIMPLEQ_INIT(&s->colors);
3238 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3239 err = add_color(&s->colors,
3240 "^-", TOG_COLOR_DIFF_MINUS,
3241 get_color_value("TOG_COLOR_DIFF_MINUS"));
3244 err = add_color(&s->colors, "^\\+",
3245 TOG_COLOR_DIFF_PLUS,
3246 get_color_value("TOG_COLOR_DIFF_PLUS"));
3248 free_colors(&s->colors);
3251 err = add_color(&s->colors,
3252 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3253 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3255 free_colors(&s->colors);
3259 err = add_color(&s->colors,
3260 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
3261 get_color_value("TOG_COLOR_DIFF_META"));
3263 free_colors(&s->colors);
3267 err = add_color(&s->colors,
3268 "^(from|via): ", TOG_COLOR_AUTHOR,
3269 get_color_value("TOG_COLOR_AUTHOR"));
3271 free_colors(&s->colors);
3275 err = add_color(&s->colors,
3276 "^date: ", TOG_COLOR_DATE,
3277 get_color_value("TOG_COLOR_DATE"));
3279 free_colors(&s->colors);
3284 if (log_view && view_is_splitscreen(view))
3285 show_log_view(log_view); /* draw vborder */
3286 diff_view_indicate_progress(view);
3288 err = create_diff(s);
3297 view->show = show_diff_view;
3298 view->input = input_diff_view;
3299 view->close = close_diff_view;
3300 view->search_start = search_start_diff_view;
3301 view->search_next = search_next_diff_view;
3306 static const struct got_error *
3307 close_diff_view(struct tog_view *view)
3309 const struct got_error *err = NULL;
3310 struct tog_diff_view_state *s = &view->state.diff;
3316 if (s->f && fclose(s->f) == EOF)
3317 err = got_error_from_errno("fclose");
3318 free_colors(&s->colors);
3319 free(s->line_offsets);
3323 static const struct got_error *
3324 show_diff_view(struct tog_view *view)
3326 const struct got_error *err;
3327 struct tog_diff_view_state *s = &view->state.diff;
3328 char *id_str1 = NULL, *id_str2, *header;
3331 err = got_object_id_str(&id_str1, s->id1);
3335 err = got_object_id_str(&id_str2, s->id2);
3339 if (asprintf(&header, "diff %s %s",
3340 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3341 err = got_error_from_errno("asprintf");
3349 return draw_file(view, s->f, &s->first_displayed_line, s->nlines,
3350 s->selected_line, view->nlines, &s->last_displayed_line, &s->eof,
3351 header, &s->colors);
3354 static const struct got_error *
3355 set_selected_commit(struct tog_diff_view_state *s,
3356 struct commit_queue_entry *entry)
3358 const struct got_error *err;
3359 const struct got_object_id_queue *parent_ids;
3360 struct got_commit_object *selected_commit;
3361 struct got_object_qid *pid;
3364 s->id2 = got_object_id_dup(entry->id);
3366 return got_error_from_errno("got_object_id_dup");
3368 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3371 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3373 pid = SIMPLEQ_FIRST(parent_ids);
3374 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3375 got_object_commit_close(selected_commit);
3379 static const struct got_error *
3380 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3381 struct tog_view **focus_view, struct tog_view *view, int ch)
3383 const struct got_error *err = NULL;
3384 struct tog_diff_view_state *s = &view->state.diff;
3385 struct tog_log_view_state *ls;
3386 struct commit_queue_entry *entry;
3392 if (s->first_displayed_line > 1)
3393 s->first_displayed_line--;
3397 if (s->first_displayed_line == 1)
3400 while (i++ < view->nlines - 1 &&
3401 s->first_displayed_line > 1)
3402 s->first_displayed_line--;
3407 s->first_displayed_line++;
3415 while (!s->eof && i++ < view->nlines - 1) {
3417 line = parse_next_line(s->f, NULL);
3418 s->first_displayed_line++;
3424 if (s->diff_context > 0) {
3426 diff_view_indicate_progress(view);
3427 err = create_diff(s);
3431 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3433 diff_view_indicate_progress(view);
3434 err = create_diff(s);
3439 if (s->log_view == NULL)
3441 ls = &s->log_view->state.log;
3442 entry = TAILQ_PREV(ls->selected_entry,
3443 commit_queue_head, entry);
3447 err = input_log_view(NULL, NULL, NULL, s->log_view,
3452 err = set_selected_commit(s, entry);
3456 s->first_displayed_line = 1;
3457 s->last_displayed_line = view->nlines;
3459 diff_view_indicate_progress(view);
3460 err = create_diff(s);
3464 if (s->log_view == NULL)
3466 ls = &s->log_view->state.log;
3468 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3469 ls->thread_args.commits_needed++;
3470 err = trigger_log_thread(s->log_view, 1,
3471 &ls->thread_args.commits_needed,
3472 &ls->thread_args.log_complete,
3473 &ls->thread_args.need_commits,
3474 &ls->thread_args.commit_loaded);
3478 err = input_log_view(NULL, NULL, NULL, s->log_view,
3483 entry = TAILQ_NEXT(ls->selected_entry, entry);
3487 err = set_selected_commit(s, entry);
3491 s->first_displayed_line = 1;
3492 s->last_displayed_line = view->nlines;
3494 diff_view_indicate_progress(view);
3495 err = create_diff(s);
3504 static const struct got_error *
3505 cmd_diff(int argc, char *argv[])
3507 const struct got_error *error = NULL;
3508 struct got_repository *repo = NULL;
3509 struct got_worktree *worktree = NULL;
3510 struct got_reflist_head refs;
3511 struct got_object_id *id1 = NULL, *id2 = NULL;
3512 char *repo_path = NULL, *cwd = NULL;
3513 char *id_str1 = NULL, *id_str2 = NULL;
3515 struct tog_view *view;
3517 SIMPLEQ_INIT(&refs);
3520 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3525 while ((ch = getopt(argc, argv, "r:")) != -1) {
3528 repo_path = realpath(optarg, NULL);
3529 if (repo_path == NULL)
3530 return got_error_from_errno2("realpath",
3543 usage_diff(); /* TODO show local worktree changes */
3544 } else if (argc == 2) {
3550 cwd = getcwd(NULL, 0);
3552 return got_error_from_errno("getcwd");
3554 error = got_worktree_open(&worktree, cwd);
3555 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3558 if (repo_path == NULL) {
3561 strdup(got_worktree_get_repo_path(worktree));
3563 repo_path = strdup(cwd);
3565 if (repo_path == NULL) {
3566 error = got_error_from_errno("strdup");
3570 error = got_repo_open(&repo, repo_path, NULL);
3576 error = apply_unveil(got_repo_get_path(repo), NULL);
3580 error = got_repo_match_object_id_prefix(&id1, id_str1,
3581 GOT_OBJ_TYPE_ANY, repo);
3585 error = got_repo_match_object_id_prefix(&id2, id_str2,
3586 GOT_OBJ_TYPE_ANY, repo);
3590 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3594 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3596 error = got_error_from_errno("view_open");
3599 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3602 error = view_loop(view);
3607 got_repo_close(repo);
3609 got_worktree_close(worktree);
3610 got_ref_list_free(&refs);
3618 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3623 struct tog_blame_line {
3625 struct got_object_id *id;
3628 static const struct got_error *
3629 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3630 const char *path, struct tog_blame_line *lines, int nlines,
3631 int blame_complete, int selected_line, int *first_displayed_line,
3632 int *last_displayed_line, int *eof, int max_lines,
3633 struct tog_colors *colors)
3635 const struct got_error *err;
3636 int lineno = 0, nprinted = 0;
3641 struct tog_blame_line *blame_line;
3642 struct got_object_id *prev_id = NULL;
3644 struct tog_color *tc;
3646 err = got_object_id_str(&id_str, id);
3651 werase(view->window);
3653 if (asprintf(&line, "commit %s", id_str) == -1) {
3654 err = got_error_from_errno("asprintf");
3659 err = format_line(&wline, &width, line, view->ncols, 0);
3664 if (view_needs_focus_indication(view))
3665 wstandout(view->window);
3666 tc = get_color(colors, TOG_COLOR_COMMIT);
3668 wattr_on(view->window,
3669 COLOR_PAIR(tc->colorpair), NULL);
3670 waddwstr(view->window, wline);
3672 wattr_off(view->window,
3673 COLOR_PAIR(tc->colorpair), NULL);
3674 if (view_needs_focus_indication(view))
3675 wstandend(view->window);
3678 if (width < view->ncols - 1)
3679 waddch(view->window, '\n');
3681 if (asprintf(&line, "[%d/%d] %s%s",
3682 *first_displayed_line - 1 + selected_line, nlines,
3683 blame_complete ? "" : "annotating... ", path) == -1) {
3685 return got_error_from_errno("asprintf");
3688 err = format_line(&wline, &width, line, view->ncols, 0);
3693 waddwstr(view->window, wline);
3696 if (width < view->ncols - 1)
3697 waddch(view->window, '\n');
3700 while (nprinted < max_lines - 2) {
3701 line = parse_next_line(f, &len);
3706 if (++lineno < *first_displayed_line) {
3711 if (view->ncols <= 9) {
3713 wline = wcsdup(L"");
3715 err = got_error_from_errno("wcsdup");
3717 err = format_line(&wline, &width, line,
3718 view->ncols - 9, 9);
3726 if (view->focussed && nprinted == selected_line - 1)
3727 wstandout(view->window);
3730 blame_line = &lines[lineno - 1];
3731 if (blame_line->annotated && prev_id &&
3732 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3734 nprinted == selected_line - 1)) {
3735 waddstr(view->window, " ");
3736 } else if (blame_line->annotated) {
3738 err = got_object_id_str(&id_str, blame_line->id);
3744 tc = get_color(colors, TOG_COLOR_COMMIT);
3746 wattr_on(view->window,
3747 COLOR_PAIR(tc->colorpair), NULL);
3748 wprintw(view->window, "%.8s", id_str);
3750 wattr_off(view->window,
3751 COLOR_PAIR(tc->colorpair), NULL);
3753 prev_id = blame_line->id;
3755 waddstr(view->window, "........");
3759 waddstr(view->window, "........");
3763 if (view->focussed && nprinted == selected_line - 1)
3764 wstandend(view->window);
3765 waddstr(view->window, " ");
3767 waddwstr(view->window, wline);
3768 if (width <= view->ncols - 1)
3769 waddch(view->window, '\n');
3770 if (++nprinted == 1)
3771 *first_displayed_line = lineno;
3776 *last_displayed_line = lineno;
3783 static const struct got_error *
3784 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3786 const struct got_error *err = NULL;
3787 struct tog_blame_cb_args *a = arg;
3788 struct tog_blame_line *line;
3791 if (nlines != a->nlines ||
3792 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3793 return got_error(GOT_ERR_RANGE);
3795 errcode = pthread_mutex_lock(&tog_mutex);
3797 return got_error_set_errno(errcode, "pthread_mutex_lock");
3799 if (*a->quit) { /* user has quit the blame view */
3800 err = got_error(GOT_ERR_ITER_COMPLETED);
3805 goto done; /* no change in this commit */
3807 line = &a->lines[lineno - 1];
3808 if (line->annotated)
3811 line->id = got_object_id_dup(id);
3812 if (line->id == NULL) {
3813 err = got_error_from_errno("got_object_id_dup");
3816 line->annotated = 1;
3818 errcode = pthread_mutex_unlock(&tog_mutex);
3820 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3825 blame_thread(void *arg)
3827 const struct got_error *err;
3828 struct tog_blame_thread_args *ta = arg;
3829 struct tog_blame_cb_args *a = ta->cb_args;
3832 err = block_signals_used_by_main_thread();
3836 err = got_blame(ta->path, a->commit_id, ta->repo,
3837 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3838 if (err && err->code == GOT_ERR_CANCELLED)
3841 errcode = pthread_mutex_lock(&tog_mutex);
3843 return (void *)got_error_set_errno(errcode,
3844 "pthread_mutex_lock");
3846 got_repo_close(ta->repo);
3850 errcode = pthread_mutex_unlock(&tog_mutex);
3851 if (errcode && err == NULL)
3852 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3857 static struct got_object_id *
3858 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3859 int first_displayed_line, int selected_line)
3861 struct tog_blame_line *line;
3866 line = &lines[first_displayed_line - 1 + selected_line - 1];
3867 if (!line->annotated)
3873 static const struct got_error *
3874 stop_blame(struct tog_blame *blame)
3876 const struct got_error *err = NULL;
3879 if (blame->thread) {
3881 errcode = pthread_mutex_unlock(&tog_mutex);
3883 return got_error_set_errno(errcode,
3884 "pthread_mutex_unlock");
3885 errcode = pthread_join(blame->thread, (void **)&err);
3887 return got_error_set_errno(errcode, "pthread_join");
3888 errcode = pthread_mutex_lock(&tog_mutex);
3890 return got_error_set_errno(errcode,
3891 "pthread_mutex_lock");
3892 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3894 blame->thread = NULL;
3896 if (blame->thread_args.repo) {
3897 got_repo_close(blame->thread_args.repo);
3898 blame->thread_args.repo = NULL;
3901 if (fclose(blame->f) != 0 && err == NULL)
3902 err = got_error_from_errno("fclose");
3906 for (i = 0; i < blame->nlines; i++)
3907 free(blame->lines[i].id);
3909 blame->lines = NULL;
3911 free(blame->cb_args.commit_id);
3912 blame->cb_args.commit_id = NULL;
3917 static const struct got_error *
3918 cancel_blame_view(void *arg)
3920 const struct got_error *err = NULL;
3924 errcode = pthread_mutex_lock(&tog_mutex);
3926 return got_error_set_errno(errcode,
3927 "pthread_mutex_unlock");
3930 err = got_error(GOT_ERR_CANCELLED);
3932 errcode = pthread_mutex_unlock(&tog_mutex);
3934 return got_error_set_errno(errcode,
3935 "pthread_mutex_lock");
3940 static const struct got_error *
3941 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3942 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3943 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3944 struct got_repository *repo)
3946 const struct got_error *err = NULL;
3947 struct got_blob_object *blob = NULL;
3948 struct got_repository *thread_repo = NULL;
3949 struct got_object_id *obj_id = NULL;
3952 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3956 err = got_object_get_type(&obj_type, repo, obj_id);
3960 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3961 err = got_error(GOT_ERR_OBJ_TYPE);
3965 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3968 blame->f = got_opentemp();
3969 if (blame->f == NULL) {
3970 err = got_error_from_errno("got_opentemp");
3973 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3974 &blame->line_offsets, blame->f, blob);
3975 if (err || blame->nlines == 0)
3978 /* Don't include \n at EOF in the blame line count. */
3979 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3982 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3983 if (blame->lines == NULL) {
3984 err = got_error_from_errno("calloc");
3988 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3992 blame->cb_args.view = view;
3993 blame->cb_args.lines = blame->lines;
3994 blame->cb_args.nlines = blame->nlines;
3995 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3996 if (blame->cb_args.commit_id == NULL) {
3997 err = got_error_from_errno("got_object_id_dup");
4000 blame->cb_args.quit = done;
4002 blame->thread_args.path = path;
4003 blame->thread_args.repo = thread_repo;
4004 blame->thread_args.cb_args = &blame->cb_args;
4005 blame->thread_args.complete = blame_complete;
4006 blame->thread_args.cancel_cb = cancel_blame_view;
4007 blame->thread_args.cancel_arg = done;
4008 *blame_complete = 0;
4012 got_object_blob_close(blob);
4019 static const struct got_error *
4020 open_blame_view(struct tog_view *view, char *path,
4021 struct got_object_id *commit_id, struct got_reflist_head *refs,
4022 struct got_repository *repo)
4024 const struct got_error *err = NULL;
4025 struct tog_blame_view_state *s = &view->state.blame;
4027 SIMPLEQ_INIT(&s->blamed_commits);
4029 s->path = strdup(path);
4030 if (s->path == NULL)
4031 return got_error_from_errno("strdup");
4033 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4039 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4040 s->first_displayed_line = 1;
4041 s->last_displayed_line = view->nlines;
4042 s->selected_line = 1;
4043 s->blame_complete = 0;
4046 s->commit_id = commit_id;
4047 memset(&s->blame, 0, sizeof(s->blame));
4049 SIMPLEQ_INIT(&s->colors);
4050 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4051 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4052 get_color_value("TOG_COLOR_COMMIT"));
4057 view->show = show_blame_view;
4058 view->input = input_blame_view;
4059 view->close = close_blame_view;
4060 view->search_start = search_start_blame_view;
4061 view->search_next = search_next_blame_view;
4063 return run_blame(&s->blame, view, &s->blame_complete,
4064 &s->first_displayed_line, &s->last_displayed_line,
4065 &s->selected_line, &s->done, &s->eof, s->path,
4066 s->blamed_commit->id, s->repo);
4069 static const struct got_error *
4070 close_blame_view(struct tog_view *view)
4072 const struct got_error *err = NULL;
4073 struct tog_blame_view_state *s = &view->state.blame;
4075 if (s->blame.thread)
4076 err = stop_blame(&s->blame);
4078 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4079 struct got_object_qid *blamed_commit;
4080 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4081 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4082 got_object_qid_free(blamed_commit);
4086 free_colors(&s->colors);
4091 static const struct got_error *
4092 search_start_blame_view(struct tog_view *view)
4094 struct tog_blame_view_state *s = &view->state.blame;
4096 s->matched_line = 0;
4100 static const struct got_error *
4101 search_next_blame_view(struct tog_view *view)
4103 struct tog_blame_view_state *s = &view->state.blame;
4106 if (!view->searching) {
4107 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4111 if (s->matched_line) {
4112 if (view->searching == TOG_SEARCH_FORWARD)
4113 lineno = s->matched_line + 1;
4115 lineno = s->matched_line - 1;
4117 if (view->searching == TOG_SEARCH_FORWARD)
4120 lineno = s->blame.nlines;
4128 if (lineno <= 0 || lineno > s->blame.nlines) {
4129 if (s->matched_line == 0) {
4130 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4135 if (view->searching == TOG_SEARCH_FORWARD)
4138 lineno = s->blame.nlines;
4141 offset = s->blame.line_offsets[lineno - 1];
4142 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4144 return got_error_from_errno("fseeko");
4147 line = parse_next_line(s->blame.f, &len);
4148 if (line && match_line(line, &view->regex)) {
4149 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4150 s->matched_line = lineno;
4155 if (view->searching == TOG_SEARCH_FORWARD)
4161 if (s->matched_line) {
4162 s->first_displayed_line = s->matched_line;
4163 s->selected_line = 1;
4169 static const struct got_error *
4170 show_blame_view(struct tog_view *view)
4172 const struct got_error *err = NULL;
4173 struct tog_blame_view_state *s = &view->state.blame;
4176 if (s->blame.thread == NULL) {
4177 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4178 &s->blame.thread_args);
4180 return got_error_set_errno(errcode, "pthread_create");
4182 halfdelay(1); /* fast refresh while annotating */
4185 if (s->blame_complete)
4186 halfdelay(10); /* disable fast refresh */
4188 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4189 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4190 s->selected_line, &s->first_displayed_line,
4191 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
4197 static const struct got_error *
4198 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4199 struct tog_view **focus_view, struct tog_view *view, int ch)
4201 const struct got_error *err = NULL, *thread_err = NULL;
4202 struct tog_view *diff_view;
4203 struct tog_blame_view_state *s = &view->state.blame;
4212 if (s->selected_line > 1)
4214 else if (s->selected_line == 1 &&
4215 s->first_displayed_line > 1)
4216 s->first_displayed_line--;
4220 if (s->first_displayed_line == 1) {
4221 s->selected_line = 1;
4224 if (s->first_displayed_line > view->nlines - 2)
4225 s->first_displayed_line -=
4228 s->first_displayed_line = 1;
4232 if (s->selected_line < view->nlines - 2 &&
4233 s->first_displayed_line +
4234 s->selected_line <= s->blame.nlines)
4236 else if (s->last_displayed_line <
4238 s->first_displayed_line++;
4242 struct got_object_id *id = NULL;
4243 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4244 s->first_displayed_line, s->selected_line);
4248 struct got_commit_object *commit;
4249 struct got_object_qid *pid;
4250 struct got_object_id *blob_id = NULL;
4252 err = got_object_open_as_commit(&commit,
4256 pid = SIMPLEQ_FIRST(
4257 got_object_commit_get_parent_ids(commit));
4259 got_object_commit_close(commit);
4262 /* Check if path history ends here. */
4263 err = got_object_id_by_path(&blob_id, s->repo,
4266 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4268 got_object_commit_close(commit);
4271 err = got_object_get_type(&obj_type, s->repo,
4274 /* Can't blame non-blob type objects. */
4275 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4276 got_object_commit_close(commit);
4279 err = got_object_qid_alloc(&s->blamed_commit,
4281 got_object_commit_close(commit);
4283 if (got_object_id_cmp(id,
4284 s->blamed_commit->id) == 0)
4286 err = got_object_qid_alloc(&s->blamed_commit,
4292 thread_err = stop_blame(&s->blame);
4296 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4297 s->blamed_commit, entry);
4298 err = run_blame(&s->blame, view, &s->blame_complete,
4299 &s->first_displayed_line, &s->last_displayed_line,
4300 &s->selected_line, &s->done, &s->eof,
4301 s->path, s->blamed_commit->id, s->repo);
4307 struct got_object_qid *first;
4308 first = SIMPLEQ_FIRST(&s->blamed_commits);
4309 if (!got_object_id_cmp(first->id, s->commit_id))
4312 thread_err = stop_blame(&s->blame);
4316 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4317 got_object_qid_free(s->blamed_commit);
4319 SIMPLEQ_FIRST(&s->blamed_commits);
4320 err = run_blame(&s->blame, view, &s->blame_complete,
4321 &s->first_displayed_line, &s->last_displayed_line,
4322 &s->selected_line, &s->done, &s->eof, s->path,
4323 s->blamed_commit->id, s->repo);
4330 struct got_object_id *id = NULL;
4331 struct got_object_qid *pid;
4332 struct got_commit_object *commit = NULL;
4333 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4334 s->first_displayed_line, s->selected_line);
4337 err = got_object_open_as_commit(&commit, s->repo, id);
4340 pid = SIMPLEQ_FIRST(
4341 got_object_commit_get_parent_ids(commit));
4342 if (view_is_parent_view(view))
4343 begin_x = view_split_begin_x(view->begin_x);
4344 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4345 if (diff_view == NULL) {
4346 got_object_commit_close(commit);
4347 err = got_error_from_errno("view_open");
4350 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4351 id, NULL, s->refs, s->repo);
4352 got_object_commit_close(commit);
4354 view_close(diff_view);
4357 if (view_is_parent_view(view)) {
4358 err = view_close_child(view);
4361 err = view_set_child(view, diff_view);
4363 view_close(diff_view);
4366 *focus_view = diff_view;
4367 view->child_focussed = 1;
4369 *new_view = diff_view;
4377 if (s->last_displayed_line >= s->blame.nlines &&
4378 s->selected_line >= MIN(s->blame.nlines,
4379 view->nlines - 2)) {
4382 if (s->last_displayed_line >= s->blame.nlines &&
4383 s->selected_line < view->nlines - 2) {
4384 s->selected_line = MIN(s->blame.nlines,
4388 if (s->last_displayed_line + view->nlines - 2
4390 s->first_displayed_line +=
4393 s->first_displayed_line =
4398 if (s->selected_line > view->nlines - 2) {
4399 s->selected_line = MIN(s->blame.nlines,
4406 return thread_err ? thread_err : err;
4409 static const struct got_error *
4410 cmd_blame(int argc, char *argv[])
4412 const struct got_error *error;
4413 struct got_repository *repo = NULL;
4414 struct got_reflist_head refs;
4415 struct got_worktree *worktree = NULL;
4416 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4417 struct got_object_id *commit_id = NULL;
4418 char *commit_id_str = NULL;
4420 struct tog_view *view;
4422 SIMPLEQ_INIT(&refs);
4425 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4430 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4433 commit_id_str = optarg;
4436 repo_path = realpath(optarg, NULL);
4437 if (repo_path == NULL)
4438 return got_error_from_errno2("realpath",
4453 cwd = getcwd(NULL, 0);
4455 return got_error_from_errno("getcwd");
4457 error = got_worktree_open(&worktree, cwd);
4458 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4461 if (repo_path == NULL) {
4464 strdup(got_worktree_get_repo_path(worktree));
4466 repo_path = strdup(cwd);
4468 if (repo_path == NULL) {
4469 error = got_error_from_errno("strdup");
4473 error = got_repo_open(&repo, repo_path, NULL);
4477 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4484 error = apply_unveil(got_repo_get_path(repo), NULL);
4488 if (commit_id_str == NULL) {
4489 struct got_reference *head_ref;
4490 error = got_ref_open(&head_ref, repo, worktree ?
4491 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4494 error = got_ref_resolve(&commit_id, repo, head_ref);
4495 got_ref_close(head_ref);
4497 error = got_repo_match_object_id(&commit_id, NULL,
4498 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4503 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4507 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4509 error = got_error_from_errno("view_open");
4512 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4516 /* Release work tree lock. */
4517 got_worktree_close(worktree);
4520 error = view_loop(view);
4527 got_worktree_close(worktree);
4529 got_repo_close(repo);
4530 got_ref_list_free(&refs);
4534 static const struct got_error *
4535 draw_tree_entries(struct tog_view *view,
4536 struct got_tree_entry **first_displayed_entry,
4537 struct got_tree_entry **last_displayed_entry,
4538 struct got_tree_entry **selected_entry, int *ndisplayed,
4539 const char *label, int show_ids, const char *parent_path,
4540 struct got_tree_object *tree, int selected, int limit,
4541 int isroot, struct tog_colors *colors)
4543 const struct got_error *err = NULL;
4544 struct got_tree_entry *te;
4546 struct tog_color *tc;
4547 int width, n, i, nentries;
4551 werase(view->window);
4556 err = format_line(&wline, &width, label, view->ncols, 0);
4559 if (view_needs_focus_indication(view))
4560 wstandout(view->window);
4561 tc = get_color(colors, TOG_COLOR_COMMIT);
4563 wattr_on(view->window,
4564 COLOR_PAIR(tc->colorpair), NULL);
4565 waddwstr(view->window, wline);
4567 wattr_off(view->window,
4568 COLOR_PAIR(tc->colorpair), NULL);
4569 if (view_needs_focus_indication(view))
4570 wstandend(view->window);
4573 if (width < view->ncols - 1)
4574 waddch(view->window, '\n');
4577 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4580 waddwstr(view->window, wline);
4583 if (width < view->ncols - 1)
4584 waddch(view->window, '\n');
4587 waddch(view->window, '\n');
4591 if (*first_displayed_entry == NULL) {
4592 te = got_object_tree_get_first_entry(tree);
4593 if (selected == 0) {
4595 wstandout(view->window);
4596 *selected_entry = NULL;
4598 waddstr(view->window, " ..\n"); /* parent directory */
4599 if (selected == 0 && view->focussed)
4600 wstandend(view->window);
4607 te = *first_displayed_entry;
4610 nentries = got_object_tree_get_nentries(tree);
4611 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4612 char *line = NULL, *id_str = NULL;
4613 const char *modestr = "";
4616 te = got_object_tree_get_entry(tree, i);
4617 mode = got_tree_entry_get_mode(te);
4620 err = got_object_id_str(&id_str,
4621 got_tree_entry_get_id(te));
4623 return got_error_from_errno(
4624 "got_object_id_str");
4626 if (got_object_tree_entry_is_submodule(te))
4628 else if (S_ISLNK(mode))
4630 else if (S_ISDIR(mode))
4632 else if (mode & S_IXUSR)
4634 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4635 got_tree_entry_get_name(te), modestr) == -1) {
4637 return got_error_from_errno("asprintf");
4640 err = format_line(&wline, &width, line, view->ncols, 0);
4645 if (n == selected) {
4647 wstandout(view->window);
4648 *selected_entry = te;
4650 tc = match_color(colors, line);
4652 wattr_on(view->window,
4653 COLOR_PAIR(tc->colorpair), NULL);
4654 waddwstr(view->window, wline);
4656 wattr_off(view->window,
4657 COLOR_PAIR(tc->colorpair), NULL);
4658 if (width < view->ncols - 1)
4659 waddch(view->window, '\n');
4660 if (n == selected && view->focussed)
4661 wstandend(view->window);
4667 *last_displayed_entry = te;
4676 tree_scroll_up(struct tog_view *view,
4677 struct got_tree_entry **first_displayed_entry, int maxscroll,
4678 struct got_tree_object *tree, int isroot)
4680 struct got_tree_entry *te;
4683 if (*first_displayed_entry == NULL)
4686 te = got_object_tree_get_entry(tree, 0);
4687 if (*first_displayed_entry == te) {
4689 *first_displayed_entry = NULL;
4694 while (*first_displayed_entry && i < maxscroll) {
4695 *first_displayed_entry = got_tree_entry_get_prev(tree,
4696 *first_displayed_entry);
4699 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4700 *first_displayed_entry = NULL;
4704 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4705 struct got_tree_entry *last_displayed_entry,
4706 struct got_tree_object *tree)
4708 struct got_tree_entry *next, *last;
4711 if (*first_displayed_entry)
4712 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4714 next = got_object_tree_get_first_entry(tree);
4716 last = last_displayed_entry;
4717 while (next && last && n++ < maxscroll) {
4718 last = got_tree_entry_get_next(tree, last);
4720 *first_displayed_entry = next;
4721 next = got_tree_entry_get_next(tree, next);
4727 static const struct got_error *
4728 tree_entry_path(char **path, struct tog_parent_trees *parents,
4729 struct got_tree_entry *te)
4731 const struct got_error *err = NULL;
4732 struct tog_parent_tree *pt;
4733 size_t len = 2; /* for leading slash and NUL */
4735 TAILQ_FOREACH(pt, parents, entry)
4736 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4739 len += strlen(got_tree_entry_get_name(te));
4741 *path = calloc(1, len);
4743 return got_error_from_errno("calloc");
4746 pt = TAILQ_LAST(parents, tog_parent_trees);
4748 const char *name = got_tree_entry_get_name(pt->selected_entry);
4749 if (strlcat(*path, name, len) >= len) {
4750 err = got_error(GOT_ERR_NO_SPACE);
4753 if (strlcat(*path, "/", len) >= len) {
4754 err = got_error(GOT_ERR_NO_SPACE);
4757 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4760 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4761 err = got_error(GOT_ERR_NO_SPACE);
4773 static const struct got_error *
4774 blame_tree_entry(struct tog_view **new_view, int begin_x,
4775 struct got_tree_entry *te, struct tog_parent_trees *parents,
4776 struct got_object_id *commit_id, struct got_reflist_head *refs,
4777 struct got_repository *repo)
4779 const struct got_error *err = NULL;
4781 struct tog_view *blame_view;
4785 err = tree_entry_path(&path, parents, te);
4789 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4790 if (blame_view == NULL) {
4791 err = got_error_from_errno("view_open");
4795 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4797 if (err->code == GOT_ERR_CANCELLED)
4799 view_close(blame_view);
4801 *new_view = blame_view;
4807 static const struct got_error *
4808 log_tree_entry(struct tog_view **new_view, int begin_x,
4809 struct got_tree_entry *te, struct tog_parent_trees *parents,
4810 struct got_object_id *commit_id, struct got_reflist_head *refs,
4811 struct got_repository *repo)
4813 struct tog_view *log_view;
4814 const struct got_error *err = NULL;
4819 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4820 if (log_view == NULL)
4821 return got_error_from_errno("view_open");
4823 err = tree_entry_path(&path, parents, te);
4827 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4829 view_close(log_view);
4831 *new_view = log_view;
4836 static const struct got_error *
4837 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4838 struct got_object_id *commit_id, struct got_reflist_head *refs,
4839 struct got_repository *repo)
4841 const struct got_error *err = NULL;
4842 char *commit_id_str = NULL;
4843 struct tog_tree_view_state *s = &view->state.tree;
4845 TAILQ_INIT(&s->parents);
4847 err = got_object_id_str(&commit_id_str, commit_id);
4851 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4852 err = got_error_from_errno("asprintf");
4856 s->root = s->tree = root;
4857 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4858 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4859 s->commit_id = got_object_id_dup(commit_id);
4860 if (s->commit_id == NULL) {
4861 err = got_error_from_errno("got_object_id_dup");
4867 SIMPLEQ_INIT(&s->colors);
4869 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4870 err = add_color(&s->colors, "\\$$",
4871 TOG_COLOR_TREE_SUBMODULE,
4872 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4875 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4876 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4878 free_colors(&s->colors);
4881 err = add_color(&s->colors, "/$",
4882 TOG_COLOR_TREE_DIRECTORY,
4883 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4885 free_colors(&s->colors);
4889 err = add_color(&s->colors, "\\*$",
4890 TOG_COLOR_TREE_EXECUTABLE,
4891 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4893 free_colors(&s->colors);
4897 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4898 get_color_value("TOG_COLOR_COMMIT"));
4900 free_colors(&s->colors);
4905 view->show = show_tree_view;
4906 view->input = input_tree_view;
4907 view->close = close_tree_view;
4908 view->search_start = search_start_tree_view;
4909 view->search_next = search_next_tree_view;
4911 free(commit_id_str);
4913 free(s->tree_label);
4914 s->tree_label = NULL;
4919 static const struct got_error *
4920 close_tree_view(struct tog_view *view)
4922 struct tog_tree_view_state *s = &view->state.tree;
4924 free_colors(&s->colors);
4925 free(s->tree_label);
4926 s->tree_label = NULL;
4928 s->commit_id = NULL;
4929 while (!TAILQ_EMPTY(&s->parents)) {
4930 struct tog_parent_tree *parent;
4931 parent = TAILQ_FIRST(&s->parents);
4932 TAILQ_REMOVE(&s->parents, parent, entry);
4936 if (s->tree != s->root)
4937 got_object_tree_close(s->tree);
4938 got_object_tree_close(s->root);
4943 static const struct got_error *
4944 search_start_tree_view(struct tog_view *view)
4946 struct tog_tree_view_state *s = &view->state.tree;
4948 s->matched_entry = NULL;
4953 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4955 regmatch_t regmatch;
4957 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
4961 static const struct got_error *
4962 search_next_tree_view(struct tog_view *view)
4964 struct tog_tree_view_state *s = &view->state.tree;
4965 struct got_tree_entry *te = NULL;
4967 if (!view->searching) {
4968 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4972 if (s->matched_entry) {
4973 if (view->searching == TOG_SEARCH_FORWARD) {
4974 if (s->selected_entry)
4975 te = got_tree_entry_get_next(s->tree,
4978 te = got_object_tree_get_first_entry(s->tree);
4980 if (s->selected_entry == NULL)
4981 te = got_object_tree_get_last_entry(s->tree);
4983 te = got_tree_entry_get_prev(s->tree,
4987 if (view->searching == TOG_SEARCH_FORWARD)
4988 te = got_object_tree_get_first_entry(s->tree);
4990 te = got_object_tree_get_last_entry(s->tree);
4995 if (s->matched_entry == NULL) {
4996 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4999 if (view->searching == TOG_SEARCH_FORWARD)
5000 te = got_object_tree_get_first_entry(s->tree);
5002 te = got_object_tree_get_last_entry(s->tree);
5005 if (match_tree_entry(te, &view->regex)) {
5006 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5007 s->matched_entry = te;
5011 if (view->searching == TOG_SEARCH_FORWARD)
5012 te = got_tree_entry_get_next(s->tree, te);
5014 te = got_tree_entry_get_prev(s->tree, te);
5017 if (s->matched_entry) {
5018 s->first_displayed_entry = s->matched_entry;
5025 static const struct got_error *
5026 show_tree_view(struct tog_view *view)
5028 const struct got_error *err = NULL;
5029 struct tog_tree_view_state *s = &view->state.tree;
5032 err = tree_entry_path(&parent_path, &s->parents, NULL);
5036 err = draw_tree_entries(view, &s->first_displayed_entry,
5037 &s->last_displayed_entry, &s->selected_entry,
5038 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5039 s->tree, s->selected, view->nlines, s->tree == s->root,
5047 static const struct got_error *
5048 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5049 struct tog_view **focus_view, struct tog_view *view, int ch)
5051 const struct got_error *err = NULL;
5052 struct tog_tree_view_state *s = &view->state.tree;
5053 struct tog_view *log_view;
5054 int begin_x = 0, nscrolled;
5058 s->show_ids = !s->show_ids;
5061 if (!s->selected_entry)
5063 if (view_is_parent_view(view))
5064 begin_x = view_split_begin_x(view->begin_x);
5065 err = log_tree_entry(&log_view, begin_x,
5066 s->selected_entry, &s->parents,
5067 s->commit_id, s->refs, s->repo);
5068 if (view_is_parent_view(view)) {
5069 err = view_close_child(view);
5072 err = view_set_child(view, log_view);
5074 view_close(log_view);
5077 *focus_view = log_view;
5078 view->child_focussed = 1;
5080 *new_view = log_view;
5084 if (s->selected > 0) {
5086 if (s->selected == 0)
5089 if (s->selected > 0)
5091 tree_scroll_up(view, &s->first_displayed_entry, 1,
5092 s->tree, s->tree == s->root);
5096 tree_scroll_up(view, &s->first_displayed_entry,
5097 MAX(0, view->nlines - 4 - s->selected), s->tree,
5098 s->tree == s->root);
5100 if (got_object_tree_get_first_entry(s->tree) ==
5101 s->first_displayed_entry && s->tree != s->root)
5102 s->first_displayed_entry = NULL;
5106 if (s->selected < s->ndisplayed - 1) {
5110 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5112 /* can't scroll any further */
5114 tree_scroll_down(&s->first_displayed_entry, 1,
5115 s->last_displayed_entry, s->tree);
5119 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5121 /* can't scroll any further; move cursor down */
5122 if (s->selected < s->ndisplayed - 1)
5123 s->selected = s->ndisplayed - 1;
5126 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5127 view->nlines, s->last_displayed_entry, s->tree);
5128 if (nscrolled < view->nlines) {
5130 struct got_tree_entry *te;
5131 te = s->first_displayed_entry;
5134 te = got_tree_entry_get_next(s->tree, te);
5136 s->selected = ndisplayed - 1;
5142 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5143 struct tog_parent_tree *parent;
5144 /* user selected '..' */
5145 if (s->tree == s->root)
5147 parent = TAILQ_FIRST(&s->parents);
5148 TAILQ_REMOVE(&s->parents, parent,
5150 got_object_tree_close(s->tree);
5151 s->tree = parent->tree;
5152 s->first_displayed_entry =
5153 parent->first_displayed_entry;
5155 parent->selected_entry;
5156 s->selected = parent->selected;
5158 } else if (S_ISDIR(got_tree_entry_get_mode(
5159 s->selected_entry))) {
5160 struct got_tree_object *subtree;
5161 err = got_object_open_as_tree(&subtree, s->repo,
5162 got_tree_entry_get_id(s->selected_entry));
5165 err = tree_view_visit_subtree(subtree, s);
5167 got_object_tree_close(subtree);
5170 } else if (S_ISREG(got_tree_entry_get_mode(
5171 s->selected_entry))) {
5172 struct tog_view *blame_view;
5173 int begin_x = view_is_parent_view(view) ?
5174 view_split_begin_x(view->begin_x) : 0;
5176 err = blame_tree_entry(&blame_view, begin_x,
5177 s->selected_entry, &s->parents,
5178 s->commit_id, s->refs, s->repo);
5181 if (view_is_parent_view(view)) {
5182 err = view_close_child(view);
5185 err = view_set_child(view, blame_view);
5187 view_close(blame_view);
5190 *focus_view = blame_view;
5191 view->child_focussed = 1;
5193 *new_view = blame_view;
5197 if (s->selected > view->nlines)
5198 s->selected = s->ndisplayed - 1;
5211 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5216 static const struct got_error *
5217 cmd_tree(int argc, char *argv[])
5219 const struct got_error *error;
5220 struct got_repository *repo = NULL;
5221 struct got_worktree *worktree = NULL;
5222 struct got_reflist_head refs;
5223 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5224 struct got_object_id *commit_id = NULL;
5225 char *commit_id_arg = NULL;
5226 struct got_commit_object *commit = NULL;
5227 struct got_tree_object *tree = NULL;
5229 struct tog_view *view;
5231 SIMPLEQ_INIT(&refs);
5234 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5239 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5242 commit_id_arg = optarg;
5245 repo_path = realpath(optarg, NULL);
5246 if (repo_path == NULL)
5247 return got_error_from_errno2("realpath",
5262 cwd = getcwd(NULL, 0);
5264 return got_error_from_errno("getcwd");
5266 error = got_worktree_open(&worktree, cwd);
5267 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5270 if (repo_path == NULL) {
5273 strdup(got_worktree_get_repo_path(worktree));
5275 repo_path = strdup(cwd);
5277 if (repo_path == NULL) {
5278 error = got_error_from_errno("strdup");
5282 error = got_repo_open(&repo, repo_path, NULL);
5286 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5293 error = apply_unveil(got_repo_get_path(repo), NULL);
5297 error = got_repo_match_object_id(&commit_id, NULL,
5298 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5299 GOT_OBJ_TYPE_COMMIT, 1, repo);
5303 error = got_object_open_as_commit(&commit, repo, commit_id);
5307 error = got_object_open_as_tree(&tree, repo,
5308 got_object_commit_get_tree_id(commit));
5312 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5316 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5318 error = got_error_from_errno("view_open");
5321 error = open_tree_view(view, tree, commit_id, &refs, repo);
5324 if (!got_path_is_root_dir(in_repo_path)) {
5325 error = tree_view_walk_path(&view->state.tree, commit_id,
5326 in_repo_path, repo);
5332 /* Release work tree lock. */
5333 got_worktree_close(worktree);
5336 error = view_loop(view);
5342 got_object_commit_close(commit);
5344 got_object_tree_close(tree);
5346 got_repo_close(repo);
5347 got_ref_list_free(&refs);
5356 fprintf(stderr, "commands:");
5357 for (i = 0; i < nitems(tog_commands); i++) {
5358 struct tog_cmd *cmd = &tog_commands[i];
5359 fprintf(stderr, " %s", cmd->name);
5361 fputc('\n', stderr);
5367 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] "
5368 "[arg ...]\n", getprogname());
5370 fprintf(stderr, "lazy usage: %s path\n", getprogname());
5377 make_argv(int argc, ...)
5385 argv = calloc(argc, sizeof(char *));
5388 for (i = 0; i < argc; i++) {
5389 argv[i] = strdup(va_arg(ap, char *));
5390 if (argv[i] == NULL)
5399 * Try to convert 'tog path' into a 'tog log path' command.
5400 * The user could simply have mistyped the command rather than knowingly
5401 * provided a path. So check whether argv[0] can in fact be resolved
5402 * to a path in the HEAD commit and print a special error if not.
5403 * This hack is for mpi@ <3
5405 static const struct got_error *
5406 tog_log_with_path(int argc, char *argv[])
5408 const struct got_error *error = NULL;
5409 struct tog_cmd *cmd = NULL;
5410 struct got_repository *repo = NULL;
5411 struct got_worktree *worktree = NULL;
5412 struct got_object_id *commit_id = NULL, *id = NULL;
5413 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5414 char *commit_id_str = NULL, **cmd_argv = NULL;
5416 cwd = getcwd(NULL, 0);
5418 return got_error_from_errno("getcwd");
5420 error = got_worktree_open(&worktree, cwd);
5421 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5425 repo_path = strdup(got_worktree_get_repo_path(worktree));
5427 repo_path = strdup(cwd);
5428 if (repo_path == NULL) {
5429 error = got_error_from_errno("strdup");
5433 error = got_repo_open(&repo, repo_path, NULL);
5437 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5442 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
5443 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
5444 GOT_OBJ_TYPE_COMMIT, 1, repo);
5449 got_worktree_close(worktree);
5453 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
5455 if (error->code != GOT_ERR_NO_TREE_ENTRY)
5457 fprintf(stderr, "%s: '%s' is no known command or path\n",
5458 getprogname(), argv[0]);
5463 got_repo_close(repo);
5466 error = got_object_id_str(&commit_id_str, commit_id);
5470 cmd = &tog_commands[0]; /* log */
5472 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
5473 error = cmd->cmd_main(argc, cmd_argv);
5476 got_repo_close(repo);
5478 got_worktree_close(worktree);
5480 free(commit_id_str);
5487 for (i = 0; i < argc; i++)
5495 main(int argc, char *argv[])
5497 const struct got_error *error = NULL;
5498 struct tog_cmd *cmd = NULL;
5499 int ch, hflag = 0, Vflag = 0;
5500 char **cmd_argv = NULL;
5501 static struct option longopts[] = {
5502 { "version", no_argument, NULL, 'V' },
5506 setlocale(LC_CTYPE, "");
5508 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5528 got_version_print_str();
5535 /* Build an argument vector which runs a default command. */
5536 cmd = &tog_commands[0];
5538 cmd_argv = make_argv(argc, cmd->name);
5542 /* Did the user specify a command? */
5543 for (i = 0; i < nitems(tog_commands); i++) {
5544 if (strncmp(tog_commands[i].name, argv[0],
5545 strlen(argv[0])) == 0) {
5546 cmd = &tog_commands[i];
5555 /* No command specified; try log with a path */
5556 error = tog_log_with_path(argc, argv);
5561 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5567 for (i = 0; i < argc; i++)
5572 if (error && error->code != GOT_ERR_CANCELLED)
5573 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);