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.3 /* 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 */
627 static int using_mock_io;
629 #define TOG_KEY_SCRDUMP SHRT_MIN
632 * We implement two types of views: parent views and child views.
634 * The 'Tab' key switches focus between a parent view and its child view.
635 * Child views are shown side-by-side to their parent view, provided
636 * there is enough screen estate.
638 * When a new view is opened from within a parent view, this new view
639 * becomes a child view of the parent view, replacing any existing child.
641 * When a new view is opened from within a child view, this new view
642 * becomes a parent view which will obscure the views below until the
643 * user quits the new parent view by typing 'q'.
645 * This list of views contains parent views only.
646 * Child views are only pointed to by their parent view.
648 TAILQ_HEAD(tog_view_list_head, tog_view);
651 TAILQ_ENTRY(tog_view) entry;
654 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
655 int resized_y, resized_x; /* begin_y/x based on user resizing */
656 int maxx, x; /* max column and current start column */
657 int lines, cols; /* copies of LINES and COLS */
658 int nscrolled, offset; /* lines scrolled and hsplit line offset */
659 int gline, hiline; /* navigate to and highlight this nG line */
660 int ch, count; /* current keymap and count prefix */
661 int resized; /* set when in a resize event */
662 int focussed; /* Only set on one parent or child view at a time. */
664 struct tog_view *parent;
665 struct tog_view *child;
668 * This flag is initially set on parent views when a new child view
669 * is created. It gets toggled when the 'Tab' key switches focus
670 * between parent and child.
671 * The flag indicates whether focus should be passed on to our child
672 * view if this parent view gets picked for focus after another parent
673 * view was closed. This prevents child views from losing focus in such
678 enum tog_view_mode mode;
679 /* type-specific state */
680 enum tog_view_type type;
682 struct tog_diff_view_state diff;
683 struct tog_log_view_state log;
684 struct tog_blame_view_state blame;
685 struct tog_tree_view_state tree;
686 struct tog_ref_view_state ref;
687 struct tog_help_view_state help;
690 const struct got_error *(*show)(struct tog_view *);
691 const struct got_error *(*input)(struct tog_view **,
692 struct tog_view *, int);
693 const struct got_error *(*reset)(struct tog_view *);
694 const struct got_error *(*resize)(struct tog_view *, int);
695 const struct got_error *(*close)(struct tog_view *);
697 const struct got_error *(*search_start)(struct tog_view *);
698 const struct got_error *(*search_next)(struct tog_view *);
699 void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
700 int **, int **, int **, int **);
703 #define TOG_SEARCH_FORWARD 1
704 #define TOG_SEARCH_BACKWARD 2
705 int search_next_done;
706 #define TOG_SEARCH_HAVE_MORE 1
707 #define TOG_SEARCH_NO_MORE 2
708 #define TOG_SEARCH_HAVE_NONE 3
714 static const struct got_error *open_diff_view(struct tog_view *,
715 struct got_object_id *, struct got_object_id *,
716 const char *, const char *, int, int, int, struct tog_view *,
717 struct got_repository *);
718 static const struct got_error *show_diff_view(struct tog_view *);
719 static const struct got_error *input_diff_view(struct tog_view **,
720 struct tog_view *, int);
721 static const struct got_error *reset_diff_view(struct tog_view *);
722 static const struct got_error* close_diff_view(struct tog_view *);
723 static const struct got_error *search_start_diff_view(struct tog_view *);
724 static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
725 size_t *, int **, int **, int **, int **);
726 static const struct got_error *search_next_view_match(struct tog_view *);
728 static const struct got_error *open_log_view(struct tog_view *,
729 struct got_object_id *, struct got_repository *,
730 const char *, const char *, int);
731 static const struct got_error * show_log_view(struct tog_view *);
732 static const struct got_error *input_log_view(struct tog_view **,
733 struct tog_view *, int);
734 static const struct got_error *resize_log_view(struct tog_view *, int);
735 static const struct got_error *close_log_view(struct tog_view *);
736 static const struct got_error *search_start_log_view(struct tog_view *);
737 static const struct got_error *search_next_log_view(struct tog_view *);
739 static const struct got_error *open_blame_view(struct tog_view *, char *,
740 struct got_object_id *, struct got_repository *);
741 static const struct got_error *show_blame_view(struct tog_view *);
742 static const struct got_error *input_blame_view(struct tog_view **,
743 struct tog_view *, int);
744 static const struct got_error *reset_blame_view(struct tog_view *);
745 static const struct got_error *close_blame_view(struct tog_view *);
746 static const struct got_error *search_start_blame_view(struct tog_view *);
747 static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
748 size_t *, int **, int **, int **, int **);
750 static const struct got_error *open_tree_view(struct tog_view *,
751 struct got_object_id *, const char *, struct got_repository *);
752 static const struct got_error *show_tree_view(struct tog_view *);
753 static const struct got_error *input_tree_view(struct tog_view **,
754 struct tog_view *, int);
755 static const struct got_error *close_tree_view(struct tog_view *);
756 static const struct got_error *search_start_tree_view(struct tog_view *);
757 static const struct got_error *search_next_tree_view(struct tog_view *);
759 static const struct got_error *open_ref_view(struct tog_view *,
760 struct got_repository *);
761 static const struct got_error *show_ref_view(struct tog_view *);
762 static const struct got_error *input_ref_view(struct tog_view **,
763 struct tog_view *, int);
764 static const struct got_error *close_ref_view(struct tog_view *);
765 static const struct got_error *search_start_ref_view(struct tog_view *);
766 static const struct got_error *search_next_ref_view(struct tog_view *);
768 static const struct got_error *open_help_view(struct tog_view *,
770 static const struct got_error *show_help_view(struct tog_view *);
771 static const struct got_error *input_help_view(struct tog_view **,
772 struct tog_view *, int);
773 static const struct got_error *reset_help_view(struct tog_view *);
774 static const struct got_error* close_help_view(struct tog_view *);
775 static const struct got_error *search_start_help_view(struct tog_view *);
776 static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
777 size_t *, int **, int **, int **, int **);
779 static volatile sig_atomic_t tog_sigwinch_received;
780 static volatile sig_atomic_t tog_sigpipe_received;
781 static volatile sig_atomic_t tog_sigcont_received;
782 static volatile sig_atomic_t tog_sigint_received;
783 static volatile sig_atomic_t tog_sigterm_received;
786 tog_sigwinch(int signo)
788 tog_sigwinch_received = 1;
792 tog_sigpipe(int signo)
794 tog_sigpipe_received = 1;
798 tog_sigcont(int signo)
800 tog_sigcont_received = 1;
804 tog_sigint(int signo)
806 tog_sigint_received = 1;
810 tog_sigterm(int signo)
812 tog_sigterm_received = 1;
816 tog_fatal_signal_received(void)
818 return (tog_sigpipe_received ||
819 tog_sigint_received || tog_sigterm_received);
822 static const struct got_error *
823 view_close(struct tog_view *view)
825 const struct got_error *err = NULL, *child_err = NULL;
828 child_err = view_close(view->child);
832 err = view->close(view);
834 del_panel(view->panel);
836 delwin(view->window);
838 return err ? err : child_err;
841 static struct tog_view *
842 view_open(int nlines, int ncols, int begin_y, int begin_x,
843 enum tog_view_type type)
845 struct tog_view *view = calloc(1, sizeof(*view));
853 view->nlines = nlines ? nlines : LINES - begin_y;
854 view->ncols = ncols ? ncols : COLS - begin_x;
855 view->begin_y = begin_y;
856 view->begin_x = begin_x;
857 view->window = newwin(nlines, ncols, begin_y, begin_x);
858 if (view->window == NULL) {
862 view->panel = new_panel(view->window);
863 if (view->panel == NULL ||
864 set_panel_userptr(view->panel, view) != OK) {
869 keypad(view->window, TRUE);
874 view_split_begin_x(int begin_x)
876 if (begin_x > 0 || COLS < 120)
878 return (COLS - MAX(COLS / 2, 80));
881 /* XXX Stub till we decide what to do. */
883 view_split_begin_y(int lines)
885 return lines * HSPLIT_SCALE;
888 static const struct got_error *view_resize(struct tog_view *);
890 static const struct got_error *
891 view_splitscreen(struct tog_view *view)
893 const struct got_error *err = NULL;
895 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
896 if (view->resized_y && view->resized_y < view->lines)
897 view->begin_y = view->resized_y;
899 view->begin_y = view_split_begin_y(view->nlines);
901 } else if (!view->resized) {
902 if (view->resized_x && view->resized_x < view->cols - 1 &&
904 view->begin_x = view->resized_x;
906 view->begin_x = view_split_begin_x(0);
909 view->nlines = LINES - view->begin_y;
910 view->ncols = COLS - view->begin_x;
913 err = view_resize(view);
917 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
918 view->parent->nlines = view->begin_y;
920 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
921 return got_error_from_errno("mvwin");
926 static const struct got_error *
927 view_fullscreen(struct tog_view *view)
929 const struct got_error *err = NULL;
932 view->begin_y = view->resized ? view->begin_y : 0;
933 view->nlines = view->resized ? view->nlines : LINES;
937 err = view_resize(view);
941 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
942 return got_error_from_errno("mvwin");
948 view_is_parent_view(struct tog_view *view)
950 return view->parent == NULL;
954 view_is_splitscreen(struct tog_view *view)
956 return view->begin_x > 0 || view->begin_y > 0;
960 view_is_fullscreen(struct tog_view *view)
962 return view->nlines == LINES && view->ncols == COLS;
966 view_is_hsplit_top(struct tog_view *view)
968 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
969 view_is_splitscreen(view->child);
973 view_border(struct tog_view *view)
976 const struct tog_view *view_above;
979 return view_border(view->parent);
981 panel = panel_above(view->panel);
985 view_above = panel_userptr(panel);
986 if (view->mode == TOG_VIEW_SPLIT_HRZN)
987 mvwhline(view->window, view_above->begin_y - 1,
988 view->begin_x, got_locale_is_utf8() ?
989 ACS_HLINE : '-', view->ncols);
991 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
992 got_locale_is_utf8() ? 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;
1498 path = getenv("TOG_SCR_DUMP");
1499 if (path == NULL || *path == '\0')
1500 return got_error_msg(GOT_ERR_BAD_PATH,
1501 "TOG_SCR_DUMP path not set to capture screen dump");
1502 f = fopen(path, "wex");
1504 return got_error_from_errno_fmt("fopen: %s", path);
1506 if ((view->child && view->child->begin_x) ||
1507 (view->parent && view->begin_x)) {
1508 int ncols = view->child ? view->ncols : view->parent->ncols;
1510 /* vertical splitscreen */
1511 for (i = 0; i < view->nlines; ++i) {
1512 err = view_write_line(f, i, ncols - 1);
1519 /* fullscreen or horizontal splitscreen */
1520 if ((view->child && view->child->begin_y) ||
1521 (view->parent && view->begin_y)) /* hsplit */
1522 hline = view->child ?
1523 view->child->begin_y : view->begin_y;
1525 for (i = 0; i < view->lines; i++) {
1526 if (hline && i == hline - 1) {
1529 /* ACS_HLINE writes out as 'q', overwrite it */
1530 for (c = 0; c < view->cols; ++c)
1536 err = view_write_line(f, i, 0);
1543 if (f && fclose(f) == EOF && err == NULL)
1544 err = got_ferror(f, GOT_ERR_IO);
1549 * Compute view->count from numeric input. Assign total to view->count and
1550 * return first non-numeric key entered.
1553 get_compound_key(struct tog_view *view, int c)
1555 struct tog_view *v = view;
1558 if (view_is_hsplit_top(view))
1560 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1564 cbreak(); /* block for input */
1565 nodelay(view->window, FALSE);
1566 wmove(v->window, v->nlines - 1, 0);
1567 wclrtoeol(v->window);
1568 waddch(v->window, ':');
1571 x = getcurx(v->window);
1572 if (x != ERR && x < view->ncols) {
1573 waddch(v->window, c);
1574 wrefresh(v->window);
1578 * Don't overflow. Max valid request should be the greatest
1579 * between the longest and total lines; cap at 10 million.
1584 n = n * 10 + (c - '0');
1585 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1587 if (c == 'G' || c == 'g') { /* nG key map */
1588 view->gline = view->hiline = n;
1593 /* Massage excessive or inapplicable values at the input handler. */
1600 action_report(struct tog_view *view)
1602 struct tog_view *v = view;
1604 if (view_is_hsplit_top(view))
1606 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1609 wmove(v->window, v->nlines - 1, 0);
1610 wclrtoeol(v->window);
1611 wprintw(v->window, ":%s", view->action);
1612 wrefresh(v->window);
1615 * Clear action status report. Only clear in blame view
1616 * once annotating is complete, otherwise it's too fast.
1618 if (view->type == TOG_VIEW_BLAME) {
1619 if (view->state.blame.blame_complete)
1620 view->action = NULL;
1622 view->action = NULL;
1626 * Read the next line from the test script and assign
1627 * key instruction to *ch. If at EOF, set the *done flag.
1629 static const struct got_error *
1630 tog_read_script_key(FILE *script, int *ch, int *done)
1632 const struct got_error *err = NULL;
1638 if (getline(&line, &linesz, script) == -1) {
1643 err = got_ferror(script, GOT_ERR_IO);
1648 if (strncasecmp(line, "WAIT_FOR_UI", 11) == 0)
1649 tog_io.wait_for_ui = 1;
1650 else if (strncasecmp(line, "KEY_ENTER", 9) == 0)
1652 else if (strncasecmp(line, "KEY_RIGHT", 9) == 0)
1654 else if (strncasecmp(line, "KEY_LEFT", 8) == 0)
1656 else if (strncasecmp(line, "KEY_DOWN", 8) == 0)
1658 else if (strncasecmp(line, "KEY_UP", 6) == 0)
1660 else if (strncasecmp(line, "SCREENDUMP", 10) == 0)
1661 *ch = TOG_KEY_SCRDUMP;
1670 static const struct got_error *
1671 view_input(struct tog_view **new, int *done, struct tog_view *view,
1672 struct tog_view_list_head *views, int fast_refresh)
1674 const struct got_error *err = NULL;
1681 action_report(view);
1683 /* Clear "no matches" indicator. */
1684 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1685 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1686 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1690 if (view->searching && !view->search_next_done) {
1691 errcode = pthread_mutex_unlock(&tog_mutex);
1693 return got_error_set_errno(errcode,
1694 "pthread_mutex_unlock");
1696 errcode = pthread_mutex_lock(&tog_mutex);
1698 return got_error_set_errno(errcode,
1699 "pthread_mutex_lock");
1700 view->search_next(view);
1704 /* Allow threads to make progress while we are waiting for input. */
1705 errcode = pthread_mutex_unlock(&tog_mutex);
1707 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1709 if (using_mock_io) {
1710 err = tog_read_script_key(tog_io.f, &ch, done);
1712 errcode = pthread_mutex_lock(&tog_mutex);
1715 } else if (view->count && --view->count) {
1717 nodelay(view->window, TRUE);
1718 ch = wgetch(view->window);
1719 /* let C-g or backspace abort unfinished count */
1720 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1725 ch = wgetch(view->window);
1726 if (ch >= '1' && ch <= '9')
1727 view->ch = ch = get_compound_key(view, ch);
1729 if (view->hiline && ch != ERR && ch != 0)
1730 view->hiline = 0; /* key pressed, clear line highlight */
1731 nodelay(view->window, TRUE);
1732 errcode = pthread_mutex_lock(&tog_mutex);
1734 return got_error_set_errno(errcode, "pthread_mutex_lock");
1736 if (tog_sigwinch_received || tog_sigcont_received) {
1738 tog_sigwinch_received = 0;
1739 tog_sigcont_received = 0;
1740 TAILQ_FOREACH(v, views, entry) {
1741 err = view_resize(v);
1744 err = v->input(new, v, KEY_RESIZE);
1748 err = view_resize(v->child);
1751 err = v->child->input(new, v->child,
1755 if (v->child->resized_x || v->child->resized_y) {
1756 err = view_resize_split(v, 0);
1768 if (view->type == TOG_VIEW_HELP)
1769 err = view->reset(view);
1771 err = view_request_new(new, view, TOG_VIEW_HELP);
1777 view->child->focussed = 1;
1778 view->focus_child = 1;
1779 } else if (view->parent) {
1781 view->parent->focussed = 1;
1782 view->parent->focus_child = 0;
1783 if (!view_is_splitscreen(view)) {
1784 if (view->parent->resize) {
1785 err = view->parent->resize(view->parent,
1790 offset_selection_up(view->parent);
1791 err = view_fullscreen(view->parent);
1798 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1799 if (view->parent->resize) {
1800 /* might need more commits to fill fullscreen */
1801 err = view->parent->resize(view->parent, 0);
1805 offset_selection_up(view->parent);
1807 err = view->input(new, view, ch);
1815 if (view_is_parent_view(view)) {
1816 if (view->child == NULL)
1818 if (view_is_splitscreen(view->child)) {
1820 view->child->focussed = 1;
1821 err = view_fullscreen(view->child);
1823 err = view_splitscreen(view->child);
1825 err = view_resize_split(view, 0);
1829 err = view->child->input(new, view->child,
1832 if (view_is_splitscreen(view)) {
1833 view->parent->focussed = 0;
1835 err = view_fullscreen(view);
1837 err = view_splitscreen(view);
1838 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1839 err = view_resize(view->parent);
1841 err = view_resize_split(view, 0);
1845 err = view->input(new, view, KEY_RESIZE);
1850 err = view->resize(view, 0);
1855 err = offset_selection_down(view->parent);
1857 err = offset_selection_down(view);
1861 err = switch_split(view);
1864 err = view_resize_split(view, -1);
1867 err = view_resize_split(view, 1);
1873 if (view->search_start)
1874 view_search_start(view, fast_refresh);
1876 err = view->input(new, view, ch);
1880 if (view->search_started && view->search_next) {
1881 view->searching = (ch == 'n' ?
1882 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1883 view->search_next_done = 0;
1884 view->search_next(view);
1886 err = view->input(new, view, ch);
1889 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1890 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1891 view->action = "Patience diff algorithm";
1893 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1894 view->action = "Myers diff algorithm";
1896 TAILQ_FOREACH(v, views, entry) {
1902 if (v->child && v->child->reset) {
1903 err = v->child->reset(v->child);
1909 case TOG_KEY_SCRDUMP:
1910 err = screendump(view);
1913 err = view->input(new, view, ch);
1921 view_needs_focus_indication(struct tog_view *view)
1923 if (view_is_parent_view(view)) {
1924 if (view->child == NULL || view->child->focussed)
1926 if (!view_is_splitscreen(view->child))
1928 } else if (!view_is_splitscreen(view))
1931 return view->focussed;
1934 static const struct got_error *
1937 const struct got_error *err = NULL;
1939 if (tog_io.cin && fclose(tog_io.cin) == EOF)
1940 err = got_ferror(tog_io.cin, GOT_ERR_IO);
1941 if (tog_io.cout && fclose(tog_io.cout) == EOF && err == NULL)
1942 err = got_ferror(tog_io.cout, GOT_ERR_IO);
1943 if (tog_io.f && fclose(tog_io.f) == EOF && err == NULL)
1944 err = got_ferror(tog_io.f, GOT_ERR_IO);
1949 static const struct got_error *
1950 view_loop(struct tog_view *view)
1952 const struct got_error *err = NULL;
1953 struct tog_view_list_head views;
1954 struct tog_view *new_view;
1956 int fast_refresh = 10;
1957 int done = 0, errcode;
1959 mode = getenv("TOG_VIEW_SPLIT_MODE");
1960 if (!mode || !(*mode == 'h' || *mode == 'H'))
1961 view->mode = TOG_VIEW_SPLIT_VERT;
1963 view->mode = TOG_VIEW_SPLIT_HRZN;
1965 errcode = pthread_mutex_lock(&tog_mutex);
1967 return got_error_set_errno(errcode, "pthread_mutex_lock");
1970 TAILQ_INSERT_HEAD(&views, view, entry);
1973 err = view->show(view);
1978 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1979 !tog_fatal_signal_received()) {
1980 /* Refresh fast during initialization, then become slower. */
1981 if (fast_refresh && --fast_refresh == 0 && !using_mock_io)
1982 halfdelay(10); /* switch to once per second */
1984 err = view_input(&new_view, &done, view, &views, fast_refresh);
1988 if (view->dying && view == TAILQ_FIRST(&views) &&
1989 TAILQ_NEXT(view, entry) == NULL)
1995 * When we quit, scroll the screen up a single line
1996 * so we don't lose any information.
1998 TAILQ_FOREACH(v, &views, entry) {
1999 wmove(v->window, 0, 0);
2000 wdeleteln(v->window);
2001 wnoutrefresh(v->window);
2002 if (v->child && !view_is_fullscreen(v)) {
2003 wmove(v->child->window, 0, 0);
2004 wdeleteln(v->child->window);
2005 wnoutrefresh(v->child->window);
2012 struct tog_view *v, *prev = NULL;
2014 if (view_is_parent_view(view))
2015 prev = TAILQ_PREV(view, tog_view_list_head,
2017 else if (view->parent)
2018 prev = view->parent;
2021 view->parent->child = NULL;
2022 view->parent->focus_child = 0;
2023 /* Restore fullscreen line height. */
2024 view->parent->nlines = view->parent->lines;
2025 err = view_resize(view->parent);
2028 /* Make resized splits persist. */
2029 view_transfer_size(view->parent, view);
2031 TAILQ_REMOVE(&views, view, entry);
2033 err = view_close(view);
2038 TAILQ_FOREACH(v, &views, entry) {
2042 if (view == NULL && new_view == NULL) {
2043 /* No view has focus. Try to pick one. */
2046 else if (!TAILQ_EMPTY(&views)) {
2047 view = TAILQ_LAST(&views,
2048 tog_view_list_head);
2051 if (view->focus_child) {
2052 view->child->focussed = 1;
2060 struct tog_view *v, *t;
2061 /* Only allow one parent view per type. */
2062 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
2063 if (v->type != new_view->type)
2065 TAILQ_REMOVE(&views, v, entry);
2066 err = view_close(v);
2071 TAILQ_INSERT_TAIL(&views, new_view, entry);
2074 if (view && !done) {
2075 if (view_is_parent_view(view)) {
2076 if (view->child && view->child->focussed)
2079 if (view->parent && view->parent->focussed)
2080 view = view->parent;
2082 show_panel(view->panel);
2083 if (view->child && view_is_splitscreen(view->child))
2084 show_panel(view->child->panel);
2085 if (view->parent && view_is_splitscreen(view)) {
2086 err = view->parent->show(view->parent);
2090 err = view->show(view);
2094 err = view->child->show(view->child);
2103 while (!TAILQ_EMPTY(&views)) {
2104 const struct got_error *close_err;
2105 view = TAILQ_FIRST(&views);
2106 TAILQ_REMOVE(&views, view, entry);
2107 close_err = view_close(view);
2108 if (close_err && err == NULL)
2112 errcode = pthread_mutex_unlock(&tog_mutex);
2113 if (errcode && err == NULL)
2114 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2124 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
2129 /* Create newly allocated wide-character string equivalent to a byte string. */
2130 static const struct got_error *
2131 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
2134 const struct got_error *err = NULL;
2137 *wlen = mbstowcs(NULL, s, 0);
2138 if (*wlen == (size_t)-1) {
2140 if (errno != EILSEQ)
2141 return got_error_from_errno("mbstowcs");
2143 /* byte string invalid in current encoding; try to "fix" it */
2144 err = got_mbsavis(&vis, &vislen, s);
2147 *wlen = mbstowcs(NULL, vis, 0);
2148 if (*wlen == (size_t)-1) {
2149 err = got_error_from_errno("mbstowcs"); /* give up */
2154 *ws = calloc(*wlen + 1, sizeof(**ws));
2156 err = got_error_from_errno("calloc");
2160 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
2161 err = got_error_from_errno("mbstowcs");
2172 static const struct got_error *
2173 expand_tab(char **ptr, const char *src)
2176 size_t len, n, idx = 0, sz = 0;
2179 n = len = strlen(src);
2180 dst = malloc(n + 1);
2182 return got_error_from_errno("malloc");
2184 while (idx < len && src[idx]) {
2185 const char c = src[idx];
2188 size_t nb = TABSIZE - sz % TABSIZE;
2191 p = realloc(dst, n + nb);
2194 return got_error_from_errno("realloc");
2199 memset(dst + sz, ' ', nb);
2202 dst[sz++] = src[idx];
2212 * Advance at most n columns from wline starting at offset off.
2213 * Return the index to the first character after the span operation.
2214 * Return the combined column width of all spanned wide character in
2218 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2220 int width, i, cols = 0;
2227 for (i = off; wline[i] != L'\0'; ++i) {
2228 if (wline[i] == L'\t')
2229 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2231 width = wcwidth(wline[i]);
2238 if (cols + width > n)
2248 * Format a line for display, ensuring that it won't overflow a width limit.
2249 * With scrolling, the width returned refers to the scrolled version of the
2250 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2252 static const struct got_error *
2253 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2254 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2256 const struct got_error *err = NULL;
2258 wchar_t *wline = NULL;
2267 err = expand_tab(&exstr, line);
2272 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2277 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2279 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2280 wline[wlen - 1] = L'\0';
2283 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2284 wline[wlen - 1] = L'\0';
2288 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2294 *scrollxp = scrollx;
2302 static const struct got_error*
2303 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2304 struct got_object_id *id, struct got_repository *repo)
2306 static const struct got_error *err = NULL;
2307 struct got_reflist_entry *re;
2313 TAILQ_FOREACH(re, refs, entry) {
2314 struct got_tag_object *tag = NULL;
2315 struct got_object_id *ref_id;
2318 name = got_ref_get_name(re->ref);
2319 if (strcmp(name, GOT_REF_HEAD) == 0)
2321 if (strncmp(name, "refs/", 5) == 0)
2323 if (strncmp(name, "got/", 4) == 0 &&
2324 strncmp(name, "got/backup/", 11) != 0)
2326 if (strncmp(name, "heads/", 6) == 0)
2328 if (strncmp(name, "remotes/", 8) == 0) {
2330 s = strstr(name, "/" GOT_REF_HEAD);
2331 if (s != NULL && s[strlen(s)] == '\0')
2334 err = got_ref_resolve(&ref_id, repo, re->ref);
2337 if (strncmp(name, "tags/", 5) == 0) {
2338 err = got_object_open_as_tag(&tag, repo, ref_id);
2340 if (err->code != GOT_ERR_OBJ_TYPE) {
2344 /* Ref points at something other than a tag. */
2349 cmp = got_object_id_cmp(tag ?
2350 got_object_tag_get_object_id(tag) : ref_id, id);
2353 got_object_tag_close(tag);
2357 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2358 s ? ", " : "", name) == -1) {
2359 err = got_error_from_errno("asprintf");
2370 static const struct got_error *
2371 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2376 smallerthan = strchr(author, '<');
2377 if (smallerthan && smallerthan[1] != '\0')
2378 author = smallerthan + 1;
2379 author[strcspn(author, "@>")] = '\0';
2380 return format_line(wauthor, author_width, NULL, author, 0, limit,
2384 static const struct got_error *
2385 draw_commit(struct tog_view *view, struct got_commit_object *commit,
2386 struct got_object_id *id, const size_t date_display_cols,
2387 int author_display_cols)
2389 struct tog_log_view_state *s = &view->state.log;
2390 const struct got_error *err = NULL;
2391 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2392 char *logmsg0 = NULL, *logmsg = NULL;
2393 char *author = NULL;
2394 wchar_t *wlogmsg = NULL, *wauthor = NULL;
2395 int author_width, logmsg_width;
2396 char *newline, *line = NULL;
2397 int col, limit, scrollx;
2398 const int avail = view->ncols;
2400 time_t committer_time;
2401 struct tog_color *tc;
2403 committer_time = got_object_commit_get_committer_time(commit);
2404 if (gmtime_r(&committer_time, &tm) == NULL)
2405 return got_error_from_errno("gmtime_r");
2406 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2407 return got_error(GOT_ERR_NO_SPACE);
2409 if (avail <= date_display_cols)
2410 limit = MIN(sizeof(datebuf) - 1, avail);
2412 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2413 tc = get_color(&s->colors, TOG_COLOR_DATE);
2415 wattr_on(view->window,
2416 COLOR_PAIR(tc->colorpair), NULL);
2417 waddnstr(view->window, datebuf, limit);
2419 wattr_off(view->window,
2420 COLOR_PAIR(tc->colorpair), NULL);
2427 err = got_object_id_str(&id_str, id);
2430 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2432 wattr_on(view->window,
2433 COLOR_PAIR(tc->colorpair), NULL);
2434 wprintw(view->window, "%.8s ", id_str);
2436 wattr_off(view->window,
2437 COLOR_PAIR(tc->colorpair), NULL);
2444 if (s->use_committer)
2445 author = strdup(got_object_commit_get_committer(commit));
2447 author = strdup(got_object_commit_get_author(commit));
2448 if (author == NULL) {
2449 err = got_error_from_errno("strdup");
2452 err = format_author(&wauthor, &author_width, author, avail - col, col);
2455 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2457 wattr_on(view->window,
2458 COLOR_PAIR(tc->colorpair), NULL);
2459 waddwstr(view->window, wauthor);
2460 col += author_width;
2461 while (col < avail && author_width < author_display_cols + 2) {
2462 waddch(view->window, ' ');
2467 wattr_off(view->window,
2468 COLOR_PAIR(tc->colorpair), NULL);
2472 err = got_object_commit_get_logmsg(&logmsg0, commit);
2476 while (*logmsg == '\n')
2478 newline = strchr(logmsg, '\n');
2481 limit = avail - col;
2482 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2483 limit--; /* for the border */
2484 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2488 waddwstr(view->window, &wlogmsg[scrollx]);
2489 col += MAX(logmsg_width, 0);
2490 while (col < avail) {
2491 waddch(view->window, ' ');
2503 static struct commit_queue_entry *
2504 alloc_commit_queue_entry(struct got_commit_object *commit,
2505 struct got_object_id *id)
2507 struct commit_queue_entry *entry;
2508 struct got_object_id *dup;
2510 entry = calloc(1, sizeof(*entry));
2514 dup = got_object_id_dup(id);
2521 entry->commit = commit;
2526 pop_commit(struct commit_queue *commits)
2528 struct commit_queue_entry *entry;
2530 entry = TAILQ_FIRST(&commits->head);
2531 TAILQ_REMOVE(&commits->head, entry, entry);
2532 got_object_commit_close(entry->commit);
2533 commits->ncommits--;
2539 free_commits(struct commit_queue *commits)
2541 while (!TAILQ_EMPTY(&commits->head))
2542 pop_commit(commits);
2545 static const struct got_error *
2546 match_commit(int *have_match, struct got_object_id *id,
2547 struct got_commit_object *commit, regex_t *regex)
2549 const struct got_error *err = NULL;
2550 regmatch_t regmatch;
2551 char *id_str = NULL, *logmsg = NULL;
2555 err = got_object_id_str(&id_str, id);
2559 err = got_object_commit_get_logmsg(&logmsg, commit);
2563 if (regexec(regex, got_object_commit_get_author(commit), 1,
2564 ®match, 0) == 0 ||
2565 regexec(regex, got_object_commit_get_committer(commit), 1,
2566 ®match, 0) == 0 ||
2567 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2568 regexec(regex, logmsg, 1, ®match, 0) == 0)
2576 static const struct got_error *
2577 queue_commits(struct tog_log_thread_args *a)
2579 const struct got_error *err = NULL;
2582 * We keep all commits open throughout the lifetime of the log
2583 * view in order to avoid having to re-fetch commits from disk
2584 * while updating the display.
2587 struct got_object_id id;
2588 struct got_commit_object *commit;
2589 struct commit_queue_entry *entry;
2590 int limit_match = 0;
2593 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2598 err = got_object_open_as_commit(&commit, a->repo, &id);
2601 entry = alloc_commit_queue_entry(commit, &id);
2602 if (entry == NULL) {
2603 err = got_error_from_errno("alloc_commit_queue_entry");
2607 errcode = pthread_mutex_lock(&tog_mutex);
2609 err = got_error_set_errno(errcode,
2610 "pthread_mutex_lock");
2614 entry->idx = a->real_commits->ncommits;
2615 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2616 a->real_commits->ncommits++;
2619 err = match_commit(&limit_match, &id, commit,
2625 struct commit_queue_entry *matched;
2627 matched = alloc_commit_queue_entry(
2628 entry->commit, entry->id);
2629 if (matched == NULL) {
2630 err = got_error_from_errno(
2631 "alloc_commit_queue_entry");
2634 matched->commit = entry->commit;
2635 got_object_commit_retain(entry->commit);
2637 matched->idx = a->limit_commits->ncommits;
2638 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2640 a->limit_commits->ncommits++;
2644 * This is how we signal log_thread() that we
2645 * have found a match, and that it should be
2646 * counted as a new entry for the view.
2648 a->limit_match = limit_match;
2651 if (*a->searching == TOG_SEARCH_FORWARD &&
2652 !*a->search_next_done) {
2654 err = match_commit(&have_match, &id, commit, a->regex);
2659 if (limit_match && have_match)
2660 *a->search_next_done =
2661 TOG_SEARCH_HAVE_MORE;
2662 } else if (have_match)
2663 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2666 errcode = pthread_mutex_unlock(&tog_mutex);
2667 if (errcode && err == NULL)
2668 err = got_error_set_errno(errcode,
2669 "pthread_mutex_unlock");
2672 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2678 select_commit(struct tog_log_view_state *s)
2680 struct commit_queue_entry *entry;
2683 entry = s->first_displayed_entry;
2685 if (ncommits == s->selected) {
2686 s->selected_entry = entry;
2689 entry = TAILQ_NEXT(entry, entry);
2694 static const struct got_error *
2695 draw_commits(struct tog_view *view)
2697 const struct got_error *err = NULL;
2698 struct tog_log_view_state *s = &view->state.log;
2699 struct commit_queue_entry *entry = s->selected_entry;
2700 int limit = view->nlines;
2702 int ncommits, author_cols = 4;
2703 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2704 char *refs_str = NULL;
2706 struct tog_color *tc;
2707 static const size_t date_display_cols = 12;
2709 if (view_is_hsplit_top(view))
2710 --limit; /* account for border */
2712 if (s->selected_entry &&
2713 !(view->searching && view->search_next_done == 0)) {
2714 struct got_reflist_head *refs;
2715 err = got_object_id_str(&id_str, s->selected_entry->id);
2718 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2719 s->selected_entry->id);
2721 err = build_refs_str(&refs_str, refs,
2722 s->selected_entry->id, s->repo);
2728 if (s->thread_args.commits_needed == 0 && !using_mock_io)
2729 halfdelay(10); /* disable fast refresh */
2731 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2732 if (asprintf(&ncommits_str, " [%d/%d] %s",
2733 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2734 (view->searching && !view->search_next_done) ?
2735 "searching..." : "loading...") == -1) {
2736 err = got_error_from_errno("asprintf");
2740 const char *search_str = NULL;
2741 const char *limit_str = NULL;
2743 if (view->searching) {
2744 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2745 search_str = "no more matches";
2746 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2747 search_str = "no matches found";
2748 else if (!view->search_next_done)
2749 search_str = "searching...";
2752 if (s->limit_view && s->commits->ncommits == 0)
2753 limit_str = "no matches found";
2755 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2756 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2757 search_str ? search_str : (refs_str ? refs_str : ""),
2758 limit_str ? limit_str : "") == -1) {
2759 err = got_error_from_errno("asprintf");
2764 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2765 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2766 "........................................",
2767 s->in_repo_path, ncommits_str) == -1) {
2768 err = got_error_from_errno("asprintf");
2772 } else if (asprintf(&header, "commit %s%s",
2773 id_str ? id_str : "........................................",
2774 ncommits_str) == -1) {
2775 err = got_error_from_errno("asprintf");
2779 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2783 werase(view->window);
2785 if (view_needs_focus_indication(view))
2786 wstandout(view->window);
2787 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2789 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2790 waddwstr(view->window, wline);
2791 while (width < view->ncols) {
2792 waddch(view->window, ' ');
2796 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2797 if (view_needs_focus_indication(view))
2798 wstandend(view->window);
2803 /* Grow author column size if necessary, and set view->maxx. */
2804 entry = s->first_displayed_entry;
2808 struct got_commit_object *c = entry->commit;
2809 char *author, *eol, *msg, *msg0;
2810 wchar_t *wauthor, *wmsg;
2812 if (ncommits >= limit - 1)
2814 if (s->use_committer)
2815 author = strdup(got_object_commit_get_committer(c));
2817 author = strdup(got_object_commit_get_author(c));
2818 if (author == NULL) {
2819 err = got_error_from_errno("strdup");
2822 err = format_author(&wauthor, &width, author, COLS,
2824 if (author_cols < width)
2825 author_cols = width;
2830 err = got_object_commit_get_logmsg(&msg0, c);
2834 while (*msg == '\n')
2836 if ((eol = strchr(msg, '\n')))
2838 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2839 date_display_cols + author_cols, 0);
2842 view->maxx = MAX(view->maxx, width);
2846 entry = TAILQ_NEXT(entry, entry);
2849 entry = s->first_displayed_entry;
2850 s->last_displayed_entry = s->first_displayed_entry;
2853 if (ncommits >= limit - 1)
2855 if (ncommits == s->selected)
2856 wstandout(view->window);
2857 err = draw_commit(view, entry->commit, entry->id,
2858 date_display_cols, author_cols);
2859 if (ncommits == s->selected)
2860 wstandend(view->window);
2864 s->last_displayed_entry = entry;
2865 entry = TAILQ_NEXT(entry, entry);
2878 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2880 struct commit_queue_entry *entry;
2883 entry = TAILQ_FIRST(&s->commits->head);
2884 if (s->first_displayed_entry == entry)
2887 entry = s->first_displayed_entry;
2888 while (entry && nscrolled < maxscroll) {
2889 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2891 s->first_displayed_entry = entry;
2897 static const struct got_error *
2898 trigger_log_thread(struct tog_view *view, int wait)
2900 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2904 halfdelay(1); /* fast refresh while loading commits */
2906 while (!ta->log_complete && !tog_thread_error &&
2907 (ta->commits_needed > 0 || ta->load_all)) {
2908 /* Wake the log thread. */
2909 errcode = pthread_cond_signal(&ta->need_commits);
2911 return got_error_set_errno(errcode,
2912 "pthread_cond_signal");
2915 * The mutex will be released while the view loop waits
2916 * in wgetch(), at which time the log thread will run.
2921 /* Display progress update in log view. */
2922 show_log_view(view);
2926 /* Wait right here while next commit is being loaded. */
2927 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2929 return got_error_set_errno(errcode,
2930 "pthread_cond_wait");
2932 /* Display progress update in log view. */
2933 show_log_view(view);
2941 static const struct got_error *
2942 request_log_commits(struct tog_view *view)
2944 struct tog_log_view_state *state = &view->state.log;
2945 const struct got_error *err = NULL;
2947 if (state->thread_args.log_complete)
2950 state->thread_args.commits_needed += view->nscrolled;
2951 err = trigger_log_thread(view, 1);
2952 view->nscrolled = 0;
2957 static const struct got_error *
2958 log_scroll_down(struct tog_view *view, int maxscroll)
2960 struct tog_log_view_state *s = &view->state.log;
2961 const struct got_error *err = NULL;
2962 struct commit_queue_entry *pentry;
2963 int nscrolled = 0, ncommits_needed;
2965 if (s->last_displayed_entry == NULL)
2968 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2969 if (s->commits->ncommits < ncommits_needed &&
2970 !s->thread_args.log_complete) {
2972 * Ask the log thread for required amount of commits.
2974 s->thread_args.commits_needed +=
2975 ncommits_needed - s->commits->ncommits;
2976 err = trigger_log_thread(view, 1);
2982 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2983 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2986 s->last_displayed_entry = pentry ?
2987 pentry : s->last_displayed_entry;
2989 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2992 s->first_displayed_entry = pentry;
2993 } while (++nscrolled < maxscroll);
2995 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2996 view->nscrolled += nscrolled;
2998 view->nscrolled = 0;
3003 static const struct got_error *
3004 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
3005 struct got_commit_object *commit, struct got_object_id *commit_id,
3006 struct tog_view *log_view, struct got_repository *repo)
3008 const struct got_error *err;
3009 struct got_object_qid *parent_id;
3010 struct tog_view *diff_view;
3012 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
3013 if (diff_view == NULL)
3014 return got_error_from_errno("view_open");
3016 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3017 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
3018 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
3020 *new_view = diff_view;
3024 static const struct got_error *
3025 tree_view_visit_subtree(struct tog_tree_view_state *s,
3026 struct got_tree_object *subtree)
3028 struct tog_parent_tree *parent;
3030 parent = calloc(1, sizeof(*parent));
3032 return got_error_from_errno("calloc");
3034 parent->tree = s->tree;
3035 parent->first_displayed_entry = s->first_displayed_entry;
3036 parent->selected_entry = s->selected_entry;
3037 parent->selected = s->selected;
3038 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3041 s->first_displayed_entry = NULL;
3045 static const struct got_error *
3046 tree_view_walk_path(struct tog_tree_view_state *s,
3047 struct got_commit_object *commit, const char *path)
3049 const struct got_error *err = NULL;
3050 struct got_tree_object *tree = NULL;
3052 char *slash, *subpath = NULL;
3054 /* Walk the path and open corresponding tree objects. */
3057 struct got_tree_entry *te;
3058 struct got_object_id *tree_id;
3064 /* Ensure the correct subtree entry is selected. */
3065 slash = strchr(p, '/');
3067 te_name = strdup(p);
3069 te_name = strndup(p, slash - p);
3070 if (te_name == NULL) {
3071 err = got_error_from_errno("strndup");
3074 te = got_object_tree_find_entry(s->tree, te_name);
3076 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
3081 s->first_displayed_entry = s->selected_entry = te;
3083 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
3084 break; /* jump to this file's entry */
3086 slash = strchr(p, '/');
3088 subpath = strndup(path, slash - path);
3090 subpath = strdup(path);
3091 if (subpath == NULL) {
3092 err = got_error_from_errno("strdup");
3096 err = got_object_id_by_path(&tree_id, s->repo, commit,
3101 err = got_object_open_as_tree(&tree, s->repo, tree_id);
3106 err = tree_view_visit_subtree(s, tree);
3108 got_object_tree_close(tree);
3122 static const struct got_error *
3123 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
3124 struct commit_queue_entry *entry, const char *path,
3125 const char *head_ref_name, struct got_repository *repo)
3127 const struct got_error *err = NULL;
3128 struct tog_tree_view_state *s;
3129 struct tog_view *tree_view;
3131 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
3132 if (tree_view == NULL)
3133 return got_error_from_errno("view_open");
3135 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
3138 s = &tree_view->state.tree;
3140 *new_view = tree_view;
3142 if (got_path_is_root_dir(path))
3145 return tree_view_walk_path(s, entry->commit, path);
3148 static const struct got_error *
3149 block_signals_used_by_main_thread(void)
3154 if (sigemptyset(&sigset) == -1)
3155 return got_error_from_errno("sigemptyset");
3157 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
3158 if (sigaddset(&sigset, SIGWINCH) == -1)
3159 return got_error_from_errno("sigaddset");
3160 if (sigaddset(&sigset, SIGCONT) == -1)
3161 return got_error_from_errno("sigaddset");
3162 if (sigaddset(&sigset, SIGINT) == -1)
3163 return got_error_from_errno("sigaddset");
3164 if (sigaddset(&sigset, SIGTERM) == -1)
3165 return got_error_from_errno("sigaddset");
3167 /* ncurses handles SIGTSTP */
3168 if (sigaddset(&sigset, SIGTSTP) == -1)
3169 return got_error_from_errno("sigaddset");
3171 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
3173 return got_error_set_errno(errcode, "pthread_sigmask");
3179 log_thread(void *arg)
3181 const struct got_error *err = NULL;
3183 struct tog_log_thread_args *a = arg;
3187 * Sync startup with main thread such that we begin our
3188 * work once view_input() has released the mutex.
3190 errcode = pthread_mutex_lock(&tog_mutex);
3192 err = got_error_set_errno(errcode, "pthread_mutex_lock");
3196 err = block_signals_used_by_main_thread();
3198 pthread_mutex_unlock(&tog_mutex);
3202 while (!done && !err && !tog_fatal_signal_received()) {
3203 errcode = pthread_mutex_unlock(&tog_mutex);
3205 err = got_error_set_errno(errcode,
3206 "pthread_mutex_unlock");
3209 err = queue_commits(a);
3211 if (err->code != GOT_ERR_ITER_COMPLETED)
3215 } else if (a->commits_needed > 0 && !a->load_all) {
3218 a->commits_needed--;
3220 a->commits_needed--;
3223 errcode = pthread_mutex_lock(&tog_mutex);
3225 err = got_error_set_errno(errcode,
3226 "pthread_mutex_lock");
3228 } else if (*a->quit)
3230 else if (*a->limiting && *a->first_displayed_entry == NULL) {
3231 *a->first_displayed_entry =
3232 TAILQ_FIRST(&a->limit_commits->head);
3233 *a->selected_entry = *a->first_displayed_entry;
3234 } else if (*a->first_displayed_entry == NULL) {
3235 *a->first_displayed_entry =
3236 TAILQ_FIRST(&a->real_commits->head);
3237 *a->selected_entry = *a->first_displayed_entry;
3240 errcode = pthread_cond_signal(&a->commit_loaded);
3242 err = got_error_set_errno(errcode,
3243 "pthread_cond_signal");
3244 pthread_mutex_unlock(&tog_mutex);
3249 a->commits_needed = 0;
3251 if (a->commits_needed == 0 && !a->load_all) {
3252 errcode = pthread_cond_wait(&a->need_commits,
3255 err = got_error_set_errno(errcode,
3256 "pthread_cond_wait");
3257 pthread_mutex_unlock(&tog_mutex);
3265 a->log_complete = 1;
3266 errcode = pthread_mutex_unlock(&tog_mutex);
3268 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3271 tog_thread_error = 1;
3272 pthread_cond_signal(&a->commit_loaded);
3277 static const struct got_error *
3278 stop_log_thread(struct tog_log_view_state *s)
3280 const struct got_error *err = NULL, *thread_err = NULL;
3285 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3287 return got_error_set_errno(errcode,
3288 "pthread_cond_signal");
3289 errcode = pthread_mutex_unlock(&tog_mutex);
3291 return got_error_set_errno(errcode,
3292 "pthread_mutex_unlock");
3293 errcode = pthread_join(s->thread, (void **)&thread_err);
3295 return got_error_set_errno(errcode, "pthread_join");
3296 errcode = pthread_mutex_lock(&tog_mutex);
3298 return got_error_set_errno(errcode,
3299 "pthread_mutex_lock");
3300 s->thread = 0; //NULL;
3303 if (s->thread_args.repo) {
3304 err = got_repo_close(s->thread_args.repo);
3305 s->thread_args.repo = NULL;
3308 if (s->thread_args.pack_fds) {
3309 const struct got_error *pack_err =
3310 got_repo_pack_fds_close(s->thread_args.pack_fds);
3313 s->thread_args.pack_fds = NULL;
3316 if (s->thread_args.graph) {
3317 got_commit_graph_close(s->thread_args.graph);
3318 s->thread_args.graph = NULL;
3321 return err ? err : thread_err;
3324 static const struct got_error *
3325 close_log_view(struct tog_view *view)
3327 const struct got_error *err = NULL;
3328 struct tog_log_view_state *s = &view->state.log;
3331 err = stop_log_thread(s);
3333 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3334 if (errcode && err == NULL)
3335 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3337 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3338 if (errcode && err == NULL)
3339 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3341 free_commits(&s->limit_commits);
3342 free_commits(&s->real_commits);
3343 free(s->in_repo_path);
3344 s->in_repo_path = NULL;
3347 free(s->head_ref_name);
3348 s->head_ref_name = NULL;
3353 * We use two queues to implement the limit feature: first consists of
3354 * commits matching the current limit_regex; second is the real queue
3355 * of all known commits (real_commits). When the user starts limiting,
3356 * we swap queues such that all movement and displaying functionality
3357 * works with very slight change.
3359 static const struct got_error *
3360 limit_log_view(struct tog_view *view)
3362 struct tog_log_view_state *s = &view->state.log;
3363 struct commit_queue_entry *entry;
3364 struct tog_view *v = view;
3365 const struct got_error *err = NULL;
3369 if (view_is_hsplit_top(view))
3371 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3374 /* Get the pattern */
3375 wmove(v->window, v->nlines - 1, 0);
3376 wclrtoeol(v->window);
3377 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3378 nodelay(v->window, FALSE);
3381 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3384 nodelay(v->window, TRUE);
3388 if (*pattern == '\0') {
3390 * Safety measure for the situation where the user
3391 * resets limit without previously limiting anything.
3397 * User could have pressed Ctrl+L, which refreshed the
3398 * commit queues, it means we can't save previously
3399 * (before limit took place) displayed entries,
3400 * because they would point to already free'ed memory,
3401 * so we are forced to always select first entry of
3404 s->commits = &s->real_commits;
3405 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3406 s->selected_entry = s->first_displayed_entry;
3413 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3418 /* Clear the screen while loading limit view */
3419 s->first_displayed_entry = NULL;
3420 s->last_displayed_entry = NULL;
3421 s->selected_entry = NULL;
3422 s->commits = &s->limit_commits;
3424 /* Prepare limit queue for new search */
3425 free_commits(&s->limit_commits);
3426 s->limit_commits.ncommits = 0;
3428 /* First process commits, which are in queue already */
3429 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3432 err = match_commit(&have_match, entry->id,
3433 entry->commit, &s->limit_regex);
3438 struct commit_queue_entry *matched;
3440 matched = alloc_commit_queue_entry(entry->commit,
3442 if (matched == NULL) {
3443 err = got_error_from_errno(
3444 "alloc_commit_queue_entry");
3447 matched->commit = entry->commit;
3448 got_object_commit_retain(entry->commit);
3450 matched->idx = s->limit_commits.ncommits;
3451 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3453 s->limit_commits.ncommits++;
3457 /* Second process all the commits, until we fill the screen */
3458 if (s->limit_commits.ncommits < view->nlines - 1 &&
3459 !s->thread_args.log_complete) {
3460 s->thread_args.commits_needed +=
3461 view->nlines - s->limit_commits.ncommits - 1;
3462 err = trigger_log_thread(view, 1);
3467 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3468 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3474 static const struct got_error *
3475 search_start_log_view(struct tog_view *view)
3477 struct tog_log_view_state *s = &view->state.log;
3479 s->matched_entry = NULL;
3480 s->search_entry = NULL;
3484 static const struct got_error *
3485 search_next_log_view(struct tog_view *view)
3487 const struct got_error *err = NULL;
3488 struct tog_log_view_state *s = &view->state.log;
3489 struct commit_queue_entry *entry;
3491 /* Display progress update in log view. */
3492 show_log_view(view);
3496 if (s->search_entry) {
3498 errcode = pthread_mutex_unlock(&tog_mutex);
3500 return got_error_set_errno(errcode,
3501 "pthread_mutex_unlock");
3502 ch = wgetch(view->window);
3503 errcode = pthread_mutex_lock(&tog_mutex);
3505 return got_error_set_errno(errcode,
3506 "pthread_mutex_lock");
3507 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3508 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3511 if (view->searching == TOG_SEARCH_FORWARD)
3512 entry = TAILQ_NEXT(s->search_entry, entry);
3514 entry = TAILQ_PREV(s->search_entry,
3515 commit_queue_head, entry);
3516 } else if (s->matched_entry) {
3518 * If the user has moved the cursor after we hit a match,
3519 * the position from where we should continue searching
3520 * might have changed.
3522 if (view->searching == TOG_SEARCH_FORWARD)
3523 entry = TAILQ_NEXT(s->selected_entry, entry);
3525 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3528 entry = s->selected_entry;
3534 if (entry == NULL) {
3535 if (s->thread_args.log_complete ||
3536 view->searching == TOG_SEARCH_BACKWARD) {
3537 view->search_next_done =
3538 (s->matched_entry == NULL ?
3539 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3540 s->search_entry = NULL;
3544 * Poke the log thread for more commits and return,
3545 * allowing the main loop to make progress. Search
3546 * will resume at s->search_entry once we come back.
3548 s->thread_args.commits_needed++;
3549 return trigger_log_thread(view, 0);
3552 err = match_commit(&have_match, entry->id, entry->commit,
3557 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3558 s->matched_entry = entry;
3562 s->search_entry = entry;
3563 if (view->searching == TOG_SEARCH_FORWARD)
3564 entry = TAILQ_NEXT(entry, entry);
3566 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3569 if (s->matched_entry) {
3570 int cur = s->selected_entry->idx;
3571 while (cur < s->matched_entry->idx) {
3572 err = input_log_view(NULL, view, KEY_DOWN);
3577 while (cur > s->matched_entry->idx) {
3578 err = input_log_view(NULL, view, KEY_UP);
3585 s->search_entry = NULL;
3590 static const struct got_error *
3591 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3592 struct got_repository *repo, const char *head_ref_name,
3593 const char *in_repo_path, int log_branches)
3595 const struct got_error *err = NULL;
3596 struct tog_log_view_state *s = &view->state.log;
3597 struct got_repository *thread_repo = NULL;
3598 struct got_commit_graph *thread_graph = NULL;
3601 if (in_repo_path != s->in_repo_path) {
3602 free(s->in_repo_path);
3603 s->in_repo_path = strdup(in_repo_path);
3604 if (s->in_repo_path == NULL) {
3605 err = got_error_from_errno("strdup");
3610 /* The commit queue only contains commits being displayed. */
3611 TAILQ_INIT(&s->real_commits.head);
3612 s->real_commits.ncommits = 0;
3613 s->commits = &s->real_commits;
3615 TAILQ_INIT(&s->limit_commits.head);
3617 s->limit_commits.ncommits = 0;
3620 if (head_ref_name) {
3621 s->head_ref_name = strdup(head_ref_name);
3622 if (s->head_ref_name == NULL) {
3623 err = got_error_from_errno("strdup");
3627 s->start_id = got_object_id_dup(start_id);
3628 if (s->start_id == NULL) {
3629 err = got_error_from_errno("got_object_id_dup");
3632 s->log_branches = log_branches;
3633 s->use_committer = 1;
3635 STAILQ_INIT(&s->colors);
3636 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3637 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3638 get_color_value("TOG_COLOR_COMMIT"));
3641 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3642 get_color_value("TOG_COLOR_AUTHOR"));
3644 free_colors(&s->colors);
3647 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3648 get_color_value("TOG_COLOR_DATE"));
3650 free_colors(&s->colors);
3655 view->show = show_log_view;
3656 view->input = input_log_view;
3657 view->resize = resize_log_view;
3658 view->close = close_log_view;
3659 view->search_start = search_start_log_view;
3660 view->search_next = search_next_log_view;
3662 if (s->thread_args.pack_fds == NULL) {
3663 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3667 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3668 s->thread_args.pack_fds);
3671 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3675 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3676 s->repo, NULL, NULL);
3680 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3682 err = got_error_set_errno(errcode, "pthread_cond_init");
3685 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3687 err = got_error_set_errno(errcode, "pthread_cond_init");
3691 s->thread_args.commits_needed = view->nlines;
3692 s->thread_args.graph = thread_graph;
3693 s->thread_args.real_commits = &s->real_commits;
3694 s->thread_args.limit_commits = &s->limit_commits;
3695 s->thread_args.in_repo_path = s->in_repo_path;
3696 s->thread_args.start_id = s->start_id;
3697 s->thread_args.repo = thread_repo;
3698 s->thread_args.log_complete = 0;
3699 s->thread_args.quit = &s->quit;
3700 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3701 s->thread_args.selected_entry = &s->selected_entry;
3702 s->thread_args.searching = &view->searching;
3703 s->thread_args.search_next_done = &view->search_next_done;
3704 s->thread_args.regex = &view->regex;
3705 s->thread_args.limiting = &s->limit_view;
3706 s->thread_args.limit_regex = &s->limit_regex;
3707 s->thread_args.limit_commits = &s->limit_commits;
3710 if (view->close == NULL)
3711 close_log_view(view);
3717 static const struct got_error *
3718 show_log_view(struct tog_view *view)
3720 const struct got_error *err;
3721 struct tog_log_view_state *s = &view->state.log;
3723 if (s->thread == 0) { //NULL) {
3724 int errcode = pthread_create(&s->thread, NULL, log_thread,
3727 return got_error_set_errno(errcode, "pthread_create");
3728 if (s->thread_args.commits_needed > 0) {
3729 err = trigger_log_thread(view, 1);
3735 return draw_commits(view);
3739 log_move_cursor_up(struct tog_view *view, int page, int home)
3741 struct tog_log_view_state *s = &view->state.log;
3743 if (s->first_displayed_entry == NULL)
3745 if (s->selected_entry->idx == 0)
3748 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3750 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3752 if (!page && !home && s->selected > 0)
3755 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3761 static const struct got_error *
3762 log_move_cursor_down(struct tog_view *view, int page)
3764 struct tog_log_view_state *s = &view->state.log;
3765 const struct got_error *err = NULL;
3766 int eos = view->nlines - 2;
3768 if (s->first_displayed_entry == NULL)
3771 if (s->thread_args.log_complete &&
3772 s->selected_entry->idx >= s->commits->ncommits - 1)
3775 if (view_is_hsplit_top(view))
3776 --eos; /* border consumes the last line */
3779 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3782 err = log_scroll_down(view, 1);
3783 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3784 struct commit_queue_entry *entry;
3788 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3789 s->last_displayed_entry = entry;
3790 for (n = 0; n <= eos; n++) {
3793 s->first_displayed_entry = entry;
3794 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3797 s->selected = n - 1;
3799 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3800 s->thread_args.log_complete)
3801 s->selected += MIN(page,
3802 s->commits->ncommits - s->selected_entry->idx - 1);
3804 err = log_scroll_down(view, page);
3810 * We might necessarily overshoot in horizontal
3811 * splits; if so, select the last displayed commit.
3813 if (s->first_displayed_entry && s->last_displayed_entry) {
3814 s->selected = MIN(s->selected,
3815 s->last_displayed_entry->idx -
3816 s->first_displayed_entry->idx);
3821 if (s->thread_args.log_complete &&
3822 s->selected_entry->idx == s->commits->ncommits - 1)
3829 view_get_split(struct tog_view *view, int *y, int *x)
3834 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3835 if (view->child && view->child->resized_y)
3836 *y = view->child->resized_y;
3837 else if (view->resized_y)
3838 *y = view->resized_y;
3840 *y = view_split_begin_y(view->lines);
3841 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3842 if (view->child && view->child->resized_x)
3843 *x = view->child->resized_x;
3844 else if (view->resized_x)
3845 *x = view->resized_x;
3847 *x = view_split_begin_x(view->begin_x);
3851 /* Split view horizontally at y and offset view->state->selected line. */
3852 static const struct got_error *
3853 view_init_hsplit(struct tog_view *view, int y)
3855 const struct got_error *err = NULL;
3859 err = view_resize(view);
3863 err = offset_selection_down(view);
3868 static const struct got_error *
3869 log_goto_line(struct tog_view *view, int nlines)
3871 const struct got_error *err = NULL;
3872 struct tog_log_view_state *s = &view->state.log;
3873 int g, idx = s->selected_entry->idx;
3875 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3881 if (g >= s->first_displayed_entry->idx + 1 &&
3882 g <= s->last_displayed_entry->idx + 1 &&
3883 g - s->first_displayed_entry->idx - 1 < nlines) {
3884 s->selected = g - s->first_displayed_entry->idx - 1;
3890 err = log_move_cursor_down(view, g - idx - 1);
3891 if (!err && g > s->selected_entry->idx + 1)
3892 err = log_move_cursor_down(view,
3893 g - s->first_displayed_entry->idx - 1);
3896 } else if (idx + 1 > g)
3897 log_move_cursor_up(view, idx - g + 1, 0);
3899 if (g < nlines && s->first_displayed_entry->idx == 0)
3900 s->selected = g - 1;
3908 horizontal_scroll_input(struct tog_view *view, int ch)
3914 view->x -= MIN(view->x, 2);
3920 if (view->x + view->ncols / 2 < view->maxx)
3929 view->x = MAX(view->maxx - view->ncols / 2, 0);
3937 static const struct got_error *
3938 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3940 const struct got_error *err = NULL;
3941 struct tog_log_view_state *s = &view->state.log;
3944 if (s->thread_args.load_all) {
3945 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3946 s->thread_args.load_all = 0;
3947 else if (s->thread_args.log_complete) {
3948 err = log_move_cursor_down(view, s->commits->ncommits);
3949 s->thread_args.load_all = 0;
3955 eos = nscroll = view->nlines - 1;
3956 if (view_is_hsplit_top(view))
3960 return log_goto_line(view, eos);
3964 err = limit_log_view(view);
3975 horizontal_scroll_input(view, ch);
3982 log_move_cursor_up(view, 0, 0);
3987 log_move_cursor_up(view, 0, 1);
3997 log_move_cursor_up(view, nscroll, 0);
4004 err = log_move_cursor_down(view, 0);
4007 s->use_committer = !s->use_committer;
4008 view->action = s->use_committer ?
4009 "show committer" : "show commit author";
4014 /* We don't know yet how many commits, so we're forced to
4015 * traverse them all. */
4017 s->thread_args.load_all = 1;
4018 if (!s->thread_args.log_complete)
4019 return trigger_log_thread(view, 0);
4020 err = log_move_cursor_down(view, s->commits->ncommits);
4021 s->thread_args.load_all = 0;
4032 err = log_move_cursor_down(view, nscroll);
4035 if (s->selected > view->nlines - 2)
4036 s->selected = view->nlines - 2;
4037 if (s->selected > s->commits->ncommits - 1)
4038 s->selected = s->commits->ncommits - 1;
4040 if (s->commits->ncommits < view->nlines - 1 &&
4041 !s->thread_args.log_complete) {
4042 s->thread_args.commits_needed += (view->nlines - 1) -
4043 s->commits->ncommits;
4044 err = trigger_log_thread(view, 1);
4050 if (s->selected_entry == NULL)
4052 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4056 if (s->selected_entry == NULL)
4058 err = view_request_new(new_view, view, TOG_VIEW_TREE);
4064 if (ch == KEY_BACKSPACE &&
4065 got_path_is_root_dir(s->in_repo_path))
4067 err = stop_log_thread(s);
4070 if (ch == KEY_BACKSPACE) {
4072 err = got_path_dirname(&parent_path, s->in_repo_path);
4075 free(s->in_repo_path);
4076 s->in_repo_path = parent_path;
4077 s->thread_args.in_repo_path = s->in_repo_path;
4078 } else if (ch == CTRL('l')) {
4079 struct got_object_id *start_id;
4080 err = got_repo_match_object_id(&start_id, NULL,
4081 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4082 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4084 if (s->head_ref_name == NULL ||
4085 err->code != GOT_ERR_NOT_REF)
4087 /* Try to cope with deleted references. */
4088 free(s->head_ref_name);
4089 s->head_ref_name = NULL;
4090 err = got_repo_match_object_id(&start_id,
4091 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4092 &tog_refs, s->repo);
4097 s->start_id = start_id;
4098 s->thread_args.start_id = s->start_id;
4100 s->log_branches = !s->log_branches;
4102 if (s->thread_args.pack_fds == NULL) {
4103 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4107 err = got_repo_open(&s->thread_args.repo,
4108 got_repo_get_path(s->repo), NULL,
4109 s->thread_args.pack_fds);
4113 err = tog_load_refs(s->repo, 0);
4116 err = got_commit_graph_open(&s->thread_args.graph,
4117 s->in_repo_path, !s->log_branches);
4120 err = got_commit_graph_iter_start(s->thread_args.graph,
4121 s->start_id, s->repo, NULL, NULL);
4124 free_commits(&s->real_commits);
4125 free_commits(&s->limit_commits);
4126 s->first_displayed_entry = NULL;
4127 s->last_displayed_entry = NULL;
4128 s->selected_entry = NULL;
4130 s->thread_args.log_complete = 0;
4132 s->thread_args.commits_needed = view->lines;
4133 s->matched_entry = NULL;
4134 s->search_entry = NULL;
4139 err = view_request_new(new_view, view, TOG_VIEW_REF);
4149 static const struct got_error *
4150 apply_unveil(const char *repo_path, const char *worktree_path)
4152 const struct got_error *error;
4155 if (unveil("gmon.out", "rwc") != 0)
4156 return got_error_from_errno2("unveil", "gmon.out");
4158 if (repo_path && unveil(repo_path, "r") != 0)
4159 return got_error_from_errno2("unveil", repo_path);
4161 if (worktree_path && unveil(worktree_path, "rwc") != 0)
4162 return got_error_from_errno2("unveil", worktree_path);
4164 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4165 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4167 error = got_privsep_unveil_exec_helpers();
4171 if (unveil(NULL, NULL) != 0)
4172 return got_error_from_errno("unveil");
4177 static const struct got_error *
4178 init_mock_term(const char *test_script_path)
4180 const struct got_error *err = NULL;
4182 if (test_script_path == NULL || *test_script_path == '\0')
4183 return got_error_msg(GOT_ERR_IO, "TOG_TEST_SCRIPT not defined");
4185 tog_io.f = fopen(test_script_path, "re");
4186 if (tog_io.f == NULL) {
4187 err = got_error_from_errno_fmt("fopen: %s",
4192 /* test mode, we don't want any output */
4193 tog_io.cout = fopen("/dev/null", "w+");
4194 if (tog_io.cout == NULL) {
4195 err = got_error_from_errno("fopen: /dev/null");
4199 tog_io.cin = fopen("/dev/tty", "r+");
4200 if (tog_io.cin == NULL) {
4201 err = got_error_from_errno("fopen: /dev/tty");
4205 if (fseeko(tog_io.f, 0L, SEEK_SET) == -1) {
4206 err = got_error_from_errno("fseeko");
4210 if (newterm(NULL, tog_io.cout, tog_io.cin) == NULL)
4211 err = got_error_msg(GOT_ERR_IO,
4212 "newterm: failed to initialise curses");
4226 * Override default signal handlers before starting ncurses.
4227 * This should prevent ncurses from installing its own
4228 * broken cleanup() signal handler.
4230 signal(SIGWINCH, tog_sigwinch);
4231 signal(SIGPIPE, tog_sigpipe);
4232 signal(SIGCONT, tog_sigcont);
4233 signal(SIGINT, tog_sigint);
4234 signal(SIGTERM, tog_sigterm);
4236 if (using_mock_io) /* In test mode we use a fake terminal */
4242 halfdelay(1); /* Fast refresh while initial view is loading. */
4245 intrflush(stdscr, FALSE);
4246 keypad(stdscr, TRUE);
4248 if (getenv("TOG_COLORS") != NULL) {
4250 use_default_colors();
4256 static const struct got_error *
4257 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4258 struct got_repository *repo, struct got_worktree *worktree)
4260 const struct got_error *err = NULL;
4263 *in_repo_path = strdup("/");
4264 if (*in_repo_path == NULL)
4265 return got_error_from_errno("strdup");
4270 const char *prefix = got_worktree_get_path_prefix(worktree);
4273 err = got_worktree_resolve_path(&p, worktree, argv[0]);
4276 if (asprintf(in_repo_path, "%s%s%s", prefix,
4277 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4279 err = got_error_from_errno("asprintf");
4280 *in_repo_path = NULL;
4284 err = got_repo_map_path(in_repo_path, repo, argv[0]);
4289 static const struct got_error *
4290 cmd_log(int argc, char *argv[])
4292 const struct got_error *error;
4293 struct got_repository *repo = NULL;
4294 struct got_worktree *worktree = NULL;
4295 struct got_object_id *start_id = NULL;
4296 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4297 char *start_commit = NULL, *label = NULL;
4298 struct got_reference *ref = NULL;
4299 const char *head_ref_name = NULL;
4300 int ch, log_branches = 0;
4301 struct tog_view *view;
4302 int *pack_fds = NULL;
4304 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4310 start_commit = optarg;
4313 repo_path = realpath(optarg, NULL);
4314 if (repo_path == NULL)
4315 return got_error_from_errno2("realpath",
4330 error = got_repo_pack_fds_open(&pack_fds);
4334 if (repo_path == NULL) {
4335 cwd = getcwd(NULL, 0);
4337 return got_error_from_errno("getcwd");
4338 error = got_worktree_open(&worktree, cwd);
4339 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4343 strdup(got_worktree_get_repo_path(worktree));
4345 repo_path = strdup(cwd);
4346 if (repo_path == NULL) {
4347 error = got_error_from_errno("strdup");
4352 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4356 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4363 error = apply_unveil(got_repo_get_path(repo),
4364 worktree ? got_worktree_get_root_path(worktree) : NULL);
4368 /* already loaded by tog_log_with_path()? */
4369 if (TAILQ_EMPTY(&tog_refs)) {
4370 error = tog_load_refs(repo, 0);
4375 if (start_commit == NULL) {
4376 error = got_repo_match_object_id(&start_id, &label,
4377 worktree ? got_worktree_get_head_ref_name(worktree) :
4378 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4381 head_ref_name = label;
4383 error = got_ref_open(&ref, repo, start_commit, 0);
4385 head_ref_name = got_ref_get_name(ref);
4386 else if (error->code != GOT_ERR_NOT_REF)
4388 error = got_repo_match_object_id(&start_id, NULL,
4389 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4394 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4396 error = got_error_from_errno("view_open");
4399 error = open_log_view(view, start_id, repo, head_ref_name,
4400 in_repo_path, log_branches);
4404 /* Release work tree lock. */
4405 got_worktree_close(worktree);
4408 error = view_loop(view);
4418 const struct got_error *close_err = got_repo_close(repo);
4423 got_worktree_close(worktree);
4425 const struct got_error *pack_err =
4426 got_repo_pack_fds_close(pack_fds);
4438 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4439 "object1 object2\n", getprogname());
4444 match_line(const char *line, regex_t *regex, size_t nmatch,
4445 regmatch_t *regmatch)
4447 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4450 static struct tog_color *
4451 match_color(struct tog_colors *colors, const char *line)
4453 struct tog_color *tc = NULL;
4455 STAILQ_FOREACH(tc, colors, entry) {
4456 if (match_line(line, &tc->regex, 0, NULL))
4463 static const struct got_error *
4464 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4465 WINDOW *window, int skipcol, regmatch_t *regmatch)
4467 const struct got_error *err = NULL;
4469 wchar_t *wline = NULL;
4470 int rme, rms, n, width, scrollx;
4471 int width0 = 0, width1 = 0, width2 = 0;
4472 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4476 rms = regmatch->rm_so;
4477 rme = regmatch->rm_eo;
4479 err = expand_tab(&exstr, line);
4483 /* Split the line into 3 segments, according to match offsets. */
4484 seg0 = strndup(exstr, rms);
4486 err = got_error_from_errno("strndup");
4489 seg1 = strndup(exstr + rms, rme - rms);
4491 err = got_error_from_errno("strndup");
4494 seg2 = strdup(exstr + rme);
4496 err = got_error_from_errno("strndup");
4500 /* draw up to matched token if we haven't scrolled past it */
4501 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4505 n = MAX(width0 - skipcol, 0);
4508 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4509 wlimit, col_tab_align, 1);
4512 waddwstr(window, &wline[scrollx]);
4522 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4526 wlen = wcslen(wline);
4528 width = wcwidth(wline[i]);
4530 /* should not happen, tabs are expanded */
4531 err = got_error(GOT_ERR_RANGE);
4534 if (width0 + w + width > skipcol)
4539 /* draw (visible part of) matched token (if scrolled into it) */
4540 if (width1 - w > 0) {
4541 wattron(window, A_STANDOUT);
4542 waddwstr(window, &wline[i]);
4543 wattroff(window, A_STANDOUT);
4544 wlimit -= (width1 - w);
4545 *wtotal += (width1 - w);
4549 if (wlimit > 0) { /* draw rest of line */
4551 if (skipcol > width0 + width1) {
4552 err = format_line(&wline, &width2, &scrollx, seg2,
4553 skipcol - (width0 + width1), wlimit,
4557 waddwstr(window, &wline[scrollx]);
4559 err = format_line(&wline, &width2, NULL, seg2, 0,
4560 wlimit, col_tab_align, 1);
4563 waddwstr(window, wline);
4577 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4580 int *eof, *first, *selected;
4582 if (view->type == TOG_VIEW_DIFF) {
4583 struct tog_diff_view_state *s = &view->state.diff;
4585 first = &s->first_displayed_line;
4589 } else if (view->type == TOG_VIEW_HELP) {
4590 struct tog_help_view_state *s = &view->state.help;
4592 first = &s->first_displayed_line;
4596 } else if (view->type == TOG_VIEW_BLAME) {
4597 struct tog_blame_view_state *s = &view->state.blame;
4599 first = &s->first_displayed_line;
4600 selected = &s->selected_line;
4606 /* Center gline in the middle of the page like vi(1). */
4607 if (*lineno < view->gline - (view->nlines - 3) / 2)
4609 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4618 *selected = view->gline <= (view->nlines - 3) / 2 ?
4619 view->gline : (view->nlines - 3) / 2 + 1;
4625 static const struct got_error *
4626 draw_file(struct tog_view *view, const char *header)
4628 struct tog_diff_view_state *s = &view->state.diff;
4629 regmatch_t *regmatch = &view->regmatch;
4630 const struct got_error *err;
4633 size_t linesize = 0;
4637 int max_lines = view->nlines;
4638 int nlines = s->nlines;
4641 s->lineno = s->first_displayed_line - 1;
4642 line_offset = s->lines[s->first_displayed_line - 1].offset;
4643 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4644 return got_error_from_errno("fseek");
4646 werase(view->window);
4648 if (view->gline > s->nlines - 1)
4649 view->gline = s->nlines - 1;
4652 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4653 1 : view->gline - (view->nlines - 3) / 2 :
4654 s->lineno + s->selected_line;
4656 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4657 return got_error_from_errno("asprintf");
4658 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4664 if (view_needs_focus_indication(view))
4665 wstandout(view->window);
4666 waddwstr(view->window, wline);
4669 while (width++ < view->ncols)
4670 waddch(view->window, ' ');
4671 if (view_needs_focus_indication(view))
4672 wstandend(view->window);
4682 while (max_lines > 0 && nprinted < max_lines) {
4683 enum got_diff_line_type linetype;
4686 linelen = getline(&line, &linesize, s->f);
4687 if (linelen == -1) {
4693 return got_ferror(s->f, GOT_ERR_IO);
4696 if (++s->lineno < s->first_displayed_line)
4698 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4700 if (s->lineno == view->hiline)
4703 /* Set view->maxx based on full line length. */
4704 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4710 view->maxx = MAX(view->maxx, width);
4714 linetype = s->lines[s->lineno].type;
4715 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4716 linetype < GOT_DIFF_LINE_CONTEXT)
4717 attr |= COLOR_PAIR(linetype);
4719 wattron(view->window, attr);
4720 if (s->first_displayed_line + nprinted == s->matched_line &&
4721 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4722 err = add_matched_line(&width, line, view->ncols, 0,
4723 view->window, view->x, regmatch);
4730 err = format_line(&wline, &width, &skip, line,
4731 view->x, view->ncols, 0, view->x ? 1 : 0);
4736 waddwstr(view->window, &wline[skip]);
4740 if (s->lineno == view->hiline) {
4741 /* highlight full gline length */
4742 while (width++ < view->ncols)
4743 waddch(view->window, ' ');
4745 if (width <= view->ncols - 1)
4746 waddch(view->window, '\n');
4749 wattroff(view->window, attr);
4750 if (++nprinted == 1)
4751 s->first_displayed_line = s->lineno;
4755 s->last_displayed_line = s->first_displayed_line +
4758 s->last_displayed_line = s->first_displayed_line;
4763 while (nprinted < view->nlines) {
4764 waddch(view->window, '\n');
4768 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4774 wstandout(view->window);
4775 waddwstr(view->window, wline);
4778 wstandend(view->window);
4785 get_datestr(time_t *time, char *datebuf)
4787 struct tm mytm, *tm;
4790 tm = gmtime_r(time, &mytm);
4793 s = asctime_r(tm, datebuf);
4796 p = strchr(s, '\n');
4802 static const struct got_error *
4803 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4804 off_t off, uint8_t type)
4806 struct got_diff_line *p;
4808 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4810 return got_error_from_errno("reallocarray");
4812 (*lines)[*nlines].offset = off;
4813 (*lines)[*nlines].type = type;
4819 static const struct got_error *
4820 cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4821 struct got_diff_line *s_lines, size_t s_nlines)
4823 struct got_diff_line *p;
4827 if (fseeko(src, 0L, SEEK_SET) == -1)
4828 return got_error_from_errno("fseeko");
4831 r = fread(buf, 1, sizeof(buf), src);
4834 return got_error_from_errno("fread");
4838 if (fwrite(buf, 1, r, dst) != r)
4839 return got_ferror(dst, GOT_ERR_IO);
4842 if (s_nlines == 0 && *d_nlines == 0)
4846 * If commit info was in dst, increment line offsets
4847 * of the appended diff content, but skip s_lines[0]
4848 * because offset zero is already in *d_lines.
4850 if (*d_nlines > 0) {
4851 for (i = 1; i < s_nlines; ++i)
4852 s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4860 p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4862 /* d_lines is freed in close_diff_view() */
4863 return got_error_from_errno("reallocarray");
4868 memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
4869 *d_nlines += s_nlines;
4874 static const struct got_error *
4875 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4876 struct got_object_id *commit_id, struct got_reflist_head *refs,
4877 struct got_repository *repo, int ignore_ws, int force_text_diff,
4878 struct got_diffstat_cb_arg *dsa, FILE *outfile)
4880 const struct got_error *err = NULL;
4881 char datebuf[26], *datestr;
4882 struct got_commit_object *commit;
4883 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4884 time_t committer_time;
4885 const char *author, *committer;
4886 char *refs_str = NULL;
4887 struct got_pathlist_entry *pe;
4892 err = build_refs_str(&refs_str, refs, commit_id, repo);
4897 err = got_object_open_as_commit(&commit, repo, commit_id);
4901 err = got_object_id_str(&id_str, commit_id);
4903 err = got_error_from_errno("got_object_id_str");
4907 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4911 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4912 refs_str ? refs_str : "", refs_str ? ")" : "");
4914 err = got_error_from_errno("fprintf");
4918 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4922 n = fprintf(outfile, "from: %s\n",
4923 got_object_commit_get_author(commit));
4925 err = got_error_from_errno("fprintf");
4929 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4933 author = got_object_commit_get_author(commit);
4934 committer = got_object_commit_get_committer(commit);
4935 if (strcmp(author, committer) != 0) {
4936 n = fprintf(outfile, "via: %s\n", committer);
4938 err = got_error_from_errno("fprintf");
4942 err = add_line_metadata(lines, nlines, outoff,
4943 GOT_DIFF_LINE_AUTHOR);
4947 committer_time = got_object_commit_get_committer_time(commit);
4948 datestr = get_datestr(&committer_time, datebuf);
4950 n = fprintf(outfile, "date: %s UTC\n", datestr);
4952 err = got_error_from_errno("fprintf");
4956 err = add_line_metadata(lines, nlines, outoff,
4957 GOT_DIFF_LINE_DATE);
4961 if (got_object_commit_get_nparents(commit) > 1) {
4962 const struct got_object_id_queue *parent_ids;
4963 struct got_object_qid *qid;
4965 parent_ids = got_object_commit_get_parent_ids(commit);
4966 STAILQ_FOREACH(qid, parent_ids, entry) {
4967 err = got_object_id_str(&id_str, &qid->id);
4970 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4972 err = got_error_from_errno("fprintf");
4976 err = add_line_metadata(lines, nlines, outoff,
4977 GOT_DIFF_LINE_META);
4985 err = got_object_commit_get_logmsg(&logmsg, commit);
4989 while ((line = strsep(&s, "\n")) != NULL) {
4990 n = fprintf(outfile, "%s\n", line);
4992 err = got_error_from_errno("fprintf");
4996 err = add_line_metadata(lines, nlines, outoff,
4997 GOT_DIFF_LINE_LOGMSG);
5002 TAILQ_FOREACH(pe, dsa->paths, entry) {
5003 struct got_diff_changed_path *cp = pe->data;
5004 int pad = dsa->max_path_len - pe->path_len + 1;
5006 n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
5007 pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
5008 dsa->rm_cols + 1, cp->rm);
5010 err = got_error_from_errno("fprintf");
5014 err = add_line_metadata(lines, nlines, outoff,
5015 GOT_DIFF_LINE_CHANGES);
5020 fputc('\n', outfile);
5022 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5026 n = fprintf(outfile,
5027 "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5028 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5029 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5031 err = got_error_from_errno("fprintf");
5035 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5039 fputc('\n', outfile);
5041 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5046 got_object_commit_close(commit);
5055 static const struct got_error *
5056 create_diff(struct tog_diff_view_state *s)
5058 const struct got_error *err = NULL;
5059 FILE *f = NULL, *tmp_diff_file = NULL;
5061 struct got_diff_line *lines = NULL;
5062 struct got_pathlist_head changed_paths;
5064 TAILQ_INIT(&changed_paths);
5067 s->lines = malloc(sizeof(*s->lines));
5068 if (s->lines == NULL)
5069 return got_error_from_errno("malloc");
5074 err = got_error_from_errno("got_opentemp");
5077 tmp_diff_file = got_opentemp();
5078 if (tmp_diff_file == NULL) {
5079 err = got_error_from_errno("got_opentemp");
5082 if (s->f && fclose(s->f) == EOF) {
5083 err = got_error_from_errno("fclose");
5089 err = got_object_get_type(&obj_type, s->repo, s->id1);
5091 err = got_object_get_type(&obj_type, s->repo, s->id2);
5096 case GOT_OBJ_TYPE_BLOB:
5097 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5098 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5099 s->label1, s->label2, tog_diff_algo, s->diff_context,
5100 s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5103 case GOT_OBJ_TYPE_TREE:
5104 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5105 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5106 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5107 s->force_text_diff, NULL, s->repo, s->f);
5109 case GOT_OBJ_TYPE_COMMIT: {
5110 const struct got_object_id_queue *parent_ids;
5111 struct got_object_qid *pid;
5112 struct got_commit_object *commit2;
5113 struct got_reflist_head *refs;
5115 struct got_diffstat_cb_arg dsa = {
5118 s->ignore_whitespace,
5123 lines = malloc(sizeof(*lines));
5124 if (lines == NULL) {
5125 err = got_error_from_errno("malloc");
5129 /* build diff first in tmp file then append to commit info */
5130 err = got_diff_objects_as_commits(&lines, &nlines,
5131 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5132 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5133 s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5137 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
5140 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5141 /* Show commit info if we're diffing to a parent/root commit. */
5142 if (s->id1 == NULL) {
5143 err = write_commit_info(&s->lines, &s->nlines, s->id2,
5144 refs, s->repo, s->ignore_whitespace,
5145 s->force_text_diff, &dsa, s->f);
5149 parent_ids = got_object_commit_get_parent_ids(commit2);
5150 STAILQ_FOREACH(pid, parent_ids, entry) {
5151 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5152 err = write_commit_info(&s->lines,
5153 &s->nlines, s->id2, refs, s->repo,
5154 s->ignore_whitespace,
5155 s->force_text_diff, &dsa, s->f);
5162 got_object_commit_close(commit2);
5164 err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5169 err = got_error(GOT_ERR_OBJ_TYPE);
5174 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5175 if (s->f && fflush(s->f) != 0 && err == NULL)
5176 err = got_error_from_errno("fflush");
5177 if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5178 err = got_error_from_errno("fclose");
5183 diff_view_indicate_progress(struct tog_view *view)
5185 mvwaddstr(view->window, 0, 0, "diffing...");
5190 static const struct got_error *
5191 search_start_diff_view(struct tog_view *view)
5193 struct tog_diff_view_state *s = &view->state.diff;
5195 s->matched_line = 0;
5200 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5201 size_t *nlines, int **first, int **last, int **match, int **selected)
5203 struct tog_diff_view_state *s = &view->state.diff;
5206 *nlines = s->nlines;
5207 *line_offsets = NULL;
5208 *match = &s->matched_line;
5209 *first = &s->first_displayed_line;
5210 *last = &s->last_displayed_line;
5211 *selected = &s->selected_line;
5214 static const struct got_error *
5215 search_next_view_match(struct tog_view *view)
5217 const struct got_error *err = NULL;
5221 size_t linesize = 0;
5223 off_t *line_offsets;
5225 int *first, *last, *match, *selected;
5227 if (!view->search_setup)
5228 return got_error_msg(GOT_ERR_NOT_IMPL,
5229 "view search not supported");
5230 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5233 if (!view->searching) {
5234 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5239 if (view->searching == TOG_SEARCH_FORWARD)
5240 lineno = *first + 1;
5242 lineno = *first - 1;
5244 lineno = *first - 1 + *selected;
5249 if (lineno <= 0 || lineno > nlines) {
5251 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5255 if (view->searching == TOG_SEARCH_FORWARD)
5261 offset = view->type == TOG_VIEW_DIFF ?
5262 view->state.diff.lines[lineno - 1].offset :
5263 line_offsets[lineno - 1];
5264 if (fseeko(f, offset, SEEK_SET) != 0) {
5266 return got_error_from_errno("fseeko");
5268 linelen = getline(&line, &linesize, f);
5269 if (linelen != -1) {
5271 err = expand_tab(&exstr, line);
5274 if (match_line(exstr, &view->regex, 1,
5276 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5283 if (view->searching == TOG_SEARCH_FORWARD)
5298 static const struct got_error *
5299 close_diff_view(struct tog_view *view)
5301 const struct got_error *err = NULL;
5302 struct tog_diff_view_state *s = &view->state.diff;
5308 if (s->f && fclose(s->f) == EOF)
5309 err = got_error_from_errno("fclose");
5311 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5312 err = got_error_from_errno("fclose");
5314 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5315 err = got_error_from_errno("fclose");
5317 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5318 err = got_error_from_errno("close");
5320 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5321 err = got_error_from_errno("close");
5329 static const struct got_error *
5330 open_diff_view(struct tog_view *view, struct got_object_id *id1,
5331 struct got_object_id *id2, const char *label1, const char *label2,
5332 int diff_context, int ignore_whitespace, int force_text_diff,
5333 struct tog_view *parent_view, struct got_repository *repo)
5335 const struct got_error *err;
5336 struct tog_diff_view_state *s = &view->state.diff;
5338 memset(s, 0, sizeof(*s));
5342 if (id1 != NULL && id2 != NULL) {
5345 err = got_object_get_type(&type1, repo, id1);
5348 err = got_object_get_type(&type2, repo, id2);
5352 if (type1 != type2) {
5353 err = got_error(GOT_ERR_OBJ_TYPE);
5357 s->first_displayed_line = 1;
5358 s->last_displayed_line = view->nlines;
5359 s->selected_line = 1;
5367 s->id1 = got_object_id_dup(id1);
5368 if (s->id1 == NULL) {
5369 err = got_error_from_errno("got_object_id_dup");
5375 s->id2 = got_object_id_dup(id2);
5376 if (s->id2 == NULL) {
5377 err = got_error_from_errno("got_object_id_dup");
5381 s->f1 = got_opentemp();
5382 if (s->f1 == NULL) {
5383 err = got_error_from_errno("got_opentemp");
5387 s->f2 = got_opentemp();
5388 if (s->f2 == NULL) {
5389 err = got_error_from_errno("got_opentemp");
5393 s->fd1 = got_opentempfd();
5395 err = got_error_from_errno("got_opentempfd");
5399 s->fd2 = got_opentempfd();
5401 err = got_error_from_errno("got_opentempfd");
5405 s->diff_context = diff_context;
5406 s->ignore_whitespace = ignore_whitespace;
5407 s->force_text_diff = force_text_diff;
5408 s->parent_view = parent_view;
5411 if (has_colors() && getenv("TOG_COLORS") != NULL && !using_mock_io) {
5414 rc = init_pair(GOT_DIFF_LINE_MINUS,
5415 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5417 rc = init_pair(GOT_DIFF_LINE_PLUS,
5418 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5420 rc = init_pair(GOT_DIFF_LINE_HUNK,
5421 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5423 rc = init_pair(GOT_DIFF_LINE_META,
5424 get_color_value("TOG_COLOR_DIFF_META"), -1);
5426 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5427 get_color_value("TOG_COLOR_DIFF_META"), -1);
5429 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5430 get_color_value("TOG_COLOR_DIFF_META"), -1);
5432 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5433 get_color_value("TOG_COLOR_DIFF_META"), -1);
5435 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5436 get_color_value("TOG_COLOR_AUTHOR"), -1);
5438 rc = init_pair(GOT_DIFF_LINE_DATE,
5439 get_color_value("TOG_COLOR_DATE"), -1);
5441 err = got_error(GOT_ERR_RANGE);
5446 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5447 view_is_splitscreen(view))
5448 show_log_view(parent_view); /* draw border */
5449 diff_view_indicate_progress(view);
5451 err = create_diff(s);
5453 view->show = show_diff_view;
5454 view->input = input_diff_view;
5455 view->reset = reset_diff_view;
5456 view->close = close_diff_view;
5457 view->search_start = search_start_diff_view;
5458 view->search_setup = search_setup_diff_view;
5459 view->search_next = search_next_view_match;
5462 if (view->close == NULL)
5463 close_diff_view(view);
5469 static const struct got_error *
5470 show_diff_view(struct tog_view *view)
5472 const struct got_error *err;
5473 struct tog_diff_view_state *s = &view->state.diff;
5474 char *id_str1 = NULL, *id_str2, *header;
5475 const char *label1, *label2;
5478 err = got_object_id_str(&id_str1, s->id1);
5481 label1 = s->label1 ? s->label1 : id_str1;
5483 label1 = "/dev/null";
5485 err = got_object_id_str(&id_str2, s->id2);
5488 label2 = s->label2 ? s->label2 : id_str2;
5490 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5491 err = got_error_from_errno("asprintf");
5499 err = draw_file(view, header);
5504 static const struct got_error *
5505 set_selected_commit(struct tog_diff_view_state *s,
5506 struct commit_queue_entry *entry)
5508 const struct got_error *err;
5509 const struct got_object_id_queue *parent_ids;
5510 struct got_commit_object *selected_commit;
5511 struct got_object_qid *pid;
5514 s->id2 = got_object_id_dup(entry->id);
5516 return got_error_from_errno("got_object_id_dup");
5518 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5521 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5523 pid = STAILQ_FIRST(parent_ids);
5524 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5525 got_object_commit_close(selected_commit);
5529 static const struct got_error *
5530 reset_diff_view(struct tog_view *view)
5532 struct tog_diff_view_state *s = &view->state.diff;
5535 wclear(view->window);
5536 s->first_displayed_line = 1;
5537 s->last_displayed_line = view->nlines;
5538 s->matched_line = 0;
5539 diff_view_indicate_progress(view);
5540 return create_diff(s);
5544 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5548 i = start = s->first_displayed_line - 1;
5550 while (s->lines[i].type != type) {
5554 return; /* do nothing, requested type not in file */
5557 s->selected_line = 1;
5558 s->first_displayed_line = i;
5562 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5566 i = start = s->first_displayed_line + 1;
5568 while (s->lines[i].type != type) {
5569 if (i == s->nlines - 1)
5572 return; /* do nothing, requested type not in file */
5575 s->selected_line = 1;
5576 s->first_displayed_line = i;
5579 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5581 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5584 static const struct got_error *
5585 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5587 const struct got_error *err = NULL;
5588 struct tog_diff_view_state *s = &view->state.diff;
5589 struct tog_log_view_state *ls;
5590 struct commit_queue_entry *old_selected_entry;
5592 size_t linesize = 0;
5594 int i, nscroll = view->nlines - 1, up = 0;
5596 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5605 horizontal_scroll_input(view, ch);
5610 s->force_text_diff = !s->force_text_diff;
5611 view->action = s->force_text_diff ?
5612 "force ASCII text enabled" :
5613 "force ASCII text disabled";
5615 else if (ch == 'w') {
5616 s->ignore_whitespace = !s->ignore_whitespace;
5617 view->action = s->ignore_whitespace ?
5618 "ignore whitespace enabled" :
5619 "ignore whitespace disabled";
5621 err = reset_diff_view(view);
5625 s->first_displayed_line = 1;
5634 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5640 if (s->first_displayed_line > 1)
5641 s->first_displayed_line--;
5652 if (s->first_displayed_line == 1) {
5657 while (i++ < nscroll && s->first_displayed_line > 1)
5658 s->first_displayed_line--;
5664 s->first_displayed_line++;
5681 while (!s->eof && i++ < nscroll) {
5682 linelen = getline(&line, &linesize, s->f);
5683 s->first_displayed_line++;
5684 if (linelen == -1) {
5688 err = got_ferror(s->f, GOT_ERR_IO);
5695 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5698 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5701 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5704 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5707 if (s->diff_context > 0) {
5709 s->matched_line = 0;
5710 diff_view_indicate_progress(view);
5711 err = create_diff(s);
5712 if (s->first_displayed_line + view->nlines - 1 >
5714 s->first_displayed_line = 1;
5715 s->last_displayed_line = view->nlines;
5721 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5723 s->matched_line = 0;
5724 diff_view_indicate_progress(view);
5725 err = create_diff(s);
5737 if (s->parent_view == NULL) {
5741 s->parent_view->count = view->count;
5743 if (s->parent_view->type == TOG_VIEW_LOG) {
5744 ls = &s->parent_view->state.log;
5745 old_selected_entry = ls->selected_entry;
5747 err = input_log_view(NULL, s->parent_view,
5748 up ? KEY_UP : KEY_DOWN);
5751 view->count = s->parent_view->count;
5753 if (old_selected_entry == ls->selected_entry)
5756 err = set_selected_commit(s, ls->selected_entry);
5759 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5760 struct tog_blame_view_state *bs;
5761 struct got_object_id *id, *prev_id;
5763 bs = &s->parent_view->state.blame;
5764 prev_id = get_annotation_for_line(bs->blame.lines,
5765 bs->blame.nlines, bs->last_diffed_line);
5767 err = input_blame_view(&view, s->parent_view,
5768 up ? KEY_UP : KEY_DOWN);
5771 view->count = s->parent_view->count;
5773 if (prev_id == NULL)
5775 id = get_selected_commit_id(bs->blame.lines,
5776 bs->blame.nlines, bs->first_displayed_line,
5781 if (!got_object_id_cmp(prev_id, id))
5784 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5788 s->first_displayed_line = 1;
5789 s->last_displayed_line = view->nlines;
5790 s->matched_line = 0;
5793 diff_view_indicate_progress(view);
5794 err = create_diff(s);
5804 static const struct got_error *
5805 cmd_diff(int argc, char *argv[])
5807 const struct got_error *error;
5808 struct got_repository *repo = NULL;
5809 struct got_worktree *worktree = NULL;
5810 struct got_object_id *id1 = NULL, *id2 = NULL;
5811 char *repo_path = NULL, *cwd = NULL;
5812 char *id_str1 = NULL, *id_str2 = NULL;
5813 char *label1 = NULL, *label2 = NULL;
5814 int diff_context = 3, ignore_whitespace = 0;
5815 int ch, force_text_diff = 0;
5817 struct tog_view *view;
5818 int *pack_fds = NULL;
5820 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5823 force_text_diff = 1;
5826 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5829 errx(1, "number of context lines is %s: %s",
5833 repo_path = realpath(optarg, NULL);
5834 if (repo_path == NULL)
5835 return got_error_from_errno2("realpath",
5837 got_path_strip_trailing_slashes(repo_path);
5840 ignore_whitespace = 1;
5852 usage_diff(); /* TODO show local worktree changes */
5853 } else if (argc == 2) {
5859 error = got_repo_pack_fds_open(&pack_fds);
5863 if (repo_path == NULL) {
5864 cwd = getcwd(NULL, 0);
5866 return got_error_from_errno("getcwd");
5867 error = got_worktree_open(&worktree, cwd);
5868 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5872 strdup(got_worktree_get_repo_path(worktree));
5874 repo_path = strdup(cwd);
5875 if (repo_path == NULL) {
5876 error = got_error_from_errno("strdup");
5881 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5887 error = apply_unveil(got_repo_get_path(repo), NULL);
5891 error = tog_load_refs(repo, 0);
5895 error = got_repo_match_object_id(&id1, &label1, id_str1,
5896 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5900 error = got_repo_match_object_id(&id2, &label2, id_str2,
5901 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5905 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5907 error = got_error_from_errno("view_open");
5910 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5911 ignore_whitespace, force_text_diff, NULL, repo);
5914 error = view_loop(view);
5921 const struct got_error *close_err = got_repo_close(repo);
5926 got_worktree_close(worktree);
5928 const struct got_error *pack_err =
5929 got_repo_pack_fds_close(pack_fds);
5942 "usage: %s blame [-c commit] [-r repository-path] path\n",
5947 struct tog_blame_line {
5949 struct got_object_id *id;
5952 static const struct got_error *
5953 draw_blame(struct tog_view *view)
5955 struct tog_blame_view_state *s = &view->state.blame;
5956 struct tog_blame *blame = &s->blame;
5957 regmatch_t *regmatch = &view->regmatch;
5958 const struct got_error *err;
5959 int lineno = 0, nprinted = 0;
5961 size_t linesize = 0;
5965 struct tog_blame_line *blame_line;
5966 struct got_object_id *prev_id = NULL;
5968 struct tog_color *tc;
5970 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5975 werase(view->window);
5977 if (asprintf(&line, "commit %s", id_str) == -1) {
5978 err = got_error_from_errno("asprintf");
5983 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5988 if (view_needs_focus_indication(view))
5989 wstandout(view->window);
5990 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5992 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5993 waddwstr(view->window, wline);
5994 while (width++ < view->ncols)
5995 waddch(view->window, ' ');
5997 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5998 if (view_needs_focus_indication(view))
5999 wstandend(view->window);
6003 if (view->gline > blame->nlines)
6004 view->gline = blame->nlines;
6006 if (tog_io.wait_for_ui) {
6007 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6010 rc = pthread_cond_wait(&bta->blame_complete, &tog_mutex);
6012 return got_error_set_errno(rc, "pthread_cond_wait");
6013 tog_io.wait_for_ui = 0;
6016 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
6017 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
6018 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
6020 return got_error_from_errno("asprintf");
6023 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6028 waddwstr(view->window, wline);
6031 if (width < view->ncols - 1)
6032 waddch(view->window, '\n');
6036 while (nprinted < view->nlines - 2) {
6037 linelen = getline(&line, &linesize, blame->f);
6038 if (linelen == -1) {
6039 if (feof(blame->f)) {
6044 return got_ferror(blame->f, GOT_ERR_IO);
6046 if (++lineno < s->first_displayed_line)
6048 if (view->gline && !gotoline(view, &lineno, &nprinted))
6051 /* Set view->maxx based on full line length. */
6052 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6059 view->maxx = MAX(view->maxx, width);
6061 if (nprinted == s->selected_line - 1)
6062 wstandout(view->window);
6064 if (blame->nlines > 0) {
6065 blame_line = &blame->lines[lineno - 1];
6066 if (blame_line->annotated && prev_id &&
6067 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6068 !(nprinted == s->selected_line - 1)) {
6069 waddstr(view->window, " ");
6070 } else if (blame_line->annotated) {
6072 err = got_object_id_str(&id_str,
6078 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6080 wattr_on(view->window,
6081 COLOR_PAIR(tc->colorpair), NULL);
6082 wprintw(view->window, "%.8s", id_str);
6084 wattr_off(view->window,
6085 COLOR_PAIR(tc->colorpair), NULL);
6087 prev_id = blame_line->id;
6089 waddstr(view->window, "........");
6093 waddstr(view->window, "........");
6097 if (nprinted == s->selected_line - 1)
6098 wstandend(view->window);
6099 waddstr(view->window, " ");
6101 if (view->ncols <= 9) {
6103 } else if (s->first_displayed_line + nprinted ==
6105 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6106 err = add_matched_line(&width, line, view->ncols - 9, 9,
6107 view->window, view->x, regmatch);
6115 err = format_line(&wline, &width, &skip, line,
6116 view->x, view->ncols - 9, 9, 1);
6121 waddwstr(view->window, &wline[skip]);
6127 if (width <= view->ncols - 1)
6128 waddch(view->window, '\n');
6129 if (++nprinted == 1)
6130 s->first_displayed_line = lineno;
6133 s->last_displayed_line = lineno;
6140 static const struct got_error *
6141 blame_cb(void *arg, int nlines, int lineno,
6142 struct got_commit_object *commit, struct got_object_id *id)
6144 const struct got_error *err = NULL;
6145 struct tog_blame_cb_args *a = arg;
6146 struct tog_blame_line *line;
6149 if (nlines != a->nlines ||
6150 (lineno != -1 && lineno < 1) || lineno > a->nlines)
6151 return got_error(GOT_ERR_RANGE);
6153 errcode = pthread_mutex_lock(&tog_mutex);
6155 return got_error_set_errno(errcode, "pthread_mutex_lock");
6157 if (*a->quit) { /* user has quit the blame view */
6158 err = got_error(GOT_ERR_ITER_COMPLETED);
6163 goto done; /* no change in this commit */
6165 line = &a->lines[lineno - 1];
6166 if (line->annotated)
6169 line->id = got_object_id_dup(id);
6170 if (line->id == NULL) {
6171 err = got_error_from_errno("got_object_id_dup");
6174 line->annotated = 1;
6176 errcode = pthread_mutex_unlock(&tog_mutex);
6178 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6183 blame_thread(void *arg)
6185 const struct got_error *err, *close_err;
6186 struct tog_blame_thread_args *ta = arg;
6187 struct tog_blame_cb_args *a = ta->cb_args;
6188 int errcode, fd1 = -1, fd2 = -1;
6189 FILE *f1 = NULL, *f2 = NULL;
6191 fd1 = got_opentempfd();
6193 return (void *)got_error_from_errno("got_opentempfd");
6195 fd2 = got_opentempfd();
6197 err = got_error_from_errno("got_opentempfd");
6201 f1 = got_opentemp();
6203 err = (void *)got_error_from_errno("got_opentemp");
6206 f2 = got_opentemp();
6208 err = (void *)got_error_from_errno("got_opentemp");
6212 err = block_signals_used_by_main_thread();
6216 err = got_blame(ta->path, a->commit_id, ta->repo,
6217 tog_diff_algo, blame_cb, ta->cb_args,
6218 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6219 if (err && err->code == GOT_ERR_CANCELLED)
6222 errcode = pthread_mutex_lock(&tog_mutex);
6224 err = got_error_set_errno(errcode, "pthread_mutex_lock");
6228 close_err = got_repo_close(ta->repo);
6234 if (tog_io.wait_for_ui) {
6235 errcode = pthread_cond_signal(&ta->blame_complete);
6236 if (errcode && err == NULL)
6237 err = got_error_set_errno(errcode,
6238 "pthread_cond_signal");
6241 errcode = pthread_mutex_unlock(&tog_mutex);
6242 if (errcode && err == NULL)
6243 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6246 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6247 err = got_error_from_errno("close");
6248 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6249 err = got_error_from_errno("close");
6250 if (f1 && fclose(f1) == EOF && err == NULL)
6251 err = got_error_from_errno("fclose");
6252 if (f2 && fclose(f2) == EOF && err == NULL)
6253 err = got_error_from_errno("fclose");
6258 static struct got_object_id *
6259 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6260 int first_displayed_line, int selected_line)
6262 struct tog_blame_line *line;
6267 line = &lines[first_displayed_line - 1 + selected_line - 1];
6268 if (!line->annotated)
6274 static struct got_object_id *
6275 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6278 struct tog_blame_line *line;
6280 if (nlines <= 0 || lineno >= nlines)
6283 line = &lines[lineno - 1];
6284 if (!line->annotated)
6290 static const struct got_error *
6291 stop_blame(struct tog_blame *blame)
6293 const struct got_error *err = NULL;
6296 if (blame->thread) {
6298 errcode = pthread_mutex_unlock(&tog_mutex);
6300 return got_error_set_errno(errcode,
6301 "pthread_mutex_unlock");
6302 errcode = pthread_join(blame->thread, (void **)&err);
6304 return got_error_set_errno(errcode, "pthread_join");
6305 errcode = pthread_mutex_lock(&tog_mutex);
6307 return got_error_set_errno(errcode,
6308 "pthread_mutex_lock");
6309 if (err && err->code == GOT_ERR_ITER_COMPLETED)
6311 blame->thread = 0; //NULL;
6313 if (blame->thread_args.repo) {
6314 const struct got_error *close_err;
6315 close_err = got_repo_close(blame->thread_args.repo);
6318 blame->thread_args.repo = NULL;
6321 if (fclose(blame->f) == EOF && err == NULL)
6322 err = got_error_from_errno("fclose");
6326 for (i = 0; i < blame->nlines; i++)
6327 free(blame->lines[i].id);
6329 blame->lines = NULL;
6331 free(blame->cb_args.commit_id);
6332 blame->cb_args.commit_id = NULL;
6333 if (blame->pack_fds) {
6334 const struct got_error *pack_err =
6335 got_repo_pack_fds_close(blame->pack_fds);
6338 blame->pack_fds = NULL;
6343 static const struct got_error *
6344 cancel_blame_view(void *arg)
6346 const struct got_error *err = NULL;
6350 errcode = pthread_mutex_lock(&tog_mutex);
6352 return got_error_set_errno(errcode,
6353 "pthread_mutex_unlock");
6356 err = got_error(GOT_ERR_CANCELLED);
6358 errcode = pthread_mutex_unlock(&tog_mutex);
6360 return got_error_set_errno(errcode,
6361 "pthread_mutex_lock");
6366 static const struct got_error *
6367 run_blame(struct tog_view *view)
6369 struct tog_blame_view_state *s = &view->state.blame;
6370 struct tog_blame *blame = &s->blame;
6371 const struct got_error *err = NULL;
6372 struct got_commit_object *commit = NULL;
6373 struct got_blob_object *blob = NULL;
6374 struct got_repository *thread_repo = NULL;
6375 struct got_object_id *obj_id = NULL;
6376 int obj_type, fd = -1;
6377 int *pack_fds = NULL;
6379 err = got_object_open_as_commit(&commit, s->repo,
6380 &s->blamed_commit->id);
6384 fd = got_opentempfd();
6386 err = got_error_from_errno("got_opentempfd");
6390 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6394 err = got_object_get_type(&obj_type, s->repo, obj_id);
6398 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6399 err = got_error(GOT_ERR_OBJ_TYPE);
6403 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6406 blame->f = got_opentemp();
6407 if (blame->f == NULL) {
6408 err = got_error_from_errno("got_opentemp");
6411 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6412 &blame->line_offsets, blame->f, blob);
6415 if (blame->nlines == 0) {
6416 s->blame_complete = 1;
6420 /* Don't include \n at EOF in the blame line count. */
6421 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6424 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6425 if (blame->lines == NULL) {
6426 err = got_error_from_errno("calloc");
6430 err = got_repo_pack_fds_open(&pack_fds);
6433 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6438 blame->pack_fds = pack_fds;
6439 blame->cb_args.view = view;
6440 blame->cb_args.lines = blame->lines;
6441 blame->cb_args.nlines = blame->nlines;
6442 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6443 if (blame->cb_args.commit_id == NULL) {
6444 err = got_error_from_errno("got_object_id_dup");
6447 blame->cb_args.quit = &s->done;
6449 blame->thread_args.path = s->path;
6450 blame->thread_args.repo = thread_repo;
6451 blame->thread_args.cb_args = &blame->cb_args;
6452 blame->thread_args.complete = &s->blame_complete;
6453 blame->thread_args.cancel_cb = cancel_blame_view;
6454 blame->thread_args.cancel_arg = &s->done;
6455 s->blame_complete = 0;
6457 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6458 s->first_displayed_line = 1;
6459 s->last_displayed_line = view->nlines;
6460 s->selected_line = 1;
6462 s->matched_line = 0;
6466 got_object_commit_close(commit);
6467 if (fd != -1 && close(fd) == -1 && err == NULL)
6468 err = got_error_from_errno("close");
6470 got_object_blob_close(blob);
6477 static const struct got_error *
6478 open_blame_view(struct tog_view *view, char *path,
6479 struct got_object_id *commit_id, struct got_repository *repo)
6481 const struct got_error *err = NULL;
6482 struct tog_blame_view_state *s = &view->state.blame;
6484 STAILQ_INIT(&s->blamed_commits);
6486 s->path = strdup(path);
6487 if (s->path == NULL)
6488 return got_error_from_errno("strdup");
6490 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6496 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6497 s->first_displayed_line = 1;
6498 s->last_displayed_line = view->nlines;
6499 s->selected_line = 1;
6500 s->blame_complete = 0;
6502 s->commit_id = commit_id;
6503 memset(&s->blame, 0, sizeof(s->blame));
6505 STAILQ_INIT(&s->colors);
6506 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6507 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6508 get_color_value("TOG_COLOR_COMMIT"));
6513 view->show = show_blame_view;
6514 view->input = input_blame_view;
6515 view->reset = reset_blame_view;
6516 view->close = close_blame_view;
6517 view->search_start = search_start_blame_view;
6518 view->search_setup = search_setup_blame_view;
6519 view->search_next = search_next_view_match;
6521 if (using_mock_io) {
6522 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6525 rc = pthread_cond_init(&bta->blame_complete, NULL);
6527 return got_error_set_errno(rc, "pthread_cond_init");
6530 return run_blame(view);
6533 static const struct got_error *
6534 close_blame_view(struct tog_view *view)
6536 const struct got_error *err = NULL;
6537 struct tog_blame_view_state *s = &view->state.blame;
6539 if (s->blame.thread)
6540 err = stop_blame(&s->blame);
6542 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6543 struct got_object_qid *blamed_commit;
6544 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6545 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6546 got_object_qid_free(blamed_commit);
6549 if (using_mock_io) {
6550 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6553 rc = pthread_cond_destroy(&bta->blame_complete);
6554 if (rc && err == NULL)
6555 err = got_error_set_errno(rc, "pthread_cond_destroy");
6559 free_colors(&s->colors);
6563 static const struct got_error *
6564 search_start_blame_view(struct tog_view *view)
6566 struct tog_blame_view_state *s = &view->state.blame;
6568 s->matched_line = 0;
6573 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6574 size_t *nlines, int **first, int **last, int **match, int **selected)
6576 struct tog_blame_view_state *s = &view->state.blame;
6579 *nlines = s->blame.nlines;
6580 *line_offsets = s->blame.line_offsets;
6581 *match = &s->matched_line;
6582 *first = &s->first_displayed_line;
6583 *last = &s->last_displayed_line;
6584 *selected = &s->selected_line;
6587 static const struct got_error *
6588 show_blame_view(struct tog_view *view)
6590 const struct got_error *err = NULL;
6591 struct tog_blame_view_state *s = &view->state.blame;
6594 if (s->blame.thread == 0 && !s->blame_complete) {
6595 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6596 &s->blame.thread_args);
6598 return got_error_set_errno(errcode, "pthread_create");
6601 halfdelay(1); /* fast refresh while annotating */
6604 if (s->blame_complete && !using_mock_io)
6605 halfdelay(10); /* disable fast refresh */
6607 err = draw_blame(view);
6613 static const struct got_error *
6614 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6615 struct got_repository *repo, struct got_object_id *id)
6617 struct tog_view *log_view;
6618 const struct got_error *err = NULL;
6622 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6623 if (log_view == NULL)
6624 return got_error_from_errno("view_open");
6626 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6628 view_close(log_view);
6630 *new_view = log_view;
6635 static const struct got_error *
6636 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6638 const struct got_error *err = NULL, *thread_err = NULL;
6639 struct tog_view *diff_view;
6640 struct tog_blame_view_state *s = &view->state.blame;
6641 int eos, nscroll, begin_y = 0, begin_x = 0;
6643 eos = nscroll = view->nlines - 2;
6644 if (view_is_hsplit_top(view))
6654 horizontal_scroll_input(view, ch);
6661 s->selected_line = 1;
6662 s->first_displayed_line = 1;
6667 if (s->blame.nlines < eos) {
6668 s->selected_line = s->blame.nlines;
6669 s->first_displayed_line = 1;
6671 s->selected_line = eos;
6672 s->first_displayed_line = s->blame.nlines - (eos - 1);
6679 if (s->selected_line > 1)
6681 else if (s->selected_line == 1 &&
6682 s->first_displayed_line > 1)
6683 s->first_displayed_line--;
6694 if (s->first_displayed_line == 1) {
6695 if (view->count > 1)
6697 s->selected_line = MAX(1, s->selected_line - nscroll);
6701 if (s->first_displayed_line > nscroll)
6702 s->first_displayed_line -= nscroll;
6704 s->first_displayed_line = 1;
6709 if (s->selected_line < eos && s->first_displayed_line +
6710 s->selected_line <= s->blame.nlines)
6712 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6713 s->first_displayed_line++;
6719 struct got_object_id *id = NULL;
6722 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6723 s->first_displayed_line, s->selected_line);
6727 struct got_commit_object *commit, *pcommit;
6728 struct got_object_qid *pid;
6729 struct got_object_id *blob_id = NULL;
6731 err = got_object_open_as_commit(&commit,
6736 got_object_commit_get_parent_ids(commit));
6738 got_object_commit_close(commit);
6741 /* Check if path history ends here. */
6742 err = got_object_open_as_commit(&pcommit,
6746 err = got_object_id_by_path(&blob_id, s->repo,
6748 got_object_commit_close(pcommit);
6750 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6752 got_object_commit_close(commit);
6755 err = got_object_get_type(&obj_type, s->repo,
6758 /* Can't blame non-blob type objects. */
6759 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6760 got_object_commit_close(commit);
6763 err = got_object_qid_alloc(&s->blamed_commit,
6765 got_object_commit_close(commit);
6767 if (got_object_id_cmp(id,
6768 &s->blamed_commit->id) == 0)
6770 err = got_object_qid_alloc(&s->blamed_commit,
6776 thread_err = stop_blame(&s->blame);
6780 STAILQ_INSERT_HEAD(&s->blamed_commits,
6781 s->blamed_commit, entry);
6782 err = run_blame(view);
6788 struct got_object_qid *first;
6791 first = STAILQ_FIRST(&s->blamed_commits);
6792 if (!got_object_id_cmp(&first->id, s->commit_id))
6795 thread_err = stop_blame(&s->blame);
6799 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6800 got_object_qid_free(s->blamed_commit);
6802 STAILQ_FIRST(&s->blamed_commits);
6803 err = run_blame(view);
6810 s->id_to_log = get_selected_commit_id(s->blame.lines,
6811 s->blame.nlines, s->first_displayed_line, s->selected_line);
6813 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6817 struct got_object_id *id = NULL;
6818 struct got_object_qid *pid;
6819 struct got_commit_object *commit = NULL;
6822 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6823 s->first_displayed_line, s->selected_line);
6826 err = got_object_open_as_commit(&commit, s->repo, id);
6829 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6831 /* traversed from diff view, release diff resources */
6832 err = close_diff_view(*new_view);
6835 diff_view = *new_view;
6837 if (view_is_parent_view(view))
6838 view_get_split(view, &begin_y, &begin_x);
6840 diff_view = view_open(0, 0, begin_y, begin_x,
6842 if (diff_view == NULL) {
6843 got_object_commit_close(commit);
6844 err = got_error_from_errno("view_open");
6848 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6849 id, NULL, NULL, 3, 0, 0, view, s->repo);
6850 got_object_commit_close(commit);
6852 view_close(diff_view);
6855 s->last_diffed_line = s->first_displayed_line - 1 +
6858 break; /* still open from active diff view */
6859 if (view_is_parent_view(view) &&
6860 view->mode == TOG_VIEW_SPLIT_HRZN) {
6861 err = view_init_hsplit(view, begin_y);
6867 diff_view->focussed = 1;
6868 diff_view->mode = view->mode;
6869 diff_view->nlines = view->lines - begin_y;
6870 if (view_is_parent_view(view)) {
6871 view_transfer_size(diff_view, view);
6872 err = view_close_child(view);
6875 err = view_set_child(view, diff_view);
6878 view->focus_child = 1;
6880 *new_view = diff_view;
6893 if (s->last_displayed_line >= s->blame.nlines &&
6894 s->selected_line >= MIN(s->blame.nlines,
6895 view->nlines - 2)) {
6899 if (s->last_displayed_line >= s->blame.nlines &&
6900 s->selected_line < view->nlines - 2) {
6902 MIN(nscroll, s->last_displayed_line -
6903 s->first_displayed_line - s->selected_line + 1);
6905 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6906 s->first_displayed_line += nscroll;
6908 s->first_displayed_line =
6909 s->blame.nlines - (view->nlines - 3);
6912 if (s->selected_line > view->nlines - 2) {
6913 s->selected_line = MIN(s->blame.nlines,
6921 return thread_err ? thread_err : err;
6924 static const struct got_error *
6925 reset_blame_view(struct tog_view *view)
6927 const struct got_error *err;
6928 struct tog_blame_view_state *s = &view->state.blame;
6932 err = stop_blame(&s->blame);
6936 return run_blame(view);
6939 static const struct got_error *
6940 cmd_blame(int argc, char *argv[])
6942 const struct got_error *error;
6943 struct got_repository *repo = NULL;
6944 struct got_worktree *worktree = NULL;
6945 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6946 char *link_target = NULL;
6947 struct got_object_id *commit_id = NULL;
6948 struct got_commit_object *commit = NULL;
6949 char *commit_id_str = NULL;
6951 struct tog_view *view = NULL;
6952 int *pack_fds = NULL;
6954 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6957 commit_id_str = optarg;
6960 repo_path = realpath(optarg, NULL);
6961 if (repo_path == NULL)
6962 return got_error_from_errno2("realpath",
6977 error = got_repo_pack_fds_open(&pack_fds);
6981 if (repo_path == NULL) {
6982 cwd = getcwd(NULL, 0);
6984 return got_error_from_errno("getcwd");
6985 error = got_worktree_open(&worktree, cwd);
6986 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6990 strdup(got_worktree_get_repo_path(worktree));
6992 repo_path = strdup(cwd);
6993 if (repo_path == NULL) {
6994 error = got_error_from_errno("strdup");
6999 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7003 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
7010 error = apply_unveil(got_repo_get_path(repo), NULL);
7014 error = tog_load_refs(repo, 0);
7018 if (commit_id_str == NULL) {
7019 struct got_reference *head_ref;
7020 error = got_ref_open(&head_ref, repo, worktree ?
7021 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
7024 error = got_ref_resolve(&commit_id, repo, head_ref);
7025 got_ref_close(head_ref);
7027 error = got_repo_match_object_id(&commit_id, NULL,
7028 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7033 error = got_object_open_as_commit(&commit, repo, commit_id);
7037 error = got_object_resolve_symlinks(&link_target, in_repo_path,
7042 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
7044 error = got_error_from_errno("view_open");
7047 error = open_blame_view(view, link_target ? link_target : in_repo_path,
7049 if (error != NULL) {
7050 if (view->close == NULL)
7051 close_blame_view(view);
7056 /* Release work tree lock. */
7057 got_worktree_close(worktree);
7060 error = view_loop(view);
7068 got_object_commit_close(commit);
7070 got_worktree_close(worktree);
7072 const struct got_error *close_err = got_repo_close(repo);
7077 const struct got_error *pack_err =
7078 got_repo_pack_fds_close(pack_fds);
7086 static const struct got_error *
7087 draw_tree_entries(struct tog_view *view, const char *parent_path)
7089 struct tog_tree_view_state *s = &view->state.tree;
7090 const struct got_error *err = NULL;
7091 struct got_tree_entry *te;
7094 struct tog_color *tc;
7095 int width, n, nentries, scrollx, i = 1;
7096 int limit = view->nlines;
7099 if (view_is_hsplit_top(view))
7100 --limit; /* border */
7102 werase(view->window);
7107 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7111 if (view_needs_focus_indication(view))
7112 wstandout(view->window);
7113 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7115 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7116 waddwstr(view->window, wline);
7119 while (width++ < view->ncols)
7120 waddch(view->window, ' ');
7122 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7123 if (view_needs_focus_indication(view))
7124 wstandend(view->window);
7129 if (s->first_displayed_entry) {
7130 i += got_tree_entry_get_index(s->first_displayed_entry);
7131 if (s->tree != s->root)
7132 ++i; /* account for ".." entry */
7134 nentries = got_object_tree_get_nentries(s->tree);
7135 if (asprintf(&index, "[%d/%d] %s",
7136 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7137 return got_error_from_errno("asprintf");
7138 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7142 waddwstr(view->window, wline);
7145 if (width < view->ncols - 1)
7146 waddch(view->window, '\n');
7149 waddch(view->window, '\n');
7153 if (s->first_displayed_entry == NULL) {
7154 te = got_object_tree_get_first_entry(s->tree);
7155 if (s->selected == 0) {
7157 wstandout(view->window);
7158 s->selected_entry = NULL;
7160 waddstr(view->window, " ..\n"); /* parent directory */
7161 if (s->selected == 0 && view->focussed)
7162 wstandend(view->window);
7169 te = s->first_displayed_entry;
7173 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7174 char *line = NULL, *id_str = NULL, *link_target = NULL;
7175 const char *modestr = "";
7178 te = got_object_tree_get_entry(s->tree, i);
7179 mode = got_tree_entry_get_mode(te);
7182 err = got_object_id_str(&id_str,
7183 got_tree_entry_get_id(te));
7185 return got_error_from_errno(
7186 "got_object_id_str");
7188 if (got_object_tree_entry_is_submodule(te))
7190 else if (S_ISLNK(mode)) {
7193 err = got_tree_entry_get_symlink_target(&link_target,
7199 for (i = 0; i < strlen(link_target); i++) {
7200 if (!isprint((unsigned char)link_target[i]))
7201 link_target[i] = '?';
7205 else if (S_ISDIR(mode))
7207 else if (mode & S_IXUSR)
7209 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7210 got_tree_entry_get_name(te), modestr,
7211 link_target ? " -> ": "",
7212 link_target ? link_target : "") == -1) {
7215 return got_error_from_errno("asprintf");
7220 /* use full line width to determine view->maxx */
7221 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7226 view->maxx = MAX(view->maxx, width);
7230 err = format_line(&wline, &width, &scrollx, line, view->x,
7236 if (n == s->selected) {
7238 wstandout(view->window);
7239 s->selected_entry = te;
7241 tc = match_color(&s->colors, line);
7243 wattr_on(view->window,
7244 COLOR_PAIR(tc->colorpair), NULL);
7245 waddwstr(view->window, &wline[scrollx]);
7247 wattr_off(view->window,
7248 COLOR_PAIR(tc->colorpair), NULL);
7249 if (width < view->ncols)
7250 waddch(view->window, '\n');
7251 if (n == s->selected && view->focussed)
7252 wstandend(view->window);
7258 s->last_displayed_entry = te;
7267 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7269 struct got_tree_entry *te;
7270 int isroot = s->tree == s->root;
7273 if (s->first_displayed_entry == NULL)
7276 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7277 while (i++ < maxscroll) {
7280 s->first_displayed_entry = NULL;
7283 s->first_displayed_entry = te;
7284 te = got_tree_entry_get_prev(s->tree, te);
7288 static const struct got_error *
7289 tree_scroll_down(struct tog_view *view, int maxscroll)
7291 struct tog_tree_view_state *s = &view->state.tree;
7292 struct got_tree_entry *next, *last;
7295 if (s->first_displayed_entry)
7296 next = got_tree_entry_get_next(s->tree,
7297 s->first_displayed_entry);
7299 next = got_object_tree_get_first_entry(s->tree);
7301 last = s->last_displayed_entry;
7302 while (next && n++ < maxscroll) {
7304 s->last_displayed_entry = last;
7305 last = got_tree_entry_get_next(s->tree, last);
7307 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7308 s->first_displayed_entry = next;
7309 next = got_tree_entry_get_next(s->tree, next);
7316 static const struct got_error *
7317 tree_entry_path(char **path, struct tog_parent_trees *parents,
7318 struct got_tree_entry *te)
7320 const struct got_error *err = NULL;
7321 struct tog_parent_tree *pt;
7322 size_t len = 2; /* for leading slash and NUL */
7324 TAILQ_FOREACH(pt, parents, entry)
7325 len += strlen(got_tree_entry_get_name(pt->selected_entry))
7328 len += strlen(got_tree_entry_get_name(te));
7330 *path = calloc(1, len);
7332 return got_error_from_errno("calloc");
7335 pt = TAILQ_LAST(parents, tog_parent_trees);
7337 const char *name = got_tree_entry_get_name(pt->selected_entry);
7338 if (strlcat(*path, name, len) >= len) {
7339 err = got_error(GOT_ERR_NO_SPACE);
7342 if (strlcat(*path, "/", len) >= len) {
7343 err = got_error(GOT_ERR_NO_SPACE);
7346 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7349 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7350 err = got_error(GOT_ERR_NO_SPACE);
7362 static const struct got_error *
7363 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7364 struct got_tree_entry *te, struct tog_parent_trees *parents,
7365 struct got_object_id *commit_id, struct got_repository *repo)
7367 const struct got_error *err = NULL;
7369 struct tog_view *blame_view;
7373 err = tree_entry_path(&path, parents, te);
7377 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7378 if (blame_view == NULL) {
7379 err = got_error_from_errno("view_open");
7383 err = open_blame_view(blame_view, path, commit_id, repo);
7385 if (err->code == GOT_ERR_CANCELLED)
7387 view_close(blame_view);
7389 *new_view = blame_view;
7395 static const struct got_error *
7396 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7397 struct tog_tree_view_state *s)
7399 struct tog_view *log_view;
7400 const struct got_error *err = NULL;
7405 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7406 if (log_view == NULL)
7407 return got_error_from_errno("view_open");
7409 err = tree_entry_path(&path, &s->parents, s->selected_entry);
7413 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7416 view_close(log_view);
7418 *new_view = log_view;
7423 static const struct got_error *
7424 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7425 const char *head_ref_name, struct got_repository *repo)
7427 const struct got_error *err = NULL;
7428 char *commit_id_str = NULL;
7429 struct tog_tree_view_state *s = &view->state.tree;
7430 struct got_commit_object *commit = NULL;
7432 TAILQ_INIT(&s->parents);
7433 STAILQ_INIT(&s->colors);
7435 s->commit_id = got_object_id_dup(commit_id);
7436 if (s->commit_id == NULL) {
7437 err = got_error_from_errno("got_object_id_dup");
7441 err = got_object_open_as_commit(&commit, repo, commit_id);
7446 * The root is opened here and will be closed when the view is closed.
7447 * Any visited subtrees and their path-wise parents are opened and
7450 err = got_object_open_as_tree(&s->root, repo,
7451 got_object_commit_get_tree_id(commit));
7456 err = got_object_id_str(&commit_id_str, commit_id);
7460 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7461 err = got_error_from_errno("asprintf");
7465 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7466 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7467 if (head_ref_name) {
7468 s->head_ref_name = strdup(head_ref_name);
7469 if (s->head_ref_name == NULL) {
7470 err = got_error_from_errno("strdup");
7476 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7477 err = add_color(&s->colors, "\\$$",
7478 TOG_COLOR_TREE_SUBMODULE,
7479 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7482 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7483 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7486 err = add_color(&s->colors, "/$",
7487 TOG_COLOR_TREE_DIRECTORY,
7488 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7492 err = add_color(&s->colors, "\\*$",
7493 TOG_COLOR_TREE_EXECUTABLE,
7494 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7498 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7499 get_color_value("TOG_COLOR_COMMIT"));
7504 view->show = show_tree_view;
7505 view->input = input_tree_view;
7506 view->close = close_tree_view;
7507 view->search_start = search_start_tree_view;
7508 view->search_next = search_next_tree_view;
7510 free(commit_id_str);
7512 got_object_commit_close(commit);
7514 if (view->close == NULL)
7515 close_tree_view(view);
7521 static const struct got_error *
7522 close_tree_view(struct tog_view *view)
7524 struct tog_tree_view_state *s = &view->state.tree;
7526 free_colors(&s->colors);
7527 free(s->tree_label);
7528 s->tree_label = NULL;
7530 s->commit_id = NULL;
7531 free(s->head_ref_name);
7532 s->head_ref_name = NULL;
7533 while (!TAILQ_EMPTY(&s->parents)) {
7534 struct tog_parent_tree *parent;
7535 parent = TAILQ_FIRST(&s->parents);
7536 TAILQ_REMOVE(&s->parents, parent, entry);
7537 if (parent->tree != s->root)
7538 got_object_tree_close(parent->tree);
7542 if (s->tree != NULL && s->tree != s->root)
7543 got_object_tree_close(s->tree);
7545 got_object_tree_close(s->root);
7549 static const struct got_error *
7550 search_start_tree_view(struct tog_view *view)
7552 struct tog_tree_view_state *s = &view->state.tree;
7554 s->matched_entry = NULL;
7559 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7561 regmatch_t regmatch;
7563 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7567 static const struct got_error *
7568 search_next_tree_view(struct tog_view *view)
7570 struct tog_tree_view_state *s = &view->state.tree;
7571 struct got_tree_entry *te = NULL;
7573 if (!view->searching) {
7574 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7578 if (s->matched_entry) {
7579 if (view->searching == TOG_SEARCH_FORWARD) {
7580 if (s->selected_entry)
7581 te = got_tree_entry_get_next(s->tree,
7584 te = got_object_tree_get_first_entry(s->tree);
7586 if (s->selected_entry == NULL)
7587 te = got_object_tree_get_last_entry(s->tree);
7589 te = got_tree_entry_get_prev(s->tree,
7593 if (s->selected_entry)
7594 te = s->selected_entry;
7595 else if (view->searching == TOG_SEARCH_FORWARD)
7596 te = got_object_tree_get_first_entry(s->tree);
7598 te = got_object_tree_get_last_entry(s->tree);
7603 if (s->matched_entry == NULL) {
7604 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7607 if (view->searching == TOG_SEARCH_FORWARD)
7608 te = got_object_tree_get_first_entry(s->tree);
7610 te = got_object_tree_get_last_entry(s->tree);
7613 if (match_tree_entry(te, &view->regex)) {
7614 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7615 s->matched_entry = te;
7619 if (view->searching == TOG_SEARCH_FORWARD)
7620 te = got_tree_entry_get_next(s->tree, te);
7622 te = got_tree_entry_get_prev(s->tree, te);
7625 if (s->matched_entry) {
7626 s->first_displayed_entry = s->matched_entry;
7633 static const struct got_error *
7634 show_tree_view(struct tog_view *view)
7636 const struct got_error *err = NULL;
7637 struct tog_tree_view_state *s = &view->state.tree;
7640 err = tree_entry_path(&parent_path, &s->parents, NULL);
7644 err = draw_tree_entries(view, parent_path);
7651 static const struct got_error *
7652 tree_goto_line(struct tog_view *view, int nlines)
7654 const struct got_error *err = NULL;
7655 struct tog_tree_view_state *s = &view->state.tree;
7656 struct got_tree_entry **fte, **lte, **ste;
7657 int g, last, first = 1, i = 1;
7658 int root = s->tree == s->root;
7659 int off = root ? 1 : 2;
7666 else if (g > got_object_tree_get_nentries(s->tree))
7667 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7669 fte = &s->first_displayed_entry;
7670 lte = &s->last_displayed_entry;
7671 ste = &s->selected_entry;
7674 first = got_tree_entry_get_index(*fte);
7675 first += off; /* account for ".." */
7677 last = got_tree_entry_get_index(*lte);
7680 if (g >= first && g <= last && g - first < nlines) {
7681 s->selected = g - first;
7682 return NULL; /* gline is on the current page */
7686 i = got_tree_entry_get_index(*ste);
7691 err = tree_scroll_down(view, g - i);
7694 if (got_tree_entry_get_index(*lte) >=
7695 got_object_tree_get_nentries(s->tree) - 1 &&
7696 first + s->selected < g &&
7697 s->selected < s->ndisplayed - 1) {
7698 first = got_tree_entry_get_index(*fte);
7700 s->selected = g - first;
7703 tree_scroll_up(s, i - g);
7706 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7707 s->selected = g - 1;
7712 static const struct got_error *
7713 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7715 const struct got_error *err = NULL;
7716 struct tog_tree_view_state *s = &view->state.tree;
7717 struct got_tree_entry *te;
7718 int n, nscroll = view->nlines - 3;
7721 return tree_goto_line(view, nscroll);
7730 horizontal_scroll_input(view, ch);
7733 s->show_ids = !s->show_ids;
7738 if (!s->selected_entry)
7740 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7744 err = view_request_new(new_view, view, TOG_VIEW_REF);
7751 if (s->tree == s->root)
7752 s->first_displayed_entry =
7753 got_object_tree_get_first_entry(s->tree);
7755 s->first_displayed_entry = NULL;
7760 int eos = view->nlines - 3;
7762 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7766 te = got_object_tree_get_last_entry(s->tree);
7767 for (n = 0; n < eos; n++) {
7769 if (s->tree != s->root) {
7770 s->first_displayed_entry = NULL;
7775 s->first_displayed_entry = te;
7776 te = got_tree_entry_get_prev(s->tree, te);
7779 s->selected = n - 1;
7785 if (s->selected > 0) {
7789 tree_scroll_up(s, 1);
7790 if (s->selected_entry == NULL ||
7791 (s->tree == s->root && s->selected_entry ==
7792 got_object_tree_get_first_entry(s->tree)))
7802 if (s->tree == s->root) {
7803 if (got_object_tree_get_first_entry(s->tree) ==
7804 s->first_displayed_entry)
7805 s->selected -= MIN(s->selected, nscroll);
7807 if (s->first_displayed_entry == NULL)
7808 s->selected -= MIN(s->selected, nscroll);
7810 tree_scroll_up(s, MAX(0, nscroll));
7811 if (s->selected_entry == NULL ||
7812 (s->tree == s->root && s->selected_entry ==
7813 got_object_tree_get_first_entry(s->tree)))
7819 if (s->selected < s->ndisplayed - 1) {
7823 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7825 /* can't scroll any further */
7829 tree_scroll_down(view, 1);
7839 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7841 /* can't scroll any further; move cursor down */
7842 if (s->selected < s->ndisplayed - 1)
7843 s->selected += MIN(nscroll,
7844 s->ndisplayed - s->selected - 1);
7849 tree_scroll_down(view, nscroll);
7854 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7855 struct tog_parent_tree *parent;
7856 /* user selected '..' */
7857 if (s->tree == s->root) {
7861 parent = TAILQ_FIRST(&s->parents);
7862 TAILQ_REMOVE(&s->parents, parent,
7864 got_object_tree_close(s->tree);
7865 s->tree = parent->tree;
7866 s->first_displayed_entry =
7867 parent->first_displayed_entry;
7869 parent->selected_entry;
7870 s->selected = parent->selected;
7871 if (s->selected > view->nlines - 3) {
7872 err = offset_selection_down(view);
7877 } else if (S_ISDIR(got_tree_entry_get_mode(
7878 s->selected_entry))) {
7879 struct got_tree_object *subtree;
7881 err = got_object_open_as_tree(&subtree, s->repo,
7882 got_tree_entry_get_id(s->selected_entry));
7885 err = tree_view_visit_subtree(s, subtree);
7887 got_object_tree_close(subtree);
7890 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7891 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7894 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7895 s->selected = view->nlines - 4;
7911 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7916 static const struct got_error *
7917 cmd_tree(int argc, char *argv[])
7919 const struct got_error *error;
7920 struct got_repository *repo = NULL;
7921 struct got_worktree *worktree = NULL;
7922 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7923 struct got_object_id *commit_id = NULL;
7924 struct got_commit_object *commit = NULL;
7925 const char *commit_id_arg = NULL;
7927 struct got_reference *ref = NULL;
7928 const char *head_ref_name = NULL;
7930 struct tog_view *view;
7931 int *pack_fds = NULL;
7933 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7936 commit_id_arg = optarg;
7939 repo_path = realpath(optarg, NULL);
7940 if (repo_path == NULL)
7941 return got_error_from_errno2("realpath",
7956 error = got_repo_pack_fds_open(&pack_fds);
7960 if (repo_path == NULL) {
7961 cwd = getcwd(NULL, 0);
7963 return got_error_from_errno("getcwd");
7964 error = got_worktree_open(&worktree, cwd);
7965 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7969 strdup(got_worktree_get_repo_path(worktree));
7971 repo_path = strdup(cwd);
7972 if (repo_path == NULL) {
7973 error = got_error_from_errno("strdup");
7978 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7982 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7989 error = apply_unveil(got_repo_get_path(repo), NULL);
7993 error = tog_load_refs(repo, 0);
7997 if (commit_id_arg == NULL) {
7998 error = got_repo_match_object_id(&commit_id, &label,
7999 worktree ? got_worktree_get_head_ref_name(worktree) :
8000 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8003 head_ref_name = label;
8005 error = got_ref_open(&ref, repo, commit_id_arg, 0);
8007 head_ref_name = got_ref_get_name(ref);
8008 else if (error->code != GOT_ERR_NOT_REF)
8010 error = got_repo_match_object_id(&commit_id, NULL,
8011 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8016 error = got_object_open_as_commit(&commit, repo, commit_id);
8020 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
8022 error = got_error_from_errno("view_open");
8025 error = open_tree_view(view, commit_id, head_ref_name, repo);
8028 if (!got_path_is_root_dir(in_repo_path)) {
8029 error = tree_view_walk_path(&view->state.tree, commit,
8036 /* Release work tree lock. */
8037 got_worktree_close(worktree);
8040 error = view_loop(view);
8049 const struct got_error *close_err = got_repo_close(repo);
8054 const struct got_error *pack_err =
8055 got_repo_pack_fds_close(pack_fds);
8063 static const struct got_error *
8064 ref_view_load_refs(struct tog_ref_view_state *s)
8066 struct got_reflist_entry *sre;
8067 struct tog_reflist_entry *re;
8070 TAILQ_FOREACH(sre, &tog_refs, entry) {
8071 if (strncmp(got_ref_get_name(sre->ref),
8072 "refs/got/", 9) == 0 &&
8073 strncmp(got_ref_get_name(sre->ref),
8074 "refs/got/backup/", 16) != 0)
8077 re = malloc(sizeof(*re));
8079 return got_error_from_errno("malloc");
8081 re->ref = got_ref_dup(sre->ref);
8082 if (re->ref == NULL)
8083 return got_error_from_errno("got_ref_dup");
8084 re->idx = s->nrefs++;
8085 TAILQ_INSERT_TAIL(&s->refs, re, entry);
8088 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8093 ref_view_free_refs(struct tog_ref_view_state *s)
8095 struct tog_reflist_entry *re;
8097 while (!TAILQ_EMPTY(&s->refs)) {
8098 re = TAILQ_FIRST(&s->refs);
8099 TAILQ_REMOVE(&s->refs, re, entry);
8100 got_ref_close(re->ref);
8105 static const struct got_error *
8106 open_ref_view(struct tog_view *view, struct got_repository *repo)
8108 const struct got_error *err = NULL;
8109 struct tog_ref_view_state *s = &view->state.ref;
8111 s->selected_entry = 0;
8114 TAILQ_INIT(&s->refs);
8115 STAILQ_INIT(&s->colors);
8117 err = ref_view_load_refs(s);
8121 if (has_colors() && getenv("TOG_COLORS") != NULL) {
8122 err = add_color(&s->colors, "^refs/heads/",
8123 TOG_COLOR_REFS_HEADS,
8124 get_color_value("TOG_COLOR_REFS_HEADS"));
8128 err = add_color(&s->colors, "^refs/tags/",
8129 TOG_COLOR_REFS_TAGS,
8130 get_color_value("TOG_COLOR_REFS_TAGS"));
8134 err = add_color(&s->colors, "^refs/remotes/",
8135 TOG_COLOR_REFS_REMOTES,
8136 get_color_value("TOG_COLOR_REFS_REMOTES"));
8140 err = add_color(&s->colors, "^refs/got/backup/",
8141 TOG_COLOR_REFS_BACKUP,
8142 get_color_value("TOG_COLOR_REFS_BACKUP"));
8147 view->show = show_ref_view;
8148 view->input = input_ref_view;
8149 view->close = close_ref_view;
8150 view->search_start = search_start_ref_view;
8151 view->search_next = search_next_ref_view;
8154 if (view->close == NULL)
8155 close_ref_view(view);
8161 static const struct got_error *
8162 close_ref_view(struct tog_view *view)
8164 struct tog_ref_view_state *s = &view->state.ref;
8166 ref_view_free_refs(s);
8167 free_colors(&s->colors);
8172 static const struct got_error *
8173 resolve_reflist_entry(struct got_object_id **commit_id,
8174 struct tog_reflist_entry *re, struct got_repository *repo)
8176 const struct got_error *err = NULL;
8177 struct got_object_id *obj_id;
8178 struct got_tag_object *tag = NULL;
8183 err = got_ref_resolve(&obj_id, repo, re->ref);
8187 err = got_object_get_type(&obj_type, repo, obj_id);
8192 case GOT_OBJ_TYPE_COMMIT:
8193 *commit_id = obj_id;
8195 case GOT_OBJ_TYPE_TAG:
8196 err = got_object_open_as_tag(&tag, repo, obj_id);
8200 err = got_object_get_type(&obj_type, repo,
8201 got_object_tag_get_object_id(tag));
8204 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
8205 err = got_error(GOT_ERR_OBJ_TYPE);
8208 *commit_id = got_object_id_dup(
8209 got_object_tag_get_object_id(tag));
8210 if (*commit_id == NULL) {
8211 err = got_error_from_errno("got_object_id_dup");
8216 err = got_error(GOT_ERR_OBJ_TYPE);
8222 got_object_tag_close(tag);
8230 static const struct got_error *
8231 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
8232 struct tog_reflist_entry *re, struct got_repository *repo)
8234 struct tog_view *log_view;
8235 const struct got_error *err = NULL;
8236 struct got_object_id *commit_id = NULL;
8240 err = resolve_reflist_entry(&commit_id, re, repo);
8242 if (err->code != GOT_ERR_OBJ_TYPE)
8248 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
8249 if (log_view == NULL) {
8250 err = got_error_from_errno("view_open");
8254 err = open_log_view(log_view, commit_id, repo,
8255 got_ref_get_name(re->ref), "", 0);
8258 view_close(log_view);
8260 *new_view = log_view;
8266 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
8268 struct tog_reflist_entry *re;
8271 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8274 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
8275 while (i++ < maxscroll) {
8278 s->first_displayed_entry = re;
8279 re = TAILQ_PREV(re, tog_reflist_head, entry);
8283 static const struct got_error *
8284 ref_scroll_down(struct tog_view *view, int maxscroll)
8286 struct tog_ref_view_state *s = &view->state.ref;
8287 struct tog_reflist_entry *next, *last;
8290 if (s->first_displayed_entry)
8291 next = TAILQ_NEXT(s->first_displayed_entry, entry);
8293 next = TAILQ_FIRST(&s->refs);
8295 last = s->last_displayed_entry;
8296 while (next && n++ < maxscroll) {
8298 s->last_displayed_entry = last;
8299 last = TAILQ_NEXT(last, entry);
8301 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
8302 s->first_displayed_entry = next;
8303 next = TAILQ_NEXT(next, entry);
8310 static const struct got_error *
8311 search_start_ref_view(struct tog_view *view)
8313 struct tog_ref_view_state *s = &view->state.ref;
8315 s->matched_entry = NULL;
8320 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8322 regmatch_t regmatch;
8324 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8328 static const struct got_error *
8329 search_next_ref_view(struct tog_view *view)
8331 struct tog_ref_view_state *s = &view->state.ref;
8332 struct tog_reflist_entry *re = NULL;
8334 if (!view->searching) {
8335 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8339 if (s->matched_entry) {
8340 if (view->searching == TOG_SEARCH_FORWARD) {
8341 if (s->selected_entry)
8342 re = TAILQ_NEXT(s->selected_entry, entry);
8344 re = TAILQ_PREV(s->selected_entry,
8345 tog_reflist_head, entry);
8347 if (s->selected_entry == NULL)
8348 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8350 re = TAILQ_PREV(s->selected_entry,
8351 tog_reflist_head, entry);
8354 if (s->selected_entry)
8355 re = s->selected_entry;
8356 else if (view->searching == TOG_SEARCH_FORWARD)
8357 re = TAILQ_FIRST(&s->refs);
8359 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8364 if (s->matched_entry == NULL) {
8365 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8368 if (view->searching == TOG_SEARCH_FORWARD)
8369 re = TAILQ_FIRST(&s->refs);
8371 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8374 if (match_reflist_entry(re, &view->regex)) {
8375 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8376 s->matched_entry = re;
8380 if (view->searching == TOG_SEARCH_FORWARD)
8381 re = TAILQ_NEXT(re, entry);
8383 re = TAILQ_PREV(re, tog_reflist_head, entry);
8386 if (s->matched_entry) {
8387 s->first_displayed_entry = s->matched_entry;
8394 static const struct got_error *
8395 show_ref_view(struct tog_view *view)
8397 const struct got_error *err = NULL;
8398 struct tog_ref_view_state *s = &view->state.ref;
8399 struct tog_reflist_entry *re;
8402 struct tog_color *tc;
8403 int width, n, scrollx;
8404 int limit = view->nlines;
8406 werase(view->window);
8409 if (view_is_hsplit_top(view))
8410 --limit; /* border */
8415 re = s->first_displayed_entry;
8417 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8419 return got_error_from_errno("asprintf");
8421 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8426 if (view_needs_focus_indication(view))
8427 wstandout(view->window);
8428 waddwstr(view->window, wline);
8429 while (width++ < view->ncols)
8430 waddch(view->window, ' ');
8431 if (view_needs_focus_indication(view))
8432 wstandend(view->window);
8442 while (re && limit > 0) {
8444 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8447 struct got_commit_object *ci;
8448 struct got_tag_object *tag;
8449 struct got_object_id *id;
8453 err = got_ref_resolve(&id, s->repo, re->ref);
8456 err = got_object_open_as_tag(&tag, s->repo, id);
8458 if (err->code != GOT_ERR_OBJ_TYPE) {
8462 err = got_object_open_as_commit(&ci, s->repo,
8468 t = got_object_commit_get_committer_time(ci);
8469 got_object_commit_close(ci);
8471 t = got_object_tag_get_tagger_time(tag);
8472 got_object_tag_close(tag);
8475 if (gmtime_r(&t, &tm) == NULL)
8476 return got_error_from_errno("gmtime_r");
8477 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8478 return got_error(GOT_ERR_NO_SPACE);
8480 if (got_ref_is_symbolic(re->ref)) {
8481 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8482 ymd : "", got_ref_get_name(re->ref),
8483 got_ref_get_symref_target(re->ref)) == -1)
8484 return got_error_from_errno("asprintf");
8485 } else if (s->show_ids) {
8486 struct got_object_id *id;
8488 err = got_ref_resolve(&id, s->repo, re->ref);
8491 err = got_object_id_str(&id_str, id);
8496 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8497 got_ref_get_name(re->ref), id_str) == -1) {
8498 err = got_error_from_errno("asprintf");
8505 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8506 got_ref_get_name(re->ref)) == -1)
8507 return got_error_from_errno("asprintf");
8509 /* use full line width to determine view->maxx */
8510 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8515 view->maxx = MAX(view->maxx, width);
8519 err = format_line(&wline, &width, &scrollx, line, view->x,
8525 if (n == s->selected) {
8527 wstandout(view->window);
8528 s->selected_entry = re;
8530 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8532 wattr_on(view->window,
8533 COLOR_PAIR(tc->colorpair), NULL);
8534 waddwstr(view->window, &wline[scrollx]);
8536 wattr_off(view->window,
8537 COLOR_PAIR(tc->colorpair), NULL);
8538 if (width < view->ncols)
8539 waddch(view->window, '\n');
8540 if (n == s->selected && view->focussed)
8541 wstandend(view->window);
8547 s->last_displayed_entry = re;
8550 re = TAILQ_NEXT(re, entry);
8557 static const struct got_error *
8558 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8559 struct tog_reflist_entry *re, struct got_repository *repo)
8561 const struct got_error *err = NULL;
8562 struct got_object_id *commit_id = NULL;
8563 struct tog_view *tree_view;
8567 err = resolve_reflist_entry(&commit_id, re, repo);
8569 if (err->code != GOT_ERR_OBJ_TYPE)
8576 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8577 if (tree_view == NULL) {
8578 err = got_error_from_errno("view_open");
8582 err = open_tree_view(tree_view, commit_id,
8583 got_ref_get_name(re->ref), repo);
8587 *new_view = tree_view;
8593 static const struct got_error *
8594 ref_goto_line(struct tog_view *view, int nlines)
8596 const struct got_error *err = NULL;
8597 struct tog_ref_view_state *s = &view->state.ref;
8598 int g, idx = s->selected_entry->idx;
8605 else if (g > s->nrefs)
8608 if (g >= s->first_displayed_entry->idx + 1 &&
8609 g <= s->last_displayed_entry->idx + 1 &&
8610 g - s->first_displayed_entry->idx - 1 < nlines) {
8611 s->selected = g - s->first_displayed_entry->idx - 1;
8616 err = ref_scroll_down(view, g - idx - 1);
8619 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8620 s->first_displayed_entry->idx + s->selected < g &&
8621 s->selected < s->ndisplayed - 1)
8622 s->selected = g - s->first_displayed_entry->idx - 1;
8623 } else if (idx + 1 > g)
8624 ref_scroll_up(s, idx - g + 1);
8626 if (g < nlines && s->first_displayed_entry->idx == 0)
8627 s->selected = g - 1;
8633 static const struct got_error *
8634 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8636 const struct got_error *err = NULL;
8637 struct tog_ref_view_state *s = &view->state.ref;
8638 struct tog_reflist_entry *re;
8639 int n, nscroll = view->nlines - 1;
8642 return ref_goto_line(view, nscroll);
8651 horizontal_scroll_input(view, ch);
8654 s->show_ids = !s->show_ids;
8658 s->show_date = !s->show_date;
8662 s->sort_by_date = !s->sort_by_date;
8663 view->action = s->sort_by_date ? "sort by date" : "sort by name";
8665 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8666 got_ref_cmp_by_commit_timestamp_descending :
8667 tog_ref_cmp_by_name, s->repo);
8670 got_reflist_object_id_map_free(tog_refs_idmap);
8671 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8672 &tog_refs, s->repo);
8675 ref_view_free_refs(s);
8676 err = ref_view_load_refs(s);
8681 if (!s->selected_entry)
8683 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8687 if (!s->selected_entry)
8689 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8696 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8701 int eos = view->nlines - 1;
8703 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8707 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8708 for (n = 0; n < eos; n++) {
8711 s->first_displayed_entry = re;
8712 re = TAILQ_PREV(re, tog_reflist_head, entry);
8715 s->selected = n - 1;
8721 if (s->selected > 0) {
8725 ref_scroll_up(s, 1);
8726 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8736 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8737 s->selected -= MIN(nscroll, s->selected);
8738 ref_scroll_up(s, MAX(0, nscroll));
8739 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8745 if (s->selected < s->ndisplayed - 1) {
8749 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8750 /* can't scroll any further */
8754 ref_scroll_down(view, 1);
8764 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8765 /* can't scroll any further; move cursor down */
8766 if (s->selected < s->ndisplayed - 1)
8767 s->selected += MIN(nscroll,
8768 s->ndisplayed - s->selected - 1);
8769 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8770 s->selected += s->ndisplayed - s->selected - 1;
8774 ref_scroll_down(view, nscroll);
8779 err = tog_load_refs(s->repo, s->sort_by_date);
8782 ref_view_free_refs(s);
8783 err = ref_view_load_refs(s);
8786 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8787 s->selected = view->nlines - 2;
8801 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8806 static const struct got_error *
8807 cmd_ref(int argc, char *argv[])
8809 const struct got_error *error;
8810 struct got_repository *repo = NULL;
8811 struct got_worktree *worktree = NULL;
8812 char *cwd = NULL, *repo_path = NULL;
8814 struct tog_view *view;
8815 int *pack_fds = NULL;
8817 while ((ch = getopt(argc, argv, "r:")) != -1) {
8820 repo_path = realpath(optarg, NULL);
8821 if (repo_path == NULL)
8822 return got_error_from_errno2("realpath",
8837 error = got_repo_pack_fds_open(&pack_fds);
8841 if (repo_path == NULL) {
8842 cwd = getcwd(NULL, 0);
8844 return got_error_from_errno("getcwd");
8845 error = got_worktree_open(&worktree, cwd);
8846 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8850 strdup(got_worktree_get_repo_path(worktree));
8852 repo_path = strdup(cwd);
8853 if (repo_path == NULL) {
8854 error = got_error_from_errno("strdup");
8859 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8865 error = apply_unveil(got_repo_get_path(repo), NULL);
8869 error = tog_load_refs(repo, 0);
8873 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8875 error = got_error_from_errno("view_open");
8879 error = open_ref_view(view, repo);
8884 /* Release work tree lock. */
8885 got_worktree_close(worktree);
8888 error = view_loop(view);
8893 const struct got_error *close_err = got_repo_close(repo);
8898 const struct got_error *pack_err =
8899 got_repo_pack_fds_close(pack_fds);
8907 static const struct got_error*
8908 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8917 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8921 if (mvwprintw(win, y, x, "%s", str) == ERR)
8922 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8929 static const struct got_error *
8930 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8934 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8936 free(*line_offsets);
8937 *line_offsets = NULL;
8938 return got_error_from_errno("reallocarray");
8942 (*line_offsets)[*nlines] = off;
8947 static const struct got_error *
8948 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8952 for (;n > 0; --n, ++km) {
8956 if (km->keys == NULL)
8959 t = t0 = strdup(km->keys);
8961 return got_error_from_errno("strdup");
8964 while ((k = strsep(&t, " ")) != NULL)
8965 len += strlen(k) > 1 ? 2 : 0;
8967 *ret = MAX(*ret, len);
8974 * Write keymap section headers, keys, and key info in km to f.
8975 * Save line offset to *off. If terminal has UTF8 encoding enabled,
8976 * wrap control and symbolic keys in guillemets, else use <>.
8978 static const struct got_error *
8979 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8984 static const char *u8_glyph[] = {
8985 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8986 "\xe2\x80\xba" /* U+203A (utf8 >) */
8989 int cs, s, first = 1;
8991 cs = got_locale_is_utf8();
8993 t = t0 = strdup(km->keys);
8995 return got_error_from_errno("strdup");
8997 len = strlen(km->keys);
8998 while ((k = strsep(&t, " ")) != NULL) {
8999 s = strlen(k) > 1; /* control or symbolic key */
9000 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
9001 cs && s ? u8_glyph[0] : s ? "<" : "", k,
9002 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
9005 return got_error_from_errno("fprintf");
9013 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
9015 return got_error_from_errno("fprintf");
9021 static const struct got_error *
9022 format_help(struct tog_help_view_state *s)
9024 const struct got_error *err = NULL;
9026 int i, max, n, show = s->all;
9027 static const struct tog_key_map km[] = {
9028 #define KEYMAP_(info, type) { NULL, (info), type }
9029 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
9035 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
9040 err = max_key_str(&max, km, n);
9044 for (i = 0; i < n; ++i) {
9045 if (km[i].keys == NULL) {
9047 if (km[i].type == TOG_KEYMAP_GLOBAL ||
9048 km[i].type == s->type || s->all)
9052 err = format_help_line(&off, s->f, &km[i], max);
9055 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9062 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9066 static const struct got_error *
9067 create_help(struct tog_help_view_state *s)
9070 const struct got_error *err;
9072 free(s->line_offsets);
9073 s->line_offsets = NULL;
9078 return got_error_from_errno("got_opentemp");
9081 err = format_help(s);
9085 if (s->f && fflush(s->f) != 0)
9086 return got_error_from_errno("fflush");
9091 static const struct got_error *
9092 search_start_help_view(struct tog_view *view)
9094 view->state.help.matched_line = 0;
9099 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
9100 size_t *nlines, int **first, int **last, int **match, int **selected)
9102 struct tog_help_view_state *s = &view->state.help;
9105 *nlines = s->nlines;
9106 *line_offsets = s->line_offsets;
9107 *match = &s->matched_line;
9108 *first = &s->first_displayed_line;
9109 *last = &s->last_displayed_line;
9110 *selected = &s->selected_line;
9113 static const struct got_error *
9114 show_help_view(struct tog_view *view)
9116 struct tog_help_view_state *s = &view->state.help;
9117 const struct got_error *err;
9118 regmatch_t *regmatch = &view->regmatch;
9123 int width, nprinted = 0, rc = 0;
9124 int eos = view->nlines;
9126 if (view_is_hsplit_top(view))
9127 --eos; /* account for border */
9131 werase(view->window);
9133 if (view->gline > s->nlines - 1)
9134 view->gline = s->nlines - 1;
9136 err = win_draw_center(view->window, 0, 0, view->ncols,
9137 view_needs_focus_indication(view),
9138 "tog help (press q to return to tog)");
9143 waddstr(view->window, "\n\n");
9149 while (eos > 0 && nprinted < eos) {
9152 linelen = getline(&line, &linesz, s->f);
9153 if (linelen == -1) {
9156 return got_ferror(s->f, GOT_ERR_IO);
9161 if (++s->lineno < s->first_displayed_line)
9163 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
9165 if (s->lineno == view->hiline)
9168 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
9174 view->maxx = MAX(view->maxx, width);
9179 wattron(view->window, attr);
9180 if (s->first_displayed_line + nprinted == s->matched_line &&
9181 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
9182 err = add_matched_line(&width, line, view->ncols - 1, 0,
9183 view->window, view->x, regmatch);
9191 err = format_line(&wline, &width, &skip, line,
9192 view->x, view->ncols, 0, view->x ? 1 : 0);
9197 waddwstr(view->window, &wline[skip]);
9201 if (s->lineno == view->hiline) {
9202 while (width++ < view->ncols)
9203 waddch(view->window, ' ');
9205 if (width < view->ncols)
9206 waddch(view->window, '\n');
9209 wattroff(view->window, attr);
9210 if (++nprinted == 1)
9211 s->first_displayed_line = s->lineno;
9215 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
9217 s->last_displayed_line = s->first_displayed_line;
9222 rc = waddnstr(view->window,
9223 "See the tog(1) manual page for full documentation",
9226 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9228 wmove(view->window, view->nlines - 1, 0);
9229 wclrtoeol(view->window);
9230 wstandout(view->window);
9231 rc = waddnstr(view->window, "scroll down for more...",
9234 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9235 if (getcurx(view->window) < view->ncols - 6) {
9236 rc = wprintw(view->window, "[%.0f%%]",
9237 100.00 * s->last_displayed_line / s->nlines);
9239 return got_error_msg(GOT_ERR_IO, "wprintw");
9241 wstandend(view->window);
9247 static const struct got_error *
9248 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
9250 struct tog_help_view_state *s = &view->state.help;
9251 const struct got_error *err = NULL;
9257 eos = nscroll = view->nlines;
9258 if (view_is_hsplit_top(view))
9261 s->lineno = s->first_displayed_line - 1 + s->selected_line;
9270 horizontal_scroll_input(view, ch);
9274 s->first_displayed_line = 1;
9282 s->first_displayed_line = (s->nlines - eos) + 3;
9287 if (s->first_displayed_line > 1)
9288 --s->first_displayed_line;
9299 if (s->first_displayed_line == 1) {
9303 while (--nscroll > 0 && s->first_displayed_line > 1)
9304 s->first_displayed_line--;
9310 ++s->first_displayed_line;
9326 while (!s->eof && --nscroll > 0) {
9327 linelen = getline(&line, &linesz, s->f);
9328 s->first_displayed_line++;
9329 if (linelen == -1) {
9333 err = got_ferror(s->f, GOT_ERR_IO);
9347 static const struct got_error *
9348 close_help_view(struct tog_view *view)
9350 struct tog_help_view_state *s = &view->state.help;
9352 free(s->line_offsets);
9353 s->line_offsets = NULL;
9354 if (fclose(s->f) == EOF)
9355 return got_error_from_errno("fclose");
9360 static const struct got_error *
9361 reset_help_view(struct tog_view *view)
9363 struct tog_help_view_state *s = &view->state.help;
9366 if (s->f && fclose(s->f) == EOF)
9367 return got_error_from_errno("fclose");
9369 wclear(view->window);
9373 s->first_displayed_line = 1;
9374 s->last_displayed_line = view->nlines;
9375 s->matched_line = 0;
9377 return create_help(s);
9380 static const struct got_error *
9381 open_help_view(struct tog_view *view, struct tog_view *parent)
9383 const struct got_error *err = NULL;
9384 struct tog_help_view_state *s = &view->state.help;
9386 s->type = (enum tog_keymap_type)parent->type;
9387 s->first_displayed_line = 1;
9388 s->last_displayed_line = view->nlines;
9389 s->selected_line = 1;
9391 view->show = show_help_view;
9392 view->input = input_help_view;
9393 view->reset = reset_help_view;
9394 view->close = close_help_view;
9395 view->search_start = search_start_help_view;
9396 view->search_setup = search_setup_help_view;
9397 view->search_next = search_next_view_match;
9399 err = create_help(s);
9403 static const struct got_error *
9404 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9405 enum tog_view_type request, int y, int x)
9407 const struct got_error *err = NULL;
9413 if (view->type == TOG_VIEW_LOG) {
9414 struct tog_log_view_state *s = &view->state.log;
9416 err = open_diff_view_for_commit(new_view, y, x,
9417 s->selected_entry->commit, s->selected_entry->id,
9420 return got_error_msg(GOT_ERR_NOT_IMPL,
9421 "parent/child view pair not supported");
9423 case TOG_VIEW_BLAME:
9424 if (view->type == TOG_VIEW_TREE) {
9425 struct tog_tree_view_state *s = &view->state.tree;
9427 err = blame_tree_entry(new_view, y, x,
9428 s->selected_entry, &s->parents, s->commit_id,
9431 return got_error_msg(GOT_ERR_NOT_IMPL,
9432 "parent/child view pair not supported");
9435 if (view->type == TOG_VIEW_BLAME)
9436 err = log_annotated_line(new_view, y, x,
9437 view->state.blame.repo, view->state.blame.id_to_log);
9438 else if (view->type == TOG_VIEW_TREE)
9439 err = log_selected_tree_entry(new_view, y, x,
9441 else if (view->type == TOG_VIEW_REF)
9442 err = log_ref_entry(new_view, y, x,
9443 view->state.ref.selected_entry,
9444 view->state.ref.repo);
9446 return got_error_msg(GOT_ERR_NOT_IMPL,
9447 "parent/child view pair not supported");
9450 if (view->type == TOG_VIEW_LOG)
9451 err = browse_commit_tree(new_view, y, x,
9452 view->state.log.selected_entry,
9453 view->state.log.in_repo_path,
9454 view->state.log.head_ref_name,
9455 view->state.log.repo);
9456 else if (view->type == TOG_VIEW_REF)
9457 err = browse_ref_tree(new_view, y, x,
9458 view->state.ref.selected_entry,
9459 view->state.ref.repo);
9461 return got_error_msg(GOT_ERR_NOT_IMPL,
9462 "parent/child view pair not supported");
9465 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9466 if (*new_view == NULL)
9467 return got_error_from_errno("view_open");
9468 if (view->type == TOG_VIEW_LOG)
9469 err = open_ref_view(*new_view, view->state.log.repo);
9470 else if (view->type == TOG_VIEW_TREE)
9471 err = open_ref_view(*new_view, view->state.tree.repo);
9473 err = got_error_msg(GOT_ERR_NOT_IMPL,
9474 "parent/child view pair not supported");
9476 view_close(*new_view);
9479 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9480 if (*new_view == NULL)
9481 return got_error_from_errno("view_open");
9482 err = open_help_view(*new_view, view);
9484 view_close(*new_view);
9487 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9494 * If view was scrolled down to move the selected line into view when opening a
9495 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9498 offset_selection_up(struct tog_view *view)
9500 switch (view->type) {
9501 case TOG_VIEW_BLAME: {
9502 struct tog_blame_view_state *s = &view->state.blame;
9503 if (s->first_displayed_line == 1) {
9504 s->selected_line = MAX(s->selected_line - view->offset,
9508 if (s->first_displayed_line > view->offset)
9509 s->first_displayed_line -= view->offset;
9511 s->first_displayed_line = 1;
9512 s->selected_line += view->offset;
9516 log_scroll_up(&view->state.log, view->offset);
9517 view->state.log.selected += view->offset;
9520 ref_scroll_up(&view->state.ref, view->offset);
9521 view->state.ref.selected += view->offset;
9524 tree_scroll_up(&view->state.tree, view->offset);
9525 view->state.tree.selected += view->offset;
9535 * If the selected line is in the section of screen covered by the bottom split,
9536 * scroll down offset lines to move it into view and index its new position.
9538 static const struct got_error *
9539 offset_selection_down(struct tog_view *view)
9541 const struct got_error *err = NULL;
9542 const struct got_error *(*scrolld)(struct tog_view *, int);
9543 int *selected = NULL;
9546 switch (view->type) {
9547 case TOG_VIEW_BLAME: {
9548 struct tog_blame_view_state *s = &view->state.blame;
9551 if (s->selected_line > view->nlines - header) {
9552 offset = abs(view->nlines - s->selected_line - header);
9553 s->first_displayed_line += offset;
9554 s->selected_line -= offset;
9555 view->offset = offset;
9559 case TOG_VIEW_LOG: {
9560 struct tog_log_view_state *s = &view->state.log;
9561 scrolld = &log_scroll_down;
9562 header = view_is_parent_view(view) ? 3 : 2;
9563 selected = &s->selected;
9566 case TOG_VIEW_REF: {
9567 struct tog_ref_view_state *s = &view->state.ref;
9568 scrolld = &ref_scroll_down;
9570 selected = &s->selected;
9573 case TOG_VIEW_TREE: {
9574 struct tog_tree_view_state *s = &view->state.tree;
9575 scrolld = &tree_scroll_down;
9577 selected = &s->selected;
9587 if (selected && *selected > view->nlines - header) {
9588 offset = abs(view->nlines - *selected - header);
9589 view->offset = offset;
9590 if (scrolld && offset) {
9591 err = scrolld(view, offset);
9592 *selected -= offset;
9600 list_commands(FILE *fp)
9604 fprintf(fp, "commands:");
9605 for (i = 0; i < nitems(tog_commands); i++) {
9606 const struct tog_cmd *cmd = &tog_commands[i];
9607 fprintf(fp, " %s", cmd->name);
9613 usage(int hflag, int status)
9615 FILE *fp = (status == 0) ? stdout : stderr;
9617 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9620 fprintf(fp, "lazy usage: %s path\n", getprogname());
9627 make_argv(int argc, ...)
9635 argv = calloc(argc, sizeof(char *));
9638 for (i = 0; i < argc; i++) {
9639 argv[i] = strdup(va_arg(ap, char *));
9640 if (argv[i] == NULL)
9649 * Try to convert 'tog path' into a 'tog log path' command.
9650 * The user could simply have mistyped the command rather than knowingly
9651 * provided a path. So check whether argv[0] can in fact be resolved
9652 * to a path in the HEAD commit and print a special error if not.
9653 * This hack is for mpi@ <3
9655 static const struct got_error *
9656 tog_log_with_path(int argc, char *argv[])
9658 const struct got_error *error = NULL, *close_err;
9659 const struct tog_cmd *cmd = NULL;
9660 struct got_repository *repo = NULL;
9661 struct got_worktree *worktree = NULL;
9662 struct got_object_id *commit_id = NULL, *id = NULL;
9663 struct got_commit_object *commit = NULL;
9664 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9665 char *commit_id_str = NULL, **cmd_argv = NULL;
9666 int *pack_fds = NULL;
9668 cwd = getcwd(NULL, 0);
9670 return got_error_from_errno("getcwd");
9672 error = got_repo_pack_fds_open(&pack_fds);
9676 error = got_worktree_open(&worktree, cwd);
9677 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9681 repo_path = strdup(got_worktree_get_repo_path(worktree));
9683 repo_path = strdup(cwd);
9684 if (repo_path == NULL) {
9685 error = got_error_from_errno("strdup");
9689 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9693 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9698 error = tog_load_refs(repo, 0);
9701 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9702 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9703 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9708 got_worktree_close(worktree);
9712 error = got_object_open_as_commit(&commit, repo, commit_id);
9716 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9718 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9720 fprintf(stderr, "%s: '%s' is no known command or path\n",
9721 getprogname(), argv[0]);
9726 error = got_object_id_str(&commit_id_str, commit_id);
9730 cmd = &tog_commands[0]; /* log */
9732 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9733 error = cmd->cmd_main(argc, cmd_argv);
9736 close_err = got_repo_close(repo);
9741 got_object_commit_close(commit);
9743 got_worktree_close(worktree);
9745 const struct got_error *pack_err =
9746 got_repo_pack_fds_close(pack_fds);
9751 free(commit_id_str);
9758 for (i = 0; i < argc; i++)
9767 main(int argc, char *argv[])
9769 const struct got_error *io_err, *error = NULL;
9770 const struct tog_cmd *cmd = NULL;
9771 int ch, hflag = 0, Vflag = 0;
9772 char **cmd_argv = NULL;
9773 static const struct option longopts[] = {
9774 { "version", no_argument, NULL, 'V' },
9777 char *diff_algo_str = NULL;
9778 const char *test_script_path;
9780 setlocale(LC_CTYPE, "");
9783 * Test mode init must happen before pledge() because "tty" will
9784 * not allow TTY-related ioctls to occur via regular files.
9786 test_script_path = getenv("TOG_TEST_SCRIPT");
9787 if (test_script_path != NULL) {
9788 error = init_mock_term(test_script_path);
9790 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9795 #if !defined(PROFILE)
9796 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9801 if (!isatty(STDIN_FILENO))
9802 errx(1, "standard input is not a tty");
9804 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9824 got_version_print_str();
9831 /* Build an argument vector which runs a default command. */
9832 cmd = &tog_commands[0];
9834 cmd_argv = make_argv(argc, cmd->name);
9838 /* Did the user specify a command? */
9839 for (i = 0; i < nitems(tog_commands); i++) {
9840 if (strncmp(tog_commands[i].name, argv[0],
9841 strlen(argv[0])) == 0) {
9842 cmd = &tog_commands[i];
9848 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9849 if (diff_algo_str) {
9850 if (strcasecmp(diff_algo_str, "patience") == 0)
9851 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9852 if (strcasecmp(diff_algo_str, "myers") == 0)
9853 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9859 /* No command specified; try log with a path */
9860 error = tog_log_with_path(argc, argv);
9865 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9868 if (using_mock_io) {
9869 io_err = tog_io_close();
9876 for (i = 0; i < argc; i++)
9881 if (error && error->code != GOT_ERR_CANCELLED &&
9882 error->code != GOT_ERR_EOF &&
9883 error->code != GOT_ERR_PRIVSEP_EXIT &&
9884 error->code != GOT_ERR_PRIVSEP_PIPE &&
9885 !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9886 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);