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 "got_compat.h"
19 #include <sys/queue.h>
21 #include <sys/ioctl.h>
25 #if defined(__FreeBSD__) || defined(__APPLE__)
26 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
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 },
114 /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
115 enum tog_keymap_type {
116 TOG_KEYMAP_KEYS = -2,
132 #define HSPLIT_SCALE 0.3f /* default horizontal split scale */
134 #define TOG_EOF_STRING "(END)"
136 struct commit_queue_entry {
137 TAILQ_ENTRY(commit_queue_entry) entry;
138 struct got_object_id *id;
139 struct got_commit_object *commit;
142 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
143 struct commit_queue {
145 struct commit_queue_head head;
149 STAILQ_ENTRY(tog_color) entry;
153 STAILQ_HEAD(tog_colors, tog_color);
155 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
156 static struct got_reflist_object_id_map *tog_refs_idmap;
157 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
159 static const struct got_error *
160 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
161 struct got_reference* re2)
163 const char *name1 = got_ref_get_name(re1);
164 const char *name2 = got_ref_get_name(re2);
165 int isbackup1, isbackup2;
167 /* Sort backup refs towards the bottom of the list. */
168 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
169 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
170 if (!isbackup1 && isbackup2) {
173 } else if (isbackup1 && !isbackup2) {
178 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
182 static const struct got_error *
183 tog_load_refs(struct got_repository *repo, int sort_by_date)
185 const struct got_error *err;
187 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
188 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
193 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
200 if (tog_refs_idmap) {
201 got_reflist_object_id_map_free(tog_refs_idmap);
202 tog_refs_idmap = NULL;
204 got_ref_list_free(&tog_refs);
207 static const struct got_error *
208 add_color(struct tog_colors *colors, const char *pattern,
209 int idx, short color)
211 const struct got_error *err = NULL;
212 struct tog_color *tc;
215 if (idx < 1 || idx > COLOR_PAIRS - 1)
218 init_pair(idx, color, -1);
220 tc = calloc(1, sizeof(*tc));
222 return got_error_from_errno("calloc");
223 regerr = regcomp(&tc->regex, pattern,
224 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
226 static char regerr_msg[512];
227 static char err_msg[512];
228 regerror(regerr, &tc->regex, regerr_msg,
230 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
232 err = got_error_msg(GOT_ERR_REGEX, err_msg);
237 STAILQ_INSERT_HEAD(colors, tc, entry);
242 free_colors(struct tog_colors *colors)
244 struct tog_color *tc;
246 while (!STAILQ_EMPTY(colors)) {
247 tc = STAILQ_FIRST(colors);
248 STAILQ_REMOVE_HEAD(colors, entry);
254 static struct tog_color *
255 get_color(struct tog_colors *colors, int colorpair)
257 struct tog_color *tc = NULL;
259 STAILQ_FOREACH(tc, colors, entry) {
260 if (tc->colorpair == colorpair)
268 default_color_value(const char *envvar)
270 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
271 return COLOR_MAGENTA;
272 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
274 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
276 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
278 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
279 return COLOR_MAGENTA;
280 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
281 return COLOR_MAGENTA;
282 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
284 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
286 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
288 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
290 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
292 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
294 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
295 return COLOR_MAGENTA;
296 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
298 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
305 get_color_value(const char *envvar)
307 const char *val = getenv(envvar);
310 return default_color_value(envvar);
312 if (strcasecmp(val, "black") == 0)
314 if (strcasecmp(val, "red") == 0)
316 if (strcasecmp(val, "green") == 0)
318 if (strcasecmp(val, "yellow") == 0)
320 if (strcasecmp(val, "blue") == 0)
322 if (strcasecmp(val, "magenta") == 0)
323 return COLOR_MAGENTA;
324 if (strcasecmp(val, "cyan") == 0)
326 if (strcasecmp(val, "white") == 0)
328 if (strcasecmp(val, "default") == 0)
331 return default_color_value(envvar);
334 struct tog_diff_view_state {
335 struct got_object_id *id1, *id2;
336 const char *label1, *label2;
340 int first_displayed_line;
341 int last_displayed_line;
344 int ignore_whitespace;
346 struct got_repository *repo;
347 struct got_diff_line *lines;
352 /* passed from log or blame view; may be NULL */
353 struct tog_view *parent_view;
356 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
357 static volatile sig_atomic_t tog_thread_error;
359 struct tog_log_thread_args {
360 pthread_cond_t need_commits;
361 pthread_cond_t commit_loaded;
364 struct got_commit_graph *graph;
365 struct commit_queue *real_commits;
366 const char *in_repo_path;
367 struct got_object_id *start_id;
368 struct got_repository *repo;
372 struct commit_queue_entry **first_displayed_entry;
373 struct commit_queue_entry **selected_entry;
375 int *search_next_done;
379 regex_t *limit_regex;
380 struct commit_queue *limit_commits;
383 struct tog_log_view_state {
384 struct commit_queue *commits;
385 struct commit_queue_entry *first_displayed_entry;
386 struct commit_queue_entry *last_displayed_entry;
387 struct commit_queue_entry *selected_entry;
388 struct commit_queue real_commits;
393 struct got_repository *repo;
394 struct got_object_id *start_id;
397 struct tog_log_thread_args thread_args;
398 struct commit_queue_entry *matched_entry;
399 struct commit_queue_entry *search_entry;
400 struct tog_colors colors;
404 struct commit_queue limit_commits;
407 #define TOG_COLOR_DIFF_MINUS 1
408 #define TOG_COLOR_DIFF_PLUS 2
409 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
410 #define TOG_COLOR_DIFF_META 4
411 #define TOG_COLOR_TREE_SUBMODULE 5
412 #define TOG_COLOR_TREE_SYMLINK 6
413 #define TOG_COLOR_TREE_DIRECTORY 7
414 #define TOG_COLOR_TREE_EXECUTABLE 8
415 #define TOG_COLOR_COMMIT 9
416 #define TOG_COLOR_AUTHOR 10
417 #define TOG_COLOR_DATE 11
418 #define TOG_COLOR_REFS_HEADS 12
419 #define TOG_COLOR_REFS_TAGS 13
420 #define TOG_COLOR_REFS_REMOTES 14
421 #define TOG_COLOR_REFS_BACKUP 15
423 struct tog_blame_cb_args {
424 struct tog_blame_line *lines; /* one per line */
427 struct tog_view *view;
428 struct got_object_id *commit_id;
432 struct tog_blame_thread_args {
434 struct got_repository *repo;
435 struct tog_blame_cb_args *cb_args;
437 got_cancel_cb cancel_cb;
439 pthread_cond_t blame_complete;
445 struct tog_blame_line *lines;
449 struct tog_blame_thread_args thread_args;
450 struct tog_blame_cb_args cb_args;
455 struct tog_blame_view_state {
456 int first_displayed_line;
457 int last_displayed_line;
459 int last_diffed_line;
463 struct got_object_id_queue blamed_commits;
464 struct got_object_qid *blamed_commit;
466 struct got_repository *repo;
467 struct got_object_id *commit_id;
468 struct got_object_id *id_to_log;
469 struct tog_blame blame;
471 struct tog_colors colors;
474 struct tog_parent_tree {
475 TAILQ_ENTRY(tog_parent_tree) entry;
476 struct got_tree_object *tree;
477 struct got_tree_entry *first_displayed_entry;
478 struct got_tree_entry *selected_entry;
482 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
484 struct tog_tree_view_state {
486 struct got_object_id *commit_id;/* commit which this tree belongs to */
487 struct got_tree_object *root; /* the commit's root tree entry */
488 struct got_tree_object *tree; /* currently displayed (sub-)tree */
489 struct got_tree_entry *first_displayed_entry;
490 struct got_tree_entry *last_displayed_entry;
491 struct got_tree_entry *selected_entry;
492 int ndisplayed, selected, show_ids;
493 struct tog_parent_trees parents; /* parent trees of current sub-tree */
495 struct got_repository *repo;
496 struct got_tree_entry *matched_entry;
497 struct tog_colors colors;
500 struct tog_reflist_entry {
501 TAILQ_ENTRY(tog_reflist_entry) entry;
502 struct got_reference *ref;
506 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
508 struct tog_ref_view_state {
509 struct tog_reflist_head refs;
510 struct tog_reflist_entry *first_displayed_entry;
511 struct tog_reflist_entry *last_displayed_entry;
512 struct tog_reflist_entry *selected_entry;
513 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
514 struct got_repository *repo;
515 struct tog_reflist_entry *matched_entry;
516 struct tog_colors colors;
519 struct tog_help_view_state {
524 int first_displayed_line;
525 int last_displayed_line;
530 enum tog_keymap_type type;
533 #define GENERATE_HELP \
534 KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
535 KEY_("H F1", "Open view-specific help (double tap for all help)"), \
536 KEY_("k C-p Up", "Move cursor or page up one line"), \
537 KEY_("j C-n Down", "Move cursor or page down one line"), \
538 KEY_("C-b b PgUp", "Scroll the view up one page"), \
539 KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
540 KEY_("C-u u", "Scroll the view up one half page"), \
541 KEY_("C-d d", "Scroll the view down one half page"), \
542 KEY_("g", "Go to line N (default: first line)"), \
543 KEY_("Home =", "Go to the first line"), \
544 KEY_("G", "Go to line N (default: last line)"), \
545 KEY_("End *", "Go to the last line"), \
546 KEY_("l Right", "Scroll the view right"), \
547 KEY_("h Left", "Scroll the view left"), \
548 KEY_("$", "Scroll view to the rightmost position"), \
549 KEY_("0", "Scroll view to the leftmost position"), \
550 KEY_("-", "Decrease size of the focussed split"), \
551 KEY_("+", "Increase size of the focussed split"), \
552 KEY_("Tab", "Switch focus between views"), \
553 KEY_("F", "Toggle fullscreen mode"), \
554 KEY_("S", "Switch split-screen layout"), \
555 KEY_("/", "Open prompt to enter search term"), \
556 KEY_("n", "Find next line/token matching the current search term"), \
557 KEY_("N", "Find previous line/token matching the current search term"),\
558 KEY_("q", "Quit the focussed view; Quit help screen"), \
559 KEY_("Q", "Quit tog"), \
561 KEYMAP_("Log view", TOG_KEYMAP_LOG), \
562 KEY_("< ,", "Move cursor up one commit"), \
563 KEY_("> .", "Move cursor down one commit"), \
564 KEY_("Enter", "Open diff view of the selected commit"), \
565 KEY_("B", "Reload the log view and toggle display of merged commits"), \
566 KEY_("R", "Open ref view of all repository references"), \
567 KEY_("T", "Display tree view of the repository from the selected" \
569 KEY_("@", "Toggle between displaying author and committer name"), \
570 KEY_("&", "Open prompt to enter term to limit commits displayed"), \
571 KEY_("C-g Backspace", "Cancel current search or log operation"), \
572 KEY_("C-l", "Reload the log view with new commits in the repository"), \
574 KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
575 KEY_("K < ,", "Display diff of next line in the file/log entry"), \
576 KEY_("J > .", "Display diff of previous line in the file/log entry"), \
577 KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
578 KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
580 KEY_("(", "Go to the previous file in the diff"), \
581 KEY_(")", "Go to the next file in the diff"), \
582 KEY_("{", "Go to the previous hunk in the diff"), \
583 KEY_("}", "Go to the next hunk in the diff"), \
584 KEY_("[", "Decrease the number of context lines"), \
585 KEY_("]", "Increase the number of context lines"), \
586 KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
588 KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
589 KEY_("Enter", "Display diff view of the selected line's commit"), \
590 KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
591 KEY_("L", "Open log view for the currently selected annotated line"), \
592 KEY_("C", "Reload view with the previously blamed commit"), \
593 KEY_("c", "Reload view with the version of the file found in the" \
594 " selected line's commit"), \
595 KEY_("p", "Reload view with the version of the file found in the" \
596 " selected line's parent commit"), \
598 KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
599 KEY_("Enter", "Enter selected directory or open blame view of the" \
601 KEY_("L", "Open log view for the selected entry"), \
602 KEY_("R", "Open ref view of all repository references"), \
603 KEY_("i", "Show object IDs for all tree entries"), \
604 KEY_("Backspace", "Return to the parent directory"), \
606 KEYMAP_("Ref view", TOG_KEYMAP_REF), \
607 KEY_("Enter", "Display log view of the selected reference"), \
608 KEY_("T", "Display tree view of the selected reference"), \
609 KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
610 KEY_("m", "Toggle display of last modified date for each reference"), \
611 KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
612 KEY_("C-l", "Reload view with all repository references")
617 enum tog_keymap_type type;
620 /* curses io for tog regress */
628 static int using_mock_io;
630 #define TOG_KEY_SCRDUMP SHRT_MIN
633 * We implement two types of views: parent views and child views.
635 * The 'Tab' key switches focus between a parent view and its child view.
636 * Child views are shown side-by-side to their parent view, provided
637 * there is enough screen estate.
639 * When a new view is opened from within a parent view, this new view
640 * becomes a child view of the parent view, replacing any existing child.
642 * When a new view is opened from within a child view, this new view
643 * becomes a parent view which will obscure the views below until the
644 * user quits the new parent view by typing 'q'.
646 * This list of views contains parent views only.
647 * Child views are only pointed to by their parent view.
649 TAILQ_HEAD(tog_view_list_head, tog_view);
652 TAILQ_ENTRY(tog_view) entry;
655 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
656 int resized_y, resized_x; /* begin_y/x based on user resizing */
657 int maxx, x; /* max column and current start column */
658 int lines, cols; /* copies of LINES and COLS */
659 int nscrolled, offset; /* lines scrolled and hsplit line offset */
660 int gline, hiline; /* navigate to and highlight this nG line */
661 int ch, count; /* current keymap and count prefix */
662 int resized; /* set when in a resize event */
663 int focussed; /* Only set on one parent or child view at a time. */
665 struct tog_view *parent;
666 struct tog_view *child;
669 * This flag is initially set on parent views when a new child view
670 * is created. It gets toggled when the 'Tab' key switches focus
671 * between parent and child.
672 * The flag indicates whether focus should be passed on to our child
673 * view if this parent view gets picked for focus after another parent
674 * view was closed. This prevents child views from losing focus in such
679 enum tog_view_mode mode;
680 /* type-specific state */
681 enum tog_view_type type;
683 struct tog_diff_view_state diff;
684 struct tog_log_view_state log;
685 struct tog_blame_view_state blame;
686 struct tog_tree_view_state tree;
687 struct tog_ref_view_state ref;
688 struct tog_help_view_state help;
691 const struct got_error *(*show)(struct tog_view *);
692 const struct got_error *(*input)(struct tog_view **,
693 struct tog_view *, int);
694 const struct got_error *(*reset)(struct tog_view *);
695 const struct got_error *(*resize)(struct tog_view *, int);
696 const struct got_error *(*close)(struct tog_view *);
698 const struct got_error *(*search_start)(struct tog_view *);
699 const struct got_error *(*search_next)(struct tog_view *);
700 void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
701 int **, int **, int **, int **);
704 #define TOG_SEARCH_FORWARD 1
705 #define TOG_SEARCH_BACKWARD 2
706 int search_next_done;
707 #define TOG_SEARCH_HAVE_MORE 1
708 #define TOG_SEARCH_NO_MORE 2
709 #define TOG_SEARCH_HAVE_NONE 3
715 static const struct got_error *open_diff_view(struct tog_view *,
716 struct got_object_id *, struct got_object_id *,
717 const char *, const char *, int, int, int, struct tog_view *,
718 struct got_repository *);
719 static const struct got_error *show_diff_view(struct tog_view *);
720 static const struct got_error *input_diff_view(struct tog_view **,
721 struct tog_view *, int);
722 static const struct got_error *reset_diff_view(struct tog_view *);
723 static const struct got_error* close_diff_view(struct tog_view *);
724 static const struct got_error *search_start_diff_view(struct tog_view *);
725 static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
726 size_t *, int **, int **, int **, int **);
727 static const struct got_error *search_next_view_match(struct tog_view *);
729 static const struct got_error *open_log_view(struct tog_view *,
730 struct got_object_id *, struct got_repository *,
731 const char *, const char *, int);
732 static const struct got_error * show_log_view(struct tog_view *);
733 static const struct got_error *input_log_view(struct tog_view **,
734 struct tog_view *, int);
735 static const struct got_error *resize_log_view(struct tog_view *, int);
736 static const struct got_error *close_log_view(struct tog_view *);
737 static const struct got_error *search_start_log_view(struct tog_view *);
738 static const struct got_error *search_next_log_view(struct tog_view *);
740 static const struct got_error *open_blame_view(struct tog_view *, char *,
741 struct got_object_id *, struct got_repository *);
742 static const struct got_error *show_blame_view(struct tog_view *);
743 static const struct got_error *input_blame_view(struct tog_view **,
744 struct tog_view *, int);
745 static const struct got_error *reset_blame_view(struct tog_view *);
746 static const struct got_error *close_blame_view(struct tog_view *);
747 static const struct got_error *search_start_blame_view(struct tog_view *);
748 static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
749 size_t *, int **, int **, int **, int **);
751 static const struct got_error *open_tree_view(struct tog_view *,
752 struct got_object_id *, const char *, struct got_repository *);
753 static const struct got_error *show_tree_view(struct tog_view *);
754 static const struct got_error *input_tree_view(struct tog_view **,
755 struct tog_view *, int);
756 static const struct got_error *close_tree_view(struct tog_view *);
757 static const struct got_error *search_start_tree_view(struct tog_view *);
758 static const struct got_error *search_next_tree_view(struct tog_view *);
760 static const struct got_error *open_ref_view(struct tog_view *,
761 struct got_repository *);
762 static const struct got_error *show_ref_view(struct tog_view *);
763 static const struct got_error *input_ref_view(struct tog_view **,
764 struct tog_view *, int);
765 static const struct got_error *close_ref_view(struct tog_view *);
766 static const struct got_error *search_start_ref_view(struct tog_view *);
767 static const struct got_error *search_next_ref_view(struct tog_view *);
769 static const struct got_error *open_help_view(struct tog_view *,
771 static const struct got_error *show_help_view(struct tog_view *);
772 static const struct got_error *input_help_view(struct tog_view **,
773 struct tog_view *, int);
774 static const struct got_error *reset_help_view(struct tog_view *);
775 static const struct got_error* close_help_view(struct tog_view *);
776 static const struct got_error *search_start_help_view(struct tog_view *);
777 static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
778 size_t *, int **, int **, int **, int **);
780 static volatile sig_atomic_t tog_sigwinch_received;
781 static volatile sig_atomic_t tog_sigpipe_received;
782 static volatile sig_atomic_t tog_sigcont_received;
783 static volatile sig_atomic_t tog_sigint_received;
784 static volatile sig_atomic_t tog_sigterm_received;
787 tog_sigwinch(int signo)
789 tog_sigwinch_received = 1;
793 tog_sigpipe(int signo)
795 tog_sigpipe_received = 1;
799 tog_sigcont(int signo)
801 tog_sigcont_received = 1;
805 tog_sigint(int signo)
807 tog_sigint_received = 1;
811 tog_sigterm(int signo)
813 tog_sigterm_received = 1;
817 tog_fatal_signal_received(void)
819 return (tog_sigpipe_received ||
820 tog_sigint_received || tog_sigterm_received);
823 static const struct got_error *
824 view_close(struct tog_view *view)
826 const struct got_error *err = NULL, *child_err = NULL;
829 child_err = view_close(view->child);
833 err = view->close(view);
835 del_panel(view->panel);
837 delwin(view->window);
839 return err ? err : child_err;
842 static struct tog_view *
843 view_open(int nlines, int ncols, int begin_y, int begin_x,
844 enum tog_view_type type)
846 struct tog_view *view = calloc(1, sizeof(*view));
854 view->nlines = nlines ? nlines : LINES - begin_y;
855 view->ncols = ncols ? ncols : COLS - begin_x;
856 view->begin_y = begin_y;
857 view->begin_x = begin_x;
858 view->window = newwin(nlines, ncols, begin_y, begin_x);
859 if (view->window == NULL) {
863 view->panel = new_panel(view->window);
864 if (view->panel == NULL ||
865 set_panel_userptr(view->panel, view) != OK) {
870 keypad(view->window, TRUE);
875 view_split_begin_x(int begin_x)
877 if (begin_x > 0 || COLS < 120)
879 return (COLS - MAX(COLS / 2, 80));
882 /* XXX Stub till we decide what to do. */
884 view_split_begin_y(int lines)
886 return lines * HSPLIT_SCALE;
889 static const struct got_error *view_resize(struct tog_view *);
891 static const struct got_error *
892 view_splitscreen(struct tog_view *view)
894 const struct got_error *err = NULL;
896 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
897 if (view->resized_y && view->resized_y < view->lines)
898 view->begin_y = view->resized_y;
900 view->begin_y = view_split_begin_y(view->nlines);
902 } else if (!view->resized) {
903 if (view->resized_x && view->resized_x < view->cols - 1 &&
905 view->begin_x = view->resized_x;
907 view->begin_x = view_split_begin_x(0);
910 view->nlines = LINES - view->begin_y;
911 view->ncols = COLS - view->begin_x;
914 err = view_resize(view);
918 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
919 view->parent->nlines = view->begin_y;
921 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
922 return got_error_from_errno("mvwin");
927 static const struct got_error *
928 view_fullscreen(struct tog_view *view)
930 const struct got_error *err = NULL;
933 view->begin_y = view->resized ? view->begin_y : 0;
934 view->nlines = view->resized ? view->nlines : LINES;
938 err = view_resize(view);
942 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
943 return got_error_from_errno("mvwin");
949 view_is_parent_view(struct tog_view *view)
951 return view->parent == NULL;
955 view_is_splitscreen(struct tog_view *view)
957 return view->begin_x > 0 || view->begin_y > 0;
961 view_is_fullscreen(struct tog_view *view)
963 return view->nlines == LINES && view->ncols == COLS;
967 view_is_hsplit_top(struct tog_view *view)
969 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
970 view_is_splitscreen(view->child);
974 view_border(struct tog_view *view)
977 const struct tog_view *view_above;
980 return view_border(view->parent);
982 panel = panel_above(view->panel);
986 view_above = panel_userptr(panel);
987 if (view->mode == TOG_VIEW_SPLIT_HRZN)
988 mvwhline(view->window, view_above->begin_y - 1,
989 view->begin_x, ACS_HLINE, view->ncols);
991 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
992 ACS_VLINE, view->nlines);
995 static const struct got_error *view_init_hsplit(struct tog_view *, int);
996 static const struct got_error *request_log_commits(struct tog_view *);
997 static const struct got_error *offset_selection_down(struct tog_view *);
998 static void offset_selection_up(struct tog_view *);
999 static void view_get_split(struct tog_view *, int *, int *);
1001 static const struct got_error *
1002 view_resize(struct tog_view *view)
1004 const struct got_error *err = NULL;
1005 int dif, nlines, ncols;
1007 dif = LINES - view->lines; /* line difference */
1009 if (view->lines > LINES)
1010 nlines = view->nlines - (view->lines - LINES);
1012 nlines = view->nlines + (LINES - view->lines);
1013 if (view->cols > COLS)
1014 ncols = view->ncols - (view->cols - COLS);
1016 ncols = view->ncols + (COLS - view->cols);
1019 int hs = view->child->begin_y;
1021 if (!view_is_fullscreen(view))
1022 view->child->begin_x = view_split_begin_x(view->begin_x);
1023 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1024 view->child->begin_x == 0) {
1027 view_fullscreen(view->child);
1028 if (view->child->focussed)
1029 show_panel(view->child->panel);
1031 show_panel(view->panel);
1033 ncols = view->child->begin_x;
1035 view_splitscreen(view->child);
1036 show_panel(view->child->panel);
1039 * XXX This is ugly and needs to be moved into the above
1040 * logic but "works" for now and my attempts at moving it
1041 * break either 'tab' or 'F' key maps in horizontal splits.
1044 err = view_splitscreen(view->child);
1047 if (dif < 0) { /* top split decreased */
1048 err = offset_selection_down(view);
1055 show_panel(view->child->panel);
1056 nlines = view->nlines;
1058 } else if (view->parent == NULL)
1061 if (view->resize && dif > 0) {
1062 err = view->resize(view, dif);
1067 if (wresize(view->window, nlines, ncols) == ERR)
1068 return got_error_from_errno("wresize");
1069 if (replace_panel(view->panel, view->window) == ERR)
1070 return got_error_from_errno("replace_panel");
1071 wclear(view->window);
1073 view->nlines = nlines;
1074 view->ncols = ncols;
1075 view->lines = LINES;
1081 static const struct got_error *
1082 resize_log_view(struct tog_view *view, int increase)
1084 struct tog_log_view_state *s = &view->state.log;
1085 const struct got_error *err = NULL;
1088 if (s->selected_entry)
1089 n = s->selected_entry->idx + view->lines - s->selected;
1092 * Request commits to account for the increased
1093 * height so we have enough to populate the view.
1095 if (s->commits->ncommits < n) {
1096 view->nscrolled = n - s->commits->ncommits + increase + 1;
1097 err = request_log_commits(view);
1104 view_adjust_offset(struct tog_view *view, int n)
1109 if (view->parent && view->parent->offset) {
1110 if (view->parent->offset + n >= 0)
1111 view->parent->offset += n;
1113 view->parent->offset = 0;
1114 } else if (view->offset) {
1115 if (view->offset - n >= 0)
1122 static const struct got_error *
1123 view_resize_split(struct tog_view *view, int resize)
1125 const struct got_error *err = NULL;
1126 struct tog_view *v = NULL;
1133 if (!v->child || !view_is_splitscreen(v->child))
1136 v->resized = v->child->resized = resize; /* lock for resize event */
1138 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1139 if (v->child->resized_y)
1140 v->child->begin_y = v->child->resized_y;
1142 v->child->begin_y -= resize;
1144 v->child->begin_y += resize;
1145 if (v->child->begin_y < 3) {
1147 v->child->begin_y = 3;
1148 } else if (v->child->begin_y > LINES - 1) {
1150 v->child->begin_y = LINES - 1;
1153 v->child->ncols = COLS;
1154 view_adjust_offset(view, resize);
1155 err = view_init_hsplit(v, v->child->begin_y);
1158 v->child->resized_y = v->child->begin_y;
1160 if (v->child->resized_x)
1161 v->child->begin_x = v->child->resized_x;
1163 v->child->begin_x -= resize;
1165 v->child->begin_x += resize;
1166 if (v->child->begin_x < 11) {
1168 v->child->begin_x = 11;
1169 } else if (v->child->begin_x > COLS - 1) {
1171 v->child->begin_x = COLS - 1;
1173 v->child->resized_x = v->child->begin_x;
1176 v->child->mode = v->mode;
1177 v->child->nlines = v->lines - v->child->begin_y;
1178 v->child->ncols = v->cols - v->child->begin_x;
1181 err = view_fullscreen(v);
1184 err = view_splitscreen(v->child);
1188 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1189 err = offset_selection_down(v->child);
1195 err = v->resize(v, 0);
1196 else if (v->child->resize)
1197 err = v->child->resize(v->child, 0);
1199 v->resized = v->child->resized = 0;
1205 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1207 struct tog_view *v = src->child ? src->child : src;
1209 dst->resized_x = v->resized_x;
1210 dst->resized_y = v->resized_y;
1213 static const struct got_error *
1214 view_close_child(struct tog_view *view)
1216 const struct got_error *err = NULL;
1218 if (view->child == NULL)
1221 err = view_close(view->child);
1226 static const struct got_error *
1227 view_set_child(struct tog_view *view, struct tog_view *child)
1229 const struct got_error *err = NULL;
1231 view->child = child;
1232 child->parent = view;
1234 err = view_resize(view);
1238 if (view->child->resized_x || view->child->resized_y)
1239 err = view_resize_split(view, 0);
1244 static const struct got_error *view_dispatch_request(struct tog_view **,
1245 struct tog_view *, enum tog_view_type, int, int);
1247 static const struct got_error *
1248 view_request_new(struct tog_view **requested, struct tog_view *view,
1249 enum tog_view_type request)
1251 struct tog_view *new_view = NULL;
1252 const struct got_error *err;
1257 if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1258 view_get_split(view, &y, &x);
1260 err = view_dispatch_request(&new_view, view, request, y, x);
1264 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1265 request != TOG_VIEW_HELP) {
1266 err = view_init_hsplit(view, y);
1272 new_view->focussed = 1;
1273 new_view->mode = view->mode;
1274 new_view->nlines = request == TOG_VIEW_HELP ?
1275 view->lines : view->lines - y;
1277 if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1278 view_transfer_size(new_view, view);
1279 err = view_close_child(view);
1282 err = view_set_child(view, new_view);
1285 view->focus_child = 1;
1287 *requested = new_view;
1293 tog_resizeterm(void)
1296 struct winsize size;
1298 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1299 cols = 80; /* Default */
1303 lines = size.ws_row;
1305 resize_term(lines, cols);
1308 static const struct got_error *
1309 view_search_start(struct tog_view *view, int fast_refresh)
1311 const struct got_error *err = NULL;
1312 struct tog_view *v = view;
1316 if (view->search_started) {
1317 regfree(&view->regex);
1318 view->searching = 0;
1319 memset(&view->regmatch, 0, sizeof(view->regmatch));
1321 view->search_started = 0;
1323 if (view->nlines < 1)
1326 if (view_is_hsplit_top(view))
1328 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1331 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1332 wclrtoeol(v->window);
1334 nodelay(v->window, FALSE); /* block for search term input */
1337 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1338 wrefresh(v->window);
1341 nodelay(v->window, TRUE);
1342 if (!fast_refresh && !using_mock_io)
1347 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1348 err = view->search_start(view);
1350 regfree(&view->regex);
1353 view->search_started = 1;
1354 view->searching = TOG_SEARCH_FORWARD;
1355 view->search_next_done = 0;
1356 view->search_next(view);
1362 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1363 static const struct got_error *
1364 switch_split(struct tog_view *view)
1366 const struct got_error *err = NULL;
1367 struct tog_view *v = NULL;
1374 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1375 v->mode = TOG_VIEW_SPLIT_VERT;
1377 v->mode = TOG_VIEW_SPLIT_HRZN;
1381 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1382 v->mode = TOG_VIEW_SPLIT_NONE;
1384 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1385 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1386 v->child->begin_y = v->child->resized_y;
1387 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1388 v->child->begin_x = v->child->resized_x;
1391 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1393 v->child->ncols = COLS;
1394 v->child->nscrolled = LINES - v->child->nlines;
1396 err = view_init_hsplit(v, v->child->begin_y);
1400 v->child->mode = v->mode;
1401 v->child->nlines = v->lines - v->child->begin_y;
1404 err = view_fullscreen(v);
1407 err = view_splitscreen(v->child);
1411 if (v->mode == TOG_VIEW_SPLIT_NONE)
1412 v->mode = TOG_VIEW_SPLIT_VERT;
1413 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1414 err = offset_selection_down(v);
1417 err = offset_selection_down(v->child);
1421 offset_selection_up(v);
1422 offset_selection_up(v->child);
1425 err = v->resize(v, 0);
1426 else if (v->child->resize)
1427 err = v->child->resize(v->child, 0);
1433 * Strip trailing whitespace from str starting at byte *n;
1434 * if *n < 0, use strlen(str). Return new str length in *n.
1437 strip_trailing_ws(char *str, int *n)
1441 if (str == NULL || *str == '\0')
1447 while (x-- > 0 && isspace((unsigned char)str[x]))
1454 * Extract visible substring of line y from the curses screen
1455 * and strip trailing whitespace. If vline is set, overwrite
1456 * line[vline] with '|' because the ACS_VLINE character is
1457 * written out as 'x'. Write the line to file f.
1459 static const struct got_error *
1460 view_write_line(FILE *f, int y, int vline)
1462 char line[COLS * MB_LEN_MAX]; /* allow for multibyte chars */
1465 r = mvwinnstr(curscr, y, 0, line, sizeof(line));
1467 return got_error_fmt(GOT_ERR_RANGE,
1468 "failed to extract line %d", y);
1471 * In some views, lines are padded with blanks to COLS width.
1472 * Strip them so we can diff without the -b flag when testing.
1474 strip_trailing_ws(line, &r);
1479 w = fprintf(f, "%s\n", line);
1480 if (w != r + 1) /* \n */
1481 return got_ferror(f, GOT_ERR_IO);
1487 * Capture the visible curses screen by writing each line to the
1488 * file at the path set via the TOG_SCR_DUMP environment variable.
1490 static const struct got_error *
1491 screendump(struct tog_view *view)
1493 const struct got_error *err;
1496 err = got_opentemp_truncate(tog_io.sdump);
1500 if ((view->child && view->child->begin_x) ||
1501 (view->parent && view->begin_x)) {
1502 int ncols = view->child ? view->ncols : view->parent->ncols;
1504 /* vertical splitscreen */
1505 for (i = 0; i < view->nlines; ++i) {
1506 err = view_write_line(tog_io.sdump, i, ncols - 1);
1513 /* fullscreen or horizontal splitscreen */
1514 if ((view->child && view->child->begin_y) ||
1515 (view->parent && view->begin_y)) /* hsplit */
1516 hline = view->child ?
1517 view->child->begin_y : view->begin_y;
1519 for (i = 0; i < view->lines; i++) {
1520 if (hline && i == hline - 1) {
1523 /* ACS_HLINE writes out as 'q', overwrite it */
1524 for (c = 0; c < view->cols; ++c)
1525 fputc('-', tog_io.sdump);
1526 fputc('\n', tog_io.sdump);
1530 err = view_write_line(tog_io.sdump, i, 0);
1541 * Compute view->count from numeric input. Assign total to view->count and
1542 * return first non-numeric key entered.
1545 get_compound_key(struct tog_view *view, int c)
1547 struct tog_view *v = view;
1550 if (view_is_hsplit_top(view))
1552 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1556 cbreak(); /* block for input */
1557 nodelay(view->window, FALSE);
1558 wmove(v->window, v->nlines - 1, 0);
1559 wclrtoeol(v->window);
1560 waddch(v->window, ':');
1563 x = getcurx(v->window);
1564 if (x != ERR && x < view->ncols) {
1565 waddch(v->window, c);
1566 wrefresh(v->window);
1570 * Don't overflow. Max valid request should be the greatest
1571 * between the longest and total lines; cap at 10 million.
1576 n = n * 10 + (c - '0');
1577 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1579 if (c == 'G' || c == 'g') { /* nG key map */
1580 view->gline = view->hiline = n;
1585 /* Massage excessive or inapplicable values at the input handler. */
1592 action_report(struct tog_view *view)
1594 struct tog_view *v = view;
1596 if (view_is_hsplit_top(view))
1598 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1601 wmove(v->window, v->nlines - 1, 0);
1602 wclrtoeol(v->window);
1603 wprintw(v->window, ":%s", view->action);
1604 wrefresh(v->window);
1607 * Clear action status report. Only clear in blame view
1608 * once annotating is complete, otherwise it's too fast.
1610 if (view->type == TOG_VIEW_BLAME) {
1611 if (view->state.blame.blame_complete)
1612 view->action = NULL;
1614 view->action = NULL;
1618 * Read the next line from the test script and assign
1619 * key instruction to *ch. If at EOF, set the *done flag.
1621 static const struct got_error *
1622 tog_read_script_key(FILE *script, struct tog_view *view, int *ch, int *done)
1624 const struct got_error *err = NULL;
1628 if (view->count && --view->count) {
1634 if (getline(&line, &linesz, script) == -1) {
1639 err = got_ferror(script, GOT_ERR_IO);
1644 if (strncasecmp(line, "WAIT_FOR_UI", 11) == 0)
1645 tog_io.wait_for_ui = 1;
1646 else if (strncasecmp(line, "KEY_ENTER", 9) == 0)
1648 else if (strncasecmp(line, "KEY_RIGHT", 9) == 0)
1650 else if (strncasecmp(line, "KEY_LEFT", 8) == 0)
1652 else if (strncasecmp(line, "KEY_DOWN", 8) == 0)
1654 else if (strncasecmp(line, "KEY_UP", 6) == 0)
1656 else if (strncasecmp(line, "TAB", 3) == 0)
1658 else if (strncasecmp(line, "SCREENDUMP", 10) == 0)
1659 *ch = TOG_KEY_SCRDUMP;
1660 else if (isdigit((unsigned char)*line)) {
1663 while (isdigit((unsigned char)*t))
1665 view->ch = *ch = *t;
1667 /* ignore error, view->count is 0 if instruction is invalid */
1668 view->count = strtonum(line, 0, INT_MAX, NULL);
1677 static const struct got_error *
1678 view_input(struct tog_view **new, int *done, struct tog_view *view,
1679 struct tog_view_list_head *views, int fast_refresh)
1681 const struct got_error *err = NULL;
1688 action_report(view);
1690 /* Clear "no matches" indicator. */
1691 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1692 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1693 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1697 if (view->searching && !view->search_next_done) {
1698 errcode = pthread_mutex_unlock(&tog_mutex);
1700 return got_error_set_errno(errcode,
1701 "pthread_mutex_unlock");
1703 errcode = pthread_mutex_lock(&tog_mutex);
1705 return got_error_set_errno(errcode,
1706 "pthread_mutex_lock");
1707 view->search_next(view);
1711 /* Allow threads to make progress while we are waiting for input. */
1712 errcode = pthread_mutex_unlock(&tog_mutex);
1714 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1716 if (using_mock_io) {
1717 err = tog_read_script_key(tog_io.f, view, &ch, done);
1719 errcode = pthread_mutex_lock(&tog_mutex);
1722 } else if (view->count && --view->count) {
1724 nodelay(view->window, TRUE);
1725 ch = wgetch(view->window);
1726 /* let C-g or backspace abort unfinished count */
1727 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1732 ch = wgetch(view->window);
1733 if (ch >= '1' && ch <= '9')
1734 view->ch = ch = get_compound_key(view, ch);
1736 if (view->hiline && ch != ERR && ch != 0)
1737 view->hiline = 0; /* key pressed, clear line highlight */
1738 nodelay(view->window, TRUE);
1739 errcode = pthread_mutex_lock(&tog_mutex);
1741 return got_error_set_errno(errcode, "pthread_mutex_lock");
1743 if (tog_sigwinch_received || tog_sigcont_received) {
1745 tog_sigwinch_received = 0;
1746 tog_sigcont_received = 0;
1747 TAILQ_FOREACH(v, views, entry) {
1748 err = view_resize(v);
1751 err = v->input(new, v, KEY_RESIZE);
1755 err = view_resize(v->child);
1758 err = v->child->input(new, v->child,
1762 if (v->child->resized_x || v->child->resized_y) {
1763 err = view_resize_split(v, 0);
1775 if (view->type == TOG_VIEW_HELP)
1776 err = view->reset(view);
1778 err = view_request_new(new, view, TOG_VIEW_HELP);
1784 view->child->focussed = 1;
1785 view->focus_child = 1;
1786 } else if (view->parent) {
1788 view->parent->focussed = 1;
1789 view->parent->focus_child = 0;
1790 if (!view_is_splitscreen(view)) {
1791 if (view->parent->resize) {
1792 err = view->parent->resize(view->parent,
1797 offset_selection_up(view->parent);
1798 err = view_fullscreen(view->parent);
1805 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1806 if (view->parent->resize) {
1807 /* might need more commits to fill fullscreen */
1808 err = view->parent->resize(view->parent, 0);
1812 offset_selection_up(view->parent);
1814 err = view->input(new, view, ch);
1822 if (view_is_parent_view(view)) {
1823 if (view->child == NULL)
1825 if (view_is_splitscreen(view->child)) {
1827 view->child->focussed = 1;
1828 err = view_fullscreen(view->child);
1830 err = view_splitscreen(view->child);
1832 err = view_resize_split(view, 0);
1836 err = view->child->input(new, view->child,
1839 if (view_is_splitscreen(view)) {
1840 view->parent->focussed = 0;
1842 err = view_fullscreen(view);
1844 err = view_splitscreen(view);
1845 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1846 err = view_resize(view->parent);
1848 err = view_resize_split(view, 0);
1852 err = view->input(new, view, KEY_RESIZE);
1857 err = view->resize(view, 0);
1862 if (view->parent->resize) {
1863 err = view->parent->resize(view->parent, 0);
1867 err = offset_selection_down(view->parent);
1871 err = offset_selection_down(view);
1875 err = switch_split(view);
1878 err = view_resize_split(view, -1);
1881 err = view_resize_split(view, 1);
1887 if (view->search_start)
1888 view_search_start(view, fast_refresh);
1890 err = view->input(new, view, ch);
1894 if (view->search_started && view->search_next) {
1895 view->searching = (ch == 'n' ?
1896 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1897 view->search_next_done = 0;
1898 view->search_next(view);
1900 err = view->input(new, view, ch);
1903 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1904 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1905 view->action = "Patience diff algorithm";
1907 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1908 view->action = "Myers diff algorithm";
1910 TAILQ_FOREACH(v, views, entry) {
1916 if (v->child && v->child->reset) {
1917 err = v->child->reset(v->child);
1923 case TOG_KEY_SCRDUMP:
1924 err = screendump(view);
1927 err = view->input(new, view, ch);
1935 view_needs_focus_indication(struct tog_view *view)
1937 if (view_is_parent_view(view)) {
1938 if (view->child == NULL || view->child->focussed)
1940 if (!view_is_splitscreen(view->child))
1942 } else if (!view_is_splitscreen(view))
1945 return view->focussed;
1948 static const struct got_error *
1951 const struct got_error *err = NULL;
1953 if (tog_io.cin && fclose(tog_io.cin) == EOF)
1954 err = got_ferror(tog_io.cin, GOT_ERR_IO);
1955 if (tog_io.cout && fclose(tog_io.cout) == EOF && err == NULL)
1956 err = got_ferror(tog_io.cout, GOT_ERR_IO);
1957 if (tog_io.f && fclose(tog_io.f) == EOF && err == NULL)
1958 err = got_ferror(tog_io.f, GOT_ERR_IO);
1959 if (tog_io.sdump && fclose(tog_io.sdump) == EOF && err == NULL)
1960 err = got_ferror(tog_io.sdump, GOT_ERR_IO);
1965 static const struct got_error *
1966 view_loop(struct tog_view *view)
1968 const struct got_error *err = NULL;
1969 struct tog_view_list_head views;
1970 struct tog_view *new_view;
1972 int fast_refresh = 10;
1973 int done = 0, errcode;
1975 mode = getenv("TOG_VIEW_SPLIT_MODE");
1976 if (!mode || !(*mode == 'h' || *mode == 'H'))
1977 view->mode = TOG_VIEW_SPLIT_VERT;
1979 view->mode = TOG_VIEW_SPLIT_HRZN;
1981 errcode = pthread_mutex_lock(&tog_mutex);
1983 return got_error_set_errno(errcode, "pthread_mutex_lock");
1986 TAILQ_INSERT_HEAD(&views, view, entry);
1989 err = view->show(view);
1994 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1995 !tog_fatal_signal_received()) {
1996 /* Refresh fast during initialization, then become slower. */
1997 if (fast_refresh && --fast_refresh == 0 && !using_mock_io)
1998 halfdelay(10); /* switch to once per second */
2000 err = view_input(&new_view, &done, view, &views, fast_refresh);
2004 if (view->dying && view == TAILQ_FIRST(&views) &&
2005 TAILQ_NEXT(view, entry) == NULL)
2011 * When we quit, scroll the screen up a single line
2012 * so we don't lose any information.
2014 TAILQ_FOREACH(v, &views, entry) {
2015 wmove(v->window, 0, 0);
2016 wdeleteln(v->window);
2017 wnoutrefresh(v->window);
2018 if (v->child && !view_is_fullscreen(v)) {
2019 wmove(v->child->window, 0, 0);
2020 wdeleteln(v->child->window);
2021 wnoutrefresh(v->child->window);
2028 struct tog_view *v, *prev = NULL;
2030 if (view_is_parent_view(view))
2031 prev = TAILQ_PREV(view, tog_view_list_head,
2033 else if (view->parent)
2034 prev = view->parent;
2037 view->parent->child = NULL;
2038 view->parent->focus_child = 0;
2039 /* Restore fullscreen line height. */
2040 view->parent->nlines = view->parent->lines;
2041 err = view_resize(view->parent);
2044 /* Make resized splits persist. */
2045 view_transfer_size(view->parent, view);
2047 TAILQ_REMOVE(&views, view, entry);
2049 err = view_close(view);
2054 TAILQ_FOREACH(v, &views, entry) {
2058 if (view == NULL && new_view == NULL) {
2059 /* No view has focus. Try to pick one. */
2062 else if (!TAILQ_EMPTY(&views)) {
2063 view = TAILQ_LAST(&views,
2064 tog_view_list_head);
2067 if (view->focus_child) {
2068 view->child->focussed = 1;
2076 struct tog_view *v, *t;
2077 /* Only allow one parent view per type. */
2078 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
2079 if (v->type != new_view->type)
2081 TAILQ_REMOVE(&views, v, entry);
2082 err = view_close(v);
2087 TAILQ_INSERT_TAIL(&views, new_view, entry);
2090 if (view && !done) {
2091 if (view_is_parent_view(view)) {
2092 if (view->child && view->child->focussed)
2095 if (view->parent && view->parent->focussed)
2096 view = view->parent;
2098 show_panel(view->panel);
2099 if (view->child && view_is_splitscreen(view->child))
2100 show_panel(view->child->panel);
2101 if (view->parent && view_is_splitscreen(view)) {
2102 err = view->parent->show(view->parent);
2106 err = view->show(view);
2110 err = view->child->show(view->child);
2119 while (!TAILQ_EMPTY(&views)) {
2120 const struct got_error *close_err;
2121 view = TAILQ_FIRST(&views);
2122 TAILQ_REMOVE(&views, view, entry);
2123 close_err = view_close(view);
2124 if (close_err && err == NULL)
2128 errcode = pthread_mutex_unlock(&tog_mutex);
2129 if (errcode && err == NULL)
2130 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2140 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
2145 /* Create newly allocated wide-character string equivalent to a byte string. */
2146 static const struct got_error *
2147 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
2150 const struct got_error *err = NULL;
2153 *wlen = mbstowcs(NULL, s, 0);
2154 if (*wlen == (size_t)-1) {
2156 if (errno != EILSEQ)
2157 return got_error_from_errno("mbstowcs");
2159 /* byte string invalid in current encoding; try to "fix" it */
2160 err = got_mbsavis(&vis, &vislen, s);
2163 *wlen = mbstowcs(NULL, vis, 0);
2164 if (*wlen == (size_t)-1) {
2165 err = got_error_from_errno("mbstowcs"); /* give up */
2170 *ws = calloc(*wlen + 1, sizeof(**ws));
2172 err = got_error_from_errno("calloc");
2176 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
2177 err = got_error_from_errno("mbstowcs");
2188 static const struct got_error *
2189 expand_tab(char **ptr, const char *src)
2192 size_t len, n, idx = 0, sz = 0;
2195 n = len = strlen(src);
2196 dst = malloc(n + 1);
2198 return got_error_from_errno("malloc");
2200 while (idx < len && src[idx]) {
2201 const char c = src[idx];
2204 size_t nb = TABSIZE - sz % TABSIZE;
2207 p = realloc(dst, n + nb);
2210 return got_error_from_errno("realloc");
2215 memset(dst + sz, ' ', nb);
2218 dst[sz++] = src[idx];
2228 * Advance at most n columns from wline starting at offset off.
2229 * Return the index to the first character after the span operation.
2230 * Return the combined column width of all spanned wide character in
2234 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2236 int width, i, cols = 0;
2243 for (i = off; wline[i] != L'\0'; ++i) {
2244 if (wline[i] == L'\t')
2245 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2247 width = wcwidth(wline[i]);
2254 if (cols + width > n)
2264 * Format a line for display, ensuring that it won't overflow a width limit.
2265 * With scrolling, the width returned refers to the scrolled version of the
2266 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2268 static const struct got_error *
2269 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2270 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2272 const struct got_error *err = NULL;
2274 wchar_t *wline = NULL;
2283 err = expand_tab(&exstr, line);
2288 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2293 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2295 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2296 wline[wlen - 1] = L'\0';
2299 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2300 wline[wlen - 1] = L'\0';
2304 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2310 *scrollxp = scrollx;
2318 static const struct got_error*
2319 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2320 struct got_object_id *id, struct got_repository *repo)
2322 static const struct got_error *err = NULL;
2323 struct got_reflist_entry *re;
2332 TAILQ_FOREACH(re, refs, entry) {
2333 struct got_tag_object *tag = NULL;
2334 struct got_object_id *ref_id;
2337 name = got_ref_get_name(re->ref);
2338 if (strcmp(name, GOT_REF_HEAD) == 0)
2340 if (strncmp(name, "refs/", 5) == 0)
2342 if (strncmp(name, "got/", 4) == 0 &&
2343 strncmp(name, "got/backup/", 11) != 0)
2345 if (strncmp(name, "heads/", 6) == 0)
2347 if (strncmp(name, "remotes/", 8) == 0) {
2349 s = strstr(name, "/" GOT_REF_HEAD);
2350 if (s != NULL && s[strlen(s)] == '\0')
2353 err = got_ref_resolve(&ref_id, repo, re->ref);
2356 if (strncmp(name, "tags/", 5) == 0) {
2357 err = got_object_open_as_tag(&tag, repo, ref_id);
2359 if (err->code != GOT_ERR_OBJ_TYPE) {
2363 /* Ref points at something other than a tag. */
2368 cmp = got_object_id_cmp(tag ?
2369 got_object_tag_get_object_id(tag) : ref_id, id);
2372 got_object_tag_close(tag);
2376 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2377 s ? ", " : "", name) == -1) {
2378 err = got_error_from_errno("asprintf");
2389 static const struct got_error *
2390 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2395 smallerthan = strchr(author, '<');
2396 if (smallerthan && smallerthan[1] != '\0')
2397 author = smallerthan + 1;
2398 author[strcspn(author, "@>")] = '\0';
2399 return format_line(wauthor, author_width, NULL, author, 0, limit,
2403 static const struct got_error *
2404 draw_commit(struct tog_view *view, struct got_commit_object *commit,
2405 struct got_object_id *id, const size_t date_display_cols,
2406 int author_display_cols)
2408 struct tog_log_view_state *s = &view->state.log;
2409 const struct got_error *err = NULL;
2410 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2411 char *refs_str = NULL;
2412 char *logmsg0 = NULL, *logmsg = NULL;
2413 char *author = NULL;
2414 wchar_t *wlogmsg = NULL, *wauthor = NULL;
2415 int author_width, logmsg_width;
2416 size_t wrefstr_len = 0;
2417 char *newline, *line = NULL;
2418 int col, limit, scrollx;
2419 const int avail = view->ncols;
2421 time_t committer_time;
2422 struct tog_color *tc;
2423 struct got_reflist_head *refs;
2425 committer_time = got_object_commit_get_committer_time(commit);
2426 if (gmtime_r(&committer_time, &tm) == NULL)
2427 return got_error_from_errno("gmtime_r");
2428 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2429 return got_error(GOT_ERR_NO_SPACE);
2431 if (avail <= date_display_cols)
2432 limit = MIN(sizeof(datebuf) - 1, avail);
2434 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2435 tc = get_color(&s->colors, TOG_COLOR_DATE);
2437 wattr_on(view->window,
2438 COLOR_PAIR(tc->colorpair), NULL);
2439 waddnstr(view->window, datebuf, limit);
2441 wattr_off(view->window,
2442 COLOR_PAIR(tc->colorpair), NULL);
2449 err = got_object_id_str(&id_str, id);
2452 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2454 wattr_on(view->window,
2455 COLOR_PAIR(tc->colorpair), NULL);
2456 wprintw(view->window, "%.8s ", id_str);
2458 wattr_off(view->window,
2459 COLOR_PAIR(tc->colorpair), NULL);
2466 if (s->use_committer)
2467 author = strdup(got_object_commit_get_committer(commit));
2469 author = strdup(got_object_commit_get_author(commit));
2470 if (author == NULL) {
2471 err = got_error_from_errno("strdup");
2474 err = format_author(&wauthor, &author_width, author, avail - col, col);
2477 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2479 wattr_on(view->window,
2480 COLOR_PAIR(tc->colorpair), NULL);
2481 waddwstr(view->window, wauthor);
2482 col += author_width;
2483 while (col < avail && author_width < author_display_cols + 2) {
2484 waddch(view->window, ' ');
2489 wattr_off(view->window,
2490 COLOR_PAIR(tc->colorpair), NULL);
2494 err = got_object_commit_get_logmsg(&logmsg0, commit);
2498 while (*logmsg == '\n')
2500 newline = strchr(logmsg, '\n');
2504 /* Prepend reference labels to log message if possible .*/
2505 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, id);
2506 err = build_refs_str(&refs_str, refs, id, s->repo);
2514 * The length of this wide-char sub-string will be
2515 * needed later for colorization.
2517 err = mbs2ws(&ws, &wrefstr_len, refs_str);
2522 wrefstr_len += 2; /* account for '[' and ']' */
2524 if (asprintf(&newlogmsg, "[%s] %s", refs_str, logmsg) == -1) {
2525 err = got_error_from_errno("asprintf");
2530 logmsg0 = newlogmsg;
2534 limit = avail - col;
2535 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2536 limit--; /* for the border */
2537 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2541 if (wrefstr_len > 0 && scrollx < wrefstr_len) {
2542 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2544 wattr_on(view->window,
2545 COLOR_PAIR(tc->colorpair), NULL);
2546 waddnwstr(view->window, &wlogmsg[scrollx],
2547 wrefstr_len - scrollx);
2549 wattr_off(view->window,
2550 COLOR_PAIR(tc->colorpair), NULL);
2551 waddwstr(view->window, &wlogmsg[wrefstr_len]);
2553 waddwstr(view->window, &wlogmsg[scrollx]);
2554 col += MAX(logmsg_width, 0);
2555 while (col < avail) {
2556 waddch(view->window, ' ');
2569 static struct commit_queue_entry *
2570 alloc_commit_queue_entry(struct got_commit_object *commit,
2571 struct got_object_id *id)
2573 struct commit_queue_entry *entry;
2574 struct got_object_id *dup;
2576 entry = calloc(1, sizeof(*entry));
2580 dup = got_object_id_dup(id);
2587 entry->commit = commit;
2592 pop_commit(struct commit_queue *commits)
2594 struct commit_queue_entry *entry;
2596 entry = TAILQ_FIRST(&commits->head);
2597 TAILQ_REMOVE(&commits->head, entry, entry);
2598 got_object_commit_close(entry->commit);
2599 commits->ncommits--;
2605 free_commits(struct commit_queue *commits)
2607 while (!TAILQ_EMPTY(&commits->head))
2608 pop_commit(commits);
2611 static const struct got_error *
2612 match_commit(int *have_match, struct got_object_id *id,
2613 struct got_commit_object *commit, regex_t *regex)
2615 const struct got_error *err = NULL;
2616 regmatch_t regmatch;
2617 char *id_str = NULL, *logmsg = NULL;
2621 err = got_object_id_str(&id_str, id);
2625 err = got_object_commit_get_logmsg(&logmsg, commit);
2629 if (regexec(regex, got_object_commit_get_author(commit), 1,
2630 ®match, 0) == 0 ||
2631 regexec(regex, got_object_commit_get_committer(commit), 1,
2632 ®match, 0) == 0 ||
2633 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2634 regexec(regex, logmsg, 1, ®match, 0) == 0)
2642 static const struct got_error *
2643 queue_commits(struct tog_log_thread_args *a)
2645 const struct got_error *err = NULL;
2648 * We keep all commits open throughout the lifetime of the log
2649 * view in order to avoid having to re-fetch commits from disk
2650 * while updating the display.
2653 struct got_object_id id;
2654 struct got_commit_object *commit;
2655 struct commit_queue_entry *entry;
2656 int limit_match = 0;
2659 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2664 err = got_object_open_as_commit(&commit, a->repo, &id);
2667 entry = alloc_commit_queue_entry(commit, &id);
2668 if (entry == NULL) {
2669 err = got_error_from_errno("alloc_commit_queue_entry");
2673 errcode = pthread_mutex_lock(&tog_mutex);
2675 err = got_error_set_errno(errcode,
2676 "pthread_mutex_lock");
2680 entry->idx = a->real_commits->ncommits;
2681 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2682 a->real_commits->ncommits++;
2685 err = match_commit(&limit_match, &id, commit,
2691 struct commit_queue_entry *matched;
2693 matched = alloc_commit_queue_entry(
2694 entry->commit, entry->id);
2695 if (matched == NULL) {
2696 err = got_error_from_errno(
2697 "alloc_commit_queue_entry");
2700 matched->commit = entry->commit;
2701 got_object_commit_retain(entry->commit);
2703 matched->idx = a->limit_commits->ncommits;
2704 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2706 a->limit_commits->ncommits++;
2710 * This is how we signal log_thread() that we
2711 * have found a match, and that it should be
2712 * counted as a new entry for the view.
2714 a->limit_match = limit_match;
2717 if (*a->searching == TOG_SEARCH_FORWARD &&
2718 !*a->search_next_done) {
2720 err = match_commit(&have_match, &id, commit, a->regex);
2725 if (limit_match && have_match)
2726 *a->search_next_done =
2727 TOG_SEARCH_HAVE_MORE;
2728 } else if (have_match)
2729 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2732 errcode = pthread_mutex_unlock(&tog_mutex);
2733 if (errcode && err == NULL)
2734 err = got_error_set_errno(errcode,
2735 "pthread_mutex_unlock");
2738 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2744 select_commit(struct tog_log_view_state *s)
2746 struct commit_queue_entry *entry;
2749 entry = s->first_displayed_entry;
2751 if (ncommits == s->selected) {
2752 s->selected_entry = entry;
2755 entry = TAILQ_NEXT(entry, entry);
2760 static const struct got_error *
2761 draw_commits(struct tog_view *view)
2763 const struct got_error *err = NULL;
2764 struct tog_log_view_state *s = &view->state.log;
2765 struct commit_queue_entry *entry = s->selected_entry;
2766 int limit = view->nlines;
2768 int ncommits, author_cols = 4;
2769 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2770 char *refs_str = NULL;
2772 struct tog_color *tc;
2773 static const size_t date_display_cols = 12;
2775 if (view_is_hsplit_top(view))
2776 --limit; /* account for border */
2778 if (s->selected_entry &&
2779 !(view->searching && view->search_next_done == 0)) {
2780 struct got_reflist_head *refs;
2781 err = got_object_id_str(&id_str, s->selected_entry->id);
2784 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2785 s->selected_entry->id);
2786 err = build_refs_str(&refs_str, refs, s->selected_entry->id,
2792 if (s->thread_args.commits_needed == 0 && !using_mock_io)
2793 halfdelay(10); /* disable fast refresh */
2795 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2796 if (asprintf(&ncommits_str, " [%d/%d] %s",
2797 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2798 (view->searching && !view->search_next_done) ?
2799 "searching..." : "loading...") == -1) {
2800 err = got_error_from_errno("asprintf");
2804 const char *search_str = NULL;
2805 const char *limit_str = NULL;
2807 if (view->searching) {
2808 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2809 search_str = "no more matches";
2810 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2811 search_str = "no matches found";
2812 else if (!view->search_next_done)
2813 search_str = "searching...";
2816 if (s->limit_view && s->commits->ncommits == 0)
2817 limit_str = "no matches found";
2819 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2820 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2821 search_str ? search_str : (refs_str ? refs_str : ""),
2822 limit_str ? limit_str : "") == -1) {
2823 err = got_error_from_errno("asprintf");
2828 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2829 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2830 "........................................",
2831 s->in_repo_path, ncommits_str) == -1) {
2832 err = got_error_from_errno("asprintf");
2836 } else if (asprintf(&header, "commit %s%s",
2837 id_str ? id_str : "........................................",
2838 ncommits_str) == -1) {
2839 err = got_error_from_errno("asprintf");
2843 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2847 werase(view->window);
2849 if (view_needs_focus_indication(view))
2850 wstandout(view->window);
2851 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2853 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2854 waddwstr(view->window, wline);
2855 while (width < view->ncols) {
2856 waddch(view->window, ' ');
2860 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2861 if (view_needs_focus_indication(view))
2862 wstandend(view->window);
2867 /* Grow author column size if necessary, and set view->maxx. */
2868 entry = s->first_displayed_entry;
2872 struct got_commit_object *c = entry->commit;
2873 char *author, *eol, *msg, *msg0;
2874 wchar_t *wauthor, *wmsg;
2876 if (ncommits >= limit - 1)
2878 if (s->use_committer)
2879 author = strdup(got_object_commit_get_committer(c));
2881 author = strdup(got_object_commit_get_author(c));
2882 if (author == NULL) {
2883 err = got_error_from_errno("strdup");
2886 err = format_author(&wauthor, &width, author, COLS,
2888 if (author_cols < width)
2889 author_cols = width;
2894 err = got_object_commit_get_logmsg(&msg0, c);
2898 while (*msg == '\n')
2900 if ((eol = strchr(msg, '\n')))
2902 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2903 date_display_cols + author_cols, 0);
2906 view->maxx = MAX(view->maxx, width);
2910 entry = TAILQ_NEXT(entry, entry);
2913 entry = s->first_displayed_entry;
2914 s->last_displayed_entry = s->first_displayed_entry;
2917 if (ncommits >= limit - 1)
2919 if (ncommits == s->selected)
2920 wstandout(view->window);
2921 err = draw_commit(view, entry->commit, entry->id,
2922 date_display_cols, author_cols);
2923 if (ncommits == s->selected)
2924 wstandend(view->window);
2928 s->last_displayed_entry = entry;
2929 entry = TAILQ_NEXT(entry, entry);
2942 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2944 struct commit_queue_entry *entry;
2947 entry = TAILQ_FIRST(&s->commits->head);
2948 if (s->first_displayed_entry == entry)
2951 entry = s->first_displayed_entry;
2952 while (entry && nscrolled < maxscroll) {
2953 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2955 s->first_displayed_entry = entry;
2961 static const struct got_error *
2962 trigger_log_thread(struct tog_view *view, int wait)
2964 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2968 halfdelay(1); /* fast refresh while loading commits */
2970 while (!ta->log_complete && !tog_thread_error &&
2971 (ta->commits_needed > 0 || ta->load_all)) {
2972 /* Wake the log thread. */
2973 errcode = pthread_cond_signal(&ta->need_commits);
2975 return got_error_set_errno(errcode,
2976 "pthread_cond_signal");
2979 * The mutex will be released while the view loop waits
2980 * in wgetch(), at which time the log thread will run.
2985 /* Display progress update in log view. */
2986 show_log_view(view);
2990 /* Wait right here while next commit is being loaded. */
2991 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2993 return got_error_set_errno(errcode,
2994 "pthread_cond_wait");
2996 /* Display progress update in log view. */
2997 show_log_view(view);
3005 static const struct got_error *
3006 request_log_commits(struct tog_view *view)
3008 struct tog_log_view_state *state = &view->state.log;
3009 const struct got_error *err = NULL;
3011 if (state->thread_args.log_complete)
3014 state->thread_args.commits_needed += view->nscrolled;
3015 err = trigger_log_thread(view, 1);
3016 view->nscrolled = 0;
3021 static const struct got_error *
3022 log_scroll_down(struct tog_view *view, int maxscroll)
3024 struct tog_log_view_state *s = &view->state.log;
3025 const struct got_error *err = NULL;
3026 struct commit_queue_entry *pentry;
3027 int nscrolled = 0, ncommits_needed;
3029 if (s->last_displayed_entry == NULL)
3032 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
3033 if (s->commits->ncommits < ncommits_needed &&
3034 !s->thread_args.log_complete) {
3036 * Ask the log thread for required amount of commits.
3038 s->thread_args.commits_needed +=
3039 ncommits_needed - s->commits->ncommits;
3040 err = trigger_log_thread(view, 1);
3046 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
3047 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
3050 s->last_displayed_entry = pentry ?
3051 pentry : s->last_displayed_entry;
3053 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
3056 s->first_displayed_entry = pentry;
3057 } while (++nscrolled < maxscroll);
3059 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
3060 view->nscrolled += nscrolled;
3062 view->nscrolled = 0;
3067 static const struct got_error *
3068 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
3069 struct got_commit_object *commit, struct got_object_id *commit_id,
3070 struct tog_view *log_view, struct got_repository *repo)
3072 const struct got_error *err;
3073 struct got_object_qid *parent_id;
3074 struct tog_view *diff_view;
3076 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
3077 if (diff_view == NULL)
3078 return got_error_from_errno("view_open");
3080 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3081 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
3082 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
3084 *new_view = diff_view;
3088 static const struct got_error *
3089 tree_view_visit_subtree(struct tog_tree_view_state *s,
3090 struct got_tree_object *subtree)
3092 struct tog_parent_tree *parent;
3094 parent = calloc(1, sizeof(*parent));
3096 return got_error_from_errno("calloc");
3098 parent->tree = s->tree;
3099 parent->first_displayed_entry = s->first_displayed_entry;
3100 parent->selected_entry = s->selected_entry;
3101 parent->selected = s->selected;
3102 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3105 s->first_displayed_entry = NULL;
3109 static const struct got_error *
3110 tree_view_walk_path(struct tog_tree_view_state *s,
3111 struct got_commit_object *commit, const char *path)
3113 const struct got_error *err = NULL;
3114 struct got_tree_object *tree = NULL;
3116 char *slash, *subpath = NULL;
3118 /* Walk the path and open corresponding tree objects. */
3121 struct got_tree_entry *te;
3122 struct got_object_id *tree_id;
3128 /* Ensure the correct subtree entry is selected. */
3129 slash = strchr(p, '/');
3131 te_name = strdup(p);
3133 te_name = strndup(p, slash - p);
3134 if (te_name == NULL) {
3135 err = got_error_from_errno("strndup");
3138 te = got_object_tree_find_entry(s->tree, te_name);
3140 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
3145 s->first_displayed_entry = s->selected_entry = te;
3147 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
3148 break; /* jump to this file's entry */
3150 slash = strchr(p, '/');
3152 subpath = strndup(path, slash - path);
3154 subpath = strdup(path);
3155 if (subpath == NULL) {
3156 err = got_error_from_errno("strdup");
3160 err = got_object_id_by_path(&tree_id, s->repo, commit,
3165 err = got_object_open_as_tree(&tree, s->repo, tree_id);
3170 err = tree_view_visit_subtree(s, tree);
3172 got_object_tree_close(tree);
3186 static const struct got_error *
3187 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
3188 struct commit_queue_entry *entry, const char *path,
3189 const char *head_ref_name, struct got_repository *repo)
3191 const struct got_error *err = NULL;
3192 struct tog_tree_view_state *s;
3193 struct tog_view *tree_view;
3195 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
3196 if (tree_view == NULL)
3197 return got_error_from_errno("view_open");
3199 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
3202 s = &tree_view->state.tree;
3204 *new_view = tree_view;
3206 if (got_path_is_root_dir(path))
3209 return tree_view_walk_path(s, entry->commit, path);
3212 static const struct got_error *
3213 block_signals_used_by_main_thread(void)
3218 if (sigemptyset(&sigset) == -1)
3219 return got_error_from_errno("sigemptyset");
3221 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
3222 if (sigaddset(&sigset, SIGWINCH) == -1)
3223 return got_error_from_errno("sigaddset");
3224 if (sigaddset(&sigset, SIGCONT) == -1)
3225 return got_error_from_errno("sigaddset");
3226 if (sigaddset(&sigset, SIGINT) == -1)
3227 return got_error_from_errno("sigaddset");
3228 if (sigaddset(&sigset, SIGTERM) == -1)
3229 return got_error_from_errno("sigaddset");
3231 /* ncurses handles SIGTSTP */
3232 if (sigaddset(&sigset, SIGTSTP) == -1)
3233 return got_error_from_errno("sigaddset");
3235 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
3237 return got_error_set_errno(errcode, "pthread_sigmask");
3243 log_thread(void *arg)
3245 const struct got_error *err = NULL;
3247 struct tog_log_thread_args *a = arg;
3251 * Sync startup with main thread such that we begin our
3252 * work once view_input() has released the mutex.
3254 errcode = pthread_mutex_lock(&tog_mutex);
3256 err = got_error_set_errno(errcode, "pthread_mutex_lock");
3260 err = block_signals_used_by_main_thread();
3262 pthread_mutex_unlock(&tog_mutex);
3266 while (!done && !err && !tog_fatal_signal_received()) {
3267 errcode = pthread_mutex_unlock(&tog_mutex);
3269 err = got_error_set_errno(errcode,
3270 "pthread_mutex_unlock");
3273 err = queue_commits(a);
3275 if (err->code != GOT_ERR_ITER_COMPLETED)
3279 } else if (a->commits_needed > 0 && !a->load_all) {
3282 a->commits_needed--;
3284 a->commits_needed--;
3287 errcode = pthread_mutex_lock(&tog_mutex);
3289 err = got_error_set_errno(errcode,
3290 "pthread_mutex_lock");
3292 } else if (*a->quit)
3294 else if (*a->limiting && *a->first_displayed_entry == NULL) {
3295 *a->first_displayed_entry =
3296 TAILQ_FIRST(&a->limit_commits->head);
3297 *a->selected_entry = *a->first_displayed_entry;
3298 } else if (*a->first_displayed_entry == NULL) {
3299 *a->first_displayed_entry =
3300 TAILQ_FIRST(&a->real_commits->head);
3301 *a->selected_entry = *a->first_displayed_entry;
3304 errcode = pthread_cond_signal(&a->commit_loaded);
3306 err = got_error_set_errno(errcode,
3307 "pthread_cond_signal");
3308 pthread_mutex_unlock(&tog_mutex);
3313 a->commits_needed = 0;
3315 if (a->commits_needed == 0 && !a->load_all) {
3316 errcode = pthread_cond_wait(&a->need_commits,
3319 err = got_error_set_errno(errcode,
3320 "pthread_cond_wait");
3321 pthread_mutex_unlock(&tog_mutex);
3329 a->log_complete = 1;
3330 errcode = pthread_mutex_unlock(&tog_mutex);
3332 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3335 tog_thread_error = 1;
3336 pthread_cond_signal(&a->commit_loaded);
3341 static const struct got_error *
3342 stop_log_thread(struct tog_log_view_state *s)
3344 const struct got_error *err = NULL, *thread_err = NULL;
3349 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3351 return got_error_set_errno(errcode,
3352 "pthread_cond_signal");
3353 errcode = pthread_mutex_unlock(&tog_mutex);
3355 return got_error_set_errno(errcode,
3356 "pthread_mutex_unlock");
3357 errcode = pthread_join(s->thread, (void **)&thread_err);
3359 return got_error_set_errno(errcode, "pthread_join");
3360 errcode = pthread_mutex_lock(&tog_mutex);
3362 return got_error_set_errno(errcode,
3363 "pthread_mutex_lock");
3364 s->thread = 0; //NULL;
3367 if (s->thread_args.repo) {
3368 err = got_repo_close(s->thread_args.repo);
3369 s->thread_args.repo = NULL;
3372 if (s->thread_args.pack_fds) {
3373 const struct got_error *pack_err =
3374 got_repo_pack_fds_close(s->thread_args.pack_fds);
3377 s->thread_args.pack_fds = NULL;
3380 if (s->thread_args.graph) {
3381 got_commit_graph_close(s->thread_args.graph);
3382 s->thread_args.graph = NULL;
3385 return err ? err : thread_err;
3388 static const struct got_error *
3389 close_log_view(struct tog_view *view)
3391 const struct got_error *err = NULL;
3392 struct tog_log_view_state *s = &view->state.log;
3395 err = stop_log_thread(s);
3397 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3398 if (errcode && err == NULL)
3399 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3401 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3402 if (errcode && err == NULL)
3403 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3405 free_commits(&s->limit_commits);
3406 free_commits(&s->real_commits);
3407 free(s->in_repo_path);
3408 s->in_repo_path = NULL;
3411 free(s->head_ref_name);
3412 s->head_ref_name = NULL;
3417 * We use two queues to implement the limit feature: first consists of
3418 * commits matching the current limit_regex; second is the real queue
3419 * of all known commits (real_commits). When the user starts limiting,
3420 * we swap queues such that all movement and displaying functionality
3421 * works with very slight change.
3423 static const struct got_error *
3424 limit_log_view(struct tog_view *view)
3426 struct tog_log_view_state *s = &view->state.log;
3427 struct commit_queue_entry *entry;
3428 struct tog_view *v = view;
3429 const struct got_error *err = NULL;
3433 if (view_is_hsplit_top(view))
3435 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3438 /* Get the pattern */
3439 wmove(v->window, v->nlines - 1, 0);
3440 wclrtoeol(v->window);
3441 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3442 nodelay(v->window, FALSE);
3445 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3448 nodelay(v->window, TRUE);
3452 if (*pattern == '\0') {
3454 * Safety measure for the situation where the user
3455 * resets limit without previously limiting anything.
3461 * User could have pressed Ctrl+L, which refreshed the
3462 * commit queues, it means we can't save previously
3463 * (before limit took place) displayed entries,
3464 * because they would point to already free'ed memory,
3465 * so we are forced to always select first entry of
3468 s->commits = &s->real_commits;
3469 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3470 s->selected_entry = s->first_displayed_entry;
3477 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3482 /* Clear the screen while loading limit view */
3483 s->first_displayed_entry = NULL;
3484 s->last_displayed_entry = NULL;
3485 s->selected_entry = NULL;
3486 s->commits = &s->limit_commits;
3488 /* Prepare limit queue for new search */
3489 free_commits(&s->limit_commits);
3490 s->limit_commits.ncommits = 0;
3492 /* First process commits, which are in queue already */
3493 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3496 err = match_commit(&have_match, entry->id,
3497 entry->commit, &s->limit_regex);
3502 struct commit_queue_entry *matched;
3504 matched = alloc_commit_queue_entry(entry->commit,
3506 if (matched == NULL) {
3507 err = got_error_from_errno(
3508 "alloc_commit_queue_entry");
3511 matched->commit = entry->commit;
3512 got_object_commit_retain(entry->commit);
3514 matched->idx = s->limit_commits.ncommits;
3515 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3517 s->limit_commits.ncommits++;
3521 /* Second process all the commits, until we fill the screen */
3522 if (s->limit_commits.ncommits < view->nlines - 1 &&
3523 !s->thread_args.log_complete) {
3524 s->thread_args.commits_needed +=
3525 view->nlines - s->limit_commits.ncommits - 1;
3526 err = trigger_log_thread(view, 1);
3531 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3532 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3538 static const struct got_error *
3539 search_start_log_view(struct tog_view *view)
3541 struct tog_log_view_state *s = &view->state.log;
3543 s->matched_entry = NULL;
3544 s->search_entry = NULL;
3548 static const struct got_error *
3549 search_next_log_view(struct tog_view *view)
3551 const struct got_error *err = NULL;
3552 struct tog_log_view_state *s = &view->state.log;
3553 struct commit_queue_entry *entry;
3555 /* Display progress update in log view. */
3556 show_log_view(view);
3560 if (s->search_entry) {
3562 errcode = pthread_mutex_unlock(&tog_mutex);
3564 return got_error_set_errno(errcode,
3565 "pthread_mutex_unlock");
3566 ch = wgetch(view->window);
3567 errcode = pthread_mutex_lock(&tog_mutex);
3569 return got_error_set_errno(errcode,
3570 "pthread_mutex_lock");
3571 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3572 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3575 if (view->searching == TOG_SEARCH_FORWARD)
3576 entry = TAILQ_NEXT(s->search_entry, entry);
3578 entry = TAILQ_PREV(s->search_entry,
3579 commit_queue_head, entry);
3580 } else if (s->matched_entry) {
3582 * If the user has moved the cursor after we hit a match,
3583 * the position from where we should continue searching
3584 * might have changed.
3586 if (view->searching == TOG_SEARCH_FORWARD)
3587 entry = TAILQ_NEXT(s->selected_entry, entry);
3589 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3592 entry = s->selected_entry;
3598 if (entry == NULL) {
3599 if (s->thread_args.log_complete ||
3600 view->searching == TOG_SEARCH_BACKWARD) {
3601 view->search_next_done =
3602 (s->matched_entry == NULL ?
3603 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3604 s->search_entry = NULL;
3608 * Poke the log thread for more commits and return,
3609 * allowing the main loop to make progress. Search
3610 * will resume at s->search_entry once we come back.
3612 s->thread_args.commits_needed++;
3613 return trigger_log_thread(view, 0);
3616 err = match_commit(&have_match, entry->id, entry->commit,
3621 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3622 s->matched_entry = entry;
3626 s->search_entry = entry;
3627 if (view->searching == TOG_SEARCH_FORWARD)
3628 entry = TAILQ_NEXT(entry, entry);
3630 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3633 if (s->matched_entry) {
3634 int cur = s->selected_entry->idx;
3635 while (cur < s->matched_entry->idx) {
3636 err = input_log_view(NULL, view, KEY_DOWN);
3641 while (cur > s->matched_entry->idx) {
3642 err = input_log_view(NULL, view, KEY_UP);
3649 s->search_entry = NULL;
3654 static const struct got_error *
3655 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3656 struct got_repository *repo, const char *head_ref_name,
3657 const char *in_repo_path, int log_branches)
3659 const struct got_error *err = NULL;
3660 struct tog_log_view_state *s = &view->state.log;
3661 struct got_repository *thread_repo = NULL;
3662 struct got_commit_graph *thread_graph = NULL;
3665 if (in_repo_path != s->in_repo_path) {
3666 free(s->in_repo_path);
3667 s->in_repo_path = strdup(in_repo_path);
3668 if (s->in_repo_path == NULL) {
3669 err = got_error_from_errno("strdup");
3674 /* The commit queue only contains commits being displayed. */
3675 TAILQ_INIT(&s->real_commits.head);
3676 s->real_commits.ncommits = 0;
3677 s->commits = &s->real_commits;
3679 TAILQ_INIT(&s->limit_commits.head);
3681 s->limit_commits.ncommits = 0;
3684 if (head_ref_name) {
3685 s->head_ref_name = strdup(head_ref_name);
3686 if (s->head_ref_name == NULL) {
3687 err = got_error_from_errno("strdup");
3691 s->start_id = got_object_id_dup(start_id);
3692 if (s->start_id == NULL) {
3693 err = got_error_from_errno("got_object_id_dup");
3696 s->log_branches = log_branches;
3697 s->use_committer = 1;
3699 STAILQ_INIT(&s->colors);
3700 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3701 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3702 get_color_value("TOG_COLOR_COMMIT"));
3705 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3706 get_color_value("TOG_COLOR_AUTHOR"));
3708 free_colors(&s->colors);
3711 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3712 get_color_value("TOG_COLOR_DATE"));
3714 free_colors(&s->colors);
3719 view->show = show_log_view;
3720 view->input = input_log_view;
3721 view->resize = resize_log_view;
3722 view->close = close_log_view;
3723 view->search_start = search_start_log_view;
3724 view->search_next = search_next_log_view;
3726 if (s->thread_args.pack_fds == NULL) {
3727 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3731 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3732 s->thread_args.pack_fds);
3735 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3739 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3740 s->repo, NULL, NULL);
3744 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3746 err = got_error_set_errno(errcode, "pthread_cond_init");
3749 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3751 err = got_error_set_errno(errcode, "pthread_cond_init");
3755 s->thread_args.commits_needed = view->nlines;
3756 s->thread_args.graph = thread_graph;
3757 s->thread_args.real_commits = &s->real_commits;
3758 s->thread_args.limit_commits = &s->limit_commits;
3759 s->thread_args.in_repo_path = s->in_repo_path;
3760 s->thread_args.start_id = s->start_id;
3761 s->thread_args.repo = thread_repo;
3762 s->thread_args.log_complete = 0;
3763 s->thread_args.quit = &s->quit;
3764 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3765 s->thread_args.selected_entry = &s->selected_entry;
3766 s->thread_args.searching = &view->searching;
3767 s->thread_args.search_next_done = &view->search_next_done;
3768 s->thread_args.regex = &view->regex;
3769 s->thread_args.limiting = &s->limit_view;
3770 s->thread_args.limit_regex = &s->limit_regex;
3771 s->thread_args.limit_commits = &s->limit_commits;
3774 if (view->close == NULL)
3775 close_log_view(view);
3781 static const struct got_error *
3782 show_log_view(struct tog_view *view)
3784 const struct got_error *err;
3785 struct tog_log_view_state *s = &view->state.log;
3787 if (s->thread == 0) { //NULL) {
3788 int errcode = pthread_create(&s->thread, NULL, log_thread,
3791 return got_error_set_errno(errcode, "pthread_create");
3792 if (s->thread_args.commits_needed > 0) {
3793 err = trigger_log_thread(view, 1);
3799 return draw_commits(view);
3803 log_move_cursor_up(struct tog_view *view, int page, int home)
3805 struct tog_log_view_state *s = &view->state.log;
3807 if (s->first_displayed_entry == NULL)
3809 if (s->selected_entry->idx == 0)
3812 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3814 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3816 if (!page && !home && s->selected > 0)
3819 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3825 static const struct got_error *
3826 log_move_cursor_down(struct tog_view *view, int page)
3828 struct tog_log_view_state *s = &view->state.log;
3829 const struct got_error *err = NULL;
3830 int eos = view->nlines - 2;
3832 if (s->first_displayed_entry == NULL)
3835 if (s->thread_args.log_complete &&
3836 s->selected_entry->idx >= s->commits->ncommits - 1)
3839 if (view_is_hsplit_top(view))
3840 --eos; /* border consumes the last line */
3843 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3846 err = log_scroll_down(view, 1);
3847 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3848 struct commit_queue_entry *entry;
3852 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3853 s->last_displayed_entry = entry;
3854 for (n = 0; n <= eos; n++) {
3857 s->first_displayed_entry = entry;
3858 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3861 s->selected = n - 1;
3863 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3864 s->thread_args.log_complete)
3865 s->selected += MIN(page,
3866 s->commits->ncommits - s->selected_entry->idx - 1);
3868 err = log_scroll_down(view, page);
3874 * We might necessarily overshoot in horizontal
3875 * splits; if so, select the last displayed commit.
3877 if (s->first_displayed_entry && s->last_displayed_entry) {
3878 s->selected = MIN(s->selected,
3879 s->last_displayed_entry->idx -
3880 s->first_displayed_entry->idx);
3885 if (s->thread_args.log_complete &&
3886 s->selected_entry->idx == s->commits->ncommits - 1)
3893 view_get_split(struct tog_view *view, int *y, int *x)
3898 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3899 if (view->child && view->child->resized_y)
3900 *y = view->child->resized_y;
3901 else if (view->resized_y)
3902 *y = view->resized_y;
3904 *y = view_split_begin_y(view->lines);
3905 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3906 if (view->child && view->child->resized_x)
3907 *x = view->child->resized_x;
3908 else if (view->resized_x)
3909 *x = view->resized_x;
3911 *x = view_split_begin_x(view->begin_x);
3915 /* Split view horizontally at y and offset view->state->selected line. */
3916 static const struct got_error *
3917 view_init_hsplit(struct tog_view *view, int y)
3919 const struct got_error *err = NULL;
3923 err = view_resize(view);
3927 err = offset_selection_down(view);
3932 static const struct got_error *
3933 log_goto_line(struct tog_view *view, int nlines)
3935 const struct got_error *err = NULL;
3936 struct tog_log_view_state *s = &view->state.log;
3937 int g, idx = s->selected_entry->idx;
3939 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3945 if (g >= s->first_displayed_entry->idx + 1 &&
3946 g <= s->last_displayed_entry->idx + 1 &&
3947 g - s->first_displayed_entry->idx - 1 < nlines) {
3948 s->selected = g - s->first_displayed_entry->idx - 1;
3954 err = log_move_cursor_down(view, g - idx - 1);
3955 if (!err && g > s->selected_entry->idx + 1)
3956 err = log_move_cursor_down(view,
3957 g - s->first_displayed_entry->idx - 1);
3960 } else if (idx + 1 > g)
3961 log_move_cursor_up(view, idx - g + 1, 0);
3963 if (g < nlines && s->first_displayed_entry->idx == 0)
3964 s->selected = g - 1;
3972 horizontal_scroll_input(struct tog_view *view, int ch)
3978 view->x -= MIN(view->x, 2);
3984 if (view->x + view->ncols / 2 < view->maxx)
3993 view->x = MAX(view->maxx - view->ncols / 2, 0);
4001 static const struct got_error *
4002 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
4004 const struct got_error *err = NULL;
4005 struct tog_log_view_state *s = &view->state.log;
4008 if (s->thread_args.load_all) {
4009 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
4010 s->thread_args.load_all = 0;
4011 else if (s->thread_args.log_complete) {
4012 err = log_move_cursor_down(view, s->commits->ncommits);
4013 s->thread_args.load_all = 0;
4019 eos = nscroll = view->nlines - 1;
4020 if (view_is_hsplit_top(view))
4024 return log_goto_line(view, eos);
4028 err = limit_log_view(view);
4039 horizontal_scroll_input(view, ch);
4046 log_move_cursor_up(view, 0, 0);
4051 log_move_cursor_up(view, 0, 1);
4061 log_move_cursor_up(view, nscroll, 0);
4068 err = log_move_cursor_down(view, 0);
4071 s->use_committer = !s->use_committer;
4072 view->action = s->use_committer ?
4073 "show committer" : "show commit author";
4078 /* We don't know yet how many commits, so we're forced to
4079 * traverse them all. */
4081 s->thread_args.load_all = 1;
4082 if (!s->thread_args.log_complete)
4083 return trigger_log_thread(view, 0);
4084 err = log_move_cursor_down(view, s->commits->ncommits);
4085 s->thread_args.load_all = 0;
4096 err = log_move_cursor_down(view, nscroll);
4099 if (s->selected > view->nlines - 2)
4100 s->selected = view->nlines - 2;
4101 if (s->selected > s->commits->ncommits - 1)
4102 s->selected = s->commits->ncommits - 1;
4104 if (s->commits->ncommits < view->nlines - 1 &&
4105 !s->thread_args.log_complete) {
4106 s->thread_args.commits_needed += (view->nlines - 1) -
4107 s->commits->ncommits;
4108 err = trigger_log_thread(view, 1);
4114 if (s->selected_entry == NULL)
4116 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4120 if (s->selected_entry == NULL)
4122 err = view_request_new(new_view, view, TOG_VIEW_TREE);
4128 if (ch == KEY_BACKSPACE &&
4129 got_path_is_root_dir(s->in_repo_path))
4131 err = stop_log_thread(s);
4134 if (ch == KEY_BACKSPACE) {
4136 err = got_path_dirname(&parent_path, s->in_repo_path);
4139 free(s->in_repo_path);
4140 s->in_repo_path = parent_path;
4141 s->thread_args.in_repo_path = s->in_repo_path;
4142 } else if (ch == CTRL('l')) {
4143 struct got_object_id *start_id;
4144 err = got_repo_match_object_id(&start_id, NULL,
4145 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4146 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4148 if (s->head_ref_name == NULL ||
4149 err->code != GOT_ERR_NOT_REF)
4151 /* Try to cope with deleted references. */
4152 free(s->head_ref_name);
4153 s->head_ref_name = NULL;
4154 err = got_repo_match_object_id(&start_id,
4155 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4156 &tog_refs, s->repo);
4161 s->start_id = start_id;
4162 s->thread_args.start_id = s->start_id;
4164 s->log_branches = !s->log_branches;
4166 if (s->thread_args.pack_fds == NULL) {
4167 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4171 err = got_repo_open(&s->thread_args.repo,
4172 got_repo_get_path(s->repo), NULL,
4173 s->thread_args.pack_fds);
4177 err = tog_load_refs(s->repo, 0);
4180 err = got_commit_graph_open(&s->thread_args.graph,
4181 s->in_repo_path, !s->log_branches);
4184 err = got_commit_graph_iter_start(s->thread_args.graph,
4185 s->start_id, s->repo, NULL, NULL);
4188 free_commits(&s->real_commits);
4189 free_commits(&s->limit_commits);
4190 s->first_displayed_entry = NULL;
4191 s->last_displayed_entry = NULL;
4192 s->selected_entry = NULL;
4194 s->thread_args.log_complete = 0;
4196 s->thread_args.commits_needed = view->lines;
4197 s->matched_entry = NULL;
4198 s->search_entry = NULL;
4203 err = view_request_new(new_view, view, TOG_VIEW_REF);
4213 static const struct got_error *
4214 apply_unveil(const char *repo_path, const char *worktree_path)
4216 const struct got_error *error;
4219 if (unveil("gmon.out", "rwc") != 0)
4220 return got_error_from_errno2("unveil", "gmon.out");
4222 if (repo_path && unveil(repo_path, "r") != 0)
4223 return got_error_from_errno2("unveil", repo_path);
4225 if (worktree_path && unveil(worktree_path, "rwc") != 0)
4226 return got_error_from_errno2("unveil", worktree_path);
4228 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4229 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4231 error = got_privsep_unveil_exec_helpers();
4235 if (unveil(NULL, NULL) != 0)
4236 return got_error_from_errno("unveil");
4241 static const struct got_error *
4242 init_mock_term(const char *test_script_path)
4244 const struct got_error *err = NULL;
4245 const char *screen_dump_path;
4248 if (test_script_path == NULL || *test_script_path == '\0')
4249 return got_error_msg(GOT_ERR_IO, "TOG_TEST_SCRIPT not defined");
4251 tog_io.f = fopen(test_script_path, "re");
4252 if (tog_io.f == NULL) {
4253 err = got_error_from_errno_fmt("fopen: %s",
4258 /* test mode, we don't want any output */
4259 tog_io.cout = fopen("/dev/null", "w+");
4260 if (tog_io.cout == NULL) {
4261 err = got_error_from_errno2("fopen", "/dev/null");
4265 in = dup(fileno(tog_io.cout));
4267 err = got_error_from_errno("dup");
4270 tog_io.cin = fdopen(in, "r");
4271 if (tog_io.cin == NULL) {
4272 err = got_error_from_errno("fdopen");
4277 screen_dump_path = getenv("TOG_SCR_DUMP");
4278 if (screen_dump_path == NULL || *screen_dump_path == '\0')
4279 return got_error_msg(GOT_ERR_IO, "TOG_SCR_DUMP not defined");
4280 tog_io.sdump = fopen(screen_dump_path, "wex");
4281 if (tog_io.sdump == NULL) {
4282 err = got_error_from_errno2("fopen", screen_dump_path);
4286 if (fseeko(tog_io.f, 0L, SEEK_SET) == -1) {
4287 err = got_error_from_errno("fseeko");
4291 if (newterm(NULL, tog_io.cout, tog_io.cin) == NULL)
4292 err = got_error_msg(GOT_ERR_IO,
4293 "newterm: failed to initialise curses");
4307 * Override default signal handlers before starting ncurses.
4308 * This should prevent ncurses from installing its own
4309 * broken cleanup() signal handler.
4311 signal(SIGWINCH, tog_sigwinch);
4312 signal(SIGPIPE, tog_sigpipe);
4313 signal(SIGCONT, tog_sigcont);
4314 signal(SIGINT, tog_sigint);
4315 signal(SIGTERM, tog_sigterm);
4317 if (using_mock_io) /* In test mode we use a fake terminal */
4323 halfdelay(1); /* Fast refresh while initial view is loading. */
4326 intrflush(stdscr, FALSE);
4327 keypad(stdscr, TRUE);
4329 if (getenv("TOG_COLORS") != NULL) {
4331 use_default_colors();
4337 static const struct got_error *
4338 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4339 struct got_repository *repo, struct got_worktree *worktree)
4341 const struct got_error *err = NULL;
4344 *in_repo_path = strdup("/");
4345 if (*in_repo_path == NULL)
4346 return got_error_from_errno("strdup");
4351 const char *prefix = got_worktree_get_path_prefix(worktree);
4354 err = got_worktree_resolve_path(&p, worktree, argv[0]);
4357 if (asprintf(in_repo_path, "%s%s%s", prefix,
4358 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4360 err = got_error_from_errno("asprintf");
4361 *in_repo_path = NULL;
4365 err = got_repo_map_path(in_repo_path, repo, argv[0]);
4370 static const struct got_error *
4371 cmd_log(int argc, char *argv[])
4373 const struct got_error *error;
4374 struct got_repository *repo = NULL;
4375 struct got_worktree *worktree = NULL;
4376 struct got_object_id *start_id = NULL;
4377 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4378 char *start_commit = NULL, *label = NULL;
4379 struct got_reference *ref = NULL;
4380 const char *head_ref_name = NULL;
4381 int ch, log_branches = 0;
4382 struct tog_view *view;
4383 int *pack_fds = NULL;
4385 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4391 start_commit = optarg;
4394 repo_path = realpath(optarg, NULL);
4395 if (repo_path == NULL)
4396 return got_error_from_errno2("realpath",
4411 error = got_repo_pack_fds_open(&pack_fds);
4415 if (repo_path == NULL) {
4416 cwd = getcwd(NULL, 0);
4418 return got_error_from_errno("getcwd");
4419 error = got_worktree_open(&worktree, cwd);
4420 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4424 strdup(got_worktree_get_repo_path(worktree));
4426 repo_path = strdup(cwd);
4427 if (repo_path == NULL) {
4428 error = got_error_from_errno("strdup");
4433 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4437 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4444 error = apply_unveil(got_repo_get_path(repo),
4445 worktree ? got_worktree_get_root_path(worktree) : NULL);
4449 /* already loaded by tog_log_with_path()? */
4450 if (TAILQ_EMPTY(&tog_refs)) {
4451 error = tog_load_refs(repo, 0);
4456 if (start_commit == NULL) {
4457 error = got_repo_match_object_id(&start_id, &label,
4458 worktree ? got_worktree_get_head_ref_name(worktree) :
4459 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4462 head_ref_name = label;
4464 error = got_ref_open(&ref, repo, start_commit, 0);
4466 head_ref_name = got_ref_get_name(ref);
4467 else if (error->code != GOT_ERR_NOT_REF)
4469 error = got_repo_match_object_id(&start_id, NULL,
4470 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4475 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4477 error = got_error_from_errno("view_open");
4480 error = open_log_view(view, start_id, repo, head_ref_name,
4481 in_repo_path, log_branches);
4485 /* Release work tree lock. */
4486 got_worktree_close(worktree);
4489 error = view_loop(view);
4499 const struct got_error *close_err = got_repo_close(repo);
4504 got_worktree_close(worktree);
4506 const struct got_error *pack_err =
4507 got_repo_pack_fds_close(pack_fds);
4519 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4520 "object1 object2\n", getprogname());
4525 match_line(const char *line, regex_t *regex, size_t nmatch,
4526 regmatch_t *regmatch)
4528 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4531 static struct tog_color *
4532 match_color(struct tog_colors *colors, const char *line)
4534 struct tog_color *tc = NULL;
4536 STAILQ_FOREACH(tc, colors, entry) {
4537 if (match_line(line, &tc->regex, 0, NULL))
4544 static const struct got_error *
4545 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4546 WINDOW *window, int skipcol, regmatch_t *regmatch)
4548 const struct got_error *err = NULL;
4550 wchar_t *wline = NULL;
4551 int rme, rms, n, width, scrollx;
4552 int width0 = 0, width1 = 0, width2 = 0;
4553 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4557 rms = regmatch->rm_so;
4558 rme = regmatch->rm_eo;
4560 err = expand_tab(&exstr, line);
4564 /* Split the line into 3 segments, according to match offsets. */
4565 seg0 = strndup(exstr, rms);
4567 err = got_error_from_errno("strndup");
4570 seg1 = strndup(exstr + rms, rme - rms);
4572 err = got_error_from_errno("strndup");
4575 seg2 = strdup(exstr + rme);
4577 err = got_error_from_errno("strndup");
4581 /* draw up to matched token if we haven't scrolled past it */
4582 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4586 n = MAX(width0 - skipcol, 0);
4589 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4590 wlimit, col_tab_align, 1);
4593 waddwstr(window, &wline[scrollx]);
4603 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4607 wlen = wcslen(wline);
4609 width = wcwidth(wline[i]);
4611 /* should not happen, tabs are expanded */
4612 err = got_error(GOT_ERR_RANGE);
4615 if (width0 + w + width > skipcol)
4620 /* draw (visible part of) matched token (if scrolled into it) */
4621 if (width1 - w > 0) {
4622 wattron(window, A_STANDOUT);
4623 waddwstr(window, &wline[i]);
4624 wattroff(window, A_STANDOUT);
4625 wlimit -= (width1 - w);
4626 *wtotal += (width1 - w);
4630 if (wlimit > 0) { /* draw rest of line */
4632 if (skipcol > width0 + width1) {
4633 err = format_line(&wline, &width2, &scrollx, seg2,
4634 skipcol - (width0 + width1), wlimit,
4638 waddwstr(window, &wline[scrollx]);
4640 err = format_line(&wline, &width2, NULL, seg2, 0,
4641 wlimit, col_tab_align, 1);
4644 waddwstr(window, wline);
4658 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4661 int *eof, *first, *selected;
4663 if (view->type == TOG_VIEW_DIFF) {
4664 struct tog_diff_view_state *s = &view->state.diff;
4666 first = &s->first_displayed_line;
4670 } else if (view->type == TOG_VIEW_HELP) {
4671 struct tog_help_view_state *s = &view->state.help;
4673 first = &s->first_displayed_line;
4677 } else if (view->type == TOG_VIEW_BLAME) {
4678 struct tog_blame_view_state *s = &view->state.blame;
4680 first = &s->first_displayed_line;
4681 selected = &s->selected_line;
4687 /* Center gline in the middle of the page like vi(1). */
4688 if (*lineno < view->gline - (view->nlines - 3) / 2)
4690 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4699 *selected = view->gline <= (view->nlines - 3) / 2 ?
4700 view->gline : (view->nlines - 3) / 2 + 1;
4706 static const struct got_error *
4707 draw_file(struct tog_view *view, const char *header)
4709 struct tog_diff_view_state *s = &view->state.diff;
4710 regmatch_t *regmatch = &view->regmatch;
4711 const struct got_error *err;
4714 size_t linesize = 0;
4718 int max_lines = view->nlines;
4719 int nlines = s->nlines;
4722 s->lineno = s->first_displayed_line - 1;
4723 line_offset = s->lines[s->first_displayed_line - 1].offset;
4724 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4725 return got_error_from_errno("fseek");
4727 werase(view->window);
4729 if (view->gline > s->nlines - 1)
4730 view->gline = s->nlines - 1;
4733 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4734 1 : view->gline - (view->nlines - 3) / 2 :
4735 s->lineno + s->selected_line;
4737 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4738 return got_error_from_errno("asprintf");
4739 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4745 if (view_needs_focus_indication(view))
4746 wstandout(view->window);
4747 waddwstr(view->window, wline);
4750 while (width++ < view->ncols)
4751 waddch(view->window, ' ');
4752 if (view_needs_focus_indication(view))
4753 wstandend(view->window);
4763 while (max_lines > 0 && nprinted < max_lines) {
4764 enum got_diff_line_type linetype;
4767 linelen = getline(&line, &linesize, s->f);
4768 if (linelen == -1) {
4774 return got_ferror(s->f, GOT_ERR_IO);
4777 if (++s->lineno < s->first_displayed_line)
4779 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4781 if (s->lineno == view->hiline)
4784 /* Set view->maxx based on full line length. */
4785 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4791 view->maxx = MAX(view->maxx, width);
4795 linetype = s->lines[s->lineno].type;
4796 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4797 linetype < GOT_DIFF_LINE_CONTEXT)
4798 attr |= COLOR_PAIR(linetype);
4800 wattron(view->window, attr);
4801 if (s->first_displayed_line + nprinted == s->matched_line &&
4802 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4803 err = add_matched_line(&width, line, view->ncols, 0,
4804 view->window, view->x, regmatch);
4811 err = format_line(&wline, &width, &skip, line,
4812 view->x, view->ncols, 0, view->x ? 1 : 0);
4817 waddwstr(view->window, &wline[skip]);
4821 if (s->lineno == view->hiline) {
4822 /* highlight full gline length */
4823 while (width++ < view->ncols)
4824 waddch(view->window, ' ');
4826 if (width <= view->ncols - 1)
4827 waddch(view->window, '\n');
4830 wattroff(view->window, attr);
4831 if (++nprinted == 1)
4832 s->first_displayed_line = s->lineno;
4836 s->last_displayed_line = s->first_displayed_line +
4839 s->last_displayed_line = s->first_displayed_line;
4844 while (nprinted < view->nlines) {
4845 waddch(view->window, '\n');
4849 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4855 wstandout(view->window);
4856 waddwstr(view->window, wline);
4859 wstandend(view->window);
4866 get_datestr(time_t *time, char *datebuf)
4868 struct tm mytm, *tm;
4871 tm = gmtime_r(time, &mytm);
4874 s = asctime_r(tm, datebuf);
4877 p = strchr(s, '\n');
4883 static const struct got_error *
4884 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4885 off_t off, uint8_t type)
4887 struct got_diff_line *p;
4889 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4891 return got_error_from_errno("reallocarray");
4893 (*lines)[*nlines].offset = off;
4894 (*lines)[*nlines].type = type;
4900 static const struct got_error *
4901 cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4902 struct got_diff_line *s_lines, size_t s_nlines)
4904 struct got_diff_line *p;
4908 if (fseeko(src, 0L, SEEK_SET) == -1)
4909 return got_error_from_errno("fseeko");
4912 r = fread(buf, 1, sizeof(buf), src);
4915 return got_error_from_errno("fread");
4919 if (fwrite(buf, 1, r, dst) != r)
4920 return got_ferror(dst, GOT_ERR_IO);
4923 if (s_nlines == 0 && *d_nlines == 0)
4927 * If commit info was in dst, increment line offsets
4928 * of the appended diff content, but skip s_lines[0]
4929 * because offset zero is already in *d_lines.
4931 if (*d_nlines > 0) {
4932 for (i = 1; i < s_nlines; ++i)
4933 s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4941 p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4943 /* d_lines is freed in close_diff_view() */
4944 return got_error_from_errno("reallocarray");
4949 memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
4950 *d_nlines += s_nlines;
4955 static const struct got_error *
4956 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4957 struct got_object_id *commit_id, struct got_reflist_head *refs,
4958 struct got_repository *repo, int ignore_ws, int force_text_diff,
4959 struct got_diffstat_cb_arg *dsa, FILE *outfile)
4961 const struct got_error *err = NULL;
4962 char datebuf[26], *datestr;
4963 struct got_commit_object *commit;
4964 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4965 time_t committer_time;
4966 const char *author, *committer;
4967 char *refs_str = NULL;
4968 struct got_pathlist_entry *pe;
4972 err = build_refs_str(&refs_str, refs, commit_id, repo);
4976 err = got_object_open_as_commit(&commit, repo, commit_id);
4980 err = got_object_id_str(&id_str, commit_id);
4982 err = got_error_from_errno("got_object_id_str");
4986 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4990 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4991 refs_str ? refs_str : "", refs_str ? ")" : "");
4993 err = got_error_from_errno("fprintf");
4997 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
5001 n = fprintf(outfile, "from: %s\n",
5002 got_object_commit_get_author(commit));
5004 err = got_error_from_errno("fprintf");
5008 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
5012 author = got_object_commit_get_author(commit);
5013 committer = got_object_commit_get_committer(commit);
5014 if (strcmp(author, committer) != 0) {
5015 n = fprintf(outfile, "via: %s\n", committer);
5017 err = got_error_from_errno("fprintf");
5021 err = add_line_metadata(lines, nlines, outoff,
5022 GOT_DIFF_LINE_AUTHOR);
5026 committer_time = got_object_commit_get_committer_time(commit);
5027 datestr = get_datestr(&committer_time, datebuf);
5029 n = fprintf(outfile, "date: %s UTC\n", datestr);
5031 err = got_error_from_errno("fprintf");
5035 err = add_line_metadata(lines, nlines, outoff,
5036 GOT_DIFF_LINE_DATE);
5040 if (got_object_commit_get_nparents(commit) > 1) {
5041 const struct got_object_id_queue *parent_ids;
5042 struct got_object_qid *qid;
5044 parent_ids = got_object_commit_get_parent_ids(commit);
5045 STAILQ_FOREACH(qid, parent_ids, entry) {
5046 err = got_object_id_str(&id_str, &qid->id);
5049 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
5051 err = got_error_from_errno("fprintf");
5055 err = add_line_metadata(lines, nlines, outoff,
5056 GOT_DIFF_LINE_META);
5064 err = got_object_commit_get_logmsg(&logmsg, commit);
5068 while ((line = strsep(&s, "\n")) != NULL) {
5069 n = fprintf(outfile, "%s\n", line);
5071 err = got_error_from_errno("fprintf");
5075 err = add_line_metadata(lines, nlines, outoff,
5076 GOT_DIFF_LINE_LOGMSG);
5081 TAILQ_FOREACH(pe, dsa->paths, entry) {
5082 struct got_diff_changed_path *cp = pe->data;
5083 int pad = dsa->max_path_len - pe->path_len + 1;
5085 n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
5086 pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
5087 dsa->rm_cols + 1, cp->rm);
5089 err = got_error_from_errno("fprintf");
5093 err = add_line_metadata(lines, nlines, outoff,
5094 GOT_DIFF_LINE_CHANGES);
5099 fputc('\n', outfile);
5101 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5105 n = fprintf(outfile,
5106 "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5107 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5108 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5110 err = got_error_from_errno("fprintf");
5114 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5118 fputc('\n', outfile);
5120 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5125 got_object_commit_close(commit);
5134 static const struct got_error *
5135 create_diff(struct tog_diff_view_state *s)
5137 const struct got_error *err = NULL;
5138 FILE *f = NULL, *tmp_diff_file = NULL;
5140 struct got_diff_line *lines = NULL;
5141 struct got_pathlist_head changed_paths;
5143 TAILQ_INIT(&changed_paths);
5146 s->lines = malloc(sizeof(*s->lines));
5147 if (s->lines == NULL)
5148 return got_error_from_errno("malloc");
5153 err = got_error_from_errno("got_opentemp");
5156 tmp_diff_file = got_opentemp();
5157 if (tmp_diff_file == NULL) {
5158 err = got_error_from_errno("got_opentemp");
5161 if (s->f && fclose(s->f) == EOF) {
5162 err = got_error_from_errno("fclose");
5168 err = got_object_get_type(&obj_type, s->repo, s->id1);
5170 err = got_object_get_type(&obj_type, s->repo, s->id2);
5175 case GOT_OBJ_TYPE_BLOB:
5176 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5177 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5178 s->label1, s->label2, tog_diff_algo, s->diff_context,
5179 s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5182 case GOT_OBJ_TYPE_TREE:
5183 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5184 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5185 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5186 s->force_text_diff, NULL, s->repo, s->f);
5188 case GOT_OBJ_TYPE_COMMIT: {
5189 const struct got_object_id_queue *parent_ids;
5190 struct got_object_qid *pid;
5191 struct got_commit_object *commit2;
5192 struct got_reflist_head *refs;
5194 struct got_diffstat_cb_arg dsa = {
5197 s->ignore_whitespace,
5202 lines = malloc(sizeof(*lines));
5203 if (lines == NULL) {
5204 err = got_error_from_errno("malloc");
5208 /* build diff first in tmp file then append to commit info */
5209 err = got_diff_objects_as_commits(&lines, &nlines,
5210 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5211 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5212 s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5216 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
5219 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5220 /* Show commit info if we're diffing to a parent/root commit. */
5221 if (s->id1 == NULL) {
5222 err = write_commit_info(&s->lines, &s->nlines, s->id2,
5223 refs, s->repo, s->ignore_whitespace,
5224 s->force_text_diff, &dsa, s->f);
5228 parent_ids = got_object_commit_get_parent_ids(commit2);
5229 STAILQ_FOREACH(pid, parent_ids, entry) {
5230 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5231 err = write_commit_info(&s->lines,
5232 &s->nlines, s->id2, refs, s->repo,
5233 s->ignore_whitespace,
5234 s->force_text_diff, &dsa, s->f);
5241 got_object_commit_close(commit2);
5243 err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5248 err = got_error(GOT_ERR_OBJ_TYPE);
5253 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5254 if (s->f && fflush(s->f) != 0 && err == NULL)
5255 err = got_error_from_errno("fflush");
5256 if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5257 err = got_error_from_errno("fclose");
5262 diff_view_indicate_progress(struct tog_view *view)
5264 mvwaddstr(view->window, 0, 0, "diffing...");
5269 static const struct got_error *
5270 search_start_diff_view(struct tog_view *view)
5272 struct tog_diff_view_state *s = &view->state.diff;
5274 s->matched_line = 0;
5279 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5280 size_t *nlines, int **first, int **last, int **match, int **selected)
5282 struct tog_diff_view_state *s = &view->state.diff;
5285 *nlines = s->nlines;
5286 *line_offsets = NULL;
5287 *match = &s->matched_line;
5288 *first = &s->first_displayed_line;
5289 *last = &s->last_displayed_line;
5290 *selected = &s->selected_line;
5293 static const struct got_error *
5294 search_next_view_match(struct tog_view *view)
5296 const struct got_error *err = NULL;
5300 size_t linesize = 0;
5302 off_t *line_offsets;
5304 int *first, *last, *match, *selected;
5306 if (!view->search_setup)
5307 return got_error_msg(GOT_ERR_NOT_IMPL,
5308 "view search not supported");
5309 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5312 if (!view->searching) {
5313 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5318 if (view->searching == TOG_SEARCH_FORWARD)
5319 lineno = *first + 1;
5321 lineno = *first - 1;
5323 lineno = *first - 1 + *selected;
5328 if (lineno <= 0 || lineno > nlines) {
5330 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5334 if (view->searching == TOG_SEARCH_FORWARD)
5340 offset = view->type == TOG_VIEW_DIFF ?
5341 view->state.diff.lines[lineno - 1].offset :
5342 line_offsets[lineno - 1];
5343 if (fseeko(f, offset, SEEK_SET) != 0) {
5345 return got_error_from_errno("fseeko");
5347 linelen = getline(&line, &linesize, f);
5348 if (linelen != -1) {
5350 err = expand_tab(&exstr, line);
5353 if (match_line(exstr, &view->regex, 1,
5355 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5362 if (view->searching == TOG_SEARCH_FORWARD)
5377 static const struct got_error *
5378 close_diff_view(struct tog_view *view)
5380 const struct got_error *err = NULL;
5381 struct tog_diff_view_state *s = &view->state.diff;
5387 if (s->f && fclose(s->f) == EOF)
5388 err = got_error_from_errno("fclose");
5390 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5391 err = got_error_from_errno("fclose");
5393 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5394 err = got_error_from_errno("fclose");
5396 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5397 err = got_error_from_errno("close");
5399 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5400 err = got_error_from_errno("close");
5408 static const struct got_error *
5409 open_diff_view(struct tog_view *view, struct got_object_id *id1,
5410 struct got_object_id *id2, const char *label1, const char *label2,
5411 int diff_context, int ignore_whitespace, int force_text_diff,
5412 struct tog_view *parent_view, struct got_repository *repo)
5414 const struct got_error *err;
5415 struct tog_diff_view_state *s = &view->state.diff;
5417 memset(s, 0, sizeof(*s));
5421 if (id1 != NULL && id2 != NULL) {
5424 err = got_object_get_type(&type1, repo, id1);
5427 err = got_object_get_type(&type2, repo, id2);
5431 if (type1 != type2) {
5432 err = got_error(GOT_ERR_OBJ_TYPE);
5436 s->first_displayed_line = 1;
5437 s->last_displayed_line = view->nlines;
5438 s->selected_line = 1;
5446 s->id1 = got_object_id_dup(id1);
5447 if (s->id1 == NULL) {
5448 err = got_error_from_errno("got_object_id_dup");
5454 s->id2 = got_object_id_dup(id2);
5455 if (s->id2 == NULL) {
5456 err = got_error_from_errno("got_object_id_dup");
5460 s->f1 = got_opentemp();
5461 if (s->f1 == NULL) {
5462 err = got_error_from_errno("got_opentemp");
5466 s->f2 = got_opentemp();
5467 if (s->f2 == NULL) {
5468 err = got_error_from_errno("got_opentemp");
5472 s->fd1 = got_opentempfd();
5474 err = got_error_from_errno("got_opentempfd");
5478 s->fd2 = got_opentempfd();
5480 err = got_error_from_errno("got_opentempfd");
5484 s->diff_context = diff_context;
5485 s->ignore_whitespace = ignore_whitespace;
5486 s->force_text_diff = force_text_diff;
5487 s->parent_view = parent_view;
5490 if (has_colors() && getenv("TOG_COLORS") != NULL && !using_mock_io) {
5493 rc = init_pair(GOT_DIFF_LINE_MINUS,
5494 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5496 rc = init_pair(GOT_DIFF_LINE_PLUS,
5497 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5499 rc = init_pair(GOT_DIFF_LINE_HUNK,
5500 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5502 rc = init_pair(GOT_DIFF_LINE_META,
5503 get_color_value("TOG_COLOR_DIFF_META"), -1);
5505 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5506 get_color_value("TOG_COLOR_DIFF_META"), -1);
5508 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5509 get_color_value("TOG_COLOR_DIFF_META"), -1);
5511 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5512 get_color_value("TOG_COLOR_DIFF_META"), -1);
5514 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5515 get_color_value("TOG_COLOR_AUTHOR"), -1);
5517 rc = init_pair(GOT_DIFF_LINE_DATE,
5518 get_color_value("TOG_COLOR_DATE"), -1);
5520 err = got_error(GOT_ERR_RANGE);
5525 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5526 view_is_splitscreen(view))
5527 show_log_view(parent_view); /* draw border */
5528 diff_view_indicate_progress(view);
5530 err = create_diff(s);
5532 view->show = show_diff_view;
5533 view->input = input_diff_view;
5534 view->reset = reset_diff_view;
5535 view->close = close_diff_view;
5536 view->search_start = search_start_diff_view;
5537 view->search_setup = search_setup_diff_view;
5538 view->search_next = search_next_view_match;
5541 if (view->close == NULL)
5542 close_diff_view(view);
5548 static const struct got_error *
5549 show_diff_view(struct tog_view *view)
5551 const struct got_error *err;
5552 struct tog_diff_view_state *s = &view->state.diff;
5553 char *id_str1 = NULL, *id_str2, *header;
5554 const char *label1, *label2;
5557 err = got_object_id_str(&id_str1, s->id1);
5560 label1 = s->label1 ? s->label1 : id_str1;
5562 label1 = "/dev/null";
5564 err = got_object_id_str(&id_str2, s->id2);
5567 label2 = s->label2 ? s->label2 : id_str2;
5569 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5570 err = got_error_from_errno("asprintf");
5578 err = draw_file(view, header);
5583 static const struct got_error *
5584 set_selected_commit(struct tog_diff_view_state *s,
5585 struct commit_queue_entry *entry)
5587 const struct got_error *err;
5588 const struct got_object_id_queue *parent_ids;
5589 struct got_commit_object *selected_commit;
5590 struct got_object_qid *pid;
5593 s->id2 = got_object_id_dup(entry->id);
5595 return got_error_from_errno("got_object_id_dup");
5597 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5600 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5602 pid = STAILQ_FIRST(parent_ids);
5603 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5604 got_object_commit_close(selected_commit);
5608 static const struct got_error *
5609 reset_diff_view(struct tog_view *view)
5611 struct tog_diff_view_state *s = &view->state.diff;
5614 wclear(view->window);
5615 s->first_displayed_line = 1;
5616 s->last_displayed_line = view->nlines;
5617 s->matched_line = 0;
5618 diff_view_indicate_progress(view);
5619 return create_diff(s);
5623 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5627 i = start = s->first_displayed_line - 1;
5629 while (s->lines[i].type != type) {
5633 return; /* do nothing, requested type not in file */
5636 s->selected_line = 1;
5637 s->first_displayed_line = i;
5641 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5645 i = start = s->first_displayed_line + 1;
5647 while (s->lines[i].type != type) {
5648 if (i == s->nlines - 1)
5651 return; /* do nothing, requested type not in file */
5654 s->selected_line = 1;
5655 s->first_displayed_line = i;
5658 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5660 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5663 static const struct got_error *
5664 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5666 const struct got_error *err = NULL;
5667 struct tog_diff_view_state *s = &view->state.diff;
5668 struct tog_log_view_state *ls;
5669 struct commit_queue_entry *old_selected_entry;
5671 size_t linesize = 0;
5673 int i, nscroll = view->nlines - 1, up = 0;
5675 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5684 horizontal_scroll_input(view, ch);
5689 s->force_text_diff = !s->force_text_diff;
5690 view->action = s->force_text_diff ?
5691 "force ASCII text enabled" :
5692 "force ASCII text disabled";
5694 else if (ch == 'w') {
5695 s->ignore_whitespace = !s->ignore_whitespace;
5696 view->action = s->ignore_whitespace ?
5697 "ignore whitespace enabled" :
5698 "ignore whitespace disabled";
5700 err = reset_diff_view(view);
5704 s->first_displayed_line = 1;
5713 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5719 if (s->first_displayed_line > 1)
5720 s->first_displayed_line--;
5731 if (s->first_displayed_line == 1) {
5736 while (i++ < nscroll && s->first_displayed_line > 1)
5737 s->first_displayed_line--;
5743 s->first_displayed_line++;
5760 while (!s->eof && i++ < nscroll) {
5761 linelen = getline(&line, &linesize, s->f);
5762 s->first_displayed_line++;
5763 if (linelen == -1) {
5767 err = got_ferror(s->f, GOT_ERR_IO);
5774 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5777 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5780 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5783 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5786 if (s->diff_context > 0) {
5788 s->matched_line = 0;
5789 diff_view_indicate_progress(view);
5790 err = create_diff(s);
5791 if (s->first_displayed_line + view->nlines - 1 >
5793 s->first_displayed_line = 1;
5794 s->last_displayed_line = view->nlines;
5800 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5802 s->matched_line = 0;
5803 diff_view_indicate_progress(view);
5804 err = create_diff(s);
5816 if (s->parent_view == NULL) {
5820 s->parent_view->count = view->count;
5822 if (s->parent_view->type == TOG_VIEW_LOG) {
5823 ls = &s->parent_view->state.log;
5824 old_selected_entry = ls->selected_entry;
5826 err = input_log_view(NULL, s->parent_view,
5827 up ? KEY_UP : KEY_DOWN);
5830 view->count = s->parent_view->count;
5832 if (old_selected_entry == ls->selected_entry)
5835 err = set_selected_commit(s, ls->selected_entry);
5838 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5839 struct tog_blame_view_state *bs;
5840 struct got_object_id *id, *prev_id;
5842 bs = &s->parent_view->state.blame;
5843 prev_id = get_annotation_for_line(bs->blame.lines,
5844 bs->blame.nlines, bs->last_diffed_line);
5846 err = input_blame_view(&view, s->parent_view,
5847 up ? KEY_UP : KEY_DOWN);
5850 view->count = s->parent_view->count;
5852 if (prev_id == NULL)
5854 id = get_selected_commit_id(bs->blame.lines,
5855 bs->blame.nlines, bs->first_displayed_line,
5860 if (!got_object_id_cmp(prev_id, id))
5863 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5867 s->first_displayed_line = 1;
5868 s->last_displayed_line = view->nlines;
5869 s->matched_line = 0;
5872 diff_view_indicate_progress(view);
5873 err = create_diff(s);
5883 static const struct got_error *
5884 cmd_diff(int argc, char *argv[])
5886 const struct got_error *error;
5887 struct got_repository *repo = NULL;
5888 struct got_worktree *worktree = NULL;
5889 struct got_object_id *id1 = NULL, *id2 = NULL;
5890 char *repo_path = NULL, *cwd = NULL;
5891 char *id_str1 = NULL, *id_str2 = NULL;
5892 char *label1 = NULL, *label2 = NULL;
5893 int diff_context = 3, ignore_whitespace = 0;
5894 int ch, force_text_diff = 0;
5896 struct tog_view *view;
5897 int *pack_fds = NULL;
5899 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5902 force_text_diff = 1;
5905 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5908 errx(1, "number of context lines is %s: %s",
5912 repo_path = realpath(optarg, NULL);
5913 if (repo_path == NULL)
5914 return got_error_from_errno2("realpath",
5916 got_path_strip_trailing_slashes(repo_path);
5919 ignore_whitespace = 1;
5931 usage_diff(); /* TODO show local worktree changes */
5932 } else if (argc == 2) {
5938 error = got_repo_pack_fds_open(&pack_fds);
5942 if (repo_path == NULL) {
5943 cwd = getcwd(NULL, 0);
5945 return got_error_from_errno("getcwd");
5946 error = got_worktree_open(&worktree, cwd);
5947 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5951 strdup(got_worktree_get_repo_path(worktree));
5953 repo_path = strdup(cwd);
5954 if (repo_path == NULL) {
5955 error = got_error_from_errno("strdup");
5960 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5966 error = apply_unveil(got_repo_get_path(repo), NULL);
5970 error = tog_load_refs(repo, 0);
5974 error = got_repo_match_object_id(&id1, &label1, id_str1,
5975 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5979 error = got_repo_match_object_id(&id2, &label2, id_str2,
5980 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5984 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5986 error = got_error_from_errno("view_open");
5989 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5990 ignore_whitespace, force_text_diff, NULL, repo);
5993 error = view_loop(view);
6000 const struct got_error *close_err = got_repo_close(repo);
6005 got_worktree_close(worktree);
6007 const struct got_error *pack_err =
6008 got_repo_pack_fds_close(pack_fds);
6021 "usage: %s blame [-c commit] [-r repository-path] path\n",
6026 struct tog_blame_line {
6028 struct got_object_id *id;
6031 static const struct got_error *
6032 draw_blame(struct tog_view *view)
6034 struct tog_blame_view_state *s = &view->state.blame;
6035 struct tog_blame *blame = &s->blame;
6036 regmatch_t *regmatch = &view->regmatch;
6037 const struct got_error *err;
6038 int lineno = 0, nprinted = 0;
6040 size_t linesize = 0;
6044 struct tog_blame_line *blame_line;
6045 struct got_object_id *prev_id = NULL;
6047 struct tog_color *tc;
6049 err = got_object_id_str(&id_str, &s->blamed_commit->id);
6054 werase(view->window);
6056 if (asprintf(&line, "commit %s", id_str) == -1) {
6057 err = got_error_from_errno("asprintf");
6062 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6067 if (view_needs_focus_indication(view))
6068 wstandout(view->window);
6069 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6071 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6072 waddwstr(view->window, wline);
6073 while (width++ < view->ncols)
6074 waddch(view->window, ' ');
6076 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6077 if (view_needs_focus_indication(view))
6078 wstandend(view->window);
6082 if (view->gline > blame->nlines)
6083 view->gline = blame->nlines;
6085 if (tog_io.wait_for_ui) {
6086 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6089 rc = pthread_cond_wait(&bta->blame_complete, &tog_mutex);
6091 return got_error_set_errno(rc, "pthread_cond_wait");
6092 tog_io.wait_for_ui = 0;
6095 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
6096 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
6097 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
6099 return got_error_from_errno("asprintf");
6102 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6107 waddwstr(view->window, wline);
6110 if (width < view->ncols - 1)
6111 waddch(view->window, '\n');
6115 while (nprinted < view->nlines - 2) {
6116 linelen = getline(&line, &linesize, blame->f);
6117 if (linelen == -1) {
6118 if (feof(blame->f)) {
6123 return got_ferror(blame->f, GOT_ERR_IO);
6125 if (++lineno < s->first_displayed_line)
6127 if (view->gline && !gotoline(view, &lineno, &nprinted))
6130 /* Set view->maxx based on full line length. */
6131 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6138 view->maxx = MAX(view->maxx, width);
6140 if (nprinted == s->selected_line - 1)
6141 wstandout(view->window);
6143 if (blame->nlines > 0) {
6144 blame_line = &blame->lines[lineno - 1];
6145 if (blame_line->annotated && prev_id &&
6146 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6147 !(nprinted == s->selected_line - 1)) {
6148 waddstr(view->window, " ");
6149 } else if (blame_line->annotated) {
6151 err = got_object_id_str(&id_str,
6157 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6159 wattr_on(view->window,
6160 COLOR_PAIR(tc->colorpair), NULL);
6161 wprintw(view->window, "%.8s", id_str);
6163 wattr_off(view->window,
6164 COLOR_PAIR(tc->colorpair), NULL);
6166 prev_id = blame_line->id;
6168 waddstr(view->window, "........");
6172 waddstr(view->window, "........");
6176 if (nprinted == s->selected_line - 1)
6177 wstandend(view->window);
6178 waddstr(view->window, " ");
6180 if (view->ncols <= 9) {
6182 } else if (s->first_displayed_line + nprinted ==
6184 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6185 err = add_matched_line(&width, line, view->ncols - 9, 9,
6186 view->window, view->x, regmatch);
6194 err = format_line(&wline, &width, &skip, line,
6195 view->x, view->ncols - 9, 9, 1);
6200 waddwstr(view->window, &wline[skip]);
6206 if (width <= view->ncols - 1)
6207 waddch(view->window, '\n');
6208 if (++nprinted == 1)
6209 s->first_displayed_line = lineno;
6212 s->last_displayed_line = lineno;
6219 static const struct got_error *
6220 blame_cb(void *arg, int nlines, int lineno,
6221 struct got_commit_object *commit, struct got_object_id *id)
6223 const struct got_error *err = NULL;
6224 struct tog_blame_cb_args *a = arg;
6225 struct tog_blame_line *line;
6228 if (nlines != a->nlines ||
6229 (lineno != -1 && lineno < 1) || lineno > a->nlines)
6230 return got_error(GOT_ERR_RANGE);
6232 errcode = pthread_mutex_lock(&tog_mutex);
6234 return got_error_set_errno(errcode, "pthread_mutex_lock");
6236 if (*a->quit) { /* user has quit the blame view */
6237 err = got_error(GOT_ERR_ITER_COMPLETED);
6242 goto done; /* no change in this commit */
6244 line = &a->lines[lineno - 1];
6245 if (line->annotated)
6248 line->id = got_object_id_dup(id);
6249 if (line->id == NULL) {
6250 err = got_error_from_errno("got_object_id_dup");
6253 line->annotated = 1;
6255 errcode = pthread_mutex_unlock(&tog_mutex);
6257 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6262 blame_thread(void *arg)
6264 const struct got_error *err, *close_err;
6265 struct tog_blame_thread_args *ta = arg;
6266 struct tog_blame_cb_args *a = ta->cb_args;
6267 int errcode, fd1 = -1, fd2 = -1;
6268 FILE *f1 = NULL, *f2 = NULL;
6270 fd1 = got_opentempfd();
6272 return (void *)got_error_from_errno("got_opentempfd");
6274 fd2 = got_opentempfd();
6276 err = got_error_from_errno("got_opentempfd");
6280 f1 = got_opentemp();
6282 err = (void *)got_error_from_errno("got_opentemp");
6285 f2 = got_opentemp();
6287 err = (void *)got_error_from_errno("got_opentemp");
6291 err = block_signals_used_by_main_thread();
6295 err = got_blame(ta->path, a->commit_id, ta->repo,
6296 tog_diff_algo, blame_cb, ta->cb_args,
6297 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6298 if (err && err->code == GOT_ERR_CANCELLED)
6301 errcode = pthread_mutex_lock(&tog_mutex);
6303 err = got_error_set_errno(errcode, "pthread_mutex_lock");
6307 close_err = got_repo_close(ta->repo);
6313 if (tog_io.wait_for_ui) {
6314 errcode = pthread_cond_signal(&ta->blame_complete);
6315 if (errcode && err == NULL)
6316 err = got_error_set_errno(errcode,
6317 "pthread_cond_signal");
6320 errcode = pthread_mutex_unlock(&tog_mutex);
6321 if (errcode && err == NULL)
6322 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6325 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6326 err = got_error_from_errno("close");
6327 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6328 err = got_error_from_errno("close");
6329 if (f1 && fclose(f1) == EOF && err == NULL)
6330 err = got_error_from_errno("fclose");
6331 if (f2 && fclose(f2) == EOF && err == NULL)
6332 err = got_error_from_errno("fclose");
6337 static struct got_object_id *
6338 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6339 int first_displayed_line, int selected_line)
6341 struct tog_blame_line *line;
6346 line = &lines[first_displayed_line - 1 + selected_line - 1];
6347 if (!line->annotated)
6353 static struct got_object_id *
6354 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6357 struct tog_blame_line *line;
6359 if (nlines <= 0 || lineno >= nlines)
6362 line = &lines[lineno - 1];
6363 if (!line->annotated)
6369 static const struct got_error *
6370 stop_blame(struct tog_blame *blame)
6372 const struct got_error *err = NULL;
6375 if (blame->thread) {
6377 errcode = pthread_mutex_unlock(&tog_mutex);
6379 return got_error_set_errno(errcode,
6380 "pthread_mutex_unlock");
6381 errcode = pthread_join(blame->thread, (void **)&err);
6383 return got_error_set_errno(errcode, "pthread_join");
6384 errcode = pthread_mutex_lock(&tog_mutex);
6386 return got_error_set_errno(errcode,
6387 "pthread_mutex_lock");
6388 if (err && err->code == GOT_ERR_ITER_COMPLETED)
6390 blame->thread = 0; //NULL;
6392 if (blame->thread_args.repo) {
6393 const struct got_error *close_err;
6394 close_err = got_repo_close(blame->thread_args.repo);
6397 blame->thread_args.repo = NULL;
6400 if (fclose(blame->f) == EOF && err == NULL)
6401 err = got_error_from_errno("fclose");
6405 for (i = 0; i < blame->nlines; i++)
6406 free(blame->lines[i].id);
6408 blame->lines = NULL;
6410 free(blame->cb_args.commit_id);
6411 blame->cb_args.commit_id = NULL;
6412 if (blame->pack_fds) {
6413 const struct got_error *pack_err =
6414 got_repo_pack_fds_close(blame->pack_fds);
6417 blame->pack_fds = NULL;
6422 static const struct got_error *
6423 cancel_blame_view(void *arg)
6425 const struct got_error *err = NULL;
6429 errcode = pthread_mutex_lock(&tog_mutex);
6431 return got_error_set_errno(errcode,
6432 "pthread_mutex_unlock");
6435 err = got_error(GOT_ERR_CANCELLED);
6437 errcode = pthread_mutex_unlock(&tog_mutex);
6439 return got_error_set_errno(errcode,
6440 "pthread_mutex_lock");
6445 static const struct got_error *
6446 run_blame(struct tog_view *view)
6448 struct tog_blame_view_state *s = &view->state.blame;
6449 struct tog_blame *blame = &s->blame;
6450 const struct got_error *err = NULL;
6451 struct got_commit_object *commit = NULL;
6452 struct got_blob_object *blob = NULL;
6453 struct got_repository *thread_repo = NULL;
6454 struct got_object_id *obj_id = NULL;
6455 int obj_type, fd = -1;
6456 int *pack_fds = NULL;
6458 err = got_object_open_as_commit(&commit, s->repo,
6459 &s->blamed_commit->id);
6463 fd = got_opentempfd();
6465 err = got_error_from_errno("got_opentempfd");
6469 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6473 err = got_object_get_type(&obj_type, s->repo, obj_id);
6477 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6478 err = got_error(GOT_ERR_OBJ_TYPE);
6482 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6485 blame->f = got_opentemp();
6486 if (blame->f == NULL) {
6487 err = got_error_from_errno("got_opentemp");
6490 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6491 &blame->line_offsets, blame->f, blob);
6494 if (blame->nlines == 0) {
6495 s->blame_complete = 1;
6499 /* Don't include \n at EOF in the blame line count. */
6500 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6503 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6504 if (blame->lines == NULL) {
6505 err = got_error_from_errno("calloc");
6509 err = got_repo_pack_fds_open(&pack_fds);
6512 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6517 blame->pack_fds = pack_fds;
6518 blame->cb_args.view = view;
6519 blame->cb_args.lines = blame->lines;
6520 blame->cb_args.nlines = blame->nlines;
6521 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6522 if (blame->cb_args.commit_id == NULL) {
6523 err = got_error_from_errno("got_object_id_dup");
6526 blame->cb_args.quit = &s->done;
6528 blame->thread_args.path = s->path;
6529 blame->thread_args.repo = thread_repo;
6530 blame->thread_args.cb_args = &blame->cb_args;
6531 blame->thread_args.complete = &s->blame_complete;
6532 blame->thread_args.cancel_cb = cancel_blame_view;
6533 blame->thread_args.cancel_arg = &s->done;
6534 s->blame_complete = 0;
6536 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6537 s->first_displayed_line = 1;
6538 s->last_displayed_line = view->nlines;
6539 s->selected_line = 1;
6541 s->matched_line = 0;
6545 got_object_commit_close(commit);
6546 if (fd != -1 && close(fd) == -1 && err == NULL)
6547 err = got_error_from_errno("close");
6549 got_object_blob_close(blob);
6556 static const struct got_error *
6557 open_blame_view(struct tog_view *view, char *path,
6558 struct got_object_id *commit_id, struct got_repository *repo)
6560 const struct got_error *err = NULL;
6561 struct tog_blame_view_state *s = &view->state.blame;
6563 STAILQ_INIT(&s->blamed_commits);
6565 s->path = strdup(path);
6566 if (s->path == NULL)
6567 return got_error_from_errno("strdup");
6569 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6575 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6576 s->first_displayed_line = 1;
6577 s->last_displayed_line = view->nlines;
6578 s->selected_line = 1;
6579 s->blame_complete = 0;
6581 s->commit_id = commit_id;
6582 memset(&s->blame, 0, sizeof(s->blame));
6584 STAILQ_INIT(&s->colors);
6585 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6586 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6587 get_color_value("TOG_COLOR_COMMIT"));
6592 view->show = show_blame_view;
6593 view->input = input_blame_view;
6594 view->reset = reset_blame_view;
6595 view->close = close_blame_view;
6596 view->search_start = search_start_blame_view;
6597 view->search_setup = search_setup_blame_view;
6598 view->search_next = search_next_view_match;
6600 if (using_mock_io) {
6601 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6604 rc = pthread_cond_init(&bta->blame_complete, NULL);
6606 return got_error_set_errno(rc, "pthread_cond_init");
6609 return run_blame(view);
6612 static const struct got_error *
6613 close_blame_view(struct tog_view *view)
6615 const struct got_error *err = NULL;
6616 struct tog_blame_view_state *s = &view->state.blame;
6618 if (s->blame.thread)
6619 err = stop_blame(&s->blame);
6621 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6622 struct got_object_qid *blamed_commit;
6623 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6624 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6625 got_object_qid_free(blamed_commit);
6628 if (using_mock_io) {
6629 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6632 rc = pthread_cond_destroy(&bta->blame_complete);
6633 if (rc && err == NULL)
6634 err = got_error_set_errno(rc, "pthread_cond_destroy");
6638 free_colors(&s->colors);
6642 static const struct got_error *
6643 search_start_blame_view(struct tog_view *view)
6645 struct tog_blame_view_state *s = &view->state.blame;
6647 s->matched_line = 0;
6652 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6653 size_t *nlines, int **first, int **last, int **match, int **selected)
6655 struct tog_blame_view_state *s = &view->state.blame;
6658 *nlines = s->blame.nlines;
6659 *line_offsets = s->blame.line_offsets;
6660 *match = &s->matched_line;
6661 *first = &s->first_displayed_line;
6662 *last = &s->last_displayed_line;
6663 *selected = &s->selected_line;
6666 static const struct got_error *
6667 show_blame_view(struct tog_view *view)
6669 const struct got_error *err = NULL;
6670 struct tog_blame_view_state *s = &view->state.blame;
6673 if (s->blame.thread == 0 && !s->blame_complete) {
6674 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6675 &s->blame.thread_args);
6677 return got_error_set_errno(errcode, "pthread_create");
6680 halfdelay(1); /* fast refresh while annotating */
6683 if (s->blame_complete && !using_mock_io)
6684 halfdelay(10); /* disable fast refresh */
6686 err = draw_blame(view);
6692 static const struct got_error *
6693 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6694 struct got_repository *repo, struct got_object_id *id)
6696 struct tog_view *log_view;
6697 const struct got_error *err = NULL;
6701 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6702 if (log_view == NULL)
6703 return got_error_from_errno("view_open");
6705 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6707 view_close(log_view);
6709 *new_view = log_view;
6714 static const struct got_error *
6715 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6717 const struct got_error *err = NULL, *thread_err = NULL;
6718 struct tog_view *diff_view;
6719 struct tog_blame_view_state *s = &view->state.blame;
6720 int eos, nscroll, begin_y = 0, begin_x = 0;
6722 eos = nscroll = view->nlines - 2;
6723 if (view_is_hsplit_top(view))
6733 horizontal_scroll_input(view, ch);
6740 s->selected_line = 1;
6741 s->first_displayed_line = 1;
6746 if (s->blame.nlines < eos) {
6747 s->selected_line = s->blame.nlines;
6748 s->first_displayed_line = 1;
6750 s->selected_line = eos;
6751 s->first_displayed_line = s->blame.nlines - (eos - 1);
6758 if (s->selected_line > 1)
6760 else if (s->selected_line == 1 &&
6761 s->first_displayed_line > 1)
6762 s->first_displayed_line--;
6773 if (s->first_displayed_line == 1) {
6774 if (view->count > 1)
6776 s->selected_line = MAX(1, s->selected_line - nscroll);
6780 if (s->first_displayed_line > nscroll)
6781 s->first_displayed_line -= nscroll;
6783 s->first_displayed_line = 1;
6788 if (s->selected_line < eos && s->first_displayed_line +
6789 s->selected_line <= s->blame.nlines)
6791 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6792 s->first_displayed_line++;
6798 struct got_object_id *id = NULL;
6801 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6802 s->first_displayed_line, s->selected_line);
6806 struct got_commit_object *commit, *pcommit;
6807 struct got_object_qid *pid;
6808 struct got_object_id *blob_id = NULL;
6810 err = got_object_open_as_commit(&commit,
6815 got_object_commit_get_parent_ids(commit));
6817 got_object_commit_close(commit);
6820 /* Check if path history ends here. */
6821 err = got_object_open_as_commit(&pcommit,
6825 err = got_object_id_by_path(&blob_id, s->repo,
6827 got_object_commit_close(pcommit);
6829 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6831 got_object_commit_close(commit);
6834 err = got_object_get_type(&obj_type, s->repo,
6837 /* Can't blame non-blob type objects. */
6838 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6839 got_object_commit_close(commit);
6842 err = got_object_qid_alloc(&s->blamed_commit,
6844 got_object_commit_close(commit);
6846 if (got_object_id_cmp(id,
6847 &s->blamed_commit->id) == 0)
6849 err = got_object_qid_alloc(&s->blamed_commit,
6855 thread_err = stop_blame(&s->blame);
6859 STAILQ_INSERT_HEAD(&s->blamed_commits,
6860 s->blamed_commit, entry);
6861 err = run_blame(view);
6867 struct got_object_qid *first;
6870 first = STAILQ_FIRST(&s->blamed_commits);
6871 if (!got_object_id_cmp(&first->id, s->commit_id))
6874 thread_err = stop_blame(&s->blame);
6878 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6879 got_object_qid_free(s->blamed_commit);
6881 STAILQ_FIRST(&s->blamed_commits);
6882 err = run_blame(view);
6889 s->id_to_log = get_selected_commit_id(s->blame.lines,
6890 s->blame.nlines, s->first_displayed_line, s->selected_line);
6892 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6896 struct got_object_id *id = NULL;
6897 struct got_object_qid *pid;
6898 struct got_commit_object *commit = NULL;
6901 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6902 s->first_displayed_line, s->selected_line);
6905 err = got_object_open_as_commit(&commit, s->repo, id);
6908 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6910 /* traversed from diff view, release diff resources */
6911 err = close_diff_view(*new_view);
6914 diff_view = *new_view;
6916 if (view_is_parent_view(view))
6917 view_get_split(view, &begin_y, &begin_x);
6919 diff_view = view_open(0, 0, begin_y, begin_x,
6921 if (diff_view == NULL) {
6922 got_object_commit_close(commit);
6923 err = got_error_from_errno("view_open");
6927 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6928 id, NULL, NULL, 3, 0, 0, view, s->repo);
6929 got_object_commit_close(commit);
6931 view_close(diff_view);
6934 s->last_diffed_line = s->first_displayed_line - 1 +
6937 break; /* still open from active diff view */
6938 if (view_is_parent_view(view) &&
6939 view->mode == TOG_VIEW_SPLIT_HRZN) {
6940 err = view_init_hsplit(view, begin_y);
6946 diff_view->focussed = 1;
6947 diff_view->mode = view->mode;
6948 diff_view->nlines = view->lines - begin_y;
6949 if (view_is_parent_view(view)) {
6950 view_transfer_size(diff_view, view);
6951 err = view_close_child(view);
6954 err = view_set_child(view, diff_view);
6957 view->focus_child = 1;
6959 *new_view = diff_view;
6972 if (s->last_displayed_line >= s->blame.nlines &&
6973 s->selected_line >= MIN(s->blame.nlines,
6974 view->nlines - 2)) {
6978 if (s->last_displayed_line >= s->blame.nlines &&
6979 s->selected_line < view->nlines - 2) {
6981 MIN(nscroll, s->last_displayed_line -
6982 s->first_displayed_line - s->selected_line + 1);
6984 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6985 s->first_displayed_line += nscroll;
6987 s->first_displayed_line =
6988 s->blame.nlines - (view->nlines - 3);
6991 if (s->selected_line > view->nlines - 2) {
6992 s->selected_line = MIN(s->blame.nlines,
7000 return thread_err ? thread_err : err;
7003 static const struct got_error *
7004 reset_blame_view(struct tog_view *view)
7006 const struct got_error *err;
7007 struct tog_blame_view_state *s = &view->state.blame;
7011 err = stop_blame(&s->blame);
7015 return run_blame(view);
7018 static const struct got_error *
7019 cmd_blame(int argc, char *argv[])
7021 const struct got_error *error;
7022 struct got_repository *repo = NULL;
7023 struct got_worktree *worktree = NULL;
7024 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7025 char *link_target = NULL;
7026 struct got_object_id *commit_id = NULL;
7027 struct got_commit_object *commit = NULL;
7028 char *commit_id_str = NULL;
7030 struct tog_view *view = NULL;
7031 int *pack_fds = NULL;
7033 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7036 commit_id_str = optarg;
7039 repo_path = realpath(optarg, NULL);
7040 if (repo_path == NULL)
7041 return got_error_from_errno2("realpath",
7056 error = got_repo_pack_fds_open(&pack_fds);
7060 if (repo_path == NULL) {
7061 cwd = getcwd(NULL, 0);
7063 return got_error_from_errno("getcwd");
7064 error = got_worktree_open(&worktree, cwd);
7065 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7069 strdup(got_worktree_get_repo_path(worktree));
7071 repo_path = strdup(cwd);
7072 if (repo_path == NULL) {
7073 error = got_error_from_errno("strdup");
7078 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7082 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
7089 error = apply_unveil(got_repo_get_path(repo), NULL);
7093 error = tog_load_refs(repo, 0);
7097 if (commit_id_str == NULL) {
7098 struct got_reference *head_ref;
7099 error = got_ref_open(&head_ref, repo, worktree ?
7100 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
7103 error = got_ref_resolve(&commit_id, repo, head_ref);
7104 got_ref_close(head_ref);
7106 error = got_repo_match_object_id(&commit_id, NULL,
7107 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7112 error = got_object_open_as_commit(&commit, repo, commit_id);
7116 error = got_object_resolve_symlinks(&link_target, in_repo_path,
7121 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
7123 error = got_error_from_errno("view_open");
7126 error = open_blame_view(view, link_target ? link_target : in_repo_path,
7128 if (error != NULL) {
7129 if (view->close == NULL)
7130 close_blame_view(view);
7135 /* Release work tree lock. */
7136 got_worktree_close(worktree);
7139 error = view_loop(view);
7147 got_object_commit_close(commit);
7149 got_worktree_close(worktree);
7151 const struct got_error *close_err = got_repo_close(repo);
7156 const struct got_error *pack_err =
7157 got_repo_pack_fds_close(pack_fds);
7165 static const struct got_error *
7166 draw_tree_entries(struct tog_view *view, const char *parent_path)
7168 struct tog_tree_view_state *s = &view->state.tree;
7169 const struct got_error *err = NULL;
7170 struct got_tree_entry *te;
7173 struct tog_color *tc;
7174 int width, n, nentries, scrollx, i = 1;
7175 int limit = view->nlines;
7178 if (view_is_hsplit_top(view))
7179 --limit; /* border */
7181 werase(view->window);
7186 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7190 if (view_needs_focus_indication(view))
7191 wstandout(view->window);
7192 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7194 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7195 waddwstr(view->window, wline);
7198 while (width++ < view->ncols)
7199 waddch(view->window, ' ');
7201 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7202 if (view_needs_focus_indication(view))
7203 wstandend(view->window);
7208 if (s->first_displayed_entry) {
7209 i += got_tree_entry_get_index(s->first_displayed_entry);
7210 if (s->tree != s->root)
7211 ++i; /* account for ".." entry */
7213 nentries = got_object_tree_get_nentries(s->tree);
7214 if (asprintf(&index, "[%d/%d] %s",
7215 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7216 return got_error_from_errno("asprintf");
7217 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7221 waddwstr(view->window, wline);
7224 if (width < view->ncols - 1)
7225 waddch(view->window, '\n');
7228 waddch(view->window, '\n');
7232 if (s->first_displayed_entry == NULL) {
7233 te = got_object_tree_get_first_entry(s->tree);
7234 if (s->selected == 0) {
7236 wstandout(view->window);
7237 s->selected_entry = NULL;
7239 waddstr(view->window, " ..\n"); /* parent directory */
7240 if (s->selected == 0 && view->focussed)
7241 wstandend(view->window);
7248 te = s->first_displayed_entry;
7252 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7253 char *line = NULL, *id_str = NULL, *link_target = NULL;
7254 const char *modestr = "";
7257 te = got_object_tree_get_entry(s->tree, i);
7258 mode = got_tree_entry_get_mode(te);
7261 err = got_object_id_str(&id_str,
7262 got_tree_entry_get_id(te));
7264 return got_error_from_errno(
7265 "got_object_id_str");
7267 if (got_object_tree_entry_is_submodule(te))
7269 else if (S_ISLNK(mode)) {
7272 err = got_tree_entry_get_symlink_target(&link_target,
7278 for (i = 0; i < strlen(link_target); i++) {
7279 if (!isprint((unsigned char)link_target[i]))
7280 link_target[i] = '?';
7284 else if (S_ISDIR(mode))
7286 else if (mode & S_IXUSR)
7288 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7289 got_tree_entry_get_name(te), modestr,
7290 link_target ? " -> ": "",
7291 link_target ? link_target : "") == -1) {
7294 return got_error_from_errno("asprintf");
7299 /* use full line width to determine view->maxx */
7300 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7305 view->maxx = MAX(view->maxx, width);
7309 err = format_line(&wline, &width, &scrollx, line, view->x,
7315 if (n == s->selected) {
7317 wstandout(view->window);
7318 s->selected_entry = te;
7320 tc = match_color(&s->colors, line);
7322 wattr_on(view->window,
7323 COLOR_PAIR(tc->colorpair), NULL);
7324 waddwstr(view->window, &wline[scrollx]);
7326 wattr_off(view->window,
7327 COLOR_PAIR(tc->colorpair), NULL);
7328 if (width < view->ncols)
7329 waddch(view->window, '\n');
7330 if (n == s->selected && view->focussed)
7331 wstandend(view->window);
7337 s->last_displayed_entry = te;
7346 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7348 struct got_tree_entry *te;
7349 int isroot = s->tree == s->root;
7352 if (s->first_displayed_entry == NULL)
7355 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7356 while (i++ < maxscroll) {
7359 s->first_displayed_entry = NULL;
7362 s->first_displayed_entry = te;
7363 te = got_tree_entry_get_prev(s->tree, te);
7367 static const struct got_error *
7368 tree_scroll_down(struct tog_view *view, int maxscroll)
7370 struct tog_tree_view_state *s = &view->state.tree;
7371 struct got_tree_entry *next, *last;
7374 if (s->first_displayed_entry)
7375 next = got_tree_entry_get_next(s->tree,
7376 s->first_displayed_entry);
7378 next = got_object_tree_get_first_entry(s->tree);
7380 last = s->last_displayed_entry;
7381 while (next && n++ < maxscroll) {
7383 s->last_displayed_entry = last;
7384 last = got_tree_entry_get_next(s->tree, last);
7386 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7387 s->first_displayed_entry = next;
7388 next = got_tree_entry_get_next(s->tree, next);
7395 static const struct got_error *
7396 tree_entry_path(char **path, struct tog_parent_trees *parents,
7397 struct got_tree_entry *te)
7399 const struct got_error *err = NULL;
7400 struct tog_parent_tree *pt;
7401 size_t len = 2; /* for leading slash and NUL */
7403 TAILQ_FOREACH(pt, parents, entry)
7404 len += strlen(got_tree_entry_get_name(pt->selected_entry))
7407 len += strlen(got_tree_entry_get_name(te));
7409 *path = calloc(1, len);
7411 return got_error_from_errno("calloc");
7414 pt = TAILQ_LAST(parents, tog_parent_trees);
7416 const char *name = got_tree_entry_get_name(pt->selected_entry);
7417 if (strlcat(*path, name, len) >= len) {
7418 err = got_error(GOT_ERR_NO_SPACE);
7421 if (strlcat(*path, "/", len) >= len) {
7422 err = got_error(GOT_ERR_NO_SPACE);
7425 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7428 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7429 err = got_error(GOT_ERR_NO_SPACE);
7441 static const struct got_error *
7442 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7443 struct got_tree_entry *te, struct tog_parent_trees *parents,
7444 struct got_object_id *commit_id, struct got_repository *repo)
7446 const struct got_error *err = NULL;
7448 struct tog_view *blame_view;
7452 err = tree_entry_path(&path, parents, te);
7456 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7457 if (blame_view == NULL) {
7458 err = got_error_from_errno("view_open");
7462 err = open_blame_view(blame_view, path, commit_id, repo);
7464 if (err->code == GOT_ERR_CANCELLED)
7466 view_close(blame_view);
7468 *new_view = blame_view;
7474 static const struct got_error *
7475 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7476 struct tog_tree_view_state *s)
7478 struct tog_view *log_view;
7479 const struct got_error *err = NULL;
7484 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7485 if (log_view == NULL)
7486 return got_error_from_errno("view_open");
7488 err = tree_entry_path(&path, &s->parents, s->selected_entry);
7492 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7495 view_close(log_view);
7497 *new_view = log_view;
7502 static const struct got_error *
7503 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7504 const char *head_ref_name, struct got_repository *repo)
7506 const struct got_error *err = NULL;
7507 char *commit_id_str = NULL;
7508 struct tog_tree_view_state *s = &view->state.tree;
7509 struct got_commit_object *commit = NULL;
7511 TAILQ_INIT(&s->parents);
7512 STAILQ_INIT(&s->colors);
7514 s->commit_id = got_object_id_dup(commit_id);
7515 if (s->commit_id == NULL) {
7516 err = got_error_from_errno("got_object_id_dup");
7520 err = got_object_open_as_commit(&commit, repo, commit_id);
7525 * The root is opened here and will be closed when the view is closed.
7526 * Any visited subtrees and their path-wise parents are opened and
7529 err = got_object_open_as_tree(&s->root, repo,
7530 got_object_commit_get_tree_id(commit));
7535 err = got_object_id_str(&commit_id_str, commit_id);
7539 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7540 err = got_error_from_errno("asprintf");
7544 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7545 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7546 if (head_ref_name) {
7547 s->head_ref_name = strdup(head_ref_name);
7548 if (s->head_ref_name == NULL) {
7549 err = got_error_from_errno("strdup");
7555 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7556 err = add_color(&s->colors, "\\$$",
7557 TOG_COLOR_TREE_SUBMODULE,
7558 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7561 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7562 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7565 err = add_color(&s->colors, "/$",
7566 TOG_COLOR_TREE_DIRECTORY,
7567 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7571 err = add_color(&s->colors, "\\*$",
7572 TOG_COLOR_TREE_EXECUTABLE,
7573 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7577 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7578 get_color_value("TOG_COLOR_COMMIT"));
7583 view->show = show_tree_view;
7584 view->input = input_tree_view;
7585 view->close = close_tree_view;
7586 view->search_start = search_start_tree_view;
7587 view->search_next = search_next_tree_view;
7589 free(commit_id_str);
7591 got_object_commit_close(commit);
7593 if (view->close == NULL)
7594 close_tree_view(view);
7600 static const struct got_error *
7601 close_tree_view(struct tog_view *view)
7603 struct tog_tree_view_state *s = &view->state.tree;
7605 free_colors(&s->colors);
7606 free(s->tree_label);
7607 s->tree_label = NULL;
7609 s->commit_id = NULL;
7610 free(s->head_ref_name);
7611 s->head_ref_name = NULL;
7612 while (!TAILQ_EMPTY(&s->parents)) {
7613 struct tog_parent_tree *parent;
7614 parent = TAILQ_FIRST(&s->parents);
7615 TAILQ_REMOVE(&s->parents, parent, entry);
7616 if (parent->tree != s->root)
7617 got_object_tree_close(parent->tree);
7621 if (s->tree != NULL && s->tree != s->root)
7622 got_object_tree_close(s->tree);
7624 got_object_tree_close(s->root);
7628 static const struct got_error *
7629 search_start_tree_view(struct tog_view *view)
7631 struct tog_tree_view_state *s = &view->state.tree;
7633 s->matched_entry = NULL;
7638 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7640 regmatch_t regmatch;
7642 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7646 static const struct got_error *
7647 search_next_tree_view(struct tog_view *view)
7649 struct tog_tree_view_state *s = &view->state.tree;
7650 struct got_tree_entry *te = NULL;
7652 if (!view->searching) {
7653 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7657 if (s->matched_entry) {
7658 if (view->searching == TOG_SEARCH_FORWARD) {
7659 if (s->selected_entry)
7660 te = got_tree_entry_get_next(s->tree,
7663 te = got_object_tree_get_first_entry(s->tree);
7665 if (s->selected_entry == NULL)
7666 te = got_object_tree_get_last_entry(s->tree);
7668 te = got_tree_entry_get_prev(s->tree,
7672 if (s->selected_entry)
7673 te = s->selected_entry;
7674 else if (view->searching == TOG_SEARCH_FORWARD)
7675 te = got_object_tree_get_first_entry(s->tree);
7677 te = got_object_tree_get_last_entry(s->tree);
7682 if (s->matched_entry == NULL) {
7683 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7686 if (view->searching == TOG_SEARCH_FORWARD)
7687 te = got_object_tree_get_first_entry(s->tree);
7689 te = got_object_tree_get_last_entry(s->tree);
7692 if (match_tree_entry(te, &view->regex)) {
7693 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7694 s->matched_entry = te;
7698 if (view->searching == TOG_SEARCH_FORWARD)
7699 te = got_tree_entry_get_next(s->tree, te);
7701 te = got_tree_entry_get_prev(s->tree, te);
7704 if (s->matched_entry) {
7705 s->first_displayed_entry = s->matched_entry;
7712 static const struct got_error *
7713 show_tree_view(struct tog_view *view)
7715 const struct got_error *err = NULL;
7716 struct tog_tree_view_state *s = &view->state.tree;
7719 err = tree_entry_path(&parent_path, &s->parents, NULL);
7723 err = draw_tree_entries(view, parent_path);
7730 static const struct got_error *
7731 tree_goto_line(struct tog_view *view, int nlines)
7733 const struct got_error *err = NULL;
7734 struct tog_tree_view_state *s = &view->state.tree;
7735 struct got_tree_entry **fte, **lte, **ste;
7736 int g, last, first = 1, i = 1;
7737 int root = s->tree == s->root;
7738 int off = root ? 1 : 2;
7745 else if (g > got_object_tree_get_nentries(s->tree))
7746 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7748 fte = &s->first_displayed_entry;
7749 lte = &s->last_displayed_entry;
7750 ste = &s->selected_entry;
7753 first = got_tree_entry_get_index(*fte);
7754 first += off; /* account for ".." */
7756 last = got_tree_entry_get_index(*lte);
7759 if (g >= first && g <= last && g - first < nlines) {
7760 s->selected = g - first;
7761 return NULL; /* gline is on the current page */
7765 i = got_tree_entry_get_index(*ste);
7770 err = tree_scroll_down(view, g - i);
7773 if (got_tree_entry_get_index(*lte) >=
7774 got_object_tree_get_nentries(s->tree) - 1 &&
7775 first + s->selected < g &&
7776 s->selected < s->ndisplayed - 1) {
7777 first = got_tree_entry_get_index(*fte);
7779 s->selected = g - first;
7782 tree_scroll_up(s, i - g);
7785 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7786 s->selected = g - 1;
7791 static const struct got_error *
7792 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7794 const struct got_error *err = NULL;
7795 struct tog_tree_view_state *s = &view->state.tree;
7796 struct got_tree_entry *te;
7797 int n, nscroll = view->nlines - 3;
7800 return tree_goto_line(view, nscroll);
7809 horizontal_scroll_input(view, ch);
7812 s->show_ids = !s->show_ids;
7817 if (!s->selected_entry)
7819 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7823 err = view_request_new(new_view, view, TOG_VIEW_REF);
7830 if (s->tree == s->root)
7831 s->first_displayed_entry =
7832 got_object_tree_get_first_entry(s->tree);
7834 s->first_displayed_entry = NULL;
7839 int eos = view->nlines - 3;
7841 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7845 te = got_object_tree_get_last_entry(s->tree);
7846 for (n = 0; n < eos; n++) {
7848 if (s->tree != s->root) {
7849 s->first_displayed_entry = NULL;
7854 s->first_displayed_entry = te;
7855 te = got_tree_entry_get_prev(s->tree, te);
7858 s->selected = n - 1;
7864 if (s->selected > 0) {
7868 tree_scroll_up(s, 1);
7869 if (s->selected_entry == NULL ||
7870 (s->tree == s->root && s->selected_entry ==
7871 got_object_tree_get_first_entry(s->tree)))
7881 if (s->tree == s->root) {
7882 if (got_object_tree_get_first_entry(s->tree) ==
7883 s->first_displayed_entry)
7884 s->selected -= MIN(s->selected, nscroll);
7886 if (s->first_displayed_entry == NULL)
7887 s->selected -= MIN(s->selected, nscroll);
7889 tree_scroll_up(s, MAX(0, nscroll));
7890 if (s->selected_entry == NULL ||
7891 (s->tree == s->root && s->selected_entry ==
7892 got_object_tree_get_first_entry(s->tree)))
7898 if (s->selected < s->ndisplayed - 1) {
7902 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7904 /* can't scroll any further */
7908 tree_scroll_down(view, 1);
7918 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7920 /* can't scroll any further; move cursor down */
7921 if (s->selected < s->ndisplayed - 1)
7922 s->selected += MIN(nscroll,
7923 s->ndisplayed - s->selected - 1);
7928 tree_scroll_down(view, nscroll);
7933 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7934 struct tog_parent_tree *parent;
7935 /* user selected '..' */
7936 if (s->tree == s->root) {
7940 parent = TAILQ_FIRST(&s->parents);
7941 TAILQ_REMOVE(&s->parents, parent,
7943 got_object_tree_close(s->tree);
7944 s->tree = parent->tree;
7945 s->first_displayed_entry =
7946 parent->first_displayed_entry;
7948 parent->selected_entry;
7949 s->selected = parent->selected;
7950 if (s->selected > view->nlines - 3) {
7951 err = offset_selection_down(view);
7956 } else if (S_ISDIR(got_tree_entry_get_mode(
7957 s->selected_entry))) {
7958 struct got_tree_object *subtree;
7960 err = got_object_open_as_tree(&subtree, s->repo,
7961 got_tree_entry_get_id(s->selected_entry));
7964 err = tree_view_visit_subtree(s, subtree);
7966 got_object_tree_close(subtree);
7969 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7970 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7973 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7974 s->selected = view->nlines - 4;
7990 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7995 static const struct got_error *
7996 cmd_tree(int argc, char *argv[])
7998 const struct got_error *error;
7999 struct got_repository *repo = NULL;
8000 struct got_worktree *worktree = NULL;
8001 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8002 struct got_object_id *commit_id = NULL;
8003 struct got_commit_object *commit = NULL;
8004 const char *commit_id_arg = NULL;
8006 struct got_reference *ref = NULL;
8007 const char *head_ref_name = NULL;
8009 struct tog_view *view;
8010 int *pack_fds = NULL;
8012 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
8015 commit_id_arg = optarg;
8018 repo_path = realpath(optarg, NULL);
8019 if (repo_path == NULL)
8020 return got_error_from_errno2("realpath",
8035 error = got_repo_pack_fds_open(&pack_fds);
8039 if (repo_path == NULL) {
8040 cwd = getcwd(NULL, 0);
8042 return got_error_from_errno("getcwd");
8043 error = got_worktree_open(&worktree, cwd);
8044 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8048 strdup(got_worktree_get_repo_path(worktree));
8050 repo_path = strdup(cwd);
8051 if (repo_path == NULL) {
8052 error = got_error_from_errno("strdup");
8057 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8061 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8068 error = apply_unveil(got_repo_get_path(repo), NULL);
8072 error = tog_load_refs(repo, 0);
8076 if (commit_id_arg == NULL) {
8077 error = got_repo_match_object_id(&commit_id, &label,
8078 worktree ? got_worktree_get_head_ref_name(worktree) :
8079 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8082 head_ref_name = label;
8084 error = got_ref_open(&ref, repo, commit_id_arg, 0);
8086 head_ref_name = got_ref_get_name(ref);
8087 else if (error->code != GOT_ERR_NOT_REF)
8089 error = got_repo_match_object_id(&commit_id, NULL,
8090 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8095 error = got_object_open_as_commit(&commit, repo, commit_id);
8099 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
8101 error = got_error_from_errno("view_open");
8104 error = open_tree_view(view, commit_id, head_ref_name, repo);
8107 if (!got_path_is_root_dir(in_repo_path)) {
8108 error = tree_view_walk_path(&view->state.tree, commit,
8115 /* Release work tree lock. */
8116 got_worktree_close(worktree);
8119 error = view_loop(view);
8128 const struct got_error *close_err = got_repo_close(repo);
8133 const struct got_error *pack_err =
8134 got_repo_pack_fds_close(pack_fds);
8142 static const struct got_error *
8143 ref_view_load_refs(struct tog_ref_view_state *s)
8145 struct got_reflist_entry *sre;
8146 struct tog_reflist_entry *re;
8149 TAILQ_FOREACH(sre, &tog_refs, entry) {
8150 if (strncmp(got_ref_get_name(sre->ref),
8151 "refs/got/", 9) == 0 &&
8152 strncmp(got_ref_get_name(sre->ref),
8153 "refs/got/backup/", 16) != 0)
8156 re = malloc(sizeof(*re));
8158 return got_error_from_errno("malloc");
8160 re->ref = got_ref_dup(sre->ref);
8161 if (re->ref == NULL)
8162 return got_error_from_errno("got_ref_dup");
8163 re->idx = s->nrefs++;
8164 TAILQ_INSERT_TAIL(&s->refs, re, entry);
8167 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8172 ref_view_free_refs(struct tog_ref_view_state *s)
8174 struct tog_reflist_entry *re;
8176 while (!TAILQ_EMPTY(&s->refs)) {
8177 re = TAILQ_FIRST(&s->refs);
8178 TAILQ_REMOVE(&s->refs, re, entry);
8179 got_ref_close(re->ref);
8184 static const struct got_error *
8185 open_ref_view(struct tog_view *view, struct got_repository *repo)
8187 const struct got_error *err = NULL;
8188 struct tog_ref_view_state *s = &view->state.ref;
8190 s->selected_entry = 0;
8193 TAILQ_INIT(&s->refs);
8194 STAILQ_INIT(&s->colors);
8196 err = ref_view_load_refs(s);
8200 if (has_colors() && getenv("TOG_COLORS") != NULL) {
8201 err = add_color(&s->colors, "^refs/heads/",
8202 TOG_COLOR_REFS_HEADS,
8203 get_color_value("TOG_COLOR_REFS_HEADS"));
8207 err = add_color(&s->colors, "^refs/tags/",
8208 TOG_COLOR_REFS_TAGS,
8209 get_color_value("TOG_COLOR_REFS_TAGS"));
8213 err = add_color(&s->colors, "^refs/remotes/",
8214 TOG_COLOR_REFS_REMOTES,
8215 get_color_value("TOG_COLOR_REFS_REMOTES"));
8219 err = add_color(&s->colors, "^refs/got/backup/",
8220 TOG_COLOR_REFS_BACKUP,
8221 get_color_value("TOG_COLOR_REFS_BACKUP"));
8226 view->show = show_ref_view;
8227 view->input = input_ref_view;
8228 view->close = close_ref_view;
8229 view->search_start = search_start_ref_view;
8230 view->search_next = search_next_ref_view;
8233 if (view->close == NULL)
8234 close_ref_view(view);
8240 static const struct got_error *
8241 close_ref_view(struct tog_view *view)
8243 struct tog_ref_view_state *s = &view->state.ref;
8245 ref_view_free_refs(s);
8246 free_colors(&s->colors);
8251 static const struct got_error *
8252 resolve_reflist_entry(struct got_object_id **commit_id,
8253 struct tog_reflist_entry *re, struct got_repository *repo)
8255 const struct got_error *err = NULL;
8256 struct got_object_id *obj_id;
8257 struct got_tag_object *tag = NULL;
8262 err = got_ref_resolve(&obj_id, repo, re->ref);
8266 err = got_object_get_type(&obj_type, repo, obj_id);
8271 case GOT_OBJ_TYPE_COMMIT:
8272 *commit_id = obj_id;
8274 case GOT_OBJ_TYPE_TAG:
8275 err = got_object_open_as_tag(&tag, repo, obj_id);
8279 err = got_object_get_type(&obj_type, repo,
8280 got_object_tag_get_object_id(tag));
8283 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
8284 err = got_error(GOT_ERR_OBJ_TYPE);
8287 *commit_id = got_object_id_dup(
8288 got_object_tag_get_object_id(tag));
8289 if (*commit_id == NULL) {
8290 err = got_error_from_errno("got_object_id_dup");
8295 err = got_error(GOT_ERR_OBJ_TYPE);
8301 got_object_tag_close(tag);
8309 static const struct got_error *
8310 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
8311 struct tog_reflist_entry *re, struct got_repository *repo)
8313 struct tog_view *log_view;
8314 const struct got_error *err = NULL;
8315 struct got_object_id *commit_id = NULL;
8319 err = resolve_reflist_entry(&commit_id, re, repo);
8321 if (err->code != GOT_ERR_OBJ_TYPE)
8327 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
8328 if (log_view == NULL) {
8329 err = got_error_from_errno("view_open");
8333 err = open_log_view(log_view, commit_id, repo,
8334 got_ref_get_name(re->ref), "", 0);
8337 view_close(log_view);
8339 *new_view = log_view;
8345 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
8347 struct tog_reflist_entry *re;
8350 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8353 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
8354 while (i++ < maxscroll) {
8357 s->first_displayed_entry = re;
8358 re = TAILQ_PREV(re, tog_reflist_head, entry);
8362 static const struct got_error *
8363 ref_scroll_down(struct tog_view *view, int maxscroll)
8365 struct tog_ref_view_state *s = &view->state.ref;
8366 struct tog_reflist_entry *next, *last;
8369 if (s->first_displayed_entry)
8370 next = TAILQ_NEXT(s->first_displayed_entry, entry);
8372 next = TAILQ_FIRST(&s->refs);
8374 last = s->last_displayed_entry;
8375 while (next && n++ < maxscroll) {
8377 s->last_displayed_entry = last;
8378 last = TAILQ_NEXT(last, entry);
8380 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
8381 s->first_displayed_entry = next;
8382 next = TAILQ_NEXT(next, entry);
8389 static const struct got_error *
8390 search_start_ref_view(struct tog_view *view)
8392 struct tog_ref_view_state *s = &view->state.ref;
8394 s->matched_entry = NULL;
8399 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8401 regmatch_t regmatch;
8403 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8407 static const struct got_error *
8408 search_next_ref_view(struct tog_view *view)
8410 struct tog_ref_view_state *s = &view->state.ref;
8411 struct tog_reflist_entry *re = NULL;
8413 if (!view->searching) {
8414 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8418 if (s->matched_entry) {
8419 if (view->searching == TOG_SEARCH_FORWARD) {
8420 if (s->selected_entry)
8421 re = TAILQ_NEXT(s->selected_entry, entry);
8423 re = TAILQ_PREV(s->selected_entry,
8424 tog_reflist_head, entry);
8426 if (s->selected_entry == NULL)
8427 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8429 re = TAILQ_PREV(s->selected_entry,
8430 tog_reflist_head, entry);
8433 if (s->selected_entry)
8434 re = s->selected_entry;
8435 else if (view->searching == TOG_SEARCH_FORWARD)
8436 re = TAILQ_FIRST(&s->refs);
8438 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8443 if (s->matched_entry == NULL) {
8444 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8447 if (view->searching == TOG_SEARCH_FORWARD)
8448 re = TAILQ_FIRST(&s->refs);
8450 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8453 if (match_reflist_entry(re, &view->regex)) {
8454 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8455 s->matched_entry = re;
8459 if (view->searching == TOG_SEARCH_FORWARD)
8460 re = TAILQ_NEXT(re, entry);
8462 re = TAILQ_PREV(re, tog_reflist_head, entry);
8465 if (s->matched_entry) {
8466 s->first_displayed_entry = s->matched_entry;
8473 static const struct got_error *
8474 show_ref_view(struct tog_view *view)
8476 const struct got_error *err = NULL;
8477 struct tog_ref_view_state *s = &view->state.ref;
8478 struct tog_reflist_entry *re;
8481 struct tog_color *tc;
8482 int width, n, scrollx;
8483 int limit = view->nlines;
8485 werase(view->window);
8488 if (view_is_hsplit_top(view))
8489 --limit; /* border */
8494 re = s->first_displayed_entry;
8496 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8498 return got_error_from_errno("asprintf");
8500 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8505 if (view_needs_focus_indication(view))
8506 wstandout(view->window);
8507 waddwstr(view->window, wline);
8508 while (width++ < view->ncols)
8509 waddch(view->window, ' ');
8510 if (view_needs_focus_indication(view))
8511 wstandend(view->window);
8521 while (re && limit > 0) {
8523 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8526 struct got_commit_object *ci;
8527 struct got_tag_object *tag;
8528 struct got_object_id *id;
8532 err = got_ref_resolve(&id, s->repo, re->ref);
8535 err = got_object_open_as_tag(&tag, s->repo, id);
8537 if (err->code != GOT_ERR_OBJ_TYPE) {
8541 err = got_object_open_as_commit(&ci, s->repo,
8547 t = got_object_commit_get_committer_time(ci);
8548 got_object_commit_close(ci);
8550 t = got_object_tag_get_tagger_time(tag);
8551 got_object_tag_close(tag);
8554 if (gmtime_r(&t, &tm) == NULL)
8555 return got_error_from_errno("gmtime_r");
8556 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8557 return got_error(GOT_ERR_NO_SPACE);
8559 if (got_ref_is_symbolic(re->ref)) {
8560 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8561 ymd : "", got_ref_get_name(re->ref),
8562 got_ref_get_symref_target(re->ref)) == -1)
8563 return got_error_from_errno("asprintf");
8564 } else if (s->show_ids) {
8565 struct got_object_id *id;
8567 err = got_ref_resolve(&id, s->repo, re->ref);
8570 err = got_object_id_str(&id_str, id);
8575 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8576 got_ref_get_name(re->ref), id_str) == -1) {
8577 err = got_error_from_errno("asprintf");
8584 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8585 got_ref_get_name(re->ref)) == -1)
8586 return got_error_from_errno("asprintf");
8588 /* use full line width to determine view->maxx */
8589 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8594 view->maxx = MAX(view->maxx, width);
8598 err = format_line(&wline, &width, &scrollx, line, view->x,
8604 if (n == s->selected) {
8606 wstandout(view->window);
8607 s->selected_entry = re;
8609 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8611 wattr_on(view->window,
8612 COLOR_PAIR(tc->colorpair), NULL);
8613 waddwstr(view->window, &wline[scrollx]);
8615 wattr_off(view->window,
8616 COLOR_PAIR(tc->colorpair), NULL);
8617 if (width < view->ncols)
8618 waddch(view->window, '\n');
8619 if (n == s->selected && view->focussed)
8620 wstandend(view->window);
8626 s->last_displayed_entry = re;
8629 re = TAILQ_NEXT(re, entry);
8636 static const struct got_error *
8637 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8638 struct tog_reflist_entry *re, struct got_repository *repo)
8640 const struct got_error *err = NULL;
8641 struct got_object_id *commit_id = NULL;
8642 struct tog_view *tree_view;
8646 err = resolve_reflist_entry(&commit_id, re, repo);
8648 if (err->code != GOT_ERR_OBJ_TYPE)
8655 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8656 if (tree_view == NULL) {
8657 err = got_error_from_errno("view_open");
8661 err = open_tree_view(tree_view, commit_id,
8662 got_ref_get_name(re->ref), repo);
8666 *new_view = tree_view;
8672 static const struct got_error *
8673 ref_goto_line(struct tog_view *view, int nlines)
8675 const struct got_error *err = NULL;
8676 struct tog_ref_view_state *s = &view->state.ref;
8677 int g, idx = s->selected_entry->idx;
8684 else if (g > s->nrefs)
8687 if (g >= s->first_displayed_entry->idx + 1 &&
8688 g <= s->last_displayed_entry->idx + 1 &&
8689 g - s->first_displayed_entry->idx - 1 < nlines) {
8690 s->selected = g - s->first_displayed_entry->idx - 1;
8695 err = ref_scroll_down(view, g - idx - 1);
8698 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8699 s->first_displayed_entry->idx + s->selected < g &&
8700 s->selected < s->ndisplayed - 1)
8701 s->selected = g - s->first_displayed_entry->idx - 1;
8702 } else if (idx + 1 > g)
8703 ref_scroll_up(s, idx - g + 1);
8705 if (g < nlines && s->first_displayed_entry->idx == 0)
8706 s->selected = g - 1;
8712 static const struct got_error *
8713 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8715 const struct got_error *err = NULL;
8716 struct tog_ref_view_state *s = &view->state.ref;
8717 struct tog_reflist_entry *re;
8718 int n, nscroll = view->nlines - 1;
8721 return ref_goto_line(view, nscroll);
8730 horizontal_scroll_input(view, ch);
8733 s->show_ids = !s->show_ids;
8737 s->show_date = !s->show_date;
8741 s->sort_by_date = !s->sort_by_date;
8742 view->action = s->sort_by_date ? "sort by date" : "sort by name";
8744 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8745 got_ref_cmp_by_commit_timestamp_descending :
8746 tog_ref_cmp_by_name, s->repo);
8749 got_reflist_object_id_map_free(tog_refs_idmap);
8750 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8751 &tog_refs, s->repo);
8754 ref_view_free_refs(s);
8755 err = ref_view_load_refs(s);
8760 if (!s->selected_entry)
8762 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8766 if (!s->selected_entry)
8768 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8775 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8780 int eos = view->nlines - 1;
8782 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8786 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8787 for (n = 0; n < eos; n++) {
8790 s->first_displayed_entry = re;
8791 re = TAILQ_PREV(re, tog_reflist_head, entry);
8794 s->selected = n - 1;
8800 if (s->selected > 0) {
8804 ref_scroll_up(s, 1);
8805 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8815 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8816 s->selected -= MIN(nscroll, s->selected);
8817 ref_scroll_up(s, MAX(0, nscroll));
8818 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8824 if (s->selected < s->ndisplayed - 1) {
8828 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8829 /* can't scroll any further */
8833 ref_scroll_down(view, 1);
8843 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8844 /* can't scroll any further; move cursor down */
8845 if (s->selected < s->ndisplayed - 1)
8846 s->selected += MIN(nscroll,
8847 s->ndisplayed - s->selected - 1);
8848 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8849 s->selected += s->ndisplayed - s->selected - 1;
8853 ref_scroll_down(view, nscroll);
8858 err = tog_load_refs(s->repo, s->sort_by_date);
8861 ref_view_free_refs(s);
8862 err = ref_view_load_refs(s);
8865 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8866 s->selected = view->nlines - 2;
8880 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8885 static const struct got_error *
8886 cmd_ref(int argc, char *argv[])
8888 const struct got_error *error;
8889 struct got_repository *repo = NULL;
8890 struct got_worktree *worktree = NULL;
8891 char *cwd = NULL, *repo_path = NULL;
8893 struct tog_view *view;
8894 int *pack_fds = NULL;
8896 while ((ch = getopt(argc, argv, "r:")) != -1) {
8899 repo_path = realpath(optarg, NULL);
8900 if (repo_path == NULL)
8901 return got_error_from_errno2("realpath",
8916 error = got_repo_pack_fds_open(&pack_fds);
8920 if (repo_path == NULL) {
8921 cwd = getcwd(NULL, 0);
8923 return got_error_from_errno("getcwd");
8924 error = got_worktree_open(&worktree, cwd);
8925 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8929 strdup(got_worktree_get_repo_path(worktree));
8931 repo_path = strdup(cwd);
8932 if (repo_path == NULL) {
8933 error = got_error_from_errno("strdup");
8938 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8944 error = apply_unveil(got_repo_get_path(repo), NULL);
8948 error = tog_load_refs(repo, 0);
8952 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8954 error = got_error_from_errno("view_open");
8958 error = open_ref_view(view, repo);
8963 /* Release work tree lock. */
8964 got_worktree_close(worktree);
8967 error = view_loop(view);
8972 const struct got_error *close_err = got_repo_close(repo);
8977 const struct got_error *pack_err =
8978 got_repo_pack_fds_close(pack_fds);
8986 static const struct got_error*
8987 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8996 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
9000 if (mvwprintw(win, y, x, "%s", str) == ERR)
9001 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
9008 static const struct got_error *
9009 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
9013 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
9015 free(*line_offsets);
9016 *line_offsets = NULL;
9017 return got_error_from_errno("reallocarray");
9021 (*line_offsets)[*nlines] = off;
9026 static const struct got_error *
9027 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
9031 for (;n > 0; --n, ++km) {
9035 if (km->keys == NULL)
9038 t = t0 = strdup(km->keys);
9040 return got_error_from_errno("strdup");
9043 while ((k = strsep(&t, " ")) != NULL)
9044 len += strlen(k) > 1 ? 2 : 0;
9046 *ret = MAX(*ret, len);
9053 * Write keymap section headers, keys, and key info in km to f.
9054 * Save line offset to *off. If terminal has UTF8 encoding enabled,
9055 * wrap control and symbolic keys in guillemets, else use <>.
9057 static const struct got_error *
9058 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
9063 static const char *u8_glyph[] = {
9064 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
9065 "\xe2\x80\xba" /* U+203A (utf8 >) */
9068 int cs, s, first = 1;
9070 cs = got_locale_is_utf8();
9072 t = t0 = strdup(km->keys);
9074 return got_error_from_errno("strdup");
9076 len = strlen(km->keys);
9077 while ((k = strsep(&t, " ")) != NULL) {
9078 s = strlen(k) > 1; /* control or symbolic key */
9079 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
9080 cs && s ? u8_glyph[0] : s ? "<" : "", k,
9081 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
9084 return got_error_from_errno("fprintf");
9092 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
9094 return got_error_from_errno("fprintf");
9100 static const struct got_error *
9101 format_help(struct tog_help_view_state *s)
9103 const struct got_error *err = NULL;
9105 int i, max, n, show = s->all;
9106 static const struct tog_key_map km[] = {
9107 #define KEYMAP_(info, type) { NULL, (info), type }
9108 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
9114 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
9119 err = max_key_str(&max, km, n);
9123 for (i = 0; i < n; ++i) {
9124 if (km[i].keys == NULL) {
9126 if (km[i].type == TOG_KEYMAP_GLOBAL ||
9127 km[i].type == s->type || s->all)
9131 err = format_help_line(&off, s->f, &km[i], max);
9134 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9141 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9145 static const struct got_error *
9146 create_help(struct tog_help_view_state *s)
9149 const struct got_error *err;
9151 free(s->line_offsets);
9152 s->line_offsets = NULL;
9157 return got_error_from_errno("got_opentemp");
9160 err = format_help(s);
9164 if (s->f && fflush(s->f) != 0)
9165 return got_error_from_errno("fflush");
9170 static const struct got_error *
9171 search_start_help_view(struct tog_view *view)
9173 view->state.help.matched_line = 0;
9178 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
9179 size_t *nlines, int **first, int **last, int **match, int **selected)
9181 struct tog_help_view_state *s = &view->state.help;
9184 *nlines = s->nlines;
9185 *line_offsets = s->line_offsets;
9186 *match = &s->matched_line;
9187 *first = &s->first_displayed_line;
9188 *last = &s->last_displayed_line;
9189 *selected = &s->selected_line;
9192 static const struct got_error *
9193 show_help_view(struct tog_view *view)
9195 struct tog_help_view_state *s = &view->state.help;
9196 const struct got_error *err;
9197 regmatch_t *regmatch = &view->regmatch;
9202 int width, nprinted = 0, rc = 0;
9203 int eos = view->nlines;
9205 if (view_is_hsplit_top(view))
9206 --eos; /* account for border */
9210 werase(view->window);
9212 if (view->gline > s->nlines - 1)
9213 view->gline = s->nlines - 1;
9215 err = win_draw_center(view->window, 0, 0, view->ncols,
9216 view_needs_focus_indication(view),
9217 "tog help (press q to return to tog)");
9222 waddstr(view->window, "\n\n");
9228 while (eos > 0 && nprinted < eos) {
9231 linelen = getline(&line, &linesz, s->f);
9232 if (linelen == -1) {
9235 return got_ferror(s->f, GOT_ERR_IO);
9240 if (++s->lineno < s->first_displayed_line)
9242 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
9244 if (s->lineno == view->hiline)
9247 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
9253 view->maxx = MAX(view->maxx, width);
9258 wattron(view->window, attr);
9259 if (s->first_displayed_line + nprinted == s->matched_line &&
9260 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
9261 err = add_matched_line(&width, line, view->ncols - 1, 0,
9262 view->window, view->x, regmatch);
9270 err = format_line(&wline, &width, &skip, line,
9271 view->x, view->ncols, 0, view->x ? 1 : 0);
9276 waddwstr(view->window, &wline[skip]);
9280 if (s->lineno == view->hiline) {
9281 while (width++ < view->ncols)
9282 waddch(view->window, ' ');
9284 if (width < view->ncols)
9285 waddch(view->window, '\n');
9288 wattroff(view->window, attr);
9289 if (++nprinted == 1)
9290 s->first_displayed_line = s->lineno;
9294 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
9296 s->last_displayed_line = s->first_displayed_line;
9301 rc = waddnstr(view->window,
9302 "See the tog(1) manual page for full documentation",
9305 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9307 wmove(view->window, view->nlines - 1, 0);
9308 wclrtoeol(view->window);
9309 wstandout(view->window);
9310 rc = waddnstr(view->window, "scroll down for more...",
9313 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9314 if (getcurx(view->window) < view->ncols - 6) {
9315 rc = wprintw(view->window, "[%.0f%%]",
9316 100.00 * s->last_displayed_line / s->nlines);
9318 return got_error_msg(GOT_ERR_IO, "wprintw");
9320 wstandend(view->window);
9326 static const struct got_error *
9327 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
9329 struct tog_help_view_state *s = &view->state.help;
9330 const struct got_error *err = NULL;
9336 eos = nscroll = view->nlines;
9337 if (view_is_hsplit_top(view))
9340 s->lineno = s->first_displayed_line - 1 + s->selected_line;
9349 horizontal_scroll_input(view, ch);
9353 s->first_displayed_line = 1;
9361 s->first_displayed_line = (s->nlines - eos) + 3;
9366 if (s->first_displayed_line > 1)
9367 --s->first_displayed_line;
9378 if (s->first_displayed_line == 1) {
9382 while (--nscroll > 0 && s->first_displayed_line > 1)
9383 s->first_displayed_line--;
9389 ++s->first_displayed_line;
9405 while (!s->eof && --nscroll > 0) {
9406 linelen = getline(&line, &linesz, s->f);
9407 s->first_displayed_line++;
9408 if (linelen == -1) {
9412 err = got_ferror(s->f, GOT_ERR_IO);
9426 static const struct got_error *
9427 close_help_view(struct tog_view *view)
9429 struct tog_help_view_state *s = &view->state.help;
9431 free(s->line_offsets);
9432 s->line_offsets = NULL;
9433 if (fclose(s->f) == EOF)
9434 return got_error_from_errno("fclose");
9439 static const struct got_error *
9440 reset_help_view(struct tog_view *view)
9442 struct tog_help_view_state *s = &view->state.help;
9445 if (s->f && fclose(s->f) == EOF)
9446 return got_error_from_errno("fclose");
9448 wclear(view->window);
9452 s->first_displayed_line = 1;
9453 s->last_displayed_line = view->nlines;
9454 s->matched_line = 0;
9456 return create_help(s);
9459 static const struct got_error *
9460 open_help_view(struct tog_view *view, struct tog_view *parent)
9462 const struct got_error *err = NULL;
9463 struct tog_help_view_state *s = &view->state.help;
9465 s->type = (enum tog_keymap_type)parent->type;
9466 s->first_displayed_line = 1;
9467 s->last_displayed_line = view->nlines;
9468 s->selected_line = 1;
9470 view->show = show_help_view;
9471 view->input = input_help_view;
9472 view->reset = reset_help_view;
9473 view->close = close_help_view;
9474 view->search_start = search_start_help_view;
9475 view->search_setup = search_setup_help_view;
9476 view->search_next = search_next_view_match;
9478 err = create_help(s);
9482 static const struct got_error *
9483 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9484 enum tog_view_type request, int y, int x)
9486 const struct got_error *err = NULL;
9492 if (view->type == TOG_VIEW_LOG) {
9493 struct tog_log_view_state *s = &view->state.log;
9495 err = open_diff_view_for_commit(new_view, y, x,
9496 s->selected_entry->commit, s->selected_entry->id,
9499 return got_error_msg(GOT_ERR_NOT_IMPL,
9500 "parent/child view pair not supported");
9502 case TOG_VIEW_BLAME:
9503 if (view->type == TOG_VIEW_TREE) {
9504 struct tog_tree_view_state *s = &view->state.tree;
9506 err = blame_tree_entry(new_view, y, x,
9507 s->selected_entry, &s->parents, s->commit_id,
9510 return got_error_msg(GOT_ERR_NOT_IMPL,
9511 "parent/child view pair not supported");
9514 if (view->type == TOG_VIEW_BLAME)
9515 err = log_annotated_line(new_view, y, x,
9516 view->state.blame.repo, view->state.blame.id_to_log);
9517 else if (view->type == TOG_VIEW_TREE)
9518 err = log_selected_tree_entry(new_view, y, x,
9520 else if (view->type == TOG_VIEW_REF)
9521 err = log_ref_entry(new_view, y, x,
9522 view->state.ref.selected_entry,
9523 view->state.ref.repo);
9525 return got_error_msg(GOT_ERR_NOT_IMPL,
9526 "parent/child view pair not supported");
9529 if (view->type == TOG_VIEW_LOG)
9530 err = browse_commit_tree(new_view, y, x,
9531 view->state.log.selected_entry,
9532 view->state.log.in_repo_path,
9533 view->state.log.head_ref_name,
9534 view->state.log.repo);
9535 else if (view->type == TOG_VIEW_REF)
9536 err = browse_ref_tree(new_view, y, x,
9537 view->state.ref.selected_entry,
9538 view->state.ref.repo);
9540 return got_error_msg(GOT_ERR_NOT_IMPL,
9541 "parent/child view pair not supported");
9544 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9545 if (*new_view == NULL)
9546 return got_error_from_errno("view_open");
9547 if (view->type == TOG_VIEW_LOG)
9548 err = open_ref_view(*new_view, view->state.log.repo);
9549 else if (view->type == TOG_VIEW_TREE)
9550 err = open_ref_view(*new_view, view->state.tree.repo);
9552 err = got_error_msg(GOT_ERR_NOT_IMPL,
9553 "parent/child view pair not supported");
9555 view_close(*new_view);
9558 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9559 if (*new_view == NULL)
9560 return got_error_from_errno("view_open");
9561 err = open_help_view(*new_view, view);
9563 view_close(*new_view);
9566 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9573 * If view was scrolled down to move the selected line into view when opening a
9574 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9577 offset_selection_up(struct tog_view *view)
9579 switch (view->type) {
9580 case TOG_VIEW_BLAME: {
9581 struct tog_blame_view_state *s = &view->state.blame;
9582 if (s->first_displayed_line == 1) {
9583 s->selected_line = MAX(s->selected_line - view->offset,
9587 if (s->first_displayed_line > view->offset)
9588 s->first_displayed_line -= view->offset;
9590 s->first_displayed_line = 1;
9591 s->selected_line += view->offset;
9595 log_scroll_up(&view->state.log, view->offset);
9596 view->state.log.selected += view->offset;
9599 ref_scroll_up(&view->state.ref, view->offset);
9600 view->state.ref.selected += view->offset;
9603 tree_scroll_up(&view->state.tree, view->offset);
9604 view->state.tree.selected += view->offset;
9614 * If the selected line is in the section of screen covered by the bottom split,
9615 * scroll down offset lines to move it into view and index its new position.
9617 static const struct got_error *
9618 offset_selection_down(struct tog_view *view)
9620 const struct got_error *err = NULL;
9621 const struct got_error *(*scrolld)(struct tog_view *, int);
9622 int *selected = NULL;
9625 switch (view->type) {
9626 case TOG_VIEW_BLAME: {
9627 struct tog_blame_view_state *s = &view->state.blame;
9630 if (s->selected_line > view->nlines - header) {
9631 offset = abs(view->nlines - s->selected_line - header);
9632 s->first_displayed_line += offset;
9633 s->selected_line -= offset;
9634 view->offset = offset;
9638 case TOG_VIEW_LOG: {
9639 struct tog_log_view_state *s = &view->state.log;
9640 scrolld = &log_scroll_down;
9641 header = view_is_parent_view(view) ? 3 : 2;
9642 selected = &s->selected;
9645 case TOG_VIEW_REF: {
9646 struct tog_ref_view_state *s = &view->state.ref;
9647 scrolld = &ref_scroll_down;
9649 selected = &s->selected;
9652 case TOG_VIEW_TREE: {
9653 struct tog_tree_view_state *s = &view->state.tree;
9654 scrolld = &tree_scroll_down;
9656 selected = &s->selected;
9666 if (selected && *selected > view->nlines - header) {
9667 offset = abs(view->nlines - *selected - header);
9668 view->offset = offset;
9669 if (scrolld && offset) {
9670 err = scrolld(view, offset);
9671 *selected -= offset;
9679 list_commands(FILE *fp)
9683 fprintf(fp, "commands:");
9684 for (i = 0; i < nitems(tog_commands); i++) {
9685 const struct tog_cmd *cmd = &tog_commands[i];
9686 fprintf(fp, " %s", cmd->name);
9692 usage(int hflag, int status)
9694 FILE *fp = (status == 0) ? stdout : stderr;
9696 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9699 fprintf(fp, "lazy usage: %s path\n", getprogname());
9706 make_argv(int argc, ...)
9714 argv = calloc(argc, sizeof(char *));
9717 for (i = 0; i < argc; i++) {
9718 argv[i] = strdup(va_arg(ap, char *));
9719 if (argv[i] == NULL)
9728 * Try to convert 'tog path' into a 'tog log path' command.
9729 * The user could simply have mistyped the command rather than knowingly
9730 * provided a path. So check whether argv[0] can in fact be resolved
9731 * to a path in the HEAD commit and print a special error if not.
9732 * This hack is for mpi@ <3
9734 static const struct got_error *
9735 tog_log_with_path(int argc, char *argv[])
9737 const struct got_error *error = NULL, *close_err;
9738 const struct tog_cmd *cmd = NULL;
9739 struct got_repository *repo = NULL;
9740 struct got_worktree *worktree = NULL;
9741 struct got_object_id *commit_id = NULL, *id = NULL;
9742 struct got_commit_object *commit = NULL;
9743 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9744 char *commit_id_str = NULL, **cmd_argv = NULL;
9745 int *pack_fds = NULL;
9747 cwd = getcwd(NULL, 0);
9749 return got_error_from_errno("getcwd");
9751 error = got_repo_pack_fds_open(&pack_fds);
9755 error = got_worktree_open(&worktree, cwd);
9756 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9760 repo_path = strdup(got_worktree_get_repo_path(worktree));
9762 repo_path = strdup(cwd);
9763 if (repo_path == NULL) {
9764 error = got_error_from_errno("strdup");
9768 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9772 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9777 error = tog_load_refs(repo, 0);
9780 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9781 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9782 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9787 got_worktree_close(worktree);
9791 error = got_object_open_as_commit(&commit, repo, commit_id);
9795 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9797 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9799 fprintf(stderr, "%s: '%s' is no known command or path\n",
9800 getprogname(), argv[0]);
9805 error = got_object_id_str(&commit_id_str, commit_id);
9809 cmd = &tog_commands[0]; /* log */
9811 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9812 error = cmd->cmd_main(argc, cmd_argv);
9815 close_err = got_repo_close(repo);
9820 got_object_commit_close(commit);
9822 got_worktree_close(worktree);
9824 const struct got_error *pack_err =
9825 got_repo_pack_fds_close(pack_fds);
9830 free(commit_id_str);
9837 for (i = 0; i < argc; i++)
9846 main(int argc, char *argv[])
9848 const struct got_error *io_err, *error = NULL;
9849 const struct tog_cmd *cmd = NULL;
9850 int ch, hflag = 0, Vflag = 0;
9851 char **cmd_argv = NULL;
9852 static const struct option longopts[] = {
9853 { "version", no_argument, NULL, 'V' },
9856 char *diff_algo_str = NULL;
9857 const char *test_script_path;
9859 setlocale(LC_CTYPE, "");
9862 * Test mode init must happen before pledge() because "tty" will
9863 * not allow TTY-related ioctls to occur via regular files.
9865 test_script_path = getenv("TOG_TEST_SCRIPT");
9866 if (test_script_path != NULL) {
9867 error = init_mock_term(test_script_path);
9869 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9872 } else if (!isatty(STDIN_FILENO))
9873 errx(1, "standard input is not a tty");
9875 #if !defined(PROFILE)
9876 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9881 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9901 got_version_print_str();
9908 /* Build an argument vector which runs a default command. */
9909 cmd = &tog_commands[0];
9911 cmd_argv = make_argv(argc, cmd->name);
9915 /* Did the user specify a command? */
9916 for (i = 0; i < nitems(tog_commands); i++) {
9917 if (strncmp(tog_commands[i].name, argv[0],
9918 strlen(argv[0])) == 0) {
9919 cmd = &tog_commands[i];
9925 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9926 if (diff_algo_str) {
9927 if (strcasecmp(diff_algo_str, "patience") == 0)
9928 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9929 if (strcasecmp(diff_algo_str, "myers") == 0)
9930 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9936 /* No command specified; try log with a path */
9937 error = tog_log_with_path(argc, argv);
9942 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9945 if (using_mock_io) {
9946 io_err = tog_io_close();
9953 for (i = 0; i < argc; i++)
9958 if (error && error->code != GOT_ERR_CANCELLED &&
9959 error->code != GOT_ERR_EOF &&
9960 error->code != GOT_ERR_PRIVSEP_EXIT &&
9961 error->code != GOT_ERR_PRIVSEP_PIPE &&
9962 !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9963 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);