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;
444 struct tog_blame_line *lines;
448 struct tog_blame_thread_args thread_args;
449 struct tog_blame_cb_args cb_args;
454 struct tog_blame_view_state {
455 int first_displayed_line;
456 int last_displayed_line;
458 int last_diffed_line;
462 struct got_object_id_queue blamed_commits;
463 struct got_object_qid *blamed_commit;
465 struct got_repository *repo;
466 struct got_object_id *commit_id;
467 struct got_object_id *id_to_log;
468 struct tog_blame blame;
470 struct tog_colors colors;
473 struct tog_parent_tree {
474 TAILQ_ENTRY(tog_parent_tree) entry;
475 struct got_tree_object *tree;
476 struct got_tree_entry *first_displayed_entry;
477 struct got_tree_entry *selected_entry;
481 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
483 struct tog_tree_view_state {
485 struct got_object_id *commit_id;/* commit which this tree belongs to */
486 struct got_tree_object *root; /* the commit's root tree entry */
487 struct got_tree_object *tree; /* currently displayed (sub-)tree */
488 struct got_tree_entry *first_displayed_entry;
489 struct got_tree_entry *last_displayed_entry;
490 struct got_tree_entry *selected_entry;
491 int ndisplayed, selected, show_ids;
492 struct tog_parent_trees parents; /* parent trees of current sub-tree */
494 struct got_repository *repo;
495 struct got_tree_entry *matched_entry;
496 struct tog_colors colors;
499 struct tog_reflist_entry {
500 TAILQ_ENTRY(tog_reflist_entry) entry;
501 struct got_reference *ref;
505 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
507 struct tog_ref_view_state {
508 struct tog_reflist_head refs;
509 struct tog_reflist_entry *first_displayed_entry;
510 struct tog_reflist_entry *last_displayed_entry;
511 struct tog_reflist_entry *selected_entry;
512 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
513 struct got_repository *repo;
514 struct tog_reflist_entry *matched_entry;
515 struct tog_colors colors;
518 struct tog_help_view_state {
523 int first_displayed_line;
524 int last_displayed_line;
529 enum tog_keymap_type type;
532 #define GENERATE_HELP \
533 KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
534 KEY_("H F1", "Open view-specific help (double tap for all help)"), \
535 KEY_("k C-p Up", "Move cursor or page up one line"), \
536 KEY_("j C-n Down", "Move cursor or page down one line"), \
537 KEY_("C-b b PgUp", "Scroll the view up one page"), \
538 KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
539 KEY_("C-u u", "Scroll the view up one half page"), \
540 KEY_("C-d d", "Scroll the view down one half page"), \
541 KEY_("g", "Go to line N (default: first line)"), \
542 KEY_("Home =", "Go to the first line"), \
543 KEY_("G", "Go to line N (default: last line)"), \
544 KEY_("End *", "Go to the last line"), \
545 KEY_("l Right", "Scroll the view right"), \
546 KEY_("h Left", "Scroll the view left"), \
547 KEY_("$", "Scroll view to the rightmost position"), \
548 KEY_("0", "Scroll view to the leftmost position"), \
549 KEY_("-", "Decrease size of the focussed split"), \
550 KEY_("+", "Increase size of the focussed split"), \
551 KEY_("Tab", "Switch focus between views"), \
552 KEY_("F", "Toggle fullscreen mode"), \
553 KEY_("/", "Open prompt to enter search term"), \
554 KEY_("n", "Find next line/token matching the current search term"), \
555 KEY_("N", "Find previous line/token matching the current search term"),\
556 KEY_("q", "Quit the focussed view; Quit help screen"), \
557 KEY_("Q", "Quit tog"), \
559 KEYMAP_("Log view", TOG_KEYMAP_LOG), \
560 KEY_("< ,", "Move cursor up one commit"), \
561 KEY_("> .", "Move cursor down one commit"), \
562 KEY_("Enter", "Open diff view of the selected commit"), \
563 KEY_("B", "Reload the log view and toggle display of merged commits"), \
564 KEY_("R", "Open ref view of all repository references"), \
565 KEY_("T", "Display tree view of the repository from the selected" \
567 KEY_("@", "Toggle between displaying author and committer name"), \
568 KEY_("&", "Open prompt to enter term to limit commits displayed"), \
569 KEY_("C-g Backspace", "Cancel current search or log operation"), \
570 KEY_("C-l", "Reload the log view with new commits in the repository"), \
572 KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
573 KEY_("K < ,", "Display diff of next line in the file/log entry"), \
574 KEY_("J > .", "Display diff of previous line in the file/log entry"), \
575 KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
576 KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
578 KEY_("(", "Go to the previous file in the diff"), \
579 KEY_(")", "Go to the next file in the diff"), \
580 KEY_("{", "Go to the previous hunk in the diff"), \
581 KEY_("}", "Go to the next hunk in the diff"), \
582 KEY_("[", "Decrease the number of context lines"), \
583 KEY_("]", "Increase the number of context lines"), \
584 KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
586 KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
587 KEY_("Enter", "Display diff view of the selected line's commit"), \
588 KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
589 KEY_("L", "Open log view for the currently selected annotated line"), \
590 KEY_("C", "Reload view with the previously blamed commit"), \
591 KEY_("c", "Reload view with the version of the file found in the" \
592 " selected line's commit"), \
593 KEY_("p", "Reload view with the version of the file found in the" \
594 " selected line's parent commit"), \
596 KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
597 KEY_("Enter", "Enter selected directory or open blame view of the" \
599 KEY_("L", "Open log view for the selected entry"), \
600 KEY_("R", "Open ref view of all repository references"), \
601 KEY_("i", "Show object IDs for all tree entries"), \
602 KEY_("Backspace", "Return to the parent directory"), \
604 KEYMAP_("Ref view", TOG_KEYMAP_REF), \
605 KEY_("Enter", "Display log view of the selected reference"), \
606 KEY_("T", "Display tree view of the selected reference"), \
607 KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
608 KEY_("m", "Toggle display of last modified date for each reference"), \
609 KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
610 KEY_("C-l", "Reload view with all repository references")
615 enum tog_keymap_type type;
618 /* curses io for tog regress */
624 static int using_mock_io;
626 #define TOG_SCREEN_DUMP "SCREENDUMP"
627 #define TOG_SCREEN_DUMP_LEN (sizeof(TOG_SCREEN_DUMP) - 1)
628 #define TOG_KEY_SCRDUMP SHRT_MIN
631 * We implement two types of views: parent views and child views.
633 * The 'Tab' key switches focus between a parent view and its child view.
634 * Child views are shown side-by-side to their parent view, provided
635 * there is enough screen estate.
637 * When a new view is opened from within a parent view, this new view
638 * becomes a child view of the parent view, replacing any existing child.
640 * When a new view is opened from within a child view, this new view
641 * becomes a parent view which will obscure the views below until the
642 * user quits the new parent view by typing 'q'.
644 * This list of views contains parent views only.
645 * Child views are only pointed to by their parent view.
647 TAILQ_HEAD(tog_view_list_head, tog_view);
650 TAILQ_ENTRY(tog_view) entry;
653 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
654 int resized_y, resized_x; /* begin_y/x based on user resizing */
655 int maxx, x; /* max column and current start column */
656 int lines, cols; /* copies of LINES and COLS */
657 int nscrolled, offset; /* lines scrolled and hsplit line offset */
658 int gline, hiline; /* navigate to and highlight this nG line */
659 int ch, count; /* current keymap and count prefix */
660 int resized; /* set when in a resize event */
661 int focussed; /* Only set on one parent or child view at a time. */
663 struct tog_view *parent;
664 struct tog_view *child;
667 * This flag is initially set on parent views when a new child view
668 * is created. It gets toggled when the 'Tab' key switches focus
669 * between parent and child.
670 * The flag indicates whether focus should be passed on to our child
671 * view if this parent view gets picked for focus after another parent
672 * view was closed. This prevents child views from losing focus in such
677 enum tog_view_mode mode;
678 /* type-specific state */
679 enum tog_view_type type;
681 struct tog_diff_view_state diff;
682 struct tog_log_view_state log;
683 struct tog_blame_view_state blame;
684 struct tog_tree_view_state tree;
685 struct tog_ref_view_state ref;
686 struct tog_help_view_state help;
689 const struct got_error *(*show)(struct tog_view *);
690 const struct got_error *(*input)(struct tog_view **,
691 struct tog_view *, int);
692 const struct got_error *(*reset)(struct tog_view *);
693 const struct got_error *(*resize)(struct tog_view *, int);
694 const struct got_error *(*close)(struct tog_view *);
696 const struct got_error *(*search_start)(struct tog_view *);
697 const struct got_error *(*search_next)(struct tog_view *);
698 void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
699 int **, int **, int **, int **);
702 #define TOG_SEARCH_FORWARD 1
703 #define TOG_SEARCH_BACKWARD 2
704 int search_next_done;
705 #define TOG_SEARCH_HAVE_MORE 1
706 #define TOG_SEARCH_NO_MORE 2
707 #define TOG_SEARCH_HAVE_NONE 3
713 static const struct got_error *open_diff_view(struct tog_view *,
714 struct got_object_id *, struct got_object_id *,
715 const char *, const char *, int, int, int, struct tog_view *,
716 struct got_repository *);
717 static const struct got_error *show_diff_view(struct tog_view *);
718 static const struct got_error *input_diff_view(struct tog_view **,
719 struct tog_view *, int);
720 static const struct got_error *reset_diff_view(struct tog_view *);
721 static const struct got_error* close_diff_view(struct tog_view *);
722 static const struct got_error *search_start_diff_view(struct tog_view *);
723 static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
724 size_t *, int **, int **, int **, int **);
725 static const struct got_error *search_next_view_match(struct tog_view *);
727 static const struct got_error *open_log_view(struct tog_view *,
728 struct got_object_id *, struct got_repository *,
729 const char *, const char *, int);
730 static const struct got_error * show_log_view(struct tog_view *);
731 static const struct got_error *input_log_view(struct tog_view **,
732 struct tog_view *, int);
733 static const struct got_error *resize_log_view(struct tog_view *, int);
734 static const struct got_error *close_log_view(struct tog_view *);
735 static const struct got_error *search_start_log_view(struct tog_view *);
736 static const struct got_error *search_next_log_view(struct tog_view *);
738 static const struct got_error *open_blame_view(struct tog_view *, char *,
739 struct got_object_id *, struct got_repository *);
740 static const struct got_error *show_blame_view(struct tog_view *);
741 static const struct got_error *input_blame_view(struct tog_view **,
742 struct tog_view *, int);
743 static const struct got_error *reset_blame_view(struct tog_view *);
744 static const struct got_error *close_blame_view(struct tog_view *);
745 static const struct got_error *search_start_blame_view(struct tog_view *);
746 static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
747 size_t *, int **, int **, int **, int **);
749 static const struct got_error *open_tree_view(struct tog_view *,
750 struct got_object_id *, const char *, struct got_repository *);
751 static const struct got_error *show_tree_view(struct tog_view *);
752 static const struct got_error *input_tree_view(struct tog_view **,
753 struct tog_view *, int);
754 static const struct got_error *close_tree_view(struct tog_view *);
755 static const struct got_error *search_start_tree_view(struct tog_view *);
756 static const struct got_error *search_next_tree_view(struct tog_view *);
758 static const struct got_error *open_ref_view(struct tog_view *,
759 struct got_repository *);
760 static const struct got_error *show_ref_view(struct tog_view *);
761 static const struct got_error *input_ref_view(struct tog_view **,
762 struct tog_view *, int);
763 static const struct got_error *close_ref_view(struct tog_view *);
764 static const struct got_error *search_start_ref_view(struct tog_view *);
765 static const struct got_error *search_next_ref_view(struct tog_view *);
767 static const struct got_error *open_help_view(struct tog_view *,
769 static const struct got_error *show_help_view(struct tog_view *);
770 static const struct got_error *input_help_view(struct tog_view **,
771 struct tog_view *, int);
772 static const struct got_error *reset_help_view(struct tog_view *);
773 static const struct got_error* close_help_view(struct tog_view *);
774 static const struct got_error *search_start_help_view(struct tog_view *);
775 static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
776 size_t *, int **, int **, int **, int **);
778 static volatile sig_atomic_t tog_sigwinch_received;
779 static volatile sig_atomic_t tog_sigpipe_received;
780 static volatile sig_atomic_t tog_sigcont_received;
781 static volatile sig_atomic_t tog_sigint_received;
782 static volatile sig_atomic_t tog_sigterm_received;
785 tog_sigwinch(int signo)
787 tog_sigwinch_received = 1;
791 tog_sigpipe(int signo)
793 tog_sigpipe_received = 1;
797 tog_sigcont(int signo)
799 tog_sigcont_received = 1;
803 tog_sigint(int signo)
805 tog_sigint_received = 1;
809 tog_sigterm(int signo)
811 tog_sigterm_received = 1;
815 tog_fatal_signal_received(void)
817 return (tog_sigpipe_received ||
818 tog_sigint_received || tog_sigterm_received);
821 static const struct got_error *
822 view_close(struct tog_view *view)
824 const struct got_error *err = NULL, *child_err = NULL;
827 child_err = view_close(view->child);
831 err = view->close(view);
833 del_panel(view->panel);
835 delwin(view->window);
837 return err ? err : child_err;
840 static struct tog_view *
841 view_open(int nlines, int ncols, int begin_y, int begin_x,
842 enum tog_view_type type)
844 struct tog_view *view = calloc(1, sizeof(*view));
852 view->nlines = nlines ? nlines : LINES - begin_y;
853 view->ncols = ncols ? ncols : COLS - begin_x;
854 view->begin_y = begin_y;
855 view->begin_x = begin_x;
856 view->window = newwin(nlines, ncols, begin_y, begin_x);
857 if (view->window == NULL) {
861 view->panel = new_panel(view->window);
862 if (view->panel == NULL ||
863 set_panel_userptr(view->panel, view) != OK) {
868 keypad(view->window, TRUE);
873 view_split_begin_x(int begin_x)
875 if (begin_x > 0 || COLS < 120)
877 return (COLS - MAX(COLS / 2, 80));
880 /* XXX Stub till we decide what to do. */
882 view_split_begin_y(int lines)
884 return lines * HSPLIT_SCALE;
887 static const struct got_error *view_resize(struct tog_view *);
889 static const struct got_error *
890 view_splitscreen(struct tog_view *view)
892 const struct got_error *err = NULL;
894 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
895 if (view->resized_y && view->resized_y < view->lines)
896 view->begin_y = view->resized_y;
898 view->begin_y = view_split_begin_y(view->nlines);
900 } else if (!view->resized) {
901 if (view->resized_x && view->resized_x < view->cols - 1 &&
903 view->begin_x = view->resized_x;
905 view->begin_x = view_split_begin_x(0);
908 view->nlines = LINES - view->begin_y;
909 view->ncols = COLS - view->begin_x;
912 err = view_resize(view);
916 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
917 view->parent->nlines = view->begin_y;
919 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
920 return got_error_from_errno("mvwin");
925 static const struct got_error *
926 view_fullscreen(struct tog_view *view)
928 const struct got_error *err = NULL;
931 view->begin_y = view->resized ? view->begin_y : 0;
932 view->nlines = view->resized ? view->nlines : LINES;
936 err = view_resize(view);
940 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
941 return got_error_from_errno("mvwin");
947 view_is_parent_view(struct tog_view *view)
949 return view->parent == NULL;
953 view_is_splitscreen(struct tog_view *view)
955 return view->begin_x > 0 || view->begin_y > 0;
959 view_is_fullscreen(struct tog_view *view)
961 return view->nlines == LINES && view->ncols == COLS;
965 view_is_hsplit_top(struct tog_view *view)
967 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
968 view_is_splitscreen(view->child);
972 view_border(struct tog_view *view)
975 const struct tog_view *view_above;
978 return view_border(view->parent);
980 panel = panel_above(view->panel);
984 view_above = panel_userptr(panel);
985 if (view->mode == TOG_VIEW_SPLIT_HRZN)
986 mvwhline(view->window, view_above->begin_y - 1,
987 view->begin_x, got_locale_is_utf8() ?
988 ACS_HLINE : '-', view->ncols);
990 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
991 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
994 static const struct got_error *view_init_hsplit(struct tog_view *, int);
995 static const struct got_error *request_log_commits(struct tog_view *);
996 static const struct got_error *offset_selection_down(struct tog_view *);
997 static void offset_selection_up(struct tog_view *);
998 static void view_get_split(struct tog_view *, int *, int *);
1000 static const struct got_error *
1001 view_resize(struct tog_view *view)
1003 const struct got_error *err = NULL;
1004 int dif, nlines, ncols;
1006 dif = LINES - view->lines; /* line difference */
1008 if (view->lines > LINES)
1009 nlines = view->nlines - (view->lines - LINES);
1011 nlines = view->nlines + (LINES - view->lines);
1012 if (view->cols > COLS)
1013 ncols = view->ncols - (view->cols - COLS);
1015 ncols = view->ncols + (COLS - view->cols);
1018 int hs = view->child->begin_y;
1020 if (!view_is_fullscreen(view))
1021 view->child->begin_x = view_split_begin_x(view->begin_x);
1022 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1023 view->child->begin_x == 0) {
1026 view_fullscreen(view->child);
1027 if (view->child->focussed)
1028 show_panel(view->child->panel);
1030 show_panel(view->panel);
1032 ncols = view->child->begin_x;
1034 view_splitscreen(view->child);
1035 show_panel(view->child->panel);
1038 * XXX This is ugly and needs to be moved into the above
1039 * logic but "works" for now and my attempts at moving it
1040 * break either 'tab' or 'F' key maps in horizontal splits.
1043 err = view_splitscreen(view->child);
1046 if (dif < 0) { /* top split decreased */
1047 err = offset_selection_down(view);
1054 show_panel(view->child->panel);
1055 nlines = view->nlines;
1057 } else if (view->parent == NULL)
1060 if (view->resize && dif > 0) {
1061 err = view->resize(view, dif);
1066 if (wresize(view->window, nlines, ncols) == ERR)
1067 return got_error_from_errno("wresize");
1068 if (replace_panel(view->panel, view->window) == ERR)
1069 return got_error_from_errno("replace_panel");
1070 wclear(view->window);
1072 view->nlines = nlines;
1073 view->ncols = ncols;
1074 view->lines = LINES;
1080 static const struct got_error *
1081 resize_log_view(struct tog_view *view, int increase)
1083 struct tog_log_view_state *s = &view->state.log;
1084 const struct got_error *err = NULL;
1087 if (s->selected_entry)
1088 n = s->selected_entry->idx + view->lines - s->selected;
1091 * Request commits to account for the increased
1092 * height so we have enough to populate the view.
1094 if (s->commits->ncommits < n) {
1095 view->nscrolled = n - s->commits->ncommits + increase + 1;
1096 err = request_log_commits(view);
1103 view_adjust_offset(struct tog_view *view, int n)
1108 if (view->parent && view->parent->offset) {
1109 if (view->parent->offset + n >= 0)
1110 view->parent->offset += n;
1112 view->parent->offset = 0;
1113 } else if (view->offset) {
1114 if (view->offset - n >= 0)
1121 static const struct got_error *
1122 view_resize_split(struct tog_view *view, int resize)
1124 const struct got_error *err = NULL;
1125 struct tog_view *v = NULL;
1132 if (!v->child || !view_is_splitscreen(v->child))
1135 v->resized = v->child->resized = resize; /* lock for resize event */
1137 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1138 if (v->child->resized_y)
1139 v->child->begin_y = v->child->resized_y;
1141 v->child->begin_y -= resize;
1143 v->child->begin_y += resize;
1144 if (v->child->begin_y < 3) {
1146 v->child->begin_y = 3;
1147 } else if (v->child->begin_y > LINES - 1) {
1149 v->child->begin_y = LINES - 1;
1152 v->child->ncols = COLS;
1153 view_adjust_offset(view, resize);
1154 err = view_init_hsplit(v, v->child->begin_y);
1157 v->child->resized_y = v->child->begin_y;
1159 if (v->child->resized_x)
1160 v->child->begin_x = v->child->resized_x;
1162 v->child->begin_x -= resize;
1164 v->child->begin_x += resize;
1165 if (v->child->begin_x < 11) {
1167 v->child->begin_x = 11;
1168 } else if (v->child->begin_x > COLS - 1) {
1170 v->child->begin_x = COLS - 1;
1172 v->child->resized_x = v->child->begin_x;
1175 v->child->mode = v->mode;
1176 v->child->nlines = v->lines - v->child->begin_y;
1177 v->child->ncols = v->cols - v->child->begin_x;
1180 err = view_fullscreen(v);
1183 err = view_splitscreen(v->child);
1187 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1188 err = offset_selection_down(v->child);
1194 err = v->resize(v, 0);
1195 else if (v->child->resize)
1196 err = v->child->resize(v->child, 0);
1198 v->resized = v->child->resized = 0;
1204 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1206 struct tog_view *v = src->child ? src->child : src;
1208 dst->resized_x = v->resized_x;
1209 dst->resized_y = v->resized_y;
1212 static const struct got_error *
1213 view_close_child(struct tog_view *view)
1215 const struct got_error *err = NULL;
1217 if (view->child == NULL)
1220 err = view_close(view->child);
1225 static const struct got_error *
1226 view_set_child(struct tog_view *view, struct tog_view *child)
1228 const struct got_error *err = NULL;
1230 view->child = child;
1231 child->parent = view;
1233 err = view_resize(view);
1237 if (view->child->resized_x || view->child->resized_y)
1238 err = view_resize_split(view, 0);
1243 static const struct got_error *view_dispatch_request(struct tog_view **,
1244 struct tog_view *, enum tog_view_type, int, int);
1246 static const struct got_error *
1247 view_request_new(struct tog_view **requested, struct tog_view *view,
1248 enum tog_view_type request)
1250 struct tog_view *new_view = NULL;
1251 const struct got_error *err;
1256 if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1257 view_get_split(view, &y, &x);
1259 err = view_dispatch_request(&new_view, view, request, y, x);
1263 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1264 request != TOG_VIEW_HELP) {
1265 err = view_init_hsplit(view, y);
1271 new_view->focussed = 1;
1272 new_view->mode = view->mode;
1273 new_view->nlines = request == TOG_VIEW_HELP ?
1274 view->lines : view->lines - y;
1276 if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1277 view_transfer_size(new_view, view);
1278 err = view_close_child(view);
1281 err = view_set_child(view, new_view);
1284 view->focus_child = 1;
1286 *requested = new_view;
1292 tog_resizeterm(void)
1295 struct winsize size;
1297 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1298 cols = 80; /* Default */
1302 lines = size.ws_row;
1304 resize_term(lines, cols);
1307 static const struct got_error *
1308 view_search_start(struct tog_view *view, int fast_refresh)
1310 const struct got_error *err = NULL;
1311 struct tog_view *v = view;
1315 if (view->search_started) {
1316 regfree(&view->regex);
1317 view->searching = 0;
1318 memset(&view->regmatch, 0, sizeof(view->regmatch));
1320 view->search_started = 0;
1322 if (view->nlines < 1)
1325 if (view_is_hsplit_top(view))
1327 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1330 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1331 wclrtoeol(v->window);
1333 nodelay(v->window, FALSE); /* block for search term input */
1336 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1337 wrefresh(v->window);
1340 nodelay(v->window, TRUE);
1341 if (!fast_refresh && !using_mock_io)
1346 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1347 err = view->search_start(view);
1349 regfree(&view->regex);
1352 view->search_started = 1;
1353 view->searching = TOG_SEARCH_FORWARD;
1354 view->search_next_done = 0;
1355 view->search_next(view);
1361 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1362 static const struct got_error *
1363 switch_split(struct tog_view *view)
1365 const struct got_error *err = NULL;
1366 struct tog_view *v = NULL;
1373 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1374 v->mode = TOG_VIEW_SPLIT_VERT;
1376 v->mode = TOG_VIEW_SPLIT_HRZN;
1380 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1381 v->mode = TOG_VIEW_SPLIT_NONE;
1383 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1384 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1385 v->child->begin_y = v->child->resized_y;
1386 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1387 v->child->begin_x = v->child->resized_x;
1390 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1392 v->child->ncols = COLS;
1393 v->child->nscrolled = LINES - v->child->nlines;
1395 err = view_init_hsplit(v, v->child->begin_y);
1399 v->child->mode = v->mode;
1400 v->child->nlines = v->lines - v->child->begin_y;
1403 err = view_fullscreen(v);
1406 err = view_splitscreen(v->child);
1410 if (v->mode == TOG_VIEW_SPLIT_NONE)
1411 v->mode = TOG_VIEW_SPLIT_VERT;
1412 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1413 err = offset_selection_down(v);
1416 err = offset_selection_down(v->child);
1420 offset_selection_up(v);
1421 offset_selection_up(v->child);
1424 err = v->resize(v, 0);
1425 else if (v->child->resize)
1426 err = v->child->resize(v->child, 0);
1432 * Strip trailing whitespace from str starting at byte *n;
1433 * if *n < 0, use strlen(str). Return new str length in *n.
1436 strip_trailing_ws(char *str, int *n)
1440 if (str == NULL || *str == '\0')
1446 while (x-- > 0 && isspace((unsigned char)str[x]))
1453 * Extract visible substring of line y from the curses screen
1454 * and strip trailing whitespace. If vline is set and locale is
1455 * UTF-8, overwrite line[vline] with '|' because the ACS_VLINE
1456 * character is written out as 'x'. Write the line to file f.
1458 static const struct got_error *
1459 view_write_line(FILE *f, int y, int vline)
1461 char line[COLS * MB_LEN_MAX]; /* allow for multibyte chars */
1464 r = mvwinnstr(curscr, y, 0, line, sizeof(line));
1466 return got_error_fmt(GOT_ERR_RANGE,
1467 "failed to extract line %d", y);
1470 * In some views, lines are padded with blanks to COLS width.
1471 * Strip them so we can diff without the -b flag when testing.
1473 strip_trailing_ws(line, &r);
1475 if (vline > 0 && got_locale_is_utf8())
1478 w = fprintf(f, "%s\n", line);
1479 if (w != r + 1) /* \n */
1480 return got_ferror(f, GOT_ERR_IO);
1486 * Capture the visible curses screen by writing each line to the
1487 * file at the path set via the TOG_SCR_DUMP environment variable.
1489 static const struct got_error *
1490 screendump(struct tog_view *view)
1492 const struct got_error *err;
1497 path = getenv("TOG_SCR_DUMP");
1498 if (path == NULL || *path == '\0')
1499 return got_error_msg(GOT_ERR_BAD_PATH,
1500 "TOG_SCR_DUMP path not set to capture screen dump");
1501 f = fopen(path, "wex");
1503 return got_error_from_errno_fmt("fopen: %s", path);
1505 if ((view->child && view->child->begin_x) ||
1506 (view->parent && view->begin_x)) {
1507 int ncols = view->child ? view->ncols : view->parent->ncols;
1509 /* vertical splitscreen */
1510 for (i = 0; i < view->nlines; ++i) {
1511 err = view_write_line(f, i, ncols - 1);
1518 /* fullscreen or horizontal splitscreen */
1519 if ((view->child && view->child->begin_y) ||
1520 (view->parent && view->begin_y)) /* hsplit */
1521 hline = view->child ?
1522 view->child->begin_y : view->begin_y;
1524 for (i = 0; i < view->lines; i++) {
1525 if (hline && got_locale_is_utf8() && i == hline - 1) {
1528 /* ACS_HLINE writes out as 'q', overwrite it */
1529 for (c = 0; c < view->cols; ++c)
1535 err = view_write_line(f, i, 0);
1542 if (f && fclose(f) == EOF && err == NULL)
1543 err = got_ferror(f, GOT_ERR_IO);
1548 * Compute view->count from numeric input. Assign total to view->count and
1549 * return first non-numeric key entered.
1552 get_compound_key(struct tog_view *view, int c)
1554 struct tog_view *v = view;
1557 if (view_is_hsplit_top(view))
1559 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1563 cbreak(); /* block for input */
1564 nodelay(view->window, FALSE);
1565 wmove(v->window, v->nlines - 1, 0);
1566 wclrtoeol(v->window);
1567 waddch(v->window, ':');
1570 x = getcurx(v->window);
1571 if (x != ERR && x < view->ncols) {
1572 waddch(v->window, c);
1573 wrefresh(v->window);
1577 * Don't overflow. Max valid request should be the greatest
1578 * between the longest and total lines; cap at 10 million.
1583 n = n * 10 + (c - '0');
1584 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1586 if (c == 'G' || c == 'g') { /* nG key map */
1587 view->gline = view->hiline = n;
1592 /* Massage excessive or inapplicable values at the input handler. */
1599 action_report(struct tog_view *view)
1601 struct tog_view *v = view;
1603 if (view_is_hsplit_top(view))
1605 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1608 wmove(v->window, v->nlines - 1, 0);
1609 wclrtoeol(v->window);
1610 wprintw(v->window, ":%s", view->action);
1611 wrefresh(v->window);
1614 * Clear action status report. Only clear in blame view
1615 * once annotating is complete, otherwise it's too fast.
1617 if (view->type == TOG_VIEW_BLAME) {
1618 if (view->state.blame.blame_complete)
1619 view->action = NULL;
1621 view->action = NULL;
1625 * Read the next line from the test script and assign
1626 * key instruction to *ch. If at EOF, set the *done flag.
1628 static const struct got_error *
1629 tog_read_script_key(FILE *script, int *ch, int *done)
1631 const struct got_error *err = NULL;
1635 if (getline(&line, &linesz, script) == -1) {
1640 err = got_ferror(script, GOT_ERR_IO);
1643 } else if (strncasecmp(line, "KEY_ENTER", 9) == 0)
1645 else if (strncasecmp(line, "KEY_RIGHT", 9) == 0)
1647 else if (strncasecmp(line, "KEY_LEFT", 8) == 0)
1649 else if (strncasecmp(line, "KEY_DOWN", 8) == 0)
1651 else if (strncasecmp(line, "KEY_UP", 6) == 0)
1653 else if (strncasecmp(line, TOG_SCREEN_DUMP, TOG_SCREEN_DUMP_LEN) == 0)
1654 *ch = TOG_KEY_SCRDUMP;
1663 static const struct got_error *
1664 view_input(struct tog_view **new, int *done, struct tog_view *view,
1665 struct tog_view_list_head *views, int fast_refresh)
1667 const struct got_error *err = NULL;
1674 action_report(view);
1676 /* Clear "no matches" indicator. */
1677 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1678 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1679 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1683 if (view->searching && !view->search_next_done) {
1684 errcode = pthread_mutex_unlock(&tog_mutex);
1686 return got_error_set_errno(errcode,
1687 "pthread_mutex_unlock");
1689 errcode = pthread_mutex_lock(&tog_mutex);
1691 return got_error_set_errno(errcode,
1692 "pthread_mutex_lock");
1693 view->search_next(view);
1697 /* Allow threads to make progress while we are waiting for input. */
1698 errcode = pthread_mutex_unlock(&tog_mutex);
1700 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1702 if (using_mock_io) {
1703 err = tog_read_script_key(tog_io.f, &ch, done);
1706 } else if (view->count && --view->count) {
1708 nodelay(view->window, TRUE);
1709 ch = wgetch(view->window);
1710 /* let C-g or backspace abort unfinished count */
1711 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1716 ch = wgetch(view->window);
1717 if (ch >= '1' && ch <= '9')
1718 view->ch = ch = get_compound_key(view, ch);
1720 if (view->hiline && ch != ERR && ch != 0)
1721 view->hiline = 0; /* key pressed, clear line highlight */
1722 nodelay(view->window, TRUE);
1723 errcode = pthread_mutex_lock(&tog_mutex);
1725 return got_error_set_errno(errcode, "pthread_mutex_lock");
1727 if (tog_sigwinch_received || tog_sigcont_received) {
1729 tog_sigwinch_received = 0;
1730 tog_sigcont_received = 0;
1731 TAILQ_FOREACH(v, views, entry) {
1732 err = view_resize(v);
1735 err = v->input(new, v, KEY_RESIZE);
1739 err = view_resize(v->child);
1742 err = v->child->input(new, v->child,
1746 if (v->child->resized_x || v->child->resized_y) {
1747 err = view_resize_split(v, 0);
1759 if (view->type == TOG_VIEW_HELP)
1760 err = view->reset(view);
1762 err = view_request_new(new, view, TOG_VIEW_HELP);
1768 view->child->focussed = 1;
1769 view->focus_child = 1;
1770 } else if (view->parent) {
1772 view->parent->focussed = 1;
1773 view->parent->focus_child = 0;
1774 if (!view_is_splitscreen(view)) {
1775 if (view->parent->resize) {
1776 err = view->parent->resize(view->parent,
1781 offset_selection_up(view->parent);
1782 err = view_fullscreen(view->parent);
1789 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1790 if (view->parent->resize) {
1791 /* might need more commits to fill fullscreen */
1792 err = view->parent->resize(view->parent, 0);
1796 offset_selection_up(view->parent);
1798 err = view->input(new, view, ch);
1806 if (view_is_parent_view(view)) {
1807 if (view->child == NULL)
1809 if (view_is_splitscreen(view->child)) {
1811 view->child->focussed = 1;
1812 err = view_fullscreen(view->child);
1814 err = view_splitscreen(view->child);
1816 err = view_resize_split(view, 0);
1820 err = view->child->input(new, view->child,
1823 if (view_is_splitscreen(view)) {
1824 view->parent->focussed = 0;
1826 err = view_fullscreen(view);
1828 err = view_splitscreen(view);
1829 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1830 err = view_resize(view->parent);
1832 err = view_resize_split(view, 0);
1836 err = view->input(new, view, KEY_RESIZE);
1841 err = view->resize(view, 0);
1846 err = offset_selection_down(view->parent);
1848 err = offset_selection_down(view);
1852 err = switch_split(view);
1855 err = view_resize_split(view, -1);
1858 err = view_resize_split(view, 1);
1864 if (view->search_start)
1865 view_search_start(view, fast_refresh);
1867 err = view->input(new, view, ch);
1871 if (view->search_started && view->search_next) {
1872 view->searching = (ch == 'n' ?
1873 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1874 view->search_next_done = 0;
1875 view->search_next(view);
1877 err = view->input(new, view, ch);
1880 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1881 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1882 view->action = "Patience diff algorithm";
1884 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1885 view->action = "Myers diff algorithm";
1887 TAILQ_FOREACH(v, views, entry) {
1893 if (v->child && v->child->reset) {
1894 err = v->child->reset(v->child);
1900 case TOG_KEY_SCRDUMP:
1901 err = screendump(view);
1904 err = view->input(new, view, ch);
1912 view_needs_focus_indication(struct tog_view *view)
1914 if (view_is_parent_view(view)) {
1915 if (view->child == NULL || view->child->focussed)
1917 if (!view_is_splitscreen(view->child))
1919 } else if (!view_is_splitscreen(view))
1922 return view->focussed;
1925 static const struct got_error *
1928 const struct got_error *err = NULL;
1930 if (tog_io.cin && fclose(tog_io.cin) == EOF)
1931 err = got_ferror(tog_io.cin, GOT_ERR_IO);
1932 if (tog_io.cout && fclose(tog_io.cout) == EOF && err == NULL)
1933 err = got_ferror(tog_io.cout, GOT_ERR_IO);
1934 if (tog_io.f && fclose(tog_io.f) == EOF && err == NULL)
1935 err = got_ferror(tog_io.f, GOT_ERR_IO);
1940 static const struct got_error *
1941 view_loop(struct tog_view *view)
1943 const struct got_error *err = NULL;
1944 struct tog_view_list_head views;
1945 struct tog_view *new_view;
1947 int fast_refresh = 10;
1948 int done = 0, errcode;
1950 mode = getenv("TOG_VIEW_SPLIT_MODE");
1951 if (!mode || !(*mode == 'h' || *mode == 'H'))
1952 view->mode = TOG_VIEW_SPLIT_VERT;
1954 view->mode = TOG_VIEW_SPLIT_HRZN;
1956 errcode = pthread_mutex_lock(&tog_mutex);
1958 return got_error_set_errno(errcode, "pthread_mutex_lock");
1961 TAILQ_INSERT_HEAD(&views, view, entry);
1964 err = view->show(view);
1969 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1970 !tog_fatal_signal_received()) {
1971 /* Refresh fast during initialization, then become slower. */
1972 if (fast_refresh && --fast_refresh == 0 && !using_mock_io)
1973 halfdelay(10); /* switch to once per second */
1975 err = view_input(&new_view, &done, view, &views, fast_refresh);
1979 if (view->dying && view == TAILQ_FIRST(&views) &&
1980 TAILQ_NEXT(view, entry) == NULL)
1986 * When we quit, scroll the screen up a single line
1987 * so we don't lose any information.
1989 TAILQ_FOREACH(v, &views, entry) {
1990 wmove(v->window, 0, 0);
1991 wdeleteln(v->window);
1992 wnoutrefresh(v->window);
1993 if (v->child && !view_is_fullscreen(v)) {
1994 wmove(v->child->window, 0, 0);
1995 wdeleteln(v->child->window);
1996 wnoutrefresh(v->child->window);
2003 struct tog_view *v, *prev = NULL;
2005 if (view_is_parent_view(view))
2006 prev = TAILQ_PREV(view, tog_view_list_head,
2008 else if (view->parent)
2009 prev = view->parent;
2012 view->parent->child = NULL;
2013 view->parent->focus_child = 0;
2014 /* Restore fullscreen line height. */
2015 view->parent->nlines = view->parent->lines;
2016 err = view_resize(view->parent);
2019 /* Make resized splits persist. */
2020 view_transfer_size(view->parent, view);
2022 TAILQ_REMOVE(&views, view, entry);
2024 err = view_close(view);
2029 TAILQ_FOREACH(v, &views, entry) {
2033 if (view == NULL && new_view == NULL) {
2034 /* No view has focus. Try to pick one. */
2037 else if (!TAILQ_EMPTY(&views)) {
2038 view = TAILQ_LAST(&views,
2039 tog_view_list_head);
2042 if (view->focus_child) {
2043 view->child->focussed = 1;
2051 struct tog_view *v, *t;
2052 /* Only allow one parent view per type. */
2053 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
2054 if (v->type != new_view->type)
2056 TAILQ_REMOVE(&views, v, entry);
2057 err = view_close(v);
2062 TAILQ_INSERT_TAIL(&views, new_view, entry);
2065 if (view && !done) {
2066 if (view_is_parent_view(view)) {
2067 if (view->child && view->child->focussed)
2070 if (view->parent && view->parent->focussed)
2071 view = view->parent;
2073 show_panel(view->panel);
2074 if (view->child && view_is_splitscreen(view->child))
2075 show_panel(view->child->panel);
2076 if (view->parent && view_is_splitscreen(view)) {
2077 err = view->parent->show(view->parent);
2081 err = view->show(view);
2085 err = view->child->show(view->child);
2094 while (!TAILQ_EMPTY(&views)) {
2095 const struct got_error *close_err;
2096 view = TAILQ_FIRST(&views);
2097 TAILQ_REMOVE(&views, view, entry);
2098 close_err = view_close(view);
2099 if (close_err && err == NULL)
2103 errcode = pthread_mutex_unlock(&tog_mutex);
2104 if (errcode && err == NULL)
2105 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2115 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
2120 /* Create newly allocated wide-character string equivalent to a byte string. */
2121 static const struct got_error *
2122 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
2125 const struct got_error *err = NULL;
2128 *wlen = mbstowcs(NULL, s, 0);
2129 if (*wlen == (size_t)-1) {
2131 if (errno != EILSEQ)
2132 return got_error_from_errno("mbstowcs");
2134 /* byte string invalid in current encoding; try to "fix" it */
2135 err = got_mbsavis(&vis, &vislen, s);
2138 *wlen = mbstowcs(NULL, vis, 0);
2139 if (*wlen == (size_t)-1) {
2140 err = got_error_from_errno("mbstowcs"); /* give up */
2145 *ws = calloc(*wlen + 1, sizeof(**ws));
2147 err = got_error_from_errno("calloc");
2151 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
2152 err = got_error_from_errno("mbstowcs");
2163 static const struct got_error *
2164 expand_tab(char **ptr, const char *src)
2167 size_t len, n, idx = 0, sz = 0;
2170 n = len = strlen(src);
2171 dst = malloc(n + 1);
2173 return got_error_from_errno("malloc");
2175 while (idx < len && src[idx]) {
2176 const char c = src[idx];
2179 size_t nb = TABSIZE - sz % TABSIZE;
2182 p = realloc(dst, n + nb);
2185 return got_error_from_errno("realloc");
2190 memset(dst + sz, ' ', nb);
2193 dst[sz++] = src[idx];
2203 * Advance at most n columns from wline starting at offset off.
2204 * Return the index to the first character after the span operation.
2205 * Return the combined column width of all spanned wide character in
2209 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2211 int width, i, cols = 0;
2218 for (i = off; wline[i] != L'\0'; ++i) {
2219 if (wline[i] == L'\t')
2220 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2222 width = wcwidth(wline[i]);
2229 if (cols + width > n)
2239 * Format a line for display, ensuring that it won't overflow a width limit.
2240 * With scrolling, the width returned refers to the scrolled version of the
2241 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2243 static const struct got_error *
2244 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2245 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2247 const struct got_error *err = NULL;
2249 wchar_t *wline = NULL;
2258 err = expand_tab(&exstr, line);
2263 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2268 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2270 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2271 wline[wlen - 1] = L'\0';
2274 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2275 wline[wlen - 1] = L'\0';
2279 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2285 *scrollxp = scrollx;
2293 static const struct got_error*
2294 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2295 struct got_object_id *id, struct got_repository *repo)
2297 static const struct got_error *err = NULL;
2298 struct got_reflist_entry *re;
2304 TAILQ_FOREACH(re, refs, entry) {
2305 struct got_tag_object *tag = NULL;
2306 struct got_object_id *ref_id;
2309 name = got_ref_get_name(re->ref);
2310 if (strcmp(name, GOT_REF_HEAD) == 0)
2312 if (strncmp(name, "refs/", 5) == 0)
2314 if (strncmp(name, "got/", 4) == 0 &&
2315 strncmp(name, "got/backup/", 11) != 0)
2317 if (strncmp(name, "heads/", 6) == 0)
2319 if (strncmp(name, "remotes/", 8) == 0) {
2321 s = strstr(name, "/" GOT_REF_HEAD);
2322 if (s != NULL && s[strlen(s)] == '\0')
2325 err = got_ref_resolve(&ref_id, repo, re->ref);
2328 if (strncmp(name, "tags/", 5) == 0) {
2329 err = got_object_open_as_tag(&tag, repo, ref_id);
2331 if (err->code != GOT_ERR_OBJ_TYPE) {
2335 /* Ref points at something other than a tag. */
2340 cmp = got_object_id_cmp(tag ?
2341 got_object_tag_get_object_id(tag) : ref_id, id);
2344 got_object_tag_close(tag);
2348 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2349 s ? ", " : "", name) == -1) {
2350 err = got_error_from_errno("asprintf");
2361 static const struct got_error *
2362 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2367 smallerthan = strchr(author, '<');
2368 if (smallerthan && smallerthan[1] != '\0')
2369 author = smallerthan + 1;
2370 author[strcspn(author, "@>")] = '\0';
2371 return format_line(wauthor, author_width, NULL, author, 0, limit,
2375 static const struct got_error *
2376 draw_commit(struct tog_view *view, struct got_commit_object *commit,
2377 struct got_object_id *id, const size_t date_display_cols,
2378 int author_display_cols)
2380 struct tog_log_view_state *s = &view->state.log;
2381 const struct got_error *err = NULL;
2382 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2383 char *logmsg0 = NULL, *logmsg = NULL;
2384 char *author = NULL;
2385 wchar_t *wlogmsg = NULL, *wauthor = NULL;
2386 int author_width, logmsg_width;
2387 char *newline, *line = NULL;
2388 int col, limit, scrollx;
2389 const int avail = view->ncols;
2391 time_t committer_time;
2392 struct tog_color *tc;
2394 committer_time = got_object_commit_get_committer_time(commit);
2395 if (gmtime_r(&committer_time, &tm) == NULL)
2396 return got_error_from_errno("gmtime_r");
2397 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2398 return got_error(GOT_ERR_NO_SPACE);
2400 if (avail <= date_display_cols)
2401 limit = MIN(sizeof(datebuf) - 1, avail);
2403 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2404 tc = get_color(&s->colors, TOG_COLOR_DATE);
2406 wattr_on(view->window,
2407 COLOR_PAIR(tc->colorpair), NULL);
2408 waddnstr(view->window, datebuf, limit);
2410 wattr_off(view->window,
2411 COLOR_PAIR(tc->colorpair), NULL);
2418 err = got_object_id_str(&id_str, id);
2421 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2423 wattr_on(view->window,
2424 COLOR_PAIR(tc->colorpair), NULL);
2425 wprintw(view->window, "%.8s ", id_str);
2427 wattr_off(view->window,
2428 COLOR_PAIR(tc->colorpair), NULL);
2435 if (s->use_committer)
2436 author = strdup(got_object_commit_get_committer(commit));
2438 author = strdup(got_object_commit_get_author(commit));
2439 if (author == NULL) {
2440 err = got_error_from_errno("strdup");
2443 err = format_author(&wauthor, &author_width, author, avail - col, col);
2446 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2448 wattr_on(view->window,
2449 COLOR_PAIR(tc->colorpair), NULL);
2450 waddwstr(view->window, wauthor);
2451 col += author_width;
2452 while (col < avail && author_width < author_display_cols + 2) {
2453 waddch(view->window, ' ');
2458 wattr_off(view->window,
2459 COLOR_PAIR(tc->colorpair), NULL);
2463 err = got_object_commit_get_logmsg(&logmsg0, commit);
2467 while (*logmsg == '\n')
2469 newline = strchr(logmsg, '\n');
2472 limit = avail - col;
2473 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2474 limit--; /* for the border */
2475 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2479 waddwstr(view->window, &wlogmsg[scrollx]);
2480 col += MAX(logmsg_width, 0);
2481 while (col < avail) {
2482 waddch(view->window, ' ');
2494 static struct commit_queue_entry *
2495 alloc_commit_queue_entry(struct got_commit_object *commit,
2496 struct got_object_id *id)
2498 struct commit_queue_entry *entry;
2499 struct got_object_id *dup;
2501 entry = calloc(1, sizeof(*entry));
2505 dup = got_object_id_dup(id);
2512 entry->commit = commit;
2517 pop_commit(struct commit_queue *commits)
2519 struct commit_queue_entry *entry;
2521 entry = TAILQ_FIRST(&commits->head);
2522 TAILQ_REMOVE(&commits->head, entry, entry);
2523 got_object_commit_close(entry->commit);
2524 commits->ncommits--;
2530 free_commits(struct commit_queue *commits)
2532 while (!TAILQ_EMPTY(&commits->head))
2533 pop_commit(commits);
2536 static const struct got_error *
2537 match_commit(int *have_match, struct got_object_id *id,
2538 struct got_commit_object *commit, regex_t *regex)
2540 const struct got_error *err = NULL;
2541 regmatch_t regmatch;
2542 char *id_str = NULL, *logmsg = NULL;
2546 err = got_object_id_str(&id_str, id);
2550 err = got_object_commit_get_logmsg(&logmsg, commit);
2554 if (regexec(regex, got_object_commit_get_author(commit), 1,
2555 ®match, 0) == 0 ||
2556 regexec(regex, got_object_commit_get_committer(commit), 1,
2557 ®match, 0) == 0 ||
2558 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2559 regexec(regex, logmsg, 1, ®match, 0) == 0)
2567 static const struct got_error *
2568 queue_commits(struct tog_log_thread_args *a)
2570 const struct got_error *err = NULL;
2573 * We keep all commits open throughout the lifetime of the log
2574 * view in order to avoid having to re-fetch commits from disk
2575 * while updating the display.
2578 struct got_object_id id;
2579 struct got_commit_object *commit;
2580 struct commit_queue_entry *entry;
2581 int limit_match = 0;
2584 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2589 err = got_object_open_as_commit(&commit, a->repo, &id);
2592 entry = alloc_commit_queue_entry(commit, &id);
2593 if (entry == NULL) {
2594 err = got_error_from_errno("alloc_commit_queue_entry");
2598 errcode = pthread_mutex_lock(&tog_mutex);
2600 err = got_error_set_errno(errcode,
2601 "pthread_mutex_lock");
2605 entry->idx = a->real_commits->ncommits;
2606 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2607 a->real_commits->ncommits++;
2610 err = match_commit(&limit_match, &id, commit,
2616 struct commit_queue_entry *matched;
2618 matched = alloc_commit_queue_entry(
2619 entry->commit, entry->id);
2620 if (matched == NULL) {
2621 err = got_error_from_errno(
2622 "alloc_commit_queue_entry");
2625 matched->commit = entry->commit;
2626 got_object_commit_retain(entry->commit);
2628 matched->idx = a->limit_commits->ncommits;
2629 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2631 a->limit_commits->ncommits++;
2635 * This is how we signal log_thread() that we
2636 * have found a match, and that it should be
2637 * counted as a new entry for the view.
2639 a->limit_match = limit_match;
2642 if (*a->searching == TOG_SEARCH_FORWARD &&
2643 !*a->search_next_done) {
2645 err = match_commit(&have_match, &id, commit, a->regex);
2650 if (limit_match && have_match)
2651 *a->search_next_done =
2652 TOG_SEARCH_HAVE_MORE;
2653 } else if (have_match)
2654 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2657 errcode = pthread_mutex_unlock(&tog_mutex);
2658 if (errcode && err == NULL)
2659 err = got_error_set_errno(errcode,
2660 "pthread_mutex_unlock");
2663 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2669 select_commit(struct tog_log_view_state *s)
2671 struct commit_queue_entry *entry;
2674 entry = s->first_displayed_entry;
2676 if (ncommits == s->selected) {
2677 s->selected_entry = entry;
2680 entry = TAILQ_NEXT(entry, entry);
2685 static const struct got_error *
2686 draw_commits(struct tog_view *view)
2688 const struct got_error *err = NULL;
2689 struct tog_log_view_state *s = &view->state.log;
2690 struct commit_queue_entry *entry = s->selected_entry;
2691 int limit = view->nlines;
2693 int ncommits, author_cols = 4;
2694 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2695 char *refs_str = NULL;
2697 struct tog_color *tc;
2698 static const size_t date_display_cols = 12;
2700 if (view_is_hsplit_top(view))
2701 --limit; /* account for border */
2703 if (s->selected_entry &&
2704 !(view->searching && view->search_next_done == 0)) {
2705 struct got_reflist_head *refs;
2706 err = got_object_id_str(&id_str, s->selected_entry->id);
2709 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2710 s->selected_entry->id);
2712 err = build_refs_str(&refs_str, refs,
2713 s->selected_entry->id, s->repo);
2719 if (s->thread_args.commits_needed == 0 && !using_mock_io)
2720 halfdelay(10); /* disable fast refresh */
2722 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2723 if (asprintf(&ncommits_str, " [%d/%d] %s",
2724 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2725 (view->searching && !view->search_next_done) ?
2726 "searching..." : "loading...") == -1) {
2727 err = got_error_from_errno("asprintf");
2731 const char *search_str = NULL;
2732 const char *limit_str = NULL;
2734 if (view->searching) {
2735 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2736 search_str = "no more matches";
2737 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2738 search_str = "no matches found";
2739 else if (!view->search_next_done)
2740 search_str = "searching...";
2743 if (s->limit_view && s->commits->ncommits == 0)
2744 limit_str = "no matches found";
2746 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2747 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2748 search_str ? search_str : (refs_str ? refs_str : ""),
2749 limit_str ? limit_str : "") == -1) {
2750 err = got_error_from_errno("asprintf");
2755 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2756 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2757 "........................................",
2758 s->in_repo_path, ncommits_str) == -1) {
2759 err = got_error_from_errno("asprintf");
2763 } else if (asprintf(&header, "commit %s%s",
2764 id_str ? id_str : "........................................",
2765 ncommits_str) == -1) {
2766 err = got_error_from_errno("asprintf");
2770 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2774 werase(view->window);
2776 if (view_needs_focus_indication(view))
2777 wstandout(view->window);
2778 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2780 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2781 waddwstr(view->window, wline);
2782 while (width < view->ncols) {
2783 waddch(view->window, ' ');
2787 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2788 if (view_needs_focus_indication(view))
2789 wstandend(view->window);
2794 /* Grow author column size if necessary, and set view->maxx. */
2795 entry = s->first_displayed_entry;
2799 struct got_commit_object *c = entry->commit;
2800 char *author, *eol, *msg, *msg0;
2801 wchar_t *wauthor, *wmsg;
2803 if (ncommits >= limit - 1)
2805 if (s->use_committer)
2806 author = strdup(got_object_commit_get_committer(c));
2808 author = strdup(got_object_commit_get_author(c));
2809 if (author == NULL) {
2810 err = got_error_from_errno("strdup");
2813 err = format_author(&wauthor, &width, author, COLS,
2815 if (author_cols < width)
2816 author_cols = width;
2821 err = got_object_commit_get_logmsg(&msg0, c);
2825 while (*msg == '\n')
2827 if ((eol = strchr(msg, '\n')))
2829 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2830 date_display_cols + author_cols, 0);
2833 view->maxx = MAX(view->maxx, width);
2837 entry = TAILQ_NEXT(entry, entry);
2840 entry = s->first_displayed_entry;
2841 s->last_displayed_entry = s->first_displayed_entry;
2844 if (ncommits >= limit - 1)
2846 if (ncommits == s->selected)
2847 wstandout(view->window);
2848 err = draw_commit(view, entry->commit, entry->id,
2849 date_display_cols, author_cols);
2850 if (ncommits == s->selected)
2851 wstandend(view->window);
2855 s->last_displayed_entry = entry;
2856 entry = TAILQ_NEXT(entry, entry);
2869 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2871 struct commit_queue_entry *entry;
2874 entry = TAILQ_FIRST(&s->commits->head);
2875 if (s->first_displayed_entry == entry)
2878 entry = s->first_displayed_entry;
2879 while (entry && nscrolled < maxscroll) {
2880 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2882 s->first_displayed_entry = entry;
2888 static const struct got_error *
2889 trigger_log_thread(struct tog_view *view, int wait)
2891 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2895 halfdelay(1); /* fast refresh while loading commits */
2897 while (!ta->log_complete && !tog_thread_error &&
2898 (ta->commits_needed > 0 || ta->load_all)) {
2899 /* Wake the log thread. */
2900 errcode = pthread_cond_signal(&ta->need_commits);
2902 return got_error_set_errno(errcode,
2903 "pthread_cond_signal");
2906 * The mutex will be released while the view loop waits
2907 * in wgetch(), at which time the log thread will run.
2912 /* Display progress update in log view. */
2913 show_log_view(view);
2917 /* Wait right here while next commit is being loaded. */
2918 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2920 return got_error_set_errno(errcode,
2921 "pthread_cond_wait");
2923 /* Display progress update in log view. */
2924 show_log_view(view);
2932 static const struct got_error *
2933 request_log_commits(struct tog_view *view)
2935 struct tog_log_view_state *state = &view->state.log;
2936 const struct got_error *err = NULL;
2938 if (state->thread_args.log_complete)
2941 state->thread_args.commits_needed += view->nscrolled;
2942 err = trigger_log_thread(view, 1);
2943 view->nscrolled = 0;
2948 static const struct got_error *
2949 log_scroll_down(struct tog_view *view, int maxscroll)
2951 struct tog_log_view_state *s = &view->state.log;
2952 const struct got_error *err = NULL;
2953 struct commit_queue_entry *pentry;
2954 int nscrolled = 0, ncommits_needed;
2956 if (s->last_displayed_entry == NULL)
2959 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2960 if (s->commits->ncommits < ncommits_needed &&
2961 !s->thread_args.log_complete) {
2963 * Ask the log thread for required amount of commits.
2965 s->thread_args.commits_needed +=
2966 ncommits_needed - s->commits->ncommits;
2967 err = trigger_log_thread(view, 1);
2973 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2974 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2977 s->last_displayed_entry = pentry ?
2978 pentry : s->last_displayed_entry;
2980 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2983 s->first_displayed_entry = pentry;
2984 } while (++nscrolled < maxscroll);
2986 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2987 view->nscrolled += nscrolled;
2989 view->nscrolled = 0;
2994 static const struct got_error *
2995 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2996 struct got_commit_object *commit, struct got_object_id *commit_id,
2997 struct tog_view *log_view, struct got_repository *repo)
2999 const struct got_error *err;
3000 struct got_object_qid *parent_id;
3001 struct tog_view *diff_view;
3003 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
3004 if (diff_view == NULL)
3005 return got_error_from_errno("view_open");
3007 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3008 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
3009 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
3011 *new_view = diff_view;
3015 static const struct got_error *
3016 tree_view_visit_subtree(struct tog_tree_view_state *s,
3017 struct got_tree_object *subtree)
3019 struct tog_parent_tree *parent;
3021 parent = calloc(1, sizeof(*parent));
3023 return got_error_from_errno("calloc");
3025 parent->tree = s->tree;
3026 parent->first_displayed_entry = s->first_displayed_entry;
3027 parent->selected_entry = s->selected_entry;
3028 parent->selected = s->selected;
3029 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3032 s->first_displayed_entry = NULL;
3036 static const struct got_error *
3037 tree_view_walk_path(struct tog_tree_view_state *s,
3038 struct got_commit_object *commit, const char *path)
3040 const struct got_error *err = NULL;
3041 struct got_tree_object *tree = NULL;
3043 char *slash, *subpath = NULL;
3045 /* Walk the path and open corresponding tree objects. */
3048 struct got_tree_entry *te;
3049 struct got_object_id *tree_id;
3055 /* Ensure the correct subtree entry is selected. */
3056 slash = strchr(p, '/');
3058 te_name = strdup(p);
3060 te_name = strndup(p, slash - p);
3061 if (te_name == NULL) {
3062 err = got_error_from_errno("strndup");
3065 te = got_object_tree_find_entry(s->tree, te_name);
3067 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
3072 s->first_displayed_entry = s->selected_entry = te;
3074 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
3075 break; /* jump to this file's entry */
3077 slash = strchr(p, '/');
3079 subpath = strndup(path, slash - path);
3081 subpath = strdup(path);
3082 if (subpath == NULL) {
3083 err = got_error_from_errno("strdup");
3087 err = got_object_id_by_path(&tree_id, s->repo, commit,
3092 err = got_object_open_as_tree(&tree, s->repo, tree_id);
3097 err = tree_view_visit_subtree(s, tree);
3099 got_object_tree_close(tree);
3113 static const struct got_error *
3114 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
3115 struct commit_queue_entry *entry, const char *path,
3116 const char *head_ref_name, struct got_repository *repo)
3118 const struct got_error *err = NULL;
3119 struct tog_tree_view_state *s;
3120 struct tog_view *tree_view;
3122 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
3123 if (tree_view == NULL)
3124 return got_error_from_errno("view_open");
3126 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
3129 s = &tree_view->state.tree;
3131 *new_view = tree_view;
3133 if (got_path_is_root_dir(path))
3136 return tree_view_walk_path(s, entry->commit, path);
3139 static const struct got_error *
3140 block_signals_used_by_main_thread(void)
3145 if (sigemptyset(&sigset) == -1)
3146 return got_error_from_errno("sigemptyset");
3148 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
3149 if (sigaddset(&sigset, SIGWINCH) == -1)
3150 return got_error_from_errno("sigaddset");
3151 if (sigaddset(&sigset, SIGCONT) == -1)
3152 return got_error_from_errno("sigaddset");
3153 if (sigaddset(&sigset, SIGINT) == -1)
3154 return got_error_from_errno("sigaddset");
3155 if (sigaddset(&sigset, SIGTERM) == -1)
3156 return got_error_from_errno("sigaddset");
3158 /* ncurses handles SIGTSTP */
3159 if (sigaddset(&sigset, SIGTSTP) == -1)
3160 return got_error_from_errno("sigaddset");
3162 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
3164 return got_error_set_errno(errcode, "pthread_sigmask");
3170 log_thread(void *arg)
3172 const struct got_error *err = NULL;
3174 struct tog_log_thread_args *a = arg;
3178 * Sync startup with main thread such that we begin our
3179 * work once view_input() has released the mutex.
3181 errcode = pthread_mutex_lock(&tog_mutex);
3183 err = got_error_set_errno(errcode, "pthread_mutex_lock");
3187 err = block_signals_used_by_main_thread();
3189 pthread_mutex_unlock(&tog_mutex);
3193 while (!done && !err && !tog_fatal_signal_received()) {
3194 errcode = pthread_mutex_unlock(&tog_mutex);
3196 err = got_error_set_errno(errcode,
3197 "pthread_mutex_unlock");
3200 err = queue_commits(a);
3202 if (err->code != GOT_ERR_ITER_COMPLETED)
3206 } else if (a->commits_needed > 0 && !a->load_all) {
3209 a->commits_needed--;
3211 a->commits_needed--;
3214 errcode = pthread_mutex_lock(&tog_mutex);
3216 err = got_error_set_errno(errcode,
3217 "pthread_mutex_lock");
3219 } else if (*a->quit)
3221 else if (*a->limiting && *a->first_displayed_entry == NULL) {
3222 *a->first_displayed_entry =
3223 TAILQ_FIRST(&a->limit_commits->head);
3224 *a->selected_entry = *a->first_displayed_entry;
3225 } else if (*a->first_displayed_entry == NULL) {
3226 *a->first_displayed_entry =
3227 TAILQ_FIRST(&a->real_commits->head);
3228 *a->selected_entry = *a->first_displayed_entry;
3231 errcode = pthread_cond_signal(&a->commit_loaded);
3233 err = got_error_set_errno(errcode,
3234 "pthread_cond_signal");
3235 pthread_mutex_unlock(&tog_mutex);
3240 a->commits_needed = 0;
3242 if (a->commits_needed == 0 && !a->load_all) {
3243 errcode = pthread_cond_wait(&a->need_commits,
3246 err = got_error_set_errno(errcode,
3247 "pthread_cond_wait");
3248 pthread_mutex_unlock(&tog_mutex);
3256 a->log_complete = 1;
3257 errcode = pthread_mutex_unlock(&tog_mutex);
3259 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3262 tog_thread_error = 1;
3263 pthread_cond_signal(&a->commit_loaded);
3268 static const struct got_error *
3269 stop_log_thread(struct tog_log_view_state *s)
3271 const struct got_error *err = NULL, *thread_err = NULL;
3276 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3278 return got_error_set_errno(errcode,
3279 "pthread_cond_signal");
3280 errcode = pthread_mutex_unlock(&tog_mutex);
3282 return got_error_set_errno(errcode,
3283 "pthread_mutex_unlock");
3284 errcode = pthread_join(s->thread, (void **)&thread_err);
3286 return got_error_set_errno(errcode, "pthread_join");
3287 errcode = pthread_mutex_lock(&tog_mutex);
3289 return got_error_set_errno(errcode,
3290 "pthread_mutex_lock");
3291 s->thread = 0; //NULL;
3294 if (s->thread_args.repo) {
3295 err = got_repo_close(s->thread_args.repo);
3296 s->thread_args.repo = NULL;
3299 if (s->thread_args.pack_fds) {
3300 const struct got_error *pack_err =
3301 got_repo_pack_fds_close(s->thread_args.pack_fds);
3304 s->thread_args.pack_fds = NULL;
3307 if (s->thread_args.graph) {
3308 got_commit_graph_close(s->thread_args.graph);
3309 s->thread_args.graph = NULL;
3312 return err ? err : thread_err;
3315 static const struct got_error *
3316 close_log_view(struct tog_view *view)
3318 const struct got_error *err = NULL;
3319 struct tog_log_view_state *s = &view->state.log;
3322 err = stop_log_thread(s);
3324 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3325 if (errcode && err == NULL)
3326 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3328 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3329 if (errcode && err == NULL)
3330 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3332 free_commits(&s->limit_commits);
3333 free_commits(&s->real_commits);
3334 free(s->in_repo_path);
3335 s->in_repo_path = NULL;
3338 free(s->head_ref_name);
3339 s->head_ref_name = NULL;
3344 * We use two queues to implement the limit feature: first consists of
3345 * commits matching the current limit_regex; second is the real queue
3346 * of all known commits (real_commits). When the user starts limiting,
3347 * we swap queues such that all movement and displaying functionality
3348 * works with very slight change.
3350 static const struct got_error *
3351 limit_log_view(struct tog_view *view)
3353 struct tog_log_view_state *s = &view->state.log;
3354 struct commit_queue_entry *entry;
3355 struct tog_view *v = view;
3356 const struct got_error *err = NULL;
3360 if (view_is_hsplit_top(view))
3362 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3365 /* Get the pattern */
3366 wmove(v->window, v->nlines - 1, 0);
3367 wclrtoeol(v->window);
3368 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3369 nodelay(v->window, FALSE);
3372 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3375 nodelay(v->window, TRUE);
3379 if (*pattern == '\0') {
3381 * Safety measure for the situation where the user
3382 * resets limit without previously limiting anything.
3388 * User could have pressed Ctrl+L, which refreshed the
3389 * commit queues, it means we can't save previously
3390 * (before limit took place) displayed entries,
3391 * because they would point to already free'ed memory,
3392 * so we are forced to always select first entry of
3395 s->commits = &s->real_commits;
3396 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3397 s->selected_entry = s->first_displayed_entry;
3404 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3409 /* Clear the screen while loading limit view */
3410 s->first_displayed_entry = NULL;
3411 s->last_displayed_entry = NULL;
3412 s->selected_entry = NULL;
3413 s->commits = &s->limit_commits;
3415 /* Prepare limit queue for new search */
3416 free_commits(&s->limit_commits);
3417 s->limit_commits.ncommits = 0;
3419 /* First process commits, which are in queue already */
3420 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3423 err = match_commit(&have_match, entry->id,
3424 entry->commit, &s->limit_regex);
3429 struct commit_queue_entry *matched;
3431 matched = alloc_commit_queue_entry(entry->commit,
3433 if (matched == NULL) {
3434 err = got_error_from_errno(
3435 "alloc_commit_queue_entry");
3438 matched->commit = entry->commit;
3439 got_object_commit_retain(entry->commit);
3441 matched->idx = s->limit_commits.ncommits;
3442 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3444 s->limit_commits.ncommits++;
3448 /* Second process all the commits, until we fill the screen */
3449 if (s->limit_commits.ncommits < view->nlines - 1 &&
3450 !s->thread_args.log_complete) {
3451 s->thread_args.commits_needed +=
3452 view->nlines - s->limit_commits.ncommits - 1;
3453 err = trigger_log_thread(view, 1);
3458 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3459 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3465 static const struct got_error *
3466 search_start_log_view(struct tog_view *view)
3468 struct tog_log_view_state *s = &view->state.log;
3470 s->matched_entry = NULL;
3471 s->search_entry = NULL;
3475 static const struct got_error *
3476 search_next_log_view(struct tog_view *view)
3478 const struct got_error *err = NULL;
3479 struct tog_log_view_state *s = &view->state.log;
3480 struct commit_queue_entry *entry;
3482 /* Display progress update in log view. */
3483 show_log_view(view);
3487 if (s->search_entry) {
3489 errcode = pthread_mutex_unlock(&tog_mutex);
3491 return got_error_set_errno(errcode,
3492 "pthread_mutex_unlock");
3493 ch = wgetch(view->window);
3494 errcode = pthread_mutex_lock(&tog_mutex);
3496 return got_error_set_errno(errcode,
3497 "pthread_mutex_lock");
3498 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3499 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3502 if (view->searching == TOG_SEARCH_FORWARD)
3503 entry = TAILQ_NEXT(s->search_entry, entry);
3505 entry = TAILQ_PREV(s->search_entry,
3506 commit_queue_head, entry);
3507 } else if (s->matched_entry) {
3509 * If the user has moved the cursor after we hit a match,
3510 * the position from where we should continue searching
3511 * might have changed.
3513 if (view->searching == TOG_SEARCH_FORWARD)
3514 entry = TAILQ_NEXT(s->selected_entry, entry);
3516 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3519 entry = s->selected_entry;
3525 if (entry == NULL) {
3526 if (s->thread_args.log_complete ||
3527 view->searching == TOG_SEARCH_BACKWARD) {
3528 view->search_next_done =
3529 (s->matched_entry == NULL ?
3530 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3531 s->search_entry = NULL;
3535 * Poke the log thread for more commits and return,
3536 * allowing the main loop to make progress. Search
3537 * will resume at s->search_entry once we come back.
3539 s->thread_args.commits_needed++;
3540 return trigger_log_thread(view, 0);
3543 err = match_commit(&have_match, entry->id, entry->commit,
3548 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3549 s->matched_entry = entry;
3553 s->search_entry = entry;
3554 if (view->searching == TOG_SEARCH_FORWARD)
3555 entry = TAILQ_NEXT(entry, entry);
3557 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3560 if (s->matched_entry) {
3561 int cur = s->selected_entry->idx;
3562 while (cur < s->matched_entry->idx) {
3563 err = input_log_view(NULL, view, KEY_DOWN);
3568 while (cur > s->matched_entry->idx) {
3569 err = input_log_view(NULL, view, KEY_UP);
3576 s->search_entry = NULL;
3581 static const struct got_error *
3582 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3583 struct got_repository *repo, const char *head_ref_name,
3584 const char *in_repo_path, int log_branches)
3586 const struct got_error *err = NULL;
3587 struct tog_log_view_state *s = &view->state.log;
3588 struct got_repository *thread_repo = NULL;
3589 struct got_commit_graph *thread_graph = NULL;
3592 if (in_repo_path != s->in_repo_path) {
3593 free(s->in_repo_path);
3594 s->in_repo_path = strdup(in_repo_path);
3595 if (s->in_repo_path == NULL) {
3596 err = got_error_from_errno("strdup");
3601 /* The commit queue only contains commits being displayed. */
3602 TAILQ_INIT(&s->real_commits.head);
3603 s->real_commits.ncommits = 0;
3604 s->commits = &s->real_commits;
3606 TAILQ_INIT(&s->limit_commits.head);
3608 s->limit_commits.ncommits = 0;
3611 if (head_ref_name) {
3612 s->head_ref_name = strdup(head_ref_name);
3613 if (s->head_ref_name == NULL) {
3614 err = got_error_from_errno("strdup");
3618 s->start_id = got_object_id_dup(start_id);
3619 if (s->start_id == NULL) {
3620 err = got_error_from_errno("got_object_id_dup");
3623 s->log_branches = log_branches;
3624 s->use_committer = 1;
3626 STAILQ_INIT(&s->colors);
3627 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3628 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3629 get_color_value("TOG_COLOR_COMMIT"));
3632 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3633 get_color_value("TOG_COLOR_AUTHOR"));
3635 free_colors(&s->colors);
3638 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3639 get_color_value("TOG_COLOR_DATE"));
3641 free_colors(&s->colors);
3646 view->show = show_log_view;
3647 view->input = input_log_view;
3648 view->resize = resize_log_view;
3649 view->close = close_log_view;
3650 view->search_start = search_start_log_view;
3651 view->search_next = search_next_log_view;
3653 if (s->thread_args.pack_fds == NULL) {
3654 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3658 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3659 s->thread_args.pack_fds);
3662 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3666 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3667 s->repo, NULL, NULL);
3671 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3673 err = got_error_set_errno(errcode, "pthread_cond_init");
3676 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3678 err = got_error_set_errno(errcode, "pthread_cond_init");
3682 s->thread_args.commits_needed = view->nlines;
3683 s->thread_args.graph = thread_graph;
3684 s->thread_args.real_commits = &s->real_commits;
3685 s->thread_args.limit_commits = &s->limit_commits;
3686 s->thread_args.in_repo_path = s->in_repo_path;
3687 s->thread_args.start_id = s->start_id;
3688 s->thread_args.repo = thread_repo;
3689 s->thread_args.log_complete = 0;
3690 s->thread_args.quit = &s->quit;
3691 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3692 s->thread_args.selected_entry = &s->selected_entry;
3693 s->thread_args.searching = &view->searching;
3694 s->thread_args.search_next_done = &view->search_next_done;
3695 s->thread_args.regex = &view->regex;
3696 s->thread_args.limiting = &s->limit_view;
3697 s->thread_args.limit_regex = &s->limit_regex;
3698 s->thread_args.limit_commits = &s->limit_commits;
3701 if (view->close == NULL)
3702 close_log_view(view);
3708 static const struct got_error *
3709 show_log_view(struct tog_view *view)
3711 const struct got_error *err;
3712 struct tog_log_view_state *s = &view->state.log;
3714 if (s->thread == 0) { //NULL) {
3715 int errcode = pthread_create(&s->thread, NULL, log_thread,
3718 return got_error_set_errno(errcode, "pthread_create");
3719 if (s->thread_args.commits_needed > 0) {
3720 err = trigger_log_thread(view, 1);
3726 return draw_commits(view);
3730 log_move_cursor_up(struct tog_view *view, int page, int home)
3732 struct tog_log_view_state *s = &view->state.log;
3734 if (s->first_displayed_entry == NULL)
3736 if (s->selected_entry->idx == 0)
3739 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3741 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3743 if (!page && !home && s->selected > 0)
3746 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3752 static const struct got_error *
3753 log_move_cursor_down(struct tog_view *view, int page)
3755 struct tog_log_view_state *s = &view->state.log;
3756 const struct got_error *err = NULL;
3757 int eos = view->nlines - 2;
3759 if (s->first_displayed_entry == NULL)
3762 if (s->thread_args.log_complete &&
3763 s->selected_entry->idx >= s->commits->ncommits - 1)
3766 if (view_is_hsplit_top(view))
3767 --eos; /* border consumes the last line */
3770 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3773 err = log_scroll_down(view, 1);
3774 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3775 struct commit_queue_entry *entry;
3779 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3780 s->last_displayed_entry = entry;
3781 for (n = 0; n <= eos; n++) {
3784 s->first_displayed_entry = entry;
3785 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3788 s->selected = n - 1;
3790 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3791 s->thread_args.log_complete)
3792 s->selected += MIN(page,
3793 s->commits->ncommits - s->selected_entry->idx - 1);
3795 err = log_scroll_down(view, page);
3801 * We might necessarily overshoot in horizontal
3802 * splits; if so, select the last displayed commit.
3804 if (s->first_displayed_entry && s->last_displayed_entry) {
3805 s->selected = MIN(s->selected,
3806 s->last_displayed_entry->idx -
3807 s->first_displayed_entry->idx);
3812 if (s->thread_args.log_complete &&
3813 s->selected_entry->idx == s->commits->ncommits - 1)
3820 view_get_split(struct tog_view *view, int *y, int *x)
3825 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3826 if (view->child && view->child->resized_y)
3827 *y = view->child->resized_y;
3828 else if (view->resized_y)
3829 *y = view->resized_y;
3831 *y = view_split_begin_y(view->lines);
3832 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3833 if (view->child && view->child->resized_x)
3834 *x = view->child->resized_x;
3835 else if (view->resized_x)
3836 *x = view->resized_x;
3838 *x = view_split_begin_x(view->begin_x);
3842 /* Split view horizontally at y and offset view->state->selected line. */
3843 static const struct got_error *
3844 view_init_hsplit(struct tog_view *view, int y)
3846 const struct got_error *err = NULL;
3850 err = view_resize(view);
3854 err = offset_selection_down(view);
3859 static const struct got_error *
3860 log_goto_line(struct tog_view *view, int nlines)
3862 const struct got_error *err = NULL;
3863 struct tog_log_view_state *s = &view->state.log;
3864 int g, idx = s->selected_entry->idx;
3866 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3872 if (g >= s->first_displayed_entry->idx + 1 &&
3873 g <= s->last_displayed_entry->idx + 1 &&
3874 g - s->first_displayed_entry->idx - 1 < nlines) {
3875 s->selected = g - s->first_displayed_entry->idx - 1;
3881 err = log_move_cursor_down(view, g - idx - 1);
3882 if (!err && g > s->selected_entry->idx + 1)
3883 err = log_move_cursor_down(view,
3884 g - s->first_displayed_entry->idx - 1);
3887 } else if (idx + 1 > g)
3888 log_move_cursor_up(view, idx - g + 1, 0);
3890 if (g < nlines && s->first_displayed_entry->idx == 0)
3891 s->selected = g - 1;
3899 horizontal_scroll_input(struct tog_view *view, int ch)
3905 view->x -= MIN(view->x, 2);
3911 if (view->x + view->ncols / 2 < view->maxx)
3920 view->x = MAX(view->maxx - view->ncols / 2, 0);
3928 static const struct got_error *
3929 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3931 const struct got_error *err = NULL;
3932 struct tog_log_view_state *s = &view->state.log;
3935 if (s->thread_args.load_all) {
3936 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3937 s->thread_args.load_all = 0;
3938 else if (s->thread_args.log_complete) {
3939 err = log_move_cursor_down(view, s->commits->ncommits);
3940 s->thread_args.load_all = 0;
3946 eos = nscroll = view->nlines - 1;
3947 if (view_is_hsplit_top(view))
3951 return log_goto_line(view, eos);
3955 err = limit_log_view(view);
3966 horizontal_scroll_input(view, ch);
3973 log_move_cursor_up(view, 0, 0);
3978 log_move_cursor_up(view, 0, 1);
3988 log_move_cursor_up(view, nscroll, 0);
3995 err = log_move_cursor_down(view, 0);
3998 s->use_committer = !s->use_committer;
3999 view->action = s->use_committer ?
4000 "show committer" : "show commit author";
4005 /* We don't know yet how many commits, so we're forced to
4006 * traverse them all. */
4008 s->thread_args.load_all = 1;
4009 if (!s->thread_args.log_complete)
4010 return trigger_log_thread(view, 0);
4011 err = log_move_cursor_down(view, s->commits->ncommits);
4012 s->thread_args.load_all = 0;
4023 err = log_move_cursor_down(view, nscroll);
4026 if (s->selected > view->nlines - 2)
4027 s->selected = view->nlines - 2;
4028 if (s->selected > s->commits->ncommits - 1)
4029 s->selected = s->commits->ncommits - 1;
4031 if (s->commits->ncommits < view->nlines - 1 &&
4032 !s->thread_args.log_complete) {
4033 s->thread_args.commits_needed += (view->nlines - 1) -
4034 s->commits->ncommits;
4035 err = trigger_log_thread(view, 1);
4041 if (s->selected_entry == NULL)
4043 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4047 if (s->selected_entry == NULL)
4049 err = view_request_new(new_view, view, TOG_VIEW_TREE);
4055 if (ch == KEY_BACKSPACE &&
4056 got_path_is_root_dir(s->in_repo_path))
4058 err = stop_log_thread(s);
4061 if (ch == KEY_BACKSPACE) {
4063 err = got_path_dirname(&parent_path, s->in_repo_path);
4066 free(s->in_repo_path);
4067 s->in_repo_path = parent_path;
4068 s->thread_args.in_repo_path = s->in_repo_path;
4069 } else if (ch == CTRL('l')) {
4070 struct got_object_id *start_id;
4071 err = got_repo_match_object_id(&start_id, NULL,
4072 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4073 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4075 if (s->head_ref_name == NULL ||
4076 err->code != GOT_ERR_NOT_REF)
4078 /* Try to cope with deleted references. */
4079 free(s->head_ref_name);
4080 s->head_ref_name = NULL;
4081 err = got_repo_match_object_id(&start_id,
4082 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4083 &tog_refs, s->repo);
4088 s->start_id = start_id;
4089 s->thread_args.start_id = s->start_id;
4091 s->log_branches = !s->log_branches;
4093 if (s->thread_args.pack_fds == NULL) {
4094 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4098 err = got_repo_open(&s->thread_args.repo,
4099 got_repo_get_path(s->repo), NULL,
4100 s->thread_args.pack_fds);
4104 err = tog_load_refs(s->repo, 0);
4107 err = got_commit_graph_open(&s->thread_args.graph,
4108 s->in_repo_path, !s->log_branches);
4111 err = got_commit_graph_iter_start(s->thread_args.graph,
4112 s->start_id, s->repo, NULL, NULL);
4115 free_commits(&s->real_commits);
4116 free_commits(&s->limit_commits);
4117 s->first_displayed_entry = NULL;
4118 s->last_displayed_entry = NULL;
4119 s->selected_entry = NULL;
4121 s->thread_args.log_complete = 0;
4123 s->thread_args.commits_needed = view->lines;
4124 s->matched_entry = NULL;
4125 s->search_entry = NULL;
4130 err = view_request_new(new_view, view, TOG_VIEW_REF);
4140 static const struct got_error *
4141 apply_unveil(const char *repo_path, const char *worktree_path)
4143 const struct got_error *error;
4146 if (unveil("gmon.out", "rwc") != 0)
4147 return got_error_from_errno2("unveil", "gmon.out");
4149 if (repo_path && unveil(repo_path, "r") != 0)
4150 return got_error_from_errno2("unveil", repo_path);
4152 if (worktree_path && unveil(worktree_path, "rwc") != 0)
4153 return got_error_from_errno2("unveil", worktree_path);
4155 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4156 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4158 error = got_privsep_unveil_exec_helpers();
4162 if (unveil(NULL, NULL) != 0)
4163 return got_error_from_errno("unveil");
4168 static const struct got_error *
4169 init_mock_term(const char *test_script_path)
4171 const struct got_error *err = NULL;
4173 if (test_script_path == NULL || *test_script_path == '\0')
4174 return got_error_msg(GOT_ERR_IO, "GOT_TOG_TEST not defined");
4176 tog_io.f = fopen(test_script_path, "re");
4177 if (tog_io.f == NULL) {
4178 err = got_error_from_errno_fmt("fopen: %s",
4183 /* test mode, we don't want any output */
4184 tog_io.cout = fopen("/dev/null", "w+");
4185 if (tog_io.cout == NULL) {
4186 err = got_error_from_errno("fopen: /dev/null");
4190 tog_io.cin = fopen("/dev/tty", "r+");
4191 if (tog_io.cin == NULL) {
4192 err = got_error_from_errno("fopen: /dev/tty");
4196 if (fseeko(tog_io.f, 0L, SEEK_SET) == -1) {
4197 err = got_error_from_errno("fseeko");
4201 /* use local TERM so we test in different environments */
4202 if (newterm(NULL, tog_io.cout, tog_io.cin) == NULL)
4203 err = got_error_msg(GOT_ERR_IO,
4204 "newterm: failed to initialise curses");
4217 * Override default signal handlers before starting ncurses.
4218 * This should prevent ncurses from installing its own
4219 * broken cleanup() signal handler.
4221 signal(SIGWINCH, tog_sigwinch);
4222 signal(SIGPIPE, tog_sigpipe);
4223 signal(SIGCONT, tog_sigcont);
4224 signal(SIGINT, tog_sigint);
4225 signal(SIGTERM, tog_sigterm);
4227 if (using_mock_io) /* In test mode we use a fake terminal */
4233 halfdelay(1); /* Fast refresh while initial view is loading. */
4236 intrflush(stdscr, FALSE);
4237 keypad(stdscr, TRUE);
4239 if (getenv("TOG_COLORS") != NULL) {
4241 use_default_colors();
4247 static const struct got_error *
4248 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4249 struct got_repository *repo, struct got_worktree *worktree)
4251 const struct got_error *err = NULL;
4254 *in_repo_path = strdup("/");
4255 if (*in_repo_path == NULL)
4256 return got_error_from_errno("strdup");
4261 const char *prefix = got_worktree_get_path_prefix(worktree);
4264 err = got_worktree_resolve_path(&p, worktree, argv[0]);
4267 if (asprintf(in_repo_path, "%s%s%s", prefix,
4268 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4270 err = got_error_from_errno("asprintf");
4271 *in_repo_path = NULL;
4275 err = got_repo_map_path(in_repo_path, repo, argv[0]);
4280 static const struct got_error *
4281 cmd_log(int argc, char *argv[])
4283 const struct got_error *error;
4284 struct got_repository *repo = NULL;
4285 struct got_worktree *worktree = NULL;
4286 struct got_object_id *start_id = NULL;
4287 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4288 char *start_commit = NULL, *label = NULL;
4289 struct got_reference *ref = NULL;
4290 const char *head_ref_name = NULL;
4291 int ch, log_branches = 0;
4292 struct tog_view *view;
4293 int *pack_fds = NULL;
4295 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4301 start_commit = optarg;
4304 repo_path = realpath(optarg, NULL);
4305 if (repo_path == NULL)
4306 return got_error_from_errno2("realpath",
4321 error = got_repo_pack_fds_open(&pack_fds);
4325 if (repo_path == NULL) {
4326 cwd = getcwd(NULL, 0);
4328 return got_error_from_errno("getcwd");
4329 error = got_worktree_open(&worktree, cwd);
4330 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4334 strdup(got_worktree_get_repo_path(worktree));
4336 repo_path = strdup(cwd);
4337 if (repo_path == NULL) {
4338 error = got_error_from_errno("strdup");
4343 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4347 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4354 error = apply_unveil(got_repo_get_path(repo),
4355 worktree ? got_worktree_get_root_path(worktree) : NULL);
4359 /* already loaded by tog_log_with_path()? */
4360 if (TAILQ_EMPTY(&tog_refs)) {
4361 error = tog_load_refs(repo, 0);
4366 if (start_commit == NULL) {
4367 error = got_repo_match_object_id(&start_id, &label,
4368 worktree ? got_worktree_get_head_ref_name(worktree) :
4369 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4372 head_ref_name = label;
4374 error = got_ref_open(&ref, repo, start_commit, 0);
4376 head_ref_name = got_ref_get_name(ref);
4377 else if (error->code != GOT_ERR_NOT_REF)
4379 error = got_repo_match_object_id(&start_id, NULL,
4380 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4385 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4387 error = got_error_from_errno("view_open");
4390 error = open_log_view(view, start_id, repo, head_ref_name,
4391 in_repo_path, log_branches);
4395 /* Release work tree lock. */
4396 got_worktree_close(worktree);
4399 error = view_loop(view);
4409 const struct got_error *close_err = got_repo_close(repo);
4414 got_worktree_close(worktree);
4416 const struct got_error *pack_err =
4417 got_repo_pack_fds_close(pack_fds);
4429 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4430 "object1 object2\n", getprogname());
4435 match_line(const char *line, regex_t *regex, size_t nmatch,
4436 regmatch_t *regmatch)
4438 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4441 static struct tog_color *
4442 match_color(struct tog_colors *colors, const char *line)
4444 struct tog_color *tc = NULL;
4446 STAILQ_FOREACH(tc, colors, entry) {
4447 if (match_line(line, &tc->regex, 0, NULL))
4454 static const struct got_error *
4455 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4456 WINDOW *window, int skipcol, regmatch_t *regmatch)
4458 const struct got_error *err = NULL;
4460 wchar_t *wline = NULL;
4461 int rme, rms, n, width, scrollx;
4462 int width0 = 0, width1 = 0, width2 = 0;
4463 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4467 rms = regmatch->rm_so;
4468 rme = regmatch->rm_eo;
4470 err = expand_tab(&exstr, line);
4474 /* Split the line into 3 segments, according to match offsets. */
4475 seg0 = strndup(exstr, rms);
4477 err = got_error_from_errno("strndup");
4480 seg1 = strndup(exstr + rms, rme - rms);
4482 err = got_error_from_errno("strndup");
4485 seg2 = strdup(exstr + rme);
4487 err = got_error_from_errno("strndup");
4491 /* draw up to matched token if we haven't scrolled past it */
4492 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4496 n = MAX(width0 - skipcol, 0);
4499 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4500 wlimit, col_tab_align, 1);
4503 waddwstr(window, &wline[scrollx]);
4513 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4517 wlen = wcslen(wline);
4519 width = wcwidth(wline[i]);
4521 /* should not happen, tabs are expanded */
4522 err = got_error(GOT_ERR_RANGE);
4525 if (width0 + w + width > skipcol)
4530 /* draw (visible part of) matched token (if scrolled into it) */
4531 if (width1 - w > 0) {
4532 wattron(window, A_STANDOUT);
4533 waddwstr(window, &wline[i]);
4534 wattroff(window, A_STANDOUT);
4535 wlimit -= (width1 - w);
4536 *wtotal += (width1 - w);
4540 if (wlimit > 0) { /* draw rest of line */
4542 if (skipcol > width0 + width1) {
4543 err = format_line(&wline, &width2, &scrollx, seg2,
4544 skipcol - (width0 + width1), wlimit,
4548 waddwstr(window, &wline[scrollx]);
4550 err = format_line(&wline, &width2, NULL, seg2, 0,
4551 wlimit, col_tab_align, 1);
4554 waddwstr(window, wline);
4568 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4571 int *eof, *first, *selected;
4573 if (view->type == TOG_VIEW_DIFF) {
4574 struct tog_diff_view_state *s = &view->state.diff;
4576 first = &s->first_displayed_line;
4580 } else if (view->type == TOG_VIEW_HELP) {
4581 struct tog_help_view_state *s = &view->state.help;
4583 first = &s->first_displayed_line;
4587 } else if (view->type == TOG_VIEW_BLAME) {
4588 struct tog_blame_view_state *s = &view->state.blame;
4590 first = &s->first_displayed_line;
4591 selected = &s->selected_line;
4597 /* Center gline in the middle of the page like vi(1). */
4598 if (*lineno < view->gline - (view->nlines - 3) / 2)
4600 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4609 *selected = view->gline <= (view->nlines - 3) / 2 ?
4610 view->gline : (view->nlines - 3) / 2 + 1;
4616 static const struct got_error *
4617 draw_file(struct tog_view *view, const char *header)
4619 struct tog_diff_view_state *s = &view->state.diff;
4620 regmatch_t *regmatch = &view->regmatch;
4621 const struct got_error *err;
4624 size_t linesize = 0;
4628 int max_lines = view->nlines;
4629 int nlines = s->nlines;
4632 s->lineno = s->first_displayed_line - 1;
4633 line_offset = s->lines[s->first_displayed_line - 1].offset;
4634 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4635 return got_error_from_errno("fseek");
4637 werase(view->window);
4639 if (view->gline > s->nlines - 1)
4640 view->gline = s->nlines - 1;
4643 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4644 1 : view->gline - (view->nlines - 3) / 2 :
4645 s->lineno + s->selected_line;
4647 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4648 return got_error_from_errno("asprintf");
4649 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4655 if (view_needs_focus_indication(view))
4656 wstandout(view->window);
4657 waddwstr(view->window, wline);
4660 while (width++ < view->ncols)
4661 waddch(view->window, ' ');
4662 if (view_needs_focus_indication(view))
4663 wstandend(view->window);
4673 while (max_lines > 0 && nprinted < max_lines) {
4674 enum got_diff_line_type linetype;
4677 linelen = getline(&line, &linesize, s->f);
4678 if (linelen == -1) {
4684 return got_ferror(s->f, GOT_ERR_IO);
4687 if (++s->lineno < s->first_displayed_line)
4689 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4691 if (s->lineno == view->hiline)
4694 /* Set view->maxx based on full line length. */
4695 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4701 view->maxx = MAX(view->maxx, width);
4705 linetype = s->lines[s->lineno].type;
4706 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4707 linetype < GOT_DIFF_LINE_CONTEXT)
4708 attr |= COLOR_PAIR(linetype);
4710 wattron(view->window, attr);
4711 if (s->first_displayed_line + nprinted == s->matched_line &&
4712 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4713 err = add_matched_line(&width, line, view->ncols, 0,
4714 view->window, view->x, regmatch);
4721 err = format_line(&wline, &width, &skip, line,
4722 view->x, view->ncols, 0, view->x ? 1 : 0);
4727 waddwstr(view->window, &wline[skip]);
4731 if (s->lineno == view->hiline) {
4732 /* highlight full gline length */
4733 while (width++ < view->ncols)
4734 waddch(view->window, ' ');
4736 if (width <= view->ncols - 1)
4737 waddch(view->window, '\n');
4740 wattroff(view->window, attr);
4741 if (++nprinted == 1)
4742 s->first_displayed_line = s->lineno;
4746 s->last_displayed_line = s->first_displayed_line +
4749 s->last_displayed_line = s->first_displayed_line;
4754 while (nprinted < view->nlines) {
4755 waddch(view->window, '\n');
4759 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4765 wstandout(view->window);
4766 waddwstr(view->window, wline);
4769 wstandend(view->window);
4776 get_datestr(time_t *time, char *datebuf)
4778 struct tm mytm, *tm;
4781 tm = gmtime_r(time, &mytm);
4784 s = asctime_r(tm, datebuf);
4787 p = strchr(s, '\n');
4793 static const struct got_error *
4794 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4795 off_t off, uint8_t type)
4797 struct got_diff_line *p;
4799 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4801 return got_error_from_errno("reallocarray");
4803 (*lines)[*nlines].offset = off;
4804 (*lines)[*nlines].type = type;
4810 static const struct got_error *
4811 cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4812 struct got_diff_line *s_lines, size_t s_nlines)
4814 struct got_diff_line *p;
4818 if (fseeko(src, 0L, SEEK_SET) == -1)
4819 return got_error_from_errno("fseeko");
4822 r = fread(buf, 1, sizeof(buf), src);
4825 return got_error_from_errno("fread");
4829 if (fwrite(buf, 1, r, dst) != r)
4830 return got_ferror(dst, GOT_ERR_IO);
4833 if (s_nlines == 0 && *d_nlines == 0)
4837 * If commit info was in dst, increment line offsets
4838 * of the appended diff content, but skip s_lines[0]
4839 * because offset zero is already in *d_lines.
4841 if (*d_nlines > 0) {
4842 for (i = 1; i < s_nlines; ++i)
4843 s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4851 p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4853 /* d_lines is freed in close_diff_view() */
4854 return got_error_from_errno("reallocarray");
4859 memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
4860 *d_nlines += s_nlines;
4865 static const struct got_error *
4866 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4867 struct got_object_id *commit_id, struct got_reflist_head *refs,
4868 struct got_repository *repo, int ignore_ws, int force_text_diff,
4869 struct got_diffstat_cb_arg *dsa, FILE *outfile)
4871 const struct got_error *err = NULL;
4872 char datebuf[26], *datestr;
4873 struct got_commit_object *commit;
4874 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4875 time_t committer_time;
4876 const char *author, *committer;
4877 char *refs_str = NULL;
4878 struct got_pathlist_entry *pe;
4883 err = build_refs_str(&refs_str, refs, commit_id, repo);
4888 err = got_object_open_as_commit(&commit, repo, commit_id);
4892 err = got_object_id_str(&id_str, commit_id);
4894 err = got_error_from_errno("got_object_id_str");
4898 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4902 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4903 refs_str ? refs_str : "", refs_str ? ")" : "");
4905 err = got_error_from_errno("fprintf");
4909 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4913 n = fprintf(outfile, "from: %s\n",
4914 got_object_commit_get_author(commit));
4916 err = got_error_from_errno("fprintf");
4920 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4924 author = got_object_commit_get_author(commit);
4925 committer = got_object_commit_get_committer(commit);
4926 if (strcmp(author, committer) != 0) {
4927 n = fprintf(outfile, "via: %s\n", committer);
4929 err = got_error_from_errno("fprintf");
4933 err = add_line_metadata(lines, nlines, outoff,
4934 GOT_DIFF_LINE_AUTHOR);
4938 committer_time = got_object_commit_get_committer_time(commit);
4939 datestr = get_datestr(&committer_time, datebuf);
4941 n = fprintf(outfile, "date: %s UTC\n", datestr);
4943 err = got_error_from_errno("fprintf");
4947 err = add_line_metadata(lines, nlines, outoff,
4948 GOT_DIFF_LINE_DATE);
4952 if (got_object_commit_get_nparents(commit) > 1) {
4953 const struct got_object_id_queue *parent_ids;
4954 struct got_object_qid *qid;
4956 parent_ids = got_object_commit_get_parent_ids(commit);
4957 STAILQ_FOREACH(qid, parent_ids, entry) {
4958 err = got_object_id_str(&id_str, &qid->id);
4961 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4963 err = got_error_from_errno("fprintf");
4967 err = add_line_metadata(lines, nlines, outoff,
4968 GOT_DIFF_LINE_META);
4976 err = got_object_commit_get_logmsg(&logmsg, commit);
4980 while ((line = strsep(&s, "\n")) != NULL) {
4981 n = fprintf(outfile, "%s\n", line);
4983 err = got_error_from_errno("fprintf");
4987 err = add_line_metadata(lines, nlines, outoff,
4988 GOT_DIFF_LINE_LOGMSG);
4993 TAILQ_FOREACH(pe, dsa->paths, entry) {
4994 struct got_diff_changed_path *cp = pe->data;
4995 int pad = dsa->max_path_len - pe->path_len + 1;
4997 n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
4998 pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
4999 dsa->rm_cols + 1, cp->rm);
5001 err = got_error_from_errno("fprintf");
5005 err = add_line_metadata(lines, nlines, outoff,
5006 GOT_DIFF_LINE_CHANGES);
5011 fputc('\n', outfile);
5013 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5017 n = fprintf(outfile,
5018 "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5019 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5020 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5022 err = got_error_from_errno("fprintf");
5026 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5030 fputc('\n', outfile);
5032 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5037 got_object_commit_close(commit);
5046 static const struct got_error *
5047 create_diff(struct tog_diff_view_state *s)
5049 const struct got_error *err = NULL;
5050 FILE *f = NULL, *tmp_diff_file = NULL;
5052 struct got_diff_line *lines = NULL;
5053 struct got_pathlist_head changed_paths;
5055 TAILQ_INIT(&changed_paths);
5058 s->lines = malloc(sizeof(*s->lines));
5059 if (s->lines == NULL)
5060 return got_error_from_errno("malloc");
5065 err = got_error_from_errno("got_opentemp");
5068 tmp_diff_file = got_opentemp();
5069 if (tmp_diff_file == NULL) {
5070 err = got_error_from_errno("got_opentemp");
5073 if (s->f && fclose(s->f) == EOF) {
5074 err = got_error_from_errno("fclose");
5080 err = got_object_get_type(&obj_type, s->repo, s->id1);
5082 err = got_object_get_type(&obj_type, s->repo, s->id2);
5087 case GOT_OBJ_TYPE_BLOB:
5088 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5089 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5090 s->label1, s->label2, tog_diff_algo, s->diff_context,
5091 s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5094 case GOT_OBJ_TYPE_TREE:
5095 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5096 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5097 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5098 s->force_text_diff, NULL, s->repo, s->f);
5100 case GOT_OBJ_TYPE_COMMIT: {
5101 const struct got_object_id_queue *parent_ids;
5102 struct got_object_qid *pid;
5103 struct got_commit_object *commit2;
5104 struct got_reflist_head *refs;
5106 struct got_diffstat_cb_arg dsa = {
5109 s->ignore_whitespace,
5114 lines = malloc(sizeof(*lines));
5115 if (lines == NULL) {
5116 err = got_error_from_errno("malloc");
5120 /* build diff first in tmp file then append to commit info */
5121 err = got_diff_objects_as_commits(&lines, &nlines,
5122 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5123 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5124 s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5128 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
5131 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5132 /* Show commit info if we're diffing to a parent/root commit. */
5133 if (s->id1 == NULL) {
5134 err = write_commit_info(&s->lines, &s->nlines, s->id2,
5135 refs, s->repo, s->ignore_whitespace,
5136 s->force_text_diff, &dsa, s->f);
5140 parent_ids = got_object_commit_get_parent_ids(commit2);
5141 STAILQ_FOREACH(pid, parent_ids, entry) {
5142 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5143 err = write_commit_info(&s->lines,
5144 &s->nlines, s->id2, refs, s->repo,
5145 s->ignore_whitespace,
5146 s->force_text_diff, &dsa, s->f);
5153 got_object_commit_close(commit2);
5155 err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5160 err = got_error(GOT_ERR_OBJ_TYPE);
5165 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5166 if (s->f && fflush(s->f) != 0 && err == NULL)
5167 err = got_error_from_errno("fflush");
5168 if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5169 err = got_error_from_errno("fclose");
5174 diff_view_indicate_progress(struct tog_view *view)
5176 mvwaddstr(view->window, 0, 0, "diffing...");
5181 static const struct got_error *
5182 search_start_diff_view(struct tog_view *view)
5184 struct tog_diff_view_state *s = &view->state.diff;
5186 s->matched_line = 0;
5191 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5192 size_t *nlines, int **first, int **last, int **match, int **selected)
5194 struct tog_diff_view_state *s = &view->state.diff;
5197 *nlines = s->nlines;
5198 *line_offsets = NULL;
5199 *match = &s->matched_line;
5200 *first = &s->first_displayed_line;
5201 *last = &s->last_displayed_line;
5202 *selected = &s->selected_line;
5205 static const struct got_error *
5206 search_next_view_match(struct tog_view *view)
5208 const struct got_error *err = NULL;
5212 size_t linesize = 0;
5214 off_t *line_offsets;
5216 int *first, *last, *match, *selected;
5218 if (!view->search_setup)
5219 return got_error_msg(GOT_ERR_NOT_IMPL,
5220 "view search not supported");
5221 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5224 if (!view->searching) {
5225 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5230 if (view->searching == TOG_SEARCH_FORWARD)
5231 lineno = *first + 1;
5233 lineno = *first - 1;
5235 lineno = *first - 1 + *selected;
5240 if (lineno <= 0 || lineno > nlines) {
5242 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5246 if (view->searching == TOG_SEARCH_FORWARD)
5252 offset = view->type == TOG_VIEW_DIFF ?
5253 view->state.diff.lines[lineno - 1].offset :
5254 line_offsets[lineno - 1];
5255 if (fseeko(f, offset, SEEK_SET) != 0) {
5257 return got_error_from_errno("fseeko");
5259 linelen = getline(&line, &linesize, f);
5260 if (linelen != -1) {
5262 err = expand_tab(&exstr, line);
5265 if (match_line(exstr, &view->regex, 1,
5267 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5274 if (view->searching == TOG_SEARCH_FORWARD)
5289 static const struct got_error *
5290 close_diff_view(struct tog_view *view)
5292 const struct got_error *err = NULL;
5293 struct tog_diff_view_state *s = &view->state.diff;
5299 if (s->f && fclose(s->f) == EOF)
5300 err = got_error_from_errno("fclose");
5302 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5303 err = got_error_from_errno("fclose");
5305 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5306 err = got_error_from_errno("fclose");
5308 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5309 err = got_error_from_errno("close");
5311 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5312 err = got_error_from_errno("close");
5320 static const struct got_error *
5321 open_diff_view(struct tog_view *view, struct got_object_id *id1,
5322 struct got_object_id *id2, const char *label1, const char *label2,
5323 int diff_context, int ignore_whitespace, int force_text_diff,
5324 struct tog_view *parent_view, struct got_repository *repo)
5326 const struct got_error *err;
5327 struct tog_diff_view_state *s = &view->state.diff;
5329 memset(s, 0, sizeof(*s));
5333 if (id1 != NULL && id2 != NULL) {
5336 err = got_object_get_type(&type1, repo, id1);
5339 err = got_object_get_type(&type2, repo, id2);
5343 if (type1 != type2) {
5344 err = got_error(GOT_ERR_OBJ_TYPE);
5348 s->first_displayed_line = 1;
5349 s->last_displayed_line = view->nlines;
5350 s->selected_line = 1;
5358 s->id1 = got_object_id_dup(id1);
5359 if (s->id1 == NULL) {
5360 err = got_error_from_errno("got_object_id_dup");
5366 s->id2 = got_object_id_dup(id2);
5367 if (s->id2 == NULL) {
5368 err = got_error_from_errno("got_object_id_dup");
5372 s->f1 = got_opentemp();
5373 if (s->f1 == NULL) {
5374 err = got_error_from_errno("got_opentemp");
5378 s->f2 = got_opentemp();
5379 if (s->f2 == NULL) {
5380 err = got_error_from_errno("got_opentemp");
5384 s->fd1 = got_opentempfd();
5386 err = got_error_from_errno("got_opentempfd");
5390 s->fd2 = got_opentempfd();
5392 err = got_error_from_errno("got_opentempfd");
5396 s->diff_context = diff_context;
5397 s->ignore_whitespace = ignore_whitespace;
5398 s->force_text_diff = force_text_diff;
5399 s->parent_view = parent_view;
5402 if (has_colors() && getenv("TOG_COLORS") != NULL && !using_mock_io) {
5405 rc = init_pair(GOT_DIFF_LINE_MINUS,
5406 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5408 rc = init_pair(GOT_DIFF_LINE_PLUS,
5409 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5411 rc = init_pair(GOT_DIFF_LINE_HUNK,
5412 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5414 rc = init_pair(GOT_DIFF_LINE_META,
5415 get_color_value("TOG_COLOR_DIFF_META"), -1);
5417 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5418 get_color_value("TOG_COLOR_DIFF_META"), -1);
5420 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5421 get_color_value("TOG_COLOR_DIFF_META"), -1);
5423 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5424 get_color_value("TOG_COLOR_DIFF_META"), -1);
5426 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5427 get_color_value("TOG_COLOR_AUTHOR"), -1);
5429 rc = init_pair(GOT_DIFF_LINE_DATE,
5430 get_color_value("TOG_COLOR_DATE"), -1);
5432 err = got_error(GOT_ERR_RANGE);
5437 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5438 view_is_splitscreen(view))
5439 show_log_view(parent_view); /* draw border */
5440 diff_view_indicate_progress(view);
5442 err = create_diff(s);
5444 view->show = show_diff_view;
5445 view->input = input_diff_view;
5446 view->reset = reset_diff_view;
5447 view->close = close_diff_view;
5448 view->search_start = search_start_diff_view;
5449 view->search_setup = search_setup_diff_view;
5450 view->search_next = search_next_view_match;
5453 if (view->close == NULL)
5454 close_diff_view(view);
5460 static const struct got_error *
5461 show_diff_view(struct tog_view *view)
5463 const struct got_error *err;
5464 struct tog_diff_view_state *s = &view->state.diff;
5465 char *id_str1 = NULL, *id_str2, *header;
5466 const char *label1, *label2;
5469 err = got_object_id_str(&id_str1, s->id1);
5472 label1 = s->label1 ? s->label1 : id_str1;
5474 label1 = "/dev/null";
5476 err = got_object_id_str(&id_str2, s->id2);
5479 label2 = s->label2 ? s->label2 : id_str2;
5481 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5482 err = got_error_from_errno("asprintf");
5490 err = draw_file(view, header);
5495 static const struct got_error *
5496 set_selected_commit(struct tog_diff_view_state *s,
5497 struct commit_queue_entry *entry)
5499 const struct got_error *err;
5500 const struct got_object_id_queue *parent_ids;
5501 struct got_commit_object *selected_commit;
5502 struct got_object_qid *pid;
5505 s->id2 = got_object_id_dup(entry->id);
5507 return got_error_from_errno("got_object_id_dup");
5509 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5512 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5514 pid = STAILQ_FIRST(parent_ids);
5515 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5516 got_object_commit_close(selected_commit);
5520 static const struct got_error *
5521 reset_diff_view(struct tog_view *view)
5523 struct tog_diff_view_state *s = &view->state.diff;
5526 wclear(view->window);
5527 s->first_displayed_line = 1;
5528 s->last_displayed_line = view->nlines;
5529 s->matched_line = 0;
5530 diff_view_indicate_progress(view);
5531 return create_diff(s);
5535 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5539 i = start = s->first_displayed_line - 1;
5541 while (s->lines[i].type != type) {
5545 return; /* do nothing, requested type not in file */
5548 s->selected_line = 1;
5549 s->first_displayed_line = i;
5553 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5557 i = start = s->first_displayed_line + 1;
5559 while (s->lines[i].type != type) {
5560 if (i == s->nlines - 1)
5563 return; /* do nothing, requested type not in file */
5566 s->selected_line = 1;
5567 s->first_displayed_line = i;
5570 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5572 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5575 static const struct got_error *
5576 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5578 const struct got_error *err = NULL;
5579 struct tog_diff_view_state *s = &view->state.diff;
5580 struct tog_log_view_state *ls;
5581 struct commit_queue_entry *old_selected_entry;
5583 size_t linesize = 0;
5585 int i, nscroll = view->nlines - 1, up = 0;
5587 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5596 horizontal_scroll_input(view, ch);
5601 s->force_text_diff = !s->force_text_diff;
5602 view->action = s->force_text_diff ?
5603 "force ASCII text enabled" :
5604 "force ASCII text disabled";
5606 else if (ch == 'w') {
5607 s->ignore_whitespace = !s->ignore_whitespace;
5608 view->action = s->ignore_whitespace ?
5609 "ignore whitespace enabled" :
5610 "ignore whitespace disabled";
5612 err = reset_diff_view(view);
5616 s->first_displayed_line = 1;
5625 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5631 if (s->first_displayed_line > 1)
5632 s->first_displayed_line--;
5643 if (s->first_displayed_line == 1) {
5648 while (i++ < nscroll && s->first_displayed_line > 1)
5649 s->first_displayed_line--;
5655 s->first_displayed_line++;
5672 while (!s->eof && i++ < nscroll) {
5673 linelen = getline(&line, &linesize, s->f);
5674 s->first_displayed_line++;
5675 if (linelen == -1) {
5679 err = got_ferror(s->f, GOT_ERR_IO);
5686 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5689 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5692 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5695 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5698 if (s->diff_context > 0) {
5700 s->matched_line = 0;
5701 diff_view_indicate_progress(view);
5702 err = create_diff(s);
5703 if (s->first_displayed_line + view->nlines - 1 >
5705 s->first_displayed_line = 1;
5706 s->last_displayed_line = view->nlines;
5712 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5714 s->matched_line = 0;
5715 diff_view_indicate_progress(view);
5716 err = create_diff(s);
5728 if (s->parent_view == NULL) {
5732 s->parent_view->count = view->count;
5734 if (s->parent_view->type == TOG_VIEW_LOG) {
5735 ls = &s->parent_view->state.log;
5736 old_selected_entry = ls->selected_entry;
5738 err = input_log_view(NULL, s->parent_view,
5739 up ? KEY_UP : KEY_DOWN);
5742 view->count = s->parent_view->count;
5744 if (old_selected_entry == ls->selected_entry)
5747 err = set_selected_commit(s, ls->selected_entry);
5750 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5751 struct tog_blame_view_state *bs;
5752 struct got_object_id *id, *prev_id;
5754 bs = &s->parent_view->state.blame;
5755 prev_id = get_annotation_for_line(bs->blame.lines,
5756 bs->blame.nlines, bs->last_diffed_line);
5758 err = input_blame_view(&view, s->parent_view,
5759 up ? KEY_UP : KEY_DOWN);
5762 view->count = s->parent_view->count;
5764 if (prev_id == NULL)
5766 id = get_selected_commit_id(bs->blame.lines,
5767 bs->blame.nlines, bs->first_displayed_line,
5772 if (!got_object_id_cmp(prev_id, id))
5775 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5779 s->first_displayed_line = 1;
5780 s->last_displayed_line = view->nlines;
5781 s->matched_line = 0;
5784 diff_view_indicate_progress(view);
5785 err = create_diff(s);
5795 static const struct got_error *
5796 cmd_diff(int argc, char *argv[])
5798 const struct got_error *error;
5799 struct got_repository *repo = NULL;
5800 struct got_worktree *worktree = NULL;
5801 struct got_object_id *id1 = NULL, *id2 = NULL;
5802 char *repo_path = NULL, *cwd = NULL;
5803 char *id_str1 = NULL, *id_str2 = NULL;
5804 char *label1 = NULL, *label2 = NULL;
5805 int diff_context = 3, ignore_whitespace = 0;
5806 int ch, force_text_diff = 0;
5808 struct tog_view *view;
5809 int *pack_fds = NULL;
5811 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5814 force_text_diff = 1;
5817 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5820 errx(1, "number of context lines is %s: %s",
5824 repo_path = realpath(optarg, NULL);
5825 if (repo_path == NULL)
5826 return got_error_from_errno2("realpath",
5828 got_path_strip_trailing_slashes(repo_path);
5831 ignore_whitespace = 1;
5843 usage_diff(); /* TODO show local worktree changes */
5844 } else if (argc == 2) {
5850 error = got_repo_pack_fds_open(&pack_fds);
5854 if (repo_path == NULL) {
5855 cwd = getcwd(NULL, 0);
5857 return got_error_from_errno("getcwd");
5858 error = got_worktree_open(&worktree, cwd);
5859 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5863 strdup(got_worktree_get_repo_path(worktree));
5865 repo_path = strdup(cwd);
5866 if (repo_path == NULL) {
5867 error = got_error_from_errno("strdup");
5872 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5878 error = apply_unveil(got_repo_get_path(repo), NULL);
5882 error = tog_load_refs(repo, 0);
5886 error = got_repo_match_object_id(&id1, &label1, id_str1,
5887 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5891 error = got_repo_match_object_id(&id2, &label2, id_str2,
5892 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5896 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5898 error = got_error_from_errno("view_open");
5901 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5902 ignore_whitespace, force_text_diff, NULL, repo);
5905 error = view_loop(view);
5912 const struct got_error *close_err = got_repo_close(repo);
5917 got_worktree_close(worktree);
5919 const struct got_error *pack_err =
5920 got_repo_pack_fds_close(pack_fds);
5933 "usage: %s blame [-c commit] [-r repository-path] path\n",
5938 struct tog_blame_line {
5940 struct got_object_id *id;
5943 static const struct got_error *
5944 draw_blame(struct tog_view *view)
5946 struct tog_blame_view_state *s = &view->state.blame;
5947 struct tog_blame *blame = &s->blame;
5948 regmatch_t *regmatch = &view->regmatch;
5949 const struct got_error *err;
5950 int lineno = 0, nprinted = 0;
5952 size_t linesize = 0;
5956 struct tog_blame_line *blame_line;
5957 struct got_object_id *prev_id = NULL;
5959 struct tog_color *tc;
5961 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5966 werase(view->window);
5968 if (asprintf(&line, "commit %s", id_str) == -1) {
5969 err = got_error_from_errno("asprintf");
5974 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5979 if (view_needs_focus_indication(view))
5980 wstandout(view->window);
5981 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5983 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5984 waddwstr(view->window, wline);
5985 while (width++ < view->ncols)
5986 waddch(view->window, ' ');
5988 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5989 if (view_needs_focus_indication(view))
5990 wstandend(view->window);
5994 if (view->gline > blame->nlines)
5995 view->gline = blame->nlines;
5997 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5998 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5999 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
6001 return got_error_from_errno("asprintf");
6004 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6009 waddwstr(view->window, wline);
6012 if (width < view->ncols - 1)
6013 waddch(view->window, '\n');
6017 while (nprinted < view->nlines - 2) {
6018 linelen = getline(&line, &linesize, blame->f);
6019 if (linelen == -1) {
6020 if (feof(blame->f)) {
6025 return got_ferror(blame->f, GOT_ERR_IO);
6027 if (++lineno < s->first_displayed_line)
6029 if (view->gline && !gotoline(view, &lineno, &nprinted))
6032 /* Set view->maxx based on full line length. */
6033 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6040 view->maxx = MAX(view->maxx, width);
6042 if (nprinted == s->selected_line - 1)
6043 wstandout(view->window);
6045 if (blame->nlines > 0) {
6046 blame_line = &blame->lines[lineno - 1];
6047 if (blame_line->annotated && prev_id &&
6048 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6049 !(nprinted == s->selected_line - 1)) {
6050 waddstr(view->window, " ");
6051 } else if (blame_line->annotated) {
6053 err = got_object_id_str(&id_str,
6059 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6061 wattr_on(view->window,
6062 COLOR_PAIR(tc->colorpair), NULL);
6063 wprintw(view->window, "%.8s", id_str);
6065 wattr_off(view->window,
6066 COLOR_PAIR(tc->colorpair), NULL);
6068 prev_id = blame_line->id;
6070 waddstr(view->window, "........");
6074 waddstr(view->window, "........");
6078 if (nprinted == s->selected_line - 1)
6079 wstandend(view->window);
6080 waddstr(view->window, " ");
6082 if (view->ncols <= 9) {
6084 } else if (s->first_displayed_line + nprinted ==
6086 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6087 err = add_matched_line(&width, line, view->ncols - 9, 9,
6088 view->window, view->x, regmatch);
6096 err = format_line(&wline, &width, &skip, line,
6097 view->x, view->ncols - 9, 9, 1);
6102 waddwstr(view->window, &wline[skip]);
6108 if (width <= view->ncols - 1)
6109 waddch(view->window, '\n');
6110 if (++nprinted == 1)
6111 s->first_displayed_line = lineno;
6114 s->last_displayed_line = lineno;
6121 static const struct got_error *
6122 blame_cb(void *arg, int nlines, int lineno,
6123 struct got_commit_object *commit, struct got_object_id *id)
6125 const struct got_error *err = NULL;
6126 struct tog_blame_cb_args *a = arg;
6127 struct tog_blame_line *line;
6130 if (nlines != a->nlines ||
6131 (lineno != -1 && lineno < 1) || lineno > a->nlines)
6132 return got_error(GOT_ERR_RANGE);
6134 errcode = pthread_mutex_lock(&tog_mutex);
6136 return got_error_set_errno(errcode, "pthread_mutex_lock");
6138 if (*a->quit) { /* user has quit the blame view */
6139 err = got_error(GOT_ERR_ITER_COMPLETED);
6144 goto done; /* no change in this commit */
6146 line = &a->lines[lineno - 1];
6147 if (line->annotated)
6150 line->id = got_object_id_dup(id);
6151 if (line->id == NULL) {
6152 err = got_error_from_errno("got_object_id_dup");
6155 line->annotated = 1;
6157 errcode = pthread_mutex_unlock(&tog_mutex);
6159 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6164 blame_thread(void *arg)
6166 const struct got_error *err, *close_err;
6167 struct tog_blame_thread_args *ta = arg;
6168 struct tog_blame_cb_args *a = ta->cb_args;
6169 int errcode, fd1 = -1, fd2 = -1;
6170 FILE *f1 = NULL, *f2 = NULL;
6172 fd1 = got_opentempfd();
6174 return (void *)got_error_from_errno("got_opentempfd");
6176 fd2 = got_opentempfd();
6178 err = got_error_from_errno("got_opentempfd");
6182 f1 = got_opentemp();
6184 err = (void *)got_error_from_errno("got_opentemp");
6187 f2 = got_opentemp();
6189 err = (void *)got_error_from_errno("got_opentemp");
6193 err = block_signals_used_by_main_thread();
6197 err = got_blame(ta->path, a->commit_id, ta->repo,
6198 tog_diff_algo, blame_cb, ta->cb_args,
6199 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6200 if (err && err->code == GOT_ERR_CANCELLED)
6203 errcode = pthread_mutex_lock(&tog_mutex);
6205 err = got_error_set_errno(errcode, "pthread_mutex_lock");
6209 close_err = got_repo_close(ta->repo);
6215 errcode = pthread_mutex_unlock(&tog_mutex);
6216 if (errcode && err == NULL)
6217 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6220 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6221 err = got_error_from_errno("close");
6222 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6223 err = got_error_from_errno("close");
6224 if (f1 && fclose(f1) == EOF && err == NULL)
6225 err = got_error_from_errno("fclose");
6226 if (f2 && fclose(f2) == EOF && err == NULL)
6227 err = got_error_from_errno("fclose");
6232 static struct got_object_id *
6233 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6234 int first_displayed_line, int selected_line)
6236 struct tog_blame_line *line;
6241 line = &lines[first_displayed_line - 1 + selected_line - 1];
6242 if (!line->annotated)
6248 static struct got_object_id *
6249 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6252 struct tog_blame_line *line;
6254 if (nlines <= 0 || lineno >= nlines)
6257 line = &lines[lineno - 1];
6258 if (!line->annotated)
6264 static const struct got_error *
6265 stop_blame(struct tog_blame *blame)
6267 const struct got_error *err = NULL;
6270 if (blame->thread) {
6272 errcode = pthread_mutex_unlock(&tog_mutex);
6274 return got_error_set_errno(errcode,
6275 "pthread_mutex_unlock");
6276 errcode = pthread_join(blame->thread, (void **)&err);
6278 return got_error_set_errno(errcode, "pthread_join");
6279 errcode = pthread_mutex_lock(&tog_mutex);
6281 return got_error_set_errno(errcode,
6282 "pthread_mutex_lock");
6283 if (err && err->code == GOT_ERR_ITER_COMPLETED)
6285 blame->thread = 0; //NULL;
6287 if (blame->thread_args.repo) {
6288 const struct got_error *close_err;
6289 close_err = got_repo_close(blame->thread_args.repo);
6292 blame->thread_args.repo = NULL;
6295 if (fclose(blame->f) == EOF && err == NULL)
6296 err = got_error_from_errno("fclose");
6300 for (i = 0; i < blame->nlines; i++)
6301 free(blame->lines[i].id);
6303 blame->lines = NULL;
6305 free(blame->cb_args.commit_id);
6306 blame->cb_args.commit_id = NULL;
6307 if (blame->pack_fds) {
6308 const struct got_error *pack_err =
6309 got_repo_pack_fds_close(blame->pack_fds);
6312 blame->pack_fds = NULL;
6317 static const struct got_error *
6318 cancel_blame_view(void *arg)
6320 const struct got_error *err = NULL;
6324 errcode = pthread_mutex_lock(&tog_mutex);
6326 return got_error_set_errno(errcode,
6327 "pthread_mutex_unlock");
6330 err = got_error(GOT_ERR_CANCELLED);
6332 errcode = pthread_mutex_unlock(&tog_mutex);
6334 return got_error_set_errno(errcode,
6335 "pthread_mutex_lock");
6340 static const struct got_error *
6341 run_blame(struct tog_view *view)
6343 struct tog_blame_view_state *s = &view->state.blame;
6344 struct tog_blame *blame = &s->blame;
6345 const struct got_error *err = NULL;
6346 struct got_commit_object *commit = NULL;
6347 struct got_blob_object *blob = NULL;
6348 struct got_repository *thread_repo = NULL;
6349 struct got_object_id *obj_id = NULL;
6350 int obj_type, fd = -1;
6351 int *pack_fds = NULL;
6353 err = got_object_open_as_commit(&commit, s->repo,
6354 &s->blamed_commit->id);
6358 fd = got_opentempfd();
6360 err = got_error_from_errno("got_opentempfd");
6364 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6368 err = got_object_get_type(&obj_type, s->repo, obj_id);
6372 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6373 err = got_error(GOT_ERR_OBJ_TYPE);
6377 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6380 blame->f = got_opentemp();
6381 if (blame->f == NULL) {
6382 err = got_error_from_errno("got_opentemp");
6385 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6386 &blame->line_offsets, blame->f, blob);
6389 if (blame->nlines == 0) {
6390 s->blame_complete = 1;
6394 /* Don't include \n at EOF in the blame line count. */
6395 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6398 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6399 if (blame->lines == NULL) {
6400 err = got_error_from_errno("calloc");
6404 err = got_repo_pack_fds_open(&pack_fds);
6407 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6412 blame->pack_fds = pack_fds;
6413 blame->cb_args.view = view;
6414 blame->cb_args.lines = blame->lines;
6415 blame->cb_args.nlines = blame->nlines;
6416 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6417 if (blame->cb_args.commit_id == NULL) {
6418 err = got_error_from_errno("got_object_id_dup");
6421 blame->cb_args.quit = &s->done;
6423 blame->thread_args.path = s->path;
6424 blame->thread_args.repo = thread_repo;
6425 blame->thread_args.cb_args = &blame->cb_args;
6426 blame->thread_args.complete = &s->blame_complete;
6427 blame->thread_args.cancel_cb = cancel_blame_view;
6428 blame->thread_args.cancel_arg = &s->done;
6429 s->blame_complete = 0;
6431 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6432 s->first_displayed_line = 1;
6433 s->last_displayed_line = view->nlines;
6434 s->selected_line = 1;
6436 s->matched_line = 0;
6440 got_object_commit_close(commit);
6441 if (fd != -1 && close(fd) == -1 && err == NULL)
6442 err = got_error_from_errno("close");
6444 got_object_blob_close(blob);
6451 static const struct got_error *
6452 open_blame_view(struct tog_view *view, char *path,
6453 struct got_object_id *commit_id, struct got_repository *repo)
6455 const struct got_error *err = NULL;
6456 struct tog_blame_view_state *s = &view->state.blame;
6458 STAILQ_INIT(&s->blamed_commits);
6460 s->path = strdup(path);
6461 if (s->path == NULL)
6462 return got_error_from_errno("strdup");
6464 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6470 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6471 s->first_displayed_line = 1;
6472 s->last_displayed_line = view->nlines;
6473 s->selected_line = 1;
6474 s->blame_complete = 0;
6476 s->commit_id = commit_id;
6477 memset(&s->blame, 0, sizeof(s->blame));
6479 STAILQ_INIT(&s->colors);
6480 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6481 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6482 get_color_value("TOG_COLOR_COMMIT"));
6487 view->show = show_blame_view;
6488 view->input = input_blame_view;
6489 view->reset = reset_blame_view;
6490 view->close = close_blame_view;
6491 view->search_start = search_start_blame_view;
6492 view->search_setup = search_setup_blame_view;
6493 view->search_next = search_next_view_match;
6495 return run_blame(view);
6498 static const struct got_error *
6499 close_blame_view(struct tog_view *view)
6501 const struct got_error *err = NULL;
6502 struct tog_blame_view_state *s = &view->state.blame;
6504 if (s->blame.thread)
6505 err = stop_blame(&s->blame);
6507 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6508 struct got_object_qid *blamed_commit;
6509 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6510 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6511 got_object_qid_free(blamed_commit);
6515 free_colors(&s->colors);
6519 static const struct got_error *
6520 search_start_blame_view(struct tog_view *view)
6522 struct tog_blame_view_state *s = &view->state.blame;
6524 s->matched_line = 0;
6529 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6530 size_t *nlines, int **first, int **last, int **match, int **selected)
6532 struct tog_blame_view_state *s = &view->state.blame;
6535 *nlines = s->blame.nlines;
6536 *line_offsets = s->blame.line_offsets;
6537 *match = &s->matched_line;
6538 *first = &s->first_displayed_line;
6539 *last = &s->last_displayed_line;
6540 *selected = &s->selected_line;
6543 static const struct got_error *
6544 show_blame_view(struct tog_view *view)
6546 const struct got_error *err = NULL;
6547 struct tog_blame_view_state *s = &view->state.blame;
6550 if (s->blame.thread == 0 && !s->blame_complete) {
6551 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6552 &s->blame.thread_args);
6554 return got_error_set_errno(errcode, "pthread_create");
6557 halfdelay(1); /* fast refresh while annotating */
6560 if (s->blame_complete && !using_mock_io)
6561 halfdelay(10); /* disable fast refresh */
6563 err = draw_blame(view);
6569 static const struct got_error *
6570 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6571 struct got_repository *repo, struct got_object_id *id)
6573 struct tog_view *log_view;
6574 const struct got_error *err = NULL;
6578 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6579 if (log_view == NULL)
6580 return got_error_from_errno("view_open");
6582 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6584 view_close(log_view);
6586 *new_view = log_view;
6591 static const struct got_error *
6592 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6594 const struct got_error *err = NULL, *thread_err = NULL;
6595 struct tog_view *diff_view;
6596 struct tog_blame_view_state *s = &view->state.blame;
6597 int eos, nscroll, begin_y = 0, begin_x = 0;
6599 eos = nscroll = view->nlines - 2;
6600 if (view_is_hsplit_top(view))
6610 horizontal_scroll_input(view, ch);
6617 s->selected_line = 1;
6618 s->first_displayed_line = 1;
6623 if (s->blame.nlines < eos) {
6624 s->selected_line = s->blame.nlines;
6625 s->first_displayed_line = 1;
6627 s->selected_line = eos;
6628 s->first_displayed_line = s->blame.nlines - (eos - 1);
6635 if (s->selected_line > 1)
6637 else if (s->selected_line == 1 &&
6638 s->first_displayed_line > 1)
6639 s->first_displayed_line--;
6650 if (s->first_displayed_line == 1) {
6651 if (view->count > 1)
6653 s->selected_line = MAX(1, s->selected_line - nscroll);
6657 if (s->first_displayed_line > nscroll)
6658 s->first_displayed_line -= nscroll;
6660 s->first_displayed_line = 1;
6665 if (s->selected_line < eos && s->first_displayed_line +
6666 s->selected_line <= s->blame.nlines)
6668 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6669 s->first_displayed_line++;
6675 struct got_object_id *id = NULL;
6678 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6679 s->first_displayed_line, s->selected_line);
6683 struct got_commit_object *commit, *pcommit;
6684 struct got_object_qid *pid;
6685 struct got_object_id *blob_id = NULL;
6687 err = got_object_open_as_commit(&commit,
6692 got_object_commit_get_parent_ids(commit));
6694 got_object_commit_close(commit);
6697 /* Check if path history ends here. */
6698 err = got_object_open_as_commit(&pcommit,
6702 err = got_object_id_by_path(&blob_id, s->repo,
6704 got_object_commit_close(pcommit);
6706 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6708 got_object_commit_close(commit);
6711 err = got_object_get_type(&obj_type, s->repo,
6714 /* Can't blame non-blob type objects. */
6715 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6716 got_object_commit_close(commit);
6719 err = got_object_qid_alloc(&s->blamed_commit,
6721 got_object_commit_close(commit);
6723 if (got_object_id_cmp(id,
6724 &s->blamed_commit->id) == 0)
6726 err = got_object_qid_alloc(&s->blamed_commit,
6732 thread_err = stop_blame(&s->blame);
6736 STAILQ_INSERT_HEAD(&s->blamed_commits,
6737 s->blamed_commit, entry);
6738 err = run_blame(view);
6744 struct got_object_qid *first;
6747 first = STAILQ_FIRST(&s->blamed_commits);
6748 if (!got_object_id_cmp(&first->id, s->commit_id))
6751 thread_err = stop_blame(&s->blame);
6755 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6756 got_object_qid_free(s->blamed_commit);
6758 STAILQ_FIRST(&s->blamed_commits);
6759 err = run_blame(view);
6766 s->id_to_log = get_selected_commit_id(s->blame.lines,
6767 s->blame.nlines, s->first_displayed_line, s->selected_line);
6769 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6773 struct got_object_id *id = NULL;
6774 struct got_object_qid *pid;
6775 struct got_commit_object *commit = NULL;
6778 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6779 s->first_displayed_line, s->selected_line);
6782 err = got_object_open_as_commit(&commit, s->repo, id);
6785 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6787 /* traversed from diff view, release diff resources */
6788 err = close_diff_view(*new_view);
6791 diff_view = *new_view;
6793 if (view_is_parent_view(view))
6794 view_get_split(view, &begin_y, &begin_x);
6796 diff_view = view_open(0, 0, begin_y, begin_x,
6798 if (diff_view == NULL) {
6799 got_object_commit_close(commit);
6800 err = got_error_from_errno("view_open");
6804 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6805 id, NULL, NULL, 3, 0, 0, view, s->repo);
6806 got_object_commit_close(commit);
6808 view_close(diff_view);
6811 s->last_diffed_line = s->first_displayed_line - 1 +
6814 break; /* still open from active diff view */
6815 if (view_is_parent_view(view) &&
6816 view->mode == TOG_VIEW_SPLIT_HRZN) {
6817 err = view_init_hsplit(view, begin_y);
6823 diff_view->focussed = 1;
6824 diff_view->mode = view->mode;
6825 diff_view->nlines = view->lines - begin_y;
6826 if (view_is_parent_view(view)) {
6827 view_transfer_size(diff_view, view);
6828 err = view_close_child(view);
6831 err = view_set_child(view, diff_view);
6834 view->focus_child = 1;
6836 *new_view = diff_view;
6849 if (s->last_displayed_line >= s->blame.nlines &&
6850 s->selected_line >= MIN(s->blame.nlines,
6851 view->nlines - 2)) {
6855 if (s->last_displayed_line >= s->blame.nlines &&
6856 s->selected_line < view->nlines - 2) {
6858 MIN(nscroll, s->last_displayed_line -
6859 s->first_displayed_line - s->selected_line + 1);
6861 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6862 s->first_displayed_line += nscroll;
6864 s->first_displayed_line =
6865 s->blame.nlines - (view->nlines - 3);
6868 if (s->selected_line > view->nlines - 2) {
6869 s->selected_line = MIN(s->blame.nlines,
6877 return thread_err ? thread_err : err;
6880 static const struct got_error *
6881 reset_blame_view(struct tog_view *view)
6883 const struct got_error *err;
6884 struct tog_blame_view_state *s = &view->state.blame;
6888 err = stop_blame(&s->blame);
6892 return run_blame(view);
6895 static const struct got_error *
6896 cmd_blame(int argc, char *argv[])
6898 const struct got_error *error;
6899 struct got_repository *repo = NULL;
6900 struct got_worktree *worktree = NULL;
6901 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6902 char *link_target = NULL;
6903 struct got_object_id *commit_id = NULL;
6904 struct got_commit_object *commit = NULL;
6905 char *commit_id_str = NULL;
6907 struct tog_view *view = NULL;
6908 int *pack_fds = NULL;
6910 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6913 commit_id_str = optarg;
6916 repo_path = realpath(optarg, NULL);
6917 if (repo_path == NULL)
6918 return got_error_from_errno2("realpath",
6933 error = got_repo_pack_fds_open(&pack_fds);
6937 if (repo_path == NULL) {
6938 cwd = getcwd(NULL, 0);
6940 return got_error_from_errno("getcwd");
6941 error = got_worktree_open(&worktree, cwd);
6942 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6946 strdup(got_worktree_get_repo_path(worktree));
6948 repo_path = strdup(cwd);
6949 if (repo_path == NULL) {
6950 error = got_error_from_errno("strdup");
6955 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6959 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6966 error = apply_unveil(got_repo_get_path(repo), NULL);
6970 error = tog_load_refs(repo, 0);
6974 if (commit_id_str == NULL) {
6975 struct got_reference *head_ref;
6976 error = got_ref_open(&head_ref, repo, worktree ?
6977 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6980 error = got_ref_resolve(&commit_id, repo, head_ref);
6981 got_ref_close(head_ref);
6983 error = got_repo_match_object_id(&commit_id, NULL,
6984 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6989 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6991 error = got_error_from_errno("view_open");
6995 error = got_object_open_as_commit(&commit, repo, commit_id);
6999 error = got_object_resolve_symlinks(&link_target, in_repo_path,
7004 error = open_blame_view(view, link_target ? link_target : in_repo_path,
7009 /* Release work tree lock. */
7010 got_worktree_close(worktree);
7013 error = view_loop(view);
7020 if (error != NULL && view != NULL) {
7021 if (view->close == NULL)
7022 close_blame_view(view);
7026 got_object_commit_close(commit);
7028 got_worktree_close(worktree);
7030 const struct got_error *close_err = got_repo_close(repo);
7035 const struct got_error *pack_err =
7036 got_repo_pack_fds_close(pack_fds);
7044 static const struct got_error *
7045 draw_tree_entries(struct tog_view *view, const char *parent_path)
7047 struct tog_tree_view_state *s = &view->state.tree;
7048 const struct got_error *err = NULL;
7049 struct got_tree_entry *te;
7052 struct tog_color *tc;
7053 int width, n, nentries, scrollx, i = 1;
7054 int limit = view->nlines;
7057 if (view_is_hsplit_top(view))
7058 --limit; /* border */
7060 werase(view->window);
7065 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7069 if (view_needs_focus_indication(view))
7070 wstandout(view->window);
7071 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7073 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7074 waddwstr(view->window, wline);
7077 while (width++ < view->ncols)
7078 waddch(view->window, ' ');
7080 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7081 if (view_needs_focus_indication(view))
7082 wstandend(view->window);
7087 if (s->first_displayed_entry) {
7088 i += got_tree_entry_get_index(s->first_displayed_entry);
7089 if (s->tree != s->root)
7090 ++i; /* account for ".." entry */
7092 nentries = got_object_tree_get_nentries(s->tree);
7093 if (asprintf(&index, "[%d/%d] %s",
7094 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7095 return got_error_from_errno("asprintf");
7096 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7100 waddwstr(view->window, wline);
7103 if (width < view->ncols - 1)
7104 waddch(view->window, '\n');
7107 waddch(view->window, '\n');
7111 if (s->first_displayed_entry == NULL) {
7112 te = got_object_tree_get_first_entry(s->tree);
7113 if (s->selected == 0) {
7115 wstandout(view->window);
7116 s->selected_entry = NULL;
7118 waddstr(view->window, " ..\n"); /* parent directory */
7119 if (s->selected == 0 && view->focussed)
7120 wstandend(view->window);
7127 te = s->first_displayed_entry;
7131 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7132 char *line = NULL, *id_str = NULL, *link_target = NULL;
7133 const char *modestr = "";
7136 te = got_object_tree_get_entry(s->tree, i);
7137 mode = got_tree_entry_get_mode(te);
7140 err = got_object_id_str(&id_str,
7141 got_tree_entry_get_id(te));
7143 return got_error_from_errno(
7144 "got_object_id_str");
7146 if (got_object_tree_entry_is_submodule(te))
7148 else if (S_ISLNK(mode)) {
7151 err = got_tree_entry_get_symlink_target(&link_target,
7157 for (i = 0; i < strlen(link_target); i++) {
7158 if (!isprint((unsigned char)link_target[i]))
7159 link_target[i] = '?';
7163 else if (S_ISDIR(mode))
7165 else if (mode & S_IXUSR)
7167 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7168 got_tree_entry_get_name(te), modestr,
7169 link_target ? " -> ": "",
7170 link_target ? link_target : "") == -1) {
7173 return got_error_from_errno("asprintf");
7178 /* use full line width to determine view->maxx */
7179 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7184 view->maxx = MAX(view->maxx, width);
7188 err = format_line(&wline, &width, &scrollx, line, view->x,
7194 if (n == s->selected) {
7196 wstandout(view->window);
7197 s->selected_entry = te;
7199 tc = match_color(&s->colors, line);
7201 wattr_on(view->window,
7202 COLOR_PAIR(tc->colorpair), NULL);
7203 waddwstr(view->window, &wline[scrollx]);
7205 wattr_off(view->window,
7206 COLOR_PAIR(tc->colorpair), NULL);
7207 if (width < view->ncols)
7208 waddch(view->window, '\n');
7209 if (n == s->selected && view->focussed)
7210 wstandend(view->window);
7216 s->last_displayed_entry = te;
7225 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7227 struct got_tree_entry *te;
7228 int isroot = s->tree == s->root;
7231 if (s->first_displayed_entry == NULL)
7234 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7235 while (i++ < maxscroll) {
7238 s->first_displayed_entry = NULL;
7241 s->first_displayed_entry = te;
7242 te = got_tree_entry_get_prev(s->tree, te);
7246 static const struct got_error *
7247 tree_scroll_down(struct tog_view *view, int maxscroll)
7249 struct tog_tree_view_state *s = &view->state.tree;
7250 struct got_tree_entry *next, *last;
7253 if (s->first_displayed_entry)
7254 next = got_tree_entry_get_next(s->tree,
7255 s->first_displayed_entry);
7257 next = got_object_tree_get_first_entry(s->tree);
7259 last = s->last_displayed_entry;
7260 while (next && n++ < maxscroll) {
7262 s->last_displayed_entry = last;
7263 last = got_tree_entry_get_next(s->tree, last);
7265 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7266 s->first_displayed_entry = next;
7267 next = got_tree_entry_get_next(s->tree, next);
7274 static const struct got_error *
7275 tree_entry_path(char **path, struct tog_parent_trees *parents,
7276 struct got_tree_entry *te)
7278 const struct got_error *err = NULL;
7279 struct tog_parent_tree *pt;
7280 size_t len = 2; /* for leading slash and NUL */
7282 TAILQ_FOREACH(pt, parents, entry)
7283 len += strlen(got_tree_entry_get_name(pt->selected_entry))
7286 len += strlen(got_tree_entry_get_name(te));
7288 *path = calloc(1, len);
7290 return got_error_from_errno("calloc");
7293 pt = TAILQ_LAST(parents, tog_parent_trees);
7295 const char *name = got_tree_entry_get_name(pt->selected_entry);
7296 if (strlcat(*path, name, len) >= len) {
7297 err = got_error(GOT_ERR_NO_SPACE);
7300 if (strlcat(*path, "/", len) >= len) {
7301 err = got_error(GOT_ERR_NO_SPACE);
7304 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7307 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7308 err = got_error(GOT_ERR_NO_SPACE);
7320 static const struct got_error *
7321 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7322 struct got_tree_entry *te, struct tog_parent_trees *parents,
7323 struct got_object_id *commit_id, struct got_repository *repo)
7325 const struct got_error *err = NULL;
7327 struct tog_view *blame_view;
7331 err = tree_entry_path(&path, parents, te);
7335 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7336 if (blame_view == NULL) {
7337 err = got_error_from_errno("view_open");
7341 err = open_blame_view(blame_view, path, commit_id, repo);
7343 if (err->code == GOT_ERR_CANCELLED)
7345 view_close(blame_view);
7347 *new_view = blame_view;
7353 static const struct got_error *
7354 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7355 struct tog_tree_view_state *s)
7357 struct tog_view *log_view;
7358 const struct got_error *err = NULL;
7363 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7364 if (log_view == NULL)
7365 return got_error_from_errno("view_open");
7367 err = tree_entry_path(&path, &s->parents, s->selected_entry);
7371 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7374 view_close(log_view);
7376 *new_view = log_view;
7381 static const struct got_error *
7382 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7383 const char *head_ref_name, struct got_repository *repo)
7385 const struct got_error *err = NULL;
7386 char *commit_id_str = NULL;
7387 struct tog_tree_view_state *s = &view->state.tree;
7388 struct got_commit_object *commit = NULL;
7390 TAILQ_INIT(&s->parents);
7391 STAILQ_INIT(&s->colors);
7393 s->commit_id = got_object_id_dup(commit_id);
7394 if (s->commit_id == NULL) {
7395 err = got_error_from_errno("got_object_id_dup");
7399 err = got_object_open_as_commit(&commit, repo, commit_id);
7404 * The root is opened here and will be closed when the view is closed.
7405 * Any visited subtrees and their path-wise parents are opened and
7408 err = got_object_open_as_tree(&s->root, repo,
7409 got_object_commit_get_tree_id(commit));
7414 err = got_object_id_str(&commit_id_str, commit_id);
7418 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7419 err = got_error_from_errno("asprintf");
7423 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7424 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7425 if (head_ref_name) {
7426 s->head_ref_name = strdup(head_ref_name);
7427 if (s->head_ref_name == NULL) {
7428 err = got_error_from_errno("strdup");
7434 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7435 err = add_color(&s->colors, "\\$$",
7436 TOG_COLOR_TREE_SUBMODULE,
7437 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7440 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7441 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7444 err = add_color(&s->colors, "/$",
7445 TOG_COLOR_TREE_DIRECTORY,
7446 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7450 err = add_color(&s->colors, "\\*$",
7451 TOG_COLOR_TREE_EXECUTABLE,
7452 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7456 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7457 get_color_value("TOG_COLOR_COMMIT"));
7462 view->show = show_tree_view;
7463 view->input = input_tree_view;
7464 view->close = close_tree_view;
7465 view->search_start = search_start_tree_view;
7466 view->search_next = search_next_tree_view;
7468 free(commit_id_str);
7470 got_object_commit_close(commit);
7472 if (view->close == NULL)
7473 close_tree_view(view);
7479 static const struct got_error *
7480 close_tree_view(struct tog_view *view)
7482 struct tog_tree_view_state *s = &view->state.tree;
7484 free_colors(&s->colors);
7485 free(s->tree_label);
7486 s->tree_label = NULL;
7488 s->commit_id = NULL;
7489 free(s->head_ref_name);
7490 s->head_ref_name = NULL;
7491 while (!TAILQ_EMPTY(&s->parents)) {
7492 struct tog_parent_tree *parent;
7493 parent = TAILQ_FIRST(&s->parents);
7494 TAILQ_REMOVE(&s->parents, parent, entry);
7495 if (parent->tree != s->root)
7496 got_object_tree_close(parent->tree);
7500 if (s->tree != NULL && s->tree != s->root)
7501 got_object_tree_close(s->tree);
7503 got_object_tree_close(s->root);
7507 static const struct got_error *
7508 search_start_tree_view(struct tog_view *view)
7510 struct tog_tree_view_state *s = &view->state.tree;
7512 s->matched_entry = NULL;
7517 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7519 regmatch_t regmatch;
7521 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7525 static const struct got_error *
7526 search_next_tree_view(struct tog_view *view)
7528 struct tog_tree_view_state *s = &view->state.tree;
7529 struct got_tree_entry *te = NULL;
7531 if (!view->searching) {
7532 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7536 if (s->matched_entry) {
7537 if (view->searching == TOG_SEARCH_FORWARD) {
7538 if (s->selected_entry)
7539 te = got_tree_entry_get_next(s->tree,
7542 te = got_object_tree_get_first_entry(s->tree);
7544 if (s->selected_entry == NULL)
7545 te = got_object_tree_get_last_entry(s->tree);
7547 te = got_tree_entry_get_prev(s->tree,
7551 if (s->selected_entry)
7552 te = s->selected_entry;
7553 else if (view->searching == TOG_SEARCH_FORWARD)
7554 te = got_object_tree_get_first_entry(s->tree);
7556 te = got_object_tree_get_last_entry(s->tree);
7561 if (s->matched_entry == NULL) {
7562 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7565 if (view->searching == TOG_SEARCH_FORWARD)
7566 te = got_object_tree_get_first_entry(s->tree);
7568 te = got_object_tree_get_last_entry(s->tree);
7571 if (match_tree_entry(te, &view->regex)) {
7572 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7573 s->matched_entry = te;
7577 if (view->searching == TOG_SEARCH_FORWARD)
7578 te = got_tree_entry_get_next(s->tree, te);
7580 te = got_tree_entry_get_prev(s->tree, te);
7583 if (s->matched_entry) {
7584 s->first_displayed_entry = s->matched_entry;
7591 static const struct got_error *
7592 show_tree_view(struct tog_view *view)
7594 const struct got_error *err = NULL;
7595 struct tog_tree_view_state *s = &view->state.tree;
7598 err = tree_entry_path(&parent_path, &s->parents, NULL);
7602 err = draw_tree_entries(view, parent_path);
7609 static const struct got_error *
7610 tree_goto_line(struct tog_view *view, int nlines)
7612 const struct got_error *err = NULL;
7613 struct tog_tree_view_state *s = &view->state.tree;
7614 struct got_tree_entry **fte, **lte, **ste;
7615 int g, last, first = 1, i = 1;
7616 int root = s->tree == s->root;
7617 int off = root ? 1 : 2;
7624 else if (g > got_object_tree_get_nentries(s->tree))
7625 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7627 fte = &s->first_displayed_entry;
7628 lte = &s->last_displayed_entry;
7629 ste = &s->selected_entry;
7632 first = got_tree_entry_get_index(*fte);
7633 first += off; /* account for ".." */
7635 last = got_tree_entry_get_index(*lte);
7638 if (g >= first && g <= last && g - first < nlines) {
7639 s->selected = g - first;
7640 return NULL; /* gline is on the current page */
7644 i = got_tree_entry_get_index(*ste);
7649 err = tree_scroll_down(view, g - i);
7652 if (got_tree_entry_get_index(*lte) >=
7653 got_object_tree_get_nentries(s->tree) - 1 &&
7654 first + s->selected < g &&
7655 s->selected < s->ndisplayed - 1) {
7656 first = got_tree_entry_get_index(*fte);
7658 s->selected = g - first;
7661 tree_scroll_up(s, i - g);
7664 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7665 s->selected = g - 1;
7670 static const struct got_error *
7671 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7673 const struct got_error *err = NULL;
7674 struct tog_tree_view_state *s = &view->state.tree;
7675 struct got_tree_entry *te;
7676 int n, nscroll = view->nlines - 3;
7679 return tree_goto_line(view, nscroll);
7688 horizontal_scroll_input(view, ch);
7691 s->show_ids = !s->show_ids;
7696 if (!s->selected_entry)
7698 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7702 err = view_request_new(new_view, view, TOG_VIEW_REF);
7709 if (s->tree == s->root)
7710 s->first_displayed_entry =
7711 got_object_tree_get_first_entry(s->tree);
7713 s->first_displayed_entry = NULL;
7718 int eos = view->nlines - 3;
7720 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7724 te = got_object_tree_get_last_entry(s->tree);
7725 for (n = 0; n < eos; n++) {
7727 if (s->tree != s->root) {
7728 s->first_displayed_entry = NULL;
7733 s->first_displayed_entry = te;
7734 te = got_tree_entry_get_prev(s->tree, te);
7737 s->selected = n - 1;
7743 if (s->selected > 0) {
7747 tree_scroll_up(s, 1);
7748 if (s->selected_entry == NULL ||
7749 (s->tree == s->root && s->selected_entry ==
7750 got_object_tree_get_first_entry(s->tree)))
7760 if (s->tree == s->root) {
7761 if (got_object_tree_get_first_entry(s->tree) ==
7762 s->first_displayed_entry)
7763 s->selected -= MIN(s->selected, nscroll);
7765 if (s->first_displayed_entry == NULL)
7766 s->selected -= MIN(s->selected, nscroll);
7768 tree_scroll_up(s, MAX(0, nscroll));
7769 if (s->selected_entry == NULL ||
7770 (s->tree == s->root && s->selected_entry ==
7771 got_object_tree_get_first_entry(s->tree)))
7777 if (s->selected < s->ndisplayed - 1) {
7781 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7783 /* can't scroll any further */
7787 tree_scroll_down(view, 1);
7797 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7799 /* can't scroll any further; move cursor down */
7800 if (s->selected < s->ndisplayed - 1)
7801 s->selected += MIN(nscroll,
7802 s->ndisplayed - s->selected - 1);
7807 tree_scroll_down(view, nscroll);
7812 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7813 struct tog_parent_tree *parent;
7814 /* user selected '..' */
7815 if (s->tree == s->root) {
7819 parent = TAILQ_FIRST(&s->parents);
7820 TAILQ_REMOVE(&s->parents, parent,
7822 got_object_tree_close(s->tree);
7823 s->tree = parent->tree;
7824 s->first_displayed_entry =
7825 parent->first_displayed_entry;
7827 parent->selected_entry;
7828 s->selected = parent->selected;
7829 if (s->selected > view->nlines - 3) {
7830 err = offset_selection_down(view);
7835 } else if (S_ISDIR(got_tree_entry_get_mode(
7836 s->selected_entry))) {
7837 struct got_tree_object *subtree;
7839 err = got_object_open_as_tree(&subtree, s->repo,
7840 got_tree_entry_get_id(s->selected_entry));
7843 err = tree_view_visit_subtree(s, subtree);
7845 got_object_tree_close(subtree);
7848 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7849 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7852 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7853 s->selected = view->nlines - 4;
7869 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7874 static const struct got_error *
7875 cmd_tree(int argc, char *argv[])
7877 const struct got_error *error;
7878 struct got_repository *repo = NULL;
7879 struct got_worktree *worktree = NULL;
7880 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7881 struct got_object_id *commit_id = NULL;
7882 struct got_commit_object *commit = NULL;
7883 const char *commit_id_arg = NULL;
7885 struct got_reference *ref = NULL;
7886 const char *head_ref_name = NULL;
7888 struct tog_view *view;
7889 int *pack_fds = NULL;
7891 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7894 commit_id_arg = optarg;
7897 repo_path = realpath(optarg, NULL);
7898 if (repo_path == NULL)
7899 return got_error_from_errno2("realpath",
7914 error = got_repo_pack_fds_open(&pack_fds);
7918 if (repo_path == NULL) {
7919 cwd = getcwd(NULL, 0);
7921 return got_error_from_errno("getcwd");
7922 error = got_worktree_open(&worktree, cwd);
7923 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7927 strdup(got_worktree_get_repo_path(worktree));
7929 repo_path = strdup(cwd);
7930 if (repo_path == NULL) {
7931 error = got_error_from_errno("strdup");
7936 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7940 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7947 error = apply_unveil(got_repo_get_path(repo), NULL);
7951 error = tog_load_refs(repo, 0);
7955 if (commit_id_arg == NULL) {
7956 error = got_repo_match_object_id(&commit_id, &label,
7957 worktree ? got_worktree_get_head_ref_name(worktree) :
7958 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7961 head_ref_name = label;
7963 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7965 head_ref_name = got_ref_get_name(ref);
7966 else if (error->code != GOT_ERR_NOT_REF)
7968 error = got_repo_match_object_id(&commit_id, NULL,
7969 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7974 error = got_object_open_as_commit(&commit, repo, commit_id);
7978 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7980 error = got_error_from_errno("view_open");
7983 error = open_tree_view(view, commit_id, head_ref_name, repo);
7986 if (!got_path_is_root_dir(in_repo_path)) {
7987 error = tree_view_walk_path(&view->state.tree, commit,
7994 /* Release work tree lock. */
7995 got_worktree_close(worktree);
7998 error = view_loop(view);
8007 const struct got_error *close_err = got_repo_close(repo);
8012 const struct got_error *pack_err =
8013 got_repo_pack_fds_close(pack_fds);
8021 static const struct got_error *
8022 ref_view_load_refs(struct tog_ref_view_state *s)
8024 struct got_reflist_entry *sre;
8025 struct tog_reflist_entry *re;
8028 TAILQ_FOREACH(sre, &tog_refs, entry) {
8029 if (strncmp(got_ref_get_name(sre->ref),
8030 "refs/got/", 9) == 0 &&
8031 strncmp(got_ref_get_name(sre->ref),
8032 "refs/got/backup/", 16) != 0)
8035 re = malloc(sizeof(*re));
8037 return got_error_from_errno("malloc");
8039 re->ref = got_ref_dup(sre->ref);
8040 if (re->ref == NULL)
8041 return got_error_from_errno("got_ref_dup");
8042 re->idx = s->nrefs++;
8043 TAILQ_INSERT_TAIL(&s->refs, re, entry);
8046 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8051 ref_view_free_refs(struct tog_ref_view_state *s)
8053 struct tog_reflist_entry *re;
8055 while (!TAILQ_EMPTY(&s->refs)) {
8056 re = TAILQ_FIRST(&s->refs);
8057 TAILQ_REMOVE(&s->refs, re, entry);
8058 got_ref_close(re->ref);
8063 static const struct got_error *
8064 open_ref_view(struct tog_view *view, struct got_repository *repo)
8066 const struct got_error *err = NULL;
8067 struct tog_ref_view_state *s = &view->state.ref;
8069 s->selected_entry = 0;
8072 TAILQ_INIT(&s->refs);
8073 STAILQ_INIT(&s->colors);
8075 err = ref_view_load_refs(s);
8079 if (has_colors() && getenv("TOG_COLORS") != NULL) {
8080 err = add_color(&s->colors, "^refs/heads/",
8081 TOG_COLOR_REFS_HEADS,
8082 get_color_value("TOG_COLOR_REFS_HEADS"));
8086 err = add_color(&s->colors, "^refs/tags/",
8087 TOG_COLOR_REFS_TAGS,
8088 get_color_value("TOG_COLOR_REFS_TAGS"));
8092 err = add_color(&s->colors, "^refs/remotes/",
8093 TOG_COLOR_REFS_REMOTES,
8094 get_color_value("TOG_COLOR_REFS_REMOTES"));
8098 err = add_color(&s->colors, "^refs/got/backup/",
8099 TOG_COLOR_REFS_BACKUP,
8100 get_color_value("TOG_COLOR_REFS_BACKUP"));
8105 view->show = show_ref_view;
8106 view->input = input_ref_view;
8107 view->close = close_ref_view;
8108 view->search_start = search_start_ref_view;
8109 view->search_next = search_next_ref_view;
8112 if (view->close == NULL)
8113 close_ref_view(view);
8119 static const struct got_error *
8120 close_ref_view(struct tog_view *view)
8122 struct tog_ref_view_state *s = &view->state.ref;
8124 ref_view_free_refs(s);
8125 free_colors(&s->colors);
8130 static const struct got_error *
8131 resolve_reflist_entry(struct got_object_id **commit_id,
8132 struct tog_reflist_entry *re, struct got_repository *repo)
8134 const struct got_error *err = NULL;
8135 struct got_object_id *obj_id;
8136 struct got_tag_object *tag = NULL;
8141 err = got_ref_resolve(&obj_id, repo, re->ref);
8145 err = got_object_get_type(&obj_type, repo, obj_id);
8150 case GOT_OBJ_TYPE_COMMIT:
8151 *commit_id = obj_id;
8153 case GOT_OBJ_TYPE_TAG:
8154 err = got_object_open_as_tag(&tag, repo, obj_id);
8158 err = got_object_get_type(&obj_type, repo,
8159 got_object_tag_get_object_id(tag));
8162 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
8163 err = got_error(GOT_ERR_OBJ_TYPE);
8166 *commit_id = got_object_id_dup(
8167 got_object_tag_get_object_id(tag));
8168 if (*commit_id == NULL) {
8169 err = got_error_from_errno("got_object_id_dup");
8174 err = got_error(GOT_ERR_OBJ_TYPE);
8180 got_object_tag_close(tag);
8188 static const struct got_error *
8189 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
8190 struct tog_reflist_entry *re, struct got_repository *repo)
8192 struct tog_view *log_view;
8193 const struct got_error *err = NULL;
8194 struct got_object_id *commit_id = NULL;
8198 err = resolve_reflist_entry(&commit_id, re, repo);
8200 if (err->code != GOT_ERR_OBJ_TYPE)
8206 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
8207 if (log_view == NULL) {
8208 err = got_error_from_errno("view_open");
8212 err = open_log_view(log_view, commit_id, repo,
8213 got_ref_get_name(re->ref), "", 0);
8216 view_close(log_view);
8218 *new_view = log_view;
8224 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
8226 struct tog_reflist_entry *re;
8229 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8232 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
8233 while (i++ < maxscroll) {
8236 s->first_displayed_entry = re;
8237 re = TAILQ_PREV(re, tog_reflist_head, entry);
8241 static const struct got_error *
8242 ref_scroll_down(struct tog_view *view, int maxscroll)
8244 struct tog_ref_view_state *s = &view->state.ref;
8245 struct tog_reflist_entry *next, *last;
8248 if (s->first_displayed_entry)
8249 next = TAILQ_NEXT(s->first_displayed_entry, entry);
8251 next = TAILQ_FIRST(&s->refs);
8253 last = s->last_displayed_entry;
8254 while (next && n++ < maxscroll) {
8256 s->last_displayed_entry = last;
8257 last = TAILQ_NEXT(last, entry);
8259 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
8260 s->first_displayed_entry = next;
8261 next = TAILQ_NEXT(next, entry);
8268 static const struct got_error *
8269 search_start_ref_view(struct tog_view *view)
8271 struct tog_ref_view_state *s = &view->state.ref;
8273 s->matched_entry = NULL;
8278 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8280 regmatch_t regmatch;
8282 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8286 static const struct got_error *
8287 search_next_ref_view(struct tog_view *view)
8289 struct tog_ref_view_state *s = &view->state.ref;
8290 struct tog_reflist_entry *re = NULL;
8292 if (!view->searching) {
8293 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8297 if (s->matched_entry) {
8298 if (view->searching == TOG_SEARCH_FORWARD) {
8299 if (s->selected_entry)
8300 re = TAILQ_NEXT(s->selected_entry, entry);
8302 re = TAILQ_PREV(s->selected_entry,
8303 tog_reflist_head, entry);
8305 if (s->selected_entry == NULL)
8306 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8308 re = TAILQ_PREV(s->selected_entry,
8309 tog_reflist_head, entry);
8312 if (s->selected_entry)
8313 re = s->selected_entry;
8314 else if (view->searching == TOG_SEARCH_FORWARD)
8315 re = TAILQ_FIRST(&s->refs);
8317 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8322 if (s->matched_entry == NULL) {
8323 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8326 if (view->searching == TOG_SEARCH_FORWARD)
8327 re = TAILQ_FIRST(&s->refs);
8329 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8332 if (match_reflist_entry(re, &view->regex)) {
8333 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8334 s->matched_entry = re;
8338 if (view->searching == TOG_SEARCH_FORWARD)
8339 re = TAILQ_NEXT(re, entry);
8341 re = TAILQ_PREV(re, tog_reflist_head, entry);
8344 if (s->matched_entry) {
8345 s->first_displayed_entry = s->matched_entry;
8352 static const struct got_error *
8353 show_ref_view(struct tog_view *view)
8355 const struct got_error *err = NULL;
8356 struct tog_ref_view_state *s = &view->state.ref;
8357 struct tog_reflist_entry *re;
8360 struct tog_color *tc;
8361 int width, n, scrollx;
8362 int limit = view->nlines;
8364 werase(view->window);
8367 if (view_is_hsplit_top(view))
8368 --limit; /* border */
8373 re = s->first_displayed_entry;
8375 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8377 return got_error_from_errno("asprintf");
8379 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8384 if (view_needs_focus_indication(view))
8385 wstandout(view->window);
8386 waddwstr(view->window, wline);
8387 while (width++ < view->ncols)
8388 waddch(view->window, ' ');
8389 if (view_needs_focus_indication(view))
8390 wstandend(view->window);
8400 while (re && limit > 0) {
8402 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8405 struct got_commit_object *ci;
8406 struct got_tag_object *tag;
8407 struct got_object_id *id;
8411 err = got_ref_resolve(&id, s->repo, re->ref);
8414 err = got_object_open_as_tag(&tag, s->repo, id);
8416 if (err->code != GOT_ERR_OBJ_TYPE) {
8420 err = got_object_open_as_commit(&ci, s->repo,
8426 t = got_object_commit_get_committer_time(ci);
8427 got_object_commit_close(ci);
8429 t = got_object_tag_get_tagger_time(tag);
8430 got_object_tag_close(tag);
8433 if (gmtime_r(&t, &tm) == NULL)
8434 return got_error_from_errno("gmtime_r");
8435 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8436 return got_error(GOT_ERR_NO_SPACE);
8438 if (got_ref_is_symbolic(re->ref)) {
8439 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8440 ymd : "", got_ref_get_name(re->ref),
8441 got_ref_get_symref_target(re->ref)) == -1)
8442 return got_error_from_errno("asprintf");
8443 } else if (s->show_ids) {
8444 struct got_object_id *id;
8446 err = got_ref_resolve(&id, s->repo, re->ref);
8449 err = got_object_id_str(&id_str, id);
8454 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8455 got_ref_get_name(re->ref), id_str) == -1) {
8456 err = got_error_from_errno("asprintf");
8463 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8464 got_ref_get_name(re->ref)) == -1)
8465 return got_error_from_errno("asprintf");
8467 /* use full line width to determine view->maxx */
8468 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8473 view->maxx = MAX(view->maxx, width);
8477 err = format_line(&wline, &width, &scrollx, line, view->x,
8483 if (n == s->selected) {
8485 wstandout(view->window);
8486 s->selected_entry = re;
8488 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8490 wattr_on(view->window,
8491 COLOR_PAIR(tc->colorpair), NULL);
8492 waddwstr(view->window, &wline[scrollx]);
8494 wattr_off(view->window,
8495 COLOR_PAIR(tc->colorpair), NULL);
8496 if (width < view->ncols)
8497 waddch(view->window, '\n');
8498 if (n == s->selected && view->focussed)
8499 wstandend(view->window);
8505 s->last_displayed_entry = re;
8508 re = TAILQ_NEXT(re, entry);
8515 static const struct got_error *
8516 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8517 struct tog_reflist_entry *re, struct got_repository *repo)
8519 const struct got_error *err = NULL;
8520 struct got_object_id *commit_id = NULL;
8521 struct tog_view *tree_view;
8525 err = resolve_reflist_entry(&commit_id, re, repo);
8527 if (err->code != GOT_ERR_OBJ_TYPE)
8534 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8535 if (tree_view == NULL) {
8536 err = got_error_from_errno("view_open");
8540 err = open_tree_view(tree_view, commit_id,
8541 got_ref_get_name(re->ref), repo);
8545 *new_view = tree_view;
8551 static const struct got_error *
8552 ref_goto_line(struct tog_view *view, int nlines)
8554 const struct got_error *err = NULL;
8555 struct tog_ref_view_state *s = &view->state.ref;
8556 int g, idx = s->selected_entry->idx;
8563 else if (g > s->nrefs)
8566 if (g >= s->first_displayed_entry->idx + 1 &&
8567 g <= s->last_displayed_entry->idx + 1 &&
8568 g - s->first_displayed_entry->idx - 1 < nlines) {
8569 s->selected = g - s->first_displayed_entry->idx - 1;
8574 err = ref_scroll_down(view, g - idx - 1);
8577 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8578 s->first_displayed_entry->idx + s->selected < g &&
8579 s->selected < s->ndisplayed - 1)
8580 s->selected = g - s->first_displayed_entry->idx - 1;
8581 } else if (idx + 1 > g)
8582 ref_scroll_up(s, idx - g + 1);
8584 if (g < nlines && s->first_displayed_entry->idx == 0)
8585 s->selected = g - 1;
8591 static const struct got_error *
8592 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8594 const struct got_error *err = NULL;
8595 struct tog_ref_view_state *s = &view->state.ref;
8596 struct tog_reflist_entry *re;
8597 int n, nscroll = view->nlines - 1;
8600 return ref_goto_line(view, nscroll);
8609 horizontal_scroll_input(view, ch);
8612 s->show_ids = !s->show_ids;
8616 s->show_date = !s->show_date;
8620 s->sort_by_date = !s->sort_by_date;
8621 view->action = s->sort_by_date ? "sort by date" : "sort by name";
8623 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8624 got_ref_cmp_by_commit_timestamp_descending :
8625 tog_ref_cmp_by_name, s->repo);
8628 got_reflist_object_id_map_free(tog_refs_idmap);
8629 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8630 &tog_refs, s->repo);
8633 ref_view_free_refs(s);
8634 err = ref_view_load_refs(s);
8639 if (!s->selected_entry)
8641 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8645 if (!s->selected_entry)
8647 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8654 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8659 int eos = view->nlines - 1;
8661 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8665 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8666 for (n = 0; n < eos; n++) {
8669 s->first_displayed_entry = re;
8670 re = TAILQ_PREV(re, tog_reflist_head, entry);
8673 s->selected = n - 1;
8679 if (s->selected > 0) {
8683 ref_scroll_up(s, 1);
8684 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8694 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8695 s->selected -= MIN(nscroll, s->selected);
8696 ref_scroll_up(s, MAX(0, nscroll));
8697 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8703 if (s->selected < s->ndisplayed - 1) {
8707 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8708 /* can't scroll any further */
8712 ref_scroll_down(view, 1);
8722 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8723 /* can't scroll any further; move cursor down */
8724 if (s->selected < s->ndisplayed - 1)
8725 s->selected += MIN(nscroll,
8726 s->ndisplayed - s->selected - 1);
8727 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8728 s->selected += s->ndisplayed - s->selected - 1;
8732 ref_scroll_down(view, nscroll);
8737 err = tog_load_refs(s->repo, s->sort_by_date);
8740 ref_view_free_refs(s);
8741 err = ref_view_load_refs(s);
8744 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8745 s->selected = view->nlines - 2;
8759 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8764 static const struct got_error *
8765 cmd_ref(int argc, char *argv[])
8767 const struct got_error *error;
8768 struct got_repository *repo = NULL;
8769 struct got_worktree *worktree = NULL;
8770 char *cwd = NULL, *repo_path = NULL;
8772 struct tog_view *view;
8773 int *pack_fds = NULL;
8775 while ((ch = getopt(argc, argv, "r:")) != -1) {
8778 repo_path = realpath(optarg, NULL);
8779 if (repo_path == NULL)
8780 return got_error_from_errno2("realpath",
8795 error = got_repo_pack_fds_open(&pack_fds);
8799 if (repo_path == NULL) {
8800 cwd = getcwd(NULL, 0);
8802 return got_error_from_errno("getcwd");
8803 error = got_worktree_open(&worktree, cwd);
8804 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8808 strdup(got_worktree_get_repo_path(worktree));
8810 repo_path = strdup(cwd);
8811 if (repo_path == NULL) {
8812 error = got_error_from_errno("strdup");
8817 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8823 error = apply_unveil(got_repo_get_path(repo), NULL);
8827 error = tog_load_refs(repo, 0);
8831 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8833 error = got_error_from_errno("view_open");
8837 error = open_ref_view(view, repo);
8842 /* Release work tree lock. */
8843 got_worktree_close(worktree);
8846 error = view_loop(view);
8851 const struct got_error *close_err = got_repo_close(repo);
8856 const struct got_error *pack_err =
8857 got_repo_pack_fds_close(pack_fds);
8865 static const struct got_error*
8866 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8875 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8879 if (mvwprintw(win, y, x, "%s", str) == ERR)
8880 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8887 static const struct got_error *
8888 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8892 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8894 free(*line_offsets);
8895 *line_offsets = NULL;
8896 return got_error_from_errno("reallocarray");
8900 (*line_offsets)[*nlines] = off;
8905 static const struct got_error *
8906 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8910 for (;n > 0; --n, ++km) {
8914 if (km->keys == NULL)
8917 t = t0 = strdup(km->keys);
8919 return got_error_from_errno("strdup");
8922 while ((k = strsep(&t, " ")) != NULL)
8923 len += strlen(k) > 1 ? 2 : 0;
8925 *ret = MAX(*ret, len);
8932 * Write keymap section headers, keys, and key info in km to f.
8933 * Save line offset to *off. If terminal has UTF8 encoding enabled,
8934 * wrap control and symbolic keys in guillemets, else use <>.
8936 static const struct got_error *
8937 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8942 static const char *u8_glyph[] = {
8943 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8944 "\xe2\x80\xba" /* U+203A (utf8 >) */
8947 int cs, s, first = 1;
8949 cs = got_locale_is_utf8();
8951 t = t0 = strdup(km->keys);
8953 return got_error_from_errno("strdup");
8955 len = strlen(km->keys);
8956 while ((k = strsep(&t, " ")) != NULL) {
8957 s = strlen(k) > 1; /* control or symbolic key */
8958 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8959 cs && s ? u8_glyph[0] : s ? "<" : "", k,
8960 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8963 return got_error_from_errno("fprintf");
8971 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8973 return got_error_from_errno("fprintf");
8979 static const struct got_error *
8980 format_help(struct tog_help_view_state *s)
8982 const struct got_error *err = NULL;
8984 int i, max, n, show = s->all;
8985 static const struct tog_key_map km[] = {
8986 #define KEYMAP_(info, type) { NULL, (info), type }
8987 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8993 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8998 err = max_key_str(&max, km, n);
9002 for (i = 0; i < n; ++i) {
9003 if (km[i].keys == NULL) {
9005 if (km[i].type == TOG_KEYMAP_GLOBAL ||
9006 km[i].type == s->type || s->all)
9010 err = format_help_line(&off, s->f, &km[i], max);
9013 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9020 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9024 static const struct got_error *
9025 create_help(struct tog_help_view_state *s)
9028 const struct got_error *err;
9030 free(s->line_offsets);
9031 s->line_offsets = NULL;
9036 return got_error_from_errno("got_opentemp");
9039 err = format_help(s);
9043 if (s->f && fflush(s->f) != 0)
9044 return got_error_from_errno("fflush");
9049 static const struct got_error *
9050 search_start_help_view(struct tog_view *view)
9052 view->state.help.matched_line = 0;
9057 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
9058 size_t *nlines, int **first, int **last, int **match, int **selected)
9060 struct tog_help_view_state *s = &view->state.help;
9063 *nlines = s->nlines;
9064 *line_offsets = s->line_offsets;
9065 *match = &s->matched_line;
9066 *first = &s->first_displayed_line;
9067 *last = &s->last_displayed_line;
9068 *selected = &s->selected_line;
9071 static const struct got_error *
9072 show_help_view(struct tog_view *view)
9074 struct tog_help_view_state *s = &view->state.help;
9075 const struct got_error *err;
9076 regmatch_t *regmatch = &view->regmatch;
9081 int width, nprinted = 0, rc = 0;
9082 int eos = view->nlines;
9084 if (view_is_hsplit_top(view))
9085 --eos; /* account for border */
9089 werase(view->window);
9091 if (view->gline > s->nlines - 1)
9092 view->gline = s->nlines - 1;
9094 err = win_draw_center(view->window, 0, 0, view->ncols,
9095 view_needs_focus_indication(view),
9096 "tog help (press q to return to tog)");
9101 waddstr(view->window, "\n\n");
9107 while (eos > 0 && nprinted < eos) {
9110 linelen = getline(&line, &linesz, s->f);
9111 if (linelen == -1) {
9114 return got_ferror(s->f, GOT_ERR_IO);
9119 if (++s->lineno < s->first_displayed_line)
9121 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
9123 if (s->lineno == view->hiline)
9126 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
9132 view->maxx = MAX(view->maxx, width);
9137 wattron(view->window, attr);
9138 if (s->first_displayed_line + nprinted == s->matched_line &&
9139 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
9140 err = add_matched_line(&width, line, view->ncols - 1, 0,
9141 view->window, view->x, regmatch);
9149 err = format_line(&wline, &width, &skip, line,
9150 view->x, view->ncols, 0, view->x ? 1 : 0);
9155 waddwstr(view->window, &wline[skip]);
9159 if (s->lineno == view->hiline) {
9160 while (width++ < view->ncols)
9161 waddch(view->window, ' ');
9163 if (width < view->ncols)
9164 waddch(view->window, '\n');
9167 wattroff(view->window, attr);
9168 if (++nprinted == 1)
9169 s->first_displayed_line = s->lineno;
9173 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
9175 s->last_displayed_line = s->first_displayed_line;
9180 rc = waddnstr(view->window,
9181 "See the tog(1) manual page for full documentation",
9184 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9186 wmove(view->window, view->nlines - 1, 0);
9187 wclrtoeol(view->window);
9188 wstandout(view->window);
9189 rc = waddnstr(view->window, "scroll down for more...",
9192 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9193 if (getcurx(view->window) < view->ncols - 6) {
9194 rc = wprintw(view->window, "[%.0f%%]",
9195 100.00 * s->last_displayed_line / s->nlines);
9197 return got_error_msg(GOT_ERR_IO, "wprintw");
9199 wstandend(view->window);
9205 static const struct got_error *
9206 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
9208 struct tog_help_view_state *s = &view->state.help;
9209 const struct got_error *err = NULL;
9215 eos = nscroll = view->nlines;
9216 if (view_is_hsplit_top(view))
9219 s->lineno = s->first_displayed_line - 1 + s->selected_line;
9228 horizontal_scroll_input(view, ch);
9232 s->first_displayed_line = 1;
9240 s->first_displayed_line = (s->nlines - eos) + 3;
9245 if (s->first_displayed_line > 1)
9246 --s->first_displayed_line;
9257 if (s->first_displayed_line == 1) {
9261 while (--nscroll > 0 && s->first_displayed_line > 1)
9262 s->first_displayed_line--;
9268 ++s->first_displayed_line;
9284 while (!s->eof && --nscroll > 0) {
9285 linelen = getline(&line, &linesz, s->f);
9286 s->first_displayed_line++;
9287 if (linelen == -1) {
9291 err = got_ferror(s->f, GOT_ERR_IO);
9305 static const struct got_error *
9306 close_help_view(struct tog_view *view)
9308 struct tog_help_view_state *s = &view->state.help;
9310 free(s->line_offsets);
9311 s->line_offsets = NULL;
9312 if (fclose(s->f) == EOF)
9313 return got_error_from_errno("fclose");
9318 static const struct got_error *
9319 reset_help_view(struct tog_view *view)
9321 struct tog_help_view_state *s = &view->state.help;
9324 if (s->f && fclose(s->f) == EOF)
9325 return got_error_from_errno("fclose");
9327 wclear(view->window);
9331 s->first_displayed_line = 1;
9332 s->last_displayed_line = view->nlines;
9333 s->matched_line = 0;
9335 return create_help(s);
9338 static const struct got_error *
9339 open_help_view(struct tog_view *view, struct tog_view *parent)
9341 const struct got_error *err = NULL;
9342 struct tog_help_view_state *s = &view->state.help;
9344 s->type = (enum tog_keymap_type)parent->type;
9345 s->first_displayed_line = 1;
9346 s->last_displayed_line = view->nlines;
9347 s->selected_line = 1;
9349 view->show = show_help_view;
9350 view->input = input_help_view;
9351 view->reset = reset_help_view;
9352 view->close = close_help_view;
9353 view->search_start = search_start_help_view;
9354 view->search_setup = search_setup_help_view;
9355 view->search_next = search_next_view_match;
9357 err = create_help(s);
9361 static const struct got_error *
9362 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9363 enum tog_view_type request, int y, int x)
9365 const struct got_error *err = NULL;
9371 if (view->type == TOG_VIEW_LOG) {
9372 struct tog_log_view_state *s = &view->state.log;
9374 err = open_diff_view_for_commit(new_view, y, x,
9375 s->selected_entry->commit, s->selected_entry->id,
9378 return got_error_msg(GOT_ERR_NOT_IMPL,
9379 "parent/child view pair not supported");
9381 case TOG_VIEW_BLAME:
9382 if (view->type == TOG_VIEW_TREE) {
9383 struct tog_tree_view_state *s = &view->state.tree;
9385 err = blame_tree_entry(new_view, y, x,
9386 s->selected_entry, &s->parents, s->commit_id,
9389 return got_error_msg(GOT_ERR_NOT_IMPL,
9390 "parent/child view pair not supported");
9393 if (view->type == TOG_VIEW_BLAME)
9394 err = log_annotated_line(new_view, y, x,
9395 view->state.blame.repo, view->state.blame.id_to_log);
9396 else if (view->type == TOG_VIEW_TREE)
9397 err = log_selected_tree_entry(new_view, y, x,
9399 else if (view->type == TOG_VIEW_REF)
9400 err = log_ref_entry(new_view, y, x,
9401 view->state.ref.selected_entry,
9402 view->state.ref.repo);
9404 return got_error_msg(GOT_ERR_NOT_IMPL,
9405 "parent/child view pair not supported");
9408 if (view->type == TOG_VIEW_LOG)
9409 err = browse_commit_tree(new_view, y, x,
9410 view->state.log.selected_entry,
9411 view->state.log.in_repo_path,
9412 view->state.log.head_ref_name,
9413 view->state.log.repo);
9414 else if (view->type == TOG_VIEW_REF)
9415 err = browse_ref_tree(new_view, y, x,
9416 view->state.ref.selected_entry,
9417 view->state.ref.repo);
9419 return got_error_msg(GOT_ERR_NOT_IMPL,
9420 "parent/child view pair not supported");
9423 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9424 if (*new_view == NULL)
9425 return got_error_from_errno("view_open");
9426 if (view->type == TOG_VIEW_LOG)
9427 err = open_ref_view(*new_view, view->state.log.repo);
9428 else if (view->type == TOG_VIEW_TREE)
9429 err = open_ref_view(*new_view, view->state.tree.repo);
9431 err = got_error_msg(GOT_ERR_NOT_IMPL,
9432 "parent/child view pair not supported");
9434 view_close(*new_view);
9437 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9438 if (*new_view == NULL)
9439 return got_error_from_errno("view_open");
9440 err = open_help_view(*new_view, view);
9442 view_close(*new_view);
9445 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9452 * If view was scrolled down to move the selected line into view when opening a
9453 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9456 offset_selection_up(struct tog_view *view)
9458 switch (view->type) {
9459 case TOG_VIEW_BLAME: {
9460 struct tog_blame_view_state *s = &view->state.blame;
9461 if (s->first_displayed_line == 1) {
9462 s->selected_line = MAX(s->selected_line - view->offset,
9466 if (s->first_displayed_line > view->offset)
9467 s->first_displayed_line -= view->offset;
9469 s->first_displayed_line = 1;
9470 s->selected_line += view->offset;
9474 log_scroll_up(&view->state.log, view->offset);
9475 view->state.log.selected += view->offset;
9478 ref_scroll_up(&view->state.ref, view->offset);
9479 view->state.ref.selected += view->offset;
9482 tree_scroll_up(&view->state.tree, view->offset);
9483 view->state.tree.selected += view->offset;
9493 * If the selected line is in the section of screen covered by the bottom split,
9494 * scroll down offset lines to move it into view and index its new position.
9496 static const struct got_error *
9497 offset_selection_down(struct tog_view *view)
9499 const struct got_error *err = NULL;
9500 const struct got_error *(*scrolld)(struct tog_view *, int);
9501 int *selected = NULL;
9504 switch (view->type) {
9505 case TOG_VIEW_BLAME: {
9506 struct tog_blame_view_state *s = &view->state.blame;
9509 if (s->selected_line > view->nlines - header) {
9510 offset = abs(view->nlines - s->selected_line - header);
9511 s->first_displayed_line += offset;
9512 s->selected_line -= offset;
9513 view->offset = offset;
9517 case TOG_VIEW_LOG: {
9518 struct tog_log_view_state *s = &view->state.log;
9519 scrolld = &log_scroll_down;
9520 header = view_is_parent_view(view) ? 3 : 2;
9521 selected = &s->selected;
9524 case TOG_VIEW_REF: {
9525 struct tog_ref_view_state *s = &view->state.ref;
9526 scrolld = &ref_scroll_down;
9528 selected = &s->selected;
9531 case TOG_VIEW_TREE: {
9532 struct tog_tree_view_state *s = &view->state.tree;
9533 scrolld = &tree_scroll_down;
9535 selected = &s->selected;
9545 if (selected && *selected > view->nlines - header) {
9546 offset = abs(view->nlines - *selected - header);
9547 view->offset = offset;
9548 if (scrolld && offset) {
9549 err = scrolld(view, offset);
9550 *selected -= offset;
9558 list_commands(FILE *fp)
9562 fprintf(fp, "commands:");
9563 for (i = 0; i < nitems(tog_commands); i++) {
9564 const struct tog_cmd *cmd = &tog_commands[i];
9565 fprintf(fp, " %s", cmd->name);
9571 usage(int hflag, int status)
9573 FILE *fp = (status == 0) ? stdout : stderr;
9575 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9578 fprintf(fp, "lazy usage: %s path\n", getprogname());
9585 make_argv(int argc, ...)
9593 argv = calloc(argc, sizeof(char *));
9596 for (i = 0; i < argc; i++) {
9597 argv[i] = strdup(va_arg(ap, char *));
9598 if (argv[i] == NULL)
9607 * Try to convert 'tog path' into a 'tog log path' command.
9608 * The user could simply have mistyped the command rather than knowingly
9609 * provided a path. So check whether argv[0] can in fact be resolved
9610 * to a path in the HEAD commit and print a special error if not.
9611 * This hack is for mpi@ <3
9613 static const struct got_error *
9614 tog_log_with_path(int argc, char *argv[])
9616 const struct got_error *error = NULL, *close_err;
9617 const struct tog_cmd *cmd = NULL;
9618 struct got_repository *repo = NULL;
9619 struct got_worktree *worktree = NULL;
9620 struct got_object_id *commit_id = NULL, *id = NULL;
9621 struct got_commit_object *commit = NULL;
9622 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9623 char *commit_id_str = NULL, **cmd_argv = NULL;
9624 int *pack_fds = NULL;
9626 cwd = getcwd(NULL, 0);
9628 return got_error_from_errno("getcwd");
9630 error = got_repo_pack_fds_open(&pack_fds);
9634 error = got_worktree_open(&worktree, cwd);
9635 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9639 repo_path = strdup(got_worktree_get_repo_path(worktree));
9641 repo_path = strdup(cwd);
9642 if (repo_path == NULL) {
9643 error = got_error_from_errno("strdup");
9647 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9651 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9656 error = tog_load_refs(repo, 0);
9659 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9660 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9661 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9666 got_worktree_close(worktree);
9670 error = got_object_open_as_commit(&commit, repo, commit_id);
9674 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9676 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9678 fprintf(stderr, "%s: '%s' is no known command or path\n",
9679 getprogname(), argv[0]);
9684 error = got_object_id_str(&commit_id_str, commit_id);
9688 cmd = &tog_commands[0]; /* log */
9690 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9691 error = cmd->cmd_main(argc, cmd_argv);
9694 close_err = got_repo_close(repo);
9699 got_object_commit_close(commit);
9701 got_worktree_close(worktree);
9703 const struct got_error *pack_err =
9704 got_repo_pack_fds_close(pack_fds);
9709 free(commit_id_str);
9716 for (i = 0; i < argc; i++)
9725 main(int argc, char *argv[])
9727 const struct got_error *io_err, *error = NULL;
9728 const struct tog_cmd *cmd = NULL;
9729 int ch, hflag = 0, Vflag = 0;
9730 char **cmd_argv = NULL;
9731 static const struct option longopts[] = {
9732 { "version", no_argument, NULL, 'V' },
9735 char *diff_algo_str = NULL;
9736 const char *test_script_path;
9738 setlocale(LC_CTYPE, "");
9741 * Test mode init must happen before pledge() because "tty" will
9742 * not allow TTY-related ioctls to occur via regular files.
9744 test_script_path = getenv("GOT_TOG_TEST");
9745 if (test_script_path != NULL) {
9746 error = init_mock_term(test_script_path);
9748 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9753 #if !defined(PROFILE)
9754 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9759 if (!isatty(STDIN_FILENO))
9760 errx(1, "standard input is not a tty");
9762 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9782 got_version_print_str();
9789 /* Build an argument vector which runs a default command. */
9790 cmd = &tog_commands[0];
9792 cmd_argv = make_argv(argc, cmd->name);
9796 /* Did the user specify a command? */
9797 for (i = 0; i < nitems(tog_commands); i++) {
9798 if (strncmp(tog_commands[i].name, argv[0],
9799 strlen(argv[0])) == 0) {
9800 cmd = &tog_commands[i];
9806 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9807 if (diff_algo_str) {
9808 if (strcasecmp(diff_algo_str, "patience") == 0)
9809 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9810 if (strcasecmp(diff_algo_str, "myers") == 0)
9811 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9817 /* No command specified; try log with a path */
9818 error = tog_log_with_path(argc, argv);
9823 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9826 if (using_mock_io) {
9827 io_err = tog_io_close();
9834 for (i = 0; i < argc; i++)
9839 if (error && error->code != GOT_ERR_CANCELLED &&
9840 error->code != GOT_ERR_EOF &&
9841 error->code != GOT_ERR_PRIVSEP_EXIT &&
9842 error->code != GOT_ERR_PRIVSEP_PIPE &&
9843 !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9844 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);