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 entry = s->selected_entry;
2210 if (entry == NULL) {
2211 if (s->thread_args.log_complete ||
2212 view->searching == TOG_SEARCH_BACKWARD) {
2213 view->search_next_done =
2214 (s->matched_entry == NULL ?
2215 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2216 s->search_entry = NULL;
2220 * Poke the log thread for more commits and return,
2221 * allowing the main loop to make progress. Search
2222 * will resume at s->search_entry once we come back.
2224 s->thread_args.commits_needed++;
2225 return trigger_log_thread(view, 0);
2228 err = match_commit(&have_match, entry->id, entry->commit,
2233 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2234 s->matched_entry = entry;
2238 s->search_entry = entry;
2239 if (view->searching == TOG_SEARCH_FORWARD)
2240 entry = TAILQ_NEXT(entry, entry);
2242 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2245 if (s->matched_entry) {
2246 int cur = s->selected_entry->idx;
2247 while (cur < s->matched_entry->idx) {
2248 err = input_log_view(NULL, view, KEY_DOWN);
2253 while (cur > s->matched_entry->idx) {
2254 err = input_log_view(NULL, view, KEY_UP);
2261 s->search_entry = NULL;
2266 static const struct got_error *
2267 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2268 struct got_repository *repo, const char *head_ref_name,
2269 const char *in_repo_path, int log_branches)
2271 const struct got_error *err = NULL;
2272 struct tog_log_view_state *s = &view->state.log;
2273 struct got_repository *thread_repo = NULL;
2274 struct got_commit_graph *thread_graph = NULL;
2277 if (in_repo_path != s->in_repo_path) {
2278 free(s->in_repo_path);
2279 s->in_repo_path = strdup(in_repo_path);
2280 if (s->in_repo_path == NULL)
2281 return got_error_from_errno("strdup");
2284 /* The commit queue only contains commits being displayed. */
2285 TAILQ_INIT(&s->commits.head);
2286 s->commits.ncommits = 0;
2289 if (head_ref_name) {
2290 s->head_ref_name = strdup(head_ref_name);
2291 if (s->head_ref_name == NULL) {
2292 err = got_error_from_errno("strdup");
2296 s->start_id = got_object_id_dup(start_id);
2297 if (s->start_id == NULL) {
2298 err = got_error_from_errno("got_object_id_dup");
2301 s->log_branches = log_branches;
2303 STAILQ_INIT(&s->colors);
2304 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2305 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2306 get_color_value("TOG_COLOR_COMMIT"));
2309 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2310 get_color_value("TOG_COLOR_AUTHOR"));
2312 free_colors(&s->colors);
2315 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2316 get_color_value("TOG_COLOR_DATE"));
2318 free_colors(&s->colors);
2323 view->show = show_log_view;
2324 view->input = input_log_view;
2325 view->close = close_log_view;
2326 view->search_start = search_start_log_view;
2327 view->search_next = search_next_log_view;
2329 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2332 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2336 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2337 s->repo, NULL, NULL);
2341 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2343 err = got_error_set_errno(errcode, "pthread_cond_init");
2346 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2348 err = got_error_set_errno(errcode, "pthread_cond_init");
2352 s->thread_args.commits_needed = view->nlines;
2353 s->thread_args.graph = thread_graph;
2354 s->thread_args.commits = &s->commits;
2355 s->thread_args.in_repo_path = s->in_repo_path;
2356 s->thread_args.start_id = s->start_id;
2357 s->thread_args.repo = thread_repo;
2358 s->thread_args.log_complete = 0;
2359 s->thread_args.quit = &s->quit;
2360 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2361 s->thread_args.selected_entry = &s->selected_entry;
2362 s->thread_args.searching = &view->searching;
2363 s->thread_args.search_next_done = &view->search_next_done;
2364 s->thread_args.regex = &view->regex;
2367 close_log_view(view);
2371 static const struct got_error *
2372 show_log_view(struct tog_view *view)
2374 const struct got_error *err;
2375 struct tog_log_view_state *s = &view->state.log;
2377 if (s->thread == 0) { //NULL) {
2378 int errcode = pthread_create(&s->thread, NULL, log_thread,
2381 return got_error_set_errno(errcode, "pthread_create");
2382 if (s->thread_args.commits_needed > 0) {
2383 err = trigger_log_thread(view, 1);
2389 return draw_commits(view);
2392 static const struct got_error *
2393 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2395 const struct got_error *err = NULL;
2396 struct tog_log_view_state *s = &view->state.log;
2397 struct tog_view *diff_view = NULL, *tree_view = NULL;
2398 struct tog_view *ref_view = NULL;
2399 struct commit_queue_entry *entry;
2402 if (s->thread_args.load_all) {
2403 if (ch == KEY_BACKSPACE)
2404 s->thread_args.load_all = 0;
2405 else if (s->thread_args.log_complete) {
2406 s->thread_args.load_all = 0;
2407 log_scroll_down(view, s->commits.ncommits);
2408 s->selected = MIN(view->nlines - 2,
2409 s->commits.ncommits - 1);
2424 if (s->first_displayed_entry == NULL)
2426 if (s->selected > 0)
2429 log_scroll_up(s, 1);
2435 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2440 if (s->first_displayed_entry == NULL)
2442 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2445 log_scroll_up(s, view->nlines - 1);
2453 if (s->first_displayed_entry == NULL)
2455 if (s->selected < MIN(view->nlines - 2,
2456 s->commits.ncommits - 1))
2459 err = log_scroll_down(view, 1);
2467 /* We don't know yet how many commits, so we're forced to
2468 * traverse them all. */
2469 if (!s->thread_args.log_complete) {
2470 s->thread_args.load_all = 1;
2471 return trigger_log_thread(view, 0);
2475 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2476 for (n = 0; n < view->nlines - 1; n++) {
2479 s->first_displayed_entry = entry;
2480 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2483 s->selected = n - 1;
2489 struct commit_queue_entry *first;
2490 first = s->first_displayed_entry;
2493 err = log_scroll_down(view, view->nlines - 1);
2496 if (first == s->first_displayed_entry &&
2497 s->selected < MIN(view->nlines - 2,
2498 s->commits.ncommits - 1)) {
2499 /* can't scroll further down */
2500 s->selected = MIN(view->nlines - 2,
2501 s->commits.ncommits - 1);
2507 if (s->selected > view->nlines - 2)
2508 s->selected = view->nlines - 2;
2509 if (s->selected > s->commits.ncommits - 1)
2510 s->selected = s->commits.ncommits - 1;
2512 if (s->commits.ncommits < view->nlines - 1 &&
2513 !s->thread_args.log_complete) {
2514 s->thread_args.commits_needed += (view->nlines - 1) -
2515 s->commits.ncommits;
2516 err = trigger_log_thread(view, 1);
2522 if (s->selected_entry == NULL)
2524 if (view_is_parent_view(view))
2525 begin_x = view_split_begin_x(view->begin_x);
2526 err = open_diff_view_for_commit(&diff_view, begin_x,
2527 s->selected_entry->commit, s->selected_entry->id,
2532 diff_view->focussed = 1;
2533 if (view_is_parent_view(view)) {
2534 err = view_close_child(view);
2537 view_set_child(view, diff_view);
2538 view->focus_child = 1;
2540 *new_view = diff_view;
2543 if (s->selected_entry == NULL)
2545 if (view_is_parent_view(view))
2546 begin_x = view_split_begin_x(view->begin_x);
2547 err = browse_commit_tree(&tree_view, begin_x,
2548 s->selected_entry, s->in_repo_path, s->head_ref_name,
2553 tree_view->focussed = 1;
2554 if (view_is_parent_view(view)) {
2555 err = view_close_child(view);
2558 view_set_child(view, tree_view);
2559 view->focus_child = 1;
2561 *new_view = tree_view;
2566 if (ch == KEY_BACKSPACE &&
2567 got_path_is_root_dir(s->in_repo_path))
2569 err = stop_log_thread(s);
2572 if (ch == KEY_BACKSPACE) {
2574 err = got_path_dirname(&parent_path, s->in_repo_path);
2577 free(s->in_repo_path);
2578 s->in_repo_path = parent_path;
2579 s->thread_args.in_repo_path = s->in_repo_path;
2580 } else if (ch == CTRL('l')) {
2581 struct got_object_id *start_id;
2582 err = got_repo_match_object_id(&start_id, NULL,
2583 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2584 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2588 s->start_id = start_id;
2589 s->thread_args.start_id = s->start_id;
2591 s->log_branches = !s->log_branches;
2593 err = got_repo_open(&s->thread_args.repo,
2594 got_repo_get_path(s->repo), NULL);
2598 err = tog_load_refs(s->repo, 0);
2601 err = got_commit_graph_open(&s->thread_args.graph,
2602 s->in_repo_path, !s->log_branches);
2605 err = got_commit_graph_iter_start(s->thread_args.graph,
2606 s->start_id, s->repo, NULL, NULL);
2609 free_commits(&s->commits);
2610 s->first_displayed_entry = NULL;
2611 s->last_displayed_entry = NULL;
2612 s->selected_entry = NULL;
2614 s->thread_args.log_complete = 0;
2616 s->thread_args.commits_needed = view->nlines;
2619 if (view_is_parent_view(view))
2620 begin_x = view_split_begin_x(view->begin_x);
2621 ref_view = view_open(view->nlines, view->ncols,
2622 view->begin_y, begin_x, TOG_VIEW_REF);
2623 if (ref_view == NULL)
2624 return got_error_from_errno("view_open");
2625 err = open_ref_view(ref_view, s->repo);
2627 view_close(ref_view);
2631 ref_view->focussed = 1;
2632 if (view_is_parent_view(view)) {
2633 err = view_close_child(view);
2636 view_set_child(view, ref_view);
2637 view->focus_child = 1;
2639 *new_view = ref_view;
2648 static const struct got_error *
2649 apply_unveil(const char *repo_path, const char *worktree_path)
2651 const struct got_error *error;
2654 if (unveil("gmon.out", "rwc") != 0)
2655 return got_error_from_errno2("unveil", "gmon.out");
2657 if (repo_path && unveil(repo_path, "r") != 0)
2658 return got_error_from_errno2("unveil", repo_path);
2660 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2661 return got_error_from_errno2("unveil", worktree_path);
2663 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2664 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2666 error = got_privsep_unveil_exec_helpers();
2670 if (unveil(NULL, NULL) != 0)
2671 return got_error_from_errno("unveil");
2681 halfdelay(1); /* Do fast refresh while initial view is loading. */
2684 intrflush(stdscr, FALSE);
2685 keypad(stdscr, TRUE);
2687 if (getenv("TOG_COLORS") != NULL) {
2689 use_default_colors();
2691 signal(SIGWINCH, tog_sigwinch);
2692 signal(SIGPIPE, tog_sigpipe);
2693 signal(SIGCONT, tog_sigcont);
2696 static const struct got_error *
2697 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2698 struct got_repository *repo, struct got_worktree *worktree)
2700 const struct got_error *err = NULL;
2703 *in_repo_path = strdup("/");
2704 if (*in_repo_path == NULL)
2705 return got_error_from_errno("strdup");
2710 const char *prefix = got_worktree_get_path_prefix(worktree);
2713 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2716 if (asprintf(in_repo_path, "%s%s%s", prefix,
2717 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2719 err = got_error_from_errno("asprintf");
2720 *in_repo_path = NULL;
2724 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2729 static const struct got_error *
2730 cmd_log(int argc, char *argv[])
2732 const struct got_error *error;
2733 struct got_repository *repo = NULL;
2734 struct got_worktree *worktree = NULL;
2735 struct got_object_id *start_id = NULL;
2736 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2737 char *start_commit = NULL, *label = NULL;
2738 struct got_reference *ref = NULL;
2739 const char *head_ref_name = NULL;
2740 int ch, log_branches = 0;
2741 struct tog_view *view;
2743 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2749 start_commit = optarg;
2752 repo_path = realpath(optarg, NULL);
2753 if (repo_path == NULL)
2754 return got_error_from_errno2("realpath",
2769 if (repo_path == NULL) {
2770 cwd = getcwd(NULL, 0);
2772 return got_error_from_errno("getcwd");
2773 error = got_worktree_open(&worktree, cwd);
2774 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2778 strdup(got_worktree_get_repo_path(worktree));
2780 repo_path = strdup(cwd);
2781 if (repo_path == NULL) {
2782 error = got_error_from_errno("strdup");
2787 error = got_repo_open(&repo, repo_path, NULL);
2791 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2798 error = apply_unveil(got_repo_get_path(repo),
2799 worktree ? got_worktree_get_root_path(worktree) : NULL);
2803 /* already loaded by tog_log_with_path()? */
2804 if (TAILQ_EMPTY(&tog_refs)) {
2805 error = tog_load_refs(repo, 0);
2810 if (start_commit == NULL) {
2811 error = got_repo_match_object_id(&start_id, &label,
2812 worktree ? got_worktree_get_head_ref_name(worktree) :
2813 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2816 head_ref_name = label;
2818 error = got_ref_open(&ref, repo, start_commit, 0);
2820 head_ref_name = got_ref_get_name(ref);
2821 else if (error->code != GOT_ERR_NOT_REF)
2823 error = got_repo_match_object_id(&start_id, NULL,
2824 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2829 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2831 error = got_error_from_errno("view_open");
2834 error = open_log_view(view, start_id, repo, head_ref_name,
2835 in_repo_path, log_branches);
2839 /* Release work tree lock. */
2840 got_worktree_close(worktree);
2843 error = view_loop(view);
2853 const struct got_error *close_err = got_repo_close(repo);
2858 got_worktree_close(worktree);
2867 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2868 "[-w] object1 object2\n", getprogname());
2873 match_line(const char *line, regex_t *regex, size_t nmatch,
2874 regmatch_t *regmatch)
2876 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2880 match_color(struct tog_colors *colors, const char *line)
2882 struct tog_color *tc = NULL;
2884 STAILQ_FOREACH(tc, colors, entry) {
2885 if (match_line(line, &tc->regex, 0, NULL))
2892 static const struct got_error *
2893 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2894 WINDOW *window, regmatch_t *regmatch)
2896 const struct got_error *err = NULL;
2903 s = strndup(line, regmatch->rm_so);
2905 return got_error_from_errno("strndup");
2907 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2912 waddwstr(window, wline);
2919 s = strndup(line + regmatch->rm_so,
2920 regmatch->rm_eo - regmatch->rm_so);
2922 err = got_error_from_errno("strndup");
2926 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2931 wattr_on(window, A_STANDOUT, NULL);
2932 waddwstr(window, wline);
2933 wattr_off(window, A_STANDOUT, NULL);
2940 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2941 err = format_line(&wline, &width,
2942 line + regmatch->rm_eo, wlimit, col_tab_align);
2945 waddwstr(window, wline);
2953 static const struct got_error *
2954 draw_file(struct tog_view *view, const char *header)
2956 struct tog_diff_view_state *s = &view->state.diff;
2957 regmatch_t *regmatch = &view->regmatch;
2958 const struct got_error *err;
2961 size_t linesize = 0;
2963 struct tog_color *tc;
2966 int max_lines = view->nlines;
2967 int nlines = s->nlines;
2970 line_offset = s->line_offsets[s->first_displayed_line - 1];
2971 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2972 return got_error_from_errno("fseek");
2974 werase(view->window);
2977 if (asprintf(&line, "[%d/%d] %s",
2978 s->first_displayed_line - 1 + s->selected_line, nlines,
2980 return got_error_from_errno("asprintf");
2981 err = format_line(&wline, &width, line, view->ncols, 0);
2986 if (view_needs_focus_indication(view))
2987 wstandout(view->window);
2988 waddwstr(view->window, wline);
2991 if (view_needs_focus_indication(view))
2992 wstandend(view->window);
2993 if (width <= view->ncols - 1)
2994 waddch(view->window, '\n');
3003 while (max_lines > 0 && nprinted < max_lines) {
3004 linelen = getline(&line, &linesize, s->f);
3005 if (linelen == -1) {
3011 return got_ferror(s->f, GOT_ERR_IO);
3014 tc = match_color(&s->colors, line);
3016 wattr_on(view->window,
3017 COLOR_PAIR(tc->colorpair), NULL);
3018 if (s->first_displayed_line + nprinted == s->matched_line &&
3019 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3020 err = add_matched_line(&width, line, view->ncols, 0,
3021 view->window, regmatch);
3027 err = format_line(&wline, &width, line, view->ncols, 0);
3032 waddwstr(view->window, wline);
3037 wattr_off(view->window,
3038 COLOR_PAIR(tc->colorpair), NULL);
3039 if (width <= view->ncols - 1)
3040 waddch(view->window, '\n');
3045 s->last_displayed_line = s->first_displayed_line +
3048 s->last_displayed_line = s->first_displayed_line;
3053 while (nprinted < view->nlines) {
3054 waddch(view->window, '\n');
3058 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3063 wstandout(view->window);
3064 waddwstr(view->window, wline);
3067 wstandend(view->window);
3074 get_datestr(time_t *time, char *datebuf)
3076 struct tm mytm, *tm;
3079 tm = gmtime_r(time, &mytm);
3082 s = asctime_r(tm, datebuf);
3085 p = strchr(s, '\n');
3091 static const struct got_error *
3092 get_changed_paths(struct got_pathlist_head *paths,
3093 struct got_commit_object *commit, struct got_repository *repo)
3095 const struct got_error *err = NULL;
3096 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3097 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3098 struct got_object_qid *qid;
3100 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3102 struct got_commit_object *pcommit;
3103 err = got_object_open_as_commit(&pcommit, repo,
3108 tree_id1 = got_object_id_dup(
3109 got_object_commit_get_tree_id(pcommit));
3110 if (tree_id1 == NULL) {
3111 got_object_commit_close(pcommit);
3112 return got_error_from_errno("got_object_id_dup");
3114 got_object_commit_close(pcommit);
3119 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3124 tree_id2 = got_object_commit_get_tree_id(commit);
3125 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3129 err = got_diff_tree(tree1, tree2, "", "", repo,
3130 got_diff_tree_collect_changed_paths, paths, 0);
3133 got_object_tree_close(tree1);
3135 got_object_tree_close(tree2);
3140 static const struct got_error *
3141 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3145 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3147 return got_error_from_errno("reallocarray");
3149 (*line_offsets)[*nlines] = off;
3154 static const struct got_error *
3155 write_commit_info(off_t **line_offsets, size_t *nlines,
3156 struct got_object_id *commit_id, struct got_reflist_head *refs,
3157 struct got_repository *repo, FILE *outfile)
3159 const struct got_error *err = NULL;
3160 char datebuf[26], *datestr;
3161 struct got_commit_object *commit;
3162 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3163 time_t committer_time;
3164 const char *author, *committer;
3165 char *refs_str = NULL;
3166 struct got_pathlist_head changed_paths;
3167 struct got_pathlist_entry *pe;
3171 TAILQ_INIT(&changed_paths);
3174 err = build_refs_str(&refs_str, refs, commit_id, repo);
3179 err = got_object_open_as_commit(&commit, repo, commit_id);
3183 err = got_object_id_str(&id_str, commit_id);
3185 err = got_error_from_errno("got_object_id_str");
3189 err = add_line_offset(line_offsets, nlines, 0);
3193 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3194 refs_str ? refs_str : "", refs_str ? ")" : "");
3196 err = got_error_from_errno("fprintf");
3200 err = add_line_offset(line_offsets, nlines, outoff);
3204 n = fprintf(outfile, "from: %s\n",
3205 got_object_commit_get_author(commit));
3207 err = got_error_from_errno("fprintf");
3211 err = add_line_offset(line_offsets, nlines, outoff);
3215 committer_time = got_object_commit_get_committer_time(commit);
3216 datestr = get_datestr(&committer_time, datebuf);
3218 n = fprintf(outfile, "date: %s UTC\n", datestr);
3220 err = got_error_from_errno("fprintf");
3224 err = add_line_offset(line_offsets, nlines, outoff);
3228 author = got_object_commit_get_author(commit);
3229 committer = got_object_commit_get_committer(commit);
3230 if (strcmp(author, committer) != 0) {
3231 n = fprintf(outfile, "via: %s\n", committer);
3233 err = got_error_from_errno("fprintf");
3237 err = add_line_offset(line_offsets, nlines, outoff);
3241 if (got_object_commit_get_nparents(commit) > 1) {
3242 const struct got_object_id_queue *parent_ids;
3243 struct got_object_qid *qid;
3245 parent_ids = got_object_commit_get_parent_ids(commit);
3246 STAILQ_FOREACH(qid, parent_ids, entry) {
3247 err = got_object_id_str(&id_str, qid->id);
3250 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3252 err = got_error_from_errno("fprintf");
3256 err = add_line_offset(line_offsets, nlines, outoff);
3264 err = got_object_commit_get_logmsg(&logmsg, commit);
3268 while ((line = strsep(&s, "\n")) != NULL) {
3269 n = fprintf(outfile, "%s\n", line);
3271 err = got_error_from_errno("fprintf");
3275 err = add_line_offset(line_offsets, nlines, outoff);
3280 err = get_changed_paths(&changed_paths, commit, repo);
3283 TAILQ_FOREACH(pe, &changed_paths, entry) {
3284 struct got_diff_changed_path *cp = pe->data;
3285 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3287 err = got_error_from_errno("fprintf");
3291 err = add_line_offset(line_offsets, nlines, outoff);
3294 free((char *)pe->path);
3298 fputc('\n', outfile);
3300 err = add_line_offset(line_offsets, nlines, outoff);
3302 got_pathlist_free(&changed_paths);
3306 got_object_commit_close(commit);
3308 free(*line_offsets);
3309 *line_offsets = NULL;
3315 static const struct got_error *
3316 create_diff(struct tog_diff_view_state *s)
3318 const struct got_error *err = NULL;
3322 free(s->line_offsets);
3323 s->line_offsets = malloc(sizeof(off_t));
3324 if (s->line_offsets == NULL)
3325 return got_error_from_errno("malloc");
3330 err = got_error_from_errno("got_opentemp");
3333 if (s->f && fclose(s->f) == EOF) {
3334 err = got_error_from_errno("fclose");
3340 err = got_object_get_type(&obj_type, s->repo, s->id1);
3342 err = got_object_get_type(&obj_type, s->repo, s->id2);
3347 case GOT_OBJ_TYPE_BLOB:
3348 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3349 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3350 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3352 case GOT_OBJ_TYPE_TREE:
3353 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3354 s->id1, s->id2, NULL, "", "", s->diff_context,
3355 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3357 case GOT_OBJ_TYPE_COMMIT: {
3358 const struct got_object_id_queue *parent_ids;
3359 struct got_object_qid *pid;
3360 struct got_commit_object *commit2;
3361 struct got_reflist_head *refs;
3363 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3366 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3367 /* Show commit info if we're diffing to a parent/root commit. */
3368 if (s->id1 == NULL) {
3369 err = write_commit_info(&s->line_offsets, &s->nlines,
3370 s->id2, refs, s->repo, s->f);
3374 parent_ids = got_object_commit_get_parent_ids(commit2);
3375 STAILQ_FOREACH(pid, parent_ids, entry) {
3376 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3377 err = write_commit_info(
3378 &s->line_offsets, &s->nlines,
3379 s->id2, refs, s->repo, s->f);
3386 got_object_commit_close(commit2);
3388 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3389 s->id1, s->id2, NULL, s->diff_context, s->ignore_whitespace,
3390 s->force_text_diff, s->repo, s->f);
3394 err = got_error(GOT_ERR_OBJ_TYPE);
3400 if (s->f && fflush(s->f) != 0 && err == NULL)
3401 err = got_error_from_errno("fflush");
3406 diff_view_indicate_progress(struct tog_view *view)
3408 mvwaddstr(view->window, 0, 0, "diffing...");
3413 static const struct got_error *
3414 search_start_diff_view(struct tog_view *view)
3416 struct tog_diff_view_state *s = &view->state.diff;
3418 s->matched_line = 0;
3422 static const struct got_error *
3423 search_next_diff_view(struct tog_view *view)
3425 struct tog_diff_view_state *s = &view->state.diff;
3428 size_t linesize = 0;
3431 if (!view->searching) {
3432 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3436 if (s->matched_line) {
3437 if (view->searching == TOG_SEARCH_FORWARD)
3438 lineno = s->matched_line + 1;
3440 lineno = s->matched_line - 1;
3442 lineno = s->first_displayed_line;
3447 if (lineno <= 0 || lineno > s->nlines) {
3448 if (s->matched_line == 0) {
3449 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3453 if (view->searching == TOG_SEARCH_FORWARD)
3459 offset = s->line_offsets[lineno - 1];
3460 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3462 return got_error_from_errno("fseeko");
3464 linelen = getline(&line, &linesize, s->f);
3465 if (linelen != -1 &&
3466 match_line(line, &view->regex, 1, &view->regmatch)) {
3467 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3468 s->matched_line = lineno;
3471 if (view->searching == TOG_SEARCH_FORWARD)
3478 if (s->matched_line) {
3479 s->first_displayed_line = s->matched_line;
3480 s->selected_line = 1;
3486 static const struct got_error *
3487 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3488 struct got_object_id *id2, const char *label1, const char *label2,
3489 int diff_context, int ignore_whitespace, int force_text_diff,
3490 struct tog_view *log_view, struct got_repository *repo)
3492 const struct got_error *err;
3493 struct tog_diff_view_state *s = &view->state.diff;
3495 if (id1 != NULL && id2 != NULL) {
3497 err = got_object_get_type(&type1, repo, id1);
3500 err = got_object_get_type(&type2, repo, id2);
3505 return got_error(GOT_ERR_OBJ_TYPE);
3507 s->first_displayed_line = 1;
3508 s->last_displayed_line = view->nlines;
3509 s->selected_line = 1;
3517 s->id1 = got_object_id_dup(id1);
3519 return got_error_from_errno("got_object_id_dup");
3523 s->id2 = got_object_id_dup(id2);
3524 if (s->id2 == NULL) {
3527 return got_error_from_errno("got_object_id_dup");
3530 s->first_displayed_line = 1;
3531 s->last_displayed_line = view->nlines;
3532 s->diff_context = diff_context;
3533 s->ignore_whitespace = ignore_whitespace;
3534 s->force_text_diff = force_text_diff;
3535 s->log_view = log_view;
3538 STAILQ_INIT(&s->colors);
3539 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3540 err = add_color(&s->colors,
3541 "^-", TOG_COLOR_DIFF_MINUS,
3542 get_color_value("TOG_COLOR_DIFF_MINUS"));
3545 err = add_color(&s->colors, "^\\+",
3546 TOG_COLOR_DIFF_PLUS,
3547 get_color_value("TOG_COLOR_DIFF_PLUS"));
3549 free_colors(&s->colors);
3552 err = add_color(&s->colors,
3553 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3554 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3556 free_colors(&s->colors);
3560 err = add_color(&s->colors,
3561 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3562 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3563 get_color_value("TOG_COLOR_DIFF_META"));
3565 free_colors(&s->colors);
3569 err = add_color(&s->colors,
3570 "^(from|via): ", TOG_COLOR_AUTHOR,
3571 get_color_value("TOG_COLOR_AUTHOR"));
3573 free_colors(&s->colors);
3577 err = add_color(&s->colors,
3578 "^date: ", TOG_COLOR_DATE,
3579 get_color_value("TOG_COLOR_DATE"));
3581 free_colors(&s->colors);
3586 if (log_view && view_is_splitscreen(view))
3587 show_log_view(log_view); /* draw vborder */
3588 diff_view_indicate_progress(view);
3590 s->line_offsets = NULL;
3592 err = create_diff(s);
3598 free_colors(&s->colors);
3602 view->show = show_diff_view;
3603 view->input = input_diff_view;
3604 view->close = close_diff_view;
3605 view->search_start = search_start_diff_view;
3606 view->search_next = search_next_diff_view;
3611 static const struct got_error *
3612 close_diff_view(struct tog_view *view)
3614 const struct got_error *err = NULL;
3615 struct tog_diff_view_state *s = &view->state.diff;
3621 if (s->f && fclose(s->f) == EOF)
3622 err = got_error_from_errno("fclose");
3623 free_colors(&s->colors);
3624 free(s->line_offsets);
3625 s->line_offsets = NULL;
3630 static const struct got_error *
3631 show_diff_view(struct tog_view *view)
3633 const struct got_error *err;
3634 struct tog_diff_view_state *s = &view->state.diff;
3635 char *id_str1 = NULL, *id_str2, *header;
3636 const char *label1, *label2;
3639 err = got_object_id_str(&id_str1, s->id1);
3642 label1 = s->label1 ? : id_str1;
3644 label1 = "/dev/null";
3646 err = got_object_id_str(&id_str2, s->id2);
3649 label2 = s->label2 ? : id_str2;
3651 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3652 err = got_error_from_errno("asprintf");
3660 err = draw_file(view, header);
3665 static const struct got_error *
3666 set_selected_commit(struct tog_diff_view_state *s,
3667 struct commit_queue_entry *entry)
3669 const struct got_error *err;
3670 const struct got_object_id_queue *parent_ids;
3671 struct got_commit_object *selected_commit;
3672 struct got_object_qid *pid;
3675 s->id2 = got_object_id_dup(entry->id);
3677 return got_error_from_errno("got_object_id_dup");
3679 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3682 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3684 pid = STAILQ_FIRST(parent_ids);
3685 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3686 got_object_commit_close(selected_commit);
3690 static const struct got_error *
3691 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3693 const struct got_error *err = NULL;
3694 struct tog_diff_view_state *s = &view->state.diff;
3695 struct tog_log_view_state *ls;
3696 struct commit_queue_entry *old_selected_entry;
3698 size_t linesize = 0;
3706 s->force_text_diff = !s->force_text_diff;
3708 s->ignore_whitespace = !s->ignore_whitespace;
3709 wclear(view->window);
3710 s->first_displayed_line = 1;
3711 s->last_displayed_line = view->nlines;
3712 s->matched_line = 0;
3713 diff_view_indicate_progress(view);
3714 err = create_diff(s);
3718 s->first_displayed_line = 1;
3725 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3731 if (s->first_displayed_line > 1)
3732 s->first_displayed_line--;
3736 if (s->first_displayed_line == 1)
3739 while (i++ < view->nlines - 1 &&
3740 s->first_displayed_line > 1)
3741 s->first_displayed_line--;
3747 s->first_displayed_line++;
3755 while (!s->eof && i++ < view->nlines - 1) {
3756 linelen = getline(&line, &linesize, s->f);
3757 s->first_displayed_line++;
3758 if (linelen == -1) {
3762 err = got_ferror(s->f, GOT_ERR_IO);
3769 if (s->diff_context > 0) {
3771 s->matched_line = 0;
3772 diff_view_indicate_progress(view);
3773 err = create_diff(s);
3774 if (s->first_displayed_line + view->nlines - 1 >
3776 s->first_displayed_line = 1;
3777 s->last_displayed_line = view->nlines;
3782 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3784 s->matched_line = 0;
3785 diff_view_indicate_progress(view);
3786 err = create_diff(s);
3791 if (s->log_view == NULL)
3793 ls = &s->log_view->state.log;
3794 old_selected_entry = ls->selected_entry;
3796 err = input_log_view(NULL, s->log_view, KEY_UP);
3800 if (old_selected_entry == ls->selected_entry)
3803 err = set_selected_commit(s, ls->selected_entry);
3807 s->first_displayed_line = 1;
3808 s->last_displayed_line = view->nlines;
3809 s->matched_line = 0;
3811 diff_view_indicate_progress(view);
3812 err = create_diff(s);
3816 if (s->log_view == NULL)
3818 ls = &s->log_view->state.log;
3819 old_selected_entry = ls->selected_entry;
3821 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3825 if (old_selected_entry == ls->selected_entry)
3828 err = set_selected_commit(s, ls->selected_entry);
3832 s->first_displayed_line = 1;
3833 s->last_displayed_line = view->nlines;
3834 s->matched_line = 0;
3836 diff_view_indicate_progress(view);
3837 err = create_diff(s);
3846 static const struct got_error *
3847 cmd_diff(int argc, char *argv[])
3849 const struct got_error *error = NULL;
3850 struct got_repository *repo = NULL;
3851 struct got_worktree *worktree = NULL;
3852 struct got_object_id *id1 = NULL, *id2 = NULL;
3853 char *repo_path = NULL, *cwd = NULL;
3854 char *id_str1 = NULL, *id_str2 = NULL;
3855 char *label1 = NULL, *label2 = NULL;
3856 int diff_context = 3, ignore_whitespace = 0;
3857 int ch, force_text_diff = 0;
3859 struct tog_view *view;
3861 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3864 force_text_diff = 1;
3867 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3870 err(1, "-C option %s", errstr);
3873 repo_path = realpath(optarg, NULL);
3874 if (repo_path == NULL)
3875 return got_error_from_errno2("realpath",
3877 got_path_strip_trailing_slashes(repo_path);
3880 ignore_whitespace = 1;
3892 usage_diff(); /* TODO show local worktree changes */
3893 } else if (argc == 2) {
3899 if (repo_path == NULL) {
3900 cwd = getcwd(NULL, 0);
3902 return got_error_from_errno("getcwd");
3903 error = got_worktree_open(&worktree, cwd);
3904 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3908 strdup(got_worktree_get_repo_path(worktree));
3910 repo_path = strdup(cwd);
3911 if (repo_path == NULL) {
3912 error = got_error_from_errno("strdup");
3917 error = got_repo_open(&repo, repo_path, NULL);
3923 error = apply_unveil(got_repo_get_path(repo), NULL);
3927 error = tog_load_refs(repo, 0);
3931 error = got_repo_match_object_id(&id1, &label1, id_str1,
3932 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3936 error = got_repo_match_object_id(&id2, &label2, id_str2,
3937 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3941 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3943 error = got_error_from_errno("view_open");
3946 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3947 ignore_whitespace, force_text_diff, NULL, repo);
3950 error = view_loop(view);
3957 const struct got_error *close_err = got_repo_close(repo);
3962 got_worktree_close(worktree);
3971 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3976 struct tog_blame_line {
3978 struct got_object_id *id;
3981 static const struct got_error *
3982 draw_blame(struct tog_view *view)
3984 struct tog_blame_view_state *s = &view->state.blame;
3985 struct tog_blame *blame = &s->blame;
3986 regmatch_t *regmatch = &view->regmatch;
3987 const struct got_error *err;
3988 int lineno = 0, nprinted = 0;
3990 size_t linesize = 0;
3994 struct tog_blame_line *blame_line;
3995 struct got_object_id *prev_id = NULL;
3997 struct tog_color *tc;
3999 err = got_object_id_str(&id_str, s->blamed_commit->id);
4004 werase(view->window);
4006 if (asprintf(&line, "commit %s", id_str) == -1) {
4007 err = got_error_from_errno("asprintf");
4012 err = format_line(&wline, &width, line, view->ncols, 0);
4017 if (view_needs_focus_indication(view))
4018 wstandout(view->window);
4019 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4021 wattr_on(view->window,
4022 COLOR_PAIR(tc->colorpair), NULL);
4023 waddwstr(view->window, wline);
4025 wattr_off(view->window,
4026 COLOR_PAIR(tc->colorpair), NULL);
4027 if (view_needs_focus_indication(view))
4028 wstandend(view->window);
4031 if (width < view->ncols - 1)
4032 waddch(view->window, '\n');
4034 if (asprintf(&line, "[%d/%d] %s%s",
4035 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4036 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4038 return got_error_from_errno("asprintf");
4041 err = format_line(&wline, &width, line, view->ncols, 0);
4046 waddwstr(view->window, wline);
4049 if (width < view->ncols - 1)
4050 waddch(view->window, '\n');
4053 while (nprinted < view->nlines - 2) {
4054 linelen = getline(&line, &linesize, blame->f);
4055 if (linelen == -1) {
4056 if (feof(blame->f)) {
4061 return got_ferror(blame->f, GOT_ERR_IO);
4063 if (++lineno < s->first_displayed_line)
4066 if (view->focussed && nprinted == s->selected_line - 1)
4067 wstandout(view->window);
4069 if (blame->nlines > 0) {
4070 blame_line = &blame->lines[lineno - 1];
4071 if (blame_line->annotated && prev_id &&
4072 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4074 nprinted == s->selected_line - 1)) {
4075 waddstr(view->window, " ");
4076 } else if (blame_line->annotated) {
4078 err = got_object_id_str(&id_str, blame_line->id);
4083 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4085 wattr_on(view->window,
4086 COLOR_PAIR(tc->colorpair), NULL);
4087 wprintw(view->window, "%.8s", id_str);
4089 wattr_off(view->window,
4090 COLOR_PAIR(tc->colorpair), NULL);
4092 prev_id = blame_line->id;
4094 waddstr(view->window, "........");
4098 waddstr(view->window, "........");
4102 if (view->focussed && nprinted == s->selected_line - 1)
4103 wstandend(view->window);
4104 waddstr(view->window, " ");
4106 if (view->ncols <= 9) {
4108 wline = wcsdup(L"");
4109 if (wline == NULL) {
4110 err = got_error_from_errno("wcsdup");
4114 } else if (s->first_displayed_line + nprinted ==
4116 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4117 err = add_matched_line(&width, line, view->ncols - 9, 9,
4118 view->window, regmatch);
4125 err = format_line(&wline, &width, line,
4126 view->ncols - 9, 9);
4127 waddwstr(view->window, wline);
4133 if (width <= view->ncols - 1)
4134 waddch(view->window, '\n');
4135 if (++nprinted == 1)
4136 s->first_displayed_line = lineno;
4139 s->last_displayed_line = lineno;
4146 static const struct got_error *
4147 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4149 const struct got_error *err = NULL;
4150 struct tog_blame_cb_args *a = arg;
4151 struct tog_blame_line *line;
4154 if (nlines != a->nlines ||
4155 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4156 return got_error(GOT_ERR_RANGE);
4158 errcode = pthread_mutex_lock(&tog_mutex);
4160 return got_error_set_errno(errcode, "pthread_mutex_lock");
4162 if (*a->quit) { /* user has quit the blame view */
4163 err = got_error(GOT_ERR_ITER_COMPLETED);
4168 goto done; /* no change in this commit */
4170 line = &a->lines[lineno - 1];
4171 if (line->annotated)
4174 line->id = got_object_id_dup(id);
4175 if (line->id == NULL) {
4176 err = got_error_from_errno("got_object_id_dup");
4179 line->annotated = 1;
4181 errcode = pthread_mutex_unlock(&tog_mutex);
4183 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4188 blame_thread(void *arg)
4190 const struct got_error *err, *close_err;
4191 struct tog_blame_thread_args *ta = arg;
4192 struct tog_blame_cb_args *a = ta->cb_args;
4195 err = block_signals_used_by_main_thread();
4199 err = got_blame(ta->path, a->commit_id, ta->repo,
4200 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4201 if (err && err->code == GOT_ERR_CANCELLED)
4204 errcode = pthread_mutex_lock(&tog_mutex);
4206 return (void *)got_error_set_errno(errcode,
4207 "pthread_mutex_lock");
4209 close_err = got_repo_close(ta->repo);
4215 errcode = pthread_mutex_unlock(&tog_mutex);
4216 if (errcode && err == NULL)
4217 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4222 static struct got_object_id *
4223 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4224 int first_displayed_line, int selected_line)
4226 struct tog_blame_line *line;
4231 line = &lines[first_displayed_line - 1 + selected_line - 1];
4232 if (!line->annotated)
4238 static const struct got_error *
4239 stop_blame(struct tog_blame *blame)
4241 const struct got_error *err = NULL;
4244 if (blame->thread) {
4246 errcode = pthread_mutex_unlock(&tog_mutex);
4248 return got_error_set_errno(errcode,
4249 "pthread_mutex_unlock");
4250 errcode = pthread_join(blame->thread, (void **)&err);
4252 return got_error_set_errno(errcode, "pthread_join");
4253 errcode = pthread_mutex_lock(&tog_mutex);
4255 return got_error_set_errno(errcode,
4256 "pthread_mutex_lock");
4257 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4259 blame->thread = 0; //NULL;
4261 if (blame->thread_args.repo) {
4262 const struct got_error *close_err;
4263 close_err = got_repo_close(blame->thread_args.repo);
4266 blame->thread_args.repo = NULL;
4269 if (fclose(blame->f) == EOF && err == NULL)
4270 err = got_error_from_errno("fclose");
4274 for (i = 0; i < blame->nlines; i++)
4275 free(blame->lines[i].id);
4277 blame->lines = NULL;
4279 free(blame->cb_args.commit_id);
4280 blame->cb_args.commit_id = NULL;
4285 static const struct got_error *
4286 cancel_blame_view(void *arg)
4288 const struct got_error *err = NULL;
4292 errcode = pthread_mutex_lock(&tog_mutex);
4294 return got_error_set_errno(errcode,
4295 "pthread_mutex_unlock");
4298 err = got_error(GOT_ERR_CANCELLED);
4300 errcode = pthread_mutex_unlock(&tog_mutex);
4302 return got_error_set_errno(errcode,
4303 "pthread_mutex_lock");
4308 static const struct got_error *
4309 run_blame(struct tog_view *view)
4311 struct tog_blame_view_state *s = &view->state.blame;
4312 struct tog_blame *blame = &s->blame;
4313 const struct got_error *err = NULL;
4314 struct got_blob_object *blob = NULL;
4315 struct got_repository *thread_repo = NULL;
4316 struct got_object_id *obj_id = NULL;
4319 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4324 err = got_object_get_type(&obj_type, s->repo, obj_id);
4328 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4329 err = got_error(GOT_ERR_OBJ_TYPE);
4333 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4336 blame->f = got_opentemp();
4337 if (blame->f == NULL) {
4338 err = got_error_from_errno("got_opentemp");
4341 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4342 &blame->line_offsets, blame->f, blob);
4345 if (blame->nlines == 0) {
4346 s->blame_complete = 1;
4350 /* Don't include \n at EOF in the blame line count. */
4351 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4354 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4355 if (blame->lines == NULL) {
4356 err = got_error_from_errno("calloc");
4360 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4364 blame->cb_args.view = view;
4365 blame->cb_args.lines = blame->lines;
4366 blame->cb_args.nlines = blame->nlines;
4367 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4368 if (blame->cb_args.commit_id == NULL) {
4369 err = got_error_from_errno("got_object_id_dup");
4372 blame->cb_args.quit = &s->done;
4374 blame->thread_args.path = s->path;
4375 blame->thread_args.repo = thread_repo;
4376 blame->thread_args.cb_args = &blame->cb_args;
4377 blame->thread_args.complete = &s->blame_complete;
4378 blame->thread_args.cancel_cb = cancel_blame_view;
4379 blame->thread_args.cancel_arg = &s->done;
4380 s->blame_complete = 0;
4382 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4383 s->first_displayed_line = 1;
4384 s->last_displayed_line = view->nlines;
4385 s->selected_line = 1;
4387 s->matched_line = 0;
4391 got_object_blob_close(blob);
4398 static const struct got_error *
4399 open_blame_view(struct tog_view *view, char *path,
4400 struct got_object_id *commit_id, struct got_repository *repo)
4402 const struct got_error *err = NULL;
4403 struct tog_blame_view_state *s = &view->state.blame;
4405 STAILQ_INIT(&s->blamed_commits);
4407 s->path = strdup(path);
4408 if (s->path == NULL)
4409 return got_error_from_errno("strdup");
4411 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4417 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4418 s->first_displayed_line = 1;
4419 s->last_displayed_line = view->nlines;
4420 s->selected_line = 1;
4421 s->blame_complete = 0;
4423 s->commit_id = commit_id;
4424 memset(&s->blame, 0, sizeof(s->blame));
4426 STAILQ_INIT(&s->colors);
4427 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4428 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4429 get_color_value("TOG_COLOR_COMMIT"));
4434 view->show = show_blame_view;
4435 view->input = input_blame_view;
4436 view->close = close_blame_view;
4437 view->search_start = search_start_blame_view;
4438 view->search_next = search_next_blame_view;
4440 return run_blame(view);
4443 static const struct got_error *
4444 close_blame_view(struct tog_view *view)
4446 const struct got_error *err = NULL;
4447 struct tog_blame_view_state *s = &view->state.blame;
4449 if (s->blame.thread)
4450 err = stop_blame(&s->blame);
4452 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4453 struct got_object_qid *blamed_commit;
4454 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4455 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4456 got_object_qid_free(blamed_commit);
4460 free_colors(&s->colors);
4465 static const struct got_error *
4466 search_start_blame_view(struct tog_view *view)
4468 struct tog_blame_view_state *s = &view->state.blame;
4470 s->matched_line = 0;
4474 static const struct got_error *
4475 search_next_blame_view(struct tog_view *view)
4477 struct tog_blame_view_state *s = &view->state.blame;
4480 size_t linesize = 0;
4483 if (!view->searching) {
4484 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4488 if (s->matched_line) {
4489 if (view->searching == TOG_SEARCH_FORWARD)
4490 lineno = s->matched_line + 1;
4492 lineno = s->matched_line - 1;
4494 lineno = s->first_displayed_line - 1 + s->selected_line;
4499 if (lineno <= 0 || lineno > s->blame.nlines) {
4500 if (s->matched_line == 0) {
4501 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4505 if (view->searching == TOG_SEARCH_FORWARD)
4508 lineno = s->blame.nlines;
4511 offset = s->blame.line_offsets[lineno - 1];
4512 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4514 return got_error_from_errno("fseeko");
4516 linelen = getline(&line, &linesize, s->blame.f);
4517 if (linelen != -1 &&
4518 match_line(line, &view->regex, 1, &view->regmatch)) {
4519 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4520 s->matched_line = lineno;
4523 if (view->searching == TOG_SEARCH_FORWARD)
4530 if (s->matched_line) {
4531 s->first_displayed_line = s->matched_line;
4532 s->selected_line = 1;
4538 static const struct got_error *
4539 show_blame_view(struct tog_view *view)
4541 const struct got_error *err = NULL;
4542 struct tog_blame_view_state *s = &view->state.blame;
4545 if (s->blame.thread == 0 && !s->blame_complete) {
4546 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4547 &s->blame.thread_args);
4549 return got_error_set_errno(errcode, "pthread_create");
4551 halfdelay(1); /* fast refresh while annotating */
4554 if (s->blame_complete)
4555 halfdelay(10); /* disable fast refresh */
4557 err = draw_blame(view);
4563 static const struct got_error *
4564 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4566 const struct got_error *err = NULL, *thread_err = NULL;
4567 struct tog_view *diff_view;
4568 struct tog_blame_view_state *s = &view->state.blame;
4577 s->selected_line = 1;
4578 s->first_displayed_line = 1;
4582 if (s->blame.nlines < view->nlines - 2) {
4583 s->selected_line = s->blame.nlines;
4584 s->first_displayed_line = 1;
4586 s->selected_line = view->nlines - 2;
4587 s->first_displayed_line = s->blame.nlines -
4594 if (s->selected_line > 1)
4596 else if (s->selected_line == 1 &&
4597 s->first_displayed_line > 1)
4598 s->first_displayed_line--;
4602 if (s->first_displayed_line == 1) {
4603 s->selected_line = 1;
4606 if (s->first_displayed_line > view->nlines - 2)
4607 s->first_displayed_line -=
4610 s->first_displayed_line = 1;
4615 if (s->selected_line < view->nlines - 2 &&
4616 s->first_displayed_line +
4617 s->selected_line <= s->blame.nlines)
4619 else if (s->last_displayed_line <
4621 s->first_displayed_line++;
4625 struct got_object_id *id = NULL;
4626 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4627 s->first_displayed_line, s->selected_line);
4631 struct got_commit_object *commit;
4632 struct got_object_qid *pid;
4633 struct got_object_id *blob_id = NULL;
4635 err = got_object_open_as_commit(&commit,
4640 got_object_commit_get_parent_ids(commit));
4642 got_object_commit_close(commit);
4645 /* Check if path history ends here. */
4646 err = got_object_id_by_path(&blob_id, s->repo,
4649 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4651 got_object_commit_close(commit);
4654 err = got_object_get_type(&obj_type, s->repo,
4657 /* Can't blame non-blob type objects. */
4658 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4659 got_object_commit_close(commit);
4662 err = got_object_qid_alloc(&s->blamed_commit,
4664 got_object_commit_close(commit);
4666 if (got_object_id_cmp(id,
4667 s->blamed_commit->id) == 0)
4669 err = got_object_qid_alloc(&s->blamed_commit,
4675 thread_err = stop_blame(&s->blame);
4679 STAILQ_INSERT_HEAD(&s->blamed_commits,
4680 s->blamed_commit, entry);
4681 err = run_blame(view);
4687 struct got_object_qid *first;
4688 first = STAILQ_FIRST(&s->blamed_commits);
4689 if (!got_object_id_cmp(first->id, s->commit_id))
4692 thread_err = stop_blame(&s->blame);
4696 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4697 got_object_qid_free(s->blamed_commit);
4699 STAILQ_FIRST(&s->blamed_commits);
4700 err = run_blame(view);
4707 struct got_object_id *id = NULL;
4708 struct got_object_qid *pid;
4709 struct got_commit_object *commit = NULL;
4710 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4711 s->first_displayed_line, s->selected_line);
4714 err = got_object_open_as_commit(&commit, s->repo, id);
4718 got_object_commit_get_parent_ids(commit));
4719 if (view_is_parent_view(view))
4720 begin_x = view_split_begin_x(view->begin_x);
4721 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4722 if (diff_view == NULL) {
4723 got_object_commit_close(commit);
4724 err = got_error_from_errno("view_open");
4727 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4728 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4729 got_object_commit_close(commit);
4731 view_close(diff_view);
4735 diff_view->focussed = 1;
4736 if (view_is_parent_view(view)) {
4737 err = view_close_child(view);
4740 view_set_child(view, diff_view);
4741 view->focus_child = 1;
4743 *new_view = diff_view;
4751 if (s->last_displayed_line >= s->blame.nlines &&
4752 s->selected_line >= MIN(s->blame.nlines,
4753 view->nlines - 2)) {
4756 if (s->last_displayed_line >= s->blame.nlines &&
4757 s->selected_line < view->nlines - 2) {
4758 s->selected_line = MIN(s->blame.nlines,
4762 if (s->last_displayed_line + view->nlines - 2
4764 s->first_displayed_line +=
4767 s->first_displayed_line =
4772 if (s->selected_line > view->nlines - 2) {
4773 s->selected_line = MIN(s->blame.nlines,
4780 return thread_err ? thread_err : err;
4783 static const struct got_error *
4784 cmd_blame(int argc, char *argv[])
4786 const struct got_error *error;
4787 struct got_repository *repo = NULL;
4788 struct got_worktree *worktree = NULL;
4789 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4790 char *link_target = NULL;
4791 struct got_object_id *commit_id = NULL;
4792 char *commit_id_str = NULL;
4794 struct tog_view *view;
4796 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4799 commit_id_str = optarg;
4802 repo_path = realpath(optarg, NULL);
4803 if (repo_path == NULL)
4804 return got_error_from_errno2("realpath",
4819 if (repo_path == NULL) {
4820 cwd = getcwd(NULL, 0);
4822 return got_error_from_errno("getcwd");
4823 error = got_worktree_open(&worktree, cwd);
4824 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4828 strdup(got_worktree_get_repo_path(worktree));
4830 repo_path = strdup(cwd);
4831 if (repo_path == NULL) {
4832 error = got_error_from_errno("strdup");
4837 error = got_repo_open(&repo, repo_path, NULL);
4841 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4848 error = apply_unveil(got_repo_get_path(repo), NULL);
4852 error = tog_load_refs(repo, 0);
4856 if (commit_id_str == NULL) {
4857 struct got_reference *head_ref;
4858 error = got_ref_open(&head_ref, repo, worktree ?
4859 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4862 error = got_ref_resolve(&commit_id, repo, head_ref);
4863 got_ref_close(head_ref);
4865 error = got_repo_match_object_id(&commit_id, NULL,
4866 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4871 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4873 error = got_error_from_errno("view_open");
4877 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4882 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4887 /* Release work tree lock. */
4888 got_worktree_close(worktree);
4891 error = view_loop(view);
4899 got_worktree_close(worktree);
4901 const struct got_error *close_err = got_repo_close(repo);
4909 static const struct got_error *
4910 draw_tree_entries(struct tog_view *view, const char *parent_path)
4912 struct tog_tree_view_state *s = &view->state.tree;
4913 const struct got_error *err = NULL;
4914 struct got_tree_entry *te;
4916 struct tog_color *tc;
4917 int width, n, i, nentries;
4918 int limit = view->nlines;
4922 werase(view->window);
4927 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4930 if (view_needs_focus_indication(view))
4931 wstandout(view->window);
4932 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4934 wattr_on(view->window,
4935 COLOR_PAIR(tc->colorpair), NULL);
4936 waddwstr(view->window, wline);
4938 wattr_off(view->window,
4939 COLOR_PAIR(tc->colorpair), NULL);
4940 if (view_needs_focus_indication(view))
4941 wstandend(view->window);
4944 if (width < view->ncols - 1)
4945 waddch(view->window, '\n');
4948 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4951 waddwstr(view->window, wline);
4954 if (width < view->ncols - 1)
4955 waddch(view->window, '\n');
4958 waddch(view->window, '\n');
4962 if (s->first_displayed_entry == NULL) {
4963 te = got_object_tree_get_first_entry(s->tree);
4964 if (s->selected == 0) {
4966 wstandout(view->window);
4967 s->selected_entry = NULL;
4969 waddstr(view->window, " ..\n"); /* parent directory */
4970 if (s->selected == 0 && view->focussed)
4971 wstandend(view->window);
4978 te = s->first_displayed_entry;
4981 nentries = got_object_tree_get_nentries(s->tree);
4982 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4983 char *line = NULL, *id_str = NULL, *link_target = NULL;
4984 const char *modestr = "";
4987 te = got_object_tree_get_entry(s->tree, i);
4988 mode = got_tree_entry_get_mode(te);
4991 err = got_object_id_str(&id_str,
4992 got_tree_entry_get_id(te));
4994 return got_error_from_errno(
4995 "got_object_id_str");
4997 if (got_object_tree_entry_is_submodule(te))
4999 else if (S_ISLNK(mode)) {
5002 err = got_tree_entry_get_symlink_target(&link_target,
5008 for (i = 0; i < strlen(link_target); i++) {
5009 if (!isprint((unsigned char)link_target[i]))
5010 link_target[i] = '?';
5014 else if (S_ISDIR(mode))
5016 else if (mode & S_IXUSR)
5018 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5019 got_tree_entry_get_name(te), modestr,
5020 link_target ? " -> ": "",
5021 link_target ? link_target : "") == -1) {
5024 return got_error_from_errno("asprintf");
5028 err = format_line(&wline, &width, line, view->ncols, 0);
5033 if (n == s->selected) {
5035 wstandout(view->window);
5036 s->selected_entry = te;
5038 tc = match_color(&s->colors, line);
5040 wattr_on(view->window,
5041 COLOR_PAIR(tc->colorpair), NULL);
5042 waddwstr(view->window, wline);
5044 wattr_off(view->window,
5045 COLOR_PAIR(tc->colorpair), NULL);
5046 if (width < view->ncols - 1)
5047 waddch(view->window, '\n');
5048 if (n == s->selected && view->focussed)
5049 wstandend(view->window);
5055 s->last_displayed_entry = te;
5064 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5066 struct got_tree_entry *te;
5067 int isroot = s->tree == s->root;
5070 if (s->first_displayed_entry == NULL)
5073 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5074 while (i++ < maxscroll) {
5077 s->first_displayed_entry = NULL;
5080 s->first_displayed_entry = te;
5081 te = got_tree_entry_get_prev(s->tree, te);
5086 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5088 struct got_tree_entry *next, *last;
5091 if (s->first_displayed_entry)
5092 next = got_tree_entry_get_next(s->tree,
5093 s->first_displayed_entry);
5095 next = got_object_tree_get_first_entry(s->tree);
5097 last = s->last_displayed_entry;
5098 while (next && last && n++ < maxscroll) {
5099 last = got_tree_entry_get_next(s->tree, last);
5101 s->first_displayed_entry = next;
5102 next = got_tree_entry_get_next(s->tree, next);
5107 static const struct got_error *
5108 tree_entry_path(char **path, struct tog_parent_trees *parents,
5109 struct got_tree_entry *te)
5111 const struct got_error *err = NULL;
5112 struct tog_parent_tree *pt;
5113 size_t len = 2; /* for leading slash and NUL */
5115 TAILQ_FOREACH(pt, parents, entry)
5116 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5119 len += strlen(got_tree_entry_get_name(te));
5121 *path = calloc(1, len);
5123 return got_error_from_errno("calloc");
5126 pt = TAILQ_LAST(parents, tog_parent_trees);
5128 const char *name = got_tree_entry_get_name(pt->selected_entry);
5129 if (strlcat(*path, name, len) >= len) {
5130 err = got_error(GOT_ERR_NO_SPACE);
5133 if (strlcat(*path, "/", len) >= len) {
5134 err = got_error(GOT_ERR_NO_SPACE);
5137 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5140 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5141 err = got_error(GOT_ERR_NO_SPACE);
5153 static const struct got_error *
5154 blame_tree_entry(struct tog_view **new_view, int begin_x,
5155 struct got_tree_entry *te, struct tog_parent_trees *parents,
5156 struct got_object_id *commit_id, struct got_repository *repo)
5158 const struct got_error *err = NULL;
5160 struct tog_view *blame_view;
5164 err = tree_entry_path(&path, parents, te);
5168 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5169 if (blame_view == NULL) {
5170 err = got_error_from_errno("view_open");
5174 err = open_blame_view(blame_view, path, commit_id, repo);
5176 if (err->code == GOT_ERR_CANCELLED)
5178 view_close(blame_view);
5180 *new_view = blame_view;
5186 static const struct got_error *
5187 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5188 struct tog_tree_view_state *s)
5190 struct tog_view *log_view;
5191 const struct got_error *err = NULL;
5196 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5197 if (log_view == NULL)
5198 return got_error_from_errno("view_open");
5200 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5204 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5207 view_close(log_view);
5209 *new_view = log_view;
5214 static const struct got_error *
5215 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5216 const char *head_ref_name, struct got_repository *repo)
5218 const struct got_error *err = NULL;
5219 char *commit_id_str = NULL;
5220 struct tog_tree_view_state *s = &view->state.tree;
5221 struct got_commit_object *commit = NULL;
5223 TAILQ_INIT(&s->parents);
5224 STAILQ_INIT(&s->colors);
5226 s->commit_id = got_object_id_dup(commit_id);
5227 if (s->commit_id == NULL)
5228 return got_error_from_errno("got_object_id_dup");
5230 err = got_object_open_as_commit(&commit, repo, commit_id);
5235 * The root is opened here and will be closed when the view is closed.
5236 * Any visited subtrees and their path-wise parents are opened and
5239 err = got_object_open_as_tree(&s->root, repo,
5240 got_object_commit_get_tree_id(commit));
5245 err = got_object_id_str(&commit_id_str, commit_id);
5249 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5250 err = got_error_from_errno("asprintf");
5254 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5255 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5256 if (head_ref_name) {
5257 s->head_ref_name = strdup(head_ref_name);
5258 if (s->head_ref_name == NULL) {
5259 err = got_error_from_errno("strdup");
5265 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5266 err = add_color(&s->colors, "\\$$",
5267 TOG_COLOR_TREE_SUBMODULE,
5268 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5271 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5272 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5275 err = add_color(&s->colors, "/$",
5276 TOG_COLOR_TREE_DIRECTORY,
5277 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5281 err = add_color(&s->colors, "\\*$",
5282 TOG_COLOR_TREE_EXECUTABLE,
5283 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5287 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5288 get_color_value("TOG_COLOR_COMMIT"));
5293 view->show = show_tree_view;
5294 view->input = input_tree_view;
5295 view->close = close_tree_view;
5296 view->search_start = search_start_tree_view;
5297 view->search_next = search_next_tree_view;
5299 free(commit_id_str);
5301 got_object_commit_close(commit);
5303 close_tree_view(view);
5307 static const struct got_error *
5308 close_tree_view(struct tog_view *view)
5310 struct tog_tree_view_state *s = &view->state.tree;
5312 free_colors(&s->colors);
5313 free(s->tree_label);
5314 s->tree_label = NULL;
5316 s->commit_id = NULL;
5317 free(s->head_ref_name);
5318 s->head_ref_name = NULL;
5319 while (!TAILQ_EMPTY(&s->parents)) {
5320 struct tog_parent_tree *parent;
5321 parent = TAILQ_FIRST(&s->parents);
5322 TAILQ_REMOVE(&s->parents, parent, entry);
5323 if (parent->tree != s->root)
5324 got_object_tree_close(parent->tree);
5328 if (s->tree != NULL && s->tree != s->root)
5329 got_object_tree_close(s->tree);
5331 got_object_tree_close(s->root);
5335 static const struct got_error *
5336 search_start_tree_view(struct tog_view *view)
5338 struct tog_tree_view_state *s = &view->state.tree;
5340 s->matched_entry = NULL;
5345 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5347 regmatch_t regmatch;
5349 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5353 static const struct got_error *
5354 search_next_tree_view(struct tog_view *view)
5356 struct tog_tree_view_state *s = &view->state.tree;
5357 struct got_tree_entry *te = NULL;
5359 if (!view->searching) {
5360 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5364 if (s->matched_entry) {
5365 if (view->searching == TOG_SEARCH_FORWARD) {
5366 if (s->selected_entry)
5367 te = got_tree_entry_get_next(s->tree,
5370 te = got_object_tree_get_first_entry(s->tree);
5372 if (s->selected_entry == NULL)
5373 te = got_object_tree_get_last_entry(s->tree);
5375 te = got_tree_entry_get_prev(s->tree,
5379 if (s->selected_entry)
5380 te = s->selected_entry;
5381 else if (view->searching == TOG_SEARCH_FORWARD)
5382 te = got_object_tree_get_first_entry(s->tree);
5384 te = got_object_tree_get_last_entry(s->tree);
5389 if (s->matched_entry == NULL) {
5390 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5393 if (view->searching == TOG_SEARCH_FORWARD)
5394 te = got_object_tree_get_first_entry(s->tree);
5396 te = got_object_tree_get_last_entry(s->tree);
5399 if (match_tree_entry(te, &view->regex)) {
5400 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5401 s->matched_entry = te;
5405 if (view->searching == TOG_SEARCH_FORWARD)
5406 te = got_tree_entry_get_next(s->tree, te);
5408 te = got_tree_entry_get_prev(s->tree, te);
5411 if (s->matched_entry) {
5412 s->first_displayed_entry = s->matched_entry;
5419 static const struct got_error *
5420 show_tree_view(struct tog_view *view)
5422 const struct got_error *err = NULL;
5423 struct tog_tree_view_state *s = &view->state.tree;
5426 err = tree_entry_path(&parent_path, &s->parents, NULL);
5430 err = draw_tree_entries(view, parent_path);
5437 static const struct got_error *
5438 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5440 const struct got_error *err = NULL;
5441 struct tog_tree_view_state *s = &view->state.tree;
5442 struct tog_view *log_view, *ref_view;
5443 struct got_tree_entry *te;
5448 s->show_ids = !s->show_ids;
5451 if (!s->selected_entry)
5453 if (view_is_parent_view(view))
5454 begin_x = view_split_begin_x(view->begin_x);
5455 err = log_selected_tree_entry(&log_view, begin_x, s);
5457 log_view->focussed = 1;
5458 if (view_is_parent_view(view)) {
5459 err = view_close_child(view);
5462 view_set_child(view, log_view);
5463 view->focus_child = 1;
5465 *new_view = log_view;
5468 if (view_is_parent_view(view))
5469 begin_x = view_split_begin_x(view->begin_x);
5470 ref_view = view_open(view->nlines, view->ncols,
5471 view->begin_y, begin_x, TOG_VIEW_REF);
5472 if (ref_view == NULL)
5473 return got_error_from_errno("view_open");
5474 err = open_ref_view(ref_view, s->repo);
5476 view_close(ref_view);
5480 ref_view->focussed = 1;
5481 if (view_is_parent_view(view)) {
5482 err = view_close_child(view);
5485 view_set_child(view, ref_view);
5486 view->focus_child = 1;
5488 *new_view = ref_view;
5493 if (s->tree == s->root)
5494 s->first_displayed_entry =
5495 got_object_tree_get_first_entry(s->tree);
5497 s->first_displayed_entry = NULL;
5502 te = got_object_tree_get_last_entry(s->tree);
5503 for (n = 0; n < view->nlines - 3; n++) {
5505 if(s->tree != s->root) {
5506 s->first_displayed_entry = NULL;
5511 s->first_displayed_entry = te;
5512 te = got_tree_entry_get_prev(s->tree, te);
5515 s->selected = n - 1;
5520 if (s->selected > 0) {
5524 tree_scroll_up(s, 1);
5528 if (s->tree == s->root) {
5529 if (got_object_tree_get_first_entry(s->tree) ==
5530 s->first_displayed_entry)
5533 if (s->first_displayed_entry == NULL)
5536 tree_scroll_up(s, MAX(0, view->nlines - 3));
5541 if (s->selected < s->ndisplayed - 1) {
5545 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5547 /* can't scroll any further */
5549 tree_scroll_down(s, 1);
5553 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5555 /* can't scroll any further; move cursor down */
5556 if (s->selected < s->ndisplayed - 1)
5557 s->selected = s->ndisplayed - 1;
5560 tree_scroll_down(s, view->nlines - 3);
5565 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5566 struct tog_parent_tree *parent;
5567 /* user selected '..' */
5568 if (s->tree == s->root)
5570 parent = TAILQ_FIRST(&s->parents);
5571 TAILQ_REMOVE(&s->parents, parent,
5573 got_object_tree_close(s->tree);
5574 s->tree = parent->tree;
5575 s->first_displayed_entry =
5576 parent->first_displayed_entry;
5578 parent->selected_entry;
5579 s->selected = parent->selected;
5581 } else if (S_ISDIR(got_tree_entry_get_mode(
5582 s->selected_entry))) {
5583 struct got_tree_object *subtree;
5584 err = got_object_open_as_tree(&subtree, s->repo,
5585 got_tree_entry_get_id(s->selected_entry));
5588 err = tree_view_visit_subtree(s, subtree);
5590 got_object_tree_close(subtree);
5593 } else if (S_ISREG(got_tree_entry_get_mode(
5594 s->selected_entry))) {
5595 struct tog_view *blame_view;
5596 int begin_x = view_is_parent_view(view) ?
5597 view_split_begin_x(view->begin_x) : 0;
5599 err = blame_tree_entry(&blame_view, begin_x,
5600 s->selected_entry, &s->parents,
5601 s->commit_id, s->repo);
5605 blame_view->focussed = 1;
5606 if (view_is_parent_view(view)) {
5607 err = view_close_child(view);
5610 view_set_child(view, blame_view);
5611 view->focus_child = 1;
5613 *new_view = blame_view;
5617 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5618 s->selected = view->nlines - 4;
5631 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5636 static const struct got_error *
5637 cmd_tree(int argc, char *argv[])
5639 const struct got_error *error;
5640 struct got_repository *repo = NULL;
5641 struct got_worktree *worktree = NULL;
5642 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5643 struct got_object_id *commit_id = NULL;
5644 const char *commit_id_arg = NULL;
5646 struct got_reference *ref = NULL;
5647 const char *head_ref_name = NULL;
5649 struct tog_view *view;
5651 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5654 commit_id_arg = optarg;
5657 repo_path = realpath(optarg, NULL);
5658 if (repo_path == NULL)
5659 return got_error_from_errno2("realpath",
5674 if (repo_path == NULL) {
5675 cwd = getcwd(NULL, 0);
5677 return got_error_from_errno("getcwd");
5678 error = got_worktree_open(&worktree, cwd);
5679 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5683 strdup(got_worktree_get_repo_path(worktree));
5685 repo_path = strdup(cwd);
5686 if (repo_path == NULL) {
5687 error = got_error_from_errno("strdup");
5692 error = got_repo_open(&repo, repo_path, NULL);
5696 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5703 error = apply_unveil(got_repo_get_path(repo), NULL);
5707 error = tog_load_refs(repo, 0);
5711 if (commit_id_arg == NULL) {
5712 error = got_repo_match_object_id(&commit_id, &label,
5713 worktree ? got_worktree_get_head_ref_name(worktree) :
5714 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5717 head_ref_name = label;
5719 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5721 head_ref_name = got_ref_get_name(ref);
5722 else if (error->code != GOT_ERR_NOT_REF)
5724 error = got_repo_match_object_id(&commit_id, NULL,
5725 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5730 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5732 error = got_error_from_errno("view_open");
5735 error = open_tree_view(view, commit_id, head_ref_name, repo);
5738 if (!got_path_is_root_dir(in_repo_path)) {
5739 error = tree_view_walk_path(&view->state.tree, commit_id,
5746 /* Release work tree lock. */
5747 got_worktree_close(worktree);
5750 error = view_loop(view);
5759 const struct got_error *close_err = got_repo_close(repo);
5767 static const struct got_error *
5768 ref_view_load_refs(struct tog_ref_view_state *s)
5770 struct got_reflist_entry *sre;
5771 struct tog_reflist_entry *re;
5774 TAILQ_FOREACH(sre, &tog_refs, entry) {
5775 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5778 re = malloc(sizeof(*re));
5780 return got_error_from_errno("malloc");
5782 re->ref = got_ref_dup(sre->ref);
5783 if (re->ref == NULL)
5784 return got_error_from_errno("got_ref_dup");
5785 re->idx = s->nrefs++;
5786 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5789 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5794 ref_view_free_refs(struct tog_ref_view_state *s)
5796 struct tog_reflist_entry *re;
5798 while (!TAILQ_EMPTY(&s->refs)) {
5799 re = TAILQ_FIRST(&s->refs);
5800 TAILQ_REMOVE(&s->refs, re, entry);
5801 got_ref_close(re->ref);
5806 static const struct got_error *
5807 open_ref_view(struct tog_view *view, struct got_repository *repo)
5809 const struct got_error *err = NULL;
5810 struct tog_ref_view_state *s = &view->state.ref;
5812 s->selected_entry = 0;
5815 TAILQ_INIT(&s->refs);
5816 STAILQ_INIT(&s->colors);
5818 err = ref_view_load_refs(s);
5822 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5823 err = add_color(&s->colors, "^refs/heads/",
5824 TOG_COLOR_REFS_HEADS,
5825 get_color_value("TOG_COLOR_REFS_HEADS"));
5829 err = add_color(&s->colors, "^refs/tags/",
5830 TOG_COLOR_REFS_TAGS,
5831 get_color_value("TOG_COLOR_REFS_TAGS"));
5835 err = add_color(&s->colors, "^refs/remotes/",
5836 TOG_COLOR_REFS_REMOTES,
5837 get_color_value("TOG_COLOR_REFS_REMOTES"));
5842 view->show = show_ref_view;
5843 view->input = input_ref_view;
5844 view->close = close_ref_view;
5845 view->search_start = search_start_ref_view;
5846 view->search_next = search_next_ref_view;
5849 free_colors(&s->colors);
5853 static const struct got_error *
5854 close_ref_view(struct tog_view *view)
5856 struct tog_ref_view_state *s = &view->state.ref;
5858 ref_view_free_refs(s);
5859 free_colors(&s->colors);
5864 static const struct got_error *
5865 resolve_reflist_entry(struct got_object_id **commit_id,
5866 struct tog_reflist_entry *re, struct got_repository *repo)
5868 const struct got_error *err = NULL;
5869 struct got_object_id *obj_id;
5870 struct got_tag_object *tag = NULL;
5875 err = got_ref_resolve(&obj_id, repo, re->ref);
5879 err = got_object_get_type(&obj_type, repo, obj_id);
5884 case GOT_OBJ_TYPE_COMMIT:
5885 *commit_id = obj_id;
5887 case GOT_OBJ_TYPE_TAG:
5888 err = got_object_open_as_tag(&tag, repo, obj_id);
5892 err = got_object_get_type(&obj_type, repo,
5893 got_object_tag_get_object_id(tag));
5896 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5897 err = got_error(GOT_ERR_OBJ_TYPE);
5900 *commit_id = got_object_id_dup(
5901 got_object_tag_get_object_id(tag));
5902 if (*commit_id == NULL) {
5903 err = got_error_from_errno("got_object_id_dup");
5908 err = got_error(GOT_ERR_OBJ_TYPE);
5914 got_object_tag_close(tag);
5922 static const struct got_error *
5923 log_ref_entry(struct tog_view **new_view, int begin_x,
5924 struct tog_reflist_entry *re, struct got_repository *repo)
5926 struct tog_view *log_view;
5927 const struct got_error *err = NULL;
5928 struct got_object_id *commit_id = NULL;
5932 err = resolve_reflist_entry(&commit_id, re, repo);
5934 if (err->code != GOT_ERR_OBJ_TYPE)
5940 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5941 if (log_view == NULL) {
5942 err = got_error_from_errno("view_open");
5946 err = open_log_view(log_view, commit_id, repo,
5947 got_ref_get_name(re->ref), "", 0);
5950 view_close(log_view);
5952 *new_view = log_view;
5958 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5960 struct tog_reflist_entry *re;
5963 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5966 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5967 while (i++ < maxscroll) {
5970 s->first_displayed_entry = re;
5971 re = TAILQ_PREV(re, tog_reflist_head, entry);
5976 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5978 struct tog_reflist_entry *next, *last;
5981 if (s->first_displayed_entry)
5982 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5984 next = TAILQ_FIRST(&s->refs);
5986 last = s->last_displayed_entry;
5987 while (next && last && n++ < maxscroll) {
5988 last = TAILQ_NEXT(last, entry);
5990 s->first_displayed_entry = next;
5991 next = TAILQ_NEXT(next, entry);
5996 static const struct got_error *
5997 search_start_ref_view(struct tog_view *view)
5999 struct tog_ref_view_state *s = &view->state.ref;
6001 s->matched_entry = NULL;
6006 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6008 regmatch_t regmatch;
6010 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6014 static const struct got_error *
6015 search_next_ref_view(struct tog_view *view)
6017 struct tog_ref_view_state *s = &view->state.ref;
6018 struct tog_reflist_entry *re = NULL;
6020 if (!view->searching) {
6021 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6025 if (s->matched_entry) {
6026 if (view->searching == TOG_SEARCH_FORWARD) {
6027 if (s->selected_entry)
6028 re = TAILQ_NEXT(s->selected_entry, entry);
6030 re = TAILQ_PREV(s->selected_entry,
6031 tog_reflist_head, entry);
6033 if (s->selected_entry == NULL)
6034 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6036 re = TAILQ_PREV(s->selected_entry,
6037 tog_reflist_head, entry);
6040 if (s->selected_entry)
6041 re = s->selected_entry;
6042 else if (view->searching == TOG_SEARCH_FORWARD)
6043 re = TAILQ_FIRST(&s->refs);
6045 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6050 if (s->matched_entry == NULL) {
6051 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6054 if (view->searching == TOG_SEARCH_FORWARD)
6055 re = TAILQ_FIRST(&s->refs);
6057 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6060 if (match_reflist_entry(re, &view->regex)) {
6061 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6062 s->matched_entry = re;
6066 if (view->searching == TOG_SEARCH_FORWARD)
6067 re = TAILQ_NEXT(re, entry);
6069 re = TAILQ_PREV(re, tog_reflist_head, entry);
6072 if (s->matched_entry) {
6073 s->first_displayed_entry = s->matched_entry;
6080 static const struct got_error *
6081 show_ref_view(struct tog_view *view)
6083 const struct got_error *err = NULL;
6084 struct tog_ref_view_state *s = &view->state.ref;
6085 struct tog_reflist_entry *re;
6088 struct tog_color *tc;
6090 int limit = view->nlines;
6092 werase(view->window);
6099 re = s->first_displayed_entry;
6101 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6103 return got_error_from_errno("asprintf");
6105 err = format_line(&wline, &width, line, view->ncols, 0);
6110 if (view_needs_focus_indication(view))
6111 wstandout(view->window);
6112 waddwstr(view->window, wline);
6113 if (view_needs_focus_indication(view))
6114 wstandend(view->window);
6119 if (width < view->ncols - 1)
6120 waddch(view->window, '\n');
6125 while (re && limit > 0) {
6128 if (got_ref_is_symbolic(re->ref)) {
6129 if (asprintf(&line, "%s -> %s",
6130 got_ref_get_name(re->ref),
6131 got_ref_get_symref_target(re->ref)) == -1)
6132 return got_error_from_errno("asprintf");
6133 } else if (s->show_ids) {
6134 struct got_object_id *id;
6136 err = got_ref_resolve(&id, s->repo, re->ref);
6139 err = got_object_id_str(&id_str, id);
6144 if (asprintf(&line, "%s: %s",
6145 got_ref_get_name(re->ref), id_str) == -1) {
6146 err = got_error_from_errno("asprintf");
6154 line = strdup(got_ref_get_name(re->ref));
6156 return got_error_from_errno("strdup");
6159 err = format_line(&wline, &width, line, view->ncols, 0);
6164 if (n == s->selected) {
6166 wstandout(view->window);
6167 s->selected_entry = re;
6169 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6171 wattr_on(view->window,
6172 COLOR_PAIR(tc->colorpair), NULL);
6173 waddwstr(view->window, wline);
6175 wattr_off(view->window,
6176 COLOR_PAIR(tc->colorpair), NULL);
6177 if (width < view->ncols - 1)
6178 waddch(view->window, '\n');
6179 if (n == s->selected && view->focussed)
6180 wstandend(view->window);
6186 s->last_displayed_entry = re;
6189 re = TAILQ_NEXT(re, entry);
6196 static const struct got_error *
6197 browse_ref_tree(struct tog_view **new_view, int begin_x,
6198 struct tog_reflist_entry *re, struct got_repository *repo)
6200 const struct got_error *err = NULL;
6201 struct got_object_id *commit_id = NULL;
6202 struct tog_view *tree_view;
6206 err = resolve_reflist_entry(&commit_id, re, repo);
6208 if (err->code != GOT_ERR_OBJ_TYPE)
6215 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6216 if (tree_view == NULL) {
6217 err = got_error_from_errno("view_open");
6221 err = open_tree_view(tree_view, commit_id,
6222 got_ref_get_name(re->ref), repo);
6226 *new_view = tree_view;
6231 static const struct got_error *
6232 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6234 const struct got_error *err = NULL;
6235 struct tog_ref_view_state *s = &view->state.ref;
6236 struct tog_view *log_view, *tree_view;
6237 struct tog_reflist_entry *re;
6242 s->show_ids = !s->show_ids;
6245 s->sort_by_date = !s->sort_by_date;
6246 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6247 got_ref_cmp_by_commit_timestamp_descending :
6248 got_ref_cmp_by_name, s->repo);
6251 got_reflist_object_id_map_free(tog_refs_idmap);
6252 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6253 &tog_refs, s->repo);
6256 ref_view_free_refs(s);
6257 err = ref_view_load_refs(s);
6261 if (!s->selected_entry)
6263 if (view_is_parent_view(view))
6264 begin_x = view_split_begin_x(view->begin_x);
6265 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6268 log_view->focussed = 1;
6269 if (view_is_parent_view(view)) {
6270 err = view_close_child(view);
6273 view_set_child(view, log_view);
6274 view->focus_child = 1;
6276 *new_view = log_view;
6279 if (!s->selected_entry)
6281 if (view_is_parent_view(view))
6282 begin_x = view_split_begin_x(view->begin_x);
6283 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6285 if (err || tree_view == NULL)
6288 tree_view->focussed = 1;
6289 if (view_is_parent_view(view)) {
6290 err = view_close_child(view);
6293 view_set_child(view, tree_view);
6294 view->focus_child = 1;
6296 *new_view = tree_view;
6301 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6306 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6307 for (n = 0; n < view->nlines - 1; n++) {
6310 s->first_displayed_entry = re;
6311 re = TAILQ_PREV(re, tog_reflist_head, entry);
6314 s->selected = n - 1;
6319 if (s->selected > 0) {
6323 ref_scroll_up(s, 1);
6327 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6329 ref_scroll_up(s, MAX(0, view->nlines - 1));
6334 if (s->selected < s->ndisplayed - 1) {
6338 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6339 /* can't scroll any further */
6341 ref_scroll_down(s, 1);
6345 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6346 /* can't scroll any further; move cursor down */
6347 if (s->selected < s->ndisplayed - 1)
6348 s->selected = s->ndisplayed - 1;
6351 ref_scroll_down(s, view->nlines - 1);
6355 err = tog_load_refs(s->repo, s->sort_by_date);
6358 ref_view_free_refs(s);
6359 err = ref_view_load_refs(s);
6362 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6363 s->selected = view->nlines - 2;
6376 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6381 static const struct got_error *
6382 cmd_ref(int argc, char *argv[])
6384 const struct got_error *error;
6385 struct got_repository *repo = NULL;
6386 struct got_worktree *worktree = NULL;
6387 char *cwd = NULL, *repo_path = NULL;
6389 struct tog_view *view;
6391 while ((ch = getopt(argc, argv, "r:")) != -1) {
6394 repo_path = realpath(optarg, NULL);
6395 if (repo_path == NULL)
6396 return got_error_from_errno2("realpath",
6411 if (repo_path == NULL) {
6412 cwd = getcwd(NULL, 0);
6414 return got_error_from_errno("getcwd");
6415 error = got_worktree_open(&worktree, cwd);
6416 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6420 strdup(got_worktree_get_repo_path(worktree));
6422 repo_path = strdup(cwd);
6423 if (repo_path == NULL) {
6424 error = got_error_from_errno("strdup");
6429 error = got_repo_open(&repo, repo_path, NULL);
6435 error = apply_unveil(got_repo_get_path(repo), NULL);
6439 error = tog_load_refs(repo, 0);
6443 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6445 error = got_error_from_errno("view_open");
6449 error = open_ref_view(view, repo);
6454 /* Release work tree lock. */
6455 got_worktree_close(worktree);
6458 error = view_loop(view);
6463 const struct got_error *close_err = got_repo_close(repo);
6472 list_commands(FILE *fp)
6476 fprintf(fp, "commands:");
6477 for (i = 0; i < nitems(tog_commands); i++) {
6478 struct tog_cmd *cmd = &tog_commands[i];
6479 fprintf(fp, " %s", cmd->name);
6485 usage(int hflag, int status)
6487 FILE *fp = (status == 0) ? stdout : stderr;
6489 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6492 fprintf(fp, "lazy usage: %s path\n", getprogname());
6499 make_argv(int argc, ...)
6507 argv = calloc(argc, sizeof(char *));
6510 for (i = 0; i < argc; i++) {
6511 argv[i] = strdup(va_arg(ap, char *));
6512 if (argv[i] == NULL)
6521 * Try to convert 'tog path' into a 'tog log path' command.
6522 * The user could simply have mistyped the command rather than knowingly
6523 * provided a path. So check whether argv[0] can in fact be resolved
6524 * to a path in the HEAD commit and print a special error if not.
6525 * This hack is for mpi@ <3
6527 static const struct got_error *
6528 tog_log_with_path(int argc, char *argv[])
6530 const struct got_error *error = NULL, *close_err;
6531 struct tog_cmd *cmd = NULL;
6532 struct got_repository *repo = NULL;
6533 struct got_worktree *worktree = NULL;
6534 struct got_object_id *commit_id = NULL, *id = NULL;
6535 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6536 char *commit_id_str = NULL, **cmd_argv = NULL;
6538 cwd = getcwd(NULL, 0);
6540 return got_error_from_errno("getcwd");
6542 error = got_worktree_open(&worktree, cwd);
6543 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6547 repo_path = strdup(got_worktree_get_repo_path(worktree));
6549 repo_path = strdup(cwd);
6550 if (repo_path == NULL) {
6551 error = got_error_from_errno("strdup");
6555 error = got_repo_open(&repo, repo_path, NULL);
6559 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6564 error = tog_load_refs(repo, 0);
6567 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6568 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6569 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6574 got_worktree_close(worktree);
6578 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6580 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6582 fprintf(stderr, "%s: '%s' is no known command or path\n",
6583 getprogname(), argv[0]);
6588 close_err = got_repo_close(repo);
6593 error = got_object_id_str(&commit_id_str, commit_id);
6597 cmd = &tog_commands[0]; /* log */
6599 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6600 error = cmd->cmd_main(argc, cmd_argv);
6603 close_err = got_repo_close(repo);
6608 got_worktree_close(worktree);
6610 free(commit_id_str);
6617 for (i = 0; i < argc; i++)
6626 main(int argc, char *argv[])
6628 const struct got_error *error = NULL;
6629 struct tog_cmd *cmd = NULL;
6630 int ch, hflag = 0, Vflag = 0;
6631 char **cmd_argv = NULL;
6632 static struct option longopts[] = {
6633 { "version", no_argument, NULL, 'V' },
6637 setlocale(LC_CTYPE, "");
6639 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6659 got_version_print_str();
6664 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6672 /* Build an argument vector which runs a default command. */
6673 cmd = &tog_commands[0];
6675 cmd_argv = make_argv(argc, cmd->name);
6679 /* Did the user specify a command? */
6680 for (i = 0; i < nitems(tog_commands); i++) {
6681 if (strncmp(tog_commands[i].name, argv[0],
6682 strlen(argv[0])) == 0) {
6683 cmd = &tog_commands[i];
6692 /* No command specified; try log with a path */
6693 error = tog_log_with_path(argc, argv);
6698 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6705 for (i = 0; i < argc; i++)
6710 if (error && error->code != GOT_ERR_CANCELLED)
6711 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);