2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
22 #define _XOPEN_SOURCE_EXTENDED
24 #undef _XOPEN_SOURCE_EXTENDED
41 #include "got_version.h"
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
47 #include "got_opentemp.h"
49 #include "got_cancel.h"
50 #include "got_commit_graph.h"
51 #include "got_blame.h"
52 #include "got_privsep.h"
54 #include "got_worktree.h"
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
64 #define CTRL(x) ((x) & 0x1f)
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
72 const struct got_error *(*cmd_main)(int, char *[]);
73 void (*cmd_usage)(void);
76 __dead static void usage(int);
77 __dead static void usage_log(void);
78 __dead static void usage_diff(void);
79 __dead static void usage_blame(void);
80 __dead static void usage_tree(void);
82 static const struct got_error* cmd_log(int, char *[]);
83 static const struct got_error* cmd_diff(int, char *[]);
84 static const struct got_error* cmd_blame(int, char *[]);
85 static const struct got_error* cmd_tree(int, char *[]);
87 static struct tog_cmd tog_commands[] = {
88 { "log", cmd_log, usage_log },
89 { "diff", cmd_diff, usage_diff },
90 { "blame", cmd_blame, usage_blame },
91 { "tree", cmd_tree, usage_tree },
101 #define TOG_EOF_STRING "(END)"
103 struct commit_queue_entry {
104 TAILQ_ENTRY(commit_queue_entry) entry;
105 struct got_object_id *id;
106 struct got_commit_object *commit;
109 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
110 struct commit_queue {
112 struct commit_queue_head head;
116 SIMPLEQ_ENTRY(tog_color) entry;
120 SIMPLEQ_HEAD(tog_colors, tog_color);
122 static const struct got_error *
123 add_color(struct tog_colors *colors, const char *pattern,
124 int idx, short color)
126 const struct got_error *err = NULL;
127 struct tog_color *tc;
130 if (idx < 1 || idx > COLOR_PAIRS - 1)
133 init_pair(idx, color, -1);
135 tc = calloc(1, sizeof(*tc));
137 return got_error_from_errno("calloc");
138 regerr = regcomp(&tc->regex, pattern,
139 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
141 static char regerr_msg[512];
142 static char err_msg[512];
143 regerror(regerr, &tc->regex, regerr_msg,
145 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
147 err = got_error_msg(GOT_ERR_REGEX, err_msg);
152 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
157 free_colors(struct tog_colors *colors)
159 struct tog_color *tc;
161 while (!SIMPLEQ_EMPTY(colors)) {
162 tc = SIMPLEQ_FIRST(colors);
163 SIMPLEQ_REMOVE_HEAD(colors, entry);
170 get_color(struct tog_colors *colors, int colorpair)
172 struct tog_color *tc = NULL;
174 SIMPLEQ_FOREACH(tc, colors, entry) {
175 if (tc->colorpair == colorpair)
183 default_color_value(const char *envvar)
185 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
186 return COLOR_MAGENTA;
187 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
189 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
191 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
193 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
194 return COLOR_MAGENTA;
195 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
197 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
199 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
201 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
203 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
205 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
212 get_color_value(const char *envvar)
214 const char *val = getenv(envvar);
217 return default_color_value(envvar);
219 if (strcasecmp(val, "black") == 0)
221 if (strcasecmp(val, "red") == 0)
223 if (strcasecmp(val, "green") == 0)
225 if (strcasecmp(val, "yellow") == 0)
227 if (strcasecmp(val, "blue") == 0)
229 if (strcasecmp(val, "magenta") == 0)
230 return COLOR_MAGENTA;
231 if (strcasecmp(val, "cyan") == 0)
233 if (strcasecmp(val, "white") == 0)
235 if (strcasecmp(val, "default") == 0)
238 return default_color_value(envvar);
242 struct tog_diff_view_state {
243 struct got_object_id *id1, *id2;
245 int first_displayed_line;
246 int last_displayed_line;
249 struct got_repository *repo;
250 struct got_reflist_head *refs;
251 struct tog_colors colors;
253 /* passed from log view; may be NULL */
254 struct tog_view *log_view;
257 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
259 struct tog_log_thread_args {
260 pthread_cond_t need_commits;
262 struct got_commit_graph *graph;
263 struct commit_queue *commits;
264 const char *in_repo_path;
265 struct got_object_id *start_id;
266 struct got_repository *repo;
269 struct commit_queue_entry **first_displayed_entry;
270 struct commit_queue_entry **selected_entry;
272 int *search_next_done;
276 struct tog_log_view_state {
277 struct commit_queue commits;
278 struct commit_queue_entry *first_displayed_entry;
279 struct commit_queue_entry *last_displayed_entry;
280 struct commit_queue_entry *selected_entry;
283 const char *head_ref_name;
284 struct got_repository *repo;
285 struct got_reflist_head *refs;
286 struct got_object_id *start_id;
289 struct tog_log_thread_args thread_args;
290 struct commit_queue_entry *matched_entry;
291 struct commit_queue_entry *search_entry;
292 struct tog_colors colors;
295 #define TOG_COLOR_DIFF_MINUS 1
296 #define TOG_COLOR_DIFF_PLUS 2
297 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
298 #define TOG_COLOR_DIFF_META 4
299 #define TOG_COLOR_TREE_SUBMODULE 5
300 #define TOG_COLOR_TREE_SYMLINK 6
301 #define TOG_COLOR_TREE_DIRECTORY 7
302 #define TOG_COLOR_TREE_EXECUTABLE 8
303 #define TOG_COLOR_COMMIT 9
304 #define TOG_COLOR_AUTHOR 10
305 #define TOG_COLOR_DATE 11
307 struct tog_blame_cb_args {
308 struct tog_blame_line *lines; /* one per line */
311 struct tog_view *view;
312 struct got_object_id *commit_id;
316 struct tog_blame_thread_args {
318 struct got_repository *repo;
319 struct tog_blame_cb_args *cb_args;
321 got_cancel_cb cancel_cb;
328 struct tog_blame_line *lines;
332 struct tog_blame_thread_args thread_args;
333 struct tog_blame_cb_args cb_args;
337 struct tog_blame_view_state {
338 int first_displayed_line;
339 int last_displayed_line;
344 struct got_object_id_queue blamed_commits;
345 struct got_object_qid *blamed_commit;
347 struct got_repository *repo;
348 struct got_reflist_head *refs;
349 struct got_object_id *commit_id;
350 struct tog_blame blame;
352 struct tog_colors colors;
355 struct tog_parent_tree {
356 TAILQ_ENTRY(tog_parent_tree) entry;
357 struct got_tree_object *tree;
358 struct got_tree_entry *first_displayed_entry;
359 struct got_tree_entry *selected_entry;
363 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
365 struct tog_tree_view_state {
367 struct got_tree_object *root;
368 struct got_tree_object *tree;
369 const struct got_tree_entries *entries;
370 struct got_tree_entry *first_displayed_entry;
371 struct got_tree_entry *last_displayed_entry;
372 struct got_tree_entry *selected_entry;
373 int ndisplayed, selected, show_ids;
374 struct tog_parent_trees parents;
375 struct got_object_id *commit_id;
376 struct got_repository *repo;
377 struct got_reflist_head *refs;
378 struct got_tree_entry *matched_entry;
379 struct tog_colors colors;
383 * We implement two types of views: parent views and child views.
385 * The 'Tab' key switches between a parent view and its child view.
386 * Child views are shown side-by-side to their parent view, provided
387 * there is enough screen estate.
389 * When a new view is opened from within a parent view, this new view
390 * becomes a child view of the parent view, replacing any existing child.
392 * When a new view is opened from within a child view, this new view
393 * becomes a parent view which will obscure the views below until the
394 * user quits the new parent view by typing 'q'.
396 * This list of views contains parent views only.
397 * Child views are only pointed to by their parent view.
399 TAILQ_HEAD(tog_view_list_head, tog_view);
402 TAILQ_ENTRY(tog_view) entry;
405 int nlines, ncols, begin_y, begin_x;
406 int lines, cols; /* copies of LINES and COLS */
408 struct tog_view *parent;
409 struct tog_view *child;
412 /* type-specific state */
413 enum tog_view_type type;
415 struct tog_diff_view_state diff;
416 struct tog_log_view_state log;
417 struct tog_blame_view_state blame;
418 struct tog_tree_view_state tree;
421 const struct got_error *(*show)(struct tog_view *);
422 const struct got_error *(*input)(struct tog_view **,
423 struct tog_view **, struct tog_view**, struct tog_view *, int);
424 const struct got_error *(*close)(struct tog_view *);
426 const struct got_error *(*search_start)(struct tog_view *);
427 const struct got_error *(*search_next)(struct tog_view *);
429 #define TOG_SEARCH_FORWARD 1
430 #define TOG_SEARCH_BACKWARD 2
431 int search_next_done;
435 static const struct got_error *open_diff_view(struct tog_view *,
436 struct got_object_id *, struct got_object_id *, struct tog_view *,
437 struct got_reflist_head *, struct got_repository *);
438 static const struct got_error *show_diff_view(struct tog_view *);
439 static const struct got_error *input_diff_view(struct tog_view **,
440 struct tog_view **, struct tog_view **, struct tog_view *, int);
441 static const struct got_error* close_diff_view(struct tog_view *);
443 static const struct got_error *open_log_view(struct tog_view *,
444 struct got_object_id *, struct got_reflist_head *,
445 struct got_repository *, const char *, const char *, int);
446 static const struct got_error * show_log_view(struct tog_view *);
447 static const struct got_error *input_log_view(struct tog_view **,
448 struct tog_view **, struct tog_view **, struct tog_view *, int);
449 static const struct got_error *close_log_view(struct tog_view *);
450 static const struct got_error *search_start_log_view(struct tog_view *);
451 static const struct got_error *search_next_log_view(struct tog_view *);
453 static const struct got_error *open_blame_view(struct tog_view *, char *,
454 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
455 static const struct got_error *show_blame_view(struct tog_view *);
456 static const struct got_error *input_blame_view(struct tog_view **,
457 struct tog_view **, struct tog_view **, struct tog_view *, int);
458 static const struct got_error *close_blame_view(struct tog_view *);
459 static const struct got_error *search_start_blame_view(struct tog_view *);
460 static const struct got_error *search_next_blame_view(struct tog_view *);
462 static const struct got_error *open_tree_view(struct tog_view *,
463 struct got_tree_object *, struct got_object_id *,
464 struct got_reflist_head *, struct got_repository *);
465 static const struct got_error *show_tree_view(struct tog_view *);
466 static const struct got_error *input_tree_view(struct tog_view **,
467 struct tog_view **, struct tog_view **, struct tog_view *, int);
468 static const struct got_error *close_tree_view(struct tog_view *);
469 static const struct got_error *search_start_tree_view(struct tog_view *);
470 static const struct got_error *search_next_tree_view(struct tog_view *);
472 static volatile sig_atomic_t tog_sigwinch_received;
473 static volatile sig_atomic_t tog_sigpipe_received;
476 tog_sigwinch(int signo)
478 tog_sigwinch_received = 1;
482 tog_sigpipe(int signo)
484 tog_sigpipe_received = 1;
487 static const struct got_error *
488 view_close(struct tog_view *view)
490 const struct got_error *err = NULL;
493 view_close(view->child);
497 err = view->close(view);
499 del_panel(view->panel);
501 delwin(view->window);
506 static struct tog_view *
507 view_open(int nlines, int ncols, int begin_y, int begin_x,
508 enum tog_view_type type)
510 struct tog_view *view = calloc(1, sizeof(*view));
518 view->nlines = nlines ? nlines : LINES - begin_y;
519 view->ncols = ncols ? ncols : COLS - begin_x;
520 view->begin_y = begin_y;
521 view->begin_x = begin_x;
522 view->window = newwin(nlines, ncols, begin_y, begin_x);
523 if (view->window == NULL) {
527 view->panel = new_panel(view->window);
528 if (view->panel == NULL ||
529 set_panel_userptr(view->panel, view) != OK) {
534 keypad(view->window, TRUE);
539 view_split_begin_x(int begin_x)
541 if (begin_x > 0 || COLS < 120)
543 return (COLS - MAX(COLS / 2, 80));
546 static const struct got_error *view_resize(struct tog_view *);
548 static const struct got_error *
549 view_splitscreen(struct tog_view *view)
551 const struct got_error *err = NULL;
554 view->begin_x = view_split_begin_x(0);
555 view->nlines = LINES;
556 view->ncols = COLS - view->begin_x;
559 err = view_resize(view);
563 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
564 return got_error_from_errno("mvwin");
569 static const struct got_error *
570 view_fullscreen(struct tog_view *view)
572 const struct got_error *err = NULL;
576 view->nlines = LINES;
580 err = view_resize(view);
584 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
585 return got_error_from_errno("mvwin");
591 view_is_parent_view(struct tog_view *view)
593 return view->parent == NULL;
596 static const struct got_error *
597 view_resize(struct tog_view *view)
601 if (view->lines > LINES)
602 nlines = view->nlines - (view->lines - LINES);
604 nlines = view->nlines + (LINES - view->lines);
606 if (view->cols > COLS)
607 ncols = view->ncols - (view->cols - COLS);
609 ncols = view->ncols + (COLS - view->cols);
611 if (wresize(view->window, nlines, ncols) == ERR)
612 return got_error_from_errno("wresize");
613 if (replace_panel(view->panel, view->window) == ERR)
614 return got_error_from_errno("replace_panel");
615 wclear(view->window);
617 view->nlines = nlines;
623 view->child->begin_x = view_split_begin_x(view->begin_x);
624 if (view->child->begin_x == 0) {
625 view_fullscreen(view->child);
626 if (view->child->focussed)
627 show_panel(view->child->panel);
629 show_panel(view->panel);
631 view_splitscreen(view->child);
632 show_panel(view->child->panel);
639 static const struct got_error *
640 view_close_child(struct tog_view *view)
642 const struct got_error *err = NULL;
644 if (view->child == NULL)
647 err = view_close(view->child);
652 static const struct got_error *
653 view_set_child(struct tog_view *view, struct tog_view *child)
655 const struct got_error *err = NULL;
658 child->parent = view;
663 view_is_splitscreen(struct tog_view *view)
665 return view->begin_x > 0;
674 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
675 cols = 80; /* Default */
681 resize_term(lines, cols);
684 static const struct got_error *
685 view_search_start(struct tog_view *view)
687 const struct got_error *err = NULL;
692 if (view->nlines < 1)
695 if (!view_is_parent_view(view))
696 begin_x = view_split_begin_x(view->begin_x);
697 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
699 wclrtoeol(view->window);
703 ret = wgetnstr(view->window, pattern, sizeof(pattern));
709 if (view->searching) {
710 regfree(&view->regex);
714 if (regcomp(&view->regex, pattern,
715 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
716 err = view->search_start(view);
718 regfree(&view->regex);
721 view->searching = TOG_SEARCH_FORWARD;
722 view->search_next_done = 0;
723 view->search_next(view);
729 static const struct got_error *
730 view_input(struct tog_view **new, struct tog_view **dead,
731 struct tog_view **focus, int *done, struct tog_view *view,
732 struct tog_view_list_head *views)
734 const struct got_error *err = NULL;
742 if (view->searching && !view->search_next_done) {
743 errcode = pthread_mutex_unlock(&tog_mutex);
745 return got_error_set_errno(errcode,
746 "pthread_mutex_unlock");
748 errcode = pthread_mutex_lock(&tog_mutex);
750 return got_error_set_errno(errcode,
751 "pthread_mutex_lock");
752 view->search_next(view);
756 nodelay(stdscr, FALSE);
757 /* Allow threads to make progress while we are waiting for input. */
758 errcode = pthread_mutex_unlock(&tog_mutex);
760 return got_error_set_errno(errcode, "pthread_mutex_unlock");
761 ch = wgetch(view->window);
762 errcode = pthread_mutex_lock(&tog_mutex);
764 return got_error_set_errno(errcode, "pthread_mutex_lock");
765 nodelay(stdscr, TRUE);
767 if (tog_sigwinch_received) {
769 tog_sigwinch_received = 0;
770 TAILQ_FOREACH(v, views, entry) {
771 err = view_resize(v);
774 err = v->input(new, dead, focus, v, KEY_RESIZE);
785 *focus = view->child;
786 view->child_focussed = 1;
787 } else if (view->parent) {
788 *focus = view->parent;
789 view->parent->child_focussed = 0;
793 err = view->input(new, dead, focus, view, ch);
800 if (view_is_parent_view(view)) {
801 if (view->child == NULL)
803 if (view_is_splitscreen(view->child)) {
804 *focus = view->child;
805 view->child_focussed = 1;
806 err = view_fullscreen(view->child);
808 err = view_splitscreen(view->child);
811 err = view->child->input(new, dead, focus,
812 view->child, KEY_RESIZE);
814 if (view_is_splitscreen(view)) {
816 view->parent->child_focussed = 1;
817 err = view_fullscreen(view);
819 err = view_splitscreen(view);
823 err = view->input(new, dead, focus, view,
830 if (view->search_start)
831 view_search_start(view);
833 err = view->input(new, dead, focus, view, ch);
837 if (view->search_next && view->searching) {
838 view->searching = (ch == 'n' ?
839 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
840 view->search_next_done = 0;
841 view->search_next(view);
843 err = view->input(new, dead, focus, view, ch);
846 err = view->input(new, dead, focus, view, ch);
854 view_vborder(struct tog_view *view)
857 struct tog_view *view_above;
860 return view_vborder(view->parent);
862 panel = panel_above(view->panel);
866 view_above = panel_userptr(panel);
867 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
868 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
872 view_needs_focus_indication(struct tog_view *view)
874 if (view_is_parent_view(view)) {
875 if (view->child == NULL || view->child_focussed)
877 if (!view_is_splitscreen(view->child))
879 } else if (!view_is_splitscreen(view))
882 return view->focussed;
885 static const struct got_error *
886 view_loop(struct tog_view *view)
888 const struct got_error *err = NULL;
889 struct tog_view_list_head views;
890 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
891 int fast_refresh = 10;
892 int done = 0, errcode;
894 errcode = pthread_mutex_lock(&tog_mutex);
896 return got_error_set_errno(errcode, "pthread_mutex_lock");
899 TAILQ_INSERT_HEAD(&views, view, entry);
903 err = view->show(view);
908 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
909 /* Refresh fast during initialization, then become slower. */
910 if (fast_refresh && fast_refresh-- == 0)
911 halfdelay(10); /* switch to once per second */
913 err = view_input(&new_view, &dead_view, &focus_view, &done,
918 struct tog_view *prev = NULL;
920 if (view_is_parent_view(dead_view))
921 prev = TAILQ_PREV(dead_view,
922 tog_view_list_head, entry);
923 else if (view->parent != dead_view)
926 if (dead_view->parent)
927 dead_view->parent->child = NULL;
929 TAILQ_REMOVE(&views, dead_view, entry);
931 err = view_close(dead_view);
932 if (err || (dead_view == main_view && new_view == NULL))
935 if (view == dead_view) {
940 else if (!TAILQ_EMPTY(&views))
941 view = TAILQ_LAST(&views,
946 if (view->child && view->child_focussed)
947 focus_view = view->child;
954 struct tog_view *v, *t;
955 /* Only allow one parent view per type. */
956 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
957 if (v->type != new_view->type)
959 TAILQ_REMOVE(&views, v, entry);
965 TAILQ_INSERT_TAIL(&views, new_view, entry);
967 if (focus_view == NULL)
968 focus_view = new_view;
971 show_panel(focus_view->panel);
974 focus_view->focussed = 1;
977 show_panel(new_view->panel);
978 if (view->child && view_is_splitscreen(view->child))
979 show_panel(view->child->panel);
982 if (focus_view == NULL) {
984 show_panel(view->panel);
985 if (view->child && view_is_splitscreen(view->child))
986 show_panel(view->child->panel);
990 err = view->parent->show(view->parent);
994 err = view->show(view);
998 err = view->child->show(view->child);
1007 while (!TAILQ_EMPTY(&views)) {
1008 view = TAILQ_FIRST(&views);
1009 TAILQ_REMOVE(&views, view, entry);
1013 errcode = pthread_mutex_unlock(&tog_mutex);
1014 if (errcode && err == NULL)
1015 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1025 "usage: %s log [-c commit] [-r repository-path] [path]\n",
1030 /* Create newly allocated wide-character string equivalent to a byte string. */
1031 static const struct got_error *
1032 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1035 const struct got_error *err = NULL;
1038 *wlen = mbstowcs(NULL, s, 0);
1039 if (*wlen == (size_t)-1) {
1041 if (errno != EILSEQ)
1042 return got_error_from_errno("mbstowcs");
1044 /* byte string invalid in current encoding; try to "fix" it */
1045 err = got_mbsavis(&vis, &vislen, s);
1048 *wlen = mbstowcs(NULL, vis, 0);
1049 if (*wlen == (size_t)-1) {
1050 err = got_error_from_errno("mbstowcs"); /* give up */
1055 *ws = calloc(*wlen + 1, sizeof(**ws));
1057 err = got_error_from_errno("calloc");
1061 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1062 err = got_error_from_errno("mbstowcs");
1073 /* Format a line for display, ensuring that it won't overflow a width limit. */
1074 static const struct got_error *
1075 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1078 const struct got_error *err = NULL;
1080 wchar_t *wline = NULL;
1087 err = mbs2ws(&wline, &wlen, line);
1093 int width = wcwidth(wline[i]);
1100 if (width == 1 || width == 2) {
1101 if (cols + width > wlimit)
1105 } else if (width == -1) {
1106 if (wline[i] == L'\t') {
1108 ((cols + col_tab_align) % TABSIZE);
1109 if (cols + width > wlimit)
1115 err = got_error_from_errno("wcwidth");
1130 static const struct got_error*
1131 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1132 struct got_object_id *id, struct got_repository *repo)
1134 static const struct got_error *err = NULL;
1135 struct got_reflist_entry *re;
1141 SIMPLEQ_FOREACH(re, refs, entry) {
1142 struct got_tag_object *tag = NULL;
1145 name = got_ref_get_name(re->ref);
1146 if (strcmp(name, GOT_REF_HEAD) == 0)
1148 if (strncmp(name, "refs/", 5) == 0)
1150 if (strncmp(name, "got/", 4) == 0)
1152 if (strncmp(name, "heads/", 6) == 0)
1154 if (strncmp(name, "remotes/", 8) == 0)
1156 if (strncmp(name, "tags/", 5) == 0) {
1157 err = got_object_open_as_tag(&tag, repo, re->id);
1159 if (err->code != GOT_ERR_OBJ_TYPE)
1161 /* Ref points at something other than a tag. */
1166 cmp = got_object_id_cmp(tag ?
1167 got_object_tag_get_object_id(tag) : re->id, id);
1169 got_object_tag_close(tag);
1173 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1174 s ? ", " : "", name) == -1) {
1175 err = got_error_from_errno("asprintf");
1186 static const struct got_error *
1187 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1190 char *smallerthan, *at;
1192 smallerthan = strchr(author, '<');
1193 if (smallerthan && smallerthan[1] != '\0')
1194 author = smallerthan + 1;
1195 at = strchr(author, '@');
1198 return format_line(wauthor, author_width, author, limit, col_tab_align);
1201 static const struct got_error *
1202 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1203 struct got_object_id *id, struct got_reflist_head *refs,
1204 const size_t date_display_cols, int author_display_cols,
1205 struct tog_colors *colors)
1207 const struct got_error *err = NULL;
1208 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1209 char *logmsg0 = NULL, *logmsg = NULL;
1210 char *author = NULL;
1211 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1212 int author_width, logmsg_width;
1213 char *newline, *line = NULL;
1215 const int avail = view->ncols;
1217 time_t committer_time;
1218 struct tog_color *tc;
1220 committer_time = got_object_commit_get_committer_time(commit);
1221 if (localtime_r(&committer_time, &tm) == NULL)
1222 return got_error_from_errno("localtime_r");
1223 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1225 return got_error(GOT_ERR_NO_SPACE);
1227 if (avail <= date_display_cols)
1228 limit = MIN(sizeof(datebuf) - 1, avail);
1230 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1231 tc = get_color(colors, TOG_COLOR_DATE);
1233 wattr_on(view->window,
1234 COLOR_PAIR(tc->colorpair), NULL);
1235 waddnstr(view->window, datebuf, limit);
1237 wattr_off(view->window,
1238 COLOR_PAIR(tc->colorpair), NULL);
1245 err = got_object_id_str(&id_str, id);
1248 tc = get_color(colors, TOG_COLOR_COMMIT);
1250 wattr_on(view->window,
1251 COLOR_PAIR(tc->colorpair), NULL);
1252 wprintw(view->window, "%.8s ", id_str);
1254 wattr_off(view->window,
1255 COLOR_PAIR(tc->colorpair), NULL);
1262 author = strdup(got_object_commit_get_author(commit));
1263 if (author == NULL) {
1264 err = got_error_from_errno("strdup");
1267 err = format_author(&wauthor, &author_width, author, avail - col, col);
1270 tc = get_color(colors, TOG_COLOR_AUTHOR);
1272 wattr_on(view->window,
1273 COLOR_PAIR(tc->colorpair), NULL);
1274 waddwstr(view->window, wauthor);
1276 wattr_off(view->window,
1277 COLOR_PAIR(tc->colorpair), NULL);
1278 col += author_width;
1279 while (col < avail && author_width < author_display_cols + 2) {
1280 waddch(view->window, ' ');
1287 err = got_object_commit_get_logmsg(&logmsg0, commit);
1291 while (*logmsg == '\n')
1293 newline = strchr(logmsg, '\n');
1296 limit = avail - col;
1297 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, 0);
1300 waddwstr(view->window, wlogmsg);
1301 col += logmsg_width;
1302 while (col < avail) {
1303 waddch(view->window, ' ');
1315 static struct commit_queue_entry *
1316 alloc_commit_queue_entry(struct got_commit_object *commit,
1317 struct got_object_id *id)
1319 struct commit_queue_entry *entry;
1321 entry = calloc(1, sizeof(*entry));
1326 entry->commit = commit;
1331 pop_commit(struct commit_queue *commits)
1333 struct commit_queue_entry *entry;
1335 entry = TAILQ_FIRST(&commits->head);
1336 TAILQ_REMOVE(&commits->head, entry, entry);
1337 got_object_commit_close(entry->commit);
1338 commits->ncommits--;
1339 /* Don't free entry->id! It is owned by the commit graph. */
1344 free_commits(struct commit_queue *commits)
1346 while (!TAILQ_EMPTY(&commits->head))
1347 pop_commit(commits);
1350 static const struct got_error *
1351 match_commit(int *have_match, struct got_object_id *id,
1352 struct got_commit_object *commit, regex_t *regex)
1354 const struct got_error *err = NULL;
1355 regmatch_t regmatch;
1356 char *id_str = NULL, *logmsg = NULL;
1360 err = got_object_id_str(&id_str, id);
1364 err = got_object_commit_get_logmsg(&logmsg, commit);
1368 if (regexec(regex, got_object_commit_get_author(commit), 1,
1369 ®match, 0) == 0 ||
1370 regexec(regex, got_object_commit_get_committer(commit), 1,
1371 ®match, 0) == 0 ||
1372 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1373 regexec(regex, logmsg, 1, ®match, 0) == 0)
1381 static const struct got_error *
1382 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1383 int minqueue, struct got_repository *repo, const char *path,
1384 int *searching, int *search_next_done, regex_t *regex)
1386 const struct got_error *err = NULL;
1387 int nqueued = 0, have_match = 0;
1390 * We keep all commits open throughout the lifetime of the log
1391 * view in order to avoid having to re-fetch commits from disk
1392 * while updating the display.
1394 while (nqueued < minqueue ||
1395 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1396 struct got_object_id *id;
1397 struct got_commit_object *commit;
1398 struct commit_queue_entry *entry;
1401 err = got_commit_graph_iter_next(&id, graph);
1403 if (err->code != GOT_ERR_ITER_NEED_MORE)
1405 err = got_commit_graph_fetch_commits(graph,
1406 minqueue, repo, NULL, NULL);
1415 err = got_object_open_as_commit(&commit, repo, id);
1418 entry = alloc_commit_queue_entry(commit, id);
1419 if (entry == NULL) {
1420 err = got_error_from_errno("alloc_commit_queue_entry");
1424 errcode = pthread_mutex_lock(&tog_mutex);
1426 err = got_error_set_errno(errcode,
1427 "pthread_mutex_lock");
1431 entry->idx = commits->ncommits;
1432 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1434 commits->ncommits++;
1436 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1437 err = match_commit(&have_match, id, commit, regex);
1439 pthread_mutex_lock(&tog_mutex);
1444 errcode = pthread_mutex_unlock(&tog_mutex);
1445 if (errcode && err == NULL)
1446 err = got_error_set_errno(errcode,
1447 "pthread_mutex_unlock");
1456 static const struct got_error *
1457 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1458 struct got_repository *repo)
1460 const struct got_error *err = NULL;
1461 struct got_reference *head_ref;
1465 err = got_ref_open(&head_ref, repo, branch_name, 0);
1469 err = got_ref_resolve(head_id, repo, head_ref);
1470 got_ref_close(head_ref);
1479 static const struct got_error *
1480 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1481 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1482 struct commit_queue *commits, int selected_idx, int limit,
1483 struct got_reflist_head *refs, const char *path, int commits_needed,
1484 struct tog_colors *colors)
1486 const struct got_error *err = NULL;
1487 struct tog_log_view_state *s = &view->state.log;
1488 struct commit_queue_entry *entry;
1490 int ncommits, author_cols = 10;
1491 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1492 char *refs_str = NULL;
1494 struct tog_color *tc;
1495 static const size_t date_display_cols = 9;
1500 if (ncommits == selected_idx) {
1504 entry = TAILQ_NEXT(entry, entry);
1508 if (*selected && !(view->searching && view->search_next_done == 0)) {
1509 err = got_object_id_str(&id_str, (*selected)->id);
1513 err = build_refs_str(&refs_str, refs, (*selected)->id,
1520 if (commits_needed == 0)
1521 halfdelay(10); /* disable fast refresh */
1523 if (asprintf(&ncommits_str, " [%d/%d] %s",
1524 entry ? entry->idx + 1 : 0, commits->ncommits,
1525 commits_needed > 0 ?
1526 (view->searching && view->search_next_done == 0
1527 ? "searching..." : "loading... ") :
1528 (refs_str ? refs_str : "")) == -1) {
1529 err = got_error_from_errno("asprintf");
1533 if (path && strcmp(path, "/") != 0) {
1534 if (asprintf(&header, "commit %s %s%s",
1535 id_str ? id_str : "........................................",
1536 path, ncommits_str) == -1) {
1537 err = got_error_from_errno("asprintf");
1541 } else if (asprintf(&header, "commit %s%s",
1542 id_str ? id_str : "........................................",
1543 ncommits_str) == -1) {
1544 err = got_error_from_errno("asprintf");
1548 err = format_line(&wline, &width, header, view->ncols, 0);
1552 werase(view->window);
1554 if (view_needs_focus_indication(view))
1555 wstandout(view->window);
1556 tc = get_color(colors, TOG_COLOR_COMMIT);
1558 wattr_on(view->window,
1559 COLOR_PAIR(tc->colorpair), NULL);
1560 waddwstr(view->window, wline);
1562 wattr_off(view->window,
1563 COLOR_PAIR(tc->colorpair), NULL);
1564 while (width < view->ncols) {
1565 waddch(view->window, ' ');
1568 if (view_needs_focus_indication(view))
1569 wstandend(view->window);
1574 /* Grow author column size if necessary. */
1581 if (ncommits >= limit - 1)
1583 author = strdup(got_object_commit_get_author(entry->commit));
1584 if (author == NULL) {
1585 err = got_error_from_errno("strdup");
1588 err = format_author(&wauthor, &width, author, COLS,
1590 if (author_cols < width)
1591 author_cols = width;
1595 entry = TAILQ_NEXT(entry, entry);
1602 if (ncommits >= limit - 1)
1604 if (ncommits == selected_idx)
1605 wstandout(view->window);
1606 err = draw_commit(view, entry->commit, entry->id, refs,
1607 date_display_cols, author_cols, colors);
1608 if (ncommits == selected_idx)
1609 wstandend(view->window);
1614 entry = TAILQ_NEXT(entry, entry);
1627 scroll_up(struct tog_view *view,
1628 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1629 struct commit_queue *commits)
1631 struct commit_queue_entry *entry;
1634 entry = TAILQ_FIRST(&commits->head);
1635 if (*first_displayed_entry == entry)
1638 entry = *first_displayed_entry;
1639 while (entry && nscrolled < maxscroll) {
1640 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1642 *first_displayed_entry = entry;
1648 static const struct got_error *
1649 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1650 pthread_cond_t *need_commits)
1655 halfdelay(1); /* fast refresh while loading commits */
1657 while (*commits_needed > 0) {
1661 /* Wake the log thread. */
1662 errcode = pthread_cond_signal(need_commits);
1664 return got_error_set_errno(errcode,
1665 "pthread_cond_signal");
1666 errcode = pthread_mutex_unlock(&tog_mutex);
1668 return got_error_set_errno(errcode,
1669 "pthread_mutex_unlock");
1671 errcode = pthread_mutex_lock(&tog_mutex);
1673 return got_error_set_errno(errcode,
1674 "pthread_mutex_lock");
1676 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1678 * Thread is not done yet; lose a key press
1679 * and let the user retry... this way the GUI
1680 * remains interactive while logging deep paths
1681 * with few commits in history.
1690 static const struct got_error *
1691 scroll_down(struct tog_view *view,
1692 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1693 struct commit_queue_entry **last_displayed_entry,
1694 struct commit_queue *commits, int *log_complete, int *commits_needed,
1695 pthread_cond_t *need_commits)
1697 const struct got_error *err = NULL;
1698 struct commit_queue_entry *pentry;
1701 if (*last_displayed_entry == NULL)
1704 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1705 if (pentry == NULL && !*log_complete) {
1707 * Ask the log thread for required amount of commits
1708 * plus some amount of pre-fetching.
1710 (*commits_needed) += maxscroll + 20;
1711 err = trigger_log_thread(0, commits_needed, log_complete,
1718 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1722 *last_displayed_entry = pentry;
1724 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1727 *first_displayed_entry = pentry;
1728 } while (++nscrolled < maxscroll);
1733 static const struct got_error *
1734 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1735 struct got_commit_object *commit, struct got_object_id *commit_id,
1736 struct tog_view *log_view, struct got_reflist_head *refs,
1737 struct got_repository *repo)
1739 const struct got_error *err;
1740 struct got_object_qid *parent_id;
1741 struct tog_view *diff_view;
1743 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1744 if (diff_view == NULL)
1745 return got_error_from_errno("view_open");
1747 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1748 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1749 commit_id, log_view, refs, repo);
1751 *new_view = diff_view;
1755 static const struct got_error *
1756 tree_view_visit_subtree(struct got_tree_object *subtree,
1757 struct tog_tree_view_state *s)
1759 struct tog_parent_tree *parent;
1761 parent = calloc(1, sizeof(*parent));
1763 return got_error_from_errno("calloc");
1765 parent->tree = s->tree;
1766 parent->first_displayed_entry = s->first_displayed_entry;
1767 parent->selected_entry = s->selected_entry;
1768 parent->selected = s->selected;
1769 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1771 s->entries = got_object_tree_get_entries(s->tree);
1773 s->first_displayed_entry = NULL;
1778 static const struct got_error *
1779 browse_commit_tree(struct tog_view **new_view, int begin_x,
1780 struct commit_queue_entry *entry, const char *path,
1781 struct got_reflist_head *refs, struct got_repository *repo)
1783 const struct got_error *err = NULL;
1784 struct got_tree_object *tree;
1785 struct got_tree_entry *te;
1786 struct tog_tree_view_state *s;
1787 struct tog_view *tree_view;
1788 char *slash, *subpath = NULL;
1791 err = got_object_open_as_tree(&tree, repo,
1792 got_object_commit_get_tree_id(entry->commit));
1796 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1797 if (tree_view == NULL)
1798 return got_error_from_errno("view_open");
1800 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1802 got_object_tree_close(tree);
1805 s = &tree_view->state.tree;
1807 *new_view = tree_view;
1809 /* Walk the path and open corresponding tree objects. */
1814 struct got_object_id *tree_id;
1816 /* Ensure the correct subtree entry is selected. */
1817 slash = strchr(p, '/');
1819 slash = strchr(p, '\0');
1820 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1821 if (strncmp(p, te->name, slash - p) == 0) {
1822 s->selected_entry = te;
1827 if (s->selected_entry == NULL) {
1828 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1831 if (s->tree != s->root)
1832 s->selected++; /* skip '..' */
1834 if (!S_ISDIR(s->selected_entry->mode)) {
1835 /* Jump to this file's entry. */
1836 s->first_displayed_entry = s->selected_entry;
1841 slash = strchr(p, '/');
1843 subpath = strndup(path, slash - path);
1845 subpath = strdup(path);
1846 if (subpath == NULL) {
1847 err = got_error_from_errno("strdup");
1851 err = got_object_id_by_path(&tree_id, repo, entry->id,
1856 err = got_object_open_as_tree(&tree, repo, tree_id);
1861 err = tree_view_visit_subtree(tree, s);
1863 got_object_tree_close(tree);
1878 log_thread(void *arg)
1880 const struct got_error *err = NULL;
1882 struct tog_log_thread_args *a = arg;
1885 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1890 while (!done && !err && !tog_sigpipe_received) {
1891 err = queue_commits(a->graph, a->commits, 1, a->repo,
1892 a->in_repo_path, a->searching, a->search_next_done,
1895 if (err->code != GOT_ERR_ITER_COMPLETED)
1899 } else if (a->commits_needed > 0)
1900 a->commits_needed--;
1902 errcode = pthread_mutex_lock(&tog_mutex);
1904 err = got_error_set_errno(errcode,
1905 "pthread_mutex_lock");
1907 } else if (*a->quit)
1909 else if (*a->first_displayed_entry == NULL) {
1910 *a->first_displayed_entry =
1911 TAILQ_FIRST(&a->commits->head);
1912 *a->selected_entry = *a->first_displayed_entry;
1916 a->commits_needed = 0;
1917 else if (a->commits_needed == 0) {
1918 errcode = pthread_cond_wait(&a->need_commits,
1921 err = got_error_set_errno(errcode,
1922 "pthread_cond_wait");
1925 errcode = pthread_mutex_unlock(&tog_mutex);
1926 if (errcode && err == NULL)
1927 err = got_error_set_errno(errcode,
1928 "pthread_mutex_unlock");
1930 a->log_complete = 1;
1934 static const struct got_error *
1935 stop_log_thread(struct tog_log_view_state *s)
1937 const struct got_error *err = NULL;
1942 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1944 return got_error_set_errno(errcode,
1945 "pthread_cond_signal");
1946 errcode = pthread_mutex_unlock(&tog_mutex);
1948 return got_error_set_errno(errcode,
1949 "pthread_mutex_unlock");
1950 errcode = pthread_join(s->thread, (void **)&err);
1952 return got_error_set_errno(errcode, "pthread_join");
1953 errcode = pthread_mutex_lock(&tog_mutex);
1955 return got_error_set_errno(errcode,
1956 "pthread_mutex_lock");
1960 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1961 if (errcode && err == NULL)
1962 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1964 if (s->thread_args.repo) {
1965 got_repo_close(s->thread_args.repo);
1966 s->thread_args.repo = NULL;
1969 if (s->thread_args.graph) {
1970 got_commit_graph_close(s->thread_args.graph);
1971 s->thread_args.graph = NULL;
1977 static const struct got_error *
1978 close_log_view(struct tog_view *view)
1980 const struct got_error *err = NULL;
1981 struct tog_log_view_state *s = &view->state.log;
1983 err = stop_log_thread(s);
1984 free_commits(&s->commits);
1985 free(s->in_repo_path);
1986 s->in_repo_path = NULL;
1992 static const struct got_error *
1993 search_start_log_view(struct tog_view *view)
1995 struct tog_log_view_state *s = &view->state.log;
1997 s->matched_entry = NULL;
1998 s->search_entry = NULL;
2002 static const struct got_error *
2003 search_next_log_view(struct tog_view *view)
2005 const struct got_error *err = NULL;
2006 struct tog_log_view_state *s = &view->state.log;
2007 struct commit_queue_entry *entry;
2009 if (!view->searching) {
2010 view->search_next_done = 1;
2014 if (s->search_entry) {
2016 errcode = pthread_mutex_unlock(&tog_mutex);
2018 return got_error_set_errno(errcode,
2019 "pthread_mutex_unlock");
2020 ch = wgetch(view->window);
2021 errcode = pthread_mutex_lock(&tog_mutex);
2023 return got_error_set_errno(errcode,
2024 "pthread_mutex_lock");
2025 if (ch == KEY_BACKSPACE) {
2026 view->search_next_done = 1;
2029 if (view->searching == TOG_SEARCH_FORWARD)
2030 entry = TAILQ_NEXT(s->search_entry, entry);
2032 entry = TAILQ_PREV(s->search_entry,
2033 commit_queue_head, entry);
2034 } else if (s->matched_entry) {
2035 if (view->searching == TOG_SEARCH_FORWARD)
2036 entry = TAILQ_NEXT(s->selected_entry, entry);
2038 entry = TAILQ_PREV(s->selected_entry,
2039 commit_queue_head, entry);
2041 if (view->searching == TOG_SEARCH_FORWARD)
2042 entry = TAILQ_FIRST(&s->commits.head);
2044 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2050 if (entry == NULL) {
2051 if (s->thread_args.log_complete ||
2052 view->searching == TOG_SEARCH_BACKWARD) {
2053 view->search_next_done = 1;
2057 * Poke the log thread for more commits and return,
2058 * allowing the main loop to make progress. Search
2059 * will resume at s->search_entry once we come back.
2061 s->thread_args.commits_needed++;
2062 return trigger_log_thread(1,
2063 &s->thread_args.commits_needed,
2064 &s->thread_args.log_complete,
2065 &s->thread_args.need_commits);
2068 err = match_commit(&have_match, entry->id, entry->commit,
2073 view->search_next_done = 1;
2074 s->matched_entry = entry;
2078 s->search_entry = entry;
2079 if (view->searching == TOG_SEARCH_FORWARD)
2080 entry = TAILQ_NEXT(entry, entry);
2082 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2085 if (s->matched_entry) {
2086 int cur = s->selected_entry->idx;
2087 while (cur < s->matched_entry->idx) {
2088 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2093 while (cur > s->matched_entry->idx) {
2094 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2101 s->search_entry = NULL;
2106 static const struct got_error *
2107 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2108 struct got_reflist_head *refs, struct got_repository *repo,
2109 const char *head_ref_name, const char *path, int check_disk)
2111 const struct got_error *err = NULL;
2112 struct tog_log_view_state *s = &view->state.log;
2113 struct got_repository *thread_repo = NULL;
2114 struct got_commit_graph *thread_graph = NULL;
2117 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2121 /* The commit queue only contains commits being displayed. */
2122 TAILQ_INIT(&s->commits.head);
2123 s->commits.ncommits = 0;
2127 s->head_ref_name = head_ref_name;
2128 s->start_id = got_object_id_dup(start_id);
2129 if (s->start_id == NULL) {
2130 err = got_error_from_errno("got_object_id_dup");
2134 SIMPLEQ_INIT(&s->colors);
2135 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2136 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2137 get_color_value("TOG_COLOR_COMMIT"));
2140 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2141 get_color_value("TOG_COLOR_AUTHOR"));
2143 free_colors(&s->colors);
2146 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2147 get_color_value("TOG_COLOR_DATE"));
2149 free_colors(&s->colors);
2154 view->show = show_log_view;
2155 view->input = input_log_view;
2156 view->close = close_log_view;
2157 view->search_start = search_start_log_view;
2158 view->search_next = search_next_log_view;
2160 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2163 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
2168 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2170 err = got_error_set_errno(errcode, "pthread_cond_init");
2174 s->thread_args.commits_needed = view->nlines;
2175 s->thread_args.graph = thread_graph;
2176 s->thread_args.commits = &s->commits;
2177 s->thread_args.in_repo_path = s->in_repo_path;
2178 s->thread_args.start_id = s->start_id;
2179 s->thread_args.repo = thread_repo;
2180 s->thread_args.log_complete = 0;
2181 s->thread_args.quit = &s->quit;
2182 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2183 s->thread_args.selected_entry = &s->selected_entry;
2184 s->thread_args.searching = &view->searching;
2185 s->thread_args.search_next_done = &view->search_next_done;
2186 s->thread_args.regex = &view->regex;
2189 close_log_view(view);
2193 static const struct got_error *
2194 show_log_view(struct tog_view *view)
2196 struct tog_log_view_state *s = &view->state.log;
2198 if (s->thread == NULL) {
2199 int errcode = pthread_create(&s->thread, NULL, log_thread,
2202 return got_error_set_errno(errcode, "pthread_create");
2205 return draw_commits(view, &s->last_displayed_entry,
2206 &s->selected_entry, s->first_displayed_entry,
2207 &s->commits, s->selected, view->nlines, s->refs,
2208 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2211 static const struct got_error *
2212 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2213 struct tog_view **focus_view, struct tog_view *view, int ch)
2215 const struct got_error *err = NULL;
2216 struct tog_log_view_state *s = &view->state.log;
2217 char *parent_path, *in_repo_path = NULL;
2218 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2220 struct got_object_id *start_id;
2230 if (s->first_displayed_entry == NULL)
2232 if (s->selected > 0)
2235 scroll_up(view, &s->first_displayed_entry, 1,
2240 if (s->first_displayed_entry == NULL)
2242 if (TAILQ_FIRST(&s->commits.head) ==
2243 s->first_displayed_entry) {
2247 scroll_up(view, &s->first_displayed_entry,
2248 view->nlines, &s->commits);
2254 if (s->first_displayed_entry == NULL)
2256 if (s->selected < MIN(view->nlines - 2,
2257 s->commits.ncommits - 1)) {
2261 err = scroll_down(view, &s->first_displayed_entry, 1,
2262 &s->last_displayed_entry, &s->commits,
2263 &s->thread_args.log_complete,
2264 &s->thread_args.commits_needed,
2265 &s->thread_args.need_commits);
2269 struct commit_queue_entry *first;
2270 first = s->first_displayed_entry;
2273 err = scroll_down(view, &s->first_displayed_entry,
2274 view->nlines, &s->last_displayed_entry,
2275 &s->commits, &s->thread_args.log_complete,
2276 &s->thread_args.commits_needed,
2277 &s->thread_args.need_commits);
2280 if (first == s->first_displayed_entry &&
2281 s->selected < MIN(view->nlines - 2,
2282 s->commits.ncommits - 1)) {
2283 /* can't scroll further down */
2284 s->selected = MIN(view->nlines - 2,
2285 s->commits.ncommits - 1);
2291 if (s->selected > view->nlines - 2)
2292 s->selected = view->nlines - 2;
2293 if (s->selected > s->commits.ncommits - 1)
2294 s->selected = s->commits.ncommits - 1;
2299 if (s->selected_entry == NULL)
2301 if (view_is_parent_view(view))
2302 begin_x = view_split_begin_x(view->begin_x);
2303 err = open_diff_view_for_commit(&diff_view, begin_x,
2304 s->selected_entry->commit, s->selected_entry->id,
2305 view, s->refs, s->repo);
2308 if (view_is_parent_view(view)) {
2309 err = view_close_child(view);
2312 err = view_set_child(view, diff_view);
2314 view_close(diff_view);
2317 *focus_view = diff_view;
2318 view->child_focussed = 1;
2320 *new_view = diff_view;
2323 if (s->selected_entry == NULL)
2325 if (view_is_parent_view(view))
2326 begin_x = view_split_begin_x(view->begin_x);
2327 err = browse_commit_tree(&tree_view, begin_x,
2328 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2331 if (view_is_parent_view(view)) {
2332 err = view_close_child(view);
2335 err = view_set_child(view, tree_view);
2337 view_close(tree_view);
2340 *focus_view = tree_view;
2341 view->child_focussed = 1;
2343 *new_view = tree_view;
2346 if (strcmp(s->in_repo_path, "/") == 0)
2348 parent_path = dirname(s->in_repo_path);
2349 if (parent_path && strcmp(parent_path, ".") != 0) {
2350 err = stop_log_thread(s);
2353 lv = view_open(view->nlines, view->ncols,
2354 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2356 return got_error_from_errno(
2358 err = open_log_view(lv, s->start_id, s->refs,
2359 s->repo, s->head_ref_name, parent_path, 0);
2362 if (view_is_parent_view(view))
2365 view_set_child(view->parent, lv);
2372 err = stop_log_thread(s);
2375 lv = view_open(view->nlines, view->ncols,
2376 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2378 return got_error_from_errno("view_open");
2379 err = get_head_commit_id(&start_id, s->head_ref_name ?
2380 s->head_ref_name : GOT_REF_HEAD, s->repo);
2385 in_repo_path = strdup(s->in_repo_path);
2386 if (in_repo_path == NULL) {
2389 return got_error_from_errno("strdup");
2391 got_ref_list_free(s->refs);
2392 err = got_ref_list(s->refs, s->repo, NULL,
2393 got_ref_cmp_by_name, NULL);
2399 err = open_log_view(lv, start_id, s->refs, s->repo,
2400 s->head_ref_name, in_repo_path, 0);
2416 static const struct got_error *
2417 apply_unveil(const char *repo_path, const char *worktree_path)
2419 const struct got_error *error;
2422 if (unveil("gmon.out", "rwc") != 0)
2423 return got_error_from_errno2("unveil", "gmon.out");
2425 if (repo_path && unveil(repo_path, "r") != 0)
2426 return got_error_from_errno2("unveil", repo_path);
2428 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2429 return got_error_from_errno2("unveil", worktree_path);
2431 if (unveil("/tmp", "rwc") != 0)
2432 return got_error_from_errno2("unveil", "/tmp");
2434 error = got_privsep_unveil_exec_helpers();
2438 if (unveil(NULL, NULL) != 0)
2439 return got_error_from_errno("unveil");
2449 halfdelay(1); /* Do fast refresh while initial view is loading. */
2452 intrflush(stdscr, FALSE);
2453 keypad(stdscr, TRUE);
2455 if (getenv("TOG_COLORS") != NULL) {
2457 use_default_colors();
2459 signal(SIGWINCH, tog_sigwinch);
2460 signal(SIGPIPE, tog_sigpipe);
2463 static const struct got_error *
2464 cmd_log(int argc, char *argv[])
2466 const struct got_error *error;
2467 struct got_repository *repo = NULL;
2468 struct got_worktree *worktree = NULL;
2469 struct got_reflist_head refs;
2470 struct got_object_id *start_id = NULL;
2471 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2472 char *start_commit = NULL, *head_ref_name = NULL;
2474 struct tog_view *view;
2476 SIMPLEQ_INIT(&refs);
2479 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2484 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2487 start_commit = optarg;
2490 repo_path = realpath(optarg, NULL);
2491 if (repo_path == NULL)
2492 return got_error_from_errno2("realpath",
2504 cwd = getcwd(NULL, 0);
2506 error = got_error_from_errno("getcwd");
2509 error = got_worktree_open(&worktree, cwd);
2510 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2517 error = got_error_from_errno("strdup");
2520 } else if (argc == 1) {
2522 error = got_worktree_resolve_path(&path, worktree,
2527 path = strdup(argv[0]);
2529 error = got_error_from_errno("strdup");
2536 if (repo_path == NULL) {
2539 got_worktree_get_repo_path(worktree));
2541 repo_path = strdup(cwd);
2543 if (repo_path == NULL) {
2544 error = got_error_from_errno("strdup");
2550 error = got_repo_open(&repo, repo_path, NULL);
2554 error = apply_unveil(got_repo_get_path(repo),
2555 worktree ? got_worktree_get_root_path(worktree) : NULL);
2559 if (start_commit == NULL)
2560 error = get_head_commit_id(&start_id, worktree ?
2561 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2564 error = get_head_commit_id(&start_id, start_commit, repo);
2566 if (error->code != GOT_ERR_NOT_REF)
2568 error = got_repo_match_object_id_prefix(&start_id,
2569 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2575 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2579 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2581 error = got_error_from_errno("view_open");
2585 head_ref_name = strdup(
2586 got_worktree_get_head_ref_name(worktree));
2587 if (head_ref_name == NULL) {
2588 error = got_error_from_errno("strdup");
2592 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2597 /* Release work tree lock. */
2598 got_worktree_close(worktree);
2601 error = view_loop(view);
2607 free(head_ref_name);
2609 got_repo_close(repo);
2611 got_worktree_close(worktree);
2612 got_ref_list_free(&refs);
2620 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2626 parse_next_line(FILE *f, size_t *len)
2631 const char delim[3] = { '\0', '\0', '\0'};
2633 line = fparseln(f, &linelen, &lineno, delim, 0);
2640 match_line(const char *line, regex_t *regex)
2642 regmatch_t regmatch;
2644 return regexec(regex, line, 1, ®match, 0) == 0;
2648 match_color(struct tog_colors *colors, const char *line)
2650 struct tog_color *tc = NULL;
2652 SIMPLEQ_FOREACH(tc, colors, entry) {
2653 if (match_line(line, &tc->regex))
2660 static const struct got_error *
2661 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2662 int *last_displayed_line, int *eof, int max_lines, char *header,
2663 struct tog_colors *colors)
2665 const struct got_error *err;
2666 int nlines = 0, nprinted = 0;
2668 struct tog_color *tc;
2674 werase(view->window);
2677 err = format_line(&wline, &width, header, view->ncols, 0);
2682 if (view_needs_focus_indication(view))
2683 wstandout(view->window);
2684 waddwstr(view->window, wline);
2685 if (view_needs_focus_indication(view))
2686 wstandend(view->window);
2687 if (width <= view->ncols - 1)
2688 waddch(view->window, '\n');
2696 while (nprinted < max_lines) {
2697 line = parse_next_line(f, &len);
2702 if (++nlines < *first_displayed_line) {
2707 err = format_line(&wline, &width, line, view->ncols, 0);
2713 tc = match_color(colors, line);
2715 wattr_on(view->window,
2716 COLOR_PAIR(tc->colorpair), NULL);
2717 waddwstr(view->window, wline);
2719 wattr_off(view->window,
2720 COLOR_PAIR(tc->colorpair), NULL);
2721 if (width <= view->ncols - 1)
2722 waddch(view->window, '\n');
2723 if (++nprinted == 1)
2724 *first_displayed_line = nlines;
2729 *last_displayed_line = nlines;
2734 while (nprinted < view->nlines) {
2735 waddch(view->window, '\n');
2739 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2744 wstandout(view->window);
2745 waddwstr(view->window, wline);
2746 wstandend(view->window);
2753 get_datestr(time_t *time, char *datebuf)
2755 struct tm mytm, *tm;
2758 tm = gmtime_r(time, &mytm);
2761 s = asctime_r(tm, datebuf);
2764 p = strchr(s, '\n');
2770 static const struct got_error *
2771 write_commit_info(struct got_object_id *commit_id,
2772 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2774 const struct got_error *err = NULL;
2775 char datebuf[26], *datestr;
2776 struct got_commit_object *commit;
2777 char *id_str = NULL, *logmsg = NULL;
2778 time_t committer_time;
2779 const char *author, *committer;
2780 char *refs_str = NULL;
2783 err = build_refs_str(&refs_str, refs, commit_id, repo);
2788 err = got_object_open_as_commit(&commit, repo, commit_id);
2792 err = got_object_id_str(&id_str, commit_id);
2794 err = got_error_from_errno("got_object_id_str");
2798 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2799 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2800 err = got_error_from_errno("fprintf");
2803 if (fprintf(outfile, "from: %s\n",
2804 got_object_commit_get_author(commit)) < 0) {
2805 err = got_error_from_errno("fprintf");
2808 committer_time = got_object_commit_get_committer_time(commit);
2809 datestr = get_datestr(&committer_time, datebuf);
2810 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2811 err = got_error_from_errno("fprintf");
2814 author = got_object_commit_get_author(commit);
2815 committer = got_object_commit_get_committer(commit);
2816 if (strcmp(author, committer) != 0 &&
2817 fprintf(outfile, "via: %s\n", committer) < 0) {
2818 err = got_error_from_errno("fprintf");
2821 err = got_object_commit_get_logmsg(&logmsg, commit);
2824 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2825 err = got_error_from_errno("fprintf");
2832 got_object_commit_close(commit);
2836 static const struct got_error *
2837 create_diff(struct tog_diff_view_state *s)
2839 const struct got_error *err = NULL;
2845 err = got_error_from_errno("got_opentemp");
2848 if (s->f && fclose(s->f) != 0) {
2849 err = got_error_from_errno("fclose");
2855 err = got_object_get_type(&obj_type, s->repo, s->id1);
2857 err = got_object_get_type(&obj_type, s->repo, s->id2);
2862 case GOT_OBJ_TYPE_BLOB:
2863 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2864 s->diff_context, 0, s->repo, f);
2866 case GOT_OBJ_TYPE_TREE:
2867 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2868 s->diff_context, 0, s->repo, f);
2870 case GOT_OBJ_TYPE_COMMIT: {
2871 const struct got_object_id_queue *parent_ids;
2872 struct got_object_qid *pid;
2873 struct got_commit_object *commit2;
2875 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2878 /* Show commit info if we're diffing to a parent/root commit. */
2880 write_commit_info(s->id2, s->refs, s->repo, f);
2882 parent_ids = got_object_commit_get_parent_ids(commit2);
2883 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2884 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2885 write_commit_info(s->id2, s->refs,
2891 got_object_commit_close(commit2);
2893 err = got_diff_objects_as_commits(s->id1, s->id2,
2894 s->diff_context, 0, s->repo, f);
2898 err = got_error(GOT_ERR_OBJ_TYPE);
2902 if (f && fflush(f) != 0 && err == NULL)
2903 err = got_error_from_errno("fflush");
2908 diff_view_indicate_progress(struct tog_view *view)
2910 mvwaddstr(view->window, 0, 0, "diffing...");
2915 static const struct got_error *
2916 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2917 struct got_object_id *id2, struct tog_view *log_view,
2918 struct got_reflist_head *refs, struct got_repository *repo)
2920 const struct got_error *err;
2922 if (id1 != NULL && id2 != NULL) {
2924 err = got_object_get_type(&type1, repo, id1);
2927 err = got_object_get_type(&type2, repo, id2);
2932 return got_error(GOT_ERR_OBJ_TYPE);
2936 view->state.diff.id1 = got_object_id_dup(id1);
2937 if (view->state.diff.id1 == NULL)
2938 return got_error_from_errno("got_object_id_dup");
2940 view->state.diff.id1 = NULL;
2942 view->state.diff.id2 = got_object_id_dup(id2);
2943 if (view->state.diff.id2 == NULL) {
2944 free(view->state.diff.id1);
2945 view->state.diff.id1 = NULL;
2946 return got_error_from_errno("got_object_id_dup");
2948 view->state.diff.f = NULL;
2949 view->state.diff.first_displayed_line = 1;
2950 view->state.diff.last_displayed_line = view->nlines;
2951 view->state.diff.diff_context = 3;
2952 view->state.diff.log_view = log_view;
2953 view->state.diff.repo = repo;
2954 view->state.diff.refs = refs;
2955 SIMPLEQ_INIT(&view->state.diff.colors);
2957 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2958 err = add_color(&view->state.diff.colors,
2959 "^-", TOG_COLOR_DIFF_MINUS,
2960 get_color_value("TOG_COLOR_DIFF_MINUS"));
2963 err = add_color(&view->state.diff.colors, "^\\+",
2964 TOG_COLOR_DIFF_PLUS,
2965 get_color_value("TOG_COLOR_DIFF_PLUS"));
2967 free_colors(&view->state.diff.colors);
2970 err = add_color(&view->state.diff.colors,
2971 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
2972 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
2974 free_colors(&view->state.diff.colors);
2978 err = add_color(&view->state.diff.colors,
2979 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
2980 get_color_value("TOG_COLOR_DIFF_META"));
2982 free_colors(&view->state.diff.colors);
2986 err = add_color(&view->state.diff.colors,
2987 "^(from|via): ", TOG_COLOR_AUTHOR,
2988 get_color_value("TOG_COLOR_AUTHOR"));
2990 free_colors(&view->state.diff.colors);
2994 err = add_color(&view->state.diff.colors,
2995 "^date: ", TOG_COLOR_DATE,
2996 get_color_value("TOG_COLOR_DATE"));
2998 free_colors(&view->state.diff.colors);
3003 if (log_view && view_is_splitscreen(view))
3004 show_log_view(log_view); /* draw vborder */
3005 diff_view_indicate_progress(view);
3007 err = create_diff(&view->state.diff);
3009 free(view->state.diff.id1);
3010 view->state.diff.id1 = NULL;
3011 free(view->state.diff.id2);
3012 view->state.diff.id2 = NULL;
3016 view->show = show_diff_view;
3017 view->input = input_diff_view;
3018 view->close = close_diff_view;
3023 static const struct got_error *
3024 close_diff_view(struct tog_view *view)
3026 const struct got_error *err = NULL;
3028 free(view->state.diff.id1);
3029 view->state.diff.id1 = NULL;
3030 free(view->state.diff.id2);
3031 view->state.diff.id2 = NULL;
3032 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
3033 err = got_error_from_errno("fclose");
3034 free_colors(&view->state.diff.colors);
3038 static const struct got_error *
3039 show_diff_view(struct tog_view *view)
3041 const struct got_error *err;
3042 struct tog_diff_view_state *s = &view->state.diff;
3043 char *id_str1 = NULL, *id_str2, *header;
3046 err = got_object_id_str(&id_str1, s->id1);
3050 err = got_object_id_str(&id_str2, s->id2);
3054 if (asprintf(&header, "diff %s %s",
3055 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3056 err = got_error_from_errno("asprintf");
3064 return draw_file(view, s->f, &s->first_displayed_line,
3065 &s->last_displayed_line, &s->eof, view->nlines,
3066 header, &s->colors);
3069 static const struct got_error *
3070 set_selected_commit(struct tog_diff_view_state *s,
3071 struct commit_queue_entry *entry)
3073 const struct got_error *err;
3074 const struct got_object_id_queue *parent_ids;
3075 struct got_commit_object *selected_commit;
3076 struct got_object_qid *pid;
3079 s->id2 = got_object_id_dup(entry->id);
3081 return got_error_from_errno("got_object_id_dup");
3083 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3086 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3088 pid = SIMPLEQ_FIRST(parent_ids);
3089 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3090 got_object_commit_close(selected_commit);
3094 static const struct got_error *
3095 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3096 struct tog_view **focus_view, struct tog_view *view, int ch)
3098 const struct got_error *err = NULL;
3099 struct tog_diff_view_state *s = &view->state.diff;
3100 struct tog_log_view_state *ls;
3101 struct commit_queue_entry *entry;
3107 if (s->first_displayed_line > 1)
3108 s->first_displayed_line--;
3112 if (s->first_displayed_line == 1)
3115 while (i++ < view->nlines - 1 &&
3116 s->first_displayed_line > 1)
3117 s->first_displayed_line--;
3122 s->first_displayed_line++;
3130 while (!s->eof && i++ < view->nlines - 1) {
3132 line = parse_next_line(s->f, NULL);
3133 s->first_displayed_line++;
3139 if (s->diff_context > 0) {
3141 diff_view_indicate_progress(view);
3142 err = create_diff(s);
3146 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3148 diff_view_indicate_progress(view);
3149 err = create_diff(s);
3154 if (s->log_view == NULL)
3156 ls = &s->log_view->state.log;
3157 entry = TAILQ_PREV(ls->selected_entry,
3158 commit_queue_head, entry);
3162 err = input_log_view(NULL, NULL, NULL, s->log_view,
3167 err = set_selected_commit(s, entry);
3171 s->first_displayed_line = 1;
3172 s->last_displayed_line = view->nlines;
3174 diff_view_indicate_progress(view);
3175 err = create_diff(s);
3179 if (s->log_view == NULL)
3181 ls = &s->log_view->state.log;
3183 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3184 ls->thread_args.commits_needed++;
3186 /* Display "loading..." in log view. */
3187 show_log_view(s->log_view);
3191 err = trigger_log_thread(1 /* load_all */,
3192 &ls->thread_args.commits_needed,
3193 &ls->thread_args.log_complete,
3194 &ls->thread_args.need_commits);
3198 err = input_log_view(NULL, NULL, NULL, s->log_view,
3203 entry = TAILQ_NEXT(ls->selected_entry, entry);
3207 err = set_selected_commit(s, entry);
3211 s->first_displayed_line = 1;
3212 s->last_displayed_line = view->nlines;
3214 diff_view_indicate_progress(view);
3215 err = create_diff(s);
3224 static const struct got_error *
3225 cmd_diff(int argc, char *argv[])
3227 const struct got_error *error = NULL;
3228 struct got_repository *repo = NULL;
3229 struct got_reflist_head refs;
3230 struct got_object_id *id1 = NULL, *id2 = NULL;
3231 char *repo_path = NULL;
3232 char *id_str1 = NULL, *id_str2 = NULL;
3234 struct tog_view *view;
3236 SIMPLEQ_INIT(&refs);
3239 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3244 while ((ch = getopt(argc, argv, "")) != -1) {
3256 usage_diff(); /* TODO show local worktree changes */
3257 } else if (argc == 2) {
3258 repo_path = getcwd(NULL, 0);
3259 if (repo_path == NULL)
3260 return got_error_from_errno("getcwd");
3263 } else if (argc == 3) {
3264 repo_path = realpath(argv[0], NULL);
3265 if (repo_path == NULL)
3266 return got_error_from_errno2("realpath", argv[0]);
3274 error = got_repo_open(&repo, repo_path, NULL);
3278 error = apply_unveil(got_repo_get_path(repo), NULL);
3282 error = got_repo_match_object_id_prefix(&id1, id_str1,
3283 GOT_OBJ_TYPE_ANY, repo);
3287 error = got_repo_match_object_id_prefix(&id2, id_str2,
3288 GOT_OBJ_TYPE_ANY, repo);
3292 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3296 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3298 error = got_error_from_errno("view_open");
3301 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3304 error = view_loop(view);
3308 got_repo_close(repo);
3309 got_ref_list_free(&refs);
3317 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3322 struct tog_blame_line {
3324 struct got_object_id *id;
3327 static const struct got_error *
3328 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3329 const char *path, struct tog_blame_line *lines, int nlines,
3330 int blame_complete, int selected_line, int *first_displayed_line,
3331 int *last_displayed_line, int *eof, int max_lines,
3332 struct tog_colors *colors)
3334 const struct got_error *err;
3335 int lineno = 0, nprinted = 0;
3340 struct tog_blame_line *blame_line;
3341 struct got_object_id *prev_id = NULL;
3343 struct tog_color *tc;
3345 err = got_object_id_str(&id_str, id);
3350 werase(view->window);
3352 if (asprintf(&line, "commit %s", id_str) == -1) {
3353 err = got_error_from_errno("asprintf");
3358 err = format_line(&wline, &width, line, view->ncols, 0);
3363 if (view_needs_focus_indication(view))
3364 wstandout(view->window);
3365 tc = get_color(colors, TOG_COLOR_COMMIT);
3367 wattr_on(view->window,
3368 COLOR_PAIR(tc->colorpair), NULL);
3369 waddwstr(view->window, wline);
3371 wattr_off(view->window,
3372 COLOR_PAIR(tc->colorpair), NULL);
3373 if (view_needs_focus_indication(view))
3374 wstandend(view->window);
3377 if (width < view->ncols - 1)
3378 waddch(view->window, '\n');
3380 if (asprintf(&line, "[%d/%d] %s%s",
3381 *first_displayed_line - 1 + selected_line, nlines,
3382 blame_complete ? "" : "annotating... ", path) == -1) {
3384 return got_error_from_errno("asprintf");
3387 err = format_line(&wline, &width, line, view->ncols, 0);
3392 waddwstr(view->window, wline);
3395 if (width < view->ncols - 1)
3396 waddch(view->window, '\n');
3399 while (nprinted < max_lines - 2) {
3400 line = parse_next_line(f, &len);
3405 if (++lineno < *first_displayed_line) {
3410 if (view->ncols <= 9) {
3412 wline = wcsdup(L"");
3414 err = got_error_from_errno("wcsdup");
3416 err = format_line(&wline, &width, line,
3417 view->ncols - 9, 9);
3425 if (view->focussed && nprinted == selected_line - 1)
3426 wstandout(view->window);
3429 blame_line = &lines[lineno - 1];
3430 if (blame_line->annotated && prev_id &&
3431 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3433 nprinted == selected_line - 1)) {
3434 waddstr(view->window, " ");
3435 } else if (blame_line->annotated) {
3437 err = got_object_id_str(&id_str, blame_line->id);
3443 tc = get_color(colors, TOG_COLOR_COMMIT);
3445 wattr_on(view->window,
3446 COLOR_PAIR(tc->colorpair), NULL);
3447 wprintw(view->window, "%.8s", id_str);
3449 wattr_off(view->window,
3450 COLOR_PAIR(tc->colorpair), NULL);
3452 prev_id = blame_line->id;
3454 waddstr(view->window, "........");
3458 waddstr(view->window, "........");
3462 if (view->focussed && nprinted == selected_line - 1)
3463 wstandend(view->window);
3464 waddstr(view->window, " ");
3466 waddwstr(view->window, wline);
3467 if (width <= view->ncols - 1)
3468 waddch(view->window, '\n');
3469 if (++nprinted == 1)
3470 *first_displayed_line = lineno;
3475 *last_displayed_line = lineno;
3482 static const struct got_error *
3483 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3485 const struct got_error *err = NULL;
3486 struct tog_blame_cb_args *a = arg;
3487 struct tog_blame_line *line;
3490 if (nlines != a->nlines ||
3491 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3492 return got_error(GOT_ERR_RANGE);
3494 errcode = pthread_mutex_lock(&tog_mutex);
3496 return got_error_set_errno(errcode, "pthread_mutex_lock");
3498 if (*a->quit) { /* user has quit the blame view */
3499 err = got_error(GOT_ERR_ITER_COMPLETED);
3504 goto done; /* no change in this commit */
3506 line = &a->lines[lineno - 1];
3507 if (line->annotated)
3510 line->id = got_object_id_dup(id);
3511 if (line->id == NULL) {
3512 err = got_error_from_errno("got_object_id_dup");
3515 line->annotated = 1;
3517 errcode = pthread_mutex_unlock(&tog_mutex);
3519 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3524 blame_thread(void *arg)
3526 const struct got_error *err;
3527 struct tog_blame_thread_args *ta = arg;
3528 struct tog_blame_cb_args *a = ta->cb_args;
3531 err = got_blame(ta->path, a->commit_id, ta->repo,
3532 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3533 if (err && err->code == GOT_ERR_CANCELLED)
3536 errcode = pthread_mutex_lock(&tog_mutex);
3538 return (void *)got_error_set_errno(errcode,
3539 "pthread_mutex_lock");
3541 got_repo_close(ta->repo);
3545 errcode = pthread_mutex_unlock(&tog_mutex);
3546 if (errcode && err == NULL)
3547 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3552 static struct got_object_id *
3553 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3554 int first_displayed_line, int selected_line)
3556 struct tog_blame_line *line;
3561 line = &lines[first_displayed_line - 1 + selected_line - 1];
3562 if (!line->annotated)
3568 static const struct got_error *
3569 stop_blame(struct tog_blame *blame)
3571 const struct got_error *err = NULL;
3574 if (blame->thread) {
3576 errcode = pthread_mutex_unlock(&tog_mutex);
3578 return got_error_set_errno(errcode,
3579 "pthread_mutex_unlock");
3580 errcode = pthread_join(blame->thread, (void **)&err);
3582 return got_error_set_errno(errcode, "pthread_join");
3583 errcode = pthread_mutex_lock(&tog_mutex);
3585 return got_error_set_errno(errcode,
3586 "pthread_mutex_lock");
3587 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3589 blame->thread = NULL;
3591 if (blame->thread_args.repo) {
3592 got_repo_close(blame->thread_args.repo);
3593 blame->thread_args.repo = NULL;
3596 if (fclose(blame->f) != 0 && err == NULL)
3597 err = got_error_from_errno("fclose");
3601 for (i = 0; i < blame->nlines; i++)
3602 free(blame->lines[i].id);
3604 blame->lines = NULL;
3606 free(blame->cb_args.commit_id);
3607 blame->cb_args.commit_id = NULL;
3612 static const struct got_error *
3613 cancel_blame_view(void *arg)
3615 const struct got_error *err = NULL;
3619 errcode = pthread_mutex_lock(&tog_mutex);
3621 return got_error_set_errno(errcode,
3622 "pthread_mutex_unlock");
3625 err = got_error(GOT_ERR_CANCELLED);
3627 errcode = pthread_mutex_unlock(&tog_mutex);
3629 return got_error_set_errno(errcode,
3630 "pthread_mutex_lock");
3635 static const struct got_error *
3636 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3637 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3638 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3639 struct got_repository *repo)
3641 const struct got_error *err = NULL;
3642 struct got_blob_object *blob = NULL;
3643 struct got_repository *thread_repo = NULL;
3644 struct got_object_id *obj_id = NULL;
3647 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3651 return got_error(GOT_ERR_NO_OBJ);
3653 err = got_object_get_type(&obj_type, repo, obj_id);
3657 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3658 err = got_error(GOT_ERR_OBJ_TYPE);
3662 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3665 blame->f = got_opentemp();
3666 if (blame->f == NULL) {
3667 err = got_error_from_errno("got_opentemp");
3670 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3671 &blame->line_offsets, blame->f, blob);
3672 if (err || blame->nlines == 0)
3675 /* Don't include \n at EOF in the blame line count. */
3676 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3679 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3680 if (blame->lines == NULL) {
3681 err = got_error_from_errno("calloc");
3685 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3689 blame->cb_args.view = view;
3690 blame->cb_args.lines = blame->lines;
3691 blame->cb_args.nlines = blame->nlines;
3692 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3693 if (blame->cb_args.commit_id == NULL) {
3694 err = got_error_from_errno("got_object_id_dup");
3697 blame->cb_args.quit = done;
3699 blame->thread_args.path = path;
3700 blame->thread_args.repo = thread_repo;
3701 blame->thread_args.cb_args = &blame->cb_args;
3702 blame->thread_args.complete = blame_complete;
3703 blame->thread_args.cancel_cb = cancel_blame_view;
3704 blame->thread_args.cancel_arg = done;
3705 *blame_complete = 0;
3709 got_object_blob_close(blob);
3716 static const struct got_error *
3717 open_blame_view(struct tog_view *view, char *path,
3718 struct got_object_id *commit_id, struct got_reflist_head *refs,
3719 struct got_repository *repo)
3721 const struct got_error *err = NULL;
3722 struct tog_blame_view_state *s = &view->state.blame;
3724 SIMPLEQ_INIT(&s->blamed_commits);
3726 s->path = strdup(path);
3727 if (s->path == NULL)
3728 return got_error_from_errno("strdup");
3730 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3736 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3737 s->first_displayed_line = 1;
3738 s->last_displayed_line = view->nlines;
3739 s->selected_line = 1;
3740 s->blame_complete = 0;
3743 s->commit_id = commit_id;
3744 memset(&s->blame, 0, sizeof(s->blame));
3746 SIMPLEQ_INIT(&s->colors);
3747 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3748 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3749 get_color_value("TOG_COLOR_COMMIT"));
3754 view->show = show_blame_view;
3755 view->input = input_blame_view;
3756 view->close = close_blame_view;
3757 view->search_start = search_start_blame_view;
3758 view->search_next = search_next_blame_view;
3760 return run_blame(&s->blame, view, &s->blame_complete,
3761 &s->first_displayed_line, &s->last_displayed_line,
3762 &s->selected_line, &s->done, &s->eof, s->path,
3763 s->blamed_commit->id, s->repo);
3766 static const struct got_error *
3767 close_blame_view(struct tog_view *view)
3769 const struct got_error *err = NULL;
3770 struct tog_blame_view_state *s = &view->state.blame;
3772 if (s->blame.thread)
3773 err = stop_blame(&s->blame);
3775 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3776 struct got_object_qid *blamed_commit;
3777 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3778 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3779 got_object_qid_free(blamed_commit);
3783 free_colors(&s->colors);
3788 static const struct got_error *
3789 search_start_blame_view(struct tog_view *view)
3791 struct tog_blame_view_state *s = &view->state.blame;
3793 s->matched_line = 0;
3797 static const struct got_error *
3798 search_next_blame_view(struct tog_view *view)
3800 struct tog_blame_view_state *s = &view->state.blame;
3803 if (!view->searching) {
3804 view->search_next_done = 1;
3808 if (s->matched_line) {
3809 if (view->searching == TOG_SEARCH_FORWARD)
3810 lineno = s->matched_line + 1;
3812 lineno = s->matched_line - 1;
3814 if (view->searching == TOG_SEARCH_FORWARD)
3817 lineno = s->blame.nlines;
3825 if (lineno <= 0 || lineno > s->blame.nlines) {
3826 if (s->matched_line == 0) {
3827 view->search_next_done = 1;
3832 if (view->searching == TOG_SEARCH_FORWARD)
3835 lineno = s->blame.nlines;
3838 offset = s->blame.line_offsets[lineno - 1];
3839 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3841 return got_error_from_errno("fseeko");
3844 line = parse_next_line(s->blame.f, &len);
3845 if (line && match_line(line, &view->regex)) {
3846 view->search_next_done = 1;
3847 s->matched_line = lineno;
3852 if (view->searching == TOG_SEARCH_FORWARD)
3858 if (s->matched_line) {
3859 s->first_displayed_line = s->matched_line;
3860 s->selected_line = 1;
3866 static const struct got_error *
3867 show_blame_view(struct tog_view *view)
3869 const struct got_error *err = NULL;
3870 struct tog_blame_view_state *s = &view->state.blame;
3873 if (s->blame.thread == NULL) {
3874 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3875 &s->blame.thread_args);
3877 return got_error_set_errno(errcode, "pthread_create");
3879 halfdelay(1); /* fast refresh while annotating */
3882 if (s->blame_complete)
3883 halfdelay(10); /* disable fast refresh */
3885 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3886 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3887 s->selected_line, &s->first_displayed_line,
3888 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
3894 static const struct got_error *
3895 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3896 struct tog_view **focus_view, struct tog_view *view, int ch)
3898 const struct got_error *err = NULL, *thread_err = NULL;
3899 struct tog_view *diff_view;
3900 struct tog_blame_view_state *s = &view->state.blame;
3909 if (s->selected_line > 1)
3911 else if (s->selected_line == 1 &&
3912 s->first_displayed_line > 1)
3913 s->first_displayed_line--;
3916 if (s->first_displayed_line == 1) {
3917 s->selected_line = 1;
3920 if (s->first_displayed_line > view->nlines - 2)
3921 s->first_displayed_line -=
3924 s->first_displayed_line = 1;
3928 if (s->selected_line < view->nlines - 2 &&
3929 s->first_displayed_line +
3930 s->selected_line <= s->blame.nlines)
3932 else if (s->last_displayed_line <
3934 s->first_displayed_line++;
3938 struct got_object_id *id = NULL;
3939 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3940 s->first_displayed_line, s->selected_line);
3944 struct got_commit_object *commit;
3945 struct got_object_qid *pid;
3946 struct got_object_id *blob_id = NULL;
3948 err = got_object_open_as_commit(&commit,
3952 pid = SIMPLEQ_FIRST(
3953 got_object_commit_get_parent_ids(commit));
3955 got_object_commit_close(commit);
3958 /* Check if path history ends here. */
3959 err = got_object_id_by_path(&blob_id, s->repo,
3962 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3964 got_object_commit_close(commit);
3967 err = got_object_get_type(&obj_type, s->repo,
3970 /* Can't blame non-blob type objects. */
3971 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3972 got_object_commit_close(commit);
3975 err = got_object_qid_alloc(&s->blamed_commit,
3977 got_object_commit_close(commit);
3979 if (got_object_id_cmp(id,
3980 s->blamed_commit->id) == 0)
3982 err = got_object_qid_alloc(&s->blamed_commit,
3988 thread_err = stop_blame(&s->blame);
3992 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3993 s->blamed_commit, entry);
3994 err = run_blame(&s->blame, view, &s->blame_complete,
3995 &s->first_displayed_line, &s->last_displayed_line,
3996 &s->selected_line, &s->done, &s->eof,
3997 s->path, s->blamed_commit->id, s->repo);
4003 struct got_object_qid *first;
4004 first = SIMPLEQ_FIRST(&s->blamed_commits);
4005 if (!got_object_id_cmp(first->id, s->commit_id))
4008 thread_err = stop_blame(&s->blame);
4012 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4013 got_object_qid_free(s->blamed_commit);
4015 SIMPLEQ_FIRST(&s->blamed_commits);
4016 err = run_blame(&s->blame, view, &s->blame_complete,
4017 &s->first_displayed_line, &s->last_displayed_line,
4018 &s->selected_line, &s->done, &s->eof, s->path,
4019 s->blamed_commit->id, s->repo);
4026 struct got_object_id *id = NULL;
4027 struct got_object_qid *pid;
4028 struct got_commit_object *commit = NULL;
4029 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4030 s->first_displayed_line, s->selected_line);
4033 err = got_object_open_as_commit(&commit, s->repo, id);
4036 pid = SIMPLEQ_FIRST(
4037 got_object_commit_get_parent_ids(commit));
4038 if (view_is_parent_view(view))
4039 begin_x = view_split_begin_x(view->begin_x);
4040 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4041 if (diff_view == NULL) {
4042 got_object_commit_close(commit);
4043 err = got_error_from_errno("view_open");
4046 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4047 id, NULL, s->refs, s->repo);
4048 got_object_commit_close(commit);
4050 view_close(diff_view);
4053 if (view_is_parent_view(view)) {
4054 err = view_close_child(view);
4057 err = view_set_child(view, diff_view);
4059 view_close(diff_view);
4062 *focus_view = diff_view;
4063 view->child_focussed = 1;
4065 *new_view = diff_view;
4072 if (s->last_displayed_line >= s->blame.nlines &&
4073 s->selected_line >= MIN(s->blame.nlines,
4074 view->nlines - 2)) {
4077 if (s->last_displayed_line >= s->blame.nlines &&
4078 s->selected_line < view->nlines - 2) {
4079 s->selected_line = MIN(s->blame.nlines,
4083 if (s->last_displayed_line + view->nlines - 2
4085 s->first_displayed_line +=
4088 s->first_displayed_line =
4093 if (s->selected_line > view->nlines - 2) {
4094 s->selected_line = MIN(s->blame.nlines,
4101 return thread_err ? thread_err : err;
4104 static const struct got_error *
4105 cmd_blame(int argc, char *argv[])
4107 const struct got_error *error;
4108 struct got_repository *repo = NULL;
4109 struct got_reflist_head refs;
4110 struct got_worktree *worktree = NULL;
4111 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4112 struct got_object_id *commit_id = NULL;
4113 char *commit_id_str = NULL;
4115 struct tog_view *view;
4117 SIMPLEQ_INIT(&refs);
4120 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4125 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4128 commit_id_str = optarg;
4131 repo_path = realpath(optarg, NULL);
4132 if (repo_path == NULL)
4133 return got_error_from_errno2("realpath",
4150 cwd = getcwd(NULL, 0);
4152 error = got_error_from_errno("getcwd");
4155 if (repo_path == NULL) {
4156 error = got_worktree_open(&worktree, cwd);
4157 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4163 strdup(got_worktree_get_repo_path(worktree));
4164 if (repo_path == NULL)
4165 error = got_error_from_errno("strdup");
4169 repo_path = strdup(cwd);
4170 if (repo_path == NULL) {
4171 error = got_error_from_errno("strdup");
4179 error = got_repo_open(&repo, repo_path, NULL);
4183 error = apply_unveil(got_repo_get_path(repo), NULL);
4188 const char *prefix = got_worktree_get_path_prefix(worktree);
4189 char *p, *worktree_subdir = cwd +
4190 strlen(got_worktree_get_root_path(worktree));
4191 if (asprintf(&p, "%s%s%s%s%s",
4192 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4193 worktree_subdir, worktree_subdir[0] ? "/" : "",
4195 error = got_error_from_errno("asprintf");
4198 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4201 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4206 if (commit_id_str == NULL) {
4207 struct got_reference *head_ref;
4208 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4211 error = got_ref_resolve(&commit_id, repo, head_ref);
4212 got_ref_close(head_ref);
4214 error = get_head_commit_id(&commit_id, commit_id_str, repo);
4216 if (error->code != GOT_ERR_NOT_REF)
4218 error = got_repo_match_object_id_prefix(&commit_id,
4219 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
4225 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4229 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4231 error = got_error_from_errno("view_open");
4234 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4238 /* Release work tree lock. */
4239 got_worktree_close(worktree);
4242 error = view_loop(view);
4248 got_worktree_close(worktree);
4250 got_repo_close(repo);
4251 got_ref_list_free(&refs);
4255 static const struct got_error *
4256 draw_tree_entries(struct tog_view *view,
4257 struct got_tree_entry **first_displayed_entry,
4258 struct got_tree_entry **last_displayed_entry,
4259 struct got_tree_entry **selected_entry, int *ndisplayed,
4260 const char *label, int show_ids, const char *parent_path,
4261 const struct got_tree_entries *entries, int selected, int limit,
4262 int isroot, struct tog_colors *colors)
4264 const struct got_error *err = NULL;
4265 struct got_tree_entry *te;
4267 struct tog_color *tc;
4272 werase(view->window);
4277 err = format_line(&wline, &width, label, view->ncols, 0);
4280 if (view_needs_focus_indication(view))
4281 wstandout(view->window);
4282 tc = get_color(colors, TOG_COLOR_COMMIT);
4284 wattr_on(view->window,
4285 COLOR_PAIR(tc->colorpair), NULL);
4286 waddwstr(view->window, wline);
4288 wattr_off(view->window,
4289 COLOR_PAIR(tc->colorpair), NULL);
4290 if (view_needs_focus_indication(view))
4291 wstandend(view->window);
4294 if (width < view->ncols - 1)
4295 waddch(view->window, '\n');
4298 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4301 waddwstr(view->window, wline);
4304 if (width < view->ncols - 1)
4305 waddch(view->window, '\n');
4308 waddch(view->window, '\n');
4312 te = SIMPLEQ_FIRST(&entries->head);
4313 if (*first_displayed_entry == NULL) {
4314 if (selected == 0) {
4316 wstandout(view->window);
4317 *selected_entry = NULL;
4319 waddstr(view->window, " ..\n"); /* parent directory */
4320 if (selected == 0 && view->focussed)
4321 wstandend(view->window);
4328 while (te != *first_displayed_entry)
4329 te = SIMPLEQ_NEXT(te, entry);
4333 char *line = NULL, *id_str = NULL;
4334 const char *modestr = "";
4337 err = got_object_id_str(&id_str, te->id);
4339 return got_error_from_errno(
4340 "got_object_id_str");
4342 if (got_object_tree_entry_is_submodule(te))
4344 else if (S_ISLNK(te->mode))
4346 else if (S_ISDIR(te->mode))
4348 else if (te->mode & S_IXUSR)
4350 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4351 te->name, modestr) == -1) {
4353 return got_error_from_errno("asprintf");
4356 err = format_line(&wline, &width, line, view->ncols, 0);
4361 if (n == selected) {
4363 wstandout(view->window);
4364 *selected_entry = te;
4366 tc = match_color(colors, line);
4368 wattr_on(view->window,
4369 COLOR_PAIR(tc->colorpair), NULL);
4370 waddwstr(view->window, wline);
4372 wattr_off(view->window,
4373 COLOR_PAIR(tc->colorpair), NULL);
4374 if (width < view->ncols - 1)
4375 waddch(view->window, '\n');
4376 if (n == selected && view->focussed)
4377 wstandend(view->window);
4383 *last_displayed_entry = te;
4386 te = SIMPLEQ_NEXT(te, entry);
4393 tree_scroll_up(struct tog_view *view,
4394 struct got_tree_entry **first_displayed_entry, int maxscroll,
4395 const struct got_tree_entries *entries, int isroot)
4397 struct got_tree_entry *te, *prev;
4400 if (*first_displayed_entry == NULL)
4403 te = SIMPLEQ_FIRST(&entries->head);
4404 if (*first_displayed_entry == te) {
4406 *first_displayed_entry = NULL;
4410 /* XXX this is stupid... switch to TAILQ? */
4411 for (i = 0; i < maxscroll; i++) {
4412 while (te != *first_displayed_entry) {
4414 te = SIMPLEQ_NEXT(te, entry);
4416 *first_displayed_entry = prev;
4417 te = SIMPLEQ_FIRST(&entries->head);
4419 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
4420 *first_displayed_entry = NULL;
4424 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4425 struct got_tree_entry *last_displayed_entry,
4426 const struct got_tree_entries *entries)
4428 struct got_tree_entry *next, *last;
4431 if (*first_displayed_entry)
4432 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
4434 next = SIMPLEQ_FIRST(&entries->head);
4435 last = last_displayed_entry;
4436 while (next && last && n++ < maxscroll) {
4437 last = SIMPLEQ_NEXT(last, entry);
4439 *first_displayed_entry = next;
4440 next = SIMPLEQ_NEXT(next, entry);
4446 static const struct got_error *
4447 tree_entry_path(char **path, struct tog_parent_trees *parents,
4448 struct got_tree_entry *te)
4450 const struct got_error *err = NULL;
4451 struct tog_parent_tree *pt;
4452 size_t len = 2; /* for leading slash and NUL */
4454 TAILQ_FOREACH(pt, parents, entry)
4455 len += strlen(pt->selected_entry->name) + 1 /* slash */;
4457 len += strlen(te->name);
4459 *path = calloc(1, len);
4461 return got_error_from_errno("calloc");
4464 pt = TAILQ_LAST(parents, tog_parent_trees);
4466 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4467 err = got_error(GOT_ERR_NO_SPACE);
4470 if (strlcat(*path, "/", len) >= len) {
4471 err = got_error(GOT_ERR_NO_SPACE);
4474 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4477 if (strlcat(*path, te->name, len) >= len) {
4478 err = got_error(GOT_ERR_NO_SPACE);
4490 static const struct got_error *
4491 blame_tree_entry(struct tog_view **new_view, int begin_x,
4492 struct got_tree_entry *te, struct tog_parent_trees *parents,
4493 struct got_object_id *commit_id, struct got_reflist_head *refs,
4494 struct got_repository *repo)
4496 const struct got_error *err = NULL;
4498 struct tog_view *blame_view;
4502 err = tree_entry_path(&path, parents, te);
4506 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4507 if (blame_view == NULL) {
4508 err = got_error_from_errno("view_open");
4512 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4514 if (err->code == GOT_ERR_CANCELLED)
4516 view_close(blame_view);
4518 *new_view = blame_view;
4524 static const struct got_error *
4525 log_tree_entry(struct tog_view **new_view, int begin_x,
4526 struct got_tree_entry *te, struct tog_parent_trees *parents,
4527 struct got_object_id *commit_id, struct got_reflist_head *refs,
4528 struct got_repository *repo)
4530 struct tog_view *log_view;
4531 const struct got_error *err = NULL;
4536 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4537 if (log_view == NULL)
4538 return got_error_from_errno("view_open");
4540 err = tree_entry_path(&path, parents, te);
4544 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4546 view_close(log_view);
4548 *new_view = log_view;
4553 static const struct got_error *
4554 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4555 struct got_object_id *commit_id, struct got_reflist_head *refs,
4556 struct got_repository *repo)
4558 const struct got_error *err = NULL;
4559 char *commit_id_str = NULL;
4560 struct tog_tree_view_state *s = &view->state.tree;
4562 TAILQ_INIT(&s->parents);
4564 err = got_object_id_str(&commit_id_str, commit_id);
4568 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4569 err = got_error_from_errno("asprintf");
4573 s->root = s->tree = root;
4574 s->entries = got_object_tree_get_entries(root);
4575 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4576 s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4577 s->commit_id = got_object_id_dup(commit_id);
4578 if (s->commit_id == NULL) {
4579 err = got_error_from_errno("got_object_id_dup");
4585 SIMPLEQ_INIT(&s->colors);
4587 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4588 err = add_color(&s->colors, "\\$$",
4589 TOG_COLOR_TREE_SUBMODULE,
4590 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4593 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4594 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4596 free_colors(&s->colors);
4599 err = add_color(&s->colors, "/$",
4600 TOG_COLOR_TREE_DIRECTORY,
4601 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4603 free_colors(&s->colors);
4607 err = add_color(&s->colors, "\\*$",
4608 TOG_COLOR_TREE_EXECUTABLE,
4609 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4611 free_colors(&s->colors);
4615 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4616 get_color_value("TOG_COLOR_COMMIT"));
4618 free_colors(&s->colors);
4623 view->show = show_tree_view;
4624 view->input = input_tree_view;
4625 view->close = close_tree_view;
4626 view->search_start = search_start_tree_view;
4627 view->search_next = search_next_tree_view;
4629 free(commit_id_str);
4631 free(s->tree_label);
4632 s->tree_label = NULL;
4637 static const struct got_error *
4638 close_tree_view(struct tog_view *view)
4640 struct tog_tree_view_state *s = &view->state.tree;
4642 free_colors(&s->colors);
4643 free(s->tree_label);
4644 s->tree_label = NULL;
4646 s->commit_id = NULL;
4647 while (!TAILQ_EMPTY(&s->parents)) {
4648 struct tog_parent_tree *parent;
4649 parent = TAILQ_FIRST(&s->parents);
4650 TAILQ_REMOVE(&s->parents, parent, entry);
4654 if (s->tree != s->root)
4655 got_object_tree_close(s->tree);
4656 got_object_tree_close(s->root);
4661 static const struct got_error *
4662 search_start_tree_view(struct tog_view *view)
4664 struct tog_tree_view_state *s = &view->state.tree;
4666 s->matched_entry = NULL;
4671 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4673 regmatch_t regmatch;
4675 return regexec(regex, te->name, 1, ®match, 0) == 0;
4678 static const struct got_error *
4679 search_next_tree_view(struct tog_view *view)
4681 struct tog_tree_view_state *s = &view->state.tree;
4682 struct got_tree_entry *entry = NULL, *te;
4684 if (!view->searching) {
4685 view->search_next_done = 1;
4689 if (s->matched_entry) {
4690 if (view->searching == TOG_SEARCH_FORWARD) {
4691 if (s->selected_entry)
4692 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4694 entry = SIMPLEQ_FIRST(&s->entries->head);
4697 if (s->selected_entry == NULL) {
4698 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4701 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4703 if (SIMPLEQ_NEXT(te, entry) ==
4710 if (view->searching == TOG_SEARCH_FORWARD)
4711 entry = SIMPLEQ_FIRST(&s->entries->head);
4713 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4719 if (entry == NULL) {
4720 if (s->matched_entry == NULL) {
4721 view->search_next_done = 1;
4724 if (view->searching == TOG_SEARCH_FORWARD)
4725 entry = SIMPLEQ_FIRST(&s->entries->head);
4727 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4732 if (match_tree_entry(entry, &view->regex)) {
4733 view->search_next_done = 1;
4734 s->matched_entry = entry;
4738 if (view->searching == TOG_SEARCH_FORWARD)
4739 entry = SIMPLEQ_NEXT(entry, entry);
4741 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4744 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4745 if (SIMPLEQ_NEXT(te, entry) == entry) {
4754 if (s->matched_entry) {
4755 s->first_displayed_entry = s->matched_entry;
4762 static const struct got_error *
4763 show_tree_view(struct tog_view *view)
4765 const struct got_error *err = NULL;
4766 struct tog_tree_view_state *s = &view->state.tree;
4769 err = tree_entry_path(&parent_path, &s->parents, NULL);
4773 err = draw_tree_entries(view, &s->first_displayed_entry,
4774 &s->last_displayed_entry, &s->selected_entry,
4775 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4776 s->entries, s->selected, view->nlines, s->tree == s->root,
4784 static const struct got_error *
4785 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4786 struct tog_view **focus_view, struct tog_view *view, int ch)
4788 const struct got_error *err = NULL;
4789 struct tog_tree_view_state *s = &view->state.tree;
4790 struct tog_view *log_view;
4791 int begin_x = 0, nscrolled;
4795 s->show_ids = !s->show_ids;
4798 if (!s->selected_entry)
4800 if (view_is_parent_view(view))
4801 begin_x = view_split_begin_x(view->begin_x);
4802 err = log_tree_entry(&log_view, begin_x,
4803 s->selected_entry, &s->parents,
4804 s->commit_id, s->refs, s->repo);
4805 if (view_is_parent_view(view)) {
4806 err = view_close_child(view);
4809 err = view_set_child(view, log_view);
4811 view_close(log_view);
4814 *focus_view = log_view;
4815 view->child_focussed = 1;
4817 *new_view = log_view;
4821 if (s->selected > 0) {
4823 if (s->selected == 0)
4826 if (s->selected > 0)
4828 tree_scroll_up(view, &s->first_displayed_entry, 1,
4829 s->entries, s->tree == s->root);
4832 tree_scroll_up(view, &s->first_displayed_entry,
4833 MAX(0, view->nlines - 4 - s->selected), s->entries,
4834 s->tree == s->root);
4836 if (SIMPLEQ_FIRST(&s->entries->head) ==
4837 s->first_displayed_entry && s->tree != s->root)
4838 s->first_displayed_entry = NULL;
4842 if (s->selected < s->ndisplayed - 1) {
4846 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4847 /* can't scroll any further */
4849 tree_scroll_down(&s->first_displayed_entry, 1,
4850 s->last_displayed_entry, s->entries);
4853 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4855 /* can't scroll any further; move cursor down */
4856 if (s->selected < s->ndisplayed - 1)
4857 s->selected = s->ndisplayed - 1;
4860 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4861 view->nlines, s->last_displayed_entry, s->entries);
4862 if (nscrolled < view->nlines) {
4864 struct got_tree_entry *te;
4865 te = s->first_displayed_entry;
4868 te = SIMPLEQ_NEXT(te, entry);
4870 s->selected = ndisplayed - 1;
4876 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4877 struct tog_parent_tree *parent;
4878 /* user selected '..' */
4879 if (s->tree == s->root)
4881 parent = TAILQ_FIRST(&s->parents);
4882 TAILQ_REMOVE(&s->parents, parent,
4884 got_object_tree_close(s->tree);
4885 s->tree = parent->tree;
4887 got_object_tree_get_entries(s->tree);
4888 s->first_displayed_entry =
4889 parent->first_displayed_entry;
4891 parent->selected_entry;
4892 s->selected = parent->selected;
4894 } else if (S_ISDIR(s->selected_entry->mode)) {
4895 struct got_tree_object *subtree;
4896 err = got_object_open_as_tree(&subtree,
4897 s->repo, s->selected_entry->id);
4900 err = tree_view_visit_subtree(subtree, s);
4902 got_object_tree_close(subtree);
4905 } else if (S_ISREG(s->selected_entry->mode)) {
4906 struct tog_view *blame_view;
4907 int begin_x = view_is_parent_view(view) ?
4908 view_split_begin_x(view->begin_x) : 0;
4910 err = blame_tree_entry(&blame_view, begin_x,
4911 s->selected_entry, &s->parents,
4912 s->commit_id, s->refs, s->repo);
4915 if (view_is_parent_view(view)) {
4916 err = view_close_child(view);
4919 err = view_set_child(view, blame_view);
4921 view_close(blame_view);
4924 *focus_view = blame_view;
4925 view->child_focussed = 1;
4927 *new_view = blame_view;
4931 if (s->selected > view->nlines)
4932 s->selected = s->ndisplayed - 1;
4945 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4950 static const struct got_error *
4951 cmd_tree(int argc, char *argv[])
4953 const struct got_error *error;
4954 struct got_repository *repo = NULL;
4955 struct got_reflist_head refs;
4956 char *repo_path = NULL;
4957 struct got_object_id *commit_id = NULL;
4958 char *commit_id_arg = NULL;
4959 struct got_commit_object *commit = NULL;
4960 struct got_tree_object *tree = NULL;
4962 struct tog_view *view;
4964 SIMPLEQ_INIT(&refs);
4967 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4972 while ((ch = getopt(argc, argv, "c:")) != -1) {
4975 commit_id_arg = optarg;
4987 struct got_worktree *worktree;
4988 char *cwd = getcwd(NULL, 0);
4990 return got_error_from_errno("getcwd");
4991 error = got_worktree_open(&worktree, cwd);
4992 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4997 strdup(got_worktree_get_repo_path(worktree));
4998 got_worktree_close(worktree);
5001 if (repo_path == NULL) {
5002 error = got_error_from_errno("strdup");
5005 } else if (argc == 1) {
5006 repo_path = realpath(argv[0], NULL);
5007 if (repo_path == NULL)
5008 return got_error_from_errno2("realpath", argv[0]);
5014 error = got_repo_open(&repo, repo_path, NULL);
5018 error = apply_unveil(got_repo_get_path(repo), NULL);
5022 if (commit_id_arg == NULL)
5023 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
5025 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
5027 if (error->code != GOT_ERR_NOT_REF)
5029 error = got_repo_match_object_id_prefix(&commit_id,
5030 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
5036 error = got_object_open_as_commit(&commit, repo, commit_id);
5040 error = got_object_open_as_tree(&tree, repo,
5041 got_object_commit_get_tree_id(commit));
5045 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5049 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5051 error = got_error_from_errno("view_open");
5054 error = open_tree_view(view, tree, commit_id, &refs, repo);
5057 error = view_loop(view);
5062 got_object_commit_close(commit);
5064 got_object_tree_close(tree);
5066 got_repo_close(repo);
5067 got_ref_list_free(&refs);
5076 fprintf(stderr, "commands:");
5077 for (i = 0; i < nitems(tog_commands); i++) {
5078 struct tog_cmd *cmd = &tog_commands[i];
5079 fprintf(stderr, " %s", cmd->name);
5081 fputc('\n', stderr);
5087 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
5095 make_argv(const char *arg0, const char *arg1)
5098 int argc = (arg1 == NULL ? 1 : 2);
5100 argv = calloc(argc, sizeof(char *));
5103 argv[0] = strdup(arg0);
5104 if (argv[0] == NULL)
5107 argv[1] = strdup(arg1);
5108 if (argv[1] == NULL)
5116 main(int argc, char *argv[])
5118 const struct got_error *error = NULL;
5119 struct tog_cmd *cmd = NULL;
5120 int ch, hflag = 0, Vflag = 0;
5121 char **cmd_argv = NULL;
5123 setlocale(LC_CTYPE, "");
5125 while ((ch = getopt(argc, argv, "hV")) != -1) {
5145 got_version_print_str();
5152 /* Build an argument vector which runs a default command. */
5153 cmd = &tog_commands[0];
5154 cmd_argv = make_argv(cmd->name, NULL);
5159 /* Did the user specific a command? */
5160 for (i = 0; i < nitems(tog_commands); i++) {
5161 if (strncmp(tog_commands[i].name, argv[0],
5162 strlen(argv[0])) == 0) {
5163 cmd = &tog_commands[i];
5169 fprintf(stderr, "%s: unknown command '%s'\n",
5170 getprogname(), argv[0]);
5179 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5183 if (error && error->code != GOT_ERR_CANCELLED)
5184 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);