2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/ioctl.h>
22 #if defined(__FreeBSD__)
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
44 #include "got_compat.h"
46 #include "got_version.h"
47 #include "got_error.h"
48 #include "got_object.h"
49 #include "got_reference.h"
50 #include "got_repository.h"
52 #include "got_opentemp.h"
54 #include "got_cancel.h"
55 #include "got_commit_graph.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
59 #include "got_worktree.h"
61 //#define update_panels() (0)
62 //#define doupdate() (0)
65 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
69 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
73 #define CTRL(x) ((x) & 0x1f)
77 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
82 const struct got_error *(*cmd_main)(int, char *[]);
83 void (*cmd_usage)(void);
86 __dead static void usage(int, int);
87 __dead static void usage_log(void);
88 __dead static void usage_diff(void);
89 __dead static void usage_blame(void);
90 __dead static void usage_tree(void);
91 __dead static void usage_ref(void);
93 static const struct got_error* cmd_log(int, char *[]);
94 static const struct got_error* cmd_diff(int, char *[]);
95 static const struct got_error* cmd_blame(int, char *[]);
96 static const struct got_error* cmd_tree(int, char *[]);
97 static const struct got_error* cmd_ref(int, char *[]);
99 static struct tog_cmd tog_commands[] = {
100 { "log", cmd_log, usage_log },
101 { "diff", cmd_diff, usage_diff },
102 { "blame", cmd_blame, usage_blame },
103 { "tree", cmd_tree, usage_tree },
104 { "ref", cmd_ref, usage_ref },
115 #define TOG_EOF_STRING "(END)"
117 struct commit_queue_entry {
118 TAILQ_ENTRY(commit_queue_entry) entry;
119 struct got_object_id *id;
120 struct got_commit_object *commit;
123 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
124 struct commit_queue {
126 struct commit_queue_head head;
130 STAILQ_ENTRY(tog_color) entry;
134 STAILQ_HEAD(tog_colors, tog_color);
136 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
137 static struct got_reflist_object_id_map *tog_refs_idmap;
139 static const struct got_error *
140 tog_load_refs(struct got_repository *repo, int sort_by_date)
142 const struct got_error *err;
144 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
145 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
150 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
157 if (tog_refs_idmap) {
158 got_reflist_object_id_map_free(tog_refs_idmap);
159 tog_refs_idmap = NULL;
161 got_ref_list_free(&tog_refs);
164 static const struct got_error *
165 add_color(struct tog_colors *colors, const char *pattern,
166 int idx, short color)
168 const struct got_error *err = NULL;
169 struct tog_color *tc;
172 if (idx < 1 || idx > COLOR_PAIRS - 1)
175 init_pair(idx, color, -1);
177 tc = calloc(1, sizeof(*tc));
179 return got_error_from_errno("calloc");
180 regerr = regcomp(&tc->regex, pattern,
181 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
183 static char regerr_msg[512];
184 static char err_msg[512];
185 regerror(regerr, &tc->regex, regerr_msg,
187 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
189 err = got_error_msg(GOT_ERR_REGEX, err_msg);
194 STAILQ_INSERT_HEAD(colors, tc, entry);
199 free_colors(struct tog_colors *colors)
201 struct tog_color *tc;
203 while (!STAILQ_EMPTY(colors)) {
204 tc = STAILQ_FIRST(colors);
205 STAILQ_REMOVE_HEAD(colors, entry);
212 get_color(struct tog_colors *colors, int colorpair)
214 struct tog_color *tc = NULL;
216 STAILQ_FOREACH(tc, colors, entry) {
217 if (tc->colorpair == colorpair)
225 default_color_value(const char *envvar)
227 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
228 return COLOR_MAGENTA;
229 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
231 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
233 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
235 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
236 return COLOR_MAGENTA;
237 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
238 return COLOR_MAGENTA;
239 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
241 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
243 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
245 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
247 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
249 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
251 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
252 return COLOR_MAGENTA;
253 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
260 get_color_value(const char *envvar)
262 const char *val = getenv(envvar);
265 return default_color_value(envvar);
267 if (strcasecmp(val, "black") == 0)
269 if (strcasecmp(val, "red") == 0)
271 if (strcasecmp(val, "green") == 0)
273 if (strcasecmp(val, "yellow") == 0)
275 if (strcasecmp(val, "blue") == 0)
277 if (strcasecmp(val, "magenta") == 0)
278 return COLOR_MAGENTA;
279 if (strcasecmp(val, "cyan") == 0)
281 if (strcasecmp(val, "white") == 0)
283 if (strcasecmp(val, "default") == 0)
286 return default_color_value(envvar);
290 struct tog_diff_view_state {
291 struct got_object_id *id1, *id2;
292 const char *label1, *label2;
294 int first_displayed_line;
295 int last_displayed_line;
298 int ignore_whitespace;
300 struct got_repository *repo;
301 struct tog_colors colors;
307 /* passed from log view; may be NULL */
308 struct tog_view *log_view;
311 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
313 struct tog_log_thread_args {
314 pthread_cond_t need_commits;
315 pthread_cond_t commit_loaded;
318 struct got_commit_graph *graph;
319 struct commit_queue *commits;
320 const char *in_repo_path;
321 struct got_object_id *start_id;
322 struct got_repository *repo;
325 struct commit_queue_entry **first_displayed_entry;
326 struct commit_queue_entry **selected_entry;
328 int *search_next_done;
332 struct tog_log_view_state {
333 struct commit_queue commits;
334 struct commit_queue_entry *first_displayed_entry;
335 struct commit_queue_entry *last_displayed_entry;
336 struct commit_queue_entry *selected_entry;
341 struct got_repository *repo;
342 struct got_object_id *start_id;
345 struct tog_log_thread_args thread_args;
346 struct commit_queue_entry *matched_entry;
347 struct commit_queue_entry *search_entry;
348 struct tog_colors colors;
351 #define TOG_COLOR_DIFF_MINUS 1
352 #define TOG_COLOR_DIFF_PLUS 2
353 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
354 #define TOG_COLOR_DIFF_META 4
355 #define TOG_COLOR_TREE_SUBMODULE 5
356 #define TOG_COLOR_TREE_SYMLINK 6
357 #define TOG_COLOR_TREE_DIRECTORY 7
358 #define TOG_COLOR_TREE_EXECUTABLE 8
359 #define TOG_COLOR_COMMIT 9
360 #define TOG_COLOR_AUTHOR 10
361 #define TOG_COLOR_DATE 11
362 #define TOG_COLOR_REFS_HEADS 12
363 #define TOG_COLOR_REFS_TAGS 13
364 #define TOG_COLOR_REFS_REMOTES 14
366 struct tog_blame_cb_args {
367 struct tog_blame_line *lines; /* one per line */
370 struct tog_view *view;
371 struct got_object_id *commit_id;
375 struct tog_blame_thread_args {
377 struct got_repository *repo;
378 struct tog_blame_cb_args *cb_args;
380 got_cancel_cb cancel_cb;
387 struct tog_blame_line *lines;
391 struct tog_blame_thread_args thread_args;
392 struct tog_blame_cb_args cb_args;
396 struct tog_blame_view_state {
397 int first_displayed_line;
398 int last_displayed_line;
403 struct got_object_id_queue blamed_commits;
404 struct got_object_qid *blamed_commit;
406 struct got_repository *repo;
407 struct got_object_id *commit_id;
408 struct tog_blame blame;
410 struct tog_colors colors;
413 struct tog_parent_tree {
414 TAILQ_ENTRY(tog_parent_tree) entry;
415 struct got_tree_object *tree;
416 struct got_tree_entry *first_displayed_entry;
417 struct got_tree_entry *selected_entry;
421 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
423 struct tog_tree_view_state {
425 struct got_object_id *commit_id;/* commit which this tree belongs to */
426 struct got_tree_object *root; /* the commit's root tree entry */
427 struct got_tree_object *tree; /* currently displayed (sub-)tree */
428 struct got_tree_entry *first_displayed_entry;
429 struct got_tree_entry *last_displayed_entry;
430 struct got_tree_entry *selected_entry;
431 int ndisplayed, selected, show_ids;
432 struct tog_parent_trees parents; /* parent trees of current sub-tree */
434 struct got_repository *repo;
435 struct got_tree_entry *matched_entry;
436 struct tog_colors colors;
439 struct tog_reflist_entry {
440 TAILQ_ENTRY(tog_reflist_entry) entry;
441 struct got_reference *ref;
445 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
447 struct tog_ref_view_state {
448 struct tog_reflist_head refs;
449 struct tog_reflist_entry *first_displayed_entry;
450 struct tog_reflist_entry *last_displayed_entry;
451 struct tog_reflist_entry *selected_entry;
452 int nrefs, ndisplayed, selected, show_ids, sort_by_date;
453 struct got_repository *repo;
454 struct tog_reflist_entry *matched_entry;
455 struct tog_colors colors;
459 * We implement two types of views: parent views and child views.
461 * The 'Tab' key switches focus between a parent view and its child view.
462 * Child views are shown side-by-side to their parent view, provided
463 * there is enough screen estate.
465 * When a new view is opened from within a parent view, this new view
466 * becomes a child view of the parent view, replacing any existing child.
468 * When a new view is opened from within a child view, this new view
469 * becomes a parent view which will obscure the views below until the
470 * user quits the new parent view by typing 'q'.
472 * This list of views contains parent views only.
473 * Child views are only pointed to by their parent view.
475 TAILQ_HEAD(tog_view_list_head, tog_view);
478 TAILQ_ENTRY(tog_view) entry;
481 int nlines, ncols, begin_y, begin_x;
482 int lines, cols; /* copies of LINES and COLS */
483 int focussed; /* Only set on one parent or child view at a time. */
485 struct tog_view *parent;
486 struct tog_view *child;
489 * This flag is initially set on parent views when a new child view
490 * is created. It gets toggled when the 'Tab' key switches focus
491 * between parent and child.
492 * The flag indicates whether focus should be passed on to our child
493 * view if this parent view gets picked for focus after another parent
494 * view was closed. This prevents child views from losing focus in such
499 /* type-specific state */
500 enum tog_view_type type;
502 struct tog_diff_view_state diff;
503 struct tog_log_view_state log;
504 struct tog_blame_view_state blame;
505 struct tog_tree_view_state tree;
506 struct tog_ref_view_state ref;
509 const struct got_error *(*show)(struct tog_view *);
510 const struct got_error *(*input)(struct tog_view **,
511 struct tog_view *, int);
512 const struct got_error *(*close)(struct tog_view *);
514 const struct got_error *(*search_start)(struct tog_view *);
515 const struct got_error *(*search_next)(struct tog_view *);
518 #define TOG_SEARCH_FORWARD 1
519 #define TOG_SEARCH_BACKWARD 2
520 int search_next_done;
521 #define TOG_SEARCH_HAVE_MORE 1
522 #define TOG_SEARCH_NO_MORE 2
523 #define TOG_SEARCH_HAVE_NONE 3
528 static const struct got_error *open_diff_view(struct tog_view *,
529 struct got_object_id *, struct got_object_id *,
530 const char *, const char *, int, int, int, struct tog_view *,
531 struct got_repository *);
532 static const struct got_error *show_diff_view(struct tog_view *);
533 static const struct got_error *input_diff_view(struct tog_view **,
534 struct tog_view *, int);
535 static const struct got_error* close_diff_view(struct tog_view *);
536 static const struct got_error *search_start_diff_view(struct tog_view *);
537 static const struct got_error *search_next_diff_view(struct tog_view *);
539 static const struct got_error *open_log_view(struct tog_view *,
540 struct got_object_id *, struct got_repository *,
541 const char *, const char *, int);
542 static const struct got_error * show_log_view(struct tog_view *);
543 static const struct got_error *input_log_view(struct tog_view **,
544 struct tog_view *, int);
545 static const struct got_error *close_log_view(struct tog_view *);
546 static const struct got_error *search_start_log_view(struct tog_view *);
547 static const struct got_error *search_next_log_view(struct tog_view *);
549 static const struct got_error *open_blame_view(struct tog_view *, char *,
550 struct got_object_id *, struct got_repository *);
551 static const struct got_error *show_blame_view(struct tog_view *);
552 static const struct got_error *input_blame_view(struct tog_view **,
553 struct tog_view *, int);
554 static const struct got_error *close_blame_view(struct tog_view *);
555 static const struct got_error *search_start_blame_view(struct tog_view *);
556 static const struct got_error *search_next_blame_view(struct tog_view *);
558 static const struct got_error *open_tree_view(struct tog_view *,
559 struct got_object_id *, const char *, struct got_repository *);
560 static const struct got_error *show_tree_view(struct tog_view *);
561 static const struct got_error *input_tree_view(struct tog_view **,
562 struct tog_view *, int);
563 static const struct got_error *close_tree_view(struct tog_view *);
564 static const struct got_error *search_start_tree_view(struct tog_view *);
565 static const struct got_error *search_next_tree_view(struct tog_view *);
567 static const struct got_error *open_ref_view(struct tog_view *,
568 struct got_repository *);
569 static const struct got_error *show_ref_view(struct tog_view *);
570 static const struct got_error *input_ref_view(struct tog_view **,
571 struct tog_view *, int);
572 static const struct got_error *close_ref_view(struct tog_view *);
573 static const struct got_error *search_start_ref_view(struct tog_view *);
574 static const struct got_error *search_next_ref_view(struct tog_view *);
576 static volatile sig_atomic_t tog_sigwinch_received;
577 static volatile sig_atomic_t tog_sigpipe_received;
578 static volatile sig_atomic_t tog_sigcont_received;
581 tog_sigwinch(int signo)
583 tog_sigwinch_received = 1;
587 tog_sigpipe(int signo)
589 tog_sigpipe_received = 1;
593 tog_sigcont(int signo)
595 tog_sigcont_received = 1;
598 static const struct got_error *
599 view_close(struct tog_view *view)
601 const struct got_error *err = NULL;
604 view_close(view->child);
608 err = view->close(view);
610 del_panel(view->panel);
612 delwin(view->window);
617 static struct tog_view *
618 view_open(int nlines, int ncols, int begin_y, int begin_x,
619 enum tog_view_type type)
621 struct tog_view *view = calloc(1, sizeof(*view));
629 view->nlines = nlines ? nlines : LINES - begin_y;
630 view->ncols = ncols ? ncols : COLS - begin_x;
631 view->begin_y = begin_y;
632 view->begin_x = begin_x;
633 view->window = newwin(nlines, ncols, begin_y, begin_x);
634 if (view->window == NULL) {
638 view->panel = new_panel(view->window);
639 if (view->panel == NULL ||
640 set_panel_userptr(view->panel, view) != OK) {
645 keypad(view->window, TRUE);
650 view_split_begin_x(int begin_x)
652 if (begin_x > 0 || COLS < 120)
654 return (COLS - MAX(COLS / 2, 80));
657 static const struct got_error *view_resize(struct tog_view *);
659 static const struct got_error *
660 view_splitscreen(struct tog_view *view)
662 const struct got_error *err = NULL;
665 view->begin_x = view_split_begin_x(0);
666 view->nlines = LINES;
667 view->ncols = COLS - view->begin_x;
670 err = view_resize(view);
674 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
675 return got_error_from_errno("mvwin");
680 static const struct got_error *
681 view_fullscreen(struct tog_view *view)
683 const struct got_error *err = NULL;
687 view->nlines = LINES;
691 err = view_resize(view);
695 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
696 return got_error_from_errno("mvwin");
702 view_is_parent_view(struct tog_view *view)
704 return view->parent == NULL;
707 static const struct got_error *
708 view_resize(struct tog_view *view)
712 if (view->lines > LINES)
713 nlines = view->nlines - (view->lines - LINES);
715 nlines = view->nlines + (LINES - view->lines);
717 if (view->cols > COLS)
718 ncols = view->ncols - (view->cols - COLS);
720 ncols = view->ncols + (COLS - view->cols);
722 if (wresize(view->window, nlines, ncols) == ERR)
723 return got_error_from_errno("wresize");
724 if (replace_panel(view->panel, view->window) == ERR)
725 return got_error_from_errno("replace_panel");
726 wclear(view->window);
728 view->nlines = nlines;
734 view->child->begin_x = view_split_begin_x(view->begin_x);
735 if (view->child->begin_x == 0) {
736 view_fullscreen(view->child);
737 if (view->child->focussed)
738 show_panel(view->child->panel);
740 show_panel(view->panel);
742 view_splitscreen(view->child);
743 show_panel(view->child->panel);
750 static const struct got_error *
751 view_close_child(struct tog_view *view)
753 const struct got_error *err = NULL;
755 if (view->child == NULL)
758 err = view_close(view->child);
764 view_set_child(struct tog_view *view, struct tog_view *child)
767 child->parent = view;
771 view_is_splitscreen(struct tog_view *view)
773 return view->begin_x > 0;
782 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
783 cols = 80; /* Default */
789 resize_term(lines, cols);
792 static const struct got_error *
793 view_search_start(struct tog_view *view)
795 const struct got_error *err = NULL;
799 if (view->search_started) {
800 regfree(&view->regex);
802 memset(&view->regmatch, 0, sizeof(view->regmatch));
804 view->search_started = 0;
806 if (view->nlines < 1)
809 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
810 wclrtoeol(view->window);
814 ret = wgetnstr(view->window, pattern, sizeof(pattern));
820 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
821 err = view->search_start(view);
823 regfree(&view->regex);
826 view->search_started = 1;
827 view->searching = TOG_SEARCH_FORWARD;
828 view->search_next_done = 0;
829 view->search_next(view);
835 static const struct got_error *
836 view_input(struct tog_view **new, int *done, struct tog_view *view,
837 struct tog_view_list_head *views)
839 const struct got_error *err = NULL;
845 /* Clear "no matches" indicator. */
846 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
847 view->search_next_done == TOG_SEARCH_HAVE_NONE)
848 view->search_next_done = TOG_SEARCH_HAVE_MORE;
850 if (view->searching && !view->search_next_done) {
851 errcode = pthread_mutex_unlock(&tog_mutex);
853 return got_error_set_errno(errcode,
854 "pthread_mutex_unlock");
856 errcode = pthread_mutex_lock(&tog_mutex);
858 return got_error_set_errno(errcode,
859 "pthread_mutex_lock");
860 view->search_next(view);
864 nodelay(stdscr, FALSE);
865 /* Allow threads to make progress while we are waiting for input. */
866 errcode = pthread_mutex_unlock(&tog_mutex);
868 return got_error_set_errno(errcode, "pthread_mutex_unlock");
869 ch = wgetch(view->window);
870 errcode = pthread_mutex_lock(&tog_mutex);
872 return got_error_set_errno(errcode, "pthread_mutex_lock");
873 nodelay(stdscr, TRUE);
875 if (tog_sigwinch_received || tog_sigcont_received) {
877 tog_sigwinch_received = 0;
878 tog_sigcont_received = 0;
879 TAILQ_FOREACH(v, views, entry) {
880 err = view_resize(v);
883 err = v->input(new, v, KEY_RESIZE);
887 err = view_resize(v->child);
890 err = v->child->input(new, v->child,
902 view->child->focussed = 1;
903 view->focus_child = 1;
904 } else if (view->parent) {
906 view->parent->focussed = 1;
907 view->parent->focus_child = 0;
911 err = view->input(new, view, ch);
918 if (view_is_parent_view(view)) {
919 if (view->child == NULL)
921 if (view_is_splitscreen(view->child)) {
923 view->child->focussed = 1;
924 err = view_fullscreen(view->child);
926 err = view_splitscreen(view->child);
929 err = view->child->input(new, view->child,
932 if (view_is_splitscreen(view)) {
933 view->parent->focussed = 0;
935 err = view_fullscreen(view);
937 err = view_splitscreen(view);
941 err = view->input(new, view, KEY_RESIZE);
947 if (view->search_start)
948 view_search_start(view);
950 err = view->input(new, view, ch);
954 if (view->search_started && view->search_next) {
955 view->searching = (ch == 'n' ?
956 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
957 view->search_next_done = 0;
958 view->search_next(view);
960 err = view->input(new, view, ch);
963 err = view->input(new, view, ch);
971 view_vborder(struct tog_view *view)
974 const struct tog_view *view_above;
977 return view_vborder(view->parent);
979 panel = panel_above(view->panel);
983 view_above = panel_userptr(panel);
984 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
985 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
989 view_needs_focus_indication(struct tog_view *view)
991 if (view_is_parent_view(view)) {
992 if (view->child == NULL || view->child->focussed)
994 if (!view_is_splitscreen(view->child))
996 } else if (!view_is_splitscreen(view))
999 return view->focussed;
1002 static const struct got_error *
1003 view_loop(struct tog_view *view)
1005 const struct got_error *err = NULL;
1006 struct tog_view_list_head views;
1007 struct tog_view *new_view;
1008 int fast_refresh = 10;
1009 int done = 0, errcode;
1011 errcode = pthread_mutex_lock(&tog_mutex);
1013 return got_error_set_errno(errcode, "pthread_mutex_lock");
1016 TAILQ_INSERT_HEAD(&views, view, entry);
1019 err = view->show(view);
1024 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1025 /* Refresh fast during initialization, then become slower. */
1026 if (fast_refresh && fast_refresh-- == 0)
1027 halfdelay(10); /* switch to once per second */
1029 err = view_input(&new_view, &done, view, &views);
1033 struct tog_view *v, *prev = NULL;
1035 if (view_is_parent_view(view))
1036 prev = TAILQ_PREV(view, tog_view_list_head,
1038 else if (view->parent)
1039 prev = view->parent;
1042 view->parent->child = NULL;
1043 view->parent->focus_child = 0;
1045 TAILQ_REMOVE(&views, view, entry);
1047 err = view_close(view);
1052 TAILQ_FOREACH(v, &views, entry) {
1056 if (view == NULL && new_view == NULL) {
1057 /* No view has focus. Try to pick one. */
1060 else if (!TAILQ_EMPTY(&views)) {
1061 view = TAILQ_LAST(&views,
1062 tog_view_list_head);
1065 if (view->focus_child) {
1066 view->child->focussed = 1;
1074 struct tog_view *v, *t;
1075 /* Only allow one parent view per type. */
1076 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1077 if (v->type != new_view->type)
1079 TAILQ_REMOVE(&views, v, entry);
1080 err = view_close(v);
1085 TAILQ_INSERT_TAIL(&views, new_view, entry);
1089 if (view_is_parent_view(view)) {
1090 if (view->child && view->child->focussed)
1093 if (view->parent && view->parent->focussed)
1094 view = view->parent;
1096 show_panel(view->panel);
1097 if (view->child && view_is_splitscreen(view->child))
1098 show_panel(view->child->panel);
1099 if (view->parent && view_is_splitscreen(view)) {
1100 err = view->parent->show(view->parent);
1104 err = view->show(view);
1108 err = view->child->show(view->child);
1117 while (!TAILQ_EMPTY(&views)) {
1118 view = TAILQ_FIRST(&views);
1119 TAILQ_REMOVE(&views, view, entry);
1123 errcode = pthread_mutex_unlock(&tog_mutex);
1124 if (errcode && err == NULL)
1125 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1135 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1140 /* Create newly allocated wide-character string equivalent to a byte string. */
1141 static const struct got_error *
1142 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1145 const struct got_error *err = NULL;
1148 *wlen = mbstowcs(NULL, s, 0);
1149 if (*wlen == (size_t)-1) {
1151 if (errno != EILSEQ)
1152 return got_error_from_errno("mbstowcs");
1154 /* byte string invalid in current encoding; try to "fix" it */
1155 err = got_mbsavis(&vis, &vislen, s);
1158 *wlen = mbstowcs(NULL, vis, 0);
1159 if (*wlen == (size_t)-1) {
1160 err = got_error_from_errno("mbstowcs"); /* give up */
1165 *ws = calloc(*wlen + 1, sizeof(**ws));
1167 err = got_error_from_errno("calloc");
1171 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1172 err = got_error_from_errno("mbstowcs");
1183 /* Format a line for display, ensuring that it won't overflow a width limit. */
1184 static const struct got_error *
1185 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1188 const struct got_error *err = NULL;
1190 wchar_t *wline = NULL;
1197 err = mbs2ws(&wline, &wlen, line);
1201 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1202 wline[wlen - 1] = L'\0';
1205 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1206 wline[wlen - 1] = L'\0';
1212 int width = wcwidth(wline[i]);
1219 if (width == 1 || width == 2) {
1220 if (cols + width > wlimit)
1224 } else if (width == -1) {
1225 if (wline[i] == L'\t') {
1227 ((cols + col_tab_align) % TABSIZE);
1232 if (cols + width > wlimit)
1237 err = got_error_from_errno("wcwidth");
1252 static const struct got_error*
1253 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1254 struct got_object_id *id, struct got_repository *repo)
1256 static const struct got_error *err = NULL;
1257 struct got_reflist_entry *re;
1263 TAILQ_FOREACH(re, refs, entry) {
1264 struct got_tag_object *tag = NULL;
1265 struct got_object_id *ref_id;
1268 name = got_ref_get_name(re->ref);
1269 if (strcmp(name, GOT_REF_HEAD) == 0)
1271 if (strncmp(name, "refs/", 5) == 0)
1273 if (strncmp(name, "got/", 4) == 0)
1275 if (strncmp(name, "heads/", 6) == 0)
1277 if (strncmp(name, "remotes/", 8) == 0) {
1279 s = strstr(name, "/" GOT_REF_HEAD);
1280 if (s != NULL && s[strlen(s)] == '\0')
1283 err = got_ref_resolve(&ref_id, repo, re->ref);
1286 if (strncmp(name, "tags/", 5) == 0) {
1287 err = got_object_open_as_tag(&tag, repo, ref_id);
1289 if (err->code != GOT_ERR_OBJ_TYPE) {
1293 /* Ref points at something other than a tag. */
1298 cmp = got_object_id_cmp(tag ?
1299 got_object_tag_get_object_id(tag) : ref_id, id);
1302 got_object_tag_close(tag);
1306 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1307 s ? ", " : "", name) == -1) {
1308 err = got_error_from_errno("asprintf");
1319 static const struct got_error *
1320 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1325 smallerthan = strchr(author, '<');
1326 if (smallerthan && smallerthan[1] != '\0')
1327 author = smallerthan + 1;
1328 author[strcspn(author, "@>")] = '\0';
1329 return format_line(wauthor, author_width, author, limit, col_tab_align);
1332 static const struct got_error *
1333 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1334 struct got_object_id *id, const size_t date_display_cols,
1335 int author_display_cols)
1337 struct tog_log_view_state *s = &view->state.log;
1338 const struct got_error *err = NULL;
1339 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1340 char *logmsg0 = NULL, *logmsg = NULL;
1341 char *author = NULL;
1342 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1343 int author_width, logmsg_width;
1344 char *newline, *line = NULL;
1346 const int avail = view->ncols;
1348 time_t committer_time;
1349 struct tog_color *tc;
1351 committer_time = got_object_commit_get_committer_time(commit);
1352 if (gmtime_r(&committer_time, &tm) == NULL)
1353 return got_error_from_errno("gmtime_r");
1354 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1355 return got_error(GOT_ERR_NO_SPACE);
1357 if (avail <= date_display_cols)
1358 limit = MIN(sizeof(datebuf) - 1, avail);
1360 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1361 tc = get_color(&s->colors, TOG_COLOR_DATE);
1363 wattr_on(view->window,
1364 COLOR_PAIR(tc->colorpair), NULL);
1365 waddnstr(view->window, datebuf, limit);
1367 wattr_off(view->window,
1368 COLOR_PAIR(tc->colorpair), NULL);
1375 err = got_object_id_str(&id_str, id);
1378 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1380 wattr_on(view->window,
1381 COLOR_PAIR(tc->colorpair), NULL);
1382 wprintw(view->window, "%.8s ", id_str);
1384 wattr_off(view->window,
1385 COLOR_PAIR(tc->colorpair), NULL);
1392 author = strdup(got_object_commit_get_author(commit));
1393 if (author == NULL) {
1394 err = got_error_from_errno("strdup");
1397 err = format_author(&wauthor, &author_width, author, avail - col, col);
1400 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1402 wattr_on(view->window,
1403 COLOR_PAIR(tc->colorpair), NULL);
1404 waddwstr(view->window, wauthor);
1406 wattr_off(view->window,
1407 COLOR_PAIR(tc->colorpair), NULL);
1408 col += author_width;
1409 while (col < avail && author_width < author_display_cols + 2) {
1410 waddch(view->window, ' ');
1417 err = got_object_commit_get_logmsg(&logmsg0, commit);
1421 while (*logmsg == '\n')
1423 newline = strchr(logmsg, '\n');
1426 limit = avail - col;
1427 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1430 waddwstr(view->window, wlogmsg);
1431 col += logmsg_width;
1432 while (col < avail) {
1433 waddch(view->window, ' ');
1445 static struct commit_queue_entry *
1446 alloc_commit_queue_entry(struct got_commit_object *commit,
1447 struct got_object_id *id)
1449 struct commit_queue_entry *entry;
1451 entry = calloc(1, sizeof(*entry));
1456 entry->commit = commit;
1461 pop_commit(struct commit_queue *commits)
1463 struct commit_queue_entry *entry;
1465 entry = TAILQ_FIRST(&commits->head);
1466 TAILQ_REMOVE(&commits->head, entry, entry);
1467 got_object_commit_close(entry->commit);
1468 commits->ncommits--;
1469 /* Don't free entry->id! It is owned by the commit graph. */
1474 free_commits(struct commit_queue *commits)
1476 while (!TAILQ_EMPTY(&commits->head))
1477 pop_commit(commits);
1480 static const struct got_error *
1481 match_commit(int *have_match, struct got_object_id *id,
1482 struct got_commit_object *commit, regex_t *regex)
1484 const struct got_error *err = NULL;
1485 regmatch_t regmatch;
1486 char *id_str = NULL, *logmsg = NULL;
1490 err = got_object_id_str(&id_str, id);
1494 err = got_object_commit_get_logmsg(&logmsg, commit);
1498 if (regexec(regex, got_object_commit_get_author(commit), 1,
1499 ®match, 0) == 0 ||
1500 regexec(regex, got_object_commit_get_committer(commit), 1,
1501 ®match, 0) == 0 ||
1502 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1503 regexec(regex, logmsg, 1, ®match, 0) == 0)
1511 static const struct got_error *
1512 queue_commits(struct tog_log_thread_args *a)
1514 const struct got_error *err = NULL;
1517 * We keep all commits open throughout the lifetime of the log
1518 * view in order to avoid having to re-fetch commits from disk
1519 * while updating the display.
1522 struct got_object_id *id;
1523 struct got_commit_object *commit;
1524 struct commit_queue_entry *entry;
1527 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1529 if (err || id == NULL)
1532 err = got_object_open_as_commit(&commit, a->repo, id);
1535 entry = alloc_commit_queue_entry(commit, id);
1536 if (entry == NULL) {
1537 err = got_error_from_errno("alloc_commit_queue_entry");
1541 errcode = pthread_mutex_lock(&tog_mutex);
1543 err = got_error_set_errno(errcode,
1544 "pthread_mutex_lock");
1548 entry->idx = a->commits->ncommits;
1549 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1550 a->commits->ncommits++;
1552 if (*a->searching == TOG_SEARCH_FORWARD &&
1553 !*a->search_next_done) {
1555 err = match_commit(&have_match, id, commit, a->regex);
1559 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1562 errcode = pthread_mutex_unlock(&tog_mutex);
1563 if (errcode && err == NULL)
1564 err = got_error_set_errno(errcode,
1565 "pthread_mutex_unlock");
1568 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1574 select_commit(struct tog_log_view_state *s)
1576 struct commit_queue_entry *entry;
1579 entry = s->first_displayed_entry;
1581 if (ncommits == s->selected) {
1582 s->selected_entry = entry;
1585 entry = TAILQ_NEXT(entry, entry);
1590 static const struct got_error *
1591 draw_commits(struct tog_view *view)
1593 const struct got_error *err = NULL;
1594 struct tog_log_view_state *s = &view->state.log;
1595 struct commit_queue_entry *entry = s->selected_entry;
1596 const int limit = view->nlines;
1598 int ncommits, author_cols = 4;
1599 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1600 char *refs_str = NULL;
1602 struct tog_color *tc;
1603 static const size_t date_display_cols = 12;
1605 if (s->selected_entry &&
1606 !(view->searching && view->search_next_done == 0)) {
1607 struct got_reflist_head *refs;
1608 err = got_object_id_str(&id_str, s->selected_entry->id);
1611 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1612 s->selected_entry->id);
1614 err = build_refs_str(&refs_str, refs,
1615 s->selected_entry->id, s->repo);
1621 if (s->thread_args.commits_needed == 0)
1622 halfdelay(10); /* disable fast refresh */
1624 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1625 if (asprintf(&ncommits_str, " [%d/%d] %s",
1626 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1627 (view->searching && !view->search_next_done) ?
1628 "searching..." : "loading...") == -1) {
1629 err = got_error_from_errno("asprintf");
1633 const char *search_str = NULL;
1635 if (view->searching) {
1636 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1637 search_str = "no more matches";
1638 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1639 search_str = "no matches found";
1640 else if (!view->search_next_done)
1641 search_str = "searching...";
1644 if (asprintf(&ncommits_str, " [%d/%d] %s",
1645 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1646 search_str ? search_str :
1647 (refs_str ? refs_str : "")) == -1) {
1648 err = got_error_from_errno("asprintf");
1653 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1654 if (asprintf(&header, "commit %s %s%s",
1655 id_str ? id_str : "........................................",
1656 s->in_repo_path, ncommits_str) == -1) {
1657 err = got_error_from_errno("asprintf");
1661 } else if (asprintf(&header, "commit %s%s",
1662 id_str ? id_str : "........................................",
1663 ncommits_str) == -1) {
1664 err = got_error_from_errno("asprintf");
1668 err = format_line(&wline, &width, header, view->ncols, 0);
1672 werase(view->window);
1674 if (view_needs_focus_indication(view))
1675 wstandout(view->window);
1676 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1678 wattr_on(view->window,
1679 COLOR_PAIR(tc->colorpair), NULL);
1680 waddwstr(view->window, wline);
1682 wattr_off(view->window,
1683 COLOR_PAIR(tc->colorpair), NULL);
1684 while (width < view->ncols) {
1685 waddch(view->window, ' ');
1688 if (view_needs_focus_indication(view))
1689 wstandend(view->window);
1694 /* Grow author column size if necessary. */
1695 entry = s->first_displayed_entry;
1701 if (ncommits >= limit - 1)
1703 author = strdup(got_object_commit_get_author(entry->commit));
1704 if (author == NULL) {
1705 err = got_error_from_errno("strdup");
1708 err = format_author(&wauthor, &width, author, COLS,
1710 if (author_cols < width)
1711 author_cols = width;
1715 entry = TAILQ_NEXT(entry, entry);
1718 entry = s->first_displayed_entry;
1719 s->last_displayed_entry = s->first_displayed_entry;
1722 if (ncommits >= limit - 1)
1724 if (ncommits == s->selected)
1725 wstandout(view->window);
1726 err = draw_commit(view, entry->commit, entry->id,
1727 date_display_cols, author_cols);
1728 if (ncommits == s->selected)
1729 wstandend(view->window);
1733 s->last_displayed_entry = entry;
1734 entry = TAILQ_NEXT(entry, entry);
1749 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1751 struct commit_queue_entry *entry;
1754 entry = TAILQ_FIRST(&s->commits.head);
1755 if (s->first_displayed_entry == entry)
1758 entry = s->first_displayed_entry;
1759 while (entry && nscrolled < maxscroll) {
1760 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1762 s->first_displayed_entry = entry;
1768 static const struct got_error *
1769 trigger_log_thread(struct tog_view *view, int wait)
1771 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1774 halfdelay(1); /* fast refresh while loading commits */
1776 while (ta->commits_needed > 0 || ta->load_all) {
1777 if (ta->log_complete)
1780 /* Wake the log thread. */
1781 errcode = pthread_cond_signal(&ta->need_commits);
1783 return got_error_set_errno(errcode,
1784 "pthread_cond_signal");
1787 * The mutex will be released while the view loop waits
1788 * in wgetch(), at which time the log thread will run.
1793 /* Display progress update in log view. */
1794 show_log_view(view);
1798 /* Wait right here while next commit is being loaded. */
1799 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1801 return got_error_set_errno(errcode,
1802 "pthread_cond_wait");
1804 /* Display progress update in log view. */
1805 show_log_view(view);
1813 static const struct got_error *
1814 log_scroll_down(struct tog_view *view, int maxscroll)
1816 struct tog_log_view_state *s = &view->state.log;
1817 const struct got_error *err = NULL;
1818 struct commit_queue_entry *pentry;
1819 int nscrolled = 0, ncommits_needed;
1821 if (s->last_displayed_entry == NULL)
1824 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1825 if (s->commits.ncommits < ncommits_needed &&
1826 !s->thread_args.log_complete) {
1828 * Ask the log thread for required amount of commits.
1830 s->thread_args.commits_needed += maxscroll;
1831 err = trigger_log_thread(view, 1);
1837 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1841 s->last_displayed_entry = pentry;
1843 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1846 s->first_displayed_entry = pentry;
1847 } while (++nscrolled < maxscroll);
1852 static const struct got_error *
1853 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1854 struct got_commit_object *commit, struct got_object_id *commit_id,
1855 struct tog_view *log_view, struct got_repository *repo)
1857 const struct got_error *err;
1858 struct got_object_qid *parent_id;
1859 struct tog_view *diff_view;
1861 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1862 if (diff_view == NULL)
1863 return got_error_from_errno("view_open");
1865 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1866 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1867 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1869 *new_view = diff_view;
1873 static const struct got_error *
1874 tree_view_visit_subtree(struct tog_tree_view_state *s,
1875 struct got_tree_object *subtree)
1877 struct tog_parent_tree *parent;
1879 parent = calloc(1, sizeof(*parent));
1881 return got_error_from_errno("calloc");
1883 parent->tree = s->tree;
1884 parent->first_displayed_entry = s->first_displayed_entry;
1885 parent->selected_entry = s->selected_entry;
1886 parent->selected = s->selected;
1887 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1890 s->first_displayed_entry = NULL;
1894 static const struct got_error *
1895 tree_view_walk_path(struct tog_tree_view_state *s,
1896 struct got_object_id *commit_id, const char *path)
1898 const struct got_error *err = NULL;
1899 struct got_tree_object *tree = NULL;
1901 char *slash, *subpath = NULL;
1903 /* Walk the path and open corresponding tree objects. */
1906 struct got_tree_entry *te;
1907 struct got_object_id *tree_id;
1913 /* Ensure the correct subtree entry is selected. */
1914 slash = strchr(p, '/');
1916 te_name = strdup(p);
1918 te_name = strndup(p, slash - p);
1919 if (te_name == NULL) {
1920 err = got_error_from_errno("strndup");
1923 te = got_object_tree_find_entry(s->tree, te_name);
1925 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1930 s->first_displayed_entry = s->selected_entry = te;
1932 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1933 break; /* jump to this file's entry */
1935 slash = strchr(p, '/');
1937 subpath = strndup(path, slash - path);
1939 subpath = strdup(path);
1940 if (subpath == NULL) {
1941 err = got_error_from_errno("strdup");
1945 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1950 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1955 err = tree_view_visit_subtree(s, tree);
1957 got_object_tree_close(tree);
1971 static const struct got_error *
1972 browse_commit_tree(struct tog_view **new_view, int begin_x,
1973 struct commit_queue_entry *entry, const char *path,
1974 const char *head_ref_name, struct got_repository *repo)
1976 const struct got_error *err = NULL;
1977 struct tog_tree_view_state *s;
1978 struct tog_view *tree_view;
1980 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1981 if (tree_view == NULL)
1982 return got_error_from_errno("view_open");
1984 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
1987 s = &tree_view->state.tree;
1989 *new_view = tree_view;
1991 if (got_path_is_root_dir(path))
1994 return tree_view_walk_path(s, entry->id, path);
1997 static const struct got_error *
1998 block_signals_used_by_main_thread(void)
2003 if (sigemptyset(&sigset) == -1)
2004 return got_error_from_errno("sigemptyset");
2006 /* tog handles SIGWINCH and SIGCONT */
2007 if (sigaddset(&sigset, SIGWINCH) == -1)
2008 return got_error_from_errno("sigaddset");
2009 if (sigaddset(&sigset, SIGCONT) == -1)
2010 return got_error_from_errno("sigaddset");
2012 /* ncurses handles SIGTSTP */
2013 if (sigaddset(&sigset, SIGTSTP) == -1)
2014 return got_error_from_errno("sigaddset");
2016 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2018 return got_error_set_errno(errcode, "pthread_sigmask");
2024 log_thread(void *arg)
2026 const struct got_error *err = NULL;
2028 struct tog_log_thread_args *a = arg;
2031 err = block_signals_used_by_main_thread();
2035 while (!done && !err && !tog_sigpipe_received) {
2036 err = queue_commits(a);
2038 if (err->code != GOT_ERR_ITER_COMPLETED)
2042 } else if (a->commits_needed > 0 && !a->load_all)
2043 a->commits_needed--;
2045 errcode = pthread_mutex_lock(&tog_mutex);
2047 err = got_error_set_errno(errcode,
2048 "pthread_mutex_lock");
2050 } else if (*a->quit)
2052 else if (*a->first_displayed_entry == NULL) {
2053 *a->first_displayed_entry =
2054 TAILQ_FIRST(&a->commits->head);
2055 *a->selected_entry = *a->first_displayed_entry;
2058 errcode = pthread_cond_signal(&a->commit_loaded);
2060 err = got_error_set_errno(errcode,
2061 "pthread_cond_signal");
2062 pthread_mutex_unlock(&tog_mutex);
2067 a->commits_needed = 0;
2069 if (a->commits_needed == 0 && !a->load_all) {
2070 errcode = pthread_cond_wait(&a->need_commits,
2073 err = got_error_set_errno(errcode,
2074 "pthread_cond_wait");
2080 errcode = pthread_mutex_unlock(&tog_mutex);
2081 if (errcode && err == NULL)
2082 err = got_error_set_errno(errcode,
2083 "pthread_mutex_unlock");
2085 a->log_complete = 1;
2089 static const struct got_error *
2090 stop_log_thread(struct tog_log_view_state *s)
2092 const struct got_error *err = NULL;
2097 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2099 return got_error_set_errno(errcode,
2100 "pthread_cond_signal");
2101 errcode = pthread_mutex_unlock(&tog_mutex);
2103 return got_error_set_errno(errcode,
2104 "pthread_mutex_unlock");
2105 errcode = pthread_join(s->thread, (void **)&err);
2107 return got_error_set_errno(errcode, "pthread_join");
2108 errcode = pthread_mutex_lock(&tog_mutex);
2110 return got_error_set_errno(errcode,
2111 "pthread_mutex_lock");
2112 s->thread = 0; //NULL;
2115 if (s->thread_args.repo) {
2116 err = got_repo_close(s->thread_args.repo);
2117 s->thread_args.repo = NULL;
2120 if (s->thread_args.graph) {
2121 got_commit_graph_close(s->thread_args.graph);
2122 s->thread_args.graph = NULL;
2128 static const struct got_error *
2129 close_log_view(struct tog_view *view)
2131 const struct got_error *err = NULL;
2132 struct tog_log_view_state *s = &view->state.log;
2135 err = stop_log_thread(s);
2137 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2138 if (errcode && err == NULL)
2139 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2141 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2142 if (errcode && err == NULL)
2143 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2145 free_commits(&s->commits);
2146 free(s->in_repo_path);
2147 s->in_repo_path = NULL;
2150 free(s->head_ref_name);
2151 s->head_ref_name = NULL;
2155 static const struct got_error *
2156 search_start_log_view(struct tog_view *view)
2158 struct tog_log_view_state *s = &view->state.log;
2160 s->matched_entry = NULL;
2161 s->search_entry = NULL;
2165 static const struct got_error *
2166 search_next_log_view(struct tog_view *view)
2168 const struct got_error *err = NULL;
2169 struct tog_log_view_state *s = &view->state.log;
2170 struct commit_queue_entry *entry;
2172 /* Display progress update in log view. */
2173 show_log_view(view);
2177 if (s->search_entry) {
2179 errcode = pthread_mutex_unlock(&tog_mutex);
2181 return got_error_set_errno(errcode,
2182 "pthread_mutex_unlock");
2183 ch = wgetch(view->window);
2184 errcode = pthread_mutex_lock(&tog_mutex);
2186 return got_error_set_errno(errcode,
2187 "pthread_mutex_lock");
2188 if (ch == KEY_BACKSPACE) {
2189 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2192 if (view->searching == TOG_SEARCH_FORWARD)
2193 entry = TAILQ_NEXT(s->search_entry, entry);
2195 entry = TAILQ_PREV(s->search_entry,
2196 commit_queue_head, entry);
2197 } else if (s->matched_entry) {
2198 if (view->searching == TOG_SEARCH_FORWARD)
2199 entry = TAILQ_NEXT(s->matched_entry, entry);
2201 entry = TAILQ_PREV(s->matched_entry,
2202 commit_queue_head, entry);
2204 if (view->searching == TOG_SEARCH_FORWARD)
2205 entry = TAILQ_FIRST(&s->commits.head);
2207 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2213 if (entry == NULL) {
2214 if (s->thread_args.log_complete ||
2215 view->searching == TOG_SEARCH_BACKWARD) {
2216 view->search_next_done =
2217 (s->matched_entry == NULL ?
2218 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2219 s->search_entry = NULL;
2223 * Poke the log thread for more commits and return,
2224 * allowing the main loop to make progress. Search
2225 * will resume at s->search_entry once we come back.
2227 s->thread_args.commits_needed++;
2228 return trigger_log_thread(view, 0);
2231 err = match_commit(&have_match, entry->id, entry->commit,
2236 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2237 s->matched_entry = entry;
2241 s->search_entry = entry;
2242 if (view->searching == TOG_SEARCH_FORWARD)
2243 entry = TAILQ_NEXT(entry, entry);
2245 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2248 if (s->matched_entry) {
2249 int cur = s->selected_entry->idx;
2250 while (cur < s->matched_entry->idx) {
2251 err = input_log_view(NULL, view, KEY_DOWN);
2256 while (cur > s->matched_entry->idx) {
2257 err = input_log_view(NULL, view, KEY_UP);
2264 s->search_entry = NULL;
2269 static const struct got_error *
2270 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2271 struct got_repository *repo, const char *head_ref_name,
2272 const char *in_repo_path, int log_branches)
2274 const struct got_error *err = NULL;
2275 struct tog_log_view_state *s = &view->state.log;
2276 struct got_repository *thread_repo = NULL;
2277 struct got_commit_graph *thread_graph = NULL;
2280 if (in_repo_path != s->in_repo_path) {
2281 free(s->in_repo_path);
2282 s->in_repo_path = strdup(in_repo_path);
2283 if (s->in_repo_path == NULL)
2284 return got_error_from_errno("strdup");
2287 /* The commit queue only contains commits being displayed. */
2288 TAILQ_INIT(&s->commits.head);
2289 s->commits.ncommits = 0;
2292 if (head_ref_name) {
2293 s->head_ref_name = strdup(head_ref_name);
2294 if (s->head_ref_name == NULL) {
2295 err = got_error_from_errno("strdup");
2299 s->start_id = got_object_id_dup(start_id);
2300 if (s->start_id == NULL) {
2301 err = got_error_from_errno("got_object_id_dup");
2304 s->log_branches = log_branches;
2306 STAILQ_INIT(&s->colors);
2307 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2308 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2309 get_color_value("TOG_COLOR_COMMIT"));
2312 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2313 get_color_value("TOG_COLOR_AUTHOR"));
2315 free_colors(&s->colors);
2318 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2319 get_color_value("TOG_COLOR_DATE"));
2321 free_colors(&s->colors);
2326 view->show = show_log_view;
2327 view->input = input_log_view;
2328 view->close = close_log_view;
2329 view->search_start = search_start_log_view;
2330 view->search_next = search_next_log_view;
2332 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2335 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2339 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2340 s->repo, NULL, NULL);
2344 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2346 err = got_error_set_errno(errcode, "pthread_cond_init");
2349 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2351 err = got_error_set_errno(errcode, "pthread_cond_init");
2355 s->thread_args.commits_needed = view->nlines;
2356 s->thread_args.graph = thread_graph;
2357 s->thread_args.commits = &s->commits;
2358 s->thread_args.in_repo_path = s->in_repo_path;
2359 s->thread_args.start_id = s->start_id;
2360 s->thread_args.repo = thread_repo;
2361 s->thread_args.log_complete = 0;
2362 s->thread_args.quit = &s->quit;
2363 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2364 s->thread_args.selected_entry = &s->selected_entry;
2365 s->thread_args.searching = &view->searching;
2366 s->thread_args.search_next_done = &view->search_next_done;
2367 s->thread_args.regex = &view->regex;
2370 close_log_view(view);
2374 static const struct got_error *
2375 show_log_view(struct tog_view *view)
2377 const struct got_error *err;
2378 struct tog_log_view_state *s = &view->state.log;
2380 if (s->thread == 0) { //NULL) {
2381 int errcode = pthread_create(&s->thread, NULL, log_thread,
2384 return got_error_set_errno(errcode, "pthread_create");
2385 if (s->thread_args.commits_needed > 0) {
2386 err = trigger_log_thread(view, 1);
2392 return draw_commits(view);
2395 static const struct got_error *
2396 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2398 const struct got_error *err = NULL;
2399 struct tog_log_view_state *s = &view->state.log;
2400 struct tog_view *diff_view = NULL, *tree_view = NULL;
2401 struct tog_view *ref_view = NULL;
2402 struct commit_queue_entry *entry;
2405 if (s->thread_args.load_all) {
2406 if (ch == KEY_BACKSPACE)
2407 s->thread_args.load_all = 0;
2408 else if (s->thread_args.log_complete) {
2409 s->thread_args.load_all = 0;
2410 log_scroll_down(view, s->commits.ncommits);
2411 s->selected = MIN(view->nlines - 2,
2412 s->commits.ncommits - 1);
2427 if (s->first_displayed_entry == NULL)
2429 if (s->selected > 0)
2432 log_scroll_up(s, 1);
2438 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2443 if (s->first_displayed_entry == NULL)
2445 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2448 log_scroll_up(s, view->nlines - 1);
2456 if (s->first_displayed_entry == NULL)
2458 if (s->selected < MIN(view->nlines - 2,
2459 s->commits.ncommits - 1))
2462 err = log_scroll_down(view, 1);
2470 /* We don't know yet how many commits, so we're forced to
2471 * traverse them all. */
2472 if (!s->thread_args.log_complete) {
2473 s->thread_args.load_all = 1;
2474 return trigger_log_thread(view, 0);
2478 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2479 for (n = 0; n < view->nlines - 1; n++) {
2482 s->first_displayed_entry = entry;
2483 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2486 s->selected = n - 1;
2492 struct commit_queue_entry *first;
2493 first = s->first_displayed_entry;
2496 err = log_scroll_down(view, view->nlines - 1);
2499 if (first == s->first_displayed_entry &&
2500 s->selected < MIN(view->nlines - 2,
2501 s->commits.ncommits - 1)) {
2502 /* can't scroll further down */
2503 s->selected = MIN(view->nlines - 2,
2504 s->commits.ncommits - 1);
2510 if (s->selected > view->nlines - 2)
2511 s->selected = view->nlines - 2;
2512 if (s->selected > s->commits.ncommits - 1)
2513 s->selected = s->commits.ncommits - 1;
2515 if (s->commits.ncommits < view->nlines - 1 &&
2516 !s->thread_args.log_complete) {
2517 s->thread_args.commits_needed += (view->nlines - 1) -
2518 s->commits.ncommits;
2519 err = trigger_log_thread(view, 1);
2525 if (s->selected_entry == NULL)
2527 if (view_is_parent_view(view))
2528 begin_x = view_split_begin_x(view->begin_x);
2529 err = open_diff_view_for_commit(&diff_view, begin_x,
2530 s->selected_entry->commit, s->selected_entry->id,
2535 diff_view->focussed = 1;
2536 if (view_is_parent_view(view)) {
2537 err = view_close_child(view);
2540 view_set_child(view, diff_view);
2541 view->focus_child = 1;
2543 *new_view = diff_view;
2546 if (s->selected_entry == NULL)
2548 if (view_is_parent_view(view))
2549 begin_x = view_split_begin_x(view->begin_x);
2550 err = browse_commit_tree(&tree_view, begin_x,
2551 s->selected_entry, s->in_repo_path, s->head_ref_name,
2556 tree_view->focussed = 1;
2557 if (view_is_parent_view(view)) {
2558 err = view_close_child(view);
2561 view_set_child(view, tree_view);
2562 view->focus_child = 1;
2564 *new_view = tree_view;
2569 if (ch == KEY_BACKSPACE &&
2570 got_path_is_root_dir(s->in_repo_path))
2572 err = stop_log_thread(s);
2575 if (ch == KEY_BACKSPACE) {
2577 err = got_path_dirname(&parent_path, s->in_repo_path);
2580 free(s->in_repo_path);
2581 s->in_repo_path = parent_path;
2582 s->thread_args.in_repo_path = s->in_repo_path;
2583 } else if (ch == CTRL('l')) {
2584 struct got_object_id *start_id;
2585 err = got_repo_match_object_id(&start_id, NULL,
2586 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2587 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2591 s->start_id = start_id;
2592 s->thread_args.start_id = s->start_id;
2594 s->log_branches = !s->log_branches;
2596 err = got_repo_open(&s->thread_args.repo,
2597 got_repo_get_path(s->repo), NULL);
2601 err = tog_load_refs(s->repo, 0);
2604 err = got_commit_graph_open(&s->thread_args.graph,
2605 s->in_repo_path, !s->log_branches);
2608 err = got_commit_graph_iter_start(s->thread_args.graph,
2609 s->start_id, s->repo, NULL, NULL);
2612 free_commits(&s->commits);
2613 s->first_displayed_entry = NULL;
2614 s->last_displayed_entry = NULL;
2615 s->selected_entry = NULL;
2617 s->thread_args.log_complete = 0;
2619 s->thread_args.commits_needed = view->nlines;
2622 if (view_is_parent_view(view))
2623 begin_x = view_split_begin_x(view->begin_x);
2624 ref_view = view_open(view->nlines, view->ncols,
2625 view->begin_y, begin_x, TOG_VIEW_REF);
2626 if (ref_view == NULL)
2627 return got_error_from_errno("view_open");
2628 err = open_ref_view(ref_view, s->repo);
2630 view_close(ref_view);
2634 ref_view->focussed = 1;
2635 if (view_is_parent_view(view)) {
2636 err = view_close_child(view);
2639 view_set_child(view, ref_view);
2640 view->focus_child = 1;
2642 *new_view = ref_view;
2651 static const struct got_error *
2652 apply_unveil(const char *repo_path, const char *worktree_path)
2654 const struct got_error *error;
2657 if (unveil("gmon.out", "rwc") != 0)
2658 return got_error_from_errno2("unveil", "gmon.out");
2660 if (repo_path && unveil(repo_path, "r") != 0)
2661 return got_error_from_errno2("unveil", repo_path);
2663 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2664 return got_error_from_errno2("unveil", worktree_path);
2666 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2667 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2669 error = got_privsep_unveil_exec_helpers();
2673 if (unveil(NULL, NULL) != 0)
2674 return got_error_from_errno("unveil");
2684 halfdelay(1); /* Do fast refresh while initial view is loading. */
2687 intrflush(stdscr, FALSE);
2688 keypad(stdscr, TRUE);
2690 if (getenv("TOG_COLORS") != NULL) {
2692 use_default_colors();
2694 signal(SIGWINCH, tog_sigwinch);
2695 signal(SIGPIPE, tog_sigpipe);
2696 signal(SIGCONT, tog_sigcont);
2699 static const struct got_error *
2700 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2701 struct got_repository *repo, struct got_worktree *worktree)
2703 const struct got_error *err = NULL;
2706 *in_repo_path = strdup("/");
2707 if (*in_repo_path == NULL)
2708 return got_error_from_errno("strdup");
2713 const char *prefix = got_worktree_get_path_prefix(worktree);
2716 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2719 if (asprintf(in_repo_path, "%s%s%s", prefix,
2720 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2722 err = got_error_from_errno("asprintf");
2723 *in_repo_path = NULL;
2727 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2732 static const struct got_error *
2733 cmd_log(int argc, char *argv[])
2735 const struct got_error *error;
2736 struct got_repository *repo = NULL;
2737 struct got_worktree *worktree = NULL;
2738 struct got_object_id *start_id = NULL;
2739 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2740 char *start_commit = NULL, *label = NULL;
2741 struct got_reference *ref = NULL;
2742 const char *head_ref_name = NULL;
2743 int ch, log_branches = 0;
2744 struct tog_view *view;
2746 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2752 start_commit = optarg;
2755 repo_path = realpath(optarg, NULL);
2756 if (repo_path == NULL)
2757 return got_error_from_errno2("realpath",
2772 if (repo_path == NULL) {
2773 cwd = getcwd(NULL, 0);
2775 return got_error_from_errno("getcwd");
2776 error = got_worktree_open(&worktree, cwd);
2777 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2781 strdup(got_worktree_get_repo_path(worktree));
2783 repo_path = strdup(cwd);
2784 if (repo_path == NULL) {
2785 error = got_error_from_errno("strdup");
2790 error = got_repo_open(&repo, repo_path, NULL);
2794 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2801 error = apply_unveil(got_repo_get_path(repo),
2802 worktree ? got_worktree_get_root_path(worktree) : NULL);
2806 /* already loaded by tog_log_with_path()? */
2807 if (TAILQ_EMPTY(&tog_refs)) {
2808 error = tog_load_refs(repo, 0);
2813 if (start_commit == NULL) {
2814 error = got_repo_match_object_id(&start_id, &label,
2815 worktree ? got_worktree_get_head_ref_name(worktree) :
2816 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2819 head_ref_name = label;
2821 error = got_ref_open(&ref, repo, start_commit, 0);
2823 head_ref_name = got_ref_get_name(ref);
2824 else if (error->code != GOT_ERR_NOT_REF)
2826 error = got_repo_match_object_id(&start_id, NULL,
2827 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2832 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2834 error = got_error_from_errno("view_open");
2837 error = open_log_view(view, start_id, repo, head_ref_name,
2838 in_repo_path, log_branches);
2842 /* Release work tree lock. */
2843 got_worktree_close(worktree);
2846 error = view_loop(view);
2856 const struct got_error *close_err = got_repo_close(repo);
2861 got_worktree_close(worktree);
2870 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2871 "[-w] object1 object2\n", getprogname());
2876 match_line(const char *line, regex_t *regex, size_t nmatch,
2877 regmatch_t *regmatch)
2879 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2883 match_color(struct tog_colors *colors, const char *line)
2885 struct tog_color *tc = NULL;
2887 STAILQ_FOREACH(tc, colors, entry) {
2888 if (match_line(line, &tc->regex, 0, NULL))
2895 static const struct got_error *
2896 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2897 WINDOW *window, regmatch_t *regmatch)
2899 const struct got_error *err = NULL;
2906 s = strndup(line, regmatch->rm_so);
2908 return got_error_from_errno("strndup");
2910 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2915 waddwstr(window, wline);
2922 s = strndup(line + regmatch->rm_so,
2923 regmatch->rm_eo - regmatch->rm_so);
2925 err = got_error_from_errno("strndup");
2929 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2934 wattr_on(window, A_STANDOUT, NULL);
2935 waddwstr(window, wline);
2936 wattr_off(window, A_STANDOUT, NULL);
2943 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2944 err = format_line(&wline, &width,
2945 line + regmatch->rm_eo, wlimit, col_tab_align);
2948 waddwstr(window, wline);
2956 static const struct got_error *
2957 draw_file(struct tog_view *view, const char *header)
2959 struct tog_diff_view_state *s = &view->state.diff;
2960 regmatch_t *regmatch = &view->regmatch;
2961 const struct got_error *err;
2964 size_t linesize = 0;
2966 struct tog_color *tc;
2969 int max_lines = view->nlines;
2970 int nlines = s->nlines;
2973 line_offset = s->line_offsets[s->first_displayed_line - 1];
2974 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2975 return got_error_from_errno("fseek");
2977 werase(view->window);
2980 if (asprintf(&line, "[%d/%d] %s",
2981 s->first_displayed_line - 1 + s->selected_line, nlines,
2983 return got_error_from_errno("asprintf");
2984 err = format_line(&wline, &width, line, view->ncols, 0);
2989 if (view_needs_focus_indication(view))
2990 wstandout(view->window);
2991 waddwstr(view->window, wline);
2994 if (view_needs_focus_indication(view))
2995 wstandend(view->window);
2996 if (width <= view->ncols - 1)
2997 waddch(view->window, '\n');
3006 while (max_lines > 0 && nprinted < max_lines) {
3007 linelen = getline(&line, &linesize, s->f);
3008 if (linelen == -1) {
3014 return got_ferror(s->f, GOT_ERR_IO);
3017 tc = match_color(&s->colors, line);
3019 wattr_on(view->window,
3020 COLOR_PAIR(tc->colorpair), NULL);
3021 if (s->first_displayed_line + nprinted == s->matched_line &&
3022 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3023 err = add_matched_line(&width, line, view->ncols, 0,
3024 view->window, regmatch);
3030 err = format_line(&wline, &width, line, view->ncols, 0);
3035 waddwstr(view->window, wline);
3040 wattr_off(view->window,
3041 COLOR_PAIR(tc->colorpair), NULL);
3042 if (width <= view->ncols - 1)
3043 waddch(view->window, '\n');
3048 s->last_displayed_line = s->first_displayed_line +
3051 s->last_displayed_line = s->first_displayed_line;
3056 while (nprinted < view->nlines) {
3057 waddch(view->window, '\n');
3061 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3066 wstandout(view->window);
3067 waddwstr(view->window, wline);
3070 wstandend(view->window);
3077 get_datestr(time_t *time, char *datebuf)
3079 struct tm mytm, *tm;
3082 tm = gmtime_r(time, &mytm);
3085 s = asctime_r(tm, datebuf);
3088 p = strchr(s, '\n');
3094 static const struct got_error *
3095 get_changed_paths(struct got_pathlist_head *paths,
3096 struct got_commit_object *commit, struct got_repository *repo)
3098 const struct got_error *err = NULL;
3099 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3100 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3101 struct got_object_qid *qid;
3103 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3105 struct got_commit_object *pcommit;
3106 err = got_object_open_as_commit(&pcommit, repo,
3111 tree_id1 = got_object_id_dup(
3112 got_object_commit_get_tree_id(pcommit));
3113 if (tree_id1 == NULL) {
3114 got_object_commit_close(pcommit);
3115 return got_error_from_errno("got_object_id_dup");
3117 got_object_commit_close(pcommit);
3122 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3127 tree_id2 = got_object_commit_get_tree_id(commit);
3128 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3132 err = got_diff_tree(tree1, tree2, "", "", repo,
3133 got_diff_tree_collect_changed_paths, paths, 0);
3136 got_object_tree_close(tree1);
3138 got_object_tree_close(tree2);
3143 static const struct got_error *
3144 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3148 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3150 return got_error_from_errno("reallocarray");
3152 (*line_offsets)[*nlines] = off;
3157 static const struct got_error *
3158 write_commit_info(off_t **line_offsets, size_t *nlines,
3159 struct got_object_id *commit_id, struct got_reflist_head *refs,
3160 struct got_repository *repo, FILE *outfile)
3162 const struct got_error *err = NULL;
3163 char datebuf[26], *datestr;
3164 struct got_commit_object *commit;
3165 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3166 time_t committer_time;
3167 const char *author, *committer;
3168 char *refs_str = NULL;
3169 struct got_pathlist_head changed_paths;
3170 struct got_pathlist_entry *pe;
3174 TAILQ_INIT(&changed_paths);
3177 err = build_refs_str(&refs_str, refs, commit_id, repo);
3182 err = got_object_open_as_commit(&commit, repo, commit_id);
3186 err = got_object_id_str(&id_str, commit_id);
3188 err = got_error_from_errno("got_object_id_str");
3192 err = add_line_offset(line_offsets, nlines, 0);
3196 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3197 refs_str ? refs_str : "", refs_str ? ")" : "");
3199 err = got_error_from_errno("fprintf");
3203 err = add_line_offset(line_offsets, nlines, outoff);
3207 n = fprintf(outfile, "from: %s\n",
3208 got_object_commit_get_author(commit));
3210 err = got_error_from_errno("fprintf");
3214 err = add_line_offset(line_offsets, nlines, outoff);
3218 committer_time = got_object_commit_get_committer_time(commit);
3219 datestr = get_datestr(&committer_time, datebuf);
3221 n = fprintf(outfile, "date: %s UTC\n", datestr);
3223 err = got_error_from_errno("fprintf");
3227 err = add_line_offset(line_offsets, nlines, outoff);
3231 author = got_object_commit_get_author(commit);
3232 committer = got_object_commit_get_committer(commit);
3233 if (strcmp(author, committer) != 0) {
3234 n = fprintf(outfile, "via: %s\n", committer);
3236 err = got_error_from_errno("fprintf");
3240 err = add_line_offset(line_offsets, nlines, outoff);
3244 if (got_object_commit_get_nparents(commit) > 1) {
3245 const struct got_object_id_queue *parent_ids;
3246 struct got_object_qid *qid;
3248 parent_ids = got_object_commit_get_parent_ids(commit);
3249 STAILQ_FOREACH(qid, parent_ids, entry) {
3250 err = got_object_id_str(&id_str, qid->id);
3253 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3255 err = got_error_from_errno("fprintf");
3259 err = add_line_offset(line_offsets, nlines, outoff);
3267 err = got_object_commit_get_logmsg(&logmsg, commit);
3271 while ((line = strsep(&s, "\n")) != NULL) {
3272 n = fprintf(outfile, "%s\n", line);
3274 err = got_error_from_errno("fprintf");
3278 err = add_line_offset(line_offsets, nlines, outoff);
3283 err = get_changed_paths(&changed_paths, commit, repo);
3286 TAILQ_FOREACH(pe, &changed_paths, entry) {
3287 struct got_diff_changed_path *cp = pe->data;
3288 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3290 err = got_error_from_errno("fprintf");
3294 err = add_line_offset(line_offsets, nlines, outoff);
3297 free((char *)pe->path);
3301 fputc('\n', outfile);
3303 err = add_line_offset(line_offsets, nlines, outoff);
3305 got_pathlist_free(&changed_paths);
3309 got_object_commit_close(commit);
3311 free(*line_offsets);
3312 *line_offsets = NULL;
3318 static const struct got_error *
3319 create_diff(struct tog_diff_view_state *s)
3321 const struct got_error *err = NULL;
3325 free(s->line_offsets);
3326 s->line_offsets = malloc(sizeof(off_t));
3327 if (s->line_offsets == NULL)
3328 return got_error_from_errno("malloc");
3333 err = got_error_from_errno("got_opentemp");
3336 if (s->f && fclose(s->f) == EOF) {
3337 err = got_error_from_errno("fclose");
3343 err = got_object_get_type(&obj_type, s->repo, s->id1);
3345 err = got_object_get_type(&obj_type, s->repo, s->id2);
3350 case GOT_OBJ_TYPE_BLOB:
3351 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3352 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3353 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3355 case GOT_OBJ_TYPE_TREE:
3356 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3357 s->id1, s->id2, NULL, "", "", s->diff_context,
3358 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3360 case GOT_OBJ_TYPE_COMMIT: {
3361 const struct got_object_id_queue *parent_ids;
3362 struct got_object_qid *pid;
3363 struct got_commit_object *commit2;
3364 struct got_reflist_head *refs;
3366 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3369 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3370 /* Show commit info if we're diffing to a parent/root commit. */
3371 if (s->id1 == NULL) {
3372 err = write_commit_info(&s->line_offsets, &s->nlines,
3373 s->id2, refs, s->repo, s->f);
3377 parent_ids = got_object_commit_get_parent_ids(commit2);
3378 STAILQ_FOREACH(pid, parent_ids, entry) {
3379 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3380 err = write_commit_info(
3381 &s->line_offsets, &s->nlines,
3382 s->id2, refs, s->repo, s->f);
3389 got_object_commit_close(commit2);
3391 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3392 s->id1, s->id2, NULL, s->diff_context, s->ignore_whitespace,
3393 s->force_text_diff, s->repo, s->f);
3397 err = got_error(GOT_ERR_OBJ_TYPE);
3403 if (s->f && fflush(s->f) != 0 && err == NULL)
3404 err = got_error_from_errno("fflush");
3409 diff_view_indicate_progress(struct tog_view *view)
3411 mvwaddstr(view->window, 0, 0, "diffing...");
3416 static const struct got_error *
3417 search_start_diff_view(struct tog_view *view)
3419 struct tog_diff_view_state *s = &view->state.diff;
3421 s->matched_line = 0;
3425 static const struct got_error *
3426 search_next_diff_view(struct tog_view *view)
3428 struct tog_diff_view_state *s = &view->state.diff;
3431 size_t linesize = 0;
3434 if (!view->searching) {
3435 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3439 if (s->matched_line) {
3440 if (view->searching == TOG_SEARCH_FORWARD)
3441 lineno = s->matched_line + 1;
3443 lineno = s->matched_line - 1;
3445 if (view->searching == TOG_SEARCH_FORWARD)
3454 if (lineno <= 0 || lineno > s->nlines) {
3455 if (s->matched_line == 0) {
3456 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3460 if (view->searching == TOG_SEARCH_FORWARD)
3466 offset = s->line_offsets[lineno - 1];
3467 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3469 return got_error_from_errno("fseeko");
3471 linelen = getline(&line, &linesize, s->f);
3472 if (linelen != -1 &&
3473 match_line(line, &view->regex, 1, &view->regmatch)) {
3474 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3475 s->matched_line = lineno;
3478 if (view->searching == TOG_SEARCH_FORWARD)
3485 if (s->matched_line) {
3486 s->first_displayed_line = s->matched_line;
3487 s->selected_line = 1;
3493 static const struct got_error *
3494 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3495 struct got_object_id *id2, const char *label1, const char *label2,
3496 int diff_context, int ignore_whitespace, int force_text_diff,
3497 struct tog_view *log_view, struct got_repository *repo)
3499 const struct got_error *err;
3500 struct tog_diff_view_state *s = &view->state.diff;
3502 if (id1 != NULL && id2 != NULL) {
3504 err = got_object_get_type(&type1, repo, id1);
3507 err = got_object_get_type(&type2, repo, id2);
3512 return got_error(GOT_ERR_OBJ_TYPE);
3514 s->first_displayed_line = 1;
3515 s->last_displayed_line = view->nlines;
3516 s->selected_line = 1;
3524 s->id1 = got_object_id_dup(id1);
3526 return got_error_from_errno("got_object_id_dup");
3530 s->id2 = got_object_id_dup(id2);
3531 if (s->id2 == NULL) {
3534 return got_error_from_errno("got_object_id_dup");
3537 s->first_displayed_line = 1;
3538 s->last_displayed_line = view->nlines;
3539 s->diff_context = diff_context;
3540 s->ignore_whitespace = ignore_whitespace;
3541 s->force_text_diff = force_text_diff;
3542 s->log_view = log_view;
3545 STAILQ_INIT(&s->colors);
3546 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3547 err = add_color(&s->colors,
3548 "^-", TOG_COLOR_DIFF_MINUS,
3549 get_color_value("TOG_COLOR_DIFF_MINUS"));
3552 err = add_color(&s->colors, "^\\+",
3553 TOG_COLOR_DIFF_PLUS,
3554 get_color_value("TOG_COLOR_DIFF_PLUS"));
3556 free_colors(&s->colors);
3559 err = add_color(&s->colors,
3560 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3561 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3563 free_colors(&s->colors);
3567 err = add_color(&s->colors,
3568 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3569 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3570 get_color_value("TOG_COLOR_DIFF_META"));
3572 free_colors(&s->colors);
3576 err = add_color(&s->colors,
3577 "^(from|via): ", TOG_COLOR_AUTHOR,
3578 get_color_value("TOG_COLOR_AUTHOR"));
3580 free_colors(&s->colors);
3584 err = add_color(&s->colors,
3585 "^date: ", TOG_COLOR_DATE,
3586 get_color_value("TOG_COLOR_DATE"));
3588 free_colors(&s->colors);
3593 if (log_view && view_is_splitscreen(view))
3594 show_log_view(log_view); /* draw vborder */
3595 diff_view_indicate_progress(view);
3597 s->line_offsets = NULL;
3599 err = create_diff(s);
3605 free_colors(&s->colors);
3609 view->show = show_diff_view;
3610 view->input = input_diff_view;
3611 view->close = close_diff_view;
3612 view->search_start = search_start_diff_view;
3613 view->search_next = search_next_diff_view;
3618 static const struct got_error *
3619 close_diff_view(struct tog_view *view)
3621 const struct got_error *err = NULL;
3622 struct tog_diff_view_state *s = &view->state.diff;
3628 if (s->f && fclose(s->f) == EOF)
3629 err = got_error_from_errno("fclose");
3630 free_colors(&s->colors);
3631 free(s->line_offsets);
3632 s->line_offsets = NULL;
3637 static const struct got_error *
3638 show_diff_view(struct tog_view *view)
3640 const struct got_error *err;
3641 struct tog_diff_view_state *s = &view->state.diff;
3642 char *id_str1 = NULL, *id_str2, *header;
3643 const char *label1, *label2;
3646 err = got_object_id_str(&id_str1, s->id1);
3649 label1 = s->label1 ? : id_str1;
3651 label1 = "/dev/null";
3653 err = got_object_id_str(&id_str2, s->id2);
3656 label2 = s->label2 ? : id_str2;
3658 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3659 err = got_error_from_errno("asprintf");
3667 err = draw_file(view, header);
3672 static const struct got_error *
3673 set_selected_commit(struct tog_diff_view_state *s,
3674 struct commit_queue_entry *entry)
3676 const struct got_error *err;
3677 const struct got_object_id_queue *parent_ids;
3678 struct got_commit_object *selected_commit;
3679 struct got_object_qid *pid;
3682 s->id2 = got_object_id_dup(entry->id);
3684 return got_error_from_errno("got_object_id_dup");
3686 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3689 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3691 pid = STAILQ_FIRST(parent_ids);
3692 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3693 got_object_commit_close(selected_commit);
3697 static const struct got_error *
3698 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3700 const struct got_error *err = NULL;
3701 struct tog_diff_view_state *s = &view->state.diff;
3702 struct tog_log_view_state *ls;
3703 struct commit_queue_entry *old_selected_entry;
3705 size_t linesize = 0;
3713 s->force_text_diff = !s->force_text_diff;
3715 s->ignore_whitespace = !s->ignore_whitespace;
3716 wclear(view->window);
3717 s->first_displayed_line = 1;
3718 s->last_displayed_line = view->nlines;
3719 diff_view_indicate_progress(view);
3720 err = create_diff(s);
3724 s->first_displayed_line = 1;
3731 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3737 if (s->first_displayed_line > 1)
3738 s->first_displayed_line--;
3742 if (s->first_displayed_line == 1)
3745 while (i++ < view->nlines - 1 &&
3746 s->first_displayed_line > 1)
3747 s->first_displayed_line--;
3753 s->first_displayed_line++;
3761 while (!s->eof && i++ < view->nlines - 1) {
3762 linelen = getline(&line, &linesize, s->f);
3763 s->first_displayed_line++;
3764 if (linelen == -1) {
3768 err = got_ferror(s->f, GOT_ERR_IO);
3775 if (s->diff_context > 0) {
3777 diff_view_indicate_progress(view);
3778 err = create_diff(s);
3779 if (s->first_displayed_line + view->nlines - 1 >
3781 s->first_displayed_line = 1;
3782 s->last_displayed_line = view->nlines;
3787 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3789 diff_view_indicate_progress(view);
3790 err = create_diff(s);
3795 if (s->log_view == NULL)
3797 ls = &s->log_view->state.log;
3798 old_selected_entry = ls->selected_entry;
3800 err = input_log_view(NULL, s->log_view, KEY_UP);
3804 if (old_selected_entry == ls->selected_entry)
3807 err = set_selected_commit(s, ls->selected_entry);
3811 s->first_displayed_line = 1;
3812 s->last_displayed_line = view->nlines;
3814 diff_view_indicate_progress(view);
3815 err = create_diff(s);
3819 if (s->log_view == NULL)
3821 ls = &s->log_view->state.log;
3822 old_selected_entry = ls->selected_entry;
3824 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3828 if (old_selected_entry == ls->selected_entry)
3831 err = set_selected_commit(s, ls->selected_entry);
3835 s->first_displayed_line = 1;
3836 s->last_displayed_line = view->nlines;
3838 diff_view_indicate_progress(view);
3839 err = create_diff(s);
3848 static const struct got_error *
3849 cmd_diff(int argc, char *argv[])
3851 const struct got_error *error = NULL;
3852 struct got_repository *repo = NULL;
3853 struct got_worktree *worktree = NULL;
3854 struct got_object_id *id1 = NULL, *id2 = NULL;
3855 char *repo_path = NULL, *cwd = NULL;
3856 char *id_str1 = NULL, *id_str2 = NULL;
3857 char *label1 = NULL, *label2 = NULL;
3858 int diff_context = 3, ignore_whitespace = 0;
3859 int ch, force_text_diff = 0;
3861 struct tog_view *view;
3863 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3866 force_text_diff = 1;
3869 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3872 err(1, "-C option %s", errstr);
3875 repo_path = realpath(optarg, NULL);
3876 if (repo_path == NULL)
3877 return got_error_from_errno2("realpath",
3879 got_path_strip_trailing_slashes(repo_path);
3882 ignore_whitespace = 1;
3894 usage_diff(); /* TODO show local worktree changes */
3895 } else if (argc == 2) {
3901 if (repo_path == NULL) {
3902 cwd = getcwd(NULL, 0);
3904 return got_error_from_errno("getcwd");
3905 error = got_worktree_open(&worktree, cwd);
3906 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3910 strdup(got_worktree_get_repo_path(worktree));
3912 repo_path = strdup(cwd);
3913 if (repo_path == NULL) {
3914 error = got_error_from_errno("strdup");
3919 error = got_repo_open(&repo, repo_path, NULL);
3925 error = apply_unveil(got_repo_get_path(repo), NULL);
3929 error = tog_load_refs(repo, 0);
3933 error = got_repo_match_object_id(&id1, &label1, id_str1,
3934 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3938 error = got_repo_match_object_id(&id2, &label2, id_str2,
3939 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3943 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3945 error = got_error_from_errno("view_open");
3948 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3949 ignore_whitespace, force_text_diff, NULL, repo);
3952 error = view_loop(view);
3959 const struct got_error *close_err = got_repo_close(repo);
3964 got_worktree_close(worktree);
3973 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3978 struct tog_blame_line {
3980 struct got_object_id *id;
3983 static const struct got_error *
3984 draw_blame(struct tog_view *view)
3986 struct tog_blame_view_state *s = &view->state.blame;
3987 struct tog_blame *blame = &s->blame;
3988 regmatch_t *regmatch = &view->regmatch;
3989 const struct got_error *err;
3990 int lineno = 0, nprinted = 0;
3992 size_t linesize = 0;
3996 struct tog_blame_line *blame_line;
3997 struct got_object_id *prev_id = NULL;
3999 struct tog_color *tc;
4001 err = got_object_id_str(&id_str, s->blamed_commit->id);
4006 werase(view->window);
4008 if (asprintf(&line, "commit %s", id_str) == -1) {
4009 err = got_error_from_errno("asprintf");
4014 err = format_line(&wline, &width, line, view->ncols, 0);
4019 if (view_needs_focus_indication(view))
4020 wstandout(view->window);
4021 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4023 wattr_on(view->window,
4024 COLOR_PAIR(tc->colorpair), NULL);
4025 waddwstr(view->window, wline);
4027 wattr_off(view->window,
4028 COLOR_PAIR(tc->colorpair), NULL);
4029 if (view_needs_focus_indication(view))
4030 wstandend(view->window);
4033 if (width < view->ncols - 1)
4034 waddch(view->window, '\n');
4036 if (asprintf(&line, "[%d/%d] %s%s",
4037 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4038 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4040 return got_error_from_errno("asprintf");
4043 err = format_line(&wline, &width, line, view->ncols, 0);
4048 waddwstr(view->window, wline);
4051 if (width < view->ncols - 1)
4052 waddch(view->window, '\n');
4055 while (nprinted < view->nlines - 2) {
4056 linelen = getline(&line, &linesize, blame->f);
4057 if (linelen == -1) {
4058 if (feof(blame->f)) {
4063 return got_ferror(blame->f, GOT_ERR_IO);
4065 if (++lineno < s->first_displayed_line)
4068 if (view->focussed && nprinted == s->selected_line - 1)
4069 wstandout(view->window);
4071 if (blame->nlines > 0) {
4072 blame_line = &blame->lines[lineno - 1];
4073 if (blame_line->annotated && prev_id &&
4074 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4076 nprinted == s->selected_line - 1)) {
4077 waddstr(view->window, " ");
4078 } else if (blame_line->annotated) {
4080 err = got_object_id_str(&id_str, blame_line->id);
4085 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4087 wattr_on(view->window,
4088 COLOR_PAIR(tc->colorpair), NULL);
4089 wprintw(view->window, "%.8s", id_str);
4091 wattr_off(view->window,
4092 COLOR_PAIR(tc->colorpair), NULL);
4094 prev_id = blame_line->id;
4096 waddstr(view->window, "........");
4100 waddstr(view->window, "........");
4104 if (view->focussed && nprinted == s->selected_line - 1)
4105 wstandend(view->window);
4106 waddstr(view->window, " ");
4108 if (view->ncols <= 9) {
4110 wline = wcsdup(L"");
4111 if (wline == NULL) {
4112 err = got_error_from_errno("wcsdup");
4116 } else if (s->first_displayed_line + nprinted ==
4118 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4119 err = add_matched_line(&width, line, view->ncols - 9, 9,
4120 view->window, regmatch);
4127 err = format_line(&wline, &width, line,
4128 view->ncols - 9, 9);
4129 waddwstr(view->window, wline);
4135 if (width <= view->ncols - 1)
4136 waddch(view->window, '\n');
4137 if (++nprinted == 1)
4138 s->first_displayed_line = lineno;
4141 s->last_displayed_line = lineno;
4148 static const struct got_error *
4149 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4151 const struct got_error *err = NULL;
4152 struct tog_blame_cb_args *a = arg;
4153 struct tog_blame_line *line;
4156 if (nlines != a->nlines ||
4157 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4158 return got_error(GOT_ERR_RANGE);
4160 errcode = pthread_mutex_lock(&tog_mutex);
4162 return got_error_set_errno(errcode, "pthread_mutex_lock");
4164 if (*a->quit) { /* user has quit the blame view */
4165 err = got_error(GOT_ERR_ITER_COMPLETED);
4170 goto done; /* no change in this commit */
4172 line = &a->lines[lineno - 1];
4173 if (line->annotated)
4176 line->id = got_object_id_dup(id);
4177 if (line->id == NULL) {
4178 err = got_error_from_errno("got_object_id_dup");
4181 line->annotated = 1;
4183 errcode = pthread_mutex_unlock(&tog_mutex);
4185 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4190 blame_thread(void *arg)
4192 const struct got_error *err, *close_err;
4193 struct tog_blame_thread_args *ta = arg;
4194 struct tog_blame_cb_args *a = ta->cb_args;
4197 err = block_signals_used_by_main_thread();
4201 err = got_blame(ta->path, a->commit_id, ta->repo,
4202 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4203 if (err && err->code == GOT_ERR_CANCELLED)
4206 errcode = pthread_mutex_lock(&tog_mutex);
4208 return (void *)got_error_set_errno(errcode,
4209 "pthread_mutex_lock");
4211 close_err = got_repo_close(ta->repo);
4217 errcode = pthread_mutex_unlock(&tog_mutex);
4218 if (errcode && err == NULL)
4219 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4224 static struct got_object_id *
4225 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4226 int first_displayed_line, int selected_line)
4228 struct tog_blame_line *line;
4233 line = &lines[first_displayed_line - 1 + selected_line - 1];
4234 if (!line->annotated)
4240 static const struct got_error *
4241 stop_blame(struct tog_blame *blame)
4243 const struct got_error *err = NULL;
4246 if (blame->thread) {
4248 errcode = pthread_mutex_unlock(&tog_mutex);
4250 return got_error_set_errno(errcode,
4251 "pthread_mutex_unlock");
4252 errcode = pthread_join(blame->thread, (void **)&err);
4254 return got_error_set_errno(errcode, "pthread_join");
4255 errcode = pthread_mutex_lock(&tog_mutex);
4257 return got_error_set_errno(errcode,
4258 "pthread_mutex_lock");
4259 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4261 blame->thread = 0; //NULL;
4263 if (blame->thread_args.repo) {
4264 const struct got_error *close_err;
4265 close_err = got_repo_close(blame->thread_args.repo);
4268 blame->thread_args.repo = NULL;
4271 if (fclose(blame->f) == EOF && err == NULL)
4272 err = got_error_from_errno("fclose");
4276 for (i = 0; i < blame->nlines; i++)
4277 free(blame->lines[i].id);
4279 blame->lines = NULL;
4281 free(blame->cb_args.commit_id);
4282 blame->cb_args.commit_id = NULL;
4287 static const struct got_error *
4288 cancel_blame_view(void *arg)
4290 const struct got_error *err = NULL;
4294 errcode = pthread_mutex_lock(&tog_mutex);
4296 return got_error_set_errno(errcode,
4297 "pthread_mutex_unlock");
4300 err = got_error(GOT_ERR_CANCELLED);
4302 errcode = pthread_mutex_unlock(&tog_mutex);
4304 return got_error_set_errno(errcode,
4305 "pthread_mutex_lock");
4310 static const struct got_error *
4311 run_blame(struct tog_view *view)
4313 struct tog_blame_view_state *s = &view->state.blame;
4314 struct tog_blame *blame = &s->blame;
4315 const struct got_error *err = NULL;
4316 struct got_blob_object *blob = NULL;
4317 struct got_repository *thread_repo = NULL;
4318 struct got_object_id *obj_id = NULL;
4321 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4326 err = got_object_get_type(&obj_type, s->repo, obj_id);
4330 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4331 err = got_error(GOT_ERR_OBJ_TYPE);
4335 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4338 blame->f = got_opentemp();
4339 if (blame->f == NULL) {
4340 err = got_error_from_errno("got_opentemp");
4343 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4344 &blame->line_offsets, blame->f, blob);
4347 if (blame->nlines == 0) {
4348 s->blame_complete = 1;
4352 /* Don't include \n at EOF in the blame line count. */
4353 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4356 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4357 if (blame->lines == NULL) {
4358 err = got_error_from_errno("calloc");
4362 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4366 blame->cb_args.view = view;
4367 blame->cb_args.lines = blame->lines;
4368 blame->cb_args.nlines = blame->nlines;
4369 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4370 if (blame->cb_args.commit_id == NULL) {
4371 err = got_error_from_errno("got_object_id_dup");
4374 blame->cb_args.quit = &s->done;
4376 blame->thread_args.path = s->path;
4377 blame->thread_args.repo = thread_repo;
4378 blame->thread_args.cb_args = &blame->cb_args;
4379 blame->thread_args.complete = &s->blame_complete;
4380 blame->thread_args.cancel_cb = cancel_blame_view;
4381 blame->thread_args.cancel_arg = &s->done;
4382 s->blame_complete = 0;
4384 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4385 s->first_displayed_line = 1;
4386 s->last_displayed_line = view->nlines;
4387 s->selected_line = 1;
4392 got_object_blob_close(blob);
4399 static const struct got_error *
4400 open_blame_view(struct tog_view *view, char *path,
4401 struct got_object_id *commit_id, struct got_repository *repo)
4403 const struct got_error *err = NULL;
4404 struct tog_blame_view_state *s = &view->state.blame;
4406 STAILQ_INIT(&s->blamed_commits);
4408 s->path = strdup(path);
4409 if (s->path == NULL)
4410 return got_error_from_errno("strdup");
4412 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4418 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4419 s->first_displayed_line = 1;
4420 s->last_displayed_line = view->nlines;
4421 s->selected_line = 1;
4422 s->blame_complete = 0;
4424 s->commit_id = commit_id;
4425 memset(&s->blame, 0, sizeof(s->blame));
4427 STAILQ_INIT(&s->colors);
4428 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4429 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4430 get_color_value("TOG_COLOR_COMMIT"));
4435 view->show = show_blame_view;
4436 view->input = input_blame_view;
4437 view->close = close_blame_view;
4438 view->search_start = search_start_blame_view;
4439 view->search_next = search_next_blame_view;
4441 return run_blame(view);
4444 static const struct got_error *
4445 close_blame_view(struct tog_view *view)
4447 const struct got_error *err = NULL;
4448 struct tog_blame_view_state *s = &view->state.blame;
4450 if (s->blame.thread)
4451 err = stop_blame(&s->blame);
4453 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4454 struct got_object_qid *blamed_commit;
4455 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4456 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4457 got_object_qid_free(blamed_commit);
4461 free_colors(&s->colors);
4466 static const struct got_error *
4467 search_start_blame_view(struct tog_view *view)
4469 struct tog_blame_view_state *s = &view->state.blame;
4471 s->matched_line = 0;
4475 static const struct got_error *
4476 search_next_blame_view(struct tog_view *view)
4478 struct tog_blame_view_state *s = &view->state.blame;
4481 size_t linesize = 0;
4484 if (!view->searching) {
4485 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4489 if (s->matched_line) {
4490 if (view->searching == TOG_SEARCH_FORWARD)
4491 lineno = s->matched_line + 1;
4493 lineno = s->matched_line - 1;
4495 if (view->searching == TOG_SEARCH_FORWARD)
4498 lineno = s->blame.nlines;
4504 if (lineno <= 0 || lineno > s->blame.nlines) {
4505 if (s->matched_line == 0) {
4506 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4510 if (view->searching == TOG_SEARCH_FORWARD)
4513 lineno = s->blame.nlines;
4516 offset = s->blame.line_offsets[lineno - 1];
4517 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4519 return got_error_from_errno("fseeko");
4521 linelen = getline(&line, &linesize, s->blame.f);
4522 if (linelen != -1 &&
4523 match_line(line, &view->regex, 1, &view->regmatch)) {
4524 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4525 s->matched_line = lineno;
4528 if (view->searching == TOG_SEARCH_FORWARD)
4535 if (s->matched_line) {
4536 s->first_displayed_line = s->matched_line;
4537 s->selected_line = 1;
4543 static const struct got_error *
4544 show_blame_view(struct tog_view *view)
4546 const struct got_error *err = NULL;
4547 struct tog_blame_view_state *s = &view->state.blame;
4550 if (s->blame.thread == 0 && !s->blame_complete) {
4551 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4552 &s->blame.thread_args);
4554 return got_error_set_errno(errcode, "pthread_create");
4556 halfdelay(1); /* fast refresh while annotating */
4559 if (s->blame_complete)
4560 halfdelay(10); /* disable fast refresh */
4562 err = draw_blame(view);
4568 static const struct got_error *
4569 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4571 const struct got_error *err = NULL, *thread_err = NULL;
4572 struct tog_view *diff_view;
4573 struct tog_blame_view_state *s = &view->state.blame;
4582 s->selected_line = 1;
4583 s->first_displayed_line = 1;
4587 if (s->blame.nlines < view->nlines - 2) {
4588 s->selected_line = s->blame.nlines;
4589 s->first_displayed_line = 1;
4591 s->selected_line = view->nlines - 2;
4592 s->first_displayed_line = s->blame.nlines -
4599 if (s->selected_line > 1)
4601 else if (s->selected_line == 1 &&
4602 s->first_displayed_line > 1)
4603 s->first_displayed_line--;
4607 if (s->first_displayed_line == 1) {
4608 s->selected_line = 1;
4611 if (s->first_displayed_line > view->nlines - 2)
4612 s->first_displayed_line -=
4615 s->first_displayed_line = 1;
4620 if (s->selected_line < view->nlines - 2 &&
4621 s->first_displayed_line +
4622 s->selected_line <= s->blame.nlines)
4624 else if (s->last_displayed_line <
4626 s->first_displayed_line++;
4630 struct got_object_id *id = NULL;
4631 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4632 s->first_displayed_line, s->selected_line);
4636 struct got_commit_object *commit;
4637 struct got_object_qid *pid;
4638 struct got_object_id *blob_id = NULL;
4640 err = got_object_open_as_commit(&commit,
4645 got_object_commit_get_parent_ids(commit));
4647 got_object_commit_close(commit);
4650 /* Check if path history ends here. */
4651 err = got_object_id_by_path(&blob_id, s->repo,
4654 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4656 got_object_commit_close(commit);
4659 err = got_object_get_type(&obj_type, s->repo,
4662 /* Can't blame non-blob type objects. */
4663 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4664 got_object_commit_close(commit);
4667 err = got_object_qid_alloc(&s->blamed_commit,
4669 got_object_commit_close(commit);
4671 if (got_object_id_cmp(id,
4672 s->blamed_commit->id) == 0)
4674 err = got_object_qid_alloc(&s->blamed_commit,
4680 thread_err = stop_blame(&s->blame);
4684 STAILQ_INSERT_HEAD(&s->blamed_commits,
4685 s->blamed_commit, entry);
4686 err = run_blame(view);
4692 struct got_object_qid *first;
4693 first = STAILQ_FIRST(&s->blamed_commits);
4694 if (!got_object_id_cmp(first->id, s->commit_id))
4697 thread_err = stop_blame(&s->blame);
4701 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4702 got_object_qid_free(s->blamed_commit);
4704 STAILQ_FIRST(&s->blamed_commits);
4705 err = run_blame(view);
4712 struct got_object_id *id = NULL;
4713 struct got_object_qid *pid;
4714 struct got_commit_object *commit = NULL;
4715 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4716 s->first_displayed_line, s->selected_line);
4719 err = got_object_open_as_commit(&commit, s->repo, id);
4723 got_object_commit_get_parent_ids(commit));
4724 if (view_is_parent_view(view))
4725 begin_x = view_split_begin_x(view->begin_x);
4726 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4727 if (diff_view == NULL) {
4728 got_object_commit_close(commit);
4729 err = got_error_from_errno("view_open");
4732 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4733 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4734 got_object_commit_close(commit);
4736 view_close(diff_view);
4740 diff_view->focussed = 1;
4741 if (view_is_parent_view(view)) {
4742 err = view_close_child(view);
4745 view_set_child(view, diff_view);
4746 view->focus_child = 1;
4748 *new_view = diff_view;
4756 if (s->last_displayed_line >= s->blame.nlines &&
4757 s->selected_line >= MIN(s->blame.nlines,
4758 view->nlines - 2)) {
4761 if (s->last_displayed_line >= s->blame.nlines &&
4762 s->selected_line < view->nlines - 2) {
4763 s->selected_line = MIN(s->blame.nlines,
4767 if (s->last_displayed_line + view->nlines - 2
4769 s->first_displayed_line +=
4772 s->first_displayed_line =
4777 if (s->selected_line > view->nlines - 2) {
4778 s->selected_line = MIN(s->blame.nlines,
4785 return thread_err ? thread_err : err;
4788 static const struct got_error *
4789 cmd_blame(int argc, char *argv[])
4791 const struct got_error *error;
4792 struct got_repository *repo = NULL;
4793 struct got_worktree *worktree = NULL;
4794 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4795 char *link_target = NULL;
4796 struct got_object_id *commit_id = NULL;
4797 char *commit_id_str = NULL;
4799 struct tog_view *view;
4801 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4804 commit_id_str = optarg;
4807 repo_path = realpath(optarg, NULL);
4808 if (repo_path == NULL)
4809 return got_error_from_errno2("realpath",
4824 if (repo_path == NULL) {
4825 cwd = getcwd(NULL, 0);
4827 return got_error_from_errno("getcwd");
4828 error = got_worktree_open(&worktree, cwd);
4829 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4833 strdup(got_worktree_get_repo_path(worktree));
4835 repo_path = strdup(cwd);
4836 if (repo_path == NULL) {
4837 error = got_error_from_errno("strdup");
4842 error = got_repo_open(&repo, repo_path, NULL);
4846 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4853 error = apply_unveil(got_repo_get_path(repo), NULL);
4857 error = tog_load_refs(repo, 0);
4861 if (commit_id_str == NULL) {
4862 struct got_reference *head_ref;
4863 error = got_ref_open(&head_ref, repo, worktree ?
4864 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4867 error = got_ref_resolve(&commit_id, repo, head_ref);
4868 got_ref_close(head_ref);
4870 error = got_repo_match_object_id(&commit_id, NULL,
4871 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4876 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4878 error = got_error_from_errno("view_open");
4882 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4887 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4892 /* Release work tree lock. */
4893 got_worktree_close(worktree);
4896 error = view_loop(view);
4904 got_worktree_close(worktree);
4906 const struct got_error *close_err = got_repo_close(repo);
4914 static const struct got_error *
4915 draw_tree_entries(struct tog_view *view, const char *parent_path)
4917 struct tog_tree_view_state *s = &view->state.tree;
4918 const struct got_error *err = NULL;
4919 struct got_tree_entry *te;
4921 struct tog_color *tc;
4922 int width, n, i, nentries;
4923 int limit = view->nlines;
4927 werase(view->window);
4932 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4935 if (view_needs_focus_indication(view))
4936 wstandout(view->window);
4937 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4939 wattr_on(view->window,
4940 COLOR_PAIR(tc->colorpair), NULL);
4941 waddwstr(view->window, wline);
4943 wattr_off(view->window,
4944 COLOR_PAIR(tc->colorpair), NULL);
4945 if (view_needs_focus_indication(view))
4946 wstandend(view->window);
4949 if (width < view->ncols - 1)
4950 waddch(view->window, '\n');
4953 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4956 waddwstr(view->window, wline);
4959 if (width < view->ncols - 1)
4960 waddch(view->window, '\n');
4963 waddch(view->window, '\n');
4967 if (s->first_displayed_entry == NULL) {
4968 te = got_object_tree_get_first_entry(s->tree);
4969 if (s->selected == 0) {
4971 wstandout(view->window);
4972 s->selected_entry = NULL;
4974 waddstr(view->window, " ..\n"); /* parent directory */
4975 if (s->selected == 0 && view->focussed)
4976 wstandend(view->window);
4983 te = s->first_displayed_entry;
4986 nentries = got_object_tree_get_nentries(s->tree);
4987 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4988 char *line = NULL, *id_str = NULL, *link_target = NULL;
4989 const char *modestr = "";
4992 te = got_object_tree_get_entry(s->tree, i);
4993 mode = got_tree_entry_get_mode(te);
4996 err = got_object_id_str(&id_str,
4997 got_tree_entry_get_id(te));
4999 return got_error_from_errno(
5000 "got_object_id_str");
5002 if (got_object_tree_entry_is_submodule(te))
5004 else if (S_ISLNK(mode)) {
5007 err = got_tree_entry_get_symlink_target(&link_target,
5013 for (i = 0; i < strlen(link_target); i++) {
5014 if (!isprint((unsigned char)link_target[i]))
5015 link_target[i] = '?';
5019 else if (S_ISDIR(mode))
5021 else if (mode & S_IXUSR)
5023 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5024 got_tree_entry_get_name(te), modestr,
5025 link_target ? " -> ": "",
5026 link_target ? link_target : "") == -1) {
5029 return got_error_from_errno("asprintf");
5033 err = format_line(&wline, &width, line, view->ncols, 0);
5038 if (n == s->selected) {
5040 wstandout(view->window);
5041 s->selected_entry = te;
5043 tc = match_color(&s->colors, line);
5045 wattr_on(view->window,
5046 COLOR_PAIR(tc->colorpair), NULL);
5047 waddwstr(view->window, wline);
5049 wattr_off(view->window,
5050 COLOR_PAIR(tc->colorpair), NULL);
5051 if (width < view->ncols - 1)
5052 waddch(view->window, '\n');
5053 if (n == s->selected && view->focussed)
5054 wstandend(view->window);
5060 s->last_displayed_entry = te;
5069 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5071 struct got_tree_entry *te;
5072 int isroot = s->tree == s->root;
5075 if (s->first_displayed_entry == NULL)
5078 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5079 while (i++ < maxscroll) {
5082 s->first_displayed_entry = NULL;
5085 s->first_displayed_entry = te;
5086 te = got_tree_entry_get_prev(s->tree, te);
5091 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5093 struct got_tree_entry *next, *last;
5096 if (s->first_displayed_entry)
5097 next = got_tree_entry_get_next(s->tree,
5098 s->first_displayed_entry);
5100 next = got_object_tree_get_first_entry(s->tree);
5102 last = s->last_displayed_entry;
5103 while (next && last && n++ < maxscroll) {
5104 last = got_tree_entry_get_next(s->tree, last);
5106 s->first_displayed_entry = next;
5107 next = got_tree_entry_get_next(s->tree, next);
5112 static const struct got_error *
5113 tree_entry_path(char **path, struct tog_parent_trees *parents,
5114 struct got_tree_entry *te)
5116 const struct got_error *err = NULL;
5117 struct tog_parent_tree *pt;
5118 size_t len = 2; /* for leading slash and NUL */
5120 TAILQ_FOREACH(pt, parents, entry)
5121 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5124 len += strlen(got_tree_entry_get_name(te));
5126 *path = calloc(1, len);
5128 return got_error_from_errno("calloc");
5131 pt = TAILQ_LAST(parents, tog_parent_trees);
5133 const char *name = got_tree_entry_get_name(pt->selected_entry);
5134 if (strlcat(*path, name, len) >= len) {
5135 err = got_error(GOT_ERR_NO_SPACE);
5138 if (strlcat(*path, "/", len) >= len) {
5139 err = got_error(GOT_ERR_NO_SPACE);
5142 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5145 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5146 err = got_error(GOT_ERR_NO_SPACE);
5158 static const struct got_error *
5159 blame_tree_entry(struct tog_view **new_view, int begin_x,
5160 struct got_tree_entry *te, struct tog_parent_trees *parents,
5161 struct got_object_id *commit_id, struct got_repository *repo)
5163 const struct got_error *err = NULL;
5165 struct tog_view *blame_view;
5169 err = tree_entry_path(&path, parents, te);
5173 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5174 if (blame_view == NULL) {
5175 err = got_error_from_errno("view_open");
5179 err = open_blame_view(blame_view, path, commit_id, repo);
5181 if (err->code == GOT_ERR_CANCELLED)
5183 view_close(blame_view);
5185 *new_view = blame_view;
5191 static const struct got_error *
5192 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5193 struct tog_tree_view_state *s)
5195 struct tog_view *log_view;
5196 const struct got_error *err = NULL;
5201 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5202 if (log_view == NULL)
5203 return got_error_from_errno("view_open");
5205 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5209 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5212 view_close(log_view);
5214 *new_view = log_view;
5219 static const struct got_error *
5220 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5221 const char *head_ref_name, struct got_repository *repo)
5223 const struct got_error *err = NULL;
5224 char *commit_id_str = NULL;
5225 struct tog_tree_view_state *s = &view->state.tree;
5226 struct got_commit_object *commit = NULL;
5228 TAILQ_INIT(&s->parents);
5229 STAILQ_INIT(&s->colors);
5231 s->commit_id = got_object_id_dup(commit_id);
5232 if (s->commit_id == NULL)
5233 return got_error_from_errno("got_object_id_dup");
5235 err = got_object_open_as_commit(&commit, repo, commit_id);
5240 * The root is opened here and will be closed when the view is closed.
5241 * Any visited subtrees and their path-wise parents are opened and
5244 err = got_object_open_as_tree(&s->root, repo,
5245 got_object_commit_get_tree_id(commit));
5250 err = got_object_id_str(&commit_id_str, commit_id);
5254 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5255 err = got_error_from_errno("asprintf");
5259 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5260 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5261 if (head_ref_name) {
5262 s->head_ref_name = strdup(head_ref_name);
5263 if (s->head_ref_name == NULL) {
5264 err = got_error_from_errno("strdup");
5270 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5271 err = add_color(&s->colors, "\\$$",
5272 TOG_COLOR_TREE_SUBMODULE,
5273 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5276 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5277 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5280 err = add_color(&s->colors, "/$",
5281 TOG_COLOR_TREE_DIRECTORY,
5282 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5286 err = add_color(&s->colors, "\\*$",
5287 TOG_COLOR_TREE_EXECUTABLE,
5288 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5292 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5293 get_color_value("TOG_COLOR_COMMIT"));
5298 view->show = show_tree_view;
5299 view->input = input_tree_view;
5300 view->close = close_tree_view;
5301 view->search_start = search_start_tree_view;
5302 view->search_next = search_next_tree_view;
5304 free(commit_id_str);
5306 got_object_commit_close(commit);
5308 close_tree_view(view);
5312 static const struct got_error *
5313 close_tree_view(struct tog_view *view)
5315 struct tog_tree_view_state *s = &view->state.tree;
5317 free_colors(&s->colors);
5318 free(s->tree_label);
5319 s->tree_label = NULL;
5321 s->commit_id = NULL;
5322 free(s->head_ref_name);
5323 s->head_ref_name = NULL;
5324 while (!TAILQ_EMPTY(&s->parents)) {
5325 struct tog_parent_tree *parent;
5326 parent = TAILQ_FIRST(&s->parents);
5327 TAILQ_REMOVE(&s->parents, parent, entry);
5328 if (parent->tree != s->root)
5329 got_object_tree_close(parent->tree);
5333 if (s->tree != NULL && s->tree != s->root)
5334 got_object_tree_close(s->tree);
5336 got_object_tree_close(s->root);
5340 static const struct got_error *
5341 search_start_tree_view(struct tog_view *view)
5343 struct tog_tree_view_state *s = &view->state.tree;
5345 s->matched_entry = NULL;
5350 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5352 regmatch_t regmatch;
5354 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5358 static const struct got_error *
5359 search_next_tree_view(struct tog_view *view)
5361 struct tog_tree_view_state *s = &view->state.tree;
5362 struct got_tree_entry *te = NULL;
5364 if (!view->searching) {
5365 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5369 if (s->matched_entry) {
5370 if (view->searching == TOG_SEARCH_FORWARD) {
5371 if (s->selected_entry)
5372 te = got_tree_entry_get_next(s->tree,
5375 te = got_object_tree_get_first_entry(s->tree);
5377 if (s->selected_entry == NULL)
5378 te = got_object_tree_get_last_entry(s->tree);
5380 te = got_tree_entry_get_prev(s->tree,
5384 if (view->searching == TOG_SEARCH_FORWARD)
5385 te = got_object_tree_get_first_entry(s->tree);
5387 te = got_object_tree_get_last_entry(s->tree);
5392 if (s->matched_entry == NULL) {
5393 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5396 if (view->searching == TOG_SEARCH_FORWARD)
5397 te = got_object_tree_get_first_entry(s->tree);
5399 te = got_object_tree_get_last_entry(s->tree);
5402 if (match_tree_entry(te, &view->regex)) {
5403 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5404 s->matched_entry = te;
5408 if (view->searching == TOG_SEARCH_FORWARD)
5409 te = got_tree_entry_get_next(s->tree, te);
5411 te = got_tree_entry_get_prev(s->tree, te);
5414 if (s->matched_entry) {
5415 s->first_displayed_entry = s->matched_entry;
5422 static const struct got_error *
5423 show_tree_view(struct tog_view *view)
5425 const struct got_error *err = NULL;
5426 struct tog_tree_view_state *s = &view->state.tree;
5429 err = tree_entry_path(&parent_path, &s->parents, NULL);
5433 err = draw_tree_entries(view, parent_path);
5440 static const struct got_error *
5441 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5443 const struct got_error *err = NULL;
5444 struct tog_tree_view_state *s = &view->state.tree;
5445 struct tog_view *log_view, *ref_view;
5446 struct got_tree_entry *te;
5451 s->show_ids = !s->show_ids;
5454 if (!s->selected_entry)
5456 if (view_is_parent_view(view))
5457 begin_x = view_split_begin_x(view->begin_x);
5458 err = log_selected_tree_entry(&log_view, begin_x, s);
5460 log_view->focussed = 1;
5461 if (view_is_parent_view(view)) {
5462 err = view_close_child(view);
5465 view_set_child(view, log_view);
5466 view->focus_child = 1;
5468 *new_view = log_view;
5471 if (view_is_parent_view(view))
5472 begin_x = view_split_begin_x(view->begin_x);
5473 ref_view = view_open(view->nlines, view->ncols,
5474 view->begin_y, begin_x, TOG_VIEW_REF);
5475 if (ref_view == NULL)
5476 return got_error_from_errno("view_open");
5477 err = open_ref_view(ref_view, s->repo);
5479 view_close(ref_view);
5483 ref_view->focussed = 1;
5484 if (view_is_parent_view(view)) {
5485 err = view_close_child(view);
5488 view_set_child(view, ref_view);
5489 view->focus_child = 1;
5491 *new_view = ref_view;
5496 if (s->tree == s->root)
5497 s->first_displayed_entry =
5498 got_object_tree_get_first_entry(s->tree);
5500 s->first_displayed_entry = NULL;
5505 te = got_object_tree_get_last_entry(s->tree);
5506 for (n = 0; n < view->nlines - 3; n++) {
5508 if(s->tree != s->root) {
5509 s->first_displayed_entry = NULL;
5514 s->first_displayed_entry = te;
5515 te = got_tree_entry_get_prev(s->tree, te);
5518 s->selected = n - 1;
5523 if (s->selected > 0) {
5527 tree_scroll_up(s, 1);
5531 if (s->tree == s->root) {
5532 if (got_object_tree_get_first_entry(s->tree) ==
5533 s->first_displayed_entry)
5536 if (s->first_displayed_entry == NULL)
5539 tree_scroll_up(s, MAX(0, view->nlines - 3));
5544 if (s->selected < s->ndisplayed - 1) {
5548 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5550 /* can't scroll any further */
5552 tree_scroll_down(s, 1);
5556 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5558 /* can't scroll any further; move cursor down */
5559 if (s->selected < s->ndisplayed - 1)
5560 s->selected = s->ndisplayed - 1;
5563 tree_scroll_down(s, view->nlines - 3);
5568 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5569 struct tog_parent_tree *parent;
5570 /* user selected '..' */
5571 if (s->tree == s->root)
5573 parent = TAILQ_FIRST(&s->parents);
5574 TAILQ_REMOVE(&s->parents, parent,
5576 got_object_tree_close(s->tree);
5577 s->tree = parent->tree;
5578 s->first_displayed_entry =
5579 parent->first_displayed_entry;
5581 parent->selected_entry;
5582 s->selected = parent->selected;
5584 } else if (S_ISDIR(got_tree_entry_get_mode(
5585 s->selected_entry))) {
5586 struct got_tree_object *subtree;
5587 err = got_object_open_as_tree(&subtree, s->repo,
5588 got_tree_entry_get_id(s->selected_entry));
5591 err = tree_view_visit_subtree(s, subtree);
5593 got_object_tree_close(subtree);
5596 } else if (S_ISREG(got_tree_entry_get_mode(
5597 s->selected_entry))) {
5598 struct tog_view *blame_view;
5599 int begin_x = view_is_parent_view(view) ?
5600 view_split_begin_x(view->begin_x) : 0;
5602 err = blame_tree_entry(&blame_view, begin_x,
5603 s->selected_entry, &s->parents,
5604 s->commit_id, s->repo);
5608 blame_view->focussed = 1;
5609 if (view_is_parent_view(view)) {
5610 err = view_close_child(view);
5613 view_set_child(view, blame_view);
5614 view->focus_child = 1;
5616 *new_view = blame_view;
5620 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5621 s->selected = view->nlines - 4;
5634 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5639 static const struct got_error *
5640 cmd_tree(int argc, char *argv[])
5642 const struct got_error *error;
5643 struct got_repository *repo = NULL;
5644 struct got_worktree *worktree = NULL;
5645 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5646 struct got_object_id *commit_id = NULL;
5647 const char *commit_id_arg = NULL;
5649 struct got_reference *ref = NULL;
5650 const char *head_ref_name = NULL;
5652 struct tog_view *view;
5654 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5657 commit_id_arg = optarg;
5660 repo_path = realpath(optarg, NULL);
5661 if (repo_path == NULL)
5662 return got_error_from_errno2("realpath",
5677 if (repo_path == NULL) {
5678 cwd = getcwd(NULL, 0);
5680 return got_error_from_errno("getcwd");
5681 error = got_worktree_open(&worktree, cwd);
5682 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5686 strdup(got_worktree_get_repo_path(worktree));
5688 repo_path = strdup(cwd);
5689 if (repo_path == NULL) {
5690 error = got_error_from_errno("strdup");
5695 error = got_repo_open(&repo, repo_path, NULL);
5699 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5706 error = apply_unveil(got_repo_get_path(repo), NULL);
5710 error = tog_load_refs(repo, 0);
5714 if (commit_id_arg == NULL) {
5715 error = got_repo_match_object_id(&commit_id, &label,
5716 worktree ? got_worktree_get_head_ref_name(worktree) :
5717 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5720 head_ref_name = label;
5722 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5724 head_ref_name = got_ref_get_name(ref);
5725 else if (error->code != GOT_ERR_NOT_REF)
5727 error = got_repo_match_object_id(&commit_id, NULL,
5728 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5733 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5735 error = got_error_from_errno("view_open");
5738 error = open_tree_view(view, commit_id, head_ref_name, repo);
5741 if (!got_path_is_root_dir(in_repo_path)) {
5742 error = tree_view_walk_path(&view->state.tree, commit_id,
5749 /* Release work tree lock. */
5750 got_worktree_close(worktree);
5753 error = view_loop(view);
5762 const struct got_error *close_err = got_repo_close(repo);
5770 static const struct got_error *
5771 ref_view_load_refs(struct tog_ref_view_state *s)
5773 struct got_reflist_entry *sre;
5774 struct tog_reflist_entry *re;
5777 TAILQ_FOREACH(sre, &tog_refs, entry) {
5778 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5781 re = malloc(sizeof(*re));
5783 return got_error_from_errno("malloc");
5785 re->ref = got_ref_dup(sre->ref);
5786 if (re->ref == NULL)
5787 return got_error_from_errno("got_ref_dup");
5788 re->idx = s->nrefs++;
5789 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5792 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5797 ref_view_free_refs(struct tog_ref_view_state *s)
5799 struct tog_reflist_entry *re;
5801 while (!TAILQ_EMPTY(&s->refs)) {
5802 re = TAILQ_FIRST(&s->refs);
5803 TAILQ_REMOVE(&s->refs, re, entry);
5804 got_ref_close(re->ref);
5809 static const struct got_error *
5810 open_ref_view(struct tog_view *view, struct got_repository *repo)
5812 const struct got_error *err = NULL;
5813 struct tog_ref_view_state *s = &view->state.ref;
5815 s->selected_entry = 0;
5818 TAILQ_INIT(&s->refs);
5819 STAILQ_INIT(&s->colors);
5821 err = ref_view_load_refs(s);
5825 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5826 err = add_color(&s->colors, "^refs/heads/",
5827 TOG_COLOR_REFS_HEADS,
5828 get_color_value("TOG_COLOR_REFS_HEADS"));
5832 err = add_color(&s->colors, "^refs/tags/",
5833 TOG_COLOR_REFS_TAGS,
5834 get_color_value("TOG_COLOR_REFS_TAGS"));
5838 err = add_color(&s->colors, "^refs/remotes/",
5839 TOG_COLOR_REFS_REMOTES,
5840 get_color_value("TOG_COLOR_REFS_REMOTES"));
5845 view->show = show_ref_view;
5846 view->input = input_ref_view;
5847 view->close = close_ref_view;
5848 view->search_start = search_start_ref_view;
5849 view->search_next = search_next_ref_view;
5852 free_colors(&s->colors);
5856 static const struct got_error *
5857 close_ref_view(struct tog_view *view)
5859 struct tog_ref_view_state *s = &view->state.ref;
5861 ref_view_free_refs(s);
5862 free_colors(&s->colors);
5867 static const struct got_error *
5868 resolve_reflist_entry(struct got_object_id **commit_id,
5869 struct tog_reflist_entry *re, struct got_repository *repo)
5871 const struct got_error *err = NULL;
5872 struct got_object_id *obj_id;
5873 struct got_tag_object *tag = NULL;
5878 err = got_ref_resolve(&obj_id, repo, re->ref);
5882 err = got_object_get_type(&obj_type, repo, obj_id);
5887 case GOT_OBJ_TYPE_COMMIT:
5888 *commit_id = obj_id;
5890 case GOT_OBJ_TYPE_TAG:
5891 err = got_object_open_as_tag(&tag, repo, obj_id);
5895 err = got_object_get_type(&obj_type, repo,
5896 got_object_tag_get_object_id(tag));
5899 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5900 err = got_error(GOT_ERR_OBJ_TYPE);
5903 *commit_id = got_object_id_dup(
5904 got_object_tag_get_object_id(tag));
5905 if (*commit_id == NULL) {
5906 err = got_error_from_errno("got_object_id_dup");
5911 err = got_error(GOT_ERR_OBJ_TYPE);
5917 got_object_tag_close(tag);
5925 static const struct got_error *
5926 log_ref_entry(struct tog_view **new_view, int begin_x,
5927 struct tog_reflist_entry *re, struct got_repository *repo)
5929 struct tog_view *log_view;
5930 const struct got_error *err = NULL;
5931 struct got_object_id *commit_id = NULL;
5935 err = resolve_reflist_entry(&commit_id, re, repo);
5937 if (err->code != GOT_ERR_OBJ_TYPE)
5943 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5944 if (log_view == NULL) {
5945 err = got_error_from_errno("view_open");
5949 err = open_log_view(log_view, commit_id, repo,
5950 got_ref_get_name(re->ref), "", 0);
5953 view_close(log_view);
5955 *new_view = log_view;
5961 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5963 struct tog_reflist_entry *re;
5966 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5969 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5970 while (i++ < maxscroll) {
5973 s->first_displayed_entry = re;
5974 re = TAILQ_PREV(re, tog_reflist_head, entry);
5979 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5981 struct tog_reflist_entry *next, *last;
5984 if (s->first_displayed_entry)
5985 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5987 next = TAILQ_FIRST(&s->refs);
5989 last = s->last_displayed_entry;
5990 while (next && last && n++ < maxscroll) {
5991 last = TAILQ_NEXT(last, entry);
5993 s->first_displayed_entry = next;
5994 next = TAILQ_NEXT(next, entry);
5999 static const struct got_error *
6000 search_start_ref_view(struct tog_view *view)
6002 struct tog_ref_view_state *s = &view->state.ref;
6004 s->matched_entry = NULL;
6009 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6011 regmatch_t regmatch;
6013 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6017 static const struct got_error *
6018 search_next_ref_view(struct tog_view *view)
6020 struct tog_ref_view_state *s = &view->state.ref;
6021 struct tog_reflist_entry *re = NULL;
6023 if (!view->searching) {
6024 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6028 if (s->matched_entry) {
6029 if (view->searching == TOG_SEARCH_FORWARD) {
6030 if (s->selected_entry)
6031 re = TAILQ_NEXT(s->selected_entry, entry);
6033 re = TAILQ_PREV(s->selected_entry,
6034 tog_reflist_head, entry);
6036 if (s->selected_entry == NULL)
6037 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6039 re = TAILQ_PREV(s->selected_entry,
6040 tog_reflist_head, entry);
6043 if (view->searching == TOG_SEARCH_FORWARD)
6044 re = TAILQ_FIRST(&s->refs);
6046 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6051 if (s->matched_entry == NULL) {
6052 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6055 if (view->searching == TOG_SEARCH_FORWARD)
6056 re = TAILQ_FIRST(&s->refs);
6058 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6061 if (match_reflist_entry(re, &view->regex)) {
6062 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6063 s->matched_entry = re;
6067 if (view->searching == TOG_SEARCH_FORWARD)
6068 re = TAILQ_NEXT(re, entry);
6070 re = TAILQ_PREV(re, tog_reflist_head, entry);
6073 if (s->matched_entry) {
6074 s->first_displayed_entry = s->matched_entry;
6081 static const struct got_error *
6082 show_ref_view(struct tog_view *view)
6084 const struct got_error *err = NULL;
6085 struct tog_ref_view_state *s = &view->state.ref;
6086 struct tog_reflist_entry *re;
6089 struct tog_color *tc;
6091 int limit = view->nlines;
6093 werase(view->window);
6100 re = s->first_displayed_entry;
6102 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6104 return got_error_from_errno("asprintf");
6106 err = format_line(&wline, &width, line, view->ncols, 0);
6111 if (view_needs_focus_indication(view))
6112 wstandout(view->window);
6113 waddwstr(view->window, wline);
6114 if (view_needs_focus_indication(view))
6115 wstandend(view->window);
6120 if (width < view->ncols - 1)
6121 waddch(view->window, '\n');
6126 while (re && limit > 0) {
6129 if (got_ref_is_symbolic(re->ref)) {
6130 if (asprintf(&line, "%s -> %s",
6131 got_ref_get_name(re->ref),
6132 got_ref_get_symref_target(re->ref)) == -1)
6133 return got_error_from_errno("asprintf");
6134 } else if (s->show_ids) {
6135 struct got_object_id *id;
6137 err = got_ref_resolve(&id, s->repo, re->ref);
6140 err = got_object_id_str(&id_str, id);
6145 if (asprintf(&line, "%s: %s",
6146 got_ref_get_name(re->ref), id_str) == -1) {
6147 err = got_error_from_errno("asprintf");
6155 line = strdup(got_ref_get_name(re->ref));
6157 return got_error_from_errno("strdup");
6160 err = format_line(&wline, &width, line, view->ncols, 0);
6165 if (n == s->selected) {
6167 wstandout(view->window);
6168 s->selected_entry = re;
6170 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6172 wattr_on(view->window,
6173 COLOR_PAIR(tc->colorpair), NULL);
6174 waddwstr(view->window, wline);
6176 wattr_off(view->window,
6177 COLOR_PAIR(tc->colorpair), NULL);
6178 if (width < view->ncols - 1)
6179 waddch(view->window, '\n');
6180 if (n == s->selected && view->focussed)
6181 wstandend(view->window);
6187 s->last_displayed_entry = re;
6190 re = TAILQ_NEXT(re, entry);
6197 static const struct got_error *
6198 browse_ref_tree(struct tog_view **new_view, int begin_x,
6199 struct tog_reflist_entry *re, struct got_repository *repo)
6201 const struct got_error *err = NULL;
6202 struct got_object_id *commit_id = NULL;
6203 struct tog_view *tree_view;
6207 err = resolve_reflist_entry(&commit_id, re, repo);
6209 if (err->code != GOT_ERR_OBJ_TYPE)
6216 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6217 if (tree_view == NULL) {
6218 err = got_error_from_errno("view_open");
6222 err = open_tree_view(tree_view, commit_id,
6223 got_ref_get_name(re->ref), repo);
6227 *new_view = tree_view;
6232 static const struct got_error *
6233 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6235 const struct got_error *err = NULL;
6236 struct tog_ref_view_state *s = &view->state.ref;
6237 struct tog_view *log_view, *tree_view;
6238 struct tog_reflist_entry *re;
6243 s->show_ids = !s->show_ids;
6246 s->sort_by_date = !s->sort_by_date;
6247 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6248 got_ref_cmp_by_commit_timestamp_descending :
6249 got_ref_cmp_by_name, s->repo);
6252 got_reflist_object_id_map_free(tog_refs_idmap);
6253 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6254 &tog_refs, s->repo);
6257 ref_view_free_refs(s);
6258 err = ref_view_load_refs(s);
6262 if (!s->selected_entry)
6264 if (view_is_parent_view(view))
6265 begin_x = view_split_begin_x(view->begin_x);
6266 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6269 log_view->focussed = 1;
6270 if (view_is_parent_view(view)) {
6271 err = view_close_child(view);
6274 view_set_child(view, log_view);
6275 view->focus_child = 1;
6277 *new_view = log_view;
6280 if (!s->selected_entry)
6282 if (view_is_parent_view(view))
6283 begin_x = view_split_begin_x(view->begin_x);
6284 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6286 if (err || tree_view == NULL)
6289 tree_view->focussed = 1;
6290 if (view_is_parent_view(view)) {
6291 err = view_close_child(view);
6294 view_set_child(view, tree_view);
6295 view->focus_child = 1;
6297 *new_view = tree_view;
6302 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6307 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6308 for (n = 0; n < view->nlines - 1; n++) {
6311 s->first_displayed_entry = re;
6312 re = TAILQ_PREV(re, tog_reflist_head, entry);
6315 s->selected = n - 1;
6320 if (s->selected > 0) {
6324 ref_scroll_up(s, 1);
6328 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6330 ref_scroll_up(s, MAX(0, view->nlines - 1));
6335 if (s->selected < s->ndisplayed - 1) {
6339 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6340 /* can't scroll any further */
6342 ref_scroll_down(s, 1);
6346 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6347 /* can't scroll any further; move cursor down */
6348 if (s->selected < s->ndisplayed - 1)
6349 s->selected = s->ndisplayed - 1;
6352 ref_scroll_down(s, view->nlines - 1);
6356 err = tog_load_refs(s->repo, s->sort_by_date);
6359 ref_view_free_refs(s);
6360 err = ref_view_load_refs(s);
6363 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6364 s->selected = view->nlines - 2;
6377 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6382 static const struct got_error *
6383 cmd_ref(int argc, char *argv[])
6385 const struct got_error *error;
6386 struct got_repository *repo = NULL;
6387 struct got_worktree *worktree = NULL;
6388 char *cwd = NULL, *repo_path = NULL;
6390 struct tog_view *view;
6392 while ((ch = getopt(argc, argv, "r:")) != -1) {
6395 repo_path = realpath(optarg, NULL);
6396 if (repo_path == NULL)
6397 return got_error_from_errno2("realpath",
6412 if (repo_path == NULL) {
6413 cwd = getcwd(NULL, 0);
6415 return got_error_from_errno("getcwd");
6416 error = got_worktree_open(&worktree, cwd);
6417 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6421 strdup(got_worktree_get_repo_path(worktree));
6423 repo_path = strdup(cwd);
6424 if (repo_path == NULL) {
6425 error = got_error_from_errno("strdup");
6430 error = got_repo_open(&repo, repo_path, NULL);
6436 error = apply_unveil(got_repo_get_path(repo), NULL);
6440 error = tog_load_refs(repo, 0);
6444 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6446 error = got_error_from_errno("view_open");
6450 error = open_ref_view(view, repo);
6455 /* Release work tree lock. */
6456 got_worktree_close(worktree);
6459 error = view_loop(view);
6464 const struct got_error *close_err = got_repo_close(repo);
6473 list_commands(FILE *fp)
6477 fprintf(fp, "commands:");
6478 for (i = 0; i < nitems(tog_commands); i++) {
6479 struct tog_cmd *cmd = &tog_commands[i];
6480 fprintf(fp, " %s", cmd->name);
6486 usage(int hflag, int status)
6488 FILE *fp = (status == 0) ? stdout : stderr;
6490 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6493 fprintf(fp, "lazy usage: %s path\n", getprogname());
6500 make_argv(int argc, ...)
6508 argv = calloc(argc, sizeof(char *));
6511 for (i = 0; i < argc; i++) {
6512 argv[i] = strdup(va_arg(ap, char *));
6513 if (argv[i] == NULL)
6522 * Try to convert 'tog path' into a 'tog log path' command.
6523 * The user could simply have mistyped the command rather than knowingly
6524 * provided a path. So check whether argv[0] can in fact be resolved
6525 * to a path in the HEAD commit and print a special error if not.
6526 * This hack is for mpi@ <3
6528 static const struct got_error *
6529 tog_log_with_path(int argc, char *argv[])
6531 const struct got_error *error = NULL, *close_err;
6532 struct tog_cmd *cmd = NULL;
6533 struct got_repository *repo = NULL;
6534 struct got_worktree *worktree = NULL;
6535 struct got_object_id *commit_id = NULL, *id = NULL;
6536 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6537 char *commit_id_str = NULL, **cmd_argv = NULL;
6539 cwd = getcwd(NULL, 0);
6541 return got_error_from_errno("getcwd");
6543 error = got_worktree_open(&worktree, cwd);
6544 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6548 repo_path = strdup(got_worktree_get_repo_path(worktree));
6550 repo_path = strdup(cwd);
6551 if (repo_path == NULL) {
6552 error = got_error_from_errno("strdup");
6556 error = got_repo_open(&repo, repo_path, NULL);
6560 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6565 error = tog_load_refs(repo, 0);
6568 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6569 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6570 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6575 got_worktree_close(worktree);
6579 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6581 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6583 fprintf(stderr, "%s: '%s' is no known command or path\n",
6584 getprogname(), argv[0]);
6589 close_err = got_repo_close(repo);
6594 error = got_object_id_str(&commit_id_str, commit_id);
6598 cmd = &tog_commands[0]; /* log */
6600 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6601 error = cmd->cmd_main(argc, cmd_argv);
6604 close_err = got_repo_close(repo);
6609 got_worktree_close(worktree);
6611 free(commit_id_str);
6618 for (i = 0; i < argc; i++)
6627 main(int argc, char *argv[])
6629 const struct got_error *error = NULL;
6630 struct tog_cmd *cmd = NULL;
6631 int ch, hflag = 0, Vflag = 0;
6632 char **cmd_argv = NULL;
6633 static struct option longopts[] = {
6634 { "version", no_argument, NULL, 'V' },
6638 setlocale(LC_CTYPE, "");
6640 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6660 got_version_print_str();
6665 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6673 /* Build an argument vector which runs a default command. */
6674 cmd = &tog_commands[0];
6676 cmd_argv = make_argv(argc, cmd->name);
6680 /* Did the user specify a command? */
6681 for (i = 0; i < nitems(tog_commands); i++) {
6682 if (strncmp(tog_commands[i].name, argv[0],
6683 strlen(argv[0])) == 0) {
6684 cmd = &tog_commands[i];
6693 /* No command specified; try log with a path */
6694 error = tog_log_with_path(argc, argv);
6699 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6706 for (i = 0; i < argc; i++)
6711 if (error && error->code != GOT_ERR_CANCELLED)
6712 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);