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 */
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_opentemp.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
57 #include "got_worktree.h"
58 #include "got_keyword.h"
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
65 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 #define CTRL(x) ((x) & 0x1f)
71 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
76 const struct got_error *(*cmd_main)(int, char *[]);
77 void (*cmd_usage)(void);
80 __dead static void usage(int, int);
81 __dead static void usage_log(void);
82 __dead static void usage_diff(void);
83 __dead static void usage_blame(void);
84 __dead static void usage_tree(void);
85 __dead static void usage_ref(void);
87 static const struct got_error* cmd_log(int, char *[]);
88 static const struct got_error* cmd_diff(int, char *[]);
89 static const struct got_error* cmd_blame(int, char *[]);
90 static const struct got_error* cmd_tree(int, char *[]);
91 static const struct got_error* cmd_ref(int, char *[]);
93 static const struct tog_cmd tog_commands[] = {
94 { "log", cmd_log, usage_log },
95 { "diff", cmd_diff, usage_diff },
96 { "blame", cmd_blame, usage_blame },
97 { "tree", cmd_tree, usage_tree },
98 { "ref", cmd_ref, usage_ref },
110 /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
111 enum tog_keymap_type {
112 TOG_KEYMAP_KEYS = -2,
128 #define HSPLIT_SCALE 0.3f /* default horizontal split scale */
130 #define TOG_EOF_STRING "(END)"
132 struct commit_queue_entry {
133 TAILQ_ENTRY(commit_queue_entry) entry;
134 struct got_object_id *id;
135 struct got_commit_object *commit;
138 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
139 struct commit_queue {
141 struct commit_queue_head head;
145 STAILQ_ENTRY(tog_color) entry;
149 STAILQ_HEAD(tog_colors, tog_color);
151 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
152 static struct got_reflist_object_id_map *tog_refs_idmap;
154 struct got_object_id *id;
158 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
160 static const struct got_error *
161 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
162 struct got_reference* re2)
164 const char *name1 = got_ref_get_name(re1);
165 const char *name2 = got_ref_get_name(re2);
166 int isbackup1, isbackup2;
168 /* Sort backup refs towards the bottom of the list. */
169 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
170 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
171 if (!isbackup1 && isbackup2) {
174 } else if (isbackup1 && !isbackup2) {
179 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
183 static const struct got_error *
184 tog_load_refs(struct got_repository *repo, int sort_by_date)
186 const struct got_error *err;
188 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
189 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
194 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
201 if (tog_refs_idmap) {
202 got_reflist_object_id_map_free(tog_refs_idmap);
203 tog_refs_idmap = NULL;
205 got_ref_list_free(&tog_refs);
208 static const struct got_error *
209 add_color(struct tog_colors *colors, const char *pattern,
210 int idx, short color)
212 const struct got_error *err = NULL;
213 struct tog_color *tc;
216 if (idx < 1 || idx > COLOR_PAIRS - 1)
219 init_pair(idx, color, -1);
221 tc = calloc(1, sizeof(*tc));
223 return got_error_from_errno("calloc");
224 regerr = regcomp(&tc->regex, pattern,
225 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
227 static char regerr_msg[512];
228 static char err_msg[512];
229 regerror(regerr, &tc->regex, regerr_msg,
231 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
233 err = got_error_msg(GOT_ERR_REGEX, err_msg);
238 STAILQ_INSERT_HEAD(colors, tc, entry);
243 free_colors(struct tog_colors *colors)
245 struct tog_color *tc;
247 while (!STAILQ_EMPTY(colors)) {
248 tc = STAILQ_FIRST(colors);
249 STAILQ_REMOVE_HEAD(colors, entry);
255 static struct tog_color *
256 get_color(struct tog_colors *colors, int colorpair)
258 struct tog_color *tc = NULL;
260 STAILQ_FOREACH(tc, colors, entry) {
261 if (tc->colorpair == colorpair)
269 default_color_value(const char *envvar)
271 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
272 return COLOR_MAGENTA;
273 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
275 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
277 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
279 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
280 return COLOR_MAGENTA;
281 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
282 return COLOR_MAGENTA;
283 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
285 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
287 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
289 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
291 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
293 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
295 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
296 return COLOR_MAGENTA;
297 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
299 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
306 get_color_value(const char *envvar)
308 const char *val = getenv(envvar);
311 return default_color_value(envvar);
313 if (strcasecmp(val, "black") == 0)
315 if (strcasecmp(val, "red") == 0)
317 if (strcasecmp(val, "green") == 0)
319 if (strcasecmp(val, "yellow") == 0)
321 if (strcasecmp(val, "blue") == 0)
323 if (strcasecmp(val, "magenta") == 0)
324 return COLOR_MAGENTA;
325 if (strcasecmp(val, "cyan") == 0)
327 if (strcasecmp(val, "white") == 0)
329 if (strcasecmp(val, "default") == 0)
332 return default_color_value(envvar);
335 struct tog_diff_view_state {
336 struct got_object_id *id1, *id2;
337 const char *label1, *label2;
341 int first_displayed_line;
342 int last_displayed_line;
345 int ignore_whitespace;
347 struct got_repository *repo;
348 struct got_diff_line *lines;
353 /* passed from log or blame view; may be NULL */
354 struct tog_view *parent_view;
357 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
358 static volatile sig_atomic_t tog_thread_error;
360 struct tog_log_thread_args {
361 pthread_cond_t need_commits;
362 pthread_cond_t commit_loaded;
365 struct got_commit_graph *graph;
366 struct commit_queue *real_commits;
367 const char *in_repo_path;
368 struct got_object_id *start_id;
369 struct got_repository *repo;
372 pthread_cond_t log_loaded;
374 struct commit_queue_entry **first_displayed_entry;
375 struct commit_queue_entry **selected_entry;
377 int *search_next_done;
381 regex_t *limit_regex;
382 struct commit_queue *limit_commits;
383 struct got_worktree *worktree;
384 int need_commit_marker;
387 struct tog_log_view_state {
388 struct commit_queue *commits;
389 struct commit_queue_entry *first_displayed_entry;
390 struct commit_queue_entry *last_displayed_entry;
391 struct commit_queue_entry *selected_entry;
392 struct commit_queue real_commits;
397 struct got_repository *repo;
398 struct got_object_id *start_id;
401 struct tog_log_thread_args thread_args;
402 struct commit_queue_entry *matched_entry;
403 struct commit_queue_entry *search_entry;
404 struct tog_colors colors;
408 struct commit_queue limit_commits;
411 #define TOG_COLOR_DIFF_MINUS 1
412 #define TOG_COLOR_DIFF_PLUS 2
413 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
414 #define TOG_COLOR_DIFF_META 4
415 #define TOG_COLOR_TREE_SUBMODULE 5
416 #define TOG_COLOR_TREE_SYMLINK 6
417 #define TOG_COLOR_TREE_DIRECTORY 7
418 #define TOG_COLOR_TREE_EXECUTABLE 8
419 #define TOG_COLOR_COMMIT 9
420 #define TOG_COLOR_AUTHOR 10
421 #define TOG_COLOR_DATE 11
422 #define TOG_COLOR_REFS_HEADS 12
423 #define TOG_COLOR_REFS_TAGS 13
424 #define TOG_COLOR_REFS_REMOTES 14
425 #define TOG_COLOR_REFS_BACKUP 15
427 struct tog_blame_cb_args {
428 struct tog_blame_line *lines; /* one per line */
431 struct tog_view *view;
432 struct got_object_id *commit_id;
436 struct tog_blame_thread_args {
438 struct got_repository *repo;
439 struct tog_blame_cb_args *cb_args;
441 got_cancel_cb cancel_cb;
443 pthread_cond_t blame_complete;
449 struct tog_blame_line *lines;
453 struct tog_blame_thread_args thread_args;
454 struct tog_blame_cb_args cb_args;
459 struct tog_blame_view_state {
460 int first_displayed_line;
461 int last_displayed_line;
463 int last_diffed_line;
467 struct got_object_id_queue blamed_commits;
468 struct got_object_qid *blamed_commit;
470 struct got_repository *repo;
471 struct got_object_id *commit_id;
472 struct got_object_id *id_to_log;
473 struct tog_blame blame;
475 struct tog_colors colors;
478 struct tog_parent_tree {
479 TAILQ_ENTRY(tog_parent_tree) entry;
480 struct got_tree_object *tree;
481 struct got_tree_entry *first_displayed_entry;
482 struct got_tree_entry *selected_entry;
486 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
488 struct tog_tree_view_state {
490 struct got_object_id *commit_id;/* commit which this tree belongs to */
491 struct got_tree_object *root; /* the commit's root tree entry */
492 struct got_tree_object *tree; /* currently displayed (sub-)tree */
493 struct got_tree_entry *first_displayed_entry;
494 struct got_tree_entry *last_displayed_entry;
495 struct got_tree_entry *selected_entry;
496 int ndisplayed, selected, show_ids;
497 struct tog_parent_trees parents; /* parent trees of current sub-tree */
499 struct got_repository *repo;
500 struct got_tree_entry *matched_entry;
501 struct tog_colors colors;
504 struct tog_reflist_entry {
505 TAILQ_ENTRY(tog_reflist_entry) entry;
506 struct got_reference *ref;
510 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
512 struct tog_ref_view_state {
513 struct tog_reflist_head refs;
514 struct tog_reflist_entry *first_displayed_entry;
515 struct tog_reflist_entry *last_displayed_entry;
516 struct tog_reflist_entry *selected_entry;
517 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
518 struct got_repository *repo;
519 struct tog_reflist_entry *matched_entry;
520 struct tog_colors colors;
523 struct tog_help_view_state {
528 int first_displayed_line;
529 int last_displayed_line;
534 enum tog_keymap_type type;
537 #define GENERATE_HELP \
538 KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
539 KEY_("H F1", "Open view-specific help (double tap for all help)"), \
540 KEY_("k C-p Up", "Move cursor or page up one line"), \
541 KEY_("j C-n Down", "Move cursor or page down one line"), \
542 KEY_("C-b b PgUp", "Scroll the view up one page"), \
543 KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
544 KEY_("C-u u", "Scroll the view up one half page"), \
545 KEY_("C-d d", "Scroll the view down one half page"), \
546 KEY_("g", "Go to line N (default: first line)"), \
547 KEY_("Home =", "Go to the first line"), \
548 KEY_("G", "Go to line N (default: last line)"), \
549 KEY_("End *", "Go to the last line"), \
550 KEY_("l Right", "Scroll the view right"), \
551 KEY_("h Left", "Scroll the view left"), \
552 KEY_("$", "Scroll view to the rightmost position"), \
553 KEY_("0", "Scroll view to the leftmost position"), \
554 KEY_("-", "Decrease size of the focussed split"), \
555 KEY_("+", "Increase size of the focussed split"), \
556 KEY_("Tab", "Switch focus between views"), \
557 KEY_("F", "Toggle fullscreen mode"), \
558 KEY_("S", "Switch split-screen layout"), \
559 KEY_("/", "Open prompt to enter search term"), \
560 KEY_("n", "Find next line/token matching the current search term"), \
561 KEY_("N", "Find previous line/token matching the current search term"),\
562 KEY_("q", "Quit the focussed view; Quit help screen"), \
563 KEY_("Q", "Quit tog"), \
565 KEYMAP_("Log view", TOG_KEYMAP_LOG), \
566 KEY_("< ,", "Move cursor up one commit"), \
567 KEY_("> .", "Move cursor down one commit"), \
568 KEY_("Enter", "Open diff view of the selected commit"), \
569 KEY_("B", "Reload the log view and toggle display of merged commits"), \
570 KEY_("R", "Open ref view of all repository references"), \
571 KEY_("T", "Display tree view of the repository from the selected" \
573 KEY_("@", "Toggle between displaying author and committer name"), \
574 KEY_("&", "Open prompt to enter term to limit commits displayed"), \
575 KEY_("C-g Backspace", "Cancel current search or log operation"), \
576 KEY_("C-l", "Reload the log view with new commits in the repository"), \
578 KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
579 KEY_("K < ,", "Display diff of next line in the file/log entry"), \
580 KEY_("J > .", "Display diff of previous line in the file/log entry"), \
581 KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
582 KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
584 KEY_("(", "Go to the previous file in the diff"), \
585 KEY_(")", "Go to the next file in the diff"), \
586 KEY_("{", "Go to the previous hunk in the diff"), \
587 KEY_("}", "Go to the next hunk in the diff"), \
588 KEY_("[", "Decrease the number of context lines"), \
589 KEY_("]", "Increase the number of context lines"), \
590 KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
592 KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
593 KEY_("Enter", "Display diff view of the selected line's commit"), \
594 KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
595 KEY_("L", "Open log view for the currently selected annotated line"), \
596 KEY_("C", "Reload view with the previously blamed commit"), \
597 KEY_("c", "Reload view with the version of the file found in the" \
598 " selected line's commit"), \
599 KEY_("p", "Reload view with the version of the file found in the" \
600 " selected line's parent commit"), \
602 KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
603 KEY_("Enter", "Enter selected directory or open blame view of the" \
605 KEY_("L", "Open log view for the selected entry"), \
606 KEY_("R", "Open ref view of all repository references"), \
607 KEY_("i", "Show object IDs for all tree entries"), \
608 KEY_("Backspace", "Return to the parent directory"), \
610 KEYMAP_("Ref view", TOG_KEYMAP_REF), \
611 KEY_("Enter", "Display log view of the selected reference"), \
612 KEY_("T", "Display tree view of the selected reference"), \
613 KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
614 KEY_("m", "Toggle display of last modified date for each reference"), \
615 KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
616 KEY_("C-l", "Reload view with all repository references")
621 enum tog_keymap_type type;
624 /* curses io for tog regress */
633 static int using_mock_io;
635 #define TOG_KEY_SCRDUMP SHRT_MIN
638 * We implement two types of views: parent views and child views.
640 * The 'Tab' key switches focus between a parent view and its child view.
641 * Child views are shown side-by-side to their parent view, provided
642 * there is enough screen estate.
644 * When a new view is opened from within a parent view, this new view
645 * becomes a child view of the parent view, replacing any existing child.
647 * When a new view is opened from within a child view, this new view
648 * becomes a parent view which will obscure the views below until the
649 * user quits the new parent view by typing 'q'.
651 * This list of views contains parent views only.
652 * Child views are only pointed to by their parent view.
654 TAILQ_HEAD(tog_view_list_head, tog_view);
657 TAILQ_ENTRY(tog_view) entry;
660 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
661 int resized_y, resized_x; /* begin_y/x based on user resizing */
662 int maxx, x; /* max column and current start column */
663 int lines, cols; /* copies of LINES and COLS */
664 int nscrolled, offset; /* lines scrolled and hsplit line offset */
665 int gline, hiline; /* navigate to and highlight this nG line */
666 int ch, count; /* current keymap and count prefix */
667 int resized; /* set when in a resize event */
668 int focussed; /* Only set on one parent or child view at a time. */
670 struct tog_view *parent;
671 struct tog_view *child;
674 * This flag is initially set on parent views when a new child view
675 * is created. It gets toggled when the 'Tab' key switches focus
676 * between parent and child.
677 * The flag indicates whether focus should be passed on to our child
678 * view if this parent view gets picked for focus after another parent
679 * view was closed. This prevents child views from losing focus in such
684 enum tog_view_mode mode;
685 /* type-specific state */
686 enum tog_view_type type;
688 struct tog_diff_view_state diff;
689 struct tog_log_view_state log;
690 struct tog_blame_view_state blame;
691 struct tog_tree_view_state tree;
692 struct tog_ref_view_state ref;
693 struct tog_help_view_state help;
696 const struct got_error *(*show)(struct tog_view *);
697 const struct got_error *(*input)(struct tog_view **,
698 struct tog_view *, int);
699 const struct got_error *(*reset)(struct tog_view *);
700 const struct got_error *(*resize)(struct tog_view *, int);
701 const struct got_error *(*close)(struct tog_view *);
703 const struct got_error *(*search_start)(struct tog_view *);
704 const struct got_error *(*search_next)(struct tog_view *);
705 void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
706 int **, int **, int **, int **);
709 #define TOG_SEARCH_FORWARD 1
710 #define TOG_SEARCH_BACKWARD 2
711 int search_next_done;
712 #define TOG_SEARCH_HAVE_MORE 1
713 #define TOG_SEARCH_NO_MORE 2
714 #define TOG_SEARCH_HAVE_NONE 3
720 static const struct got_error *open_diff_view(struct tog_view *,
721 struct got_object_id *, struct got_object_id *,
722 const char *, const char *, int, int, int, struct tog_view *,
723 struct got_repository *);
724 static const struct got_error *show_diff_view(struct tog_view *);
725 static const struct got_error *input_diff_view(struct tog_view **,
726 struct tog_view *, int);
727 static const struct got_error *reset_diff_view(struct tog_view *);
728 static const struct got_error* close_diff_view(struct tog_view *);
729 static const struct got_error *search_start_diff_view(struct tog_view *);
730 static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
731 size_t *, int **, int **, int **, int **);
732 static const struct got_error *search_next_view_match(struct tog_view *);
734 static const struct got_error *open_log_view(struct tog_view *,
735 struct got_object_id *, struct got_repository *,
736 const char *, const char *, int, struct got_worktree *);
737 static const struct got_error * show_log_view(struct tog_view *);
738 static const struct got_error *input_log_view(struct tog_view **,
739 struct tog_view *, int);
740 static const struct got_error *resize_log_view(struct tog_view *, int);
741 static const struct got_error *close_log_view(struct tog_view *);
742 static const struct got_error *search_start_log_view(struct tog_view *);
743 static const struct got_error *search_next_log_view(struct tog_view *);
745 static const struct got_error *open_blame_view(struct tog_view *, char *,
746 struct got_object_id *, struct got_repository *);
747 static const struct got_error *show_blame_view(struct tog_view *);
748 static const struct got_error *input_blame_view(struct tog_view **,
749 struct tog_view *, int);
750 static const struct got_error *reset_blame_view(struct tog_view *);
751 static const struct got_error *close_blame_view(struct tog_view *);
752 static const struct got_error *search_start_blame_view(struct tog_view *);
753 static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
754 size_t *, int **, int **, int **, int **);
756 static const struct got_error *open_tree_view(struct tog_view *,
757 struct got_object_id *, const char *, struct got_repository *);
758 static const struct got_error *show_tree_view(struct tog_view *);
759 static const struct got_error *input_tree_view(struct tog_view **,
760 struct tog_view *, int);
761 static const struct got_error *close_tree_view(struct tog_view *);
762 static const struct got_error *search_start_tree_view(struct tog_view *);
763 static const struct got_error *search_next_tree_view(struct tog_view *);
765 static const struct got_error *open_ref_view(struct tog_view *,
766 struct got_repository *);
767 static const struct got_error *show_ref_view(struct tog_view *);
768 static const struct got_error *input_ref_view(struct tog_view **,
769 struct tog_view *, int);
770 static const struct got_error *close_ref_view(struct tog_view *);
771 static const struct got_error *search_start_ref_view(struct tog_view *);
772 static const struct got_error *search_next_ref_view(struct tog_view *);
774 static const struct got_error *open_help_view(struct tog_view *,
776 static const struct got_error *show_help_view(struct tog_view *);
777 static const struct got_error *input_help_view(struct tog_view **,
778 struct tog_view *, int);
779 static const struct got_error *reset_help_view(struct tog_view *);
780 static const struct got_error* close_help_view(struct tog_view *);
781 static const struct got_error *search_start_help_view(struct tog_view *);
782 static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
783 size_t *, int **, int **, int **, int **);
785 static volatile sig_atomic_t tog_sigwinch_received;
786 static volatile sig_atomic_t tog_sigpipe_received;
787 static volatile sig_atomic_t tog_sigcont_received;
788 static volatile sig_atomic_t tog_sigint_received;
789 static volatile sig_atomic_t tog_sigterm_received;
792 tog_sigwinch(int signo)
794 tog_sigwinch_received = 1;
798 tog_sigpipe(int signo)
800 tog_sigpipe_received = 1;
804 tog_sigcont(int signo)
806 tog_sigcont_received = 1;
810 tog_sigint(int signo)
812 tog_sigint_received = 1;
816 tog_sigterm(int signo)
818 tog_sigterm_received = 1;
822 tog_fatal_signal_received(void)
824 return (tog_sigpipe_received ||
825 tog_sigint_received || tog_sigterm_received);
828 static const struct got_error *
829 view_close(struct tog_view *view)
831 const struct got_error *err = NULL, *child_err = NULL;
834 child_err = view_close(view->child);
838 err = view->close(view);
840 del_panel(view->panel);
842 delwin(view->window);
844 return err ? err : child_err;
847 static struct tog_view *
848 view_open(int nlines, int ncols, int begin_y, int begin_x,
849 enum tog_view_type type)
851 struct tog_view *view = calloc(1, sizeof(*view));
859 view->nlines = nlines ? nlines : LINES - begin_y;
860 view->ncols = ncols ? ncols : COLS - begin_x;
861 view->begin_y = begin_y;
862 view->begin_x = begin_x;
863 view->window = newwin(nlines, ncols, begin_y, begin_x);
864 if (view->window == NULL) {
868 view->panel = new_panel(view->window);
869 if (view->panel == NULL ||
870 set_panel_userptr(view->panel, view) != OK) {
875 keypad(view->window, TRUE);
880 view_split_begin_x(int begin_x)
882 if (begin_x > 0 || COLS < 120)
884 return (COLS - MAX(COLS / 2, 80));
887 /* XXX Stub till we decide what to do. */
889 view_split_begin_y(int lines)
891 return lines * HSPLIT_SCALE;
894 static const struct got_error *view_resize(struct tog_view *);
896 static const struct got_error *
897 view_splitscreen(struct tog_view *view)
899 const struct got_error *err = NULL;
901 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
902 if (view->resized_y && view->resized_y < view->lines)
903 view->begin_y = view->resized_y;
905 view->begin_y = view_split_begin_y(view->nlines);
907 } else if (!view->resized) {
908 if (view->resized_x && view->resized_x < view->cols - 1 &&
910 view->begin_x = view->resized_x;
912 view->begin_x = view_split_begin_x(0);
915 view->nlines = LINES - view->begin_y;
916 view->ncols = COLS - view->begin_x;
919 err = view_resize(view);
923 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
924 view->parent->nlines = view->begin_y;
926 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
927 return got_error_from_errno("mvwin");
932 static const struct got_error *
933 view_fullscreen(struct tog_view *view)
935 const struct got_error *err = NULL;
938 view->begin_y = view->resized ? view->begin_y : 0;
939 view->nlines = view->resized ? view->nlines : LINES;
943 err = view_resize(view);
947 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
948 return got_error_from_errno("mvwin");
954 view_is_parent_view(struct tog_view *view)
956 return view->parent == NULL;
960 view_is_splitscreen(struct tog_view *view)
962 return view->begin_x > 0 || view->begin_y > 0;
966 view_is_fullscreen(struct tog_view *view)
968 return view->nlines == LINES && view->ncols == COLS;
972 view_is_hsplit_top(struct tog_view *view)
974 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
975 view_is_splitscreen(view->child);
979 view_border(struct tog_view *view)
982 const struct tog_view *view_above;
985 return view_border(view->parent);
987 panel = panel_above(view->panel);
991 view_above = panel_userptr(panel);
992 if (view->mode == TOG_VIEW_SPLIT_HRZN)
993 mvwhline(view->window, view_above->begin_y - 1,
994 view->begin_x, ACS_HLINE, view->ncols);
996 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
997 ACS_VLINE, view->nlines);
1000 static const struct got_error *view_init_hsplit(struct tog_view *, int);
1001 static const struct got_error *request_log_commits(struct tog_view *);
1002 static const struct got_error *offset_selection_down(struct tog_view *);
1003 static void offset_selection_up(struct tog_view *);
1004 static void view_get_split(struct tog_view *, int *, int *);
1006 static const struct got_error *
1007 view_resize(struct tog_view *view)
1009 const struct got_error *err = NULL;
1010 int dif, nlines, ncols;
1012 dif = LINES - view->lines; /* line difference */
1014 if (view->lines > LINES)
1015 nlines = view->nlines - (view->lines - LINES);
1017 nlines = view->nlines + (LINES - view->lines);
1018 if (view->cols > COLS)
1019 ncols = view->ncols - (view->cols - COLS);
1021 ncols = view->ncols + (COLS - view->cols);
1024 int hs = view->child->begin_y;
1026 if (!view_is_fullscreen(view))
1027 view->child->begin_x = view_split_begin_x(view->begin_x);
1028 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1029 view->child->begin_x == 0) {
1032 view_fullscreen(view->child);
1033 if (view->child->focussed)
1034 show_panel(view->child->panel);
1036 show_panel(view->panel);
1038 ncols = view->child->begin_x;
1040 view_splitscreen(view->child);
1041 show_panel(view->child->panel);
1044 * XXX This is ugly and needs to be moved into the above
1045 * logic but "works" for now and my attempts at moving it
1046 * break either 'tab' or 'F' key maps in horizontal splits.
1049 err = view_splitscreen(view->child);
1052 if (dif < 0) { /* top split decreased */
1053 err = offset_selection_down(view);
1060 show_panel(view->child->panel);
1061 nlines = view->nlines;
1063 } else if (view->parent == NULL)
1066 if (view->resize && dif > 0) {
1067 err = view->resize(view, dif);
1072 if (wresize(view->window, nlines, ncols) == ERR)
1073 return got_error_from_errno("wresize");
1074 if (replace_panel(view->panel, view->window) == ERR)
1075 return got_error_from_errno("replace_panel");
1076 wclear(view->window);
1078 view->nlines = nlines;
1079 view->ncols = ncols;
1080 view->lines = LINES;
1086 static const struct got_error *
1087 resize_log_view(struct tog_view *view, int increase)
1089 struct tog_log_view_state *s = &view->state.log;
1090 const struct got_error *err = NULL;
1093 if (s->selected_entry)
1094 n = s->selected_entry->idx + view->lines - s->selected;
1097 * Request commits to account for the increased
1098 * height so we have enough to populate the view.
1100 if (s->commits->ncommits < n) {
1101 view->nscrolled = n - s->commits->ncommits + increase + 1;
1102 err = request_log_commits(view);
1109 view_adjust_offset(struct tog_view *view, int n)
1114 if (view->parent && view->parent->offset) {
1115 if (view->parent->offset + n >= 0)
1116 view->parent->offset += n;
1118 view->parent->offset = 0;
1119 } else if (view->offset) {
1120 if (view->offset - n >= 0)
1127 static const struct got_error *
1128 view_resize_split(struct tog_view *view, int resize)
1130 const struct got_error *err = NULL;
1131 struct tog_view *v = NULL;
1138 if (!v->child || !view_is_splitscreen(v->child))
1141 v->resized = v->child->resized = resize; /* lock for resize event */
1143 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1144 if (v->child->resized_y)
1145 v->child->begin_y = v->child->resized_y;
1147 v->child->begin_y -= resize;
1149 v->child->begin_y += resize;
1150 if (v->child->begin_y < 3) {
1152 v->child->begin_y = 3;
1153 } else if (v->child->begin_y > LINES - 1) {
1155 v->child->begin_y = LINES - 1;
1158 v->child->ncols = COLS;
1159 view_adjust_offset(view, resize);
1160 err = view_init_hsplit(v, v->child->begin_y);
1163 v->child->resized_y = v->child->begin_y;
1165 if (v->child->resized_x)
1166 v->child->begin_x = v->child->resized_x;
1168 v->child->begin_x -= resize;
1170 v->child->begin_x += resize;
1171 if (v->child->begin_x < 11) {
1173 v->child->begin_x = 11;
1174 } else if (v->child->begin_x > COLS - 1) {
1176 v->child->begin_x = COLS - 1;
1178 v->child->resized_x = v->child->begin_x;
1181 v->child->mode = v->mode;
1182 v->child->nlines = v->lines - v->child->begin_y;
1183 v->child->ncols = v->cols - v->child->begin_x;
1186 err = view_fullscreen(v);
1189 err = view_splitscreen(v->child);
1193 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1194 err = offset_selection_down(v->child);
1200 err = v->resize(v, 0);
1201 else if (v->child->resize)
1202 err = v->child->resize(v->child, 0);
1204 v->resized = v->child->resized = 0;
1210 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1212 struct tog_view *v = src->child ? src->child : src;
1214 dst->resized_x = v->resized_x;
1215 dst->resized_y = v->resized_y;
1218 static const struct got_error *
1219 view_close_child(struct tog_view *view)
1221 const struct got_error *err = NULL;
1223 if (view->child == NULL)
1226 err = view_close(view->child);
1231 static const struct got_error *
1232 view_set_child(struct tog_view *view, struct tog_view *child)
1234 const struct got_error *err = NULL;
1236 view->child = child;
1237 child->parent = view;
1239 err = view_resize(view);
1243 if (view->child->resized_x || view->child->resized_y)
1244 err = view_resize_split(view, 0);
1249 static const struct got_error *view_dispatch_request(struct tog_view **,
1250 struct tog_view *, enum tog_view_type, int, int);
1252 static const struct got_error *
1253 view_request_new(struct tog_view **requested, struct tog_view *view,
1254 enum tog_view_type request)
1256 struct tog_view *new_view = NULL;
1257 const struct got_error *err;
1262 if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1263 view_get_split(view, &y, &x);
1265 err = view_dispatch_request(&new_view, view, request, y, x);
1269 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1270 request != TOG_VIEW_HELP) {
1271 err = view_init_hsplit(view, y);
1277 new_view->focussed = 1;
1278 new_view->mode = view->mode;
1279 new_view->nlines = request == TOG_VIEW_HELP ?
1280 view->lines : view->lines - y;
1282 if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1283 view_transfer_size(new_view, view);
1284 err = view_close_child(view);
1287 err = view_set_child(view, new_view);
1290 view->focus_child = 1;
1292 *requested = new_view;
1298 tog_resizeterm(void)
1301 struct winsize size;
1303 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1304 cols = 80; /* Default */
1308 lines = size.ws_row;
1310 resize_term(lines, cols);
1313 static const struct got_error *
1314 view_search_start(struct tog_view *view, int fast_refresh)
1316 const struct got_error *err = NULL;
1317 struct tog_view *v = view;
1321 if (view->search_started) {
1322 regfree(&view->regex);
1323 view->searching = 0;
1324 memset(&view->regmatch, 0, sizeof(view->regmatch));
1326 view->search_started = 0;
1328 if (view->nlines < 1)
1331 if (view_is_hsplit_top(view))
1333 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1336 if (tog_io.input_str != NULL) {
1337 if (strlcpy(pattern, tog_io.input_str, sizeof(pattern)) >=
1339 return got_error(GOT_ERR_NO_SPACE);
1341 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1342 wclrtoeol(v->window);
1343 nodelay(v->window, FALSE); /* block for search term input */
1346 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1347 wrefresh(v->window);
1350 nodelay(v->window, TRUE);
1351 if (!fast_refresh && !using_mock_io)
1357 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1358 err = view->search_start(view);
1360 regfree(&view->regex);
1363 view->search_started = 1;
1364 view->searching = TOG_SEARCH_FORWARD;
1365 view->search_next_done = 0;
1366 view->search_next(view);
1372 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1373 static const struct got_error *
1374 switch_split(struct tog_view *view)
1376 const struct got_error *err = NULL;
1377 struct tog_view *v = NULL;
1384 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1385 v->mode = TOG_VIEW_SPLIT_VERT;
1387 v->mode = TOG_VIEW_SPLIT_HRZN;
1391 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1392 v->mode = TOG_VIEW_SPLIT_NONE;
1394 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1395 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1396 v->child->begin_y = v->child->resized_y;
1397 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1398 v->child->begin_x = v->child->resized_x;
1401 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1403 v->child->ncols = COLS;
1404 v->child->nscrolled = LINES - v->child->nlines;
1406 err = view_init_hsplit(v, v->child->begin_y);
1410 v->child->mode = v->mode;
1411 v->child->nlines = v->lines - v->child->begin_y;
1414 err = view_fullscreen(v);
1417 err = view_splitscreen(v->child);
1421 if (v->mode == TOG_VIEW_SPLIT_NONE)
1422 v->mode = TOG_VIEW_SPLIT_VERT;
1423 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1424 err = offset_selection_down(v);
1427 err = offset_selection_down(v->child);
1431 offset_selection_up(v);
1432 offset_selection_up(v->child);
1435 err = v->resize(v, 0);
1436 else if (v->child->resize)
1437 err = v->child->resize(v->child, 0);
1443 * Strip trailing whitespace from str starting at byte *n;
1444 * if *n < 0, use strlen(str). Return new str length in *n.
1447 strip_trailing_ws(char *str, int *n)
1451 if (str == NULL || *str == '\0')
1457 while (x-- > 0 && isspace((unsigned char)str[x]))
1464 * Extract visible substring of line y from the curses screen
1465 * and strip trailing whitespace. If vline is set, overwrite
1466 * line[vline] with '|' because the ACS_VLINE character is
1467 * written out as 'x'. Write the line to file f.
1469 static const struct got_error *
1470 view_write_line(FILE *f, int y, int vline)
1472 char line[COLS * MB_LEN_MAX]; /* allow for multibyte chars */
1475 r = mvwinnstr(curscr, y, 0, line, sizeof(line));
1477 return got_error_fmt(GOT_ERR_RANGE,
1478 "failed to extract line %d", y);
1481 * In some views, lines are padded with blanks to COLS width.
1482 * Strip them so we can diff without the -b flag when testing.
1484 strip_trailing_ws(line, &r);
1489 w = fprintf(f, "%s\n", line);
1490 if (w != r + 1) /* \n */
1491 return got_ferror(f, GOT_ERR_IO);
1497 * Capture the visible curses screen by writing each line to the
1498 * file at the path set via the TOG_SCR_DUMP environment variable.
1500 static const struct got_error *
1501 screendump(struct tog_view *view)
1503 const struct got_error *err;
1506 err = got_opentemp_truncate(tog_io.sdump);
1510 if ((view->child && view->child->begin_x) ||
1511 (view->parent && view->begin_x)) {
1512 int ncols = view->child ? view->ncols : view->parent->ncols;
1514 /* vertical splitscreen */
1515 for (i = 0; i < view->nlines; ++i) {
1516 err = view_write_line(tog_io.sdump, i, ncols - 1);
1523 /* fullscreen or horizontal splitscreen */
1524 if ((view->child && view->child->begin_y) ||
1525 (view->parent && view->begin_y)) /* hsplit */
1526 hline = view->child ?
1527 view->child->begin_y : view->begin_y;
1529 for (i = 0; i < view->lines; i++) {
1530 if (hline && i == hline - 1) {
1533 /* ACS_HLINE writes out as 'q', overwrite it */
1534 for (c = 0; c < view->cols; ++c)
1535 fputc('-', tog_io.sdump);
1536 fputc('\n', tog_io.sdump);
1540 err = view_write_line(tog_io.sdump, i, 0);
1551 * Compute view->count from numeric input. Assign total to view->count and
1552 * return first non-numeric key entered.
1555 get_compound_key(struct tog_view *view, int c)
1557 struct tog_view *v = view;
1560 if (view_is_hsplit_top(view))
1562 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1566 cbreak(); /* block for input */
1567 nodelay(view->window, FALSE);
1568 wmove(v->window, v->nlines - 1, 0);
1569 wclrtoeol(v->window);
1570 waddch(v->window, ':');
1573 x = getcurx(v->window);
1574 if (x != ERR && x < view->ncols) {
1575 waddch(v->window, c);
1576 wrefresh(v->window);
1580 * Don't overflow. Max valid request should be the greatest
1581 * between the longest and total lines; cap at 10 million.
1586 n = n * 10 + (c - '0');
1587 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1589 if (c == 'G' || c == 'g') { /* nG key map */
1590 view->gline = view->hiline = n;
1595 /* Massage excessive or inapplicable values at the input handler. */
1602 action_report(struct tog_view *view)
1604 struct tog_view *v = view;
1606 if (view_is_hsplit_top(view))
1608 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1611 wmove(v->window, v->nlines - 1, 0);
1612 wclrtoeol(v->window);
1613 wprintw(v->window, ":%s", view->action);
1614 wrefresh(v->window);
1617 * Clear action status report. Only clear in blame view
1618 * once annotating is complete, otherwise it's too fast.
1620 if (view->type == TOG_VIEW_BLAME) {
1621 if (view->state.blame.blame_complete)
1622 view->action = NULL;
1624 view->action = NULL;
1628 * Read the next line from the test script and assign
1629 * key instruction to *ch. If at EOF, set the *done flag.
1631 static const struct got_error *
1632 tog_read_script_key(FILE *script, struct tog_view *view, int *ch, int *done)
1634 const struct got_error *err = NULL;
1640 if (view->count && --view->count) {
1646 if ((n = getline(&line, &linesz, script)) == -1) {
1651 err = got_ferror(script, GOT_ERR_IO);
1656 if (strncasecmp(line, "WAIT_FOR_UI", 11) == 0)
1657 tog_io.wait_for_ui = 1;
1658 else if (strncasecmp(line, "KEY_ENTER", 9) == 0)
1660 else if (strncasecmp(line, "KEY_RIGHT", 9) == 0)
1662 else if (strncasecmp(line, "KEY_LEFT", 8) == 0)
1664 else if (strncasecmp(line, "KEY_DOWN", 8) == 0)
1666 else if (strncasecmp(line, "KEY_UP", 6) == 0)
1668 else if (strncasecmp(line, "TAB", 3) == 0)
1670 else if (strncasecmp(line, "SCREENDUMP", 10) == 0)
1671 *ch = TOG_KEY_SCRDUMP;
1672 else if (isdigit((unsigned char)*line)) {
1675 while (isdigit((unsigned char)*t))
1677 view->ch = *ch = *t;
1679 /* ignore error, view->count is 0 if instruction is invalid */
1680 view->count = strtonum(line, 0, INT_MAX, NULL);
1683 if (n > 2 && (*ch == '/' || *ch == '&')) {
1684 /* skip leading keymap and trim trailing newline */
1685 tog_io.input_str = strndup(line + 1, n - 2);
1686 if (tog_io.input_str == NULL) {
1687 err = got_error_from_errno("strndup");
1698 static const struct got_error *
1699 view_input(struct tog_view **new, int *done, struct tog_view *view,
1700 struct tog_view_list_head *views, int fast_refresh)
1702 const struct got_error *err = NULL;
1709 action_report(view);
1711 /* Clear "no matches" indicator. */
1712 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1713 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1714 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1718 if (view->searching && !view->search_next_done) {
1719 view->search_next(view);
1723 /* Allow threads to make progress while we are waiting for input. */
1724 errcode = pthread_mutex_unlock(&tog_mutex);
1726 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1728 if (using_mock_io) {
1729 err = tog_read_script_key(tog_io.f, view, &ch, done);
1731 errcode = pthread_mutex_lock(&tog_mutex);
1734 } else if (view->count && --view->count) {
1736 nodelay(view->window, TRUE);
1737 ch = wgetch(view->window);
1738 /* let C-g or backspace abort unfinished count */
1739 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1744 ch = wgetch(view->window);
1745 if (ch >= '1' && ch <= '9')
1746 view->ch = ch = get_compound_key(view, ch);
1748 if (view->hiline && ch != ERR && ch != 0)
1749 view->hiline = 0; /* key pressed, clear line highlight */
1750 nodelay(view->window, TRUE);
1751 errcode = pthread_mutex_lock(&tog_mutex);
1753 return got_error_set_errno(errcode, "pthread_mutex_lock");
1755 if (tog_sigwinch_received || tog_sigcont_received) {
1757 tog_sigwinch_received = 0;
1758 tog_sigcont_received = 0;
1759 TAILQ_FOREACH(v, views, entry) {
1760 err = view_resize(v);
1763 err = v->input(new, v, KEY_RESIZE);
1767 err = view_resize(v->child);
1770 err = v->child->input(new, v->child,
1774 if (v->child->resized_x || v->child->resized_y) {
1775 err = view_resize_split(v, 0);
1787 if (view->type == TOG_VIEW_HELP)
1788 err = view->reset(view);
1790 err = view_request_new(new, view, TOG_VIEW_HELP);
1796 view->child->focussed = 1;
1797 view->focus_child = 1;
1798 } else if (view->parent) {
1800 view->parent->focussed = 1;
1801 view->parent->focus_child = 0;
1802 if (!view_is_splitscreen(view)) {
1803 if (view->parent->resize) {
1804 err = view->parent->resize(view->parent,
1809 offset_selection_up(view->parent);
1810 err = view_fullscreen(view->parent);
1817 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1818 if (view->parent->resize) {
1819 /* might need more commits to fill fullscreen */
1820 err = view->parent->resize(view->parent, 0);
1824 offset_selection_up(view->parent);
1826 err = view->input(new, view, ch);
1834 if (view_is_parent_view(view)) {
1835 if (view->child == NULL)
1837 if (view_is_splitscreen(view->child)) {
1839 view->child->focussed = 1;
1840 err = view_fullscreen(view->child);
1842 err = view_splitscreen(view->child);
1844 err = view_resize_split(view, 0);
1848 err = view->child->input(new, view->child,
1851 if (view_is_splitscreen(view)) {
1852 view->parent->focussed = 0;
1854 err = view_fullscreen(view);
1856 err = view_splitscreen(view);
1857 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1858 err = view_resize(view->parent);
1860 err = view_resize_split(view, 0);
1864 err = view->input(new, view, KEY_RESIZE);
1869 err = view->resize(view, 0);
1874 if (view->parent->resize) {
1875 err = view->parent->resize(view->parent, 0);
1879 err = offset_selection_down(view->parent);
1883 err = offset_selection_down(view);
1887 err = switch_split(view);
1890 err = view_resize_split(view, -1);
1893 err = view_resize_split(view, 1);
1899 if (view->search_start)
1900 view_search_start(view, fast_refresh);
1902 err = view->input(new, view, ch);
1906 if (view->search_started && view->search_next) {
1907 view->searching = (ch == 'n' ?
1908 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1909 view->search_next_done = 0;
1910 view->search_next(view);
1912 err = view->input(new, view, ch);
1915 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1916 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1917 view->action = "Patience diff algorithm";
1919 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1920 view->action = "Myers diff algorithm";
1922 TAILQ_FOREACH(v, views, entry) {
1928 if (v->child && v->child->reset) {
1929 err = v->child->reset(v->child);
1935 case TOG_KEY_SCRDUMP:
1936 err = screendump(view);
1939 err = view->input(new, view, ch);
1947 view_needs_focus_indication(struct tog_view *view)
1949 if (view_is_parent_view(view)) {
1950 if (view->child == NULL || view->child->focussed)
1952 if (!view_is_splitscreen(view->child))
1954 } else if (!view_is_splitscreen(view))
1957 return view->focussed;
1960 static const struct got_error *
1963 const struct got_error *err = NULL;
1965 if (tog_io.cin && fclose(tog_io.cin) == EOF)
1966 err = got_ferror(tog_io.cin, GOT_ERR_IO);
1967 if (tog_io.cout && fclose(tog_io.cout) == EOF && err == NULL)
1968 err = got_ferror(tog_io.cout, GOT_ERR_IO);
1969 if (tog_io.f && fclose(tog_io.f) == EOF && err == NULL)
1970 err = got_ferror(tog_io.f, GOT_ERR_IO);
1971 if (tog_io.sdump && fclose(tog_io.sdump) == EOF && err == NULL)
1972 err = got_ferror(tog_io.sdump, GOT_ERR_IO);
1973 if (tog_io.input_str != NULL)
1974 free(tog_io.input_str);
1979 static const struct got_error *
1980 view_loop(struct tog_view *view)
1982 const struct got_error *err = NULL;
1983 struct tog_view_list_head views;
1984 struct tog_view *new_view;
1986 int fast_refresh = 10;
1987 int done = 0, errcode;
1989 mode = getenv("TOG_VIEW_SPLIT_MODE");
1990 if (!mode || !(*mode == 'h' || *mode == 'H'))
1991 view->mode = TOG_VIEW_SPLIT_VERT;
1993 view->mode = TOG_VIEW_SPLIT_HRZN;
1995 errcode = pthread_mutex_lock(&tog_mutex);
1997 return got_error_set_errno(errcode, "pthread_mutex_lock");
2000 TAILQ_INSERT_HEAD(&views, view, entry);
2003 err = view->show(view);
2008 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
2009 !tog_fatal_signal_received()) {
2010 /* Refresh fast during initialization, then become slower. */
2011 if (fast_refresh && --fast_refresh == 0 && !using_mock_io)
2012 halfdelay(10); /* switch to once per second */
2014 err = view_input(&new_view, &done, view, &views, fast_refresh);
2018 if (view->dying && view == TAILQ_FIRST(&views) &&
2019 TAILQ_NEXT(view, entry) == NULL)
2025 * When we quit, scroll the screen up a single line
2026 * so we don't lose any information.
2028 TAILQ_FOREACH(v, &views, entry) {
2029 wmove(v->window, 0, 0);
2030 wdeleteln(v->window);
2031 wnoutrefresh(v->window);
2032 if (v->child && !view_is_fullscreen(v)) {
2033 wmove(v->child->window, 0, 0);
2034 wdeleteln(v->child->window);
2035 wnoutrefresh(v->child->window);
2042 struct tog_view *v, *prev = NULL;
2044 if (view_is_parent_view(view))
2045 prev = TAILQ_PREV(view, tog_view_list_head,
2047 else if (view->parent)
2048 prev = view->parent;
2051 view->parent->child = NULL;
2052 view->parent->focus_child = 0;
2053 /* Restore fullscreen line height. */
2054 view->parent->nlines = view->parent->lines;
2055 err = view_resize(view->parent);
2058 /* Make resized splits persist. */
2059 view_transfer_size(view->parent, view);
2061 TAILQ_REMOVE(&views, view, entry);
2063 err = view_close(view);
2068 TAILQ_FOREACH(v, &views, entry) {
2072 if (view == NULL && new_view == NULL) {
2073 /* No view has focus. Try to pick one. */
2076 else if (!TAILQ_EMPTY(&views)) {
2077 view = TAILQ_LAST(&views,
2078 tog_view_list_head);
2081 if (view->focus_child) {
2082 view->child->focussed = 1;
2090 struct tog_view *v, *t;
2091 /* Only allow one parent view per type. */
2092 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
2093 if (v->type != new_view->type)
2095 TAILQ_REMOVE(&views, v, entry);
2096 err = view_close(v);
2101 TAILQ_INSERT_TAIL(&views, new_view, entry);
2104 if (view && !done) {
2105 if (view_is_parent_view(view)) {
2106 if (view->child && view->child->focussed)
2109 if (view->parent && view->parent->focussed)
2110 view = view->parent;
2112 show_panel(view->panel);
2113 if (view->child && view_is_splitscreen(view->child))
2114 show_panel(view->child->panel);
2115 if (view->parent && view_is_splitscreen(view)) {
2116 err = view->parent->show(view->parent);
2120 err = view->show(view);
2124 err = view->child->show(view->child);
2133 while (!TAILQ_EMPTY(&views)) {
2134 const struct got_error *close_err;
2135 view = TAILQ_FIRST(&views);
2136 TAILQ_REMOVE(&views, view, entry);
2137 close_err = view_close(view);
2138 if (close_err && err == NULL)
2142 errcode = pthread_mutex_unlock(&tog_mutex);
2143 if (errcode && err == NULL)
2144 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2154 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
2159 /* Create newly allocated wide-character string equivalent to a byte string. */
2160 static const struct got_error *
2161 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
2164 const struct got_error *err = NULL;
2167 *wlen = mbstowcs(NULL, s, 0);
2168 if (*wlen == (size_t)-1) {
2170 if (errno != EILSEQ)
2171 return got_error_from_errno("mbstowcs");
2173 /* byte string invalid in current encoding; try to "fix" it */
2174 err = got_mbsavis(&vis, &vislen, s);
2177 *wlen = mbstowcs(NULL, vis, 0);
2178 if (*wlen == (size_t)-1) {
2179 err = got_error_from_errno("mbstowcs"); /* give up */
2184 *ws = calloc(*wlen + 1, sizeof(**ws));
2186 err = got_error_from_errno("calloc");
2190 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
2191 err = got_error_from_errno("mbstowcs");
2202 static const struct got_error *
2203 expand_tab(char **ptr, const char *src)
2206 size_t len, n, idx = 0, sz = 0;
2209 n = len = strlen(src);
2210 dst = malloc(n + 1);
2212 return got_error_from_errno("malloc");
2214 while (idx < len && src[idx]) {
2215 const char c = src[idx];
2218 size_t nb = TABSIZE - sz % TABSIZE;
2221 p = realloc(dst, n + nb);
2224 return got_error_from_errno("realloc");
2229 memset(dst + sz, ' ', nb);
2232 dst[sz++] = src[idx];
2242 * Advance at most n columns from wline starting at offset off.
2243 * Return the index to the first character after the span operation.
2244 * Return the combined column width of all spanned wide characters in
2248 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2250 int width, i, cols = 0;
2257 for (i = off; wline[i] != L'\0'; ++i) {
2258 if (wline[i] == L'\t')
2259 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2261 width = wcwidth(wline[i]);
2268 if (cols + width > n)
2278 * Format a line for display, ensuring that it won't overflow a width limit.
2279 * With scrolling, the width returned refers to the scrolled version of the
2280 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2282 static const struct got_error *
2283 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2284 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2286 const struct got_error *err = NULL;
2288 wchar_t *wline = NULL;
2297 err = expand_tab(&exstr, line);
2302 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2307 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2308 wline[wlen - 1] = L'\0';
2311 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2312 wline[wlen - 1] = L'\0';
2316 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2318 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2324 *scrollxp = scrollx;
2332 static const struct got_error*
2333 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2334 struct got_object_id *id, struct got_repository *repo)
2336 static const struct got_error *err = NULL;
2337 struct got_reflist_entry *re;
2346 TAILQ_FOREACH(re, refs, entry) {
2347 struct got_tag_object *tag = NULL;
2348 struct got_object_id *ref_id;
2351 name = got_ref_get_name(re->ref);
2352 if (strcmp(name, GOT_REF_HEAD) == 0)
2354 if (strncmp(name, "refs/", 5) == 0)
2356 if (strncmp(name, "got/", 4) == 0)
2358 if (strncmp(name, "heads/", 6) == 0)
2360 if (strncmp(name, "remotes/", 8) == 0) {
2362 s = strstr(name, "/" GOT_REF_HEAD);
2363 if (s != NULL && strcmp(s, "/" GOT_REF_HEAD) == 0)
2366 err = got_ref_resolve(&ref_id, repo, re->ref);
2369 if (strncmp(name, "tags/", 5) == 0) {
2370 err = got_object_open_as_tag(&tag, repo, ref_id);
2372 if (err->code != GOT_ERR_OBJ_TYPE) {
2376 /* Ref points at something other than a tag. */
2381 cmp = got_object_id_cmp(tag ?
2382 got_object_tag_get_object_id(tag) : ref_id, id);
2385 got_object_tag_close(tag);
2389 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2390 s ? ", " : "", name) == -1) {
2391 err = got_error_from_errno("asprintf");
2402 static const struct got_error *
2403 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2408 smallerthan = strchr(author, '<');
2409 if (smallerthan && smallerthan[1] != '\0')
2410 author = smallerthan + 1;
2411 author[strcspn(author, "@>")] = '\0';
2412 return format_line(wauthor, author_width, NULL, author, 0, limit,
2416 static const struct got_error *
2417 draw_commit(struct tog_view *view, struct commit_queue_entry *entry,
2418 const size_t date_display_cols, int author_display_cols)
2420 struct tog_log_view_state *s = &view->state.log;
2421 const struct got_error *err = NULL;
2422 struct got_commit_object *commit = entry->commit;
2423 struct got_object_id *id = entry->id;
2424 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2425 char *refs_str = NULL;
2426 char *logmsg0 = NULL, *logmsg = NULL;
2427 char *author = NULL;
2428 wchar_t *wrefstr = NULL, *wlogmsg = NULL, *wauthor = NULL;
2429 int author_width, refstr_width, logmsg_width;
2430 char *newline, *line = NULL;
2431 int col, limit, scrollx, logmsg_x;
2432 const int avail = view->ncols, marker_column = author_display_cols + 1;
2434 time_t committer_time;
2435 struct tog_color *tc;
2436 struct got_reflist_head *refs;
2438 if (tog_base_commit.id != NULL && tog_base_commit.idx == -1 &&
2439 got_object_id_cmp(id, tog_base_commit.id) == 0)
2440 tog_base_commit.idx = entry->idx;
2441 if (tog_io.wait_for_ui && s->thread_args.need_commit_marker) {
2444 rc = pthread_cond_wait(&s->thread_args.log_loaded, &tog_mutex);
2446 return got_error_set_errno(rc, "pthread_cond_wait");
2449 committer_time = got_object_commit_get_committer_time(commit);
2450 if (gmtime_r(&committer_time, &tm) == NULL)
2451 return got_error_from_errno("gmtime_r");
2452 if (strftime(datebuf, sizeof(datebuf), "%F ", &tm) == 0)
2453 return got_error(GOT_ERR_NO_SPACE);
2455 if (avail <= date_display_cols)
2456 limit = MIN(sizeof(datebuf) - 1, avail);
2458 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2459 tc = get_color(&s->colors, TOG_COLOR_DATE);
2461 wattr_on(view->window,
2462 COLOR_PAIR(tc->colorpair), NULL);
2463 waddnstr(view->window, datebuf, limit);
2465 wattr_off(view->window,
2466 COLOR_PAIR(tc->colorpair), NULL);
2473 err = got_object_id_str(&id_str, id);
2476 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2478 wattr_on(view->window,
2479 COLOR_PAIR(tc->colorpair), NULL);
2480 wprintw(view->window, "%.8s ", id_str);
2482 wattr_off(view->window,
2483 COLOR_PAIR(tc->colorpair), NULL);
2490 if (s->use_committer)
2491 author = strdup(got_object_commit_get_committer(commit));
2493 author = strdup(got_object_commit_get_author(commit));
2494 if (author == NULL) {
2495 err = got_error_from_errno("strdup");
2498 err = format_author(&wauthor, &author_width, author, avail - col, col);
2501 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2503 wattr_on(view->window,
2504 COLOR_PAIR(tc->colorpair), NULL);
2505 waddwstr(view->window, wauthor);
2506 col += author_width;
2507 while (col < avail && author_width < author_display_cols + 2) {
2508 if (tog_base_commit.marker != GOT_WORKTREE_STATE_UNKNOWN &&
2509 author_width == marker_column &&
2510 entry->idx == tog_base_commit.idx && !s->limit_view) {
2511 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2513 wattr_on(view->window,
2514 COLOR_PAIR(tc->colorpair), NULL);
2515 waddch(view->window, tog_base_commit.marker);
2517 wattr_off(view->window,
2518 COLOR_PAIR(tc->colorpair), NULL);
2520 waddch(view->window, ' ');
2525 wattr_off(view->window,
2526 COLOR_PAIR(tc->colorpair), NULL);
2530 err = got_object_commit_get_logmsg(&logmsg0, commit);
2534 while (*logmsg == '\n')
2536 newline = strchr(logmsg, '\n');
2540 limit = avail - col;
2541 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2542 limit--; /* for the border */
2544 /* Prepend reference labels to log message if possible .*/
2545 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, id);
2546 err = build_refs_str(&refs_str, refs, id, s->repo);
2552 if (asprintf(&rs, "[%s]", refs_str) == -1) {
2553 err = got_error_from_errno("asprintf");
2556 err = format_line(&wrefstr, &refstr_width,
2557 &scrollx, rs, view->x, limit, col, 1);
2561 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2563 wattr_on(view->window,
2564 COLOR_PAIR(tc->colorpair), NULL);
2565 waddwstr(view->window, &wrefstr[scrollx]);
2567 wattr_off(view->window,
2568 COLOR_PAIR(tc->colorpair), NULL);
2569 col += MAX(refstr_width, 0);
2574 waddch(view->window, ' ');
2578 if (refstr_width > 0)
2581 int unscrolled_refstr_width;
2582 size_t len = wcslen(wrefstr);
2585 * No need to check for -1 return value here since
2586 * unprintables have been replaced by span_wline().
2588 unscrolled_refstr_width = wcswidth(wrefstr, len);
2589 unscrolled_refstr_width += 1; /* trailing space */
2590 logmsg_x = view->x - unscrolled_refstr_width;
2593 limit = avail - col;
2594 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2595 limit--; /* for the border */
2599 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, logmsg_x,
2603 waddwstr(view->window, &wlogmsg[scrollx]);
2604 col += MAX(logmsg_width, 0);
2605 while (col < avail) {
2606 waddch(view->window, ' ');
2620 static struct commit_queue_entry *
2621 alloc_commit_queue_entry(struct got_commit_object *commit,
2622 struct got_object_id *id)
2624 struct commit_queue_entry *entry;
2625 struct got_object_id *dup;
2627 entry = calloc(1, sizeof(*entry));
2631 dup = got_object_id_dup(id);
2638 entry->commit = commit;
2643 pop_commit(struct commit_queue *commits)
2645 struct commit_queue_entry *entry;
2647 entry = TAILQ_FIRST(&commits->head);
2648 TAILQ_REMOVE(&commits->head, entry, entry);
2649 got_object_commit_close(entry->commit);
2650 commits->ncommits--;
2656 free_commits(struct commit_queue *commits)
2658 while (!TAILQ_EMPTY(&commits->head))
2659 pop_commit(commits);
2662 static const struct got_error *
2663 match_commit(int *have_match, struct got_object_id *id,
2664 struct got_commit_object *commit, regex_t *regex)
2666 const struct got_error *err = NULL;
2667 regmatch_t regmatch;
2668 char *id_str = NULL, *logmsg = NULL;
2672 err = got_object_id_str(&id_str, id);
2676 err = got_object_commit_get_logmsg(&logmsg, commit);
2680 if (regexec(regex, got_object_commit_get_author(commit), 1,
2681 ®match, 0) == 0 ||
2682 regexec(regex, got_object_commit_get_committer(commit), 1,
2683 ®match, 0) == 0 ||
2684 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2685 regexec(regex, logmsg, 1, ®match, 0) == 0)
2693 static const struct got_error *
2694 queue_commits(struct tog_log_thread_args *a)
2696 const struct got_error *err = NULL;
2699 * We keep all commits open throughout the lifetime of the log
2700 * view in order to avoid having to re-fetch commits from disk
2701 * while updating the display.
2704 struct got_object_id id;
2705 struct got_commit_object *commit;
2706 struct commit_queue_entry *entry;
2707 int limit_match = 0;
2710 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2715 err = got_object_open_as_commit(&commit, a->repo, &id);
2718 entry = alloc_commit_queue_entry(commit, &id);
2719 if (entry == NULL) {
2720 err = got_error_from_errno("alloc_commit_queue_entry");
2724 errcode = pthread_mutex_lock(&tog_mutex);
2726 err = got_error_set_errno(errcode,
2727 "pthread_mutex_lock");
2731 entry->idx = a->real_commits->ncommits;
2732 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2733 a->real_commits->ncommits++;
2736 err = match_commit(&limit_match, &id, commit,
2742 struct commit_queue_entry *matched;
2744 matched = alloc_commit_queue_entry(
2745 entry->commit, entry->id);
2746 if (matched == NULL) {
2747 err = got_error_from_errno(
2748 "alloc_commit_queue_entry");
2751 matched->commit = entry->commit;
2752 got_object_commit_retain(entry->commit);
2754 matched->idx = a->limit_commits->ncommits;
2755 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2757 a->limit_commits->ncommits++;
2761 * This is how we signal log_thread() that we
2762 * have found a match, and that it should be
2763 * counted as a new entry for the view.
2765 a->limit_match = limit_match;
2768 if (*a->searching == TOG_SEARCH_FORWARD &&
2769 !*a->search_next_done) {
2771 err = match_commit(&have_match, &id, commit, a->regex);
2776 if (limit_match && have_match)
2777 *a->search_next_done =
2778 TOG_SEARCH_HAVE_MORE;
2779 } else if (have_match)
2780 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2783 errcode = pthread_mutex_unlock(&tog_mutex);
2784 if (errcode && err == NULL)
2785 err = got_error_set_errno(errcode,
2786 "pthread_mutex_unlock");
2789 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2795 select_commit(struct tog_log_view_state *s)
2797 struct commit_queue_entry *entry;
2800 entry = s->first_displayed_entry;
2802 if (ncommits == s->selected) {
2803 s->selected_entry = entry;
2806 entry = TAILQ_NEXT(entry, entry);
2811 static const struct got_error *
2812 draw_commits(struct tog_view *view)
2814 const struct got_error *err = NULL;
2815 struct tog_log_view_state *s = &view->state.log;
2816 struct commit_queue_entry *entry = s->selected_entry;
2817 int limit = view->nlines;
2819 int ncommits, author_cols = 4, refstr_cols;
2820 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2821 char *refs_str = NULL;
2823 struct tog_color *tc;
2824 static const size_t date_display_cols = 12;
2825 struct got_reflist_head *refs;
2827 if (view_is_hsplit_top(view))
2828 --limit; /* account for border */
2830 if (s->selected_entry &&
2831 !(view->searching && view->search_next_done == 0)) {
2832 err = got_object_id_str(&id_str, s->selected_entry->id);
2835 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2836 s->selected_entry->id);
2837 err = build_refs_str(&refs_str, refs, s->selected_entry->id,
2843 if (s->thread_args.commits_needed == 0 && !using_mock_io)
2844 halfdelay(10); /* disable fast refresh */
2846 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2847 if (asprintf(&ncommits_str, " [%d/%d] %s",
2848 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2849 (view->searching && !view->search_next_done) ?
2850 "searching..." : "loading...") == -1) {
2851 err = got_error_from_errno("asprintf");
2855 const char *search_str = NULL;
2856 const char *limit_str = NULL;
2858 if (view->searching) {
2859 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2860 search_str = "no more matches";
2861 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2862 search_str = "no matches found";
2863 else if (!view->search_next_done)
2864 search_str = "searching...";
2867 if (s->limit_view && s->commits->ncommits == 0)
2868 limit_str = "no matches found";
2870 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2871 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2872 search_str ? search_str : (refs_str ? refs_str : ""),
2873 limit_str ? limit_str : "") == -1) {
2874 err = got_error_from_errno("asprintf");
2882 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2883 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2884 "........................................",
2885 s->in_repo_path, ncommits_str) == -1) {
2886 err = got_error_from_errno("asprintf");
2890 } else if (asprintf(&header, "commit %s%s",
2891 id_str ? id_str : "........................................",
2892 ncommits_str) == -1) {
2893 err = got_error_from_errno("asprintf");
2897 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2901 werase(view->window);
2903 if (view_needs_focus_indication(view))
2904 wstandout(view->window);
2905 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2907 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2908 waddwstr(view->window, wline);
2909 while (width < view->ncols) {
2910 waddch(view->window, ' ');
2914 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2915 if (view_needs_focus_indication(view))
2916 wstandend(view->window);
2921 /* Grow author column size if necessary, and set view->maxx. */
2922 entry = s->first_displayed_entry;
2926 struct got_commit_object *c = entry->commit;
2927 char *author, *eol, *msg, *msg0;
2928 wchar_t *wauthor, *wmsg;
2930 if (ncommits >= limit - 1)
2932 if (s->use_committer)
2933 author = strdup(got_object_commit_get_committer(c));
2935 author = strdup(got_object_commit_get_author(c));
2936 if (author == NULL) {
2937 err = got_error_from_errno("strdup");
2940 err = format_author(&wauthor, &width, author, COLS,
2942 if (author_cols < width)
2943 author_cols = width;
2948 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2950 err = build_refs_str(&refs_str, refs, entry->id, s->repo);
2955 err = format_line(&ws, &width, NULL, refs_str,
2956 0, INT_MAX, date_display_cols + author_cols, 0);
2962 refstr_cols = width + 3; /* account for [ ] + space */
2965 err = got_object_commit_get_logmsg(&msg0, c);
2969 while (*msg == '\n')
2971 if ((eol = strchr(msg, '\n')))
2973 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2974 date_display_cols + author_cols + refstr_cols, 0);
2977 view->maxx = MAX(view->maxx, width + refstr_cols);
2981 entry = TAILQ_NEXT(entry, entry);
2984 entry = s->first_displayed_entry;
2985 s->last_displayed_entry = s->first_displayed_entry;
2988 if (ncommits >= limit - 1)
2990 if (ncommits == s->selected)
2991 wstandout(view->window);
2992 err = draw_commit(view, entry, date_display_cols, author_cols);
2993 if (ncommits == s->selected)
2994 wstandend(view->window);
2998 s->last_displayed_entry = entry;
2999 entry = TAILQ_NEXT(entry, entry);
3012 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
3014 struct commit_queue_entry *entry;
3017 entry = TAILQ_FIRST(&s->commits->head);
3018 if (s->first_displayed_entry == entry)
3021 entry = s->first_displayed_entry;
3022 while (entry && nscrolled < maxscroll) {
3023 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3025 s->first_displayed_entry = entry;
3031 static const struct got_error *
3032 trigger_log_thread(struct tog_view *view, int wait)
3034 struct tog_log_thread_args *ta = &view->state.log.thread_args;
3038 halfdelay(1); /* fast refresh while loading commits */
3040 while (!ta->log_complete && !tog_thread_error &&
3041 (ta->commits_needed > 0 || ta->load_all)) {
3042 /* Wake the log thread. */
3043 errcode = pthread_cond_signal(&ta->need_commits);
3045 return got_error_set_errno(errcode,
3046 "pthread_cond_signal");
3049 * The mutex will be released while the view loop waits
3050 * in wgetch(), at which time the log thread will run.
3055 /* Display progress update in log view. */
3056 show_log_view(view);
3060 /* Wait right here while next commit is being loaded. */
3061 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
3063 return got_error_set_errno(errcode,
3064 "pthread_cond_wait");
3066 /* Display progress update in log view. */
3067 show_log_view(view);
3075 static const struct got_error *
3076 request_log_commits(struct tog_view *view)
3078 struct tog_log_view_state *state = &view->state.log;
3079 const struct got_error *err = NULL;
3081 if (state->thread_args.log_complete)
3084 state->thread_args.commits_needed += view->nscrolled;
3085 err = trigger_log_thread(view, 1);
3086 view->nscrolled = 0;
3091 static const struct got_error *
3092 log_scroll_down(struct tog_view *view, int maxscroll)
3094 struct tog_log_view_state *s = &view->state.log;
3095 const struct got_error *err = NULL;
3096 struct commit_queue_entry *pentry;
3097 int nscrolled = 0, ncommits_needed;
3099 if (s->last_displayed_entry == NULL)
3102 ncommits_needed = s->last_displayed_entry->idx + 2 + maxscroll;
3103 if (s->commits->ncommits < ncommits_needed &&
3104 !s->thread_args.log_complete) {
3106 * Ask the log thread for required amount of commits.
3108 s->thread_args.commits_needed +=
3109 ncommits_needed - s->commits->ncommits;
3110 err = trigger_log_thread(view, 1);
3116 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
3117 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
3120 s->last_displayed_entry = pentry ?
3121 pentry : s->last_displayed_entry;
3123 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
3126 s->first_displayed_entry = pentry;
3127 } while (++nscrolled < maxscroll);
3129 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
3130 view->nscrolled += nscrolled;
3132 view->nscrolled = 0;
3137 static const struct got_error *
3138 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
3139 struct got_commit_object *commit, struct got_object_id *commit_id,
3140 struct tog_view *log_view, struct got_repository *repo)
3142 const struct got_error *err;
3143 struct got_object_qid *parent_id;
3144 struct tog_view *diff_view;
3146 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
3147 if (diff_view == NULL)
3148 return got_error_from_errno("view_open");
3150 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3151 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
3152 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
3154 *new_view = diff_view;
3158 static const struct got_error *
3159 tree_view_visit_subtree(struct tog_tree_view_state *s,
3160 struct got_tree_object *subtree)
3162 struct tog_parent_tree *parent;
3164 parent = calloc(1, sizeof(*parent));
3166 return got_error_from_errno("calloc");
3168 parent->tree = s->tree;
3169 parent->first_displayed_entry = s->first_displayed_entry;
3170 parent->selected_entry = s->selected_entry;
3171 parent->selected = s->selected;
3172 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3175 s->first_displayed_entry = NULL;
3179 static const struct got_error *
3180 tree_view_walk_path(struct tog_tree_view_state *s,
3181 struct got_commit_object *commit, const char *path)
3183 const struct got_error *err = NULL;
3184 struct got_tree_object *tree = NULL;
3186 char *slash, *subpath = NULL;
3188 /* Walk the path and open corresponding tree objects. */
3191 struct got_tree_entry *te;
3192 struct got_object_id *tree_id;
3198 /* Ensure the correct subtree entry is selected. */
3199 slash = strchr(p, '/');
3201 te_name = strdup(p);
3203 te_name = strndup(p, slash - p);
3204 if (te_name == NULL) {
3205 err = got_error_from_errno("strndup");
3208 te = got_object_tree_find_entry(s->tree, te_name);
3210 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
3215 s->first_displayed_entry = s->selected_entry = te;
3217 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
3218 break; /* jump to this file's entry */
3220 slash = strchr(p, '/');
3222 subpath = strndup(path, slash - path);
3224 subpath = strdup(path);
3225 if (subpath == NULL) {
3226 err = got_error_from_errno("strdup");
3230 err = got_object_id_by_path(&tree_id, s->repo, commit,
3235 err = got_object_open_as_tree(&tree, s->repo, tree_id);
3240 err = tree_view_visit_subtree(s, tree);
3242 got_object_tree_close(tree);
3256 static const struct got_error *
3257 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
3258 struct commit_queue_entry *entry, const char *path,
3259 const char *head_ref_name, struct got_repository *repo)
3261 const struct got_error *err = NULL;
3262 struct tog_tree_view_state *s;
3263 struct tog_view *tree_view;
3265 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
3266 if (tree_view == NULL)
3267 return got_error_from_errno("view_open");
3269 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
3272 s = &tree_view->state.tree;
3274 *new_view = tree_view;
3276 if (got_path_is_root_dir(path))
3279 return tree_view_walk_path(s, entry->commit, path);
3282 static const struct got_error *
3283 block_signals_used_by_main_thread(void)
3288 if (sigemptyset(&sigset) == -1)
3289 return got_error_from_errno("sigemptyset");
3291 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
3292 if (sigaddset(&sigset, SIGWINCH) == -1)
3293 return got_error_from_errno("sigaddset");
3294 if (sigaddset(&sigset, SIGCONT) == -1)
3295 return got_error_from_errno("sigaddset");
3296 if (sigaddset(&sigset, SIGINT) == -1)
3297 return got_error_from_errno("sigaddset");
3298 if (sigaddset(&sigset, SIGTERM) == -1)
3299 return got_error_from_errno("sigaddset");
3301 /* ncurses handles SIGTSTP */
3302 if (sigaddset(&sigset, SIGTSTP) == -1)
3303 return got_error_from_errno("sigaddset");
3305 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
3307 return got_error_set_errno(errcode, "pthread_sigmask");
3313 log_thread(void *arg)
3315 const struct got_error *err = NULL;
3317 struct tog_log_thread_args *a = arg;
3321 * Sync startup with main thread such that we begin our
3322 * work once view_input() has released the mutex.
3324 errcode = pthread_mutex_lock(&tog_mutex);
3326 err = got_error_set_errno(errcode, "pthread_mutex_lock");
3330 err = block_signals_used_by_main_thread();
3332 pthread_mutex_unlock(&tog_mutex);
3336 while (!done && !err && !tog_fatal_signal_received()) {
3337 errcode = pthread_mutex_unlock(&tog_mutex);
3339 err = got_error_set_errno(errcode,
3340 "pthread_mutex_unlock");
3343 err = queue_commits(a);
3345 if (err->code != GOT_ERR_ITER_COMPLETED)
3349 a->commits_needed = 0;
3350 } else if (a->commits_needed > 0 && !a->load_all) {
3353 a->commits_needed--;
3355 a->commits_needed--;
3358 errcode = pthread_mutex_lock(&tog_mutex);
3360 err = got_error_set_errno(errcode,
3361 "pthread_mutex_lock");
3363 } else if (*a->quit)
3365 else if (*a->limiting && *a->first_displayed_entry == NULL) {
3366 *a->first_displayed_entry =
3367 TAILQ_FIRST(&a->limit_commits->head);
3368 *a->selected_entry = *a->first_displayed_entry;
3369 } else if (*a->first_displayed_entry == NULL) {
3370 *a->first_displayed_entry =
3371 TAILQ_FIRST(&a->real_commits->head);
3372 *a->selected_entry = *a->first_displayed_entry;
3375 errcode = pthread_cond_signal(&a->commit_loaded);
3377 err = got_error_set_errno(errcode,
3378 "pthread_cond_signal");
3379 pthread_mutex_unlock(&tog_mutex);
3383 if (a->commits_needed == 0 &&
3384 a->need_commit_marker && a->worktree) {
3385 errcode = pthread_mutex_unlock(&tog_mutex);
3387 err = got_error_set_errno(errcode,
3388 "pthread_mutex_unlock");
3391 err = got_worktree_get_state(&tog_base_commit.marker,
3392 a->repo, a->worktree, NULL, NULL);
3395 errcode = pthread_mutex_lock(&tog_mutex);
3397 err = got_error_set_errno(errcode,
3398 "pthread_mutex_lock");
3401 a->need_commit_marker = 0;
3403 * The main thread did not close this
3404 * work tree yet. Close it now.
3406 got_worktree_close(a->worktree);
3414 a->commits_needed = 0;
3416 if (a->commits_needed == 0 && !a->load_all) {
3417 if (tog_io.wait_for_ui) {
3418 errcode = pthread_cond_signal(
3420 if (errcode && err == NULL)
3421 err = got_error_set_errno(
3423 "pthread_cond_signal");
3426 errcode = pthread_cond_wait(&a->need_commits,
3429 err = got_error_set_errno(errcode,
3430 "pthread_cond_wait");
3431 pthread_mutex_unlock(&tog_mutex);
3439 a->log_complete = 1;
3440 if (tog_io.wait_for_ui) {
3441 errcode = pthread_cond_signal(&a->log_loaded);
3442 if (errcode && err == NULL)
3443 err = got_error_set_errno(errcode,
3444 "pthread_cond_signal");
3447 errcode = pthread_mutex_unlock(&tog_mutex);
3449 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3452 tog_thread_error = 1;
3453 pthread_cond_signal(&a->commit_loaded);
3455 got_worktree_close(a->worktree);
3462 static const struct got_error *
3463 stop_log_thread(struct tog_log_view_state *s)
3465 const struct got_error *err = NULL, *thread_err = NULL;
3470 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3472 return got_error_set_errno(errcode,
3473 "pthread_cond_signal");
3474 errcode = pthread_mutex_unlock(&tog_mutex);
3476 return got_error_set_errno(errcode,
3477 "pthread_mutex_unlock");
3478 errcode = pthread_join(s->thread, (void **)&thread_err);
3480 return got_error_set_errno(errcode, "pthread_join");
3481 errcode = pthread_mutex_lock(&tog_mutex);
3483 return got_error_set_errno(errcode,
3484 "pthread_mutex_lock");
3488 if (s->thread_args.repo) {
3489 err = got_repo_close(s->thread_args.repo);
3490 s->thread_args.repo = NULL;
3493 if (s->thread_args.pack_fds) {
3494 const struct got_error *pack_err =
3495 got_repo_pack_fds_close(s->thread_args.pack_fds);
3498 s->thread_args.pack_fds = NULL;
3501 if (s->thread_args.graph) {
3502 got_commit_graph_close(s->thread_args.graph);
3503 s->thread_args.graph = NULL;
3506 return err ? err : thread_err;
3509 static const struct got_error *
3510 close_log_view(struct tog_view *view)
3512 const struct got_error *err = NULL;
3513 struct tog_log_view_state *s = &view->state.log;
3516 err = stop_log_thread(s);
3518 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3519 if (errcode && err == NULL)
3520 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3522 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3523 if (errcode && err == NULL)
3524 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3526 free_commits(&s->limit_commits);
3527 free_commits(&s->real_commits);
3528 free_colors(&s->colors);
3529 free(s->in_repo_path);
3530 s->in_repo_path = NULL;
3533 free(s->head_ref_name);
3534 s->head_ref_name = NULL;
3539 * We use two queues to implement the limit feature: first consists of
3540 * commits matching the current limit_regex; second is the real queue
3541 * of all known commits (real_commits). When the user starts limiting,
3542 * we swap queues such that all movement and displaying functionality
3543 * works with very slight change.
3545 static const struct got_error *
3546 limit_log_view(struct tog_view *view)
3548 struct tog_log_view_state *s = &view->state.log;
3549 struct commit_queue_entry *entry;
3550 struct tog_view *v = view;
3551 const struct got_error *err = NULL;
3555 if (view_is_hsplit_top(view))
3557 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3560 if (tog_io.input_str != NULL) {
3561 if (strlcpy(pattern, tog_io.input_str, sizeof(pattern)) >=
3563 return got_error(GOT_ERR_NO_SPACE);
3565 wmove(v->window, v->nlines - 1, 0);
3566 wclrtoeol(v->window);
3567 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3568 nodelay(v->window, FALSE);
3571 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3574 nodelay(v->window, TRUE);
3579 if (*pattern == '\0') {
3581 * Safety measure for the situation where the user
3582 * resets limit without previously limiting anything.
3588 * User could have pressed Ctrl+L, which refreshed the
3589 * commit queues, it means we can't save previously
3590 * (before limit took place) displayed entries,
3591 * because they would point to already free'ed memory,
3592 * so we are forced to always select first entry of
3595 s->commits = &s->real_commits;
3596 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3597 s->selected_entry = s->first_displayed_entry;
3604 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3609 /* Clear the screen while loading limit view */
3610 s->first_displayed_entry = NULL;
3611 s->last_displayed_entry = NULL;
3612 s->selected_entry = NULL;
3613 s->commits = &s->limit_commits;
3615 /* Prepare limit queue for new search */
3616 free_commits(&s->limit_commits);
3617 s->limit_commits.ncommits = 0;
3619 /* First process commits, which are in queue already */
3620 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3623 err = match_commit(&have_match, entry->id,
3624 entry->commit, &s->limit_regex);
3629 struct commit_queue_entry *matched;
3631 matched = alloc_commit_queue_entry(entry->commit,
3633 if (matched == NULL) {
3634 err = got_error_from_errno(
3635 "alloc_commit_queue_entry");
3638 matched->commit = entry->commit;
3639 got_object_commit_retain(entry->commit);
3641 matched->idx = s->limit_commits.ncommits;
3642 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3644 s->limit_commits.ncommits++;
3648 /* Second process all the commits, until we fill the screen */
3649 if (s->limit_commits.ncommits < view->nlines - 1 &&
3650 !s->thread_args.log_complete) {
3651 s->thread_args.commits_needed +=
3652 view->nlines - s->limit_commits.ncommits - 1;
3653 err = trigger_log_thread(view, 1);
3658 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3659 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3665 static const struct got_error *
3666 search_start_log_view(struct tog_view *view)
3668 struct tog_log_view_state *s = &view->state.log;
3670 s->matched_entry = NULL;
3671 s->search_entry = NULL;
3675 static const struct got_error *
3676 search_next_log_view(struct tog_view *view)
3678 const struct got_error *err = NULL;
3679 struct tog_log_view_state *s = &view->state.log;
3680 struct commit_queue_entry *entry;
3682 /* Display progress update in log view. */
3683 show_log_view(view);
3687 if (s->search_entry) {
3688 if (!using_mock_io) {
3691 errcode = pthread_mutex_unlock(&tog_mutex);
3693 return got_error_set_errno(errcode,
3694 "pthread_mutex_unlock");
3695 ch = wgetch(view->window);
3696 errcode = pthread_mutex_lock(&tog_mutex);
3698 return got_error_set_errno(errcode,
3699 "pthread_mutex_lock");
3700 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3701 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3705 if (view->searching == TOG_SEARCH_FORWARD)
3706 entry = TAILQ_NEXT(s->search_entry, entry);
3708 entry = TAILQ_PREV(s->search_entry,
3709 commit_queue_head, entry);
3710 } else if (s->matched_entry) {
3712 * If the user has moved the cursor after we hit a match,
3713 * the position from where we should continue searching
3714 * might have changed.
3716 if (view->searching == TOG_SEARCH_FORWARD)
3717 entry = TAILQ_NEXT(s->selected_entry, entry);
3719 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3722 entry = s->selected_entry;
3728 if (entry == NULL) {
3729 if (s->thread_args.log_complete ||
3730 view->searching == TOG_SEARCH_BACKWARD) {
3731 view->search_next_done =
3732 (s->matched_entry == NULL ?
3733 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3734 s->search_entry = NULL;
3738 * Poke the log thread for more commits and return,
3739 * allowing the main loop to make progress. Search
3740 * will resume at s->search_entry once we come back.
3742 s->search_entry = s->selected_entry;
3743 s->thread_args.commits_needed++;
3744 return trigger_log_thread(view, 0);
3747 err = match_commit(&have_match, entry->id, entry->commit,
3752 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3753 s->matched_entry = entry;
3757 s->search_entry = entry;
3758 if (view->searching == TOG_SEARCH_FORWARD)
3759 entry = TAILQ_NEXT(entry, entry);
3761 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3764 if (s->matched_entry) {
3765 int cur = s->selected_entry->idx;
3766 while (cur < s->matched_entry->idx) {
3767 err = input_log_view(NULL, view, KEY_DOWN);
3772 while (cur > s->matched_entry->idx) {
3773 err = input_log_view(NULL, view, KEY_UP);
3780 s->search_entry = NULL;
3785 static const struct got_error *
3786 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3787 struct got_repository *repo, const char *head_ref_name,
3788 const char *in_repo_path, int log_branches,
3789 struct got_worktree *worktree)
3791 const struct got_error *err = NULL;
3792 struct tog_log_view_state *s = &view->state.log;
3793 struct got_repository *thread_repo = NULL;
3794 struct got_commit_graph *thread_graph = NULL;
3797 if (in_repo_path != s->in_repo_path) {
3798 free(s->in_repo_path);
3799 s->in_repo_path = strdup(in_repo_path);
3800 if (s->in_repo_path == NULL) {
3801 err = got_error_from_errno("strdup");
3806 /* The commit queue only contains commits being displayed. */
3807 TAILQ_INIT(&s->real_commits.head);
3808 s->real_commits.ncommits = 0;
3809 s->commits = &s->real_commits;
3811 TAILQ_INIT(&s->limit_commits.head);
3813 s->limit_commits.ncommits = 0;
3816 if (head_ref_name) {
3817 s->head_ref_name = strdup(head_ref_name);
3818 if (s->head_ref_name == NULL) {
3819 err = got_error_from_errno("strdup");
3823 s->start_id = got_object_id_dup(start_id);
3824 if (s->start_id == NULL) {
3825 err = got_error_from_errno("got_object_id_dup");
3828 s->log_branches = log_branches;
3829 s->use_committer = 1;
3831 STAILQ_INIT(&s->colors);
3832 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3833 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3834 get_color_value("TOG_COLOR_COMMIT"));
3837 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3838 get_color_value("TOG_COLOR_AUTHOR"));
3841 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3842 get_color_value("TOG_COLOR_DATE"));
3847 view->show = show_log_view;
3848 view->input = input_log_view;
3849 view->resize = resize_log_view;
3850 view->close = close_log_view;
3851 view->search_start = search_start_log_view;
3852 view->search_next = search_next_log_view;
3854 if (s->thread_args.pack_fds == NULL) {
3855 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3859 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3860 s->thread_args.pack_fds);
3863 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3867 err = got_commit_graph_bfsort(thread_graph, s->start_id,
3868 s->repo, NULL, NULL);
3872 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3874 err = got_error_set_errno(errcode, "pthread_cond_init");
3877 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3879 err = got_error_set_errno(errcode, "pthread_cond_init");
3883 if (using_mock_io) {
3886 rc = pthread_cond_init(&s->thread_args.log_loaded, NULL);
3888 return got_error_set_errno(rc, "pthread_cond_init");
3891 s->thread_args.commits_needed = view->nlines;
3892 s->thread_args.graph = thread_graph;
3893 s->thread_args.real_commits = &s->real_commits;
3894 s->thread_args.limit_commits = &s->limit_commits;
3895 s->thread_args.in_repo_path = s->in_repo_path;
3896 s->thread_args.start_id = s->start_id;
3897 s->thread_args.repo = thread_repo;
3898 s->thread_args.log_complete = 0;
3899 s->thread_args.quit = &s->quit;
3900 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3901 s->thread_args.selected_entry = &s->selected_entry;
3902 s->thread_args.searching = &view->searching;
3903 s->thread_args.search_next_done = &view->search_next_done;
3904 s->thread_args.regex = &view->regex;
3905 s->thread_args.limiting = &s->limit_view;
3906 s->thread_args.limit_regex = &s->limit_regex;
3907 s->thread_args.limit_commits = &s->limit_commits;
3908 s->thread_args.worktree = worktree;
3910 s->thread_args.need_commit_marker = 1;
3913 if (view->close == NULL)
3914 close_log_view(view);
3920 static const struct got_error *
3921 show_log_view(struct tog_view *view)
3923 const struct got_error *err;
3924 struct tog_log_view_state *s = &view->state.log;
3926 if (s->thread == NULL) {
3927 int errcode = pthread_create(&s->thread, NULL, log_thread,
3930 return got_error_set_errno(errcode, "pthread_create");
3931 if (s->thread_args.commits_needed > 0) {
3932 err = trigger_log_thread(view, 1);
3938 return draw_commits(view);
3942 log_move_cursor_up(struct tog_view *view, int page, int home)
3944 struct tog_log_view_state *s = &view->state.log;
3946 if (s->first_displayed_entry == NULL)
3948 if (s->selected_entry->idx == 0)
3951 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3953 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3955 if (!page && !home && s->selected > 0)
3958 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3964 static const struct got_error *
3965 log_move_cursor_down(struct tog_view *view, int page)
3967 struct tog_log_view_state *s = &view->state.log;
3968 const struct got_error *err = NULL;
3969 int eos = view->nlines - 2;
3971 if (s->first_displayed_entry == NULL)
3974 if (s->thread_args.log_complete &&
3975 s->selected_entry->idx >= s->commits->ncommits - 1)
3978 if (view_is_hsplit_top(view))
3979 --eos; /* border consumes the last line */
3982 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3985 err = log_scroll_down(view, 1);
3986 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3987 struct commit_queue_entry *entry;
3991 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3992 s->last_displayed_entry = entry;
3993 for (n = 0; n <= eos; n++) {
3996 s->first_displayed_entry = entry;
3997 entry = TAILQ_PREV(entry, commit_queue_head, entry);
4000 s->selected = n - 1;
4002 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
4003 s->thread_args.log_complete)
4004 s->selected += MIN(page,
4005 s->commits->ncommits - s->selected_entry->idx - 1);
4007 err = log_scroll_down(view, page);
4013 * We might necessarily overshoot in horizontal
4014 * splits; if so, select the last displayed commit.
4016 if (view_is_hsplit_top(view) && s->first_displayed_entry &&
4017 s->last_displayed_entry) {
4018 s->selected = MIN(s->selected,
4019 s->last_displayed_entry->idx -
4020 s->first_displayed_entry->idx);
4025 if (s->thread_args.log_complete &&
4026 s->selected_entry->idx == s->commits->ncommits - 1)
4033 view_get_split(struct tog_view *view, int *y, int *x)
4038 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
4039 if (view->child && view->child->resized_y)
4040 *y = view->child->resized_y;
4041 else if (view->resized_y)
4042 *y = view->resized_y;
4044 *y = view_split_begin_y(view->lines);
4045 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
4046 if (view->child && view->child->resized_x)
4047 *x = view->child->resized_x;
4048 else if (view->resized_x)
4049 *x = view->resized_x;
4051 *x = view_split_begin_x(view->begin_x);
4055 /* Split view horizontally at y and offset view->state->selected line. */
4056 static const struct got_error *
4057 view_init_hsplit(struct tog_view *view, int y)
4059 const struct got_error *err = NULL;
4063 err = view_resize(view);
4067 err = offset_selection_down(view);
4072 static const struct got_error *
4073 log_goto_line(struct tog_view *view, int nlines)
4075 const struct got_error *err = NULL;
4076 struct tog_log_view_state *s = &view->state.log;
4077 int g, idx = s->selected_entry->idx;
4079 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
4085 if (g >= s->first_displayed_entry->idx + 1 &&
4086 g <= s->last_displayed_entry->idx + 1 &&
4087 g - s->first_displayed_entry->idx - 1 < nlines) {
4088 s->selected = g - s->first_displayed_entry->idx - 1;
4094 err = log_move_cursor_down(view, g - idx - 1);
4095 if (!err && g > s->selected_entry->idx + 1)
4096 err = log_move_cursor_down(view,
4097 g - s->first_displayed_entry->idx - 1);
4100 } else if (idx + 1 > g)
4101 log_move_cursor_up(view, idx - g + 1, 0);
4103 if (g < nlines && s->first_displayed_entry->idx == 0)
4104 s->selected = g - 1;
4112 horizontal_scroll_input(struct tog_view *view, int ch)
4118 view->x -= MIN(view->x, 2);
4124 if (view->x + view->ncols / 2 < view->maxx)
4133 view->x = MAX(view->maxx - view->ncols / 2, 0);
4141 static const struct got_error *
4142 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
4144 const struct got_error *err = NULL;
4145 struct tog_log_view_state *s = &view->state.log;
4148 if (s->thread_args.load_all) {
4149 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
4150 s->thread_args.load_all = 0;
4151 else if (s->thread_args.log_complete) {
4152 err = log_move_cursor_down(view, s->commits->ncommits);
4153 s->thread_args.load_all = 0;
4159 eos = nscroll = view->nlines - 1;
4160 if (view_is_hsplit_top(view))
4164 return log_goto_line(view, eos);
4168 err = limit_log_view(view);
4179 horizontal_scroll_input(view, ch);
4186 log_move_cursor_up(view, 0, 0);
4191 log_move_cursor_up(view, 0, 1);
4201 log_move_cursor_up(view, nscroll, 0);
4208 err = log_move_cursor_down(view, 0);
4211 s->use_committer = !s->use_committer;
4212 view->action = s->use_committer ?
4213 "show committer" : "show commit author";
4218 /* We don't know yet how many commits, so we're forced to
4219 * traverse them all. */
4221 s->thread_args.load_all = 1;
4222 if (!s->thread_args.log_complete)
4223 return trigger_log_thread(view, 0);
4224 err = log_move_cursor_down(view, s->commits->ncommits);
4225 s->thread_args.load_all = 0;
4236 err = log_move_cursor_down(view, nscroll);
4239 if (s->selected > view->nlines - 2)
4240 s->selected = view->nlines - 2;
4241 if (s->selected > s->commits->ncommits - 1)
4242 s->selected = s->commits->ncommits - 1;
4244 if (s->commits->ncommits < view->nlines - 1 &&
4245 !s->thread_args.log_complete) {
4246 s->thread_args.commits_needed += (view->nlines - 1) -
4247 s->commits->ncommits;
4248 err = trigger_log_thread(view, 1);
4254 if (s->selected_entry == NULL)
4256 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4260 if (s->selected_entry == NULL)
4262 err = view_request_new(new_view, view, TOG_VIEW_TREE);
4268 if (ch == KEY_BACKSPACE &&
4269 got_path_is_root_dir(s->in_repo_path))
4271 err = stop_log_thread(s);
4274 if (ch == KEY_BACKSPACE) {
4276 err = got_path_dirname(&parent_path, s->in_repo_path);
4279 free(s->in_repo_path);
4280 s->in_repo_path = parent_path;
4281 s->thread_args.in_repo_path = s->in_repo_path;
4282 } else if (ch == CTRL('l')) {
4283 struct got_object_id *start_id;
4284 err = got_repo_match_object_id(&start_id, NULL,
4285 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4286 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4288 if (s->head_ref_name == NULL ||
4289 err->code != GOT_ERR_NOT_REF)
4291 /* Try to cope with deleted references. */
4292 free(s->head_ref_name);
4293 s->head_ref_name = NULL;
4294 err = got_repo_match_object_id(&start_id,
4295 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4296 &tog_refs, s->repo);
4301 s->start_id = start_id;
4302 s->thread_args.start_id = s->start_id;
4304 s->log_branches = !s->log_branches;
4306 if (s->thread_args.pack_fds == NULL) {
4307 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4311 err = got_repo_open(&s->thread_args.repo,
4312 got_repo_get_path(s->repo), NULL,
4313 s->thread_args.pack_fds);
4317 err = tog_load_refs(s->repo, 0);
4320 err = got_commit_graph_open(&s->thread_args.graph,
4321 s->in_repo_path, !s->log_branches);
4324 err = got_commit_graph_bfsort(s->thread_args.graph,
4325 s->start_id, s->repo, NULL, NULL);
4328 free_commits(&s->real_commits);
4329 free_commits(&s->limit_commits);
4330 s->first_displayed_entry = NULL;
4331 s->last_displayed_entry = NULL;
4332 s->selected_entry = NULL;
4334 s->thread_args.log_complete = 0;
4336 s->thread_args.commits_needed = view->lines;
4337 s->matched_entry = NULL;
4338 s->search_entry = NULL;
4343 err = view_request_new(new_view, view, TOG_VIEW_REF);
4353 static const struct got_error *
4354 apply_unveil(const char *repo_path, const char *worktree_path)
4356 const struct got_error *error;
4359 if (unveil("gmon.out", "rwc") != 0)
4360 return got_error_from_errno2("unveil", "gmon.out");
4362 if (repo_path && unveil(repo_path, "r") != 0)
4363 return got_error_from_errno2("unveil", repo_path);
4365 if (worktree_path && unveil(worktree_path, "rwc") != 0)
4366 return got_error_from_errno2("unveil", worktree_path);
4368 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4369 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4371 error = got_privsep_unveil_exec_helpers();
4375 if (unveil(NULL, NULL) != 0)
4376 return got_error_from_errno("unveil");
4381 static const struct got_error *
4382 init_mock_term(const char *test_script_path)
4384 const struct got_error *err = NULL;
4385 const char *screen_dump_path;
4388 if (test_script_path == NULL || *test_script_path == '\0')
4389 return got_error_msg(GOT_ERR_IO, "TOG_TEST_SCRIPT not defined");
4391 tog_io.f = fopen(test_script_path, "re");
4392 if (tog_io.f == NULL) {
4393 err = got_error_from_errno_fmt("fopen: %s",
4398 /* test mode, we don't want any output */
4399 tog_io.cout = fopen("/dev/null", "w+");
4400 if (tog_io.cout == NULL) {
4401 err = got_error_from_errno2("fopen", "/dev/null");
4405 in = dup(fileno(tog_io.cout));
4407 err = got_error_from_errno("dup");
4410 tog_io.cin = fdopen(in, "r");
4411 if (tog_io.cin == NULL) {
4412 err = got_error_from_errno("fdopen");
4417 screen_dump_path = getenv("TOG_SCR_DUMP");
4418 if (screen_dump_path == NULL || *screen_dump_path == '\0')
4419 return got_error_msg(GOT_ERR_IO, "TOG_SCR_DUMP not defined");
4420 tog_io.sdump = fopen(screen_dump_path, "we");
4421 if (tog_io.sdump == NULL) {
4422 err = got_error_from_errno2("fopen", screen_dump_path);
4426 if (fseeko(tog_io.f, 0L, SEEK_SET) == -1) {
4427 err = got_error_from_errno("fseeko");
4431 if (newterm(NULL, tog_io.cout, tog_io.cin) == NULL)
4432 err = got_error_msg(GOT_ERR_IO,
4433 "newterm: failed to initialise curses");
4446 if (using_mock_io) /* In test mode we use a fake terminal */
4452 halfdelay(1); /* Fast refresh while initial view is loading. */
4455 intrflush(stdscr, FALSE);
4456 keypad(stdscr, TRUE);
4458 if (getenv("TOG_COLORS") != NULL) {
4460 use_default_colors();
4466 static const struct got_error *
4467 set_tog_base_commit(struct got_repository *repo, struct got_worktree *worktree)
4469 tog_base_commit.id = got_object_id_dup(
4470 got_worktree_get_base_commit_id(worktree));
4471 if (tog_base_commit.id == NULL)
4472 return got_error_from_errno( "got_object_id_dup");
4477 static const struct got_error *
4478 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4479 struct got_repository *repo, struct got_worktree *worktree)
4481 const struct got_error *err = NULL;
4484 *in_repo_path = strdup("/");
4485 if (*in_repo_path == NULL)
4486 return got_error_from_errno("strdup");
4491 const char *prefix = got_worktree_get_path_prefix(worktree);
4494 err = got_worktree_resolve_path(&p, worktree, argv[0]);
4497 if (asprintf(in_repo_path, "%s%s%s", prefix,
4498 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4500 err = got_error_from_errno("asprintf");
4501 *in_repo_path = NULL;
4505 err = got_repo_map_path(in_repo_path, repo, argv[0]);
4510 static const struct got_error *
4511 cmd_log(int argc, char *argv[])
4513 const struct got_error *error;
4514 struct got_repository *repo = NULL;
4515 struct got_worktree *worktree = NULL;
4516 struct got_object_id *start_id = NULL;
4517 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4518 char *keyword_idstr = NULL, *start_commit = NULL, *label = NULL;
4519 struct got_reference *ref = NULL;
4520 const char *head_ref_name = NULL;
4521 int ch, log_branches = 0;
4522 struct tog_view *view;
4523 int *pack_fds = NULL;
4525 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4531 start_commit = optarg;
4534 repo_path = realpath(optarg, NULL);
4535 if (repo_path == NULL)
4536 return got_error_from_errno2("realpath",
4551 error = got_repo_pack_fds_open(&pack_fds);
4555 if (repo_path == NULL) {
4556 cwd = getcwd(NULL, 0);
4558 error = got_error_from_errno("getcwd");
4561 error = got_worktree_open(&worktree, cwd, NULL);
4562 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4566 strdup(got_worktree_get_repo_path(worktree));
4568 repo_path = strdup(cwd);
4569 if (repo_path == NULL) {
4570 error = got_error_from_errno("strdup");
4575 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4579 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4586 error = apply_unveil(got_repo_get_path(repo),
4587 worktree ? got_worktree_get_root_path(worktree) : NULL);
4591 /* already loaded by tog_log_with_path()? */
4592 if (TAILQ_EMPTY(&tog_refs)) {
4593 error = tog_load_refs(repo, 0);
4598 if (start_commit == NULL) {
4599 error = got_repo_match_object_id(&start_id, &label,
4600 worktree ? got_worktree_get_head_ref_name(worktree) :
4601 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4604 head_ref_name = label;
4606 error = got_keyword_to_idstr(&keyword_idstr, start_commit,
4610 if (keyword_idstr != NULL)
4611 start_commit = keyword_idstr;
4613 error = got_ref_open(&ref, repo, start_commit, 0);
4615 head_ref_name = got_ref_get_name(ref);
4616 else if (error->code != GOT_ERR_NOT_REF)
4618 error = got_repo_match_object_id(&start_id, NULL,
4619 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4624 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4626 error = got_error_from_errno("view_open");
4631 error = set_tog_base_commit(repo, worktree);
4636 error = open_log_view(view, start_id, repo, head_ref_name,
4637 in_repo_path, log_branches, worktree);
4642 /* The work tree will be closed by the log thread. */
4646 error = view_loop(view);
4649 free(tog_base_commit.id);
4650 free(keyword_idstr);
4659 const struct got_error *close_err = got_repo_close(repo);
4664 got_worktree_close(worktree);
4666 const struct got_error *pack_err =
4667 got_repo_pack_fds_close(pack_fds);
4679 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4680 "object1 object2\n", getprogname());
4685 match_line(const char *line, regex_t *regex, size_t nmatch,
4686 regmatch_t *regmatch)
4688 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4691 static struct tog_color *
4692 match_color(struct tog_colors *colors, const char *line)
4694 struct tog_color *tc = NULL;
4696 STAILQ_FOREACH(tc, colors, entry) {
4697 if (match_line(line, &tc->regex, 0, NULL))
4704 static const struct got_error *
4705 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4706 WINDOW *window, int skipcol, regmatch_t *regmatch)
4708 const struct got_error *err = NULL;
4710 wchar_t *wline = NULL;
4711 int rme, rms, n, width, scrollx;
4712 int width0 = 0, width1 = 0, width2 = 0;
4713 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4717 rms = regmatch->rm_so;
4718 rme = regmatch->rm_eo;
4720 err = expand_tab(&exstr, line);
4724 /* Split the line into 3 segments, according to match offsets. */
4725 seg0 = strndup(exstr, rms);
4727 err = got_error_from_errno("strndup");
4730 seg1 = strndup(exstr + rms, rme - rms);
4732 err = got_error_from_errno("strndup");
4735 seg2 = strdup(exstr + rme);
4737 err = got_error_from_errno("strndup");
4741 /* draw up to matched token if we haven't scrolled past it */
4742 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4746 n = MAX(width0 - skipcol, 0);
4749 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4750 wlimit, col_tab_align, 1);
4753 waddwstr(window, &wline[scrollx]);
4763 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4767 wlen = wcslen(wline);
4769 width = wcwidth(wline[i]);
4771 /* should not happen, tabs are expanded */
4772 err = got_error(GOT_ERR_RANGE);
4775 if (width0 + w + width > skipcol)
4780 /* draw (visible part of) matched token (if scrolled into it) */
4781 if (width1 - w > 0) {
4782 wattron(window, A_STANDOUT);
4783 waddwstr(window, &wline[i]);
4784 wattroff(window, A_STANDOUT);
4785 wlimit -= (width1 - w);
4786 *wtotal += (width1 - w);
4790 if (wlimit > 0) { /* draw rest of line */
4792 if (skipcol > width0 + width1) {
4793 err = format_line(&wline, &width2, &scrollx, seg2,
4794 skipcol - (width0 + width1), wlimit,
4798 waddwstr(window, &wline[scrollx]);
4800 err = format_line(&wline, &width2, NULL, seg2, 0,
4801 wlimit, col_tab_align, 1);
4804 waddwstr(window, wline);
4818 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4821 int *eof, *first, *selected;
4823 if (view->type == TOG_VIEW_DIFF) {
4824 struct tog_diff_view_state *s = &view->state.diff;
4826 first = &s->first_displayed_line;
4830 } else if (view->type == TOG_VIEW_HELP) {
4831 struct tog_help_view_state *s = &view->state.help;
4833 first = &s->first_displayed_line;
4837 } else if (view->type == TOG_VIEW_BLAME) {
4838 struct tog_blame_view_state *s = &view->state.blame;
4840 first = &s->first_displayed_line;
4841 selected = &s->selected_line;
4847 /* Center gline in the middle of the page like vi(1). */
4848 if (*lineno < view->gline - (view->nlines - 3) / 2)
4850 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4859 *selected = view->gline <= (view->nlines - 3) / 2 ?
4860 view->gline : (view->nlines - 3) / 2 + 1;
4866 static const struct got_error *
4867 draw_file(struct tog_view *view, const char *header)
4869 struct tog_diff_view_state *s = &view->state.diff;
4870 regmatch_t *regmatch = &view->regmatch;
4871 const struct got_error *err;
4874 size_t linesize = 0;
4878 int max_lines = view->nlines;
4879 int nlines = s->nlines;
4882 s->lineno = s->first_displayed_line - 1;
4883 line_offset = s->lines[s->first_displayed_line - 1].offset;
4884 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4885 return got_error_from_errno("fseek");
4887 werase(view->window);
4889 if (view->gline > s->nlines - 1)
4890 view->gline = s->nlines - 1;
4893 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4894 1 : view->gline - (view->nlines - 3) / 2 :
4895 s->lineno + s->selected_line;
4897 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4898 return got_error_from_errno("asprintf");
4899 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4905 if (view_needs_focus_indication(view))
4906 wstandout(view->window);
4907 waddwstr(view->window, wline);
4910 while (width++ < view->ncols)
4911 waddch(view->window, ' ');
4912 if (view_needs_focus_indication(view))
4913 wstandend(view->window);
4923 while (max_lines > 0 && nprinted < max_lines) {
4924 enum got_diff_line_type linetype;
4927 linelen = getline(&line, &linesize, s->f);
4928 if (linelen == -1) {
4934 return got_ferror(s->f, GOT_ERR_IO);
4937 if (++s->lineno < s->first_displayed_line)
4939 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4941 if (s->lineno == view->hiline)
4944 /* Set view->maxx based on full line length. */
4945 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4951 view->maxx = MAX(view->maxx, width);
4955 linetype = s->lines[s->lineno].type;
4956 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4957 linetype < GOT_DIFF_LINE_CONTEXT)
4958 attr |= COLOR_PAIR(linetype);
4960 wattron(view->window, attr);
4961 if (s->first_displayed_line + nprinted == s->matched_line &&
4962 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4963 err = add_matched_line(&width, line, view->ncols, 0,
4964 view->window, view->x, regmatch);
4971 err = format_line(&wline, &width, &skip, line,
4972 view->x, view->ncols, 0, view->x ? 1 : 0);
4977 waddwstr(view->window, &wline[skip]);
4981 if (s->lineno == view->hiline) {
4982 /* highlight full gline length */
4983 while (width++ < view->ncols)
4984 waddch(view->window, ' ');
4986 if (width <= view->ncols - 1)
4987 waddch(view->window, '\n');
4990 wattroff(view->window, attr);
4991 if (++nprinted == 1)
4992 s->first_displayed_line = s->lineno;
4996 s->last_displayed_line = s->first_displayed_line +
4999 s->last_displayed_line = s->first_displayed_line;
5004 while (nprinted < view->nlines) {
5005 waddch(view->window, '\n');
5009 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
5015 wstandout(view->window);
5016 waddwstr(view->window, wline);
5019 wstandend(view->window);
5026 get_datestr(time_t *time, char *datebuf)
5028 struct tm mytm, *tm;
5031 tm = gmtime_r(time, &mytm);
5034 s = asctime_r(tm, datebuf);
5037 p = strchr(s, '\n');
5043 static const struct got_error *
5044 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
5045 off_t off, uint8_t type)
5047 struct got_diff_line *p;
5049 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
5051 return got_error_from_errno("reallocarray");
5053 (*lines)[*nlines].offset = off;
5054 (*lines)[*nlines].type = type;
5060 static const struct got_error *
5061 cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
5062 struct got_diff_line *s_lines, size_t s_nlines)
5064 struct got_diff_line *p;
5068 if (fseeko(src, 0L, SEEK_SET) == -1)
5069 return got_error_from_errno("fseeko");
5072 r = fread(buf, 1, sizeof(buf), src);
5075 return got_error_from_errno("fread");
5079 if (fwrite(buf, 1, r, dst) != r)
5080 return got_ferror(dst, GOT_ERR_IO);
5083 if (s_nlines == 0 && *d_nlines == 0)
5087 * If commit info was in dst, increment line offsets
5088 * of the appended diff content, but skip s_lines[0]
5089 * because offset zero is already in *d_lines.
5091 if (*d_nlines > 0) {
5092 for (i = 1; i < s_nlines; ++i)
5093 s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
5101 p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
5103 /* d_lines is freed in close_diff_view() */
5104 return got_error_from_errno("reallocarray");
5109 memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
5110 *d_nlines += s_nlines;
5115 static const struct got_error *
5116 write_commit_info(struct got_diff_line **lines, size_t *nlines,
5117 struct got_object_id *commit_id, struct got_reflist_head *refs,
5118 struct got_repository *repo, int ignore_ws, int force_text_diff,
5119 struct got_diffstat_cb_arg *dsa, FILE *outfile)
5121 const struct got_error *err = NULL;
5122 char datebuf[26], *datestr;
5123 struct got_commit_object *commit;
5124 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
5125 time_t committer_time;
5126 const char *author, *committer;
5127 char *refs_str = NULL;
5128 struct got_pathlist_entry *pe;
5132 err = build_refs_str(&refs_str, refs, commit_id, repo);
5136 err = got_object_open_as_commit(&commit, repo, commit_id);
5140 err = got_object_id_str(&id_str, commit_id);
5142 err = got_error_from_errno("got_object_id_str");
5146 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
5150 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
5151 refs_str ? refs_str : "", refs_str ? ")" : "");
5153 err = got_error_from_errno("fprintf");
5157 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
5161 n = fprintf(outfile, "from: %s\n",
5162 got_object_commit_get_author(commit));
5164 err = got_error_from_errno("fprintf");
5168 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
5172 author = got_object_commit_get_author(commit);
5173 committer = got_object_commit_get_committer(commit);
5174 if (strcmp(author, committer) != 0) {
5175 n = fprintf(outfile, "via: %s\n", committer);
5177 err = got_error_from_errno("fprintf");
5181 err = add_line_metadata(lines, nlines, outoff,
5182 GOT_DIFF_LINE_AUTHOR);
5186 committer_time = got_object_commit_get_committer_time(commit);
5187 datestr = get_datestr(&committer_time, datebuf);
5189 n = fprintf(outfile, "date: %s UTC\n", datestr);
5191 err = got_error_from_errno("fprintf");
5195 err = add_line_metadata(lines, nlines, outoff,
5196 GOT_DIFF_LINE_DATE);
5200 if (got_object_commit_get_nparents(commit) > 1) {
5201 const struct got_object_id_queue *parent_ids;
5202 struct got_object_qid *qid;
5204 parent_ids = got_object_commit_get_parent_ids(commit);
5205 STAILQ_FOREACH(qid, parent_ids, entry) {
5206 err = got_object_id_str(&id_str, &qid->id);
5209 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
5211 err = got_error_from_errno("fprintf");
5215 err = add_line_metadata(lines, nlines, outoff,
5216 GOT_DIFF_LINE_META);
5224 err = got_object_commit_get_logmsg(&logmsg, commit);
5228 while ((line = strsep(&s, "\n")) != NULL) {
5229 n = fprintf(outfile, "%s\n", line);
5231 err = got_error_from_errno("fprintf");
5235 err = add_line_metadata(lines, nlines, outoff,
5236 GOT_DIFF_LINE_LOGMSG);
5241 TAILQ_FOREACH(pe, dsa->paths, entry) {
5242 struct got_diff_changed_path *cp = pe->data;
5243 int pad = dsa->max_path_len - pe->path_len + 1;
5245 n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
5246 pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
5247 dsa->rm_cols + 1, cp->rm);
5249 err = got_error_from_errno("fprintf");
5253 err = add_line_metadata(lines, nlines, outoff,
5254 GOT_DIFF_LINE_CHANGES);
5259 fputc('\n', outfile);
5261 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5265 n = fprintf(outfile,
5266 "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5267 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5268 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5270 err = got_error_from_errno("fprintf");
5274 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5278 fputc('\n', outfile);
5280 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5285 got_object_commit_close(commit);
5294 static const struct got_error *
5295 create_diff(struct tog_diff_view_state *s)
5297 const struct got_error *err = NULL;
5298 FILE *tmp_diff_file = NULL;
5300 struct got_diff_line *lines = NULL;
5301 struct got_pathlist_head changed_paths;
5302 struct got_commit_object *commit2 = NULL;
5304 TAILQ_INIT(&changed_paths);
5307 s->lines = malloc(sizeof(*s->lines));
5308 if (s->lines == NULL)
5309 return got_error_from_errno("malloc");
5312 if (s->f && fclose(s->f) == EOF) {
5314 return got_error_from_errno("fclose");
5317 s->f = got_opentemp();
5319 return got_error_from_errno("got_opentemp");
5321 tmp_diff_file = got_opentemp();
5322 if (tmp_diff_file == NULL)
5323 return got_error_from_errno("got_opentemp");
5326 err = got_object_get_type(&obj_type, s->repo, s->id1);
5328 err = got_object_get_type(&obj_type, s->repo, s->id2);
5333 case GOT_OBJ_TYPE_BLOB:
5334 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5335 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5336 s->label1, s->label2, tog_diff_algo, s->diff_context,
5337 s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5340 case GOT_OBJ_TYPE_TREE:
5341 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5342 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5343 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5344 s->force_text_diff, NULL, s->repo, s->f);
5346 case GOT_OBJ_TYPE_COMMIT: {
5347 const struct got_object_id_queue *parent_ids;
5348 struct got_object_qid *pid;
5349 struct got_reflist_head *refs;
5351 struct got_diffstat_cb_arg dsa = {
5354 s->ignore_whitespace,
5359 lines = malloc(sizeof(*lines));
5360 if (lines == NULL) {
5361 err = got_error_from_errno("malloc");
5365 /* build diff first in tmp file then append to commit info */
5366 err = got_diff_objects_as_commits(&lines, &nlines,
5367 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5368 tog_diff_algo, s->diff_context, s->ignore_whitespace,
5369 s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5373 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5374 /* Show commit info if we're diffing to a parent/root commit. */
5375 if (s->id1 == NULL) {
5376 err = write_commit_info(&s->lines, &s->nlines, s->id2,
5377 refs, s->repo, s->ignore_whitespace,
5378 s->force_text_diff, &dsa, s->f);
5382 err = got_object_open_as_commit(&commit2, s->repo,
5387 parent_ids = got_object_commit_get_parent_ids(commit2);
5388 STAILQ_FOREACH(pid, parent_ids, entry) {
5389 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5390 err = write_commit_info(&s->lines,
5391 &s->nlines, s->id2, refs, s->repo,
5392 s->ignore_whitespace,
5393 s->force_text_diff, &dsa, s->f);
5401 err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5406 err = got_error(GOT_ERR_OBJ_TYPE);
5411 if (commit2 != NULL)
5412 got_object_commit_close(commit2);
5413 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5414 if (s->f && fflush(s->f) != 0 && err == NULL)
5415 err = got_error_from_errno("fflush");
5416 if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5417 err = got_error_from_errno("fclose");
5422 diff_view_indicate_progress(struct tog_view *view)
5424 mvwaddstr(view->window, 0, 0, "diffing...");
5429 static const struct got_error *
5430 search_start_diff_view(struct tog_view *view)
5432 struct tog_diff_view_state *s = &view->state.diff;
5434 s->matched_line = 0;
5439 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5440 size_t *nlines, int **first, int **last, int **match, int **selected)
5442 struct tog_diff_view_state *s = &view->state.diff;
5445 *nlines = s->nlines;
5446 *line_offsets = NULL;
5447 *match = &s->matched_line;
5448 *first = &s->first_displayed_line;
5449 *last = &s->last_displayed_line;
5450 *selected = &s->selected_line;
5453 static const struct got_error *
5454 search_next_view_match(struct tog_view *view)
5456 const struct got_error *err = NULL;
5460 size_t linesize = 0;
5462 off_t *line_offsets;
5464 int *first, *last, *match, *selected;
5466 if (!view->search_setup)
5467 return got_error_msg(GOT_ERR_NOT_IMPL,
5468 "view search not supported");
5469 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5472 if (!view->searching) {
5473 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5478 if (view->searching == TOG_SEARCH_FORWARD)
5479 lineno = *first + 1;
5481 lineno = *first - 1;
5483 lineno = *first - 1 + *selected;
5488 if (lineno <= 0 || lineno > nlines) {
5490 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5494 if (view->searching == TOG_SEARCH_FORWARD)
5500 offset = view->type == TOG_VIEW_DIFF ?
5501 view->state.diff.lines[lineno - 1].offset :
5502 line_offsets[lineno - 1];
5503 if (fseeko(f, offset, SEEK_SET) != 0) {
5505 return got_error_from_errno("fseeko");
5507 linelen = getline(&line, &linesize, f);
5508 if (linelen != -1) {
5510 err = expand_tab(&exstr, line);
5513 if (match_line(exstr, &view->regex, 1,
5515 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5522 if (view->searching == TOG_SEARCH_FORWARD)
5537 static const struct got_error *
5538 close_diff_view(struct tog_view *view)
5540 const struct got_error *err = NULL;
5541 struct tog_diff_view_state *s = &view->state.diff;
5547 if (s->f && fclose(s->f) == EOF)
5548 err = got_error_from_errno("fclose");
5550 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5551 err = got_error_from_errno("fclose");
5553 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5554 err = got_error_from_errno("fclose");
5556 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5557 err = got_error_from_errno("close");
5559 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5560 err = got_error_from_errno("close");
5568 static const struct got_error *
5569 open_diff_view(struct tog_view *view, struct got_object_id *id1,
5570 struct got_object_id *id2, const char *label1, const char *label2,
5571 int diff_context, int ignore_whitespace, int force_text_diff,
5572 struct tog_view *parent_view, struct got_repository *repo)
5574 const struct got_error *err;
5575 struct tog_diff_view_state *s = &view->state.diff;
5577 memset(s, 0, sizeof(*s));
5581 if (id1 != NULL && id2 != NULL) {
5584 err = got_object_get_type(&type1, repo, id1);
5587 err = got_object_get_type(&type2, repo, id2);
5591 if (type1 != type2) {
5592 err = got_error(GOT_ERR_OBJ_TYPE);
5596 s->first_displayed_line = 1;
5597 s->last_displayed_line = view->nlines;
5598 s->selected_line = 1;
5604 s->id1 = got_object_id_dup(id1);
5605 if (s->id1 == NULL) {
5606 err = got_error_from_errno("got_object_id_dup");
5612 s->id2 = got_object_id_dup(id2);
5613 if (s->id2 == NULL) {
5614 err = got_error_from_errno("got_object_id_dup");
5618 s->f1 = got_opentemp();
5619 if (s->f1 == NULL) {
5620 err = got_error_from_errno("got_opentemp");
5624 s->f2 = got_opentemp();
5625 if (s->f2 == NULL) {
5626 err = got_error_from_errno("got_opentemp");
5630 s->fd1 = got_opentempfd();
5632 err = got_error_from_errno("got_opentempfd");
5636 s->fd2 = got_opentempfd();
5638 err = got_error_from_errno("got_opentempfd");
5642 s->diff_context = diff_context;
5643 s->ignore_whitespace = ignore_whitespace;
5644 s->force_text_diff = force_text_diff;
5645 s->parent_view = parent_view;
5648 if (has_colors() && getenv("TOG_COLORS") != NULL && !using_mock_io) {
5651 rc = init_pair(GOT_DIFF_LINE_MINUS,
5652 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5654 rc = init_pair(GOT_DIFF_LINE_PLUS,
5655 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5657 rc = init_pair(GOT_DIFF_LINE_HUNK,
5658 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5660 rc = init_pair(GOT_DIFF_LINE_META,
5661 get_color_value("TOG_COLOR_DIFF_META"), -1);
5663 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5664 get_color_value("TOG_COLOR_DIFF_META"), -1);
5666 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5667 get_color_value("TOG_COLOR_DIFF_META"), -1);
5669 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5670 get_color_value("TOG_COLOR_DIFF_META"), -1);
5672 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5673 get_color_value("TOG_COLOR_AUTHOR"), -1);
5675 rc = init_pair(GOT_DIFF_LINE_DATE,
5676 get_color_value("TOG_COLOR_DATE"), -1);
5678 err = got_error(GOT_ERR_RANGE);
5683 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5684 view_is_splitscreen(view))
5685 show_log_view(parent_view); /* draw border */
5686 diff_view_indicate_progress(view);
5688 err = create_diff(s);
5690 view->show = show_diff_view;
5691 view->input = input_diff_view;
5692 view->reset = reset_diff_view;
5693 view->close = close_diff_view;
5694 view->search_start = search_start_diff_view;
5695 view->search_setup = search_setup_diff_view;
5696 view->search_next = search_next_view_match;
5699 if (view->close == NULL)
5700 close_diff_view(view);
5706 static const struct got_error *
5707 show_diff_view(struct tog_view *view)
5709 const struct got_error *err;
5710 struct tog_diff_view_state *s = &view->state.diff;
5711 char *id_str1 = NULL, *id_str2, *header;
5712 const char *label1, *label2;
5715 err = got_object_id_str(&id_str1, s->id1);
5718 label1 = s->label1 ? s->label1 : id_str1;
5720 label1 = "/dev/null";
5722 err = got_object_id_str(&id_str2, s->id2);
5725 label2 = s->label2 ? s->label2 : id_str2;
5727 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5728 err = got_error_from_errno("asprintf");
5736 err = draw_file(view, header);
5741 static const struct got_error *
5742 set_selected_commit(struct tog_diff_view_state *s,
5743 struct commit_queue_entry *entry)
5745 const struct got_error *err;
5746 const struct got_object_id_queue *parent_ids;
5747 struct got_commit_object *selected_commit;
5748 struct got_object_qid *pid;
5751 s->id2 = got_object_id_dup(entry->id);
5753 return got_error_from_errno("got_object_id_dup");
5755 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5758 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5760 pid = STAILQ_FIRST(parent_ids);
5761 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5762 got_object_commit_close(selected_commit);
5766 static const struct got_error *
5767 reset_diff_view(struct tog_view *view)
5769 struct tog_diff_view_state *s = &view->state.diff;
5772 wclear(view->window);
5773 s->first_displayed_line = 1;
5774 s->last_displayed_line = view->nlines;
5775 s->matched_line = 0;
5776 diff_view_indicate_progress(view);
5777 return create_diff(s);
5781 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5785 i = start = s->first_displayed_line - 1;
5787 while (s->lines[i].type != type) {
5791 return; /* do nothing, requested type not in file */
5794 s->selected_line = 1;
5795 s->first_displayed_line = i;
5799 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5803 i = start = s->first_displayed_line + 1;
5805 while (s->lines[i].type != type) {
5806 if (i == s->nlines - 1)
5809 return; /* do nothing, requested type not in file */
5812 s->selected_line = 1;
5813 s->first_displayed_line = i;
5816 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5818 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5821 static const struct got_error *
5822 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5824 const struct got_error *err = NULL;
5825 struct tog_diff_view_state *s = &view->state.diff;
5826 struct tog_log_view_state *ls;
5827 struct commit_queue_entry *old_selected_entry;
5829 size_t linesize = 0;
5831 int i, nscroll = view->nlines - 1, up = 0;
5833 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5842 horizontal_scroll_input(view, ch);
5847 s->force_text_diff = !s->force_text_diff;
5848 view->action = s->force_text_diff ?
5849 "force ASCII text enabled" :
5850 "force ASCII text disabled";
5852 else if (ch == 'w') {
5853 s->ignore_whitespace = !s->ignore_whitespace;
5854 view->action = s->ignore_whitespace ?
5855 "ignore whitespace enabled" :
5856 "ignore whitespace disabled";
5858 err = reset_diff_view(view);
5862 s->first_displayed_line = 1;
5871 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5877 if (s->first_displayed_line > 1)
5878 s->first_displayed_line--;
5889 if (s->first_displayed_line == 1) {
5894 while (i++ < nscroll && s->first_displayed_line > 1)
5895 s->first_displayed_line--;
5901 s->first_displayed_line++;
5918 while (!s->eof && i++ < nscroll) {
5919 linelen = getline(&line, &linesize, s->f);
5920 s->first_displayed_line++;
5921 if (linelen == -1) {
5925 err = got_ferror(s->f, GOT_ERR_IO);
5932 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5935 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5938 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5941 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5944 if (s->diff_context > 0) {
5946 s->matched_line = 0;
5947 diff_view_indicate_progress(view);
5948 err = create_diff(s);
5949 if (s->first_displayed_line + view->nlines - 1 >
5951 s->first_displayed_line = 1;
5952 s->last_displayed_line = view->nlines;
5958 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5960 s->matched_line = 0;
5961 diff_view_indicate_progress(view);
5962 err = create_diff(s);
5974 if (s->parent_view == NULL) {
5978 s->parent_view->count = view->count;
5980 if (s->parent_view->type == TOG_VIEW_LOG) {
5981 ls = &s->parent_view->state.log;
5982 old_selected_entry = ls->selected_entry;
5984 err = input_log_view(NULL, s->parent_view,
5985 up ? KEY_UP : KEY_DOWN);
5988 view->count = s->parent_view->count;
5990 if (old_selected_entry == ls->selected_entry)
5993 err = set_selected_commit(s, ls->selected_entry);
5996 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5997 struct tog_blame_view_state *bs;
5998 struct got_object_id *id, *prev_id;
6000 bs = &s->parent_view->state.blame;
6001 prev_id = get_annotation_for_line(bs->blame.lines,
6002 bs->blame.nlines, bs->last_diffed_line);
6004 err = input_blame_view(&view, s->parent_view,
6005 up ? KEY_UP : KEY_DOWN);
6008 view->count = s->parent_view->count;
6010 if (prev_id == NULL)
6012 id = get_selected_commit_id(bs->blame.lines,
6013 bs->blame.nlines, bs->first_displayed_line,
6018 if (!got_object_id_cmp(prev_id, id))
6021 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
6025 s->first_displayed_line = 1;
6026 s->last_displayed_line = view->nlines;
6027 s->matched_line = 0;
6030 diff_view_indicate_progress(view);
6031 err = create_diff(s);
6041 static const struct got_error *
6042 cmd_diff(int argc, char *argv[])
6044 const struct got_error *error;
6045 struct got_repository *repo = NULL;
6046 struct got_worktree *worktree = NULL;
6047 struct got_object_id *id1 = NULL, *id2 = NULL;
6048 char *repo_path = NULL, *cwd = NULL;
6049 char *id_str1 = NULL, *id_str2 = NULL;
6050 char *keyword_idstr1 = NULL, *keyword_idstr2 = NULL;
6051 char *label1 = NULL, *label2 = NULL;
6052 int diff_context = 3, ignore_whitespace = 0;
6053 int ch, force_text_diff = 0;
6055 struct tog_view *view;
6056 int *pack_fds = NULL;
6058 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
6061 force_text_diff = 1;
6064 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
6067 errx(1, "number of context lines is %s: %s",
6071 repo_path = realpath(optarg, NULL);
6072 if (repo_path == NULL)
6073 return got_error_from_errno2("realpath",
6075 got_path_strip_trailing_slashes(repo_path);
6078 ignore_whitespace = 1;
6090 usage_diff(); /* TODO show local worktree changes */
6091 } else if (argc == 2) {
6097 error = got_repo_pack_fds_open(&pack_fds);
6101 if (repo_path == NULL) {
6102 cwd = getcwd(NULL, 0);
6104 return got_error_from_errno("getcwd");
6105 error = got_worktree_open(&worktree, cwd, NULL);
6106 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6110 strdup(got_worktree_get_repo_path(worktree));
6112 repo_path = strdup(cwd);
6113 if (repo_path == NULL) {
6114 error = got_error_from_errno("strdup");
6119 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6125 error = apply_unveil(got_repo_get_path(repo), NULL);
6129 error = tog_load_refs(repo, 0);
6133 if (id_str1 != NULL) {
6134 error = got_keyword_to_idstr(&keyword_idstr1, id_str1,
6138 if (keyword_idstr1 != NULL)
6139 id_str1 = keyword_idstr1;
6141 if (id_str2 != NULL) {
6142 error = got_keyword_to_idstr(&keyword_idstr2, id_str2,
6146 if (keyword_idstr2 != NULL)
6147 id_str2 = keyword_idstr2;
6150 error = got_repo_match_object_id(&id1, &label1, id_str1,
6151 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
6155 error = got_repo_match_object_id(&id2, &label2, id_str2,
6156 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
6160 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
6162 error = got_error_from_errno("view_open");
6165 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
6166 ignore_whitespace, force_text_diff, NULL, repo);
6171 error = set_tog_base_commit(repo, worktree);
6175 /* Release work tree lock. */
6176 got_worktree_close(worktree);
6180 error = view_loop(view);
6183 free(tog_base_commit.id);
6184 free(keyword_idstr1);
6185 free(keyword_idstr2);
6193 const struct got_error *close_err = got_repo_close(repo);
6198 got_worktree_close(worktree);
6200 const struct got_error *pack_err =
6201 got_repo_pack_fds_close(pack_fds);
6214 "usage: %s blame [-c commit] [-r repository-path] path\n",
6219 struct tog_blame_line {
6221 struct got_object_id *id;
6224 static const struct got_error *
6225 draw_blame(struct tog_view *view)
6227 struct tog_blame_view_state *s = &view->state.blame;
6228 struct tog_blame *blame = &s->blame;
6229 regmatch_t *regmatch = &view->regmatch;
6230 const struct got_error *err;
6231 int lineno = 0, nprinted = 0;
6233 size_t linesize = 0;
6237 struct tog_blame_line *blame_line;
6238 struct got_object_id *prev_id = NULL;
6240 struct tog_color *tc;
6242 err = got_object_id_str(&id_str, &s->blamed_commit->id);
6247 werase(view->window);
6249 if (asprintf(&line, "commit %s", id_str) == -1) {
6250 err = got_error_from_errno("asprintf");
6255 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6260 if (view_needs_focus_indication(view))
6261 wstandout(view->window);
6262 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6264 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6265 waddwstr(view->window, wline);
6266 while (width++ < view->ncols)
6267 waddch(view->window, ' ');
6269 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6270 if (view_needs_focus_indication(view))
6271 wstandend(view->window);
6275 if (view->gline > blame->nlines)
6276 view->gline = blame->nlines;
6278 if (tog_io.wait_for_ui) {
6279 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6282 rc = pthread_cond_wait(&bta->blame_complete, &tog_mutex);
6284 return got_error_set_errno(rc, "pthread_cond_wait");
6285 tog_io.wait_for_ui = 0;
6288 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
6289 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
6290 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
6292 return got_error_from_errno("asprintf");
6295 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6300 waddwstr(view->window, wline);
6303 if (width < view->ncols - 1)
6304 waddch(view->window, '\n');
6308 while (nprinted < view->nlines - 2) {
6309 linelen = getline(&line, &linesize, blame->f);
6310 if (linelen == -1) {
6311 if (feof(blame->f)) {
6316 return got_ferror(blame->f, GOT_ERR_IO);
6318 if (++lineno < s->first_displayed_line)
6320 if (view->gline && !gotoline(view, &lineno, &nprinted))
6323 /* Set view->maxx based on full line length. */
6324 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6331 view->maxx = MAX(view->maxx, width);
6333 if (nprinted == s->selected_line - 1)
6334 wstandout(view->window);
6336 if (blame->nlines > 0) {
6337 blame_line = &blame->lines[lineno - 1];
6338 if (blame_line->annotated && prev_id &&
6339 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6340 !(nprinted == s->selected_line - 1)) {
6341 waddstr(view->window, " ");
6342 } else if (blame_line->annotated) {
6344 err = got_object_id_str(&id_str,
6350 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6352 wattr_on(view->window,
6353 COLOR_PAIR(tc->colorpair), NULL);
6354 wprintw(view->window, "%.8s", id_str);
6356 wattr_off(view->window,
6357 COLOR_PAIR(tc->colorpair), NULL);
6359 prev_id = blame_line->id;
6361 waddstr(view->window, "........");
6365 waddstr(view->window, "........");
6369 if (nprinted == s->selected_line - 1)
6370 wstandend(view->window);
6371 waddstr(view->window, " ");
6373 if (view->ncols <= 9) {
6375 } else if (s->first_displayed_line + nprinted ==
6377 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6378 err = add_matched_line(&width, line, view->ncols - 9, 9,
6379 view->window, view->x, regmatch);
6387 err = format_line(&wline, &width, &skip, line,
6388 view->x, view->ncols - 9, 9, 1);
6393 waddwstr(view->window, &wline[skip]);
6399 if (width <= view->ncols - 1)
6400 waddch(view->window, '\n');
6401 if (++nprinted == 1)
6402 s->first_displayed_line = lineno;
6405 s->last_displayed_line = lineno;
6412 static const struct got_error *
6413 blame_cb(void *arg, int nlines, int lineno,
6414 struct got_commit_object *commit, struct got_object_id *id)
6416 const struct got_error *err = NULL;
6417 struct tog_blame_cb_args *a = arg;
6418 struct tog_blame_line *line;
6421 if (nlines != a->nlines ||
6422 (lineno != -1 && lineno < 1) || lineno > a->nlines)
6423 return got_error(GOT_ERR_RANGE);
6425 errcode = pthread_mutex_lock(&tog_mutex);
6427 return got_error_set_errno(errcode, "pthread_mutex_lock");
6429 if (*a->quit) { /* user has quit the blame view */
6430 err = got_error(GOT_ERR_ITER_COMPLETED);
6435 goto done; /* no change in this commit */
6437 line = &a->lines[lineno - 1];
6438 if (line->annotated)
6441 line->id = got_object_id_dup(id);
6442 if (line->id == NULL) {
6443 err = got_error_from_errno("got_object_id_dup");
6446 line->annotated = 1;
6448 errcode = pthread_mutex_unlock(&tog_mutex);
6450 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6455 blame_thread(void *arg)
6457 const struct got_error *err, *close_err;
6458 struct tog_blame_thread_args *ta = arg;
6459 struct tog_blame_cb_args *a = ta->cb_args;
6460 int errcode, fd1 = -1, fd2 = -1;
6461 FILE *f1 = NULL, *f2 = NULL;
6463 fd1 = got_opentempfd();
6465 return (void *)got_error_from_errno("got_opentempfd");
6467 fd2 = got_opentempfd();
6469 err = got_error_from_errno("got_opentempfd");
6473 f1 = got_opentemp();
6475 err = (void *)got_error_from_errno("got_opentemp");
6478 f2 = got_opentemp();
6480 err = (void *)got_error_from_errno("got_opentemp");
6484 err = block_signals_used_by_main_thread();
6488 err = got_blame(ta->path, a->commit_id, ta->repo,
6489 tog_diff_algo, blame_cb, ta->cb_args,
6490 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6491 if (err && err->code == GOT_ERR_CANCELLED)
6494 errcode = pthread_mutex_lock(&tog_mutex);
6496 err = got_error_set_errno(errcode, "pthread_mutex_lock");
6500 close_err = got_repo_close(ta->repo);
6506 if (tog_io.wait_for_ui) {
6507 errcode = pthread_cond_signal(&ta->blame_complete);
6508 if (errcode && err == NULL)
6509 err = got_error_set_errno(errcode,
6510 "pthread_cond_signal");
6513 errcode = pthread_mutex_unlock(&tog_mutex);
6514 if (errcode && err == NULL)
6515 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6518 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6519 err = got_error_from_errno("close");
6520 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6521 err = got_error_from_errno("close");
6522 if (f1 && fclose(f1) == EOF && err == NULL)
6523 err = got_error_from_errno("fclose");
6524 if (f2 && fclose(f2) == EOF && err == NULL)
6525 err = got_error_from_errno("fclose");
6530 static struct got_object_id *
6531 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6532 int first_displayed_line, int selected_line)
6534 struct tog_blame_line *line;
6539 line = &lines[first_displayed_line - 1 + selected_line - 1];
6540 if (!line->annotated)
6546 static struct got_object_id *
6547 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6550 struct tog_blame_line *line;
6552 if (nlines <= 0 || lineno >= nlines)
6555 line = &lines[lineno - 1];
6556 if (!line->annotated)
6562 static const struct got_error *
6563 stop_blame(struct tog_blame *blame)
6565 const struct got_error *err = NULL;
6568 if (blame->thread) {
6570 errcode = pthread_mutex_unlock(&tog_mutex);
6572 return got_error_set_errno(errcode,
6573 "pthread_mutex_unlock");
6574 errcode = pthread_join(blame->thread, (void **)&err);
6576 return got_error_set_errno(errcode, "pthread_join");
6577 errcode = pthread_mutex_lock(&tog_mutex);
6579 return got_error_set_errno(errcode,
6580 "pthread_mutex_lock");
6581 if (err && err->code == GOT_ERR_ITER_COMPLETED)
6583 blame->thread = NULL;
6585 if (blame->thread_args.repo) {
6586 const struct got_error *close_err;
6587 close_err = got_repo_close(blame->thread_args.repo);
6590 blame->thread_args.repo = NULL;
6593 if (fclose(blame->f) == EOF && err == NULL)
6594 err = got_error_from_errno("fclose");
6598 for (i = 0; i < blame->nlines; i++)
6599 free(blame->lines[i].id);
6601 blame->lines = NULL;
6603 free(blame->cb_args.commit_id);
6604 blame->cb_args.commit_id = NULL;
6605 if (blame->pack_fds) {
6606 const struct got_error *pack_err =
6607 got_repo_pack_fds_close(blame->pack_fds);
6610 blame->pack_fds = NULL;
6612 free(blame->line_offsets);
6613 blame->line_offsets = NULL;
6617 static const struct got_error *
6618 cancel_blame_view(void *arg)
6620 const struct got_error *err = NULL;
6624 errcode = pthread_mutex_lock(&tog_mutex);
6626 return got_error_set_errno(errcode,
6627 "pthread_mutex_unlock");
6630 err = got_error(GOT_ERR_CANCELLED);
6632 errcode = pthread_mutex_unlock(&tog_mutex);
6634 return got_error_set_errno(errcode,
6635 "pthread_mutex_lock");
6640 static const struct got_error *
6641 run_blame(struct tog_view *view)
6643 struct tog_blame_view_state *s = &view->state.blame;
6644 struct tog_blame *blame = &s->blame;
6645 const struct got_error *err = NULL;
6646 struct got_commit_object *commit = NULL;
6647 struct got_blob_object *blob = NULL;
6648 struct got_repository *thread_repo = NULL;
6649 struct got_object_id *obj_id = NULL;
6650 int obj_type, fd = -1;
6651 int *pack_fds = NULL;
6653 err = got_object_open_as_commit(&commit, s->repo,
6654 &s->blamed_commit->id);
6658 fd = got_opentempfd();
6660 err = got_error_from_errno("got_opentempfd");
6664 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6668 err = got_object_get_type(&obj_type, s->repo, obj_id);
6672 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6673 err = got_error(GOT_ERR_OBJ_TYPE);
6677 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6680 blame->f = got_opentemp();
6681 if (blame->f == NULL) {
6682 err = got_error_from_errno("got_opentemp");
6685 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6686 &blame->line_offsets, blame->f, blob);
6689 if (blame->nlines == 0) {
6690 s->blame_complete = 1;
6694 /* Don't include \n at EOF in the blame line count. */
6695 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6698 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6699 if (blame->lines == NULL) {
6700 err = got_error_from_errno("calloc");
6704 err = got_repo_pack_fds_open(&pack_fds);
6707 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6712 blame->pack_fds = pack_fds;
6713 blame->cb_args.view = view;
6714 blame->cb_args.lines = blame->lines;
6715 blame->cb_args.nlines = blame->nlines;
6716 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6717 if (blame->cb_args.commit_id == NULL) {
6718 err = got_error_from_errno("got_object_id_dup");
6721 blame->cb_args.quit = &s->done;
6723 blame->thread_args.path = s->path;
6724 blame->thread_args.repo = thread_repo;
6725 blame->thread_args.cb_args = &blame->cb_args;
6726 blame->thread_args.complete = &s->blame_complete;
6727 blame->thread_args.cancel_cb = cancel_blame_view;
6728 blame->thread_args.cancel_arg = &s->done;
6729 s->blame_complete = 0;
6731 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6732 s->first_displayed_line = 1;
6733 s->last_displayed_line = view->nlines;
6734 s->selected_line = 1;
6736 s->matched_line = 0;
6740 got_object_commit_close(commit);
6741 if (fd != -1 && close(fd) == -1 && err == NULL)
6742 err = got_error_from_errno("close");
6744 got_object_blob_close(blob);
6751 static const struct got_error *
6752 open_blame_view(struct tog_view *view, char *path,
6753 struct got_object_id *commit_id, struct got_repository *repo)
6755 const struct got_error *err = NULL;
6756 struct tog_blame_view_state *s = &view->state.blame;
6758 STAILQ_INIT(&s->blamed_commits);
6760 s->path = strdup(path);
6761 if (s->path == NULL)
6762 return got_error_from_errno("strdup");
6764 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6770 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6771 s->first_displayed_line = 1;
6772 s->last_displayed_line = view->nlines;
6773 s->selected_line = 1;
6774 s->blame_complete = 0;
6776 s->commit_id = commit_id;
6777 memset(&s->blame, 0, sizeof(s->blame));
6779 STAILQ_INIT(&s->colors);
6780 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6781 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6782 get_color_value("TOG_COLOR_COMMIT"));
6787 view->show = show_blame_view;
6788 view->input = input_blame_view;
6789 view->reset = reset_blame_view;
6790 view->close = close_blame_view;
6791 view->search_start = search_start_blame_view;
6792 view->search_setup = search_setup_blame_view;
6793 view->search_next = search_next_view_match;
6795 if (using_mock_io) {
6796 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6799 rc = pthread_cond_init(&bta->blame_complete, NULL);
6801 return got_error_set_errno(rc, "pthread_cond_init");
6804 return run_blame(view);
6807 static const struct got_error *
6808 close_blame_view(struct tog_view *view)
6810 const struct got_error *err = NULL;
6811 struct tog_blame_view_state *s = &view->state.blame;
6813 if (s->blame.thread)
6814 err = stop_blame(&s->blame);
6816 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6817 struct got_object_qid *blamed_commit;
6818 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6819 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6820 got_object_qid_free(blamed_commit);
6823 if (using_mock_io) {
6824 struct tog_blame_thread_args *bta = &s->blame.thread_args;
6827 rc = pthread_cond_destroy(&bta->blame_complete);
6828 if (rc && err == NULL)
6829 err = got_error_set_errno(rc, "pthread_cond_destroy");
6833 free_colors(&s->colors);
6837 static const struct got_error *
6838 search_start_blame_view(struct tog_view *view)
6840 struct tog_blame_view_state *s = &view->state.blame;
6842 s->matched_line = 0;
6847 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6848 size_t *nlines, int **first, int **last, int **match, int **selected)
6850 struct tog_blame_view_state *s = &view->state.blame;
6853 *nlines = s->blame.nlines;
6854 *line_offsets = s->blame.line_offsets;
6855 *match = &s->matched_line;
6856 *first = &s->first_displayed_line;
6857 *last = &s->last_displayed_line;
6858 *selected = &s->selected_line;
6861 static const struct got_error *
6862 show_blame_view(struct tog_view *view)
6864 const struct got_error *err = NULL;
6865 struct tog_blame_view_state *s = &view->state.blame;
6868 if (s->blame.thread == NULL && !s->blame_complete) {
6869 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6870 &s->blame.thread_args);
6872 return got_error_set_errno(errcode, "pthread_create");
6875 halfdelay(1); /* fast refresh while annotating */
6878 if (s->blame_complete && !using_mock_io)
6879 halfdelay(10); /* disable fast refresh */
6881 err = draw_blame(view);
6887 static const struct got_error *
6888 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6889 struct got_repository *repo, struct got_object_id *id)
6891 struct tog_view *log_view;
6892 const struct got_error *err = NULL;
6896 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6897 if (log_view == NULL)
6898 return got_error_from_errno("view_open");
6900 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0, NULL);
6902 view_close(log_view);
6904 *new_view = log_view;
6909 static const struct got_error *
6910 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6912 const struct got_error *err = NULL, *thread_err = NULL;
6913 struct tog_view *diff_view;
6914 struct tog_blame_view_state *s = &view->state.blame;
6915 int eos, nscroll, begin_y = 0, begin_x = 0;
6917 eos = nscroll = view->nlines - 2;
6918 if (view_is_hsplit_top(view))
6928 horizontal_scroll_input(view, ch);
6935 s->selected_line = 1;
6936 s->first_displayed_line = 1;
6941 if (s->blame.nlines < eos) {
6942 s->selected_line = s->blame.nlines;
6943 s->first_displayed_line = 1;
6945 s->selected_line = eos;
6946 s->first_displayed_line = s->blame.nlines - (eos - 1);
6953 if (s->selected_line > 1)
6955 else if (s->selected_line == 1 &&
6956 s->first_displayed_line > 1)
6957 s->first_displayed_line--;
6968 if (s->first_displayed_line == 1) {
6969 if (view->count > 1)
6971 s->selected_line = MAX(1, s->selected_line - nscroll);
6975 if (s->first_displayed_line > nscroll)
6976 s->first_displayed_line -= nscroll;
6978 s->first_displayed_line = 1;
6983 if (s->selected_line < eos && s->first_displayed_line +
6984 s->selected_line <= s->blame.nlines)
6986 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6987 s->first_displayed_line++;
6993 struct got_object_id *id = NULL;
6996 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6997 s->first_displayed_line, s->selected_line);
7001 struct got_commit_object *commit, *pcommit;
7002 struct got_object_qid *pid;
7003 struct got_object_id *blob_id = NULL;
7005 err = got_object_open_as_commit(&commit,
7010 got_object_commit_get_parent_ids(commit));
7012 got_object_commit_close(commit);
7015 /* Check if path history ends here. */
7016 err = got_object_open_as_commit(&pcommit,
7020 err = got_object_id_by_path(&blob_id, s->repo,
7022 got_object_commit_close(pcommit);
7024 if (err->code == GOT_ERR_NO_TREE_ENTRY)
7026 got_object_commit_close(commit);
7029 err = got_object_get_type(&obj_type, s->repo,
7032 /* Can't blame non-blob type objects. */
7033 if (obj_type != GOT_OBJ_TYPE_BLOB) {
7034 got_object_commit_close(commit);
7037 err = got_object_qid_alloc(&s->blamed_commit,
7039 got_object_commit_close(commit);
7041 if (got_object_id_cmp(id,
7042 &s->blamed_commit->id) == 0)
7044 err = got_object_qid_alloc(&s->blamed_commit,
7050 thread_err = stop_blame(&s->blame);
7054 STAILQ_INSERT_HEAD(&s->blamed_commits,
7055 s->blamed_commit, entry);
7056 err = run_blame(view);
7062 struct got_object_qid *first;
7065 first = STAILQ_FIRST(&s->blamed_commits);
7066 if (!got_object_id_cmp(&first->id, s->commit_id))
7069 thread_err = stop_blame(&s->blame);
7073 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
7074 got_object_qid_free(s->blamed_commit);
7076 STAILQ_FIRST(&s->blamed_commits);
7077 err = run_blame(view);
7084 s->id_to_log = get_selected_commit_id(s->blame.lines,
7085 s->blame.nlines, s->first_displayed_line, s->selected_line);
7087 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7091 struct got_object_id *id = NULL;
7092 struct got_object_qid *pid;
7093 struct got_commit_object *commit = NULL;
7096 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
7097 s->first_displayed_line, s->selected_line);
7100 err = got_object_open_as_commit(&commit, s->repo, id);
7103 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7105 /* traversed from diff view, release diff resources */
7106 err = close_diff_view(*new_view);
7109 diff_view = *new_view;
7111 if (view_is_parent_view(view))
7112 view_get_split(view, &begin_y, &begin_x);
7114 diff_view = view_open(0, 0, begin_y, begin_x,
7116 if (diff_view == NULL) {
7117 got_object_commit_close(commit);
7118 err = got_error_from_errno("view_open");
7122 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
7123 id, NULL, NULL, 3, 0, 0, view, s->repo);
7124 got_object_commit_close(commit);
7127 s->last_diffed_line = s->first_displayed_line - 1 +
7130 break; /* still open from active diff view */
7131 if (view_is_parent_view(view) &&
7132 view->mode == TOG_VIEW_SPLIT_HRZN) {
7133 err = view_init_hsplit(view, begin_y);
7139 diff_view->focussed = 1;
7140 diff_view->mode = view->mode;
7141 diff_view->nlines = view->lines - begin_y;
7142 if (view_is_parent_view(view)) {
7143 view_transfer_size(diff_view, view);
7144 err = view_close_child(view);
7147 err = view_set_child(view, diff_view);
7150 view->focus_child = 1;
7152 *new_view = diff_view;
7165 if (s->last_displayed_line >= s->blame.nlines &&
7166 s->selected_line >= MIN(s->blame.nlines,
7167 view->nlines - 2)) {
7171 if (s->last_displayed_line >= s->blame.nlines &&
7172 s->selected_line < view->nlines - 2) {
7174 MIN(nscroll, s->last_displayed_line -
7175 s->first_displayed_line - s->selected_line + 1);
7177 if (s->last_displayed_line + nscroll <= s->blame.nlines)
7178 s->first_displayed_line += nscroll;
7180 s->first_displayed_line =
7181 s->blame.nlines - (view->nlines - 3);
7184 if (s->selected_line > view->nlines - 2) {
7185 s->selected_line = MIN(s->blame.nlines,
7193 return thread_err ? thread_err : err;
7196 static const struct got_error *
7197 reset_blame_view(struct tog_view *view)
7199 const struct got_error *err;
7200 struct tog_blame_view_state *s = &view->state.blame;
7204 err = stop_blame(&s->blame);
7208 return run_blame(view);
7211 static const struct got_error *
7212 cmd_blame(int argc, char *argv[])
7214 const struct got_error *error;
7215 struct got_repository *repo = NULL;
7216 struct got_worktree *worktree = NULL;
7217 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7218 char *link_target = NULL;
7219 struct got_object_id *commit_id = NULL;
7220 struct got_commit_object *commit = NULL;
7221 char *keyword_idstr = NULL, *commit_id_str = NULL;
7223 struct tog_view *view = NULL;
7224 int *pack_fds = NULL;
7226 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7229 commit_id_str = optarg;
7232 repo_path = realpath(optarg, NULL);
7233 if (repo_path == NULL)
7234 return got_error_from_errno2("realpath",
7249 error = got_repo_pack_fds_open(&pack_fds);
7253 if (repo_path == NULL) {
7254 cwd = getcwd(NULL, 0);
7256 return got_error_from_errno("getcwd");
7257 error = got_worktree_open(&worktree, cwd, NULL);
7258 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7262 strdup(got_worktree_get_repo_path(worktree));
7264 repo_path = strdup(cwd);
7265 if (repo_path == NULL) {
7266 error = got_error_from_errno("strdup");
7271 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7275 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
7282 error = apply_unveil(got_repo_get_path(repo), NULL);
7286 error = tog_load_refs(repo, 0);
7290 if (commit_id_str == NULL) {
7291 struct got_reference *head_ref;
7292 error = got_ref_open(&head_ref, repo, worktree ?
7293 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
7296 error = got_ref_resolve(&commit_id, repo, head_ref);
7297 got_ref_close(head_ref);
7299 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
7303 if (keyword_idstr != NULL)
7304 commit_id_str = keyword_idstr;
7306 error = got_repo_match_object_id(&commit_id, NULL,
7307 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7312 error = got_object_open_as_commit(&commit, repo, commit_id);
7316 error = got_object_resolve_symlinks(&link_target, in_repo_path,
7321 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
7323 error = got_error_from_errno("view_open");
7326 error = open_blame_view(view, link_target ? link_target : in_repo_path,
7328 if (error != NULL) {
7329 if (view->close == NULL)
7330 close_blame_view(view);
7336 error = set_tog_base_commit(repo, worktree);
7340 /* Release work tree lock. */
7341 got_worktree_close(worktree);
7345 error = view_loop(view);
7348 free(tog_base_commit.id);
7354 free(keyword_idstr);
7356 got_object_commit_close(commit);
7358 got_worktree_close(worktree);
7360 const struct got_error *close_err = got_repo_close(repo);
7365 const struct got_error *pack_err =
7366 got_repo_pack_fds_close(pack_fds);
7374 static const struct got_error *
7375 draw_tree_entries(struct tog_view *view, const char *parent_path)
7377 struct tog_tree_view_state *s = &view->state.tree;
7378 const struct got_error *err = NULL;
7379 struct got_tree_entry *te;
7382 struct tog_color *tc;
7383 int width, n, nentries, scrollx, i = 1;
7384 int limit = view->nlines;
7387 if (view_is_hsplit_top(view))
7388 --limit; /* border */
7390 werase(view->window);
7395 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7399 if (view_needs_focus_indication(view))
7400 wstandout(view->window);
7401 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7403 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7404 waddwstr(view->window, wline);
7407 while (width++ < view->ncols)
7408 waddch(view->window, ' ');
7410 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7411 if (view_needs_focus_indication(view))
7412 wstandend(view->window);
7417 if (s->first_displayed_entry) {
7418 i += got_tree_entry_get_index(s->first_displayed_entry);
7419 if (s->tree != s->root)
7420 ++i; /* account for ".." entry */
7422 nentries = got_object_tree_get_nentries(s->tree);
7423 if (asprintf(&index, "[%d/%d] %s",
7424 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7425 return got_error_from_errno("asprintf");
7426 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7430 waddwstr(view->window, wline);
7433 if (width < view->ncols - 1)
7434 waddch(view->window, '\n');
7437 waddch(view->window, '\n');
7441 if (s->first_displayed_entry == NULL) {
7442 te = got_object_tree_get_first_entry(s->tree);
7443 if (s->selected == 0) {
7445 wstandout(view->window);
7446 s->selected_entry = NULL;
7448 waddstr(view->window, " ..\n"); /* parent directory */
7449 if (s->selected == 0 && view->focussed)
7450 wstandend(view->window);
7457 te = s->first_displayed_entry;
7461 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7462 char *line = NULL, *id_str = NULL, *link_target = NULL;
7463 const char *modestr = "";
7466 te = got_object_tree_get_entry(s->tree, i);
7467 mode = got_tree_entry_get_mode(te);
7470 err = got_object_id_str(&id_str,
7471 got_tree_entry_get_id(te));
7473 return got_error_from_errno(
7474 "got_object_id_str");
7476 if (got_object_tree_entry_is_submodule(te))
7478 else if (S_ISLNK(mode)) {
7481 err = got_tree_entry_get_symlink_target(&link_target,
7487 for (i = 0; link_target[i] != '\0'; i++) {
7488 if (!isprint((unsigned char)link_target[i]))
7489 link_target[i] = '?';
7493 else if (S_ISDIR(mode))
7495 else if (mode & S_IXUSR)
7497 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7498 got_tree_entry_get_name(te), modestr,
7499 link_target ? " -> ": "",
7500 link_target ? link_target : "") == -1) {
7503 return got_error_from_errno("asprintf");
7508 /* use full line width to determine view->maxx */
7509 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7514 view->maxx = MAX(view->maxx, width);
7518 err = format_line(&wline, &width, &scrollx, line, view->x,
7524 if (n == s->selected) {
7526 wstandout(view->window);
7527 s->selected_entry = te;
7529 tc = match_color(&s->colors, line);
7531 wattr_on(view->window,
7532 COLOR_PAIR(tc->colorpair), NULL);
7533 waddwstr(view->window, &wline[scrollx]);
7535 wattr_off(view->window,
7536 COLOR_PAIR(tc->colorpair), NULL);
7537 if (width < view->ncols)
7538 waddch(view->window, '\n');
7539 if (n == s->selected && view->focussed)
7540 wstandend(view->window);
7546 s->last_displayed_entry = te;
7555 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7557 struct got_tree_entry *te;
7558 int isroot = s->tree == s->root;
7561 if (s->first_displayed_entry == NULL)
7564 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7565 while (i++ < maxscroll) {
7568 s->first_displayed_entry = NULL;
7571 s->first_displayed_entry = te;
7572 te = got_tree_entry_get_prev(s->tree, te);
7576 static const struct got_error *
7577 tree_scroll_down(struct tog_view *view, int maxscroll)
7579 struct tog_tree_view_state *s = &view->state.tree;
7580 struct got_tree_entry *next, *last;
7583 if (s->first_displayed_entry)
7584 next = got_tree_entry_get_next(s->tree,
7585 s->first_displayed_entry);
7587 next = got_object_tree_get_first_entry(s->tree);
7589 last = s->last_displayed_entry;
7590 while (next && n++ < maxscroll) {
7592 s->last_displayed_entry = last;
7593 last = got_tree_entry_get_next(s->tree, last);
7595 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7596 s->first_displayed_entry = next;
7597 next = got_tree_entry_get_next(s->tree, next);
7604 static const struct got_error *
7605 tree_entry_path(char **path, struct tog_parent_trees *parents,
7606 struct got_tree_entry *te)
7608 const struct got_error *err = NULL;
7609 struct tog_parent_tree *pt;
7610 size_t len = 2; /* for leading slash and NUL */
7612 TAILQ_FOREACH(pt, parents, entry)
7613 len += strlen(got_tree_entry_get_name(pt->selected_entry))
7616 len += strlen(got_tree_entry_get_name(te));
7618 *path = calloc(1, len);
7620 return got_error_from_errno("calloc");
7623 pt = TAILQ_LAST(parents, tog_parent_trees);
7625 const char *name = got_tree_entry_get_name(pt->selected_entry);
7626 if (strlcat(*path, name, len) >= len) {
7627 err = got_error(GOT_ERR_NO_SPACE);
7630 if (strlcat(*path, "/", len) >= len) {
7631 err = got_error(GOT_ERR_NO_SPACE);
7634 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7637 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7638 err = got_error(GOT_ERR_NO_SPACE);
7650 static const struct got_error *
7651 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7652 struct got_tree_entry *te, struct tog_parent_trees *parents,
7653 struct got_object_id *commit_id, struct got_repository *repo)
7655 const struct got_error *err = NULL;
7657 struct tog_view *blame_view;
7661 err = tree_entry_path(&path, parents, te);
7665 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7666 if (blame_view == NULL) {
7667 err = got_error_from_errno("view_open");
7671 err = open_blame_view(blame_view, path, commit_id, repo);
7673 if (err->code == GOT_ERR_CANCELLED)
7675 view_close(blame_view);
7677 *new_view = blame_view;
7683 static const struct got_error *
7684 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7685 struct tog_tree_view_state *s)
7687 struct tog_view *log_view;
7688 const struct got_error *err = NULL;
7693 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7694 if (log_view == NULL)
7695 return got_error_from_errno("view_open");
7697 err = tree_entry_path(&path, &s->parents, s->selected_entry);
7701 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7704 view_close(log_view);
7706 *new_view = log_view;
7711 static const struct got_error *
7712 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7713 const char *head_ref_name, struct got_repository *repo)
7715 const struct got_error *err = NULL;
7716 char *commit_id_str = NULL;
7717 struct tog_tree_view_state *s = &view->state.tree;
7718 struct got_commit_object *commit = NULL;
7720 TAILQ_INIT(&s->parents);
7721 STAILQ_INIT(&s->colors);
7723 s->commit_id = got_object_id_dup(commit_id);
7724 if (s->commit_id == NULL) {
7725 err = got_error_from_errno("got_object_id_dup");
7729 err = got_object_open_as_commit(&commit, repo, commit_id);
7734 * The root is opened here and will be closed when the view is closed.
7735 * Any visited subtrees and their path-wise parents are opened and
7738 err = got_object_open_as_tree(&s->root, repo,
7739 got_object_commit_get_tree_id(commit));
7744 err = got_object_id_str(&commit_id_str, commit_id);
7748 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7749 err = got_error_from_errno("asprintf");
7753 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7754 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7755 if (head_ref_name) {
7756 s->head_ref_name = strdup(head_ref_name);
7757 if (s->head_ref_name == NULL) {
7758 err = got_error_from_errno("strdup");
7764 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7765 err = add_color(&s->colors, "\\$$",
7766 TOG_COLOR_TREE_SUBMODULE,
7767 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7770 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7771 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7774 err = add_color(&s->colors, "/$",
7775 TOG_COLOR_TREE_DIRECTORY,
7776 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7780 err = add_color(&s->colors, "\\*$",
7781 TOG_COLOR_TREE_EXECUTABLE,
7782 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7786 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7787 get_color_value("TOG_COLOR_COMMIT"));
7792 view->show = show_tree_view;
7793 view->input = input_tree_view;
7794 view->close = close_tree_view;
7795 view->search_start = search_start_tree_view;
7796 view->search_next = search_next_tree_view;
7798 free(commit_id_str);
7800 got_object_commit_close(commit);
7802 if (view->close == NULL)
7803 close_tree_view(view);
7809 static const struct got_error *
7810 close_tree_view(struct tog_view *view)
7812 struct tog_tree_view_state *s = &view->state.tree;
7814 free_colors(&s->colors);
7815 free(s->tree_label);
7816 s->tree_label = NULL;
7818 s->commit_id = NULL;
7819 free(s->head_ref_name);
7820 s->head_ref_name = NULL;
7821 while (!TAILQ_EMPTY(&s->parents)) {
7822 struct tog_parent_tree *parent;
7823 parent = TAILQ_FIRST(&s->parents);
7824 TAILQ_REMOVE(&s->parents, parent, entry);
7825 if (parent->tree != s->root)
7826 got_object_tree_close(parent->tree);
7830 if (s->tree != NULL && s->tree != s->root)
7831 got_object_tree_close(s->tree);
7833 got_object_tree_close(s->root);
7837 static const struct got_error *
7838 search_start_tree_view(struct tog_view *view)
7840 struct tog_tree_view_state *s = &view->state.tree;
7842 s->matched_entry = NULL;
7847 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7849 regmatch_t regmatch;
7851 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7855 static const struct got_error *
7856 search_next_tree_view(struct tog_view *view)
7858 struct tog_tree_view_state *s = &view->state.tree;
7859 struct got_tree_entry *te = NULL;
7861 if (!view->searching) {
7862 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7866 if (s->matched_entry) {
7867 if (view->searching == TOG_SEARCH_FORWARD) {
7868 if (s->selected_entry)
7869 te = got_tree_entry_get_next(s->tree,
7872 te = got_object_tree_get_first_entry(s->tree);
7874 if (s->selected_entry == NULL)
7875 te = got_object_tree_get_last_entry(s->tree);
7877 te = got_tree_entry_get_prev(s->tree,
7881 if (s->selected_entry)
7882 te = s->selected_entry;
7883 else if (view->searching == TOG_SEARCH_FORWARD)
7884 te = got_object_tree_get_first_entry(s->tree);
7886 te = got_object_tree_get_last_entry(s->tree);
7891 if (s->matched_entry == NULL) {
7892 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7895 if (view->searching == TOG_SEARCH_FORWARD)
7896 te = got_object_tree_get_first_entry(s->tree);
7898 te = got_object_tree_get_last_entry(s->tree);
7901 if (match_tree_entry(te, &view->regex)) {
7902 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7903 s->matched_entry = te;
7907 if (view->searching == TOG_SEARCH_FORWARD)
7908 te = got_tree_entry_get_next(s->tree, te);
7910 te = got_tree_entry_get_prev(s->tree, te);
7913 if (s->matched_entry) {
7914 s->first_displayed_entry = s->matched_entry;
7921 static const struct got_error *
7922 show_tree_view(struct tog_view *view)
7924 const struct got_error *err = NULL;
7925 struct tog_tree_view_state *s = &view->state.tree;
7928 err = tree_entry_path(&parent_path, &s->parents, NULL);
7932 err = draw_tree_entries(view, parent_path);
7939 static const struct got_error *
7940 tree_goto_line(struct tog_view *view, int nlines)
7942 const struct got_error *err = NULL;
7943 struct tog_tree_view_state *s = &view->state.tree;
7944 struct got_tree_entry **fte, **lte, **ste;
7945 int g, last, first = 1, i = 1;
7946 int root = s->tree == s->root;
7947 int off = root ? 1 : 2;
7954 else if (g > got_object_tree_get_nentries(s->tree))
7955 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7957 fte = &s->first_displayed_entry;
7958 lte = &s->last_displayed_entry;
7959 ste = &s->selected_entry;
7962 first = got_tree_entry_get_index(*fte);
7963 first += off; /* account for ".." */
7965 last = got_tree_entry_get_index(*lte);
7968 if (g >= first && g <= last && g - first < nlines) {
7969 s->selected = g - first;
7970 return NULL; /* gline is on the current page */
7974 i = got_tree_entry_get_index(*ste);
7979 err = tree_scroll_down(view, g - i);
7982 if (got_tree_entry_get_index(*lte) >=
7983 got_object_tree_get_nentries(s->tree) - 1 &&
7984 first + s->selected < g &&
7985 s->selected < s->ndisplayed - 1) {
7986 first = got_tree_entry_get_index(*fte);
7988 s->selected = g - first;
7991 tree_scroll_up(s, i - g);
7994 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7995 s->selected = g - 1;
8000 static const struct got_error *
8001 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
8003 const struct got_error *err = NULL;
8004 struct tog_tree_view_state *s = &view->state.tree;
8005 struct got_tree_entry *te;
8006 int n, nscroll = view->nlines - 3;
8009 return tree_goto_line(view, nscroll);
8018 horizontal_scroll_input(view, ch);
8021 s->show_ids = !s->show_ids;
8026 if (!s->selected_entry)
8028 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8032 err = view_request_new(new_view, view, TOG_VIEW_REF);
8039 if (s->tree == s->root)
8040 s->first_displayed_entry =
8041 got_object_tree_get_first_entry(s->tree);
8043 s->first_displayed_entry = NULL;
8048 int eos = view->nlines - 3;
8050 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8054 te = got_object_tree_get_last_entry(s->tree);
8055 for (n = 0; n < eos; n++) {
8057 if (s->tree != s->root) {
8058 s->first_displayed_entry = NULL;
8063 s->first_displayed_entry = te;
8064 te = got_tree_entry_get_prev(s->tree, te);
8067 s->selected = n - 1;
8073 if (s->selected > 0) {
8077 tree_scroll_up(s, 1);
8078 if (s->selected_entry == NULL ||
8079 (s->tree == s->root && s->selected_entry ==
8080 got_object_tree_get_first_entry(s->tree)))