2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_opentemp.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
57 #include "got_worktree.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 #define CTRL(x) ((x) & 0x1f)
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static const struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
109 /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
110 enum tog_keymap_type {
111 TOG_KEYMAP_KEYS = -2,
127 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
129 #define TOG_EOF_STRING "(END)"
131 struct commit_queue_entry {
132 TAILQ_ENTRY(commit_queue_entry) entry;
133 struct got_object_id *id;
134 struct got_commit_object *commit;
137 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
138 struct commit_queue {
140 struct commit_queue_head head;
144 STAILQ_ENTRY(tog_color) entry;
148 STAILQ_HEAD(tog_colors, tog_color);
150 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
151 static struct got_reflist_object_id_map *tog_refs_idmap;
152 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
154 static const struct got_error *
155 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
156 struct got_reference* re2)
158 const char *name1 = got_ref_get_name(re1);
159 const char *name2 = got_ref_get_name(re2);
160 int isbackup1, isbackup2;
162 /* Sort backup refs towards the bottom of the list. */
163 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
164 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
165 if (!isbackup1 && isbackup2) {
168 } else if (isbackup1 && !isbackup2) {
173 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
177 static const struct got_error *
178 tog_load_refs(struct got_repository *repo, int sort_by_date)
180 const struct got_error *err;
182 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
183 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
188 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
195 if (tog_refs_idmap) {
196 got_reflist_object_id_map_free(tog_refs_idmap);
197 tog_refs_idmap = NULL;
199 got_ref_list_free(&tog_refs);
202 static const struct got_error *
203 add_color(struct tog_colors *colors, const char *pattern,
204 int idx, short color)
206 const struct got_error *err = NULL;
207 struct tog_color *tc;
210 if (idx < 1 || idx > COLOR_PAIRS - 1)
213 init_pair(idx, color, -1);
215 tc = calloc(1, sizeof(*tc));
217 return got_error_from_errno("calloc");
218 regerr = regcomp(&tc->regex, pattern,
219 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
221 static char regerr_msg[512];
222 static char err_msg[512];
223 regerror(regerr, &tc->regex, regerr_msg,
225 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
227 err = got_error_msg(GOT_ERR_REGEX, err_msg);
232 STAILQ_INSERT_HEAD(colors, tc, entry);
237 free_colors(struct tog_colors *colors)
239 struct tog_color *tc;
241 while (!STAILQ_EMPTY(colors)) {
242 tc = STAILQ_FIRST(colors);
243 STAILQ_REMOVE_HEAD(colors, entry);
249 static struct tog_color *
250 get_color(struct tog_colors *colors, int colorpair)
252 struct tog_color *tc = NULL;
254 STAILQ_FOREACH(tc, colors, entry) {
255 if (tc->colorpair == colorpair)
263 default_color_value(const char *envvar)
265 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
266 return COLOR_MAGENTA;
267 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
269 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
271 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
273 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
274 return COLOR_MAGENTA;
275 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
276 return COLOR_MAGENTA;
277 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
279 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
281 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
283 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
285 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
287 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
289 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
290 return COLOR_MAGENTA;
291 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
293 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
300 get_color_value(const char *envvar)
302 const char *val = getenv(envvar);
305 return default_color_value(envvar);
307 if (strcasecmp(val, "black") == 0)
309 if (strcasecmp(val, "red") == 0)
311 if (strcasecmp(val, "green") == 0)
313 if (strcasecmp(val, "yellow") == 0)
315 if (strcasecmp(val, "blue") == 0)
317 if (strcasecmp(val, "magenta") == 0)
318 return COLOR_MAGENTA;
319 if (strcasecmp(val, "cyan") == 0)
321 if (strcasecmp(val, "white") == 0)
323 if (strcasecmp(val, "default") == 0)
326 return default_color_value(envvar);
329 struct tog_diff_view_state {
330 struct got_object_id *id1, *id2;
331 const char *label1, *label2;
335 int first_displayed_line;
336 int last_displayed_line;
339 int ignore_whitespace;
341 struct got_repository *repo;
342 struct got_diff_line *lines;
347 /* passed from log or blame view; may be NULL */
348 struct tog_view *parent_view;
351 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
352 static volatile sig_atomic_t tog_thread_error;
354 struct tog_log_thread_args {
355 pthread_cond_t need_commits;
356 pthread_cond_t commit_loaded;
359 struct got_commit_graph *graph;
360 struct commit_queue *real_commits;
361 const char *in_repo_path;
362 struct got_object_id *start_id;
363 struct got_repository *repo;
367 struct commit_queue_entry **first_displayed_entry;
368 struct commit_queue_entry **selected_entry;
370 int *search_next_done;
374 regex_t *limit_regex;
375 struct commit_queue *limit_commits;
378 struct tog_log_view_state {
379 struct commit_queue *commits;
380 struct commit_queue_entry *first_displayed_entry;
381 struct commit_queue_entry *last_displayed_entry;
382 struct commit_queue_entry *selected_entry;
383 struct commit_queue real_commits;
388 struct got_repository *repo;
389 struct got_object_id *start_id;
392 struct tog_log_thread_args thread_args;
393 struct commit_queue_entry *matched_entry;
394 struct commit_queue_entry *search_entry;
395 struct tog_colors colors;
399 struct commit_queue limit_commits;
402 #define TOG_COLOR_DIFF_MINUS 1
403 #define TOG_COLOR_DIFF_PLUS 2
404 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
405 #define TOG_COLOR_DIFF_META 4
406 #define TOG_COLOR_TREE_SUBMODULE 5
407 #define TOG_COLOR_TREE_SYMLINK 6
408 #define TOG_COLOR_TREE_DIRECTORY 7
409 #define TOG_COLOR_TREE_EXECUTABLE 8
410 #define TOG_COLOR_COMMIT 9
411 #define TOG_COLOR_AUTHOR 10
412 #define TOG_COLOR_DATE 11
413 #define TOG_COLOR_REFS_HEADS 12
414 #define TOG_COLOR_REFS_TAGS 13
415 #define TOG_COLOR_REFS_REMOTES 14
416 #define TOG_COLOR_REFS_BACKUP 15
418 struct tog_blame_cb_args {
419 struct tog_blame_line *lines; /* one per line */
422 struct tog_view *view;
423 struct got_object_id *commit_id;
427 struct tog_blame_thread_args {
429 struct got_repository *repo;
430 struct tog_blame_cb_args *cb_args;
432 got_cancel_cb cancel_cb;
439 struct tog_blame_line *lines;
443 struct tog_blame_thread_args thread_args;
444 struct tog_blame_cb_args cb_args;
449 struct tog_blame_view_state {
450 int first_displayed_line;
451 int last_displayed_line;
453 int last_diffed_line;
457 struct got_object_id_queue blamed_commits;
458 struct got_object_qid *blamed_commit;
460 struct got_repository *repo;
461 struct got_object_id *commit_id;
462 struct got_object_id *id_to_log;
463 struct tog_blame blame;
465 struct tog_colors colors;
468 struct tog_parent_tree {
469 TAILQ_ENTRY(tog_parent_tree) entry;
470 struct got_tree_object *tree;
471 struct got_tree_entry *first_displayed_entry;
472 struct got_tree_entry *selected_entry;
476 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
478 struct tog_tree_view_state {
480 struct got_object_id *commit_id;/* commit which this tree belongs to */
481 struct got_tree_object *root; /* the commit's root tree entry */
482 struct got_tree_object *tree; /* currently displayed (sub-)tree */
483 struct got_tree_entry *first_displayed_entry;
484 struct got_tree_entry *last_displayed_entry;
485 struct got_tree_entry *selected_entry;
486 int ndisplayed, selected, show_ids;
487 struct tog_parent_trees parents; /* parent trees of current sub-tree */
489 struct got_repository *repo;
490 struct got_tree_entry *matched_entry;
491 struct tog_colors colors;
494 struct tog_reflist_entry {
495 TAILQ_ENTRY(tog_reflist_entry) entry;
496 struct got_reference *ref;
500 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
502 struct tog_ref_view_state {
503 struct tog_reflist_head refs;
504 struct tog_reflist_entry *first_displayed_entry;
505 struct tog_reflist_entry *last_displayed_entry;
506 struct tog_reflist_entry *selected_entry;
507 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
508 struct got_repository *repo;
509 struct tog_reflist_entry *matched_entry;
510 struct tog_colors colors;
513 struct tog_help_view_state {
518 int first_displayed_line;
519 int last_displayed_line;
524 enum tog_keymap_type type;
527 #define GENERATE_HELP \
528 KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
529 KEY_("H F1", "Open view-specific help (double tap for all help)"), \
530 KEY_("k C-p Up", "Move cursor or page up one line"), \
531 KEY_("j C-n Down", "Move cursor or page down one line"), \
532 KEY_("C-b b PgUp", "Scroll the view up one page"), \
533 KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
534 KEY_("C-u u", "Scroll the view up one half page"), \
535 KEY_("C-d d", "Scroll the view down one half page"), \
536 KEY_("g", "Go to line N (default: first line)"), \
537 KEY_("Home =", "Go to the first line"), \
538 KEY_("G", "Go to line N (default: last line)"), \
539 KEY_("End *", "Go to the last line"), \
540 KEY_("l Right", "Scroll the view right"), \
541 KEY_("h Left", "Scroll the view left"), \
542 KEY_("$", "Scroll view to the rightmost position"), \
543 KEY_("0", "Scroll view to the leftmost position"), \
544 KEY_("-", "Decrease size of the focussed split"), \
545 KEY_("+", "Increase size of the focussed split"), \
546 KEY_("Tab", "Switch focus between views"), \
547 KEY_("F", "Toggle fullscreen mode"), \
548 KEY_("/", "Open prompt to enter search term"), \
549 KEY_("n", "Find next line/token matching the current search term"), \
550 KEY_("N", "Find previous line/token matching the current search term"),\
551 KEY_("q", "Quit the focussed view; Quit help screen"), \
552 KEY_("Q", "Quit tog"), \
554 KEYMAP_("Log view", TOG_KEYMAP_LOG), \
555 KEY_("< ,", "Move cursor up one commit"), \
556 KEY_("> .", "Move cursor down one commit"), \
557 KEY_("Enter", "Open diff view of the selected commit"), \
558 KEY_("B", "Reload the log view and toggle display of merged commits"), \
559 KEY_("R", "Open ref view of all repository references"), \
560 KEY_("T", "Display tree view of the repository from the selected" \
562 KEY_("@", "Toggle between displaying author and committer name"), \
563 KEY_("&", "Open prompt to enter term to limit commits displayed"), \
564 KEY_("C-g Backspace", "Cancel current search or log operation"), \
565 KEY_("C-l", "Reload the log view with new commits in the repository"), \
567 KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
568 KEY_("K < ,", "Display diff of next line in the file/log entry"), \
569 KEY_("J > .", "Display diff of previous line in the file/log entry"), \
570 KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
571 KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
573 KEY_("(", "Go to the previous file in the diff"), \
574 KEY_(")", "Go to the next file in the diff"), \
575 KEY_("{", "Go to the previous hunk in the diff"), \
576 KEY_("}", "Go to the next hunk in the diff"), \
577 KEY_("[", "Decrease the number of context lines"), \
578 KEY_("]", "Increase the number of context lines"), \
579 KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
581 KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
582 KEY_("Enter", "Display diff view of the selected line's commit"), \
583 KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
584 KEY_("L", "Open log view for the currently selected annotated line"), \
585 KEY_("C", "Reload view with the previously blamed commit"), \
586 KEY_("c", "Reload view with the version of the file found in the" \
587 " selected line's commit"), \
588 KEY_("p", "Reload view with the version of the file found in the" \
589 " selected line's parent commit"), \
591 KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
592 KEY_("Enter", "Enter selected directory or open blame view of the" \
594 KEY_("L", "Open log view for the selected entry"), \
595 KEY_("R", "Open ref view of all repository references"), \
596 KEY_("i", "Show object IDs for all tree entries"), \
597 KEY_("Backspace", "Return to the parent directory"), \
599 KEYMAP_("Ref view", TOG_KEYMAP_REF), \
600 KEY_("Enter", "Display log view of the selected reference"), \
601 KEY_("T", "Display tree view of the selected reference"), \
602 KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
603 KEY_("m", "Toggle display of last modified date for each reference"), \
604 KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
605 KEY_("C-l", "Reload view with all repository references")
610 enum tog_keymap_type type;
614 * We implement two types of views: parent views and child views.
616 * The 'Tab' key switches focus between a parent view and its child view.
617 * Child views are shown side-by-side to their parent view, provided
618 * there is enough screen estate.
620 * When a new view is opened from within a parent view, this new view
621 * becomes a child view of the parent view, replacing any existing child.
623 * When a new view is opened from within a child view, this new view
624 * becomes a parent view which will obscure the views below until the
625 * user quits the new parent view by typing 'q'.
627 * This list of views contains parent views only.
628 * Child views are only pointed to by their parent view.
630 TAILQ_HEAD(tog_view_list_head, tog_view);
633 TAILQ_ENTRY(tog_view) entry;
636 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
637 int resized_y, resized_x; /* begin_y/x based on user resizing */
638 int maxx, x; /* max column and current start column */
639 int lines, cols; /* copies of LINES and COLS */
640 int nscrolled, offset; /* lines scrolled and hsplit line offset */
641 int gline, hiline; /* navigate to and highlight this nG line */
642 int ch, count; /* current keymap and count prefix */
643 int resized; /* set when in a resize event */
644 int focussed; /* Only set on one parent or child view at a time. */
646 struct tog_view *parent;
647 struct tog_view *child;
650 * This flag is initially set on parent views when a new child view
651 * is created. It gets toggled when the 'Tab' key switches focus
652 * between parent and child.
653 * The flag indicates whether focus should be passed on to our child
654 * view if this parent view gets picked for focus after another parent
655 * view was closed. This prevents child views from losing focus in such
660 enum tog_view_mode mode;
661 /* type-specific state */
662 enum tog_view_type type;
664 struct tog_diff_view_state diff;
665 struct tog_log_view_state log;
666 struct tog_blame_view_state blame;
667 struct tog_tree_view_state tree;
668 struct tog_ref_view_state ref;
669 struct tog_help_view_state help;
672 const struct got_error *(*show)(struct tog_view *);
673 const struct got_error *(*input)(struct tog_view **,
674 struct tog_view *, int);
675 const struct got_error *(*reset)(struct tog_view *);
676 const struct got_error *(*resize)(struct tog_view *, int);
677 const struct got_error *(*close)(struct tog_view *);
679 const struct got_error *(*search_start)(struct tog_view *);
680 const struct got_error *(*search_next)(struct tog_view *);
681 void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
682 int **, int **, int **, int **);
685 #define TOG_SEARCH_FORWARD 1
686 #define TOG_SEARCH_BACKWARD 2
687 int search_next_done;
688 #define TOG_SEARCH_HAVE_MORE 1
689 #define TOG_SEARCH_NO_MORE 2
690 #define TOG_SEARCH_HAVE_NONE 3
695 static const struct got_error *open_diff_view(struct tog_view *,
696 struct got_object_id *, struct got_object_id *,
697 const char *, const char *, int, int, int, struct tog_view *,
698 struct got_repository *);
699 static const struct got_error *show_diff_view(struct tog_view *);
700 static const struct got_error *input_diff_view(struct tog_view **,
701 struct tog_view *, int);
702 static const struct got_error *reset_diff_view(struct tog_view *);
703 static const struct got_error* close_diff_view(struct tog_view *);
704 static const struct got_error *search_start_diff_view(struct tog_view *);
705 static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
706 size_t *, int **, int **, int **, int **);
707 static const struct got_error *search_next_view_match(struct tog_view *);
709 static const struct got_error *open_log_view(struct tog_view *,
710 struct got_object_id *, struct got_repository *,
711 const char *, const char *, int);
712 static const struct got_error * show_log_view(struct tog_view *);
713 static const struct got_error *input_log_view(struct tog_view **,
714 struct tog_view *, int);
715 static const struct got_error *resize_log_view(struct tog_view *, int);
716 static const struct got_error *close_log_view(struct tog_view *);
717 static const struct got_error *search_start_log_view(struct tog_view *);
718 static const struct got_error *search_next_log_view(struct tog_view *);
720 static const struct got_error *open_blame_view(struct tog_view *, char *,
721 struct got_object_id *, struct got_repository *);
722 static const struct got_error *show_blame_view(struct tog_view *);
723 static const struct got_error *input_blame_view(struct tog_view **,
724 struct tog_view *, int);
725 static const struct got_error *reset_blame_view(struct tog_view *);
726 static const struct got_error *close_blame_view(struct tog_view *);
727 static const struct got_error *search_start_blame_view(struct tog_view *);
728 static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
729 size_t *, int **, int **, int **, int **);
731 static const struct got_error *open_tree_view(struct tog_view *,
732 struct got_object_id *, const char *, struct got_repository *);
733 static const struct got_error *show_tree_view(struct tog_view *);
734 static const struct got_error *input_tree_view(struct tog_view **,
735 struct tog_view *, int);
736 static const struct got_error *close_tree_view(struct tog_view *);
737 static const struct got_error *search_start_tree_view(struct tog_view *);
738 static const struct got_error *search_next_tree_view(struct tog_view *);
740 static const struct got_error *open_ref_view(struct tog_view *,
741 struct got_repository *);
742 static const struct got_error *show_ref_view(struct tog_view *);
743 static const struct got_error *input_ref_view(struct tog_view **,
744 struct tog_view *, int);
745 static const struct got_error *close_ref_view(struct tog_view *);
746 static const struct got_error *search_start_ref_view(struct tog_view *);
747 static const struct got_error *search_next_ref_view(struct tog_view *);
749 static const struct got_error *open_help_view(struct tog_view *,
751 static const struct got_error *show_help_view(struct tog_view *);
752 static const struct got_error *input_help_view(struct tog_view **,
753 struct tog_view *, int);
754 static const struct got_error *reset_help_view(struct tog_view *);
755 static const struct got_error* close_help_view(struct tog_view *);
756 static const struct got_error *search_start_help_view(struct tog_view *);
757 static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
758 size_t *, int **, int **, int **, int **);
760 static volatile sig_atomic_t tog_sigwinch_received;
761 static volatile sig_atomic_t tog_sigpipe_received;
762 static volatile sig_atomic_t tog_sigcont_received;
763 static volatile sig_atomic_t tog_sigint_received;
764 static volatile sig_atomic_t tog_sigterm_received;
767 tog_sigwinch(int signo)
769 tog_sigwinch_received = 1;
773 tog_sigpipe(int signo)
775 tog_sigpipe_received = 1;
779 tog_sigcont(int signo)
781 tog_sigcont_received = 1;
785 tog_sigint(int signo)
787 tog_sigint_received = 1;
791 tog_sigterm(int signo)
793 tog_sigterm_received = 1;
797 tog_fatal_signal_received(void)
799 return (tog_sigpipe_received ||
800 tog_sigint_received || tog_sigterm_received);
803 static const struct got_error *
804 view_close(struct tog_view *view)
806 const struct got_error *err = NULL, *child_err = NULL;
809 child_err = view_close(view->child);
813 err = view->close(view);
815 del_panel(view->panel);
817 delwin(view->window);
819 return err ? err : child_err;
822 static struct tog_view *
823 view_open(int nlines, int ncols, int begin_y, int begin_x,
824 enum tog_view_type type)
826 struct tog_view *view = calloc(1, sizeof(*view));
834 view->nlines = nlines ? nlines : LINES - begin_y;
835 view->ncols = ncols ? ncols : COLS - begin_x;
836 view->begin_y = begin_y;
837 view->begin_x = begin_x;
838 view->window = newwin(nlines, ncols, begin_y, begin_x);
839 if (view->window == NULL) {
843 view->panel = new_panel(view->window);
844 if (view->panel == NULL ||
845 set_panel_userptr(view->panel, view) != OK) {
850 keypad(view->window, TRUE);
855 view_split_begin_x(int begin_x)
857 if (begin_x > 0 || COLS < 120)
859 return (COLS - MAX(COLS / 2, 80));
862 /* XXX Stub till we decide what to do. */
864 view_split_begin_y(int lines)
866 return lines * HSPLIT_SCALE;
869 static const struct got_error *view_resize(struct tog_view *);
871 static const struct got_error *
872 view_splitscreen(struct tog_view *view)
874 const struct got_error *err = NULL;
876 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
877 if (view->resized_y && view->resized_y < view->lines)
878 view->begin_y = view->resized_y;
880 view->begin_y = view_split_begin_y(view->nlines);
882 } else if (!view->resized) {
883 if (view->resized_x && view->resized_x < view->cols - 1 &&
885 view->begin_x = view->resized_x;
887 view->begin_x = view_split_begin_x(0);
890 view->nlines = LINES - view->begin_y;
891 view->ncols = COLS - view->begin_x;
894 err = view_resize(view);
898 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
899 view->parent->nlines = view->begin_y;
901 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
902 return got_error_from_errno("mvwin");
907 static const struct got_error *
908 view_fullscreen(struct tog_view *view)
910 const struct got_error *err = NULL;
913 view->begin_y = view->resized ? view->begin_y : 0;
914 view->nlines = view->resized ? view->nlines : LINES;
918 err = view_resize(view);
922 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
923 return got_error_from_errno("mvwin");
929 view_is_parent_view(struct tog_view *view)
931 return view->parent == NULL;
935 view_is_splitscreen(struct tog_view *view)
937 return view->begin_x > 0 || view->begin_y > 0;
941 view_is_fullscreen(struct tog_view *view)
943 return view->nlines == LINES && view->ncols == COLS;
947 view_is_hsplit_top(struct tog_view *view)
949 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
950 view_is_splitscreen(view->child);
954 view_border(struct tog_view *view)
957 const struct tog_view *view_above;
960 return view_border(view->parent);
962 panel = panel_above(view->panel);
966 view_above = panel_userptr(panel);
967 if (view->mode == TOG_VIEW_SPLIT_HRZN)
968 mvwhline(view->window, view_above->begin_y - 1,
969 view->begin_x, got_locale_is_utf8() ?
970 ACS_HLINE : '-', view->ncols);
972 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
973 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
976 static const struct got_error *view_init_hsplit(struct tog_view *, int);
977 static const struct got_error *request_log_commits(struct tog_view *);
978 static const struct got_error *offset_selection_down(struct tog_view *);
979 static void offset_selection_up(struct tog_view *);
980 static void view_get_split(struct tog_view *, int *, int *);
982 static const struct got_error *
983 view_resize(struct tog_view *view)
985 const struct got_error *err = NULL;
986 int dif, nlines, ncols;
988 dif = LINES - view->lines; /* line difference */
990 if (view->lines > LINES)
991 nlines = view->nlines - (view->lines - LINES);
993 nlines = view->nlines + (LINES - view->lines);
994 if (view->cols > COLS)
995 ncols = view->ncols - (view->cols - COLS);
997 ncols = view->ncols + (COLS - view->cols);
1000 int hs = view->child->begin_y;
1002 if (!view_is_fullscreen(view))
1003 view->child->begin_x = view_split_begin_x(view->begin_x);
1004 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1005 view->child->begin_x == 0) {
1008 view_fullscreen(view->child);
1009 if (view->child->focussed)
1010 show_panel(view->child->panel);
1012 show_panel(view->panel);
1014 ncols = view->child->begin_x;
1016 view_splitscreen(view->child);
1017 show_panel(view->child->panel);
1020 * XXX This is ugly and needs to be moved into the above
1021 * logic but "works" for now and my attempts at moving it
1022 * break either 'tab' or 'F' key maps in horizontal splits.
1025 err = view_splitscreen(view->child);
1028 if (dif < 0) { /* top split decreased */
1029 err = offset_selection_down(view);
1036 show_panel(view->child->panel);
1037 nlines = view->nlines;
1039 } else if (view->parent == NULL)
1042 if (view->resize && dif > 0) {
1043 err = view->resize(view, dif);
1048 if (wresize(view->window, nlines, ncols) == ERR)
1049 return got_error_from_errno("wresize");
1050 if (replace_panel(view->panel, view->window) == ERR)
1051 return got_error_from_errno("replace_panel");
1052 wclear(view->window);
1054 view->nlines = nlines;
1055 view->ncols = ncols;
1056 view->lines = LINES;
1062 static const struct got_error *
1063 resize_log_view(struct tog_view *view, int increase)
1065 struct tog_log_view_state *s = &view->state.log;
1066 const struct got_error *err = NULL;
1069 if (s->selected_entry)
1070 n = s->selected_entry->idx + view->lines - s->selected;
1073 * Request commits to account for the increased
1074 * height so we have enough to populate the view.
1076 if (s->commits->ncommits < n) {
1077 view->nscrolled = n - s->commits->ncommits + increase + 1;
1078 err = request_log_commits(view);
1085 view_adjust_offset(struct tog_view *view, int n)
1090 if (view->parent && view->parent->offset) {
1091 if (view->parent->offset + n >= 0)
1092 view->parent->offset += n;
1094 view->parent->offset = 0;
1095 } else if (view->offset) {
1096 if (view->offset - n >= 0)
1103 static const struct got_error *
1104 view_resize_split(struct tog_view *view, int resize)
1106 const struct got_error *err = NULL;
1107 struct tog_view *v = NULL;
1114 if (!v->child || !view_is_splitscreen(v->child))
1117 v->resized = v->child->resized = resize; /* lock for resize event */
1119 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1120 if (v->child->resized_y)
1121 v->child->begin_y = v->child->resized_y;
1123 v->child->begin_y -= resize;
1125 v->child->begin_y += resize;
1126 if (v->child->begin_y < 3) {
1128 v->child->begin_y = 3;
1129 } else if (v->child->begin_y > LINES - 1) {
1131 v->child->begin_y = LINES - 1;
1134 v->child->ncols = COLS;
1135 view_adjust_offset(view, resize);
1136 err = view_init_hsplit(v, v->child->begin_y);
1139 v->child->resized_y = v->child->begin_y;
1141 if (v->child->resized_x)
1142 v->child->begin_x = v->child->resized_x;
1144 v->child->begin_x -= resize;
1146 v->child->begin_x += resize;
1147 if (v->child->begin_x < 11) {
1149 v->child->begin_x = 11;
1150 } else if (v->child->begin_x > COLS - 1) {
1152 v->child->begin_x = COLS - 1;
1154 v->child->resized_x = v->child->begin_x;
1157 v->child->mode = v->mode;
1158 v->child->nlines = v->lines - v->child->begin_y;
1159 v->child->ncols = v->cols - v->child->begin_x;
1162 err = view_fullscreen(v);
1165 err = view_splitscreen(v->child);
1169 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1170 err = offset_selection_down(v->child);
1176 err = v->resize(v, 0);
1177 else if (v->child->resize)
1178 err = v->child->resize(v->child, 0);
1180 v->resized = v->child->resized = 0;
1186 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1188 struct tog_view *v = src->child ? src->child : src;
1190 dst->resized_x = v->resized_x;
1191 dst->resized_y = v->resized_y;
1194 static const struct got_error *
1195 view_close_child(struct tog_view *view)
1197 const struct got_error *err = NULL;
1199 if (view->child == NULL)
1202 err = view_close(view->child);
1207 static const struct got_error *
1208 view_set_child(struct tog_view *view, struct tog_view *child)
1210 const struct got_error *err = NULL;
1212 view->child = child;
1213 child->parent = view;
1215 err = view_resize(view);
1219 if (view->child->resized_x || view->child->resized_y)
1220 err = view_resize_split(view, 0);
1225 static const struct got_error *view_dispatch_request(struct tog_view **,
1226 struct tog_view *, enum tog_view_type, int, int);
1228 static const struct got_error *
1229 view_request_new(struct tog_view **requested, struct tog_view *view,
1230 enum tog_view_type request)
1232 struct tog_view *new_view = NULL;
1233 const struct got_error *err;
1238 if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1239 view_get_split(view, &y, &x);
1241 err = view_dispatch_request(&new_view, view, request, y, x);
1245 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1246 request != TOG_VIEW_HELP) {
1247 err = view_init_hsplit(view, y);
1253 new_view->focussed = 1;
1254 new_view->mode = view->mode;
1255 new_view->nlines = request == TOG_VIEW_HELP ?
1256 view->lines : view->lines - y;
1258 if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1259 view_transfer_size(new_view, view);
1260 err = view_close_child(view);
1263 err = view_set_child(view, new_view);
1266 view->focus_child = 1;
1268 *requested = new_view;
1274 tog_resizeterm(void)
1277 struct winsize size;
1279 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1280 cols = 80; /* Default */
1284 lines = size.ws_row;
1286 resize_term(lines, cols);
1289 static const struct got_error *
1290 view_search_start(struct tog_view *view)
1292 const struct got_error *err = NULL;
1293 struct tog_view *v = view;
1297 if (view->search_started) {
1298 regfree(&view->regex);
1299 view->searching = 0;
1300 memset(&view->regmatch, 0, sizeof(view->regmatch));
1302 view->search_started = 0;
1304 if (view->nlines < 1)
1307 if (view_is_hsplit_top(view))
1309 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1312 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1313 wclrtoeol(v->window);
1315 nodelay(v->window, FALSE); /* block for search term input */
1318 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1319 wrefresh(v->window);
1322 nodelay(v->window, TRUE);
1326 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1327 err = view->search_start(view);
1329 regfree(&view->regex);
1332 view->search_started = 1;
1333 view->searching = TOG_SEARCH_FORWARD;
1334 view->search_next_done = 0;
1335 view->search_next(view);
1341 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1342 static const struct got_error *
1343 switch_split(struct tog_view *view)
1345 const struct got_error *err = NULL;
1346 struct tog_view *v = NULL;
1353 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1354 v->mode = TOG_VIEW_SPLIT_VERT;
1356 v->mode = TOG_VIEW_SPLIT_HRZN;
1360 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1361 v->mode = TOG_VIEW_SPLIT_NONE;
1363 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1364 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1365 v->child->begin_y = v->child->resized_y;
1366 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1367 v->child->begin_x = v->child->resized_x;
1370 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1372 v->child->ncols = COLS;
1373 v->child->nscrolled = LINES - v->child->nlines;
1375 err = view_init_hsplit(v, v->child->begin_y);
1379 v->child->mode = v->mode;
1380 v->child->nlines = v->lines - v->child->begin_y;
1383 err = view_fullscreen(v);
1386 err = view_splitscreen(v->child);
1390 if (v->mode == TOG_VIEW_SPLIT_NONE)
1391 v->mode = TOG_VIEW_SPLIT_VERT;
1392 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1393 err = offset_selection_down(v);
1396 err = offset_selection_down(v->child);
1400 offset_selection_up(v);
1401 offset_selection_up(v->child);
1404 err = v->resize(v, 0);
1405 else if (v->child->resize)
1406 err = v->child->resize(v->child, 0);
1412 * Compute view->count from numeric input. Assign total to view->count and
1413 * return first non-numeric key entered.
1416 get_compound_key(struct tog_view *view, int c)
1418 struct tog_view *v = view;
1421 if (view_is_hsplit_top(view))
1423 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1427 cbreak(); /* block for input */
1428 nodelay(view->window, FALSE);
1429 wmove(v->window, v->nlines - 1, 0);
1430 wclrtoeol(v->window);
1431 waddch(v->window, ':');
1434 x = getcurx(v->window);
1435 if (x != ERR && x < view->ncols) {
1436 waddch(v->window, c);
1437 wrefresh(v->window);
1441 * Don't overflow. Max valid request should be the greatest
1442 * between the longest and total lines; cap at 10 million.
1447 n = n * 10 + (c - '0');
1448 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1450 if (c == 'G' || c == 'g') { /* nG key map */
1451 view->gline = view->hiline = n;
1456 /* Massage excessive or inapplicable values at the input handler. */
1462 static const struct got_error *
1463 view_input(struct tog_view **new, int *done, struct tog_view *view,
1464 struct tog_view_list_head *views)
1466 const struct got_error *err = NULL;
1472 /* Clear "no matches" indicator. */
1473 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1474 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1475 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1479 if (view->searching && !view->search_next_done) {
1480 errcode = pthread_mutex_unlock(&tog_mutex);
1482 return got_error_set_errno(errcode,
1483 "pthread_mutex_unlock");
1485 errcode = pthread_mutex_lock(&tog_mutex);
1487 return got_error_set_errno(errcode,
1488 "pthread_mutex_lock");
1489 view->search_next(view);
1493 /* Allow threads to make progress while we are waiting for input. */
1494 errcode = pthread_mutex_unlock(&tog_mutex);
1496 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1497 /* If we have an unfinished count, let C-g or backspace abort. */
1498 if (view->count && --view->count) {
1500 nodelay(view->window, TRUE);
1501 ch = wgetch(view->window);
1502 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1507 ch = wgetch(view->window);
1508 if (ch >= '1' && ch <= '9')
1509 view->ch = ch = get_compound_key(view, ch);
1511 if (view->hiline && ch != ERR && ch != 0)
1512 view->hiline = 0; /* key pressed, clear line highlight */
1513 nodelay(view->window, TRUE);
1514 errcode = pthread_mutex_lock(&tog_mutex);
1516 return got_error_set_errno(errcode, "pthread_mutex_lock");
1518 if (tog_sigwinch_received || tog_sigcont_received) {
1520 tog_sigwinch_received = 0;
1521 tog_sigcont_received = 0;
1522 TAILQ_FOREACH(v, views, entry) {
1523 err = view_resize(v);
1526 err = v->input(new, v, KEY_RESIZE);
1530 err = view_resize(v->child);
1533 err = v->child->input(new, v->child,
1537 if (v->child->resized_x || v->child->resized_y) {
1538 err = view_resize_split(v, 0);
1550 if (view->type == TOG_VIEW_HELP)
1551 err = view->reset(view);
1553 err = view_request_new(new, view, TOG_VIEW_HELP);
1559 view->child->focussed = 1;
1560 view->focus_child = 1;
1561 } else if (view->parent) {
1563 view->parent->focussed = 1;
1564 view->parent->focus_child = 0;
1565 if (!view_is_splitscreen(view)) {
1566 if (view->parent->resize) {
1567 err = view->parent->resize(view->parent,
1572 offset_selection_up(view->parent);
1573 err = view_fullscreen(view->parent);
1580 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1581 if (view->parent->resize) {
1582 /* might need more commits to fill fullscreen */
1583 err = view->parent->resize(view->parent, 0);
1587 offset_selection_up(view->parent);
1589 err = view->input(new, view, ch);
1597 if (view_is_parent_view(view)) {
1598 if (view->child == NULL)
1600 if (view_is_splitscreen(view->child)) {
1602 view->child->focussed = 1;
1603 err = view_fullscreen(view->child);
1605 err = view_splitscreen(view->child);
1607 err = view_resize_split(view, 0);
1611 err = view->child->input(new, view->child,
1614 if (view_is_splitscreen(view)) {
1615 view->parent->focussed = 0;
1617 err = view_fullscreen(view);
1619 err = view_splitscreen(view);
1620 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1621 err = view_resize(view->parent);
1623 err = view_resize_split(view, 0);
1627 err = view->input(new, view, KEY_RESIZE);
1632 err = view->resize(view, 0);
1637 err = offset_selection_down(view->parent);
1639 err = offset_selection_down(view);
1643 err = switch_split(view);
1646 err = view_resize_split(view, -1);
1649 err = view_resize_split(view, 1);
1655 if (view->search_start)
1656 view_search_start(view);
1658 err = view->input(new, view, ch);
1662 if (view->search_started && view->search_next) {
1663 view->searching = (ch == 'n' ?
1664 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1665 view->search_next_done = 0;
1666 view->search_next(view);
1668 err = view->input(new, view, ch);
1671 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1672 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1674 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1675 TAILQ_FOREACH(v, views, entry) {
1681 if (v->child && v->child->reset) {
1682 err = v->child->reset(v->child);
1689 err = view->input(new, view, ch);
1697 view_needs_focus_indication(struct tog_view *view)
1699 if (view_is_parent_view(view)) {
1700 if (view->child == NULL || view->child->focussed)
1702 if (!view_is_splitscreen(view->child))
1704 } else if (!view_is_splitscreen(view))
1707 return view->focussed;
1710 static const struct got_error *
1711 view_loop(struct tog_view *view)
1713 const struct got_error *err = NULL;
1714 struct tog_view_list_head views;
1715 struct tog_view *new_view;
1717 int fast_refresh = 10;
1718 int done = 0, errcode;
1720 mode = getenv("TOG_VIEW_SPLIT_MODE");
1721 if (!mode || !(*mode == 'h' || *mode == 'H'))
1722 view->mode = TOG_VIEW_SPLIT_VERT;
1724 view->mode = TOG_VIEW_SPLIT_HRZN;
1726 errcode = pthread_mutex_lock(&tog_mutex);
1728 return got_error_set_errno(errcode, "pthread_mutex_lock");
1731 TAILQ_INSERT_HEAD(&views, view, entry);
1734 err = view->show(view);
1739 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1740 !tog_fatal_signal_received()) {
1741 /* Refresh fast during initialization, then become slower. */
1742 if (fast_refresh && fast_refresh-- == 0)
1743 halfdelay(10); /* switch to once per second */
1745 err = view_input(&new_view, &done, view, &views);
1749 struct tog_view *v, *prev = NULL;
1751 if (view_is_parent_view(view))
1752 prev = TAILQ_PREV(view, tog_view_list_head,
1754 else if (view->parent)
1755 prev = view->parent;
1758 view->parent->child = NULL;
1759 view->parent->focus_child = 0;
1760 /* Restore fullscreen line height. */
1761 view->parent->nlines = view->parent->lines;
1762 err = view_resize(view->parent);
1765 /* Make resized splits persist. */
1766 view_transfer_size(view->parent, view);
1768 TAILQ_REMOVE(&views, view, entry);
1770 err = view_close(view);
1775 TAILQ_FOREACH(v, &views, entry) {
1779 if (view == NULL && new_view == NULL) {
1780 /* No view has focus. Try to pick one. */
1783 else if (!TAILQ_EMPTY(&views)) {
1784 view = TAILQ_LAST(&views,
1785 tog_view_list_head);
1788 if (view->focus_child) {
1789 view->child->focussed = 1;
1797 struct tog_view *v, *t;
1798 /* Only allow one parent view per type. */
1799 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1800 if (v->type != new_view->type)
1802 TAILQ_REMOVE(&views, v, entry);
1803 err = view_close(v);
1808 TAILQ_INSERT_TAIL(&views, new_view, entry);
1812 if (view_is_parent_view(view)) {
1813 if (view->child && view->child->focussed)
1816 if (view->parent && view->parent->focussed)
1817 view = view->parent;
1819 show_panel(view->panel);
1820 if (view->child && view_is_splitscreen(view->child))
1821 show_panel(view->child->panel);
1822 if (view->parent && view_is_splitscreen(view)) {
1823 err = view->parent->show(view->parent);
1827 err = view->show(view);
1831 err = view->child->show(view->child);
1840 while (!TAILQ_EMPTY(&views)) {
1841 const struct got_error *close_err;
1842 view = TAILQ_FIRST(&views);
1843 TAILQ_REMOVE(&views, view, entry);
1844 close_err = view_close(view);
1845 if (close_err && err == NULL)
1849 errcode = pthread_mutex_unlock(&tog_mutex);
1850 if (errcode && err == NULL)
1851 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1861 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1866 /* Create newly allocated wide-character string equivalent to a byte string. */
1867 static const struct got_error *
1868 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1871 const struct got_error *err = NULL;
1874 *wlen = mbstowcs(NULL, s, 0);
1875 if (*wlen == (size_t)-1) {
1877 if (errno != EILSEQ)
1878 return got_error_from_errno("mbstowcs");
1880 /* byte string invalid in current encoding; try to "fix" it */
1881 err = got_mbsavis(&vis, &vislen, s);
1884 *wlen = mbstowcs(NULL, vis, 0);
1885 if (*wlen == (size_t)-1) {
1886 err = got_error_from_errno("mbstowcs"); /* give up */
1891 *ws = calloc(*wlen + 1, sizeof(**ws));
1893 err = got_error_from_errno("calloc");
1897 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1898 err = got_error_from_errno("mbstowcs");
1909 static const struct got_error *
1910 expand_tab(char **ptr, const char *src)
1913 size_t len, n, idx = 0, sz = 0;
1916 n = len = strlen(src);
1917 dst = malloc(n + 1);
1919 return got_error_from_errno("malloc");
1921 while (idx < len && src[idx]) {
1922 const char c = src[idx];
1925 size_t nb = TABSIZE - sz % TABSIZE;
1928 p = realloc(dst, n + nb);
1931 return got_error_from_errno("realloc");
1936 memset(dst + sz, ' ', nb);
1939 dst[sz++] = src[idx];
1949 * Advance at most n columns from wline starting at offset off.
1950 * Return the index to the first character after the span operation.
1951 * Return the combined column width of all spanned wide character in
1955 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1957 int width, i, cols = 0;
1964 for (i = off; wline[i] != L'\0'; ++i) {
1965 if (wline[i] == L'\t')
1966 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1968 width = wcwidth(wline[i]);
1975 if (cols + width > n)
1985 * Format a line for display, ensuring that it won't overflow a width limit.
1986 * With scrolling, the width returned refers to the scrolled version of the
1987 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1989 static const struct got_error *
1990 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1991 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1993 const struct got_error *err = NULL;
1995 wchar_t *wline = NULL;
2004 err = expand_tab(&exstr, line);
2009 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2014 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2016 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2017 wline[wlen - 1] = L'\0';
2020 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2021 wline[wlen - 1] = L'\0';
2025 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2031 *scrollxp = scrollx;
2039 static const struct got_error*
2040 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2041 struct got_object_id *id, struct got_repository *repo)
2043 static const struct got_error *err = NULL;
2044 struct got_reflist_entry *re;
2050 TAILQ_FOREACH(re, refs, entry) {
2051 struct got_tag_object *tag = NULL;
2052 struct got_object_id *ref_id;
2055 name = got_ref_get_name(re->ref);
2056 if (strcmp(name, GOT_REF_HEAD) == 0)
2058 if (strncmp(name, "refs/", 5) == 0)
2060 if (strncmp(name, "got/", 4) == 0 &&
2061 strncmp(name, "got/backup/", 11) != 0)
2063 if (strncmp(name, "heads/", 6) == 0)
2065 if (strncmp(name, "remotes/", 8) == 0) {
2067 s = strstr(name, "/" GOT_REF_HEAD);
2068 if (s != NULL && s[strlen(s)] == '\0')
2071 err = got_ref_resolve(&ref_id, repo, re->ref);
2074 if (strncmp(name, "tags/", 5) == 0) {
2075 err = got_object_open_as_tag(&tag, repo, ref_id);
2077 if (err->code != GOT_ERR_OBJ_TYPE) {
2081 /* Ref points at something other than a tag. */
2086 cmp = got_object_id_cmp(tag ?
2087 got_object_tag_get_object_id(tag) : ref_id, id);
2090 got_object_tag_close(tag);
2094 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2095 s ? ", " : "", name) == -1) {
2096 err = got_error_from_errno("asprintf");
2107 static const struct got_error *
2108 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2113 smallerthan = strchr(author, '<');
2114 if (smallerthan && smallerthan[1] != '\0')
2115 author = smallerthan + 1;
2116 author[strcspn(author, "@>")] = '\0';
2117 return format_line(wauthor, author_width, NULL, author, 0, limit,
2121 static const struct got_error *
2122 draw_commit(struct tog_view *view, struct got_commit_object *commit,
2123 struct got_object_id *id, const size_t date_display_cols,
2124 int author_display_cols)
2126 struct tog_log_view_state *s = &view->state.log;
2127 const struct got_error *err = NULL;
2128 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2129 char *logmsg0 = NULL, *logmsg = NULL;
2130 char *author = NULL;
2131 wchar_t *wlogmsg = NULL, *wauthor = NULL;
2132 int author_width, logmsg_width;
2133 char *newline, *line = NULL;
2134 int col, limit, scrollx;
2135 const int avail = view->ncols;
2137 time_t committer_time;
2138 struct tog_color *tc;
2140 committer_time = got_object_commit_get_committer_time(commit);
2141 if (gmtime_r(&committer_time, &tm) == NULL)
2142 return got_error_from_errno("gmtime_r");
2143 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2144 return got_error(GOT_ERR_NO_SPACE);
2146 if (avail <= date_display_cols)
2147 limit = MIN(sizeof(datebuf) - 1, avail);
2149 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2150 tc = get_color(&s->colors, TOG_COLOR_DATE);
2152 wattr_on(view->window,
2153 COLOR_PAIR(tc->colorpair), NULL);
2154 waddnstr(view->window, datebuf, limit);
2156 wattr_off(view->window,
2157 COLOR_PAIR(tc->colorpair), NULL);
2164 err = got_object_id_str(&id_str, id);
2167 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2169 wattr_on(view->window,
2170 COLOR_PAIR(tc->colorpair), NULL);
2171 wprintw(view->window, "%.8s ", id_str);
2173 wattr_off(view->window,
2174 COLOR_PAIR(tc->colorpair), NULL);
2181 if (s->use_committer)
2182 author = strdup(got_object_commit_get_committer(commit));
2184 author = strdup(got_object_commit_get_author(commit));
2185 if (author == NULL) {
2186 err = got_error_from_errno("strdup");
2189 err = format_author(&wauthor, &author_width, author, avail - col, col);
2192 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2194 wattr_on(view->window,
2195 COLOR_PAIR(tc->colorpair), NULL);
2196 waddwstr(view->window, wauthor);
2197 col += author_width;
2198 while (col < avail && author_width < author_display_cols + 2) {
2199 waddch(view->window, ' ');
2204 wattr_off(view->window,
2205 COLOR_PAIR(tc->colorpair), NULL);
2209 err = got_object_commit_get_logmsg(&logmsg0, commit);
2213 while (*logmsg == '\n')
2215 newline = strchr(logmsg, '\n');
2218 limit = avail - col;
2219 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2220 limit--; /* for the border */
2221 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2225 waddwstr(view->window, &wlogmsg[scrollx]);
2226 col += MAX(logmsg_width, 0);
2227 while (col < avail) {
2228 waddch(view->window, ' ');
2240 static struct commit_queue_entry *
2241 alloc_commit_queue_entry(struct got_commit_object *commit,
2242 struct got_object_id *id)
2244 struct commit_queue_entry *entry;
2245 struct got_object_id *dup;
2247 entry = calloc(1, sizeof(*entry));
2251 dup = got_object_id_dup(id);
2258 entry->commit = commit;
2263 pop_commit(struct commit_queue *commits)
2265 struct commit_queue_entry *entry;
2267 entry = TAILQ_FIRST(&commits->head);
2268 TAILQ_REMOVE(&commits->head, entry, entry);
2269 got_object_commit_close(entry->commit);
2270 commits->ncommits--;
2276 free_commits(struct commit_queue *commits)
2278 while (!TAILQ_EMPTY(&commits->head))
2279 pop_commit(commits);
2282 static const struct got_error *
2283 match_commit(int *have_match, struct got_object_id *id,
2284 struct got_commit_object *commit, regex_t *regex)
2286 const struct got_error *err = NULL;
2287 regmatch_t regmatch;
2288 char *id_str = NULL, *logmsg = NULL;
2292 err = got_object_id_str(&id_str, id);
2296 err = got_object_commit_get_logmsg(&logmsg, commit);
2300 if (regexec(regex, got_object_commit_get_author(commit), 1,
2301 ®match, 0) == 0 ||
2302 regexec(regex, got_object_commit_get_committer(commit), 1,
2303 ®match, 0) == 0 ||
2304 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2305 regexec(regex, logmsg, 1, ®match, 0) == 0)
2313 static const struct got_error *
2314 queue_commits(struct tog_log_thread_args *a)
2316 const struct got_error *err = NULL;
2319 * We keep all commits open throughout the lifetime of the log
2320 * view in order to avoid having to re-fetch commits from disk
2321 * while updating the display.
2324 struct got_object_id id;
2325 struct got_commit_object *commit;
2326 struct commit_queue_entry *entry;
2327 int limit_match = 0;
2330 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2335 err = got_object_open_as_commit(&commit, a->repo, &id);
2338 entry = alloc_commit_queue_entry(commit, &id);
2339 if (entry == NULL) {
2340 err = got_error_from_errno("alloc_commit_queue_entry");
2344 errcode = pthread_mutex_lock(&tog_mutex);
2346 err = got_error_set_errno(errcode,
2347 "pthread_mutex_lock");
2351 entry->idx = a->real_commits->ncommits;
2352 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2353 a->real_commits->ncommits++;
2356 err = match_commit(&limit_match, &id, commit,
2362 struct commit_queue_entry *matched;
2364 matched = alloc_commit_queue_entry(
2365 entry->commit, entry->id);
2366 if (matched == NULL) {
2367 err = got_error_from_errno(
2368 "alloc_commit_queue_entry");
2371 matched->commit = entry->commit;
2372 got_object_commit_retain(entry->commit);
2374 matched->idx = a->limit_commits->ncommits;
2375 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2377 a->limit_commits->ncommits++;
2381 * This is how we signal log_thread() that we
2382 * have found a match, and that it should be
2383 * counted as a new entry for the view.
2385 a->limit_match = limit_match;
2388 if (*a->searching == TOG_SEARCH_FORWARD &&
2389 !*a->search_next_done) {
2391 err = match_commit(&have_match, &id, commit, a->regex);
2396 if (limit_match && have_match)
2397 *a->search_next_done =
2398 TOG_SEARCH_HAVE_MORE;
2399 } else if (have_match)
2400 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2403 errcode = pthread_mutex_unlock(&tog_mutex);
2404 if (errcode && err == NULL)
2405 err = got_error_set_errno(errcode,
2406 "pthread_mutex_unlock");
2409 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2415 select_commit(struct tog_log_view_state *s)
2417 struct commit_queue_entry *entry;
2420 entry = s->first_displayed_entry;
2422 if (ncommits == s->selected) {
2423 s->selected_entry = entry;
2426 entry = TAILQ_NEXT(entry, entry);
2431 static const struct got_error *
2432 draw_commits(struct tog_view *view)
2434 const struct got_error *err = NULL;
2435 struct tog_log_view_state *s = &view->state.log;
2436 struct commit_queue_entry *entry = s->selected_entry;
2437 int limit = view->nlines;
2439 int ncommits, author_cols = 4;
2440 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2441 char *refs_str = NULL;
2443 struct tog_color *tc;
2444 static const size_t date_display_cols = 12;
2446 if (view_is_hsplit_top(view))
2447 --limit; /* account for border */
2449 if (s->selected_entry &&
2450 !(view->searching && view->search_next_done == 0)) {
2451 struct got_reflist_head *refs;
2452 err = got_object_id_str(&id_str, s->selected_entry->id);
2455 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2456 s->selected_entry->id);
2458 err = build_refs_str(&refs_str, refs,
2459 s->selected_entry->id, s->repo);
2465 if (s->thread_args.commits_needed == 0)
2466 halfdelay(10); /* disable fast refresh */
2468 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2469 if (asprintf(&ncommits_str, " [%d/%d] %s",
2470 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2471 (view->searching && !view->search_next_done) ?
2472 "searching..." : "loading...") == -1) {
2473 err = got_error_from_errno("asprintf");
2477 const char *search_str = NULL;
2478 const char *limit_str = NULL;
2480 if (view->searching) {
2481 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2482 search_str = "no more matches";
2483 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2484 search_str = "no matches found";
2485 else if (!view->search_next_done)
2486 search_str = "searching...";
2489 if (s->limit_view && s->commits->ncommits == 0)
2490 limit_str = "no matches found";
2492 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2493 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2494 search_str ? search_str : (refs_str ? refs_str : ""),
2495 limit_str ? limit_str : "") == -1) {
2496 err = got_error_from_errno("asprintf");
2501 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2502 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2503 "........................................",
2504 s->in_repo_path, ncommits_str) == -1) {
2505 err = got_error_from_errno("asprintf");
2509 } else if (asprintf(&header, "commit %s%s",
2510 id_str ? id_str : "........................................",
2511 ncommits_str) == -1) {
2512 err = got_error_from_errno("asprintf");
2516 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2520 werase(view->window);
2522 if (view_needs_focus_indication(view))
2523 wstandout(view->window);
2524 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2526 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2527 waddwstr(view->window, wline);
2528 while (width < view->ncols) {
2529 waddch(view->window, ' ');
2533 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2534 if (view_needs_focus_indication(view))
2535 wstandend(view->window);
2540 /* Grow author column size if necessary, and set view->maxx. */
2541 entry = s->first_displayed_entry;
2545 struct got_commit_object *c = entry->commit;
2546 char *author, *eol, *msg, *msg0;
2547 wchar_t *wauthor, *wmsg;
2549 if (ncommits >= limit - 1)
2551 if (s->use_committer)
2552 author = strdup(got_object_commit_get_committer(c));
2554 author = strdup(got_object_commit_get_author(c));
2555 if (author == NULL) {
2556 err = got_error_from_errno("strdup");
2559 err = format_author(&wauthor, &width, author, COLS,
2561 if (author_cols < width)
2562 author_cols = width;
2567 err = got_object_commit_get_logmsg(&msg0, c);
2571 while (*msg == '\n')
2573 if ((eol = strchr(msg, '\n')))
2575 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2576 date_display_cols + author_cols, 0);
2579 view->maxx = MAX(view->maxx, width);
2583 entry = TAILQ_NEXT(entry, entry);
2586 entry = s->first_displayed_entry;
2587 s->last_displayed_entry = s->first_displayed_entry;
2590 if (ncommits >= limit - 1)
2592 if (ncommits == s->selected)
2593 wstandout(view->window);
2594 err = draw_commit(view, entry->commit, entry->id,
2595 date_display_cols, author_cols);
2596 if (ncommits == s->selected)
2597 wstandend(view->window);
2601 s->last_displayed_entry = entry;
2602 entry = TAILQ_NEXT(entry, entry);
2615 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2617 struct commit_queue_entry *entry;
2620 entry = TAILQ_FIRST(&s->commits->head);
2621 if (s->first_displayed_entry == entry)
2624 entry = s->first_displayed_entry;
2625 while (entry && nscrolled < maxscroll) {
2626 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2628 s->first_displayed_entry = entry;
2634 static const struct got_error *
2635 trigger_log_thread(struct tog_view *view, int wait)
2637 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2640 halfdelay(1); /* fast refresh while loading commits */
2642 while (!ta->log_complete && !tog_thread_error &&
2643 (ta->commits_needed > 0 || ta->load_all)) {
2644 /* Wake the log thread. */
2645 errcode = pthread_cond_signal(&ta->need_commits);
2647 return got_error_set_errno(errcode,
2648 "pthread_cond_signal");
2651 * The mutex will be released while the view loop waits
2652 * in wgetch(), at which time the log thread will run.
2657 /* Display progress update in log view. */
2658 show_log_view(view);
2662 /* Wait right here while next commit is being loaded. */
2663 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2665 return got_error_set_errno(errcode,
2666 "pthread_cond_wait");
2668 /* Display progress update in log view. */
2669 show_log_view(view);
2677 static const struct got_error *
2678 request_log_commits(struct tog_view *view)
2680 struct tog_log_view_state *state = &view->state.log;
2681 const struct got_error *err = NULL;
2683 if (state->thread_args.log_complete)
2686 state->thread_args.commits_needed += view->nscrolled;
2687 err = trigger_log_thread(view, 1);
2688 view->nscrolled = 0;
2693 static const struct got_error *
2694 log_scroll_down(struct tog_view *view, int maxscroll)
2696 struct tog_log_view_state *s = &view->state.log;
2697 const struct got_error *err = NULL;
2698 struct commit_queue_entry *pentry;
2699 int nscrolled = 0, ncommits_needed;
2701 if (s->last_displayed_entry == NULL)
2704 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2705 if (s->commits->ncommits < ncommits_needed &&
2706 !s->thread_args.log_complete) {
2708 * Ask the log thread for required amount of commits.
2710 s->thread_args.commits_needed +=
2711 ncommits_needed - s->commits->ncommits;
2712 err = trigger_log_thread(view, 1);
2718 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2719 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2722 s->last_displayed_entry = pentry ?
2723 pentry : s->last_displayed_entry;;
2725 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2728 s->first_displayed_entry = pentry;
2729 } while (++nscrolled < maxscroll);
2731 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2732 view->nscrolled += nscrolled;
2734 view->nscrolled = 0;
2739 static const struct got_error *
2740 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2741 struct got_commit_object *commit, struct got_object_id *commit_id,
2742 struct tog_view *log_view, struct got_repository *repo)
2744 const struct got_error *err;
2745 struct got_object_qid *parent_id;
2746 struct tog_view *diff_view;
2748 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2749 if (diff_view == NULL)
2750 return got_error_from_errno("view_open");
2752 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2753 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2754 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2756 *new_view = diff_view;
2760 static const struct got_error *
2761 tree_view_visit_subtree(struct tog_tree_view_state *s,
2762 struct got_tree_object *subtree)
2764 struct tog_parent_tree *parent;
2766 parent = calloc(1, sizeof(*parent));
2768 return got_error_from_errno("calloc");
2770 parent->tree = s->tree;
2771 parent->first_displayed_entry = s->first_displayed_entry;
2772 parent->selected_entry = s->selected_entry;
2773 parent->selected = s->selected;
2774 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2777 s->first_displayed_entry = NULL;
2781 static const struct got_error *
2782 tree_view_walk_path(struct tog_tree_view_state *s,
2783 struct got_commit_object *commit, const char *path)
2785 const struct got_error *err = NULL;
2786 struct got_tree_object *tree = NULL;
2788 char *slash, *subpath = NULL;
2790 /* Walk the path and open corresponding tree objects. */
2793 struct got_tree_entry *te;
2794 struct got_object_id *tree_id;
2800 /* Ensure the correct subtree entry is selected. */
2801 slash = strchr(p, '/');
2803 te_name = strdup(p);
2805 te_name = strndup(p, slash - p);
2806 if (te_name == NULL) {
2807 err = got_error_from_errno("strndup");
2810 te = got_object_tree_find_entry(s->tree, te_name);
2812 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2817 s->first_displayed_entry = s->selected_entry = te;
2819 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2820 break; /* jump to this file's entry */
2822 slash = strchr(p, '/');
2824 subpath = strndup(path, slash - path);
2826 subpath = strdup(path);
2827 if (subpath == NULL) {
2828 err = got_error_from_errno("strdup");
2832 err = got_object_id_by_path(&tree_id, s->repo, commit,
2837 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2842 err = tree_view_visit_subtree(s, tree);
2844 got_object_tree_close(tree);
2858 static const struct got_error *
2859 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2860 struct commit_queue_entry *entry, const char *path,
2861 const char *head_ref_name, struct got_repository *repo)
2863 const struct got_error *err = NULL;
2864 struct tog_tree_view_state *s;
2865 struct tog_view *tree_view;
2867 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2868 if (tree_view == NULL)
2869 return got_error_from_errno("view_open");
2871 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2874 s = &tree_view->state.tree;
2876 *new_view = tree_view;
2878 if (got_path_is_root_dir(path))
2881 return tree_view_walk_path(s, entry->commit, path);
2884 static const struct got_error *
2885 block_signals_used_by_main_thread(void)
2890 if (sigemptyset(&sigset) == -1)
2891 return got_error_from_errno("sigemptyset");
2893 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2894 if (sigaddset(&sigset, SIGWINCH) == -1)
2895 return got_error_from_errno("sigaddset");
2896 if (sigaddset(&sigset, SIGCONT) == -1)
2897 return got_error_from_errno("sigaddset");
2898 if (sigaddset(&sigset, SIGINT) == -1)
2899 return got_error_from_errno("sigaddset");
2900 if (sigaddset(&sigset, SIGTERM) == -1)
2901 return got_error_from_errno("sigaddset");
2903 /* ncurses handles SIGTSTP */
2904 if (sigaddset(&sigset, SIGTSTP) == -1)
2905 return got_error_from_errno("sigaddset");
2907 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2909 return got_error_set_errno(errcode, "pthread_sigmask");
2915 log_thread(void *arg)
2917 const struct got_error *err = NULL;
2919 struct tog_log_thread_args *a = arg;
2923 * Sync startup with main thread such that we begin our
2924 * work once view_input() has released the mutex.
2926 errcode = pthread_mutex_lock(&tog_mutex);
2928 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2932 err = block_signals_used_by_main_thread();
2934 pthread_mutex_unlock(&tog_mutex);
2938 while (!done && !err && !tog_fatal_signal_received()) {
2939 errcode = pthread_mutex_unlock(&tog_mutex);
2941 err = got_error_set_errno(errcode,
2942 "pthread_mutex_unlock");
2945 err = queue_commits(a);
2947 if (err->code != GOT_ERR_ITER_COMPLETED)
2951 } else if (a->commits_needed > 0 && !a->load_all) {
2954 a->commits_needed--;
2956 a->commits_needed--;
2959 errcode = pthread_mutex_lock(&tog_mutex);
2961 err = got_error_set_errno(errcode,
2962 "pthread_mutex_lock");
2964 } else if (*a->quit)
2966 else if (*a->limiting && *a->first_displayed_entry == NULL) {
2967 *a->first_displayed_entry =
2968 TAILQ_FIRST(&a->limit_commits->head);
2969 *a->selected_entry = *a->first_displayed_entry;
2970 } else if (*a->first_displayed_entry == NULL) {
2971 *a->first_displayed_entry =
2972 TAILQ_FIRST(&a->real_commits->head);
2973 *a->selected_entry = *a->first_displayed_entry;
2976 errcode = pthread_cond_signal(&a->commit_loaded);
2978 err = got_error_set_errno(errcode,
2979 "pthread_cond_signal");
2980 pthread_mutex_unlock(&tog_mutex);
2985 a->commits_needed = 0;
2987 if (a->commits_needed == 0 && !a->load_all) {
2988 errcode = pthread_cond_wait(&a->need_commits,
2991 err = got_error_set_errno(errcode,
2992 "pthread_cond_wait");
2993 pthread_mutex_unlock(&tog_mutex);
3001 a->log_complete = 1;
3002 errcode = pthread_mutex_unlock(&tog_mutex);
3004 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3007 tog_thread_error = 1;
3008 pthread_cond_signal(&a->commit_loaded);
3013 static const struct got_error *
3014 stop_log_thread(struct tog_log_view_state *s)
3016 const struct got_error *err = NULL, *thread_err = NULL;
3021 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3023 return got_error_set_errno(errcode,
3024 "pthread_cond_signal");
3025 errcode = pthread_mutex_unlock(&tog_mutex);
3027 return got_error_set_errno(errcode,
3028 "pthread_mutex_unlock");
3029 errcode = pthread_join(s->thread, (void **)&thread_err);
3031 return got_error_set_errno(errcode, "pthread_join");
3032 errcode = pthread_mutex_lock(&tog_mutex);
3034 return got_error_set_errno(errcode,
3035 "pthread_mutex_lock");
3039 if (s->thread_args.repo) {
3040 err = got_repo_close(s->thread_args.repo);
3041 s->thread_args.repo = NULL;
3044 if (s->thread_args.pack_fds) {
3045 const struct got_error *pack_err =
3046 got_repo_pack_fds_close(s->thread_args.pack_fds);
3049 s->thread_args.pack_fds = NULL;
3052 if (s->thread_args.graph) {
3053 got_commit_graph_close(s->thread_args.graph);
3054 s->thread_args.graph = NULL;
3057 return err ? err : thread_err;
3060 static const struct got_error *
3061 close_log_view(struct tog_view *view)
3063 const struct got_error *err = NULL;
3064 struct tog_log_view_state *s = &view->state.log;
3067 err = stop_log_thread(s);
3069 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3070 if (errcode && err == NULL)
3071 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3073 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3074 if (errcode && err == NULL)
3075 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3077 free_commits(&s->limit_commits);
3078 free_commits(&s->real_commits);
3079 free(s->in_repo_path);
3080 s->in_repo_path = NULL;
3083 free(s->head_ref_name);
3084 s->head_ref_name = NULL;
3089 * We use two queues to implement the limit feature: first consists of
3090 * commits matching the current limit_regex; second is the real queue
3091 * of all known commits (real_commits). When the user starts limiting,
3092 * we swap queues such that all movement and displaying functionality
3093 * works with very slight change.
3095 static const struct got_error *
3096 limit_log_view(struct tog_view *view)
3098 struct tog_log_view_state *s = &view->state.log;
3099 struct commit_queue_entry *entry;
3100 struct tog_view *v = view;
3101 const struct got_error *err = NULL;
3105 if (view_is_hsplit_top(view))
3107 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3110 /* Get the pattern */
3111 wmove(v->window, v->nlines - 1, 0);
3112 wclrtoeol(v->window);
3113 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3114 nodelay(v->window, FALSE);
3117 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3120 nodelay(v->window, TRUE);
3124 if (*pattern == '\0') {
3126 * Safety measure for the situation where the user
3127 * resets limit without previously limiting anything.
3133 * User could have pressed Ctrl+L, which refreshed the
3134 * commit queues, it means we can't save previously
3135 * (before limit took place) displayed entries,
3136 * because they would point to already free'ed memory,
3137 * so we are forced to always select first entry of
3140 s->commits = &s->real_commits;
3141 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3142 s->selected_entry = s->first_displayed_entry;
3149 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3154 /* Clear the screen while loading limit view */
3155 s->first_displayed_entry = NULL;
3156 s->last_displayed_entry = NULL;
3157 s->selected_entry = NULL;
3158 s->commits = &s->limit_commits;
3160 /* Prepare limit queue for new search */
3161 free_commits(&s->limit_commits);
3162 s->limit_commits.ncommits = 0;
3164 /* First process commits, which are in queue already */
3165 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3168 err = match_commit(&have_match, entry->id,
3169 entry->commit, &s->limit_regex);
3174 struct commit_queue_entry *matched;
3176 matched = alloc_commit_queue_entry(entry->commit,
3178 if (matched == NULL) {
3179 err = got_error_from_errno(
3180 "alloc_commit_queue_entry");
3183 matched->commit = entry->commit;
3184 got_object_commit_retain(entry->commit);
3186 matched->idx = s->limit_commits.ncommits;
3187 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3189 s->limit_commits.ncommits++;
3193 /* Second process all the commits, until we fill the screen */
3194 if (s->limit_commits.ncommits < view->nlines - 1 &&
3195 !s->thread_args.log_complete) {
3196 s->thread_args.commits_needed +=
3197 view->nlines - s->limit_commits.ncommits - 1;
3198 err = trigger_log_thread(view, 1);
3203 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3204 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3210 static const struct got_error *
3211 search_start_log_view(struct tog_view *view)
3213 struct tog_log_view_state *s = &view->state.log;
3215 s->matched_entry = NULL;
3216 s->search_entry = NULL;
3220 static const struct got_error *
3221 search_next_log_view(struct tog_view *view)
3223 const struct got_error *err = NULL;
3224 struct tog_log_view_state *s = &view->state.log;
3225 struct commit_queue_entry *entry;
3227 /* Display progress update in log view. */
3228 show_log_view(view);
3232 if (s->search_entry) {
3234 errcode = pthread_mutex_unlock(&tog_mutex);
3236 return got_error_set_errno(errcode,
3237 "pthread_mutex_unlock");
3238 ch = wgetch(view->window);
3239 errcode = pthread_mutex_lock(&tog_mutex);
3241 return got_error_set_errno(errcode,
3242 "pthread_mutex_lock");
3243 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3244 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3247 if (view->searching == TOG_SEARCH_FORWARD)
3248 entry = TAILQ_NEXT(s->search_entry, entry);
3250 entry = TAILQ_PREV(s->search_entry,
3251 commit_queue_head, entry);
3252 } else if (s->matched_entry) {
3254 * If the user has moved the cursor after we hit a match,
3255 * the position from where we should continue searching
3256 * might have changed.
3258 if (view->searching == TOG_SEARCH_FORWARD)
3259 entry = TAILQ_NEXT(s->selected_entry, entry);
3261 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3264 entry = s->selected_entry;
3270 if (entry == NULL) {
3271 if (s->thread_args.log_complete ||
3272 view->searching == TOG_SEARCH_BACKWARD) {
3273 view->search_next_done =
3274 (s->matched_entry == NULL ?
3275 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3276 s->search_entry = NULL;
3280 * Poke the log thread for more commits and return,
3281 * allowing the main loop to make progress. Search
3282 * will resume at s->search_entry once we come back.
3284 s->thread_args.commits_needed++;
3285 return trigger_log_thread(view, 0);
3288 err = match_commit(&have_match, entry->id, entry->commit,
3293 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3294 s->matched_entry = entry;
3298 s->search_entry = entry;
3299 if (view->searching == TOG_SEARCH_FORWARD)
3300 entry = TAILQ_NEXT(entry, entry);
3302 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3305 if (s->matched_entry) {
3306 int cur = s->selected_entry->idx;
3307 while (cur < s->matched_entry->idx) {
3308 err = input_log_view(NULL, view, KEY_DOWN);
3313 while (cur > s->matched_entry->idx) {
3314 err = input_log_view(NULL, view, KEY_UP);
3321 s->search_entry = NULL;
3326 static const struct got_error *
3327 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3328 struct got_repository *repo, const char *head_ref_name,
3329 const char *in_repo_path, int log_branches)
3331 const struct got_error *err = NULL;
3332 struct tog_log_view_state *s = &view->state.log;
3333 struct got_repository *thread_repo = NULL;
3334 struct got_commit_graph *thread_graph = NULL;
3337 if (in_repo_path != s->in_repo_path) {
3338 free(s->in_repo_path);
3339 s->in_repo_path = strdup(in_repo_path);
3340 if (s->in_repo_path == NULL)
3341 return got_error_from_errno("strdup");
3344 /* The commit queue only contains commits being displayed. */
3345 TAILQ_INIT(&s->real_commits.head);
3346 s->real_commits.ncommits = 0;
3347 s->commits = &s->real_commits;
3349 TAILQ_INIT(&s->limit_commits.head);
3351 s->limit_commits.ncommits = 0;
3354 if (head_ref_name) {
3355 s->head_ref_name = strdup(head_ref_name);
3356 if (s->head_ref_name == NULL) {
3357 err = got_error_from_errno("strdup");
3361 s->start_id = got_object_id_dup(start_id);
3362 if (s->start_id == NULL) {
3363 err = got_error_from_errno("got_object_id_dup");
3366 s->log_branches = log_branches;
3367 s->use_committer = 1;
3369 STAILQ_INIT(&s->colors);
3370 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3371 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3372 get_color_value("TOG_COLOR_COMMIT"));
3375 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3376 get_color_value("TOG_COLOR_AUTHOR"));
3378 free_colors(&s->colors);
3381 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3382 get_color_value("TOG_COLOR_DATE"));
3384 free_colors(&s->colors);
3389 view->show = show_log_view;
3390 view->input = input_log_view;
3391 view->resize = resize_log_view;
3392 view->close = close_log_view;
3393 view->search_start = search_start_log_view;
3394 view->search_next = search_next_log_view;
3396 if (s->thread_args.pack_fds == NULL) {
3397 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3401 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3402 s->thread_args.pack_fds);
3405 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3409 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3410 s->repo, NULL, NULL);
3414 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3416 err = got_error_set_errno(errcode, "pthread_cond_init");
3419 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3421 err = got_error_set_errno(errcode, "pthread_cond_init");
3425 s->thread_args.commits_needed = view->nlines;
3426 s->thread_args.graph = thread_graph;
3427 s->thread_args.real_commits = &s->real_commits;
3428 s->thread_args.limit_commits = &s->limit_commits;
3429 s->thread_args.in_repo_path = s->in_repo_path;
3430 s->thread_args.start_id = s->start_id;
3431 s->thread_args.repo = thread_repo;
3432 s->thread_args.log_complete = 0;
3433 s->thread_args.quit = &s->quit;
3434 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3435 s->thread_args.selected_entry = &s->selected_entry;
3436 s->thread_args.searching = &view->searching;
3437 s->thread_args.search_next_done = &view->search_next_done;
3438 s->thread_args.regex = &view->regex;
3439 s->thread_args.limiting = &s->limit_view;
3440 s->thread_args.limit_regex = &s->limit_regex;
3441 s->thread_args.limit_commits = &s->limit_commits;
3444 close_log_view(view);
3448 static const struct got_error *
3449 show_log_view(struct tog_view *view)
3451 const struct got_error *err;
3452 struct tog_log_view_state *s = &view->state.log;
3454 if (s->thread == NULL) {
3455 int errcode = pthread_create(&s->thread, NULL, log_thread,
3458 return got_error_set_errno(errcode, "pthread_create");
3459 if (s->thread_args.commits_needed > 0) {
3460 err = trigger_log_thread(view, 1);
3466 return draw_commits(view);
3470 log_move_cursor_up(struct tog_view *view, int page, int home)
3472 struct tog_log_view_state *s = &view->state.log;
3474 if (s->first_displayed_entry == NULL)
3476 if (s->selected_entry->idx == 0)
3479 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3481 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3483 if (!page && !home && s->selected > 0)
3486 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3492 static const struct got_error *
3493 log_move_cursor_down(struct tog_view *view, int page)
3495 struct tog_log_view_state *s = &view->state.log;
3496 const struct got_error *err = NULL;
3497 int eos = view->nlines - 2;
3499 if (s->first_displayed_entry == NULL)
3502 if (s->thread_args.log_complete &&
3503 s->selected_entry->idx >= s->commits->ncommits - 1)
3506 if (view_is_hsplit_top(view))
3507 --eos; /* border consumes the last line */
3510 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3513 err = log_scroll_down(view, 1);
3514 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3515 struct commit_queue_entry *entry;
3519 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3520 s->last_displayed_entry = entry;
3521 for (n = 0; n <= eos; n++) {
3524 s->first_displayed_entry = entry;
3525 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3528 s->selected = n - 1;
3530 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3531 s->thread_args.log_complete)
3532 s->selected += MIN(page,
3533 s->commits->ncommits - s->selected_entry->idx - 1);
3535 err = log_scroll_down(view, page);
3541 * We might necessarily overshoot in horizontal
3542 * splits; if so, select the last displayed commit.
3544 if (s->first_displayed_entry && s->last_displayed_entry) {
3545 s->selected = MIN(s->selected,
3546 s->last_displayed_entry->idx -
3547 s->first_displayed_entry->idx);
3552 if (s->thread_args.log_complete &&
3553 s->selected_entry->idx == s->commits->ncommits - 1)
3560 view_get_split(struct tog_view *view, int *y, int *x)
3565 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3566 if (view->child && view->child->resized_y)
3567 *y = view->child->resized_y;
3568 else if (view->resized_y)
3569 *y = view->resized_y;
3571 *y = view_split_begin_y(view->lines);
3572 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3573 if (view->child && view->child->resized_x)
3574 *x = view->child->resized_x;
3575 else if (view->resized_x)
3576 *x = view->resized_x;
3578 *x = view_split_begin_x(view->begin_x);
3582 /* Split view horizontally at y and offset view->state->selected line. */
3583 static const struct got_error *
3584 view_init_hsplit(struct tog_view *view, int y)
3586 const struct got_error *err = NULL;
3590 err = view_resize(view);
3594 err = offset_selection_down(view);
3599 static const struct got_error *
3600 log_goto_line(struct tog_view *view, int nlines)
3602 const struct got_error *err = NULL;
3603 struct tog_log_view_state *s = &view->state.log;
3604 int g, idx = s->selected_entry->idx;
3606 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3612 if (g >= s->first_displayed_entry->idx + 1 &&
3613 g <= s->last_displayed_entry->idx + 1 &&
3614 g - s->first_displayed_entry->idx - 1 < nlines) {
3615 s->selected = g - s->first_displayed_entry->idx - 1;
3621 err = log_move_cursor_down(view, g - idx - 1);
3622 if (!err && g > s->selected_entry->idx + 1)
3623 err = log_move_cursor_down(view,
3624 g - s->first_displayed_entry->idx - 1);
3627 } else if (idx + 1 > g)
3628 log_move_cursor_up(view, idx - g + 1, 0);
3630 if (g < nlines && s->first_displayed_entry->idx == 0)
3631 s->selected = g - 1;
3638 static const struct got_error *
3639 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3641 const struct got_error *err = NULL;
3642 struct tog_log_view_state *s = &view->state.log;
3645 if (s->thread_args.load_all) {
3646 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3647 s->thread_args.load_all = 0;
3648 else if (s->thread_args.log_complete) {
3649 err = log_move_cursor_down(view, s->commits->ncommits);
3650 s->thread_args.load_all = 0;
3656 eos = nscroll = view->nlines - 1;
3657 if (view_is_hsplit_top(view))
3661 return log_goto_line(view, eos);
3665 err = limit_log_view(view);
3674 view->x = MAX(view->maxx - view->ncols / 2, 0);
3679 if (view->x + view->ncols / 2 < view->maxx)
3680 view->x += 2; /* move two columns right */
3686 view->x -= MIN(view->x, 2); /* move two columns back */
3695 log_move_cursor_up(view, 0, 0);
3700 log_move_cursor_up(view, 0, 1);
3710 log_move_cursor_up(view, nscroll, 0);
3717 err = log_move_cursor_down(view, 0);
3720 s->use_committer = !s->use_committer;
3725 /* We don't know yet how many commits, so we're forced to
3726 * traverse them all. */
3728 s->thread_args.load_all = 1;
3729 if (!s->thread_args.log_complete)
3730 return trigger_log_thread(view, 0);
3731 err = log_move_cursor_down(view, s->commits->ncommits);
3732 s->thread_args.load_all = 0;
3743 err = log_move_cursor_down(view, nscroll);
3746 if (s->selected > view->nlines - 2)
3747 s->selected = view->nlines - 2;
3748 if (s->selected > s->commits->ncommits - 1)
3749 s->selected = s->commits->ncommits - 1;
3751 if (s->commits->ncommits < view->nlines - 1 &&
3752 !s->thread_args.log_complete) {
3753 s->thread_args.commits_needed += (view->nlines - 1) -
3754 s->commits->ncommits;
3755 err = trigger_log_thread(view, 1);
3761 if (s->selected_entry == NULL)
3763 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3767 if (s->selected_entry == NULL)
3769 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3775 if (ch == KEY_BACKSPACE &&
3776 got_path_is_root_dir(s->in_repo_path))
3778 err = stop_log_thread(s);
3781 if (ch == KEY_BACKSPACE) {
3783 err = got_path_dirname(&parent_path, s->in_repo_path);
3786 free(s->in_repo_path);
3787 s->in_repo_path = parent_path;
3788 s->thread_args.in_repo_path = s->in_repo_path;
3789 } else if (ch == CTRL('l')) {
3790 struct got_object_id *start_id;
3791 err = got_repo_match_object_id(&start_id, NULL,
3792 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3793 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3795 if (s->head_ref_name == NULL ||
3796 err->code != GOT_ERR_NOT_REF)
3798 /* Try to cope with deleted references. */
3799 free(s->head_ref_name);
3800 s->head_ref_name = NULL;
3801 err = got_repo_match_object_id(&start_id,
3802 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3803 &tog_refs, s->repo);
3808 s->start_id = start_id;
3809 s->thread_args.start_id = s->start_id;
3811 s->log_branches = !s->log_branches;
3813 if (s->thread_args.pack_fds == NULL) {
3814 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3818 err = got_repo_open(&s->thread_args.repo,
3819 got_repo_get_path(s->repo), NULL,
3820 s->thread_args.pack_fds);
3824 err = tog_load_refs(s->repo, 0);
3827 err = got_commit_graph_open(&s->thread_args.graph,
3828 s->in_repo_path, !s->log_branches);
3831 err = got_commit_graph_iter_start(s->thread_args.graph,
3832 s->start_id, s->repo, NULL, NULL);
3835 free_commits(&s->real_commits);
3836 free_commits(&s->limit_commits);
3837 s->first_displayed_entry = NULL;
3838 s->last_displayed_entry = NULL;
3839 s->selected_entry = NULL;
3841 s->thread_args.log_complete = 0;
3843 s->thread_args.commits_needed = view->lines;
3844 s->matched_entry = NULL;
3845 s->search_entry = NULL;
3850 err = view_request_new(new_view, view, TOG_VIEW_REF);
3860 static const struct got_error *
3861 apply_unveil(const char *repo_path, const char *worktree_path)
3863 const struct got_error *error;
3866 if (unveil("gmon.out", "rwc") != 0)
3867 return got_error_from_errno2("unveil", "gmon.out");
3869 if (repo_path && unveil(repo_path, "r") != 0)
3870 return got_error_from_errno2("unveil", repo_path);
3872 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3873 return got_error_from_errno2("unveil", worktree_path);
3875 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3876 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3878 error = got_privsep_unveil_exec_helpers();
3882 if (unveil(NULL, NULL) != 0)
3883 return got_error_from_errno("unveil");
3892 * Override default signal handlers before starting ncurses.
3893 * This should prevent ncurses from installing its own
3894 * broken cleanup() signal handler.
3896 signal(SIGWINCH, tog_sigwinch);
3897 signal(SIGPIPE, tog_sigpipe);
3898 signal(SIGCONT, tog_sigcont);
3899 signal(SIGINT, tog_sigint);
3900 signal(SIGTERM, tog_sigterm);
3904 halfdelay(1); /* Do fast refresh while initial view is loading. */
3907 intrflush(stdscr, FALSE);
3908 keypad(stdscr, TRUE);
3910 if (getenv("TOG_COLORS") != NULL) {
3912 use_default_colors();
3916 static const struct got_error *
3917 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3918 struct got_repository *repo, struct got_worktree *worktree)
3920 const struct got_error *err = NULL;
3923 *in_repo_path = strdup("/");
3924 if (*in_repo_path == NULL)
3925 return got_error_from_errno("strdup");
3930 const char *prefix = got_worktree_get_path_prefix(worktree);
3933 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3936 if (asprintf(in_repo_path, "%s%s%s", prefix,
3937 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3939 err = got_error_from_errno("asprintf");
3940 *in_repo_path = NULL;
3944 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3949 static const struct got_error *
3950 cmd_log(int argc, char *argv[])
3952 const struct got_error *error;
3953 struct got_repository *repo = NULL;
3954 struct got_worktree *worktree = NULL;
3955 struct got_object_id *start_id = NULL;
3956 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3957 char *start_commit = NULL, *label = NULL;
3958 struct got_reference *ref = NULL;
3959 const char *head_ref_name = NULL;
3960 int ch, log_branches = 0;
3961 struct tog_view *view;
3962 int *pack_fds = NULL;
3964 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3970 start_commit = optarg;
3973 repo_path = realpath(optarg, NULL);
3974 if (repo_path == NULL)
3975 return got_error_from_errno2("realpath",
3990 error = got_repo_pack_fds_open(&pack_fds);
3994 if (repo_path == NULL) {
3995 cwd = getcwd(NULL, 0);
3997 return got_error_from_errno("getcwd");
3998 error = got_worktree_open(&worktree, cwd);
3999 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4003 strdup(got_worktree_get_repo_path(worktree));
4005 repo_path = strdup(cwd);
4006 if (repo_path == NULL) {
4007 error = got_error_from_errno("strdup");
4012 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4016 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4023 error = apply_unveil(got_repo_get_path(repo),
4024 worktree ? got_worktree_get_root_path(worktree) : NULL);
4028 /* already loaded by tog_log_with_path()? */
4029 if (TAILQ_EMPTY(&tog_refs)) {
4030 error = tog_load_refs(repo, 0);
4035 if (start_commit == NULL) {
4036 error = got_repo_match_object_id(&start_id, &label,
4037 worktree ? got_worktree_get_head_ref_name(worktree) :
4038 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4041 head_ref_name = label;
4043 error = got_ref_open(&ref, repo, start_commit, 0);
4045 head_ref_name = got_ref_get_name(ref);
4046 else if (error->code != GOT_ERR_NOT_REF)
4048 error = got_repo_match_object_id(&start_id, NULL,
4049 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4054 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4056 error = got_error_from_errno("view_open");
4059 error = open_log_view(view, start_id, repo, head_ref_name,
4060 in_repo_path, log_branches);
4064 /* Release work tree lock. */
4065 got_worktree_close(worktree);
4068 error = view_loop(view);
4078 const struct got_error *close_err = got_repo_close(repo);
4083 got_worktree_close(worktree);
4085 const struct got_error *pack_err =
4086 got_repo_pack_fds_close(pack_fds);
4098 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4099 "object1 object2\n", getprogname());
4104 match_line(const char *line, regex_t *regex, size_t nmatch,
4105 regmatch_t *regmatch)
4107 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4110 static struct tog_color *
4111 match_color(struct tog_colors *colors, const char *line)
4113 struct tog_color *tc = NULL;
4115 STAILQ_FOREACH(tc, colors, entry) {
4116 if (match_line(line, &tc->regex, 0, NULL))
4123 static const struct got_error *
4124 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4125 WINDOW *window, int skipcol, regmatch_t *regmatch)
4127 const struct got_error *err = NULL;
4129 wchar_t *wline = NULL;
4130 int rme, rms, n, width, scrollx;
4131 int width0 = 0, width1 = 0, width2 = 0;
4132 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4136 rms = regmatch->rm_so;
4137 rme = regmatch->rm_eo;
4139 err = expand_tab(&exstr, line);
4143 /* Split the line into 3 segments, according to match offsets. */
4144 seg0 = strndup(exstr, rms);
4146 err = got_error_from_errno("strndup");
4149 seg1 = strndup(exstr + rms, rme - rms);
4151 err = got_error_from_errno("strndup");
4154 seg2 = strdup(exstr + rme);
4156 err = got_error_from_errno("strndup");
4160 /* draw up to matched token if we haven't scrolled past it */
4161 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4165 n = MAX(width0 - skipcol, 0);
4168 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4169 wlimit, col_tab_align, 1);
4172 waddwstr(window, &wline[scrollx]);
4182 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4186 wlen = wcslen(wline);
4188 width = wcwidth(wline[i]);
4190 /* should not happen, tabs are expanded */
4191 err = got_error(GOT_ERR_RANGE);
4194 if (width0 + w + width > skipcol)
4199 /* draw (visible part of) matched token (if scrolled into it) */
4200 if (width1 - w > 0) {
4201 wattron(window, A_STANDOUT);
4202 waddwstr(window, &wline[i]);
4203 wattroff(window, A_STANDOUT);
4204 wlimit -= (width1 - w);
4205 *wtotal += (width1 - w);
4209 if (wlimit > 0) { /* draw rest of line */
4211 if (skipcol > width0 + width1) {
4212 err = format_line(&wline, &width2, &scrollx, seg2,
4213 skipcol - (width0 + width1), wlimit,
4217 waddwstr(window, &wline[scrollx]);
4219 err = format_line(&wline, &width2, NULL, seg2, 0,
4220 wlimit, col_tab_align, 1);
4223 waddwstr(window, wline);
4237 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4240 int *eof, *first, *selected;
4242 if (view->type == TOG_VIEW_DIFF) {
4243 struct tog_diff_view_state *s = &view->state.diff;
4245 first = &s->first_displayed_line;
4249 } else if (view->type == TOG_VIEW_HELP) {
4250 struct tog_help_view_state *s = &view->state.help;
4252 first = &s->first_displayed_line;
4256 } else if (view->type == TOG_VIEW_BLAME) {
4257 struct tog_blame_view_state *s = &view->state.blame;
4259 first = &s->first_displayed_line;
4260 selected = &s->selected_line;
4266 /* Center gline in the middle of the page like vi(1). */
4267 if (*lineno < view->gline - (view->nlines - 3) / 2)
4269 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4278 *selected = view->gline <= (view->nlines - 3) / 2 ?
4279 view->gline : (view->nlines - 3) / 2 + 1;
4285 static const struct got_error *
4286 draw_file(struct tog_view *view, const char *header)
4288 struct tog_diff_view_state *s = &view->state.diff;
4289 regmatch_t *regmatch = &view->regmatch;
4290 const struct got_error *err;
4293 size_t linesize = 0;
4297 int max_lines = view->nlines;
4298 int nlines = s->nlines;
4301 s->lineno = s->first_displayed_line - 1;
4302 line_offset = s->lines[s->first_displayed_line - 1].offset;
4303 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4304 return got_error_from_errno("fseek");
4306 werase(view->window);
4308 if (view->gline > s->nlines - 1)
4309 view->gline = s->nlines - 1;
4312 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4313 1 : view->gline - (view->nlines - 3) / 2 :
4314 s->lineno + s->selected_line;
4316 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4317 return got_error_from_errno("asprintf");
4318 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4324 if (view_needs_focus_indication(view))
4325 wstandout(view->window);
4326 waddwstr(view->window, wline);
4329 while (width++ < view->ncols)
4330 waddch(view->window, ' ');
4331 if (view_needs_focus_indication(view))
4332 wstandend(view->window);
4342 while (max_lines > 0 && nprinted < max_lines) {
4343 enum got_diff_line_type linetype;
4346 linelen = getline(&line, &linesize, s->f);
4347 if (linelen == -1) {
4353 return got_ferror(s->f, GOT_ERR_IO);
4356 if (++s->lineno < s->first_displayed_line)
4358 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4360 if (s->lineno == view->hiline)
4363 /* Set view->maxx based on full line length. */
4364 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4370 view->maxx = MAX(view->maxx, width);
4374 linetype = s->lines[s->lineno].type;
4375 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4376 linetype < GOT_DIFF_LINE_CONTEXT)
4377 attr |= COLOR_PAIR(linetype);
4379 wattron(view->window, attr);
4380 if (s->first_displayed_line + nprinted == s->matched_line &&
4381 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4382 err = add_matched_line(&width, line, view->ncols, 0,
4383 view->window, view->x, regmatch);
4390 err = format_line(&wline, &width, &skip, line,
4391 view->x, view->ncols, 0, view->x ? 1 : 0);
4396 waddwstr(view->window, &wline[skip]);
4400 if (s->lineno == view->hiline) {
4401 /* highlight full gline length */
4402 while (width++ < view->ncols)
4403 waddch(view->window, ' ');
4405 if (width <= view->ncols - 1)
4406 waddch(view->window, '\n');
4409 wattroff(view->window, attr);
4410 if (++nprinted == 1)
4411 s->first_displayed_line = s->lineno;
4415 s->last_displayed_line = s->first_displayed_line +
4418 s->last_displayed_line = s->first_displayed_line;
4423 while (nprinted < view->nlines) {
4424 waddch(view->window, '\n');
4428 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4434 wstandout(view->window);
4435 waddwstr(view->window, wline);
4438 wstandend(view->window);
4445 get_datestr(time_t *time, char *datebuf)
4447 struct tm mytm, *tm;
4450 tm = gmtime_r(time, &mytm);
4453 s = asctime_r(tm, datebuf);
4456 p = strchr(s, '\n');
4462 static const struct got_error *
4463 get_changed_paths(struct got_pathlist_head *paths,
4464 struct got_commit_object *commit, struct got_repository *repo,
4465 struct got_diffstat_cb_arg *dsa)
4467 const struct got_error *err = NULL;
4468 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4469 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4470 struct got_object_qid *qid;
4471 FILE *f1 = NULL, *f2 = NULL;
4472 int fd1 = -1, fd2 = -1;
4474 f1 = got_opentemp();
4476 err = got_error_from_errno("got_opentemp");
4479 f2 = got_opentemp();
4481 err = got_error_from_errno("got_opentemp");
4485 fd1 = got_opentempfd();
4487 err = got_error_from_errno("got_opentempfd");
4490 fd2 = got_opentempfd();
4492 err = got_error_from_errno("got_opentempfd");
4496 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4498 struct got_commit_object *pcommit;
4499 err = got_object_open_as_commit(&pcommit, repo,
4504 tree_id1 = got_object_id_dup(
4505 got_object_commit_get_tree_id(pcommit));
4506 if (tree_id1 == NULL) {
4507 got_object_commit_close(pcommit);
4508 return got_error_from_errno("got_object_id_dup");
4510 got_object_commit_close(pcommit);
4515 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4520 tree_id2 = got_object_commit_get_tree_id(commit);
4521 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4525 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
4526 got_diff_tree_compute_diffstat, dsa, 1);
4529 got_object_tree_close(tree1);
4531 got_object_tree_close(tree2);
4532 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4533 err = got_error_from_errno("close");
4534 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4535 err = got_error_from_errno("close");
4536 if (f1 && fclose(f1) == EOF && err == NULL)
4537 err = got_error_from_errno("fclose");
4538 if (f2 && fclose(f2) == EOF && err == NULL)
4539 err = got_error_from_errno("fclose");
4544 static const struct got_error *
4545 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4546 off_t off, uint8_t type)
4548 struct got_diff_line *p;
4550 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4552 return got_error_from_errno("reallocarray");
4554 (*lines)[*nlines].offset = off;
4555 (*lines)[*nlines].type = type;
4561 static const struct got_error *
4562 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4563 struct got_object_id *commit_id, struct got_reflist_head *refs,
4564 struct got_repository *repo, int ignore_ws, int force_text_diff,
4567 const struct got_error *err = NULL;
4568 char datebuf[26], *datestr;
4569 struct got_commit_object *commit;
4570 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4571 time_t committer_time;
4572 const char *author, *committer;
4573 char *refs_str = NULL;
4574 struct got_pathlist_head changed_paths;
4575 struct got_pathlist_entry *pe;
4576 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0, &changed_paths,
4577 ignore_ws, force_text_diff, tog_diff_algo };
4581 TAILQ_INIT(&changed_paths);
4584 err = build_refs_str(&refs_str, refs, commit_id, repo);
4589 err = got_object_open_as_commit(&commit, repo, commit_id);
4593 err = got_object_id_str(&id_str, commit_id);
4595 err = got_error_from_errno("got_object_id_str");
4599 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4603 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4604 refs_str ? refs_str : "", refs_str ? ")" : "");
4606 err = got_error_from_errno("fprintf");
4610 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4614 n = fprintf(outfile, "from: %s\n",
4615 got_object_commit_get_author(commit));
4617 err = got_error_from_errno("fprintf");
4621 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4625 author = got_object_commit_get_author(commit);
4626 committer = got_object_commit_get_committer(commit);
4627 if (strcmp(author, committer) != 0) {
4628 n = fprintf(outfile, "via: %s\n", committer);
4630 err = got_error_from_errno("fprintf");
4634 err = add_line_metadata(lines, nlines, outoff,
4635 GOT_DIFF_LINE_AUTHOR);
4639 committer_time = got_object_commit_get_committer_time(commit);
4640 datestr = get_datestr(&committer_time, datebuf);
4642 n = fprintf(outfile, "date: %s UTC\n", datestr);
4644 err = got_error_from_errno("fprintf");
4648 err = add_line_metadata(lines, nlines, outoff,
4649 GOT_DIFF_LINE_DATE);
4653 if (got_object_commit_get_nparents(commit) > 1) {
4654 const struct got_object_id_queue *parent_ids;
4655 struct got_object_qid *qid;
4657 parent_ids = got_object_commit_get_parent_ids(commit);
4658 STAILQ_FOREACH(qid, parent_ids, entry) {
4659 err = got_object_id_str(&id_str, &qid->id);
4662 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4664 err = got_error_from_errno("fprintf");
4668 err = add_line_metadata(lines, nlines, outoff,
4669 GOT_DIFF_LINE_META);
4677 err = got_object_commit_get_logmsg(&logmsg, commit);
4681 while ((line = strsep(&s, "\n")) != NULL) {
4682 n = fprintf(outfile, "%s\n", line);
4684 err = got_error_from_errno("fprintf");
4688 err = add_line_metadata(lines, nlines, outoff,
4689 GOT_DIFF_LINE_LOGMSG);
4694 err = get_changed_paths(&changed_paths, commit, repo, &dsa);
4698 TAILQ_FOREACH(pe, &changed_paths, entry) {
4699 struct got_diff_changed_path *cp = pe->data;
4700 int pad = dsa.max_path_len - pe->path_len + 1;
4702 n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
4703 pe->path, pad, ' ', dsa.add_cols + 1, cp->add,
4704 dsa.rm_cols + 1, cp->rm);
4706 err = got_error_from_errno("fprintf");
4710 err = add_line_metadata(lines, nlines, outoff,
4711 GOT_DIFF_LINE_CHANGES);
4714 free((char *)pe->path);
4718 fputc('\n', outfile);
4720 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4724 n = fprintf(outfile,
4725 "%d file%s changed, %d insertions(+), %d deletions(-)\n",
4726 dsa.nfiles, dsa.nfiles > 1 ? "s" : "", dsa.ins, dsa.del);
4728 err = got_error_from_errno("fprintf");
4732 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4736 fputc('\n', outfile);
4738 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4740 got_pathlist_free(&changed_paths);
4744 got_object_commit_close(commit);
4753 static const struct got_error *
4754 create_diff(struct tog_diff_view_state *s)
4756 const struct got_error *err = NULL;
4761 s->lines = malloc(sizeof(*s->lines));
4762 if (s->lines == NULL)
4763 return got_error_from_errno("malloc");
4768 err = got_error_from_errno("got_opentemp");
4771 if (s->f && fclose(s->f) == EOF) {
4772 err = got_error_from_errno("fclose");
4778 err = got_object_get_type(&obj_type, s->repo, s->id1);
4780 err = got_object_get_type(&obj_type, s->repo, s->id2);
4785 case GOT_OBJ_TYPE_BLOB:
4786 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4787 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4788 s->label1, s->label2, tog_diff_algo, s->diff_context,
4789 s->ignore_whitespace, s->force_text_diff, 0, NULL, s->repo,
4792 case GOT_OBJ_TYPE_TREE:
4793 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4794 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4795 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4796 s->force_text_diff, 0, NULL, s->repo, s->f);
4798 case GOT_OBJ_TYPE_COMMIT: {
4799 const struct got_object_id_queue *parent_ids;
4800 struct got_object_qid *pid;
4801 struct got_commit_object *commit2;
4802 struct got_reflist_head *refs;
4804 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4807 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4808 /* Show commit info if we're diffing to a parent/root commit. */
4809 if (s->id1 == NULL) {
4810 err = write_commit_info(&s->lines, &s->nlines, s->id2,
4811 refs, s->repo, s->ignore_whitespace,
4812 s->force_text_diff, s->f);
4816 parent_ids = got_object_commit_get_parent_ids(commit2);
4817 STAILQ_FOREACH(pid, parent_ids, entry) {
4818 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4819 err = write_commit_info(&s->lines,
4820 &s->nlines, s->id2, refs, s->repo,
4821 s->ignore_whitespace,
4822 s->force_text_diff, s->f);
4829 got_object_commit_close(commit2);
4831 err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4832 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4833 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4834 s->force_text_diff, 0, NULL, s->repo, s->f);
4838 err = got_error(GOT_ERR_OBJ_TYPE);
4842 if (s->f && fflush(s->f) != 0 && err == NULL)
4843 err = got_error_from_errno("fflush");
4848 diff_view_indicate_progress(struct tog_view *view)
4850 mvwaddstr(view->window, 0, 0, "diffing...");
4855 static const struct got_error *
4856 search_start_diff_view(struct tog_view *view)
4858 struct tog_diff_view_state *s = &view->state.diff;
4860 s->matched_line = 0;
4865 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4866 size_t *nlines, int **first, int **last, int **match, int **selected)
4868 struct tog_diff_view_state *s = &view->state.diff;
4871 *nlines = s->nlines;
4872 *line_offsets = NULL;
4873 *match = &s->matched_line;
4874 *first = &s->first_displayed_line;
4875 *last = &s->last_displayed_line;
4876 *selected = &s->selected_line;
4879 static const struct got_error *
4880 search_next_view_match(struct tog_view *view)
4882 const struct got_error *err = NULL;
4886 size_t linesize = 0;
4888 off_t *line_offsets;
4890 int *first, *last, *match, *selected;
4892 if (!view->search_setup)
4893 return got_error_msg(GOT_ERR_NOT_IMPL,
4894 "view search not supported");
4895 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4898 if (!view->searching) {
4899 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4904 if (view->searching == TOG_SEARCH_FORWARD)
4905 lineno = *match + 1;
4907 lineno = *match - 1;
4909 lineno = *first - 1 + *selected;
4914 if (lineno <= 0 || lineno > nlines) {
4916 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4920 if (view->searching == TOG_SEARCH_FORWARD)
4926 offset = view->type == TOG_VIEW_DIFF ?
4927 view->state.diff.lines[lineno - 1].offset :
4928 line_offsets[lineno - 1];
4929 if (fseeko(f, offset, SEEK_SET) != 0) {
4931 return got_error_from_errno("fseeko");
4933 linelen = getline(&line, &linesize, f);
4934 if (linelen != -1) {
4936 err = expand_tab(&exstr, line);
4939 if (match_line(exstr, &view->regex, 1,
4941 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4948 if (view->searching == TOG_SEARCH_FORWARD)
4963 static const struct got_error *
4964 close_diff_view(struct tog_view *view)
4966 const struct got_error *err = NULL;
4967 struct tog_diff_view_state *s = &view->state.diff;
4973 if (s->f && fclose(s->f) == EOF)
4974 err = got_error_from_errno("fclose");
4976 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4977 err = got_error_from_errno("fclose");
4979 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4980 err = got_error_from_errno("fclose");
4982 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4983 err = got_error_from_errno("close");
4985 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4986 err = got_error_from_errno("close");
4994 static const struct got_error *
4995 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4996 struct got_object_id *id2, const char *label1, const char *label2,
4997 int diff_context, int ignore_whitespace, int force_text_diff,
4998 struct tog_view *parent_view, struct got_repository *repo)
5000 const struct got_error *err;
5001 struct tog_diff_view_state *s = &view->state.diff;
5003 memset(s, 0, sizeof(*s));
5007 if (id1 != NULL && id2 != NULL) {
5009 err = got_object_get_type(&type1, repo, id1);
5012 err = got_object_get_type(&type2, repo, id2);
5017 return got_error(GOT_ERR_OBJ_TYPE);
5019 s->first_displayed_line = 1;
5020 s->last_displayed_line = view->nlines;
5021 s->selected_line = 1;
5029 s->id1 = got_object_id_dup(id1);
5031 return got_error_from_errno("got_object_id_dup");
5035 s->id2 = got_object_id_dup(id2);
5036 if (s->id2 == NULL) {
5037 err = got_error_from_errno("got_object_id_dup");
5041 s->f1 = got_opentemp();
5042 if (s->f1 == NULL) {
5043 err = got_error_from_errno("got_opentemp");
5047 s->f2 = got_opentemp();
5048 if (s->f2 == NULL) {
5049 err = got_error_from_errno("got_opentemp");
5053 s->fd1 = got_opentempfd();
5055 err = got_error_from_errno("got_opentempfd");
5059 s->fd2 = got_opentempfd();
5061 err = got_error_from_errno("got_opentempfd");
5065 s->first_displayed_line = 1;
5066 s->last_displayed_line = view->nlines;
5067 s->diff_context = diff_context;
5068 s->ignore_whitespace = ignore_whitespace;
5069 s->force_text_diff = force_text_diff;
5070 s->parent_view = parent_view;
5073 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5076 rc = init_pair(GOT_DIFF_LINE_MINUS,
5077 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5079 rc = init_pair(GOT_DIFF_LINE_PLUS,
5080 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5082 rc = init_pair(GOT_DIFF_LINE_HUNK,
5083 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5085 rc = init_pair(GOT_DIFF_LINE_META,
5086 get_color_value("TOG_COLOR_DIFF_META"), -1);
5088 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5089 get_color_value("TOG_COLOR_DIFF_META"), -1);
5091 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5092 get_color_value("TOG_COLOR_DIFF_META"), -1);
5094 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5095 get_color_value("TOG_COLOR_DIFF_META"), -1);
5097 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5098 get_color_value("TOG_COLOR_AUTHOR"), -1);
5100 rc = init_pair(GOT_DIFF_LINE_DATE,
5101 get_color_value("TOG_COLOR_DATE"), -1);
5103 err = got_error(GOT_ERR_RANGE);
5108 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5109 view_is_splitscreen(view))
5110 show_log_view(parent_view); /* draw border */
5111 diff_view_indicate_progress(view);
5113 err = create_diff(s);
5115 view->show = show_diff_view;
5116 view->input = input_diff_view;
5117 view->reset = reset_diff_view;
5118 view->close = close_diff_view;
5119 view->search_start = search_start_diff_view;
5120 view->search_setup = search_setup_diff_view;
5121 view->search_next = search_next_view_match;
5124 close_diff_view(view);
5128 static const struct got_error *
5129 show_diff_view(struct tog_view *view)
5131 const struct got_error *err;
5132 struct tog_diff_view_state *s = &view->state.diff;
5133 char *id_str1 = NULL, *id_str2, *header;
5134 const char *label1, *label2;
5137 err = got_object_id_str(&id_str1, s->id1);
5140 label1 = s->label1 ? s->label1 : id_str1;
5142 label1 = "/dev/null";
5144 err = got_object_id_str(&id_str2, s->id2);
5147 label2 = s->label2 ? s->label2 : id_str2;
5149 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5150 err = got_error_from_errno("asprintf");
5158 err = draw_file(view, header);
5163 static const struct got_error *
5164 set_selected_commit(struct tog_diff_view_state *s,
5165 struct commit_queue_entry *entry)
5167 const struct got_error *err;
5168 const struct got_object_id_queue *parent_ids;
5169 struct got_commit_object *selected_commit;
5170 struct got_object_qid *pid;
5173 s->id2 = got_object_id_dup(entry->id);
5175 return got_error_from_errno("got_object_id_dup");
5177 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5180 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5182 pid = STAILQ_FIRST(parent_ids);
5183 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5184 got_object_commit_close(selected_commit);
5188 static const struct got_error *
5189 reset_diff_view(struct tog_view *view)
5191 struct tog_diff_view_state *s = &view->state.diff;
5194 wclear(view->window);
5195 s->first_displayed_line = 1;
5196 s->last_displayed_line = view->nlines;
5197 s->matched_line = 0;
5198 diff_view_indicate_progress(view);
5199 return create_diff(s);
5203 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5207 i = start = s->first_displayed_line - 1;
5209 while (s->lines[i].type != type) {
5213 return; /* do nothing, requested type not in file */
5216 s->selected_line = 1;
5217 s->first_displayed_line = i;
5221 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5225 i = start = s->first_displayed_line + 1;
5227 while (s->lines[i].type != type) {
5228 if (i == s->nlines - 1)
5231 return; /* do nothing, requested type not in file */
5234 s->selected_line = 1;
5235 s->first_displayed_line = i;
5238 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5240 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5243 static const struct got_error *
5244 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5246 const struct got_error *err = NULL;
5247 struct tog_diff_view_state *s = &view->state.diff;
5248 struct tog_log_view_state *ls;
5249 struct commit_queue_entry *old_selected_entry;
5251 size_t linesize = 0;
5253 int i, nscroll = view->nlines - 1, up = 0;
5255 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5262 view->x = MAX(view->maxx - view->ncols / 3, 0);
5267 if (view->x + view->ncols / 3 < view->maxx)
5268 view->x += 2; /* move two columns right */
5274 view->x -= MIN(view->x, 2); /* move two columns back */
5281 s->force_text_diff = !s->force_text_diff;
5283 s->ignore_whitespace = !s->ignore_whitespace;
5284 err = reset_diff_view(view);
5288 s->first_displayed_line = 1;
5297 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5303 if (s->first_displayed_line > 1)
5304 s->first_displayed_line--;
5315 if (s->first_displayed_line == 1) {
5320 while (i++ < nscroll && s->first_displayed_line > 1)
5321 s->first_displayed_line--;
5327 s->first_displayed_line++;
5344 while (!s->eof && i++ < nscroll) {
5345 linelen = getline(&line, &linesize, s->f);
5346 s->first_displayed_line++;
5347 if (linelen == -1) {
5351 err = got_ferror(s->f, GOT_ERR_IO);
5358 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5361 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5364 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5367 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5370 if (s->diff_context > 0) {
5372 s->matched_line = 0;
5373 diff_view_indicate_progress(view);
5374 err = create_diff(s);
5375 if (s->first_displayed_line + view->nlines - 1 >
5377 s->first_displayed_line = 1;
5378 s->last_displayed_line = view->nlines;
5384 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5386 s->matched_line = 0;
5387 diff_view_indicate_progress(view);
5388 err = create_diff(s);
5400 if (s->parent_view == NULL) {
5404 s->parent_view->count = view->count;
5406 if (s->parent_view->type == TOG_VIEW_LOG) {
5407 ls = &s->parent_view->state.log;
5408 old_selected_entry = ls->selected_entry;
5410 err = input_log_view(NULL, s->parent_view,
5411 up ? KEY_UP : KEY_DOWN);
5414 view->count = s->parent_view->count;
5416 if (old_selected_entry == ls->selected_entry)
5419 err = set_selected_commit(s, ls->selected_entry);
5422 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5423 struct tog_blame_view_state *bs;
5424 struct got_object_id *id, *prev_id;
5426 bs = &s->parent_view->state.blame;
5427 prev_id = get_annotation_for_line(bs->blame.lines,
5428 bs->blame.nlines, bs->last_diffed_line);
5430 err = input_blame_view(&view, s->parent_view,
5431 up ? KEY_UP : KEY_DOWN);
5434 view->count = s->parent_view->count;
5436 if (prev_id == NULL)
5438 id = get_selected_commit_id(bs->blame.lines,
5439 bs->blame.nlines, bs->first_displayed_line,
5444 if (!got_object_id_cmp(prev_id, id))
5447 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5451 s->first_displayed_line = 1;
5452 s->last_displayed_line = view->nlines;
5453 s->matched_line = 0;
5456 diff_view_indicate_progress(view);
5457 err = create_diff(s);
5467 static const struct got_error *
5468 cmd_diff(int argc, char *argv[])
5470 const struct got_error *error = NULL;
5471 struct got_repository *repo = NULL;
5472 struct got_worktree *worktree = NULL;
5473 struct got_object_id *id1 = NULL, *id2 = NULL;
5474 char *repo_path = NULL, *cwd = NULL;
5475 char *id_str1 = NULL, *id_str2 = NULL;
5476 char *label1 = NULL, *label2 = NULL;
5477 int diff_context = 3, ignore_whitespace = 0;
5478 int ch, force_text_diff = 0;
5480 struct tog_view *view;
5481 int *pack_fds = NULL;
5483 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5486 force_text_diff = 1;
5489 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5492 errx(1, "number of context lines is %s: %s",
5496 repo_path = realpath(optarg, NULL);
5497 if (repo_path == NULL)
5498 return got_error_from_errno2("realpath",
5500 got_path_strip_trailing_slashes(repo_path);
5503 ignore_whitespace = 1;
5515 usage_diff(); /* TODO show local worktree changes */
5516 } else if (argc == 2) {
5522 error = got_repo_pack_fds_open(&pack_fds);
5526 if (repo_path == NULL) {
5527 cwd = getcwd(NULL, 0);
5529 return got_error_from_errno("getcwd");
5530 error = got_worktree_open(&worktree, cwd);
5531 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5535 strdup(got_worktree_get_repo_path(worktree));
5537 repo_path = strdup(cwd);
5538 if (repo_path == NULL) {
5539 error = got_error_from_errno("strdup");
5544 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5550 error = apply_unveil(got_repo_get_path(repo), NULL);
5554 error = tog_load_refs(repo, 0);
5558 error = got_repo_match_object_id(&id1, &label1, id_str1,
5559 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5563 error = got_repo_match_object_id(&id2, &label2, id_str2,
5564 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5568 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5570 error = got_error_from_errno("view_open");
5573 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5574 ignore_whitespace, force_text_diff, NULL, repo);
5577 error = view_loop(view);
5584 const struct got_error *close_err = got_repo_close(repo);
5589 got_worktree_close(worktree);
5591 const struct got_error *pack_err =
5592 got_repo_pack_fds_close(pack_fds);
5605 "usage: %s blame [-c commit] [-r repository-path] path\n",
5610 struct tog_blame_line {
5612 struct got_object_id *id;
5615 static const struct got_error *
5616 draw_blame(struct tog_view *view)
5618 struct tog_blame_view_state *s = &view->state.blame;
5619 struct tog_blame *blame = &s->blame;
5620 regmatch_t *regmatch = &view->regmatch;
5621 const struct got_error *err;
5622 int lineno = 0, nprinted = 0;
5624 size_t linesize = 0;
5628 struct tog_blame_line *blame_line;
5629 struct got_object_id *prev_id = NULL;
5631 struct tog_color *tc;
5633 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5638 werase(view->window);
5640 if (asprintf(&line, "commit %s", id_str) == -1) {
5641 err = got_error_from_errno("asprintf");
5646 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5651 if (view_needs_focus_indication(view))
5652 wstandout(view->window);
5653 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5655 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5656 waddwstr(view->window, wline);
5657 while (width++ < view->ncols)
5658 waddch(view->window, ' ');
5660 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5661 if (view_needs_focus_indication(view))
5662 wstandend(view->window);
5666 if (view->gline > blame->nlines)
5667 view->gline = blame->nlines;
5669 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5670 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5671 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5673 return got_error_from_errno("asprintf");
5676 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5681 waddwstr(view->window, wline);
5684 if (width < view->ncols - 1)
5685 waddch(view->window, '\n');
5689 while (nprinted < view->nlines - 2) {
5690 linelen = getline(&line, &linesize, blame->f);
5691 if (linelen == -1) {
5692 if (feof(blame->f)) {
5697 return got_ferror(blame->f, GOT_ERR_IO);
5699 if (++lineno < s->first_displayed_line)
5701 if (view->gline && !gotoline(view, &lineno, &nprinted))
5704 /* Set view->maxx based on full line length. */
5705 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5712 view->maxx = MAX(view->maxx, width);
5714 if (nprinted == s->selected_line - 1)
5715 wstandout(view->window);
5717 if (blame->nlines > 0) {
5718 blame_line = &blame->lines[lineno - 1];
5719 if (blame_line->annotated && prev_id &&
5720 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5721 !(nprinted == s->selected_line - 1)) {
5722 waddstr(view->window, " ");
5723 } else if (blame_line->annotated) {
5725 err = got_object_id_str(&id_str,
5731 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5733 wattr_on(view->window,
5734 COLOR_PAIR(tc->colorpair), NULL);
5735 wprintw(view->window, "%.8s", id_str);
5737 wattr_off(view->window,
5738 COLOR_PAIR(tc->colorpair), NULL);
5740 prev_id = blame_line->id;
5742 waddstr(view->window, "........");
5746 waddstr(view->window, "........");
5750 if (nprinted == s->selected_line - 1)
5751 wstandend(view->window);
5752 waddstr(view->window, " ");
5754 if (view->ncols <= 9) {
5756 } else if (s->first_displayed_line + nprinted ==
5758 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5759 err = add_matched_line(&width, line, view->ncols - 9, 9,
5760 view->window, view->x, regmatch);
5768 err = format_line(&wline, &width, &skip, line,
5769 view->x, view->ncols - 9, 9, 1);
5774 waddwstr(view->window, &wline[skip]);
5780 if (width <= view->ncols - 1)
5781 waddch(view->window, '\n');
5782 if (++nprinted == 1)
5783 s->first_displayed_line = lineno;
5786 s->last_displayed_line = lineno;
5793 static const struct got_error *
5794 blame_cb(void *arg, int nlines, int lineno,
5795 struct got_commit_object *commit, struct got_object_id *id)
5797 const struct got_error *err = NULL;
5798 struct tog_blame_cb_args *a = arg;
5799 struct tog_blame_line *line;
5802 if (nlines != a->nlines ||
5803 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5804 return got_error(GOT_ERR_RANGE);
5806 errcode = pthread_mutex_lock(&tog_mutex);
5808 return got_error_set_errno(errcode, "pthread_mutex_lock");
5810 if (*a->quit) { /* user has quit the blame view */
5811 err = got_error(GOT_ERR_ITER_COMPLETED);
5816 goto done; /* no change in this commit */
5818 line = &a->lines[lineno - 1];
5819 if (line->annotated)
5822 line->id = got_object_id_dup(id);
5823 if (line->id == NULL) {
5824 err = got_error_from_errno("got_object_id_dup");
5827 line->annotated = 1;
5829 errcode = pthread_mutex_unlock(&tog_mutex);
5831 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5836 blame_thread(void *arg)
5838 const struct got_error *err, *close_err;
5839 struct tog_blame_thread_args *ta = arg;
5840 struct tog_blame_cb_args *a = ta->cb_args;
5841 int errcode, fd1 = -1, fd2 = -1;
5842 FILE *f1 = NULL, *f2 = NULL;
5844 fd1 = got_opentempfd();
5846 return (void *)got_error_from_errno("got_opentempfd");
5848 fd2 = got_opentempfd();
5850 err = got_error_from_errno("got_opentempfd");
5854 f1 = got_opentemp();
5856 err = (void *)got_error_from_errno("got_opentemp");
5859 f2 = got_opentemp();
5861 err = (void *)got_error_from_errno("got_opentemp");
5865 err = block_signals_used_by_main_thread();
5869 err = got_blame(ta->path, a->commit_id, ta->repo,
5870 tog_diff_algo, blame_cb, ta->cb_args,
5871 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5872 if (err && err->code == GOT_ERR_CANCELLED)
5875 errcode = pthread_mutex_lock(&tog_mutex);
5877 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5881 close_err = got_repo_close(ta->repo);
5887 errcode = pthread_mutex_unlock(&tog_mutex);
5888 if (errcode && err == NULL)
5889 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5892 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5893 err = got_error_from_errno("close");
5894 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5895 err = got_error_from_errno("close");
5896 if (f1 && fclose(f1) == EOF && err == NULL)
5897 err = got_error_from_errno("fclose");
5898 if (f2 && fclose(f2) == EOF && err == NULL)
5899 err = got_error_from_errno("fclose");
5904 static struct got_object_id *
5905 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5906 int first_displayed_line, int selected_line)
5908 struct tog_blame_line *line;
5913 line = &lines[first_displayed_line - 1 + selected_line - 1];
5914 if (!line->annotated)
5920 static struct got_object_id *
5921 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5924 struct tog_blame_line *line;
5926 if (nlines <= 0 || lineno >= nlines)
5929 line = &lines[lineno - 1];
5930 if (!line->annotated)
5936 static const struct got_error *
5937 stop_blame(struct tog_blame *blame)
5939 const struct got_error *err = NULL;
5942 if (blame->thread) {
5944 errcode = pthread_mutex_unlock(&tog_mutex);
5946 return got_error_set_errno(errcode,
5947 "pthread_mutex_unlock");
5948 errcode = pthread_join(blame->thread, (void **)&err);
5950 return got_error_set_errno(errcode, "pthread_join");
5951 errcode = pthread_mutex_lock(&tog_mutex);
5953 return got_error_set_errno(errcode,
5954 "pthread_mutex_lock");
5955 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5957 blame->thread = NULL;
5959 if (blame->thread_args.repo) {
5960 const struct got_error *close_err;
5961 close_err = got_repo_close(blame->thread_args.repo);
5964 blame->thread_args.repo = NULL;
5967 if (fclose(blame->f) == EOF && err == NULL)
5968 err = got_error_from_errno("fclose");
5972 for (i = 0; i < blame->nlines; i++)
5973 free(blame->lines[i].id);
5975 blame->lines = NULL;
5977 free(blame->cb_args.commit_id);
5978 blame->cb_args.commit_id = NULL;
5979 if (blame->pack_fds) {
5980 const struct got_error *pack_err =
5981 got_repo_pack_fds_close(blame->pack_fds);
5984 blame->pack_fds = NULL;
5989 static const struct got_error *
5990 cancel_blame_view(void *arg)
5992 const struct got_error *err = NULL;
5996 errcode = pthread_mutex_lock(&tog_mutex);
5998 return got_error_set_errno(errcode,
5999 "pthread_mutex_unlock");
6002 err = got_error(GOT_ERR_CANCELLED);
6004 errcode = pthread_mutex_unlock(&tog_mutex);
6006 return got_error_set_errno(errcode,
6007 "pthread_mutex_lock");
6012 static const struct got_error *
6013 run_blame(struct tog_view *view)
6015 struct tog_blame_view_state *s = &view->state.blame;
6016 struct tog_blame *blame = &s->blame;
6017 const struct got_error *err = NULL;
6018 struct got_commit_object *commit = NULL;
6019 struct got_blob_object *blob = NULL;
6020 struct got_repository *thread_repo = NULL;
6021 struct got_object_id *obj_id = NULL;
6022 int obj_type, fd = -1;
6023 int *pack_fds = NULL;
6025 err = got_object_open_as_commit(&commit, s->repo,
6026 &s->blamed_commit->id);
6030 fd = got_opentempfd();
6032 err = got_error_from_errno("got_opentempfd");
6036 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6040 err = got_object_get_type(&obj_type, s->repo, obj_id);
6044 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6045 err = got_error(GOT_ERR_OBJ_TYPE);
6049 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6052 blame->f = got_opentemp();
6053 if (blame->f == NULL) {
6054 err = got_error_from_errno("got_opentemp");
6057 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6058 &blame->line_offsets, blame->f, blob);
6061 if (blame->nlines == 0) {
6062 s->blame_complete = 1;
6066 /* Don't include \n at EOF in the blame line count. */
6067 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6070 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6071 if (blame->lines == NULL) {
6072 err = got_error_from_errno("calloc");
6076 err = got_repo_pack_fds_open(&pack_fds);
6079 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6084 blame->pack_fds = pack_fds;
6085 blame->cb_args.view = view;
6086 blame->cb_args.lines = blame->lines;
6087 blame->cb_args.nlines = blame->nlines;
6088 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6089 if (blame->cb_args.commit_id == NULL) {
6090 err = got_error_from_errno("got_object_id_dup");
6093 blame->cb_args.quit = &s->done;
6095 blame->thread_args.path = s->path;
6096 blame->thread_args.repo = thread_repo;
6097 blame->thread_args.cb_args = &blame->cb_args;
6098 blame->thread_args.complete = &s->blame_complete;
6099 blame->thread_args.cancel_cb = cancel_blame_view;
6100 blame->thread_args.cancel_arg = &s->done;
6101 s->blame_complete = 0;
6103 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6104 s->first_displayed_line = 1;
6105 s->last_displayed_line = view->nlines;
6106 s->selected_line = 1;
6108 s->matched_line = 0;
6112 got_object_commit_close(commit);
6113 if (fd != -1 && close(fd) == -1 && err == NULL)
6114 err = got_error_from_errno("close");
6116 got_object_blob_close(blob);
6123 static const struct got_error *
6124 open_blame_view(struct tog_view *view, char *path,
6125 struct got_object_id *commit_id, struct got_repository *repo)
6127 const struct got_error *err = NULL;
6128 struct tog_blame_view_state *s = &view->state.blame;
6130 STAILQ_INIT(&s->blamed_commits);
6132 s->path = strdup(path);
6133 if (s->path == NULL)
6134 return got_error_from_errno("strdup");
6136 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6142 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6143 s->first_displayed_line = 1;
6144 s->last_displayed_line = view->nlines;
6145 s->selected_line = 1;
6146 s->blame_complete = 0;
6148 s->commit_id = commit_id;
6149 memset(&s->blame, 0, sizeof(s->blame));
6151 STAILQ_INIT(&s->colors);
6152 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6153 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6154 get_color_value("TOG_COLOR_COMMIT"));
6159 view->show = show_blame_view;
6160 view->input = input_blame_view;
6161 view->reset = reset_blame_view;
6162 view->close = close_blame_view;
6163 view->search_start = search_start_blame_view;
6164 view->search_setup = search_setup_blame_view;
6165 view->search_next = search_next_view_match;
6167 return run_blame(view);
6170 static const struct got_error *
6171 close_blame_view(struct tog_view *view)
6173 const struct got_error *err = NULL;
6174 struct tog_blame_view_state *s = &view->state.blame;
6176 if (s->blame.thread)
6177 err = stop_blame(&s->blame);
6179 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6180 struct got_object_qid *blamed_commit;
6181 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6182 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6183 got_object_qid_free(blamed_commit);
6187 free_colors(&s->colors);
6191 static const struct got_error *
6192 search_start_blame_view(struct tog_view *view)
6194 struct tog_blame_view_state *s = &view->state.blame;
6196 s->matched_line = 0;
6201 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6202 size_t *nlines, int **first, int **last, int **match, int **selected)
6204 struct tog_blame_view_state *s = &view->state.blame;
6207 *nlines = s->blame.nlines;
6208 *line_offsets = s->blame.line_offsets;
6209 *match = &s->matched_line;
6210 *first = &s->first_displayed_line;
6211 *last = &s->last_displayed_line;
6212 *selected = &s->selected_line;
6215 static const struct got_error *
6216 show_blame_view(struct tog_view *view)
6218 const struct got_error *err = NULL;
6219 struct tog_blame_view_state *s = &view->state.blame;
6222 if (s->blame.thread == NULL && !s->blame_complete) {
6223 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6224 &s->blame.thread_args);
6226 return got_error_set_errno(errcode, "pthread_create");
6228 halfdelay(1); /* fast refresh while annotating */
6231 if (s->blame_complete)
6232 halfdelay(10); /* disable fast refresh */
6234 err = draw_blame(view);
6240 static const struct got_error *
6241 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6242 struct got_repository *repo, struct got_object_id *id)
6244 struct tog_view *log_view;
6245 const struct got_error *err = NULL;
6249 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6250 if (log_view == NULL)
6251 return got_error_from_errno("view_open");
6253 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6255 view_close(log_view);
6257 *new_view = log_view;
6262 static const struct got_error *
6263 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6265 const struct got_error *err = NULL, *thread_err = NULL;
6266 struct tog_view *diff_view;
6267 struct tog_blame_view_state *s = &view->state.blame;
6268 int eos, nscroll, begin_y = 0, begin_x = 0;
6270 eos = nscroll = view->nlines - 2;
6271 if (view_is_hsplit_top(view))
6279 view->x = MAX(view->maxx - view->ncols / 3, 0);
6284 if (view->x + view->ncols / 3 < view->maxx)
6285 view->x += 2; /* move two columns right */
6291 view->x -= MIN(view->x, 2); /* move two columns back */
6300 s->selected_line = 1;
6301 s->first_displayed_line = 1;
6306 if (s->blame.nlines < eos) {
6307 s->selected_line = s->blame.nlines;
6308 s->first_displayed_line = 1;
6310 s->selected_line = eos;
6311 s->first_displayed_line = s->blame.nlines - (eos - 1);
6318 if (s->selected_line > 1)
6320 else if (s->selected_line == 1 &&
6321 s->first_displayed_line > 1)
6322 s->first_displayed_line--;
6333 if (s->first_displayed_line == 1) {
6334 if (view->count > 1)
6336 s->selected_line = MAX(1, s->selected_line - nscroll);
6340 if (s->first_displayed_line > nscroll)
6341 s->first_displayed_line -= nscroll;
6343 s->first_displayed_line = 1;
6348 if (s->selected_line < eos && s->first_displayed_line +
6349 s->selected_line <= s->blame.nlines)
6351 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6352 s->first_displayed_line++;
6358 struct got_object_id *id = NULL;
6361 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6362 s->first_displayed_line, s->selected_line);
6366 struct got_commit_object *commit, *pcommit;
6367 struct got_object_qid *pid;
6368 struct got_object_id *blob_id = NULL;
6370 err = got_object_open_as_commit(&commit,
6375 got_object_commit_get_parent_ids(commit));
6377 got_object_commit_close(commit);
6380 /* Check if path history ends here. */
6381 err = got_object_open_as_commit(&pcommit,
6385 err = got_object_id_by_path(&blob_id, s->repo,
6387 got_object_commit_close(pcommit);
6389 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6391 got_object_commit_close(commit);
6394 err = got_object_get_type(&obj_type, s->repo,
6397 /* Can't blame non-blob type objects. */
6398 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6399 got_object_commit_close(commit);
6402 err = got_object_qid_alloc(&s->blamed_commit,
6404 got_object_commit_close(commit);
6406 if (got_object_id_cmp(id,
6407 &s->blamed_commit->id) == 0)
6409 err = got_object_qid_alloc(&s->blamed_commit,
6415 thread_err = stop_blame(&s->blame);
6419 STAILQ_INSERT_HEAD(&s->blamed_commits,
6420 s->blamed_commit, entry);
6421 err = run_blame(view);
6427 struct got_object_qid *first;
6430 first = STAILQ_FIRST(&s->blamed_commits);
6431 if (!got_object_id_cmp(&first->id, s->commit_id))
6434 thread_err = stop_blame(&s->blame);
6438 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6439 got_object_qid_free(s->blamed_commit);
6441 STAILQ_FIRST(&s->blamed_commits);
6442 err = run_blame(view);
6449 s->id_to_log = get_selected_commit_id(s->blame.lines,
6450 s->blame.nlines, s->first_displayed_line, s->selected_line);
6452 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6456 struct got_object_id *id = NULL;
6457 struct got_object_qid *pid;
6458 struct got_commit_object *commit = NULL;
6461 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6462 s->first_displayed_line, s->selected_line);
6465 err = got_object_open_as_commit(&commit, s->repo, id);
6468 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6470 /* traversed from diff view, release diff resources */
6471 err = close_diff_view(*new_view);
6474 diff_view = *new_view;
6476 if (view_is_parent_view(view))
6477 view_get_split(view, &begin_y, &begin_x);
6479 diff_view = view_open(0, 0, begin_y, begin_x,
6481 if (diff_view == NULL) {
6482 got_object_commit_close(commit);
6483 err = got_error_from_errno("view_open");
6487 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6488 id, NULL, NULL, 3, 0, 0, view, s->repo);
6489 got_object_commit_close(commit);
6491 view_close(diff_view);
6494 s->last_diffed_line = s->first_displayed_line - 1 +
6497 break; /* still open from active diff view */
6498 if (view_is_parent_view(view) &&
6499 view->mode == TOG_VIEW_SPLIT_HRZN) {
6500 err = view_init_hsplit(view, begin_y);
6506 diff_view->focussed = 1;
6507 diff_view->mode = view->mode;
6508 diff_view->nlines = view->lines - begin_y;
6509 if (view_is_parent_view(view)) {
6510 view_transfer_size(diff_view, view);
6511 err = view_close_child(view);
6514 err = view_set_child(view, diff_view);
6517 view->focus_child = 1;
6519 *new_view = diff_view;
6532 if (s->last_displayed_line >= s->blame.nlines &&
6533 s->selected_line >= MIN(s->blame.nlines,
6534 view->nlines - 2)) {
6538 if (s->last_displayed_line >= s->blame.nlines &&
6539 s->selected_line < view->nlines - 2) {
6541 MIN(nscroll, s->last_displayed_line -
6542 s->first_displayed_line - s->selected_line + 1);
6544 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6545 s->first_displayed_line += nscroll;
6547 s->first_displayed_line =
6548 s->blame.nlines - (view->nlines - 3);
6551 if (s->selected_line > view->nlines - 2) {
6552 s->selected_line = MIN(s->blame.nlines,
6560 return thread_err ? thread_err : err;
6563 static const struct got_error *
6564 reset_blame_view(struct tog_view *view)
6566 const struct got_error *err;
6567 struct tog_blame_view_state *s = &view->state.blame;
6571 err = stop_blame(&s->blame);
6575 return run_blame(view);
6578 static const struct got_error *
6579 cmd_blame(int argc, char *argv[])
6581 const struct got_error *error;
6582 struct got_repository *repo = NULL;
6583 struct got_worktree *worktree = NULL;
6584 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6585 char *link_target = NULL;
6586 struct got_object_id *commit_id = NULL;
6587 struct got_commit_object *commit = NULL;
6588 char *commit_id_str = NULL;
6590 struct tog_view *view;
6591 int *pack_fds = NULL;
6593 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6596 commit_id_str = optarg;
6599 repo_path = realpath(optarg, NULL);
6600 if (repo_path == NULL)
6601 return got_error_from_errno2("realpath",
6616 error = got_repo_pack_fds_open(&pack_fds);
6620 if (repo_path == NULL) {
6621 cwd = getcwd(NULL, 0);
6623 return got_error_from_errno("getcwd");
6624 error = got_worktree_open(&worktree, cwd);
6625 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6629 strdup(got_worktree_get_repo_path(worktree));
6631 repo_path = strdup(cwd);
6632 if (repo_path == NULL) {
6633 error = got_error_from_errno("strdup");
6638 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6642 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6649 error = apply_unveil(got_repo_get_path(repo), NULL);
6653 error = tog_load_refs(repo, 0);
6657 if (commit_id_str == NULL) {
6658 struct got_reference *head_ref;
6659 error = got_ref_open(&head_ref, repo, worktree ?
6660 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6663 error = got_ref_resolve(&commit_id, repo, head_ref);
6664 got_ref_close(head_ref);
6666 error = got_repo_match_object_id(&commit_id, NULL,
6667 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6672 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6674 error = got_error_from_errno("view_open");
6678 error = got_object_open_as_commit(&commit, repo, commit_id);
6682 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6687 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6692 /* Release work tree lock. */
6693 got_worktree_close(worktree);
6696 error = view_loop(view);
6704 got_object_commit_close(commit);
6706 got_worktree_close(worktree);
6708 const struct got_error *close_err = got_repo_close(repo);
6713 const struct got_error *pack_err =
6714 got_repo_pack_fds_close(pack_fds);
6722 static const struct got_error *
6723 draw_tree_entries(struct tog_view *view, const char *parent_path)
6725 struct tog_tree_view_state *s = &view->state.tree;
6726 const struct got_error *err = NULL;
6727 struct got_tree_entry *te;
6730 struct tog_color *tc;
6731 int width, n, nentries, i = 1;
6732 int limit = view->nlines;
6735 if (view_is_hsplit_top(view))
6736 --limit; /* border */
6738 werase(view->window);
6743 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6747 if (view_needs_focus_indication(view))
6748 wstandout(view->window);
6749 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6751 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6752 waddwstr(view->window, wline);
6755 while (width++ < view->ncols)
6756 waddch(view->window, ' ');
6758 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6759 if (view_needs_focus_indication(view))
6760 wstandend(view->window);
6765 if (s->first_displayed_entry) {
6766 i += got_tree_entry_get_index(s->first_displayed_entry);
6767 if (s->tree != s->root)
6768 ++i; /* account for ".." entry */
6770 nentries = got_object_tree_get_nentries(s->tree);
6771 if (asprintf(&index, "[%d/%d] %s",
6772 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6773 return got_error_from_errno("asprintf");
6774 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6778 waddwstr(view->window, wline);
6781 if (width < view->ncols - 1)
6782 waddch(view->window, '\n');
6785 waddch(view->window, '\n');
6789 if (s->first_displayed_entry == NULL) {
6790 te = got_object_tree_get_first_entry(s->tree);
6791 if (s->selected == 0) {
6793 wstandout(view->window);
6794 s->selected_entry = NULL;
6796 waddstr(view->window, " ..\n"); /* parent directory */
6797 if (s->selected == 0 && view->focussed)
6798 wstandend(view->window);
6805 te = s->first_displayed_entry;
6808 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6809 char *line = NULL, *id_str = NULL, *link_target = NULL;
6810 const char *modestr = "";
6813 te = got_object_tree_get_entry(s->tree, i);
6814 mode = got_tree_entry_get_mode(te);
6817 err = got_object_id_str(&id_str,
6818 got_tree_entry_get_id(te));
6820 return got_error_from_errno(
6821 "got_object_id_str");
6823 if (got_object_tree_entry_is_submodule(te))
6825 else if (S_ISLNK(mode)) {
6828 err = got_tree_entry_get_symlink_target(&link_target,
6834 for (i = 0; i < strlen(link_target); i++) {
6835 if (!isprint((unsigned char)link_target[i]))
6836 link_target[i] = '?';
6840 else if (S_ISDIR(mode))
6842 else if (mode & S_IXUSR)
6844 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6845 got_tree_entry_get_name(te), modestr,
6846 link_target ? " -> ": "",
6847 link_target ? link_target : "") == -1) {
6850 return got_error_from_errno("asprintf");
6854 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6860 if (n == s->selected) {
6862 wstandout(view->window);
6863 s->selected_entry = te;
6865 tc = match_color(&s->colors, line);
6867 wattr_on(view->window,
6868 COLOR_PAIR(tc->colorpair), NULL);
6869 waddwstr(view->window, wline);
6871 wattr_off(view->window,
6872 COLOR_PAIR(tc->colorpair), NULL);
6873 if (width < view->ncols - 1)
6874 waddch(view->window, '\n');
6875 if (n == s->selected && view->focussed)
6876 wstandend(view->window);
6882 s->last_displayed_entry = te;
6891 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6893 struct got_tree_entry *te;
6894 int isroot = s->tree == s->root;
6897 if (s->first_displayed_entry == NULL)
6900 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6901 while (i++ < maxscroll) {
6904 s->first_displayed_entry = NULL;
6907 s->first_displayed_entry = te;
6908 te = got_tree_entry_get_prev(s->tree, te);
6912 static const struct got_error *
6913 tree_scroll_down(struct tog_view *view, int maxscroll)
6915 struct tog_tree_view_state *s = &view->state.tree;
6916 struct got_tree_entry *next, *last;
6919 if (s->first_displayed_entry)
6920 next = got_tree_entry_get_next(s->tree,
6921 s->first_displayed_entry);
6923 next = got_object_tree_get_first_entry(s->tree);
6925 last = s->last_displayed_entry;
6926 while (next && n++ < maxscroll) {
6928 s->last_displayed_entry = last;
6929 last = got_tree_entry_get_next(s->tree, last);
6931 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6932 s->first_displayed_entry = next;
6933 next = got_tree_entry_get_next(s->tree, next);
6940 static const struct got_error *
6941 tree_entry_path(char **path, struct tog_parent_trees *parents,
6942 struct got_tree_entry *te)
6944 const struct got_error *err = NULL;
6945 struct tog_parent_tree *pt;
6946 size_t len = 2; /* for leading slash and NUL */
6948 TAILQ_FOREACH(pt, parents, entry)
6949 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6952 len += strlen(got_tree_entry_get_name(te));
6954 *path = calloc(1, len);
6956 return got_error_from_errno("calloc");
6959 pt = TAILQ_LAST(parents, tog_parent_trees);
6961 const char *name = got_tree_entry_get_name(pt->selected_entry);
6962 if (strlcat(*path, name, len) >= len) {
6963 err = got_error(GOT_ERR_NO_SPACE);
6966 if (strlcat(*path, "/", len) >= len) {
6967 err = got_error(GOT_ERR_NO_SPACE);
6970 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6973 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6974 err = got_error(GOT_ERR_NO_SPACE);
6986 static const struct got_error *
6987 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6988 struct got_tree_entry *te, struct tog_parent_trees *parents,
6989 struct got_object_id *commit_id, struct got_repository *repo)
6991 const struct got_error *err = NULL;
6993 struct tog_view *blame_view;
6997 err = tree_entry_path(&path, parents, te);
7001 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7002 if (blame_view == NULL) {
7003 err = got_error_from_errno("view_open");
7007 err = open_blame_view(blame_view, path, commit_id, repo);
7009 if (err->code == GOT_ERR_CANCELLED)
7011 view_close(blame_view);
7013 *new_view = blame_view;
7019 static const struct got_error *
7020 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7021 struct tog_tree_view_state *s)
7023 struct tog_view *log_view;
7024 const struct got_error *err = NULL;
7029 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7030 if (log_view == NULL)
7031 return got_error_from_errno("view_open");
7033 err = tree_entry_path(&path, &s->parents, s->selected_entry);
7037 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7040 view_close(log_view);
7042 *new_view = log_view;
7047 static const struct got_error *
7048 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7049 const char *head_ref_name, struct got_repository *repo)
7051 const struct got_error *err = NULL;
7052 char *commit_id_str = NULL;
7053 struct tog_tree_view_state *s = &view->state.tree;
7054 struct got_commit_object *commit = NULL;
7056 TAILQ_INIT(&s->parents);
7057 STAILQ_INIT(&s->colors);
7059 s->commit_id = got_object_id_dup(commit_id);
7060 if (s->commit_id == NULL)
7061 return got_error_from_errno("got_object_id_dup");
7063 err = got_object_open_as_commit(&commit, repo, commit_id);
7068 * The root is opened here and will be closed when the view is closed.
7069 * Any visited subtrees and their path-wise parents are opened and
7072 err = got_object_open_as_tree(&s->root, repo,
7073 got_object_commit_get_tree_id(commit));
7078 err = got_object_id_str(&commit_id_str, commit_id);
7082 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7083 err = got_error_from_errno("asprintf");
7087 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7088 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7089 if (head_ref_name) {
7090 s->head_ref_name = strdup(head_ref_name);
7091 if (s->head_ref_name == NULL) {
7092 err = got_error_from_errno("strdup");
7098 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7099 err = add_color(&s->colors, "\\$$",
7100 TOG_COLOR_TREE_SUBMODULE,
7101 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7104 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7105 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7108 err = add_color(&s->colors, "/$",
7109 TOG_COLOR_TREE_DIRECTORY,
7110 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7114 err = add_color(&s->colors, "\\*$",
7115 TOG_COLOR_TREE_EXECUTABLE,
7116 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7120 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7121 get_color_value("TOG_COLOR_COMMIT"));
7126 view->show = show_tree_view;
7127 view->input = input_tree_view;
7128 view->close = close_tree_view;
7129 view->search_start = search_start_tree_view;
7130 view->search_next = search_next_tree_view;
7132 free(commit_id_str);
7134 got_object_commit_close(commit);
7136 close_tree_view(view);
7140 static const struct got_error *
7141 close_tree_view(struct tog_view *view)
7143 struct tog_tree_view_state *s = &view->state.tree;
7145 free_colors(&s->colors);
7146 free(s->tree_label);
7147 s->tree_label = NULL;
7149 s->commit_id = NULL;
7150 free(s->head_ref_name);
7151 s->head_ref_name = NULL;
7152 while (!TAILQ_EMPTY(&s->parents)) {
7153 struct tog_parent_tree *parent;
7154 parent = TAILQ_FIRST(&s->parents);
7155 TAILQ_REMOVE(&s->parents, parent, entry);
7156 if (parent->tree != s->root)
7157 got_object_tree_close(parent->tree);
7161 if (s->tree != NULL && s->tree != s->root)
7162 got_object_tree_close(s->tree);
7164 got_object_tree_close(s->root);
7168 static const struct got_error *
7169 search_start_tree_view(struct tog_view *view)
7171 struct tog_tree_view_state *s = &view->state.tree;
7173 s->matched_entry = NULL;
7178 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7180 regmatch_t regmatch;
7182 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7186 static const struct got_error *
7187 search_next_tree_view(struct tog_view *view)
7189 struct tog_tree_view_state *s = &view->state.tree;
7190 struct got_tree_entry *te = NULL;
7192 if (!view->searching) {
7193 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7197 if (s->matched_entry) {
7198 if (view->searching == TOG_SEARCH_FORWARD) {
7199 if (s->selected_entry)
7200 te = got_tree_entry_get_next(s->tree,
7203 te = got_object_tree_get_first_entry(s->tree);
7205 if (s->selected_entry == NULL)
7206 te = got_object_tree_get_last_entry(s->tree);
7208 te = got_tree_entry_get_prev(s->tree,
7212 if (s->selected_entry)
7213 te = s->selected_entry;
7214 else if (view->searching == TOG_SEARCH_FORWARD)
7215 te = got_object_tree_get_first_entry(s->tree);
7217 te = got_object_tree_get_last_entry(s->tree);
7222 if (s->matched_entry == NULL) {
7223 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7226 if (view->searching == TOG_SEARCH_FORWARD)
7227 te = got_object_tree_get_first_entry(s->tree);
7229 te = got_object_tree_get_last_entry(s->tree);
7232 if (match_tree_entry(te, &view->regex)) {
7233 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7234 s->matched_entry = te;
7238 if (view->searching == TOG_SEARCH_FORWARD)
7239 te = got_tree_entry_get_next(s->tree, te);
7241 te = got_tree_entry_get_prev(s->tree, te);
7244 if (s->matched_entry) {
7245 s->first_displayed_entry = s->matched_entry;
7252 static const struct got_error *
7253 show_tree_view(struct tog_view *view)
7255 const struct got_error *err = NULL;
7256 struct tog_tree_view_state *s = &view->state.tree;
7259 err = tree_entry_path(&parent_path, &s->parents, NULL);
7263 err = draw_tree_entries(view, parent_path);
7270 static const struct got_error *
7271 tree_goto_line(struct tog_view *view, int nlines)
7273 const struct got_error *err = NULL;
7274 struct tog_tree_view_state *s = &view->state.tree;
7275 struct got_tree_entry **fte, **lte, **ste;
7276 int g, last, first = 1, i = 1;
7277 int root = s->tree == s->root;
7278 int off = root ? 1 : 2;
7285 else if (g > got_object_tree_get_nentries(s->tree))
7286 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7288 fte = &s->first_displayed_entry;
7289 lte = &s->last_displayed_entry;
7290 ste = &s->selected_entry;
7293 first = got_tree_entry_get_index(*fte);
7294 first += off; /* account for ".." */
7296 last = got_tree_entry_get_index(*lte);
7299 if (g >= first && g <= last && g - first < nlines) {
7300 s->selected = g - first;
7301 return NULL; /* gline is on the current page */
7305 i = got_tree_entry_get_index(*ste);
7310 err = tree_scroll_down(view, g - i);
7313 if (got_tree_entry_get_index(*lte) >=
7314 got_object_tree_get_nentries(s->tree) - 1 &&
7315 first + s->selected < g &&
7316 s->selected < s->ndisplayed - 1) {
7317 first = got_tree_entry_get_index(*fte);
7319 s->selected = g - first;
7322 tree_scroll_up(s, i - g);
7325 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7326 s->selected = g - 1;
7331 static const struct got_error *
7332 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7334 const struct got_error *err = NULL;
7335 struct tog_tree_view_state *s = &view->state.tree;
7336 struct got_tree_entry *te;
7337 int n, nscroll = view->nlines - 3;
7340 return tree_goto_line(view, nscroll);
7344 s->show_ids = !s->show_ids;
7349 if (!s->selected_entry)
7351 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7355 err = view_request_new(new_view, view, TOG_VIEW_REF);
7362 if (s->tree == s->root)
7363 s->first_displayed_entry =
7364 got_object_tree_get_first_entry(s->tree);
7366 s->first_displayed_entry = NULL;
7371 int eos = view->nlines - 3;
7373 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7377 te = got_object_tree_get_last_entry(s->tree);
7378 for (n = 0; n < eos; n++) {
7380 if (s->tree != s->root) {
7381 s->first_displayed_entry = NULL;
7386 s->first_displayed_entry = te;
7387 te = got_tree_entry_get_prev(s->tree, te);
7390 s->selected = n - 1;
7396 if (s->selected > 0) {
7400 tree_scroll_up(s, 1);
7401 if (s->selected_entry == NULL ||
7402 (s->tree == s->root && s->selected_entry ==
7403 got_object_tree_get_first_entry(s->tree)))
7413 if (s->tree == s->root) {
7414 if (got_object_tree_get_first_entry(s->tree) ==
7415 s->first_displayed_entry)
7416 s->selected -= MIN(s->selected, nscroll);
7418 if (s->first_displayed_entry == NULL)
7419 s->selected -= MIN(s->selected, nscroll);
7421 tree_scroll_up(s, MAX(0, nscroll));
7422 if (s->selected_entry == NULL ||
7423 (s->tree == s->root && s->selected_entry ==
7424 got_object_tree_get_first_entry(s->tree)))
7430 if (s->selected < s->ndisplayed - 1) {
7434 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7436 /* can't scroll any further */
7440 tree_scroll_down(view, 1);
7450 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7452 /* can't scroll any further; move cursor down */
7453 if (s->selected < s->ndisplayed - 1)
7454 s->selected += MIN(nscroll,
7455 s->ndisplayed - s->selected - 1);
7460 tree_scroll_down(view, nscroll);
7465 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7466 struct tog_parent_tree *parent;
7467 /* user selected '..' */
7468 if (s->tree == s->root) {
7472 parent = TAILQ_FIRST(&s->parents);
7473 TAILQ_REMOVE(&s->parents, parent,
7475 got_object_tree_close(s->tree);
7476 s->tree = parent->tree;
7477 s->first_displayed_entry =
7478 parent->first_displayed_entry;
7480 parent->selected_entry;
7481 s->selected = parent->selected;
7482 if (s->selected > view->nlines - 3) {
7483 err = offset_selection_down(view);
7488 } else if (S_ISDIR(got_tree_entry_get_mode(
7489 s->selected_entry))) {
7490 struct got_tree_object *subtree;
7492 err = got_object_open_as_tree(&subtree, s->repo,
7493 got_tree_entry_get_id(s->selected_entry));
7496 err = tree_view_visit_subtree(s, subtree);
7498 got_object_tree_close(subtree);
7501 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7502 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7505 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7506 s->selected = view->nlines - 4;
7522 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7527 static const struct got_error *
7528 cmd_tree(int argc, char *argv[])
7530 const struct got_error *error;
7531 struct got_repository *repo = NULL;
7532 struct got_worktree *worktree = NULL;
7533 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7534 struct got_object_id *commit_id = NULL;
7535 struct got_commit_object *commit = NULL;
7536 const char *commit_id_arg = NULL;
7538 struct got_reference *ref = NULL;
7539 const char *head_ref_name = NULL;
7541 struct tog_view *view;
7542 int *pack_fds = NULL;
7544 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7547 commit_id_arg = optarg;
7550 repo_path = realpath(optarg, NULL);
7551 if (repo_path == NULL)
7552 return got_error_from_errno2("realpath",
7567 error = got_repo_pack_fds_open(&pack_fds);
7571 if (repo_path == NULL) {
7572 cwd = getcwd(NULL, 0);
7574 return got_error_from_errno("getcwd");
7575 error = got_worktree_open(&worktree, cwd);
7576 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7580 strdup(got_worktree_get_repo_path(worktree));
7582 repo_path = strdup(cwd);
7583 if (repo_path == NULL) {
7584 error = got_error_from_errno("strdup");
7589 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7593 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7600 error = apply_unveil(got_repo_get_path(repo), NULL);
7604 error = tog_load_refs(repo, 0);
7608 if (commit_id_arg == NULL) {
7609 error = got_repo_match_object_id(&commit_id, &label,
7610 worktree ? got_worktree_get_head_ref_name(worktree) :
7611 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7614 head_ref_name = label;
7616 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7618 head_ref_name = got_ref_get_name(ref);
7619 else if (error->code != GOT_ERR_NOT_REF)
7621 error = got_repo_match_object_id(&commit_id, NULL,
7622 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7627 error = got_object_open_as_commit(&commit, repo, commit_id);
7631 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7633 error = got_error_from_errno("view_open");
7636 error = open_tree_view(view, commit_id, head_ref_name, repo);
7639 if (!got_path_is_root_dir(in_repo_path)) {
7640 error = tree_view_walk_path(&view->state.tree, commit,
7647 /* Release work tree lock. */
7648 got_worktree_close(worktree);
7651 error = view_loop(view);
7660 const struct got_error *close_err = got_repo_close(repo);
7665 const struct got_error *pack_err =
7666 got_repo_pack_fds_close(pack_fds);
7674 static const struct got_error *
7675 ref_view_load_refs(struct tog_ref_view_state *s)
7677 struct got_reflist_entry *sre;
7678 struct tog_reflist_entry *re;
7681 TAILQ_FOREACH(sre, &tog_refs, entry) {
7682 if (strncmp(got_ref_get_name(sre->ref),
7683 "refs/got/", 9) == 0 &&
7684 strncmp(got_ref_get_name(sre->ref),
7685 "refs/got/backup/", 16) != 0)
7688 re = malloc(sizeof(*re));
7690 return got_error_from_errno("malloc");
7692 re->ref = got_ref_dup(sre->ref);
7693 if (re->ref == NULL)
7694 return got_error_from_errno("got_ref_dup");
7695 re->idx = s->nrefs++;
7696 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7699 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7704 ref_view_free_refs(struct tog_ref_view_state *s)
7706 struct tog_reflist_entry *re;
7708 while (!TAILQ_EMPTY(&s->refs)) {
7709 re = TAILQ_FIRST(&s->refs);
7710 TAILQ_REMOVE(&s->refs, re, entry);
7711 got_ref_close(re->ref);
7716 static const struct got_error *
7717 open_ref_view(struct tog_view *view, struct got_repository *repo)
7719 const struct got_error *err = NULL;
7720 struct tog_ref_view_state *s = &view->state.ref;
7722 s->selected_entry = 0;
7725 TAILQ_INIT(&s->refs);
7726 STAILQ_INIT(&s->colors);
7728 err = ref_view_load_refs(s);
7732 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7733 err = add_color(&s->colors, "^refs/heads/",
7734 TOG_COLOR_REFS_HEADS,
7735 get_color_value("TOG_COLOR_REFS_HEADS"));
7739 err = add_color(&s->colors, "^refs/tags/",
7740 TOG_COLOR_REFS_TAGS,
7741 get_color_value("TOG_COLOR_REFS_TAGS"));
7745 err = add_color(&s->colors, "^refs/remotes/",
7746 TOG_COLOR_REFS_REMOTES,
7747 get_color_value("TOG_COLOR_REFS_REMOTES"));
7751 err = add_color(&s->colors, "^refs/got/backup/",
7752 TOG_COLOR_REFS_BACKUP,
7753 get_color_value("TOG_COLOR_REFS_BACKUP"));
7758 view->show = show_ref_view;
7759 view->input = input_ref_view;
7760 view->close = close_ref_view;
7761 view->search_start = search_start_ref_view;
7762 view->search_next = search_next_ref_view;
7765 free_colors(&s->colors);
7769 static const struct got_error *
7770 close_ref_view(struct tog_view *view)
7772 struct tog_ref_view_state *s = &view->state.ref;
7774 ref_view_free_refs(s);
7775 free_colors(&s->colors);
7780 static const struct got_error *
7781 resolve_reflist_entry(struct got_object_id **commit_id,
7782 struct tog_reflist_entry *re, struct got_repository *repo)
7784 const struct got_error *err = NULL;
7785 struct got_object_id *obj_id;
7786 struct got_tag_object *tag = NULL;
7791 err = got_ref_resolve(&obj_id, repo, re->ref);
7795 err = got_object_get_type(&obj_type, repo, obj_id);
7800 case GOT_OBJ_TYPE_COMMIT:
7801 *commit_id = obj_id;
7803 case GOT_OBJ_TYPE_TAG:
7804 err = got_object_open_as_tag(&tag, repo, obj_id);
7808 err = got_object_get_type(&obj_type, repo,
7809 got_object_tag_get_object_id(tag));
7812 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7813 err = got_error(GOT_ERR_OBJ_TYPE);
7816 *commit_id = got_object_id_dup(
7817 got_object_tag_get_object_id(tag));
7818 if (*commit_id == NULL) {
7819 err = got_error_from_errno("got_object_id_dup");
7824 err = got_error(GOT_ERR_OBJ_TYPE);
7830 got_object_tag_close(tag);
7838 static const struct got_error *
7839 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7840 struct tog_reflist_entry *re, struct got_repository *repo)
7842 struct tog_view *log_view;
7843 const struct got_error *err = NULL;
7844 struct got_object_id *commit_id = NULL;
7848 err = resolve_reflist_entry(&commit_id, re, repo);
7850 if (err->code != GOT_ERR_OBJ_TYPE)
7856 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7857 if (log_view == NULL) {
7858 err = got_error_from_errno("view_open");
7862 err = open_log_view(log_view, commit_id, repo,
7863 got_ref_get_name(re->ref), "", 0);
7866 view_close(log_view);
7868 *new_view = log_view;
7874 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7876 struct tog_reflist_entry *re;
7879 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7882 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7883 while (i++ < maxscroll) {
7886 s->first_displayed_entry = re;
7887 re = TAILQ_PREV(re, tog_reflist_head, entry);
7891 static const struct got_error *
7892 ref_scroll_down(struct tog_view *view, int maxscroll)
7894 struct tog_ref_view_state *s = &view->state.ref;
7895 struct tog_reflist_entry *next, *last;
7898 if (s->first_displayed_entry)
7899 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7901 next = TAILQ_FIRST(&s->refs);
7903 last = s->last_displayed_entry;
7904 while (next && n++ < maxscroll) {
7906 s->last_displayed_entry = last;
7907 last = TAILQ_NEXT(last, entry);
7909 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7910 s->first_displayed_entry = next;
7911 next = TAILQ_NEXT(next, entry);
7918 static const struct got_error *
7919 search_start_ref_view(struct tog_view *view)
7921 struct tog_ref_view_state *s = &view->state.ref;
7923 s->matched_entry = NULL;
7928 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7930 regmatch_t regmatch;
7932 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7936 static const struct got_error *
7937 search_next_ref_view(struct tog_view *view)
7939 struct tog_ref_view_state *s = &view->state.ref;
7940 struct tog_reflist_entry *re = NULL;
7942 if (!view->searching) {
7943 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7947 if (s->matched_entry) {
7948 if (view->searching == TOG_SEARCH_FORWARD) {
7949 if (s->selected_entry)
7950 re = TAILQ_NEXT(s->selected_entry, entry);
7952 re = TAILQ_PREV(s->selected_entry,
7953 tog_reflist_head, entry);
7955 if (s->selected_entry == NULL)
7956 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7958 re = TAILQ_PREV(s->selected_entry,
7959 tog_reflist_head, entry);
7962 if (s->selected_entry)
7963 re = s->selected_entry;
7964 else if (view->searching == TOG_SEARCH_FORWARD)
7965 re = TAILQ_FIRST(&s->refs);
7967 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7972 if (s->matched_entry == NULL) {
7973 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7976 if (view->searching == TOG_SEARCH_FORWARD)
7977 re = TAILQ_FIRST(&s->refs);
7979 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7982 if (match_reflist_entry(re, &view->regex)) {
7983 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7984 s->matched_entry = re;
7988 if (view->searching == TOG_SEARCH_FORWARD)
7989 re = TAILQ_NEXT(re, entry);
7991 re = TAILQ_PREV(re, tog_reflist_head, entry);
7994 if (s->matched_entry) {
7995 s->first_displayed_entry = s->matched_entry;
8002 static const struct got_error *
8003 show_ref_view(struct tog_view *view)
8005 const struct got_error *err = NULL;
8006 struct tog_ref_view_state *s = &view->state.ref;
8007 struct tog_reflist_entry *re;
8010 struct tog_color *tc;
8012 int limit = view->nlines;
8014 werase(view->window);
8017 if (view_is_hsplit_top(view))
8018 --limit; /* border */
8023 re = s->first_displayed_entry;
8025 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8027 return got_error_from_errno("asprintf");
8029 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8034 if (view_needs_focus_indication(view))
8035 wstandout(view->window);
8036 waddwstr(view->window, wline);
8037 while (width++ < view->ncols)
8038 waddch(view->window, ' ');
8039 if (view_needs_focus_indication(view))
8040 wstandend(view->window);
8049 while (re && limit > 0) {
8051 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8054 struct got_commit_object *ci;
8055 struct got_tag_object *tag;
8056 struct got_object_id *id;
8060 err = got_ref_resolve(&id, s->repo, re->ref);
8063 err = got_object_open_as_tag(&tag, s->repo, id);
8065 if (err->code != GOT_ERR_OBJ_TYPE) {
8069 err = got_object_open_as_commit(&ci, s->repo,
8075 t = got_object_commit_get_committer_time(ci);
8076 got_object_commit_close(ci);
8078 t = got_object_tag_get_tagger_time(tag);
8079 got_object_tag_close(tag);
8082 if (gmtime_r(&t, &tm) == NULL)
8083 return got_error_from_errno("gmtime_r");
8084 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8085 return got_error(GOT_ERR_NO_SPACE);
8087 if (got_ref_is_symbolic(re->ref)) {
8088 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8089 ymd : "", got_ref_get_name(re->ref),
8090 got_ref_get_symref_target(re->ref)) == -1)
8091 return got_error_from_errno("asprintf");
8092 } else if (s->show_ids) {
8093 struct got_object_id *id;
8095 err = got_ref_resolve(&id, s->repo, re->ref);
8098 err = got_object_id_str(&id_str, id);
8103 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8104 got_ref_get_name(re->ref), id_str) == -1) {
8105 err = got_error_from_errno("asprintf");
8112 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8113 got_ref_get_name(re->ref)) == -1)
8114 return got_error_from_errno("asprintf");
8116 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
8122 if (n == s->selected) {
8124 wstandout(view->window);
8125 s->selected_entry = re;
8127 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8129 wattr_on(view->window,
8130 COLOR_PAIR(tc->colorpair), NULL);
8131 waddwstr(view->window, wline);
8133 wattr_off(view->window,
8134 COLOR_PAIR(tc->colorpair), NULL);
8135 if (width < view->ncols - 1)
8136 waddch(view->window, '\n');
8137 if (n == s->selected && view->focussed)
8138 wstandend(view->window);
8144 s->last_displayed_entry = re;
8147 re = TAILQ_NEXT(re, entry);
8154 static const struct got_error *
8155 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8156 struct tog_reflist_entry *re, struct got_repository *repo)
8158 const struct got_error *err = NULL;
8159 struct got_object_id *commit_id = NULL;
8160 struct tog_view *tree_view;
8164 err = resolve_reflist_entry(&commit_id, re, repo);
8166 if (err->code != GOT_ERR_OBJ_TYPE)
8173 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8174 if (tree_view == NULL) {
8175 err = got_error_from_errno("view_open");
8179 err = open_tree_view(tree_view, commit_id,
8180 got_ref_get_name(re->ref), repo);
8184 *new_view = tree_view;
8190 static const struct got_error *
8191 ref_goto_line(struct tog_view *view, int nlines)
8193 const struct got_error *err = NULL;
8194 struct tog_ref_view_state *s = &view->state.ref;
8195 int g, idx = s->selected_entry->idx;
8202 else if (g > s->nrefs)
8205 if (g >= s->first_displayed_entry->idx + 1 &&
8206 g <= s->last_displayed_entry->idx + 1 &&
8207 g - s->first_displayed_entry->idx - 1 < nlines) {
8208 s->selected = g - s->first_displayed_entry->idx - 1;
8213 err = ref_scroll_down(view, g - idx - 1);
8216 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8217 s->first_displayed_entry->idx + s->selected < g &&
8218 s->selected < s->ndisplayed - 1)
8219 s->selected = g - s->first_displayed_entry->idx - 1;
8220 } else if (idx + 1 > g)
8221 ref_scroll_up(s, idx - g + 1);
8223 if (g < nlines && s->first_displayed_entry->idx == 0)
8224 s->selected = g - 1;
8230 static const struct got_error *
8231 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8233 const struct got_error *err = NULL;
8234 struct tog_ref_view_state *s = &view->state.ref;
8235 struct tog_reflist_entry *re;
8236 int n, nscroll = view->nlines - 1;
8239 return ref_goto_line(view, nscroll);
8243 s->show_ids = !s->show_ids;
8247 s->show_date = !s->show_date;
8251 s->sort_by_date = !s->sort_by_date;
8253 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8254 got_ref_cmp_by_commit_timestamp_descending :
8255 tog_ref_cmp_by_name, s->repo);
8258 got_reflist_object_id_map_free(tog_refs_idmap);
8259 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8260 &tog_refs, s->repo);
8263 ref_view_free_refs(s);
8264 err = ref_view_load_refs(s);
8269 if (!s->selected_entry)
8271 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8275 if (!s->selected_entry)
8277 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8284 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8289 int eos = view->nlines - 1;
8291 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8295 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8296 for (n = 0; n < eos; n++) {
8299 s->first_displayed_entry = re;
8300 re = TAILQ_PREV(re, tog_reflist_head, entry);
8303 s->selected = n - 1;
8309 if (s->selected > 0) {
8313 ref_scroll_up(s, 1);
8314 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8324 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8325 s->selected -= MIN(nscroll, s->selected);
8326 ref_scroll_up(s, MAX(0, nscroll));
8327 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8333 if (s->selected < s->ndisplayed - 1) {
8337 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8338 /* can't scroll any further */
8342 ref_scroll_down(view, 1);
8352 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8353 /* can't scroll any further; move cursor down */
8354 if (s->selected < s->ndisplayed - 1)
8355 s->selected += MIN(nscroll,
8356 s->ndisplayed - s->selected - 1);
8357 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8358 s->selected += s->ndisplayed - s->selected - 1;
8362 ref_scroll_down(view, nscroll);
8367 err = tog_load_refs(s->repo, s->sort_by_date);
8370 ref_view_free_refs(s);
8371 err = ref_view_load_refs(s);
8374 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8375 s->selected = view->nlines - 2;
8389 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8394 static const struct got_error *
8395 cmd_ref(int argc, char *argv[])
8397 const struct got_error *error;
8398 struct got_repository *repo = NULL;
8399 struct got_worktree *worktree = NULL;
8400 char *cwd = NULL, *repo_path = NULL;
8402 struct tog_view *view;
8403 int *pack_fds = NULL;
8405 while ((ch = getopt(argc, argv, "r:")) != -1) {
8408 repo_path = realpath(optarg, NULL);
8409 if (repo_path == NULL)
8410 return got_error_from_errno2("realpath",
8425 error = got_repo_pack_fds_open(&pack_fds);
8429 if (repo_path == NULL) {
8430 cwd = getcwd(NULL, 0);
8432 return got_error_from_errno("getcwd");
8433 error = got_worktree_open(&worktree, cwd);
8434 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8438 strdup(got_worktree_get_repo_path(worktree));
8440 repo_path = strdup(cwd);
8441 if (repo_path == NULL) {
8442 error = got_error_from_errno("strdup");
8447 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8453 error = apply_unveil(got_repo_get_path(repo), NULL);
8457 error = tog_load_refs(repo, 0);
8461 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8463 error = got_error_from_errno("view_open");
8467 error = open_ref_view(view, repo);
8472 /* Release work tree lock. */
8473 got_worktree_close(worktree);
8476 error = view_loop(view);
8481 const struct got_error *close_err = got_repo_close(repo);
8486 const struct got_error *pack_err =
8487 got_repo_pack_fds_close(pack_fds);
8495 static const struct got_error*
8496 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8505 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8509 if (mvwprintw(win, y, x, "%s", str) == ERR)
8510 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8517 static const struct got_error *
8518 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8522 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8524 free(*line_offsets);
8525 *line_offsets = NULL;
8526 return got_error_from_errno("reallocarray");
8530 (*line_offsets)[*nlines] = off;
8535 static const struct got_error *
8536 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8540 for (;n > 0; --n, ++km) {
8544 if (km->keys == NULL)
8547 t = t0 = strdup(km->keys);
8549 return got_error_from_errno("strdup");
8552 while ((k = strsep(&t, " ")) != NULL)
8553 len += strlen(k) > 1 ? 2 : 0;
8555 *ret = MAX(*ret, len);
8562 * Write keymap section headers, keys, and key info in km to f.
8563 * Save line offset to *off. If terminal has UTF8 encoding enabled,
8564 * wrap control and symbolic keys in guillemets, else use <>.
8566 static const struct got_error *
8567 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8572 static const char *u8_glyph[] = {
8573 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8574 "\xe2\x80\xba" /* U+203A (utf8 >) */
8577 int cs, s, first = 1;
8579 cs = got_locale_is_utf8();
8581 t = t0 = strdup(km->keys);
8583 return got_error_from_errno("strdup");
8585 len = strlen(km->keys);
8586 while ((k = strsep(&t, " ")) != NULL) {
8587 s = strlen(k) > 1; /* control or symbolic key */
8588 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8589 cs && s ? u8_glyph[0] : s ? "<" : "", k,
8590 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8593 return got_error_from_errno("fprintf");
8601 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8603 return got_error_from_errno("fprintf");
8609 static const struct got_error *
8610 format_help(struct tog_help_view_state *s)
8612 const struct got_error *err = NULL;
8614 int i, max, n, show = s->all;
8615 static const struct tog_key_map km[] = {
8616 #define KEYMAP_(info, type) { NULL, (info), type }
8617 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8623 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8628 err = max_key_str(&max, km, n);
8632 for (i = 0; i < n; ++i) {
8633 if (km[i].keys == NULL) {
8635 if (km[i].type == TOG_KEYMAP_GLOBAL ||
8636 km[i].type == s->type || s->all)
8640 err = format_help_line(&off, s->f, &km[i], max);
8643 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8650 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8654 static const struct got_error *
8655 create_help(struct tog_help_view_state *s)
8658 const struct got_error *err;
8660 free(s->line_offsets);
8661 s->line_offsets = NULL;
8666 return got_error_from_errno("got_opentemp");
8669 err = format_help(s);
8673 if (s->f && fflush(s->f) != 0)
8674 return got_error_from_errno("fflush");
8679 static const struct got_error *
8680 search_start_help_view(struct tog_view *view)
8682 view->state.help.matched_line = 0;
8687 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8688 size_t *nlines, int **first, int **last, int **match, int **selected)
8690 struct tog_help_view_state *s = &view->state.help;
8693 *nlines = s->nlines;
8694 *line_offsets = s->line_offsets;
8695 *match = &s->matched_line;
8696 *first = &s->first_displayed_line;
8697 *last = &s->last_displayed_line;
8698 *selected = &s->selected_line;
8701 static const struct got_error *
8702 show_help_view(struct tog_view *view)
8704 struct tog_help_view_state *s = &view->state.help;
8705 const struct got_error *err;
8706 regmatch_t *regmatch = &view->regmatch;
8711 int width, nprinted = 0, rc = 0;
8712 int eos = view->nlines;
8714 if (view_is_hsplit_top(view))
8715 --eos; /* account for border */
8719 werase(view->window);
8721 if (view->gline > s->nlines - 1)
8722 view->gline = s->nlines - 1;
8724 err = win_draw_center(view->window, 0, 0, view->ncols,
8725 view_needs_focus_indication(view),
8726 "tog help (press q to return to tog)");
8731 waddstr(view->window, "\n\n");
8737 while (eos > 0 && nprinted < eos) {
8740 linelen = getline(&line, &linesz, s->f);
8741 if (linelen == -1) {
8744 return got_ferror(s->f, GOT_ERR_IO);
8749 if (++s->lineno < s->first_displayed_line)
8751 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8753 if (s->lineno == view->hiline)
8756 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8762 view->maxx = MAX(view->maxx, width);
8767 wattron(view->window, attr);
8768 if (s->first_displayed_line + nprinted == s->matched_line &&
8769 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8770 err = add_matched_line(&width, line, view->ncols - 1, 0,
8771 view->window, view->x, regmatch);
8779 err = format_line(&wline, &width, &skip, line,
8780 view->x, view->ncols - 1, 0, view->x ? 1 : 0);
8785 rc = waddwstr(view->window, &wline[skip]);
8789 return got_error_msg(GOT_ERR_IO, "waddwstr");
8791 if (s->lineno == view->hiline) {
8792 while (width++ < view->ncols)
8793 waddch(view->window, ' ');
8795 if (width <= view->ncols)
8796 waddch(view->window, '\n');
8799 wattroff(view->window, attr);
8800 if (++nprinted == 1)
8801 s->first_displayed_line = s->lineno;
8805 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8807 s->last_displayed_line = s->first_displayed_line;
8812 rc = waddnstr(view->window,
8813 "See the tog(1) manual page for full documentation",
8816 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8818 wmove(view->window, view->nlines - 1, 0);
8819 wclrtoeol(view->window);
8820 wstandout(view->window);
8821 rc = waddnstr(view->window, "scroll down for more...",
8824 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8825 if (getcurx(view->window) < view->ncols - 6) {
8826 rc = wprintw(view->window, "[%.0f%%]",
8827 100.00 * s->last_displayed_line / s->nlines);
8829 return got_error_msg(GOT_ERR_IO, "wprintw");
8831 wstandend(view->window);
8837 static const struct got_error *
8838 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8840 struct tog_help_view_state *s = &view->state.help;
8841 const struct got_error *err = NULL;
8847 eos = nscroll = view->nlines;
8848 if (view_is_hsplit_top(view))
8851 s->lineno = s->first_displayed_line - 1 + s->selected_line;
8858 view->x = MAX(view->maxx - view->ncols / 3, 0);
8863 if (view->x + view->ncols / 3 < view->maxx)
8870 view->x -= MIN(view->x, 2);
8876 s->first_displayed_line = 1;
8884 s->first_displayed_line = (s->nlines - eos) + 3;
8889 if (s->first_displayed_line > 1)
8890 --s->first_displayed_line;
8901 if (s->first_displayed_line == 1) {
8905 while (--nscroll > 0 && s->first_displayed_line > 1)
8906 s->first_displayed_line--;
8912 ++s->first_displayed_line;
8928 while (!s->eof && --nscroll > 0) {
8929 linelen = getline(&line, &linesz, s->f);
8930 s->first_displayed_line++;
8931 if (linelen == -1) {
8935 err = got_ferror(s->f, GOT_ERR_IO);
8949 static const struct got_error *
8950 close_help_view(struct tog_view *view)
8952 struct tog_help_view_state *s = &view->state.help;
8954 free(s->line_offsets);
8955 s->line_offsets = NULL;
8956 if (fclose(s->f) == EOF)
8957 return got_error_from_errno("fclose");
8962 static const struct got_error *
8963 reset_help_view(struct tog_view *view)
8965 struct tog_help_view_state *s = &view->state.help;
8968 if (s->f && fclose(s->f) == EOF)
8969 return got_error_from_errno("fclose");
8971 wclear(view->window);
8975 s->first_displayed_line = 1;
8976 s->last_displayed_line = view->nlines;
8977 s->matched_line = 0;
8979 return create_help(s);
8982 static const struct got_error *
8983 open_help_view(struct tog_view *view, struct tog_view *parent)
8985 const struct got_error *err = NULL;
8986 struct tog_help_view_state *s = &view->state.help;
8988 s->type = (enum tog_keymap_type)parent->type;
8989 s->first_displayed_line = 1;
8990 s->last_displayed_line = view->nlines;
8991 s->selected_line = 1;
8993 view->show = show_help_view;
8994 view->input = input_help_view;
8995 view->reset = reset_help_view;
8996 view->close = close_help_view;
8997 view->search_start = search_start_help_view;
8998 view->search_setup = search_setup_help_view;
8999 view->search_next = search_next_view_match;
9001 err = create_help(s);
9005 static const struct got_error *
9006 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9007 enum tog_view_type request, int y, int x)
9009 const struct got_error *err = NULL;
9015 if (view->type == TOG_VIEW_LOG) {
9016 struct tog_log_view_state *s = &view->state.log;
9018 err = open_diff_view_for_commit(new_view, y, x,
9019 s->selected_entry->commit, s->selected_entry->id,
9022 return got_error_msg(GOT_ERR_NOT_IMPL,
9023 "parent/child view pair not supported");
9025 case TOG_VIEW_BLAME:
9026 if (view->type == TOG_VIEW_TREE) {
9027 struct tog_tree_view_state *s = &view->state.tree;
9029 err = blame_tree_entry(new_view, y, x,
9030 s->selected_entry, &s->parents, s->commit_id,
9033 return got_error_msg(GOT_ERR_NOT_IMPL,
9034 "parent/child view pair not supported");
9037 if (view->type == TOG_VIEW_BLAME)
9038 err = log_annotated_line(new_view, y, x,
9039 view->state.blame.repo, view->state.blame.id_to_log);
9040 else if (view->type == TOG_VIEW_TREE)
9041 err = log_selected_tree_entry(new_view, y, x,
9043 else if (view->type == TOG_VIEW_REF)
9044 err = log_ref_entry(new_view, y, x,
9045 view->state.ref.selected_entry,
9046 view->state.ref.repo);
9048 return got_error_msg(GOT_ERR_NOT_IMPL,
9049 "parent/child view pair not supported");
9052 if (view->type == TOG_VIEW_LOG)
9053 err = browse_commit_tree(new_view, y, x,
9054 view->state.log.selected_entry,
9055 view->state.log.in_repo_path,
9056 view->state.log.head_ref_name,
9057 view->state.log.repo);
9058 else if (view->type == TOG_VIEW_REF)
9059 err = browse_ref_tree(new_view, y, x,
9060 view->state.ref.selected_entry,
9061 view->state.ref.repo);
9063 return got_error_msg(GOT_ERR_NOT_IMPL,
9064 "parent/child view pair not supported");
9067 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9068 if (*new_view == NULL)
9069 return got_error_from_errno("view_open");
9070 if (view->type == TOG_VIEW_LOG)
9071 err = open_ref_view(*new_view, view->state.log.repo);
9072 else if (view->type == TOG_VIEW_TREE)
9073 err = open_ref_view(*new_view, view->state.tree.repo);
9075 err = got_error_msg(GOT_ERR_NOT_IMPL,
9076 "parent/child view pair not supported");
9078 view_close(*new_view);
9081 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9082 if (*new_view == NULL)
9083 return got_error_from_errno("view_open");
9084 err = open_help_view(*new_view, view);
9086 view_close(*new_view);
9089 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9096 * If view was scrolled down to move the selected line into view when opening a
9097 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9100 offset_selection_up(struct tog_view *view)
9102 switch (view->type) {
9103 case TOG_VIEW_BLAME: {
9104 struct tog_blame_view_state *s = &view->state.blame;
9105 if (s->first_displayed_line == 1) {
9106 s->selected_line = MAX(s->selected_line - view->offset,
9110 if (s->first_displayed_line > view->offset)
9111 s->first_displayed_line -= view->offset;
9113 s->first_displayed_line = 1;
9114 s->selected_line += view->offset;
9118 log_scroll_up(&view->state.log, view->offset);
9119 view->state.log.selected += view->offset;
9122 ref_scroll_up(&view->state.ref, view->offset);
9123 view->state.ref.selected += view->offset;
9126 tree_scroll_up(&view->state.tree, view->offset);
9127 view->state.tree.selected += view->offset;
9137 * If the selected line is in the section of screen covered by the bottom split,
9138 * scroll down offset lines to move it into view and index its new position.
9140 static const struct got_error *
9141 offset_selection_down(struct tog_view *view)
9143 const struct got_error *err = NULL;
9144 const struct got_error *(*scrolld)(struct tog_view *, int);
9145 int *selected = NULL;
9148 switch (view->type) {
9149 case TOG_VIEW_BLAME: {
9150 struct tog_blame_view_state *s = &view->state.blame;
9153 if (s->selected_line > view->nlines - header) {
9154 offset = abs(view->nlines - s->selected_line - header);
9155 s->first_displayed_line += offset;
9156 s->selected_line -= offset;
9157 view->offset = offset;
9161 case TOG_VIEW_LOG: {
9162 struct tog_log_view_state *s = &view->state.log;
9163 scrolld = &log_scroll_down;
9164 header = view_is_parent_view(view) ? 3 : 2;
9165 selected = &s->selected;
9168 case TOG_VIEW_REF: {
9169 struct tog_ref_view_state *s = &view->state.ref;
9170 scrolld = &ref_scroll_down;
9172 selected = &s->selected;
9175 case TOG_VIEW_TREE: {
9176 struct tog_tree_view_state *s = &view->state.tree;
9177 scrolld = &tree_scroll_down;
9179 selected = &s->selected;
9189 if (selected && *selected > view->nlines - header) {
9190 offset = abs(view->nlines - *selected - header);
9191 view->offset = offset;
9192 if (scrolld && offset) {
9193 err = scrolld(view, offset);
9194 *selected -= offset;
9202 list_commands(FILE *fp)
9206 fprintf(fp, "commands:");
9207 for (i = 0; i < nitems(tog_commands); i++) {
9208 const struct tog_cmd *cmd = &tog_commands[i];
9209 fprintf(fp, " %s", cmd->name);
9215 usage(int hflag, int status)
9217 FILE *fp = (status == 0) ? stdout : stderr;
9219 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9222 fprintf(fp, "lazy usage: %s path\n", getprogname());
9229 make_argv(int argc, ...)
9237 argv = calloc(argc, sizeof(char *));
9240 for (i = 0; i < argc; i++) {
9241 argv[i] = strdup(va_arg(ap, char *));
9242 if (argv[i] == NULL)
9251 * Try to convert 'tog path' into a 'tog log path' command.
9252 * The user could simply have mistyped the command rather than knowingly
9253 * provided a path. So check whether argv[0] can in fact be resolved
9254 * to a path in the HEAD commit and print a special error if not.
9255 * This hack is for mpi@ <3
9257 static const struct got_error *
9258 tog_log_with_path(int argc, char *argv[])
9260 const struct got_error *error = NULL, *close_err;
9261 const struct tog_cmd *cmd = NULL;
9262 struct got_repository *repo = NULL;
9263 struct got_worktree *worktree = NULL;
9264 struct got_object_id *commit_id = NULL, *id = NULL;
9265 struct got_commit_object *commit = NULL;
9266 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9267 char *commit_id_str = NULL, **cmd_argv = NULL;
9268 int *pack_fds = NULL;
9270 cwd = getcwd(NULL, 0);
9272 return got_error_from_errno("getcwd");
9274 error = got_repo_pack_fds_open(&pack_fds);
9278 error = got_worktree_open(&worktree, cwd);
9279 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9283 repo_path = strdup(got_worktree_get_repo_path(worktree));
9285 repo_path = strdup(cwd);
9286 if (repo_path == NULL) {
9287 error = got_error_from_errno("strdup");
9291 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9295 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9300 error = tog_load_refs(repo, 0);
9303 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9304 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9305 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9310 got_worktree_close(worktree);
9314 error = got_object_open_as_commit(&commit, repo, commit_id);
9318 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9320 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9322 fprintf(stderr, "%s: '%s' is no known command or path\n",
9323 getprogname(), argv[0]);
9328 error = got_object_id_str(&commit_id_str, commit_id);
9332 cmd = &tog_commands[0]; /* log */
9334 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9335 error = cmd->cmd_main(argc, cmd_argv);
9338 close_err = got_repo_close(repo);
9343 got_object_commit_close(commit);
9345 got_worktree_close(worktree);
9347 const struct got_error *pack_err =
9348 got_repo_pack_fds_close(pack_fds);
9353 free(commit_id_str);
9360 for (i = 0; i < argc; i++)
9369 main(int argc, char *argv[])
9371 const struct got_error *error = NULL;
9372 const struct tog_cmd *cmd = NULL;
9373 int ch, hflag = 0, Vflag = 0;
9374 char **cmd_argv = NULL;
9375 static const struct option longopts[] = {
9376 { "version", no_argument, NULL, 'V' },
9379 char *diff_algo_str = NULL;
9381 if (!isatty(STDIN_FILENO))
9382 errx(1, "standard input is not a tty");
9384 setlocale(LC_CTYPE, "");
9386 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9406 got_version_print_str();
9411 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9419 /* Build an argument vector which runs a default command. */
9420 cmd = &tog_commands[0];
9422 cmd_argv = make_argv(argc, cmd->name);
9426 /* Did the user specify a command? */
9427 for (i = 0; i < nitems(tog_commands); i++) {
9428 if (strncmp(tog_commands[i].name, argv[0],
9429 strlen(argv[0])) == 0) {
9430 cmd = &tog_commands[i];
9436 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9437 if (diff_algo_str) {
9438 if (strcasecmp(diff_algo_str, "patience") == 0)
9439 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9440 if (strcasecmp(diff_algo_str, "myers") == 0)
9441 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9447 /* No command specified; try log with a path */
9448 error = tog_log_with_path(argc, argv);
9453 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9460 for (i = 0; i < argc; i++)
9465 if (error && error->code != GOT_ERR_CANCELLED &&
9466 error->code != GOT_ERR_EOF &&
9467 error->code != GOT_ERR_PRIVSEP_EXIT &&
9468 error->code != GOT_ERR_PRIVSEP_PIPE &&
9469 !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9470 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);