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 #if defined(__FreeBSD__) || defined(__APPLE__)
24 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
45 #include "got_compat.h"
47 #include "got_version.h"
48 #include "got_error.h"
49 #include "got_object.h"
50 #include "got_reference.h"
51 #include "got_repository.h"
53 #include "got_opentemp.h"
55 #include "got_cancel.h"
56 #include "got_commit_graph.h"
57 #include "got_blame.h"
58 #include "got_privsep.h"
60 #include "got_worktree.h"
63 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
67 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
71 #define CTRL(x) ((x) & 0x1f)
75 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
80 const struct got_error *(*cmd_main)(int, char *[]);
81 void (*cmd_usage)(void);
84 __dead static void usage(int, int);
85 __dead static void usage_log(void);
86 __dead static void usage_diff(void);
87 __dead static void usage_blame(void);
88 __dead static void usage_tree(void);
89 __dead static void usage_ref(void);
91 static const struct got_error* cmd_log(int, char *[]);
92 static const struct got_error* cmd_diff(int, char *[]);
93 static const struct got_error* cmd_blame(int, char *[]);
94 static const struct got_error* cmd_tree(int, char *[]);
95 static const struct got_error* cmd_ref(int, char *[]);
97 static const struct tog_cmd tog_commands[] = {
98 { "log", cmd_log, usage_log },
99 { "diff", cmd_diff, usage_diff },
100 { "blame", cmd_blame, usage_blame },
101 { "tree", cmd_tree, usage_tree },
102 { "ref", cmd_ref, usage_ref },
114 /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
115 enum tog_keymap_type {
116 TOG_KEYMAP_KEYS = -2,
132 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
134 #define TOG_EOF_STRING "(END)"
136 struct commit_queue_entry {
137 TAILQ_ENTRY(commit_queue_entry) entry;
138 struct got_object_id *id;
139 struct got_commit_object *commit;
142 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
143 struct commit_queue {
145 struct commit_queue_head head;
149 STAILQ_ENTRY(tog_color) entry;
153 STAILQ_HEAD(tog_colors, tog_color);
155 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
156 static struct got_reflist_object_id_map *tog_refs_idmap;
157 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
159 static const struct got_error *
160 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
161 struct got_reference* re2)
163 const char *name1 = got_ref_get_name(re1);
164 const char *name2 = got_ref_get_name(re2);
165 int isbackup1, isbackup2;
167 /* Sort backup refs towards the bottom of the list. */
168 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
169 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
170 if (!isbackup1 && isbackup2) {
173 } else if (isbackup1 && !isbackup2) {
178 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
182 static const struct got_error *
183 tog_load_refs(struct got_repository *repo, int sort_by_date)
185 const struct got_error *err;
187 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
188 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
193 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
200 if (tog_refs_idmap) {
201 got_reflist_object_id_map_free(tog_refs_idmap);
202 tog_refs_idmap = NULL;
204 got_ref_list_free(&tog_refs);
207 static const struct got_error *
208 add_color(struct tog_colors *colors, const char *pattern,
209 int idx, short color)
211 const struct got_error *err = NULL;
212 struct tog_color *tc;
215 if (idx < 1 || idx > COLOR_PAIRS - 1)
218 init_pair(idx, color, -1);
220 tc = calloc(1, sizeof(*tc));
222 return got_error_from_errno("calloc");
223 regerr = regcomp(&tc->regex, pattern,
224 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
226 static char regerr_msg[512];
227 static char err_msg[512];
228 regerror(regerr, &tc->regex, regerr_msg,
230 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
232 err = got_error_msg(GOT_ERR_REGEX, err_msg);
237 STAILQ_INSERT_HEAD(colors, tc, entry);
242 free_colors(struct tog_colors *colors)
244 struct tog_color *tc;
246 while (!STAILQ_EMPTY(colors)) {
247 tc = STAILQ_FIRST(colors);
248 STAILQ_REMOVE_HEAD(colors, entry);
254 static struct tog_color *
255 get_color(struct tog_colors *colors, int colorpair)
257 struct tog_color *tc = NULL;
259 STAILQ_FOREACH(tc, colors, entry) {
260 if (tc->colorpair == colorpair)
268 default_color_value(const char *envvar)
270 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
271 return COLOR_MAGENTA;
272 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
274 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
276 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
278 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
279 return COLOR_MAGENTA;
280 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
281 return COLOR_MAGENTA;
282 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
284 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
286 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
288 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
290 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
292 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
294 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
295 return COLOR_MAGENTA;
296 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
298 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
305 get_color_value(const char *envvar)
307 const char *val = getenv(envvar);
310 return default_color_value(envvar);
312 if (strcasecmp(val, "black") == 0)
314 if (strcasecmp(val, "red") == 0)
316 if (strcasecmp(val, "green") == 0)
318 if (strcasecmp(val, "yellow") == 0)
320 if (strcasecmp(val, "blue") == 0)
322 if (strcasecmp(val, "magenta") == 0)
323 return COLOR_MAGENTA;
324 if (strcasecmp(val, "cyan") == 0)
326 if (strcasecmp(val, "white") == 0)
328 if (strcasecmp(val, "default") == 0)
331 return default_color_value(envvar);
334 struct tog_diff_view_state {
335 struct got_object_id *id1, *id2;
336 const char *label1, *label2;
340 int first_displayed_line;
341 int last_displayed_line;
344 int ignore_whitespace;
346 struct got_repository *repo;
347 struct got_diff_line *lines;
352 /* passed from log or blame view; may be NULL */
353 struct tog_view *parent_view;
356 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
357 static volatile sig_atomic_t tog_thread_error;
359 struct tog_log_thread_args {
360 pthread_cond_t need_commits;
361 pthread_cond_t commit_loaded;
364 struct got_commit_graph *graph;
365 struct commit_queue *real_commits;
366 const char *in_repo_path;
367 struct got_object_id *start_id;
368 struct got_repository *repo;
372 struct commit_queue_entry **first_displayed_entry;
373 struct commit_queue_entry **selected_entry;
375 int *search_next_done;
379 regex_t *limit_regex;
380 struct commit_queue *limit_commits;
383 struct tog_log_view_state {
384 struct commit_queue *commits;
385 struct commit_queue_entry *first_displayed_entry;
386 struct commit_queue_entry *last_displayed_entry;
387 struct commit_queue_entry *selected_entry;
388 struct commit_queue real_commits;
393 struct got_repository *repo;
394 struct got_object_id *start_id;
397 struct tog_log_thread_args thread_args;
398 struct commit_queue_entry *matched_entry;
399 struct commit_queue_entry *search_entry;
400 struct tog_colors colors;
404 struct commit_queue limit_commits;
407 #define TOG_COLOR_DIFF_MINUS 1
408 #define TOG_COLOR_DIFF_PLUS 2
409 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
410 #define TOG_COLOR_DIFF_META 4
411 #define TOG_COLOR_TREE_SUBMODULE 5
412 #define TOG_COLOR_TREE_SYMLINK 6
413 #define TOG_COLOR_TREE_DIRECTORY 7
414 #define TOG_COLOR_TREE_EXECUTABLE 8
415 #define TOG_COLOR_COMMIT 9
416 #define TOG_COLOR_AUTHOR 10
417 #define TOG_COLOR_DATE 11
418 #define TOG_COLOR_REFS_HEADS 12
419 #define TOG_COLOR_REFS_TAGS 13
420 #define TOG_COLOR_REFS_REMOTES 14
421 #define TOG_COLOR_REFS_BACKUP 15
423 struct tog_blame_cb_args {
424 struct tog_blame_line *lines; /* one per line */
427 struct tog_view *view;
428 struct got_object_id *commit_id;
432 struct tog_blame_thread_args {
434 struct got_repository *repo;
435 struct tog_blame_cb_args *cb_args;
437 got_cancel_cb cancel_cb;
444 struct tog_blame_line *lines;
448 struct tog_blame_thread_args thread_args;
449 struct tog_blame_cb_args cb_args;
454 struct tog_blame_view_state {
455 int first_displayed_line;
456 int last_displayed_line;
458 int last_diffed_line;
462 struct got_object_id_queue blamed_commits;
463 struct got_object_qid *blamed_commit;
465 struct got_repository *repo;
466 struct got_object_id *commit_id;
467 struct got_object_id *id_to_log;
468 struct tog_blame blame;
470 struct tog_colors colors;
473 struct tog_parent_tree {
474 TAILQ_ENTRY(tog_parent_tree) entry;
475 struct got_tree_object *tree;
476 struct got_tree_entry *first_displayed_entry;
477 struct got_tree_entry *selected_entry;
481 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
483 struct tog_tree_view_state {
485 struct got_object_id *commit_id;/* commit which this tree belongs to */
486 struct got_tree_object *root; /* the commit's root tree entry */
487 struct got_tree_object *tree; /* currently displayed (sub-)tree */
488 struct got_tree_entry *first_displayed_entry;
489 struct got_tree_entry *last_displayed_entry;
490 struct got_tree_entry *selected_entry;
491 int ndisplayed, selected, show_ids;
492 struct tog_parent_trees parents; /* parent trees of current sub-tree */
494 struct got_repository *repo;
495 struct got_tree_entry *matched_entry;
496 struct tog_colors colors;
499 struct tog_reflist_entry {
500 TAILQ_ENTRY(tog_reflist_entry) entry;
501 struct got_reference *ref;
505 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
507 struct tog_ref_view_state {
508 struct tog_reflist_head refs;
509 struct tog_reflist_entry *first_displayed_entry;
510 struct tog_reflist_entry *last_displayed_entry;
511 struct tog_reflist_entry *selected_entry;
512 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
513 struct got_repository *repo;
514 struct tog_reflist_entry *matched_entry;
515 struct tog_colors colors;
518 struct tog_help_view_state {
523 int first_displayed_line;
524 int last_displayed_line;
529 enum tog_keymap_type type;
532 #define GENERATE_HELP \
533 KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
534 KEY_("H F1", "Open view-specific help (double tap for all help)"), \
535 KEY_("k C-p Up", "Move cursor or page up one line"), \
536 KEY_("j C-n Down", "Move cursor or page down one line"), \
537 KEY_("C-b b PgUp", "Scroll the view up one page"), \
538 KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
539 KEY_("C-u u", "Scroll the view up one half page"), \
540 KEY_("C-d d", "Scroll the view down one half page"), \
541 KEY_("g", "Go to line N (default: first line)"), \
542 KEY_("Home =", "Go to the first line"), \
543 KEY_("G", "Go to line N (default: last line)"), \
544 KEY_("End *", "Go to the last line"), \
545 KEY_("l Right", "Scroll the view right"), \
546 KEY_("h Left", "Scroll the view left"), \
547 KEY_("$", "Scroll view to the rightmost position"), \
548 KEY_("0", "Scroll view to the leftmost position"), \
549 KEY_("-", "Decrease size of the focussed split"), \
550 KEY_("+", "Increase size of the focussed split"), \
551 KEY_("Tab", "Switch focus between views"), \
552 KEY_("F", "Toggle fullscreen mode"), \
553 KEY_("/", "Open prompt to enter search term"), \
554 KEY_("n", "Find next line/token matching the current search term"), \
555 KEY_("N", "Find previous line/token matching the current search term"),\
556 KEY_("q", "Quit the focussed view; Quit help screen"), \
557 KEY_("Q", "Quit tog"), \
559 KEYMAP_("Log view", TOG_KEYMAP_LOG), \
560 KEY_("< ,", "Move cursor up one commit"), \
561 KEY_("> .", "Move cursor down one commit"), \
562 KEY_("Enter", "Open diff view of the selected commit"), \
563 KEY_("B", "Reload the log view and toggle display of merged commits"), \
564 KEY_("R", "Open ref view of all repository references"), \
565 KEY_("T", "Display tree view of the repository from the selected" \
567 KEY_("@", "Toggle between displaying author and committer name"), \
568 KEY_("&", "Open prompt to enter term to limit commits displayed"), \
569 KEY_("C-g Backspace", "Cancel current search or log operation"), \
570 KEY_("C-l", "Reload the log view with new commits in the repository"), \
572 KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
573 KEY_("K < ,", "Display diff of next line in the file/log entry"), \
574 KEY_("J > .", "Display diff of previous line in the file/log entry"), \
575 KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
576 KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
578 KEY_("(", "Go to the previous file in the diff"), \
579 KEY_(")", "Go to the next file in the diff"), \
580 KEY_("{", "Go to the previous hunk in the diff"), \
581 KEY_("}", "Go to the next hunk in the diff"), \
582 KEY_("[", "Decrease the number of context lines"), \
583 KEY_("]", "Increase the number of context lines"), \
584 KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
586 KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
587 KEY_("Enter", "Display diff view of the selected line's commit"), \
588 KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
589 KEY_("L", "Open log view for the currently selected annotated line"), \
590 KEY_("C", "Reload view with the previously blamed commit"), \
591 KEY_("c", "Reload view with the version of the file found in the" \
592 " selected line's commit"), \
593 KEY_("p", "Reload view with the version of the file found in the" \
594 " selected line's parent commit"), \
596 KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
597 KEY_("Enter", "Enter selected directory or open blame view of the" \
599 KEY_("L", "Open log view for the selected entry"), \
600 KEY_("R", "Open ref view of all repository references"), \
601 KEY_("i", "Show object IDs for all tree entries"), \
602 KEY_("Backspace", "Return to the parent directory"), \
604 KEYMAP_("Ref view", TOG_KEYMAP_REF), \
605 KEY_("Enter", "Display log view of the selected reference"), \
606 KEY_("T", "Display tree view of the selected reference"), \
607 KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
608 KEY_("m", "Toggle display of last modified date for each reference"), \
609 KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
610 KEY_("C-l", "Reload view with all repository references")
615 enum tog_keymap_type type;
619 * We implement two types of views: parent views and child views.
621 * The 'Tab' key switches focus between a parent view and its child view.
622 * Child views are shown side-by-side to their parent view, provided
623 * there is enough screen estate.
625 * When a new view is opened from within a parent view, this new view
626 * becomes a child view of the parent view, replacing any existing child.
628 * When a new view is opened from within a child view, this new view
629 * becomes a parent view which will obscure the views below until the
630 * user quits the new parent view by typing 'q'.
632 * This list of views contains parent views only.
633 * Child views are only pointed to by their parent view.
635 TAILQ_HEAD(tog_view_list_head, tog_view);
638 TAILQ_ENTRY(tog_view) entry;
641 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
642 int resized_y, resized_x; /* begin_y/x based on user resizing */
643 int maxx, x; /* max column and current start column */
644 int lines, cols; /* copies of LINES and COLS */
645 int nscrolled, offset; /* lines scrolled and hsplit line offset */
646 int gline, hiline; /* navigate to and highlight this nG line */
647 int ch, count; /* current keymap and count prefix */
648 int resized; /* set when in a resize event */
649 int focussed; /* Only set on one parent or child view at a time. */
651 struct tog_view *parent;
652 struct tog_view *child;
655 * This flag is initially set on parent views when a new child view
656 * is created. It gets toggled when the 'Tab' key switches focus
657 * between parent and child.
658 * The flag indicates whether focus should be passed on to our child
659 * view if this parent view gets picked for focus after another parent
660 * view was closed. This prevents child views from losing focus in such
665 enum tog_view_mode mode;
666 /* type-specific state */
667 enum tog_view_type type;
669 struct tog_diff_view_state diff;
670 struct tog_log_view_state log;
671 struct tog_blame_view_state blame;
672 struct tog_tree_view_state tree;
673 struct tog_ref_view_state ref;
674 struct tog_help_view_state help;
677 const struct got_error *(*show)(struct tog_view *);
678 const struct got_error *(*input)(struct tog_view **,
679 struct tog_view *, int);
680 const struct got_error *(*reset)(struct tog_view *);
681 const struct got_error *(*resize)(struct tog_view *, int);
682 const struct got_error *(*close)(struct tog_view *);
684 const struct got_error *(*search_start)(struct tog_view *);
685 const struct got_error *(*search_next)(struct tog_view *);
686 void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
687 int **, int **, int **, int **);
690 #define TOG_SEARCH_FORWARD 1
691 #define TOG_SEARCH_BACKWARD 2
692 int search_next_done;
693 #define TOG_SEARCH_HAVE_MORE 1
694 #define TOG_SEARCH_NO_MORE 2
695 #define TOG_SEARCH_HAVE_NONE 3
701 static const struct got_error *open_diff_view(struct tog_view *,
702 struct got_object_id *, struct got_object_id *,
703 const char *, const char *, int, int, int, struct tog_view *,
704 struct got_repository *);
705 static const struct got_error *show_diff_view(struct tog_view *);
706 static const struct got_error *input_diff_view(struct tog_view **,
707 struct tog_view *, int);
708 static const struct got_error *reset_diff_view(struct tog_view *);
709 static const struct got_error* close_diff_view(struct tog_view *);
710 static const struct got_error *search_start_diff_view(struct tog_view *);
711 static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
712 size_t *, int **, int **, int **, int **);
713 static const struct got_error *search_next_view_match(struct tog_view *);
715 static const struct got_error *open_log_view(struct tog_view *,
716 struct got_object_id *, struct got_repository *,
717 const char *, const char *, int);
718 static const struct got_error * show_log_view(struct tog_view *);
719 static const struct got_error *input_log_view(struct tog_view **,
720 struct tog_view *, int);
721 static const struct got_error *resize_log_view(struct tog_view *, int);
722 static const struct got_error *close_log_view(struct tog_view *);
723 static const struct got_error *search_start_log_view(struct tog_view *);
724 static const struct got_error *search_next_log_view(struct tog_view *);
726 static const struct got_error *open_blame_view(struct tog_view *, char *,
727 struct got_object_id *, struct got_repository *);
728 static const struct got_error *show_blame_view(struct tog_view *);
729 static const struct got_error *input_blame_view(struct tog_view **,
730 struct tog_view *, int);
731 static const struct got_error *reset_blame_view(struct tog_view *);
732 static const struct got_error *close_blame_view(struct tog_view *);
733 static const struct got_error *search_start_blame_view(struct tog_view *);
734 static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
735 size_t *, int **, int **, int **, int **);
737 static const struct got_error *open_tree_view(struct tog_view *,
738 struct got_object_id *, const char *, struct got_repository *);
739 static const struct got_error *show_tree_view(struct tog_view *);
740 static const struct got_error *input_tree_view(struct tog_view **,
741 struct tog_view *, int);
742 static const struct got_error *close_tree_view(struct tog_view *);
743 static const struct got_error *search_start_tree_view(struct tog_view *);
744 static const struct got_error *search_next_tree_view(struct tog_view *);
746 static const struct got_error *open_ref_view(struct tog_view *,
747 struct got_repository *);
748 static const struct got_error *show_ref_view(struct tog_view *);
749 static const struct got_error *input_ref_view(struct tog_view **,
750 struct tog_view *, int);
751 static const struct got_error *close_ref_view(struct tog_view *);
752 static const struct got_error *search_start_ref_view(struct tog_view *);
753 static const struct got_error *search_next_ref_view(struct tog_view *);
755 static const struct got_error *open_help_view(struct tog_view *,
757 static const struct got_error *show_help_view(struct tog_view *);
758 static const struct got_error *input_help_view(struct tog_view **,
759 struct tog_view *, int);
760 static const struct got_error *reset_help_view(struct tog_view *);
761 static const struct got_error* close_help_view(struct tog_view *);
762 static const struct got_error *search_start_help_view(struct tog_view *);
763 static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
764 size_t *, int **, int **, int **, int **);
766 static volatile sig_atomic_t tog_sigwinch_received;
767 static volatile sig_atomic_t tog_sigpipe_received;
768 static volatile sig_atomic_t tog_sigcont_received;
769 static volatile sig_atomic_t tog_sigint_received;
770 static volatile sig_atomic_t tog_sigterm_received;
773 tog_sigwinch(int signo)
775 tog_sigwinch_received = 1;
779 tog_sigpipe(int signo)
781 tog_sigpipe_received = 1;
785 tog_sigcont(int signo)
787 tog_sigcont_received = 1;
791 tog_sigint(int signo)
793 tog_sigint_received = 1;
797 tog_sigterm(int signo)
799 tog_sigterm_received = 1;
803 tog_fatal_signal_received(void)
805 return (tog_sigpipe_received ||
806 tog_sigint_received || tog_sigterm_received);
809 static const struct got_error *
810 view_close(struct tog_view *view)
812 const struct got_error *err = NULL, *child_err = NULL;
815 child_err = view_close(view->child);
819 err = view->close(view);
821 del_panel(view->panel);
823 delwin(view->window);
825 return err ? err : child_err;
828 static struct tog_view *
829 view_open(int nlines, int ncols, int begin_y, int begin_x,
830 enum tog_view_type type)
832 struct tog_view *view = calloc(1, sizeof(*view));
840 view->nlines = nlines ? nlines : LINES - begin_y;
841 view->ncols = ncols ? ncols : COLS - begin_x;
842 view->begin_y = begin_y;
843 view->begin_x = begin_x;
844 view->window = newwin(nlines, ncols, begin_y, begin_x);
845 if (view->window == NULL) {
849 view->panel = new_panel(view->window);
850 if (view->panel == NULL ||
851 set_panel_userptr(view->panel, view) != OK) {
856 keypad(view->window, TRUE);
861 view_split_begin_x(int begin_x)
863 if (begin_x > 0 || COLS < 120)
865 return (COLS - MAX(COLS / 2, 80));
868 /* XXX Stub till we decide what to do. */
870 view_split_begin_y(int lines)
872 return lines * HSPLIT_SCALE;
875 static const struct got_error *view_resize(struct tog_view *);
877 static const struct got_error *
878 view_splitscreen(struct tog_view *view)
880 const struct got_error *err = NULL;
882 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
883 if (view->resized_y && view->resized_y < view->lines)
884 view->begin_y = view->resized_y;
886 view->begin_y = view_split_begin_y(view->nlines);
888 } else if (!view->resized) {
889 if (view->resized_x && view->resized_x < view->cols - 1 &&
891 view->begin_x = view->resized_x;
893 view->begin_x = view_split_begin_x(0);
896 view->nlines = LINES - view->begin_y;
897 view->ncols = COLS - view->begin_x;
900 err = view_resize(view);
904 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
905 view->parent->nlines = view->begin_y;
907 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
908 return got_error_from_errno("mvwin");
913 static const struct got_error *
914 view_fullscreen(struct tog_view *view)
916 const struct got_error *err = NULL;
919 view->begin_y = view->resized ? view->begin_y : 0;
920 view->nlines = view->resized ? view->nlines : LINES;
924 err = view_resize(view);
928 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
929 return got_error_from_errno("mvwin");
935 view_is_parent_view(struct tog_view *view)
937 return view->parent == NULL;
941 view_is_splitscreen(struct tog_view *view)
943 return view->begin_x > 0 || view->begin_y > 0;
947 view_is_fullscreen(struct tog_view *view)
949 return view->nlines == LINES && view->ncols == COLS;
953 view_is_hsplit_top(struct tog_view *view)
955 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
956 view_is_splitscreen(view->child);
960 view_border(struct tog_view *view)
963 const struct tog_view *view_above;
966 return view_border(view->parent);
968 panel = panel_above(view->panel);
972 view_above = panel_userptr(panel);
973 if (view->mode == TOG_VIEW_SPLIT_HRZN)
974 mvwhline(view->window, view_above->begin_y - 1,
975 view->begin_x, got_locale_is_utf8() ?
976 ACS_HLINE : '-', view->ncols);
978 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
979 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
982 static const struct got_error *view_init_hsplit(struct tog_view *, int);
983 static const struct got_error *request_log_commits(struct tog_view *);
984 static const struct got_error *offset_selection_down(struct tog_view *);
985 static void offset_selection_up(struct tog_view *);
986 static void view_get_split(struct tog_view *, int *, int *);
988 static const struct got_error *
989 view_resize(struct tog_view *view)
991 const struct got_error *err = NULL;
992 int dif, nlines, ncols;
994 dif = LINES - view->lines; /* line difference */
996 if (view->lines > LINES)
997 nlines = view->nlines - (view->lines - LINES);
999 nlines = view->nlines + (LINES - view->lines);
1000 if (view->cols > COLS)
1001 ncols = view->ncols - (view->cols - COLS);
1003 ncols = view->ncols + (COLS - view->cols);
1006 int hs = view->child->begin_y;
1008 if (!view_is_fullscreen(view))
1009 view->child->begin_x = view_split_begin_x(view->begin_x);
1010 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1011 view->child->begin_x == 0) {
1014 view_fullscreen(view->child);
1015 if (view->child->focussed)
1016 show_panel(view->child->panel);
1018 show_panel(view->panel);
1020 ncols = view->child->begin_x;
1022 view_splitscreen(view->child);
1023 show_panel(view->child->panel);
1026 * XXX This is ugly and needs to be moved into the above
1027 * logic but "works" for now and my attempts at moving it
1028 * break either 'tab' or 'F' key maps in horizontal splits.
1031 err = view_splitscreen(view->child);
1034 if (dif < 0) { /* top split decreased */
1035 err = offset_selection_down(view);
1042 show_panel(view->child->panel);
1043 nlines = view->nlines;
1045 } else if (view->parent == NULL)
1048 if (view->resize && dif > 0) {
1049 err = view->resize(view, dif);
1054 if (wresize(view->window, nlines, ncols) == ERR)
1055 return got_error_from_errno("wresize");
1056 if (replace_panel(view->panel, view->window) == ERR)
1057 return got_error_from_errno("replace_panel");
1058 wclear(view->window);
1060 view->nlines = nlines;
1061 view->ncols = ncols;
1062 view->lines = LINES;
1068 static const struct got_error *
1069 resize_log_view(struct tog_view *view, int increase)
1071 struct tog_log_view_state *s = &view->state.log;
1072 const struct got_error *err = NULL;
1075 if (s->selected_entry)
1076 n = s->selected_entry->idx + view->lines - s->selected;
1079 * Request commits to account for the increased
1080 * height so we have enough to populate the view.
1082 if (s->commits->ncommits < n) {
1083 view->nscrolled = n - s->commits->ncommits + increase + 1;
1084 err = request_log_commits(view);
1091 view_adjust_offset(struct tog_view *view, int n)
1096 if (view->parent && view->parent->offset) {
1097 if (view->parent->offset + n >= 0)
1098 view->parent->offset += n;
1100 view->parent->offset = 0;
1101 } else if (view->offset) {
1102 if (view->offset - n >= 0)
1109 static const struct got_error *
1110 view_resize_split(struct tog_view *view, int resize)
1112 const struct got_error *err = NULL;
1113 struct tog_view *v = NULL;
1120 if (!v->child || !view_is_splitscreen(v->child))
1123 v->resized = v->child->resized = resize; /* lock for resize event */
1125 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1126 if (v->child->resized_y)
1127 v->child->begin_y = v->child->resized_y;
1129 v->child->begin_y -= resize;
1131 v->child->begin_y += resize;
1132 if (v->child->begin_y < 3) {
1134 v->child->begin_y = 3;
1135 } else if (v->child->begin_y > LINES - 1) {
1137 v->child->begin_y = LINES - 1;
1140 v->child->ncols = COLS;
1141 view_adjust_offset(view, resize);
1142 err = view_init_hsplit(v, v->child->begin_y);
1145 v->child->resized_y = v->child->begin_y;
1147 if (v->child->resized_x)
1148 v->child->begin_x = v->child->resized_x;
1150 v->child->begin_x -= resize;
1152 v->child->begin_x += resize;
1153 if (v->child->begin_x < 11) {
1155 v->child->begin_x = 11;
1156 } else if (v->child->begin_x > COLS - 1) {
1158 v->child->begin_x = COLS - 1;
1160 v->child->resized_x = v->child->begin_x;
1163 v->child->mode = v->mode;
1164 v->child->nlines = v->lines - v->child->begin_y;
1165 v->child->ncols = v->cols - v->child->begin_x;
1168 err = view_fullscreen(v);
1171 err = view_splitscreen(v->child);
1175 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1176 err = offset_selection_down(v->child);
1182 err = v->resize(v, 0);
1183 else if (v->child->resize)
1184 err = v->child->resize(v->child, 0);
1186 v->resized = v->child->resized = 0;
1192 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1194 struct tog_view *v = src->child ? src->child : src;
1196 dst->resized_x = v->resized_x;
1197 dst->resized_y = v->resized_y;
1200 static const struct got_error *
1201 view_close_child(struct tog_view *view)
1203 const struct got_error *err = NULL;
1205 if (view->child == NULL)
1208 err = view_close(view->child);
1213 static const struct got_error *
1214 view_set_child(struct tog_view *view, struct tog_view *child)
1216 const struct got_error *err = NULL;
1218 view->child = child;
1219 child->parent = view;
1221 err = view_resize(view);
1225 if (view->child->resized_x || view->child->resized_y)
1226 err = view_resize_split(view, 0);
1231 static const struct got_error *view_dispatch_request(struct tog_view **,
1232 struct tog_view *, enum tog_view_type, int, int);
1234 static const struct got_error *
1235 view_request_new(struct tog_view **requested, struct tog_view *view,
1236 enum tog_view_type request)
1238 struct tog_view *new_view = NULL;
1239 const struct got_error *err;
1244 if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1245 view_get_split(view, &y, &x);
1247 err = view_dispatch_request(&new_view, view, request, y, x);
1251 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1252 request != TOG_VIEW_HELP) {
1253 err = view_init_hsplit(view, y);
1259 new_view->focussed = 1;
1260 new_view->mode = view->mode;
1261 new_view->nlines = request == TOG_VIEW_HELP ?
1262 view->lines : view->lines - y;
1264 if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1265 view_transfer_size(new_view, view);
1266 err = view_close_child(view);
1269 err = view_set_child(view, new_view);
1272 view->focus_child = 1;
1274 *requested = new_view;
1280 tog_resizeterm(void)
1283 struct winsize size;
1285 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1286 cols = 80; /* Default */
1290 lines = size.ws_row;
1292 resize_term(lines, cols);
1295 static const struct got_error *
1296 view_search_start(struct tog_view *view)
1298 const struct got_error *err = NULL;
1299 struct tog_view *v = view;
1303 if (view->search_started) {
1304 regfree(&view->regex);
1305 view->searching = 0;
1306 memset(&view->regmatch, 0, sizeof(view->regmatch));
1308 view->search_started = 0;
1310 if (view->nlines < 1)
1313 if (view_is_hsplit_top(view))
1315 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1318 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1319 wclrtoeol(v->window);
1321 nodelay(v->window, FALSE); /* block for search term input */
1324 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1325 wrefresh(v->window);
1328 nodelay(v->window, TRUE);
1332 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1333 err = view->search_start(view);
1335 regfree(&view->regex);
1338 view->search_started = 1;
1339 view->searching = TOG_SEARCH_FORWARD;
1340 view->search_next_done = 0;
1341 view->search_next(view);
1347 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1348 static const struct got_error *
1349 switch_split(struct tog_view *view)
1351 const struct got_error *err = NULL;
1352 struct tog_view *v = NULL;
1359 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1360 v->mode = TOG_VIEW_SPLIT_VERT;
1362 v->mode = TOG_VIEW_SPLIT_HRZN;
1366 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1367 v->mode = TOG_VIEW_SPLIT_NONE;
1369 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1370 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1371 v->child->begin_y = v->child->resized_y;
1372 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1373 v->child->begin_x = v->child->resized_x;
1376 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1378 v->child->ncols = COLS;
1379 v->child->nscrolled = LINES - v->child->nlines;
1381 err = view_init_hsplit(v, v->child->begin_y);
1385 v->child->mode = v->mode;
1386 v->child->nlines = v->lines - v->child->begin_y;
1389 err = view_fullscreen(v);
1392 err = view_splitscreen(v->child);
1396 if (v->mode == TOG_VIEW_SPLIT_NONE)
1397 v->mode = TOG_VIEW_SPLIT_VERT;
1398 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1399 err = offset_selection_down(v);
1402 err = offset_selection_down(v->child);
1406 offset_selection_up(v);
1407 offset_selection_up(v->child);
1410 err = v->resize(v, 0);
1411 else if (v->child->resize)
1412 err = v->child->resize(v->child, 0);
1418 * Compute view->count from numeric input. Assign total to view->count and
1419 * return first non-numeric key entered.
1422 get_compound_key(struct tog_view *view, int c)
1424 struct tog_view *v = view;
1427 if (view_is_hsplit_top(view))
1429 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1433 cbreak(); /* block for input */
1434 nodelay(view->window, FALSE);
1435 wmove(v->window, v->nlines - 1, 0);
1436 wclrtoeol(v->window);
1437 waddch(v->window, ':');
1440 x = getcurx(v->window);
1441 if (x != ERR && x < view->ncols) {
1442 waddch(v->window, c);
1443 wrefresh(v->window);
1447 * Don't overflow. Max valid request should be the greatest
1448 * between the longest and total lines; cap at 10 million.
1453 n = n * 10 + (c - '0');
1454 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1456 if (c == 'G' || c == 'g') { /* nG key map */
1457 view->gline = view->hiline = n;
1462 /* Massage excessive or inapplicable values at the input handler. */
1469 action_report(struct tog_view *view)
1471 struct tog_view *v = view;
1473 if (view_is_hsplit_top(view))
1475 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1478 wmove(v->window, v->nlines - 1, 0);
1479 wclrtoeol(v->window);
1480 wprintw(v->window, ":%s", view->action);
1481 wrefresh(v->window);
1484 * Clear action status report. Only clear in blame view
1485 * once annotating is complete, otherwise it's too fast.
1487 if (view->type == TOG_VIEW_BLAME) {
1488 if (view->state.blame.blame_complete)
1489 view->action = NULL;
1491 view->action = NULL;
1494 static const struct got_error *
1495 view_input(struct tog_view **new, int *done, struct tog_view *view,
1496 struct tog_view_list_head *views)
1498 const struct got_error *err = NULL;
1505 action_report(view);
1507 /* Clear "no matches" indicator. */
1508 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1509 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1510 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1514 if (view->searching && !view->search_next_done) {
1515 errcode = pthread_mutex_unlock(&tog_mutex);
1517 return got_error_set_errno(errcode,
1518 "pthread_mutex_unlock");
1520 errcode = pthread_mutex_lock(&tog_mutex);
1522 return got_error_set_errno(errcode,
1523 "pthread_mutex_lock");
1524 view->search_next(view);
1528 /* Allow threads to make progress while we are waiting for input. */
1529 errcode = pthread_mutex_unlock(&tog_mutex);
1531 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1532 /* If we have an unfinished count, let C-g or backspace abort. */
1533 if (view->count && --view->count) {
1535 nodelay(view->window, TRUE);
1536 ch = wgetch(view->window);
1537 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1542 ch = wgetch(view->window);
1543 if (ch >= '1' && ch <= '9')
1544 view->ch = ch = get_compound_key(view, ch);
1546 if (view->hiline && ch != ERR && ch != 0)
1547 view->hiline = 0; /* key pressed, clear line highlight */
1548 nodelay(view->window, TRUE);
1549 errcode = pthread_mutex_lock(&tog_mutex);
1551 return got_error_set_errno(errcode, "pthread_mutex_lock");
1553 if (tog_sigwinch_received || tog_sigcont_received) {
1555 tog_sigwinch_received = 0;
1556 tog_sigcont_received = 0;
1557 TAILQ_FOREACH(v, views, entry) {
1558 err = view_resize(v);
1561 err = v->input(new, v, KEY_RESIZE);
1565 err = view_resize(v->child);
1568 err = v->child->input(new, v->child,
1572 if (v->child->resized_x || v->child->resized_y) {
1573 err = view_resize_split(v, 0);
1585 if (view->type == TOG_VIEW_HELP)
1586 err = view->reset(view);
1588 err = view_request_new(new, view, TOG_VIEW_HELP);
1594 view->child->focussed = 1;
1595 view->focus_child = 1;
1596 } else if (view->parent) {
1598 view->parent->focussed = 1;
1599 view->parent->focus_child = 0;
1600 if (!view_is_splitscreen(view)) {
1601 if (view->parent->resize) {
1602 err = view->parent->resize(view->parent,
1607 offset_selection_up(view->parent);
1608 err = view_fullscreen(view->parent);
1615 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1616 if (view->parent->resize) {
1617 /* might need more commits to fill fullscreen */
1618 err = view->parent->resize(view->parent, 0);
1622 offset_selection_up(view->parent);
1624 err = view->input(new, view, ch);
1632 if (view_is_parent_view(view)) {
1633 if (view->child == NULL)
1635 if (view_is_splitscreen(view->child)) {
1637 view->child->focussed = 1;
1638 err = view_fullscreen(view->child);
1640 err = view_splitscreen(view->child);
1642 err = view_resize_split(view, 0);
1646 err = view->child->input(new, view->child,
1649 if (view_is_splitscreen(view)) {
1650 view->parent->focussed = 0;
1652 err = view_fullscreen(view);
1654 err = view_splitscreen(view);
1655 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1656 err = view_resize(view->parent);
1658 err = view_resize_split(view, 0);
1662 err = view->input(new, view, KEY_RESIZE);
1667 err = view->resize(view, 0);
1672 err = offset_selection_down(view->parent);
1674 err = offset_selection_down(view);
1678 err = switch_split(view);
1681 err = view_resize_split(view, -1);
1684 err = view_resize_split(view, 1);
1690 if (view->search_start)
1691 view_search_start(view);
1693 err = view->input(new, view, ch);
1697 if (view->search_started && view->search_next) {
1698 view->searching = (ch == 'n' ?
1699 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1700 view->search_next_done = 0;
1701 view->search_next(view);
1703 err = view->input(new, view, ch);
1706 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1707 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1708 view->action = "Patience diff algorithm";
1710 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1711 view->action = "Myers diff algorithm";
1713 TAILQ_FOREACH(v, views, entry) {
1719 if (v->child && v->child->reset) {
1720 err = v->child->reset(v->child);
1727 err = view->input(new, view, ch);
1735 view_needs_focus_indication(struct tog_view *view)
1737 if (view_is_parent_view(view)) {
1738 if (view->child == NULL || view->child->focussed)
1740 if (!view_is_splitscreen(view->child))
1742 } else if (!view_is_splitscreen(view))
1745 return view->focussed;
1748 static const struct got_error *
1749 view_loop(struct tog_view *view)
1751 const struct got_error *err = NULL;
1752 struct tog_view_list_head views;
1753 struct tog_view *new_view;
1755 int fast_refresh = 10;
1756 int done = 0, errcode;
1758 mode = getenv("TOG_VIEW_SPLIT_MODE");
1759 if (!mode || !(*mode == 'h' || *mode == 'H'))
1760 view->mode = TOG_VIEW_SPLIT_VERT;
1762 view->mode = TOG_VIEW_SPLIT_HRZN;
1764 errcode = pthread_mutex_lock(&tog_mutex);
1766 return got_error_set_errno(errcode, "pthread_mutex_lock");
1769 TAILQ_INSERT_HEAD(&views, view, entry);
1772 err = view->show(view);
1777 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1778 !tog_fatal_signal_received()) {
1779 /* Refresh fast during initialization, then become slower. */
1780 if (fast_refresh && --fast_refresh == 0)
1781 halfdelay(10); /* switch to once per second */
1783 err = view_input(&new_view, &done, view, &views);
1787 if (view->dying && view == TAILQ_FIRST(&views) &&
1788 TAILQ_NEXT(view, entry) == NULL)
1794 * When we quit, scroll the screen up a single line
1795 * so we don't lose any information.
1797 TAILQ_FOREACH(v, &views, entry) {
1798 wmove(v->window, 0, 0);
1799 wdeleteln(v->window);
1800 wnoutrefresh(v->window);
1801 if (v->child && !view_is_fullscreen(v)) {
1802 wmove(v->child->window, 0, 0);
1803 wdeleteln(v->child->window);
1804 wnoutrefresh(v->child->window);
1811 struct tog_view *v, *prev = NULL;
1813 if (view_is_parent_view(view))
1814 prev = TAILQ_PREV(view, tog_view_list_head,
1816 else if (view->parent)
1817 prev = view->parent;
1820 view->parent->child = NULL;
1821 view->parent->focus_child = 0;
1822 /* Restore fullscreen line height. */
1823 view->parent->nlines = view->parent->lines;
1824 err = view_resize(view->parent);
1827 /* Make resized splits persist. */
1828 view_transfer_size(view->parent, view);
1830 TAILQ_REMOVE(&views, view, entry);
1832 err = view_close(view);
1837 TAILQ_FOREACH(v, &views, entry) {
1841 if (view == NULL && new_view == NULL) {
1842 /* No view has focus. Try to pick one. */
1845 else if (!TAILQ_EMPTY(&views)) {
1846 view = TAILQ_LAST(&views,
1847 tog_view_list_head);
1850 if (view->focus_child) {
1851 view->child->focussed = 1;
1859 struct tog_view *v, *t;
1860 /* Only allow one parent view per type. */
1861 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1862 if (v->type != new_view->type)
1864 TAILQ_REMOVE(&views, v, entry);
1865 err = view_close(v);
1870 TAILQ_INSERT_TAIL(&views, new_view, entry);
1873 if (view && !done) {
1874 if (view_is_parent_view(view)) {
1875 if (view->child && view->child->focussed)
1878 if (view->parent && view->parent->focussed)
1879 view = view->parent;
1881 show_panel(view->panel);
1882 if (view->child && view_is_splitscreen(view->child))
1883 show_panel(view->child->panel);
1884 if (view->parent && view_is_splitscreen(view)) {
1885 err = view->parent->show(view->parent);
1889 err = view->show(view);
1893 err = view->child->show(view->child);
1902 while (!TAILQ_EMPTY(&views)) {
1903 const struct got_error *close_err;
1904 view = TAILQ_FIRST(&views);
1905 TAILQ_REMOVE(&views, view, entry);
1906 close_err = view_close(view);
1907 if (close_err && err == NULL)
1911 errcode = pthread_mutex_unlock(&tog_mutex);
1912 if (errcode && err == NULL)
1913 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1923 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1928 /* Create newly allocated wide-character string equivalent to a byte string. */
1929 static const struct got_error *
1930 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1933 const struct got_error *err = NULL;
1936 *wlen = mbstowcs(NULL, s, 0);
1937 if (*wlen == (size_t)-1) {
1939 if (errno != EILSEQ)
1940 return got_error_from_errno("mbstowcs");
1942 /* byte string invalid in current encoding; try to "fix" it */
1943 err = got_mbsavis(&vis, &vislen, s);
1946 *wlen = mbstowcs(NULL, vis, 0);
1947 if (*wlen == (size_t)-1) {
1948 err = got_error_from_errno("mbstowcs"); /* give up */
1953 *ws = calloc(*wlen + 1, sizeof(**ws));
1955 err = got_error_from_errno("calloc");
1959 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1960 err = got_error_from_errno("mbstowcs");
1971 static const struct got_error *
1972 expand_tab(char **ptr, const char *src)
1975 size_t len, n, idx = 0, sz = 0;
1978 n = len = strlen(src);
1979 dst = malloc(n + 1);
1981 return got_error_from_errno("malloc");
1983 while (idx < len && src[idx]) {
1984 const char c = src[idx];
1987 size_t nb = TABSIZE - sz % TABSIZE;
1990 p = realloc(dst, n + nb);
1993 return got_error_from_errno("realloc");
1998 memset(dst + sz, ' ', nb);
2001 dst[sz++] = src[idx];
2011 * Advance at most n columns from wline starting at offset off.
2012 * Return the index to the first character after the span operation.
2013 * Return the combined column width of all spanned wide character in
2017 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2019 int width, i, cols = 0;
2026 for (i = off; wline[i] != L'\0'; ++i) {
2027 if (wline[i] == L'\t')
2028 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2030 width = wcwidth(wline[i]);
2037 if (cols + width > n)
2047 * Format a line for display, ensuring that it won't overflow a width limit.
2048 * With scrolling, the width returned refers to the scrolled version of the
2049 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2051 static const struct got_error *
2052 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2053 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2055 const struct got_error *err = NULL;
2057 wchar_t *wline = NULL;
2066 err = expand_tab(&exstr, line);
2071 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2076 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2078 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2079 wline[wlen - 1] = L'\0';
2082 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2083 wline[wlen - 1] = L'\0';
2087 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2093 *scrollxp = scrollx;
2101 static const struct got_error*
2102 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2103 struct got_object_id *id, struct got_repository *repo)
2105 static const struct got_error *err = NULL;
2106 struct got_reflist_entry *re;
2112 TAILQ_FOREACH(re, refs, entry) {
2113 struct got_tag_object *tag = NULL;
2114 struct got_object_id *ref_id;
2117 name = got_ref_get_name(re->ref);
2118 if (strcmp(name, GOT_REF_HEAD) == 0)
2120 if (strncmp(name, "refs/", 5) == 0)
2122 if (strncmp(name, "got/", 4) == 0 &&
2123 strncmp(name, "got/backup/", 11) != 0)
2125 if (strncmp(name, "heads/", 6) == 0)
2127 if (strncmp(name, "remotes/", 8) == 0) {
2129 s = strstr(name, "/" GOT_REF_HEAD);
2130 if (s != NULL && s[strlen(s)] == '\0')
2133 err = got_ref_resolve(&ref_id, repo, re->ref);
2136 if (strncmp(name, "tags/", 5) == 0) {
2137 err = got_object_open_as_tag(&tag, repo, ref_id);
2139 if (err->code != GOT_ERR_OBJ_TYPE) {
2143 /* Ref points at something other than a tag. */
2148 cmp = got_object_id_cmp(tag ?
2149 got_object_tag_get_object_id(tag) : ref_id, id);
2152 got_object_tag_close(tag);
2156 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2157 s ? ", " : "", name) == -1) {
2158 err = got_error_from_errno("asprintf");
2169 static const struct got_error *
2170 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2175 smallerthan = strchr(author, '<');
2176 if (smallerthan && smallerthan[1] != '\0')
2177 author = smallerthan + 1;
2178 author[strcspn(author, "@>")] = '\0';
2179 return format_line(wauthor, author_width, NULL, author, 0, limit,
2183 static const struct got_error *
2184 draw_commit(struct tog_view *view, struct got_commit_object *commit,
2185 struct got_object_id *id, const size_t date_display_cols,
2186 int author_display_cols)
2188 struct tog_log_view_state *s = &view->state.log;
2189 const struct got_error *err = NULL;
2190 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2191 char *logmsg0 = NULL, *logmsg = NULL;
2192 char *author = NULL;
2193 wchar_t *wlogmsg = NULL, *wauthor = NULL;
2194 int author_width, logmsg_width;
2195 char *newline, *line = NULL;
2196 int col, limit, scrollx;
2197 const int avail = view->ncols;
2199 time_t committer_time;
2200 struct tog_color *tc;
2202 committer_time = got_object_commit_get_committer_time(commit);
2203 if (gmtime_r(&committer_time, &tm) == NULL)
2204 return got_error_from_errno("gmtime_r");
2205 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2206 return got_error(GOT_ERR_NO_SPACE);
2208 if (avail <= date_display_cols)
2209 limit = MIN(sizeof(datebuf) - 1, avail);
2211 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2212 tc = get_color(&s->colors, TOG_COLOR_DATE);
2214 wattr_on(view->window,
2215 COLOR_PAIR(tc->colorpair), NULL);
2216 waddnstr(view->window, datebuf, limit);
2218 wattr_off(view->window,
2219 COLOR_PAIR(tc->colorpair), NULL);
2226 err = got_object_id_str(&id_str, id);
2229 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2231 wattr_on(view->window,
2232 COLOR_PAIR(tc->colorpair), NULL);
2233 wprintw(view->window, "%.8s ", id_str);
2235 wattr_off(view->window,
2236 COLOR_PAIR(tc->colorpair), NULL);
2243 if (s->use_committer)
2244 author = strdup(got_object_commit_get_committer(commit));
2246 author = strdup(got_object_commit_get_author(commit));
2247 if (author == NULL) {
2248 err = got_error_from_errno("strdup");
2251 err = format_author(&wauthor, &author_width, author, avail - col, col);
2254 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2256 wattr_on(view->window,
2257 COLOR_PAIR(tc->colorpair), NULL);
2258 waddwstr(view->window, wauthor);
2259 col += author_width;
2260 while (col < avail && author_width < author_display_cols + 2) {
2261 waddch(view->window, ' ');
2266 wattr_off(view->window,
2267 COLOR_PAIR(tc->colorpair), NULL);
2271 err = got_object_commit_get_logmsg(&logmsg0, commit);
2275 while (*logmsg == '\n')
2277 newline = strchr(logmsg, '\n');
2280 limit = avail - col;
2281 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2282 limit--; /* for the border */
2283 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2287 waddwstr(view->window, &wlogmsg[scrollx]);
2288 col += MAX(logmsg_width, 0);
2289 while (col < avail) {
2290 waddch(view->window, ' ');
2302 static struct commit_queue_entry *
2303 alloc_commit_queue_entry(struct got_commit_object *commit,
2304 struct got_object_id *id)
2306 struct commit_queue_entry *entry;
2307 struct got_object_id *dup;
2309 entry = calloc(1, sizeof(*entry));
2313 dup = got_object_id_dup(id);
2320 entry->commit = commit;
2325 pop_commit(struct commit_queue *commits)
2327 struct commit_queue_entry *entry;
2329 entry = TAILQ_FIRST(&commits->head);
2330 TAILQ_REMOVE(&commits->head, entry, entry);
2331 got_object_commit_close(entry->commit);
2332 commits->ncommits--;
2338 free_commits(struct commit_queue *commits)
2340 while (!TAILQ_EMPTY(&commits->head))
2341 pop_commit(commits);
2344 static const struct got_error *
2345 match_commit(int *have_match, struct got_object_id *id,
2346 struct got_commit_object *commit, regex_t *regex)
2348 const struct got_error *err = NULL;
2349 regmatch_t regmatch;
2350 char *id_str = NULL, *logmsg = NULL;
2354 err = got_object_id_str(&id_str, id);
2358 err = got_object_commit_get_logmsg(&logmsg, commit);
2362 if (regexec(regex, got_object_commit_get_author(commit), 1,
2363 ®match, 0) == 0 ||
2364 regexec(regex, got_object_commit_get_committer(commit), 1,
2365 ®match, 0) == 0 ||
2366 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2367 regexec(regex, logmsg, 1, ®match, 0) == 0)
2375 static const struct got_error *
2376 queue_commits(struct tog_log_thread_args *a)
2378 const struct got_error *err = NULL;
2381 * We keep all commits open throughout the lifetime of the log
2382 * view in order to avoid having to re-fetch commits from disk
2383 * while updating the display.
2386 struct got_object_id id;
2387 struct got_commit_object *commit;
2388 struct commit_queue_entry *entry;
2389 int limit_match = 0;
2392 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2397 err = got_object_open_as_commit(&commit, a->repo, &id);
2400 entry = alloc_commit_queue_entry(commit, &id);
2401 if (entry == NULL) {
2402 err = got_error_from_errno("alloc_commit_queue_entry");
2406 errcode = pthread_mutex_lock(&tog_mutex);
2408 err = got_error_set_errno(errcode,
2409 "pthread_mutex_lock");
2413 entry->idx = a->real_commits->ncommits;
2414 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2415 a->real_commits->ncommits++;
2418 err = match_commit(&limit_match, &id, commit,
2424 struct commit_queue_entry *matched;
2426 matched = alloc_commit_queue_entry(
2427 entry->commit, entry->id);
2428 if (matched == NULL) {
2429 err = got_error_from_errno(
2430 "alloc_commit_queue_entry");
2433 matched->commit = entry->commit;
2434 got_object_commit_retain(entry->commit);
2436 matched->idx = a->limit_commits->ncommits;
2437 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2439 a->limit_commits->ncommits++;
2443 * This is how we signal log_thread() that we
2444 * have found a match, and that it should be
2445 * counted as a new entry for the view.
2447 a->limit_match = limit_match;
2450 if (*a->searching == TOG_SEARCH_FORWARD &&
2451 !*a->search_next_done) {
2453 err = match_commit(&have_match, &id, commit, a->regex);
2458 if (limit_match && have_match)
2459 *a->search_next_done =
2460 TOG_SEARCH_HAVE_MORE;
2461 } else if (have_match)
2462 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2465 errcode = pthread_mutex_unlock(&tog_mutex);
2466 if (errcode && err == NULL)
2467 err = got_error_set_errno(errcode,
2468 "pthread_mutex_unlock");
2471 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2477 select_commit(struct tog_log_view_state *s)
2479 struct commit_queue_entry *entry;
2482 entry = s->first_displayed_entry;
2484 if (ncommits == s->selected) {
2485 s->selected_entry = entry;
2488 entry = TAILQ_NEXT(entry, entry);
2493 static const struct got_error *
2494 draw_commits(struct tog_view *view)
2496 const struct got_error *err = NULL;
2497 struct tog_log_view_state *s = &view->state.log;
2498 struct commit_queue_entry *entry = s->selected_entry;
2499 int limit = view->nlines;
2501 int ncommits, author_cols = 4;
2502 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2503 char *refs_str = NULL;
2505 struct tog_color *tc;
2506 static const size_t date_display_cols = 12;
2508 if (view_is_hsplit_top(view))
2509 --limit; /* account for border */
2511 if (s->selected_entry &&
2512 !(view->searching && view->search_next_done == 0)) {
2513 struct got_reflist_head *refs;
2514 err = got_object_id_str(&id_str, s->selected_entry->id);
2517 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2518 s->selected_entry->id);
2520 err = build_refs_str(&refs_str, refs,
2521 s->selected_entry->id, s->repo);
2527 if (s->thread_args.commits_needed == 0)
2528 halfdelay(10); /* disable fast refresh */
2530 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2531 if (asprintf(&ncommits_str, " [%d/%d] %s",
2532 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2533 (view->searching && !view->search_next_done) ?
2534 "searching..." : "loading...") == -1) {
2535 err = got_error_from_errno("asprintf");
2539 const char *search_str = NULL;
2540 const char *limit_str = NULL;
2542 if (view->searching) {
2543 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2544 search_str = "no more matches";
2545 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2546 search_str = "no matches found";
2547 else if (!view->search_next_done)
2548 search_str = "searching...";
2551 if (s->limit_view && s->commits->ncommits == 0)
2552 limit_str = "no matches found";
2554 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2555 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2556 search_str ? search_str : (refs_str ? refs_str : ""),
2557 limit_str ? limit_str : "") == -1) {
2558 err = got_error_from_errno("asprintf");
2563 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2564 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2565 "........................................",
2566 s->in_repo_path, ncommits_str) == -1) {
2567 err = got_error_from_errno("asprintf");
2571 } else if (asprintf(&header, "commit %s%s",
2572 id_str ? id_str : "........................................",
2573 ncommits_str) == -1) {
2574 err = got_error_from_errno("asprintf");
2578 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2582 werase(view->window);
2584 if (view_needs_focus_indication(view))
2585 wstandout(view->window);
2586 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2588 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2589 waddwstr(view->window, wline);
2590 while (width < view->ncols) {
2591 waddch(view->window, ' ');
2595 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2596 if (view_needs_focus_indication(view))
2597 wstandend(view->window);
2602 /* Grow author column size if necessary, and set view->maxx. */
2603 entry = s->first_displayed_entry;
2607 struct got_commit_object *c = entry->commit;
2608 char *author, *eol, *msg, *msg0;
2609 wchar_t *wauthor, *wmsg;
2611 if (ncommits >= limit - 1)
2613 if (s->use_committer)
2614 author = strdup(got_object_commit_get_committer(c));
2616 author = strdup(got_object_commit_get_author(c));
2617 if (author == NULL) {
2618 err = got_error_from_errno("strdup");
2621 err = format_author(&wauthor, &width, author, COLS,
2623 if (author_cols < width)
2624 author_cols = width;
2629 err = got_object_commit_get_logmsg(&msg0, c);
2633 while (*msg == '\n')
2635 if ((eol = strchr(msg, '\n')))
2637 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2638 date_display_cols + author_cols, 0);
2641 view->maxx = MAX(view->maxx, width);
2645 entry = TAILQ_NEXT(entry, entry);
2648 entry = s->first_displayed_entry;
2649 s->last_displayed_entry = s->first_displayed_entry;
2652 if (ncommits >= limit - 1)
2654 if (ncommits == s->selected)
2655 wstandout(view->window);
2656 err = draw_commit(view, entry->commit, entry->id,
2657 date_display_cols, author_cols);
2658 if (ncommits == s->selected)
2659 wstandend(view->window);
2663 s->last_displayed_entry = entry;
2664 entry = TAILQ_NEXT(entry, entry);
2677 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2679 struct commit_queue_entry *entry;
2682 entry = TAILQ_FIRST(&s->commits->head);
2683 if (s->first_displayed_entry == entry)
2686 entry = s->first_displayed_entry;
2687 while (entry && nscrolled < maxscroll) {
2688 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2690 s->first_displayed_entry = entry;
2696 static const struct got_error *
2697 trigger_log_thread(struct tog_view *view, int wait)
2699 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2702 halfdelay(1); /* fast refresh while loading commits */
2704 while (!ta->log_complete && !tog_thread_error &&
2705 (ta->commits_needed > 0 || ta->load_all)) {
2706 /* Wake the log thread. */
2707 errcode = pthread_cond_signal(&ta->need_commits);
2709 return got_error_set_errno(errcode,
2710 "pthread_cond_signal");
2713 * The mutex will be released while the view loop waits
2714 * in wgetch(), at which time the log thread will run.
2719 /* Display progress update in log view. */
2720 show_log_view(view);
2724 /* Wait right here while next commit is being loaded. */
2725 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2727 return got_error_set_errno(errcode,
2728 "pthread_cond_wait");
2730 /* Display progress update in log view. */
2731 show_log_view(view);
2739 static const struct got_error *
2740 request_log_commits(struct tog_view *view)
2742 struct tog_log_view_state *state = &view->state.log;
2743 const struct got_error *err = NULL;
2745 if (state->thread_args.log_complete)
2748 state->thread_args.commits_needed += view->nscrolled;
2749 err = trigger_log_thread(view, 1);
2750 view->nscrolled = 0;
2755 static const struct got_error *
2756 log_scroll_down(struct tog_view *view, int maxscroll)
2758 struct tog_log_view_state *s = &view->state.log;
2759 const struct got_error *err = NULL;
2760 struct commit_queue_entry *pentry;
2761 int nscrolled = 0, ncommits_needed;
2763 if (s->last_displayed_entry == NULL)
2766 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2767 if (s->commits->ncommits < ncommits_needed &&
2768 !s->thread_args.log_complete) {
2770 * Ask the log thread for required amount of commits.
2772 s->thread_args.commits_needed +=
2773 ncommits_needed - s->commits->ncommits;
2774 err = trigger_log_thread(view, 1);
2780 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2781 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2784 s->last_displayed_entry = pentry ?
2785 pentry : s->last_displayed_entry;
2787 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2790 s->first_displayed_entry = pentry;
2791 } while (++nscrolled < maxscroll);
2793 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2794 view->nscrolled += nscrolled;
2796 view->nscrolled = 0;
2801 static const struct got_error *
2802 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2803 struct got_commit_object *commit, struct got_object_id *commit_id,
2804 struct tog_view *log_view, struct got_repository *repo)
2806 const struct got_error *err;
2807 struct got_object_qid *parent_id;
2808 struct tog_view *diff_view;
2810 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2811 if (diff_view == NULL)
2812 return got_error_from_errno("view_open");
2814 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2815 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2816 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2818 *new_view = diff_view;
2822 static const struct got_error *
2823 tree_view_visit_subtree(struct tog_tree_view_state *s,
2824 struct got_tree_object *subtree)
2826 struct tog_parent_tree *parent;
2828 parent = calloc(1, sizeof(*parent));
2830 return got_error_from_errno("calloc");
2832 parent->tree = s->tree;
2833 parent->first_displayed_entry = s->first_displayed_entry;
2834 parent->selected_entry = s->selected_entry;
2835 parent->selected = s->selected;
2836 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2839 s->first_displayed_entry = NULL;
2843 static const struct got_error *
2844 tree_view_walk_path(struct tog_tree_view_state *s,
2845 struct got_commit_object *commit, const char *path)
2847 const struct got_error *err = NULL;
2848 struct got_tree_object *tree = NULL;
2850 char *slash, *subpath = NULL;
2852 /* Walk the path and open corresponding tree objects. */
2855 struct got_tree_entry *te;
2856 struct got_object_id *tree_id;
2862 /* Ensure the correct subtree entry is selected. */
2863 slash = strchr(p, '/');
2865 te_name = strdup(p);
2867 te_name = strndup(p, slash - p);
2868 if (te_name == NULL) {
2869 err = got_error_from_errno("strndup");
2872 te = got_object_tree_find_entry(s->tree, te_name);
2874 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2879 s->first_displayed_entry = s->selected_entry = te;
2881 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2882 break; /* jump to this file's entry */
2884 slash = strchr(p, '/');
2886 subpath = strndup(path, slash - path);
2888 subpath = strdup(path);
2889 if (subpath == NULL) {
2890 err = got_error_from_errno("strdup");
2894 err = got_object_id_by_path(&tree_id, s->repo, commit,
2899 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2904 err = tree_view_visit_subtree(s, tree);
2906 got_object_tree_close(tree);
2920 static const struct got_error *
2921 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2922 struct commit_queue_entry *entry, const char *path,
2923 const char *head_ref_name, struct got_repository *repo)
2925 const struct got_error *err = NULL;
2926 struct tog_tree_view_state *s;
2927 struct tog_view *tree_view;
2929 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2930 if (tree_view == NULL)
2931 return got_error_from_errno("view_open");
2933 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2936 s = &tree_view->state.tree;
2938 *new_view = tree_view;
2940 if (got_path_is_root_dir(path))
2943 return tree_view_walk_path(s, entry->commit, path);
2946 static const struct got_error *
2947 block_signals_used_by_main_thread(void)
2952 if (sigemptyset(&sigset) == -1)
2953 return got_error_from_errno("sigemptyset");
2955 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2956 if (sigaddset(&sigset, SIGWINCH) == -1)
2957 return got_error_from_errno("sigaddset");
2958 if (sigaddset(&sigset, SIGCONT) == -1)
2959 return got_error_from_errno("sigaddset");
2960 if (sigaddset(&sigset, SIGINT) == -1)
2961 return got_error_from_errno("sigaddset");
2962 if (sigaddset(&sigset, SIGTERM) == -1)
2963 return got_error_from_errno("sigaddset");
2965 /* ncurses handles SIGTSTP */
2966 if (sigaddset(&sigset, SIGTSTP) == -1)
2967 return got_error_from_errno("sigaddset");
2969 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2971 return got_error_set_errno(errcode, "pthread_sigmask");
2977 log_thread(void *arg)
2979 const struct got_error *err = NULL;
2981 struct tog_log_thread_args *a = arg;
2985 * Sync startup with main thread such that we begin our
2986 * work once view_input() has released the mutex.
2988 errcode = pthread_mutex_lock(&tog_mutex);
2990 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2994 err = block_signals_used_by_main_thread();
2996 pthread_mutex_unlock(&tog_mutex);
3000 while (!done && !err && !tog_fatal_signal_received()) {
3001 errcode = pthread_mutex_unlock(&tog_mutex);
3003 err = got_error_set_errno(errcode,
3004 "pthread_mutex_unlock");
3007 err = queue_commits(a);
3009 if (err->code != GOT_ERR_ITER_COMPLETED)
3013 } else if (a->commits_needed > 0 && !a->load_all) {
3016 a->commits_needed--;
3018 a->commits_needed--;
3021 errcode = pthread_mutex_lock(&tog_mutex);
3023 err = got_error_set_errno(errcode,
3024 "pthread_mutex_lock");
3026 } else if (*a->quit)
3028 else if (*a->limiting && *a->first_displayed_entry == NULL) {
3029 *a->first_displayed_entry =
3030 TAILQ_FIRST(&a->limit_commits->head);
3031 *a->selected_entry = *a->first_displayed_entry;
3032 } else if (*a->first_displayed_entry == NULL) {
3033 *a->first_displayed_entry =
3034 TAILQ_FIRST(&a->real_commits->head);
3035 *a->selected_entry = *a->first_displayed_entry;
3038 errcode = pthread_cond_signal(&a->commit_loaded);
3040 err = got_error_set_errno(errcode,
3041 "pthread_cond_signal");
3042 pthread_mutex_unlock(&tog_mutex);
3047 a->commits_needed = 0;
3049 if (a->commits_needed == 0 && !a->load_all) {
3050 errcode = pthread_cond_wait(&a->need_commits,
3053 err = got_error_set_errno(errcode,
3054 "pthread_cond_wait");
3055 pthread_mutex_unlock(&tog_mutex);
3063 a->log_complete = 1;
3064 errcode = pthread_mutex_unlock(&tog_mutex);
3066 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3069 tog_thread_error = 1;
3070 pthread_cond_signal(&a->commit_loaded);
3075 static const struct got_error *
3076 stop_log_thread(struct tog_log_view_state *s)
3078 const struct got_error *err = NULL, *thread_err = NULL;
3083 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3085 return got_error_set_errno(errcode,
3086 "pthread_cond_signal");
3087 errcode = pthread_mutex_unlock(&tog_mutex);
3089 return got_error_set_errno(errcode,
3090 "pthread_mutex_unlock");
3091 errcode = pthread_join(s->thread, (void **)&thread_err);
3093 return got_error_set_errno(errcode, "pthread_join");
3094 errcode = pthread_mutex_lock(&tog_mutex);
3096 return got_error_set_errno(errcode,
3097 "pthread_mutex_lock");
3098 s->thread = 0; //NULL;
3101 if (s->thread_args.repo) {
3102 err = got_repo_close(s->thread_args.repo);
3103 s->thread_args.repo = NULL;
3106 if (s->thread_args.pack_fds) {
3107 const struct got_error *pack_err =
3108 got_repo_pack_fds_close(s->thread_args.pack_fds);
3111 s->thread_args.pack_fds = NULL;
3114 if (s->thread_args.graph) {
3115 got_commit_graph_close(s->thread_args.graph);
3116 s->thread_args.graph = NULL;
3119 return err ? err : thread_err;
3122 static const struct got_error *
3123 close_log_view(struct tog_view *view)
3125 const struct got_error *err = NULL;
3126 struct tog_log_view_state *s = &view->state.log;
3129 err = stop_log_thread(s);
3131 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3132 if (errcode && err == NULL)
3133 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3135 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3136 if (errcode && err == NULL)
3137 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3139 free_commits(&s->limit_commits);
3140 free_commits(&s->real_commits);
3141 free(s->in_repo_path);
3142 s->in_repo_path = NULL;
3145 free(s->head_ref_name);
3146 s->head_ref_name = NULL;
3151 * We use two queues to implement the limit feature: first consists of
3152 * commits matching the current limit_regex; second is the real queue
3153 * of all known commits (real_commits). When the user starts limiting,
3154 * we swap queues such that all movement and displaying functionality
3155 * works with very slight change.
3157 static const struct got_error *
3158 limit_log_view(struct tog_view *view)
3160 struct tog_log_view_state *s = &view->state.log;
3161 struct commit_queue_entry *entry;
3162 struct tog_view *v = view;
3163 const struct got_error *err = NULL;
3167 if (view_is_hsplit_top(view))
3169 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3172 /* Get the pattern */
3173 wmove(v->window, v->nlines - 1, 0);
3174 wclrtoeol(v->window);
3175 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3176 nodelay(v->window, FALSE);
3179 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3182 nodelay(v->window, TRUE);
3186 if (*pattern == '\0') {
3188 * Safety measure for the situation where the user
3189 * resets limit without previously limiting anything.
3195 * User could have pressed Ctrl+L, which refreshed the
3196 * commit queues, it means we can't save previously
3197 * (before limit took place) displayed entries,
3198 * because they would point to already free'ed memory,
3199 * so we are forced to always select first entry of
3202 s->commits = &s->real_commits;
3203 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3204 s->selected_entry = s->first_displayed_entry;
3211 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3216 /* Clear the screen while loading limit view */
3217 s->first_displayed_entry = NULL;
3218 s->last_displayed_entry = NULL;
3219 s->selected_entry = NULL;
3220 s->commits = &s->limit_commits;
3222 /* Prepare limit queue for new search */
3223 free_commits(&s->limit_commits);
3224 s->limit_commits.ncommits = 0;
3226 /* First process commits, which are in queue already */
3227 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3230 err = match_commit(&have_match, entry->id,
3231 entry->commit, &s->limit_regex);
3236 struct commit_queue_entry *matched;
3238 matched = alloc_commit_queue_entry(entry->commit,
3240 if (matched == NULL) {
3241 err = got_error_from_errno(
3242 "alloc_commit_queue_entry");
3245 matched->commit = entry->commit;
3246 got_object_commit_retain(entry->commit);
3248 matched->idx = s->limit_commits.ncommits;
3249 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3251 s->limit_commits.ncommits++;
3255 /* Second process all the commits, until we fill the screen */
3256 if (s->limit_commits.ncommits < view->nlines - 1 &&
3257 !s->thread_args.log_complete) {
3258 s->thread_args.commits_needed +=
3259 view->nlines - s->limit_commits.ncommits - 1;
3260 err = trigger_log_thread(view, 1);
3265 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3266 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3272 static const struct got_error *
3273 search_start_log_view(struct tog_view *view)
3275 struct tog_log_view_state *s = &view->state.log;
3277 s->matched_entry = NULL;
3278 s->search_entry = NULL;
3282 static const struct got_error *
3283 search_next_log_view(struct tog_view *view)
3285 const struct got_error *err = NULL;
3286 struct tog_log_view_state *s = &view->state.log;
3287 struct commit_queue_entry *entry;
3289 /* Display progress update in log view. */
3290 show_log_view(view);
3294 if (s->search_entry) {
3296 errcode = pthread_mutex_unlock(&tog_mutex);
3298 return got_error_set_errno(errcode,
3299 "pthread_mutex_unlock");
3300 ch = wgetch(view->window);
3301 errcode = pthread_mutex_lock(&tog_mutex);
3303 return got_error_set_errno(errcode,
3304 "pthread_mutex_lock");
3305 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3306 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3309 if (view->searching == TOG_SEARCH_FORWARD)
3310 entry = TAILQ_NEXT(s->search_entry, entry);
3312 entry = TAILQ_PREV(s->search_entry,
3313 commit_queue_head, entry);
3314 } else if (s->matched_entry) {
3316 * If the user has moved the cursor after we hit a match,
3317 * the position from where we should continue searching
3318 * might have changed.
3320 if (view->searching == TOG_SEARCH_FORWARD)
3321 entry = TAILQ_NEXT(s->selected_entry, entry);
3323 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3326 entry = s->selected_entry;
3332 if (entry == NULL) {
3333 if (s->thread_args.log_complete ||
3334 view->searching == TOG_SEARCH_BACKWARD) {
3335 view->search_next_done =
3336 (s->matched_entry == NULL ?
3337 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3338 s->search_entry = NULL;
3342 * Poke the log thread for more commits and return,
3343 * allowing the main loop to make progress. Search
3344 * will resume at s->search_entry once we come back.
3346 s->thread_args.commits_needed++;
3347 return trigger_log_thread(view, 0);
3350 err = match_commit(&have_match, entry->id, entry->commit,
3355 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3356 s->matched_entry = entry;
3360 s->search_entry = entry;
3361 if (view->searching == TOG_SEARCH_FORWARD)
3362 entry = TAILQ_NEXT(entry, entry);
3364 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3367 if (s->matched_entry) {
3368 int cur = s->selected_entry->idx;
3369 while (cur < s->matched_entry->idx) {
3370 err = input_log_view(NULL, view, KEY_DOWN);
3375 while (cur > s->matched_entry->idx) {
3376 err = input_log_view(NULL, view, KEY_UP);
3383 s->search_entry = NULL;
3388 static const struct got_error *
3389 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3390 struct got_repository *repo, const char *head_ref_name,
3391 const char *in_repo_path, int log_branches)
3393 const struct got_error *err = NULL;
3394 struct tog_log_view_state *s = &view->state.log;
3395 struct got_repository *thread_repo = NULL;
3396 struct got_commit_graph *thread_graph = NULL;
3399 if (in_repo_path != s->in_repo_path) {
3400 free(s->in_repo_path);
3401 s->in_repo_path = strdup(in_repo_path);
3402 if (s->in_repo_path == NULL)
3403 return got_error_from_errno("strdup");
3406 /* The commit queue only contains commits being displayed. */
3407 TAILQ_INIT(&s->real_commits.head);
3408 s->real_commits.ncommits = 0;
3409 s->commits = &s->real_commits;
3411 TAILQ_INIT(&s->limit_commits.head);
3413 s->limit_commits.ncommits = 0;
3416 if (head_ref_name) {
3417 s->head_ref_name = strdup(head_ref_name);
3418 if (s->head_ref_name == NULL) {
3419 err = got_error_from_errno("strdup");
3423 s->start_id = got_object_id_dup(start_id);
3424 if (s->start_id == NULL) {
3425 err = got_error_from_errno("got_object_id_dup");
3428 s->log_branches = log_branches;
3429 s->use_committer = 1;
3431 STAILQ_INIT(&s->colors);
3432 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3433 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3434 get_color_value("TOG_COLOR_COMMIT"));
3437 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3438 get_color_value("TOG_COLOR_AUTHOR"));
3440 free_colors(&s->colors);
3443 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3444 get_color_value("TOG_COLOR_DATE"));
3446 free_colors(&s->colors);
3451 view->show = show_log_view;
3452 view->input = input_log_view;
3453 view->resize = resize_log_view;
3454 view->close = close_log_view;
3455 view->search_start = search_start_log_view;
3456 view->search_next = search_next_log_view;
3458 if (s->thread_args.pack_fds == NULL) {
3459 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3463 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3464 s->thread_args.pack_fds);
3467 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3471 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3472 s->repo, NULL, NULL);
3476 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3478 err = got_error_set_errno(errcode, "pthread_cond_init");
3481 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3483 err = got_error_set_errno(errcode, "pthread_cond_init");
3487 s->thread_args.commits_needed = view->nlines;
3488 s->thread_args.graph = thread_graph;
3489 s->thread_args.real_commits = &s->real_commits;
3490 s->thread_args.limit_commits = &s->limit_commits;
3491 s->thread_args.in_repo_path = s->in_repo_path;
3492 s->thread_args.start_id = s->start_id;
3493 s->thread_args.repo = thread_repo;
3494 s->thread_args.log_complete = 0;
3495 s->thread_args.quit = &s->quit;
3496 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3497 s->thread_args.selected_entry = &s->selected_entry;
3498 s->thread_args.searching = &view->searching;
3499 s->thread_args.search_next_done = &view->search_next_done;
3500 s->thread_args.regex = &view->regex;
3501 s->thread_args.limiting = &s->limit_view;
3502 s->thread_args.limit_regex = &s->limit_regex;
3503 s->thread_args.limit_commits = &s->limit_commits;
3506 close_log_view(view);
3510 static const struct got_error *
3511 show_log_view(struct tog_view *view)
3513 const struct got_error *err;
3514 struct tog_log_view_state *s = &view->state.log;
3516 if (s->thread == 0) { //NULL) {
3517 int errcode = pthread_create(&s->thread, NULL, log_thread,
3520 return got_error_set_errno(errcode, "pthread_create");
3521 if (s->thread_args.commits_needed > 0) {
3522 err = trigger_log_thread(view, 1);
3528 return draw_commits(view);
3532 log_move_cursor_up(struct tog_view *view, int page, int home)
3534 struct tog_log_view_state *s = &view->state.log;
3536 if (s->first_displayed_entry == NULL)
3538 if (s->selected_entry->idx == 0)
3541 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3543 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3545 if (!page && !home && s->selected > 0)
3548 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3554 static const struct got_error *
3555 log_move_cursor_down(struct tog_view *view, int page)
3557 struct tog_log_view_state *s = &view->state.log;
3558 const struct got_error *err = NULL;
3559 int eos = view->nlines - 2;
3561 if (s->first_displayed_entry == NULL)
3564 if (s->thread_args.log_complete &&
3565 s->selected_entry->idx >= s->commits->ncommits - 1)
3568 if (view_is_hsplit_top(view))
3569 --eos; /* border consumes the last line */
3572 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3575 err = log_scroll_down(view, 1);
3576 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3577 struct commit_queue_entry *entry;
3581 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3582 s->last_displayed_entry = entry;
3583 for (n = 0; n <= eos; n++) {
3586 s->first_displayed_entry = entry;
3587 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3590 s->selected = n - 1;
3592 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3593 s->thread_args.log_complete)
3594 s->selected += MIN(page,
3595 s->commits->ncommits - s->selected_entry->idx - 1);
3597 err = log_scroll_down(view, page);
3603 * We might necessarily overshoot in horizontal
3604 * splits; if so, select the last displayed commit.
3606 if (s->first_displayed_entry && s->last_displayed_entry) {
3607 s->selected = MIN(s->selected,
3608 s->last_displayed_entry->idx -
3609 s->first_displayed_entry->idx);
3614 if (s->thread_args.log_complete &&
3615 s->selected_entry->idx == s->commits->ncommits - 1)
3622 view_get_split(struct tog_view *view, int *y, int *x)
3627 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3628 if (view->child && view->child->resized_y)
3629 *y = view->child->resized_y;
3630 else if (view->resized_y)
3631 *y = view->resized_y;
3633 *y = view_split_begin_y(view->lines);
3634 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3635 if (view->child && view->child->resized_x)
3636 *x = view->child->resized_x;
3637 else if (view->resized_x)
3638 *x = view->resized_x;
3640 *x = view_split_begin_x(view->begin_x);
3644 /* Split view horizontally at y and offset view->state->selected line. */
3645 static const struct got_error *
3646 view_init_hsplit(struct tog_view *view, int y)
3648 const struct got_error *err = NULL;
3652 err = view_resize(view);
3656 err = offset_selection_down(view);
3661 static const struct got_error *
3662 log_goto_line(struct tog_view *view, int nlines)
3664 const struct got_error *err = NULL;
3665 struct tog_log_view_state *s = &view->state.log;
3666 int g, idx = s->selected_entry->idx;
3668 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3674 if (g >= s->first_displayed_entry->idx + 1 &&
3675 g <= s->last_displayed_entry->idx + 1 &&
3676 g - s->first_displayed_entry->idx - 1 < nlines) {
3677 s->selected = g - s->first_displayed_entry->idx - 1;
3683 err = log_move_cursor_down(view, g - idx - 1);
3684 if (!err && g > s->selected_entry->idx + 1)
3685 err = log_move_cursor_down(view,
3686 g - s->first_displayed_entry->idx - 1);
3689 } else if (idx + 1 > g)
3690 log_move_cursor_up(view, idx - g + 1, 0);
3692 if (g < nlines && s->first_displayed_entry->idx == 0)
3693 s->selected = g - 1;
3700 static const struct got_error *
3701 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3703 const struct got_error *err = NULL;
3704 struct tog_log_view_state *s = &view->state.log;
3707 if (s->thread_args.load_all) {
3708 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3709 s->thread_args.load_all = 0;
3710 else if (s->thread_args.log_complete) {
3711 err = log_move_cursor_down(view, s->commits->ncommits);
3712 s->thread_args.load_all = 0;
3718 eos = nscroll = view->nlines - 1;
3719 if (view_is_hsplit_top(view))
3723 return log_goto_line(view, eos);
3727 err = limit_log_view(view);
3736 view->x = MAX(view->maxx - view->ncols / 2, 0);
3741 if (view->x + view->ncols / 2 < view->maxx)
3742 view->x += 2; /* move two columns right */
3748 view->x -= MIN(view->x, 2); /* move two columns back */
3757 log_move_cursor_up(view, 0, 0);
3762 log_move_cursor_up(view, 0, 1);
3772 log_move_cursor_up(view, nscroll, 0);
3779 err = log_move_cursor_down(view, 0);
3782 s->use_committer = !s->use_committer;
3783 view->action = s->use_committer ?
3784 "show committer" : "show commit author";
3789 /* We don't know yet how many commits, so we're forced to
3790 * traverse them all. */
3792 s->thread_args.load_all = 1;
3793 if (!s->thread_args.log_complete)
3794 return trigger_log_thread(view, 0);
3795 err = log_move_cursor_down(view, s->commits->ncommits);
3796 s->thread_args.load_all = 0;
3807 err = log_move_cursor_down(view, nscroll);
3810 if (s->selected > view->nlines - 2)
3811 s->selected = view->nlines - 2;
3812 if (s->selected > s->commits->ncommits - 1)
3813 s->selected = s->commits->ncommits - 1;
3815 if (s->commits->ncommits < view->nlines - 1 &&
3816 !s->thread_args.log_complete) {
3817 s->thread_args.commits_needed += (view->nlines - 1) -
3818 s->commits->ncommits;
3819 err = trigger_log_thread(view, 1);
3825 if (s->selected_entry == NULL)
3827 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3831 if (s->selected_entry == NULL)
3833 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3839 if (ch == KEY_BACKSPACE &&
3840 got_path_is_root_dir(s->in_repo_path))
3842 err = stop_log_thread(s);
3845 if (ch == KEY_BACKSPACE) {
3847 err = got_path_dirname(&parent_path, s->in_repo_path);
3850 free(s->in_repo_path);
3851 s->in_repo_path = parent_path;
3852 s->thread_args.in_repo_path = s->in_repo_path;
3853 } else if (ch == CTRL('l')) {
3854 struct got_object_id *start_id;
3855 err = got_repo_match_object_id(&start_id, NULL,
3856 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3857 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3859 if (s->head_ref_name == NULL ||
3860 err->code != GOT_ERR_NOT_REF)
3862 /* Try to cope with deleted references. */
3863 free(s->head_ref_name);
3864 s->head_ref_name = NULL;
3865 err = got_repo_match_object_id(&start_id,
3866 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3867 &tog_refs, s->repo);
3872 s->start_id = start_id;
3873 s->thread_args.start_id = s->start_id;
3875 s->log_branches = !s->log_branches;
3877 if (s->thread_args.pack_fds == NULL) {
3878 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3882 err = got_repo_open(&s->thread_args.repo,
3883 got_repo_get_path(s->repo), NULL,
3884 s->thread_args.pack_fds);
3888 err = tog_load_refs(s->repo, 0);
3891 err = got_commit_graph_open(&s->thread_args.graph,
3892 s->in_repo_path, !s->log_branches);
3895 err = got_commit_graph_iter_start(s->thread_args.graph,
3896 s->start_id, s->repo, NULL, NULL);
3899 free_commits(&s->real_commits);
3900 free_commits(&s->limit_commits);
3901 s->first_displayed_entry = NULL;
3902 s->last_displayed_entry = NULL;
3903 s->selected_entry = NULL;
3905 s->thread_args.log_complete = 0;
3907 s->thread_args.commits_needed = view->lines;
3908 s->matched_entry = NULL;
3909 s->search_entry = NULL;
3914 err = view_request_new(new_view, view, TOG_VIEW_REF);
3924 static const struct got_error *
3925 apply_unveil(const char *repo_path, const char *worktree_path)
3927 const struct got_error *error;
3930 if (unveil("gmon.out", "rwc") != 0)
3931 return got_error_from_errno2("unveil", "gmon.out");
3933 if (repo_path && unveil(repo_path, "r") != 0)
3934 return got_error_from_errno2("unveil", repo_path);
3936 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3937 return got_error_from_errno2("unveil", worktree_path);
3939 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3940 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3942 error = got_privsep_unveil_exec_helpers();
3946 if (unveil(NULL, NULL) != 0)
3947 return got_error_from_errno("unveil");
3956 * Override default signal handlers before starting ncurses.
3957 * This should prevent ncurses from installing its own
3958 * broken cleanup() signal handler.
3960 signal(SIGWINCH, tog_sigwinch);
3961 signal(SIGPIPE, tog_sigpipe);
3962 signal(SIGCONT, tog_sigcont);
3963 signal(SIGINT, tog_sigint);
3964 signal(SIGTERM, tog_sigterm);
3968 halfdelay(1); /* Do fast refresh while initial view is loading. */
3971 intrflush(stdscr, FALSE);
3972 keypad(stdscr, TRUE);
3974 if (getenv("TOG_COLORS") != NULL) {
3976 use_default_colors();
3980 static const struct got_error *
3981 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3982 struct got_repository *repo, struct got_worktree *worktree)
3984 const struct got_error *err = NULL;
3987 *in_repo_path = strdup("/");
3988 if (*in_repo_path == NULL)
3989 return got_error_from_errno("strdup");
3994 const char *prefix = got_worktree_get_path_prefix(worktree);
3997 err = got_worktree_resolve_path(&p, worktree, argv[0]);
4000 if (asprintf(in_repo_path, "%s%s%s", prefix,
4001 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4003 err = got_error_from_errno("asprintf");
4004 *in_repo_path = NULL;
4008 err = got_repo_map_path(in_repo_path, repo, argv[0]);
4013 static const struct got_error *
4014 cmd_log(int argc, char *argv[])
4016 const struct got_error *error;
4017 struct got_repository *repo = NULL;
4018 struct got_worktree *worktree = NULL;
4019 struct got_object_id *start_id = NULL;
4020 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4021 char *start_commit = NULL, *label = NULL;
4022 struct got_reference *ref = NULL;
4023 const char *head_ref_name = NULL;
4024 int ch, log_branches = 0;
4025 struct tog_view *view;
4026 int *pack_fds = NULL;
4028 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4034 start_commit = optarg;
4037 repo_path = realpath(optarg, NULL);
4038 if (repo_path == NULL)
4039 return got_error_from_errno2("realpath",
4054 error = got_repo_pack_fds_open(&pack_fds);
4058 if (repo_path == NULL) {
4059 cwd = getcwd(NULL, 0);
4061 return got_error_from_errno("getcwd");
4062 error = got_worktree_open(&worktree, cwd);
4063 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4067 strdup(got_worktree_get_repo_path(worktree));
4069 repo_path = strdup(cwd);
4070 if (repo_path == NULL) {
4071 error = got_error_from_errno("strdup");
4076 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4080 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4087 error = apply_unveil(got_repo_get_path(repo),
4088 worktree ? got_worktree_get_root_path(worktree) : NULL);
4092 /* already loaded by tog_log_with_path()? */
4093 if (TAILQ_EMPTY(&tog_refs)) {
4094 error = tog_load_refs(repo, 0);
4099 if (start_commit == NULL) {
4100 error = got_repo_match_object_id(&start_id, &label,
4101 worktree ? got_worktree_get_head_ref_name(worktree) :
4102 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4105 head_ref_name = label;
4107 error = got_ref_open(&ref, repo, start_commit, 0);
4109 head_ref_name = got_ref_get_name(ref);
4110 else if (error->code != GOT_ERR_NOT_REF)
4112 error = got_repo_match_object_id(&start_id, NULL,
4113 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4118 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4120 error = got_error_from_errno("view_open");
4123 error = open_log_view(view, start_id, repo, head_ref_name,
4124 in_repo_path, log_branches);
4128 /* Release work tree lock. */
4129 got_worktree_close(worktree);
4132 error = view_loop(view);
4142 const struct got_error *close_err = got_repo_close(repo);
4147 got_worktree_close(worktree);
4149 const struct got_error *pack_err =
4150 got_repo_pack_fds_close(pack_fds);
4162 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4163 "object1 object2\n", getprogname());
4168 match_line(const char *line, regex_t *regex, size_t nmatch,
4169 regmatch_t *regmatch)
4171 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4174 static struct tog_color *
4175 match_color(struct tog_colors *colors, const char *line)
4177 struct tog_color *tc = NULL;
4179 STAILQ_FOREACH(tc, colors, entry) {
4180 if (match_line(line, &tc->regex, 0, NULL))
4187 static const struct got_error *
4188 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4189 WINDOW *window, int skipcol, regmatch_t *regmatch)
4191 const struct got_error *err = NULL;
4193 wchar_t *wline = NULL;
4194 int rme, rms, n, width, scrollx;
4195 int width0 = 0, width1 = 0, width2 = 0;
4196 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4200 rms = regmatch->rm_so;
4201 rme = regmatch->rm_eo;
4203 err = expand_tab(&exstr, line);
4207 /* Split the line into 3 segments, according to match offsets. */
4208 seg0 = strndup(exstr, rms);
4210 err = got_error_from_errno("strndup");
4213 seg1 = strndup(exstr + rms, rme - rms);
4215 err = got_error_from_errno("strndup");
4218 seg2 = strdup(exstr + rme);
4220 err = got_error_from_errno("strndup");
4224 /* draw up to matched token if we haven't scrolled past it */
4225 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4229 n = MAX(width0 - skipcol, 0);
4232 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4233 wlimit, col_tab_align, 1);
4236 waddwstr(window, &wline[scrollx]);
4246 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4250 wlen = wcslen(wline);
4252 width = wcwidth(wline[i]);
4254 /* should not happen, tabs are expanded */
4255 err = got_error(GOT_ERR_RANGE);
4258 if (width0 + w + width > skipcol)
4263 /* draw (visible part of) matched token (if scrolled into it) */
4264 if (width1 - w > 0) {
4265 wattron(window, A_STANDOUT);
4266 waddwstr(window, &wline[i]);
4267 wattroff(window, A_STANDOUT);
4268 wlimit -= (width1 - w);
4269 *wtotal += (width1 - w);
4273 if (wlimit > 0) { /* draw rest of line */
4275 if (skipcol > width0 + width1) {
4276 err = format_line(&wline, &width2, &scrollx, seg2,
4277 skipcol - (width0 + width1), wlimit,
4281 waddwstr(window, &wline[scrollx]);
4283 err = format_line(&wline, &width2, NULL, seg2, 0,
4284 wlimit, col_tab_align, 1);
4287 waddwstr(window, wline);
4301 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4304 int *eof, *first, *selected;
4306 if (view->type == TOG_VIEW_DIFF) {
4307 struct tog_diff_view_state *s = &view->state.diff;
4309 first = &s->first_displayed_line;
4313 } else if (view->type == TOG_VIEW_HELP) {
4314 struct tog_help_view_state *s = &view->state.help;
4316 first = &s->first_displayed_line;
4320 } else if (view->type == TOG_VIEW_BLAME) {
4321 struct tog_blame_view_state *s = &view->state.blame;
4323 first = &s->first_displayed_line;
4324 selected = &s->selected_line;
4330 /* Center gline in the middle of the page like vi(1). */
4331 if (*lineno < view->gline - (view->nlines - 3) / 2)
4333 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4342 *selected = view->gline <= (view->nlines - 3) / 2 ?
4343 view->gline : (view->nlines - 3) / 2 + 1;
4349 static const struct got_error *
4350 draw_file(struct tog_view *view, const char *header)
4352 struct tog_diff_view_state *s = &view->state.diff;
4353 regmatch_t *regmatch = &view->regmatch;
4354 const struct got_error *err;
4357 size_t linesize = 0;
4361 int max_lines = view->nlines;
4362 int nlines = s->nlines;
4365 s->lineno = s->first_displayed_line - 1;
4366 line_offset = s->lines[s->first_displayed_line - 1].offset;
4367 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4368 return got_error_from_errno("fseek");
4370 werase(view->window);
4372 if (view->gline > s->nlines - 1)
4373 view->gline = s->nlines - 1;
4376 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4377 1 : view->gline - (view->nlines - 3) / 2 :
4378 s->lineno + s->selected_line;
4380 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4381 return got_error_from_errno("asprintf");
4382 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4388 if (view_needs_focus_indication(view))
4389 wstandout(view->window);
4390 waddwstr(view->window, wline);
4393 while (width++ < view->ncols)
4394 waddch(view->window, ' ');
4395 if (view_needs_focus_indication(view))
4396 wstandend(view->window);
4406 while (max_lines > 0 && nprinted < max_lines) {
4407 enum got_diff_line_type linetype;
4410 linelen = getline(&line, &linesize, s->f);
4411 if (linelen == -1) {
4417 return got_ferror(s->f, GOT_ERR_IO);
4420 if (++s->lineno < s->first_displayed_line)
4422 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4424 if (s->lineno == view->hiline)
4427 /* Set view->maxx based on full line length. */
4428 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4434 view->maxx = MAX(view->maxx, width);
4438 linetype = s->lines[s->lineno].type;
4439 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4440 linetype < GOT_DIFF_LINE_CONTEXT)
4441 attr |= COLOR_PAIR(linetype);
4443 wattron(view->window, attr);
4444 if (s->first_displayed_line + nprinted == s->matched_line &&
4445 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4446 err = add_matched_line(&width, line, view->ncols, 0,
4447 view->window, view->x, regmatch);
4454 err = format_line(&wline, &width, &skip, line,
4455 view->x, view->ncols, 0, view->x ? 1 : 0);
4460 waddwstr(view->window, &wline[skip]);
4464 if (s->lineno == view->hiline) {
4465 /* highlight full gline length */
4466 while (width++ < view->ncols)
4467 waddch(view->window, ' ');
4469 if (width <= view->ncols - 1)
4470 waddch(view->window, '\n');
4473 wattroff(view->window, attr);
4474 if (++nprinted == 1)
4475 s->first_displayed_line = s->lineno;
4479 s->last_displayed_line = s->first_displayed_line +
4482 s->last_displayed_line = s->first_displayed_line;
4487 while (nprinted < view->nlines) {
4488 waddch(view->window, '\n');
4492 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4498 wstandout(view->window);
4499 waddwstr(view->window, wline);
4502 wstandend(view->window);
4509 get_datestr(time_t *time, char *datebuf)
4511 struct tm mytm, *tm;
4514 tm = gmtime_r(time, &mytm);
4517 s = asctime_r(tm, datebuf);
4520 p = strchr(s, '\n');
4526 static const struct got_error *
4527 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4528 off_t off, uint8_t type)
4530 struct got_diff_line *p;
4532 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4534 return got_error_from_errno("reallocarray");
4536 (*lines)[*nlines].offset = off;
4537 (*lines)[*nlines].type = type;
4543 static const struct got_error *
4544 cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4545 struct got_diff_line *s_lines, size_t s_nlines)
4547 struct got_diff_line *p;
4551 if (fseeko(src, 0L, SEEK_SET) == -1)
4552 return got_error_from_errno("fseeko");
4555 r = fread(buf, 1, sizeof(buf), src);
4558 return got_error_from_errno("fread");
4562 if (fwrite(buf, 1, r, dst) != r)
4563 return got_ferror(dst, GOT_ERR_IO);
4567 * The diff driver initialises the first line at offset zero when the
4568 * array isn't prepopulated, skip it; we already have it in *d_lines.
4570 for (i = 1; i < s_nlines; ++i)
4571 s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4575 p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4577 /* d_lines is freed in close_diff_view() */
4578 return got_error_from_errno("reallocarray");
4583 memcpy(*d_lines + *d_nlines, s_lines + 1, s_nlines * sizeof(*s_lines));
4584 *d_nlines += s_nlines;
4589 static const struct got_error *
4590 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4591 struct got_object_id *commit_id, struct got_reflist_head *refs,
4592 struct got_repository *repo, int ignore_ws, int force_text_diff,
4593 struct got_diffstat_cb_arg *dsa, FILE *outfile)
4595 const struct got_error *err = NULL;
4596 char datebuf[26], *datestr;
4597 struct got_commit_object *commit;
4598 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4599 time_t committer_time;
4600 const char *author, *committer;
4601 char *refs_str = NULL;
4602 struct got_pathlist_entry *pe;
4607 err = build_refs_str(&refs_str, refs, commit_id, repo);
4612 err = got_object_open_as_commit(&commit, repo, commit_id);
4616 err = got_object_id_str(&id_str, commit_id);
4618 err = got_error_from_errno("got_object_id_str");
4622 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4626 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4627 refs_str ? refs_str : "", refs_str ? ")" : "");
4629 err = got_error_from_errno("fprintf");
4633 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4637 n = fprintf(outfile, "from: %s\n",
4638 got_object_commit_get_author(commit));
4640 err = got_error_from_errno("fprintf");
4644 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4648 author = got_object_commit_get_author(commit);
4649 committer = got_object_commit_get_committer(commit);
4650 if (strcmp(author, committer) != 0) {
4651 n = fprintf(outfile, "via: %s\n", committer);
4653 err = got_error_from_errno("fprintf");
4657 err = add_line_metadata(lines, nlines, outoff,
4658 GOT_DIFF_LINE_AUTHOR);
4662 committer_time = got_object_commit_get_committer_time(commit);
4663 datestr = get_datestr(&committer_time, datebuf);
4665 n = fprintf(outfile, "date: %s UTC\n", datestr);
4667 err = got_error_from_errno("fprintf");
4671 err = add_line_metadata(lines, nlines, outoff,
4672 GOT_DIFF_LINE_DATE);
4676 if (got_object_commit_get_nparents(commit) > 1) {
4677 const struct got_object_id_queue *parent_ids;
4678 struct got_object_qid *qid;
4680 parent_ids = got_object_commit_get_parent_ids(commit);
4681 STAILQ_FOREACH(qid, parent_ids, entry) {
4682 err = got_object_id_str(&id_str, &qid->id);
4685 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4687 err = got_error_from_errno("fprintf");
4691 err = add_line_metadata(lines, nlines, outoff,
4692 GOT_DIFF_LINE_META);
4700 err = got_object_commit_get_logmsg(&logmsg, commit);
4704 while ((line = strsep(&s, "\n")) != NULL) {
4705 n = fprintf(outfile, "%s\n", line);
4707 err = got_error_from_errno("fprintf");
4711 err = add_line_metadata(lines, nlines, outoff,
4712 GOT_DIFF_LINE_LOGMSG);
4717 TAILQ_FOREACH(pe, dsa->paths, entry) {
4718 struct got_diff_changed_path *cp = pe->data;
4719 int pad = dsa->max_path_len - pe->path_len + 1;
4721 n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
4722 pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
4723 dsa->rm_cols + 1, cp->rm);
4725 err = got_error_from_errno("fprintf");
4729 err = add_line_metadata(lines, nlines, outoff,
4730 GOT_DIFF_LINE_CHANGES);
4735 fputc('\n', outfile);
4737 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4741 n = fprintf(outfile,
4742 "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
4743 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4744 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4746 err = got_error_from_errno("fprintf");
4750 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4754 fputc('\n', outfile);
4756 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4761 got_object_commit_close(commit);
4770 static const struct got_error *
4771 create_diff(struct tog_diff_view_state *s)
4773 const struct got_error *err = NULL;
4774 FILE *f = NULL, *tmp_diff_file = NULL;
4776 struct got_diff_line *lines = NULL;
4777 struct got_pathlist_head changed_paths;
4779 TAILQ_INIT(&changed_paths);
4782 s->lines = malloc(sizeof(*s->lines));
4783 if (s->lines == NULL)
4784 return got_error_from_errno("malloc");
4789 err = got_error_from_errno("got_opentemp");
4792 tmp_diff_file = got_opentemp();
4793 if (tmp_diff_file == NULL) {
4794 err = got_error_from_errno("got_opentemp");
4797 if (s->f && fclose(s->f) == EOF) {
4798 err = got_error_from_errno("fclose");
4804 err = got_object_get_type(&obj_type, s->repo, s->id1);
4806 err = got_object_get_type(&obj_type, s->repo, s->id2);
4811 case GOT_OBJ_TYPE_BLOB:
4812 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4813 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4814 s->label1, s->label2, tog_diff_algo, s->diff_context,
4815 s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
4818 case GOT_OBJ_TYPE_TREE:
4819 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4820 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4821 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4822 s->force_text_diff, NULL, s->repo, s->f);
4824 case GOT_OBJ_TYPE_COMMIT: {
4825 const struct got_object_id_queue *parent_ids;
4826 struct got_object_qid *pid;
4827 struct got_commit_object *commit2;
4828 struct got_reflist_head *refs;
4830 struct got_diffstat_cb_arg dsa = {
4833 s->ignore_whitespace,
4838 lines = malloc(sizeof(*lines));
4839 if (lines == NULL) {
4840 err = got_error_from_errno("malloc");
4844 /* build diff first in tmp file then append to commit info */
4845 err = got_diff_objects_as_commits(&lines, &nlines,
4846 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4847 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4848 s->force_text_diff, &dsa, s->repo, tmp_diff_file);
4852 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4855 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4856 /* Show commit info if we're diffing to a parent/root commit. */
4857 if (s->id1 == NULL) {
4858 err = write_commit_info(&s->lines, &s->nlines, s->id2,
4859 refs, s->repo, s->ignore_whitespace,
4860 s->force_text_diff, &dsa, s->f);
4864 parent_ids = got_object_commit_get_parent_ids(commit2);
4865 STAILQ_FOREACH(pid, parent_ids, entry) {
4866 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4867 err = write_commit_info(&s->lines,
4868 &s->nlines, s->id2, refs, s->repo,
4869 s->ignore_whitespace,
4870 s->force_text_diff, &dsa, s->f);
4877 got_object_commit_close(commit2);
4879 err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
4884 err = got_error(GOT_ERR_OBJ_TYPE);
4889 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4890 if (s->f && fflush(s->f) != 0 && err == NULL)
4891 err = got_error_from_errno("fflush");
4892 if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
4893 err = got_error_from_errno("fclose");
4898 diff_view_indicate_progress(struct tog_view *view)
4900 mvwaddstr(view->window, 0, 0, "diffing...");
4905 static const struct got_error *
4906 search_start_diff_view(struct tog_view *view)
4908 struct tog_diff_view_state *s = &view->state.diff;
4910 s->matched_line = 0;
4915 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4916 size_t *nlines, int **first, int **last, int **match, int **selected)
4918 struct tog_diff_view_state *s = &view->state.diff;
4921 *nlines = s->nlines;
4922 *line_offsets = NULL;
4923 *match = &s->matched_line;
4924 *first = &s->first_displayed_line;
4925 *last = &s->last_displayed_line;
4926 *selected = &s->selected_line;
4929 static const struct got_error *
4930 search_next_view_match(struct tog_view *view)
4932 const struct got_error *err = NULL;
4936 size_t linesize = 0;
4938 off_t *line_offsets;
4940 int *first, *last, *match, *selected;
4942 if (!view->search_setup)
4943 return got_error_msg(GOT_ERR_NOT_IMPL,
4944 "view search not supported");
4945 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4948 if (!view->searching) {
4949 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4954 if (view->searching == TOG_SEARCH_FORWARD)
4955 lineno = *match + 1;
4957 lineno = *match - 1;
4959 lineno = *first - 1 + *selected;
4964 if (lineno <= 0 || lineno > nlines) {
4966 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4970 if (view->searching == TOG_SEARCH_FORWARD)
4976 offset = view->type == TOG_VIEW_DIFF ?
4977 view->state.diff.lines[lineno - 1].offset :
4978 line_offsets[lineno - 1];
4979 if (fseeko(f, offset, SEEK_SET) != 0) {
4981 return got_error_from_errno("fseeko");
4983 linelen = getline(&line, &linesize, f);
4984 if (linelen != -1) {
4986 err = expand_tab(&exstr, line);
4989 if (match_line(exstr, &view->regex, 1,
4991 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4998 if (view->searching == TOG_SEARCH_FORWARD)
5013 static const struct got_error *
5014 close_diff_view(struct tog_view *view)
5016 const struct got_error *err = NULL;
5017 struct tog_diff_view_state *s = &view->state.diff;
5023 if (s->f && fclose(s->f) == EOF)
5024 err = got_error_from_errno("fclose");
5026 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5027 err = got_error_from_errno("fclose");
5029 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5030 err = got_error_from_errno("fclose");
5032 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5033 err = got_error_from_errno("close");
5035 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5036 err = got_error_from_errno("close");
5044 static const struct got_error *
5045 open_diff_view(struct tog_view *view, struct got_object_id *id1,
5046 struct got_object_id *id2, const char *label1, const char *label2,
5047 int diff_context, int ignore_whitespace, int force_text_diff,
5048 struct tog_view *parent_view, struct got_repository *repo)
5050 const struct got_error *err;
5051 struct tog_diff_view_state *s = &view->state.diff;
5053 memset(s, 0, sizeof(*s));
5057 if (id1 != NULL && id2 != NULL) {
5059 err = got_object_get_type(&type1, repo, id1);
5062 err = got_object_get_type(&type2, repo, id2);
5067 return got_error(GOT_ERR_OBJ_TYPE);
5069 s->first_displayed_line = 1;
5070 s->last_displayed_line = view->nlines;
5071 s->selected_line = 1;
5079 s->id1 = got_object_id_dup(id1);
5081 return got_error_from_errno("got_object_id_dup");
5085 s->id2 = got_object_id_dup(id2);
5086 if (s->id2 == NULL) {
5087 err = got_error_from_errno("got_object_id_dup");
5091 s->f1 = got_opentemp();
5092 if (s->f1 == NULL) {
5093 err = got_error_from_errno("got_opentemp");
5097 s->f2 = got_opentemp();
5098 if (s->f2 == NULL) {
5099 err = got_error_from_errno("got_opentemp");
5103 s->fd1 = got_opentempfd();
5105 err = got_error_from_errno("got_opentempfd");
5109 s->fd2 = got_opentempfd();
5111 err = got_error_from_errno("got_opentempfd");
5115 s->diff_context = diff_context;
5116 s->ignore_whitespace = ignore_whitespace;
5117 s->force_text_diff = force_text_diff;
5118 s->parent_view = parent_view;
5121 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5124 rc = init_pair(GOT_DIFF_LINE_MINUS,
5125 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5127 rc = init_pair(GOT_DIFF_LINE_PLUS,
5128 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5130 rc = init_pair(GOT_DIFF_LINE_HUNK,
5131 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5133 rc = init_pair(GOT_DIFF_LINE_META,
5134 get_color_value("TOG_COLOR_DIFF_META"), -1);
5136 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5137 get_color_value("TOG_COLOR_DIFF_META"), -1);
5139 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5140 get_color_value("TOG_COLOR_DIFF_META"), -1);
5142 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5143 get_color_value("TOG_COLOR_DIFF_META"), -1);
5145 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5146 get_color_value("TOG_COLOR_AUTHOR"), -1);
5148 rc = init_pair(GOT_DIFF_LINE_DATE,
5149 get_color_value("TOG_COLOR_DATE"), -1);
5151 err = got_error(GOT_ERR_RANGE);
5156 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5157 view_is_splitscreen(view))
5158 show_log_view(parent_view); /* draw border */
5159 diff_view_indicate_progress(view);
5161 err = create_diff(s);
5163 view->show = show_diff_view;
5164 view->input = input_diff_view;
5165 view->reset = reset_diff_view;
5166 view->close = close_diff_view;
5167 view->search_start = search_start_diff_view;
5168 view->search_setup = search_setup_diff_view;
5169 view->search_next = search_next_view_match;
5172 close_diff_view(view);
5176 static const struct got_error *
5177 show_diff_view(struct tog_view *view)
5179 const struct got_error *err;
5180 struct tog_diff_view_state *s = &view->state.diff;
5181 char *id_str1 = NULL, *id_str2, *header;
5182 const char *label1, *label2;
5185 err = got_object_id_str(&id_str1, s->id1);
5188 label1 = s->label1 ? s->label1 : id_str1;
5190 label1 = "/dev/null";
5192 err = got_object_id_str(&id_str2, s->id2);
5195 label2 = s->label2 ? s->label2 : id_str2;
5197 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5198 err = got_error_from_errno("asprintf");
5206 err = draw_file(view, header);
5211 static const struct got_error *
5212 set_selected_commit(struct tog_diff_view_state *s,
5213 struct commit_queue_entry *entry)
5215 const struct got_error *err;
5216 const struct got_object_id_queue *parent_ids;
5217 struct got_commit_object *selected_commit;
5218 struct got_object_qid *pid;
5221 s->id2 = got_object_id_dup(entry->id);
5223 return got_error_from_errno("got_object_id_dup");
5225 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5228 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5230 pid = STAILQ_FIRST(parent_ids);
5231 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5232 got_object_commit_close(selected_commit);
5236 static const struct got_error *
5237 reset_diff_view(struct tog_view *view)
5239 struct tog_diff_view_state *s = &view->state.diff;
5242 wclear(view->window);
5243 s->first_displayed_line = 1;
5244 s->last_displayed_line = view->nlines;
5245 s->matched_line = 0;
5246 diff_view_indicate_progress(view);
5247 return create_diff(s);
5251 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5255 i = start = s->first_displayed_line - 1;
5257 while (s->lines[i].type != type) {
5261 return; /* do nothing, requested type not in file */
5264 s->selected_line = 1;
5265 s->first_displayed_line = i;
5269 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5273 i = start = s->first_displayed_line + 1;
5275 while (s->lines[i].type != type) {
5276 if (i == s->nlines - 1)
5279 return; /* do nothing, requested type not in file */
5282 s->selected_line = 1;
5283 s->first_displayed_line = i;
5286 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5288 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5291 static const struct got_error *
5292 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5294 const struct got_error *err = NULL;
5295 struct tog_diff_view_state *s = &view->state.diff;
5296 struct tog_log_view_state *ls;
5297 struct commit_queue_entry *old_selected_entry;
5299 size_t linesize = 0;
5301 int i, nscroll = view->nlines - 1, up = 0;
5303 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5310 view->x = MAX(view->maxx - view->ncols / 3, 0);
5315 if (view->x + view->ncols / 3 < view->maxx)
5316 view->x += 2; /* move two columns right */
5322 view->x -= MIN(view->x, 2); /* move two columns back */
5329 s->force_text_diff = !s->force_text_diff;
5330 view->action = s->force_text_diff ?
5331 "force ASCII text enabled" :
5332 "force ASCII text disabled";
5334 else if (ch == 'w') {
5335 s->ignore_whitespace = !s->ignore_whitespace;
5336 view->action = s->ignore_whitespace ?
5337 "ignore whitespace enabled" :
5338 "ignore whitespace disabled";
5340 err = reset_diff_view(view);
5344 s->first_displayed_line = 1;
5353 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5359 if (s->first_displayed_line > 1)
5360 s->first_displayed_line--;
5371 if (s->first_displayed_line == 1) {
5376 while (i++ < nscroll && s->first_displayed_line > 1)
5377 s->first_displayed_line--;
5383 s->first_displayed_line++;
5400 while (!s->eof && i++ < nscroll) {
5401 linelen = getline(&line, &linesize, s->f);
5402 s->first_displayed_line++;
5403 if (linelen == -1) {
5407 err = got_ferror(s->f, GOT_ERR_IO);
5414 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5417 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5420 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5423 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5426 if (s->diff_context > 0) {
5428 s->matched_line = 0;
5429 diff_view_indicate_progress(view);
5430 err = create_diff(s);
5431 if (s->first_displayed_line + view->nlines - 1 >
5433 s->first_displayed_line = 1;
5434 s->last_displayed_line = view->nlines;
5440 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5442 s->matched_line = 0;
5443 diff_view_indicate_progress(view);
5444 err = create_diff(s);
5456 if (s->parent_view == NULL) {
5460 s->parent_view->count = view->count;
5462 if (s->parent_view->type == TOG_VIEW_LOG) {
5463 ls = &s->parent_view->state.log;
5464 old_selected_entry = ls->selected_entry;
5466 err = input_log_view(NULL, s->parent_view,
5467 up ? KEY_UP : KEY_DOWN);
5470 view->count = s->parent_view->count;
5472 if (old_selected_entry == ls->selected_entry)
5475 err = set_selected_commit(s, ls->selected_entry);
5478 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5479 struct tog_blame_view_state *bs;
5480 struct got_object_id *id, *prev_id;
5482 bs = &s->parent_view->state.blame;
5483 prev_id = get_annotation_for_line(bs->blame.lines,
5484 bs->blame.nlines, bs->last_diffed_line);
5486 err = input_blame_view(&view, s->parent_view,
5487 up ? KEY_UP : KEY_DOWN);
5490 view->count = s->parent_view->count;
5492 if (prev_id == NULL)
5494 id = get_selected_commit_id(bs->blame.lines,
5495 bs->blame.nlines, bs->first_displayed_line,
5500 if (!got_object_id_cmp(prev_id, id))
5503 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5507 s->first_displayed_line = 1;
5508 s->last_displayed_line = view->nlines;
5509 s->matched_line = 0;
5512 diff_view_indicate_progress(view);
5513 err = create_diff(s);
5523 static const struct got_error *
5524 cmd_diff(int argc, char *argv[])
5526 const struct got_error *error = NULL;
5527 struct got_repository *repo = NULL;
5528 struct got_worktree *worktree = NULL;
5529 struct got_object_id *id1 = NULL, *id2 = NULL;
5530 char *repo_path = NULL, *cwd = NULL;
5531 char *id_str1 = NULL, *id_str2 = NULL;
5532 char *label1 = NULL, *label2 = NULL;
5533 int diff_context = 3, ignore_whitespace = 0;
5534 int ch, force_text_diff = 0;
5536 struct tog_view *view;
5537 int *pack_fds = NULL;
5539 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5542 force_text_diff = 1;
5545 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5548 errx(1, "number of context lines is %s: %s",
5552 repo_path = realpath(optarg, NULL);
5553 if (repo_path == NULL)
5554 return got_error_from_errno2("realpath",
5556 got_path_strip_trailing_slashes(repo_path);
5559 ignore_whitespace = 1;
5571 usage_diff(); /* TODO show local worktree changes */
5572 } else if (argc == 2) {
5578 error = got_repo_pack_fds_open(&pack_fds);
5582 if (repo_path == NULL) {
5583 cwd = getcwd(NULL, 0);
5585 return got_error_from_errno("getcwd");
5586 error = got_worktree_open(&worktree, cwd);
5587 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5591 strdup(got_worktree_get_repo_path(worktree));
5593 repo_path = strdup(cwd);
5594 if (repo_path == NULL) {
5595 error = got_error_from_errno("strdup");
5600 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5606 error = apply_unveil(got_repo_get_path(repo), NULL);
5610 error = tog_load_refs(repo, 0);
5614 error = got_repo_match_object_id(&id1, &label1, id_str1,
5615 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5619 error = got_repo_match_object_id(&id2, &label2, id_str2,
5620 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5624 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5626 error = got_error_from_errno("view_open");
5629 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5630 ignore_whitespace, force_text_diff, NULL, repo);
5633 error = view_loop(view);
5640 const struct got_error *close_err = got_repo_close(repo);
5645 got_worktree_close(worktree);
5647 const struct got_error *pack_err =
5648 got_repo_pack_fds_close(pack_fds);
5661 "usage: %s blame [-c commit] [-r repository-path] path\n",
5666 struct tog_blame_line {
5668 struct got_object_id *id;
5671 static const struct got_error *
5672 draw_blame(struct tog_view *view)
5674 struct tog_blame_view_state *s = &view->state.blame;
5675 struct tog_blame *blame = &s->blame;
5676 regmatch_t *regmatch = &view->regmatch;
5677 const struct got_error *err;
5678 int lineno = 0, nprinted = 0;
5680 size_t linesize = 0;
5684 struct tog_blame_line *blame_line;
5685 struct got_object_id *prev_id = NULL;
5687 struct tog_color *tc;
5689 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5694 werase(view->window);
5696 if (asprintf(&line, "commit %s", id_str) == -1) {
5697 err = got_error_from_errno("asprintf");
5702 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5707 if (view_needs_focus_indication(view))
5708 wstandout(view->window);
5709 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5711 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5712 waddwstr(view->window, wline);
5713 while (width++ < view->ncols)
5714 waddch(view->window, ' ');
5716 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5717 if (view_needs_focus_indication(view))
5718 wstandend(view->window);
5722 if (view->gline > blame->nlines)
5723 view->gline = blame->nlines;
5725 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5726 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5727 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5729 return got_error_from_errno("asprintf");
5732 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5737 waddwstr(view->window, wline);
5740 if (width < view->ncols - 1)
5741 waddch(view->window, '\n');
5745 while (nprinted < view->nlines - 2) {
5746 linelen = getline(&line, &linesize, blame->f);
5747 if (linelen == -1) {
5748 if (feof(blame->f)) {
5753 return got_ferror(blame->f, GOT_ERR_IO);
5755 if (++lineno < s->first_displayed_line)
5757 if (view->gline && !gotoline(view, &lineno, &nprinted))
5760 /* Set view->maxx based on full line length. */
5761 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5768 view->maxx = MAX(view->maxx, width);
5770 if (nprinted == s->selected_line - 1)
5771 wstandout(view->window);
5773 if (blame->nlines > 0) {
5774 blame_line = &blame->lines[lineno - 1];
5775 if (blame_line->annotated && prev_id &&
5776 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5777 !(nprinted == s->selected_line - 1)) {
5778 waddstr(view->window, " ");
5779 } else if (blame_line->annotated) {
5781 err = got_object_id_str(&id_str,
5787 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5789 wattr_on(view->window,
5790 COLOR_PAIR(tc->colorpair), NULL);
5791 wprintw(view->window, "%.8s", id_str);
5793 wattr_off(view->window,
5794 COLOR_PAIR(tc->colorpair), NULL);
5796 prev_id = blame_line->id;
5798 waddstr(view->window, "........");
5802 waddstr(view->window, "........");
5806 if (nprinted == s->selected_line - 1)
5807 wstandend(view->window);
5808 waddstr(view->window, " ");
5810 if (view->ncols <= 9) {
5812 } else if (s->first_displayed_line + nprinted ==
5814 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5815 err = add_matched_line(&width, line, view->ncols - 9, 9,
5816 view->window, view->x, regmatch);
5824 err = format_line(&wline, &width, &skip, line,
5825 view->x, view->ncols - 9, 9, 1);
5830 waddwstr(view->window, &wline[skip]);
5836 if (width <= view->ncols - 1)
5837 waddch(view->window, '\n');
5838 if (++nprinted == 1)
5839 s->first_displayed_line = lineno;
5842 s->last_displayed_line = lineno;
5849 static const struct got_error *
5850 blame_cb(void *arg, int nlines, int lineno,
5851 struct got_commit_object *commit, struct got_object_id *id)
5853 const struct got_error *err = NULL;
5854 struct tog_blame_cb_args *a = arg;
5855 struct tog_blame_line *line;
5858 if (nlines != a->nlines ||
5859 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5860 return got_error(GOT_ERR_RANGE);
5862 errcode = pthread_mutex_lock(&tog_mutex);
5864 return got_error_set_errno(errcode, "pthread_mutex_lock");
5866 if (*a->quit) { /* user has quit the blame view */
5867 err = got_error(GOT_ERR_ITER_COMPLETED);
5872 goto done; /* no change in this commit */
5874 line = &a->lines[lineno - 1];
5875 if (line->annotated)
5878 line->id = got_object_id_dup(id);
5879 if (line->id == NULL) {
5880 err = got_error_from_errno("got_object_id_dup");
5883 line->annotated = 1;
5885 errcode = pthread_mutex_unlock(&tog_mutex);
5887 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5892 blame_thread(void *arg)
5894 const struct got_error *err, *close_err;
5895 struct tog_blame_thread_args *ta = arg;
5896 struct tog_blame_cb_args *a = ta->cb_args;
5897 int errcode, fd1 = -1, fd2 = -1;
5898 FILE *f1 = NULL, *f2 = NULL;
5900 fd1 = got_opentempfd();
5902 return (void *)got_error_from_errno("got_opentempfd");
5904 fd2 = got_opentempfd();
5906 err = got_error_from_errno("got_opentempfd");
5910 f1 = got_opentemp();
5912 err = (void *)got_error_from_errno("got_opentemp");
5915 f2 = got_opentemp();
5917 err = (void *)got_error_from_errno("got_opentemp");
5921 err = block_signals_used_by_main_thread();
5925 err = got_blame(ta->path, a->commit_id, ta->repo,
5926 tog_diff_algo, blame_cb, ta->cb_args,
5927 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5928 if (err && err->code == GOT_ERR_CANCELLED)
5931 errcode = pthread_mutex_lock(&tog_mutex);
5933 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5937 close_err = got_repo_close(ta->repo);
5943 errcode = pthread_mutex_unlock(&tog_mutex);
5944 if (errcode && err == NULL)
5945 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5948 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5949 err = got_error_from_errno("close");
5950 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5951 err = got_error_from_errno("close");
5952 if (f1 && fclose(f1) == EOF && err == NULL)
5953 err = got_error_from_errno("fclose");
5954 if (f2 && fclose(f2) == EOF && err == NULL)
5955 err = got_error_from_errno("fclose");
5960 static struct got_object_id *
5961 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5962 int first_displayed_line, int selected_line)
5964 struct tog_blame_line *line;
5969 line = &lines[first_displayed_line - 1 + selected_line - 1];
5970 if (!line->annotated)
5976 static struct got_object_id *
5977 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5980 struct tog_blame_line *line;
5982 if (nlines <= 0 || lineno >= nlines)
5985 line = &lines[lineno - 1];
5986 if (!line->annotated)
5992 static const struct got_error *
5993 stop_blame(struct tog_blame *blame)
5995 const struct got_error *err = NULL;
5998 if (blame->thread) {
6000 errcode = pthread_mutex_unlock(&tog_mutex);
6002 return got_error_set_errno(errcode,
6003 "pthread_mutex_unlock");
6004 errcode = pthread_join(blame->thread, (void **)&err);
6006 return got_error_set_errno(errcode, "pthread_join");
6007 errcode = pthread_mutex_lock(&tog_mutex);
6009 return got_error_set_errno(errcode,
6010 "pthread_mutex_lock");
6011 if (err && err->code == GOT_ERR_ITER_COMPLETED)
6013 blame->thread = 0; //NULL;
6015 if (blame->thread_args.repo) {
6016 const struct got_error *close_err;
6017 close_err = got_repo_close(blame->thread_args.repo);
6020 blame->thread_args.repo = NULL;
6023 if (fclose(blame->f) == EOF && err == NULL)
6024 err = got_error_from_errno("fclose");
6028 for (i = 0; i < blame->nlines; i++)
6029 free(blame->lines[i].id);
6031 blame->lines = NULL;
6033 free(blame->cb_args.commit_id);
6034 blame->cb_args.commit_id = NULL;
6035 if (blame->pack_fds) {
6036 const struct got_error *pack_err =
6037 got_repo_pack_fds_close(blame->pack_fds);
6040 blame->pack_fds = NULL;
6045 static const struct got_error *
6046 cancel_blame_view(void *arg)
6048 const struct got_error *err = NULL;
6052 errcode = pthread_mutex_lock(&tog_mutex);
6054 return got_error_set_errno(errcode,
6055 "pthread_mutex_unlock");
6058 err = got_error(GOT_ERR_CANCELLED);
6060 errcode = pthread_mutex_unlock(&tog_mutex);
6062 return got_error_set_errno(errcode,
6063 "pthread_mutex_lock");
6068 static const struct got_error *
6069 run_blame(struct tog_view *view)
6071 struct tog_blame_view_state *s = &view->state.blame;
6072 struct tog_blame *blame = &s->blame;
6073 const struct got_error *err = NULL;
6074 struct got_commit_object *commit = NULL;
6075 struct got_blob_object *blob = NULL;
6076 struct got_repository *thread_repo = NULL;
6077 struct got_object_id *obj_id = NULL;
6078 int obj_type, fd = -1;
6079 int *pack_fds = NULL;
6081 err = got_object_open_as_commit(&commit, s->repo,
6082 &s->blamed_commit->id);
6086 fd = got_opentempfd();
6088 err = got_error_from_errno("got_opentempfd");
6092 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6096 err = got_object_get_type(&obj_type, s->repo, obj_id);
6100 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6101 err = got_error(GOT_ERR_OBJ_TYPE);
6105 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6108 blame->f = got_opentemp();
6109 if (blame->f == NULL) {
6110 err = got_error_from_errno("got_opentemp");
6113 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6114 &blame->line_offsets, blame->f, blob);
6117 if (blame->nlines == 0) {
6118 s->blame_complete = 1;
6122 /* Don't include \n at EOF in the blame line count. */
6123 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6126 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6127 if (blame->lines == NULL) {
6128 err = got_error_from_errno("calloc");
6132 err = got_repo_pack_fds_open(&pack_fds);
6135 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6140 blame->pack_fds = pack_fds;
6141 blame->cb_args.view = view;
6142 blame->cb_args.lines = blame->lines;
6143 blame->cb_args.nlines = blame->nlines;
6144 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6145 if (blame->cb_args.commit_id == NULL) {
6146 err = got_error_from_errno("got_object_id_dup");
6149 blame->cb_args.quit = &s->done;
6151 blame->thread_args.path = s->path;
6152 blame->thread_args.repo = thread_repo;
6153 blame->thread_args.cb_args = &blame->cb_args;
6154 blame->thread_args.complete = &s->blame_complete;
6155 blame->thread_args.cancel_cb = cancel_blame_view;
6156 blame->thread_args.cancel_arg = &s->done;
6157 s->blame_complete = 0;
6159 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6160 s->first_displayed_line = 1;
6161 s->last_displayed_line = view->nlines;
6162 s->selected_line = 1;
6164 s->matched_line = 0;
6168 got_object_commit_close(commit);
6169 if (fd != -1 && close(fd) == -1 && err == NULL)
6170 err = got_error_from_errno("close");
6172 got_object_blob_close(blob);
6179 static const struct got_error *
6180 open_blame_view(struct tog_view *view, char *path,
6181 struct got_object_id *commit_id, struct got_repository *repo)
6183 const struct got_error *err = NULL;
6184 struct tog_blame_view_state *s = &view->state.blame;
6186 STAILQ_INIT(&s->blamed_commits);
6188 s->path = strdup(path);
6189 if (s->path == NULL)
6190 return got_error_from_errno("strdup");
6192 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6198 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6199 s->first_displayed_line = 1;
6200 s->last_displayed_line = view->nlines;
6201 s->selected_line = 1;
6202 s->blame_complete = 0;
6204 s->commit_id = commit_id;
6205 memset(&s->blame, 0, sizeof(s->blame));
6207 STAILQ_INIT(&s->colors);
6208 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6209 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6210 get_color_value("TOG_COLOR_COMMIT"));
6215 view->show = show_blame_view;
6216 view->input = input_blame_view;
6217 view->reset = reset_blame_view;
6218 view->close = close_blame_view;
6219 view->search_start = search_start_blame_view;
6220 view->search_setup = search_setup_blame_view;
6221 view->search_next = search_next_view_match;
6223 return run_blame(view);
6226 static const struct got_error *
6227 close_blame_view(struct tog_view *view)
6229 const struct got_error *err = NULL;
6230 struct tog_blame_view_state *s = &view->state.blame;
6232 if (s->blame.thread)
6233 err = stop_blame(&s->blame);
6235 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6236 struct got_object_qid *blamed_commit;
6237 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6238 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6239 got_object_qid_free(blamed_commit);
6243 free_colors(&s->colors);
6247 static const struct got_error *
6248 search_start_blame_view(struct tog_view *view)
6250 struct tog_blame_view_state *s = &view->state.blame;
6252 s->matched_line = 0;
6257 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6258 size_t *nlines, int **first, int **last, int **match, int **selected)
6260 struct tog_blame_view_state *s = &view->state.blame;
6263 *nlines = s->blame.nlines;
6264 *line_offsets = s->blame.line_offsets;
6265 *match = &s->matched_line;
6266 *first = &s->first_displayed_line;
6267 *last = &s->last_displayed_line;
6268 *selected = &s->selected_line;
6271 static const struct got_error *
6272 show_blame_view(struct tog_view *view)
6274 const struct got_error *err = NULL;
6275 struct tog_blame_view_state *s = &view->state.blame;
6278 if (s->blame.thread == 0 && !s->blame_complete) {
6279 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6280 &s->blame.thread_args);
6282 return got_error_set_errno(errcode, "pthread_create");
6284 halfdelay(1); /* fast refresh while annotating */
6287 if (s->blame_complete)
6288 halfdelay(10); /* disable fast refresh */
6290 err = draw_blame(view);
6296 static const struct got_error *
6297 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6298 struct got_repository *repo, struct got_object_id *id)
6300 struct tog_view *log_view;
6301 const struct got_error *err = NULL;
6305 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6306 if (log_view == NULL)
6307 return got_error_from_errno("view_open");
6309 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6311 view_close(log_view);
6313 *new_view = log_view;
6318 static const struct got_error *
6319 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6321 const struct got_error *err = NULL, *thread_err = NULL;
6322 struct tog_view *diff_view;
6323 struct tog_blame_view_state *s = &view->state.blame;
6324 int eos, nscroll, begin_y = 0, begin_x = 0;
6326 eos = nscroll = view->nlines - 2;
6327 if (view_is_hsplit_top(view))
6335 view->x = MAX(view->maxx - view->ncols / 3, 0);
6340 if (view->x + view->ncols / 3 < view->maxx)
6341 view->x += 2; /* move two columns right */
6347 view->x -= MIN(view->x, 2); /* move two columns back */
6356 s->selected_line = 1;
6357 s->first_displayed_line = 1;
6362 if (s->blame.nlines < eos) {
6363 s->selected_line = s->blame.nlines;
6364 s->first_displayed_line = 1;
6366 s->selected_line = eos;
6367 s->first_displayed_line = s->blame.nlines - (eos - 1);
6374 if (s->selected_line > 1)
6376 else if (s->selected_line == 1 &&
6377 s->first_displayed_line > 1)
6378 s->first_displayed_line--;
6389 if (s->first_displayed_line == 1) {
6390 if (view->count > 1)
6392 s->selected_line = MAX(1, s->selected_line - nscroll);
6396 if (s->first_displayed_line > nscroll)
6397 s->first_displayed_line -= nscroll;
6399 s->first_displayed_line = 1;
6404 if (s->selected_line < eos && s->first_displayed_line +
6405 s->selected_line <= s->blame.nlines)
6407 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6408 s->first_displayed_line++;
6414 struct got_object_id *id = NULL;
6417 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6418 s->first_displayed_line, s->selected_line);
6422 struct got_commit_object *commit, *pcommit;
6423 struct got_object_qid *pid;
6424 struct got_object_id *blob_id = NULL;
6426 err = got_object_open_as_commit(&commit,
6431 got_object_commit_get_parent_ids(commit));
6433 got_object_commit_close(commit);
6436 /* Check if path history ends here. */
6437 err = got_object_open_as_commit(&pcommit,
6441 err = got_object_id_by_path(&blob_id, s->repo,
6443 got_object_commit_close(pcommit);
6445 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6447 got_object_commit_close(commit);
6450 err = got_object_get_type(&obj_type, s->repo,
6453 /* Can't blame non-blob type objects. */
6454 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6455 got_object_commit_close(commit);
6458 err = got_object_qid_alloc(&s->blamed_commit,
6460 got_object_commit_close(commit);
6462 if (got_object_id_cmp(id,
6463 &s->blamed_commit->id) == 0)
6465 err = got_object_qid_alloc(&s->blamed_commit,
6471 thread_err = stop_blame(&s->blame);
6475 STAILQ_INSERT_HEAD(&s->blamed_commits,
6476 s->blamed_commit, entry);
6477 err = run_blame(view);
6483 struct got_object_qid *first;
6486 first = STAILQ_FIRST(&s->blamed_commits);
6487 if (!got_object_id_cmp(&first->id, s->commit_id))
6490 thread_err = stop_blame(&s->blame);
6494 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6495 got_object_qid_free(s->blamed_commit);
6497 STAILQ_FIRST(&s->blamed_commits);
6498 err = run_blame(view);
6505 s->id_to_log = get_selected_commit_id(s->blame.lines,
6506 s->blame.nlines, s->first_displayed_line, s->selected_line);
6508 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6512 struct got_object_id *id = NULL;
6513 struct got_object_qid *pid;
6514 struct got_commit_object *commit = NULL;
6517 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6518 s->first_displayed_line, s->selected_line);
6521 err = got_object_open_as_commit(&commit, s->repo, id);
6524 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6526 /* traversed from diff view, release diff resources */
6527 err = close_diff_view(*new_view);
6530 diff_view = *new_view;
6532 if (view_is_parent_view(view))
6533 view_get_split(view, &begin_y, &begin_x);
6535 diff_view = view_open(0, 0, begin_y, begin_x,
6537 if (diff_view == NULL) {
6538 got_object_commit_close(commit);
6539 err = got_error_from_errno("view_open");
6543 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6544 id, NULL, NULL, 3, 0, 0, view, s->repo);
6545 got_object_commit_close(commit);
6547 view_close(diff_view);
6550 s->last_diffed_line = s->first_displayed_line - 1 +
6553 break; /* still open from active diff view */
6554 if (view_is_parent_view(view) &&
6555 view->mode == TOG_VIEW_SPLIT_HRZN) {
6556 err = view_init_hsplit(view, begin_y);
6562 diff_view->focussed = 1;
6563 diff_view->mode = view->mode;
6564 diff_view->nlines = view->lines - begin_y;
6565 if (view_is_parent_view(view)) {
6566 view_transfer_size(diff_view, view);
6567 err = view_close_child(view);
6570 err = view_set_child(view, diff_view);
6573 view->focus_child = 1;
6575 *new_view = diff_view;
6588 if (s->last_displayed_line >= s->blame.nlines &&
6589 s->selected_line >= MIN(s->blame.nlines,
6590 view->nlines - 2)) {
6594 if (s->last_displayed_line >= s->blame.nlines &&
6595 s->selected_line < view->nlines - 2) {
6597 MIN(nscroll, s->last_displayed_line -
6598 s->first_displayed_line - s->selected_line + 1);
6600 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6601 s->first_displayed_line += nscroll;
6603 s->first_displayed_line =
6604 s->blame.nlines - (view->nlines - 3);
6607 if (s->selected_line > view->nlines - 2) {
6608 s->selected_line = MIN(s->blame.nlines,
6616 return thread_err ? thread_err : err;
6619 static const struct got_error *
6620 reset_blame_view(struct tog_view *view)
6622 const struct got_error *err;
6623 struct tog_blame_view_state *s = &view->state.blame;
6627 err = stop_blame(&s->blame);
6631 return run_blame(view);
6634 static const struct got_error *
6635 cmd_blame(int argc, char *argv[])
6637 const struct got_error *error;
6638 struct got_repository *repo = NULL;
6639 struct got_worktree *worktree = NULL;
6640 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6641 char *link_target = NULL;
6642 struct got_object_id *commit_id = NULL;
6643 struct got_commit_object *commit = NULL;
6644 char *commit_id_str = NULL;
6646 struct tog_view *view;
6647 int *pack_fds = NULL;
6649 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6652 commit_id_str = optarg;
6655 repo_path = realpath(optarg, NULL);
6656 if (repo_path == NULL)
6657 return got_error_from_errno2("realpath",
6672 error = got_repo_pack_fds_open(&pack_fds);
6676 if (repo_path == NULL) {
6677 cwd = getcwd(NULL, 0);
6679 return got_error_from_errno("getcwd");
6680 error = got_worktree_open(&worktree, cwd);
6681 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6685 strdup(got_worktree_get_repo_path(worktree));
6687 repo_path = strdup(cwd);
6688 if (repo_path == NULL) {
6689 error = got_error_from_errno("strdup");
6694 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6698 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6705 error = apply_unveil(got_repo_get_path(repo), NULL);
6709 error = tog_load_refs(repo, 0);
6713 if (commit_id_str == NULL) {
6714 struct got_reference *head_ref;
6715 error = got_ref_open(&head_ref, repo, worktree ?
6716 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6719 error = got_ref_resolve(&commit_id, repo, head_ref);
6720 got_ref_close(head_ref);
6722 error = got_repo_match_object_id(&commit_id, NULL,
6723 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6728 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6730 error = got_error_from_errno("view_open");
6734 error = got_object_open_as_commit(&commit, repo, commit_id);
6738 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6743 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6748 /* Release work tree lock. */
6749 got_worktree_close(worktree);
6752 error = view_loop(view);
6760 got_object_commit_close(commit);
6762 got_worktree_close(worktree);
6764 const struct got_error *close_err = got_repo_close(repo);
6769 const struct got_error *pack_err =
6770 got_repo_pack_fds_close(pack_fds);
6778 static const struct got_error *
6779 draw_tree_entries(struct tog_view *view, const char *parent_path)
6781 struct tog_tree_view_state *s = &view->state.tree;
6782 const struct got_error *err = NULL;
6783 struct got_tree_entry *te;
6786 struct tog_color *tc;
6787 int width, n, nentries, i = 1;
6788 int limit = view->nlines;
6791 if (view_is_hsplit_top(view))
6792 --limit; /* border */
6794 werase(view->window);
6799 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6803 if (view_needs_focus_indication(view))
6804 wstandout(view->window);
6805 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6807 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6808 waddwstr(view->window, wline);
6811 while (width++ < view->ncols)
6812 waddch(view->window, ' ');
6814 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6815 if (view_needs_focus_indication(view))
6816 wstandend(view->window);
6821 if (s->first_displayed_entry) {
6822 i += got_tree_entry_get_index(s->first_displayed_entry);
6823 if (s->tree != s->root)
6824 ++i; /* account for ".." entry */
6826 nentries = got_object_tree_get_nentries(s->tree);
6827 if (asprintf(&index, "[%d/%d] %s",
6828 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6829 return got_error_from_errno("asprintf");
6830 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6834 waddwstr(view->window, wline);
6837 if (width < view->ncols - 1)
6838 waddch(view->window, '\n');
6841 waddch(view->window, '\n');
6845 if (s->first_displayed_entry == NULL) {
6846 te = got_object_tree_get_first_entry(s->tree);
6847 if (s->selected == 0) {
6849 wstandout(view->window);
6850 s->selected_entry = NULL;
6852 waddstr(view->window, " ..\n"); /* parent directory */
6853 if (s->selected == 0 && view->focussed)
6854 wstandend(view->window);
6861 te = s->first_displayed_entry;
6864 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6865 char *line = NULL, *id_str = NULL, *link_target = NULL;
6866 const char *modestr = "";
6869 te = got_object_tree_get_entry(s->tree, i);
6870 mode = got_tree_entry_get_mode(te);
6873 err = got_object_id_str(&id_str,
6874 got_tree_entry_get_id(te));
6876 return got_error_from_errno(
6877 "got_object_id_str");
6879 if (got_object_tree_entry_is_submodule(te))
6881 else if (S_ISLNK(mode)) {
6884 err = got_tree_entry_get_symlink_target(&link_target,
6890 for (i = 0; i < strlen(link_target); i++) {
6891 if (!isprint((unsigned char)link_target[i]))
6892 link_target[i] = '?';
6896 else if (S_ISDIR(mode))
6898 else if (mode & S_IXUSR)
6900 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6901 got_tree_entry_get_name(te), modestr,
6902 link_target ? " -> ": "",
6903 link_target ? link_target : "") == -1) {
6906 return got_error_from_errno("asprintf");
6910 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6916 if (n == s->selected) {
6918 wstandout(view->window);
6919 s->selected_entry = te;
6921 tc = match_color(&s->colors, line);
6923 wattr_on(view->window,
6924 COLOR_PAIR(tc->colorpair), NULL);
6925 waddwstr(view->window, wline);
6927 wattr_off(view->window,
6928 COLOR_PAIR(tc->colorpair), NULL);
6929 if (width < view->ncols - 1)
6930 waddch(view->window, '\n');
6931 if (n == s->selected && view->focussed)
6932 wstandend(view->window);
6938 s->last_displayed_entry = te;
6947 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6949 struct got_tree_entry *te;
6950 int isroot = s->tree == s->root;
6953 if (s->first_displayed_entry == NULL)
6956 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6957 while (i++ < maxscroll) {
6960 s->first_displayed_entry = NULL;
6963 s->first_displayed_entry = te;
6964 te = got_tree_entry_get_prev(s->tree, te);
6968 static const struct got_error *
6969 tree_scroll_down(struct tog_view *view, int maxscroll)
6971 struct tog_tree_view_state *s = &view->state.tree;
6972 struct got_tree_entry *next, *last;
6975 if (s->first_displayed_entry)
6976 next = got_tree_entry_get_next(s->tree,
6977 s->first_displayed_entry);
6979 next = got_object_tree_get_first_entry(s->tree);
6981 last = s->last_displayed_entry;
6982 while (next && n++ < maxscroll) {
6984 s->last_displayed_entry = last;
6985 last = got_tree_entry_get_next(s->tree, last);
6987 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6988 s->first_displayed_entry = next;
6989 next = got_tree_entry_get_next(s->tree, next);
6996 static const struct got_error *
6997 tree_entry_path(char **path, struct tog_parent_trees *parents,
6998 struct got_tree_entry *te)
7000 const struct got_error *err = NULL;
7001 struct tog_parent_tree *pt;
7002 size_t len = 2; /* for leading slash and NUL */
7004 TAILQ_FOREACH(pt, parents, entry)
7005 len += strlen(got_tree_entry_get_name(pt->selected_entry))
7008 len += strlen(got_tree_entry_get_name(te));
7010 *path = calloc(1, len);
7012 return got_error_from_errno("calloc");
7015 pt = TAILQ_LAST(parents, tog_parent_trees);
7017 const char *name = got_tree_entry_get_name(pt->selected_entry);
7018 if (strlcat(*path, name, len) >= len) {
7019 err = got_error(GOT_ERR_NO_SPACE);
7022 if (strlcat(*path, "/", len) >= len) {
7023 err = got_error(GOT_ERR_NO_SPACE);
7026 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7029 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7030 err = got_error(GOT_ERR_NO_SPACE);
7042 static const struct got_error *
7043 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7044 struct got_tree_entry *te, struct tog_parent_trees *parents,
7045 struct got_object_id *commit_id, struct got_repository *repo)
7047 const struct got_error *err = NULL;
7049 struct tog_view *blame_view;
7053 err = tree_entry_path(&path, parents, te);
7057 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7058 if (blame_view == NULL) {
7059 err = got_error_from_errno("view_open");
7063 err = open_blame_view(blame_view, path, commit_id, repo);
7065 if (err->code == GOT_ERR_CANCELLED)
7067 view_close(blame_view);
7069 *new_view = blame_view;
7075 static const struct got_error *
7076 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7077 struct tog_tree_view_state *s)
7079 struct tog_view *log_view;
7080 const struct got_error *err = NULL;
7085 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7086 if (log_view == NULL)
7087 return got_error_from_errno("view_open");
7089 err = tree_entry_path(&path, &s->parents, s->selected_entry);
7093 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7096 view_close(log_view);
7098 *new_view = log_view;
7103 static const struct got_error *
7104 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7105 const char *head_ref_name, struct got_repository *repo)
7107 const struct got_error *err = NULL;
7108 char *commit_id_str = NULL;
7109 struct tog_tree_view_state *s = &view->state.tree;
7110 struct got_commit_object *commit = NULL;
7112 TAILQ_INIT(&s->parents);
7113 STAILQ_INIT(&s->colors);
7115 s->commit_id = got_object_id_dup(commit_id);
7116 if (s->commit_id == NULL)
7117 return got_error_from_errno("got_object_id_dup");
7119 err = got_object_open_as_commit(&commit, repo, commit_id);
7124 * The root is opened here and will be closed when the view is closed.
7125 * Any visited subtrees and their path-wise parents are opened and
7128 err = got_object_open_as_tree(&s->root, repo,
7129 got_object_commit_get_tree_id(commit));
7134 err = got_object_id_str(&commit_id_str, commit_id);
7138 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7139 err = got_error_from_errno("asprintf");
7143 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7144 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7145 if (head_ref_name) {
7146 s->head_ref_name = strdup(head_ref_name);
7147 if (s->head_ref_name == NULL) {
7148 err = got_error_from_errno("strdup");
7154 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7155 err = add_color(&s->colors, "\\$$",
7156 TOG_COLOR_TREE_SUBMODULE,
7157 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7160 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7161 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7164 err = add_color(&s->colors, "/$",
7165 TOG_COLOR_TREE_DIRECTORY,
7166 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7170 err = add_color(&s->colors, "\\*$",
7171 TOG_COLOR_TREE_EXECUTABLE,
7172 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7176 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7177 get_color_value("TOG_COLOR_COMMIT"));
7182 view->show = show_tree_view;
7183 view->input = input_tree_view;
7184 view->close = close_tree_view;
7185 view->search_start = search_start_tree_view;
7186 view->search_next = search_next_tree_view;
7188 free(commit_id_str);
7190 got_object_commit_close(commit);
7192 close_tree_view(view);
7196 static const struct got_error *
7197 close_tree_view(struct tog_view *view)
7199 struct tog_tree_view_state *s = &view->state.tree;
7201 free_colors(&s->colors);
7202 free(s->tree_label);
7203 s->tree_label = NULL;
7205 s->commit_id = NULL;
7206 free(s->head_ref_name);
7207 s->head_ref_name = NULL;
7208 while (!TAILQ_EMPTY(&s->parents)) {
7209 struct tog_parent_tree *parent;
7210 parent = TAILQ_FIRST(&s->parents);
7211 TAILQ_REMOVE(&s->parents, parent, entry);
7212 if (parent->tree != s->root)
7213 got_object_tree_close(parent->tree);
7217 if (s->tree != NULL && s->tree != s->root)
7218 got_object_tree_close(s->tree);
7220 got_object_tree_close(s->root);
7224 static const struct got_error *
7225 search_start_tree_view(struct tog_view *view)
7227 struct tog_tree_view_state *s = &view->state.tree;
7229 s->matched_entry = NULL;
7234 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7236 regmatch_t regmatch;
7238 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7242 static const struct got_error *
7243 search_next_tree_view(struct tog_view *view)
7245 struct tog_tree_view_state *s = &view->state.tree;
7246 struct got_tree_entry *te = NULL;
7248 if (!view->searching) {
7249 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7253 if (s->matched_entry) {
7254 if (view->searching == TOG_SEARCH_FORWARD) {
7255 if (s->selected_entry)
7256 te = got_tree_entry_get_next(s->tree,
7259 te = got_object_tree_get_first_entry(s->tree);
7261 if (s->selected_entry == NULL)
7262 te = got_object_tree_get_last_entry(s->tree);
7264 te = got_tree_entry_get_prev(s->tree,
7268 if (s->selected_entry)
7269 te = s->selected_entry;
7270 else if (view->searching == TOG_SEARCH_FORWARD)
7271 te = got_object_tree_get_first_entry(s->tree);
7273 te = got_object_tree_get_last_entry(s->tree);
7278 if (s->matched_entry == NULL) {
7279 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7282 if (view->searching == TOG_SEARCH_FORWARD)
7283 te = got_object_tree_get_first_entry(s->tree);
7285 te = got_object_tree_get_last_entry(s->tree);
7288 if (match_tree_entry(te, &view->regex)) {
7289 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7290 s->matched_entry = te;
7294 if (view->searching == TOG_SEARCH_FORWARD)
7295 te = got_tree_entry_get_next(s->tree, te);
7297 te = got_tree_entry_get_prev(s->tree, te);
7300 if (s->matched_entry) {
7301 s->first_displayed_entry = s->matched_entry;
7308 static const struct got_error *
7309 show_tree_view(struct tog_view *view)
7311 const struct got_error *err = NULL;
7312 struct tog_tree_view_state *s = &view->state.tree;
7315 err = tree_entry_path(&parent_path, &s->parents, NULL);
7319 err = draw_tree_entries(view, parent_path);
7326 static const struct got_error *
7327 tree_goto_line(struct tog_view *view, int nlines)
7329 const struct got_error *err = NULL;
7330 struct tog_tree_view_state *s = &view->state.tree;
7331 struct got_tree_entry **fte, **lte, **ste;
7332 int g, last, first = 1, i = 1;
7333 int root = s->tree == s->root;
7334 int off = root ? 1 : 2;
7341 else if (g > got_object_tree_get_nentries(s->tree))
7342 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7344 fte = &s->first_displayed_entry;
7345 lte = &s->last_displayed_entry;
7346 ste = &s->selected_entry;
7349 first = got_tree_entry_get_index(*fte);
7350 first += off; /* account for ".." */
7352 last = got_tree_entry_get_index(*lte);
7355 if (g >= first && g <= last && g - first < nlines) {
7356 s->selected = g - first;
7357 return NULL; /* gline is on the current page */
7361 i = got_tree_entry_get_index(*ste);
7366 err = tree_scroll_down(view, g - i);
7369 if (got_tree_entry_get_index(*lte) >=
7370 got_object_tree_get_nentries(s->tree) - 1 &&
7371 first + s->selected < g &&
7372 s->selected < s->ndisplayed - 1) {
7373 first = got_tree_entry_get_index(*fte);
7375 s->selected = g - first;
7378 tree_scroll_up(s, i - g);
7381 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7382 s->selected = g - 1;
7387 static const struct got_error *
7388 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7390 const struct got_error *err = NULL;
7391 struct tog_tree_view_state *s = &view->state.tree;
7392 struct got_tree_entry *te;
7393 int n, nscroll = view->nlines - 3;
7396 return tree_goto_line(view, nscroll);
7400 s->show_ids = !s->show_ids;
7405 if (!s->selected_entry)
7407 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7411 err = view_request_new(new_view, view, TOG_VIEW_REF);
7418 if (s->tree == s->root)
7419 s->first_displayed_entry =
7420 got_object_tree_get_first_entry(s->tree);
7422 s->first_displayed_entry = NULL;
7427 int eos = view->nlines - 3;
7429 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7433 te = got_object_tree_get_last_entry(s->tree);
7434 for (n = 0; n < eos; n++) {
7436 if (s->tree != s->root) {
7437 s->first_displayed_entry = NULL;
7442 s->first_displayed_entry = te;
7443 te = got_tree_entry_get_prev(s->tree, te);
7446 s->selected = n - 1;
7452 if (s->selected > 0) {
7456 tree_scroll_up(s, 1);
7457 if (s->selected_entry == NULL ||
7458 (s->tree == s->root && s->selected_entry ==
7459 got_object_tree_get_first_entry(s->tree)))
7469 if (s->tree == s->root) {
7470 if (got_object_tree_get_first_entry(s->tree) ==
7471 s->first_displayed_entry)
7472 s->selected -= MIN(s->selected, nscroll);
7474 if (s->first_displayed_entry == NULL)
7475 s->selected -= MIN(s->selected, nscroll);
7477 tree_scroll_up(s, MAX(0, nscroll));
7478 if (s->selected_entry == NULL ||
7479 (s->tree == s->root && s->selected_entry ==
7480 got_object_tree_get_first_entry(s->tree)))
7486 if (s->selected < s->ndisplayed - 1) {
7490 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7492 /* can't scroll any further */
7496 tree_scroll_down(view, 1);
7506 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7508 /* can't scroll any further; move cursor down */
7509 if (s->selected < s->ndisplayed - 1)
7510 s->selected += MIN(nscroll,
7511 s->ndisplayed - s->selected - 1);
7516 tree_scroll_down(view, nscroll);
7521 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7522 struct tog_parent_tree *parent;
7523 /* user selected '..' */
7524 if (s->tree == s->root) {
7528 parent = TAILQ_FIRST(&s->parents);
7529 TAILQ_REMOVE(&s->parents, parent,
7531 got_object_tree_close(s->tree);
7532 s->tree = parent->tree;
7533 s->first_displayed_entry =
7534 parent->first_displayed_entry;
7536 parent->selected_entry;
7537 s->selected = parent->selected;
7538 if (s->selected > view->nlines - 3) {
7539 err = offset_selection_down(view);
7544 } else if (S_ISDIR(got_tree_entry_get_mode(
7545 s->selected_entry))) {
7546 struct got_tree_object *subtree;
7548 err = got_object_open_as_tree(&subtree, s->repo,
7549 got_tree_entry_get_id(s->selected_entry));
7552 err = tree_view_visit_subtree(s, subtree);
7554 got_object_tree_close(subtree);
7557 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7558 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7561 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7562 s->selected = view->nlines - 4;
7578 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7583 static const struct got_error *
7584 cmd_tree(int argc, char *argv[])
7586 const struct got_error *error;
7587 struct got_repository *repo = NULL;
7588 struct got_worktree *worktree = NULL;
7589 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7590 struct got_object_id *commit_id = NULL;
7591 struct got_commit_object *commit = NULL;
7592 const char *commit_id_arg = NULL;
7594 struct got_reference *ref = NULL;
7595 const char *head_ref_name = NULL;
7597 struct tog_view *view;
7598 int *pack_fds = NULL;
7600 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7603 commit_id_arg = optarg;
7606 repo_path = realpath(optarg, NULL);
7607 if (repo_path == NULL)
7608 return got_error_from_errno2("realpath",
7623 error = got_repo_pack_fds_open(&pack_fds);
7627 if (repo_path == NULL) {
7628 cwd = getcwd(NULL, 0);
7630 return got_error_from_errno("getcwd");
7631 error = got_worktree_open(&worktree, cwd);
7632 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7636 strdup(got_worktree_get_repo_path(worktree));
7638 repo_path = strdup(cwd);
7639 if (repo_path == NULL) {
7640 error = got_error_from_errno("strdup");
7645 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7649 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7656 error = apply_unveil(got_repo_get_path(repo), NULL);
7660 error = tog_load_refs(repo, 0);
7664 if (commit_id_arg == NULL) {
7665 error = got_repo_match_object_id(&commit_id, &label,
7666 worktree ? got_worktree_get_head_ref_name(worktree) :
7667 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7670 head_ref_name = label;
7672 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7674 head_ref_name = got_ref_get_name(ref);
7675 else if (error->code != GOT_ERR_NOT_REF)
7677 error = got_repo_match_object_id(&commit_id, NULL,
7678 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7683 error = got_object_open_as_commit(&commit, repo, commit_id);
7687 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7689 error = got_error_from_errno("view_open");
7692 error = open_tree_view(view, commit_id, head_ref_name, repo);
7695 if (!got_path_is_root_dir(in_repo_path)) {
7696 error = tree_view_walk_path(&view->state.tree, commit,
7703 /* Release work tree lock. */
7704 got_worktree_close(worktree);
7707 error = view_loop(view);
7716 const struct got_error *close_err = got_repo_close(repo);
7721 const struct got_error *pack_err =
7722 got_repo_pack_fds_close(pack_fds);
7730 static const struct got_error *
7731 ref_view_load_refs(struct tog_ref_view_state *s)
7733 struct got_reflist_entry *sre;
7734 struct tog_reflist_entry *re;
7737 TAILQ_FOREACH(sre, &tog_refs, entry) {
7738 if (strncmp(got_ref_get_name(sre->ref),
7739 "refs/got/", 9) == 0 &&
7740 strncmp(got_ref_get_name(sre->ref),
7741 "refs/got/backup/", 16) != 0)
7744 re = malloc(sizeof(*re));
7746 return got_error_from_errno("malloc");
7748 re->ref = got_ref_dup(sre->ref);
7749 if (re->ref == NULL)
7750 return got_error_from_errno("got_ref_dup");
7751 re->idx = s->nrefs++;
7752 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7755 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7760 ref_view_free_refs(struct tog_ref_view_state *s)
7762 struct tog_reflist_entry *re;
7764 while (!TAILQ_EMPTY(&s->refs)) {
7765 re = TAILQ_FIRST(&s->refs);
7766 TAILQ_REMOVE(&s->refs, re, entry);
7767 got_ref_close(re->ref);
7772 static const struct got_error *
7773 open_ref_view(struct tog_view *view, struct got_repository *repo)
7775 const struct got_error *err = NULL;
7776 struct tog_ref_view_state *s = &view->state.ref;
7778 s->selected_entry = 0;
7781 TAILQ_INIT(&s->refs);
7782 STAILQ_INIT(&s->colors);
7784 err = ref_view_load_refs(s);
7788 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7789 err = add_color(&s->colors, "^refs/heads/",
7790 TOG_COLOR_REFS_HEADS,
7791 get_color_value("TOG_COLOR_REFS_HEADS"));
7795 err = add_color(&s->colors, "^refs/tags/",
7796 TOG_COLOR_REFS_TAGS,
7797 get_color_value("TOG_COLOR_REFS_TAGS"));
7801 err = add_color(&s->colors, "^refs/remotes/",
7802 TOG_COLOR_REFS_REMOTES,
7803 get_color_value("TOG_COLOR_REFS_REMOTES"));
7807 err = add_color(&s->colors, "^refs/got/backup/",
7808 TOG_COLOR_REFS_BACKUP,
7809 get_color_value("TOG_COLOR_REFS_BACKUP"));
7814 view->show = show_ref_view;
7815 view->input = input_ref_view;
7816 view->close = close_ref_view;
7817 view->search_start = search_start_ref_view;
7818 view->search_next = search_next_ref_view;
7821 free_colors(&s->colors);
7825 static const struct got_error *
7826 close_ref_view(struct tog_view *view)
7828 struct tog_ref_view_state *s = &view->state.ref;
7830 ref_view_free_refs(s);
7831 free_colors(&s->colors);
7836 static const struct got_error *
7837 resolve_reflist_entry(struct got_object_id **commit_id,
7838 struct tog_reflist_entry *re, struct got_repository *repo)
7840 const struct got_error *err = NULL;
7841 struct got_object_id *obj_id;
7842 struct got_tag_object *tag = NULL;
7847 err = got_ref_resolve(&obj_id, repo, re->ref);
7851 err = got_object_get_type(&obj_type, repo, obj_id);
7856 case GOT_OBJ_TYPE_COMMIT:
7857 *commit_id = obj_id;
7859 case GOT_OBJ_TYPE_TAG:
7860 err = got_object_open_as_tag(&tag, repo, obj_id);
7864 err = got_object_get_type(&obj_type, repo,
7865 got_object_tag_get_object_id(tag));
7868 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7869 err = got_error(GOT_ERR_OBJ_TYPE);
7872 *commit_id = got_object_id_dup(
7873 got_object_tag_get_object_id(tag));
7874 if (*commit_id == NULL) {
7875 err = got_error_from_errno("got_object_id_dup");
7880 err = got_error(GOT_ERR_OBJ_TYPE);
7886 got_object_tag_close(tag);
7894 static const struct got_error *
7895 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7896 struct tog_reflist_entry *re, struct got_repository *repo)
7898 struct tog_view *log_view;
7899 const struct got_error *err = NULL;
7900 struct got_object_id *commit_id = NULL;
7904 err = resolve_reflist_entry(&commit_id, re, repo);
7906 if (err->code != GOT_ERR_OBJ_TYPE)
7912 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7913 if (log_view == NULL) {
7914 err = got_error_from_errno("view_open");
7918 err = open_log_view(log_view, commit_id, repo,
7919 got_ref_get_name(re->ref), "", 0);
7922 view_close(log_view);
7924 *new_view = log_view;
7930 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7932 struct tog_reflist_entry *re;
7935 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7938 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7939 while (i++ < maxscroll) {
7942 s->first_displayed_entry = re;
7943 re = TAILQ_PREV(re, tog_reflist_head, entry);
7947 static const struct got_error *
7948 ref_scroll_down(struct tog_view *view, int maxscroll)
7950 struct tog_ref_view_state *s = &view->state.ref;
7951 struct tog_reflist_entry *next, *last;
7954 if (s->first_displayed_entry)
7955 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7957 next = TAILQ_FIRST(&s->refs);
7959 last = s->last_displayed_entry;
7960 while (next && n++ < maxscroll) {
7962 s->last_displayed_entry = last;
7963 last = TAILQ_NEXT(last, entry);
7965 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7966 s->first_displayed_entry = next;
7967 next = TAILQ_NEXT(next, entry);
7974 static const struct got_error *
7975 search_start_ref_view(struct tog_view *view)
7977 struct tog_ref_view_state *s = &view->state.ref;
7979 s->matched_entry = NULL;
7984 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7986 regmatch_t regmatch;
7988 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7992 static const struct got_error *
7993 search_next_ref_view(struct tog_view *view)
7995 struct tog_ref_view_state *s = &view->state.ref;
7996 struct tog_reflist_entry *re = NULL;
7998 if (!view->searching) {
7999 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8003 if (s->matched_entry) {
8004 if (view->searching == TOG_SEARCH_FORWARD) {
8005 if (s->selected_entry)
8006 re = TAILQ_NEXT(s->selected_entry, entry);
8008 re = TAILQ_PREV(s->selected_entry,
8009 tog_reflist_head, entry);
8011 if (s->selected_entry == NULL)
8012 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8014 re = TAILQ_PREV(s->selected_entry,
8015 tog_reflist_head, entry);
8018 if (s->selected_entry)
8019 re = s->selected_entry;
8020 else if (view->searching == TOG_SEARCH_FORWARD)
8021 re = TAILQ_FIRST(&s->refs);
8023 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8028 if (s->matched_entry == NULL) {
8029 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8032 if (view->searching == TOG_SEARCH_FORWARD)
8033 re = TAILQ_FIRST(&s->refs);
8035 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8038 if (match_reflist_entry(re, &view->regex)) {
8039 view->search_next_done = TOG_SEARCH_HAVE_MORE;
8040 s->matched_entry = re;
8044 if (view->searching == TOG_SEARCH_FORWARD)
8045 re = TAILQ_NEXT(re, entry);
8047 re = TAILQ_PREV(re, tog_reflist_head, entry);
8050 if (s->matched_entry) {
8051 s->first_displayed_entry = s->matched_entry;
8058 static const struct got_error *
8059 show_ref_view(struct tog_view *view)
8061 const struct got_error *err = NULL;
8062 struct tog_ref_view_state *s = &view->state.ref;
8063 struct tog_reflist_entry *re;
8066 struct tog_color *tc;
8068 int limit = view->nlines;
8070 werase(view->window);
8073 if (view_is_hsplit_top(view))
8074 --limit; /* border */
8079 re = s->first_displayed_entry;
8081 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8083 return got_error_from_errno("asprintf");
8085 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8090 if (view_needs_focus_indication(view))
8091 wstandout(view->window);
8092 waddwstr(view->window, wline);
8093 while (width++ < view->ncols)
8094 waddch(view->window, ' ');
8095 if (view_needs_focus_indication(view))
8096 wstandend(view->window);
8105 while (re && limit > 0) {
8107 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8110 struct got_commit_object *ci;
8111 struct got_tag_object *tag;
8112 struct got_object_id *id;
8116 err = got_ref_resolve(&id, s->repo, re->ref);
8119 err = got_object_open_as_tag(&tag, s->repo, id);
8121 if (err->code != GOT_ERR_OBJ_TYPE) {
8125 err = got_object_open_as_commit(&ci, s->repo,
8131 t = got_object_commit_get_committer_time(ci);
8132 got_object_commit_close(ci);
8134 t = got_object_tag_get_tagger_time(tag);
8135 got_object_tag_close(tag);
8138 if (gmtime_r(&t, &tm) == NULL)
8139 return got_error_from_errno("gmtime_r");
8140 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8141 return got_error(GOT_ERR_NO_SPACE);
8143 if (got_ref_is_symbolic(re->ref)) {
8144 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8145 ymd : "", got_ref_get_name(re->ref),
8146 got_ref_get_symref_target(re->ref)) == -1)
8147 return got_error_from_errno("asprintf");
8148 } else if (s->show_ids) {
8149 struct got_object_id *id;
8151 err = got_ref_resolve(&id, s->repo, re->ref);
8154 err = got_object_id_str(&id_str, id);
8159 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8160 got_ref_get_name(re->ref), id_str) == -1) {
8161 err = got_error_from_errno("asprintf");
8168 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8169 got_ref_get_name(re->ref)) == -1)
8170 return got_error_from_errno("asprintf");
8172 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
8178 if (n == s->selected) {
8180 wstandout(view->window);
8181 s->selected_entry = re;
8183 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8185 wattr_on(view->window,
8186 COLOR_PAIR(tc->colorpair), NULL);
8187 waddwstr(view->window, wline);
8189 wattr_off(view->window,
8190 COLOR_PAIR(tc->colorpair), NULL);
8191 if (width < view->ncols - 1)
8192 waddch(view->window, '\n');
8193 if (n == s->selected && view->focussed)
8194 wstandend(view->window);
8200 s->last_displayed_entry = re;
8203 re = TAILQ_NEXT(re, entry);
8210 static const struct got_error *
8211 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8212 struct tog_reflist_entry *re, struct got_repository *repo)
8214 const struct got_error *err = NULL;
8215 struct got_object_id *commit_id = NULL;
8216 struct tog_view *tree_view;
8220 err = resolve_reflist_entry(&commit_id, re, repo);
8222 if (err->code != GOT_ERR_OBJ_TYPE)
8229 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8230 if (tree_view == NULL) {
8231 err = got_error_from_errno("view_open");
8235 err = open_tree_view(tree_view, commit_id,
8236 got_ref_get_name(re->ref), repo);
8240 *new_view = tree_view;
8246 static const struct got_error *
8247 ref_goto_line(struct tog_view *view, int nlines)
8249 const struct got_error *err = NULL;
8250 struct tog_ref_view_state *s = &view->state.ref;
8251 int g, idx = s->selected_entry->idx;
8258 else if (g > s->nrefs)
8261 if (g >= s->first_displayed_entry->idx + 1 &&
8262 g <= s->last_displayed_entry->idx + 1 &&
8263 g - s->first_displayed_entry->idx - 1 < nlines) {
8264 s->selected = g - s->first_displayed_entry->idx - 1;
8269 err = ref_scroll_down(view, g - idx - 1);
8272 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8273 s->first_displayed_entry->idx + s->selected < g &&
8274 s->selected < s->ndisplayed - 1)
8275 s->selected = g - s->first_displayed_entry->idx - 1;
8276 } else if (idx + 1 > g)
8277 ref_scroll_up(s, idx - g + 1);
8279 if (g < nlines && s->first_displayed_entry->idx == 0)
8280 s->selected = g - 1;
8286 static const struct got_error *
8287 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8289 const struct got_error *err = NULL;
8290 struct tog_ref_view_state *s = &view->state.ref;
8291 struct tog_reflist_entry *re;
8292 int n, nscroll = view->nlines - 1;
8295 return ref_goto_line(view, nscroll);
8299 s->show_ids = !s->show_ids;
8303 s->show_date = !s->show_date;
8307 s->sort_by_date = !s->sort_by_date;
8308 view->action = s->sort_by_date ? "sort by date" : "sort by name";
8310 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8311 got_ref_cmp_by_commit_timestamp_descending :
8312 tog_ref_cmp_by_name, s->repo);
8315 got_reflist_object_id_map_free(tog_refs_idmap);
8316 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8317 &tog_refs, s->repo);
8320 ref_view_free_refs(s);
8321 err = ref_view_load_refs(s);
8326 if (!s->selected_entry)
8328 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8332 if (!s->selected_entry)
8334 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8341 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8346 int eos = view->nlines - 1;
8348 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8352 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8353 for (n = 0; n < eos; n++) {
8356 s->first_displayed_entry = re;
8357 re = TAILQ_PREV(re, tog_reflist_head, entry);
8360 s->selected = n - 1;
8366 if (s->selected > 0) {
8370 ref_scroll_up(s, 1);
8371 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8381 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8382 s->selected -= MIN(nscroll, s->selected);
8383 ref_scroll_up(s, MAX(0, nscroll));
8384 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8390 if (s->selected < s->ndisplayed - 1) {
8394 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8395 /* can't scroll any further */
8399 ref_scroll_down(view, 1);
8409 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8410 /* can't scroll any further; move cursor down */
8411 if (s->selected < s->ndisplayed - 1)
8412 s->selected += MIN(nscroll,
8413 s->ndisplayed - s->selected - 1);
8414 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8415 s->selected += s->ndisplayed - s->selected - 1;
8419 ref_scroll_down(view, nscroll);
8424 err = tog_load_refs(s->repo, s->sort_by_date);
8427 ref_view_free_refs(s);
8428 err = ref_view_load_refs(s);
8431 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8432 s->selected = view->nlines - 2;
8446 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8451 static const struct got_error *
8452 cmd_ref(int argc, char *argv[])
8454 const struct got_error *error;
8455 struct got_repository *repo = NULL;
8456 struct got_worktree *worktree = NULL;
8457 char *cwd = NULL, *repo_path = NULL;
8459 struct tog_view *view;
8460 int *pack_fds = NULL;
8462 while ((ch = getopt(argc, argv, "r:")) != -1) {
8465 repo_path = realpath(optarg, NULL);
8466 if (repo_path == NULL)
8467 return got_error_from_errno2("realpath",
8482 error = got_repo_pack_fds_open(&pack_fds);
8486 if (repo_path == NULL) {
8487 cwd = getcwd(NULL, 0);
8489 return got_error_from_errno("getcwd");
8490 error = got_worktree_open(&worktree, cwd);
8491 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8495 strdup(got_worktree_get_repo_path(worktree));
8497 repo_path = strdup(cwd);
8498 if (repo_path == NULL) {
8499 error = got_error_from_errno("strdup");
8504 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8510 error = apply_unveil(got_repo_get_path(repo), NULL);
8514 error = tog_load_refs(repo, 0);
8518 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8520 error = got_error_from_errno("view_open");
8524 error = open_ref_view(view, repo);
8529 /* Release work tree lock. */
8530 got_worktree_close(worktree);
8533 error = view_loop(view);
8538 const struct got_error *close_err = got_repo_close(repo);
8543 const struct got_error *pack_err =
8544 got_repo_pack_fds_close(pack_fds);
8552 static const struct got_error*
8553 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8562 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8566 if (mvwprintw(win, y, x, "%s", str) == ERR)
8567 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8574 static const struct got_error *
8575 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8579 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8581 free(*line_offsets);
8582 *line_offsets = NULL;
8583 return got_error_from_errno("reallocarray");
8587 (*line_offsets)[*nlines] = off;
8592 static const struct got_error *
8593 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8597 for (;n > 0; --n, ++km) {
8601 if (km->keys == NULL)
8604 t = t0 = strdup(km->keys);
8606 return got_error_from_errno("strdup");
8609 while ((k = strsep(&t, " ")) != NULL)
8610 len += strlen(k) > 1 ? 2 : 0;
8612 *ret = MAX(*ret, len);
8619 * Write keymap section headers, keys, and key info in km to f.
8620 * Save line offset to *off. If terminal has UTF8 encoding enabled,
8621 * wrap control and symbolic keys in guillemets, else use <>.
8623 static const struct got_error *
8624 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8629 static const char *u8_glyph[] = {
8630 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8631 "\xe2\x80\xba" /* U+203A (utf8 >) */
8634 int cs, s, first = 1;
8636 cs = got_locale_is_utf8();
8638 t = t0 = strdup(km->keys);
8640 return got_error_from_errno("strdup");
8642 len = strlen(km->keys);
8643 while ((k = strsep(&t, " ")) != NULL) {
8644 s = strlen(k) > 1; /* control or symbolic key */
8645 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8646 cs && s ? u8_glyph[0] : s ? "<" : "", k,
8647 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8650 return got_error_from_errno("fprintf");
8658 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8660 return got_error_from_errno("fprintf");
8666 static const struct got_error *
8667 format_help(struct tog_help_view_state *s)
8669 const struct got_error *err = NULL;
8671 int i, max, n, show = s->all;
8672 static const struct tog_key_map km[] = {
8673 #define KEYMAP_(info, type) { NULL, (info), type }
8674 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8680 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8685 err = max_key_str(&max, km, n);
8689 for (i = 0; i < n; ++i) {
8690 if (km[i].keys == NULL) {
8692 if (km[i].type == TOG_KEYMAP_GLOBAL ||
8693 km[i].type == s->type || s->all)
8697 err = format_help_line(&off, s->f, &km[i], max);
8700 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8707 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8711 static const struct got_error *
8712 create_help(struct tog_help_view_state *s)
8715 const struct got_error *err;
8717 free(s->line_offsets);
8718 s->line_offsets = NULL;
8723 return got_error_from_errno("got_opentemp");
8726 err = format_help(s);
8730 if (s->f && fflush(s->f) != 0)
8731 return got_error_from_errno("fflush");
8736 static const struct got_error *
8737 search_start_help_view(struct tog_view *view)
8739 view->state.help.matched_line = 0;
8744 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8745 size_t *nlines, int **first, int **last, int **match, int **selected)
8747 struct tog_help_view_state *s = &view->state.help;
8750 *nlines = s->nlines;
8751 *line_offsets = s->line_offsets;
8752 *match = &s->matched_line;
8753 *first = &s->first_displayed_line;
8754 *last = &s->last_displayed_line;
8755 *selected = &s->selected_line;
8758 static const struct got_error *
8759 show_help_view(struct tog_view *view)
8761 struct tog_help_view_state *s = &view->state.help;
8762 const struct got_error *err;
8763 regmatch_t *regmatch = &view->regmatch;
8768 int width, nprinted = 0, rc = 0;
8769 int eos = view->nlines;
8771 if (view_is_hsplit_top(view))
8772 --eos; /* account for border */
8776 werase(view->window);
8778 if (view->gline > s->nlines - 1)
8779 view->gline = s->nlines - 1;
8781 err = win_draw_center(view->window, 0, 0, view->ncols,
8782 view_needs_focus_indication(view),
8783 "tog help (press q to return to tog)");
8788 waddstr(view->window, "\n\n");
8794 while (eos > 0 && nprinted < eos) {
8797 linelen = getline(&line, &linesz, s->f);
8798 if (linelen == -1) {
8801 return got_ferror(s->f, GOT_ERR_IO);
8806 if (++s->lineno < s->first_displayed_line)
8808 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8810 if (s->lineno == view->hiline)
8813 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8819 view->maxx = MAX(view->maxx, width);
8824 wattron(view->window, attr);
8825 if (s->first_displayed_line + nprinted == s->matched_line &&
8826 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8827 err = add_matched_line(&width, line, view->ncols - 1, 0,
8828 view->window, view->x, regmatch);
8836 err = format_line(&wline, &width, &skip, line,
8837 view->x, view->ncols - 1, 0, view->x ? 1 : 0);
8842 rc = waddwstr(view->window, &wline[skip]);
8846 return got_error_msg(GOT_ERR_IO, "waddwstr");
8848 if (s->lineno == view->hiline) {
8849 while (width++ < view->ncols)
8850 waddch(view->window, ' ');
8852 if (width <= view->ncols)
8853 waddch(view->window, '\n');
8856 wattroff(view->window, attr);
8857 if (++nprinted == 1)
8858 s->first_displayed_line = s->lineno;
8862 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8864 s->last_displayed_line = s->first_displayed_line;
8869 rc = waddnstr(view->window,
8870 "See the tog(1) manual page for full documentation",
8873 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8875 wmove(view->window, view->nlines - 1, 0);
8876 wclrtoeol(view->window);
8877 wstandout(view->window);
8878 rc = waddnstr(view->window, "scroll down for more...",
8881 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8882 if (getcurx(view->window) < view->ncols - 6) {
8883 rc = wprintw(view->window, "[%.0f%%]",
8884 100.00 * s->last_displayed_line / s->nlines);
8886 return got_error_msg(GOT_ERR_IO, "wprintw");
8888 wstandend(view->window);
8894 static const struct got_error *
8895 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8897 struct tog_help_view_state *s = &view->state.help;
8898 const struct got_error *err = NULL;
8904 eos = nscroll = view->nlines;
8905 if (view_is_hsplit_top(view))
8908 s->lineno = s->first_displayed_line - 1 + s->selected_line;
8915 view->x = MAX(view->maxx - view->ncols / 3, 0);
8920 if (view->x + view->ncols / 3 < view->maxx)
8927 view->x -= MIN(view->x, 2);
8933 s->first_displayed_line = 1;
8941 s->first_displayed_line = (s->nlines - eos) + 3;
8946 if (s->first_displayed_line > 1)
8947 --s->first_displayed_line;
8958 if (s->first_displayed_line == 1) {
8962 while (--nscroll > 0 && s->first_displayed_line > 1)
8963 s->first_displayed_line--;
8969 ++s->first_displayed_line;
8985 while (!s->eof && --nscroll > 0) {
8986 linelen = getline(&line, &linesz, s->f);
8987 s->first_displayed_line++;
8988 if (linelen == -1) {
8992 err = got_ferror(s->f, GOT_ERR_IO);
9006 static const struct got_error *
9007 close_help_view(struct tog_view *view)
9009 struct tog_help_view_state *s = &view->state.help;
9011 free(s->line_offsets);
9012 s->line_offsets = NULL;
9013 if (fclose(s->f) == EOF)
9014 return got_error_from_errno("fclose");
9019 static const struct got_error *
9020 reset_help_view(struct tog_view *view)
9022 struct tog_help_view_state *s = &view->state.help;
9025 if (s->f && fclose(s->f) == EOF)
9026 return got_error_from_errno("fclose");
9028 wclear(view->window);
9032 s->first_displayed_line = 1;
9033 s->last_displayed_line = view->nlines;
9034 s->matched_line = 0;
9036 return create_help(s);
9039 static const struct got_error *
9040 open_help_view(struct tog_view *view, struct tog_view *parent)
9042 const struct got_error *err = NULL;
9043 struct tog_help_view_state *s = &view->state.help;
9045 s->type = (enum tog_keymap_type)parent->type;
9046 s->first_displayed_line = 1;
9047 s->last_displayed_line = view->nlines;
9048 s->selected_line = 1;
9050 view->show = show_help_view;
9051 view->input = input_help_view;
9052 view->reset = reset_help_view;
9053 view->close = close_help_view;
9054 view->search_start = search_start_help_view;
9055 view->search_setup = search_setup_help_view;
9056 view->search_next = search_next_view_match;
9058 err = create_help(s);
9062 static const struct got_error *
9063 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9064 enum tog_view_type request, int y, int x)
9066 const struct got_error *err = NULL;
9072 if (view->type == TOG_VIEW_LOG) {
9073 struct tog_log_view_state *s = &view->state.log;
9075 err = open_diff_view_for_commit(new_view, y, x,
9076 s->selected_entry->commit, s->selected_entry->id,
9079 return got_error_msg(GOT_ERR_NOT_IMPL,
9080 "parent/child view pair not supported");
9082 case TOG_VIEW_BLAME:
9083 if (view->type == TOG_VIEW_TREE) {
9084 struct tog_tree_view_state *s = &view->state.tree;
9086 err = blame_tree_entry(new_view, y, x,
9087 s->selected_entry, &s->parents, s->commit_id,
9090 return got_error_msg(GOT_ERR_NOT_IMPL,
9091 "parent/child view pair not supported");
9094 if (view->type == TOG_VIEW_BLAME)
9095 err = log_annotated_line(new_view, y, x,
9096 view->state.blame.repo, view->state.blame.id_to_log);
9097 else if (view->type == TOG_VIEW_TREE)
9098 err = log_selected_tree_entry(new_view, y, x,
9100 else if (view->type == TOG_VIEW_REF)
9101 err = log_ref_entry(new_view, y, x,
9102 view->state.ref.selected_entry,
9103 view->state.ref.repo);
9105 return got_error_msg(GOT_ERR_NOT_IMPL,
9106 "parent/child view pair not supported");
9109 if (view->type == TOG_VIEW_LOG)
9110 err = browse_commit_tree(new_view, y, x,
9111 view->state.log.selected_entry,
9112 view->state.log.in_repo_path,
9113 view->state.log.head_ref_name,
9114 view->state.log.repo);
9115 else if (view->type == TOG_VIEW_REF)
9116 err = browse_ref_tree(new_view, y, x,
9117 view->state.ref.selected_entry,
9118 view->state.ref.repo);
9120 return got_error_msg(GOT_ERR_NOT_IMPL,
9121 "parent/child view pair not supported");
9124 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9125 if (*new_view == NULL)
9126 return got_error_from_errno("view_open");
9127 if (view->type == TOG_VIEW_LOG)
9128 err = open_ref_view(*new_view, view->state.log.repo);
9129 else if (view->type == TOG_VIEW_TREE)
9130 err = open_ref_view(*new_view, view->state.tree.repo);
9132 err = got_error_msg(GOT_ERR_NOT_IMPL,
9133 "parent/child view pair not supported");
9135 view_close(*new_view);
9138 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9139 if (*new_view == NULL)
9140 return got_error_from_errno("view_open");
9141 err = open_help_view(*new_view, view);
9143 view_close(*new_view);
9146 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9153 * If view was scrolled down to move the selected line into view when opening a
9154 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9157 offset_selection_up(struct tog_view *view)
9159 switch (view->type) {
9160 case TOG_VIEW_BLAME: {
9161 struct tog_blame_view_state *s = &view->state.blame;
9162 if (s->first_displayed_line == 1) {
9163 s->selected_line = MAX(s->selected_line - view->offset,
9167 if (s->first_displayed_line > view->offset)
9168 s->first_displayed_line -= view->offset;
9170 s->first_displayed_line = 1;
9171 s->selected_line += view->offset;
9175 log_scroll_up(&view->state.log, view->offset);
9176 view->state.log.selected += view->offset;
9179 ref_scroll_up(&view->state.ref, view->offset);
9180 view->state.ref.selected += view->offset;
9183 tree_scroll_up(&view->state.tree, view->offset);
9184 view->state.tree.selected += view->offset;
9194 * If the selected line is in the section of screen covered by the bottom split,
9195 * scroll down offset lines to move it into view and index its new position.
9197 static const struct got_error *
9198 offset_selection_down(struct tog_view *view)
9200 const struct got_error *err = NULL;
9201 const struct got_error *(*scrolld)(struct tog_view *, int);
9202 int *selected = NULL;
9205 switch (view->type) {
9206 case TOG_VIEW_BLAME: {
9207 struct tog_blame_view_state *s = &view->state.blame;
9210 if (s->selected_line > view->nlines - header) {
9211 offset = abs(view->nlines - s->selected_line - header);
9212 s->first_displayed_line += offset;
9213 s->selected_line -= offset;
9214 view->offset = offset;
9218 case TOG_VIEW_LOG: {
9219 struct tog_log_view_state *s = &view->state.log;
9220 scrolld = &log_scroll_down;
9221 header = view_is_parent_view(view) ? 3 : 2;
9222 selected = &s->selected;
9225 case TOG_VIEW_REF: {
9226 struct tog_ref_view_state *s = &view->state.ref;
9227 scrolld = &ref_scroll_down;
9229 selected = &s->selected;
9232 case TOG_VIEW_TREE: {
9233 struct tog_tree_view_state *s = &view->state.tree;
9234 scrolld = &tree_scroll_down;
9236 selected = &s->selected;
9246 if (selected && *selected > view->nlines - header) {
9247 offset = abs(view->nlines - *selected - header);
9248 view->offset = offset;
9249 if (scrolld && offset) {
9250 err = scrolld(view, offset);
9251 *selected -= offset;
9259 list_commands(FILE *fp)
9263 fprintf(fp, "commands:");
9264 for (i = 0; i < nitems(tog_commands); i++) {
9265 const struct tog_cmd *cmd = &tog_commands[i];
9266 fprintf(fp, " %s", cmd->name);
9272 usage(int hflag, int status)
9274 FILE *fp = (status == 0) ? stdout : stderr;
9276 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9279 fprintf(fp, "lazy usage: %s path\n", getprogname());
9286 make_argv(int argc, ...)
9294 argv = calloc(argc, sizeof(char *));
9297 for (i = 0; i < argc; i++) {
9298 argv[i] = strdup(va_arg(ap, char *));
9299 if (argv[i] == NULL)
9308 * Try to convert 'tog path' into a 'tog log path' command.
9309 * The user could simply have mistyped the command rather than knowingly
9310 * provided a path. So check whether argv[0] can in fact be resolved
9311 * to a path in the HEAD commit and print a special error if not.
9312 * This hack is for mpi@ <3
9314 static const struct got_error *
9315 tog_log_with_path(int argc, char *argv[])
9317 const struct got_error *error = NULL, *close_err;
9318 const struct tog_cmd *cmd = NULL;
9319 struct got_repository *repo = NULL;
9320 struct got_worktree *worktree = NULL;
9321 struct got_object_id *commit_id = NULL, *id = NULL;
9322 struct got_commit_object *commit = NULL;
9323 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9324 char *commit_id_str = NULL, **cmd_argv = NULL;
9325 int *pack_fds = NULL;
9327 cwd = getcwd(NULL, 0);
9329 return got_error_from_errno("getcwd");
9331 error = got_repo_pack_fds_open(&pack_fds);
9335 error = got_worktree_open(&worktree, cwd);
9336 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9340 repo_path = strdup(got_worktree_get_repo_path(worktree));
9342 repo_path = strdup(cwd);
9343 if (repo_path == NULL) {
9344 error = got_error_from_errno("strdup");
9348 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9352 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9357 error = tog_load_refs(repo, 0);
9360 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9361 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9362 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9367 got_worktree_close(worktree);
9371 error = got_object_open_as_commit(&commit, repo, commit_id);
9375 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9377 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9379 fprintf(stderr, "%s: '%s' is no known command or path\n",
9380 getprogname(), argv[0]);
9385 error = got_object_id_str(&commit_id_str, commit_id);
9389 cmd = &tog_commands[0]; /* log */
9391 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9392 error = cmd->cmd_main(argc, cmd_argv);
9395 close_err = got_repo_close(repo);
9400 got_object_commit_close(commit);
9402 got_worktree_close(worktree);
9404 const struct got_error *pack_err =
9405 got_repo_pack_fds_close(pack_fds);
9410 free(commit_id_str);
9417 for (i = 0; i < argc; i++)
9426 main(int argc, char *argv[])
9428 const struct got_error *error = NULL;
9429 const struct tog_cmd *cmd = NULL;
9430 int ch, hflag = 0, Vflag = 0;
9431 char **cmd_argv = NULL;
9432 static const struct option longopts[] = {
9433 { "version", no_argument, NULL, 'V' },
9436 char *diff_algo_str = NULL;
9438 if (!isatty(STDIN_FILENO))
9439 errx(1, "standard input is not a tty");
9441 setlocale(LC_CTYPE, "");
9443 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9463 got_version_print_str();
9468 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9476 /* Build an argument vector which runs a default command. */
9477 cmd = &tog_commands[0];
9479 cmd_argv = make_argv(argc, cmd->name);
9483 /* Did the user specify a command? */
9484 for (i = 0; i < nitems(tog_commands); i++) {
9485 if (strncmp(tog_commands[i].name, argv[0],
9486 strlen(argv[0])) == 0) {
9487 cmd = &tog_commands[i];
9493 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9494 if (diff_algo_str) {
9495 if (strcasecmp(diff_algo_str, "patience") == 0)
9496 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9497 if (strcasecmp(diff_algo_str, "myers") == 0)
9498 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9504 /* No command specified; try log with a path */
9505 error = tog_log_with_path(argc, argv);
9510 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9516 for (i = 0; i < argc; i++)
9521 if (error && error->code != GOT_ERR_CANCELLED &&
9522 error->code != GOT_ERR_EOF &&
9523 error->code != GOT_ERR_PRIVSEP_EXIT &&
9524 error->code != GOT_ERR_PRIVSEP_PIPE &&
9525 !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9526 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);