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.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_opentemp.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
57 #include "got_worktree.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 #define CTRL(x) ((x) & 0x1f)
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static const struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
108 #define TOG_EOF_STRING "(END)"
110 struct commit_queue_entry {
111 TAILQ_ENTRY(commit_queue_entry) entry;
112 struct got_object_id *id;
113 struct got_commit_object *commit;
116 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 struct commit_queue {
119 struct commit_queue_head head;
123 STAILQ_ENTRY(tog_color) entry;
127 STAILQ_HEAD(tog_colors, tog_color);
129 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
130 static struct got_reflist_object_id_map *tog_refs_idmap;
132 static const struct got_error *
133 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
134 struct got_reference* re2)
136 const char *name1 = got_ref_get_name(re1);
137 const char *name2 = got_ref_get_name(re2);
138 int isbackup1, isbackup2;
140 /* Sort backup refs towards the bottom of the list. */
141 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
142 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
143 if (!isbackup1 && isbackup2) {
146 } else if (isbackup1 && !isbackup2) {
151 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
155 static const struct got_error *
156 tog_load_refs(struct got_repository *repo, int sort_by_date)
158 const struct got_error *err;
160 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
161 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
166 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
173 if (tog_refs_idmap) {
174 got_reflist_object_id_map_free(tog_refs_idmap);
175 tog_refs_idmap = NULL;
177 got_ref_list_free(&tog_refs);
180 static const struct got_error *
181 add_color(struct tog_colors *colors, const char *pattern,
182 int idx, short color)
184 const struct got_error *err = NULL;
185 struct tog_color *tc;
188 if (idx < 1 || idx > COLOR_PAIRS - 1)
191 init_pair(idx, color, -1);
193 tc = calloc(1, sizeof(*tc));
195 return got_error_from_errno("calloc");
196 regerr = regcomp(&tc->regex, pattern,
197 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
199 static char regerr_msg[512];
200 static char err_msg[512];
201 regerror(regerr, &tc->regex, regerr_msg,
203 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
205 err = got_error_msg(GOT_ERR_REGEX, err_msg);
210 STAILQ_INSERT_HEAD(colors, tc, entry);
215 free_colors(struct tog_colors *colors)
217 struct tog_color *tc;
219 while (!STAILQ_EMPTY(colors)) {
220 tc = STAILQ_FIRST(colors);
221 STAILQ_REMOVE_HEAD(colors, entry);
228 get_color(struct tog_colors *colors, int colorpair)
230 struct tog_color *tc = NULL;
232 STAILQ_FOREACH(tc, colors, entry) {
233 if (tc->colorpair == colorpair)
241 default_color_value(const char *envvar)
243 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
244 return COLOR_MAGENTA;
245 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
247 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
249 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
251 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
252 return COLOR_MAGENTA;
253 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
254 return COLOR_MAGENTA;
255 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
257 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
259 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
261 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
263 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
265 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
267 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
268 return COLOR_MAGENTA;
269 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
271 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
278 get_color_value(const char *envvar)
280 const char *val = getenv(envvar);
283 return default_color_value(envvar);
285 if (strcasecmp(val, "black") == 0)
287 if (strcasecmp(val, "red") == 0)
289 if (strcasecmp(val, "green") == 0)
291 if (strcasecmp(val, "yellow") == 0)
293 if (strcasecmp(val, "blue") == 0)
295 if (strcasecmp(val, "magenta") == 0)
296 return COLOR_MAGENTA;
297 if (strcasecmp(val, "cyan") == 0)
299 if (strcasecmp(val, "white") == 0)
301 if (strcasecmp(val, "default") == 0)
304 return default_color_value(envvar);
308 struct tog_diff_view_state {
309 struct got_object_id *id1, *id2;
310 const char *label1, *label2;
312 int first_displayed_line;
313 int last_displayed_line;
316 int ignore_whitespace;
318 struct got_repository *repo;
319 struct tog_colors colors;
325 /* passed from log view; may be NULL */
326 struct tog_view *log_view;
329 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
331 struct tog_log_thread_args {
332 pthread_cond_t need_commits;
333 pthread_cond_t commit_loaded;
336 struct got_commit_graph *graph;
337 struct commit_queue *commits;
338 const char *in_repo_path;
339 struct got_object_id *start_id;
340 struct got_repository *repo;
343 struct commit_queue_entry **first_displayed_entry;
344 struct commit_queue_entry **selected_entry;
346 int *search_next_done;
350 struct tog_log_view_state {
351 struct commit_queue commits;
352 struct commit_queue_entry *first_displayed_entry;
353 struct commit_queue_entry *last_displayed_entry;
354 struct commit_queue_entry *selected_entry;
359 struct got_repository *repo;
360 struct got_object_id *start_id;
363 struct tog_log_thread_args thread_args;
364 struct commit_queue_entry *matched_entry;
365 struct commit_queue_entry *search_entry;
366 struct tog_colors colors;
369 #define TOG_COLOR_DIFF_MINUS 1
370 #define TOG_COLOR_DIFF_PLUS 2
371 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
372 #define TOG_COLOR_DIFF_META 4
373 #define TOG_COLOR_TREE_SUBMODULE 5
374 #define TOG_COLOR_TREE_SYMLINK 6
375 #define TOG_COLOR_TREE_DIRECTORY 7
376 #define TOG_COLOR_TREE_EXECUTABLE 8
377 #define TOG_COLOR_COMMIT 9
378 #define TOG_COLOR_AUTHOR 10
379 #define TOG_COLOR_DATE 11
380 #define TOG_COLOR_REFS_HEADS 12
381 #define TOG_COLOR_REFS_TAGS 13
382 #define TOG_COLOR_REFS_REMOTES 14
383 #define TOG_COLOR_REFS_BACKUP 15
385 struct tog_blame_cb_args {
386 struct tog_blame_line *lines; /* one per line */
389 struct tog_view *view;
390 struct got_object_id *commit_id;
394 struct tog_blame_thread_args {
396 struct got_repository *repo;
397 struct tog_blame_cb_args *cb_args;
399 got_cancel_cb cancel_cb;
406 struct tog_blame_line *lines;
410 struct tog_blame_thread_args thread_args;
411 struct tog_blame_cb_args cb_args;
415 struct tog_blame_view_state {
416 int first_displayed_line;
417 int last_displayed_line;
422 struct got_object_id_queue blamed_commits;
423 struct got_object_qid *blamed_commit;
425 struct got_repository *repo;
426 struct got_object_id *commit_id;
427 struct tog_blame blame;
429 struct tog_colors colors;
432 struct tog_parent_tree {
433 TAILQ_ENTRY(tog_parent_tree) entry;
434 struct got_tree_object *tree;
435 struct got_tree_entry *first_displayed_entry;
436 struct got_tree_entry *selected_entry;
440 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
442 struct tog_tree_view_state {
444 struct got_object_id *commit_id;/* commit which this tree belongs to */
445 struct got_tree_object *root; /* the commit's root tree entry */
446 struct got_tree_object *tree; /* currently displayed (sub-)tree */
447 struct got_tree_entry *first_displayed_entry;
448 struct got_tree_entry *last_displayed_entry;
449 struct got_tree_entry *selected_entry;
450 int ndisplayed, selected, show_ids;
451 struct tog_parent_trees parents; /* parent trees of current sub-tree */
453 struct got_repository *repo;
454 struct got_tree_entry *matched_entry;
455 struct tog_colors colors;
458 struct tog_reflist_entry {
459 TAILQ_ENTRY(tog_reflist_entry) entry;
460 struct got_reference *ref;
464 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
466 struct tog_ref_view_state {
467 struct tog_reflist_head refs;
468 struct tog_reflist_entry *first_displayed_entry;
469 struct tog_reflist_entry *last_displayed_entry;
470 struct tog_reflist_entry *selected_entry;
471 int nrefs, ndisplayed, selected, show_ids, sort_by_date;
472 struct got_repository *repo;
473 struct tog_reflist_entry *matched_entry;
474 struct tog_colors colors;
478 * We implement two types of views: parent views and child views.
480 * The 'Tab' key switches focus between a parent view and its child view.
481 * Child views are shown side-by-side to their parent view, provided
482 * there is enough screen estate.
484 * When a new view is opened from within a parent view, this new view
485 * becomes a child view of the parent view, replacing any existing child.
487 * When a new view is opened from within a child view, this new view
488 * becomes a parent view which will obscure the views below until the
489 * user quits the new parent view by typing 'q'.
491 * This list of views contains parent views only.
492 * Child views are only pointed to by their parent view.
494 TAILQ_HEAD(tog_view_list_head, tog_view);
497 TAILQ_ENTRY(tog_view) entry;
500 int nlines, ncols, begin_y, begin_x;
501 int lines, cols; /* copies of LINES and COLS */
502 int focussed; /* Only set on one parent or child view at a time. */
504 struct tog_view *parent;
505 struct tog_view *child;
508 * This flag is initially set on parent views when a new child view
509 * is created. It gets toggled when the 'Tab' key switches focus
510 * between parent and child.
511 * The flag indicates whether focus should be passed on to our child
512 * view if this parent view gets picked for focus after another parent
513 * view was closed. This prevents child views from losing focus in such
518 /* type-specific state */
519 enum tog_view_type type;
521 struct tog_diff_view_state diff;
522 struct tog_log_view_state log;
523 struct tog_blame_view_state blame;
524 struct tog_tree_view_state tree;
525 struct tog_ref_view_state ref;
528 const struct got_error *(*show)(struct tog_view *);
529 const struct got_error *(*input)(struct tog_view **,
530 struct tog_view *, int);
531 const struct got_error *(*close)(struct tog_view *);
533 const struct got_error *(*search_start)(struct tog_view *);
534 const struct got_error *(*search_next)(struct tog_view *);
537 #define TOG_SEARCH_FORWARD 1
538 #define TOG_SEARCH_BACKWARD 2
539 int search_next_done;
540 #define TOG_SEARCH_HAVE_MORE 1
541 #define TOG_SEARCH_NO_MORE 2
542 #define TOG_SEARCH_HAVE_NONE 3
547 static const struct got_error *open_diff_view(struct tog_view *,
548 struct got_object_id *, struct got_object_id *,
549 const char *, const char *, int, int, int, struct tog_view *,
550 struct got_repository *);
551 static const struct got_error *show_diff_view(struct tog_view *);
552 static const struct got_error *input_diff_view(struct tog_view **,
553 struct tog_view *, int);
554 static const struct got_error* close_diff_view(struct tog_view *);
555 static const struct got_error *search_start_diff_view(struct tog_view *);
556 static const struct got_error *search_next_diff_view(struct tog_view *);
558 static const struct got_error *open_log_view(struct tog_view *,
559 struct got_object_id *, struct got_repository *,
560 const char *, const char *, int);
561 static const struct got_error * show_log_view(struct tog_view *);
562 static const struct got_error *input_log_view(struct tog_view **,
563 struct tog_view *, int);
564 static const struct got_error *close_log_view(struct tog_view *);
565 static const struct got_error *search_start_log_view(struct tog_view *);
566 static const struct got_error *search_next_log_view(struct tog_view *);
568 static const struct got_error *open_blame_view(struct tog_view *, char *,
569 struct got_object_id *, struct got_repository *);
570 static const struct got_error *show_blame_view(struct tog_view *);
571 static const struct got_error *input_blame_view(struct tog_view **,
572 struct tog_view *, int);
573 static const struct got_error *close_blame_view(struct tog_view *);
574 static const struct got_error *search_start_blame_view(struct tog_view *);
575 static const struct got_error *search_next_blame_view(struct tog_view *);
577 static const struct got_error *open_tree_view(struct tog_view *,
578 struct got_object_id *, const char *, struct got_repository *);
579 static const struct got_error *show_tree_view(struct tog_view *);
580 static const struct got_error *input_tree_view(struct tog_view **,
581 struct tog_view *, int);
582 static const struct got_error *close_tree_view(struct tog_view *);
583 static const struct got_error *search_start_tree_view(struct tog_view *);
584 static const struct got_error *search_next_tree_view(struct tog_view *);
586 static const struct got_error *open_ref_view(struct tog_view *,
587 struct got_repository *);
588 static const struct got_error *show_ref_view(struct tog_view *);
589 static const struct got_error *input_ref_view(struct tog_view **,
590 struct tog_view *, int);
591 static const struct got_error *close_ref_view(struct tog_view *);
592 static const struct got_error *search_start_ref_view(struct tog_view *);
593 static const struct got_error *search_next_ref_view(struct tog_view *);
595 static volatile sig_atomic_t tog_sigwinch_received;
596 static volatile sig_atomic_t tog_sigpipe_received;
597 static volatile sig_atomic_t tog_sigcont_received;
600 tog_sigwinch(int signo)
602 tog_sigwinch_received = 1;
606 tog_sigpipe(int signo)
608 tog_sigpipe_received = 1;
612 tog_sigcont(int signo)
614 tog_sigcont_received = 1;
617 static const struct got_error *
618 view_close(struct tog_view *view)
620 const struct got_error *err = NULL;
623 view_close(view->child);
627 err = view->close(view);
629 del_panel(view->panel);
631 delwin(view->window);
636 static struct tog_view *
637 view_open(int nlines, int ncols, int begin_y, int begin_x,
638 enum tog_view_type type)
640 struct tog_view *view = calloc(1, sizeof(*view));
648 view->nlines = nlines ? nlines : LINES - begin_y;
649 view->ncols = ncols ? ncols : COLS - begin_x;
650 view->begin_y = begin_y;
651 view->begin_x = begin_x;
652 view->window = newwin(nlines, ncols, begin_y, begin_x);
653 if (view->window == NULL) {
657 view->panel = new_panel(view->window);
658 if (view->panel == NULL ||
659 set_panel_userptr(view->panel, view) != OK) {
664 keypad(view->window, TRUE);
669 view_split_begin_x(int begin_x)
671 if (begin_x > 0 || COLS < 120)
673 return (COLS - MAX(COLS / 2, 80));
676 static const struct got_error *view_resize(struct tog_view *);
678 static const struct got_error *
679 view_splitscreen(struct tog_view *view)
681 const struct got_error *err = NULL;
684 view->begin_x = view_split_begin_x(0);
685 view->nlines = LINES;
686 view->ncols = COLS - view->begin_x;
689 err = view_resize(view);
693 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
694 return got_error_from_errno("mvwin");
699 static const struct got_error *
700 view_fullscreen(struct tog_view *view)
702 const struct got_error *err = NULL;
706 view->nlines = LINES;
710 err = view_resize(view);
714 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
715 return got_error_from_errno("mvwin");
721 view_is_parent_view(struct tog_view *view)
723 return view->parent == NULL;
726 static const struct got_error *
727 view_resize(struct tog_view *view)
731 if (view->lines > LINES)
732 nlines = view->nlines - (view->lines - LINES);
734 nlines = view->nlines + (LINES - view->lines);
736 if (view->cols > COLS)
737 ncols = view->ncols - (view->cols - COLS);
739 ncols = view->ncols + (COLS - view->cols);
741 if (wresize(view->window, nlines, ncols) == ERR)
742 return got_error_from_errno("wresize");
743 if (replace_panel(view->panel, view->window) == ERR)
744 return got_error_from_errno("replace_panel");
745 wclear(view->window);
747 view->nlines = nlines;
753 view->child->begin_x = view_split_begin_x(view->begin_x);
754 if (view->child->begin_x == 0) {
755 view_fullscreen(view->child);
756 if (view->child->focussed)
757 show_panel(view->child->panel);
759 show_panel(view->panel);
761 view_splitscreen(view->child);
762 show_panel(view->child->panel);
769 static const struct got_error *
770 view_close_child(struct tog_view *view)
772 const struct got_error *err = NULL;
774 if (view->child == NULL)
777 err = view_close(view->child);
783 view_set_child(struct tog_view *view, struct tog_view *child)
786 child->parent = view;
790 view_is_splitscreen(struct tog_view *view)
792 return view->begin_x > 0;
801 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
802 cols = 80; /* Default */
808 resize_term(lines, cols);
811 static const struct got_error *
812 view_search_start(struct tog_view *view)
814 const struct got_error *err = NULL;
818 if (view->search_started) {
819 regfree(&view->regex);
821 memset(&view->regmatch, 0, sizeof(view->regmatch));
823 view->search_started = 0;
825 if (view->nlines < 1)
828 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
829 wclrtoeol(view->window);
833 ret = wgetnstr(view->window, pattern, sizeof(pattern));
839 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
840 err = view->search_start(view);
842 regfree(&view->regex);
845 view->search_started = 1;
846 view->searching = TOG_SEARCH_FORWARD;
847 view->search_next_done = 0;
848 view->search_next(view);
854 static const struct got_error *
855 view_input(struct tog_view **new, int *done, struct tog_view *view,
856 struct tog_view_list_head *views)
858 const struct got_error *err = NULL;
864 /* Clear "no matches" indicator. */
865 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
866 view->search_next_done == TOG_SEARCH_HAVE_NONE)
867 view->search_next_done = TOG_SEARCH_HAVE_MORE;
869 if (view->searching && !view->search_next_done) {
870 errcode = pthread_mutex_unlock(&tog_mutex);
872 return got_error_set_errno(errcode,
873 "pthread_mutex_unlock");
875 errcode = pthread_mutex_lock(&tog_mutex);
877 return got_error_set_errno(errcode,
878 "pthread_mutex_lock");
879 view->search_next(view);
883 nodelay(stdscr, FALSE);
884 /* Allow threads to make progress while we are waiting for input. */
885 errcode = pthread_mutex_unlock(&tog_mutex);
887 return got_error_set_errno(errcode, "pthread_mutex_unlock");
888 ch = wgetch(view->window);
889 errcode = pthread_mutex_lock(&tog_mutex);
891 return got_error_set_errno(errcode, "pthread_mutex_lock");
892 nodelay(stdscr, TRUE);
894 if (tog_sigwinch_received || tog_sigcont_received) {
896 tog_sigwinch_received = 0;
897 tog_sigcont_received = 0;
898 TAILQ_FOREACH(v, views, entry) {
899 err = view_resize(v);
902 err = v->input(new, v, KEY_RESIZE);
906 err = view_resize(v->child);
909 err = v->child->input(new, v->child,
921 view->child->focussed = 1;
922 view->focus_child = 1;
923 } else if (view->parent) {
925 view->parent->focussed = 1;
926 view->parent->focus_child = 0;
930 err = view->input(new, view, ch);
937 if (view_is_parent_view(view)) {
938 if (view->child == NULL)
940 if (view_is_splitscreen(view->child)) {
942 view->child->focussed = 1;
943 err = view_fullscreen(view->child);
945 err = view_splitscreen(view->child);
948 err = view->child->input(new, view->child,
951 if (view_is_splitscreen(view)) {
952 view->parent->focussed = 0;
954 err = view_fullscreen(view);
956 err = view_splitscreen(view);
960 err = view->input(new, view, KEY_RESIZE);
966 if (view->search_start)
967 view_search_start(view);
969 err = view->input(new, view, ch);
973 if (view->search_started && view->search_next) {
974 view->searching = (ch == 'n' ?
975 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
976 view->search_next_done = 0;
977 view->search_next(view);
979 err = view->input(new, view, ch);
982 err = view->input(new, view, ch);
990 view_vborder(struct tog_view *view)
993 const struct tog_view *view_above;
996 return view_vborder(view->parent);
998 panel = panel_above(view->panel);
1002 view_above = panel_userptr(panel);
1003 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1004 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1008 view_needs_focus_indication(struct tog_view *view)
1010 if (view_is_parent_view(view)) {
1011 if (view->child == NULL || view->child->focussed)
1013 if (!view_is_splitscreen(view->child))
1015 } else if (!view_is_splitscreen(view))
1018 return view->focussed;
1021 static const struct got_error *
1022 view_loop(struct tog_view *view)
1024 const struct got_error *err = NULL;
1025 struct tog_view_list_head views;
1026 struct tog_view *new_view;
1027 int fast_refresh = 10;
1028 int done = 0, errcode;
1030 errcode = pthread_mutex_lock(&tog_mutex);
1032 return got_error_set_errno(errcode, "pthread_mutex_lock");
1035 TAILQ_INSERT_HEAD(&views, view, entry);
1038 err = view->show(view);
1043 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1044 /* Refresh fast during initialization, then become slower. */
1045 if (fast_refresh && fast_refresh-- == 0)
1046 halfdelay(10); /* switch to once per second */
1048 err = view_input(&new_view, &done, view, &views);
1052 struct tog_view *v, *prev = NULL;
1054 if (view_is_parent_view(view))
1055 prev = TAILQ_PREV(view, tog_view_list_head,
1057 else if (view->parent)
1058 prev = view->parent;
1061 view->parent->child = NULL;
1062 view->parent->focus_child = 0;
1064 TAILQ_REMOVE(&views, view, entry);
1066 err = view_close(view);
1071 TAILQ_FOREACH(v, &views, entry) {
1075 if (view == NULL && new_view == NULL) {
1076 /* No view has focus. Try to pick one. */
1079 else if (!TAILQ_EMPTY(&views)) {
1080 view = TAILQ_LAST(&views,
1081 tog_view_list_head);
1084 if (view->focus_child) {
1085 view->child->focussed = 1;
1093 struct tog_view *v, *t;
1094 /* Only allow one parent view per type. */
1095 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1096 if (v->type != new_view->type)
1098 TAILQ_REMOVE(&views, v, entry);
1099 err = view_close(v);
1104 TAILQ_INSERT_TAIL(&views, new_view, entry);
1108 if (view_is_parent_view(view)) {
1109 if (view->child && view->child->focussed)
1112 if (view->parent && view->parent->focussed)
1113 view = view->parent;
1115 show_panel(view->panel);
1116 if (view->child && view_is_splitscreen(view->child))
1117 show_panel(view->child->panel);
1118 if (view->parent && view_is_splitscreen(view)) {
1119 err = view->parent->show(view->parent);
1123 err = view->show(view);
1127 err = view->child->show(view->child);
1136 while (!TAILQ_EMPTY(&views)) {
1137 view = TAILQ_FIRST(&views);
1138 TAILQ_REMOVE(&views, view, entry);
1142 errcode = pthread_mutex_unlock(&tog_mutex);
1143 if (errcode && err == NULL)
1144 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1154 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1159 /* Create newly allocated wide-character string equivalent to a byte string. */
1160 static const struct got_error *
1161 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1164 const struct got_error *err = NULL;
1167 *wlen = mbstowcs(NULL, s, 0);
1168 if (*wlen == (size_t)-1) {
1170 if (errno != EILSEQ)
1171 return got_error_from_errno("mbstowcs");
1173 /* byte string invalid in current encoding; try to "fix" it */
1174 err = got_mbsavis(&vis, &vislen, s);
1177 *wlen = mbstowcs(NULL, vis, 0);
1178 if (*wlen == (size_t)-1) {
1179 err = got_error_from_errno("mbstowcs"); /* give up */
1184 *ws = calloc(*wlen + 1, sizeof(**ws));
1186 err = got_error_from_errno("calloc");
1190 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1191 err = got_error_from_errno("mbstowcs");
1202 /* Format a line for display, ensuring that it won't overflow a width limit. */
1203 static const struct got_error *
1204 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1207 const struct got_error *err = NULL;
1209 wchar_t *wline = NULL;
1216 err = mbs2ws(&wline, &wlen, line);
1220 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1221 wline[wlen - 1] = L'\0';
1224 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1225 wline[wlen - 1] = L'\0';
1231 int width = wcwidth(wline[i]);
1238 if (width == 1 || width == 2) {
1239 if (cols + width > wlimit)
1243 } else if (width == -1) {
1244 if (wline[i] == L'\t') {
1246 ((cols + col_tab_align) % TABSIZE);
1251 if (cols + width > wlimit)
1256 err = got_error_from_errno("wcwidth");
1271 static const struct got_error*
1272 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1273 struct got_object_id *id, struct got_repository *repo)
1275 static const struct got_error *err = NULL;
1276 struct got_reflist_entry *re;
1282 TAILQ_FOREACH(re, refs, entry) {
1283 struct got_tag_object *tag = NULL;
1284 struct got_object_id *ref_id;
1287 name = got_ref_get_name(re->ref);
1288 if (strcmp(name, GOT_REF_HEAD) == 0)
1290 if (strncmp(name, "refs/", 5) == 0)
1292 if (strncmp(name, "got/", 4) == 0 &&
1293 strncmp(name, "got/backup/", 11) != 0)
1295 if (strncmp(name, "heads/", 6) == 0)
1297 if (strncmp(name, "remotes/", 8) == 0) {
1299 s = strstr(name, "/" GOT_REF_HEAD);
1300 if (s != NULL && s[strlen(s)] == '\0')
1303 err = got_ref_resolve(&ref_id, repo, re->ref);
1306 if (strncmp(name, "tags/", 5) == 0) {
1307 err = got_object_open_as_tag(&tag, repo, ref_id);
1309 if (err->code != GOT_ERR_OBJ_TYPE) {
1313 /* Ref points at something other than a tag. */
1318 cmp = got_object_id_cmp(tag ?
1319 got_object_tag_get_object_id(tag) : ref_id, id);
1322 got_object_tag_close(tag);
1326 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1327 s ? ", " : "", name) == -1) {
1328 err = got_error_from_errno("asprintf");
1339 static const struct got_error *
1340 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1345 smallerthan = strchr(author, '<');
1346 if (smallerthan && smallerthan[1] != '\0')
1347 author = smallerthan + 1;
1348 author[strcspn(author, "@>")] = '\0';
1349 return format_line(wauthor, author_width, author, limit, col_tab_align);
1352 static const struct got_error *
1353 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1354 struct got_object_id *id, const size_t date_display_cols,
1355 int author_display_cols)
1357 struct tog_log_view_state *s = &view->state.log;
1358 const struct got_error *err = NULL;
1359 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1360 char *logmsg0 = NULL, *logmsg = NULL;
1361 char *author = NULL;
1362 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1363 int author_width, logmsg_width;
1364 char *newline, *line = NULL;
1366 const int avail = view->ncols;
1368 time_t committer_time;
1369 struct tog_color *tc;
1371 committer_time = got_object_commit_get_committer_time(commit);
1372 if (gmtime_r(&committer_time, &tm) == NULL)
1373 return got_error_from_errno("gmtime_r");
1374 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1375 return got_error(GOT_ERR_NO_SPACE);
1377 if (avail <= date_display_cols)
1378 limit = MIN(sizeof(datebuf) - 1, avail);
1380 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1381 tc = get_color(&s->colors, TOG_COLOR_DATE);
1383 wattr_on(view->window,
1384 COLOR_PAIR(tc->colorpair), NULL);
1385 waddnstr(view->window, datebuf, limit);
1387 wattr_off(view->window,
1388 COLOR_PAIR(tc->colorpair), NULL);
1395 err = got_object_id_str(&id_str, id);
1398 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1400 wattr_on(view->window,
1401 COLOR_PAIR(tc->colorpair), NULL);
1402 wprintw(view->window, "%.8s ", id_str);
1404 wattr_off(view->window,
1405 COLOR_PAIR(tc->colorpair), NULL);
1412 author = strdup(got_object_commit_get_author(commit));
1413 if (author == NULL) {
1414 err = got_error_from_errno("strdup");
1417 err = format_author(&wauthor, &author_width, author, avail - col, col);
1420 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1422 wattr_on(view->window,
1423 COLOR_PAIR(tc->colorpair), NULL);
1424 waddwstr(view->window, wauthor);
1426 wattr_off(view->window,
1427 COLOR_PAIR(tc->colorpair), NULL);
1428 col += author_width;
1429 while (col < avail && author_width < author_display_cols + 2) {
1430 waddch(view->window, ' ');
1437 err = got_object_commit_get_logmsg(&logmsg0, commit);
1441 while (*logmsg == '\n')
1443 newline = strchr(logmsg, '\n');
1446 limit = avail - col;
1447 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1450 waddwstr(view->window, wlogmsg);
1451 col += logmsg_width;
1452 while (col < avail) {
1453 waddch(view->window, ' ');
1465 static struct commit_queue_entry *
1466 alloc_commit_queue_entry(struct got_commit_object *commit,
1467 struct got_object_id *id)
1469 struct commit_queue_entry *entry;
1471 entry = calloc(1, sizeof(*entry));
1476 entry->commit = commit;
1481 pop_commit(struct commit_queue *commits)
1483 struct commit_queue_entry *entry;
1485 entry = TAILQ_FIRST(&commits->head);
1486 TAILQ_REMOVE(&commits->head, entry, entry);
1487 got_object_commit_close(entry->commit);
1488 commits->ncommits--;
1489 /* Don't free entry->id! It is owned by the commit graph. */
1494 free_commits(struct commit_queue *commits)
1496 while (!TAILQ_EMPTY(&commits->head))
1497 pop_commit(commits);
1500 static const struct got_error *
1501 match_commit(int *have_match, struct got_object_id *id,
1502 struct got_commit_object *commit, regex_t *regex)
1504 const struct got_error *err = NULL;
1505 regmatch_t regmatch;
1506 char *id_str = NULL, *logmsg = NULL;
1510 err = got_object_id_str(&id_str, id);
1514 err = got_object_commit_get_logmsg(&logmsg, commit);
1518 if (regexec(regex, got_object_commit_get_author(commit), 1,
1519 ®match, 0) == 0 ||
1520 regexec(regex, got_object_commit_get_committer(commit), 1,
1521 ®match, 0) == 0 ||
1522 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1523 regexec(regex, logmsg, 1, ®match, 0) == 0)
1531 static const struct got_error *
1532 queue_commits(struct tog_log_thread_args *a)
1534 const struct got_error *err = NULL;
1537 * We keep all commits open throughout the lifetime of the log
1538 * view in order to avoid having to re-fetch commits from disk
1539 * while updating the display.
1542 struct got_object_id *id;
1543 struct got_commit_object *commit;
1544 struct commit_queue_entry *entry;
1547 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1549 if (err || id == NULL)
1552 err = got_object_open_as_commit(&commit, a->repo, id);
1555 entry = alloc_commit_queue_entry(commit, id);
1556 if (entry == NULL) {
1557 err = got_error_from_errno("alloc_commit_queue_entry");
1561 errcode = pthread_mutex_lock(&tog_mutex);
1563 err = got_error_set_errno(errcode,
1564 "pthread_mutex_lock");
1568 entry->idx = a->commits->ncommits;
1569 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1570 a->commits->ncommits++;
1572 if (*a->searching == TOG_SEARCH_FORWARD &&
1573 !*a->search_next_done) {
1575 err = match_commit(&have_match, id, commit, a->regex);
1579 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1582 errcode = pthread_mutex_unlock(&tog_mutex);
1583 if (errcode && err == NULL)
1584 err = got_error_set_errno(errcode,
1585 "pthread_mutex_unlock");
1588 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1594 select_commit(struct tog_log_view_state *s)
1596 struct commit_queue_entry *entry;
1599 entry = s->first_displayed_entry;
1601 if (ncommits == s->selected) {
1602 s->selected_entry = entry;
1605 entry = TAILQ_NEXT(entry, entry);
1610 static const struct got_error *
1611 draw_commits(struct tog_view *view)
1613 const struct got_error *err = NULL;
1614 struct tog_log_view_state *s = &view->state.log;
1615 struct commit_queue_entry *entry = s->selected_entry;
1616 const int limit = view->nlines;
1618 int ncommits, author_cols = 4;
1619 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1620 char *refs_str = NULL;
1622 struct tog_color *tc;
1623 static const size_t date_display_cols = 12;
1625 if (s->selected_entry &&
1626 !(view->searching && view->search_next_done == 0)) {
1627 struct got_reflist_head *refs;
1628 err = got_object_id_str(&id_str, s->selected_entry->id);
1631 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1632 s->selected_entry->id);
1634 err = build_refs_str(&refs_str, refs,
1635 s->selected_entry->id, s->repo);
1641 if (s->thread_args.commits_needed == 0)
1642 halfdelay(10); /* disable fast refresh */
1644 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1645 if (asprintf(&ncommits_str, " [%d/%d] %s",
1646 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1647 (view->searching && !view->search_next_done) ?
1648 "searching..." : "loading...") == -1) {
1649 err = got_error_from_errno("asprintf");
1653 const char *search_str = NULL;
1655 if (view->searching) {
1656 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1657 search_str = "no more matches";
1658 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1659 search_str = "no matches found";
1660 else if (!view->search_next_done)
1661 search_str = "searching...";
1664 if (asprintf(&ncommits_str, " [%d/%d] %s",
1665 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1666 search_str ? search_str :
1667 (refs_str ? refs_str : "")) == -1) {
1668 err = got_error_from_errno("asprintf");
1673 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1674 if (asprintf(&header, "commit %s %s%s",
1675 id_str ? id_str : "........................................",
1676 s->in_repo_path, ncommits_str) == -1) {
1677 err = got_error_from_errno("asprintf");
1681 } else if (asprintf(&header, "commit %s%s",
1682 id_str ? id_str : "........................................",
1683 ncommits_str) == -1) {
1684 err = got_error_from_errno("asprintf");
1688 err = format_line(&wline, &width, header, view->ncols, 0);
1692 werase(view->window);
1694 if (view_needs_focus_indication(view))
1695 wstandout(view->window);
1696 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1698 wattr_on(view->window,
1699 COLOR_PAIR(tc->colorpair), NULL);
1700 waddwstr(view->window, wline);
1702 wattr_off(view->window,
1703 COLOR_PAIR(tc->colorpair), NULL);
1704 while (width < view->ncols) {
1705 waddch(view->window, ' ');
1708 if (view_needs_focus_indication(view))
1709 wstandend(view->window);
1714 /* Grow author column size if necessary. */
1715 entry = s->first_displayed_entry;
1721 if (ncommits >= limit - 1)
1723 author = strdup(got_object_commit_get_author(entry->commit));
1724 if (author == NULL) {
1725 err = got_error_from_errno("strdup");
1728 err = format_author(&wauthor, &width, author, COLS,
1730 if (author_cols < width)
1731 author_cols = width;
1735 entry = TAILQ_NEXT(entry, entry);
1738 entry = s->first_displayed_entry;
1739 s->last_displayed_entry = s->first_displayed_entry;
1742 if (ncommits >= limit - 1)
1744 if (ncommits == s->selected)
1745 wstandout(view->window);
1746 err = draw_commit(view, entry->commit, entry->id,
1747 date_display_cols, author_cols);
1748 if (ncommits == s->selected)
1749 wstandend(view->window);
1753 s->last_displayed_entry = entry;
1754 entry = TAILQ_NEXT(entry, entry);
1767 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1769 struct commit_queue_entry *entry;
1772 entry = TAILQ_FIRST(&s->commits.head);
1773 if (s->first_displayed_entry == entry)
1776 entry = s->first_displayed_entry;
1777 while (entry && nscrolled < maxscroll) {
1778 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1780 s->first_displayed_entry = entry;
1786 static const struct got_error *
1787 trigger_log_thread(struct tog_view *view, int wait)
1789 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1792 halfdelay(1); /* fast refresh while loading commits */
1794 while (ta->commits_needed > 0 || ta->load_all) {
1795 if (ta->log_complete)
1798 /* Wake the log thread. */
1799 errcode = pthread_cond_signal(&ta->need_commits);
1801 return got_error_set_errno(errcode,
1802 "pthread_cond_signal");
1805 * The mutex will be released while the view loop waits
1806 * in wgetch(), at which time the log thread will run.
1811 /* Display progress update in log view. */
1812 show_log_view(view);
1816 /* Wait right here while next commit is being loaded. */
1817 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1819 return got_error_set_errno(errcode,
1820 "pthread_cond_wait");
1822 /* Display progress update in log view. */
1823 show_log_view(view);
1831 static const struct got_error *
1832 log_scroll_down(struct tog_view *view, int maxscroll)
1834 struct tog_log_view_state *s = &view->state.log;
1835 const struct got_error *err = NULL;
1836 struct commit_queue_entry *pentry;
1837 int nscrolled = 0, ncommits_needed;
1839 if (s->last_displayed_entry == NULL)
1842 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1843 if (s->commits.ncommits < ncommits_needed &&
1844 !s->thread_args.log_complete) {
1846 * Ask the log thread for required amount of commits.
1848 s->thread_args.commits_needed += maxscroll;
1849 err = trigger_log_thread(view, 1);
1855 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1859 s->last_displayed_entry = pentry;
1861 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1864 s->first_displayed_entry = pentry;
1865 } while (++nscrolled < maxscroll);
1870 static const struct got_error *
1871 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1872 struct got_commit_object *commit, struct got_object_id *commit_id,
1873 struct tog_view *log_view, struct got_repository *repo)
1875 const struct got_error *err;
1876 struct got_object_qid *parent_id;
1877 struct tog_view *diff_view;
1879 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1880 if (diff_view == NULL)
1881 return got_error_from_errno("view_open");
1883 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1884 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1885 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1887 *new_view = diff_view;
1891 static const struct got_error *
1892 tree_view_visit_subtree(struct tog_tree_view_state *s,
1893 struct got_tree_object *subtree)
1895 struct tog_parent_tree *parent;
1897 parent = calloc(1, sizeof(*parent));
1899 return got_error_from_errno("calloc");
1901 parent->tree = s->tree;
1902 parent->first_displayed_entry = s->first_displayed_entry;
1903 parent->selected_entry = s->selected_entry;
1904 parent->selected = s->selected;
1905 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1908 s->first_displayed_entry = NULL;
1912 static const struct got_error *
1913 tree_view_walk_path(struct tog_tree_view_state *s,
1914 struct got_commit_object *commit, const char *path)
1916 const struct got_error *err = NULL;
1917 struct got_tree_object *tree = NULL;
1919 char *slash, *subpath = NULL;
1921 /* Walk the path and open corresponding tree objects. */
1924 struct got_tree_entry *te;
1925 struct got_object_id *tree_id;
1931 /* Ensure the correct subtree entry is selected. */
1932 slash = strchr(p, '/');
1934 te_name = strdup(p);
1936 te_name = strndup(p, slash - p);
1937 if (te_name == NULL) {
1938 err = got_error_from_errno("strndup");
1941 te = got_object_tree_find_entry(s->tree, te_name);
1943 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1948 s->first_displayed_entry = s->selected_entry = te;
1950 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1951 break; /* jump to this file's entry */
1953 slash = strchr(p, '/');
1955 subpath = strndup(path, slash - path);
1957 subpath = strdup(path);
1958 if (subpath == NULL) {
1959 err = got_error_from_errno("strdup");
1963 err = got_object_id_by_path(&tree_id, s->repo, commit,
1968 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1973 err = tree_view_visit_subtree(s, tree);
1975 got_object_tree_close(tree);
1989 static const struct got_error *
1990 browse_commit_tree(struct tog_view **new_view, int begin_x,
1991 struct commit_queue_entry *entry, const char *path,
1992 const char *head_ref_name, struct got_repository *repo)
1994 const struct got_error *err = NULL;
1995 struct tog_tree_view_state *s;
1996 struct tog_view *tree_view;
1998 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1999 if (tree_view == NULL)
2000 return got_error_from_errno("view_open");
2002 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2005 s = &tree_view->state.tree;
2007 *new_view = tree_view;
2009 if (got_path_is_root_dir(path))
2012 return tree_view_walk_path(s, entry->commit, path);
2015 static const struct got_error *
2016 block_signals_used_by_main_thread(void)
2021 if (sigemptyset(&sigset) == -1)
2022 return got_error_from_errno("sigemptyset");
2024 /* tog handles SIGWINCH and SIGCONT */
2025 if (sigaddset(&sigset, SIGWINCH) == -1)
2026 return got_error_from_errno("sigaddset");
2027 if (sigaddset(&sigset, SIGCONT) == -1)
2028 return got_error_from_errno("sigaddset");
2030 /* ncurses handles SIGTSTP */
2031 if (sigaddset(&sigset, SIGTSTP) == -1)
2032 return got_error_from_errno("sigaddset");
2034 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2036 return got_error_set_errno(errcode, "pthread_sigmask");
2042 log_thread(void *arg)
2044 const struct got_error *err = NULL;
2046 struct tog_log_thread_args *a = arg;
2049 err = block_signals_used_by_main_thread();
2053 while (!done && !err && !tog_sigpipe_received) {
2054 err = queue_commits(a);
2056 if (err->code != GOT_ERR_ITER_COMPLETED)
2060 } else if (a->commits_needed > 0 && !a->load_all)
2061 a->commits_needed--;
2063 errcode = pthread_mutex_lock(&tog_mutex);
2065 err = got_error_set_errno(errcode,
2066 "pthread_mutex_lock");
2068 } else if (*a->quit)
2070 else if (*a->first_displayed_entry == NULL) {
2071 *a->first_displayed_entry =
2072 TAILQ_FIRST(&a->commits->head);
2073 *a->selected_entry = *a->first_displayed_entry;
2076 errcode = pthread_cond_signal(&a->commit_loaded);
2078 err = got_error_set_errno(errcode,
2079 "pthread_cond_signal");
2080 pthread_mutex_unlock(&tog_mutex);
2085 a->commits_needed = 0;
2087 if (a->commits_needed == 0 && !a->load_all) {
2088 errcode = pthread_cond_wait(&a->need_commits,
2091 err = got_error_set_errno(errcode,
2092 "pthread_cond_wait");
2098 errcode = pthread_mutex_unlock(&tog_mutex);
2099 if (errcode && err == NULL)
2100 err = got_error_set_errno(errcode,
2101 "pthread_mutex_unlock");
2103 a->log_complete = 1;
2107 static const struct got_error *
2108 stop_log_thread(struct tog_log_view_state *s)
2110 const struct got_error *err = NULL;
2115 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2117 return got_error_set_errno(errcode,
2118 "pthread_cond_signal");
2119 errcode = pthread_mutex_unlock(&tog_mutex);
2121 return got_error_set_errno(errcode,
2122 "pthread_mutex_unlock");
2123 errcode = pthread_join(s->thread, (void **)&err);
2125 return got_error_set_errno(errcode, "pthread_join");
2126 errcode = pthread_mutex_lock(&tog_mutex);
2128 return got_error_set_errno(errcode,
2129 "pthread_mutex_lock");
2133 if (s->thread_args.repo) {
2134 err = got_repo_close(s->thread_args.repo);
2135 s->thread_args.repo = NULL;
2138 if (s->thread_args.graph) {
2139 got_commit_graph_close(s->thread_args.graph);
2140 s->thread_args.graph = NULL;
2146 static const struct got_error *
2147 close_log_view(struct tog_view *view)
2149 const struct got_error *err = NULL;
2150 struct tog_log_view_state *s = &view->state.log;
2153 err = stop_log_thread(s);
2155 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2156 if (errcode && err == NULL)
2157 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2159 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2160 if (errcode && err == NULL)
2161 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2163 free_commits(&s->commits);
2164 free(s->in_repo_path);
2165 s->in_repo_path = NULL;
2168 free(s->head_ref_name);
2169 s->head_ref_name = NULL;
2173 static const struct got_error *
2174 search_start_log_view(struct tog_view *view)
2176 struct tog_log_view_state *s = &view->state.log;
2178 s->matched_entry = NULL;
2179 s->search_entry = NULL;
2183 static const struct got_error *
2184 search_next_log_view(struct tog_view *view)
2186 const struct got_error *err = NULL;
2187 struct tog_log_view_state *s = &view->state.log;
2188 struct commit_queue_entry *entry;
2190 /* Display progress update in log view. */
2191 show_log_view(view);
2195 if (s->search_entry) {
2197 errcode = pthread_mutex_unlock(&tog_mutex);
2199 return got_error_set_errno(errcode,
2200 "pthread_mutex_unlock");
2201 ch = wgetch(view->window);
2202 errcode = pthread_mutex_lock(&tog_mutex);
2204 return got_error_set_errno(errcode,
2205 "pthread_mutex_lock");
2206 if (ch == KEY_BACKSPACE) {
2207 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2210 if (view->searching == TOG_SEARCH_FORWARD)
2211 entry = TAILQ_NEXT(s->search_entry, entry);
2213 entry = TAILQ_PREV(s->search_entry,
2214 commit_queue_head, entry);
2215 } else if (s->matched_entry) {
2216 if (view->searching == TOG_SEARCH_FORWARD)
2217 entry = TAILQ_NEXT(s->matched_entry, entry);
2219 entry = TAILQ_PREV(s->matched_entry,
2220 commit_queue_head, entry);
2222 entry = s->selected_entry;
2228 if (entry == NULL) {
2229 if (s->thread_args.log_complete ||
2230 view->searching == TOG_SEARCH_BACKWARD) {
2231 view->search_next_done =
2232 (s->matched_entry == NULL ?
2233 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2234 s->search_entry = NULL;
2238 * Poke the log thread for more commits and return,
2239 * allowing the main loop to make progress. Search
2240 * will resume at s->search_entry once we come back.
2242 s->thread_args.commits_needed++;
2243 return trigger_log_thread(view, 0);
2246 err = match_commit(&have_match, entry->id, entry->commit,
2251 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2252 s->matched_entry = entry;
2256 s->search_entry = entry;
2257 if (view->searching == TOG_SEARCH_FORWARD)
2258 entry = TAILQ_NEXT(entry, entry);
2260 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2263 if (s->matched_entry) {
2264 int cur = s->selected_entry->idx;
2265 while (cur < s->matched_entry->idx) {
2266 err = input_log_view(NULL, view, KEY_DOWN);
2271 while (cur > s->matched_entry->idx) {
2272 err = input_log_view(NULL, view, KEY_UP);
2279 s->search_entry = NULL;
2284 static const struct got_error *
2285 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2286 struct got_repository *repo, const char *head_ref_name,
2287 const char *in_repo_path, int log_branches)
2289 const struct got_error *err = NULL;
2290 struct tog_log_view_state *s = &view->state.log;
2291 struct got_repository *thread_repo = NULL;
2292 struct got_commit_graph *thread_graph = NULL;
2295 if (in_repo_path != s->in_repo_path) {
2296 free(s->in_repo_path);
2297 s->in_repo_path = strdup(in_repo_path);
2298 if (s->in_repo_path == NULL)
2299 return got_error_from_errno("strdup");
2302 /* The commit queue only contains commits being displayed. */
2303 TAILQ_INIT(&s->commits.head);
2304 s->commits.ncommits = 0;
2307 if (head_ref_name) {
2308 s->head_ref_name = strdup(head_ref_name);
2309 if (s->head_ref_name == NULL) {
2310 err = got_error_from_errno("strdup");
2314 s->start_id = got_object_id_dup(start_id);
2315 if (s->start_id == NULL) {
2316 err = got_error_from_errno("got_object_id_dup");
2319 s->log_branches = log_branches;
2321 STAILQ_INIT(&s->colors);
2322 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2323 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2324 get_color_value("TOG_COLOR_COMMIT"));
2327 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2328 get_color_value("TOG_COLOR_AUTHOR"));
2330 free_colors(&s->colors);
2333 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2334 get_color_value("TOG_COLOR_DATE"));
2336 free_colors(&s->colors);
2341 view->show = show_log_view;
2342 view->input = input_log_view;
2343 view->close = close_log_view;
2344 view->search_start = search_start_log_view;
2345 view->search_next = search_next_log_view;
2347 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2350 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2354 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2355 s->repo, NULL, NULL);
2359 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2361 err = got_error_set_errno(errcode, "pthread_cond_init");
2364 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2366 err = got_error_set_errno(errcode, "pthread_cond_init");
2370 s->thread_args.commits_needed = view->nlines;
2371 s->thread_args.graph = thread_graph;
2372 s->thread_args.commits = &s->commits;
2373 s->thread_args.in_repo_path = s->in_repo_path;
2374 s->thread_args.start_id = s->start_id;
2375 s->thread_args.repo = thread_repo;
2376 s->thread_args.log_complete = 0;
2377 s->thread_args.quit = &s->quit;
2378 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2379 s->thread_args.selected_entry = &s->selected_entry;
2380 s->thread_args.searching = &view->searching;
2381 s->thread_args.search_next_done = &view->search_next_done;
2382 s->thread_args.regex = &view->regex;
2385 close_log_view(view);
2389 static const struct got_error *
2390 show_log_view(struct tog_view *view)
2392 const struct got_error *err;
2393 struct tog_log_view_state *s = &view->state.log;
2395 if (s->thread == NULL) {
2396 int errcode = pthread_create(&s->thread, NULL, log_thread,
2399 return got_error_set_errno(errcode, "pthread_create");
2400 if (s->thread_args.commits_needed > 0) {
2401 err = trigger_log_thread(view, 1);
2407 return draw_commits(view);
2410 static const struct got_error *
2411 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2413 const struct got_error *err = NULL;
2414 struct tog_log_view_state *s = &view->state.log;
2415 struct tog_view *diff_view = NULL, *tree_view = NULL;
2416 struct tog_view *ref_view = NULL;
2417 struct commit_queue_entry *entry;
2420 if (s->thread_args.load_all) {
2421 if (ch == KEY_BACKSPACE)
2422 s->thread_args.load_all = 0;
2423 else if (s->thread_args.log_complete) {
2424 s->thread_args.load_all = 0;
2425 log_scroll_down(view, s->commits.ncommits);
2426 s->selected = MIN(view->nlines - 2,
2427 s->commits.ncommits - 1);
2442 if (s->first_displayed_entry == NULL)
2444 if (s->selected > 0)
2447 log_scroll_up(s, 1);
2453 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2458 if (s->first_displayed_entry == NULL)
2460 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2463 log_scroll_up(s, view->nlines - 1);
2471 if (s->first_displayed_entry == NULL)
2473 if (s->selected < MIN(view->nlines - 2,
2474 s->commits.ncommits - 1))
2477 err = log_scroll_down(view, 1);
2485 /* We don't know yet how many commits, so we're forced to
2486 * traverse them all. */
2487 if (!s->thread_args.log_complete) {
2488 s->thread_args.load_all = 1;
2489 return trigger_log_thread(view, 0);
2493 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2494 for (n = 0; n < view->nlines - 1; n++) {
2497 s->first_displayed_entry = entry;
2498 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2501 s->selected = n - 1;
2507 struct commit_queue_entry *first;
2508 first = s->first_displayed_entry;
2511 err = log_scroll_down(view, view->nlines - 1);
2514 if (first == s->first_displayed_entry &&
2515 s->selected < MIN(view->nlines - 2,
2516 s->commits.ncommits - 1)) {
2517 /* can't scroll further down */
2518 s->selected = MIN(view->nlines - 2,
2519 s->commits.ncommits - 1);
2525 if (s->selected > view->nlines - 2)
2526 s->selected = view->nlines - 2;
2527 if (s->selected > s->commits.ncommits - 1)
2528 s->selected = s->commits.ncommits - 1;
2530 if (s->commits.ncommits < view->nlines - 1 &&
2531 !s->thread_args.log_complete) {
2532 s->thread_args.commits_needed += (view->nlines - 1) -
2533 s->commits.ncommits;
2534 err = trigger_log_thread(view, 1);
2540 if (s->selected_entry == NULL)
2542 if (view_is_parent_view(view))
2543 begin_x = view_split_begin_x(view->begin_x);
2544 err = open_diff_view_for_commit(&diff_view, begin_x,
2545 s->selected_entry->commit, s->selected_entry->id,
2550 diff_view->focussed = 1;
2551 if (view_is_parent_view(view)) {
2552 err = view_close_child(view);
2555 view_set_child(view, diff_view);
2556 view->focus_child = 1;
2558 *new_view = diff_view;
2561 if (s->selected_entry == NULL)
2563 if (view_is_parent_view(view))
2564 begin_x = view_split_begin_x(view->begin_x);
2565 err = browse_commit_tree(&tree_view, begin_x,
2566 s->selected_entry, s->in_repo_path, s->head_ref_name,
2571 tree_view->focussed = 1;
2572 if (view_is_parent_view(view)) {
2573 err = view_close_child(view);
2576 view_set_child(view, tree_view);
2577 view->focus_child = 1;
2579 *new_view = tree_view;
2584 if (ch == KEY_BACKSPACE &&
2585 got_path_is_root_dir(s->in_repo_path))
2587 err = stop_log_thread(s);
2590 if (ch == KEY_BACKSPACE) {
2592 err = got_path_dirname(&parent_path, s->in_repo_path);
2595 free(s->in_repo_path);
2596 s->in_repo_path = parent_path;
2597 s->thread_args.in_repo_path = s->in_repo_path;
2598 } else if (ch == CTRL('l')) {
2599 struct got_object_id *start_id;
2600 err = got_repo_match_object_id(&start_id, NULL,
2601 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2602 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2606 s->start_id = start_id;
2607 s->thread_args.start_id = s->start_id;
2609 s->log_branches = !s->log_branches;
2611 err = got_repo_open(&s->thread_args.repo,
2612 got_repo_get_path(s->repo), NULL);
2616 err = tog_load_refs(s->repo, 0);
2619 err = got_commit_graph_open(&s->thread_args.graph,
2620 s->in_repo_path, !s->log_branches);
2623 err = got_commit_graph_iter_start(s->thread_args.graph,
2624 s->start_id, s->repo, NULL, NULL);
2627 free_commits(&s->commits);
2628 s->first_displayed_entry = NULL;
2629 s->last_displayed_entry = NULL;
2630 s->selected_entry = NULL;
2632 s->thread_args.log_complete = 0;
2634 s->thread_args.commits_needed = view->nlines;
2637 if (view_is_parent_view(view))
2638 begin_x = view_split_begin_x(view->begin_x);
2639 ref_view = view_open(view->nlines, view->ncols,
2640 view->begin_y, begin_x, TOG_VIEW_REF);
2641 if (ref_view == NULL)
2642 return got_error_from_errno("view_open");
2643 err = open_ref_view(ref_view, s->repo);
2645 view_close(ref_view);
2649 ref_view->focussed = 1;
2650 if (view_is_parent_view(view)) {
2651 err = view_close_child(view);
2654 view_set_child(view, ref_view);
2655 view->focus_child = 1;
2657 *new_view = ref_view;
2666 static const struct got_error *
2667 apply_unveil(const char *repo_path, const char *worktree_path)
2669 const struct got_error *error;
2672 if (unveil("gmon.out", "rwc") != 0)
2673 return got_error_from_errno2("unveil", "gmon.out");
2675 if (repo_path && unveil(repo_path, "r") != 0)
2676 return got_error_from_errno2("unveil", repo_path);
2678 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2679 return got_error_from_errno2("unveil", worktree_path);
2681 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2682 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2684 error = got_privsep_unveil_exec_helpers();
2688 if (unveil(NULL, NULL) != 0)
2689 return got_error_from_errno("unveil");
2699 halfdelay(1); /* Do fast refresh while initial view is loading. */
2702 intrflush(stdscr, FALSE);
2703 keypad(stdscr, TRUE);
2705 if (getenv("TOG_COLORS") != NULL) {
2707 use_default_colors();
2709 signal(SIGWINCH, tog_sigwinch);
2710 signal(SIGPIPE, tog_sigpipe);
2711 signal(SIGCONT, tog_sigcont);
2714 static const struct got_error *
2715 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2716 struct got_repository *repo, struct got_worktree *worktree)
2718 const struct got_error *err = NULL;
2721 *in_repo_path = strdup("/");
2722 if (*in_repo_path == NULL)
2723 return got_error_from_errno("strdup");
2728 const char *prefix = got_worktree_get_path_prefix(worktree);
2731 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2734 if (asprintf(in_repo_path, "%s%s%s", prefix,
2735 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2737 err = got_error_from_errno("asprintf");
2738 *in_repo_path = NULL;
2742 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2747 static const struct got_error *
2748 cmd_log(int argc, char *argv[])
2750 const struct got_error *error;
2751 struct got_repository *repo = NULL;
2752 struct got_worktree *worktree = NULL;
2753 struct got_object_id *start_id = NULL;
2754 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2755 char *start_commit = NULL, *label = NULL;
2756 struct got_reference *ref = NULL;
2757 const char *head_ref_name = NULL;
2758 int ch, log_branches = 0;
2759 struct tog_view *view;
2761 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2767 start_commit = optarg;
2770 repo_path = realpath(optarg, NULL);
2771 if (repo_path == NULL)
2772 return got_error_from_errno2("realpath",
2787 if (repo_path == NULL) {
2788 cwd = getcwd(NULL, 0);
2790 return got_error_from_errno("getcwd");
2791 error = got_worktree_open(&worktree, cwd);
2792 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2796 strdup(got_worktree_get_repo_path(worktree));
2798 repo_path = strdup(cwd);
2799 if (repo_path == NULL) {
2800 error = got_error_from_errno("strdup");
2805 error = got_repo_open(&repo, repo_path, NULL);
2809 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2816 error = apply_unveil(got_repo_get_path(repo),
2817 worktree ? got_worktree_get_root_path(worktree) : NULL);
2821 /* already loaded by tog_log_with_path()? */
2822 if (TAILQ_EMPTY(&tog_refs)) {
2823 error = tog_load_refs(repo, 0);
2828 if (start_commit == NULL) {
2829 error = got_repo_match_object_id(&start_id, &label,
2830 worktree ? got_worktree_get_head_ref_name(worktree) :
2831 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2834 head_ref_name = label;
2836 error = got_ref_open(&ref, repo, start_commit, 0);
2838 head_ref_name = got_ref_get_name(ref);
2839 else if (error->code != GOT_ERR_NOT_REF)
2841 error = got_repo_match_object_id(&start_id, NULL,
2842 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2847 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2849 error = got_error_from_errno("view_open");
2852 error = open_log_view(view, start_id, repo, head_ref_name,
2853 in_repo_path, log_branches);
2857 /* Release work tree lock. */
2858 got_worktree_close(worktree);
2861 error = view_loop(view);
2871 const struct got_error *close_err = got_repo_close(repo);
2876 got_worktree_close(worktree);
2885 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2886 "[-w] object1 object2\n", getprogname());
2891 match_line(const char *line, regex_t *regex, size_t nmatch,
2892 regmatch_t *regmatch)
2894 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2898 match_color(struct tog_colors *colors, const char *line)
2900 struct tog_color *tc = NULL;
2902 STAILQ_FOREACH(tc, colors, entry) {
2903 if (match_line(line, &tc->regex, 0, NULL))
2910 static const struct got_error *
2911 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2912 WINDOW *window, regmatch_t *regmatch)
2914 const struct got_error *err = NULL;
2921 s = strndup(line, regmatch->rm_so);
2923 return got_error_from_errno("strndup");
2925 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2930 waddwstr(window, wline);
2937 s = strndup(line + regmatch->rm_so,
2938 regmatch->rm_eo - regmatch->rm_so);
2940 err = got_error_from_errno("strndup");
2944 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2949 wattr_on(window, A_STANDOUT, NULL);
2950 waddwstr(window, wline);
2951 wattr_off(window, A_STANDOUT, NULL);
2958 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2959 err = format_line(&wline, &width,
2960 line + regmatch->rm_eo, wlimit, col_tab_align);
2963 waddwstr(window, wline);
2971 static const struct got_error *
2972 draw_file(struct tog_view *view, const char *header)
2974 struct tog_diff_view_state *s = &view->state.diff;
2975 regmatch_t *regmatch = &view->regmatch;
2976 const struct got_error *err;
2979 size_t linesize = 0;
2981 struct tog_color *tc;
2984 int max_lines = view->nlines;
2985 int nlines = s->nlines;
2988 line_offset = s->line_offsets[s->first_displayed_line - 1];
2989 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2990 return got_error_from_errno("fseek");
2992 werase(view->window);
2995 if (asprintf(&line, "[%d/%d] %s",
2996 s->first_displayed_line - 1 + s->selected_line, nlines,
2998 return got_error_from_errno("asprintf");
2999 err = format_line(&wline, &width, line, view->ncols, 0);
3004 if (view_needs_focus_indication(view))
3005 wstandout(view->window);
3006 waddwstr(view->window, wline);
3009 if (view_needs_focus_indication(view))
3010 wstandend(view->window);
3011 if (width <= view->ncols - 1)
3012 waddch(view->window, '\n');
3021 while (max_lines > 0 && nprinted < max_lines) {
3022 linelen = getline(&line, &linesize, s->f);
3023 if (linelen == -1) {
3029 return got_ferror(s->f, GOT_ERR_IO);
3032 tc = match_color(&s->colors, line);
3034 wattr_on(view->window,
3035 COLOR_PAIR(tc->colorpair), NULL);
3036 if (s->first_displayed_line + nprinted == s->matched_line &&
3037 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3038 err = add_matched_line(&width, line, view->ncols, 0,
3039 view->window, regmatch);
3045 err = format_line(&wline, &width, line, view->ncols, 0);
3050 waddwstr(view->window, wline);
3055 wattr_off(view->window,
3056 COLOR_PAIR(tc->colorpair), NULL);
3057 if (width <= view->ncols - 1)
3058 waddch(view->window, '\n');
3063 s->last_displayed_line = s->first_displayed_line +
3066 s->last_displayed_line = s->first_displayed_line;
3071 while (nprinted < view->nlines) {
3072 waddch(view->window, '\n');
3076 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3081 wstandout(view->window);
3082 waddwstr(view->window, wline);
3085 wstandend(view->window);
3092 get_datestr(time_t *time, char *datebuf)
3094 struct tm mytm, *tm;
3097 tm = gmtime_r(time, &mytm);
3100 s = asctime_r(tm, datebuf);
3103 p = strchr(s, '\n');
3109 static const struct got_error *
3110 get_changed_paths(struct got_pathlist_head *paths,
3111 struct got_commit_object *commit, struct got_repository *repo)
3113 const struct got_error *err = NULL;
3114 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3115 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3116 struct got_object_qid *qid;
3118 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3120 struct got_commit_object *pcommit;
3121 err = got_object_open_as_commit(&pcommit, repo,
3126 tree_id1 = got_object_id_dup(
3127 got_object_commit_get_tree_id(pcommit));
3128 if (tree_id1 == NULL) {
3129 got_object_commit_close(pcommit);
3130 return got_error_from_errno("got_object_id_dup");
3132 got_object_commit_close(pcommit);
3137 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3142 tree_id2 = got_object_commit_get_tree_id(commit);
3143 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3147 err = got_diff_tree(tree1, tree2, "", "", repo,
3148 got_diff_tree_collect_changed_paths, paths, 0);
3151 got_object_tree_close(tree1);
3153 got_object_tree_close(tree2);
3158 static const struct got_error *
3159 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3163 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3165 return got_error_from_errno("reallocarray");
3167 (*line_offsets)[*nlines] = off;
3172 static const struct got_error *
3173 write_commit_info(off_t **line_offsets, size_t *nlines,
3174 struct got_object_id *commit_id, struct got_reflist_head *refs,
3175 struct got_repository *repo, FILE *outfile)
3177 const struct got_error *err = NULL;
3178 char datebuf[26], *datestr;
3179 struct got_commit_object *commit;
3180 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3181 time_t committer_time;
3182 const char *author, *committer;
3183 char *refs_str = NULL;
3184 struct got_pathlist_head changed_paths;
3185 struct got_pathlist_entry *pe;
3189 TAILQ_INIT(&changed_paths);
3192 err = build_refs_str(&refs_str, refs, commit_id, repo);
3197 err = got_object_open_as_commit(&commit, repo, commit_id);
3201 err = got_object_id_str(&id_str, commit_id);
3203 err = got_error_from_errno("got_object_id_str");
3207 err = add_line_offset(line_offsets, nlines, 0);
3211 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3212 refs_str ? refs_str : "", refs_str ? ")" : "");
3214 err = got_error_from_errno("fprintf");
3218 err = add_line_offset(line_offsets, nlines, outoff);
3222 n = fprintf(outfile, "from: %s\n",
3223 got_object_commit_get_author(commit));
3225 err = got_error_from_errno("fprintf");
3229 err = add_line_offset(line_offsets, nlines, outoff);
3233 committer_time = got_object_commit_get_committer_time(commit);
3234 datestr = get_datestr(&committer_time, datebuf);
3236 n = fprintf(outfile, "date: %s UTC\n", datestr);
3238 err = got_error_from_errno("fprintf");
3242 err = add_line_offset(line_offsets, nlines, outoff);
3246 author = got_object_commit_get_author(commit);
3247 committer = got_object_commit_get_committer(commit);
3248 if (strcmp(author, committer) != 0) {
3249 n = fprintf(outfile, "via: %s\n", committer);
3251 err = got_error_from_errno("fprintf");
3255 err = add_line_offset(line_offsets, nlines, outoff);
3259 if (got_object_commit_get_nparents(commit) > 1) {
3260 const struct got_object_id_queue *parent_ids;
3261 struct got_object_qid *qid;
3263 parent_ids = got_object_commit_get_parent_ids(commit);
3264 STAILQ_FOREACH(qid, parent_ids, entry) {
3265 err = got_object_id_str(&id_str, &qid->id);
3268 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3270 err = got_error_from_errno("fprintf");
3274 err = add_line_offset(line_offsets, nlines, outoff);
3282 err = got_object_commit_get_logmsg(&logmsg, commit);
3286 while ((line = strsep(&s, "\n")) != NULL) {
3287 n = fprintf(outfile, "%s\n", line);
3289 err = got_error_from_errno("fprintf");
3293 err = add_line_offset(line_offsets, nlines, outoff);
3298 err = get_changed_paths(&changed_paths, commit, repo);
3301 TAILQ_FOREACH(pe, &changed_paths, entry) {
3302 struct got_diff_changed_path *cp = pe->data;
3303 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3305 err = got_error_from_errno("fprintf");
3309 err = add_line_offset(line_offsets, nlines, outoff);
3312 free((char *)pe->path);
3316 fputc('\n', outfile);
3318 err = add_line_offset(line_offsets, nlines, outoff);
3320 got_pathlist_free(&changed_paths);
3324 got_object_commit_close(commit);
3326 free(*line_offsets);
3327 *line_offsets = NULL;
3333 static const struct got_error *
3334 create_diff(struct tog_diff_view_state *s)
3336 const struct got_error *err = NULL;
3340 free(s->line_offsets);
3341 s->line_offsets = malloc(sizeof(off_t));
3342 if (s->line_offsets == NULL)
3343 return got_error_from_errno("malloc");
3348 err = got_error_from_errno("got_opentemp");
3351 if (s->f && fclose(s->f) == EOF) {
3352 err = got_error_from_errno("fclose");
3358 err = got_object_get_type(&obj_type, s->repo, s->id1);
3360 err = got_object_get_type(&obj_type, s->repo, s->id2);
3365 case GOT_OBJ_TYPE_BLOB:
3366 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3367 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3368 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3370 case GOT_OBJ_TYPE_TREE:
3371 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3372 s->id1, s->id2, NULL, "", "", s->diff_context,
3373 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3375 case GOT_OBJ_TYPE_COMMIT: {
3376 const struct got_object_id_queue *parent_ids;
3377 struct got_object_qid *pid;
3378 struct got_commit_object *commit2;
3379 struct got_reflist_head *refs;
3381 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3384 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3385 /* Show commit info if we're diffing to a parent/root commit. */
3386 if (s->id1 == NULL) {
3387 err = write_commit_info(&s->line_offsets, &s->nlines,
3388 s->id2, refs, s->repo, s->f);
3392 parent_ids = got_object_commit_get_parent_ids(commit2);
3393 STAILQ_FOREACH(pid, parent_ids, entry) {
3394 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3395 err = write_commit_info(
3396 &s->line_offsets, &s->nlines,
3397 s->id2, refs, s->repo, s->f);
3404 got_object_commit_close(commit2);
3406 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3407 s->id1, s->id2, NULL, s->diff_context, s->ignore_whitespace,
3408 s->force_text_diff, s->repo, s->f);
3412 err = got_error(GOT_ERR_OBJ_TYPE);
3418 if (s->f && fflush(s->f) != 0 && err == NULL)
3419 err = got_error_from_errno("fflush");
3424 diff_view_indicate_progress(struct tog_view *view)
3426 mvwaddstr(view->window, 0, 0, "diffing...");
3431 static const struct got_error *
3432 search_start_diff_view(struct tog_view *view)
3434 struct tog_diff_view_state *s = &view->state.diff;
3436 s->matched_line = 0;
3440 static const struct got_error *
3441 search_next_diff_view(struct tog_view *view)
3443 struct tog_diff_view_state *s = &view->state.diff;
3446 size_t linesize = 0;
3449 if (!view->searching) {
3450 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3454 if (s->matched_line) {
3455 if (view->searching == TOG_SEARCH_FORWARD)
3456 lineno = s->matched_line + 1;
3458 lineno = s->matched_line - 1;
3460 lineno = s->first_displayed_line;
3465 if (lineno <= 0 || lineno > s->nlines) {
3466 if (s->matched_line == 0) {
3467 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3471 if (view->searching == TOG_SEARCH_FORWARD)
3477 offset = s->line_offsets[lineno - 1];
3478 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3480 return got_error_from_errno("fseeko");
3482 linelen = getline(&line, &linesize, s->f);
3483 if (linelen != -1 &&
3484 match_line(line, &view->regex, 1, &view->regmatch)) {
3485 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3486 s->matched_line = lineno;
3489 if (view->searching == TOG_SEARCH_FORWARD)
3496 if (s->matched_line) {
3497 s->first_displayed_line = s->matched_line;
3498 s->selected_line = 1;
3504 static const struct got_error *
3505 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3506 struct got_object_id *id2, const char *label1, const char *label2,
3507 int diff_context, int ignore_whitespace, int force_text_diff,
3508 struct tog_view *log_view, struct got_repository *repo)
3510 const struct got_error *err;
3511 struct tog_diff_view_state *s = &view->state.diff;
3513 if (id1 != NULL && id2 != NULL) {
3515 err = got_object_get_type(&type1, repo, id1);
3518 err = got_object_get_type(&type2, repo, id2);
3523 return got_error(GOT_ERR_OBJ_TYPE);
3525 s->first_displayed_line = 1;
3526 s->last_displayed_line = view->nlines;
3527 s->selected_line = 1;
3535 s->id1 = got_object_id_dup(id1);
3537 return got_error_from_errno("got_object_id_dup");
3541 s->id2 = got_object_id_dup(id2);
3542 if (s->id2 == NULL) {
3545 return got_error_from_errno("got_object_id_dup");
3548 s->first_displayed_line = 1;
3549 s->last_displayed_line = view->nlines;
3550 s->diff_context = diff_context;
3551 s->ignore_whitespace = ignore_whitespace;
3552 s->force_text_diff = force_text_diff;
3553 s->log_view = log_view;
3556 STAILQ_INIT(&s->colors);
3557 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3558 err = add_color(&s->colors,
3559 "^-", TOG_COLOR_DIFF_MINUS,
3560 get_color_value("TOG_COLOR_DIFF_MINUS"));
3563 err = add_color(&s->colors, "^\\+",
3564 TOG_COLOR_DIFF_PLUS,
3565 get_color_value("TOG_COLOR_DIFF_PLUS"));
3567 free_colors(&s->colors);
3570 err = add_color(&s->colors,
3571 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3572 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3574 free_colors(&s->colors);
3578 err = add_color(&s->colors,
3579 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3580 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3581 get_color_value("TOG_COLOR_DIFF_META"));
3583 free_colors(&s->colors);
3587 err = add_color(&s->colors,
3588 "^(from|via): ", TOG_COLOR_AUTHOR,
3589 get_color_value("TOG_COLOR_AUTHOR"));
3591 free_colors(&s->colors);
3595 err = add_color(&s->colors,
3596 "^date: ", TOG_COLOR_DATE,
3597 get_color_value("TOG_COLOR_DATE"));
3599 free_colors(&s->colors);
3604 if (log_view && view_is_splitscreen(view))
3605 show_log_view(log_view); /* draw vborder */
3606 diff_view_indicate_progress(view);
3608 s->line_offsets = NULL;
3610 err = create_diff(s);
3616 free_colors(&s->colors);
3620 view->show = show_diff_view;
3621 view->input = input_diff_view;
3622 view->close = close_diff_view;
3623 view->search_start = search_start_diff_view;
3624 view->search_next = search_next_diff_view;
3629 static const struct got_error *
3630 close_diff_view(struct tog_view *view)
3632 const struct got_error *err = NULL;
3633 struct tog_diff_view_state *s = &view->state.diff;
3639 if (s->f && fclose(s->f) == EOF)
3640 err = got_error_from_errno("fclose");
3641 free_colors(&s->colors);
3642 free(s->line_offsets);
3643 s->line_offsets = NULL;
3648 static const struct got_error *
3649 show_diff_view(struct tog_view *view)
3651 const struct got_error *err;
3652 struct tog_diff_view_state *s = &view->state.diff;
3653 char *id_str1 = NULL, *id_str2, *header;
3654 const char *label1, *label2;
3657 err = got_object_id_str(&id_str1, s->id1);
3660 label1 = s->label1 ? : id_str1;
3662 label1 = "/dev/null";
3664 err = got_object_id_str(&id_str2, s->id2);
3667 label2 = s->label2 ? : id_str2;
3669 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3670 err = got_error_from_errno("asprintf");
3678 err = draw_file(view, header);
3683 static const struct got_error *
3684 set_selected_commit(struct tog_diff_view_state *s,
3685 struct commit_queue_entry *entry)
3687 const struct got_error *err;
3688 const struct got_object_id_queue *parent_ids;
3689 struct got_commit_object *selected_commit;
3690 struct got_object_qid *pid;
3693 s->id2 = got_object_id_dup(entry->id);
3695 return got_error_from_errno("got_object_id_dup");
3697 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3700 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3702 pid = STAILQ_FIRST(parent_ids);
3703 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3704 got_object_commit_close(selected_commit);
3708 static const struct got_error *
3709 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3711 const struct got_error *err = NULL;
3712 struct tog_diff_view_state *s = &view->state.diff;
3713 struct tog_log_view_state *ls;
3714 struct commit_queue_entry *old_selected_entry;
3716 size_t linesize = 0;
3724 s->force_text_diff = !s->force_text_diff;
3726 s->ignore_whitespace = !s->ignore_whitespace;
3727 wclear(view->window);
3728 s->first_displayed_line = 1;
3729 s->last_displayed_line = view->nlines;
3730 s->matched_line = 0;
3731 diff_view_indicate_progress(view);
3732 err = create_diff(s);
3736 s->first_displayed_line = 1;
3743 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3749 if (s->first_displayed_line > 1)
3750 s->first_displayed_line--;
3754 if (s->first_displayed_line == 1)
3757 while (i++ < view->nlines - 1 &&
3758 s->first_displayed_line > 1)
3759 s->first_displayed_line--;
3765 s->first_displayed_line++;
3773 while (!s->eof && i++ < view->nlines - 1) {
3774 linelen = getline(&line, &linesize, s->f);
3775 s->first_displayed_line++;
3776 if (linelen == -1) {
3780 err = got_ferror(s->f, GOT_ERR_IO);
3787 if (s->diff_context > 0) {
3789 s->matched_line = 0;
3790 diff_view_indicate_progress(view);
3791 err = create_diff(s);
3792 if (s->first_displayed_line + view->nlines - 1 >
3794 s->first_displayed_line = 1;
3795 s->last_displayed_line = view->nlines;
3800 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3802 s->matched_line = 0;
3803 diff_view_indicate_progress(view);
3804 err = create_diff(s);
3809 if (s->log_view == NULL)
3811 ls = &s->log_view->state.log;
3812 old_selected_entry = ls->selected_entry;
3814 err = input_log_view(NULL, s->log_view, KEY_UP);
3818 if (old_selected_entry == ls->selected_entry)
3821 err = set_selected_commit(s, ls->selected_entry);
3825 s->first_displayed_line = 1;
3826 s->last_displayed_line = view->nlines;
3827 s->matched_line = 0;
3829 diff_view_indicate_progress(view);
3830 err = create_diff(s);
3834 if (s->log_view == NULL)
3836 ls = &s->log_view->state.log;
3837 old_selected_entry = ls->selected_entry;
3839 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3843 if (old_selected_entry == ls->selected_entry)
3846 err = set_selected_commit(s, ls->selected_entry);
3850 s->first_displayed_line = 1;
3851 s->last_displayed_line = view->nlines;
3852 s->matched_line = 0;
3854 diff_view_indicate_progress(view);
3855 err = create_diff(s);
3864 static const struct got_error *
3865 cmd_diff(int argc, char *argv[])
3867 const struct got_error *error = NULL;
3868 struct got_repository *repo = NULL;
3869 struct got_worktree *worktree = NULL;
3870 struct got_object_id *id1 = NULL, *id2 = NULL;
3871 char *repo_path = NULL, *cwd = NULL;
3872 char *id_str1 = NULL, *id_str2 = NULL;
3873 char *label1 = NULL, *label2 = NULL;
3874 int diff_context = 3, ignore_whitespace = 0;
3875 int ch, force_text_diff = 0;
3877 struct tog_view *view;
3879 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3882 force_text_diff = 1;
3885 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3888 errx(1, "number of context lines is %s: %s",
3892 repo_path = realpath(optarg, NULL);
3893 if (repo_path == NULL)
3894 return got_error_from_errno2("realpath",
3896 got_path_strip_trailing_slashes(repo_path);
3899 ignore_whitespace = 1;
3911 usage_diff(); /* TODO show local worktree changes */
3912 } else if (argc == 2) {
3918 if (repo_path == NULL) {
3919 cwd = getcwd(NULL, 0);
3921 return got_error_from_errno("getcwd");
3922 error = got_worktree_open(&worktree, cwd);
3923 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3927 strdup(got_worktree_get_repo_path(worktree));
3929 repo_path = strdup(cwd);
3930 if (repo_path == NULL) {
3931 error = got_error_from_errno("strdup");
3936 error = got_repo_open(&repo, repo_path, NULL);
3942 error = apply_unveil(got_repo_get_path(repo), NULL);
3946 error = tog_load_refs(repo, 0);
3950 error = got_repo_match_object_id(&id1, &label1, id_str1,
3951 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3955 error = got_repo_match_object_id(&id2, &label2, id_str2,
3956 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3960 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3962 error = got_error_from_errno("view_open");
3965 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3966 ignore_whitespace, force_text_diff, NULL, repo);
3969 error = view_loop(view);
3976 const struct got_error *close_err = got_repo_close(repo);
3981 got_worktree_close(worktree);
3990 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3995 struct tog_blame_line {
3997 struct got_object_id *id;
4000 static const struct got_error *
4001 draw_blame(struct tog_view *view)
4003 struct tog_blame_view_state *s = &view->state.blame;
4004 struct tog_blame *blame = &s->blame;
4005 regmatch_t *regmatch = &view->regmatch;
4006 const struct got_error *err;
4007 int lineno = 0, nprinted = 0;
4009 size_t linesize = 0;
4013 struct tog_blame_line *blame_line;
4014 struct got_object_id *prev_id = NULL;
4016 struct tog_color *tc;
4018 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4023 werase(view->window);
4025 if (asprintf(&line, "commit %s", id_str) == -1) {
4026 err = got_error_from_errno("asprintf");
4031 err = format_line(&wline, &width, line, view->ncols, 0);
4036 if (view_needs_focus_indication(view))
4037 wstandout(view->window);
4038 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4040 wattr_on(view->window,
4041 COLOR_PAIR(tc->colorpair), NULL);
4042 waddwstr(view->window, wline);
4044 wattr_off(view->window,
4045 COLOR_PAIR(tc->colorpair), NULL);
4046 if (view_needs_focus_indication(view))
4047 wstandend(view->window);
4050 if (width < view->ncols - 1)
4051 waddch(view->window, '\n');
4053 if (asprintf(&line, "[%d/%d] %s%s",
4054 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4055 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4057 return got_error_from_errno("asprintf");
4060 err = format_line(&wline, &width, line, view->ncols, 0);
4065 waddwstr(view->window, wline);
4068 if (width < view->ncols - 1)
4069 waddch(view->window, '\n');
4072 while (nprinted < view->nlines - 2) {
4073 linelen = getline(&line, &linesize, blame->f);
4074 if (linelen == -1) {
4075 if (feof(blame->f)) {
4080 return got_ferror(blame->f, GOT_ERR_IO);
4082 if (++lineno < s->first_displayed_line)
4085 if (view->focussed && nprinted == s->selected_line - 1)
4086 wstandout(view->window);
4088 if (blame->nlines > 0) {
4089 blame_line = &blame->lines[lineno - 1];
4090 if (blame_line->annotated && prev_id &&
4091 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4093 nprinted == s->selected_line - 1)) {
4094 waddstr(view->window, " ");
4095 } else if (blame_line->annotated) {
4097 err = got_object_id_str(&id_str, blame_line->id);
4102 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4104 wattr_on(view->window,
4105 COLOR_PAIR(tc->colorpair), NULL);
4106 wprintw(view->window, "%.8s", id_str);
4108 wattr_off(view->window,
4109 COLOR_PAIR(tc->colorpair), NULL);
4111 prev_id = blame_line->id;
4113 waddstr(view->window, "........");
4117 waddstr(view->window, "........");
4121 if (view->focussed && nprinted == s->selected_line - 1)
4122 wstandend(view->window);
4123 waddstr(view->window, " ");
4125 if (view->ncols <= 9) {
4127 wline = wcsdup(L"");
4128 if (wline == NULL) {
4129 err = got_error_from_errno("wcsdup");
4133 } else if (s->first_displayed_line + nprinted ==
4135 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4136 err = add_matched_line(&width, line, view->ncols - 9, 9,
4137 view->window, regmatch);
4144 err = format_line(&wline, &width, line,
4145 view->ncols - 9, 9);
4146 waddwstr(view->window, wline);
4152 if (width <= view->ncols - 1)
4153 waddch(view->window, '\n');
4154 if (++nprinted == 1)
4155 s->first_displayed_line = lineno;
4158 s->last_displayed_line = lineno;
4165 static const struct got_error *
4166 blame_cb(void *arg, int nlines, int lineno,
4167 struct got_commit_object *commit, struct got_object_id *id)
4169 const struct got_error *err = NULL;
4170 struct tog_blame_cb_args *a = arg;
4171 struct tog_blame_line *line;
4174 if (nlines != a->nlines ||
4175 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4176 return got_error(GOT_ERR_RANGE);
4178 errcode = pthread_mutex_lock(&tog_mutex);
4180 return got_error_set_errno(errcode, "pthread_mutex_lock");
4182 if (*a->quit) { /* user has quit the blame view */
4183 err = got_error(GOT_ERR_ITER_COMPLETED);
4188 goto done; /* no change in this commit */
4190 line = &a->lines[lineno - 1];
4191 if (line->annotated)
4194 line->id = got_object_id_dup(id);
4195 if (line->id == NULL) {
4196 err = got_error_from_errno("got_object_id_dup");
4199 line->annotated = 1;
4201 errcode = pthread_mutex_unlock(&tog_mutex);
4203 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4208 blame_thread(void *arg)
4210 const struct got_error *err, *close_err;
4211 struct tog_blame_thread_args *ta = arg;
4212 struct tog_blame_cb_args *a = ta->cb_args;
4215 err = block_signals_used_by_main_thread();
4219 err = got_blame(ta->path, a->commit_id, ta->repo,
4220 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4221 if (err && err->code == GOT_ERR_CANCELLED)
4224 errcode = pthread_mutex_lock(&tog_mutex);
4226 return (void *)got_error_set_errno(errcode,
4227 "pthread_mutex_lock");
4229 close_err = got_repo_close(ta->repo);
4235 errcode = pthread_mutex_unlock(&tog_mutex);
4236 if (errcode && err == NULL)
4237 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4242 static struct got_object_id *
4243 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4244 int first_displayed_line, int selected_line)
4246 struct tog_blame_line *line;
4251 line = &lines[first_displayed_line - 1 + selected_line - 1];
4252 if (!line->annotated)
4258 static const struct got_error *
4259 stop_blame(struct tog_blame *blame)
4261 const struct got_error *err = NULL;
4264 if (blame->thread) {
4266 errcode = pthread_mutex_unlock(&tog_mutex);
4268 return got_error_set_errno(errcode,
4269 "pthread_mutex_unlock");
4270 errcode = pthread_join(blame->thread, (void **)&err);
4272 return got_error_set_errno(errcode, "pthread_join");
4273 errcode = pthread_mutex_lock(&tog_mutex);
4275 return got_error_set_errno(errcode,
4276 "pthread_mutex_lock");
4277 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4279 blame->thread = NULL;
4281 if (blame->thread_args.repo) {
4282 const struct got_error *close_err;
4283 close_err = got_repo_close(blame->thread_args.repo);
4286 blame->thread_args.repo = NULL;
4289 if (fclose(blame->f) == EOF && err == NULL)
4290 err = got_error_from_errno("fclose");
4294 for (i = 0; i < blame->nlines; i++)
4295 free(blame->lines[i].id);
4297 blame->lines = NULL;
4299 free(blame->cb_args.commit_id);
4300 blame->cb_args.commit_id = NULL;
4305 static const struct got_error *
4306 cancel_blame_view(void *arg)
4308 const struct got_error *err = NULL;
4312 errcode = pthread_mutex_lock(&tog_mutex);
4314 return got_error_set_errno(errcode,
4315 "pthread_mutex_unlock");
4318 err = got_error(GOT_ERR_CANCELLED);
4320 errcode = pthread_mutex_unlock(&tog_mutex);
4322 return got_error_set_errno(errcode,
4323 "pthread_mutex_lock");
4328 static const struct got_error *
4329 run_blame(struct tog_view *view)
4331 struct tog_blame_view_state *s = &view->state.blame;
4332 struct tog_blame *blame = &s->blame;
4333 const struct got_error *err = NULL;
4334 struct got_commit_object *commit = NULL;
4335 struct got_blob_object *blob = NULL;
4336 struct got_repository *thread_repo = NULL;
4337 struct got_object_id *obj_id = NULL;
4340 err = got_object_open_as_commit(&commit, s->repo,
4341 &s->blamed_commit->id);
4345 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4349 err = got_object_get_type(&obj_type, s->repo, obj_id);
4353 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4354 err = got_error(GOT_ERR_OBJ_TYPE);
4358 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4361 blame->f = got_opentemp();
4362 if (blame->f == NULL) {
4363 err = got_error_from_errno("got_opentemp");
4366 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4367 &blame->line_offsets, blame->f, blob);
4370 if (blame->nlines == 0) {
4371 s->blame_complete = 1;
4375 /* Don't include \n at EOF in the blame line count. */
4376 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4379 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4380 if (blame->lines == NULL) {
4381 err = got_error_from_errno("calloc");
4385 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4389 blame->cb_args.view = view;
4390 blame->cb_args.lines = blame->lines;
4391 blame->cb_args.nlines = blame->nlines;
4392 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4393 if (blame->cb_args.commit_id == NULL) {
4394 err = got_error_from_errno("got_object_id_dup");
4397 blame->cb_args.quit = &s->done;
4399 blame->thread_args.path = s->path;
4400 blame->thread_args.repo = thread_repo;
4401 blame->thread_args.cb_args = &blame->cb_args;
4402 blame->thread_args.complete = &s->blame_complete;
4403 blame->thread_args.cancel_cb = cancel_blame_view;
4404 blame->thread_args.cancel_arg = &s->done;
4405 s->blame_complete = 0;
4407 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4408 s->first_displayed_line = 1;
4409 s->last_displayed_line = view->nlines;
4410 s->selected_line = 1;
4412 s->matched_line = 0;
4416 got_object_commit_close(commit);
4418 got_object_blob_close(blob);
4425 static const struct got_error *
4426 open_blame_view(struct tog_view *view, char *path,
4427 struct got_object_id *commit_id, struct got_repository *repo)
4429 const struct got_error *err = NULL;
4430 struct tog_blame_view_state *s = &view->state.blame;
4432 STAILQ_INIT(&s->blamed_commits);
4434 s->path = strdup(path);
4435 if (s->path == NULL)
4436 return got_error_from_errno("strdup");
4438 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4444 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4445 s->first_displayed_line = 1;
4446 s->last_displayed_line = view->nlines;
4447 s->selected_line = 1;
4448 s->blame_complete = 0;
4450 s->commit_id = commit_id;
4451 memset(&s->blame, 0, sizeof(s->blame));
4453 STAILQ_INIT(&s->colors);
4454 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4455 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4456 get_color_value("TOG_COLOR_COMMIT"));
4461 view->show = show_blame_view;
4462 view->input = input_blame_view;
4463 view->close = close_blame_view;
4464 view->search_start = search_start_blame_view;
4465 view->search_next = search_next_blame_view;
4467 return run_blame(view);
4470 static const struct got_error *
4471 close_blame_view(struct tog_view *view)
4473 const struct got_error *err = NULL;
4474 struct tog_blame_view_state *s = &view->state.blame;
4476 if (s->blame.thread)
4477 err = stop_blame(&s->blame);
4479 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4480 struct got_object_qid *blamed_commit;
4481 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4482 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4483 got_object_qid_free(blamed_commit);
4487 free_colors(&s->colors);
4492 static const struct got_error *
4493 search_start_blame_view(struct tog_view *view)
4495 struct tog_blame_view_state *s = &view->state.blame;
4497 s->matched_line = 0;
4501 static const struct got_error *
4502 search_next_blame_view(struct tog_view *view)
4504 struct tog_blame_view_state *s = &view->state.blame;
4507 size_t linesize = 0;
4510 if (!view->searching) {
4511 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4515 if (s->matched_line) {
4516 if (view->searching == TOG_SEARCH_FORWARD)
4517 lineno = s->matched_line + 1;
4519 lineno = s->matched_line - 1;
4521 lineno = s->first_displayed_line - 1 + s->selected_line;
4526 if (lineno <= 0 || lineno > s->blame.nlines) {
4527 if (s->matched_line == 0) {
4528 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4532 if (view->searching == TOG_SEARCH_FORWARD)
4535 lineno = s->blame.nlines;
4538 offset = s->blame.line_offsets[lineno - 1];
4539 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4541 return got_error_from_errno("fseeko");
4543 linelen = getline(&line, &linesize, s->blame.f);
4544 if (linelen != -1 &&
4545 match_line(line, &view->regex, 1, &view->regmatch)) {
4546 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4547 s->matched_line = lineno;
4550 if (view->searching == TOG_SEARCH_FORWARD)
4557 if (s->matched_line) {
4558 s->first_displayed_line = s->matched_line;
4559 s->selected_line = 1;
4565 static const struct got_error *
4566 show_blame_view(struct tog_view *view)
4568 const struct got_error *err = NULL;
4569 struct tog_blame_view_state *s = &view->state.blame;
4572 if (s->blame.thread == NULL && !s->blame_complete) {
4573 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4574 &s->blame.thread_args);
4576 return got_error_set_errno(errcode, "pthread_create");
4578 halfdelay(1); /* fast refresh while annotating */
4581 if (s->blame_complete)
4582 halfdelay(10); /* disable fast refresh */
4584 err = draw_blame(view);
4590 static const struct got_error *
4591 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4593 const struct got_error *err = NULL, *thread_err = NULL;
4594 struct tog_view *diff_view;
4595 struct tog_blame_view_state *s = &view->state.blame;
4604 s->selected_line = 1;
4605 s->first_displayed_line = 1;
4609 if (s->blame.nlines < view->nlines - 2) {
4610 s->selected_line = s->blame.nlines;
4611 s->first_displayed_line = 1;
4613 s->selected_line = view->nlines - 2;
4614 s->first_displayed_line = s->blame.nlines -
4621 if (s->selected_line > 1)
4623 else if (s->selected_line == 1 &&
4624 s->first_displayed_line > 1)
4625 s->first_displayed_line--;
4629 if (s->first_displayed_line == 1) {
4630 s->selected_line = 1;
4633 if (s->first_displayed_line > view->nlines - 2)
4634 s->first_displayed_line -=
4637 s->first_displayed_line = 1;
4642 if (s->selected_line < view->nlines - 2 &&
4643 s->first_displayed_line +
4644 s->selected_line <= s->blame.nlines)
4646 else if (s->last_displayed_line <
4648 s->first_displayed_line++;
4652 struct got_object_id *id = NULL;
4653 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4654 s->first_displayed_line, s->selected_line);
4658 struct got_commit_object *commit, *pcommit;
4659 struct got_object_qid *pid;
4660 struct got_object_id *blob_id = NULL;
4662 err = got_object_open_as_commit(&commit,
4667 got_object_commit_get_parent_ids(commit));
4669 got_object_commit_close(commit);
4672 /* Check if path history ends here. */
4673 err = got_object_open_as_commit(&pcommit,
4677 err = got_object_id_by_path(&blob_id, s->repo,
4679 got_object_commit_close(pcommit);
4681 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4683 got_object_commit_close(commit);
4686 err = got_object_get_type(&obj_type, s->repo,
4689 /* Can't blame non-blob type objects. */
4690 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4691 got_object_commit_close(commit);
4694 err = got_object_qid_alloc(&s->blamed_commit,
4696 got_object_commit_close(commit);
4698 if (got_object_id_cmp(id,
4699 &s->blamed_commit->id) == 0)
4701 err = got_object_qid_alloc(&s->blamed_commit,
4707 thread_err = stop_blame(&s->blame);
4711 STAILQ_INSERT_HEAD(&s->blamed_commits,
4712 s->blamed_commit, entry);
4713 err = run_blame(view);
4719 struct got_object_qid *first;
4720 first = STAILQ_FIRST(&s->blamed_commits);
4721 if (!got_object_id_cmp(&first->id, s->commit_id))
4724 thread_err = stop_blame(&s->blame);
4728 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4729 got_object_qid_free(s->blamed_commit);
4731 STAILQ_FIRST(&s->blamed_commits);
4732 err = run_blame(view);
4739 struct got_object_id *id = NULL;
4740 struct got_object_qid *pid;
4741 struct got_commit_object *commit = NULL;
4742 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4743 s->first_displayed_line, s->selected_line);
4746 err = got_object_open_as_commit(&commit, s->repo, id);
4750 got_object_commit_get_parent_ids(commit));
4751 if (view_is_parent_view(view))
4752 begin_x = view_split_begin_x(view->begin_x);
4753 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4754 if (diff_view == NULL) {
4755 got_object_commit_close(commit);
4756 err = got_error_from_errno("view_open");
4759 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4760 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4761 got_object_commit_close(commit);
4763 view_close(diff_view);
4767 diff_view->focussed = 1;
4768 if (view_is_parent_view(view)) {
4769 err = view_close_child(view);
4772 view_set_child(view, diff_view);
4773 view->focus_child = 1;
4775 *new_view = diff_view;
4783 if (s->last_displayed_line >= s->blame.nlines &&
4784 s->selected_line >= MIN(s->blame.nlines,
4785 view->nlines - 2)) {
4788 if (s->last_displayed_line >= s->blame.nlines &&
4789 s->selected_line < view->nlines - 2) {
4790 s->selected_line = MIN(s->blame.nlines,
4794 if (s->last_displayed_line + view->nlines - 2
4796 s->first_displayed_line +=
4799 s->first_displayed_line =
4804 if (s->selected_line > view->nlines - 2) {
4805 s->selected_line = MIN(s->blame.nlines,
4812 return thread_err ? thread_err : err;
4815 static const struct got_error *
4816 cmd_blame(int argc, char *argv[])
4818 const struct got_error *error;
4819 struct got_repository *repo = NULL;
4820 struct got_worktree *worktree = NULL;
4821 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4822 char *link_target = NULL;
4823 struct got_object_id *commit_id = NULL;
4824 struct got_commit_object *commit = NULL;
4825 char *commit_id_str = NULL;
4827 struct tog_view *view;
4829 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4832 commit_id_str = optarg;
4835 repo_path = realpath(optarg, NULL);
4836 if (repo_path == NULL)
4837 return got_error_from_errno2("realpath",
4852 if (repo_path == NULL) {
4853 cwd = getcwd(NULL, 0);
4855 return got_error_from_errno("getcwd");
4856 error = got_worktree_open(&worktree, cwd);
4857 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4861 strdup(got_worktree_get_repo_path(worktree));
4863 repo_path = strdup(cwd);
4864 if (repo_path == NULL) {
4865 error = got_error_from_errno("strdup");
4870 error = got_repo_open(&repo, repo_path, NULL);
4874 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4881 error = apply_unveil(got_repo_get_path(repo), NULL);
4885 error = tog_load_refs(repo, 0);
4889 if (commit_id_str == NULL) {
4890 struct got_reference *head_ref;
4891 error = got_ref_open(&head_ref, repo, worktree ?
4892 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4895 error = got_ref_resolve(&commit_id, repo, head_ref);
4896 got_ref_close(head_ref);
4898 error = got_repo_match_object_id(&commit_id, NULL,
4899 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4904 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4906 error = got_error_from_errno("view_open");
4910 error = got_object_open_as_commit(&commit, repo, commit_id);
4914 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4919 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4924 /* Release work tree lock. */
4925 got_worktree_close(worktree);
4928 error = view_loop(view);
4936 got_object_commit_close(commit);
4938 got_worktree_close(worktree);
4940 const struct got_error *close_err = got_repo_close(repo);
4948 static const struct got_error *
4949 draw_tree_entries(struct tog_view *view, const char *parent_path)
4951 struct tog_tree_view_state *s = &view->state.tree;
4952 const struct got_error *err = NULL;
4953 struct got_tree_entry *te;
4955 struct tog_color *tc;
4956 int width, n, i, nentries;
4957 int limit = view->nlines;
4961 werase(view->window);
4966 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4969 if (view_needs_focus_indication(view))
4970 wstandout(view->window);
4971 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4973 wattr_on(view->window,
4974 COLOR_PAIR(tc->colorpair), NULL);
4975 waddwstr(view->window, wline);
4977 wattr_off(view->window,
4978 COLOR_PAIR(tc->colorpair), NULL);
4979 if (view_needs_focus_indication(view))
4980 wstandend(view->window);
4983 if (width < view->ncols - 1)
4984 waddch(view->window, '\n');
4987 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4990 waddwstr(view->window, wline);
4993 if (width < view->ncols - 1)
4994 waddch(view->window, '\n');
4997 waddch(view->window, '\n');
5001 if (s->first_displayed_entry == NULL) {
5002 te = got_object_tree_get_first_entry(s->tree);
5003 if (s->selected == 0) {
5005 wstandout(view->window);
5006 s->selected_entry = NULL;
5008 waddstr(view->window, " ..\n"); /* parent directory */
5009 if (s->selected == 0 && view->focussed)
5010 wstandend(view->window);
5017 te = s->first_displayed_entry;
5020 nentries = got_object_tree_get_nentries(s->tree);
5021 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5022 char *line = NULL, *id_str = NULL, *link_target = NULL;
5023 const char *modestr = "";
5026 te = got_object_tree_get_entry(s->tree, i);
5027 mode = got_tree_entry_get_mode(te);
5030 err = got_object_id_str(&id_str,
5031 got_tree_entry_get_id(te));
5033 return got_error_from_errno(
5034 "got_object_id_str");
5036 if (got_object_tree_entry_is_submodule(te))
5038 else if (S_ISLNK(mode)) {
5041 err = got_tree_entry_get_symlink_target(&link_target,
5047 for (i = 0; i < strlen(link_target); i++) {
5048 if (!isprint((unsigned char)link_target[i]))
5049 link_target[i] = '?';
5053 else if (S_ISDIR(mode))
5055 else if (mode & S_IXUSR)
5057 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5058 got_tree_entry_get_name(te), modestr,
5059 link_target ? " -> ": "",
5060 link_target ? link_target : "") == -1) {
5063 return got_error_from_errno("asprintf");
5067 err = format_line(&wline, &width, line, view->ncols, 0);
5072 if (n == s->selected) {
5074 wstandout(view->window);
5075 s->selected_entry = te;
5077 tc = match_color(&s->colors, line);
5079 wattr_on(view->window,
5080 COLOR_PAIR(tc->colorpair), NULL);
5081 waddwstr(view->window, wline);
5083 wattr_off(view->window,
5084 COLOR_PAIR(tc->colorpair), NULL);
5085 if (width < view->ncols - 1)
5086 waddch(view->window, '\n');
5087 if (n == s->selected && view->focussed)
5088 wstandend(view->window);
5094 s->last_displayed_entry = te;
5103 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5105 struct got_tree_entry *te;
5106 int isroot = s->tree == s->root;
5109 if (s->first_displayed_entry == NULL)
5112 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5113 while (i++ < maxscroll) {
5116 s->first_displayed_entry = NULL;
5119 s->first_displayed_entry = te;
5120 te = got_tree_entry_get_prev(s->tree, te);
5125 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5127 struct got_tree_entry *next, *last;
5130 if (s->first_displayed_entry)
5131 next = got_tree_entry_get_next(s->tree,
5132 s->first_displayed_entry);
5134 next = got_object_tree_get_first_entry(s->tree);
5136 last = s->last_displayed_entry;
5137 while (next && last && n++ < maxscroll) {
5138 last = got_tree_entry_get_next(s->tree, last);
5140 s->first_displayed_entry = next;
5141 next = got_tree_entry_get_next(s->tree, next);
5146 static const struct got_error *
5147 tree_entry_path(char **path, struct tog_parent_trees *parents,
5148 struct got_tree_entry *te)
5150 const struct got_error *err = NULL;
5151 struct tog_parent_tree *pt;
5152 size_t len = 2; /* for leading slash and NUL */
5154 TAILQ_FOREACH(pt, parents, entry)
5155 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5158 len += strlen(got_tree_entry_get_name(te));
5160 *path = calloc(1, len);
5162 return got_error_from_errno("calloc");
5165 pt = TAILQ_LAST(parents, tog_parent_trees);
5167 const char *name = got_tree_entry_get_name(pt->selected_entry);
5168 if (strlcat(*path, name, len) >= len) {
5169 err = got_error(GOT_ERR_NO_SPACE);
5172 if (strlcat(*path, "/", len) >= len) {
5173 err = got_error(GOT_ERR_NO_SPACE);
5176 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5179 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5180 err = got_error(GOT_ERR_NO_SPACE);
5192 static const struct got_error *
5193 blame_tree_entry(struct tog_view **new_view, int begin_x,
5194 struct got_tree_entry *te, struct tog_parent_trees *parents,
5195 struct got_object_id *commit_id, struct got_repository *repo)
5197 const struct got_error *err = NULL;
5199 struct tog_view *blame_view;
5203 err = tree_entry_path(&path, parents, te);
5207 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5208 if (blame_view == NULL) {
5209 err = got_error_from_errno("view_open");
5213 err = open_blame_view(blame_view, path, commit_id, repo);
5215 if (err->code == GOT_ERR_CANCELLED)
5217 view_close(blame_view);
5219 *new_view = blame_view;
5225 static const struct got_error *
5226 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5227 struct tog_tree_view_state *s)
5229 struct tog_view *log_view;
5230 const struct got_error *err = NULL;
5235 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5236 if (log_view == NULL)
5237 return got_error_from_errno("view_open");
5239 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5243 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5246 view_close(log_view);
5248 *new_view = log_view;
5253 static const struct got_error *
5254 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5255 const char *head_ref_name, struct got_repository *repo)
5257 const struct got_error *err = NULL;
5258 char *commit_id_str = NULL;
5259 struct tog_tree_view_state *s = &view->state.tree;
5260 struct got_commit_object *commit = NULL;
5262 TAILQ_INIT(&s->parents);
5263 STAILQ_INIT(&s->colors);
5265 s->commit_id = got_object_id_dup(commit_id);
5266 if (s->commit_id == NULL)
5267 return got_error_from_errno("got_object_id_dup");
5269 err = got_object_open_as_commit(&commit, repo, commit_id);
5274 * The root is opened here and will be closed when the view is closed.
5275 * Any visited subtrees and their path-wise parents are opened and
5278 err = got_object_open_as_tree(&s->root, repo,
5279 got_object_commit_get_tree_id(commit));
5284 err = got_object_id_str(&commit_id_str, commit_id);
5288 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5289 err = got_error_from_errno("asprintf");
5293 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5294 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5295 if (head_ref_name) {
5296 s->head_ref_name = strdup(head_ref_name);
5297 if (s->head_ref_name == NULL) {
5298 err = got_error_from_errno("strdup");
5304 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5305 err = add_color(&s->colors, "\\$$",
5306 TOG_COLOR_TREE_SUBMODULE,
5307 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5310 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5311 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5314 err = add_color(&s->colors, "/$",
5315 TOG_COLOR_TREE_DIRECTORY,
5316 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5320 err = add_color(&s->colors, "\\*$",
5321 TOG_COLOR_TREE_EXECUTABLE,
5322 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5326 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5327 get_color_value("TOG_COLOR_COMMIT"));
5332 view->show = show_tree_view;
5333 view->input = input_tree_view;
5334 view->close = close_tree_view;
5335 view->search_start = search_start_tree_view;
5336 view->search_next = search_next_tree_view;
5338 free(commit_id_str);
5340 got_object_commit_close(commit);
5342 close_tree_view(view);
5346 static const struct got_error *
5347 close_tree_view(struct tog_view *view)
5349 struct tog_tree_view_state *s = &view->state.tree;
5351 free_colors(&s->colors);
5352 free(s->tree_label);
5353 s->tree_label = NULL;
5355 s->commit_id = NULL;
5356 free(s->head_ref_name);
5357 s->head_ref_name = NULL;
5358 while (!TAILQ_EMPTY(&s->parents)) {
5359 struct tog_parent_tree *parent;
5360 parent = TAILQ_FIRST(&s->parents);
5361 TAILQ_REMOVE(&s->parents, parent, entry);
5362 if (parent->tree != s->root)
5363 got_object_tree_close(parent->tree);
5367 if (s->tree != NULL && s->tree != s->root)
5368 got_object_tree_close(s->tree);
5370 got_object_tree_close(s->root);
5374 static const struct got_error *
5375 search_start_tree_view(struct tog_view *view)
5377 struct tog_tree_view_state *s = &view->state.tree;
5379 s->matched_entry = NULL;
5384 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5386 regmatch_t regmatch;
5388 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5392 static const struct got_error *
5393 search_next_tree_view(struct tog_view *view)
5395 struct tog_tree_view_state *s = &view->state.tree;
5396 struct got_tree_entry *te = NULL;
5398 if (!view->searching) {
5399 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5403 if (s->matched_entry) {
5404 if (view->searching == TOG_SEARCH_FORWARD) {
5405 if (s->selected_entry)
5406 te = got_tree_entry_get_next(s->tree,
5409 te = got_object_tree_get_first_entry(s->tree);
5411 if (s->selected_entry == NULL)
5412 te = got_object_tree_get_last_entry(s->tree);
5414 te = got_tree_entry_get_prev(s->tree,
5418 if (s->selected_entry)
5419 te = s->selected_entry;
5420 else if (view->searching == TOG_SEARCH_FORWARD)
5421 te = got_object_tree_get_first_entry(s->tree);
5423 te = got_object_tree_get_last_entry(s->tree);
5428 if (s->matched_entry == NULL) {
5429 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5432 if (view->searching == TOG_SEARCH_FORWARD)
5433 te = got_object_tree_get_first_entry(s->tree);
5435 te = got_object_tree_get_last_entry(s->tree);
5438 if (match_tree_entry(te, &view->regex)) {
5439 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5440 s->matched_entry = te;
5444 if (view->searching == TOG_SEARCH_FORWARD)
5445 te = got_tree_entry_get_next(s->tree, te);
5447 te = got_tree_entry_get_prev(s->tree, te);
5450 if (s->matched_entry) {
5451 s->first_displayed_entry = s->matched_entry;
5458 static const struct got_error *
5459 show_tree_view(struct tog_view *view)
5461 const struct got_error *err = NULL;
5462 struct tog_tree_view_state *s = &view->state.tree;
5465 err = tree_entry_path(&parent_path, &s->parents, NULL);
5469 err = draw_tree_entries(view, parent_path);
5476 static const struct got_error *
5477 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5479 const struct got_error *err = NULL;
5480 struct tog_tree_view_state *s = &view->state.tree;
5481 struct tog_view *log_view, *ref_view;
5482 struct got_tree_entry *te;
5487 s->show_ids = !s->show_ids;
5490 if (!s->selected_entry)
5492 if (view_is_parent_view(view))
5493 begin_x = view_split_begin_x(view->begin_x);
5494 err = log_selected_tree_entry(&log_view, begin_x, s);
5496 log_view->focussed = 1;
5497 if (view_is_parent_view(view)) {
5498 err = view_close_child(view);
5501 view_set_child(view, log_view);
5502 view->focus_child = 1;
5504 *new_view = log_view;
5507 if (view_is_parent_view(view))
5508 begin_x = view_split_begin_x(view->begin_x);
5509 ref_view = view_open(view->nlines, view->ncols,
5510 view->begin_y, begin_x, TOG_VIEW_REF);
5511 if (ref_view == NULL)
5512 return got_error_from_errno("view_open");
5513 err = open_ref_view(ref_view, s->repo);
5515 view_close(ref_view);
5519 ref_view->focussed = 1;
5520 if (view_is_parent_view(view)) {
5521 err = view_close_child(view);
5524 view_set_child(view, ref_view);
5525 view->focus_child = 1;
5527 *new_view = ref_view;
5532 if (s->tree == s->root)
5533 s->first_displayed_entry =
5534 got_object_tree_get_first_entry(s->tree);
5536 s->first_displayed_entry = NULL;
5541 te = got_object_tree_get_last_entry(s->tree);
5542 for (n = 0; n < view->nlines - 3; n++) {
5544 if(s->tree != s->root) {
5545 s->first_displayed_entry = NULL;
5550 s->first_displayed_entry = te;
5551 te = got_tree_entry_get_prev(s->tree, te);
5554 s->selected = n - 1;
5559 if (s->selected > 0) {
5563 tree_scroll_up(s, 1);
5567 if (s->tree == s->root) {
5568 if (got_object_tree_get_first_entry(s->tree) ==
5569 s->first_displayed_entry)
5572 if (s->first_displayed_entry == NULL)
5575 tree_scroll_up(s, MAX(0, view->nlines - 3));
5580 if (s->selected < s->ndisplayed - 1) {
5584 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5586 /* can't scroll any further */
5588 tree_scroll_down(s, 1);
5592 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5594 /* can't scroll any further; move cursor down */
5595 if (s->selected < s->ndisplayed - 1)
5596 s->selected = s->ndisplayed - 1;
5599 tree_scroll_down(s, view->nlines - 3);
5604 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5605 struct tog_parent_tree *parent;
5606 /* user selected '..' */
5607 if (s->tree == s->root)
5609 parent = TAILQ_FIRST(&s->parents);
5610 TAILQ_REMOVE(&s->parents, parent,
5612 got_object_tree_close(s->tree);
5613 s->tree = parent->tree;
5614 s->first_displayed_entry =
5615 parent->first_displayed_entry;
5617 parent->selected_entry;
5618 s->selected = parent->selected;
5620 } else if (S_ISDIR(got_tree_entry_get_mode(
5621 s->selected_entry))) {
5622 struct got_tree_object *subtree;
5623 err = got_object_open_as_tree(&subtree, s->repo,
5624 got_tree_entry_get_id(s->selected_entry));
5627 err = tree_view_visit_subtree(s, subtree);
5629 got_object_tree_close(subtree);
5632 } else if (S_ISREG(got_tree_entry_get_mode(
5633 s->selected_entry))) {
5634 struct tog_view *blame_view;
5635 int begin_x = view_is_parent_view(view) ?
5636 view_split_begin_x(view->begin_x) : 0;
5638 err = blame_tree_entry(&blame_view, begin_x,
5639 s->selected_entry, &s->parents,
5640 s->commit_id, s->repo);
5644 blame_view->focussed = 1;
5645 if (view_is_parent_view(view)) {
5646 err = view_close_child(view);
5649 view_set_child(view, blame_view);
5650 view->focus_child = 1;
5652 *new_view = blame_view;
5656 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5657 s->selected = view->nlines - 4;
5670 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5675 static const struct got_error *
5676 cmd_tree(int argc, char *argv[])
5678 const struct got_error *error;
5679 struct got_repository *repo = NULL;
5680 struct got_worktree *worktree = NULL;
5681 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5682 struct got_object_id *commit_id = NULL;
5683 struct got_commit_object *commit = NULL;
5684 const char *commit_id_arg = NULL;
5686 struct got_reference *ref = NULL;
5687 const char *head_ref_name = NULL;
5689 struct tog_view *view;
5691 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5694 commit_id_arg = optarg;
5697 repo_path = realpath(optarg, NULL);
5698 if (repo_path == NULL)
5699 return got_error_from_errno2("realpath",
5714 if (repo_path == NULL) {
5715 cwd = getcwd(NULL, 0);
5717 return got_error_from_errno("getcwd");
5718 error = got_worktree_open(&worktree, cwd);
5719 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5723 strdup(got_worktree_get_repo_path(worktree));
5725 repo_path = strdup(cwd);
5726 if (repo_path == NULL) {
5727 error = got_error_from_errno("strdup");
5732 error = got_repo_open(&repo, repo_path, NULL);
5736 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5743 error = apply_unveil(got_repo_get_path(repo), NULL);
5747 error = tog_load_refs(repo, 0);
5751 if (commit_id_arg == NULL) {
5752 error = got_repo_match_object_id(&commit_id, &label,
5753 worktree ? got_worktree_get_head_ref_name(worktree) :
5754 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5757 head_ref_name = label;
5759 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5761 head_ref_name = got_ref_get_name(ref);
5762 else if (error->code != GOT_ERR_NOT_REF)
5764 error = got_repo_match_object_id(&commit_id, NULL,
5765 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5770 error = got_object_open_as_commit(&commit, repo, commit_id);
5774 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5776 error = got_error_from_errno("view_open");
5779 error = open_tree_view(view, commit_id, head_ref_name, repo);
5782 if (!got_path_is_root_dir(in_repo_path)) {
5783 error = tree_view_walk_path(&view->state.tree, commit,
5790 /* Release work tree lock. */
5791 got_worktree_close(worktree);
5794 error = view_loop(view);
5803 const struct got_error *close_err = got_repo_close(repo);
5811 static const struct got_error *
5812 ref_view_load_refs(struct tog_ref_view_state *s)
5814 struct got_reflist_entry *sre;
5815 struct tog_reflist_entry *re;
5818 TAILQ_FOREACH(sre, &tog_refs, entry) {
5819 if (strncmp(got_ref_get_name(sre->ref),
5820 "refs/got/", 9) == 0 &&
5821 strncmp(got_ref_get_name(sre->ref),
5822 "refs/got/backup/", 16) != 0)
5825 re = malloc(sizeof(*re));
5827 return got_error_from_errno("malloc");
5829 re->ref = got_ref_dup(sre->ref);
5830 if (re->ref == NULL)
5831 return got_error_from_errno("got_ref_dup");
5832 re->idx = s->nrefs++;
5833 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5836 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5841 ref_view_free_refs(struct tog_ref_view_state *s)
5843 struct tog_reflist_entry *re;
5845 while (!TAILQ_EMPTY(&s->refs)) {
5846 re = TAILQ_FIRST(&s->refs);
5847 TAILQ_REMOVE(&s->refs, re, entry);
5848 got_ref_close(re->ref);
5853 static const struct got_error *
5854 open_ref_view(struct tog_view *view, struct got_repository *repo)
5856 const struct got_error *err = NULL;
5857 struct tog_ref_view_state *s = &view->state.ref;
5859 s->selected_entry = 0;
5862 TAILQ_INIT(&s->refs);
5863 STAILQ_INIT(&s->colors);
5865 err = ref_view_load_refs(s);
5869 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5870 err = add_color(&s->colors, "^refs/heads/",
5871 TOG_COLOR_REFS_HEADS,
5872 get_color_value("TOG_COLOR_REFS_HEADS"));
5876 err = add_color(&s->colors, "^refs/tags/",
5877 TOG_COLOR_REFS_TAGS,
5878 get_color_value("TOG_COLOR_REFS_TAGS"));
5882 err = add_color(&s->colors, "^refs/remotes/",
5883 TOG_COLOR_REFS_REMOTES,
5884 get_color_value("TOG_COLOR_REFS_REMOTES"));
5888 err = add_color(&s->colors, "^refs/got/backup/",
5889 TOG_COLOR_REFS_BACKUP,
5890 get_color_value("TOG_COLOR_REFS_BACKUP"));
5895 view->show = show_ref_view;
5896 view->input = input_ref_view;
5897 view->close = close_ref_view;
5898 view->search_start = search_start_ref_view;
5899 view->search_next = search_next_ref_view;
5902 free_colors(&s->colors);
5906 static const struct got_error *
5907 close_ref_view(struct tog_view *view)
5909 struct tog_ref_view_state *s = &view->state.ref;
5911 ref_view_free_refs(s);
5912 free_colors(&s->colors);
5917 static const struct got_error *
5918 resolve_reflist_entry(struct got_object_id **commit_id,
5919 struct tog_reflist_entry *re, struct got_repository *repo)
5921 const struct got_error *err = NULL;
5922 struct got_object_id *obj_id;
5923 struct got_tag_object *tag = NULL;
5928 err = got_ref_resolve(&obj_id, repo, re->ref);
5932 err = got_object_get_type(&obj_type, repo, obj_id);
5937 case GOT_OBJ_TYPE_COMMIT:
5938 *commit_id = obj_id;
5940 case GOT_OBJ_TYPE_TAG:
5941 err = got_object_open_as_tag(&tag, repo, obj_id);
5945 err = got_object_get_type(&obj_type, repo,
5946 got_object_tag_get_object_id(tag));
5949 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5950 err = got_error(GOT_ERR_OBJ_TYPE);
5953 *commit_id = got_object_id_dup(
5954 got_object_tag_get_object_id(tag));
5955 if (*commit_id == NULL) {
5956 err = got_error_from_errno("got_object_id_dup");
5961 err = got_error(GOT_ERR_OBJ_TYPE);
5967 got_object_tag_close(tag);
5975 static const struct got_error *
5976 log_ref_entry(struct tog_view **new_view, int begin_x,
5977 struct tog_reflist_entry *re, struct got_repository *repo)
5979 struct tog_view *log_view;
5980 const struct got_error *err = NULL;
5981 struct got_object_id *commit_id = NULL;
5985 err = resolve_reflist_entry(&commit_id, re, repo);
5987 if (err->code != GOT_ERR_OBJ_TYPE)
5993 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5994 if (log_view == NULL) {
5995 err = got_error_from_errno("view_open");
5999 err = open_log_view(log_view, commit_id, repo,
6000 got_ref_get_name(re->ref), "", 0);
6003 view_close(log_view);
6005 *new_view = log_view;
6011 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6013 struct tog_reflist_entry *re;
6016 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6019 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6020 while (i++ < maxscroll) {
6023 s->first_displayed_entry = re;
6024 re = TAILQ_PREV(re, tog_reflist_head, entry);
6029 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6031 struct tog_reflist_entry *next, *last;
6034 if (s->first_displayed_entry)
6035 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6037 next = TAILQ_FIRST(&s->refs);
6039 last = s->last_displayed_entry;
6040 while (next && last && n++ < maxscroll) {
6041 last = TAILQ_NEXT(last, entry);
6043 s->first_displayed_entry = next;
6044 next = TAILQ_NEXT(next, entry);
6049 static const struct got_error *
6050 search_start_ref_view(struct tog_view *view)
6052 struct tog_ref_view_state *s = &view->state.ref;
6054 s->matched_entry = NULL;
6059 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6061 regmatch_t regmatch;
6063 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6067 static const struct got_error *
6068 search_next_ref_view(struct tog_view *view)
6070 struct tog_ref_view_state *s = &view->state.ref;
6071 struct tog_reflist_entry *re = NULL;
6073 if (!view->searching) {
6074 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6078 if (s->matched_entry) {
6079 if (view->searching == TOG_SEARCH_FORWARD) {
6080 if (s->selected_entry)
6081 re = TAILQ_NEXT(s->selected_entry, entry);
6083 re = TAILQ_PREV(s->selected_entry,
6084 tog_reflist_head, entry);
6086 if (s->selected_entry == NULL)
6087 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6089 re = TAILQ_PREV(s->selected_entry,
6090 tog_reflist_head, entry);
6093 if (s->selected_entry)
6094 re = s->selected_entry;
6095 else if (view->searching == TOG_SEARCH_FORWARD)
6096 re = TAILQ_FIRST(&s->refs);
6098 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6103 if (s->matched_entry == NULL) {
6104 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6107 if (view->searching == TOG_SEARCH_FORWARD)
6108 re = TAILQ_FIRST(&s->refs);
6110 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6113 if (match_reflist_entry(re, &view->regex)) {
6114 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6115 s->matched_entry = re;
6119 if (view->searching == TOG_SEARCH_FORWARD)
6120 re = TAILQ_NEXT(re, entry);
6122 re = TAILQ_PREV(re, tog_reflist_head, entry);
6125 if (s->matched_entry) {
6126 s->first_displayed_entry = s->matched_entry;
6133 static const struct got_error *
6134 show_ref_view(struct tog_view *view)
6136 const struct got_error *err = NULL;
6137 struct tog_ref_view_state *s = &view->state.ref;
6138 struct tog_reflist_entry *re;
6141 struct tog_color *tc;
6143 int limit = view->nlines;
6145 werase(view->window);
6152 re = s->first_displayed_entry;
6154 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6156 return got_error_from_errno("asprintf");
6158 err = format_line(&wline, &width, line, view->ncols, 0);
6163 if (view_needs_focus_indication(view))
6164 wstandout(view->window);
6165 waddwstr(view->window, wline);
6166 if (view_needs_focus_indication(view))
6167 wstandend(view->window);
6172 if (width < view->ncols - 1)
6173 waddch(view->window, '\n');
6178 while (re && limit > 0) {
6181 if (got_ref_is_symbolic(re->ref)) {
6182 if (asprintf(&line, "%s -> %s",
6183 got_ref_get_name(re->ref),
6184 got_ref_get_symref_target(re->ref)) == -1)
6185 return got_error_from_errno("asprintf");
6186 } else if (s->show_ids) {
6187 struct got_object_id *id;
6189 err = got_ref_resolve(&id, s->repo, re->ref);
6192 err = got_object_id_str(&id_str, id);
6197 if (asprintf(&line, "%s: %s",
6198 got_ref_get_name(re->ref), id_str) == -1) {
6199 err = got_error_from_errno("asprintf");
6207 line = strdup(got_ref_get_name(re->ref));
6209 return got_error_from_errno("strdup");
6212 err = format_line(&wline, &width, line, view->ncols, 0);
6217 if (n == s->selected) {
6219 wstandout(view->window);
6220 s->selected_entry = re;
6222 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6224 wattr_on(view->window,
6225 COLOR_PAIR(tc->colorpair), NULL);
6226 waddwstr(view->window, wline);
6228 wattr_off(view->window,
6229 COLOR_PAIR(tc->colorpair), NULL);
6230 if (width < view->ncols - 1)
6231 waddch(view->window, '\n');
6232 if (n == s->selected && view->focussed)
6233 wstandend(view->window);
6239 s->last_displayed_entry = re;
6242 re = TAILQ_NEXT(re, entry);
6249 static const struct got_error *
6250 browse_ref_tree(struct tog_view **new_view, int begin_x,
6251 struct tog_reflist_entry *re, struct got_repository *repo)
6253 const struct got_error *err = NULL;
6254 struct got_object_id *commit_id = NULL;
6255 struct tog_view *tree_view;
6259 err = resolve_reflist_entry(&commit_id, re, repo);
6261 if (err->code != GOT_ERR_OBJ_TYPE)
6268 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6269 if (tree_view == NULL) {
6270 err = got_error_from_errno("view_open");
6274 err = open_tree_view(tree_view, commit_id,
6275 got_ref_get_name(re->ref), repo);
6279 *new_view = tree_view;
6284 static const struct got_error *
6285 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6287 const struct got_error *err = NULL;
6288 struct tog_ref_view_state *s = &view->state.ref;
6289 struct tog_view *log_view, *tree_view;
6290 struct tog_reflist_entry *re;
6295 s->show_ids = !s->show_ids;
6298 s->sort_by_date = !s->sort_by_date;
6299 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6300 got_ref_cmp_by_commit_timestamp_descending :
6301 tog_ref_cmp_by_name, s->repo);
6304 got_reflist_object_id_map_free(tog_refs_idmap);
6305 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6306 &tog_refs, s->repo);
6309 ref_view_free_refs(s);
6310 err = ref_view_load_refs(s);
6314 if (!s->selected_entry)
6316 if (view_is_parent_view(view))
6317 begin_x = view_split_begin_x(view->begin_x);
6318 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6321 log_view->focussed = 1;
6322 if (view_is_parent_view(view)) {
6323 err = view_close_child(view);
6326 view_set_child(view, log_view);
6327 view->focus_child = 1;
6329 *new_view = log_view;
6332 if (!s->selected_entry)
6334 if (view_is_parent_view(view))
6335 begin_x = view_split_begin_x(view->begin_x);
6336 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6338 if (err || tree_view == NULL)
6341 tree_view->focussed = 1;
6342 if (view_is_parent_view(view)) {
6343 err = view_close_child(view);
6346 view_set_child(view, tree_view);
6347 view->focus_child = 1;
6349 *new_view = tree_view;
6354 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6359 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6360 for (n = 0; n < view->nlines - 1; n++) {
6363 s->first_displayed_entry = re;
6364 re = TAILQ_PREV(re, tog_reflist_head, entry);
6367 s->selected = n - 1;
6372 if (s->selected > 0) {
6376 ref_scroll_up(s, 1);
6380 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6382 ref_scroll_up(s, MAX(0, view->nlines - 1));
6387 if (s->selected < s->ndisplayed - 1) {
6391 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6392 /* can't scroll any further */
6394 ref_scroll_down(s, 1);
6398 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6399 /* can't scroll any further; move cursor down */
6400 if (s->selected < s->ndisplayed - 1)
6401 s->selected = s->ndisplayed - 1;
6404 ref_scroll_down(s, view->nlines - 1);
6408 err = tog_load_refs(s->repo, s->sort_by_date);
6411 ref_view_free_refs(s);
6412 err = ref_view_load_refs(s);
6415 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6416 s->selected = view->nlines - 2;
6429 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6434 static const struct got_error *
6435 cmd_ref(int argc, char *argv[])
6437 const struct got_error *error;
6438 struct got_repository *repo = NULL;
6439 struct got_worktree *worktree = NULL;
6440 char *cwd = NULL, *repo_path = NULL;
6442 struct tog_view *view;
6444 while ((ch = getopt(argc, argv, "r:")) != -1) {
6447 repo_path = realpath(optarg, NULL);
6448 if (repo_path == NULL)
6449 return got_error_from_errno2("realpath",
6464 if (repo_path == NULL) {
6465 cwd = getcwd(NULL, 0);
6467 return got_error_from_errno("getcwd");
6468 error = got_worktree_open(&worktree, cwd);
6469 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6473 strdup(got_worktree_get_repo_path(worktree));
6475 repo_path = strdup(cwd);
6476 if (repo_path == NULL) {
6477 error = got_error_from_errno("strdup");
6482 error = got_repo_open(&repo, repo_path, NULL);
6488 error = apply_unveil(got_repo_get_path(repo), NULL);
6492 error = tog_load_refs(repo, 0);
6496 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6498 error = got_error_from_errno("view_open");
6502 error = open_ref_view(view, repo);
6507 /* Release work tree lock. */
6508 got_worktree_close(worktree);
6511 error = view_loop(view);
6516 const struct got_error *close_err = got_repo_close(repo);
6525 list_commands(FILE *fp)
6529 fprintf(fp, "commands:");
6530 for (i = 0; i < nitems(tog_commands); i++) {
6531 const struct tog_cmd *cmd = &tog_commands[i];
6532 fprintf(fp, " %s", cmd->name);
6538 usage(int hflag, int status)
6540 FILE *fp = (status == 0) ? stdout : stderr;
6542 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6545 fprintf(fp, "lazy usage: %s path\n", getprogname());
6552 make_argv(int argc, ...)
6560 argv = calloc(argc, sizeof(char *));
6563 for (i = 0; i < argc; i++) {
6564 argv[i] = strdup(va_arg(ap, char *));
6565 if (argv[i] == NULL)
6574 * Try to convert 'tog path' into a 'tog log path' command.
6575 * The user could simply have mistyped the command rather than knowingly
6576 * provided a path. So check whether argv[0] can in fact be resolved
6577 * to a path in the HEAD commit and print a special error if not.
6578 * This hack is for mpi@ <3
6580 static const struct got_error *
6581 tog_log_with_path(int argc, char *argv[])
6583 const struct got_error *error = NULL, *close_err;
6584 const struct tog_cmd *cmd = NULL;
6585 struct got_repository *repo = NULL;
6586 struct got_worktree *worktree = NULL;
6587 struct got_object_id *commit_id = NULL, *id = NULL;
6588 struct got_commit_object *commit = NULL;
6589 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6590 char *commit_id_str = NULL, **cmd_argv = NULL;
6592 cwd = getcwd(NULL, 0);
6594 return got_error_from_errno("getcwd");
6596 error = got_worktree_open(&worktree, cwd);
6597 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6601 repo_path = strdup(got_worktree_get_repo_path(worktree));
6603 repo_path = strdup(cwd);
6604 if (repo_path == NULL) {
6605 error = got_error_from_errno("strdup");
6609 error = got_repo_open(&repo, repo_path, NULL);
6613 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6618 error = tog_load_refs(repo, 0);
6621 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6622 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6623 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6628 got_worktree_close(worktree);
6632 error = got_object_open_as_commit(&commit, repo, commit_id);
6636 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6638 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6640 fprintf(stderr, "%s: '%s' is no known command or path\n",
6641 getprogname(), argv[0]);
6646 close_err = got_repo_close(repo);
6651 error = got_object_id_str(&commit_id_str, commit_id);
6655 cmd = &tog_commands[0]; /* log */
6657 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6658 error = cmd->cmd_main(argc, cmd_argv);
6661 close_err = got_repo_close(repo);
6666 got_object_commit_close(commit);
6668 got_worktree_close(worktree);
6670 free(commit_id_str);
6677 for (i = 0; i < argc; i++)
6686 main(int argc, char *argv[])
6688 const struct got_error *error = NULL;
6689 const struct tog_cmd *cmd = NULL;
6690 int ch, hflag = 0, Vflag = 0;
6691 char **cmd_argv = NULL;
6692 static const struct option longopts[] = {
6693 { "version", no_argument, NULL, 'V' },
6697 setlocale(LC_CTYPE, "");
6699 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6719 got_version_print_str();
6724 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6732 /* Build an argument vector which runs a default command. */
6733 cmd = &tog_commands[0];
6735 cmd_argv = make_argv(argc, cmd->name);
6739 /* Did the user specify a command? */
6740 for (i = 0; i < nitems(tog_commands); i++) {
6741 if (strncmp(tog_commands[i].name, argv[0],
6742 strlen(argv[0])) == 0) {
6743 cmd = &tog_commands[i];
6752 /* No command specified; try log with a path */
6753 error = tog_log_with_path(argc, argv);
6758 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6765 for (i = 0; i < argc; i++)
6770 if (error && error->code != GOT_ERR_CANCELLED)
6771 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);