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, 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;
253 struct got_repository *repo;
254 struct got_reflist_head *refs;
255 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
448 static const struct got_error *open_diff_view(struct tog_view *,
449 struct got_object_id *, struct got_object_id *, int, struct tog_view *,
450 struct got_reflist_head *, struct got_repository *);
451 static const struct got_error *show_diff_view(struct tog_view *);
452 static const struct got_error *input_diff_view(struct tog_view **,
453 struct tog_view **, struct tog_view **, struct tog_view *, int);
454 static const struct got_error* close_diff_view(struct tog_view *);
455 static const struct got_error *search_start_diff_view(struct tog_view *);
456 static const struct got_error *search_next_diff_view(struct tog_view *);
458 static const struct got_error *open_log_view(struct tog_view *,
459 struct got_object_id *, struct got_reflist_head *,
460 struct got_repository *, const char *, const char *, int);
461 static const struct got_error * show_log_view(struct tog_view *);
462 static const struct got_error *input_log_view(struct tog_view **,
463 struct tog_view **, struct tog_view **, struct tog_view *, int);
464 static const struct got_error *close_log_view(struct tog_view *);
465 static const struct got_error *search_start_log_view(struct tog_view *);
466 static const struct got_error *search_next_log_view(struct tog_view *);
468 static const struct got_error *open_blame_view(struct tog_view *, char *,
469 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
470 static const struct got_error *show_blame_view(struct tog_view *);
471 static const struct got_error *input_blame_view(struct tog_view **,
472 struct tog_view **, struct tog_view **, struct tog_view *, int);
473 static const struct got_error *close_blame_view(struct tog_view *);
474 static const struct got_error *search_start_blame_view(struct tog_view *);
475 static const struct got_error *search_next_blame_view(struct tog_view *);
477 static const struct got_error *open_tree_view(struct tog_view *,
478 struct got_tree_object *, struct got_object_id *, struct got_reflist_head *,
479 struct got_repository *);
480 static const struct got_error *show_tree_view(struct tog_view *);
481 static const struct got_error *input_tree_view(struct tog_view **,
482 struct tog_view **, struct tog_view **, struct tog_view *, int);
483 static const struct got_error *close_tree_view(struct tog_view *);
484 static const struct got_error *search_start_tree_view(struct tog_view *);
485 static const struct got_error *search_next_tree_view(struct tog_view *);
487 static volatile sig_atomic_t tog_sigwinch_received;
488 static volatile sig_atomic_t tog_sigpipe_received;
489 static volatile sig_atomic_t tog_sigcont_received;
492 tog_sigwinch(int signo)
494 tog_sigwinch_received = 1;
498 tog_sigpipe(int signo)
500 tog_sigpipe_received = 1;
504 tog_sigcont(int signo)
506 tog_sigcont_received = 1;
509 static const struct got_error *
510 view_close(struct tog_view *view)
512 const struct got_error *err = NULL;
515 view_close(view->child);
519 err = view->close(view);
521 del_panel(view->panel);
523 delwin(view->window);
528 static struct tog_view *
529 view_open(int nlines, int ncols, int begin_y, int begin_x,
530 enum tog_view_type type)
532 struct tog_view *view = calloc(1, sizeof(*view));
540 view->nlines = nlines ? nlines : LINES - begin_y;
541 view->ncols = ncols ? ncols : COLS - begin_x;
542 view->begin_y = begin_y;
543 view->begin_x = begin_x;
544 view->window = newwin(nlines, ncols, begin_y, begin_x);
545 if (view->window == NULL) {
549 view->panel = new_panel(view->window);
550 if (view->panel == NULL ||
551 set_panel_userptr(view->panel, view) != OK) {
556 keypad(view->window, TRUE);
561 view_split_begin_x(int begin_x)
563 if (begin_x > 0 || COLS < 120)
565 return (COLS - MAX(COLS / 2, 80));
568 static const struct got_error *view_resize(struct tog_view *);
570 static const struct got_error *
571 view_splitscreen(struct tog_view *view)
573 const struct got_error *err = NULL;
576 view->begin_x = view_split_begin_x(0);
577 view->nlines = LINES;
578 view->ncols = COLS - view->begin_x;
581 err = view_resize(view);
585 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
586 return got_error_from_errno("mvwin");
591 static const struct got_error *
592 view_fullscreen(struct tog_view *view)
594 const struct got_error *err = NULL;
598 view->nlines = LINES;
602 err = view_resize(view);
606 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
607 return got_error_from_errno("mvwin");
613 view_is_parent_view(struct tog_view *view)
615 return view->parent == NULL;
618 static const struct got_error *
619 view_resize(struct tog_view *view)
623 if (view->lines > LINES)
624 nlines = view->nlines - (view->lines - LINES);
626 nlines = view->nlines + (LINES - view->lines);
628 if (view->cols > COLS)
629 ncols = view->ncols - (view->cols - COLS);
631 ncols = view->ncols + (COLS - view->cols);
633 if (wresize(view->window, nlines, ncols) == ERR)
634 return got_error_from_errno("wresize");
635 if (replace_panel(view->panel, view->window) == ERR)
636 return got_error_from_errno("replace_panel");
637 wclear(view->window);
639 view->nlines = nlines;
645 view->child->begin_x = view_split_begin_x(view->begin_x);
646 if (view->child->begin_x == 0) {
647 view_fullscreen(view->child);
648 if (view->child->focussed)
649 show_panel(view->child->panel);
651 show_panel(view->panel);
653 view_splitscreen(view->child);
654 show_panel(view->child->panel);
661 static const struct got_error *
662 view_close_child(struct tog_view *view)
664 const struct got_error *err = NULL;
666 if (view->child == NULL)
669 err = view_close(view->child);
674 static const struct got_error *
675 view_set_child(struct tog_view *view, struct tog_view *child)
677 const struct got_error *err = NULL;
680 child->parent = view;
685 view_is_splitscreen(struct tog_view *view)
687 return view->begin_x > 0;
696 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
697 cols = 80; /* Default */
703 resize_term(lines, cols);
706 static const struct got_error *
707 view_search_start(struct tog_view *view)
709 const struct got_error *err = NULL;
713 if (view->nlines < 1)
716 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
717 wclrtoeol(view->window);
721 ret = wgetnstr(view->window, pattern, sizeof(pattern));
727 if (view->searching) {
728 regfree(&view->regex);
732 if (regcomp(&view->regex, pattern, REG_EXTENDED | 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;
1166 struct got_object_id *ref_id;
1169 name = got_ref_get_name(re->ref);
1170 if (strcmp(name, GOT_REF_HEAD) == 0)
1172 if (strncmp(name, "refs/", 5) == 0)
1174 if (strncmp(name, "got/", 4) == 0)
1176 if (strncmp(name, "heads/", 6) == 0)
1178 if (strncmp(name, "remotes/", 8) == 0) {
1180 s = strstr(name, "/" GOT_REF_HEAD);
1181 if (s != NULL && s[strlen(s)] == '\0')
1184 err = got_ref_resolve(&ref_id, repo, re->ref);
1187 if (strncmp(name, "tags/", 5) == 0) {
1188 err = got_object_open_as_tag(&tag, repo, ref_id);
1190 if (err->code != GOT_ERR_OBJ_TYPE) {
1194 /* Ref points at something other than a tag. */
1199 cmp = got_object_id_cmp(tag ?
1200 got_object_tag_get_object_id(tag) : ref_id, id);
1203 got_object_tag_close(tag);
1207 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1208 s ? ", " : "", name) == -1) {
1209 err = got_error_from_errno("asprintf");
1220 static const struct got_error *
1221 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1224 char *smallerthan, *at;
1226 smallerthan = strchr(author, '<');
1227 if (smallerthan && smallerthan[1] != '\0')
1228 author = smallerthan + 1;
1229 at = strchr(author, '@');
1232 return format_line(wauthor, author_width, author, limit, col_tab_align);
1235 static const struct got_error *
1236 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1237 struct got_object_id *id, struct got_reflist_head *refs,
1238 const size_t date_display_cols, int author_display_cols,
1239 struct tog_colors *colors)
1241 const struct got_error *err = NULL;
1242 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1243 char *logmsg0 = NULL, *logmsg = NULL;
1244 char *author = NULL;
1245 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1246 int author_width, logmsg_width;
1247 char *newline, *line = NULL;
1249 const int avail = view->ncols;
1251 time_t committer_time;
1252 struct tog_color *tc;
1254 committer_time = got_object_commit_get_committer_time(commit);
1255 if (localtime_r(&committer_time, &tm) == NULL)
1256 return got_error_from_errno("localtime_r");
1257 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1259 return got_error(GOT_ERR_NO_SPACE);
1261 if (avail <= date_display_cols)
1262 limit = MIN(sizeof(datebuf) - 1, avail);
1264 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1265 tc = get_color(colors, TOG_COLOR_DATE);
1267 wattr_on(view->window,
1268 COLOR_PAIR(tc->colorpair), NULL);
1269 waddnstr(view->window, datebuf, limit);
1271 wattr_off(view->window,
1272 COLOR_PAIR(tc->colorpair), NULL);
1279 err = got_object_id_str(&id_str, id);
1282 tc = get_color(colors, TOG_COLOR_COMMIT);
1284 wattr_on(view->window,
1285 COLOR_PAIR(tc->colorpair), NULL);
1286 wprintw(view->window, "%.8s ", id_str);
1288 wattr_off(view->window,
1289 COLOR_PAIR(tc->colorpair), NULL);
1296 author = strdup(got_object_commit_get_author(commit));
1297 if (author == NULL) {
1298 err = got_error_from_errno("strdup");
1301 err = format_author(&wauthor, &author_width, author, avail - col, col);
1304 tc = get_color(colors, TOG_COLOR_AUTHOR);
1306 wattr_on(view->window,
1307 COLOR_PAIR(tc->colorpair), NULL);
1308 waddwstr(view->window, wauthor);
1310 wattr_off(view->window,
1311 COLOR_PAIR(tc->colorpair), NULL);
1312 col += author_width;
1313 while (col < avail && author_width < author_display_cols + 2) {
1314 waddch(view->window, ' ');
1321 err = got_object_commit_get_logmsg(&logmsg0, commit);
1325 while (*logmsg == '\n')
1327 newline = strchr(logmsg, '\n');
1330 limit = avail - col;
1331 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1334 waddwstr(view->window, wlogmsg);
1335 col += logmsg_width;
1336 while (col < avail) {
1337 waddch(view->window, ' ');
1349 static struct commit_queue_entry *
1350 alloc_commit_queue_entry(struct got_commit_object *commit,
1351 struct got_object_id *id)
1353 struct commit_queue_entry *entry;
1355 entry = calloc(1, sizeof(*entry));
1360 entry->commit = commit;
1365 pop_commit(struct commit_queue *commits)
1367 struct commit_queue_entry *entry;
1369 entry = TAILQ_FIRST(&commits->head);
1370 TAILQ_REMOVE(&commits->head, entry, entry);
1371 got_object_commit_close(entry->commit);
1372 commits->ncommits--;
1373 /* Don't free entry->id! It is owned by the commit graph. */
1378 free_commits(struct commit_queue *commits)
1380 while (!TAILQ_EMPTY(&commits->head))
1381 pop_commit(commits);
1384 static const struct got_error *
1385 match_commit(int *have_match, struct got_object_id *id,
1386 struct got_commit_object *commit, regex_t *regex)
1388 const struct got_error *err = NULL;
1389 regmatch_t regmatch;
1390 char *id_str = NULL, *logmsg = NULL;
1394 err = got_object_id_str(&id_str, id);
1398 err = got_object_commit_get_logmsg(&logmsg, commit);
1402 if (regexec(regex, got_object_commit_get_author(commit), 1,
1403 ®match, 0) == 0 ||
1404 regexec(regex, got_object_commit_get_committer(commit), 1,
1405 ®match, 0) == 0 ||
1406 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1407 regexec(regex, logmsg, 1, ®match, 0) == 0)
1415 static const struct got_error *
1416 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1417 int minqueue, struct got_repository *repo, const char *path,
1418 int *searching, int *search_next_done, regex_t *regex)
1420 const struct got_error *err = NULL;
1424 * We keep all commits open throughout the lifetime of the log
1425 * view in order to avoid having to re-fetch commits from disk
1426 * while updating the display.
1428 while (nqueued < minqueue ||
1429 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1430 struct got_object_id *id;
1431 struct got_commit_object *commit;
1432 struct commit_queue_entry *entry;
1435 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1436 if (err || id == NULL)
1439 err = got_object_open_as_commit(&commit, repo, id);
1442 entry = alloc_commit_queue_entry(commit, id);
1443 if (entry == NULL) {
1444 err = got_error_from_errno("alloc_commit_queue_entry");
1448 errcode = pthread_mutex_lock(&tog_mutex);
1450 err = got_error_set_errno(errcode,
1451 "pthread_mutex_lock");
1455 entry->idx = commits->ncommits;
1456 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1458 commits->ncommits++;
1460 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1462 err = match_commit(&have_match, id, commit, regex);
1466 *search_next_done = TOG_SEARCH_HAVE_MORE;
1469 errcode = pthread_mutex_unlock(&tog_mutex);
1470 if (errcode && err == NULL)
1471 err = got_error_set_errno(errcode,
1472 "pthread_mutex_unlock");
1480 static const struct got_error *
1481 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1482 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1483 struct commit_queue *commits, int selected_idx, int limit,
1484 struct got_reflist_head *refs, const char *path, int commits_needed,
1485 struct tog_colors *colors)
1487 const struct got_error *err = NULL;
1488 struct tog_log_view_state *s = &view->state.log;
1489 struct commit_queue_entry *entry;
1491 int ncommits, author_cols = 4;
1492 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1493 char *refs_str = NULL;
1495 struct tog_color *tc;
1496 static const size_t date_display_cols = 12;
1501 if (ncommits == selected_idx) {
1505 entry = TAILQ_NEXT(entry, entry);
1509 if (*selected && !(view->searching && view->search_next_done == 0)) {
1510 err = got_object_id_str(&id_str, (*selected)->id);
1514 err = build_refs_str(&refs_str, refs, (*selected)->id,
1521 if (commits_needed == 0)
1522 halfdelay(10); /* disable fast refresh */
1524 if (commits_needed > 0) {
1525 if (asprintf(&ncommits_str, " [%d/%d] %s",
1526 entry ? entry->idx + 1 : 0, commits->ncommits,
1527 (view->searching && !view->search_next_done) ?
1528 "searching..." : "loading...") == -1) {
1529 err = got_error_from_errno("asprintf");
1533 const char *search_str = NULL;
1535 if (view->searching) {
1536 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1537 search_str = "no more matches";
1538 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1539 search_str = "no matches found";
1540 else if (!view->search_next_done)
1541 search_str = "searching...";
1544 if (asprintf(&ncommits_str, " [%d/%d] %s",
1545 entry ? entry->idx + 1 : 0, commits->ncommits,
1546 search_str ? search_str :
1547 (refs_str ? refs_str : "")) == -1) {
1548 err = got_error_from_errno("asprintf");
1553 if (path && strcmp(path, "/") != 0) {
1554 if (asprintf(&header, "commit %s %s%s",
1555 id_str ? id_str : "........................................",
1556 path, ncommits_str) == -1) {
1557 err = got_error_from_errno("asprintf");
1561 } else if (asprintf(&header, "commit %s%s",
1562 id_str ? id_str : "........................................",
1563 ncommits_str) == -1) {
1564 err = got_error_from_errno("asprintf");
1568 err = format_line(&wline, &width, header, view->ncols, 0);
1572 werase(view->window);
1574 if (view_needs_focus_indication(view))
1575 wstandout(view->window);
1576 tc = get_color(colors, TOG_COLOR_COMMIT);
1578 wattr_on(view->window,
1579 COLOR_PAIR(tc->colorpair), NULL);
1580 waddwstr(view->window, wline);
1582 wattr_off(view->window,
1583 COLOR_PAIR(tc->colorpair), NULL);
1584 while (width < view->ncols) {
1585 waddch(view->window, ' ');
1588 if (view_needs_focus_indication(view))
1589 wstandend(view->window);
1594 /* Grow author column size if necessary. */
1601 if (ncommits >= limit - 1)
1603 author = strdup(got_object_commit_get_author(entry->commit));
1604 if (author == NULL) {
1605 err = got_error_from_errno("strdup");
1608 err = format_author(&wauthor, &width, author, COLS,
1610 if (author_cols < width)
1611 author_cols = width;
1615 entry = TAILQ_NEXT(entry, entry);
1622 if (ncommits >= limit - 1)
1624 if (ncommits == selected_idx)
1625 wstandout(view->window);
1626 err = draw_commit(view, entry->commit, entry->id, refs,
1627 date_display_cols, author_cols, colors);
1628 if (ncommits == selected_idx)
1629 wstandend(view->window);
1634 entry = TAILQ_NEXT(entry, entry);
1647 scroll_up(struct tog_view *view,
1648 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1649 struct commit_queue *commits)
1651 struct commit_queue_entry *entry;
1654 entry = TAILQ_FIRST(&commits->head);
1655 if (*first_displayed_entry == entry)
1658 entry = *first_displayed_entry;
1659 while (entry && nscrolled < maxscroll) {
1660 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1662 *first_displayed_entry = entry;
1668 static const struct got_error *
1669 trigger_log_thread(struct tog_view *log_view, int wait,
1670 int *commits_needed, int *log_complete,
1671 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1675 halfdelay(1); /* fast refresh while loading commits */
1677 while (*commits_needed > 0) {
1681 /* Wake the log thread. */
1682 errcode = pthread_cond_signal(need_commits);
1684 return got_error_set_errno(errcode,
1685 "pthread_cond_signal");
1688 * The mutex will be released while the view loop waits
1689 * in wgetch(), at which time the log thread will run.
1694 /* Display progress update in log view. */
1695 show_log_view(log_view);
1699 /* Wait right here while next commit is being loaded. */
1700 errcode = pthread_cond_wait(commit_loaded, &tog_mutex);
1702 return got_error_set_errno(errcode,
1703 "pthread_cond_wait");
1705 /* Display progress update in log view. */
1706 show_log_view(log_view);
1714 static const struct got_error *
1715 scroll_down(struct tog_view *log_view,
1716 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1717 struct commit_queue_entry **last_displayed_entry,
1718 struct commit_queue *commits, int *log_complete, int *commits_needed,
1719 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1721 const struct got_error *err = NULL;
1722 struct commit_queue_entry *pentry;
1723 int nscrolled = 0, ncommits_needed;
1725 if (*last_displayed_entry == NULL)
1728 ncommits_needed = (*last_displayed_entry)->idx + 1 + maxscroll;
1729 if (commits->ncommits < ncommits_needed && !*log_complete) {
1731 * Ask the log thread for required amount of commits.
1733 (*commits_needed) += maxscroll;
1734 err = trigger_log_thread(log_view, 1, commits_needed,
1735 log_complete, need_commits, commit_loaded);
1741 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1745 *last_displayed_entry = pentry;
1747 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1750 *first_displayed_entry = pentry;
1751 } while (++nscrolled < maxscroll);
1756 static const struct got_error *
1757 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1758 struct got_commit_object *commit, struct got_object_id *commit_id,
1759 struct tog_view *log_view, struct got_reflist_head *refs,
1760 struct got_repository *repo)
1762 const struct got_error *err;
1763 struct got_object_qid *parent_id;
1764 struct tog_view *diff_view;
1766 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1767 if (diff_view == NULL)
1768 return got_error_from_errno("view_open");
1770 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1771 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1772 commit_id, 0, log_view, refs, repo);
1774 *new_view = diff_view;
1778 static const struct got_error *
1779 tree_view_visit_subtree(struct got_tree_object *subtree,
1780 struct tog_tree_view_state *s)
1782 struct tog_parent_tree *parent;
1784 parent = calloc(1, sizeof(*parent));
1786 return got_error_from_errno("calloc");
1788 parent->tree = s->tree;
1789 parent->first_displayed_entry = s->first_displayed_entry;
1790 parent->selected_entry = s->selected_entry;
1791 parent->selected = s->selected;
1792 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1795 s->first_displayed_entry = NULL;
1799 static const struct got_error *
1800 tree_view_walk_path(struct tog_tree_view_state *s,
1801 struct got_object_id *commit_id,
1802 const char *path, struct got_repository *repo)
1804 const struct got_error *err = NULL;
1805 struct got_tree_object *tree = NULL;
1807 char *slash, *subpath = NULL;
1809 /* Walk the path and open corresponding tree objects. */
1812 struct got_tree_entry *te;
1813 struct got_object_id *tree_id;
1819 /* Ensure the correct subtree entry is selected. */
1820 slash = strchr(p, '/');
1822 te_name = strdup(p);
1824 te_name = strndup(p, slash - p);
1825 if (te_name == NULL) {
1826 err = got_error_from_errno("strndup");
1829 te = got_object_tree_find_entry(s->tree, te_name);
1831 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1836 s->selected_entry = te;
1837 s->selected = got_tree_entry_get_index(te);
1838 if (s->tree != s->root)
1839 s->selected++; /* skip '..' */
1841 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1842 /* Jump to this file's entry. */
1843 s->first_displayed_entry = s->selected_entry;
1848 slash = strchr(p, '/');
1850 subpath = strndup(path, slash - path);
1852 subpath = strdup(path);
1853 if (subpath == NULL) {
1854 err = got_error_from_errno("strdup");
1858 err = got_object_id_by_path(&tree_id, repo, commit_id,
1863 err = got_object_open_as_tree(&tree, repo, tree_id);
1868 err = tree_view_visit_subtree(tree, s);
1870 got_object_tree_close(tree);
1884 static const struct got_error *
1885 browse_commit_tree(struct tog_view **new_view, int begin_x,
1886 struct commit_queue_entry *entry, const char *path,
1887 struct got_reflist_head *refs, struct got_repository *repo)
1889 const struct got_error *err = NULL;
1890 struct got_tree_object *tree;
1891 struct tog_tree_view_state *s;
1892 struct tog_view *tree_view;
1894 err = got_object_open_as_tree(&tree, repo,
1895 got_object_commit_get_tree_id(entry->commit));
1899 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1900 if (tree_view == NULL)
1901 return got_error_from_errno("view_open");
1903 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1905 got_object_tree_close(tree);
1908 s = &tree_view->state.tree;
1910 *new_view = tree_view;
1912 if (got_path_is_root_dir(path))
1915 return tree_view_walk_path(s, entry->id, path, repo);
1918 static const struct got_error *
1919 block_signals_used_by_main_thread(void)
1924 if (sigemptyset(&sigset) == -1)
1925 return got_error_from_errno("sigemptyset");
1927 /* tog handles SIGWINCH and SIGCONT */
1928 if (sigaddset(&sigset, SIGWINCH) == -1)
1929 return got_error_from_errno("sigaddset");
1930 if (sigaddset(&sigset, SIGCONT) == -1)
1931 return got_error_from_errno("sigaddset");
1933 /* ncurses handles SIGTSTP */
1934 if (sigaddset(&sigset, SIGTSTP) == -1)
1935 return got_error_from_errno("sigaddset");
1937 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1939 return got_error_set_errno(errcode, "pthread_sigmask");
1945 log_thread(void *arg)
1947 const struct got_error *err = NULL;
1949 struct tog_log_thread_args *a = arg;
1952 err = block_signals_used_by_main_thread();
1956 while (!done && !err && !tog_sigpipe_received) {
1957 err = queue_commits(a->graph, a->commits, 1, a->repo,
1958 a->in_repo_path, a->searching, a->search_next_done,
1961 if (err->code != GOT_ERR_ITER_COMPLETED)
1965 } else if (a->commits_needed > 0)
1966 a->commits_needed--;
1968 errcode = pthread_mutex_lock(&tog_mutex);
1970 err = got_error_set_errno(errcode,
1971 "pthread_mutex_lock");
1973 } else if (*a->quit)
1975 else if (*a->first_displayed_entry == NULL) {
1976 *a->first_displayed_entry =
1977 TAILQ_FIRST(&a->commits->head);
1978 *a->selected_entry = *a->first_displayed_entry;
1981 errcode = pthread_cond_signal(&a->commit_loaded);
1983 err = got_error_set_errno(errcode,
1984 "pthread_cond_signal");
1985 pthread_mutex_unlock(&tog_mutex);
1990 a->commits_needed = 0;
1992 if (a->commits_needed == 0) {
1993 errcode = pthread_cond_wait(&a->need_commits,
1996 err = got_error_set_errno(errcode,
1997 "pthread_cond_wait");
2001 errcode = pthread_mutex_unlock(&tog_mutex);
2002 if (errcode && err == NULL)
2003 err = got_error_set_errno(errcode,
2004 "pthread_mutex_unlock");
2006 a->log_complete = 1;
2010 static const struct got_error *
2011 stop_log_thread(struct tog_log_view_state *s)
2013 const struct got_error *err = NULL;
2018 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2020 return got_error_set_errno(errcode,
2021 "pthread_cond_signal");
2022 errcode = pthread_mutex_unlock(&tog_mutex);
2024 return got_error_set_errno(errcode,
2025 "pthread_mutex_unlock");
2026 errcode = pthread_join(s->thread, (void **)&err);
2028 return got_error_set_errno(errcode, "pthread_join");
2029 errcode = pthread_mutex_lock(&tog_mutex);
2031 return got_error_set_errno(errcode,
2032 "pthread_mutex_lock");
2036 if (s->thread_args.repo) {
2037 got_repo_close(s->thread_args.repo);
2038 s->thread_args.repo = NULL;
2041 if (s->thread_args.graph) {
2042 got_commit_graph_close(s->thread_args.graph);
2043 s->thread_args.graph = NULL;
2049 static const struct got_error *
2050 close_log_view(struct tog_view *view)
2052 const struct got_error *err = NULL;
2053 struct tog_log_view_state *s = &view->state.log;
2056 err = stop_log_thread(s);
2058 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2059 if (errcode && err == NULL)
2060 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2062 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2063 if (errcode && err == NULL)
2064 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2066 free_commits(&s->commits);
2067 free(s->in_repo_path);
2068 s->in_repo_path = NULL;
2074 static const struct got_error *
2075 search_start_log_view(struct tog_view *view)
2077 struct tog_log_view_state *s = &view->state.log;
2079 s->matched_entry = NULL;
2080 s->search_entry = NULL;
2084 static const struct got_error *
2085 search_next_log_view(struct tog_view *view)
2087 const struct got_error *err = NULL;
2088 struct tog_log_view_state *s = &view->state.log;
2089 struct commit_queue_entry *entry;
2091 /* Display progress update in log view. */
2092 show_log_view(view);
2096 if (s->search_entry) {
2098 errcode = pthread_mutex_unlock(&tog_mutex);
2100 return got_error_set_errno(errcode,
2101 "pthread_mutex_unlock");
2102 ch = wgetch(view->window);
2103 errcode = pthread_mutex_lock(&tog_mutex);
2105 return got_error_set_errno(errcode,
2106 "pthread_mutex_lock");
2107 if (ch == KEY_BACKSPACE) {
2108 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2111 if (view->searching == TOG_SEARCH_FORWARD)
2112 entry = TAILQ_NEXT(s->search_entry, entry);
2114 entry = TAILQ_PREV(s->search_entry,
2115 commit_queue_head, entry);
2116 } else if (s->matched_entry) {
2117 if (view->searching == TOG_SEARCH_FORWARD)
2118 entry = TAILQ_NEXT(s->matched_entry, entry);
2120 entry = TAILQ_PREV(s->matched_entry,
2121 commit_queue_head, entry);
2123 if (view->searching == TOG_SEARCH_FORWARD)
2124 entry = TAILQ_FIRST(&s->commits.head);
2126 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2132 if (entry == NULL) {
2133 if (s->thread_args.log_complete ||
2134 view->searching == TOG_SEARCH_BACKWARD) {
2135 view->search_next_done =
2136 (s->matched_entry == NULL ?
2137 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2138 s->search_entry = NULL;
2142 * Poke the log thread for more commits and return,
2143 * allowing the main loop to make progress. Search
2144 * will resume at s->search_entry once we come back.
2146 s->thread_args.commits_needed++;
2147 return trigger_log_thread(view, 0,
2148 &s->thread_args.commits_needed,
2149 &s->thread_args.log_complete,
2150 &s->thread_args.need_commits,
2151 &s->thread_args.commit_loaded);
2154 err = match_commit(&have_match, entry->id, entry->commit,
2159 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2160 s->matched_entry = entry;
2164 s->search_entry = entry;
2165 if (view->searching == TOG_SEARCH_FORWARD)
2166 entry = TAILQ_NEXT(entry, entry);
2168 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2171 if (s->matched_entry) {
2172 int cur = s->selected_entry->idx;
2173 while (cur < s->matched_entry->idx) {
2174 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2179 while (cur > s->matched_entry->idx) {
2180 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2187 s->search_entry = NULL;
2192 static const struct got_error *
2193 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2194 struct got_reflist_head *refs, struct got_repository *repo,
2195 const char *head_ref_name, const char *in_repo_path,
2198 const struct got_error *err = NULL;
2199 struct tog_log_view_state *s = &view->state.log;
2200 struct got_repository *thread_repo = NULL;
2201 struct got_commit_graph *thread_graph = NULL;
2204 if (in_repo_path != s->in_repo_path) {
2205 free(s->in_repo_path);
2206 s->in_repo_path = strdup(in_repo_path);
2207 if (s->in_repo_path == NULL)
2208 return got_error_from_errno("strdup");
2211 /* The commit queue only contains commits being displayed. */
2212 TAILQ_INIT(&s->commits.head);
2213 s->commits.ncommits = 0;
2217 s->head_ref_name = head_ref_name;
2218 s->start_id = got_object_id_dup(start_id);
2219 if (s->start_id == NULL) {
2220 err = got_error_from_errno("got_object_id_dup");
2223 s->log_branches = log_branches;
2225 SIMPLEQ_INIT(&s->colors);
2226 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2227 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2228 get_color_value("TOG_COLOR_COMMIT"));
2231 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2232 get_color_value("TOG_COLOR_AUTHOR"));
2234 free_colors(&s->colors);
2237 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2238 get_color_value("TOG_COLOR_DATE"));
2240 free_colors(&s->colors);
2245 view->show = show_log_view;
2246 view->input = input_log_view;
2247 view->close = close_log_view;
2248 view->search_start = search_start_log_view;
2249 view->search_next = search_next_log_view;
2251 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2254 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2258 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2259 s->repo, NULL, NULL);
2263 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2265 err = got_error_set_errno(errcode, "pthread_cond_init");
2268 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2270 err = got_error_set_errno(errcode, "pthread_cond_init");
2274 s->thread_args.commits_needed = view->nlines;
2275 s->thread_args.graph = thread_graph;
2276 s->thread_args.commits = &s->commits;
2277 s->thread_args.in_repo_path = s->in_repo_path;
2278 s->thread_args.start_id = s->start_id;
2279 s->thread_args.repo = thread_repo;
2280 s->thread_args.log_complete = 0;
2281 s->thread_args.quit = &s->quit;
2282 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2283 s->thread_args.selected_entry = &s->selected_entry;
2284 s->thread_args.searching = &view->searching;
2285 s->thread_args.search_next_done = &view->search_next_done;
2286 s->thread_args.regex = &view->regex;
2289 close_log_view(view);
2293 static const struct got_error *
2294 show_log_view(struct tog_view *view)
2296 struct tog_log_view_state *s = &view->state.log;
2298 if (s->thread == NULL) {
2299 int errcode = pthread_create(&s->thread, NULL, log_thread,
2302 return got_error_set_errno(errcode, "pthread_create");
2305 return draw_commits(view, &s->last_displayed_entry,
2306 &s->selected_entry, s->first_displayed_entry,
2307 &s->commits, s->selected, view->nlines, s->refs,
2308 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2311 static const struct got_error *
2312 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2313 struct tog_view **focus_view, struct tog_view *view, int ch)
2315 const struct got_error *err = NULL;
2316 struct tog_log_view_state *s = &view->state.log;
2317 char *parent_path, *in_repo_path = NULL;
2318 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2320 struct got_object_id *start_id;
2330 if (s->first_displayed_entry == NULL)
2332 if (s->selected > 0)
2335 scroll_up(view, &s->first_displayed_entry, 1,
2340 if (s->first_displayed_entry == NULL)
2342 if (TAILQ_FIRST(&s->commits.head) ==
2343 s->first_displayed_entry) {
2347 scroll_up(view, &s->first_displayed_entry,
2348 view->nlines - 1, &s->commits);
2354 if (s->first_displayed_entry == NULL)
2356 if (s->selected < MIN(view->nlines - 2,
2357 s->commits.ncommits - 1)) {
2361 err = scroll_down(view, &s->first_displayed_entry, 1,
2362 &s->last_displayed_entry, &s->commits,
2363 &s->thread_args.log_complete,
2364 &s->thread_args.commits_needed,
2365 &s->thread_args.need_commits,
2366 &s->thread_args.commit_loaded);
2370 struct commit_queue_entry *first;
2371 first = s->first_displayed_entry;
2374 err = scroll_down(view, &s->first_displayed_entry,
2375 view->nlines - 1, &s->last_displayed_entry,
2376 &s->commits, &s->thread_args.log_complete,
2377 &s->thread_args.commits_needed,
2378 &s->thread_args.need_commits,
2379 &s->thread_args.commit_loaded);
2382 if (first == s->first_displayed_entry &&
2383 s->selected < MIN(view->nlines - 2,
2384 s->commits.ncommits - 1)) {
2385 /* can't scroll further down */
2386 s->selected = MIN(view->nlines - 2,
2387 s->commits.ncommits - 1);
2393 if (s->selected > view->nlines - 2)
2394 s->selected = view->nlines - 2;
2395 if (s->selected > s->commits.ncommits - 1)
2396 s->selected = s->commits.ncommits - 1;
2401 if (s->selected_entry == NULL)
2403 if (view_is_parent_view(view))
2404 begin_x = view_split_begin_x(view->begin_x);
2405 err = open_diff_view_for_commit(&diff_view, begin_x,
2406 s->selected_entry->commit, s->selected_entry->id,
2407 view, s->refs, s->repo);
2410 if (view_is_parent_view(view)) {
2411 err = view_close_child(view);
2414 err = view_set_child(view, diff_view);
2416 view_close(diff_view);
2419 *focus_view = diff_view;
2420 view->child_focussed = 1;
2422 *new_view = diff_view;
2425 if (s->selected_entry == NULL)
2427 if (view_is_parent_view(view))
2428 begin_x = view_split_begin_x(view->begin_x);
2429 err = browse_commit_tree(&tree_view, begin_x,
2430 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2433 if (view_is_parent_view(view)) {
2434 err = view_close_child(view);
2437 err = view_set_child(view, tree_view);
2439 view_close(tree_view);
2442 *focus_view = tree_view;
2443 view->child_focussed = 1;
2445 *new_view = tree_view;
2448 if (got_path_cmp(s->in_repo_path, "/",
2449 strlen(s->in_repo_path), 1) == 0)
2451 err = got_path_dirname(&parent_path, s->in_repo_path);
2454 err = stop_log_thread(s);
2459 lv = view_open(view->nlines, view->ncols,
2460 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2463 return got_error_from_errno("view_open");
2465 err = open_log_view(lv, s->start_id, s->refs,
2466 s->repo, s->head_ref_name, parent_path,
2471 if (view_is_parent_view(view))
2474 view_set_child(view->parent, lv);
2479 err = stop_log_thread(s);
2482 lv = view_open(view->nlines, view->ncols,
2483 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2485 return got_error_from_errno("view_open");
2486 err = got_repo_match_object_id(&start_id, NULL,
2487 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2488 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2493 in_repo_path = strdup(s->in_repo_path);
2494 if (in_repo_path == NULL) {
2497 return got_error_from_errno("strdup");
2499 got_ref_list_free(s->refs);
2500 err = got_ref_list(s->refs, s->repo, NULL,
2501 got_ref_cmp_by_name, NULL);
2507 err = open_log_view(lv, start_id, s->refs, s->repo,
2508 s->head_ref_name, in_repo_path, s->log_branches);
2518 s->log_branches = !s->log_branches;
2519 err = stop_log_thread(s);
2522 lv = view_open(view->nlines, view->ncols,
2523 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2525 return got_error_from_errno("view_open");
2526 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2527 s->head_ref_name, s->in_repo_path, s->log_branches);
2542 static const struct got_error *
2543 apply_unveil(const char *repo_path, const char *worktree_path)
2545 const struct got_error *error;
2548 if (unveil("gmon.out", "rwc") != 0)
2549 return got_error_from_errno2("unveil", "gmon.out");
2551 if (repo_path && unveil(repo_path, "r") != 0)
2552 return got_error_from_errno2("unveil", repo_path);
2554 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2555 return got_error_from_errno2("unveil", worktree_path);
2557 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2558 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2560 error = got_privsep_unveil_exec_helpers();
2564 if (unveil(NULL, NULL) != 0)
2565 return got_error_from_errno("unveil");
2575 halfdelay(1); /* Do fast refresh while initial view is loading. */
2578 intrflush(stdscr, FALSE);
2579 keypad(stdscr, TRUE);
2581 if (getenv("TOG_COLORS") != NULL) {
2583 use_default_colors();
2585 signal(SIGWINCH, tog_sigwinch);
2586 signal(SIGPIPE, tog_sigpipe);
2587 signal(SIGCONT, tog_sigcont);
2590 static const struct got_error *
2591 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2592 struct got_repository *repo, struct got_worktree *worktree)
2594 const struct got_error *err = NULL;
2597 *in_repo_path = strdup("/");
2598 if (*in_repo_path == NULL)
2599 return got_error_from_errno("strdup");
2604 const char *prefix = got_worktree_get_path_prefix(worktree);
2607 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2610 if (asprintf(in_repo_path, "%s%s%s", prefix,
2611 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2613 err = got_error_from_errno("asprintf");
2614 *in_repo_path = NULL;
2618 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2623 static const struct got_error *
2624 cmd_log(int argc, char *argv[])
2626 const struct got_error *error;
2627 struct got_repository *repo = NULL;
2628 struct got_worktree *worktree = NULL;
2629 struct got_reflist_head refs;
2630 struct got_object_id *start_id = NULL;
2631 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2632 char *start_commit = NULL, *head_ref_name = NULL;
2633 int ch, log_branches = 0;
2634 struct tog_view *view;
2636 SIMPLEQ_INIT(&refs);
2639 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2644 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2650 start_commit = optarg;
2653 repo_path = realpath(optarg, NULL);
2654 if (repo_path == NULL)
2655 return got_error_from_errno2("realpath",
2670 cwd = getcwd(NULL, 0);
2672 return got_error_from_errno("getcwd");
2674 error = got_worktree_open(&worktree, cwd);
2675 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2678 if (repo_path == NULL) {
2681 strdup(got_worktree_get_repo_path(worktree));
2683 repo_path = strdup(cwd);
2685 if (repo_path == NULL) {
2686 error = got_error_from_errno("strdup");
2690 error = got_repo_open(&repo, repo_path, NULL);
2694 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2701 error = apply_unveil(got_repo_get_path(repo),
2702 worktree ? got_worktree_get_root_path(worktree) : NULL);
2706 if (start_commit == NULL)
2707 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2708 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2709 GOT_OBJ_TYPE_COMMIT, 1, repo);
2711 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2712 GOT_OBJ_TYPE_COMMIT, 1, repo);
2716 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2720 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2722 error = got_error_from_errno("view_open");
2726 head_ref_name = strdup(
2727 got_worktree_get_head_ref_name(worktree));
2728 if (head_ref_name == NULL) {
2729 error = got_error_from_errno("strdup");
2733 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2734 in_repo_path, log_branches);
2738 /* Release work tree lock. */
2739 got_worktree_close(worktree);
2742 error = view_loop(view);
2748 free(head_ref_name);
2750 got_repo_close(repo);
2752 got_worktree_close(worktree);
2753 got_ref_list_free(&refs);
2761 fprintf(stderr, "usage: %s diff [-a] [-r repository-path] "
2762 "object1 object2\n", getprogname());
2767 parse_next_line(FILE *f, size_t *len)
2772 const char delim[3] = { '\0', '\0', '\0'};
2774 line = fparseln(f, &linelen, &lineno, delim, 0);
2781 match_line(const char *line, regex_t *regex, size_t nmatch,
2782 regmatch_t *regmatch)
2784 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2788 match_color(struct tog_colors *colors, const char *line)
2790 struct tog_color *tc = NULL;
2792 SIMPLEQ_FOREACH(tc, colors, entry) {
2793 if (match_line(line, &tc->regex, 0, NULL))
2800 static const struct got_error *
2801 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2802 WINDOW *window, regmatch_t *regmatch)
2804 const struct got_error *err = NULL;
2811 s = strndup(line, regmatch->rm_so);
2813 return got_error_from_errno("strndup");
2815 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2820 waddwstr(window, wline);
2827 s = strndup(line + regmatch->rm_so,
2828 regmatch->rm_eo - regmatch->rm_so);
2830 err = got_error_from_errno("strndup");
2834 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2839 wattr_on(window, A_STANDOUT, NULL);
2840 waddwstr(window, wline);
2841 wattr_off(window, A_STANDOUT, NULL);
2848 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2849 err = format_line(&wline, &width,
2850 line + regmatch->rm_eo, wlimit, col_tab_align);
2853 waddwstr(window, wline);
2861 static const struct got_error *
2862 draw_file(struct tog_view *view, FILE *f, int first_displayed_line, int nlines,
2863 off_t *line_offsets, int selected_line, int max_lines,
2864 int *last_displayed_line, int *eof, char *header,
2865 struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
2867 const struct got_error *err;
2870 struct tog_color *tc;
2876 line_offset = line_offsets[first_displayed_line - 1];
2877 if (fseeko(f, line_offset, SEEK_SET) == -1)
2878 return got_error_from_errno("fseek");
2880 werase(view->window);
2883 if (asprintf(&line, "[%d/%d] %s",
2884 first_displayed_line - 1 + selected_line, nlines,
2886 return got_error_from_errno("asprintf");
2887 err = format_line(&wline, &width, line, view->ncols, 0);
2892 if (view_needs_focus_indication(view))
2893 wstandout(view->window);
2894 waddwstr(view->window, wline);
2897 if (view_needs_focus_indication(view))
2898 wstandend(view->window);
2899 if (width <= view->ncols - 1)
2900 waddch(view->window, '\n');
2908 while (max_lines > 0 && nprinted < max_lines) {
2909 line = parse_next_line(f, &len);
2915 tc = match_color(colors, line);
2917 wattr_on(view->window,
2918 COLOR_PAIR(tc->colorpair), NULL);
2919 if (first_displayed_line + nprinted == matched_line &&
2920 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2921 err = add_matched_line(&width, line, view->ncols, 0,
2922 view->window, regmatch);
2928 err = format_line(&wline, &width, line, view->ncols, 0);
2933 waddwstr(view->window, wline);
2938 wattr_off(view->window,
2939 COLOR_PAIR(tc->colorpair), NULL);
2940 if (width <= view->ncols - 1)
2941 waddch(view->window, '\n');
2946 *last_displayed_line = first_displayed_line + (nprinted - 1);
2948 *last_displayed_line = first_displayed_line;
2953 while (nprinted < view->nlines) {
2954 waddch(view->window, '\n');
2958 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2963 wstandout(view->window);
2964 waddwstr(view->window, wline);
2967 wstandend(view->window);
2974 get_datestr(time_t *time, char *datebuf)
2976 struct tm mytm, *tm;
2979 tm = gmtime_r(time, &mytm);
2982 s = asctime_r(tm, datebuf);
2985 p = strchr(s, '\n');
2991 static const struct got_error *
2992 get_changed_paths(struct got_pathlist_head *paths,
2993 struct got_commit_object *commit, struct got_repository *repo)
2995 const struct got_error *err = NULL;
2996 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2997 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2998 struct got_object_qid *qid;
3000 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3002 struct got_commit_object *pcommit;
3003 err = got_object_open_as_commit(&pcommit, repo,
3008 tree_id1 = got_object_commit_get_tree_id(pcommit);
3009 got_object_commit_close(pcommit);
3014 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3019 tree_id2 = got_object_commit_get_tree_id(commit);
3020 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3024 err = got_diff_tree(tree1, tree2, "", "", repo,
3025 got_diff_tree_collect_changed_paths, paths, 0);
3028 got_object_tree_close(tree1);
3030 got_object_tree_close(tree2);
3034 static const struct got_error *
3035 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3039 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3041 return got_error_from_errno("reallocarray");
3043 (*line_offsets)[*nlines] = off;
3048 static const struct got_error *
3049 write_commit_info(off_t **line_offsets, size_t *nlines,
3050 struct got_object_id *commit_id, struct got_reflist_head *refs,
3051 struct got_repository *repo, FILE *outfile)
3053 const struct got_error *err = NULL;
3054 char datebuf[26], *datestr;
3055 struct got_commit_object *commit;
3056 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3057 time_t committer_time;
3058 const char *author, *committer;
3059 char *refs_str = NULL;
3060 struct got_pathlist_head changed_paths;
3061 struct got_pathlist_entry *pe;
3065 TAILQ_INIT(&changed_paths);
3068 err = build_refs_str(&refs_str, refs, commit_id, repo);
3073 err = got_object_open_as_commit(&commit, repo, commit_id);
3077 err = got_object_id_str(&id_str, commit_id);
3079 err = got_error_from_errno("got_object_id_str");
3083 err = add_line_offset(line_offsets, nlines, 0);
3087 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3088 refs_str ? refs_str : "", refs_str ? ")" : "");
3090 err = got_error_from_errno("fprintf");
3094 err = add_line_offset(line_offsets, nlines, outoff);
3098 n = fprintf(outfile, "from: %s\n",
3099 got_object_commit_get_author(commit));
3101 err = got_error_from_errno("fprintf");
3105 err = add_line_offset(line_offsets, nlines, outoff);
3109 committer_time = got_object_commit_get_committer_time(commit);
3110 datestr = get_datestr(&committer_time, datebuf);
3112 n = fprintf(outfile, "date: %s UTC\n", datestr);
3114 err = got_error_from_errno("fprintf");
3118 err = add_line_offset(line_offsets, nlines, outoff);
3122 author = got_object_commit_get_author(commit);
3123 committer = got_object_commit_get_committer(commit);
3124 if (strcmp(author, committer) != 0) {
3125 n = fprintf(outfile, "via: %s\n", committer);
3127 err = got_error_from_errno("fprintf");
3131 err = add_line_offset(line_offsets, nlines, outoff);
3135 err = got_object_commit_get_logmsg(&logmsg, commit);
3139 while ((line = strsep(&s, "\n")) != NULL) {
3140 n = fprintf(outfile, "%s\n", line);
3142 err = got_error_from_errno("fprintf");
3146 err = add_line_offset(line_offsets, nlines, outoff);
3151 err = get_changed_paths(&changed_paths, commit, repo);
3154 TAILQ_FOREACH(pe, &changed_paths, entry) {
3155 struct got_diff_changed_path *cp = pe->data;
3156 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3158 err = got_error_from_errno("fprintf");
3162 err = add_line_offset(line_offsets, nlines, outoff);
3165 free((char *)pe->path);
3169 fputc('\n', outfile);
3171 err = add_line_offset(line_offsets, nlines, outoff);
3173 got_pathlist_free(&changed_paths);
3177 got_object_commit_close(commit);
3179 free(*line_offsets);
3180 *line_offsets = NULL;
3186 static const struct got_error *
3187 create_diff(struct tog_diff_view_state *s)
3189 const struct got_error *err = NULL;
3193 free(s->line_offsets);
3194 s->line_offsets = malloc(sizeof(off_t));
3195 if (s->line_offsets == NULL)
3196 return got_error_from_errno("malloc");
3201 err = got_error_from_errno("got_opentemp");
3204 if (s->f && fclose(s->f) != 0) {
3205 err = got_error_from_errno("fclose");
3211 err = got_object_get_type(&obj_type, s->repo, s->id1);
3213 err = got_object_get_type(&obj_type, s->repo, s->id2);
3218 case GOT_OBJ_TYPE_BLOB:
3219 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3220 s->id1, s->id2, NULL, NULL, s->diff_context, 0,
3221 s->force_text_diff, s->repo, s->f);
3223 case GOT_OBJ_TYPE_TREE:
3224 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3225 s->id1, s->id2, "", "", s->diff_context, 0,
3226 s->force_text_diff, s->repo, s->f);
3228 case GOT_OBJ_TYPE_COMMIT: {
3229 const struct got_object_id_queue *parent_ids;
3230 struct got_object_qid *pid;
3231 struct got_commit_object *commit2;
3233 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3236 /* Show commit info if we're diffing to a parent/root commit. */
3237 if (s->id1 == NULL) {
3238 err = write_commit_info(&s->line_offsets, &s->nlines,
3239 s->id2, s->refs, s->repo, s->f);
3243 parent_ids = got_object_commit_get_parent_ids(commit2);
3244 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3245 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3246 err = write_commit_info(
3247 &s->line_offsets, &s->nlines,
3248 s->id2, s->refs, s->repo, s->f);
3255 got_object_commit_close(commit2);
3257 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3258 s->id1, s->id2, s->diff_context, 0, s->force_text_diff,
3263 err = got_error(GOT_ERR_OBJ_TYPE);
3269 if (s->f && fflush(s->f) != 0 && err == NULL)
3270 err = got_error_from_errno("fflush");
3275 diff_view_indicate_progress(struct tog_view *view)
3277 mvwaddstr(view->window, 0, 0, "diffing...");
3282 static const struct got_error *
3283 search_start_diff_view(struct tog_view *view)
3285 struct tog_diff_view_state *s = &view->state.diff;
3287 s->matched_line = 0;
3291 static const struct got_error *
3292 search_next_diff_view(struct tog_view *view)
3294 struct tog_diff_view_state *s = &view->state.diff;
3297 if (!view->searching) {
3298 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3302 if (s->matched_line) {
3303 if (view->searching == TOG_SEARCH_FORWARD)
3304 lineno = s->matched_line + 1;
3306 lineno = s->matched_line - 1;
3308 if (view->searching == TOG_SEARCH_FORWARD)
3319 if (lineno <= 0 || lineno > s->nlines) {
3320 if (s->matched_line == 0) {
3321 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3326 if (view->searching == TOG_SEARCH_FORWARD)
3332 offset = s->line_offsets[lineno - 1];
3333 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3335 return got_error_from_errno("fseeko");
3338 line = parse_next_line(s->f, &len);
3340 match_line(line, &view->regex, 1, &view->regmatch)) {
3341 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3342 s->matched_line = lineno;
3347 if (view->searching == TOG_SEARCH_FORWARD)
3353 if (s->matched_line) {
3354 s->first_displayed_line = s->matched_line;
3355 s->selected_line = 1;
3361 static const struct got_error *
3362 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3363 struct got_object_id *id2, int force_text_diff, struct tog_view *log_view,
3364 struct got_reflist_head *refs, struct got_repository *repo)
3366 const struct got_error *err;
3367 struct tog_diff_view_state *s = &view->state.diff;
3369 if (id1 != NULL && id2 != NULL) {
3371 err = got_object_get_type(&type1, repo, id1);
3374 err = got_object_get_type(&type2, repo, id2);
3379 return got_error(GOT_ERR_OBJ_TYPE);
3381 s->first_displayed_line = 1;
3382 s->last_displayed_line = view->nlines;
3383 s->selected_line = 1;
3390 s->id1 = got_object_id_dup(id1);
3392 return got_error_from_errno("got_object_id_dup");
3396 s->id2 = got_object_id_dup(id2);
3397 if (s->id2 == NULL) {
3400 return got_error_from_errno("got_object_id_dup");
3403 s->first_displayed_line = 1;
3404 s->last_displayed_line = view->nlines;
3405 s->diff_context = 3;
3406 s->force_text_diff = force_text_diff;
3407 s->log_view = log_view;
3411 SIMPLEQ_INIT(&s->colors);
3412 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3413 err = add_color(&s->colors,
3414 "^-", TOG_COLOR_DIFF_MINUS,
3415 get_color_value("TOG_COLOR_DIFF_MINUS"));
3418 err = add_color(&s->colors, "^\\+",
3419 TOG_COLOR_DIFF_PLUS,
3420 get_color_value("TOG_COLOR_DIFF_PLUS"));
3422 free_colors(&s->colors);
3425 err = add_color(&s->colors,
3426 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3427 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3429 free_colors(&s->colors);
3433 err = add_color(&s->colors,
3434 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3435 TOG_COLOR_DIFF_META,
3436 get_color_value("TOG_COLOR_DIFF_META"));
3438 free_colors(&s->colors);
3442 err = add_color(&s->colors,
3443 "^(from|via): ", TOG_COLOR_AUTHOR,
3444 get_color_value("TOG_COLOR_AUTHOR"));
3446 free_colors(&s->colors);
3450 err = add_color(&s->colors,
3451 "^date: ", TOG_COLOR_DATE,
3452 get_color_value("TOG_COLOR_DATE"));
3454 free_colors(&s->colors);
3459 if (log_view && view_is_splitscreen(view))
3460 show_log_view(log_view); /* draw vborder */
3461 diff_view_indicate_progress(view);
3463 s->line_offsets = NULL;
3465 err = create_diff(s);
3474 view->show = show_diff_view;
3475 view->input = input_diff_view;
3476 view->close = close_diff_view;
3477 view->search_start = search_start_diff_view;
3478 view->search_next = search_next_diff_view;
3483 static const struct got_error *
3484 close_diff_view(struct tog_view *view)
3486 const struct got_error *err = NULL;
3487 struct tog_diff_view_state *s = &view->state.diff;
3493 if (s->f && fclose(s->f) == EOF)
3494 err = got_error_from_errno("fclose");
3495 free_colors(&s->colors);
3496 free(s->line_offsets);
3497 s->line_offsets = NULL;
3502 static const struct got_error *
3503 show_diff_view(struct tog_view *view)
3505 const struct got_error *err;
3506 struct tog_diff_view_state *s = &view->state.diff;
3507 char *id_str1 = NULL, *id_str2, *header;
3510 err = got_object_id_str(&id_str1, s->id1);
3514 err = got_object_id_str(&id_str2, s->id2);
3518 if (asprintf(&header, "diff %s %s",
3519 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3520 err = got_error_from_errno("asprintf");
3528 return draw_file(view, s->f, s->first_displayed_line, s->nlines,
3529 s->line_offsets, s->selected_line, view->nlines,
3530 &s->last_displayed_line, &s->eof, header, &s->colors,
3531 s->matched_line, &view->regmatch);
3534 static const struct got_error *
3535 set_selected_commit(struct tog_diff_view_state *s,
3536 struct commit_queue_entry *entry)
3538 const struct got_error *err;
3539 const struct got_object_id_queue *parent_ids;
3540 struct got_commit_object *selected_commit;
3541 struct got_object_qid *pid;
3544 s->id2 = got_object_id_dup(entry->id);
3546 return got_error_from_errno("got_object_id_dup");
3548 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3551 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3553 pid = SIMPLEQ_FIRST(parent_ids);
3554 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3555 got_object_commit_close(selected_commit);
3559 static const struct got_error *
3560 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3561 struct tog_view **focus_view, struct tog_view *view, int ch)
3563 const struct got_error *err = NULL;
3564 struct tog_diff_view_state *s = &view->state.diff;
3565 struct tog_log_view_state *ls;
3566 struct commit_queue_entry *entry;
3571 s->force_text_diff = !s->force_text_diff;
3572 wclear(view->window);
3573 s->first_displayed_line = 1;
3574 s->last_displayed_line = view->nlines;
3575 diff_view_indicate_progress(view);
3576 err = create_diff(s);
3580 if (s->first_displayed_line > 1)
3581 s->first_displayed_line--;
3585 if (s->first_displayed_line == 1)
3588 while (i++ < view->nlines - 1 &&
3589 s->first_displayed_line > 1)
3590 s->first_displayed_line--;
3595 s->first_displayed_line++;
3603 while (!s->eof && i++ < view->nlines - 1) {
3605 line = parse_next_line(s->f, NULL);
3606 s->first_displayed_line++;
3612 if (s->diff_context > 0) {
3614 diff_view_indicate_progress(view);
3615 err = create_diff(s);
3616 if (s->first_displayed_line + view->nlines - 1 >
3618 s->first_displayed_line = 1;
3619 s->last_displayed_line = view->nlines;
3624 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3626 diff_view_indicate_progress(view);
3627 err = create_diff(s);
3632 if (s->log_view == NULL)
3634 ls = &s->log_view->state.log;
3635 entry = TAILQ_PREV(ls->selected_entry,
3636 commit_queue_head, entry);
3640 err = input_log_view(NULL, NULL, NULL, s->log_view,
3645 err = set_selected_commit(s, entry);
3649 s->first_displayed_line = 1;
3650 s->last_displayed_line = view->nlines;
3652 diff_view_indicate_progress(view);
3653 err = create_diff(s);
3657 if (s->log_view == NULL)
3659 ls = &s->log_view->state.log;
3661 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3662 ls->thread_args.commits_needed++;
3663 err = trigger_log_thread(s->log_view, 1,
3664 &ls->thread_args.commits_needed,
3665 &ls->thread_args.log_complete,
3666 &ls->thread_args.need_commits,
3667 &ls->thread_args.commit_loaded);
3671 err = input_log_view(NULL, NULL, NULL, s->log_view,
3676 entry = TAILQ_NEXT(ls->selected_entry, entry);
3680 err = set_selected_commit(s, entry);
3684 s->first_displayed_line = 1;
3685 s->last_displayed_line = view->nlines;
3687 diff_view_indicate_progress(view);
3688 err = create_diff(s);
3697 static const struct got_error *
3698 cmd_diff(int argc, char *argv[])
3700 const struct got_error *error = NULL;
3701 struct got_repository *repo = NULL;
3702 struct got_worktree *worktree = NULL;
3703 struct got_reflist_head refs;
3704 struct got_object_id *id1 = NULL, *id2 = NULL;
3705 char *repo_path = NULL, *cwd = NULL;
3706 char *id_str1 = NULL, *id_str2 = NULL;
3707 int ch, force_text_diff = 0;
3708 struct tog_view *view;
3710 SIMPLEQ_INIT(&refs);
3713 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3718 while ((ch = getopt(argc, argv, "ar:")) != -1) {
3721 force_text_diff = 1;
3724 repo_path = realpath(optarg, NULL);
3725 if (repo_path == NULL)
3726 return got_error_from_errno2("realpath",
3739 usage_diff(); /* TODO show local worktree changes */
3740 } else if (argc == 2) {
3746 cwd = getcwd(NULL, 0);
3748 return got_error_from_errno("getcwd");
3750 error = got_worktree_open(&worktree, cwd);
3751 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3754 if (repo_path == NULL) {
3757 strdup(got_worktree_get_repo_path(worktree));
3759 repo_path = strdup(cwd);
3761 if (repo_path == NULL) {
3762 error = got_error_from_errno("strdup");
3766 error = got_repo_open(&repo, repo_path, NULL);
3772 error = apply_unveil(got_repo_get_path(repo), NULL);
3776 error = got_repo_match_object_id_prefix(&id1, id_str1,
3777 GOT_OBJ_TYPE_ANY, repo);
3781 error = got_repo_match_object_id_prefix(&id2, id_str2,
3782 GOT_OBJ_TYPE_ANY, repo);
3786 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3790 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3792 error = got_error_from_errno("view_open");
3795 error = open_diff_view(view, id1, id2, force_text_diff, NULL, &refs,
3799 error = view_loop(view);
3804 got_repo_close(repo);
3806 got_worktree_close(worktree);
3807 got_ref_list_free(&refs);
3815 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3820 struct tog_blame_line {
3822 struct got_object_id *id;
3825 static const struct got_error *
3826 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3827 const char *path, struct tog_blame_line *lines, int nlines,
3828 int blame_complete, int selected_line, int *first_displayed_line,
3829 int *last_displayed_line, int *eof, int max_lines,
3830 struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
3832 const struct got_error *err;
3833 int lineno = 0, nprinted = 0;
3838 struct tog_blame_line *blame_line;
3839 struct got_object_id *prev_id = NULL;
3841 struct tog_color *tc;
3843 err = got_object_id_str(&id_str, id);
3848 werase(view->window);
3850 if (asprintf(&line, "commit %s", id_str) == -1) {
3851 err = got_error_from_errno("asprintf");
3856 err = format_line(&wline, &width, line, view->ncols, 0);
3861 if (view_needs_focus_indication(view))
3862 wstandout(view->window);
3863 tc = get_color(colors, TOG_COLOR_COMMIT);
3865 wattr_on(view->window,
3866 COLOR_PAIR(tc->colorpair), NULL);
3867 waddwstr(view->window, wline);
3869 wattr_off(view->window,
3870 COLOR_PAIR(tc->colorpair), NULL);
3871 if (view_needs_focus_indication(view))
3872 wstandend(view->window);
3875 if (width < view->ncols - 1)
3876 waddch(view->window, '\n');
3878 if (asprintf(&line, "[%d/%d] %s%s",
3879 *first_displayed_line - 1 + selected_line, nlines,
3880 blame_complete ? "" : "annotating... ", path) == -1) {
3882 return got_error_from_errno("asprintf");
3885 err = format_line(&wline, &width, line, view->ncols, 0);
3890 waddwstr(view->window, wline);
3893 if (width < view->ncols - 1)
3894 waddch(view->window, '\n');
3897 while (nprinted < max_lines - 2) {
3898 line = parse_next_line(f, &len);
3903 if (++lineno < *first_displayed_line) {
3908 if (view->focussed && nprinted == selected_line - 1)
3909 wstandout(view->window);
3912 blame_line = &lines[lineno - 1];
3913 if (blame_line->annotated && prev_id &&
3914 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3916 nprinted == selected_line - 1)) {
3917 waddstr(view->window, " ");
3918 } else if (blame_line->annotated) {
3920 err = got_object_id_str(&id_str, blame_line->id);
3925 tc = get_color(colors, TOG_COLOR_COMMIT);
3927 wattr_on(view->window,
3928 COLOR_PAIR(tc->colorpair), NULL);
3929 wprintw(view->window, "%.8s", id_str);
3931 wattr_off(view->window,
3932 COLOR_PAIR(tc->colorpair), NULL);
3934 prev_id = blame_line->id;
3936 waddstr(view->window, "........");
3940 waddstr(view->window, "........");
3944 if (view->focussed && nprinted == selected_line - 1)
3945 wstandend(view->window);
3946 waddstr(view->window, " ");
3948 if (view->ncols <= 9) {
3950 wline = wcsdup(L"");
3951 if (wline == NULL) {
3952 err = got_error_from_errno("wcsdup");
3956 } else if (*first_displayed_line + nprinted == matched_line &&
3957 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3958 err = add_matched_line(&width, line, view->ncols - 9, 9,
3959 view->window, regmatch);
3966 err = format_line(&wline, &width, line,
3967 view->ncols - 9, 9);
3968 waddwstr(view->window, wline);
3974 if (width <= view->ncols - 1)
3975 waddch(view->window, '\n');
3976 if (++nprinted == 1)
3977 *first_displayed_line = lineno;
3980 *last_displayed_line = lineno;
3987 static const struct got_error *
3988 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3990 const struct got_error *err = NULL;
3991 struct tog_blame_cb_args *a = arg;
3992 struct tog_blame_line *line;
3995 if (nlines != a->nlines ||
3996 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3997 return got_error(GOT_ERR_RANGE);
3999 errcode = pthread_mutex_lock(&tog_mutex);
4001 return got_error_set_errno(errcode, "pthread_mutex_lock");
4003 if (*a->quit) { /* user has quit the blame view */
4004 err = got_error(GOT_ERR_ITER_COMPLETED);
4009 goto done; /* no change in this commit */
4011 line = &a->lines[lineno - 1];
4012 if (line->annotated)
4015 line->id = got_object_id_dup(id);
4016 if (line->id == NULL) {
4017 err = got_error_from_errno("got_object_id_dup");
4020 line->annotated = 1;
4022 errcode = pthread_mutex_unlock(&tog_mutex);
4024 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4029 blame_thread(void *arg)
4031 const struct got_error *err;
4032 struct tog_blame_thread_args *ta = arg;
4033 struct tog_blame_cb_args *a = ta->cb_args;
4036 err = block_signals_used_by_main_thread();
4040 err = got_blame(ta->path, a->commit_id, ta->repo,
4041 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4042 if (err && err->code == GOT_ERR_CANCELLED)
4045 errcode = pthread_mutex_lock(&tog_mutex);
4047 return (void *)got_error_set_errno(errcode,
4048 "pthread_mutex_lock");
4050 got_repo_close(ta->repo);
4054 errcode = pthread_mutex_unlock(&tog_mutex);
4055 if (errcode && err == NULL)
4056 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4061 static struct got_object_id *
4062 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4063 int first_displayed_line, int selected_line)
4065 struct tog_blame_line *line;
4070 line = &lines[first_displayed_line - 1 + selected_line - 1];
4071 if (!line->annotated)
4077 static const struct got_error *
4078 stop_blame(struct tog_blame *blame)
4080 const struct got_error *err = NULL;
4083 if (blame->thread) {
4085 errcode = pthread_mutex_unlock(&tog_mutex);
4087 return got_error_set_errno(errcode,
4088 "pthread_mutex_unlock");
4089 errcode = pthread_join(blame->thread, (void **)&err);
4091 return got_error_set_errno(errcode, "pthread_join");
4092 errcode = pthread_mutex_lock(&tog_mutex);
4094 return got_error_set_errno(errcode,
4095 "pthread_mutex_lock");
4096 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4098 blame->thread = NULL;
4100 if (blame->thread_args.repo) {
4101 got_repo_close(blame->thread_args.repo);
4102 blame->thread_args.repo = NULL;
4105 if (fclose(blame->f) != 0 && err == NULL)
4106 err = got_error_from_errno("fclose");
4110 for (i = 0; i < blame->nlines; i++)
4111 free(blame->lines[i].id);
4113 blame->lines = NULL;
4115 free(blame->cb_args.commit_id);
4116 blame->cb_args.commit_id = NULL;
4121 static const struct got_error *
4122 cancel_blame_view(void *arg)
4124 const struct got_error *err = NULL;
4128 errcode = pthread_mutex_lock(&tog_mutex);
4130 return got_error_set_errno(errcode,
4131 "pthread_mutex_unlock");
4134 err = got_error(GOT_ERR_CANCELLED);
4136 errcode = pthread_mutex_unlock(&tog_mutex);
4138 return got_error_set_errno(errcode,
4139 "pthread_mutex_lock");
4144 static const struct got_error *
4145 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
4146 int *first_displayed_line, int *last_displayed_line, int *selected_line,
4147 int *done, int *eof, const char *path, struct got_object_id *commit_id,
4148 struct got_repository *repo)
4150 const struct got_error *err = NULL;
4151 struct got_blob_object *blob = NULL;
4152 struct got_repository *thread_repo = NULL;
4153 struct got_object_id *obj_id = NULL;
4156 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
4160 err = got_object_get_type(&obj_type, repo, obj_id);
4164 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4165 err = got_error(GOT_ERR_OBJ_TYPE);
4169 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4172 blame->f = got_opentemp();
4173 if (blame->f == NULL) {
4174 err = got_error_from_errno("got_opentemp");
4177 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4178 &blame->line_offsets, blame->f, blob);
4179 if (err || blame->nlines == 0)
4182 /* Don't include \n at EOF in the blame line count. */
4183 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4186 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4187 if (blame->lines == NULL) {
4188 err = got_error_from_errno("calloc");
4192 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
4196 blame->cb_args.view = view;
4197 blame->cb_args.lines = blame->lines;
4198 blame->cb_args.nlines = blame->nlines;
4199 blame->cb_args.commit_id = got_object_id_dup(commit_id);
4200 if (blame->cb_args.commit_id == NULL) {
4201 err = got_error_from_errno("got_object_id_dup");
4204 blame->cb_args.quit = done;
4206 blame->thread_args.path = path;
4207 blame->thread_args.repo = thread_repo;
4208 blame->thread_args.cb_args = &blame->cb_args;
4209 blame->thread_args.complete = blame_complete;
4210 blame->thread_args.cancel_cb = cancel_blame_view;
4211 blame->thread_args.cancel_arg = done;
4212 *blame_complete = 0;
4216 got_object_blob_close(blob);
4223 static const struct got_error *
4224 open_blame_view(struct tog_view *view, char *path,
4225 struct got_object_id *commit_id, struct got_reflist_head *refs,
4226 struct got_repository *repo)
4228 const struct got_error *err = NULL;
4229 struct tog_blame_view_state *s = &view->state.blame;
4231 SIMPLEQ_INIT(&s->blamed_commits);
4233 s->path = strdup(path);
4234 if (s->path == NULL)
4235 return got_error_from_errno("strdup");
4237 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4243 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4244 s->first_displayed_line = 1;
4245 s->last_displayed_line = view->nlines;
4246 s->selected_line = 1;
4247 s->blame_complete = 0;
4250 s->commit_id = commit_id;
4251 memset(&s->blame, 0, sizeof(s->blame));
4253 SIMPLEQ_INIT(&s->colors);
4254 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4255 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4256 get_color_value("TOG_COLOR_COMMIT"));
4261 view->show = show_blame_view;
4262 view->input = input_blame_view;
4263 view->close = close_blame_view;
4264 view->search_start = search_start_blame_view;
4265 view->search_next = search_next_blame_view;
4267 return run_blame(&s->blame, view, &s->blame_complete,
4268 &s->first_displayed_line, &s->last_displayed_line,
4269 &s->selected_line, &s->done, &s->eof, s->path,
4270 s->blamed_commit->id, s->repo);
4273 static const struct got_error *
4274 close_blame_view(struct tog_view *view)
4276 const struct got_error *err = NULL;
4277 struct tog_blame_view_state *s = &view->state.blame;
4279 if (s->blame.thread)
4280 err = stop_blame(&s->blame);
4282 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4283 struct got_object_qid *blamed_commit;
4284 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4285 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4286 got_object_qid_free(blamed_commit);
4290 free_colors(&s->colors);
4295 static const struct got_error *
4296 search_start_blame_view(struct tog_view *view)
4298 struct tog_blame_view_state *s = &view->state.blame;
4300 s->matched_line = 0;
4304 static const struct got_error *
4305 search_next_blame_view(struct tog_view *view)
4307 struct tog_blame_view_state *s = &view->state.blame;
4310 if (!view->searching) {
4311 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4315 if (s->matched_line) {
4316 if (view->searching == TOG_SEARCH_FORWARD)
4317 lineno = s->matched_line + 1;
4319 lineno = s->matched_line - 1;
4321 if (view->searching == TOG_SEARCH_FORWARD)
4324 lineno = s->blame.nlines;
4332 if (lineno <= 0 || lineno > s->blame.nlines) {
4333 if (s->matched_line == 0) {
4334 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4339 if (view->searching == TOG_SEARCH_FORWARD)
4342 lineno = s->blame.nlines;
4345 offset = s->blame.line_offsets[lineno - 1];
4346 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4348 return got_error_from_errno("fseeko");
4351 line = parse_next_line(s->blame.f, &len);
4353 match_line(line, &view->regex, 1, &view->regmatch)) {
4354 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4355 s->matched_line = lineno;
4360 if (view->searching == TOG_SEARCH_FORWARD)
4366 if (s->matched_line) {
4367 s->first_displayed_line = s->matched_line;
4368 s->selected_line = 1;
4374 static const struct got_error *
4375 show_blame_view(struct tog_view *view)
4377 const struct got_error *err = NULL;
4378 struct tog_blame_view_state *s = &view->state.blame;
4381 if (s->blame.thread == NULL) {
4382 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4383 &s->blame.thread_args);
4385 return got_error_set_errno(errcode, "pthread_create");
4387 halfdelay(1); /* fast refresh while annotating */
4390 if (s->blame_complete)
4391 halfdelay(10); /* disable fast refresh */
4393 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4394 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4395 s->selected_line, &s->first_displayed_line,
4396 &s->last_displayed_line, &s->eof, view->nlines, &s->colors,
4397 s->matched_line, &view->regmatch);
4403 static const struct got_error *
4404 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4405 struct tog_view **focus_view, struct tog_view *view, int ch)
4407 const struct got_error *err = NULL, *thread_err = NULL;
4408 struct tog_view *diff_view;
4409 struct tog_blame_view_state *s = &view->state.blame;
4418 if (s->selected_line > 1)
4420 else if (s->selected_line == 1 &&
4421 s->first_displayed_line > 1)
4422 s->first_displayed_line--;
4426 if (s->first_displayed_line == 1) {
4427 s->selected_line = 1;
4430 if (s->first_displayed_line > view->nlines - 2)
4431 s->first_displayed_line -=
4434 s->first_displayed_line = 1;
4438 if (s->selected_line < view->nlines - 2 &&
4439 s->first_displayed_line +
4440 s->selected_line <= s->blame.nlines)
4442 else if (s->last_displayed_line <
4444 s->first_displayed_line++;
4448 struct got_object_id *id = NULL;
4449 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4450 s->first_displayed_line, s->selected_line);
4454 struct got_commit_object *commit;
4455 struct got_object_qid *pid;
4456 struct got_object_id *blob_id = NULL;
4458 err = got_object_open_as_commit(&commit,
4462 pid = SIMPLEQ_FIRST(
4463 got_object_commit_get_parent_ids(commit));
4465 got_object_commit_close(commit);
4468 /* Check if path history ends here. */
4469 err = got_object_id_by_path(&blob_id, s->repo,
4472 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4474 got_object_commit_close(commit);
4477 err = got_object_get_type(&obj_type, s->repo,
4480 /* Can't blame non-blob type objects. */
4481 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4482 got_object_commit_close(commit);
4485 err = got_object_qid_alloc(&s->blamed_commit,
4487 got_object_commit_close(commit);
4489 if (got_object_id_cmp(id,
4490 s->blamed_commit->id) == 0)
4492 err = got_object_qid_alloc(&s->blamed_commit,
4498 thread_err = stop_blame(&s->blame);
4502 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4503 s->blamed_commit, entry);
4504 err = run_blame(&s->blame, view, &s->blame_complete,
4505 &s->first_displayed_line, &s->last_displayed_line,
4506 &s->selected_line, &s->done, &s->eof,
4507 s->path, s->blamed_commit->id, s->repo);
4513 struct got_object_qid *first;
4514 first = SIMPLEQ_FIRST(&s->blamed_commits);
4515 if (!got_object_id_cmp(first->id, s->commit_id))
4518 thread_err = stop_blame(&s->blame);
4522 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4523 got_object_qid_free(s->blamed_commit);
4525 SIMPLEQ_FIRST(&s->blamed_commits);
4526 err = run_blame(&s->blame, view, &s->blame_complete,
4527 &s->first_displayed_line, &s->last_displayed_line,
4528 &s->selected_line, &s->done, &s->eof, s->path,
4529 s->blamed_commit->id, s->repo);
4536 struct got_object_id *id = NULL;
4537 struct got_object_qid *pid;
4538 struct got_commit_object *commit = NULL;
4539 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4540 s->first_displayed_line, s->selected_line);
4543 err = got_object_open_as_commit(&commit, s->repo, id);
4546 pid = SIMPLEQ_FIRST(
4547 got_object_commit_get_parent_ids(commit));
4548 if (view_is_parent_view(view))
4549 begin_x = view_split_begin_x(view->begin_x);
4550 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4551 if (diff_view == NULL) {
4552 got_object_commit_close(commit);
4553 err = got_error_from_errno("view_open");
4556 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4557 id, 0, NULL, s->refs, s->repo);
4558 got_object_commit_close(commit);
4560 view_close(diff_view);
4563 if (view_is_parent_view(view)) {
4564 err = view_close_child(view);
4567 err = view_set_child(view, diff_view);
4569 view_close(diff_view);
4572 *focus_view = diff_view;
4573 view->child_focussed = 1;
4575 *new_view = diff_view;
4583 if (s->last_displayed_line >= s->blame.nlines &&
4584 s->selected_line >= MIN(s->blame.nlines,
4585 view->nlines - 2)) {
4588 if (s->last_displayed_line >= s->blame.nlines &&
4589 s->selected_line < view->nlines - 2) {
4590 s->selected_line = MIN(s->blame.nlines,
4594 if (s->last_displayed_line + view->nlines - 2
4596 s->first_displayed_line +=
4599 s->first_displayed_line =
4604 if (s->selected_line > view->nlines - 2) {
4605 s->selected_line = MIN(s->blame.nlines,
4612 return thread_err ? thread_err : err;
4615 static const struct got_error *
4616 cmd_blame(int argc, char *argv[])
4618 const struct got_error *error;
4619 struct got_repository *repo = NULL;
4620 struct got_reflist_head refs;
4621 struct got_worktree *worktree = NULL;
4622 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4623 char *link_target = NULL;
4624 struct got_object_id *commit_id = NULL;
4625 char *commit_id_str = NULL;
4627 struct tog_view *view;
4629 SIMPLEQ_INIT(&refs);
4632 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4637 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4640 commit_id_str = optarg;
4643 repo_path = realpath(optarg, NULL);
4644 if (repo_path == NULL)
4645 return got_error_from_errno2("realpath",
4660 cwd = getcwd(NULL, 0);
4662 return got_error_from_errno("getcwd");
4664 error = got_worktree_open(&worktree, cwd);
4665 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4668 if (repo_path == NULL) {
4671 strdup(got_worktree_get_repo_path(worktree));
4673 repo_path = strdup(cwd);
4675 if (repo_path == NULL) {
4676 error = got_error_from_errno("strdup");
4680 error = got_repo_open(&repo, repo_path, NULL);
4684 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4691 error = apply_unveil(got_repo_get_path(repo), NULL);
4695 if (commit_id_str == NULL) {
4696 struct got_reference *head_ref;
4697 error = got_ref_open(&head_ref, repo, worktree ?
4698 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4701 error = got_ref_resolve(&commit_id, repo, head_ref);
4702 got_ref_close(head_ref);
4704 error = got_repo_match_object_id(&commit_id, NULL,
4705 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4710 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4714 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4716 error = got_error_from_errno("view_open");
4720 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4725 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4726 commit_id, &refs, repo);
4730 /* Release work tree lock. */
4731 got_worktree_close(worktree);
4734 error = view_loop(view);
4742 got_worktree_close(worktree);
4744 got_repo_close(repo);
4745 got_ref_list_free(&refs);
4749 static const struct got_error *
4750 draw_tree_entries(struct tog_view *view,
4751 struct got_tree_entry **first_displayed_entry,
4752 struct got_tree_entry **last_displayed_entry,
4753 struct got_tree_entry **selected_entry, int *ndisplayed,
4754 const char *label, int show_ids, const char *parent_path,
4755 struct got_tree_object *tree, int selected, int limit,
4756 int isroot, struct tog_colors *colors, struct got_repository *repo)
4758 const struct got_error *err = NULL;
4759 struct got_tree_entry *te;
4761 struct tog_color *tc;
4762 int width, n, i, nentries;
4766 werase(view->window);
4771 err = format_line(&wline, &width, label, view->ncols, 0);
4774 if (view_needs_focus_indication(view))
4775 wstandout(view->window);
4776 tc = get_color(colors, TOG_COLOR_COMMIT);
4778 wattr_on(view->window,
4779 COLOR_PAIR(tc->colorpair), NULL);
4780 waddwstr(view->window, wline);
4782 wattr_off(view->window,
4783 COLOR_PAIR(tc->colorpair), NULL);
4784 if (view_needs_focus_indication(view))
4785 wstandend(view->window);
4788 if (width < view->ncols - 1)
4789 waddch(view->window, '\n');
4792 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4795 waddwstr(view->window, wline);
4798 if (width < view->ncols - 1)
4799 waddch(view->window, '\n');
4802 waddch(view->window, '\n');
4806 if (*first_displayed_entry == NULL) {
4807 te = got_object_tree_get_first_entry(tree);
4808 if (selected == 0) {
4810 wstandout(view->window);
4811 *selected_entry = NULL;
4813 waddstr(view->window, " ..\n"); /* parent directory */
4814 if (selected == 0 && view->focussed)
4815 wstandend(view->window);
4822 te = *first_displayed_entry;
4825 nentries = got_object_tree_get_nentries(tree);
4826 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4827 char *line = NULL, *id_str = NULL, *link_target = NULL;
4828 const char *modestr = "";
4831 te = got_object_tree_get_entry(tree, i);
4832 mode = got_tree_entry_get_mode(te);
4835 err = got_object_id_str(&id_str,
4836 got_tree_entry_get_id(te));
4838 return got_error_from_errno(
4839 "got_object_id_str");
4841 if (got_object_tree_entry_is_submodule(te))
4843 else if (S_ISLNK(mode)) {
4846 err = got_tree_entry_get_symlink_target(&link_target,
4852 for (i = 0; i < strlen(link_target); i++) {
4853 if (!isprint((unsigned char)link_target[i]))
4854 link_target[i] = '?';
4858 else if (S_ISDIR(mode))
4860 else if (mode & S_IXUSR)
4862 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4863 got_tree_entry_get_name(te), modestr,
4864 link_target ? " -> ": "",
4865 link_target ? link_target : "") == -1) {
4868 return got_error_from_errno("asprintf");
4872 err = format_line(&wline, &width, line, view->ncols, 0);
4877 if (n == selected) {
4879 wstandout(view->window);
4880 *selected_entry = te;
4882 tc = match_color(colors, line);
4884 wattr_on(view->window,
4885 COLOR_PAIR(tc->colorpair), NULL);
4886 waddwstr(view->window, wline);
4888 wattr_off(view->window,
4889 COLOR_PAIR(tc->colorpair), NULL);
4890 if (width < view->ncols - 1)
4891 waddch(view->window, '\n');
4892 if (n == selected && view->focussed)
4893 wstandend(view->window);
4899 *last_displayed_entry = te;
4908 tree_scroll_up(struct tog_view *view,
4909 struct got_tree_entry **first_displayed_entry, int maxscroll,
4910 struct got_tree_object *tree, int isroot)
4912 struct got_tree_entry *te;
4915 if (*first_displayed_entry == NULL)
4918 te = got_object_tree_get_entry(tree, 0);
4919 if (*first_displayed_entry == te) {
4921 *first_displayed_entry = NULL;
4926 while (*first_displayed_entry && i < maxscroll) {
4927 *first_displayed_entry = got_tree_entry_get_prev(tree,
4928 *first_displayed_entry);
4931 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4932 *first_displayed_entry = NULL;
4936 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4937 struct got_tree_entry *last_displayed_entry,
4938 struct got_tree_object *tree)
4940 struct got_tree_entry *next, *last;
4943 if (*first_displayed_entry)
4944 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4946 next = got_object_tree_get_first_entry(tree);
4948 last = last_displayed_entry;
4949 while (next && last && n++ < maxscroll) {
4950 last = got_tree_entry_get_next(tree, last);
4952 *first_displayed_entry = next;
4953 next = got_tree_entry_get_next(tree, next);
4959 static const struct got_error *
4960 tree_entry_path(char **path, struct tog_parent_trees *parents,
4961 struct got_tree_entry *te)
4963 const struct got_error *err = NULL;
4964 struct tog_parent_tree *pt;
4965 size_t len = 2; /* for leading slash and NUL */
4967 TAILQ_FOREACH(pt, parents, entry)
4968 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4971 len += strlen(got_tree_entry_get_name(te));
4973 *path = calloc(1, len);
4975 return got_error_from_errno("calloc");
4978 pt = TAILQ_LAST(parents, tog_parent_trees);
4980 const char *name = got_tree_entry_get_name(pt->selected_entry);
4981 if (strlcat(*path, name, len) >= len) {
4982 err = got_error(GOT_ERR_NO_SPACE);
4985 if (strlcat(*path, "/", len) >= len) {
4986 err = got_error(GOT_ERR_NO_SPACE);
4989 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4992 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4993 err = got_error(GOT_ERR_NO_SPACE);
5005 static const struct got_error *
5006 blame_tree_entry(struct tog_view **new_view, int begin_x,
5007 struct got_tree_entry *te, struct tog_parent_trees *parents,
5008 struct got_object_id *commit_id, struct got_reflist_head *refs,
5009 struct got_repository *repo)
5011 const struct got_error *err = NULL;
5013 struct tog_view *blame_view;
5017 err = tree_entry_path(&path, parents, te);
5021 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5022 if (blame_view == NULL) {
5023 err = got_error_from_errno("view_open");
5027 err = open_blame_view(blame_view, path, commit_id, refs, repo);
5029 if (err->code == GOT_ERR_CANCELLED)
5031 view_close(blame_view);
5033 *new_view = blame_view;
5039 static const struct got_error *
5040 log_tree_entry(struct tog_view **new_view, int begin_x,
5041 struct got_tree_entry *te, struct tog_parent_trees *parents,
5042 struct got_object_id *commit_id, struct got_reflist_head *refs,
5043 struct got_repository *repo)
5045 struct tog_view *log_view;
5046 const struct got_error *err = NULL;
5051 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5052 if (log_view == NULL)
5053 return got_error_from_errno("view_open");
5055 err = tree_entry_path(&path, parents, te);
5059 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
5061 view_close(log_view);
5063 *new_view = log_view;
5068 static const struct got_error *
5069 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5070 struct got_object_id *commit_id, struct got_reflist_head *refs,
5071 struct got_repository *repo)
5073 const struct got_error *err = NULL;
5074 char *commit_id_str = NULL;
5075 struct tog_tree_view_state *s = &view->state.tree;
5077 TAILQ_INIT(&s->parents);
5079 err = got_object_id_str(&commit_id_str, commit_id);
5083 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5084 err = got_error_from_errno("asprintf");
5088 s->root = s->tree = root;
5089 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5090 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5091 s->commit_id = got_object_id_dup(commit_id);
5092 if (s->commit_id == NULL) {
5093 err = got_error_from_errno("got_object_id_dup");
5099 SIMPLEQ_INIT(&s->colors);
5101 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5102 err = add_color(&s->colors, "\\$$",
5103 TOG_COLOR_TREE_SUBMODULE,
5104 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5107 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5108 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5110 free_colors(&s->colors);
5113 err = add_color(&s->colors, "/$",
5114 TOG_COLOR_TREE_DIRECTORY,
5115 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5117 free_colors(&s->colors);
5121 err = add_color(&s->colors, "\\*$",
5122 TOG_COLOR_TREE_EXECUTABLE,
5123 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5125 free_colors(&s->colors);
5129 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5130 get_color_value("TOG_COLOR_COMMIT"));
5132 free_colors(&s->colors);
5137 view->show = show_tree_view;
5138 view->input = input_tree_view;
5139 view->close = close_tree_view;
5140 view->search_start = search_start_tree_view;
5141 view->search_next = search_next_tree_view;
5143 free(commit_id_str);
5145 free(s->tree_label);
5146 s->tree_label = NULL;
5151 static const struct got_error *
5152 close_tree_view(struct tog_view *view)
5154 struct tog_tree_view_state *s = &view->state.tree;
5156 free_colors(&s->colors);
5157 free(s->tree_label);
5158 s->tree_label = NULL;
5160 s->commit_id = NULL;
5161 while (!TAILQ_EMPTY(&s->parents)) {
5162 struct tog_parent_tree *parent;
5163 parent = TAILQ_FIRST(&s->parents);
5164 TAILQ_REMOVE(&s->parents, parent, entry);
5168 if (s->tree != s->root)
5169 got_object_tree_close(s->tree);
5170 got_object_tree_close(s->root);
5175 static const struct got_error *
5176 search_start_tree_view(struct tog_view *view)
5178 struct tog_tree_view_state *s = &view->state.tree;
5180 s->matched_entry = NULL;
5185 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5187 regmatch_t regmatch;
5189 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5193 static const struct got_error *
5194 search_next_tree_view(struct tog_view *view)
5196 struct tog_tree_view_state *s = &view->state.tree;
5197 struct got_tree_entry *te = NULL;
5199 if (!view->searching) {
5200 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5204 if (s->matched_entry) {
5205 if (view->searching == TOG_SEARCH_FORWARD) {
5206 if (s->selected_entry)
5207 te = got_tree_entry_get_next(s->tree,
5210 te = got_object_tree_get_first_entry(s->tree);
5212 if (s->selected_entry == NULL)
5213 te = got_object_tree_get_last_entry(s->tree);
5215 te = got_tree_entry_get_prev(s->tree,
5219 if (view->searching == TOG_SEARCH_FORWARD)
5220 te = got_object_tree_get_first_entry(s->tree);
5222 te = got_object_tree_get_last_entry(s->tree);
5227 if (s->matched_entry == NULL) {
5228 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5231 if (view->searching == TOG_SEARCH_FORWARD)
5232 te = got_object_tree_get_first_entry(s->tree);
5234 te = got_object_tree_get_last_entry(s->tree);
5237 if (match_tree_entry(te, &view->regex)) {
5238 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5239 s->matched_entry = te;
5243 if (view->searching == TOG_SEARCH_FORWARD)
5244 te = got_tree_entry_get_next(s->tree, te);
5246 te = got_tree_entry_get_prev(s->tree, te);
5249 if (s->matched_entry) {
5250 s->first_displayed_entry = s->matched_entry;
5257 static const struct got_error *
5258 show_tree_view(struct tog_view *view)
5260 const struct got_error *err = NULL;
5261 struct tog_tree_view_state *s = &view->state.tree;
5264 err = tree_entry_path(&parent_path, &s->parents, NULL);
5268 err = draw_tree_entries(view, &s->first_displayed_entry,
5269 &s->last_displayed_entry, &s->selected_entry,
5270 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5271 s->tree, s->selected, view->nlines, s->tree == s->root,
5272 &s->colors, s->repo);
5279 static const struct got_error *
5280 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5281 struct tog_view **focus_view, struct tog_view *view, int ch)
5283 const struct got_error *err = NULL;
5284 struct tog_tree_view_state *s = &view->state.tree;
5285 struct tog_view *log_view;
5286 int begin_x = 0, nscrolled;
5290 s->show_ids = !s->show_ids;
5293 if (!s->selected_entry)
5295 if (view_is_parent_view(view))
5296 begin_x = view_split_begin_x(view->begin_x);
5297 err = log_tree_entry(&log_view, begin_x,
5298 s->selected_entry, &s->parents,
5299 s->commit_id, s->refs, s->repo);
5300 if (view_is_parent_view(view)) {
5301 err = view_close_child(view);
5304 err = view_set_child(view, log_view);
5306 view_close(log_view);
5309 *focus_view = log_view;
5310 view->child_focussed = 1;
5312 *new_view = log_view;
5316 if (s->selected > 0) {
5318 if (s->selected == 0)
5321 if (s->selected > 0)
5323 tree_scroll_up(view, &s->first_displayed_entry, 1,
5324 s->tree, s->tree == s->root);
5328 tree_scroll_up(view, &s->first_displayed_entry,
5329 MAX(0, view->nlines - 4 - s->selected), s->tree,
5330 s->tree == s->root);
5332 if (got_object_tree_get_first_entry(s->tree) ==
5333 s->first_displayed_entry && s->tree != s->root)
5334 s->first_displayed_entry = NULL;
5338 if (s->selected < s->ndisplayed - 1) {
5342 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5344 /* can't scroll any further */
5346 tree_scroll_down(&s->first_displayed_entry, 1,
5347 s->last_displayed_entry, s->tree);
5351 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5353 /* can't scroll any further; move cursor down */
5354 if (s->selected < s->ndisplayed - 1)
5355 s->selected = s->ndisplayed - 1;
5358 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5359 view->nlines, s->last_displayed_entry, s->tree);
5360 if (nscrolled < view->nlines) {
5362 struct got_tree_entry *te;
5363 te = s->first_displayed_entry;
5366 te = got_tree_entry_get_next(s->tree, te);
5368 s->selected = ndisplayed - 1;
5374 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5375 struct tog_parent_tree *parent;
5376 /* user selected '..' */
5377 if (s->tree == s->root)
5379 parent = TAILQ_FIRST(&s->parents);
5380 TAILQ_REMOVE(&s->parents, parent,
5382 got_object_tree_close(s->tree);
5383 s->tree = parent->tree;
5384 s->first_displayed_entry =
5385 parent->first_displayed_entry;
5387 parent->selected_entry;
5388 s->selected = parent->selected;
5390 } else if (S_ISDIR(got_tree_entry_get_mode(
5391 s->selected_entry))) {
5392 struct got_tree_object *subtree;
5393 err = got_object_open_as_tree(&subtree, s->repo,
5394 got_tree_entry_get_id(s->selected_entry));
5397 err = tree_view_visit_subtree(subtree, s);
5399 got_object_tree_close(subtree);
5402 } else if (S_ISREG(got_tree_entry_get_mode(
5403 s->selected_entry))) {
5404 struct tog_view *blame_view;
5405 int begin_x = view_is_parent_view(view) ?
5406 view_split_begin_x(view->begin_x) : 0;
5408 err = blame_tree_entry(&blame_view, begin_x,
5409 s->selected_entry, &s->parents,
5410 s->commit_id, s->refs, s->repo);
5413 if (view_is_parent_view(view)) {
5414 err = view_close_child(view);
5417 err = view_set_child(view, blame_view);
5419 view_close(blame_view);
5422 *focus_view = blame_view;
5423 view->child_focussed = 1;
5425 *new_view = blame_view;
5429 if (s->selected > view->nlines)
5430 s->selected = s->ndisplayed - 1;
5443 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5448 static const struct got_error *
5449 cmd_tree(int argc, char *argv[])
5451 const struct got_error *error;
5452 struct got_repository *repo = NULL;
5453 struct got_worktree *worktree = NULL;
5454 struct got_reflist_head refs;
5455 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5456 struct got_object_id *commit_id = NULL;
5457 char *commit_id_arg = NULL;
5458 struct got_commit_object *commit = NULL;
5459 struct got_tree_object *tree = NULL;
5461 struct tog_view *view;
5463 SIMPLEQ_INIT(&refs);
5466 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5471 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5474 commit_id_arg = optarg;
5477 repo_path = realpath(optarg, NULL);
5478 if (repo_path == NULL)
5479 return got_error_from_errno2("realpath",
5494 cwd = getcwd(NULL, 0);
5496 return got_error_from_errno("getcwd");
5498 error = got_worktree_open(&worktree, cwd);
5499 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5502 if (repo_path == NULL) {
5505 strdup(got_worktree_get_repo_path(worktree));
5507 repo_path = strdup(cwd);
5509 if (repo_path == NULL) {
5510 error = got_error_from_errno("strdup");
5514 error = got_repo_open(&repo, repo_path, NULL);
5518 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5525 error = apply_unveil(got_repo_get_path(repo), NULL);
5529 error = got_repo_match_object_id(&commit_id, NULL,
5530 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5531 GOT_OBJ_TYPE_COMMIT, 1, repo);
5535 error = got_object_open_as_commit(&commit, repo, commit_id);
5539 error = got_object_open_as_tree(&tree, repo,
5540 got_object_commit_get_tree_id(commit));
5544 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5548 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5550 error = got_error_from_errno("view_open");
5553 error = open_tree_view(view, tree, commit_id, &refs, repo);
5556 if (!got_path_is_root_dir(in_repo_path)) {
5557 error = tree_view_walk_path(&view->state.tree, commit_id,
5558 in_repo_path, repo);
5564 /* Release work tree lock. */
5565 got_worktree_close(worktree);
5568 error = view_loop(view);
5574 got_object_commit_close(commit);
5576 got_object_tree_close(tree);
5578 got_repo_close(repo);
5579 got_ref_list_free(&refs);
5584 list_commands(FILE *fp)
5588 fprintf(fp, "commands:");
5589 for (i = 0; i < nitems(tog_commands); i++) {
5590 struct tog_cmd *cmd = &tog_commands[i];
5591 fprintf(fp, " %s", cmd->name);
5597 usage(int hflag, int status)
5599 FILE *fp = (status == 0) ? stdout : stderr;
5601 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5604 fprintf(fp, "lazy usage: %s path\n", getprogname());
5611 make_argv(int argc, ...)
5619 argv = calloc(argc, sizeof(char *));
5622 for (i = 0; i < argc; i++) {
5623 argv[i] = strdup(va_arg(ap, char *));
5624 if (argv[i] == NULL)
5633 * Try to convert 'tog path' into a 'tog log path' command.
5634 * The user could simply have mistyped the command rather than knowingly
5635 * provided a path. So check whether argv[0] can in fact be resolved
5636 * to a path in the HEAD commit and print a special error if not.
5637 * This hack is for mpi@ <3
5639 static const struct got_error *
5640 tog_log_with_path(int argc, char *argv[])
5642 const struct got_error *error = NULL;
5643 struct tog_cmd *cmd = NULL;
5644 struct got_repository *repo = NULL;
5645 struct got_worktree *worktree = NULL;
5646 struct got_object_id *commit_id = NULL, *id = NULL;
5647 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5648 char *commit_id_str = NULL, **cmd_argv = NULL;
5650 cwd = getcwd(NULL, 0);
5652 return got_error_from_errno("getcwd");
5654 error = got_worktree_open(&worktree, cwd);
5655 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5659 repo_path = strdup(got_worktree_get_repo_path(worktree));
5661 repo_path = strdup(cwd);
5662 if (repo_path == NULL) {
5663 error = got_error_from_errno("strdup");
5667 error = got_repo_open(&repo, repo_path, NULL);
5671 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5676 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
5677 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
5678 GOT_OBJ_TYPE_COMMIT, 1, repo);
5683 got_worktree_close(worktree);
5687 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
5689 if (error->code != GOT_ERR_NO_TREE_ENTRY)
5691 fprintf(stderr, "%s: '%s' is no known command or path\n",
5692 getprogname(), argv[0]);
5697 got_repo_close(repo);
5700 error = got_object_id_str(&commit_id_str, commit_id);
5704 cmd = &tog_commands[0]; /* log */
5706 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
5707 error = cmd->cmd_main(argc, cmd_argv);
5710 got_repo_close(repo);
5712 got_worktree_close(worktree);
5714 free(commit_id_str);
5721 for (i = 0; i < argc; i++)
5729 main(int argc, char *argv[])
5731 const struct got_error *error = NULL;
5732 struct tog_cmd *cmd = NULL;
5733 int ch, hflag = 0, Vflag = 0;
5734 char **cmd_argv = NULL;
5735 static struct option longopts[] = {
5736 { "version", no_argument, NULL, 'V' },
5740 setlocale(LC_CTYPE, "");
5742 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5762 got_version_print_str();
5769 /* Build an argument vector which runs a default command. */
5770 cmd = &tog_commands[0];
5772 cmd_argv = make_argv(argc, cmd->name);
5776 /* Did the user specify a command? */
5777 for (i = 0; i < nitems(tog_commands); i++) {
5778 if (strncmp(tog_commands[i].name, argv[0],
5779 strlen(argv[0])) == 0) {
5780 cmd = &tog_commands[i];
5789 /* No command specified; try log with a path */
5790 error = tog_log_with_path(argc, argv);
5795 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5802 for (i = 0; i < argc; i++)
5807 if (error && error->code != GOT_ERR_CANCELLED)
5808 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);