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)
142 const struct got_error *err;
144 err = got_ref_list(&tog_refs, repo, NULL, got_ref_cmp_by_name, NULL);
148 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
155 if (tog_refs_idmap) {
156 got_reflist_object_id_map_free(tog_refs_idmap);
157 tog_refs_idmap = NULL;
159 got_ref_list_free(&tog_refs);
162 static const struct got_error *
163 add_color(struct tog_colors *colors, const char *pattern,
164 int idx, short color)
166 const struct got_error *err = NULL;
167 struct tog_color *tc;
170 if (idx < 1 || idx > COLOR_PAIRS - 1)
173 init_pair(idx, color, -1);
175 tc = calloc(1, sizeof(*tc));
177 return got_error_from_errno("calloc");
178 regerr = regcomp(&tc->regex, pattern,
179 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
181 static char regerr_msg[512];
182 static char err_msg[512];
183 regerror(regerr, &tc->regex, regerr_msg,
185 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
187 err = got_error_msg(GOT_ERR_REGEX, err_msg);
192 STAILQ_INSERT_HEAD(colors, tc, entry);
197 free_colors(struct tog_colors *colors)
199 struct tog_color *tc;
201 while (!STAILQ_EMPTY(colors)) {
202 tc = STAILQ_FIRST(colors);
203 STAILQ_REMOVE_HEAD(colors, entry);
210 get_color(struct tog_colors *colors, int colorpair)
212 struct tog_color *tc = NULL;
214 STAILQ_FOREACH(tc, colors, entry) {
215 if (tc->colorpair == colorpair)
223 default_color_value(const char *envvar)
225 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
226 return COLOR_MAGENTA;
227 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
229 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
231 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
233 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
234 return COLOR_MAGENTA;
235 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
236 return COLOR_MAGENTA;
237 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
239 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
241 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
243 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
245 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
247 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
249 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
250 return COLOR_MAGENTA;
251 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
258 get_color_value(const char *envvar)
260 const char *val = getenv(envvar);
263 return default_color_value(envvar);
265 if (strcasecmp(val, "black") == 0)
267 if (strcasecmp(val, "red") == 0)
269 if (strcasecmp(val, "green") == 0)
271 if (strcasecmp(val, "yellow") == 0)
273 if (strcasecmp(val, "blue") == 0)
275 if (strcasecmp(val, "magenta") == 0)
276 return COLOR_MAGENTA;
277 if (strcasecmp(val, "cyan") == 0)
279 if (strcasecmp(val, "white") == 0)
281 if (strcasecmp(val, "default") == 0)
284 return default_color_value(envvar);
288 struct tog_diff_view_state {
289 struct got_object_id *id1, *id2;
290 const char *label1, *label2;
292 int first_displayed_line;
293 int last_displayed_line;
296 int ignore_whitespace;
298 struct got_repository *repo;
299 struct tog_colors colors;
305 /* passed from log view; may be NULL */
306 struct tog_view *log_view;
309 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
311 struct tog_log_thread_args {
312 pthread_cond_t need_commits;
313 pthread_cond_t commit_loaded;
316 struct got_commit_graph *graph;
317 struct commit_queue *commits;
318 const char *in_repo_path;
319 struct got_object_id *start_id;
320 struct got_repository *repo;
323 struct commit_queue_entry **first_displayed_entry;
324 struct commit_queue_entry **selected_entry;
326 int *search_next_done;
330 struct tog_log_view_state {
331 struct commit_queue commits;
332 struct commit_queue_entry *first_displayed_entry;
333 struct commit_queue_entry *last_displayed_entry;
334 struct commit_queue_entry *selected_entry;
339 struct got_repository *repo;
340 struct got_object_id *start_id;
343 struct tog_log_thread_args thread_args;
344 struct commit_queue_entry *matched_entry;
345 struct commit_queue_entry *search_entry;
346 struct tog_colors colors;
349 #define TOG_COLOR_DIFF_MINUS 1
350 #define TOG_COLOR_DIFF_PLUS 2
351 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
352 #define TOG_COLOR_DIFF_META 4
353 #define TOG_COLOR_TREE_SUBMODULE 5
354 #define TOG_COLOR_TREE_SYMLINK 6
355 #define TOG_COLOR_TREE_DIRECTORY 7
356 #define TOG_COLOR_TREE_EXECUTABLE 8
357 #define TOG_COLOR_COMMIT 9
358 #define TOG_COLOR_AUTHOR 10
359 #define TOG_COLOR_DATE 11
360 #define TOG_COLOR_REFS_HEADS 12
361 #define TOG_COLOR_REFS_TAGS 13
362 #define TOG_COLOR_REFS_REMOTES 14
364 struct tog_blame_cb_args {
365 struct tog_blame_line *lines; /* one per line */
368 struct tog_view *view;
369 struct got_object_id *commit_id;
373 struct tog_blame_thread_args {
375 struct got_repository *repo;
376 struct tog_blame_cb_args *cb_args;
378 got_cancel_cb cancel_cb;
385 struct tog_blame_line *lines;
389 struct tog_blame_thread_args thread_args;
390 struct tog_blame_cb_args cb_args;
394 struct tog_blame_view_state {
395 int first_displayed_line;
396 int last_displayed_line;
401 struct got_object_id_queue blamed_commits;
402 struct got_object_qid *blamed_commit;
404 struct got_repository *repo;
405 struct got_object_id *commit_id;
406 struct tog_blame blame;
408 struct tog_colors colors;
411 struct tog_parent_tree {
412 TAILQ_ENTRY(tog_parent_tree) entry;
413 struct got_tree_object *tree;
414 struct got_tree_entry *first_displayed_entry;
415 struct got_tree_entry *selected_entry;
419 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
421 struct tog_tree_view_state {
423 struct got_object_id *commit_id;/* commit which this tree belongs to */
424 struct got_tree_object *root; /* the commit's root tree entry */
425 struct got_tree_object *tree; /* currently displayed (sub-)tree */
426 struct got_tree_entry *first_displayed_entry;
427 struct got_tree_entry *last_displayed_entry;
428 struct got_tree_entry *selected_entry;
429 int ndisplayed, selected, show_ids;
430 struct tog_parent_trees parents; /* parent trees of current sub-tree */
432 struct got_repository *repo;
433 struct got_tree_entry *matched_entry;
434 struct tog_colors colors;
437 struct tog_reflist_entry {
438 TAILQ_ENTRY(tog_reflist_entry) entry;
439 struct got_reference *ref;
443 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
445 struct tog_ref_view_state {
446 struct tog_reflist_head refs;
447 struct tog_reflist_entry *first_displayed_entry;
448 struct tog_reflist_entry *last_displayed_entry;
449 struct tog_reflist_entry *selected_entry;
450 int nrefs, ndisplayed, selected, show_ids;
451 struct got_repository *repo;
452 struct tog_reflist_entry *matched_entry;
453 struct tog_colors colors;
457 * We implement two types of views: parent views and child views.
459 * The 'Tab' key switches focus between a parent view and its child view.
460 * Child views are shown side-by-side to their parent view, provided
461 * there is enough screen estate.
463 * When a new view is opened from within a parent view, this new view
464 * becomes a child view of the parent view, replacing any existing child.
466 * When a new view is opened from within a child view, this new view
467 * becomes a parent view which will obscure the views below until the
468 * user quits the new parent view by typing 'q'.
470 * This list of views contains parent views only.
471 * Child views are only pointed to by their parent view.
473 TAILQ_HEAD(tog_view_list_head, tog_view);
476 TAILQ_ENTRY(tog_view) entry;
479 int nlines, ncols, begin_y, begin_x;
480 int lines, cols; /* copies of LINES and COLS */
481 int focussed; /* Only set on one parent or child view at a time. */
483 struct tog_view *parent;
484 struct tog_view *child;
487 * This flag is initially set on parent views when a new child view
488 * is created. It gets toggled when the 'Tab' key switches focus
489 * between parent and child.
490 * The flag indicates whether focus should be passed on to our child
491 * view if this parent view gets picked for focus after another parent
492 * view was closed. This prevents child views from losing focus in such
497 /* type-specific state */
498 enum tog_view_type type;
500 struct tog_diff_view_state diff;
501 struct tog_log_view_state log;
502 struct tog_blame_view_state blame;
503 struct tog_tree_view_state tree;
504 struct tog_ref_view_state ref;
507 const struct got_error *(*show)(struct tog_view *);
508 const struct got_error *(*input)(struct tog_view **,
509 struct tog_view *, int);
510 const struct got_error *(*close)(struct tog_view *);
512 const struct got_error *(*search_start)(struct tog_view *);
513 const struct got_error *(*search_next)(struct tog_view *);
516 #define TOG_SEARCH_FORWARD 1
517 #define TOG_SEARCH_BACKWARD 2
518 int search_next_done;
519 #define TOG_SEARCH_HAVE_MORE 1
520 #define TOG_SEARCH_NO_MORE 2
521 #define TOG_SEARCH_HAVE_NONE 3
526 static const struct got_error *open_diff_view(struct tog_view *,
527 struct got_object_id *, struct got_object_id *,
528 const char *, const char *, int, int, int, struct tog_view *,
529 struct got_repository *);
530 static const struct got_error *show_diff_view(struct tog_view *);
531 static const struct got_error *input_diff_view(struct tog_view **,
532 struct tog_view *, int);
533 static const struct got_error* close_diff_view(struct tog_view *);
534 static const struct got_error *search_start_diff_view(struct tog_view *);
535 static const struct got_error *search_next_diff_view(struct tog_view *);
537 static const struct got_error *open_log_view(struct tog_view *,
538 struct got_object_id *, struct got_repository *,
539 const char *, const char *, int);
540 static const struct got_error * show_log_view(struct tog_view *);
541 static const struct got_error *input_log_view(struct tog_view **,
542 struct tog_view *, int);
543 static const struct got_error *close_log_view(struct tog_view *);
544 static const struct got_error *search_start_log_view(struct tog_view *);
545 static const struct got_error *search_next_log_view(struct tog_view *);
547 static const struct got_error *open_blame_view(struct tog_view *, char *,
548 struct got_object_id *, struct got_repository *);
549 static const struct got_error *show_blame_view(struct tog_view *);
550 static const struct got_error *input_blame_view(struct tog_view **,
551 struct tog_view *, int);
552 static const struct got_error *close_blame_view(struct tog_view *);
553 static const struct got_error *search_start_blame_view(struct tog_view *);
554 static const struct got_error *search_next_blame_view(struct tog_view *);
556 static const struct got_error *open_tree_view(struct tog_view *,
557 struct got_object_id *, const char *, struct got_repository *);
558 static const struct got_error *show_tree_view(struct tog_view *);
559 static const struct got_error *input_tree_view(struct tog_view **,
560 struct tog_view *, int);
561 static const struct got_error *close_tree_view(struct tog_view *);
562 static const struct got_error *search_start_tree_view(struct tog_view *);
563 static const struct got_error *search_next_tree_view(struct tog_view *);
565 static const struct got_error *open_ref_view(struct tog_view *,
566 struct got_repository *);
567 static const struct got_error *show_ref_view(struct tog_view *);
568 static const struct got_error *input_ref_view(struct tog_view **,
569 struct tog_view *, int);
570 static const struct got_error *close_ref_view(struct tog_view *);
571 static const struct got_error *search_start_ref_view(struct tog_view *);
572 static const struct got_error *search_next_ref_view(struct tog_view *);
574 static volatile sig_atomic_t tog_sigwinch_received;
575 static volatile sig_atomic_t tog_sigpipe_received;
576 static volatile sig_atomic_t tog_sigcont_received;
579 tog_sigwinch(int signo)
581 tog_sigwinch_received = 1;
585 tog_sigpipe(int signo)
587 tog_sigpipe_received = 1;
591 tog_sigcont(int signo)
593 tog_sigcont_received = 1;
596 static const struct got_error *
597 view_close(struct tog_view *view)
599 const struct got_error *err = NULL;
602 view_close(view->child);
606 err = view->close(view);
608 del_panel(view->panel);
610 delwin(view->window);
615 static struct tog_view *
616 view_open(int nlines, int ncols, int begin_y, int begin_x,
617 enum tog_view_type type)
619 struct tog_view *view = calloc(1, sizeof(*view));
627 view->nlines = nlines ? nlines : LINES - begin_y;
628 view->ncols = ncols ? ncols : COLS - begin_x;
629 view->begin_y = begin_y;
630 view->begin_x = begin_x;
631 view->window = newwin(nlines, ncols, begin_y, begin_x);
632 if (view->window == NULL) {
636 view->panel = new_panel(view->window);
637 if (view->panel == NULL ||
638 set_panel_userptr(view->panel, view) != OK) {
643 keypad(view->window, TRUE);
648 view_split_begin_x(int begin_x)
650 if (begin_x > 0 || COLS < 120)
652 return (COLS - MAX(COLS / 2, 80));
655 static const struct got_error *view_resize(struct tog_view *);
657 static const struct got_error *
658 view_splitscreen(struct tog_view *view)
660 const struct got_error *err = NULL;
663 view->begin_x = view_split_begin_x(0);
664 view->nlines = LINES;
665 view->ncols = COLS - view->begin_x;
668 err = view_resize(view);
672 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
673 return got_error_from_errno("mvwin");
678 static const struct got_error *
679 view_fullscreen(struct tog_view *view)
681 const struct got_error *err = NULL;
685 view->nlines = LINES;
689 err = view_resize(view);
693 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
694 return got_error_from_errno("mvwin");
700 view_is_parent_view(struct tog_view *view)
702 return view->parent == NULL;
705 static const struct got_error *
706 view_resize(struct tog_view *view)
710 if (view->lines > LINES)
711 nlines = view->nlines - (view->lines - LINES);
713 nlines = view->nlines + (LINES - view->lines);
715 if (view->cols > COLS)
716 ncols = view->ncols - (view->cols - COLS);
718 ncols = view->ncols + (COLS - view->cols);
720 if (wresize(view->window, nlines, ncols) == ERR)
721 return got_error_from_errno("wresize");
722 if (replace_panel(view->panel, view->window) == ERR)
723 return got_error_from_errno("replace_panel");
724 wclear(view->window);
726 view->nlines = nlines;
732 view->child->begin_x = view_split_begin_x(view->begin_x);
733 if (view->child->begin_x == 0) {
734 view_fullscreen(view->child);
735 if (view->child->focussed)
736 show_panel(view->child->panel);
738 show_panel(view->panel);
740 view_splitscreen(view->child);
741 show_panel(view->child->panel);
748 static const struct got_error *
749 view_close_child(struct tog_view *view)
751 const struct got_error *err = NULL;
753 if (view->child == NULL)
756 err = view_close(view->child);
762 view_set_child(struct tog_view *view, struct tog_view *child)
765 child->parent = view;
769 view_is_splitscreen(struct tog_view *view)
771 return view->begin_x > 0;
780 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
781 cols = 80; /* Default */
787 resize_term(lines, cols);
790 static const struct got_error *
791 view_search_start(struct tog_view *view)
793 const struct got_error *err = NULL;
797 if (view->search_started) {
798 regfree(&view->regex);
800 memset(&view->regmatch, 0, sizeof(view->regmatch));
802 view->search_started = 0;
804 if (view->nlines < 1)
807 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
808 wclrtoeol(view->window);
812 ret = wgetnstr(view->window, pattern, sizeof(pattern));
818 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
819 err = view->search_start(view);
821 regfree(&view->regex);
824 view->search_started = 1;
825 view->searching = TOG_SEARCH_FORWARD;
826 view->search_next_done = 0;
827 view->search_next(view);
833 static const struct got_error *
834 view_input(struct tog_view **new, int *done, struct tog_view *view,
835 struct tog_view_list_head *views)
837 const struct got_error *err = NULL;
843 /* Clear "no matches" indicator. */
844 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
845 view->search_next_done == TOG_SEARCH_HAVE_NONE)
846 view->search_next_done = TOG_SEARCH_HAVE_MORE;
848 if (view->searching && !view->search_next_done) {
849 errcode = pthread_mutex_unlock(&tog_mutex);
851 return got_error_set_errno(errcode,
852 "pthread_mutex_unlock");
854 errcode = pthread_mutex_lock(&tog_mutex);
856 return got_error_set_errno(errcode,
857 "pthread_mutex_lock");
858 view->search_next(view);
862 nodelay(stdscr, FALSE);
863 /* Allow threads to make progress while we are waiting for input. */
864 errcode = pthread_mutex_unlock(&tog_mutex);
866 return got_error_set_errno(errcode, "pthread_mutex_unlock");
867 ch = wgetch(view->window);
868 errcode = pthread_mutex_lock(&tog_mutex);
870 return got_error_set_errno(errcode, "pthread_mutex_lock");
871 nodelay(stdscr, TRUE);
873 if (tog_sigwinch_received || tog_sigcont_received) {
875 tog_sigwinch_received = 0;
876 tog_sigcont_received = 0;
877 TAILQ_FOREACH(v, views, entry) {
878 err = view_resize(v);
881 err = v->input(new, v, KEY_RESIZE);
885 err = view_resize(v->child);
888 err = v->child->input(new, v->child,
900 view->child->focussed = 1;
901 view->focus_child = 1;
902 } else if (view->parent) {
904 view->parent->focussed = 1;
905 view->parent->focus_child = 0;
909 err = view->input(new, view, ch);
916 if (view_is_parent_view(view)) {
917 if (view->child == NULL)
919 if (view_is_splitscreen(view->child)) {
921 view->child->focussed = 1;
922 err = view_fullscreen(view->child);
924 err = view_splitscreen(view->child);
927 err = view->child->input(new, view->child,
930 if (view_is_splitscreen(view)) {
931 view->parent->focussed = 0;
933 err = view_fullscreen(view);
935 err = view_splitscreen(view);
939 err = view->input(new, view, KEY_RESIZE);
945 if (view->search_start)
946 view_search_start(view);
948 err = view->input(new, view, ch);
952 if (view->search_started && view->search_next) {
953 view->searching = (ch == 'n' ?
954 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
955 view->search_next_done = 0;
956 view->search_next(view);
958 err = view->input(new, view, ch);
961 err = view->input(new, view, ch);
969 view_vborder(struct tog_view *view)
972 const struct tog_view *view_above;
975 return view_vborder(view->parent);
977 panel = panel_above(view->panel);
981 view_above = panel_userptr(panel);
982 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
983 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
987 view_needs_focus_indication(struct tog_view *view)
989 if (view_is_parent_view(view)) {
990 if (view->child == NULL || view->child->focussed)
992 if (!view_is_splitscreen(view->child))
994 } else if (!view_is_splitscreen(view))
997 return view->focussed;
1000 static const struct got_error *
1001 view_loop(struct tog_view *view)
1003 const struct got_error *err = NULL;
1004 struct tog_view_list_head views;
1005 struct tog_view *new_view;
1006 int fast_refresh = 10;
1007 int done = 0, errcode;
1009 errcode = pthread_mutex_lock(&tog_mutex);
1011 return got_error_set_errno(errcode, "pthread_mutex_lock");
1014 TAILQ_INSERT_HEAD(&views, view, entry);
1017 err = view->show(view);
1022 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1023 /* Refresh fast during initialization, then become slower. */
1024 if (fast_refresh && fast_refresh-- == 0)
1025 halfdelay(10); /* switch to once per second */
1027 err = view_input(&new_view, &done, view, &views);
1031 struct tog_view *v, *prev = NULL;
1033 if (view_is_parent_view(view))
1034 prev = TAILQ_PREV(view, tog_view_list_head,
1036 else if (view->parent)
1037 prev = view->parent;
1040 view->parent->child = NULL;
1041 view->parent->focus_child = 0;
1043 TAILQ_REMOVE(&views, view, entry);
1045 err = view_close(view);
1050 TAILQ_FOREACH(v, &views, entry) {
1054 if (view == NULL && new_view == NULL) {
1055 /* No view has focus. Try to pick one. */
1058 else if (!TAILQ_EMPTY(&views)) {
1059 view = TAILQ_LAST(&views,
1060 tog_view_list_head);
1063 if (view->focus_child) {
1064 view->child->focussed = 1;
1072 struct tog_view *v, *t;
1073 /* Only allow one parent view per type. */
1074 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1075 if (v->type != new_view->type)
1077 TAILQ_REMOVE(&views, v, entry);
1078 err = view_close(v);
1083 TAILQ_INSERT_TAIL(&views, new_view, entry);
1087 if (view_is_parent_view(view)) {
1088 if (view->child && view->child->focussed)
1091 if (view->parent && view->parent->focussed)
1092 view = view->parent;
1094 show_panel(view->panel);
1095 if (view->child && view_is_splitscreen(view->child))
1096 show_panel(view->child->panel);
1097 if (view->parent && view_is_splitscreen(view)) {
1098 err = view->parent->show(view->parent);
1102 err = view->show(view);
1106 err = view->child->show(view->child);
1115 while (!TAILQ_EMPTY(&views)) {
1116 view = TAILQ_FIRST(&views);
1117 TAILQ_REMOVE(&views, view, entry);
1121 errcode = pthread_mutex_unlock(&tog_mutex);
1122 if (errcode && err == NULL)
1123 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1133 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1138 /* Create newly allocated wide-character string equivalent to a byte string. */
1139 static const struct got_error *
1140 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1143 const struct got_error *err = NULL;
1146 *wlen = mbstowcs(NULL, s, 0);
1147 if (*wlen == (size_t)-1) {
1149 if (errno != EILSEQ)
1150 return got_error_from_errno("mbstowcs");
1152 /* byte string invalid in current encoding; try to "fix" it */
1153 err = got_mbsavis(&vis, &vislen, s);
1156 *wlen = mbstowcs(NULL, vis, 0);
1157 if (*wlen == (size_t)-1) {
1158 err = got_error_from_errno("mbstowcs"); /* give up */
1163 *ws = calloc(*wlen + 1, sizeof(**ws));
1165 err = got_error_from_errno("calloc");
1169 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1170 err = got_error_from_errno("mbstowcs");
1181 /* Format a line for display, ensuring that it won't overflow a width limit. */
1182 static const struct got_error *
1183 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1186 const struct got_error *err = NULL;
1188 wchar_t *wline = NULL;
1195 err = mbs2ws(&wline, &wlen, line);
1199 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1200 wline[wlen - 1] = L'\0';
1203 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1204 wline[wlen - 1] = L'\0';
1210 int width = wcwidth(wline[i]);
1217 if (width == 1 || width == 2) {
1218 if (cols + width > wlimit)
1222 } else if (width == -1) {
1223 if (wline[i] == L'\t') {
1225 ((cols + col_tab_align) % TABSIZE);
1230 if (cols + width > wlimit)
1235 err = got_error_from_errno("wcwidth");
1250 static const struct got_error*
1251 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1252 struct got_object_id *id, struct got_repository *repo)
1254 static const struct got_error *err = NULL;
1255 struct got_reflist_entry *re;
1261 TAILQ_FOREACH(re, refs, entry) {
1262 struct got_tag_object *tag = NULL;
1263 struct got_object_id *ref_id;
1266 name = got_ref_get_name(re->ref);
1267 if (strcmp(name, GOT_REF_HEAD) == 0)
1269 if (strncmp(name, "refs/", 5) == 0)
1271 if (strncmp(name, "got/", 4) == 0)
1273 if (strncmp(name, "heads/", 6) == 0)
1275 if (strncmp(name, "remotes/", 8) == 0) {
1277 s = strstr(name, "/" GOT_REF_HEAD);
1278 if (s != NULL && s[strlen(s)] == '\0')
1281 err = got_ref_resolve(&ref_id, repo, re->ref);
1284 if (strncmp(name, "tags/", 5) == 0) {
1285 err = got_object_open_as_tag(&tag, repo, ref_id);
1287 if (err->code != GOT_ERR_OBJ_TYPE) {
1291 /* Ref points at something other than a tag. */
1296 cmp = got_object_id_cmp(tag ?
1297 got_object_tag_get_object_id(tag) : ref_id, id);
1300 got_object_tag_close(tag);
1304 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1305 s ? ", " : "", name) == -1) {
1306 err = got_error_from_errno("asprintf");
1317 static const struct got_error *
1318 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1323 smallerthan = strchr(author, '<');
1324 if (smallerthan && smallerthan[1] != '\0')
1325 author = smallerthan + 1;
1326 author[strcspn(author, "@>")] = '\0';
1327 return format_line(wauthor, author_width, author, limit, col_tab_align);
1330 static const struct got_error *
1331 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1332 struct got_object_id *id, const size_t date_display_cols,
1333 int author_display_cols)
1335 struct tog_log_view_state *s = &view->state.log;
1336 const struct got_error *err = NULL;
1337 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1338 char *logmsg0 = NULL, *logmsg = NULL;
1339 char *author = NULL;
1340 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1341 int author_width, logmsg_width;
1342 char *newline, *line = NULL;
1344 const int avail = view->ncols;
1346 time_t committer_time;
1347 struct tog_color *tc;
1349 committer_time = got_object_commit_get_committer_time(commit);
1350 if (gmtime_r(&committer_time, &tm) == NULL)
1351 return got_error_from_errno("gmtime_r");
1352 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1353 return got_error(GOT_ERR_NO_SPACE);
1355 if (avail <= date_display_cols)
1356 limit = MIN(sizeof(datebuf) - 1, avail);
1358 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1359 tc = get_color(&s->colors, TOG_COLOR_DATE);
1361 wattr_on(view->window,
1362 COLOR_PAIR(tc->colorpair), NULL);
1363 waddnstr(view->window, datebuf, limit);
1365 wattr_off(view->window,
1366 COLOR_PAIR(tc->colorpair), NULL);
1373 err = got_object_id_str(&id_str, id);
1376 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1378 wattr_on(view->window,
1379 COLOR_PAIR(tc->colorpair), NULL);
1380 wprintw(view->window, "%.8s ", id_str);
1382 wattr_off(view->window,
1383 COLOR_PAIR(tc->colorpair), NULL);
1390 author = strdup(got_object_commit_get_author(commit));
1391 if (author == NULL) {
1392 err = got_error_from_errno("strdup");
1395 err = format_author(&wauthor, &author_width, author, avail - col, col);
1398 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1400 wattr_on(view->window,
1401 COLOR_PAIR(tc->colorpair), NULL);
1402 waddwstr(view->window, wauthor);
1404 wattr_off(view->window,
1405 COLOR_PAIR(tc->colorpair), NULL);
1406 col += author_width;
1407 while (col < avail && author_width < author_display_cols + 2) {
1408 waddch(view->window, ' ');
1415 err = got_object_commit_get_logmsg(&logmsg0, commit);
1419 while (*logmsg == '\n')
1421 newline = strchr(logmsg, '\n');
1424 limit = avail - col;
1425 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1428 waddwstr(view->window, wlogmsg);
1429 col += logmsg_width;
1430 while (col < avail) {
1431 waddch(view->window, ' ');
1443 static struct commit_queue_entry *
1444 alloc_commit_queue_entry(struct got_commit_object *commit,
1445 struct got_object_id *id)
1447 struct commit_queue_entry *entry;
1449 entry = calloc(1, sizeof(*entry));
1454 entry->commit = commit;
1459 pop_commit(struct commit_queue *commits)
1461 struct commit_queue_entry *entry;
1463 entry = TAILQ_FIRST(&commits->head);
1464 TAILQ_REMOVE(&commits->head, entry, entry);
1465 got_object_commit_close(entry->commit);
1466 commits->ncommits--;
1467 /* Don't free entry->id! It is owned by the commit graph. */
1472 free_commits(struct commit_queue *commits)
1474 while (!TAILQ_EMPTY(&commits->head))
1475 pop_commit(commits);
1478 static const struct got_error *
1479 match_commit(int *have_match, struct got_object_id *id,
1480 struct got_commit_object *commit, regex_t *regex)
1482 const struct got_error *err = NULL;
1483 regmatch_t regmatch;
1484 char *id_str = NULL, *logmsg = NULL;
1488 err = got_object_id_str(&id_str, id);
1492 err = got_object_commit_get_logmsg(&logmsg, commit);
1496 if (regexec(regex, got_object_commit_get_author(commit), 1,
1497 ®match, 0) == 0 ||
1498 regexec(regex, got_object_commit_get_committer(commit), 1,
1499 ®match, 0) == 0 ||
1500 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1501 regexec(regex, logmsg, 1, ®match, 0) == 0)
1509 static const struct got_error *
1510 queue_commits(struct tog_log_thread_args *a)
1512 const struct got_error *err = NULL;
1515 * We keep all commits open throughout the lifetime of the log
1516 * view in order to avoid having to re-fetch commits from disk
1517 * while updating the display.
1520 struct got_object_id *id;
1521 struct got_commit_object *commit;
1522 struct commit_queue_entry *entry;
1525 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1527 if (err || id == NULL)
1530 err = got_object_open_as_commit(&commit, a->repo, id);
1533 entry = alloc_commit_queue_entry(commit, id);
1534 if (entry == NULL) {
1535 err = got_error_from_errno("alloc_commit_queue_entry");
1539 errcode = pthread_mutex_lock(&tog_mutex);
1541 err = got_error_set_errno(errcode,
1542 "pthread_mutex_lock");
1546 entry->idx = a->commits->ncommits;
1547 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1548 a->commits->ncommits++;
1550 if (*a->searching == TOG_SEARCH_FORWARD &&
1551 !*a->search_next_done) {
1553 err = match_commit(&have_match, id, commit, a->regex);
1557 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1560 errcode = pthread_mutex_unlock(&tog_mutex);
1561 if (errcode && err == NULL)
1562 err = got_error_set_errno(errcode,
1563 "pthread_mutex_unlock");
1566 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1572 select_commit(struct tog_log_view_state *s)
1574 struct commit_queue_entry *entry;
1577 entry = s->first_displayed_entry;
1579 if (ncommits == s->selected) {
1580 s->selected_entry = entry;
1583 entry = TAILQ_NEXT(entry, entry);
1588 static const struct got_error *
1589 draw_commits(struct tog_view *view)
1591 const struct got_error *err = NULL;
1592 struct tog_log_view_state *s = &view->state.log;
1593 struct commit_queue_entry *entry = s->selected_entry;
1594 const int limit = view->nlines;
1596 int ncommits, author_cols = 4;
1597 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1598 char *refs_str = NULL;
1600 struct tog_color *tc;
1601 static const size_t date_display_cols = 12;
1603 if (s->selected_entry &&
1604 !(view->searching && view->search_next_done == 0)) {
1605 struct got_reflist_head *refs;
1606 err = got_object_id_str(&id_str, s->selected_entry->id);
1609 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1610 s->selected_entry->id);
1612 err = build_refs_str(&refs_str, refs,
1613 s->selected_entry->id, s->repo);
1619 if (s->thread_args.commits_needed == 0)
1620 halfdelay(10); /* disable fast refresh */
1622 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1623 if (asprintf(&ncommits_str, " [%d/%d] %s",
1624 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1625 (view->searching && !view->search_next_done) ?
1626 "searching..." : "loading...") == -1) {
1627 err = got_error_from_errno("asprintf");
1631 const char *search_str = NULL;
1633 if (view->searching) {
1634 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1635 search_str = "no more matches";
1636 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1637 search_str = "no matches found";
1638 else if (!view->search_next_done)
1639 search_str = "searching...";
1642 if (asprintf(&ncommits_str, " [%d/%d] %s",
1643 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1644 search_str ? search_str :
1645 (refs_str ? refs_str : "")) == -1) {
1646 err = got_error_from_errno("asprintf");
1651 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1652 if (asprintf(&header, "commit %s %s%s",
1653 id_str ? id_str : "........................................",
1654 s->in_repo_path, ncommits_str) == -1) {
1655 err = got_error_from_errno("asprintf");
1659 } else if (asprintf(&header, "commit %s%s",
1660 id_str ? id_str : "........................................",
1661 ncommits_str) == -1) {
1662 err = got_error_from_errno("asprintf");
1666 err = format_line(&wline, &width, header, view->ncols, 0);
1670 werase(view->window);
1672 if (view_needs_focus_indication(view))
1673 wstandout(view->window);
1674 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1676 wattr_on(view->window,
1677 COLOR_PAIR(tc->colorpair), NULL);
1678 waddwstr(view->window, wline);
1680 wattr_off(view->window,
1681 COLOR_PAIR(tc->colorpair), NULL);
1682 while (width < view->ncols) {
1683 waddch(view->window, ' ');
1686 if (view_needs_focus_indication(view))
1687 wstandend(view->window);
1692 /* Grow author column size if necessary. */
1693 entry = s->first_displayed_entry;
1699 if (ncommits >= limit - 1)
1701 author = strdup(got_object_commit_get_author(entry->commit));
1702 if (author == NULL) {
1703 err = got_error_from_errno("strdup");
1706 err = format_author(&wauthor, &width, author, COLS,
1708 if (author_cols < width)
1709 author_cols = width;
1713 entry = TAILQ_NEXT(entry, entry);
1716 entry = s->first_displayed_entry;
1717 s->last_displayed_entry = s->first_displayed_entry;
1720 if (ncommits >= limit - 1)
1722 if (ncommits == s->selected)
1723 wstandout(view->window);
1724 err = draw_commit(view, entry->commit, entry->id,
1725 date_display_cols, author_cols);
1726 if (ncommits == s->selected)
1727 wstandend(view->window);
1731 s->last_displayed_entry = entry;
1732 entry = TAILQ_NEXT(entry, entry);
1747 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1749 struct commit_queue_entry *entry;
1752 entry = TAILQ_FIRST(&s->commits.head);
1753 if (s->first_displayed_entry == entry)
1756 entry = s->first_displayed_entry;
1757 while (entry && nscrolled < maxscroll) {
1758 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1760 s->first_displayed_entry = entry;
1766 static const struct got_error *
1767 trigger_log_thread(struct tog_view *view, int wait)
1769 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1772 halfdelay(1); /* fast refresh while loading commits */
1774 while (ta->commits_needed > 0 || ta->load_all) {
1775 if (ta->log_complete)
1778 /* Wake the log thread. */
1779 errcode = pthread_cond_signal(&ta->need_commits);
1781 return got_error_set_errno(errcode,
1782 "pthread_cond_signal");
1785 * The mutex will be released while the view loop waits
1786 * in wgetch(), at which time the log thread will run.
1791 /* Display progress update in log view. */
1792 show_log_view(view);
1796 /* Wait right here while next commit is being loaded. */
1797 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1799 return got_error_set_errno(errcode,
1800 "pthread_cond_wait");
1802 /* Display progress update in log view. */
1803 show_log_view(view);
1811 static const struct got_error *
1812 log_scroll_down(struct tog_view *view, int maxscroll)
1814 struct tog_log_view_state *s = &view->state.log;
1815 const struct got_error *err = NULL;
1816 struct commit_queue_entry *pentry;
1817 int nscrolled = 0, ncommits_needed;
1819 if (s->last_displayed_entry == NULL)
1822 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1823 if (s->commits.ncommits < ncommits_needed &&
1824 !s->thread_args.log_complete) {
1826 * Ask the log thread for required amount of commits.
1828 s->thread_args.commits_needed += maxscroll;
1829 err = trigger_log_thread(view, 1);
1835 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1839 s->last_displayed_entry = pentry;
1841 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1844 s->first_displayed_entry = pentry;
1845 } while (++nscrolled < maxscroll);
1850 static const struct got_error *
1851 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1852 struct got_commit_object *commit, struct got_object_id *commit_id,
1853 struct tog_view *log_view, struct got_repository *repo)
1855 const struct got_error *err;
1856 struct got_object_qid *parent_id;
1857 struct tog_view *diff_view;
1859 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1860 if (diff_view == NULL)
1861 return got_error_from_errno("view_open");
1863 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1864 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1865 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1867 *new_view = diff_view;
1871 static const struct got_error *
1872 tree_view_visit_subtree(struct tog_tree_view_state *s,
1873 struct got_tree_object *subtree)
1875 struct tog_parent_tree *parent;
1877 parent = calloc(1, sizeof(*parent));
1879 return got_error_from_errno("calloc");
1881 parent->tree = s->tree;
1882 parent->first_displayed_entry = s->first_displayed_entry;
1883 parent->selected_entry = s->selected_entry;
1884 parent->selected = s->selected;
1885 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1888 s->first_displayed_entry = NULL;
1892 static const struct got_error *
1893 tree_view_walk_path(struct tog_tree_view_state *s,
1894 struct got_object_id *commit_id, const char *path)
1896 const struct got_error *err = NULL;
1897 struct got_tree_object *tree = NULL;
1899 char *slash, *subpath = NULL;
1901 /* Walk the path and open corresponding tree objects. */
1904 struct got_tree_entry *te;
1905 struct got_object_id *tree_id;
1911 /* Ensure the correct subtree entry is selected. */
1912 slash = strchr(p, '/');
1914 te_name = strdup(p);
1916 te_name = strndup(p, slash - p);
1917 if (te_name == NULL) {
1918 err = got_error_from_errno("strndup");
1921 te = got_object_tree_find_entry(s->tree, te_name);
1923 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1928 s->first_displayed_entry = s->selected_entry = te;
1930 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1931 break; /* jump to this file's entry */
1933 slash = strchr(p, '/');
1935 subpath = strndup(path, slash - path);
1937 subpath = strdup(path);
1938 if (subpath == NULL) {
1939 err = got_error_from_errno("strdup");
1943 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1948 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1953 err = tree_view_visit_subtree(s, tree);
1955 got_object_tree_close(tree);
1969 static const struct got_error *
1970 browse_commit_tree(struct tog_view **new_view, int begin_x,
1971 struct commit_queue_entry *entry, const char *path,
1972 const char *head_ref_name, struct got_repository *repo)
1974 const struct got_error *err = NULL;
1975 struct tog_tree_view_state *s;
1976 struct tog_view *tree_view;
1978 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1979 if (tree_view == NULL)
1980 return got_error_from_errno("view_open");
1982 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
1985 s = &tree_view->state.tree;
1987 *new_view = tree_view;
1989 if (got_path_is_root_dir(path))
1992 return tree_view_walk_path(s, entry->id, path);
1995 static const struct got_error *
1996 block_signals_used_by_main_thread(void)
2001 if (sigemptyset(&sigset) == -1)
2002 return got_error_from_errno("sigemptyset");
2004 /* tog handles SIGWINCH and SIGCONT */
2005 if (sigaddset(&sigset, SIGWINCH) == -1)
2006 return got_error_from_errno("sigaddset");
2007 if (sigaddset(&sigset, SIGCONT) == -1)
2008 return got_error_from_errno("sigaddset");
2010 /* ncurses handles SIGTSTP */
2011 if (sigaddset(&sigset, SIGTSTP) == -1)
2012 return got_error_from_errno("sigaddset");
2014 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2016 return got_error_set_errno(errcode, "pthread_sigmask");
2022 log_thread(void *arg)
2024 const struct got_error *err = NULL;
2026 struct tog_log_thread_args *a = arg;
2029 err = block_signals_used_by_main_thread();
2033 while (!done && !err && !tog_sigpipe_received) {
2034 err = queue_commits(a);
2036 if (err->code != GOT_ERR_ITER_COMPLETED)
2040 } else if (a->commits_needed > 0 && !a->load_all)
2041 a->commits_needed--;
2043 errcode = pthread_mutex_lock(&tog_mutex);
2045 err = got_error_set_errno(errcode,
2046 "pthread_mutex_lock");
2048 } else if (*a->quit)
2050 else if (*a->first_displayed_entry == NULL) {
2051 *a->first_displayed_entry =
2052 TAILQ_FIRST(&a->commits->head);
2053 *a->selected_entry = *a->first_displayed_entry;
2056 errcode = pthread_cond_signal(&a->commit_loaded);
2058 err = got_error_set_errno(errcode,
2059 "pthread_cond_signal");
2060 pthread_mutex_unlock(&tog_mutex);
2065 a->commits_needed = 0;
2067 if (a->commits_needed == 0 && !a->load_all) {
2068 errcode = pthread_cond_wait(&a->need_commits,
2071 err = got_error_set_errno(errcode,
2072 "pthread_cond_wait");
2078 errcode = pthread_mutex_unlock(&tog_mutex);
2079 if (errcode && err == NULL)
2080 err = got_error_set_errno(errcode,
2081 "pthread_mutex_unlock");
2083 a->log_complete = 1;
2087 static const struct got_error *
2088 stop_log_thread(struct tog_log_view_state *s)
2090 const struct got_error *err = NULL;
2095 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2097 return got_error_set_errno(errcode,
2098 "pthread_cond_signal");
2099 errcode = pthread_mutex_unlock(&tog_mutex);
2101 return got_error_set_errno(errcode,
2102 "pthread_mutex_unlock");
2103 errcode = pthread_join(s->thread, (void **)&err);
2105 return got_error_set_errno(errcode, "pthread_join");
2106 errcode = pthread_mutex_lock(&tog_mutex);
2108 return got_error_set_errno(errcode,
2109 "pthread_mutex_lock");
2110 s->thread = 0; //NULL;
2113 if (s->thread_args.repo) {
2114 err = got_repo_close(s->thread_args.repo);
2115 s->thread_args.repo = NULL;
2118 if (s->thread_args.graph) {
2119 got_commit_graph_close(s->thread_args.graph);
2120 s->thread_args.graph = NULL;
2126 static const struct got_error *
2127 close_log_view(struct tog_view *view)
2129 const struct got_error *err = NULL;
2130 struct tog_log_view_state *s = &view->state.log;
2133 err = stop_log_thread(s);
2135 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2136 if (errcode && err == NULL)
2137 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2139 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2140 if (errcode && err == NULL)
2141 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2143 free_commits(&s->commits);
2144 free(s->in_repo_path);
2145 s->in_repo_path = NULL;
2148 free(s->head_ref_name);
2149 s->head_ref_name = NULL;
2153 static const struct got_error *
2154 search_start_log_view(struct tog_view *view)
2156 struct tog_log_view_state *s = &view->state.log;
2158 s->matched_entry = NULL;
2159 s->search_entry = NULL;
2163 static const struct got_error *
2164 search_next_log_view(struct tog_view *view)
2166 const struct got_error *err = NULL;
2167 struct tog_log_view_state *s = &view->state.log;
2168 struct commit_queue_entry *entry;
2170 /* Display progress update in log view. */
2171 show_log_view(view);
2175 if (s->search_entry) {
2177 errcode = pthread_mutex_unlock(&tog_mutex);
2179 return got_error_set_errno(errcode,
2180 "pthread_mutex_unlock");
2181 ch = wgetch(view->window);
2182 errcode = pthread_mutex_lock(&tog_mutex);
2184 return got_error_set_errno(errcode,
2185 "pthread_mutex_lock");
2186 if (ch == KEY_BACKSPACE) {
2187 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2190 if (view->searching == TOG_SEARCH_FORWARD)
2191 entry = TAILQ_NEXT(s->search_entry, entry);
2193 entry = TAILQ_PREV(s->search_entry,
2194 commit_queue_head, entry);
2195 } else if (s->matched_entry) {
2196 if (view->searching == TOG_SEARCH_FORWARD)
2197 entry = TAILQ_NEXT(s->matched_entry, entry);
2199 entry = TAILQ_PREV(s->matched_entry,
2200 commit_queue_head, entry);
2202 if (view->searching == TOG_SEARCH_FORWARD)
2203 entry = TAILQ_FIRST(&s->commits.head);
2205 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2211 if (entry == NULL) {
2212 if (s->thread_args.log_complete ||
2213 view->searching == TOG_SEARCH_BACKWARD) {
2214 view->search_next_done =
2215 (s->matched_entry == NULL ?
2216 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2217 s->search_entry = NULL;
2221 * Poke the log thread for more commits and return,
2222 * allowing the main loop to make progress. Search
2223 * will resume at s->search_entry once we come back.
2225 s->thread_args.commits_needed++;
2226 return trigger_log_thread(view, 0);
2229 err = match_commit(&have_match, entry->id, entry->commit,
2234 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2235 s->matched_entry = entry;
2239 s->search_entry = entry;
2240 if (view->searching == TOG_SEARCH_FORWARD)
2241 entry = TAILQ_NEXT(entry, entry);
2243 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2246 if (s->matched_entry) {
2247 int cur = s->selected_entry->idx;
2248 while (cur < s->matched_entry->idx) {
2249 err = input_log_view(NULL, view, KEY_DOWN);
2254 while (cur > s->matched_entry->idx) {
2255 err = input_log_view(NULL, view, KEY_UP);
2262 s->search_entry = NULL;
2267 static const struct got_error *
2268 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2269 struct got_repository *repo, const char *head_ref_name,
2270 const char *in_repo_path, int log_branches)
2272 const struct got_error *err = NULL;
2273 struct tog_log_view_state *s = &view->state.log;
2274 struct got_repository *thread_repo = NULL;
2275 struct got_commit_graph *thread_graph = NULL;
2278 if (in_repo_path != s->in_repo_path) {
2279 free(s->in_repo_path);
2280 s->in_repo_path = strdup(in_repo_path);
2281 if (s->in_repo_path == NULL)
2282 return got_error_from_errno("strdup");
2285 /* The commit queue only contains commits being displayed. */
2286 TAILQ_INIT(&s->commits.head);
2287 s->commits.ncommits = 0;
2290 if (head_ref_name) {
2291 s->head_ref_name = strdup(head_ref_name);
2292 if (s->head_ref_name == NULL) {
2293 err = got_error_from_errno("strdup");
2297 s->start_id = got_object_id_dup(start_id);
2298 if (s->start_id == NULL) {
2299 err = got_error_from_errno("got_object_id_dup");
2302 s->log_branches = log_branches;
2304 STAILQ_INIT(&s->colors);
2305 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2306 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2307 get_color_value("TOG_COLOR_COMMIT"));
2310 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2311 get_color_value("TOG_COLOR_AUTHOR"));
2313 free_colors(&s->colors);
2316 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2317 get_color_value("TOG_COLOR_DATE"));
2319 free_colors(&s->colors);
2324 view->show = show_log_view;
2325 view->input = input_log_view;
2326 view->close = close_log_view;
2327 view->search_start = search_start_log_view;
2328 view->search_next = search_next_log_view;
2330 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2333 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2337 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2338 s->repo, NULL, NULL);
2342 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2344 err = got_error_set_errno(errcode, "pthread_cond_init");
2347 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2349 err = got_error_set_errno(errcode, "pthread_cond_init");
2353 s->thread_args.commits_needed = view->nlines;
2354 s->thread_args.graph = thread_graph;
2355 s->thread_args.commits = &s->commits;
2356 s->thread_args.in_repo_path = s->in_repo_path;
2357 s->thread_args.start_id = s->start_id;
2358 s->thread_args.repo = thread_repo;
2359 s->thread_args.log_complete = 0;
2360 s->thread_args.quit = &s->quit;
2361 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2362 s->thread_args.selected_entry = &s->selected_entry;
2363 s->thread_args.searching = &view->searching;
2364 s->thread_args.search_next_done = &view->search_next_done;
2365 s->thread_args.regex = &view->regex;
2368 close_log_view(view);
2372 static const struct got_error *
2373 show_log_view(struct tog_view *view)
2375 const struct got_error *err;
2376 struct tog_log_view_state *s = &view->state.log;
2378 if (s->thread == 0) { //NULL) {
2379 int errcode = pthread_create(&s->thread, NULL, log_thread,
2382 return got_error_set_errno(errcode, "pthread_create");
2383 if (s->thread_args.commits_needed > 0) {
2384 err = trigger_log_thread(view, 1);
2390 return draw_commits(view);
2393 static const struct got_error *
2394 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2396 const struct got_error *err = NULL;
2397 struct tog_log_view_state *s = &view->state.log;
2398 struct tog_view *diff_view = NULL, *tree_view = NULL;
2399 struct tog_view *ref_view = NULL;
2400 struct commit_queue_entry *entry;
2403 if (s->thread_args.load_all) {
2404 if (ch == KEY_BACKSPACE)
2405 s->thread_args.load_all = 0;
2406 else if (s->thread_args.log_complete) {
2407 s->thread_args.load_all = 0;
2408 log_scroll_down(view, s->commits.ncommits);
2409 s->selected = MIN(view->nlines - 2,
2410 s->commits.ncommits - 1);
2425 if (s->first_displayed_entry == NULL)
2427 if (s->selected > 0)
2430 log_scroll_up(s, 1);
2436 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2441 if (s->first_displayed_entry == NULL)
2443 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2446 log_scroll_up(s, view->nlines - 1);
2454 if (s->first_displayed_entry == NULL)
2456 if (s->selected < MIN(view->nlines - 2,
2457 s->commits.ncommits - 1))
2460 err = log_scroll_down(view, 1);
2468 /* We don't know yet how many commits, so we're forced to
2469 * traverse them all. */
2470 if (!s->thread_args.log_complete) {
2471 s->thread_args.load_all = 1;
2472 return trigger_log_thread(view, 0);
2476 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2477 for (n = 0; n < view->nlines - 1; n++) {
2480 s->first_displayed_entry = entry;
2481 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2484 s->selected = n - 1;
2490 struct commit_queue_entry *first;
2491 first = s->first_displayed_entry;
2494 err = log_scroll_down(view, view->nlines - 1);
2497 if (first == s->first_displayed_entry &&
2498 s->selected < MIN(view->nlines - 2,
2499 s->commits.ncommits - 1)) {
2500 /* can't scroll further down */
2501 s->selected = MIN(view->nlines - 2,
2502 s->commits.ncommits - 1);
2508 if (s->selected > view->nlines - 2)
2509 s->selected = view->nlines - 2;
2510 if (s->selected > s->commits.ncommits - 1)
2511 s->selected = s->commits.ncommits - 1;
2513 if (s->commits.ncommits < view->nlines - 1 &&
2514 !s->thread_args.log_complete) {
2515 s->thread_args.commits_needed += (view->nlines - 1) -
2516 s->commits.ncommits;
2517 err = trigger_log_thread(view, 1);
2523 if (s->selected_entry == NULL)
2525 if (view_is_parent_view(view))
2526 begin_x = view_split_begin_x(view->begin_x);
2527 err = open_diff_view_for_commit(&diff_view, begin_x,
2528 s->selected_entry->commit, s->selected_entry->id,
2533 diff_view->focussed = 1;
2534 if (view_is_parent_view(view)) {
2535 err = view_close_child(view);
2538 view_set_child(view, diff_view);
2539 view->focus_child = 1;
2541 *new_view = diff_view;
2544 if (s->selected_entry == NULL)
2546 if (view_is_parent_view(view))
2547 begin_x = view_split_begin_x(view->begin_x);
2548 err = browse_commit_tree(&tree_view, begin_x,
2549 s->selected_entry, s->in_repo_path, s->head_ref_name,
2554 tree_view->focussed = 1;
2555 if (view_is_parent_view(view)) {
2556 err = view_close_child(view);
2559 view_set_child(view, tree_view);
2560 view->focus_child = 1;
2562 *new_view = tree_view;
2567 if (ch == KEY_BACKSPACE &&
2568 got_path_is_root_dir(s->in_repo_path))
2570 err = stop_log_thread(s);
2573 if (ch == KEY_BACKSPACE) {
2575 err = got_path_dirname(&parent_path, s->in_repo_path);
2578 free(s->in_repo_path);
2579 s->in_repo_path = parent_path;
2580 s->thread_args.in_repo_path = s->in_repo_path;
2581 } else if (ch == CTRL('l')) {
2582 struct got_object_id *start_id;
2583 err = got_repo_match_object_id(&start_id, NULL,
2584 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2585 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2589 s->start_id = start_id;
2590 s->thread_args.start_id = s->start_id;
2592 s->log_branches = !s->log_branches;
2594 err = got_repo_open(&s->thread_args.repo,
2595 got_repo_get_path(s->repo), NULL);
2599 err = tog_load_refs(s->repo);
2602 err = got_commit_graph_open(&s->thread_args.graph,
2603 s->in_repo_path, !s->log_branches);
2606 err = got_commit_graph_iter_start(s->thread_args.graph,
2607 s->start_id, s->repo, NULL, NULL);
2610 free_commits(&s->commits);
2611 s->first_displayed_entry = NULL;
2612 s->last_displayed_entry = NULL;
2613 s->selected_entry = NULL;
2615 s->thread_args.log_complete = 0;
2617 s->thread_args.commits_needed = view->nlines;
2620 if (view_is_parent_view(view))
2621 begin_x = view_split_begin_x(view->begin_x);
2622 ref_view = view_open(view->nlines, view->ncols,
2623 view->begin_y, begin_x, TOG_VIEW_REF);
2624 if (ref_view == NULL)
2625 return got_error_from_errno("view_open");
2626 err = open_ref_view(ref_view, s->repo);
2628 view_close(ref_view);
2632 ref_view->focussed = 1;
2633 if (view_is_parent_view(view)) {
2634 err = view_close_child(view);
2637 view_set_child(view, ref_view);
2638 view->focus_child = 1;
2640 *new_view = ref_view;
2649 static const struct got_error *
2650 apply_unveil(const char *repo_path, const char *worktree_path)
2652 const struct got_error *error;
2655 if (unveil("gmon.out", "rwc") != 0)
2656 return got_error_from_errno2("unveil", "gmon.out");
2658 if (repo_path && unveil(repo_path, "r") != 0)
2659 return got_error_from_errno2("unveil", repo_path);
2661 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2662 return got_error_from_errno2("unveil", worktree_path);
2664 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2665 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2667 error = got_privsep_unveil_exec_helpers();
2671 if (unveil(NULL, NULL) != 0)
2672 return got_error_from_errno("unveil");
2682 halfdelay(1); /* Do fast refresh while initial view is loading. */
2685 intrflush(stdscr, FALSE);
2686 keypad(stdscr, TRUE);
2688 if (getenv("TOG_COLORS") != NULL) {
2690 use_default_colors();
2692 signal(SIGWINCH, tog_sigwinch);
2693 signal(SIGPIPE, tog_sigpipe);
2694 signal(SIGCONT, tog_sigcont);
2697 static const struct got_error *
2698 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2699 struct got_repository *repo, struct got_worktree *worktree)
2701 const struct got_error *err = NULL;
2704 *in_repo_path = strdup("/");
2705 if (*in_repo_path == NULL)
2706 return got_error_from_errno("strdup");
2711 const char *prefix = got_worktree_get_path_prefix(worktree);
2714 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2717 if (asprintf(in_repo_path, "%s%s%s", prefix,
2718 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2720 err = got_error_from_errno("asprintf");
2721 *in_repo_path = NULL;
2725 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2730 static const struct got_error *
2731 cmd_log(int argc, char *argv[])
2733 const struct got_error *error;
2734 struct got_repository *repo = NULL;
2735 struct got_worktree *worktree = NULL;
2736 struct got_object_id *start_id = NULL;
2737 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2738 char *start_commit = NULL, *label = NULL;
2739 struct got_reference *ref = NULL;
2740 const char *head_ref_name = NULL;
2741 int ch, log_branches = 0;
2742 struct tog_view *view;
2744 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2750 start_commit = optarg;
2753 repo_path = realpath(optarg, NULL);
2754 if (repo_path == NULL)
2755 return got_error_from_errno2("realpath",
2770 if (repo_path == NULL) {
2771 cwd = getcwd(NULL, 0);
2773 return got_error_from_errno("getcwd");
2774 error = got_worktree_open(&worktree, cwd);
2775 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2779 strdup(got_worktree_get_repo_path(worktree));
2781 repo_path = strdup(cwd);
2782 if (repo_path == NULL) {
2783 error = got_error_from_errno("strdup");
2788 error = got_repo_open(&repo, repo_path, NULL);
2792 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2799 error = apply_unveil(got_repo_get_path(repo),
2800 worktree ? got_worktree_get_root_path(worktree) : NULL);
2804 /* already loaded by tog_log_with_path()? */
2805 if (TAILQ_EMPTY(&tog_refs)) {
2806 error = tog_load_refs(repo);
2811 if (start_commit == NULL) {
2812 error = got_repo_match_object_id(&start_id, &label,
2813 worktree ? got_worktree_get_head_ref_name(worktree) :
2814 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2817 head_ref_name = label;
2819 error = got_ref_open(&ref, repo, start_commit, 0);
2821 head_ref_name = got_ref_get_name(ref);
2822 else if (error->code != GOT_ERR_NOT_REF)
2824 error = got_repo_match_object_id(&start_id, NULL,
2825 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2830 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2832 error = got_error_from_errno("view_open");
2835 error = open_log_view(view, start_id, repo, head_ref_name,
2836 in_repo_path, log_branches);
2840 /* Release work tree lock. */
2841 got_worktree_close(worktree);
2844 error = view_loop(view);
2854 const struct got_error *close_err = got_repo_close(repo);
2859 got_worktree_close(worktree);
2868 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2869 "[-w] object1 object2\n", getprogname());
2874 match_line(const char *line, regex_t *regex, size_t nmatch,
2875 regmatch_t *regmatch)
2877 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2881 match_color(struct tog_colors *colors, const char *line)
2883 struct tog_color *tc = NULL;
2885 STAILQ_FOREACH(tc, colors, entry) {
2886 if (match_line(line, &tc->regex, 0, NULL))
2893 static const struct got_error *
2894 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2895 WINDOW *window, regmatch_t *regmatch)
2897 const struct got_error *err = NULL;
2904 s = strndup(line, regmatch->rm_so);
2906 return got_error_from_errno("strndup");
2908 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2913 waddwstr(window, wline);
2920 s = strndup(line + regmatch->rm_so,
2921 regmatch->rm_eo - regmatch->rm_so);
2923 err = got_error_from_errno("strndup");
2927 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2932 wattr_on(window, A_STANDOUT, NULL);
2933 waddwstr(window, wline);
2934 wattr_off(window, A_STANDOUT, NULL);
2941 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2942 err = format_line(&wline, &width,
2943 line + regmatch->rm_eo, wlimit, col_tab_align);
2946 waddwstr(window, wline);
2954 static const struct got_error *
2955 draw_file(struct tog_view *view, const char *header)
2957 struct tog_diff_view_state *s = &view->state.diff;
2958 regmatch_t *regmatch = &view->regmatch;
2959 const struct got_error *err;
2962 size_t linesize = 0;
2964 struct tog_color *tc;
2967 int max_lines = view->nlines;
2968 int nlines = s->nlines;
2971 line_offset = s->line_offsets[s->first_displayed_line - 1];
2972 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2973 return got_error_from_errno("fseek");
2975 werase(view->window);
2978 if (asprintf(&line, "[%d/%d] %s",
2979 s->first_displayed_line - 1 + s->selected_line, nlines,
2981 return got_error_from_errno("asprintf");
2982 err = format_line(&wline, &width, line, view->ncols, 0);
2987 if (view_needs_focus_indication(view))
2988 wstandout(view->window);
2989 waddwstr(view->window, wline);
2992 if (view_needs_focus_indication(view))
2993 wstandend(view->window);
2994 if (width <= view->ncols - 1)
2995 waddch(view->window, '\n');
3004 while (max_lines > 0 && nprinted < max_lines) {
3005 linelen = getline(&line, &linesize, s->f);
3006 if (linelen == -1) {
3012 return got_ferror(s->f, GOT_ERR_IO);
3015 tc = match_color(&s->colors, line);
3017 wattr_on(view->window,
3018 COLOR_PAIR(tc->colorpair), NULL);
3019 if (s->first_displayed_line + nprinted == s->matched_line &&
3020 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3021 err = add_matched_line(&width, line, view->ncols, 0,
3022 view->window, regmatch);
3028 err = format_line(&wline, &width, line, view->ncols, 0);
3033 waddwstr(view->window, wline);
3038 wattr_off(view->window,
3039 COLOR_PAIR(tc->colorpair), NULL);
3040 if (width <= view->ncols - 1)
3041 waddch(view->window, '\n');
3046 s->last_displayed_line = s->first_displayed_line +
3049 s->last_displayed_line = s->first_displayed_line;
3054 while (nprinted < view->nlines) {
3055 waddch(view->window, '\n');
3059 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3064 wstandout(view->window);
3065 waddwstr(view->window, wline);
3068 wstandend(view->window);
3075 get_datestr(time_t *time, char *datebuf)
3077 struct tm mytm, *tm;
3080 tm = gmtime_r(time, &mytm);
3083 s = asctime_r(tm, datebuf);
3086 p = strchr(s, '\n');
3092 static const struct got_error *
3093 get_changed_paths(struct got_pathlist_head *paths,
3094 struct got_commit_object *commit, struct got_repository *repo)
3096 const struct got_error *err = NULL;
3097 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3098 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3099 struct got_object_qid *qid;
3101 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3103 struct got_commit_object *pcommit;
3104 err = got_object_open_as_commit(&pcommit, repo,
3109 tree_id1 = got_object_id_dup(
3110 got_object_commit_get_tree_id(pcommit));
3111 if (tree_id1 == NULL) {
3112 got_object_commit_close(pcommit);
3113 return got_error_from_errno("got_object_id_dup");
3115 got_object_commit_close(pcommit);
3120 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3125 tree_id2 = got_object_commit_get_tree_id(commit);
3126 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3130 err = got_diff_tree(tree1, tree2, "", "", repo,
3131 got_diff_tree_collect_changed_paths, paths, 0);
3134 got_object_tree_close(tree1);
3136 got_object_tree_close(tree2);
3141 static const struct got_error *
3142 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3146 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3148 return got_error_from_errno("reallocarray");
3150 (*line_offsets)[*nlines] = off;
3155 static const struct got_error *
3156 write_commit_info(off_t **line_offsets, size_t *nlines,
3157 struct got_object_id *commit_id, struct got_reflist_head *refs,
3158 struct got_repository *repo, FILE *outfile)
3160 const struct got_error *err = NULL;
3161 char datebuf[26], *datestr;
3162 struct got_commit_object *commit;
3163 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3164 time_t committer_time;
3165 const char *author, *committer;
3166 char *refs_str = NULL;
3167 struct got_pathlist_head changed_paths;
3168 struct got_pathlist_entry *pe;
3172 TAILQ_INIT(&changed_paths);
3175 err = build_refs_str(&refs_str, refs, commit_id, repo);
3180 err = got_object_open_as_commit(&commit, repo, commit_id);
3184 err = got_object_id_str(&id_str, commit_id);
3186 err = got_error_from_errno("got_object_id_str");
3190 err = add_line_offset(line_offsets, nlines, 0);
3194 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3195 refs_str ? refs_str : "", refs_str ? ")" : "");
3197 err = got_error_from_errno("fprintf");
3201 err = add_line_offset(line_offsets, nlines, outoff);
3205 n = fprintf(outfile, "from: %s\n",
3206 got_object_commit_get_author(commit));
3208 err = got_error_from_errno("fprintf");
3212 err = add_line_offset(line_offsets, nlines, outoff);
3216 committer_time = got_object_commit_get_committer_time(commit);
3217 datestr = get_datestr(&committer_time, datebuf);
3219 n = fprintf(outfile, "date: %s UTC\n", datestr);
3221 err = got_error_from_errno("fprintf");
3225 err = add_line_offset(line_offsets, nlines, outoff);
3229 author = got_object_commit_get_author(commit);
3230 committer = got_object_commit_get_committer(commit);
3231 if (strcmp(author, committer) != 0) {
3232 n = fprintf(outfile, "via: %s\n", committer);
3234 err = got_error_from_errno("fprintf");
3238 err = add_line_offset(line_offsets, nlines, outoff);
3242 if (got_object_commit_get_nparents(commit) > 1) {
3243 const struct got_object_id_queue *parent_ids;
3244 struct got_object_qid *qid;
3246 parent_ids = got_object_commit_get_parent_ids(commit);
3247 STAILQ_FOREACH(qid, parent_ids, entry) {
3248 err = got_object_id_str(&id_str, qid->id);
3251 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3253 err = got_error_from_errno("fprintf");
3257 err = add_line_offset(line_offsets, nlines, outoff);
3265 err = got_object_commit_get_logmsg(&logmsg, commit);
3269 while ((line = strsep(&s, "\n")) != NULL) {
3270 n = fprintf(outfile, "%s\n", line);
3272 err = got_error_from_errno("fprintf");
3276 err = add_line_offset(line_offsets, nlines, outoff);
3281 err = get_changed_paths(&changed_paths, commit, repo);
3284 TAILQ_FOREACH(pe, &changed_paths, entry) {
3285 struct got_diff_changed_path *cp = pe->data;
3286 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3288 err = got_error_from_errno("fprintf");
3292 err = add_line_offset(line_offsets, nlines, outoff);
3295 free((char *)pe->path);
3299 fputc('\n', outfile);
3301 err = add_line_offset(line_offsets, nlines, outoff);
3303 got_pathlist_free(&changed_paths);
3307 got_object_commit_close(commit);
3309 free(*line_offsets);
3310 *line_offsets = NULL;
3316 static const struct got_error *
3317 create_diff(struct tog_diff_view_state *s)
3319 const struct got_error *err = NULL;
3323 free(s->line_offsets);
3324 s->line_offsets = malloc(sizeof(off_t));
3325 if (s->line_offsets == NULL)
3326 return got_error_from_errno("malloc");
3331 err = got_error_from_errno("got_opentemp");
3334 if (s->f && fclose(s->f) == EOF) {
3335 err = got_error_from_errno("fclose");
3341 err = got_object_get_type(&obj_type, s->repo, s->id1);
3343 err = got_object_get_type(&obj_type, s->repo, s->id2);
3348 case GOT_OBJ_TYPE_BLOB:
3349 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3350 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3351 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3353 case GOT_OBJ_TYPE_TREE:
3354 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3355 s->id1, s->id2, NULL, "", "", s->diff_context,
3356 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3358 case GOT_OBJ_TYPE_COMMIT: {
3359 const struct got_object_id_queue *parent_ids;
3360 struct got_object_qid *pid;
3361 struct got_commit_object *commit2;
3362 struct got_reflist_head *refs;
3364 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3367 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3368 /* Show commit info if we're diffing to a parent/root commit. */
3369 if (s->id1 == NULL) {
3370 err = write_commit_info(&s->line_offsets, &s->nlines,
3371 s->id2, refs, s->repo, s->f);
3375 parent_ids = got_object_commit_get_parent_ids(commit2);
3376 STAILQ_FOREACH(pid, parent_ids, entry) {
3377 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3378 err = write_commit_info(
3379 &s->line_offsets, &s->nlines,
3380 s->id2, refs, s->repo, s->f);
3387 got_object_commit_close(commit2);
3389 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3390 s->id1, s->id2, NULL, s->diff_context, s->ignore_whitespace,
3391 s->force_text_diff, s->repo, s->f);
3395 err = got_error(GOT_ERR_OBJ_TYPE);
3401 if (s->f && fflush(s->f) != 0 && err == NULL)
3402 err = got_error_from_errno("fflush");
3407 diff_view_indicate_progress(struct tog_view *view)
3409 mvwaddstr(view->window, 0, 0, "diffing...");
3414 static const struct got_error *
3415 search_start_diff_view(struct tog_view *view)
3417 struct tog_diff_view_state *s = &view->state.diff;
3419 s->matched_line = 0;
3423 static const struct got_error *
3424 search_next_diff_view(struct tog_view *view)
3426 struct tog_diff_view_state *s = &view->state.diff;
3429 size_t linesize = 0;
3432 if (!view->searching) {
3433 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3437 if (s->matched_line) {
3438 if (view->searching == TOG_SEARCH_FORWARD)
3439 lineno = s->matched_line + 1;
3441 lineno = s->matched_line - 1;
3443 if (view->searching == TOG_SEARCH_FORWARD)
3452 if (lineno <= 0 || lineno > s->nlines) {
3453 if (s->matched_line == 0) {
3454 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3458 if (view->searching == TOG_SEARCH_FORWARD)
3464 offset = s->line_offsets[lineno - 1];
3465 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3467 return got_error_from_errno("fseeko");
3469 linelen = getline(&line, &linesize, s->f);
3470 if (linelen != -1 &&
3471 match_line(line, &view->regex, 1, &view->regmatch)) {
3472 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3473 s->matched_line = lineno;
3476 if (view->searching == TOG_SEARCH_FORWARD)
3483 if (s->matched_line) {
3484 s->first_displayed_line = s->matched_line;
3485 s->selected_line = 1;
3491 static const struct got_error *
3492 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3493 struct got_object_id *id2, const char *label1, const char *label2,
3494 int diff_context, int ignore_whitespace, int force_text_diff,
3495 struct tog_view *log_view, struct got_repository *repo)
3497 const struct got_error *err;
3498 struct tog_diff_view_state *s = &view->state.diff;
3500 if (id1 != NULL && id2 != NULL) {
3502 err = got_object_get_type(&type1, repo, id1);
3505 err = got_object_get_type(&type2, repo, id2);
3510 return got_error(GOT_ERR_OBJ_TYPE);
3512 s->first_displayed_line = 1;
3513 s->last_displayed_line = view->nlines;
3514 s->selected_line = 1;
3522 s->id1 = got_object_id_dup(id1);
3524 return got_error_from_errno("got_object_id_dup");
3528 s->id2 = got_object_id_dup(id2);
3529 if (s->id2 == NULL) {
3532 return got_error_from_errno("got_object_id_dup");
3535 s->first_displayed_line = 1;
3536 s->last_displayed_line = view->nlines;
3537 s->diff_context = diff_context;
3538 s->ignore_whitespace = ignore_whitespace;
3539 s->force_text_diff = force_text_diff;
3540 s->log_view = log_view;
3543 STAILQ_INIT(&s->colors);
3544 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3545 err = add_color(&s->colors,
3546 "^-", TOG_COLOR_DIFF_MINUS,
3547 get_color_value("TOG_COLOR_DIFF_MINUS"));
3550 err = add_color(&s->colors, "^\\+",
3551 TOG_COLOR_DIFF_PLUS,
3552 get_color_value("TOG_COLOR_DIFF_PLUS"));
3554 free_colors(&s->colors);
3557 err = add_color(&s->colors,
3558 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3559 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3561 free_colors(&s->colors);
3565 err = add_color(&s->colors,
3566 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3567 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3568 get_color_value("TOG_COLOR_DIFF_META"));
3570 free_colors(&s->colors);
3574 err = add_color(&s->colors,
3575 "^(from|via): ", TOG_COLOR_AUTHOR,
3576 get_color_value("TOG_COLOR_AUTHOR"));
3578 free_colors(&s->colors);
3582 err = add_color(&s->colors,
3583 "^date: ", TOG_COLOR_DATE,
3584 get_color_value("TOG_COLOR_DATE"));
3586 free_colors(&s->colors);
3591 if (log_view && view_is_splitscreen(view))
3592 show_log_view(log_view); /* draw vborder */
3593 diff_view_indicate_progress(view);
3595 s->line_offsets = NULL;
3597 err = create_diff(s);
3603 free_colors(&s->colors);
3607 view->show = show_diff_view;
3608 view->input = input_diff_view;
3609 view->close = close_diff_view;
3610 view->search_start = search_start_diff_view;
3611 view->search_next = search_next_diff_view;
3616 static const struct got_error *
3617 close_diff_view(struct tog_view *view)
3619 const struct got_error *err = NULL;
3620 struct tog_diff_view_state *s = &view->state.diff;
3626 if (s->f && fclose(s->f) == EOF)
3627 err = got_error_from_errno("fclose");
3628 free_colors(&s->colors);
3629 free(s->line_offsets);
3630 s->line_offsets = NULL;
3635 static const struct got_error *
3636 show_diff_view(struct tog_view *view)
3638 const struct got_error *err;
3639 struct tog_diff_view_state *s = &view->state.diff;
3640 char *id_str1 = NULL, *id_str2, *header;
3641 const char *label1, *label2;
3644 err = got_object_id_str(&id_str1, s->id1);
3647 label1 = s->label1 ? : id_str1;
3649 label1 = "/dev/null";
3651 err = got_object_id_str(&id_str2, s->id2);
3654 label2 = s->label2 ? : id_str2;
3656 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3657 err = got_error_from_errno("asprintf");
3665 err = draw_file(view, header);
3670 static const struct got_error *
3671 set_selected_commit(struct tog_diff_view_state *s,
3672 struct commit_queue_entry *entry)
3674 const struct got_error *err;
3675 const struct got_object_id_queue *parent_ids;
3676 struct got_commit_object *selected_commit;
3677 struct got_object_qid *pid;
3680 s->id2 = got_object_id_dup(entry->id);
3682 return got_error_from_errno("got_object_id_dup");
3684 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3687 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3689 pid = STAILQ_FIRST(parent_ids);
3690 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3691 got_object_commit_close(selected_commit);
3695 static const struct got_error *
3696 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3698 const struct got_error *err = NULL;
3699 struct tog_diff_view_state *s = &view->state.diff;
3700 struct tog_log_view_state *ls;
3701 struct commit_queue_entry *old_selected_entry;
3703 size_t linesize = 0;
3711 s->force_text_diff = !s->force_text_diff;
3713 s->ignore_whitespace = !s->ignore_whitespace;
3714 wclear(view->window);
3715 s->first_displayed_line = 1;
3716 s->last_displayed_line = view->nlines;
3717 diff_view_indicate_progress(view);
3718 err = create_diff(s);
3722 s->first_displayed_line = 1;
3729 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3735 if (s->first_displayed_line > 1)
3736 s->first_displayed_line--;
3740 if (s->first_displayed_line == 1)
3743 while (i++ < view->nlines - 1 &&
3744 s->first_displayed_line > 1)
3745 s->first_displayed_line--;
3751 s->first_displayed_line++;
3759 while (!s->eof && i++ < view->nlines - 1) {
3760 linelen = getline(&line, &linesize, s->f);
3761 s->first_displayed_line++;
3762 if (linelen == -1) {
3766 err = got_ferror(s->f, GOT_ERR_IO);
3773 if (s->diff_context > 0) {
3775 diff_view_indicate_progress(view);
3776 err = create_diff(s);
3777 if (s->first_displayed_line + view->nlines - 1 >
3779 s->first_displayed_line = 1;
3780 s->last_displayed_line = view->nlines;
3785 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3787 diff_view_indicate_progress(view);
3788 err = create_diff(s);
3793 if (s->log_view == NULL)
3795 ls = &s->log_view->state.log;
3796 old_selected_entry = ls->selected_entry;
3798 err = input_log_view(NULL, s->log_view, KEY_UP);
3802 if (old_selected_entry == ls->selected_entry)
3805 err = set_selected_commit(s, ls->selected_entry);
3809 s->first_displayed_line = 1;
3810 s->last_displayed_line = view->nlines;
3812 diff_view_indicate_progress(view);
3813 err = create_diff(s);
3817 if (s->log_view == NULL)
3819 ls = &s->log_view->state.log;
3820 old_selected_entry = ls->selected_entry;
3822 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3826 if (old_selected_entry == ls->selected_entry)
3829 err = set_selected_commit(s, ls->selected_entry);
3833 s->first_displayed_line = 1;
3834 s->last_displayed_line = view->nlines;
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);
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;
4390 got_object_blob_close(blob);
4397 static const struct got_error *
4398 open_blame_view(struct tog_view *view, char *path,
4399 struct got_object_id *commit_id, struct got_repository *repo)
4401 const struct got_error *err = NULL;
4402 struct tog_blame_view_state *s = &view->state.blame;
4404 STAILQ_INIT(&s->blamed_commits);
4406 s->path = strdup(path);
4407 if (s->path == NULL)
4408 return got_error_from_errno("strdup");
4410 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4416 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4417 s->first_displayed_line = 1;
4418 s->last_displayed_line = view->nlines;
4419 s->selected_line = 1;
4420 s->blame_complete = 0;
4422 s->commit_id = commit_id;
4423 memset(&s->blame, 0, sizeof(s->blame));
4425 STAILQ_INIT(&s->colors);
4426 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4427 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4428 get_color_value("TOG_COLOR_COMMIT"));
4433 view->show = show_blame_view;
4434 view->input = input_blame_view;
4435 view->close = close_blame_view;
4436 view->search_start = search_start_blame_view;
4437 view->search_next = search_next_blame_view;
4439 return run_blame(view);
4442 static const struct got_error *
4443 close_blame_view(struct tog_view *view)
4445 const struct got_error *err = NULL;
4446 struct tog_blame_view_state *s = &view->state.blame;
4448 if (s->blame.thread)
4449 err = stop_blame(&s->blame);
4451 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4452 struct got_object_qid *blamed_commit;
4453 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4454 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4455 got_object_qid_free(blamed_commit);
4459 free_colors(&s->colors);
4464 static const struct got_error *
4465 search_start_blame_view(struct tog_view *view)
4467 struct tog_blame_view_state *s = &view->state.blame;
4469 s->matched_line = 0;
4473 static const struct got_error *
4474 search_next_blame_view(struct tog_view *view)
4476 struct tog_blame_view_state *s = &view->state.blame;
4479 size_t linesize = 0;
4482 if (!view->searching) {
4483 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4487 if (s->matched_line) {
4488 if (view->searching == TOG_SEARCH_FORWARD)
4489 lineno = s->matched_line + 1;
4491 lineno = s->matched_line - 1;
4493 if (view->searching == TOG_SEARCH_FORWARD)
4496 lineno = s->blame.nlines;
4502 if (lineno <= 0 || lineno > s->blame.nlines) {
4503 if (s->matched_line == 0) {
4504 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4508 if (view->searching == TOG_SEARCH_FORWARD)
4511 lineno = s->blame.nlines;
4514 offset = s->blame.line_offsets[lineno - 1];
4515 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4517 return got_error_from_errno("fseeko");
4519 linelen = getline(&line, &linesize, s->blame.f);
4520 if (linelen != -1 &&
4521 match_line(line, &view->regex, 1, &view->regmatch)) {
4522 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4523 s->matched_line = lineno;
4526 if (view->searching == TOG_SEARCH_FORWARD)
4533 if (s->matched_line) {
4534 s->first_displayed_line = s->matched_line;
4535 s->selected_line = 1;
4541 static const struct got_error *
4542 show_blame_view(struct tog_view *view)
4544 const struct got_error *err = NULL;
4545 struct tog_blame_view_state *s = &view->state.blame;
4548 if (s->blame.thread == 0 && !s->blame_complete) {
4549 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4550 &s->blame.thread_args);
4552 return got_error_set_errno(errcode, "pthread_create");
4554 halfdelay(1); /* fast refresh while annotating */
4557 if (s->blame_complete)
4558 halfdelay(10); /* disable fast refresh */
4560 err = draw_blame(view);
4566 static const struct got_error *
4567 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4569 const struct got_error *err = NULL, *thread_err = NULL;
4570 struct tog_view *diff_view;
4571 struct tog_blame_view_state *s = &view->state.blame;
4580 s->selected_line = 1;
4581 s->first_displayed_line = 1;
4585 if (s->blame.nlines < view->nlines - 2) {
4586 s->selected_line = s->blame.nlines;
4587 s->first_displayed_line = 1;
4589 s->selected_line = view->nlines - 2;
4590 s->first_displayed_line = s->blame.nlines -
4597 if (s->selected_line > 1)
4599 else if (s->selected_line == 1 &&
4600 s->first_displayed_line > 1)
4601 s->first_displayed_line--;
4605 if (s->first_displayed_line == 1) {
4606 s->selected_line = 1;
4609 if (s->first_displayed_line > view->nlines - 2)
4610 s->first_displayed_line -=
4613 s->first_displayed_line = 1;
4618 if (s->selected_line < view->nlines - 2 &&
4619 s->first_displayed_line +
4620 s->selected_line <= s->blame.nlines)
4622 else if (s->last_displayed_line <
4624 s->first_displayed_line++;
4628 struct got_object_id *id = NULL;
4629 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4630 s->first_displayed_line, s->selected_line);
4634 struct got_commit_object *commit;
4635 struct got_object_qid *pid;
4636 struct got_object_id *blob_id = NULL;
4638 err = got_object_open_as_commit(&commit,
4643 got_object_commit_get_parent_ids(commit));
4645 got_object_commit_close(commit);
4648 /* Check if path history ends here. */
4649 err = got_object_id_by_path(&blob_id, s->repo,
4652 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4654 got_object_commit_close(commit);
4657 err = got_object_get_type(&obj_type, s->repo,
4660 /* Can't blame non-blob type objects. */
4661 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4662 got_object_commit_close(commit);
4665 err = got_object_qid_alloc(&s->blamed_commit,
4667 got_object_commit_close(commit);
4669 if (got_object_id_cmp(id,
4670 s->blamed_commit->id) == 0)
4672 err = got_object_qid_alloc(&s->blamed_commit,
4678 thread_err = stop_blame(&s->blame);
4682 STAILQ_INSERT_HEAD(&s->blamed_commits,
4683 s->blamed_commit, entry);
4684 err = run_blame(view);
4690 struct got_object_qid *first;
4691 first = STAILQ_FIRST(&s->blamed_commits);
4692 if (!got_object_id_cmp(first->id, s->commit_id))
4695 thread_err = stop_blame(&s->blame);
4699 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4700 got_object_qid_free(s->blamed_commit);
4702 STAILQ_FIRST(&s->blamed_commits);
4703 err = run_blame(view);
4710 struct got_object_id *id = NULL;
4711 struct got_object_qid *pid;
4712 struct got_commit_object *commit = NULL;
4713 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4714 s->first_displayed_line, s->selected_line);
4717 err = got_object_open_as_commit(&commit, s->repo, id);
4721 got_object_commit_get_parent_ids(commit));
4722 if (view_is_parent_view(view))
4723 begin_x = view_split_begin_x(view->begin_x);
4724 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4725 if (diff_view == NULL) {
4726 got_object_commit_close(commit);
4727 err = got_error_from_errno("view_open");
4730 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4731 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4732 got_object_commit_close(commit);
4734 view_close(diff_view);
4738 diff_view->focussed = 1;
4739 if (view_is_parent_view(view)) {
4740 err = view_close_child(view);
4743 view_set_child(view, diff_view);
4744 view->focus_child = 1;
4746 *new_view = diff_view;
4754 if (s->last_displayed_line >= s->blame.nlines &&
4755 s->selected_line >= MIN(s->blame.nlines,
4756 view->nlines - 2)) {
4759 if (s->last_displayed_line >= s->blame.nlines &&
4760 s->selected_line < view->nlines - 2) {
4761 s->selected_line = MIN(s->blame.nlines,
4765 if (s->last_displayed_line + view->nlines - 2
4767 s->first_displayed_line +=
4770 s->first_displayed_line =
4775 if (s->selected_line > view->nlines - 2) {
4776 s->selected_line = MIN(s->blame.nlines,
4783 return thread_err ? thread_err : err;
4786 static const struct got_error *
4787 cmd_blame(int argc, char *argv[])
4789 const struct got_error *error;
4790 struct got_repository *repo = NULL;
4791 struct got_worktree *worktree = NULL;
4792 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4793 char *link_target = NULL;
4794 struct got_object_id *commit_id = NULL;
4795 char *commit_id_str = NULL;
4797 struct tog_view *view;
4799 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4802 commit_id_str = optarg;
4805 repo_path = realpath(optarg, NULL);
4806 if (repo_path == NULL)
4807 return got_error_from_errno2("realpath",
4822 if (repo_path == NULL) {
4823 cwd = getcwd(NULL, 0);
4825 return got_error_from_errno("getcwd");
4826 error = got_worktree_open(&worktree, cwd);
4827 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4831 strdup(got_worktree_get_repo_path(worktree));
4833 repo_path = strdup(cwd);
4834 if (repo_path == NULL) {
4835 error = got_error_from_errno("strdup");
4840 error = got_repo_open(&repo, repo_path, NULL);
4844 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4851 error = apply_unveil(got_repo_get_path(repo), NULL);
4855 error = tog_load_refs(repo);
4859 if (commit_id_str == NULL) {
4860 struct got_reference *head_ref;
4861 error = got_ref_open(&head_ref, repo, worktree ?
4862 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4865 error = got_ref_resolve(&commit_id, repo, head_ref);
4866 got_ref_close(head_ref);
4868 error = got_repo_match_object_id(&commit_id, NULL,
4869 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4874 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4876 error = got_error_from_errno("view_open");
4880 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4885 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4890 /* Release work tree lock. */
4891 got_worktree_close(worktree);
4894 error = view_loop(view);
4902 got_worktree_close(worktree);
4904 const struct got_error *close_err = got_repo_close(repo);
4912 static const struct got_error *
4913 draw_tree_entries(struct tog_view *view, const char *parent_path)
4915 struct tog_tree_view_state *s = &view->state.tree;
4916 const struct got_error *err = NULL;
4917 struct got_tree_entry *te;
4919 struct tog_color *tc;
4920 int width, n, i, nentries;
4921 int limit = view->nlines;
4925 werase(view->window);
4930 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4933 if (view_needs_focus_indication(view))
4934 wstandout(view->window);
4935 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4937 wattr_on(view->window,
4938 COLOR_PAIR(tc->colorpair), NULL);
4939 waddwstr(view->window, wline);
4941 wattr_off(view->window,
4942 COLOR_PAIR(tc->colorpair), NULL);
4943 if (view_needs_focus_indication(view))
4944 wstandend(view->window);
4947 if (width < view->ncols - 1)
4948 waddch(view->window, '\n');
4951 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4954 waddwstr(view->window, wline);
4957 if (width < view->ncols - 1)
4958 waddch(view->window, '\n');
4961 waddch(view->window, '\n');
4965 if (s->first_displayed_entry == NULL) {
4966 te = got_object_tree_get_first_entry(s->tree);
4967 if (s->selected == 0) {
4969 wstandout(view->window);
4970 s->selected_entry = NULL;
4972 waddstr(view->window, " ..\n"); /* parent directory */
4973 if (s->selected == 0 && view->focussed)
4974 wstandend(view->window);
4981 te = s->first_displayed_entry;
4984 nentries = got_object_tree_get_nentries(s->tree);
4985 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4986 char *line = NULL, *id_str = NULL, *link_target = NULL;
4987 const char *modestr = "";
4990 te = got_object_tree_get_entry(s->tree, i);
4991 mode = got_tree_entry_get_mode(te);
4994 err = got_object_id_str(&id_str,
4995 got_tree_entry_get_id(te));
4997 return got_error_from_errno(
4998 "got_object_id_str");
5000 if (got_object_tree_entry_is_submodule(te))
5002 else if (S_ISLNK(mode)) {
5005 err = got_tree_entry_get_symlink_target(&link_target,
5011 for (i = 0; i < strlen(link_target); i++) {
5012 if (!isprint((unsigned char)link_target[i]))
5013 link_target[i] = '?';
5017 else if (S_ISDIR(mode))
5019 else if (mode & S_IXUSR)
5021 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5022 got_tree_entry_get_name(te), modestr,
5023 link_target ? " -> ": "",
5024 link_target ? link_target : "") == -1) {
5027 return got_error_from_errno("asprintf");
5031 err = format_line(&wline, &width, line, view->ncols, 0);
5036 if (n == s->selected) {
5038 wstandout(view->window);
5039 s->selected_entry = te;
5041 tc = match_color(&s->colors, line);
5043 wattr_on(view->window,
5044 COLOR_PAIR(tc->colorpair), NULL);
5045 waddwstr(view->window, wline);
5047 wattr_off(view->window,
5048 COLOR_PAIR(tc->colorpair), NULL);
5049 if (width < view->ncols - 1)
5050 waddch(view->window, '\n');
5051 if (n == s->selected && view->focussed)
5052 wstandend(view->window);
5058 s->last_displayed_entry = te;
5067 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5069 struct got_tree_entry *te;
5070 int isroot = s->tree == s->root;
5073 if (s->first_displayed_entry == NULL)
5076 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5077 while (i++ < maxscroll) {
5080 s->first_displayed_entry = NULL;
5083 s->first_displayed_entry = te;
5084 te = got_tree_entry_get_prev(s->tree, te);
5089 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5091 struct got_tree_entry *next, *last;
5094 if (s->first_displayed_entry)
5095 next = got_tree_entry_get_next(s->tree,
5096 s->first_displayed_entry);
5098 next = got_object_tree_get_first_entry(s->tree);
5100 last = s->last_displayed_entry;
5101 while (next && last && n++ < maxscroll) {
5102 last = got_tree_entry_get_next(s->tree, last);
5104 s->first_displayed_entry = next;
5105 next = got_tree_entry_get_next(s->tree, next);
5110 static const struct got_error *
5111 tree_entry_path(char **path, struct tog_parent_trees *parents,
5112 struct got_tree_entry *te)
5114 const struct got_error *err = NULL;
5115 struct tog_parent_tree *pt;
5116 size_t len = 2; /* for leading slash and NUL */
5118 TAILQ_FOREACH(pt, parents, entry)
5119 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5122 len += strlen(got_tree_entry_get_name(te));
5124 *path = calloc(1, len);
5126 return got_error_from_errno("calloc");
5129 pt = TAILQ_LAST(parents, tog_parent_trees);
5131 const char *name = got_tree_entry_get_name(pt->selected_entry);
5132 if (strlcat(*path, name, len) >= len) {
5133 err = got_error(GOT_ERR_NO_SPACE);
5136 if (strlcat(*path, "/", len) >= len) {
5137 err = got_error(GOT_ERR_NO_SPACE);
5140 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5143 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5144 err = got_error(GOT_ERR_NO_SPACE);
5156 static const struct got_error *
5157 blame_tree_entry(struct tog_view **new_view, int begin_x,
5158 struct got_tree_entry *te, struct tog_parent_trees *parents,
5159 struct got_object_id *commit_id, struct got_repository *repo)
5161 const struct got_error *err = NULL;
5163 struct tog_view *blame_view;
5167 err = tree_entry_path(&path, parents, te);
5171 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5172 if (blame_view == NULL) {
5173 err = got_error_from_errno("view_open");
5177 err = open_blame_view(blame_view, path, commit_id, repo);
5179 if (err->code == GOT_ERR_CANCELLED)
5181 view_close(blame_view);
5183 *new_view = blame_view;
5189 static const struct got_error *
5190 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5191 struct tog_tree_view_state *s)
5193 struct tog_view *log_view;
5194 const struct got_error *err = NULL;
5199 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5200 if (log_view == NULL)
5201 return got_error_from_errno("view_open");
5203 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5207 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5210 view_close(log_view);
5212 *new_view = log_view;
5217 static const struct got_error *
5218 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5219 const char *head_ref_name, struct got_repository *repo)
5221 const struct got_error *err = NULL;
5222 char *commit_id_str = NULL;
5223 struct tog_tree_view_state *s = &view->state.tree;
5224 struct got_commit_object *commit = NULL;
5226 TAILQ_INIT(&s->parents);
5227 STAILQ_INIT(&s->colors);
5229 s->commit_id = got_object_id_dup(commit_id);
5230 if (s->commit_id == NULL)
5231 return got_error_from_errno("got_object_id_dup");
5233 err = got_object_open_as_commit(&commit, repo, commit_id);
5238 * The root is opened here and will be closed when the view is closed.
5239 * Any visited subtrees and their path-wise parents are opened and
5242 err = got_object_open_as_tree(&s->root, repo,
5243 got_object_commit_get_tree_id(commit));
5248 err = got_object_id_str(&commit_id_str, commit_id);
5252 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5253 err = got_error_from_errno("asprintf");
5257 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5258 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5259 if (head_ref_name) {
5260 s->head_ref_name = strdup(head_ref_name);
5261 if (s->head_ref_name == NULL) {
5262 err = got_error_from_errno("strdup");
5268 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5269 err = add_color(&s->colors, "\\$$",
5270 TOG_COLOR_TREE_SUBMODULE,
5271 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5274 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5275 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5278 err = add_color(&s->colors, "/$",
5279 TOG_COLOR_TREE_DIRECTORY,
5280 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5284 err = add_color(&s->colors, "\\*$",
5285 TOG_COLOR_TREE_EXECUTABLE,
5286 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5290 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5291 get_color_value("TOG_COLOR_COMMIT"));
5296 view->show = show_tree_view;
5297 view->input = input_tree_view;
5298 view->close = close_tree_view;
5299 view->search_start = search_start_tree_view;
5300 view->search_next = search_next_tree_view;
5302 free(commit_id_str);
5304 got_object_commit_close(commit);
5306 close_tree_view(view);
5310 static const struct got_error *
5311 close_tree_view(struct tog_view *view)
5313 struct tog_tree_view_state *s = &view->state.tree;
5315 free_colors(&s->colors);
5316 free(s->tree_label);
5317 s->tree_label = NULL;
5319 s->commit_id = NULL;
5320 free(s->head_ref_name);
5321 s->head_ref_name = NULL;
5322 while (!TAILQ_EMPTY(&s->parents)) {
5323 struct tog_parent_tree *parent;
5324 parent = TAILQ_FIRST(&s->parents);
5325 TAILQ_REMOVE(&s->parents, parent, entry);
5326 if (parent->tree != s->root)
5327 got_object_tree_close(parent->tree);
5331 if (s->tree != NULL && s->tree != s->root)
5332 got_object_tree_close(s->tree);
5334 got_object_tree_close(s->root);
5338 static const struct got_error *
5339 search_start_tree_view(struct tog_view *view)
5341 struct tog_tree_view_state *s = &view->state.tree;
5343 s->matched_entry = NULL;
5348 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5350 regmatch_t regmatch;
5352 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5356 static const struct got_error *
5357 search_next_tree_view(struct tog_view *view)
5359 struct tog_tree_view_state *s = &view->state.tree;
5360 struct got_tree_entry *te = NULL;
5362 if (!view->searching) {
5363 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5367 if (s->matched_entry) {
5368 if (view->searching == TOG_SEARCH_FORWARD) {
5369 if (s->selected_entry)
5370 te = got_tree_entry_get_next(s->tree,
5373 te = got_object_tree_get_first_entry(s->tree);
5375 if (s->selected_entry == NULL)
5376 te = got_object_tree_get_last_entry(s->tree);
5378 te = got_tree_entry_get_prev(s->tree,
5382 if (view->searching == TOG_SEARCH_FORWARD)
5383 te = got_object_tree_get_first_entry(s->tree);
5385 te = got_object_tree_get_last_entry(s->tree);
5390 if (s->matched_entry == NULL) {
5391 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5394 if (view->searching == TOG_SEARCH_FORWARD)
5395 te = got_object_tree_get_first_entry(s->tree);
5397 te = got_object_tree_get_last_entry(s->tree);
5400 if (match_tree_entry(te, &view->regex)) {
5401 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5402 s->matched_entry = te;
5406 if (view->searching == TOG_SEARCH_FORWARD)
5407 te = got_tree_entry_get_next(s->tree, te);
5409 te = got_tree_entry_get_prev(s->tree, te);
5412 if (s->matched_entry) {
5413 s->first_displayed_entry = s->matched_entry;
5420 static const struct got_error *
5421 show_tree_view(struct tog_view *view)
5423 const struct got_error *err = NULL;
5424 struct tog_tree_view_state *s = &view->state.tree;
5427 err = tree_entry_path(&parent_path, &s->parents, NULL);
5431 err = draw_tree_entries(view, parent_path);
5438 static const struct got_error *
5439 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5441 const struct got_error *err = NULL;
5442 struct tog_tree_view_state *s = &view->state.tree;
5443 struct tog_view *log_view, *ref_view;
5444 struct got_tree_entry *te;
5449 s->show_ids = !s->show_ids;
5452 if (!s->selected_entry)
5454 if (view_is_parent_view(view))
5455 begin_x = view_split_begin_x(view->begin_x);
5456 err = log_selected_tree_entry(&log_view, begin_x, s);
5458 log_view->focussed = 1;
5459 if (view_is_parent_view(view)) {
5460 err = view_close_child(view);
5463 view_set_child(view, log_view);
5464 view->focus_child = 1;
5466 *new_view = log_view;
5469 if (view_is_parent_view(view))
5470 begin_x = view_split_begin_x(view->begin_x);
5471 ref_view = view_open(view->nlines, view->ncols,
5472 view->begin_y, begin_x, TOG_VIEW_REF);
5473 if (ref_view == NULL)
5474 return got_error_from_errno("view_open");
5475 err = open_ref_view(ref_view, s->repo);
5477 view_close(ref_view);
5481 ref_view->focussed = 1;
5482 if (view_is_parent_view(view)) {
5483 err = view_close_child(view);
5486 view_set_child(view, ref_view);
5487 view->focus_child = 1;
5489 *new_view = ref_view;
5494 if (s->tree == s->root)
5495 s->first_displayed_entry =
5496 got_object_tree_get_first_entry(s->tree);
5498 s->first_displayed_entry = NULL;
5503 te = got_object_tree_get_last_entry(s->tree);
5504 for (n = 0; n < view->nlines - 3; n++) {
5506 if(s->tree != s->root) {
5507 s->first_displayed_entry = NULL;
5512 s->first_displayed_entry = te;
5513 te = got_tree_entry_get_prev(s->tree, te);
5516 s->selected = n - 1;
5521 if (s->selected > 0) {
5525 tree_scroll_up(s, 1);
5529 if (s->tree == s->root) {
5530 if (got_object_tree_get_first_entry(s->tree) ==
5531 s->first_displayed_entry)
5534 if (s->first_displayed_entry == NULL)
5537 tree_scroll_up(s, MAX(0, view->nlines - 3));
5542 if (s->selected < s->ndisplayed - 1) {
5546 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5548 /* can't scroll any further */
5550 tree_scroll_down(s, 1);
5554 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5556 /* can't scroll any further; move cursor down */
5557 if (s->selected < s->ndisplayed - 1)
5558 s->selected = s->ndisplayed - 1;
5561 tree_scroll_down(s, view->nlines - 3);
5566 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5567 struct tog_parent_tree *parent;
5568 /* user selected '..' */
5569 if (s->tree == s->root)
5571 parent = TAILQ_FIRST(&s->parents);
5572 TAILQ_REMOVE(&s->parents, parent,
5574 got_object_tree_close(s->tree);
5575 s->tree = parent->tree;
5576 s->first_displayed_entry =
5577 parent->first_displayed_entry;
5579 parent->selected_entry;
5580 s->selected = parent->selected;
5582 } else if (S_ISDIR(got_tree_entry_get_mode(
5583 s->selected_entry))) {
5584 struct got_tree_object *subtree;
5585 err = got_object_open_as_tree(&subtree, s->repo,
5586 got_tree_entry_get_id(s->selected_entry));
5589 err = tree_view_visit_subtree(s, subtree);
5591 got_object_tree_close(subtree);
5594 } else if (S_ISREG(got_tree_entry_get_mode(
5595 s->selected_entry))) {
5596 struct tog_view *blame_view;
5597 int begin_x = view_is_parent_view(view) ?
5598 view_split_begin_x(view->begin_x) : 0;
5600 err = blame_tree_entry(&blame_view, begin_x,
5601 s->selected_entry, &s->parents,
5602 s->commit_id, s->repo);
5606 blame_view->focussed = 1;
5607 if (view_is_parent_view(view)) {
5608 err = view_close_child(view);
5611 view_set_child(view, blame_view);
5612 view->focus_child = 1;
5614 *new_view = blame_view;
5618 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5619 s->selected = view->nlines - 4;
5632 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5637 static const struct got_error *
5638 cmd_tree(int argc, char *argv[])
5640 const struct got_error *error;
5641 struct got_repository *repo = NULL;
5642 struct got_worktree *worktree = NULL;
5643 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5644 struct got_object_id *commit_id = NULL;
5645 const char *commit_id_arg = NULL;
5647 struct got_reference *ref = NULL;
5648 const char *head_ref_name = NULL;
5650 struct tog_view *view;
5652 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5655 commit_id_arg = optarg;
5658 repo_path = realpath(optarg, NULL);
5659 if (repo_path == NULL)
5660 return got_error_from_errno2("realpath",
5675 if (repo_path == NULL) {
5676 cwd = getcwd(NULL, 0);
5678 return got_error_from_errno("getcwd");
5679 error = got_worktree_open(&worktree, cwd);
5680 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5684 strdup(got_worktree_get_repo_path(worktree));
5686 repo_path = strdup(cwd);
5687 if (repo_path == NULL) {
5688 error = got_error_from_errno("strdup");
5693 error = got_repo_open(&repo, repo_path, NULL);
5697 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5704 error = apply_unveil(got_repo_get_path(repo), NULL);
5708 error = tog_load_refs(repo);
5712 if (commit_id_arg == NULL) {
5713 error = got_repo_match_object_id(&commit_id, &label,
5714 worktree ? got_worktree_get_head_ref_name(worktree) :
5715 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5718 head_ref_name = label;
5720 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5722 head_ref_name = got_ref_get_name(ref);
5723 else if (error->code != GOT_ERR_NOT_REF)
5725 error = got_repo_match_object_id(&commit_id, NULL,
5726 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5731 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5733 error = got_error_from_errno("view_open");
5736 error = open_tree_view(view, commit_id, head_ref_name, repo);
5739 if (!got_path_is_root_dir(in_repo_path)) {
5740 error = tree_view_walk_path(&view->state.tree, commit_id,
5747 /* Release work tree lock. */
5748 got_worktree_close(worktree);
5751 error = view_loop(view);
5760 const struct got_error *close_err = got_repo_close(repo);
5768 static const struct got_error *
5769 ref_view_load_refs(struct tog_ref_view_state *s)
5771 struct got_reflist_entry *sre;
5772 struct tog_reflist_entry *re;
5775 TAILQ_FOREACH(sre, &tog_refs, entry) {
5776 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5779 re = malloc(sizeof(*re));
5781 return got_error_from_errno("malloc");
5783 re->ref = got_ref_dup(sre->ref);
5784 if (re->ref == NULL)
5785 return got_error_from_errno("got_ref_dup");
5786 re->idx = s->nrefs++;
5787 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5790 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5795 ref_view_free_refs(struct tog_ref_view_state *s)
5797 struct tog_reflist_entry *re;
5799 while (!TAILQ_EMPTY(&s->refs)) {
5800 re = TAILQ_FIRST(&s->refs);
5801 TAILQ_REMOVE(&s->refs, re, entry);
5802 got_ref_close(re->ref);
5807 static const struct got_error *
5808 open_ref_view(struct tog_view *view, struct got_repository *repo)
5810 const struct got_error *err = NULL;
5811 struct tog_ref_view_state *s = &view->state.ref;
5813 s->selected_entry = 0;
5816 TAILQ_INIT(&s->refs);
5817 STAILQ_INIT(&s->colors);
5819 err = ref_view_load_refs(s);
5823 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5824 err = add_color(&s->colors, "^refs/heads/",
5825 TOG_COLOR_REFS_HEADS,
5826 get_color_value("TOG_COLOR_REFS_HEADS"));
5830 err = add_color(&s->colors, "^refs/tags/",
5831 TOG_COLOR_REFS_TAGS,
5832 get_color_value("TOG_COLOR_REFS_TAGS"));
5836 err = add_color(&s->colors, "^refs/remotes/",
5837 TOG_COLOR_REFS_REMOTES,
5838 get_color_value("TOG_COLOR_REFS_REMOTES"));
5843 view->show = show_ref_view;
5844 view->input = input_ref_view;
5845 view->close = close_ref_view;
5846 view->search_start = search_start_ref_view;
5847 view->search_next = search_next_ref_view;
5850 free_colors(&s->colors);
5854 static const struct got_error *
5855 close_ref_view(struct tog_view *view)
5857 struct tog_ref_view_state *s = &view->state.ref;
5859 ref_view_free_refs(s);
5860 free_colors(&s->colors);
5865 static const struct got_error *
5866 resolve_reflist_entry(struct got_object_id **commit_id,
5867 struct tog_reflist_entry *re, struct got_repository *repo)
5869 const struct got_error *err = NULL;
5870 struct got_object_id *obj_id;
5871 struct got_tag_object *tag = NULL;
5876 err = got_ref_resolve(&obj_id, repo, re->ref);
5880 err = got_object_get_type(&obj_type, repo, obj_id);
5885 case GOT_OBJ_TYPE_COMMIT:
5886 *commit_id = obj_id;
5888 case GOT_OBJ_TYPE_TAG:
5889 err = got_object_open_as_tag(&tag, repo, obj_id);
5893 err = got_object_get_type(&obj_type, repo,
5894 got_object_tag_get_object_id(tag));
5897 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5898 err = got_error(GOT_ERR_OBJ_TYPE);
5901 *commit_id = got_object_id_dup(
5902 got_object_tag_get_object_id(tag));
5903 if (*commit_id == NULL) {
5904 err = got_error_from_errno("got_object_id_dup");
5909 err = got_error(GOT_ERR_OBJ_TYPE);
5915 got_object_tag_close(tag);
5923 static const struct got_error *
5924 log_ref_entry(struct tog_view **new_view, int begin_x,
5925 struct tog_reflist_entry *re, struct got_repository *repo)
5927 struct tog_view *log_view;
5928 const struct got_error *err = NULL;
5929 struct got_object_id *commit_id = NULL;
5933 err = resolve_reflist_entry(&commit_id, re, repo);
5935 if (err->code != GOT_ERR_OBJ_TYPE)
5941 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5942 if (log_view == NULL) {
5943 err = got_error_from_errno("view_open");
5947 err = open_log_view(log_view, commit_id, repo,
5948 got_ref_get_name(re->ref), "", 0);
5951 view_close(log_view);
5953 *new_view = log_view;
5959 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5961 struct tog_reflist_entry *re;
5964 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5967 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5968 while (i++ < maxscroll) {
5971 s->first_displayed_entry = re;
5972 re = TAILQ_PREV(re, tog_reflist_head, entry);
5977 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5979 struct tog_reflist_entry *next, *last;
5982 if (s->first_displayed_entry)
5983 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5985 next = TAILQ_FIRST(&s->refs);
5987 last = s->last_displayed_entry;
5988 while (next && last && n++ < maxscroll) {
5989 last = TAILQ_NEXT(last, entry);
5991 s->first_displayed_entry = next;
5992 next = TAILQ_NEXT(next, entry);
5997 static const struct got_error *
5998 search_start_ref_view(struct tog_view *view)
6000 struct tog_ref_view_state *s = &view->state.ref;
6002 s->matched_entry = NULL;
6007 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6009 regmatch_t regmatch;
6011 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6015 static const struct got_error *
6016 search_next_ref_view(struct tog_view *view)
6018 struct tog_ref_view_state *s = &view->state.ref;
6019 struct tog_reflist_entry *re = NULL;
6021 if (!view->searching) {
6022 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6026 if (s->matched_entry) {
6027 if (view->searching == TOG_SEARCH_FORWARD) {
6028 if (s->selected_entry)
6029 re = TAILQ_NEXT(s->selected_entry, entry);
6031 re = TAILQ_PREV(s->selected_entry,
6032 tog_reflist_head, entry);
6034 if (s->selected_entry == NULL)
6035 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6037 re = TAILQ_PREV(s->selected_entry,
6038 tog_reflist_head, entry);
6041 if (view->searching == TOG_SEARCH_FORWARD)
6042 re = TAILQ_FIRST(&s->refs);
6044 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6049 if (s->matched_entry == NULL) {
6050 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6053 if (view->searching == TOG_SEARCH_FORWARD)
6054 re = TAILQ_FIRST(&s->refs);
6056 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6059 if (match_reflist_entry(re, &view->regex)) {
6060 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6061 s->matched_entry = re;
6065 if (view->searching == TOG_SEARCH_FORWARD)
6066 re = TAILQ_NEXT(re, entry);
6068 re = TAILQ_PREV(re, tog_reflist_head, entry);
6071 if (s->matched_entry) {
6072 s->first_displayed_entry = s->matched_entry;
6079 static const struct got_error *
6080 show_ref_view(struct tog_view *view)
6082 const struct got_error *err = NULL;
6083 struct tog_ref_view_state *s = &view->state.ref;
6084 struct tog_reflist_entry *re;
6087 struct tog_color *tc;
6089 int limit = view->nlines;
6091 werase(view->window);
6098 re = s->first_displayed_entry;
6100 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6102 return got_error_from_errno("asprintf");
6104 err = format_line(&wline, &width, line, view->ncols, 0);
6109 if (view_needs_focus_indication(view))
6110 wstandout(view->window);
6111 waddwstr(view->window, wline);
6112 if (view_needs_focus_indication(view))
6113 wstandend(view->window);
6118 if (width < view->ncols - 1)
6119 waddch(view->window, '\n');
6124 while (re && limit > 0) {
6127 if (got_ref_is_symbolic(re->ref)) {
6128 if (asprintf(&line, "%s -> %s",
6129 got_ref_get_name(re->ref),
6130 got_ref_get_symref_target(re->ref)) == -1)
6131 return got_error_from_errno("asprintf");
6132 } else if (s->show_ids) {
6133 struct got_object_id *id;
6135 err = got_ref_resolve(&id, s->repo, re->ref);
6138 err = got_object_id_str(&id_str, id);
6143 if (asprintf(&line, "%s: %s",
6144 got_ref_get_name(re->ref), id_str) == -1) {
6145 err = got_error_from_errno("asprintf");
6153 line = strdup(got_ref_get_name(re->ref));
6155 return got_error_from_errno("strdup");
6158 err = format_line(&wline, &width, line, view->ncols, 0);
6163 if (n == s->selected) {
6165 wstandout(view->window);
6166 s->selected_entry = re;
6168 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6170 wattr_on(view->window,
6171 COLOR_PAIR(tc->colorpair), NULL);
6172 waddwstr(view->window, wline);
6174 wattr_off(view->window,
6175 COLOR_PAIR(tc->colorpair), NULL);
6176 if (width < view->ncols - 1)
6177 waddch(view->window, '\n');
6178 if (n == s->selected && view->focussed)
6179 wstandend(view->window);
6185 s->last_displayed_entry = re;
6188 re = TAILQ_NEXT(re, entry);
6195 static const struct got_error *
6196 browse_ref_tree(struct tog_view **new_view, int begin_x,
6197 struct tog_reflist_entry *re, struct got_repository *repo)
6199 const struct got_error *err = NULL;
6200 struct got_object_id *commit_id = NULL;
6201 struct tog_view *tree_view;
6205 err = resolve_reflist_entry(&commit_id, re, repo);
6207 if (err->code != GOT_ERR_OBJ_TYPE)
6214 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6215 if (tree_view == NULL) {
6216 err = got_error_from_errno("view_open");
6220 err = open_tree_view(tree_view, commit_id,
6221 got_ref_get_name(re->ref), repo);
6225 *new_view = tree_view;
6230 static const struct got_error *
6231 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6233 const struct got_error *err = NULL;
6234 struct tog_ref_view_state *s = &view->state.ref;
6235 struct tog_view *log_view, *tree_view;
6236 struct tog_reflist_entry *re;
6241 s->show_ids = !s->show_ids;
6245 if (!s->selected_entry)
6247 if (view_is_parent_view(view))
6248 begin_x = view_split_begin_x(view->begin_x);
6249 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6252 log_view->focussed = 1;
6253 if (view_is_parent_view(view)) {
6254 err = view_close_child(view);
6257 view_set_child(view, log_view);
6258 view->focus_child = 1;
6260 *new_view = log_view;
6263 if (!s->selected_entry)
6265 if (view_is_parent_view(view))
6266 begin_x = view_split_begin_x(view->begin_x);
6267 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6269 if (err || tree_view == NULL)
6272 tree_view->focussed = 1;
6273 if (view_is_parent_view(view)) {
6274 err = view_close_child(view);
6277 view_set_child(view, tree_view);
6278 view->focus_child = 1;
6280 *new_view = tree_view;
6285 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6290 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6291 for (n = 0; n < view->nlines - 1; n++) {
6294 s->first_displayed_entry = re;
6295 re = TAILQ_PREV(re, tog_reflist_head, entry);
6298 s->selected = n - 1;
6303 if (s->selected > 0) {
6307 ref_scroll_up(s, 1);
6311 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6313 ref_scroll_up(s, MAX(0, view->nlines - 1));
6318 if (s->selected < s->ndisplayed - 1) {
6322 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6323 /* can't scroll any further */
6325 ref_scroll_down(s, 1);
6329 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6330 /* can't scroll any further; move cursor down */
6331 if (s->selected < s->ndisplayed - 1)
6332 s->selected = s->ndisplayed - 1;
6335 ref_scroll_down(s, view->nlines - 1);
6339 err = tog_load_refs(s->repo);
6342 ref_view_free_refs(s);
6343 err = ref_view_load_refs(s);
6346 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6347 s->selected = view->nlines - 2;
6360 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6365 static const struct got_error *
6366 cmd_ref(int argc, char *argv[])
6368 const struct got_error *error;
6369 struct got_repository *repo = NULL;
6370 struct got_worktree *worktree = NULL;
6371 char *cwd = NULL, *repo_path = NULL;
6373 struct tog_view *view;
6375 while ((ch = getopt(argc, argv, "r:")) != -1) {
6378 repo_path = realpath(optarg, NULL);
6379 if (repo_path == NULL)
6380 return got_error_from_errno2("realpath",
6395 if (repo_path == NULL) {
6396 cwd = getcwd(NULL, 0);
6398 return got_error_from_errno("getcwd");
6399 error = got_worktree_open(&worktree, cwd);
6400 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6404 strdup(got_worktree_get_repo_path(worktree));
6406 repo_path = strdup(cwd);
6407 if (repo_path == NULL) {
6408 error = got_error_from_errno("strdup");
6413 error = got_repo_open(&repo, repo_path, NULL);
6419 error = apply_unveil(got_repo_get_path(repo), NULL);
6423 error = tog_load_refs(repo);
6427 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6429 error = got_error_from_errno("view_open");
6433 error = open_ref_view(view, repo);
6438 /* Release work tree lock. */
6439 got_worktree_close(worktree);
6442 error = view_loop(view);
6447 const struct got_error *close_err = got_repo_close(repo);
6456 list_commands(FILE *fp)
6460 fprintf(fp, "commands:");
6461 for (i = 0; i < nitems(tog_commands); i++) {
6462 struct tog_cmd *cmd = &tog_commands[i];
6463 fprintf(fp, " %s", cmd->name);
6469 usage(int hflag, int status)
6471 FILE *fp = (status == 0) ? stdout : stderr;
6473 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6476 fprintf(fp, "lazy usage: %s path\n", getprogname());
6483 make_argv(int argc, ...)
6491 argv = calloc(argc, sizeof(char *));
6494 for (i = 0; i < argc; i++) {
6495 argv[i] = strdup(va_arg(ap, char *));
6496 if (argv[i] == NULL)
6505 * Try to convert 'tog path' into a 'tog log path' command.
6506 * The user could simply have mistyped the command rather than knowingly
6507 * provided a path. So check whether argv[0] can in fact be resolved
6508 * to a path in the HEAD commit and print a special error if not.
6509 * This hack is for mpi@ <3
6511 static const struct got_error *
6512 tog_log_with_path(int argc, char *argv[])
6514 const struct got_error *error = NULL, *close_err;
6515 struct tog_cmd *cmd = NULL;
6516 struct got_repository *repo = NULL;
6517 struct got_worktree *worktree = NULL;
6518 struct got_object_id *commit_id = NULL, *id = NULL;
6519 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6520 char *commit_id_str = NULL, **cmd_argv = NULL;
6522 cwd = getcwd(NULL, 0);
6524 return got_error_from_errno("getcwd");
6526 error = got_worktree_open(&worktree, cwd);
6527 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6531 repo_path = strdup(got_worktree_get_repo_path(worktree));
6533 repo_path = strdup(cwd);
6534 if (repo_path == NULL) {
6535 error = got_error_from_errno("strdup");
6539 error = got_repo_open(&repo, repo_path, NULL);
6543 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6548 error = tog_load_refs(repo);
6551 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6552 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6553 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6558 got_worktree_close(worktree);
6562 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6564 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6566 fprintf(stderr, "%s: '%s' is no known command or path\n",
6567 getprogname(), argv[0]);
6572 close_err = got_repo_close(repo);
6577 error = got_object_id_str(&commit_id_str, commit_id);
6581 cmd = &tog_commands[0]; /* log */
6583 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6584 error = cmd->cmd_main(argc, cmd_argv);
6587 close_err = got_repo_close(repo);
6592 got_worktree_close(worktree);
6594 free(commit_id_str);
6601 for (i = 0; i < argc; i++)
6610 main(int argc, char *argv[])
6612 const struct got_error *error = NULL;
6613 struct tog_cmd *cmd = NULL;
6614 int ch, hflag = 0, Vflag = 0;
6615 char **cmd_argv = NULL;
6616 static struct option longopts[] = {
6617 { "version", no_argument, NULL, 'V' },
6621 setlocale(LC_CTYPE, "");
6623 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6643 got_version_print_str();
6648 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6656 /* Build an argument vector which runs a default command. */
6657 cmd = &tog_commands[0];
6659 cmd_argv = make_argv(argc, cmd->name);
6663 /* Did the user specify a command? */
6664 for (i = 0; i < nitems(tog_commands); i++) {
6665 if (strncmp(tog_commands[i].name, argv[0],
6666 strlen(argv[0])) == 0) {
6667 cmd = &tog_commands[i];
6676 /* No command specified; try log with a path */
6677 error = tog_log_with_path(argc, argv);
6682 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6689 for (i = 0; i < argc; i++)
6694 if (error && error->code != GOT_ERR_CANCELLED)
6695 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);