2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
45 #include "got_version.h"
46 #include "got_error.h"
47 #include "got_object.h"
48 #include "got_reference.h"
49 #include "got_repository.h"
51 #include "got_opentemp.h"
53 #include "got_cancel.h"
54 #include "got_commit_graph.h"
55 #include "got_blame.h"
56 #include "got_privsep.h"
58 #include "got_worktree.h"
59 #include "got_keyword.h"
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
66 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
69 #define CTRL(x) ((x) & 0x1f)
72 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
77 const struct got_error *(*cmd_main)(int, char *[]);
78 void (*cmd_usage)(void);
81 __dead static void usage(int, int);
82 __dead static void usage_log(void);
83 __dead static void usage_diff(void);
84 __dead static void usage_blame(void);
85 __dead static void usage_tree(void);
86 __dead static void usage_ref(void);
88 static const struct got_error* cmd_log(int, char *[]);
89 static const struct got_error* cmd_diff(int, char *[]);
90 static const struct got_error* cmd_blame(int, char *[]);
91 static const struct got_error* cmd_tree(int, char *[]);
92 static const struct got_error* cmd_ref(int, char *[]);
94 static const struct tog_cmd tog_commands[] = {
95 { "log", cmd_log, usage_log },
96 { "diff", cmd_diff, usage_diff },
97 { "blame", cmd_blame, usage_blame },
98 { "tree", cmd_tree, usage_tree },
99 { "ref", cmd_ref, usage_ref },
111 /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
112 enum tog_keymap_type {
113 TOG_KEYMAP_KEYS = -2,
129 #define HSPLIT_SCALE 0.3f /* default horizontal split scale */
131 #define TOG_EOF_STRING "(END)"
133 struct commit_queue_entry {
134 TAILQ_ENTRY(commit_queue_entry) entry;
135 struct got_object_id *id;
136 struct got_commit_object *commit;
139 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
140 struct commit_queue {
142 struct commit_queue_head head;
146 STAILQ_ENTRY(tog_color) entry;
150 STAILQ_HEAD(tog_colors, tog_color);
152 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
153 static struct got_reflist_object_id_map *tog_refs_idmap;
154 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
156 static const struct got_error *
157 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
158 struct got_reference* re2)
160 const char *name1 = got_ref_get_name(re1);
161 const char *name2 = got_ref_get_name(re2);
162 int isbackup1, isbackup2;
164 /* Sort backup refs towards the bottom of the list. */
165 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
166 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
167 if (!isbackup1 && isbackup2) {
170 } else if (isbackup1 && !isbackup2) {
175 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
179 static const struct got_error *
180 tog_load_refs(struct got_repository *repo, int sort_by_date)
182 const struct got_error *err;
184 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
185 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
190 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
197 if (tog_refs_idmap) {
198 got_reflist_object_id_map_free(tog_refs_idmap);
199 tog_refs_idmap = NULL;
201 got_ref_list_free(&tog_refs);
204 static const struct got_error *
205 add_color(struct tog_colors *colors, const char *pattern,
206 int idx, short color)
208 const struct got_error *err = NULL;
209 struct tog_color *tc;
212 if (idx < 1 || idx > COLOR_PAIRS - 1)
215 init_pair(idx, color, -1);
217 tc = calloc(1, sizeof(*tc));
219 return got_error_from_errno("calloc");
220 regerr = regcomp(&tc->regex, pattern,
221 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
223 static char regerr_msg[512];
224 static char err_msg[512];
225 regerror(regerr, &tc->regex, regerr_msg,
227 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
229 err = got_error_msg(GOT_ERR_REGEX, err_msg);
234 STAILQ_INSERT_HEAD(colors, tc, entry);
239 free_colors(struct tog_colors *colors)
241 struct tog_color *tc;
243 while (!STAILQ_EMPTY(colors)) {
244 tc = STAILQ_FIRST(colors);
245 STAILQ_REMOVE_HEAD(colors, entry);
251 static struct tog_color *
252 get_color(struct tog_colors *colors, int colorpair)
254 struct tog_color *tc = NULL;
256 STAILQ_FOREACH(tc, colors, entry) {
257 if (tc->colorpair == colorpair)
265 default_color_value(const char *envvar)
267 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
268 return COLOR_MAGENTA;
269 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
271 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
273 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
275 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
276 return COLOR_MAGENTA;
277 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
278 return COLOR_MAGENTA;
279 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
281 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
283 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
285 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
287 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
289 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
291 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
292 return COLOR_MAGENTA;
293 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
295 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
302 get_color_value(const char *envvar)
304 const char *val = getenv(envvar);
307 return default_color_value(envvar);
309 if (strcasecmp(val, "black") == 0)
311 if (strcasecmp(val, "red") == 0)
313 if (strcasecmp(val, "green") == 0)
315 if (strcasecmp(val, "yellow") == 0)
317 if (strcasecmp(val, "blue") == 0)
319 if (strcasecmp(val, "magenta") == 0)
320 return COLOR_MAGENTA;
321 if (strcasecmp(val, "cyan") == 0)
323 if (strcasecmp(val, "white") == 0)
325 if (strcasecmp(val, "default") == 0)
328 return default_color_value(envvar);
331 struct tog_diff_view_state {
332 struct got_object_id *id1, *id2;
333 const char *label1, *label2;
337 int first_displayed_line;
338 int last_displayed_line;
341 int ignore_whitespace;
343 struct got_repository *repo;
344 struct got_diff_line *lines;
349 /* passed from log or blame view; may be NULL */
350 struct tog_view *parent_view;
353 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
354 static volatile sig_atomic_t tog_thread_error;
356 struct tog_log_thread_args {
357 pthread_cond_t need_commits;
358 pthread_cond_t commit_loaded;
361 struct got_commit_graph *graph;
362 struct commit_queue *real_commits;
363 const char *in_repo_path;
364 struct got_object_id *start_id;
365 struct got_repository *repo;
369 struct commit_queue_entry **first_displayed_entry;
370 struct commit_queue_entry **selected_entry;
372 int *search_next_done;
376 regex_t *limit_regex;
377 struct commit_queue *limit_commits;
380 struct tog_log_view_state {
381 struct commit_queue *commits;
382 struct commit_queue_entry *first_displayed_entry;
383 struct commit_queue_entry *last_displayed_entry;
384 struct commit_queue_entry *selected_entry;
385 struct commit_queue real_commits;
390 struct got_repository *repo;
391 struct got_object_id *start_id;
394 struct tog_log_thread_args thread_args;
395 struct commit_queue_entry *matched_entry;
396 struct commit_queue_entry *search_entry;
397 struct tog_colors colors;
401 struct commit_queue limit_commits;
404 #define TOG_COLOR_DIFF_MINUS 1
405 #define TOG_COLOR_DIFF_PLUS 2
406 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
407 #define TOG_COLOR_DIFF_META 4
408 #define TOG_COLOR_TREE_SUBMODULE 5
409 #define TOG_COLOR_TREE_SYMLINK 6
410 #define TOG_COLOR_TREE_DIRECTORY 7
411 #define TOG_COLOR_TREE_EXECUTABLE 8
412 #define TOG_COLOR_COMMIT 9
413 #define TOG_COLOR_AUTHOR 10
414 #define TOG_COLOR_DATE 11
415 #define TOG_COLOR_REFS_HEADS 12
416 #define TOG_COLOR_REFS_TAGS 13
417 #define TOG_COLOR_REFS_REMOTES 14
418 #define TOG_COLOR_REFS_BACKUP 15
420 struct tog_blame_cb_args {
421 struct tog_blame_line *lines; /* one per line */
424 struct tog_view *view;
425 struct got_object_id *commit_id;
429 struct tog_blame_thread_args {
431 struct got_repository *repo;
432 struct tog_blame_cb_args *cb_args;
434 got_cancel_cb cancel_cb;
436 pthread_cond_t blame_complete;
442 struct tog_blame_line *lines;
446 struct tog_blame_thread_args thread_args;
447 struct tog_blame_cb_args cb_args;
452 struct tog_blame_view_state {
453 int first_displayed_line;
454 int last_displayed_line;
456 int last_diffed_line;
460 struct got_object_id_queue blamed_commits;
461 struct got_object_qid *blamed_commit;
463 struct got_repository *repo;
464 struct got_object_id *commit_id;
465 struct got_object_id *id_to_log;
466 struct tog_blame blame;
468 struct tog_colors colors;
471 struct tog_parent_tree {
472 TAILQ_ENTRY(tog_parent_tree) entry;
473 struct got_tree_object *tree;
474 struct got_tree_entry *first_displayed_entry;
475 struct got_tree_entry *selected_entry;
479 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
481 struct tog_tree_view_state {
483 struct got_object_id *commit_id;/* commit which this tree belongs to */
484 struct got_tree_object *root; /* the commit's root tree entry */
485 struct got_tree_object *tree; /* currently displayed (sub-)tree */
486 struct got_tree_entry *first_displayed_entry;
487 struct got_tree_entry *last_displayed_entry;
488 struct got_tree_entry *selected_entry;
489 int ndisplayed, selected, show_ids;
490 struct tog_parent_trees parents; /* parent trees of current sub-tree */
492 struct got_repository *repo;
493 struct got_tree_entry *matched_entry;
494 struct tog_colors colors;
497 struct tog_reflist_entry {
498 TAILQ_ENTRY(tog_reflist_entry) entry;
499 struct got_reference *ref;
503 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
505 struct tog_ref_view_state {
506 struct tog_reflist_head refs;
507 struct tog_reflist_entry *first_displayed_entry;
508 struct tog_reflist_entry *last_displayed_entry;
509 struct tog_reflist_entry *selected_entry;
510 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
511 struct got_repository *repo;
512 struct tog_reflist_entry *matched_entry;
513 struct tog_colors colors;
516 struct tog_help_view_state {
521 int first_displayed_line;
522 int last_displayed_line;
527 enum tog_keymap_type type;
530 #define GENERATE_HELP \
531 KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
532 KEY_("H F1", "Open view-specific help (double tap for all help)"), \
533 KEY_("k C-p Up", "Move cursor or page up one line"), \
534 KEY_("j C-n Down", "Move cursor or page down one line"), \
535 KEY_("C-b b PgUp", "Scroll the view up one page"), \
536 KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
537 KEY_("C-u u", "Scroll the view up one half page"), \
538 KEY_("C-d d", "Scroll the view down one half page"), \
539 KEY_("g", "Go to line N (default: first line)"), \
540 KEY_("Home =", "Go to the first line"), \
541 KEY_("G", "Go to line N (default: last line)"), \
542 KEY_("End *", "Go to the last line"), \
543 KEY_("l Right", "Scroll the view right"), \
544 KEY_("h Left", "Scroll the view left"), \
545 KEY_("$", "Scroll view to the rightmost position"), \
546 KEY_("0", "Scroll view to the leftmost position"), \
547 KEY_("-", "Decrease size of the focussed split"), \
548 KEY_("+", "Increase size of the focussed split"), \
549 KEY_("Tab", "Switch focus between views"), \
550 KEY_("F", "Toggle fullscreen mode"), \
551 KEY_("S", "Switch split-screen layout"), \
552 KEY_("/", "Open prompt to enter search term"), \
553 KEY_("n", "Find next line/token matching the current search term"), \
554 KEY_("N", "Find previous line/token matching the current search term"),\
555 KEY_("q", "Quit the focussed view; Quit help screen"), \
556 KEY_("Q", "Quit tog"), \
558 KEYMAP_("Log view", TOG_KEYMAP_LOG), \
559 KEY_("< ,", "Move cursor up one commit"), \
560 KEY_("> .", "Move cursor down one commit"), \
561 KEY_("Enter", "Open diff view of the selected commit"), \
562 KEY_("B", "Reload the log view and toggle display of merged commits"), \
563 KEY_("R", "Open ref view of all repository references"), \
564 KEY_("T", "Display tree view of the repository from the selected" \
566 KEY_("@", "Toggle between displaying author and committer name"), \
567 KEY_("&", "Open prompt to enter term to limit commits displayed"), \
568 KEY_("C-g Backspace", "Cancel current search or log operation"), \
569 KEY_("C-l", "Reload the log view with new commits in the repository"), \
571 KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
572 KEY_("K < ,", "Display diff of next line in the file/log entry"), \
573 KEY_("J > .", "Display diff of previous line in the file/log entry"), \
574 KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
575 KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
577 KEY_("(", "Go to the previous file in the diff"), \
578 KEY_(")", "Go to the next file in the diff"), \
579 KEY_("{", "Go to the previous hunk in the diff"), \
580 KEY_("}", "Go to the next hunk in the diff"), \
581 KEY_("[", "Decrease the number of context lines"), \
582 KEY_("]", "Increase the number of context lines"), \
583 KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
585 KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
586 KEY_("Enter", "Display diff view of the selected line's commit"), \
587 KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
588 KEY_("L", "Open log view for the currently selected annotated line"), \
589 KEY_("C", "Reload view with the previously blamed commit"), \
590 KEY_("c", "Reload view with the version of the file found in the" \
591 " selected line's commit"), \
592 KEY_("p", "Reload view with the version of the file found in the" \
593 " selected line's parent commit"), \
595 KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
596 KEY_("Enter", "Enter selected directory or open blame view of the" \
598 KEY_("L", "Open log view for the selected entry"), \
599 KEY_("R", "Open ref view of all repository references"), \
600 KEY_("i", "Show object IDs for all tree entries"), \
601 KEY_("Backspace", "Return to the parent directory"), \
603 KEYMAP_("Ref view", TOG_KEYMAP_REF), \
604 KEY_("Enter", "Display log view of the selected reference"), \
605 KEY_("T", "Display tree view of the selected reference"), \
606 KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
607 KEY_("m", "Toggle display of last modified date for each reference"), \
608 KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
609 KEY_("C-l", "Reload view with all repository references")
614 enum tog_keymap_type type;
617 /* curses io for tog regress */
625 static int using_mock_io;
627 #define TOG_KEY_SCRDUMP SHRT_MIN
630 * We implement two types of views: parent views and child views.
632 * The 'Tab' key switches focus between a parent view and its child view.
633 * Child views are shown side-by-side to their parent view, provided
634 * there is enough screen estate.
636 * When a new view is opened from within a parent view, this new view
637 * becomes a child view of the parent view, replacing any existing child.
639 * When a new view is opened from within a child view, this new view
640 * becomes a parent view which will obscure the views below until the
641 * user quits the new parent view by typing 'q'.
643 * This list of views contains parent views only.
644 * Child views are only pointed to by their parent view.
646 TAILQ_HEAD(tog_view_list_head, tog_view);
649 TAILQ_ENTRY(tog_view) entry;
652 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
653 int resized_y, resized_x; /* begin_y/x based on user resizing */
654 int maxx, x; /* max column and current start column */
655 int lines, cols; /* copies of LINES and COLS */
656 int nscrolled, offset; /* lines scrolled and hsplit line offset */
657 int gline, hiline; /* navigate to and highlight this nG line */
658 int ch, count; /* current keymap and count prefix */
659 int resized; /* set when in a resize event */
660 int focussed; /* Only set on one parent or child view at a time. */
662 struct tog_view *parent;
663 struct tog_view *child;
666 * This flag is initially set on parent views when a new child view
667 * is created. It gets toggled when the 'Tab' key switches focus
668 * between parent and child.
669 * The flag indicates whether focus should be passed on to our child
670 * view if this parent view gets picked for focus after another parent
671 * view was closed. This prevents child views from losing focus in such
676 enum tog_view_mode mode;
677 /* type-specific state */
678 enum tog_view_type type;
680 struct tog_diff_view_state diff;
681 struct tog_log_view_state log;
682 struct tog_blame_view_state blame;
683 struct tog_tree_view_state tree;
684 struct tog_ref_view_state ref;
685 struct tog_help_view_state help;
688 const struct got_error *(*show)(struct tog_view *);
689 const struct got_error *(*input)(struct tog_view **,
690 struct tog_view *, int);
691 const struct got_error *(*reset)(struct tog_view *);
692 const struct got_error *(*resize)(struct tog_view *, int);
693 const struct got_error *(*close)(struct tog_view *);
695 const struct got_error *(*search_start)(struct tog_view *);
696 const struct got_error *(*search_next)(struct tog_view *);
697 void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
698 int **, int **, int **, int **);
701 #define TOG_SEARCH_FORWARD 1
702 #define TOG_SEARCH_BACKWARD 2
703 int search_next_done;
704 #define TOG_SEARCH_HAVE_MORE 1
705 #define TOG_SEARCH_NO_MORE 2
706 #define TOG_SEARCH_HAVE_NONE 3
712 static const struct got_error *open_diff_view(struct tog_view *,
713 struct got_object_id *, struct got_object_id *,
714 const char *, const char *, int, int, int, struct tog_view *,
715 struct got_repository *);
716 static const struct got_error *show_diff_view(struct tog_view *);
717 static const struct got_error *input_diff_view(struct tog_view **,
718 struct tog_view *, int);
719 static const struct got_error *reset_diff_view(struct tog_view *);
720 static const struct got_error* close_diff_view(struct tog_view *);
721 static const struct got_error *search_start_diff_view(struct tog_view *);
722 static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
723 size_t *, int **, int **, int **, int **);
724 static const struct got_error *search_next_view_match(struct tog_view *);
726 static const struct got_error *open_log_view(struct tog_view *,
727 struct got_object_id *, struct got_repository *,
728 const char *, const char *, int);
729 static const struct got_error * show_log_view(struct tog_view *);
730 static const struct got_error *input_log_view(struct tog_view **,
731 struct tog_view *, int);
732 static const struct got_error *resize_log_view(struct tog_view *, int);
733 static const struct got_error *close_log_view(struct tog_view *);
734 static const struct got_error *search_start_log_view(struct tog_view *);
735 static const struct got_error *search_next_log_view(struct tog_view *);
737 static const struct got_error *open_blame_view(struct tog_view *, char *,
738 struct got_object_id *, struct got_repository *);
739 static const struct got_error *show_blame_view(struct tog_view *);
740 static const struct got_error *input_blame_view(struct tog_view **,
741 struct tog_view *, int);
742 static const struct got_error *reset_blame_view(struct tog_view *);
743 static const struct got_error *close_blame_view(struct tog_view *);
744 static const struct got_error *search_start_blame_view(struct tog_view *);
745 static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
746 size_t *, int **, int **, int **, int **);
748 static const struct got_error *open_tree_view(struct tog_view *,
749 struct got_object_id *, const char *, struct got_repository *);
750 static const struct got_error *show_tree_view(struct tog_view *);
751 static const struct got_error *input_tree_view(struct tog_view **,
752 struct tog_view *, int);
753 static const struct got_error *close_tree_view(struct tog_view *);
754 static const struct got_error *search_start_tree_view(struct tog_view *);
755 static const struct got_error *search_next_tree_view(struct tog_view *);
757 static const struct got_error *open_ref_view(struct tog_view *,
758 struct got_repository *);
759 static const struct got_error *show_ref_view(struct tog_view *);
760 static const struct got_error *input_ref_view(struct tog_view **,
761 struct tog_view *, int);
762 static const struct got_error *close_ref_view(struct tog_view *);
763 static const struct got_error *search_start_ref_view(struct tog_view *);
764 static const struct got_error *search_next_ref_view(struct tog_view *);
766 static const struct got_error *open_help_view(struct tog_view *,
768 static const struct got_error *show_help_view(struct tog_view *);
769 static const struct got_error *input_help_view(struct tog_view **,
770 struct tog_view *, int);
771 static const struct got_error *reset_help_view(struct tog_view *);
772 static const struct got_error* close_help_view(struct tog_view *);
773 static const struct got_error *search_start_help_view(struct tog_view *);
774 static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
775 size_t *, int **, int **, int **, int **);
777 static volatile sig_atomic_t tog_sigwinch_received;
778 static volatile sig_atomic_t tog_sigpipe_received;
779 static volatile sig_atomic_t tog_sigcont_received;
780 static volatile sig_atomic_t tog_sigint_received;
781 static volatile sig_atomic_t tog_sigterm_received;
784 tog_sigwinch(int signo)
786 tog_sigwinch_received = 1;
790 tog_sigpipe(int signo)
792 tog_sigpipe_received = 1;
796 tog_sigcont(int signo)
798 tog_sigcont_received = 1;
802 tog_sigint(int signo)
804 tog_sigint_received = 1;
808 tog_sigterm(int signo)
810 tog_sigterm_received = 1;
814 tog_fatal_signal_received(void)
816 return (tog_sigpipe_received ||
817 tog_sigint_received || tog_sigterm_received);
820 static const struct got_error *
821 view_close(struct tog_view *view)
823 const struct got_error *err = NULL, *child_err = NULL;
826 child_err = view_close(view->child);
830 err = view->close(view);
832 del_panel(view->panel);
834 delwin(view->window);
836 return err ? err : child_err;
839 static struct tog_view *
840 view_open(int nlines, int ncols, int begin_y, int begin_x,
841 enum tog_view_type type)
843 struct tog_view *view = calloc(1, sizeof(*view));
851 view->nlines = nlines ? nlines : LINES - begin_y;
852 view->ncols = ncols ? ncols : COLS - begin_x;
853 view->begin_y = begin_y;
854 view->begin_x = begin_x;
855 view->window = newwin(nlines, ncols, begin_y, begin_x);
856 if (view->window == NULL) {
860 view->panel = new_panel(view->window);
861 if (view->panel == NULL ||
862 set_panel_userptr(view->panel, view) != OK) {
867 keypad(view->window, TRUE);
872 view_split_begin_x(int begin_x)
874 if (begin_x > 0 || COLS < 120)
876 return (COLS - MAX(COLS / 2, 80));
879 /* XXX Stub till we decide what to do. */
881 view_split_begin_y(int lines)
883 return lines * HSPLIT_SCALE;
886 static const struct got_error *view_resize(struct tog_view *);
888 static const struct got_error *
889 view_splitscreen(struct tog_view *view)
891 const struct got_error *err = NULL;
893 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
894 if (view->resized_y && view->resized_y < view->lines)
895 view->begin_y = view->resized_y;
897 view->begin_y = view_split_begin_y(view->nlines);
899 } else if (!view->resized) {
900 if (view->resized_x && view->resized_x < view->cols - 1 &&
902 view->begin_x = view->resized_x;
904 view->begin_x = view_split_begin_x(0);
907 view->nlines = LINES - view->begin_y;
908 view->ncols = COLS - view->begin_x;
911 err = view_resize(view);
915 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
916 view->parent->nlines = view->begin_y;
918 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
919 return got_error_from_errno("mvwin");
924 static const struct got_error *
925 view_fullscreen(struct tog_view *view)
927 const struct got_error *err = NULL;
930 view->begin_y = view->resized ? view->begin_y : 0;
931 view->nlines = view->resized ? view->nlines : LINES;
935 err = view_resize(view);
939 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
940 return got_error_from_errno("mvwin");
946 view_is_parent_view(struct tog_view *view)
948 return view->parent == NULL;
952 view_is_splitscreen(struct tog_view *view)
954 return view->begin_x > 0 || view->begin_y > 0;
958 view_is_fullscreen(struct tog_view *view)
960 return view->nlines == LINES && view->ncols == COLS;
964 view_is_hsplit_top(struct tog_view *view)
966 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
967 view_is_splitscreen(view->child);
971 view_border(struct tog_view *view)
974 const struct tog_view *view_above;
977 return view_border(view->parent);
979 panel = panel_above(view->panel);
983 view_above = panel_userptr(panel);
984 if (view->mode == TOG_VIEW_SPLIT_HRZN)
985 mvwhline(view->window, view_above->begin_y - 1,
986 view->begin_x, ACS_HLINE, view->ncols);
988 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
989 ACS_VLINE, view->nlines);
992 static const struct got_error *view_init_hsplit(struct tog_view *, int);
993 static const struct got_error *request_log_commits(struct tog_view *);
994 static const struct got_error *offset_selection_down(struct tog_view *);
995 static void offset_selection_up(struct tog_view *);
996 static void view_get_split(struct tog_view *, int *, int *);
998 static const struct got_error *
999 view_resize(struct tog_view *view)
1001 const struct got_error *err = NULL;
1002 int dif, nlines, ncols;
1004 dif = LINES - view->lines; /* line difference */
1006 if (view->lines > LINES)
1007 nlines = view->nlines - (view->lines - LINES);
1009 nlines = view->nlines + (LINES - view->lines);
1010 if (view->cols > COLS)
1011 ncols = view->ncols - (view->cols - COLS);
1013 ncols = view->ncols + (COLS - view->cols);
1016 int hs = view->child->begin_y;
1018 if (!view_is_fullscreen(view))
1019 view->child->begin_x = view_split_begin_x(view->begin_x);
1020 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1021 view->child->begin_x == 0) {
1024 view_fullscreen(view->child);
1025 if (view->child->focussed)
1026 show_panel(view->child->panel);
1028 show_panel(view->panel);
1030 ncols = view->child->begin_x;
1032 view_splitscreen(view->child);
1033 show_panel(view->child->panel);
1036 * XXX This is ugly and needs to be moved into the above
1037 * logic but "works" for now and my attempts at moving it
1038 * break either 'tab' or 'F' key maps in horizontal splits.
1041 err = view_splitscreen(view->child);
1044 if (dif < 0) { /* top split decreased */
1045 err = offset_selection_down(view);
1052 show_panel(view->child->panel);
1053 nlines = view->nlines;
1055 } else if (view->parent == NULL)
1058 if (view->resize && dif > 0) {
1059 err = view->resize(view, dif);
1064 if (wresize(view->window, nlines, ncols) == ERR)
1065 return got_error_from_errno("wresize");
1066 if (replace_panel(view->panel, view->window) == ERR)
1067 return got_error_from_errno("replace_panel");
1068 wclear(view->window);
1070 view->nlines = nlines;
1071 view->ncols = ncols;
1072 view->lines = LINES;
1078 static const struct got_error *
1079 resize_log_view(struct tog_view *view, int increase)
1081 struct tog_log_view_state *s = &view->state.log;
1082 const struct got_error *err = NULL;
1085 if (s->selected_entry)
1086 n = s->selected_entry->idx + view->lines - s->selected;
1089 * Request commits to account for the increased
1090 * height so we have enough to populate the view.
1092 if (s->commits->ncommits < n) {
1093 view->nscrolled = n - s->commits->ncommits + increase + 1;
1094 err = request_log_commits(view);
1101 view_adjust_offset(struct tog_view *view, int n)
1106 if (view->parent && view->parent->offset) {
1107 if (view->parent->offset + n >= 0)
1108 view->parent->offset += n;
1110 view->parent->offset = 0;
1111 } else if (view->offset) {
1112 if (view->offset - n >= 0)
1119 static const struct got_error *
1120 view_resize_split(struct tog_view *view, int resize)
1122 const struct got_error *err = NULL;
1123 struct tog_view *v = NULL;
1130 if (!v->child || !view_is_splitscreen(v->child))
1133 v->resized = v->child->resized = resize; /* lock for resize event */
1135 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1136 if (v->child->resized_y)
1137 v->child->begin_y = v->child->resized_y;
1139 v->child->begin_y -= resize;
1141 v->child->begin_y += resize;
1142 if (v->child->begin_y < 3) {
1144 v->child->begin_y = 3;
1145 } else if (v->child->begin_y > LINES - 1) {
1147 v->child->begin_y = LINES - 1;
1150 v->child->ncols = COLS;
1151 view_adjust_offset(view, resize);
1152 err = view_init_hsplit(v, v->child->begin_y);
1155 v->child->resized_y = v->child->begin_y;
1157 if (v->child->resized_x)
1158 v->child->begin_x = v->child->resized_x;
1160 v->child->begin_x -= resize;
1162 v->child->begin_x += resize;
1163 if (v->child->begin_x < 11) {
1165 v->child->begin_x = 11;
1166 } else if (v->child->begin_x > COLS - 1) {
1168 v->child->begin_x = COLS - 1;
1170 v->child->resized_x = v->child->begin_x;
1173 v->child->mode = v->mode;
1174 v->child->nlines = v->lines - v->child->begin_y;
1175 v->child->ncols = v->cols - v->child->begin_x;
1178 err = view_fullscreen(v);
1181 err = view_splitscreen(v->child);
1185 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1186 err = offset_selection_down(v->child);
1192 err = v->resize(v, 0);
1193 else if (v->child->resize)
1194 err = v->child->resize(v->child, 0);
1196 v->resized = v->child->resized = 0;
1202 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1204 struct tog_view *v = src->child ? src->child : src;
1206 dst->resized_x = v->resized_x;
1207 dst->resized_y = v->resized_y;
1210 static const struct got_error *
1211 view_close_child(struct tog_view *view)
1213 const struct got_error *err = NULL;
1215 if (view->child == NULL)
1218 err = view_close(view->child);
1223 static const struct got_error *
1224 view_set_child(struct tog_view *view, struct tog_view *child)
1226 const struct got_error *err = NULL;
1228 view->child = child;
1229 child->parent = view;
1231 err = view_resize(view);
1235 if (view->child->resized_x || view->child->resized_y)
1236 err = view_resize_split(view, 0);
1241 static const struct got_error *view_dispatch_request(struct tog_view **,
1242 struct tog_view *, enum tog_view_type, int, int);
1244 static const struct got_error *
1245 view_request_new(struct tog_view **requested, struct tog_view *view,
1246 enum tog_view_type request)
1248 struct tog_view *new_view = NULL;
1249 const struct got_error *err;
1254 if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1255 view_get_split(view, &y, &x);
1257 err = view_dispatch_request(&new_view, view, request, y, x);
1261 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1262 request != TOG_VIEW_HELP) {
1263 err = view_init_hsplit(view, y);
1269 new_view->focussed = 1;
1270 new_view->mode = view->mode;
1271 new_view->nlines = request == TOG_VIEW_HELP ?
1272 view->lines : view->lines - y;
1274 if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1275 view_transfer_size(new_view, view);
1276 err = view_close_child(view);
1279 err = view_set_child(view, new_view);
1282 view->focus_child = 1;
1284 *requested = new_view;
1290 tog_resizeterm(void)
1293 struct winsize size;
1295 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1296 cols = 80; /* Default */
1300 lines = size.ws_row;
1302 resize_term(lines, cols);
1305 static const struct got_error *
1306 view_search_start(struct tog_view *view, int fast_refresh)
1308 const struct got_error *err = NULL;
1309 struct tog_view *v = view;
1313 if (view->search_started) {
1314 regfree(&view->regex);
1315 view->searching = 0;
1316 memset(&view->regmatch, 0, sizeof(view->regmatch));
1318 view->search_started = 0;
1320 if (view->nlines < 1)
1323 if (view_is_hsplit_top(view))
1325 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1328 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1329 wclrtoeol(v->window);
1331 nodelay(v->window, FALSE); /* block for search term input */
1334 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1335 wrefresh(v->window);
1338 nodelay(v->window, TRUE);
1339 if (!fast_refresh && !using_mock_io)
1344 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1345 err = view->search_start(view);
1347 regfree(&view->regex);
1350 view->search_started = 1;
1351 view->searching = TOG_SEARCH_FORWARD;
1352 view->search_next_done = 0;
1353 view->search_next(view);
1359 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1360 static const struct got_error *
1361 switch_split(struct tog_view *view)
1363 const struct got_error *err = NULL;
1364 struct tog_view *v = NULL;
1371 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1372 v->mode = TOG_VIEW_SPLIT_VERT;
1374 v->mode = TOG_VIEW_SPLIT_HRZN;
1378 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1379 v->mode = TOG_VIEW_SPLIT_NONE;
1381 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1382 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1383 v->child->begin_y = v->child->resized_y;
1384 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1385 v->child->begin_x = v->child->resized_x;
1388 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1390 v->child->ncols = COLS;
1391 v->child->nscrolled = LINES - v->child->nlines;
1393 err = view_init_hsplit(v, v->child->begin_y);
1397 v->child->mode = v->mode;
1398 v->child->nlines = v->lines - v->child->begin_y;
1401 err = view_fullscreen(v);
1404 err = view_splitscreen(v->child);
1408 if (v->mode == TOG_VIEW_SPLIT_NONE)
1409 v->mode = TOG_VIEW_SPLIT_VERT;
1410 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1411 err = offset_selection_down(v);
1414 err = offset_selection_down(v->child);
1418 offset_selection_up(v);
1419 offset_selection_up(v->child);
1422 err = v->resize(v, 0);
1423 else if (v->child->resize)
1424 err = v->child->resize(v->child, 0);
1430 * Strip trailing whitespace from str starting at byte *n;
1431 * if *n < 0, use strlen(str). Return new str length in *n.
1434 strip_trailing_ws(char *str, int *n)
1438 if (str == NULL || *str == '\0')
1444 while (x-- > 0 && isspace((unsigned char)str[x]))
1451 * Extract visible substring of line y from the curses screen
1452 * and strip trailing whitespace. If vline is set, overwrite
1453 * line[vline] with '|' because the ACS_VLINE character is
1454 * written out as 'x'. Write the line to file f.
1456 static const struct got_error *
1457 view_write_line(FILE *f, int y, int vline)
1459 char line[COLS * MB_LEN_MAX]; /* allow for multibyte chars */
1462 r = mvwinnstr(curscr, y, 0, line, sizeof(line));
1464 return got_error_fmt(GOT_ERR_RANGE,
1465 "failed to extract line %d", y);
1468 * In some views, lines are padded with blanks to COLS width.
1469 * Strip them so we can diff without the -b flag when testing.
1471 strip_trailing_ws(line, &r);
1476 w = fprintf(f, "%s\n", line);
1477 if (w != r + 1) /* \n */
1478 return got_ferror(f, GOT_ERR_IO);
1484 * Capture the visible curses screen by writing each line to the
1485 * file at the path set via the TOG_SCR_DUMP environment variable.
1487 static const struct got_error *
1488 screendump(struct tog_view *view)
1490 const struct got_error *err;
1493 err = got_opentemp_truncate(tog_io.sdump);
1497 if ((view->child && view->child->begin_x) ||
1498 (view->parent && view->begin_x)) {
1499 int ncols = view->child ? view->ncols : view->parent->ncols;
1501 /* vertical splitscreen */
1502 for (i = 0; i < view->nlines; ++i) {
1503 err = view_write_line(tog_io.sdump, i, ncols - 1);
1510 /* fullscreen or horizontal splitscreen */
1511 if ((view->child && view->child->begin_y) ||
1512 (view->parent && view->begin_y)) /* hsplit */
1513 hline = view->child ?
1514 view->child->begin_y : view->begin_y;
1516 for (i = 0; i < view->lines; i++) {
1517 if (hline && i == hline - 1) {
1520 /* ACS_HLINE writes out as 'q', overwrite it */
1521 for (c = 0; c < view->cols; ++c)
1522 fputc('-', tog_io.sdump);
1523 fputc('\n', tog_io.sdump);
1527 err = view_write_line(tog_io.sdump, i, 0);
1538 * Compute view->count from numeric input. Assign total to view->count and
1539 * return first non-numeric key entered.
1542 get_compound_key(struct tog_view *view, int c)
1544 struct tog_view *v = view;
1547 if (view_is_hsplit_top(view))
1549 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1553 cbreak(); /* block for input */
1554 nodelay(view->window, FALSE);
1555 wmove(v->window, v->nlines - 1, 0);
1556 wclrtoeol(v->window);
1557 waddch(v->window, ':');
1560 x = getcurx(v->window);
1561 if (x != ERR && x < view->ncols) {
1562 waddch(v->window, c);
1563 wrefresh(v->window);
1567 * Don't overflow. Max valid request should be the greatest
1568 * between the longest and total lines; cap at 10 million.
1573 n = n * 10 + (c - '0');
1574 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1576 if (c == 'G' || c == 'g') { /* nG key map */
1577 view->gline = view->hiline = n;
1582 /* Massage excessive or inapplicable values at the input handler. */
1589 action_report(struct tog_view *view)
1591 struct tog_view *v = view;
1593 if (view_is_hsplit_top(view))
1595 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1598 wmove(v->window, v->nlines - 1, 0);
1599 wclrtoeol(v->window);
1600 wprintw(v->window, ":%s", view->action);
1601 wrefresh(v->window);
1604 * Clear action status report. Only clear in blame view
1605 * once annotating is complete, otherwise it's too fast.
1607 if (view->type == TOG_VIEW_BLAME) {
1608 if (view->state.blame.blame_complete)
1609 view->action = NULL;
1611 view->action = NULL;
1615 * Read the next line from the test script and assign
1616 * key instruction to *ch. If at EOF, set the *done flag.
1618 static const struct got_error *
1619 tog_read_script_key(FILE *script, struct tog_view *view, int *ch, int *done)
1621 const struct got_error *err = NULL;
1625 if (view->count && --view->count) {
1631 if (getline(&line, &linesz, script) == -1) {
1636 err = got_ferror(script, GOT_ERR_IO);
1641 if (strncasecmp(line, "WAIT_FOR_UI", 11) == 0)
1642 tog_io.wait_for_ui = 1;
1643 else if (strncasecmp(line, "KEY_ENTER", 9) == 0)
1645 else if (strncasecmp(line, "KEY_RIGHT", 9) == 0)
1647 else if (strncasecmp(line, "KEY_LEFT", 8) == 0)
1649 else if (strncasecmp(line, "KEY_DOWN", 8) == 0)
1651 else if (strncasecmp(line, "KEY_UP", 6) == 0)
1653 else if (strncasecmp(line, "TAB", 3) == 0)
1655 else if (strncasecmp(line, "SCREENDUMP", 10) == 0)
1656 *ch = TOG_KEY_SCRDUMP;
1657 else if (isdigit((unsigned char)*line)) {
1660 while (isdigit((unsigned char)*t))
1662 view->ch = *ch = *t;
1664 /* ignore error, view->count is 0 if instruction is invalid */
1665 view->count = strtonum(line, 0, INT_MAX, NULL);
1674 static const struct got_error *
1675 view_input(struct tog_view **new, int *done, struct tog_view *view,
1676 struct tog_view_list_head *views, int fast_refresh)
1678 const struct got_error *err = NULL;
1685 action_report(view);
1687 /* Clear "no matches" indicator. */
1688 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1689 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1690 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1694 if (view->searching && !view->search_next_done) {
1695 errcode = pthread_mutex_unlock(&tog_mutex);
1697 return got_error_set_errno(errcode,
1698 "pthread_mutex_unlock");
1700 errcode = pthread_mutex_lock(&tog_mutex);
1702 return got_error_set_errno(errcode,
1703 "pthread_mutex_lock");
1704 view->search_next(view);
1708 /* Allow threads to make progress while we are waiting for input. */
1709 errcode = pthread_mutex_unlock(&tog_mutex);
1711 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1713 if (using_mock_io) {
1714 err = tog_read_script_key(tog_io.f, view, &ch, done);
1716 errcode = pthread_mutex_lock(&tog_mutex);
1719 } else if (view->count && --view->count) {
1721 nodelay(view->window, TRUE);
1722 ch = wgetch(view->window);
1723 /* let C-g or backspace abort unfinished count */
1724 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1729 ch = wgetch(view->window);
1730 if (ch >= '1' && ch <= '9')
1731 view->ch = ch = get_compound_key(view, ch);
1733 if (view->hiline && ch != ERR && ch != 0)
1734 view->hiline = 0; /* key pressed, clear line highlight */
1735 nodelay(view->window, TRUE);
1736 errcode = pthread_mutex_lock(&tog_mutex);
1738 return got_error_set_errno(errcode, "pthread_mutex_lock");
1740 if (tog_sigwinch_received || tog_sigcont_received) {
1742 tog_sigwinch_received = 0;
1743 tog_sigcont_received = 0;
1744 TAILQ_FOREACH(v, views, entry) {
1745 err = view_resize(v);
1748 err = v->input(new, v, KEY_RESIZE);
1752 err = view_resize(v->child);
1755 err = v->child->input(new, v->child,
1759 if (v->child->resized_x || v->child->resized_y) {
1760 err = view_resize_split(v, 0);
1772 if (view->type == TOG_VIEW_HELP)
1773 err = view->reset(view);
1775 err = view_request_new(new, view, TOG_VIEW_HELP);
1781 view->child->focussed = 1;
1782 view->focus_child = 1;
1783 } else if (view->parent) {
1785 view->parent->focussed = 1;
1786 view->parent->focus_child = 0;
1787 if (!view_is_splitscreen(view)) {
1788 if (view->parent->resize) {
1789 err = view->parent->resize(view->parent,
1794 offset_selection_up(view->parent);
1795 err = view_fullscreen(view->parent);
1802 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1803 if (view->parent->resize) {
1804 /* might need more commits to fill fullscreen */
1805 err = view->parent->resize(view->parent, 0);
1809 offset_selection_up(view->parent);
1811 err = view->input(new, view, ch);
1819 if (view_is_parent_view(view)) {
1820 if (view->child == NULL)
1822 if (view_is_splitscreen(view->child)) {
1824 view->child->focussed = 1;
1825 err = view_fullscreen(view->child);
1827 err = view_splitscreen(view->child);
1829 err = view_resize_split(view, 0);
1833 err = view->child->input(new, view->child,
1836 if (view_is_splitscreen(view)) {
1837 view->parent->focussed = 0;
1839 err = view_fullscreen(view);
1841 err = view_splitscreen(view);
1842 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1843 err = view_resize(view->parent);
1845 err = view_resize_split(view, 0);
1849 err = view->input(new, view, KEY_RESIZE);
1854 err = view->resize(view, 0);
1859 if (view->parent->resize) {
1860 err = view->parent->resize(view->parent, 0);
1864 err = offset_selection_down(view->parent);
1868 err = offset_selection_down(view);
1872 err = switch_split(view);
1875 err = view_resize_split(view, -1);
1878 err = view_resize_split(view, 1);
1884 if (view->search_start)
1885 view_search_start(view, fast_refresh);
1887 err = view->input(new, view, ch);
1891 if (view->search_started && view->search_next) {
1892 view->searching = (ch == 'n' ?
1893 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1894 view->search_next_done = 0;
1895 view->search_next(view);
1897 err = view->input(new, view, ch);
1900 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1901 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1902 view->action = "Patience diff algorithm";
1904 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1905 view->action = "Myers diff algorithm";
1907 TAILQ_FOREACH(v, views, entry) {
1913 if (v->child && v->child->reset) {
1914 err = v->child->reset(v->child);
1920 case TOG_KEY_SCRDUMP:
1921 err = screendump(view);
1924 err = view->input(new, view, ch);
1932 view_needs_focus_indication(struct tog_view *view)
1934 if (view_is_parent_view(view)) {
1935 if (view->child == NULL || view->child->focussed)
1937 if (!view_is_splitscreen(view->child))
1939 } else if (!view_is_splitscreen(view))
1942 return view->focussed;
1945 static const struct got_error *
1948 const struct got_error *err = NULL;
1950 if (tog_io.cin && fclose(tog_io.cin) == EOF)
1951 err = got_ferror(tog_io.cin, GOT_ERR_IO);
1952 if (tog_io.cout && fclose(tog_io.cout) == EOF && err == NULL)
1953 err = got_ferror(tog_io.cout, GOT_ERR_IO);
1954 if (tog_io.f && fclose(tog_io.f) == EOF && err == NULL)
1955 err = got_ferror(tog_io.f, GOT_ERR_IO);
1956 if (tog_io.sdump && fclose(tog_io.sdump) == EOF && err == NULL)
1957 err = got_ferror(tog_io.sdump, GOT_ERR_IO);
1962 static const struct got_error *
1963 view_loop(struct tog_view *view)
1965 const struct got_error *err = NULL;
1966 struct tog_view_list_head views;
1967 struct tog_view *new_view;
1969 int fast_refresh = 10;
1970 int done = 0, errcode;
1972 mode = getenv("TOG_VIEW_SPLIT_MODE");
1973 if (!mode || !(*mode == 'h' || *mode == 'H'))
1974 view->mode = TOG_VIEW_SPLIT_VERT;
1976 view->mode = TOG_VIEW_SPLIT_HRZN;
1978 errcode = pthread_mutex_lock(&tog_mutex);
1980 return got_error_set_errno(errcode, "pthread_mutex_lock");
1983 TAILQ_INSERT_HEAD(&views, view, entry);
1986 err = view->show(view);
1991 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1992 !tog_fatal_signal_received()) {
1993 /* Refresh fast during initialization, then become slower. */
1994 if (fast_refresh && --fast_refresh == 0 && !using_mock_io)
1995 halfdelay(10); /* switch to once per second */
1997 err = view_input(&new_view, &done, view, &views, fast_refresh);
2001 if (view->dying && view == TAILQ_FIRST(&views) &&
2002 TAILQ_NEXT(view, entry) == NULL)
2008 * When we quit, scroll the screen up a single line
2009 * so we don't lose any information.
2011 TAILQ_FOREACH(v, &views, entry) {
2012 wmove(v->window, 0, 0);
2013 wdeleteln(v->window);
2014 wnoutrefresh(v->window);
2015 if (v->child && !view_is_fullscreen(v)) {
2016 wmove(v->child->window, 0, 0);
2017 wdeleteln(v->child->window);
2018 wnoutrefresh(v->child->window);
2025 struct tog_view *v, *prev = NULL;
2027 if (view_is_parent_view(view))
2028 prev = TAILQ_PREV(view, tog_view_list_head,
2030 else if (view->parent)
2031 prev = view->parent;
2034 view->parent->child = NULL;
2035 view->parent->focus_child = 0;
2036 /* Restore fullscreen line height. */
2037 view->parent->nlines = view->parent->lines;
2038 err = view_resize(view->parent);
2041 /* Make resized splits persist. */
2042 view_transfer_size(view->parent, view);
2044 TAILQ_REMOVE(&views, view, entry);
2046 err = view_close(view);
2051 TAILQ_FOREACH(v, &views, entry) {
2055 if (view == NULL && new_view == NULL) {
2056 /* No view has focus. Try to pick one. */
2059 else if (!TAILQ_EMPTY(&views)) {
2060 view = TAILQ_LAST(&views,
2061 tog_view_list_head);
2064 if (view->focus_child) {
2065 view->child->focussed = 1;
2073 struct tog_view *v, *t;
2074 /* Only allow one parent view per type. */
2075 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
2076 if (v->type != new_view->type)
2078 TAILQ_REMOVE(&views, v, entry);
2079 err = view_close(v);
2084 TAILQ_INSERT_TAIL(&views, new_view, entry);
2087 if (view && !done) {
2088 if (view_is_parent_view(view)) {
2089 if (view->child && view->child->focussed)
2092 if (view->parent && view->parent->focussed)
2093 view = view->parent;
2095 show_panel(view->panel);
2096 if (view->child && view_is_splitscreen(view->child))
2097 show_panel(view->child->panel);
2098 if (view->parent && view_is_splitscreen(view)) {
2099 err = view->parent->show(view->parent);
2103 err = view->show(view);
2107 err = view->child->show(view->child);
2116 while (!TAILQ_EMPTY(&views)) {
2117 const struct got_error *close_err;
2118 view = TAILQ_FIRST(&views);
2119 TAILQ_REMOVE(&views, view, entry);
2120 close_err = view_close(view);
2121 if (close_err && err == NULL)
2125 errcode = pthread_mutex_unlock(&tog_mutex);
2126 if (errcode && err == NULL)
2127 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2137 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
2142 /* Create newly allocated wide-character string equivalent to a byte string. */
2143 static const struct got_error *
2144 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
2147 const struct got_error *err = NULL;
2150 *wlen = mbstowcs(NULL, s, 0);
2151 if (*wlen == (size_t)-1) {
2153 if (errno != EILSEQ)
2154 return got_error_from_errno("mbstowcs");
2156 /* byte string invalid in current encoding; try to "fix" it */
2157 err = got_mbsavis(&vis, &vislen, s);
2160 *wlen = mbstowcs(NULL, vis, 0);
2161 if (*wlen == (size_t)-1) {
2162 err = got_error_from_errno("mbstowcs"); /* give up */
2167 *ws = calloc(*wlen + 1, sizeof(**ws));
2169 err = got_error_from_errno("calloc");
2173 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
2174 err = got_error_from_errno("mbstowcs");
2185 static const struct got_error *
2186 expand_tab(char **ptr, const char *src)
2189 size_t len, n, idx = 0, sz = 0;
2192 n = len = strlen(src);
2193 dst = malloc(n + 1);
2195 return got_error_from_errno("malloc");
2197 while (idx < len && src[idx]) {
2198 const char c = src[idx];
2201 size_t nb = TABSIZE - sz % TABSIZE;
2204 p = realloc(dst, n + nb);
2207 return got_error_from_errno("realloc");
2212 memset(dst + sz, ' ', nb);
2215 dst[sz++] = src[idx];
2225 * Advance at most n columns from wline starting at offset off.
2226 * Return the index to the first character after the span operation.
2227 * Return the combined column width of all spanned wide characters in
2231 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2233 int width, i, cols = 0;
2240 for (i = off; wline[i] != L'\0'; ++i) {
2241 if (wline[i] == L'\t')
2242 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2244 width = wcwidth(wline[i]);
2251 if (cols + width > n)
2261 * Format a line for display, ensuring that it won't overflow a width limit.
2262 * With scrolling, the width returned refers to the scrolled version of the
2263 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2265 static const struct got_error *
2266 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2267 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2269 const struct got_error *err = NULL;
2271 wchar_t *wline = NULL;
2280 err = expand_tab(&exstr, line);
2285 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2290 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2292 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2293 wline[wlen - 1] = L'\0';
2296 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2297 wline[wlen - 1] = L'\0';
2301 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2307 *scrollxp = scrollx;
2315 static const struct got_error*
2316 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2317 struct got_object_id *id, struct got_repository *repo)
2319 static const struct got_error *err = NULL;
2320 struct got_reflist_entry *re;
2329 TAILQ_FOREACH(re, refs, entry) {
2330 struct got_tag_object *tag = NULL;
2331 struct got_object_id *ref_id;
2334 name = got_ref_get_name(re->ref);
2335 if (strcmp(name, GOT_REF_HEAD) == 0)
2337 if (strncmp(name, "refs/", 5) == 0)
2339 if (strncmp(name, "got/", 4) == 0)
2341 if (strncmp(name, "heads/", 6) == 0)
2343 if (strncmp(name, "remotes/", 8) == 0) {
2345 s = strstr(name, "/" GOT_REF_HEAD);
2346 if (s != NULL && strcmp(s, "/" GOT_REF_HEAD) == 0)
2349 err = got_ref_resolve(&ref_id, repo, re->ref);
2352 if (strncmp(name, "tags/", 5) == 0) {
2353 err = got_object_open_as_tag(&tag, repo, ref_id);
2355 if (err->code != GOT_ERR_OBJ_TYPE) {
2359 /* Ref points at something other than a tag. */
2364 cmp = got_object_id_cmp(tag ?
2365 got_object_tag_get_object_id(tag) : ref_id, id);
2368 got_object_tag_close(tag);
2372 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2373 s ? ", " : "", name) == -1) {
2374 err = got_error_from_errno("asprintf");
2385 static const struct got_error *
2386 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2391 smallerthan = strchr(author, '<');
2392 if (smallerthan && smallerthan[1] != '\0')
2393 author = smallerthan + 1;
2394 author[strcspn(author, "@>")] = '\0';
2395 return format_line(wauthor, author_width, NULL, author, 0, limit,
2399 static const struct got_error *
2400 draw_commit(struct tog_view *view, struct got_commit_object *commit,
2401 struct got_object_id *id, const size_t date_display_cols,
2402 int author_display_cols)
2404 struct tog_log_view_state *s = &view->state.log;
2405 const struct got_error *err = NULL;
2406 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2407 char *refs_str = NULL;
2408 char *logmsg0 = NULL, *logmsg = NULL;
2409 char *author = NULL;
2410 wchar_t *wrefstr = NULL, *wlogmsg = NULL, *wauthor = NULL;
2411 int author_width, refstr_width, logmsg_width;
2412 char *newline, *line = NULL;
2413 int col, limit, scrollx, logmsg_x;
2414 const int avail = view->ncols;
2416 time_t committer_time;
2417 struct tog_color *tc;
2418 struct got_reflist_head *refs;
2420 committer_time = got_object_commit_get_committer_time(commit);
2421 if (gmtime_r(&committer_time, &tm) == NULL)
2422 return got_error_from_errno("gmtime_r");
2423 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2424 return got_error(GOT_ERR_NO_SPACE);
2426 if (avail <= date_display_cols)
2427 limit = MIN(sizeof(datebuf) - 1, avail);
2429 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2430 tc = get_color(&s->colors, TOG_COLOR_DATE);
2432 wattr_on(view->window,
2433 COLOR_PAIR(tc->colorpair), NULL);
2434 waddnstr(view->window, datebuf, limit);
2436 wattr_off(view->window,
2437 COLOR_PAIR(tc->colorpair), NULL);
2444 err = got_object_id_str(&id_str, id);
2447 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2449 wattr_on(view->window,
2450 COLOR_PAIR(tc->colorpair), NULL);
2451 wprintw(view->window, "%.8s ", id_str);
2453 wattr_off(view->window,
2454 COLOR_PAIR(tc->colorpair), NULL);
2461 if (s->use_committer)
2462 author = strdup(got_object_commit_get_committer(commit));
2464 author = strdup(got_object_commit_get_author(commit));
2465 if (author == NULL) {
2466 err = got_error_from_errno("strdup");
2469 err = format_author(&wauthor, &author_width, author, avail - col, col);
2472 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2474 wattr_on(view->window,
2475 COLOR_PAIR(tc->colorpair), NULL);
2476 waddwstr(view->window, wauthor);
2477 col += author_width;
2478 while (col < avail && author_width < author_display_cols + 2) {
2479 waddch(view->window, ' ');
2484 wattr_off(view->window,
2485 COLOR_PAIR(tc->colorpair), NULL);
2489 err = got_object_commit_get_logmsg(&logmsg0, commit);
2493 while (*logmsg == '\n')
2495 newline = strchr(logmsg, '\n');
2499 limit = avail - col;
2500 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2501 limit--; /* for the border */
2503 /* Prepend reference labels to log message if possible .*/
2504 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, id);
2505 err = build_refs_str(&refs_str, refs, id, s->repo);
2511 if (asprintf(&rs, "[%s]", refs_str) == -1) {
2512 err = got_error_from_errno("asprintf");
2515 err = format_line(&wrefstr, &refstr_width,
2516 &scrollx, rs, view->x, limit, col, 1);
2520 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2522 wattr_on(view->window,
2523 COLOR_PAIR(tc->colorpair), NULL);
2524 waddwstr(view->window, &wrefstr[scrollx]);
2526 wattr_off(view->window,
2527 COLOR_PAIR(tc->colorpair), NULL);
2528 col += MAX(refstr_width, 0);
2533 waddch(view->window, ' ');
2537 if (refstr_width > 0)
2540 int unscrolled_refstr_width;
2541 size_t len = wcslen(wrefstr);
2544 * No need to check for -1 return value here since
2545 * unprintables have been replaced by span_wline().
2547 unscrolled_refstr_width = wcswidth(wrefstr, len);
2548 unscrolled_refstr_width += 1; /* trailing space */
2549 logmsg_x = view->x - unscrolled_refstr_width;
2552 limit = avail - col;
2553 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2554 limit--; /* for the border */
2558 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, logmsg_x,
2562 waddwstr(view->window, &wlogmsg[scrollx]);
2563 col += MAX(logmsg_width, 0);
2564 while (col < avail) {
2565 waddch(view->window, ' ');
2579 static struct commit_queue_entry *
2580 alloc_commit_queue_entry(struct got_commit_object *commit,
2581 struct got_object_id *id)
2583 struct commit_queue_entry *entry;
2584 struct got_object_id *dup;
2586 entry = calloc(1, sizeof(*entry));
2590 dup = got_object_id_dup(id);
2597 entry->commit = commit;
2602 pop_commit(struct commit_queue *commits)
2604 struct commit_queue_entry *entry;
2606 entry = TAILQ_FIRST(&commits->head);
2607 TAILQ_REMOVE(&commits->head, entry, entry);
2608 got_object_commit_close(entry->commit);
2609 commits->ncommits--;
2615 free_commits(struct commit_queue *commits)
2617 while (!TAILQ_EMPTY(&commits->head))
2618 pop_commit(commits);
2621 static const struct got_error *
2622 match_commit(int *have_match, struct got_object_id *id,
2623 struct got_commit_object *commit, regex_t *regex)
2625 const struct got_error *err = NULL;
2626 regmatch_t regmatch;
2627 char *id_str = NULL, *logmsg = NULL;
2631 err = got_object_id_str(&id_str, id);
2635 err = got_object_commit_get_logmsg(&logmsg, commit);
2639 if (regexec(regex, got_object_commit_get_author(commit), 1,
2640 ®match, 0) == 0 ||
2641 regexec(regex, got_object_commit_get_committer(commit), 1,
2642 ®match, 0) == 0 ||
2643 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2644 regexec(regex, logmsg, 1, ®match, 0) == 0)
2652 static const struct got_error *
2653 queue_commits(struct tog_log_thread_args *a)
2655 const struct got_error *err = NULL;
2658 * We keep all commits open throughout the lifetime of the log
2659 * view in order to avoid having to re-fetch commits from disk
2660 * while updating the display.
2663 struct got_object_id id;
2664 struct got_commit_object *commit;
2665 struct commit_queue_entry *entry;
2666 int limit_match = 0;
2669 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2674 err = got_object_open_as_commit(&commit, a->repo, &id);
2677 entry = alloc_commit_queue_entry(commit, &id);
2678 if (entry == NULL) {
2679 err = got_error_from_errno("alloc_commit_queue_entry");
2683 errcode = pthread_mutex_lock(&tog_mutex);
2685 err = got_error_set_errno(errcode,
2686 "pthread_mutex_lock");
2690 entry->idx = a->real_commits->ncommits;
2691 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2692 a->real_commits->ncommits++;
2695 err = match_commit(&limit_match, &id, commit,
2701 struct commit_queue_entry *matched;
2703 matched = alloc_commit_queue_entry(
2704 entry->commit, entry->id);
2705 if (matched == NULL) {
2706 err = got_error_from_errno(
2707 "alloc_commit_queue_entry");
2710 matched->commit = entry->commit;
2711 got_object_commit_retain(entry->commit);
2713 matched->idx = a->limit_commits->ncommits;
2714 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2716 a->limit_commits->ncommits++;
2720 * This is how we signal log_thread() that we
2721 * have found a match, and that it should be
2722 * counted as a new entry for the view.
2724 a->limit_match = limit_match;
2727 if (*a->searching == TOG_SEARCH_FORWARD &&
2728 !*a->search_next_done) {
2730 err = match_commit(&have_match, &id, commit, a->regex);
2735 if (limit_match && have_match)
2736 *a->search_next_done =
2737 TOG_SEARCH_HAVE_MORE;
2738 } else if (have_match)
2739 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2742 errcode = pthread_mutex_unlock(&tog_mutex);
2743 if (errcode && err == NULL)
2744 err = got_error_set_errno(errcode,
2745 "pthread_mutex_unlock");
2748 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2754 select_commit(struct tog_log_view_state *s)
2756 struct commit_queue_entry *entry;
2759 entry = s->first_displayed_entry;
2761 if (ncommits == s->selected) {
2762 s->selected_entry = entry;
2765 entry = TAILQ_NEXT(entry, entry);
2770 static const struct got_error *
2771 draw_commits(struct tog_view *view)
2773 const struct got_error *err = NULL;
2774 struct tog_log_view_state *s = &view->state.log;
2775 struct commit_queue_entry *entry = s->selected_entry;
2776 int limit = view->nlines;
2778 int ncommits, author_cols = 4, refstr_cols;
2779 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2780 char *refs_str = NULL;
2782 struct tog_color *tc;
2783 static const size_t date_display_cols = 12;
2784 struct got_reflist_head *refs;
2786 if (view_is_hsplit_top(view))
2787 --limit; /* account for border */
2789 if (s->selected_entry &&
2790 !(view->searching && view->search_next_done == 0)) {
2791 err = got_object_id_str(&id_str, s->selected_entry->id);
2794 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2795 s->selected_entry->id);
2796 err = build_refs_str(&refs_str, refs, s->selected_entry->id,
2802 if (s->thread_args.commits_needed == 0 && !using_mock_io)
2803 halfdelay(10); /* disable fast refresh */
2805 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2806 if (asprintf(&ncommits_str, " [%d/%d] %s",
2807 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2808 (view->searching && !view->search_next_done) ?
2809 "searching..." : "loading...") == -1) {
2810 err = got_error_from_errno("asprintf");
2814 const char *search_str = NULL;
2815 const char *limit_str = NULL;
2817 if (view->searching) {
2818 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2819 search_str = "no more matches";
2820 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2821 search_str = "no matches found";
2822 else if (!view->search_next_done)
2823 search_str = "searching...";
2826 if (s->limit_view && s->commits->ncommits == 0)
2827 limit_str = "no matches found";
2829 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2830 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2831 search_str ? search_str : (refs_str ? refs_str : ""),
2832 limit_str ? limit_str : "") == -1) {
2833 err = got_error_from_errno("asprintf");
2841 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2842 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2843 "........................................",
2844 s->in_repo_path, ncommits_str) == -1) {
2845 err = got_error_from_errno("asprintf");
2849 } else if (asprintf(&header, "commit %s%s",
2850 id_str ? id_str : "........................................",
2851 ncommits_str) == -1) {
2852 err = got_error_from_errno("asprintf");
2856 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2860 werase(view->window);
2862 if (view_needs_focus_indication(view))
2863 wstandout(view->window);
2864 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2866 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2867 waddwstr(view->window, wline);
2868 while (width < view->ncols) {
2869 waddch(view->window, ' ');
2873 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2874 if (view_needs_focus_indication(view))
2875 wstandend(view->window);
2880 /* Grow author column size if necessary, and set view->maxx. */
2881 entry = s->first_displayed_entry;
2885 struct got_commit_object *c = entry->commit;
2886 char *author, *eol, *msg, *msg0;
2887 wchar_t *wauthor, *wmsg;
2889 if (ncommits >= limit - 1)
2891 if (s->use_committer)
2892 author = strdup(got_object_commit_get_committer(c));
2894 author = strdup(got_object_commit_get_author(c));
2895 if (author == NULL) {
2896 err = got_error_from_errno("strdup");
2899 err = format_author(&wauthor, &width, author, COLS,
2901 if (author_cols < width)
2902 author_cols = width;
2907 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2909 err = build_refs_str(&refs_str, refs, entry->id, s->repo);
2914 err = format_line(&ws, &width, NULL, refs_str,
2915 0, INT_MAX, date_display_cols + author_cols, 0);
2921 refstr_cols = width + 3; /* account for [ ] + space */
2924 err = got_object_commit_get_logmsg(&msg0, c);
2928 while (*msg == '\n')
2930 if ((eol = strchr(msg, '\n')))
2932 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2933 date_display_cols + author_cols + refstr_cols, 0);
2936 view->maxx = MAX(view->maxx, width + refstr_cols);
2940 entry = TAILQ_NEXT(entry, entry);
2943 entry = s->first_displayed_entry;
2944 s->last_displayed_entry = s->first_displayed_entry;
2947 if (ncommits >= limit - 1)
2949 if (ncommits == s->selected)
2950 wstandout(view->window);
2951 err = draw_commit(view, entry->commit, entry->id,
2952 date_display_cols, author_cols);
2953 if (ncommits == s->selected)
2954 wstandend(view->window);
2958 s->last_displayed_entry = entry;
2959 entry = TAILQ_NEXT(entry, entry);
2972 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2974 struct commit_queue_entry *entry;
2977 entry = TAILQ_FIRST(&s->commits->head);
2978 if (s->first_displayed_entry == entry)
2981 entry = s->first_displayed_entry;
2982 while (entry && nscrolled < maxscroll) {
2983 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2985 s->first_displayed_entry = entry;
2991 static const struct got_error *
2992 trigger_log_thread(struct tog_view *view, int wait)
2994 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2998 halfdelay(1); /* fast refresh while loading commits */
3000 while (!ta->log_complete && !tog_thread_error &&
3001 (ta->commits_needed > 0 || ta->load_all)) {
3002 /* Wake the log thread. */
3003 errcode = pthread_cond_signal(&ta->need_commits);
3005 return got_error_set_errno(errcode,
3006 "pthread_cond_signal");
3009 * The mutex will be released while the view loop waits
3010 * in wgetch(), at which time the log thread will run.
3015 /* Display progress update in log view. */
3016 show_log_view(view);
3020 /* Wait right here while next commit is being loaded. */
3021 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
3023 return got_error_set_errno(errcode,
3024 "pthread_cond_wait");
3026 /* Display progress update in log view. */
3027 show_log_view(view);
3035 static const struct got_error *
3036 request_log_commits(struct tog_view *view)
3038 struct tog_log_view_state *state = &view->state.log;
3039 const struct got_error *err = NULL;
3041 if (state->thread_args.log_complete)
3044 state->thread_args.commits_needed += view->nscrolled;
3045 err = trigger_log_thread(view, 1);
3046 view->nscrolled = 0;
3051 static const struct got_error *
3052 log_scroll_down(struct tog_view *view, int maxscroll)
3054 struct tog_log_view_state *s = &view->state.log;
3055 const struct got_error *err = NULL;
3056 struct commit_queue_entry *pentry;
3057 int nscrolled = 0, ncommits_needed;
3059 if (s->last_displayed_entry == NULL)
3062 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
3063 if (s->commits->ncommits < ncommits_needed &&
3064 !s->thread_args.log_complete) {
3066 * Ask the log thread for required amount of commits.
3068 s->thread_args.commits_needed +=
3069 ncommits_needed - s->commits->ncommits;
3070 err = trigger_log_thread(view, 1);
3076 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
3077 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
3080 s->last_displayed_entry = pentry ?
3081 pentry : s->last_displayed_entry;
3083 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
3086 s->first_displayed_entry = pentry;
3087 } while (++nscrolled < maxscroll);
3089 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
3090 view->nscrolled += nscrolled;
3092 view->nscrolled = 0;
3097 static const struct got_error *
3098 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
3099 struct got_commit_object *commit, struct got_object_id *commit_id,
3100 struct tog_view *log_view, struct got_repository *repo)
3102 const struct got_error *err;
3103 struct got_object_qid *parent_id;
3104 struct tog_view *diff_view;
3106 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
3107 if (diff_view == NULL)
3108 return got_error_from_errno("view_open");
3110 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3111 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
3112 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
3114 *new_view = diff_view;
3118 static const struct got_error *
3119 tree_view_visit_subtree(struct tog_tree_view_state *s,
3120 struct got_tree_object *subtree)
3122 struct tog_parent_tree *parent;
3124 parent = calloc(1, sizeof(*parent));
3126 return got_error_from_errno("calloc");
3128 parent->tree = s->tree;
3129 parent->first_displayed_entry = s->first_displayed_entry;
3130 parent->selected_entry = s->selected_entry;
3131 parent->selected = s->selected;
3132 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3135 s->first_displayed_entry = NULL;
3139 static const struct got_error *
3140 tree_view_walk_path(struct tog_tree_view_state *s,
3141 struct got_commit_object *commit, const char *path)
3143 const struct got_error *err = NULL;
3144 struct got_tree_object *tree = NULL;
3146 char *slash, *subpath = NULL;
3148 /* Walk the path and open corresponding tree objects. */
3151 struct got_tree_entry *te;
3152 struct got_object_id *tree_id;
3158 /* Ensure the correct subtree entry is selected. */
3159 slash = strchr(p, '/');
3161 te_name = strdup(p);
3163 te_name = strndup(p, slash - p);
3164 if (te_name == NULL) {
3165 err = got_error_from_errno("strndup");
3168 te = got_object_tree_find_entry(s->tree, te_name);
3170 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
3175 s->first_displayed_entry = s->selected_entry = te;
3177 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
3178 break; /* jump to this file's entry */
3180 slash = strchr(p, '/');
3182 subpath = strndup(path, slash - path);
3184 subpath = strdup(path);
3185 if (subpath == NULL) {
3186 err = got_error_from_errno("strdup");
3190 err = got_object_id_by_path(&tree_id, s->repo, commit,
3195 err = got_object_open_as_tree(&tree, s->repo, tree_id);
3200 err = tree_view_visit_subtree(s, tree);
3202 got_object_tree_close(tree);
3216 static const struct got_error *
3217 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
3218 struct commit_queue_entry *entry, const char *path,
3219 const char *head_ref_name, struct got_repository *repo)
3221 const struct got_error *err = NULL;
3222 struct tog_tree_view_state *s;
3223 struct tog_view *tree_view;
3225 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
3226 if (tree_view == NULL)
3227 return got_error_from_errno("view_open");
3229 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
3232 s = &tree_view->state.tree;
3234 *new_view = tree_view;
3236 if (got_path_is_root_dir(path))
3239 return tree_view_walk_path(s, entry->commit, path);
3242 static const struct got_error *
3243 block_signals_used_by_main_thread(void)
3248 if (sigemptyset(&sigset) == -1)
3249 return got_error_from_errno("sigemptyset");
3251 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
3252 if (sigaddset(&sigset, SIGWINCH) == -1)
3253 return got_error_from_errno("sigaddset");
3254 if (sigaddset(&sigset, SIGCONT) == -1)
3255 return got_error_from_errno("sigaddset");
3256 if (sigaddset(&sigset, SIGINT) == -1)
3257 return got_error_from_errno("sigaddset");
3258 if (sigaddset(&sigset, SIGTERM) == -1)
3259 return got_error_from_errno("sigaddset");
3261 /* ncurses handles SIGTSTP */
3262 if (sigaddset(&sigset, SIGTSTP) == -1)
3263 return got_error_from_errno("sigaddset");
3265 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
3267 return got_error_set_errno(errcode, "pthread_sigmask");
3273 log_thread(void *arg)
3275 const struct got_error *err = NULL;
3277 struct tog_log_thread_args *a = arg;
3281 * Sync startup with main thread such that we begin our
3282 * work once view_input() has released the mutex.
3284 errcode = pthread_mutex_lock(&tog_mutex);
3286 err = got_error_set_errno(errcode, "pthread_mutex_lock");
3290 err = block_signals_used_by_main_thread();
3292 pthread_mutex_unlock(&tog_mutex);
3296 while (!done && !err && !tog_fatal_signal_received()) {
3297 errcode = pthread_mutex_unlock(&tog_mutex);
3299 err = got_error_set_errno(errcode,
3300 "pthread_mutex_unlock");
3303 err = queue_commits(a);
3305 if (err->code != GOT_ERR_ITER_COMPLETED)
3309 } else if (a->commits_needed > 0 && !a->load_all) {
3312 a->commits_needed--;
3314 a->commits_needed--;
3317 errcode = pthread_mutex_lock(&tog_mutex);
3319 err = got_error_set_errno(errcode,
3320 "pthread_mutex_lock");
3322 } else if (*a->quit)
3324 else if (*a->limiting && *a->first_displayed_entry == NULL) {
3325 *a->first_displayed_entry =
3326 TAILQ_FIRST(&a->limit_commits->head);
3327 *a->selected_entry = *a->first_displayed_entry;
3328 } else if (*a->first_displayed_entry == NULL) {
3329 *a->first_displayed_entry =
3330 TAILQ_FIRST(&a->real_commits->head);
3331 *a->selected_entry = *a->first_displayed_entry;
3334 errcode = pthread_cond_signal(&a->commit_loaded);
3336 err = got_error_set_errno(errcode,
3337 "pthread_cond_signal");
3338 pthread_mutex_unlock(&tog_mutex);
3343 a->commits_needed = 0;
3345 if (a->commits_needed == 0 && !a->load_all) {
3346 errcode = pthread_cond_wait(&a->need_commits,
3349 err = got_error_set_errno(errcode,
3350 "pthread_cond_wait");
3351 pthread_mutex_unlock(&tog_mutex);
3359 a->log_complete = 1;
3360 errcode = pthread_mutex_unlock(&tog_mutex);
3362 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3365 tog_thread_error = 1;
3366 pthread_cond_signal(&a->commit_loaded);
3371 static const struct got_error *
3372 stop_log_thread(struct tog_log_view_state *s)
3374 const struct got_error *err = NULL, *thread_err = NULL;
3379 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3381 return got_error_set_errno(errcode,
3382 "pthread_cond_signal");
3383 errcode = pthread_mutex_unlock(&tog_mutex);
3385 return got_error_set_errno(errcode,
3386 "pthread_mutex_unlock");
3387 errcode = pthread_join(s->thread, (void **)&thread_err);
3389 return got_error_set_errno(errcode, "pthread_join");
3390 errcode = pthread_mutex_lock(&tog_mutex);
3392 return got_error_set_errno(errcode,
3393 "pthread_mutex_lock");
3397 if (s->thread_args.repo) {
3398 err = got_repo_close(s->thread_args.repo);
3399 s->thread_args.repo = NULL;
3402 if (s->thread_args.pack_fds) {
3403 const struct got_error *pack_err =
3404 got_repo_pack_fds_close(s->thread_args.pack_fds);
3407 s->thread_args.pack_fds = NULL;
3410 if (s->thread_args.graph) {
3411 got_commit_graph_close(s->thread_args.graph);
3412 s->thread_args.graph = NULL;
3415 return err ? err : thread_err;
3418 static const struct got_error *
3419 close_log_view(struct tog_view *view)
3421 const struct got_error *err = NULL;
3422 struct tog_log_view_state *s = &view->state.log;
3425 err = stop_log_thread(s);
3427 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3428 if (errcode && err == NULL)
3429 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3431 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3432 if (errcode && err == NULL)
3433 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3435 free_commits(&s->limit_commits);
3436 free_commits(&s->real_commits);
3437 free(s->in_repo_path);
3438 s->in_repo_path = NULL;
3441 free(s->head_ref_name);
3442 s->head_ref_name = NULL;
3447 * We use two queues to implement the limit feature: first consists of
3448 * commits matching the current limit_regex; second is the real queue
3449 * of all known commits (real_commits). When the user starts limiting,
3450 * we swap queues such that all movement and displaying functionality
3451 * works with very slight change.
3453 static const struct got_error *
3454 limit_log_view(struct tog_view *view)
3456 struct tog_log_view_state *s = &view->state.log;
3457 struct commit_queue_entry *entry;
3458 struct tog_view *v = view;
3459 const struct got_error *err = NULL;
3463 if (view_is_hsplit_top(view))
3465 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3468 /* Get the pattern */
3469 wmove(v->window, v->nlines - 1, 0);
3470 wclrtoeol(v->window);
3471 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3472 nodelay(v->window, FALSE);
3475 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3478 nodelay(v->window, TRUE);
3482 if (*pattern == '\0') {
3484 * Safety measure for the situation where the user
3485 * resets limit without previously limiting anything.
3491 * User could have pressed Ctrl+L, which refreshed the
3492 * commit queues, it means we can't save previously
3493 * (before limit took place) displayed entries,
3494 * because they would point to already free'ed memory,
3495 * so we are forced to always select first entry of
3498 s->commits = &s->real_commits;
3499 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3500 s->selected_entry = s->first_displayed_entry;
3507 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3512 /* Clear the screen while loading limit view */
3513 s->first_displayed_entry = NULL;
3514 s->last_displayed_entry = NULL;
3515 s->selected_entry = NULL;
3516 s->commits = &s->limit_commits;
3518 /* Prepare limit queue for new search */
3519 free_commits(&s->limit_commits);
3520 s->limit_commits.ncommits = 0;
3522 /* First process commits, which are in queue already */
3523 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3526 err = match_commit(&have_match, entry->id,
3527 entry->commit, &s->limit_regex);
3532 struct commit_queue_entry *matched;
3534 matched = alloc_commit_queue_entry(entry->commit,
3536 if (matched == NULL) {
3537 err = got_error_from_errno(
3538 "alloc_commit_queue_entry");
3541 matched->commit = entry->commit;
3542 got_object_commit_retain(entry->commit);
3544 matched->idx = s->limit_commits.ncommits;
3545 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3547 s->limit_commits.ncommits++;
3551 /* Second process all the commits, until we fill the screen */
3552 if (s->limit_commits.ncommits < view->nlines - 1 &&
3553 !s->thread_args.log_complete) {
3554 s->thread_args.commits_needed +=
3555 view->nlines - s->limit_commits.ncommits - 1;
3556 err = trigger_log_thread(view, 1);
3561 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3562 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3568 static const struct got_error *
3569 search_start_log_view(struct tog_view *view)
3571 struct tog_log_view_state *s = &view->state.log;
3573 s->matched_entry = NULL;
3574 s->search_entry = NULL;
3578 static const struct got_error *
3579 search_next_log_view(struct tog_view *view)
3581 const struct got_error *err = NULL;
3582 struct tog_log_view_state *s = &view->state.log;
3583 struct commit_queue_entry *entry;
3585 /* Display progress update in log view. */
3586 show_log_view(view);
3590 if (s->search_entry) {
3592 errcode = pthread_mutex_unlock(&tog_mutex);
3594 return got_error_set_errno(errcode,
3595 "pthread_mutex_unlock");
3596 ch = wgetch(view->window);
3597 errcode = pthread_mutex_lock(&tog_mutex);
3599 return got_error_set_errno(errcode,
3600 "pthread_mutex_lock");
3601 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3602 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3605 if (view->searching == TOG_SEARCH_FORWARD)
3606 entry = TAILQ_NEXT(s->search_entry, entry);
3608 entry = TAILQ_PREV(s->search_entry,
3609 commit_queue_head, entry);
3610 } else if (s->matched_entry) {
3612 * If the user has moved the cursor after we hit a match,
3613 * the position from where we should continue searching
3614 * might have changed.
3616 if (view->searching == TOG_SEARCH_FORWARD)
3617 entry = TAILQ_NEXT(s->selected_entry, entry);
3619 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3622 entry = s->selected_entry;
3628 if (entry == NULL) {
3629 if (s->thread_args.log_complete ||
3630 view->searching == TOG_SEARCH_BACKWARD) {
3631 view->search_next_done =
3632 (s->matched_entry == NULL ?
3633 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3634 s->search_entry = NULL;
3638 * Poke the log thread for more commits and return,
3639 * allowing the main loop to make progress. Search
3640 * will resume at s->search_entry once we come back.
3642 s->thread_args.commits_needed++;
3643 return trigger_log_thread(view, 0);
3646 err = match_commit(&have_match, entry->id, entry->commit,
3651 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3652 s->matched_entry = entry;
3656 s->search_entry = entry;
3657 if (view->searching == TOG_SEARCH_FORWARD)
3658 entry = TAILQ_NEXT(entry, entry);
3660 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3663 if (s->matched_entry) {
3664 int cur = s->selected_entry->idx;
3665 while (cur < s->matched_entry->idx) {
3666 err = input_log_view(NULL, view, KEY_DOWN);
3671 while (cur > s->matched_entry->idx) {
3672 err = input_log_view(NULL, view, KEY_UP);
3679 s->search_entry = NULL;
3684 static const struct got_error *
3685 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3686 struct got_repository *repo, const char *head_ref_name,
3687 const char *in_repo_path, int log_branches)
3689 const struct got_error *err = NULL;
3690 struct tog_log_view_state *s = &view->state.log;
3691 struct got_repository *thread_repo = NULL;
3692 struct got_commit_graph *thread_graph = NULL;
3695 if (in_repo_path != s->in_repo_path) {
3696 free(s->in_repo_path);
3697 s->in_repo_path = strdup(in_repo_path);
3698 if (s->in_repo_path == NULL) {
3699 err = got_error_from_errno("strdup");
3704 /* The commit queue only contains commits being displayed. */
3705 TAILQ_INIT(&s->real_commits.head);
3706 s->real_commits.ncommits = 0;
3707 s->commits = &s->real_commits;
3709 TAILQ_INIT(&s->limit_commits.head);
3711 s->limit_commits.ncommits = 0;
3714 if (head_ref_name) {
3715 s->head_ref_name = strdup(head_ref_name);
3716 if (s->head_ref_name == NULL) {
3717 err = got_error_from_errno("strdup");
3721 s->start_id = got_object_id_dup(start_id);
3722 if (s->start_id == NULL) {
3723 err = got_error_from_errno("got_object_id_dup");
3726 s->log_branches = log_branches;
3727 s->use_committer = 1;
3729 STAILQ_INIT(&s->colors);
3730 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3731 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3732 get_color_value("TOG_COLOR_COMMIT"));
3735 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3736 get_color_value("TOG_COLOR_AUTHOR"));
3738 free_colors(&s->colors);
3741 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3742 get_color_value("TOG_COLOR_DATE"));
3744 free_colors(&s->colors);
3749 view->show = show_log_view;
3750 view->input = input_log_view;
3751 view->resize = resize_log_view;
3752 view->close = close_log_view;
3753 view->search_start = search_start_log_view;
3754 view->search_next = search_next_log_view;
3756 if (s->thread_args.pack_fds == NULL) {
3757 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3761 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3762 s->thread_args.pack_fds);
3765 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3769 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3770 s->repo, NULL, NULL);
3774 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3776 err = got_error_set_errno(errcode, "pthread_cond_init");
3779 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3781 err = got_error_set_errno(errcode, "pthread_cond_init");
3785 s->thread_args.commits_needed = view->nlines;
3786 s->thread_args.graph = thread_graph;
3787 s->thread_args.real_commits = &s->real_commits;
3788 s->thread_args.limit_commits = &s->limit_commits;
3789 s->thread_args.in_repo_path = s->in_repo_path;
3790 s->thread_args.start_id = s->start_id;
3791 s->thread_args.repo = thread_repo;
3792 s->thread_args.log_complete = 0;
3793 s->thread_args.quit = &s->quit;
3794 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3795 s->thread_args.selected_entry = &s->selected_entry;
3796 s->thread_args.searching = &view->searching;
3797 s->thread_args.search_next_done = &view->search_next_done;
3798 s->thread_args.regex = &view->regex;
3799 s->thread_args.limiting = &s->limit_view;
3800 s->thread_args.limit_regex = &s->limit_regex;
3801 s->thread_args.limit_commits = &s->limit_commits;
3804 if (view->close == NULL)
3805 close_log_view(view);
3811 static const struct got_error *
3812 show_log_view(struct tog_view *view)
3814 const struct got_error *err;
3815 struct tog_log_view_state *s = &view->state.log;
3817 if (s->thread == NULL) {
3818 int errcode = pthread_create(&s->thread, NULL, log_thread,
3821 return got_error_set_errno(errcode, "pthread_create");
3822 if (s->thread_args.commits_needed > 0) {
3823 err = trigger_log_thread(view, 1);
3829 return draw_commits(view);
3833 log_move_cursor_up(struct tog_view *view, int page, int home)
3835 struct tog_log_view_state *s = &view->state.log;
3837 if (s->first_displayed_entry == NULL)
3839 if (s->selected_entry->idx == 0)
3842 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3844 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3846 if (!page && !home && s->selected > 0)
3849 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3855 static const struct got_error *
3856 log_move_cursor_down(struct tog_view *view, int page)
3858 struct tog_log_view_state *s = &view->state.log;
3859 const struct got_error *err = NULL;
3860 int eos = view->nlines - 2;
3862 if (s->first_displayed_entry == NULL)
3865 if (s->thread_args.log_complete &&
3866 s->selected_entry->idx >= s->commits->ncommits - 1)
3869 if (view_is_hsplit_top(view))
3870 --eos; /* border consumes the last line */
3873 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3876 err = log_scroll_down(view, 1);
3877 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3878 struct commit_queue_entry *entry;
3882 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3883 s->last_displayed_entry = entry;
3884 for (n = 0; n <= eos; n++) {
3887 s->first_displayed_entry = entry;
3888 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3891 s->selected = n - 1;
3893 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3894 s->thread_args.log_complete)
3895 s->selected += MIN(page,
3896 s->commits->ncommits - s->selected_entry->idx - 1);
3898 err = log_scroll_down(view, page);
3904 * We might necessarily overshoot in horizontal
3905 * splits; if so, select the last displayed commit.
3907 if (s->first_displayed_entry && s->last_displayed_entry) {
3908 s->selected = MIN(s->selected,
3909 s->last_displayed_entry->idx -
3910 s->first_displayed_entry->idx);
3915 if (s->thread_args.log_complete &&
3916 s->selected_entry->idx == s->commits->ncommits - 1)
3923 view_get_split(struct tog_view *view, int *y, int *x)
3928 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3929 if (view->child && view->child->resized_y)
3930 *y = view->child->resized_y;
3931 else if (view->resized_y)
3932 *y = view->resized_y;
3934 *y = view_split_begin_y(view->lines);
3935 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3936 if (view->child && view->child->resized_x)
3937 *x = view->child->resized_x;
3938 else if (view->resized_x)
3939 *x = view->resized_x;
3941 *x = view_split_begin_x(view->begin_x);
3945 /* Split view horizontally at y and offset view->state->selected line. */
3946 static const struct got_error *
3947 view_init_hsplit(struct tog_view *view, int y)
3949 const struct got_error *err = NULL;
3953 err = view_resize(view);
3957 err = offset_selection_down(view);
3962 static const struct got_error *
3963 log_goto_line(struct tog_view *view, int nlines)
3965 const struct got_error *err = NULL;
3966 struct tog_log_view_state *s = &view->state.log;
3967 int g, idx = s->selected_entry->idx;
3969 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3975 if (g >= s->first_displayed_entry->idx + 1 &&
3976 g <= s->last_displayed_entry->idx + 1 &&
3977 g - s->first_displayed_entry->idx - 1 < nlines) {
3978 s->selected = g - s->first_displayed_entry->idx - 1;
3984 err = log_move_cursor_down(view, g - idx - 1);
3985 if (!err && g > s->selected_entry->idx + 1)
3986 err = log_move_cursor_down(view,
3987 g - s->first_displayed_entry->idx - 1);
3990 } else if (idx + 1 > g)
3991 log_move_cursor_up(view, idx - g + 1, 0);
3993 if (g < nlines && s->first_displayed_entry->idx == 0)
3994 s->selected = g - 1;
4002 horizontal_scroll_input(struct tog_view *view, int ch)
4008 view->x -= MIN(view->x, 2);
4014 if (view->x + view->ncols / 2 < view->maxx)
4023 view->x = MAX(view->maxx - view->ncols / 2, 0);
4031 static const struct got_error *
4032 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
4034 const struct got_error *err = NULL;
4035 struct tog_log_view_state *s = &view->state.log;
4038 if (s->thread_args.load_all) {
4039 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
4040 s->thread_args.load_all = 0;
4041 else if (s->thread_args.log_complete) {
4042 err = log_move_cursor_down(view, s->commits->ncommits);
4043 s->thread_args.load_all = 0;
4049 eos = nscroll = view->nlines - 1;
4050 if (view_is_hsplit_top(view))
4054 return log_goto_line(view, eos);
4058 err = limit_log_view(view);
4069 horizontal_scroll_input(view, ch);
4076 log_move_cursor_up(view, 0, 0);
4081 log_move_cursor_up(view, 0, 1);
4091 log_move_cursor_up(view, nscroll, 0);
4098 err = log_move_cursor_down(view, 0);
4101 s->use_committer = !s->use_committer;
4102 view->action = s->use_committer ?
4103 "show committer" : "show commit author";
4108 /* We don't know yet how many commits, so we're forced to
4109 * traverse them all. */
4111 s->thread_args.load_all = 1;
4112 if (!s->thread_args.log_complete)
4113 return trigger_log_thread(view, 0);
4114 err = log_move_cursor_down(view, s->commits->ncommits);
4115 s->thread_args.load_all = 0;
4126 err = log_move_cursor_down(view, nscroll);
4129 if (s->selected > view->nlines - 2)
4130 s->selected = view->nlines - 2;
4131 if (s->selected > s->commits->ncommits - 1)
4132 s->selected = s->commits->ncommits - 1;
4134 if (s->commits->ncommits < view->nlines - 1 &&
4135 !s->thread_args.log_complete) {
4136 s->thread_args.commits_needed += (view->nlines - 1) -
4137 s->commits->ncommits;
4138 err = trigger_log_thread(view, 1);
4144 if (s->selected_entry == NULL)
4146 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4150 if (s->selected_entry == NULL)
4152 err = view_request_new(new_view, view, TOG_VIEW_TREE);
4158 if (ch == KEY_BACKSPACE &&
4159 got_path_is_root_dir(s->in_repo_path))
4161 err = stop_log_thread(s);
4164 if (ch == KEY_BACKSPACE) {
4166 err = got_path_dirname(&parent_path, s->in_repo_path);
4169 free(s->in_repo_path);
4170 s->in_repo_path = parent_path;
4171 s->thread_args.in_repo_path = s->in_repo_path;
4172 } else if (ch == CTRL('l')) {
4173 struct got_object_id *start_id;
4174 err = got_repo_match_object_id(&start_id, NULL,
4175 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4176 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4178 if (s->head_ref_name == NULL ||
4179 err->code != GOT_ERR_NOT_REF)
4181 /* Try to cope with deleted references. */
4182 free(s->head_ref_name);
4183 s->head_ref_name = NULL;
4184 err = got_repo_match_object_id(&start_id,
4185 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4186 &tog_refs, s->repo);
4191 s->start_id = start_id;
4192 s->thread_args.start_id = s->start_id;
4194 s->log_branches = !s->log_branches;
4196 if (s->thread_args.pack_fds == NULL) {
4197 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4201 err = got_repo_open(&s->thread_args.repo,
4202 got_repo_get_path(s->repo), NULL,
4203 s->thread_args.pack_fds);
4207 err = tog_load_refs(s->repo, 0);
4210 err = got_commit_graph_open(&s->thread_args.graph,
4211 s->in_repo_path, !s->log_branches);
4214 err = got_commit_graph_iter_start(s->thread_args.graph,
4215 s->start_id, s->repo, NULL, NULL);
4218 free_commits(&s->real_commits);
4219 free_commits(&s->limit_commits);
4220 s->first_displayed_entry = NULL;
4221 s->last_displayed_entry = NULL;
4222 s->selected_entry = NULL;
4224 s->thread_args.log_complete = 0;
4226 s->thread_args.commits_needed = view->lines;
4227 s->matched_entry = NULL;
4228 s->search_entry = NULL;
4233 err = view_request_new(new_view, view, TOG_VIEW_REF);
4243 static const struct got_error *
4244 apply_unveil(const char *repo_path, const char *worktree_path)
4246 const struct got_error *error;
4249 if (unveil("gmon.out", "rwc") != 0)
4250 return got_error_from_errno2("unveil", "gmon.out");
4252 if (repo_path && unveil(repo_path, "r") != 0)
4253 return got_error_from_errno2("unveil", repo_path);
4255 if (worktree_path && unveil(worktree_path, "rwc") != 0)
4256 return got_error_from_errno2("unveil", worktree_path);
4258 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4259 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4261 error = got_privsep_unveil_exec_helpers();
4265 if (unveil(NULL, NULL) != 0)
4266 return got_error_from_errno("unveil");
4271 static const struct got_error *
4272 init_mock_term(const char *test_script_path)
4274 const struct got_error *err = NULL;
4275 const char *screen_dump_path;
4278 if (test_script_path == NULL || *test_script_path == '\0')
4279 return got_error_msg(GOT_ERR_IO, "TOG_TEST_SCRIPT not defined");
4281 tog_io.f = fopen(test_script_path, "re");
4282 if (tog_io.f == NULL) {
4283 err = got_error_from_errno_fmt("fopen: %s",
4288 /* test mode, we don't want any output */
4289 tog_io.cout = fopen("/dev/null", "w+");
4290 if (tog_io.cout == NULL) {
4291 err = got_error_from_errno2("fopen", "/dev/null");
4295 in = dup(fileno(tog_io.cout));
4297 err = got_error_from_errno("dup");
4300 tog_io.cin = fdopen(in, "r");
4301 if (tog_io.cin == NULL) {
4302 err = got_error_from_errno("fdopen");
4307 screen_dump_path = getenv("TOG_SCR_DUMP");
4308 if (screen_dump_path == NULL || *screen_dump_path == '\0')
4309 return got_error_msg(GOT_ERR_IO, "TOG_SCR_DUMP not defined");
4310 tog_io.sdump = fopen(screen_dump_path, "we");
4311 if (tog_io.sdump == NULL) {
4312 err = got_error_from_errno2("fopen", screen_dump_path);
4316 if (fseeko(tog_io.f, 0L, SEEK_SET) == -1) {
4317 err = got_error_from_errno("fseeko");
4321 if (newterm(NULL, tog_io.cout, tog_io.cin) == NULL)
4322 err = got_error_msg(GOT_ERR_IO,
4323 "newterm: failed to initialise curses");
4337 * Override default signal handlers before starting ncurses.
4338 * This should prevent ncurses from installing its own
4339 * broken cleanup() signal handler.
4341 signal(SIGWINCH, tog_sigwinch);
4342 signal(SIGPIPE, tog_sigpipe);
4343 signal(SIGCONT, tog_sigcont);
4344 signal(SIGINT, tog_sigint);
4345 signal(SIGTERM, tog_sigterm);
4347 if (using_mock_io) /* In test mode we use a fake terminal */
4353 halfdelay(1); /* Fast refresh while initial view is loading. */
4356 intrflush(stdscr, FALSE);
4357 keypad(stdscr, TRUE);
4359 if (getenv("TOG_COLORS") != NULL) {
4361 use_default_colors();
4367 static const struct got_error *
4368 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4369 struct got_repository *repo, struct got_worktree *worktree)
4371 const struct got_error *err = NULL;
4374 *in_repo_path = strdup("/");
4375 if (*in_repo_path == NULL)
4376 return got_error_from_errno("strdup");
4381 const char *prefix = got_worktree_get_path_prefix(worktree);
4384 err = got_worktree_resolve_path(&p, worktree, argv[0]);
4387 if (asprintf(in_repo_path, "%s%s%s", prefix,
4388 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4390 err = got_error_from_errno("asprintf");
4391 *in_repo_path = NULL;
4395 err = got_repo_map_path(in_repo_path, repo, argv[0]);
4400 static const struct got_error *
4401 cmd_log(int argc, char *argv[])
4403 const struct got_error *error;
4404 struct got_repository *repo = NULL;
4405 struct got_worktree *worktree = NULL;
4406 struct got_object_id *start_id = NULL;
4407 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4408 char *keyword_idstr = NULL, *start_commit = NULL, *label = NULL;
4409 struct got_reference *ref = NULL;
4410 const char *head_ref_name = NULL;
4411 int ch, log_branches = 0;
4412 struct tog_view *view;
4413 int *pack_fds = NULL;
4415 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4421 start_commit = optarg;
4424 repo_path = realpath(optarg, NULL);
4425 if (repo_path == NULL)
4426 return got_error_from_errno2("realpath",
4441 error = got_repo_pack_fds_open(&pack_fds);
4445 if (repo_path == NULL) {
4446 cwd = getcwd(NULL, 0);
4448 return got_error_from_errno("getcwd");
4449 error = got_worktree_open(&worktree, cwd, NULL);
4450 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4454 strdup(got_worktree_get_repo_path(worktree));
4456 repo_path = strdup(cwd);
4457 if (repo_path == NULL) {
4458 error = got_error_from_errno("strdup");
4463 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4467 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4474 error = apply_unveil(got_repo_get_path(repo),
4475 worktree ? got_worktree_get_root_path(worktree) : NULL);
4479 /* already loaded by tog_log_with_path()? */
4480 if (TAILQ_EMPTY(&tog_refs)) {
4481 error = tog_load_refs(repo, 0);
4486 if (start_commit == NULL) {
4487 error = got_repo_match_object_id(&start_id, &label,
4488 worktree ? got_worktree_get_head_ref_name(worktree) :
4489 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4492 head_ref_name = label;
4494 error = got_keyword_to_idstr(&keyword_idstr, start_commit,
4498 if (keyword_idstr != NULL)
4499 start_commit = keyword_idstr;
4501 error = got_ref_open(&ref, repo, start_commit, 0);
4503 head_ref_name = got_ref_get_name(ref);
4504 else if (error->code != GOT_ERR_NOT_REF)
4506 error = got_repo_match_object_id(&start_id, NULL,
4507 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4512 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4514 error = got_error_from_errno("view_open");
4517 error = open_log_view(view, start_id, repo, head_ref_name,
4518 in_repo_path, log_branches);
4522 /* Release work tree lock. */
4523 got_worktree_close(worktree);
4526 error = view_loop(view);
4528 free(keyword_idstr);
4537 const struct got_error *close_err = got_repo_close(repo);
4542 got_worktree_close(worktree);
4544 const struct got_error *pack_err =
4545 got_repo_pack_fds_close(pack_fds);
4557 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4558 "object1 object2\n", getprogname());
4563 match_line(const char *line, regex_t *regex, size_t nmatch,
4564 regmatch_t *regmatch)
4566 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4569 static struct tog_color *
4570 match_color(struct tog_colors *colors, const char *line)
4572 struct tog_color *tc = NULL;
4574 STAILQ_FOREACH(tc, colors, entry) {
4575 if (match_line(line, &tc->regex, 0, NULL))
4582 static const struct got_error *
4583 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4584 WINDOW *window, int skipcol, regmatch_t *regmatch)
4586 const struct got_error *err = NULL;
4588 wchar_t *wline = NULL;
4589 int rme, rms, n, width, scrollx;
4590 int width0 = 0, width1 = 0, width2 = 0;
4591 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4595 rms = regmatch->rm_so;
4596 rme = regmatch->rm_eo;
4598 err = expand_tab(&exstr, line);
4602 /* Split the line into 3 segments, according to match offsets. */
4603 seg0 = strndup(exstr, rms);
4605 err = got_error_from_errno("strndup");
4608 seg1 = strndup(exstr + rms, rme - rms);
4610 err = got_error_from_errno("strndup");
4613 seg2 = strdup(exstr + rme);
4615 err = got_error_from_errno("strndup");
4619 /* draw up to matched token if we haven't scrolled past it */
4620 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4624 n = MAX(width0 - skipcol, 0);
4627 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4628 wlimit, col_tab_align, 1);
4631 waddwstr(window, &wline[scrollx]);
4641 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4645 wlen = wcslen(wline);
4647 width = wcwidth(wline[i]);
4649 /* should not happen, tabs are expanded */
4650 err = got_error(GOT_ERR_RANGE);
4653 if (width0 + w + width > skipcol)
4658 /* draw (visible part of) matched token (if scrolled into it) */
4659 if (width1 - w > 0) {
4660 wattron(window, A_STANDOUT);
4661 waddwstr(window, &wline[i]);
4662 wattroff(window, A_STANDOUT);
4663 wlimit -= (width1 - w);
4664 *wtotal += (width1 - w);
4668 if (wlimit > 0) { /* draw rest of line */
4670 if (skipcol > width0 + width1) {
4671 err = format_line(&wline, &width2, &scrollx, seg2,
4672 skipcol - (width0 + width1), wlimit,
4676 waddwstr(window, &wline[scrollx]);
4678 err = format_line(&wline, &width2, NULL, seg2, 0,
4679 wlimit, col_tab_align, 1);
4682 waddwstr(window, wline);
4696 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4699 int *eof, *first, *selected;
4701 if (view->type == TOG_VIEW_DIFF) {
4702 struct tog_diff_view_state *s = &view->state.diff;
4704 first = &s->first_displayed_line;
4708 } else if (view->type == TOG_VIEW_HELP) {
4709 struct tog_help_view_state *s = &view->state.help;
4711 first = &s->first_displayed_line;
4715 } else if (view->type == TOG_VIEW_BLAME) {
4716 struct tog_blame_view_state *s = &view->state.blame;
4718 first = &s->first_displayed_line;
4719 selected = &s->selected_line;
4725 /* Center gline in the middle of the page like vi(1). */
4726 if (*lineno < view->gline - (view->nlines - 3) / 2)
4728 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4737 *selected = view->gline <= (view->nlines - 3) / 2 ?
4738 view->gline : (view->nlines - 3) / 2 + 1;
4744 static const struct got_error *
4745 draw_file(struct tog_view *view, const char *header)
4747 struct tog_diff_view_state *s = &view->state.diff;
4748 regmatch_t *regmatch = &view->regmatch;
4749 const struct got_error *err;
4752 size_t linesize = 0;
4756 int max_lines = view->nlines;
4757 int nlines = s->nlines;
4760 s->lineno = s->first_displayed_line - 1;
4761 line_offset = s->lines[s->first_displayed_line - 1].offset;
4762 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4763 return got_error_from_errno("fseek");
4765 werase(view->window);
4767 if (view->gline > s->nlines - 1)
4768 view->gline = s->nlines - 1;
4771 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4772 1 : view->gline - (view->nlines - 3) / 2 :
4773 s->lineno + s->selected_line;
4775 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4776 return got_error_from_errno("asprintf");
4777 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4783 if (view_needs_focus_indication(view))
4784 wstandout(view->window);
4785 waddwstr(view->window, wline);
4788 while (width++ < view->ncols)
4789 waddch(view->window, ' ');
4790 if (view_needs_focus_indication(view))
4791 wstandend(view->window);
4801 while (max_lines > 0 && nprinted < max_lines) {
4802 enum got_diff_line_type linetype;
4805 linelen = getline(&line, &linesize, s->f);
4806 if (linelen == -1) {
4812 return got_ferror(s->f, GOT_ERR_IO);
4815 if (++s->lineno < s->first_displayed_line)
4817 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4819 if (s->lineno == view->hiline)
4822 /* Set view->maxx based on full line length. */
4823 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4829 view->maxx = MAX(view->maxx, width);
4833 linetype = s->lines[s->lineno].type;
4834 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4835 linetype < GOT_DIFF_LINE_CONTEXT)
4836 attr |= COLOR_PAIR(linetype);
4838 wattron(view->window, attr);
4839 if (s->first_displayed_line + nprinted == s->matched_line &&
4840 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4841 err = add_matched_line(&width, line, view->ncols, 0,
4842 view->window, view->x, regmatch);
4849 err = format_line(&wline, &width, &skip, line,
4850 view->x, view->ncols, 0, view->x ? 1 : 0);
4855 waddwstr(view->window, &wline[skip]);
4859 if (s->lineno == view->hiline) {
4860 /* highlight full gline length */
4861 while (width++ < view->ncols)
4862 waddch(view->window, ' ');
4864 if (width <= view->ncols - 1)
4865 waddch(view->window, '\n');
4868 wattroff(view->window, attr);
4869 if (++nprinted == 1)
4870 s->first_displayed_line = s->lineno;
4874 s->last_displayed_line = s->first_displayed_line +
4877 s->last_displayed_line = s->first_displayed_line;
4882 while (nprinted < view->nlines) {
4883 waddch(view->window, '\n');
4887 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4893 wstandout(view->window);
4894 waddwstr(view->window, wline);
4897 wstandend(view->window);
4904 get_datestr(time_t *time, char *datebuf)
4906 struct tm mytm, *tm;
4909 tm = gmtime_r(time, &mytm);
4912 s = asctime_r(tm, datebuf);
4915 p = strchr(s, '\n');
4921 static const struct got_error *
4922 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4923 off_t off, uint8_t type)
4925 struct got_diff_line *p;
4927 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4929 return got_error_from_errno("reallocarray");
4931 (*lines)[*nlines].offset = off;
4932 (*lines)[*nlines].type = type;
4938 static const struct got_error *
4939 cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4940 struct got_diff_line *s_lines, size_t s_nlines)
4942 struct got_diff_line *p;
4946 if (fseeko(src, 0L, SEEK_SET) == -1)
4947 return got_error_from_errno("fseeko");
4950 r = fread(buf, 1, sizeof(buf), src);
4953 return got_error_from_errno("fread");
4957 if (fwrite(buf, 1, r, dst) != r)
4958 return got_ferror(dst, GOT_ERR_IO);
4961 if (s_nlines == 0 && *d_nlines == 0)
4965 * If commit info was in dst, increment line offsets
4966 * of the appended diff content, but skip s_lines[0]
4967 * because offset zero is already in *d_lines.
4969 if (*d_nlines > 0) {
4970 for (i = 1; i < s_nlines; ++i)
4971 s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4979 p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4981 /* d_lines is freed in close_diff_view() */
4982 return got_error_from_errno("reallocarray");
4987 memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
4988 *d_nlines += s_nlines;
4993 static const struct got_error *
4994 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4995 struct got_object_id *commit_id, struct got_reflist_head *refs,
4996 struct got_repository *repo, int ignore_ws, int force_text_diff,
4997 struct got_diffstat_cb_arg *dsa, FILE *outfile)
4999 const struct got_error *err = NULL;
5000 char datebuf[26], *datestr;
5001 struct got_commit_object *commit;
5002 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
5003 time_t committer_time;
5004 const char *author, *committer;
5005 char *refs_str = NULL;
5006 struct got_pathlist_entry *pe;
5010 err = build_refs_str(&refs_str, refs, commit_id, repo);
5014 err = got_object_open_as_commit(&commit, repo, commit_id);
5018 err = got_object_id_str(&id_str, commit_id);
5020 err = got_error_from_errno("got_object_id_str");
5024 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
5028 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
5029 refs_str ? refs_str : "", refs_str ? ")" : "");
5031 err = got_error_from_errno("fprintf");
5035 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
5039 n = fprintf(outfile, "from: %s\n",
5040 got_object_commit_get_author(commit));
5042 err = got_error_from_errno("fprintf");
5046 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
5050 author = got_object_commit_get_author(commit);
5051 committer = got_object_commit_get_committer(commit);
5052 if (strcmp(author, committer) != 0) {
5053 n = fprintf(outfile, "via: %s\n", committer);
5055 err = got_error_from_errno("fprintf");
5059 err = add_line_metadata(lines, nlines, outoff,
5060 GOT_DIFF_LINE_AUTHOR);
5064 committer_time = got_object_commit_get_committer_time(commit);
5065 datestr = get_datestr(&committer_time, datebuf);
5067 n = fprintf(outfile, "date: %s UTC\n", datestr);
5069 err = got_error_from_errno("fprintf");
5073 err = add_line_metadata(lines, nlines, outoff,
5074 GOT_DIFF_LINE_DATE);
5078 if (got_object_commit_get_nparents(commit) > 1) {
5079 const struct got_object_id_queue *parent_ids;
5080 struct got_object_qid *qid;
5082 parent_ids = got_object_commit_get_parent_ids(commit);
5083 STAILQ_FOREACH(qid, parent_ids, entry) {
5084 err = got_object_id_str(&id_str, &qid->id);
5087 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
5089 err = got_error_from_errno("fprintf");
5093 err = add_line_metadata(lines, nlines, outoff,
5094 GOT_DIFF_LINE_META);
5102 err = got_object_commit_get_logmsg(&logmsg, commit);
5106 while ((line = strsep(&s, "\n")) != NULL) {
5107 n = fprintf(outfile, "%s\n", line);
5109 err = got_error_from_errno("fprintf");
5113 err = add_line_metadata(lines, nlines, outoff,
5114 GOT_DIFF_LINE_LOGMSG);
5119 TAILQ_FOREACH(pe, dsa->paths, entry) {
5120 struct got_diff_changed_path *cp = pe->data;
5121 int pad = dsa->max_path_len - pe->path_len + 1;
5123 n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
5124 pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
5125 dsa->rm_cols + 1, cp->rm);
5127 err = got_error_from_errno("fprintf");
5131 err = add_line_metadata(lines, nlines, outoff,
5132 GOT_DIFF_LINE_CHANGES);
5137 fputc('\n', outfile);
5139 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5143 n = fprintf(outfile,
5144 "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5145 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5146 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5148 err = got_error_from_errno("fprintf");
5152 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5156 fputc('\n', outfile);
5158 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5163 got_object_commit_close(commit);
5172 static const struct got_error *
5173 create_diff(struct tog_diff_view_state *s)
5175 const struct got_error *err = NULL;
5176 FILE *f = NULL, *tmp_diff_file = NULL;
5178 struct got_diff_line *lines = NULL;
5179 struct got_pathlist_head changed_paths;
5181 TAILQ_INIT(&changed_paths);
5184 s->lines = malloc(sizeof(*s->lines));
5185 if (s->lines == NULL)
5186 return got_error_from_errno("malloc");
5191 err = got_error_from_errno("got_opentemp");
5194 tmp_diff_file = got_opentemp();
5195 if (tmp_diff_file == NULL) {
5196 err = got_error_from_errno("got_opentemp");
5199 if (s->f && fclose(s->f) == EOF) {
5200 err = got_error_from_errno("fclose");
5206 err = got_object_get_type(&obj_type, s->repo, s->id1);
5208 err = got_object_get_type(&obj_type, s->repo, s->id2);
5213 case GOT_OBJ_TYPE_BLOB:
5214 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5215 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5216 s->label1, s->label2, tog_diff_algo, s->diff_context,
5217 s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5220 case GOT_OBJ_TYPE_TREE:
5221 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5222 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5223 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5224 s->force_text_diff, NULL, s->repo, s->f);
5226 case GOT_OBJ_TYPE_COMMIT: {
5227 const struct got_object_id_queue *parent_ids;
5228 struct got_object_qid *pid;
5229 struct got_commit_object *commit2;
5230 struct got_reflist_head *refs;
5232 struct got_diffstat_cb_arg dsa = {
5235 s->ignore_whitespace,
5240 lines = malloc(sizeof(*lines));
5241 if (lines == NULL) {
5242 err = got_error_from_errno("malloc");
5246 /* build diff first in tmp file then append to commit info */
5247 err = got_diff_objects_as_commits(&lines, &nlines,
5248 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5249 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5250 s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5254 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
5257 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5258 /* Show commit info if we're diffing to a parent/root commit. */
5259 if (s->id1 == NULL) {
5260 err = write_commit_info(&s->lines, &s->nlines, s->id2,
5261 refs, s->repo, s->ignore_whitespace,
5262 s->force_text_diff, &dsa, s->f);
5266 parent_ids = got_object_commit_get_parent_ids(commit2);
5267 STAILQ_FOREACH(pid, parent_ids, entry) {
5268 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5269 err = write_commit_info(&s->lines,
5270 &s->nlines, s->id2, refs, s->repo,
5271 s->ignore_whitespace,
5272 s->force_text_diff, &dsa, s->f);
5279 got_object_commit_close(commit2);
5281 err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5286 err = got_error(GOT_ERR_OBJ_TYPE);
5291 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5292 if (s->f && fflush(s->f) != 0 && err == NULL)
5293 err = got_error_from_errno("fflush");
5294 if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5295 err = got_error_from_errno("fclose");
5300 diff_view_indicate_progress(struct tog_view *view)
5302 mvwaddstr(view->window, 0, 0, "diffing...");
5307 static const struct got_error *
5308 search_start_diff_view(struct tog_view *view)
5310 struct tog_diff_view_state *s = &view->state.diff;
5312 s->matched_line = 0;
5317 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5318 size_t *nlines, int **first, int **last, int **match, int **selected)
5320 struct tog_diff_view_state *s = &view->state.diff;
5323 *nlines = s->nlines;
5324 *line_offsets = NULL;
5325 *match = &s->matched_line;
5326 *first = &s->first_displayed_line;
5327 *last = &s->last_displayed_line;
5328 *selected = &s->selected_line;
5331 static const struct got_error *
5332 search_next_view_match(struct tog_view *view)
5334 const struct got_error *err = NULL;
5338 size_t linesize = 0;
5340 off_t *line_offsets;
5342 int *first, *last, *match, *selected;
5344 if (!view->search_setup)
5345 return got_error_msg(GOT_ERR_NOT_IMPL,
5346 "view search not supported");
5347 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5350 if (!view->searching) {
5351 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5356 if (view->searching == TOG_SEARCH_FORWARD)
5357 lineno = *first + 1;
5359 lineno = *first - 1;
5361 lineno = *first - 1 + *selected;
5366 if (lineno <= 0 || lineno > nlines) {
5368 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5372 if (view->searching == TOG_SEARCH_FORWARD)
5378 offset = view->type == TOG_VIEW_DIFF ?
5379 view->state.diff.lines[lineno - 1].offset :
5380 line_offsets[lineno - 1];
5381 if (fseeko(f, offset, SEEK_SET) != 0) {
5383 return got_error_from_errno("fseeko");
5385 linelen = getline(&line, &linesize, f);
5386 if (linelen != -1) {
5388 err = expand_tab(&exstr, line);
5391 if (match_line(exstr, &view->regex, 1,
5393 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5400 if (view->searching == TOG_SEARCH_FORWARD)
5415 static const struct got_error *
5416 close_diff_view(struct tog_view *view)
5418 const struct got_error *err = NULL;
5419 struct tog_diff_view_state *s = &view->state.diff;
5425 if (s->f && fclose(s->f) == EOF)
5426 err = got_error_from_errno("fclose");
5428 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5429 err = got_error_from_errno("fclose");
5431 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5432 err = got_error_from_errno("fclose");
5434 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5435 err = got_error_from_errno("close");
5437 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5438 err = got_error_from_errno("close");
5446 static const struct got_error *
5447 open_diff_view(struct tog_view *view, struct got_object_id *id1,
5448 struct got_object_id *id2, const char *label1, const char *label2,
5449 int diff_context, int ignore_whitespace, int force_text_diff,
5450 struct tog_view *parent_view, struct got_repository *repo)
5452 const struct got_error *err;
5453 struct tog_diff_view_state *s = &view->state.diff;
5455 memset(s, 0, sizeof(*s));
5459 if (id1 != NULL && id2 != NULL) {
5462 err = got_object_get_type(&type1, repo, id1);
5465 err = got_object_get_type(&type2, repo, id2);
5469 if (type1 != type2) {
5470 err = got_error(GOT_ERR_OBJ_TYPE);
5474 s->first_displayed_line = 1;
5475 s->last_displayed_line = view->nlines;
5476 s->selected_line = 1;
5484 s->id1 = got_object_id_dup(id1);
5485 if (s->id1 == NULL) {
5486 err = got_error_from_errno("got_object_id_dup");
5492 s->id2 = got_object_id_dup(id2);
5493 if (s->id2 == NULL) {
5494 err = got_error_from_errno("got_object_id_dup");
5498 s->f1 = got_opentemp();
5499 if (s->f1 == NULL) {
5500 err = got_error_from_errno("got_opentemp");
5504 s->f2 = got_opentemp();
5505 if (s->f2 == NULL) {
5506 err = got_error_from_errno("got_opentemp");
5510 s->fd1 = got_opentempfd();
5512 err = got_error_from_errno("got_opentempfd");
5516 s->fd2 = got_opentempfd();
5518 err = got_error_from_errno("got_opentempfd");
5522 s->diff_context = diff_context;
5523 s->ignore_whitespace = ignore_whitespace;
5524 s->force_text_diff = force_text_diff;
5525 s->parent_view = parent_view;
5528 if (has_colors() && getenv("TOG_COLORS") != NULL && !using_mock_io) {
5531 rc = init_pair(GOT_DIFF_LINE_MINUS,
5532 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5534 rc = init_pair(GOT_DIFF_LINE_PLUS,
5535 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5537 rc = init_pair(GOT_DIFF_LINE_HUNK,
5538 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5540 rc = init_pair(GOT_DIFF_LINE_META,
5541 get_color_value("TOG_COLOR_DIFF_META"), -1);
5543 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5544 get_color_value("TOG_COLOR_DIFF_META"), -1);
5546 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5547 get_color_value("TOG_COLOR_DIFF_META"), -1);
5549 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5550 get_color_value("TOG_COLOR_DIFF_META"), -1);
5552 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5553 get_color_value("TOG_COLOR_AUTHOR"), -1);
5555 rc = init_pair(GOT_DIFF_LINE_DATE,
5556 get_color_value("TOG_COLOR_DATE"), -1);
5558 err = got_error(GOT_ERR_RANGE);
5563 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5564 view_is_splitscreen(view))
5565 show_log_view(parent_view); /* draw border */
5566 diff_view_indicate_progress(view);
5568 err = create_diff(s);
5570 view->show = show_diff_view;
5571 view->input = input_diff_view;
5572 view->reset = reset_diff_view;
5573 view->close = close_diff_view;
5574 view->search_start = search_start_diff_view;
5575 view->search_setup = search_setup_diff_view;
5576 view->search_next = search_next_view_match;
5579 if (view->close == NULL)
5580 close_diff_view(view);
5586 static const struct got_error *
5587 show_diff_view(struct tog_view *view)
5589 const struct got_error *err;
5590 struct tog_diff_view_state *s = &view->state.diff;
5591 char *id_str1 = NULL, *id_str2, *header;
5592 const char *label1, *label2;
5595 err = got_object_id_str(&id_str1, s->id1);
5598 label1 = s->label1 ? s->label1 : id_str1;
5600 label1 = "/dev/null";
5602 err = got_object_id_str(&id_str2, s->id2);
5605 label2 = s->label2 ? s->label2 : id_str2;
5607 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5608 err = got_error_from_errno("asprintf");
5616 err = draw_file(view, header);
5621 static const struct got_error *
5622 set_selected_commit(struct tog_diff_view_state *s,
5623 struct commit_queue_entry *entry)
5625 const struct got_error *err;
5626 const struct got_object_id_queue *parent_ids;
5627 struct got_commit_object *selected_commit;
5628 struct got_object_qid *pid;
5631 s->id2 = got_object_id_dup(entry->id);
5633 return got_error_from_errno("got_object_id_dup");
5635 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5638 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5640 pid = STAILQ_FIRST(parent_ids);
5641 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5642 got_object_commit_close(selected_commit);
5646 static const struct got_error *
5647 reset_diff_view(struct tog_view *view)
5649 struct tog_diff_view_state *s = &view->state.diff;
5652 wclear(view->window);
5653 s->first_displayed_line = 1;
5654 s->last_displayed_line = view->nlines;
5655 s->matched_line = 0;
5656 diff_view_indicate_progress(view);
5657 return create_diff(s);
5661 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5665 i = start = s->first_displayed_line - 1;
5667 while (s->lines[i].type != type) {
5671 return; /* do nothing, requested type not in file */
5674 s->selected_line = 1;
5675 s->first_displayed_line = i;
5679 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5683 i = start = s->first_displayed_line + 1;
5685 while (s->lines[i].type != type) {
5686 if (i == s->nlines - 1)
5689 return; /* do nothing, requested type not in file */
5692 s->selected_line = 1;
5693 s->first_displayed_line = i;
5696 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5698 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5701 static const struct got_error *
5702 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5704 const struct got_error *err = NULL;
5705 struct tog_diff_view_state *s = &view->state.diff;
5706 struct tog_log_view_state *ls;
5707 struct commit_queue_entry *old_selected_entry;
5709 size_t linesize = 0;
5711 int i, nscroll = view->nlines - 1, up = 0;
5713 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5722 horizontal_scroll_input(view, ch);
5727 s->force_text_diff = !s->force_text_diff;
5728 view->action = s->force_text_diff ?
5729 "force ASCII text enabled" :
5730 "force ASCII text disabled";
5732 else if (ch == 'w') {
5733 s->ignore_whitespace = !s->ignore_whitespace;
5734 view->action = s->ignore_whitespace ?
5735 "ignore whitespace enabled" :
5736 "ignore whitespace disabled";
5738 err = reset_diff_view(view);
5742 s->first_displayed_line = 1;
5751 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5757 if (s->first_displayed_line > 1)
5758 s->first_displayed_line--;
5769 if (s->first_displayed_line == 1) {
5774 while (i++ < nscroll && s->first_displayed_line > 1)
5775 s->first_displayed_line--;
5781 s->first_displayed_line++;
5798 while (!s->eof && i++ < nscroll) {
5799 linelen = getline(&line, &linesize, s->f);
5800 s->first_displayed_line++;
5801 if (linelen == -1) {
5805 err = got_ferror(s->f, GOT_ERR_IO);
5812 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5815 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5818 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5821 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5824 if (s->diff_context > 0) {
5826 s->matched_line = 0;
5827 diff_view_indicate_progress(view);
5828 err = create_diff(s);
5829 if (s->first_displayed_line + view->nlines - 1 >
5831 s->first_displayed_line = 1;
5832 s->last_displayed_line = view->nlines;
5838 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5840 s->matched_line = 0;
5841 diff_view_indicate_progress(view);
5842 err = create_diff(s);
5854 if (s->parent_view == NULL) {
5858 s->parent_view->count = view->count;
5860 if (s->parent_view->type == TOG_VIEW_LOG) {
5861 ls = &s->parent_view->state.log;
5862 old_selected_entry = ls->selected_entry;
5864 err = input_log_view(NULL, s->parent_view,
5865 up ? KEY_UP : KEY_DOWN);
5868 view->count = s->parent_view->count;
5870 if (old_selected_entry == ls->selected_entry)
5873 err = set_selected_commit(s, ls->selected_entry);
5876 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5877 struct tog_blame_view_state *bs;
5878 struct got_object_id *id, *prev_id;
5880 bs = &s->parent_view->state.blame;
5881 prev_id = get_annotation_for_line(bs->blame.lines,
5882 bs->blame.nlines, bs->last_diffed_line);
5884 err = input_blame_view(&view, s->parent_view,
5885 up ? KEY_UP : KEY_DOWN);
5888 view->count = s->parent_view->count;
5890 if (prev_id == NULL)
5892 id = get_selected_commit_id(bs->blame.lines,
5893 bs->blame.nlines, bs->first_displayed_line,
5898 if (!got_object_id_cmp(prev_id, id))
5901 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5905 s->first_displayed_line = 1;
5906 s->last_displayed_line = view->nlines;
5907 s->matched_line = 0;
5910 diff_view_indicate_progress(view);
5911 err = create_diff(s);
5921 static const struct got_error *
5922 cmd_diff(int argc, char *argv[])
5924 const struct got_error *error;
5925 struct got_repository *repo = NULL;
5926 struct got_worktree *worktree = NULL;
5927 struct got_object_id *id1 = NULL, *id2 = NULL;
5928 char *repo_path = NULL, *cwd = NULL;
5929 char *id_str1 = NULL, *id_str2 = NULL;
5930 char *keyword_idstr1 = NULL, *keyword_idstr2 = NULL;
5931 char *label1 = NULL, *label2 = NULL;
5932 int diff_context = 3, ignore_whitespace = 0;
5933 int ch, force_text_diff = 0;
5935 struct tog_view *view;
5936 int *pack_fds = NULL;
5938 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5941 force_text_diff = 1;
5944 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5947 errx(1, "number of context lines is %s: %s",
5951 repo_path = realpath(optarg, NULL);
5952 if (repo_path == NULL)
5953 return got_error_from_errno2("realpath",
5955 got_path_strip_trailing_slashes(repo_path);
5958 ignore_whitespace = 1;
5970 usage_diff(); /* TODO show local worktree changes */
5971 } else if (argc == 2) {
5977 error = got_repo_pack_fds_open(&pack_fds);
5981 if (repo_path == NULL) {
5982 cwd = getcwd(NULL, 0);
5984 return got_error_from_errno("getcwd");
5985 error = got_worktree_open(&worktree, cwd, NULL);
5986 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5990 strdup(got_worktree_get_repo_path(worktree));
5992 repo_path = strdup(cwd);
5993 if (repo_path == NULL) {
5994 error = got_error_from_errno("strdup");
5999 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6005 error = apply_unveil(got_repo_get_path(repo), NULL);
6009 error = tog_load_refs(repo, 0);
6013 if (id_str1 != NULL) {
6014 error = got_keyword_to_idstr(&keyword_idstr1, id_str1,
6018 if (keyword_idstr1 != NULL)
6019 id_str1 = keyword_idstr1;
6021 if (id_str2 != NULL) {
6022 error = got_keyword_to_idstr(&keyword_idstr2, id_str2,
6026 if (keyword_idstr2 != NULL)
6027 id_str2 = keyword_idstr2;
6030 error = got_repo_match_object_id(&id1, &label1, id_str1,
6031 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
6035 error = got_repo_match_object_id(&id2, &label2, id_str2,
6036 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
6040 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
6042 error = got_error_from_errno("view_open");
6045 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
6046 ignore_whitespace, force_text_diff, NULL, repo);
6049 error = view_loop(view);
6051 free(keyword_idstr1);
6052 free(keyword_idstr2);
6058 const struct got_error *close_err = got_repo_close(repo);
6063 got_worktree_close(worktree);
6065 const struct got_error *pack_err =
6066 got_repo_pack_fds_close(pack_fds);
6079 "usage: %s blame [-c commit] [-r repository-path] path\n",
6084 struct tog_blame_line {
6086 struct got_object_id *id;
6089 static const struct got_error *
6090 draw_blame(struct tog_view *view)
6092 struct tog_blame_view_state *s = &view->state.blame;
6093 struct tog_blame *blame = &s->blame;
6094 regmatch_t *regmatch = &view->regmatch;
6095 const struct got_error *err;
6096 int lineno = 0, nprinted = 0;
6098 size_t linesize = 0;
6102 struct tog_blame_line *blame_line;
6103 struct got_object_id *prev_id = NULL;
6105 struct tog_color *tc;
6107 err = got_object_id_str(&id_str, &s->blamed_commit->id);
6112 werase(view->window);
6114 if (asprintf(&line, "commit %s", id_str) == -1) {
6115 err = got_error_from_errno("asprintf");
6120 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6125 if (view_needs_focus_indication(view))
6126 wstandout(view->window);
6127 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6129 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6130 waddwstr(view->window, wline);
6131 while (width++ < view->ncols)
6132 waddch(view->window, ' ');
6134 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6135 if (view_needs_focus_indication(view))
6136 wstandend(view->window);
6140 if (view->gline > blame->nlines)
6141 view->gline = blame->nlines;
6143 if (tog_io.wait_for_ui) {
6144 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6147 rc = pthread_cond_wait(&bta->blame_complete, &tog_mutex);
6149 return got_error_set_errno(rc, "pthread_cond_wait");
6150 tog_io.wait_for_ui = 0;
6153 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
6154 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
6155 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
6157 return got_error_from_errno("asprintf");
6160 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6165 waddwstr(view->window, wline);
6168 if (width < view->ncols - 1)
6169 waddch(view->window, '\n');
6173 while (nprinted < view->nlines - 2) {
6174 linelen = getline(&line, &linesize, blame->f);
6175 if (linelen == -1) {
6176 if (feof(blame->f)) {
6181 return got_ferror(blame->f, GOT_ERR_IO);
6183 if (++lineno < s->first_displayed_line)
6185 if (view->gline && !gotoline(view, &lineno, &nprinted))
6188 /* Set view->maxx based on full line length. */
6189 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6196 view->maxx = MAX(view->maxx, width);
6198 if (nprinted == s->selected_line - 1)
6199 wstandout(view->window);
6201 if (blame->nlines > 0) {
6202 blame_line = &blame->lines[lineno - 1];
6203 if (blame_line->annotated && prev_id &&
6204 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6205 !(nprinted == s->selected_line - 1)) {
6206 waddstr(view->window, " ");
6207 } else if (blame_line->annotated) {
6209 err = got_object_id_str(&id_str,
6215 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6217 wattr_on(view->window,
6218 COLOR_PAIR(tc->colorpair), NULL);
6219 wprintw(view->window, "%.8s", id_str);
6221 wattr_off(view->window,
6222 COLOR_PAIR(tc->colorpair), NULL);
6224 prev_id = blame_line->id;
6226 waddstr(view->window, "........");
6230 waddstr(view->window, "........");
6234 if (nprinted == s->selected_line - 1)
6235 wstandend(view->window);
6236 waddstr(view->window, " ");
6238 if (view->ncols <= 9) {
6240 } else if (s->first_displayed_line + nprinted ==
6242 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6243 err = add_matched_line(&width, line, view->ncols - 9, 9,
6244 view->window, view->x, regmatch);
6252 err = format_line(&wline, &width, &skip, line,
6253 view->x, view->ncols - 9, 9, 1);
6258 waddwstr(view->window, &wline[skip]);
6264 if (width <= view->ncols - 1)
6265 waddch(view->window, '\n');
6266 if (++nprinted == 1)
6267 s->first_displayed_line = lineno;
6270 s->last_displayed_line = lineno;
6277 static const struct got_error *
6278 blame_cb(void *arg, int nlines, int lineno,
6279 struct got_commit_object *commit, struct got_object_id *id)
6281 const struct got_error *err = NULL;
6282 struct tog_blame_cb_args *a = arg;
6283 struct tog_blame_line *line;
6286 if (nlines != a->nlines ||
6287 (lineno != -1 && lineno < 1) || lineno > a->nlines)
6288 return got_error(GOT_ERR_RANGE);
6290 errcode = pthread_mutex_lock(&tog_mutex);
6292 return got_error_set_errno(errcode, "pthread_mutex_lock");
6294 if (*a->quit) { /* user has quit the blame view */
6295 err = got_error(GOT_ERR_ITER_COMPLETED);
6300 goto done; /* no change in this commit */
6302 line = &a->lines[lineno - 1];
6303 if (line->annotated)
6306 line->id = got_object_id_dup(id);
6307 if (line->id == NULL) {
6308 err = got_error_from_errno("got_object_id_dup");
6311 line->annotated = 1;
6313 errcode = pthread_mutex_unlock(&tog_mutex);
6315 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6320 blame_thread(void *arg)
6322 const struct got_error *err, *close_err;
6323 struct tog_blame_thread_args *ta = arg;
6324 struct tog_blame_cb_args *a = ta->cb_args;
6325 int errcode, fd1 = -1, fd2 = -1;
6326 FILE *f1 = NULL, *f2 = NULL;
6328 fd1 = got_opentempfd();
6330 return (void *)got_error_from_errno("got_opentempfd");
6332 fd2 = got_opentempfd();
6334 err = got_error_from_errno("got_opentempfd");
6338 f1 = got_opentemp();
6340 err = (void *)got_error_from_errno("got_opentemp");
6343 f2 = got_opentemp();
6345 err = (void *)got_error_from_errno("got_opentemp");
6349 err = block_signals_used_by_main_thread();
6353 err = got_blame(ta->path, a->commit_id, ta->repo,
6354 tog_diff_algo, blame_cb, ta->cb_args,
6355 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6356 if (err && err->code == GOT_ERR_CANCELLED)
6359 errcode = pthread_mutex_lock(&tog_mutex);
6361 err = got_error_set_errno(errcode, "pthread_mutex_lock");
6365 close_err = got_repo_close(ta->repo);
6371 if (tog_io.wait_for_ui) {
6372 errcode = pthread_cond_signal(&ta->blame_complete);
6373 if (errcode && err == NULL)
6374 err = got_error_set_errno(errcode,
6375 "pthread_cond_signal");
6378 errcode = pthread_mutex_unlock(&tog_mutex);
6379 if (errcode && err == NULL)
6380 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6383 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6384 err = got_error_from_errno("close");
6385 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6386 err = got_error_from_errno("close");
6387 if (f1 && fclose(f1) == EOF && err == NULL)
6388 err = got_error_from_errno("fclose");
6389 if (f2 && fclose(f2) == EOF && err == NULL)
6390 err = got_error_from_errno("fclose");
6395 static struct got_object_id *
6396 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6397 int first_displayed_line, int selected_line)
6399 struct tog_blame_line *line;
6404 line = &lines[first_displayed_line - 1 + selected_line - 1];
6405 if (!line->annotated)
6411 static struct got_object_id *
6412 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6415 struct tog_blame_line *line;
6417 if (nlines <= 0 || lineno >= nlines)
6420 line = &lines[lineno - 1];
6421 if (!line->annotated)
6427 static const struct got_error *
6428 stop_blame(struct tog_blame *blame)
6430 const struct got_error *err = NULL;
6433 if (blame->thread) {
6435 errcode = pthread_mutex_unlock(&tog_mutex);
6437 return got_error_set_errno(errcode,
6438 "pthread_mutex_unlock");
6439 errcode = pthread_join(blame->thread, (void **)&err);
6441 return got_error_set_errno(errcode, "pthread_join");
6442 errcode = pthread_mutex_lock(&tog_mutex);
6444 return got_error_set_errno(errcode,
6445 "pthread_mutex_lock");
6446 if (err && err->code == GOT_ERR_ITER_COMPLETED)
6448 blame->thread = NULL;
6450 if (blame->thread_args.repo) {
6451 const struct got_error *close_err;
6452 close_err = got_repo_close(blame->thread_args.repo);
6455 blame->thread_args.repo = NULL;
6458 if (fclose(blame->f) == EOF && err == NULL)
6459 err = got_error_from_errno("fclose");
6463 for (i = 0; i < blame->nlines; i++)
6464 free(blame->lines[i].id);
6466 blame->lines = NULL;
6468 free(blame->cb_args.commit_id);
6469 blame->cb_args.commit_id = NULL;
6470 if (blame->pack_fds) {
6471 const struct got_error *pack_err =
6472 got_repo_pack_fds_close(blame->pack_fds);
6475 blame->pack_fds = NULL;
6480 static const struct got_error *
6481 cancel_blame_view(void *arg)
6483 const struct got_error *err = NULL;
6487 errcode = pthread_mutex_lock(&tog_mutex);
6489 return got_error_set_errno(errcode,
6490 "pthread_mutex_unlock");
6493 err = got_error(GOT_ERR_CANCELLED);
6495 errcode = pthread_mutex_unlock(&tog_mutex);
6497 return got_error_set_errno(errcode,
6498 "pthread_mutex_lock");
6503 static const struct got_error *
6504 run_blame(struct tog_view *view)
6506 struct tog_blame_view_state *s = &view->state.blame;
6507 struct tog_blame *blame = &s->blame;
6508 const struct got_error *err = NULL;
6509 struct got_commit_object *commit = NULL;
6510 struct got_blob_object *blob = NULL;
6511 struct got_repository *thread_repo = NULL;
6512 struct got_object_id *obj_id = NULL;
6513 int obj_type, fd = -1;
6514 int *pack_fds = NULL;
6516 err = got_object_open_as_commit(&commit, s->repo,
6517 &s->blamed_commit->id);
6521 fd = got_opentempfd();
6523 err = got_error_from_errno("got_opentempfd");
6527 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6531 err = got_object_get_type(&obj_type, s->repo, obj_id);
6535 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6536 err = got_error(GOT_ERR_OBJ_TYPE);
6540 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6543 blame->f = got_opentemp();
6544 if (blame->f == NULL) {
6545 err = got_error_from_errno("got_opentemp");
6548 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6549 &blame->line_offsets, blame->f, blob);
6552 if (blame->nlines == 0) {
6553 s->blame_complete = 1;
6557 /* Don't include \n at EOF in the blame line count. */
6558 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6561 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6562 if (blame->lines == NULL) {
6563 err = got_error_from_errno("calloc");
6567 err = got_repo_pack_fds_open(&pack_fds);
6570 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6575 blame->pack_fds = pack_fds;
6576 blame->cb_args.view = view;
6577 blame->cb_args.lines = blame->lines;
6578 blame->cb_args.nlines = blame->nlines;
6579 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6580 if (blame->cb_args.commit_id == NULL) {
6581 err = got_error_from_errno("got_object_id_dup");
6584 blame->cb_args.quit = &s->done;
6586 blame->thread_args.path = s->path;
6587 blame->thread_args.repo = thread_repo;
6588 blame->thread_args.cb_args = &blame->cb_args;
6589 blame->thread_args.complete = &s->blame_complete;
6590 blame->thread_args.cancel_cb = cancel_blame_view;
6591 blame->thread_args.cancel_arg = &s->done;
6592 s->blame_complete = 0;
6594 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6595 s->first_displayed_line = 1;
6596 s->last_displayed_line = view->nlines;
6597 s->selected_line = 1;
6599 s->matched_line = 0;
6603 got_object_commit_close(commit);
6604 if (fd != -1 && close(fd) == -1 && err == NULL)
6605 err = got_error_from_errno("close");
6607 got_object_blob_close(blob);
6614 static const struct got_error *
6615 open_blame_view(struct tog_view *view, char *path,
6616 struct got_object_id *commit_id, struct got_repository *repo)
6618 const struct got_error *err = NULL;
6619 struct tog_blame_view_state *s = &view->state.blame;
6621 STAILQ_INIT(&s->blamed_commits);
6623 s->path = strdup(path);
6624 if (s->path == NULL)
6625 return got_error_from_errno("strdup");
6627 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6633 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6634 s->first_displayed_line = 1;
6635 s->last_displayed_line = view->nlines;
6636 s->selected_line = 1;
6637 s->blame_complete = 0;
6639 s->commit_id = commit_id;
6640 memset(&s->blame, 0, sizeof(s->blame));
6642 STAILQ_INIT(&s->colors);
6643 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6644 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6645 get_color_value("TOG_COLOR_COMMIT"));
6650 view->show = show_blame_view;
6651 view->input = input_blame_view;
6652 view->reset = reset_blame_view;
6653 view->close = close_blame_view;
6654 view->search_start = search_start_blame_view;
6655 view->search_setup = search_setup_blame_view;
6656 view->search_next = search_next_view_match;
6658 if (using_mock_io) {
6659 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6662 rc = pthread_cond_init(&bta->blame_complete, NULL);
6664 return got_error_set_errno(rc, "pthread_cond_init");
6667 return run_blame(view);
6670 static const struct got_error *
6671 close_blame_view(struct tog_view *view)
6673 const struct got_error *err = NULL;
6674 struct tog_blame_view_state *s = &view->state.blame;
6676 if (s->blame.thread)
6677 err = stop_blame(&s->blame);
6679 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6680 struct got_object_qid *blamed_commit;
6681 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6682 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6683 got_object_qid_free(blamed_commit);
6686 if (using_mock_io) {
6687 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6690 rc = pthread_cond_destroy(&bta->blame_complete);
6691 if (rc && err == NULL)
6692 err = got_error_set_errno(rc, "pthread_cond_destroy");
6696 free_colors(&s->colors);
6700 static const struct got_error *
6701 search_start_blame_view(struct tog_view *view)
6703 struct tog_blame_view_state *s = &view->state.blame;
6705 s->matched_line = 0;
6710 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6711 size_t *nlines, int **first, int **last, int **match, int **selected)
6713 struct tog_blame_view_state *s = &view->state.blame;
6716 *nlines = s->blame.nlines;
6717 *line_offsets = s->blame.line_offsets;
6718 *match = &s->matched_line;
6719 *first = &s->first_displayed_line;
6720 *last = &s->last_displayed_line;
6721 *selected = &s->selected_line;
6724 static const struct got_error *
6725 show_blame_view(struct tog_view *view)
6727 const struct got_error *err = NULL;
6728 struct tog_blame_view_state *s = &view->state.blame;
6731 if (s->blame.thread == NULL && !s->blame_complete) {
6732 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6733 &s->blame.thread_args);
6735 return got_error_set_errno(errcode, "pthread_create");
6738 halfdelay(1); /* fast refresh while annotating */
6741 if (s->blame_complete && !using_mock_io)
6742 halfdelay(10); /* disable fast refresh */
6744 err = draw_blame(view);
6750 static const struct got_error *
6751 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6752 struct got_repository *repo, struct got_object_id *id)
6754 struct tog_view *log_view;
6755 const struct got_error *err = NULL;
6759 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6760 if (log_view == NULL)
6761 return got_error_from_errno("view_open");
6763 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6765 view_close(log_view);
6767 *new_view = log_view;
6772 static const struct got_error *
6773 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6775 const struct got_error *err = NULL, *thread_err = NULL;
6776 struct tog_view *diff_view;
6777 struct tog_blame_view_state *s = &view->state.blame;
6778 int eos, nscroll, begin_y = 0, begin_x = 0;
6780 eos = nscroll = view->nlines - 2;
6781 if (view_is_hsplit_top(view))
6791 horizontal_scroll_input(view, ch);
6798 s->selected_line = 1;
6799 s->first_displayed_line = 1;
6804 if (s->blame.nlines < eos) {
6805 s->selected_line = s->blame.nlines;
6806 s->first_displayed_line = 1;
6808 s->selected_line = eos;
6809 s->first_displayed_line = s->blame.nlines - (eos - 1);
6816 if (s->selected_line > 1)
6818 else if (s->selected_line == 1 &&
6819 s->first_displayed_line > 1)
6820 s->first_displayed_line--;
6831 if (s->first_displayed_line == 1) {
6832 if (view->count > 1)
6834 s->selected_line = MAX(1, s->selected_line - nscroll);
6838 if (s->first_displayed_line > nscroll)
6839 s->first_displayed_line -= nscroll;
6841 s->first_displayed_line = 1;
6846 if (s->selected_line < eos && s->first_displayed_line +
6847 s->selected_line <= s->blame.nlines)
6849 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6850 s->first_displayed_line++;
6856 struct got_object_id *id = NULL;
6859 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6860 s->first_displayed_line, s->selected_line);
6864 struct got_commit_object *commit, *pcommit;
6865 struct got_object_qid *pid;
6866 struct got_object_id *blob_id = NULL;
6868 err = got_object_open_as_commit(&commit,
6873 got_object_commit_get_parent_ids(commit));
6875 got_object_commit_close(commit);
6878 /* Check if path history ends here. */
6879 err = got_object_open_as_commit(&pcommit,
6883 err = got_object_id_by_path(&blob_id, s->repo,
6885 got_object_commit_close(pcommit);
6887 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6889 got_object_commit_close(commit);
6892 err = got_object_get_type(&obj_type, s->repo,
6895 /* Can't blame non-blob type objects. */
6896 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6897 got_object_commit_close(commit);
6900 err = got_object_qid_alloc(&s->blamed_commit,
6902 got_object_commit_close(commit);
6904 if (got_object_id_cmp(id,
6905 &s->blamed_commit->id) == 0)
6907 err = got_object_qid_alloc(&s->blamed_commit,
6913 thread_err = stop_blame(&s->blame);
6917 STAILQ_INSERT_HEAD(&s->blamed_commits,
6918 s->blamed_commit, entry);
6919 err = run_blame(view);
6925 struct got_object_qid *first;
6928 first = STAILQ_FIRST(&s->blamed_commits);
6929 if (!got_object_id_cmp(&first->id, s->commit_id))
6932 thread_err = stop_blame(&s->blame);
6936 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6937 got_object_qid_free(s->blamed_commit);
6939 STAILQ_FIRST(&s->blamed_commits);
6940 err = run_blame(view);
6947 s->id_to_log = get_selected_commit_id(s->blame.lines,
6948 s->blame.nlines, s->first_displayed_line, s->selected_line);
6950 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6954 struct got_object_id *id = NULL;
6955 struct got_object_qid *pid;
6956 struct got_commit_object *commit = NULL;
6959 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6960 s->first_displayed_line, s->selected_line);
6963 err = got_object_open_as_commit(&commit, s->repo, id);
6966 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6968 /* traversed from diff view, release diff resources */
6969 err = close_diff_view(*new_view);
6972 diff_view = *new_view;
6974 if (view_is_parent_view(view))
6975 view_get_split(view, &begin_y, &begin_x);
6977 diff_view = view_open(0, 0, begin_y, begin_x,
6979 if (diff_view == NULL) {
6980 got_object_commit_close(commit);
6981 err = got_error_from_errno("view_open");
6985 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6986 id, NULL, NULL, 3, 0, 0, view, s->repo);
6987 got_object_commit_close(commit);
6990 s->last_diffed_line = s->first_displayed_line - 1 +
6993 break; /* still open from active diff view */
6994 if (view_is_parent_view(view) &&
6995 view->mode == TOG_VIEW_SPLIT_HRZN) {
6996 err = view_init_hsplit(view, begin_y);
7002 diff_view->focussed = 1;
7003 diff_view->mode = view->mode;
7004 diff_view->nlines = view->lines - begin_y;
7005 if (view_is_parent_view(view)) {
7006 view_transfer_size(diff_view, view);
7007 err = view_close_child(view);
7010 err = view_set_child(view, diff_view);
7013 view->focus_child = 1;
7015 *new_view = diff_view;
7028 if (s->last_displayed_line >= s->blame.nlines &&
7029 s->selected_line >= MIN(s->blame.nlines,
7030 view->nlines - 2)) {
7034 if (s->last_displayed_line >= s->blame.nlines &&
7035 s->selected_line < view->nlines - 2) {
7037 MIN(nscroll, s->last_displayed_line -
7038 s->first_displayed_line - s->selected_line + 1);
7040 if (s->last_displayed_line + nscroll <= s->blame.nlines)
7041 s->first_displayed_line += nscroll;
7043 s->first_displayed_line =
7044 s->blame.nlines - (view->nlines - 3);
7047 if (s->selected_line > view->nlines - 2) {
7048 s->selected_line = MIN(s->blame.nlines,
7056 return thread_err ? thread_err : err;
7059 static const struct got_error *
7060 reset_blame_view(struct tog_view *view)
7062 const struct got_error *err;
7063 struct tog_blame_view_state *s = &view->state.blame;
7067 err = stop_blame(&s->blame);
7071 return run_blame(view);
7074 static const struct got_error *
7075 cmd_blame(int argc, char *argv[])
7077 const struct got_error *error;
7078 struct got_repository *repo = NULL;
7079 struct got_worktree *worktree = NULL;
7080 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7081 char *link_target = NULL;
7082 struct got_object_id *commit_id = NULL;
7083 struct got_commit_object *commit = NULL;
7084 char *keyword_idstr = NULL, *commit_id_str = NULL;
7086 struct tog_view *view = NULL;
7087 int *pack_fds = NULL;
7089 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7092 commit_id_str = optarg;
7095 repo_path = realpath(optarg, NULL);
7096 if (repo_path == NULL)
7097 return got_error_from_errno2("realpath",
7112 error = got_repo_pack_fds_open(&pack_fds);
7116 if (repo_path == NULL) {
7117 cwd = getcwd(NULL, 0);
7119 return got_error_from_errno("getcwd");
7120 error = got_worktree_open(&worktree, cwd, NULL);
7121 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7125 strdup(got_worktree_get_repo_path(worktree));
7127 repo_path = strdup(cwd);
7128 if (repo_path == NULL) {
7129 error = got_error_from_errno("strdup");
7134 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7138 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
7145 error = apply_unveil(got_repo_get_path(repo), NULL);
7149 error = tog_load_refs(repo, 0);
7153 if (commit_id_str == NULL) {
7154 struct got_reference *head_ref;
7155 error = got_ref_open(&head_ref, repo, worktree ?
7156 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
7159 error = got_ref_resolve(&commit_id, repo, head_ref);
7160 got_ref_close(head_ref);
7162 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
7166 if (keyword_idstr != NULL)
7167 commit_id_str = keyword_idstr;
7169 error = got_repo_match_object_id(&commit_id, NULL,
7170 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7175 error = got_object_open_as_commit(&commit, repo, commit_id);
7179 error = got_object_resolve_symlinks(&link_target, in_repo_path,
7184 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
7186 error = got_error_from_errno("view_open");
7189 error = open_blame_view(view, link_target ? link_target : in_repo_path,
7191 if (error != NULL) {
7192 if (view->close == NULL)
7193 close_blame_view(view);
7198 /* Release work tree lock. */
7199 got_worktree_close(worktree);
7202 error = view_loop(view);
7209 free(keyword_idstr);
7211 got_object_commit_close(commit);
7213 got_worktree_close(worktree);
7215 const struct got_error *close_err = got_repo_close(repo);
7220 const struct got_error *pack_err =
7221 got_repo_pack_fds_close(pack_fds);
7229 static const struct got_error *
7230 draw_tree_entries(struct tog_view *view, const char *parent_path)
7232 struct tog_tree_view_state *s = &view->state.tree;
7233 const struct got_error *err = NULL;
7234 struct got_tree_entry *te;
7237 struct tog_color *tc;
7238 int width, n, nentries, scrollx, i = 1;
7239 int limit = view->nlines;
7242 if (view_is_hsplit_top(view))
7243 --limit; /* border */
7245 werase(view->window);
7250 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7254 if (view_needs_focus_indication(view))
7255 wstandout(view->window);
7256 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7258 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7259 waddwstr(view->window, wline);
7262 while (width++ < view->ncols)
7263 waddch(view->window, ' ');
7265 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7266 if (view_needs_focus_indication(view))
7267 wstandend(view->window);
7272 if (s->first_displayed_entry) {
7273 i += got_tree_entry_get_index(s->first_displayed_entry);
7274 if (s->tree != s->root)
7275 ++i; /* account for ".." entry */
7277 nentries = got_object_tree_get_nentries(s->tree);
7278 if (asprintf(&index, "[%d/%d] %s",
7279 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7280 return got_error_from_errno("asprintf");
7281 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7285 waddwstr(view->window, wline);
7288 if (width < view->ncols - 1)
7289 waddch(view->window, '\n');
7292 waddch(view->window, '\n');
7296 if (s->first_displayed_entry == NULL) {
7297 te = got_object_tree_get_first_entry(s->tree);
7298 if (s->selected == 0) {
7300 wstandout(view->window);
7301 s->selected_entry = NULL;
7303 waddstr(view->window, " ..\n"); /* parent directory */
7304 if (s->selected == 0 && view->focussed)
7305 wstandend(view->window);
7312 te = s->first_displayed_entry;
7316 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7317 char *line = NULL, *id_str = NULL, *link_target = NULL;
7318 const char *modestr = "";
7321 te = got_object_tree_get_entry(s->tree, i);
7322 mode = got_tree_entry_get_mode(te);
7325 err = got_object_id_str(&id_str,
7326 got_tree_entry_get_id(te));
7328 return got_error_from_errno(
7329 "got_object_id_str");
7331 if (got_object_tree_entry_is_submodule(te))
7333 else if (S_ISLNK(mode)) {
7336 err = got_tree_entry_get_symlink_target(&link_target,
7342 for (i = 0; link_target[i] != '\0'; i++) {
7343 if (!isprint((unsigned char)link_target[i]))
7344 link_target[i] = '?';
7348 else if (S_ISDIR(mode))
7350 else if (mode & S_IXUSR)
7352 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7353 got_tree_entry_get_name(te), modestr,
7354 link_target ? " -> ": "",
7355 link_target ? link_target : "") == -1) {
7358 return got_error_from_errno("asprintf");
7363 /* use full line width to determine view->maxx */
7364 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7369 view->maxx = MAX(view->maxx, width);
7373 err = format_line(&wline, &width, &scrollx, line, view->x,
7379 if (n == s->selected) {
7381 wstandout(view->window);
7382 s->selected_entry = te;
7384 tc = match_color(&s->colors, line);
7386 wattr_on(view->window,
7387 COLOR_PAIR(tc->colorpair), NULL);
7388 waddwstr(view->window, &wline[scrollx]);
7390 wattr_off(view->window,
7391 COLOR_PAIR(tc->colorpair), NULL);
7392 if (width < view->ncols)
7393 waddch(view->window, '\n');
7394 if (n == s->selected && view->focussed)
7395 wstandend(view->window);
7401 s->last_displayed_entry = te;
7410 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7412 struct got_tree_entry *te;
7413 int isroot = s->tree == s->root;
7416 if (s->first_displayed_entry == NULL)
7419 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7420 while (i++ < maxscroll) {
7423 s->first_displayed_entry = NULL;
7426 s->first_displayed_entry = te;
7427 te = got_tree_entry_get_prev(s->tree, te);
7431 static const struct got_error *
7432 tree_scroll_down(struct tog_view *view, int maxscroll)
7434 struct tog_tree_view_state *s = &view->state.tree;
7435 struct got_tree_entry *next, *last;
7438 if (s->first_displayed_entry)
7439 next = got_tree_entry_get_next(s->tree,
7440 s->first_displayed_entry);
7442 next = got_object_tree_get_first_entry(s->tree);
7444 last = s->last_displayed_entry;
7445 while (next && n++ < maxscroll) {
7447 s->last_displayed_entry = last;
7448 last = got_tree_entry_get_next(s->tree, last);
7450 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7451 s->first_displayed_entry = next;
7452 next = got_tree_entry_get_next(s->tree, next);
7459 static const struct got_error *
7460 tree_entry_path(char **path, struct tog_parent_trees *parents,
7461 struct got_tree_entry *te)
7463 const struct got_error *err = NULL;
7464 struct tog_parent_tree *pt;
7465 size_t len = 2; /* for leading slash and NUL */
7467 TAILQ_FOREACH(pt, parents, entry)
7468 len += strlen(got_tree_entry_get_name(pt->selected_entry))
7471 len += strlen(got_tree_entry_get_name(te));
7473 *path = calloc(1, len);
7475 return got_error_from_errno("calloc");
7478 pt = TAILQ_LAST(parents, tog_parent_trees);
7480 const char *name = got_tree_entry_get_name(pt->selected_entry);
7481 if (strlcat(*path, name, len) >= len) {
7482 err = got_error(GOT_ERR_NO_SPACE);
7485 if (strlcat(*path, "/", len) >= len) {
7486 err = got_error(GOT_ERR_NO_SPACE);
7489 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7492 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7493 err = got_error(GOT_ERR_NO_SPACE);
7505 static const struct got_error *
7506 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7507 struct got_tree_entry *te, struct tog_parent_trees *parents,
7508 struct got_object_id *commit_id, struct got_repository *repo)
7510 const struct got_error *err = NULL;
7512 struct tog_view *blame_view;
7516 err = tree_entry_path(&path, parents, te);
7520 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7521 if (blame_view == NULL) {
7522 err = got_error_from_errno("view_open");
7526 err = open_blame_view(blame_view, path, commit_id, repo);
7528 if (err->code == GOT_ERR_CANCELLED)
7530 view_close(blame_view);
7532 *new_view = blame_view;
7538 static const struct got_error *
7539 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7540 struct tog_tree_view_state *s)
7542 struct tog_view *log_view;
7543 const struct got_error *err = NULL;
7548 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7549 if (log_view == NULL)
7550 return got_error_from_errno("view_open");
7552 err = tree_entry_path(&path, &s->parents, s->selected_entry);
7556 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7559 view_close(log_view);
7561 *new_view = log_view;
7566 static const struct got_error *
7567 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7568 const char *head_ref_name, struct got_repository *repo)
7570 const struct got_error *err = NULL;
7571 char *commit_id_str = NULL;
7572 struct tog_tree_view_state *s = &view->state.tree;
7573 struct got_commit_object *commit = NULL;
7575 TAILQ_INIT(&s->parents);
7576 STAILQ_INIT(&s->colors);
7578 s->commit_id = got_object_id_dup(commit_id);
7579 if (s->commit_id == NULL) {
7580 err = got_error_from_errno("got_object_id_dup");
7584 err = got_object_open_as_commit(&commit, repo, commit_id);
7589 * The root is opened here and will be closed when the view is closed.
7590 * Any visited subtrees and their path-wise parents are opened and
7593 err = got_object_open_as_tree(&s->root, repo,
7594 got_object_commit_get_tree_id(commit));
7599 err = got_object_id_str(&commit_id_str, commit_id);
7603 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7604 err = got_error_from_errno("asprintf");
7608 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7609 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7610 if (head_ref_name) {
7611 s->head_ref_name = strdup(head_ref_name);
7612 if (s->head_ref_name == NULL) {
7613 err = got_error_from_errno("strdup");
7619 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7620 err = add_color(&s->colors, "\\$$",
7621 TOG_COLOR_TREE_SUBMODULE,
7622 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7625 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7626 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7629 err = add_color(&s->colors, "/$",
7630 TOG_COLOR_TREE_DIRECTORY,
7631 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7635 err = add_color(&s->colors, "\\*$",
7636 TOG_COLOR_TREE_EXECUTABLE,
7637 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7641 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7642 get_color_value("TOG_COLOR_COMMIT"));
7647 view->show = show_tree_view;
7648 view->input = input_tree_view;
7649 view->close = close_tree_view;
7650 view->search_start = search_start_tree_view;
7651 view->search_next = search_next_tree_view;
7653 free(commit_id_str);
7655 got_object_commit_close(commit);
7657 if (view->close == NULL)
7658 close_tree_view(view);
7664 static const struct got_error *
7665 close_tree_view(struct tog_view *view)
7667 struct tog_tree_view_state *s = &view->state.tree;
7669 free_colors(&s->colors);
7670 free(s->tree_label);
7671 s->tree_label = NULL;
7673 s->commit_id = NULL;
7674 free(s->head_ref_name);
7675 s->head_ref_name = NULL;
7676 while (!TAILQ_EMPTY(&s->parents)) {
7677 struct tog_parent_tree *parent;
7678 parent = TAILQ_FIRST(&s->parents);
7679 TAILQ_REMOVE(&s->parents, parent, entry);
7680 if (parent->tree != s->root)
7681 got_object_tree_close(parent->tree);
7685 if (s->tree != NULL && s->tree != s->root)
7686 got_object_tree_close(s->tree);
7688 got_object_tree_close(s->root);
7692 static const struct got_error *
7693 search_start_tree_view(struct tog_view *view)
7695 struct tog_tree_view_state *s = &view->state.tree;
7697 s->matched_entry = NULL;
7702 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7704 regmatch_t regmatch;
7706 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7710 static const struct got_error *
7711 search_next_tree_view(struct tog_view *view)
7713 struct tog_tree_view_state *s = &view->state.tree;
7714 struct got_tree_entry *te = NULL;
7716 if (!view->searching) {
7717 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7721 if (s->matched_entry) {
7722 if (view->searching == TOG_SEARCH_FORWARD) {
7723 if (s->selected_entry)
7724 te = got_tree_entry_get_next(s->tree,
7727 te = got_object_tree_get_first_entry(s->tree);
7729 if (s->selected_entry == NULL)
7730 te = got_object_tree_get_last_entry(s->tree);
7732 te = got_tree_entry_get_prev(s->tree,
7736 if (s->selected_entry)
7737 te = s->selected_entry;
7738 else if (view->searching == TOG_SEARCH_FORWARD)
7739 te = got_object_tree_get_first_entry(s->tree);
7741 te = got_object_tree_get_last_entry(s->tree);
7746 if (s->matched_entry == NULL) {
7747 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7750 if (view->searching == TOG_SEARCH_FORWARD)
7751 te = got_object_tree_get_first_entry(s->tree);
7753 te = got_object_tree_get_last_entry(s->tree);
7756 if (match_tree_entry(te, &view->regex)) {
7757 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7758 s->matched_entry = te;
7762 if (view->searching == TOG_SEARCH_FORWARD)
7763 te = got_tree_entry_get_next(s->tree, te);
7765 te = got_tree_entry_get_prev(s->tree, te);
7768 if (s->matched_entry) {
7769 s->first_displayed_entry = s->matched_entry;
7776 static const struct got_error *
7777 show_tree_view(struct tog_view *view)
7779 const struct got_error *err = NULL;
7780 struct tog_tree_view_state *s = &view->state.tree;
7783 err = tree_entry_path(&parent_path, &s->parents, NULL);
7787 err = draw_tree_entries(view, parent_path);
7794 static const struct got_error *
7795 tree_goto_line(struct tog_view *view, int nlines)
7797 const struct got_error *err = NULL;
7798 struct tog_tree_view_state *s = &view->state.tree;
7799 struct got_tree_entry **fte, **lte, **ste;
7800 int g, last, first = 1, i = 1;
7801 int root = s->tree == s->root;
7802 int off = root ? 1 : 2;
7809 else if (g > got_object_tree_get_nentries(s->tree))
7810 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7812 fte = &s->first_displayed_entry;
7813 lte = &s->last_displayed_entry;
7814 ste = &s->selected_entry;
7817 first = got_tree_entry_get_index(*fte);
7818 first += off; /* account for ".." */
7820 last = got_tree_entry_get_index(*lte);
7823 if (g >= first && g <= last && g - first < nlines) {
7824 s->selected = g - first;
7825 return NULL; /* gline is on the current page */
7829 i = got_tree_entry_get_index(*ste);
7834 err = tree_scroll_down(view, g - i);
7837 if (got_tree_entry_get_index(*lte) >=
7838 got_object_tree_get_nentries(s->tree) - 1 &&
7839 first + s->selected < g &&
7840 s->selected < s->ndisplayed - 1) {
7841 first = got_tree_entry_get_index(*fte);
7843 s->selected = g - first;
7846 tree_scroll_up(s, i - g);
7849 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7850 s->selected = g - 1;
7855 static const struct got_error *
7856 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7858 const struct got_error *err = NULL;
7859 struct tog_tree_view_state *s = &view->state.tree;
7860 struct got_tree_entry *te;
7861 int n, nscroll = view->nlines - 3;
7864 return tree_goto_line(view, nscroll);
7873 horizontal_scroll_input(view, ch);
7876 s->show_ids = !s->show_ids;
7881 if (!s->selected_entry)
7883 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7887 err = view_request_new(new_view, view, TOG_VIEW_REF);
7894 if (s->tree == s->root)
7895 s->first_displayed_entry =
7896 got_object_tree_get_first_entry(s->tree);
7898 s->first_displayed_entry = NULL;
7903 int eos = view->nlines - 3;
7905 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7909 te = got_object_tree_get_last_entry(s->tree);
7910 for (n = 0; n < eos; n++) {
7912 if (s->tree != s->root) {
7913 s->first_displayed_entry = NULL;
7918 s->first_displayed_entry = te;
7919 te = got_tree_entry_get_prev(s->tree, te);
7922 s->selected = n - 1;
7928 if (s->selected > 0) {
7932 tree_scroll_up(s, 1);
7933 if (s->selected_entry == NULL ||
7934 (s->tree == s->root && s->selected_entry ==
7935 got_object_tree_get_first_entry(s->tree)))
7945 if (s->tree == s->root) {
7946 if (got_object_tree_get_first_entry(s->tree) ==
7947 s->first_displayed_entry)
7948 s->selected -= MIN(s->selected, nscroll);
7950 if (s->first_displayed_entry == NULL)
7951 s->selected -= MIN(s->selected, nscroll);
7953 tree_scroll_up(s, MAX(0, nscroll));
7954 if (s->selected_entry == NULL ||
7955 (s->tree == s->root && s->selected_entry ==
7956 got_object_tree_get_first_entry(s->tree)))
7962 if (s->selected < s->ndisplayed - 1) {
7966 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7968 /* can't scroll any further */
7972 tree_scroll_down(view, 1);
7982 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7984 /* can't scroll any further; move cursor down */
7985 if (s->selected < s->ndisplayed - 1)
7986 s->selected += MIN(nscroll,
7987 s->ndisplayed - s->selected - 1);
7992 tree_scroll_down(view, nscroll);
7997 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7998 struct tog_parent_tree *parent;
7999 /* user selected '..' */
8000 if (s->tree == s->root) {
8004 parent = TAILQ_FIRST(&s->parents);
8005 TAILQ_REMOVE(&s->parents, parent,
8007 got_object_tree_close(s->tree);
8008 s->tree = parent->tree;
8009 s->first_displayed_entry =
8010 parent->first_displayed_entry;
8012 parent->selected_entry;
8013 s->selected = parent->selected;
8014 if (s->selected > view->nlines - 3) {
8015 err = offset_selection_down(view);
8020 } else if (S_ISDIR(got_tree_entry_get_mode(
8021 s->selected_entry))) {
8022 struct got_tree_object *subtree;
8024 err = got_object_open_as_tree(&subtree, s->repo,
8025 got_tree_entry_get_id(s->selected_entry));
8028 err = tree_view_visit_subtree(s, subtree);
8030 got_object_tree_close(subtree);
8033 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
8034 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
8037 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
8038 s->selected = view->nlines - 4;
8054 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
8059 static const struct got_error *
8060 cmd_tree(int argc, char *argv[])
8062 const struct got_error *error;
8063 struct got_repository *repo = NULL;
8064 struct got_worktree *worktree = NULL;
8065 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8066 struct got_object_id *commit_id = NULL;
8067 struct got_commit_object *commit = NULL;
8068 const char *commit_id_arg = NULL;
8069 char *keyword_idstr = NULL, *label = NULL;
8070 struct got_reference *ref = NULL;
8071 const char *head_ref_name = NULL;
8073 struct tog_view *view;
8074 int *pack_fds = NULL;
8076 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
8079 commit_id_arg = optarg;
8082 repo_path = realpath(optarg, NULL);
8083 if (repo_path == NULL)
8084 return got_error_from_errno2("realpath",
8099 error = got_repo_pack_fds_open(&pack_fds);
8103 if (repo_path == NULL) {
8104 cwd = getcwd(NULL, 0);
8106 return got_error_from_errno("getcwd");
8107 error = got_worktree_open(&worktree, cwd, NULL);
8108 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8112 strdup(got_worktree_get_repo_path(worktree));
8114 repo_path = strdup(cwd);
8115 if (repo_path == NULL) {
8116 error = got_error_from_errno("strdup");
8121 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8125 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8132 error = apply_unveil(got_repo_get_path(repo), NULL);
8136 error = tog_load_refs(repo, 0);
8140 if (commit_id_arg == NULL) {
8141 error = got_repo_match_object_id(&commit_id, &label,
8142 worktree ? got_worktree_get_head_ref_name(worktree) :
8143 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8146 head_ref_name = label;
8148 error = got_keyword_to_idstr(&keyword_idstr, commit_id_arg,
8152 if (keyword_idstr != NULL)
8153 commit_id_arg = keyword_idstr;
8155 error = got_ref_open(&ref, repo, commit_id_arg, 0);
8157 head_ref_name = got_ref_get_name(ref);
8158 else if (error->code != GOT_ERR_NOT_REF)
8160 error = got_repo_match_object_id(&commit_id, NULL,
8161 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8166 error = got_object_open_as_commit(&commit, repo, commit_id);
8170 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
8172 error = got_error_from_errno("view_open");
8175 error = open_tree_view(view, commit_id, head_ref_name, repo);
8178 if (!got_path_is_root_dir(in_repo_path)) {
8179 error = tree_view_walk_path(&view->state.tree, commit,
8186 /* Release work tree lock. */
8187 got_worktree_close(worktree);
8190 error = view_loop(view);
8192 free(keyword_idstr);
8200 const struct got_error *close_err = got_repo_close(repo);
8205 const struct got_error *pack_err =
8206 got_repo_pack_fds_close(pack_fds);
8214 static const struct got_error *
8215 ref_view_load_refs(struct tog_ref_view_state *s)
8217 struct got_reflist_entry *sre;
8218 struct tog_reflist_entry *re;
8221 TAILQ_FOREACH(sre, &tog_refs, entry) {
8222 if (strncmp(got_ref_get_name(sre->ref),
8223 "refs/got/", 9) == 0 &&
8224 strncmp(got_ref_get_name(sre->ref),
8225 "refs/got/backup/", 16) != 0)
8228 re = malloc(sizeof(*re));
8230 return got_error_from_errno("malloc");
8232 re->ref = got_ref_dup(sre->ref);
8233 if (re->ref == NULL)
8234 return got_error_from_errno("got_ref_dup");
8235 re->idx = s->nrefs++;
8236 TAILQ_INSERT_TAIL(&s->refs, re, entry);
8239 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8244 ref_view_free_refs(struct tog_ref_view_state *s)
8246 struct tog_reflist_entry *re;
8248 while (!TAILQ_EMPTY(&s->refs)) {
8249 re = TAILQ_FIRST(&s->refs);
8250 TAILQ_REMOVE(&s->refs, re, entry);
8251 got_ref_close(re->ref);
8256 static const struct got_error *
8257 open_ref_view(struct tog_view *view, struct got_repository *repo)
8259 const struct got_error *err = NULL;
8260 struct tog_ref_view_state *s = &view->state.ref;
8262 s->selected_entry = 0;
8265 TAILQ_INIT(&s->refs);
8266 STAILQ_INIT(&s->colors);
8268 err = ref_view_load_refs(s);
8272 if (has_colors() && getenv("TOG_COLORS") != NULL) {
8273 err = add_color(&s->colors, "^refs/heads/",
8274 TOG_COLOR_REFS_HEADS,
8275 get_color_value("TOG_COLOR_REFS_HEADS"));
8279 err = add_color(&s->colors, "^refs/tags/",
8280 TOG_COLOR_REFS_TAGS,
8281 get_color_value("TOG_COLOR_REFS_TAGS"));
8285 err = add_color(&s->colors, "^refs/remotes/",
8286 TOG_COLOR_REFS_REMOTES,
8287 get_color_value("TOG_COLOR_REFS_REMOTES"));
8291 err = add_color(&s->colors, "^refs/got/backup/",
8292 TOG_COLOR_REFS_BACKUP,
8293 get_color_value("TOG_COLOR_REFS_BACKUP"));
8298 view->show = show_ref_view;
8299 view->input = input_ref_view;
8300 view->close = close_ref_view;
8301 view->search_start = search_start_ref_view;
8302 view->search_next = search_next_ref_view;
8305 if (view->close == NULL)
8306 close_ref_view(view);
8312 static const struct got_error *
8313 close_ref_view(struct tog_view *view)
8315 struct tog_ref_view_state *s = &view->state.ref;
8317 ref_view_free_refs(s);
8318 free_colors(&s->colors);
8323 static const struct got_error *
8324 resolve_reflist_entry(struct got_object_id **commit_id,
8325 struct tog_reflist_entry *re, struct got_repository *repo)
8327 const struct got_error *err = NULL;
8328 struct got_object_id *obj_id;
8329 struct got_tag_object *tag = NULL;
8334 err = got_ref_resolve(&obj_id, repo, re->ref);
8338 err = got_object_get_type(&obj_type, repo, obj_id);
8343 case GOT_OBJ_TYPE_COMMIT:
8344 *commit_id = obj_id;
8346 case GOT_OBJ_TYPE_TAG:
8347 err = got_object_open_as_tag(&tag, repo, obj_id);
8351 err = got_object_get_type(&obj_type, repo,
8352 got_object_tag_get_object_id(tag));
8355 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
8356 err = got_error(GOT_ERR_OBJ_TYPE);
8359 *commit_id = got_object_id_dup(
8360 got_object_tag_get_object_id(tag));
8361 if (*commit_id == NULL) {
8362 err = got_error_from_errno("got_object_id_dup");
8367 err = got_error(GOT_ERR_OBJ_TYPE);
8373 got_object_tag_close(tag);
8381 static const struct got_error *
8382 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
8383 struct tog_reflist_entry *re, struct got_repository *repo)
8385 struct tog_view *log_view;
8386 const struct got_error *err = NULL;
8387 struct got_object_id *commit_id = NULL;
8391 err = resolve_reflist_entry(&commit_id, re, repo);
8393 if (err->code != GOT_ERR_OBJ_TYPE)
8399 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
8400 if (log_view == NULL) {
8401 err = got_error_from_errno("view_open");
8405 err = open_log_view(log_view, commit_id, repo,
8406 got_ref_get_name(re->ref), "", 0);
8409 view_close(log_view);
8411 *new_view = log_view;
8417 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
8419 struct tog_reflist_entry *re;
8422 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8425 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
8426 while (i++ < maxscroll) {
8429 s->first_displayed_entry = re;
8430 re = TAILQ_PREV(re, tog_reflist_head, entry);
8434 static const struct got_error *
8435 ref_scroll_down(struct tog_view *view, int maxscroll)
8437 struct tog_ref_view_state *s = &view->state.ref;
8438 struct tog_reflist_entry *next, *last;
8441 if (s->first_displayed_entry)
8442 next = TAILQ_NEXT(s->first_displayed_entry, entry);
8444 next = TAILQ_FIRST(&s->refs);
8446 last = s->last_displayed_entry;
8447 while (next && n++ < maxscroll) {
8449 s->last_displayed_entry = last;
8450 last = TAILQ_NEXT(last, entry);
8452 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
8453 s->first_displayed_entry = next;
8454 next = TAILQ_NEXT(next, entry);
8461 static const struct got_error *
8462 search_start_ref_view(struct tog_view *view)
8464 struct tog_ref_view_state *s = &view->state.ref;
8466 s->matched_entry = NULL;
8471 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8473 regmatch_t regmatch;
8475 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8479 static const struct got_error *
8480 search_next_ref_view(struct tog_view *view)
8482 struct tog_ref_view_state *s = &view->state.ref;
8483 struct tog_reflist_entry *re = NULL;
8485 if (!view->searching) {
8486 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8490 if (s->matched_entry) {
8491 if (view->searching == TOG_SEARCH_FORWARD) {
8492 if (s->selected_entry)
8493 re = TAILQ_NEXT(s->selected_entry, entry);
8495 re = TAILQ_PREV(s->selected_entry,
8496 tog_reflist_head, entry);
8498 if (s->selected_entry == NULL)
8499 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8501 re = TAILQ_PREV(s->selected_entry,
8502 tog_reflist_head, entry);
8505 if (s->selected_entry)
8506 re = s->selected_entry;
8507 else if (view->searching == TOG_SEARCH_FORWARD)
8508 re = TAILQ_FIRST(&s->refs);
8510 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8515 if (s->matched_entry == NULL) {
8516 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8519 if (view->searching == TOG_SEARCH_FORWARD)
8520 re = TAILQ_FIRST(&s->refs);
8522 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8525 if (match_reflist_entry(re, &view->regex)) {
8526 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8527 s->matched_entry = re;
8531 if (view->searching == TOG_SEARCH_FORWARD)
8532 re = TAILQ_NEXT(re, entry);
8534 re = TAILQ_PREV(re, tog_reflist_head, entry);
8537 if (s->matched_entry) {
8538 s->first_displayed_entry = s->matched_entry;
8545 static const struct got_error *
8546 show_ref_view(struct tog_view *view)
8548 const struct got_error *err = NULL;
8549 struct tog_ref_view_state *s = &view->state.ref;
8550 struct tog_reflist_entry *re;
8553 struct tog_color *tc;
8554 int width, n, scrollx;
8555 int limit = view->nlines;
8557 werase(view->window);
8560 if (view_is_hsplit_top(view))
8561 --limit; /* border */
8566 re = s->first_displayed_entry;
8568 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8570 return got_error_from_errno("asprintf");
8572 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8577 if (view_needs_focus_indication(view))
8578 wstandout(view->window);
8579 waddwstr(view->window, wline);
8580 while (width++ < view->ncols)
8581 waddch(view->window, ' ');
8582 if (view_needs_focus_indication(view))
8583 wstandend(view->window);
8593 while (re && limit > 0) {
8595 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8598 struct got_commit_object *ci;
8599 struct got_tag_object *tag;
8600 struct got_object_id *id;
8604 err = got_ref_resolve(&id, s->repo, re->ref);
8607 err = got_object_open_as_tag(&tag, s->repo, id);
8609 if (err->code != GOT_ERR_OBJ_TYPE) {
8613 err = got_object_open_as_commit(&ci, s->repo,
8619 t = got_object_commit_get_committer_time(ci);
8620 got_object_commit_close(ci);
8622 t = got_object_tag_get_tagger_time(tag);
8623 got_object_tag_close(tag);
8626 if (gmtime_r(&t, &tm) == NULL)
8627 return got_error_from_errno("gmtime_r");
8628 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8629 return got_error(GOT_ERR_NO_SPACE);
8631 if (got_ref_is_symbolic(re->ref)) {
8632 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8633 ymd : "", got_ref_get_name(re->ref),
8634 got_ref_get_symref_target(re->ref)) == -1)
8635 return got_error_from_errno("asprintf");
8636 } else if (s->show_ids) {
8637 struct got_object_id *id;
8639 err = got_ref_resolve(&id, s->repo, re->ref);
8642 err = got_object_id_str(&id_str, id);
8647 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8648 got_ref_get_name(re->ref), id_str) == -1) {
8649 err = got_error_from_errno("asprintf");
8656 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8657 got_ref_get_name(re->ref)) == -1)
8658 return got_error_from_errno("asprintf");
8660 /* use full line width to determine view->maxx */
8661 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8666 view->maxx = MAX(view->maxx, width);
8670 err = format_line(&wline, &width, &scrollx, line, view->x,
8676 if (n == s->selected) {
8678 wstandout(view->window);
8679 s->selected_entry = re;
8681 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8683 wattr_on(view->window,
8684 COLOR_PAIR(tc->colorpair), NULL);
8685 waddwstr(view->window, &wline[scrollx]);
8687 wattr_off(view->window,
8688 COLOR_PAIR(tc->colorpair), NULL);
8689 if (width < view->ncols)
8690 waddch(view->window, '\n');
8691 if (n == s->selected && view->focussed)
8692 wstandend(view->window);
8698 s->last_displayed_entry = re;
8701 re = TAILQ_NEXT(re, entry);
8708 static const struct got_error *
8709 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8710 struct tog_reflist_entry *re, struct got_repository *repo)
8712 const struct got_error *err = NULL;
8713 struct got_object_id *commit_id = NULL;
8714 struct tog_view *tree_view;
8718 err = resolve_reflist_entry(&commit_id, re, repo);
8720 if (err->code != GOT_ERR_OBJ_TYPE)
8727 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8728 if (tree_view == NULL) {
8729 err = got_error_from_errno("view_open");
8733 err = open_tree_view(tree_view, commit_id,
8734 got_ref_get_name(re->ref), repo);
8738 *new_view = tree_view;
8744 static const struct got_error *
8745 ref_goto_line(struct tog_view *view, int nlines)
8747 const struct got_error *err = NULL;
8748 struct tog_ref_view_state *s = &view->state.ref;
8749 int g, idx = s->selected_entry->idx;
8756 else if (g > s->nrefs)
8759 if (g >= s->first_displayed_entry->idx + 1 &&
8760 g <= s->last_displayed_entry->idx + 1 &&
8761 g - s->first_displayed_entry->idx - 1 < nlines) {
8762 s->selected = g - s->first_displayed_entry->idx - 1;
8767 err = ref_scroll_down(view, g - idx - 1);
8770 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8771 s->first_displayed_entry->idx + s->selected < g &&
8772 s->selected < s->ndisplayed - 1)
8773 s->selected = g - s->first_displayed_entry->idx - 1;
8774 } else if (idx + 1 > g)
8775 ref_scroll_up(s, idx - g + 1);
8777 if (g < nlines && s->first_displayed_entry->idx == 0)
8778 s->selected = g - 1;
8784 static const struct got_error *
8785 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8787 const struct got_error *err = NULL;
8788 struct tog_ref_view_state *s = &view->state.ref;
8789 struct tog_reflist_entry *re;
8790 int n, nscroll = view->nlines - 1;
8793 return ref_goto_line(view, nscroll);
8802 horizontal_scroll_input(view, ch);
8805 s->show_ids = !s->show_ids;
8809 s->show_date = !s->show_date;
8813 s->sort_by_date = !s->sort_by_date;
8814 view->action = s->sort_by_date ? "sort by date" : "sort by name";
8816 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8817 got_ref_cmp_by_commit_timestamp_descending :
8818 tog_ref_cmp_by_name, s->repo);
8821 got_reflist_object_id_map_free(tog_refs_idmap);
8822 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8823 &tog_refs, s->repo);
8826 ref_view_free_refs(s);
8827 err = ref_view_load_refs(s);
8832 if (!s->selected_entry)
8834 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8838 if (!s->selected_entry)
8840 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8847 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8852 int eos = view->nlines - 1;
8854 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8858 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8859 for (n = 0; n < eos; n++) {
8862 s->first_displayed_entry = re;
8863 re = TAILQ_PREV(re, tog_reflist_head, entry);
8866 s->selected = n - 1;
8872 if (s->selected > 0) {
8876 ref_scroll_up(s, 1);
8877 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8887 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8888 s->selected -= MIN(nscroll, s->selected);
8889 ref_scroll_up(s, MAX(0, nscroll));
8890 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8896 if (s->selected < s->ndisplayed - 1) {
8900 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8901 /* can't scroll any further */
8905 ref_scroll_down(view, 1);
8915 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8916 /* can't scroll any further; move cursor down */
8917 if (s->selected < s->ndisplayed - 1)
8918 s->selected += MIN(nscroll,
8919 s->ndisplayed - s->selected - 1);
8920 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8921 s->selected += s->ndisplayed - s->selected - 1;
8925 ref_scroll_down(view, nscroll);
8930 err = tog_load_refs(s->repo, s->sort_by_date);
8933 ref_view_free_refs(s);
8934 err = ref_view_load_refs(s);
8937 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8938 s->selected = view->nlines - 2;
8952 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8957 static const struct got_error *
8958 cmd_ref(int argc, char *argv[])
8960 const struct got_error *error;
8961 struct got_repository *repo = NULL;
8962 struct got_worktree *worktree = NULL;
8963 char *cwd = NULL, *repo_path = NULL;
8965 struct tog_view *view;
8966 int *pack_fds = NULL;
8968 while ((ch = getopt(argc, argv, "r:")) != -1) {
8971 repo_path = realpath(optarg, NULL);
8972 if (repo_path == NULL)
8973 return got_error_from_errno2("realpath",
8988 error = got_repo_pack_fds_open(&pack_fds);
8992 if (repo_path == NULL) {
8993 cwd = getcwd(NULL, 0);
8995 return got_error_from_errno("getcwd");
8996 error = got_worktree_open(&worktree, cwd, NULL);
8997 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9001 strdup(got_worktree_get_repo_path(worktree));
9003 repo_path = strdup(cwd);
9004 if (repo_path == NULL) {
9005 error = got_error_from_errno("strdup");
9010 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9016 error = apply_unveil(got_repo_get_path(repo), NULL);
9020 error = tog_load_refs(repo, 0);
9024 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
9026 error = got_error_from_errno("view_open");
9030 error = open_ref_view(view, repo);
9035 /* Release work tree lock. */
9036 got_worktree_close(worktree);
9039 error = view_loop(view);
9044 const struct got_error *close_err = got_repo_close(repo);
9049 const struct got_error *pack_err =
9050 got_repo_pack_fds_close(pack_fds);
9058 static const struct got_error*
9059 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
9068 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
9072 if (mvwprintw(win, y, x, "%s", str) == ERR)
9073 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
9080 static const struct got_error *
9081 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
9085 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
9087 free(*line_offsets);
9088 *line_offsets = NULL;
9089 return got_error_from_errno("reallocarray");
9093 (*line_offsets)[*nlines] = off;
9098 static const struct got_error *
9099 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
9103 for (;n > 0; --n, ++km) {
9107 if (km->keys == NULL)
9110 t = t0 = strdup(km->keys);
9112 return got_error_from_errno("strdup");
9115 while ((k = strsep(&t, " ")) != NULL)
9116 len += strlen(k) > 1 ? 2 : 0;
9118 *ret = MAX(*ret, len);
9125 * Write keymap section headers, keys, and key info in km to f.
9126 * Save line offset to *off. If terminal has UTF8 encoding enabled,
9127 * wrap control and symbolic keys in guillemets, else use <>.
9129 static const struct got_error *
9130 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
9135 static const char *u8_glyph[] = {
9136 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
9137 "\xe2\x80\xba" /* U+203A (utf8 >) */
9140 int cs, s, first = 1;
9142 cs = got_locale_is_utf8();
9144 t = t0 = strdup(km->keys);
9146 return got_error_from_errno("strdup");
9148 len = strlen(km->keys);
9149 while ((k = strsep(&t, " ")) != NULL) {
9150 s = strlen(k) > 1; /* control or symbolic key */
9151 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
9152 cs && s ? u8_glyph[0] : s ? "<" : "", k,
9153 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
9156 return got_error_from_errno("fprintf");
9164 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
9166 return got_error_from_errno("fprintf");
9172 static const struct got_error *
9173 format_help(struct tog_help_view_state *s)
9175 const struct got_error *err = NULL;
9177 int i, max, n, show = s->all;
9178 static const struct tog_key_map km[] = {
9179 #define KEYMAP_(info, type) { NULL, (info), type }
9180 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
9186 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
9191 err = max_key_str(&max, km, n);
9195 for (i = 0; i < n; ++i) {
9196 if (km[i].keys == NULL) {
9198 if (km[i].type == TOG_KEYMAP_GLOBAL ||
9199 km[i].type == s->type || s->all)
9203 err = format_help_line(&off, s->f, &km[i], max);
9206 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9213 err = add_line_offset(&s->line_offsets, &s->nlines, off);
9217 static const struct got_error *
9218 create_help(struct tog_help_view_state *s)
9221 const struct got_error *err;
9223 free(s->line_offsets);
9224 s->line_offsets = NULL;
9229 return got_error_from_errno("got_opentemp");
9232 err = format_help(s);
9236 if (s->f && fflush(s->f) != 0)
9237 return got_error_from_errno("fflush");
9242 static const struct got_error *
9243 search_start_help_view(struct tog_view *view)
9245 view->state.help.matched_line = 0;
9250 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
9251 size_t *nlines, int **first, int **last, int **match, int **selected)
9253 struct tog_help_view_state *s = &view->state.help;
9256 *nlines = s->nlines;
9257 *line_offsets = s->line_offsets;
9258 *match = &s->matched_line;
9259 *first = &s->first_displayed_line;
9260 *last = &s->last_displayed_line;
9261 *selected = &s->selected_line;
9264 static const struct got_error *
9265 show_help_view(struct tog_view *view)
9267 struct tog_help_view_state *s = &view->state.help;
9268 const struct got_error *err;
9269 regmatch_t *regmatch = &view->regmatch;
9274 int width, nprinted = 0, rc = 0;
9275 int eos = view->nlines;
9277 if (view_is_hsplit_top(view))
9278 --eos; /* account for border */
9282 werase(view->window);
9284 if (view->gline > s->nlines - 1)
9285 view->gline = s->nlines - 1;
9287 err = win_draw_center(view->window, 0, 0, view->ncols,
9288 view_needs_focus_indication(view),
9289 "tog help (press q to return to tog)");
9294 waddstr(view->window, "\n\n");
9300 while (eos > 0 && nprinted < eos) {
9303 linelen = getline(&line, &linesz, s->f);
9304 if (linelen == -1) {
9307 return got_ferror(s->f, GOT_ERR_IO);
9312 if (++s->lineno < s->first_displayed_line)
9314 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
9316 if (s->lineno == view->hiline)
9319 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
9325 view->maxx = MAX(view->maxx, width);
9330 wattron(view->window, attr);
9331 if (s->first_displayed_line + nprinted == s->matched_line &&
9332 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
9333 err = add_matched_line(&width, line, view->ncols - 1, 0,
9334 view->window, view->x, regmatch);
9342 err = format_line(&wline, &width, &skip, line,
9343 view->x, view->ncols, 0, view->x ? 1 : 0);
9348 waddwstr(view->window, &wline[skip]);
9352 if (s->lineno == view->hiline) {
9353 while (width++ < view->ncols)
9354 waddch(view->window, ' ');
9356 if (width < view->ncols)
9357 waddch(view->window, '\n');
9360 wattroff(view->window, attr);
9361 if (++nprinted == 1)
9362 s->first_displayed_line = s->lineno;
9366 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
9368 s->last_displayed_line = s->first_displayed_line;
9373 rc = waddnstr(view->window,
9374 "See the tog(1) manual page for full documentation",
9377 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9379 wmove(view->window, view->nlines - 1, 0);
9380 wclrtoeol(view->window);
9381 wstandout(view->window);
9382 rc = waddnstr(view->window, "scroll down for more...",
9385 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9386 if (getcurx(view->window) < view->ncols - 6) {
9387 rc = wprintw(view->window, "[%.0f%%]",
9388 100.00 * s->last_displayed_line / s->nlines);
9390 return got_error_msg(GOT_ERR_IO, "wprintw");
9392 wstandend(view->window);
9398 static const struct got_error *
9399 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
9401 struct tog_help_view_state *s = &view->state.help;
9402 const struct got_error *err = NULL;
9408 eos = nscroll = view->nlines;
9409 if (view_is_hsplit_top(view))
9412 s->lineno = s->first_displayed_line - 1 + s->selected_line;
9421 horizontal_scroll_input(view, ch);
9425 s->first_displayed_line = 1;
9433 s->first_displayed_line = (s->nlines - eos) + 3;
9438 if (s->first_displayed_line > 1)
9439 --s->first_displayed_line;
9450 if (s->first_displayed_line == 1) {
9454 while (--nscroll > 0 && s->first_displayed_line > 1)
9455 s->first_displayed_line--;
9461 ++s->first_displayed_line;
9477 while (!s->eof && --nscroll > 0) {
9478 linelen = getline(&line, &linesz, s->f);
9479 s->first_displayed_line++;
9480 if (linelen == -1) {
9484 err = got_ferror(s->f, GOT_ERR_IO);
9498 static const struct got_error *
9499 close_help_view(struct tog_view *view)
9501 struct tog_help_view_state *s = &view->state.help;
9503 free(s->line_offsets);
9504 s->line_offsets = NULL;
9505 if (fclose(s->f) == EOF)
9506 return got_error_from_errno("fclose");
9511 static const struct got_error *
9512 reset_help_view(struct tog_view *view)
9514 struct tog_help_view_state *s = &view->state.help;
9517 if (s->f && fclose(s->f) == EOF)
9518 return got_error_from_errno("fclose");
9520 wclear(view->window);
9524 s->first_displayed_line = 1;
9525 s->last_displayed_line = view->nlines;
9526 s->matched_line = 0;
9528 return create_help(s);
9531 static const struct got_error *
9532 open_help_view(struct tog_view *view, struct tog_view *parent)
9534 const struct got_error *err = NULL;
9535 struct tog_help_view_state *s = &view->state.help;
9537 s->type = (enum tog_keymap_type)parent->type;
9538 s->first_displayed_line = 1;
9539 s->last_displayed_line = view->nlines;
9540 s->selected_line = 1;
9542 view->show = show_help_view;
9543 view->input = input_help_view;
9544 view->reset = reset_help_view;
9545 view->close = close_help_view;
9546 view->search_start = search_start_help_view;
9547 view->search_setup = search_setup_help_view;
9548 view->search_next = search_next_view_match;
9550 err = create_help(s);
9554 static const struct got_error *
9555 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9556 enum tog_view_type request, int y, int x)
9558 const struct got_error *err = NULL;
9564 if (view->type == TOG_VIEW_LOG) {
9565 struct tog_log_view_state *s = &view->state.log;
9567 err = open_diff_view_for_commit(new_view, y, x,
9568 s->selected_entry->commit, s->selected_entry->id,
9571 return got_error_msg(GOT_ERR_NOT_IMPL,
9572 "parent/child view pair not supported");
9574 case TOG_VIEW_BLAME:
9575 if (view->type == TOG_VIEW_TREE) {
9576 struct tog_tree_view_state *s = &view->state.tree;
9578 err = blame_tree_entry(new_view, y, x,
9579 s->selected_entry, &s->parents, s->commit_id,
9582 return got_error_msg(GOT_ERR_NOT_IMPL,
9583 "parent/child view pair not supported");
9586 if (view->type == TOG_VIEW_BLAME)
9587 err = log_annotated_line(new_view, y, x,
9588 view->state.blame.repo, view->state.blame.id_to_log);
9589 else if (view->type == TOG_VIEW_TREE)
9590 err = log_selected_tree_entry(new_view, y, x,
9592 else if (view->type == TOG_VIEW_REF)
9593 err = log_ref_entry(new_view, y, x,
9594 view->state.ref.selected_entry,
9595 view->state.ref.repo);
9597 return got_error_msg(GOT_ERR_NOT_IMPL,
9598 "parent/child view pair not supported");
9601 if (view->type == TOG_VIEW_LOG)
9602 err = browse_commit_tree(new_view, y, x,
9603 view->state.log.selected_entry,
9604 view->state.log.in_repo_path,
9605 view->state.log.head_ref_name,
9606 view->state.log.repo);
9607 else if (view->type == TOG_VIEW_REF)
9608 err = browse_ref_tree(new_view, y, x,
9609 view->state.ref.selected_entry,
9610 view->state.ref.repo);
9612 return got_error_msg(GOT_ERR_NOT_IMPL,
9613 "parent/child view pair not supported");
9616 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9617 if (*new_view == NULL)
9618 return got_error_from_errno("view_open");
9619 if (view->type == TOG_VIEW_LOG)
9620 err = open_ref_view(*new_view, view->state.log.repo);
9621 else if (view->type == TOG_VIEW_TREE)
9622 err = open_ref_view(*new_view, view->state.tree.repo);
9624 err = got_error_msg(GOT_ERR_NOT_IMPL,
9625 "parent/child view pair not supported");
9627 view_close(*new_view);
9630 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9631 if (*new_view == NULL)
9632 return got_error_from_errno("view_open");
9633 err = open_help_view(*new_view, view);
9635 view_close(*new_view);
9638 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9645 * If view was scrolled down to move the selected line into view when opening a
9646 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9649 offset_selection_up(struct tog_view *view)
9651 switch (view->type) {
9652 case TOG_VIEW_BLAME: {
9653 struct tog_blame_view_state *s = &view->state.blame;
9654 if (s->first_displayed_line == 1) {
9655 s->selected_line = MAX(s->selected_line - view->offset,
9659 if (s->first_displayed_line > view->offset)
9660 s->first_displayed_line -= view->offset;
9662 s->first_displayed_line = 1;
9663 s->selected_line += view->offset;
9667 log_scroll_up(&view->state.log, view->offset);
9668 view->state.log.selected += view->offset;
9671 ref_scroll_up(&view->state.ref, view->offset);
9672 view->state.ref.selected += view->offset;
9675 tree_scroll_up(&view->state.tree, view->offset);
9676 view->state.tree.selected += view->offset;
9686 * If the selected line is in the section of screen covered by the bottom split,
9687 * scroll down offset lines to move it into view and index its new position.
9689 static const struct got_error *
9690 offset_selection_down(struct tog_view *view)
9692 const struct got_error *err = NULL;
9693 const struct got_error *(*scrolld)(struct tog_view *, int);
9694 int *selected = NULL;
9697 switch (view->type) {
9698 case TOG_VIEW_BLAME: {
9699 struct tog_blame_view_state *s = &view->state.blame;
9702 if (s->selected_line > view->nlines - header) {
9703 offset = abs(view->nlines - s->selected_line - header);
9704 s->first_displayed_line += offset;
9705 s->selected_line -= offset;
9706 view->offset = offset;
9710 case TOG_VIEW_LOG: {
9711 struct tog_log_view_state *s = &view->state.log;
9712 scrolld = &log_scroll_down;
9713 header = view_is_parent_view(view) ? 3 : 2;
9714 selected = &s->selected;
9717 case TOG_VIEW_REF: {
9718 struct tog_ref_view_state *s = &view->state.ref;
9719 scrolld = &ref_scroll_down;
9721 selected = &s->selected;
9724 case TOG_VIEW_TREE: {
9725 struct tog_tree_view_state *s = &view->state.tree;
9726 scrolld = &tree_scroll_down;
9728 selected = &s->selected;
9738 if (selected && *selected > view->nlines - header) {
9739 offset = abs(view->nlines - *selected - header);
9740 view->offset = offset;
9741 if (scrolld && offset) {
9742 err = scrolld(view, offset);
9743 *selected -= offset;
9751 list_commands(FILE *fp)
9755 fprintf(fp, "commands:");
9756 for (i = 0; i < nitems(tog_commands); i++) {
9757 const struct tog_cmd *cmd = &tog_commands[i];
9758 fprintf(fp, " %s", cmd->name);
9764 usage(int hflag, int status)
9766 FILE *fp = (status == 0) ? stdout : stderr;
9768 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9771 fprintf(fp, "lazy usage: %s path\n", getprogname());
9778 make_argv(int argc, ...)
9786 argv = calloc(argc, sizeof(char *));
9789 for (i = 0; i < argc; i++) {
9790 argv[i] = strdup(va_arg(ap, char *));
9791 if (argv[i] == NULL)
9800 * Try to convert 'tog path' into a 'tog log path' command.
9801 * The user could simply have mistyped the command rather than knowingly
9802 * provided a path. So check whether argv[0] can in fact be resolved
9803 * to a path in the HEAD commit and print a special error if not.
9804 * This hack is for mpi@ <3
9806 static const struct got_error *
9807 tog_log_with_path(int argc, char *argv[])
9809 const struct got_error *error = NULL, *close_err;
9810 const struct tog_cmd *cmd = NULL;
9811 struct got_repository *repo = NULL;
9812 struct got_worktree *worktree = NULL;
9813 struct got_object_id *commit_id = NULL, *id = NULL;
9814 struct got_commit_object *commit = NULL;
9815 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9816 char *commit_id_str = NULL, **cmd_argv = NULL;
9817 int *pack_fds = NULL;
9819 cwd = getcwd(NULL, 0);
9821 return got_error_from_errno("getcwd");
9823 error = got_repo_pack_fds_open(&pack_fds);
9827 error = got_worktree_open(&worktree, cwd, NULL);
9828 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9832 repo_path = strdup(got_worktree_get_repo_path(worktree));
9834 repo_path = strdup(cwd);
9835 if (repo_path == NULL) {
9836 error = got_error_from_errno("strdup");
9840 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9844 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9849 error = tog_load_refs(repo, 0);
9852 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9853 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9854 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9859 got_worktree_close(worktree);
9863 error = got_object_open_as_commit(&commit, repo, commit_id);
9867 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9869 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9871 fprintf(stderr, "%s: '%s' is no known command or path\n",
9872 getprogname(), argv[0]);
9877 error = got_object_id_str(&commit_id_str, commit_id);
9881 cmd = &tog_commands[0]; /* log */
9883 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9884 error = cmd->cmd_main(argc, cmd_argv);
9887 close_err = got_repo_close(repo);
9892 got_object_commit_close(commit);
9894 got_worktree_close(worktree);
9896 const struct got_error *pack_err =
9897 got_repo_pack_fds_close(pack_fds);
9902 free(commit_id_str);
9909 for (i = 0; i < argc; i++)
9918 main(int argc, char *argv[])
9920 const struct got_error *io_err, *error = NULL;
9921 const struct tog_cmd *cmd = NULL;
9922 int ch, hflag = 0, Vflag = 0;
9923 char **cmd_argv = NULL;
9924 static const struct option longopts[] = {
9925 { "version", no_argument, NULL, 'V' },
9928 char *diff_algo_str = NULL;
9929 const char *test_script_path;
9931 setlocale(LC_CTYPE, "");
9934 * Test mode init must happen before pledge() because "tty" will
9935 * not allow TTY-related ioctls to occur via regular files.
9937 test_script_path = getenv("TOG_TEST_SCRIPT");
9938 if (test_script_path != NULL) {
9939 error = init_mock_term(test_script_path);
9941 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9944 } else if (!isatty(STDIN_FILENO))
9945 errx(1, "standard input is not a tty");
9947 #if !defined(PROFILE)
9948 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9953 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9973 got_version_print_str();
9980 /* Build an argument vector which runs a default command. */
9981 cmd = &tog_commands[0];
9983 cmd_argv = make_argv(argc, cmd->name);
9987 /* Did the user specify a command? */
9988 for (i = 0; i < nitems(tog_commands); i++) {
9989 if (strncmp(tog_commands[i].name, argv[0],
9990 strlen(argv[0])) == 0) {
9991 cmd = &tog_commands[i];
9997 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9998 if (diff_algo_str) {
9999 if (strcasecmp(diff_algo_str, "patience") == 0)
10000 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
10001 if (strcasecmp(diff_algo_str, "myers") == 0)
10002 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
10008 /* No command specified; try log with a path */
10009 error = tog_log_with_path(argc, argv);
10014 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
10017 if (using_mock_io) {
10018 io_err = tog_io_close();
10025 for (i = 0; i < argc; i++)
10030 if (error && error->code != GOT_ERR_CANCELLED &&
10031 error->code != GOT_ERR_EOF &&
10032 error->code != GOT_ERR_PRIVSEP_EXIT &&
10033 error->code != GOT_ERR_PRIVSEP_PIPE &&
10034 !(error->code == GOT_ERR_ERRNO && errno == EINTR)) {
10035 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);