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;
440 struct tog_blame_line *lines;
444 struct tog_blame_thread_args thread_args;
445 struct tog_blame_cb_args cb_args;
450 struct tog_blame_view_state {
451 int first_displayed_line;
452 int last_displayed_line;
454 int last_diffed_line;
458 struct got_object_id_queue blamed_commits;
459 struct got_object_qid *blamed_commit;
461 struct got_repository *repo;
462 struct got_object_id *commit_id;
463 struct got_object_id *id_to_log;
464 struct tog_blame blame;
466 struct tog_colors colors;
469 struct tog_parent_tree {
470 TAILQ_ENTRY(tog_parent_tree) entry;
471 struct got_tree_object *tree;
472 struct got_tree_entry *first_displayed_entry;
473 struct got_tree_entry *selected_entry;
477 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
479 struct tog_tree_view_state {
481 struct got_object_id *commit_id;/* commit which this tree belongs to */
482 struct got_tree_object *root; /* the commit's root tree entry */
483 struct got_tree_object *tree; /* currently displayed (sub-)tree */
484 struct got_tree_entry *first_displayed_entry;
485 struct got_tree_entry *last_displayed_entry;
486 struct got_tree_entry *selected_entry;
487 int ndisplayed, selected, show_ids;
488 struct tog_parent_trees parents; /* parent trees of current sub-tree */
490 struct got_repository *repo;
491 struct got_tree_entry *matched_entry;
492 struct tog_colors colors;
495 struct tog_reflist_entry {
496 TAILQ_ENTRY(tog_reflist_entry) entry;
497 struct got_reference *ref;
501 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
503 struct tog_ref_view_state {
504 struct tog_reflist_head refs;
505 struct tog_reflist_entry *first_displayed_entry;
506 struct tog_reflist_entry *last_displayed_entry;
507 struct tog_reflist_entry *selected_entry;
508 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
509 struct got_repository *repo;
510 struct tog_reflist_entry *matched_entry;
511 struct tog_colors colors;
514 struct tog_help_view_state {
519 int first_displayed_line;
520 int last_displayed_line;
525 enum tog_keymap_type type;
528 #define GENERATE_HELP \
529 KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
530 KEY_("H F1", "Open view-specific help (double tap for all help)"), \
531 KEY_("k C-p Up", "Move cursor or page up one line"), \
532 KEY_("j C-n Down", "Move cursor or page down one line"), \
533 KEY_("C-b b PgUp", "Scroll the view up one page"), \
534 KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
535 KEY_("C-u u", "Scroll the view up one half page"), \
536 KEY_("C-d d", "Scroll the view down one half page"), \
537 KEY_("g", "Go to line N (default: first line)"), \
538 KEY_("Home =", "Go to the first line"), \
539 KEY_("G", "Go to line N (default: last line)"), \
540 KEY_("End *", "Go to the last line"), \
541 KEY_("l Right", "Scroll the view right"), \
542 KEY_("h Left", "Scroll the view left"), \
543 KEY_("$", "Scroll view to the rightmost position"), \
544 KEY_("0", "Scroll view to the leftmost position"), \
545 KEY_("-", "Decrease size of the focussed split"), \
546 KEY_("+", "Increase size of the focussed split"), \
547 KEY_("Tab", "Switch focus between views"), \
548 KEY_("F", "Toggle fullscreen mode"), \
549 KEY_("S", "Switch split-screen layout"), \
550 KEY_("/", "Open prompt to enter search term"), \
551 KEY_("n", "Find next line/token matching the current search term"), \
552 KEY_("N", "Find previous line/token matching the current search term"),\
553 KEY_("q", "Quit the focussed view; Quit help screen"), \
554 KEY_("Q", "Quit tog"), \
556 KEYMAP_("Log view", TOG_KEYMAP_LOG), \
557 KEY_("< ,", "Move cursor up one commit"), \
558 KEY_("> .", "Move cursor down one commit"), \
559 KEY_("Enter", "Open diff view of the selected commit"), \
560 KEY_("B", "Reload the log view and toggle display of merged commits"), \
561 KEY_("R", "Open ref view of all repository references"), \
562 KEY_("T", "Display tree view of the repository from the selected" \
564 KEY_("@", "Toggle between displaying author and committer name"), \
565 KEY_("&", "Open prompt to enter term to limit commits displayed"), \
566 KEY_("C-g Backspace", "Cancel current search or log operation"), \
567 KEY_("C-l", "Reload the log view with new commits in the repository"), \
569 KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
570 KEY_("K < ,", "Display diff of next line in the file/log entry"), \
571 KEY_("J > .", "Display diff of previous line in the file/log entry"), \
572 KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
573 KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
575 KEY_("(", "Go to the previous file in the diff"), \
576 KEY_(")", "Go to the next file in the diff"), \
577 KEY_("{", "Go to the previous hunk in the diff"), \
578 KEY_("}", "Go to the next hunk in the diff"), \
579 KEY_("[", "Decrease the number of context lines"), \
580 KEY_("]", "Increase the number of context lines"), \
581 KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
583 KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
584 KEY_("Enter", "Display diff view of the selected line's commit"), \
585 KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
586 KEY_("L", "Open log view for the currently selected annotated line"), \
587 KEY_("C", "Reload view with the previously blamed commit"), \
588 KEY_("c", "Reload view with the version of the file found in the" \
589 " selected line's commit"), \
590 KEY_("p", "Reload view with the version of the file found in the" \
591 " selected line's parent commit"), \
593 KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
594 KEY_("Enter", "Enter selected directory or open blame view of the" \
596 KEY_("L", "Open log view for the selected entry"), \
597 KEY_("R", "Open ref view of all repository references"), \
598 KEY_("i", "Show object IDs for all tree entries"), \
599 KEY_("Backspace", "Return to the parent directory"), \
601 KEYMAP_("Ref view", TOG_KEYMAP_REF), \
602 KEY_("Enter", "Display log view of the selected reference"), \
603 KEY_("T", "Display tree view of the selected reference"), \
604 KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
605 KEY_("m", "Toggle display of last modified date for each reference"), \
606 KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
607 KEY_("C-l", "Reload view with all repository references")
612 enum tog_keymap_type type;
615 /* curses io for tog regress */
621 static int using_mock_io;
623 #define TOG_SCREEN_DUMP "SCREENDUMP"
624 #define TOG_SCREEN_DUMP_LEN (sizeof(TOG_SCREEN_DUMP) - 1)
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, got_locale_is_utf8() ?
985 ACS_HLINE : '-', view->ncols);
987 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
988 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
991 static const struct got_error *view_init_hsplit(struct tog_view *, int);
992 static const struct got_error *request_log_commits(struct tog_view *);
993 static const struct got_error *offset_selection_down(struct tog_view *);
994 static void offset_selection_up(struct tog_view *);
995 static void view_get_split(struct tog_view *, int *, int *);
997 static const struct got_error *
998 view_resize(struct tog_view *view)
1000 const struct got_error *err = NULL;
1001 int dif, nlines, ncols;
1003 dif = LINES - view->lines; /* line difference */
1005 if (view->lines > LINES)
1006 nlines = view->nlines - (view->lines - LINES);
1008 nlines = view->nlines + (LINES - view->lines);
1009 if (view->cols > COLS)
1010 ncols = view->ncols - (view->cols - COLS);
1012 ncols = view->ncols + (COLS - view->cols);
1015 int hs = view->child->begin_y;
1017 if (!view_is_fullscreen(view))
1018 view->child->begin_x = view_split_begin_x(view->begin_x);
1019 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1020 view->child->begin_x == 0) {
1023 view_fullscreen(view->child);
1024 if (view->child->focussed)
1025 show_panel(view->child->panel);
1027 show_panel(view->panel);
1029 ncols = view->child->begin_x;
1031 view_splitscreen(view->child);
1032 show_panel(view->child->panel);
1035 * XXX This is ugly and needs to be moved into the above
1036 * logic but "works" for now and my attempts at moving it
1037 * break either 'tab' or 'F' key maps in horizontal splits.
1040 err = view_splitscreen(view->child);
1043 if (dif < 0) { /* top split decreased */
1044 err = offset_selection_down(view);
1051 show_panel(view->child->panel);
1052 nlines = view->nlines;
1054 } else if (view->parent == NULL)
1057 if (view->resize && dif > 0) {
1058 err = view->resize(view, dif);
1063 if (wresize(view->window, nlines, ncols) == ERR)
1064 return got_error_from_errno("wresize");
1065 if (replace_panel(view->panel, view->window) == ERR)
1066 return got_error_from_errno("replace_panel");
1067 wclear(view->window);
1069 view->nlines = nlines;
1070 view->ncols = ncols;
1071 view->lines = LINES;
1077 static const struct got_error *
1078 resize_log_view(struct tog_view *view, int increase)
1080 struct tog_log_view_state *s = &view->state.log;
1081 const struct got_error *err = NULL;
1084 if (s->selected_entry)
1085 n = s->selected_entry->idx + view->lines - s->selected;
1088 * Request commits to account for the increased
1089 * height so we have enough to populate the view.
1091 if (s->commits->ncommits < n) {
1092 view->nscrolled = n - s->commits->ncommits + increase + 1;
1093 err = request_log_commits(view);
1100 view_adjust_offset(struct tog_view *view, int n)
1105 if (view->parent && view->parent->offset) {
1106 if (view->parent->offset + n >= 0)
1107 view->parent->offset += n;
1109 view->parent->offset = 0;
1110 } else if (view->offset) {
1111 if (view->offset - n >= 0)
1118 static const struct got_error *
1119 view_resize_split(struct tog_view *view, int resize)
1121 const struct got_error *err = NULL;
1122 struct tog_view *v = NULL;
1129 if (!v->child || !view_is_splitscreen(v->child))
1132 v->resized = v->child->resized = resize; /* lock for resize event */
1134 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1135 if (v->child->resized_y)
1136 v->child->begin_y = v->child->resized_y;
1138 v->child->begin_y -= resize;
1140 v->child->begin_y += resize;
1141 if (v->child->begin_y < 3) {
1143 v->child->begin_y = 3;
1144 } else if (v->child->begin_y > LINES - 1) {
1146 v->child->begin_y = LINES - 1;
1149 v->child->ncols = COLS;
1150 view_adjust_offset(view, resize);
1151 err = view_init_hsplit(v, v->child->begin_y);
1154 v->child->resized_y = v->child->begin_y;
1156 if (v->child->resized_x)
1157 v->child->begin_x = v->child->resized_x;
1159 v->child->begin_x -= resize;
1161 v->child->begin_x += resize;
1162 if (v->child->begin_x < 11) {
1164 v->child->begin_x = 11;
1165 } else if (v->child->begin_x > COLS - 1) {
1167 v->child->begin_x = COLS - 1;
1169 v->child->resized_x = v->child->begin_x;
1172 v->child->mode = v->mode;
1173 v->child->nlines = v->lines - v->child->begin_y;
1174 v->child->ncols = v->cols - v->child->begin_x;
1177 err = view_fullscreen(v);
1180 err = view_splitscreen(v->child);
1184 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1185 err = offset_selection_down(v->child);
1191 err = v->resize(v, 0);
1192 else if (v->child->resize)
1193 err = v->child->resize(v->child, 0);
1195 v->resized = v->child->resized = 0;
1201 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1203 struct tog_view *v = src->child ? src->child : src;
1205 dst->resized_x = v->resized_x;
1206 dst->resized_y = v->resized_y;
1209 static const struct got_error *
1210 view_close_child(struct tog_view *view)
1212 const struct got_error *err = NULL;
1214 if (view->child == NULL)
1217 err = view_close(view->child);
1222 static const struct got_error *
1223 view_set_child(struct tog_view *view, struct tog_view *child)
1225 const struct got_error *err = NULL;
1227 view->child = child;
1228 child->parent = view;
1230 err = view_resize(view);
1234 if (view->child->resized_x || view->child->resized_y)
1235 err = view_resize_split(view, 0);
1240 static const struct got_error *view_dispatch_request(struct tog_view **,
1241 struct tog_view *, enum tog_view_type, int, int);
1243 static const struct got_error *
1244 view_request_new(struct tog_view **requested, struct tog_view *view,
1245 enum tog_view_type request)
1247 struct tog_view *new_view = NULL;
1248 const struct got_error *err;
1253 if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1254 view_get_split(view, &y, &x);
1256 err = view_dispatch_request(&new_view, view, request, y, x);
1260 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1261 request != TOG_VIEW_HELP) {
1262 err = view_init_hsplit(view, y);
1268 new_view->focussed = 1;
1269 new_view->mode = view->mode;
1270 new_view->nlines = request == TOG_VIEW_HELP ?
1271 view->lines : view->lines - y;
1273 if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1274 view_transfer_size(new_view, view);
1275 err = view_close_child(view);
1278 err = view_set_child(view, new_view);
1281 view->focus_child = 1;
1283 *requested = new_view;
1289 tog_resizeterm(void)
1292 struct winsize size;
1294 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1295 cols = 80; /* Default */
1299 lines = size.ws_row;
1301 resize_term(lines, cols);
1304 static const struct got_error *
1305 view_search_start(struct tog_view *view, int fast_refresh)
1307 const struct got_error *err = NULL;
1308 struct tog_view *v = view;
1312 if (view->search_started) {
1313 regfree(&view->regex);
1314 view->searching = 0;
1315 memset(&view->regmatch, 0, sizeof(view->regmatch));
1317 view->search_started = 0;
1319 if (view->nlines < 1)
1322 if (view_is_hsplit_top(view))
1324 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1327 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1328 wclrtoeol(v->window);
1330 nodelay(v->window, FALSE); /* block for search term input */
1333 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1334 wrefresh(v->window);
1337 nodelay(v->window, TRUE);
1338 if (!fast_refresh && !using_mock_io)
1343 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1344 err = view->search_start(view);
1346 regfree(&view->regex);
1349 view->search_started = 1;
1350 view->searching = TOG_SEARCH_FORWARD;
1351 view->search_next_done = 0;
1352 view->search_next(view);
1358 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1359 static const struct got_error *
1360 switch_split(struct tog_view *view)
1362 const struct got_error *err = NULL;
1363 struct tog_view *v = NULL;
1370 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1371 v->mode = TOG_VIEW_SPLIT_VERT;
1373 v->mode = TOG_VIEW_SPLIT_HRZN;
1377 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1378 v->mode = TOG_VIEW_SPLIT_NONE;
1380 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1381 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1382 v->child->begin_y = v->child->resized_y;
1383 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1384 v->child->begin_x = v->child->resized_x;
1387 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1389 v->child->ncols = COLS;
1390 v->child->nscrolled = LINES - v->child->nlines;
1392 err = view_init_hsplit(v, v->child->begin_y);
1396 v->child->mode = v->mode;
1397 v->child->nlines = v->lines - v->child->begin_y;
1400 err = view_fullscreen(v);
1403 err = view_splitscreen(v->child);
1407 if (v->mode == TOG_VIEW_SPLIT_NONE)
1408 v->mode = TOG_VIEW_SPLIT_VERT;
1409 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1410 err = offset_selection_down(v);
1413 err = offset_selection_down(v->child);
1417 offset_selection_up(v);
1418 offset_selection_up(v->child);
1421 err = v->resize(v, 0);
1422 else if (v->child->resize)
1423 err = v->child->resize(v->child, 0);
1429 * Strip trailing whitespace from str starting at byte *n;
1430 * if *n < 0, use strlen(str). Return new str length in *n.
1433 strip_trailing_ws(char *str, int *n)
1437 if (str == NULL || *str == '\0')
1443 while (x-- > 0 && isspace((unsigned char)str[x]))
1450 * Extract visible substring of line y from the curses screen
1451 * and strip trailing whitespace. If vline is set and locale is
1452 * UTF-8, overwrite line[vline] with '|' because the ACS_VLINE
1453 * character is written out as 'x'. Write the line to file f.
1455 static const struct got_error *
1456 view_write_line(FILE *f, int y, int vline)
1458 char line[COLS * MB_LEN_MAX]; /* allow for multibyte chars */
1461 r = mvwinnstr(curscr, y, 0, line, sizeof(line));
1463 return got_error_fmt(GOT_ERR_RANGE,
1464 "failed to extract line %d", y);
1467 * In some views, lines are padded with blanks to COLS width.
1468 * Strip them so we can diff without the -b flag when testing.
1470 strip_trailing_ws(line, &r);
1472 if (vline > 0 && got_locale_is_utf8())
1475 w = fprintf(f, "%s\n", line);
1476 if (w != r + 1) /* \n */
1477 return got_ferror(f, GOT_ERR_IO);
1483 * Capture the visible curses screen by writing each line to the
1484 * file at the path set via the TOG_SCR_DUMP environment variable.
1486 static const struct got_error *
1487 screendump(struct tog_view *view)
1489 const struct got_error *err;
1494 path = getenv("TOG_SCR_DUMP");
1495 if (path == NULL || *path == '\0')
1496 return got_error_msg(GOT_ERR_BAD_PATH,
1497 "TOG_SCR_DUMP path not set to capture screen dump");
1498 f = fopen(path, "wex");
1500 return got_error_from_errno_fmt("fopen: %s", path);
1502 if ((view->child && view->child->begin_x) ||
1503 (view->parent && view->begin_x)) {
1504 int ncols = view->child ? view->ncols : view->parent->ncols;
1506 /* vertical splitscreen */
1507 for (i = 0; i < view->nlines; ++i) {
1508 err = view_write_line(f, i, ncols - 1);
1515 /* fullscreen or horizontal splitscreen */
1516 if ((view->child && view->child->begin_y) ||
1517 (view->parent && view->begin_y)) /* hsplit */
1518 hline = view->child ?
1519 view->child->begin_y : view->begin_y;
1521 for (i = 0; i < view->lines; i++) {
1522 if (hline && got_locale_is_utf8() && i == hline - 1) {
1525 /* ACS_HLINE writes out as 'q', overwrite it */
1526 for (c = 0; c < view->cols; ++c)
1532 err = view_write_line(f, i, 0);
1539 if (f && fclose(f) == EOF && err == NULL)
1540 err = got_ferror(f, GOT_ERR_IO);
1545 * Compute view->count from numeric input. Assign total to view->count and
1546 * return first non-numeric key entered.
1549 get_compound_key(struct tog_view *view, int c)
1551 struct tog_view *v = view;
1554 if (view_is_hsplit_top(view))
1556 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1560 cbreak(); /* block for input */
1561 nodelay(view->window, FALSE);
1562 wmove(v->window, v->nlines - 1, 0);
1563 wclrtoeol(v->window);
1564 waddch(v->window, ':');
1567 x = getcurx(v->window);
1568 if (x != ERR && x < view->ncols) {
1569 waddch(v->window, c);
1570 wrefresh(v->window);
1574 * Don't overflow. Max valid request should be the greatest
1575 * between the longest and total lines; cap at 10 million.
1580 n = n * 10 + (c - '0');
1581 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1583 if (c == 'G' || c == 'g') { /* nG key map */
1584 view->gline = view->hiline = n;
1589 /* Massage excessive or inapplicable values at the input handler. */
1596 action_report(struct tog_view *view)
1598 struct tog_view *v = view;
1600 if (view_is_hsplit_top(view))
1602 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1605 wmove(v->window, v->nlines - 1, 0);
1606 wclrtoeol(v->window);
1607 wprintw(v->window, ":%s", view->action);
1608 wrefresh(v->window);
1611 * Clear action status report. Only clear in blame view
1612 * once annotating is complete, otherwise it's too fast.
1614 if (view->type == TOG_VIEW_BLAME) {
1615 if (view->state.blame.blame_complete)
1616 view->action = NULL;
1618 view->action = NULL;
1622 * Read the next line from the test script and assign
1623 * key instruction to *ch. If at EOF, set the *done flag.
1625 static const struct got_error *
1626 tog_read_script_key(FILE *script, int *ch, int *done)
1628 const struct got_error *err = NULL;
1632 if (getline(&line, &linesz, script) == -1) {
1637 err = got_ferror(script, GOT_ERR_IO);
1640 } else if (strncasecmp(line, "KEY_ENTER", 9) == 0)
1642 else if (strncasecmp(line, "KEY_RIGHT", 9) == 0)
1644 else if (strncasecmp(line, "KEY_LEFT", 8) == 0)
1646 else if (strncasecmp(line, "KEY_DOWN", 8) == 0)
1648 else if (strncasecmp(line, "KEY_UP", 6) == 0)
1650 else if (strncasecmp(line, TOG_SCREEN_DUMP, TOG_SCREEN_DUMP_LEN) == 0)
1651 *ch = TOG_KEY_SCRDUMP;
1660 static const struct got_error *
1661 view_input(struct tog_view **new, int *done, struct tog_view *view,
1662 struct tog_view_list_head *views, int fast_refresh)
1664 const struct got_error *err = NULL;
1671 action_report(view);
1673 /* Clear "no matches" indicator. */
1674 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1675 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1676 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1680 if (view->searching && !view->search_next_done) {
1681 errcode = pthread_mutex_unlock(&tog_mutex);
1683 return got_error_set_errno(errcode,
1684 "pthread_mutex_unlock");
1686 errcode = pthread_mutex_lock(&tog_mutex);
1688 return got_error_set_errno(errcode,
1689 "pthread_mutex_lock");
1690 view->search_next(view);
1694 /* Allow threads to make progress while we are waiting for input. */
1695 errcode = pthread_mutex_unlock(&tog_mutex);
1697 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1699 if (using_mock_io) {
1700 err = tog_read_script_key(tog_io.f, &ch, done);
1703 } else if (view->count && --view->count) {
1705 nodelay(view->window, TRUE);
1706 ch = wgetch(view->window);
1707 /* let C-g or backspace abort unfinished count */
1708 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1713 ch = wgetch(view->window);
1714 if (ch >= '1' && ch <= '9')
1715 view->ch = ch = get_compound_key(view, ch);
1717 if (view->hiline && ch != ERR && ch != 0)
1718 view->hiline = 0; /* key pressed, clear line highlight */
1719 nodelay(view->window, TRUE);
1720 errcode = pthread_mutex_lock(&tog_mutex);
1722 return got_error_set_errno(errcode, "pthread_mutex_lock");
1724 if (tog_sigwinch_received || tog_sigcont_received) {
1726 tog_sigwinch_received = 0;
1727 tog_sigcont_received = 0;
1728 TAILQ_FOREACH(v, views, entry) {
1729 err = view_resize(v);
1732 err = v->input(new, v, KEY_RESIZE);
1736 err = view_resize(v->child);
1739 err = v->child->input(new, v->child,
1743 if (v->child->resized_x || v->child->resized_y) {
1744 err = view_resize_split(v, 0);
1756 if (view->type == TOG_VIEW_HELP)
1757 err = view->reset(view);
1759 err = view_request_new(new, view, TOG_VIEW_HELP);
1765 view->child->focussed = 1;
1766 view->focus_child = 1;
1767 } else if (view->parent) {
1769 view->parent->focussed = 1;
1770 view->parent->focus_child = 0;
1771 if (!view_is_splitscreen(view)) {
1772 if (view->parent->resize) {
1773 err = view->parent->resize(view->parent,
1778 offset_selection_up(view->parent);
1779 err = view_fullscreen(view->parent);
1786 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1787 if (view->parent->resize) {
1788 /* might need more commits to fill fullscreen */
1789 err = view->parent->resize(view->parent, 0);
1793 offset_selection_up(view->parent);
1795 err = view->input(new, view, ch);
1803 if (view_is_parent_view(view)) {
1804 if (view->child == NULL)
1806 if (view_is_splitscreen(view->child)) {
1808 view->child->focussed = 1;
1809 err = view_fullscreen(view->child);
1811 err = view_splitscreen(view->child);
1813 err = view_resize_split(view, 0);
1817 err = view->child->input(new, view->child,
1820 if (view_is_splitscreen(view)) {
1821 view->parent->focussed = 0;
1823 err = view_fullscreen(view);
1825 err = view_splitscreen(view);
1826 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1827 err = view_resize(view->parent);
1829 err = view_resize_split(view, 0);
1833 err = view->input(new, view, KEY_RESIZE);
1838 err = view->resize(view, 0);
1843 err = offset_selection_down(view->parent);
1845 err = offset_selection_down(view);
1849 err = switch_split(view);
1852 err = view_resize_split(view, -1);
1855 err = view_resize_split(view, 1);
1861 if (view->search_start)
1862 view_search_start(view, fast_refresh);
1864 err = view->input(new, view, ch);
1868 if (view->search_started && view->search_next) {
1869 view->searching = (ch == 'n' ?
1870 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1871 view->search_next_done = 0;
1872 view->search_next(view);
1874 err = view->input(new, view, ch);
1877 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1878 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1879 view->action = "Patience diff algorithm";
1881 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1882 view->action = "Myers diff algorithm";
1884 TAILQ_FOREACH(v, views, entry) {
1890 if (v->child && v->child->reset) {
1891 err = v->child->reset(v->child);
1897 case TOG_KEY_SCRDUMP:
1898 err = screendump(view);
1901 err = view->input(new, view, ch);
1909 view_needs_focus_indication(struct tog_view *view)
1911 if (view_is_parent_view(view)) {
1912 if (view->child == NULL || view->child->focussed)
1914 if (!view_is_splitscreen(view->child))
1916 } else if (!view_is_splitscreen(view))
1919 return view->focussed;
1922 static const struct got_error *
1925 const struct got_error *err = NULL;
1927 if (tog_io.cin && fclose(tog_io.cin) == EOF)
1928 err = got_ferror(tog_io.cin, GOT_ERR_IO);
1929 if (tog_io.cout && fclose(tog_io.cout) == EOF && err == NULL)
1930 err = got_ferror(tog_io.cout, GOT_ERR_IO);
1931 if (tog_io.f && fclose(tog_io.f) == EOF && err == NULL)
1932 err = got_ferror(tog_io.f, GOT_ERR_IO);
1937 static const struct got_error *
1938 view_loop(struct tog_view *view)
1940 const struct got_error *err = NULL;
1941 struct tog_view_list_head views;
1942 struct tog_view *new_view;
1944 int fast_refresh = 10;
1945 int done = 0, errcode;
1947 mode = getenv("TOG_VIEW_SPLIT_MODE");
1948 if (!mode || !(*mode == 'h' || *mode == 'H'))
1949 view->mode = TOG_VIEW_SPLIT_VERT;
1951 view->mode = TOG_VIEW_SPLIT_HRZN;
1953 errcode = pthread_mutex_lock(&tog_mutex);
1955 return got_error_set_errno(errcode, "pthread_mutex_lock");
1958 TAILQ_INSERT_HEAD(&views, view, entry);
1961 err = view->show(view);
1966 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1967 !tog_fatal_signal_received()) {
1968 /* Refresh fast during initialization, then become slower. */
1969 if (fast_refresh && --fast_refresh == 0 && !using_mock_io)
1970 halfdelay(10); /* switch to once per second */
1972 err = view_input(&new_view, &done, view, &views, fast_refresh);
1976 if (view->dying && view == TAILQ_FIRST(&views) &&
1977 TAILQ_NEXT(view, entry) == NULL)
1983 * When we quit, scroll the screen up a single line
1984 * so we don't lose any information.
1986 TAILQ_FOREACH(v, &views, entry) {
1987 wmove(v->window, 0, 0);
1988 wdeleteln(v->window);
1989 wnoutrefresh(v->window);
1990 if (v->child && !view_is_fullscreen(v)) {
1991 wmove(v->child->window, 0, 0);
1992 wdeleteln(v->child->window);
1993 wnoutrefresh(v->child->window);
2000 struct tog_view *v, *prev = NULL;
2002 if (view_is_parent_view(view))
2003 prev = TAILQ_PREV(view, tog_view_list_head,
2005 else if (view->parent)
2006 prev = view->parent;
2009 view->parent->child = NULL;
2010 view->parent->focus_child = 0;
2011 /* Restore fullscreen line height. */
2012 view->parent->nlines = view->parent->lines;
2013 err = view_resize(view->parent);
2016 /* Make resized splits persist. */
2017 view_transfer_size(view->parent, view);
2019 TAILQ_REMOVE(&views, view, entry);
2021 err = view_close(view);
2026 TAILQ_FOREACH(v, &views, entry) {
2030 if (view == NULL && new_view == NULL) {
2031 /* No view has focus. Try to pick one. */
2034 else if (!TAILQ_EMPTY(&views)) {
2035 view = TAILQ_LAST(&views,
2036 tog_view_list_head);
2039 if (view->focus_child) {
2040 view->child->focussed = 1;
2048 struct tog_view *v, *t;
2049 /* Only allow one parent view per type. */
2050 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
2051 if (v->type != new_view->type)
2053 TAILQ_REMOVE(&views, v, entry);
2054 err = view_close(v);
2059 TAILQ_INSERT_TAIL(&views, new_view, entry);
2062 if (view && !done) {
2063 if (view_is_parent_view(view)) {
2064 if (view->child && view->child->focussed)
2067 if (view->parent && view->parent->focussed)
2068 view = view->parent;
2070 show_panel(view->panel);
2071 if (view->child && view_is_splitscreen(view->child))
2072 show_panel(view->child->panel);
2073 if (view->parent && view_is_splitscreen(view)) {
2074 err = view->parent->show(view->parent);
2078 err = view->show(view);
2082 err = view->child->show(view->child);
2091 while (!TAILQ_EMPTY(&views)) {
2092 const struct got_error *close_err;
2093 view = TAILQ_FIRST(&views);
2094 TAILQ_REMOVE(&views, view, entry);
2095 close_err = view_close(view);
2096 if (close_err && err == NULL)
2100 errcode = pthread_mutex_unlock(&tog_mutex);
2101 if (errcode && err == NULL)
2102 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2112 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
2117 /* Create newly allocated wide-character string equivalent to a byte string. */
2118 static const struct got_error *
2119 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
2122 const struct got_error *err = NULL;
2125 *wlen = mbstowcs(NULL, s, 0);
2126 if (*wlen == (size_t)-1) {
2128 if (errno != EILSEQ)
2129 return got_error_from_errno("mbstowcs");
2131 /* byte string invalid in current encoding; try to "fix" it */
2132 err = got_mbsavis(&vis, &vislen, s);
2135 *wlen = mbstowcs(NULL, vis, 0);
2136 if (*wlen == (size_t)-1) {
2137 err = got_error_from_errno("mbstowcs"); /* give up */
2142 *ws = calloc(*wlen + 1, sizeof(**ws));
2144 err = got_error_from_errno("calloc");
2148 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
2149 err = got_error_from_errno("mbstowcs");
2160 static const struct got_error *
2161 expand_tab(char **ptr, const char *src)
2164 size_t len, n, idx = 0, sz = 0;
2167 n = len = strlen(src);
2168 dst = malloc(n + 1);
2170 return got_error_from_errno("malloc");
2172 while (idx < len && src[idx]) {
2173 const char c = src[idx];
2176 size_t nb = TABSIZE - sz % TABSIZE;
2179 p = realloc(dst, n + nb);
2182 return got_error_from_errno("realloc");
2187 memset(dst + sz, ' ', nb);
2190 dst[sz++] = src[idx];
2200 * Advance at most n columns from wline starting at offset off.
2201 * Return the index to the first character after the span operation.
2202 * Return the combined column width of all spanned wide character in
2206 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2208 int width, i, cols = 0;
2215 for (i = off; wline[i] != L'\0'; ++i) {
2216 if (wline[i] == L'\t')
2217 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2219 width = wcwidth(wline[i]);
2226 if (cols + width > n)
2236 * Format a line for display, ensuring that it won't overflow a width limit.
2237 * With scrolling, the width returned refers to the scrolled version of the
2238 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2240 static const struct got_error *
2241 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2242 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2244 const struct got_error *err = NULL;
2246 wchar_t *wline = NULL;
2255 err = expand_tab(&exstr, line);
2260 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2265 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2267 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2268 wline[wlen - 1] = L'\0';
2271 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2272 wline[wlen - 1] = L'\0';
2276 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2282 *scrollxp = scrollx;
2290 static const struct got_error*
2291 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2292 struct got_object_id *id, struct got_repository *repo)
2294 static const struct got_error *err = NULL;
2295 struct got_reflist_entry *re;
2301 TAILQ_FOREACH(re, refs, entry) {
2302 struct got_tag_object *tag = NULL;
2303 struct got_object_id *ref_id;
2306 name = got_ref_get_name(re->ref);
2307 if (strcmp(name, GOT_REF_HEAD) == 0)
2309 if (strncmp(name, "refs/", 5) == 0)
2311 if (strncmp(name, "got/", 4) == 0 &&
2312 strncmp(name, "got/backup/", 11) != 0)
2314 if (strncmp(name, "heads/", 6) == 0)
2316 if (strncmp(name, "remotes/", 8) == 0) {
2318 s = strstr(name, "/" GOT_REF_HEAD);
2319 if (s != NULL && s[strlen(s)] == '\0')
2322 err = got_ref_resolve(&ref_id, repo, re->ref);
2325 if (strncmp(name, "tags/", 5) == 0) {
2326 err = got_object_open_as_tag(&tag, repo, ref_id);
2328 if (err->code != GOT_ERR_OBJ_TYPE) {
2332 /* Ref points at something other than a tag. */
2337 cmp = got_object_id_cmp(tag ?
2338 got_object_tag_get_object_id(tag) : ref_id, id);
2341 got_object_tag_close(tag);
2345 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2346 s ? ", " : "", name) == -1) {
2347 err = got_error_from_errno("asprintf");
2358 static const struct got_error *
2359 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2364 smallerthan = strchr(author, '<');
2365 if (smallerthan && smallerthan[1] != '\0')
2366 author = smallerthan + 1;
2367 author[strcspn(author, "@>")] = '\0';
2368 return format_line(wauthor, author_width, NULL, author, 0, limit,
2372 static const struct got_error *
2373 draw_commit(struct tog_view *view, struct got_commit_object *commit,
2374 struct got_object_id *id, const size_t date_display_cols,
2375 int author_display_cols)
2377 struct tog_log_view_state *s = &view->state.log;
2378 const struct got_error *err = NULL;
2379 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2380 char *logmsg0 = NULL, *logmsg = NULL;
2381 char *author = NULL;
2382 wchar_t *wlogmsg = NULL, *wauthor = NULL;
2383 int author_width, logmsg_width;
2384 char *newline, *line = NULL;
2385 int col, limit, scrollx;
2386 const int avail = view->ncols;
2388 time_t committer_time;
2389 struct tog_color *tc;
2391 committer_time = got_object_commit_get_committer_time(commit);
2392 if (gmtime_r(&committer_time, &tm) == NULL)
2393 return got_error_from_errno("gmtime_r");
2394 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2395 return got_error(GOT_ERR_NO_SPACE);
2397 if (avail <= date_display_cols)
2398 limit = MIN(sizeof(datebuf) - 1, avail);
2400 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2401 tc = get_color(&s->colors, TOG_COLOR_DATE);
2403 wattr_on(view->window,
2404 COLOR_PAIR(tc->colorpair), NULL);
2405 waddnstr(view->window, datebuf, limit);
2407 wattr_off(view->window,
2408 COLOR_PAIR(tc->colorpair), NULL);
2415 err = got_object_id_str(&id_str, id);
2418 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2420 wattr_on(view->window,
2421 COLOR_PAIR(tc->colorpair), NULL);
2422 wprintw(view->window, "%.8s ", id_str);
2424 wattr_off(view->window,
2425 COLOR_PAIR(tc->colorpair), NULL);
2432 if (s->use_committer)
2433 author = strdup(got_object_commit_get_committer(commit));
2435 author = strdup(got_object_commit_get_author(commit));
2436 if (author == NULL) {
2437 err = got_error_from_errno("strdup");
2440 err = format_author(&wauthor, &author_width, author, avail - col, col);
2443 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2445 wattr_on(view->window,
2446 COLOR_PAIR(tc->colorpair), NULL);
2447 waddwstr(view->window, wauthor);
2448 col += author_width;
2449 while (col < avail && author_width < author_display_cols + 2) {
2450 waddch(view->window, ' ');
2455 wattr_off(view->window,
2456 COLOR_PAIR(tc->colorpair), NULL);
2460 err = got_object_commit_get_logmsg(&logmsg0, commit);
2464 while (*logmsg == '\n')
2466 newline = strchr(logmsg, '\n');
2469 limit = avail - col;
2470 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2471 limit--; /* for the border */
2472 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2476 waddwstr(view->window, &wlogmsg[scrollx]);
2477 col += MAX(logmsg_width, 0);
2478 while (col < avail) {
2479 waddch(view->window, ' ');
2491 static struct commit_queue_entry *
2492 alloc_commit_queue_entry(struct got_commit_object *commit,
2493 struct got_object_id *id)
2495 struct commit_queue_entry *entry;
2496 struct got_object_id *dup;
2498 entry = calloc(1, sizeof(*entry));
2502 dup = got_object_id_dup(id);
2509 entry->commit = commit;
2514 pop_commit(struct commit_queue *commits)
2516 struct commit_queue_entry *entry;
2518 entry = TAILQ_FIRST(&commits->head);
2519 TAILQ_REMOVE(&commits->head, entry, entry);
2520 got_object_commit_close(entry->commit);
2521 commits->ncommits--;
2527 free_commits(struct commit_queue *commits)
2529 while (!TAILQ_EMPTY(&commits->head))
2530 pop_commit(commits);
2533 static const struct got_error *
2534 match_commit(int *have_match, struct got_object_id *id,
2535 struct got_commit_object *commit, regex_t *regex)
2537 const struct got_error *err = NULL;
2538 regmatch_t regmatch;
2539 char *id_str = NULL, *logmsg = NULL;
2543 err = got_object_id_str(&id_str, id);
2547 err = got_object_commit_get_logmsg(&logmsg, commit);
2551 if (regexec(regex, got_object_commit_get_author(commit), 1,
2552 ®match, 0) == 0 ||
2553 regexec(regex, got_object_commit_get_committer(commit), 1,
2554 ®match, 0) == 0 ||
2555 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2556 regexec(regex, logmsg, 1, ®match, 0) == 0)
2564 static const struct got_error *
2565 queue_commits(struct tog_log_thread_args *a)
2567 const struct got_error *err = NULL;
2570 * We keep all commits open throughout the lifetime of the log
2571 * view in order to avoid having to re-fetch commits from disk
2572 * while updating the display.
2575 struct got_object_id id;
2576 struct got_commit_object *commit;
2577 struct commit_queue_entry *entry;
2578 int limit_match = 0;
2581 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2586 err = got_object_open_as_commit(&commit, a->repo, &id);
2589 entry = alloc_commit_queue_entry(commit, &id);
2590 if (entry == NULL) {
2591 err = got_error_from_errno("alloc_commit_queue_entry");
2595 errcode = pthread_mutex_lock(&tog_mutex);
2597 err = got_error_set_errno(errcode,
2598 "pthread_mutex_lock");
2602 entry->idx = a->real_commits->ncommits;
2603 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2604 a->real_commits->ncommits++;
2607 err = match_commit(&limit_match, &id, commit,
2613 struct commit_queue_entry *matched;
2615 matched = alloc_commit_queue_entry(
2616 entry->commit, entry->id);
2617 if (matched == NULL) {
2618 err = got_error_from_errno(
2619 "alloc_commit_queue_entry");
2622 matched->commit = entry->commit;
2623 got_object_commit_retain(entry->commit);
2625 matched->idx = a->limit_commits->ncommits;
2626 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2628 a->limit_commits->ncommits++;
2632 * This is how we signal log_thread() that we
2633 * have found a match, and that it should be
2634 * counted as a new entry for the view.
2636 a->limit_match = limit_match;
2639 if (*a->searching == TOG_SEARCH_FORWARD &&
2640 !*a->search_next_done) {
2642 err = match_commit(&have_match, &id, commit, a->regex);
2647 if (limit_match && have_match)
2648 *a->search_next_done =
2649 TOG_SEARCH_HAVE_MORE;
2650 } else if (have_match)
2651 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2654 errcode = pthread_mutex_unlock(&tog_mutex);
2655 if (errcode && err == NULL)
2656 err = got_error_set_errno(errcode,
2657 "pthread_mutex_unlock");
2660 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2666 select_commit(struct tog_log_view_state *s)
2668 struct commit_queue_entry *entry;
2671 entry = s->first_displayed_entry;
2673 if (ncommits == s->selected) {
2674 s->selected_entry = entry;
2677 entry = TAILQ_NEXT(entry, entry);
2682 static const struct got_error *
2683 draw_commits(struct tog_view *view)
2685 const struct got_error *err = NULL;
2686 struct tog_log_view_state *s = &view->state.log;
2687 struct commit_queue_entry *entry = s->selected_entry;
2688 int limit = view->nlines;
2690 int ncommits, author_cols = 4;
2691 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2692 char *refs_str = NULL;
2694 struct tog_color *tc;
2695 static const size_t date_display_cols = 12;
2697 if (view_is_hsplit_top(view))
2698 --limit; /* account for border */
2700 if (s->selected_entry &&
2701 !(view->searching && view->search_next_done == 0)) {
2702 struct got_reflist_head *refs;
2703 err = got_object_id_str(&id_str, s->selected_entry->id);
2706 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2707 s->selected_entry->id);
2709 err = build_refs_str(&refs_str, refs,
2710 s->selected_entry->id, s->repo);
2716 if (s->thread_args.commits_needed == 0 && !using_mock_io)
2717 halfdelay(10); /* disable fast refresh */
2719 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2720 if (asprintf(&ncommits_str, " [%d/%d] %s",
2721 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2722 (view->searching && !view->search_next_done) ?
2723 "searching..." : "loading...") == -1) {
2724 err = got_error_from_errno("asprintf");
2728 const char *search_str = NULL;
2729 const char *limit_str = NULL;
2731 if (view->searching) {
2732 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2733 search_str = "no more matches";
2734 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2735 search_str = "no matches found";
2736 else if (!view->search_next_done)
2737 search_str = "searching...";
2740 if (s->limit_view && s->commits->ncommits == 0)
2741 limit_str = "no matches found";
2743 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2744 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2745 search_str ? search_str : (refs_str ? refs_str : ""),
2746 limit_str ? limit_str : "") == -1) {
2747 err = got_error_from_errno("asprintf");
2752 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2753 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2754 "........................................",
2755 s->in_repo_path, ncommits_str) == -1) {
2756 err = got_error_from_errno("asprintf");
2760 } else if (asprintf(&header, "commit %s%s",
2761 id_str ? id_str : "........................................",
2762 ncommits_str) == -1) {
2763 err = got_error_from_errno("asprintf");
2767 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2771 werase(view->window);
2773 if (view_needs_focus_indication(view))
2774 wstandout(view->window);
2775 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2777 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2778 waddwstr(view->window, wline);
2779 while (width < view->ncols) {
2780 waddch(view->window, ' ');
2784 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2785 if (view_needs_focus_indication(view))
2786 wstandend(view->window);
2791 /* Grow author column size if necessary, and set view->maxx. */
2792 entry = s->first_displayed_entry;
2796 struct got_commit_object *c = entry->commit;
2797 char *author, *eol, *msg, *msg0;
2798 wchar_t *wauthor, *wmsg;
2800 if (ncommits >= limit - 1)
2802 if (s->use_committer)
2803 author = strdup(got_object_commit_get_committer(c));
2805 author = strdup(got_object_commit_get_author(c));
2806 if (author == NULL) {
2807 err = got_error_from_errno("strdup");
2810 err = format_author(&wauthor, &width, author, COLS,
2812 if (author_cols < width)
2813 author_cols = width;
2818 err = got_object_commit_get_logmsg(&msg0, c);
2822 while (*msg == '\n')
2824 if ((eol = strchr(msg, '\n')))
2826 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2827 date_display_cols + author_cols, 0);
2830 view->maxx = MAX(view->maxx, width);
2834 entry = TAILQ_NEXT(entry, entry);
2837 entry = s->first_displayed_entry;
2838 s->last_displayed_entry = s->first_displayed_entry;
2841 if (ncommits >= limit - 1)
2843 if (ncommits == s->selected)
2844 wstandout(view->window);
2845 err = draw_commit(view, entry->commit, entry->id,
2846 date_display_cols, author_cols);
2847 if (ncommits == s->selected)
2848 wstandend(view->window);
2852 s->last_displayed_entry = entry;
2853 entry = TAILQ_NEXT(entry, entry);
2866 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2868 struct commit_queue_entry *entry;
2871 entry = TAILQ_FIRST(&s->commits->head);
2872 if (s->first_displayed_entry == entry)
2875 entry = s->first_displayed_entry;
2876 while (entry && nscrolled < maxscroll) {
2877 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2879 s->first_displayed_entry = entry;
2885 static const struct got_error *
2886 trigger_log_thread(struct tog_view *view, int wait)
2888 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2892 halfdelay(1); /* fast refresh while loading commits */
2894 while (!ta->log_complete && !tog_thread_error &&
2895 (ta->commits_needed > 0 || ta->load_all)) {
2896 /* Wake the log thread. */
2897 errcode = pthread_cond_signal(&ta->need_commits);
2899 return got_error_set_errno(errcode,
2900 "pthread_cond_signal");
2903 * The mutex will be released while the view loop waits
2904 * in wgetch(), at which time the log thread will run.
2909 /* Display progress update in log view. */
2910 show_log_view(view);
2914 /* Wait right here while next commit is being loaded. */
2915 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2917 return got_error_set_errno(errcode,
2918 "pthread_cond_wait");
2920 /* Display progress update in log view. */
2921 show_log_view(view);
2929 static const struct got_error *
2930 request_log_commits(struct tog_view *view)
2932 struct tog_log_view_state *state = &view->state.log;
2933 const struct got_error *err = NULL;
2935 if (state->thread_args.log_complete)
2938 state->thread_args.commits_needed += view->nscrolled;
2939 err = trigger_log_thread(view, 1);
2940 view->nscrolled = 0;
2945 static const struct got_error *
2946 log_scroll_down(struct tog_view *view, int maxscroll)
2948 struct tog_log_view_state *s = &view->state.log;
2949 const struct got_error *err = NULL;
2950 struct commit_queue_entry *pentry;
2951 int nscrolled = 0, ncommits_needed;
2953 if (s->last_displayed_entry == NULL)
2956 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2957 if (s->commits->ncommits < ncommits_needed &&
2958 !s->thread_args.log_complete) {
2960 * Ask the log thread for required amount of commits.
2962 s->thread_args.commits_needed +=
2963 ncommits_needed - s->commits->ncommits;
2964 err = trigger_log_thread(view, 1);
2970 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2971 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2974 s->last_displayed_entry = pentry ?
2975 pentry : s->last_displayed_entry;
2977 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2980 s->first_displayed_entry = pentry;
2981 } while (++nscrolled < maxscroll);
2983 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2984 view->nscrolled += nscrolled;
2986 view->nscrolled = 0;
2991 static const struct got_error *
2992 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2993 struct got_commit_object *commit, struct got_object_id *commit_id,
2994 struct tog_view *log_view, struct got_repository *repo)
2996 const struct got_error *err;
2997 struct got_object_qid *parent_id;
2998 struct tog_view *diff_view;
3000 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
3001 if (diff_view == NULL)
3002 return got_error_from_errno("view_open");
3004 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3005 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
3006 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
3008 *new_view = diff_view;
3012 static const struct got_error *
3013 tree_view_visit_subtree(struct tog_tree_view_state *s,
3014 struct got_tree_object *subtree)
3016 struct tog_parent_tree *parent;
3018 parent = calloc(1, sizeof(*parent));
3020 return got_error_from_errno("calloc");
3022 parent->tree = s->tree;
3023 parent->first_displayed_entry = s->first_displayed_entry;
3024 parent->selected_entry = s->selected_entry;
3025 parent->selected = s->selected;
3026 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3029 s->first_displayed_entry = NULL;
3033 static const struct got_error *
3034 tree_view_walk_path(struct tog_tree_view_state *s,
3035 struct got_commit_object *commit, const char *path)
3037 const struct got_error *err = NULL;
3038 struct got_tree_object *tree = NULL;
3040 char *slash, *subpath = NULL;
3042 /* Walk the path and open corresponding tree objects. */
3045 struct got_tree_entry *te;
3046 struct got_object_id *tree_id;
3052 /* Ensure the correct subtree entry is selected. */
3053 slash = strchr(p, '/');
3055 te_name = strdup(p);
3057 te_name = strndup(p, slash - p);
3058 if (te_name == NULL) {
3059 err = got_error_from_errno("strndup");
3062 te = got_object_tree_find_entry(s->tree, te_name);
3064 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
3069 s->first_displayed_entry = s->selected_entry = te;
3071 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
3072 break; /* jump to this file's entry */
3074 slash = strchr(p, '/');
3076 subpath = strndup(path, slash - path);
3078 subpath = strdup(path);
3079 if (subpath == NULL) {
3080 err = got_error_from_errno("strdup");
3084 err = got_object_id_by_path(&tree_id, s->repo, commit,
3089 err = got_object_open_as_tree(&tree, s->repo, tree_id);
3094 err = tree_view_visit_subtree(s, tree);
3096 got_object_tree_close(tree);
3110 static const struct got_error *
3111 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
3112 struct commit_queue_entry *entry, const char *path,
3113 const char *head_ref_name, struct got_repository *repo)
3115 const struct got_error *err = NULL;
3116 struct tog_tree_view_state *s;
3117 struct tog_view *tree_view;
3119 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
3120 if (tree_view == NULL)
3121 return got_error_from_errno("view_open");
3123 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
3126 s = &tree_view->state.tree;
3128 *new_view = tree_view;
3130 if (got_path_is_root_dir(path))
3133 return tree_view_walk_path(s, entry->commit, path);
3136 static const struct got_error *
3137 block_signals_used_by_main_thread(void)
3142 if (sigemptyset(&sigset) == -1)
3143 return got_error_from_errno("sigemptyset");
3145 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
3146 if (sigaddset(&sigset, SIGWINCH) == -1)
3147 return got_error_from_errno("sigaddset");
3148 if (sigaddset(&sigset, SIGCONT) == -1)
3149 return got_error_from_errno("sigaddset");
3150 if (sigaddset(&sigset, SIGINT) == -1)
3151 return got_error_from_errno("sigaddset");
3152 if (sigaddset(&sigset, SIGTERM) == -1)
3153 return got_error_from_errno("sigaddset");
3155 /* ncurses handles SIGTSTP */
3156 if (sigaddset(&sigset, SIGTSTP) == -1)
3157 return got_error_from_errno("sigaddset");
3159 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
3161 return got_error_set_errno(errcode, "pthread_sigmask");
3167 log_thread(void *arg)
3169 const struct got_error *err = NULL;
3171 struct tog_log_thread_args *a = arg;
3175 * Sync startup with main thread such that we begin our
3176 * work once view_input() has released the mutex.
3178 errcode = pthread_mutex_lock(&tog_mutex);
3180 err = got_error_set_errno(errcode, "pthread_mutex_lock");
3184 err = block_signals_used_by_main_thread();
3186 pthread_mutex_unlock(&tog_mutex);
3190 while (!done && !err && !tog_fatal_signal_received()) {
3191 errcode = pthread_mutex_unlock(&tog_mutex);
3193 err = got_error_set_errno(errcode,
3194 "pthread_mutex_unlock");
3197 err = queue_commits(a);
3199 if (err->code != GOT_ERR_ITER_COMPLETED)
3203 } else if (a->commits_needed > 0 && !a->load_all) {
3206 a->commits_needed--;
3208 a->commits_needed--;
3211 errcode = pthread_mutex_lock(&tog_mutex);
3213 err = got_error_set_errno(errcode,
3214 "pthread_mutex_lock");
3216 } else if (*a->quit)
3218 else if (*a->limiting && *a->first_displayed_entry == NULL) {
3219 *a->first_displayed_entry =
3220 TAILQ_FIRST(&a->limit_commits->head);
3221 *a->selected_entry = *a->first_displayed_entry;
3222 } else if (*a->first_displayed_entry == NULL) {
3223 *a->first_displayed_entry =
3224 TAILQ_FIRST(&a->real_commits->head);
3225 *a->selected_entry = *a->first_displayed_entry;
3228 errcode = pthread_cond_signal(&a->commit_loaded);
3230 err = got_error_set_errno(errcode,
3231 "pthread_cond_signal");
3232 pthread_mutex_unlock(&tog_mutex);
3237 a->commits_needed = 0;
3239 if (a->commits_needed == 0 && !a->load_all) {
3240 errcode = pthread_cond_wait(&a->need_commits,
3243 err = got_error_set_errno(errcode,
3244 "pthread_cond_wait");
3245 pthread_mutex_unlock(&tog_mutex);
3253 a->log_complete = 1;
3254 errcode = pthread_mutex_unlock(&tog_mutex);
3256 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3259 tog_thread_error = 1;
3260 pthread_cond_signal(&a->commit_loaded);
3265 static const struct got_error *
3266 stop_log_thread(struct tog_log_view_state *s)
3268 const struct got_error *err = NULL, *thread_err = NULL;
3273 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3275 return got_error_set_errno(errcode,
3276 "pthread_cond_signal");
3277 errcode = pthread_mutex_unlock(&tog_mutex);
3279 return got_error_set_errno(errcode,
3280 "pthread_mutex_unlock");
3281 errcode = pthread_join(s->thread, (void **)&thread_err);
3283 return got_error_set_errno(errcode, "pthread_join");
3284 errcode = pthread_mutex_lock(&tog_mutex);
3286 return got_error_set_errno(errcode,
3287 "pthread_mutex_lock");
3291 if (s->thread_args.repo) {
3292 err = got_repo_close(s->thread_args.repo);
3293 s->thread_args.repo = NULL;
3296 if (s->thread_args.pack_fds) {
3297 const struct got_error *pack_err =
3298 got_repo_pack_fds_close(s->thread_args.pack_fds);
3301 s->thread_args.pack_fds = NULL;
3304 if (s->thread_args.graph) {
3305 got_commit_graph_close(s->thread_args.graph);
3306 s->thread_args.graph = NULL;
3309 return err ? err : thread_err;
3312 static const struct got_error *
3313 close_log_view(struct tog_view *view)
3315 const struct got_error *err = NULL;
3316 struct tog_log_view_state *s = &view->state.log;
3319 err = stop_log_thread(s);
3321 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3322 if (errcode && err == NULL)
3323 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3325 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3326 if (errcode && err == NULL)
3327 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3329 free_commits(&s->limit_commits);
3330 free_commits(&s->real_commits);
3331 free(s->in_repo_path);
3332 s->in_repo_path = NULL;
3335 free(s->head_ref_name);
3336 s->head_ref_name = NULL;
3341 * We use two queues to implement the limit feature: first consists of
3342 * commits matching the current limit_regex; second is the real queue
3343 * of all known commits (real_commits). When the user starts limiting,
3344 * we swap queues such that all movement and displaying functionality
3345 * works with very slight change.
3347 static const struct got_error *
3348 limit_log_view(struct tog_view *view)
3350 struct tog_log_view_state *s = &view->state.log;
3351 struct commit_queue_entry *entry;
3352 struct tog_view *v = view;
3353 const struct got_error *err = NULL;
3357 if (view_is_hsplit_top(view))
3359 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3362 /* Get the pattern */
3363 wmove(v->window, v->nlines - 1, 0);
3364 wclrtoeol(v->window);
3365 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3366 nodelay(v->window, FALSE);
3369 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3372 nodelay(v->window, TRUE);
3376 if (*pattern == '\0') {
3378 * Safety measure for the situation where the user
3379 * resets limit without previously limiting anything.
3385 * User could have pressed Ctrl+L, which refreshed the
3386 * commit queues, it means we can't save previously
3387 * (before limit took place) displayed entries,
3388 * because they would point to already free'ed memory,
3389 * so we are forced to always select first entry of
3392 s->commits = &s->real_commits;
3393 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3394 s->selected_entry = s->first_displayed_entry;
3401 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3406 /* Clear the screen while loading limit view */
3407 s->first_displayed_entry = NULL;
3408 s->last_displayed_entry = NULL;
3409 s->selected_entry = NULL;
3410 s->commits = &s->limit_commits;
3412 /* Prepare limit queue for new search */
3413 free_commits(&s->limit_commits);
3414 s->limit_commits.ncommits = 0;
3416 /* First process commits, which are in queue already */
3417 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3420 err = match_commit(&have_match, entry->id,
3421 entry->commit, &s->limit_regex);
3426 struct commit_queue_entry *matched;
3428 matched = alloc_commit_queue_entry(entry->commit,
3430 if (matched == NULL) {
3431 err = got_error_from_errno(
3432 "alloc_commit_queue_entry");
3435 matched->commit = entry->commit;
3436 got_object_commit_retain(entry->commit);
3438 matched->idx = s->limit_commits.ncommits;
3439 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3441 s->limit_commits.ncommits++;
3445 /* Second process all the commits, until we fill the screen */
3446 if (s->limit_commits.ncommits < view->nlines - 1 &&
3447 !s->thread_args.log_complete) {
3448 s->thread_args.commits_needed +=
3449 view->nlines - s->limit_commits.ncommits - 1;
3450 err = trigger_log_thread(view, 1);
3455 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3456 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3462 static const struct got_error *
3463 search_start_log_view(struct tog_view *view)
3465 struct tog_log_view_state *s = &view->state.log;
3467 s->matched_entry = NULL;
3468 s->search_entry = NULL;
3472 static const struct got_error *
3473 search_next_log_view(struct tog_view *view)
3475 const struct got_error *err = NULL;
3476 struct tog_log_view_state *s = &view->state.log;
3477 struct commit_queue_entry *entry;
3479 /* Display progress update in log view. */
3480 show_log_view(view);
3484 if (s->search_entry) {
3486 errcode = pthread_mutex_unlock(&tog_mutex);
3488 return got_error_set_errno(errcode,
3489 "pthread_mutex_unlock");
3490 ch = wgetch(view->window);
3491 errcode = pthread_mutex_lock(&tog_mutex);
3493 return got_error_set_errno(errcode,
3494 "pthread_mutex_lock");
3495 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3496 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3499 if (view->searching == TOG_SEARCH_FORWARD)
3500 entry = TAILQ_NEXT(s->search_entry, entry);
3502 entry = TAILQ_PREV(s->search_entry,
3503 commit_queue_head, entry);
3504 } else if (s->matched_entry) {
3506 * If the user has moved the cursor after we hit a match,
3507 * the position from where we should continue searching
3508 * might have changed.
3510 if (view->searching == TOG_SEARCH_FORWARD)
3511 entry = TAILQ_NEXT(s->selected_entry, entry);
3513 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3516 entry = s->selected_entry;
3522 if (entry == NULL) {
3523 if (s->thread_args.log_complete ||
3524 view->searching == TOG_SEARCH_BACKWARD) {
3525 view->search_next_done =
3526 (s->matched_entry == NULL ?
3527 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3528 s->search_entry = NULL;
3532 * Poke the log thread for more commits and return,
3533 * allowing the main loop to make progress. Search
3534 * will resume at s->search_entry once we come back.
3536 s->thread_args.commits_needed++;
3537 return trigger_log_thread(view, 0);
3540 err = match_commit(&have_match, entry->id, entry->commit,
3545 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3546 s->matched_entry = entry;
3550 s->search_entry = entry;
3551 if (view->searching == TOG_SEARCH_FORWARD)
3552 entry = TAILQ_NEXT(entry, entry);
3554 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3557 if (s->matched_entry) {
3558 int cur = s->selected_entry->idx;
3559 while (cur < s->matched_entry->idx) {
3560 err = input_log_view(NULL, view, KEY_DOWN);
3565 while (cur > s->matched_entry->idx) {
3566 err = input_log_view(NULL, view, KEY_UP);
3573 s->search_entry = NULL;
3578 static const struct got_error *
3579 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3580 struct got_repository *repo, const char *head_ref_name,
3581 const char *in_repo_path, int log_branches)
3583 const struct got_error *err = NULL;
3584 struct tog_log_view_state *s = &view->state.log;
3585 struct got_repository *thread_repo = NULL;
3586 struct got_commit_graph *thread_graph = NULL;
3589 if (in_repo_path != s->in_repo_path) {
3590 free(s->in_repo_path);
3591 s->in_repo_path = strdup(in_repo_path);
3592 if (s->in_repo_path == NULL) {
3593 err = got_error_from_errno("strdup");
3598 /* The commit queue only contains commits being displayed. */
3599 TAILQ_INIT(&s->real_commits.head);
3600 s->real_commits.ncommits = 0;
3601 s->commits = &s->real_commits;
3603 TAILQ_INIT(&s->limit_commits.head);
3605 s->limit_commits.ncommits = 0;
3608 if (head_ref_name) {
3609 s->head_ref_name = strdup(head_ref_name);
3610 if (s->head_ref_name == NULL) {
3611 err = got_error_from_errno("strdup");
3615 s->start_id = got_object_id_dup(start_id);
3616 if (s->start_id == NULL) {
3617 err = got_error_from_errno("got_object_id_dup");
3620 s->log_branches = log_branches;
3621 s->use_committer = 1;
3623 STAILQ_INIT(&s->colors);
3624 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3625 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3626 get_color_value("TOG_COLOR_COMMIT"));
3629 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3630 get_color_value("TOG_COLOR_AUTHOR"));
3632 free_colors(&s->colors);
3635 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3636 get_color_value("TOG_COLOR_DATE"));
3638 free_colors(&s->colors);
3643 view->show = show_log_view;
3644 view->input = input_log_view;
3645 view->resize = resize_log_view;
3646 view->close = close_log_view;
3647 view->search_start = search_start_log_view;
3648 view->search_next = search_next_log_view;
3650 if (s->thread_args.pack_fds == NULL) {
3651 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3655 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3656 s->thread_args.pack_fds);
3659 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3663 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3664 s->repo, NULL, NULL);
3668 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3670 err = got_error_set_errno(errcode, "pthread_cond_init");
3673 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3675 err = got_error_set_errno(errcode, "pthread_cond_init");
3679 s->thread_args.commits_needed = view->nlines;
3680 s->thread_args.graph = thread_graph;
3681 s->thread_args.real_commits = &s->real_commits;
3682 s->thread_args.limit_commits = &s->limit_commits;
3683 s->thread_args.in_repo_path = s->in_repo_path;
3684 s->thread_args.start_id = s->start_id;
3685 s->thread_args.repo = thread_repo;
3686 s->thread_args.log_complete = 0;
3687 s->thread_args.quit = &s->quit;
3688 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3689 s->thread_args.selected_entry = &s->selected_entry;
3690 s->thread_args.searching = &view->searching;
3691 s->thread_args.search_next_done = &view->search_next_done;
3692 s->thread_args.regex = &view->regex;
3693 s->thread_args.limiting = &s->limit_view;
3694 s->thread_args.limit_regex = &s->limit_regex;
3695 s->thread_args.limit_commits = &s->limit_commits;
3698 if (view->close == NULL)
3699 close_log_view(view);
3705 static const struct got_error *
3706 show_log_view(struct tog_view *view)
3708 const struct got_error *err;
3709 struct tog_log_view_state *s = &view->state.log;
3711 if (s->thread == NULL) {
3712 int errcode = pthread_create(&s->thread, NULL, log_thread,
3715 return got_error_set_errno(errcode, "pthread_create");
3716 if (s->thread_args.commits_needed > 0) {
3717 err = trigger_log_thread(view, 1);
3723 return draw_commits(view);
3727 log_move_cursor_up(struct tog_view *view, int page, int home)
3729 struct tog_log_view_state *s = &view->state.log;
3731 if (s->first_displayed_entry == NULL)
3733 if (s->selected_entry->idx == 0)
3736 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3738 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3740 if (!page && !home && s->selected > 0)
3743 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3749 static const struct got_error *
3750 log_move_cursor_down(struct tog_view *view, int page)
3752 struct tog_log_view_state *s = &view->state.log;
3753 const struct got_error *err = NULL;
3754 int eos = view->nlines - 2;
3756 if (s->first_displayed_entry == NULL)
3759 if (s->thread_args.log_complete &&
3760 s->selected_entry->idx >= s->commits->ncommits - 1)
3763 if (view_is_hsplit_top(view))
3764 --eos; /* border consumes the last line */
3767 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3770 err = log_scroll_down(view, 1);
3771 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3772 struct commit_queue_entry *entry;
3776 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3777 s->last_displayed_entry = entry;
3778 for (n = 0; n <= eos; n++) {
3781 s->first_displayed_entry = entry;
3782 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3785 s->selected = n - 1;
3787 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3788 s->thread_args.log_complete)
3789 s->selected += MIN(page,
3790 s->commits->ncommits - s->selected_entry->idx - 1);
3792 err = log_scroll_down(view, page);
3798 * We might necessarily overshoot in horizontal
3799 * splits; if so, select the last displayed commit.
3801 if (s->first_displayed_entry && s->last_displayed_entry) {
3802 s->selected = MIN(s->selected,
3803 s->last_displayed_entry->idx -
3804 s->first_displayed_entry->idx);
3809 if (s->thread_args.log_complete &&
3810 s->selected_entry->idx == s->commits->ncommits - 1)
3817 view_get_split(struct tog_view *view, int *y, int *x)
3822 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3823 if (view->child && view->child->resized_y)
3824 *y = view->child->resized_y;
3825 else if (view->resized_y)
3826 *y = view->resized_y;
3828 *y = view_split_begin_y(view->lines);
3829 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3830 if (view->child && view->child->resized_x)
3831 *x = view->child->resized_x;
3832 else if (view->resized_x)
3833 *x = view->resized_x;
3835 *x = view_split_begin_x(view->begin_x);
3839 /* Split view horizontally at y and offset view->state->selected line. */
3840 static const struct got_error *
3841 view_init_hsplit(struct tog_view *view, int y)
3843 const struct got_error *err = NULL;
3847 err = view_resize(view);
3851 err = offset_selection_down(view);
3856 static const struct got_error *
3857 log_goto_line(struct tog_view *view, int nlines)
3859 const struct got_error *err = NULL;
3860 struct tog_log_view_state *s = &view->state.log;
3861 int g, idx = s->selected_entry->idx;
3863 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3869 if (g >= s->first_displayed_entry->idx + 1 &&
3870 g <= s->last_displayed_entry->idx + 1 &&
3871 g - s->first_displayed_entry->idx - 1 < nlines) {
3872 s->selected = g - s->first_displayed_entry->idx - 1;
3878 err = log_move_cursor_down(view, g - idx - 1);
3879 if (!err && g > s->selected_entry->idx + 1)
3880 err = log_move_cursor_down(view,
3881 g - s->first_displayed_entry->idx - 1);
3884 } else if (idx + 1 > g)
3885 log_move_cursor_up(view, idx - g + 1, 0);
3887 if (g < nlines && s->first_displayed_entry->idx == 0)
3888 s->selected = g - 1;
3896 horizontal_scroll_input(struct tog_view *view, int ch)
3902 view->x -= MIN(view->x, 2);
3908 if (view->x + view->ncols / 2 < view->maxx)
3917 view->x = MAX(view->maxx - view->ncols / 2, 0);
3925 static const struct got_error *
3926 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3928 const struct got_error *err = NULL;
3929 struct tog_log_view_state *s = &view->state.log;
3932 if (s->thread_args.load_all) {
3933 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3934 s->thread_args.load_all = 0;
3935 else if (s->thread_args.log_complete) {
3936 err = log_move_cursor_down(view, s->commits->ncommits);
3937 s->thread_args.load_all = 0;
3943 eos = nscroll = view->nlines - 1;
3944 if (view_is_hsplit_top(view))
3948 return log_goto_line(view, eos);
3952 err = limit_log_view(view);
3963 horizontal_scroll_input(view, ch);
3970 log_move_cursor_up(view, 0, 0);
3975 log_move_cursor_up(view, 0, 1);
3985 log_move_cursor_up(view, nscroll, 0);
3992 err = log_move_cursor_down(view, 0);
3995 s->use_committer = !s->use_committer;
3996 view->action = s->use_committer ?
3997 "show committer" : "show commit author";
4002 /* We don't know yet how many commits, so we're forced to
4003 * traverse them all. */
4005 s->thread_args.load_all = 1;
4006 if (!s->thread_args.log_complete)
4007 return trigger_log_thread(view, 0);
4008 err = log_move_cursor_down(view, s->commits->ncommits);
4009 s->thread_args.load_all = 0;
4020 err = log_move_cursor_down(view, nscroll);
4023 if (s->selected > view->nlines - 2)
4024 s->selected = view->nlines - 2;
4025 if (s->selected > s->commits->ncommits - 1)
4026 s->selected = s->commits->ncommits - 1;
4028 if (s->commits->ncommits < view->nlines - 1 &&
4029 !s->thread_args.log_complete) {
4030 s->thread_args.commits_needed += (view->nlines - 1) -
4031 s->commits->ncommits;
4032 err = trigger_log_thread(view, 1);
4038 if (s->selected_entry == NULL)
4040 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4044 if (s->selected_entry == NULL)
4046 err = view_request_new(new_view, view, TOG_VIEW_TREE);
4052 if (ch == KEY_BACKSPACE &&
4053 got_path_is_root_dir(s->in_repo_path))
4055 err = stop_log_thread(s);
4058 if (ch == KEY_BACKSPACE) {
4060 err = got_path_dirname(&parent_path, s->in_repo_path);
4063 free(s->in_repo_path);
4064 s->in_repo_path = parent_path;
4065 s->thread_args.in_repo_path = s->in_repo_path;
4066 } else if (ch == CTRL('l')) {
4067 struct got_object_id *start_id;
4068 err = got_repo_match_object_id(&start_id, NULL,
4069 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4070 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4072 if (s->head_ref_name == NULL ||
4073 err->code != GOT_ERR_NOT_REF)
4075 /* Try to cope with deleted references. */
4076 free(s->head_ref_name);
4077 s->head_ref_name = NULL;
4078 err = got_repo_match_object_id(&start_id,
4079 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4080 &tog_refs, s->repo);
4085 s->start_id = start_id;
4086 s->thread_args.start_id = s->start_id;
4088 s->log_branches = !s->log_branches;
4090 if (s->thread_args.pack_fds == NULL) {
4091 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4095 err = got_repo_open(&s->thread_args.repo,
4096 got_repo_get_path(s->repo), NULL,
4097 s->thread_args.pack_fds);
4101 err = tog_load_refs(s->repo, 0);
4104 err = got_commit_graph_open(&s->thread_args.graph,
4105 s->in_repo_path, !s->log_branches);
4108 err = got_commit_graph_iter_start(s->thread_args.graph,
4109 s->start_id, s->repo, NULL, NULL);
4112 free_commits(&s->real_commits);
4113 free_commits(&s->limit_commits);
4114 s->first_displayed_entry = NULL;
4115 s->last_displayed_entry = NULL;
4116 s->selected_entry = NULL;
4118 s->thread_args.log_complete = 0;
4120 s->thread_args.commits_needed = view->lines;
4121 s->matched_entry = NULL;
4122 s->search_entry = NULL;
4127 err = view_request_new(new_view, view, TOG_VIEW_REF);
4137 static const struct got_error *
4138 apply_unveil(const char *repo_path, const char *worktree_path)
4140 const struct got_error *error;
4143 if (unveil("gmon.out", "rwc") != 0)
4144 return got_error_from_errno2("unveil", "gmon.out");
4146 if (repo_path && unveil(repo_path, "r") != 0)
4147 return got_error_from_errno2("unveil", repo_path);
4149 if (worktree_path && unveil(worktree_path, "rwc") != 0)
4150 return got_error_from_errno2("unveil", worktree_path);
4152 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4153 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4155 error = got_privsep_unveil_exec_helpers();
4159 if (unveil(NULL, NULL) != 0)
4160 return got_error_from_errno("unveil");
4165 static const struct got_error *
4166 init_mock_term(const char *test_script_path)
4168 const struct got_error *err = NULL;
4170 if (test_script_path == NULL || *test_script_path == '\0')
4171 return got_error_msg(GOT_ERR_IO, "GOT_TOG_TEST not defined");
4173 tog_io.f = fopen(test_script_path, "re");
4174 if (tog_io.f == NULL) {
4175 err = got_error_from_errno_fmt("fopen: %s",
4180 /* test mode, we don't want any output */
4181 tog_io.cout = fopen("/dev/null", "w+");
4182 if (tog_io.cout == NULL) {
4183 err = got_error_from_errno("fopen: /dev/null");
4187 tog_io.cin = fopen("/dev/tty", "r+");
4188 if (tog_io.cin == NULL) {
4189 err = got_error_from_errno("fopen: /dev/tty");
4193 if (fseeko(tog_io.f, 0L, SEEK_SET) == -1) {
4194 err = got_error_from_errno("fseeko");
4198 /* use local TERM so we test in different environments */
4199 if (newterm(NULL, tog_io.cout, tog_io.cin) == NULL)
4200 err = got_error_msg(GOT_ERR_IO,
4201 "newterm: failed to initialise curses");
4214 * Override default signal handlers before starting ncurses.
4215 * This should prevent ncurses from installing its own
4216 * broken cleanup() signal handler.
4218 signal(SIGWINCH, tog_sigwinch);
4219 signal(SIGPIPE, tog_sigpipe);
4220 signal(SIGCONT, tog_sigcont);
4221 signal(SIGINT, tog_sigint);
4222 signal(SIGTERM, tog_sigterm);
4224 if (using_mock_io) /* In test mode we use a fake terminal */
4230 halfdelay(1); /* Fast refresh while initial view is loading. */
4233 intrflush(stdscr, FALSE);
4234 keypad(stdscr, TRUE);
4236 if (getenv("TOG_COLORS") != NULL) {
4238 use_default_colors();
4244 static const struct got_error *
4245 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4246 struct got_repository *repo, struct got_worktree *worktree)
4248 const struct got_error *err = NULL;
4251 *in_repo_path = strdup("/");
4252 if (*in_repo_path == NULL)
4253 return got_error_from_errno("strdup");
4258 const char *prefix = got_worktree_get_path_prefix(worktree);
4261 err = got_worktree_resolve_path(&p, worktree, argv[0]);
4264 if (asprintf(in_repo_path, "%s%s%s", prefix,
4265 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4267 err = got_error_from_errno("asprintf");
4268 *in_repo_path = NULL;
4272 err = got_repo_map_path(in_repo_path, repo, argv[0]);
4277 static const struct got_error *
4278 cmd_log(int argc, char *argv[])
4280 const struct got_error *error;
4281 struct got_repository *repo = NULL;
4282 struct got_worktree *worktree = NULL;
4283 struct got_object_id *start_id = NULL;
4284 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4285 char *start_commit = NULL, *label = NULL;
4286 struct got_reference *ref = NULL;
4287 const char *head_ref_name = NULL;
4288 int ch, log_branches = 0;
4289 struct tog_view *view;
4290 int *pack_fds = NULL;
4292 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4298 start_commit = optarg;
4301 repo_path = realpath(optarg, NULL);
4302 if (repo_path == NULL)
4303 return got_error_from_errno2("realpath",
4318 error = got_repo_pack_fds_open(&pack_fds);
4322 if (repo_path == NULL) {
4323 cwd = getcwd(NULL, 0);
4325 return got_error_from_errno("getcwd");
4326 error = got_worktree_open(&worktree, cwd);
4327 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4331 strdup(got_worktree_get_repo_path(worktree));
4333 repo_path = strdup(cwd);
4334 if (repo_path == NULL) {
4335 error = got_error_from_errno("strdup");
4340 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4344 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4351 error = apply_unveil(got_repo_get_path(repo),
4352 worktree ? got_worktree_get_root_path(worktree) : NULL);
4356 /* already loaded by tog_log_with_path()? */
4357 if (TAILQ_EMPTY(&tog_refs)) {
4358 error = tog_load_refs(repo, 0);
4363 if (start_commit == NULL) {
4364 error = got_repo_match_object_id(&start_id, &label,
4365 worktree ? got_worktree_get_head_ref_name(worktree) :
4366 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4369 head_ref_name = label;
4371 error = got_ref_open(&ref, repo, start_commit, 0);
4373 head_ref_name = got_ref_get_name(ref);
4374 else if (error->code != GOT_ERR_NOT_REF)
4376 error = got_repo_match_object_id(&start_id, NULL,
4377 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4382 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4384 error = got_error_from_errno("view_open");
4387 error = open_log_view(view, start_id, repo, head_ref_name,
4388 in_repo_path, log_branches);
4392 /* Release work tree lock. */
4393 got_worktree_close(worktree);
4396 error = view_loop(view);
4406 const struct got_error *close_err = got_repo_close(repo);
4411 got_worktree_close(worktree);
4413 const struct got_error *pack_err =
4414 got_repo_pack_fds_close(pack_fds);
4426 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4427 "object1 object2\n", getprogname());
4432 match_line(const char *line, regex_t *regex, size_t nmatch,
4433 regmatch_t *regmatch)
4435 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4438 static struct tog_color *
4439 match_color(struct tog_colors *colors, const char *line)
4441 struct tog_color *tc = NULL;
4443 STAILQ_FOREACH(tc, colors, entry) {
4444 if (match_line(line, &tc->regex, 0, NULL))
4451 static const struct got_error *
4452 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4453 WINDOW *window, int skipcol, regmatch_t *regmatch)
4455 const struct got_error *err = NULL;
4457 wchar_t *wline = NULL;
4458 int rme, rms, n, width, scrollx;
4459 int width0 = 0, width1 = 0, width2 = 0;
4460 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4464 rms = regmatch->rm_so;
4465 rme = regmatch->rm_eo;
4467 err = expand_tab(&exstr, line);
4471 /* Split the line into 3 segments, according to match offsets. */
4472 seg0 = strndup(exstr, rms);
4474 err = got_error_from_errno("strndup");
4477 seg1 = strndup(exstr + rms, rme - rms);
4479 err = got_error_from_errno("strndup");
4482 seg2 = strdup(exstr + rme);
4484 err = got_error_from_errno("strndup");
4488 /* draw up to matched token if we haven't scrolled past it */
4489 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4493 n = MAX(width0 - skipcol, 0);
4496 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4497 wlimit, col_tab_align, 1);
4500 waddwstr(window, &wline[scrollx]);
4510 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4514 wlen = wcslen(wline);
4516 width = wcwidth(wline[i]);
4518 /* should not happen, tabs are expanded */
4519 err = got_error(GOT_ERR_RANGE);
4522 if (width0 + w + width > skipcol)
4527 /* draw (visible part of) matched token (if scrolled into it) */
4528 if (width1 - w > 0) {
4529 wattron(window, A_STANDOUT);
4530 waddwstr(window, &wline[i]);
4531 wattroff(window, A_STANDOUT);
4532 wlimit -= (width1 - w);
4533 *wtotal += (width1 - w);
4537 if (wlimit > 0) { /* draw rest of line */
4539 if (skipcol > width0 + width1) {
4540 err = format_line(&wline, &width2, &scrollx, seg2,
4541 skipcol - (width0 + width1), wlimit,
4545 waddwstr(window, &wline[scrollx]);
4547 err = format_line(&wline, &width2, NULL, seg2, 0,
4548 wlimit, col_tab_align, 1);
4551 waddwstr(window, wline);
4565 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4568 int *eof, *first, *selected;
4570 if (view->type == TOG_VIEW_DIFF) {
4571 struct tog_diff_view_state *s = &view->state.diff;
4573 first = &s->first_displayed_line;
4577 } else if (view->type == TOG_VIEW_HELP) {
4578 struct tog_help_view_state *s = &view->state.help;
4580 first = &s->first_displayed_line;
4584 } else if (view->type == TOG_VIEW_BLAME) {
4585 struct tog_blame_view_state *s = &view->state.blame;
4587 first = &s->first_displayed_line;
4588 selected = &s->selected_line;
4594 /* Center gline in the middle of the page like vi(1). */
4595 if (*lineno < view->gline - (view->nlines - 3) / 2)
4597 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4606 *selected = view->gline <= (view->nlines - 3) / 2 ?
4607 view->gline : (view->nlines - 3) / 2 + 1;
4613 static const struct got_error *
4614 draw_file(struct tog_view *view, const char *header)
4616 struct tog_diff_view_state *s = &view->state.diff;
4617 regmatch_t *regmatch = &view->regmatch;
4618 const struct got_error *err;
4621 size_t linesize = 0;
4625 int max_lines = view->nlines;
4626 int nlines = s->nlines;
4629 s->lineno = s->first_displayed_line - 1;
4630 line_offset = s->lines[s->first_displayed_line - 1].offset;
4631 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4632 return got_error_from_errno("fseek");
4634 werase(view->window);
4636 if (view->gline > s->nlines - 1)
4637 view->gline = s->nlines - 1;
4640 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4641 1 : view->gline - (view->nlines - 3) / 2 :
4642 s->lineno + s->selected_line;
4644 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4645 return got_error_from_errno("asprintf");
4646 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4652 if (view_needs_focus_indication(view))
4653 wstandout(view->window);
4654 waddwstr(view->window, wline);
4657 while (width++ < view->ncols)
4658 waddch(view->window, ' ');
4659 if (view_needs_focus_indication(view))
4660 wstandend(view->window);
4670 while (max_lines > 0 && nprinted < max_lines) {
4671 enum got_diff_line_type linetype;
4674 linelen = getline(&line, &linesize, s->f);
4675 if (linelen == -1) {
4681 return got_ferror(s->f, GOT_ERR_IO);
4684 if (++s->lineno < s->first_displayed_line)
4686 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4688 if (s->lineno == view->hiline)
4691 /* Set view->maxx based on full line length. */
4692 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4698 view->maxx = MAX(view->maxx, width);
4702 linetype = s->lines[s->lineno].type;
4703 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4704 linetype < GOT_DIFF_LINE_CONTEXT)
4705 attr |= COLOR_PAIR(linetype);
4707 wattron(view->window, attr);
4708 if (s->first_displayed_line + nprinted == s->matched_line &&
4709 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4710 err = add_matched_line(&width, line, view->ncols, 0,
4711 view->window, view->x, regmatch);
4718 err = format_line(&wline, &width, &skip, line,
4719 view->x, view->ncols, 0, view->x ? 1 : 0);
4724 waddwstr(view->window, &wline[skip]);
4728 if (s->lineno == view->hiline) {
4729 /* highlight full gline length */
4730 while (width++ < view->ncols)
4731 waddch(view->window, ' ');
4733 if (width <= view->ncols - 1)
4734 waddch(view->window, '\n');
4737 wattroff(view->window, attr);
4738 if (++nprinted == 1)
4739 s->first_displayed_line = s->lineno;
4743 s->last_displayed_line = s->first_displayed_line +
4746 s->last_displayed_line = s->first_displayed_line;
4751 while (nprinted < view->nlines) {
4752 waddch(view->window, '\n');
4756 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4762 wstandout(view->window);
4763 waddwstr(view->window, wline);
4766 wstandend(view->window);
4773 get_datestr(time_t *time, char *datebuf)
4775 struct tm mytm, *tm;
4778 tm = gmtime_r(time, &mytm);
4781 s = asctime_r(tm, datebuf);
4784 p = strchr(s, '\n');
4790 static const struct got_error *
4791 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4792 off_t off, uint8_t type)
4794 struct got_diff_line *p;
4796 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4798 return got_error_from_errno("reallocarray");
4800 (*lines)[*nlines].offset = off;
4801 (*lines)[*nlines].type = type;
4807 static const struct got_error *
4808 cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4809 struct got_diff_line *s_lines, size_t s_nlines)
4811 struct got_diff_line *p;
4815 if (fseeko(src, 0L, SEEK_SET) == -1)
4816 return got_error_from_errno("fseeko");
4819 r = fread(buf, 1, sizeof(buf), src);
4822 return got_error_from_errno("fread");
4826 if (fwrite(buf, 1, r, dst) != r)
4827 return got_ferror(dst, GOT_ERR_IO);
4830 if (s_nlines == 0 && *d_nlines == 0)
4834 * If commit info was in dst, increment line offsets
4835 * of the appended diff content, but skip s_lines[0]
4836 * because offset zero is already in *d_lines.
4838 if (*d_nlines > 0) {
4839 for (i = 1; i < s_nlines; ++i)
4840 s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4848 p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4850 /* d_lines is freed in close_diff_view() */
4851 return got_error_from_errno("reallocarray");
4856 memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
4857 *d_nlines += s_nlines;
4862 static const struct got_error *
4863 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4864 struct got_object_id *commit_id, struct got_reflist_head *refs,
4865 struct got_repository *repo, int ignore_ws, int force_text_diff,
4866 struct got_diffstat_cb_arg *dsa, FILE *outfile)
4868 const struct got_error *err = NULL;
4869 char datebuf[26], *datestr;
4870 struct got_commit_object *commit;
4871 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4872 time_t committer_time;
4873 const char *author, *committer;
4874 char *refs_str = NULL;
4875 struct got_pathlist_entry *pe;
4880 err = build_refs_str(&refs_str, refs, commit_id, repo);
4885 err = got_object_open_as_commit(&commit, repo, commit_id);
4889 err = got_object_id_str(&id_str, commit_id);
4891 err = got_error_from_errno("got_object_id_str");
4895 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4899 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4900 refs_str ? refs_str : "", refs_str ? ")" : "");
4902 err = got_error_from_errno("fprintf");
4906 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4910 n = fprintf(outfile, "from: %s\n",
4911 got_object_commit_get_author(commit));
4913 err = got_error_from_errno("fprintf");
4917 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4921 author = got_object_commit_get_author(commit);
4922 committer = got_object_commit_get_committer(commit);
4923 if (strcmp(author, committer) != 0) {
4924 n = fprintf(outfile, "via: %s\n", committer);
4926 err = got_error_from_errno("fprintf");
4930 err = add_line_metadata(lines, nlines, outoff,
4931 GOT_DIFF_LINE_AUTHOR);
4935 committer_time = got_object_commit_get_committer_time(commit);
4936 datestr = get_datestr(&committer_time, datebuf);
4938 n = fprintf(outfile, "date: %s UTC\n", datestr);
4940 err = got_error_from_errno("fprintf");
4944 err = add_line_metadata(lines, nlines, outoff,
4945 GOT_DIFF_LINE_DATE);
4949 if (got_object_commit_get_nparents(commit) > 1) {
4950 const struct got_object_id_queue *parent_ids;
4951 struct got_object_qid *qid;
4953 parent_ids = got_object_commit_get_parent_ids(commit);
4954 STAILQ_FOREACH(qid, parent_ids, entry) {
4955 err = got_object_id_str(&id_str, &qid->id);
4958 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4960 err = got_error_from_errno("fprintf");
4964 err = add_line_metadata(lines, nlines, outoff,
4965 GOT_DIFF_LINE_META);
4973 err = got_object_commit_get_logmsg(&logmsg, commit);
4977 while ((line = strsep(&s, "\n")) != NULL) {
4978 n = fprintf(outfile, "%s\n", line);
4980 err = got_error_from_errno("fprintf");
4984 err = add_line_metadata(lines, nlines, outoff,
4985 GOT_DIFF_LINE_LOGMSG);
4990 TAILQ_FOREACH(pe, dsa->paths, entry) {
4991 struct got_diff_changed_path *cp = pe->data;
4992 int pad = dsa->max_path_len - pe->path_len + 1;
4994 n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
4995 pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
4996 dsa->rm_cols + 1, cp->rm);
4998 err = got_error_from_errno("fprintf");
5002 err = add_line_metadata(lines, nlines, outoff,
5003 GOT_DIFF_LINE_CHANGES);
5008 fputc('\n', outfile);
5010 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5014 n = fprintf(outfile,
5015 "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5016 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5017 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5019 err = got_error_from_errno("fprintf");
5023 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5027 fputc('\n', outfile);
5029 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5034 got_object_commit_close(commit);
5043 static const struct got_error *
5044 create_diff(struct tog_diff_view_state *s)
5046 const struct got_error *err = NULL;
5047 FILE *f = NULL, *tmp_diff_file = NULL;
5049 struct got_diff_line *lines = NULL;
5050 struct got_pathlist_head changed_paths;
5052 TAILQ_INIT(&changed_paths);
5055 s->lines = malloc(sizeof(*s->lines));
5056 if (s->lines == NULL)
5057 return got_error_from_errno("malloc");
5062 err = got_error_from_errno("got_opentemp");
5065 tmp_diff_file = got_opentemp();
5066 if (tmp_diff_file == NULL) {
5067 err = got_error_from_errno("got_opentemp");
5070 if (s->f && fclose(s->f) == EOF) {
5071 err = got_error_from_errno("fclose");
5077 err = got_object_get_type(&obj_type, s->repo, s->id1);
5079 err = got_object_get_type(&obj_type, s->repo, s->id2);
5084 case GOT_OBJ_TYPE_BLOB:
5085 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5086 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5087 s->label1, s->label2, tog_diff_algo, s->diff_context,
5088 s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5091 case GOT_OBJ_TYPE_TREE:
5092 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5093 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5094 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5095 s->force_text_diff, NULL, s->repo, s->f);
5097 case GOT_OBJ_TYPE_COMMIT: {
5098 const struct got_object_id_queue *parent_ids;
5099 struct got_object_qid *pid;
5100 struct got_commit_object *commit2;
5101 struct got_reflist_head *refs;
5103 struct got_diffstat_cb_arg dsa = {
5106 s->ignore_whitespace,
5111 lines = malloc(sizeof(*lines));
5112 if (lines == NULL) {
5113 err = got_error_from_errno("malloc");
5117 /* build diff first in tmp file then append to commit info */
5118 err = got_diff_objects_as_commits(&lines, &nlines,
5119 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5120 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5121 s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5125 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
5128 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5129 /* Show commit info if we're diffing to a parent/root commit. */
5130 if (s->id1 == NULL) {
5131 err = write_commit_info(&s->lines, &s->nlines, s->id2,
5132 refs, s->repo, s->ignore_whitespace,
5133 s->force_text_diff, &dsa, s->f);
5137 parent_ids = got_object_commit_get_parent_ids(commit2);
5138 STAILQ_FOREACH(pid, parent_ids, entry) {
5139 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5140 err = write_commit_info(&s->lines,
5141 &s->nlines, s->id2, refs, s->repo,
5142 s->ignore_whitespace,
5143 s->force_text_diff, &dsa, s->f);
5150 got_object_commit_close(commit2);
5152 err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5157 err = got_error(GOT_ERR_OBJ_TYPE);
5162 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5163 if (s->f && fflush(s->f) != 0 && err == NULL)
5164 err = got_error_from_errno("fflush");
5165 if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5166 err = got_error_from_errno("fclose");
5171 diff_view_indicate_progress(struct tog_view *view)
5173 mvwaddstr(view->window, 0, 0, "diffing...");
5178 static const struct got_error *
5179 search_start_diff_view(struct tog_view *view)
5181 struct tog_diff_view_state *s = &view->state.diff;
5183 s->matched_line = 0;
5188 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5189 size_t *nlines, int **first, int **last, int **match, int **selected)
5191 struct tog_diff_view_state *s = &view->state.diff;
5194 *nlines = s->nlines;
5195 *line_offsets = NULL;
5196 *match = &s->matched_line;
5197 *first = &s->first_displayed_line;
5198 *last = &s->last_displayed_line;
5199 *selected = &s->selected_line;
5202 static const struct got_error *
5203 search_next_view_match(struct tog_view *view)
5205 const struct got_error *err = NULL;
5209 size_t linesize = 0;
5211 off_t *line_offsets;
5213 int *first, *last, *match, *selected;
5215 if (!view->search_setup)
5216 return got_error_msg(GOT_ERR_NOT_IMPL,
5217 "view search not supported");
5218 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5221 if (!view->searching) {
5222 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5227 if (view->searching == TOG_SEARCH_FORWARD)
5228 lineno = *first + 1;
5230 lineno = *first - 1;
5232 lineno = *first - 1 + *selected;
5237 if (lineno <= 0 || lineno > nlines) {
5239 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5243 if (view->searching == TOG_SEARCH_FORWARD)
5249 offset = view->type == TOG_VIEW_DIFF ?
5250 view->state.diff.lines[lineno - 1].offset :
5251 line_offsets[lineno - 1];
5252 if (fseeko(f, offset, SEEK_SET) != 0) {
5254 return got_error_from_errno("fseeko");
5256 linelen = getline(&line, &linesize, f);
5257 if (linelen != -1) {
5259 err = expand_tab(&exstr, line);
5262 if (match_line(exstr, &view->regex, 1,
5264 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5271 if (view->searching == TOG_SEARCH_FORWARD)
5286 static const struct got_error *
5287 close_diff_view(struct tog_view *view)
5289 const struct got_error *err = NULL;
5290 struct tog_diff_view_state *s = &view->state.diff;
5296 if (s->f && fclose(s->f) == EOF)
5297 err = got_error_from_errno("fclose");
5299 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5300 err = got_error_from_errno("fclose");
5302 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5303 err = got_error_from_errno("fclose");
5305 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5306 err = got_error_from_errno("close");
5308 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5309 err = got_error_from_errno("close");
5317 static const struct got_error *
5318 open_diff_view(struct tog_view *view, struct got_object_id *id1,
5319 struct got_object_id *id2, const char *label1, const char *label2,
5320 int diff_context, int ignore_whitespace, int force_text_diff,
5321 struct tog_view *parent_view, struct got_repository *repo)
5323 const struct got_error *err;
5324 struct tog_diff_view_state *s = &view->state.diff;
5326 memset(s, 0, sizeof(*s));
5330 if (id1 != NULL && id2 != NULL) {
5333 err = got_object_get_type(&type1, repo, id1);
5336 err = got_object_get_type(&type2, repo, id2);
5340 if (type1 != type2) {
5341 err = got_error(GOT_ERR_OBJ_TYPE);
5345 s->first_displayed_line = 1;
5346 s->last_displayed_line = view->nlines;
5347 s->selected_line = 1;
5355 s->id1 = got_object_id_dup(id1);
5356 if (s->id1 == NULL) {
5357 err = got_error_from_errno("got_object_id_dup");
5363 s->id2 = got_object_id_dup(id2);
5364 if (s->id2 == NULL) {
5365 err = got_error_from_errno("got_object_id_dup");
5369 s->f1 = got_opentemp();
5370 if (s->f1 == NULL) {
5371 err = got_error_from_errno("got_opentemp");
5375 s->f2 = got_opentemp();
5376 if (s->f2 == NULL) {
5377 err = got_error_from_errno("got_opentemp");
5381 s->fd1 = got_opentempfd();
5383 err = got_error_from_errno("got_opentempfd");
5387 s->fd2 = got_opentempfd();
5389 err = got_error_from_errno("got_opentempfd");
5393 s->diff_context = diff_context;
5394 s->ignore_whitespace = ignore_whitespace;
5395 s->force_text_diff = force_text_diff;
5396 s->parent_view = parent_view;
5399 if (has_colors() && getenv("TOG_COLORS") != NULL && !using_mock_io) {
5402 rc = init_pair(GOT_DIFF_LINE_MINUS,
5403 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5405 rc = init_pair(GOT_DIFF_LINE_PLUS,
5406 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5408 rc = init_pair(GOT_DIFF_LINE_HUNK,
5409 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5411 rc = init_pair(GOT_DIFF_LINE_META,
5412 get_color_value("TOG_COLOR_DIFF_META"), -1);
5414 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5415 get_color_value("TOG_COLOR_DIFF_META"), -1);
5417 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5418 get_color_value("TOG_COLOR_DIFF_META"), -1);
5420 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5421 get_color_value("TOG_COLOR_DIFF_META"), -1);
5423 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5424 get_color_value("TOG_COLOR_AUTHOR"), -1);
5426 rc = init_pair(GOT_DIFF_LINE_DATE,
5427 get_color_value("TOG_COLOR_DATE"), -1);
5429 err = got_error(GOT_ERR_RANGE);
5434 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5435 view_is_splitscreen(view))
5436 show_log_view(parent_view); /* draw border */
5437 diff_view_indicate_progress(view);
5439 err = create_diff(s);
5441 view->show = show_diff_view;
5442 view->input = input_diff_view;
5443 view->reset = reset_diff_view;
5444 view->close = close_diff_view;
5445 view->search_start = search_start_diff_view;
5446 view->search_setup = search_setup_diff_view;
5447 view->search_next = search_next_view_match;
5450 if (view->close == NULL)
5451 close_diff_view(view);
5457 static const struct got_error *
5458 show_diff_view(struct tog_view *view)
5460 const struct got_error *err;
5461 struct tog_diff_view_state *s = &view->state.diff;
5462 char *id_str1 = NULL, *id_str2, *header;
5463 const char *label1, *label2;
5466 err = got_object_id_str(&id_str1, s->id1);
5469 label1 = s->label1 ? s->label1 : id_str1;
5471 label1 = "/dev/null";
5473 err = got_object_id_str(&id_str2, s->id2);
5476 label2 = s->label2 ? s->label2 : id_str2;
5478 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5479 err = got_error_from_errno("asprintf");
5487 err = draw_file(view, header);
5492 static const struct got_error *
5493 set_selected_commit(struct tog_diff_view_state *s,
5494 struct commit_queue_entry *entry)
5496 const struct got_error *err;
5497 const struct got_object_id_queue *parent_ids;
5498 struct got_commit_object *selected_commit;
5499 struct got_object_qid *pid;
5502 s->id2 = got_object_id_dup(entry->id);
5504 return got_error_from_errno("got_object_id_dup");
5506 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5509 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5511 pid = STAILQ_FIRST(parent_ids);
5512 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5513 got_object_commit_close(selected_commit);
5517 static const struct got_error *
5518 reset_diff_view(struct tog_view *view)
5520 struct tog_diff_view_state *s = &view->state.diff;
5523 wclear(view->window);
5524 s->first_displayed_line = 1;
5525 s->last_displayed_line = view->nlines;
5526 s->matched_line = 0;
5527 diff_view_indicate_progress(view);
5528 return create_diff(s);
5532 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5536 i = start = s->first_displayed_line - 1;
5538 while (s->lines[i].type != type) {
5542 return; /* do nothing, requested type not in file */
5545 s->selected_line = 1;
5546 s->first_displayed_line = i;
5550 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5554 i = start = s->first_displayed_line + 1;
5556 while (s->lines[i].type != type) {
5557 if (i == s->nlines - 1)
5560 return; /* do nothing, requested type not in file */
5563 s->selected_line = 1;
5564 s->first_displayed_line = i;
5567 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5569 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5572 static const struct got_error *
5573 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5575 const struct got_error *err = NULL;
5576 struct tog_diff_view_state *s = &view->state.diff;
5577 struct tog_log_view_state *ls;
5578 struct commit_queue_entry *old_selected_entry;
5580 size_t linesize = 0;
5582 int i, nscroll = view->nlines - 1, up = 0;
5584 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5593 horizontal_scroll_input(view, ch);
5598 s->force_text_diff = !s->force_text_diff;
5599 view->action = s->force_text_diff ?
5600 "force ASCII text enabled" :
5601 "force ASCII text disabled";
5603 else if (ch == 'w') {
5604 s->ignore_whitespace = !s->ignore_whitespace;
5605 view->action = s->ignore_whitespace ?
5606 "ignore whitespace enabled" :
5607 "ignore whitespace disabled";
5609 err = reset_diff_view(view);
5613 s->first_displayed_line = 1;
5622 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5628 if (s->first_displayed_line > 1)
5629 s->first_displayed_line--;
5640 if (s->first_displayed_line == 1) {
5645 while (i++ < nscroll && s->first_displayed_line > 1)
5646 s->first_displayed_line--;
5652 s->first_displayed_line++;
5669 while (!s->eof && i++ < nscroll) {
5670 linelen = getline(&line, &linesize, s->f);
5671 s->first_displayed_line++;
5672 if (linelen == -1) {
5676 err = got_ferror(s->f, GOT_ERR_IO);
5683 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5686 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5689 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5692 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5695 if (s->diff_context > 0) {
5697 s->matched_line = 0;
5698 diff_view_indicate_progress(view);
5699 err = create_diff(s);
5700 if (s->first_displayed_line + view->nlines - 1 >
5702 s->first_displayed_line = 1;
5703 s->last_displayed_line = view->nlines;
5709 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5711 s->matched_line = 0;
5712 diff_view_indicate_progress(view);
5713 err = create_diff(s);
5725 if (s->parent_view == NULL) {
5729 s->parent_view->count = view->count;
5731 if (s->parent_view->type == TOG_VIEW_LOG) {
5732 ls = &s->parent_view->state.log;
5733 old_selected_entry = ls->selected_entry;
5735 err = input_log_view(NULL, s->parent_view,
5736 up ? KEY_UP : KEY_DOWN);
5739 view->count = s->parent_view->count;
5741 if (old_selected_entry == ls->selected_entry)
5744 err = set_selected_commit(s, ls->selected_entry);
5747 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5748 struct tog_blame_view_state *bs;
5749 struct got_object_id *id, *prev_id;
5751 bs = &s->parent_view->state.blame;
5752 prev_id = get_annotation_for_line(bs->blame.lines,
5753 bs->blame.nlines, bs->last_diffed_line);
5755 err = input_blame_view(&view, s->parent_view,
5756 up ? KEY_UP : KEY_DOWN);
5759 view->count = s->parent_view->count;
5761 if (prev_id == NULL)
5763 id = get_selected_commit_id(bs->blame.lines,
5764 bs->blame.nlines, bs->first_displayed_line,
5769 if (!got_object_id_cmp(prev_id, id))
5772 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5776 s->first_displayed_line = 1;
5777 s->last_displayed_line = view->nlines;
5778 s->matched_line = 0;
5781 diff_view_indicate_progress(view);
5782 err = create_diff(s);
5792 static const struct got_error *
5793 cmd_diff(int argc, char *argv[])
5795 const struct got_error *error;
5796 struct got_repository *repo = NULL;
5797 struct got_worktree *worktree = NULL;
5798 struct got_object_id *id1 = NULL, *id2 = NULL;
5799 char *repo_path = NULL, *cwd = NULL;
5800 char *id_str1 = NULL, *id_str2 = NULL;
5801 char *label1 = NULL, *label2 = NULL;
5802 int diff_context = 3, ignore_whitespace = 0;
5803 int ch, force_text_diff = 0;
5805 struct tog_view *view;
5806 int *pack_fds = NULL;
5808 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5811 force_text_diff = 1;
5814 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5817 errx(1, "number of context lines is %s: %s",
5821 repo_path = realpath(optarg, NULL);
5822 if (repo_path == NULL)
5823 return got_error_from_errno2("realpath",
5825 got_path_strip_trailing_slashes(repo_path);
5828 ignore_whitespace = 1;
5840 usage_diff(); /* TODO show local worktree changes */
5841 } else if (argc == 2) {
5847 error = got_repo_pack_fds_open(&pack_fds);
5851 if (repo_path == NULL) {
5852 cwd = getcwd(NULL, 0);
5854 return got_error_from_errno("getcwd");
5855 error = got_worktree_open(&worktree, cwd);
5856 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5860 strdup(got_worktree_get_repo_path(worktree));
5862 repo_path = strdup(cwd);
5863 if (repo_path == NULL) {
5864 error = got_error_from_errno("strdup");
5869 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5875 error = apply_unveil(got_repo_get_path(repo), NULL);
5879 error = tog_load_refs(repo, 0);
5883 error = got_repo_match_object_id(&id1, &label1, id_str1,
5884 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5888 error = got_repo_match_object_id(&id2, &label2, id_str2,
5889 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5893 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5895 error = got_error_from_errno("view_open");
5898 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5899 ignore_whitespace, force_text_diff, NULL, repo);
5902 error = view_loop(view);
5909 const struct got_error *close_err = got_repo_close(repo);
5914 got_worktree_close(worktree);
5916 const struct got_error *pack_err =
5917 got_repo_pack_fds_close(pack_fds);
5930 "usage: %s blame [-c commit] [-r repository-path] path\n",
5935 struct tog_blame_line {
5937 struct got_object_id *id;
5940 static const struct got_error *
5941 draw_blame(struct tog_view *view)
5943 struct tog_blame_view_state *s = &view->state.blame;
5944 struct tog_blame *blame = &s->blame;
5945 regmatch_t *regmatch = &view->regmatch;
5946 const struct got_error *err;
5947 int lineno = 0, nprinted = 0;
5949 size_t linesize = 0;
5953 struct tog_blame_line *blame_line;
5954 struct got_object_id *prev_id = NULL;
5956 struct tog_color *tc;
5958 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5963 werase(view->window);
5965 if (asprintf(&line, "commit %s", id_str) == -1) {
5966 err = got_error_from_errno("asprintf");
5971 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5976 if (view_needs_focus_indication(view))
5977 wstandout(view->window);
5978 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5980 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5981 waddwstr(view->window, wline);
5982 while (width++ < view->ncols)
5983 waddch(view->window, ' ');
5985 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5986 if (view_needs_focus_indication(view))
5987 wstandend(view->window);
5991 if (view->gline > blame->nlines)
5992 view->gline = blame->nlines;
5994 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5995 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5996 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5998 return got_error_from_errno("asprintf");
6001 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6006 waddwstr(view->window, wline);
6009 if (width < view->ncols - 1)
6010 waddch(view->window, '\n');
6014 while (nprinted < view->nlines - 2) {
6015 linelen = getline(&line, &linesize, blame->f);
6016 if (linelen == -1) {
6017 if (feof(blame->f)) {
6022 return got_ferror(blame->f, GOT_ERR_IO);
6024 if (++lineno < s->first_displayed_line)
6026 if (view->gline && !gotoline(view, &lineno, &nprinted))
6029 /* Set view->maxx based on full line length. */
6030 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6037 view->maxx = MAX(view->maxx, width);
6039 if (nprinted == s->selected_line - 1)
6040 wstandout(view->window);
6042 if (blame->nlines > 0) {
6043 blame_line = &blame->lines[lineno - 1];
6044 if (blame_line->annotated && prev_id &&
6045 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6046 !(nprinted == s->selected_line - 1)) {
6047 waddstr(view->window, " ");
6048 } else if (blame_line->annotated) {
6050 err = got_object_id_str(&id_str,
6056 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6058 wattr_on(view->window,
6059 COLOR_PAIR(tc->colorpair), NULL);
6060 wprintw(view->window, "%.8s", id_str);
6062 wattr_off(view->window,
6063 COLOR_PAIR(tc->colorpair), NULL);
6065 prev_id = blame_line->id;
6067 waddstr(view->window, "........");
6071 waddstr(view->window, "........");
6075 if (nprinted == s->selected_line - 1)
6076 wstandend(view->window);
6077 waddstr(view->window, " ");
6079 if (view->ncols <= 9) {
6081 } else if (s->first_displayed_line + nprinted ==
6083 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6084 err = add_matched_line(&width, line, view->ncols - 9, 9,
6085 view->window, view->x, regmatch);
6093 err = format_line(&wline, &width, &skip, line,
6094 view->x, view->ncols - 9, 9, 1);
6099 waddwstr(view->window, &wline[skip]);
6105 if (width <= view->ncols - 1)
6106 waddch(view->window, '\n');
6107 if (++nprinted == 1)
6108 s->first_displayed_line = lineno;
6111 s->last_displayed_line = lineno;
6118 static const struct got_error *
6119 blame_cb(void *arg, int nlines, int lineno,
6120 struct got_commit_object *commit, struct got_object_id *id)
6122 const struct got_error *err = NULL;
6123 struct tog_blame_cb_args *a = arg;
6124 struct tog_blame_line *line;
6127 if (nlines != a->nlines ||
6128 (lineno != -1 && lineno < 1) || lineno > a->nlines)
6129 return got_error(GOT_ERR_RANGE);
6131 errcode = pthread_mutex_lock(&tog_mutex);
6133 return got_error_set_errno(errcode, "pthread_mutex_lock");
6135 if (*a->quit) { /* user has quit the blame view */
6136 err = got_error(GOT_ERR_ITER_COMPLETED);
6141 goto done; /* no change in this commit */
6143 line = &a->lines[lineno - 1];
6144 if (line->annotated)
6147 line->id = got_object_id_dup(id);
6148 if (line->id == NULL) {
6149 err = got_error_from_errno("got_object_id_dup");
6152 line->annotated = 1;
6154 errcode = pthread_mutex_unlock(&tog_mutex);
6156 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6161 blame_thread(void *arg)
6163 const struct got_error *err, *close_err;
6164 struct tog_blame_thread_args *ta = arg;
6165 struct tog_blame_cb_args *a = ta->cb_args;
6166 int errcode, fd1 = -1, fd2 = -1;
6167 FILE *f1 = NULL, *f2 = NULL;
6169 fd1 = got_opentempfd();
6171 return (void *)got_error_from_errno("got_opentempfd");
6173 fd2 = got_opentempfd();
6175 err = got_error_from_errno("got_opentempfd");
6179 f1 = got_opentemp();
6181 err = (void *)got_error_from_errno("got_opentemp");
6184 f2 = got_opentemp();
6186 err = (void *)got_error_from_errno("got_opentemp");
6190 err = block_signals_used_by_main_thread();
6194 err = got_blame(ta->path, a->commit_id, ta->repo,
6195 tog_diff_algo, blame_cb, ta->cb_args,
6196 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6197 if (err && err->code == GOT_ERR_CANCELLED)
6200 errcode = pthread_mutex_lock(&tog_mutex);
6202 err = got_error_set_errno(errcode, "pthread_mutex_lock");
6206 close_err = got_repo_close(ta->repo);
6212 errcode = pthread_mutex_unlock(&tog_mutex);
6213 if (errcode && err == NULL)
6214 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6217 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6218 err = got_error_from_errno("close");
6219 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6220 err = got_error_from_errno("close");
6221 if (f1 && fclose(f1) == EOF && err == NULL)
6222 err = got_error_from_errno("fclose");
6223 if (f2 && fclose(f2) == EOF && err == NULL)
6224 err = got_error_from_errno("fclose");
6229 static struct got_object_id *
6230 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6231 int first_displayed_line, int selected_line)
6233 struct tog_blame_line *line;
6238 line = &lines[first_displayed_line - 1 + selected_line - 1];
6239 if (!line->annotated)
6245 static struct got_object_id *
6246 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6249 struct tog_blame_line *line;
6251 if (nlines <= 0 || lineno >= nlines)
6254 line = &lines[lineno - 1];
6255 if (!line->annotated)
6261 static const struct got_error *
6262 stop_blame(struct tog_blame *blame)
6264 const struct got_error *err = NULL;
6267 if (blame->thread) {
6269 errcode = pthread_mutex_unlock(&tog_mutex);
6271 return got_error_set_errno(errcode,
6272 "pthread_mutex_unlock");
6273 errcode = pthread_join(blame->thread, (void **)&err);
6275 return got_error_set_errno(errcode, "pthread_join");
6276 errcode = pthread_mutex_lock(&tog_mutex);
6278 return got_error_set_errno(errcode,
6279 "pthread_mutex_lock");
6280 if (err && err->code == GOT_ERR_ITER_COMPLETED)
6282 blame->thread = NULL;
6284 if (blame->thread_args.repo) {
6285 const struct got_error *close_err;
6286 close_err = got_repo_close(blame->thread_args.repo);
6289 blame->thread_args.repo = NULL;
6292 if (fclose(blame->f) == EOF && err == NULL)
6293 err = got_error_from_errno("fclose");
6297 for (i = 0; i < blame->nlines; i++)
6298 free(blame->lines[i].id);
6300 blame->lines = NULL;
6302 free(blame->cb_args.commit_id);
6303 blame->cb_args.commit_id = NULL;
6304 if (blame->pack_fds) {
6305 const struct got_error *pack_err =
6306 got_repo_pack_fds_close(blame->pack_fds);
6309 blame->pack_fds = NULL;
6314 static const struct got_error *
6315 cancel_blame_view(void *arg)
6317 const struct got_error *err = NULL;
6321 errcode = pthread_mutex_lock(&tog_mutex);
6323 return got_error_set_errno(errcode,
6324 "pthread_mutex_unlock");
6327 err = got_error(GOT_ERR_CANCELLED);
6329 errcode = pthread_mutex_unlock(&tog_mutex);
6331 return got_error_set_errno(errcode,
6332 "pthread_mutex_lock");
6337 static const struct got_error *
6338 run_blame(struct tog_view *view)
6340 struct tog_blame_view_state *s = &view->state.blame;
6341 struct tog_blame *blame = &s->blame;
6342 const struct got_error *err = NULL;
6343 struct got_commit_object *commit = NULL;
6344 struct got_blob_object *blob = NULL;
6345 struct got_repository *thread_repo = NULL;
6346 struct got_object_id *obj_id = NULL;
6347 int obj_type, fd = -1;
6348 int *pack_fds = NULL;
6350 err = got_object_open_as_commit(&commit, s->repo,
6351 &s->blamed_commit->id);
6355 fd = got_opentempfd();
6357 err = got_error_from_errno("got_opentempfd");
6361 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6365 err = got_object_get_type(&obj_type, s->repo, obj_id);
6369 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6370 err = got_error(GOT_ERR_OBJ_TYPE);
6374 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6377 blame->f = got_opentemp();
6378 if (blame->f == NULL) {
6379 err = got_error_from_errno("got_opentemp");
6382 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6383 &blame->line_offsets, blame->f, blob);
6386 if (blame->nlines == 0) {
6387 s->blame_complete = 1;
6391 /* Don't include \n at EOF in the blame line count. */
6392 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6395 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6396 if (blame->lines == NULL) {
6397 err = got_error_from_errno("calloc");
6401 err = got_repo_pack_fds_open(&pack_fds);
6404 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6409 blame->pack_fds = pack_fds;
6410 blame->cb_args.view = view;
6411 blame->cb_args.lines = blame->lines;
6412 blame->cb_args.nlines = blame->nlines;
6413 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6414 if (blame->cb_args.commit_id == NULL) {
6415 err = got_error_from_errno("got_object_id_dup");
6418 blame->cb_args.quit = &s->done;
6420 blame->thread_args.path = s->path;
6421 blame->thread_args.repo = thread_repo;
6422 blame->thread_args.cb_args = &blame->cb_args;
6423 blame->thread_args.complete = &s->blame_complete;
6424 blame->thread_args.cancel_cb = cancel_blame_view;
6425 blame->thread_args.cancel_arg = &s->done;
6426 s->blame_complete = 0;
6428 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6429 s->first_displayed_line = 1;
6430 s->last_displayed_line = view->nlines;
6431 s->selected_line = 1;
6433 s->matched_line = 0;
6437 got_object_commit_close(commit);
6438 if (fd != -1 && close(fd) == -1 && err == NULL)
6439 err = got_error_from_errno("close");
6441 got_object_blob_close(blob);
6448 static const struct got_error *
6449 open_blame_view(struct tog_view *view, char *path,
6450 struct got_object_id *commit_id, struct got_repository *repo)
6452 const struct got_error *err = NULL;
6453 struct tog_blame_view_state *s = &view->state.blame;
6455 STAILQ_INIT(&s->blamed_commits);
6457 s->path = strdup(path);
6458 if (s->path == NULL)
6459 return got_error_from_errno("strdup");
6461 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6467 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6468 s->first_displayed_line = 1;
6469 s->last_displayed_line = view->nlines;
6470 s->selected_line = 1;
6471 s->blame_complete = 0;
6473 s->commit_id = commit_id;
6474 memset(&s->blame, 0, sizeof(s->blame));
6476 STAILQ_INIT(&s->colors);
6477 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6478 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6479 get_color_value("TOG_COLOR_COMMIT"));
6484 view->show = show_blame_view;
6485 view->input = input_blame_view;
6486 view->reset = reset_blame_view;
6487 view->close = close_blame_view;
6488 view->search_start = search_start_blame_view;
6489 view->search_setup = search_setup_blame_view;
6490 view->search_next = search_next_view_match;
6492 return run_blame(view);
6495 static const struct got_error *
6496 close_blame_view(struct tog_view *view)
6498 const struct got_error *err = NULL;
6499 struct tog_blame_view_state *s = &view->state.blame;
6501 if (s->blame.thread)
6502 err = stop_blame(&s->blame);
6504 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6505 struct got_object_qid *blamed_commit;
6506 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6507 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6508 got_object_qid_free(blamed_commit);
6512 free_colors(&s->colors);
6516 static const struct got_error *
6517 search_start_blame_view(struct tog_view *view)
6519 struct tog_blame_view_state *s = &view->state.blame;
6521 s->matched_line = 0;
6526 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6527 size_t *nlines, int **first, int **last, int **match, int **selected)
6529 struct tog_blame_view_state *s = &view->state.blame;
6532 *nlines = s->blame.nlines;
6533 *line_offsets = s->blame.line_offsets;
6534 *match = &s->matched_line;
6535 *first = &s->first_displayed_line;
6536 *last = &s->last_displayed_line;
6537 *selected = &s->selected_line;
6540 static const struct got_error *
6541 show_blame_view(struct tog_view *view)
6543 const struct got_error *err = NULL;
6544 struct tog_blame_view_state *s = &view->state.blame;
6547 if (s->blame.thread == NULL && !s->blame_complete) {
6548 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6549 &s->blame.thread_args);
6551 return got_error_set_errno(errcode, "pthread_create");
6554 halfdelay(1); /* fast refresh while annotating */
6557 if (s->blame_complete && !using_mock_io)
6558 halfdelay(10); /* disable fast refresh */
6560 err = draw_blame(view);
6566 static const struct got_error *
6567 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6568 struct got_repository *repo, struct got_object_id *id)
6570 struct tog_view *log_view;
6571 const struct got_error *err = NULL;
6575 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6576 if (log_view == NULL)
6577 return got_error_from_errno("view_open");
6579 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6581 view_close(log_view);
6583 *new_view = log_view;
6588 static const struct got_error *
6589 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6591 const struct got_error *err = NULL, *thread_err = NULL;
6592 struct tog_view *diff_view;
6593 struct tog_blame_view_state *s = &view->state.blame;
6594 int eos, nscroll, begin_y = 0, begin_x = 0;
6596 eos = nscroll = view->nlines - 2;
6597 if (view_is_hsplit_top(view))
6607 horizontal_scroll_input(view, ch);
6614 s->selected_line = 1;
6615 s->first_displayed_line = 1;
6620 if (s->blame.nlines < eos) {
6621 s->selected_line = s->blame.nlines;
6622 s->first_displayed_line = 1;
6624 s->selected_line = eos;
6625 s->first_displayed_line = s->blame.nlines - (eos - 1);
6632 if (s->selected_line > 1)
6634 else if (s->selected_line == 1 &&
6635 s->first_displayed_line > 1)
6636 s->first_displayed_line--;
6647 if (s->first_displayed_line == 1) {
6648 if (view->count > 1)
6650 s->selected_line = MAX(1, s->selected_line - nscroll);
6654 if (s->first_displayed_line > nscroll)
6655 s->first_displayed_line -= nscroll;
6657 s->first_displayed_line = 1;
6662 if (s->selected_line < eos && s->first_displayed_line +
6663 s->selected_line <= s->blame.nlines)
6665 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6666 s->first_displayed_line++;
6672 struct got_object_id *id = NULL;
6675 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6676 s->first_displayed_line, s->selected_line);
6680 struct got_commit_object *commit, *pcommit;
6681 struct got_object_qid *pid;
6682 struct got_object_id *blob_id = NULL;
6684 err = got_object_open_as_commit(&commit,
6689 got_object_commit_get_parent_ids(commit));
6691 got_object_commit_close(commit);
6694 /* Check if path history ends here. */
6695 err = got_object_open_as_commit(&pcommit,
6699 err = got_object_id_by_path(&blob_id, s->repo,
6701 got_object_commit_close(pcommit);
6703 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6705 got_object_commit_close(commit);
6708 err = got_object_get_type(&obj_type, s->repo,
6711 /* Can't blame non-blob type objects. */
6712 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6713 got_object_commit_close(commit);
6716 err = got_object_qid_alloc(&s->blamed_commit,
6718 got_object_commit_close(commit);
6720 if (got_object_id_cmp(id,
6721 &s->blamed_commit->id) == 0)
6723 err = got_object_qid_alloc(&s->blamed_commit,
6729 thread_err = stop_blame(&s->blame);
6733 STAILQ_INSERT_HEAD(&s->blamed_commits,
6734 s->blamed_commit, entry);
6735 err = run_blame(view);
6741 struct got_object_qid *first;
6744 first = STAILQ_FIRST(&s->blamed_commits);
6745 if (!got_object_id_cmp(&first->id, s->commit_id))
6748 thread_err = stop_blame(&s->blame);
6752 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6753 got_object_qid_free(s->blamed_commit);
6755 STAILQ_FIRST(&s->blamed_commits);
6756 err = run_blame(view);
6763 s->id_to_log = get_selected_commit_id(s->blame.lines,
6764 s->blame.nlines, s->first_displayed_line, s->selected_line);
6766 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6770 struct got_object_id *id = NULL;
6771 struct got_object_qid *pid;
6772 struct got_commit_object *commit = NULL;
6775 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6776 s->first_displayed_line, s->selected_line);
6779 err = got_object_open_as_commit(&commit, s->repo, id);
6782 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6784 /* traversed from diff view, release diff resources */
6785 err = close_diff_view(*new_view);
6788 diff_view = *new_view;
6790 if (view_is_parent_view(view))
6791 view_get_split(view, &begin_y, &begin_x);
6793 diff_view = view_open(0, 0, begin_y, begin_x,
6795 if (diff_view == NULL) {
6796 got_object_commit_close(commit);
6797 err = got_error_from_errno("view_open");
6801 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6802 id, NULL, NULL, 3, 0, 0, view, s->repo);
6803 got_object_commit_close(commit);
6805 view_close(diff_view);
6808 s->last_diffed_line = s->first_displayed_line - 1 +
6811 break; /* still open from active diff view */
6812 if (view_is_parent_view(view) &&
6813 view->mode == TOG_VIEW_SPLIT_HRZN) {
6814 err = view_init_hsplit(view, begin_y);
6820 diff_view->focussed = 1;
6821 diff_view->mode = view->mode;
6822 diff_view->nlines = view->lines - begin_y;
6823 if (view_is_parent_view(view)) {
6824 view_transfer_size(diff_view, view);
6825 err = view_close_child(view);
6828 err = view_set_child(view, diff_view);
6831 view->focus_child = 1;
6833 *new_view = diff_view;
6846 if (s->last_displayed_line >= s->blame.nlines &&
6847 s->selected_line >= MIN(s->blame.nlines,
6848 view->nlines - 2)) {
6852 if (s->last_displayed_line >= s->blame.nlines &&
6853 s->selected_line < view->nlines - 2) {
6855 MIN(nscroll, s->last_displayed_line -
6856 s->first_displayed_line - s->selected_line + 1);
6858 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6859 s->first_displayed_line += nscroll;
6861 s->first_displayed_line =
6862 s->blame.nlines - (view->nlines - 3);
6865 if (s->selected_line > view->nlines - 2) {
6866 s->selected_line = MIN(s->blame.nlines,
6874 return thread_err ? thread_err : err;
6877 static const struct got_error *
6878 reset_blame_view(struct tog_view *view)
6880 const struct got_error *err;
6881 struct tog_blame_view_state *s = &view->state.blame;
6885 err = stop_blame(&s->blame);
6889 return run_blame(view);
6892 static const struct got_error *
6893 cmd_blame(int argc, char *argv[])
6895 const struct got_error *error;
6896 struct got_repository *repo = NULL;
6897 struct got_worktree *worktree = NULL;
6898 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6899 char *link_target = NULL;
6900 struct got_object_id *commit_id = NULL;
6901 struct got_commit_object *commit = NULL;
6902 char *commit_id_str = NULL;
6904 struct tog_view *view = NULL;
6905 int *pack_fds = NULL;
6907 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6910 commit_id_str = optarg;
6913 repo_path = realpath(optarg, NULL);
6914 if (repo_path == NULL)
6915 return got_error_from_errno2("realpath",
6930 error = got_repo_pack_fds_open(&pack_fds);
6934 if (repo_path == NULL) {
6935 cwd = getcwd(NULL, 0);
6937 return got_error_from_errno("getcwd");
6938 error = got_worktree_open(&worktree, cwd);
6939 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6943 strdup(got_worktree_get_repo_path(worktree));
6945 repo_path = strdup(cwd);
6946 if (repo_path == NULL) {
6947 error = got_error_from_errno("strdup");
6952 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6956 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6963 error = apply_unveil(got_repo_get_path(repo), NULL);
6967 error = tog_load_refs(repo, 0);
6971 if (commit_id_str == NULL) {
6972 struct got_reference *head_ref;
6973 error = got_ref_open(&head_ref, repo, worktree ?
6974 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6977 error = got_ref_resolve(&commit_id, repo, head_ref);
6978 got_ref_close(head_ref);
6980 error = got_repo_match_object_id(&commit_id, NULL,
6981 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6986 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6988 error = got_error_from_errno("view_open");
6992 error = got_object_open_as_commit(&commit, repo, commit_id);
6996 error = got_object_resolve_symlinks(&link_target, in_repo_path,
7001 error = open_blame_view(view, link_target ? link_target : in_repo_path,
7006 /* Release work tree lock. */
7007 got_worktree_close(worktree);
7010 error = view_loop(view);
7017 if (error != NULL && view != NULL) {
7018 if (view->close == NULL)
7019 close_blame_view(view);
7023 got_object_commit_close(commit);
7025 got_worktree_close(worktree);
7027 const struct got_error *close_err = got_repo_close(repo);
7032 const struct got_error *pack_err =
7033 got_repo_pack_fds_close(pack_fds);
7041 static const struct got_error *
7042 draw_tree_entries(struct tog_view *view, const char *parent_path)
7044 struct tog_tree_view_state *s = &view->state.tree;
7045 const struct got_error *err = NULL;
7046 struct got_tree_entry *te;
7049 struct tog_color *tc;
7050 int width, n, nentries, scrollx, i = 1;
7051 int limit = view->nlines;
7054 if (view_is_hsplit_top(view))
7055 --limit; /* border */
7057 werase(view->window);
7062 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7066 if (view_needs_focus_indication(view))
7067 wstandout(view->window);
7068 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7070 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7071 waddwstr(view->window, wline);
7074 while (width++ < view->ncols)
7075 waddch(view->window, ' ');
7077 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7078 if (view_needs_focus_indication(view))
7079 wstandend(view->window);
7084 if (s->first_displayed_entry) {
7085 i += got_tree_entry_get_index(s->first_displayed_entry);
7086 if (s->tree != s->root)
7087 ++i; /* account for ".." entry */
7089 nentries = got_object_tree_get_nentries(s->tree);
7090 if (asprintf(&index, "[%d/%d] %s",
7091 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7092 return got_error_from_errno("asprintf");
7093 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7097 waddwstr(view->window, wline);
7100 if (width < view->ncols - 1)
7101 waddch(view->window, '\n');
7104 waddch(view->window, '\n');
7108 if (s->first_displayed_entry == NULL) {
7109 te = got_object_tree_get_first_entry(s->tree);
7110 if (s->selected == 0) {
7112 wstandout(view->window);
7113 s->selected_entry = NULL;
7115 waddstr(view->window, " ..\n"); /* parent directory */
7116 if (s->selected == 0 && view->focussed)
7117 wstandend(view->window);
7124 te = s->first_displayed_entry;
7128 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7129 char *line = NULL, *id_str = NULL, *link_target = NULL;
7130 const char *modestr = "";
7133 te = got_object_tree_get_entry(s->tree, i);
7134 mode = got_tree_entry_get_mode(te);
7137 err = got_object_id_str(&id_str,
7138 got_tree_entry_get_id(te));
7140 return got_error_from_errno(
7141 "got_object_id_str");
7143 if (got_object_tree_entry_is_submodule(te))
7145 else if (S_ISLNK(mode)) {
7148 err = got_tree_entry_get_symlink_target(&link_target,
7154 for (i = 0; i < strlen(link_target); i++) {
7155 if (!isprint((unsigned char)link_target[i]))
7156 link_target[i] = '?';
7160 else if (S_ISDIR(mode))
7162 else if (mode & S_IXUSR)
7164 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7165 got_tree_entry_get_name(te), modestr,
7166 link_target ? " -> ": "",
7167 link_target ? link_target : "") == -1) {
7170 return got_error_from_errno("asprintf");
7175 /* use full line width to determine view->maxx */
7176 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7181 view->maxx = MAX(view->maxx, width);
7185 err = format_line(&wline, &width, &scrollx, line, view->x,
7191 if (n == s->selected) {
7193 wstandout(view->window);
7194 s->selected_entry = te;
7196 tc = match_color(&s->colors, line);
7198 wattr_on(view->window,
7199 COLOR_PAIR(tc->colorpair), NULL);
7200 waddwstr(view->window, &wline[scrollx]);
7202 wattr_off(view->window,
7203 COLOR_PAIR(tc->colorpair), NULL);
7204 if (width < view->ncols)
7205 waddch(view->window, '\n');
7206 if (n == s->selected && view->focussed)
7207 wstandend(view->window);
7213 s->last_displayed_entry = te;
7222 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7224 struct got_tree_entry *te;
7225 int isroot = s->tree == s->root;
7228 if (s->first_displayed_entry == NULL)
7231 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7232 while (i++ < maxscroll) {
7235 s->first_displayed_entry = NULL;
7238 s->first_displayed_entry = te;
7239 te = got_tree_entry_get_prev(s->tree, te);
7243 static const struct got_error *
7244 tree_scroll_down(struct tog_view *view, int maxscroll)
7246 struct tog_tree_view_state *s = &view->state.tree;
7247 struct got_tree_entry *next, *last;
7250 if (s->first_displayed_entry)
7251 next = got_tree_entry_get_next(s->tree,
7252 s->first_displayed_entry);
7254 next = got_object_tree_get_first_entry(s->tree);
7256 last = s->last_displayed_entry;
7257 while (next && n++ < maxscroll) {
7259 s->last_displayed_entry = last;
7260 last = got_tree_entry_get_next(s->tree, last);
7262 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7263 s->first_displayed_entry = next;
7264 next = got_tree_entry_get_next(s->tree, next);
7271 static const struct got_error *
7272 tree_entry_path(char **path, struct tog_parent_trees *parents,
7273 struct got_tree_entry *te)
7275 const struct got_error *err = NULL;
7276 struct tog_parent_tree *pt;
7277 size_t len = 2; /* for leading slash and NUL */
7279 TAILQ_FOREACH(pt, parents, entry)
7280 len += strlen(got_tree_entry_get_name(pt->selected_entry))
7283 len += strlen(got_tree_entry_get_name(te));
7285 *path = calloc(1, len);
7287 return got_error_from_errno("calloc");
7290 pt = TAILQ_LAST(parents, tog_parent_trees);
7292 const char *name = got_tree_entry_get_name(pt->selected_entry);
7293 if (strlcat(*path, name, len) >= len) {
7294 err = got_error(GOT_ERR_NO_SPACE);
7297 if (strlcat(*path, "/", len) >= len) {
7298 err = got_error(GOT_ERR_NO_SPACE);
7301 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7304 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7305 err = got_error(GOT_ERR_NO_SPACE);
7317 static const struct got_error *
7318 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7319 struct got_tree_entry *te, struct tog_parent_trees *parents,
7320 struct got_object_id *commit_id, struct got_repository *repo)
7322 const struct got_error *err = NULL;
7324 struct tog_view *blame_view;
7328 err = tree_entry_path(&path, parents, te);
7332 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7333 if (blame_view == NULL) {
7334 err = got_error_from_errno("view_open");
7338 err = open_blame_view(blame_view, path, commit_id, repo);
7340 if (err->code == GOT_ERR_CANCELLED)
7342 view_close(blame_view);
7344 *new_view = blame_view;
7350 static const struct got_error *
7351 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7352 struct tog_tree_view_state *s)
7354 struct tog_view *log_view;
7355 const struct got_error *err = NULL;
7360 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7361 if (log_view == NULL)
7362 return got_error_from_errno("view_open");
7364 err = tree_entry_path(&path, &s->parents, s->selected_entry);
7368 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7371 view_close(log_view);
7373 *new_view = log_view;
7378 static const struct got_error *
7379 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7380 const char *head_ref_name, struct got_repository *repo)
7382 const struct got_error *err = NULL;
7383 char *commit_id_str = NULL;
7384 struct tog_tree_view_state *s = &view->state.tree;
7385 struct got_commit_object *commit = NULL;
7387 TAILQ_INIT(&s->parents);
7388 STAILQ_INIT(&s->colors);
7390 s->commit_id = got_object_id_dup(commit_id);
7391 if (s->commit_id == NULL) {
7392 err = got_error_from_errno("got_object_id_dup");
7396 err = got_object_open_as_commit(&commit, repo, commit_id);
7401 * The root is opened here and will be closed when the view is closed.
7402 * Any visited subtrees and their path-wise parents are opened and
7405 err = got_object_open_as_tree(&s->root, repo,
7406 got_object_commit_get_tree_id(commit));
7411 err = got_object_id_str(&commit_id_str, commit_id);
7415 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7416 err = got_error_from_errno("asprintf");
7420 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7421 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7422 if (head_ref_name) {
7423 s->head_ref_name = strdup(head_ref_name);
7424 if (s->head_ref_name == NULL) {
7425 err = got_error_from_errno("strdup");
7431 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7432 err = add_color(&s->colors, "\\$$",
7433 TOG_COLOR_TREE_SUBMODULE,
7434 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7437 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7438 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7441 err = add_color(&s->colors, "/$",
7442 TOG_COLOR_TREE_DIRECTORY,
7443 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7447 err = add_color(&s->colors, "\\*$",
7448 TOG_COLOR_TREE_EXECUTABLE,
7449 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7453 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7454 get_color_value("TOG_COLOR_COMMIT"));
7459 view->show = show_tree_view;
7460 view->input = input_tree_view;
7461 view->close = close_tree_view;
7462 view->search_start = search_start_tree_view;
7463 view->search_next = search_next_tree_view;
7465 free(commit_id_str);
7467 got_object_commit_close(commit);
7469 if (view->close == NULL)
7470 close_tree_view(view);
7476 static const struct got_error *
7477 close_tree_view(struct tog_view *view)
7479 struct tog_tree_view_state *s = &view->state.tree;
7481 free_colors(&s->colors);
7482 free(s->tree_label);
7483 s->tree_label = NULL;
7485 s->commit_id = NULL;
7486 free(s->head_ref_name);
7487 s->head_ref_name = NULL;
7488 while (!TAILQ_EMPTY(&s->parents)) {
7489 struct tog_parent_tree *parent;
7490 parent = TAILQ_FIRST(&s->parents);
7491 TAILQ_REMOVE(&s->parents, parent, entry);
7492 if (parent->tree != s->root)
7493 got_object_tree_close(parent->tree);
7497 if (s->tree != NULL && s->tree != s->root)
7498 got_object_tree_close(s->tree);
7500 got_object_tree_close(s->root);
7504 static const struct got_error *
7505 search_start_tree_view(struct tog_view *view)
7507 struct tog_tree_view_state *s = &view->state.tree;
7509 s->matched_entry = NULL;
7514 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7516 regmatch_t regmatch;
7518 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7522 static const struct got_error *
7523 search_next_tree_view(struct tog_view *view)
7525 struct tog_tree_view_state *s = &view->state.tree;
7526 struct got_tree_entry *te = NULL;
7528 if (!view->searching) {
7529 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7533 if (s->matched_entry) {
7534 if (view->searching == TOG_SEARCH_FORWARD) {
7535 if (s->selected_entry)
7536 te = got_tree_entry_get_next(s->tree,
7539 te = got_object_tree_get_first_entry(s->tree);
7541 if (s->selected_entry == NULL)
7542 te = got_object_tree_get_last_entry(s->tree);
7544 te = got_tree_entry_get_prev(s->tree,
7548 if (s->selected_entry)
7549 te = s->selected_entry;
7550 else if (view->searching == TOG_SEARCH_FORWARD)
7551 te = got_object_tree_get_first_entry(s->tree);
7553 te = got_object_tree_get_last_entry(s->tree);
7558 if (s->matched_entry == NULL) {
7559 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7562 if (view->searching == TOG_SEARCH_FORWARD)
7563 te = got_object_tree_get_first_entry(s->tree);
7565 te = got_object_tree_get_last_entry(s->tree);
7568 if (match_tree_entry(te, &view->regex)) {
7569 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7570 s->matched_entry = te;
7574 if (view->searching == TOG_SEARCH_FORWARD)
7575 te = got_tree_entry_get_next(s->tree, te);
7577 te = got_tree_entry_get_prev(s->tree, te);
7580 if (s->matched_entry) {
7581 s->first_displayed_entry = s->matched_entry;
7588 static const struct got_error *
7589 show_tree_view(struct tog_view *view)
7591 const struct got_error *err = NULL;
7592 struct tog_tree_view_state *s = &view->state.tree;
7595 err = tree_entry_path(&parent_path, &s->parents, NULL);
7599 err = draw_tree_entries(view, parent_path);
7606 static const struct got_error *
7607 tree_goto_line(struct tog_view *view, int nlines)
7609 const struct got_error *err = NULL;
7610 struct tog_tree_view_state *s = &view->state.tree;
7611 struct got_tree_entry **fte, **lte, **ste;
7612 int g, last, first = 1, i = 1;
7613 int root = s->tree == s->root;
7614 int off = root ? 1 : 2;
7621 else if (g > got_object_tree_get_nentries(s->tree))
7622 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7624 fte = &s->first_displayed_entry;
7625 lte = &s->last_displayed_entry;
7626 ste = &s->selected_entry;
7629 first = got_tree_entry_get_index(*fte);
7630 first += off; /* account for ".." */
7632 last = got_tree_entry_get_index(*lte);
7635 if (g >= first && g <= last && g - first < nlines) {
7636 s->selected = g - first;
7637 return NULL; /* gline is on the current page */
7641 i = got_tree_entry_get_index(*ste);
7646 err = tree_scroll_down(view, g - i);
7649 if (got_tree_entry_get_index(*lte) >=
7650 got_object_tree_get_nentries(s->tree) - 1 &&
7651 first + s->selected < g &&
7652 s->selected < s->ndisplayed - 1) {
7653 first = got_tree_entry_get_index(*fte);
7655 s->selected = g - first;
7658 tree_scroll_up(s, i - g);
7661 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7662 s->selected = g - 1;
7667 static const struct got_error *
7668 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7670 const struct got_error *err = NULL;
7671 struct tog_tree_view_state *s = &view->state.tree;
7672 struct got_tree_entry *te;
7673 int n, nscroll = view->nlines - 3;
7676 return tree_goto_line(view, nscroll);
7685 horizontal_scroll_input(view, ch);
7688 s->show_ids = !s->show_ids;
7693 if (!s->selected_entry)
7695 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7699 err = view_request_new(new_view, view, TOG_VIEW_REF);
7706 if (s->tree == s->root)
7707 s->first_displayed_entry =
7708 got_object_tree_get_first_entry(s->tree);
7710 s->first_displayed_entry = NULL;
7715 int eos = view->nlines - 3;
7717 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7721 te = got_object_tree_get_last_entry(s->tree);
7722 for (n = 0; n < eos; n++) {
7724 if (s->tree != s->root) {
7725 s->first_displayed_entry = NULL;
7730 s->first_displayed_entry = te;
7731 te = got_tree_entry_get_prev(s->tree, te);
7734 s->selected = n - 1;
7740 if (s->selected > 0) {
7744 tree_scroll_up(s, 1);
7745 if (s->selected_entry == NULL ||
7746 (s->tree == s->root && s->selected_entry ==
7747 got_object_tree_get_first_entry(s->tree)))
7757 if (s->tree == s->root) {
7758 if (got_object_tree_get_first_entry(s->tree) ==
7759 s->first_displayed_entry)
7760 s->selected -= MIN(s->selected, nscroll);
7762 if (s->first_displayed_entry == NULL)
7763 s->selected -= MIN(s->selected, nscroll);
7765 tree_scroll_up(s, MAX(0, nscroll));
7766 if (s->selected_entry == NULL ||
7767 (s->tree == s->root && s->selected_entry ==
7768 got_object_tree_get_first_entry(s->tree)))
7774 if (s->selected < s->ndisplayed - 1) {
7778 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7780 /* can't scroll any further */
7784 tree_scroll_down(view, 1);
7794 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7796 /* can't scroll any further; move cursor down */
7797 if (s->selected < s->ndisplayed - 1)
7798 s->selected += MIN(nscroll,
7799 s->ndisplayed - s->selected - 1);
7804 tree_scroll_down(view, nscroll);
7809 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7810 struct tog_parent_tree *parent;
7811 /* user selected '..' */
7812 if (s->tree == s->root) {
7816 parent = TAILQ_FIRST(&s->parents);
7817 TAILQ_REMOVE(&s->parents, parent,
7819 got_object_tree_close(s->tree);
7820 s->tree = parent->tree;
7821 s->first_displayed_entry =
7822 parent->first_displayed_entry;
7824 parent->selected_entry;
7825 s->selected = parent->selected;
7826 if (s->selected > view->nlines - 3) {
7827 err = offset_selection_down(view);
7832 } else if (S_ISDIR(got_tree_entry_get_mode(
7833 s->selected_entry))) {
7834 struct got_tree_object *subtree;
7836 err = got_object_open_as_tree(&subtree, s->repo,
7837 got_tree_entry_get_id(s->selected_entry));
7840 err = tree_view_visit_subtree(s, subtree);
7842 got_object_tree_close(subtree);
7845 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7846 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7849 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7850 s->selected = view->nlines - 4;
7866 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7871 static const struct got_error *
7872 cmd_tree(int argc, char *argv[])
7874 const struct got_error *error;
7875 struct got_repository *repo = NULL;
7876 struct got_worktree *worktree = NULL;
7877 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7878 struct got_object_id *commit_id = NULL;
7879 struct got_commit_object *commit = NULL;
7880 const char *commit_id_arg = NULL;
7882 struct got_reference *ref = NULL;
7883 const char *head_ref_name = NULL;
7885 struct tog_view *view;
7886 int *pack_fds = NULL;
7888 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7891 commit_id_arg = optarg;
7894 repo_path = realpath(optarg, NULL);
7895 if (repo_path == NULL)
7896 return got_error_from_errno2("realpath",
7911 error = got_repo_pack_fds_open(&pack_fds);
7915 if (repo_path == NULL) {
7916 cwd = getcwd(NULL, 0);
7918 return got_error_from_errno("getcwd");
7919 error = got_worktree_open(&worktree, cwd);
7920 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7924 strdup(got_worktree_get_repo_path(worktree));
7926 repo_path = strdup(cwd);
7927 if (repo_path == NULL) {
7928 error = got_error_from_errno("strdup");
7933 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7937 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7944 error = apply_unveil(got_repo_get_path(repo), NULL);
7948 error = tog_load_refs(repo, 0);
7952 if (commit_id_arg == NULL) {
7953 error = got_repo_match_object_id(&commit_id, &label,
7954 worktree ? got_worktree_get_head_ref_name(worktree) :
7955 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7958 head_ref_name = label;
7960 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7962 head_ref_name = got_ref_get_name(ref);
7963 else if (error->code != GOT_ERR_NOT_REF)
7965 error = got_repo_match_object_id(&commit_id, NULL,
7966 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7971 error = got_object_open_as_commit(&commit, repo, commit_id);
7975 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7977 error = got_error_from_errno("view_open");
7980 error = open_tree_view(view, commit_id, head_ref_name, repo);
7983 if (!got_path_is_root_dir(in_repo_path)) {
7984 error = tree_view_walk_path(&view->state.tree, commit,
7991 /* Release work tree lock. */
7992 got_worktree_close(worktree);
7995 error = view_loop(view);
8004 const struct got_error *close_err = got_repo_close(repo);
8009 const struct got_error *pack_err =
8010 got_repo_pack_fds_close(pack_fds);
8018 static const struct got_error *
8019 ref_view_load_refs(struct tog_ref_view_state *s)
8021 struct got_reflist_entry *sre;
8022 struct tog_reflist_entry *re;
8025 TAILQ_FOREACH(sre, &tog_refs, entry) {
8026 if (strncmp(got_ref_get_name(sre->ref),
8027 "refs/got/", 9) == 0 &&
8028 strncmp(got_ref_get_name(sre->ref),
8029 "refs/got/backup/", 16) != 0)
8032 re = malloc(sizeof(*re));
8034 return got_error_from_errno("malloc");
8036 re->ref = got_ref_dup(sre->ref);
8037 if (re->ref == NULL)
8038 return got_error_from_errno("got_ref_dup");
8039 re->idx = s->nrefs++;
8040 TAILQ_INSERT_TAIL(&s->refs, re, entry);
8043 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8048 ref_view_free_refs(struct tog_ref_view_state *s)
8050 struct tog_reflist_entry *re;
8052 while (!TAILQ_EMPTY(&s->refs)) {
8053 re = TAILQ_FIRST(&s->refs);
8054 TAILQ_REMOVE(&s->refs, re, entry);
8055 got_ref_close(re->ref);
8060 static const struct got_error *
8061 open_ref_view(struct tog_view *view, struct got_repository *repo)
8063 const struct got_error *err = NULL;
8064 struct tog_ref_view_state *s = &view->state.ref;
8066 s->selected_entry = 0;
8069 TAILQ_INIT(&s->refs);
8070 STAILQ_INIT(&s->colors);
8072 err = ref_view_load_refs(s);
8076 if (has_colors() && getenv("TOG_COLORS") != NULL) {
8077 err = add_color(&s->colors, "^refs/heads/",
8078 TOG_COLOR_REFS_HEADS,
8079 get_color_value("TOG_COLOR_REFS_HEADS"));
8083 err = add_color(&s->colors, "^refs/tags/",
8084 TOG_COLOR_REFS_TAGS,
8085 get_color_value("TOG_COLOR_REFS_TAGS"));
8089 err = add_color(&s->colors, "^refs/remotes/",
8090 TOG_COLOR_REFS_REMOTES,
8091 get_color_value("TOG_COLOR_REFS_REMOTES"));
8095 err = add_color(&s->colors, "^refs/got/backup/",
8096 TOG_COLOR_REFS_BACKUP,
8097 get_color_value("TOG_COLOR_REFS_BACKUP"));
8102 view->show = show_ref_view;
8103 view->input = input_ref_view;
8104 view->close = close_ref_view;
8105 view->search_start = search_start_ref_view;
8106 view->search_next = search_next_ref_view;
8109 if (view->close == NULL)
8110 close_ref_view(view);
8116 static const struct got_error *
8117 close_ref_view(struct tog_view *view)
8119 struct tog_ref_view_state *s = &view->state.ref;
8121 ref_view_free_refs(s);
8122 free_colors(&s->colors);
8127 static const struct got_error *
8128 resolve_reflist_entry(struct got_object_id **commit_id,
8129 struct tog_reflist_entry *re, struct got_repository *repo)
8131 const struct got_error *err = NULL;
8132 struct got_object_id *obj_id;
8133 struct got_tag_object *tag = NULL;
8138 err = got_ref_resolve(&obj_id, repo, re->ref);
8142 err = got_object_get_type(&obj_type, repo, obj_id);
8147 case GOT_OBJ_TYPE_COMMIT:
8148 *commit_id = obj_id;
8150 case GOT_OBJ_TYPE_TAG:
8151 err = got_object_open_as_tag(&tag, repo, obj_id);
8155 err = got_object_get_type(&obj_type, repo,
8156 got_object_tag_get_object_id(tag));
8159 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
8160 err = got_error(GOT_ERR_OBJ_TYPE);
8163 *commit_id = got_object_id_dup(
8164 got_object_tag_get_object_id(tag));
8165 if (*commit_id == NULL) {
8166 err = got_error_from_errno("got_object_id_dup");
8171 err = got_error(GOT_ERR_OBJ_TYPE);
8177 got_object_tag_close(tag);
8185 static const struct got_error *
8186 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
8187 struct tog_reflist_entry *re, struct got_repository *repo)
8189 struct tog_view *log_view;
8190 const struct got_error *err = NULL;
8191 struct got_object_id *commit_id = NULL;
8195 err = resolve_reflist_entry(&commit_id, re, repo);
8197 if (err->code != GOT_ERR_OBJ_TYPE)
8203 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
8204 if (log_view == NULL) {
8205 err = got_error_from_errno("view_open");
8209 err = open_log_view(log_view, commit_id, repo,
8210 got_ref_get_name(re->ref), "", 0);
8213 view_close(log_view);
8215 *new_view = log_view;
8221 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
8223 struct tog_reflist_entry *re;
8226 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8229 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
8230 while (i++ < maxscroll) {
8233 s->first_displayed_entry = re;
8234 re = TAILQ_PREV(re, tog_reflist_head, entry);
8238 static const struct got_error *
8239 ref_scroll_down(struct tog_view *view, int maxscroll)
8241 struct tog_ref_view_state *s = &view->state.ref;
8242 struct tog_reflist_entry *next, *last;
8245 if (s->first_displayed_entry)
8246 next = TAILQ_NEXT(s->first_displayed_entry, entry);
8248 next = TAILQ_FIRST(&s->refs);
8250 last = s->last_displayed_entry;
8251 while (next && n++ < maxscroll) {
8253 s->last_displayed_entry = last;
8254 last = TAILQ_NEXT(last, entry);
8256 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
8257 s->first_displayed_entry = next;
8258 next = TAILQ_NEXT(next, entry);
8265 static const struct got_error *
8266 search_start_ref_view(struct tog_view *view)
8268 struct tog_ref_view_state *s = &view->state.ref;
8270 s->matched_entry = NULL;
8275 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8277 regmatch_t regmatch;
8279 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8283 static const struct got_error *
8284 search_next_ref_view(struct tog_view *view)
8286 struct tog_ref_view_state *s = &view->state.ref;
8287 struct tog_reflist_entry *re = NULL;
8289 if (!view->searching) {
8290 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8294 if (s->matched_entry) {
8295 if (view->searching == TOG_SEARCH_FORWARD) {
8296 if (s->selected_entry)
8297 re = TAILQ_NEXT(s->selected_entry, entry);
8299 re = TAILQ_PREV(s->selected_entry,
8300 tog_reflist_head, entry);
8302 if (s->selected_entry == NULL)
8303 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8305 re = TAILQ_PREV(s->selected_entry,
8306 tog_reflist_head, entry);
8309 if (s->selected_entry)
8310 re = s->selected_entry;
8311 else if (view->searching == TOG_SEARCH_FORWARD)
8312 re = TAILQ_FIRST(&s->refs);
8314 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8319 if (s->matched_entry == NULL) {
8320 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8323 if (view->searching == TOG_SEARCH_FORWARD)
8324 re = TAILQ_FIRST(&s->refs);
8326 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8329 if (match_reflist_entry(re, &view->regex)) {
8330 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8331 s->matched_entry = re;
8335 if (view->searching == TOG_SEARCH_FORWARD)
8336 re = TAILQ_NEXT(re, entry);
8338 re = TAILQ_PREV(re, tog_reflist_head, entry);
8341 if (s->matched_entry) {
8342 s->first_displayed_entry = s->matched_entry;
8349 static const struct got_error *
8350 show_ref_view(struct tog_view *view)
8352 const struct got_error *err = NULL;
8353 struct tog_ref_view_state *s = &view->state.ref;
8354 struct tog_reflist_entry *re;
8357 struct tog_color *tc;
8358 int width, n, scrollx;
8359 int limit = view->nlines;
8361 werase(view->window);
8364 if (view_is_hsplit_top(view))
8365 --limit; /* border */
8370 re = s->first_displayed_entry;
8372 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8374 return got_error_from_errno("asprintf");
8376 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8381 if (view_needs_focus_indication(view))
8382 wstandout(view->window);
8383 waddwstr(view->window, wline);
8384 while (width++ < view->ncols)
8385 waddch(view->window, ' ');
8386 if (view_needs_focus_indication(view))
8387 wstandend(view->window);
8397 while (re && limit > 0) {
8399 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8402 struct got_commit_object *ci;
8403 struct got_tag_object *tag;
8404 struct got_object_id *id;
8408 err = got_ref_resolve(&id, s->repo, re->ref);
8411 err = got_object_open_as_tag(&tag, s->repo, id);
8413 if (err->code != GOT_ERR_OBJ_TYPE) {
8417 err = got_object_open_as_commit(&ci, s->repo,
8423 t = got_object_commit_get_committer_time(ci);
8424 got_object_commit_close(ci);
8426 t = got_object_tag_get_tagger_time(tag);
8427 got_object_tag_close(tag);
8430 if (gmtime_r(&t, &tm) == NULL)
8431 return got_error_from_errno("gmtime_r");
8432 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8433 return got_error(GOT_ERR_NO_SPACE);
8435 if (got_ref_is_symbolic(re->ref)) {
8436 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8437 ymd : "", got_ref_get_name(re->ref),
8438 got_ref_get_symref_target(re->ref)) == -1)
8439 return got_error_from_errno("asprintf");
8440 } else if (s->show_ids) {
8441 struct got_object_id *id;
8443 err = got_ref_resolve(&id, s->repo, re->ref);
8446 err = got_object_id_str(&id_str, id);
8451 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8452 got_ref_get_name(re->ref), id_str) == -1) {
8453 err = got_error_from_errno("asprintf");
8460 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8461 got_ref_get_name(re->ref)) == -1)
8462 return got_error_from_errno("asprintf");
8464 /* use full line width to determine view->maxx */
8465 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8470 view->maxx = MAX(view->maxx, width);
8474 err = format_line(&wline, &width, &scrollx, line, view->x,
8480 if (n == s->selected) {
8482 wstandout(view->window);
8483 s->selected_entry = re;
8485 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8487 wattr_on(view->window,
8488 COLOR_PAIR(tc->colorpair), NULL);
8489 waddwstr(view->window, &wline[scrollx]);
8491 wattr_off(view->window,
8492 COLOR_PAIR(tc->colorpair), NULL);
8493 if (width < view->ncols)
8494 waddch(view->window, '\n');
8495 if (n == s->selected && view->focussed)
8496 wstandend(view->window);
8502 s->last_displayed_entry = re;
8505 re = TAILQ_NEXT(re, entry);
8512 static const struct got_error *
8513 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8514 struct tog_reflist_entry *re, struct got_repository *repo)
8516 const struct got_error *err = NULL;
8517 struct got_object_id *commit_id = NULL;
8518 struct tog_view *tree_view;
8522 err = resolve_reflist_entry(&commit_id, re, repo);
8524 if (err->code != GOT_ERR_OBJ_TYPE)
8531 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8532 if (tree_view == NULL) {
8533 err = got_error_from_errno("view_open");
8537 err = open_tree_view(tree_view, commit_id,
8538 got_ref_get_name(re->ref), repo);
8542 *new_view = tree_view;
8548 static const struct got_error *
8549 ref_goto_line(struct tog_view *view, int nlines)
8551 const struct got_error *err = NULL;
8552 struct tog_ref_view_state *s = &view->state.ref;
8553 int g, idx = s->selected_entry->idx;
8560 else if (g > s->nrefs)
8563 if (g >= s->first_displayed_entry->idx + 1 &&
8564 g <= s->last_displayed_entry->idx + 1 &&
8565 g - s->first_displayed_entry->idx - 1 < nlines) {
8566 s->selected = g - s->first_displayed_entry->idx - 1;
8571 err = ref_scroll_down(view, g - idx - 1);
8574 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8575 s->first_displayed_entry->idx + s->selected < g &&
8576 s->selected < s->ndisplayed - 1)
8577 s->selected = g - s->first_displayed_entry->idx - 1;
8578 } else if (idx + 1 > g)
8579 ref_scroll_up(s, idx - g + 1);
8581 if (g < nlines && s->first_displayed_entry->idx == 0)
8582 s->selected = g - 1;
8588 static const struct got_error *
8589 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8591 const struct got_error *err = NULL;
8592 struct tog_ref_view_state *s = &view->state.ref;
8593 struct tog_reflist_entry *re;
8594 int n, nscroll = view->nlines - 1;
8597 return ref_goto_line(view, nscroll);
8606 horizontal_scroll_input(view, ch);
8609 s->show_ids = !s->show_ids;
8613 s->show_date = !s->show_date;
8617 s->sort_by_date = !s->sort_by_date;
8618 view->action = s->sort_by_date ? "sort by date" : "sort by name";
8620 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8621 got_ref_cmp_by_commit_timestamp_descending :
8622 tog_ref_cmp_by_name, s->repo);
8625 got_reflist_object_id_map_free(tog_refs_idmap);
8626 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8627 &tog_refs, s->repo);
8630 ref_view_free_refs(s);
8631 err = ref_view_load_refs(s);
8636 if (!s->selected_entry)
8638 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8642 if (!s->selected_entry)
8644 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8651 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8656 int eos = view->nlines - 1;
8658 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8662 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8663 for (n = 0; n < eos; n++) {
8666 s->first_displayed_entry = re;
8667 re = TAILQ_PREV(re, tog_reflist_head, entry);
8670 s->selected = n - 1;
8676 if (s->selected > 0) {
8680 ref_scroll_up(s, 1);
8681 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8691 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8692 s->selected -= MIN(nscroll, s->selected);
8693 ref_scroll_up(s, MAX(0, nscroll));
8694 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8700 if (s->selected < s->ndisplayed - 1) {
8704 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8705 /* can't scroll any further */
8709 ref_scroll_down(view, 1);
8719 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8720 /* can't scroll any further; move cursor down */
8721 if (s->selected < s->ndisplayed - 1)
8722 s->selected += MIN(nscroll,
8723 s->ndisplayed - s->selected - 1);
8724 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8725 s->selected += s->ndisplayed - s->selected - 1;
8729 ref_scroll_down(view, nscroll);
8734 err = tog_load_refs(s->repo, s->sort_by_date);
8737 ref_view_free_refs(s);
8738 err = ref_view_load_refs(s);
8741 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8742 s->selected = view->nlines - 2;
8756 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8761 static const struct got_error *
8762 cmd_ref(int argc, char *argv[])
8764 const struct got_error *error;
8765 struct got_repository *repo = NULL;
8766 struct got_worktree *worktree = NULL;
8767 char *cwd = NULL, *repo_path = NULL;
8769 struct tog_view *view;
8770 int *pack_fds = NULL;
8772 while ((ch = getopt(argc, argv, "r:")) != -1) {
8775 repo_path = realpath(optarg, NULL);
8776 if (repo_path == NULL)
8777 return got_error_from_errno2("realpath",
8792 error = got_repo_pack_fds_open(&pack_fds);
8796 if (repo_path == NULL) {
8797 cwd = getcwd(NULL, 0);
8799 return got_error_from_errno("getcwd");
8800 error = got_worktree_open(&worktree, cwd);
8801 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8805 strdup(got_worktree_get_repo_path(worktree));
8807 repo_path = strdup(cwd);
8808 if (repo_path == NULL) {
8809 error = got_error_from_errno("strdup");
8814 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8820 error = apply_unveil(got_repo_get_path(repo), NULL);
8824 error = tog_load_refs(repo, 0);
8828 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8830 error = got_error_from_errno("view_open");
8834 error = open_ref_view(view, repo);
8839 /* Release work tree lock. */
8840 got_worktree_close(worktree);
8843 error = view_loop(view);
8848 const struct got_error *close_err = got_repo_close(repo);
8853 const struct got_error *pack_err =
8854 got_repo_pack_fds_close(pack_fds);
8862 static const struct got_error*
8863 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8872 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8876 if (mvwprintw(win, y, x, "%s", str) == ERR)
8877 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8884 static const struct got_error *
8885 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8889 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8891 free(*line_offsets);
8892 *line_offsets = NULL;
8893 return got_error_from_errno("reallocarray");
8897 (*line_offsets)[*nlines] = off;
8902 static const struct got_error *
8903 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8907 for (;n > 0; --n, ++km) {
8911 if (km->keys == NULL)
8914 t = t0 = strdup(km->keys);
8916 return got_error_from_errno("strdup");
8919 while ((k = strsep(&t, " ")) != NULL)
8920 len += strlen(k) > 1 ? 2 : 0;
8922 *ret = MAX(*ret, len);
8929 * Write keymap section headers, keys, and key info in km to f.
8930 * Save line offset to *off. If terminal has UTF8 encoding enabled,
8931 * wrap control and symbolic keys in guillemets, else use <>.
8933 static const struct got_error *
8934 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8939 static const char *u8_glyph[] = {
8940 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8941 "\xe2\x80\xba" /* U+203A (utf8 >) */
8944 int cs, s, first = 1;
8946 cs = got_locale_is_utf8();
8948 t = t0 = strdup(km->keys);
8950 return got_error_from_errno("strdup");
8952 len = strlen(km->keys);
8953 while ((k = strsep(&t, " ")) != NULL) {
8954 s = strlen(k) > 1; /* control or symbolic key */
8955 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8956 cs && s ? u8_glyph[0] : s ? "<" : "", k,
8957 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8960 return got_error_from_errno("fprintf");
8968 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8970 return got_error_from_errno("fprintf");
8976 static const struct got_error *
8977 format_help(struct tog_help_view_state *s)
8979 const struct got_error *err = NULL;
8981 int i, max, n, show = s->all;
8982 static const struct tog_key_map km[] = {
8983 #define KEYMAP_(info, type) { NULL, (info), type }
8984 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8990 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8995 err = max_key_str(&max, km, n);
8999 for (i = 0; i < n; ++i) {
9000 if (km[i].keys == NULL) {
9002 if (km[i].type == TOG_KEYMAP_GLOBAL ||
9003 km[i].type == s->type || s->all)
9007 err = format_help_line(&off, s->f, &km[i], max);
9010 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9017 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9021 static const struct got_error *
9022 create_help(struct tog_help_view_state *s)
9025 const struct got_error *err;
9027 free(s->line_offsets);
9028 s->line_offsets = NULL;
9033 return got_error_from_errno("got_opentemp");
9036 err = format_help(s);
9040 if (s->f && fflush(s->f) != 0)
9041 return got_error_from_errno("fflush");
9046 static const struct got_error *
9047 search_start_help_view(struct tog_view *view)
9049 view->state.help.matched_line = 0;
9054 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
9055 size_t *nlines, int **first, int **last, int **match, int **selected)
9057 struct tog_help_view_state *s = &view->state.help;
9060 *nlines = s->nlines;
9061 *line_offsets = s->line_offsets;
9062 *match = &s->matched_line;
9063 *first = &s->first_displayed_line;
9064 *last = &s->last_displayed_line;
9065 *selected = &s->selected_line;
9068 static const struct got_error *
9069 show_help_view(struct tog_view *view)
9071 struct tog_help_view_state *s = &view->state.help;
9072 const struct got_error *err;
9073 regmatch_t *regmatch = &view->regmatch;
9078 int width, nprinted = 0, rc = 0;
9079 int eos = view->nlines;
9081 if (view_is_hsplit_top(view))
9082 --eos; /* account for border */
9086 werase(view->window);
9088 if (view->gline > s->nlines - 1)
9089 view->gline = s->nlines - 1;
9091 err = win_draw_center(view->window, 0, 0, view->ncols,
9092 view_needs_focus_indication(view),
9093 "tog help (press q to return to tog)");
9098 waddstr(view->window, "\n\n");
9104 while (eos > 0 && nprinted < eos) {
9107 linelen = getline(&line, &linesz, s->f);
9108 if (linelen == -1) {
9111 return got_ferror(s->f, GOT_ERR_IO);
9116 if (++s->lineno < s->first_displayed_line)
9118 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
9120 if (s->lineno == view->hiline)
9123 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
9129 view->maxx = MAX(view->maxx, width);
9134 wattron(view->window, attr);
9135 if (s->first_displayed_line + nprinted == s->matched_line &&
9136 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
9137 err = add_matched_line(&width, line, view->ncols - 1, 0,
9138 view->window, view->x, regmatch);
9146 err = format_line(&wline, &width, &skip, line,
9147 view->x, view->ncols, 0, view->x ? 1 : 0);
9152 waddwstr(view->window, &wline[skip]);
9156 if (s->lineno == view->hiline) {
9157 while (width++ < view->ncols)
9158 waddch(view->window, ' ');
9160 if (width < view->ncols)
9161 waddch(view->window, '\n');
9164 wattroff(view->window, attr);
9165 if (++nprinted == 1)
9166 s->first_displayed_line = s->lineno;
9170 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
9172 s->last_displayed_line = s->first_displayed_line;
9177 rc = waddnstr(view->window,
9178 "See the tog(1) manual page for full documentation",
9181 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9183 wmove(view->window, view->nlines - 1, 0);
9184 wclrtoeol(view->window);
9185 wstandout(view->window);
9186 rc = waddnstr(view->window, "scroll down for more...",
9189 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9190 if (getcurx(view->window) < view->ncols - 6) {
9191 rc = wprintw(view->window, "[%.0f%%]",
9192 100.00 * s->last_displayed_line / s->nlines);
9194 return got_error_msg(GOT_ERR_IO, "wprintw");
9196 wstandend(view->window);
9202 static const struct got_error *
9203 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
9205 struct tog_help_view_state *s = &view->state.help;
9206 const struct got_error *err = NULL;
9212 eos = nscroll = view->nlines;
9213 if (view_is_hsplit_top(view))
9216 s->lineno = s->first_displayed_line - 1 + s->selected_line;
9225 horizontal_scroll_input(view, ch);
9229 s->first_displayed_line = 1;
9237 s->first_displayed_line = (s->nlines - eos) + 3;
9242 if (s->first_displayed_line > 1)
9243 --s->first_displayed_line;
9254 if (s->first_displayed_line == 1) {
9258 while (--nscroll > 0 && s->first_displayed_line > 1)
9259 s->first_displayed_line--;
9265 ++s->first_displayed_line;
9281 while (!s->eof && --nscroll > 0) {
9282 linelen = getline(&line, &linesz, s->f);
9283 s->first_displayed_line++;
9284 if (linelen == -1) {
9288 err = got_ferror(s->f, GOT_ERR_IO);
9302 static const struct got_error *
9303 close_help_view(struct tog_view *view)
9305 struct tog_help_view_state *s = &view->state.help;
9307 free(s->line_offsets);
9308 s->line_offsets = NULL;
9309 if (fclose(s->f) == EOF)
9310 return got_error_from_errno("fclose");
9315 static const struct got_error *
9316 reset_help_view(struct tog_view *view)
9318 struct tog_help_view_state *s = &view->state.help;
9321 if (s->f && fclose(s->f) == EOF)
9322 return got_error_from_errno("fclose");
9324 wclear(view->window);
9328 s->first_displayed_line = 1;
9329 s->last_displayed_line = view->nlines;
9330 s->matched_line = 0;
9332 return create_help(s);
9335 static const struct got_error *
9336 open_help_view(struct tog_view *view, struct tog_view *parent)
9338 const struct got_error *err = NULL;
9339 struct tog_help_view_state *s = &view->state.help;
9341 s->type = (enum tog_keymap_type)parent->type;
9342 s->first_displayed_line = 1;
9343 s->last_displayed_line = view->nlines;
9344 s->selected_line = 1;
9346 view->show = show_help_view;
9347 view->input = input_help_view;
9348 view->reset = reset_help_view;
9349 view->close = close_help_view;
9350 view->search_start = search_start_help_view;
9351 view->search_setup = search_setup_help_view;
9352 view->search_next = search_next_view_match;
9354 err = create_help(s);
9358 static const struct got_error *
9359 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9360 enum tog_view_type request, int y, int x)
9362 const struct got_error *err = NULL;
9368 if (view->type == TOG_VIEW_LOG) {
9369 struct tog_log_view_state *s = &view->state.log;
9371 err = open_diff_view_for_commit(new_view, y, x,
9372 s->selected_entry->commit, s->selected_entry->id,
9375 return got_error_msg(GOT_ERR_NOT_IMPL,
9376 "parent/child view pair not supported");
9378 case TOG_VIEW_BLAME:
9379 if (view->type == TOG_VIEW_TREE) {
9380 struct tog_tree_view_state *s = &view->state.tree;
9382 err = blame_tree_entry(new_view, y, x,
9383 s->selected_entry, &s->parents, s->commit_id,
9386 return got_error_msg(GOT_ERR_NOT_IMPL,
9387 "parent/child view pair not supported");
9390 if (view->type == TOG_VIEW_BLAME)
9391 err = log_annotated_line(new_view, y, x,
9392 view->state.blame.repo, view->state.blame.id_to_log);
9393 else if (view->type == TOG_VIEW_TREE)
9394 err = log_selected_tree_entry(new_view, y, x,
9396 else if (view->type == TOG_VIEW_REF)
9397 err = log_ref_entry(new_view, y, x,
9398 view->state.ref.selected_entry,
9399 view->state.ref.repo);
9401 return got_error_msg(GOT_ERR_NOT_IMPL,
9402 "parent/child view pair not supported");
9405 if (view->type == TOG_VIEW_LOG)
9406 err = browse_commit_tree(new_view, y, x,
9407 view->state.log.selected_entry,
9408 view->state.log.in_repo_path,
9409 view->state.log.head_ref_name,
9410 view->state.log.repo);
9411 else if (view->type == TOG_VIEW_REF)
9412 err = browse_ref_tree(new_view, y, x,
9413 view->state.ref.selected_entry,
9414 view->state.ref.repo);
9416 return got_error_msg(GOT_ERR_NOT_IMPL,
9417 "parent/child view pair not supported");
9420 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9421 if (*new_view == NULL)
9422 return got_error_from_errno("view_open");
9423 if (view->type == TOG_VIEW_LOG)
9424 err = open_ref_view(*new_view, view->state.log.repo);
9425 else if (view->type == TOG_VIEW_TREE)
9426 err = open_ref_view(*new_view, view->state.tree.repo);
9428 err = got_error_msg(GOT_ERR_NOT_IMPL,
9429 "parent/child view pair not supported");
9431 view_close(*new_view);
9434 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9435 if (*new_view == NULL)
9436 return got_error_from_errno("view_open");
9437 err = open_help_view(*new_view, view);
9439 view_close(*new_view);
9442 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9449 * If view was scrolled down to move the selected line into view when opening a
9450 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9453 offset_selection_up(struct tog_view *view)
9455 switch (view->type) {
9456 case TOG_VIEW_BLAME: {
9457 struct tog_blame_view_state *s = &view->state.blame;
9458 if (s->first_displayed_line == 1) {
9459 s->selected_line = MAX(s->selected_line - view->offset,
9463 if (s->first_displayed_line > view->offset)
9464 s->first_displayed_line -= view->offset;
9466 s->first_displayed_line = 1;
9467 s->selected_line += view->offset;
9471 log_scroll_up(&view->state.log, view->offset);
9472 view->state.log.selected += view->offset;
9475 ref_scroll_up(&view->state.ref, view->offset);
9476 view->state.ref.selected += view->offset;
9479 tree_scroll_up(&view->state.tree, view->offset);
9480 view->state.tree.selected += view->offset;
9490 * If the selected line is in the section of screen covered by the bottom split,
9491 * scroll down offset lines to move it into view and index its new position.
9493 static const struct got_error *
9494 offset_selection_down(struct tog_view *view)
9496 const struct got_error *err = NULL;
9497 const struct got_error *(*scrolld)(struct tog_view *, int);
9498 int *selected = NULL;
9501 switch (view->type) {
9502 case TOG_VIEW_BLAME: {
9503 struct tog_blame_view_state *s = &view->state.blame;
9506 if (s->selected_line > view->nlines - header) {
9507 offset = abs(view->nlines - s->selected_line - header);
9508 s->first_displayed_line += offset;
9509 s->selected_line -= offset;
9510 view->offset = offset;
9514 case TOG_VIEW_LOG: {
9515 struct tog_log_view_state *s = &view->state.log;
9516 scrolld = &log_scroll_down;
9517 header = view_is_parent_view(view) ? 3 : 2;
9518 selected = &s->selected;
9521 case TOG_VIEW_REF: {
9522 struct tog_ref_view_state *s = &view->state.ref;
9523 scrolld = &ref_scroll_down;
9525 selected = &s->selected;
9528 case TOG_VIEW_TREE: {
9529 struct tog_tree_view_state *s = &view->state.tree;
9530 scrolld = &tree_scroll_down;
9532 selected = &s->selected;
9542 if (selected && *selected > view->nlines - header) {
9543 offset = abs(view->nlines - *selected - header);
9544 view->offset = offset;
9545 if (scrolld && offset) {
9546 err = scrolld(view, offset);
9547 *selected -= offset;
9555 list_commands(FILE *fp)
9559 fprintf(fp, "commands:");
9560 for (i = 0; i < nitems(tog_commands); i++) {
9561 const struct tog_cmd *cmd = &tog_commands[i];
9562 fprintf(fp, " %s", cmd->name);
9568 usage(int hflag, int status)
9570 FILE *fp = (status == 0) ? stdout : stderr;
9572 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9575 fprintf(fp, "lazy usage: %s path\n", getprogname());
9582 make_argv(int argc, ...)
9590 argv = calloc(argc, sizeof(char *));
9593 for (i = 0; i < argc; i++) {
9594 argv[i] = strdup(va_arg(ap, char *));
9595 if (argv[i] == NULL)
9604 * Try to convert 'tog path' into a 'tog log path' command.
9605 * The user could simply have mistyped the command rather than knowingly
9606 * provided a path. So check whether argv[0] can in fact be resolved
9607 * to a path in the HEAD commit and print a special error if not.
9608 * This hack is for mpi@ <3
9610 static const struct got_error *
9611 tog_log_with_path(int argc, char *argv[])
9613 const struct got_error *error = NULL, *close_err;
9614 const struct tog_cmd *cmd = NULL;
9615 struct got_repository *repo = NULL;
9616 struct got_worktree *worktree = NULL;
9617 struct got_object_id *commit_id = NULL, *id = NULL;
9618 struct got_commit_object *commit = NULL;
9619 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9620 char *commit_id_str = NULL, **cmd_argv = NULL;
9621 int *pack_fds = NULL;
9623 cwd = getcwd(NULL, 0);
9625 return got_error_from_errno("getcwd");
9627 error = got_repo_pack_fds_open(&pack_fds);
9631 error = got_worktree_open(&worktree, cwd);
9632 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9636 repo_path = strdup(got_worktree_get_repo_path(worktree));
9638 repo_path = strdup(cwd);
9639 if (repo_path == NULL) {
9640 error = got_error_from_errno("strdup");
9644 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9648 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9653 error = tog_load_refs(repo, 0);
9656 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9657 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9658 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9663 got_worktree_close(worktree);
9667 error = got_object_open_as_commit(&commit, repo, commit_id);
9671 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9673 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9675 fprintf(stderr, "%s: '%s' is no known command or path\n",
9676 getprogname(), argv[0]);
9681 error = got_object_id_str(&commit_id_str, commit_id);
9685 cmd = &tog_commands[0]; /* log */
9687 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9688 error = cmd->cmd_main(argc, cmd_argv);
9691 close_err = got_repo_close(repo);
9696 got_object_commit_close(commit);
9698 got_worktree_close(worktree);
9700 const struct got_error *pack_err =
9701 got_repo_pack_fds_close(pack_fds);
9706 free(commit_id_str);
9713 for (i = 0; i < argc; i++)
9722 main(int argc, char *argv[])
9724 const struct got_error *io_err, *error = NULL;
9725 const struct tog_cmd *cmd = NULL;
9726 int ch, hflag = 0, Vflag = 0;
9727 char **cmd_argv = NULL;
9728 static const struct option longopts[] = {
9729 { "version", no_argument, NULL, 'V' },
9732 char *diff_algo_str = NULL;
9733 const char *test_script_path;
9735 setlocale(LC_CTYPE, "");
9738 * Test mode init must happen before pledge() because "tty" will
9739 * not allow TTY-related ioctls to occur via regular files.
9741 test_script_path = getenv("GOT_TOG_TEST");
9742 if (test_script_path != NULL) {
9743 error = init_mock_term(test_script_path);
9745 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9750 #if !defined(PROFILE)
9751 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9756 if (!isatty(STDIN_FILENO))
9757 errx(1, "standard input is not a tty");
9759 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9779 got_version_print_str();
9786 /* Build an argument vector which runs a default command. */
9787 cmd = &tog_commands[0];
9789 cmd_argv = make_argv(argc, cmd->name);
9793 /* Did the user specify a command? */
9794 for (i = 0; i < nitems(tog_commands); i++) {
9795 if (strncmp(tog_commands[i].name, argv[0],
9796 strlen(argv[0])) == 0) {
9797 cmd = &tog_commands[i];
9803 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9804 if (diff_algo_str) {
9805 if (strcasecmp(diff_algo_str, "patience") == 0)
9806 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9807 if (strcasecmp(diff_algo_str, "myers") == 0)
9808 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9814 /* No command specified; try log with a path */
9815 error = tog_log_with_path(argc, argv);
9820 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9823 if (using_mock_io) {
9824 io_err = tog_io_close();
9831 for (i = 0; i < argc; i++)
9836 if (error && error->code != GOT_ERR_CANCELLED &&
9837 error->code != GOT_ERR_EOF &&
9838 error->code != GOT_ERR_PRIVSEP_EXIT &&
9839 error->code != GOT_ERR_PRIVSEP_PIPE &&
9840 !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9841 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);