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"
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
65 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 #define CTRL(x) ((x) & 0x1f)
71 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
76 const struct got_error *(*cmd_main)(int, char *[]);
77 void (*cmd_usage)(void);
80 __dead static void usage(int, int);
81 __dead static void usage_log(void);
82 __dead static void usage_diff(void);
83 __dead static void usage_blame(void);
84 __dead static void usage_tree(void);
85 __dead static void usage_ref(void);
87 static const struct got_error* cmd_log(int, char *[]);
88 static const struct got_error* cmd_diff(int, char *[]);
89 static const struct got_error* cmd_blame(int, char *[]);
90 static const struct got_error* cmd_tree(int, char *[]);
91 static const struct got_error* cmd_ref(int, char *[]);
93 static const struct tog_cmd tog_commands[] = {
94 { "log", cmd_log, usage_log },
95 { "diff", cmd_diff, usage_diff },
96 { "blame", cmd_blame, usage_blame },
97 { "tree", cmd_tree, usage_tree },
98 { "ref", cmd_ref, usage_ref },
110 /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
111 enum tog_keymap_type {
112 TOG_KEYMAP_KEYS = -2,
128 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
130 #define TOG_EOF_STRING "(END)"
132 struct commit_queue_entry {
133 TAILQ_ENTRY(commit_queue_entry) entry;
134 struct got_object_id *id;
135 struct got_commit_object *commit;
138 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
139 struct commit_queue {
141 struct commit_queue_head head;
145 STAILQ_ENTRY(tog_color) entry;
149 STAILQ_HEAD(tog_colors, tog_color);
151 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
152 static struct got_reflist_object_id_map *tog_refs_idmap;
153 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
155 static const struct got_error *
156 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
157 struct got_reference* re2)
159 const char *name1 = got_ref_get_name(re1);
160 const char *name2 = got_ref_get_name(re2);
161 int isbackup1, isbackup2;
163 /* Sort backup refs towards the bottom of the list. */
164 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
165 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
166 if (!isbackup1 && isbackup2) {
169 } else if (isbackup1 && !isbackup2) {
174 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
178 static const struct got_error *
179 tog_load_refs(struct got_repository *repo, int sort_by_date)
181 const struct got_error *err;
183 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
184 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
189 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
196 if (tog_refs_idmap) {
197 got_reflist_object_id_map_free(tog_refs_idmap);
198 tog_refs_idmap = NULL;
200 got_ref_list_free(&tog_refs);
203 static const struct got_error *
204 add_color(struct tog_colors *colors, const char *pattern,
205 int idx, short color)
207 const struct got_error *err = NULL;
208 struct tog_color *tc;
211 if (idx < 1 || idx > COLOR_PAIRS - 1)
214 init_pair(idx, color, -1);
216 tc = calloc(1, sizeof(*tc));
218 return got_error_from_errno("calloc");
219 regerr = regcomp(&tc->regex, pattern,
220 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
222 static char regerr_msg[512];
223 static char err_msg[512];
224 regerror(regerr, &tc->regex, regerr_msg,
226 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
228 err = got_error_msg(GOT_ERR_REGEX, err_msg);
233 STAILQ_INSERT_HEAD(colors, tc, entry);
238 free_colors(struct tog_colors *colors)
240 struct tog_color *tc;
242 while (!STAILQ_EMPTY(colors)) {
243 tc = STAILQ_FIRST(colors);
244 STAILQ_REMOVE_HEAD(colors, entry);
250 static struct tog_color *
251 get_color(struct tog_colors *colors, int colorpair)
253 struct tog_color *tc = NULL;
255 STAILQ_FOREACH(tc, colors, entry) {
256 if (tc->colorpair == colorpair)
264 default_color_value(const char *envvar)
266 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
267 return COLOR_MAGENTA;
268 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
270 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
272 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
274 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
275 return COLOR_MAGENTA;
276 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
277 return COLOR_MAGENTA;
278 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
280 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
282 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
284 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
286 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
288 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
290 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
291 return COLOR_MAGENTA;
292 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
294 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
301 get_color_value(const char *envvar)
303 const char *val = getenv(envvar);
306 return default_color_value(envvar);
308 if (strcasecmp(val, "black") == 0)
310 if (strcasecmp(val, "red") == 0)
312 if (strcasecmp(val, "green") == 0)
314 if (strcasecmp(val, "yellow") == 0)
316 if (strcasecmp(val, "blue") == 0)
318 if (strcasecmp(val, "magenta") == 0)
319 return COLOR_MAGENTA;
320 if (strcasecmp(val, "cyan") == 0)
322 if (strcasecmp(val, "white") == 0)
324 if (strcasecmp(val, "default") == 0)
327 return default_color_value(envvar);
330 struct tog_diff_view_state {
331 struct got_object_id *id1, *id2;
332 const char *label1, *label2;
336 int first_displayed_line;
337 int last_displayed_line;
340 int ignore_whitespace;
342 struct got_repository *repo;
343 struct got_diff_line *lines;
348 /* passed from log or blame view; may be NULL */
349 struct tog_view *parent_view;
352 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
353 static volatile sig_atomic_t tog_thread_error;
355 struct tog_log_thread_args {
356 pthread_cond_t need_commits;
357 pthread_cond_t commit_loaded;
360 struct got_commit_graph *graph;
361 struct commit_queue *real_commits;
362 const char *in_repo_path;
363 struct got_object_id *start_id;
364 struct got_repository *repo;
368 struct commit_queue_entry **first_displayed_entry;
369 struct commit_queue_entry **selected_entry;
371 int *search_next_done;
375 regex_t *limit_regex;
376 struct commit_queue *limit_commits;
379 struct tog_log_view_state {
380 struct commit_queue *commits;
381 struct commit_queue_entry *first_displayed_entry;
382 struct commit_queue_entry *last_displayed_entry;
383 struct commit_queue_entry *selected_entry;
384 struct commit_queue real_commits;
389 struct got_repository *repo;
390 struct got_object_id *start_id;
393 struct tog_log_thread_args thread_args;
394 struct commit_queue_entry *matched_entry;
395 struct commit_queue_entry *search_entry;
396 struct tog_colors colors;
400 struct commit_queue limit_commits;
403 #define TOG_COLOR_DIFF_MINUS 1
404 #define TOG_COLOR_DIFF_PLUS 2
405 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
406 #define TOG_COLOR_DIFF_META 4
407 #define TOG_COLOR_TREE_SUBMODULE 5
408 #define TOG_COLOR_TREE_SYMLINK 6
409 #define TOG_COLOR_TREE_DIRECTORY 7
410 #define TOG_COLOR_TREE_EXECUTABLE 8
411 #define TOG_COLOR_COMMIT 9
412 #define TOG_COLOR_AUTHOR 10
413 #define TOG_COLOR_DATE 11
414 #define TOG_COLOR_REFS_HEADS 12
415 #define TOG_COLOR_REFS_TAGS 13
416 #define TOG_COLOR_REFS_REMOTES 14
417 #define TOG_COLOR_REFS_BACKUP 15
419 struct tog_blame_cb_args {
420 struct tog_blame_line *lines; /* one per line */
423 struct tog_view *view;
424 struct got_object_id *commit_id;
428 struct tog_blame_thread_args {
430 struct got_repository *repo;
431 struct tog_blame_cb_args *cb_args;
433 got_cancel_cb cancel_cb;
435 pthread_cond_t blame_complete;
441 struct tog_blame_line *lines;
445 struct tog_blame_thread_args thread_args;
446 struct tog_blame_cb_args cb_args;
451 struct tog_blame_view_state {
452 int first_displayed_line;
453 int last_displayed_line;
455 int last_diffed_line;
459 struct got_object_id_queue blamed_commits;
460 struct got_object_qid *blamed_commit;
462 struct got_repository *repo;
463 struct got_object_id *commit_id;
464 struct got_object_id *id_to_log;
465 struct tog_blame blame;
467 struct tog_colors colors;
470 struct tog_parent_tree {
471 TAILQ_ENTRY(tog_parent_tree) entry;
472 struct got_tree_object *tree;
473 struct got_tree_entry *first_displayed_entry;
474 struct got_tree_entry *selected_entry;
478 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
480 struct tog_tree_view_state {
482 struct got_object_id *commit_id;/* commit which this tree belongs to */
483 struct got_tree_object *root; /* the commit's root tree entry */
484 struct got_tree_object *tree; /* currently displayed (sub-)tree */
485 struct got_tree_entry *first_displayed_entry;
486 struct got_tree_entry *last_displayed_entry;
487 struct got_tree_entry *selected_entry;
488 int ndisplayed, selected, show_ids;
489 struct tog_parent_trees parents; /* parent trees of current sub-tree */
491 struct got_repository *repo;
492 struct got_tree_entry *matched_entry;
493 struct tog_colors colors;
496 struct tog_reflist_entry {
497 TAILQ_ENTRY(tog_reflist_entry) entry;
498 struct got_reference *ref;
502 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
504 struct tog_ref_view_state {
505 struct tog_reflist_head refs;
506 struct tog_reflist_entry *first_displayed_entry;
507 struct tog_reflist_entry *last_displayed_entry;
508 struct tog_reflist_entry *selected_entry;
509 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
510 struct got_repository *repo;
511 struct tog_reflist_entry *matched_entry;
512 struct tog_colors colors;
515 struct tog_help_view_state {
520 int first_displayed_line;
521 int last_displayed_line;
526 enum tog_keymap_type type;
529 #define GENERATE_HELP \
530 KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
531 KEY_("H F1", "Open view-specific help (double tap for all help)"), \
532 KEY_("k C-p Up", "Move cursor or page up one line"), \
533 KEY_("j C-n Down", "Move cursor or page down one line"), \
534 KEY_("C-b b PgUp", "Scroll the view up one page"), \
535 KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
536 KEY_("C-u u", "Scroll the view up one half page"), \
537 KEY_("C-d d", "Scroll the view down one half page"), \
538 KEY_("g", "Go to line N (default: first line)"), \
539 KEY_("Home =", "Go to the first line"), \
540 KEY_("G", "Go to line N (default: last line)"), \
541 KEY_("End *", "Go to the last line"), \
542 KEY_("l Right", "Scroll the view right"), \
543 KEY_("h Left", "Scroll the view left"), \
544 KEY_("$", "Scroll view to the rightmost position"), \
545 KEY_("0", "Scroll view to the leftmost position"), \
546 KEY_("-", "Decrease size of the focussed split"), \
547 KEY_("+", "Increase size of the focussed split"), \
548 KEY_("Tab", "Switch focus between views"), \
549 KEY_("F", "Toggle fullscreen mode"), \
550 KEY_("S", "Switch split-screen layout"), \
551 KEY_("/", "Open prompt to enter search term"), \
552 KEY_("n", "Find next line/token matching the current search term"), \
553 KEY_("N", "Find previous line/token matching the current search term"),\
554 KEY_("q", "Quit the focussed view; Quit help screen"), \
555 KEY_("Q", "Quit tog"), \
557 KEYMAP_("Log view", TOG_KEYMAP_LOG), \
558 KEY_("< ,", "Move cursor up one commit"), \
559 KEY_("> .", "Move cursor down one commit"), \
560 KEY_("Enter", "Open diff view of the selected commit"), \
561 KEY_("B", "Reload the log view and toggle display of merged commits"), \
562 KEY_("R", "Open ref view of all repository references"), \
563 KEY_("T", "Display tree view of the repository from the selected" \
565 KEY_("@", "Toggle between displaying author and committer name"), \
566 KEY_("&", "Open prompt to enter term to limit commits displayed"), \
567 KEY_("C-g Backspace", "Cancel current search or log operation"), \
568 KEY_("C-l", "Reload the log view with new commits in the repository"), \
570 KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
571 KEY_("K < ,", "Display diff of next line in the file/log entry"), \
572 KEY_("J > .", "Display diff of previous line in the file/log entry"), \
573 KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
574 KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
576 KEY_("(", "Go to the previous file in the diff"), \
577 KEY_(")", "Go to the next file in the diff"), \
578 KEY_("{", "Go to the previous hunk in the diff"), \
579 KEY_("}", "Go to the next hunk in the diff"), \
580 KEY_("[", "Decrease the number of context lines"), \
581 KEY_("]", "Increase the number of context lines"), \
582 KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
584 KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
585 KEY_("Enter", "Display diff view of the selected line's commit"), \
586 KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
587 KEY_("L", "Open log view for the currently selected annotated line"), \
588 KEY_("C", "Reload view with the previously blamed commit"), \
589 KEY_("c", "Reload view with the version of the file found in the" \
590 " selected line's commit"), \
591 KEY_("p", "Reload view with the version of the file found in the" \
592 " selected line's parent commit"), \
594 KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
595 KEY_("Enter", "Enter selected directory or open blame view of the" \
597 KEY_("L", "Open log view for the selected entry"), \
598 KEY_("R", "Open ref view of all repository references"), \
599 KEY_("i", "Show object IDs for all tree entries"), \
600 KEY_("Backspace", "Return to the parent directory"), \
602 KEYMAP_("Ref view", TOG_KEYMAP_REF), \
603 KEY_("Enter", "Display log view of the selected reference"), \
604 KEY_("T", "Display tree view of the selected reference"), \
605 KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
606 KEY_("m", "Toggle display of last modified date for each reference"), \
607 KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
608 KEY_("C-l", "Reload view with all repository references")
613 enum tog_keymap_type type;
616 /* curses io for tog regress */
623 static int using_mock_io;
625 #define TOG_KEY_SCRDUMP SHRT_MIN
628 * We implement two types of views: parent views and child views.
630 * The 'Tab' key switches focus between a parent view and its child view.
631 * Child views are shown side-by-side to their parent view, provided
632 * there is enough screen estate.
634 * When a new view is opened from within a parent view, this new view
635 * becomes a child view of the parent view, replacing any existing child.
637 * When a new view is opened from within a child view, this new view
638 * becomes a parent view which will obscure the views below until the
639 * user quits the new parent view by typing 'q'.
641 * This list of views contains parent views only.
642 * Child views are only pointed to by their parent view.
644 TAILQ_HEAD(tog_view_list_head, tog_view);
647 TAILQ_ENTRY(tog_view) entry;
650 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
651 int resized_y, resized_x; /* begin_y/x based on user resizing */
652 int maxx, x; /* max column and current start column */
653 int lines, cols; /* copies of LINES and COLS */
654 int nscrolled, offset; /* lines scrolled and hsplit line offset */
655 int gline, hiline; /* navigate to and highlight this nG line */
656 int ch, count; /* current keymap and count prefix */
657 int resized; /* set when in a resize event */
658 int focussed; /* Only set on one parent or child view at a time. */
660 struct tog_view *parent;
661 struct tog_view *child;
664 * This flag is initially set on parent views when a new child view
665 * is created. It gets toggled when the 'Tab' key switches focus
666 * between parent and child.
667 * The flag indicates whether focus should be passed on to our child
668 * view if this parent view gets picked for focus after another parent
669 * view was closed. This prevents child views from losing focus in such
674 enum tog_view_mode mode;
675 /* type-specific state */
676 enum tog_view_type type;
678 struct tog_diff_view_state diff;
679 struct tog_log_view_state log;
680 struct tog_blame_view_state blame;
681 struct tog_tree_view_state tree;
682 struct tog_ref_view_state ref;
683 struct tog_help_view_state help;
686 const struct got_error *(*show)(struct tog_view *);
687 const struct got_error *(*input)(struct tog_view **,
688 struct tog_view *, int);
689 const struct got_error *(*reset)(struct tog_view *);
690 const struct got_error *(*resize)(struct tog_view *, int);
691 const struct got_error *(*close)(struct tog_view *);
693 const struct got_error *(*search_start)(struct tog_view *);
694 const struct got_error *(*search_next)(struct tog_view *);
695 void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
696 int **, int **, int **, int **);
699 #define TOG_SEARCH_FORWARD 1
700 #define TOG_SEARCH_BACKWARD 2
701 int search_next_done;
702 #define TOG_SEARCH_HAVE_MORE 1
703 #define TOG_SEARCH_NO_MORE 2
704 #define TOG_SEARCH_HAVE_NONE 3
710 static const struct got_error *open_diff_view(struct tog_view *,
711 struct got_object_id *, struct got_object_id *,
712 const char *, const char *, int, int, int, struct tog_view *,
713 struct got_repository *);
714 static const struct got_error *show_diff_view(struct tog_view *);
715 static const struct got_error *input_diff_view(struct tog_view **,
716 struct tog_view *, int);
717 static const struct got_error *reset_diff_view(struct tog_view *);
718 static const struct got_error* close_diff_view(struct tog_view *);
719 static const struct got_error *search_start_diff_view(struct tog_view *);
720 static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
721 size_t *, int **, int **, int **, int **);
722 static const struct got_error *search_next_view_match(struct tog_view *);
724 static const struct got_error *open_log_view(struct tog_view *,
725 struct got_object_id *, struct got_repository *,
726 const char *, const char *, int);
727 static const struct got_error * show_log_view(struct tog_view *);
728 static const struct got_error *input_log_view(struct tog_view **,
729 struct tog_view *, int);
730 static const struct got_error *resize_log_view(struct tog_view *, int);
731 static const struct got_error *close_log_view(struct tog_view *);
732 static const struct got_error *search_start_log_view(struct tog_view *);
733 static const struct got_error *search_next_log_view(struct tog_view *);
735 static const struct got_error *open_blame_view(struct tog_view *, char *,
736 struct got_object_id *, struct got_repository *);
737 static const struct got_error *show_blame_view(struct tog_view *);
738 static const struct got_error *input_blame_view(struct tog_view **,
739 struct tog_view *, int);
740 static const struct got_error *reset_blame_view(struct tog_view *);
741 static const struct got_error *close_blame_view(struct tog_view *);
742 static const struct got_error *search_start_blame_view(struct tog_view *);
743 static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
744 size_t *, int **, int **, int **, int **);
746 static const struct got_error *open_tree_view(struct tog_view *,
747 struct got_object_id *, const char *, struct got_repository *);
748 static const struct got_error *show_tree_view(struct tog_view *);
749 static const struct got_error *input_tree_view(struct tog_view **,
750 struct tog_view *, int);
751 static const struct got_error *close_tree_view(struct tog_view *);
752 static const struct got_error *search_start_tree_view(struct tog_view *);
753 static const struct got_error *search_next_tree_view(struct tog_view *);
755 static const struct got_error *open_ref_view(struct tog_view *,
756 struct got_repository *);
757 static const struct got_error *show_ref_view(struct tog_view *);
758 static const struct got_error *input_ref_view(struct tog_view **,
759 struct tog_view *, int);
760 static const struct got_error *close_ref_view(struct tog_view *);
761 static const struct got_error *search_start_ref_view(struct tog_view *);
762 static const struct got_error *search_next_ref_view(struct tog_view *);
764 static const struct got_error *open_help_view(struct tog_view *,
766 static const struct got_error *show_help_view(struct tog_view *);
767 static const struct got_error *input_help_view(struct tog_view **,
768 struct tog_view *, int);
769 static const struct got_error *reset_help_view(struct tog_view *);
770 static const struct got_error* close_help_view(struct tog_view *);
771 static const struct got_error *search_start_help_view(struct tog_view *);
772 static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
773 size_t *, int **, int **, int **, int **);
775 static volatile sig_atomic_t tog_sigwinch_received;
776 static volatile sig_atomic_t tog_sigpipe_received;
777 static volatile sig_atomic_t tog_sigcont_received;
778 static volatile sig_atomic_t tog_sigint_received;
779 static volatile sig_atomic_t tog_sigterm_received;
782 tog_sigwinch(int signo)
784 tog_sigwinch_received = 1;
788 tog_sigpipe(int signo)
790 tog_sigpipe_received = 1;
794 tog_sigcont(int signo)
796 tog_sigcont_received = 1;
800 tog_sigint(int signo)
802 tog_sigint_received = 1;
806 tog_sigterm(int signo)
808 tog_sigterm_received = 1;
812 tog_fatal_signal_received(void)
814 return (tog_sigpipe_received ||
815 tog_sigint_received || tog_sigterm_received);
818 static const struct got_error *
819 view_close(struct tog_view *view)
821 const struct got_error *err = NULL, *child_err = NULL;
824 child_err = view_close(view->child);
828 err = view->close(view);
830 del_panel(view->panel);
832 delwin(view->window);
834 return err ? err : child_err;
837 static struct tog_view *
838 view_open(int nlines, int ncols, int begin_y, int begin_x,
839 enum tog_view_type type)
841 struct tog_view *view = calloc(1, sizeof(*view));
849 view->nlines = nlines ? nlines : LINES - begin_y;
850 view->ncols = ncols ? ncols : COLS - begin_x;
851 view->begin_y = begin_y;
852 view->begin_x = begin_x;
853 view->window = newwin(nlines, ncols, begin_y, begin_x);
854 if (view->window == NULL) {
858 view->panel = new_panel(view->window);
859 if (view->panel == NULL ||
860 set_panel_userptr(view->panel, view) != OK) {
865 keypad(view->window, TRUE);
870 view_split_begin_x(int begin_x)
872 if (begin_x > 0 || COLS < 120)
874 return (COLS - MAX(COLS / 2, 80));
877 /* XXX Stub till we decide what to do. */
879 view_split_begin_y(int lines)
881 return lines * HSPLIT_SCALE;
884 static const struct got_error *view_resize(struct tog_view *);
886 static const struct got_error *
887 view_splitscreen(struct tog_view *view)
889 const struct got_error *err = NULL;
891 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
892 if (view->resized_y && view->resized_y < view->lines)
893 view->begin_y = view->resized_y;
895 view->begin_y = view_split_begin_y(view->nlines);
897 } else if (!view->resized) {
898 if (view->resized_x && view->resized_x < view->cols - 1 &&
900 view->begin_x = view->resized_x;
902 view->begin_x = view_split_begin_x(0);
905 view->nlines = LINES - view->begin_y;
906 view->ncols = COLS - view->begin_x;
909 err = view_resize(view);
913 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
914 view->parent->nlines = view->begin_y;
916 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
917 return got_error_from_errno("mvwin");
922 static const struct got_error *
923 view_fullscreen(struct tog_view *view)
925 const struct got_error *err = NULL;
928 view->begin_y = view->resized ? view->begin_y : 0;
929 view->nlines = view->resized ? view->nlines : LINES;
933 err = view_resize(view);
937 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
938 return got_error_from_errno("mvwin");
944 view_is_parent_view(struct tog_view *view)
946 return view->parent == NULL;
950 view_is_splitscreen(struct tog_view *view)
952 return view->begin_x > 0 || view->begin_y > 0;
956 view_is_fullscreen(struct tog_view *view)
958 return view->nlines == LINES && view->ncols == COLS;
962 view_is_hsplit_top(struct tog_view *view)
964 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
965 view_is_splitscreen(view->child);
969 view_border(struct tog_view *view)
972 const struct tog_view *view_above;
975 return view_border(view->parent);
977 panel = panel_above(view->panel);
981 view_above = panel_userptr(panel);
982 if (view->mode == TOG_VIEW_SPLIT_HRZN)
983 mvwhline(view->window, view_above->begin_y - 1,
984 view->begin_x, ACS_HLINE, view->ncols);
986 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
987 ACS_VLINE, view->nlines);
990 static const struct got_error *view_init_hsplit(struct tog_view *, int);
991 static const struct got_error *request_log_commits(struct tog_view *);
992 static const struct got_error *offset_selection_down(struct tog_view *);
993 static void offset_selection_up(struct tog_view *);
994 static void view_get_split(struct tog_view *, int *, int *);
996 static const struct got_error *
997 view_resize(struct tog_view *view)
999 const struct got_error *err = NULL;
1000 int dif, nlines, ncols;
1002 dif = LINES - view->lines; /* line difference */
1004 if (view->lines > LINES)
1005 nlines = view->nlines - (view->lines - LINES);
1007 nlines = view->nlines + (LINES - view->lines);
1008 if (view->cols > COLS)
1009 ncols = view->ncols - (view->cols - COLS);
1011 ncols = view->ncols + (COLS - view->cols);
1014 int hs = view->child->begin_y;
1016 if (!view_is_fullscreen(view))
1017 view->child->begin_x = view_split_begin_x(view->begin_x);
1018 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1019 view->child->begin_x == 0) {
1022 view_fullscreen(view->child);
1023 if (view->child->focussed)
1024 show_panel(view->child->panel);
1026 show_panel(view->panel);
1028 ncols = view->child->begin_x;
1030 view_splitscreen(view->child);
1031 show_panel(view->child->panel);
1034 * XXX This is ugly and needs to be moved into the above
1035 * logic but "works" for now and my attempts at moving it
1036 * break either 'tab' or 'F' key maps in horizontal splits.
1039 err = view_splitscreen(view->child);
1042 if (dif < 0) { /* top split decreased */
1043 err = offset_selection_down(view);
1050 show_panel(view->child->panel);
1051 nlines = view->nlines;
1053 } else if (view->parent == NULL)
1056 if (view->resize && dif > 0) {
1057 err = view->resize(view, dif);
1062 if (wresize(view->window, nlines, ncols) == ERR)
1063 return got_error_from_errno("wresize");
1064 if (replace_panel(view->panel, view->window) == ERR)
1065 return got_error_from_errno("replace_panel");
1066 wclear(view->window);
1068 view->nlines = nlines;
1069 view->ncols = ncols;
1070 view->lines = LINES;
1076 static const struct got_error *
1077 resize_log_view(struct tog_view *view, int increase)
1079 struct tog_log_view_state *s = &view->state.log;
1080 const struct got_error *err = NULL;
1083 if (s->selected_entry)
1084 n = s->selected_entry->idx + view->lines - s->selected;
1087 * Request commits to account for the increased
1088 * height so we have enough to populate the view.
1090 if (s->commits->ncommits < n) {
1091 view->nscrolled = n - s->commits->ncommits + increase + 1;
1092 err = request_log_commits(view);
1099 view_adjust_offset(struct tog_view *view, int n)
1104 if (view->parent && view->parent->offset) {
1105 if (view->parent->offset + n >= 0)
1106 view->parent->offset += n;
1108 view->parent->offset = 0;
1109 } else if (view->offset) {
1110 if (view->offset - n >= 0)
1117 static const struct got_error *
1118 view_resize_split(struct tog_view *view, int resize)
1120 const struct got_error *err = NULL;
1121 struct tog_view *v = NULL;
1128 if (!v->child || !view_is_splitscreen(v->child))
1131 v->resized = v->child->resized = resize; /* lock for resize event */
1133 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1134 if (v->child->resized_y)
1135 v->child->begin_y = v->child->resized_y;
1137 v->child->begin_y -= resize;
1139 v->child->begin_y += resize;
1140 if (v->child->begin_y < 3) {
1142 v->child->begin_y = 3;
1143 } else if (v->child->begin_y > LINES - 1) {
1145 v->child->begin_y = LINES - 1;
1148 v->child->ncols = COLS;
1149 view_adjust_offset(view, resize);
1150 err = view_init_hsplit(v, v->child->begin_y);
1153 v->child->resized_y = v->child->begin_y;
1155 if (v->child->resized_x)
1156 v->child->begin_x = v->child->resized_x;
1158 v->child->begin_x -= resize;
1160 v->child->begin_x += resize;
1161 if (v->child->begin_x < 11) {
1163 v->child->begin_x = 11;
1164 } else if (v->child->begin_x > COLS - 1) {
1166 v->child->begin_x = COLS - 1;
1168 v->child->resized_x = v->child->begin_x;
1171 v->child->mode = v->mode;
1172 v->child->nlines = v->lines - v->child->begin_y;
1173 v->child->ncols = v->cols - v->child->begin_x;
1176 err = view_fullscreen(v);
1179 err = view_splitscreen(v->child);
1183 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1184 err = offset_selection_down(v->child);
1190 err = v->resize(v, 0);
1191 else if (v->child->resize)
1192 err = v->child->resize(v->child, 0);
1194 v->resized = v->child->resized = 0;
1200 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1202 struct tog_view *v = src->child ? src->child : src;
1204 dst->resized_x = v->resized_x;
1205 dst->resized_y = v->resized_y;
1208 static const struct got_error *
1209 view_close_child(struct tog_view *view)
1211 const struct got_error *err = NULL;
1213 if (view->child == NULL)
1216 err = view_close(view->child);
1221 static const struct got_error *
1222 view_set_child(struct tog_view *view, struct tog_view *child)
1224 const struct got_error *err = NULL;
1226 view->child = child;
1227 child->parent = view;
1229 err = view_resize(view);
1233 if (view->child->resized_x || view->child->resized_y)
1234 err = view_resize_split(view, 0);
1239 static const struct got_error *view_dispatch_request(struct tog_view **,
1240 struct tog_view *, enum tog_view_type, int, int);
1242 static const struct got_error *
1243 view_request_new(struct tog_view **requested, struct tog_view *view,
1244 enum tog_view_type request)
1246 struct tog_view *new_view = NULL;
1247 const struct got_error *err;
1252 if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1253 view_get_split(view, &y, &x);
1255 err = view_dispatch_request(&new_view, view, request, y, x);
1259 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1260 request != TOG_VIEW_HELP) {
1261 err = view_init_hsplit(view, y);
1267 new_view->focussed = 1;
1268 new_view->mode = view->mode;
1269 new_view->nlines = request == TOG_VIEW_HELP ?
1270 view->lines : view->lines - y;
1272 if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1273 view_transfer_size(new_view, view);
1274 err = view_close_child(view);
1277 err = view_set_child(view, new_view);
1280 view->focus_child = 1;
1282 *requested = new_view;
1288 tog_resizeterm(void)
1291 struct winsize size;
1293 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1294 cols = 80; /* Default */
1298 lines = size.ws_row;
1300 resize_term(lines, cols);
1303 static const struct got_error *
1304 view_search_start(struct tog_view *view, int fast_refresh)
1306 const struct got_error *err = NULL;
1307 struct tog_view *v = view;
1311 if (view->search_started) {
1312 regfree(&view->regex);
1313 view->searching = 0;
1314 memset(&view->regmatch, 0, sizeof(view->regmatch));
1316 view->search_started = 0;
1318 if (view->nlines < 1)
1321 if (view_is_hsplit_top(view))
1323 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1326 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1327 wclrtoeol(v->window);
1329 nodelay(v->window, FALSE); /* block for search term input */
1332 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1333 wrefresh(v->window);
1336 nodelay(v->window, TRUE);
1337 if (!fast_refresh && !using_mock_io)
1342 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1343 err = view->search_start(view);
1345 regfree(&view->regex);
1348 view->search_started = 1;
1349 view->searching = TOG_SEARCH_FORWARD;
1350 view->search_next_done = 0;
1351 view->search_next(view);
1357 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1358 static const struct got_error *
1359 switch_split(struct tog_view *view)
1361 const struct got_error *err = NULL;
1362 struct tog_view *v = NULL;
1369 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1370 v->mode = TOG_VIEW_SPLIT_VERT;
1372 v->mode = TOG_VIEW_SPLIT_HRZN;
1376 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1377 v->mode = TOG_VIEW_SPLIT_NONE;
1379 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1380 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1381 v->child->begin_y = v->child->resized_y;
1382 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1383 v->child->begin_x = v->child->resized_x;
1386 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1388 v->child->ncols = COLS;
1389 v->child->nscrolled = LINES - v->child->nlines;
1391 err = view_init_hsplit(v, v->child->begin_y);
1395 v->child->mode = v->mode;
1396 v->child->nlines = v->lines - v->child->begin_y;
1399 err = view_fullscreen(v);
1402 err = view_splitscreen(v->child);
1406 if (v->mode == TOG_VIEW_SPLIT_NONE)
1407 v->mode = TOG_VIEW_SPLIT_VERT;
1408 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1409 err = offset_selection_down(v);
1412 err = offset_selection_down(v->child);
1416 offset_selection_up(v);
1417 offset_selection_up(v->child);
1420 err = v->resize(v, 0);
1421 else if (v->child->resize)
1422 err = v->child->resize(v->child, 0);
1428 * Strip trailing whitespace from str starting at byte *n;
1429 * if *n < 0, use strlen(str). Return new str length in *n.
1432 strip_trailing_ws(char *str, int *n)
1436 if (str == NULL || *str == '\0')
1442 while (x-- > 0 && isspace((unsigned char)str[x]))
1449 * Extract visible substring of line y from the curses screen
1450 * and strip trailing whitespace. If vline is set, overwrite
1451 * line[vline] with '|' because the ACS_VLINE character is
1452 * written out as 'x'. Write the line to file f.
1454 static const struct got_error *
1455 view_write_line(FILE *f, int y, int vline)
1457 char line[COLS * MB_LEN_MAX]; /* allow for multibyte chars */
1460 r = mvwinnstr(curscr, y, 0, line, sizeof(line));
1462 return got_error_fmt(GOT_ERR_RANGE,
1463 "failed to extract line %d", y);
1466 * In some views, lines are padded with blanks to COLS width.
1467 * Strip them so we can diff without the -b flag when testing.
1469 strip_trailing_ws(line, &r);
1474 w = fprintf(f, "%s\n", line);
1475 if (w != r + 1) /* \n */
1476 return got_ferror(f, GOT_ERR_IO);
1482 * Capture the visible curses screen by writing each line to the
1483 * file at the path set via the TOG_SCR_DUMP environment variable.
1485 static const struct got_error *
1486 screendump(struct tog_view *view)
1488 const struct got_error *err;
1493 path = getenv("TOG_SCR_DUMP");
1494 if (path == NULL || *path == '\0')
1495 return got_error_msg(GOT_ERR_BAD_PATH,
1496 "TOG_SCR_DUMP path not set to capture screen dump");
1497 f = fopen(path, "wex");
1499 return got_error_from_errno_fmt("fopen: %s", path);
1501 if ((view->child && view->child->begin_x) ||
1502 (view->parent && view->begin_x)) {
1503 int ncols = view->child ? view->ncols : view->parent->ncols;
1505 /* vertical splitscreen */
1506 for (i = 0; i < view->nlines; ++i) {
1507 err = view_write_line(f, i, ncols - 1);
1514 /* fullscreen or horizontal splitscreen */
1515 if ((view->child && view->child->begin_y) ||
1516 (view->parent && view->begin_y)) /* hsplit */
1517 hline = view->child ?
1518 view->child->begin_y : view->begin_y;
1520 for (i = 0; i < view->lines; i++) {
1521 if (hline && i == hline - 1) {
1524 /* ACS_HLINE writes out as 'q', overwrite it */
1525 for (c = 0; c < view->cols; ++c)
1531 err = view_write_line(f, i, 0);
1538 if (f && fclose(f) == EOF && err == NULL)
1539 err = got_ferror(f, GOT_ERR_IO);
1544 * Compute view->count from numeric input. Assign total to view->count and
1545 * return first non-numeric key entered.
1548 get_compound_key(struct tog_view *view, int c)
1550 struct tog_view *v = view;
1553 if (view_is_hsplit_top(view))
1555 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1559 cbreak(); /* block for input */
1560 nodelay(view->window, FALSE);
1561 wmove(v->window, v->nlines - 1, 0);
1562 wclrtoeol(v->window);
1563 waddch(v->window, ':');
1566 x = getcurx(v->window);
1567 if (x != ERR && x < view->ncols) {
1568 waddch(v->window, c);
1569 wrefresh(v->window);
1573 * Don't overflow. Max valid request should be the greatest
1574 * between the longest and total lines; cap at 10 million.
1579 n = n * 10 + (c - '0');
1580 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1582 if (c == 'G' || c == 'g') { /* nG key map */
1583 view->gline = view->hiline = n;
1588 /* Massage excessive or inapplicable values at the input handler. */
1595 action_report(struct tog_view *view)
1597 struct tog_view *v = view;
1599 if (view_is_hsplit_top(view))
1601 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1604 wmove(v->window, v->nlines - 1, 0);
1605 wclrtoeol(v->window);
1606 wprintw(v->window, ":%s", view->action);
1607 wrefresh(v->window);
1610 * Clear action status report. Only clear in blame view
1611 * once annotating is complete, otherwise it's too fast.
1613 if (view->type == TOG_VIEW_BLAME) {
1614 if (view->state.blame.blame_complete)
1615 view->action = NULL;
1617 view->action = NULL;
1621 * Read the next line from the test script and assign
1622 * key instruction to *ch. If at EOF, set the *done flag.
1624 static const struct got_error *
1625 tog_read_script_key(FILE *script, struct tog_view *view, int *ch, int *done)
1627 const struct got_error *err = NULL;
1631 if (view->count && --view->count) {
1637 if (getline(&line, &linesz, script) == -1) {
1642 err = got_ferror(script, GOT_ERR_IO);
1647 if (strncasecmp(line, "WAIT_FOR_UI", 11) == 0)
1648 tog_io.wait_for_ui = 1;
1649 else if (strncasecmp(line, "KEY_ENTER", 9) == 0)
1651 else if (strncasecmp(line, "KEY_RIGHT", 9) == 0)
1653 else if (strncasecmp(line, "KEY_LEFT", 8) == 0)
1655 else if (strncasecmp(line, "KEY_DOWN", 8) == 0)
1657 else if (strncasecmp(line, "KEY_UP", 6) == 0)
1659 else if (strncasecmp(line, "SCREENDUMP", 10) == 0)
1660 *ch = TOG_KEY_SCRDUMP;
1661 else if (isdigit((unsigned char)*line)) {
1664 while (isdigit((unsigned char)*t))
1666 view->ch = *ch = *t;
1668 /* ignore error, view->count is 0 if instruction is invalid */
1669 view->count = strtonum(line, 0, INT_MAX, NULL);
1678 static const struct got_error *
1679 view_input(struct tog_view **new, int *done, struct tog_view *view,
1680 struct tog_view_list_head *views, int fast_refresh)
1682 const struct got_error *err = NULL;
1689 action_report(view);
1691 /* Clear "no matches" indicator. */
1692 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1693 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1694 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1698 if (view->searching && !view->search_next_done) {
1699 errcode = pthread_mutex_unlock(&tog_mutex);
1701 return got_error_set_errno(errcode,
1702 "pthread_mutex_unlock");
1704 errcode = pthread_mutex_lock(&tog_mutex);
1706 return got_error_set_errno(errcode,
1707 "pthread_mutex_lock");
1708 view->search_next(view);
1712 /* Allow threads to make progress while we are waiting for input. */
1713 errcode = pthread_mutex_unlock(&tog_mutex);
1715 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1717 if (using_mock_io) {
1718 err = tog_read_script_key(tog_io.f, view, &ch, done);
1720 errcode = pthread_mutex_lock(&tog_mutex);
1723 } else if (view->count && --view->count) {
1725 nodelay(view->window, TRUE);
1726 ch = wgetch(view->window);
1727 /* let C-g or backspace abort unfinished count */
1728 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1733 ch = wgetch(view->window);
1734 if (ch >= '1' && ch <= '9')
1735 view->ch = ch = get_compound_key(view, ch);
1737 if (view->hiline && ch != ERR && ch != 0)
1738 view->hiline = 0; /* key pressed, clear line highlight */
1739 nodelay(view->window, TRUE);
1740 errcode = pthread_mutex_lock(&tog_mutex);
1742 return got_error_set_errno(errcode, "pthread_mutex_lock");
1744 if (tog_sigwinch_received || tog_sigcont_received) {
1746 tog_sigwinch_received = 0;
1747 tog_sigcont_received = 0;
1748 TAILQ_FOREACH(v, views, entry) {
1749 err = view_resize(v);
1752 err = v->input(new, v, KEY_RESIZE);
1756 err = view_resize(v->child);
1759 err = v->child->input(new, v->child,
1763 if (v->child->resized_x || v->child->resized_y) {
1764 err = view_resize_split(v, 0);
1776 if (view->type == TOG_VIEW_HELP)
1777 err = view->reset(view);
1779 err = view_request_new(new, view, TOG_VIEW_HELP);
1785 view->child->focussed = 1;
1786 view->focus_child = 1;
1787 } else if (view->parent) {
1789 view->parent->focussed = 1;
1790 view->parent->focus_child = 0;
1791 if (!view_is_splitscreen(view)) {
1792 if (view->parent->resize) {
1793 err = view->parent->resize(view->parent,
1798 offset_selection_up(view->parent);
1799 err = view_fullscreen(view->parent);
1806 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1807 if (view->parent->resize) {
1808 /* might need more commits to fill fullscreen */
1809 err = view->parent->resize(view->parent, 0);
1813 offset_selection_up(view->parent);
1815 err = view->input(new, view, ch);
1823 if (view_is_parent_view(view)) {
1824 if (view->child == NULL)
1826 if (view_is_splitscreen(view->child)) {
1828 view->child->focussed = 1;
1829 err = view_fullscreen(view->child);
1831 err = view_splitscreen(view->child);
1833 err = view_resize_split(view, 0);
1837 err = view->child->input(new, view->child,
1840 if (view_is_splitscreen(view)) {
1841 view->parent->focussed = 0;
1843 err = view_fullscreen(view);
1845 err = view_splitscreen(view);
1846 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1847 err = view_resize(view->parent);
1849 err = view_resize_split(view, 0);
1853 err = view->input(new, view, KEY_RESIZE);
1858 err = view->resize(view, 0);
1863 err = offset_selection_down(view->parent);
1865 err = offset_selection_down(view);
1869 err = switch_split(view);
1872 err = view_resize_split(view, -1);
1875 err = view_resize_split(view, 1);
1881 if (view->search_start)
1882 view_search_start(view, fast_refresh);
1884 err = view->input(new, view, ch);
1888 if (view->search_started && view->search_next) {
1889 view->searching = (ch == 'n' ?
1890 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1891 view->search_next_done = 0;
1892 view->search_next(view);
1894 err = view->input(new, view, ch);
1897 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1898 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1899 view->action = "Patience diff algorithm";
1901 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1902 view->action = "Myers diff algorithm";
1904 TAILQ_FOREACH(v, views, entry) {
1910 if (v->child && v->child->reset) {
1911 err = v->child->reset(v->child);
1917 case TOG_KEY_SCRDUMP:
1918 err = screendump(view);
1921 err = view->input(new, view, ch);
1929 view_needs_focus_indication(struct tog_view *view)
1931 if (view_is_parent_view(view)) {
1932 if (view->child == NULL || view->child->focussed)
1934 if (!view_is_splitscreen(view->child))
1936 } else if (!view_is_splitscreen(view))
1939 return view->focussed;
1942 static const struct got_error *
1945 const struct got_error *err = NULL;
1947 if (tog_io.cin && fclose(tog_io.cin) == EOF)
1948 err = got_ferror(tog_io.cin, GOT_ERR_IO);
1949 if (tog_io.cout && fclose(tog_io.cout) == EOF && err == NULL)
1950 err = got_ferror(tog_io.cout, GOT_ERR_IO);
1951 if (tog_io.f && fclose(tog_io.f) == EOF && err == NULL)
1952 err = got_ferror(tog_io.f, GOT_ERR_IO);
1957 static const struct got_error *
1958 view_loop(struct tog_view *view)
1960 const struct got_error *err = NULL;
1961 struct tog_view_list_head views;
1962 struct tog_view *new_view;
1964 int fast_refresh = 10;
1965 int done = 0, errcode;
1967 mode = getenv("TOG_VIEW_SPLIT_MODE");
1968 if (!mode || !(*mode == 'h' || *mode == 'H'))
1969 view->mode = TOG_VIEW_SPLIT_VERT;
1971 view->mode = TOG_VIEW_SPLIT_HRZN;
1973 errcode = pthread_mutex_lock(&tog_mutex);
1975 return got_error_set_errno(errcode, "pthread_mutex_lock");
1978 TAILQ_INSERT_HEAD(&views, view, entry);
1981 err = view->show(view);
1986 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1987 !tog_fatal_signal_received()) {
1988 /* Refresh fast during initialization, then become slower. */
1989 if (fast_refresh && --fast_refresh == 0 && !using_mock_io)
1990 halfdelay(10); /* switch to once per second */
1992 err = view_input(&new_view, &done, view, &views, fast_refresh);
1996 if (view->dying && view == TAILQ_FIRST(&views) &&
1997 TAILQ_NEXT(view, entry) == NULL)
2003 * When we quit, scroll the screen up a single line
2004 * so we don't lose any information.
2006 TAILQ_FOREACH(v, &views, entry) {
2007 wmove(v->window, 0, 0);
2008 wdeleteln(v->window);
2009 wnoutrefresh(v->window);
2010 if (v->child && !view_is_fullscreen(v)) {
2011 wmove(v->child->window, 0, 0);
2012 wdeleteln(v->child->window);
2013 wnoutrefresh(v->child->window);
2020 struct tog_view *v, *prev = NULL;
2022 if (view_is_parent_view(view))
2023 prev = TAILQ_PREV(view, tog_view_list_head,
2025 else if (view->parent)
2026 prev = view->parent;
2029 view->parent->child = NULL;
2030 view->parent->focus_child = 0;
2031 /* Restore fullscreen line height. */
2032 view->parent->nlines = view->parent->lines;
2033 err = view_resize(view->parent);
2036 /* Make resized splits persist. */
2037 view_transfer_size(view->parent, view);
2039 TAILQ_REMOVE(&views, view, entry);
2041 err = view_close(view);
2046 TAILQ_FOREACH(v, &views, entry) {
2050 if (view == NULL && new_view == NULL) {
2051 /* No view has focus. Try to pick one. */
2054 else if (!TAILQ_EMPTY(&views)) {
2055 view = TAILQ_LAST(&views,
2056 tog_view_list_head);
2059 if (view->focus_child) {
2060 view->child->focussed = 1;
2068 struct tog_view *v, *t;
2069 /* Only allow one parent view per type. */
2070 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
2071 if (v->type != new_view->type)
2073 TAILQ_REMOVE(&views, v, entry);
2074 err = view_close(v);
2079 TAILQ_INSERT_TAIL(&views, new_view, entry);
2082 if (view && !done) {
2083 if (view_is_parent_view(view)) {
2084 if (view->child && view->child->focussed)
2087 if (view->parent && view->parent->focussed)
2088 view = view->parent;
2090 show_panel(view->panel);
2091 if (view->child && view_is_splitscreen(view->child))
2092 show_panel(view->child->panel);
2093 if (view->parent && view_is_splitscreen(view)) {
2094 err = view->parent->show(view->parent);
2098 err = view->show(view);
2102 err = view->child->show(view->child);
2111 while (!TAILQ_EMPTY(&views)) {
2112 const struct got_error *close_err;
2113 view = TAILQ_FIRST(&views);
2114 TAILQ_REMOVE(&views, view, entry);
2115 close_err = view_close(view);
2116 if (close_err && err == NULL)
2120 errcode = pthread_mutex_unlock(&tog_mutex);
2121 if (errcode && err == NULL)
2122 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2132 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
2137 /* Create newly allocated wide-character string equivalent to a byte string. */
2138 static const struct got_error *
2139 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
2142 const struct got_error *err = NULL;
2145 *wlen = mbstowcs(NULL, s, 0);
2146 if (*wlen == (size_t)-1) {
2148 if (errno != EILSEQ)
2149 return got_error_from_errno("mbstowcs");
2151 /* byte string invalid in current encoding; try to "fix" it */
2152 err = got_mbsavis(&vis, &vislen, s);
2155 *wlen = mbstowcs(NULL, vis, 0);
2156 if (*wlen == (size_t)-1) {
2157 err = got_error_from_errno("mbstowcs"); /* give up */
2162 *ws = calloc(*wlen + 1, sizeof(**ws));
2164 err = got_error_from_errno("calloc");
2168 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
2169 err = got_error_from_errno("mbstowcs");
2180 static const struct got_error *
2181 expand_tab(char **ptr, const char *src)
2184 size_t len, n, idx = 0, sz = 0;
2187 n = len = strlen(src);
2188 dst = malloc(n + 1);
2190 return got_error_from_errno("malloc");
2192 while (idx < len && src[idx]) {
2193 const char c = src[idx];
2196 size_t nb = TABSIZE - sz % TABSIZE;
2199 p = realloc(dst, n + nb);
2202 return got_error_from_errno("realloc");
2207 memset(dst + sz, ' ', nb);
2210 dst[sz++] = src[idx];
2220 * Advance at most n columns from wline starting at offset off.
2221 * Return the index to the first character after the span operation.
2222 * Return the combined column width of all spanned wide character in
2226 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2228 int width, i, cols = 0;
2235 for (i = off; wline[i] != L'\0'; ++i) {
2236 if (wline[i] == L'\t')
2237 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2239 width = wcwidth(wline[i]);
2246 if (cols + width > n)
2256 * Format a line for display, ensuring that it won't overflow a width limit.
2257 * With scrolling, the width returned refers to the scrolled version of the
2258 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2260 static const struct got_error *
2261 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2262 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2264 const struct got_error *err = NULL;
2266 wchar_t *wline = NULL;
2275 err = expand_tab(&exstr, line);
2280 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2285 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2287 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2288 wline[wlen - 1] = L'\0';
2291 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2292 wline[wlen - 1] = L'\0';
2296 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2302 *scrollxp = scrollx;
2310 static const struct got_error*
2311 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2312 struct got_object_id *id, struct got_repository *repo)
2314 static const struct got_error *err = NULL;
2315 struct got_reflist_entry *re;
2321 TAILQ_FOREACH(re, refs, entry) {
2322 struct got_tag_object *tag = NULL;
2323 struct got_object_id *ref_id;
2326 name = got_ref_get_name(re->ref);
2327 if (strcmp(name, GOT_REF_HEAD) == 0)
2329 if (strncmp(name, "refs/", 5) == 0)
2331 if (strncmp(name, "got/", 4) == 0 &&
2332 strncmp(name, "got/backup/", 11) != 0)
2334 if (strncmp(name, "heads/", 6) == 0)
2336 if (strncmp(name, "remotes/", 8) == 0) {
2338 s = strstr(name, "/" GOT_REF_HEAD);
2339 if (s != NULL && s[strlen(s)] == '\0')
2342 err = got_ref_resolve(&ref_id, repo, re->ref);
2345 if (strncmp(name, "tags/", 5) == 0) {
2346 err = got_object_open_as_tag(&tag, repo, ref_id);
2348 if (err->code != GOT_ERR_OBJ_TYPE) {
2352 /* Ref points at something other than a tag. */
2357 cmp = got_object_id_cmp(tag ?
2358 got_object_tag_get_object_id(tag) : ref_id, id);
2361 got_object_tag_close(tag);
2365 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2366 s ? ", " : "", name) == -1) {
2367 err = got_error_from_errno("asprintf");
2378 static const struct got_error *
2379 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2384 smallerthan = strchr(author, '<');
2385 if (smallerthan && smallerthan[1] != '\0')
2386 author = smallerthan + 1;
2387 author[strcspn(author, "@>")] = '\0';
2388 return format_line(wauthor, author_width, NULL, author, 0, limit,
2392 static const struct got_error *
2393 draw_commit(struct tog_view *view, struct got_commit_object *commit,
2394 struct got_object_id *id, const size_t date_display_cols,
2395 int author_display_cols)
2397 struct tog_log_view_state *s = &view->state.log;
2398 const struct got_error *err = NULL;
2399 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2400 char *logmsg0 = NULL, *logmsg = NULL;
2401 char *author = NULL;
2402 wchar_t *wlogmsg = NULL, *wauthor = NULL;
2403 int author_width, logmsg_width;
2404 char *newline, *line = NULL;
2405 int col, limit, scrollx;
2406 const int avail = view->ncols;
2408 time_t committer_time;
2409 struct tog_color *tc;
2411 committer_time = got_object_commit_get_committer_time(commit);
2412 if (gmtime_r(&committer_time, &tm) == NULL)
2413 return got_error_from_errno("gmtime_r");
2414 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2415 return got_error(GOT_ERR_NO_SPACE);
2417 if (avail <= date_display_cols)
2418 limit = MIN(sizeof(datebuf) - 1, avail);
2420 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2421 tc = get_color(&s->colors, TOG_COLOR_DATE);
2423 wattr_on(view->window,
2424 COLOR_PAIR(tc->colorpair), NULL);
2425 waddnstr(view->window, datebuf, limit);
2427 wattr_off(view->window,
2428 COLOR_PAIR(tc->colorpair), NULL);
2435 err = got_object_id_str(&id_str, id);
2438 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2440 wattr_on(view->window,
2441 COLOR_PAIR(tc->colorpair), NULL);
2442 wprintw(view->window, "%.8s ", id_str);
2444 wattr_off(view->window,
2445 COLOR_PAIR(tc->colorpair), NULL);
2452 if (s->use_committer)
2453 author = strdup(got_object_commit_get_committer(commit));
2455 author = strdup(got_object_commit_get_author(commit));
2456 if (author == NULL) {
2457 err = got_error_from_errno("strdup");
2460 err = format_author(&wauthor, &author_width, author, avail - col, col);
2463 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2465 wattr_on(view->window,
2466 COLOR_PAIR(tc->colorpair), NULL);
2467 waddwstr(view->window, wauthor);
2468 col += author_width;
2469 while (col < avail && author_width < author_display_cols + 2) {
2470 waddch(view->window, ' ');
2475 wattr_off(view->window,
2476 COLOR_PAIR(tc->colorpair), NULL);
2480 err = got_object_commit_get_logmsg(&logmsg0, commit);
2484 while (*logmsg == '\n')
2486 newline = strchr(logmsg, '\n');
2489 limit = avail - col;
2490 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2491 limit--; /* for the border */
2492 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2496 waddwstr(view->window, &wlogmsg[scrollx]);
2497 col += MAX(logmsg_width, 0);
2498 while (col < avail) {
2499 waddch(view->window, ' ');
2511 static struct commit_queue_entry *
2512 alloc_commit_queue_entry(struct got_commit_object *commit,
2513 struct got_object_id *id)
2515 struct commit_queue_entry *entry;
2516 struct got_object_id *dup;
2518 entry = calloc(1, sizeof(*entry));
2522 dup = got_object_id_dup(id);
2529 entry->commit = commit;
2534 pop_commit(struct commit_queue *commits)
2536 struct commit_queue_entry *entry;
2538 entry = TAILQ_FIRST(&commits->head);
2539 TAILQ_REMOVE(&commits->head, entry, entry);
2540 got_object_commit_close(entry->commit);
2541 commits->ncommits--;
2547 free_commits(struct commit_queue *commits)
2549 while (!TAILQ_EMPTY(&commits->head))
2550 pop_commit(commits);
2553 static const struct got_error *
2554 match_commit(int *have_match, struct got_object_id *id,
2555 struct got_commit_object *commit, regex_t *regex)
2557 const struct got_error *err = NULL;
2558 regmatch_t regmatch;
2559 char *id_str = NULL, *logmsg = NULL;
2563 err = got_object_id_str(&id_str, id);
2567 err = got_object_commit_get_logmsg(&logmsg, commit);
2571 if (regexec(regex, got_object_commit_get_author(commit), 1,
2572 ®match, 0) == 0 ||
2573 regexec(regex, got_object_commit_get_committer(commit), 1,
2574 ®match, 0) == 0 ||
2575 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2576 regexec(regex, logmsg, 1, ®match, 0) == 0)
2584 static const struct got_error *
2585 queue_commits(struct tog_log_thread_args *a)
2587 const struct got_error *err = NULL;
2590 * We keep all commits open throughout the lifetime of the log
2591 * view in order to avoid having to re-fetch commits from disk
2592 * while updating the display.
2595 struct got_object_id id;
2596 struct got_commit_object *commit;
2597 struct commit_queue_entry *entry;
2598 int limit_match = 0;
2601 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2606 err = got_object_open_as_commit(&commit, a->repo, &id);
2609 entry = alloc_commit_queue_entry(commit, &id);
2610 if (entry == NULL) {
2611 err = got_error_from_errno("alloc_commit_queue_entry");
2615 errcode = pthread_mutex_lock(&tog_mutex);
2617 err = got_error_set_errno(errcode,
2618 "pthread_mutex_lock");
2622 entry->idx = a->real_commits->ncommits;
2623 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2624 a->real_commits->ncommits++;
2627 err = match_commit(&limit_match, &id, commit,
2633 struct commit_queue_entry *matched;
2635 matched = alloc_commit_queue_entry(
2636 entry->commit, entry->id);
2637 if (matched == NULL) {
2638 err = got_error_from_errno(
2639 "alloc_commit_queue_entry");
2642 matched->commit = entry->commit;
2643 got_object_commit_retain(entry->commit);
2645 matched->idx = a->limit_commits->ncommits;
2646 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2648 a->limit_commits->ncommits++;
2652 * This is how we signal log_thread() that we
2653 * have found a match, and that it should be
2654 * counted as a new entry for the view.
2656 a->limit_match = limit_match;
2659 if (*a->searching == TOG_SEARCH_FORWARD &&
2660 !*a->search_next_done) {
2662 err = match_commit(&have_match, &id, commit, a->regex);
2667 if (limit_match && have_match)
2668 *a->search_next_done =
2669 TOG_SEARCH_HAVE_MORE;
2670 } else if (have_match)
2671 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2674 errcode = pthread_mutex_unlock(&tog_mutex);
2675 if (errcode && err == NULL)
2676 err = got_error_set_errno(errcode,
2677 "pthread_mutex_unlock");
2680 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2686 select_commit(struct tog_log_view_state *s)
2688 struct commit_queue_entry *entry;
2691 entry = s->first_displayed_entry;
2693 if (ncommits == s->selected) {
2694 s->selected_entry = entry;
2697 entry = TAILQ_NEXT(entry, entry);
2702 static const struct got_error *
2703 draw_commits(struct tog_view *view)
2705 const struct got_error *err = NULL;
2706 struct tog_log_view_state *s = &view->state.log;
2707 struct commit_queue_entry *entry = s->selected_entry;
2708 int limit = view->nlines;
2710 int ncommits, author_cols = 4;
2711 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2712 char *refs_str = NULL;
2714 struct tog_color *tc;
2715 static const size_t date_display_cols = 12;
2717 if (view_is_hsplit_top(view))
2718 --limit; /* account for border */
2720 if (s->selected_entry &&
2721 !(view->searching && view->search_next_done == 0)) {
2722 struct got_reflist_head *refs;
2723 err = got_object_id_str(&id_str, s->selected_entry->id);
2726 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2727 s->selected_entry->id);
2729 err = build_refs_str(&refs_str, refs,
2730 s->selected_entry->id, s->repo);
2736 if (s->thread_args.commits_needed == 0 && !using_mock_io)
2737 halfdelay(10); /* disable fast refresh */
2739 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2740 if (asprintf(&ncommits_str, " [%d/%d] %s",
2741 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2742 (view->searching && !view->search_next_done) ?
2743 "searching..." : "loading...") == -1) {
2744 err = got_error_from_errno("asprintf");
2748 const char *search_str = NULL;
2749 const char *limit_str = NULL;
2751 if (view->searching) {
2752 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2753 search_str = "no more matches";
2754 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2755 search_str = "no matches found";
2756 else if (!view->search_next_done)
2757 search_str = "searching...";
2760 if (s->limit_view && s->commits->ncommits == 0)
2761 limit_str = "no matches found";
2763 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2764 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2765 search_str ? search_str : (refs_str ? refs_str : ""),
2766 limit_str ? limit_str : "") == -1) {
2767 err = got_error_from_errno("asprintf");
2772 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2773 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2774 "........................................",
2775 s->in_repo_path, ncommits_str) == -1) {
2776 err = got_error_from_errno("asprintf");
2780 } else if (asprintf(&header, "commit %s%s",
2781 id_str ? id_str : "........................................",
2782 ncommits_str) == -1) {
2783 err = got_error_from_errno("asprintf");
2787 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2791 werase(view->window);
2793 if (view_needs_focus_indication(view))
2794 wstandout(view->window);
2795 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2797 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2798 waddwstr(view->window, wline);
2799 while (width < view->ncols) {
2800 waddch(view->window, ' ');
2804 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2805 if (view_needs_focus_indication(view))
2806 wstandend(view->window);
2811 /* Grow author column size if necessary, and set view->maxx. */
2812 entry = s->first_displayed_entry;
2816 struct got_commit_object *c = entry->commit;
2817 char *author, *eol, *msg, *msg0;
2818 wchar_t *wauthor, *wmsg;
2820 if (ncommits >= limit - 1)
2822 if (s->use_committer)
2823 author = strdup(got_object_commit_get_committer(c));
2825 author = strdup(got_object_commit_get_author(c));
2826 if (author == NULL) {
2827 err = got_error_from_errno("strdup");
2830 err = format_author(&wauthor, &width, author, COLS,
2832 if (author_cols < width)
2833 author_cols = width;
2838 err = got_object_commit_get_logmsg(&msg0, c);
2842 while (*msg == '\n')
2844 if ((eol = strchr(msg, '\n')))
2846 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2847 date_display_cols + author_cols, 0);
2850 view->maxx = MAX(view->maxx, width);
2854 entry = TAILQ_NEXT(entry, entry);
2857 entry = s->first_displayed_entry;
2858 s->last_displayed_entry = s->first_displayed_entry;
2861 if (ncommits >= limit - 1)
2863 if (ncommits == s->selected)
2864 wstandout(view->window);
2865 err = draw_commit(view, entry->commit, entry->id,
2866 date_display_cols, author_cols);
2867 if (ncommits == s->selected)
2868 wstandend(view->window);
2872 s->last_displayed_entry = entry;
2873 entry = TAILQ_NEXT(entry, entry);
2886 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2888 struct commit_queue_entry *entry;
2891 entry = TAILQ_FIRST(&s->commits->head);
2892 if (s->first_displayed_entry == entry)
2895 entry = s->first_displayed_entry;
2896 while (entry && nscrolled < maxscroll) {
2897 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2899 s->first_displayed_entry = entry;
2905 static const struct got_error *
2906 trigger_log_thread(struct tog_view *view, int wait)
2908 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2912 halfdelay(1); /* fast refresh while loading commits */
2914 while (!ta->log_complete && !tog_thread_error &&
2915 (ta->commits_needed > 0 || ta->load_all)) {
2916 /* Wake the log thread. */
2917 errcode = pthread_cond_signal(&ta->need_commits);
2919 return got_error_set_errno(errcode,
2920 "pthread_cond_signal");
2923 * The mutex will be released while the view loop waits
2924 * in wgetch(), at which time the log thread will run.
2929 /* Display progress update in log view. */
2930 show_log_view(view);
2934 /* Wait right here while next commit is being loaded. */
2935 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2937 return got_error_set_errno(errcode,
2938 "pthread_cond_wait");
2940 /* Display progress update in log view. */
2941 show_log_view(view);
2949 static const struct got_error *
2950 request_log_commits(struct tog_view *view)
2952 struct tog_log_view_state *state = &view->state.log;
2953 const struct got_error *err = NULL;
2955 if (state->thread_args.log_complete)
2958 state->thread_args.commits_needed += view->nscrolled;
2959 err = trigger_log_thread(view, 1);
2960 view->nscrolled = 0;
2965 static const struct got_error *
2966 log_scroll_down(struct tog_view *view, int maxscroll)
2968 struct tog_log_view_state *s = &view->state.log;
2969 const struct got_error *err = NULL;
2970 struct commit_queue_entry *pentry;
2971 int nscrolled = 0, ncommits_needed;
2973 if (s->last_displayed_entry == NULL)
2976 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2977 if (s->commits->ncommits < ncommits_needed &&
2978 !s->thread_args.log_complete) {
2980 * Ask the log thread for required amount of commits.
2982 s->thread_args.commits_needed +=
2983 ncommits_needed - s->commits->ncommits;
2984 err = trigger_log_thread(view, 1);
2990 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2991 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2994 s->last_displayed_entry = pentry ?
2995 pentry : s->last_displayed_entry;
2997 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
3000 s->first_displayed_entry = pentry;
3001 } while (++nscrolled < maxscroll);
3003 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
3004 view->nscrolled += nscrolled;
3006 view->nscrolled = 0;
3011 static const struct got_error *
3012 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
3013 struct got_commit_object *commit, struct got_object_id *commit_id,
3014 struct tog_view *log_view, struct got_repository *repo)
3016 const struct got_error *err;
3017 struct got_object_qid *parent_id;
3018 struct tog_view *diff_view;
3020 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
3021 if (diff_view == NULL)
3022 return got_error_from_errno("view_open");
3024 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3025 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
3026 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
3028 *new_view = diff_view;
3032 static const struct got_error *
3033 tree_view_visit_subtree(struct tog_tree_view_state *s,
3034 struct got_tree_object *subtree)
3036 struct tog_parent_tree *parent;
3038 parent = calloc(1, sizeof(*parent));
3040 return got_error_from_errno("calloc");
3042 parent->tree = s->tree;
3043 parent->first_displayed_entry = s->first_displayed_entry;
3044 parent->selected_entry = s->selected_entry;
3045 parent->selected = s->selected;
3046 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3049 s->first_displayed_entry = NULL;
3053 static const struct got_error *
3054 tree_view_walk_path(struct tog_tree_view_state *s,
3055 struct got_commit_object *commit, const char *path)
3057 const struct got_error *err = NULL;
3058 struct got_tree_object *tree = NULL;
3060 char *slash, *subpath = NULL;
3062 /* Walk the path and open corresponding tree objects. */
3065 struct got_tree_entry *te;
3066 struct got_object_id *tree_id;
3072 /* Ensure the correct subtree entry is selected. */
3073 slash = strchr(p, '/');
3075 te_name = strdup(p);
3077 te_name = strndup(p, slash - p);
3078 if (te_name == NULL) {
3079 err = got_error_from_errno("strndup");
3082 te = got_object_tree_find_entry(s->tree, te_name);
3084 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
3089 s->first_displayed_entry = s->selected_entry = te;
3091 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
3092 break; /* jump to this file's entry */
3094 slash = strchr(p, '/');
3096 subpath = strndup(path, slash - path);
3098 subpath = strdup(path);
3099 if (subpath == NULL) {
3100 err = got_error_from_errno("strdup");
3104 err = got_object_id_by_path(&tree_id, s->repo, commit,
3109 err = got_object_open_as_tree(&tree, s->repo, tree_id);
3114 err = tree_view_visit_subtree(s, tree);
3116 got_object_tree_close(tree);
3130 static const struct got_error *
3131 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
3132 struct commit_queue_entry *entry, const char *path,
3133 const char *head_ref_name, struct got_repository *repo)
3135 const struct got_error *err = NULL;
3136 struct tog_tree_view_state *s;
3137 struct tog_view *tree_view;
3139 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
3140 if (tree_view == NULL)
3141 return got_error_from_errno("view_open");
3143 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
3146 s = &tree_view->state.tree;
3148 *new_view = tree_view;
3150 if (got_path_is_root_dir(path))
3153 return tree_view_walk_path(s, entry->commit, path);
3156 static const struct got_error *
3157 block_signals_used_by_main_thread(void)
3162 if (sigemptyset(&sigset) == -1)
3163 return got_error_from_errno("sigemptyset");
3165 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
3166 if (sigaddset(&sigset, SIGWINCH) == -1)
3167 return got_error_from_errno("sigaddset");
3168 if (sigaddset(&sigset, SIGCONT) == -1)
3169 return got_error_from_errno("sigaddset");
3170 if (sigaddset(&sigset, SIGINT) == -1)
3171 return got_error_from_errno("sigaddset");
3172 if (sigaddset(&sigset, SIGTERM) == -1)
3173 return got_error_from_errno("sigaddset");
3175 /* ncurses handles SIGTSTP */
3176 if (sigaddset(&sigset, SIGTSTP) == -1)
3177 return got_error_from_errno("sigaddset");
3179 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
3181 return got_error_set_errno(errcode, "pthread_sigmask");
3187 log_thread(void *arg)
3189 const struct got_error *err = NULL;
3191 struct tog_log_thread_args *a = arg;
3195 * Sync startup with main thread such that we begin our
3196 * work once view_input() has released the mutex.
3198 errcode = pthread_mutex_lock(&tog_mutex);
3200 err = got_error_set_errno(errcode, "pthread_mutex_lock");
3204 err = block_signals_used_by_main_thread();
3206 pthread_mutex_unlock(&tog_mutex);
3210 while (!done && !err && !tog_fatal_signal_received()) {
3211 errcode = pthread_mutex_unlock(&tog_mutex);
3213 err = got_error_set_errno(errcode,
3214 "pthread_mutex_unlock");
3217 err = queue_commits(a);
3219 if (err->code != GOT_ERR_ITER_COMPLETED)
3223 } else if (a->commits_needed > 0 && !a->load_all) {
3226 a->commits_needed--;
3228 a->commits_needed--;
3231 errcode = pthread_mutex_lock(&tog_mutex);
3233 err = got_error_set_errno(errcode,
3234 "pthread_mutex_lock");
3236 } else if (*a->quit)
3238 else if (*a->limiting && *a->first_displayed_entry == NULL) {
3239 *a->first_displayed_entry =
3240 TAILQ_FIRST(&a->limit_commits->head);
3241 *a->selected_entry = *a->first_displayed_entry;
3242 } else if (*a->first_displayed_entry == NULL) {
3243 *a->first_displayed_entry =
3244 TAILQ_FIRST(&a->real_commits->head);
3245 *a->selected_entry = *a->first_displayed_entry;
3248 errcode = pthread_cond_signal(&a->commit_loaded);
3250 err = got_error_set_errno(errcode,
3251 "pthread_cond_signal");
3252 pthread_mutex_unlock(&tog_mutex);
3257 a->commits_needed = 0;
3259 if (a->commits_needed == 0 && !a->load_all) {
3260 errcode = pthread_cond_wait(&a->need_commits,
3263 err = got_error_set_errno(errcode,
3264 "pthread_cond_wait");
3265 pthread_mutex_unlock(&tog_mutex);
3273 a->log_complete = 1;
3274 errcode = pthread_mutex_unlock(&tog_mutex);
3276 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3279 tog_thread_error = 1;
3280 pthread_cond_signal(&a->commit_loaded);
3285 static const struct got_error *
3286 stop_log_thread(struct tog_log_view_state *s)
3288 const struct got_error *err = NULL, *thread_err = NULL;
3293 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3295 return got_error_set_errno(errcode,
3296 "pthread_cond_signal");
3297 errcode = pthread_mutex_unlock(&tog_mutex);
3299 return got_error_set_errno(errcode,
3300 "pthread_mutex_unlock");
3301 errcode = pthread_join(s->thread, (void **)&thread_err);
3303 return got_error_set_errno(errcode, "pthread_join");
3304 errcode = pthread_mutex_lock(&tog_mutex);
3306 return got_error_set_errno(errcode,
3307 "pthread_mutex_lock");
3311 if (s->thread_args.repo) {
3312 err = got_repo_close(s->thread_args.repo);
3313 s->thread_args.repo = NULL;
3316 if (s->thread_args.pack_fds) {
3317 const struct got_error *pack_err =
3318 got_repo_pack_fds_close(s->thread_args.pack_fds);
3321 s->thread_args.pack_fds = NULL;
3324 if (s->thread_args.graph) {
3325 got_commit_graph_close(s->thread_args.graph);
3326 s->thread_args.graph = NULL;
3329 return err ? err : thread_err;
3332 static const struct got_error *
3333 close_log_view(struct tog_view *view)
3335 const struct got_error *err = NULL;
3336 struct tog_log_view_state *s = &view->state.log;
3339 err = stop_log_thread(s);
3341 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3342 if (errcode && err == NULL)
3343 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3345 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3346 if (errcode && err == NULL)
3347 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3349 free_commits(&s->limit_commits);
3350 free_commits(&s->real_commits);
3351 free(s->in_repo_path);
3352 s->in_repo_path = NULL;
3355 free(s->head_ref_name);
3356 s->head_ref_name = NULL;
3361 * We use two queues to implement the limit feature: first consists of
3362 * commits matching the current limit_regex; second is the real queue
3363 * of all known commits (real_commits). When the user starts limiting,
3364 * we swap queues such that all movement and displaying functionality
3365 * works with very slight change.
3367 static const struct got_error *
3368 limit_log_view(struct tog_view *view)
3370 struct tog_log_view_state *s = &view->state.log;
3371 struct commit_queue_entry *entry;
3372 struct tog_view *v = view;
3373 const struct got_error *err = NULL;
3377 if (view_is_hsplit_top(view))
3379 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3382 /* Get the pattern */
3383 wmove(v->window, v->nlines - 1, 0);
3384 wclrtoeol(v->window);
3385 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3386 nodelay(v->window, FALSE);
3389 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3392 nodelay(v->window, TRUE);
3396 if (*pattern == '\0') {
3398 * Safety measure for the situation where the user
3399 * resets limit without previously limiting anything.
3405 * User could have pressed Ctrl+L, which refreshed the
3406 * commit queues, it means we can't save previously
3407 * (before limit took place) displayed entries,
3408 * because they would point to already free'ed memory,
3409 * so we are forced to always select first entry of
3412 s->commits = &s->real_commits;
3413 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3414 s->selected_entry = s->first_displayed_entry;
3421 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3426 /* Clear the screen while loading limit view */
3427 s->first_displayed_entry = NULL;
3428 s->last_displayed_entry = NULL;
3429 s->selected_entry = NULL;
3430 s->commits = &s->limit_commits;
3432 /* Prepare limit queue for new search */
3433 free_commits(&s->limit_commits);
3434 s->limit_commits.ncommits = 0;
3436 /* First process commits, which are in queue already */
3437 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3440 err = match_commit(&have_match, entry->id,
3441 entry->commit, &s->limit_regex);
3446 struct commit_queue_entry *matched;
3448 matched = alloc_commit_queue_entry(entry->commit,
3450 if (matched == NULL) {
3451 err = got_error_from_errno(
3452 "alloc_commit_queue_entry");
3455 matched->commit = entry->commit;
3456 got_object_commit_retain(entry->commit);
3458 matched->idx = s->limit_commits.ncommits;
3459 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3461 s->limit_commits.ncommits++;
3465 /* Second process all the commits, until we fill the screen */
3466 if (s->limit_commits.ncommits < view->nlines - 1 &&
3467 !s->thread_args.log_complete) {
3468 s->thread_args.commits_needed +=
3469 view->nlines - s->limit_commits.ncommits - 1;
3470 err = trigger_log_thread(view, 1);
3475 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3476 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3482 static const struct got_error *
3483 search_start_log_view(struct tog_view *view)
3485 struct tog_log_view_state *s = &view->state.log;
3487 s->matched_entry = NULL;
3488 s->search_entry = NULL;
3492 static const struct got_error *
3493 search_next_log_view(struct tog_view *view)
3495 const struct got_error *err = NULL;
3496 struct tog_log_view_state *s = &view->state.log;
3497 struct commit_queue_entry *entry;
3499 /* Display progress update in log view. */
3500 show_log_view(view);
3504 if (s->search_entry) {
3506 errcode = pthread_mutex_unlock(&tog_mutex);
3508 return got_error_set_errno(errcode,
3509 "pthread_mutex_unlock");
3510 ch = wgetch(view->window);
3511 errcode = pthread_mutex_lock(&tog_mutex);
3513 return got_error_set_errno(errcode,
3514 "pthread_mutex_lock");
3515 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3516 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3519 if (view->searching == TOG_SEARCH_FORWARD)
3520 entry = TAILQ_NEXT(s->search_entry, entry);
3522 entry = TAILQ_PREV(s->search_entry,
3523 commit_queue_head, entry);
3524 } else if (s->matched_entry) {
3526 * If the user has moved the cursor after we hit a match,
3527 * the position from where we should continue searching
3528 * might have changed.
3530 if (view->searching == TOG_SEARCH_FORWARD)
3531 entry = TAILQ_NEXT(s->selected_entry, entry);
3533 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3536 entry = s->selected_entry;
3542 if (entry == NULL) {
3543 if (s->thread_args.log_complete ||
3544 view->searching == TOG_SEARCH_BACKWARD) {
3545 view->search_next_done =
3546 (s->matched_entry == NULL ?
3547 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3548 s->search_entry = NULL;
3552 * Poke the log thread for more commits and return,
3553 * allowing the main loop to make progress. Search
3554 * will resume at s->search_entry once we come back.
3556 s->thread_args.commits_needed++;
3557 return trigger_log_thread(view, 0);
3560 err = match_commit(&have_match, entry->id, entry->commit,
3565 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3566 s->matched_entry = entry;
3570 s->search_entry = entry;
3571 if (view->searching == TOG_SEARCH_FORWARD)
3572 entry = TAILQ_NEXT(entry, entry);
3574 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3577 if (s->matched_entry) {
3578 int cur = s->selected_entry->idx;
3579 while (cur < s->matched_entry->idx) {
3580 err = input_log_view(NULL, view, KEY_DOWN);
3585 while (cur > s->matched_entry->idx) {
3586 err = input_log_view(NULL, view, KEY_UP);
3593 s->search_entry = NULL;
3598 static const struct got_error *
3599 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3600 struct got_repository *repo, const char *head_ref_name,
3601 const char *in_repo_path, int log_branches)
3603 const struct got_error *err = NULL;
3604 struct tog_log_view_state *s = &view->state.log;
3605 struct got_repository *thread_repo = NULL;
3606 struct got_commit_graph *thread_graph = NULL;
3609 if (in_repo_path != s->in_repo_path) {
3610 free(s->in_repo_path);
3611 s->in_repo_path = strdup(in_repo_path);
3612 if (s->in_repo_path == NULL) {
3613 err = got_error_from_errno("strdup");
3618 /* The commit queue only contains commits being displayed. */
3619 TAILQ_INIT(&s->real_commits.head);
3620 s->real_commits.ncommits = 0;
3621 s->commits = &s->real_commits;
3623 TAILQ_INIT(&s->limit_commits.head);
3625 s->limit_commits.ncommits = 0;
3628 if (head_ref_name) {
3629 s->head_ref_name = strdup(head_ref_name);
3630 if (s->head_ref_name == NULL) {
3631 err = got_error_from_errno("strdup");
3635 s->start_id = got_object_id_dup(start_id);
3636 if (s->start_id == NULL) {
3637 err = got_error_from_errno("got_object_id_dup");
3640 s->log_branches = log_branches;
3641 s->use_committer = 1;
3643 STAILQ_INIT(&s->colors);
3644 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3645 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3646 get_color_value("TOG_COLOR_COMMIT"));
3649 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3650 get_color_value("TOG_COLOR_AUTHOR"));
3652 free_colors(&s->colors);
3655 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3656 get_color_value("TOG_COLOR_DATE"));
3658 free_colors(&s->colors);
3663 view->show = show_log_view;
3664 view->input = input_log_view;
3665 view->resize = resize_log_view;
3666 view->close = close_log_view;
3667 view->search_start = search_start_log_view;
3668 view->search_next = search_next_log_view;
3670 if (s->thread_args.pack_fds == NULL) {
3671 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3675 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3676 s->thread_args.pack_fds);
3679 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3683 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3684 s->repo, NULL, NULL);
3688 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3690 err = got_error_set_errno(errcode, "pthread_cond_init");
3693 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3695 err = got_error_set_errno(errcode, "pthread_cond_init");
3699 s->thread_args.commits_needed = view->nlines;
3700 s->thread_args.graph = thread_graph;
3701 s->thread_args.real_commits = &s->real_commits;
3702 s->thread_args.limit_commits = &s->limit_commits;
3703 s->thread_args.in_repo_path = s->in_repo_path;
3704 s->thread_args.start_id = s->start_id;
3705 s->thread_args.repo = thread_repo;
3706 s->thread_args.log_complete = 0;
3707 s->thread_args.quit = &s->quit;
3708 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3709 s->thread_args.selected_entry = &s->selected_entry;
3710 s->thread_args.searching = &view->searching;
3711 s->thread_args.search_next_done = &view->search_next_done;
3712 s->thread_args.regex = &view->regex;
3713 s->thread_args.limiting = &s->limit_view;
3714 s->thread_args.limit_regex = &s->limit_regex;
3715 s->thread_args.limit_commits = &s->limit_commits;
3718 if (view->close == NULL)
3719 close_log_view(view);
3725 static const struct got_error *
3726 show_log_view(struct tog_view *view)
3728 const struct got_error *err;
3729 struct tog_log_view_state *s = &view->state.log;
3731 if (s->thread == NULL) {
3732 int errcode = pthread_create(&s->thread, NULL, log_thread,
3735 return got_error_set_errno(errcode, "pthread_create");
3736 if (s->thread_args.commits_needed > 0) {
3737 err = trigger_log_thread(view, 1);
3743 return draw_commits(view);
3747 log_move_cursor_up(struct tog_view *view, int page, int home)
3749 struct tog_log_view_state *s = &view->state.log;
3751 if (s->first_displayed_entry == NULL)
3753 if (s->selected_entry->idx == 0)
3756 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3758 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3760 if (!page && !home && s->selected > 0)
3763 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3769 static const struct got_error *
3770 log_move_cursor_down(struct tog_view *view, int page)
3772 struct tog_log_view_state *s = &view->state.log;
3773 const struct got_error *err = NULL;
3774 int eos = view->nlines - 2;
3776 if (s->first_displayed_entry == NULL)
3779 if (s->thread_args.log_complete &&
3780 s->selected_entry->idx >= s->commits->ncommits - 1)
3783 if (view_is_hsplit_top(view))
3784 --eos; /* border consumes the last line */
3787 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3790 err = log_scroll_down(view, 1);
3791 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3792 struct commit_queue_entry *entry;
3796 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3797 s->last_displayed_entry = entry;
3798 for (n = 0; n <= eos; n++) {
3801 s->first_displayed_entry = entry;
3802 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3805 s->selected = n - 1;
3807 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3808 s->thread_args.log_complete)
3809 s->selected += MIN(page,
3810 s->commits->ncommits - s->selected_entry->idx - 1);
3812 err = log_scroll_down(view, page);
3818 * We might necessarily overshoot in horizontal
3819 * splits; if so, select the last displayed commit.
3821 if (s->first_displayed_entry && s->last_displayed_entry) {
3822 s->selected = MIN(s->selected,
3823 s->last_displayed_entry->idx -
3824 s->first_displayed_entry->idx);
3829 if (s->thread_args.log_complete &&
3830 s->selected_entry->idx == s->commits->ncommits - 1)
3837 view_get_split(struct tog_view *view, int *y, int *x)
3842 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3843 if (view->child && view->child->resized_y)
3844 *y = view->child->resized_y;
3845 else if (view->resized_y)
3846 *y = view->resized_y;
3848 *y = view_split_begin_y(view->lines);
3849 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3850 if (view->child && view->child->resized_x)
3851 *x = view->child->resized_x;
3852 else if (view->resized_x)
3853 *x = view->resized_x;
3855 *x = view_split_begin_x(view->begin_x);
3859 /* Split view horizontally at y and offset view->state->selected line. */
3860 static const struct got_error *
3861 view_init_hsplit(struct tog_view *view, int y)
3863 const struct got_error *err = NULL;
3867 err = view_resize(view);
3871 err = offset_selection_down(view);
3876 static const struct got_error *
3877 log_goto_line(struct tog_view *view, int nlines)
3879 const struct got_error *err = NULL;
3880 struct tog_log_view_state *s = &view->state.log;
3881 int g, idx = s->selected_entry->idx;
3883 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3889 if (g >= s->first_displayed_entry->idx + 1 &&
3890 g <= s->last_displayed_entry->idx + 1 &&
3891 g - s->first_displayed_entry->idx - 1 < nlines) {
3892 s->selected = g - s->first_displayed_entry->idx - 1;
3898 err = log_move_cursor_down(view, g - idx - 1);
3899 if (!err && g > s->selected_entry->idx + 1)
3900 err = log_move_cursor_down(view,
3901 g - s->first_displayed_entry->idx - 1);
3904 } else if (idx + 1 > g)
3905 log_move_cursor_up(view, idx - g + 1, 0);
3907 if (g < nlines && s->first_displayed_entry->idx == 0)
3908 s->selected = g - 1;
3916 horizontal_scroll_input(struct tog_view *view, int ch)
3922 view->x -= MIN(view->x, 2);
3928 if (view->x + view->ncols / 2 < view->maxx)
3937 view->x = MAX(view->maxx - view->ncols / 2, 0);
3945 static const struct got_error *
3946 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3948 const struct got_error *err = NULL;
3949 struct tog_log_view_state *s = &view->state.log;
3952 if (s->thread_args.load_all) {
3953 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3954 s->thread_args.load_all = 0;
3955 else if (s->thread_args.log_complete) {
3956 err = log_move_cursor_down(view, s->commits->ncommits);
3957 s->thread_args.load_all = 0;
3963 eos = nscroll = view->nlines - 1;
3964 if (view_is_hsplit_top(view))
3968 return log_goto_line(view, eos);
3972 err = limit_log_view(view);
3983 horizontal_scroll_input(view, ch);
3990 log_move_cursor_up(view, 0, 0);
3995 log_move_cursor_up(view, 0, 1);
4005 log_move_cursor_up(view, nscroll, 0);
4012 err = log_move_cursor_down(view, 0);
4015 s->use_committer = !s->use_committer;
4016 view->action = s->use_committer ?
4017 "show committer" : "show commit author";
4022 /* We don't know yet how many commits, so we're forced to
4023 * traverse them all. */
4025 s->thread_args.load_all = 1;
4026 if (!s->thread_args.log_complete)
4027 return trigger_log_thread(view, 0);
4028 err = log_move_cursor_down(view, s->commits->ncommits);
4029 s->thread_args.load_all = 0;
4040 err = log_move_cursor_down(view, nscroll);
4043 if (s->selected > view->nlines - 2)
4044 s->selected = view->nlines - 2;
4045 if (s->selected > s->commits->ncommits - 1)
4046 s->selected = s->commits->ncommits - 1;
4048 if (s->commits->ncommits < view->nlines - 1 &&
4049 !s->thread_args.log_complete) {
4050 s->thread_args.commits_needed += (view->nlines - 1) -
4051 s->commits->ncommits;
4052 err = trigger_log_thread(view, 1);
4058 if (s->selected_entry == NULL)
4060 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4064 if (s->selected_entry == NULL)
4066 err = view_request_new(new_view, view, TOG_VIEW_TREE);
4072 if (ch == KEY_BACKSPACE &&
4073 got_path_is_root_dir(s->in_repo_path))
4075 err = stop_log_thread(s);
4078 if (ch == KEY_BACKSPACE) {
4080 err = got_path_dirname(&parent_path, s->in_repo_path);
4083 free(s->in_repo_path);
4084 s->in_repo_path = parent_path;
4085 s->thread_args.in_repo_path = s->in_repo_path;
4086 } else if (ch == CTRL('l')) {
4087 struct got_object_id *start_id;
4088 err = got_repo_match_object_id(&start_id, NULL,
4089 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4090 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4092 if (s->head_ref_name == NULL ||
4093 err->code != GOT_ERR_NOT_REF)
4095 /* Try to cope with deleted references. */
4096 free(s->head_ref_name);
4097 s->head_ref_name = NULL;
4098 err = got_repo_match_object_id(&start_id,
4099 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4100 &tog_refs, s->repo);
4105 s->start_id = start_id;
4106 s->thread_args.start_id = s->start_id;
4108 s->log_branches = !s->log_branches;
4110 if (s->thread_args.pack_fds == NULL) {
4111 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4115 err = got_repo_open(&s->thread_args.repo,
4116 got_repo_get_path(s->repo), NULL,
4117 s->thread_args.pack_fds);
4121 err = tog_load_refs(s->repo, 0);
4124 err = got_commit_graph_open(&s->thread_args.graph,
4125 s->in_repo_path, !s->log_branches);
4128 err = got_commit_graph_iter_start(s->thread_args.graph,
4129 s->start_id, s->repo, NULL, NULL);
4132 free_commits(&s->real_commits);
4133 free_commits(&s->limit_commits);
4134 s->first_displayed_entry = NULL;
4135 s->last_displayed_entry = NULL;
4136 s->selected_entry = NULL;
4138 s->thread_args.log_complete = 0;
4140 s->thread_args.commits_needed = view->lines;
4141 s->matched_entry = NULL;
4142 s->search_entry = NULL;
4147 err = view_request_new(new_view, view, TOG_VIEW_REF);
4157 static const struct got_error *
4158 apply_unveil(const char *repo_path, const char *worktree_path)
4160 const struct got_error *error;
4163 if (unveil("gmon.out", "rwc") != 0)
4164 return got_error_from_errno2("unveil", "gmon.out");
4166 if (repo_path && unveil(repo_path, "r") != 0)
4167 return got_error_from_errno2("unveil", repo_path);
4169 if (worktree_path && unveil(worktree_path, "rwc") != 0)
4170 return got_error_from_errno2("unveil", worktree_path);
4172 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4173 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4175 error = got_privsep_unveil_exec_helpers();
4179 if (unveil(NULL, NULL) != 0)
4180 return got_error_from_errno("unveil");
4185 static const struct got_error *
4186 init_mock_term(const char *test_script_path)
4188 const struct got_error *err = NULL;
4191 if (test_script_path == NULL || *test_script_path == '\0')
4192 return got_error_msg(GOT_ERR_IO, "TOG_TEST_SCRIPT not defined");
4194 tog_io.f = fopen(test_script_path, "re");
4195 if (tog_io.f == NULL) {
4196 err = got_error_from_errno_fmt("fopen: %s",
4201 /* test mode, we don't want any output */
4202 tog_io.cout = fopen("/dev/null", "w+");
4203 if (tog_io.cout == NULL) {
4204 err = got_error_from_errno2("fopen", "/dev/null");
4208 in = dup(fileno(tog_io.cout));
4210 err = got_error_from_errno("dup");
4213 tog_io.cin = fdopen(in, "r");
4214 if (tog_io.cin == NULL) {
4215 err = got_error_from_errno("fdopen");
4220 if (fseeko(tog_io.f, 0L, SEEK_SET) == -1) {
4221 err = got_error_from_errno("fseeko");
4225 if (newterm(NULL, tog_io.cout, tog_io.cin) == NULL)
4226 err = got_error_msg(GOT_ERR_IO,
4227 "newterm: failed to initialise curses");
4241 * Override default signal handlers before starting ncurses.
4242 * This should prevent ncurses from installing its own
4243 * broken cleanup() signal handler.
4245 signal(SIGWINCH, tog_sigwinch);
4246 signal(SIGPIPE, tog_sigpipe);
4247 signal(SIGCONT, tog_sigcont);
4248 signal(SIGINT, tog_sigint);
4249 signal(SIGTERM, tog_sigterm);
4251 if (using_mock_io) /* In test mode we use a fake terminal */
4257 halfdelay(1); /* Fast refresh while initial view is loading. */
4260 intrflush(stdscr, FALSE);
4261 keypad(stdscr, TRUE);
4263 if (getenv("TOG_COLORS") != NULL) {
4265 use_default_colors();
4271 static const struct got_error *
4272 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4273 struct got_repository *repo, struct got_worktree *worktree)
4275 const struct got_error *err = NULL;
4278 *in_repo_path = strdup("/");
4279 if (*in_repo_path == NULL)
4280 return got_error_from_errno("strdup");
4285 const char *prefix = got_worktree_get_path_prefix(worktree);
4288 err = got_worktree_resolve_path(&p, worktree, argv[0]);
4291 if (asprintf(in_repo_path, "%s%s%s", prefix,
4292 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4294 err = got_error_from_errno("asprintf");
4295 *in_repo_path = NULL;
4299 err = got_repo_map_path(in_repo_path, repo, argv[0]);
4304 static const struct got_error *
4305 cmd_log(int argc, char *argv[])
4307 const struct got_error *error;
4308 struct got_repository *repo = NULL;
4309 struct got_worktree *worktree = NULL;
4310 struct got_object_id *start_id = NULL;
4311 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4312 char *start_commit = NULL, *label = NULL;
4313 struct got_reference *ref = NULL;
4314 const char *head_ref_name = NULL;
4315 int ch, log_branches = 0;
4316 struct tog_view *view;
4317 int *pack_fds = NULL;
4319 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4325 start_commit = optarg;
4328 repo_path = realpath(optarg, NULL);
4329 if (repo_path == NULL)
4330 return got_error_from_errno2("realpath",
4345 error = got_repo_pack_fds_open(&pack_fds);
4349 if (repo_path == NULL) {
4350 cwd = getcwd(NULL, 0);
4352 return got_error_from_errno("getcwd");
4353 error = got_worktree_open(&worktree, cwd);
4354 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4358 strdup(got_worktree_get_repo_path(worktree));
4360 repo_path = strdup(cwd);
4361 if (repo_path == NULL) {
4362 error = got_error_from_errno("strdup");
4367 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4371 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4378 error = apply_unveil(got_repo_get_path(repo),
4379 worktree ? got_worktree_get_root_path(worktree) : NULL);
4383 /* already loaded by tog_log_with_path()? */
4384 if (TAILQ_EMPTY(&tog_refs)) {
4385 error = tog_load_refs(repo, 0);
4390 if (start_commit == NULL) {
4391 error = got_repo_match_object_id(&start_id, &label,
4392 worktree ? got_worktree_get_head_ref_name(worktree) :
4393 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4396 head_ref_name = label;
4398 error = got_ref_open(&ref, repo, start_commit, 0);
4400 head_ref_name = got_ref_get_name(ref);
4401 else if (error->code != GOT_ERR_NOT_REF)
4403 error = got_repo_match_object_id(&start_id, NULL,
4404 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4409 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4411 error = got_error_from_errno("view_open");
4414 error = open_log_view(view, start_id, repo, head_ref_name,
4415 in_repo_path, log_branches);
4419 /* Release work tree lock. */
4420 got_worktree_close(worktree);
4423 error = view_loop(view);
4433 const struct got_error *close_err = got_repo_close(repo);
4438 got_worktree_close(worktree);
4440 const struct got_error *pack_err =
4441 got_repo_pack_fds_close(pack_fds);
4453 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4454 "object1 object2\n", getprogname());
4459 match_line(const char *line, regex_t *regex, size_t nmatch,
4460 regmatch_t *regmatch)
4462 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4465 static struct tog_color *
4466 match_color(struct tog_colors *colors, const char *line)
4468 struct tog_color *tc = NULL;
4470 STAILQ_FOREACH(tc, colors, entry) {
4471 if (match_line(line, &tc->regex, 0, NULL))
4478 static const struct got_error *
4479 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4480 WINDOW *window, int skipcol, regmatch_t *regmatch)
4482 const struct got_error *err = NULL;
4484 wchar_t *wline = NULL;
4485 int rme, rms, n, width, scrollx;
4486 int width0 = 0, width1 = 0, width2 = 0;
4487 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4491 rms = regmatch->rm_so;
4492 rme = regmatch->rm_eo;
4494 err = expand_tab(&exstr, line);
4498 /* Split the line into 3 segments, according to match offsets. */
4499 seg0 = strndup(exstr, rms);
4501 err = got_error_from_errno("strndup");
4504 seg1 = strndup(exstr + rms, rme - rms);
4506 err = got_error_from_errno("strndup");
4509 seg2 = strdup(exstr + rme);
4511 err = got_error_from_errno("strndup");
4515 /* draw up to matched token if we haven't scrolled past it */
4516 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4520 n = MAX(width0 - skipcol, 0);
4523 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4524 wlimit, col_tab_align, 1);
4527 waddwstr(window, &wline[scrollx]);
4537 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4541 wlen = wcslen(wline);
4543 width = wcwidth(wline[i]);
4545 /* should not happen, tabs are expanded */
4546 err = got_error(GOT_ERR_RANGE);
4549 if (width0 + w + width > skipcol)
4554 /* draw (visible part of) matched token (if scrolled into it) */
4555 if (width1 - w > 0) {
4556 wattron(window, A_STANDOUT);
4557 waddwstr(window, &wline[i]);
4558 wattroff(window, A_STANDOUT);
4559 wlimit -= (width1 - w);
4560 *wtotal += (width1 - w);
4564 if (wlimit > 0) { /* draw rest of line */
4566 if (skipcol > width0 + width1) {
4567 err = format_line(&wline, &width2, &scrollx, seg2,
4568 skipcol - (width0 + width1), wlimit,
4572 waddwstr(window, &wline[scrollx]);
4574 err = format_line(&wline, &width2, NULL, seg2, 0,
4575 wlimit, col_tab_align, 1);
4578 waddwstr(window, wline);
4592 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4595 int *eof, *first, *selected;
4597 if (view->type == TOG_VIEW_DIFF) {
4598 struct tog_diff_view_state *s = &view->state.diff;
4600 first = &s->first_displayed_line;
4604 } else if (view->type == TOG_VIEW_HELP) {
4605 struct tog_help_view_state *s = &view->state.help;
4607 first = &s->first_displayed_line;
4611 } else if (view->type == TOG_VIEW_BLAME) {
4612 struct tog_blame_view_state *s = &view->state.blame;
4614 first = &s->first_displayed_line;
4615 selected = &s->selected_line;
4621 /* Center gline in the middle of the page like vi(1). */
4622 if (*lineno < view->gline - (view->nlines - 3) / 2)
4624 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4633 *selected = view->gline <= (view->nlines - 3) / 2 ?
4634 view->gline : (view->nlines - 3) / 2 + 1;
4640 static const struct got_error *
4641 draw_file(struct tog_view *view, const char *header)
4643 struct tog_diff_view_state *s = &view->state.diff;
4644 regmatch_t *regmatch = &view->regmatch;
4645 const struct got_error *err;
4648 size_t linesize = 0;
4652 int max_lines = view->nlines;
4653 int nlines = s->nlines;
4656 s->lineno = s->first_displayed_line - 1;
4657 line_offset = s->lines[s->first_displayed_line - 1].offset;
4658 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4659 return got_error_from_errno("fseek");
4661 werase(view->window);
4663 if (view->gline > s->nlines - 1)
4664 view->gline = s->nlines - 1;
4667 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4668 1 : view->gline - (view->nlines - 3) / 2 :
4669 s->lineno + s->selected_line;
4671 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4672 return got_error_from_errno("asprintf");
4673 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4679 if (view_needs_focus_indication(view))
4680 wstandout(view->window);
4681 waddwstr(view->window, wline);
4684 while (width++ < view->ncols)
4685 waddch(view->window, ' ');
4686 if (view_needs_focus_indication(view))
4687 wstandend(view->window);
4697 while (max_lines > 0 && nprinted < max_lines) {
4698 enum got_diff_line_type linetype;
4701 linelen = getline(&line, &linesize, s->f);
4702 if (linelen == -1) {
4708 return got_ferror(s->f, GOT_ERR_IO);
4711 if (++s->lineno < s->first_displayed_line)
4713 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4715 if (s->lineno == view->hiline)
4718 /* Set view->maxx based on full line length. */
4719 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4725 view->maxx = MAX(view->maxx, width);
4729 linetype = s->lines[s->lineno].type;
4730 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4731 linetype < GOT_DIFF_LINE_CONTEXT)
4732 attr |= COLOR_PAIR(linetype);
4734 wattron(view->window, attr);
4735 if (s->first_displayed_line + nprinted == s->matched_line &&
4736 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4737 err = add_matched_line(&width, line, view->ncols, 0,
4738 view->window, view->x, regmatch);
4745 err = format_line(&wline, &width, &skip, line,
4746 view->x, view->ncols, 0, view->x ? 1 : 0);
4751 waddwstr(view->window, &wline[skip]);
4755 if (s->lineno == view->hiline) {
4756 /* highlight full gline length */
4757 while (width++ < view->ncols)
4758 waddch(view->window, ' ');
4760 if (width <= view->ncols - 1)
4761 waddch(view->window, '\n');
4764 wattroff(view->window, attr);
4765 if (++nprinted == 1)
4766 s->first_displayed_line = s->lineno;
4770 s->last_displayed_line = s->first_displayed_line +
4773 s->last_displayed_line = s->first_displayed_line;
4778 while (nprinted < view->nlines) {
4779 waddch(view->window, '\n');
4783 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4789 wstandout(view->window);
4790 waddwstr(view->window, wline);
4793 wstandend(view->window);
4800 get_datestr(time_t *time, char *datebuf)
4802 struct tm mytm, *tm;
4805 tm = gmtime_r(time, &mytm);
4808 s = asctime_r(tm, datebuf);
4811 p = strchr(s, '\n');
4817 static const struct got_error *
4818 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4819 off_t off, uint8_t type)
4821 struct got_diff_line *p;
4823 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4825 return got_error_from_errno("reallocarray");
4827 (*lines)[*nlines].offset = off;
4828 (*lines)[*nlines].type = type;
4834 static const struct got_error *
4835 cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4836 struct got_diff_line *s_lines, size_t s_nlines)
4838 struct got_diff_line *p;
4842 if (fseeko(src, 0L, SEEK_SET) == -1)
4843 return got_error_from_errno("fseeko");
4846 r = fread(buf, 1, sizeof(buf), src);
4849 return got_error_from_errno("fread");
4853 if (fwrite(buf, 1, r, dst) != r)
4854 return got_ferror(dst, GOT_ERR_IO);
4857 if (s_nlines == 0 && *d_nlines == 0)
4861 * If commit info was in dst, increment line offsets
4862 * of the appended diff content, but skip s_lines[0]
4863 * because offset zero is already in *d_lines.
4865 if (*d_nlines > 0) {
4866 for (i = 1; i < s_nlines; ++i)
4867 s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4875 p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4877 /* d_lines is freed in close_diff_view() */
4878 return got_error_from_errno("reallocarray");
4883 memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
4884 *d_nlines += s_nlines;
4889 static const struct got_error *
4890 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4891 struct got_object_id *commit_id, struct got_reflist_head *refs,
4892 struct got_repository *repo, int ignore_ws, int force_text_diff,
4893 struct got_diffstat_cb_arg *dsa, FILE *outfile)
4895 const struct got_error *err = NULL;
4896 char datebuf[26], *datestr;
4897 struct got_commit_object *commit;
4898 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4899 time_t committer_time;
4900 const char *author, *committer;
4901 char *refs_str = NULL;
4902 struct got_pathlist_entry *pe;
4907 err = build_refs_str(&refs_str, refs, commit_id, repo);
4912 err = got_object_open_as_commit(&commit, repo, commit_id);
4916 err = got_object_id_str(&id_str, commit_id);
4918 err = got_error_from_errno("got_object_id_str");
4922 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4926 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4927 refs_str ? refs_str : "", refs_str ? ")" : "");
4929 err = got_error_from_errno("fprintf");
4933 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4937 n = fprintf(outfile, "from: %s\n",
4938 got_object_commit_get_author(commit));
4940 err = got_error_from_errno("fprintf");
4944 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4948 author = got_object_commit_get_author(commit);
4949 committer = got_object_commit_get_committer(commit);
4950 if (strcmp(author, committer) != 0) {
4951 n = fprintf(outfile, "via: %s\n", committer);
4953 err = got_error_from_errno("fprintf");
4957 err = add_line_metadata(lines, nlines, outoff,
4958 GOT_DIFF_LINE_AUTHOR);
4962 committer_time = got_object_commit_get_committer_time(commit);
4963 datestr = get_datestr(&committer_time, datebuf);
4965 n = fprintf(outfile, "date: %s UTC\n", datestr);
4967 err = got_error_from_errno("fprintf");
4971 err = add_line_metadata(lines, nlines, outoff,
4972 GOT_DIFF_LINE_DATE);
4976 if (got_object_commit_get_nparents(commit) > 1) {
4977 const struct got_object_id_queue *parent_ids;
4978 struct got_object_qid *qid;
4980 parent_ids = got_object_commit_get_parent_ids(commit);
4981 STAILQ_FOREACH(qid, parent_ids, entry) {
4982 err = got_object_id_str(&id_str, &qid->id);
4985 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4987 err = got_error_from_errno("fprintf");
4991 err = add_line_metadata(lines, nlines, outoff,
4992 GOT_DIFF_LINE_META);
5000 err = got_object_commit_get_logmsg(&logmsg, commit);
5004 while ((line = strsep(&s, "\n")) != NULL) {
5005 n = fprintf(outfile, "%s\n", line);
5007 err = got_error_from_errno("fprintf");
5011 err = add_line_metadata(lines, nlines, outoff,
5012 GOT_DIFF_LINE_LOGMSG);
5017 TAILQ_FOREACH(pe, dsa->paths, entry) {
5018 struct got_diff_changed_path *cp = pe->data;
5019 int pad = dsa->max_path_len - pe->path_len + 1;
5021 n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
5022 pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
5023 dsa->rm_cols + 1, cp->rm);
5025 err = got_error_from_errno("fprintf");
5029 err = add_line_metadata(lines, nlines, outoff,
5030 GOT_DIFF_LINE_CHANGES);
5035 fputc('\n', outfile);
5037 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5041 n = fprintf(outfile,
5042 "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5043 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5044 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5046 err = got_error_from_errno("fprintf");
5050 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5054 fputc('\n', outfile);
5056 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5061 got_object_commit_close(commit);
5070 static const struct got_error *
5071 create_diff(struct tog_diff_view_state *s)
5073 const struct got_error *err = NULL;
5074 FILE *f = NULL, *tmp_diff_file = NULL;
5076 struct got_diff_line *lines = NULL;
5077 struct got_pathlist_head changed_paths;
5079 TAILQ_INIT(&changed_paths);
5082 s->lines = malloc(sizeof(*s->lines));
5083 if (s->lines == NULL)
5084 return got_error_from_errno("malloc");
5089 err = got_error_from_errno("got_opentemp");
5092 tmp_diff_file = got_opentemp();
5093 if (tmp_diff_file == NULL) {
5094 err = got_error_from_errno("got_opentemp");
5097 if (s->f && fclose(s->f) == EOF) {
5098 err = got_error_from_errno("fclose");
5104 err = got_object_get_type(&obj_type, s->repo, s->id1);
5106 err = got_object_get_type(&obj_type, s->repo, s->id2);
5111 case GOT_OBJ_TYPE_BLOB:
5112 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5113 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5114 s->label1, s->label2, tog_diff_algo, s->diff_context,
5115 s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5118 case GOT_OBJ_TYPE_TREE:
5119 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5120 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5121 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5122 s->force_text_diff, NULL, s->repo, s->f);
5124 case GOT_OBJ_TYPE_COMMIT: {
5125 const struct got_object_id_queue *parent_ids;
5126 struct got_object_qid *pid;
5127 struct got_commit_object *commit2;
5128 struct got_reflist_head *refs;
5130 struct got_diffstat_cb_arg dsa = {
5133 s->ignore_whitespace,
5138 lines = malloc(sizeof(*lines));
5139 if (lines == NULL) {
5140 err = got_error_from_errno("malloc");
5144 /* build diff first in tmp file then append to commit info */
5145 err = got_diff_objects_as_commits(&lines, &nlines,
5146 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5147 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5148 s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5152 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
5155 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5156 /* Show commit info if we're diffing to a parent/root commit. */
5157 if (s->id1 == NULL) {
5158 err = write_commit_info(&s->lines, &s->nlines, s->id2,
5159 refs, s->repo, s->ignore_whitespace,
5160 s->force_text_diff, &dsa, s->f);
5164 parent_ids = got_object_commit_get_parent_ids(commit2);
5165 STAILQ_FOREACH(pid, parent_ids, entry) {
5166 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5167 err = write_commit_info(&s->lines,
5168 &s->nlines, s->id2, refs, s->repo,
5169 s->ignore_whitespace,
5170 s->force_text_diff, &dsa, s->f);
5177 got_object_commit_close(commit2);
5179 err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5184 err = got_error(GOT_ERR_OBJ_TYPE);
5189 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5190 if (s->f && fflush(s->f) != 0 && err == NULL)
5191 err = got_error_from_errno("fflush");
5192 if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5193 err = got_error_from_errno("fclose");
5198 diff_view_indicate_progress(struct tog_view *view)
5200 mvwaddstr(view->window, 0, 0, "diffing...");
5205 static const struct got_error *
5206 search_start_diff_view(struct tog_view *view)
5208 struct tog_diff_view_state *s = &view->state.diff;
5210 s->matched_line = 0;
5215 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5216 size_t *nlines, int **first, int **last, int **match, int **selected)
5218 struct tog_diff_view_state *s = &view->state.diff;
5221 *nlines = s->nlines;
5222 *line_offsets = NULL;
5223 *match = &s->matched_line;
5224 *first = &s->first_displayed_line;
5225 *last = &s->last_displayed_line;
5226 *selected = &s->selected_line;
5229 static const struct got_error *
5230 search_next_view_match(struct tog_view *view)
5232 const struct got_error *err = NULL;
5236 size_t linesize = 0;
5238 off_t *line_offsets;
5240 int *first, *last, *match, *selected;
5242 if (!view->search_setup)
5243 return got_error_msg(GOT_ERR_NOT_IMPL,
5244 "view search not supported");
5245 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5248 if (!view->searching) {
5249 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5254 if (view->searching == TOG_SEARCH_FORWARD)
5255 lineno = *first + 1;
5257 lineno = *first - 1;
5259 lineno = *first - 1 + *selected;
5264 if (lineno <= 0 || lineno > nlines) {
5266 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5270 if (view->searching == TOG_SEARCH_FORWARD)
5276 offset = view->type == TOG_VIEW_DIFF ?
5277 view->state.diff.lines[lineno - 1].offset :
5278 line_offsets[lineno - 1];
5279 if (fseeko(f, offset, SEEK_SET) != 0) {
5281 return got_error_from_errno("fseeko");
5283 linelen = getline(&line, &linesize, f);
5284 if (linelen != -1) {
5286 err = expand_tab(&exstr, line);
5289 if (match_line(exstr, &view->regex, 1,
5291 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5298 if (view->searching == TOG_SEARCH_FORWARD)
5313 static const struct got_error *
5314 close_diff_view(struct tog_view *view)
5316 const struct got_error *err = NULL;
5317 struct tog_diff_view_state *s = &view->state.diff;
5323 if (s->f && fclose(s->f) == EOF)
5324 err = got_error_from_errno("fclose");
5326 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5327 err = got_error_from_errno("fclose");
5329 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5330 err = got_error_from_errno("fclose");
5332 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5333 err = got_error_from_errno("close");
5335 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5336 err = got_error_from_errno("close");
5344 static const struct got_error *
5345 open_diff_view(struct tog_view *view, struct got_object_id *id1,
5346 struct got_object_id *id2, const char *label1, const char *label2,
5347 int diff_context, int ignore_whitespace, int force_text_diff,
5348 struct tog_view *parent_view, struct got_repository *repo)
5350 const struct got_error *err;
5351 struct tog_diff_view_state *s = &view->state.diff;
5353 memset(s, 0, sizeof(*s));
5357 if (id1 != NULL && id2 != NULL) {
5360 err = got_object_get_type(&type1, repo, id1);
5363 err = got_object_get_type(&type2, repo, id2);
5367 if (type1 != type2) {
5368 err = got_error(GOT_ERR_OBJ_TYPE);
5372 s->first_displayed_line = 1;
5373 s->last_displayed_line = view->nlines;
5374 s->selected_line = 1;
5382 s->id1 = got_object_id_dup(id1);
5383 if (s->id1 == NULL) {
5384 err = got_error_from_errno("got_object_id_dup");
5390 s->id2 = got_object_id_dup(id2);
5391 if (s->id2 == NULL) {
5392 err = got_error_from_errno("got_object_id_dup");
5396 s->f1 = got_opentemp();
5397 if (s->f1 == NULL) {
5398 err = got_error_from_errno("got_opentemp");
5402 s->f2 = got_opentemp();
5403 if (s->f2 == NULL) {
5404 err = got_error_from_errno("got_opentemp");
5408 s->fd1 = got_opentempfd();
5410 err = got_error_from_errno("got_opentempfd");
5414 s->fd2 = got_opentempfd();
5416 err = got_error_from_errno("got_opentempfd");
5420 s->diff_context = diff_context;
5421 s->ignore_whitespace = ignore_whitespace;
5422 s->force_text_diff = force_text_diff;
5423 s->parent_view = parent_view;
5426 if (has_colors() && getenv("TOG_COLORS") != NULL && !using_mock_io) {
5429 rc = init_pair(GOT_DIFF_LINE_MINUS,
5430 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5432 rc = init_pair(GOT_DIFF_LINE_PLUS,
5433 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5435 rc = init_pair(GOT_DIFF_LINE_HUNK,
5436 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5438 rc = init_pair(GOT_DIFF_LINE_META,
5439 get_color_value("TOG_COLOR_DIFF_META"), -1);
5441 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5442 get_color_value("TOG_COLOR_DIFF_META"), -1);
5444 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5445 get_color_value("TOG_COLOR_DIFF_META"), -1);
5447 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5448 get_color_value("TOG_COLOR_DIFF_META"), -1);
5450 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5451 get_color_value("TOG_COLOR_AUTHOR"), -1);
5453 rc = init_pair(GOT_DIFF_LINE_DATE,
5454 get_color_value("TOG_COLOR_DATE"), -1);
5456 err = got_error(GOT_ERR_RANGE);
5461 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5462 view_is_splitscreen(view))
5463 show_log_view(parent_view); /* draw border */
5464 diff_view_indicate_progress(view);
5466 err = create_diff(s);
5468 view->show = show_diff_view;
5469 view->input = input_diff_view;
5470 view->reset = reset_diff_view;
5471 view->close = close_diff_view;
5472 view->search_start = search_start_diff_view;
5473 view->search_setup = search_setup_diff_view;
5474 view->search_next = search_next_view_match;
5477 if (view->close == NULL)
5478 close_diff_view(view);
5484 static const struct got_error *
5485 show_diff_view(struct tog_view *view)
5487 const struct got_error *err;
5488 struct tog_diff_view_state *s = &view->state.diff;
5489 char *id_str1 = NULL, *id_str2, *header;
5490 const char *label1, *label2;
5493 err = got_object_id_str(&id_str1, s->id1);
5496 label1 = s->label1 ? s->label1 : id_str1;
5498 label1 = "/dev/null";
5500 err = got_object_id_str(&id_str2, s->id2);
5503 label2 = s->label2 ? s->label2 : id_str2;
5505 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5506 err = got_error_from_errno("asprintf");
5514 err = draw_file(view, header);
5519 static const struct got_error *
5520 set_selected_commit(struct tog_diff_view_state *s,
5521 struct commit_queue_entry *entry)
5523 const struct got_error *err;
5524 const struct got_object_id_queue *parent_ids;
5525 struct got_commit_object *selected_commit;
5526 struct got_object_qid *pid;
5529 s->id2 = got_object_id_dup(entry->id);
5531 return got_error_from_errno("got_object_id_dup");
5533 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5536 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5538 pid = STAILQ_FIRST(parent_ids);
5539 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5540 got_object_commit_close(selected_commit);
5544 static const struct got_error *
5545 reset_diff_view(struct tog_view *view)
5547 struct tog_diff_view_state *s = &view->state.diff;
5550 wclear(view->window);
5551 s->first_displayed_line = 1;
5552 s->last_displayed_line = view->nlines;
5553 s->matched_line = 0;
5554 diff_view_indicate_progress(view);
5555 return create_diff(s);
5559 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5563 i = start = s->first_displayed_line - 1;
5565 while (s->lines[i].type != type) {
5569 return; /* do nothing, requested type not in file */
5572 s->selected_line = 1;
5573 s->first_displayed_line = i;
5577 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5581 i = start = s->first_displayed_line + 1;
5583 while (s->lines[i].type != type) {
5584 if (i == s->nlines - 1)
5587 return; /* do nothing, requested type not in file */
5590 s->selected_line = 1;
5591 s->first_displayed_line = i;
5594 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5596 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5599 static const struct got_error *
5600 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5602 const struct got_error *err = NULL;
5603 struct tog_diff_view_state *s = &view->state.diff;
5604 struct tog_log_view_state *ls;
5605 struct commit_queue_entry *old_selected_entry;
5607 size_t linesize = 0;
5609 int i, nscroll = view->nlines - 1, up = 0;
5611 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5620 horizontal_scroll_input(view, ch);
5625 s->force_text_diff = !s->force_text_diff;
5626 view->action = s->force_text_diff ?
5627 "force ASCII text enabled" :
5628 "force ASCII text disabled";
5630 else if (ch == 'w') {
5631 s->ignore_whitespace = !s->ignore_whitespace;
5632 view->action = s->ignore_whitespace ?
5633 "ignore whitespace enabled" :
5634 "ignore whitespace disabled";
5636 err = reset_diff_view(view);
5640 s->first_displayed_line = 1;
5649 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5655 if (s->first_displayed_line > 1)
5656 s->first_displayed_line--;
5667 if (s->first_displayed_line == 1) {
5672 while (i++ < nscroll && s->first_displayed_line > 1)
5673 s->first_displayed_line--;
5679 s->first_displayed_line++;
5696 while (!s->eof && i++ < nscroll) {
5697 linelen = getline(&line, &linesize, s->f);
5698 s->first_displayed_line++;
5699 if (linelen == -1) {
5703 err = got_ferror(s->f, GOT_ERR_IO);
5710 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5713 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5716 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5719 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5722 if (s->diff_context > 0) {
5724 s->matched_line = 0;
5725 diff_view_indicate_progress(view);
5726 err = create_diff(s);
5727 if (s->first_displayed_line + view->nlines - 1 >
5729 s->first_displayed_line = 1;
5730 s->last_displayed_line = view->nlines;
5736 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5738 s->matched_line = 0;
5739 diff_view_indicate_progress(view);
5740 err = create_diff(s);
5752 if (s->parent_view == NULL) {
5756 s->parent_view->count = view->count;
5758 if (s->parent_view->type == TOG_VIEW_LOG) {
5759 ls = &s->parent_view->state.log;
5760 old_selected_entry = ls->selected_entry;
5762 err = input_log_view(NULL, s->parent_view,
5763 up ? KEY_UP : KEY_DOWN);
5766 view->count = s->parent_view->count;
5768 if (old_selected_entry == ls->selected_entry)
5771 err = set_selected_commit(s, ls->selected_entry);
5774 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5775 struct tog_blame_view_state *bs;
5776 struct got_object_id *id, *prev_id;
5778 bs = &s->parent_view->state.blame;
5779 prev_id = get_annotation_for_line(bs->blame.lines,
5780 bs->blame.nlines, bs->last_diffed_line);
5782 err = input_blame_view(&view, s->parent_view,
5783 up ? KEY_UP : KEY_DOWN);
5786 view->count = s->parent_view->count;
5788 if (prev_id == NULL)
5790 id = get_selected_commit_id(bs->blame.lines,
5791 bs->blame.nlines, bs->first_displayed_line,
5796 if (!got_object_id_cmp(prev_id, id))
5799 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5803 s->first_displayed_line = 1;
5804 s->last_displayed_line = view->nlines;
5805 s->matched_line = 0;
5808 diff_view_indicate_progress(view);
5809 err = create_diff(s);
5819 static const struct got_error *
5820 cmd_diff(int argc, char *argv[])
5822 const struct got_error *error;
5823 struct got_repository *repo = NULL;
5824 struct got_worktree *worktree = NULL;
5825 struct got_object_id *id1 = NULL, *id2 = NULL;
5826 char *repo_path = NULL, *cwd = NULL;
5827 char *id_str1 = NULL, *id_str2 = NULL;
5828 char *label1 = NULL, *label2 = NULL;
5829 int diff_context = 3, ignore_whitespace = 0;
5830 int ch, force_text_diff = 0;
5832 struct tog_view *view;
5833 int *pack_fds = NULL;
5835 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5838 force_text_diff = 1;
5841 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5844 errx(1, "number of context lines is %s: %s",
5848 repo_path = realpath(optarg, NULL);
5849 if (repo_path == NULL)
5850 return got_error_from_errno2("realpath",
5852 got_path_strip_trailing_slashes(repo_path);
5855 ignore_whitespace = 1;
5867 usage_diff(); /* TODO show local worktree changes */
5868 } else if (argc == 2) {
5874 error = got_repo_pack_fds_open(&pack_fds);
5878 if (repo_path == NULL) {
5879 cwd = getcwd(NULL, 0);
5881 return got_error_from_errno("getcwd");
5882 error = got_worktree_open(&worktree, cwd);
5883 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5887 strdup(got_worktree_get_repo_path(worktree));
5889 repo_path = strdup(cwd);
5890 if (repo_path == NULL) {
5891 error = got_error_from_errno("strdup");
5896 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5902 error = apply_unveil(got_repo_get_path(repo), NULL);
5906 error = tog_load_refs(repo, 0);
5910 error = got_repo_match_object_id(&id1, &label1, id_str1,
5911 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5915 error = got_repo_match_object_id(&id2, &label2, id_str2,
5916 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5920 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5922 error = got_error_from_errno("view_open");
5925 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5926 ignore_whitespace, force_text_diff, NULL, repo);
5929 error = view_loop(view);
5936 const struct got_error *close_err = got_repo_close(repo);
5941 got_worktree_close(worktree);
5943 const struct got_error *pack_err =
5944 got_repo_pack_fds_close(pack_fds);
5957 "usage: %s blame [-c commit] [-r repository-path] path\n",
5962 struct tog_blame_line {
5964 struct got_object_id *id;
5967 static const struct got_error *
5968 draw_blame(struct tog_view *view)
5970 struct tog_blame_view_state *s = &view->state.blame;
5971 struct tog_blame *blame = &s->blame;
5972 regmatch_t *regmatch = &view->regmatch;
5973 const struct got_error *err;
5974 int lineno = 0, nprinted = 0;
5976 size_t linesize = 0;
5980 struct tog_blame_line *blame_line;
5981 struct got_object_id *prev_id = NULL;
5983 struct tog_color *tc;
5985 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5990 werase(view->window);
5992 if (asprintf(&line, "commit %s", id_str) == -1) {
5993 err = got_error_from_errno("asprintf");
5998 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6003 if (view_needs_focus_indication(view))
6004 wstandout(view->window);
6005 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6007 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6008 waddwstr(view->window, wline);
6009 while (width++ < view->ncols)
6010 waddch(view->window, ' ');
6012 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6013 if (view_needs_focus_indication(view))
6014 wstandend(view->window);
6018 if (view->gline > blame->nlines)
6019 view->gline = blame->nlines;
6021 if (tog_io.wait_for_ui) {
6022 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6025 rc = pthread_cond_wait(&bta->blame_complete, &tog_mutex);
6027 return got_error_set_errno(rc, "pthread_cond_wait");
6028 tog_io.wait_for_ui = 0;
6031 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
6032 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
6033 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
6035 return got_error_from_errno("asprintf");
6038 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6043 waddwstr(view->window, wline);
6046 if (width < view->ncols - 1)
6047 waddch(view->window, '\n');
6051 while (nprinted < view->nlines - 2) {
6052 linelen = getline(&line, &linesize, blame->f);
6053 if (linelen == -1) {
6054 if (feof(blame->f)) {
6059 return got_ferror(blame->f, GOT_ERR_IO);
6061 if (++lineno < s->first_displayed_line)
6063 if (view->gline && !gotoline(view, &lineno, &nprinted))
6066 /* Set view->maxx based on full line length. */
6067 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6074 view->maxx = MAX(view->maxx, width);
6076 if (nprinted == s->selected_line - 1)
6077 wstandout(view->window);
6079 if (blame->nlines > 0) {
6080 blame_line = &blame->lines[lineno - 1];
6081 if (blame_line->annotated && prev_id &&
6082 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6083 !(nprinted == s->selected_line - 1)) {
6084 waddstr(view->window, " ");
6085 } else if (blame_line->annotated) {
6087 err = got_object_id_str(&id_str,
6093 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6095 wattr_on(view->window,
6096 COLOR_PAIR(tc->colorpair), NULL);
6097 wprintw(view->window, "%.8s", id_str);
6099 wattr_off(view->window,
6100 COLOR_PAIR(tc->colorpair), NULL);
6102 prev_id = blame_line->id;
6104 waddstr(view->window, "........");
6108 waddstr(view->window, "........");
6112 if (nprinted == s->selected_line - 1)
6113 wstandend(view->window);
6114 waddstr(view->window, " ");
6116 if (view->ncols <= 9) {
6118 } else if (s->first_displayed_line + nprinted ==
6120 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6121 err = add_matched_line(&width, line, view->ncols - 9, 9,
6122 view->window, view->x, regmatch);
6130 err = format_line(&wline, &width, &skip, line,
6131 view->x, view->ncols - 9, 9, 1);
6136 waddwstr(view->window, &wline[skip]);
6142 if (width <= view->ncols - 1)
6143 waddch(view->window, '\n');
6144 if (++nprinted == 1)
6145 s->first_displayed_line = lineno;
6148 s->last_displayed_line = lineno;
6155 static const struct got_error *
6156 blame_cb(void *arg, int nlines, int lineno,
6157 struct got_commit_object *commit, struct got_object_id *id)
6159 const struct got_error *err = NULL;
6160 struct tog_blame_cb_args *a = arg;
6161 struct tog_blame_line *line;
6164 if (nlines != a->nlines ||
6165 (lineno != -1 && lineno < 1) || lineno > a->nlines)
6166 return got_error(GOT_ERR_RANGE);
6168 errcode = pthread_mutex_lock(&tog_mutex);
6170 return got_error_set_errno(errcode, "pthread_mutex_lock");
6172 if (*a->quit) { /* user has quit the blame view */
6173 err = got_error(GOT_ERR_ITER_COMPLETED);
6178 goto done; /* no change in this commit */
6180 line = &a->lines[lineno - 1];
6181 if (line->annotated)
6184 line->id = got_object_id_dup(id);
6185 if (line->id == NULL) {
6186 err = got_error_from_errno("got_object_id_dup");
6189 line->annotated = 1;
6191 errcode = pthread_mutex_unlock(&tog_mutex);
6193 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6198 blame_thread(void *arg)
6200 const struct got_error *err, *close_err;
6201 struct tog_blame_thread_args *ta = arg;
6202 struct tog_blame_cb_args *a = ta->cb_args;
6203 int errcode, fd1 = -1, fd2 = -1;
6204 FILE *f1 = NULL, *f2 = NULL;
6206 fd1 = got_opentempfd();
6208 return (void *)got_error_from_errno("got_opentempfd");
6210 fd2 = got_opentempfd();
6212 err = got_error_from_errno("got_opentempfd");
6216 f1 = got_opentemp();
6218 err = (void *)got_error_from_errno("got_opentemp");
6221 f2 = got_opentemp();
6223 err = (void *)got_error_from_errno("got_opentemp");
6227 err = block_signals_used_by_main_thread();
6231 err = got_blame(ta->path, a->commit_id, ta->repo,
6232 tog_diff_algo, blame_cb, ta->cb_args,
6233 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6234 if (err && err->code == GOT_ERR_CANCELLED)
6237 errcode = pthread_mutex_lock(&tog_mutex);
6239 err = got_error_set_errno(errcode, "pthread_mutex_lock");
6243 close_err = got_repo_close(ta->repo);
6249 if (tog_io.wait_for_ui) {
6250 errcode = pthread_cond_signal(&ta->blame_complete);
6251 if (errcode && err == NULL)
6252 err = got_error_set_errno(errcode,
6253 "pthread_cond_signal");
6256 errcode = pthread_mutex_unlock(&tog_mutex);
6257 if (errcode && err == NULL)
6258 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6261 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6262 err = got_error_from_errno("close");
6263 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6264 err = got_error_from_errno("close");
6265 if (f1 && fclose(f1) == EOF && err == NULL)
6266 err = got_error_from_errno("fclose");
6267 if (f2 && fclose(f2) == EOF && err == NULL)
6268 err = got_error_from_errno("fclose");
6273 static struct got_object_id *
6274 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6275 int first_displayed_line, int selected_line)
6277 struct tog_blame_line *line;
6282 line = &lines[first_displayed_line - 1 + selected_line - 1];
6283 if (!line->annotated)
6289 static struct got_object_id *
6290 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6293 struct tog_blame_line *line;
6295 if (nlines <= 0 || lineno >= nlines)
6298 line = &lines[lineno - 1];
6299 if (!line->annotated)
6305 static const struct got_error *
6306 stop_blame(struct tog_blame *blame)
6308 const struct got_error *err = NULL;
6311 if (blame->thread) {
6313 errcode = pthread_mutex_unlock(&tog_mutex);
6315 return got_error_set_errno(errcode,
6316 "pthread_mutex_unlock");
6317 errcode = pthread_join(blame->thread, (void **)&err);
6319 return got_error_set_errno(errcode, "pthread_join");
6320 errcode = pthread_mutex_lock(&tog_mutex);
6322 return got_error_set_errno(errcode,
6323 "pthread_mutex_lock");
6324 if (err && err->code == GOT_ERR_ITER_COMPLETED)
6326 blame->thread = NULL;
6328 if (blame->thread_args.repo) {
6329 const struct got_error *close_err;
6330 close_err = got_repo_close(blame->thread_args.repo);
6333 blame->thread_args.repo = NULL;
6336 if (fclose(blame->f) == EOF && err == NULL)
6337 err = got_error_from_errno("fclose");
6341 for (i = 0; i < blame->nlines; i++)
6342 free(blame->lines[i].id);
6344 blame->lines = NULL;
6346 free(blame->cb_args.commit_id);
6347 blame->cb_args.commit_id = NULL;
6348 if (blame->pack_fds) {
6349 const struct got_error *pack_err =
6350 got_repo_pack_fds_close(blame->pack_fds);
6353 blame->pack_fds = NULL;
6358 static const struct got_error *
6359 cancel_blame_view(void *arg)
6361 const struct got_error *err = NULL;
6365 errcode = pthread_mutex_lock(&tog_mutex);
6367 return got_error_set_errno(errcode,
6368 "pthread_mutex_unlock");
6371 err = got_error(GOT_ERR_CANCELLED);
6373 errcode = pthread_mutex_unlock(&tog_mutex);
6375 return got_error_set_errno(errcode,
6376 "pthread_mutex_lock");
6381 static const struct got_error *
6382 run_blame(struct tog_view *view)
6384 struct tog_blame_view_state *s = &view->state.blame;
6385 struct tog_blame *blame = &s->blame;
6386 const struct got_error *err = NULL;
6387 struct got_commit_object *commit = NULL;
6388 struct got_blob_object *blob = NULL;
6389 struct got_repository *thread_repo = NULL;
6390 struct got_object_id *obj_id = NULL;
6391 int obj_type, fd = -1;
6392 int *pack_fds = NULL;
6394 err = got_object_open_as_commit(&commit, s->repo,
6395 &s->blamed_commit->id);
6399 fd = got_opentempfd();
6401 err = got_error_from_errno("got_opentempfd");
6405 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6409 err = got_object_get_type(&obj_type, s->repo, obj_id);
6413 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6414 err = got_error(GOT_ERR_OBJ_TYPE);
6418 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6421 blame->f = got_opentemp();
6422 if (blame->f == NULL) {
6423 err = got_error_from_errno("got_opentemp");
6426 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6427 &blame->line_offsets, blame->f, blob);
6430 if (blame->nlines == 0) {
6431 s->blame_complete = 1;
6435 /* Don't include \n at EOF in the blame line count. */
6436 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6439 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6440 if (blame->lines == NULL) {
6441 err = got_error_from_errno("calloc");
6445 err = got_repo_pack_fds_open(&pack_fds);
6448 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6453 blame->pack_fds = pack_fds;
6454 blame->cb_args.view = view;
6455 blame->cb_args.lines = blame->lines;
6456 blame->cb_args.nlines = blame->nlines;
6457 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6458 if (blame->cb_args.commit_id == NULL) {
6459 err = got_error_from_errno("got_object_id_dup");
6462 blame->cb_args.quit = &s->done;
6464 blame->thread_args.path = s->path;
6465 blame->thread_args.repo = thread_repo;
6466 blame->thread_args.cb_args = &blame->cb_args;
6467 blame->thread_args.complete = &s->blame_complete;
6468 blame->thread_args.cancel_cb = cancel_blame_view;
6469 blame->thread_args.cancel_arg = &s->done;
6470 s->blame_complete = 0;
6472 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6473 s->first_displayed_line = 1;
6474 s->last_displayed_line = view->nlines;
6475 s->selected_line = 1;
6477 s->matched_line = 0;
6481 got_object_commit_close(commit);
6482 if (fd != -1 && close(fd) == -1 && err == NULL)
6483 err = got_error_from_errno("close");
6485 got_object_blob_close(blob);
6492 static const struct got_error *
6493 open_blame_view(struct tog_view *view, char *path,
6494 struct got_object_id *commit_id, struct got_repository *repo)
6496 const struct got_error *err = NULL;
6497 struct tog_blame_view_state *s = &view->state.blame;
6499 STAILQ_INIT(&s->blamed_commits);
6501 s->path = strdup(path);
6502 if (s->path == NULL)
6503 return got_error_from_errno("strdup");
6505 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6511 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6512 s->first_displayed_line = 1;
6513 s->last_displayed_line = view->nlines;
6514 s->selected_line = 1;
6515 s->blame_complete = 0;
6517 s->commit_id = commit_id;
6518 memset(&s->blame, 0, sizeof(s->blame));
6520 STAILQ_INIT(&s->colors);
6521 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6522 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6523 get_color_value("TOG_COLOR_COMMIT"));
6528 view->show = show_blame_view;
6529 view->input = input_blame_view;
6530 view->reset = reset_blame_view;
6531 view->close = close_blame_view;
6532 view->search_start = search_start_blame_view;
6533 view->search_setup = search_setup_blame_view;
6534 view->search_next = search_next_view_match;
6536 if (using_mock_io) {
6537 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6540 rc = pthread_cond_init(&bta->blame_complete, NULL);
6542 return got_error_set_errno(rc, "pthread_cond_init");
6545 return run_blame(view);
6548 static const struct got_error *
6549 close_blame_view(struct tog_view *view)
6551 const struct got_error *err = NULL;
6552 struct tog_blame_view_state *s = &view->state.blame;
6554 if (s->blame.thread)
6555 err = stop_blame(&s->blame);
6557 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6558 struct got_object_qid *blamed_commit;
6559 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6560 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6561 got_object_qid_free(blamed_commit);
6564 if (using_mock_io) {
6565 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6568 rc = pthread_cond_destroy(&bta->blame_complete);
6569 if (rc && err == NULL)
6570 err = got_error_set_errno(rc, "pthread_cond_destroy");
6574 free_colors(&s->colors);
6578 static const struct got_error *
6579 search_start_blame_view(struct tog_view *view)
6581 struct tog_blame_view_state *s = &view->state.blame;
6583 s->matched_line = 0;
6588 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6589 size_t *nlines, int **first, int **last, int **match, int **selected)
6591 struct tog_blame_view_state *s = &view->state.blame;
6594 *nlines = s->blame.nlines;
6595 *line_offsets = s->blame.line_offsets;
6596 *match = &s->matched_line;
6597 *first = &s->first_displayed_line;
6598 *last = &s->last_displayed_line;
6599 *selected = &s->selected_line;
6602 static const struct got_error *
6603 show_blame_view(struct tog_view *view)
6605 const struct got_error *err = NULL;
6606 struct tog_blame_view_state *s = &view->state.blame;
6609 if (s->blame.thread == NULL && !s->blame_complete) {
6610 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6611 &s->blame.thread_args);
6613 return got_error_set_errno(errcode, "pthread_create");
6616 halfdelay(1); /* fast refresh while annotating */
6619 if (s->blame_complete && !using_mock_io)
6620 halfdelay(10); /* disable fast refresh */
6622 err = draw_blame(view);
6628 static const struct got_error *
6629 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6630 struct got_repository *repo, struct got_object_id *id)
6632 struct tog_view *log_view;
6633 const struct got_error *err = NULL;
6637 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6638 if (log_view == NULL)
6639 return got_error_from_errno("view_open");
6641 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6643 view_close(log_view);
6645 *new_view = log_view;
6650 static const struct got_error *
6651 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6653 const struct got_error *err = NULL, *thread_err = NULL;
6654 struct tog_view *diff_view;
6655 struct tog_blame_view_state *s = &view->state.blame;
6656 int eos, nscroll, begin_y = 0, begin_x = 0;
6658 eos = nscroll = view->nlines - 2;
6659 if (view_is_hsplit_top(view))
6669 horizontal_scroll_input(view, ch);
6676 s->selected_line = 1;
6677 s->first_displayed_line = 1;
6682 if (s->blame.nlines < eos) {
6683 s->selected_line = s->blame.nlines;
6684 s->first_displayed_line = 1;
6686 s->selected_line = eos;
6687 s->first_displayed_line = s->blame.nlines - (eos - 1);
6694 if (s->selected_line > 1)
6696 else if (s->selected_line == 1 &&
6697 s->first_displayed_line > 1)
6698 s->first_displayed_line--;
6709 if (s->first_displayed_line == 1) {
6710 if (view->count > 1)
6712 s->selected_line = MAX(1, s->selected_line - nscroll);
6716 if (s->first_displayed_line > nscroll)
6717 s->first_displayed_line -= nscroll;
6719 s->first_displayed_line = 1;
6724 if (s->selected_line < eos && s->first_displayed_line +
6725 s->selected_line <= s->blame.nlines)
6727 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6728 s->first_displayed_line++;
6734 struct got_object_id *id = NULL;
6737 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6738 s->first_displayed_line, s->selected_line);
6742 struct got_commit_object *commit, *pcommit;
6743 struct got_object_qid *pid;
6744 struct got_object_id *blob_id = NULL;
6746 err = got_object_open_as_commit(&commit,
6751 got_object_commit_get_parent_ids(commit));
6753 got_object_commit_close(commit);
6756 /* Check if path history ends here. */
6757 err = got_object_open_as_commit(&pcommit,
6761 err = got_object_id_by_path(&blob_id, s->repo,
6763 got_object_commit_close(pcommit);
6765 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6767 got_object_commit_close(commit);
6770 err = got_object_get_type(&obj_type, s->repo,
6773 /* Can't blame non-blob type objects. */
6774 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6775 got_object_commit_close(commit);
6778 err = got_object_qid_alloc(&s->blamed_commit,
6780 got_object_commit_close(commit);
6782 if (got_object_id_cmp(id,
6783 &s->blamed_commit->id) == 0)
6785 err = got_object_qid_alloc(&s->blamed_commit,
6791 thread_err = stop_blame(&s->blame);
6795 STAILQ_INSERT_HEAD(&s->blamed_commits,
6796 s->blamed_commit, entry);
6797 err = run_blame(view);
6803 struct got_object_qid *first;
6806 first = STAILQ_FIRST(&s->blamed_commits);
6807 if (!got_object_id_cmp(&first->id, s->commit_id))
6810 thread_err = stop_blame(&s->blame);
6814 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6815 got_object_qid_free(s->blamed_commit);
6817 STAILQ_FIRST(&s->blamed_commits);
6818 err = run_blame(view);
6825 s->id_to_log = get_selected_commit_id(s->blame.lines,
6826 s->blame.nlines, s->first_displayed_line, s->selected_line);
6828 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6832 struct got_object_id *id = NULL;
6833 struct got_object_qid *pid;
6834 struct got_commit_object *commit = NULL;
6837 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6838 s->first_displayed_line, s->selected_line);
6841 err = got_object_open_as_commit(&commit, s->repo, id);
6844 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6846 /* traversed from diff view, release diff resources */
6847 err = close_diff_view(*new_view);
6850 diff_view = *new_view;
6852 if (view_is_parent_view(view))
6853 view_get_split(view, &begin_y, &begin_x);
6855 diff_view = view_open(0, 0, begin_y, begin_x,
6857 if (diff_view == NULL) {
6858 got_object_commit_close(commit);
6859 err = got_error_from_errno("view_open");
6863 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6864 id, NULL, NULL, 3, 0, 0, view, s->repo);
6865 got_object_commit_close(commit);
6867 view_close(diff_view);
6870 s->last_diffed_line = s->first_displayed_line - 1 +
6873 break; /* still open from active diff view */
6874 if (view_is_parent_view(view) &&
6875 view->mode == TOG_VIEW_SPLIT_HRZN) {
6876 err = view_init_hsplit(view, begin_y);
6882 diff_view->focussed = 1;
6883 diff_view->mode = view->mode;
6884 diff_view->nlines = view->lines - begin_y;
6885 if (view_is_parent_view(view)) {
6886 view_transfer_size(diff_view, view);
6887 err = view_close_child(view);
6890 err = view_set_child(view, diff_view);
6893 view->focus_child = 1;
6895 *new_view = diff_view;
6908 if (s->last_displayed_line >= s->blame.nlines &&
6909 s->selected_line >= MIN(s->blame.nlines,
6910 view->nlines - 2)) {
6914 if (s->last_displayed_line >= s->blame.nlines &&
6915 s->selected_line < view->nlines - 2) {
6917 MIN(nscroll, s->last_displayed_line -
6918 s->first_displayed_line - s->selected_line + 1);
6920 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6921 s->first_displayed_line += nscroll;
6923 s->first_displayed_line =
6924 s->blame.nlines - (view->nlines - 3);
6927 if (s->selected_line > view->nlines - 2) {
6928 s->selected_line = MIN(s->blame.nlines,
6936 return thread_err ? thread_err : err;
6939 static const struct got_error *
6940 reset_blame_view(struct tog_view *view)
6942 const struct got_error *err;
6943 struct tog_blame_view_state *s = &view->state.blame;
6947 err = stop_blame(&s->blame);
6951 return run_blame(view);
6954 static const struct got_error *
6955 cmd_blame(int argc, char *argv[])
6957 const struct got_error *error;
6958 struct got_repository *repo = NULL;
6959 struct got_worktree *worktree = NULL;
6960 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6961 char *link_target = NULL;
6962 struct got_object_id *commit_id = NULL;
6963 struct got_commit_object *commit = NULL;
6964 char *commit_id_str = NULL;
6966 struct tog_view *view = NULL;
6967 int *pack_fds = NULL;
6969 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6972 commit_id_str = optarg;
6975 repo_path = realpath(optarg, NULL);
6976 if (repo_path == NULL)
6977 return got_error_from_errno2("realpath",
6992 error = got_repo_pack_fds_open(&pack_fds);
6996 if (repo_path == NULL) {
6997 cwd = getcwd(NULL, 0);
6999 return got_error_from_errno("getcwd");
7000 error = got_worktree_open(&worktree, cwd);
7001 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7005 strdup(got_worktree_get_repo_path(worktree));
7007 repo_path = strdup(cwd);
7008 if (repo_path == NULL) {
7009 error = got_error_from_errno("strdup");
7014 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7018 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
7025 error = apply_unveil(got_repo_get_path(repo), NULL);
7029 error = tog_load_refs(repo, 0);
7033 if (commit_id_str == NULL) {
7034 struct got_reference *head_ref;
7035 error = got_ref_open(&head_ref, repo, worktree ?
7036 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
7039 error = got_ref_resolve(&commit_id, repo, head_ref);
7040 got_ref_close(head_ref);
7042 error = got_repo_match_object_id(&commit_id, NULL,
7043 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7048 error = got_object_open_as_commit(&commit, repo, commit_id);
7052 error = got_object_resolve_symlinks(&link_target, in_repo_path,
7057 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
7059 error = got_error_from_errno("view_open");
7062 error = open_blame_view(view, link_target ? link_target : in_repo_path,
7064 if (error != NULL) {
7065 if (view->close == NULL)
7066 close_blame_view(view);
7071 /* Release work tree lock. */
7072 got_worktree_close(worktree);
7075 error = view_loop(view);
7083 got_object_commit_close(commit);
7085 got_worktree_close(worktree);
7087 const struct got_error *close_err = got_repo_close(repo);
7092 const struct got_error *pack_err =
7093 got_repo_pack_fds_close(pack_fds);
7101 static const struct got_error *
7102 draw_tree_entries(struct tog_view *view, const char *parent_path)
7104 struct tog_tree_view_state *s = &view->state.tree;
7105 const struct got_error *err = NULL;
7106 struct got_tree_entry *te;
7109 struct tog_color *tc;
7110 int width, n, nentries, scrollx, i = 1;
7111 int limit = view->nlines;
7114 if (view_is_hsplit_top(view))
7115 --limit; /* border */
7117 werase(view->window);
7122 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7126 if (view_needs_focus_indication(view))
7127 wstandout(view->window);
7128 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7130 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7131 waddwstr(view->window, wline);
7134 while (width++ < view->ncols)
7135 waddch(view->window, ' ');
7137 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7138 if (view_needs_focus_indication(view))
7139 wstandend(view->window);
7144 if (s->first_displayed_entry) {
7145 i += got_tree_entry_get_index(s->first_displayed_entry);
7146 if (s->tree != s->root)
7147 ++i; /* account for ".." entry */
7149 nentries = got_object_tree_get_nentries(s->tree);
7150 if (asprintf(&index, "[%d/%d] %s",
7151 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7152 return got_error_from_errno("asprintf");
7153 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7157 waddwstr(view->window, wline);
7160 if (width < view->ncols - 1)
7161 waddch(view->window, '\n');
7164 waddch(view->window, '\n');
7168 if (s->first_displayed_entry == NULL) {
7169 te = got_object_tree_get_first_entry(s->tree);
7170 if (s->selected == 0) {
7172 wstandout(view->window);
7173 s->selected_entry = NULL;
7175 waddstr(view->window, " ..\n"); /* parent directory */
7176 if (s->selected == 0 && view->focussed)
7177 wstandend(view->window);
7184 te = s->first_displayed_entry;
7188 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7189 char *line = NULL, *id_str = NULL, *link_target = NULL;
7190 const char *modestr = "";
7193 te = got_object_tree_get_entry(s->tree, i);
7194 mode = got_tree_entry_get_mode(te);
7197 err = got_object_id_str(&id_str,
7198 got_tree_entry_get_id(te));
7200 return got_error_from_errno(
7201 "got_object_id_str");
7203 if (got_object_tree_entry_is_submodule(te))
7205 else if (S_ISLNK(mode)) {
7208 err = got_tree_entry_get_symlink_target(&link_target,
7214 for (i = 0; i < strlen(link_target); i++) {
7215 if (!isprint((unsigned char)link_target[i]))
7216 link_target[i] = '?';
7220 else if (S_ISDIR(mode))
7222 else if (mode & S_IXUSR)
7224 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7225 got_tree_entry_get_name(te), modestr,
7226 link_target ? " -> ": "",
7227 link_target ? link_target : "") == -1) {
7230 return got_error_from_errno("asprintf");
7235 /* use full line width to determine view->maxx */
7236 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7241 view->maxx = MAX(view->maxx, width);
7245 err = format_line(&wline, &width, &scrollx, line, view->x,
7251 if (n == s->selected) {
7253 wstandout(view->window);
7254 s->selected_entry = te;
7256 tc = match_color(&s->colors, line);
7258 wattr_on(view->window,
7259 COLOR_PAIR(tc->colorpair), NULL);
7260 waddwstr(view->window, &wline[scrollx]);
7262 wattr_off(view->window,
7263 COLOR_PAIR(tc->colorpair), NULL);
7264 if (width < view->ncols)
7265 waddch(view->window, '\n');
7266 if (n == s->selected && view->focussed)
7267 wstandend(view->window);
7273 s->last_displayed_entry = te;
7282 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7284 struct got_tree_entry *te;
7285 int isroot = s->tree == s->root;
7288 if (s->first_displayed_entry == NULL)
7291 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7292 while (i++ < maxscroll) {
7295 s->first_displayed_entry = NULL;
7298 s->first_displayed_entry = te;
7299 te = got_tree_entry_get_prev(s->tree, te);
7303 static const struct got_error *
7304 tree_scroll_down(struct tog_view *view, int maxscroll)
7306 struct tog_tree_view_state *s = &view->state.tree;
7307 struct got_tree_entry *next, *last;
7310 if (s->first_displayed_entry)
7311 next = got_tree_entry_get_next(s->tree,
7312 s->first_displayed_entry);
7314 next = got_object_tree_get_first_entry(s->tree);
7316 last = s->last_displayed_entry;
7317 while (next && n++ < maxscroll) {
7319 s->last_displayed_entry = last;
7320 last = got_tree_entry_get_next(s->tree, last);
7322 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7323 s->first_displayed_entry = next;
7324 next = got_tree_entry_get_next(s->tree, next);
7331 static const struct got_error *
7332 tree_entry_path(char **path, struct tog_parent_trees *parents,
7333 struct got_tree_entry *te)
7335 const struct got_error *err = NULL;
7336 struct tog_parent_tree *pt;
7337 size_t len = 2; /* for leading slash and NUL */
7339 TAILQ_FOREACH(pt, parents, entry)
7340 len += strlen(got_tree_entry_get_name(pt->selected_entry))
7343 len += strlen(got_tree_entry_get_name(te));
7345 *path = calloc(1, len);
7347 return got_error_from_errno("calloc");
7350 pt = TAILQ_LAST(parents, tog_parent_trees);
7352 const char *name = got_tree_entry_get_name(pt->selected_entry);
7353 if (strlcat(*path, name, len) >= len) {
7354 err = got_error(GOT_ERR_NO_SPACE);
7357 if (strlcat(*path, "/", len) >= len) {
7358 err = got_error(GOT_ERR_NO_SPACE);
7361 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7364 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7365 err = got_error(GOT_ERR_NO_SPACE);
7377 static const struct got_error *
7378 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7379 struct got_tree_entry *te, struct tog_parent_trees *parents,
7380 struct got_object_id *commit_id, struct got_repository *repo)
7382 const struct got_error *err = NULL;
7384 struct tog_view *blame_view;
7388 err = tree_entry_path(&path, parents, te);
7392 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7393 if (blame_view == NULL) {
7394 err = got_error_from_errno("view_open");
7398 err = open_blame_view(blame_view, path, commit_id, repo);
7400 if (err->code == GOT_ERR_CANCELLED)
7402 view_close(blame_view);
7404 *new_view = blame_view;
7410 static const struct got_error *
7411 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7412 struct tog_tree_view_state *s)
7414 struct tog_view *log_view;
7415 const struct got_error *err = NULL;
7420 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7421 if (log_view == NULL)
7422 return got_error_from_errno("view_open");
7424 err = tree_entry_path(&path, &s->parents, s->selected_entry);
7428 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7431 view_close(log_view);
7433 *new_view = log_view;
7438 static const struct got_error *
7439 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7440 const char *head_ref_name, struct got_repository *repo)
7442 const struct got_error *err = NULL;
7443 char *commit_id_str = NULL;
7444 struct tog_tree_view_state *s = &view->state.tree;
7445 struct got_commit_object *commit = NULL;
7447 TAILQ_INIT(&s->parents);
7448 STAILQ_INIT(&s->colors);
7450 s->commit_id = got_object_id_dup(commit_id);
7451 if (s->commit_id == NULL) {
7452 err = got_error_from_errno("got_object_id_dup");
7456 err = got_object_open_as_commit(&commit, repo, commit_id);
7461 * The root is opened here and will be closed when the view is closed.
7462 * Any visited subtrees and their path-wise parents are opened and
7465 err = got_object_open_as_tree(&s->root, repo,
7466 got_object_commit_get_tree_id(commit));
7471 err = got_object_id_str(&commit_id_str, commit_id);
7475 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7476 err = got_error_from_errno("asprintf");
7480 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7481 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7482 if (head_ref_name) {
7483 s->head_ref_name = strdup(head_ref_name);
7484 if (s->head_ref_name == NULL) {
7485 err = got_error_from_errno("strdup");
7491 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7492 err = add_color(&s->colors, "\\$$",
7493 TOG_COLOR_TREE_SUBMODULE,
7494 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7497 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7498 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7501 err = add_color(&s->colors, "/$",
7502 TOG_COLOR_TREE_DIRECTORY,
7503 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7507 err = add_color(&s->colors, "\\*$",
7508 TOG_COLOR_TREE_EXECUTABLE,
7509 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7513 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7514 get_color_value("TOG_COLOR_COMMIT"));
7519 view->show = show_tree_view;
7520 view->input = input_tree_view;
7521 view->close = close_tree_view;
7522 view->search_start = search_start_tree_view;
7523 view->search_next = search_next_tree_view;
7525 free(commit_id_str);
7527 got_object_commit_close(commit);
7529 if (view->close == NULL)
7530 close_tree_view(view);
7536 static const struct got_error *
7537 close_tree_view(struct tog_view *view)
7539 struct tog_tree_view_state *s = &view->state.tree;
7541 free_colors(&s->colors);
7542 free(s->tree_label);
7543 s->tree_label = NULL;
7545 s->commit_id = NULL;
7546 free(s->head_ref_name);
7547 s->head_ref_name = NULL;
7548 while (!TAILQ_EMPTY(&s->parents)) {
7549 struct tog_parent_tree *parent;
7550 parent = TAILQ_FIRST(&s->parents);
7551 TAILQ_REMOVE(&s->parents, parent, entry);
7552 if (parent->tree != s->root)
7553 got_object_tree_close(parent->tree);
7557 if (s->tree != NULL && s->tree != s->root)
7558 got_object_tree_close(s->tree);
7560 got_object_tree_close(s->root);
7564 static const struct got_error *
7565 search_start_tree_view(struct tog_view *view)
7567 struct tog_tree_view_state *s = &view->state.tree;
7569 s->matched_entry = NULL;
7574 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7576 regmatch_t regmatch;
7578 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7582 static const struct got_error *
7583 search_next_tree_view(struct tog_view *view)
7585 struct tog_tree_view_state *s = &view->state.tree;
7586 struct got_tree_entry *te = NULL;
7588 if (!view->searching) {
7589 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7593 if (s->matched_entry) {
7594 if (view->searching == TOG_SEARCH_FORWARD) {
7595 if (s->selected_entry)
7596 te = got_tree_entry_get_next(s->tree,
7599 te = got_object_tree_get_first_entry(s->tree);
7601 if (s->selected_entry == NULL)
7602 te = got_object_tree_get_last_entry(s->tree);
7604 te = got_tree_entry_get_prev(s->tree,
7608 if (s->selected_entry)
7609 te = s->selected_entry;
7610 else if (view->searching == TOG_SEARCH_FORWARD)
7611 te = got_object_tree_get_first_entry(s->tree);
7613 te = got_object_tree_get_last_entry(s->tree);
7618 if (s->matched_entry == NULL) {
7619 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7622 if (view->searching == TOG_SEARCH_FORWARD)
7623 te = got_object_tree_get_first_entry(s->tree);
7625 te = got_object_tree_get_last_entry(s->tree);
7628 if (match_tree_entry(te, &view->regex)) {
7629 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7630 s->matched_entry = te;
7634 if (view->searching == TOG_SEARCH_FORWARD)
7635 te = got_tree_entry_get_next(s->tree, te);
7637 te = got_tree_entry_get_prev(s->tree, te);
7640 if (s->matched_entry) {
7641 s->first_displayed_entry = s->matched_entry;
7648 static const struct got_error *
7649 show_tree_view(struct tog_view *view)
7651 const struct got_error *err = NULL;
7652 struct tog_tree_view_state *s = &view->state.tree;
7655 err = tree_entry_path(&parent_path, &s->parents, NULL);
7659 err = draw_tree_entries(view, parent_path);
7666 static const struct got_error *
7667 tree_goto_line(struct tog_view *view, int nlines)
7669 const struct got_error *err = NULL;
7670 struct tog_tree_view_state *s = &view->state.tree;
7671 struct got_tree_entry **fte, **lte, **ste;
7672 int g, last, first = 1, i = 1;
7673 int root = s->tree == s->root;
7674 int off = root ? 1 : 2;
7681 else if (g > got_object_tree_get_nentries(s->tree))
7682 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7684 fte = &s->first_displayed_entry;
7685 lte = &s->last_displayed_entry;
7686 ste = &s->selected_entry;
7689 first = got_tree_entry_get_index(*fte);
7690 first += off; /* account for ".." */
7692 last = got_tree_entry_get_index(*lte);
7695 if (g >= first && g <= last && g - first < nlines) {
7696 s->selected = g - first;
7697 return NULL; /* gline is on the current page */
7701 i = got_tree_entry_get_index(*ste);
7706 err = tree_scroll_down(view, g - i);
7709 if (got_tree_entry_get_index(*lte) >=
7710 got_object_tree_get_nentries(s->tree) - 1 &&
7711 first + s->selected < g &&
7712 s->selected < s->ndisplayed - 1) {
7713 first = got_tree_entry_get_index(*fte);
7715 s->selected = g - first;
7718 tree_scroll_up(s, i - g);
7721 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7722 s->selected = g - 1;
7727 static const struct got_error *
7728 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7730 const struct got_error *err = NULL;
7731 struct tog_tree_view_state *s = &view->state.tree;
7732 struct got_tree_entry *te;
7733 int n, nscroll = view->nlines - 3;
7736 return tree_goto_line(view, nscroll);
7745 horizontal_scroll_input(view, ch);
7748 s->show_ids = !s->show_ids;
7753 if (!s->selected_entry)
7755 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7759 err = view_request_new(new_view, view, TOG_VIEW_REF);
7766 if (s->tree == s->root)
7767 s->first_displayed_entry =
7768 got_object_tree_get_first_entry(s->tree);
7770 s->first_displayed_entry = NULL;
7775 int eos = view->nlines - 3;
7777 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7781 te = got_object_tree_get_last_entry(s->tree);
7782 for (n = 0; n < eos; n++) {
7784 if (s->tree != s->root) {
7785 s->first_displayed_entry = NULL;
7790 s->first_displayed_entry = te;
7791 te = got_tree_entry_get_prev(s->tree, te);
7794 s->selected = n - 1;
7800 if (s->selected > 0) {
7804 tree_scroll_up(s, 1);
7805 if (s->selected_entry == NULL ||
7806 (s->tree == s->root && s->selected_entry ==
7807 got_object_tree_get_first_entry(s->tree)))
7817 if (s->tree == s->root) {
7818 if (got_object_tree_get_first_entry(s->tree) ==
7819 s->first_displayed_entry)
7820 s->selected -= MIN(s->selected, nscroll);
7822 if (s->first_displayed_entry == NULL)
7823 s->selected -= MIN(s->selected, nscroll);
7825 tree_scroll_up(s, MAX(0, nscroll));
7826 if (s->selected_entry == NULL ||
7827 (s->tree == s->root && s->selected_entry ==
7828 got_object_tree_get_first_entry(s->tree)))
7834 if (s->selected < s->ndisplayed - 1) {
7838 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7840 /* can't scroll any further */
7844 tree_scroll_down(view, 1);
7854 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7856 /* can't scroll any further; move cursor down */
7857 if (s->selected < s->ndisplayed - 1)
7858 s->selected += MIN(nscroll,
7859 s->ndisplayed - s->selected - 1);
7864 tree_scroll_down(view, nscroll);
7869 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7870 struct tog_parent_tree *parent;
7871 /* user selected '..' */
7872 if (s->tree == s->root) {
7876 parent = TAILQ_FIRST(&s->parents);
7877 TAILQ_REMOVE(&s->parents, parent,
7879 got_object_tree_close(s->tree);
7880 s->tree = parent->tree;
7881 s->first_displayed_entry =
7882 parent->first_displayed_entry;
7884 parent->selected_entry;
7885 s->selected = parent->selected;
7886 if (s->selected > view->nlines - 3) {
7887 err = offset_selection_down(view);
7892 } else if (S_ISDIR(got_tree_entry_get_mode(
7893 s->selected_entry))) {
7894 struct got_tree_object *subtree;
7896 err = got_object_open_as_tree(&subtree, s->repo,
7897 got_tree_entry_get_id(s->selected_entry));
7900 err = tree_view_visit_subtree(s, subtree);
7902 got_object_tree_close(subtree);
7905 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7906 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7909 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7910 s->selected = view->nlines - 4;
7926 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7931 static const struct got_error *
7932 cmd_tree(int argc, char *argv[])
7934 const struct got_error *error;
7935 struct got_repository *repo = NULL;
7936 struct got_worktree *worktree = NULL;
7937 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7938 struct got_object_id *commit_id = NULL;
7939 struct got_commit_object *commit = NULL;
7940 const char *commit_id_arg = NULL;
7942 struct got_reference *ref = NULL;
7943 const char *head_ref_name = NULL;
7945 struct tog_view *view;
7946 int *pack_fds = NULL;
7948 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7951 commit_id_arg = optarg;
7954 repo_path = realpath(optarg, NULL);
7955 if (repo_path == NULL)
7956 return got_error_from_errno2("realpath",
7971 error = got_repo_pack_fds_open(&pack_fds);
7975 if (repo_path == NULL) {
7976 cwd = getcwd(NULL, 0);
7978 return got_error_from_errno("getcwd");
7979 error = got_worktree_open(&worktree, cwd);
7980 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7984 strdup(got_worktree_get_repo_path(worktree));
7986 repo_path = strdup(cwd);
7987 if (repo_path == NULL) {
7988 error = got_error_from_errno("strdup");
7993 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7997 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8004 error = apply_unveil(got_repo_get_path(repo), NULL);
8008 error = tog_load_refs(repo, 0);
8012 if (commit_id_arg == NULL) {
8013 error = got_repo_match_object_id(&commit_id, &label,
8014 worktree ? got_worktree_get_head_ref_name(worktree) :
8015 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8018 head_ref_name = label;
8020 error = got_ref_open(&ref, repo, commit_id_arg, 0);
8022 head_ref_name = got_ref_get_name(ref);
8023 else if (error->code != GOT_ERR_NOT_REF)
8025 error = got_repo_match_object_id(&commit_id, NULL,
8026 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8031 error = got_object_open_as_commit(&commit, repo, commit_id);
8035 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
8037 error = got_error_from_errno("view_open");
8040 error = open_tree_view(view, commit_id, head_ref_name, repo);
8043 if (!got_path_is_root_dir(in_repo_path)) {
8044 error = tree_view_walk_path(&view->state.tree, commit,
8051 /* Release work tree lock. */
8052 got_worktree_close(worktree);
8055 error = view_loop(view);
8064 const struct got_error *close_err = got_repo_close(repo);
8069 const struct got_error *pack_err =
8070 got_repo_pack_fds_close(pack_fds);
8078 static const struct got_error *
8079 ref_view_load_refs(struct tog_ref_view_state *s)
8081 struct got_reflist_entry *sre;
8082 struct tog_reflist_entry *re;
8085 TAILQ_FOREACH(sre, &tog_refs, entry) {
8086 if (strncmp(got_ref_get_name(sre->ref),
8087 "refs/got/", 9) == 0 &&
8088 strncmp(got_ref_get_name(sre->ref),
8089 "refs/got/backup/", 16) != 0)
8092 re = malloc(sizeof(*re));
8094 return got_error_from_errno("malloc");
8096 re->ref = got_ref_dup(sre->ref);
8097 if (re->ref == NULL)
8098 return got_error_from_errno("got_ref_dup");
8099 re->idx = s->nrefs++;
8100 TAILQ_INSERT_TAIL(&s->refs, re, entry);
8103 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8108 ref_view_free_refs(struct tog_ref_view_state *s)
8110 struct tog_reflist_entry *re;
8112 while (!TAILQ_EMPTY(&s->refs)) {
8113 re = TAILQ_FIRST(&s->refs);
8114 TAILQ_REMOVE(&s->refs, re, entry);
8115 got_ref_close(re->ref);
8120 static const struct got_error *
8121 open_ref_view(struct tog_view *view, struct got_repository *repo)
8123 const struct got_error *err = NULL;
8124 struct tog_ref_view_state *s = &view->state.ref;
8126 s->selected_entry = 0;
8129 TAILQ_INIT(&s->refs);
8130 STAILQ_INIT(&s->colors);
8132 err = ref_view_load_refs(s);
8136 if (has_colors() && getenv("TOG_COLORS") != NULL) {
8137 err = add_color(&s->colors, "^refs/heads/",
8138 TOG_COLOR_REFS_HEADS,
8139 get_color_value("TOG_COLOR_REFS_HEADS"));
8143 err = add_color(&s->colors, "^refs/tags/",
8144 TOG_COLOR_REFS_TAGS,
8145 get_color_value("TOG_COLOR_REFS_TAGS"));
8149 err = add_color(&s->colors, "^refs/remotes/",
8150 TOG_COLOR_REFS_REMOTES,
8151 get_color_value("TOG_COLOR_REFS_REMOTES"));
8155 err = add_color(&s->colors, "^refs/got/backup/",
8156 TOG_COLOR_REFS_BACKUP,
8157 get_color_value("TOG_COLOR_REFS_BACKUP"));
8162 view->show = show_ref_view;
8163 view->input = input_ref_view;
8164 view->close = close_ref_view;
8165 view->search_start = search_start_ref_view;
8166 view->search_next = search_next_ref_view;
8169 if (view->close == NULL)
8170 close_ref_view(view);
8176 static const struct got_error *
8177 close_ref_view(struct tog_view *view)
8179 struct tog_ref_view_state *s = &view->state.ref;
8181 ref_view_free_refs(s);
8182 free_colors(&s->colors);
8187 static const struct got_error *
8188 resolve_reflist_entry(struct got_object_id **commit_id,
8189 struct tog_reflist_entry *re, struct got_repository *repo)
8191 const struct got_error *err = NULL;
8192 struct got_object_id *obj_id;
8193 struct got_tag_object *tag = NULL;
8198 err = got_ref_resolve(&obj_id, repo, re->ref);
8202 err = got_object_get_type(&obj_type, repo, obj_id);
8207 case GOT_OBJ_TYPE_COMMIT:
8208 *commit_id = obj_id;
8210 case GOT_OBJ_TYPE_TAG:
8211 err = got_object_open_as_tag(&tag, repo, obj_id);
8215 err = got_object_get_type(&obj_type, repo,
8216 got_object_tag_get_object_id(tag));
8219 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
8220 err = got_error(GOT_ERR_OBJ_TYPE);
8223 *commit_id = got_object_id_dup(
8224 got_object_tag_get_object_id(tag));
8225 if (*commit_id == NULL) {
8226 err = got_error_from_errno("got_object_id_dup");
8231 err = got_error(GOT_ERR_OBJ_TYPE);
8237 got_object_tag_close(tag);
8245 static const struct got_error *
8246 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
8247 struct tog_reflist_entry *re, struct got_repository *repo)
8249 struct tog_view *log_view;
8250 const struct got_error *err = NULL;
8251 struct got_object_id *commit_id = NULL;
8255 err = resolve_reflist_entry(&commit_id, re, repo);
8257 if (err->code != GOT_ERR_OBJ_TYPE)
8263 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
8264 if (log_view == NULL) {
8265 err = got_error_from_errno("view_open");
8269 err = open_log_view(log_view, commit_id, repo,
8270 got_ref_get_name(re->ref), "", 0);
8273 view_close(log_view);
8275 *new_view = log_view;
8281 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
8283 struct tog_reflist_entry *re;
8286 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8289 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
8290 while (i++ < maxscroll) {
8293 s->first_displayed_entry = re;
8294 re = TAILQ_PREV(re, tog_reflist_head, entry);
8298 static const struct got_error *
8299 ref_scroll_down(struct tog_view *view, int maxscroll)
8301 struct tog_ref_view_state *s = &view->state.ref;
8302 struct tog_reflist_entry *next, *last;
8305 if (s->first_displayed_entry)
8306 next = TAILQ_NEXT(s->first_displayed_entry, entry);
8308 next = TAILQ_FIRST(&s->refs);
8310 last = s->last_displayed_entry;
8311 while (next && n++ < maxscroll) {
8313 s->last_displayed_entry = last;
8314 last = TAILQ_NEXT(last, entry);
8316 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
8317 s->first_displayed_entry = next;
8318 next = TAILQ_NEXT(next, entry);
8325 static const struct got_error *
8326 search_start_ref_view(struct tog_view *view)
8328 struct tog_ref_view_state *s = &view->state.ref;
8330 s->matched_entry = NULL;
8335 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8337 regmatch_t regmatch;
8339 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8343 static const struct got_error *
8344 search_next_ref_view(struct tog_view *view)
8346 struct tog_ref_view_state *s = &view->state.ref;
8347 struct tog_reflist_entry *re = NULL;
8349 if (!view->searching) {
8350 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8354 if (s->matched_entry) {
8355 if (view->searching == TOG_SEARCH_FORWARD) {
8356 if (s->selected_entry)
8357 re = TAILQ_NEXT(s->selected_entry, entry);
8359 re = TAILQ_PREV(s->selected_entry,
8360 tog_reflist_head, entry);
8362 if (s->selected_entry == NULL)
8363 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8365 re = TAILQ_PREV(s->selected_entry,
8366 tog_reflist_head, entry);
8369 if (s->selected_entry)
8370 re = s->selected_entry;
8371 else if (view->searching == TOG_SEARCH_FORWARD)
8372 re = TAILQ_FIRST(&s->refs);
8374 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8379 if (s->matched_entry == NULL) {
8380 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8383 if (view->searching == TOG_SEARCH_FORWARD)
8384 re = TAILQ_FIRST(&s->refs);
8386 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8389 if (match_reflist_entry(re, &view->regex)) {
8390 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8391 s->matched_entry = re;
8395 if (view->searching == TOG_SEARCH_FORWARD)
8396 re = TAILQ_NEXT(re, entry);
8398 re = TAILQ_PREV(re, tog_reflist_head, entry);
8401 if (s->matched_entry) {
8402 s->first_displayed_entry = s->matched_entry;
8409 static const struct got_error *
8410 show_ref_view(struct tog_view *view)
8412 const struct got_error *err = NULL;
8413 struct tog_ref_view_state *s = &view->state.ref;
8414 struct tog_reflist_entry *re;
8417 struct tog_color *tc;
8418 int width, n, scrollx;
8419 int limit = view->nlines;
8421 werase(view->window);
8424 if (view_is_hsplit_top(view))
8425 --limit; /* border */
8430 re = s->first_displayed_entry;
8432 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8434 return got_error_from_errno("asprintf");
8436 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8441 if (view_needs_focus_indication(view))
8442 wstandout(view->window);
8443 waddwstr(view->window, wline);
8444 while (width++ < view->ncols)
8445 waddch(view->window, ' ');
8446 if (view_needs_focus_indication(view))
8447 wstandend(view->window);
8457 while (re && limit > 0) {
8459 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8462 struct got_commit_object *ci;
8463 struct got_tag_object *tag;
8464 struct got_object_id *id;
8468 err = got_ref_resolve(&id, s->repo, re->ref);
8471 err = got_object_open_as_tag(&tag, s->repo, id);
8473 if (err->code != GOT_ERR_OBJ_TYPE) {
8477 err = got_object_open_as_commit(&ci, s->repo,
8483 t = got_object_commit_get_committer_time(ci);
8484 got_object_commit_close(ci);
8486 t = got_object_tag_get_tagger_time(tag);
8487 got_object_tag_close(tag);
8490 if (gmtime_r(&t, &tm) == NULL)
8491 return got_error_from_errno("gmtime_r");
8492 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8493 return got_error(GOT_ERR_NO_SPACE);
8495 if (got_ref_is_symbolic(re->ref)) {
8496 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8497 ymd : "", got_ref_get_name(re->ref),
8498 got_ref_get_symref_target(re->ref)) == -1)
8499 return got_error_from_errno("asprintf");
8500 } else if (s->show_ids) {
8501 struct got_object_id *id;
8503 err = got_ref_resolve(&id, s->repo, re->ref);
8506 err = got_object_id_str(&id_str, id);
8511 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8512 got_ref_get_name(re->ref), id_str) == -1) {
8513 err = got_error_from_errno("asprintf");
8520 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8521 got_ref_get_name(re->ref)) == -1)
8522 return got_error_from_errno("asprintf");
8524 /* use full line width to determine view->maxx */
8525 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8530 view->maxx = MAX(view->maxx, width);
8534 err = format_line(&wline, &width, &scrollx, line, view->x,
8540 if (n == s->selected) {
8542 wstandout(view->window);
8543 s->selected_entry = re;
8545 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8547 wattr_on(view->window,
8548 COLOR_PAIR(tc->colorpair), NULL);
8549 waddwstr(view->window, &wline[scrollx]);
8551 wattr_off(view->window,
8552 COLOR_PAIR(tc->colorpair), NULL);
8553 if (width < view->ncols)
8554 waddch(view->window, '\n');
8555 if (n == s->selected && view->focussed)
8556 wstandend(view->window);
8562 s->last_displayed_entry = re;
8565 re = TAILQ_NEXT(re, entry);
8572 static const struct got_error *
8573 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8574 struct tog_reflist_entry *re, struct got_repository *repo)
8576 const struct got_error *err = NULL;
8577 struct got_object_id *commit_id = NULL;
8578 struct tog_view *tree_view;
8582 err = resolve_reflist_entry(&commit_id, re, repo);
8584 if (err->code != GOT_ERR_OBJ_TYPE)
8591 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8592 if (tree_view == NULL) {
8593 err = got_error_from_errno("view_open");
8597 err = open_tree_view(tree_view, commit_id,
8598 got_ref_get_name(re->ref), repo);
8602 *new_view = tree_view;
8608 static const struct got_error *
8609 ref_goto_line(struct tog_view *view, int nlines)
8611 const struct got_error *err = NULL;
8612 struct tog_ref_view_state *s = &view->state.ref;
8613 int g, idx = s->selected_entry->idx;
8620 else if (g > s->nrefs)
8623 if (g >= s->first_displayed_entry->idx + 1 &&
8624 g <= s->last_displayed_entry->idx + 1 &&
8625 g - s->first_displayed_entry->idx - 1 < nlines) {
8626 s->selected = g - s->first_displayed_entry->idx - 1;
8631 err = ref_scroll_down(view, g - idx - 1);
8634 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8635 s->first_displayed_entry->idx + s->selected < g &&
8636 s->selected < s->ndisplayed - 1)
8637 s->selected = g - s->first_displayed_entry->idx - 1;
8638 } else if (idx + 1 > g)
8639 ref_scroll_up(s, idx - g + 1);
8641 if (g < nlines && s->first_displayed_entry->idx == 0)
8642 s->selected = g - 1;
8648 static const struct got_error *
8649 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8651 const struct got_error *err = NULL;
8652 struct tog_ref_view_state *s = &view->state.ref;
8653 struct tog_reflist_entry *re;
8654 int n, nscroll = view->nlines - 1;
8657 return ref_goto_line(view, nscroll);
8666 horizontal_scroll_input(view, ch);
8669 s->show_ids = !s->show_ids;
8673 s->show_date = !s->show_date;
8677 s->sort_by_date = !s->sort_by_date;
8678 view->action = s->sort_by_date ? "sort by date" : "sort by name";
8680 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8681 got_ref_cmp_by_commit_timestamp_descending :
8682 tog_ref_cmp_by_name, s->repo);
8685 got_reflist_object_id_map_free(tog_refs_idmap);
8686 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8687 &tog_refs, s->repo);
8690 ref_view_free_refs(s);
8691 err = ref_view_load_refs(s);
8696 if (!s->selected_entry)
8698 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8702 if (!s->selected_entry)
8704 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8711 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8716 int eos = view->nlines - 1;
8718 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8722 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8723 for (n = 0; n < eos; n++) {
8726 s->first_displayed_entry = re;
8727 re = TAILQ_PREV(re, tog_reflist_head, entry);
8730 s->selected = n - 1;
8736 if (s->selected > 0) {
8740 ref_scroll_up(s, 1);
8741 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8751 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8752 s->selected -= MIN(nscroll, s->selected);
8753 ref_scroll_up(s, MAX(0, nscroll));
8754 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8760 if (s->selected < s->ndisplayed - 1) {
8764 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8765 /* can't scroll any further */
8769 ref_scroll_down(view, 1);
8779 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8780 /* can't scroll any further; move cursor down */
8781 if (s->selected < s->ndisplayed - 1)
8782 s->selected += MIN(nscroll,
8783 s->ndisplayed - s->selected - 1);
8784 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8785 s->selected += s->ndisplayed - s->selected - 1;
8789 ref_scroll_down(view, nscroll);
8794 err = tog_load_refs(s->repo, s->sort_by_date);
8797 ref_view_free_refs(s);
8798 err = ref_view_load_refs(s);
8801 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8802 s->selected = view->nlines - 2;
8816 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8821 static const struct got_error *
8822 cmd_ref(int argc, char *argv[])
8824 const struct got_error *error;
8825 struct got_repository *repo = NULL;
8826 struct got_worktree *worktree = NULL;
8827 char *cwd = NULL, *repo_path = NULL;
8829 struct tog_view *view;
8830 int *pack_fds = NULL;
8832 while ((ch = getopt(argc, argv, "r:")) != -1) {
8835 repo_path = realpath(optarg, NULL);
8836 if (repo_path == NULL)
8837 return got_error_from_errno2("realpath",
8852 error = got_repo_pack_fds_open(&pack_fds);
8856 if (repo_path == NULL) {
8857 cwd = getcwd(NULL, 0);
8859 return got_error_from_errno("getcwd");
8860 error = got_worktree_open(&worktree, cwd);
8861 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8865 strdup(got_worktree_get_repo_path(worktree));
8867 repo_path = strdup(cwd);
8868 if (repo_path == NULL) {
8869 error = got_error_from_errno("strdup");
8874 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8880 error = apply_unveil(got_repo_get_path(repo), NULL);
8884 error = tog_load_refs(repo, 0);
8888 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8890 error = got_error_from_errno("view_open");
8894 error = open_ref_view(view, repo);
8899 /* Release work tree lock. */
8900 got_worktree_close(worktree);
8903 error = view_loop(view);
8908 const struct got_error *close_err = got_repo_close(repo);
8913 const struct got_error *pack_err =
8914 got_repo_pack_fds_close(pack_fds);
8922 static const struct got_error*
8923 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8932 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8936 if (mvwprintw(win, y, x, "%s", str) == ERR)
8937 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8944 static const struct got_error *
8945 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8949 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8951 free(*line_offsets);
8952 *line_offsets = NULL;
8953 return got_error_from_errno("reallocarray");
8957 (*line_offsets)[*nlines] = off;
8962 static const struct got_error *
8963 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8967 for (;n > 0; --n, ++km) {
8971 if (km->keys == NULL)
8974 t = t0 = strdup(km->keys);
8976 return got_error_from_errno("strdup");
8979 while ((k = strsep(&t, " ")) != NULL)
8980 len += strlen(k) > 1 ? 2 : 0;
8982 *ret = MAX(*ret, len);
8989 * Write keymap section headers, keys, and key info in km to f.
8990 * Save line offset to *off. If terminal has UTF8 encoding enabled,
8991 * wrap control and symbolic keys in guillemets, else use <>.
8993 static const struct got_error *
8994 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8999 static const char *u8_glyph[] = {
9000 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
9001 "\xe2\x80\xba" /* U+203A (utf8 >) */
9004 int cs, s, first = 1;
9006 cs = got_locale_is_utf8();
9008 t = t0 = strdup(km->keys);
9010 return got_error_from_errno("strdup");
9012 len = strlen(km->keys);
9013 while ((k = strsep(&t, " ")) != NULL) {
9014 s = strlen(k) > 1; /* control or symbolic key */
9015 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
9016 cs && s ? u8_glyph[0] : s ? "<" : "", k,
9017 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
9020 return got_error_from_errno("fprintf");
9028 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
9030 return got_error_from_errno("fprintf");
9036 static const struct got_error *
9037 format_help(struct tog_help_view_state *s)
9039 const struct got_error *err = NULL;
9041 int i, max, n, show = s->all;
9042 static const struct tog_key_map km[] = {
9043 #define KEYMAP_(info, type) { NULL, (info), type }
9044 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
9050 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
9055 err = max_key_str(&max, km, n);
9059 for (i = 0; i < n; ++i) {
9060 if (km[i].keys == NULL) {
9062 if (km[i].type == TOG_KEYMAP_GLOBAL ||
9063 km[i].type == s->type || s->all)
9067 err = format_help_line(&off, s->f, &km[i], max);
9070 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9077 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9081 static const struct got_error *
9082 create_help(struct tog_help_view_state *s)
9085 const struct got_error *err;
9087 free(s->line_offsets);
9088 s->line_offsets = NULL;
9093 return got_error_from_errno("got_opentemp");
9096 err = format_help(s);
9100 if (s->f && fflush(s->f) != 0)
9101 return got_error_from_errno("fflush");
9106 static const struct got_error *
9107 search_start_help_view(struct tog_view *view)
9109 view->state.help.matched_line = 0;
9114 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
9115 size_t *nlines, int **first, int **last, int **match, int **selected)
9117 struct tog_help_view_state *s = &view->state.help;
9120 *nlines = s->nlines;
9121 *line_offsets = s->line_offsets;
9122 *match = &s->matched_line;
9123 *first = &s->first_displayed_line;
9124 *last = &s->last_displayed_line;
9125 *selected = &s->selected_line;
9128 static const struct got_error *
9129 show_help_view(struct tog_view *view)
9131 struct tog_help_view_state *s = &view->state.help;
9132 const struct got_error *err;
9133 regmatch_t *regmatch = &view->regmatch;
9138 int width, nprinted = 0, rc = 0;
9139 int eos = view->nlines;
9141 if (view_is_hsplit_top(view))
9142 --eos; /* account for border */
9146 werase(view->window);
9148 if (view->gline > s->nlines - 1)
9149 view->gline = s->nlines - 1;
9151 err = win_draw_center(view->window, 0, 0, view->ncols,
9152 view_needs_focus_indication(view),
9153 "tog help (press q to return to tog)");
9158 waddstr(view->window, "\n\n");
9164 while (eos > 0 && nprinted < eos) {
9167 linelen = getline(&line, &linesz, s->f);
9168 if (linelen == -1) {
9171 return got_ferror(s->f, GOT_ERR_IO);
9176 if (++s->lineno < s->first_displayed_line)
9178 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
9180 if (s->lineno == view->hiline)
9183 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
9189 view->maxx = MAX(view->maxx, width);
9194 wattron(view->window, attr);
9195 if (s->first_displayed_line + nprinted == s->matched_line &&
9196 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
9197 err = add_matched_line(&width, line, view->ncols - 1, 0,
9198 view->window, view->x, regmatch);
9206 err = format_line(&wline, &width, &skip, line,
9207 view->x, view->ncols, 0, view->x ? 1 : 0);
9212 waddwstr(view->window, &wline[skip]);
9216 if (s->lineno == view->hiline) {
9217 while (width++ < view->ncols)
9218 waddch(view->window, ' ');
9220 if (width < view->ncols)
9221 waddch(view->window, '\n');
9224 wattroff(view->window, attr);
9225 if (++nprinted == 1)
9226 s->first_displayed_line = s->lineno;
9230 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
9232 s->last_displayed_line = s->first_displayed_line;
9237 rc = waddnstr(view->window,
9238 "See the tog(1) manual page for full documentation",
9241 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9243 wmove(view->window, view->nlines - 1, 0);
9244 wclrtoeol(view->window);
9245 wstandout(view->window);
9246 rc = waddnstr(view->window, "scroll down for more...",
9249 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9250 if (getcurx(view->window) < view->ncols - 6) {
9251 rc = wprintw(view->window, "[%.0f%%]",
9252 100.00 * s->last_displayed_line / s->nlines);
9254 return got_error_msg(GOT_ERR_IO, "wprintw");
9256 wstandend(view->window);
9262 static const struct got_error *
9263 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
9265 struct tog_help_view_state *s = &view->state.help;
9266 const struct got_error *err = NULL;
9272 eos = nscroll = view->nlines;
9273 if (view_is_hsplit_top(view))
9276 s->lineno = s->first_displayed_line - 1 + s->selected_line;
9285 horizontal_scroll_input(view, ch);
9289 s->first_displayed_line = 1;
9297 s->first_displayed_line = (s->nlines - eos) + 3;
9302 if (s->first_displayed_line > 1)
9303 --s->first_displayed_line;
9314 if (s->first_displayed_line == 1) {
9318 while (--nscroll > 0 && s->first_displayed_line > 1)
9319 s->first_displayed_line--;
9325 ++s->first_displayed_line;
9341 while (!s->eof && --nscroll > 0) {
9342 linelen = getline(&line, &linesz, s->f);
9343 s->first_displayed_line++;
9344 if (linelen == -1) {
9348 err = got_ferror(s->f, GOT_ERR_IO);
9362 static const struct got_error *
9363 close_help_view(struct tog_view *view)
9365 struct tog_help_view_state *s = &view->state.help;
9367 free(s->line_offsets);
9368 s->line_offsets = NULL;
9369 if (fclose(s->f) == EOF)
9370 return got_error_from_errno("fclose");
9375 static const struct got_error *
9376 reset_help_view(struct tog_view *view)
9378 struct tog_help_view_state *s = &view->state.help;
9381 if (s->f && fclose(s->f) == EOF)
9382 return got_error_from_errno("fclose");
9384 wclear(view->window);
9388 s->first_displayed_line = 1;
9389 s->last_displayed_line = view->nlines;
9390 s->matched_line = 0;
9392 return create_help(s);
9395 static const struct got_error *
9396 open_help_view(struct tog_view *view, struct tog_view *parent)
9398 const struct got_error *err = NULL;
9399 struct tog_help_view_state *s = &view->state.help;
9401 s->type = (enum tog_keymap_type)parent->type;
9402 s->first_displayed_line = 1;
9403 s->last_displayed_line = view->nlines;
9404 s->selected_line = 1;
9406 view->show = show_help_view;
9407 view->input = input_help_view;
9408 view->reset = reset_help_view;
9409 view->close = close_help_view;
9410 view->search_start = search_start_help_view;
9411 view->search_setup = search_setup_help_view;
9412 view->search_next = search_next_view_match;
9414 err = create_help(s);
9418 static const struct got_error *
9419 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9420 enum tog_view_type request, int y, int x)
9422 const struct got_error *err = NULL;
9428 if (view->type == TOG_VIEW_LOG) {
9429 struct tog_log_view_state *s = &view->state.log;
9431 err = open_diff_view_for_commit(new_view, y, x,
9432 s->selected_entry->commit, s->selected_entry->id,
9435 return got_error_msg(GOT_ERR_NOT_IMPL,
9436 "parent/child view pair not supported");
9438 case TOG_VIEW_BLAME:
9439 if (view->type == TOG_VIEW_TREE) {
9440 struct tog_tree_view_state *s = &view->state.tree;
9442 err = blame_tree_entry(new_view, y, x,
9443 s->selected_entry, &s->parents, s->commit_id,
9446 return got_error_msg(GOT_ERR_NOT_IMPL,
9447 "parent/child view pair not supported");
9450 if (view->type == TOG_VIEW_BLAME)
9451 err = log_annotated_line(new_view, y, x,
9452 view->state.blame.repo, view->state.blame.id_to_log);
9453 else if (view->type == TOG_VIEW_TREE)
9454 err = log_selected_tree_entry(new_view, y, x,
9456 else if (view->type == TOG_VIEW_REF)
9457 err = log_ref_entry(new_view, y, x,
9458 view->state.ref.selected_entry,
9459 view->state.ref.repo);
9461 return got_error_msg(GOT_ERR_NOT_IMPL,
9462 "parent/child view pair not supported");
9465 if (view->type == TOG_VIEW_LOG)
9466 err = browse_commit_tree(new_view, y, x,
9467 view->state.log.selected_entry,
9468 view->state.log.in_repo_path,
9469 view->state.log.head_ref_name,
9470 view->state.log.repo);
9471 else if (view->type == TOG_VIEW_REF)
9472 err = browse_ref_tree(new_view, y, x,
9473 view->state.ref.selected_entry,
9474 view->state.ref.repo);
9476 return got_error_msg(GOT_ERR_NOT_IMPL,
9477 "parent/child view pair not supported");
9480 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9481 if (*new_view == NULL)
9482 return got_error_from_errno("view_open");
9483 if (view->type == TOG_VIEW_LOG)
9484 err = open_ref_view(*new_view, view->state.log.repo);
9485 else if (view->type == TOG_VIEW_TREE)
9486 err = open_ref_view(*new_view, view->state.tree.repo);
9488 err = got_error_msg(GOT_ERR_NOT_IMPL,
9489 "parent/child view pair not supported");
9491 view_close(*new_view);
9494 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9495 if (*new_view == NULL)
9496 return got_error_from_errno("view_open");
9497 err = open_help_view(*new_view, view);
9499 view_close(*new_view);
9502 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9509 * If view was scrolled down to move the selected line into view when opening a
9510 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9513 offset_selection_up(struct tog_view *view)
9515 switch (view->type) {
9516 case TOG_VIEW_BLAME: {
9517 struct tog_blame_view_state *s = &view->state.blame;
9518 if (s->first_displayed_line == 1) {
9519 s->selected_line = MAX(s->selected_line - view->offset,
9523 if (s->first_displayed_line > view->offset)
9524 s->first_displayed_line -= view->offset;
9526 s->first_displayed_line = 1;
9527 s->selected_line += view->offset;
9531 log_scroll_up(&view->state.log, view->offset);
9532 view->state.log.selected += view->offset;
9535 ref_scroll_up(&view->state.ref, view->offset);
9536 view->state.ref.selected += view->offset;
9539 tree_scroll_up(&view->state.tree, view->offset);
9540 view->state.tree.selected += view->offset;
9550 * If the selected line is in the section of screen covered by the bottom split,
9551 * scroll down offset lines to move it into view and index its new position.
9553 static const struct got_error *
9554 offset_selection_down(struct tog_view *view)
9556 const struct got_error *err = NULL;
9557 const struct got_error *(*scrolld)(struct tog_view *, int);
9558 int *selected = NULL;
9561 switch (view->type) {
9562 case TOG_VIEW_BLAME: {
9563 struct tog_blame_view_state *s = &view->state.blame;
9566 if (s->selected_line > view->nlines - header) {
9567 offset = abs(view->nlines - s->selected_line - header);
9568 s->first_displayed_line += offset;
9569 s->selected_line -= offset;
9570 view->offset = offset;
9574 case TOG_VIEW_LOG: {
9575 struct tog_log_view_state *s = &view->state.log;
9576 scrolld = &log_scroll_down;
9577 header = view_is_parent_view(view) ? 3 : 2;
9578 selected = &s->selected;
9581 case TOG_VIEW_REF: {
9582 struct tog_ref_view_state *s = &view->state.ref;
9583 scrolld = &ref_scroll_down;
9585 selected = &s->selected;
9588 case TOG_VIEW_TREE: {
9589 struct tog_tree_view_state *s = &view->state.tree;
9590 scrolld = &tree_scroll_down;
9592 selected = &s->selected;
9602 if (selected && *selected > view->nlines - header) {
9603 offset = abs(view->nlines - *selected - header);
9604 view->offset = offset;
9605 if (scrolld && offset) {
9606 err = scrolld(view, offset);
9607 *selected -= offset;
9615 list_commands(FILE *fp)
9619 fprintf(fp, "commands:");
9620 for (i = 0; i < nitems(tog_commands); i++) {
9621 const struct tog_cmd *cmd = &tog_commands[i];
9622 fprintf(fp, " %s", cmd->name);
9628 usage(int hflag, int status)
9630 FILE *fp = (status == 0) ? stdout : stderr;
9632 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9635 fprintf(fp, "lazy usage: %s path\n", getprogname());
9642 make_argv(int argc, ...)
9650 argv = calloc(argc, sizeof(char *));
9653 for (i = 0; i < argc; i++) {
9654 argv[i] = strdup(va_arg(ap, char *));
9655 if (argv[i] == NULL)
9664 * Try to convert 'tog path' into a 'tog log path' command.
9665 * The user could simply have mistyped the command rather than knowingly
9666 * provided a path. So check whether argv[0] can in fact be resolved
9667 * to a path in the HEAD commit and print a special error if not.
9668 * This hack is for mpi@ <3
9670 static const struct got_error *
9671 tog_log_with_path(int argc, char *argv[])
9673 const struct got_error *error = NULL, *close_err;
9674 const struct tog_cmd *cmd = NULL;
9675 struct got_repository *repo = NULL;
9676 struct got_worktree *worktree = NULL;
9677 struct got_object_id *commit_id = NULL, *id = NULL;
9678 struct got_commit_object *commit = NULL;
9679 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9680 char *commit_id_str = NULL, **cmd_argv = NULL;
9681 int *pack_fds = NULL;
9683 cwd = getcwd(NULL, 0);
9685 return got_error_from_errno("getcwd");
9687 error = got_repo_pack_fds_open(&pack_fds);
9691 error = got_worktree_open(&worktree, cwd);
9692 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9696 repo_path = strdup(got_worktree_get_repo_path(worktree));
9698 repo_path = strdup(cwd);
9699 if (repo_path == NULL) {
9700 error = got_error_from_errno("strdup");
9704 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9708 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9713 error = tog_load_refs(repo, 0);
9716 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9717 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9718 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9723 got_worktree_close(worktree);
9727 error = got_object_open_as_commit(&commit, repo, commit_id);
9731 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9733 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9735 fprintf(stderr, "%s: '%s' is no known command or path\n",
9736 getprogname(), argv[0]);
9741 error = got_object_id_str(&commit_id_str, commit_id);
9745 cmd = &tog_commands[0]; /* log */
9747 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9748 error = cmd->cmd_main(argc, cmd_argv);
9751 close_err = got_repo_close(repo);
9756 got_object_commit_close(commit);
9758 got_worktree_close(worktree);
9760 const struct got_error *pack_err =
9761 got_repo_pack_fds_close(pack_fds);
9766 free(commit_id_str);
9773 for (i = 0; i < argc; i++)
9782 main(int argc, char *argv[])
9784 const struct got_error *io_err, *error = NULL;
9785 const struct tog_cmd *cmd = NULL;
9786 int ch, hflag = 0, Vflag = 0;
9787 char **cmd_argv = NULL;
9788 static const struct option longopts[] = {
9789 { "version", no_argument, NULL, 'V' },
9792 char *diff_algo_str = NULL;
9793 const char *test_script_path;
9795 setlocale(LC_CTYPE, "");
9798 * Test mode init must happen before pledge() because "tty" will
9799 * not allow TTY-related ioctls to occur via regular files.
9801 test_script_path = getenv("TOG_TEST_SCRIPT");
9802 if (test_script_path != NULL) {
9803 error = init_mock_term(test_script_path);
9805 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9808 } else if (!isatty(STDIN_FILENO))
9809 errx(1, "standard input is not a tty");
9811 #if !defined(PROFILE)
9812 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9817 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9837 got_version_print_str();
9844 /* Build an argument vector which runs a default command. */
9845 cmd = &tog_commands[0];
9847 cmd_argv = make_argv(argc, cmd->name);
9851 /* Did the user specify a command? */
9852 for (i = 0; i < nitems(tog_commands); i++) {
9853 if (strncmp(tog_commands[i].name, argv[0],
9854 strlen(argv[0])) == 0) {
9855 cmd = &tog_commands[i];
9861 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9862 if (diff_algo_str) {
9863 if (strcasecmp(diff_algo_str, "patience") == 0)
9864 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9865 if (strcasecmp(diff_algo_str, "myers") == 0)
9866 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9872 /* No command specified; try log with a path */
9873 error = tog_log_with_path(argc, argv);
9878 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9881 if (using_mock_io) {
9882 io_err = tog_io_close();
9889 for (i = 0; i < argc; i++)
9894 if (error && error->code != GOT_ERR_CANCELLED &&
9895 error->code != GOT_ERR_EOF &&
9896 error->code != GOT_ERR_PRIVSEP_EXIT &&
9897 error->code != GOT_ERR_PRIVSEP_PIPE &&
9898 !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9899 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);