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_MYERS;
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;
374 struct commit_queue_entry **first_displayed_entry;
375 struct commit_queue_entry **selected_entry;
377 int *search_next_done;
381 regex_t *limit_regex;
382 struct commit_queue *limit_commits;
385 struct tog_log_view_state {
386 struct commit_queue *commits;
387 struct commit_queue_entry *first_displayed_entry;
388 struct commit_queue_entry *last_displayed_entry;
389 struct commit_queue_entry *selected_entry;
390 struct commit_queue real_commits;
395 struct got_repository *repo;
396 struct got_object_id *start_id;
399 struct tog_log_thread_args thread_args;
400 struct commit_queue_entry *matched_entry;
401 struct commit_queue_entry *search_entry;
402 struct tog_colors colors;
406 struct commit_queue limit_commits;
409 #define TOG_COLOR_DIFF_MINUS 1
410 #define TOG_COLOR_DIFF_PLUS 2
411 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
412 #define TOG_COLOR_DIFF_META 4
413 #define TOG_COLOR_TREE_SUBMODULE 5
414 #define TOG_COLOR_TREE_SYMLINK 6
415 #define TOG_COLOR_TREE_DIRECTORY 7
416 #define TOG_COLOR_TREE_EXECUTABLE 8
417 #define TOG_COLOR_COMMIT 9
418 #define TOG_COLOR_AUTHOR 10
419 #define TOG_COLOR_DATE 11
420 #define TOG_COLOR_REFS_HEADS 12
421 #define TOG_COLOR_REFS_TAGS 13
422 #define TOG_COLOR_REFS_REMOTES 14
423 #define TOG_COLOR_REFS_BACKUP 15
425 struct tog_blame_cb_args {
426 struct tog_blame_line *lines; /* one per line */
429 struct tog_view *view;
430 struct got_object_id *commit_id;
434 struct tog_blame_thread_args {
436 struct got_repository *repo;
437 struct tog_blame_cb_args *cb_args;
439 got_cancel_cb cancel_cb;
441 pthread_cond_t blame_complete;
447 struct tog_blame_line *lines;
451 struct tog_blame_thread_args thread_args;
452 struct tog_blame_cb_args cb_args;
457 struct tog_blame_view_state {
458 int first_displayed_line;
459 int last_displayed_line;
461 int last_diffed_line;
465 struct got_object_id_queue blamed_commits;
466 struct got_object_qid *blamed_commit;
468 struct got_repository *repo;
469 struct got_object_id *commit_id;
470 struct got_object_id *id_to_log;
471 struct tog_blame blame;
473 struct tog_colors colors;
476 struct tog_parent_tree {
477 TAILQ_ENTRY(tog_parent_tree) entry;
478 struct got_tree_object *tree;
479 struct got_tree_entry *first_displayed_entry;
480 struct got_tree_entry *selected_entry;
484 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
486 struct tog_tree_view_state {
488 struct got_object_id *commit_id;/* commit which this tree belongs to */
489 struct got_tree_object *root; /* the commit's root tree entry */
490 struct got_tree_object *tree; /* currently displayed (sub-)tree */
491 struct got_tree_entry *first_displayed_entry;
492 struct got_tree_entry *last_displayed_entry;
493 struct got_tree_entry *selected_entry;
494 int ndisplayed, selected, show_ids;
495 struct tog_parent_trees parents; /* parent trees of current sub-tree */
497 struct got_repository *repo;
498 struct got_tree_entry *matched_entry;
499 struct tog_colors colors;
502 struct tog_reflist_entry {
503 TAILQ_ENTRY(tog_reflist_entry) entry;
504 struct got_reference *ref;
508 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
510 struct tog_ref_view_state {
511 struct tog_reflist_head refs;
512 struct tog_reflist_entry *first_displayed_entry;
513 struct tog_reflist_entry *last_displayed_entry;
514 struct tog_reflist_entry *selected_entry;
515 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
516 struct got_repository *repo;
517 struct tog_reflist_entry *matched_entry;
518 struct tog_colors colors;
521 struct tog_help_view_state {
526 int first_displayed_line;
527 int last_displayed_line;
532 enum tog_keymap_type type;
535 #define GENERATE_HELP \
536 KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
537 KEY_("H F1", "Open view-specific help (double tap for all help)"), \
538 KEY_("k C-p Up", "Move cursor or page up one line"), \
539 KEY_("j C-n Down", "Move cursor or page down one line"), \
540 KEY_("C-b b PgUp", "Scroll the view up one page"), \
541 KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
542 KEY_("C-u u", "Scroll the view up one half page"), \
543 KEY_("C-d d", "Scroll the view down one half page"), \
544 KEY_("g", "Go to line N (default: first line)"), \
545 KEY_("Home =", "Go to the first line"), \
546 KEY_("G", "Go to line N (default: last line)"), \
547 KEY_("End *", "Go to the last line"), \
548 KEY_("l Right", "Scroll the view right"), \
549 KEY_("h Left", "Scroll the view left"), \
550 KEY_("$", "Scroll view to the rightmost position"), \
551 KEY_("0", "Scroll view to the leftmost position"), \
552 KEY_("-", "Decrease size of the focussed split"), \
553 KEY_("+", "Increase size of the focussed split"), \
554 KEY_("Tab", "Switch focus between views"), \
555 KEY_("F", "Toggle fullscreen mode"), \
556 KEY_("S", "Switch split-screen layout"), \
557 KEY_("/", "Open prompt to enter search term"), \
558 KEY_("n", "Find next line/token matching the current search term"), \
559 KEY_("N", "Find previous line/token matching the current search term"),\
560 KEY_("q", "Quit the focussed view; Quit help screen"), \
561 KEY_("Q", "Quit tog"), \
563 KEYMAP_("Log view", TOG_KEYMAP_LOG), \
564 KEY_("< ,", "Move cursor up one commit"), \
565 KEY_("> .", "Move cursor down one commit"), \
566 KEY_("Enter", "Open diff view of the selected commit"), \
567 KEY_("B", "Reload the log view and toggle display of merged commits"), \
568 KEY_("R", "Open ref view of all repository references"), \
569 KEY_("T", "Display tree view of the repository from the selected" \
571 KEY_("@", "Toggle between displaying author and committer name"), \
572 KEY_("&", "Open prompt to enter term to limit commits displayed"), \
573 KEY_("C-g Backspace", "Cancel current search or log operation"), \
574 KEY_("C-l", "Reload the log view with new commits in the repository"), \
576 KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
577 KEY_("K < ,", "Display diff of next line in the file/log entry"), \
578 KEY_("J > .", "Display diff of previous line in the file/log entry"), \
579 KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
580 KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
582 KEY_("(", "Go to the previous file in the diff"), \
583 KEY_(")", "Go to the next file in the diff"), \
584 KEY_("{", "Go to the previous hunk in the diff"), \
585 KEY_("}", "Go to the next hunk in the diff"), \
586 KEY_("[", "Decrease the number of context lines"), \
587 KEY_("]", "Increase the number of context lines"), \
588 KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
590 KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
591 KEY_("Enter", "Display diff view of the selected line's commit"), \
592 KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
593 KEY_("L", "Open log view for the currently selected annotated line"), \
594 KEY_("C", "Reload view with the previously blamed commit"), \
595 KEY_("c", "Reload view with the version of the file found in the" \
596 " selected line's commit"), \
597 KEY_("p", "Reload view with the version of the file found in the" \
598 " selected line's parent commit"), \
600 KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
601 KEY_("Enter", "Enter selected directory or open blame view of the" \
603 KEY_("L", "Open log view for the selected entry"), \
604 KEY_("R", "Open ref view of all repository references"), \
605 KEY_("i", "Show object IDs for all tree entries"), \
606 KEY_("Backspace", "Return to the parent directory"), \
608 KEYMAP_("Ref view", TOG_KEYMAP_REF), \
609 KEY_("Enter", "Display log view of the selected reference"), \
610 KEY_("T", "Display tree view of the selected reference"), \
611 KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
612 KEY_("m", "Toggle display of last modified date for each reference"), \
613 KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
614 KEY_("C-l", "Reload view with all repository references")
619 enum tog_keymap_type type;
622 /* curses io for tog regress */
630 static int using_mock_io;
632 #define TOG_KEY_SCRDUMP SHRT_MIN
635 * We implement two types of views: parent views and child views.
637 * The 'Tab' key switches focus between a parent view and its child view.
638 * Child views are shown side-by-side to their parent view, provided
639 * there is enough screen estate.
641 * When a new view is opened from within a parent view, this new view
642 * becomes a child view of the parent view, replacing any existing child.
644 * When a new view is opened from within a child view, this new view
645 * becomes a parent view which will obscure the views below until the
646 * user quits the new parent view by typing 'q'.
648 * This list of views contains parent views only.
649 * Child views are only pointed to by their parent view.
651 TAILQ_HEAD(tog_view_list_head, tog_view);
654 TAILQ_ENTRY(tog_view) entry;
657 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
658 int resized_y, resized_x; /* begin_y/x based on user resizing */
659 int maxx, x; /* max column and current start column */
660 int lines, cols; /* copies of LINES and COLS */
661 int nscrolled, offset; /* lines scrolled and hsplit line offset */
662 int gline, hiline; /* navigate to and highlight this nG line */
663 int ch, count; /* current keymap and count prefix */
664 int resized; /* set when in a resize event */
665 int focussed; /* Only set on one parent or child view at a time. */
667 struct tog_view *parent;
668 struct tog_view *child;
671 * This flag is initially set on parent views when a new child view
672 * is created. It gets toggled when the 'Tab' key switches focus
673 * between parent and child.
674 * The flag indicates whether focus should be passed on to our child
675 * view if this parent view gets picked for focus after another parent
676 * view was closed. This prevents child views from losing focus in such
681 enum tog_view_mode mode;
682 /* type-specific state */
683 enum tog_view_type type;
685 struct tog_diff_view_state diff;
686 struct tog_log_view_state log;
687 struct tog_blame_view_state blame;
688 struct tog_tree_view_state tree;
689 struct tog_ref_view_state ref;
690 struct tog_help_view_state help;
693 const struct got_error *(*show)(struct tog_view *);
694 const struct got_error *(*input)(struct tog_view **,
695 struct tog_view *, int);
696 const struct got_error *(*reset)(struct tog_view *);
697 const struct got_error *(*resize)(struct tog_view *, int);
698 const struct got_error *(*close)(struct tog_view *);
700 const struct got_error *(*search_start)(struct tog_view *);
701 const struct got_error *(*search_next)(struct tog_view *);
702 void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
703 int **, int **, int **, int **);
706 #define TOG_SEARCH_FORWARD 1
707 #define TOG_SEARCH_BACKWARD 2
708 int search_next_done;
709 #define TOG_SEARCH_HAVE_MORE 1
710 #define TOG_SEARCH_NO_MORE 2
711 #define TOG_SEARCH_HAVE_NONE 3
717 static const struct got_error *open_diff_view(struct tog_view *,
718 struct got_object_id *, struct got_object_id *,
719 const char *, const char *, int, int, int, struct tog_view *,
720 struct got_repository *);
721 static const struct got_error *show_diff_view(struct tog_view *);
722 static const struct got_error *input_diff_view(struct tog_view **,
723 struct tog_view *, int);
724 static const struct got_error *reset_diff_view(struct tog_view *);
725 static const struct got_error* close_diff_view(struct tog_view *);
726 static const struct got_error *search_start_diff_view(struct tog_view *);
727 static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
728 size_t *, int **, int **, int **, int **);
729 static const struct got_error *search_next_view_match(struct tog_view *);
731 static const struct got_error *open_log_view(struct tog_view *,
732 struct got_object_id *, struct got_repository *,
733 const char *, const char *, int);
734 static const struct got_error * show_log_view(struct tog_view *);
735 static const struct got_error *input_log_view(struct tog_view **,
736 struct tog_view *, int);
737 static const struct got_error *resize_log_view(struct tog_view *, int);
738 static const struct got_error *close_log_view(struct tog_view *);
739 static const struct got_error *search_start_log_view(struct tog_view *);
740 static const struct got_error *search_next_log_view(struct tog_view *);
742 static const struct got_error *open_blame_view(struct tog_view *, char *,
743 struct got_object_id *, struct got_repository *);
744 static const struct got_error *show_blame_view(struct tog_view *);
745 static const struct got_error *input_blame_view(struct tog_view **,
746 struct tog_view *, int);
747 static const struct got_error *reset_blame_view(struct tog_view *);
748 static const struct got_error *close_blame_view(struct tog_view *);
749 static const struct got_error *search_start_blame_view(struct tog_view *);
750 static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
751 size_t *, int **, int **, int **, int **);
753 static const struct got_error *open_tree_view(struct tog_view *,
754 struct got_object_id *, const char *, struct got_repository *);
755 static const struct got_error *show_tree_view(struct tog_view *);
756 static const struct got_error *input_tree_view(struct tog_view **,
757 struct tog_view *, int);
758 static const struct got_error *close_tree_view(struct tog_view *);
759 static const struct got_error *search_start_tree_view(struct tog_view *);
760 static const struct got_error *search_next_tree_view(struct tog_view *);
762 static const struct got_error *open_ref_view(struct tog_view *,
763 struct got_repository *);
764 static const struct got_error *show_ref_view(struct tog_view *);
765 static const struct got_error *input_ref_view(struct tog_view **,
766 struct tog_view *, int);
767 static const struct got_error *close_ref_view(struct tog_view *);
768 static const struct got_error *search_start_ref_view(struct tog_view *);
769 static const struct got_error *search_next_ref_view(struct tog_view *);
771 static const struct got_error *open_help_view(struct tog_view *,
773 static const struct got_error *show_help_view(struct tog_view *);
774 static const struct got_error *input_help_view(struct tog_view **,
775 struct tog_view *, int);
776 static const struct got_error *reset_help_view(struct tog_view *);
777 static const struct got_error* close_help_view(struct tog_view *);
778 static const struct got_error *search_start_help_view(struct tog_view *);
779 static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
780 size_t *, int **, int **, int **, int **);
782 static volatile sig_atomic_t tog_sigwinch_received;
783 static volatile sig_atomic_t tog_sigpipe_received;
784 static volatile sig_atomic_t tog_sigcont_received;
785 static volatile sig_atomic_t tog_sigint_received;
786 static volatile sig_atomic_t tog_sigterm_received;
789 tog_sigwinch(int signo)
791 tog_sigwinch_received = 1;
795 tog_sigpipe(int signo)
797 tog_sigpipe_received = 1;
801 tog_sigcont(int signo)
803 tog_sigcont_received = 1;
807 tog_sigint(int signo)
809 tog_sigint_received = 1;
813 tog_sigterm(int signo)
815 tog_sigterm_received = 1;
819 tog_fatal_signal_received(void)
821 return (tog_sigpipe_received ||
822 tog_sigint_received || tog_sigterm_received);
825 static const struct got_error *
826 view_close(struct tog_view *view)
828 const struct got_error *err = NULL, *child_err = NULL;
831 child_err = view_close(view->child);
835 err = view->close(view);
837 del_panel(view->panel);
839 delwin(view->window);
841 return err ? err : child_err;
844 static struct tog_view *
845 view_open(int nlines, int ncols, int begin_y, int begin_x,
846 enum tog_view_type type)
848 struct tog_view *view = calloc(1, sizeof(*view));
856 view->nlines = nlines ? nlines : LINES - begin_y;
857 view->ncols = ncols ? ncols : COLS - begin_x;
858 view->begin_y = begin_y;
859 view->begin_x = begin_x;
860 view->window = newwin(nlines, ncols, begin_y, begin_x);
861 if (view->window == NULL) {
865 view->panel = new_panel(view->window);
866 if (view->panel == NULL ||
867 set_panel_userptr(view->panel, view) != OK) {
872 keypad(view->window, TRUE);
877 view_split_begin_x(int begin_x)
879 if (begin_x > 0 || COLS < 120)
881 return (COLS - MAX(COLS / 2, 80));
884 /* XXX Stub till we decide what to do. */
886 view_split_begin_y(int lines)
888 return lines * HSPLIT_SCALE;
891 static const struct got_error *view_resize(struct tog_view *);
893 static const struct got_error *
894 view_splitscreen(struct tog_view *view)
896 const struct got_error *err = NULL;
898 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
899 if (view->resized_y && view->resized_y < view->lines)
900 view->begin_y = view->resized_y;
902 view->begin_y = view_split_begin_y(view->nlines);
904 } else if (!view->resized) {
905 if (view->resized_x && view->resized_x < view->cols - 1 &&
907 view->begin_x = view->resized_x;
909 view->begin_x = view_split_begin_x(0);
912 view->nlines = LINES - view->begin_y;
913 view->ncols = COLS - view->begin_x;
916 err = view_resize(view);
920 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
921 view->parent->nlines = view->begin_y;
923 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
924 return got_error_from_errno("mvwin");
929 static const struct got_error *
930 view_fullscreen(struct tog_view *view)
932 const struct got_error *err = NULL;
935 view->begin_y = view->resized ? view->begin_y : 0;
936 view->nlines = view->resized ? view->nlines : LINES;
940 err = view_resize(view);
944 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
945 return got_error_from_errno("mvwin");
951 view_is_parent_view(struct tog_view *view)
953 return view->parent == NULL;
957 view_is_splitscreen(struct tog_view *view)
959 return view->begin_x > 0 || view->begin_y > 0;
963 view_is_fullscreen(struct tog_view *view)
965 return view->nlines == LINES && view->ncols == COLS;
969 view_is_hsplit_top(struct tog_view *view)
971 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
972 view_is_splitscreen(view->child);
976 view_border(struct tog_view *view)
979 const struct tog_view *view_above;
982 return view_border(view->parent);
984 panel = panel_above(view->panel);
988 view_above = panel_userptr(panel);
989 if (view->mode == TOG_VIEW_SPLIT_HRZN)
990 mvwhline(view->window, view_above->begin_y - 1,
991 view->begin_x, ACS_HLINE, view->ncols);
993 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
994 ACS_VLINE, view->nlines);
997 static const struct got_error *view_init_hsplit(struct tog_view *, int);
998 static const struct got_error *request_log_commits(struct tog_view *);
999 static const struct got_error *offset_selection_down(struct tog_view *);
1000 static void offset_selection_up(struct tog_view *);
1001 static void view_get_split(struct tog_view *, int *, int *);
1003 static const struct got_error *
1004 view_resize(struct tog_view *view)
1006 const struct got_error *err = NULL;
1007 int dif, nlines, ncols;
1009 dif = LINES - view->lines; /* line difference */
1011 if (view->lines > LINES)
1012 nlines = view->nlines - (view->lines - LINES);
1014 nlines = view->nlines + (LINES - view->lines);
1015 if (view->cols > COLS)
1016 ncols = view->ncols - (view->cols - COLS);
1018 ncols = view->ncols + (COLS - view->cols);
1021 int hs = view->child->begin_y;
1023 if (!view_is_fullscreen(view))
1024 view->child->begin_x = view_split_begin_x(view->begin_x);
1025 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1026 view->child->begin_x == 0) {
1029 view_fullscreen(view->child);
1030 if (view->child->focussed)
1031 show_panel(view->child->panel);
1033 show_panel(view->panel);
1035 ncols = view->child->begin_x;
1037 view_splitscreen(view->child);
1038 show_panel(view->child->panel);
1041 * XXX This is ugly and needs to be moved into the above
1042 * logic but "works" for now and my attempts at moving it
1043 * break either 'tab' or 'F' key maps in horizontal splits.
1046 err = view_splitscreen(view->child);
1049 if (dif < 0) { /* top split decreased */
1050 err = offset_selection_down(view);
1057 show_panel(view->child->panel);
1058 nlines = view->nlines;
1060 } else if (view->parent == NULL)
1063 if (view->resize && dif > 0) {
1064 err = view->resize(view, dif);
1069 if (wresize(view->window, nlines, ncols) == ERR)
1070 return got_error_from_errno("wresize");
1071 if (replace_panel(view->panel, view->window) == ERR)
1072 return got_error_from_errno("replace_panel");
1073 wclear(view->window);
1075 view->nlines = nlines;
1076 view->ncols = ncols;
1077 view->lines = LINES;
1083 static const struct got_error *
1084 resize_log_view(struct tog_view *view, int increase)
1086 struct tog_log_view_state *s = &view->state.log;
1087 const struct got_error *err = NULL;
1090 if (s->selected_entry)
1091 n = s->selected_entry->idx + view->lines - s->selected;
1094 * Request commits to account for the increased
1095 * height so we have enough to populate the view.
1097 if (s->commits->ncommits < n) {
1098 view->nscrolled = n - s->commits->ncommits + increase + 1;
1099 err = request_log_commits(view);
1106 view_adjust_offset(struct tog_view *view, int n)
1111 if (view->parent && view->parent->offset) {
1112 if (view->parent->offset + n >= 0)
1113 view->parent->offset += n;
1115 view->parent->offset = 0;
1116 } else if (view->offset) {
1117 if (view->offset - n >= 0)
1124 static const struct got_error *
1125 view_resize_split(struct tog_view *view, int resize)
1127 const struct got_error *err = NULL;
1128 struct tog_view *v = NULL;
1135 if (!v->child || !view_is_splitscreen(v->child))
1138 v->resized = v->child->resized = resize; /* lock for resize event */
1140 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1141 if (v->child->resized_y)
1142 v->child->begin_y = v->child->resized_y;
1144 v->child->begin_y -= resize;
1146 v->child->begin_y += resize;
1147 if (v->child->begin_y < 3) {
1149 v->child->begin_y = 3;
1150 } else if (v->child->begin_y > LINES - 1) {
1152 v->child->begin_y = LINES - 1;
1155 v->child->ncols = COLS;
1156 view_adjust_offset(view, resize);
1157 err = view_init_hsplit(v, v->child->begin_y);
1160 v->child->resized_y = v->child->begin_y;
1162 if (v->child->resized_x)
1163 v->child->begin_x = v->child->resized_x;
1165 v->child->begin_x -= resize;
1167 v->child->begin_x += resize;
1168 if (v->child->begin_x < 11) {
1170 v->child->begin_x = 11;
1171 } else if (v->child->begin_x > COLS - 1) {
1173 v->child->begin_x = COLS - 1;
1175 v->child->resized_x = v->child->begin_x;
1178 v->child->mode = v->mode;
1179 v->child->nlines = v->lines - v->child->begin_y;
1180 v->child->ncols = v->cols - v->child->begin_x;
1183 err = view_fullscreen(v);
1186 err = view_splitscreen(v->child);
1190 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1191 err = offset_selection_down(v->child);
1197 err = v->resize(v, 0);
1198 else if (v->child->resize)
1199 err = v->child->resize(v->child, 0);
1201 v->resized = v->child->resized = 0;
1207 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1209 struct tog_view *v = src->child ? src->child : src;
1211 dst->resized_x = v->resized_x;
1212 dst->resized_y = v->resized_y;
1215 static const struct got_error *
1216 view_close_child(struct tog_view *view)
1218 const struct got_error *err = NULL;
1220 if (view->child == NULL)
1223 err = view_close(view->child);
1228 static const struct got_error *
1229 view_set_child(struct tog_view *view, struct tog_view *child)
1231 const struct got_error *err = NULL;
1233 view->child = child;
1234 child->parent = view;
1236 err = view_resize(view);
1240 if (view->child->resized_x || view->child->resized_y)
1241 err = view_resize_split(view, 0);
1246 static const struct got_error *view_dispatch_request(struct tog_view **,
1247 struct tog_view *, enum tog_view_type, int, int);
1249 static const struct got_error *
1250 view_request_new(struct tog_view **requested, struct tog_view *view,
1251 enum tog_view_type request)
1253 struct tog_view *new_view = NULL;
1254 const struct got_error *err;
1259 if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1260 view_get_split(view, &y, &x);
1262 err = view_dispatch_request(&new_view, view, request, y, x);
1266 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1267 request != TOG_VIEW_HELP) {
1268 err = view_init_hsplit(view, y);
1274 new_view->focussed = 1;
1275 new_view->mode = view->mode;
1276 new_view->nlines = request == TOG_VIEW_HELP ?
1277 view->lines : view->lines - y;
1279 if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1280 view_transfer_size(new_view, view);
1281 err = view_close_child(view);
1284 err = view_set_child(view, new_view);
1287 view->focus_child = 1;
1289 *requested = new_view;
1295 tog_resizeterm(void)
1298 struct winsize size;
1300 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1301 cols = 80; /* Default */
1305 lines = size.ws_row;
1307 resize_term(lines, cols);
1310 static const struct got_error *
1311 view_search_start(struct tog_view *view, int fast_refresh)
1313 const struct got_error *err = NULL;
1314 struct tog_view *v = view;
1318 if (view->search_started) {
1319 regfree(&view->regex);
1320 view->searching = 0;
1321 memset(&view->regmatch, 0, sizeof(view->regmatch));
1323 view->search_started = 0;
1325 if (view->nlines < 1)
1328 if (view_is_hsplit_top(view))
1330 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1333 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1334 wclrtoeol(v->window);
1336 nodelay(v->window, FALSE); /* block for search term input */
1339 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1340 wrefresh(v->window);
1343 nodelay(v->window, TRUE);
1344 if (!fast_refresh && !using_mock_io)
1349 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1350 err = view->search_start(view);
1352 regfree(&view->regex);
1355 view->search_started = 1;
1356 view->searching = TOG_SEARCH_FORWARD;
1357 view->search_next_done = 0;
1358 view->search_next(view);
1364 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1365 static const struct got_error *
1366 switch_split(struct tog_view *view)
1368 const struct got_error *err = NULL;
1369 struct tog_view *v = NULL;
1376 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1377 v->mode = TOG_VIEW_SPLIT_VERT;
1379 v->mode = TOG_VIEW_SPLIT_HRZN;
1383 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1384 v->mode = TOG_VIEW_SPLIT_NONE;
1386 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1387 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1388 v->child->begin_y = v->child->resized_y;
1389 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1390 v->child->begin_x = v->child->resized_x;
1393 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1395 v->child->ncols = COLS;
1396 v->child->nscrolled = LINES - v->child->nlines;
1398 err = view_init_hsplit(v, v->child->begin_y);
1402 v->child->mode = v->mode;
1403 v->child->nlines = v->lines - v->child->begin_y;
1406 err = view_fullscreen(v);
1409 err = view_splitscreen(v->child);
1413 if (v->mode == TOG_VIEW_SPLIT_NONE)
1414 v->mode = TOG_VIEW_SPLIT_VERT;
1415 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1416 err = offset_selection_down(v);
1419 err = offset_selection_down(v->child);
1423 offset_selection_up(v);
1424 offset_selection_up(v->child);
1427 err = v->resize(v, 0);
1428 else if (v->child->resize)
1429 err = v->child->resize(v->child, 0);
1435 * Strip trailing whitespace from str starting at byte *n;
1436 * if *n < 0, use strlen(str). Return new str length in *n.
1439 strip_trailing_ws(char *str, int *n)
1443 if (str == NULL || *str == '\0')
1449 while (x-- > 0 && isspace((unsigned char)str[x]))
1456 * Extract visible substring of line y from the curses screen
1457 * and strip trailing whitespace. If vline is set, overwrite
1458 * line[vline] with '|' because the ACS_VLINE character is
1459 * written out as 'x'. Write the line to file f.
1461 static const struct got_error *
1462 view_write_line(FILE *f, int y, int vline)
1464 char line[COLS * MB_LEN_MAX]; /* allow for multibyte chars */
1467 r = mvwinnstr(curscr, y, 0, line, sizeof(line));
1469 return got_error_fmt(GOT_ERR_RANGE,
1470 "failed to extract line %d", y);
1473 * In some views, lines are padded with blanks to COLS width.
1474 * Strip them so we can diff without the -b flag when testing.
1476 strip_trailing_ws(line, &r);
1481 w = fprintf(f, "%s\n", line);
1482 if (w != r + 1) /* \n */
1483 return got_ferror(f, GOT_ERR_IO);
1489 * Capture the visible curses screen by writing each line to the
1490 * file at the path set via the TOG_SCR_DUMP environment variable.
1492 static const struct got_error *
1493 screendump(struct tog_view *view)
1495 const struct got_error *err;
1498 err = got_opentemp_truncate(tog_io.sdump);
1502 if ((view->child && view->child->begin_x) ||
1503 (view->parent && view->begin_x)) {
1504 int ncols = view->child ? view->ncols : view->parent->ncols;
1506 /* vertical splitscreen */
1507 for (i = 0; i < view->nlines; ++i) {
1508 err = view_write_line(tog_io.sdump, i, ncols - 1);
1515 /* fullscreen or horizontal splitscreen */
1516 if ((view->child && view->child->begin_y) ||
1517 (view->parent && view->begin_y)) /* hsplit */
1518 hline = view->child ?
1519 view->child->begin_y : view->begin_y;
1521 for (i = 0; i < view->lines; i++) {
1522 if (hline && i == hline - 1) {
1525 /* ACS_HLINE writes out as 'q', overwrite it */
1526 for (c = 0; c < view->cols; ++c)
1527 fputc('-', tog_io.sdump);
1528 fputc('\n', tog_io.sdump);
1532 err = view_write_line(tog_io.sdump, i, 0);
1543 * Compute view->count from numeric input. Assign total to view->count and
1544 * return first non-numeric key entered.
1547 get_compound_key(struct tog_view *view, int c)
1549 struct tog_view *v = view;
1552 if (view_is_hsplit_top(view))
1554 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1558 cbreak(); /* block for input */
1559 nodelay(view->window, FALSE);
1560 wmove(v->window, v->nlines - 1, 0);
1561 wclrtoeol(v->window);
1562 waddch(v->window, ':');
1565 x = getcurx(v->window);
1566 if (x != ERR && x < view->ncols) {
1567 waddch(v->window, c);
1568 wrefresh(v->window);
1572 * Don't overflow. Max valid request should be the greatest
1573 * between the longest and total lines; cap at 10 million.
1578 n = n * 10 + (c - '0');
1579 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1581 if (c == 'G' || c == 'g') { /* nG key map */
1582 view->gline = view->hiline = n;
1587 /* Massage excessive or inapplicable values at the input handler. */
1594 action_report(struct tog_view *view)
1596 struct tog_view *v = view;
1598 if (view_is_hsplit_top(view))
1600 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1603 wmove(v->window, v->nlines - 1, 0);
1604 wclrtoeol(v->window);
1605 wprintw(v->window, ":%s", view->action);
1606 wrefresh(v->window);
1609 * Clear action status report. Only clear in blame view
1610 * once annotating is complete, otherwise it's too fast.
1612 if (view->type == TOG_VIEW_BLAME) {
1613 if (view->state.blame.blame_complete)
1614 view->action = NULL;
1616 view->action = NULL;
1620 * Read the next line from the test script and assign
1621 * key instruction to *ch. If at EOF, set the *done flag.
1623 static const struct got_error *
1624 tog_read_script_key(FILE *script, struct tog_view *view, int *ch, int *done)
1626 const struct got_error *err = NULL;
1630 if (view->count && --view->count) {
1636 if (getline(&line, &linesz, script) == -1) {
1641 err = got_ferror(script, GOT_ERR_IO);
1646 if (strncasecmp(line, "WAIT_FOR_UI", 11) == 0)
1647 tog_io.wait_for_ui = 1;
1648 else if (strncasecmp(line, "KEY_ENTER", 9) == 0)
1650 else if (strncasecmp(line, "KEY_RIGHT", 9) == 0)
1652 else if (strncasecmp(line, "KEY_LEFT", 8) == 0)
1654 else if (strncasecmp(line, "KEY_DOWN", 8) == 0)
1656 else if (strncasecmp(line, "KEY_UP", 6) == 0)
1658 else if (strncasecmp(line, "TAB", 3) == 0)
1660 else if (strncasecmp(line, "SCREENDUMP", 10) == 0)
1661 *ch = TOG_KEY_SCRDUMP;
1662 else if (isdigit((unsigned char)*line)) {
1665 while (isdigit((unsigned char)*t))
1667 view->ch = *ch = *t;
1669 /* ignore error, view->count is 0 if instruction is invalid */
1670 view->count = strtonum(line, 0, INT_MAX, NULL);
1679 static const struct got_error *
1680 view_input(struct tog_view **new, int *done, struct tog_view *view,
1681 struct tog_view_list_head *views, int fast_refresh)
1683 const struct got_error *err = NULL;
1690 action_report(view);
1692 /* Clear "no matches" indicator. */
1693 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1694 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1695 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1699 if (view->searching && !view->search_next_done) {
1700 errcode = pthread_mutex_unlock(&tog_mutex);
1702 return got_error_set_errno(errcode,
1703 "pthread_mutex_unlock");
1705 errcode = pthread_mutex_lock(&tog_mutex);
1707 return got_error_set_errno(errcode,
1708 "pthread_mutex_lock");
1709 view->search_next(view);
1713 /* Allow threads to make progress while we are waiting for input. */
1714 errcode = pthread_mutex_unlock(&tog_mutex);
1716 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1718 if (using_mock_io) {
1719 err = tog_read_script_key(tog_io.f, view, &ch, done);
1721 errcode = pthread_mutex_lock(&tog_mutex);
1724 } else if (view->count && --view->count) {
1726 nodelay(view->window, TRUE);
1727 ch = wgetch(view->window);
1728 /* let C-g or backspace abort unfinished count */
1729 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1734 ch = wgetch(view->window);
1735 if (ch >= '1' && ch <= '9')
1736 view->ch = ch = get_compound_key(view, ch);
1738 if (view->hiline && ch != ERR && ch != 0)
1739 view->hiline = 0; /* key pressed, clear line highlight */
1740 nodelay(view->window, TRUE);
1741 errcode = pthread_mutex_lock(&tog_mutex);
1743 return got_error_set_errno(errcode, "pthread_mutex_lock");
1745 if (tog_sigwinch_received || tog_sigcont_received) {
1747 tog_sigwinch_received = 0;
1748 tog_sigcont_received = 0;
1749 TAILQ_FOREACH(v, views, entry) {
1750 err = view_resize(v);
1753 err = v->input(new, v, KEY_RESIZE);
1757 err = view_resize(v->child);
1760 err = v->child->input(new, v->child,
1764 if (v->child->resized_x || v->child->resized_y) {
1765 err = view_resize_split(v, 0);
1777 if (view->type == TOG_VIEW_HELP)
1778 err = view->reset(view);
1780 err = view_request_new(new, view, TOG_VIEW_HELP);
1786 view->child->focussed = 1;
1787 view->focus_child = 1;
1788 } else if (view->parent) {
1790 view->parent->focussed = 1;
1791 view->parent->focus_child = 0;
1792 if (!view_is_splitscreen(view)) {
1793 if (view->parent->resize) {
1794 err = view->parent->resize(view->parent,
1799 offset_selection_up(view->parent);
1800 err = view_fullscreen(view->parent);
1807 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1808 if (view->parent->resize) {
1809 /* might need more commits to fill fullscreen */
1810 err = view->parent->resize(view->parent, 0);
1814 offset_selection_up(view->parent);
1816 err = view->input(new, view, ch);
1824 if (view_is_parent_view(view)) {
1825 if (view->child == NULL)
1827 if (view_is_splitscreen(view->child)) {
1829 view->child->focussed = 1;
1830 err = view_fullscreen(view->child);
1832 err = view_splitscreen(view->child);
1834 err = view_resize_split(view, 0);
1838 err = view->child->input(new, view->child,
1841 if (view_is_splitscreen(view)) {
1842 view->parent->focussed = 0;
1844 err = view_fullscreen(view);
1846 err = view_splitscreen(view);
1847 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1848 err = view_resize(view->parent);
1850 err = view_resize_split(view, 0);
1854 err = view->input(new, view, KEY_RESIZE);
1859 err = view->resize(view, 0);
1864 if (view->parent->resize) {
1865 err = view->parent->resize(view->parent, 0);
1869 err = offset_selection_down(view->parent);
1873 err = offset_selection_down(view);
1877 err = switch_split(view);
1880 err = view_resize_split(view, -1);
1883 err = view_resize_split(view, 1);
1889 if (view->search_start)
1890 view_search_start(view, fast_refresh);
1892 err = view->input(new, view, ch);
1896 if (view->search_started && view->search_next) {
1897 view->searching = (ch == 'n' ?
1898 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1899 view->search_next_done = 0;
1900 view->search_next(view);
1902 err = view->input(new, view, ch);
1905 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1906 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1907 view->action = "Patience diff algorithm";
1909 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1910 view->action = "Myers diff algorithm";
1912 TAILQ_FOREACH(v, views, entry) {
1918 if (v->child && v->child->reset) {
1919 err = v->child->reset(v->child);
1925 case TOG_KEY_SCRDUMP:
1926 err = screendump(view);
1929 err = view->input(new, view, ch);
1937 view_needs_focus_indication(struct tog_view *view)
1939 if (view_is_parent_view(view)) {
1940 if (view->child == NULL || view->child->focussed)
1942 if (!view_is_splitscreen(view->child))
1944 } else if (!view_is_splitscreen(view))
1947 return view->focussed;
1950 static const struct got_error *
1953 const struct got_error *err = NULL;
1955 if (tog_io.cin && fclose(tog_io.cin) == EOF)
1956 err = got_ferror(tog_io.cin, GOT_ERR_IO);
1957 if (tog_io.cout && fclose(tog_io.cout) == EOF && err == NULL)
1958 err = got_ferror(tog_io.cout, GOT_ERR_IO);
1959 if (tog_io.f && fclose(tog_io.f) == EOF && err == NULL)
1960 err = got_ferror(tog_io.f, GOT_ERR_IO);
1961 if (tog_io.sdump && fclose(tog_io.sdump) == EOF && err == NULL)
1962 err = got_ferror(tog_io.sdump, GOT_ERR_IO);
1967 static const struct got_error *
1968 view_loop(struct tog_view *view)
1970 const struct got_error *err = NULL;
1971 struct tog_view_list_head views;
1972 struct tog_view *new_view;
1974 int fast_refresh = 10;
1975 int done = 0, errcode;
1977 mode = getenv("TOG_VIEW_SPLIT_MODE");
1978 if (!mode || !(*mode == 'h' || *mode == 'H'))
1979 view->mode = TOG_VIEW_SPLIT_VERT;
1981 view->mode = TOG_VIEW_SPLIT_HRZN;
1983 errcode = pthread_mutex_lock(&tog_mutex);
1985 return got_error_set_errno(errcode, "pthread_mutex_lock");
1988 TAILQ_INSERT_HEAD(&views, view, entry);
1991 err = view->show(view);
1996 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1997 !tog_fatal_signal_received()) {
1998 /* Refresh fast during initialization, then become slower. */
1999 if (fast_refresh && --fast_refresh == 0 && !using_mock_io)
2000 halfdelay(10); /* switch to once per second */
2002 err = view_input(&new_view, &done, view, &views, fast_refresh);
2006 if (view->dying && view == TAILQ_FIRST(&views) &&
2007 TAILQ_NEXT(view, entry) == NULL)
2013 * When we quit, scroll the screen up a single line
2014 * so we don't lose any information.
2016 TAILQ_FOREACH(v, &views, entry) {
2017 wmove(v->window, 0, 0);
2018 wdeleteln(v->window);
2019 wnoutrefresh(v->window);
2020 if (v->child && !view_is_fullscreen(v)) {
2021 wmove(v->child->window, 0, 0);
2022 wdeleteln(v->child->window);
2023 wnoutrefresh(v->child->window);
2030 struct tog_view *v, *prev = NULL;
2032 if (view_is_parent_view(view))
2033 prev = TAILQ_PREV(view, tog_view_list_head,
2035 else if (view->parent)
2036 prev = view->parent;
2039 view->parent->child = NULL;
2040 view->parent->focus_child = 0;
2041 /* Restore fullscreen line height. */
2042 view->parent->nlines = view->parent->lines;
2043 err = view_resize(view->parent);
2046 /* Make resized splits persist. */
2047 view_transfer_size(view->parent, view);
2049 TAILQ_REMOVE(&views, view, entry);
2051 err = view_close(view);
2056 TAILQ_FOREACH(v, &views, entry) {
2060 if (view == NULL && new_view == NULL) {
2061 /* No view has focus. Try to pick one. */
2064 else if (!TAILQ_EMPTY(&views)) {
2065 view = TAILQ_LAST(&views,
2066 tog_view_list_head);
2069 if (view->focus_child) {
2070 view->child->focussed = 1;
2078 struct tog_view *v, *t;
2079 /* Only allow one parent view per type. */
2080 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
2081 if (v->type != new_view->type)
2083 TAILQ_REMOVE(&views, v, entry);
2084 err = view_close(v);
2089 TAILQ_INSERT_TAIL(&views, new_view, entry);
2092 if (view && !done) {
2093 if (view_is_parent_view(view)) {
2094 if (view->child && view->child->focussed)
2097 if (view->parent && view->parent->focussed)
2098 view = view->parent;
2100 show_panel(view->panel);
2101 if (view->child && view_is_splitscreen(view->child))
2102 show_panel(view->child->panel);
2103 if (view->parent && view_is_splitscreen(view)) {
2104 err = view->parent->show(view->parent);
2108 err = view->show(view);
2112 err = view->child->show(view->child);
2121 while (!TAILQ_EMPTY(&views)) {
2122 const struct got_error *close_err;
2123 view = TAILQ_FIRST(&views);
2124 TAILQ_REMOVE(&views, view, entry);
2125 close_err = view_close(view);
2126 if (close_err && err == NULL)
2130 errcode = pthread_mutex_unlock(&tog_mutex);
2131 if (errcode && err == NULL)
2132 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2142 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
2147 /* Create newly allocated wide-character string equivalent to a byte string. */
2148 static const struct got_error *
2149 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
2152 const struct got_error *err = NULL;
2155 *wlen = mbstowcs(NULL, s, 0);
2156 if (*wlen == (size_t)-1) {
2158 if (errno != EILSEQ)
2159 return got_error_from_errno("mbstowcs");
2161 /* byte string invalid in current encoding; try to "fix" it */
2162 err = got_mbsavis(&vis, &vislen, s);
2165 *wlen = mbstowcs(NULL, vis, 0);
2166 if (*wlen == (size_t)-1) {
2167 err = got_error_from_errno("mbstowcs"); /* give up */
2172 *ws = calloc(*wlen + 1, sizeof(**ws));
2174 err = got_error_from_errno("calloc");
2178 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
2179 err = got_error_from_errno("mbstowcs");
2190 static const struct got_error *
2191 expand_tab(char **ptr, const char *src)
2194 size_t len, n, idx = 0, sz = 0;
2197 n = len = strlen(src);
2198 dst = malloc(n + 1);
2200 return got_error_from_errno("malloc");
2202 while (idx < len && src[idx]) {
2203 const char c = src[idx];
2206 size_t nb = TABSIZE - sz % TABSIZE;
2209 p = realloc(dst, n + nb);
2212 return got_error_from_errno("realloc");
2217 memset(dst + sz, ' ', nb);
2220 dst[sz++] = src[idx];
2230 * Advance at most n columns from wline starting at offset off.
2231 * Return the index to the first character after the span operation.
2232 * Return the combined column width of all spanned wide characters in
2236 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2238 int width, i, cols = 0;
2245 for (i = off; wline[i] != L'\0'; ++i) {
2246 if (wline[i] == L'\t')
2247 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2249 width = wcwidth(wline[i]);
2256 if (cols + width > n)
2266 * Format a line for display, ensuring that it won't overflow a width limit.
2267 * With scrolling, the width returned refers to the scrolled version of the
2268 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2270 static const struct got_error *
2271 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2272 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2274 const struct got_error *err = NULL;
2276 wchar_t *wline = NULL;
2285 err = expand_tab(&exstr, line);
2290 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2295 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2297 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2298 wline[wlen - 1] = L'\0';
2301 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2302 wline[wlen - 1] = L'\0';
2306 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2312 *scrollxp = scrollx;
2320 static const struct got_error*
2321 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2322 struct got_object_id *id, struct got_repository *repo)
2324 static const struct got_error *err = NULL;
2325 struct got_reflist_entry *re;
2334 TAILQ_FOREACH(re, refs, entry) {
2335 struct got_tag_object *tag = NULL;
2336 struct got_object_id *ref_id;
2339 name = got_ref_get_name(re->ref);
2340 if (strcmp(name, GOT_REF_HEAD) == 0)
2342 if (strncmp(name, "refs/", 5) == 0)
2344 if (strncmp(name, "got/", 4) == 0)
2346 if (strncmp(name, "heads/", 6) == 0)
2348 if (strncmp(name, "remotes/", 8) == 0) {
2350 s = strstr(name, "/" GOT_REF_HEAD);
2351 if (s != NULL && strcmp(s, "/" GOT_REF_HEAD) == 0)
2354 err = got_ref_resolve(&ref_id, repo, re->ref);
2357 if (strncmp(name, "tags/", 5) == 0) {
2358 err = got_object_open_as_tag(&tag, repo, ref_id);
2360 if (err->code != GOT_ERR_OBJ_TYPE) {
2364 /* Ref points at something other than a tag. */
2369 cmp = got_object_id_cmp(tag ?
2370 got_object_tag_get_object_id(tag) : ref_id, id);
2373 got_object_tag_close(tag);
2377 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2378 s ? ", " : "", name) == -1) {
2379 err = got_error_from_errno("asprintf");
2390 static const struct got_error *
2391 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2396 smallerthan = strchr(author, '<');
2397 if (smallerthan && smallerthan[1] != '\0')
2398 author = smallerthan + 1;
2399 author[strcspn(author, "@>")] = '\0';
2400 return format_line(wauthor, author_width, NULL, author, 0, limit,
2404 static const struct got_error *
2405 draw_commit(struct tog_view *view, struct commit_queue_entry *entry,
2406 const size_t date_display_cols, int author_display_cols)
2408 struct tog_log_view_state *s = &view->state.log;
2409 const struct got_error *err = NULL;
2410 struct got_commit_object *commit = entry->commit;
2411 struct got_object_id *id = entry->id;
2412 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2413 char *refs_str = NULL;
2414 char *logmsg0 = NULL, *logmsg = NULL;
2415 char *author = NULL;
2416 wchar_t *wrefstr = NULL, *wlogmsg = NULL, *wauthor = NULL;
2417 int author_width, refstr_width, logmsg_width;
2418 char *newline, *line = NULL;
2419 int col, limit, scrollx, logmsg_x;
2420 const int avail = view->ncols, marker_column = author_display_cols + 1;
2422 time_t committer_time;
2423 struct tog_color *tc;
2424 struct got_reflist_head *refs;
2426 committer_time = got_object_commit_get_committer_time(commit);
2427 if (gmtime_r(&committer_time, &tm) == NULL)
2428 return got_error_from_errno("gmtime_r");
2429 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2430 return got_error(GOT_ERR_NO_SPACE);
2432 if (avail <= date_display_cols)
2433 limit = MIN(sizeof(datebuf) - 1, avail);
2435 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2436 tc = get_color(&s->colors, TOG_COLOR_DATE);
2438 wattr_on(view->window,
2439 COLOR_PAIR(tc->colorpair), NULL);
2440 waddnstr(view->window, datebuf, limit);
2442 wattr_off(view->window,
2443 COLOR_PAIR(tc->colorpair), NULL);
2450 err = got_object_id_str(&id_str, id);
2453 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2455 wattr_on(view->window,
2456 COLOR_PAIR(tc->colorpair), NULL);
2457 wprintw(view->window, "%.8s ", id_str);
2459 wattr_off(view->window,
2460 COLOR_PAIR(tc->colorpair), NULL);
2467 if (s->use_committer)
2468 author = strdup(got_object_commit_get_committer(commit));
2470 author = strdup(got_object_commit_get_author(commit));
2471 if (author == NULL) {
2472 err = got_error_from_errno("strdup");
2475 err = format_author(&wauthor, &author_width, author, avail - col, col);
2478 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2480 wattr_on(view->window,
2481 COLOR_PAIR(tc->colorpair), NULL);
2482 waddwstr(view->window, wauthor);
2483 col += author_width;
2484 while (col < avail && author_width < author_display_cols + 2) {
2485 if (tog_base_commit.id != NULL &&
2486 author_width == marker_column &&
2487 entry->idx == tog_base_commit.idx) {
2488 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2490 wattr_on(view->window,
2491 COLOR_PAIR(tc->colorpair), NULL);
2492 waddch(view->window, tog_base_commit.marker);
2494 wattr_off(view->window,
2495 COLOR_PAIR(tc->colorpair), NULL);
2497 waddch(view->window, ' ');
2502 wattr_off(view->window,
2503 COLOR_PAIR(tc->colorpair), NULL);
2507 err = got_object_commit_get_logmsg(&logmsg0, commit);
2511 while (*logmsg == '\n')
2513 newline = strchr(logmsg, '\n');
2517 limit = avail - col;
2518 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2519 limit--; /* for the border */
2521 /* Prepend reference labels to log message if possible .*/
2522 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, id);
2523 err = build_refs_str(&refs_str, refs, id, s->repo);
2529 if (asprintf(&rs, "[%s]", refs_str) == -1) {
2530 err = got_error_from_errno("asprintf");
2533 err = format_line(&wrefstr, &refstr_width,
2534 &scrollx, rs, view->x, limit, col, 1);
2538 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2540 wattr_on(view->window,
2541 COLOR_PAIR(tc->colorpair), NULL);
2542 waddwstr(view->window, &wrefstr[scrollx]);
2544 wattr_off(view->window,
2545 COLOR_PAIR(tc->colorpair), NULL);
2546 col += MAX(refstr_width, 0);
2551 waddch(view->window, ' ');
2555 if (refstr_width > 0)
2558 int unscrolled_refstr_width;
2559 size_t len = wcslen(wrefstr);
2562 * No need to check for -1 return value here since
2563 * unprintables have been replaced by span_wline().
2565 unscrolled_refstr_width = wcswidth(wrefstr, len);
2566 unscrolled_refstr_width += 1; /* trailing space */
2567 logmsg_x = view->x - unscrolled_refstr_width;
2570 limit = avail - col;
2571 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2572 limit--; /* for the border */
2576 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, logmsg_x,
2580 waddwstr(view->window, &wlogmsg[scrollx]);
2581 col += MAX(logmsg_width, 0);
2582 while (col < avail) {
2583 waddch(view->window, ' ');
2597 static struct commit_queue_entry *
2598 alloc_commit_queue_entry(struct got_commit_object *commit,
2599 struct got_object_id *id)
2601 struct commit_queue_entry *entry;
2602 struct got_object_id *dup;
2604 entry = calloc(1, sizeof(*entry));
2608 dup = got_object_id_dup(id);
2615 entry->commit = commit;
2620 pop_commit(struct commit_queue *commits)
2622 struct commit_queue_entry *entry;
2624 entry = TAILQ_FIRST(&commits->head);
2625 TAILQ_REMOVE(&commits->head, entry, entry);
2626 got_object_commit_close(entry->commit);
2627 commits->ncommits--;
2633 free_commits(struct commit_queue *commits)
2635 while (!TAILQ_EMPTY(&commits->head))
2636 pop_commit(commits);
2639 static const struct got_error *
2640 match_commit(int *have_match, struct got_object_id *id,
2641 struct got_commit_object *commit, regex_t *regex)
2643 const struct got_error *err = NULL;
2644 regmatch_t regmatch;
2645 char *id_str = NULL, *logmsg = NULL;
2649 err = got_object_id_str(&id_str, id);
2653 err = got_object_commit_get_logmsg(&logmsg, commit);
2657 if (regexec(regex, got_object_commit_get_author(commit), 1,
2658 ®match, 0) == 0 ||
2659 regexec(regex, got_object_commit_get_committer(commit), 1,
2660 ®match, 0) == 0 ||
2661 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2662 regexec(regex, logmsg, 1, ®match, 0) == 0)
2670 static const struct got_error *
2671 queue_commits(struct tog_log_thread_args *a)
2673 const struct got_error *err = NULL;
2676 * We keep all commits open throughout the lifetime of the log
2677 * view in order to avoid having to re-fetch commits from disk
2678 * while updating the display.
2681 struct got_object_id id;
2682 struct got_commit_object *commit;
2683 struct commit_queue_entry *entry;
2684 int limit_match = 0;
2687 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2692 err = got_object_open_as_commit(&commit, a->repo, &id);
2695 entry = alloc_commit_queue_entry(commit, &id);
2696 if (entry == NULL) {
2697 err = got_error_from_errno("alloc_commit_queue_entry");
2701 errcode = pthread_mutex_lock(&tog_mutex);
2703 err = got_error_set_errno(errcode,
2704 "pthread_mutex_lock");
2708 entry->idx = a->real_commits->ncommits;
2709 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2710 a->real_commits->ncommits++;
2712 if (tog_base_commit.id != NULL && tog_base_commit.idx == -1 &&
2713 got_object_id_cmp(&id, tog_base_commit.id) == 0)
2714 tog_base_commit.idx = entry->idx;
2717 err = match_commit(&limit_match, &id, commit,
2723 struct commit_queue_entry *matched;
2725 matched = alloc_commit_queue_entry(
2726 entry->commit, entry->id);
2727 if (matched == NULL) {
2728 err = got_error_from_errno(
2729 "alloc_commit_queue_entry");
2732 matched->commit = entry->commit;
2733 got_object_commit_retain(entry->commit);
2735 matched->idx = a->limit_commits->ncommits;
2736 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2738 a->limit_commits->ncommits++;
2742 * This is how we signal log_thread() that we
2743 * have found a match, and that it should be
2744 * counted as a new entry for the view.
2746 a->limit_match = limit_match;
2749 if (*a->searching == TOG_SEARCH_FORWARD &&
2750 !*a->search_next_done) {
2752 err = match_commit(&have_match, &id, commit, a->regex);
2757 if (limit_match && have_match)
2758 *a->search_next_done =
2759 TOG_SEARCH_HAVE_MORE;
2760 } else if (have_match)
2761 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2764 errcode = pthread_mutex_unlock(&tog_mutex);
2765 if (errcode && err == NULL)
2766 err = got_error_set_errno(errcode,
2767 "pthread_mutex_unlock");
2770 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2776 select_commit(struct tog_log_view_state *s)
2778 struct commit_queue_entry *entry;
2781 entry = s->first_displayed_entry;
2783 if (ncommits == s->selected) {
2784 s->selected_entry = entry;
2787 entry = TAILQ_NEXT(entry, entry);
2792 static const struct got_error *
2793 draw_commits(struct tog_view *view)
2795 const struct got_error *err = NULL;
2796 struct tog_log_view_state *s = &view->state.log;
2797 struct commit_queue_entry *entry = s->selected_entry;
2798 int limit = view->nlines;
2800 int ncommits, author_cols = 4, refstr_cols;
2801 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2802 char *refs_str = NULL;
2804 struct tog_color *tc;
2805 static const size_t date_display_cols = 12;
2806 struct got_reflist_head *refs;
2808 if (view_is_hsplit_top(view))
2809 --limit; /* account for border */
2811 if (s->selected_entry &&
2812 !(view->searching && view->search_next_done == 0)) {
2813 err = got_object_id_str(&id_str, s->selected_entry->id);
2816 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2817 s->selected_entry->id);
2818 err = build_refs_str(&refs_str, refs, s->selected_entry->id,
2824 if (s->thread_args.commits_needed == 0 && !using_mock_io)
2825 halfdelay(10); /* disable fast refresh */
2827 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2828 if (asprintf(&ncommits_str, " [%d/%d] %s",
2829 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2830 (view->searching && !view->search_next_done) ?
2831 "searching..." : "loading...") == -1) {
2832 err = got_error_from_errno("asprintf");
2836 const char *search_str = NULL;
2837 const char *limit_str = NULL;
2839 if (view->searching) {
2840 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2841 search_str = "no more matches";
2842 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2843 search_str = "no matches found";
2844 else if (!view->search_next_done)
2845 search_str = "searching...";
2848 if (s->limit_view && s->commits->ncommits == 0)
2849 limit_str = "no matches found";
2851 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2852 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2853 search_str ? search_str : (refs_str ? refs_str : ""),
2854 limit_str ? limit_str : "") == -1) {
2855 err = got_error_from_errno("asprintf");
2863 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2864 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2865 "........................................",
2866 s->in_repo_path, ncommits_str) == -1) {
2867 err = got_error_from_errno("asprintf");
2871 } else if (asprintf(&header, "commit %s%s",
2872 id_str ? id_str : "........................................",
2873 ncommits_str) == -1) {
2874 err = got_error_from_errno("asprintf");
2878 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2882 werase(view->window);
2884 if (view_needs_focus_indication(view))
2885 wstandout(view->window);
2886 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2888 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2889 waddwstr(view->window, wline);
2890 while (width < view->ncols) {
2891 waddch(view->window, ' ');
2895 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2896 if (view_needs_focus_indication(view))
2897 wstandend(view->window);
2902 /* Grow author column size if necessary, and set view->maxx. */
2903 entry = s->first_displayed_entry;
2907 struct got_commit_object *c = entry->commit;
2908 char *author, *eol, *msg, *msg0;
2909 wchar_t *wauthor, *wmsg;
2911 if (ncommits >= limit - 1)
2913 if (s->use_committer)
2914 author = strdup(got_object_commit_get_committer(c));
2916 author = strdup(got_object_commit_get_author(c));
2917 if (author == NULL) {
2918 err = got_error_from_errno("strdup");
2921 err = format_author(&wauthor, &width, author, COLS,
2923 if (author_cols < width)
2924 author_cols = width;
2929 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2931 err = build_refs_str(&refs_str, refs, entry->id, s->repo);
2936 err = format_line(&ws, &width, NULL, refs_str,
2937 0, INT_MAX, date_display_cols + author_cols, 0);
2943 refstr_cols = width + 3; /* account for [ ] + space */
2946 err = got_object_commit_get_logmsg(&msg0, c);
2950 while (*msg == '\n')
2952 if ((eol = strchr(msg, '\n')))
2954 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2955 date_display_cols + author_cols + refstr_cols, 0);
2958 view->maxx = MAX(view->maxx, width + refstr_cols);
2962 entry = TAILQ_NEXT(entry, entry);
2965 entry = s->first_displayed_entry;
2966 s->last_displayed_entry = s->first_displayed_entry;
2969 if (ncommits >= limit - 1)
2971 if (ncommits == s->selected)
2972 wstandout(view->window);
2973 err = draw_commit(view, entry, date_display_cols, author_cols);
2974 if (ncommits == s->selected)
2975 wstandend(view->window);
2979 s->last_displayed_entry = entry;
2980 entry = TAILQ_NEXT(entry, entry);
2993 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2995 struct commit_queue_entry *entry;
2998 entry = TAILQ_FIRST(&s->commits->head);
2999 if (s->first_displayed_entry == entry)
3002 entry = s->first_displayed_entry;
3003 while (entry && nscrolled < maxscroll) {
3004 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3006 s->first_displayed_entry = entry;
3012 static const struct got_error *
3013 trigger_log_thread(struct tog_view *view, int wait)
3015 struct tog_log_thread_args *ta = &view->state.log.thread_args;
3019 halfdelay(1); /* fast refresh while loading commits */
3021 while (!ta->log_complete && !tog_thread_error &&
3022 (ta->commits_needed > 0 || ta->load_all)) {
3023 /* Wake the log thread. */
3024 errcode = pthread_cond_signal(&ta->need_commits);
3026 return got_error_set_errno(errcode,
3027 "pthread_cond_signal");
3030 * The mutex will be released while the view loop waits
3031 * in wgetch(), at which time the log thread will run.
3036 /* Display progress update in log view. */
3037 show_log_view(view);
3041 /* Wait right here while next commit is being loaded. */
3042 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
3044 return got_error_set_errno(errcode,
3045 "pthread_cond_wait");
3047 /* Display progress update in log view. */
3048 show_log_view(view);
3056 static const struct got_error *
3057 request_log_commits(struct tog_view *view)
3059 struct tog_log_view_state *state = &view->state.log;
3060 const struct got_error *err = NULL;
3062 if (state->thread_args.log_complete)
3065 state->thread_args.commits_needed += view->nscrolled;
3066 err = trigger_log_thread(view, 1);
3067 view->nscrolled = 0;
3072 static const struct got_error *
3073 log_scroll_down(struct tog_view *view, int maxscroll)
3075 struct tog_log_view_state *s = &view->state.log;
3076 const struct got_error *err = NULL;
3077 struct commit_queue_entry *pentry;
3078 int nscrolled = 0, ncommits_needed;
3080 if (s->last_displayed_entry == NULL)
3083 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
3084 if (s->commits->ncommits < ncommits_needed &&
3085 !s->thread_args.log_complete) {
3087 * Ask the log thread for required amount of commits.
3089 s->thread_args.commits_needed +=
3090 ncommits_needed - s->commits->ncommits;
3091 err = trigger_log_thread(view, 1);
3097 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
3098 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
3101 s->last_displayed_entry = pentry ?
3102 pentry : s->last_displayed_entry;
3104 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
3107 s->first_displayed_entry = pentry;
3108 } while (++nscrolled < maxscroll);
3110 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
3111 view->nscrolled += nscrolled;
3113 view->nscrolled = 0;
3118 static const struct got_error *
3119 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
3120 struct got_commit_object *commit, struct got_object_id *commit_id,
3121 struct tog_view *log_view, struct got_repository *repo)
3123 const struct got_error *err;
3124 struct got_object_qid *parent_id;
3125 struct tog_view *diff_view;
3127 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
3128 if (diff_view == NULL)
3129 return got_error_from_errno("view_open");
3131 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3132 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
3133 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
3135 *new_view = diff_view;
3139 static const struct got_error *
3140 tree_view_visit_subtree(struct tog_tree_view_state *s,
3141 struct got_tree_object *subtree)
3143 struct tog_parent_tree *parent;
3145 parent = calloc(1, sizeof(*parent));
3147 return got_error_from_errno("calloc");
3149 parent->tree = s->tree;
3150 parent->first_displayed_entry = s->first_displayed_entry;
3151 parent->selected_entry = s->selected_entry;
3152 parent->selected = s->selected;
3153 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3156 s->first_displayed_entry = NULL;
3160 static const struct got_error *
3161 tree_view_walk_path(struct tog_tree_view_state *s,
3162 struct got_commit_object *commit, const char *path)
3164 const struct got_error *err = NULL;
3165 struct got_tree_object *tree = NULL;
3167 char *slash, *subpath = NULL;
3169 /* Walk the path and open corresponding tree objects. */
3172 struct got_tree_entry *te;
3173 struct got_object_id *tree_id;
3179 /* Ensure the correct subtree entry is selected. */
3180 slash = strchr(p, '/');
3182 te_name = strdup(p);
3184 te_name = strndup(p, slash - p);
3185 if (te_name == NULL) {
3186 err = got_error_from_errno("strndup");
3189 te = got_object_tree_find_entry(s->tree, te_name);
3191 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
3196 s->first_displayed_entry = s->selected_entry = te;
3198 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
3199 break; /* jump to this file's entry */
3201 slash = strchr(p, '/');
3203 subpath = strndup(path, slash - path);
3205 subpath = strdup(path);
3206 if (subpath == NULL) {
3207 err = got_error_from_errno("strdup");
3211 err = got_object_id_by_path(&tree_id, s->repo, commit,
3216 err = got_object_open_as_tree(&tree, s->repo, tree_id);
3221 err = tree_view_visit_subtree(s, tree);
3223 got_object_tree_close(tree);
3237 static const struct got_error *
3238 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
3239 struct commit_queue_entry *entry, const char *path,
3240 const char *head_ref_name, struct got_repository *repo)
3242 const struct got_error *err = NULL;
3243 struct tog_tree_view_state *s;
3244 struct tog_view *tree_view;
3246 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
3247 if (tree_view == NULL)
3248 return got_error_from_errno("view_open");
3250 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
3253 s = &tree_view->state.tree;
3255 *new_view = tree_view;
3257 if (got_path_is_root_dir(path))
3260 return tree_view_walk_path(s, entry->commit, path);
3263 static const struct got_error *
3264 block_signals_used_by_main_thread(void)
3269 if (sigemptyset(&sigset) == -1)
3270 return got_error_from_errno("sigemptyset");
3272 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
3273 if (sigaddset(&sigset, SIGWINCH) == -1)
3274 return got_error_from_errno("sigaddset");
3275 if (sigaddset(&sigset, SIGCONT) == -1)
3276 return got_error_from_errno("sigaddset");
3277 if (sigaddset(&sigset, SIGINT) == -1)
3278 return got_error_from_errno("sigaddset");
3279 if (sigaddset(&sigset, SIGTERM) == -1)
3280 return got_error_from_errno("sigaddset");
3282 /* ncurses handles SIGTSTP */
3283 if (sigaddset(&sigset, SIGTSTP) == -1)
3284 return got_error_from_errno("sigaddset");
3286 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
3288 return got_error_set_errno(errcode, "pthread_sigmask");
3294 log_thread(void *arg)
3296 const struct got_error *err = NULL;
3298 struct tog_log_thread_args *a = arg;
3302 * Sync startup with main thread such that we begin our
3303 * work once view_input() has released the mutex.
3305 errcode = pthread_mutex_lock(&tog_mutex);
3307 err = got_error_set_errno(errcode, "pthread_mutex_lock");
3311 err = block_signals_used_by_main_thread();
3313 pthread_mutex_unlock(&tog_mutex);
3317 while (!done && !err && !tog_fatal_signal_received()) {
3318 errcode = pthread_mutex_unlock(&tog_mutex);
3320 err = got_error_set_errno(errcode,
3321 "pthread_mutex_unlock");
3324 err = queue_commits(a);
3326 if (err->code != GOT_ERR_ITER_COMPLETED)
3330 } else if (a->commits_needed > 0 && !a->load_all) {
3333 a->commits_needed--;
3335 a->commits_needed--;
3338 errcode = pthread_mutex_lock(&tog_mutex);
3340 err = got_error_set_errno(errcode,
3341 "pthread_mutex_lock");
3343 } else if (*a->quit)
3345 else if (*a->limiting && *a->first_displayed_entry == NULL) {
3346 *a->first_displayed_entry =
3347 TAILQ_FIRST(&a->limit_commits->head);
3348 *a->selected_entry = *a->first_displayed_entry;
3349 } else if (*a->first_displayed_entry == NULL) {
3350 *a->first_displayed_entry =
3351 TAILQ_FIRST(&a->real_commits->head);
3352 *a->selected_entry = *a->first_displayed_entry;
3355 errcode = pthread_cond_signal(&a->commit_loaded);
3357 err = got_error_set_errno(errcode,
3358 "pthread_cond_signal");
3359 pthread_mutex_unlock(&tog_mutex);
3364 a->commits_needed = 0;
3366 if (a->commits_needed == 0 && !a->load_all) {
3367 errcode = pthread_cond_wait(&a->need_commits,
3370 err = got_error_set_errno(errcode,
3371 "pthread_cond_wait");
3372 pthread_mutex_unlock(&tog_mutex);
3380 a->log_complete = 1;
3381 errcode = pthread_mutex_unlock(&tog_mutex);
3383 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3386 tog_thread_error = 1;
3387 pthread_cond_signal(&a->commit_loaded);
3392 static const struct got_error *
3393 stop_log_thread(struct tog_log_view_state *s)
3395 const struct got_error *err = NULL, *thread_err = NULL;
3400 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3402 return got_error_set_errno(errcode,
3403 "pthread_cond_signal");
3404 errcode = pthread_mutex_unlock(&tog_mutex);
3406 return got_error_set_errno(errcode,
3407 "pthread_mutex_unlock");
3408 errcode = pthread_join(s->thread, (void **)&thread_err);
3410 return got_error_set_errno(errcode, "pthread_join");
3411 errcode = pthread_mutex_lock(&tog_mutex);
3413 return got_error_set_errno(errcode,
3414 "pthread_mutex_lock");
3418 if (s->thread_args.repo) {
3419 err = got_repo_close(s->thread_args.repo);
3420 s->thread_args.repo = NULL;
3423 if (s->thread_args.pack_fds) {
3424 const struct got_error *pack_err =
3425 got_repo_pack_fds_close(s->thread_args.pack_fds);
3428 s->thread_args.pack_fds = NULL;
3431 if (s->thread_args.graph) {
3432 got_commit_graph_close(s->thread_args.graph);
3433 s->thread_args.graph = NULL;
3436 return err ? err : thread_err;
3439 static const struct got_error *
3440 close_log_view(struct tog_view *view)
3442 const struct got_error *err = NULL;
3443 struct tog_log_view_state *s = &view->state.log;
3446 err = stop_log_thread(s);
3448 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3449 if (errcode && err == NULL)
3450 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3452 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3453 if (errcode && err == NULL)
3454 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3456 free_commits(&s->limit_commits);
3457 free_commits(&s->real_commits);
3458 free(s->in_repo_path);
3459 s->in_repo_path = NULL;
3462 free(s->head_ref_name);
3463 s->head_ref_name = NULL;
3468 * We use two queues to implement the limit feature: first consists of
3469 * commits matching the current limit_regex; second is the real queue
3470 * of all known commits (real_commits). When the user starts limiting,
3471 * we swap queues such that all movement and displaying functionality
3472 * works with very slight change.
3474 static const struct got_error *
3475 limit_log_view(struct tog_view *view)
3477 struct tog_log_view_state *s = &view->state.log;
3478 struct commit_queue_entry *entry;
3479 struct tog_view *v = view;
3480 const struct got_error *err = NULL;
3484 if (view_is_hsplit_top(view))
3486 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3489 /* Get the pattern */
3490 wmove(v->window, v->nlines - 1, 0);
3491 wclrtoeol(v->window);
3492 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3493 nodelay(v->window, FALSE);
3496 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3499 nodelay(v->window, TRUE);
3503 if (*pattern == '\0') {
3505 * Safety measure for the situation where the user
3506 * resets limit without previously limiting anything.
3512 * User could have pressed Ctrl+L, which refreshed the
3513 * commit queues, it means we can't save previously
3514 * (before limit took place) displayed entries,
3515 * because they would point to already free'ed memory,
3516 * so we are forced to always select first entry of
3519 s->commits = &s->real_commits;
3520 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3521 s->selected_entry = s->first_displayed_entry;
3528 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3533 /* Clear the screen while loading limit view */
3534 s->first_displayed_entry = NULL;
3535 s->last_displayed_entry = NULL;
3536 s->selected_entry = NULL;
3537 s->commits = &s->limit_commits;
3539 /* Prepare limit queue for new search */
3540 free_commits(&s->limit_commits);
3541 s->limit_commits.ncommits = 0;
3543 /* First process commits, which are in queue already */
3544 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3547 err = match_commit(&have_match, entry->id,
3548 entry->commit, &s->limit_regex);
3553 struct commit_queue_entry *matched;
3555 matched = alloc_commit_queue_entry(entry->commit,
3557 if (matched == NULL) {
3558 err = got_error_from_errno(
3559 "alloc_commit_queue_entry");
3562 matched->commit = entry->commit;
3563 got_object_commit_retain(entry->commit);
3565 matched->idx = s->limit_commits.ncommits;
3566 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3568 s->limit_commits.ncommits++;
3572 /* Second process all the commits, until we fill the screen */
3573 if (s->limit_commits.ncommits < view->nlines - 1 &&
3574 !s->thread_args.log_complete) {
3575 s->thread_args.commits_needed +=
3576 view->nlines - s->limit_commits.ncommits - 1;
3577 err = trigger_log_thread(view, 1);
3582 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3583 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3589 static const struct got_error *
3590 search_start_log_view(struct tog_view *view)
3592 struct tog_log_view_state *s = &view->state.log;
3594 s->matched_entry = NULL;
3595 s->search_entry = NULL;
3599 static const struct got_error *
3600 search_next_log_view(struct tog_view *view)
3602 const struct got_error *err = NULL;
3603 struct tog_log_view_state *s = &view->state.log;
3604 struct commit_queue_entry *entry;
3606 /* Display progress update in log view. */
3607 show_log_view(view);
3611 if (s->search_entry) {
3613 errcode = pthread_mutex_unlock(&tog_mutex);
3615 return got_error_set_errno(errcode,
3616 "pthread_mutex_unlock");
3617 ch = wgetch(view->window);
3618 errcode = pthread_mutex_lock(&tog_mutex);
3620 return got_error_set_errno(errcode,
3621 "pthread_mutex_lock");
3622 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3623 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3626 if (view->searching == TOG_SEARCH_FORWARD)
3627 entry = TAILQ_NEXT(s->search_entry, entry);
3629 entry = TAILQ_PREV(s->search_entry,
3630 commit_queue_head, entry);
3631 } else if (s->matched_entry) {
3633 * If the user has moved the cursor after we hit a match,
3634 * the position from where we should continue searching
3635 * might have changed.
3637 if (view->searching == TOG_SEARCH_FORWARD)
3638 entry = TAILQ_NEXT(s->selected_entry, entry);
3640 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3643 entry = s->selected_entry;
3649 if (entry == NULL) {
3650 if (s->thread_args.log_complete ||
3651 view->searching == TOG_SEARCH_BACKWARD) {
3652 view->search_next_done =
3653 (s->matched_entry == NULL ?
3654 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3655 s->search_entry = NULL;
3659 * Poke the log thread for more commits and return,
3660 * allowing the main loop to make progress. Search
3661 * will resume at s->search_entry once we come back.
3663 s->thread_args.commits_needed++;
3664 return trigger_log_thread(view, 0);
3667 err = match_commit(&have_match, entry->id, entry->commit,
3672 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3673 s->matched_entry = entry;
3677 s->search_entry = entry;
3678 if (view->searching == TOG_SEARCH_FORWARD)
3679 entry = TAILQ_NEXT(entry, entry);
3681 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3684 if (s->matched_entry) {
3685 int cur = s->selected_entry->idx;
3686 while (cur < s->matched_entry->idx) {
3687 err = input_log_view(NULL, view, KEY_DOWN);
3692 while (cur > s->matched_entry->idx) {
3693 err = input_log_view(NULL, view, KEY_UP);
3700 s->search_entry = NULL;
3705 static const struct got_error *
3706 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3707 struct got_repository *repo, const char *head_ref_name,
3708 const char *in_repo_path, int log_branches)
3710 const struct got_error *err = NULL;
3711 struct tog_log_view_state *s = &view->state.log;
3712 struct got_repository *thread_repo = NULL;
3713 struct got_commit_graph *thread_graph = NULL;
3716 if (in_repo_path != s->in_repo_path) {
3717 free(s->in_repo_path);
3718 s->in_repo_path = strdup(in_repo_path);
3719 if (s->in_repo_path == NULL) {
3720 err = got_error_from_errno("strdup");
3725 /* The commit queue only contains commits being displayed. */
3726 TAILQ_INIT(&s->real_commits.head);
3727 s->real_commits.ncommits = 0;
3728 s->commits = &s->real_commits;
3730 TAILQ_INIT(&s->limit_commits.head);
3732 s->limit_commits.ncommits = 0;
3735 if (head_ref_name) {
3736 s->head_ref_name = strdup(head_ref_name);
3737 if (s->head_ref_name == NULL) {
3738 err = got_error_from_errno("strdup");
3742 s->start_id = got_object_id_dup(start_id);
3743 if (s->start_id == NULL) {
3744 err = got_error_from_errno("got_object_id_dup");
3747 s->log_branches = log_branches;
3748 s->use_committer = 1;
3750 STAILQ_INIT(&s->colors);
3751 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3752 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3753 get_color_value("TOG_COLOR_COMMIT"));
3756 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3757 get_color_value("TOG_COLOR_AUTHOR"));
3759 free_colors(&s->colors);
3762 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3763 get_color_value("TOG_COLOR_DATE"));
3765 free_colors(&s->colors);
3770 view->show = show_log_view;
3771 view->input = input_log_view;
3772 view->resize = resize_log_view;
3773 view->close = close_log_view;
3774 view->search_start = search_start_log_view;
3775 view->search_next = search_next_log_view;
3777 if (s->thread_args.pack_fds == NULL) {
3778 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3782 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3783 s->thread_args.pack_fds);
3786 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3790 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3791 s->repo, NULL, NULL);
3795 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3797 err = got_error_set_errno(errcode, "pthread_cond_init");
3800 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3802 err = got_error_set_errno(errcode, "pthread_cond_init");
3806 s->thread_args.commits_needed = view->nlines;
3807 s->thread_args.graph = thread_graph;
3808 s->thread_args.real_commits = &s->real_commits;
3809 s->thread_args.limit_commits = &s->limit_commits;
3810 s->thread_args.in_repo_path = s->in_repo_path;
3811 s->thread_args.start_id = s->start_id;
3812 s->thread_args.repo = thread_repo;
3813 s->thread_args.log_complete = 0;
3814 s->thread_args.quit = &s->quit;
3815 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3816 s->thread_args.selected_entry = &s->selected_entry;
3817 s->thread_args.searching = &view->searching;
3818 s->thread_args.search_next_done = &view->search_next_done;
3819 s->thread_args.regex = &view->regex;
3820 s->thread_args.limiting = &s->limit_view;
3821 s->thread_args.limit_regex = &s->limit_regex;
3822 s->thread_args.limit_commits = &s->limit_commits;
3825 if (view->close == NULL)
3826 close_log_view(view);
3832 static const struct got_error *
3833 show_log_view(struct tog_view *view)
3835 const struct got_error *err;
3836 struct tog_log_view_state *s = &view->state.log;
3838 if (s->thread == NULL) {
3839 int errcode = pthread_create(&s->thread, NULL, log_thread,
3842 return got_error_set_errno(errcode, "pthread_create");
3843 if (s->thread_args.commits_needed > 0) {
3844 err = trigger_log_thread(view, 1);
3850 return draw_commits(view);
3854 log_move_cursor_up(struct tog_view *view, int page, int home)
3856 struct tog_log_view_state *s = &view->state.log;
3858 if (s->first_displayed_entry == NULL)
3860 if (s->selected_entry->idx == 0)
3863 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3865 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3867 if (!page && !home && s->selected > 0)
3870 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3876 static const struct got_error *
3877 log_move_cursor_down(struct tog_view *view, int page)
3879 struct tog_log_view_state *s = &view->state.log;
3880 const struct got_error *err = NULL;
3881 int eos = view->nlines - 2;
3883 if (s->first_displayed_entry == NULL)
3886 if (s->thread_args.log_complete &&
3887 s->selected_entry->idx >= s->commits->ncommits - 1)
3890 if (view_is_hsplit_top(view))
3891 --eos; /* border consumes the last line */
3894 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3897 err = log_scroll_down(view, 1);
3898 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3899 struct commit_queue_entry *entry;
3903 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3904 s->last_displayed_entry = entry;
3905 for (n = 0; n <= eos; n++) {
3908 s->first_displayed_entry = entry;
3909 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3912 s->selected = n - 1;
3914 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3915 s->thread_args.log_complete)
3916 s->selected += MIN(page,
3917 s->commits->ncommits - s->selected_entry->idx - 1);
3919 err = log_scroll_down(view, page);
3925 * We might necessarily overshoot in horizontal
3926 * splits; if so, select the last displayed commit.
3928 if (s->first_displayed_entry && s->last_displayed_entry) {
3929 s->selected = MIN(s->selected,
3930 s->last_displayed_entry->idx -
3931 s->first_displayed_entry->idx);
3936 if (s->thread_args.log_complete &&
3937 s->selected_entry->idx == s->commits->ncommits - 1)
3944 view_get_split(struct tog_view *view, int *y, int *x)
3949 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3950 if (view->child && view->child->resized_y)
3951 *y = view->child->resized_y;
3952 else if (view->resized_y)
3953 *y = view->resized_y;
3955 *y = view_split_begin_y(view->lines);
3956 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3957 if (view->child && view->child->resized_x)
3958 *x = view->child->resized_x;
3959 else if (view->resized_x)
3960 *x = view->resized_x;
3962 *x = view_split_begin_x(view->begin_x);
3966 /* Split view horizontally at y and offset view->state->selected line. */
3967 static const struct got_error *
3968 view_init_hsplit(struct tog_view *view, int y)
3970 const struct got_error *err = NULL;
3974 err = view_resize(view);
3978 err = offset_selection_down(view);
3983 static const struct got_error *
3984 log_goto_line(struct tog_view *view, int nlines)
3986 const struct got_error *err = NULL;
3987 struct tog_log_view_state *s = &view->state.log;
3988 int g, idx = s->selected_entry->idx;
3990 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3996 if (g >= s->first_displayed_entry->idx + 1 &&
3997 g <= s->last_displayed_entry->idx + 1 &&
3998 g - s->first_displayed_entry->idx - 1 < nlines) {
3999 s->selected = g - s->first_displayed_entry->idx - 1;
4005 err = log_move_cursor_down(view, g - idx - 1);
4006 if (!err && g > s->selected_entry->idx + 1)
4007 err = log_move_cursor_down(view,
4008 g - s->first_displayed_entry->idx - 1);
4011 } else if (idx + 1 > g)
4012 log_move_cursor_up(view, idx - g + 1, 0);
4014 if (g < nlines && s->first_displayed_entry->idx == 0)
4015 s->selected = g - 1;
4023 horizontal_scroll_input(struct tog_view *view, int ch)
4029 view->x -= MIN(view->x, 2);
4035 if (view->x + view->ncols / 2 < view->maxx)
4044 view->x = MAX(view->maxx - view->ncols / 2, 0);
4052 static const struct got_error *
4053 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
4055 const struct got_error *err = NULL;
4056 struct tog_log_view_state *s = &view->state.log;
4059 if (s->thread_args.load_all) {
4060 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
4061 s->thread_args.load_all = 0;
4062 else if (s->thread_args.log_complete) {
4063 err = log_move_cursor_down(view, s->commits->ncommits);
4064 s->thread_args.load_all = 0;
4070 eos = nscroll = view->nlines - 1;
4071 if (view_is_hsplit_top(view))
4075 return log_goto_line(view, eos);
4079 err = limit_log_view(view);
4090 horizontal_scroll_input(view, ch);
4097 log_move_cursor_up(view, 0, 0);
4102 log_move_cursor_up(view, 0, 1);
4112 log_move_cursor_up(view, nscroll, 0);
4119 err = log_move_cursor_down(view, 0);
4122 s->use_committer = !s->use_committer;
4123 view->action = s->use_committer ?
4124 "show committer" : "show commit author";
4129 /* We don't know yet how many commits, so we're forced to
4130 * traverse them all. */
4132 s->thread_args.load_all = 1;
4133 if (!s->thread_args.log_complete)
4134 return trigger_log_thread(view, 0);
4135 err = log_move_cursor_down(view, s->commits->ncommits);
4136 s->thread_args.load_all = 0;
4147 err = log_move_cursor_down(view, nscroll);
4150 if (s->selected > view->nlines - 2)
4151 s->selected = view->nlines - 2;
4152 if (s->selected > s->commits->ncommits - 1)
4153 s->selected = s->commits->ncommits - 1;
4155 if (s->commits->ncommits < view->nlines - 1 &&
4156 !s->thread_args.log_complete) {
4157 s->thread_args.commits_needed += (view->nlines - 1) -
4158 s->commits->ncommits;
4159 err = trigger_log_thread(view, 1);
4165 if (s->selected_entry == NULL)
4167 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4171 if (s->selected_entry == NULL)
4173 err = view_request_new(new_view, view, TOG_VIEW_TREE);
4179 if (ch == KEY_BACKSPACE &&
4180 got_path_is_root_dir(s->in_repo_path))
4182 err = stop_log_thread(s);
4185 if (ch == KEY_BACKSPACE) {
4187 err = got_path_dirname(&parent_path, s->in_repo_path);
4190 free(s->in_repo_path);
4191 s->in_repo_path = parent_path;
4192 s->thread_args.in_repo_path = s->in_repo_path;
4193 } else if (ch == CTRL('l')) {
4194 struct got_object_id *start_id;
4195 err = got_repo_match_object_id(&start_id, NULL,
4196 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4197 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4199 if (s->head_ref_name == NULL ||
4200 err->code != GOT_ERR_NOT_REF)
4202 /* Try to cope with deleted references. */
4203 free(s->head_ref_name);
4204 s->head_ref_name = NULL;
4205 err = got_repo_match_object_id(&start_id,
4206 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4207 &tog_refs, s->repo);
4212 s->start_id = start_id;
4213 s->thread_args.start_id = s->start_id;
4215 s->log_branches = !s->log_branches;
4217 if (s->thread_args.pack_fds == NULL) {
4218 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4222 err = got_repo_open(&s->thread_args.repo,
4223 got_repo_get_path(s->repo), NULL,
4224 s->thread_args.pack_fds);
4228 err = tog_load_refs(s->repo, 0);
4231 err = got_commit_graph_open(&s->thread_args.graph,
4232 s->in_repo_path, !s->log_branches);
4235 err = got_commit_graph_iter_start(s->thread_args.graph,
4236 s->start_id, s->repo, NULL, NULL);
4239 free_commits(&s->real_commits);
4240 free_commits(&s->limit_commits);
4241 s->first_displayed_entry = NULL;
4242 s->last_displayed_entry = NULL;
4243 s->selected_entry = NULL;
4245 s->thread_args.log_complete = 0;
4247 s->thread_args.commits_needed = view->lines;
4248 s->matched_entry = NULL;
4249 s->search_entry = NULL;
4254 err = view_request_new(new_view, view, TOG_VIEW_REF);
4264 static const struct got_error *
4265 apply_unveil(const char *repo_path, const char *worktree_path)
4267 const struct got_error *error;
4270 if (unveil("gmon.out", "rwc") != 0)
4271 return got_error_from_errno2("unveil", "gmon.out");
4273 if (repo_path && unveil(repo_path, "r") != 0)
4274 return got_error_from_errno2("unveil", repo_path);
4276 if (worktree_path && unveil(worktree_path, "rwc") != 0)
4277 return got_error_from_errno2("unveil", worktree_path);
4279 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4280 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4282 error = got_privsep_unveil_exec_helpers();
4286 if (unveil(NULL, NULL) != 0)
4287 return got_error_from_errno("unveil");
4292 static const struct got_error *
4293 init_mock_term(const char *test_script_path)
4295 const struct got_error *err = NULL;
4296 const char *screen_dump_path;
4299 if (test_script_path == NULL || *test_script_path == '\0')
4300 return got_error_msg(GOT_ERR_IO, "TOG_TEST_SCRIPT not defined");
4302 tog_io.f = fopen(test_script_path, "re");
4303 if (tog_io.f == NULL) {
4304 err = got_error_from_errno_fmt("fopen: %s",
4309 /* test mode, we don't want any output */
4310 tog_io.cout = fopen("/dev/null", "w+");
4311 if (tog_io.cout == NULL) {
4312 err = got_error_from_errno2("fopen", "/dev/null");
4316 in = dup(fileno(tog_io.cout));
4318 err = got_error_from_errno("dup");
4321 tog_io.cin = fdopen(in, "r");
4322 if (tog_io.cin == NULL) {
4323 err = got_error_from_errno("fdopen");
4328 screen_dump_path = getenv("TOG_SCR_DUMP");
4329 if (screen_dump_path == NULL || *screen_dump_path == '\0')
4330 return got_error_msg(GOT_ERR_IO, "TOG_SCR_DUMP not defined");
4331 tog_io.sdump = fopen(screen_dump_path, "we");
4332 if (tog_io.sdump == NULL) {
4333 err = got_error_from_errno2("fopen", screen_dump_path);
4337 if (fseeko(tog_io.f, 0L, SEEK_SET) == -1) {
4338 err = got_error_from_errno("fseeko");
4342 if (newterm(NULL, tog_io.cout, tog_io.cin) == NULL)
4343 err = got_error_msg(GOT_ERR_IO,
4344 "newterm: failed to initialise curses");
4358 * Override default signal handlers before starting ncurses.
4359 * This should prevent ncurses from installing its own
4360 * broken cleanup() signal handler.
4362 signal(SIGWINCH, tog_sigwinch);
4363 signal(SIGPIPE, tog_sigpipe);
4364 signal(SIGCONT, tog_sigcont);
4365 signal(SIGINT, tog_sigint);
4366 signal(SIGTERM, tog_sigterm);
4368 if (using_mock_io) /* In test mode we use a fake terminal */
4374 halfdelay(1); /* Fast refresh while initial view is loading. */
4377 intrflush(stdscr, FALSE);
4378 keypad(stdscr, TRUE);
4380 if (getenv("TOG_COLORS") != NULL) {
4382 use_default_colors();
4388 static const struct got_error *
4389 set_tog_base_commit(struct got_repository *repo, struct got_worktree *worktree)
4391 tog_base_commit.id = got_object_id_dup(
4392 got_worktree_get_base_commit_id(worktree));
4393 if (tog_base_commit.id == NULL)
4394 return got_error_from_errno( "got_object_id_dup");
4396 tog_base_commit.idx = -1;
4398 return got_worktree_get_state(&tog_base_commit.marker, repo,
4402 static const struct got_error *
4403 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4404 struct got_repository *repo, struct got_worktree *worktree)
4406 const struct got_error *err = NULL;
4409 *in_repo_path = strdup("/");
4410 if (*in_repo_path == NULL)
4411 return got_error_from_errno("strdup");
4416 const char *prefix = got_worktree_get_path_prefix(worktree);
4419 err = got_worktree_resolve_path(&p, worktree, argv[0]);
4422 if (asprintf(in_repo_path, "%s%s%s", prefix,
4423 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4425 err = got_error_from_errno("asprintf");
4426 *in_repo_path = NULL;
4430 err = got_repo_map_path(in_repo_path, repo, argv[0]);
4435 static const struct got_error *
4436 cmd_log(int argc, char *argv[])
4438 const struct got_error *error;
4439 struct got_repository *repo = NULL;
4440 struct got_worktree *worktree = NULL;
4441 struct got_object_id *start_id = NULL;
4442 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4443 char *keyword_idstr = NULL, *start_commit = NULL, *label = NULL;
4444 struct got_reference *ref = NULL;
4445 const char *head_ref_name = NULL;
4446 int ch, log_branches = 0;
4447 struct tog_view *view;
4448 int *pack_fds = NULL;
4450 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4456 start_commit = optarg;
4459 repo_path = realpath(optarg, NULL);
4460 if (repo_path == NULL)
4461 return got_error_from_errno2("realpath",
4476 error = got_repo_pack_fds_open(&pack_fds);
4480 if (repo_path == NULL) {
4481 cwd = getcwd(NULL, 0);
4483 error = got_error_from_errno("getcwd");
4486 error = got_worktree_open(&worktree, cwd, NULL);
4487 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4491 strdup(got_worktree_get_repo_path(worktree));
4493 repo_path = strdup(cwd);
4494 if (repo_path == NULL) {
4495 error = got_error_from_errno("strdup");
4500 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4504 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4511 error = apply_unveil(got_repo_get_path(repo),
4512 worktree ? got_worktree_get_root_path(worktree) : NULL);
4516 /* already loaded by tog_log_with_path()? */
4517 if (TAILQ_EMPTY(&tog_refs)) {
4518 error = tog_load_refs(repo, 0);
4523 if (start_commit == NULL) {
4524 error = got_repo_match_object_id(&start_id, &label,
4525 worktree ? got_worktree_get_head_ref_name(worktree) :
4526 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4529 head_ref_name = label;
4531 error = got_keyword_to_idstr(&keyword_idstr, start_commit,
4535 if (keyword_idstr != NULL)
4536 start_commit = keyword_idstr;
4538 error = got_ref_open(&ref, repo, start_commit, 0);
4540 head_ref_name = got_ref_get_name(ref);
4541 else if (error->code != GOT_ERR_NOT_REF)
4543 error = got_repo_match_object_id(&start_id, NULL,
4544 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4549 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4551 error = got_error_from_errno("view_open");
4554 error = open_log_view(view, start_id, repo, head_ref_name,
4555 in_repo_path, log_branches);
4560 error = set_tog_base_commit(repo, worktree);
4564 /* Release work tree lock. */
4565 got_worktree_close(worktree);
4569 error = view_loop(view);
4572 free(tog_base_commit.id);
4573 free(keyword_idstr);
4582 const struct got_error *close_err = got_repo_close(repo);
4587 got_worktree_close(worktree);
4589 const struct got_error *pack_err =
4590 got_repo_pack_fds_close(pack_fds);
4602 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4603 "object1 object2\n", getprogname());
4608 match_line(const char *line, regex_t *regex, size_t nmatch,
4609 regmatch_t *regmatch)
4611 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4614 static struct tog_color *
4615 match_color(struct tog_colors *colors, const char *line)
4617 struct tog_color *tc = NULL;
4619 STAILQ_FOREACH(tc, colors, entry) {
4620 if (match_line(line, &tc->regex, 0, NULL))
4627 static const struct got_error *
4628 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4629 WINDOW *window, int skipcol, regmatch_t *regmatch)
4631 const struct got_error *err = NULL;
4633 wchar_t *wline = NULL;
4634 int rme, rms, n, width, scrollx;
4635 int width0 = 0, width1 = 0, width2 = 0;
4636 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4640 rms = regmatch->rm_so;
4641 rme = regmatch->rm_eo;
4643 err = expand_tab(&exstr, line);
4647 /* Split the line into 3 segments, according to match offsets. */
4648 seg0 = strndup(exstr, rms);
4650 err = got_error_from_errno("strndup");
4653 seg1 = strndup(exstr + rms, rme - rms);
4655 err = got_error_from_errno("strndup");
4658 seg2 = strdup(exstr + rme);
4660 err = got_error_from_errno("strndup");
4664 /* draw up to matched token if we haven't scrolled past it */
4665 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4669 n = MAX(width0 - skipcol, 0);
4672 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4673 wlimit, col_tab_align, 1);
4676 waddwstr(window, &wline[scrollx]);
4686 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4690 wlen = wcslen(wline);
4692 width = wcwidth(wline[i]);
4694 /* should not happen, tabs are expanded */
4695 err = got_error(GOT_ERR_RANGE);
4698 if (width0 + w + width > skipcol)
4703 /* draw (visible part of) matched token (if scrolled into it) */
4704 if (width1 - w > 0) {
4705 wattron(window, A_STANDOUT);
4706 waddwstr(window, &wline[i]);
4707 wattroff(window, A_STANDOUT);
4708 wlimit -= (width1 - w);
4709 *wtotal += (width1 - w);
4713 if (wlimit > 0) { /* draw rest of line */
4715 if (skipcol > width0 + width1) {
4716 err = format_line(&wline, &width2, &scrollx, seg2,
4717 skipcol - (width0 + width1), wlimit,
4721 waddwstr(window, &wline[scrollx]);
4723 err = format_line(&wline, &width2, NULL, seg2, 0,
4724 wlimit, col_tab_align, 1);
4727 waddwstr(window, wline);
4741 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4744 int *eof, *first, *selected;
4746 if (view->type == TOG_VIEW_DIFF) {
4747 struct tog_diff_view_state *s = &view->state.diff;
4749 first = &s->first_displayed_line;
4753 } else if (view->type == TOG_VIEW_HELP) {
4754 struct tog_help_view_state *s = &view->state.help;
4756 first = &s->first_displayed_line;
4760 } else if (view->type == TOG_VIEW_BLAME) {
4761 struct tog_blame_view_state *s = &view->state.blame;
4763 first = &s->first_displayed_line;
4764 selected = &s->selected_line;
4770 /* Center gline in the middle of the page like vi(1). */
4771 if (*lineno < view->gline - (view->nlines - 3) / 2)
4773 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4782 *selected = view->gline <= (view->nlines - 3) / 2 ?
4783 view->gline : (view->nlines - 3) / 2 + 1;
4789 static const struct got_error *
4790 draw_file(struct tog_view *view, const char *header)
4792 struct tog_diff_view_state *s = &view->state.diff;
4793 regmatch_t *regmatch = &view->regmatch;
4794 const struct got_error *err;
4797 size_t linesize = 0;
4801 int max_lines = view->nlines;
4802 int nlines = s->nlines;
4805 s->lineno = s->first_displayed_line - 1;
4806 line_offset = s->lines[s->first_displayed_line - 1].offset;
4807 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4808 return got_error_from_errno("fseek");
4810 werase(view->window);
4812 if (view->gline > s->nlines - 1)
4813 view->gline = s->nlines - 1;
4816 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4817 1 : view->gline - (view->nlines - 3) / 2 :
4818 s->lineno + s->selected_line;
4820 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4821 return got_error_from_errno("asprintf");
4822 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4828 if (view_needs_focus_indication(view))
4829 wstandout(view->window);
4830 waddwstr(view->window, wline);
4833 while (width++ < view->ncols)
4834 waddch(view->window, ' ');
4835 if (view_needs_focus_indication(view))
4836 wstandend(view->window);
4846 while (max_lines > 0 && nprinted < max_lines) {
4847 enum got_diff_line_type linetype;
4850 linelen = getline(&line, &linesize, s->f);
4851 if (linelen == -1) {
4857 return got_ferror(s->f, GOT_ERR_IO);
4860 if (++s->lineno < s->first_displayed_line)
4862 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4864 if (s->lineno == view->hiline)
4867 /* Set view->maxx based on full line length. */
4868 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4874 view->maxx = MAX(view->maxx, width);
4878 linetype = s->lines[s->lineno].type;
4879 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4880 linetype < GOT_DIFF_LINE_CONTEXT)
4881 attr |= COLOR_PAIR(linetype);
4883 wattron(view->window, attr);
4884 if (s->first_displayed_line + nprinted == s->matched_line &&
4885 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4886 err = add_matched_line(&width, line, view->ncols, 0,
4887 view->window, view->x, regmatch);
4894 err = format_line(&wline, &width, &skip, line,
4895 view->x, view->ncols, 0, view->x ? 1 : 0);
4900 waddwstr(view->window, &wline[skip]);
4904 if (s->lineno == view->hiline) {
4905 /* highlight full gline length */
4906 while (width++ < view->ncols)
4907 waddch(view->window, ' ');
4909 if (width <= view->ncols - 1)
4910 waddch(view->window, '\n');
4913 wattroff(view->window, attr);
4914 if (++nprinted == 1)
4915 s->first_displayed_line = s->lineno;
4919 s->last_displayed_line = s->first_displayed_line +
4922 s->last_displayed_line = s->first_displayed_line;
4927 while (nprinted < view->nlines) {
4928 waddch(view->window, '\n');
4932 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4938 wstandout(view->window);
4939 waddwstr(view->window, wline);
4942 wstandend(view->window);
4949 get_datestr(time_t *time, char *datebuf)
4951 struct tm mytm, *tm;
4954 tm = gmtime_r(time, &mytm);
4957 s = asctime_r(tm, datebuf);
4960 p = strchr(s, '\n');
4966 static const struct got_error *
4967 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4968 off_t off, uint8_t type)
4970 struct got_diff_line *p;
4972 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4974 return got_error_from_errno("reallocarray");
4976 (*lines)[*nlines].offset = off;
4977 (*lines)[*nlines].type = type;
4983 static const struct got_error *
4984 cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4985 struct got_diff_line *s_lines, size_t s_nlines)
4987 struct got_diff_line *p;
4991 if (fseeko(src, 0L, SEEK_SET) == -1)
4992 return got_error_from_errno("fseeko");
4995 r = fread(buf, 1, sizeof(buf), src);
4998 return got_error_from_errno("fread");
5002 if (fwrite(buf, 1, r, dst) != r)
5003 return got_ferror(dst, GOT_ERR_IO);
5006 if (s_nlines == 0 && *d_nlines == 0)
5010 * If commit info was in dst, increment line offsets
5011 * of the appended diff content, but skip s_lines[0]
5012 * because offset zero is already in *d_lines.
5014 if (*d_nlines > 0) {
5015 for (i = 1; i < s_nlines; ++i)
5016 s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
5024 p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
5026 /* d_lines is freed in close_diff_view() */
5027 return got_error_from_errno("reallocarray");
5032 memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
5033 *d_nlines += s_nlines;
5038 static const struct got_error *
5039 write_commit_info(struct got_diff_line **lines, size_t *nlines,
5040 struct got_object_id *commit_id, struct got_reflist_head *refs,
5041 struct got_repository *repo, int ignore_ws, int force_text_diff,
5042 struct got_diffstat_cb_arg *dsa, FILE *outfile)
5044 const struct got_error *err = NULL;
5045 char datebuf[26], *datestr;
5046 struct got_commit_object *commit;
5047 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
5048 time_t committer_time;
5049 const char *author, *committer;
5050 char *refs_str = NULL;
5051 struct got_pathlist_entry *pe;
5055 err = build_refs_str(&refs_str, refs, commit_id, repo);
5059 err = got_object_open_as_commit(&commit, repo, commit_id);
5063 err = got_object_id_str(&id_str, commit_id);
5065 err = got_error_from_errno("got_object_id_str");
5069 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
5073 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
5074 refs_str ? refs_str : "", refs_str ? ")" : "");
5076 err = got_error_from_errno("fprintf");
5080 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
5084 n = fprintf(outfile, "from: %s\n",
5085 got_object_commit_get_author(commit));
5087 err = got_error_from_errno("fprintf");
5091 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
5095 author = got_object_commit_get_author(commit);
5096 committer = got_object_commit_get_committer(commit);
5097 if (strcmp(author, committer) != 0) {
5098 n = fprintf(outfile, "via: %s\n", committer);
5100 err = got_error_from_errno("fprintf");
5104 err = add_line_metadata(lines, nlines, outoff,
5105 GOT_DIFF_LINE_AUTHOR);
5109 committer_time = got_object_commit_get_committer_time(commit);
5110 datestr = get_datestr(&committer_time, datebuf);
5112 n = fprintf(outfile, "date: %s UTC\n", datestr);
5114 err = got_error_from_errno("fprintf");
5118 err = add_line_metadata(lines, nlines, outoff,
5119 GOT_DIFF_LINE_DATE);
5123 if (got_object_commit_get_nparents(commit) > 1) {
5124 const struct got_object_id_queue *parent_ids;
5125 struct got_object_qid *qid;
5127 parent_ids = got_object_commit_get_parent_ids(commit);
5128 STAILQ_FOREACH(qid, parent_ids, entry) {
5129 err = got_object_id_str(&id_str, &qid->id);
5132 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
5134 err = got_error_from_errno("fprintf");
5138 err = add_line_metadata(lines, nlines, outoff,
5139 GOT_DIFF_LINE_META);
5147 err = got_object_commit_get_logmsg(&logmsg, commit);
5151 while ((line = strsep(&s, "\n")) != NULL) {
5152 n = fprintf(outfile, "%s\n", line);
5154 err = got_error_from_errno("fprintf");
5158 err = add_line_metadata(lines, nlines, outoff,
5159 GOT_DIFF_LINE_LOGMSG);
5164 TAILQ_FOREACH(pe, dsa->paths, entry) {
5165 struct got_diff_changed_path *cp = pe->data;
5166 int pad = dsa->max_path_len - pe->path_len + 1;
5168 n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
5169 pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
5170 dsa->rm_cols + 1, cp->rm);
5172 err = got_error_from_errno("fprintf");
5176 err = add_line_metadata(lines, nlines, outoff,
5177 GOT_DIFF_LINE_CHANGES);
5182 fputc('\n', outfile);
5184 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5188 n = fprintf(outfile,
5189 "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5190 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5191 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5193 err = got_error_from_errno("fprintf");
5197 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5201 fputc('\n', outfile);
5203 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5208 got_object_commit_close(commit);
5217 static const struct got_error *
5218 create_diff(struct tog_diff_view_state *s)
5220 const struct got_error *err = NULL;
5221 FILE *f = NULL, *tmp_diff_file = NULL;
5223 struct got_diff_line *lines = NULL;
5224 struct got_pathlist_head changed_paths;
5226 TAILQ_INIT(&changed_paths);
5229 s->lines = malloc(sizeof(*s->lines));
5230 if (s->lines == NULL)
5231 return got_error_from_errno("malloc");
5236 err = got_error_from_errno("got_opentemp");
5239 tmp_diff_file = got_opentemp();
5240 if (tmp_diff_file == NULL) {
5241 err = got_error_from_errno("got_opentemp");
5244 if (s->f && fclose(s->f) == EOF) {
5245 err = got_error_from_errno("fclose");
5251 err = got_object_get_type(&obj_type, s->repo, s->id1);
5253 err = got_object_get_type(&obj_type, s->repo, s->id2);
5258 case GOT_OBJ_TYPE_BLOB:
5259 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5260 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5261 s->label1, s->label2, tog_diff_algo, s->diff_context,
5262 s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5265 case GOT_OBJ_TYPE_TREE:
5266 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5267 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5268 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5269 s->force_text_diff, NULL, s->repo, s->f);
5271 case GOT_OBJ_TYPE_COMMIT: {
5272 const struct got_object_id_queue *parent_ids;
5273 struct got_object_qid *pid;
5274 struct got_commit_object *commit2;
5275 struct got_reflist_head *refs;
5277 struct got_diffstat_cb_arg dsa = {
5280 s->ignore_whitespace,
5285 lines = malloc(sizeof(*lines));
5286 if (lines == NULL) {
5287 err = got_error_from_errno("malloc");
5291 /* build diff first in tmp file then append to commit info */
5292 err = got_diff_objects_as_commits(&lines, &nlines,
5293 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5294 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5295 s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5299 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
5302 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5303 /* Show commit info if we're diffing to a parent/root commit. */
5304 if (s->id1 == NULL) {
5305 err = write_commit_info(&s->lines, &s->nlines, s->id2,
5306 refs, s->repo, s->ignore_whitespace,
5307 s->force_text_diff, &dsa, s->f);
5311 parent_ids = got_object_commit_get_parent_ids(commit2);
5312 STAILQ_FOREACH(pid, parent_ids, entry) {
5313 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5314 err = write_commit_info(&s->lines,
5315 &s->nlines, s->id2, refs, s->repo,
5316 s->ignore_whitespace,
5317 s->force_text_diff, &dsa, s->f);
5324 got_object_commit_close(commit2);
5326 err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5331 err = got_error(GOT_ERR_OBJ_TYPE);
5336 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5337 if (s->f && fflush(s->f) != 0 && err == NULL)
5338 err = got_error_from_errno("fflush");
5339 if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5340 err = got_error_from_errno("fclose");
5345 diff_view_indicate_progress(struct tog_view *view)
5347 mvwaddstr(view->window, 0, 0, "diffing...");
5352 static const struct got_error *
5353 search_start_diff_view(struct tog_view *view)
5355 struct tog_diff_view_state *s = &view->state.diff;
5357 s->matched_line = 0;
5362 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5363 size_t *nlines, int **first, int **last, int **match, int **selected)
5365 struct tog_diff_view_state *s = &view->state.diff;
5368 *nlines = s->nlines;
5369 *line_offsets = NULL;
5370 *match = &s->matched_line;
5371 *first = &s->first_displayed_line;
5372 *last = &s->last_displayed_line;
5373 *selected = &s->selected_line;
5376 static const struct got_error *
5377 search_next_view_match(struct tog_view *view)
5379 const struct got_error *err = NULL;
5383 size_t linesize = 0;
5385 off_t *line_offsets;
5387 int *first, *last, *match, *selected;
5389 if (!view->search_setup)
5390 return got_error_msg(GOT_ERR_NOT_IMPL,
5391 "view search not supported");
5392 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5395 if (!view->searching) {
5396 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5401 if (view->searching == TOG_SEARCH_FORWARD)
5402 lineno = *first + 1;
5404 lineno = *first - 1;
5406 lineno = *first - 1 + *selected;
5411 if (lineno <= 0 || lineno > nlines) {
5413 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5417 if (view->searching == TOG_SEARCH_FORWARD)
5423 offset = view->type == TOG_VIEW_DIFF ?
5424 view->state.diff.lines[lineno - 1].offset :
5425 line_offsets[lineno - 1];
5426 if (fseeko(f, offset, SEEK_SET) != 0) {
5428 return got_error_from_errno("fseeko");
5430 linelen = getline(&line, &linesize, f);
5431 if (linelen != -1) {
5433 err = expand_tab(&exstr, line);
5436 if (match_line(exstr, &view->regex, 1,
5438 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5445 if (view->searching == TOG_SEARCH_FORWARD)
5460 static const struct got_error *
5461 close_diff_view(struct tog_view *view)
5463 const struct got_error *err = NULL;
5464 struct tog_diff_view_state *s = &view->state.diff;
5470 if (s->f && fclose(s->f) == EOF)
5471 err = got_error_from_errno("fclose");
5473 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5474 err = got_error_from_errno("fclose");
5476 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5477 err = got_error_from_errno("fclose");
5479 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5480 err = got_error_from_errno("close");
5482 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5483 err = got_error_from_errno("close");
5491 static const struct got_error *
5492 open_diff_view(struct tog_view *view, struct got_object_id *id1,
5493 struct got_object_id *id2, const char *label1, const char *label2,
5494 int diff_context, int ignore_whitespace, int force_text_diff,
5495 struct tog_view *parent_view, struct got_repository *repo)
5497 const struct got_error *err;
5498 struct tog_diff_view_state *s = &view->state.diff;
5500 memset(s, 0, sizeof(*s));
5504 if (id1 != NULL && id2 != NULL) {
5507 err = got_object_get_type(&type1, repo, id1);
5510 err = got_object_get_type(&type2, repo, id2);
5514 if (type1 != type2) {
5515 err = got_error(GOT_ERR_OBJ_TYPE);
5519 s->first_displayed_line = 1;
5520 s->last_displayed_line = view->nlines;
5521 s->selected_line = 1;
5529 s->id1 = got_object_id_dup(id1);
5530 if (s->id1 == NULL) {
5531 err = got_error_from_errno("got_object_id_dup");
5537 s->id2 = got_object_id_dup(id2);
5538 if (s->id2 == NULL) {
5539 err = got_error_from_errno("got_object_id_dup");
5543 s->f1 = got_opentemp();
5544 if (s->f1 == NULL) {
5545 err = got_error_from_errno("got_opentemp");
5549 s->f2 = got_opentemp();
5550 if (s->f2 == NULL) {
5551 err = got_error_from_errno("got_opentemp");
5555 s->fd1 = got_opentempfd();
5557 err = got_error_from_errno("got_opentempfd");
5561 s->fd2 = got_opentempfd();
5563 err = got_error_from_errno("got_opentempfd");
5567 s->diff_context = diff_context;
5568 s->ignore_whitespace = ignore_whitespace;
5569 s->force_text_diff = force_text_diff;
5570 s->parent_view = parent_view;
5573 if (has_colors() && getenv("TOG_COLORS") != NULL && !using_mock_io) {
5576 rc = init_pair(GOT_DIFF_LINE_MINUS,
5577 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5579 rc = init_pair(GOT_DIFF_LINE_PLUS,
5580 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5582 rc = init_pair(GOT_DIFF_LINE_HUNK,
5583 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5585 rc = init_pair(GOT_DIFF_LINE_META,
5586 get_color_value("TOG_COLOR_DIFF_META"), -1);
5588 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5589 get_color_value("TOG_COLOR_DIFF_META"), -1);
5591 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5592 get_color_value("TOG_COLOR_DIFF_META"), -1);
5594 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5595 get_color_value("TOG_COLOR_DIFF_META"), -1);
5597 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5598 get_color_value("TOG_COLOR_AUTHOR"), -1);
5600 rc = init_pair(GOT_DIFF_LINE_DATE,
5601 get_color_value("TOG_COLOR_DATE"), -1);
5603 err = got_error(GOT_ERR_RANGE);
5608 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5609 view_is_splitscreen(view))
5610 show_log_view(parent_view); /* draw border */
5611 diff_view_indicate_progress(view);
5613 err = create_diff(s);
5615 view->show = show_diff_view;
5616 view->input = input_diff_view;
5617 view->reset = reset_diff_view;
5618 view->close = close_diff_view;
5619 view->search_start = search_start_diff_view;
5620 view->search_setup = search_setup_diff_view;
5621 view->search_next = search_next_view_match;
5624 if (view->close == NULL)
5625 close_diff_view(view);
5631 static const struct got_error *
5632 show_diff_view(struct tog_view *view)
5634 const struct got_error *err;
5635 struct tog_diff_view_state *s = &view->state.diff;
5636 char *id_str1 = NULL, *id_str2, *header;
5637 const char *label1, *label2;
5640 err = got_object_id_str(&id_str1, s->id1);
5643 label1 = s->label1 ? s->label1 : id_str1;
5645 label1 = "/dev/null";
5647 err = got_object_id_str(&id_str2, s->id2);
5650 label2 = s->label2 ? s->label2 : id_str2;
5652 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5653 err = got_error_from_errno("asprintf");
5661 err = draw_file(view, header);
5666 static const struct got_error *
5667 set_selected_commit(struct tog_diff_view_state *s,
5668 struct commit_queue_entry *entry)
5670 const struct got_error *err;
5671 const struct got_object_id_queue *parent_ids;
5672 struct got_commit_object *selected_commit;
5673 struct got_object_qid *pid;
5676 s->id2 = got_object_id_dup(entry->id);
5678 return got_error_from_errno("got_object_id_dup");
5680 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5683 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5685 pid = STAILQ_FIRST(parent_ids);
5686 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5687 got_object_commit_close(selected_commit);
5691 static const struct got_error *
5692 reset_diff_view(struct tog_view *view)
5694 struct tog_diff_view_state *s = &view->state.diff;
5697 wclear(view->window);
5698 s->first_displayed_line = 1;
5699 s->last_displayed_line = view->nlines;
5700 s->matched_line = 0;
5701 diff_view_indicate_progress(view);
5702 return create_diff(s);
5706 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5710 i = start = s->first_displayed_line - 1;
5712 while (s->lines[i].type != type) {
5716 return; /* do nothing, requested type not in file */
5719 s->selected_line = 1;
5720 s->first_displayed_line = i;
5724 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5728 i = start = s->first_displayed_line + 1;
5730 while (s->lines[i].type != type) {
5731 if (i == s->nlines - 1)
5734 return; /* do nothing, requested type not in file */
5737 s->selected_line = 1;
5738 s->first_displayed_line = i;
5741 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5743 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5746 static const struct got_error *
5747 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5749 const struct got_error *err = NULL;
5750 struct tog_diff_view_state *s = &view->state.diff;
5751 struct tog_log_view_state *ls;
5752 struct commit_queue_entry *old_selected_entry;
5754 size_t linesize = 0;
5756 int i, nscroll = view->nlines - 1, up = 0;
5758 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5767 horizontal_scroll_input(view, ch);
5772 s->force_text_diff = !s->force_text_diff;
5773 view->action = s->force_text_diff ?
5774 "force ASCII text enabled" :
5775 "force ASCII text disabled";
5777 else if (ch == 'w') {
5778 s->ignore_whitespace = !s->ignore_whitespace;
5779 view->action = s->ignore_whitespace ?
5780 "ignore whitespace enabled" :
5781 "ignore whitespace disabled";
5783 err = reset_diff_view(view);
5787 s->first_displayed_line = 1;
5796 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5802 if (s->first_displayed_line > 1)
5803 s->first_displayed_line--;
5814 if (s->first_displayed_line == 1) {
5819 while (i++ < nscroll && s->first_displayed_line > 1)
5820 s->first_displayed_line--;
5826 s->first_displayed_line++;
5843 while (!s->eof && i++ < nscroll) {
5844 linelen = getline(&line, &linesize, s->f);
5845 s->first_displayed_line++;
5846 if (linelen == -1) {
5850 err = got_ferror(s->f, GOT_ERR_IO);
5857 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5860 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5863 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5866 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5869 if (s->diff_context > 0) {
5871 s->matched_line = 0;
5872 diff_view_indicate_progress(view);
5873 err = create_diff(s);
5874 if (s->first_displayed_line + view->nlines - 1 >
5876 s->first_displayed_line = 1;
5877 s->last_displayed_line = view->nlines;
5883 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5885 s->matched_line = 0;
5886 diff_view_indicate_progress(view);
5887 err = create_diff(s);
5899 if (s->parent_view == NULL) {
5903 s->parent_view->count = view->count;
5905 if (s->parent_view->type == TOG_VIEW_LOG) {
5906 ls = &s->parent_view->state.log;
5907 old_selected_entry = ls->selected_entry;
5909 err = input_log_view(NULL, s->parent_view,
5910 up ? KEY_UP : KEY_DOWN);
5913 view->count = s->parent_view->count;
5915 if (old_selected_entry == ls->selected_entry)
5918 err = set_selected_commit(s, ls->selected_entry);
5921 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5922 struct tog_blame_view_state *bs;
5923 struct got_object_id *id, *prev_id;
5925 bs = &s->parent_view->state.blame;
5926 prev_id = get_annotation_for_line(bs->blame.lines,
5927 bs->blame.nlines, bs->last_diffed_line);
5929 err = input_blame_view(&view, s->parent_view,
5930 up ? KEY_UP : KEY_DOWN);
5933 view->count = s->parent_view->count;
5935 if (prev_id == NULL)
5937 id = get_selected_commit_id(bs->blame.lines,
5938 bs->blame.nlines, bs->first_displayed_line,
5943 if (!got_object_id_cmp(prev_id, id))
5946 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5950 s->first_displayed_line = 1;
5951 s->last_displayed_line = view->nlines;
5952 s->matched_line = 0;
5955 diff_view_indicate_progress(view);
5956 err = create_diff(s);
5966 static const struct got_error *
5967 cmd_diff(int argc, char *argv[])
5969 const struct got_error *error;
5970 struct got_repository *repo = NULL;
5971 struct got_worktree *worktree = NULL;
5972 struct got_object_id *id1 = NULL, *id2 = NULL;
5973 char *repo_path = NULL, *cwd = NULL;
5974 char *id_str1 = NULL, *id_str2 = NULL;
5975 char *keyword_idstr1 = NULL, *keyword_idstr2 = NULL;
5976 char *label1 = NULL, *label2 = NULL;
5977 int diff_context = 3, ignore_whitespace = 0;
5978 int ch, force_text_diff = 0;
5980 struct tog_view *view;
5981 int *pack_fds = NULL;
5983 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5986 force_text_diff = 1;
5989 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5992 errx(1, "number of context lines is %s: %s",
5996 repo_path = realpath(optarg, NULL);
5997 if (repo_path == NULL)
5998 return got_error_from_errno2("realpath",
6000 got_path_strip_trailing_slashes(repo_path);
6003 ignore_whitespace = 1;
6015 usage_diff(); /* TODO show local worktree changes */
6016 } else if (argc == 2) {
6022 error = got_repo_pack_fds_open(&pack_fds);
6026 if (repo_path == NULL) {
6027 cwd = getcwd(NULL, 0);
6029 return got_error_from_errno("getcwd");
6030 error = got_worktree_open(&worktree, cwd, NULL);
6031 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6035 strdup(got_worktree_get_repo_path(worktree));
6037 repo_path = strdup(cwd);
6038 if (repo_path == NULL) {
6039 error = got_error_from_errno("strdup");
6044 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6050 error = apply_unveil(got_repo_get_path(repo), NULL);
6054 error = tog_load_refs(repo, 0);
6058 if (id_str1 != NULL) {
6059 error = got_keyword_to_idstr(&keyword_idstr1, id_str1,
6063 if (keyword_idstr1 != NULL)
6064 id_str1 = keyword_idstr1;
6066 if (id_str2 != NULL) {
6067 error = got_keyword_to_idstr(&keyword_idstr2, id_str2,
6071 if (keyword_idstr2 != NULL)
6072 id_str2 = keyword_idstr2;
6075 error = got_repo_match_object_id(&id1, &label1, id_str1,
6076 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
6080 error = got_repo_match_object_id(&id2, &label2, id_str2,
6081 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
6085 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
6087 error = got_error_from_errno("view_open");
6090 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
6091 ignore_whitespace, force_text_diff, NULL, repo);
6096 error = set_tog_base_commit(repo, worktree);
6101 error = view_loop(view);
6104 free(tog_base_commit.id);
6105 free(keyword_idstr1);
6106 free(keyword_idstr2);
6112 const struct got_error *close_err = got_repo_close(repo);
6117 got_worktree_close(worktree);
6119 const struct got_error *pack_err =
6120 got_repo_pack_fds_close(pack_fds);
6133 "usage: %s blame [-c commit] [-r repository-path] path\n",
6138 struct tog_blame_line {
6140 struct got_object_id *id;
6143 static const struct got_error *
6144 draw_blame(struct tog_view *view)
6146 struct tog_blame_view_state *s = &view->state.blame;
6147 struct tog_blame *blame = &s->blame;
6148 regmatch_t *regmatch = &view->regmatch;
6149 const struct got_error *err;
6150 int lineno = 0, nprinted = 0;
6152 size_t linesize = 0;
6156 struct tog_blame_line *blame_line;
6157 struct got_object_id *prev_id = NULL;
6159 struct tog_color *tc;
6161 err = got_object_id_str(&id_str, &s->blamed_commit->id);
6166 werase(view->window);
6168 if (asprintf(&line, "commit %s", id_str) == -1) {
6169 err = got_error_from_errno("asprintf");
6174 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6179 if (view_needs_focus_indication(view))
6180 wstandout(view->window);
6181 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6183 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6184 waddwstr(view->window, wline);
6185 while (width++ < view->ncols)
6186 waddch(view->window, ' ');
6188 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6189 if (view_needs_focus_indication(view))
6190 wstandend(view->window);
6194 if (view->gline > blame->nlines)
6195 view->gline = blame->nlines;
6197 if (tog_io.wait_for_ui) {
6198 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6201 rc = pthread_cond_wait(&bta->blame_complete, &tog_mutex);
6203 return got_error_set_errno(rc, "pthread_cond_wait");
6204 tog_io.wait_for_ui = 0;
6207 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
6208 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
6209 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
6211 return got_error_from_errno("asprintf");
6214 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6219 waddwstr(view->window, wline);
6222 if (width < view->ncols - 1)
6223 waddch(view->window, '\n');
6227 while (nprinted < view->nlines - 2) {
6228 linelen = getline(&line, &linesize, blame->f);
6229 if (linelen == -1) {
6230 if (feof(blame->f)) {
6235 return got_ferror(blame->f, GOT_ERR_IO);
6237 if (++lineno < s->first_displayed_line)
6239 if (view->gline && !gotoline(view, &lineno, &nprinted))
6242 /* Set view->maxx based on full line length. */
6243 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6250 view->maxx = MAX(view->maxx, width);
6252 if (nprinted == s->selected_line - 1)
6253 wstandout(view->window);
6255 if (blame->nlines > 0) {
6256 blame_line = &blame->lines[lineno - 1];
6257 if (blame_line->annotated && prev_id &&
6258 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6259 !(nprinted == s->selected_line - 1)) {
6260 waddstr(view->window, " ");
6261 } else if (blame_line->annotated) {
6263 err = got_object_id_str(&id_str,
6269 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6271 wattr_on(view->window,
6272 COLOR_PAIR(tc->colorpair), NULL);
6273 wprintw(view->window, "%.8s", id_str);
6275 wattr_off(view->window,
6276 COLOR_PAIR(tc->colorpair), NULL);
6278 prev_id = blame_line->id;
6280 waddstr(view->window, "........");
6284 waddstr(view->window, "........");
6288 if (nprinted == s->selected_line - 1)
6289 wstandend(view->window);
6290 waddstr(view->window, " ");
6292 if (view->ncols <= 9) {
6294 } else if (s->first_displayed_line + nprinted ==
6296 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6297 err = add_matched_line(&width, line, view->ncols - 9, 9,
6298 view->window, view->x, regmatch);
6306 err = format_line(&wline, &width, &skip, line,
6307 view->x, view->ncols - 9, 9, 1);
6312 waddwstr(view->window, &wline[skip]);
6318 if (width <= view->ncols - 1)
6319 waddch(view->window, '\n');
6320 if (++nprinted == 1)
6321 s->first_displayed_line = lineno;
6324 s->last_displayed_line = lineno;
6331 static const struct got_error *
6332 blame_cb(void *arg, int nlines, int lineno,
6333 struct got_commit_object *commit, struct got_object_id *id)
6335 const struct got_error *err = NULL;
6336 struct tog_blame_cb_args *a = arg;
6337 struct tog_blame_line *line;
6340 if (nlines != a->nlines ||
6341 (lineno != -1 && lineno < 1) || lineno > a->nlines)
6342 return got_error(GOT_ERR_RANGE);
6344 errcode = pthread_mutex_lock(&tog_mutex);
6346 return got_error_set_errno(errcode, "pthread_mutex_lock");
6348 if (*a->quit) { /* user has quit the blame view */
6349 err = got_error(GOT_ERR_ITER_COMPLETED);
6354 goto done; /* no change in this commit */
6356 line = &a->lines[lineno - 1];
6357 if (line->annotated)
6360 line->id = got_object_id_dup(id);
6361 if (line->id == NULL) {
6362 err = got_error_from_errno("got_object_id_dup");
6365 line->annotated = 1;
6367 errcode = pthread_mutex_unlock(&tog_mutex);
6369 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6374 blame_thread(void *arg)
6376 const struct got_error *err, *close_err;
6377 struct tog_blame_thread_args *ta = arg;
6378 struct tog_blame_cb_args *a = ta->cb_args;
6379 int errcode, fd1 = -1, fd2 = -1;
6380 FILE *f1 = NULL, *f2 = NULL;
6382 fd1 = got_opentempfd();
6384 return (void *)got_error_from_errno("got_opentempfd");
6386 fd2 = got_opentempfd();
6388 err = got_error_from_errno("got_opentempfd");
6392 f1 = got_opentemp();
6394 err = (void *)got_error_from_errno("got_opentemp");
6397 f2 = got_opentemp();
6399 err = (void *)got_error_from_errno("got_opentemp");
6403 err = block_signals_used_by_main_thread();
6407 err = got_blame(ta->path, a->commit_id, ta->repo,
6408 tog_diff_algo, blame_cb, ta->cb_args,
6409 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6410 if (err && err->code == GOT_ERR_CANCELLED)
6413 errcode = pthread_mutex_lock(&tog_mutex);
6415 err = got_error_set_errno(errcode, "pthread_mutex_lock");
6419 close_err = got_repo_close(ta->repo);
6425 if (tog_io.wait_for_ui) {
6426 errcode = pthread_cond_signal(&ta->blame_complete);
6427 if (errcode && err == NULL)
6428 err = got_error_set_errno(errcode,
6429 "pthread_cond_signal");
6432 errcode = pthread_mutex_unlock(&tog_mutex);
6433 if (errcode && err == NULL)
6434 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6437 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6438 err = got_error_from_errno("close");
6439 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6440 err = got_error_from_errno("close");
6441 if (f1 && fclose(f1) == EOF && err == NULL)
6442 err = got_error_from_errno("fclose");
6443 if (f2 && fclose(f2) == EOF && err == NULL)
6444 err = got_error_from_errno("fclose");
6449 static struct got_object_id *
6450 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6451 int first_displayed_line, int selected_line)
6453 struct tog_blame_line *line;
6458 line = &lines[first_displayed_line - 1 + selected_line - 1];
6459 if (!line->annotated)
6465 static struct got_object_id *
6466 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6469 struct tog_blame_line *line;
6471 if (nlines <= 0 || lineno >= nlines)
6474 line = &lines[lineno - 1];
6475 if (!line->annotated)
6481 static const struct got_error *
6482 stop_blame(struct tog_blame *blame)
6484 const struct got_error *err = NULL;
6487 if (blame->thread) {
6489 errcode = pthread_mutex_unlock(&tog_mutex);
6491 return got_error_set_errno(errcode,
6492 "pthread_mutex_unlock");
6493 errcode = pthread_join(blame->thread, (void **)&err);
6495 return got_error_set_errno(errcode, "pthread_join");
6496 errcode = pthread_mutex_lock(&tog_mutex);
6498 return got_error_set_errno(errcode,
6499 "pthread_mutex_lock");
6500 if (err && err->code == GOT_ERR_ITER_COMPLETED)
6502 blame->thread = NULL;
6504 if (blame->thread_args.repo) {
6505 const struct got_error *close_err;
6506 close_err = got_repo_close(blame->thread_args.repo);
6509 blame->thread_args.repo = NULL;
6512 if (fclose(blame->f) == EOF && err == NULL)
6513 err = got_error_from_errno("fclose");
6517 for (i = 0; i < blame->nlines; i++)
6518 free(blame->lines[i].id);
6520 blame->lines = NULL;
6522 free(blame->cb_args.commit_id);
6523 blame->cb_args.commit_id = NULL;
6524 if (blame->pack_fds) {
6525 const struct got_error *pack_err =
6526 got_repo_pack_fds_close(blame->pack_fds);
6529 blame->pack_fds = NULL;
6534 static const struct got_error *
6535 cancel_blame_view(void *arg)
6537 const struct got_error *err = NULL;
6541 errcode = pthread_mutex_lock(&tog_mutex);
6543 return got_error_set_errno(errcode,
6544 "pthread_mutex_unlock");
6547 err = got_error(GOT_ERR_CANCELLED);
6549 errcode = pthread_mutex_unlock(&tog_mutex);
6551 return got_error_set_errno(errcode,
6552 "pthread_mutex_lock");
6557 static const struct got_error *
6558 run_blame(struct tog_view *view)
6560 struct tog_blame_view_state *s = &view->state.blame;
6561 struct tog_blame *blame = &s->blame;
6562 const struct got_error *err = NULL;
6563 struct got_commit_object *commit = NULL;
6564 struct got_blob_object *blob = NULL;
6565 struct got_repository *thread_repo = NULL;
6566 struct got_object_id *obj_id = NULL;
6567 int obj_type, fd = -1;
6568 int *pack_fds = NULL;
6570 err = got_object_open_as_commit(&commit, s->repo,
6571 &s->blamed_commit->id);
6575 fd = got_opentempfd();
6577 err = got_error_from_errno("got_opentempfd");
6581 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6585 err = got_object_get_type(&obj_type, s->repo, obj_id);
6589 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6590 err = got_error(GOT_ERR_OBJ_TYPE);
6594 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6597 blame->f = got_opentemp();
6598 if (blame->f == NULL) {
6599 err = got_error_from_errno("got_opentemp");
6602 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6603 &blame->line_offsets, blame->f, blob);
6606 if (blame->nlines == 0) {
6607 s->blame_complete = 1;
6611 /* Don't include \n at EOF in the blame line count. */
6612 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6615 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6616 if (blame->lines == NULL) {
6617 err = got_error_from_errno("calloc");
6621 err = got_repo_pack_fds_open(&pack_fds);
6624 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6629 blame->pack_fds = pack_fds;
6630 blame->cb_args.view = view;
6631 blame->cb_args.lines = blame->lines;
6632 blame->cb_args.nlines = blame->nlines;
6633 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6634 if (blame->cb_args.commit_id == NULL) {
6635 err = got_error_from_errno("got_object_id_dup");
6638 blame->cb_args.quit = &s->done;
6640 blame->thread_args.path = s->path;
6641 blame->thread_args.repo = thread_repo;
6642 blame->thread_args.cb_args = &blame->cb_args;
6643 blame->thread_args.complete = &s->blame_complete;
6644 blame->thread_args.cancel_cb = cancel_blame_view;
6645 blame->thread_args.cancel_arg = &s->done;
6646 s->blame_complete = 0;
6648 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6649 s->first_displayed_line = 1;
6650 s->last_displayed_line = view->nlines;
6651 s->selected_line = 1;
6653 s->matched_line = 0;
6657 got_object_commit_close(commit);
6658 if (fd != -1 && close(fd) == -1 && err == NULL)
6659 err = got_error_from_errno("close");
6661 got_object_blob_close(blob);
6668 static const struct got_error *
6669 open_blame_view(struct tog_view *view, char *path,
6670 struct got_object_id *commit_id, struct got_repository *repo)
6672 const struct got_error *err = NULL;
6673 struct tog_blame_view_state *s = &view->state.blame;
6675 STAILQ_INIT(&s->blamed_commits);
6677 s->path = strdup(path);
6678 if (s->path == NULL)
6679 return got_error_from_errno("strdup");
6681 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6687 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6688 s->first_displayed_line = 1;
6689 s->last_displayed_line = view->nlines;
6690 s->selected_line = 1;
6691 s->blame_complete = 0;
6693 s->commit_id = commit_id;
6694 memset(&s->blame, 0, sizeof(s->blame));
6696 STAILQ_INIT(&s->colors);
6697 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6698 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6699 get_color_value("TOG_COLOR_COMMIT"));
6704 view->show = show_blame_view;
6705 view->input = input_blame_view;
6706 view->reset = reset_blame_view;
6707 view->close = close_blame_view;
6708 view->search_start = search_start_blame_view;
6709 view->search_setup = search_setup_blame_view;
6710 view->search_next = search_next_view_match;
6712 if (using_mock_io) {
6713 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6716 rc = pthread_cond_init(&bta->blame_complete, NULL);
6718 return got_error_set_errno(rc, "pthread_cond_init");
6721 return run_blame(view);
6724 static const struct got_error *
6725 close_blame_view(struct tog_view *view)
6727 const struct got_error *err = NULL;
6728 struct tog_blame_view_state *s = &view->state.blame;
6730 if (s->blame.thread)
6731 err = stop_blame(&s->blame);
6733 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6734 struct got_object_qid *blamed_commit;
6735 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6736 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6737 got_object_qid_free(blamed_commit);
6740 if (using_mock_io) {
6741 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6744 rc = pthread_cond_destroy(&bta->blame_complete);
6745 if (rc && err == NULL)
6746 err = got_error_set_errno(rc, "pthread_cond_destroy");
6750 free_colors(&s->colors);
6754 static const struct got_error *
6755 search_start_blame_view(struct tog_view *view)
6757 struct tog_blame_view_state *s = &view->state.blame;
6759 s->matched_line = 0;
6764 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6765 size_t *nlines, int **first, int **last, int **match, int **selected)
6767 struct tog_blame_view_state *s = &view->state.blame;
6770 *nlines = s->blame.nlines;
6771 *line_offsets = s->blame.line_offsets;
6772 *match = &s->matched_line;
6773 *first = &s->first_displayed_line;
6774 *last = &s->last_displayed_line;
6775 *selected = &s->selected_line;
6778 static const struct got_error *
6779 show_blame_view(struct tog_view *view)
6781 const struct got_error *err = NULL;
6782 struct tog_blame_view_state *s = &view->state.blame;
6785 if (s->blame.thread == NULL && !s->blame_complete) {
6786 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6787 &s->blame.thread_args);
6789 return got_error_set_errno(errcode, "pthread_create");
6792 halfdelay(1); /* fast refresh while annotating */
6795 if (s->blame_complete && !using_mock_io)
6796 halfdelay(10); /* disable fast refresh */
6798 err = draw_blame(view);
6804 static const struct got_error *
6805 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6806 struct got_repository *repo, struct got_object_id *id)
6808 struct tog_view *log_view;
6809 const struct got_error *err = NULL;
6813 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6814 if (log_view == NULL)
6815 return got_error_from_errno("view_open");
6817 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6819 view_close(log_view);
6821 *new_view = log_view;
6826 static const struct got_error *
6827 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6829 const struct got_error *err = NULL, *thread_err = NULL;
6830 struct tog_view *diff_view;
6831 struct tog_blame_view_state *s = &view->state.blame;
6832 int eos, nscroll, begin_y = 0, begin_x = 0;
6834 eos = nscroll = view->nlines - 2;
6835 if (view_is_hsplit_top(view))
6845 horizontal_scroll_input(view, ch);
6852 s->selected_line = 1;
6853 s->first_displayed_line = 1;
6858 if (s->blame.nlines < eos) {
6859 s->selected_line = s->blame.nlines;
6860 s->first_displayed_line = 1;
6862 s->selected_line = eos;
6863 s->first_displayed_line = s->blame.nlines - (eos - 1);
6870 if (s->selected_line > 1)
6872 else if (s->selected_line == 1 &&
6873 s->first_displayed_line > 1)
6874 s->first_displayed_line--;
6885 if (s->first_displayed_line == 1) {
6886 if (view->count > 1)
6888 s->selected_line = MAX(1, s->selected_line - nscroll);
6892 if (s->first_displayed_line > nscroll)
6893 s->first_displayed_line -= nscroll;
6895 s->first_displayed_line = 1;
6900 if (s->selected_line < eos && s->first_displayed_line +
6901 s->selected_line <= s->blame.nlines)
6903 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6904 s->first_displayed_line++;
6910 struct got_object_id *id = NULL;
6913 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6914 s->first_displayed_line, s->selected_line);
6918 struct got_commit_object *commit, *pcommit;
6919 struct got_object_qid *pid;
6920 struct got_object_id *blob_id = NULL;
6922 err = got_object_open_as_commit(&commit,
6927 got_object_commit_get_parent_ids(commit));
6929 got_object_commit_close(commit);
6932 /* Check if path history ends here. */
6933 err = got_object_open_as_commit(&pcommit,
6937 err = got_object_id_by_path(&blob_id, s->repo,
6939 got_object_commit_close(pcommit);
6941 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6943 got_object_commit_close(commit);
6946 err = got_object_get_type(&obj_type, s->repo,
6949 /* Can't blame non-blob type objects. */
6950 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6951 got_object_commit_close(commit);
6954 err = got_object_qid_alloc(&s->blamed_commit,
6956 got_object_commit_close(commit);
6958 if (got_object_id_cmp(id,
6959 &s->blamed_commit->id) == 0)
6961 err = got_object_qid_alloc(&s->blamed_commit,
6967 thread_err = stop_blame(&s->blame);
6971 STAILQ_INSERT_HEAD(&s->blamed_commits,
6972 s->blamed_commit, entry);
6973 err = run_blame(view);
6979 struct got_object_qid *first;
6982 first = STAILQ_FIRST(&s->blamed_commits);
6983 if (!got_object_id_cmp(&first->id, s->commit_id))
6986 thread_err = stop_blame(&s->blame);
6990 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6991 got_object_qid_free(s->blamed_commit);
6993 STAILQ_FIRST(&s->blamed_commits);
6994 err = run_blame(view);
7001 s->id_to_log = get_selected_commit_id(s->blame.lines,
7002 s->blame.nlines, s->first_displayed_line, s->selected_line);
7004 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7008 struct got_object_id *id = NULL;
7009 struct got_object_qid *pid;
7010 struct got_commit_object *commit = NULL;
7013 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
7014 s->first_displayed_line, s->selected_line);
7017 err = got_object_open_as_commit(&commit, s->repo, id);
7020 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7022 /* traversed from diff view, release diff resources */
7023 err = close_diff_view(*new_view);
7026 diff_view = *new_view;
7028 if (view_is_parent_view(view))
7029 view_get_split(view, &begin_y, &begin_x);
7031 diff_view = view_open(0, 0, begin_y, begin_x,
7033 if (diff_view == NULL) {
7034 got_object_commit_close(commit);
7035 err = got_error_from_errno("view_open");
7039 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
7040 id, NULL, NULL, 3, 0, 0, view, s->repo);
7041 got_object_commit_close(commit);
7044 s->last_diffed_line = s->first_displayed_line - 1 +
7047 break; /* still open from active diff view */
7048 if (view_is_parent_view(view) &&
7049 view->mode == TOG_VIEW_SPLIT_HRZN) {
7050 err = view_init_hsplit(view, begin_y);
7056 diff_view->focussed = 1;
7057 diff_view->mode = view->mode;
7058 diff_view->nlines = view->lines - begin_y;
7059 if (view_is_parent_view(view)) {
7060 view_transfer_size(diff_view, view);
7061 err = view_close_child(view);
7064 err = view_set_child(view, diff_view);
7067 view->focus_child = 1;
7069 *new_view = diff_view;
7082 if (s->last_displayed_line >= s->blame.nlines &&
7083 s->selected_line >= MIN(s->blame.nlines,
7084 view->nlines - 2)) {
7088 if (s->last_displayed_line >= s->blame.nlines &&
7089 s->selected_line < view->nlines - 2) {
7091 MIN(nscroll, s->last_displayed_line -
7092 s->first_displayed_line - s->selected_line + 1);
7094 if (s->last_displayed_line + nscroll <= s->blame.nlines)
7095 s->first_displayed_line += nscroll;
7097 s->first_displayed_line =
7098 s->blame.nlines - (view->nlines - 3);
7101 if (s->selected_line > view->nlines - 2) {
7102 s->selected_line = MIN(s->blame.nlines,
7110 return thread_err ? thread_err : err;
7113 static const struct got_error *
7114 reset_blame_view(struct tog_view *view)
7116 const struct got_error *err;
7117 struct tog_blame_view_state *s = &view->state.blame;
7121 err = stop_blame(&s->blame);
7125 return run_blame(view);
7128 static const struct got_error *
7129 cmd_blame(int argc, char *argv[])
7131 const struct got_error *error;
7132 struct got_repository *repo = NULL;
7133 struct got_worktree *worktree = NULL;
7134 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7135 char *link_target = NULL;
7136 struct got_object_id *commit_id = NULL;
7137 struct got_commit_object *commit = NULL;
7138 char *keyword_idstr = NULL, *commit_id_str = NULL;
7140 struct tog_view *view = NULL;
7141 int *pack_fds = NULL;
7143 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7146 commit_id_str = optarg;
7149 repo_path = realpath(optarg, NULL);
7150 if (repo_path == NULL)
7151 return got_error_from_errno2("realpath",
7166 error = got_repo_pack_fds_open(&pack_fds);
7170 if (repo_path == NULL) {
7171 cwd = getcwd(NULL, 0);
7173 return got_error_from_errno("getcwd");
7174 error = got_worktree_open(&worktree, cwd, NULL);
7175 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7179 strdup(got_worktree_get_repo_path(worktree));
7181 repo_path = strdup(cwd);
7182 if (repo_path == NULL) {
7183 error = got_error_from_errno("strdup");
7188 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7192 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
7199 error = apply_unveil(got_repo_get_path(repo), NULL);
7203 error = tog_load_refs(repo, 0);
7207 if (commit_id_str == NULL) {
7208 struct got_reference *head_ref;
7209 error = got_ref_open(&head_ref, repo, worktree ?
7210 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
7213 error = got_ref_resolve(&commit_id, repo, head_ref);
7214 got_ref_close(head_ref);
7216 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
7220 if (keyword_idstr != NULL)
7221 commit_id_str = keyword_idstr;
7223 error = got_repo_match_object_id(&commit_id, NULL,
7224 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7229 error = got_object_open_as_commit(&commit, repo, commit_id);
7233 error = got_object_resolve_symlinks(&link_target, in_repo_path,
7238 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
7240 error = got_error_from_errno("view_open");
7243 error = open_blame_view(view, link_target ? link_target : in_repo_path,
7245 if (error != NULL) {
7246 if (view->close == NULL)
7247 close_blame_view(view);
7253 error = set_tog_base_commit(repo, worktree);
7257 /* Release work tree lock. */
7258 got_worktree_close(worktree);
7262 error = view_loop(view);
7265 free(tog_base_commit.id);
7271 free(keyword_idstr);
7273 got_object_commit_close(commit);
7275 got_worktree_close(worktree);
7277 const struct got_error *close_err = got_repo_close(repo);
7282 const struct got_error *pack_err =
7283 got_repo_pack_fds_close(pack_fds);
7291 static const struct got_error *
7292 draw_tree_entries(struct tog_view *view, const char *parent_path)
7294 struct tog_tree_view_state *s = &view->state.tree;
7295 const struct got_error *err = NULL;
7296 struct got_tree_entry *te;
7299 struct tog_color *tc;
7300 int width, n, nentries, scrollx, i = 1;
7301 int limit = view->nlines;
7304 if (view_is_hsplit_top(view))
7305 --limit; /* border */
7307 werase(view->window);
7312 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7316 if (view_needs_focus_indication(view))
7317 wstandout(view->window);
7318 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7320 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7321 waddwstr(view->window, wline);
7324 while (width++ < view->ncols)
7325 waddch(view->window, ' ');
7327 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7328 if (view_needs_focus_indication(view))
7329 wstandend(view->window);
7334 if (s->first_displayed_entry) {
7335 i += got_tree_entry_get_index(s->first_displayed_entry);
7336 if (s->tree != s->root)
7337 ++i; /* account for ".." entry */
7339 nentries = got_object_tree_get_nentries(s->tree);
7340 if (asprintf(&index, "[%d/%d] %s",
7341 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7342 return got_error_from_errno("asprintf");
7343 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7347 waddwstr(view->window, wline);
7350 if (width < view->ncols - 1)
7351 waddch(view->window, '\n');
7354 waddch(view->window, '\n');
7358 if (s->first_displayed_entry == NULL) {
7359 te = got_object_tree_get_first_entry(s->tree);
7360 if (s->selected == 0) {
7362 wstandout(view->window);
7363 s->selected_entry = NULL;
7365 waddstr(view->window, " ..\n"); /* parent directory */
7366 if (s->selected == 0 && view->focussed)
7367 wstandend(view->window);
7374 te = s->first_displayed_entry;
7378 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7379 char *line = NULL, *id_str = NULL, *link_target = NULL;
7380 const char *modestr = "";
7383 te = got_object_tree_get_entry(s->tree, i);
7384 mode = got_tree_entry_get_mode(te);
7387 err = got_object_id_str(&id_str,
7388 got_tree_entry_get_id(te));
7390 return got_error_from_errno(
7391 "got_object_id_str");
7393 if (got_object_tree_entry_is_submodule(te))
7395 else if (S_ISLNK(mode)) {
7398 err = got_tree_entry_get_symlink_target(&link_target,
7404 for (i = 0; link_target[i] != '\0'; i++) {
7405 if (!isprint((unsigned char)link_target[i]))
7406 link_target[i] = '?';
7410 else if (S_ISDIR(mode))
7412 else if (mode & S_IXUSR)
7414 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7415 got_tree_entry_get_name(te), modestr,
7416 link_target ? " -> ": "",
7417 link_target ? link_target : "") == -1) {
7420 return got_error_from_errno("asprintf");
7425 /* use full line width to determine view->maxx */
7426 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7431 view->maxx = MAX(view->maxx, width);
7435 err = format_line(&wline, &width, &scrollx, line, view->x,
7441 if (n == s->selected) {
7443 wstandout(view->window);
7444 s->selected_entry = te;
7446 tc = match_color(&s->colors, line);
7448 wattr_on(view->window,
7449 COLOR_PAIR(tc->colorpair), NULL);
7450 waddwstr(view->window, &wline[scrollx]);
7452 wattr_off(view->window,
7453 COLOR_PAIR(tc->colorpair), NULL);
7454 if (width < view->ncols)
7455 waddch(view->window, '\n');
7456 if (n == s->selected && view->focussed)
7457 wstandend(view->window);
7463 s->last_displayed_entry = te;
7472 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7474 struct got_tree_entry *te;
7475 int isroot = s->tree == s->root;
7478 if (s->first_displayed_entry == NULL)
7481 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7482 while (i++ < maxscroll) {
7485 s->first_displayed_entry = NULL;
7488 s->first_displayed_entry = te;
7489 te = got_tree_entry_get_prev(s->tree, te);
7493 static const struct got_error *
7494 tree_scroll_down(struct tog_view *view, int maxscroll)
7496 struct tog_tree_view_state *s = &view->state.tree;
7497 struct got_tree_entry *next, *last;
7500 if (s->first_displayed_entry)
7501 next = got_tree_entry_get_next(s->tree,
7502 s->first_displayed_entry);
7504 next = got_object_tree_get_first_entry(s->tree);
7506 last = s->last_displayed_entry;
7507 while (next && n++ < maxscroll) {
7509 s->last_displayed_entry = last;
7510 last = got_tree_entry_get_next(s->tree, last);
7512 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7513 s->first_displayed_entry = next;
7514 next = got_tree_entry_get_next(s->tree, next);
7521 static const struct got_error *
7522 tree_entry_path(char **path, struct tog_parent_trees *parents,
7523 struct got_tree_entry *te)
7525 const struct got_error *err = NULL;
7526 struct tog_parent_tree *pt;
7527 size_t len = 2; /* for leading slash and NUL */
7529 TAILQ_FOREACH(pt, parents, entry)
7530 len += strlen(got_tree_entry_get_name(pt->selected_entry))
7533 len += strlen(got_tree_entry_get_name(te));
7535 *path = calloc(1, len);
7537 return got_error_from_errno("calloc");
7540 pt = TAILQ_LAST(parents, tog_parent_trees);
7542 const char *name = got_tree_entry_get_name(pt->selected_entry);
7543 if (strlcat(*path, name, len) >= len) {
7544 err = got_error(GOT_ERR_NO_SPACE);
7547 if (strlcat(*path, "/", len) >= len) {
7548 err = got_error(GOT_ERR_NO_SPACE);
7551 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7554 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7555 err = got_error(GOT_ERR_NO_SPACE);
7567 static const struct got_error *
7568 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7569 struct got_tree_entry *te, struct tog_parent_trees *parents,
7570 struct got_object_id *commit_id, struct got_repository *repo)
7572 const struct got_error *err = NULL;
7574 struct tog_view *blame_view;
7578 err = tree_entry_path(&path, parents, te);
7582 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7583 if (blame_view == NULL) {
7584 err = got_error_from_errno("view_open");
7588 err = open_blame_view(blame_view, path, commit_id, repo);
7590 if (err->code == GOT_ERR_CANCELLED)
7592 view_close(blame_view);
7594 *new_view = blame_view;
7600 static const struct got_error *
7601 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7602 struct tog_tree_view_state *s)
7604 struct tog_view *log_view;
7605 const struct got_error *err = NULL;
7610 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7611 if (log_view == NULL)
7612 return got_error_from_errno("view_open");
7614 err = tree_entry_path(&path, &s->parents, s->selected_entry);
7618 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7621 view_close(log_view);
7623 *new_view = log_view;
7628 static const struct got_error *
7629 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7630 const char *head_ref_name, struct got_repository *repo)
7632 const struct got_error *err = NULL;
7633 char *commit_id_str = NULL;
7634 struct tog_tree_view_state *s = &view->state.tree;
7635 struct got_commit_object *commit = NULL;
7637 TAILQ_INIT(&s->parents);
7638 STAILQ_INIT(&s->colors);
7640 s->commit_id = got_object_id_dup(commit_id);
7641 if (s->commit_id == NULL) {
7642 err = got_error_from_errno("got_object_id_dup");
7646 err = got_object_open_as_commit(&commit, repo, commit_id);
7651 * The root is opened here and will be closed when the view is closed.
7652 * Any visited subtrees and their path-wise parents are opened and
7655 err = got_object_open_as_tree(&s->root, repo,
7656 got_object_commit_get_tree_id(commit));
7661 err = got_object_id_str(&commit_id_str, commit_id);
7665 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7666 err = got_error_from_errno("asprintf");
7670 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7671 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7672 if (head_ref_name) {
7673 s->head_ref_name = strdup(head_ref_name);
7674 if (s->head_ref_name == NULL) {
7675 err = got_error_from_errno("strdup");
7681 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7682 err = add_color(&s->colors, "\\$$",
7683 TOG_COLOR_TREE_SUBMODULE,
7684 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7687 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7688 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7691 err = add_color(&s->colors, "/$",
7692 TOG_COLOR_TREE_DIRECTORY,
7693 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7697 err = add_color(&s->colors, "\\*$",
7698 TOG_COLOR_TREE_EXECUTABLE,
7699 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7703 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7704 get_color_value("TOG_COLOR_COMMIT"));
7709 view->show = show_tree_view;
7710 view->input = input_tree_view;
7711 view->close = close_tree_view;
7712 view->search_start = search_start_tree_view;
7713 view->search_next = search_next_tree_view;
7715 free(commit_id_str);
7717 got_object_commit_close(commit);
7719 if (view->close == NULL)
7720 close_tree_view(view);
7726 static const struct got_error *
7727 close_tree_view(struct tog_view *view)
7729 struct tog_tree_view_state *s = &view->state.tree;
7731 free_colors(&s->colors);
7732 free(s->tree_label);
7733 s->tree_label = NULL;
7735 s->commit_id = NULL;
7736 free(s->head_ref_name);
7737 s->head_ref_name = NULL;
7738 while (!TAILQ_EMPTY(&s->parents)) {
7739 struct tog_parent_tree *parent;
7740 parent = TAILQ_FIRST(&s->parents);
7741 TAILQ_REMOVE(&s->parents, parent, entry);
7742 if (parent->tree != s->root)
7743 got_object_tree_close(parent->tree);
7747 if (s->tree != NULL && s->tree != s->root)
7748 got_object_tree_close(s->tree);
7750 got_object_tree_close(s->root);
7754 static const struct got_error *
7755 search_start_tree_view(struct tog_view *view)
7757 struct tog_tree_view_state *s = &view->state.tree;
7759 s->matched_entry = NULL;
7764 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7766 regmatch_t regmatch;
7768 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7772 static const struct got_error *
7773 search_next_tree_view(struct tog_view *view)
7775 struct tog_tree_view_state *s = &view->state.tree;
7776 struct got_tree_entry *te = NULL;
7778 if (!view->searching) {
7779 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7783 if (s->matched_entry) {
7784 if (view->searching == TOG_SEARCH_FORWARD) {
7785 if (s->selected_entry)
7786 te = got_tree_entry_get_next(s->tree,
7789 te = got_object_tree_get_first_entry(s->tree);
7791 if (s->selected_entry == NULL)
7792 te = got_object_tree_get_last_entry(s->tree);
7794 te = got_tree_entry_get_prev(s->tree,
7798 if (s->selected_entry)
7799 te = s->selected_entry;
7800 else if (view->searching == TOG_SEARCH_FORWARD)
7801 te = got_object_tree_get_first_entry(s->tree);
7803 te = got_object_tree_get_last_entry(s->tree);
7808 if (s->matched_entry == NULL) {
7809 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7812 if (view->searching == TOG_SEARCH_FORWARD)
7813 te = got_object_tree_get_first_entry(s->tree);
7815 te = got_object_tree_get_last_entry(s->tree);
7818 if (match_tree_entry(te, &view->regex)) {
7819 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7820 s->matched_entry = te;
7824 if (view->searching == TOG_SEARCH_FORWARD)
7825 te = got_tree_entry_get_next(s->tree, te);
7827 te = got_tree_entry_get_prev(s->tree, te);
7830 if (s->matched_entry) {
7831 s->first_displayed_entry = s->matched_entry;
7838 static const struct got_error *
7839 show_tree_view(struct tog_view *view)
7841 const struct got_error *err = NULL;
7842 struct tog_tree_view_state *s = &view->state.tree;
7845 err = tree_entry_path(&parent_path, &s->parents, NULL);
7849 err = draw_tree_entries(view, parent_path);
7856 static const struct got_error *
7857 tree_goto_line(struct tog_view *view, int nlines)
7859 const struct got_error *err = NULL;
7860 struct tog_tree_view_state *s = &view->state.tree;
7861 struct got_tree_entry **fte, **lte, **ste;
7862 int g, last, first = 1, i = 1;
7863 int root = s->tree == s->root;
7864 int off = root ? 1 : 2;
7871 else if (g > got_object_tree_get_nentries(s->tree))
7872 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7874 fte = &s->first_displayed_entry;
7875 lte = &s->last_displayed_entry;
7876 ste = &s->selected_entry;
7879 first = got_tree_entry_get_index(*fte);
7880 first += off; /* account for ".." */
7882 last = got_tree_entry_get_index(*lte);
7885 if (g >= first && g <= last && g - first < nlines) {
7886 s->selected = g - first;
7887 return NULL; /* gline is on the current page */
7891 i = got_tree_entry_get_index(*ste);
7896 err = tree_scroll_down(view, g - i);
7899 if (got_tree_entry_get_index(*lte) >=
7900 got_object_tree_get_nentries(s->tree) - 1 &&
7901 first + s->selected < g &&
7902 s->selected < s->ndisplayed - 1) {
7903 first = got_tree_entry_get_index(*fte);
7905 s->selected = g - first;
7908 tree_scroll_up(s, i - g);
7911 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7912 s->selected = g - 1;
7917 static const struct got_error *
7918 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7920 const struct got_error *err = NULL;
7921 struct tog_tree_view_state *s = &view->state.tree;
7922 struct got_tree_entry *te;
7923 int n, nscroll = view->nlines - 3;
7926 return tree_goto_line(view, nscroll);
7935 horizontal_scroll_input(view, ch);
7938 s->show_ids = !s->show_ids;
7943 if (!s->selected_entry)
7945 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7949 err = view_request_new(new_view, view, TOG_VIEW_REF);
7956 if (s->tree == s->root)
7957 s->first_displayed_entry =
7958 got_object_tree_get_first_entry(s->tree);
7960 s->first_displayed_entry = NULL;
7965 int eos = view->nlines - 3;
7967 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7971 te = got_object_tree_get_last_entry(s->tree);
7972 for (n = 0; n < eos; n++) {
7974 if (s->tree != s->root) {
7975 s->first_displayed_entry = NULL;
7980 s->first_displayed_entry = te;
7981 te = got_tree_entry_get_prev(s->tree, te);
7984 s->selected = n - 1;
7990 if (s->selected > 0) {
7994 tree_scroll_up(s, 1);
7995 if (s->selected_entry == NULL ||
7996 (s->tree == s->root && s->selected_entry ==
7997 got_object_tree_get_first_entry(s->tree)))
8007 if (s->tree == s->root) {
8008 if (got_object_tree_get_first_entry(s->tree) ==
8009 s->first_displayed_entry)
8010 s->selected -= MIN(s->selected, nscroll);
8012 if (s->first_displayed_entry == NULL)
8013 s->selected -= MIN(s->selected, nscroll);
8015 tree_scroll_up(s, MAX(0, nscroll));
8016 if (s->selected_entry == NULL ||
8017 (s->tree == s->root && s->selected_entry ==
8018 got_object_tree_get_first_entry(s->tree)))
8024 if (s->selected < s->ndisplayed - 1) {
8028 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
8030 /* can't scroll any further */
8034 tree_scroll_down(view, 1);
8044 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
8046 /* can't scroll any further; move cursor down */
8047 if (s->selected < s->ndisplayed - 1)
8048 s->selected += MIN(nscroll,
8049 s->ndisplayed - s->selected - 1);
8054 tree_scroll_down(view, nscroll);
8059 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
8060 struct tog_parent_tree *parent;
8061 /* user selected '..' */
8062 if (s->tree == s->root) {
8066 parent = TAILQ_FIRST(&s->parents);
8067 TAILQ_REMOVE(&s->parents, parent,
8069 got_object_tree_close(s->tree);
8070 s->tree = parent->tree;
8071 s->first_displayed_entry =
8072 parent->first_displayed_entry;
8074 parent->selected_entry;
8075 s->selected = parent->selected;
8076 if (s->selected > view->nlines - 3) {
8077 err = offset_selection_down(view);
8082 } else if (S_ISDIR(got_tree_entry_get_mode(
8083 s->selected_entry))) {
8084 struct got_tree_object *subtree;
8086 err = got_object_open_as_tree(&subtree, s->repo,
8087 got_tree_entry_get_id(s->selected_entry));
8090 err = tree_view_visit_subtree(s, subtree);
8092 got_object_tree_close(subtree);
8095 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
8096 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
8099 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
8100 s->selected = view->nlines - 4;
8116 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
8121 static const struct got_error *
8122 cmd_tree(int argc, char *argv[])
8124 const struct got_error *error;
8125 struct got_repository *repo = NULL;
8126 struct got_worktree *worktree = NULL;
8127 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8128 struct got_object_id *commit_id = NULL;
8129 struct got_commit_object *commit = NULL;
8130 const char *commit_id_arg = NULL;
8131 char *keyword_idstr = NULL, *label = NULL;
8132 struct got_reference *ref = NULL;
8133 const char *head_ref_name = NULL;
8135 struct tog_view *view;
8136 int *pack_fds = NULL;
8138 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
8141 commit_id_arg = optarg;
8144 repo_path = realpath(optarg, NULL);
8145 if (repo_path == NULL)
8146 return got_error_from_errno2("realpath",
8161 error = got_repo_pack_fds_open(&pack_fds);
8165 if (repo_path == NULL) {
8166 cwd = getcwd(NULL, 0);
8168 return got_error_from_errno("getcwd");
8169 error = got_worktree_open(&worktree, cwd, NULL);
8170 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8174 strdup(got_worktree_get_repo_path(worktree));
8176 repo_path = strdup(cwd);
8177 if (repo_path == NULL) {
8178 error = got_error_from_errno("strdup");
8183 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8187 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8194 error = apply_unveil(got_repo_get_path(repo), NULL);
8198 error = tog_load_refs(repo, 0);
8202 if (commit_id_arg == NULL) {
8203 error = got_repo_match_object_id(&commit_id, &label,
8204 worktree ? got_worktree_get_head_ref_name(worktree) :
8205 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8208 head_ref_name = label;
8210 error = got_keyword_to_idstr(&keyword_idstr, commit_id_arg,
8214 if (keyword_idstr != NULL)
8215 commit_id_arg = keyword_idstr;
8217 error = got_ref_open(&ref, repo, commit_id_arg, 0);
8219 head_ref_name = got_ref_get_name(ref);
8220 else if (error->code != GOT_ERR_NOT_REF)
8222 error = got_repo_match_object_id(&commit_id, NULL,
8223 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8228 error = got_object_open_as_commit(&commit, repo, commit_id);
8232 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
8234 error = got_error_from_errno("view_open");
8237 error = open_tree_view(view, commit_id, head_ref_name, repo);
8240 if (!got_path_is_root_dir(in_repo_path)) {
8241 error = tree_view_walk_path(&view->state.tree, commit,
8248 error = set_tog_base_commit(repo, worktree);
8252 /* Release work tree lock. */
8253 got_worktree_close(worktree);
8257 error = view_loop(view);
8260 free(tog_base_commit.id);
8261 free(keyword_idstr);
8268 if (worktree != NULL)
8269 got_worktree_close(worktree);
8271 const struct got_error *close_err = got_repo_close(repo);
8276 const struct got_error *pack_err =
8277 got_repo_pack_fds_close(pack_fds);
8285 static const struct got_error *
8286 ref_view_load_refs(struct tog_ref_view_state *s)
8288 struct got_reflist_entry *sre;
8289 struct tog_reflist_entry *re;
8292 TAILQ_FOREACH(sre, &tog_refs, entry) {
8293 if (strncmp(got_ref_get_name(sre->ref),
8294 "refs/got/", 9) == 0 &&
8295 strncmp(got_ref_get_name(sre->ref),
8296 "refs/got/backup/", 16) != 0)
8299 re = malloc(sizeof(*re));
8301 return got_error_from_errno("malloc");
8303 re->ref = got_ref_dup(sre->ref);
8304 if (re->ref == NULL)
8305 return got_error_from_errno("got_ref_dup");
8306 re->idx = s->nrefs++;
8307 TAILQ_INSERT_TAIL(&s->refs, re, entry);
8310 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8315 ref_view_free_refs(struct tog_ref_view_state *s)
8317 struct tog_reflist_entry *re;
8319 while (!TAILQ_EMPTY(&s->refs)) {
8320 re = TAILQ_FIRST(&s->refs);
8321 TAILQ_REMOVE(&s->refs, re, entry);
8322 got_ref_close(re->ref);
8327 static const struct got_error *
8328 open_ref_view(struct tog_view *view, struct got_repository *repo)
8330 const struct got_error *err = NULL;
8331 struct tog_ref_view_state *s = &view->state.ref;
8333 s->selected_entry = 0;
8336 TAILQ_INIT(&s->refs);
8337 STAILQ_INIT(&s->colors);
8339 err = ref_view_load_refs(s);
8343 if (has_colors() && getenv("TOG_COLORS") != NULL) {
8344 err = add_color(&s->colors, "^refs/heads/",
8345 TOG_COLOR_REFS_HEADS,
8346 get_color_value("TOG_COLOR_REFS_HEADS"));
8350 err = add_color(&s->colors, "^refs/tags/",
8351 TOG_COLOR_REFS_TAGS,
8352 get_color_value("TOG_COLOR_REFS_TAGS"));
8356 err = add_color(&s->colors, "^refs/remotes/",
8357 TOG_COLOR_REFS_REMOTES,
8358 get_color_value("TOG_COLOR_REFS_REMOTES"));
8362 err = add_color(&s->colors, "^refs/got/backup/",
8363 TOG_COLOR_REFS_BACKUP,
8364 get_color_value("TOG_COLOR_REFS_BACKUP"));
8369 view->show = show_ref_view;
8370 view->input = input_ref_view;
8371 view->close = close_ref_view;
8372 view->search_start = search_start_ref_view;
8373 view->search_next = search_next_ref_view;
8376 if (view->close == NULL)
8377 close_ref_view(view);
8383 static const struct got_error *
8384 close_ref_view(struct tog_view *view)
8386 struct tog_ref_view_state *s = &view->state.ref;
8388 ref_view_free_refs(s);
8389 free_colors(&s->colors);
8394 static const struct got_error *
8395 resolve_reflist_entry(struct got_object_id **commit_id,
8396 struct tog_reflist_entry *re, struct got_repository *repo)
8398 const struct got_error *err = NULL;
8399 struct got_object_id *obj_id;
8400 struct got_tag_object *tag = NULL;
8405 err = got_ref_resolve(&obj_id, repo, re->ref);
8409 err = got_object_get_type(&obj_type, repo, obj_id);
8414 case GOT_OBJ_TYPE_COMMIT:
8415 *commit_id = obj_id;
8417 case GOT_OBJ_TYPE_TAG:
8418 err = got_object_open_as_tag(&tag, repo, obj_id);
8422 err = got_object_get_type(&obj_type, repo,
8423 got_object_tag_get_object_id(tag));
8426 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
8427 err = got_error(GOT_ERR_OBJ_TYPE);
8430 *commit_id = got_object_id_dup(
8431 got_object_tag_get_object_id(tag));
8432 if (*commit_id == NULL) {
8433 err = got_error_from_errno("got_object_id_dup");
8438 err = got_error(GOT_ERR_OBJ_TYPE);
8444 got_object_tag_close(tag);
8452 static const struct got_error *
8453 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
8454 struct tog_reflist_entry *re, struct got_repository *repo)
8456 struct tog_view *log_view;
8457 const struct got_error *err = NULL;
8458 struct got_object_id *commit_id = NULL;
8462 err = resolve_reflist_entry(&commit_id, re, repo);
8464 if (err->code != GOT_ERR_OBJ_TYPE)
8470 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
8471 if (log_view == NULL) {
8472 err = got_error_from_errno("view_open");
8476 err = open_log_view(log_view, commit_id, repo,
8477 got_ref_get_name(re->ref), "", 0);
8480 view_close(log_view);
8482 *new_view = log_view;
8488 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
8490 struct tog_reflist_entry *re;
8493 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8496 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
8497 while (i++ < maxscroll) {
8500 s->first_displayed_entry = re;
8501 re = TAILQ_PREV(re, tog_reflist_head, entry);
8505 static const struct got_error *
8506 ref_scroll_down(struct tog_view *view, int maxscroll)
8508 struct tog_ref_view_state *s = &view->state.ref;
8509 struct tog_reflist_entry *next, *last;
8512 if (s->first_displayed_entry)
8513 next = TAILQ_NEXT(s->first_displayed_entry, entry);
8515 next = TAILQ_FIRST(&s->refs);
8517 last = s->last_displayed_entry;
8518 while (next && n++ < maxscroll) {
8520 s->last_displayed_entry = last;
8521 last = TAILQ_NEXT(last, entry);
8523 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
8524 s->first_displayed_entry = next;
8525 next = TAILQ_NEXT(next, entry);
8532 static const struct got_error *
8533 search_start_ref_view(struct tog_view *view)
8535 struct tog_ref_view_state *s = &view->state.ref;
8537 s->matched_entry = NULL;
8542 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8544 regmatch_t regmatch;
8546 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8550 static const struct got_error *
8551 search_next_ref_view(struct tog_view *view)
8553 struct tog_ref_view_state *s = &view->state.ref;
8554 struct tog_reflist_entry *re = NULL;
8556 if (!view->searching) {
8557 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8561 if (s->matched_entry) {
8562 if (view->searching == TOG_SEARCH_FORWARD) {
8563 if (s->selected_entry)
8564 re = TAILQ_NEXT(s->selected_entry, entry);
8566 re = TAILQ_PREV(s->selected_entry,
8567 tog_reflist_head, entry);
8569 if (s->selected_entry == NULL)
8570 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8572 re = TAILQ_PREV(s->selected_entry,
8573 tog_reflist_head, entry);
8576 if (s->selected_entry)
8577 re = s->selected_entry;
8578 else if (view->searching == TOG_SEARCH_FORWARD)
8579 re = TAILQ_FIRST(&s->refs);
8581 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8586 if (s->matched_entry == NULL) {
8587 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8590 if (view->searching == TOG_SEARCH_FORWARD)
8591 re = TAILQ_FIRST(&s->refs);
8593 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8596 if (match_reflist_entry(re, &view->regex)) {
8597 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8598 s->matched_entry = re;
8602 if (view->searching == TOG_SEARCH_FORWARD)
8603 re = TAILQ_NEXT(re, entry);
8605 re = TAILQ_PREV(re, tog_reflist_head, entry);
8608 if (s->matched_entry) {
8609 s->first_displayed_entry = s->matched_entry;
8616 static const struct got_error *
8617 show_ref_view(struct tog_view *view)
8619 const struct got_error *err = NULL;
8620 struct tog_ref_view_state *s = &view->state.ref;
8621 struct tog_reflist_entry *re;
8624 struct tog_color *tc;
8625 int width, n, scrollx;
8626 int limit = view->nlines;
8628 werase(view->window);
8631 if (view_is_hsplit_top(view))
8632 --limit; /* border */
8637 re = s->first_displayed_entry;
8639 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8641 return got_error_from_errno("asprintf");
8643 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8648 if (view_needs_focus_indication(view))
8649 wstandout(view->window);
8650 waddwstr(view->window, wline);
8651 while (width++ < view->ncols)
8652 waddch(view->window, ' ');
8653 if (view_needs_focus_indication(view))
8654 wstandend(view->window);
8664 while (re && limit > 0) {
8666 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8669 struct got_commit_object *ci;
8670 struct got_tag_object *tag;
8671 struct got_object_id *id;
8675 err = got_ref_resolve(&id, s->repo, re->ref);
8678 err = got_object_open_as_tag(&tag, s->repo, id);
8680 if (err->code != GOT_ERR_OBJ_TYPE) {
8684 err = got_object_open_as_commit(&ci, s->repo,
8690 t = got_object_commit_get_committer_time(ci);
8691 got_object_commit_close(ci);
8693 t = got_object_tag_get_tagger_time(tag);
8694 got_object_tag_close(tag);
8697 if (gmtime_r(&t, &tm) == NULL)
8698 return got_error_from_errno("gmtime_r");
8699 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8700 return got_error(GOT_ERR_NO_SPACE);
8702 if (got_ref_is_symbolic(re->ref)) {
8703 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8704 ymd : "", got_ref_get_name(re->ref),
8705 got_ref_get_symref_target(re->ref)) == -1)
8706 return got_error_from_errno("asprintf");
8707 } else if (s->show_ids) {
8708 struct got_object_id *id;
8710 err = got_ref_resolve(&id, s->repo, re->ref);
8713 err = got_object_id_str(&id_str, id);
8718 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8719 got_ref_get_name(re->ref), id_str) == -1) {
8720 err = got_error_from_errno("asprintf");
8727 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8728 got_ref_get_name(re->ref)) == -1)
8729 return got_error_from_errno("asprintf");
8731 /* use full line width to determine view->maxx */
8732 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8737 view->maxx = MAX(view->maxx, width);
8741 err = format_line(&wline, &width, &scrollx, line, view->x,
8747 if (n == s->selected) {
8749 wstandout(view->window);
8750 s->selected_entry = re;
8752 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8754 wattr_on(view->window,
8755 COLOR_PAIR(tc->colorpair), NULL);
8756 waddwstr(view->window, &wline[scrollx]);
8758 wattr_off(view->window,
8759 COLOR_PAIR(tc->colorpair), NULL);
8760 if (width < view->ncols)
8761 waddch(view->window, '\n');
8762 if (n == s->selected && view->focussed)
8763 wstandend(view->window);
8769 s->last_displayed_entry = re;
8772 re = TAILQ_NEXT(re, entry);
8779 static const struct got_error *
8780 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8781 struct tog_reflist_entry *re, struct got_repository *repo)
8783 const struct got_error *err = NULL;
8784 struct got_object_id *commit_id = NULL;
8785 struct tog_view *tree_view;
8789 err = resolve_reflist_entry(&commit_id, re, repo);
8791 if (err->code != GOT_ERR_OBJ_TYPE)
8798 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8799 if (tree_view == NULL) {
8800 err = got_error_from_errno("view_open");
8804 err = open_tree_view(tree_view, commit_id,
8805 got_ref_get_name(re->ref), repo);
8809 *new_view = tree_view;
8815 static const struct got_error *
8816 ref_goto_line(struct tog_view *view, int nlines)
8818 const struct got_error *err = NULL;
8819 struct tog_ref_view_state *s = &view->state.ref;
8820 int g, idx = s->selected_entry->idx;
8827 else if (g > s->nrefs)
8830 if (g >= s->first_displayed_entry->idx + 1 &&
8831 g <= s->last_displayed_entry->idx + 1 &&
8832 g - s->first_displayed_entry->idx - 1 < nlines) {
8833 s->selected = g - s->first_displayed_entry->idx - 1;
8838 err = ref_scroll_down(view, g - idx - 1);
8841 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8842 s->first_displayed_entry->idx + s->selected < g &&
8843 s->selected < s->ndisplayed - 1)
8844 s->selected = g - s->first_displayed_entry->idx - 1;
8845 } else if (idx + 1 > g)
8846 ref_scroll_up(s, idx - g + 1);
8848 if (g < nlines && s->first_displayed_entry->idx == 0)
8849 s->selected = g - 1;
8855 static const struct got_error *
8856 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8858 const struct got_error *err = NULL;
8859 struct tog_ref_view_state *s = &view->state.ref;
8860 struct tog_reflist_entry *re;
8861 int n, nscroll = view->nlines - 1;
8864 return ref_goto_line(view, nscroll);
8873 horizontal_scroll_input(view, ch);
8876 s->show_ids = !s->show_ids;
8880 s->show_date = !s->show_date;
8884 s->sort_by_date = !s->sort_by_date;
8885 view->action = s->sort_by_date ? "sort by date" : "sort by name";
8887 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8888 got_ref_cmp_by_commit_timestamp_descending :
8889 tog_ref_cmp_by_name, s->repo);
8892 got_reflist_object_id_map_free(tog_refs_idmap);
8893 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8894 &tog_refs, s->repo);
8897 ref_view_free_refs(s);
8898 err = ref_view_load_refs(s);
8903 if (!s->selected_entry)
8905 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8909 if (!s->selected_entry)
8911 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8918 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8923 int eos = view->nlines - 1;
8925 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8929 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8930 for (n = 0; n < eos; n++) {
8933 s->first_displayed_entry = re;
8934 re = TAILQ_PREV(re, tog_reflist_head, entry);
8937 s->selected = n - 1;
8943 if (s->selected > 0) {
8947 ref_scroll_up(s, 1);
8948 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8958 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8959 s->selected -= MIN(nscroll, s->selected);
8960 ref_scroll_up(s, MAX(0, nscroll));
8961 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8967 if (s->selected < s->ndisplayed - 1) {
8971 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8972 /* can't scroll any further */
8976 ref_scroll_down(view, 1);
8986 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8987 /* can't scroll any further; move cursor down */
8988 if (s->selected < s->ndisplayed - 1)
8989 s->selected += MIN(nscroll,
8990 s->ndisplayed - s->selected - 1);
8991 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8992 s->selected += s->ndisplayed - s->selected - 1;
8996 ref_scroll_down(view, nscroll);
9001 err = tog_load_refs(s->repo, s->sort_by_date);
9004 ref_view_free_refs(s);
9005 err = ref_view_load_refs(s);
9008 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
9009 s->selected = view->nlines - 2;
9023 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
9028 static const struct got_error *
9029 cmd_ref(int argc, char *argv[])
9031 const struct got_error *error;
9032 struct got_repository *repo = NULL;
9033 struct got_worktree *worktree = NULL;
9034 char *cwd = NULL, *repo_path = NULL;
9036 struct tog_view *view;
9037 int *pack_fds = NULL;
9039 while ((ch = getopt(argc, argv, "r:")) != -1) {
9042 repo_path = realpath(optarg, NULL);
9043 if (repo_path == NULL)
9044 return got_error_from_errno2("realpath",
9059 error = got_repo_pack_fds_open(&pack_fds);
9063 if (repo_path == NULL) {
9064 cwd = getcwd(NULL, 0);
9066 return got_error_from_errno("getcwd");
9067 error = got_worktree_open(&worktree, cwd, NULL);
9068 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9072 strdup(got_worktree_get_repo_path(worktree));
9074 repo_path = strdup(cwd);
9075 if (repo_path == NULL) {
9076 error = got_error_from_errno("strdup");
9081 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9087 error = apply_unveil(got_repo_get_path(repo), NULL);
9091 error = tog_load_refs(repo, 0);
9095 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
9097 error = got_error_from_errno("view_open");
9101 error = open_ref_view(view, repo);
9106 error = set_tog_base_commit(repo, worktree);
9110 /* Release work tree lock. */
9111 got_worktree_close(worktree);
9115 error = view_loop(view);
9118 free(tog_base_commit.id);
9121 if (worktree != NULL)
9122 got_worktree_close(worktree);
9124 const struct got_error *close_err = got_repo_close(repo);
9129 const struct got_error *pack_err =
9130 got_repo_pack_fds_close(pack_fds);
9138 static const struct got_error*
9139 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
9148 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
9152 if (mvwprintw(win, y, x, "%s", str) == ERR)
9153 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
9160 static const struct got_error *
9161 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
9165 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
9167 free(*line_offsets);
9168 *line_offsets = NULL;
9169 return got_error_from_errno("reallocarray");
9173 (*line_offsets)[*nlines] = off;
9178 static const struct got_error *
9179 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
9183 for (;n > 0; --n, ++km) {
9187 if (km->keys == NULL)
9190 t = t0 = strdup(km->keys);
9192 return got_error_from_errno("strdup");
9195 while ((k = strsep(&t, " ")) != NULL)
9196 len += strlen(k) > 1 ? 2 : 0;
9198 *ret = MAX(*ret, len);
9205 * Write keymap section headers, keys, and key info in km to f.
9206 * Save line offset to *off. If terminal has UTF8 encoding enabled,
9207 * wrap control and symbolic keys in guillemets, else use <>.
9209 static const struct got_error *
9210 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
9215 static const char *u8_glyph[] = {
9216 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
9217 "\xe2\x80\xba" /* U+203A (utf8 >) */
9220 int cs, s, first = 1;
9222 cs = got_locale_is_utf8();
9224 t = t0 = strdup(km->keys);
9226 return got_error_from_errno("strdup");
9228 len = strlen(km->keys);
9229 while ((k = strsep(&t, " ")) != NULL) {
9230 s = strlen(k) > 1; /* control or symbolic key */
9231 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
9232 cs && s ? u8_glyph[0] : s ? "<" : "", k,
9233 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
9236 return got_error_from_errno("fprintf");
9244 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
9246 return got_error_from_errno("fprintf");
9252 static const struct got_error *
9253 format_help(struct tog_help_view_state *s)
9255 const struct got_error *err = NULL;
9257 int i, max, n, show = s->all;
9258 static const struct tog_key_map km[] = {
9259 #define KEYMAP_(info, type) { NULL, (info), type }
9260 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
9266 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
9271 err = max_key_str(&max, km, n);
9275 for (i = 0; i < n; ++i) {
9276 if (km[i].keys == NULL) {
9278 if (km[i].type == TOG_KEYMAP_GLOBAL ||
9279 km[i].type == s->type || s->all)
9283 err = format_help_line(&off, s->f, &km[i], max);
9286 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9293 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9297 static const struct got_error *
9298 create_help(struct tog_help_view_state *s)
9301 const struct got_error *err;
9303 free(s->line_offsets);
9304 s->line_offsets = NULL;
9309 return got_error_from_errno("got_opentemp");
9312 err = format_help(s);
9316 if (s->f && fflush(s->f) != 0)
9317 return got_error_from_errno("fflush");
9322 static const struct got_error *
9323 search_start_help_view(struct tog_view *view)
9325 view->state.help.matched_line = 0;
9330 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
9331 size_t *nlines, int **first, int **last, int **match, int **selected)
9333 struct tog_help_view_state *s = &view->state.help;
9336 *nlines = s->nlines;
9337 *line_offsets = s->line_offsets;
9338 *match = &s->matched_line;
9339 *first = &s->first_displayed_line;
9340 *last = &s->last_displayed_line;
9341 *selected = &s->selected_line;
9344 static const struct got_error *
9345 show_help_view(struct tog_view *view)
9347 struct tog_help_view_state *s = &view->state.help;
9348 const struct got_error *err;
9349 regmatch_t *regmatch = &view->regmatch;
9354 int width, nprinted = 0, rc = 0;
9355 int eos = view->nlines;
9357 if (view_is_hsplit_top(view))
9358 --eos; /* account for border */
9362 werase(view->window);
9364 if (view->gline > s->nlines - 1)
9365 view->gline = s->nlines - 1;
9367 err = win_draw_center(view->window, 0, 0, view->ncols,
9368 view_needs_focus_indication(view),
9369 "tog help (press q to return to tog)");
9374 waddstr(view->window, "\n\n");
9380 while (eos > 0 && nprinted < eos) {
9383 linelen = getline(&line, &linesz, s->f);
9384 if (linelen == -1) {
9387 return got_ferror(s->f, GOT_ERR_IO);
9392 if (++s->lineno < s->first_displayed_line)
9394 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
9396 if (s->lineno == view->hiline)
9399 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
9405 view->maxx = MAX(view->maxx, width);
9410 wattron(view->window, attr);
9411 if (s->first_displayed_line + nprinted == s->matched_line &&
9412 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
9413 err = add_matched_line(&width, line, view->ncols - 1, 0,
9414 view->window, view->x, regmatch);
9422 err = format_line(&wline, &width, &skip, line,
9423 view->x, view->ncols, 0, view->x ? 1 : 0);
9428 waddwstr(view->window, &wline[skip]);
9432 if (s->lineno == view->hiline) {
9433 while (width++ < view->ncols)
9434 waddch(view->window, ' ');
9436 if (width < view->ncols)
9437 waddch(view->window, '\n');
9440 wattroff(view->window, attr);
9441 if (++nprinted == 1)
9442 s->first_displayed_line = s->lineno;
9446 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
9448 s->last_displayed_line = s->first_displayed_line;
9453 rc = waddnstr(view->window,
9454 "See the tog(1) manual page for full documentation",
9457 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9459 wmove(view->window, view->nlines - 1, 0);
9460 wclrtoeol(view->window);
9461 wstandout(view->window);
9462 rc = waddnstr(view->window, "scroll down for more...",
9465 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9466 if (getcurx(view->window) < view->ncols - 6) {
9467 rc = wprintw(view->window, "[%.0f%%]",
9468 100.00 * s->last_displayed_line / s->nlines);
9470 return got_error_msg(GOT_ERR_IO, "wprintw");
9472 wstandend(view->window);
9478 static const struct got_error *
9479 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
9481 struct tog_help_view_state *s = &view->state.help;
9482 const struct got_error *err = NULL;
9488 eos = nscroll = view->nlines;
9489 if (view_is_hsplit_top(view))
9492 s->lineno = s->first_displayed_line - 1 + s->selected_line;
9501 horizontal_scroll_input(view, ch);
9505 s->first_displayed_line = 1;
9513 s->first_displayed_line = (s->nlines - eos) + 3;
9518 if (s->first_displayed_line > 1)
9519 --s->first_displayed_line;
9530 if (s->first_displayed_line == 1) {
9534 while (--nscroll > 0 && s->first_displayed_line > 1)
9535 s->first_displayed_line--;
9541 ++s->first_displayed_line;
9557 while (!s->eof && --nscroll > 0) {
9558 linelen = getline(&line, &linesz, s->f);
9559 s->first_displayed_line++;
9560 if (linelen == -1) {
9564 err = got_ferror(s->f, GOT_ERR_IO);
9578 static const struct got_error *
9579 close_help_view(struct tog_view *view)
9581 struct tog_help_view_state *s = &view->state.help;
9583 free(s->line_offsets);
9584 s->line_offsets = NULL;
9585 if (fclose(s->f) == EOF)
9586 return got_error_from_errno("fclose");
9591 static const struct got_error *
9592 reset_help_view(struct tog_view *view)
9594 struct tog_help_view_state *s = &view->state.help;
9597 if (s->f && fclose(s->f) == EOF)
9598 return got_error_from_errno("fclose");
9600 wclear(view->window);
9604 s->first_displayed_line = 1;
9605 s->last_displayed_line = view->nlines;
9606 s->matched_line = 0;
9608 return create_help(s);
9611 static const struct got_error *
9612 open_help_view(struct tog_view *view, struct tog_view *parent)
9614 const struct got_error *err = NULL;
9615 struct tog_help_view_state *s = &view->state.help;
9617 s->type = (enum tog_keymap_type)parent->type;
9618 s->first_displayed_line = 1;
9619 s->last_displayed_line = view->nlines;
9620 s->selected_line = 1;
9622 view->show = show_help_view;
9623 view->input = input_help_view;
9624 view->reset = reset_help_view;
9625 view->close = close_help_view;
9626 view->search_start = search_start_help_view;
9627 view->search_setup = search_setup_help_view;
9628 view->search_next = search_next_view_match;
9630 err = create_help(s);
9634 static const struct got_error *
9635 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9636 enum tog_view_type request, int y, int x)
9638 const struct got_error *err = NULL;
9644 if (view->type == TOG_VIEW_LOG) {
9645 struct tog_log_view_state *s = &view->state.log;
9647 err = open_diff_view_for_commit(new_view, y, x,
9648 s->selected_entry->commit, s->selected_entry->id,
9651 return got_error_msg(GOT_ERR_NOT_IMPL,
9652 "parent/child view pair not supported");
9654 case TOG_VIEW_BLAME:
9655 if (view->type == TOG_VIEW_TREE) {
9656 struct tog_tree_view_state *s = &view->state.tree;
9658 err = blame_tree_entry(new_view, y, x,
9659 s->selected_entry, &s->parents, s->commit_id,
9662 return got_error_msg(GOT_ERR_NOT_IMPL,
9663 "parent/child view pair not supported");
9666 if (view->type == TOG_VIEW_BLAME)
9667 err = log_annotated_line(new_view, y, x,
9668 view->state.blame.repo, view->state.blame.id_to_log);
9669 else if (view->type == TOG_VIEW_TREE)
9670 err = log_selected_tree_entry(new_view, y, x,
9672 else if (view->type == TOG_VIEW_REF)
9673 err = log_ref_entry(new_view, y, x,
9674 view->state.ref.selected_entry,
9675 view->state.ref.repo);
9677 return got_error_msg(GOT_ERR_NOT_IMPL,
9678 "parent/child view pair not supported");
9681 if (view->type == TOG_VIEW_LOG)
9682 err = browse_commit_tree(new_view, y, x,
9683 view->state.log.selected_entry,
9684 view->state.log.in_repo_path,
9685 view->state.log.head_ref_name,
9686 view->state.log.repo);
9687 else if (view->type == TOG_VIEW_REF)
9688 err = browse_ref_tree(new_view, y, x,
9689 view->state.ref.selected_entry,
9690 view->state.ref.repo);
9692 return got_error_msg(GOT_ERR_NOT_IMPL,
9693 "parent/child view pair not supported");
9696 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9697 if (*new_view == NULL)
9698 return got_error_from_errno("view_open");
9699 if (view->type == TOG_VIEW_LOG)
9700 err = open_ref_view(*new_view, view->state.log.repo);
9701 else if (view->type == TOG_VIEW_TREE)
9702 err = open_ref_view(*new_view, view->state.tree.repo);
9704 err = got_error_msg(GOT_ERR_NOT_IMPL,
9705 "parent/child view pair not supported");
9707 view_close(*new_view);
9710 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9711 if (*new_view == NULL)
9712 return got_error_from_errno("view_open");
9713 err = open_help_view(*new_view, view);
9715 view_close(*new_view);
9718 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9725 * If view was scrolled down to move the selected line into view when opening a
9726 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9729 offset_selection_up(struct tog_view *view)
9731 switch (view->type) {
9732 case TOG_VIEW_BLAME: {
9733 struct tog_blame_view_state *s = &view->state.blame;
9734 if (s->first_displayed_line == 1) {
9735 s->selected_line = MAX(s->selected_line - view->offset,
9739 if (s->first_displayed_line > view->offset)
9740 s->first_displayed_line -= view->offset;
9742 s->first_displayed_line = 1;
9743 s->selected_line += view->offset;
9747 log_scroll_up(&view->state.log, view->offset);
9748 view->state.log.selected += view->offset;
9751 ref_scroll_up(&view->state.ref, view->offset);
9752 view->state.ref.selected += view->offset;
9755 tree_scroll_up(&view->state.tree, view->offset);
9756 view->state.tree.selected += view->offset;
9766 * If the selected line is in the section of screen covered by the bottom split,
9767 * scroll down offset lines to move it into view and index its new position.
9769 static const struct got_error *
9770 offset_selection_down(struct tog_view *view)
9772 const struct got_error *err = NULL;
9773 const struct got_error *(*scrolld)(struct tog_view *, int);
9774 int *selected = NULL;
9777 switch (view->type) {
9778 case TOG_VIEW_BLAME: {
9779 struct tog_blame_view_state *s = &view->state.blame;
9782 if (s->selected_line > view->nlines - header) {
9783 offset = abs(view->nlines - s->selected_line - header);
9784 s->first_displayed_line += offset;
9785 s->selected_line -= offset;
9786 view->offset = offset;
9790 case TOG_VIEW_LOG: {
9791 struct tog_log_view_state *s = &view->state.log;
9792 scrolld = &log_scroll_down;
9793 header = view_is_parent_view(view) ? 3 : 2;
9794 selected = &s->selected;
9797 case TOG_VIEW_REF: {
9798 struct tog_ref_view_state *s = &view->state.ref;
9799 scrolld = &ref_scroll_down;
9801 selected = &s->selected;
9804 case TOG_VIEW_TREE: {
9805 struct tog_tree_view_state *s = &view->state.tree;
9806 scrolld = &tree_scroll_down;
9808 selected = &s->selected;
9818 if (selected && *selected > view->nlines - header) {
9819 offset = abs(view->nlines - *selected - header);
9820 view->offset = offset;
9821 if (scrolld && offset) {
9822 err = scrolld(view, offset);
9823 *selected -= offset;
9831 list_commands(FILE *fp)
9835 fprintf(fp, "commands:");
9836 for (i = 0; i < nitems(tog_commands); i++) {
9837 const struct tog_cmd *cmd = &tog_commands[i];
9838 fprintf(fp, " %s", cmd->name);
9844 usage(int hflag, int status)
9846 FILE *fp = (status == 0) ? stdout : stderr;
9848 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9851 fprintf(fp, "lazy usage: %s path\n", getprogname());
9858 make_argv(int argc, ...)
9866 argv = calloc(argc, sizeof(char *));
9869 for (i = 0; i < argc; i++) {
9870 argv[i] = strdup(va_arg(ap, char *));
9871 if (argv[i] == NULL)
9880 * Try to convert 'tog path' into a 'tog log path' command.
9881 * The user could simply have mistyped the command rather than knowingly
9882 * provided a path. So check whether argv[0] can in fact be resolved
9883 * to a path in the HEAD commit and print a special error if not.
9884 * This hack is for mpi@ <3
9886 static const struct got_error *
9887 tog_log_with_path(int argc, char *argv[])
9889 const struct got_error *error = NULL, *close_err;
9890 const struct tog_cmd *cmd = NULL;
9891 struct got_repository *repo = NULL;
9892 struct got_worktree *worktree = NULL;
9893 struct got_object_id *commit_id = NULL, *id = NULL;
9894 struct got_commit_object *commit = NULL;
9895 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9896 char *commit_id_str = NULL, **cmd_argv = NULL;
9897 int *pack_fds = NULL;
9899 cwd = getcwd(NULL, 0);
9901 return got_error_from_errno("getcwd");
9903 error = got_repo_pack_fds_open(&pack_fds);
9907 error = got_worktree_open(&worktree, cwd, NULL);
9908 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9912 repo_path = strdup(got_worktree_get_repo_path(worktree));
9914 repo_path = strdup(cwd);
9915 if (repo_path == NULL) {
9916 error = got_error_from_errno("strdup");
9920 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9924 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9929 error = tog_load_refs(repo, 0);
9932 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9933 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9934 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9939 got_worktree_close(worktree);
9943 error = got_object_open_as_commit(&commit, repo, commit_id);
9947 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9949 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9951 fprintf(stderr, "%s: '%s' is no known command or path\n",
9952 getprogname(), argv[0]);
9957 error = got_object_id_str(&commit_id_str, commit_id);
9961 cmd = &tog_commands[0]; /* log */
9963 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9964 error = cmd->cmd_main(argc, cmd_argv);
9967 close_err = got_repo_close(repo);
9972 got_object_commit_close(commit);
9974 got_worktree_close(worktree);
9976 const struct got_error *pack_err =
9977 got_repo_pack_fds_close(pack_fds);
9982 free(commit_id_str);
9989 for (i = 0; i < argc; i++)
9998 main(int argc, char *argv[])
10000 const struct got_error *io_err, *error = NULL;
10001 const struct tog_cmd *cmd = NULL;
10002 int ch, hflag = 0, Vflag = 0;
10003 char **cmd_argv = NULL;
10004 static const struct option longopts[] = {
10005 { "version", no_argument, NULL, 'V' },
10006 { NULL, 0, NULL, 0}
10008 char *diff_algo_str = NULL;
10009 const char *test_script_path;
10011 setlocale(LC_CTYPE, "");
10014 * Test mode init must happen before pledge() because "tty" will
10015 * not allow TTY-related ioctls to occur via regular files.
10017 test_script_path = getenv("TOG_TEST_SCRIPT");
10018 if (test_script_path != NULL) {
10019 error = init_mock_term(test_script_path);
10021 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
10024 } else if (!isatty(STDIN_FILENO))
10025 errx(1, "standard input is not a tty");
10027 #if !defined(PROFILE)
10028 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
10033 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
10053 got_version_print_str();
10060 /* Build an argument vector which runs a default command. */
10061 cmd = &tog_commands[0];
10063 cmd_argv = make_argv(argc, cmd->name);
10067 /* Did the user specify a command? */
10068 for (i = 0; i < nitems(tog_commands); i++) {
10069 if (strncmp(tog_commands[i].name, argv[0],
10070 strlen(argv[0])) == 0) {
10071 cmd = &tog_commands[i];
10077 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
10078 if (diff_algo_str) {
10079 if (strcasecmp(diff_algo_str, "patience") == 0)
10080 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
10081 if (strcasecmp(diff_algo_str, "myers") == 0)
10082 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
10088 /* No command specified; try log with a path */
10089 error = tog_log_with_path(argc, argv);
10094 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
10097 if (using_mock_io) {
10098 io_err = tog_io_close();
10105 for (i = 0; i < argc; i++)
10110 if (error && error->code != GOT_ERR_CANCELLED &&
10111 error->code != GOT_ERR_EOF &&
10112 error->code != GOT_ERR_PRIVSEP_EXIT &&
10113 error->code != GOT_ERR_PRIVSEP_PIPE &&
10114 !(error->code == GOT_ERR_ERRNO && errno == EINTR)) {
10115 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);