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>
41 #include "got_compat.h"
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
49 #include "got_opentemp.h"
51 #include "got_cancel.h"
52 #include "got_commit_graph.h"
53 #include "got_blame.h"
54 #include "got_privsep.h"
56 #include "got_worktree.h"
58 //#define update_panels() (0)
59 //#define doupdate() (0)
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
66 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
70 #define CTRL(x) ((x) & 0x1f)
74 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
79 const struct got_error *(*cmd_main)(int, char *[]);
80 void (*cmd_usage)(void);
83 __dead static void usage(int, int);
84 __dead static void usage_log(void);
85 __dead static void usage_diff(void);
86 __dead static void usage_blame(void);
87 __dead static void usage_tree(void);
88 __dead static void usage_ref(void);
90 static const struct got_error* cmd_log(int, char *[]);
91 static const struct got_error* cmd_diff(int, char *[]);
92 static const struct got_error* cmd_blame(int, char *[]);
93 static const struct got_error* cmd_tree(int, char *[]);
94 static const struct got_error* cmd_ref(int, char *[]);
96 static struct tog_cmd tog_commands[] = {
97 { "log", cmd_log, usage_log },
98 { "diff", cmd_diff, usage_diff },
99 { "blame", cmd_blame, usage_blame },
100 { "tree", cmd_tree, usage_tree },
101 { "ref", cmd_ref, usage_ref },
112 #define TOG_EOF_STRING "(END)"
114 struct commit_queue_entry {
115 TAILQ_ENTRY(commit_queue_entry) entry;
116 struct got_object_id *id;
117 struct got_commit_object *commit;
120 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
121 struct commit_queue {
123 struct commit_queue_head head;
127 STAILQ_ENTRY(tog_color) entry;
131 STAILQ_HEAD(tog_colors, tog_color);
133 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
134 static struct got_reflist_object_id_map *tog_refs_idmap;
136 static const struct got_error *
137 tog_load_refs(struct got_repository *repo)
139 const struct got_error *err;
141 err = got_ref_list(&tog_refs, repo, NULL, got_ref_cmp_by_name, NULL);
145 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
152 if (tog_refs_idmap) {
153 got_reflist_object_id_map_free(tog_refs_idmap);
154 tog_refs_idmap = NULL;
156 got_ref_list_free(&tog_refs);
159 static const struct got_error *
160 add_color(struct tog_colors *colors, const char *pattern,
161 int idx, short color)
163 const struct got_error *err = NULL;
164 struct tog_color *tc;
167 if (idx < 1 || idx > COLOR_PAIRS - 1)
170 init_pair(idx, color, -1);
172 tc = calloc(1, sizeof(*tc));
174 return got_error_from_errno("calloc");
175 regerr = regcomp(&tc->regex, pattern,
176 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
178 static char regerr_msg[512];
179 static char err_msg[512];
180 regerror(regerr, &tc->regex, regerr_msg,
182 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
184 err = got_error_msg(GOT_ERR_REGEX, err_msg);
189 STAILQ_INSERT_HEAD(colors, tc, entry);
194 free_colors(struct tog_colors *colors)
196 struct tog_color *tc;
198 while (!STAILQ_EMPTY(colors)) {
199 tc = STAILQ_FIRST(colors);
200 STAILQ_REMOVE_HEAD(colors, entry);
207 get_color(struct tog_colors *colors, int colorpair)
209 struct tog_color *tc = NULL;
211 STAILQ_FOREACH(tc, colors, entry) {
212 if (tc->colorpair == colorpair)
220 default_color_value(const char *envvar)
222 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
223 return COLOR_MAGENTA;
224 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
226 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
228 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
230 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
231 return COLOR_MAGENTA;
232 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
233 return COLOR_MAGENTA;
234 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
236 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
238 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
240 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
242 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
244 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
246 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
247 return COLOR_MAGENTA;
248 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
255 get_color_value(const char *envvar)
257 const char *val = getenv(envvar);
260 return default_color_value(envvar);
262 if (strcasecmp(val, "black") == 0)
264 if (strcasecmp(val, "red") == 0)
266 if (strcasecmp(val, "green") == 0)
268 if (strcasecmp(val, "yellow") == 0)
270 if (strcasecmp(val, "blue") == 0)
272 if (strcasecmp(val, "magenta") == 0)
273 return COLOR_MAGENTA;
274 if (strcasecmp(val, "cyan") == 0)
276 if (strcasecmp(val, "white") == 0)
278 if (strcasecmp(val, "default") == 0)
281 return default_color_value(envvar);
285 struct tog_diff_view_state {
286 struct got_object_id *id1, *id2;
287 const char *label1, *label2;
289 int first_displayed_line;
290 int last_displayed_line;
293 int ignore_whitespace;
295 struct got_repository *repo;
296 struct tog_colors colors;
302 /* passed from log view; may be NULL */
303 struct tog_view *log_view;
306 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
308 struct tog_log_thread_args {
309 pthread_cond_t need_commits;
310 pthread_cond_t commit_loaded;
313 struct got_commit_graph *graph;
314 struct commit_queue *commits;
315 const char *in_repo_path;
316 struct got_object_id *start_id;
317 struct got_repository *repo;
320 struct commit_queue_entry **first_displayed_entry;
321 struct commit_queue_entry **selected_entry;
323 int *search_next_done;
327 struct tog_log_view_state {
328 struct commit_queue commits;
329 struct commit_queue_entry *first_displayed_entry;
330 struct commit_queue_entry *last_displayed_entry;
331 struct commit_queue_entry *selected_entry;
336 struct got_repository *repo;
337 struct got_object_id *start_id;
340 struct tog_log_thread_args thread_args;
341 struct commit_queue_entry *matched_entry;
342 struct commit_queue_entry *search_entry;
343 struct tog_colors colors;
346 #define TOG_COLOR_DIFF_MINUS 1
347 #define TOG_COLOR_DIFF_PLUS 2
348 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
349 #define TOG_COLOR_DIFF_META 4
350 #define TOG_COLOR_TREE_SUBMODULE 5
351 #define TOG_COLOR_TREE_SYMLINK 6
352 #define TOG_COLOR_TREE_DIRECTORY 7
353 #define TOG_COLOR_TREE_EXECUTABLE 8
354 #define TOG_COLOR_COMMIT 9
355 #define TOG_COLOR_AUTHOR 10
356 #define TOG_COLOR_DATE 11
357 #define TOG_COLOR_REFS_HEADS 12
358 #define TOG_COLOR_REFS_TAGS 13
359 #define TOG_COLOR_REFS_REMOTES 14
361 struct tog_blame_cb_args {
362 struct tog_blame_line *lines; /* one per line */
365 struct tog_view *view;
366 struct got_object_id *commit_id;
370 struct tog_blame_thread_args {
372 struct got_repository *repo;
373 struct tog_blame_cb_args *cb_args;
375 got_cancel_cb cancel_cb;
382 struct tog_blame_line *lines;
386 struct tog_blame_thread_args thread_args;
387 struct tog_blame_cb_args cb_args;
391 struct tog_blame_view_state {
392 int first_displayed_line;
393 int last_displayed_line;
398 struct got_object_id_queue blamed_commits;
399 struct got_object_qid *blamed_commit;
401 struct got_repository *repo;
402 struct got_object_id *commit_id;
403 struct tog_blame blame;
405 struct tog_colors colors;
408 struct tog_parent_tree {
409 TAILQ_ENTRY(tog_parent_tree) entry;
410 struct got_tree_object *tree;
411 struct got_tree_entry *first_displayed_entry;
412 struct got_tree_entry *selected_entry;
416 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
418 struct tog_tree_view_state {
420 struct got_object_id *commit_id;/* commit which this tree belongs to */
421 struct got_tree_object *root; /* the commit's root tree entry */
422 struct got_tree_object *tree; /* currently displayed (sub-)tree */
423 struct got_tree_entry *first_displayed_entry;
424 struct got_tree_entry *last_displayed_entry;
425 struct got_tree_entry *selected_entry;
426 int ndisplayed, selected, show_ids;
427 struct tog_parent_trees parents; /* parent trees of current sub-tree */
429 struct got_repository *repo;
430 struct got_tree_entry *matched_entry;
431 struct tog_colors colors;
434 struct tog_reflist_entry {
435 TAILQ_ENTRY(tog_reflist_entry) entry;
436 struct got_reference *ref;
440 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
442 struct tog_ref_view_state {
443 struct tog_reflist_head refs;
444 struct tog_reflist_entry *first_displayed_entry;
445 struct tog_reflist_entry *last_displayed_entry;
446 struct tog_reflist_entry *selected_entry;
447 int nrefs, ndisplayed, selected, show_ids;
448 struct got_repository *repo;
449 struct tog_reflist_entry *matched_entry;
450 struct tog_colors colors;
454 * We implement two types of views: parent views and child views.
456 * The 'Tab' key switches focus between a parent view and its child view.
457 * Child views are shown side-by-side to their parent view, provided
458 * there is enough screen estate.
460 * When a new view is opened from within a parent view, this new view
461 * becomes a child view of the parent view, replacing any existing child.
463 * When a new view is opened from within a child view, this new view
464 * becomes a parent view which will obscure the views below until the
465 * user quits the new parent view by typing 'q'.
467 * This list of views contains parent views only.
468 * Child views are only pointed to by their parent view.
470 TAILQ_HEAD(tog_view_list_head, tog_view);
473 TAILQ_ENTRY(tog_view) entry;
476 int nlines, ncols, begin_y, begin_x;
477 int lines, cols; /* copies of LINES and COLS */
478 int focussed; /* Only set on one parent or child view at a time. */
480 struct tog_view *parent;
481 struct tog_view *child;
484 * This flag is initially set on parent views when a new child view
485 * is created. It gets toggled when the 'Tab' key switches focus
486 * between parent and child.
487 * The flag indicates whether focus should be passed on to our child
488 * view if this parent view gets picked for focus after another parent
489 * view was closed. This prevents child views from losing focus in such
494 /* type-specific state */
495 enum tog_view_type type;
497 struct tog_diff_view_state diff;
498 struct tog_log_view_state log;
499 struct tog_blame_view_state blame;
500 struct tog_tree_view_state tree;
501 struct tog_ref_view_state ref;
504 const struct got_error *(*show)(struct tog_view *);
505 const struct got_error *(*input)(struct tog_view **,
506 struct tog_view *, int);
507 const struct got_error *(*close)(struct tog_view *);
509 const struct got_error *(*search_start)(struct tog_view *);
510 const struct got_error *(*search_next)(struct tog_view *);
513 #define TOG_SEARCH_FORWARD 1
514 #define TOG_SEARCH_BACKWARD 2
515 int search_next_done;
516 #define TOG_SEARCH_HAVE_MORE 1
517 #define TOG_SEARCH_NO_MORE 2
518 #define TOG_SEARCH_HAVE_NONE 3
523 static const struct got_error *open_diff_view(struct tog_view *,
524 struct got_object_id *, struct got_object_id *,
525 const char *, const char *, int, int, int, struct tog_view *,
526 struct got_repository *);
527 static const struct got_error *show_diff_view(struct tog_view *);
528 static const struct got_error *input_diff_view(struct tog_view **,
529 struct tog_view *, int);
530 static const struct got_error* close_diff_view(struct tog_view *);
531 static const struct got_error *search_start_diff_view(struct tog_view *);
532 static const struct got_error *search_next_diff_view(struct tog_view *);
534 static const struct got_error *open_log_view(struct tog_view *,
535 struct got_object_id *, struct got_repository *,
536 const char *, const char *, int);
537 static const struct got_error * show_log_view(struct tog_view *);
538 static const struct got_error *input_log_view(struct tog_view **,
539 struct tog_view *, int);
540 static const struct got_error *close_log_view(struct tog_view *);
541 static const struct got_error *search_start_log_view(struct tog_view *);
542 static const struct got_error *search_next_log_view(struct tog_view *);
544 static const struct got_error *open_blame_view(struct tog_view *, char *,
545 struct got_object_id *, struct got_repository *);
546 static const struct got_error *show_blame_view(struct tog_view *);
547 static const struct got_error *input_blame_view(struct tog_view **,
548 struct tog_view *, int);
549 static const struct got_error *close_blame_view(struct tog_view *);
550 static const struct got_error *search_start_blame_view(struct tog_view *);
551 static const struct got_error *search_next_blame_view(struct tog_view *);
553 static const struct got_error *open_tree_view(struct tog_view *,
554 struct got_object_id *, const char *, struct got_repository *);
555 static const struct got_error *show_tree_view(struct tog_view *);
556 static const struct got_error *input_tree_view(struct tog_view **,
557 struct tog_view *, int);
558 static const struct got_error *close_tree_view(struct tog_view *);
559 static const struct got_error *search_start_tree_view(struct tog_view *);
560 static const struct got_error *search_next_tree_view(struct tog_view *);
562 static const struct got_error *open_ref_view(struct tog_view *,
563 struct got_repository *);
564 static const struct got_error *show_ref_view(struct tog_view *);
565 static const struct got_error *input_ref_view(struct tog_view **,
566 struct tog_view *, int);
567 static const struct got_error *close_ref_view(struct tog_view *);
568 static const struct got_error *search_start_ref_view(struct tog_view *);
569 static const struct got_error *search_next_ref_view(struct tog_view *);
571 static volatile sig_atomic_t tog_sigwinch_received;
572 static volatile sig_atomic_t tog_sigpipe_received;
573 static volatile sig_atomic_t tog_sigcont_received;
576 tog_sigwinch(int signo)
578 tog_sigwinch_received = 1;
582 tog_sigpipe(int signo)
584 tog_sigpipe_received = 1;
588 tog_sigcont(int signo)
590 tog_sigcont_received = 1;
593 static const struct got_error *
594 view_close(struct tog_view *view)
596 const struct got_error *err = NULL;
599 view_close(view->child);
603 err = view->close(view);
605 del_panel(view->panel);
607 delwin(view->window);
612 static struct tog_view *
613 view_open(int nlines, int ncols, int begin_y, int begin_x,
614 enum tog_view_type type)
616 struct tog_view *view = calloc(1, sizeof(*view));
624 view->nlines = nlines ? nlines : LINES - begin_y;
625 view->ncols = ncols ? ncols : COLS - begin_x;
626 view->begin_y = begin_y;
627 view->begin_x = begin_x;
628 view->window = newwin(nlines, ncols, begin_y, begin_x);
629 if (view->window == NULL) {
633 view->panel = new_panel(view->window);
634 if (view->panel == NULL ||
635 set_panel_userptr(view->panel, view) != OK) {
640 keypad(view->window, TRUE);
645 view_split_begin_x(int begin_x)
647 if (begin_x > 0 || COLS < 120)
649 return (COLS - MAX(COLS / 2, 80));
652 static const struct got_error *view_resize(struct tog_view *);
654 static const struct got_error *
655 view_splitscreen(struct tog_view *view)
657 const struct got_error *err = NULL;
660 view->begin_x = view_split_begin_x(0);
661 view->nlines = LINES;
662 view->ncols = COLS - view->begin_x;
665 err = view_resize(view);
669 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
670 return got_error_from_errno("mvwin");
675 static const struct got_error *
676 view_fullscreen(struct tog_view *view)
678 const struct got_error *err = NULL;
682 view->nlines = LINES;
686 err = view_resize(view);
690 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
691 return got_error_from_errno("mvwin");
697 view_is_parent_view(struct tog_view *view)
699 return view->parent == NULL;
702 static const struct got_error *
703 view_resize(struct tog_view *view)
707 if (view->lines > LINES)
708 nlines = view->nlines - (view->lines - LINES);
710 nlines = view->nlines + (LINES - view->lines);
712 if (view->cols > COLS)
713 ncols = view->ncols - (view->cols - COLS);
715 ncols = view->ncols + (COLS - view->cols);
717 if (wresize(view->window, nlines, ncols) == ERR)
718 return got_error_from_errno("wresize");
719 if (replace_panel(view->panel, view->window) == ERR)
720 return got_error_from_errno("replace_panel");
721 wclear(view->window);
723 view->nlines = nlines;
729 view->child->begin_x = view_split_begin_x(view->begin_x);
730 if (view->child->begin_x == 0) {
731 view_fullscreen(view->child);
732 if (view->child->focussed)
733 show_panel(view->child->panel);
735 show_panel(view->panel);
737 view_splitscreen(view->child);
738 show_panel(view->child->panel);
745 static const struct got_error *
746 view_close_child(struct tog_view *view)
748 const struct got_error *err = NULL;
750 if (view->child == NULL)
753 err = view_close(view->child);
759 view_set_child(struct tog_view *view, struct tog_view *child)
762 child->parent = view;
766 view_is_splitscreen(struct tog_view *view)
768 return view->begin_x > 0;
777 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
778 cols = 80; /* Default */
784 resize_term(lines, cols);
787 static const struct got_error *
788 view_search_start(struct tog_view *view)
790 const struct got_error *err = NULL;
794 if (view->search_started) {
795 regfree(&view->regex);
797 memset(&view->regmatch, 0, sizeof(view->regmatch));
799 view->search_started = 0;
801 if (view->nlines < 1)
804 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
805 wclrtoeol(view->window);
809 ret = wgetnstr(view->window, pattern, sizeof(pattern));
815 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
816 err = view->search_start(view);
818 regfree(&view->regex);
821 view->search_started = 1;
822 view->searching = TOG_SEARCH_FORWARD;
823 view->search_next_done = 0;
824 view->search_next(view);
830 static const struct got_error *
831 view_input(struct tog_view **new, int *done, struct tog_view *view,
832 struct tog_view_list_head *views)
834 const struct got_error *err = NULL;
840 /* Clear "no matches" indicator. */
841 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
842 view->search_next_done == TOG_SEARCH_HAVE_NONE)
843 view->search_next_done = TOG_SEARCH_HAVE_MORE;
845 if (view->searching && !view->search_next_done) {
846 errcode = pthread_mutex_unlock(&tog_mutex);
848 return got_error_set_errno(errcode,
849 "pthread_mutex_unlock");
851 errcode = pthread_mutex_lock(&tog_mutex);
853 return got_error_set_errno(errcode,
854 "pthread_mutex_lock");
855 view->search_next(view);
859 nodelay(stdscr, FALSE);
860 /* Allow threads to make progress while we are waiting for input. */
861 errcode = pthread_mutex_unlock(&tog_mutex);
863 return got_error_set_errno(errcode, "pthread_mutex_unlock");
864 ch = wgetch(view->window);
865 errcode = pthread_mutex_lock(&tog_mutex);
867 return got_error_set_errno(errcode, "pthread_mutex_lock");
868 nodelay(stdscr, TRUE);
870 if (tog_sigwinch_received || tog_sigcont_received) {
872 tog_sigwinch_received = 0;
873 tog_sigcont_received = 0;
874 TAILQ_FOREACH(v, views, entry) {
875 err = view_resize(v);
878 err = v->input(new, v, KEY_RESIZE);
882 err = view_resize(v->child);
885 err = v->child->input(new, v->child,
897 view->child->focussed = 1;
898 view->focus_child = 1;
899 } else if (view->parent) {
901 view->parent->focussed = 1;
902 view->parent->focus_child = 0;
906 err = view->input(new, view, ch);
913 if (view_is_parent_view(view)) {
914 if (view->child == NULL)
916 if (view_is_splitscreen(view->child)) {
918 view->child->focussed = 1;
919 err = view_fullscreen(view->child);
921 err = view_splitscreen(view->child);
924 err = view->child->input(new, view->child,
927 if (view_is_splitscreen(view)) {
928 view->parent->focussed = 0;
930 err = view_fullscreen(view);
932 err = view_splitscreen(view);
936 err = view->input(new, view, KEY_RESIZE);
942 if (view->search_start)
943 view_search_start(view);
945 err = view->input(new, view, ch);
949 if (view->search_started && view->search_next) {
950 view->searching = (ch == 'n' ?
951 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
952 view->search_next_done = 0;
953 view->search_next(view);
955 err = view->input(new, view, ch);
958 err = view->input(new, view, ch);
966 view_vborder(struct tog_view *view)
969 const struct tog_view *view_above;
972 return view_vborder(view->parent);
974 panel = panel_above(view->panel);
978 view_above = panel_userptr(panel);
979 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
980 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
984 view_needs_focus_indication(struct tog_view *view)
986 if (view_is_parent_view(view)) {
987 if (view->child == NULL || view->child->focussed)
989 if (!view_is_splitscreen(view->child))
991 } else if (!view_is_splitscreen(view))
994 return view->focussed;
997 static const struct got_error *
998 view_loop(struct tog_view *view)
1000 const struct got_error *err = NULL;
1001 struct tog_view_list_head views;
1002 struct tog_view *new_view;
1003 int fast_refresh = 10;
1004 int done = 0, errcode;
1006 errcode = pthread_mutex_lock(&tog_mutex);
1008 return got_error_set_errno(errcode, "pthread_mutex_lock");
1011 TAILQ_INSERT_HEAD(&views, view, entry);
1014 err = view->show(view);
1019 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1020 /* Refresh fast during initialization, then become slower. */
1021 if (fast_refresh && fast_refresh-- == 0)
1022 halfdelay(10); /* switch to once per second */
1024 err = view_input(&new_view, &done, view, &views);
1028 struct tog_view *v, *prev = NULL;
1030 if (view_is_parent_view(view))
1031 prev = TAILQ_PREV(view, tog_view_list_head,
1033 else if (view->parent)
1034 prev = view->parent;
1037 view->parent->child = NULL;
1038 view->parent->focus_child = 0;
1040 TAILQ_REMOVE(&views, view, entry);
1042 err = view_close(view);
1047 TAILQ_FOREACH(v, &views, entry) {
1051 if (view == NULL && new_view == NULL) {
1052 /* No view has focus. Try to pick one. */
1055 else if (!TAILQ_EMPTY(&views)) {
1056 view = TAILQ_LAST(&views,
1057 tog_view_list_head);
1060 if (view->focus_child) {
1061 view->child->focussed = 1;
1069 struct tog_view *v, *t;
1070 /* Only allow one parent view per type. */
1071 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1072 if (v->type != new_view->type)
1074 TAILQ_REMOVE(&views, v, entry);
1075 err = view_close(v);
1080 TAILQ_INSERT_TAIL(&views, new_view, entry);
1084 if (view_is_parent_view(view)) {
1085 if (view->child && view->child->focussed)
1088 if (view->parent && view->parent->focussed)
1089 view = view->parent;
1091 show_panel(view->panel);
1092 if (view->child && view_is_splitscreen(view->child))
1093 show_panel(view->child->panel);
1094 if (view->parent && view_is_splitscreen(view)) {
1095 err = view->parent->show(view->parent);
1099 err = view->show(view);
1103 err = view->child->show(view->child);
1112 while (!TAILQ_EMPTY(&views)) {
1113 view = TAILQ_FIRST(&views);
1114 TAILQ_REMOVE(&views, view, entry);
1118 errcode = pthread_mutex_unlock(&tog_mutex);
1119 if (errcode && err == NULL)
1120 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1130 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1135 /* Create newly allocated wide-character string equivalent to a byte string. */
1136 static const struct got_error *
1137 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1140 const struct got_error *err = NULL;
1143 *wlen = mbstowcs(NULL, s, 0);
1144 if (*wlen == (size_t)-1) {
1146 if (errno != EILSEQ)
1147 return got_error_from_errno("mbstowcs");
1149 /* byte string invalid in current encoding; try to "fix" it */
1150 err = got_mbsavis(&vis, &vislen, s);
1153 *wlen = mbstowcs(NULL, vis, 0);
1154 if (*wlen == (size_t)-1) {
1155 err = got_error_from_errno("mbstowcs"); /* give up */
1160 *ws = calloc(*wlen + 1, sizeof(**ws));
1162 err = got_error_from_errno("calloc");
1166 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1167 err = got_error_from_errno("mbstowcs");
1178 /* Format a line for display, ensuring that it won't overflow a width limit. */
1179 static const struct got_error *
1180 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1183 const struct got_error *err = NULL;
1185 wchar_t *wline = NULL;
1192 err = mbs2ws(&wline, &wlen, line);
1196 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1197 wline[wlen - 1] = L'\0';
1200 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1201 wline[wlen - 1] = L'\0';
1207 int width = wcwidth(wline[i]);
1214 if (width == 1 || width == 2) {
1215 if (cols + width > wlimit)
1219 } else if (width == -1) {
1220 if (wline[i] == L'\t') {
1222 ((cols + col_tab_align) % TABSIZE);
1227 if (cols + width > wlimit)
1232 err = got_error_from_errno("wcwidth");
1247 static const struct got_error*
1248 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1249 struct got_object_id *id, struct got_repository *repo)
1251 static const struct got_error *err = NULL;
1252 struct got_reflist_entry *re;
1258 TAILQ_FOREACH(re, refs, entry) {
1259 struct got_tag_object *tag = NULL;
1260 struct got_object_id *ref_id;
1263 name = got_ref_get_name(re->ref);
1264 if (strcmp(name, GOT_REF_HEAD) == 0)
1266 if (strncmp(name, "refs/", 5) == 0)
1268 if (strncmp(name, "got/", 4) == 0)
1270 if (strncmp(name, "heads/", 6) == 0)
1272 if (strncmp(name, "remotes/", 8) == 0) {
1274 s = strstr(name, "/" GOT_REF_HEAD);
1275 if (s != NULL && s[strlen(s)] == '\0')
1278 err = got_ref_resolve(&ref_id, repo, re->ref);
1281 if (strncmp(name, "tags/", 5) == 0) {
1282 err = got_object_open_as_tag(&tag, repo, ref_id);
1284 if (err->code != GOT_ERR_OBJ_TYPE) {
1288 /* Ref points at something other than a tag. */
1293 cmp = got_object_id_cmp(tag ?
1294 got_object_tag_get_object_id(tag) : ref_id, id);
1297 got_object_tag_close(tag);
1301 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1302 s ? ", " : "", name) == -1) {
1303 err = got_error_from_errno("asprintf");
1314 static const struct got_error *
1315 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1320 smallerthan = strchr(author, '<');
1321 if (smallerthan && smallerthan[1] != '\0')
1322 author = smallerthan + 1;
1323 author[strcspn(author, "@>")] = '\0';
1324 return format_line(wauthor, author_width, author, limit, col_tab_align);
1327 static const struct got_error *
1328 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1329 struct got_object_id *id, const size_t date_display_cols,
1330 int author_display_cols)
1332 struct tog_log_view_state *s = &view->state.log;
1333 const struct got_error *err = NULL;
1334 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1335 char *logmsg0 = NULL, *logmsg = NULL;
1336 char *author = NULL;
1337 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1338 int author_width, logmsg_width;
1339 char *newline, *line = NULL;
1341 const int avail = view->ncols;
1343 time_t committer_time;
1344 struct tog_color *tc;
1346 committer_time = got_object_commit_get_committer_time(commit);
1347 if (gmtime_r(&committer_time, &tm) == NULL)
1348 return got_error_from_errno("gmtime_r");
1349 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1350 return got_error(GOT_ERR_NO_SPACE);
1352 if (avail <= date_display_cols)
1353 limit = MIN(sizeof(datebuf) - 1, avail);
1355 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1356 tc = get_color(&s->colors, TOG_COLOR_DATE);
1358 wattr_on(view->window,
1359 COLOR_PAIR(tc->colorpair), NULL);
1360 waddnstr(view->window, datebuf, limit);
1362 wattr_off(view->window,
1363 COLOR_PAIR(tc->colorpair), NULL);
1370 err = got_object_id_str(&id_str, id);
1373 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1375 wattr_on(view->window,
1376 COLOR_PAIR(tc->colorpair), NULL);
1377 wprintw(view->window, "%.8s ", id_str);
1379 wattr_off(view->window,
1380 COLOR_PAIR(tc->colorpair), NULL);
1387 author = strdup(got_object_commit_get_author(commit));
1388 if (author == NULL) {
1389 err = got_error_from_errno("strdup");
1392 err = format_author(&wauthor, &author_width, author, avail - col, col);
1395 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1397 wattr_on(view->window,
1398 COLOR_PAIR(tc->colorpair), NULL);
1399 waddwstr(view->window, wauthor);
1401 wattr_off(view->window,
1402 COLOR_PAIR(tc->colorpair), NULL);
1403 col += author_width;
1404 while (col < avail && author_width < author_display_cols + 2) {
1405 waddch(view->window, ' ');
1412 err = got_object_commit_get_logmsg(&logmsg0, commit);
1416 while (*logmsg == '\n')
1418 newline = strchr(logmsg, '\n');
1421 limit = avail - col;
1422 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1425 waddwstr(view->window, wlogmsg);
1426 col += logmsg_width;
1427 while (col < avail) {
1428 waddch(view->window, ' ');
1440 static struct commit_queue_entry *
1441 alloc_commit_queue_entry(struct got_commit_object *commit,
1442 struct got_object_id *id)
1444 struct commit_queue_entry *entry;
1446 entry = calloc(1, sizeof(*entry));
1451 entry->commit = commit;
1456 pop_commit(struct commit_queue *commits)
1458 struct commit_queue_entry *entry;
1460 entry = TAILQ_FIRST(&commits->head);
1461 TAILQ_REMOVE(&commits->head, entry, entry);
1462 got_object_commit_close(entry->commit);
1463 commits->ncommits--;
1464 /* Don't free entry->id! It is owned by the commit graph. */
1469 free_commits(struct commit_queue *commits)
1471 while (!TAILQ_EMPTY(&commits->head))
1472 pop_commit(commits);
1475 static const struct got_error *
1476 match_commit(int *have_match, struct got_object_id *id,
1477 struct got_commit_object *commit, regex_t *regex)
1479 const struct got_error *err = NULL;
1480 regmatch_t regmatch;
1481 char *id_str = NULL, *logmsg = NULL;
1485 err = got_object_id_str(&id_str, id);
1489 err = got_object_commit_get_logmsg(&logmsg, commit);
1493 if (regexec(regex, got_object_commit_get_author(commit), 1,
1494 ®match, 0) == 0 ||
1495 regexec(regex, got_object_commit_get_committer(commit), 1,
1496 ®match, 0) == 0 ||
1497 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1498 regexec(regex, logmsg, 1, ®match, 0) == 0)
1506 static const struct got_error *
1507 queue_commits(struct tog_log_thread_args *a)
1509 const struct got_error *err = NULL;
1512 * We keep all commits open throughout the lifetime of the log
1513 * view in order to avoid having to re-fetch commits from disk
1514 * while updating the display.
1517 struct got_object_id *id;
1518 struct got_commit_object *commit;
1519 struct commit_queue_entry *entry;
1522 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1524 if (err || id == NULL)
1527 err = got_object_open_as_commit(&commit, a->repo, id);
1530 entry = alloc_commit_queue_entry(commit, id);
1531 if (entry == NULL) {
1532 err = got_error_from_errno("alloc_commit_queue_entry");
1536 errcode = pthread_mutex_lock(&tog_mutex);
1538 err = got_error_set_errno(errcode,
1539 "pthread_mutex_lock");
1543 entry->idx = a->commits->ncommits;
1544 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1545 a->commits->ncommits++;
1547 if (*a->searching == TOG_SEARCH_FORWARD &&
1548 !*a->search_next_done) {
1550 err = match_commit(&have_match, id, commit, a->regex);
1554 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1557 errcode = pthread_mutex_unlock(&tog_mutex);
1558 if (errcode && err == NULL)
1559 err = got_error_set_errno(errcode,
1560 "pthread_mutex_unlock");
1563 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1569 select_commit(struct tog_log_view_state *s)
1571 struct commit_queue_entry *entry;
1574 entry = s->first_displayed_entry;
1576 if (ncommits == s->selected) {
1577 s->selected_entry = entry;
1580 entry = TAILQ_NEXT(entry, entry);
1585 static const struct got_error *
1586 draw_commits(struct tog_view *view)
1588 const struct got_error *err = NULL;
1589 struct tog_log_view_state *s = &view->state.log;
1590 struct commit_queue_entry *entry = s->selected_entry;
1591 const int limit = view->nlines;
1593 int ncommits, author_cols = 4;
1594 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1595 char *refs_str = NULL;
1597 struct tog_color *tc;
1598 static const size_t date_display_cols = 12;
1600 if (s->selected_entry &&
1601 !(view->searching && view->search_next_done == 0)) {
1602 struct got_reflist_head *refs;
1603 err = got_object_id_str(&id_str, s->selected_entry->id);
1606 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1607 s->selected_entry->id);
1609 err = build_refs_str(&refs_str, refs,
1610 s->selected_entry->id, s->repo);
1616 if (s->thread_args.commits_needed == 0)
1617 halfdelay(10); /* disable fast refresh */
1619 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1620 if (asprintf(&ncommits_str, " [%d/%d] %s",
1621 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1622 (view->searching && !view->search_next_done) ?
1623 "searching..." : "loading...") == -1) {
1624 err = got_error_from_errno("asprintf");
1628 const char *search_str = NULL;
1630 if (view->searching) {
1631 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1632 search_str = "no more matches";
1633 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1634 search_str = "no matches found";
1635 else if (!view->search_next_done)
1636 search_str = "searching...";
1639 if (asprintf(&ncommits_str, " [%d/%d] %s",
1640 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1641 search_str ? search_str :
1642 (refs_str ? refs_str : "")) == -1) {
1643 err = got_error_from_errno("asprintf");
1648 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1649 if (asprintf(&header, "commit %s %s%s",
1650 id_str ? id_str : "........................................",
1651 s->in_repo_path, ncommits_str) == -1) {
1652 err = got_error_from_errno("asprintf");
1656 } else if (asprintf(&header, "commit %s%s",
1657 id_str ? id_str : "........................................",
1658 ncommits_str) == -1) {
1659 err = got_error_from_errno("asprintf");
1663 err = format_line(&wline, &width, header, view->ncols, 0);
1667 werase(view->window);
1669 if (view_needs_focus_indication(view))
1670 wstandout(view->window);
1671 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1673 wattr_on(view->window,
1674 COLOR_PAIR(tc->colorpair), NULL);
1675 waddwstr(view->window, wline);
1677 wattr_off(view->window,
1678 COLOR_PAIR(tc->colorpair), NULL);
1679 while (width < view->ncols) {
1680 waddch(view->window, ' ');
1683 if (view_needs_focus_indication(view))
1684 wstandend(view->window);
1689 /* Grow author column size if necessary. */
1690 entry = s->first_displayed_entry;
1696 if (ncommits >= limit - 1)
1698 author = strdup(got_object_commit_get_author(entry->commit));
1699 if (author == NULL) {
1700 err = got_error_from_errno("strdup");
1703 err = format_author(&wauthor, &width, author, COLS,
1705 if (author_cols < width)
1706 author_cols = width;
1710 entry = TAILQ_NEXT(entry, entry);
1713 entry = s->first_displayed_entry;
1714 s->last_displayed_entry = s->first_displayed_entry;
1717 if (ncommits >= limit - 1)
1719 if (ncommits == s->selected)
1720 wstandout(view->window);
1721 err = draw_commit(view, entry->commit, entry->id,
1722 date_display_cols, author_cols);
1723 if (ncommits == s->selected)
1724 wstandend(view->window);
1728 s->last_displayed_entry = entry;
1729 entry = TAILQ_NEXT(entry, entry);
1744 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1746 struct commit_queue_entry *entry;
1749 entry = TAILQ_FIRST(&s->commits.head);
1750 if (s->first_displayed_entry == entry)
1753 entry = s->first_displayed_entry;
1754 while (entry && nscrolled < maxscroll) {
1755 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1757 s->first_displayed_entry = entry;
1763 static const struct got_error *
1764 trigger_log_thread(struct tog_view *view, int wait)
1766 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1769 halfdelay(1); /* fast refresh while loading commits */
1771 while (ta->commits_needed > 0 || ta->load_all) {
1772 if (ta->log_complete)
1775 /* Wake the log thread. */
1776 errcode = pthread_cond_signal(&ta->need_commits);
1778 return got_error_set_errno(errcode,
1779 "pthread_cond_signal");
1782 * The mutex will be released while the view loop waits
1783 * in wgetch(), at which time the log thread will run.
1788 /* Display progress update in log view. */
1789 show_log_view(view);
1793 /* Wait right here while next commit is being loaded. */
1794 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1796 return got_error_set_errno(errcode,
1797 "pthread_cond_wait");
1799 /* Display progress update in log view. */
1800 show_log_view(view);
1808 static const struct got_error *
1809 log_scroll_down(struct tog_view *view, int maxscroll)
1811 struct tog_log_view_state *s = &view->state.log;
1812 const struct got_error *err = NULL;
1813 struct commit_queue_entry *pentry;
1814 int nscrolled = 0, ncommits_needed;
1816 if (s->last_displayed_entry == NULL)
1819 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1820 if (s->commits.ncommits < ncommits_needed &&
1821 !s->thread_args.log_complete) {
1823 * Ask the log thread for required amount of commits.
1825 s->thread_args.commits_needed += maxscroll;
1826 err = trigger_log_thread(view, 1);
1832 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1836 s->last_displayed_entry = pentry;
1838 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1841 s->first_displayed_entry = pentry;
1842 } while (++nscrolled < maxscroll);
1847 static const struct got_error *
1848 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1849 struct got_commit_object *commit, struct got_object_id *commit_id,
1850 struct tog_view *log_view, struct got_repository *repo)
1852 const struct got_error *err;
1853 struct got_object_qid *parent_id;
1854 struct tog_view *diff_view;
1856 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1857 if (diff_view == NULL)
1858 return got_error_from_errno("view_open");
1860 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1861 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1862 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1864 *new_view = diff_view;
1868 static const struct got_error *
1869 tree_view_visit_subtree(struct tog_tree_view_state *s,
1870 struct got_tree_object *subtree)
1872 struct tog_parent_tree *parent;
1874 parent = calloc(1, sizeof(*parent));
1876 return got_error_from_errno("calloc");
1878 parent->tree = s->tree;
1879 parent->first_displayed_entry = s->first_displayed_entry;
1880 parent->selected_entry = s->selected_entry;
1881 parent->selected = s->selected;
1882 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1885 s->first_displayed_entry = NULL;
1889 static const struct got_error *
1890 tree_view_walk_path(struct tog_tree_view_state *s,
1891 struct got_object_id *commit_id, const char *path)
1893 const struct got_error *err = NULL;
1894 struct got_tree_object *tree = NULL;
1896 char *slash, *subpath = NULL;
1898 /* Walk the path and open corresponding tree objects. */
1901 struct got_tree_entry *te;
1902 struct got_object_id *tree_id;
1908 /* Ensure the correct subtree entry is selected. */
1909 slash = strchr(p, '/');
1911 te_name = strdup(p);
1913 te_name = strndup(p, slash - p);
1914 if (te_name == NULL) {
1915 err = got_error_from_errno("strndup");
1918 te = got_object_tree_find_entry(s->tree, te_name);
1920 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1925 s->first_displayed_entry = s->selected_entry = te;
1927 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1928 break; /* jump to this file's entry */
1930 slash = strchr(p, '/');
1932 subpath = strndup(path, slash - path);
1934 subpath = strdup(path);
1935 if (subpath == NULL) {
1936 err = got_error_from_errno("strdup");
1940 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1945 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1950 err = tree_view_visit_subtree(s, tree);
1952 got_object_tree_close(tree);
1966 static const struct got_error *
1967 browse_commit_tree(struct tog_view **new_view, int begin_x,
1968 struct commit_queue_entry *entry, const char *path,
1969 const char *head_ref_name, struct got_repository *repo)
1971 const struct got_error *err = NULL;
1972 struct tog_tree_view_state *s;
1973 struct tog_view *tree_view;
1975 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1976 if (tree_view == NULL)
1977 return got_error_from_errno("view_open");
1979 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
1982 s = &tree_view->state.tree;
1984 *new_view = tree_view;
1986 if (got_path_is_root_dir(path))
1989 return tree_view_walk_path(s, entry->id, path);
1992 static const struct got_error *
1993 block_signals_used_by_main_thread(void)
1998 if (sigemptyset(&sigset) == -1)
1999 return got_error_from_errno("sigemptyset");
2001 /* tog handles SIGWINCH and SIGCONT */
2002 if (sigaddset(&sigset, SIGWINCH) == -1)
2003 return got_error_from_errno("sigaddset");
2004 if (sigaddset(&sigset, SIGCONT) == -1)
2005 return got_error_from_errno("sigaddset");
2007 /* ncurses handles SIGTSTP */
2008 if (sigaddset(&sigset, SIGTSTP) == -1)
2009 return got_error_from_errno("sigaddset");
2011 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2013 return got_error_set_errno(errcode, "pthread_sigmask");
2019 log_thread(void *arg)
2021 const struct got_error *err = NULL;
2023 struct tog_log_thread_args *a = arg;
2026 err = block_signals_used_by_main_thread();
2030 while (!done && !err && !tog_sigpipe_received) {
2031 err = queue_commits(a);
2033 if (err->code != GOT_ERR_ITER_COMPLETED)
2037 } else if (a->commits_needed > 0 && !a->load_all)
2038 a->commits_needed--;
2040 errcode = pthread_mutex_lock(&tog_mutex);
2042 err = got_error_set_errno(errcode,
2043 "pthread_mutex_lock");
2045 } else if (*a->quit)
2047 else if (*a->first_displayed_entry == NULL) {
2048 *a->first_displayed_entry =
2049 TAILQ_FIRST(&a->commits->head);
2050 *a->selected_entry = *a->first_displayed_entry;
2053 errcode = pthread_cond_signal(&a->commit_loaded);
2055 err = got_error_set_errno(errcode,
2056 "pthread_cond_signal");
2057 pthread_mutex_unlock(&tog_mutex);
2062 a->commits_needed = 0;
2064 if (a->commits_needed == 0 && !a->load_all) {
2065 errcode = pthread_cond_wait(&a->need_commits,
2068 err = got_error_set_errno(errcode,
2069 "pthread_cond_wait");
2075 errcode = pthread_mutex_unlock(&tog_mutex);
2076 if (errcode && err == NULL)
2077 err = got_error_set_errno(errcode,
2078 "pthread_mutex_unlock");
2080 a->log_complete = 1;
2084 static const struct got_error *
2085 stop_log_thread(struct tog_log_view_state *s)
2087 const struct got_error *err = NULL;
2092 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2094 return got_error_set_errno(errcode,
2095 "pthread_cond_signal");
2096 errcode = pthread_mutex_unlock(&tog_mutex);
2098 return got_error_set_errno(errcode,
2099 "pthread_mutex_unlock");
2100 errcode = pthread_join(s->thread, (void **)&err);
2102 return got_error_set_errno(errcode, "pthread_join");
2103 errcode = pthread_mutex_lock(&tog_mutex);
2105 return got_error_set_errno(errcode,
2106 "pthread_mutex_lock");
2107 s->thread = 0; //NULL;
2110 if (s->thread_args.repo) {
2111 err = got_repo_close(s->thread_args.repo);
2112 s->thread_args.repo = NULL;
2115 if (s->thread_args.graph) {
2116 got_commit_graph_close(s->thread_args.graph);
2117 s->thread_args.graph = NULL;
2123 static const struct got_error *
2124 close_log_view(struct tog_view *view)
2126 const struct got_error *err = NULL;
2127 struct tog_log_view_state *s = &view->state.log;
2130 err = stop_log_thread(s);
2132 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2133 if (errcode && err == NULL)
2134 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2136 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2137 if (errcode && err == NULL)
2138 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2140 free_commits(&s->commits);
2141 free(s->in_repo_path);
2142 s->in_repo_path = NULL;
2145 free(s->head_ref_name);
2146 s->head_ref_name = NULL;
2150 static const struct got_error *
2151 search_start_log_view(struct tog_view *view)
2153 struct tog_log_view_state *s = &view->state.log;
2155 s->matched_entry = NULL;
2156 s->search_entry = NULL;
2160 static const struct got_error *
2161 search_next_log_view(struct tog_view *view)
2163 const struct got_error *err = NULL;
2164 struct tog_log_view_state *s = &view->state.log;
2165 struct commit_queue_entry *entry;
2167 /* Display progress update in log view. */
2168 show_log_view(view);
2172 if (s->search_entry) {
2174 errcode = pthread_mutex_unlock(&tog_mutex);
2176 return got_error_set_errno(errcode,
2177 "pthread_mutex_unlock");
2178 ch = wgetch(view->window);
2179 errcode = pthread_mutex_lock(&tog_mutex);
2181 return got_error_set_errno(errcode,
2182 "pthread_mutex_lock");
2183 if (ch == KEY_BACKSPACE) {
2184 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2187 if (view->searching == TOG_SEARCH_FORWARD)
2188 entry = TAILQ_NEXT(s->search_entry, entry);
2190 entry = TAILQ_PREV(s->search_entry,
2191 commit_queue_head, entry);
2192 } else if (s->matched_entry) {
2193 if (view->searching == TOG_SEARCH_FORWARD)
2194 entry = TAILQ_NEXT(s->matched_entry, entry);
2196 entry = TAILQ_PREV(s->matched_entry,
2197 commit_queue_head, entry);
2199 if (view->searching == TOG_SEARCH_FORWARD)
2200 entry = TAILQ_FIRST(&s->commits.head);
2202 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2208 if (entry == NULL) {
2209 if (s->thread_args.log_complete ||
2210 view->searching == TOG_SEARCH_BACKWARD) {
2211 view->search_next_done =
2212 (s->matched_entry == NULL ?
2213 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2214 s->search_entry = NULL;
2218 * Poke the log thread for more commits and return,
2219 * allowing the main loop to make progress. Search
2220 * will resume at s->search_entry once we come back.
2222 s->thread_args.commits_needed++;
2223 return trigger_log_thread(view, 0);
2226 err = match_commit(&have_match, entry->id, entry->commit,
2231 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2232 s->matched_entry = entry;
2236 s->search_entry = entry;
2237 if (view->searching == TOG_SEARCH_FORWARD)
2238 entry = TAILQ_NEXT(entry, entry);
2240 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2243 if (s->matched_entry) {
2244 int cur = s->selected_entry->idx;
2245 while (cur < s->matched_entry->idx) {
2246 err = input_log_view(NULL, view, KEY_DOWN);
2251 while (cur > s->matched_entry->idx) {
2252 err = input_log_view(NULL, view, KEY_UP);
2259 s->search_entry = NULL;
2264 static const struct got_error *
2265 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2266 struct got_repository *repo, const char *head_ref_name,
2267 const char *in_repo_path, int log_branches)
2269 const struct got_error *err = NULL;
2270 struct tog_log_view_state *s = &view->state.log;
2271 struct got_repository *thread_repo = NULL;
2272 struct got_commit_graph *thread_graph = NULL;
2275 if (in_repo_path != s->in_repo_path) {
2276 free(s->in_repo_path);
2277 s->in_repo_path = strdup(in_repo_path);
2278 if (s->in_repo_path == NULL)
2279 return got_error_from_errno("strdup");
2282 /* The commit queue only contains commits being displayed. */
2283 TAILQ_INIT(&s->commits.head);
2284 s->commits.ncommits = 0;
2287 if (head_ref_name) {
2288 s->head_ref_name = strdup(head_ref_name);
2289 if (s->head_ref_name == NULL) {
2290 err = got_error_from_errno("strdup");
2294 s->start_id = got_object_id_dup(start_id);
2295 if (s->start_id == NULL) {
2296 err = got_error_from_errno("got_object_id_dup");
2299 s->log_branches = log_branches;
2301 STAILQ_INIT(&s->colors);
2302 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2303 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2304 get_color_value("TOG_COLOR_COMMIT"));
2307 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2308 get_color_value("TOG_COLOR_AUTHOR"));
2310 free_colors(&s->colors);
2313 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2314 get_color_value("TOG_COLOR_DATE"));
2316 free_colors(&s->colors);
2321 view->show = show_log_view;
2322 view->input = input_log_view;
2323 view->close = close_log_view;
2324 view->search_start = search_start_log_view;
2325 view->search_next = search_next_log_view;
2327 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2330 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2334 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2335 s->repo, NULL, NULL);
2339 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2341 err = got_error_set_errno(errcode, "pthread_cond_init");
2344 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2346 err = got_error_set_errno(errcode, "pthread_cond_init");
2350 s->thread_args.commits_needed = view->nlines;
2351 s->thread_args.graph = thread_graph;
2352 s->thread_args.commits = &s->commits;
2353 s->thread_args.in_repo_path = s->in_repo_path;
2354 s->thread_args.start_id = s->start_id;
2355 s->thread_args.repo = thread_repo;
2356 s->thread_args.log_complete = 0;
2357 s->thread_args.quit = &s->quit;
2358 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2359 s->thread_args.selected_entry = &s->selected_entry;
2360 s->thread_args.searching = &view->searching;
2361 s->thread_args.search_next_done = &view->search_next_done;
2362 s->thread_args.regex = &view->regex;
2365 close_log_view(view);
2369 static const struct got_error *
2370 show_log_view(struct tog_view *view)
2372 const struct got_error *err;
2373 struct tog_log_view_state *s = &view->state.log;
2375 if (s->thread == 0) { //NULL) {
2376 int errcode = pthread_create(&s->thread, NULL, log_thread,
2379 return got_error_set_errno(errcode, "pthread_create");
2380 if (s->thread_args.commits_needed > 0) {
2381 err = trigger_log_thread(view, 1);
2387 return draw_commits(view);
2390 static const struct got_error *
2391 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2393 const struct got_error *err = NULL;
2394 struct tog_log_view_state *s = &view->state.log;
2395 struct tog_view *diff_view = NULL, *tree_view = NULL;
2396 struct tog_view *ref_view = NULL;
2397 struct commit_queue_entry *entry;
2400 if (s->thread_args.load_all) {
2401 if (ch == KEY_BACKSPACE)
2402 s->thread_args.load_all = 0;
2403 else if (s->thread_args.log_complete) {
2404 s->thread_args.load_all = 0;
2405 log_scroll_down(view, s->commits.ncommits);
2406 s->selected = MIN(view->nlines - 2,
2407 s->commits.ncommits - 1);
2421 if (s->first_displayed_entry == NULL)
2423 if (s->selected > 0)
2426 log_scroll_up(s, 1);
2432 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2437 if (s->first_displayed_entry == NULL)
2439 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2442 log_scroll_up(s, view->nlines - 1);
2449 if (s->first_displayed_entry == NULL)
2451 if (s->selected < MIN(view->nlines - 2,
2452 s->commits.ncommits - 1))
2455 err = log_scroll_down(view, 1);
2463 /* We don't know yet how many commits, so we're forced to
2464 * traverse them all. */
2465 if (!s->thread_args.log_complete) {
2466 s->thread_args.load_all = 1;
2467 return trigger_log_thread(view, 0);
2471 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2472 for (n = 0; n < view->nlines - 1; n++) {
2475 s->first_displayed_entry = entry;
2476 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2479 s->selected = n - 1;
2485 struct commit_queue_entry *first;
2486 first = s->first_displayed_entry;
2489 err = log_scroll_down(view, view->nlines - 1);
2492 if (first == s->first_displayed_entry &&
2493 s->selected < MIN(view->nlines - 2,
2494 s->commits.ncommits - 1)) {
2495 /* can't scroll further down */
2496 s->selected = MIN(view->nlines - 2,
2497 s->commits.ncommits - 1);
2503 if (s->selected > view->nlines - 2)
2504 s->selected = view->nlines - 2;
2505 if (s->selected > s->commits.ncommits - 1)
2506 s->selected = s->commits.ncommits - 1;
2508 if (s->commits.ncommits < view->nlines - 1 &&
2509 !s->thread_args.log_complete) {
2510 s->thread_args.commits_needed += (view->nlines - 1) -
2511 s->commits.ncommits;
2512 err = trigger_log_thread(view, 1);
2518 if (s->selected_entry == NULL)
2520 if (view_is_parent_view(view))
2521 begin_x = view_split_begin_x(view->begin_x);
2522 err = open_diff_view_for_commit(&diff_view, begin_x,
2523 s->selected_entry->commit, s->selected_entry->id,
2528 diff_view->focussed = 1;
2529 if (view_is_parent_view(view)) {
2530 err = view_close_child(view);
2533 view_set_child(view, diff_view);
2534 view->focus_child = 1;
2536 *new_view = diff_view;
2539 if (s->selected_entry == NULL)
2541 if (view_is_parent_view(view))
2542 begin_x = view_split_begin_x(view->begin_x);
2543 err = browse_commit_tree(&tree_view, begin_x,
2544 s->selected_entry, s->in_repo_path, s->head_ref_name,
2549 tree_view->focussed = 1;
2550 if (view_is_parent_view(view)) {
2551 err = view_close_child(view);
2554 view_set_child(view, tree_view);
2555 view->focus_child = 1;
2557 *new_view = tree_view;
2562 if (ch == KEY_BACKSPACE &&
2563 got_path_is_root_dir(s->in_repo_path))
2565 err = stop_log_thread(s);
2568 if (ch == KEY_BACKSPACE) {
2570 err = got_path_dirname(&parent_path, s->in_repo_path);
2573 free(s->in_repo_path);
2574 s->in_repo_path = parent_path;
2575 s->thread_args.in_repo_path = s->in_repo_path;
2576 } else if (ch == CTRL('l')) {
2577 struct got_object_id *start_id;
2578 err = got_repo_match_object_id(&start_id, NULL,
2579 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2580 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2584 s->start_id = start_id;
2585 s->thread_args.start_id = s->start_id;
2587 s->log_branches = !s->log_branches;
2589 err = got_repo_open(&s->thread_args.repo,
2590 got_repo_get_path(s->repo), NULL);
2594 err = tog_load_refs(s->repo);
2597 err = got_commit_graph_open(&s->thread_args.graph,
2598 s->in_repo_path, !s->log_branches);
2601 err = got_commit_graph_iter_start(s->thread_args.graph,
2602 s->start_id, s->repo, NULL, NULL);
2605 free_commits(&s->commits);
2606 s->first_displayed_entry = NULL;
2607 s->last_displayed_entry = NULL;
2608 s->selected_entry = NULL;
2610 s->thread_args.log_complete = 0;
2612 s->thread_args.commits_needed = view->nlines;
2615 if (view_is_parent_view(view))
2616 begin_x = view_split_begin_x(view->begin_x);
2617 ref_view = view_open(view->nlines, view->ncols,
2618 view->begin_y, begin_x, TOG_VIEW_REF);
2619 if (ref_view == NULL)
2620 return got_error_from_errno("view_open");
2621 err = open_ref_view(ref_view, s->repo);
2623 view_close(ref_view);
2627 ref_view->focussed = 1;
2628 if (view_is_parent_view(view)) {
2629 err = view_close_child(view);
2632 view_set_child(view, ref_view);
2633 view->focus_child = 1;
2635 *new_view = ref_view;
2644 static const struct got_error *
2645 apply_unveil(const char *repo_path, const char *worktree_path)
2647 const struct got_error *error;
2650 if (unveil("gmon.out", "rwc") != 0)
2651 return got_error_from_errno2("unveil", "gmon.out");
2653 if (repo_path && unveil(repo_path, "r") != 0)
2654 return got_error_from_errno2("unveil", repo_path);
2656 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2657 return got_error_from_errno2("unveil", worktree_path);
2659 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2660 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2662 error = got_privsep_unveil_exec_helpers();
2666 if (unveil(NULL, NULL) != 0)
2667 return got_error_from_errno("unveil");
2677 halfdelay(1); /* Do fast refresh while initial view is loading. */
2680 intrflush(stdscr, FALSE);
2681 keypad(stdscr, TRUE);
2683 if (getenv("TOG_COLORS") != NULL) {
2685 use_default_colors();
2687 signal(SIGWINCH, tog_sigwinch);
2688 signal(SIGPIPE, tog_sigpipe);
2689 signal(SIGCONT, tog_sigcont);
2692 static const struct got_error *
2693 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2694 struct got_repository *repo, struct got_worktree *worktree)
2696 const struct got_error *err = NULL;
2699 *in_repo_path = strdup("/");
2700 if (*in_repo_path == NULL)
2701 return got_error_from_errno("strdup");
2706 const char *prefix = got_worktree_get_path_prefix(worktree);
2709 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2712 if (asprintf(in_repo_path, "%s%s%s", prefix,
2713 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2715 err = got_error_from_errno("asprintf");
2716 *in_repo_path = NULL;
2720 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2725 static const struct got_error *
2726 cmd_log(int argc, char *argv[])
2728 const struct got_error *error;
2729 struct got_repository *repo = NULL;
2730 struct got_worktree *worktree = NULL;
2731 struct got_object_id *start_id = NULL;
2732 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2733 char *start_commit = NULL, *label = NULL;
2734 struct got_reference *ref = NULL;
2735 const char *head_ref_name = NULL;
2736 int ch, log_branches = 0;
2737 struct tog_view *view;
2739 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2745 start_commit = optarg;
2748 repo_path = realpath(optarg, NULL);
2749 if (repo_path == NULL)
2750 return got_error_from_errno2("realpath",
2765 if (repo_path == NULL) {
2766 cwd = getcwd(NULL, 0);
2768 return got_error_from_errno("getcwd");
2769 error = got_worktree_open(&worktree, cwd);
2770 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2774 strdup(got_worktree_get_repo_path(worktree));
2776 repo_path = strdup(cwd);
2777 if (repo_path == NULL) {
2778 error = got_error_from_errno("strdup");
2783 error = got_repo_open(&repo, repo_path, NULL);
2787 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2794 error = apply_unveil(got_repo_get_path(repo),
2795 worktree ? got_worktree_get_root_path(worktree) : NULL);
2799 /* already loaded by tog_log_with_path()? */
2800 if (TAILQ_EMPTY(&tog_refs)) {
2801 error = tog_load_refs(repo);
2806 if (start_commit == NULL) {
2807 error = got_repo_match_object_id(&start_id, &label,
2808 worktree ? got_worktree_get_head_ref_name(worktree) :
2809 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2812 head_ref_name = label;
2814 error = got_ref_open(&ref, repo, start_commit, 0);
2816 head_ref_name = got_ref_get_name(ref);
2817 else if (error->code != GOT_ERR_NOT_REF)
2819 error = got_repo_match_object_id(&start_id, NULL,
2820 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2825 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2827 error = got_error_from_errno("view_open");
2830 error = open_log_view(view, start_id, repo, head_ref_name,
2831 in_repo_path, log_branches);
2835 /* Release work tree lock. */
2836 got_worktree_close(worktree);
2839 error = view_loop(view);
2849 const struct got_error *close_err = got_repo_close(repo);
2854 got_worktree_close(worktree);
2863 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2864 "[-w] object1 object2\n", getprogname());
2869 match_line(const char *line, regex_t *regex, size_t nmatch,
2870 regmatch_t *regmatch)
2872 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2876 match_color(struct tog_colors *colors, const char *line)
2878 struct tog_color *tc = NULL;
2880 STAILQ_FOREACH(tc, colors, entry) {
2881 if (match_line(line, &tc->regex, 0, NULL))
2888 static const struct got_error *
2889 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2890 WINDOW *window, regmatch_t *regmatch)
2892 const struct got_error *err = NULL;
2899 s = strndup(line, regmatch->rm_so);
2901 return got_error_from_errno("strndup");
2903 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2908 waddwstr(window, wline);
2915 s = strndup(line + regmatch->rm_so,
2916 regmatch->rm_eo - regmatch->rm_so);
2918 err = got_error_from_errno("strndup");
2922 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2927 wattr_on(window, A_STANDOUT, NULL);
2928 waddwstr(window, wline);
2929 wattr_off(window, A_STANDOUT, NULL);
2936 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2937 err = format_line(&wline, &width,
2938 line + regmatch->rm_eo, wlimit, col_tab_align);
2941 waddwstr(window, wline);
2949 static const struct got_error *
2950 draw_file(struct tog_view *view, const char *header)
2952 struct tog_diff_view_state *s = &view->state.diff;
2953 regmatch_t *regmatch = &view->regmatch;
2954 const struct got_error *err;
2957 size_t linesize = 0;
2959 struct tog_color *tc;
2962 int max_lines = view->nlines;
2963 int nlines = s->nlines;
2966 line_offset = s->line_offsets[s->first_displayed_line - 1];
2967 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2968 return got_error_from_errno("fseek");
2970 werase(view->window);
2973 if (asprintf(&line, "[%d/%d] %s",
2974 s->first_displayed_line - 1 + s->selected_line, nlines,
2976 return got_error_from_errno("asprintf");
2977 err = format_line(&wline, &width, line, view->ncols, 0);
2982 if (view_needs_focus_indication(view))
2983 wstandout(view->window);
2984 waddwstr(view->window, wline);
2987 if (view_needs_focus_indication(view))
2988 wstandend(view->window);
2989 if (width <= view->ncols - 1)
2990 waddch(view->window, '\n');
2999 while (max_lines > 0 && nprinted < max_lines) {
3000 linelen = getline(&line, &linesize, s->f);
3001 if (linelen == -1) {
3007 return got_ferror(s->f, GOT_ERR_IO);
3010 tc = match_color(&s->colors, line);
3012 wattr_on(view->window,
3013 COLOR_PAIR(tc->colorpair), NULL);
3014 if (s->first_displayed_line + nprinted == s->matched_line &&
3015 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3016 err = add_matched_line(&width, line, view->ncols, 0,
3017 view->window, regmatch);
3023 err = format_line(&wline, &width, line, view->ncols, 0);
3028 waddwstr(view->window, wline);
3033 wattr_off(view->window,
3034 COLOR_PAIR(tc->colorpair), NULL);
3035 if (width <= view->ncols - 1)
3036 waddch(view->window, '\n');
3041 s->last_displayed_line = s->first_displayed_line +
3044 s->last_displayed_line = s->first_displayed_line;
3049 while (nprinted < view->nlines) {
3050 waddch(view->window, '\n');
3054 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3059 wstandout(view->window);
3060 waddwstr(view->window, wline);
3063 wstandend(view->window);
3070 get_datestr(time_t *time, char *datebuf)
3072 struct tm mytm, *tm;
3075 tm = gmtime_r(time, &mytm);
3078 s = asctime_r(tm, datebuf);
3081 p = strchr(s, '\n');
3087 static const struct got_error *
3088 get_changed_paths(struct got_pathlist_head *paths,
3089 struct got_commit_object *commit, struct got_repository *repo)
3091 const struct got_error *err = NULL;
3092 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3093 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3094 struct got_object_qid *qid;
3096 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3098 struct got_commit_object *pcommit;
3099 err = got_object_open_as_commit(&pcommit, repo,
3104 tree_id1 = got_object_id_dup(
3105 got_object_commit_get_tree_id(pcommit));
3106 if (tree_id1 == NULL) {
3107 got_object_commit_close(pcommit);
3108 return got_error_from_errno("got_object_id_dup");
3110 got_object_commit_close(pcommit);
3115 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3120 tree_id2 = got_object_commit_get_tree_id(commit);
3121 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3125 err = got_diff_tree(tree1, tree2, "", "", repo,
3126 got_diff_tree_collect_changed_paths, paths, 0);
3129 got_object_tree_close(tree1);
3131 got_object_tree_close(tree2);
3136 static const struct got_error *
3137 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3141 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3143 return got_error_from_errno("reallocarray");
3145 (*line_offsets)[*nlines] = off;
3150 static const struct got_error *
3151 write_commit_info(off_t **line_offsets, size_t *nlines,
3152 struct got_object_id *commit_id, struct got_reflist_head *refs,
3153 struct got_repository *repo, FILE *outfile)
3155 const struct got_error *err = NULL;
3156 char datebuf[26], *datestr;
3157 struct got_commit_object *commit;
3158 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3159 time_t committer_time;
3160 const char *author, *committer;
3161 char *refs_str = NULL;
3162 struct got_pathlist_head changed_paths;
3163 struct got_pathlist_entry *pe;
3167 TAILQ_INIT(&changed_paths);
3170 err = build_refs_str(&refs_str, refs, commit_id, repo);
3175 err = got_object_open_as_commit(&commit, repo, commit_id);
3179 err = got_object_id_str(&id_str, commit_id);
3181 err = got_error_from_errno("got_object_id_str");
3185 err = add_line_offset(line_offsets, nlines, 0);
3189 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3190 refs_str ? refs_str : "", refs_str ? ")" : "");
3192 err = got_error_from_errno("fprintf");
3196 err = add_line_offset(line_offsets, nlines, outoff);
3200 n = fprintf(outfile, "from: %s\n",
3201 got_object_commit_get_author(commit));
3203 err = got_error_from_errno("fprintf");
3207 err = add_line_offset(line_offsets, nlines, outoff);
3211 committer_time = got_object_commit_get_committer_time(commit);
3212 datestr = get_datestr(&committer_time, datebuf);
3214 n = fprintf(outfile, "date: %s UTC\n", datestr);
3216 err = got_error_from_errno("fprintf");
3220 err = add_line_offset(line_offsets, nlines, outoff);
3224 author = got_object_commit_get_author(commit);
3225 committer = got_object_commit_get_committer(commit);
3226 if (strcmp(author, committer) != 0) {
3227 n = fprintf(outfile, "via: %s\n", committer);
3229 err = got_error_from_errno("fprintf");
3233 err = add_line_offset(line_offsets, nlines, outoff);
3237 err = got_object_commit_get_logmsg(&logmsg, commit);
3241 while ((line = strsep(&s, "\n")) != NULL) {
3242 n = fprintf(outfile, "%s\n", line);
3244 err = got_error_from_errno("fprintf");
3248 err = add_line_offset(line_offsets, nlines, outoff);
3253 err = get_changed_paths(&changed_paths, commit, repo);
3256 TAILQ_FOREACH(pe, &changed_paths, entry) {
3257 struct got_diff_changed_path *cp = pe->data;
3258 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3260 err = got_error_from_errno("fprintf");
3264 err = add_line_offset(line_offsets, nlines, outoff);
3267 free((char *)pe->path);
3271 fputc('\n', outfile);
3273 err = add_line_offset(line_offsets, nlines, outoff);
3275 got_pathlist_free(&changed_paths);
3279 got_object_commit_close(commit);
3281 free(*line_offsets);
3282 *line_offsets = NULL;
3288 static const struct got_error *
3289 create_diff(struct tog_diff_view_state *s)
3291 const struct got_error *err = NULL;
3295 free(s->line_offsets);
3296 s->line_offsets = malloc(sizeof(off_t));
3297 if (s->line_offsets == NULL)
3298 return got_error_from_errno("malloc");
3303 err = got_error_from_errno("got_opentemp");
3306 if (s->f && fclose(s->f) == EOF) {
3307 err = got_error_from_errno("fclose");
3313 err = got_object_get_type(&obj_type, s->repo, s->id1);
3315 err = got_object_get_type(&obj_type, s->repo, s->id2);
3320 case GOT_OBJ_TYPE_BLOB:
3321 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3322 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3323 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3325 case GOT_OBJ_TYPE_TREE:
3326 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3327 s->id1, s->id2, "", "", s->diff_context,
3328 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3330 case GOT_OBJ_TYPE_COMMIT: {
3331 const struct got_object_id_queue *parent_ids;
3332 struct got_object_qid *pid;
3333 struct got_commit_object *commit2;
3334 struct got_reflist_head *refs;
3336 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3339 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3340 /* Show commit info if we're diffing to a parent/root commit. */
3341 if (s->id1 == NULL) {
3342 err = write_commit_info(&s->line_offsets, &s->nlines,
3343 s->id2, refs, s->repo, s->f);
3347 parent_ids = got_object_commit_get_parent_ids(commit2);
3348 STAILQ_FOREACH(pid, parent_ids, entry) {
3349 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3350 err = write_commit_info(
3351 &s->line_offsets, &s->nlines,
3352 s->id2, refs, s->repo, s->f);
3359 got_object_commit_close(commit2);
3361 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3362 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3363 s->force_text_diff, s->repo, s->f);
3367 err = got_error(GOT_ERR_OBJ_TYPE);
3373 if (s->f && fflush(s->f) != 0 && err == NULL)
3374 err = got_error_from_errno("fflush");
3379 diff_view_indicate_progress(struct tog_view *view)
3381 mvwaddstr(view->window, 0, 0, "diffing...");
3386 static const struct got_error *
3387 search_start_diff_view(struct tog_view *view)
3389 struct tog_diff_view_state *s = &view->state.diff;
3391 s->matched_line = 0;
3395 static const struct got_error *
3396 search_next_diff_view(struct tog_view *view)
3398 struct tog_diff_view_state *s = &view->state.diff;
3401 size_t linesize = 0;
3404 if (!view->searching) {
3405 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3409 if (s->matched_line) {
3410 if (view->searching == TOG_SEARCH_FORWARD)
3411 lineno = s->matched_line + 1;
3413 lineno = s->matched_line - 1;
3415 if (view->searching == TOG_SEARCH_FORWARD)
3424 if (lineno <= 0 || lineno > s->nlines) {
3425 if (s->matched_line == 0) {
3426 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3430 if (view->searching == TOG_SEARCH_FORWARD)
3436 offset = s->line_offsets[lineno - 1];
3437 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3439 return got_error_from_errno("fseeko");
3441 linelen = getline(&line, &linesize, s->f);
3442 if (linelen != -1 &&
3443 match_line(line, &view->regex, 1, &view->regmatch)) {
3444 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3445 s->matched_line = lineno;
3448 if (view->searching == TOG_SEARCH_FORWARD)
3455 if (s->matched_line) {
3456 s->first_displayed_line = s->matched_line;
3457 s->selected_line = 1;
3463 static const struct got_error *
3464 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3465 struct got_object_id *id2, const char *label1, const char *label2,
3466 int diff_context, int ignore_whitespace, int force_text_diff,
3467 struct tog_view *log_view, struct got_repository *repo)
3469 const struct got_error *err;
3470 struct tog_diff_view_state *s = &view->state.diff;
3472 if (id1 != NULL && id2 != NULL) {
3474 err = got_object_get_type(&type1, repo, id1);
3477 err = got_object_get_type(&type2, repo, id2);
3482 return got_error(GOT_ERR_OBJ_TYPE);
3484 s->first_displayed_line = 1;
3485 s->last_displayed_line = view->nlines;
3486 s->selected_line = 1;
3494 s->id1 = got_object_id_dup(id1);
3496 return got_error_from_errno("got_object_id_dup");
3500 s->id2 = got_object_id_dup(id2);
3501 if (s->id2 == NULL) {
3504 return got_error_from_errno("got_object_id_dup");
3507 s->first_displayed_line = 1;
3508 s->last_displayed_line = view->nlines;
3509 s->diff_context = diff_context;
3510 s->ignore_whitespace = ignore_whitespace;
3511 s->force_text_diff = force_text_diff;
3512 s->log_view = log_view;
3515 STAILQ_INIT(&s->colors);
3516 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3517 err = add_color(&s->colors,
3518 "^-", TOG_COLOR_DIFF_MINUS,
3519 get_color_value("TOG_COLOR_DIFF_MINUS"));
3522 err = add_color(&s->colors, "^\\+",
3523 TOG_COLOR_DIFF_PLUS,
3524 get_color_value("TOG_COLOR_DIFF_PLUS"));
3526 free_colors(&s->colors);
3529 err = add_color(&s->colors,
3530 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3531 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3533 free_colors(&s->colors);
3537 err = add_color(&s->colors,
3538 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3539 TOG_COLOR_DIFF_META,
3540 get_color_value("TOG_COLOR_DIFF_META"));
3542 free_colors(&s->colors);
3546 err = add_color(&s->colors,
3547 "^(from|via): ", TOG_COLOR_AUTHOR,
3548 get_color_value("TOG_COLOR_AUTHOR"));
3550 free_colors(&s->colors);
3554 err = add_color(&s->colors,
3555 "^date: ", TOG_COLOR_DATE,
3556 get_color_value("TOG_COLOR_DATE"));
3558 free_colors(&s->colors);
3563 if (log_view && view_is_splitscreen(view))
3564 show_log_view(log_view); /* draw vborder */
3565 diff_view_indicate_progress(view);
3567 s->line_offsets = NULL;
3569 err = create_diff(s);
3575 free_colors(&s->colors);
3579 view->show = show_diff_view;
3580 view->input = input_diff_view;
3581 view->close = close_diff_view;
3582 view->search_start = search_start_diff_view;
3583 view->search_next = search_next_diff_view;
3588 static const struct got_error *
3589 close_diff_view(struct tog_view *view)
3591 const struct got_error *err = NULL;
3592 struct tog_diff_view_state *s = &view->state.diff;
3598 if (s->f && fclose(s->f) == EOF)
3599 err = got_error_from_errno("fclose");
3600 free_colors(&s->colors);
3601 free(s->line_offsets);
3602 s->line_offsets = NULL;
3607 static const struct got_error *
3608 show_diff_view(struct tog_view *view)
3610 const struct got_error *err;
3611 struct tog_diff_view_state *s = &view->state.diff;
3612 char *id_str1 = NULL, *id_str2, *header;
3613 const char *label1, *label2;
3616 err = got_object_id_str(&id_str1, s->id1);
3619 label1 = s->label1 ? : id_str1;
3621 label1 = "/dev/null";
3623 err = got_object_id_str(&id_str2, s->id2);
3626 label2 = s->label2 ? : id_str2;
3628 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3629 err = got_error_from_errno("asprintf");
3637 err = draw_file(view, header);
3642 static const struct got_error *
3643 set_selected_commit(struct tog_diff_view_state *s,
3644 struct commit_queue_entry *entry)
3646 const struct got_error *err;
3647 const struct got_object_id_queue *parent_ids;
3648 struct got_commit_object *selected_commit;
3649 struct got_object_qid *pid;
3652 s->id2 = got_object_id_dup(entry->id);
3654 return got_error_from_errno("got_object_id_dup");
3656 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3659 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3661 pid = STAILQ_FIRST(parent_ids);
3662 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3663 got_object_commit_close(selected_commit);
3667 static const struct got_error *
3668 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3670 const struct got_error *err = NULL;
3671 struct tog_diff_view_state *s = &view->state.diff;
3672 struct tog_log_view_state *ls;
3673 struct commit_queue_entry *old_selected_entry;
3675 size_t linesize = 0;
3683 s->force_text_diff = !s->force_text_diff;
3685 s->ignore_whitespace = !s->ignore_whitespace;
3686 wclear(view->window);
3687 s->first_displayed_line = 1;
3688 s->last_displayed_line = view->nlines;
3689 diff_view_indicate_progress(view);
3690 err = create_diff(s);
3694 s->first_displayed_line = 1;
3701 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3706 if (s->first_displayed_line > 1)
3707 s->first_displayed_line--;
3711 if (s->first_displayed_line == 1)
3714 while (i++ < view->nlines - 1 &&
3715 s->first_displayed_line > 1)
3716 s->first_displayed_line--;
3721 s->first_displayed_line++;
3729 while (!s->eof && i++ < view->nlines - 1) {
3730 linelen = getline(&line, &linesize, s->f);
3731 s->first_displayed_line++;
3732 if (linelen == -1) {
3736 err = got_ferror(s->f, GOT_ERR_IO);
3743 if (s->diff_context > 0) {
3745 diff_view_indicate_progress(view);
3746 err = create_diff(s);
3747 if (s->first_displayed_line + view->nlines - 1 >
3749 s->first_displayed_line = 1;
3750 s->last_displayed_line = view->nlines;
3755 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3757 diff_view_indicate_progress(view);
3758 err = create_diff(s);
3763 if (s->log_view == NULL)
3765 ls = &s->log_view->state.log;
3766 old_selected_entry = ls->selected_entry;
3768 err = input_log_view(NULL, s->log_view, KEY_UP);
3772 if (old_selected_entry == ls->selected_entry)
3775 err = set_selected_commit(s, ls->selected_entry);
3779 s->first_displayed_line = 1;
3780 s->last_displayed_line = view->nlines;
3782 diff_view_indicate_progress(view);
3783 err = create_diff(s);
3787 if (s->log_view == NULL)
3789 ls = &s->log_view->state.log;
3790 old_selected_entry = ls->selected_entry;
3792 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3796 if (old_selected_entry == ls->selected_entry)
3799 err = set_selected_commit(s, ls->selected_entry);
3803 s->first_displayed_line = 1;
3804 s->last_displayed_line = view->nlines;
3806 diff_view_indicate_progress(view);
3807 err = create_diff(s);
3816 static const struct got_error *
3817 cmd_diff(int argc, char *argv[])
3819 const struct got_error *error = NULL;
3820 struct got_repository *repo = NULL;
3821 struct got_worktree *worktree = NULL;
3822 struct got_object_id *id1 = NULL, *id2 = NULL;
3823 char *repo_path = NULL, *cwd = NULL;
3824 char *id_str1 = NULL, *id_str2 = NULL;
3825 char *label1 = NULL, *label2 = NULL;
3826 int diff_context = 3, ignore_whitespace = 0;
3827 int ch, force_text_diff = 0;
3829 struct tog_view *view;
3831 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3834 force_text_diff = 1;
3837 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3840 err(1, "-C option %s", errstr);
3843 repo_path = realpath(optarg, NULL);
3844 if (repo_path == NULL)
3845 return got_error_from_errno2("realpath",
3847 got_path_strip_trailing_slashes(repo_path);
3850 ignore_whitespace = 1;
3862 usage_diff(); /* TODO show local worktree changes */
3863 } else if (argc == 2) {
3869 if (repo_path == NULL) {
3870 cwd = getcwd(NULL, 0);
3872 return got_error_from_errno("getcwd");
3873 error = got_worktree_open(&worktree, cwd);
3874 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3878 strdup(got_worktree_get_repo_path(worktree));
3880 repo_path = strdup(cwd);
3881 if (repo_path == NULL) {
3882 error = got_error_from_errno("strdup");
3887 error = got_repo_open(&repo, repo_path, NULL);
3893 error = apply_unveil(got_repo_get_path(repo), NULL);
3897 error = tog_load_refs(repo);
3901 error = got_repo_match_object_id(&id1, &label1, id_str1,
3902 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3906 error = got_repo_match_object_id(&id2, &label2, id_str2,
3907 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3911 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3913 error = got_error_from_errno("view_open");
3916 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3917 ignore_whitespace, force_text_diff, NULL, repo);
3920 error = view_loop(view);
3927 const struct got_error *close_err = got_repo_close(repo);
3932 got_worktree_close(worktree);
3941 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3946 struct tog_blame_line {
3948 struct got_object_id *id;
3951 static const struct got_error *
3952 draw_blame(struct tog_view *view)
3954 struct tog_blame_view_state *s = &view->state.blame;
3955 struct tog_blame *blame = &s->blame;
3956 regmatch_t *regmatch = &view->regmatch;
3957 const struct got_error *err;
3958 int lineno = 0, nprinted = 0;
3960 size_t linesize = 0;
3964 struct tog_blame_line *blame_line;
3965 struct got_object_id *prev_id = NULL;
3967 struct tog_color *tc;
3969 err = got_object_id_str(&id_str, s->blamed_commit->id);
3974 werase(view->window);
3976 if (asprintf(&line, "commit %s", id_str) == -1) {
3977 err = got_error_from_errno("asprintf");
3982 err = format_line(&wline, &width, line, view->ncols, 0);
3987 if (view_needs_focus_indication(view))
3988 wstandout(view->window);
3989 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3991 wattr_on(view->window,
3992 COLOR_PAIR(tc->colorpair), NULL);
3993 waddwstr(view->window, wline);
3995 wattr_off(view->window,
3996 COLOR_PAIR(tc->colorpair), NULL);
3997 if (view_needs_focus_indication(view))
3998 wstandend(view->window);
4001 if (width < view->ncols - 1)
4002 waddch(view->window, '\n');
4004 if (asprintf(&line, "[%d/%d] %s%s",
4005 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4006 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4008 return got_error_from_errno("asprintf");
4011 err = format_line(&wline, &width, line, view->ncols, 0);
4016 waddwstr(view->window, wline);
4019 if (width < view->ncols - 1)
4020 waddch(view->window, '\n');
4023 while (nprinted < view->nlines - 2) {
4024 linelen = getline(&line, &linesize, blame->f);
4025 if (linelen == -1) {
4026 if (feof(blame->f)) {
4031 return got_ferror(blame->f, GOT_ERR_IO);
4033 if (++lineno < s->first_displayed_line)
4036 if (view->focussed && nprinted == s->selected_line - 1)
4037 wstandout(view->window);
4039 if (blame->nlines > 0) {
4040 blame_line = &blame->lines[lineno - 1];
4041 if (blame_line->annotated && prev_id &&
4042 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4044 nprinted == s->selected_line - 1)) {
4045 waddstr(view->window, " ");
4046 } else if (blame_line->annotated) {
4048 err = got_object_id_str(&id_str, blame_line->id);
4053 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4055 wattr_on(view->window,
4056 COLOR_PAIR(tc->colorpair), NULL);
4057 wprintw(view->window, "%.8s", id_str);
4059 wattr_off(view->window,
4060 COLOR_PAIR(tc->colorpair), NULL);
4062 prev_id = blame_line->id;
4064 waddstr(view->window, "........");
4068 waddstr(view->window, "........");
4072 if (view->focussed && nprinted == s->selected_line - 1)
4073 wstandend(view->window);
4074 waddstr(view->window, " ");
4076 if (view->ncols <= 9) {
4078 wline = wcsdup(L"");
4079 if (wline == NULL) {
4080 err = got_error_from_errno("wcsdup");
4084 } else if (s->first_displayed_line + nprinted ==
4086 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4087 err = add_matched_line(&width, line, view->ncols - 9, 9,
4088 view->window, regmatch);
4095 err = format_line(&wline, &width, line,
4096 view->ncols - 9, 9);
4097 waddwstr(view->window, wline);
4103 if (width <= view->ncols - 1)
4104 waddch(view->window, '\n');
4105 if (++nprinted == 1)
4106 s->first_displayed_line = lineno;
4109 s->last_displayed_line = lineno;
4116 static const struct got_error *
4117 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4119 const struct got_error *err = NULL;
4120 struct tog_blame_cb_args *a = arg;
4121 struct tog_blame_line *line;
4124 if (nlines != a->nlines ||
4125 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4126 return got_error(GOT_ERR_RANGE);
4128 errcode = pthread_mutex_lock(&tog_mutex);
4130 return got_error_set_errno(errcode, "pthread_mutex_lock");
4132 if (*a->quit) { /* user has quit the blame view */
4133 err = got_error(GOT_ERR_ITER_COMPLETED);
4138 goto done; /* no change in this commit */
4140 line = &a->lines[lineno - 1];
4141 if (line->annotated)
4144 line->id = got_object_id_dup(id);
4145 if (line->id == NULL) {
4146 err = got_error_from_errno("got_object_id_dup");
4149 line->annotated = 1;
4151 errcode = pthread_mutex_unlock(&tog_mutex);
4153 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4158 blame_thread(void *arg)
4160 const struct got_error *err, *close_err;
4161 struct tog_blame_thread_args *ta = arg;
4162 struct tog_blame_cb_args *a = ta->cb_args;
4165 err = block_signals_used_by_main_thread();
4169 err = got_blame(ta->path, a->commit_id, ta->repo,
4170 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4171 if (err && err->code == GOT_ERR_CANCELLED)
4174 errcode = pthread_mutex_lock(&tog_mutex);
4176 return (void *)got_error_set_errno(errcode,
4177 "pthread_mutex_lock");
4179 close_err = got_repo_close(ta->repo);
4185 errcode = pthread_mutex_unlock(&tog_mutex);
4186 if (errcode && err == NULL)
4187 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4192 static struct got_object_id *
4193 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4194 int first_displayed_line, int selected_line)
4196 struct tog_blame_line *line;
4201 line = &lines[first_displayed_line - 1 + selected_line - 1];
4202 if (!line->annotated)
4208 static const struct got_error *
4209 stop_blame(struct tog_blame *blame)
4211 const struct got_error *err = NULL;
4214 if (blame->thread) {
4216 errcode = pthread_mutex_unlock(&tog_mutex);
4218 return got_error_set_errno(errcode,
4219 "pthread_mutex_unlock");
4220 errcode = pthread_join(blame->thread, (void **)&err);
4222 return got_error_set_errno(errcode, "pthread_join");
4223 errcode = pthread_mutex_lock(&tog_mutex);
4225 return got_error_set_errno(errcode,
4226 "pthread_mutex_lock");
4227 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4229 blame->thread = 0; //NULL;
4231 if (blame->thread_args.repo) {
4232 const struct got_error *close_err;
4233 close_err = got_repo_close(blame->thread_args.repo);
4236 blame->thread_args.repo = NULL;
4239 if (fclose(blame->f) == EOF && err == NULL)
4240 err = got_error_from_errno("fclose");
4244 for (i = 0; i < blame->nlines; i++)
4245 free(blame->lines[i].id);
4247 blame->lines = NULL;
4249 free(blame->cb_args.commit_id);
4250 blame->cb_args.commit_id = NULL;
4255 static const struct got_error *
4256 cancel_blame_view(void *arg)
4258 const struct got_error *err = NULL;
4262 errcode = pthread_mutex_lock(&tog_mutex);
4264 return got_error_set_errno(errcode,
4265 "pthread_mutex_unlock");
4268 err = got_error(GOT_ERR_CANCELLED);
4270 errcode = pthread_mutex_unlock(&tog_mutex);
4272 return got_error_set_errno(errcode,
4273 "pthread_mutex_lock");
4278 static const struct got_error *
4279 run_blame(struct tog_view *view)
4281 struct tog_blame_view_state *s = &view->state.blame;
4282 struct tog_blame *blame = &s->blame;
4283 const struct got_error *err = NULL;
4284 struct got_blob_object *blob = NULL;
4285 struct got_repository *thread_repo = NULL;
4286 struct got_object_id *obj_id = NULL;
4289 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4294 err = got_object_get_type(&obj_type, s->repo, obj_id);
4298 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4299 err = got_error(GOT_ERR_OBJ_TYPE);
4303 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4306 blame->f = got_opentemp();
4307 if (blame->f == NULL) {
4308 err = got_error_from_errno("got_opentemp");
4311 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4312 &blame->line_offsets, blame->f, blob);
4315 if (blame->nlines == 0) {
4316 s->blame_complete = 1;
4320 /* Don't include \n at EOF in the blame line count. */
4321 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4324 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4325 if (blame->lines == NULL) {
4326 err = got_error_from_errno("calloc");
4330 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4334 blame->cb_args.view = view;
4335 blame->cb_args.lines = blame->lines;
4336 blame->cb_args.nlines = blame->nlines;
4337 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4338 if (blame->cb_args.commit_id == NULL) {
4339 err = got_error_from_errno("got_object_id_dup");
4342 blame->cb_args.quit = &s->done;
4344 blame->thread_args.path = s->path;
4345 blame->thread_args.repo = thread_repo;
4346 blame->thread_args.cb_args = &blame->cb_args;
4347 blame->thread_args.complete = &s->blame_complete;
4348 blame->thread_args.cancel_cb = cancel_blame_view;
4349 blame->thread_args.cancel_arg = &s->done;
4350 s->blame_complete = 0;
4352 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4353 s->first_displayed_line = 1;
4354 s->last_displayed_line = view->nlines;
4355 s->selected_line = 1;
4360 got_object_blob_close(blob);
4367 static const struct got_error *
4368 open_blame_view(struct tog_view *view, char *path,
4369 struct got_object_id *commit_id, struct got_repository *repo)
4371 const struct got_error *err = NULL;
4372 struct tog_blame_view_state *s = &view->state.blame;
4374 STAILQ_INIT(&s->blamed_commits);
4376 s->path = strdup(path);
4377 if (s->path == NULL)
4378 return got_error_from_errno("strdup");
4380 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4386 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4387 s->first_displayed_line = 1;
4388 s->last_displayed_line = view->nlines;
4389 s->selected_line = 1;
4390 s->blame_complete = 0;
4392 s->commit_id = commit_id;
4393 memset(&s->blame, 0, sizeof(s->blame));
4395 STAILQ_INIT(&s->colors);
4396 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4397 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4398 get_color_value("TOG_COLOR_COMMIT"));
4403 view->show = show_blame_view;
4404 view->input = input_blame_view;
4405 view->close = close_blame_view;
4406 view->search_start = search_start_blame_view;
4407 view->search_next = search_next_blame_view;
4409 return run_blame(view);
4412 static const struct got_error *
4413 close_blame_view(struct tog_view *view)
4415 const struct got_error *err = NULL;
4416 struct tog_blame_view_state *s = &view->state.blame;
4418 if (s->blame.thread)
4419 err = stop_blame(&s->blame);
4421 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4422 struct got_object_qid *blamed_commit;
4423 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4424 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4425 got_object_qid_free(blamed_commit);
4429 free_colors(&s->colors);
4434 static const struct got_error *
4435 search_start_blame_view(struct tog_view *view)
4437 struct tog_blame_view_state *s = &view->state.blame;
4439 s->matched_line = 0;
4443 static const struct got_error *
4444 search_next_blame_view(struct tog_view *view)
4446 struct tog_blame_view_state *s = &view->state.blame;
4449 size_t linesize = 0;
4452 if (!view->searching) {
4453 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4457 if (s->matched_line) {
4458 if (view->searching == TOG_SEARCH_FORWARD)
4459 lineno = s->matched_line + 1;
4461 lineno = s->matched_line - 1;
4463 if (view->searching == TOG_SEARCH_FORWARD)
4466 lineno = s->blame.nlines;
4472 if (lineno <= 0 || lineno > s->blame.nlines) {
4473 if (s->matched_line == 0) {
4474 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4478 if (view->searching == TOG_SEARCH_FORWARD)
4481 lineno = s->blame.nlines;
4484 offset = s->blame.line_offsets[lineno - 1];
4485 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4487 return got_error_from_errno("fseeko");
4489 linelen = getline(&line, &linesize, s->blame.f);
4490 if (linelen != -1 &&
4491 match_line(line, &view->regex, 1, &view->regmatch)) {
4492 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4493 s->matched_line = lineno;
4496 if (view->searching == TOG_SEARCH_FORWARD)
4503 if (s->matched_line) {
4504 s->first_displayed_line = s->matched_line;
4505 s->selected_line = 1;
4511 static const struct got_error *
4512 show_blame_view(struct tog_view *view)
4514 const struct got_error *err = NULL;
4515 struct tog_blame_view_state *s = &view->state.blame;
4518 if (s->blame.thread != 0 && !s->blame_complete) {
4519 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4520 &s->blame.thread_args);
4522 return got_error_set_errno(errcode, "pthread_create");
4524 halfdelay(1); /* fast refresh while annotating */
4527 if (s->blame_complete)
4528 halfdelay(10); /* disable fast refresh */
4530 err = draw_blame(view);
4536 static const struct got_error *
4537 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4539 const struct got_error *err = NULL, *thread_err = NULL;
4540 struct tog_view *diff_view;
4541 struct tog_blame_view_state *s = &view->state.blame;
4550 s->selected_line = 1;
4551 s->first_displayed_line = 1;
4555 if (s->blame.nlines < view->nlines - 2) {
4556 s->selected_line = s->blame.nlines;
4557 s->first_displayed_line = 1;
4559 s->selected_line = view->nlines - 2;
4560 s->first_displayed_line = s->blame.nlines -
4566 if (s->selected_line > 1)
4568 else if (s->selected_line == 1 &&
4569 s->first_displayed_line > 1)
4570 s->first_displayed_line--;
4574 if (s->first_displayed_line == 1) {
4575 s->selected_line = 1;
4578 if (s->first_displayed_line > view->nlines - 2)
4579 s->first_displayed_line -=
4582 s->first_displayed_line = 1;
4586 if (s->selected_line < view->nlines - 2 &&
4587 s->first_displayed_line +
4588 s->selected_line <= s->blame.nlines)
4590 else if (s->last_displayed_line <
4592 s->first_displayed_line++;
4596 struct got_object_id *id = NULL;
4597 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4598 s->first_displayed_line, s->selected_line);
4602 struct got_commit_object *commit;
4603 struct got_object_qid *pid;
4604 struct got_object_id *blob_id = NULL;
4606 err = got_object_open_as_commit(&commit,
4611 got_object_commit_get_parent_ids(commit));
4613 got_object_commit_close(commit);
4616 /* Check if path history ends here. */
4617 err = got_object_id_by_path(&blob_id, s->repo,
4620 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4622 got_object_commit_close(commit);
4625 err = got_object_get_type(&obj_type, s->repo,
4628 /* Can't blame non-blob type objects. */
4629 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4630 got_object_commit_close(commit);
4633 err = got_object_qid_alloc(&s->blamed_commit,
4635 got_object_commit_close(commit);
4637 if (got_object_id_cmp(id,
4638 s->blamed_commit->id) == 0)
4640 err = got_object_qid_alloc(&s->blamed_commit,
4646 thread_err = stop_blame(&s->blame);
4650 STAILQ_INSERT_HEAD(&s->blamed_commits,
4651 s->blamed_commit, entry);
4652 err = run_blame(view);
4658 struct got_object_qid *first;
4659 first = STAILQ_FIRST(&s->blamed_commits);
4660 if (!got_object_id_cmp(first->id, s->commit_id))
4663 thread_err = stop_blame(&s->blame);
4667 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4668 got_object_qid_free(s->blamed_commit);
4670 STAILQ_FIRST(&s->blamed_commits);
4671 err = run_blame(view);
4678 struct got_object_id *id = NULL;
4679 struct got_object_qid *pid;
4680 struct got_commit_object *commit = NULL;
4681 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4682 s->first_displayed_line, s->selected_line);
4685 err = got_object_open_as_commit(&commit, s->repo, id);
4689 got_object_commit_get_parent_ids(commit));
4690 if (view_is_parent_view(view))
4691 begin_x = view_split_begin_x(view->begin_x);
4692 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4693 if (diff_view == NULL) {
4694 got_object_commit_close(commit);
4695 err = got_error_from_errno("view_open");
4698 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4699 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4700 got_object_commit_close(commit);
4702 view_close(diff_view);
4706 diff_view->focussed = 1;
4707 if (view_is_parent_view(view)) {
4708 err = view_close_child(view);
4711 view_set_child(view, diff_view);
4712 view->focus_child = 1;
4714 *new_view = diff_view;
4722 if (s->last_displayed_line >= s->blame.nlines &&
4723 s->selected_line >= MIN(s->blame.nlines,
4724 view->nlines - 2)) {
4727 if (s->last_displayed_line >= s->blame.nlines &&
4728 s->selected_line < view->nlines - 2) {
4729 s->selected_line = MIN(s->blame.nlines,
4733 if (s->last_displayed_line + view->nlines - 2
4735 s->first_displayed_line +=
4738 s->first_displayed_line =
4743 if (s->selected_line > view->nlines - 2) {
4744 s->selected_line = MIN(s->blame.nlines,
4751 return thread_err ? thread_err : err;
4754 static const struct got_error *
4755 cmd_blame(int argc, char *argv[])
4757 const struct got_error *error;
4758 struct got_repository *repo = NULL;
4759 struct got_worktree *worktree = NULL;
4760 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4761 char *link_target = NULL;
4762 struct got_object_id *commit_id = NULL;
4763 char *commit_id_str = NULL;
4765 struct tog_view *view;
4767 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4770 commit_id_str = optarg;
4773 repo_path = realpath(optarg, NULL);
4774 if (repo_path == NULL)
4775 return got_error_from_errno2("realpath",
4790 if (repo_path == NULL) {
4791 cwd = getcwd(NULL, 0);
4793 return got_error_from_errno("getcwd");
4794 error = got_worktree_open(&worktree, cwd);
4795 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4799 strdup(got_worktree_get_repo_path(worktree));
4801 repo_path = strdup(cwd);
4802 if (repo_path == NULL) {
4803 error = got_error_from_errno("strdup");
4808 error = got_repo_open(&repo, repo_path, NULL);
4812 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4819 error = apply_unveil(got_repo_get_path(repo), NULL);
4823 error = tog_load_refs(repo);
4827 if (commit_id_str == NULL) {
4828 struct got_reference *head_ref;
4829 error = got_ref_open(&head_ref, repo, worktree ?
4830 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4833 error = got_ref_resolve(&commit_id, repo, head_ref);
4834 got_ref_close(head_ref);
4836 error = got_repo_match_object_id(&commit_id, NULL,
4837 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4842 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4844 error = got_error_from_errno("view_open");
4848 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4853 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4858 /* Release work tree lock. */
4859 got_worktree_close(worktree);
4862 error = view_loop(view);
4870 got_worktree_close(worktree);
4872 const struct got_error *close_err = got_repo_close(repo);
4880 static const struct got_error *
4881 draw_tree_entries(struct tog_view *view, const char *parent_path)
4883 struct tog_tree_view_state *s = &view->state.tree;
4884 const struct got_error *err = NULL;
4885 struct got_tree_entry *te;
4887 struct tog_color *tc;
4888 int width, n, i, nentries;
4889 int limit = view->nlines;
4893 werase(view->window);
4898 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4901 if (view_needs_focus_indication(view))
4902 wstandout(view->window);
4903 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4905 wattr_on(view->window,
4906 COLOR_PAIR(tc->colorpair), NULL);
4907 waddwstr(view->window, wline);
4909 wattr_off(view->window,
4910 COLOR_PAIR(tc->colorpair), NULL);
4911 if (view_needs_focus_indication(view))
4912 wstandend(view->window);
4915 if (width < view->ncols - 1)
4916 waddch(view->window, '\n');
4919 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4922 waddwstr(view->window, wline);
4925 if (width < view->ncols - 1)
4926 waddch(view->window, '\n');
4929 waddch(view->window, '\n');
4933 if (s->first_displayed_entry == NULL) {
4934 te = got_object_tree_get_first_entry(s->tree);
4935 if (s->selected == 0) {
4937 wstandout(view->window);
4938 s->selected_entry = NULL;
4940 waddstr(view->window, " ..\n"); /* parent directory */
4941 if (s->selected == 0 && view->focussed)
4942 wstandend(view->window);
4949 te = s->first_displayed_entry;
4952 nentries = got_object_tree_get_nentries(s->tree);
4953 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4954 char *line = NULL, *id_str = NULL, *link_target = NULL;
4955 const char *modestr = "";
4958 te = got_object_tree_get_entry(s->tree, i);
4959 mode = got_tree_entry_get_mode(te);
4962 err = got_object_id_str(&id_str,
4963 got_tree_entry_get_id(te));
4965 return got_error_from_errno(
4966 "got_object_id_str");
4968 if (got_object_tree_entry_is_submodule(te))
4970 else if (S_ISLNK(mode)) {
4973 err = got_tree_entry_get_symlink_target(&link_target,
4979 for (i = 0; i < strlen(link_target); i++) {
4980 if (!isprint((unsigned char)link_target[i]))
4981 link_target[i] = '?';
4985 else if (S_ISDIR(mode))
4987 else if (mode & S_IXUSR)
4989 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4990 got_tree_entry_get_name(te), modestr,
4991 link_target ? " -> ": "",
4992 link_target ? link_target : "") == -1) {
4995 return got_error_from_errno("asprintf");
4999 err = format_line(&wline, &width, line, view->ncols, 0);
5004 if (n == s->selected) {
5006 wstandout(view->window);
5007 s->selected_entry = te;
5009 tc = match_color(&s->colors, line);
5011 wattr_on(view->window,
5012 COLOR_PAIR(tc->colorpair), NULL);
5013 waddwstr(view->window, wline);
5015 wattr_off(view->window,
5016 COLOR_PAIR(tc->colorpair), NULL);
5017 if (width < view->ncols - 1)
5018 waddch(view->window, '\n');
5019 if (n == s->selected && view->focussed)
5020 wstandend(view->window);
5026 s->last_displayed_entry = te;
5035 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5037 struct got_tree_entry *te;
5038 int isroot = s->tree == s->root;
5041 if (s->first_displayed_entry == NULL)
5044 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5045 while (i++ < maxscroll) {
5048 s->first_displayed_entry = NULL;
5051 s->first_displayed_entry = te;
5052 te = got_tree_entry_get_prev(s->tree, te);
5057 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5059 struct got_tree_entry *next, *last;
5062 if (s->first_displayed_entry)
5063 next = got_tree_entry_get_next(s->tree,
5064 s->first_displayed_entry);
5066 next = got_object_tree_get_first_entry(s->tree);
5068 last = s->last_displayed_entry;
5069 while (next && last && n++ < maxscroll) {
5070 last = got_tree_entry_get_next(s->tree, last);
5072 s->first_displayed_entry = next;
5073 next = got_tree_entry_get_next(s->tree, next);
5078 static const struct got_error *
5079 tree_entry_path(char **path, struct tog_parent_trees *parents,
5080 struct got_tree_entry *te)
5082 const struct got_error *err = NULL;
5083 struct tog_parent_tree *pt;
5084 size_t len = 2; /* for leading slash and NUL */
5086 TAILQ_FOREACH(pt, parents, entry)
5087 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5090 len += strlen(got_tree_entry_get_name(te));
5092 *path = calloc(1, len);
5094 return got_error_from_errno("calloc");
5097 pt = TAILQ_LAST(parents, tog_parent_trees);
5099 const char *name = got_tree_entry_get_name(pt->selected_entry);
5100 if (strlcat(*path, name, len) >= len) {
5101 err = got_error(GOT_ERR_NO_SPACE);
5104 if (strlcat(*path, "/", len) >= len) {
5105 err = got_error(GOT_ERR_NO_SPACE);
5108 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5111 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5112 err = got_error(GOT_ERR_NO_SPACE);
5124 static const struct got_error *
5125 blame_tree_entry(struct tog_view **new_view, int begin_x,
5126 struct got_tree_entry *te, struct tog_parent_trees *parents,
5127 struct got_object_id *commit_id, struct got_repository *repo)
5129 const struct got_error *err = NULL;
5131 struct tog_view *blame_view;
5135 err = tree_entry_path(&path, parents, te);
5139 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5140 if (blame_view == NULL) {
5141 err = got_error_from_errno("view_open");
5145 err = open_blame_view(blame_view, path, commit_id, repo);
5147 if (err->code == GOT_ERR_CANCELLED)
5149 view_close(blame_view);
5151 *new_view = blame_view;
5157 static const struct got_error *
5158 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5159 struct tog_tree_view_state *s)
5161 struct tog_view *log_view;
5162 const struct got_error *err = NULL;
5167 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5168 if (log_view == NULL)
5169 return got_error_from_errno("view_open");
5171 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5175 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5178 view_close(log_view);
5180 *new_view = log_view;
5185 static const struct got_error *
5186 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5187 const char *head_ref_name, struct got_repository *repo)
5189 const struct got_error *err = NULL;
5190 char *commit_id_str = NULL;
5191 struct tog_tree_view_state *s = &view->state.tree;
5192 struct got_commit_object *commit = NULL;
5194 TAILQ_INIT(&s->parents);
5195 STAILQ_INIT(&s->colors);
5197 s->commit_id = got_object_id_dup(commit_id);
5198 if (s->commit_id == NULL)
5199 return got_error_from_errno("got_object_id_dup");
5201 err = got_object_open_as_commit(&commit, repo, commit_id);
5206 * The root is opened here and will be closed when the view is closed.
5207 * Any visited subtrees and their path-wise parents are opened and
5210 err = got_object_open_as_tree(&s->root, repo,
5211 got_object_commit_get_tree_id(commit));
5216 err = got_object_id_str(&commit_id_str, commit_id);
5220 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5221 err = got_error_from_errno("asprintf");
5225 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5226 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5227 if (head_ref_name) {
5228 s->head_ref_name = strdup(head_ref_name);
5229 if (s->head_ref_name == NULL) {
5230 err = got_error_from_errno("strdup");
5236 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5237 err = add_color(&s->colors, "\\$$",
5238 TOG_COLOR_TREE_SUBMODULE,
5239 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5242 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5243 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5246 err = add_color(&s->colors, "/$",
5247 TOG_COLOR_TREE_DIRECTORY,
5248 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5252 err = add_color(&s->colors, "\\*$",
5253 TOG_COLOR_TREE_EXECUTABLE,
5254 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5258 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5259 get_color_value("TOG_COLOR_COMMIT"));
5264 view->show = show_tree_view;
5265 view->input = input_tree_view;
5266 view->close = close_tree_view;
5267 view->search_start = search_start_tree_view;
5268 view->search_next = search_next_tree_view;
5270 free(commit_id_str);
5272 got_object_commit_close(commit);
5274 close_tree_view(view);
5278 static const struct got_error *
5279 close_tree_view(struct tog_view *view)
5281 struct tog_tree_view_state *s = &view->state.tree;
5283 free_colors(&s->colors);
5284 free(s->tree_label);
5285 s->tree_label = NULL;
5287 s->commit_id = NULL;
5288 free(s->head_ref_name);
5289 s->head_ref_name = NULL;
5290 while (!TAILQ_EMPTY(&s->parents)) {
5291 struct tog_parent_tree *parent;
5292 parent = TAILQ_FIRST(&s->parents);
5293 TAILQ_REMOVE(&s->parents, parent, entry);
5294 if (parent->tree != s->root)
5295 got_object_tree_close(parent->tree);
5299 if (s->tree != NULL && s->tree != s->root)
5300 got_object_tree_close(s->tree);
5302 got_object_tree_close(s->root);
5306 static const struct got_error *
5307 search_start_tree_view(struct tog_view *view)
5309 struct tog_tree_view_state *s = &view->state.tree;
5311 s->matched_entry = NULL;
5316 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5318 regmatch_t regmatch;
5320 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5324 static const struct got_error *
5325 search_next_tree_view(struct tog_view *view)
5327 struct tog_tree_view_state *s = &view->state.tree;
5328 struct got_tree_entry *te = NULL;
5330 if (!view->searching) {
5331 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5335 if (s->matched_entry) {
5336 if (view->searching == TOG_SEARCH_FORWARD) {
5337 if (s->selected_entry)
5338 te = got_tree_entry_get_next(s->tree,
5341 te = got_object_tree_get_first_entry(s->tree);
5343 if (s->selected_entry == NULL)
5344 te = got_object_tree_get_last_entry(s->tree);
5346 te = got_tree_entry_get_prev(s->tree,
5350 if (view->searching == TOG_SEARCH_FORWARD)
5351 te = got_object_tree_get_first_entry(s->tree);
5353 te = got_object_tree_get_last_entry(s->tree);
5358 if (s->matched_entry == NULL) {
5359 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5362 if (view->searching == TOG_SEARCH_FORWARD)
5363 te = got_object_tree_get_first_entry(s->tree);
5365 te = got_object_tree_get_last_entry(s->tree);
5368 if (match_tree_entry(te, &view->regex)) {
5369 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5370 s->matched_entry = te;
5374 if (view->searching == TOG_SEARCH_FORWARD)
5375 te = got_tree_entry_get_next(s->tree, te);
5377 te = got_tree_entry_get_prev(s->tree, te);
5380 if (s->matched_entry) {
5381 s->first_displayed_entry = s->matched_entry;
5388 static const struct got_error *
5389 show_tree_view(struct tog_view *view)
5391 const struct got_error *err = NULL;
5392 struct tog_tree_view_state *s = &view->state.tree;
5395 err = tree_entry_path(&parent_path, &s->parents, NULL);
5399 err = draw_tree_entries(view, parent_path);
5406 static const struct got_error *
5407 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5409 const struct got_error *err = NULL;
5410 struct tog_tree_view_state *s = &view->state.tree;
5411 struct tog_view *log_view, *ref_view;
5412 struct got_tree_entry *te;
5417 s->show_ids = !s->show_ids;
5420 if (!s->selected_entry)
5422 if (view_is_parent_view(view))
5423 begin_x = view_split_begin_x(view->begin_x);
5424 err = log_selected_tree_entry(&log_view, begin_x, s);
5426 log_view->focussed = 1;
5427 if (view_is_parent_view(view)) {
5428 err = view_close_child(view);
5431 view_set_child(view, log_view);
5432 view->focus_child = 1;
5434 *new_view = log_view;
5437 if (view_is_parent_view(view))
5438 begin_x = view_split_begin_x(view->begin_x);
5439 ref_view = view_open(view->nlines, view->ncols,
5440 view->begin_y, begin_x, TOG_VIEW_REF);
5441 if (ref_view == NULL)
5442 return got_error_from_errno("view_open");
5443 err = open_ref_view(ref_view, s->repo);
5445 view_close(ref_view);
5449 ref_view->focussed = 1;
5450 if (view_is_parent_view(view)) {
5451 err = view_close_child(view);
5454 view_set_child(view, ref_view);
5455 view->focus_child = 1;
5457 *new_view = ref_view;
5462 if (s->tree == s->root)
5463 s->first_displayed_entry =
5464 got_object_tree_get_first_entry(s->tree);
5466 s->first_displayed_entry = NULL;
5471 te = got_object_tree_get_last_entry(s->tree);
5472 for (n = 0; n < view->nlines - 3; n++) {
5474 if(s->tree != s->root) {
5475 s->first_displayed_entry = NULL;
5480 s->first_displayed_entry = te;
5481 te = got_tree_entry_get_prev(s->tree, te);
5484 s->selected = n - 1;
5488 if (s->selected > 0) {
5492 tree_scroll_up(s, 1);
5496 if (s->tree == s->root) {
5497 if (got_object_tree_get_first_entry(s->tree) ==
5498 s->first_displayed_entry)
5501 if (s->first_displayed_entry == NULL)
5504 tree_scroll_up(s, MAX(0, view->nlines - 3));
5508 if (s->selected < s->ndisplayed - 1) {
5512 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5514 /* can't scroll any further */
5516 tree_scroll_down(s, 1);
5520 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5522 /* can't scroll any further; move cursor down */
5523 if (s->selected < s->ndisplayed - 1)
5524 s->selected = s->ndisplayed - 1;
5527 tree_scroll_down(s, view->nlines - 3);
5532 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5533 struct tog_parent_tree *parent;
5534 /* user selected '..' */
5535 if (s->tree == s->root)
5537 parent = TAILQ_FIRST(&s->parents);
5538 TAILQ_REMOVE(&s->parents, parent,
5540 got_object_tree_close(s->tree);
5541 s->tree = parent->tree;
5542 s->first_displayed_entry =
5543 parent->first_displayed_entry;
5545 parent->selected_entry;
5546 s->selected = parent->selected;
5548 } else if (S_ISDIR(got_tree_entry_get_mode(
5549 s->selected_entry))) {
5550 struct got_tree_object *subtree;
5551 err = got_object_open_as_tree(&subtree, s->repo,
5552 got_tree_entry_get_id(s->selected_entry));
5555 err = tree_view_visit_subtree(s, subtree);
5557 got_object_tree_close(subtree);
5560 } else if (S_ISREG(got_tree_entry_get_mode(
5561 s->selected_entry))) {
5562 struct tog_view *blame_view;
5563 int begin_x = view_is_parent_view(view) ?
5564 view_split_begin_x(view->begin_x) : 0;
5566 err = blame_tree_entry(&blame_view, begin_x,
5567 s->selected_entry, &s->parents,
5568 s->commit_id, s->repo);
5572 blame_view->focussed = 1;
5573 if (view_is_parent_view(view)) {
5574 err = view_close_child(view);
5577 view_set_child(view, blame_view);
5578 view->focus_child = 1;
5580 *new_view = blame_view;
5584 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5585 s->selected = view->nlines - 4;
5598 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5603 static const struct got_error *
5604 cmd_tree(int argc, char *argv[])
5606 const struct got_error *error;
5607 struct got_repository *repo = NULL;
5608 struct got_worktree *worktree = NULL;
5609 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5610 struct got_object_id *commit_id = NULL;
5611 const char *commit_id_arg = NULL;
5613 struct got_reference *ref = NULL;
5614 const char *head_ref_name = NULL;
5616 struct tog_view *view;
5618 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5621 commit_id_arg = optarg;
5624 repo_path = realpath(optarg, NULL);
5625 if (repo_path == NULL)
5626 return got_error_from_errno2("realpath",
5641 if (repo_path == NULL) {
5642 cwd = getcwd(NULL, 0);
5644 return got_error_from_errno("getcwd");
5645 error = got_worktree_open(&worktree, cwd);
5646 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5650 strdup(got_worktree_get_repo_path(worktree));
5652 repo_path = strdup(cwd);
5653 if (repo_path == NULL) {
5654 error = got_error_from_errno("strdup");
5659 error = got_repo_open(&repo, repo_path, NULL);
5663 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5670 error = apply_unveil(got_repo_get_path(repo), NULL);
5674 error = tog_load_refs(repo);
5678 if (commit_id_arg == NULL) {
5679 error = got_repo_match_object_id(&commit_id, &label,
5680 worktree ? got_worktree_get_head_ref_name(worktree) :
5681 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5684 head_ref_name = label;
5686 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5688 head_ref_name = got_ref_get_name(ref);
5689 else if (error->code != GOT_ERR_NOT_REF)
5691 error = got_repo_match_object_id(&commit_id, NULL,
5692 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5697 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5699 error = got_error_from_errno("view_open");
5702 error = open_tree_view(view, commit_id, head_ref_name, repo);
5705 if (!got_path_is_root_dir(in_repo_path)) {
5706 error = tree_view_walk_path(&view->state.tree, commit_id,
5713 /* Release work tree lock. */
5714 got_worktree_close(worktree);
5717 error = view_loop(view);
5726 const struct got_error *close_err = got_repo_close(repo);
5734 static const struct got_error *
5735 ref_view_load_refs(struct tog_ref_view_state *s)
5737 struct got_reflist_entry *sre;
5738 struct tog_reflist_entry *re;
5741 TAILQ_FOREACH(sre, &tog_refs, entry) {
5742 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5745 re = malloc(sizeof(*re));
5747 return got_error_from_errno("malloc");
5749 re->ref = got_ref_dup(sre->ref);
5750 if (re->ref == NULL)
5751 return got_error_from_errno("got_ref_dup");
5752 re->idx = s->nrefs++;
5753 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5756 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5761 ref_view_free_refs(struct tog_ref_view_state *s)
5763 struct tog_reflist_entry *re;
5765 while (!TAILQ_EMPTY(&s->refs)) {
5766 re = TAILQ_FIRST(&s->refs);
5767 TAILQ_REMOVE(&s->refs, re, entry);
5768 got_ref_close(re->ref);
5773 static const struct got_error *
5774 open_ref_view(struct tog_view *view, struct got_repository *repo)
5776 const struct got_error *err = NULL;
5777 struct tog_ref_view_state *s = &view->state.ref;
5779 s->selected_entry = 0;
5782 TAILQ_INIT(&s->refs);
5783 STAILQ_INIT(&s->colors);
5785 err = ref_view_load_refs(s);
5789 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5790 err = add_color(&s->colors, "^refs/heads/",
5791 TOG_COLOR_REFS_HEADS,
5792 get_color_value("TOG_COLOR_REFS_HEADS"));
5796 err = add_color(&s->colors, "^refs/tags/",
5797 TOG_COLOR_REFS_TAGS,
5798 get_color_value("TOG_COLOR_REFS_TAGS"));
5802 err = add_color(&s->colors, "^refs/remotes/",
5803 TOG_COLOR_REFS_REMOTES,
5804 get_color_value("TOG_COLOR_REFS_REMOTES"));
5809 view->show = show_ref_view;
5810 view->input = input_ref_view;
5811 view->close = close_ref_view;
5812 view->search_start = search_start_ref_view;
5813 view->search_next = search_next_ref_view;
5816 free_colors(&s->colors);
5820 static const struct got_error *
5821 close_ref_view(struct tog_view *view)
5823 struct tog_ref_view_state *s = &view->state.ref;
5825 ref_view_free_refs(s);
5826 free_colors(&s->colors);
5831 static const struct got_error *
5832 resolve_reflist_entry(struct got_object_id **commit_id,
5833 struct tog_reflist_entry *re, struct got_repository *repo)
5835 const struct got_error *err = NULL;
5836 struct got_object_id *obj_id;
5837 struct got_tag_object *tag = NULL;
5842 err = got_ref_resolve(&obj_id, repo, re->ref);
5846 err = got_object_get_type(&obj_type, repo, obj_id);
5851 case GOT_OBJ_TYPE_COMMIT:
5852 *commit_id = obj_id;
5854 case GOT_OBJ_TYPE_TAG:
5855 err = got_object_open_as_tag(&tag, repo, obj_id);
5859 err = got_object_get_type(&obj_type, repo,
5860 got_object_tag_get_object_id(tag));
5863 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5864 err = got_error(GOT_ERR_OBJ_TYPE);
5867 *commit_id = got_object_id_dup(
5868 got_object_tag_get_object_id(tag));
5869 if (*commit_id == NULL) {
5870 err = got_error_from_errno("got_object_id_dup");
5875 err = got_error(GOT_ERR_OBJ_TYPE);
5881 got_object_tag_close(tag);
5889 static const struct got_error *
5890 log_ref_entry(struct tog_view **new_view, int begin_x,
5891 struct tog_reflist_entry *re, struct got_repository *repo)
5893 struct tog_view *log_view;
5894 const struct got_error *err = NULL;
5895 struct got_object_id *commit_id = NULL;
5899 err = resolve_reflist_entry(&commit_id, re, repo);
5901 if (err->code != GOT_ERR_OBJ_TYPE)
5907 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5908 if (log_view == NULL) {
5909 err = got_error_from_errno("view_open");
5913 err = open_log_view(log_view, commit_id, repo,
5914 got_ref_get_name(re->ref), "", 0);
5917 view_close(log_view);
5919 *new_view = log_view;
5925 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5927 struct tog_reflist_entry *re;
5930 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5933 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5934 while (i++ < maxscroll) {
5937 s->first_displayed_entry = re;
5938 re = TAILQ_PREV(re, tog_reflist_head, entry);
5943 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5945 struct tog_reflist_entry *next, *last;
5948 if (s->first_displayed_entry)
5949 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5951 next = TAILQ_FIRST(&s->refs);
5953 last = s->last_displayed_entry;
5954 while (next && last && n++ < maxscroll) {
5955 last = TAILQ_NEXT(last, entry);
5957 s->first_displayed_entry = next;
5958 next = TAILQ_NEXT(next, entry);
5963 static const struct got_error *
5964 search_start_ref_view(struct tog_view *view)
5966 struct tog_ref_view_state *s = &view->state.ref;
5968 s->matched_entry = NULL;
5973 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5975 regmatch_t regmatch;
5977 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5981 static const struct got_error *
5982 search_next_ref_view(struct tog_view *view)
5984 struct tog_ref_view_state *s = &view->state.ref;
5985 struct tog_reflist_entry *re = NULL;
5987 if (!view->searching) {
5988 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5992 if (s->matched_entry) {
5993 if (view->searching == TOG_SEARCH_FORWARD) {
5994 if (s->selected_entry)
5995 re = TAILQ_NEXT(s->selected_entry, entry);
5997 re = TAILQ_PREV(s->selected_entry,
5998 tog_reflist_head, entry);
6000 if (s->selected_entry == NULL)
6001 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6003 re = TAILQ_PREV(s->selected_entry,
6004 tog_reflist_head, entry);
6007 if (view->searching == TOG_SEARCH_FORWARD)
6008 re = TAILQ_FIRST(&s->refs);
6010 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6015 if (s->matched_entry == NULL) {
6016 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6019 if (view->searching == TOG_SEARCH_FORWARD)
6020 re = TAILQ_FIRST(&s->refs);
6022 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6025 if (match_reflist_entry(re, &view->regex)) {
6026 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6027 s->matched_entry = re;
6031 if (view->searching == TOG_SEARCH_FORWARD)
6032 re = TAILQ_NEXT(re, entry);
6034 re = TAILQ_PREV(re, tog_reflist_head, entry);
6037 if (s->matched_entry) {
6038 s->first_displayed_entry = s->matched_entry;
6045 static const struct got_error *
6046 show_ref_view(struct tog_view *view)
6048 const struct got_error *err = NULL;
6049 struct tog_ref_view_state *s = &view->state.ref;
6050 struct tog_reflist_entry *re;
6053 struct tog_color *tc;
6055 int limit = view->nlines;
6057 werase(view->window);
6064 re = s->first_displayed_entry;
6066 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6068 return got_error_from_errno("asprintf");
6070 err = format_line(&wline, &width, line, view->ncols, 0);
6075 if (view_needs_focus_indication(view))
6076 wstandout(view->window);
6077 waddwstr(view->window, wline);
6078 if (view_needs_focus_indication(view))
6079 wstandend(view->window);
6084 if (width < view->ncols - 1)
6085 waddch(view->window, '\n');
6090 while (re && limit > 0) {
6093 if (got_ref_is_symbolic(re->ref)) {
6094 if (asprintf(&line, "%s -> %s",
6095 got_ref_get_name(re->ref),
6096 got_ref_get_symref_target(re->ref)) == -1)
6097 return got_error_from_errno("asprintf");
6098 } else if (s->show_ids) {
6099 struct got_object_id *id;
6101 err = got_ref_resolve(&id, s->repo, re->ref);
6104 err = got_object_id_str(&id_str, id);
6109 if (asprintf(&line, "%s: %s",
6110 got_ref_get_name(re->ref), id_str) == -1) {
6111 err = got_error_from_errno("asprintf");
6119 line = strdup(got_ref_get_name(re->ref));
6121 return got_error_from_errno("strdup");
6124 err = format_line(&wline, &width, line, view->ncols, 0);
6129 if (n == s->selected) {
6131 wstandout(view->window);
6132 s->selected_entry = re;
6134 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6136 wattr_on(view->window,
6137 COLOR_PAIR(tc->colorpair), NULL);
6138 waddwstr(view->window, wline);
6140 wattr_off(view->window,
6141 COLOR_PAIR(tc->colorpair), NULL);
6142 if (width < view->ncols - 1)
6143 waddch(view->window, '\n');
6144 if (n == s->selected && view->focussed)
6145 wstandend(view->window);
6151 s->last_displayed_entry = re;
6154 re = TAILQ_NEXT(re, entry);
6161 static const struct got_error *
6162 browse_ref_tree(struct tog_view **new_view, int begin_x,
6163 struct tog_reflist_entry *re, struct got_repository *repo)
6165 const struct got_error *err = NULL;
6166 struct got_object_id *commit_id = NULL;
6167 struct tog_view *tree_view;
6171 err = resolve_reflist_entry(&commit_id, re, repo);
6173 if (err->code != GOT_ERR_OBJ_TYPE)
6180 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6181 if (tree_view == NULL) {
6182 err = got_error_from_errno("view_open");
6186 err = open_tree_view(tree_view, commit_id,
6187 got_ref_get_name(re->ref), repo);
6191 *new_view = tree_view;
6196 static const struct got_error *
6197 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6199 const struct got_error *err = NULL;
6200 struct tog_ref_view_state *s = &view->state.ref;
6201 struct tog_view *log_view, *tree_view;
6202 struct tog_reflist_entry *re;
6207 s->show_ids = !s->show_ids;
6211 if (!s->selected_entry)
6213 if (view_is_parent_view(view))
6214 begin_x = view_split_begin_x(view->begin_x);
6215 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6218 log_view->focussed = 1;
6219 if (view_is_parent_view(view)) {
6220 err = view_close_child(view);
6223 view_set_child(view, log_view);
6224 view->focus_child = 1;
6226 *new_view = log_view;
6229 if (!s->selected_entry)
6231 if (view_is_parent_view(view))
6232 begin_x = view_split_begin_x(view->begin_x);
6233 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6235 if (err || tree_view == NULL)
6238 tree_view->focussed = 1;
6239 if (view_is_parent_view(view)) {
6240 err = view_close_child(view);
6243 view_set_child(view, tree_view);
6244 view->focus_child = 1;
6246 *new_view = tree_view;
6251 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6256 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6257 for (n = 0; n < view->nlines - 1; n++) {
6260 s->first_displayed_entry = re;
6261 re = TAILQ_PREV(re, tog_reflist_head, entry);
6264 s->selected = n - 1;
6268 if (s->selected > 0) {
6272 ref_scroll_up(s, 1);
6276 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6278 ref_scroll_up(s, MAX(0, view->nlines - 1));
6282 if (s->selected < s->ndisplayed - 1) {
6286 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6287 /* can't scroll any further */
6289 ref_scroll_down(s, 1);
6293 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6294 /* can't scroll any further; move cursor down */
6295 if (s->selected < s->ndisplayed - 1)
6296 s->selected = s->ndisplayed - 1;
6299 ref_scroll_down(s, view->nlines - 1);
6303 err = tog_load_refs(s->repo);
6306 ref_view_free_refs(s);
6307 err = ref_view_load_refs(s);
6310 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6311 s->selected = view->nlines - 2;
6324 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6329 static const struct got_error *
6330 cmd_ref(int argc, char *argv[])
6332 const struct got_error *error;
6333 struct got_repository *repo = NULL;
6334 struct got_worktree *worktree = NULL;
6335 char *cwd = NULL, *repo_path = NULL;
6337 struct tog_view *view;
6339 while ((ch = getopt(argc, argv, "r:")) != -1) {
6342 repo_path = realpath(optarg, NULL);
6343 if (repo_path == NULL)
6344 return got_error_from_errno2("realpath",
6359 if (repo_path == NULL) {
6360 cwd = getcwd(NULL, 0);
6362 return got_error_from_errno("getcwd");
6363 error = got_worktree_open(&worktree, cwd);
6364 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6368 strdup(got_worktree_get_repo_path(worktree));
6370 repo_path = strdup(cwd);
6371 if (repo_path == NULL) {
6372 error = got_error_from_errno("strdup");
6377 error = got_repo_open(&repo, repo_path, NULL);
6383 error = apply_unveil(got_repo_get_path(repo), NULL);
6387 error = tog_load_refs(repo);
6391 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6393 error = got_error_from_errno("view_open");
6397 error = open_ref_view(view, repo);
6402 /* Release work tree lock. */
6403 got_worktree_close(worktree);
6406 error = view_loop(view);
6411 const struct got_error *close_err = got_repo_close(repo);
6420 list_commands(FILE *fp)
6424 fprintf(fp, "commands:");
6425 for (i = 0; i < nitems(tog_commands); i++) {
6426 struct tog_cmd *cmd = &tog_commands[i];
6427 fprintf(fp, " %s", cmd->name);
6433 usage(int hflag, int status)
6435 FILE *fp = (status == 0) ? stdout : stderr;
6437 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6440 fprintf(fp, "lazy usage: %s path\n", getprogname());
6447 make_argv(int argc, ...)
6455 argv = calloc(argc, sizeof(char *));
6458 for (i = 0; i < argc; i++) {
6459 argv[i] = strdup(va_arg(ap, char *));
6460 if (argv[i] == NULL)
6469 * Try to convert 'tog path' into a 'tog log path' command.
6470 * The user could simply have mistyped the command rather than knowingly
6471 * provided a path. So check whether argv[0] can in fact be resolved
6472 * to a path in the HEAD commit and print a special error if not.
6473 * This hack is for mpi@ <3
6475 static const struct got_error *
6476 tog_log_with_path(int argc, char *argv[])
6478 const struct got_error *error = NULL, *close_err;
6479 struct tog_cmd *cmd = NULL;
6480 struct got_repository *repo = NULL;
6481 struct got_worktree *worktree = NULL;
6482 struct got_object_id *commit_id = NULL, *id = NULL;
6483 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6484 char *commit_id_str = NULL, **cmd_argv = NULL;
6486 cwd = getcwd(NULL, 0);
6488 return got_error_from_errno("getcwd");
6490 error = got_worktree_open(&worktree, cwd);
6491 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6495 repo_path = strdup(got_worktree_get_repo_path(worktree));
6497 repo_path = strdup(cwd);
6498 if (repo_path == NULL) {
6499 error = got_error_from_errno("strdup");
6503 error = got_repo_open(&repo, repo_path, NULL);
6507 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6512 error = tog_load_refs(repo);
6515 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6516 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6517 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6522 got_worktree_close(worktree);
6526 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6528 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6530 fprintf(stderr, "%s: '%s' is no known command or path\n",
6531 getprogname(), argv[0]);
6536 close_err = got_repo_close(repo);
6541 error = got_object_id_str(&commit_id_str, commit_id);
6545 cmd = &tog_commands[0]; /* log */
6547 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6548 error = cmd->cmd_main(argc, cmd_argv);
6551 close_err = got_repo_close(repo);
6556 got_worktree_close(worktree);
6558 free(commit_id_str);
6565 for (i = 0; i < argc; i++)
6574 main(int argc, char *argv[])
6576 const struct got_error *error = NULL;
6577 struct tog_cmd *cmd = NULL;
6578 int ch, hflag = 0, Vflag = 0;
6579 char **cmd_argv = NULL;
6580 static struct option longopts[] = {
6581 { "version", no_argument, NULL, 'V' },
6585 setlocale(LC_CTYPE, "");
6587 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6607 got_version_print_str();
6612 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6620 /* Build an argument vector which runs a default command. */
6621 cmd = &tog_commands[0];
6623 cmd_argv = make_argv(argc, cmd->name);
6627 /* Did the user specify a command? */
6628 for (i = 0; i < nitems(tog_commands); i++) {
6629 if (strncmp(tog_commands[i].name, argv[0],
6630 strlen(argv[0])) == 0) {
6631 cmd = &tog_commands[i];
6640 /* No command specified; try log with a path */
6641 error = tog_log_with_path(argc, argv);
6646 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6653 for (i = 0; i < argc; i++)
6658 if (error && error->code != GOT_ERR_CANCELLED)
6659 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);