2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
45 #include "got_version.h"
46 #include "got_error.h"
47 #include "got_object.h"
48 #include "got_reference.h"
49 #include "got_repository.h"
51 #include "got_opentemp.h"
53 #include "got_cancel.h"
54 #include "got_commit_graph.h"
55 #include "got_blame.h"
56 #include "got_privsep.h"
58 #include "got_worktree.h"
59 #include "got_keyword.h"
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
66 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
69 #define CTRL(x) ((x) & 0x1f)
72 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
77 const struct got_error *(*cmd_main)(int, char *[]);
78 void (*cmd_usage)(void);
81 __dead static void usage(int, int);
82 __dead static void usage_log(void);
83 __dead static void usage_diff(void);
84 __dead static void usage_blame(void);
85 __dead static void usage_tree(void);
86 __dead static void usage_ref(void);
88 static const struct got_error* cmd_log(int, char *[]);
89 static const struct got_error* cmd_diff(int, char *[]);
90 static const struct got_error* cmd_blame(int, char *[]);
91 static const struct got_error* cmd_tree(int, char *[]);
92 static const struct got_error* cmd_ref(int, char *[]);
94 static const struct tog_cmd tog_commands[] = {
95 { "log", cmd_log, usage_log },
96 { "diff", cmd_diff, usage_diff },
97 { "blame", cmd_blame, usage_blame },
98 { "tree", cmd_tree, usage_tree },
99 { "ref", cmd_ref, usage_ref },
111 /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
112 enum tog_keymap_type {
113 TOG_KEYMAP_KEYS = -2,
129 #define HSPLIT_SCALE 0.3f /* default horizontal split scale */
131 #define TOG_EOF_STRING "(END)"
133 struct commit_queue_entry {
134 TAILQ_ENTRY(commit_queue_entry) entry;
135 struct got_object_id *id;
136 struct got_commit_object *commit;
139 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
140 struct commit_queue {
142 struct commit_queue_head head;
146 STAILQ_ENTRY(tog_color) entry;
150 STAILQ_HEAD(tog_colors, tog_color);
152 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
153 static struct got_reflist_object_id_map *tog_refs_idmap;
155 struct got_object_id *id;
159 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
161 static const struct got_error *
162 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
163 struct got_reference* re2)
165 const char *name1 = got_ref_get_name(re1);
166 const char *name2 = got_ref_get_name(re2);
167 int isbackup1, isbackup2;
169 /* Sort backup refs towards the bottom of the list. */
170 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
171 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
172 if (!isbackup1 && isbackup2) {
175 } else if (isbackup1 && !isbackup2) {
180 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
184 static const struct got_error *
185 tog_load_refs(struct got_repository *repo, int sort_by_date)
187 const struct got_error *err;
189 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
190 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
195 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
202 if (tog_refs_idmap) {
203 got_reflist_object_id_map_free(tog_refs_idmap);
204 tog_refs_idmap = NULL;
206 got_ref_list_free(&tog_refs);
209 static const struct got_error *
210 add_color(struct tog_colors *colors, const char *pattern,
211 int idx, short color)
213 const struct got_error *err = NULL;
214 struct tog_color *tc;
217 if (idx < 1 || idx > COLOR_PAIRS - 1)
220 init_pair(idx, color, -1);
222 tc = calloc(1, sizeof(*tc));
224 return got_error_from_errno("calloc");
225 regerr = regcomp(&tc->regex, pattern,
226 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
228 static char regerr_msg[512];
229 static char err_msg[512];
230 regerror(regerr, &tc->regex, regerr_msg,
232 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
234 err = got_error_msg(GOT_ERR_REGEX, err_msg);
239 STAILQ_INSERT_HEAD(colors, tc, entry);
244 free_colors(struct tog_colors *colors)
246 struct tog_color *tc;
248 while (!STAILQ_EMPTY(colors)) {
249 tc = STAILQ_FIRST(colors);
250 STAILQ_REMOVE_HEAD(colors, entry);
256 static struct tog_color *
257 get_color(struct tog_colors *colors, int colorpair)
259 struct tog_color *tc = NULL;
261 STAILQ_FOREACH(tc, colors, entry) {
262 if (tc->colorpair == colorpair)
270 default_color_value(const char *envvar)
272 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
273 return COLOR_MAGENTA;
274 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
276 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
278 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
280 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
281 return COLOR_MAGENTA;
282 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
283 return COLOR_MAGENTA;
284 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
286 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
288 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
290 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
292 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
294 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
296 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
297 return COLOR_MAGENTA;
298 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
300 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
307 get_color_value(const char *envvar)
309 const char *val = getenv(envvar);
312 return default_color_value(envvar);
314 if (strcasecmp(val, "black") == 0)
316 if (strcasecmp(val, "red") == 0)
318 if (strcasecmp(val, "green") == 0)
320 if (strcasecmp(val, "yellow") == 0)
322 if (strcasecmp(val, "blue") == 0)
324 if (strcasecmp(val, "magenta") == 0)
325 return COLOR_MAGENTA;
326 if (strcasecmp(val, "cyan") == 0)
328 if (strcasecmp(val, "white") == 0)
330 if (strcasecmp(val, "default") == 0)
333 return default_color_value(envvar);
336 struct tog_diff_view_state {
337 struct got_object_id *id1, *id2;
338 const char *label1, *label2;
342 int first_displayed_line;
343 int last_displayed_line;
346 int ignore_whitespace;
348 struct got_repository *repo;
349 struct got_diff_line *lines;
354 /* passed from log or blame view; may be NULL */
355 struct tog_view *parent_view;
358 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
359 static volatile sig_atomic_t tog_thread_error;
361 struct tog_log_thread_args {
362 pthread_cond_t need_commits;
363 pthread_cond_t commit_loaded;
366 struct got_commit_graph *graph;
367 struct commit_queue *real_commits;
368 const char *in_repo_path;
369 struct got_object_id *start_id;
370 struct got_repository *repo;
373 pthread_cond_t log_loaded;
375 struct commit_queue_entry **first_displayed_entry;
376 struct commit_queue_entry **selected_entry;
378 int *search_next_done;
382 regex_t *limit_regex;
383 struct commit_queue *limit_commits;
384 struct got_worktree *worktree;
385 int need_commit_marker;
388 struct tog_log_view_state {
389 struct commit_queue *commits;
390 struct commit_queue_entry *first_displayed_entry;
391 struct commit_queue_entry *last_displayed_entry;
392 struct commit_queue_entry *selected_entry;
393 struct commit_queue real_commits;
398 struct got_repository *repo;
399 struct got_object_id *start_id;
402 struct tog_log_thread_args thread_args;
403 struct commit_queue_entry *matched_entry;
404 struct commit_queue_entry *search_entry;
405 struct tog_colors colors;
409 struct commit_queue limit_commits;
412 #define TOG_COLOR_DIFF_MINUS 1
413 #define TOG_COLOR_DIFF_PLUS 2
414 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
415 #define TOG_COLOR_DIFF_META 4
416 #define TOG_COLOR_TREE_SUBMODULE 5
417 #define TOG_COLOR_TREE_SYMLINK 6
418 #define TOG_COLOR_TREE_DIRECTORY 7
419 #define TOG_COLOR_TREE_EXECUTABLE 8
420 #define TOG_COLOR_COMMIT 9
421 #define TOG_COLOR_AUTHOR 10
422 #define TOG_COLOR_DATE 11
423 #define TOG_COLOR_REFS_HEADS 12
424 #define TOG_COLOR_REFS_TAGS 13
425 #define TOG_COLOR_REFS_REMOTES 14
426 #define TOG_COLOR_REFS_BACKUP 15
428 struct tog_blame_cb_args {
429 struct tog_blame_line *lines; /* one per line */
432 struct tog_view *view;
433 struct got_object_id *commit_id;
437 struct tog_blame_thread_args {
439 struct got_repository *repo;
440 struct tog_blame_cb_args *cb_args;
442 got_cancel_cb cancel_cb;
444 pthread_cond_t blame_complete;
450 struct tog_blame_line *lines;
454 struct tog_blame_thread_args thread_args;
455 struct tog_blame_cb_args cb_args;
460 struct tog_blame_view_state {
461 int first_displayed_line;
462 int last_displayed_line;
464 int last_diffed_line;
468 struct got_object_id_queue blamed_commits;
469 struct got_object_qid *blamed_commit;
471 struct got_repository *repo;
472 struct got_object_id *commit_id;
473 struct got_object_id *id_to_log;
474 struct tog_blame blame;
476 struct tog_colors colors;
479 struct tog_parent_tree {
480 TAILQ_ENTRY(tog_parent_tree) entry;
481 struct got_tree_object *tree;
482 struct got_tree_entry *first_displayed_entry;
483 struct got_tree_entry *selected_entry;
487 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
489 struct tog_tree_view_state {
491 struct got_object_id *commit_id;/* commit which this tree belongs to */
492 struct got_tree_object *root; /* the commit's root tree entry */
493 struct got_tree_object *tree; /* currently displayed (sub-)tree */
494 struct got_tree_entry *first_displayed_entry;
495 struct got_tree_entry *last_displayed_entry;
496 struct got_tree_entry *selected_entry;
497 int ndisplayed, selected, show_ids;
498 struct tog_parent_trees parents; /* parent trees of current sub-tree */
500 struct got_repository *repo;
501 struct got_tree_entry *matched_entry;
502 struct tog_colors colors;
505 struct tog_reflist_entry {
506 TAILQ_ENTRY(tog_reflist_entry) entry;
507 struct got_reference *ref;
511 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
513 struct tog_ref_view_state {
514 struct tog_reflist_head refs;
515 struct tog_reflist_entry *first_displayed_entry;
516 struct tog_reflist_entry *last_displayed_entry;
517 struct tog_reflist_entry *selected_entry;
518 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
519 struct got_repository *repo;
520 struct tog_reflist_entry *matched_entry;
521 struct tog_colors colors;
524 struct tog_help_view_state {
529 int first_displayed_line;
530 int last_displayed_line;
535 enum tog_keymap_type type;
538 #define GENERATE_HELP \
539 KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
540 KEY_("H F1", "Open view-specific help (double tap for all help)"), \
541 KEY_("k C-p Up", "Move cursor or page up one line"), \
542 KEY_("j C-n Down", "Move cursor or page down one line"), \
543 KEY_("C-b b PgUp", "Scroll the view up one page"), \
544 KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
545 KEY_("C-u u", "Scroll the view up one half page"), \
546 KEY_("C-d d", "Scroll the view down one half page"), \
547 KEY_("g", "Go to line N (default: first line)"), \
548 KEY_("Home =", "Go to the first line"), \
549 KEY_("G", "Go to line N (default: last line)"), \
550 KEY_("End *", "Go to the last line"), \
551 KEY_("l Right", "Scroll the view right"), \
552 KEY_("h Left", "Scroll the view left"), \
553 KEY_("$", "Scroll view to the rightmost position"), \
554 KEY_("0", "Scroll view to the leftmost position"), \
555 KEY_("-", "Decrease size of the focussed split"), \
556 KEY_("+", "Increase size of the focussed split"), \
557 KEY_("Tab", "Switch focus between views"), \
558 KEY_("F", "Toggle fullscreen mode"), \
559 KEY_("S", "Switch split-screen layout"), \
560 KEY_("/", "Open prompt to enter search term"), \
561 KEY_("n", "Find next line/token matching the current search term"), \
562 KEY_("N", "Find previous line/token matching the current search term"),\
563 KEY_("q", "Quit the focussed view; Quit help screen"), \
564 KEY_("Q", "Quit tog"), \
566 KEYMAP_("Log view", TOG_KEYMAP_LOG), \
567 KEY_("< ,", "Move cursor up one commit"), \
568 KEY_("> .", "Move cursor down one commit"), \
569 KEY_("Enter", "Open diff view of the selected commit"), \
570 KEY_("B", "Reload the log view and toggle display of merged commits"), \
571 KEY_("R", "Open ref view of all repository references"), \
572 KEY_("T", "Display tree view of the repository from the selected" \
574 KEY_("@", "Toggle between displaying author and committer name"), \
575 KEY_("&", "Open prompt to enter term to limit commits displayed"), \
576 KEY_("C-g Backspace", "Cancel current search or log operation"), \
577 KEY_("C-l", "Reload the log view with new commits in the repository"), \
579 KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
580 KEY_("K < ,", "Display diff of next line in the file/log entry"), \
581 KEY_("J > .", "Display diff of previous line in the file/log entry"), \
582 KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
583 KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
585 KEY_("(", "Go to the previous file in the diff"), \
586 KEY_(")", "Go to the next file in the diff"), \
587 KEY_("{", "Go to the previous hunk in the diff"), \
588 KEY_("}", "Go to the next hunk in the diff"), \
589 KEY_("[", "Decrease the number of context lines"), \
590 KEY_("]", "Increase the number of context lines"), \
591 KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
593 KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
594 KEY_("Enter", "Display diff view of the selected line's commit"), \
595 KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
596 KEY_("L", "Open log view for the currently selected annotated line"), \
597 KEY_("C", "Reload view with the previously blamed commit"), \
598 KEY_("c", "Reload view with the version of the file found in the" \
599 " selected line's commit"), \
600 KEY_("p", "Reload view with the version of the file found in the" \
601 " selected line's parent commit"), \
603 KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
604 KEY_("Enter", "Enter selected directory or open blame view of the" \
606 KEY_("L", "Open log view for the selected entry"), \
607 KEY_("R", "Open ref view of all repository references"), \
608 KEY_("i", "Show object IDs for all tree entries"), \
609 KEY_("Backspace", "Return to the parent directory"), \
611 KEYMAP_("Ref view", TOG_KEYMAP_REF), \
612 KEY_("Enter", "Display log view of the selected reference"), \
613 KEY_("T", "Display tree view of the selected reference"), \
614 KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
615 KEY_("m", "Toggle display of last modified date for each reference"), \
616 KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
617 KEY_("C-l", "Reload view with all repository references")
622 enum tog_keymap_type type;
625 /* curses io for tog regress */
634 static int using_mock_io;
636 #define TOG_KEY_SCRDUMP SHRT_MIN
639 * We implement two types of views: parent views and child views.
641 * The 'Tab' key switches focus between a parent view and its child view.
642 * Child views are shown side-by-side to their parent view, provided
643 * there is enough screen estate.
645 * When a new view is opened from within a parent view, this new view
646 * becomes a child view of the parent view, replacing any existing child.
648 * When a new view is opened from within a child view, this new view
649 * becomes a parent view which will obscure the views below until the
650 * user quits the new parent view by typing 'q'.
652 * This list of views contains parent views only.
653 * Child views are only pointed to by their parent view.
655 TAILQ_HEAD(tog_view_list_head, tog_view);
658 TAILQ_ENTRY(tog_view) entry;
661 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
662 int resized_y, resized_x; /* begin_y/x based on user resizing */
663 int maxx, x; /* max column and current start column */
664 int lines, cols; /* copies of LINES and COLS */
665 int nscrolled, offset; /* lines scrolled and hsplit line offset */
666 int gline, hiline; /* navigate to and highlight this nG line */
667 int ch, count; /* current keymap and count prefix */
668 int resized; /* set when in a resize event */
669 int focussed; /* Only set on one parent or child view at a time. */
671 struct tog_view *parent;
672 struct tog_view *child;
675 * This flag is initially set on parent views when a new child view
676 * is created. It gets toggled when the 'Tab' key switches focus
677 * between parent and child.
678 * The flag indicates whether focus should be passed on to our child
679 * view if this parent view gets picked for focus after another parent
680 * view was closed. This prevents child views from losing focus in such
685 enum tog_view_mode mode;
686 /* type-specific state */
687 enum tog_view_type type;
689 struct tog_diff_view_state diff;
690 struct tog_log_view_state log;
691 struct tog_blame_view_state blame;
692 struct tog_tree_view_state tree;
693 struct tog_ref_view_state ref;
694 struct tog_help_view_state help;
697 const struct got_error *(*show)(struct tog_view *);
698 const struct got_error *(*input)(struct tog_view **,
699 struct tog_view *, int);
700 const struct got_error *(*reset)(struct tog_view *);
701 const struct got_error *(*resize)(struct tog_view *, int);
702 const struct got_error *(*close)(struct tog_view *);
704 const struct got_error *(*search_start)(struct tog_view *);
705 const struct got_error *(*search_next)(struct tog_view *);
706 void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
707 int **, int **, int **, int **);
710 #define TOG_SEARCH_FORWARD 1
711 #define TOG_SEARCH_BACKWARD 2
712 int search_next_done;
713 #define TOG_SEARCH_HAVE_MORE 1
714 #define TOG_SEARCH_NO_MORE 2
715 #define TOG_SEARCH_HAVE_NONE 3
721 static const struct got_error *open_diff_view(struct tog_view *,
722 struct got_object_id *, struct got_object_id *,
723 const char *, const char *, int, int, int, struct tog_view *,
724 struct got_repository *);
725 static const struct got_error *show_diff_view(struct tog_view *);
726 static const struct got_error *input_diff_view(struct tog_view **,
727 struct tog_view *, int);
728 static const struct got_error *reset_diff_view(struct tog_view *);
729 static const struct got_error* close_diff_view(struct tog_view *);
730 static const struct got_error *search_start_diff_view(struct tog_view *);
731 static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
732 size_t *, int **, int **, int **, int **);
733 static const struct got_error *search_next_view_match(struct tog_view *);
735 static const struct got_error *open_log_view(struct tog_view *,
736 struct got_object_id *, struct got_repository *,
737 const char *, const char *, int, struct got_worktree *);
738 static const struct got_error * show_log_view(struct tog_view *);
739 static const struct got_error *input_log_view(struct tog_view **,
740 struct tog_view *, int);
741 static const struct got_error *resize_log_view(struct tog_view *, int);
742 static const struct got_error *close_log_view(struct tog_view *);
743 static const struct got_error *search_start_log_view(struct tog_view *);
744 static const struct got_error *search_next_log_view(struct tog_view *);
746 static const struct got_error *open_blame_view(struct tog_view *, char *,
747 struct got_object_id *, struct got_repository *);
748 static const struct got_error *show_blame_view(struct tog_view *);
749 static const struct got_error *input_blame_view(struct tog_view **,
750 struct tog_view *, int);
751 static const struct got_error *reset_blame_view(struct tog_view *);
752 static const struct got_error *close_blame_view(struct tog_view *);
753 static const struct got_error *search_start_blame_view(struct tog_view *);
754 static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
755 size_t *, int **, int **, int **, int **);
757 static const struct got_error *open_tree_view(struct tog_view *,
758 struct got_object_id *, const char *, struct got_repository *);
759 static const struct got_error *show_tree_view(struct tog_view *);
760 static const struct got_error *input_tree_view(struct tog_view **,
761 struct tog_view *, int);
762 static const struct got_error *close_tree_view(struct tog_view *);
763 static const struct got_error *search_start_tree_view(struct tog_view *);
764 static const struct got_error *search_next_tree_view(struct tog_view *);
766 static const struct got_error *open_ref_view(struct tog_view *,
767 struct got_repository *);
768 static const struct got_error *show_ref_view(struct tog_view *);
769 static const struct got_error *input_ref_view(struct tog_view **,
770 struct tog_view *, int);
771 static const struct got_error *close_ref_view(struct tog_view *);
772 static const struct got_error *search_start_ref_view(struct tog_view *);
773 static const struct got_error *search_next_ref_view(struct tog_view *);
775 static const struct got_error *open_help_view(struct tog_view *,
777 static const struct got_error *show_help_view(struct tog_view *);
778 static const struct got_error *input_help_view(struct tog_view **,
779 struct tog_view *, int);
780 static const struct got_error *reset_help_view(struct tog_view *);
781 static const struct got_error* close_help_view(struct tog_view *);
782 static const struct got_error *search_start_help_view(struct tog_view *);
783 static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
784 size_t *, int **, int **, int **, int **);
786 static volatile sig_atomic_t tog_sigwinch_received;
787 static volatile sig_atomic_t tog_sigpipe_received;
788 static volatile sig_atomic_t tog_sigcont_received;
789 static volatile sig_atomic_t tog_sigint_received;
790 static volatile sig_atomic_t tog_sigterm_received;
793 tog_sigwinch(int signo)
795 tog_sigwinch_received = 1;
799 tog_sigpipe(int signo)
801 tog_sigpipe_received = 1;
805 tog_sigcont(int signo)
807 tog_sigcont_received = 1;
811 tog_sigint(int signo)
813 tog_sigint_received = 1;
817 tog_sigterm(int signo)
819 tog_sigterm_received = 1;
823 tog_fatal_signal_received(void)
825 return (tog_sigpipe_received ||
826 tog_sigint_received || tog_sigterm_received);
829 static const struct got_error *
830 view_close(struct tog_view *view)
832 const struct got_error *err = NULL, *child_err = NULL;
835 child_err = view_close(view->child);
839 err = view->close(view);
841 del_panel(view->panel);
843 delwin(view->window);
845 return err ? err : child_err;
848 static struct tog_view *
849 view_open(int nlines, int ncols, int begin_y, int begin_x,
850 enum tog_view_type type)
852 struct tog_view *view = calloc(1, sizeof(*view));
860 view->nlines = nlines ? nlines : LINES - begin_y;
861 view->ncols = ncols ? ncols : COLS - begin_x;
862 view->begin_y = begin_y;
863 view->begin_x = begin_x;
864 view->window = newwin(nlines, ncols, begin_y, begin_x);
865 if (view->window == NULL) {
869 view->panel = new_panel(view->window);
870 if (view->panel == NULL ||
871 set_panel_userptr(view->panel, view) != OK) {
876 keypad(view->window, TRUE);
881 view_split_begin_x(int begin_x)
883 if (begin_x > 0 || COLS < 120)
885 return (COLS - MAX(COLS / 2, 80));
888 /* XXX Stub till we decide what to do. */
890 view_split_begin_y(int lines)
892 return lines * HSPLIT_SCALE;
895 static const struct got_error *view_resize(struct tog_view *);
897 static const struct got_error *
898 view_splitscreen(struct tog_view *view)
900 const struct got_error *err = NULL;
902 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
903 if (view->resized_y && view->resized_y < view->lines)
904 view->begin_y = view->resized_y;
906 view->begin_y = view_split_begin_y(view->nlines);
908 } else if (!view->resized) {
909 if (view->resized_x && view->resized_x < view->cols - 1 &&
911 view->begin_x = view->resized_x;
913 view->begin_x = view_split_begin_x(0);
916 view->nlines = LINES - view->begin_y;
917 view->ncols = COLS - view->begin_x;
920 err = view_resize(view);
924 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
925 view->parent->nlines = view->begin_y;
927 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
928 return got_error_from_errno("mvwin");
933 static const struct got_error *
934 view_fullscreen(struct tog_view *view)
936 const struct got_error *err = NULL;
939 view->begin_y = view->resized ? view->begin_y : 0;
940 view->nlines = view->resized ? view->nlines : LINES;
944 err = view_resize(view);
948 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
949 return got_error_from_errno("mvwin");
955 view_is_parent_view(struct tog_view *view)
957 return view->parent == NULL;
961 view_is_splitscreen(struct tog_view *view)
963 return view->begin_x > 0 || view->begin_y > 0;
967 view_is_fullscreen(struct tog_view *view)
969 return view->nlines == LINES && view->ncols == COLS;
973 view_is_hsplit_top(struct tog_view *view)
975 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
976 view_is_splitscreen(view->child);
980 view_border(struct tog_view *view)
983 const struct tog_view *view_above;
986 return view_border(view->parent);
988 panel = panel_above(view->panel);
992 view_above = panel_userptr(panel);
993 if (view->mode == TOG_VIEW_SPLIT_HRZN)
994 mvwhline(view->window, view_above->begin_y - 1,
995 view->begin_x, ACS_HLINE, view->ncols);
997 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
998 ACS_VLINE, view->nlines);
1001 static const struct got_error *view_init_hsplit(struct tog_view *, int);
1002 static const struct got_error *request_log_commits(struct tog_view *);
1003 static const struct got_error *offset_selection_down(struct tog_view *);
1004 static void offset_selection_up(struct tog_view *);
1005 static void view_get_split(struct tog_view *, int *, int *);
1007 static const struct got_error *
1008 view_resize(struct tog_view *view)
1010 const struct got_error *err = NULL;
1011 int dif, nlines, ncols;
1013 dif = LINES - view->lines; /* line difference */
1015 if (view->lines > LINES)
1016 nlines = view->nlines - (view->lines - LINES);
1018 nlines = view->nlines + (LINES - view->lines);
1019 if (view->cols > COLS)
1020 ncols = view->ncols - (view->cols - COLS);
1022 ncols = view->ncols + (COLS - view->cols);
1025 int hs = view->child->begin_y;
1027 if (!view_is_fullscreen(view))
1028 view->child->begin_x = view_split_begin_x(view->begin_x);
1029 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1030 view->child->begin_x == 0) {
1033 view_fullscreen(view->child);
1034 if (view->child->focussed)
1035 show_panel(view->child->panel);
1037 show_panel(view->panel);
1039 ncols = view->child->begin_x;
1041 view_splitscreen(view->child);
1042 show_panel(view->child->panel);
1045 * XXX This is ugly and needs to be moved into the above
1046 * logic but "works" for now and my attempts at moving it
1047 * break either 'tab' or 'F' key maps in horizontal splits.
1050 err = view_splitscreen(view->child);
1053 if (dif < 0) { /* top split decreased */
1054 err = offset_selection_down(view);
1061 show_panel(view->child->panel);
1062 nlines = view->nlines;
1064 } else if (view->parent == NULL)
1067 if (view->resize && dif > 0) {
1068 err = view->resize(view, dif);
1073 if (wresize(view->window, nlines, ncols) == ERR)
1074 return got_error_from_errno("wresize");
1075 if (replace_panel(view->panel, view->window) == ERR)
1076 return got_error_from_errno("replace_panel");
1077 wclear(view->window);
1079 view->nlines = nlines;
1080 view->ncols = ncols;
1081 view->lines = LINES;
1087 static const struct got_error *
1088 resize_log_view(struct tog_view *view, int increase)
1090 struct tog_log_view_state *s = &view->state.log;
1091 const struct got_error *err = NULL;
1094 if (s->selected_entry)
1095 n = s->selected_entry->idx + view->lines - s->selected;
1098 * Request commits to account for the increased
1099 * height so we have enough to populate the view.
1101 if (s->commits->ncommits < n) {
1102 view->nscrolled = n - s->commits->ncommits + increase + 1;
1103 err = request_log_commits(view);
1110 view_adjust_offset(struct tog_view *view, int n)
1115 if (view->parent && view->parent->offset) {
1116 if (view->parent->offset + n >= 0)
1117 view->parent->offset += n;
1119 view->parent->offset = 0;
1120 } else if (view->offset) {
1121 if (view->offset - n >= 0)
1128 static const struct got_error *
1129 view_resize_split(struct tog_view *view, int resize)
1131 const struct got_error *err = NULL;
1132 struct tog_view *v = NULL;
1139 if (!v->child || !view_is_splitscreen(v->child))
1142 v->resized = v->child->resized = resize; /* lock for resize event */
1144 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1145 if (v->child->resized_y)
1146 v->child->begin_y = v->child->resized_y;
1148 v->child->begin_y -= resize;
1150 v->child->begin_y += resize;
1151 if (v->child->begin_y < 3) {
1153 v->child->begin_y = 3;
1154 } else if (v->child->begin_y > LINES - 1) {
1156 v->child->begin_y = LINES - 1;
1159 v->child->ncols = COLS;
1160 view_adjust_offset(view, resize);
1161 err = view_init_hsplit(v, v->child->begin_y);
1164 v->child->resized_y = v->child->begin_y;
1166 if (v->child->resized_x)
1167 v->child->begin_x = v->child->resized_x;
1169 v->child->begin_x -= resize;
1171 v->child->begin_x += resize;
1172 if (v->child->begin_x < 11) {
1174 v->child->begin_x = 11;
1175 } else if (v->child->begin_x > COLS - 1) {
1177 v->child->begin_x = COLS - 1;
1179 v->child->resized_x = v->child->begin_x;
1182 v->child->mode = v->mode;
1183 v->child->nlines = v->lines - v->child->begin_y;
1184 v->child->ncols = v->cols - v->child->begin_x;
1187 err = view_fullscreen(v);
1190 err = view_splitscreen(v->child);
1194 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1195 err = offset_selection_down(v->child);
1201 err = v->resize(v, 0);
1202 else if (v->child->resize)
1203 err = v->child->resize(v->child, 0);
1205 v->resized = v->child->resized = 0;
1211 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1213 struct tog_view *v = src->child ? src->child : src;
1215 dst->resized_x = v->resized_x;
1216 dst->resized_y = v->resized_y;
1219 static const struct got_error *
1220 view_close_child(struct tog_view *view)
1222 const struct got_error *err = NULL;
1224 if (view->child == NULL)
1227 err = view_close(view->child);
1232 static const struct got_error *
1233 view_set_child(struct tog_view *view, struct tog_view *child)
1235 const struct got_error *err = NULL;
1237 view->child = child;
1238 child->parent = view;
1240 err = view_resize(view);
1244 if (view->child->resized_x || view->child->resized_y)
1245 err = view_resize_split(view, 0);
1250 static const struct got_error *view_dispatch_request(struct tog_view **,
1251 struct tog_view *, enum tog_view_type, int, int);
1253 static const struct got_error *
1254 view_request_new(struct tog_view **requested, struct tog_view *view,
1255 enum tog_view_type request)
1257 struct tog_view *new_view = NULL;
1258 const struct got_error *err;
1263 if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1264 view_get_split(view, &y, &x);
1266 err = view_dispatch_request(&new_view, view, request, y, x);
1270 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1271 request != TOG_VIEW_HELP) {
1272 err = view_init_hsplit(view, y);
1278 new_view->focussed = 1;
1279 new_view->mode = view->mode;
1280 new_view->nlines = request == TOG_VIEW_HELP ?
1281 view->lines : view->lines - y;
1283 if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1284 view_transfer_size(new_view, view);
1285 err = view_close_child(view);
1288 err = view_set_child(view, new_view);
1291 view->focus_child = 1;
1293 *requested = new_view;
1299 tog_resizeterm(void)
1302 struct winsize size;
1304 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1305 cols = 80; /* Default */
1309 lines = size.ws_row;
1311 resize_term(lines, cols);
1314 static const struct got_error *
1315 view_search_start(struct tog_view *view, int fast_refresh)
1317 const struct got_error *err = NULL;
1318 struct tog_view *v = view;
1322 if (view->search_started) {
1323 regfree(&view->regex);
1324 view->searching = 0;
1325 memset(&view->regmatch, 0, sizeof(view->regmatch));
1327 view->search_started = 0;
1329 if (view->nlines < 1)
1332 if (view_is_hsplit_top(view))
1334 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1337 if (tog_io.input_str != NULL) {
1338 if (strlcpy(pattern, tog_io.input_str, sizeof(pattern)) >=
1340 return got_error(GOT_ERR_NO_SPACE);
1342 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1343 wclrtoeol(v->window);
1344 nodelay(v->window, FALSE); /* block for search term input */
1347 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1348 wrefresh(v->window);
1351 nodelay(v->window, TRUE);
1352 if (!fast_refresh && !using_mock_io)
1358 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1359 err = view->search_start(view);
1361 regfree(&view->regex);
1364 view->search_started = 1;
1365 view->searching = TOG_SEARCH_FORWARD;
1366 view->search_next_done = 0;
1367 view->search_next(view);
1373 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1374 static const struct got_error *
1375 switch_split(struct tog_view *view)
1377 const struct got_error *err = NULL;
1378 struct tog_view *v = NULL;
1385 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1386 v->mode = TOG_VIEW_SPLIT_VERT;
1388 v->mode = TOG_VIEW_SPLIT_HRZN;
1392 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1393 v->mode = TOG_VIEW_SPLIT_NONE;
1395 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1396 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1397 v->child->begin_y = v->child->resized_y;
1398 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1399 v->child->begin_x = v->child->resized_x;
1402 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1404 v->child->ncols = COLS;
1405 v->child->nscrolled = LINES - v->child->nlines;
1407 err = view_init_hsplit(v, v->child->begin_y);
1411 v->child->mode = v->mode;
1412 v->child->nlines = v->lines - v->child->begin_y;
1415 err = view_fullscreen(v);
1418 err = view_splitscreen(v->child);
1422 if (v->mode == TOG_VIEW_SPLIT_NONE)
1423 v->mode = TOG_VIEW_SPLIT_VERT;
1424 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1425 err = offset_selection_down(v);
1428 err = offset_selection_down(v->child);
1432 offset_selection_up(v);
1433 offset_selection_up(v->child);
1436 err = v->resize(v, 0);
1437 else if (v->child->resize)
1438 err = v->child->resize(v->child, 0);
1444 * Strip trailing whitespace from str starting at byte *n;
1445 * if *n < 0, use strlen(str). Return new str length in *n.
1448 strip_trailing_ws(char *str, int *n)
1452 if (str == NULL || *str == '\0')
1458 while (x-- > 0 && isspace((unsigned char)str[x]))
1465 * Extract visible substring of line y from the curses screen
1466 * and strip trailing whitespace. If vline is set, overwrite
1467 * line[vline] with '|' because the ACS_VLINE character is
1468 * written out as 'x'. Write the line to file f.
1470 static const struct got_error *
1471 view_write_line(FILE *f, int y, int vline)
1473 char line[COLS * MB_LEN_MAX]; /* allow for multibyte chars */
1476 r = mvwinnstr(curscr, y, 0, line, sizeof(line));
1478 return got_error_fmt(GOT_ERR_RANGE,
1479 "failed to extract line %d", y);
1482 * In some views, lines are padded with blanks to COLS width.
1483 * Strip them so we can diff without the -b flag when testing.
1485 strip_trailing_ws(line, &r);
1490 w = fprintf(f, "%s\n", line);
1491 if (w != r + 1) /* \n */
1492 return got_ferror(f, GOT_ERR_IO);
1498 * Capture the visible curses screen by writing each line to the
1499 * file at the path set via the TOG_SCR_DUMP environment variable.
1501 static const struct got_error *
1502 screendump(struct tog_view *view)
1504 const struct got_error *err;
1507 err = got_opentemp_truncate(tog_io.sdump);
1511 if ((view->child && view->child->begin_x) ||
1512 (view->parent && view->begin_x)) {
1513 int ncols = view->child ? view->ncols : view->parent->ncols;
1515 /* vertical splitscreen */
1516 for (i = 0; i < view->nlines; ++i) {
1517 err = view_write_line(tog_io.sdump, i, ncols - 1);
1524 /* fullscreen or horizontal splitscreen */
1525 if ((view->child && view->child->begin_y) ||
1526 (view->parent && view->begin_y)) /* hsplit */
1527 hline = view->child ?
1528 view->child->begin_y : view->begin_y;
1530 for (i = 0; i < view->lines; i++) {
1531 if (hline && i == hline - 1) {
1534 /* ACS_HLINE writes out as 'q', overwrite it */
1535 for (c = 0; c < view->cols; ++c)
1536 fputc('-', tog_io.sdump);
1537 fputc('\n', tog_io.sdump);
1541 err = view_write_line(tog_io.sdump, i, 0);
1552 * Compute view->count from numeric input. Assign total to view->count and
1553 * return first non-numeric key entered.
1556 get_compound_key(struct tog_view *view, int c)
1558 struct tog_view *v = view;
1561 if (view_is_hsplit_top(view))
1563 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1567 cbreak(); /* block for input */
1568 nodelay(view->window, FALSE);
1569 wmove(v->window, v->nlines - 1, 0);
1570 wclrtoeol(v->window);
1571 waddch(v->window, ':');
1574 x = getcurx(v->window);
1575 if (x != ERR && x < view->ncols) {
1576 waddch(v->window, c);
1577 wrefresh(v->window);
1581 * Don't overflow. Max valid request should be the greatest
1582 * between the longest and total lines; cap at 10 million.
1587 n = n * 10 + (c - '0');
1588 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1590 if (c == 'G' || c == 'g') { /* nG key map */
1591 view->gline = view->hiline = n;
1596 /* Massage excessive or inapplicable values at the input handler. */
1603 action_report(struct tog_view *view)
1605 struct tog_view *v = view;
1607 if (view_is_hsplit_top(view))
1609 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1612 wmove(v->window, v->nlines - 1, 0);
1613 wclrtoeol(v->window);
1614 wprintw(v->window, ":%s", view->action);
1615 wrefresh(v->window);
1618 * Clear action status report. Only clear in blame view
1619 * once annotating is complete, otherwise it's too fast.
1621 if (view->type == TOG_VIEW_BLAME) {
1622 if (view->state.blame.blame_complete)
1623 view->action = NULL;
1625 view->action = NULL;
1629 * Read the next line from the test script and assign
1630 * key instruction to *ch. If at EOF, set the *done flag.
1632 static const struct got_error *
1633 tog_read_script_key(FILE *script, struct tog_view *view, int *ch, int *done)
1635 const struct got_error *err = NULL;
1641 if (view->count && --view->count) {
1647 if ((n = getline(&line, &linesz, script)) == -1) {
1652 err = got_ferror(script, GOT_ERR_IO);
1657 if (strncasecmp(line, "WAIT_FOR_UI", 11) == 0)
1658 tog_io.wait_for_ui = 1;
1659 else if (strncasecmp(line, "KEY_ENTER", 9) == 0)
1661 else if (strncasecmp(line, "KEY_RIGHT", 9) == 0)
1663 else if (strncasecmp(line, "KEY_LEFT", 8) == 0)
1665 else if (strncasecmp(line, "KEY_DOWN", 8) == 0)
1667 else if (strncasecmp(line, "KEY_UP", 6) == 0)
1669 else if (strncasecmp(line, "TAB", 3) == 0)
1671 else if (strncasecmp(line, "SCREENDUMP", 10) == 0)
1672 *ch = TOG_KEY_SCRDUMP;
1673 else if (isdigit((unsigned char)*line)) {
1676 while (isdigit((unsigned char)*t))
1678 view->ch = *ch = *t;
1680 /* ignore error, view->count is 0 if instruction is invalid */
1681 view->count = strtonum(line, 0, INT_MAX, NULL);
1684 if (n > 2 && (*ch == '/' || *ch == '&')) {
1685 /* skip leading keymap and trim trailing newline */
1686 tog_io.input_str = strndup(line + 1, n - 2);
1687 if (tog_io.input_str == NULL) {
1688 err = got_error_from_errno("strndup");
1699 static const struct got_error *
1700 view_input(struct tog_view **new, int *done, struct tog_view *view,
1701 struct tog_view_list_head *views, int fast_refresh)
1703 const struct got_error *err = NULL;
1710 action_report(view);
1712 /* Clear "no matches" indicator. */
1713 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1714 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1715 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1719 if (view->searching && !view->search_next_done) {
1720 errcode = pthread_mutex_unlock(&tog_mutex);
1722 return got_error_set_errno(errcode,
1723 "pthread_mutex_unlock");
1725 errcode = pthread_mutex_lock(&tog_mutex);
1727 return got_error_set_errno(errcode,
1728 "pthread_mutex_lock");
1729 view->search_next(view);
1733 /* Allow threads to make progress while we are waiting for input. */
1734 errcode = pthread_mutex_unlock(&tog_mutex);
1736 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1738 if (using_mock_io) {
1739 err = tog_read_script_key(tog_io.f, view, &ch, done);
1741 errcode = pthread_mutex_lock(&tog_mutex);
1744 } else if (view->count && --view->count) {
1746 nodelay(view->window, TRUE);
1747 ch = wgetch(view->window);
1748 /* let C-g or backspace abort unfinished count */
1749 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1754 ch = wgetch(view->window);
1755 if (ch >= '1' && ch <= '9')
1756 view->ch = ch = get_compound_key(view, ch);
1758 if (view->hiline && ch != ERR && ch != 0)
1759 view->hiline = 0; /* key pressed, clear line highlight */
1760 nodelay(view->window, TRUE);
1761 errcode = pthread_mutex_lock(&tog_mutex);
1763 return got_error_set_errno(errcode, "pthread_mutex_lock");
1765 if (tog_sigwinch_received || tog_sigcont_received) {
1767 tog_sigwinch_received = 0;
1768 tog_sigcont_received = 0;
1769 TAILQ_FOREACH(v, views, entry) {
1770 err = view_resize(v);
1773 err = v->input(new, v, KEY_RESIZE);
1777 err = view_resize(v->child);
1780 err = v->child->input(new, v->child,
1784 if (v->child->resized_x || v->child->resized_y) {
1785 err = view_resize_split(v, 0);
1797 if (view->type == TOG_VIEW_HELP)
1798 err = view->reset(view);
1800 err = view_request_new(new, view, TOG_VIEW_HELP);
1806 view->child->focussed = 1;
1807 view->focus_child = 1;
1808 } else if (view->parent) {
1810 view->parent->focussed = 1;
1811 view->parent->focus_child = 0;
1812 if (!view_is_splitscreen(view)) {
1813 if (view->parent->resize) {
1814 err = view->parent->resize(view->parent,
1819 offset_selection_up(view->parent);
1820 err = view_fullscreen(view->parent);
1827 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1828 if (view->parent->resize) {
1829 /* might need more commits to fill fullscreen */
1830 err = view->parent->resize(view->parent, 0);
1834 offset_selection_up(view->parent);
1836 err = view->input(new, view, ch);
1844 if (view_is_parent_view(view)) {
1845 if (view->child == NULL)
1847 if (view_is_splitscreen(view->child)) {
1849 view->child->focussed = 1;
1850 err = view_fullscreen(view->child);
1852 err = view_splitscreen(view->child);
1854 err = view_resize_split(view, 0);
1858 err = view->child->input(new, view->child,
1861 if (view_is_splitscreen(view)) {
1862 view->parent->focussed = 0;
1864 err = view_fullscreen(view);
1866 err = view_splitscreen(view);
1867 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1868 err = view_resize(view->parent);
1870 err = view_resize_split(view, 0);
1874 err = view->input(new, view, KEY_RESIZE);
1879 err = view->resize(view, 0);
1884 if (view->parent->resize) {
1885 err = view->parent->resize(view->parent, 0);
1889 err = offset_selection_down(view->parent);
1893 err = offset_selection_down(view);
1897 err = switch_split(view);
1900 err = view_resize_split(view, -1);
1903 err = view_resize_split(view, 1);
1909 if (view->search_start)
1910 view_search_start(view, fast_refresh);
1912 err = view->input(new, view, ch);
1916 if (view->search_started && view->search_next) {
1917 view->searching = (ch == 'n' ?
1918 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1919 view->search_next_done = 0;
1920 view->search_next(view);
1922 err = view->input(new, view, ch);
1925 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1926 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1927 view->action = "Patience diff algorithm";
1929 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1930 view->action = "Myers diff algorithm";
1932 TAILQ_FOREACH(v, views, entry) {
1938 if (v->child && v->child->reset) {
1939 err = v->child->reset(v->child);
1945 case TOG_KEY_SCRDUMP:
1946 err = screendump(view);
1949 err = view->input(new, view, ch);
1957 view_needs_focus_indication(struct tog_view *view)
1959 if (view_is_parent_view(view)) {
1960 if (view->child == NULL || view->child->focussed)
1962 if (!view_is_splitscreen(view->child))
1964 } else if (!view_is_splitscreen(view))
1967 return view->focussed;
1970 static const struct got_error *
1973 const struct got_error *err = NULL;
1975 if (tog_io.cin && fclose(tog_io.cin) == EOF)
1976 err = got_ferror(tog_io.cin, GOT_ERR_IO);
1977 if (tog_io.cout && fclose(tog_io.cout) == EOF && err == NULL)
1978 err = got_ferror(tog_io.cout, GOT_ERR_IO);
1979 if (tog_io.f && fclose(tog_io.f) == EOF && err == NULL)
1980 err = got_ferror(tog_io.f, GOT_ERR_IO);
1981 if (tog_io.sdump && fclose(tog_io.sdump) == EOF && err == NULL)
1982 err = got_ferror(tog_io.sdump, GOT_ERR_IO);
1983 if (tog_io.input_str != NULL)
1984 free(tog_io.input_str);
1989 static const struct got_error *
1990 view_loop(struct tog_view *view)
1992 const struct got_error *err = NULL;
1993 struct tog_view_list_head views;
1994 struct tog_view *new_view;
1996 int fast_refresh = 10;
1997 int done = 0, errcode;
1999 mode = getenv("TOG_VIEW_SPLIT_MODE");
2000 if (!mode || !(*mode == 'h' || *mode == 'H'))
2001 view->mode = TOG_VIEW_SPLIT_VERT;
2003 view->mode = TOG_VIEW_SPLIT_HRZN;
2005 errcode = pthread_mutex_lock(&tog_mutex);
2007 return got_error_set_errno(errcode, "pthread_mutex_lock");
2010 TAILQ_INSERT_HEAD(&views, view, entry);
2013 err = view->show(view);
2018 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
2019 !tog_fatal_signal_received()) {
2020 /* Refresh fast during initialization, then become slower. */
2021 if (fast_refresh && --fast_refresh == 0 && !using_mock_io)
2022 halfdelay(10); /* switch to once per second */
2024 err = view_input(&new_view, &done, view, &views, fast_refresh);
2028 if (view->dying && view == TAILQ_FIRST(&views) &&
2029 TAILQ_NEXT(view, entry) == NULL)
2035 * When we quit, scroll the screen up a single line
2036 * so we don't lose any information.
2038 TAILQ_FOREACH(v, &views, entry) {
2039 wmove(v->window, 0, 0);
2040 wdeleteln(v->window);
2041 wnoutrefresh(v->window);
2042 if (v->child && !view_is_fullscreen(v)) {
2043 wmove(v->child->window, 0, 0);
2044 wdeleteln(v->child->window);
2045 wnoutrefresh(v->child->window);
2052 struct tog_view *v, *prev = NULL;
2054 if (view_is_parent_view(view))
2055 prev = TAILQ_PREV(view, tog_view_list_head,
2057 else if (view->parent)
2058 prev = view->parent;
2061 view->parent->child = NULL;
2062 view->parent->focus_child = 0;
2063 /* Restore fullscreen line height. */
2064 view->parent->nlines = view->parent->lines;
2065 err = view_resize(view->parent);
2068 /* Make resized splits persist. */
2069 view_transfer_size(view->parent, view);
2071 TAILQ_REMOVE(&views, view, entry);
2073 err = view_close(view);
2078 TAILQ_FOREACH(v, &views, entry) {
2082 if (view == NULL && new_view == NULL) {
2083 /* No view has focus. Try to pick one. */
2086 else if (!TAILQ_EMPTY(&views)) {
2087 view = TAILQ_LAST(&views,
2088 tog_view_list_head);
2091 if (view->focus_child) {
2092 view->child->focussed = 1;
2100 struct tog_view *v, *t;
2101 /* Only allow one parent view per type. */
2102 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
2103 if (v->type != new_view->type)
2105 TAILQ_REMOVE(&views, v, entry);
2106 err = view_close(v);
2111 TAILQ_INSERT_TAIL(&views, new_view, entry);
2114 if (view && !done) {
2115 if (view_is_parent_view(view)) {
2116 if (view->child && view->child->focussed)
2119 if (view->parent && view->parent->focussed)
2120 view = view->parent;
2122 show_panel(view->panel);
2123 if (view->child && view_is_splitscreen(view->child))
2124 show_panel(view->child->panel);
2125 if (view->parent && view_is_splitscreen(view)) {
2126 err = view->parent->show(view->parent);
2130 err = view->show(view);
2134 err = view->child->show(view->child);
2143 while (!TAILQ_EMPTY(&views)) {
2144 const struct got_error *close_err;
2145 view = TAILQ_FIRST(&views);
2146 TAILQ_REMOVE(&views, view, entry);
2147 close_err = view_close(view);
2148 if (close_err && err == NULL)
2152 errcode = pthread_mutex_unlock(&tog_mutex);
2153 if (errcode && err == NULL)
2154 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2164 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
2169 /* Create newly allocated wide-character string equivalent to a byte string. */
2170 static const struct got_error *
2171 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
2174 const struct got_error *err = NULL;
2177 *wlen = mbstowcs(NULL, s, 0);
2178 if (*wlen == (size_t)-1) {
2180 if (errno != EILSEQ)
2181 return got_error_from_errno("mbstowcs");
2183 /* byte string invalid in current encoding; try to "fix" it */
2184 err = got_mbsavis(&vis, &vislen, s);
2187 *wlen = mbstowcs(NULL, vis, 0);
2188 if (*wlen == (size_t)-1) {
2189 err = got_error_from_errno("mbstowcs"); /* give up */
2194 *ws = calloc(*wlen + 1, sizeof(**ws));
2196 err = got_error_from_errno("calloc");
2200 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
2201 err = got_error_from_errno("mbstowcs");
2212 static const struct got_error *
2213 expand_tab(char **ptr, const char *src)
2216 size_t len, n, idx = 0, sz = 0;
2219 n = len = strlen(src);
2220 dst = malloc(n + 1);
2222 return got_error_from_errno("malloc");
2224 while (idx < len && src[idx]) {
2225 const char c = src[idx];
2228 size_t nb = TABSIZE - sz % TABSIZE;
2231 p = realloc(dst, n + nb);
2234 return got_error_from_errno("realloc");
2239 memset(dst + sz, ' ', nb);
2242 dst[sz++] = src[idx];
2252 * Advance at most n columns from wline starting at offset off.
2253 * Return the index to the first character after the span operation.
2254 * Return the combined column width of all spanned wide characters in
2258 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2260 int width, i, cols = 0;
2267 for (i = off; wline[i] != L'\0'; ++i) {
2268 if (wline[i] == L'\t')
2269 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2271 width = wcwidth(wline[i]);
2278 if (cols + width > n)
2288 * Format a line for display, ensuring that it won't overflow a width limit.
2289 * With scrolling, the width returned refers to the scrolled version of the
2290 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2292 static const struct got_error *
2293 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2294 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2296 const struct got_error *err = NULL;
2298 wchar_t *wline = NULL;
2307 err = expand_tab(&exstr, line);
2312 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2317 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2319 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2320 wline[wlen - 1] = L'\0';
2323 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2324 wline[wlen - 1] = L'\0';
2328 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2334 *scrollxp = scrollx;
2342 static const struct got_error*
2343 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2344 struct got_object_id *id, struct got_repository *repo)
2346 static const struct got_error *err = NULL;
2347 struct got_reflist_entry *re;
2356 TAILQ_FOREACH(re, refs, entry) {
2357 struct got_tag_object *tag = NULL;
2358 struct got_object_id *ref_id;
2361 name = got_ref_get_name(re->ref);
2362 if (strcmp(name, GOT_REF_HEAD) == 0)
2364 if (strncmp(name, "refs/", 5) == 0)
2366 if (strncmp(name, "got/", 4) == 0)
2368 if (strncmp(name, "heads/", 6) == 0)
2370 if (strncmp(name, "remotes/", 8) == 0) {
2372 s = strstr(name, "/" GOT_REF_HEAD);
2373 if (s != NULL && strcmp(s, "/" GOT_REF_HEAD) == 0)
2376 err = got_ref_resolve(&ref_id, repo, re->ref);
2379 if (strncmp(name, "tags/", 5) == 0) {
2380 err = got_object_open_as_tag(&tag, repo, ref_id);
2382 if (err->code != GOT_ERR_OBJ_TYPE) {
2386 /* Ref points at something other than a tag. */
2391 cmp = got_object_id_cmp(tag ?
2392 got_object_tag_get_object_id(tag) : ref_id, id);
2395 got_object_tag_close(tag);
2399 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2400 s ? ", " : "", name) == -1) {
2401 err = got_error_from_errno("asprintf");
2412 static const struct got_error *
2413 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2418 smallerthan = strchr(author, '<');
2419 if (smallerthan && smallerthan[1] != '\0')
2420 author = smallerthan + 1;
2421 author[strcspn(author, "@>")] = '\0';
2422 return format_line(wauthor, author_width, NULL, author, 0, limit,
2426 static const struct got_error *
2427 draw_commit(struct tog_view *view, struct commit_queue_entry *entry,
2428 const size_t date_display_cols, int author_display_cols)
2430 struct tog_log_view_state *s = &view->state.log;
2431 const struct got_error *err = NULL;
2432 struct got_commit_object *commit = entry->commit;
2433 struct got_object_id *id = entry->id;
2434 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2435 char *refs_str = NULL;
2436 char *logmsg0 = NULL, *logmsg = NULL;
2437 char *author = NULL;
2438 wchar_t *wrefstr = NULL, *wlogmsg = NULL, *wauthor = NULL;
2439 int author_width, refstr_width, logmsg_width;
2440 char *newline, *line = NULL;
2441 int col, limit, scrollx, logmsg_x;
2442 const int avail = view->ncols, marker_column = author_display_cols + 1;
2444 time_t committer_time;
2445 struct tog_color *tc;
2446 struct got_reflist_head *refs;
2448 if (tog_base_commit.id != NULL && tog_base_commit.idx == -1 &&
2449 got_object_id_cmp(id, tog_base_commit.id) == 0)
2450 tog_base_commit.idx = entry->idx;
2451 if (tog_io.wait_for_ui && s->thread_args.need_commit_marker) {
2454 rc = pthread_cond_wait(&s->thread_args.log_loaded, &tog_mutex);
2456 return got_error_set_errno(rc, "pthread_cond_wait");
2459 committer_time = got_object_commit_get_committer_time(commit);
2460 if (gmtime_r(&committer_time, &tm) == NULL)
2461 return got_error_from_errno("gmtime_r");
2462 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2463 return got_error(GOT_ERR_NO_SPACE);
2465 if (avail <= date_display_cols)
2466 limit = MIN(sizeof(datebuf) - 1, avail);
2468 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2469 tc = get_color(&s->colors, TOG_COLOR_DATE);
2471 wattr_on(view->window,
2472 COLOR_PAIR(tc->colorpair), NULL);
2473 waddnstr(view->window, datebuf, limit);
2475 wattr_off(view->window,
2476 COLOR_PAIR(tc->colorpair), NULL);
2483 err = got_object_id_str(&id_str, id);
2486 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2488 wattr_on(view->window,
2489 COLOR_PAIR(tc->colorpair), NULL);
2490 wprintw(view->window, "%.8s ", id_str);
2492 wattr_off(view->window,
2493 COLOR_PAIR(tc->colorpair), NULL);
2500 if (s->use_committer)
2501 author = strdup(got_object_commit_get_committer(commit));
2503 author = strdup(got_object_commit_get_author(commit));
2504 if (author == NULL) {
2505 err = got_error_from_errno("strdup");
2508 err = format_author(&wauthor, &author_width, author, avail - col, col);
2511 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2513 wattr_on(view->window,
2514 COLOR_PAIR(tc->colorpair), NULL);
2515 waddwstr(view->window, wauthor);
2516 col += author_width;
2517 while (col < avail && author_width < author_display_cols + 2) {
2518 if (tog_base_commit.marker != GOT_WORKTREE_STATE_UNKNOWN &&
2519 author_width == marker_column &&
2520 entry->idx == tog_base_commit.idx && !s->limit_view) {
2521 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2523 wattr_on(view->window,
2524 COLOR_PAIR(tc->colorpair), NULL);
2525 waddch(view->window, tog_base_commit.marker);
2527 wattr_off(view->window,
2528 COLOR_PAIR(tc->colorpair), NULL);
2530 waddch(view->window, ' ');
2535 wattr_off(view->window,
2536 COLOR_PAIR(tc->colorpair), NULL);
2540 err = got_object_commit_get_logmsg(&logmsg0, commit);
2544 while (*logmsg == '\n')
2546 newline = strchr(logmsg, '\n');
2550 limit = avail - col;
2551 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2552 limit--; /* for the border */
2554 /* Prepend reference labels to log message if possible .*/
2555 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, id);
2556 err = build_refs_str(&refs_str, refs, id, s->repo);
2562 if (asprintf(&rs, "[%s]", refs_str) == -1) {
2563 err = got_error_from_errno("asprintf");
2566 err = format_line(&wrefstr, &refstr_width,
2567 &scrollx, rs, view->x, limit, col, 1);
2571 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2573 wattr_on(view->window,
2574 COLOR_PAIR(tc->colorpair), NULL);
2575 waddwstr(view->window, &wrefstr[scrollx]);
2577 wattr_off(view->window,
2578 COLOR_PAIR(tc->colorpair), NULL);
2579 col += MAX(refstr_width, 0);
2584 waddch(view->window, ' ');
2588 if (refstr_width > 0)
2591 int unscrolled_refstr_width;
2592 size_t len = wcslen(wrefstr);
2595 * No need to check for -1 return value here since
2596 * unprintables have been replaced by span_wline().
2598 unscrolled_refstr_width = wcswidth(wrefstr, len);
2599 unscrolled_refstr_width += 1; /* trailing space */
2600 logmsg_x = view->x - unscrolled_refstr_width;
2603 limit = avail - col;
2604 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2605 limit--; /* for the border */
2609 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, logmsg_x,
2613 waddwstr(view->window, &wlogmsg[scrollx]);
2614 col += MAX(logmsg_width, 0);
2615 while (col < avail) {
2616 waddch(view->window, ' ');
2630 static struct commit_queue_entry *
2631 alloc_commit_queue_entry(struct got_commit_object *commit,
2632 struct got_object_id *id)
2634 struct commit_queue_entry *entry;
2635 struct got_object_id *dup;
2637 entry = calloc(1, sizeof(*entry));
2641 dup = got_object_id_dup(id);
2648 entry->commit = commit;
2653 pop_commit(struct commit_queue *commits)
2655 struct commit_queue_entry *entry;
2657 entry = TAILQ_FIRST(&commits->head);
2658 TAILQ_REMOVE(&commits->head, entry, entry);
2659 got_object_commit_close(entry->commit);
2660 commits->ncommits--;
2666 free_commits(struct commit_queue *commits)
2668 while (!TAILQ_EMPTY(&commits->head))
2669 pop_commit(commits);
2672 static const struct got_error *
2673 match_commit(int *have_match, struct got_object_id *id,
2674 struct got_commit_object *commit, regex_t *regex)
2676 const struct got_error *err = NULL;
2677 regmatch_t regmatch;
2678 char *id_str = NULL, *logmsg = NULL;
2682 err = got_object_id_str(&id_str, id);
2686 err = got_object_commit_get_logmsg(&logmsg, commit);
2690 if (regexec(regex, got_object_commit_get_author(commit), 1,
2691 ®match, 0) == 0 ||
2692 regexec(regex, got_object_commit_get_committer(commit), 1,
2693 ®match, 0) == 0 ||
2694 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2695 regexec(regex, logmsg, 1, ®match, 0) == 0)
2703 static const struct got_error *
2704 queue_commits(struct tog_log_thread_args *a)
2706 const struct got_error *err = NULL;
2709 * We keep all commits open throughout the lifetime of the log
2710 * view in order to avoid having to re-fetch commits from disk
2711 * while updating the display.
2714 struct got_object_id id;
2715 struct got_commit_object *commit;
2716 struct commit_queue_entry *entry;
2717 int limit_match = 0;
2720 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2725 err = got_object_open_as_commit(&commit, a->repo, &id);
2728 entry = alloc_commit_queue_entry(commit, &id);
2729 if (entry == NULL) {
2730 err = got_error_from_errno("alloc_commit_queue_entry");
2734 errcode = pthread_mutex_lock(&tog_mutex);
2736 err = got_error_set_errno(errcode,
2737 "pthread_mutex_lock");
2741 entry->idx = a->real_commits->ncommits;
2742 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2743 a->real_commits->ncommits++;
2746 err = match_commit(&limit_match, &id, commit,
2752 struct commit_queue_entry *matched;
2754 matched = alloc_commit_queue_entry(
2755 entry->commit, entry->id);
2756 if (matched == NULL) {
2757 err = got_error_from_errno(
2758 "alloc_commit_queue_entry");
2761 matched->commit = entry->commit;
2762 got_object_commit_retain(entry->commit);
2764 matched->idx = a->limit_commits->ncommits;
2765 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2767 a->limit_commits->ncommits++;
2771 * This is how we signal log_thread() that we
2772 * have found a match, and that it should be
2773 * counted as a new entry for the view.
2775 a->limit_match = limit_match;
2778 if (*a->searching == TOG_SEARCH_FORWARD &&
2779 !*a->search_next_done) {
2781 err = match_commit(&have_match, &id, commit, a->regex);
2786 if (limit_match && have_match)
2787 *a->search_next_done =
2788 TOG_SEARCH_HAVE_MORE;
2789 } else if (have_match)
2790 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2793 errcode = pthread_mutex_unlock(&tog_mutex);
2794 if (errcode && err == NULL)
2795 err = got_error_set_errno(errcode,
2796 "pthread_mutex_unlock");
2799 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2805 select_commit(struct tog_log_view_state *s)
2807 struct commit_queue_entry *entry;
2810 entry = s->first_displayed_entry;
2812 if (ncommits == s->selected) {
2813 s->selected_entry = entry;
2816 entry = TAILQ_NEXT(entry, entry);
2821 static const struct got_error *
2822 draw_commits(struct tog_view *view)
2824 const struct got_error *err = NULL;
2825 struct tog_log_view_state *s = &view->state.log;
2826 struct commit_queue_entry *entry = s->selected_entry;
2827 int limit = view->nlines;
2829 int ncommits, author_cols = 4, refstr_cols;
2830 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2831 char *refs_str = NULL;
2833 struct tog_color *tc;
2834 static const size_t date_display_cols = 12;
2835 struct got_reflist_head *refs;
2837 if (view_is_hsplit_top(view))
2838 --limit; /* account for border */
2840 if (s->selected_entry &&
2841 !(view->searching && view->search_next_done == 0)) {
2842 err = got_object_id_str(&id_str, s->selected_entry->id);
2845 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2846 s->selected_entry->id);
2847 err = build_refs_str(&refs_str, refs, s->selected_entry->id,
2853 if (s->thread_args.commits_needed == 0 && !using_mock_io)
2854 halfdelay(10); /* disable fast refresh */
2856 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2857 if (asprintf(&ncommits_str, " [%d/%d] %s",
2858 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2859 (view->searching && !view->search_next_done) ?
2860 "searching..." : "loading...") == -1) {
2861 err = got_error_from_errno("asprintf");
2865 const char *search_str = NULL;
2866 const char *limit_str = NULL;
2868 if (view->searching) {
2869 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2870 search_str = "no more matches";
2871 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2872 search_str = "no matches found";
2873 else if (!view->search_next_done)
2874 search_str = "searching...";
2877 if (s->limit_view && s->commits->ncommits == 0)
2878 limit_str = "no matches found";
2880 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2881 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2882 search_str ? search_str : (refs_str ? refs_str : ""),
2883 limit_str ? limit_str : "") == -1) {
2884 err = got_error_from_errno("asprintf");
2892 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2893 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2894 "........................................",
2895 s->in_repo_path, ncommits_str) == -1) {
2896 err = got_error_from_errno("asprintf");
2900 } else if (asprintf(&header, "commit %s%s",
2901 id_str ? id_str : "........................................",
2902 ncommits_str) == -1) {
2903 err = got_error_from_errno("asprintf");
2907 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2911 werase(view->window);
2913 if (view_needs_focus_indication(view))
2914 wstandout(view->window);
2915 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2917 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2918 waddwstr(view->window, wline);
2919 while (width < view->ncols) {
2920 waddch(view->window, ' ');
2924 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2925 if (view_needs_focus_indication(view))
2926 wstandend(view->window);
2931 /* Grow author column size if necessary, and set view->maxx. */
2932 entry = s->first_displayed_entry;
2936 struct got_commit_object *c = entry->commit;
2937 char *author, *eol, *msg, *msg0;
2938 wchar_t *wauthor, *wmsg;
2940 if (ncommits >= limit - 1)
2942 if (s->use_committer)
2943 author = strdup(got_object_commit_get_committer(c));
2945 author = strdup(got_object_commit_get_author(c));
2946 if (author == NULL) {
2947 err = got_error_from_errno("strdup");
2950 err = format_author(&wauthor, &width, author, COLS,
2952 if (author_cols < width)
2953 author_cols = width;
2958 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2960 err = build_refs_str(&refs_str, refs, entry->id, s->repo);
2965 err = format_line(&ws, &width, NULL, refs_str,
2966 0, INT_MAX, date_display_cols + author_cols, 0);
2972 refstr_cols = width + 3; /* account for [ ] + space */
2975 err = got_object_commit_get_logmsg(&msg0, c);
2979 while (*msg == '\n')
2981 if ((eol = strchr(msg, '\n')))
2983 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2984 date_display_cols + author_cols + refstr_cols, 0);
2987 view->maxx = MAX(view->maxx, width + refstr_cols);
2991 entry = TAILQ_NEXT(entry, entry);
2994 entry = s->first_displayed_entry;
2995 s->last_displayed_entry = s->first_displayed_entry;
2998 if (ncommits >= limit - 1)
3000 if (ncommits == s->selected)
3001 wstandout(view->window);
3002 err = draw_commit(view, entry, date_display_cols, author_cols);
3003 if (ncommits == s->selected)
3004 wstandend(view->window);
3008 s->last_displayed_entry = entry;
3009 entry = TAILQ_NEXT(entry, entry);
3022 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
3024 struct commit_queue_entry *entry;
3027 entry = TAILQ_FIRST(&s->commits->head);
3028 if (s->first_displayed_entry == entry)
3031 entry = s->first_displayed_entry;
3032 while (entry && nscrolled < maxscroll) {
3033 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3035 s->first_displayed_entry = entry;
3041 static const struct got_error *
3042 trigger_log_thread(struct tog_view *view, int wait)
3044 struct tog_log_thread_args *ta = &view->state.log.thread_args;
3048 halfdelay(1); /* fast refresh while loading commits */
3050 while (!ta->log_complete && !tog_thread_error &&
3051 (ta->commits_needed > 0 || ta->load_all)) {
3052 /* Wake the log thread. */
3053 errcode = pthread_cond_signal(&ta->need_commits);
3055 return got_error_set_errno(errcode,
3056 "pthread_cond_signal");
3059 * The mutex will be released while the view loop waits
3060 * in wgetch(), at which time the log thread will run.
3065 /* Display progress update in log view. */
3066 show_log_view(view);
3070 /* Wait right here while next commit is being loaded. */
3071 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
3073 return got_error_set_errno(errcode,
3074 "pthread_cond_wait");
3076 /* Display progress update in log view. */
3077 show_log_view(view);
3085 static const struct got_error *
3086 request_log_commits(struct tog_view *view)
3088 struct tog_log_view_state *state = &view->state.log;
3089 const struct got_error *err = NULL;
3091 if (state->thread_args.log_complete)
3094 state->thread_args.commits_needed += view->nscrolled;
3095 err = trigger_log_thread(view, 1);
3096 view->nscrolled = 0;
3101 static const struct got_error *
3102 log_scroll_down(struct tog_view *view, int maxscroll)
3104 struct tog_log_view_state *s = &view->state.log;
3105 const struct got_error *err = NULL;
3106 struct commit_queue_entry *pentry;
3107 int nscrolled = 0, ncommits_needed;
3109 if (s->last_displayed_entry == NULL)
3112 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
3113 if (s->commits->ncommits < ncommits_needed &&
3114 !s->thread_args.log_complete) {
3116 * Ask the log thread for required amount of commits.
3118 s->thread_args.commits_needed +=
3119 ncommits_needed - s->commits->ncommits;
3120 err = trigger_log_thread(view, 1);
3126 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
3127 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
3130 s->last_displayed_entry = pentry ?
3131 pentry : s->last_displayed_entry;
3133 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
3136 s->first_displayed_entry = pentry;
3137 } while (++nscrolled < maxscroll);
3139 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
3140 view->nscrolled += nscrolled;
3142 view->nscrolled = 0;
3147 static const struct got_error *
3148 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
3149 struct got_commit_object *commit, struct got_object_id *commit_id,
3150 struct tog_view *log_view, struct got_repository *repo)
3152 const struct got_error *err;
3153 struct got_object_qid *parent_id;
3154 struct tog_view *diff_view;
3156 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
3157 if (diff_view == NULL)
3158 return got_error_from_errno("view_open");
3160 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3161 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
3162 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
3164 *new_view = diff_view;
3168 static const struct got_error *
3169 tree_view_visit_subtree(struct tog_tree_view_state *s,
3170 struct got_tree_object *subtree)
3172 struct tog_parent_tree *parent;
3174 parent = calloc(1, sizeof(*parent));
3176 return got_error_from_errno("calloc");
3178 parent->tree = s->tree;
3179 parent->first_displayed_entry = s->first_displayed_entry;
3180 parent->selected_entry = s->selected_entry;
3181 parent->selected = s->selected;
3182 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3185 s->first_displayed_entry = NULL;
3189 static const struct got_error *
3190 tree_view_walk_path(struct tog_tree_view_state *s,
3191 struct got_commit_object *commit, const char *path)
3193 const struct got_error *err = NULL;
3194 struct got_tree_object *tree = NULL;
3196 char *slash, *subpath = NULL;
3198 /* Walk the path and open corresponding tree objects. */
3201 struct got_tree_entry *te;
3202 struct got_object_id *tree_id;
3208 /* Ensure the correct subtree entry is selected. */
3209 slash = strchr(p, '/');
3211 te_name = strdup(p);
3213 te_name = strndup(p, slash - p);
3214 if (te_name == NULL) {
3215 err = got_error_from_errno("strndup");
3218 te = got_object_tree_find_entry(s->tree, te_name);
3220 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
3225 s->first_displayed_entry = s->selected_entry = te;
3227 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
3228 break; /* jump to this file's entry */
3230 slash = strchr(p, '/');
3232 subpath = strndup(path, slash - path);
3234 subpath = strdup(path);
3235 if (subpath == NULL) {
3236 err = got_error_from_errno("strdup");
3240 err = got_object_id_by_path(&tree_id, s->repo, commit,
3245 err = got_object_open_as_tree(&tree, s->repo, tree_id);
3250 err = tree_view_visit_subtree(s, tree);
3252 got_object_tree_close(tree);
3266 static const struct got_error *
3267 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
3268 struct commit_queue_entry *entry, const char *path,
3269 const char *head_ref_name, struct got_repository *repo)
3271 const struct got_error *err = NULL;
3272 struct tog_tree_view_state *s;
3273 struct tog_view *tree_view;
3275 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
3276 if (tree_view == NULL)
3277 return got_error_from_errno("view_open");
3279 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
3282 s = &tree_view->state.tree;
3284 *new_view = tree_view;
3286 if (got_path_is_root_dir(path))
3289 return tree_view_walk_path(s, entry->commit, path);
3292 static const struct got_error *
3293 block_signals_used_by_main_thread(void)
3298 if (sigemptyset(&sigset) == -1)
3299 return got_error_from_errno("sigemptyset");
3301 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
3302 if (sigaddset(&sigset, SIGWINCH) == -1)
3303 return got_error_from_errno("sigaddset");
3304 if (sigaddset(&sigset, SIGCONT) == -1)
3305 return got_error_from_errno("sigaddset");
3306 if (sigaddset(&sigset, SIGINT) == -1)
3307 return got_error_from_errno("sigaddset");
3308 if (sigaddset(&sigset, SIGTERM) == -1)
3309 return got_error_from_errno("sigaddset");
3311 /* ncurses handles SIGTSTP */
3312 if (sigaddset(&sigset, SIGTSTP) == -1)
3313 return got_error_from_errno("sigaddset");
3315 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
3317 return got_error_set_errno(errcode, "pthread_sigmask");
3323 log_thread(void *arg)
3325 const struct got_error *err = NULL;
3327 struct tog_log_thread_args *a = arg;
3331 * Sync startup with main thread such that we begin our
3332 * work once view_input() has released the mutex.
3334 errcode = pthread_mutex_lock(&tog_mutex);
3336 err = got_error_set_errno(errcode, "pthread_mutex_lock");
3340 err = block_signals_used_by_main_thread();
3342 pthread_mutex_unlock(&tog_mutex);
3346 while (!done && !err && !tog_fatal_signal_received()) {
3347 errcode = pthread_mutex_unlock(&tog_mutex);
3349 err = got_error_set_errno(errcode,
3350 "pthread_mutex_unlock");
3353 err = queue_commits(a);
3355 if (err->code != GOT_ERR_ITER_COMPLETED)
3359 a->commits_needed = 0;
3360 } else if (a->commits_needed > 0 && !a->load_all) {
3363 a->commits_needed--;
3365 a->commits_needed--;
3368 errcode = pthread_mutex_lock(&tog_mutex);
3370 err = got_error_set_errno(errcode,
3371 "pthread_mutex_lock");
3373 } else if (*a->quit)
3375 else if (*a->limiting && *a->first_displayed_entry == NULL) {
3376 *a->first_displayed_entry =
3377 TAILQ_FIRST(&a->limit_commits->head);
3378 *a->selected_entry = *a->first_displayed_entry;
3379 } else if (*a->first_displayed_entry == NULL) {
3380 *a->first_displayed_entry =
3381 TAILQ_FIRST(&a->real_commits->head);
3382 *a->selected_entry = *a->first_displayed_entry;
3385 errcode = pthread_cond_signal(&a->commit_loaded);
3387 err = got_error_set_errno(errcode,
3388 "pthread_cond_signal");
3389 pthread_mutex_unlock(&tog_mutex);
3393 if (a->commits_needed == 0 &&
3394 a->need_commit_marker && a->worktree) {
3395 errcode = pthread_mutex_unlock(&tog_mutex);
3397 err = got_error_set_errno(errcode,
3398 "pthread_mutex_unlock");
3401 err = got_worktree_get_state(&tog_base_commit.marker,
3402 a->repo, a->worktree, NULL, NULL);
3405 errcode = pthread_mutex_lock(&tog_mutex);
3407 err = got_error_set_errno(errcode,
3408 "pthread_mutex_lock");
3411 a->need_commit_marker = 0;
3413 * The main thread did not close this
3414 * work tree yet. Close it now.
3416 got_worktree_close(a->worktree);
3424 a->commits_needed = 0;
3426 if (a->commits_needed == 0 && !a->load_all) {
3427 if (tog_io.wait_for_ui) {
3428 errcode = pthread_cond_signal(
3430 if (errcode && err == NULL)
3431 err = got_error_set_errno(
3433 "pthread_cond_signal");
3436 errcode = pthread_cond_wait(&a->need_commits,
3439 err = got_error_set_errno(errcode,
3440 "pthread_cond_wait");
3441 pthread_mutex_unlock(&tog_mutex);
3449 a->log_complete = 1;
3450 if (tog_io.wait_for_ui) {
3451 errcode = pthread_cond_signal(&a->log_loaded);
3452 if (errcode && err == NULL)
3453 err = got_error_set_errno(errcode,
3454 "pthread_cond_signal");
3457 errcode = pthread_mutex_unlock(&tog_mutex);
3459 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3462 tog_thread_error = 1;
3463 pthread_cond_signal(&a->commit_loaded);
3465 got_worktree_close(a->worktree);
3472 static const struct got_error *
3473 stop_log_thread(struct tog_log_view_state *s)
3475 const struct got_error *err = NULL, *thread_err = NULL;
3480 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3482 return got_error_set_errno(errcode,
3483 "pthread_cond_signal");
3484 errcode = pthread_mutex_unlock(&tog_mutex);
3486 return got_error_set_errno(errcode,
3487 "pthread_mutex_unlock");
3488 errcode = pthread_join(s->thread, (void **)&thread_err);
3490 return got_error_set_errno(errcode, "pthread_join");
3491 errcode = pthread_mutex_lock(&tog_mutex);
3493 return got_error_set_errno(errcode,
3494 "pthread_mutex_lock");
3498 if (s->thread_args.repo) {
3499 err = got_repo_close(s->thread_args.repo);
3500 s->thread_args.repo = NULL;
3503 if (s->thread_args.pack_fds) {
3504 const struct got_error *pack_err =
3505 got_repo_pack_fds_close(s->thread_args.pack_fds);
3508 s->thread_args.pack_fds = NULL;
3511 if (s->thread_args.graph) {
3512 got_commit_graph_close(s->thread_args.graph);
3513 s->thread_args.graph = NULL;
3516 return err ? err : thread_err;
3519 static const struct got_error *
3520 close_log_view(struct tog_view *view)
3522 const struct got_error *err = NULL;
3523 struct tog_log_view_state *s = &view->state.log;
3526 err = stop_log_thread(s);
3528 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3529 if (errcode && err == NULL)
3530 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3532 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3533 if (errcode && err == NULL)
3534 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3536 free_commits(&s->limit_commits);
3537 free_commits(&s->real_commits);
3538 free_colors(&s->colors);
3539 free(s->in_repo_path);
3540 s->in_repo_path = NULL;
3543 free(s->head_ref_name);
3544 s->head_ref_name = NULL;
3549 * We use two queues to implement the limit feature: first consists of
3550 * commits matching the current limit_regex; second is the real queue
3551 * of all known commits (real_commits). When the user starts limiting,
3552 * we swap queues such that all movement and displaying functionality
3553 * works with very slight change.
3555 static const struct got_error *
3556 limit_log_view(struct tog_view *view)
3558 struct tog_log_view_state *s = &view->state.log;
3559 struct commit_queue_entry *entry;
3560 struct tog_view *v = view;
3561 const struct got_error *err = NULL;
3565 if (view_is_hsplit_top(view))
3567 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3570 if (tog_io.input_str != NULL) {
3571 if (strlcpy(pattern, tog_io.input_str, sizeof(pattern)) >=
3573 return got_error(GOT_ERR_NO_SPACE);
3575 wmove(v->window, v->nlines - 1, 0);
3576 wclrtoeol(v->window);
3577 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3578 nodelay(v->window, FALSE);
3581 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3584 nodelay(v->window, TRUE);
3589 if (*pattern == '\0') {
3591 * Safety measure for the situation where the user
3592 * resets limit without previously limiting anything.
3598 * User could have pressed Ctrl+L, which refreshed the
3599 * commit queues, it means we can't save previously
3600 * (before limit took place) displayed entries,
3601 * because they would point to already free'ed memory,
3602 * so we are forced to always select first entry of
3605 s->commits = &s->real_commits;
3606 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3607 s->selected_entry = s->first_displayed_entry;
3614 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3619 /* Clear the screen while loading limit view */
3620 s->first_displayed_entry = NULL;
3621 s->last_displayed_entry = NULL;
3622 s->selected_entry = NULL;
3623 s->commits = &s->limit_commits;
3625 /* Prepare limit queue for new search */
3626 free_commits(&s->limit_commits);
3627 s->limit_commits.ncommits = 0;
3629 /* First process commits, which are in queue already */
3630 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3633 err = match_commit(&have_match, entry->id,
3634 entry->commit, &s->limit_regex);
3639 struct commit_queue_entry *matched;
3641 matched = alloc_commit_queue_entry(entry->commit,
3643 if (matched == NULL) {
3644 err = got_error_from_errno(
3645 "alloc_commit_queue_entry");
3648 matched->commit = entry->commit;
3649 got_object_commit_retain(entry->commit);
3651 matched->idx = s->limit_commits.ncommits;
3652 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3654 s->limit_commits.ncommits++;
3658 /* Second process all the commits, until we fill the screen */
3659 if (s->limit_commits.ncommits < view->nlines - 1 &&
3660 !s->thread_args.log_complete) {
3661 s->thread_args.commits_needed +=
3662 view->nlines - s->limit_commits.ncommits - 1;
3663 err = trigger_log_thread(view, 1);
3668 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3669 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3675 static const struct got_error *
3676 search_start_log_view(struct tog_view *view)
3678 struct tog_log_view_state *s = &view->state.log;
3680 s->matched_entry = NULL;
3681 s->search_entry = NULL;
3685 static const struct got_error *
3686 search_next_log_view(struct tog_view *view)
3688 const struct got_error *err = NULL;
3689 struct tog_log_view_state *s = &view->state.log;
3690 struct commit_queue_entry *entry;
3692 /* Display progress update in log view. */
3693 show_log_view(view);
3697 if (s->search_entry) {
3698 if (!using_mock_io) {
3701 errcode = pthread_mutex_unlock(&tog_mutex);
3703 return got_error_set_errno(errcode,
3704 "pthread_mutex_unlock");
3705 ch = wgetch(view->window);
3706 errcode = pthread_mutex_lock(&tog_mutex);
3708 return got_error_set_errno(errcode,
3709 "pthread_mutex_lock");
3710 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3711 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3715 if (view->searching == TOG_SEARCH_FORWARD)
3716 entry = TAILQ_NEXT(s->search_entry, entry);
3718 entry = TAILQ_PREV(s->search_entry,
3719 commit_queue_head, entry);
3720 } else if (s->matched_entry) {
3722 * If the user has moved the cursor after we hit a match,
3723 * the position from where we should continue searching
3724 * might have changed.
3726 if (view->searching == TOG_SEARCH_FORWARD)
3727 entry = TAILQ_NEXT(s->selected_entry, entry);
3729 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3732 entry = s->selected_entry;
3738 if (entry == NULL) {
3739 if (s->thread_args.log_complete ||
3740 view->searching == TOG_SEARCH_BACKWARD) {
3741 view->search_next_done =
3742 (s->matched_entry == NULL ?
3743 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3744 s->search_entry = NULL;
3748 * Poke the log thread for more commits and return,
3749 * allowing the main loop to make progress. Search
3750 * will resume at s->search_entry once we come back.
3752 s->search_entry = s->selected_entry;
3753 s->thread_args.commits_needed++;
3754 return trigger_log_thread(view, 0);
3757 err = match_commit(&have_match, entry->id, entry->commit,
3762 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3763 s->matched_entry = entry;
3767 s->search_entry = entry;
3768 if (view->searching == TOG_SEARCH_FORWARD)
3769 entry = TAILQ_NEXT(entry, entry);
3771 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3774 if (s->matched_entry) {
3775 int cur = s->selected_entry->idx;
3776 while (cur < s->matched_entry->idx) {
3777 err = input_log_view(NULL, view, KEY_DOWN);
3782 while (cur > s->matched_entry->idx) {
3783 err = input_log_view(NULL, view, KEY_UP);
3790 s->search_entry = NULL;
3795 static const struct got_error *
3796 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3797 struct got_repository *repo, const char *head_ref_name,
3798 const char *in_repo_path, int log_branches,
3799 struct got_worktree *worktree)
3801 const struct got_error *err = NULL;
3802 struct tog_log_view_state *s = &view->state.log;
3803 struct got_repository *thread_repo = NULL;
3804 struct got_commit_graph *thread_graph = NULL;
3807 if (in_repo_path != s->in_repo_path) {
3808 free(s->in_repo_path);
3809 s->in_repo_path = strdup(in_repo_path);
3810 if (s->in_repo_path == NULL) {
3811 err = got_error_from_errno("strdup");
3816 /* The commit queue only contains commits being displayed. */
3817 TAILQ_INIT(&s->real_commits.head);
3818 s->real_commits.ncommits = 0;
3819 s->commits = &s->real_commits;
3821 TAILQ_INIT(&s->limit_commits.head);
3823 s->limit_commits.ncommits = 0;
3826 if (head_ref_name) {
3827 s->head_ref_name = strdup(head_ref_name);
3828 if (s->head_ref_name == NULL) {
3829 err = got_error_from_errno("strdup");
3833 s->start_id = got_object_id_dup(start_id);
3834 if (s->start_id == NULL) {
3835 err = got_error_from_errno("got_object_id_dup");
3838 s->log_branches = log_branches;
3839 s->use_committer = 1;
3841 STAILQ_INIT(&s->colors);
3842 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3843 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3844 get_color_value("TOG_COLOR_COMMIT"));
3847 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3848 get_color_value("TOG_COLOR_AUTHOR"));
3851 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3852 get_color_value("TOG_COLOR_DATE"));
3857 view->show = show_log_view;
3858 view->input = input_log_view;
3859 view->resize = resize_log_view;
3860 view->close = close_log_view;
3861 view->search_start = search_start_log_view;
3862 view->search_next = search_next_log_view;
3864 if (s->thread_args.pack_fds == NULL) {
3865 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3869 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3870 s->thread_args.pack_fds);
3873 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3877 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3878 s->repo, NULL, NULL);
3882 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3884 err = got_error_set_errno(errcode, "pthread_cond_init");
3887 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3889 err = got_error_set_errno(errcode, "pthread_cond_init");
3893 if (using_mock_io) {
3896 rc = pthread_cond_init(&s->thread_args.log_loaded, NULL);
3898 return got_error_set_errno(rc, "pthread_cond_init");
3901 s->thread_args.commits_needed = view->nlines;
3902 s->thread_args.graph = thread_graph;
3903 s->thread_args.real_commits = &s->real_commits;
3904 s->thread_args.limit_commits = &s->limit_commits;
3905 s->thread_args.in_repo_path = s->in_repo_path;
3906 s->thread_args.start_id = s->start_id;
3907 s->thread_args.repo = thread_repo;
3908 s->thread_args.log_complete = 0;
3909 s->thread_args.quit = &s->quit;
3910 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3911 s->thread_args.selected_entry = &s->selected_entry;
3912 s->thread_args.searching = &view->searching;
3913 s->thread_args.search_next_done = &view->search_next_done;
3914 s->thread_args.regex = &view->regex;
3915 s->thread_args.limiting = &s->limit_view;
3916 s->thread_args.limit_regex = &s->limit_regex;
3917 s->thread_args.limit_commits = &s->limit_commits;
3918 s->thread_args.worktree = worktree;
3920 s->thread_args.need_commit_marker = 1;
3923 if (view->close == NULL)
3924 close_log_view(view);
3930 static const struct got_error *
3931 show_log_view(struct tog_view *view)
3933 const struct got_error *err;
3934 struct tog_log_view_state *s = &view->state.log;
3936 if (s->thread == NULL) {
3937 int errcode = pthread_create(&s->thread, NULL, log_thread,
3940 return got_error_set_errno(errcode, "pthread_create");
3941 if (s->thread_args.commits_needed > 0) {
3942 err = trigger_log_thread(view, 1);
3948 return draw_commits(view);
3952 log_move_cursor_up(struct tog_view *view, int page, int home)
3954 struct tog_log_view_state *s = &view->state.log;
3956 if (s->first_displayed_entry == NULL)
3958 if (s->selected_entry->idx == 0)
3961 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3963 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3965 if (!page && !home && s->selected > 0)
3968 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3974 static const struct got_error *
3975 log_move_cursor_down(struct tog_view *view, int page)
3977 struct tog_log_view_state *s = &view->state.log;
3978 const struct got_error *err = NULL;
3979 int eos = view->nlines - 2;
3981 if (s->first_displayed_entry == NULL)
3984 if (s->thread_args.log_complete &&
3985 s->selected_entry->idx >= s->commits->ncommits - 1)
3988 if (view_is_hsplit_top(view))
3989 --eos; /* border consumes the last line */
3992 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3995 err = log_scroll_down(view, 1);
3996 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3997 struct commit_queue_entry *entry;
4001 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
4002 s->last_displayed_entry = entry;
4003 for (n = 0; n <= eos; n++) {
4006 s->first_displayed_entry = entry;
4007 entry = TAILQ_PREV(entry, commit_queue_head, entry);
4010 s->selected = n - 1;
4012 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
4013 s->thread_args.log_complete)
4014 s->selected += MIN(page,
4015 s->commits->ncommits - s->selected_entry->idx - 1);
4017 err = log_scroll_down(view, page);
4023 * We might necessarily overshoot in horizontal
4024 * splits; if so, select the last displayed commit.
4026 if (s->first_displayed_entry && s->last_displayed_entry) {
4027 s->selected = MIN(s->selected,
4028 s->last_displayed_entry->idx -
4029 s->first_displayed_entry->idx);
4034 if (s->thread_args.log_complete &&
4035 s->selected_entry->idx == s->commits->ncommits - 1)
4042 view_get_split(struct tog_view *view, int *y, int *x)
4047 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
4048 if (view->child && view->child->resized_y)
4049 *y = view->child->resized_y;
4050 else if (view->resized_y)
4051 *y = view->resized_y;
4053 *y = view_split_begin_y(view->lines);
4054 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
4055 if (view->child && view->child->resized_x)
4056 *x = view->child->resized_x;
4057 else if (view->resized_x)
4058 *x = view->resized_x;
4060 *x = view_split_begin_x(view->begin_x);
4064 /* Split view horizontally at y and offset view->state->selected line. */
4065 static const struct got_error *
4066 view_init_hsplit(struct tog_view *view, int y)
4068 const struct got_error *err = NULL;
4072 err = view_resize(view);
4076 err = offset_selection_down(view);
4081 static const struct got_error *
4082 log_goto_line(struct tog_view *view, int nlines)
4084 const struct got_error *err = NULL;
4085 struct tog_log_view_state *s = &view->state.log;
4086 int g, idx = s->selected_entry->idx;
4088 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
4094 if (g >= s->first_displayed_entry->idx + 1 &&
4095 g <= s->last_displayed_entry->idx + 1 &&
4096 g - s->first_displayed_entry->idx - 1 < nlines) {
4097 s->selected = g - s->first_displayed_entry->idx - 1;
4103 err = log_move_cursor_down(view, g - idx - 1);
4104 if (!err && g > s->selected_entry->idx + 1)
4105 err = log_move_cursor_down(view,
4106 g - s->first_displayed_entry->idx - 1);
4109 } else if (idx + 1 > g)
4110 log_move_cursor_up(view, idx - g + 1, 0);
4112 if (g < nlines && s->first_displayed_entry->idx == 0)
4113 s->selected = g - 1;
4121 horizontal_scroll_input(struct tog_view *view, int ch)
4127 view->x -= MIN(view->x, 2);
4133 if (view->x + view->ncols / 2 < view->maxx)
4142 view->x = MAX(view->maxx - view->ncols / 2, 0);
4150 static const struct got_error *
4151 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
4153 const struct got_error *err = NULL;
4154 struct tog_log_view_state *s = &view->state.log;
4157 if (s->thread_args.load_all) {
4158 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
4159 s->thread_args.load_all = 0;
4160 else if (s->thread_args.log_complete) {
4161 err = log_move_cursor_down(view, s->commits->ncommits);
4162 s->thread_args.load_all = 0;
4168 eos = nscroll = view->nlines - 1;
4169 if (view_is_hsplit_top(view))
4173 return log_goto_line(view, eos);
4177 err = limit_log_view(view);
4188 horizontal_scroll_input(view, ch);
4195 log_move_cursor_up(view, 0, 0);
4200 log_move_cursor_up(view, 0, 1);
4210 log_move_cursor_up(view, nscroll, 0);
4217 err = log_move_cursor_down(view, 0);
4220 s->use_committer = !s->use_committer;
4221 view->action = s->use_committer ?
4222 "show committer" : "show commit author";
4227 /* We don't know yet how many commits, so we're forced to
4228 * traverse them all. */
4230 s->thread_args.load_all = 1;
4231 if (!s->thread_args.log_complete)
4232 return trigger_log_thread(view, 0);
4233 err = log_move_cursor_down(view, s->commits->ncommits);
4234 s->thread_args.load_all = 0;
4245 err = log_move_cursor_down(view, nscroll);
4248 if (s->selected > view->nlines - 2)
4249 s->selected = view->nlines - 2;
4250 if (s->selected > s->commits->ncommits - 1)
4251 s->selected = s->commits->ncommits - 1;
4253 if (s->commits->ncommits < view->nlines - 1 &&
4254 !s->thread_args.log_complete) {
4255 s->thread_args.commits_needed += (view->nlines - 1) -
4256 s->commits->ncommits;
4257 err = trigger_log_thread(view, 1);
4263 if (s->selected_entry == NULL)
4265 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4269 if (s->selected_entry == NULL)
4271 err = view_request_new(new_view, view, TOG_VIEW_TREE);
4277 if (ch == KEY_BACKSPACE &&
4278 got_path_is_root_dir(s->in_repo_path))
4280 err = stop_log_thread(s);
4283 if (ch == KEY_BACKSPACE) {
4285 err = got_path_dirname(&parent_path, s->in_repo_path);
4288 free(s->in_repo_path);
4289 s->in_repo_path = parent_path;
4290 s->thread_args.in_repo_path = s->in_repo_path;
4291 } else if (ch == CTRL('l')) {
4292 struct got_object_id *start_id;
4293 err = got_repo_match_object_id(&start_id, NULL,
4294 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4295 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4297 if (s->head_ref_name == NULL ||
4298 err->code != GOT_ERR_NOT_REF)
4300 /* Try to cope with deleted references. */
4301 free(s->head_ref_name);
4302 s->head_ref_name = NULL;
4303 err = got_repo_match_object_id(&start_id,
4304 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4305 &tog_refs, s->repo);
4310 s->start_id = start_id;
4311 s->thread_args.start_id = s->start_id;
4313 s->log_branches = !s->log_branches;
4315 if (s->thread_args.pack_fds == NULL) {
4316 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4320 err = got_repo_open(&s->thread_args.repo,
4321 got_repo_get_path(s->repo), NULL,
4322 s->thread_args.pack_fds);
4326 err = tog_load_refs(s->repo, 0);
4329 err = got_commit_graph_open(&s->thread_args.graph,
4330 s->in_repo_path, !s->log_branches);
4333 err = got_commit_graph_iter_start(s->thread_args.graph,
4334 s->start_id, s->repo, NULL, NULL);
4337 free_commits(&s->real_commits);
4338 free_commits(&s->limit_commits);
4339 s->first_displayed_entry = NULL;
4340 s->last_displayed_entry = NULL;
4341 s->selected_entry = NULL;
4343 s->thread_args.log_complete = 0;
4345 s->thread_args.commits_needed = view->lines;
4346 s->matched_entry = NULL;
4347 s->search_entry = NULL;
4352 err = view_request_new(new_view, view, TOG_VIEW_REF);
4362 static const struct got_error *
4363 apply_unveil(const char *repo_path, const char *worktree_path)
4365 const struct got_error *error;
4368 if (unveil("gmon.out", "rwc") != 0)
4369 return got_error_from_errno2("unveil", "gmon.out");
4371 if (repo_path && unveil(repo_path, "r") != 0)
4372 return got_error_from_errno2("unveil", repo_path);
4374 if (worktree_path && unveil(worktree_path, "rwc") != 0)
4375 return got_error_from_errno2("unveil", worktree_path);
4377 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4378 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4380 error = got_privsep_unveil_exec_helpers();
4384 if (unveil(NULL, NULL) != 0)
4385 return got_error_from_errno("unveil");
4390 static const struct got_error *
4391 init_mock_term(const char *test_script_path)
4393 const struct got_error *err = NULL;
4394 const char *screen_dump_path;
4397 if (test_script_path == NULL || *test_script_path == '\0')
4398 return got_error_msg(GOT_ERR_IO, "TOG_TEST_SCRIPT not defined");
4400 tog_io.f = fopen(test_script_path, "re");
4401 if (tog_io.f == NULL) {
4402 err = got_error_from_errno_fmt("fopen: %s",
4407 /* test mode, we don't want any output */
4408 tog_io.cout = fopen("/dev/null", "w+");
4409 if (tog_io.cout == NULL) {
4410 err = got_error_from_errno2("fopen", "/dev/null");
4414 in = dup(fileno(tog_io.cout));
4416 err = got_error_from_errno("dup");
4419 tog_io.cin = fdopen(in, "r");
4420 if (tog_io.cin == NULL) {
4421 err = got_error_from_errno("fdopen");
4426 screen_dump_path = getenv("TOG_SCR_DUMP");
4427 if (screen_dump_path == NULL || *screen_dump_path == '\0')
4428 return got_error_msg(GOT_ERR_IO, "TOG_SCR_DUMP not defined");
4429 tog_io.sdump = fopen(screen_dump_path, "we");
4430 if (tog_io.sdump == NULL) {
4431 err = got_error_from_errno2("fopen", screen_dump_path);
4435 if (fseeko(tog_io.f, 0L, SEEK_SET) == -1) {
4436 err = got_error_from_errno("fseeko");
4440 if (newterm(NULL, tog_io.cout, tog_io.cin) == NULL)
4441 err = got_error_msg(GOT_ERR_IO,
4442 "newterm: failed to initialise curses");
4455 if (using_mock_io) /* In test mode we use a fake terminal */
4461 halfdelay(1); /* Fast refresh while initial view is loading. */
4464 intrflush(stdscr, FALSE);
4465 keypad(stdscr, TRUE);
4467 if (getenv("TOG_COLORS") != NULL) {
4469 use_default_colors();
4475 static const struct got_error *
4476 set_tog_base_commit(struct got_repository *repo, struct got_worktree *worktree)
4478 tog_base_commit.id = got_object_id_dup(
4479 got_worktree_get_base_commit_id(worktree));
4480 if (tog_base_commit.id == NULL)
4481 return got_error_from_errno( "got_object_id_dup");
4486 static const struct got_error *
4487 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4488 struct got_repository *repo, struct got_worktree *worktree)
4490 const struct got_error *err = NULL;
4493 *in_repo_path = strdup("/");
4494 if (*in_repo_path == NULL)
4495 return got_error_from_errno("strdup");
4500 const char *prefix = got_worktree_get_path_prefix(worktree);
4503 err = got_worktree_resolve_path(&p, worktree, argv[0]);
4506 if (asprintf(in_repo_path, "%s%s%s", prefix,
4507 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4509 err = got_error_from_errno("asprintf");
4510 *in_repo_path = NULL;
4514 err = got_repo_map_path(in_repo_path, repo, argv[0]);
4519 static const struct got_error *
4520 cmd_log(int argc, char *argv[])
4522 const struct got_error *error;
4523 struct got_repository *repo = NULL;
4524 struct got_worktree *worktree = NULL;
4525 struct got_object_id *start_id = NULL;
4526 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4527 char *keyword_idstr = NULL, *start_commit = NULL, *label = NULL;
4528 struct got_reference *ref = NULL;
4529 const char *head_ref_name = NULL;
4530 int ch, log_branches = 0;
4531 struct tog_view *view;
4532 int *pack_fds = NULL;
4534 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4540 start_commit = optarg;
4543 repo_path = realpath(optarg, NULL);
4544 if (repo_path == NULL)
4545 return got_error_from_errno2("realpath",
4560 error = got_repo_pack_fds_open(&pack_fds);
4564 if (repo_path == NULL) {
4565 cwd = getcwd(NULL, 0);
4567 error = got_error_from_errno("getcwd");
4570 error = got_worktree_open(&worktree, cwd, NULL);
4571 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4575 strdup(got_worktree_get_repo_path(worktree));
4577 repo_path = strdup(cwd);
4578 if (repo_path == NULL) {
4579 error = got_error_from_errno("strdup");
4584 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4588 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4595 error = apply_unveil(got_repo_get_path(repo),
4596 worktree ? got_worktree_get_root_path(worktree) : NULL);
4600 /* already loaded by tog_log_with_path()? */
4601 if (TAILQ_EMPTY(&tog_refs)) {
4602 error = tog_load_refs(repo, 0);
4607 if (start_commit == NULL) {
4608 error = got_repo_match_object_id(&start_id, &label,
4609 worktree ? got_worktree_get_head_ref_name(worktree) :
4610 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4613 head_ref_name = label;
4615 error = got_keyword_to_idstr(&keyword_idstr, start_commit,
4619 if (keyword_idstr != NULL)
4620 start_commit = keyword_idstr;
4622 error = got_ref_open(&ref, repo, start_commit, 0);
4624 head_ref_name = got_ref_get_name(ref);
4625 else if (error->code != GOT_ERR_NOT_REF)
4627 error = got_repo_match_object_id(&start_id, NULL,
4628 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4633 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4635 error = got_error_from_errno("view_open");
4640 error = set_tog_base_commit(repo, worktree);
4645 error = open_log_view(view, start_id, repo, head_ref_name,
4646 in_repo_path, log_branches, worktree);
4651 /* The work tree will be closed by the log thread. */
4655 error = view_loop(view);
4658 free(tog_base_commit.id);
4659 free(keyword_idstr);
4668 const struct got_error *close_err = got_repo_close(repo);
4673 got_worktree_close(worktree);
4675 const struct got_error *pack_err =
4676 got_repo_pack_fds_close(pack_fds);
4688 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4689 "object1 object2\n", getprogname());
4694 match_line(const char *line, regex_t *regex, size_t nmatch,
4695 regmatch_t *regmatch)
4697 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4700 static struct tog_color *
4701 match_color(struct tog_colors *colors, const char *line)
4703 struct tog_color *tc = NULL;
4705 STAILQ_FOREACH(tc, colors, entry) {
4706 if (match_line(line, &tc->regex, 0, NULL))
4713 static const struct got_error *
4714 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4715 WINDOW *window, int skipcol, regmatch_t *regmatch)
4717 const struct got_error *err = NULL;
4719 wchar_t *wline = NULL;
4720 int rme, rms, n, width, scrollx;
4721 int width0 = 0, width1 = 0, width2 = 0;
4722 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4726 rms = regmatch->rm_so;
4727 rme = regmatch->rm_eo;
4729 err = expand_tab(&exstr, line);
4733 /* Split the line into 3 segments, according to match offsets. */
4734 seg0 = strndup(exstr, rms);
4736 err = got_error_from_errno("strndup");
4739 seg1 = strndup(exstr + rms, rme - rms);
4741 err = got_error_from_errno("strndup");
4744 seg2 = strdup(exstr + rme);
4746 err = got_error_from_errno("strndup");
4750 /* draw up to matched token if we haven't scrolled past it */
4751 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4755 n = MAX(width0 - skipcol, 0);
4758 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4759 wlimit, col_tab_align, 1);
4762 waddwstr(window, &wline[scrollx]);
4772 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4776 wlen = wcslen(wline);
4778 width = wcwidth(wline[i]);
4780 /* should not happen, tabs are expanded */
4781 err = got_error(GOT_ERR_RANGE);
4784 if (width0 + w + width > skipcol)
4789 /* draw (visible part of) matched token (if scrolled into it) */
4790 if (width1 - w > 0) {
4791 wattron(window, A_STANDOUT);
4792 waddwstr(window, &wline[i]);
4793 wattroff(window, A_STANDOUT);
4794 wlimit -= (width1 - w);
4795 *wtotal += (width1 - w);
4799 if (wlimit > 0) { /* draw rest of line */
4801 if (skipcol > width0 + width1) {
4802 err = format_line(&wline, &width2, &scrollx, seg2,
4803 skipcol - (width0 + width1), wlimit,
4807 waddwstr(window, &wline[scrollx]);
4809 err = format_line(&wline, &width2, NULL, seg2, 0,
4810 wlimit, col_tab_align, 1);
4813 waddwstr(window, wline);
4827 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4830 int *eof, *first, *selected;
4832 if (view->type == TOG_VIEW_DIFF) {
4833 struct tog_diff_view_state *s = &view->state.diff;
4835 first = &s->first_displayed_line;
4839 } else if (view->type == TOG_VIEW_HELP) {
4840 struct tog_help_view_state *s = &view->state.help;
4842 first = &s->first_displayed_line;
4846 } else if (view->type == TOG_VIEW_BLAME) {
4847 struct tog_blame_view_state *s = &view->state.blame;
4849 first = &s->first_displayed_line;
4850 selected = &s->selected_line;
4856 /* Center gline in the middle of the page like vi(1). */
4857 if (*lineno < view->gline - (view->nlines - 3) / 2)
4859 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4868 *selected = view->gline <= (view->nlines - 3) / 2 ?
4869 view->gline : (view->nlines - 3) / 2 + 1;
4875 static const struct got_error *
4876 draw_file(struct tog_view *view, const char *header)
4878 struct tog_diff_view_state *s = &view->state.diff;
4879 regmatch_t *regmatch = &view->regmatch;
4880 const struct got_error *err;
4883 size_t linesize = 0;
4887 int max_lines = view->nlines;
4888 int nlines = s->nlines;
4891 s->lineno = s->first_displayed_line - 1;
4892 line_offset = s->lines[s->first_displayed_line - 1].offset;
4893 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4894 return got_error_from_errno("fseek");
4896 werase(view->window);
4898 if (view->gline > s->nlines - 1)
4899 view->gline = s->nlines - 1;
4902 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4903 1 : view->gline - (view->nlines - 3) / 2 :
4904 s->lineno + s->selected_line;
4906 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4907 return got_error_from_errno("asprintf");
4908 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4914 if (view_needs_focus_indication(view))
4915 wstandout(view->window);
4916 waddwstr(view->window, wline);
4919 while (width++ < view->ncols)
4920 waddch(view->window, ' ');
4921 if (view_needs_focus_indication(view))
4922 wstandend(view->window);
4932 while (max_lines > 0 && nprinted < max_lines) {
4933 enum got_diff_line_type linetype;
4936 linelen = getline(&line, &linesize, s->f);
4937 if (linelen == -1) {
4943 return got_ferror(s->f, GOT_ERR_IO);
4946 if (++s->lineno < s->first_displayed_line)
4948 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4950 if (s->lineno == view->hiline)
4953 /* Set view->maxx based on full line length. */
4954 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4960 view->maxx = MAX(view->maxx, width);
4964 linetype = s->lines[s->lineno].type;
4965 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4966 linetype < GOT_DIFF_LINE_CONTEXT)
4967 attr |= COLOR_PAIR(linetype);
4969 wattron(view->window, attr);
4970 if (s->first_displayed_line + nprinted == s->matched_line &&
4971 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4972 err = add_matched_line(&width, line, view->ncols, 0,
4973 view->window, view->x, regmatch);
4980 err = format_line(&wline, &width, &skip, line,
4981 view->x, view->ncols, 0, view->x ? 1 : 0);
4986 waddwstr(view->window, &wline[skip]);
4990 if (s->lineno == view->hiline) {
4991 /* highlight full gline length */
4992 while (width++ < view->ncols)
4993 waddch(view->window, ' ');
4995 if (width <= view->ncols - 1)
4996 waddch(view->window, '\n');
4999 wattroff(view->window, attr);
5000 if (++nprinted == 1)
5001 s->first_displayed_line = s->lineno;
5005 s->last_displayed_line = s->first_displayed_line +
5008 s->last_displayed_line = s->first_displayed_line;
5013 while (nprinted < view->nlines) {
5014 waddch(view->window, '\n');
5018 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
5024 wstandout(view->window);
5025 waddwstr(view->window, wline);
5028 wstandend(view->window);
5035 get_datestr(time_t *time, char *datebuf)
5037 struct tm mytm, *tm;
5040 tm = gmtime_r(time, &mytm);
5043 s = asctime_r(tm, datebuf);
5046 p = strchr(s, '\n');
5052 static const struct got_error *
5053 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
5054 off_t off, uint8_t type)
5056 struct got_diff_line *p;
5058 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
5060 return got_error_from_errno("reallocarray");
5062 (*lines)[*nlines].offset = off;
5063 (*lines)[*nlines].type = type;
5069 static const struct got_error *
5070 cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
5071 struct got_diff_line *s_lines, size_t s_nlines)
5073 struct got_diff_line *p;
5077 if (fseeko(src, 0L, SEEK_SET) == -1)
5078 return got_error_from_errno("fseeko");
5081 r = fread(buf, 1, sizeof(buf), src);
5084 return got_error_from_errno("fread");
5088 if (fwrite(buf, 1, r, dst) != r)
5089 return got_ferror(dst, GOT_ERR_IO);
5092 if (s_nlines == 0 && *d_nlines == 0)
5096 * If commit info was in dst, increment line offsets
5097 * of the appended diff content, but skip s_lines[0]
5098 * because offset zero is already in *d_lines.
5100 if (*d_nlines > 0) {
5101 for (i = 1; i < s_nlines; ++i)
5102 s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
5110 p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
5112 /* d_lines is freed in close_diff_view() */
5113 return got_error_from_errno("reallocarray");
5118 memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
5119 *d_nlines += s_nlines;
5124 static const struct got_error *
5125 write_commit_info(struct got_diff_line **lines, size_t *nlines,
5126 struct got_object_id *commit_id, struct got_reflist_head *refs,
5127 struct got_repository *repo, int ignore_ws, int force_text_diff,
5128 struct got_diffstat_cb_arg *dsa, FILE *outfile)
5130 const struct got_error *err = NULL;
5131 char datebuf[26], *datestr;
5132 struct got_commit_object *commit;
5133 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
5134 time_t committer_time;
5135 const char *author, *committer;
5136 char *refs_str = NULL;
5137 struct got_pathlist_entry *pe;
5141 err = build_refs_str(&refs_str, refs, commit_id, repo);
5145 err = got_object_open_as_commit(&commit, repo, commit_id);
5149 err = got_object_id_str(&id_str, commit_id);
5151 err = got_error_from_errno("got_object_id_str");
5155 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
5159 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
5160 refs_str ? refs_str : "", refs_str ? ")" : "");
5162 err = got_error_from_errno("fprintf");
5166 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
5170 n = fprintf(outfile, "from: %s\n",
5171 got_object_commit_get_author(commit));
5173 err = got_error_from_errno("fprintf");
5177 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
5181 author = got_object_commit_get_author(commit);
5182 committer = got_object_commit_get_committer(commit);
5183 if (strcmp(author, committer) != 0) {
5184 n = fprintf(outfile, "via: %s\n", committer);
5186 err = got_error_from_errno("fprintf");
5190 err = add_line_metadata(lines, nlines, outoff,
5191 GOT_DIFF_LINE_AUTHOR);
5195 committer_time = got_object_commit_get_committer_time(commit);
5196 datestr = get_datestr(&committer_time, datebuf);
5198 n = fprintf(outfile, "date: %s UTC\n", datestr);
5200 err = got_error_from_errno("fprintf");
5204 err = add_line_metadata(lines, nlines, outoff,
5205 GOT_DIFF_LINE_DATE);
5209 if (got_object_commit_get_nparents(commit) > 1) {
5210 const struct got_object_id_queue *parent_ids;
5211 struct got_object_qid *qid;
5213 parent_ids = got_object_commit_get_parent_ids(commit);
5214 STAILQ_FOREACH(qid, parent_ids, entry) {
5215 err = got_object_id_str(&id_str, &qid->id);
5218 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
5220 err = got_error_from_errno("fprintf");
5224 err = add_line_metadata(lines, nlines, outoff,
5225 GOT_DIFF_LINE_META);
5233 err = got_object_commit_get_logmsg(&logmsg, commit);
5237 while ((line = strsep(&s, "\n")) != NULL) {
5238 n = fprintf(outfile, "%s\n", line);
5240 err = got_error_from_errno("fprintf");
5244 err = add_line_metadata(lines, nlines, outoff,
5245 GOT_DIFF_LINE_LOGMSG);
5250 TAILQ_FOREACH(pe, dsa->paths, entry) {
5251 struct got_diff_changed_path *cp = pe->data;
5252 int pad = dsa->max_path_len - pe->path_len + 1;
5254 n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
5255 pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
5256 dsa->rm_cols + 1, cp->rm);
5258 err = got_error_from_errno("fprintf");
5262 err = add_line_metadata(lines, nlines, outoff,
5263 GOT_DIFF_LINE_CHANGES);
5268 fputc('\n', outfile);
5270 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5274 n = fprintf(outfile,
5275 "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5276 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5277 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5279 err = got_error_from_errno("fprintf");
5283 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5287 fputc('\n', outfile);
5289 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5294 got_object_commit_close(commit);
5303 static const struct got_error *
5304 create_diff(struct tog_diff_view_state *s)
5306 const struct got_error *err = NULL;
5307 FILE *f = NULL, *tmp_diff_file = NULL;
5309 struct got_diff_line *lines = NULL;
5310 struct got_pathlist_head changed_paths;
5311 struct got_commit_object *commit2 = NULL;
5313 TAILQ_INIT(&changed_paths);
5316 s->lines = malloc(sizeof(*s->lines));
5317 if (s->lines == NULL)
5318 return got_error_from_errno("malloc");
5323 err = got_error_from_errno("got_opentemp");
5326 tmp_diff_file = got_opentemp();
5327 if (tmp_diff_file == NULL) {
5328 err = got_error_from_errno("got_opentemp");
5331 if (s->f && fclose(s->f) == EOF) {
5332 err = got_error_from_errno("fclose");
5338 err = got_object_get_type(&obj_type, s->repo, s->id1);
5340 err = got_object_get_type(&obj_type, s->repo, s->id2);
5345 case GOT_OBJ_TYPE_BLOB:
5346 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5347 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5348 s->label1, s->label2, tog_diff_algo, s->diff_context,
5349 s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5352 case GOT_OBJ_TYPE_TREE:
5353 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5354 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5355 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5356 s->force_text_diff, NULL, s->repo, s->f);
5358 case GOT_OBJ_TYPE_COMMIT: {
5359 const struct got_object_id_queue *parent_ids;
5360 struct got_object_qid *pid;
5361 struct got_reflist_head *refs;
5363 struct got_diffstat_cb_arg dsa = {
5366 s->ignore_whitespace,
5371 lines = malloc(sizeof(*lines));
5372 if (lines == NULL) {
5373 err = got_error_from_errno("malloc");
5377 /* build diff first in tmp file then append to commit info */
5378 err = got_diff_objects_as_commits(&lines, &nlines,
5379 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5380 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5381 s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5385 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5386 /* Show commit info if we're diffing to a parent/root commit. */
5387 if (s->id1 == NULL) {
5388 err = write_commit_info(&s->lines, &s->nlines, s->id2,
5389 refs, s->repo, s->ignore_whitespace,
5390 s->force_text_diff, &dsa, s->f);
5394 err = got_object_open_as_commit(&commit2, s->repo,
5399 parent_ids = got_object_commit_get_parent_ids(commit2);
5400 STAILQ_FOREACH(pid, parent_ids, entry) {
5401 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5402 err = write_commit_info(&s->lines,
5403 &s->nlines, s->id2, refs, s->repo,
5404 s->ignore_whitespace,
5405 s->force_text_diff, &dsa, s->f);
5413 err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5418 err = got_error(GOT_ERR_OBJ_TYPE);
5423 if (commit2 != NULL)
5424 got_object_commit_close(commit2);
5425 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5426 if (s->f && fflush(s->f) != 0 && err == NULL)
5427 err = got_error_from_errno("fflush");
5428 if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5429 err = got_error_from_errno("fclose");
5434 diff_view_indicate_progress(struct tog_view *view)
5436 mvwaddstr(view->window, 0, 0, "diffing...");
5441 static const struct got_error *
5442 search_start_diff_view(struct tog_view *view)
5444 struct tog_diff_view_state *s = &view->state.diff;
5446 s->matched_line = 0;
5451 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5452 size_t *nlines, int **first, int **last, int **match, int **selected)
5454 struct tog_diff_view_state *s = &view->state.diff;
5457 *nlines = s->nlines;
5458 *line_offsets = NULL;
5459 *match = &s->matched_line;
5460 *first = &s->first_displayed_line;
5461 *last = &s->last_displayed_line;
5462 *selected = &s->selected_line;
5465 static const struct got_error *
5466 search_next_view_match(struct tog_view *view)
5468 const struct got_error *err = NULL;
5472 size_t linesize = 0;
5474 off_t *line_offsets;
5476 int *first, *last, *match, *selected;
5478 if (!view->search_setup)
5479 return got_error_msg(GOT_ERR_NOT_IMPL,
5480 "view search not supported");
5481 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5484 if (!view->searching) {
5485 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5490 if (view->searching == TOG_SEARCH_FORWARD)
5491 lineno = *first + 1;
5493 lineno = *first - 1;
5495 lineno = *first - 1 + *selected;
5500 if (lineno <= 0 || lineno > nlines) {
5502 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5506 if (view->searching == TOG_SEARCH_FORWARD)
5512 offset = view->type == TOG_VIEW_DIFF ?
5513 view->state.diff.lines[lineno - 1].offset :
5514 line_offsets[lineno - 1];
5515 if (fseeko(f, offset, SEEK_SET) != 0) {
5517 return got_error_from_errno("fseeko");
5519 linelen = getline(&line, &linesize, f);
5520 if (linelen != -1) {
5522 err = expand_tab(&exstr, line);
5525 if (match_line(exstr, &view->regex, 1,
5527 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5534 if (view->searching == TOG_SEARCH_FORWARD)
5549 static const struct got_error *
5550 close_diff_view(struct tog_view *view)
5552 const struct got_error *err = NULL;
5553 struct tog_diff_view_state *s = &view->state.diff;
5559 if (s->f && fclose(s->f) == EOF)
5560 err = got_error_from_errno("fclose");
5562 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5563 err = got_error_from_errno("fclose");
5565 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5566 err = got_error_from_errno("fclose");
5568 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5569 err = got_error_from_errno("close");
5571 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5572 err = got_error_from_errno("close");
5580 static const struct got_error *
5581 open_diff_view(struct tog_view *view, struct got_object_id *id1,
5582 struct got_object_id *id2, const char *label1, const char *label2,
5583 int diff_context, int ignore_whitespace, int force_text_diff,
5584 struct tog_view *parent_view, struct got_repository *repo)
5586 const struct got_error *err;
5587 struct tog_diff_view_state *s = &view->state.diff;
5589 memset(s, 0, sizeof(*s));
5593 if (id1 != NULL && id2 != NULL) {
5596 err = got_object_get_type(&type1, repo, id1);
5599 err = got_object_get_type(&type2, repo, id2);
5603 if (type1 != type2) {
5604 err = got_error(GOT_ERR_OBJ_TYPE);
5608 s->first_displayed_line = 1;
5609 s->last_displayed_line = view->nlines;
5610 s->selected_line = 1;
5616 s->id1 = got_object_id_dup(id1);
5617 if (s->id1 == NULL) {
5618 err = got_error_from_errno("got_object_id_dup");
5624 s->id2 = got_object_id_dup(id2);
5625 if (s->id2 == NULL) {
5626 err = got_error_from_errno("got_object_id_dup");
5630 s->f1 = got_opentemp();
5631 if (s->f1 == NULL) {
5632 err = got_error_from_errno("got_opentemp");
5636 s->f2 = got_opentemp();
5637 if (s->f2 == NULL) {
5638 err = got_error_from_errno("got_opentemp");
5642 s->fd1 = got_opentempfd();
5644 err = got_error_from_errno("got_opentempfd");
5648 s->fd2 = got_opentempfd();
5650 err = got_error_from_errno("got_opentempfd");
5654 s->diff_context = diff_context;
5655 s->ignore_whitespace = ignore_whitespace;
5656 s->force_text_diff = force_text_diff;
5657 s->parent_view = parent_view;
5660 if (has_colors() && getenv("TOG_COLORS") != NULL && !using_mock_io) {
5663 rc = init_pair(GOT_DIFF_LINE_MINUS,
5664 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5666 rc = init_pair(GOT_DIFF_LINE_PLUS,
5667 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5669 rc = init_pair(GOT_DIFF_LINE_HUNK,
5670 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5672 rc = init_pair(GOT_DIFF_LINE_META,
5673 get_color_value("TOG_COLOR_DIFF_META"), -1);
5675 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5676 get_color_value("TOG_COLOR_DIFF_META"), -1);
5678 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5679 get_color_value("TOG_COLOR_DIFF_META"), -1);
5681 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5682 get_color_value("TOG_COLOR_DIFF_META"), -1);
5684 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5685 get_color_value("TOG_COLOR_AUTHOR"), -1);
5687 rc = init_pair(GOT_DIFF_LINE_DATE,
5688 get_color_value("TOG_COLOR_DATE"), -1);
5690 err = got_error(GOT_ERR_RANGE);
5695 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5696 view_is_splitscreen(view))
5697 show_log_view(parent_view); /* draw border */
5698 diff_view_indicate_progress(view);
5700 err = create_diff(s);
5702 view->show = show_diff_view;
5703 view->input = input_diff_view;
5704 view->reset = reset_diff_view;
5705 view->close = close_diff_view;
5706 view->search_start = search_start_diff_view;
5707 view->search_setup = search_setup_diff_view;
5708 view->search_next = search_next_view_match;
5711 if (view->close == NULL)
5712 close_diff_view(view);
5718 static const struct got_error *
5719 show_diff_view(struct tog_view *view)
5721 const struct got_error *err;
5722 struct tog_diff_view_state *s = &view->state.diff;
5723 char *id_str1 = NULL, *id_str2, *header;
5724 const char *label1, *label2;
5727 err = got_object_id_str(&id_str1, s->id1);
5730 label1 = s->label1 ? s->label1 : id_str1;
5732 label1 = "/dev/null";
5734 err = got_object_id_str(&id_str2, s->id2);
5737 label2 = s->label2 ? s->label2 : id_str2;
5739 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5740 err = got_error_from_errno("asprintf");
5748 err = draw_file(view, header);
5753 static const struct got_error *
5754 set_selected_commit(struct tog_diff_view_state *s,
5755 struct commit_queue_entry *entry)
5757 const struct got_error *err;
5758 const struct got_object_id_queue *parent_ids;
5759 struct got_commit_object *selected_commit;
5760 struct got_object_qid *pid;
5763 s->id2 = got_object_id_dup(entry->id);
5765 return got_error_from_errno("got_object_id_dup");
5767 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5770 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5772 pid = STAILQ_FIRST(parent_ids);
5773 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5774 got_object_commit_close(selected_commit);
5778 static const struct got_error *
5779 reset_diff_view(struct tog_view *view)
5781 struct tog_diff_view_state *s = &view->state.diff;
5784 wclear(view->window);
5785 s->first_displayed_line = 1;
5786 s->last_displayed_line = view->nlines;
5787 s->matched_line = 0;
5788 diff_view_indicate_progress(view);
5789 return create_diff(s);
5793 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5797 i = start = s->first_displayed_line - 1;
5799 while (s->lines[i].type != type) {
5803 return; /* do nothing, requested type not in file */
5806 s->selected_line = 1;
5807 s->first_displayed_line = i;
5811 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5815 i = start = s->first_displayed_line + 1;
5817 while (s->lines[i].type != type) {
5818 if (i == s->nlines - 1)
5821 return; /* do nothing, requested type not in file */
5824 s->selected_line = 1;
5825 s->first_displayed_line = i;
5828 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5830 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5833 static const struct got_error *
5834 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5836 const struct got_error *err = NULL;
5837 struct tog_diff_view_state *s = &view->state.diff;
5838 struct tog_log_view_state *ls;
5839 struct commit_queue_entry *old_selected_entry;
5841 size_t linesize = 0;
5843 int i, nscroll = view->nlines - 1, up = 0;
5845 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5854 horizontal_scroll_input(view, ch);
5859 s->force_text_diff = !s->force_text_diff;
5860 view->action = s->force_text_diff ?
5861 "force ASCII text enabled" :
5862 "force ASCII text disabled";
5864 else if (ch == 'w') {
5865 s->ignore_whitespace = !s->ignore_whitespace;
5866 view->action = s->ignore_whitespace ?
5867 "ignore whitespace enabled" :
5868 "ignore whitespace disabled";
5870 err = reset_diff_view(view);
5874 s->first_displayed_line = 1;
5883 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5889 if (s->first_displayed_line > 1)
5890 s->first_displayed_line--;
5901 if (s->first_displayed_line == 1) {
5906 while (i++ < nscroll && s->first_displayed_line > 1)
5907 s->first_displayed_line--;
5913 s->first_displayed_line++;
5930 while (!s->eof && i++ < nscroll) {
5931 linelen = getline(&line, &linesize, s->f);
5932 s->first_displayed_line++;
5933 if (linelen == -1) {
5937 err = got_ferror(s->f, GOT_ERR_IO);
5944 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5947 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5950 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5953 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5956 if (s->diff_context > 0) {
5958 s->matched_line = 0;
5959 diff_view_indicate_progress(view);
5960 err = create_diff(s);
5961 if (s->first_displayed_line + view->nlines - 1 >
5963 s->first_displayed_line = 1;
5964 s->last_displayed_line = view->nlines;
5970 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5972 s->matched_line = 0;
5973 diff_view_indicate_progress(view);
5974 err = create_diff(s);
5986 if (s->parent_view == NULL) {
5990 s->parent_view->count = view->count;
5992 if (s->parent_view->type == TOG_VIEW_LOG) {
5993 ls = &s->parent_view->state.log;
5994 old_selected_entry = ls->selected_entry;
5996 err = input_log_view(NULL, s->parent_view,
5997 up ? KEY_UP : KEY_DOWN);
6000 view->count = s->parent_view->count;
6002 if (old_selected_entry == ls->selected_entry)
6005 err = set_selected_commit(s, ls->selected_entry);
6008 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
6009 struct tog_blame_view_state *bs;
6010 struct got_object_id *id, *prev_id;
6012 bs = &s->parent_view->state.blame;
6013 prev_id = get_annotation_for_line(bs->blame.lines,
6014 bs->blame.nlines, bs->last_diffed_line);
6016 err = input_blame_view(&view, s->parent_view,
6017 up ? KEY_UP : KEY_DOWN);
6020 view->count = s->parent_view->count;
6022 if (prev_id == NULL)
6024 id = get_selected_commit_id(bs->blame.lines,
6025 bs->blame.nlines, bs->first_displayed_line,
6030 if (!got_object_id_cmp(prev_id, id))
6033 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
6037 s->first_displayed_line = 1;
6038 s->last_displayed_line = view->nlines;
6039 s->matched_line = 0;
6042 diff_view_indicate_progress(view);
6043 err = create_diff(s);
6053 static const struct got_error *
6054 cmd_diff(int argc, char *argv[])
6056 const struct got_error *error;
6057 struct got_repository *repo = NULL;
6058 struct got_worktree *worktree = NULL;
6059 struct got_object_id *id1 = NULL, *id2 = NULL;
6060 char *repo_path = NULL, *cwd = NULL;
6061 char *id_str1 = NULL, *id_str2 = NULL;
6062 char *keyword_idstr1 = NULL, *keyword_idstr2 = NULL;
6063 char *label1 = NULL, *label2 = NULL;
6064 int diff_context = 3, ignore_whitespace = 0;
6065 int ch, force_text_diff = 0;
6067 struct tog_view *view;
6068 int *pack_fds = NULL;
6070 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
6073 force_text_diff = 1;
6076 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
6079 errx(1, "number of context lines is %s: %s",
6083 repo_path = realpath(optarg, NULL);
6084 if (repo_path == NULL)
6085 return got_error_from_errno2("realpath",
6087 got_path_strip_trailing_slashes(repo_path);
6090 ignore_whitespace = 1;
6102 usage_diff(); /* TODO show local worktree changes */
6103 } else if (argc == 2) {
6109 error = got_repo_pack_fds_open(&pack_fds);
6113 if (repo_path == NULL) {
6114 cwd = getcwd(NULL, 0);
6116 return got_error_from_errno("getcwd");
6117 error = got_worktree_open(&worktree, cwd, NULL);
6118 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6122 strdup(got_worktree_get_repo_path(worktree));
6124 repo_path = strdup(cwd);
6125 if (repo_path == NULL) {
6126 error = got_error_from_errno("strdup");
6131 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6137 error = apply_unveil(got_repo_get_path(repo), NULL);
6141 error = tog_load_refs(repo, 0);
6145 if (id_str1 != NULL) {
6146 error = got_keyword_to_idstr(&keyword_idstr1, id_str1,
6150 if (keyword_idstr1 != NULL)
6151 id_str1 = keyword_idstr1;
6153 if (id_str2 != NULL) {
6154 error = got_keyword_to_idstr(&keyword_idstr2, id_str2,
6158 if (keyword_idstr2 != NULL)
6159 id_str2 = keyword_idstr2;
6162 error = got_repo_match_object_id(&id1, &label1, id_str1,
6163 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
6167 error = got_repo_match_object_id(&id2, &label2, id_str2,
6168 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
6172 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
6174 error = got_error_from_errno("view_open");
6177 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
6178 ignore_whitespace, force_text_diff, NULL, repo);
6183 error = set_tog_base_commit(repo, worktree);
6187 /* Release work tree lock. */
6188 got_worktree_close(worktree);
6192 error = view_loop(view);
6195 free(tog_base_commit.id);
6196 free(keyword_idstr1);
6197 free(keyword_idstr2);
6205 const struct got_error *close_err = got_repo_close(repo);
6210 got_worktree_close(worktree);
6212 const struct got_error *pack_err =
6213 got_repo_pack_fds_close(pack_fds);
6226 "usage: %s blame [-c commit] [-r repository-path] path\n",
6231 struct tog_blame_line {
6233 struct got_object_id *id;
6236 static const struct got_error *
6237 draw_blame(struct tog_view *view)
6239 struct tog_blame_view_state *s = &view->state.blame;
6240 struct tog_blame *blame = &s->blame;
6241 regmatch_t *regmatch = &view->regmatch;
6242 const struct got_error *err;
6243 int lineno = 0, nprinted = 0;
6245 size_t linesize = 0;
6249 struct tog_blame_line *blame_line;
6250 struct got_object_id *prev_id = NULL;
6252 struct tog_color *tc;
6254 err = got_object_id_str(&id_str, &s->blamed_commit->id);
6259 werase(view->window);
6261 if (asprintf(&line, "commit %s", id_str) == -1) {
6262 err = got_error_from_errno("asprintf");
6267 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6272 if (view_needs_focus_indication(view))
6273 wstandout(view->window);
6274 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6276 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6277 waddwstr(view->window, wline);
6278 while (width++ < view->ncols)
6279 waddch(view->window, ' ');
6281 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6282 if (view_needs_focus_indication(view))
6283 wstandend(view->window);
6287 if (view->gline > blame->nlines)
6288 view->gline = blame->nlines;
6290 if (tog_io.wait_for_ui) {
6291 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6294 rc = pthread_cond_wait(&bta->blame_complete, &tog_mutex);
6296 return got_error_set_errno(rc, "pthread_cond_wait");
6297 tog_io.wait_for_ui = 0;
6300 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
6301 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
6302 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
6304 return got_error_from_errno("asprintf");
6307 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6312 waddwstr(view->window, wline);
6315 if (width < view->ncols - 1)
6316 waddch(view->window, '\n');
6320 while (nprinted < view->nlines - 2) {
6321 linelen = getline(&line, &linesize, blame->f);
6322 if (linelen == -1) {
6323 if (feof(blame->f)) {
6328 return got_ferror(blame->f, GOT_ERR_IO);
6330 if (++lineno < s->first_displayed_line)
6332 if (view->gline && !gotoline(view, &lineno, &nprinted))
6335 /* Set view->maxx based on full line length. */
6336 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6343 view->maxx = MAX(view->maxx, width);
6345 if (nprinted == s->selected_line - 1)
6346 wstandout(view->window);
6348 if (blame->nlines > 0) {
6349 blame_line = &blame->lines[lineno - 1];
6350 if (blame_line->annotated && prev_id &&
6351 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6352 !(nprinted == s->selected_line - 1)) {
6353 waddstr(view->window, " ");
6354 } else if (blame_line->annotated) {
6356 err = got_object_id_str(&id_str,
6362 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6364 wattr_on(view->window,
6365 COLOR_PAIR(tc->colorpair), NULL);
6366 wprintw(view->window, "%.8s", id_str);
6368 wattr_off(view->window,
6369 COLOR_PAIR(tc->colorpair), NULL);
6371 prev_id = blame_line->id;
6373 waddstr(view->window, "........");
6377 waddstr(view->window, "........");
6381 if (nprinted == s->selected_line - 1)
6382 wstandend(view->window);
6383 waddstr(view->window, " ");
6385 if (view->ncols <= 9) {
6387 } else if (s->first_displayed_line + nprinted ==
6389 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6390 err = add_matched_line(&width, line, view->ncols - 9, 9,
6391 view->window, view->x, regmatch);
6399 err = format_line(&wline, &width, &skip, line,
6400 view->x, view->ncols - 9, 9, 1);
6405 waddwstr(view->window, &wline[skip]);
6411 if (width <= view->ncols - 1)
6412 waddch(view->window, '\n');
6413 if (++nprinted == 1)
6414 s->first_displayed_line = lineno;
6417 s->last_displayed_line = lineno;
6424 static const struct got_error *
6425 blame_cb(void *arg, int nlines, int lineno,
6426 struct got_commit_object *commit, struct got_object_id *id)
6428 const struct got_error *err = NULL;
6429 struct tog_blame_cb_args *a = arg;
6430 struct tog_blame_line *line;
6433 if (nlines != a->nlines ||
6434 (lineno != -1 && lineno < 1) || lineno > a->nlines)
6435 return got_error(GOT_ERR_RANGE);
6437 errcode = pthread_mutex_lock(&tog_mutex);
6439 return got_error_set_errno(errcode, "pthread_mutex_lock");
6441 if (*a->quit) { /* user has quit the blame view */
6442 err = got_error(GOT_ERR_ITER_COMPLETED);
6447 goto done; /* no change in this commit */
6449 line = &a->lines[lineno - 1];
6450 if (line->annotated)
6453 line->id = got_object_id_dup(id);
6454 if (line->id == NULL) {
6455 err = got_error_from_errno("got_object_id_dup");
6458 line->annotated = 1;
6460 errcode = pthread_mutex_unlock(&tog_mutex);
6462 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6467 blame_thread(void *arg)
6469 const struct got_error *err, *close_err;
6470 struct tog_blame_thread_args *ta = arg;
6471 struct tog_blame_cb_args *a = ta->cb_args;
6472 int errcode, fd1 = -1, fd2 = -1;
6473 FILE *f1 = NULL, *f2 = NULL;
6475 fd1 = got_opentempfd();
6477 return (void *)got_error_from_errno("got_opentempfd");
6479 fd2 = got_opentempfd();
6481 err = got_error_from_errno("got_opentempfd");
6485 f1 = got_opentemp();
6487 err = (void *)got_error_from_errno("got_opentemp");
6490 f2 = got_opentemp();
6492 err = (void *)got_error_from_errno("got_opentemp");
6496 err = block_signals_used_by_main_thread();
6500 err = got_blame(ta->path, a->commit_id, ta->repo,
6501 tog_diff_algo, blame_cb, ta->cb_args,
6502 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6503 if (err && err->code == GOT_ERR_CANCELLED)
6506 errcode = pthread_mutex_lock(&tog_mutex);
6508 err = got_error_set_errno(errcode, "pthread_mutex_lock");
6512 close_err = got_repo_close(ta->repo);
6518 if (tog_io.wait_for_ui) {
6519 errcode = pthread_cond_signal(&ta->blame_complete);
6520 if (errcode && err == NULL)
6521 err = got_error_set_errno(errcode,
6522 "pthread_cond_signal");
6525 errcode = pthread_mutex_unlock(&tog_mutex);
6526 if (errcode && err == NULL)
6527 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6530 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6531 err = got_error_from_errno("close");
6532 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6533 err = got_error_from_errno("close");
6534 if (f1 && fclose(f1) == EOF && err == NULL)
6535 err = got_error_from_errno("fclose");
6536 if (f2 && fclose(f2) == EOF && err == NULL)
6537 err = got_error_from_errno("fclose");
6542 static struct got_object_id *
6543 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6544 int first_displayed_line, int selected_line)
6546 struct tog_blame_line *line;
6551 line = &lines[first_displayed_line - 1 + selected_line - 1];
6552 if (!line->annotated)
6558 static struct got_object_id *
6559 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6562 struct tog_blame_line *line;
6564 if (nlines <= 0 || lineno >= nlines)
6567 line = &lines[lineno - 1];
6568 if (!line->annotated)
6574 static const struct got_error *
6575 stop_blame(struct tog_blame *blame)
6577 const struct got_error *err = NULL;
6580 if (blame->thread) {
6582 errcode = pthread_mutex_unlock(&tog_mutex);
6584 return got_error_set_errno(errcode,
6585 "pthread_mutex_unlock");
6586 errcode = pthread_join(blame->thread, (void **)&err);
6588 return got_error_set_errno(errcode, "pthread_join");
6589 errcode = pthread_mutex_lock(&tog_mutex);
6591 return got_error_set_errno(errcode,
6592 "pthread_mutex_lock");
6593 if (err && err->code == GOT_ERR_ITER_COMPLETED)
6595 blame->thread = NULL;
6597 if (blame->thread_args.repo) {
6598 const struct got_error *close_err;
6599 close_err = got_repo_close(blame->thread_args.repo);
6602 blame->thread_args.repo = NULL;
6605 if (fclose(blame->f) == EOF && err == NULL)
6606 err = got_error_from_errno("fclose");
6610 for (i = 0; i < blame->nlines; i++)
6611 free(blame->lines[i].id);
6613 blame->lines = NULL;
6615 free(blame->cb_args.commit_id);
6616 blame->cb_args.commit_id = NULL;
6617 if (blame->pack_fds) {
6618 const struct got_error *pack_err =
6619 got_repo_pack_fds_close(blame->pack_fds);
6622 blame->pack_fds = NULL;
6624 free(blame->line_offsets);
6625 blame->line_offsets = NULL;
6629 static const struct got_error *
6630 cancel_blame_view(void *arg)
6632 const struct got_error *err = NULL;
6636 errcode = pthread_mutex_lock(&tog_mutex);
6638 return got_error_set_errno(errcode,
6639 "pthread_mutex_unlock");
6642 err = got_error(GOT_ERR_CANCELLED);
6644 errcode = pthread_mutex_unlock(&tog_mutex);
6646 return got_error_set_errno(errcode,
6647 "pthread_mutex_lock");
6652 static const struct got_error *
6653 run_blame(struct tog_view *view)
6655 struct tog_blame_view_state *s = &view->state.blame;
6656 struct tog_blame *blame = &s->blame;
6657 const struct got_error *err = NULL;
6658 struct got_commit_object *commit = NULL;
6659 struct got_blob_object *blob = NULL;
6660 struct got_repository *thread_repo = NULL;
6661 struct got_object_id *obj_id = NULL;
6662 int obj_type, fd = -1;
6663 int *pack_fds = NULL;
6665 err = got_object_open_as_commit(&commit, s->repo,
6666 &s->blamed_commit->id);
6670 fd = got_opentempfd();
6672 err = got_error_from_errno("got_opentempfd");
6676 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6680 err = got_object_get_type(&obj_type, s->repo, obj_id);
6684 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6685 err = got_error(GOT_ERR_OBJ_TYPE);
6689 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6692 blame->f = got_opentemp();
6693 if (blame->f == NULL) {
6694 err = got_error_from_errno("got_opentemp");
6697 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6698 &blame->line_offsets, blame->f, blob);
6701 if (blame->nlines == 0) {
6702 s->blame_complete = 1;
6706 /* Don't include \n at EOF in the blame line count. */
6707 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6710 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6711 if (blame->lines == NULL) {
6712 err = got_error_from_errno("calloc");
6716 err = got_repo_pack_fds_open(&pack_fds);
6719 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6724 blame->pack_fds = pack_fds;
6725 blame->cb_args.view = view;
6726 blame->cb_args.lines = blame->lines;
6727 blame->cb_args.nlines = blame->nlines;
6728 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6729 if (blame->cb_args.commit_id == NULL) {
6730 err = got_error_from_errno("got_object_id_dup");
6733 blame->cb_args.quit = &s->done;
6735 blame->thread_args.path = s->path;
6736 blame->thread_args.repo = thread_repo;
6737 blame->thread_args.cb_args = &blame->cb_args;
6738 blame->thread_args.complete = &s->blame_complete;
6739 blame->thread_args.cancel_cb = cancel_blame_view;
6740 blame->thread_args.cancel_arg = &s->done;
6741 s->blame_complete = 0;
6743 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6744 s->first_displayed_line = 1;
6745 s->last_displayed_line = view->nlines;
6746 s->selected_line = 1;
6748 s->matched_line = 0;
6752 got_object_commit_close(commit);
6753 if (fd != -1 && close(fd) == -1 && err == NULL)
6754 err = got_error_from_errno("close");
6756 got_object_blob_close(blob);
6763 static const struct got_error *
6764 open_blame_view(struct tog_view *view, char *path,
6765 struct got_object_id *commit_id, struct got_repository *repo)
6767 const struct got_error *err = NULL;
6768 struct tog_blame_view_state *s = &view->state.blame;
6770 STAILQ_INIT(&s->blamed_commits);
6772 s->path = strdup(path);
6773 if (s->path == NULL)
6774 return got_error_from_errno("strdup");
6776 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6782 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6783 s->first_displayed_line = 1;
6784 s->last_displayed_line = view->nlines;
6785 s->selected_line = 1;
6786 s->blame_complete = 0;
6788 s->commit_id = commit_id;
6789 memset(&s->blame, 0, sizeof(s->blame));
6791 STAILQ_INIT(&s->colors);
6792 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6793 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6794 get_color_value("TOG_COLOR_COMMIT"));
6799 view->show = show_blame_view;
6800 view->input = input_blame_view;
6801 view->reset = reset_blame_view;
6802 view->close = close_blame_view;
6803 view->search_start = search_start_blame_view;
6804 view->search_setup = search_setup_blame_view;
6805 view->search_next = search_next_view_match;
6807 if (using_mock_io) {
6808 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6811 rc = pthread_cond_init(&bta->blame_complete, NULL);
6813 return got_error_set_errno(rc, "pthread_cond_init");
6816 return run_blame(view);
6819 static const struct got_error *
6820 close_blame_view(struct tog_view *view)
6822 const struct got_error *err = NULL;
6823 struct tog_blame_view_state *s = &view->state.blame;
6825 if (s->blame.thread)
6826 err = stop_blame(&s->blame);
6828 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6829 struct got_object_qid *blamed_commit;
6830 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6831 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6832 got_object_qid_free(blamed_commit);
6835 if (using_mock_io) {
6836 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6839 rc = pthread_cond_destroy(&bta->blame_complete);
6840 if (rc && err == NULL)
6841 err = got_error_set_errno(rc, "pthread_cond_destroy");
6845 free_colors(&s->colors);
6849 static const struct got_error *
6850 search_start_blame_view(struct tog_view *view)
6852 struct tog_blame_view_state *s = &view->state.blame;
6854 s->matched_line = 0;
6859 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6860 size_t *nlines, int **first, int **last, int **match, int **selected)
6862 struct tog_blame_view_state *s = &view->state.blame;
6865 *nlines = s->blame.nlines;
6866 *line_offsets = s->blame.line_offsets;
6867 *match = &s->matched_line;
6868 *first = &s->first_displayed_line;
6869 *last = &s->last_displayed_line;
6870 *selected = &s->selected_line;
6873 static const struct got_error *
6874 show_blame_view(struct tog_view *view)
6876 const struct got_error *err = NULL;
6877 struct tog_blame_view_state *s = &view->state.blame;
6880 if (s->blame.thread == NULL && !s->blame_complete) {
6881 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6882 &s->blame.thread_args);
6884 return got_error_set_errno(errcode, "pthread_create");
6887 halfdelay(1); /* fast refresh while annotating */
6890 if (s->blame_complete && !using_mock_io)
6891 halfdelay(10); /* disable fast refresh */
6893 err = draw_blame(view);
6899 static const struct got_error *
6900 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6901 struct got_repository *repo, struct got_object_id *id)
6903 struct tog_view *log_view;
6904 const struct got_error *err = NULL;
6908 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6909 if (log_view == NULL)
6910 return got_error_from_errno("view_open");
6912 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0, NULL);
6914 view_close(log_view);
6916 *new_view = log_view;
6921 static const struct got_error *
6922 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6924 const struct got_error *err = NULL, *thread_err = NULL;
6925 struct tog_view *diff_view;
6926 struct tog_blame_view_state *s = &view->state.blame;
6927 int eos, nscroll, begin_y = 0, begin_x = 0;
6929 eos = nscroll = view->nlines - 2;
6930 if (view_is_hsplit_top(view))
6940 horizontal_scroll_input(view, ch);
6947 s->selected_line = 1;
6948 s->first_displayed_line = 1;
6953 if (s->blame.nlines < eos) {
6954 s->selected_line = s->blame.nlines;
6955 s->first_displayed_line = 1;
6957 s->selected_line = eos;
6958 s->first_displayed_line = s->blame.nlines - (eos - 1);
6965 if (s->selected_line > 1)
6967 else if (s->selected_line == 1 &&
6968 s->first_displayed_line > 1)
6969 s->first_displayed_line--;
6980 if (s->first_displayed_line == 1) {
6981 if (view->count > 1)
6983 s->selected_line = MAX(1, s->selected_line - nscroll);
6987 if (s->first_displayed_line > nscroll)
6988 s->first_displayed_line -= nscroll;
6990 s->first_displayed_line = 1;
6995 if (s->selected_line < eos && s->first_displayed_line +
6996 s->selected_line <= s->blame.nlines)
6998 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6999 s->first_displayed_line++;
7005 struct got_object_id *id = NULL;
7008 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
7009 s->first_displayed_line, s->selected_line);
7013 struct got_commit_object *commit, *pcommit;
7014 struct got_object_qid *pid;
7015 struct got_object_id *blob_id = NULL;
7017 err = got_object_open_as_commit(&commit,
7022 got_object_commit_get_parent_ids(commit));
7024 got_object_commit_close(commit);
7027 /* Check if path history ends here. */
7028 err = got_object_open_as_commit(&pcommit,
7032 err = got_object_id_by_path(&blob_id, s->repo,
7034 got_object_commit_close(pcommit);
7036 if (err->code == GOT_ERR_NO_TREE_ENTRY)
7038 got_object_commit_close(commit);
7041 err = got_object_get_type(&obj_type, s->repo,
7044 /* Can't blame non-blob type objects. */
7045 if (obj_type != GOT_OBJ_TYPE_BLOB) {
7046 got_object_commit_close(commit);
7049 err = got_object_qid_alloc(&s->blamed_commit,
7051 got_object_commit_close(commit);
7053 if (got_object_id_cmp(id,
7054 &s->blamed_commit->id) == 0)
7056 err = got_object_qid_alloc(&s->blamed_commit,
7062 thread_err = stop_blame(&s->blame);
7066 STAILQ_INSERT_HEAD(&s->blamed_commits,
7067 s->blamed_commit, entry);
7068 err = run_blame(view);
7074 struct got_object_qid *first;
7077 first = STAILQ_FIRST(&s->blamed_commits);
7078 if (!got_object_id_cmp(&first->id, s->commit_id))
7081 thread_err = stop_blame(&s->blame);
7085 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
7086 got_object_qid_free(s->blamed_commit);
7088 STAILQ_FIRST(&s->blamed_commits);
7089 err = run_blame(view);
7096 s->id_to_log = get_selected_commit_id(s->blame.lines,
7097 s->blame.nlines, s->first_displayed_line, s->selected_line);
7099 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7103 struct got_object_id *id = NULL;
7104 struct got_object_qid *pid;
7105 struct got_commit_object *commit = NULL;
7108 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
7109 s->first_displayed_line, s->selected_line);
7112 err = got_object_open_as_commit(&commit, s->repo, id);
7115 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7117 /* traversed from diff view, release diff resources */
7118 err = close_diff_view(*new_view);
7121 diff_view = *new_view;
7123 if (view_is_parent_view(view))
7124 view_get_split(view, &begin_y, &begin_x);
7126 diff_view = view_open(0, 0, begin_y, begin_x,
7128 if (diff_view == NULL) {
7129 got_object_commit_close(commit);
7130 err = got_error_from_errno("view_open");
7134 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
7135 id, NULL, NULL, 3, 0, 0, view, s->repo);
7136 got_object_commit_close(commit);
7139 s->last_diffed_line = s->first_displayed_line - 1 +
7142 break; /* still open from active diff view */
7143 if (view_is_parent_view(view) &&
7144 view->mode == TOG_VIEW_SPLIT_HRZN) {
7145 err = view_init_hsplit(view, begin_y);
7151 diff_view->focussed = 1;
7152 diff_view->mode = view->mode;
7153 diff_view->nlines = view->lines - begin_y;
7154 if (view_is_parent_view(view)) {
7155 view_transfer_size(diff_view, view);
7156 err = view_close_child(view);
7159 err = view_set_child(view, diff_view);
7162 view->focus_child = 1;
7164 *new_view = diff_view;
7177 if (s->last_displayed_line >= s->blame.nlines &&
7178 s->selected_line >= MIN(s->blame.nlines,
7179 view->nlines - 2)) {
7183 if (s->last_displayed_line >= s->blame.nlines &&
7184 s->selected_line < view->nlines - 2) {
7186 MIN(nscroll, s->last_displayed_line -
7187 s->first_displayed_line - s->selected_line + 1);
7189 if (s->last_displayed_line + nscroll <= s->blame.nlines)
7190 s->first_displayed_line += nscroll;
7192 s->first_displayed_line =
7193 s->blame.nlines - (view->nlines - 3);
7196 if (s->selected_line > view->nlines - 2) {
7197 s->selected_line = MIN(s->blame.nlines,
7205 return thread_err ? thread_err : err;
7208 static const struct got_error *
7209 reset_blame_view(struct tog_view *view)
7211 const struct got_error *err;
7212 struct tog_blame_view_state *s = &view->state.blame;
7216 err = stop_blame(&s->blame);
7220 return run_blame(view);
7223 static const struct got_error *
7224 cmd_blame(int argc, char *argv[])
7226 const struct got_error *error;
7227 struct got_repository *repo = NULL;
7228 struct got_worktree *worktree = NULL;
7229 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7230 char *link_target = NULL;
7231 struct got_object_id *commit_id = NULL;
7232 struct got_commit_object *commit = NULL;
7233 char *keyword_idstr = NULL, *commit_id_str = NULL;
7235 struct tog_view *view = NULL;
7236 int *pack_fds = NULL;
7238 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7241 commit_id_str = optarg;
7244 repo_path = realpath(optarg, NULL);
7245 if (repo_path == NULL)
7246 return got_error_from_errno2("realpath",
7261 error = got_repo_pack_fds_open(&pack_fds);
7265 if (repo_path == NULL) {
7266 cwd = getcwd(NULL, 0);
7268 return got_error_from_errno("getcwd");
7269 error = got_worktree_open(&worktree, cwd, NULL);
7270 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7274 strdup(got_worktree_get_repo_path(worktree));
7276 repo_path = strdup(cwd);
7277 if (repo_path == NULL) {
7278 error = got_error_from_errno("strdup");
7283 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7287 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
7294 error = apply_unveil(got_repo_get_path(repo), NULL);
7298 error = tog_load_refs(repo, 0);
7302 if (commit_id_str == NULL) {
7303 struct got_reference *head_ref;
7304 error = got_ref_open(&head_ref, repo, worktree ?
7305 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
7308 error = got_ref_resolve(&commit_id, repo, head_ref);
7309 got_ref_close(head_ref);
7311 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
7315 if (keyword_idstr != NULL)
7316 commit_id_str = keyword_idstr;
7318 error = got_repo_match_object_id(&commit_id, NULL,
7319 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7324 error = got_object_open_as_commit(&commit, repo, commit_id);
7328 error = got_object_resolve_symlinks(&link_target, in_repo_path,
7333 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
7335 error = got_error_from_errno("view_open");
7338 error = open_blame_view(view, link_target ? link_target : in_repo_path,
7340 if (error != NULL) {
7341 if (view->close == NULL)
7342 close_blame_view(view);
7348 error = set_tog_base_commit(repo, worktree);
7352 /* Release work tree lock. */
7353 got_worktree_close(worktree);
7357 error = view_loop(view);
7360 free(tog_base_commit.id);
7366 free(keyword_idstr);
7368 got_object_commit_close(commit);
7370 got_worktree_close(worktree);
7372 const struct got_error *close_err = got_repo_close(repo);
7377 const struct got_error *pack_err =
7378 got_repo_pack_fds_close(pack_fds);
7386 static const struct got_error *
7387 draw_tree_entries(struct tog_view *view, const char *parent_path)
7389 struct tog_tree_view_state *s = &view->state.tree;
7390 const struct got_error *err = NULL;
7391 struct got_tree_entry *te;
7394 struct tog_color *tc;
7395 int width, n, nentries, scrollx, i = 1;
7396 int limit = view->nlines;
7399 if (view_is_hsplit_top(view))
7400 --limit; /* border */
7402 werase(view->window);
7407 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7411 if (view_needs_focus_indication(view))
7412 wstandout(view->window);
7413 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7415 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7416 waddwstr(view->window, wline);
7419 while (width++ < view->ncols)
7420 waddch(view->window, ' ');
7422 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7423 if (view_needs_focus_indication(view))
7424 wstandend(view->window);
7429 if (s->first_displayed_entry) {
7430 i += got_tree_entry_get_index(s->first_displayed_entry);
7431 if (s->tree != s->root)
7432 ++i; /* account for ".." entry */
7434 nentries = got_object_tree_get_nentries(s->tree);
7435 if (asprintf(&index, "[%d/%d] %s",
7436 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7437 return got_error_from_errno("asprintf");
7438 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7442 waddwstr(view->window, wline);
7445 if (width < view->ncols - 1)
7446 waddch(view->window, '\n');
7449 waddch(view->window, '\n');
7453 if (s->first_displayed_entry == NULL) {
7454 te = got_object_tree_get_first_entry(s->tree);
7455 if (s->selected == 0) {
7457 wstandout(view->window);
7458 s->selected_entry = NULL;
7460 waddstr(view->window, " ..\n"); /* parent directory */
7461 if (s->selected == 0 && view->focussed)
7462 wstandend(view->window);
7469 te = s->first_displayed_entry;
7473 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7474 char *line = NULL, *id_str = NULL, *link_target = NULL;
7475 const char *modestr = "";
7478 te = got_object_tree_get_entry(s->tree, i);
7479 mode = got_tree_entry_get_mode(te);
7482 err = got_object_id_str(&id_str,
7483 got_tree_entry_get_id(te));
7485 return got_error_from_errno(
7486 "got_object_id_str");
7488 if (got_object_tree_entry_is_submodule(te))
7490 else if (S_ISLNK(mode)) {
7493 err = got_tree_entry_get_symlink_target(&link_target,
7499 for (i = 0; link_target[i] != '\0'; i++) {
7500 if (!isprint((unsigned char)link_target[i]))
7501 link_target[i] = '?';
7505 else if (S_ISDIR(mode))
7507 else if (mode & S_IXUSR)
7509 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7510 got_tree_entry_get_name(te), modestr,
7511 link_target ? " -> ": "",
7512 link_target ? link_target : "") == -1) {
7515 return got_error_from_errno("asprintf");
7520 /* use full line width to determine view->maxx */
7521 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7526 view->maxx = MAX(view->maxx, width);
7530 err = format_line(&wline, &width, &scrollx, line, view->x,
7536 if (n == s->selected) {
7538 wstandout(view->window);
7539 s->selected_entry = te;
7541 tc = match_color(&s->colors, line);
7543 wattr_on(view->window,
7544 COLOR_PAIR(tc->colorpair), NULL);
7545 waddwstr(view->window, &wline[scrollx]);
7547 wattr_off(view->window,
7548 COLOR_PAIR(tc->colorpair), NULL);
7549 if (width < view->ncols)
7550 waddch(view->window, '\n');
7551 if (n == s->selected && view->focussed)
7552 wstandend(view->window);
7558 s->last_displayed_entry = te;
7567 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7569 struct got_tree_entry *te;
7570 int isroot = s->tree == s->root;
7573 if (s->first_displayed_entry == NULL)
7576 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7577 while (i++ < maxscroll) {
7580 s->first_displayed_entry = NULL;
7583 s->first_displayed_entry = te;
7584 te = got_tree_entry_get_prev(s->tree, te);
7588 static const struct got_error *
7589 tree_scroll_down(struct tog_view *view, int maxscroll)
7591 struct tog_tree_view_state *s = &view->state.tree;
7592 struct got_tree_entry *next, *last;
7595 if (s->first_displayed_entry)
7596 next = got_tree_entry_get_next(s->tree,
7597 s->first_displayed_entry);
7599 next = got_object_tree_get_first_entry(s->tree);
7601 last = s->last_displayed_entry;
7602 while (next && n++ < maxscroll) {
7604 s->last_displayed_entry = last;
7605 last = got_tree_entry_get_next(s->tree, last);
7607 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7608 s->first_displayed_entry = next;
7609 next = got_tree_entry_get_next(s->tree, next);
7616 static const struct got_error *
7617 tree_entry_path(char **path, struct tog_parent_trees *parents,
7618 struct got_tree_entry *te)
7620 const struct got_error *err = NULL;
7621 struct tog_parent_tree *pt;
7622 size_t len = 2; /* for leading slash and NUL */
7624 TAILQ_FOREACH(pt, parents, entry)
7625 len += strlen(got_tree_entry_get_name(pt->selected_entry))
7628 len += strlen(got_tree_entry_get_name(te));
7630 *path = calloc(1, len);
7632 return got_error_from_errno("calloc");
7635 pt = TAILQ_LAST(parents, tog_parent_trees);
7637 const char *name = got_tree_entry_get_name(pt->selected_entry);
7638 if (strlcat(*path, name, len) >= len) {
7639 err = got_error(GOT_ERR_NO_SPACE);
7642 if (strlcat(*path, "/", len) >= len) {
7643 err = got_error(GOT_ERR_NO_SPACE);
7646 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7649 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7650 err = got_error(GOT_ERR_NO_SPACE);
7662 static const struct got_error *
7663 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7664 struct got_tree_entry *te, struct tog_parent_trees *parents,
7665 struct got_object_id *commit_id, struct got_repository *repo)
7667 const struct got_error *err = NULL;
7669 struct tog_view *blame_view;
7673 err = tree_entry_path(&path, parents, te);
7677 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7678 if (blame_view == NULL) {
7679 err = got_error_from_errno("view_open");
7683 err = open_blame_view(blame_view, path, commit_id, repo);
7685 if (err->code == GOT_ERR_CANCELLED)
7687 view_close(blame_view);
7689 *new_view = blame_view;
7695 static const struct got_error *
7696 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7697 struct tog_tree_view_state *s)
7699 struct tog_view *log_view;
7700 const struct got_error *err = NULL;
7705 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7706 if (log_view == NULL)
7707 return got_error_from_errno("view_open");
7709 err = tree_entry_path(&path, &s->parents, s->selected_entry);
7713 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7716 view_close(log_view);
7718 *new_view = log_view;
7723 static const struct got_error *
7724 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7725 const char *head_ref_name, struct got_repository *repo)
7727 const struct got_error *err = NULL;
7728 char *commit_id_str = NULL;
7729 struct tog_tree_view_state *s = &view->state.tree;
7730 struct got_commit_object *commit = NULL;
7732 TAILQ_INIT(&s->parents);
7733 STAILQ_INIT(&s->colors);
7735 s->commit_id = got_object_id_dup(commit_id);
7736 if (s->commit_id == NULL) {
7737 err = got_error_from_errno("got_object_id_dup");
7741 err = got_object_open_as_commit(&commit, repo, commit_id);
7746 * The root is opened here and will be closed when the view is closed.
7747 * Any visited subtrees and their path-wise parents are opened and
7750 err = got_object_open_as_tree(&s->root, repo,
7751 got_object_commit_get_tree_id(commit));
7756 err = got_object_id_str(&commit_id_str, commit_id);
7760 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7761 err = got_error_from_errno("asprintf");
7765 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7766 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7767 if (head_ref_name) {
7768 s->head_ref_name = strdup(head_ref_name);
7769 if (s->head_ref_name == NULL) {
7770 err = got_error_from_errno("strdup");
7776 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7777 err = add_color(&s->colors, "\\$$",
7778 TOG_COLOR_TREE_SUBMODULE,
7779 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7782 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7783 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7786 err = add_color(&s->colors, "/$",
7787 TOG_COLOR_TREE_DIRECTORY,
7788 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7792 err = add_color(&s->colors, "\\*$",
7793 TOG_COLOR_TREE_EXECUTABLE,
7794 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7798 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7799 get_color_value("TOG_COLOR_COMMIT"));
7804 view->show = show_tree_view;
7805 view->input = input_tree_view;
7806 view->close = close_tree_view;
7807 view->search_start = search_start_tree_view;
7808 view->search_next = search_next_tree_view;
7810 free(commit_id_str);
7812 got_object_commit_close(commit);
7814 if (view->close == NULL)
7815 close_tree_view(view);
7821 static const struct got_error *
7822 close_tree_view(struct tog_view *view)
7824 struct tog_tree_view_state *s = &view->state.tree;
7826 free_colors(&s->colors);
7827 free(s->tree_label);
7828 s->tree_label = NULL;
7830 s->commit_id = NULL;
7831 free(s->head_ref_name);
7832 s->head_ref_name = NULL;
7833 while (!TAILQ_EMPTY(&s->parents)) {
7834 struct tog_parent_tree *parent;
7835 parent = TAILQ_FIRST(&s->parents);
7836 TAILQ_REMOVE(&s->parents, parent, entry);
7837 if (parent->tree != s->root)
7838 got_object_tree_close(parent->tree);
7842 if (s->tree != NULL && s->tree != s->root)
7843 got_object_tree_close(s->tree);
7845 got_object_tree_close(s->root);
7849 static const struct got_error *
7850 search_start_tree_view(struct tog_view *view)
7852 struct tog_tree_view_state *s = &view->state.tree;
7854 s->matched_entry = NULL;
7859 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7861 regmatch_t regmatch;
7863 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7867 static const struct got_error *
7868 search_next_tree_view(struct tog_view *view)
7870 struct tog_tree_view_state *s = &view->state.tree;
7871 struct got_tree_entry *te = NULL;
7873 if (!view->searching) {
7874 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7878 if (s->matched_entry) {
7879 if (view->searching == TOG_SEARCH_FORWARD) {
7880 if (s->selected_entry)
7881 te = got_tree_entry_get_next(s->tree,
7884 te = got_object_tree_get_first_entry(s->tree);
7886 if (s->selected_entry == NULL)
7887 te = got_object_tree_get_last_entry(s->tree);
7889 te = got_tree_entry_get_prev(s->tree,
7893 if (s->selected_entry)
7894 te = s->selected_entry;
7895 else if (view->searching == TOG_SEARCH_FORWARD)
7896 te = got_object_tree_get_first_entry(s->tree);
7898 te = got_object_tree_get_last_entry(s->tree);
7903 if (s->matched_entry == NULL) {
7904 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7907 if (view->searching == TOG_SEARCH_FORWARD)
7908 te = got_object_tree_get_first_entry(s->tree);
7910 te = got_object_tree_get_last_entry(s->tree);
7913 if (match_tree_entry(te, &view->regex)) {
7914 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7915 s->matched_entry = te;
7919 if (view->searching == TOG_SEARCH_FORWARD)
7920 te = got_tree_entry_get_next(s->tree, te);
7922 te = got_tree_entry_get_prev(s->tree, te);
7925 if (s->matched_entry) {
7926 s->first_displayed_entry = s->matched_entry;
7933 static const struct got_error *
7934 show_tree_view(struct tog_view *view)
7936 const struct got_error *err = NULL;
7937 struct tog_tree_view_state *s = &view->state.tree;
7940 err = tree_entry_path(&parent_path, &s->parents, NULL);
7944 err = draw_tree_entries(view, parent_path);
7951 static const struct got_error *
7952 tree_goto_line(struct tog_view *view, int nlines)
7954 const struct got_error *err = NULL;
7955 struct tog_tree_view_state *s = &view->state.tree;
7956 struct got_tree_entry **fte, **lte, **ste;
7957 int g, last, first = 1, i = 1;
7958 int root = s->tree == s->root;
7959 int off = root ? 1 : 2;
7966 else if (g > got_object_tree_get_nentries(s->tree))
7967 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7969 fte = &s->first_displayed_entry;
7970 lte = &s->last_displayed_entry;
7971 ste = &s->selected_entry;
7974 first = got_tree_entry_get_index(*fte);
7975 first += off; /* account for ".." */
7977 last = got_tree_entry_get_index(*lte);
7980 if (g >= first && g <= last && g - first < nlines) {
7981 s->selected = g - first;
7982 return NULL; /* gline is on the current page */
7986 i = got_tree_entry_get_index(*ste);
7991 err = tree_scroll_down(view, g - i);
7994 if (got_tree_entry_get_index(*lte) >=
7995 got_object_tree_get_nentries(s->tree) - 1 &&
7996 first + s->selected < g &&
7997 s->selected < s->ndisplayed - 1) {
7998 first = got_tree_entry_get_index(*fte);
8000 s->selected = g - first;
8003 tree_scroll_up(s, i - g);
8006 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
8007 s->selected = g - 1;
8012 static const struct got_error *
8013 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
8015 const struct got_error *err = NULL;
8016 struct tog_tree_view_state *s = &view->state.tree;
8017 struct got_tree_entry *te;
8018 int n, nscroll = view->nlines - 3;
8021 return tree_goto_line(view, nscroll);
8030 horizontal_scroll_input(view, ch);
8033 s->show_ids = !s->show_ids;
8038 if (!s->selected_entry)
8040 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8044 err = view_request_new(new_view, view, TOG_VIEW_REF);
8051 if (s->tree == s->root)
8052 s->first_displayed_entry =
8053 got_object_tree_get_first_entry(s->tree);
8055 s->first_displayed_entry = NULL;
8060 int eos = view->nlines - 3;
8062 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8066 te = got_object_tree_get_last_entry(s->tree);
8067 for (n = 0; n < eos; n++) {
8069 if (s->tree != s->root) {
8070 s->first_displayed_entry = NULL;
8075 s->first_displayed_entry = te;
8076 te = got_tree_entry_get_prev(s->tree, te);
8079 s->selected = n - 1;
8085 if (s->selected > 0) {
8089 tree_scroll_up(s, 1);
8090 if (s->selected_entry == NULL ||
8091 (s->tree == s->root && s->selected_entry ==
8092 got_object_tree_get_first_entry(s->tree)))
8102 if (s->tree == s->root) {
8103 if (got_object_tree_get_first_entry(s->tree) ==
8104 s->first_displayed_entry)
8105 s->selected -= MIN(s->selected, nscroll);
8107 if (s->first_displayed_entry == NULL)
8108 s->selected -= MIN(s->selected, nscroll);
8110 tree_scroll_up(s, MAX(0, nscroll));
8111 if (s->selected_entry == NULL ||
8112 (s->tree == s->root && s->selected_entry ==
8113 got_object_tree_get_first_entry(s->tree)))
8119 if (s->selected < s->ndisplayed - 1) {
8123 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
8125 /* can't scroll any further */
8129 tree_scroll_down(view, 1);
8139 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
8141 /* can't scroll any further; move cursor down */
8142 if (s->selected < s->ndisplayed - 1)
8143 s->selected += MIN(nscroll,
8144 s->ndisplayed - s->selected - 1);
8149 tree_scroll_down(view, nscroll);
8154 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
8155 struct tog_parent_tree *parent;
8156 /* user selected '..' */
8157 if (s->tree == s->root) {
8161 parent = TAILQ_FIRST(&s->parents);
8162 TAILQ_REMOVE(&s->parents, parent,
8164 got_object_tree_close(s->tree);
8165 s->tree = parent->tree;
8166 s->first_displayed_entry =
8167 parent->first_displayed_entry;
8169 parent->selected_entry;
8170 s->selected = parent->selected;
8171 if (s->selected > view->nlines - 3) {
8172 err = offset_selection_down(view);
8177 } else if (S_ISDIR(got_tree_entry_get_mode(
8178 s->selected_entry))) {
8179 struct got_tree_object *subtree;
8181 err = got_object_open_as_tree(&subtree, s->repo,
8182 got_tree_entry_get_id(s->selected_entry));
8185 err = tree_view_visit_subtree(s, subtree);
8187 got_object_tree_close(subtree);
8190 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
8191 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
8194 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
8195 s->selected = view->nlines - 4;
8211 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
8216 static const struct got_error *
8217 cmd_tree(int argc, char *argv[])
8219 const struct got_error *error;
8220 struct got_repository *repo = NULL;
8221 struct got_worktree *worktree = NULL;
8222 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8223 struct got_object_id *commit_id = NULL;
8224 struct got_commit_object *commit = NULL;
8225 const char *commit_id_arg = NULL;
8226 char *keyword_idstr = NULL, *label = NULL;
8227 struct got_reference *ref = NULL;
8228 const char *head_ref_name = NULL;
8230 struct tog_view *view;
8231 int *pack_fds = NULL;
8233 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
8236 commit_id_arg = optarg;
8239 repo_path = realpath(optarg, NULL);
8240 if (repo_path == NULL)
8241 return got_error_from_errno2("realpath",
8256 error = got_repo_pack_fds_open(&pack_fds);
8260 if (repo_path == NULL) {
8261 cwd = getcwd(NULL, 0);
8263 return got_error_from_errno("getcwd");
8264 error = got_worktree_open(&worktree, cwd, NULL);
8265 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8269 strdup(got_worktree_get_repo_path(worktree));
8271 repo_path = strdup(cwd);
8272 if (repo_path == NULL) {
8273 error = got_error_from_errno("strdup");
8278 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8282 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8289 error = apply_unveil(got_repo_get_path(repo), NULL);
8293 error = tog_load_refs(repo, 0);
8297 if (commit_id_arg == NULL) {
8298 error = got_repo_match_object_id(&commit_id, &label,
8299 worktree ? got_worktree_get_head_ref_name(worktree) :
8300 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8303 head_ref_name = label;
8305 error = got_keyword_to_idstr(&keyword_idstr, commit_id_arg,
8309 if (keyword_idstr != NULL)
8310 commit_id_arg = keyword_idstr;
8312 error = got_ref_open(&ref, repo, commit_id_arg, 0);
8314 head_ref_name = got_ref_get_name(ref);
8315 else if (error->code != GOT_ERR_NOT_REF)
8317 error = got_repo_match_object_id(&commit_id, NULL,
8318 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8323 error = got_object_open_as_commit(&commit, repo, commit_id);
8327 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
8329 error = got_error_from_errno("view_open");
8332 error = open_tree_view(view, commit_id, head_ref_name, repo);
8335 if (!got_path_is_root_dir(in_repo_path)) {
8336 error = tree_view_walk_path(&view->state.tree, commit,
8343 error = set_tog_base_commit(repo, worktree);
8347 /* Release work tree lock. */
8348 got_worktree_close(worktree);
8352 error = view_loop(view);
8355 free(tog_base_commit.id);
8356 free(keyword_idstr);
8362 got_object_commit_close(commit);
8365 if (worktree != NULL)
8366 got_worktree_close(worktree);
8368 const struct got_error *close_err = got_repo_close(repo);
8373 const struct got_error *pack_err =
8374 got_repo_pack_fds_close(pack_fds);
8382 static const struct got_error *
8383 ref_view_load_refs(struct tog_ref_view_state *s)
8385 struct got_reflist_entry *sre;
8386 struct tog_reflist_entry *re;
8389 TAILQ_FOREACH(sre, &tog_refs, entry) {
8390 if (strncmp(got_ref_get_name(sre->ref),
8391 "refs/got/", 9) == 0 &&
8392 strncmp(got_ref_get_name(sre->ref),
8393 "refs/got/backup/", 16) != 0)
8396 re = malloc(sizeof(*re));
8398 return got_error_from_errno("malloc");
8400 re->ref = got_ref_dup(sre->ref);
8401 if (re->ref == NULL)
8402 return got_error_from_errno("got_ref_dup");
8403 re->idx = s->nrefs++;
8404 TAILQ_INSERT_TAIL(&s->refs, re, entry);
8407 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8412 ref_view_free_refs(struct tog_ref_view_state *s)
8414 struct tog_reflist_entry *re;
8416 while (!TAILQ_EMPTY(&s->refs)) {
8417 re = TAILQ_FIRST(&s->refs);
8418 TAILQ_REMOVE(&s->refs, re, entry);
8419 got_ref_close(re->ref);
8424 static const struct got_error *
8425 open_ref_view(struct tog_view *view, struct got_repository *repo)
8427 const struct got_error *err = NULL;
8428 struct tog_ref_view_state *s = &view->state.ref;
8430 s->selected_entry = 0;
8433 TAILQ_INIT(&s->refs);
8434 STAILQ_INIT(&s->colors);
8436 err = ref_view_load_refs(s);
8440 if (has_colors() && getenv("TOG_COLORS") != NULL) {
8441 err = add_color(&s->colors, "^refs/heads/",
8442 TOG_COLOR_REFS_HEADS,
8443 get_color_value("TOG_COLOR_REFS_HEADS"));
8447 err = add_color(&s->colors, "^refs/tags/",
8448 TOG_COLOR_REFS_TAGS,
8449 get_color_value("TOG_COLOR_REFS_TAGS"));
8453 err = add_color(&s->colors, "^refs/remotes/",
8454 TOG_COLOR_REFS_REMOTES,
8455 get_color_value("TOG_COLOR_REFS_REMOTES"));
8459 err = add_color(&s->colors, "^refs/got/backup/",
8460 TOG_COLOR_REFS_BACKUP,
8461 get_color_value("TOG_COLOR_REFS_BACKUP"));
8466 view->show = show_ref_view;
8467 view->input = input_ref_view;
8468 view->close = close_ref_view;
8469 view->search_start = search_start_ref_view;
8470 view->search_next = search_next_ref_view;
8473 if (view->close == NULL)
8474 close_ref_view(view);
8480 static const struct got_error *
8481 close_ref_view(struct tog_view *view)
8483 struct tog_ref_view_state *s = &view->state.ref;
8485 ref_view_free_refs(s);
8486 free_colors(&s->colors);
8491 static const struct got_error *
8492 resolve_reflist_entry(struct got_object_id **commit_id,
8493 struct tog_reflist_entry *re, struct got_repository *repo)
8495 const struct got_error *err = NULL;
8496 struct got_object_id *obj_id;
8497 struct got_tag_object *tag = NULL;
8502 err = got_ref_resolve(&obj_id, repo, re->ref);
8506 err = got_object_get_type(&obj_type, repo, obj_id);
8511 case GOT_OBJ_TYPE_COMMIT:
8512 *commit_id = obj_id;
8514 case GOT_OBJ_TYPE_TAG:
8515 err = got_object_open_as_tag(&tag, repo, obj_id);
8519 err = got_object_get_type(&obj_type, repo,
8520 got_object_tag_get_object_id(tag));
8523 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
8524 err = got_error(GOT_ERR_OBJ_TYPE);
8527 *commit_id = got_object_id_dup(
8528 got_object_tag_get_object_id(tag));
8529 if (*commit_id == NULL) {
8530 err = got_error_from_errno("got_object_id_dup");
8535 err = got_error(GOT_ERR_OBJ_TYPE);
8541 got_object_tag_close(tag);
8549 static const struct got_error *
8550 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
8551 struct tog_reflist_entry *re, struct got_repository *repo)
8553 struct tog_view *log_view;
8554 const struct got_error *err = NULL;
8555 struct got_object_id *commit_id = NULL;
8559 err = resolve_reflist_entry(&commit_id, re, repo);
8561 if (err->code != GOT_ERR_OBJ_TYPE)
8567 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
8568 if (log_view == NULL) {
8569 err = got_error_from_errno("view_open");
8573 err = open_log_view(log_view, commit_id, repo,
8574 got_ref_get_name(re->ref), "", 0, NULL);
8577 view_close(log_view);
8579 *new_view = log_view;
8585 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
8587 struct tog_reflist_entry *re;
8590 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8593 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
8594 while (i++ < maxscroll) {
8597 s->first_displayed_entry = re;
8598 re = TAILQ_PREV(re, tog_reflist_head, entry);
8602 static const struct got_error *
8603 ref_scroll_down(struct tog_view *view, int maxscroll)
8605 struct tog_ref_view_state *s = &view->state.ref;
8606 struct tog_reflist_entry *next, *last;
8609 if (s->first_displayed_entry)
8610 next = TAILQ_NEXT(s->first_displayed_entry, entry);
8612 next = TAILQ_FIRST(&s->refs);
8614 last = s->last_displayed_entry;
8615 while (next && n++ < maxscroll) {
8617 s->last_displayed_entry = last;
8618 last = TAILQ_NEXT(last, entry);
8620 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
8621 s->first_displayed_entry = next;
8622 next = TAILQ_NEXT(next, entry);
8629 static const struct got_error *
8630 search_start_ref_view(struct tog_view *view)
8632 struct tog_ref_view_state *s = &view->state.ref;
8634 s->matched_entry = NULL;
8639 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8641 regmatch_t regmatch;
8643 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8647 static const struct got_error *
8648 search_next_ref_view(struct tog_view *view)
8650 struct tog_ref_view_state *s = &view->state.ref;
8651 struct tog_reflist_entry *re = NULL;
8653 if (!view->searching) {
8654 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8658 if (s->matched_entry) {
8659 if (view->searching == TOG_SEARCH_FORWARD) {
8660 if (s->selected_entry)
8661 re = TAILQ_NEXT(s->selected_entry, entry);
8663 re = TAILQ_PREV(s->selected_entry,
8664 tog_reflist_head, entry);
8666 if (s->selected_entry == NULL)
8667 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8669 re = TAILQ_PREV(s->selected_entry,
8670 tog_reflist_head, entry);
8673 if (s->selected_entry)
8674 re = s->selected_entry;
8675 else if (view->searching == TOG_SEARCH_FORWARD)
8676 re = TAILQ_FIRST(&s->refs);
8678 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8683 if (s->matched_entry == NULL) {
8684 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8687 if (view->searching == TOG_SEARCH_FORWARD)
8688 re = TAILQ_FIRST(&s->refs);
8690 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8693 if (match_reflist_entry(re, &view->regex)) {
8694 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8695 s->matched_entry = re;
8699 if (view->searching == TOG_SEARCH_FORWARD)
8700 re = TAILQ_NEXT(re, entry);
8702 re = TAILQ_PREV(re, tog_reflist_head, entry);
8705 if (s->matched_entry) {
8706 s->first_displayed_entry = s->matched_entry;
8713 static const struct got_error *
8714 show_ref_view(struct tog_view *view)
8716 const struct got_error *err = NULL;
8717 struct tog_ref_view_state *s = &view->state.ref;
8718 struct tog_reflist_entry *re;
8721 struct tog_color *tc;
8722 int width, n, scrollx;
8723 int limit = view->nlines;
8725 werase(view->window);
8728 if (view_is_hsplit_top(view))
8729 --limit; /* border */
8734 re = s->first_displayed_entry;
8736 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8738 return got_error_from_errno("asprintf");
8740 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8745 if (view_needs_focus_indication(view))
8746 wstandout(view->window);
8747 waddwstr(view->window, wline);
8748 while (width++ < view->ncols)
8749 waddch(view->window, ' ');
8750 if (view_needs_focus_indication(view))
8751 wstandend(view->window);
8761 while (re && limit > 0) {
8763 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8766 struct got_commit_object *ci;
8767 struct got_tag_object *tag;
8768 struct got_object_id *id;
8772 err = got_ref_resolve(&id, s->repo, re->ref);
8775 err = got_object_open_as_tag(&tag, s->repo, id);
8777 if (err->code != GOT_ERR_OBJ_TYPE) {
8781 err = got_object_open_as_commit(&ci, s->repo,
8787 t = got_object_commit_get_committer_time(ci);
8788 got_object_commit_close(ci);
8790 t = got_object_tag_get_tagger_time(tag);
8791 got_object_tag_close(tag);
8794 if (gmtime_r(&t, &tm) == NULL)
8795 return got_error_from_errno("gmtime_r");
8796 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8797 return got_error(GOT_ERR_NO_SPACE);
8799 if (got_ref_is_symbolic(re->ref)) {
8800 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8801 ymd : "", got_ref_get_name(re->ref),
8802 got_ref_get_symref_target(re->ref)) == -1)
8803 return got_error_from_errno("asprintf");
8804 } else if (s->show_ids) {
8805 struct got_object_id *id;
8807 err = got_ref_resolve(&id, s->repo, re->ref);
8810 err = got_object_id_str(&id_str, id);
8815 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8816 got_ref_get_name(re->ref), id_str) == -1) {
8817 err = got_error_from_errno("asprintf");
8824 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8825 got_ref_get_name(re->ref)) == -1)
8826 return got_error_from_errno("asprintf");
8828 /* use full line width to determine view->maxx */
8829 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8834 view->maxx = MAX(view->maxx, width);
8838 err = format_line(&wline, &width, &scrollx, line, view->x,
8844 if (n == s->selected) {
8846 wstandout(view->window);
8847 s->selected_entry = re;
8849 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8851 wattr_on(view->window,
8852 COLOR_PAIR(tc->colorpair), NULL);
8853 waddwstr(view->window, &wline[scrollx]);
8855 wattr_off(view->window,
8856 COLOR_PAIR(tc->colorpair), NULL);
8857 if (width < view->ncols)
8858 waddch(view->window, '\n');
8859 if (n == s->selected && view->focussed)
8860 wstandend(view->window);
8866 s->last_displayed_entry = re;
8869 re = TAILQ_NEXT(re, entry);
8876 static const struct got_error *
8877 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8878 struct tog_reflist_entry *re, struct got_repository *repo)
8880 const struct got_error *err = NULL;
8881 struct got_object_id *commit_id = NULL;
8882 struct tog_view *tree_view;
8886 err = resolve_reflist_entry(&commit_id, re, repo);
8888 if (err->code != GOT_ERR_OBJ_TYPE)
8895 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8896 if (tree_view == NULL) {
8897 err = got_error_from_errno("view_open");
8901 err = open_tree_view(tree_view, commit_id,
8902 got_ref_get_name(re->ref), repo);
8906 *new_view = tree_view;
8912 static const struct got_error *
8913 ref_goto_line(struct tog_view *view, int nlines)
8915 const struct got_error *err = NULL;
8916 struct tog_ref_view_state *s = &view->state.ref;
8917 int g, idx = s->selected_entry->idx;
8924 else if (g > s->nrefs)
8927 if (g >= s->first_displayed_entry->idx + 1 &&
8928 g <= s->last_displayed_entry->idx + 1 &&
8929 g - s->first_displayed_entry->idx - 1 < nlines) {
8930 s->selected = g - s->first_displayed_entry->idx - 1;
8935 err = ref_scroll_down(view, g - idx - 1);
8938 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8939 s->first_displayed_entry->idx + s->selected < g &&
8940 s->selected < s->ndisplayed - 1)
8941 s->selected = g - s->first_displayed_entry->idx - 1;
8942 } else if (idx + 1 > g)
8943 ref_scroll_up(s, idx - g + 1);
8945 if (g < nlines && s->first_displayed_entry->idx == 0)
8946 s->selected = g - 1;
8952 static const struct got_error *
8953 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8955 const struct got_error *err = NULL;
8956 struct tog_ref_view_state *s = &view->state.ref;
8957 struct tog_reflist_entry *re;
8958 int n, nscroll = view->nlines - 1;
8961 return ref_goto_line(view, nscroll);
8970 horizontal_scroll_input(view, ch);
8973 s->show_ids = !s->show_ids;
8977 s->show_date = !s->show_date;
8981 s->sort_by_date = !s->sort_by_date;
8982 view->action = s->sort_by_date ? "sort by date" : "sort by name";
8984 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8985 got_ref_cmp_by_commit_timestamp_descending :
8986 tog_ref_cmp_by_name, s->repo);
8989 got_reflist_object_id_map_free(tog_refs_idmap);
8990 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8991 &tog_refs, s->repo);
8994 ref_view_free_refs(s);
8995 err = ref_view_load_refs(s);
9000 if (!s->selected_entry)
9002 err = view_request_new(new_view, view, TOG_VIEW_LOG);
9006 if (!s->selected_entry)
9008 err = view_request_new(new_view, view, TOG_VIEW_TREE);
9015 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
9020 int eos = view->nlines - 1;
9022 if (view->mode == TOG_VIEW_SPLIT_HRZN)
9026 re = TAILQ_LAST(&s->refs, tog_reflist_head);
9027 for (n = 0; n < eos; n++) {
9030 s->first_displayed_entry = re;
9031 re = TAILQ_PREV(re, tog_reflist_head, entry);
9034 s->selected = n - 1;
9040 if (s->selected > 0) {
9044 ref_scroll_up(s, 1);
9045 if (s->selected_entry == TAILQ_FIRST(&s->refs))
9055 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
9056 s->selected -= MIN(nscroll, s->selected);
9057 ref_scroll_up(s, MAX(0, nscroll));
9058 if (s->selected_entry == TAILQ_FIRST(&s->refs))
9064 if (s->selected < s->ndisplayed - 1) {
9068 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
9069 /* can't scroll any further */
9073 ref_scroll_down(view, 1);
9083 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
9084 /* can't scroll any further; move cursor down */
9085 if (s->selected < s->ndisplayed - 1)
9086 s->selected += MIN(nscroll,
9087 s->ndisplayed - s->selected - 1);
9088 if (view->count > 1 && s->selected < s->ndisplayed - 1)
9089 s->selected += s->ndisplayed - s->selected - 1;
9093 ref_scroll_down(view, nscroll);
9098 err = tog_load_refs(s->repo, s->sort_by_date);
9101 ref_view_free_refs(s);
9102 err = ref_view_load_refs(s);
9105 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
9106 s->selected = view->nlines - 2;
9120 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
9125 static const struct got_error *
9126 cmd_ref(int argc, char *argv[])
9128 const struct got_error *error;
9129 struct got_repository *repo = NULL;
9130 struct got_worktree *worktree = NULL;
9131 char *cwd = NULL, *repo_path = NULL;
9133 struct tog_view *view;
9134 int *pack_fds = NULL;
9136 while ((ch = getopt(argc, argv, "r:")) != -1) {
9139 repo_path = realpath(optarg, NULL);
9140 if (repo_path == NULL)
9141 return got_error_from_errno2("realpath",
9156 error = got_repo_pack_fds_open(&pack_fds);
9160 if (repo_path == NULL) {
9161 cwd = getcwd(NULL, 0);
9163 return got_error_from_errno("getcwd");
9164 error = got_worktree_open(&worktree, cwd, NULL);
9165 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9169 strdup(got_worktree_get_repo_path(worktree));
9171 repo_path = strdup(cwd);
9172 if (repo_path == NULL) {
9173 error = got_error_from_errno("strdup");
9178 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9184 error = apply_unveil(got_repo_get_path(repo), NULL);
9188 error = tog_load_refs(repo, 0);
9192 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
9194 error = got_error_from_errno("view_open");
9198 error = open_ref_view(view, repo);
9203 error = set_tog_base_commit(repo, worktree);
9207 /* Release work tree lock. */
9208 got_worktree_close(worktree);
9212 error = view_loop(view);
9215 free(tog_base_commit.id);
9218 if (worktree != NULL)
9219 got_worktree_close(worktree);
9221 const struct got_error *close_err = got_repo_close(repo);
9226 const struct got_error *pack_err =
9227 got_repo_pack_fds_close(pack_fds);
9235 static const struct got_error*
9236 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
9245 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
9249 if (mvwprintw(win, y, x, "%s", str) == ERR)
9250 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
9257 static const struct got_error *
9258 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
9262 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
9264 free(*line_offsets);
9265 *line_offsets = NULL;
9266 return got_error_from_errno("reallocarray");
9270 (*line_offsets)[*nlines] = off;
9275 static const struct got_error *
9276 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
9280 for (;n > 0; --n, ++km) {
9284 if (km->keys == NULL)
9287 t = t0 = strdup(km->keys);
9289 return got_error_from_errno("strdup");
9292 while ((k = strsep(&t, " ")) != NULL)
9293 len += strlen(k) > 1 ? 2 : 0;
9295 *ret = MAX(*ret, len);
9302 * Write keymap section headers, keys, and key info in km to f.
9303 * Save line offset to *off. If terminal has UTF8 encoding enabled,
9304 * wrap control and symbolic keys in guillemets, else use <>.
9306 static const struct got_error *
9307 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
9312 static const char *u8_glyph[] = {
9313 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
9314 "\xe2\x80\xba" /* U+203A (utf8 >) */
9317 int cs, s, first = 1;
9319 cs = got_locale_is_utf8();
9321 t = t0 = strdup(km->keys);
9323 return got_error_from_errno("strdup");
9325 len = strlen(km->keys);
9326 while ((k = strsep(&t, " ")) != NULL) {
9327 s = strlen(k) > 1; /* control or symbolic key */
9328 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
9329 cs && s ? u8_glyph[0] : s ? "<" : "", k,
9330 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
9333 return got_error_from_errno("fprintf");
9341 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
9343 return got_error_from_errno("fprintf");
9349 static const struct got_error *
9350 format_help(struct tog_help_view_state *s)
9352 const struct got_error *err = NULL;
9354 int i, max, n, show = s->all;
9355 static const struct tog_key_map km[] = {
9356 #define KEYMAP_(info, type) { NULL, (info), type }
9357 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
9363 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
9368 err = max_key_str(&max, km, n);
9372 for (i = 0; i < n; ++i) {
9373 if (km[i].keys == NULL) {
9375 if (km[i].type == TOG_KEYMAP_GLOBAL ||
9376 km[i].type == s->type || s->all)
9380 err = format_help_line(&off, s->f, &km[i], max);
9383 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9390 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9394 static const struct got_error *
9395 create_help(struct tog_help_view_state *s)
9398 const struct got_error *err;
9400 free(s->line_offsets);
9401 s->line_offsets = NULL;
9406 return got_error_from_errno("got_opentemp");
9409 err = format_help(s);
9413 if (s->f && fflush(s->f) != 0)
9414 return got_error_from_errno("fflush");
9419 static const struct got_error *
9420 search_start_help_view(struct tog_view *view)
9422 view->state.help.matched_line = 0;
9427 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
9428 size_t *nlines, int **first, int **last, int **match, int **selected)
9430 struct tog_help_view_state *s = &view->state.help;
9433 *nlines = s->nlines;
9434 *line_offsets = s->line_offsets;
9435 *match = &s->matched_line;
9436 *first = &s->first_displayed_line;
9437 *last = &s->last_displayed_line;
9438 *selected = &s->selected_line;
9441 static const struct got_error *
9442 show_help_view(struct tog_view *view)
9444 struct tog_help_view_state *s = &view->state.help;
9445 const struct got_error *err;
9446 regmatch_t *regmatch = &view->regmatch;
9451 int width, nprinted = 0, rc = 0;
9452 int eos = view->nlines;
9454 if (view_is_hsplit_top(view))
9455 --eos; /* account for border */
9459 werase(view->window);
9461 if (view->gline > s->nlines - 1)
9462 view->gline = s->nlines - 1;
9464 err = win_draw_center(view->window, 0, 0, view->ncols,
9465 view_needs_focus_indication(view),
9466 "tog help (press q to return to tog)");
9471 waddstr(view->window, "\n\n");
9477 while (eos > 0 && nprinted < eos) {
9480 linelen = getline(&line, &linesz, s->f);
9481 if (linelen == -1) {
9484 return got_ferror(s->f, GOT_ERR_IO);
9489 if (++s->lineno < s->first_displayed_line)
9491 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
9493 if (s->lineno == view->hiline)
9496 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
9502 view->maxx = MAX(view->maxx, width);
9507 wattron(view->window, attr);
9508 if (s->first_displayed_line + nprinted == s->matched_line &&
9509 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
9510 err = add_matched_line(&width, line, view->ncols - 1, 0,
9511 view->window, view->x, regmatch);
9519 err = format_line(&wline, &width, &skip, line,
9520 view->x, view->ncols, 0, view->x ? 1 : 0);
9525 waddwstr(view->window, &wline[skip]);
9529 if (s->lineno == view->hiline) {
9530 while (width++ < view->ncols)
9531 waddch(view->window, ' ');
9533 if (width < view->ncols)
9534 waddch(view->window, '\n');
9537 wattroff(view->window, attr);
9538 if (++nprinted == 1)
9539 s->first_displayed_line = s->lineno;
9543 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
9545 s->last_displayed_line = s->first_displayed_line;
9550 rc = waddnstr(view->window,
9551 "See the tog(1) manual page for full documentation",
9554 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9556 wmove(view->window, view->nlines - 1, 0);
9557 wclrtoeol(view->window);
9558 wstandout(view->window);
9559 rc = waddnstr(view->window, "scroll down for more...",
9562 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9563 if (getcurx(view->window) < view->ncols - 6) {
9564 rc = wprintw(view->window, "[%.0f%%]",
9565 100.00 * s->last_displayed_line / s->nlines);
9567 return got_error_msg(GOT_ERR_IO, "wprintw");
9569 wstandend(view->window);
9575 static const struct got_error *
9576 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
9578 struct tog_help_view_state *s = &view->state.help;
9579 const struct got_error *err = NULL;
9585 eos = nscroll = view->nlines;
9586 if (view_is_hsplit_top(view))
9589 s->lineno = s->first_displayed_line - 1 + s->selected_line;
9598 horizontal_scroll_input(view, ch);
9602 s->first_displayed_line = 1;
9610 s->first_displayed_line = (s->nlines - eos) + 3;
9615 if (s->first_displayed_line > 1)
9616 --s->first_displayed_line;
9627 if (s->first_displayed_line == 1) {
9631 while (--nscroll > 0 && s->first_displayed_line > 1)
9632 s->first_displayed_line--;
9638 ++s->first_displayed_line;
9654 while (!s->eof && --nscroll > 0) {
9655 linelen = getline(&line, &linesz, s->f);
9656 s->first_displayed_line++;
9657 if (linelen == -1) {
9661 err = got_ferror(s->f, GOT_ERR_IO);
9675 static const struct got_error *
9676 close_help_view(struct tog_view *view)
9678 struct tog_help_view_state *s = &view->state.help;
9680 free(s->line_offsets);
9681 s->line_offsets = NULL;
9682 if (fclose(s->f) == EOF)
9683 return got_error_from_errno("fclose");
9688 static const struct got_error *
9689 reset_help_view(struct tog_view *view)
9691 struct tog_help_view_state *s = &view->state.help;
9694 if (s->f && fclose(s->f) == EOF)
9695 return got_error_from_errno("fclose");
9697 wclear(view->window);
9701 s->first_displayed_line = 1;
9702 s->last_displayed_line = view->nlines;
9703 s->matched_line = 0;
9705 return create_help(s);
9708 static const struct got_error *
9709 open_help_view(struct tog_view *view, struct tog_view *parent)
9711 const struct got_error *err = NULL;
9712 struct tog_help_view_state *s = &view->state.help;
9714 s->type = (enum tog_keymap_type)parent->type;
9715 s->first_displayed_line = 1;
9716 s->last_displayed_line = view->nlines;
9717 s->selected_line = 1;
9719 view->show = show_help_view;
9720 view->input = input_help_view;
9721 view->reset = reset_help_view;
9722 view->close = close_help_view;
9723 view->search_start = search_start_help_view;
9724 view->search_setup = search_setup_help_view;
9725 view->search_next = search_next_view_match;
9727 err = create_help(s);
9731 static const struct got_error *
9732 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9733 enum tog_view_type request, int y, int x)
9735 const struct got_error *err = NULL;
9741 if (view->type == TOG_VIEW_LOG) {
9742 struct tog_log_view_state *s = &view->state.log;
9744 err = open_diff_view_for_commit(new_view, y, x,
9745 s->selected_entry->commit, s->selected_entry->id,
9748 return got_error_msg(GOT_ERR_NOT_IMPL,
9749 "parent/child view pair not supported");
9751 case TOG_VIEW_BLAME:
9752 if (view->type == TOG_VIEW_TREE) {
9753 struct tog_tree_view_state *s = &view->state.tree;
9755 err = blame_tree_entry(new_view, y, x,
9756 s->selected_entry, &s->parents, s->commit_id,
9759 return got_error_msg(GOT_ERR_NOT_IMPL,
9760 "parent/child view pair not supported");
9763 if (view->type == TOG_VIEW_BLAME)
9764 err = log_annotated_line(new_view, y, x,
9765 view->state.blame.repo, view->state.blame.id_to_log);
9766 else if (view->type == TOG_VIEW_TREE)
9767 err = log_selected_tree_entry(new_view, y, x,
9769 else if (view->type == TOG_VIEW_REF)
9770 err = log_ref_entry(new_view, y, x,
9771 view->state.ref.selected_entry,
9772 view->state.ref.repo);
9774 return got_error_msg(GOT_ERR_NOT_IMPL,
9775 "parent/child view pair not supported");
9778 if (view->type == TOG_VIEW_LOG)
9779 err = browse_commit_tree(new_view, y, x,
9780 view->state.log.selected_entry,
9781 view->state.log.in_repo_path,
9782 view->state.log.head_ref_name,
9783 view->state.log.repo);
9784 else if (view->type == TOG_VIEW_REF)
9785 err = browse_ref_tree(new_view, y, x,
9786 view->state.ref.selected_entry,
9787 view->state.ref.repo);
9789 return got_error_msg(GOT_ERR_NOT_IMPL,
9790 "parent/child view pair not supported");
9793 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9794 if (*new_view == NULL)
9795 return got_error_from_errno("view_open");
9796 if (view->type == TOG_VIEW_LOG)
9797 err = open_ref_view(*new_view, view->state.log.repo);
9798 else if (view->type == TOG_VIEW_TREE)
9799 err = open_ref_view(*new_view, view->state.tree.repo);
9801 err = got_error_msg(GOT_ERR_NOT_IMPL,
9802 "parent/child view pair not supported");
9804 view_close(*new_view);
9807 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9808 if (*new_view == NULL)
9809 return got_error_from_errno("view_open");
9810 err = open_help_view(*new_view, view);
9812 view_close(*new_view);
9815 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9822 * If view was scrolled down to move the selected line into view when opening a
9823 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9826 offset_selection_up(struct tog_view *view)
9828 switch (view->type) {
9829 case TOG_VIEW_BLAME: {
9830 struct tog_blame_view_state *s = &view->state.blame;
9831 if (s->first_displayed_line == 1) {
9832 s->selected_line = MAX(s->selected_line - view->offset,
9836 if (s->first_displayed_line > view->offset)
9837 s->first_displayed_line -= view->offset;
9839 s->first_displayed_line = 1;
9840 s->selected_line += view->offset;
9844 log_scroll_up(&view->state.log, view->offset);
9845 view->state.log.selected += view->offset;
9848 ref_scroll_up(&view->state.ref, view->offset);
9849 view->state.ref.selected += view->offset;
9852 tree_scroll_up(&view->state.tree, view->offset);
9853 view->state.tree.selected += view->offset;
9863 * If the selected line is in the section of screen covered by the bottom split,
9864 * scroll down offset lines to move it into view and index its new position.
9866 static const struct got_error *
9867 offset_selection_down(struct tog_view *view)
9869 const struct got_error *err = NULL;
9870 const struct got_error *(*scrolld)(struct tog_view *, int);
9871 int *selected = NULL;
9874 switch (view->type) {
9875 case TOG_VIEW_BLAME: {
9876 struct tog_blame_view_state *s = &view->state.blame;
9879 if (s->selected_line > view->nlines - header) {
9880 offset = abs(view->nlines - s->selected_line - header);
9881 s->first_displayed_line += offset;
9882 s->selected_line -= offset;
9883 view->offset = offset;
9887 case TOG_VIEW_LOG: {
9888 struct tog_log_view_state *s = &view->state.log;
9889 scrolld = &log_scroll_down;
9890 header = view_is_parent_view(view) ? 3 : 2;
9891 selected = &s->selected;
9894 case TOG_VIEW_REF: {
9895 struct tog_ref_view_state *s = &view->state.ref;
9896 scrolld = &ref_scroll_down;
9898 selected = &s->selected;
9901 case TOG_VIEW_TREE: {
9902 struct tog_tree_view_state *s = &view->state.tree;
9903 scrolld = &tree_scroll_down;
9905 selected = &s->selected;
9915 if (selected && *selected > view->nlines - header) {
9916 offset = abs(view->nlines - *selected - header);
9917 view->offset = offset;
9918 if (scrolld && offset) {
9919 err = scrolld(view, offset);
9920 *selected -= offset;
9928 list_commands(FILE *fp)
9932 fprintf(fp, "commands:");
9933 for (i = 0; i < nitems(tog_commands); i++) {
9934 const struct tog_cmd *cmd = &tog_commands[i];
9935 fprintf(fp, " %s", cmd->name);
9941 usage(int hflag, int status)
9943 FILE *fp = (status == 0) ? stdout : stderr;
9945 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9948 fprintf(fp, "lazy usage: %s path\n", getprogname());
9955 make_argv(int argc, ...)
9963 argv = calloc(argc, sizeof(char *));
9966 for (i = 0; i < argc; i++) {
9967 argv[i] = strdup(va_arg(ap, char *));
9968 if (argv[i] == NULL)
9977 * Try to convert 'tog path' into a 'tog log path' command.
9978 * The user could simply have mistyped the command rather than knowingly
9979 * provided a path. So check whether argv[0] can in fact be resolved
9980 * to a path in the HEAD commit and print a special error if not.
9981 * This hack is for mpi@ <3
9983 static const struct got_error *
9984 tog_log_with_path(int argc, char *argv[])
9986 const struct got_error *error = NULL, *close_err;
9987 const struct tog_cmd *cmd = NULL;
9988 struct got_repository *repo = NULL;
9989 struct got_worktree *worktree = NULL;
9990 struct got_object_id *commit_id = NULL, *id = NULL;
9991 struct got_commit_object *commit = NULL;
9992 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9993 char *commit_id_str = NULL, **cmd_argv = NULL;
9994 int *pack_fds = NULL;
9996 cwd = getcwd(NULL, 0);
9998 return got_error_from_errno("getcwd");
10000 error = got_repo_pack_fds_open(&pack_fds);
10004 error = got_worktree_open(&worktree, cwd, NULL);
10005 if (error && error->code != GOT_ERR_NOT_WORKTREE)
10009 repo_path = strdup(got_worktree_get_repo_path(worktree));
10011 repo_path = strdup(cwd);
10012 if (repo_path == NULL) {
10013 error = got_error_from_errno("strdup");
10017 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
10021 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
10026 error = tog_load_refs(repo, 0);
10029 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
10030 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
10031 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
10036 got_worktree_close(worktree);
10040 error = got_object_open_as_commit(&commit, repo, commit_id);
10044 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
10046 if (error->code != GOT_ERR_NO_TREE_ENTRY)
10048 fprintf(stderr, "%s: '%s' is no known command or path\n",
10049 getprogname(), argv[0]);
10054 error = got_object_id_str(&commit_id_str, commit_id);
10058 cmd = &tog_commands[0]; /* log */
10060 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
10061 error = cmd->cmd_main(argc, cmd_argv);
10064 close_err = got_repo_close(repo);
10069 got_object_commit_close(commit);
10071 got_worktree_close(worktree);
10073 const struct got_error *pack_err =
10074 got_repo_pack_fds_close(pack_fds);
10079 free(commit_id_str);
10083 free(in_repo_path);
10086 for (i = 0; i < argc; i++)
10095 main(int argc, char *argv[])
10097 const struct got_error *io_err, *error = NULL;
10098 const struct tog_cmd *cmd = NULL;
10099 int ch, hflag = 0, Vflag = 0;
10100 char **cmd_argv = NULL;
10101 static const struct option longopts[] = {
10102 { "version", no_argument, NULL, 'V' },
10103 { NULL, 0, NULL, 0}
10105 char *diff_algo_str = NULL;
10106 const char *test_script_path;
10108 setlocale(LC_CTYPE, "");
10111 * Override default signal handlers before starting ncurses.
10112 * This should prevent ncurses from installing its own
10113 * broken cleanup() signal handler.
10115 signal(SIGWINCH, tog_sigwinch);
10116 signal(SIGPIPE, tog_sigpipe);
10117 signal(SIGCONT, tog_sigcont);
10118 signal(SIGINT, tog_sigint);
10119 signal(SIGTERM, tog_sigterm);
10122 * Test mode init must happen before pledge() because "tty" will
10123 * not allow TTY-related ioctls to occur via regular files.
10125 test_script_path = getenv("TOG_TEST_SCRIPT");
10126 if (test_script_path != NULL) {
10127 error = init_mock_term(test_script_path);
10129 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
10132 } else if (!isatty(STDIN_FILENO))
10133 errx(1, "standard input is not a tty");
10135 #if !defined(PROFILE)
10136 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
10141 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
10161 got_version_print_str();
10168 /* Build an argument vector which runs a default command. */
10169 cmd = &tog_commands[0];
10171 cmd_argv = make_argv(argc, cmd->name);
10175 /* Did the user specify a command? */
10176 for (i = 0; i < nitems(tog_commands); i++) {
10177 if (strncmp(tog_commands[i].name, argv[0],
10178 strlen(argv[0])) == 0) {
10179 cmd = &tog_commands[i];
10185 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
10186 if (diff_algo_str) {
10187 if (strcasecmp(diff_algo_str, "patience") == 0)
10188 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
10189 if (strcasecmp(diff_algo_str, "myers") == 0)
10190 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
10193 tog_base_commit.idx = -1;
10194 tog_base_commit.marker = GOT_WORKTREE_STATE_UNKNOWN;
10199 /* No command specified; try log with a path */
10200 error = tog_log_with_path(argc, argv);
10205 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
10208 if (using_mock_io) {
10209 io_err = tog_io_close();
10216 for (i = 0; i < argc; i++)
10221 if (error && error->code != GOT_ERR_CANCELLED &&
10222 error->code != GOT_ERR_EOF &&
10223 error->code != GOT_ERR_PRIVSEP_EXIT &&
10224 error->code != GOT_ERR_PRIVSEP_PIPE &&
10225 !(error->code == GOT_ERR_ERRNO && errno == EINTR)) {
10226 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);