2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED
25 #undef _XOPEN_SOURCE_EXTENDED
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_opentemp.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
57 #include "got_worktree.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 #define CTRL(x) ((x) & 0x1f)
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
79 __dead static void usage(int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
85 static const struct got_error* cmd_log(int, char *[]);
86 static const struct got_error* cmd_diff(int, char *[]);
87 static const struct got_error* cmd_blame(int, char *[]);
88 static const struct got_error* cmd_tree(int, char *[]);
90 static struct tog_cmd tog_commands[] = {
91 { "log", cmd_log, usage_log },
92 { "diff", cmd_diff, usage_diff },
93 { "blame", cmd_blame, usage_blame },
94 { "tree", cmd_tree, usage_tree },
104 #define TOG_EOF_STRING "(END)"
106 struct commit_queue_entry {
107 TAILQ_ENTRY(commit_queue_entry) entry;
108 struct got_object_id *id;
109 struct got_commit_object *commit;
112 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
113 struct commit_queue {
115 struct commit_queue_head head;
119 SIMPLEQ_ENTRY(tog_color) entry;
123 SIMPLEQ_HEAD(tog_colors, tog_color);
125 static const struct got_error *
126 add_color(struct tog_colors *colors, const char *pattern,
127 int idx, short color)
129 const struct got_error *err = NULL;
130 struct tog_color *tc;
133 if (idx < 1 || idx > COLOR_PAIRS - 1)
136 init_pair(idx, color, -1);
138 tc = calloc(1, sizeof(*tc));
140 return got_error_from_errno("calloc");
141 regerr = regcomp(&tc->regex, pattern,
142 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
144 static char regerr_msg[512];
145 static char err_msg[512];
146 regerror(regerr, &tc->regex, regerr_msg,
148 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
150 err = got_error_msg(GOT_ERR_REGEX, err_msg);
155 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
160 free_colors(struct tog_colors *colors)
162 struct tog_color *tc;
164 while (!SIMPLEQ_EMPTY(colors)) {
165 tc = SIMPLEQ_FIRST(colors);
166 SIMPLEQ_REMOVE_HEAD(colors, entry);
173 get_color(struct tog_colors *colors, int colorpair)
175 struct tog_color *tc = NULL;
177 SIMPLEQ_FOREACH(tc, colors, entry) {
178 if (tc->colorpair == colorpair)
186 default_color_value(const char *envvar)
188 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
189 return COLOR_MAGENTA;
190 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
192 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
194 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
196 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
197 return COLOR_MAGENTA;
198 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
199 return COLOR_MAGENTA;
200 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
202 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
204 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
206 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
208 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
215 get_color_value(const char *envvar)
217 const char *val = getenv(envvar);
220 return default_color_value(envvar);
222 if (strcasecmp(val, "black") == 0)
224 if (strcasecmp(val, "red") == 0)
226 if (strcasecmp(val, "green") == 0)
228 if (strcasecmp(val, "yellow") == 0)
230 if (strcasecmp(val, "blue") == 0)
232 if (strcasecmp(val, "magenta") == 0)
233 return COLOR_MAGENTA;
234 if (strcasecmp(val, "cyan") == 0)
236 if (strcasecmp(val, "white") == 0)
238 if (strcasecmp(val, "default") == 0)
241 return default_color_value(envvar);
245 struct tog_diff_view_state {
246 struct got_object_id *id1, *id2;
248 int first_displayed_line;
249 int last_displayed_line;
252 struct got_repository *repo;
253 struct got_reflist_head *refs;
254 struct tog_colors colors;
261 /* passed from log view; may be NULL */
262 struct tog_view *log_view;
265 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
267 struct tog_log_thread_args {
268 pthread_cond_t need_commits;
269 pthread_cond_t commit_loaded;
271 struct got_commit_graph *graph;
272 struct commit_queue *commits;
273 const char *in_repo_path;
274 struct got_object_id *start_id;
275 struct got_repository *repo;
278 struct commit_queue_entry **first_displayed_entry;
279 struct commit_queue_entry **selected_entry;
281 int *search_next_done;
285 struct tog_log_view_state {
286 struct commit_queue commits;
287 struct commit_queue_entry *first_displayed_entry;
288 struct commit_queue_entry *last_displayed_entry;
289 struct commit_queue_entry *selected_entry;
292 const char *head_ref_name;
294 struct got_repository *repo;
295 struct got_reflist_head *refs;
296 struct got_object_id *start_id;
299 struct tog_log_thread_args thread_args;
300 struct commit_queue_entry *matched_entry;
301 struct commit_queue_entry *search_entry;
302 struct tog_colors colors;
305 #define TOG_COLOR_DIFF_MINUS 1
306 #define TOG_COLOR_DIFF_PLUS 2
307 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
308 #define TOG_COLOR_DIFF_META 4
309 #define TOG_COLOR_TREE_SUBMODULE 5
310 #define TOG_COLOR_TREE_SYMLINK 6
311 #define TOG_COLOR_TREE_DIRECTORY 7
312 #define TOG_COLOR_TREE_EXECUTABLE 8
313 #define TOG_COLOR_COMMIT 9
314 #define TOG_COLOR_AUTHOR 10
315 #define TOG_COLOR_DATE 11
317 struct tog_blame_cb_args {
318 struct tog_blame_line *lines; /* one per line */
321 struct tog_view *view;
322 struct got_object_id *commit_id;
326 struct tog_blame_thread_args {
328 struct got_repository *repo;
329 struct tog_blame_cb_args *cb_args;
331 got_cancel_cb cancel_cb;
338 struct tog_blame_line *lines;
342 struct tog_blame_thread_args thread_args;
343 struct tog_blame_cb_args cb_args;
347 struct tog_blame_view_state {
348 int first_displayed_line;
349 int last_displayed_line;
354 struct got_object_id_queue blamed_commits;
355 struct got_object_qid *blamed_commit;
357 struct got_repository *repo;
358 struct got_reflist_head *refs;
359 struct got_object_id *commit_id;
360 struct tog_blame blame;
362 struct tog_colors colors;
365 struct tog_parent_tree {
366 TAILQ_ENTRY(tog_parent_tree) entry;
367 struct got_tree_object *tree;
368 struct got_tree_entry *first_displayed_entry;
369 struct got_tree_entry *selected_entry;
373 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
375 struct tog_tree_view_state {
377 struct got_tree_object *root;
378 struct got_tree_object *tree;
379 struct got_tree_entry *first_displayed_entry;
380 struct got_tree_entry *last_displayed_entry;
381 struct got_tree_entry *selected_entry;
382 int ndisplayed, selected, show_ids;
383 struct tog_parent_trees parents;
384 struct got_object_id *commit_id;
385 struct got_repository *repo;
386 struct got_reflist_head *refs;
387 struct got_tree_entry *matched_entry;
388 struct tog_colors colors;
392 * We implement two types of views: parent views and child views.
394 * The 'Tab' key switches between a parent view and its child view.
395 * Child views are shown side-by-side to their parent view, provided
396 * there is enough screen estate.
398 * When a new view is opened from within a parent view, this new view
399 * becomes a child view of the parent view, replacing any existing child.
401 * When a new view is opened from within a child view, this new view
402 * becomes a parent view which will obscure the views below until the
403 * user quits the new parent view by typing 'q'.
405 * This list of views contains parent views only.
406 * Child views are only pointed to by their parent view.
408 TAILQ_HEAD(tog_view_list_head, tog_view);
411 TAILQ_ENTRY(tog_view) entry;
414 int nlines, ncols, begin_y, begin_x;
415 int lines, cols; /* copies of LINES and COLS */
417 struct tog_view *parent;
418 struct tog_view *child;
421 /* type-specific state */
422 enum tog_view_type type;
424 struct tog_diff_view_state diff;
425 struct tog_log_view_state log;
426 struct tog_blame_view_state blame;
427 struct tog_tree_view_state tree;
430 const struct got_error *(*show)(struct tog_view *);
431 const struct got_error *(*input)(struct tog_view **,
432 struct tog_view **, struct tog_view**, struct tog_view *, int);
433 const struct got_error *(*close)(struct tog_view *);
435 const struct got_error *(*search_start)(struct tog_view *);
436 const struct got_error *(*search_next)(struct tog_view *);
438 #define TOG_SEARCH_FORWARD 1
439 #define TOG_SEARCH_BACKWARD 2
440 int search_next_done;
441 #define TOG_SEARCH_HAVE_MORE 1
442 #define TOG_SEARCH_NO_MORE 2
443 #define TOG_SEARCH_HAVE_NONE 3
447 static const struct got_error *open_diff_view(struct tog_view *,
448 struct got_object_id *, struct got_object_id *, struct tog_view *,
449 struct got_reflist_head *, struct got_repository *);
450 static const struct got_error *show_diff_view(struct tog_view *);
451 static const struct got_error *input_diff_view(struct tog_view **,
452 struct tog_view **, struct tog_view **, struct tog_view *, int);
453 static const struct got_error* close_diff_view(struct tog_view *);
454 static const struct got_error *search_start_diff_view(struct tog_view *);
455 static const struct got_error *search_next_diff_view(struct tog_view *);
457 static const struct got_error *open_log_view(struct tog_view *,
458 struct got_object_id *, struct got_reflist_head *,
459 struct got_repository *, const char *, const char *, int);
460 static const struct got_error * show_log_view(struct tog_view *);
461 static const struct got_error *input_log_view(struct tog_view **,
462 struct tog_view **, struct tog_view **, struct tog_view *, int);
463 static const struct got_error *close_log_view(struct tog_view *);
464 static const struct got_error *search_start_log_view(struct tog_view *);
465 static const struct got_error *search_next_log_view(struct tog_view *);
467 static const struct got_error *open_blame_view(struct tog_view *, char *,
468 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
469 static const struct got_error *show_blame_view(struct tog_view *);
470 static const struct got_error *input_blame_view(struct tog_view **,
471 struct tog_view **, struct tog_view **, struct tog_view *, int);
472 static const struct got_error *close_blame_view(struct tog_view *);
473 static const struct got_error *search_start_blame_view(struct tog_view *);
474 static const struct got_error *search_next_blame_view(struct tog_view *);
476 static const struct got_error *open_tree_view(struct tog_view *,
477 struct got_tree_object *, struct got_object_id *, struct got_reflist_head *,
478 struct got_repository *);
479 static const struct got_error *show_tree_view(struct tog_view *);
480 static const struct got_error *input_tree_view(struct tog_view **,
481 struct tog_view **, struct tog_view **, struct tog_view *, int);
482 static const struct got_error *close_tree_view(struct tog_view *);
483 static const struct got_error *search_start_tree_view(struct tog_view *);
484 static const struct got_error *search_next_tree_view(struct tog_view *);
486 static volatile sig_atomic_t tog_sigwinch_received;
487 static volatile sig_atomic_t tog_sigpipe_received;
488 static volatile sig_atomic_t tog_sigcont_received;
491 tog_sigwinch(int signo)
493 tog_sigwinch_received = 1;
497 tog_sigpipe(int signo)
499 tog_sigpipe_received = 1;
503 tog_sigcont(int signo)
505 tog_sigcont_received = 1;
508 static const struct got_error *
509 view_close(struct tog_view *view)
511 const struct got_error *err = NULL;
514 view_close(view->child);
518 err = view->close(view);
520 del_panel(view->panel);
522 delwin(view->window);
527 static struct tog_view *
528 view_open(int nlines, int ncols, int begin_y, int begin_x,
529 enum tog_view_type type)
531 struct tog_view *view = calloc(1, sizeof(*view));
539 view->nlines = nlines ? nlines : LINES - begin_y;
540 view->ncols = ncols ? ncols : COLS - begin_x;
541 view->begin_y = begin_y;
542 view->begin_x = begin_x;
543 view->window = newwin(nlines, ncols, begin_y, begin_x);
544 if (view->window == NULL) {
548 view->panel = new_panel(view->window);
549 if (view->panel == NULL ||
550 set_panel_userptr(view->panel, view) != OK) {
555 keypad(view->window, TRUE);
560 view_split_begin_x(int begin_x)
562 if (begin_x > 0 || COLS < 120)
564 return (COLS - MAX(COLS / 2, 80));
567 static const struct got_error *view_resize(struct tog_view *);
569 static const struct got_error *
570 view_splitscreen(struct tog_view *view)
572 const struct got_error *err = NULL;
575 view->begin_x = view_split_begin_x(0);
576 view->nlines = LINES;
577 view->ncols = COLS - view->begin_x;
580 err = view_resize(view);
584 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
585 return got_error_from_errno("mvwin");
590 static const struct got_error *
591 view_fullscreen(struct tog_view *view)
593 const struct got_error *err = NULL;
597 view->nlines = LINES;
601 err = view_resize(view);
605 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
606 return got_error_from_errno("mvwin");
612 view_is_parent_view(struct tog_view *view)
614 return view->parent == NULL;
617 static const struct got_error *
618 view_resize(struct tog_view *view)
622 if (view->lines > LINES)
623 nlines = view->nlines - (view->lines - LINES);
625 nlines = view->nlines + (LINES - view->lines);
627 if (view->cols > COLS)
628 ncols = view->ncols - (view->cols - COLS);
630 ncols = view->ncols + (COLS - view->cols);
632 if (wresize(view->window, nlines, ncols) == ERR)
633 return got_error_from_errno("wresize");
634 if (replace_panel(view->panel, view->window) == ERR)
635 return got_error_from_errno("replace_panel");
636 wclear(view->window);
638 view->nlines = nlines;
644 view->child->begin_x = view_split_begin_x(view->begin_x);
645 if (view->child->begin_x == 0) {
646 view_fullscreen(view->child);
647 if (view->child->focussed)
648 show_panel(view->child->panel);
650 show_panel(view->panel);
652 view_splitscreen(view->child);
653 show_panel(view->child->panel);
660 static const struct got_error *
661 view_close_child(struct tog_view *view)
663 const struct got_error *err = NULL;
665 if (view->child == NULL)
668 err = view_close(view->child);
673 static const struct got_error *
674 view_set_child(struct tog_view *view, struct tog_view *child)
676 const struct got_error *err = NULL;
679 child->parent = view;
684 view_is_splitscreen(struct tog_view *view)
686 return view->begin_x > 0;
695 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
696 cols = 80; /* Default */
702 resize_term(lines, cols);
705 static const struct got_error *
706 view_search_start(struct tog_view *view)
708 const struct got_error *err = NULL;
712 if (view->nlines < 1)
715 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
716 wclrtoeol(view->window);
720 ret = wgetnstr(view->window, pattern, sizeof(pattern));
726 if (view->searching) {
727 regfree(&view->regex);
731 if (regcomp(&view->regex, pattern,
732 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
733 err = view->search_start(view);
735 regfree(&view->regex);
738 view->searching = TOG_SEARCH_FORWARD;
739 view->search_next_done = 0;
740 view->search_next(view);
746 static const struct got_error *
747 view_input(struct tog_view **new, struct tog_view **dead,
748 struct tog_view **focus, int *done, struct tog_view *view,
749 struct tog_view_list_head *views)
751 const struct got_error *err = NULL;
759 /* Clear "no matches" indicator. */
760 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
761 view->search_next_done == TOG_SEARCH_HAVE_NONE)
762 view->search_next_done = TOG_SEARCH_HAVE_MORE;
764 if (view->searching && !view->search_next_done) {
765 errcode = pthread_mutex_unlock(&tog_mutex);
767 return got_error_set_errno(errcode,
768 "pthread_mutex_unlock");
770 errcode = pthread_mutex_lock(&tog_mutex);
772 return got_error_set_errno(errcode,
773 "pthread_mutex_lock");
774 view->search_next(view);
778 nodelay(stdscr, FALSE);
779 /* Allow threads to make progress while we are waiting for input. */
780 errcode = pthread_mutex_unlock(&tog_mutex);
782 return got_error_set_errno(errcode, "pthread_mutex_unlock");
783 ch = wgetch(view->window);
784 errcode = pthread_mutex_lock(&tog_mutex);
786 return got_error_set_errno(errcode, "pthread_mutex_lock");
787 nodelay(stdscr, TRUE);
789 if (tog_sigwinch_received || tog_sigcont_received) {
791 tog_sigwinch_received = 0;
792 tog_sigcont_received = 0;
793 TAILQ_FOREACH(v, views, entry) {
794 err = view_resize(v);
797 err = v->input(new, dead, focus, v, KEY_RESIZE);
808 *focus = view->child;
809 view->child_focussed = 1;
810 } else if (view->parent) {
811 *focus = view->parent;
812 view->parent->child_focussed = 0;
816 err = view->input(new, dead, focus, view, ch);
823 if (view_is_parent_view(view)) {
824 if (view->child == NULL)
826 if (view_is_splitscreen(view->child)) {
827 *focus = view->child;
828 view->child_focussed = 1;
829 err = view_fullscreen(view->child);
831 err = view_splitscreen(view->child);
834 err = view->child->input(new, dead, focus,
835 view->child, KEY_RESIZE);
837 if (view_is_splitscreen(view)) {
839 view->parent->child_focussed = 1;
840 err = view_fullscreen(view);
842 err = view_splitscreen(view);
846 err = view->input(new, dead, focus, view,
853 if (view->search_start)
854 view_search_start(view);
856 err = view->input(new, dead, focus, view, ch);
860 if (view->search_next) {
861 view->searching = (ch == 'n' ?
862 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
863 view->search_next_done = 0;
864 view->search_next(view);
866 err = view->input(new, dead, focus, view, ch);
869 err = view->input(new, dead, focus, view, ch);
877 view_vborder(struct tog_view *view)
880 struct tog_view *view_above;
883 return view_vborder(view->parent);
885 panel = panel_above(view->panel);
889 view_above = panel_userptr(panel);
890 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
891 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
895 view_needs_focus_indication(struct tog_view *view)
897 if (view_is_parent_view(view)) {
898 if (view->child == NULL || view->child_focussed)
900 if (!view_is_splitscreen(view->child))
902 } else if (!view_is_splitscreen(view))
905 return view->focussed;
908 static const struct got_error *
909 view_loop(struct tog_view *view)
911 const struct got_error *err = NULL;
912 struct tog_view_list_head views;
913 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
914 int fast_refresh = 10;
915 int done = 0, errcode;
917 errcode = pthread_mutex_lock(&tog_mutex);
919 return got_error_set_errno(errcode, "pthread_mutex_lock");
922 TAILQ_INSERT_HEAD(&views, view, entry);
926 err = view->show(view);
931 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
932 /* Refresh fast during initialization, then become slower. */
933 if (fast_refresh && fast_refresh-- == 0)
934 halfdelay(10); /* switch to once per second */
936 err = view_input(&new_view, &dead_view, &focus_view, &done,
941 struct tog_view *prev = NULL;
943 if (view_is_parent_view(dead_view))
944 prev = TAILQ_PREV(dead_view,
945 tog_view_list_head, entry);
946 else if (view->parent != dead_view)
949 if (dead_view->parent)
950 dead_view->parent->child = NULL;
952 TAILQ_REMOVE(&views, dead_view, entry);
954 err = view_close(dead_view);
955 if (err || (dead_view == main_view && new_view == NULL))
958 if (view == dead_view) {
963 else if (!TAILQ_EMPTY(&views))
964 view = TAILQ_LAST(&views,
969 if (view->child && view->child_focussed)
970 focus_view = view->child;
977 struct tog_view *v, *t;
978 /* Only allow one parent view per type. */
979 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
980 if (v->type != new_view->type)
982 TAILQ_REMOVE(&views, v, entry);
988 TAILQ_INSERT_TAIL(&views, new_view, entry);
990 if (focus_view == NULL)
991 focus_view = new_view;
994 show_panel(focus_view->panel);
997 focus_view->focussed = 1;
1000 show_panel(new_view->panel);
1001 if (view->child && view_is_splitscreen(view->child))
1002 show_panel(view->child->panel);
1005 if (focus_view == NULL) {
1007 show_panel(view->panel);
1008 if (view->child && view_is_splitscreen(view->child))
1009 show_panel(view->child->panel);
1013 err = view->parent->show(view->parent);
1017 err = view->show(view);
1021 err = view->child->show(view->child);
1030 while (!TAILQ_EMPTY(&views)) {
1031 view = TAILQ_FIRST(&views);
1032 TAILQ_REMOVE(&views, view, entry);
1036 errcode = pthread_mutex_unlock(&tog_mutex);
1037 if (errcode && err == NULL)
1038 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1048 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1053 /* Create newly allocated wide-character string equivalent to a byte string. */
1054 static const struct got_error *
1055 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1058 const struct got_error *err = NULL;
1061 *wlen = mbstowcs(NULL, s, 0);
1062 if (*wlen == (size_t)-1) {
1064 if (errno != EILSEQ)
1065 return got_error_from_errno("mbstowcs");
1067 /* byte string invalid in current encoding; try to "fix" it */
1068 err = got_mbsavis(&vis, &vislen, s);
1071 *wlen = mbstowcs(NULL, vis, 0);
1072 if (*wlen == (size_t)-1) {
1073 err = got_error_from_errno("mbstowcs"); /* give up */
1078 *ws = calloc(*wlen + 1, sizeof(**ws));
1080 err = got_error_from_errno("calloc");
1084 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1085 err = got_error_from_errno("mbstowcs");
1096 /* Format a line for display, ensuring that it won't overflow a width limit. */
1097 static const struct got_error *
1098 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1101 const struct got_error *err = NULL;
1103 wchar_t *wline = NULL;
1110 err = mbs2ws(&wline, &wlen, line);
1116 int width = wcwidth(wline[i]);
1123 if (width == 1 || width == 2) {
1124 if (cols + width > wlimit)
1128 } else if (width == -1) {
1129 if (wline[i] == L'\t') {
1131 ((cols + col_tab_align) % TABSIZE);
1132 if (cols + width > wlimit)
1138 err = got_error_from_errno("wcwidth");
1153 static const struct got_error*
1154 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1155 struct got_object_id *id, struct got_repository *repo)
1157 static const struct got_error *err = NULL;
1158 struct got_reflist_entry *re;
1164 SIMPLEQ_FOREACH(re, refs, entry) {
1165 struct got_tag_object *tag = NULL;
1168 name = got_ref_get_name(re->ref);
1169 if (strcmp(name, GOT_REF_HEAD) == 0)
1171 if (strncmp(name, "refs/", 5) == 0)
1173 if (strncmp(name, "got/", 4) == 0)
1175 if (strncmp(name, "heads/", 6) == 0)
1177 if (strncmp(name, "remotes/", 8) == 0) {
1179 s = strstr(name, "/" GOT_REF_HEAD);
1180 if (s != NULL && s[strlen(s)] == '\0')
1183 if (strncmp(name, "tags/", 5) == 0) {
1184 err = got_object_open_as_tag(&tag, repo, re->id);
1186 if (err->code != GOT_ERR_OBJ_TYPE)
1188 /* Ref points at something other than a tag. */
1193 cmp = got_object_id_cmp(tag ?
1194 got_object_tag_get_object_id(tag) : re->id, id);
1196 got_object_tag_close(tag);
1200 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1201 s ? ", " : "", name) == -1) {
1202 err = got_error_from_errno("asprintf");
1213 static const struct got_error *
1214 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1217 char *smallerthan, *at;
1219 smallerthan = strchr(author, '<');
1220 if (smallerthan && smallerthan[1] != '\0')
1221 author = smallerthan + 1;
1222 at = strchr(author, '@');
1225 return format_line(wauthor, author_width, author, limit, col_tab_align);
1228 static const struct got_error *
1229 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1230 struct got_object_id *id, struct got_reflist_head *refs,
1231 const size_t date_display_cols, int author_display_cols,
1232 struct tog_colors *colors)
1234 const struct got_error *err = NULL;
1235 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1236 char *logmsg0 = NULL, *logmsg = NULL;
1237 char *author = NULL;
1238 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1239 int author_width, logmsg_width;
1240 char *newline, *line = NULL;
1242 const int avail = view->ncols;
1244 time_t committer_time;
1245 struct tog_color *tc;
1247 committer_time = got_object_commit_get_committer_time(commit);
1248 if (localtime_r(&committer_time, &tm) == NULL)
1249 return got_error_from_errno("localtime_r");
1250 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1252 return got_error(GOT_ERR_NO_SPACE);
1254 if (avail <= date_display_cols)
1255 limit = MIN(sizeof(datebuf) - 1, avail);
1257 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1258 tc = get_color(colors, TOG_COLOR_DATE);
1260 wattr_on(view->window,
1261 COLOR_PAIR(tc->colorpair), NULL);
1262 waddnstr(view->window, datebuf, limit);
1264 wattr_off(view->window,
1265 COLOR_PAIR(tc->colorpair), NULL);
1272 err = got_object_id_str(&id_str, id);
1275 tc = get_color(colors, TOG_COLOR_COMMIT);
1277 wattr_on(view->window,
1278 COLOR_PAIR(tc->colorpair), NULL);
1279 wprintw(view->window, "%.8s ", id_str);
1281 wattr_off(view->window,
1282 COLOR_PAIR(tc->colorpair), NULL);
1289 author = strdup(got_object_commit_get_author(commit));
1290 if (author == NULL) {
1291 err = got_error_from_errno("strdup");
1294 err = format_author(&wauthor, &author_width, author, avail - col, col);
1297 tc = get_color(colors, TOG_COLOR_AUTHOR);
1299 wattr_on(view->window,
1300 COLOR_PAIR(tc->colorpair), NULL);
1301 waddwstr(view->window, wauthor);
1303 wattr_off(view->window,
1304 COLOR_PAIR(tc->colorpair), NULL);
1305 col += author_width;
1306 while (col < avail && author_width < author_display_cols + 2) {
1307 waddch(view->window, ' ');
1314 err = got_object_commit_get_logmsg(&logmsg0, commit);
1318 while (*logmsg == '\n')
1320 newline = strchr(logmsg, '\n');
1323 limit = avail - col;
1324 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1327 waddwstr(view->window, wlogmsg);
1328 col += logmsg_width;
1329 while (col < avail) {
1330 waddch(view->window, ' ');
1342 static struct commit_queue_entry *
1343 alloc_commit_queue_entry(struct got_commit_object *commit,
1344 struct got_object_id *id)
1346 struct commit_queue_entry *entry;
1348 entry = calloc(1, sizeof(*entry));
1353 entry->commit = commit;
1358 pop_commit(struct commit_queue *commits)
1360 struct commit_queue_entry *entry;
1362 entry = TAILQ_FIRST(&commits->head);
1363 TAILQ_REMOVE(&commits->head, entry, entry);
1364 got_object_commit_close(entry->commit);
1365 commits->ncommits--;
1366 /* Don't free entry->id! It is owned by the commit graph. */
1371 free_commits(struct commit_queue *commits)
1373 while (!TAILQ_EMPTY(&commits->head))
1374 pop_commit(commits);
1377 static const struct got_error *
1378 match_commit(int *have_match, struct got_object_id *id,
1379 struct got_commit_object *commit, regex_t *regex)
1381 const struct got_error *err = NULL;
1382 regmatch_t regmatch;
1383 char *id_str = NULL, *logmsg = NULL;
1387 err = got_object_id_str(&id_str, id);
1391 err = got_object_commit_get_logmsg(&logmsg, commit);
1395 if (regexec(regex, got_object_commit_get_author(commit), 1,
1396 ®match, 0) == 0 ||
1397 regexec(regex, got_object_commit_get_committer(commit), 1,
1398 ®match, 0) == 0 ||
1399 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1400 regexec(regex, logmsg, 1, ®match, 0) == 0)
1408 static const struct got_error *
1409 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1410 int minqueue, struct got_repository *repo, const char *path,
1411 int *searching, int *search_next_done, regex_t *regex)
1413 const struct got_error *err = NULL;
1417 * We keep all commits open throughout the lifetime of the log
1418 * view in order to avoid having to re-fetch commits from disk
1419 * while updating the display.
1421 while (nqueued < minqueue ||
1422 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1423 struct got_object_id *id;
1424 struct got_commit_object *commit;
1425 struct commit_queue_entry *entry;
1428 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1429 if (err || id == NULL)
1432 err = got_object_open_as_commit(&commit, repo, id);
1435 entry = alloc_commit_queue_entry(commit, id);
1436 if (entry == NULL) {
1437 err = got_error_from_errno("alloc_commit_queue_entry");
1441 errcode = pthread_mutex_lock(&tog_mutex);
1443 err = got_error_set_errno(errcode,
1444 "pthread_mutex_lock");
1448 entry->idx = commits->ncommits;
1449 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1451 commits->ncommits++;
1453 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1455 err = match_commit(&have_match, id, commit, regex);
1459 *search_next_done = TOG_SEARCH_HAVE_MORE;
1462 errcode = pthread_mutex_unlock(&tog_mutex);
1463 if (errcode && err == NULL)
1464 err = got_error_set_errno(errcode,
1465 "pthread_mutex_unlock");
1473 static const struct got_error *
1474 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1475 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1476 struct commit_queue *commits, int selected_idx, int limit,
1477 struct got_reflist_head *refs, const char *path, int commits_needed,
1478 struct tog_colors *colors)
1480 const struct got_error *err = NULL;
1481 struct tog_log_view_state *s = &view->state.log;
1482 struct commit_queue_entry *entry;
1484 int ncommits, author_cols = 4;
1485 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1486 char *refs_str = NULL;
1488 struct tog_color *tc;
1489 static const size_t date_display_cols = 12;
1494 if (ncommits == selected_idx) {
1498 entry = TAILQ_NEXT(entry, entry);
1502 if (*selected && !(view->searching && view->search_next_done == 0)) {
1503 err = got_object_id_str(&id_str, (*selected)->id);
1507 err = build_refs_str(&refs_str, refs, (*selected)->id,
1514 if (commits_needed == 0)
1515 halfdelay(10); /* disable fast refresh */
1517 if (commits_needed > 0) {
1518 if (asprintf(&ncommits_str, " [%d/%d] %s",
1519 entry ? entry->idx + 1 : 0, commits->ncommits,
1520 (view->searching && !view->search_next_done) ?
1521 "searching..." : "loading...") == -1) {
1522 err = got_error_from_errno("asprintf");
1526 const char *search_str = NULL;
1528 if (view->searching) {
1529 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1530 search_str = "no more matches";
1531 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1532 search_str = "no matches found";
1533 else if (!view->search_next_done)
1534 search_str = "searching...";
1537 if (asprintf(&ncommits_str, " [%d/%d] %s",
1538 entry ? entry->idx + 1 : 0, commits->ncommits,
1539 search_str ? search_str :
1540 (refs_str ? refs_str : "")) == -1) {
1541 err = got_error_from_errno("asprintf");
1546 if (path && strcmp(path, "/") != 0) {
1547 if (asprintf(&header, "commit %s %s%s",
1548 id_str ? id_str : "........................................",
1549 path, ncommits_str) == -1) {
1550 err = got_error_from_errno("asprintf");
1554 } else if (asprintf(&header, "commit %s%s",
1555 id_str ? id_str : "........................................",
1556 ncommits_str) == -1) {
1557 err = got_error_from_errno("asprintf");
1561 err = format_line(&wline, &width, header, view->ncols, 0);
1565 werase(view->window);
1567 if (view_needs_focus_indication(view))
1568 wstandout(view->window);
1569 tc = get_color(colors, TOG_COLOR_COMMIT);
1571 wattr_on(view->window,
1572 COLOR_PAIR(tc->colorpair), NULL);
1573 waddwstr(view->window, wline);
1575 wattr_off(view->window,
1576 COLOR_PAIR(tc->colorpair), NULL);
1577 while (width < view->ncols) {
1578 waddch(view->window, ' ');
1581 if (view_needs_focus_indication(view))
1582 wstandend(view->window);
1587 /* Grow author column size if necessary. */
1594 if (ncommits >= limit - 1)
1596 author = strdup(got_object_commit_get_author(entry->commit));
1597 if (author == NULL) {
1598 err = got_error_from_errno("strdup");
1601 err = format_author(&wauthor, &width, author, COLS,
1603 if (author_cols < width)
1604 author_cols = width;
1608 entry = TAILQ_NEXT(entry, entry);
1615 if (ncommits >= limit - 1)
1617 if (ncommits == selected_idx)
1618 wstandout(view->window);
1619 err = draw_commit(view, entry->commit, entry->id, refs,
1620 date_display_cols, author_cols, colors);
1621 if (ncommits == selected_idx)
1622 wstandend(view->window);
1627 entry = TAILQ_NEXT(entry, entry);
1640 scroll_up(struct tog_view *view,
1641 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1642 struct commit_queue *commits)
1644 struct commit_queue_entry *entry;
1647 entry = TAILQ_FIRST(&commits->head);
1648 if (*first_displayed_entry == entry)
1651 entry = *first_displayed_entry;
1652 while (entry && nscrolled < maxscroll) {
1653 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1655 *first_displayed_entry = entry;
1661 static const struct got_error *
1662 trigger_log_thread(struct tog_view *log_view, int wait,
1663 int *commits_needed, int *log_complete,
1664 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1668 halfdelay(1); /* fast refresh while loading commits */
1670 while (*commits_needed > 0) {
1674 /* Wake the log thread. */
1675 errcode = pthread_cond_signal(need_commits);
1677 return got_error_set_errno(errcode,
1678 "pthread_cond_signal");
1681 * The mutex will be released while the view loop waits
1682 * in wgetch(), at which time the log thread will run.
1687 /* Display progress update in log view. */
1688 show_log_view(log_view);
1692 /* Wait right here while next commit is being loaded. */
1693 errcode = pthread_cond_wait(commit_loaded, &tog_mutex);
1695 return got_error_set_errno(errcode,
1696 "pthread_cond_wait");
1698 /* Display progress update in log view. */
1699 show_log_view(log_view);
1707 static const struct got_error *
1708 scroll_down(struct tog_view *log_view,
1709 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1710 struct commit_queue_entry **last_displayed_entry,
1711 struct commit_queue *commits, int *log_complete, int *commits_needed,
1712 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1714 const struct got_error *err = NULL;
1715 struct commit_queue_entry *pentry;
1716 int nscrolled = 0, ncommits_needed;
1718 if (*last_displayed_entry == NULL)
1721 ncommits_needed = (*last_displayed_entry)->idx + 1 + maxscroll;
1722 if (commits->ncommits < ncommits_needed && !*log_complete) {
1724 * Ask the log thread for required amount of commits.
1726 (*commits_needed) += maxscroll;
1727 err = trigger_log_thread(log_view, 1, commits_needed,
1728 log_complete, need_commits, commit_loaded);
1734 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1738 *last_displayed_entry = pentry;
1740 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1743 *first_displayed_entry = pentry;
1744 } while (++nscrolled < maxscroll);
1749 static const struct got_error *
1750 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1751 struct got_commit_object *commit, struct got_object_id *commit_id,
1752 struct tog_view *log_view, struct got_reflist_head *refs,
1753 struct got_repository *repo)
1755 const struct got_error *err;
1756 struct got_object_qid *parent_id;
1757 struct tog_view *diff_view;
1759 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1760 if (diff_view == NULL)
1761 return got_error_from_errno("view_open");
1763 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1764 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1765 commit_id, log_view, refs, repo);
1767 *new_view = diff_view;
1771 static const struct got_error *
1772 tree_view_visit_subtree(struct got_tree_object *subtree,
1773 struct tog_tree_view_state *s)
1775 struct tog_parent_tree *parent;
1777 parent = calloc(1, sizeof(*parent));
1779 return got_error_from_errno("calloc");
1781 parent->tree = s->tree;
1782 parent->first_displayed_entry = s->first_displayed_entry;
1783 parent->selected_entry = s->selected_entry;
1784 parent->selected = s->selected;
1785 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1788 s->first_displayed_entry = NULL;
1792 static const struct got_error *
1793 tree_view_walk_path(struct tog_tree_view_state *s,
1794 struct got_object_id *commit_id,
1795 const char *path, struct got_repository *repo)
1797 const struct got_error *err = NULL;
1798 struct got_tree_object *tree = NULL;
1800 char *slash, *subpath = NULL;
1802 /* Walk the path and open corresponding tree objects. */
1805 struct got_tree_entry *te;
1806 struct got_object_id *tree_id;
1812 /* Ensure the correct subtree entry is selected. */
1813 slash = strchr(p, '/');
1815 te_name = strdup(p);
1817 te_name = strndup(p, slash - p);
1818 if (te_name == NULL) {
1819 err = got_error_from_errno("strndup");
1822 te = got_object_tree_find_entry(s->tree, te_name);
1824 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1829 s->selected_entry = te;
1830 s->selected = got_tree_entry_get_index(te);
1831 if (s->tree != s->root)
1832 s->selected++; /* skip '..' */
1834 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1835 /* Jump to this file's entry. */
1836 s->first_displayed_entry = s->selected_entry;
1841 slash = strchr(p, '/');
1843 subpath = strndup(path, slash - path);
1845 subpath = strdup(path);
1846 if (subpath == NULL) {
1847 err = got_error_from_errno("strdup");
1851 err = got_object_id_by_path(&tree_id, repo, commit_id,
1856 err = got_object_open_as_tree(&tree, repo, tree_id);
1861 err = tree_view_visit_subtree(tree, s);
1863 got_object_tree_close(tree);
1877 static const struct got_error *
1878 browse_commit_tree(struct tog_view **new_view, int begin_x,
1879 struct commit_queue_entry *entry, const char *path,
1880 struct got_reflist_head *refs, struct got_repository *repo)
1882 const struct got_error *err = NULL;
1883 struct got_tree_object *tree;
1884 struct tog_tree_view_state *s;
1885 struct tog_view *tree_view;
1887 err = got_object_open_as_tree(&tree, repo,
1888 got_object_commit_get_tree_id(entry->commit));
1892 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1893 if (tree_view == NULL)
1894 return got_error_from_errno("view_open");
1896 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1898 got_object_tree_close(tree);
1901 s = &tree_view->state.tree;
1903 *new_view = tree_view;
1905 if (got_path_is_root_dir(path))
1908 return tree_view_walk_path(s, entry->id, path, repo);
1911 static const struct got_error *
1912 block_signals_used_by_main_thread(void)
1917 if (sigemptyset(&sigset) == -1)
1918 return got_error_from_errno("sigemptyset");
1920 /* tog handles SIGWINCH and SIGCONT */
1921 if (sigaddset(&sigset, SIGWINCH) == -1)
1922 return got_error_from_errno("sigaddset");
1923 if (sigaddset(&sigset, SIGCONT) == -1)
1924 return got_error_from_errno("sigaddset");
1926 /* ncurses handles SIGTSTP */
1927 if (sigaddset(&sigset, SIGTSTP) == -1)
1928 return got_error_from_errno("sigaddset");
1930 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1932 return got_error_set_errno(errcode, "pthread_sigmask");
1938 log_thread(void *arg)
1940 const struct got_error *err = NULL;
1942 struct tog_log_thread_args *a = arg;
1945 err = block_signals_used_by_main_thread();
1949 while (!done && !err && !tog_sigpipe_received) {
1950 err = queue_commits(a->graph, a->commits, 1, a->repo,
1951 a->in_repo_path, a->searching, a->search_next_done,
1954 if (err->code != GOT_ERR_ITER_COMPLETED)
1958 } else if (a->commits_needed > 0)
1959 a->commits_needed--;
1961 errcode = pthread_mutex_lock(&tog_mutex);
1963 err = got_error_set_errno(errcode,
1964 "pthread_mutex_lock");
1966 } else if (*a->quit)
1968 else if (*a->first_displayed_entry == NULL) {
1969 *a->first_displayed_entry =
1970 TAILQ_FIRST(&a->commits->head);
1971 *a->selected_entry = *a->first_displayed_entry;
1974 errcode = pthread_cond_signal(&a->commit_loaded);
1976 err = got_error_set_errno(errcode,
1977 "pthread_cond_signal");
1978 pthread_mutex_unlock(&tog_mutex);
1983 a->commits_needed = 0;
1985 if (a->commits_needed == 0) {
1986 errcode = pthread_cond_wait(&a->need_commits,
1989 err = got_error_set_errno(errcode,
1990 "pthread_cond_wait");
1994 errcode = pthread_mutex_unlock(&tog_mutex);
1995 if (errcode && err == NULL)
1996 err = got_error_set_errno(errcode,
1997 "pthread_mutex_unlock");
1999 a->log_complete = 1;
2003 static const struct got_error *
2004 stop_log_thread(struct tog_log_view_state *s)
2006 const struct got_error *err = NULL;
2011 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2013 return got_error_set_errno(errcode,
2014 "pthread_cond_signal");
2015 errcode = pthread_mutex_unlock(&tog_mutex);
2017 return got_error_set_errno(errcode,
2018 "pthread_mutex_unlock");
2019 errcode = pthread_join(s->thread, (void **)&err);
2021 return got_error_set_errno(errcode, "pthread_join");
2022 errcode = pthread_mutex_lock(&tog_mutex);
2024 return got_error_set_errno(errcode,
2025 "pthread_mutex_lock");
2029 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2030 if (errcode && err == NULL)
2031 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2033 if (s->thread_args.repo) {
2034 got_repo_close(s->thread_args.repo);
2035 s->thread_args.repo = NULL;
2038 if (s->thread_args.graph) {
2039 got_commit_graph_close(s->thread_args.graph);
2040 s->thread_args.graph = NULL;
2046 static const struct got_error *
2047 close_log_view(struct tog_view *view)
2049 const struct got_error *err = NULL;
2050 struct tog_log_view_state *s = &view->state.log;
2052 err = stop_log_thread(s);
2053 free_commits(&s->commits);
2054 free(s->in_repo_path);
2055 s->in_repo_path = NULL;
2061 static const struct got_error *
2062 search_start_log_view(struct tog_view *view)
2064 struct tog_log_view_state *s = &view->state.log;
2066 s->matched_entry = NULL;
2067 s->search_entry = NULL;
2071 static const struct got_error *
2072 search_next_log_view(struct tog_view *view)
2074 const struct got_error *err = NULL;
2075 struct tog_log_view_state *s = &view->state.log;
2076 struct commit_queue_entry *entry;
2078 /* Display progress update in log view. */
2079 show_log_view(view);
2083 if (s->search_entry) {
2085 errcode = pthread_mutex_unlock(&tog_mutex);
2087 return got_error_set_errno(errcode,
2088 "pthread_mutex_unlock");
2089 ch = wgetch(view->window);
2090 errcode = pthread_mutex_lock(&tog_mutex);
2092 return got_error_set_errno(errcode,
2093 "pthread_mutex_lock");
2094 if (ch == KEY_BACKSPACE) {
2095 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2098 if (view->searching == TOG_SEARCH_FORWARD)
2099 entry = TAILQ_NEXT(s->search_entry, entry);
2101 entry = TAILQ_PREV(s->search_entry,
2102 commit_queue_head, entry);
2103 } else if (s->matched_entry) {
2104 if (view->searching == TOG_SEARCH_FORWARD)
2105 entry = TAILQ_NEXT(s->matched_entry, entry);
2107 entry = TAILQ_PREV(s->matched_entry,
2108 commit_queue_head, entry);
2110 if (view->searching == TOG_SEARCH_FORWARD)
2111 entry = TAILQ_FIRST(&s->commits.head);
2113 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2119 if (entry == NULL) {
2120 if (s->thread_args.log_complete ||
2121 view->searching == TOG_SEARCH_BACKWARD) {
2122 view->search_next_done =
2123 (s->matched_entry == NULL ?
2124 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2125 s->search_entry = NULL;
2129 * Poke the log thread for more commits and return,
2130 * allowing the main loop to make progress. Search
2131 * will resume at s->search_entry once we come back.
2133 s->thread_args.commits_needed++;
2134 return trigger_log_thread(view, 0,
2135 &s->thread_args.commits_needed,
2136 &s->thread_args.log_complete,
2137 &s->thread_args.need_commits,
2138 &s->thread_args.commit_loaded);
2141 err = match_commit(&have_match, entry->id, entry->commit,
2146 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2147 s->matched_entry = entry;
2151 s->search_entry = entry;
2152 if (view->searching == TOG_SEARCH_FORWARD)
2153 entry = TAILQ_NEXT(entry, entry);
2155 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2158 if (s->matched_entry) {
2159 int cur = s->selected_entry->idx;
2160 while (cur < s->matched_entry->idx) {
2161 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2166 while (cur > s->matched_entry->idx) {
2167 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2174 s->search_entry = NULL;
2179 static const struct got_error *
2180 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2181 struct got_reflist_head *refs, struct got_repository *repo,
2182 const char *head_ref_name, const char *in_repo_path,
2185 const struct got_error *err = NULL;
2186 struct tog_log_view_state *s = &view->state.log;
2187 struct got_repository *thread_repo = NULL;
2188 struct got_commit_graph *thread_graph = NULL;
2191 if (in_repo_path != s->in_repo_path) {
2192 free(s->in_repo_path);
2193 s->in_repo_path = strdup(in_repo_path);
2194 if (s->in_repo_path == NULL)
2195 return got_error_from_errno("strdup");
2198 /* The commit queue only contains commits being displayed. */
2199 TAILQ_INIT(&s->commits.head);
2200 s->commits.ncommits = 0;
2204 s->head_ref_name = head_ref_name;
2205 s->start_id = got_object_id_dup(start_id);
2206 if (s->start_id == NULL) {
2207 err = got_error_from_errno("got_object_id_dup");
2210 s->log_branches = log_branches;
2212 SIMPLEQ_INIT(&s->colors);
2213 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2214 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2215 get_color_value("TOG_COLOR_COMMIT"));
2218 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2219 get_color_value("TOG_COLOR_AUTHOR"));
2221 free_colors(&s->colors);
2224 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2225 get_color_value("TOG_COLOR_DATE"));
2227 free_colors(&s->colors);
2232 view->show = show_log_view;
2233 view->input = input_log_view;
2234 view->close = close_log_view;
2235 view->search_start = search_start_log_view;
2236 view->search_next = search_next_log_view;
2238 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2241 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2245 err = got_commit_graph_iter_start(thread_graph,
2246 s->start_id, s->repo, NULL, NULL);
2250 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2252 err = got_error_set_errno(errcode, "pthread_cond_init");
2255 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2257 err = got_error_set_errno(errcode, "pthread_cond_init");
2261 s->thread_args.commits_needed = view->nlines;
2262 s->thread_args.graph = thread_graph;
2263 s->thread_args.commits = &s->commits;
2264 s->thread_args.in_repo_path = s->in_repo_path;
2265 s->thread_args.start_id = s->start_id;
2266 s->thread_args.repo = thread_repo;
2267 s->thread_args.log_complete = 0;
2268 s->thread_args.quit = &s->quit;
2269 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2270 s->thread_args.selected_entry = &s->selected_entry;
2271 s->thread_args.searching = &view->searching;
2272 s->thread_args.search_next_done = &view->search_next_done;
2273 s->thread_args.regex = &view->regex;
2276 close_log_view(view);
2280 static const struct got_error *
2281 show_log_view(struct tog_view *view)
2283 struct tog_log_view_state *s = &view->state.log;
2285 if (s->thread == NULL) {
2286 int errcode = pthread_create(&s->thread, NULL, log_thread,
2289 return got_error_set_errno(errcode, "pthread_create");
2292 return draw_commits(view, &s->last_displayed_entry,
2293 &s->selected_entry, s->first_displayed_entry,
2294 &s->commits, s->selected, view->nlines, s->refs,
2295 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2298 static const struct got_error *
2299 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2300 struct tog_view **focus_view, struct tog_view *view, int ch)
2302 const struct got_error *err = NULL;
2303 struct tog_log_view_state *s = &view->state.log;
2304 char *parent_path, *in_repo_path = NULL;
2305 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2307 struct got_object_id *start_id;
2317 if (s->first_displayed_entry == NULL)
2319 if (s->selected > 0)
2322 scroll_up(view, &s->first_displayed_entry, 1,
2327 if (s->first_displayed_entry == NULL)
2329 if (TAILQ_FIRST(&s->commits.head) ==
2330 s->first_displayed_entry) {
2334 scroll_up(view, &s->first_displayed_entry,
2335 view->nlines - 1, &s->commits);
2341 if (s->first_displayed_entry == NULL)
2343 if (s->selected < MIN(view->nlines - 2,
2344 s->commits.ncommits - 1)) {
2348 err = scroll_down(view, &s->first_displayed_entry, 1,
2349 &s->last_displayed_entry, &s->commits,
2350 &s->thread_args.log_complete,
2351 &s->thread_args.commits_needed,
2352 &s->thread_args.need_commits,
2353 &s->thread_args.commit_loaded);
2357 struct commit_queue_entry *first;
2358 first = s->first_displayed_entry;
2361 err = scroll_down(view, &s->first_displayed_entry,
2362 view->nlines - 1, &s->last_displayed_entry,
2363 &s->commits, &s->thread_args.log_complete,
2364 &s->thread_args.commits_needed,
2365 &s->thread_args.need_commits,
2366 &s->thread_args.commit_loaded);
2369 if (first == s->first_displayed_entry &&
2370 s->selected < MIN(view->nlines - 2,
2371 s->commits.ncommits - 1)) {
2372 /* can't scroll further down */
2373 s->selected = MIN(view->nlines - 2,
2374 s->commits.ncommits - 1);
2380 if (s->selected > view->nlines - 2)
2381 s->selected = view->nlines - 2;
2382 if (s->selected > s->commits.ncommits - 1)
2383 s->selected = s->commits.ncommits - 1;
2388 if (s->selected_entry == NULL)
2390 if (view_is_parent_view(view))
2391 begin_x = view_split_begin_x(view->begin_x);
2392 err = open_diff_view_for_commit(&diff_view, begin_x,
2393 s->selected_entry->commit, s->selected_entry->id,
2394 view, s->refs, s->repo);
2397 if (view_is_parent_view(view)) {
2398 err = view_close_child(view);
2401 err = view_set_child(view, diff_view);
2403 view_close(diff_view);
2406 *focus_view = diff_view;
2407 view->child_focussed = 1;
2409 *new_view = diff_view;
2412 if (s->selected_entry == NULL)
2414 if (view_is_parent_view(view))
2415 begin_x = view_split_begin_x(view->begin_x);
2416 err = browse_commit_tree(&tree_view, begin_x,
2417 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2420 if (view_is_parent_view(view)) {
2421 err = view_close_child(view);
2424 err = view_set_child(view, tree_view);
2426 view_close(tree_view);
2429 *focus_view = tree_view;
2430 view->child_focussed = 1;
2432 *new_view = tree_view;
2435 if (strcmp(s->in_repo_path, "/") == 0)
2437 parent_path = dirname(s->in_repo_path);
2438 if (parent_path && strcmp(parent_path, ".") != 0) {
2439 err = stop_log_thread(s);
2442 lv = view_open(view->nlines, view->ncols,
2443 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2445 return got_error_from_errno(
2447 err = open_log_view(lv, s->start_id, s->refs,
2448 s->repo, s->head_ref_name, parent_path,
2452 if (view_is_parent_view(view))
2455 view_set_child(view->parent, lv);
2462 err = stop_log_thread(s);
2465 lv = view_open(view->nlines, view->ncols,
2466 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2468 return got_error_from_errno("view_open");
2469 err = got_repo_match_object_id(&start_id, NULL,
2470 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2471 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2476 in_repo_path = strdup(s->in_repo_path);
2477 if (in_repo_path == NULL) {
2480 return got_error_from_errno("strdup");
2482 got_ref_list_free(s->refs);
2483 err = got_ref_list(s->refs, s->repo, NULL,
2484 got_ref_cmp_by_name, NULL);
2490 err = open_log_view(lv, start_id, s->refs, s->repo,
2491 s->head_ref_name, in_repo_path, s->log_branches);
2501 s->log_branches = !s->log_branches;
2502 err = stop_log_thread(s);
2505 lv = view_open(view->nlines, view->ncols,
2506 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2508 return got_error_from_errno("view_open");
2509 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2510 s->head_ref_name, s->in_repo_path, s->log_branches);
2525 static const struct got_error *
2526 apply_unveil(const char *repo_path, const char *worktree_path)
2528 const struct got_error *error;
2531 if (unveil("gmon.out", "rwc") != 0)
2532 return got_error_from_errno2("unveil", "gmon.out");
2534 if (repo_path && unveil(repo_path, "r") != 0)
2535 return got_error_from_errno2("unveil", repo_path);
2537 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2538 return got_error_from_errno2("unveil", worktree_path);
2540 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2541 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2543 error = got_privsep_unveil_exec_helpers();
2547 if (unveil(NULL, NULL) != 0)
2548 return got_error_from_errno("unveil");
2558 halfdelay(1); /* Do fast refresh while initial view is loading. */
2561 intrflush(stdscr, FALSE);
2562 keypad(stdscr, TRUE);
2564 if (getenv("TOG_COLORS") != NULL) {
2566 use_default_colors();
2568 signal(SIGWINCH, tog_sigwinch);
2569 signal(SIGPIPE, tog_sigpipe);
2570 signal(SIGCONT, tog_sigcont);
2573 static const struct got_error *
2574 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2575 struct got_repository *repo, struct got_worktree *worktree)
2577 const struct got_error *err = NULL;
2580 *in_repo_path = strdup("/");
2581 if (*in_repo_path == NULL)
2582 return got_error_from_errno("strdup");
2587 const char *prefix = got_worktree_get_path_prefix(worktree);
2590 err = got_worktree_resolve_path(&wt_path, worktree, argv[0]);
2594 if (asprintf(&p, "%s%s%s", prefix,
2595 (strcmp(prefix, "/") != 0) ? "/" : "", wt_path) == -1) {
2596 err = got_error_from_errno("asprintf");
2600 err = got_repo_map_path(in_repo_path, repo, p, 0);
2604 err = got_repo_map_path(in_repo_path, repo, argv[0], 1);
2609 static const struct got_error *
2610 cmd_log(int argc, char *argv[])
2612 const struct got_error *error;
2613 struct got_repository *repo = NULL;
2614 struct got_worktree *worktree = NULL;
2615 struct got_reflist_head refs;
2616 struct got_object_id *start_id = NULL;
2617 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2618 char *start_commit = NULL, *head_ref_name = NULL;
2619 int ch, log_branches = 0;
2620 struct tog_view *view;
2622 SIMPLEQ_INIT(&refs);
2625 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2630 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2636 start_commit = optarg;
2639 repo_path = realpath(optarg, NULL);
2640 if (repo_path == NULL)
2641 return got_error_from_errno2("realpath",
2656 cwd = getcwd(NULL, 0);
2658 return got_error_from_errno("getcwd");
2660 error = got_worktree_open(&worktree, cwd);
2661 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2664 if (repo_path == NULL) {
2667 strdup(got_worktree_get_repo_path(worktree));
2669 repo_path = strdup(cwd);
2671 if (repo_path == NULL) {
2672 error = got_error_from_errno("strdup");
2676 error = got_repo_open(&repo, repo_path, NULL);
2680 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2687 error = apply_unveil(got_repo_get_path(repo),
2688 worktree ? got_worktree_get_root_path(worktree) : NULL);
2692 if (start_commit == NULL)
2693 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2694 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2695 GOT_OBJ_TYPE_COMMIT, 1, repo);
2697 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2698 GOT_OBJ_TYPE_COMMIT, 1, repo);
2702 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2706 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2708 error = got_error_from_errno("view_open");
2712 head_ref_name = strdup(
2713 got_worktree_get_head_ref_name(worktree));
2714 if (head_ref_name == NULL) {
2715 error = got_error_from_errno("strdup");
2719 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2720 in_repo_path, log_branches);
2724 /* Release work tree lock. */
2725 got_worktree_close(worktree);
2728 error = view_loop(view);
2734 free(head_ref_name);
2736 got_repo_close(repo);
2738 got_worktree_close(worktree);
2739 got_ref_list_free(&refs);
2747 fprintf(stderr, "usage: %s diff [-r repository-path] object1 object2\n",
2753 parse_next_line(FILE *f, size_t *len)
2758 const char delim[3] = { '\0', '\0', '\0'};
2760 line = fparseln(f, &linelen, &lineno, delim, 0);
2767 match_line(const char *line, regex_t *regex)
2769 regmatch_t regmatch;
2771 return regexec(regex, line, 1, ®match, 0) == 0;
2775 match_color(struct tog_colors *colors, const char *line)
2777 struct tog_color *tc = NULL;
2779 SIMPLEQ_FOREACH(tc, colors, entry) {
2780 if (match_line(line, &tc->regex))
2787 static const struct got_error *
2788 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line, int nlines,
2789 int selected_line, int max_lines, int *last_displayed_line, int *eof,
2790 char *header, struct tog_colors *colors)
2792 const struct got_error *err;
2793 int lineno = 0, nprinted = 0;
2795 struct tog_color *tc;
2801 werase(view->window);
2804 err = format_line(&wline, &width, header, view->ncols, 0);
2809 if (view_needs_focus_indication(view))
2810 wstandout(view->window);
2811 waddwstr(view->window, wline);
2812 if (view_needs_focus_indication(view))
2813 wstandend(view->window);
2814 if (width <= view->ncols - 1)
2815 waddch(view->window, '\n');
2823 while (nprinted < max_lines) {
2824 line = parse_next_line(f, &len);
2829 if (++lineno < *first_displayed_line) {
2834 err = format_line(&wline, &width, line, view->ncols, 0);
2840 tc = match_color(colors, line);
2842 wattr_on(view->window,
2843 COLOR_PAIR(tc->colorpair), NULL);
2844 waddwstr(view->window, wline);
2846 wattr_off(view->window,
2847 COLOR_PAIR(tc->colorpair), NULL);
2848 if (width <= view->ncols - 1)
2849 waddch(view->window, '\n');
2850 if (++nprinted == 1)
2851 *first_displayed_line = lineno;
2856 *last_displayed_line = lineno;
2861 while (nprinted < view->nlines) {
2862 waddch(view->window, '\n');
2866 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2871 wstandout(view->window);
2872 waddwstr(view->window, wline);
2873 wstandend(view->window);
2880 get_datestr(time_t *time, char *datebuf)
2882 struct tm mytm, *tm;
2885 tm = gmtime_r(time, &mytm);
2888 s = asctime_r(tm, datebuf);
2891 p = strchr(s, '\n');
2897 static const struct got_error *
2898 get_changed_paths(struct got_pathlist_head *paths,
2899 struct got_commit_object *commit, struct got_repository *repo)
2901 const struct got_error *err = NULL;
2902 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2903 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2904 struct got_object_qid *qid;
2906 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2908 struct got_commit_object *pcommit;
2909 err = got_object_open_as_commit(&pcommit, repo,
2914 tree_id1 = got_object_commit_get_tree_id(pcommit);
2915 got_object_commit_close(pcommit);
2920 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2925 tree_id2 = got_object_commit_get_tree_id(commit);
2926 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2930 err = got_diff_tree(tree1, tree2, "", "", repo,
2931 got_diff_tree_collect_changed_paths, paths, 0);
2934 got_object_tree_close(tree1);
2936 got_object_tree_close(tree2);
2940 static const struct got_error *
2941 write_commit_info(struct got_object_id *commit_id,
2942 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2944 const struct got_error *err = NULL;
2945 char datebuf[26], *datestr;
2946 struct got_commit_object *commit;
2947 char *id_str = NULL, *logmsg = NULL;
2948 time_t committer_time;
2949 const char *author, *committer;
2950 char *refs_str = NULL;
2951 struct got_pathlist_head changed_paths;
2952 struct got_pathlist_entry *pe;
2954 TAILQ_INIT(&changed_paths);
2957 err = build_refs_str(&refs_str, refs, commit_id, repo);
2962 err = got_object_open_as_commit(&commit, repo, commit_id);
2966 err = got_object_id_str(&id_str, commit_id);
2968 err = got_error_from_errno("got_object_id_str");
2972 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2973 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2974 err = got_error_from_errno("fprintf");
2977 if (fprintf(outfile, "from: %s\n",
2978 got_object_commit_get_author(commit)) < 0) {
2979 err = got_error_from_errno("fprintf");
2982 committer_time = got_object_commit_get_committer_time(commit);
2983 datestr = get_datestr(&committer_time, datebuf);
2984 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2985 err = got_error_from_errno("fprintf");
2988 author = got_object_commit_get_author(commit);
2989 committer = got_object_commit_get_committer(commit);
2990 if (strcmp(author, committer) != 0 &&
2991 fprintf(outfile, "via: %s\n", committer) < 0) {
2992 err = got_error_from_errno("fprintf");
2995 err = got_object_commit_get_logmsg(&logmsg, commit);
2998 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2999 err = got_error_from_errno("fprintf");
3002 err = get_changed_paths(&changed_paths, commit, repo);
3005 TAILQ_FOREACH(pe, &changed_paths, entry) {
3006 struct got_diff_changed_path *cp = pe->data;
3007 fprintf(outfile, "%c %s\n", cp->status, pe->path);
3008 free((char *)pe->path);
3011 fputc('\n', outfile);
3013 got_pathlist_free(&changed_paths);
3017 got_object_commit_close(commit);
3021 const struct got_error *
3022 get_filestream_info(size_t *filesize, int *nlines, off_t **line_offsets,
3028 size_t noffsets = 0;
3032 *line_offsets = NULL;
3038 if (fseek(infile, 0, SEEK_END) == -1)
3039 return got_error_from_errno("fseek");
3040 len = ftell(infile) + 1;
3042 return got_error_from_errno("ftell");
3043 if (fseek(infile, 0, SEEK_SET) == -1)
3044 return got_error_from_errno("fseek");
3048 if ((buf = calloc(len, sizeof(char *))) == NULL)
3049 return got_error_from_errno("calloc");
3051 fread(buf, 1, len, infile);
3053 return got_error_from_errno("fread");
3056 if (line_offsets && nlines) {
3057 if (*line_offsets == NULL) {
3058 /* Have some data but perhaps no '\n'. */
3061 *line_offsets = calloc(1, sizeof(**line_offsets));
3062 if (*line_offsets == NULL)
3063 return got_error_from_errno("calloc");
3064 /* Skip forward over end of first line. */
3071 /* Scan '\n' offsets in remaining chunk of data. */
3073 if (buf[i] != '\n') {
3078 if (noffsets < *nlines) {
3079 off_t *o = recallocarray(*line_offsets,
3081 sizeof(**line_offsets));
3083 free(*line_offsets);
3084 *line_offsets = NULL;
3085 return got_error_from_errno(
3092 (*line_offsets)[*nlines - 1] = off;
3097 if (fflush(infile) != 0)
3098 return got_error_from_errno("fflush");
3107 static const struct got_error *
3108 create_diff(struct tog_diff_view_state *s)
3110 const struct got_error *err = NULL;
3116 err = got_error_from_errno("got_opentemp");
3119 if (s->f && fclose(s->f) != 0) {
3120 err = got_error_from_errno("fclose");
3126 err = got_object_get_type(&obj_type, s->repo, s->id1);
3128 err = got_object_get_type(&obj_type, s->repo, s->id2);
3133 case GOT_OBJ_TYPE_BLOB:
3134 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
3135 s->diff_context, 0, s->repo, s->f);
3137 case GOT_OBJ_TYPE_TREE:
3138 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
3139 s->diff_context, 0, s->repo, s->f);
3141 case GOT_OBJ_TYPE_COMMIT: {
3142 const struct got_object_id_queue *parent_ids;
3143 struct got_object_qid *pid;
3144 struct got_commit_object *commit2;
3146 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3149 /* Show commit info if we're diffing to a parent/root commit. */
3150 if (s->id1 == NULL) {
3151 err = write_commit_info(s->id2, s->refs, s->repo, s->f);
3155 parent_ids = got_object_commit_get_parent_ids(commit2);
3156 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3157 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3158 err = write_commit_info(s->id2, s->refs,
3166 got_object_commit_close(commit2);
3168 err = got_diff_objects_as_commits(s->id1, s->id2,
3169 s->diff_context, 0, s->repo, s->f);
3173 err = got_error(GOT_ERR_OBJ_TYPE);
3178 err = get_filestream_info(&s->filesize, &s->nlines, &s->line_offsets,
3181 if (s->f && fflush(s->f) != 0 && err == NULL)
3182 err = got_error_from_errno("fflush");
3187 diff_view_indicate_progress(struct tog_view *view)
3189 mvwaddstr(view->window, 0, 0, "diffing...");
3194 static const struct got_error *
3195 search_start_diff_view(struct tog_view *view)
3197 struct tog_diff_view_state *s = &view->state.diff;
3199 s->matched_line = 0;
3203 static const struct got_error *
3204 search_next_diff_view(struct tog_view *view)
3206 struct tog_diff_view_state *s = &view->state.diff;
3209 if (!view->searching) {
3210 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3214 if (s->matched_line) {
3215 if (view->searching == TOG_SEARCH_FORWARD)
3216 lineno = s->matched_line + 1;
3218 lineno = s->matched_line - 1;
3220 if (view->searching == TOG_SEARCH_FORWARD)
3231 if (lineno <= 0 || lineno > s->nlines) {
3232 if (s->matched_line == 0) {
3233 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3238 if (view->searching == TOG_SEARCH_FORWARD)
3244 offset = s->line_offsets[lineno - 1];
3245 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3247 return got_error_from_errno("fseeko");
3250 line = parse_next_line(s->f, &len);
3251 if (line && match_line(line, &view->regex)) {
3252 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3253 s->matched_line = lineno;
3258 if (view->searching == TOG_SEARCH_FORWARD)
3264 if (s->matched_line) {
3265 s->first_displayed_line = s->matched_line;
3266 s->selected_line = 1;
3272 static const struct got_error *
3273 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3274 struct got_object_id *id2, struct tog_view *log_view,
3275 struct got_reflist_head *refs, struct got_repository *repo)
3277 const struct got_error *err;
3278 struct tog_diff_view_state *s = &view->state.diff;
3280 if (id1 != NULL && id2 != NULL) {
3282 err = got_object_get_type(&type1, repo, id1);
3285 err = got_object_get_type(&type2, repo, id2);
3290 return got_error(GOT_ERR_OBJ_TYPE);
3292 s->first_displayed_line = 1;
3293 s->last_displayed_line = view->nlines;
3294 s->selected_line = 1;
3301 s->id1 = got_object_id_dup(id1);
3303 return got_error_from_errno("got_object_id_dup");
3307 s->id2 = got_object_id_dup(id2);
3308 if (s->id2 == NULL) {
3311 return got_error_from_errno("got_object_id_dup");
3314 s->first_displayed_line = 1;
3315 s->last_displayed_line = view->nlines;
3316 s->diff_context = 3;
3317 s->log_view = log_view;
3321 SIMPLEQ_INIT(&s->colors);
3322 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3323 err = add_color(&s->colors,
3324 "^-", TOG_COLOR_DIFF_MINUS,
3325 get_color_value("TOG_COLOR_DIFF_MINUS"));
3328 err = add_color(&s->colors, "^\\+",
3329 TOG_COLOR_DIFF_PLUS,
3330 get_color_value("TOG_COLOR_DIFF_PLUS"));
3332 free_colors(&s->colors);
3335 err = add_color(&s->colors,
3336 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3337 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3339 free_colors(&s->colors);
3343 err = add_color(&s->colors,
3344 "^(commit|(blob|file) [-+] |[MDmA] [^ ])",
3345 TOG_COLOR_DIFF_META,
3346 get_color_value("TOG_COLOR_DIFF_META"));
3348 free_colors(&s->colors);
3352 err = add_color(&s->colors,
3353 "^(from|via): ", TOG_COLOR_AUTHOR,
3354 get_color_value("TOG_COLOR_AUTHOR"));
3356 free_colors(&s->colors);
3360 err = add_color(&s->colors,
3361 "^date: ", TOG_COLOR_DATE,
3362 get_color_value("TOG_COLOR_DATE"));
3364 free_colors(&s->colors);
3369 if (log_view && view_is_splitscreen(view))
3370 show_log_view(log_view); /* draw vborder */
3371 diff_view_indicate_progress(view);
3373 err = create_diff(s);
3382 view->show = show_diff_view;
3383 view->input = input_diff_view;
3384 view->close = close_diff_view;
3385 view->search_start = search_start_diff_view;
3386 view->search_next = search_next_diff_view;
3391 static const struct got_error *
3392 close_diff_view(struct tog_view *view)
3394 const struct got_error *err = NULL;
3395 struct tog_diff_view_state *s = &view->state.diff;
3401 if (s->f && fclose(s->f) == EOF)
3402 err = got_error_from_errno("fclose");
3403 free_colors(&s->colors);
3404 free(s->line_offsets);
3408 static const struct got_error *
3409 show_diff_view(struct tog_view *view)
3411 const struct got_error *err;
3412 struct tog_diff_view_state *s = &view->state.diff;
3413 char *id_str1 = NULL, *id_str2, *header;
3416 err = got_object_id_str(&id_str1, s->id1);
3420 err = got_object_id_str(&id_str2, s->id2);
3424 if (asprintf(&header, "diff %s %s",
3425 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3426 err = got_error_from_errno("asprintf");
3434 return draw_file(view, s->f, &s->first_displayed_line, s->nlines,
3435 s->selected_line, view->nlines, &s->last_displayed_line, &s->eof,
3436 header, &s->colors);
3439 static const struct got_error *
3440 set_selected_commit(struct tog_diff_view_state *s,
3441 struct commit_queue_entry *entry)
3443 const struct got_error *err;
3444 const struct got_object_id_queue *parent_ids;
3445 struct got_commit_object *selected_commit;
3446 struct got_object_qid *pid;
3449 s->id2 = got_object_id_dup(entry->id);
3451 return got_error_from_errno("got_object_id_dup");
3453 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3456 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3458 pid = SIMPLEQ_FIRST(parent_ids);
3459 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3460 got_object_commit_close(selected_commit);
3464 static const struct got_error *
3465 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3466 struct tog_view **focus_view, struct tog_view *view, int ch)
3468 const struct got_error *err = NULL;
3469 struct tog_diff_view_state *s = &view->state.diff;
3470 struct tog_log_view_state *ls;
3471 struct commit_queue_entry *entry;
3477 if (s->first_displayed_line > 1)
3478 s->first_displayed_line--;
3482 if (s->first_displayed_line == 1)
3485 while (i++ < view->nlines - 1 &&
3486 s->first_displayed_line > 1)
3487 s->first_displayed_line--;
3492 s->first_displayed_line++;
3500 while (!s->eof && i++ < view->nlines - 1) {
3502 line = parse_next_line(s->f, NULL);
3503 s->first_displayed_line++;
3509 if (s->diff_context > 0) {
3511 diff_view_indicate_progress(view);
3512 err = create_diff(s);
3516 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3518 diff_view_indicate_progress(view);
3519 err = create_diff(s);
3524 if (s->log_view == NULL)
3526 ls = &s->log_view->state.log;
3527 entry = TAILQ_PREV(ls->selected_entry,
3528 commit_queue_head, entry);
3532 err = input_log_view(NULL, NULL, NULL, s->log_view,
3537 err = set_selected_commit(s, entry);
3541 s->first_displayed_line = 1;
3542 s->last_displayed_line = view->nlines;
3544 diff_view_indicate_progress(view);
3545 err = create_diff(s);
3549 if (s->log_view == NULL)
3551 ls = &s->log_view->state.log;
3553 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3554 ls->thread_args.commits_needed++;
3555 err = trigger_log_thread(s->log_view, 1,
3556 &ls->thread_args.commits_needed,
3557 &ls->thread_args.log_complete,
3558 &ls->thread_args.need_commits,
3559 &ls->thread_args.commit_loaded);
3563 err = input_log_view(NULL, NULL, NULL, s->log_view,
3568 entry = TAILQ_NEXT(ls->selected_entry, entry);
3572 err = set_selected_commit(s, entry);
3576 s->first_displayed_line = 1;
3577 s->last_displayed_line = view->nlines;
3579 diff_view_indicate_progress(view);
3580 err = create_diff(s);
3589 static const struct got_error *
3590 cmd_diff(int argc, char *argv[])
3592 const struct got_error *error = NULL;
3593 struct got_repository *repo = NULL;
3594 struct got_worktree *worktree = NULL;
3595 struct got_reflist_head refs;
3596 struct got_object_id *id1 = NULL, *id2 = NULL;
3597 char *repo_path = NULL, *cwd = NULL;
3598 char *id_str1 = NULL, *id_str2 = NULL;
3600 struct tog_view *view;
3602 SIMPLEQ_INIT(&refs);
3605 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3610 while ((ch = getopt(argc, argv, "r:")) != -1) {
3613 repo_path = realpath(optarg, NULL);
3614 if (repo_path == NULL)
3615 return got_error_from_errno2("realpath",
3628 usage_diff(); /* TODO show local worktree changes */
3629 } else if (argc == 2) {
3635 cwd = getcwd(NULL, 0);
3637 return got_error_from_errno("getcwd");
3639 error = got_worktree_open(&worktree, cwd);
3640 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3643 if (repo_path == NULL) {
3646 strdup(got_worktree_get_repo_path(worktree));
3648 repo_path = strdup(cwd);
3650 if (repo_path == NULL) {
3651 error = got_error_from_errno("strdup");
3655 error = got_repo_open(&repo, repo_path, NULL);
3661 error = apply_unveil(got_repo_get_path(repo), NULL);
3665 error = got_repo_match_object_id_prefix(&id1, id_str1,
3666 GOT_OBJ_TYPE_ANY, repo);
3670 error = got_repo_match_object_id_prefix(&id2, id_str2,
3671 GOT_OBJ_TYPE_ANY, repo);
3675 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3679 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3681 error = got_error_from_errno("view_open");
3684 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3687 error = view_loop(view);
3692 got_repo_close(repo);
3694 got_worktree_close(worktree);
3695 got_ref_list_free(&refs);
3703 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3708 struct tog_blame_line {
3710 struct got_object_id *id;
3713 static const struct got_error *
3714 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3715 const char *path, struct tog_blame_line *lines, int nlines,
3716 int blame_complete, int selected_line, int *first_displayed_line,
3717 int *last_displayed_line, int *eof, int max_lines,
3718 struct tog_colors *colors)
3720 const struct got_error *err;
3721 int lineno = 0, nprinted = 0;
3726 struct tog_blame_line *blame_line;
3727 struct got_object_id *prev_id = NULL;
3729 struct tog_color *tc;
3731 err = got_object_id_str(&id_str, id);
3736 werase(view->window);
3738 if (asprintf(&line, "commit %s", id_str) == -1) {
3739 err = got_error_from_errno("asprintf");
3744 err = format_line(&wline, &width, line, view->ncols, 0);
3749 if (view_needs_focus_indication(view))
3750 wstandout(view->window);
3751 tc = get_color(colors, TOG_COLOR_COMMIT);
3753 wattr_on(view->window,
3754 COLOR_PAIR(tc->colorpair), NULL);
3755 waddwstr(view->window, wline);
3757 wattr_off(view->window,
3758 COLOR_PAIR(tc->colorpair), NULL);
3759 if (view_needs_focus_indication(view))
3760 wstandend(view->window);
3763 if (width < view->ncols - 1)
3764 waddch(view->window, '\n');
3766 if (asprintf(&line, "[%d/%d] %s%s",
3767 *first_displayed_line - 1 + selected_line, nlines,
3768 blame_complete ? "" : "annotating... ", path) == -1) {
3770 return got_error_from_errno("asprintf");
3773 err = format_line(&wline, &width, line, view->ncols, 0);
3778 waddwstr(view->window, wline);
3781 if (width < view->ncols - 1)
3782 waddch(view->window, '\n');
3785 while (nprinted < max_lines - 2) {
3786 line = parse_next_line(f, &len);
3791 if (++lineno < *first_displayed_line) {
3796 if (view->ncols <= 9) {
3798 wline = wcsdup(L"");
3800 err = got_error_from_errno("wcsdup");
3802 err = format_line(&wline, &width, line,
3803 view->ncols - 9, 9);
3811 if (view->focussed && nprinted == selected_line - 1)
3812 wstandout(view->window);
3815 blame_line = &lines[lineno - 1];
3816 if (blame_line->annotated && prev_id &&
3817 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3819 nprinted == selected_line - 1)) {
3820 waddstr(view->window, " ");
3821 } else if (blame_line->annotated) {
3823 err = got_object_id_str(&id_str, blame_line->id);
3829 tc = get_color(colors, TOG_COLOR_COMMIT);
3831 wattr_on(view->window,
3832 COLOR_PAIR(tc->colorpair), NULL);
3833 wprintw(view->window, "%.8s", id_str);
3835 wattr_off(view->window,
3836 COLOR_PAIR(tc->colorpair), NULL);
3838 prev_id = blame_line->id;
3840 waddstr(view->window, "........");
3844 waddstr(view->window, "........");
3848 if (view->focussed && nprinted == selected_line - 1)
3849 wstandend(view->window);
3850 waddstr(view->window, " ");
3852 waddwstr(view->window, wline);
3853 if (width <= view->ncols - 1)
3854 waddch(view->window, '\n');
3855 if (++nprinted == 1)
3856 *first_displayed_line = lineno;
3861 *last_displayed_line = lineno;
3868 static const struct got_error *
3869 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3871 const struct got_error *err = NULL;
3872 struct tog_blame_cb_args *a = arg;
3873 struct tog_blame_line *line;
3876 if (nlines != a->nlines ||
3877 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3878 return got_error(GOT_ERR_RANGE);
3880 errcode = pthread_mutex_lock(&tog_mutex);
3882 return got_error_set_errno(errcode, "pthread_mutex_lock");
3884 if (*a->quit) { /* user has quit the blame view */
3885 err = got_error(GOT_ERR_ITER_COMPLETED);
3890 goto done; /* no change in this commit */
3892 line = &a->lines[lineno - 1];
3893 if (line->annotated)
3896 line->id = got_object_id_dup(id);
3897 if (line->id == NULL) {
3898 err = got_error_from_errno("got_object_id_dup");
3901 line->annotated = 1;
3903 errcode = pthread_mutex_unlock(&tog_mutex);
3905 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3910 blame_thread(void *arg)
3912 const struct got_error *err;
3913 struct tog_blame_thread_args *ta = arg;
3914 struct tog_blame_cb_args *a = ta->cb_args;
3917 err = block_signals_used_by_main_thread();
3921 err = got_blame(ta->path, a->commit_id, ta->repo,
3922 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3923 if (err && err->code == GOT_ERR_CANCELLED)
3926 errcode = pthread_mutex_lock(&tog_mutex);
3928 return (void *)got_error_set_errno(errcode,
3929 "pthread_mutex_lock");
3931 got_repo_close(ta->repo);
3935 errcode = pthread_mutex_unlock(&tog_mutex);
3936 if (errcode && err == NULL)
3937 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3942 static struct got_object_id *
3943 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3944 int first_displayed_line, int selected_line)
3946 struct tog_blame_line *line;
3951 line = &lines[first_displayed_line - 1 + selected_line - 1];
3952 if (!line->annotated)
3958 static const struct got_error *
3959 stop_blame(struct tog_blame *blame)
3961 const struct got_error *err = NULL;
3964 if (blame->thread) {
3966 errcode = pthread_mutex_unlock(&tog_mutex);
3968 return got_error_set_errno(errcode,
3969 "pthread_mutex_unlock");
3970 errcode = pthread_join(blame->thread, (void **)&err);
3972 return got_error_set_errno(errcode, "pthread_join");
3973 errcode = pthread_mutex_lock(&tog_mutex);
3975 return got_error_set_errno(errcode,
3976 "pthread_mutex_lock");
3977 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3979 blame->thread = NULL;
3981 if (blame->thread_args.repo) {
3982 got_repo_close(blame->thread_args.repo);
3983 blame->thread_args.repo = NULL;
3986 if (fclose(blame->f) != 0 && err == NULL)
3987 err = got_error_from_errno("fclose");
3991 for (i = 0; i < blame->nlines; i++)
3992 free(blame->lines[i].id);
3994 blame->lines = NULL;
3996 free(blame->cb_args.commit_id);
3997 blame->cb_args.commit_id = NULL;
4002 static const struct got_error *
4003 cancel_blame_view(void *arg)
4005 const struct got_error *err = NULL;
4009 errcode = pthread_mutex_lock(&tog_mutex);
4011 return got_error_set_errno(errcode,
4012 "pthread_mutex_unlock");
4015 err = got_error(GOT_ERR_CANCELLED);
4017 errcode = pthread_mutex_unlock(&tog_mutex);
4019 return got_error_set_errno(errcode,
4020 "pthread_mutex_lock");
4025 static const struct got_error *
4026 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
4027 int *first_displayed_line, int *last_displayed_line, int *selected_line,
4028 int *done, int *eof, const char *path, struct got_object_id *commit_id,
4029 struct got_repository *repo)
4031 const struct got_error *err = NULL;
4032 struct got_blob_object *blob = NULL;
4033 struct got_repository *thread_repo = NULL;
4034 struct got_object_id *obj_id = NULL;
4037 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
4041 err = got_object_get_type(&obj_type, repo, obj_id);
4045 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4046 err = got_error(GOT_ERR_OBJ_TYPE);
4050 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4053 blame->f = got_opentemp();
4054 if (blame->f == NULL) {
4055 err = got_error_from_errno("got_opentemp");
4058 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4059 &blame->line_offsets, blame->f, blob);
4060 if (err || blame->nlines == 0)
4063 /* Don't include \n at EOF in the blame line count. */
4064 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4067 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4068 if (blame->lines == NULL) {
4069 err = got_error_from_errno("calloc");
4073 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
4077 blame->cb_args.view = view;
4078 blame->cb_args.lines = blame->lines;
4079 blame->cb_args.nlines = blame->nlines;
4080 blame->cb_args.commit_id = got_object_id_dup(commit_id);
4081 if (blame->cb_args.commit_id == NULL) {
4082 err = got_error_from_errno("got_object_id_dup");
4085 blame->cb_args.quit = done;
4087 blame->thread_args.path = path;
4088 blame->thread_args.repo = thread_repo;
4089 blame->thread_args.cb_args = &blame->cb_args;
4090 blame->thread_args.complete = blame_complete;
4091 blame->thread_args.cancel_cb = cancel_blame_view;
4092 blame->thread_args.cancel_arg = done;
4093 *blame_complete = 0;
4097 got_object_blob_close(blob);
4104 static const struct got_error *
4105 open_blame_view(struct tog_view *view, char *path,
4106 struct got_object_id *commit_id, struct got_reflist_head *refs,
4107 struct got_repository *repo)
4109 const struct got_error *err = NULL;
4110 struct tog_blame_view_state *s = &view->state.blame;
4112 SIMPLEQ_INIT(&s->blamed_commits);
4114 s->path = strdup(path);
4115 if (s->path == NULL)
4116 return got_error_from_errno("strdup");
4118 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4124 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4125 s->first_displayed_line = 1;
4126 s->last_displayed_line = view->nlines;
4127 s->selected_line = 1;
4128 s->blame_complete = 0;
4131 s->commit_id = commit_id;
4132 memset(&s->blame, 0, sizeof(s->blame));
4134 SIMPLEQ_INIT(&s->colors);
4135 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4136 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4137 get_color_value("TOG_COLOR_COMMIT"));
4142 view->show = show_blame_view;
4143 view->input = input_blame_view;
4144 view->close = close_blame_view;
4145 view->search_start = search_start_blame_view;
4146 view->search_next = search_next_blame_view;
4148 return run_blame(&s->blame, view, &s->blame_complete,
4149 &s->first_displayed_line, &s->last_displayed_line,
4150 &s->selected_line, &s->done, &s->eof, s->path,
4151 s->blamed_commit->id, s->repo);
4154 static const struct got_error *
4155 close_blame_view(struct tog_view *view)
4157 const struct got_error *err = NULL;
4158 struct tog_blame_view_state *s = &view->state.blame;
4160 if (s->blame.thread)
4161 err = stop_blame(&s->blame);
4163 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4164 struct got_object_qid *blamed_commit;
4165 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4166 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4167 got_object_qid_free(blamed_commit);
4171 free_colors(&s->colors);
4176 static const struct got_error *
4177 search_start_blame_view(struct tog_view *view)
4179 struct tog_blame_view_state *s = &view->state.blame;
4181 s->matched_line = 0;
4185 static const struct got_error *
4186 search_next_blame_view(struct tog_view *view)
4188 struct tog_blame_view_state *s = &view->state.blame;
4191 if (!view->searching) {
4192 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4196 if (s->matched_line) {
4197 if (view->searching == TOG_SEARCH_FORWARD)
4198 lineno = s->matched_line + 1;
4200 lineno = s->matched_line - 1;
4202 if (view->searching == TOG_SEARCH_FORWARD)
4205 lineno = s->blame.nlines;
4213 if (lineno <= 0 || lineno > s->blame.nlines) {
4214 if (s->matched_line == 0) {
4215 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4220 if (view->searching == TOG_SEARCH_FORWARD)
4223 lineno = s->blame.nlines;
4226 offset = s->blame.line_offsets[lineno - 1];
4227 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4229 return got_error_from_errno("fseeko");
4232 line = parse_next_line(s->blame.f, &len);
4233 if (line && match_line(line, &view->regex)) {
4234 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4235 s->matched_line = lineno;
4240 if (view->searching == TOG_SEARCH_FORWARD)
4246 if (s->matched_line) {
4247 s->first_displayed_line = s->matched_line;
4248 s->selected_line = 1;
4254 static const struct got_error *
4255 show_blame_view(struct tog_view *view)
4257 const struct got_error *err = NULL;
4258 struct tog_blame_view_state *s = &view->state.blame;
4261 if (s->blame.thread == NULL) {
4262 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4263 &s->blame.thread_args);
4265 return got_error_set_errno(errcode, "pthread_create");
4267 halfdelay(1); /* fast refresh while annotating */
4270 if (s->blame_complete)
4271 halfdelay(10); /* disable fast refresh */
4273 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4274 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4275 s->selected_line, &s->first_displayed_line,
4276 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
4282 static const struct got_error *
4283 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4284 struct tog_view **focus_view, struct tog_view *view, int ch)
4286 const struct got_error *err = NULL, *thread_err = NULL;
4287 struct tog_view *diff_view;
4288 struct tog_blame_view_state *s = &view->state.blame;
4297 if (s->selected_line > 1)
4299 else if (s->selected_line == 1 &&
4300 s->first_displayed_line > 1)
4301 s->first_displayed_line--;
4305 if (s->first_displayed_line == 1) {
4306 s->selected_line = 1;
4309 if (s->first_displayed_line > view->nlines - 2)
4310 s->first_displayed_line -=
4313 s->first_displayed_line = 1;
4317 if (s->selected_line < view->nlines - 2 &&
4318 s->first_displayed_line +
4319 s->selected_line <= s->blame.nlines)
4321 else if (s->last_displayed_line <
4323 s->first_displayed_line++;
4327 struct got_object_id *id = NULL;
4328 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4329 s->first_displayed_line, s->selected_line);
4333 struct got_commit_object *commit;
4334 struct got_object_qid *pid;
4335 struct got_object_id *blob_id = NULL;
4337 err = got_object_open_as_commit(&commit,
4341 pid = SIMPLEQ_FIRST(
4342 got_object_commit_get_parent_ids(commit));
4344 got_object_commit_close(commit);
4347 /* Check if path history ends here. */
4348 err = got_object_id_by_path(&blob_id, s->repo,
4351 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4353 got_object_commit_close(commit);
4356 err = got_object_get_type(&obj_type, s->repo,
4359 /* Can't blame non-blob type objects. */
4360 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4361 got_object_commit_close(commit);
4364 err = got_object_qid_alloc(&s->blamed_commit,
4366 got_object_commit_close(commit);
4368 if (got_object_id_cmp(id,
4369 s->blamed_commit->id) == 0)
4371 err = got_object_qid_alloc(&s->blamed_commit,
4377 thread_err = stop_blame(&s->blame);
4381 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4382 s->blamed_commit, entry);
4383 err = run_blame(&s->blame, view, &s->blame_complete,
4384 &s->first_displayed_line, &s->last_displayed_line,
4385 &s->selected_line, &s->done, &s->eof,
4386 s->path, s->blamed_commit->id, s->repo);
4392 struct got_object_qid *first;
4393 first = SIMPLEQ_FIRST(&s->blamed_commits);
4394 if (!got_object_id_cmp(first->id, s->commit_id))
4397 thread_err = stop_blame(&s->blame);
4401 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4402 got_object_qid_free(s->blamed_commit);
4404 SIMPLEQ_FIRST(&s->blamed_commits);
4405 err = run_blame(&s->blame, view, &s->blame_complete,
4406 &s->first_displayed_line, &s->last_displayed_line,
4407 &s->selected_line, &s->done, &s->eof, s->path,
4408 s->blamed_commit->id, s->repo);
4415 struct got_object_id *id = NULL;
4416 struct got_object_qid *pid;
4417 struct got_commit_object *commit = NULL;
4418 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4419 s->first_displayed_line, s->selected_line);
4422 err = got_object_open_as_commit(&commit, s->repo, id);
4425 pid = SIMPLEQ_FIRST(
4426 got_object_commit_get_parent_ids(commit));
4427 if (view_is_parent_view(view))
4428 begin_x = view_split_begin_x(view->begin_x);
4429 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4430 if (diff_view == NULL) {
4431 got_object_commit_close(commit);
4432 err = got_error_from_errno("view_open");
4435 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4436 id, NULL, s->refs, s->repo);
4437 got_object_commit_close(commit);
4439 view_close(diff_view);
4442 if (view_is_parent_view(view)) {
4443 err = view_close_child(view);
4446 err = view_set_child(view, diff_view);
4448 view_close(diff_view);
4451 *focus_view = diff_view;
4452 view->child_focussed = 1;
4454 *new_view = diff_view;
4462 if (s->last_displayed_line >= s->blame.nlines &&
4463 s->selected_line >= MIN(s->blame.nlines,
4464 view->nlines - 2)) {
4467 if (s->last_displayed_line >= s->blame.nlines &&
4468 s->selected_line < view->nlines - 2) {
4469 s->selected_line = MIN(s->blame.nlines,
4473 if (s->last_displayed_line + view->nlines - 2
4475 s->first_displayed_line +=
4478 s->first_displayed_line =
4483 if (s->selected_line > view->nlines - 2) {
4484 s->selected_line = MIN(s->blame.nlines,
4491 return thread_err ? thread_err : err;
4494 static const struct got_error *
4495 cmd_blame(int argc, char *argv[])
4497 const struct got_error *error;
4498 struct got_repository *repo = NULL;
4499 struct got_reflist_head refs;
4500 struct got_worktree *worktree = NULL;
4501 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4502 char *link_target = NULL;
4503 struct got_object_id *commit_id = NULL;
4504 char *commit_id_str = NULL;
4506 struct tog_view *view;
4508 SIMPLEQ_INIT(&refs);
4511 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4516 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4519 commit_id_str = optarg;
4522 repo_path = realpath(optarg, NULL);
4523 if (repo_path == NULL)
4524 return got_error_from_errno2("realpath",
4539 cwd = getcwd(NULL, 0);
4541 return got_error_from_errno("getcwd");
4543 error = got_worktree_open(&worktree, cwd);
4544 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4547 if (repo_path == NULL) {
4550 strdup(got_worktree_get_repo_path(worktree));
4552 repo_path = strdup(cwd);
4554 if (repo_path == NULL) {
4555 error = got_error_from_errno("strdup");
4559 error = got_repo_open(&repo, repo_path, NULL);
4563 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4570 error = apply_unveil(got_repo_get_path(repo), NULL);
4574 if (commit_id_str == NULL) {
4575 struct got_reference *head_ref;
4576 error = got_ref_open(&head_ref, repo, worktree ?
4577 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4580 error = got_ref_resolve(&commit_id, repo, head_ref);
4581 got_ref_close(head_ref);
4583 error = got_repo_match_object_id(&commit_id, NULL,
4584 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4589 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4593 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4595 error = got_error_from_errno("view_open");
4599 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4604 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4605 commit_id, &refs, repo);
4609 /* Release work tree lock. */
4610 got_worktree_close(worktree);
4613 error = view_loop(view);
4621 got_worktree_close(worktree);
4623 got_repo_close(repo);
4624 got_ref_list_free(&refs);
4628 static const struct got_error *
4629 draw_tree_entries(struct tog_view *view,
4630 struct got_tree_entry **first_displayed_entry,
4631 struct got_tree_entry **last_displayed_entry,
4632 struct got_tree_entry **selected_entry, int *ndisplayed,
4633 const char *label, int show_ids, const char *parent_path,
4634 struct got_tree_object *tree, int selected, int limit,
4635 int isroot, struct tog_colors *colors, struct got_repository *repo)
4637 const struct got_error *err = NULL;
4638 struct got_tree_entry *te;
4640 struct tog_color *tc;
4641 int width, n, i, nentries;
4645 werase(view->window);
4650 err = format_line(&wline, &width, label, view->ncols, 0);
4653 if (view_needs_focus_indication(view))
4654 wstandout(view->window);
4655 tc = get_color(colors, TOG_COLOR_COMMIT);
4657 wattr_on(view->window,
4658 COLOR_PAIR(tc->colorpair), NULL);
4659 waddwstr(view->window, wline);
4661 wattr_off(view->window,
4662 COLOR_PAIR(tc->colorpair), NULL);
4663 if (view_needs_focus_indication(view))
4664 wstandend(view->window);
4667 if (width < view->ncols - 1)
4668 waddch(view->window, '\n');
4671 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4674 waddwstr(view->window, wline);
4677 if (width < view->ncols - 1)
4678 waddch(view->window, '\n');
4681 waddch(view->window, '\n');
4685 if (*first_displayed_entry == NULL) {
4686 te = got_object_tree_get_first_entry(tree);
4687 if (selected == 0) {
4689 wstandout(view->window);
4690 *selected_entry = NULL;
4692 waddstr(view->window, " ..\n"); /* parent directory */
4693 if (selected == 0 && view->focussed)
4694 wstandend(view->window);
4701 te = *first_displayed_entry;
4704 nentries = got_object_tree_get_nentries(tree);
4705 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4706 char *line = NULL, *id_str = NULL, *link_target = NULL;
4707 const char *modestr = "";
4710 te = got_object_tree_get_entry(tree, i);
4711 mode = got_tree_entry_get_mode(te);
4714 err = got_object_id_str(&id_str,
4715 got_tree_entry_get_id(te));
4717 return got_error_from_errno(
4718 "got_object_id_str");
4720 if (got_object_tree_entry_is_submodule(te))
4722 else if (S_ISLNK(mode)) {
4725 err = got_tree_entry_get_symlink_target(&link_target,
4731 for (i = 0; i < strlen(link_target); i++) {
4732 if (!isprint((unsigned char)link_target[i]))
4733 link_target[i] = '?';
4737 else if (S_ISDIR(mode))
4739 else if (mode & S_IXUSR)
4741 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4742 got_tree_entry_get_name(te), modestr,
4743 link_target ? " -> ": "",
4744 link_target ? link_target : "") == -1) {
4747 return got_error_from_errno("asprintf");
4751 err = format_line(&wline, &width, line, view->ncols, 0);
4756 if (n == selected) {
4758 wstandout(view->window);
4759 *selected_entry = te;
4761 tc = match_color(colors, line);
4763 wattr_on(view->window,
4764 COLOR_PAIR(tc->colorpair), NULL);
4765 waddwstr(view->window, wline);
4767 wattr_off(view->window,
4768 COLOR_PAIR(tc->colorpair), NULL);
4769 if (width < view->ncols - 1)
4770 waddch(view->window, '\n');
4771 if (n == selected && view->focussed)
4772 wstandend(view->window);
4778 *last_displayed_entry = te;
4787 tree_scroll_up(struct tog_view *view,
4788 struct got_tree_entry **first_displayed_entry, int maxscroll,
4789 struct got_tree_object *tree, int isroot)
4791 struct got_tree_entry *te;
4794 if (*first_displayed_entry == NULL)
4797 te = got_object_tree_get_entry(tree, 0);
4798 if (*first_displayed_entry == te) {
4800 *first_displayed_entry = NULL;
4805 while (*first_displayed_entry && i < maxscroll) {
4806 *first_displayed_entry = got_tree_entry_get_prev(tree,
4807 *first_displayed_entry);
4810 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4811 *first_displayed_entry = NULL;
4815 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4816 struct got_tree_entry *last_displayed_entry,
4817 struct got_tree_object *tree)
4819 struct got_tree_entry *next, *last;
4822 if (*first_displayed_entry)
4823 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4825 next = got_object_tree_get_first_entry(tree);
4827 last = last_displayed_entry;
4828 while (next && last && n++ < maxscroll) {
4829 last = got_tree_entry_get_next(tree, last);
4831 *first_displayed_entry = next;
4832 next = got_tree_entry_get_next(tree, next);
4838 static const struct got_error *
4839 tree_entry_path(char **path, struct tog_parent_trees *parents,
4840 struct got_tree_entry *te)
4842 const struct got_error *err = NULL;
4843 struct tog_parent_tree *pt;
4844 size_t len = 2; /* for leading slash and NUL */
4846 TAILQ_FOREACH(pt, parents, entry)
4847 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4850 len += strlen(got_tree_entry_get_name(te));
4852 *path = calloc(1, len);
4854 return got_error_from_errno("calloc");
4857 pt = TAILQ_LAST(parents, tog_parent_trees);
4859 const char *name = got_tree_entry_get_name(pt->selected_entry);
4860 if (strlcat(*path, name, len) >= len) {
4861 err = got_error(GOT_ERR_NO_SPACE);
4864 if (strlcat(*path, "/", len) >= len) {
4865 err = got_error(GOT_ERR_NO_SPACE);
4868 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4871 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4872 err = got_error(GOT_ERR_NO_SPACE);
4884 static const struct got_error *
4885 blame_tree_entry(struct tog_view **new_view, int begin_x,
4886 struct got_tree_entry *te, struct tog_parent_trees *parents,
4887 struct got_object_id *commit_id, struct got_reflist_head *refs,
4888 struct got_repository *repo)
4890 const struct got_error *err = NULL;
4892 struct tog_view *blame_view;
4896 err = tree_entry_path(&path, parents, te);
4900 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4901 if (blame_view == NULL) {
4902 err = got_error_from_errno("view_open");
4906 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4908 if (err->code == GOT_ERR_CANCELLED)
4910 view_close(blame_view);
4912 *new_view = blame_view;
4918 static const struct got_error *
4919 log_tree_entry(struct tog_view **new_view, int begin_x,
4920 struct got_tree_entry *te, struct tog_parent_trees *parents,
4921 struct got_object_id *commit_id, struct got_reflist_head *refs,
4922 struct got_repository *repo)
4924 struct tog_view *log_view;
4925 const struct got_error *err = NULL;
4930 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4931 if (log_view == NULL)
4932 return got_error_from_errno("view_open");
4934 err = tree_entry_path(&path, parents, te);
4938 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4940 view_close(log_view);
4942 *new_view = log_view;
4947 static const struct got_error *
4948 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4949 struct got_object_id *commit_id, struct got_reflist_head *refs,
4950 struct got_repository *repo)
4952 const struct got_error *err = NULL;
4953 char *commit_id_str = NULL;
4954 struct tog_tree_view_state *s = &view->state.tree;
4956 TAILQ_INIT(&s->parents);
4958 err = got_object_id_str(&commit_id_str, commit_id);
4962 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4963 err = got_error_from_errno("asprintf");
4967 s->root = s->tree = root;
4968 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4969 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4970 s->commit_id = got_object_id_dup(commit_id);
4971 if (s->commit_id == NULL) {
4972 err = got_error_from_errno("got_object_id_dup");
4978 SIMPLEQ_INIT(&s->colors);
4980 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4981 err = add_color(&s->colors, "\\$$",
4982 TOG_COLOR_TREE_SUBMODULE,
4983 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4986 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4987 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4989 free_colors(&s->colors);
4992 err = add_color(&s->colors, "/$",
4993 TOG_COLOR_TREE_DIRECTORY,
4994 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4996 free_colors(&s->colors);
5000 err = add_color(&s->colors, "\\*$",
5001 TOG_COLOR_TREE_EXECUTABLE,
5002 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5004 free_colors(&s->colors);
5008 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5009 get_color_value("TOG_COLOR_COMMIT"));
5011 free_colors(&s->colors);
5016 view->show = show_tree_view;
5017 view->input = input_tree_view;
5018 view->close = close_tree_view;
5019 view->search_start = search_start_tree_view;
5020 view->search_next = search_next_tree_view;
5022 free(commit_id_str);
5024 free(s->tree_label);
5025 s->tree_label = NULL;
5030 static const struct got_error *
5031 close_tree_view(struct tog_view *view)
5033 struct tog_tree_view_state *s = &view->state.tree;
5035 free_colors(&s->colors);
5036 free(s->tree_label);
5037 s->tree_label = NULL;
5039 s->commit_id = NULL;
5040 while (!TAILQ_EMPTY(&s->parents)) {
5041 struct tog_parent_tree *parent;
5042 parent = TAILQ_FIRST(&s->parents);
5043 TAILQ_REMOVE(&s->parents, parent, entry);
5047 if (s->tree != s->root)
5048 got_object_tree_close(s->tree);
5049 got_object_tree_close(s->root);
5054 static const struct got_error *
5055 search_start_tree_view(struct tog_view *view)
5057 struct tog_tree_view_state *s = &view->state.tree;
5059 s->matched_entry = NULL;
5064 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5066 regmatch_t regmatch;
5068 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5072 static const struct got_error *
5073 search_next_tree_view(struct tog_view *view)
5075 struct tog_tree_view_state *s = &view->state.tree;
5076 struct got_tree_entry *te = NULL;
5078 if (!view->searching) {
5079 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5083 if (s->matched_entry) {
5084 if (view->searching == TOG_SEARCH_FORWARD) {
5085 if (s->selected_entry)
5086 te = got_tree_entry_get_next(s->tree,
5089 te = got_object_tree_get_first_entry(s->tree);
5091 if (s->selected_entry == NULL)
5092 te = got_object_tree_get_last_entry(s->tree);
5094 te = got_tree_entry_get_prev(s->tree,
5098 if (view->searching == TOG_SEARCH_FORWARD)
5099 te = got_object_tree_get_first_entry(s->tree);
5101 te = got_object_tree_get_last_entry(s->tree);
5106 if (s->matched_entry == NULL) {
5107 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5110 if (view->searching == TOG_SEARCH_FORWARD)
5111 te = got_object_tree_get_first_entry(s->tree);
5113 te = got_object_tree_get_last_entry(s->tree);
5116 if (match_tree_entry(te, &view->regex)) {
5117 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5118 s->matched_entry = te;
5122 if (view->searching == TOG_SEARCH_FORWARD)
5123 te = got_tree_entry_get_next(s->tree, te);
5125 te = got_tree_entry_get_prev(s->tree, te);
5128 if (s->matched_entry) {
5129 s->first_displayed_entry = s->matched_entry;
5136 static const struct got_error *
5137 show_tree_view(struct tog_view *view)
5139 const struct got_error *err = NULL;
5140 struct tog_tree_view_state *s = &view->state.tree;
5143 err = tree_entry_path(&parent_path, &s->parents, NULL);
5147 err = draw_tree_entries(view, &s->first_displayed_entry,
5148 &s->last_displayed_entry, &s->selected_entry,
5149 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5150 s->tree, s->selected, view->nlines, s->tree == s->root,
5151 &s->colors, s->repo);
5158 static const struct got_error *
5159 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5160 struct tog_view **focus_view, struct tog_view *view, int ch)
5162 const struct got_error *err = NULL;
5163 struct tog_tree_view_state *s = &view->state.tree;
5164 struct tog_view *log_view;
5165 int begin_x = 0, nscrolled;
5169 s->show_ids = !s->show_ids;
5172 if (!s->selected_entry)
5174 if (view_is_parent_view(view))
5175 begin_x = view_split_begin_x(view->begin_x);
5176 err = log_tree_entry(&log_view, begin_x,
5177 s->selected_entry, &s->parents,
5178 s->commit_id, s->refs, s->repo);
5179 if (view_is_parent_view(view)) {
5180 err = view_close_child(view);
5183 err = view_set_child(view, log_view);
5185 view_close(log_view);
5188 *focus_view = log_view;
5189 view->child_focussed = 1;
5191 *new_view = log_view;
5195 if (s->selected > 0) {
5197 if (s->selected == 0)
5200 if (s->selected > 0)
5202 tree_scroll_up(view, &s->first_displayed_entry, 1,
5203 s->tree, s->tree == s->root);
5207 tree_scroll_up(view, &s->first_displayed_entry,
5208 MAX(0, view->nlines - 4 - s->selected), s->tree,
5209 s->tree == s->root);
5211 if (got_object_tree_get_first_entry(s->tree) ==
5212 s->first_displayed_entry && s->tree != s->root)
5213 s->first_displayed_entry = NULL;
5217 if (s->selected < s->ndisplayed - 1) {
5221 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5223 /* can't scroll any further */
5225 tree_scroll_down(&s->first_displayed_entry, 1,
5226 s->last_displayed_entry, s->tree);
5230 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5232 /* can't scroll any further; move cursor down */
5233 if (s->selected < s->ndisplayed - 1)
5234 s->selected = s->ndisplayed - 1;
5237 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5238 view->nlines, s->last_displayed_entry, s->tree);
5239 if (nscrolled < view->nlines) {
5241 struct got_tree_entry *te;
5242 te = s->first_displayed_entry;
5245 te = got_tree_entry_get_next(s->tree, te);
5247 s->selected = ndisplayed - 1;
5253 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5254 struct tog_parent_tree *parent;
5255 /* user selected '..' */
5256 if (s->tree == s->root)
5258 parent = TAILQ_FIRST(&s->parents);
5259 TAILQ_REMOVE(&s->parents, parent,
5261 got_object_tree_close(s->tree);
5262 s->tree = parent->tree;
5263 s->first_displayed_entry =
5264 parent->first_displayed_entry;
5266 parent->selected_entry;
5267 s->selected = parent->selected;
5269 } else if (S_ISDIR(got_tree_entry_get_mode(
5270 s->selected_entry))) {
5271 struct got_tree_object *subtree;
5272 err = got_object_open_as_tree(&subtree, s->repo,
5273 got_tree_entry_get_id(s->selected_entry));
5276 err = tree_view_visit_subtree(subtree, s);
5278 got_object_tree_close(subtree);
5281 } else if (S_ISREG(got_tree_entry_get_mode(
5282 s->selected_entry))) {
5283 struct tog_view *blame_view;
5284 int begin_x = view_is_parent_view(view) ?
5285 view_split_begin_x(view->begin_x) : 0;
5287 err = blame_tree_entry(&blame_view, begin_x,
5288 s->selected_entry, &s->parents,
5289 s->commit_id, s->refs, s->repo);
5292 if (view_is_parent_view(view)) {
5293 err = view_close_child(view);
5296 err = view_set_child(view, blame_view);
5298 view_close(blame_view);
5301 *focus_view = blame_view;
5302 view->child_focussed = 1;
5304 *new_view = blame_view;
5308 if (s->selected > view->nlines)
5309 s->selected = s->ndisplayed - 1;
5322 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5327 static const struct got_error *
5328 cmd_tree(int argc, char *argv[])
5330 const struct got_error *error;
5331 struct got_repository *repo = NULL;
5332 struct got_worktree *worktree = NULL;
5333 struct got_reflist_head refs;
5334 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5335 struct got_object_id *commit_id = NULL;
5336 char *commit_id_arg = NULL;
5337 struct got_commit_object *commit = NULL;
5338 struct got_tree_object *tree = NULL;
5340 struct tog_view *view;
5342 SIMPLEQ_INIT(&refs);
5345 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5350 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5353 commit_id_arg = optarg;
5356 repo_path = realpath(optarg, NULL);
5357 if (repo_path == NULL)
5358 return got_error_from_errno2("realpath",
5373 cwd = getcwd(NULL, 0);
5375 return got_error_from_errno("getcwd");
5377 error = got_worktree_open(&worktree, cwd);
5378 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5381 if (repo_path == NULL) {
5384 strdup(got_worktree_get_repo_path(worktree));
5386 repo_path = strdup(cwd);
5388 if (repo_path == NULL) {
5389 error = got_error_from_errno("strdup");
5393 error = got_repo_open(&repo, repo_path, NULL);
5397 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5404 error = apply_unveil(got_repo_get_path(repo), NULL);
5408 error = got_repo_match_object_id(&commit_id, NULL,
5409 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5410 GOT_OBJ_TYPE_COMMIT, 1, repo);
5414 error = got_object_open_as_commit(&commit, repo, commit_id);
5418 error = got_object_open_as_tree(&tree, repo,
5419 got_object_commit_get_tree_id(commit));
5423 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5427 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5429 error = got_error_from_errno("view_open");
5432 error = open_tree_view(view, tree, commit_id, &refs, repo);
5435 if (!got_path_is_root_dir(in_repo_path)) {
5436 error = tree_view_walk_path(&view->state.tree, commit_id,
5437 in_repo_path, repo);
5443 /* Release work tree lock. */
5444 got_worktree_close(worktree);
5447 error = view_loop(view);
5453 got_object_commit_close(commit);
5455 got_object_tree_close(tree);
5457 got_repo_close(repo);
5458 got_ref_list_free(&refs);
5467 fprintf(stderr, "commands:");
5468 for (i = 0; i < nitems(tog_commands); i++) {
5469 struct tog_cmd *cmd = &tog_commands[i];
5470 fprintf(stderr, " %s", cmd->name);
5472 fputc('\n', stderr);
5478 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] "
5479 "[arg ...]\n", getprogname());
5481 fprintf(stderr, "lazy usage: %s path\n", getprogname());
5488 make_argv(int argc, ...)
5496 argv = calloc(argc, sizeof(char *));
5499 for (i = 0; i < argc; i++) {
5500 argv[i] = strdup(va_arg(ap, char *));
5501 if (argv[i] == NULL)
5510 * Try to convert 'tog path' into a 'tog log path' command.
5511 * The user could simply have mistyped the command rather than knowingly
5512 * provided a path. So check whether argv[0] can in fact be resolved
5513 * to a path in the HEAD commit and print a special error if not.
5514 * This hack is for mpi@ <3
5516 static const struct got_error *
5517 tog_log_with_path(int argc, char *argv[])
5519 const struct got_error *error = NULL;
5520 struct tog_cmd *cmd = NULL;
5521 struct got_repository *repo = NULL;
5522 struct got_worktree *worktree = NULL;
5523 struct got_object_id *commit_id = NULL, *id = NULL;
5524 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5525 char *commit_id_str = NULL, **cmd_argv = NULL;
5527 cwd = getcwd(NULL, 0);
5529 return got_error_from_errno("getcwd");
5531 error = got_worktree_open(&worktree, cwd);
5532 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5536 repo_path = strdup(got_worktree_get_repo_path(worktree));
5538 repo_path = strdup(cwd);
5539 if (repo_path == NULL) {
5540 error = got_error_from_errno("strdup");
5544 error = got_repo_open(&repo, repo_path, NULL);
5548 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5553 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
5554 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
5555 GOT_OBJ_TYPE_COMMIT, 1, repo);
5560 got_worktree_close(worktree);
5564 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
5566 if (error->code != GOT_ERR_NO_TREE_ENTRY)
5568 fprintf(stderr, "%s: '%s' is no known command or path\n",
5569 getprogname(), argv[0]);
5574 got_repo_close(repo);
5577 error = got_object_id_str(&commit_id_str, commit_id);
5581 cmd = &tog_commands[0]; /* log */
5583 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
5584 error = cmd->cmd_main(argc, cmd_argv);
5587 got_repo_close(repo);
5589 got_worktree_close(worktree);
5591 free(commit_id_str);
5598 for (i = 0; i < argc; i++)
5606 main(int argc, char *argv[])
5608 const struct got_error *error = NULL;
5609 struct tog_cmd *cmd = NULL;
5610 int ch, hflag = 0, Vflag = 0;
5611 char **cmd_argv = NULL;
5612 static struct option longopts[] = {
5613 { "version", no_argument, NULL, 'V' },
5617 setlocale(LC_CTYPE, "");
5619 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5639 got_version_print_str();
5646 /* Build an argument vector which runs a default command. */
5647 cmd = &tog_commands[0];
5649 cmd_argv = make_argv(argc, cmd->name);
5653 /* Did the user specify a command? */
5654 for (i = 0; i < nitems(tog_commands); i++) {
5655 if (strncmp(tog_commands[i].name, argv[0],
5656 strlen(argv[0])) == 0) {
5657 cmd = &tog_commands[i];
5666 /* No command specified; try log with a path */
5667 error = tog_log_with_path(argc, argv);
5672 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5678 for (i = 0; i < argc; i++)
5683 if (error && error->code != GOT_ERR_CANCELLED)
5684 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);