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 #if defined(__FreeBSD__) || defined(__APPLE__)
24 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
45 #include "got_compat.h"
47 #include "got_version.h"
48 #include "got_error.h"
49 #include "got_object.h"
50 #include "got_reference.h"
51 #include "got_repository.h"
53 #include "got_opentemp.h"
55 #include "got_cancel.h"
56 #include "got_commit_graph.h"
57 #include "got_blame.h"
58 #include "got_privsep.h"
60 #include "got_worktree.h"
63 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
67 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
71 #define CTRL(x) ((x) & 0x1f)
75 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
80 const struct got_error *(*cmd_main)(int, char *[]);
81 void (*cmd_usage)(void);
84 __dead static void usage(int, int);
85 __dead static void usage_log(void);
86 __dead static void usage_diff(void);
87 __dead static void usage_blame(void);
88 __dead static void usage_tree(void);
89 __dead static void usage_ref(void);
91 static const struct got_error* cmd_log(int, char *[]);
92 static const struct got_error* cmd_diff(int, char *[]);
93 static const struct got_error* cmd_blame(int, char *[]);
94 static const struct got_error* cmd_tree(int, char *[]);
95 static const struct got_error* cmd_ref(int, char *[]);
97 static const struct tog_cmd tog_commands[] = {
98 { "log", cmd_log, usage_log },
99 { "diff", cmd_diff, usage_diff },
100 { "blame", cmd_blame, usage_blame },
101 { "tree", cmd_tree, usage_tree },
102 { "ref", cmd_ref, usage_ref },
119 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
121 #define TOG_EOF_STRING "(END)"
123 struct commit_queue_entry {
124 TAILQ_ENTRY(commit_queue_entry) entry;
125 struct got_object_id *id;
126 struct got_commit_object *commit;
129 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
130 struct commit_queue {
132 struct commit_queue_head head;
136 STAILQ_ENTRY(tog_color) entry;
140 STAILQ_HEAD(tog_colors, tog_color);
142 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
143 static struct got_reflist_object_id_map *tog_refs_idmap;
144 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
146 static const struct got_error *
147 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
148 struct got_reference* re2)
150 const char *name1 = got_ref_get_name(re1);
151 const char *name2 = got_ref_get_name(re2);
152 int isbackup1, isbackup2;
154 /* Sort backup refs towards the bottom of the list. */
155 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
156 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
157 if (!isbackup1 && isbackup2) {
160 } else if (isbackup1 && !isbackup2) {
165 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
169 static const struct got_error *
170 tog_load_refs(struct got_repository *repo, int sort_by_date)
172 const struct got_error *err;
174 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
175 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
180 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
187 if (tog_refs_idmap) {
188 got_reflist_object_id_map_free(tog_refs_idmap);
189 tog_refs_idmap = NULL;
191 got_ref_list_free(&tog_refs);
194 static const struct got_error *
195 add_color(struct tog_colors *colors, const char *pattern,
196 int idx, short color)
198 const struct got_error *err = NULL;
199 struct tog_color *tc;
202 if (idx < 1 || idx > COLOR_PAIRS - 1)
205 init_pair(idx, color, -1);
207 tc = calloc(1, sizeof(*tc));
209 return got_error_from_errno("calloc");
210 regerr = regcomp(&tc->regex, pattern,
211 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
213 static char regerr_msg[512];
214 static char err_msg[512];
215 regerror(regerr, &tc->regex, regerr_msg,
217 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
219 err = got_error_msg(GOT_ERR_REGEX, err_msg);
224 STAILQ_INSERT_HEAD(colors, tc, entry);
229 free_colors(struct tog_colors *colors)
231 struct tog_color *tc;
233 while (!STAILQ_EMPTY(colors)) {
234 tc = STAILQ_FIRST(colors);
235 STAILQ_REMOVE_HEAD(colors, entry);
241 static struct tog_color *
242 get_color(struct tog_colors *colors, int colorpair)
244 struct tog_color *tc = NULL;
246 STAILQ_FOREACH(tc, colors, entry) {
247 if (tc->colorpair == colorpair)
255 default_color_value(const char *envvar)
257 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
258 return COLOR_MAGENTA;
259 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
261 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
263 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
265 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
266 return COLOR_MAGENTA;
267 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
268 return COLOR_MAGENTA;
269 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
271 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
273 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
275 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
277 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
279 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
281 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
282 return COLOR_MAGENTA;
283 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
285 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
292 get_color_value(const char *envvar)
294 const char *val = getenv(envvar);
297 return default_color_value(envvar);
299 if (strcasecmp(val, "black") == 0)
301 if (strcasecmp(val, "red") == 0)
303 if (strcasecmp(val, "green") == 0)
305 if (strcasecmp(val, "yellow") == 0)
307 if (strcasecmp(val, "blue") == 0)
309 if (strcasecmp(val, "magenta") == 0)
310 return COLOR_MAGENTA;
311 if (strcasecmp(val, "cyan") == 0)
313 if (strcasecmp(val, "white") == 0)
315 if (strcasecmp(val, "default") == 0)
318 return default_color_value(envvar);
322 struct tog_diff_view_state {
323 struct got_object_id *id1, *id2;
324 const char *label1, *label2;
327 int first_displayed_line;
328 int last_displayed_line;
331 int ignore_whitespace;
333 struct got_repository *repo;
334 struct tog_colors colors;
340 /* passed from log or blame view; may be NULL */
341 struct tog_view *parent_view;
344 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
345 static volatile sig_atomic_t tog_thread_error;
347 struct tog_log_thread_args {
348 pthread_cond_t need_commits;
349 pthread_cond_t commit_loaded;
352 struct got_commit_graph *graph;
353 struct commit_queue *commits;
354 const char *in_repo_path;
355 struct got_object_id *start_id;
356 struct got_repository *repo;
360 struct commit_queue_entry **first_displayed_entry;
361 struct commit_queue_entry **selected_entry;
363 int *search_next_done;
367 struct tog_log_view_state {
368 struct commit_queue commits;
369 struct commit_queue_entry *first_displayed_entry;
370 struct commit_queue_entry *last_displayed_entry;
371 struct commit_queue_entry *selected_entry;
376 struct got_repository *repo;
377 struct got_object_id *start_id;
380 struct tog_log_thread_args thread_args;
381 struct commit_queue_entry *matched_entry;
382 struct commit_queue_entry *search_entry;
383 struct tog_colors colors;
386 #define TOG_COLOR_DIFF_MINUS 1
387 #define TOG_COLOR_DIFF_PLUS 2
388 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
389 #define TOG_COLOR_DIFF_META 4
390 #define TOG_COLOR_TREE_SUBMODULE 5
391 #define TOG_COLOR_TREE_SYMLINK 6
392 #define TOG_COLOR_TREE_DIRECTORY 7
393 #define TOG_COLOR_TREE_EXECUTABLE 8
394 #define TOG_COLOR_COMMIT 9
395 #define TOG_COLOR_AUTHOR 10
396 #define TOG_COLOR_DATE 11
397 #define TOG_COLOR_REFS_HEADS 12
398 #define TOG_COLOR_REFS_TAGS 13
399 #define TOG_COLOR_REFS_REMOTES 14
400 #define TOG_COLOR_REFS_BACKUP 15
402 struct tog_blame_cb_args {
403 struct tog_blame_line *lines; /* one per line */
406 struct tog_view *view;
407 struct got_object_id *commit_id;
411 struct tog_blame_thread_args {
413 struct got_repository *repo;
414 struct tog_blame_cb_args *cb_args;
416 got_cancel_cb cancel_cb;
423 struct tog_blame_line *lines;
427 struct tog_blame_thread_args thread_args;
428 struct tog_blame_cb_args cb_args;
433 struct tog_blame_view_state {
434 int first_displayed_line;
435 int last_displayed_line;
437 int last_diffed_line;
441 struct got_object_id_queue blamed_commits;
442 struct got_object_qid *blamed_commit;
444 struct got_repository *repo;
445 struct got_object_id *commit_id;
446 struct tog_blame blame;
448 struct tog_colors colors;
451 struct tog_parent_tree {
452 TAILQ_ENTRY(tog_parent_tree) entry;
453 struct got_tree_object *tree;
454 struct got_tree_entry *first_displayed_entry;
455 struct got_tree_entry *selected_entry;
459 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
461 struct tog_tree_view_state {
463 struct got_object_id *commit_id;/* commit which this tree belongs to */
464 struct got_tree_object *root; /* the commit's root tree entry */
465 struct got_tree_object *tree; /* currently displayed (sub-)tree */
466 struct got_tree_entry *first_displayed_entry;
467 struct got_tree_entry *last_displayed_entry;
468 struct got_tree_entry *selected_entry;
469 int ndisplayed, selected, show_ids;
470 struct tog_parent_trees parents; /* parent trees of current sub-tree */
472 struct got_repository *repo;
473 struct got_tree_entry *matched_entry;
474 struct tog_colors colors;
477 struct tog_reflist_entry {
478 TAILQ_ENTRY(tog_reflist_entry) entry;
479 struct got_reference *ref;
483 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
485 struct tog_ref_view_state {
486 struct tog_reflist_head refs;
487 struct tog_reflist_entry *first_displayed_entry;
488 struct tog_reflist_entry *last_displayed_entry;
489 struct tog_reflist_entry *selected_entry;
490 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
491 struct got_repository *repo;
492 struct tog_reflist_entry *matched_entry;
493 struct tog_colors colors;
497 * We implement two types of views: parent views and child views.
499 * The 'Tab' key switches focus between a parent view and its child view.
500 * Child views are shown side-by-side to their parent view, provided
501 * there is enough screen estate.
503 * When a new view is opened from within a parent view, this new view
504 * becomes a child view of the parent view, replacing any existing child.
506 * When a new view is opened from within a child view, this new view
507 * becomes a parent view which will obscure the views below until the
508 * user quits the new parent view by typing 'q'.
510 * This list of views contains parent views only.
511 * Child views are only pointed to by their parent view.
513 TAILQ_HEAD(tog_view_list_head, tog_view);
516 TAILQ_ENTRY(tog_view) entry;
519 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
520 int resized_y, resized_x; /* begin_y/x based on user resizing */
521 int maxx, x; /* max column and current start column */
522 int lines, cols; /* copies of LINES and COLS */
523 int nscrolled, offset; /* lines scrolled and hsplit line offset */
524 int ch, count; /* current keymap and count prefix */
525 int resized; /* set when in a resize event */
526 int focussed; /* Only set on one parent or child view at a time. */
528 struct tog_view *parent;
529 struct tog_view *child;
532 * This flag is initially set on parent views when a new child view
533 * is created. It gets toggled when the 'Tab' key switches focus
534 * between parent and child.
535 * The flag indicates whether focus should be passed on to our child
536 * view if this parent view gets picked for focus after another parent
537 * view was closed. This prevents child views from losing focus in such
542 enum tog_view_mode mode;
543 /* type-specific state */
544 enum tog_view_type type;
546 struct tog_diff_view_state diff;
547 struct tog_log_view_state log;
548 struct tog_blame_view_state blame;
549 struct tog_tree_view_state tree;
550 struct tog_ref_view_state ref;
553 const struct got_error *(*show)(struct tog_view *);
554 const struct got_error *(*input)(struct tog_view **,
555 struct tog_view *, int);
556 const struct got_error *(*reset)(struct tog_view *);
557 const struct got_error *(*resize)(struct tog_view *, int);
558 const struct got_error *(*close)(struct tog_view *);
560 const struct got_error *(*search_start)(struct tog_view *);
561 const struct got_error *(*search_next)(struct tog_view *);
564 #define TOG_SEARCH_FORWARD 1
565 #define TOG_SEARCH_BACKWARD 2
566 int search_next_done;
567 #define TOG_SEARCH_HAVE_MORE 1
568 #define TOG_SEARCH_NO_MORE 2
569 #define TOG_SEARCH_HAVE_NONE 3
574 static const struct got_error *open_diff_view(struct tog_view *,
575 struct got_object_id *, struct got_object_id *,
576 const char *, const char *, int, int, int, struct tog_view *,
577 struct got_repository *);
578 static const struct got_error *show_diff_view(struct tog_view *);
579 static const struct got_error *input_diff_view(struct tog_view **,
580 struct tog_view *, int);
581 static const struct got_error *reset_diff_view(struct tog_view *);
582 static const struct got_error* close_diff_view(struct tog_view *);
583 static const struct got_error *search_start_diff_view(struct tog_view *);
584 static const struct got_error *search_next_diff_view(struct tog_view *);
586 static const struct got_error *open_log_view(struct tog_view *,
587 struct got_object_id *, struct got_repository *,
588 const char *, const char *, int);
589 static const struct got_error * show_log_view(struct tog_view *);
590 static const struct got_error *input_log_view(struct tog_view **,
591 struct tog_view *, int);
592 static const struct got_error *resize_log_view(struct tog_view *, int);
593 static const struct got_error *close_log_view(struct tog_view *);
594 static const struct got_error *search_start_log_view(struct tog_view *);
595 static const struct got_error *search_next_log_view(struct tog_view *);
597 static const struct got_error *open_blame_view(struct tog_view *, char *,
598 struct got_object_id *, struct got_repository *);
599 static const struct got_error *show_blame_view(struct tog_view *);
600 static const struct got_error *input_blame_view(struct tog_view **,
601 struct tog_view *, int);
602 static const struct got_error *reset_blame_view(struct tog_view *);
603 static const struct got_error *close_blame_view(struct tog_view *);
604 static const struct got_error *search_start_blame_view(struct tog_view *);
605 static const struct got_error *search_next_blame_view(struct tog_view *);
607 static const struct got_error *open_tree_view(struct tog_view *,
608 struct got_object_id *, const char *, struct got_repository *);
609 static const struct got_error *show_tree_view(struct tog_view *);
610 static const struct got_error *input_tree_view(struct tog_view **,
611 struct tog_view *, int);
612 static const struct got_error *close_tree_view(struct tog_view *);
613 static const struct got_error *search_start_tree_view(struct tog_view *);
614 static const struct got_error *search_next_tree_view(struct tog_view *);
616 static const struct got_error *open_ref_view(struct tog_view *,
617 struct got_repository *);
618 static const struct got_error *show_ref_view(struct tog_view *);
619 static const struct got_error *input_ref_view(struct tog_view **,
620 struct tog_view *, int);
621 static const struct got_error *close_ref_view(struct tog_view *);
622 static const struct got_error *search_start_ref_view(struct tog_view *);
623 static const struct got_error *search_next_ref_view(struct tog_view *);
625 static volatile sig_atomic_t tog_sigwinch_received;
626 static volatile sig_atomic_t tog_sigpipe_received;
627 static volatile sig_atomic_t tog_sigcont_received;
628 static volatile sig_atomic_t tog_sigint_received;
629 static volatile sig_atomic_t tog_sigterm_received;
632 tog_sigwinch(int signo)
634 tog_sigwinch_received = 1;
638 tog_sigpipe(int signo)
640 tog_sigpipe_received = 1;
644 tog_sigcont(int signo)
646 tog_sigcont_received = 1;
650 tog_sigint(int signo)
652 tog_sigint_received = 1;
656 tog_sigterm(int signo)
658 tog_sigterm_received = 1;
662 tog_fatal_signal_received(void)
664 return (tog_sigpipe_received ||
665 tog_sigint_received || tog_sigint_received);
668 static const struct got_error *
669 view_close(struct tog_view *view)
671 const struct got_error *err = NULL, *child_err = NULL;
674 child_err = view_close(view->child);
678 err = view->close(view);
680 del_panel(view->panel);
682 delwin(view->window);
684 return err ? err : child_err;
687 static struct tog_view *
688 view_open(int nlines, int ncols, int begin_y, int begin_x,
689 enum tog_view_type type)
691 struct tog_view *view = calloc(1, sizeof(*view));
699 view->nlines = nlines ? nlines : LINES - begin_y;
700 view->ncols = ncols ? ncols : COLS - begin_x;
701 view->begin_y = begin_y;
702 view->begin_x = begin_x;
703 view->window = newwin(nlines, ncols, begin_y, begin_x);
704 if (view->window == NULL) {
708 view->panel = new_panel(view->window);
709 if (view->panel == NULL ||
710 set_panel_userptr(view->panel, view) != OK) {
715 keypad(view->window, TRUE);
720 view_split_begin_x(int begin_x)
722 if (begin_x > 0 || COLS < 120)
724 return (COLS - MAX(COLS / 2, 80));
727 /* XXX Stub till we decide what to do. */
729 view_split_begin_y(int lines)
731 return lines * HSPLIT_SCALE;
734 static const struct got_error *view_resize(struct tog_view *);
736 static const struct got_error *
737 view_splitscreen(struct tog_view *view)
739 const struct got_error *err = NULL;
741 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
742 if (view->resized_y && view->resized_y < view->lines)
743 view->begin_y = view->resized_y;
745 view->begin_y = view_split_begin_y(view->nlines);
747 } else if (!view->resized) {
748 if (view->resized_x && view->resized_x < view->cols - 1 &&
750 view->begin_x = view->resized_x;
752 view->begin_x = view_split_begin_x(0);
755 view->nlines = LINES - view->begin_y;
756 view->ncols = COLS - view->begin_x;
759 err = view_resize(view);
763 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
764 view->parent->nlines = view->begin_y;
766 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
767 return got_error_from_errno("mvwin");
772 static const struct got_error *
773 view_fullscreen(struct tog_view *view)
775 const struct got_error *err = NULL;
778 view->begin_y = view->resized ? view->begin_y : 0;
779 view->nlines = view->resized ? view->nlines : LINES;
783 err = view_resize(view);
787 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
788 return got_error_from_errno("mvwin");
794 view_is_parent_view(struct tog_view *view)
796 return view->parent == NULL;
800 view_is_splitscreen(struct tog_view *view)
802 return view->begin_x > 0 || view->begin_y > 0;
806 view_is_fullscreen(struct tog_view *view)
808 return view->nlines == LINES && view->ncols == COLS;
812 view_is_hsplit_top(struct tog_view *view)
814 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
815 view_is_splitscreen(view->child);
819 view_border(struct tog_view *view)
822 const struct tog_view *view_above;
825 return view_border(view->parent);
827 panel = panel_above(view->panel);
831 view_above = panel_userptr(panel);
832 if (view->mode == TOG_VIEW_SPLIT_HRZN)
833 mvwhline(view->window, view_above->begin_y - 1,
834 view->begin_x, got_locale_is_utf8() ?
835 ACS_HLINE : '-', view->ncols);
837 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
838 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
841 static const struct got_error *view_init_hsplit(struct tog_view *, int);
842 static const struct got_error *request_log_commits(struct tog_view *);
843 static const struct got_error *offset_selection_down(struct tog_view *);
844 static void offset_selection_up(struct tog_view *);
845 static void view_get_split(struct tog_view *, int *, int *);
847 static const struct got_error *
848 view_resize(struct tog_view *view)
850 const struct got_error *err = NULL;
851 int dif, nlines, ncols;
853 dif = LINES - view->lines; /* line difference */
855 if (view->lines > LINES)
856 nlines = view->nlines - (view->lines - LINES);
858 nlines = view->nlines + (LINES - view->lines);
859 if (view->cols > COLS)
860 ncols = view->ncols - (view->cols - COLS);
862 ncols = view->ncols + (COLS - view->cols);
865 int hs = view->child->begin_y;
867 if (!view_is_fullscreen(view))
868 view->child->begin_x = view_split_begin_x(view->begin_x);
869 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
870 view->child->begin_x == 0) {
873 view_fullscreen(view->child);
874 if (view->child->focussed)
875 show_panel(view->child->panel);
877 show_panel(view->panel);
879 ncols = view->child->begin_x;
881 view_splitscreen(view->child);
882 show_panel(view->child->panel);
885 * XXX This is ugly and needs to be moved into the above
886 * logic but "works" for now and my attempts at moving it
887 * break either 'tab' or 'F' key maps in horizontal splits.
890 err = view_splitscreen(view->child);
893 if (dif < 0) { /* top split decreased */
894 err = offset_selection_down(view);
901 show_panel(view->child->panel);
902 nlines = view->nlines;
904 } else if (view->parent == NULL)
907 if (view->resize && dif > 0) {
908 err = view->resize(view, dif);
913 if (wresize(view->window, nlines, ncols) == ERR)
914 return got_error_from_errno("wresize");
915 if (replace_panel(view->panel, view->window) == ERR)
916 return got_error_from_errno("replace_panel");
917 wclear(view->window);
919 view->nlines = nlines;
927 static const struct got_error *
928 resize_log_view(struct tog_view *view, int increase)
930 struct tog_log_view_state *s = &view->state.log;
931 const struct got_error *err = NULL;
932 int n = s->selected_entry->idx + view->lines - s->selected;
935 * Request commits to account for the increased
936 * height so we have enough to populate the view.
938 if (s->commits.ncommits < n) {
939 view->nscrolled = n - s->commits.ncommits + increase + 1;
940 err = request_log_commits(view);
947 view_adjust_offset(struct tog_view *view, int n)
952 if (view->parent && view->parent->offset) {
953 if (view->parent->offset + n >= 0)
954 view->parent->offset += n;
956 view->parent->offset = 0;
957 } else if (view->offset) {
958 if (view->offset - n >= 0)
965 static const struct got_error *
966 view_resize_split(struct tog_view *view, int resize)
968 const struct got_error *err = NULL;
969 struct tog_view *v = NULL;
976 if (!v->child || !view_is_splitscreen(v->child))
979 v->resized = v->child->resized = resize; /* lock for resize event */
981 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
982 int y = v->child->begin_y;
984 if (v->child->resized_y)
985 v->child->begin_y = v->child->resized_y;
987 v->child->begin_y -= resize;
989 v->child->begin_y += resize;
990 if (v->child->begin_y < 3) {
992 v->child->begin_y = 3;
993 } else if (v->child->begin_y > LINES - 1) {
995 v->child->begin_y = LINES - 1;
998 v->child->ncols = COLS;
999 view_adjust_offset(view, resize);
1000 err = view_init_hsplit(v, v->child->begin_y);
1003 v->child->resized_y = v->child->begin_y;
1004 if (y > v->child->begin_y && v->child->type == TOG_VIEW_LOG)
1005 v->child->nscrolled = y - v->child->begin_y;
1006 else if (y < v->child->begin_y && v->type == TOG_VIEW_LOG)
1007 v->nscrolled = v->child->begin_y - y;
1009 if (v->child->resized_x)
1010 v->child->begin_x = v->child->resized_x;
1012 v->child->begin_x -= resize;
1014 v->child->begin_x += resize;
1015 if (v->child->begin_x < 11) {
1017 v->child->begin_x = 11;
1018 } else if (v->child->begin_x > COLS - 1) {
1020 v->child->begin_x = COLS - 1;
1022 v->child->resized_x = v->child->begin_x;
1025 v->child->mode = v->mode;
1026 v->child->nlines = v->lines - v->child->begin_y;
1027 v->child->ncols = v->cols - v->child->begin_x;
1030 err = view_fullscreen(v);
1033 err = view_splitscreen(v->child);
1037 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1038 err = offset_selection_down(v->child);
1044 err = request_log_commits(v);
1045 else if (v->child->nscrolled)
1046 err = request_log_commits(v->child);
1048 v->resized = v->child->resized = 0;
1054 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1056 struct tog_view *v = src->child ? src->child : src;
1058 dst->resized_x = v->resized_x;
1059 dst->resized_y = v->resized_y;
1062 static const struct got_error *
1063 view_close_child(struct tog_view *view)
1065 const struct got_error *err = NULL;
1067 if (view->child == NULL)
1070 err = view_close(view->child);
1075 static const struct got_error *
1076 view_set_child(struct tog_view *view, struct tog_view *child)
1078 const struct got_error *err = NULL;
1080 view->child = child;
1081 child->parent = view;
1083 err = view_resize(view);
1087 if (view->child->resized_x || view->child->resized_y)
1088 err = view_resize_split(view, 0);
1094 tog_resizeterm(void)
1097 struct winsize size;
1099 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1100 cols = 80; /* Default */
1104 lines = size.ws_row;
1106 resize_term(lines, cols);
1109 static const struct got_error *
1110 view_search_start(struct tog_view *view)
1112 const struct got_error *err = NULL;
1113 struct tog_view *v = view;
1117 if (view->search_started) {
1118 regfree(&view->regex);
1119 view->searching = 0;
1120 memset(&view->regmatch, 0, sizeof(view->regmatch));
1122 view->search_started = 0;
1124 if (view->nlines < 1)
1127 if (view_is_hsplit_top(view))
1130 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1131 wclrtoeol(v->window);
1133 nodelay(view->window, FALSE); /* block for search term input */
1136 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1137 wrefresh(v->window);
1140 nodelay(view->window, TRUE);
1144 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1145 err = view->search_start(view);
1147 regfree(&view->regex);
1150 view->search_started = 1;
1151 view->searching = TOG_SEARCH_FORWARD;
1152 view->search_next_done = 0;
1153 view->search_next(view);
1159 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1160 static const struct got_error *
1161 switch_split(struct tog_view *view)
1163 const struct got_error *err = NULL;
1164 struct tog_view *v = NULL;
1171 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1172 v->mode = TOG_VIEW_SPLIT_VERT;
1174 v->mode = TOG_VIEW_SPLIT_HRZN;
1178 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1179 v->mode = TOG_VIEW_SPLIT_NONE;
1181 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1182 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1183 v->child->begin_y = v->child->resized_y;
1184 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1185 v->child->begin_x = v->child->resized_x;
1188 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1190 v->child->ncols = COLS;
1191 v->child->nscrolled = LINES - v->child->nlines;
1193 err = view_init_hsplit(v, v->child->begin_y);
1197 v->child->mode = v->mode;
1198 v->child->nlines = v->lines - v->child->begin_y;
1201 err = view_fullscreen(v);
1204 err = view_splitscreen(v->child);
1208 if (v->mode == TOG_VIEW_SPLIT_NONE)
1209 v->mode = TOG_VIEW_SPLIT_VERT;
1210 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1211 err = offset_selection_down(v);
1212 err = offset_selection_down(v->child);
1214 offset_selection_up(v);
1215 offset_selection_up(v->child);
1217 if (v->type == TOG_VIEW_LOG && v->nscrolled)
1218 err = request_log_commits(v);
1219 else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1220 err = request_log_commits(v->child);
1226 * Compute view->count from numeric input. Assign total to view->count and
1227 * return first non-numeric key entered.
1230 get_compound_key(struct tog_view *view, int c)
1232 struct tog_view *v = view;
1235 if (view_is_hsplit_top(view))
1237 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1241 cbreak(); /* block for input */
1242 wmove(v->window, v->nlines - 1, 0);
1243 wclrtoeol(v->window);
1244 waddch(v->window, ':');
1247 x = getcurx(v->window);
1248 if (x != ERR && x < view->ncols) {
1249 waddch(v->window, c);
1250 wrefresh(v->window);
1254 * Don't overflow. Max valid request should be the greatest
1255 * between the longest and total lines; cap at 10 million.
1260 n = n * 10 + (c - '0');
1261 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1263 /* Massage excessive or inapplicable values at the input handler. */
1269 static const struct got_error *
1270 view_input(struct tog_view **new, int *done, struct tog_view *view,
1271 struct tog_view_list_head *views)
1273 const struct got_error *err = NULL;
1279 /* Clear "no matches" indicator. */
1280 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1281 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1282 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1286 if (view->searching && !view->search_next_done) {
1287 errcode = pthread_mutex_unlock(&tog_mutex);
1289 return got_error_set_errno(errcode,
1290 "pthread_mutex_unlock");
1292 errcode = pthread_mutex_lock(&tog_mutex);
1294 return got_error_set_errno(errcode,
1295 "pthread_mutex_lock");
1296 view->search_next(view);
1300 nodelay(view->window, FALSE);
1301 /* Allow threads to make progress while we are waiting for input. */
1302 errcode = pthread_mutex_unlock(&tog_mutex);
1304 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1305 /* If we have an unfinished count, let C-g or backspace abort. */
1306 if (view->count && --view->count) {
1308 nodelay(view->window, TRUE);
1309 ch = wgetch(view->window);
1310 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1315 ch = wgetch(view->window);
1316 if (ch >= '1' && ch <= '9')
1317 view->ch = ch = get_compound_key(view, ch);
1319 errcode = pthread_mutex_lock(&tog_mutex);
1321 return got_error_set_errno(errcode, "pthread_mutex_lock");
1322 nodelay(view->window, TRUE);
1324 if (tog_sigwinch_received || tog_sigcont_received) {
1326 tog_sigwinch_received = 0;
1327 tog_sigcont_received = 0;
1328 TAILQ_FOREACH(v, views, entry) {
1329 err = view_resize(v);
1332 err = v->input(new, v, KEY_RESIZE);
1336 err = view_resize(v->child);
1339 err = v->child->input(new, v->child,
1343 if (v->child->resized_x || v->child->resized_y) {
1344 err = view_resize_split(v, 0);
1357 view->child->focussed = 1;
1358 view->focus_child = 1;
1359 } else if (view->parent) {
1361 view->parent->focussed = 1;
1362 view->parent->focus_child = 0;
1363 if (!view_is_splitscreen(view)) {
1364 if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1365 view->parent->type == TOG_VIEW_LOG) {
1366 err = request_log_commits(view->parent);
1370 offset_selection_up(view->parent);
1371 err = view_fullscreen(view->parent);
1378 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1379 if (view->parent->type == TOG_VIEW_LOG) {
1380 /* might need more commits to fill fullscreen */
1381 err = request_log_commits(view->parent);
1385 offset_selection_up(view->parent);
1387 err = view->input(new, view, ch);
1395 if (view_is_parent_view(view)) {
1396 if (view->child == NULL)
1398 if (view_is_splitscreen(view->child)) {
1400 view->child->focussed = 1;
1401 err = view_fullscreen(view->child);
1403 err = view_splitscreen(view->child);
1405 err = view_resize_split(view, 0);
1409 err = view->child->input(new, view->child,
1412 if (view_is_splitscreen(view)) {
1413 view->parent->focussed = 0;
1415 err = view_fullscreen(view);
1417 err = view_splitscreen(view);
1418 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1419 err = view_resize(view->parent);
1421 err = view_resize_split(view, 0);
1425 err = view->input(new, view, KEY_RESIZE);
1429 if (view->type == TOG_VIEW_LOG) {
1430 err = request_log_commits(view);
1435 err = offset_selection_down(view->parent);
1437 err = offset_selection_down(view);
1441 err = switch_split(view);
1444 err = view_resize_split(view, -1);
1447 err = view_resize_split(view, 1);
1453 if (view->search_start)
1454 view_search_start(view);
1456 err = view->input(new, view, ch);
1460 if (view->search_started && view->search_next) {
1461 view->searching = (ch == 'n' ?
1462 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1463 view->search_next_done = 0;
1464 view->search_next(view);
1466 err = view->input(new, view, ch);
1469 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1470 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1472 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1473 TAILQ_FOREACH(v, views, entry) {
1479 if (v->child && v->child->reset) {
1480 err = v->child->reset(v->child);
1487 err = view->input(new, view, ch);
1495 view_needs_focus_indication(struct tog_view *view)
1497 if (view_is_parent_view(view)) {
1498 if (view->child == NULL || view->child->focussed)
1500 if (!view_is_splitscreen(view->child))
1502 } else if (!view_is_splitscreen(view))
1505 return view->focussed;
1508 static const struct got_error *
1509 view_loop(struct tog_view *view)
1511 const struct got_error *err = NULL;
1512 struct tog_view_list_head views;
1513 struct tog_view *new_view;
1515 int fast_refresh = 10;
1516 int done = 0, errcode;
1518 mode = getenv("TOG_VIEW_SPLIT_MODE");
1519 if (!mode || !(*mode == 'h' || *mode == 'H'))
1520 view->mode = TOG_VIEW_SPLIT_VERT;
1522 view->mode = TOG_VIEW_SPLIT_HRZN;
1524 errcode = pthread_mutex_lock(&tog_mutex);
1526 return got_error_set_errno(errcode, "pthread_mutex_lock");
1529 TAILQ_INSERT_HEAD(&views, view, entry);
1532 err = view->show(view);
1537 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1538 !tog_fatal_signal_received()) {
1539 /* Refresh fast during initialization, then become slower. */
1540 if (fast_refresh && fast_refresh-- == 0)
1541 halfdelay(10); /* switch to once per second */
1543 err = view_input(&new_view, &done, view, &views);
1547 struct tog_view *v, *prev = NULL;
1549 if (view_is_parent_view(view))
1550 prev = TAILQ_PREV(view, tog_view_list_head,
1552 else if (view->parent)
1553 prev = view->parent;
1556 view->parent->child = NULL;
1557 view->parent->focus_child = 0;
1558 /* Restore fullscreen line height. */
1559 view->parent->nlines = view->parent->lines;
1560 err = view_resize(view->parent);
1563 /* Make resized splits persist. */
1564 view_transfer_size(view->parent, view);
1566 TAILQ_REMOVE(&views, view, entry);
1568 err = view_close(view);
1573 TAILQ_FOREACH(v, &views, entry) {
1577 if (view == NULL && new_view == NULL) {
1578 /* No view has focus. Try to pick one. */
1581 else if (!TAILQ_EMPTY(&views)) {
1582 view = TAILQ_LAST(&views,
1583 tog_view_list_head);
1586 if (view->focus_child) {
1587 view->child->focussed = 1;
1595 struct tog_view *v, *t;
1596 /* Only allow one parent view per type. */
1597 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1598 if (v->type != new_view->type)
1600 TAILQ_REMOVE(&views, v, entry);
1601 err = view_close(v);
1606 TAILQ_INSERT_TAIL(&views, new_view, entry);
1610 if (view_is_parent_view(view)) {
1611 if (view->child && view->child->focussed)
1614 if (view->parent && view->parent->focussed)
1615 view = view->parent;
1617 show_panel(view->panel);
1618 if (view->child && view_is_splitscreen(view->child))
1619 show_panel(view->child->panel);
1620 if (view->parent && view_is_splitscreen(view)) {
1621 err = view->parent->show(view->parent);
1625 err = view->show(view);
1629 err = view->child->show(view->child);
1638 while (!TAILQ_EMPTY(&views)) {
1639 const struct got_error *close_err;
1640 view = TAILQ_FIRST(&views);
1641 TAILQ_REMOVE(&views, view, entry);
1642 close_err = view_close(view);
1643 if (close_err && err == NULL)
1647 errcode = pthread_mutex_unlock(&tog_mutex);
1648 if (errcode && err == NULL)
1649 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1659 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1664 /* Create newly allocated wide-character string equivalent to a byte string. */
1665 static const struct got_error *
1666 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1669 const struct got_error *err = NULL;
1672 *wlen = mbstowcs(NULL, s, 0);
1673 if (*wlen == (size_t)-1) {
1675 if (errno != EILSEQ)
1676 return got_error_from_errno("mbstowcs");
1678 /* byte string invalid in current encoding; try to "fix" it */
1679 err = got_mbsavis(&vis, &vislen, s);
1682 *wlen = mbstowcs(NULL, vis, 0);
1683 if (*wlen == (size_t)-1) {
1684 err = got_error_from_errno("mbstowcs"); /* give up */
1689 *ws = calloc(*wlen + 1, sizeof(**ws));
1691 err = got_error_from_errno("calloc");
1695 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1696 err = got_error_from_errno("mbstowcs");
1707 static const struct got_error *
1708 expand_tab(char **ptr, const char *src)
1711 size_t len, n, idx = 0, sz = 0;
1714 n = len = strlen(src);
1715 dst = malloc(n + 1);
1717 return got_error_from_errno("malloc");
1719 while (idx < len && src[idx]) {
1720 const char c = src[idx];
1723 size_t nb = TABSIZE - sz % TABSIZE;
1726 p = realloc(dst, n + nb);
1729 return got_error_from_errno("realloc");
1734 memset(dst + sz, ' ', nb);
1737 dst[sz++] = src[idx];
1747 * Advance at most n columns from wline starting at offset off.
1748 * Return the index to the first character after the span operation.
1749 * Return the combined column width of all spanned wide character in
1753 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1755 int width, i, cols = 0;
1762 for (i = off; wline[i] != L'\0'; ++i) {
1763 if (wline[i] == L'\t')
1764 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1766 width = wcwidth(wline[i]);
1773 if (cols + width > n)
1783 * Format a line for display, ensuring that it won't overflow a width limit.
1784 * With scrolling, the width returned refers to the scrolled version of the
1785 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1787 static const struct got_error *
1788 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1789 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1791 const struct got_error *err = NULL;
1793 wchar_t *wline = NULL;
1802 err = expand_tab(&exstr, line);
1807 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1812 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1814 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1815 wline[wlen - 1] = L'\0';
1818 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1819 wline[wlen - 1] = L'\0';
1823 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1829 *scrollxp = scrollx;
1837 static const struct got_error*
1838 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1839 struct got_object_id *id, struct got_repository *repo)
1841 static const struct got_error *err = NULL;
1842 struct got_reflist_entry *re;
1848 TAILQ_FOREACH(re, refs, entry) {
1849 struct got_tag_object *tag = NULL;
1850 struct got_object_id *ref_id;
1853 name = got_ref_get_name(re->ref);
1854 if (strcmp(name, GOT_REF_HEAD) == 0)
1856 if (strncmp(name, "refs/", 5) == 0)
1858 if (strncmp(name, "got/", 4) == 0 &&
1859 strncmp(name, "got/backup/", 11) != 0)
1861 if (strncmp(name, "heads/", 6) == 0)
1863 if (strncmp(name, "remotes/", 8) == 0) {
1865 s = strstr(name, "/" GOT_REF_HEAD);
1866 if (s != NULL && s[strlen(s)] == '\0')
1869 err = got_ref_resolve(&ref_id, repo, re->ref);
1872 if (strncmp(name, "tags/", 5) == 0) {
1873 err = got_object_open_as_tag(&tag, repo, ref_id);
1875 if (err->code != GOT_ERR_OBJ_TYPE) {
1879 /* Ref points at something other than a tag. */
1884 cmp = got_object_id_cmp(tag ?
1885 got_object_tag_get_object_id(tag) : ref_id, id);
1888 got_object_tag_close(tag);
1892 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1893 s ? ", " : "", name) == -1) {
1894 err = got_error_from_errno("asprintf");
1905 static const struct got_error *
1906 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1911 smallerthan = strchr(author, '<');
1912 if (smallerthan && smallerthan[1] != '\0')
1913 author = smallerthan + 1;
1914 author[strcspn(author, "@>")] = '\0';
1915 return format_line(wauthor, author_width, NULL, author, 0, limit,
1919 static const struct got_error *
1920 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1921 struct got_object_id *id, const size_t date_display_cols,
1922 int author_display_cols)
1924 struct tog_log_view_state *s = &view->state.log;
1925 const struct got_error *err = NULL;
1926 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1927 char *logmsg0 = NULL, *logmsg = NULL;
1928 char *author = NULL;
1929 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1930 int author_width, logmsg_width;
1931 char *newline, *line = NULL;
1932 int col, limit, scrollx;
1933 const int avail = view->ncols;
1935 time_t committer_time;
1936 struct tog_color *tc;
1938 committer_time = got_object_commit_get_committer_time(commit);
1939 if (gmtime_r(&committer_time, &tm) == NULL)
1940 return got_error_from_errno("gmtime_r");
1941 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1942 return got_error(GOT_ERR_NO_SPACE);
1944 if (avail <= date_display_cols)
1945 limit = MIN(sizeof(datebuf) - 1, avail);
1947 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1948 tc = get_color(&s->colors, TOG_COLOR_DATE);
1950 wattr_on(view->window,
1951 COLOR_PAIR(tc->colorpair), NULL);
1952 waddnstr(view->window, datebuf, limit);
1954 wattr_off(view->window,
1955 COLOR_PAIR(tc->colorpair), NULL);
1962 err = got_object_id_str(&id_str, id);
1965 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1967 wattr_on(view->window,
1968 COLOR_PAIR(tc->colorpair), NULL);
1969 wprintw(view->window, "%.8s ", id_str);
1971 wattr_off(view->window,
1972 COLOR_PAIR(tc->colorpair), NULL);
1979 author = strdup(got_object_commit_get_author(commit));
1980 if (author == NULL) {
1981 err = got_error_from_errno("strdup");
1984 err = format_author(&wauthor, &author_width, author, avail - col, col);
1987 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1989 wattr_on(view->window,
1990 COLOR_PAIR(tc->colorpair), NULL);
1991 waddwstr(view->window, wauthor);
1993 wattr_off(view->window,
1994 COLOR_PAIR(tc->colorpair), NULL);
1995 col += author_width;
1996 while (col < avail && author_width < author_display_cols + 2) {
1997 waddch(view->window, ' ');
2004 err = got_object_commit_get_logmsg(&logmsg0, commit);
2008 while (*logmsg == '\n')
2010 newline = strchr(logmsg, '\n');
2013 limit = avail - col;
2014 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2015 limit--; /* for the border */
2016 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2020 waddwstr(view->window, &wlogmsg[scrollx]);
2021 col += MAX(logmsg_width, 0);
2022 while (col < avail) {
2023 waddch(view->window, ' ');
2035 static struct commit_queue_entry *
2036 alloc_commit_queue_entry(struct got_commit_object *commit,
2037 struct got_object_id *id)
2039 struct commit_queue_entry *entry;
2041 entry = calloc(1, sizeof(*entry));
2046 entry->commit = commit;
2051 pop_commit(struct commit_queue *commits)
2053 struct commit_queue_entry *entry;
2055 entry = TAILQ_FIRST(&commits->head);
2056 TAILQ_REMOVE(&commits->head, entry, entry);
2057 got_object_commit_close(entry->commit);
2058 commits->ncommits--;
2059 /* Don't free entry->id! It is owned by the commit graph. */
2064 free_commits(struct commit_queue *commits)
2066 while (!TAILQ_EMPTY(&commits->head))
2067 pop_commit(commits);
2070 static const struct got_error *
2071 match_commit(int *have_match, struct got_object_id *id,
2072 struct got_commit_object *commit, regex_t *regex)
2074 const struct got_error *err = NULL;
2075 regmatch_t regmatch;
2076 char *id_str = NULL, *logmsg = NULL;
2080 err = got_object_id_str(&id_str, id);
2084 err = got_object_commit_get_logmsg(&logmsg, commit);
2088 if (regexec(regex, got_object_commit_get_author(commit), 1,
2089 ®match, 0) == 0 ||
2090 regexec(regex, got_object_commit_get_committer(commit), 1,
2091 ®match, 0) == 0 ||
2092 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2093 regexec(regex, logmsg, 1, ®match, 0) == 0)
2101 static const struct got_error *
2102 queue_commits(struct tog_log_thread_args *a)
2104 const struct got_error *err = NULL;
2107 * We keep all commits open throughout the lifetime of the log
2108 * view in order to avoid having to re-fetch commits from disk
2109 * while updating the display.
2112 struct got_object_id *id;
2113 struct got_commit_object *commit;
2114 struct commit_queue_entry *entry;
2117 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2119 if (err || id == NULL)
2122 err = got_object_open_as_commit(&commit, a->repo, id);
2125 entry = alloc_commit_queue_entry(commit, id);
2126 if (entry == NULL) {
2127 err = got_error_from_errno("alloc_commit_queue_entry");
2131 errcode = pthread_mutex_lock(&tog_mutex);
2133 err = got_error_set_errno(errcode,
2134 "pthread_mutex_lock");
2138 entry->idx = a->commits->ncommits;
2139 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2140 a->commits->ncommits++;
2142 if (*a->searching == TOG_SEARCH_FORWARD &&
2143 !*a->search_next_done) {
2145 err = match_commit(&have_match, id, commit, a->regex);
2149 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2152 errcode = pthread_mutex_unlock(&tog_mutex);
2153 if (errcode && err == NULL)
2154 err = got_error_set_errno(errcode,
2155 "pthread_mutex_unlock");
2158 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2164 select_commit(struct tog_log_view_state *s)
2166 struct commit_queue_entry *entry;
2169 entry = s->first_displayed_entry;
2171 if (ncommits == s->selected) {
2172 s->selected_entry = entry;
2175 entry = TAILQ_NEXT(entry, entry);
2180 static const struct got_error *
2181 draw_commits(struct tog_view *view)
2183 const struct got_error *err = NULL;
2184 struct tog_log_view_state *s = &view->state.log;
2185 struct commit_queue_entry *entry = s->selected_entry;
2186 const int limit = view->nlines;
2188 int ncommits, author_cols = 4;
2189 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2190 char *refs_str = NULL;
2192 struct tog_color *tc;
2193 static const size_t date_display_cols = 12;
2195 if (s->selected_entry &&
2196 !(view->searching && view->search_next_done == 0)) {
2197 struct got_reflist_head *refs;
2198 err = got_object_id_str(&id_str, s->selected_entry->id);
2201 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2202 s->selected_entry->id);
2204 err = build_refs_str(&refs_str, refs,
2205 s->selected_entry->id, s->repo);
2211 if (s->thread_args.commits_needed == 0)
2212 halfdelay(10); /* disable fast refresh */
2214 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2215 if (asprintf(&ncommits_str, " [%d/%d] %s",
2216 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2217 (view->searching && !view->search_next_done) ?
2218 "searching..." : "loading...") == -1) {
2219 err = got_error_from_errno("asprintf");
2223 const char *search_str = NULL;
2225 if (view->searching) {
2226 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2227 search_str = "no more matches";
2228 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2229 search_str = "no matches found";
2230 else if (!view->search_next_done)
2231 search_str = "searching...";
2234 if (asprintf(&ncommits_str, " [%d/%d] %s",
2235 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2236 search_str ? search_str :
2237 (refs_str ? refs_str : "")) == -1) {
2238 err = got_error_from_errno("asprintf");
2243 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2244 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2245 "........................................",
2246 s->in_repo_path, ncommits_str) == -1) {
2247 err = got_error_from_errno("asprintf");
2251 } else if (asprintf(&header, "commit %s%s",
2252 id_str ? id_str : "........................................",
2253 ncommits_str) == -1) {
2254 err = got_error_from_errno("asprintf");
2258 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2262 werase(view->window);
2264 if (view_needs_focus_indication(view))
2265 wstandout(view->window);
2266 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2268 wattr_on(view->window,
2269 COLOR_PAIR(tc->colorpair), NULL);
2270 waddwstr(view->window, wline);
2272 wattr_off(view->window,
2273 COLOR_PAIR(tc->colorpair), NULL);
2274 while (width < view->ncols) {
2275 waddch(view->window, ' ');
2278 if (view_needs_focus_indication(view))
2279 wstandend(view->window);
2284 /* Grow author column size if necessary, and set view->maxx. */
2285 entry = s->first_displayed_entry;
2289 char *author, *eol, *msg, *msg0;
2290 wchar_t *wauthor, *wmsg;
2292 if (ncommits >= limit - 1)
2294 author = strdup(got_object_commit_get_author(entry->commit));
2295 if (author == NULL) {
2296 err = got_error_from_errno("strdup");
2299 err = format_author(&wauthor, &width, author, COLS,
2301 if (author_cols < width)
2302 author_cols = width;
2305 err = got_object_commit_get_logmsg(&msg0, entry->commit);
2309 while (*msg == '\n')
2311 if ((eol = strchr(msg, '\n')))
2313 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2314 date_display_cols + author_cols, 0);
2317 view->maxx = MAX(view->maxx, width);
2321 entry = TAILQ_NEXT(entry, entry);
2324 entry = s->first_displayed_entry;
2325 s->last_displayed_entry = s->first_displayed_entry;
2328 if (ncommits >= limit - 1)
2330 if (ncommits == s->selected)
2331 wstandout(view->window);
2332 err = draw_commit(view, entry->commit, entry->id,
2333 date_display_cols, author_cols);
2334 if (ncommits == s->selected)
2335 wstandend(view->window);
2339 s->last_displayed_entry = entry;
2340 entry = TAILQ_NEXT(entry, entry);
2353 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2355 struct commit_queue_entry *entry;
2358 entry = TAILQ_FIRST(&s->commits.head);
2359 if (s->first_displayed_entry == entry)
2362 entry = s->first_displayed_entry;
2363 while (entry && nscrolled < maxscroll) {
2364 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2366 s->first_displayed_entry = entry;
2372 static const struct got_error *
2373 trigger_log_thread(struct tog_view *view, int wait)
2375 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2378 halfdelay(1); /* fast refresh while loading commits */
2380 while (!ta->log_complete && !tog_thread_error &&
2381 (ta->commits_needed > 0 || ta->load_all)) {
2382 /* Wake the log thread. */
2383 errcode = pthread_cond_signal(&ta->need_commits);
2385 return got_error_set_errno(errcode,
2386 "pthread_cond_signal");
2389 * The mutex will be released while the view loop waits
2390 * in wgetch(), at which time the log thread will run.
2395 /* Display progress update in log view. */
2396 show_log_view(view);
2400 /* Wait right here while next commit is being loaded. */
2401 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2403 return got_error_set_errno(errcode,
2404 "pthread_cond_wait");
2406 /* Display progress update in log view. */
2407 show_log_view(view);
2415 static const struct got_error *
2416 request_log_commits(struct tog_view *view)
2418 struct tog_log_view_state *state = &view->state.log;
2419 const struct got_error *err = NULL;
2421 if (state->thread_args.log_complete)
2424 state->thread_args.commits_needed += view->nscrolled;
2425 err = trigger_log_thread(view, 1);
2426 view->nscrolled = 0;
2431 static const struct got_error *
2432 log_scroll_down(struct tog_view *view, int maxscroll)
2434 struct tog_log_view_state *s = &view->state.log;
2435 const struct got_error *err = NULL;
2436 struct commit_queue_entry *pentry;
2437 int nscrolled = 0, ncommits_needed;
2439 if (s->last_displayed_entry == NULL)
2442 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2443 if (s->commits.ncommits < ncommits_needed &&
2444 !s->thread_args.log_complete) {
2446 * Ask the log thread for required amount of commits.
2448 s->thread_args.commits_needed += maxscroll;
2449 err = trigger_log_thread(view, 1);
2455 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2456 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2459 s->last_displayed_entry = pentry ?
2460 pentry : s->last_displayed_entry;;
2462 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2465 s->first_displayed_entry = pentry;
2466 } while (++nscrolled < maxscroll);
2468 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2469 view->nscrolled += nscrolled;
2471 view->nscrolled = 0;
2476 static const struct got_error *
2477 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2478 struct got_commit_object *commit, struct got_object_id *commit_id,
2479 struct tog_view *log_view, struct got_repository *repo)
2481 const struct got_error *err;
2482 struct got_object_qid *parent_id;
2483 struct tog_view *diff_view;
2485 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2486 if (diff_view == NULL)
2487 return got_error_from_errno("view_open");
2489 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2490 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2491 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2493 *new_view = diff_view;
2497 static const struct got_error *
2498 tree_view_visit_subtree(struct tog_tree_view_state *s,
2499 struct got_tree_object *subtree)
2501 struct tog_parent_tree *parent;
2503 parent = calloc(1, sizeof(*parent));
2505 return got_error_from_errno("calloc");
2507 parent->tree = s->tree;
2508 parent->first_displayed_entry = s->first_displayed_entry;
2509 parent->selected_entry = s->selected_entry;
2510 parent->selected = s->selected;
2511 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2514 s->first_displayed_entry = NULL;
2518 static const struct got_error *
2519 tree_view_walk_path(struct tog_tree_view_state *s,
2520 struct got_commit_object *commit, const char *path)
2522 const struct got_error *err = NULL;
2523 struct got_tree_object *tree = NULL;
2525 char *slash, *subpath = NULL;
2527 /* Walk the path and open corresponding tree objects. */
2530 struct got_tree_entry *te;
2531 struct got_object_id *tree_id;
2537 /* Ensure the correct subtree entry is selected. */
2538 slash = strchr(p, '/');
2540 te_name = strdup(p);
2542 te_name = strndup(p, slash - p);
2543 if (te_name == NULL) {
2544 err = got_error_from_errno("strndup");
2547 te = got_object_tree_find_entry(s->tree, te_name);
2549 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2554 s->first_displayed_entry = s->selected_entry = te;
2556 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2557 break; /* jump to this file's entry */
2559 slash = strchr(p, '/');
2561 subpath = strndup(path, slash - path);
2563 subpath = strdup(path);
2564 if (subpath == NULL) {
2565 err = got_error_from_errno("strdup");
2569 err = got_object_id_by_path(&tree_id, s->repo, commit,
2574 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2579 err = tree_view_visit_subtree(s, tree);
2581 got_object_tree_close(tree);
2595 static const struct got_error *
2596 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2597 struct commit_queue_entry *entry, const char *path,
2598 const char *head_ref_name, struct got_repository *repo)
2600 const struct got_error *err = NULL;
2601 struct tog_tree_view_state *s;
2602 struct tog_view *tree_view;
2604 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2605 if (tree_view == NULL)
2606 return got_error_from_errno("view_open");
2608 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2611 s = &tree_view->state.tree;
2613 *new_view = tree_view;
2615 if (got_path_is_root_dir(path))
2618 return tree_view_walk_path(s, entry->commit, path);
2621 static const struct got_error *
2622 block_signals_used_by_main_thread(void)
2627 if (sigemptyset(&sigset) == -1)
2628 return got_error_from_errno("sigemptyset");
2630 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2631 if (sigaddset(&sigset, SIGWINCH) == -1)
2632 return got_error_from_errno("sigaddset");
2633 if (sigaddset(&sigset, SIGCONT) == -1)
2634 return got_error_from_errno("sigaddset");
2635 if (sigaddset(&sigset, SIGINT) == -1)
2636 return got_error_from_errno("sigaddset");
2637 if (sigaddset(&sigset, SIGTERM) == -1)
2638 return got_error_from_errno("sigaddset");
2640 /* ncurses handles SIGTSTP */
2641 if (sigaddset(&sigset, SIGTSTP) == -1)
2642 return got_error_from_errno("sigaddset");
2644 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2646 return got_error_set_errno(errcode, "pthread_sigmask");
2652 log_thread(void *arg)
2654 const struct got_error *err = NULL;
2656 struct tog_log_thread_args *a = arg;
2660 * Sync startup with main thread such that we begin our
2661 * work once view_input() has released the mutex.
2663 errcode = pthread_mutex_lock(&tog_mutex);
2665 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2669 err = block_signals_used_by_main_thread();
2671 pthread_mutex_unlock(&tog_mutex);
2675 while (!done && !err && !tog_fatal_signal_received()) {
2676 errcode = pthread_mutex_unlock(&tog_mutex);
2678 err = got_error_set_errno(errcode,
2679 "pthread_mutex_unlock");
2682 err = queue_commits(a);
2684 if (err->code != GOT_ERR_ITER_COMPLETED)
2688 } else if (a->commits_needed > 0 && !a->load_all)
2689 a->commits_needed--;
2691 errcode = pthread_mutex_lock(&tog_mutex);
2693 err = got_error_set_errno(errcode,
2694 "pthread_mutex_lock");
2696 } else if (*a->quit)
2698 else if (*a->first_displayed_entry == NULL) {
2699 *a->first_displayed_entry =
2700 TAILQ_FIRST(&a->commits->head);
2701 *a->selected_entry = *a->first_displayed_entry;
2704 errcode = pthread_cond_signal(&a->commit_loaded);
2706 err = got_error_set_errno(errcode,
2707 "pthread_cond_signal");
2708 pthread_mutex_unlock(&tog_mutex);
2713 a->commits_needed = 0;
2715 if (a->commits_needed == 0 && !a->load_all) {
2716 errcode = pthread_cond_wait(&a->need_commits,
2719 err = got_error_set_errno(errcode,
2720 "pthread_cond_wait");
2721 pthread_mutex_unlock(&tog_mutex);
2729 a->log_complete = 1;
2730 errcode = pthread_mutex_unlock(&tog_mutex);
2732 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2735 tog_thread_error = 1;
2736 pthread_cond_signal(&a->commit_loaded);
2741 static const struct got_error *
2742 stop_log_thread(struct tog_log_view_state *s)
2744 const struct got_error *err = NULL, *thread_err = NULL;
2749 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2751 return got_error_set_errno(errcode,
2752 "pthread_cond_signal");
2753 errcode = pthread_mutex_unlock(&tog_mutex);
2755 return got_error_set_errno(errcode,
2756 "pthread_mutex_unlock");
2757 errcode = pthread_join(s->thread, (void **)&thread_err);
2759 return got_error_set_errno(errcode, "pthread_join");
2760 errcode = pthread_mutex_lock(&tog_mutex);
2762 return got_error_set_errno(errcode,
2763 "pthread_mutex_lock");
2764 s->thread = 0; //NULL;
2767 if (s->thread_args.repo) {
2768 err = got_repo_close(s->thread_args.repo);
2769 s->thread_args.repo = NULL;
2772 if (s->thread_args.pack_fds) {
2773 const struct got_error *pack_err =
2774 got_repo_pack_fds_close(s->thread_args.pack_fds);
2777 s->thread_args.pack_fds = NULL;
2780 if (s->thread_args.graph) {
2781 got_commit_graph_close(s->thread_args.graph);
2782 s->thread_args.graph = NULL;
2785 return err ? err : thread_err;
2788 static const struct got_error *
2789 close_log_view(struct tog_view *view)
2791 const struct got_error *err = NULL;
2792 struct tog_log_view_state *s = &view->state.log;
2795 err = stop_log_thread(s);
2797 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2798 if (errcode && err == NULL)
2799 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2801 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2802 if (errcode && err == NULL)
2803 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2805 free_commits(&s->commits);
2806 free(s->in_repo_path);
2807 s->in_repo_path = NULL;
2810 free(s->head_ref_name);
2811 s->head_ref_name = NULL;
2815 static const struct got_error *
2816 search_start_log_view(struct tog_view *view)
2818 struct tog_log_view_state *s = &view->state.log;
2820 s->matched_entry = NULL;
2821 s->search_entry = NULL;
2825 static const struct got_error *
2826 search_next_log_view(struct tog_view *view)
2828 const struct got_error *err = NULL;
2829 struct tog_log_view_state *s = &view->state.log;
2830 struct commit_queue_entry *entry;
2832 /* Display progress update in log view. */
2833 show_log_view(view);
2837 if (s->search_entry) {
2839 errcode = pthread_mutex_unlock(&tog_mutex);
2841 return got_error_set_errno(errcode,
2842 "pthread_mutex_unlock");
2843 ch = wgetch(view->window);
2844 errcode = pthread_mutex_lock(&tog_mutex);
2846 return got_error_set_errno(errcode,
2847 "pthread_mutex_lock");
2848 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2849 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2852 if (view->searching == TOG_SEARCH_FORWARD)
2853 entry = TAILQ_NEXT(s->search_entry, entry);
2855 entry = TAILQ_PREV(s->search_entry,
2856 commit_queue_head, entry);
2857 } else if (s->matched_entry) {
2858 int matched_idx = s->matched_entry->idx;
2859 int selected_idx = s->selected_entry->idx;
2862 * If the user has moved the cursor after we hit a match,
2863 * the position from where we should continue searching
2864 * might have changed.
2866 if (view->searching == TOG_SEARCH_FORWARD) {
2867 if (matched_idx > selected_idx)
2868 entry = TAILQ_NEXT(s->selected_entry, entry);
2870 entry = TAILQ_NEXT(s->matched_entry, entry);
2872 if (matched_idx < selected_idx)
2873 entry = TAILQ_PREV(s->selected_entry,
2874 commit_queue_head, entry);
2876 entry = TAILQ_PREV(s->matched_entry,
2877 commit_queue_head, entry);
2880 entry = s->selected_entry;
2886 if (entry == NULL) {
2887 if (s->thread_args.log_complete ||
2888 view->searching == TOG_SEARCH_BACKWARD) {
2889 view->search_next_done =
2890 (s->matched_entry == NULL ?
2891 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2892 s->search_entry = NULL;
2896 * Poke the log thread for more commits and return,
2897 * allowing the main loop to make progress. Search
2898 * will resume at s->search_entry once we come back.
2900 s->thread_args.commits_needed++;
2901 return trigger_log_thread(view, 0);
2904 err = match_commit(&have_match, entry->id, entry->commit,
2909 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2910 s->matched_entry = entry;
2914 s->search_entry = entry;
2915 if (view->searching == TOG_SEARCH_FORWARD)
2916 entry = TAILQ_NEXT(entry, entry);
2918 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2921 if (s->matched_entry) {
2922 int cur = s->selected_entry->idx;
2923 while (cur < s->matched_entry->idx) {
2924 err = input_log_view(NULL, view, KEY_DOWN);
2929 while (cur > s->matched_entry->idx) {
2930 err = input_log_view(NULL, view, KEY_UP);
2937 s->search_entry = NULL;
2942 static const struct got_error *
2943 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2944 struct got_repository *repo, const char *head_ref_name,
2945 const char *in_repo_path, int log_branches)
2947 const struct got_error *err = NULL;
2948 struct tog_log_view_state *s = &view->state.log;
2949 struct got_repository *thread_repo = NULL;
2950 struct got_commit_graph *thread_graph = NULL;
2953 if (in_repo_path != s->in_repo_path) {
2954 free(s->in_repo_path);
2955 s->in_repo_path = strdup(in_repo_path);
2956 if (s->in_repo_path == NULL)
2957 return got_error_from_errno("strdup");
2960 /* The commit queue only contains commits being displayed. */
2961 TAILQ_INIT(&s->commits.head);
2962 s->commits.ncommits = 0;
2965 if (head_ref_name) {
2966 s->head_ref_name = strdup(head_ref_name);
2967 if (s->head_ref_name == NULL) {
2968 err = got_error_from_errno("strdup");
2972 s->start_id = got_object_id_dup(start_id);
2973 if (s->start_id == NULL) {
2974 err = got_error_from_errno("got_object_id_dup");
2977 s->log_branches = log_branches;
2979 STAILQ_INIT(&s->colors);
2980 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2981 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2982 get_color_value("TOG_COLOR_COMMIT"));
2985 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2986 get_color_value("TOG_COLOR_AUTHOR"));
2988 free_colors(&s->colors);
2991 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2992 get_color_value("TOG_COLOR_DATE"));
2994 free_colors(&s->colors);
2999 view->show = show_log_view;
3000 view->input = input_log_view;
3001 view->resize = resize_log_view;
3002 view->close = close_log_view;
3003 view->search_start = search_start_log_view;
3004 view->search_next = search_next_log_view;
3006 if (s->thread_args.pack_fds == NULL) {
3007 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3011 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3012 s->thread_args.pack_fds);
3015 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3019 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3020 s->repo, NULL, NULL);
3024 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3026 err = got_error_set_errno(errcode, "pthread_cond_init");
3029 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3031 err = got_error_set_errno(errcode, "pthread_cond_init");
3035 s->thread_args.commits_needed = view->nlines;
3036 s->thread_args.graph = thread_graph;
3037 s->thread_args.commits = &s->commits;
3038 s->thread_args.in_repo_path = s->in_repo_path;
3039 s->thread_args.start_id = s->start_id;
3040 s->thread_args.repo = thread_repo;
3041 s->thread_args.log_complete = 0;
3042 s->thread_args.quit = &s->quit;
3043 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3044 s->thread_args.selected_entry = &s->selected_entry;
3045 s->thread_args.searching = &view->searching;
3046 s->thread_args.search_next_done = &view->search_next_done;
3047 s->thread_args.regex = &view->regex;
3050 close_log_view(view);
3054 static const struct got_error *
3055 show_log_view(struct tog_view *view)
3057 const struct got_error *err;
3058 struct tog_log_view_state *s = &view->state.log;
3060 if (s->thread == 0) { //NULL) {
3061 int errcode = pthread_create(&s->thread, NULL, log_thread,
3064 return got_error_set_errno(errcode, "pthread_create");
3065 if (s->thread_args.commits_needed > 0) {
3066 err = trigger_log_thread(view, 1);
3072 return draw_commits(view);
3076 log_move_cursor_up(struct tog_view *view, int page, int home)
3078 struct tog_log_view_state *s = &view->state.log;
3080 if (s->selected_entry->idx == 0)
3082 if (s->first_displayed_entry == NULL)
3085 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3087 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3089 if (!page && !home && s->selected > 0)
3092 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3098 static const struct got_error *
3099 log_move_cursor_down(struct tog_view *view, int page)
3101 struct tog_log_view_state *s = &view->state.log;
3102 struct commit_queue_entry *first;
3103 const struct got_error *err = NULL;
3105 first = s->first_displayed_entry;
3106 if (first == NULL) {
3111 if (s->thread_args.log_complete &&
3112 s->selected_entry->idx >= s->commits.ncommits - 1)
3116 int eos = view->nlines - 2;
3118 if (view_is_hsplit_top(view))
3119 --eos; /* border consumes the last line */
3120 if (s->selected < MIN(eos, s->commits.ncommits - 1))
3123 err = log_scroll_down(view, 1);
3124 } else if (s->thread_args.load_all) {
3125 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3126 s->selected += MIN(s->last_displayed_entry->idx -
3127 s->selected_entry->idx, page + 1);
3129 err = log_scroll_down(view, MIN(page,
3130 s->commits.ncommits - s->selected_entry->idx - 1));
3131 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3133 err = log_scroll_down(view, page);
3136 if (first == s->first_displayed_entry && s->selected <
3137 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3138 s->selected = MIN(s->commits.ncommits - 1, page);
3145 * We might necessarily overshoot in horizontal
3146 * splits; if so, select the last displayed commit.
3148 s->selected = MIN(s->selected,
3149 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3153 if (s->thread_args.log_complete &&
3154 s->selected_entry->idx == s->commits.ncommits - 1)
3161 view_get_split(struct tog_view *view, int *y, int *x)
3166 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3167 if (view->child && view->child->resized_y)
3168 *y = view->child->resized_y;
3169 else if (view->resized_y)
3170 *y = view->resized_y;
3172 *y = view_split_begin_y(view->lines);
3173 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3174 if (view->child && view->child->resized_x)
3175 *x = view->child->resized_x;
3176 else if (view->resized_x)
3177 *x = view->resized_x;
3179 *x = view_split_begin_x(view->begin_x);
3183 /* Split view horizontally at y and offset view->state->selected line. */
3184 static const struct got_error *
3185 view_init_hsplit(struct tog_view *view, int y)
3187 const struct got_error *err = NULL;
3191 err = view_resize(view);
3195 err = offset_selection_down(view);
3200 static const struct got_error *
3201 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3203 const struct got_error *err = NULL;
3204 struct tog_log_view_state *s = &view->state.log;
3205 struct tog_view *diff_view = NULL, *tree_view = NULL;
3206 struct tog_view *ref_view = NULL;
3207 struct commit_queue_entry *entry;
3208 int begin_x = 0, begin_y = 0, eos, n, nscroll;
3210 if (s->thread_args.load_all) {
3211 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3212 s->thread_args.load_all = 0;
3213 else if (s->thread_args.log_complete) {
3214 err = log_move_cursor_down(view, s->commits.ncommits);
3215 s->thread_args.load_all = 0;
3220 eos = nscroll = view->nlines - 1;
3221 if (view_is_hsplit_top(view))
3232 view->x = MAX(view->maxx - view->ncols / 2, 0);
3237 if (view->x + view->ncols / 2 < view->maxx)
3238 view->x += 2; /* move two columns right */
3244 view->x -= MIN(view->x, 2); /* move two columns back */
3253 log_move_cursor_up(view, 0, 0);
3257 log_move_cursor_up(view, 0, 1);
3267 log_move_cursor_up(view, nscroll, 0);
3274 err = log_move_cursor_down(view, 0);
3278 /* We don't know yet how many commits, so we're forced to
3279 * traverse them all. */
3281 if (!s->thread_args.log_complete) {
3282 s->thread_args.load_all = 1;
3283 return trigger_log_thread(view, 0);
3287 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3288 for (n = 0; n < eos; n++) {
3291 s->first_displayed_entry = entry;
3292 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3295 s->selected = n - 1;
3307 err = log_move_cursor_down(view, nscroll);
3310 if (s->selected > view->nlines - 2)
3311 s->selected = view->nlines - 2;
3312 if (s->selected > s->commits.ncommits - 1)
3313 s->selected = s->commits.ncommits - 1;
3315 if (s->commits.ncommits < view->nlines - 1 &&
3316 !s->thread_args.log_complete) {
3317 s->thread_args.commits_needed += (view->nlines - 1) -
3318 s->commits.ncommits;
3319 err = trigger_log_thread(view, 1);
3325 if (s->selected_entry == NULL)
3328 /* get dimensions--don't split till initialisation succeeds */
3329 if (view_is_parent_view(view))
3330 view_get_split(view, &begin_y, &begin_x);
3332 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3333 s->selected_entry->commit, s->selected_entry->id,
3338 if (view_is_parent_view(view) &&
3339 view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3340 err = view_init_hsplit(view, begin_y);
3346 diff_view->focussed = 1;
3347 diff_view->mode = view->mode;
3348 diff_view->nlines = view->lines - begin_y;
3350 if (view_is_parent_view(view)) {
3351 view_transfer_size(diff_view, view);
3352 err = view_close_child(view);
3355 err = view_set_child(view, diff_view);
3358 view->focus_child = 1;
3360 *new_view = diff_view;
3364 if (s->selected_entry == NULL)
3366 if (view_is_parent_view(view))
3367 view_get_split(view, &begin_y, &begin_x);
3368 err = browse_commit_tree(&tree_view, begin_y, begin_x,
3369 s->selected_entry, s->in_repo_path, s->head_ref_name,
3373 if (view_is_parent_view(view) &&
3374 view->mode == TOG_VIEW_SPLIT_HRZN) {
3375 err = view_init_hsplit(view, begin_y);
3380 tree_view->focussed = 1;
3381 tree_view->mode = view->mode;
3382 tree_view->nlines = view->lines - begin_y;
3383 if (view_is_parent_view(view)) {
3384 view_transfer_size(tree_view, view);
3385 err = view_close_child(view);
3388 err = view_set_child(view, tree_view);
3391 view->focus_child = 1;
3393 *new_view = tree_view;
3399 if (ch == KEY_BACKSPACE &&
3400 got_path_is_root_dir(s->in_repo_path))
3402 err = stop_log_thread(s);
3405 if (ch == KEY_BACKSPACE) {
3407 err = got_path_dirname(&parent_path, s->in_repo_path);
3410 free(s->in_repo_path);
3411 s->in_repo_path = parent_path;
3412 s->thread_args.in_repo_path = s->in_repo_path;
3413 } else if (ch == CTRL('l')) {
3414 struct got_object_id *start_id;
3415 err = got_repo_match_object_id(&start_id, NULL,
3416 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3417 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3421 s->start_id = start_id;
3422 s->thread_args.start_id = s->start_id;
3424 s->log_branches = !s->log_branches;
3426 if (s->thread_args.pack_fds == NULL) {
3427 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3431 err = got_repo_open(&s->thread_args.repo,
3432 got_repo_get_path(s->repo), NULL,
3433 s->thread_args.pack_fds);
3437 err = tog_load_refs(s->repo, 0);
3440 err = got_commit_graph_open(&s->thread_args.graph,
3441 s->in_repo_path, !s->log_branches);
3444 err = got_commit_graph_iter_start(s->thread_args.graph,
3445 s->start_id, s->repo, NULL, NULL);
3448 free_commits(&s->commits);
3449 s->first_displayed_entry = NULL;
3450 s->last_displayed_entry = NULL;
3451 s->selected_entry = NULL;
3453 s->thread_args.log_complete = 0;
3455 s->thread_args.commits_needed = view->lines;
3456 s->matched_entry = NULL;
3457 s->search_entry = NULL;
3461 if (view_is_parent_view(view))
3462 view_get_split(view, &begin_y, &begin_x);
3463 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3464 if (ref_view == NULL)
3465 return got_error_from_errno("view_open");
3466 err = open_ref_view(ref_view, s->repo);
3468 view_close(ref_view);
3471 if (view_is_parent_view(view) &&
3472 view->mode == TOG_VIEW_SPLIT_HRZN) {
3473 err = view_init_hsplit(view, begin_y);
3478 ref_view->focussed = 1;
3479 ref_view->mode = view->mode;
3480 ref_view->nlines = view->lines - begin_y;
3481 if (view_is_parent_view(view)) {
3482 view_transfer_size(ref_view, view);
3483 err = view_close_child(view);
3486 err = view_set_child(view, ref_view);
3489 view->focus_child = 1;
3491 *new_view = ref_view;
3501 static const struct got_error *
3502 apply_unveil(const char *repo_path, const char *worktree_path)
3504 const struct got_error *error;
3507 if (unveil("gmon.out", "rwc") != 0)
3508 return got_error_from_errno2("unveil", "gmon.out");
3510 if (repo_path && unveil(repo_path, "r") != 0)
3511 return got_error_from_errno2("unveil", repo_path);
3513 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3514 return got_error_from_errno2("unveil", worktree_path);
3516 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3517 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3519 error = got_privsep_unveil_exec_helpers();
3523 if (unveil(NULL, NULL) != 0)
3524 return got_error_from_errno("unveil");
3533 * Override default signal handlers before starting ncurses.
3534 * This should prevent ncurses from installing its own
3535 * broken cleanup() signal handler.
3537 signal(SIGWINCH, tog_sigwinch);
3538 signal(SIGPIPE, tog_sigpipe);
3539 signal(SIGCONT, tog_sigcont);
3540 signal(SIGINT, tog_sigint);
3541 signal(SIGTERM, tog_sigterm);
3545 halfdelay(1); /* Do fast refresh while initial view is loading. */
3548 intrflush(stdscr, FALSE);
3549 keypad(stdscr, TRUE);
3551 if (getenv("TOG_COLORS") != NULL) {
3553 use_default_colors();
3557 static const struct got_error *
3558 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3559 struct got_repository *repo, struct got_worktree *worktree)
3561 const struct got_error *err = NULL;
3564 *in_repo_path = strdup("/");
3565 if (*in_repo_path == NULL)
3566 return got_error_from_errno("strdup");
3571 const char *prefix = got_worktree_get_path_prefix(worktree);
3574 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3577 if (asprintf(in_repo_path, "%s%s%s", prefix,
3578 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3580 err = got_error_from_errno("asprintf");
3581 *in_repo_path = NULL;
3585 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3590 static const struct got_error *
3591 cmd_log(int argc, char *argv[])
3593 const struct got_error *error;
3594 struct got_repository *repo = NULL;
3595 struct got_worktree *worktree = NULL;
3596 struct got_object_id *start_id = NULL;
3597 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3598 char *start_commit = NULL, *label = NULL;
3599 struct got_reference *ref = NULL;
3600 const char *head_ref_name = NULL;
3601 int ch, log_branches = 0;
3602 struct tog_view *view;
3603 int *pack_fds = NULL;
3605 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3611 start_commit = optarg;
3614 repo_path = realpath(optarg, NULL);
3615 if (repo_path == NULL)
3616 return got_error_from_errno2("realpath",
3631 error = got_repo_pack_fds_open(&pack_fds);
3635 if (repo_path == NULL) {
3636 cwd = getcwd(NULL, 0);
3638 return got_error_from_errno("getcwd");
3639 error = got_worktree_open(&worktree, cwd);
3640 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3644 strdup(got_worktree_get_repo_path(worktree));
3646 repo_path = strdup(cwd);
3647 if (repo_path == NULL) {
3648 error = got_error_from_errno("strdup");
3653 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3657 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3664 error = apply_unveil(got_repo_get_path(repo),
3665 worktree ? got_worktree_get_root_path(worktree) : NULL);
3669 /* already loaded by tog_log_with_path()? */
3670 if (TAILQ_EMPTY(&tog_refs)) {
3671 error = tog_load_refs(repo, 0);
3676 if (start_commit == NULL) {
3677 error = got_repo_match_object_id(&start_id, &label,
3678 worktree ? got_worktree_get_head_ref_name(worktree) :
3679 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3682 head_ref_name = label;
3684 error = got_ref_open(&ref, repo, start_commit, 0);
3686 head_ref_name = got_ref_get_name(ref);
3687 else if (error->code != GOT_ERR_NOT_REF)
3689 error = got_repo_match_object_id(&start_id, NULL,
3690 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3695 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3697 error = got_error_from_errno("view_open");
3700 error = open_log_view(view, start_id, repo, head_ref_name,
3701 in_repo_path, log_branches);
3705 /* Release work tree lock. */
3706 got_worktree_close(worktree);
3709 error = view_loop(view);
3719 const struct got_error *close_err = got_repo_close(repo);
3724 got_worktree_close(worktree);
3726 const struct got_error *pack_err =
3727 got_repo_pack_fds_close(pack_fds);
3739 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3740 "[-w] object1 object2\n", getprogname());
3745 match_line(const char *line, regex_t *regex, size_t nmatch,
3746 regmatch_t *regmatch)
3748 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3751 static struct tog_color *
3752 match_color(struct tog_colors *colors, const char *line)
3754 struct tog_color *tc = NULL;
3756 STAILQ_FOREACH(tc, colors, entry) {
3757 if (match_line(line, &tc->regex, 0, NULL))
3764 static const struct got_error *
3765 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3766 WINDOW *window, int skipcol, regmatch_t *regmatch)
3768 const struct got_error *err = NULL;
3770 wchar_t *wline = NULL;
3771 int rme, rms, n, width, scrollx;
3772 int width0 = 0, width1 = 0, width2 = 0;
3773 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3777 rms = regmatch->rm_so;
3778 rme = regmatch->rm_eo;
3780 err = expand_tab(&exstr, line);
3784 /* Split the line into 3 segments, according to match offsets. */
3785 seg0 = strndup(exstr, rms);
3787 err = got_error_from_errno("strndup");
3790 seg1 = strndup(exstr + rms, rme - rms);
3792 err = got_error_from_errno("strndup");
3795 seg2 = strdup(exstr + rme);
3797 err = got_error_from_errno("strndup");
3801 /* draw up to matched token if we haven't scrolled past it */
3802 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3806 n = MAX(width0 - skipcol, 0);
3809 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3810 wlimit, col_tab_align, 1);
3813 waddwstr(window, &wline[scrollx]);
3823 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3827 wlen = wcslen(wline);
3829 width = wcwidth(wline[i]);
3831 /* should not happen, tabs are expanded */
3832 err = got_error(GOT_ERR_RANGE);
3835 if (width0 + w + width > skipcol)
3840 /* draw (visible part of) matched token (if scrolled into it) */
3841 if (width1 - w > 0) {
3842 wattron(window, A_STANDOUT);
3843 waddwstr(window, &wline[i]);
3844 wattroff(window, A_STANDOUT);
3845 wlimit -= (width1 - w);
3846 *wtotal += (width1 - w);
3850 if (wlimit > 0) { /* draw rest of line */
3852 if (skipcol > width0 + width1) {
3853 err = format_line(&wline, &width2, &scrollx, seg2,
3854 skipcol - (width0 + width1), wlimit,
3858 waddwstr(window, &wline[scrollx]);
3860 err = format_line(&wline, &width2, NULL, seg2, 0,
3861 wlimit, col_tab_align, 1);
3864 waddwstr(window, wline);
3877 static const struct got_error *
3878 draw_file(struct tog_view *view, const char *header)
3880 struct tog_diff_view_state *s = &view->state.diff;
3881 regmatch_t *regmatch = &view->regmatch;
3882 const struct got_error *err;
3885 size_t linesize = 0;
3887 struct tog_color *tc;
3890 int max_lines = view->nlines;
3891 int nlines = s->nlines;
3894 line_offset = s->line_offsets[s->first_displayed_line - 1];
3895 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3896 return got_error_from_errno("fseek");
3898 werase(view->window);
3901 if (asprintf(&line, "[%d/%d] %s",
3902 s->first_displayed_line - 1 + s->selected_line, nlines,
3904 return got_error_from_errno("asprintf");
3905 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3911 if (view_needs_focus_indication(view))
3912 wstandout(view->window);
3913 waddwstr(view->window, wline);
3916 if (view_needs_focus_indication(view))
3917 wstandend(view->window);
3918 if (width <= view->ncols - 1)
3919 waddch(view->window, '\n');
3929 while (max_lines > 0 && nprinted < max_lines) {
3930 linelen = getline(&line, &linesize, s->f);
3931 if (linelen == -1) {
3937 return got_ferror(s->f, GOT_ERR_IO);
3940 /* Set view->maxx based on full line length. */
3941 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3947 view->maxx = MAX(view->maxx, width);
3951 tc = match_color(&s->colors, line);
3953 wattr_on(view->window,
3954 COLOR_PAIR(tc->colorpair), NULL);
3955 if (s->first_displayed_line + nprinted == s->matched_line &&
3956 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3957 err = add_matched_line(&width, line, view->ncols, 0,
3958 view->window, view->x, regmatch);
3965 err = format_line(&wline, &width, &skip, line,
3966 view->x, view->ncols, 0, view->x ? 1 : 0);
3971 waddwstr(view->window, &wline[skip]);
3976 wattr_off(view->window,
3977 COLOR_PAIR(tc->colorpair), NULL);
3978 if (width <= view->ncols - 1)
3979 waddch(view->window, '\n');
3984 s->last_displayed_line = s->first_displayed_line +
3987 s->last_displayed_line = s->first_displayed_line;
3992 while (nprinted < view->nlines) {
3993 waddch(view->window, '\n');
3997 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4003 wstandout(view->window);
4004 waddwstr(view->window, wline);
4007 wstandend(view->window);
4014 get_datestr(time_t *time, char *datebuf)
4016 struct tm mytm, *tm;
4019 tm = gmtime_r(time, &mytm);
4022 s = asctime_r(tm, datebuf);
4025 p = strchr(s, '\n');
4031 static const struct got_error *
4032 get_changed_paths(struct got_pathlist_head *paths,
4033 struct got_commit_object *commit, struct got_repository *repo)
4035 const struct got_error *err = NULL;
4036 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4037 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4038 struct got_object_qid *qid;
4040 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4042 struct got_commit_object *pcommit;
4043 err = got_object_open_as_commit(&pcommit, repo,
4048 tree_id1 = got_object_id_dup(
4049 got_object_commit_get_tree_id(pcommit));
4050 if (tree_id1 == NULL) {
4051 got_object_commit_close(pcommit);
4052 return got_error_from_errno("got_object_id_dup");
4054 got_object_commit_close(pcommit);
4059 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4064 tree_id2 = got_object_commit_get_tree_id(commit);
4065 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4069 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4070 got_diff_tree_collect_changed_paths, paths, 0);
4073 got_object_tree_close(tree1);
4075 got_object_tree_close(tree2);
4080 static const struct got_error *
4081 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4085 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4087 return got_error_from_errno("reallocarray");
4089 (*line_offsets)[*nlines] = off;
4094 static const struct got_error *
4095 write_commit_info(off_t **line_offsets, size_t *nlines,
4096 struct got_object_id *commit_id, struct got_reflist_head *refs,
4097 struct got_repository *repo, FILE *outfile)
4099 const struct got_error *err = NULL;
4100 char datebuf[26], *datestr;
4101 struct got_commit_object *commit;
4102 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4103 time_t committer_time;
4104 const char *author, *committer;
4105 char *refs_str = NULL;
4106 struct got_pathlist_head changed_paths;
4107 struct got_pathlist_entry *pe;
4111 TAILQ_INIT(&changed_paths);
4114 err = build_refs_str(&refs_str, refs, commit_id, repo);
4119 err = got_object_open_as_commit(&commit, repo, commit_id);
4123 err = got_object_id_str(&id_str, commit_id);
4125 err = got_error_from_errno("got_object_id_str");
4129 err = add_line_offset(line_offsets, nlines, 0);
4133 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4134 refs_str ? refs_str : "", refs_str ? ")" : "");
4136 err = got_error_from_errno("fprintf");
4140 err = add_line_offset(line_offsets, nlines, outoff);
4144 n = fprintf(outfile, "from: %s\n",
4145 got_object_commit_get_author(commit));
4147 err = got_error_from_errno("fprintf");
4151 err = add_line_offset(line_offsets, nlines, outoff);
4155 committer_time = got_object_commit_get_committer_time(commit);
4156 datestr = get_datestr(&committer_time, datebuf);
4158 n = fprintf(outfile, "date: %s UTC\n", datestr);
4160 err = got_error_from_errno("fprintf");
4164 err = add_line_offset(line_offsets, nlines, outoff);
4168 author = got_object_commit_get_author(commit);
4169 committer = got_object_commit_get_committer(commit);
4170 if (strcmp(author, committer) != 0) {
4171 n = fprintf(outfile, "via: %s\n", committer);
4173 err = got_error_from_errno("fprintf");
4177 err = add_line_offset(line_offsets, nlines, outoff);
4181 if (got_object_commit_get_nparents(commit) > 1) {
4182 const struct got_object_id_queue *parent_ids;
4183 struct got_object_qid *qid;
4185 parent_ids = got_object_commit_get_parent_ids(commit);
4186 STAILQ_FOREACH(qid, parent_ids, entry) {
4187 err = got_object_id_str(&id_str, &qid->id);
4190 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4192 err = got_error_from_errno("fprintf");
4196 err = add_line_offset(line_offsets, nlines, outoff);
4204 err = got_object_commit_get_logmsg(&logmsg, commit);
4208 while ((line = strsep(&s, "\n")) != NULL) {
4209 n = fprintf(outfile, "%s\n", line);
4211 err = got_error_from_errno("fprintf");
4215 err = add_line_offset(line_offsets, nlines, outoff);
4220 err = get_changed_paths(&changed_paths, commit, repo);
4223 TAILQ_FOREACH(pe, &changed_paths, entry) {
4224 struct got_diff_changed_path *cp = pe->data;
4225 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4227 err = got_error_from_errno("fprintf");
4231 err = add_line_offset(line_offsets, nlines, outoff);
4234 free((char *)pe->path);
4238 fputc('\n', outfile);
4240 err = add_line_offset(line_offsets, nlines, outoff);
4242 got_pathlist_free(&changed_paths);
4246 got_object_commit_close(commit);
4248 free(*line_offsets);
4249 *line_offsets = NULL;
4255 static const struct got_error *
4256 create_diff(struct tog_diff_view_state *s)
4258 const struct got_error *err = NULL;
4262 free(s->line_offsets);
4263 s->line_offsets = malloc(sizeof(off_t));
4264 if (s->line_offsets == NULL)
4265 return got_error_from_errno("malloc");
4270 err = got_error_from_errno("got_opentemp");
4273 if (s->f && fclose(s->f) == EOF) {
4274 err = got_error_from_errno("fclose");
4280 err = got_object_get_type(&obj_type, s->repo, s->id1);
4282 err = got_object_get_type(&obj_type, s->repo, s->id2);
4287 case GOT_OBJ_TYPE_BLOB:
4288 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4289 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4290 s->label1, s->label2, tog_diff_algo, s->diff_context,
4291 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4293 case GOT_OBJ_TYPE_TREE:
4294 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4295 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4296 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4297 s->force_text_diff, s->repo, s->f);
4299 case GOT_OBJ_TYPE_COMMIT: {
4300 const struct got_object_id_queue *parent_ids;
4301 struct got_object_qid *pid;
4302 struct got_commit_object *commit2;
4303 struct got_reflist_head *refs;
4305 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4308 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4309 /* Show commit info if we're diffing to a parent/root commit. */
4310 if (s->id1 == NULL) {
4311 err = write_commit_info(&s->line_offsets, &s->nlines,
4312 s->id2, refs, s->repo, s->f);
4316 parent_ids = got_object_commit_get_parent_ids(commit2);
4317 STAILQ_FOREACH(pid, parent_ids, entry) {
4318 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4319 err = write_commit_info(
4320 &s->line_offsets, &s->nlines,
4321 s->id2, refs, s->repo, s->f);
4328 got_object_commit_close(commit2);
4330 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4331 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4332 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4333 s->force_text_diff, s->repo, s->f);
4337 err = got_error(GOT_ERR_OBJ_TYPE);
4343 if (s->f && fflush(s->f) != 0 && err == NULL)
4344 err = got_error_from_errno("fflush");
4349 diff_view_indicate_progress(struct tog_view *view)
4351 mvwaddstr(view->window, 0, 0, "diffing...");
4356 static const struct got_error *
4357 search_start_diff_view(struct tog_view *view)
4359 struct tog_diff_view_state *s = &view->state.diff;
4361 s->matched_line = 0;
4365 static const struct got_error *
4366 search_next_diff_view(struct tog_view *view)
4368 struct tog_diff_view_state *s = &view->state.diff;
4369 const struct got_error *err = NULL;
4372 size_t linesize = 0;
4375 if (!view->searching) {
4376 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4380 if (s->matched_line) {
4381 if (view->searching == TOG_SEARCH_FORWARD)
4382 lineno = s->matched_line + 1;
4384 lineno = s->matched_line - 1;
4386 lineno = s->first_displayed_line;
4391 if (lineno <= 0 || lineno > s->nlines) {
4392 if (s->matched_line == 0) {
4393 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4397 if (view->searching == TOG_SEARCH_FORWARD)
4403 offset = s->line_offsets[lineno - 1];
4404 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4406 return got_error_from_errno("fseeko");
4408 linelen = getline(&line, &linesize, s->f);
4409 if (linelen != -1) {
4411 err = expand_tab(&exstr, line);
4414 if (match_line(exstr, &view->regex, 1,
4416 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4417 s->matched_line = lineno;
4423 if (view->searching == TOG_SEARCH_FORWARD)
4430 if (s->matched_line) {
4431 s->first_displayed_line = s->matched_line;
4432 s->selected_line = 1;
4438 static const struct got_error *
4439 close_diff_view(struct tog_view *view)
4441 const struct got_error *err = NULL;
4442 struct tog_diff_view_state *s = &view->state.diff;
4448 if (s->f && fclose(s->f) == EOF)
4449 err = got_error_from_errno("fclose");
4451 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4452 err = got_error_from_errno("fclose");
4454 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4455 err = got_error_from_errno("fclose");
4457 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4458 err = got_error_from_errno("close");
4460 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4461 err = got_error_from_errno("close");
4463 free_colors(&s->colors);
4464 free(s->line_offsets);
4465 s->line_offsets = NULL;
4470 static const struct got_error *
4471 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4472 struct got_object_id *id2, const char *label1, const char *label2,
4473 int diff_context, int ignore_whitespace, int force_text_diff,
4474 struct tog_view *parent_view, struct got_repository *repo)
4476 const struct got_error *err;
4477 struct tog_diff_view_state *s = &view->state.diff;
4479 memset(s, 0, sizeof(*s));
4483 if (id1 != NULL && id2 != NULL) {
4485 err = got_object_get_type(&type1, repo, id1);
4488 err = got_object_get_type(&type2, repo, id2);
4493 return got_error(GOT_ERR_OBJ_TYPE);
4495 s->first_displayed_line = 1;
4496 s->last_displayed_line = view->nlines;
4497 s->selected_line = 1;
4505 s->id1 = got_object_id_dup(id1);
4507 return got_error_from_errno("got_object_id_dup");
4511 s->id2 = got_object_id_dup(id2);
4512 if (s->id2 == NULL) {
4513 err = got_error_from_errno("got_object_id_dup");
4517 s->f1 = got_opentemp();
4518 if (s->f1 == NULL) {
4519 err = got_error_from_errno("got_opentemp");
4523 s->f2 = got_opentemp();
4524 if (s->f2 == NULL) {
4525 err = got_error_from_errno("got_opentemp");
4529 s->fd1 = got_opentempfd();
4531 err = got_error_from_errno("got_opentempfd");
4535 s->fd2 = got_opentempfd();
4537 err = got_error_from_errno("got_opentempfd");
4541 s->first_displayed_line = 1;
4542 s->last_displayed_line = view->nlines;
4543 s->diff_context = diff_context;
4544 s->ignore_whitespace = ignore_whitespace;
4545 s->force_text_diff = force_text_diff;
4546 s->parent_view = parent_view;
4549 STAILQ_INIT(&s->colors);
4550 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4551 err = add_color(&s->colors,
4552 "^-", TOG_COLOR_DIFF_MINUS,
4553 get_color_value("TOG_COLOR_DIFF_MINUS"));
4556 err = add_color(&s->colors, "^\\+",
4557 TOG_COLOR_DIFF_PLUS,
4558 get_color_value("TOG_COLOR_DIFF_PLUS"));
4561 err = add_color(&s->colors,
4562 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4563 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4567 err = add_color(&s->colors,
4568 "^(commit [0-9a-f]|parent [0-9]|"
4569 "(blob|file|tree|commit) [-+] |"
4570 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4571 get_color_value("TOG_COLOR_DIFF_META"));
4575 err = add_color(&s->colors,
4576 "^(from|via): ", TOG_COLOR_AUTHOR,
4577 get_color_value("TOG_COLOR_AUTHOR"));
4581 err = add_color(&s->colors,
4582 "^date: ", TOG_COLOR_DATE,
4583 get_color_value("TOG_COLOR_DATE"));
4588 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4589 view_is_splitscreen(view))
4590 show_log_view(parent_view); /* draw border */
4591 diff_view_indicate_progress(view);
4593 err = create_diff(s);
4595 view->show = show_diff_view;
4596 view->input = input_diff_view;
4597 view->reset = reset_diff_view;
4598 view->close = close_diff_view;
4599 view->search_start = search_start_diff_view;
4600 view->search_next = search_next_diff_view;
4603 close_diff_view(view);
4607 static const struct got_error *
4608 show_diff_view(struct tog_view *view)
4610 const struct got_error *err;
4611 struct tog_diff_view_state *s = &view->state.diff;
4612 char *id_str1 = NULL, *id_str2, *header;
4613 const char *label1, *label2;
4616 err = got_object_id_str(&id_str1, s->id1);
4619 label1 = s->label1 ? : id_str1;
4621 label1 = "/dev/null";
4623 err = got_object_id_str(&id_str2, s->id2);
4626 label2 = s->label2 ? : id_str2;
4628 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4629 err = got_error_from_errno("asprintf");
4637 err = draw_file(view, header);
4642 static const struct got_error *
4643 set_selected_commit(struct tog_diff_view_state *s,
4644 struct commit_queue_entry *entry)
4646 const struct got_error *err;
4647 const struct got_object_id_queue *parent_ids;
4648 struct got_commit_object *selected_commit;
4649 struct got_object_qid *pid;
4652 s->id2 = got_object_id_dup(entry->id);
4654 return got_error_from_errno("got_object_id_dup");
4656 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4659 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4661 pid = STAILQ_FIRST(parent_ids);
4662 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4663 got_object_commit_close(selected_commit);
4667 static const struct got_error *
4668 reset_diff_view(struct tog_view *view)
4670 struct tog_diff_view_state *s = &view->state.diff;
4673 wclear(view->window);
4674 s->first_displayed_line = 1;
4675 s->last_displayed_line = view->nlines;
4676 s->matched_line = 0;
4677 diff_view_indicate_progress(view);
4678 return create_diff(s);
4681 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4683 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4686 static const struct got_error *
4687 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4689 const struct got_error *err = NULL;
4690 struct tog_diff_view_state *s = &view->state.diff;
4691 struct tog_log_view_state *ls;
4692 struct commit_queue_entry *old_selected_entry;
4694 size_t linesize = 0;
4696 int i, nscroll = view->nlines - 1, up = 0;
4703 view->x = MAX(view->maxx - view->ncols / 3, 0);
4708 if (view->x + view->ncols / 3 < view->maxx)
4709 view->x += 2; /* move two columns right */
4715 view->x -= MIN(view->x, 2); /* move two columns back */
4722 s->force_text_diff = !s->force_text_diff;
4724 s->ignore_whitespace = !s->ignore_whitespace;
4725 err = reset_diff_view(view);
4729 s->first_displayed_line = 1;
4738 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4744 if (s->first_displayed_line > 1)
4745 s->first_displayed_line--;
4756 if (s->first_displayed_line == 1) {
4761 while (i++ < nscroll && s->first_displayed_line > 1)
4762 s->first_displayed_line--;
4768 s->first_displayed_line++;
4785 while (!s->eof && i++ < nscroll) {
4786 linelen = getline(&line, &linesize, s->f);
4787 s->first_displayed_line++;
4788 if (linelen == -1) {
4792 err = got_ferror(s->f, GOT_ERR_IO);
4799 if (s->diff_context > 0) {
4801 s->matched_line = 0;
4802 diff_view_indicate_progress(view);
4803 err = create_diff(s);
4804 if (s->first_displayed_line + view->nlines - 1 >
4806 s->first_displayed_line = 1;
4807 s->last_displayed_line = view->nlines;
4813 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4815 s->matched_line = 0;
4816 diff_view_indicate_progress(view);
4817 err = create_diff(s);
4827 if (s->parent_view == NULL) {
4831 s->parent_view->count = view->count;
4833 if (s->parent_view->type == TOG_VIEW_LOG) {
4834 ls = &s->parent_view->state.log;
4835 old_selected_entry = ls->selected_entry;
4837 err = input_log_view(NULL, s->parent_view,
4838 up ? KEY_UP : KEY_DOWN);
4841 view->count = s->parent_view->count;
4843 if (old_selected_entry == ls->selected_entry)
4846 err = set_selected_commit(s, ls->selected_entry);
4849 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4850 struct tog_blame_view_state *bs;
4851 struct got_object_id *id, *prev_id;
4853 bs = &s->parent_view->state.blame;
4854 prev_id = get_annotation_for_line(bs->blame.lines,
4855 bs->blame.nlines, bs->last_diffed_line);
4857 err = input_blame_view(&view, s->parent_view,
4858 up ? KEY_UP : KEY_DOWN);
4861 view->count = s->parent_view->count;
4863 if (prev_id == NULL)
4865 id = get_selected_commit_id(bs->blame.lines,
4866 bs->blame.nlines, bs->first_displayed_line,
4871 if (!got_object_id_cmp(prev_id, id))
4874 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4878 s->first_displayed_line = 1;
4879 s->last_displayed_line = view->nlines;
4880 s->matched_line = 0;
4883 diff_view_indicate_progress(view);
4884 err = create_diff(s);
4894 static const struct got_error *
4895 cmd_diff(int argc, char *argv[])
4897 const struct got_error *error = NULL;
4898 struct got_repository *repo = NULL;
4899 struct got_worktree *worktree = NULL;
4900 struct got_object_id *id1 = NULL, *id2 = NULL;
4901 char *repo_path = NULL, *cwd = NULL;
4902 char *id_str1 = NULL, *id_str2 = NULL;
4903 char *label1 = NULL, *label2 = NULL;
4904 int diff_context = 3, ignore_whitespace = 0;
4905 int ch, force_text_diff = 0;
4907 struct tog_view *view;
4908 int *pack_fds = NULL;
4910 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4913 force_text_diff = 1;
4916 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4919 errx(1, "number of context lines is %s: %s",
4923 repo_path = realpath(optarg, NULL);
4924 if (repo_path == NULL)
4925 return got_error_from_errno2("realpath",
4927 got_path_strip_trailing_slashes(repo_path);
4930 ignore_whitespace = 1;
4942 usage_diff(); /* TODO show local worktree changes */
4943 } else if (argc == 2) {
4949 error = got_repo_pack_fds_open(&pack_fds);
4953 if (repo_path == NULL) {
4954 cwd = getcwd(NULL, 0);
4956 return got_error_from_errno("getcwd");
4957 error = got_worktree_open(&worktree, cwd);
4958 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4962 strdup(got_worktree_get_repo_path(worktree));
4964 repo_path = strdup(cwd);
4965 if (repo_path == NULL) {
4966 error = got_error_from_errno("strdup");
4971 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4977 error = apply_unveil(got_repo_get_path(repo), NULL);
4981 error = tog_load_refs(repo, 0);
4985 error = got_repo_match_object_id(&id1, &label1, id_str1,
4986 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4990 error = got_repo_match_object_id(&id2, &label2, id_str2,
4991 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4995 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4997 error = got_error_from_errno("view_open");
5000 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5001 ignore_whitespace, force_text_diff, NULL, repo);
5004 error = view_loop(view);
5011 const struct got_error *close_err = got_repo_close(repo);
5016 got_worktree_close(worktree);
5018 const struct got_error *pack_err =
5019 got_repo_pack_fds_close(pack_fds);
5032 "usage: %s blame [-c commit] [-r repository-path] path\n",
5037 struct tog_blame_line {
5039 struct got_object_id *id;
5042 static const struct got_error *
5043 draw_blame(struct tog_view *view)
5045 struct tog_blame_view_state *s = &view->state.blame;
5046 struct tog_blame *blame = &s->blame;
5047 regmatch_t *regmatch = &view->regmatch;
5048 const struct got_error *err;
5049 int lineno = 0, nprinted = 0;
5051 size_t linesize = 0;
5055 struct tog_blame_line *blame_line;
5056 struct got_object_id *prev_id = NULL;
5058 struct tog_color *tc;
5060 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5065 werase(view->window);
5067 if (asprintf(&line, "commit %s", id_str) == -1) {
5068 err = got_error_from_errno("asprintf");
5073 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5078 if (view_needs_focus_indication(view))
5079 wstandout(view->window);
5080 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5082 wattr_on(view->window,
5083 COLOR_PAIR(tc->colorpair), NULL);
5084 waddwstr(view->window, wline);
5086 wattr_off(view->window,
5087 COLOR_PAIR(tc->colorpair), NULL);
5088 if (view_needs_focus_indication(view))
5089 wstandend(view->window);
5092 if (width < view->ncols - 1)
5093 waddch(view->window, '\n');
5095 if (asprintf(&line, "[%d/%d] %s%s",
5096 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5097 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5099 return got_error_from_errno("asprintf");
5102 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5107 waddwstr(view->window, wline);
5110 if (width < view->ncols - 1)
5111 waddch(view->window, '\n');
5115 while (nprinted < view->nlines - 2) {
5116 linelen = getline(&line, &linesize, blame->f);
5117 if (linelen == -1) {
5118 if (feof(blame->f)) {
5123 return got_ferror(blame->f, GOT_ERR_IO);
5125 if (++lineno < s->first_displayed_line)
5128 /* Set view->maxx based on full line length. */
5129 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5136 view->maxx = MAX(view->maxx, width);
5138 if (nprinted == s->selected_line - 1)
5139 wstandout(view->window);
5141 if (blame->nlines > 0) {
5142 blame_line = &blame->lines[lineno - 1];
5143 if (blame_line->annotated && prev_id &&
5144 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5145 !(nprinted == s->selected_line - 1)) {
5146 waddstr(view->window, " ");
5147 } else if (blame_line->annotated) {
5149 err = got_object_id_str(&id_str,
5155 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5157 wattr_on(view->window,
5158 COLOR_PAIR(tc->colorpair), NULL);
5159 wprintw(view->window, "%.8s", id_str);
5161 wattr_off(view->window,
5162 COLOR_PAIR(tc->colorpair), NULL);
5164 prev_id = blame_line->id;
5166 waddstr(view->window, "........");
5170 waddstr(view->window, "........");
5174 if (nprinted == s->selected_line - 1)
5175 wstandend(view->window);
5176 waddstr(view->window, " ");
5178 if (view->ncols <= 9) {
5180 } else if (s->first_displayed_line + nprinted ==
5182 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5183 err = add_matched_line(&width, line, view->ncols - 9, 9,
5184 view->window, view->x, regmatch);
5192 err = format_line(&wline, &width, &skip, line,
5193 view->x, view->ncols - 9, 9, 1);
5198 waddwstr(view->window, &wline[skip]);
5204 if (width <= view->ncols - 1)
5205 waddch(view->window, '\n');
5206 if (++nprinted == 1)
5207 s->first_displayed_line = lineno;
5210 s->last_displayed_line = lineno;
5217 static const struct got_error *
5218 blame_cb(void *arg, int nlines, int lineno,
5219 struct got_commit_object *commit, struct got_object_id *id)
5221 const struct got_error *err = NULL;
5222 struct tog_blame_cb_args *a = arg;
5223 struct tog_blame_line *line;
5226 if (nlines != a->nlines ||
5227 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5228 return got_error(GOT_ERR_RANGE);
5230 errcode = pthread_mutex_lock(&tog_mutex);
5232 return got_error_set_errno(errcode, "pthread_mutex_lock");
5234 if (*a->quit) { /* user has quit the blame view */
5235 err = got_error(GOT_ERR_ITER_COMPLETED);
5240 goto done; /* no change in this commit */
5242 line = &a->lines[lineno - 1];
5243 if (line->annotated)
5246 line->id = got_object_id_dup(id);
5247 if (line->id == NULL) {
5248 err = got_error_from_errno("got_object_id_dup");
5251 line->annotated = 1;
5253 errcode = pthread_mutex_unlock(&tog_mutex);
5255 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5260 blame_thread(void *arg)
5262 const struct got_error *err, *close_err;
5263 struct tog_blame_thread_args *ta = arg;
5264 struct tog_blame_cb_args *a = ta->cb_args;
5265 int errcode, fd1 = -1, fd2 = -1;
5266 FILE *f1 = NULL, *f2 = NULL;
5268 fd1 = got_opentempfd();
5270 return (void *)got_error_from_errno("got_opentempfd");
5272 fd2 = got_opentempfd();
5274 err = got_error_from_errno("got_opentempfd");
5278 f1 = got_opentemp();
5280 err = (void *)got_error_from_errno("got_opentemp");
5283 f2 = got_opentemp();
5285 err = (void *)got_error_from_errno("got_opentemp");
5289 err = block_signals_used_by_main_thread();
5293 err = got_blame(ta->path, a->commit_id, ta->repo,
5294 tog_diff_algo, blame_cb, ta->cb_args,
5295 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5296 if (err && err->code == GOT_ERR_CANCELLED)
5299 errcode = pthread_mutex_lock(&tog_mutex);
5301 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5305 close_err = got_repo_close(ta->repo);
5311 errcode = pthread_mutex_unlock(&tog_mutex);
5312 if (errcode && err == NULL)
5313 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5316 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5317 err = got_error_from_errno("close");
5318 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5319 err = got_error_from_errno("close");
5320 if (f1 && fclose(f1) == EOF && err == NULL)
5321 err = got_error_from_errno("fclose");
5322 if (f2 && fclose(f2) == EOF && err == NULL)
5323 err = got_error_from_errno("fclose");
5328 static struct got_object_id *
5329 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5330 int first_displayed_line, int selected_line)
5332 struct tog_blame_line *line;
5337 line = &lines[first_displayed_line - 1 + selected_line - 1];
5338 if (!line->annotated)
5344 static struct got_object_id *
5345 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5348 struct tog_blame_line *line;
5350 if (nlines <= 0 || lineno >= nlines)
5353 line = &lines[lineno - 1];
5354 if (!line->annotated)
5360 static const struct got_error *
5361 stop_blame(struct tog_blame *blame)
5363 const struct got_error *err = NULL;
5366 if (blame->thread) {
5368 errcode = pthread_mutex_unlock(&tog_mutex);
5370 return got_error_set_errno(errcode,
5371 "pthread_mutex_unlock");
5372 errcode = pthread_join(blame->thread, (void **)&err);
5374 return got_error_set_errno(errcode, "pthread_join");
5375 errcode = pthread_mutex_lock(&tog_mutex);
5377 return got_error_set_errno(errcode,
5378 "pthread_mutex_lock");
5379 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5381 blame->thread = 0; //NULL;
5383 if (blame->thread_args.repo) {
5384 const struct got_error *close_err;
5385 close_err = got_repo_close(blame->thread_args.repo);
5388 blame->thread_args.repo = NULL;
5391 if (fclose(blame->f) == EOF && err == NULL)
5392 err = got_error_from_errno("fclose");
5396 for (i = 0; i < blame->nlines; i++)
5397 free(blame->lines[i].id);
5399 blame->lines = NULL;
5401 free(blame->cb_args.commit_id);
5402 blame->cb_args.commit_id = NULL;
5403 if (blame->pack_fds) {
5404 const struct got_error *pack_err =
5405 got_repo_pack_fds_close(blame->pack_fds);
5408 blame->pack_fds = NULL;
5413 static const struct got_error *
5414 cancel_blame_view(void *arg)
5416 const struct got_error *err = NULL;
5420 errcode = pthread_mutex_lock(&tog_mutex);
5422 return got_error_set_errno(errcode,
5423 "pthread_mutex_unlock");
5426 err = got_error(GOT_ERR_CANCELLED);
5428 errcode = pthread_mutex_unlock(&tog_mutex);
5430 return got_error_set_errno(errcode,
5431 "pthread_mutex_lock");
5436 static const struct got_error *
5437 run_blame(struct tog_view *view)
5439 struct tog_blame_view_state *s = &view->state.blame;
5440 struct tog_blame *blame = &s->blame;
5441 const struct got_error *err = NULL;
5442 struct got_commit_object *commit = NULL;
5443 struct got_blob_object *blob = NULL;
5444 struct got_repository *thread_repo = NULL;
5445 struct got_object_id *obj_id = NULL;
5446 int obj_type, fd = -1;
5447 int *pack_fds = NULL;
5449 err = got_object_open_as_commit(&commit, s->repo,
5450 &s->blamed_commit->id);
5454 fd = got_opentempfd();
5456 err = got_error_from_errno("got_opentempfd");
5460 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5464 err = got_object_get_type(&obj_type, s->repo, obj_id);
5468 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5469 err = got_error(GOT_ERR_OBJ_TYPE);
5473 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5476 blame->f = got_opentemp();
5477 if (blame->f == NULL) {
5478 err = got_error_from_errno("got_opentemp");
5481 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5482 &blame->line_offsets, blame->f, blob);
5485 if (blame->nlines == 0) {
5486 s->blame_complete = 1;
5490 /* Don't include \n at EOF in the blame line count. */
5491 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5494 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5495 if (blame->lines == NULL) {
5496 err = got_error_from_errno("calloc");
5500 err = got_repo_pack_fds_open(&pack_fds);
5503 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5508 blame->pack_fds = pack_fds;
5509 blame->cb_args.view = view;
5510 blame->cb_args.lines = blame->lines;
5511 blame->cb_args.nlines = blame->nlines;
5512 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5513 if (blame->cb_args.commit_id == NULL) {
5514 err = got_error_from_errno("got_object_id_dup");
5517 blame->cb_args.quit = &s->done;
5519 blame->thread_args.path = s->path;
5520 blame->thread_args.repo = thread_repo;
5521 blame->thread_args.cb_args = &blame->cb_args;
5522 blame->thread_args.complete = &s->blame_complete;
5523 blame->thread_args.cancel_cb = cancel_blame_view;
5524 blame->thread_args.cancel_arg = &s->done;
5525 s->blame_complete = 0;
5527 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5528 s->first_displayed_line = 1;
5529 s->last_displayed_line = view->nlines;
5530 s->selected_line = 1;
5532 s->matched_line = 0;
5536 got_object_commit_close(commit);
5537 if (fd != -1 && close(fd) == -1 && err == NULL)
5538 err = got_error_from_errno("close");
5540 got_object_blob_close(blob);
5547 static const struct got_error *
5548 open_blame_view(struct tog_view *view, char *path,
5549 struct got_object_id *commit_id, struct got_repository *repo)
5551 const struct got_error *err = NULL;
5552 struct tog_blame_view_state *s = &view->state.blame;
5554 STAILQ_INIT(&s->blamed_commits);
5556 s->path = strdup(path);
5557 if (s->path == NULL)
5558 return got_error_from_errno("strdup");
5560 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5566 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5567 s->first_displayed_line = 1;
5568 s->last_displayed_line = view->nlines;
5569 s->selected_line = 1;
5570 s->blame_complete = 0;
5572 s->commit_id = commit_id;
5573 memset(&s->blame, 0, sizeof(s->blame));
5575 STAILQ_INIT(&s->colors);
5576 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5577 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5578 get_color_value("TOG_COLOR_COMMIT"));
5583 view->show = show_blame_view;
5584 view->input = input_blame_view;
5585 view->reset = reset_blame_view;
5586 view->close = close_blame_view;
5587 view->search_start = search_start_blame_view;
5588 view->search_next = search_next_blame_view;
5590 return run_blame(view);
5593 static const struct got_error *
5594 close_blame_view(struct tog_view *view)
5596 const struct got_error *err = NULL;
5597 struct tog_blame_view_state *s = &view->state.blame;
5599 if (s->blame.thread)
5600 err = stop_blame(&s->blame);
5602 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5603 struct got_object_qid *blamed_commit;
5604 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5605 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5606 got_object_qid_free(blamed_commit);
5610 free_colors(&s->colors);
5614 static const struct got_error *
5615 search_start_blame_view(struct tog_view *view)
5617 struct tog_blame_view_state *s = &view->state.blame;
5619 s->matched_line = 0;
5623 static const struct got_error *
5624 search_next_blame_view(struct tog_view *view)
5626 struct tog_blame_view_state *s = &view->state.blame;
5627 const struct got_error *err = NULL;
5630 size_t linesize = 0;
5633 if (!view->searching) {
5634 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5638 if (s->matched_line) {
5639 if (view->searching == TOG_SEARCH_FORWARD)
5640 lineno = s->matched_line + 1;
5642 lineno = s->matched_line - 1;
5644 lineno = s->first_displayed_line - 1 + s->selected_line;
5649 if (lineno <= 0 || lineno > s->blame.nlines) {
5650 if (s->matched_line == 0) {
5651 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5655 if (view->searching == TOG_SEARCH_FORWARD)
5658 lineno = s->blame.nlines;
5661 offset = s->blame.line_offsets[lineno - 1];
5662 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5664 return got_error_from_errno("fseeko");
5666 linelen = getline(&line, &linesize, s->blame.f);
5667 if (linelen != -1) {
5669 err = expand_tab(&exstr, line);
5672 if (match_line(exstr, &view->regex, 1,
5674 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5675 s->matched_line = lineno;
5681 if (view->searching == TOG_SEARCH_FORWARD)
5688 if (s->matched_line) {
5689 s->first_displayed_line = s->matched_line;
5690 s->selected_line = 1;
5696 static const struct got_error *
5697 show_blame_view(struct tog_view *view)
5699 const struct got_error *err = NULL;
5700 struct tog_blame_view_state *s = &view->state.blame;
5703 if (s->blame.thread == 0 && !s->blame_complete) {
5704 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5705 &s->blame.thread_args);
5707 return got_error_set_errno(errcode, "pthread_create");
5709 halfdelay(1); /* fast refresh while annotating */
5712 if (s->blame_complete)
5713 halfdelay(10); /* disable fast refresh */
5715 err = draw_blame(view);
5721 static const struct got_error *
5722 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5724 const struct got_error *err = NULL, *thread_err = NULL;
5725 struct tog_view *diff_view;
5726 struct tog_blame_view_state *s = &view->state.blame;
5727 int eos, nscroll, begin_y = 0, begin_x = 0;
5729 eos = nscroll = view->nlines - 2;
5730 if (view_is_hsplit_top(view))
5738 view->x = MAX(view->maxx - view->ncols / 3, 0);
5743 if (view->x + view->ncols / 3 < view->maxx)
5744 view->x += 2; /* move two columns right */
5750 view->x -= MIN(view->x, 2); /* move two columns back */
5759 s->selected_line = 1;
5760 s->first_displayed_line = 1;
5765 if (s->blame.nlines < eos) {
5766 s->selected_line = s->blame.nlines;
5767 s->first_displayed_line = 1;
5769 s->selected_line = eos;
5770 s->first_displayed_line = s->blame.nlines - (eos - 1);
5777 if (s->selected_line > 1)
5779 else if (s->selected_line == 1 &&
5780 s->first_displayed_line > 1)
5781 s->first_displayed_line--;
5792 if (s->first_displayed_line == 1) {
5793 if (view->count > 1)
5795 s->selected_line = MAX(1, s->selected_line - nscroll);
5799 if (s->first_displayed_line > nscroll)
5800 s->first_displayed_line -= nscroll;
5802 s->first_displayed_line = 1;
5807 if (s->selected_line < eos && s->first_displayed_line +
5808 s->selected_line <= s->blame.nlines)
5810 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5811 s->first_displayed_line++;
5817 struct got_object_id *id = NULL;
5820 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5821 s->first_displayed_line, s->selected_line);
5825 struct got_commit_object *commit, *pcommit;
5826 struct got_object_qid *pid;
5827 struct got_object_id *blob_id = NULL;
5829 err = got_object_open_as_commit(&commit,
5834 got_object_commit_get_parent_ids(commit));
5836 got_object_commit_close(commit);
5839 /* Check if path history ends here. */
5840 err = got_object_open_as_commit(&pcommit,
5844 err = got_object_id_by_path(&blob_id, s->repo,
5846 got_object_commit_close(pcommit);
5848 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5850 got_object_commit_close(commit);
5853 err = got_object_get_type(&obj_type, s->repo,
5856 /* Can't blame non-blob type objects. */
5857 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5858 got_object_commit_close(commit);
5861 err = got_object_qid_alloc(&s->blamed_commit,
5863 got_object_commit_close(commit);
5865 if (got_object_id_cmp(id,
5866 &s->blamed_commit->id) == 0)
5868 err = got_object_qid_alloc(&s->blamed_commit,
5874 thread_err = stop_blame(&s->blame);
5878 STAILQ_INSERT_HEAD(&s->blamed_commits,
5879 s->blamed_commit, entry);
5880 err = run_blame(view);
5886 struct got_object_qid *first;
5889 first = STAILQ_FIRST(&s->blamed_commits);
5890 if (!got_object_id_cmp(&first->id, s->commit_id))
5893 thread_err = stop_blame(&s->blame);
5897 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5898 got_object_qid_free(s->blamed_commit);
5900 STAILQ_FIRST(&s->blamed_commits);
5901 err = run_blame(view);
5908 struct got_object_id *id = NULL;
5909 struct got_object_qid *pid;
5910 struct got_commit_object *commit = NULL;
5913 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5914 s->first_displayed_line, s->selected_line);
5917 err = got_object_open_as_commit(&commit, s->repo, id);
5920 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5922 /* traversed from diff view, release diff resources */
5923 err = close_diff_view(*new_view);
5926 diff_view = *new_view;
5928 if (view_is_parent_view(view))
5929 view_get_split(view, &begin_y, &begin_x);
5931 diff_view = view_open(0, 0, begin_y, begin_x,
5933 if (diff_view == NULL) {
5934 got_object_commit_close(commit);
5935 err = got_error_from_errno("view_open");
5939 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5940 id, NULL, NULL, 3, 0, 0, view, s->repo);
5941 got_object_commit_close(commit);
5943 view_close(diff_view);
5946 s->last_diffed_line = s->first_displayed_line - 1 +
5949 break; /* still open from active diff view */
5950 if (view_is_parent_view(view) &&
5951 view->mode == TOG_VIEW_SPLIT_HRZN) {
5952 err = view_init_hsplit(view, begin_y);
5958 diff_view->focussed = 1;
5959 diff_view->mode = view->mode;
5960 diff_view->nlines = view->lines - begin_y;
5961 if (view_is_parent_view(view)) {
5962 view_transfer_size(diff_view, view);
5963 err = view_close_child(view);
5966 err = view_set_child(view, diff_view);
5969 view->focus_child = 1;
5971 *new_view = diff_view;
5984 if (s->last_displayed_line >= s->blame.nlines &&
5985 s->selected_line >= MIN(s->blame.nlines,
5986 view->nlines - 2)) {
5990 if (s->last_displayed_line >= s->blame.nlines &&
5991 s->selected_line < view->nlines - 2) {
5993 MIN(nscroll, s->last_displayed_line -
5994 s->first_displayed_line - s->selected_line + 1);
5996 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5997 s->first_displayed_line += nscroll;
5999 s->first_displayed_line =
6000 s->blame.nlines - (view->nlines - 3);
6003 if (s->selected_line > view->nlines - 2) {
6004 s->selected_line = MIN(s->blame.nlines,
6012 return thread_err ? thread_err : err;
6015 static const struct got_error *
6016 reset_blame_view(struct tog_view *view)
6018 const struct got_error *err;
6019 struct tog_blame_view_state *s = &view->state.blame;
6023 err = stop_blame(&s->blame);
6027 return run_blame(view);
6030 static const struct got_error *
6031 cmd_blame(int argc, char *argv[])
6033 const struct got_error *error;
6034 struct got_repository *repo = NULL;
6035 struct got_worktree *worktree = NULL;
6036 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6037 char *link_target = NULL;
6038 struct got_object_id *commit_id = NULL;
6039 struct got_commit_object *commit = NULL;
6040 char *commit_id_str = NULL;
6042 struct tog_view *view;
6043 int *pack_fds = NULL;
6045 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6048 commit_id_str = optarg;
6051 repo_path = realpath(optarg, NULL);
6052 if (repo_path == NULL)
6053 return got_error_from_errno2("realpath",
6068 error = got_repo_pack_fds_open(&pack_fds);
6072 if (repo_path == NULL) {
6073 cwd = getcwd(NULL, 0);
6075 return got_error_from_errno("getcwd");
6076 error = got_worktree_open(&worktree, cwd);
6077 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6081 strdup(got_worktree_get_repo_path(worktree));
6083 repo_path = strdup(cwd);
6084 if (repo_path == NULL) {
6085 error = got_error_from_errno("strdup");
6090 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6094 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6101 error = apply_unveil(got_repo_get_path(repo), NULL);
6105 error = tog_load_refs(repo, 0);
6109 if (commit_id_str == NULL) {
6110 struct got_reference *head_ref;
6111 error = got_ref_open(&head_ref, repo, worktree ?
6112 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6115 error = got_ref_resolve(&commit_id, repo, head_ref);
6116 got_ref_close(head_ref);
6118 error = got_repo_match_object_id(&commit_id, NULL,
6119 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6124 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6126 error = got_error_from_errno("view_open");
6130 error = got_object_open_as_commit(&commit, repo, commit_id);
6134 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6139 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6144 /* Release work tree lock. */
6145 got_worktree_close(worktree);
6148 error = view_loop(view);
6156 got_object_commit_close(commit);
6158 got_worktree_close(worktree);
6160 const struct got_error *close_err = got_repo_close(repo);
6165 const struct got_error *pack_err =
6166 got_repo_pack_fds_close(pack_fds);
6174 static const struct got_error *
6175 draw_tree_entries(struct tog_view *view, const char *parent_path)
6177 struct tog_tree_view_state *s = &view->state.tree;
6178 const struct got_error *err = NULL;
6179 struct got_tree_entry *te;
6181 struct tog_color *tc;
6182 int width, n, i, nentries;
6183 int limit = view->nlines;
6186 if (view_is_hsplit_top(view))
6187 --limit; /* border */
6189 werase(view->window);
6194 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6198 if (view_needs_focus_indication(view))
6199 wstandout(view->window);
6200 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6202 wattr_on(view->window,
6203 COLOR_PAIR(tc->colorpair), NULL);
6204 waddwstr(view->window, wline);
6206 wattr_off(view->window,
6207 COLOR_PAIR(tc->colorpair), NULL);
6208 if (view_needs_focus_indication(view))
6209 wstandend(view->window);
6212 if (width < view->ncols - 1)
6213 waddch(view->window, '\n');
6216 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6220 waddwstr(view->window, wline);
6223 if (width < view->ncols - 1)
6224 waddch(view->window, '\n');
6227 waddch(view->window, '\n');
6231 if (s->first_displayed_entry == NULL) {
6232 te = got_object_tree_get_first_entry(s->tree);
6233 if (s->selected == 0) {
6235 wstandout(view->window);
6236 s->selected_entry = NULL;
6238 waddstr(view->window, " ..\n"); /* parent directory */
6239 if (s->selected == 0 && view->focussed)
6240 wstandend(view->window);
6247 te = s->first_displayed_entry;
6250 nentries = got_object_tree_get_nentries(s->tree);
6251 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6252 char *line = NULL, *id_str = NULL, *link_target = NULL;
6253 const char *modestr = "";
6256 te = got_object_tree_get_entry(s->tree, i);
6257 mode = got_tree_entry_get_mode(te);
6260 err = got_object_id_str(&id_str,
6261 got_tree_entry_get_id(te));
6263 return got_error_from_errno(
6264 "got_object_id_str");
6266 if (got_object_tree_entry_is_submodule(te))
6268 else if (S_ISLNK(mode)) {
6271 err = got_tree_entry_get_symlink_target(&link_target,
6277 for (i = 0; i < strlen(link_target); i++) {
6278 if (!isprint((unsigned char)link_target[i]))
6279 link_target[i] = '?';
6283 else if (S_ISDIR(mode))
6285 else if (mode & S_IXUSR)
6287 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6288 got_tree_entry_get_name(te), modestr,
6289 link_target ? " -> ": "",
6290 link_target ? link_target : "") == -1) {
6293 return got_error_from_errno("asprintf");
6297 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6303 if (n == s->selected) {
6305 wstandout(view->window);
6306 s->selected_entry = te;
6308 tc = match_color(&s->colors, line);
6310 wattr_on(view->window,
6311 COLOR_PAIR(tc->colorpair), NULL);
6312 waddwstr(view->window, wline);
6314 wattr_off(view->window,
6315 COLOR_PAIR(tc->colorpair), NULL);
6316 if (width < view->ncols - 1)
6317 waddch(view->window, '\n');
6318 if (n == s->selected && view->focussed)
6319 wstandend(view->window);
6325 s->last_displayed_entry = te;
6334 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6336 struct got_tree_entry *te;
6337 int isroot = s->tree == s->root;
6340 if (s->first_displayed_entry == NULL)
6343 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6344 while (i++ < maxscroll) {
6347 s->first_displayed_entry = NULL;
6350 s->first_displayed_entry = te;
6351 te = got_tree_entry_get_prev(s->tree, te);
6355 static const struct got_error *
6356 tree_scroll_down(struct tog_view *view, int maxscroll)
6358 struct tog_tree_view_state *s = &view->state.tree;
6359 struct got_tree_entry *next, *last;
6362 if (s->first_displayed_entry)
6363 next = got_tree_entry_get_next(s->tree,
6364 s->first_displayed_entry);
6366 next = got_object_tree_get_first_entry(s->tree);
6368 last = s->last_displayed_entry;
6369 while (next && n++ < maxscroll) {
6371 last = got_tree_entry_get_next(s->tree, last);
6372 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6373 s->first_displayed_entry = next;
6374 next = got_tree_entry_get_next(s->tree, next);
6381 static const struct got_error *
6382 tree_entry_path(char **path, struct tog_parent_trees *parents,
6383 struct got_tree_entry *te)
6385 const struct got_error *err = NULL;
6386 struct tog_parent_tree *pt;
6387 size_t len = 2; /* for leading slash and NUL */
6389 TAILQ_FOREACH(pt, parents, entry)
6390 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6393 len += strlen(got_tree_entry_get_name(te));
6395 *path = calloc(1, len);
6397 return got_error_from_errno("calloc");
6400 pt = TAILQ_LAST(parents, tog_parent_trees);
6402 const char *name = got_tree_entry_get_name(pt->selected_entry);
6403 if (strlcat(*path, name, len) >= len) {
6404 err = got_error(GOT_ERR_NO_SPACE);
6407 if (strlcat(*path, "/", len) >= len) {
6408 err = got_error(GOT_ERR_NO_SPACE);
6411 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6414 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6415 err = got_error(GOT_ERR_NO_SPACE);
6427 static const struct got_error *
6428 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6429 struct got_tree_entry *te, struct tog_parent_trees *parents,
6430 struct got_object_id *commit_id, struct got_repository *repo)
6432 const struct got_error *err = NULL;
6434 struct tog_view *blame_view;
6438 err = tree_entry_path(&path, parents, te);
6442 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6443 if (blame_view == NULL) {
6444 err = got_error_from_errno("view_open");
6448 err = open_blame_view(blame_view, path, commit_id, repo);
6450 if (err->code == GOT_ERR_CANCELLED)
6452 view_close(blame_view);
6454 *new_view = blame_view;
6460 static const struct got_error *
6461 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6462 struct tog_tree_view_state *s)
6464 struct tog_view *log_view;
6465 const struct got_error *err = NULL;
6470 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6471 if (log_view == NULL)
6472 return got_error_from_errno("view_open");
6474 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6478 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6481 view_close(log_view);
6483 *new_view = log_view;
6488 static const struct got_error *
6489 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6490 const char *head_ref_name, struct got_repository *repo)
6492 const struct got_error *err = NULL;
6493 char *commit_id_str = NULL;
6494 struct tog_tree_view_state *s = &view->state.tree;
6495 struct got_commit_object *commit = NULL;
6497 TAILQ_INIT(&s->parents);
6498 STAILQ_INIT(&s->colors);
6500 s->commit_id = got_object_id_dup(commit_id);
6501 if (s->commit_id == NULL)
6502 return got_error_from_errno("got_object_id_dup");
6504 err = got_object_open_as_commit(&commit, repo, commit_id);
6509 * The root is opened here and will be closed when the view is closed.
6510 * Any visited subtrees and their path-wise parents are opened and
6513 err = got_object_open_as_tree(&s->root, repo,
6514 got_object_commit_get_tree_id(commit));
6519 err = got_object_id_str(&commit_id_str, commit_id);
6523 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6524 err = got_error_from_errno("asprintf");
6528 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6529 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6530 if (head_ref_name) {
6531 s->head_ref_name = strdup(head_ref_name);
6532 if (s->head_ref_name == NULL) {
6533 err = got_error_from_errno("strdup");
6539 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6540 err = add_color(&s->colors, "\\$$",
6541 TOG_COLOR_TREE_SUBMODULE,
6542 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6545 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6546 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6549 err = add_color(&s->colors, "/$",
6550 TOG_COLOR_TREE_DIRECTORY,
6551 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6555 err = add_color(&s->colors, "\\*$",
6556 TOG_COLOR_TREE_EXECUTABLE,
6557 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6561 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6562 get_color_value("TOG_COLOR_COMMIT"));
6567 view->show = show_tree_view;
6568 view->input = input_tree_view;
6569 view->close = close_tree_view;
6570 view->search_start = search_start_tree_view;
6571 view->search_next = search_next_tree_view;
6573 free(commit_id_str);
6575 got_object_commit_close(commit);
6577 close_tree_view(view);
6581 static const struct got_error *
6582 close_tree_view(struct tog_view *view)
6584 struct tog_tree_view_state *s = &view->state.tree;
6586 free_colors(&s->colors);
6587 free(s->tree_label);
6588 s->tree_label = NULL;
6590 s->commit_id = NULL;
6591 free(s->head_ref_name);
6592 s->head_ref_name = NULL;
6593 while (!TAILQ_EMPTY(&s->parents)) {
6594 struct tog_parent_tree *parent;
6595 parent = TAILQ_FIRST(&s->parents);
6596 TAILQ_REMOVE(&s->parents, parent, entry);
6597 if (parent->tree != s->root)
6598 got_object_tree_close(parent->tree);
6602 if (s->tree != NULL && s->tree != s->root)
6603 got_object_tree_close(s->tree);
6605 got_object_tree_close(s->root);
6609 static const struct got_error *
6610 search_start_tree_view(struct tog_view *view)
6612 struct tog_tree_view_state *s = &view->state.tree;
6614 s->matched_entry = NULL;
6619 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6621 regmatch_t regmatch;
6623 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6627 static const struct got_error *
6628 search_next_tree_view(struct tog_view *view)
6630 struct tog_tree_view_state *s = &view->state.tree;
6631 struct got_tree_entry *te = NULL;
6633 if (!view->searching) {
6634 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6638 if (s->matched_entry) {
6639 if (view->searching == TOG_SEARCH_FORWARD) {
6640 if (s->selected_entry)
6641 te = got_tree_entry_get_next(s->tree,
6644 te = got_object_tree_get_first_entry(s->tree);
6646 if (s->selected_entry == NULL)
6647 te = got_object_tree_get_last_entry(s->tree);
6649 te = got_tree_entry_get_prev(s->tree,
6653 if (s->selected_entry)
6654 te = s->selected_entry;
6655 else if (view->searching == TOG_SEARCH_FORWARD)
6656 te = got_object_tree_get_first_entry(s->tree);
6658 te = got_object_tree_get_last_entry(s->tree);
6663 if (s->matched_entry == NULL) {
6664 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6667 if (view->searching == TOG_SEARCH_FORWARD)
6668 te = got_object_tree_get_first_entry(s->tree);
6670 te = got_object_tree_get_last_entry(s->tree);
6673 if (match_tree_entry(te, &view->regex)) {
6674 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6675 s->matched_entry = te;
6679 if (view->searching == TOG_SEARCH_FORWARD)
6680 te = got_tree_entry_get_next(s->tree, te);
6682 te = got_tree_entry_get_prev(s->tree, te);
6685 if (s->matched_entry) {
6686 s->first_displayed_entry = s->matched_entry;
6693 static const struct got_error *
6694 show_tree_view(struct tog_view *view)
6696 const struct got_error *err = NULL;
6697 struct tog_tree_view_state *s = &view->state.tree;
6700 err = tree_entry_path(&parent_path, &s->parents, NULL);
6704 err = draw_tree_entries(view, parent_path);
6711 static const struct got_error *
6712 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6714 const struct got_error *err = NULL;
6715 struct tog_tree_view_state *s = &view->state.tree;
6716 struct tog_view *log_view, *ref_view;
6717 struct got_tree_entry *te;
6718 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6722 s->show_ids = !s->show_ids;
6727 if (!s->selected_entry)
6729 if (view_is_parent_view(view))
6730 view_get_split(view, &begin_y, &begin_x);
6731 err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6732 if (view_is_parent_view(view) &&
6733 view->mode == TOG_VIEW_SPLIT_HRZN) {
6734 err = view_init_hsplit(view, begin_y);
6739 log_view->focussed = 1;
6740 log_view->mode = view->mode;
6741 log_view->nlines = view->lines - begin_y;
6742 if (view_is_parent_view(view)) {
6743 view_transfer_size(log_view, view);
6744 err = view_close_child(view);
6747 err = view_set_child(view, log_view);
6750 view->focus_child = 1;
6752 *new_view = log_view;
6756 if (view_is_parent_view(view))
6757 view_get_split(view, &begin_y, &begin_x);
6758 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6759 if (ref_view == NULL)
6760 return got_error_from_errno("view_open");
6761 err = open_ref_view(ref_view, s->repo);
6763 view_close(ref_view);
6766 if (view_is_parent_view(view) &&
6767 view->mode == TOG_VIEW_SPLIT_HRZN) {
6768 err = view_init_hsplit(view, begin_y);
6773 ref_view->focussed = 1;
6774 ref_view->mode = view->mode;
6775 ref_view->nlines = view->lines - begin_y;
6776 if (view_is_parent_view(view)) {
6777 view_transfer_size(ref_view, view);
6778 err = view_close_child(view);
6781 err = view_set_child(view, ref_view);
6784 view->focus_child = 1;
6786 *new_view = ref_view;
6792 if (s->tree == s->root)
6793 s->first_displayed_entry =
6794 got_object_tree_get_first_entry(s->tree);
6796 s->first_displayed_entry = NULL;
6800 int eos = view->nlines - 3;
6802 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6806 te = got_object_tree_get_last_entry(s->tree);
6807 for (n = 0; n < eos; n++) {
6809 if (s->tree != s->root) {
6810 s->first_displayed_entry = NULL;
6815 s->first_displayed_entry = te;
6816 te = got_tree_entry_get_prev(s->tree, te);
6819 s->selected = n - 1;
6825 if (s->selected > 0) {
6829 tree_scroll_up(s, 1);
6830 if (s->selected_entry == NULL ||
6831 (s->tree == s->root && s->selected_entry ==
6832 got_object_tree_get_first_entry(s->tree)))
6842 if (s->tree == s->root) {
6843 if (got_object_tree_get_first_entry(s->tree) ==
6844 s->first_displayed_entry)
6845 s->selected -= MIN(s->selected, nscroll);
6847 if (s->first_displayed_entry == NULL)
6848 s->selected -= MIN(s->selected, nscroll);
6850 tree_scroll_up(s, MAX(0, nscroll));
6851 if (s->selected_entry == NULL ||
6852 (s->tree == s->root && s->selected_entry ==
6853 got_object_tree_get_first_entry(s->tree)))
6859 if (s->selected < s->ndisplayed - 1) {
6863 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6865 /* can't scroll any further */
6869 tree_scroll_down(view, 1);
6879 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6881 /* can't scroll any further; move cursor down */
6882 if (s->selected < s->ndisplayed - 1)
6883 s->selected += MIN(nscroll,
6884 s->ndisplayed - s->selected - 1);
6889 tree_scroll_down(view, nscroll);
6894 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6895 struct tog_parent_tree *parent;
6896 /* user selected '..' */
6897 if (s->tree == s->root) {
6901 parent = TAILQ_FIRST(&s->parents);
6902 TAILQ_REMOVE(&s->parents, parent,
6904 got_object_tree_close(s->tree);
6905 s->tree = parent->tree;
6906 s->first_displayed_entry =
6907 parent->first_displayed_entry;
6909 parent->selected_entry;
6910 s->selected = parent->selected;
6911 if (s->selected > view->nlines - 3) {
6912 err = offset_selection_down(view);
6917 } else if (S_ISDIR(got_tree_entry_get_mode(
6918 s->selected_entry))) {
6919 struct got_tree_object *subtree;
6921 err = got_object_open_as_tree(&subtree, s->repo,
6922 got_tree_entry_get_id(s->selected_entry));
6925 err = tree_view_visit_subtree(s, subtree);
6927 got_object_tree_close(subtree);
6930 } else if (S_ISREG(got_tree_entry_get_mode(
6931 s->selected_entry))) {
6932 struct tog_view *blame_view;
6933 int begin_x = 0, begin_y = 0;
6935 if (view_is_parent_view(view))
6936 view_get_split(view, &begin_y, &begin_x);
6938 err = blame_tree_entry(&blame_view, begin_y, begin_x,
6939 s->selected_entry, &s->parents,
6940 s->commit_id, s->repo);
6944 if (view_is_parent_view(view) &&
6945 view->mode == TOG_VIEW_SPLIT_HRZN) {
6946 err = view_init_hsplit(view, begin_y);
6953 blame_view->focussed = 1;
6954 blame_view->mode = view->mode;
6955 blame_view->nlines = view->lines - begin_y;
6956 if (view_is_parent_view(view)) {
6957 view_transfer_size(blame_view, view);
6958 err = view_close_child(view);
6961 err = view_set_child(view, blame_view);
6964 view->focus_child = 1;
6966 *new_view = blame_view;
6970 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6971 s->selected = view->nlines - 4;
6987 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6992 static const struct got_error *
6993 cmd_tree(int argc, char *argv[])
6995 const struct got_error *error;
6996 struct got_repository *repo = NULL;
6997 struct got_worktree *worktree = NULL;
6998 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6999 struct got_object_id *commit_id = NULL;
7000 struct got_commit_object *commit = NULL;
7001 const char *commit_id_arg = NULL;
7003 struct got_reference *ref = NULL;
7004 const char *head_ref_name = NULL;
7006 struct tog_view *view;
7007 int *pack_fds = NULL;
7009 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7012 commit_id_arg = optarg;
7015 repo_path = realpath(optarg, NULL);
7016 if (repo_path == NULL)
7017 return got_error_from_errno2("realpath",
7032 error = got_repo_pack_fds_open(&pack_fds);
7036 if (repo_path == NULL) {
7037 cwd = getcwd(NULL, 0);
7039 return got_error_from_errno("getcwd");
7040 error = got_worktree_open(&worktree, cwd);
7041 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7045 strdup(got_worktree_get_repo_path(worktree));
7047 repo_path = strdup(cwd);
7048 if (repo_path == NULL) {
7049 error = got_error_from_errno("strdup");
7054 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7058 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7065 error = apply_unveil(got_repo_get_path(repo), NULL);
7069 error = tog_load_refs(repo, 0);
7073 if (commit_id_arg == NULL) {
7074 error = got_repo_match_object_id(&commit_id, &label,
7075 worktree ? got_worktree_get_head_ref_name(worktree) :
7076 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7079 head_ref_name = label;
7081 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7083 head_ref_name = got_ref_get_name(ref);
7084 else if (error->code != GOT_ERR_NOT_REF)
7086 error = got_repo_match_object_id(&commit_id, NULL,
7087 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7092 error = got_object_open_as_commit(&commit, repo, commit_id);
7096 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7098 error = got_error_from_errno("view_open");
7101 error = open_tree_view(view, commit_id, head_ref_name, repo);
7104 if (!got_path_is_root_dir(in_repo_path)) {
7105 error = tree_view_walk_path(&view->state.tree, commit,
7112 /* Release work tree lock. */
7113 got_worktree_close(worktree);
7116 error = view_loop(view);
7125 const struct got_error *close_err = got_repo_close(repo);
7130 const struct got_error *pack_err =
7131 got_repo_pack_fds_close(pack_fds);
7139 static const struct got_error *
7140 ref_view_load_refs(struct tog_ref_view_state *s)
7142 struct got_reflist_entry *sre;
7143 struct tog_reflist_entry *re;
7146 TAILQ_FOREACH(sre, &tog_refs, entry) {
7147 if (strncmp(got_ref_get_name(sre->ref),
7148 "refs/got/", 9) == 0 &&
7149 strncmp(got_ref_get_name(sre->ref),
7150 "refs/got/backup/", 16) != 0)
7153 re = malloc(sizeof(*re));
7155 return got_error_from_errno("malloc");
7157 re->ref = got_ref_dup(sre->ref);
7158 if (re->ref == NULL)
7159 return got_error_from_errno("got_ref_dup");
7160 re->idx = s->nrefs++;
7161 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7164 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7169 ref_view_free_refs(struct tog_ref_view_state *s)
7171 struct tog_reflist_entry *re;
7173 while (!TAILQ_EMPTY(&s->refs)) {
7174 re = TAILQ_FIRST(&s->refs);
7175 TAILQ_REMOVE(&s->refs, re, entry);
7176 got_ref_close(re->ref);
7181 static const struct got_error *
7182 open_ref_view(struct tog_view *view, struct got_repository *repo)
7184 const struct got_error *err = NULL;
7185 struct tog_ref_view_state *s = &view->state.ref;
7187 s->selected_entry = 0;
7190 TAILQ_INIT(&s->refs);
7191 STAILQ_INIT(&s->colors);
7193 err = ref_view_load_refs(s);
7197 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7198 err = add_color(&s->colors, "^refs/heads/",
7199 TOG_COLOR_REFS_HEADS,
7200 get_color_value("TOG_COLOR_REFS_HEADS"));
7204 err = add_color(&s->colors, "^refs/tags/",
7205 TOG_COLOR_REFS_TAGS,
7206 get_color_value("TOG_COLOR_REFS_TAGS"));
7210 err = add_color(&s->colors, "^refs/remotes/",
7211 TOG_COLOR_REFS_REMOTES,
7212 get_color_value("TOG_COLOR_REFS_REMOTES"));
7216 err = add_color(&s->colors, "^refs/got/backup/",
7217 TOG_COLOR_REFS_BACKUP,
7218 get_color_value("TOG_COLOR_REFS_BACKUP"));
7223 view->show = show_ref_view;
7224 view->input = input_ref_view;
7225 view->close = close_ref_view;
7226 view->search_start = search_start_ref_view;
7227 view->search_next = search_next_ref_view;
7230 free_colors(&s->colors);
7234 static const struct got_error *
7235 close_ref_view(struct tog_view *view)
7237 struct tog_ref_view_state *s = &view->state.ref;
7239 ref_view_free_refs(s);
7240 free_colors(&s->colors);
7245 static const struct got_error *
7246 resolve_reflist_entry(struct got_object_id **commit_id,
7247 struct tog_reflist_entry *re, struct got_repository *repo)
7249 const struct got_error *err = NULL;
7250 struct got_object_id *obj_id;
7251 struct got_tag_object *tag = NULL;
7256 err = got_ref_resolve(&obj_id, repo, re->ref);
7260 err = got_object_get_type(&obj_type, repo, obj_id);
7265 case GOT_OBJ_TYPE_COMMIT:
7266 *commit_id = obj_id;
7268 case GOT_OBJ_TYPE_TAG:
7269 err = got_object_open_as_tag(&tag, repo, obj_id);
7273 err = got_object_get_type(&obj_type, repo,
7274 got_object_tag_get_object_id(tag));
7277 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7278 err = got_error(GOT_ERR_OBJ_TYPE);
7281 *commit_id = got_object_id_dup(
7282 got_object_tag_get_object_id(tag));
7283 if (*commit_id == NULL) {
7284 err = got_error_from_errno("got_object_id_dup");
7289 err = got_error(GOT_ERR_OBJ_TYPE);
7295 got_object_tag_close(tag);
7303 static const struct got_error *
7304 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7305 struct tog_reflist_entry *re, struct got_repository *repo)
7307 struct tog_view *log_view;
7308 const struct got_error *err = NULL;
7309 struct got_object_id *commit_id = NULL;
7313 err = resolve_reflist_entry(&commit_id, re, repo);
7315 if (err->code != GOT_ERR_OBJ_TYPE)
7321 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7322 if (log_view == NULL) {
7323 err = got_error_from_errno("view_open");
7327 err = open_log_view(log_view, commit_id, repo,
7328 got_ref_get_name(re->ref), "", 0);
7331 view_close(log_view);
7333 *new_view = log_view;
7339 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7341 struct tog_reflist_entry *re;
7344 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7347 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7348 while (i++ < maxscroll) {
7351 s->first_displayed_entry = re;
7352 re = TAILQ_PREV(re, tog_reflist_head, entry);
7356 static const struct got_error *
7357 ref_scroll_down(struct tog_view *view, int maxscroll)
7359 struct tog_ref_view_state *s = &view->state.ref;
7360 struct tog_reflist_entry *next, *last;
7363 if (s->first_displayed_entry)
7364 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7366 next = TAILQ_FIRST(&s->refs);
7368 last = s->last_displayed_entry;
7369 while (next && n++ < maxscroll) {
7371 last = TAILQ_NEXT(last, entry);
7372 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7373 s->first_displayed_entry = next;
7374 next = TAILQ_NEXT(next, entry);
7381 static const struct got_error *
7382 search_start_ref_view(struct tog_view *view)
7384 struct tog_ref_view_state *s = &view->state.ref;
7386 s->matched_entry = NULL;
7391 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7393 regmatch_t regmatch;
7395 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7399 static const struct got_error *
7400 search_next_ref_view(struct tog_view *view)
7402 struct tog_ref_view_state *s = &view->state.ref;
7403 struct tog_reflist_entry *re = NULL;
7405 if (!view->searching) {
7406 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7410 if (s->matched_entry) {
7411 if (view->searching == TOG_SEARCH_FORWARD) {
7412 if (s->selected_entry)
7413 re = TAILQ_NEXT(s->selected_entry, entry);
7415 re = TAILQ_PREV(s->selected_entry,
7416 tog_reflist_head, entry);
7418 if (s->selected_entry == NULL)
7419 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7421 re = TAILQ_PREV(s->selected_entry,
7422 tog_reflist_head, entry);
7425 if (s->selected_entry)
7426 re = s->selected_entry;
7427 else if (view->searching == TOG_SEARCH_FORWARD)
7428 re = TAILQ_FIRST(&s->refs);
7430 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7435 if (s->matched_entry == NULL) {
7436 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7439 if (view->searching == TOG_SEARCH_FORWARD)
7440 re = TAILQ_FIRST(&s->refs);
7442 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7445 if (match_reflist_entry(re, &view->regex)) {
7446 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7447 s->matched_entry = re;
7451 if (view->searching == TOG_SEARCH_FORWARD)
7452 re = TAILQ_NEXT(re, entry);
7454 re = TAILQ_PREV(re, tog_reflist_head, entry);
7457 if (s->matched_entry) {
7458 s->first_displayed_entry = s->matched_entry;
7465 static const struct got_error *
7466 show_ref_view(struct tog_view *view)
7468 const struct got_error *err = NULL;
7469 struct tog_ref_view_state *s = &view->state.ref;
7470 struct tog_reflist_entry *re;
7473 struct tog_color *tc;
7475 int limit = view->nlines;
7477 werase(view->window);
7480 if (view_is_hsplit_top(view))
7481 --limit; /* border */
7486 re = s->first_displayed_entry;
7488 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7490 return got_error_from_errno("asprintf");
7492 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7497 if (view_needs_focus_indication(view))
7498 wstandout(view->window);
7499 waddwstr(view->window, wline);
7500 if (view_needs_focus_indication(view))
7501 wstandend(view->window);
7506 if (width < view->ncols - 1)
7507 waddch(view->window, '\n');
7512 while (re && limit > 0) {
7514 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7517 struct got_commit_object *ci;
7518 struct got_tag_object *tag;
7519 struct got_object_id *id;
7523 err = got_ref_resolve(&id, s->repo, re->ref);
7526 err = got_object_open_as_tag(&tag, s->repo, id);
7528 if (err->code != GOT_ERR_OBJ_TYPE) {
7532 err = got_object_open_as_commit(&ci, s->repo,
7538 t = got_object_commit_get_committer_time(ci);
7539 got_object_commit_close(ci);
7541 t = got_object_tag_get_tagger_time(tag);
7542 got_object_tag_close(tag);
7545 if (gmtime_r(&t, &tm) == NULL)
7546 return got_error_from_errno("gmtime_r");
7547 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7548 return got_error(GOT_ERR_NO_SPACE);
7550 if (got_ref_is_symbolic(re->ref)) {
7551 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7552 ymd : "", got_ref_get_name(re->ref),
7553 got_ref_get_symref_target(re->ref)) == -1)
7554 return got_error_from_errno("asprintf");
7555 } else if (s->show_ids) {
7556 struct got_object_id *id;
7558 err = got_ref_resolve(&id, s->repo, re->ref);
7561 err = got_object_id_str(&id_str, id);
7566 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7567 got_ref_get_name(re->ref), id_str) == -1) {
7568 err = got_error_from_errno("asprintf");
7575 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7576 got_ref_get_name(re->ref)) == -1)
7577 return got_error_from_errno("asprintf");
7579 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7585 if (n == s->selected) {
7587 wstandout(view->window);
7588 s->selected_entry = re;
7590 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7592 wattr_on(view->window,
7593 COLOR_PAIR(tc->colorpair), NULL);
7594 waddwstr(view->window, wline);
7596 wattr_off(view->window,
7597 COLOR_PAIR(tc->colorpair), NULL);
7598 if (width < view->ncols - 1)
7599 waddch(view->window, '\n');
7600 if (n == s->selected && view->focussed)
7601 wstandend(view->window);
7607 s->last_displayed_entry = re;
7610 re = TAILQ_NEXT(re, entry);
7617 static const struct got_error *
7618 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7619 struct tog_reflist_entry *re, struct got_repository *repo)
7621 const struct got_error *err = NULL;
7622 struct got_object_id *commit_id = NULL;
7623 struct tog_view *tree_view;
7627 err = resolve_reflist_entry(&commit_id, re, repo);
7629 if (err->code != GOT_ERR_OBJ_TYPE)
7636 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7637 if (tree_view == NULL) {
7638 err = got_error_from_errno("view_open");
7642 err = open_tree_view(tree_view, commit_id,
7643 got_ref_get_name(re->ref), repo);
7647 *new_view = tree_view;
7652 static const struct got_error *
7653 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7655 const struct got_error *err = NULL;
7656 struct tog_ref_view_state *s = &view->state.ref;
7657 struct tog_view *log_view, *tree_view;
7658 struct tog_reflist_entry *re;
7659 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7663 s->show_ids = !s->show_ids;
7667 s->show_date = !s->show_date;
7671 s->sort_by_date = !s->sort_by_date;
7673 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7674 got_ref_cmp_by_commit_timestamp_descending :
7675 tog_ref_cmp_by_name, s->repo);
7678 got_reflist_object_id_map_free(tog_refs_idmap);
7679 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7680 &tog_refs, s->repo);
7683 ref_view_free_refs(s);
7684 err = ref_view_load_refs(s);
7689 if (!s->selected_entry)
7691 if (view_is_parent_view(view))
7692 view_get_split(view, &begin_y, &begin_x);
7694 err = log_ref_entry(&log_view, begin_y, begin_x,
7695 s->selected_entry, s->repo);
7699 if (view_is_parent_view(view) &&
7700 view->mode == TOG_VIEW_SPLIT_HRZN) {
7701 err = view_init_hsplit(view, begin_y);
7707 log_view->focussed = 1;
7708 log_view->mode = view->mode;
7709 log_view->nlines = view->lines - begin_y;
7710 if (view_is_parent_view(view)) {
7711 view_transfer_size(log_view, view);
7712 err = view_close_child(view);
7715 err = view_set_child(view, log_view);
7718 view->focus_child = 1;
7720 *new_view = log_view;
7724 if (!s->selected_entry)
7726 if (view_is_parent_view(view))
7727 view_get_split(view, &begin_y, &begin_x);
7728 err = browse_ref_tree(&tree_view, begin_y, begin_x,
7729 s->selected_entry, s->repo);
7730 if (err || tree_view == NULL)
7732 if (view_is_parent_view(view) &&
7733 view->mode == TOG_VIEW_SPLIT_HRZN) {
7734 err = view_init_hsplit(view, begin_y);
7739 tree_view->focussed = 1;
7740 tree_view->mode = view->mode;
7741 tree_view->nlines = view->lines - begin_y;
7742 if (view_is_parent_view(view)) {
7743 view_transfer_size(tree_view, view);
7744 err = view_close_child(view);
7747 err = view_set_child(view, tree_view);
7750 view->focus_child = 1;
7752 *new_view = tree_view;
7758 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7762 int eos = view->nlines - 1;
7764 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7768 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7769 for (n = 0; n < eos; n++) {
7772 s->first_displayed_entry = re;
7773 re = TAILQ_PREV(re, tog_reflist_head, entry);
7776 s->selected = n - 1;
7782 if (s->selected > 0) {
7786 ref_scroll_up(s, 1);
7787 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7797 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7798 s->selected -= MIN(nscroll, s->selected);
7799 ref_scroll_up(s, MAX(0, nscroll));
7800 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7806 if (s->selected < s->ndisplayed - 1) {
7810 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7811 /* can't scroll any further */
7815 ref_scroll_down(view, 1);
7825 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7826 /* can't scroll any further; move cursor down */
7827 if (s->selected < s->ndisplayed - 1)
7828 s->selected += MIN(nscroll,
7829 s->ndisplayed - s->selected - 1);
7830 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7831 s->selected += s->ndisplayed - s->selected - 1;
7835 ref_scroll_down(view, nscroll);
7840 err = tog_load_refs(s->repo, s->sort_by_date);
7843 ref_view_free_refs(s);
7844 err = ref_view_load_refs(s);
7847 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7848 s->selected = view->nlines - 2;
7862 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7867 static const struct got_error *
7868 cmd_ref(int argc, char *argv[])
7870 const struct got_error *error;
7871 struct got_repository *repo = NULL;
7872 struct got_worktree *worktree = NULL;
7873 char *cwd = NULL, *repo_path = NULL;
7875 struct tog_view *view;
7876 int *pack_fds = NULL;
7878 while ((ch = getopt(argc, argv, "r:")) != -1) {
7881 repo_path = realpath(optarg, NULL);
7882 if (repo_path == NULL)
7883 return got_error_from_errno2("realpath",
7898 error = got_repo_pack_fds_open(&pack_fds);
7902 if (repo_path == NULL) {
7903 cwd = getcwd(NULL, 0);
7905 return got_error_from_errno("getcwd");
7906 error = got_worktree_open(&worktree, cwd);
7907 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7911 strdup(got_worktree_get_repo_path(worktree));
7913 repo_path = strdup(cwd);
7914 if (repo_path == NULL) {
7915 error = got_error_from_errno("strdup");
7920 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7926 error = apply_unveil(got_repo_get_path(repo), NULL);
7930 error = tog_load_refs(repo, 0);
7934 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7936 error = got_error_from_errno("view_open");
7940 error = open_ref_view(view, repo);
7945 /* Release work tree lock. */
7946 got_worktree_close(worktree);
7949 error = view_loop(view);
7954 const struct got_error *close_err = got_repo_close(repo);
7959 const struct got_error *pack_err =
7960 got_repo_pack_fds_close(pack_fds);
7969 * If view was scrolled down to move the selected line into view when opening a
7970 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7973 offset_selection_up(struct tog_view *view)
7975 switch (view->type) {
7976 case TOG_VIEW_BLAME: {
7977 struct tog_blame_view_state *s = &view->state.blame;
7978 if (s->first_displayed_line == 1) {
7979 s->selected_line = MAX(s->selected_line - view->offset,
7983 if (s->first_displayed_line > view->offset)
7984 s->first_displayed_line -= view->offset;
7986 s->first_displayed_line = 1;
7987 s->selected_line += view->offset;
7991 log_scroll_up(&view->state.log, view->offset);
7992 view->state.log.selected += view->offset;
7995 ref_scroll_up(&view->state.ref, view->offset);
7996 view->state.ref.selected += view->offset;
7999 tree_scroll_up(&view->state.tree, view->offset);
8000 view->state.tree.selected += view->offset;
8010 * If the selected line is in the section of screen covered by the bottom split,
8011 * scroll down offset lines to move it into view and index its new position.
8013 static const struct got_error *
8014 offset_selection_down(struct tog_view *view)
8016 const struct got_error *err = NULL;
8017 const struct got_error *(*scrolld)(struct tog_view *, int);
8018 int *selected = NULL;
8021 switch (view->type) {
8022 case TOG_VIEW_BLAME: {
8023 struct tog_blame_view_state *s = &view->state.blame;
8026 if (s->selected_line > view->nlines - header) {
8027 offset = abs(view->nlines - s->selected_line - header);
8028 s->first_displayed_line += offset;
8029 s->selected_line -= offset;
8030 view->offset = offset;
8034 case TOG_VIEW_LOG: {
8035 struct tog_log_view_state *s = &view->state.log;
8036 scrolld = &log_scroll_down;
8037 header = view_is_parent_view(view) ? 3 : 2;
8038 selected = &s->selected;
8041 case TOG_VIEW_REF: {
8042 struct tog_ref_view_state *s = &view->state.ref;
8043 scrolld = &ref_scroll_down;
8045 selected = &s->selected;
8048 case TOG_VIEW_TREE: {
8049 struct tog_tree_view_state *s = &view->state.tree;
8050 scrolld = &tree_scroll_down;
8052 selected = &s->selected;
8062 if (selected && *selected > view->nlines - header) {
8063 offset = abs(view->nlines - *selected - header);
8064 view->offset = offset;
8065 if (scrolld && offset) {
8066 err = scrolld(view, offset);
8067 *selected -= offset;
8075 list_commands(FILE *fp)
8079 fprintf(fp, "commands:");
8080 for (i = 0; i < nitems(tog_commands); i++) {
8081 const struct tog_cmd *cmd = &tog_commands[i];
8082 fprintf(fp, " %s", cmd->name);
8088 usage(int hflag, int status)
8090 FILE *fp = (status == 0) ? stdout : stderr;
8092 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8095 fprintf(fp, "lazy usage: %s path\n", getprogname());
8102 make_argv(int argc, ...)
8110 argv = calloc(argc, sizeof(char *));
8113 for (i = 0; i < argc; i++) {
8114 argv[i] = strdup(va_arg(ap, char *));
8115 if (argv[i] == NULL)
8124 * Try to convert 'tog path' into a 'tog log path' command.
8125 * The user could simply have mistyped the command rather than knowingly
8126 * provided a path. So check whether argv[0] can in fact be resolved
8127 * to a path in the HEAD commit and print a special error if not.
8128 * This hack is for mpi@ <3
8130 static const struct got_error *
8131 tog_log_with_path(int argc, char *argv[])
8133 const struct got_error *error = NULL, *close_err;
8134 const struct tog_cmd *cmd = NULL;
8135 struct got_repository *repo = NULL;
8136 struct got_worktree *worktree = NULL;
8137 struct got_object_id *commit_id = NULL, *id = NULL;
8138 struct got_commit_object *commit = NULL;
8139 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8140 char *commit_id_str = NULL, **cmd_argv = NULL;
8141 int *pack_fds = NULL;
8143 cwd = getcwd(NULL, 0);
8145 return got_error_from_errno("getcwd");
8147 error = got_repo_pack_fds_open(&pack_fds);
8151 error = got_worktree_open(&worktree, cwd);
8152 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8156 repo_path = strdup(got_worktree_get_repo_path(worktree));
8158 repo_path = strdup(cwd);
8159 if (repo_path == NULL) {
8160 error = got_error_from_errno("strdup");
8164 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8168 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8173 error = tog_load_refs(repo, 0);
8176 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8177 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8178 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8183 got_worktree_close(worktree);
8187 error = got_object_open_as_commit(&commit, repo, commit_id);
8191 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8193 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8195 fprintf(stderr, "%s: '%s' is no known command or path\n",
8196 getprogname(), argv[0]);
8201 close_err = got_repo_close(repo);
8206 error = got_object_id_str(&commit_id_str, commit_id);
8210 cmd = &tog_commands[0]; /* log */
8212 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8213 error = cmd->cmd_main(argc, cmd_argv);
8216 close_err = got_repo_close(repo);
8221 got_object_commit_close(commit);
8223 got_worktree_close(worktree);
8225 const struct got_error *pack_err =
8226 got_repo_pack_fds_close(pack_fds);
8231 free(commit_id_str);
8238 for (i = 0; i < argc; i++)
8247 main(int argc, char *argv[])
8249 const struct got_error *error = NULL;
8250 const struct tog_cmd *cmd = NULL;
8251 int ch, hflag = 0, Vflag = 0;
8252 char **cmd_argv = NULL;
8253 static const struct option longopts[] = {
8254 { "version", no_argument, NULL, 'V' },
8257 char *diff_algo_str = NULL;
8259 setlocale(LC_CTYPE, "");
8261 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8281 got_version_print_str();
8286 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8294 /* Build an argument vector which runs a default command. */
8295 cmd = &tog_commands[0];
8297 cmd_argv = make_argv(argc, cmd->name);
8301 /* Did the user specify a command? */
8302 for (i = 0; i < nitems(tog_commands); i++) {
8303 if (strncmp(tog_commands[i].name, argv[0],
8304 strlen(argv[0])) == 0) {
8305 cmd = &tog_commands[i];
8311 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8312 if (diff_algo_str) {
8313 if (strcasecmp(diff_algo_str, "patience") == 0)
8314 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8315 if (strcasecmp(diff_algo_str, "myers") == 0)
8316 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8322 /* No command specified; try log with a path */
8323 error = tog_log_with_path(argc, argv);
8328 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8335 for (i = 0; i < argc; i++)
8340 if (error && error->code != GOT_ERR_CANCELLED)
8341 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);