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 Home", "Go to line N (default: first line)"), \
537 KEY_("G End", "Go to line N (default: last line)"), \
538 KEY_("l Right", "Scroll the view right"), \
539 KEY_("h Left", "Scroll the view left"), \
540 KEY_("$", "Scroll view to the rightmost position"), \
541 KEY_("0", "Scroll view to the leftmost position"), \
542 KEY_("-", "Decrease size of the focussed split"), \
543 KEY_("+", "Increase size of the focussed split"), \
544 KEY_("Tab", "Switch focus between views"), \
545 KEY_("F", "Toggle fullscreen mode"), \
546 KEY_("/", "Open prompt to enter search term"), \
547 KEY_("n", "Find next line/token matching the current search term"), \
548 KEY_("N", "Find previous line/token matching the current search term"),\
549 KEY_("q", "Quit the focussed view; Quit help screen"), \
550 KEY_("Q", "Quit tog"), \
552 KEYMAP_("Log view", TOG_KEYMAP_LOG), \
553 KEY_("< ,", "Move cursor up one commit"), \
554 KEY_("> .", "Move cursor down one commit"), \
555 KEY_("Enter", "Open diff view of the selected commit"), \
556 KEY_("B", "Reload the log view and toggle display of merged commits"), \
557 KEY_("R", "Open ref view of all repository references"), \
558 KEY_("T", "Display tree view of the repository from the selected" \
560 KEY_("@", "Toggle between displaying author and committer name"), \
561 KEY_("&", "Open prompt to enter term to limit commits displayed"), \
562 KEY_("C-g Backspace", "Cancel current search or log operation"), \
563 KEY_("C-l", "Reload the log view with new commits in the repository"), \
565 KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
566 KEY_("K < ,", "Display diff of next line in the file/log entry"), \
567 KEY_("J > .", "Display diff of previous line in the file/log entry"), \
568 KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
569 KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
571 KEY_("(", "Go to the previous file in the diff"), \
572 KEY_(")", "Go to the next file in the diff"), \
573 KEY_("{", "Go to the previous hunk in the diff"), \
574 KEY_("}", "Go to the next hunk in the diff"), \
575 KEY_("[", "Decrease the number of context lines"), \
576 KEY_("]", "Increase the number of context lines"), \
577 KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
579 KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
580 KEY_("Enter", "Display diff view of the selected line's commit"), \
581 KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
582 KEY_("L", "Open log view for the currently selected annotated line"), \
583 KEY_("C", "Reload view with the previously blamed commit"), \
584 KEY_("c", "Reload view with the version of the file found in the" \
585 " selected line's commit"), \
586 KEY_("p", "Reload view with the version of the file found in the" \
587 " selected line's parent commit"), \
589 KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
590 KEY_("Enter", "Enter selected directory or open blame view of the" \
592 KEY_("L", "Open log view for the selected entry"), \
593 KEY_("R", "Open ref view of all repository references"), \
594 KEY_("i", "Show object IDs for all tree entries"), \
595 KEY_("Backspace", "Return to the parent directory"), \
597 KEYMAP_("Ref view", TOG_KEYMAP_REF), \
598 KEY_("Enter", "Display log view of the selected reference"), \
599 KEY_("T", "Display tree view of the selected reference"), \
600 KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
601 KEY_("m", "Toggle display of last modified date for each reference"), \
602 KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
603 KEY_("C-l", "Reload view with all repository references")
608 enum tog_keymap_type type;
612 * We implement two types of views: parent views and child views.
614 * The 'Tab' key switches focus between a parent view and its child view.
615 * Child views are shown side-by-side to their parent view, provided
616 * there is enough screen estate.
618 * When a new view is opened from within a parent view, this new view
619 * becomes a child view of the parent view, replacing any existing child.
621 * When a new view is opened from within a child view, this new view
622 * becomes a parent view which will obscure the views below until the
623 * user quits the new parent view by typing 'q'.
625 * This list of views contains parent views only.
626 * Child views are only pointed to by their parent view.
628 TAILQ_HEAD(tog_view_list_head, tog_view);
631 TAILQ_ENTRY(tog_view) entry;
634 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
635 int resized_y, resized_x; /* begin_y/x based on user resizing */
636 int maxx, x; /* max column and current start column */
637 int lines, cols; /* copies of LINES and COLS */
638 int nscrolled, offset; /* lines scrolled and hsplit line offset */
639 int gline, hiline; /* navigate to and highlight this nG line */
640 int ch, count; /* current keymap and count prefix */
641 int resized; /* set when in a resize event */
642 int focussed; /* Only set on one parent or child view at a time. */
644 struct tog_view *parent;
645 struct tog_view *child;
648 * This flag is initially set on parent views when a new child view
649 * is created. It gets toggled when the 'Tab' key switches focus
650 * between parent and child.
651 * The flag indicates whether focus should be passed on to our child
652 * view if this parent view gets picked for focus after another parent
653 * view was closed. This prevents child views from losing focus in such
658 enum tog_view_mode mode;
659 /* type-specific state */
660 enum tog_view_type type;
662 struct tog_diff_view_state diff;
663 struct tog_log_view_state log;
664 struct tog_blame_view_state blame;
665 struct tog_tree_view_state tree;
666 struct tog_ref_view_state ref;
667 struct tog_help_view_state help;
670 const struct got_error *(*show)(struct tog_view *);
671 const struct got_error *(*input)(struct tog_view **,
672 struct tog_view *, int);
673 const struct got_error *(*reset)(struct tog_view *);
674 const struct got_error *(*resize)(struct tog_view *, int);
675 const struct got_error *(*close)(struct tog_view *);
677 const struct got_error *(*search_start)(struct tog_view *);
678 const struct got_error *(*search_next)(struct tog_view *);
679 void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
680 int **, int **, int **, int **);
683 #define TOG_SEARCH_FORWARD 1
684 #define TOG_SEARCH_BACKWARD 2
685 int search_next_done;
686 #define TOG_SEARCH_HAVE_MORE 1
687 #define TOG_SEARCH_NO_MORE 2
688 #define TOG_SEARCH_HAVE_NONE 3
693 static const struct got_error *open_diff_view(struct tog_view *,
694 struct got_object_id *, struct got_object_id *,
695 const char *, const char *, int, int, int, struct tog_view *,
696 struct got_repository *);
697 static const struct got_error *show_diff_view(struct tog_view *);
698 static const struct got_error *input_diff_view(struct tog_view **,
699 struct tog_view *, int);
700 static const struct got_error *reset_diff_view(struct tog_view *);
701 static const struct got_error* close_diff_view(struct tog_view *);
702 static const struct got_error *search_start_diff_view(struct tog_view *);
703 static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
704 size_t *, int **, int **, int **, int **);
705 static const struct got_error *search_next_view_match(struct tog_view *);
707 static const struct got_error *open_log_view(struct tog_view *,
708 struct got_object_id *, struct got_repository *,
709 const char *, const char *, int);
710 static const struct got_error * show_log_view(struct tog_view *);
711 static const struct got_error *input_log_view(struct tog_view **,
712 struct tog_view *, int);
713 static const struct got_error *resize_log_view(struct tog_view *, int);
714 static const struct got_error *close_log_view(struct tog_view *);
715 static const struct got_error *search_start_log_view(struct tog_view *);
716 static const struct got_error *search_next_log_view(struct tog_view *);
718 static const struct got_error *open_blame_view(struct tog_view *, char *,
719 struct got_object_id *, struct got_repository *);
720 static const struct got_error *show_blame_view(struct tog_view *);
721 static const struct got_error *input_blame_view(struct tog_view **,
722 struct tog_view *, int);
723 static const struct got_error *reset_blame_view(struct tog_view *);
724 static const struct got_error *close_blame_view(struct tog_view *);
725 static const struct got_error *search_start_blame_view(struct tog_view *);
726 static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
727 size_t *, int **, int **, int **, int **);
729 static const struct got_error *open_tree_view(struct tog_view *,
730 struct got_object_id *, const char *, struct got_repository *);
731 static const struct got_error *show_tree_view(struct tog_view *);
732 static const struct got_error *input_tree_view(struct tog_view **,
733 struct tog_view *, int);
734 static const struct got_error *close_tree_view(struct tog_view *);
735 static const struct got_error *search_start_tree_view(struct tog_view *);
736 static const struct got_error *search_next_tree_view(struct tog_view *);
738 static const struct got_error *open_ref_view(struct tog_view *,
739 struct got_repository *);
740 static const struct got_error *show_ref_view(struct tog_view *);
741 static const struct got_error *input_ref_view(struct tog_view **,
742 struct tog_view *, int);
743 static const struct got_error *close_ref_view(struct tog_view *);
744 static const struct got_error *search_start_ref_view(struct tog_view *);
745 static const struct got_error *search_next_ref_view(struct tog_view *);
747 static const struct got_error *open_help_view(struct tog_view *,
749 static const struct got_error *show_help_view(struct tog_view *);
750 static const struct got_error *input_help_view(struct tog_view **,
751 struct tog_view *, int);
752 static const struct got_error *reset_help_view(struct tog_view *);
753 static const struct got_error* close_help_view(struct tog_view *);
754 static const struct got_error *search_start_help_view(struct tog_view *);
755 static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
756 size_t *, int **, int **, int **, int **);
758 static volatile sig_atomic_t tog_sigwinch_received;
759 static volatile sig_atomic_t tog_sigpipe_received;
760 static volatile sig_atomic_t tog_sigcont_received;
761 static volatile sig_atomic_t tog_sigint_received;
762 static volatile sig_atomic_t tog_sigterm_received;
765 tog_sigwinch(int signo)
767 tog_sigwinch_received = 1;
771 tog_sigpipe(int signo)
773 tog_sigpipe_received = 1;
777 tog_sigcont(int signo)
779 tog_sigcont_received = 1;
783 tog_sigint(int signo)
785 tog_sigint_received = 1;
789 tog_sigterm(int signo)
791 tog_sigterm_received = 1;
795 tog_fatal_signal_received(void)
797 return (tog_sigpipe_received ||
798 tog_sigint_received || tog_sigterm_received);
801 static const struct got_error *
802 view_close(struct tog_view *view)
804 const struct got_error *err = NULL, *child_err = NULL;
807 child_err = view_close(view->child);
811 err = view->close(view);
813 del_panel(view->panel);
815 delwin(view->window);
817 return err ? err : child_err;
820 static struct tog_view *
821 view_open(int nlines, int ncols, int begin_y, int begin_x,
822 enum tog_view_type type)
824 struct tog_view *view = calloc(1, sizeof(*view));
832 view->nlines = nlines ? nlines : LINES - begin_y;
833 view->ncols = ncols ? ncols : COLS - begin_x;
834 view->begin_y = begin_y;
835 view->begin_x = begin_x;
836 view->window = newwin(nlines, ncols, begin_y, begin_x);
837 if (view->window == NULL) {
841 view->panel = new_panel(view->window);
842 if (view->panel == NULL ||
843 set_panel_userptr(view->panel, view) != OK) {
848 keypad(view->window, TRUE);
853 view_split_begin_x(int begin_x)
855 if (begin_x > 0 || COLS < 120)
857 return (COLS - MAX(COLS / 2, 80));
860 /* XXX Stub till we decide what to do. */
862 view_split_begin_y(int lines)
864 return lines * HSPLIT_SCALE;
867 static const struct got_error *view_resize(struct tog_view *);
869 static const struct got_error *
870 view_splitscreen(struct tog_view *view)
872 const struct got_error *err = NULL;
874 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
875 if (view->resized_y && view->resized_y < view->lines)
876 view->begin_y = view->resized_y;
878 view->begin_y = view_split_begin_y(view->nlines);
880 } else if (!view->resized) {
881 if (view->resized_x && view->resized_x < view->cols - 1 &&
883 view->begin_x = view->resized_x;
885 view->begin_x = view_split_begin_x(0);
888 view->nlines = LINES - view->begin_y;
889 view->ncols = COLS - view->begin_x;
892 err = view_resize(view);
896 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
897 view->parent->nlines = view->begin_y;
899 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
900 return got_error_from_errno("mvwin");
905 static const struct got_error *
906 view_fullscreen(struct tog_view *view)
908 const struct got_error *err = NULL;
911 view->begin_y = view->resized ? view->begin_y : 0;
912 view->nlines = view->resized ? view->nlines : LINES;
916 err = view_resize(view);
920 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
921 return got_error_from_errno("mvwin");
927 view_is_parent_view(struct tog_view *view)
929 return view->parent == NULL;
933 view_is_splitscreen(struct tog_view *view)
935 return view->begin_x > 0 || view->begin_y > 0;
939 view_is_fullscreen(struct tog_view *view)
941 return view->nlines == LINES && view->ncols == COLS;
945 view_is_hsplit_top(struct tog_view *view)
947 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
948 view_is_splitscreen(view->child);
952 view_border(struct tog_view *view)
955 const struct tog_view *view_above;
958 return view_border(view->parent);
960 panel = panel_above(view->panel);
964 view_above = panel_userptr(panel);
965 if (view->mode == TOG_VIEW_SPLIT_HRZN)
966 mvwhline(view->window, view_above->begin_y - 1,
967 view->begin_x, got_locale_is_utf8() ?
968 ACS_HLINE : '-', view->ncols);
970 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
971 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
974 static const struct got_error *view_init_hsplit(struct tog_view *, int);
975 static const struct got_error *request_log_commits(struct tog_view *);
976 static const struct got_error *offset_selection_down(struct tog_view *);
977 static void offset_selection_up(struct tog_view *);
978 static void view_get_split(struct tog_view *, int *, int *);
980 static const struct got_error *
981 view_resize(struct tog_view *view)
983 const struct got_error *err = NULL;
984 int dif, nlines, ncols;
986 dif = LINES - view->lines; /* line difference */
988 if (view->lines > LINES)
989 nlines = view->nlines - (view->lines - LINES);
991 nlines = view->nlines + (LINES - view->lines);
992 if (view->cols > COLS)
993 ncols = view->ncols - (view->cols - COLS);
995 ncols = view->ncols + (COLS - view->cols);
998 int hs = view->child->begin_y;
1000 if (!view_is_fullscreen(view))
1001 view->child->begin_x = view_split_begin_x(view->begin_x);
1002 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1003 view->child->begin_x == 0) {
1006 view_fullscreen(view->child);
1007 if (view->child->focussed)
1008 show_panel(view->child->panel);
1010 show_panel(view->panel);
1012 ncols = view->child->begin_x;
1014 view_splitscreen(view->child);
1015 show_panel(view->child->panel);
1018 * XXX This is ugly and needs to be moved into the above
1019 * logic but "works" for now and my attempts at moving it
1020 * break either 'tab' or 'F' key maps in horizontal splits.
1023 err = view_splitscreen(view->child);
1026 if (dif < 0) { /* top split decreased */
1027 err = offset_selection_down(view);
1034 show_panel(view->child->panel);
1035 nlines = view->nlines;
1037 } else if (view->parent == NULL)
1040 if (view->resize && dif > 0) {
1041 err = view->resize(view, dif);
1046 if (wresize(view->window, nlines, ncols) == ERR)
1047 return got_error_from_errno("wresize");
1048 if (replace_panel(view->panel, view->window) == ERR)
1049 return got_error_from_errno("replace_panel");
1050 wclear(view->window);
1052 view->nlines = nlines;
1053 view->ncols = ncols;
1054 view->lines = LINES;
1060 static const struct got_error *
1061 resize_log_view(struct tog_view *view, int increase)
1063 struct tog_log_view_state *s = &view->state.log;
1064 const struct got_error *err = NULL;
1067 if (s->selected_entry)
1068 n = s->selected_entry->idx + view->lines - s->selected;
1071 * Request commits to account for the increased
1072 * height so we have enough to populate the view.
1074 if (s->commits->ncommits < n) {
1075 view->nscrolled = n - s->commits->ncommits + increase + 1;
1076 err = request_log_commits(view);
1083 view_adjust_offset(struct tog_view *view, int n)
1088 if (view->parent && view->parent->offset) {
1089 if (view->parent->offset + n >= 0)
1090 view->parent->offset += n;
1092 view->parent->offset = 0;
1093 } else if (view->offset) {
1094 if (view->offset - n >= 0)
1101 static const struct got_error *
1102 view_resize_split(struct tog_view *view, int resize)
1104 const struct got_error *err = NULL;
1105 struct tog_view *v = NULL;
1112 if (!v->child || !view_is_splitscreen(v->child))
1115 v->resized = v->child->resized = resize; /* lock for resize event */
1117 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1118 if (v->child->resized_y)
1119 v->child->begin_y = v->child->resized_y;
1121 v->child->begin_y -= resize;
1123 v->child->begin_y += resize;
1124 if (v->child->begin_y < 3) {
1126 v->child->begin_y = 3;
1127 } else if (v->child->begin_y > LINES - 1) {
1129 v->child->begin_y = LINES - 1;
1132 v->child->ncols = COLS;
1133 view_adjust_offset(view, resize);
1134 err = view_init_hsplit(v, v->child->begin_y);
1137 v->child->resized_y = v->child->begin_y;
1139 if (v->child->resized_x)
1140 v->child->begin_x = v->child->resized_x;
1142 v->child->begin_x -= resize;
1144 v->child->begin_x += resize;
1145 if (v->child->begin_x < 11) {
1147 v->child->begin_x = 11;
1148 } else if (v->child->begin_x > COLS - 1) {
1150 v->child->begin_x = COLS - 1;
1152 v->child->resized_x = v->child->begin_x;
1155 v->child->mode = v->mode;
1156 v->child->nlines = v->lines - v->child->begin_y;
1157 v->child->ncols = v->cols - v->child->begin_x;
1160 err = view_fullscreen(v);
1163 err = view_splitscreen(v->child);
1167 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1168 err = offset_selection_down(v->child);
1174 err = v->resize(v, 0);
1175 else if (v->child->resize)
1176 err = v->child->resize(v->child, 0);
1178 v->resized = v->child->resized = 0;
1184 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1186 struct tog_view *v = src->child ? src->child : src;
1188 dst->resized_x = v->resized_x;
1189 dst->resized_y = v->resized_y;
1192 static const struct got_error *
1193 view_close_child(struct tog_view *view)
1195 const struct got_error *err = NULL;
1197 if (view->child == NULL)
1200 err = view_close(view->child);
1205 static const struct got_error *
1206 view_set_child(struct tog_view *view, struct tog_view *child)
1208 const struct got_error *err = NULL;
1210 view->child = child;
1211 child->parent = view;
1213 err = view_resize(view);
1217 if (view->child->resized_x || view->child->resized_y)
1218 err = view_resize_split(view, 0);
1223 static const struct got_error *view_dispatch_request(struct tog_view **,
1224 struct tog_view *, enum tog_view_type, int, int);
1226 static const struct got_error *
1227 view_request_new(struct tog_view **requested, struct tog_view *view,
1228 enum tog_view_type request)
1230 struct tog_view *new_view = NULL;
1231 const struct got_error *err;
1236 if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1237 view_get_split(view, &y, &x);
1239 err = view_dispatch_request(&new_view, view, request, y, x);
1243 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1244 request != TOG_VIEW_HELP) {
1245 err = view_init_hsplit(view, y);
1251 new_view->focussed = 1;
1252 new_view->mode = view->mode;
1253 new_view->nlines = request == TOG_VIEW_HELP ?
1254 view->lines : view->lines - y;
1256 if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1257 view_transfer_size(new_view, view);
1258 err = view_close_child(view);
1261 err = view_set_child(view, new_view);
1264 view->focus_child = 1;
1266 *requested = new_view;
1272 tog_resizeterm(void)
1275 struct winsize size;
1277 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1278 cols = 80; /* Default */
1282 lines = size.ws_row;
1284 resize_term(lines, cols);
1287 static const struct got_error *
1288 view_search_start(struct tog_view *view)
1290 const struct got_error *err = NULL;
1291 struct tog_view *v = view;
1295 if (view->search_started) {
1296 regfree(&view->regex);
1297 view->searching = 0;
1298 memset(&view->regmatch, 0, sizeof(view->regmatch));
1300 view->search_started = 0;
1302 if (view->nlines < 1)
1305 if (view_is_hsplit_top(view))
1307 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1310 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1311 wclrtoeol(v->window);
1313 nodelay(v->window, FALSE); /* block for search term input */
1316 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1317 wrefresh(v->window);
1320 nodelay(v->window, TRUE);
1324 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1325 err = view->search_start(view);
1327 regfree(&view->regex);
1330 view->search_started = 1;
1331 view->searching = TOG_SEARCH_FORWARD;
1332 view->search_next_done = 0;
1333 view->search_next(view);
1339 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1340 static const struct got_error *
1341 switch_split(struct tog_view *view)
1343 const struct got_error *err = NULL;
1344 struct tog_view *v = NULL;
1351 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1352 v->mode = TOG_VIEW_SPLIT_VERT;
1354 v->mode = TOG_VIEW_SPLIT_HRZN;
1358 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1359 v->mode = TOG_VIEW_SPLIT_NONE;
1361 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1362 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1363 v->child->begin_y = v->child->resized_y;
1364 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1365 v->child->begin_x = v->child->resized_x;
1368 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1370 v->child->ncols = COLS;
1371 v->child->nscrolled = LINES - v->child->nlines;
1373 err = view_init_hsplit(v, v->child->begin_y);
1377 v->child->mode = v->mode;
1378 v->child->nlines = v->lines - v->child->begin_y;
1381 err = view_fullscreen(v);
1384 err = view_splitscreen(v->child);
1388 if (v->mode == TOG_VIEW_SPLIT_NONE)
1389 v->mode = TOG_VIEW_SPLIT_VERT;
1390 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1391 err = offset_selection_down(v);
1394 err = offset_selection_down(v->child);
1398 offset_selection_up(v);
1399 offset_selection_up(v->child);
1402 err = v->resize(v, 0);
1403 else if (v->child->resize)
1404 err = v->child->resize(v->child, 0);
1410 * Compute view->count from numeric input. Assign total to view->count and
1411 * return first non-numeric key entered.
1414 get_compound_key(struct tog_view *view, int c)
1416 struct tog_view *v = view;
1419 if (view_is_hsplit_top(view))
1421 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1425 cbreak(); /* block for input */
1426 nodelay(view->window, FALSE);
1427 wmove(v->window, v->nlines - 1, 0);
1428 wclrtoeol(v->window);
1429 waddch(v->window, ':');
1432 x = getcurx(v->window);
1433 if (x != ERR && x < view->ncols) {
1434 waddch(v->window, c);
1435 wrefresh(v->window);
1439 * Don't overflow. Max valid request should be the greatest
1440 * between the longest and total lines; cap at 10 million.
1445 n = n * 10 + (c - '0');
1446 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1448 if (c == 'G' || c == 'g') { /* nG key map */
1449 view->gline = view->hiline = n;
1454 /* Massage excessive or inapplicable values at the input handler. */
1460 static const struct got_error *
1461 view_input(struct tog_view **new, int *done, struct tog_view *view,
1462 struct tog_view_list_head *views)
1464 const struct got_error *err = NULL;
1470 /* Clear "no matches" indicator. */
1471 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1472 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1473 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1477 if (view->searching && !view->search_next_done) {
1478 errcode = pthread_mutex_unlock(&tog_mutex);
1480 return got_error_set_errno(errcode,
1481 "pthread_mutex_unlock");
1483 errcode = pthread_mutex_lock(&tog_mutex);
1485 return got_error_set_errno(errcode,
1486 "pthread_mutex_lock");
1487 view->search_next(view);
1491 /* Allow threads to make progress while we are waiting for input. */
1492 errcode = pthread_mutex_unlock(&tog_mutex);
1494 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1495 /* If we have an unfinished count, let C-g or backspace abort. */
1496 if (view->count && --view->count) {
1498 nodelay(view->window, TRUE);
1499 ch = wgetch(view->window);
1500 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1505 ch = wgetch(view->window);
1506 if (ch >= '1' && ch <= '9')
1507 view->ch = ch = get_compound_key(view, ch);
1509 if (view->hiline && ch != ERR && ch != 0)
1510 view->hiline = 0; /* key pressed, clear line highlight */
1511 nodelay(view->window, TRUE);
1512 errcode = pthread_mutex_lock(&tog_mutex);
1514 return got_error_set_errno(errcode, "pthread_mutex_lock");
1516 if (tog_sigwinch_received || tog_sigcont_received) {
1518 tog_sigwinch_received = 0;
1519 tog_sigcont_received = 0;
1520 TAILQ_FOREACH(v, views, entry) {
1521 err = view_resize(v);
1524 err = v->input(new, v, KEY_RESIZE);
1528 err = view_resize(v->child);
1531 err = v->child->input(new, v->child,
1535 if (v->child->resized_x || v->child->resized_y) {
1536 err = view_resize_split(v, 0);
1548 if (view->type == TOG_VIEW_HELP)
1549 err = view->reset(view);
1551 err = view_request_new(new, view, TOG_VIEW_HELP);
1557 view->child->focussed = 1;
1558 view->focus_child = 1;
1559 } else if (view->parent) {
1561 view->parent->focussed = 1;
1562 view->parent->focus_child = 0;
1563 if (!view_is_splitscreen(view)) {
1564 if (view->parent->resize) {
1565 err = view->parent->resize(view->parent,
1570 offset_selection_up(view->parent);
1571 err = view_fullscreen(view->parent);
1578 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1579 if (view->parent->resize) {
1580 /* might need more commits to fill fullscreen */
1581 err = view->parent->resize(view->parent, 0);
1585 offset_selection_up(view->parent);
1587 err = view->input(new, view, ch);
1595 if (view_is_parent_view(view)) {
1596 if (view->child == NULL)
1598 if (view_is_splitscreen(view->child)) {
1600 view->child->focussed = 1;
1601 err = view_fullscreen(view->child);
1603 err = view_splitscreen(view->child);
1605 err = view_resize_split(view, 0);
1609 err = view->child->input(new, view->child,
1612 if (view_is_splitscreen(view)) {
1613 view->parent->focussed = 0;
1615 err = view_fullscreen(view);
1617 err = view_splitscreen(view);
1618 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1619 err = view_resize(view->parent);
1621 err = view_resize_split(view, 0);
1625 err = view->input(new, view, KEY_RESIZE);
1630 err = view->resize(view, 0);
1635 err = offset_selection_down(view->parent);
1637 err = offset_selection_down(view);
1641 err = switch_split(view);
1644 err = view_resize_split(view, -1);
1647 err = view_resize_split(view, 1);
1653 if (view->search_start)
1654 view_search_start(view);
1656 err = view->input(new, view, ch);
1660 if (view->search_started && view->search_next) {
1661 view->searching = (ch == 'n' ?
1662 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1663 view->search_next_done = 0;
1664 view->search_next(view);
1666 err = view->input(new, view, ch);
1669 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1670 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1672 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1673 TAILQ_FOREACH(v, views, entry) {
1679 if (v->child && v->child->reset) {
1680 err = v->child->reset(v->child);
1687 err = view->input(new, view, ch);
1695 view_needs_focus_indication(struct tog_view *view)
1697 if (view_is_parent_view(view)) {
1698 if (view->child == NULL || view->child->focussed)
1700 if (!view_is_splitscreen(view->child))
1702 } else if (!view_is_splitscreen(view))
1705 return view->focussed;
1708 static const struct got_error *
1709 view_loop(struct tog_view *view)
1711 const struct got_error *err = NULL;
1712 struct tog_view_list_head views;
1713 struct tog_view *new_view;
1715 int fast_refresh = 10;
1716 int done = 0, errcode;
1718 mode = getenv("TOG_VIEW_SPLIT_MODE");
1719 if (!mode || !(*mode == 'h' || *mode == 'H'))
1720 view->mode = TOG_VIEW_SPLIT_VERT;
1722 view->mode = TOG_VIEW_SPLIT_HRZN;
1724 errcode = pthread_mutex_lock(&tog_mutex);
1726 return got_error_set_errno(errcode, "pthread_mutex_lock");
1729 TAILQ_INSERT_HEAD(&views, view, entry);
1732 err = view->show(view);
1737 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1738 !tog_fatal_signal_received()) {
1739 /* Refresh fast during initialization, then become slower. */
1740 if (fast_refresh && fast_refresh-- == 0)
1741 halfdelay(10); /* switch to once per second */
1743 err = view_input(&new_view, &done, view, &views);
1747 struct tog_view *v, *prev = NULL;
1749 if (view_is_parent_view(view))
1750 prev = TAILQ_PREV(view, tog_view_list_head,
1752 else if (view->parent)
1753 prev = view->parent;
1756 view->parent->child = NULL;
1757 view->parent->focus_child = 0;
1758 /* Restore fullscreen line height. */
1759 view->parent->nlines = view->parent->lines;
1760 err = view_resize(view->parent);
1763 /* Make resized splits persist. */
1764 view_transfer_size(view->parent, view);
1766 TAILQ_REMOVE(&views, view, entry);
1768 err = view_close(view);
1773 TAILQ_FOREACH(v, &views, entry) {
1777 if (view == NULL && new_view == NULL) {
1778 /* No view has focus. Try to pick one. */
1781 else if (!TAILQ_EMPTY(&views)) {
1782 view = TAILQ_LAST(&views,
1783 tog_view_list_head);
1786 if (view->focus_child) {
1787 view->child->focussed = 1;
1795 struct tog_view *v, *t;
1796 /* Only allow one parent view per type. */
1797 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1798 if (v->type != new_view->type)
1800 TAILQ_REMOVE(&views, v, entry);
1801 err = view_close(v);
1806 TAILQ_INSERT_TAIL(&views, new_view, entry);
1810 if (view_is_parent_view(view)) {
1811 if (view->child && view->child->focussed)
1814 if (view->parent && view->parent->focussed)
1815 view = view->parent;
1817 show_panel(view->panel);
1818 if (view->child && view_is_splitscreen(view->child))
1819 show_panel(view->child->panel);
1820 if (view->parent && view_is_splitscreen(view)) {
1821 err = view->parent->show(view->parent);
1825 err = view->show(view);
1829 err = view->child->show(view->child);
1838 while (!TAILQ_EMPTY(&views)) {
1839 const struct got_error *close_err;
1840 view = TAILQ_FIRST(&views);
1841 TAILQ_REMOVE(&views, view, entry);
1842 close_err = view_close(view);
1843 if (close_err && err == NULL)
1847 errcode = pthread_mutex_unlock(&tog_mutex);
1848 if (errcode && err == NULL)
1849 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1859 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1864 /* Create newly allocated wide-character string equivalent to a byte string. */
1865 static const struct got_error *
1866 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1869 const struct got_error *err = NULL;
1872 *wlen = mbstowcs(NULL, s, 0);
1873 if (*wlen == (size_t)-1) {
1875 if (errno != EILSEQ)
1876 return got_error_from_errno("mbstowcs");
1878 /* byte string invalid in current encoding; try to "fix" it */
1879 err = got_mbsavis(&vis, &vislen, s);
1882 *wlen = mbstowcs(NULL, vis, 0);
1883 if (*wlen == (size_t)-1) {
1884 err = got_error_from_errno("mbstowcs"); /* give up */
1889 *ws = calloc(*wlen + 1, sizeof(**ws));
1891 err = got_error_from_errno("calloc");
1895 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1896 err = got_error_from_errno("mbstowcs");
1907 static const struct got_error *
1908 expand_tab(char **ptr, const char *src)
1911 size_t len, n, idx = 0, sz = 0;
1914 n = len = strlen(src);
1915 dst = malloc(n + 1);
1917 return got_error_from_errno("malloc");
1919 while (idx < len && src[idx]) {
1920 const char c = src[idx];
1923 size_t nb = TABSIZE - sz % TABSIZE;
1926 p = realloc(dst, n + nb);
1929 return got_error_from_errno("realloc");
1934 memset(dst + sz, ' ', nb);
1937 dst[sz++] = src[idx];
1947 * Advance at most n columns from wline starting at offset off.
1948 * Return the index to the first character after the span operation.
1949 * Return the combined column width of all spanned wide character in
1953 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1955 int width, i, cols = 0;
1962 for (i = off; wline[i] != L'\0'; ++i) {
1963 if (wline[i] == L'\t')
1964 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1966 width = wcwidth(wline[i]);
1973 if (cols + width > n)
1983 * Format a line for display, ensuring that it won't overflow a width limit.
1984 * With scrolling, the width returned refers to the scrolled version of the
1985 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1987 static const struct got_error *
1988 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1989 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1991 const struct got_error *err = NULL;
1993 wchar_t *wline = NULL;
2002 err = expand_tab(&exstr, line);
2007 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2012 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2014 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2015 wline[wlen - 1] = L'\0';
2018 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2019 wline[wlen - 1] = L'\0';
2023 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2029 *scrollxp = scrollx;
2037 static const struct got_error*
2038 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2039 struct got_object_id *id, struct got_repository *repo)
2041 static const struct got_error *err = NULL;
2042 struct got_reflist_entry *re;
2048 TAILQ_FOREACH(re, refs, entry) {
2049 struct got_tag_object *tag = NULL;
2050 struct got_object_id *ref_id;
2053 name = got_ref_get_name(re->ref);
2054 if (strcmp(name, GOT_REF_HEAD) == 0)
2056 if (strncmp(name, "refs/", 5) == 0)
2058 if (strncmp(name, "got/", 4) == 0 &&
2059 strncmp(name, "got/backup/", 11) != 0)
2061 if (strncmp(name, "heads/", 6) == 0)
2063 if (strncmp(name, "remotes/", 8) == 0) {
2065 s = strstr(name, "/" GOT_REF_HEAD);
2066 if (s != NULL && s[strlen(s)] == '\0')
2069 err = got_ref_resolve(&ref_id, repo, re->ref);
2072 if (strncmp(name, "tags/", 5) == 0) {
2073 err = got_object_open_as_tag(&tag, repo, ref_id);
2075 if (err->code != GOT_ERR_OBJ_TYPE) {
2079 /* Ref points at something other than a tag. */
2084 cmp = got_object_id_cmp(tag ?
2085 got_object_tag_get_object_id(tag) : ref_id, id);
2088 got_object_tag_close(tag);
2092 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2093 s ? ", " : "", name) == -1) {
2094 err = got_error_from_errno("asprintf");
2105 static const struct got_error *
2106 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2111 smallerthan = strchr(author, '<');
2112 if (smallerthan && smallerthan[1] != '\0')
2113 author = smallerthan + 1;
2114 author[strcspn(author, "@>")] = '\0';
2115 return format_line(wauthor, author_width, NULL, author, 0, limit,
2119 static const struct got_error *
2120 draw_commit(struct tog_view *view, struct got_commit_object *commit,
2121 struct got_object_id *id, const size_t date_display_cols,
2122 int author_display_cols)
2124 struct tog_log_view_state *s = &view->state.log;
2125 const struct got_error *err = NULL;
2126 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2127 char *logmsg0 = NULL, *logmsg = NULL;
2128 char *author = NULL;
2129 wchar_t *wlogmsg = NULL, *wauthor = NULL;
2130 int author_width, logmsg_width;
2131 char *newline, *line = NULL;
2132 int col, limit, scrollx;
2133 const int avail = view->ncols;
2135 time_t committer_time;
2136 struct tog_color *tc;
2138 committer_time = got_object_commit_get_committer_time(commit);
2139 if (gmtime_r(&committer_time, &tm) == NULL)
2140 return got_error_from_errno("gmtime_r");
2141 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2142 return got_error(GOT_ERR_NO_SPACE);
2144 if (avail <= date_display_cols)
2145 limit = MIN(sizeof(datebuf) - 1, avail);
2147 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2148 tc = get_color(&s->colors, TOG_COLOR_DATE);
2150 wattr_on(view->window,
2151 COLOR_PAIR(tc->colorpair), NULL);
2152 waddnstr(view->window, datebuf, limit);
2154 wattr_off(view->window,
2155 COLOR_PAIR(tc->colorpair), NULL);
2162 err = got_object_id_str(&id_str, id);
2165 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2167 wattr_on(view->window,
2168 COLOR_PAIR(tc->colorpair), NULL);
2169 wprintw(view->window, "%.8s ", id_str);
2171 wattr_off(view->window,
2172 COLOR_PAIR(tc->colorpair), NULL);
2179 if (s->use_committer)
2180 author = strdup(got_object_commit_get_committer(commit));
2182 author = strdup(got_object_commit_get_author(commit));
2183 if (author == NULL) {
2184 err = got_error_from_errno("strdup");
2187 err = format_author(&wauthor, &author_width, author, avail - col, col);
2190 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2192 wattr_on(view->window,
2193 COLOR_PAIR(tc->colorpair), NULL);
2194 waddwstr(view->window, wauthor);
2195 col += author_width;
2196 while (col < avail && author_width < author_display_cols + 2) {
2197 waddch(view->window, ' ');
2202 wattr_off(view->window,
2203 COLOR_PAIR(tc->colorpair), NULL);
2207 err = got_object_commit_get_logmsg(&logmsg0, commit);
2211 while (*logmsg == '\n')
2213 newline = strchr(logmsg, '\n');
2216 limit = avail - col;
2217 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2218 limit--; /* for the border */
2219 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2223 waddwstr(view->window, &wlogmsg[scrollx]);
2224 col += MAX(logmsg_width, 0);
2225 while (col < avail) {
2226 waddch(view->window, ' ');
2238 static struct commit_queue_entry *
2239 alloc_commit_queue_entry(struct got_commit_object *commit,
2240 struct got_object_id *id)
2242 struct commit_queue_entry *entry;
2243 struct got_object_id *dup;
2245 entry = calloc(1, sizeof(*entry));
2249 dup = got_object_id_dup(id);
2256 entry->commit = commit;
2261 pop_commit(struct commit_queue *commits)
2263 struct commit_queue_entry *entry;
2265 entry = TAILQ_FIRST(&commits->head);
2266 TAILQ_REMOVE(&commits->head, entry, entry);
2267 got_object_commit_close(entry->commit);
2268 commits->ncommits--;
2274 free_commits(struct commit_queue *commits)
2276 while (!TAILQ_EMPTY(&commits->head))
2277 pop_commit(commits);
2280 static const struct got_error *
2281 match_commit(int *have_match, struct got_object_id *id,
2282 struct got_commit_object *commit, regex_t *regex)
2284 const struct got_error *err = NULL;
2285 regmatch_t regmatch;
2286 char *id_str = NULL, *logmsg = NULL;
2290 err = got_object_id_str(&id_str, id);
2294 err = got_object_commit_get_logmsg(&logmsg, commit);
2298 if (regexec(regex, got_object_commit_get_author(commit), 1,
2299 ®match, 0) == 0 ||
2300 regexec(regex, got_object_commit_get_committer(commit), 1,
2301 ®match, 0) == 0 ||
2302 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2303 regexec(regex, logmsg, 1, ®match, 0) == 0)
2311 static const struct got_error *
2312 queue_commits(struct tog_log_thread_args *a)
2314 const struct got_error *err = NULL;
2317 * We keep all commits open throughout the lifetime of the log
2318 * view in order to avoid having to re-fetch commits from disk
2319 * while updating the display.
2322 struct got_object_id id;
2323 struct got_commit_object *commit;
2324 struct commit_queue_entry *entry;
2325 int limit_match = 0;
2328 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2333 err = got_object_open_as_commit(&commit, a->repo, &id);
2336 entry = alloc_commit_queue_entry(commit, &id);
2337 if (entry == NULL) {
2338 err = got_error_from_errno("alloc_commit_queue_entry");
2342 errcode = pthread_mutex_lock(&tog_mutex);
2344 err = got_error_set_errno(errcode,
2345 "pthread_mutex_lock");
2349 entry->idx = a->real_commits->ncommits;
2350 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2351 a->real_commits->ncommits++;
2354 err = match_commit(&limit_match, &id, commit,
2360 struct commit_queue_entry *matched;
2362 matched = alloc_commit_queue_entry(
2363 entry->commit, entry->id);
2364 if (matched == NULL) {
2365 err = got_error_from_errno(
2366 "alloc_commit_queue_entry");
2369 matched->commit = entry->commit;
2370 got_object_commit_retain(entry->commit);
2372 matched->idx = a->limit_commits->ncommits;
2373 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2375 a->limit_commits->ncommits++;
2379 * This is how we signal log_thread() that we
2380 * have found a match, and that it should be
2381 * counted as a new entry for the view.
2383 a->limit_match = limit_match;
2386 if (*a->searching == TOG_SEARCH_FORWARD &&
2387 !*a->search_next_done) {
2389 err = match_commit(&have_match, &id, commit, a->regex);
2394 if (limit_match && have_match)
2395 *a->search_next_done =
2396 TOG_SEARCH_HAVE_MORE;
2397 } else if (have_match)
2398 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2401 errcode = pthread_mutex_unlock(&tog_mutex);
2402 if (errcode && err == NULL)
2403 err = got_error_set_errno(errcode,
2404 "pthread_mutex_unlock");
2407 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2413 select_commit(struct tog_log_view_state *s)
2415 struct commit_queue_entry *entry;
2418 entry = s->first_displayed_entry;
2420 if (ncommits == s->selected) {
2421 s->selected_entry = entry;
2424 entry = TAILQ_NEXT(entry, entry);
2429 static const struct got_error *
2430 draw_commits(struct tog_view *view)
2432 const struct got_error *err = NULL;
2433 struct tog_log_view_state *s = &view->state.log;
2434 struct commit_queue_entry *entry = s->selected_entry;
2435 int limit = view->nlines;
2437 int ncommits, author_cols = 4;
2438 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2439 char *refs_str = NULL;
2441 struct tog_color *tc;
2442 static const size_t date_display_cols = 12;
2444 if (view_is_hsplit_top(view))
2445 --limit; /* account for border */
2447 if (s->selected_entry &&
2448 !(view->searching && view->search_next_done == 0)) {
2449 struct got_reflist_head *refs;
2450 err = got_object_id_str(&id_str, s->selected_entry->id);
2453 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2454 s->selected_entry->id);
2456 err = build_refs_str(&refs_str, refs,
2457 s->selected_entry->id, s->repo);
2463 if (s->thread_args.commits_needed == 0)
2464 halfdelay(10); /* disable fast refresh */
2466 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2467 if (asprintf(&ncommits_str, " [%d/%d] %s",
2468 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2469 (view->searching && !view->search_next_done) ?
2470 "searching..." : "loading...") == -1) {
2471 err = got_error_from_errno("asprintf");
2475 const char *search_str = NULL;
2476 const char *limit_str = NULL;
2478 if (view->searching) {
2479 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2480 search_str = "no more matches";
2481 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2482 search_str = "no matches found";
2483 else if (!view->search_next_done)
2484 search_str = "searching...";
2487 if (s->limit_view && s->commits->ncommits == 0)
2488 limit_str = "no matches found";
2490 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2491 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2492 search_str ? search_str : (refs_str ? refs_str : ""),
2493 limit_str ? limit_str : "") == -1) {
2494 err = got_error_from_errno("asprintf");
2499 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2500 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2501 "........................................",
2502 s->in_repo_path, ncommits_str) == -1) {
2503 err = got_error_from_errno("asprintf");
2507 } else if (asprintf(&header, "commit %s%s",
2508 id_str ? id_str : "........................................",
2509 ncommits_str) == -1) {
2510 err = got_error_from_errno("asprintf");
2514 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2518 werase(view->window);
2520 if (view_needs_focus_indication(view))
2521 wstandout(view->window);
2522 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2524 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2525 waddwstr(view->window, wline);
2526 while (width < view->ncols) {
2527 waddch(view->window, ' ');
2531 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2532 if (view_needs_focus_indication(view))
2533 wstandend(view->window);
2538 /* Grow author column size if necessary, and set view->maxx. */
2539 entry = s->first_displayed_entry;
2543 struct got_commit_object *c = entry->commit;
2544 char *author, *eol, *msg, *msg0;
2545 wchar_t *wauthor, *wmsg;
2547 if (ncommits >= limit - 1)
2549 if (s->use_committer)
2550 author = strdup(got_object_commit_get_committer(c));
2552 author = strdup(got_object_commit_get_author(c));
2553 if (author == NULL) {
2554 err = got_error_from_errno("strdup");
2557 err = format_author(&wauthor, &width, author, COLS,
2559 if (author_cols < width)
2560 author_cols = width;
2565 err = got_object_commit_get_logmsg(&msg0, c);
2569 while (*msg == '\n')
2571 if ((eol = strchr(msg, '\n')))
2573 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2574 date_display_cols + author_cols, 0);
2577 view->maxx = MAX(view->maxx, width);
2581 entry = TAILQ_NEXT(entry, entry);
2584 entry = s->first_displayed_entry;
2585 s->last_displayed_entry = s->first_displayed_entry;
2588 if (ncommits >= limit - 1)
2590 if (ncommits == s->selected)
2591 wstandout(view->window);
2592 err = draw_commit(view, entry->commit, entry->id,
2593 date_display_cols, author_cols);
2594 if (ncommits == s->selected)
2595 wstandend(view->window);
2599 s->last_displayed_entry = entry;
2600 entry = TAILQ_NEXT(entry, entry);
2613 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2615 struct commit_queue_entry *entry;
2618 entry = TAILQ_FIRST(&s->commits->head);
2619 if (s->first_displayed_entry == entry)
2622 entry = s->first_displayed_entry;
2623 while (entry && nscrolled < maxscroll) {
2624 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2626 s->first_displayed_entry = entry;
2632 static const struct got_error *
2633 trigger_log_thread(struct tog_view *view, int wait)
2635 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2638 halfdelay(1); /* fast refresh while loading commits */
2640 while (!ta->log_complete && !tog_thread_error &&
2641 (ta->commits_needed > 0 || ta->load_all)) {
2642 /* Wake the log thread. */
2643 errcode = pthread_cond_signal(&ta->need_commits);
2645 return got_error_set_errno(errcode,
2646 "pthread_cond_signal");
2649 * The mutex will be released while the view loop waits
2650 * in wgetch(), at which time the log thread will run.
2655 /* Display progress update in log view. */
2656 show_log_view(view);
2660 /* Wait right here while next commit is being loaded. */
2661 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2663 return got_error_set_errno(errcode,
2664 "pthread_cond_wait");
2666 /* Display progress update in log view. */
2667 show_log_view(view);
2675 static const struct got_error *
2676 request_log_commits(struct tog_view *view)
2678 struct tog_log_view_state *state = &view->state.log;
2679 const struct got_error *err = NULL;
2681 if (state->thread_args.log_complete)
2684 state->thread_args.commits_needed += view->nscrolled;
2685 err = trigger_log_thread(view, 1);
2686 view->nscrolled = 0;
2691 static const struct got_error *
2692 log_scroll_down(struct tog_view *view, int maxscroll)
2694 struct tog_log_view_state *s = &view->state.log;
2695 const struct got_error *err = NULL;
2696 struct commit_queue_entry *pentry;
2697 int nscrolled = 0, ncommits_needed;
2699 if (s->last_displayed_entry == NULL)
2702 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2703 if (s->commits->ncommits < ncommits_needed &&
2704 !s->thread_args.log_complete) {
2706 * Ask the log thread for required amount of commits.
2708 s->thread_args.commits_needed +=
2709 ncommits_needed - s->commits->ncommits;
2710 err = trigger_log_thread(view, 1);
2716 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2717 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2720 s->last_displayed_entry = pentry ?
2721 pentry : s->last_displayed_entry;;
2723 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2726 s->first_displayed_entry = pentry;
2727 } while (++nscrolled < maxscroll);
2729 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2730 view->nscrolled += nscrolled;
2732 view->nscrolled = 0;
2737 static const struct got_error *
2738 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2739 struct got_commit_object *commit, struct got_object_id *commit_id,
2740 struct tog_view *log_view, struct got_repository *repo)
2742 const struct got_error *err;
2743 struct got_object_qid *parent_id;
2744 struct tog_view *diff_view;
2746 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2747 if (diff_view == NULL)
2748 return got_error_from_errno("view_open");
2750 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2751 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2752 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2754 *new_view = diff_view;
2758 static const struct got_error *
2759 tree_view_visit_subtree(struct tog_tree_view_state *s,
2760 struct got_tree_object *subtree)
2762 struct tog_parent_tree *parent;
2764 parent = calloc(1, sizeof(*parent));
2766 return got_error_from_errno("calloc");
2768 parent->tree = s->tree;
2769 parent->first_displayed_entry = s->first_displayed_entry;
2770 parent->selected_entry = s->selected_entry;
2771 parent->selected = s->selected;
2772 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2775 s->first_displayed_entry = NULL;
2779 static const struct got_error *
2780 tree_view_walk_path(struct tog_tree_view_state *s,
2781 struct got_commit_object *commit, const char *path)
2783 const struct got_error *err = NULL;
2784 struct got_tree_object *tree = NULL;
2786 char *slash, *subpath = NULL;
2788 /* Walk the path and open corresponding tree objects. */
2791 struct got_tree_entry *te;
2792 struct got_object_id *tree_id;
2798 /* Ensure the correct subtree entry is selected. */
2799 slash = strchr(p, '/');
2801 te_name = strdup(p);
2803 te_name = strndup(p, slash - p);
2804 if (te_name == NULL) {
2805 err = got_error_from_errno("strndup");
2808 te = got_object_tree_find_entry(s->tree, te_name);
2810 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2815 s->first_displayed_entry = s->selected_entry = te;
2817 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2818 break; /* jump to this file's entry */
2820 slash = strchr(p, '/');
2822 subpath = strndup(path, slash - path);
2824 subpath = strdup(path);
2825 if (subpath == NULL) {
2826 err = got_error_from_errno("strdup");
2830 err = got_object_id_by_path(&tree_id, s->repo, commit,
2835 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2840 err = tree_view_visit_subtree(s, tree);
2842 got_object_tree_close(tree);
2856 static const struct got_error *
2857 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2858 struct commit_queue_entry *entry, const char *path,
2859 const char *head_ref_name, struct got_repository *repo)
2861 const struct got_error *err = NULL;
2862 struct tog_tree_view_state *s;
2863 struct tog_view *tree_view;
2865 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2866 if (tree_view == NULL)
2867 return got_error_from_errno("view_open");
2869 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2872 s = &tree_view->state.tree;
2874 *new_view = tree_view;
2876 if (got_path_is_root_dir(path))
2879 return tree_view_walk_path(s, entry->commit, path);
2882 static const struct got_error *
2883 block_signals_used_by_main_thread(void)
2888 if (sigemptyset(&sigset) == -1)
2889 return got_error_from_errno("sigemptyset");
2891 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2892 if (sigaddset(&sigset, SIGWINCH) == -1)
2893 return got_error_from_errno("sigaddset");
2894 if (sigaddset(&sigset, SIGCONT) == -1)
2895 return got_error_from_errno("sigaddset");
2896 if (sigaddset(&sigset, SIGINT) == -1)
2897 return got_error_from_errno("sigaddset");
2898 if (sigaddset(&sigset, SIGTERM) == -1)
2899 return got_error_from_errno("sigaddset");
2901 /* ncurses handles SIGTSTP */
2902 if (sigaddset(&sigset, SIGTSTP) == -1)
2903 return got_error_from_errno("sigaddset");
2905 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2907 return got_error_set_errno(errcode, "pthread_sigmask");
2913 log_thread(void *arg)
2915 const struct got_error *err = NULL;
2917 struct tog_log_thread_args *a = arg;
2921 * Sync startup with main thread such that we begin our
2922 * work once view_input() has released the mutex.
2924 errcode = pthread_mutex_lock(&tog_mutex);
2926 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2930 err = block_signals_used_by_main_thread();
2932 pthread_mutex_unlock(&tog_mutex);
2936 while (!done && !err && !tog_fatal_signal_received()) {
2937 errcode = pthread_mutex_unlock(&tog_mutex);
2939 err = got_error_set_errno(errcode,
2940 "pthread_mutex_unlock");
2943 err = queue_commits(a);
2945 if (err->code != GOT_ERR_ITER_COMPLETED)
2949 } else if (a->commits_needed > 0 && !a->load_all) {
2952 a->commits_needed--;
2954 a->commits_needed--;
2957 errcode = pthread_mutex_lock(&tog_mutex);
2959 err = got_error_set_errno(errcode,
2960 "pthread_mutex_lock");
2962 } else if (*a->quit)
2964 else if (*a->limiting && *a->first_displayed_entry == NULL) {
2965 *a->first_displayed_entry =
2966 TAILQ_FIRST(&a->limit_commits->head);
2967 *a->selected_entry = *a->first_displayed_entry;
2968 } else if (*a->first_displayed_entry == NULL) {
2969 *a->first_displayed_entry =
2970 TAILQ_FIRST(&a->real_commits->head);
2971 *a->selected_entry = *a->first_displayed_entry;
2974 errcode = pthread_cond_signal(&a->commit_loaded);
2976 err = got_error_set_errno(errcode,
2977 "pthread_cond_signal");
2978 pthread_mutex_unlock(&tog_mutex);
2983 a->commits_needed = 0;
2985 if (a->commits_needed == 0 && !a->load_all) {
2986 errcode = pthread_cond_wait(&a->need_commits,
2989 err = got_error_set_errno(errcode,
2990 "pthread_cond_wait");
2991 pthread_mutex_unlock(&tog_mutex);
2999 a->log_complete = 1;
3000 errcode = pthread_mutex_unlock(&tog_mutex);
3002 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3005 tog_thread_error = 1;
3006 pthread_cond_signal(&a->commit_loaded);
3011 static const struct got_error *
3012 stop_log_thread(struct tog_log_view_state *s)
3014 const struct got_error *err = NULL, *thread_err = NULL;
3019 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3021 return got_error_set_errno(errcode,
3022 "pthread_cond_signal");
3023 errcode = pthread_mutex_unlock(&tog_mutex);
3025 return got_error_set_errno(errcode,
3026 "pthread_mutex_unlock");
3027 errcode = pthread_join(s->thread, (void **)&thread_err);
3029 return got_error_set_errno(errcode, "pthread_join");
3030 errcode = pthread_mutex_lock(&tog_mutex);
3032 return got_error_set_errno(errcode,
3033 "pthread_mutex_lock");
3037 if (s->thread_args.repo) {
3038 err = got_repo_close(s->thread_args.repo);
3039 s->thread_args.repo = NULL;
3042 if (s->thread_args.pack_fds) {
3043 const struct got_error *pack_err =
3044 got_repo_pack_fds_close(s->thread_args.pack_fds);
3047 s->thread_args.pack_fds = NULL;
3050 if (s->thread_args.graph) {
3051 got_commit_graph_close(s->thread_args.graph);
3052 s->thread_args.graph = NULL;
3055 return err ? err : thread_err;
3058 static const struct got_error *
3059 close_log_view(struct tog_view *view)
3061 const struct got_error *err = NULL;
3062 struct tog_log_view_state *s = &view->state.log;
3065 err = stop_log_thread(s);
3067 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3068 if (errcode && err == NULL)
3069 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3071 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3072 if (errcode && err == NULL)
3073 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3075 free_commits(&s->limit_commits);
3076 free_commits(&s->real_commits);
3077 free(s->in_repo_path);
3078 s->in_repo_path = NULL;
3081 free(s->head_ref_name);
3082 s->head_ref_name = NULL;
3087 * We use two queues to implement the limit feature: first consists of
3088 * commits matching the current limit_regex; second is the real queue
3089 * of all known commits (real_commits). When the user starts limiting,
3090 * we swap queues such that all movement and displaying functionality
3091 * works with very slight change.
3093 static const struct got_error *
3094 limit_log_view(struct tog_view *view)
3096 struct tog_log_view_state *s = &view->state.log;
3097 struct commit_queue_entry *entry;
3098 struct tog_view *v = view;
3099 const struct got_error *err = NULL;
3103 if (view_is_hsplit_top(view))
3105 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3108 /* Get the pattern */
3109 wmove(v->window, v->nlines - 1, 0);
3110 wclrtoeol(v->window);
3111 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3112 nodelay(v->window, FALSE);
3115 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3118 nodelay(v->window, TRUE);
3122 if (*pattern == '\0') {
3124 * Safety measure for the situation where the user
3125 * resets limit without previously limiting anything.
3131 * User could have pressed Ctrl+L, which refreshed the
3132 * commit queues, it means we can't save previously
3133 * (before limit took place) displayed entries,
3134 * because they would point to already free'ed memory,
3135 * so we are forced to always select first entry of
3138 s->commits = &s->real_commits;
3139 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3140 s->selected_entry = s->first_displayed_entry;
3147 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3152 /* Clear the screen while loading limit view */
3153 s->first_displayed_entry = NULL;
3154 s->last_displayed_entry = NULL;
3155 s->selected_entry = NULL;
3156 s->commits = &s->limit_commits;
3158 /* Prepare limit queue for new search */
3159 free_commits(&s->limit_commits);
3160 s->limit_commits.ncommits = 0;
3162 /* First process commits, which are in queue already */
3163 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3166 err = match_commit(&have_match, entry->id,
3167 entry->commit, &s->limit_regex);
3172 struct commit_queue_entry *matched;
3174 matched = alloc_commit_queue_entry(entry->commit,
3176 if (matched == NULL) {
3177 err = got_error_from_errno(
3178 "alloc_commit_queue_entry");
3181 matched->commit = entry->commit;
3182 got_object_commit_retain(entry->commit);
3184 matched->idx = s->limit_commits.ncommits;
3185 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3187 s->limit_commits.ncommits++;
3191 /* Second process all the commits, until we fill the screen */
3192 if (s->limit_commits.ncommits < view->nlines - 1 &&
3193 !s->thread_args.log_complete) {
3194 s->thread_args.commits_needed +=
3195 view->nlines - s->limit_commits.ncommits - 1;
3196 err = trigger_log_thread(view, 1);
3201 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3202 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3208 static const struct got_error *
3209 search_start_log_view(struct tog_view *view)
3211 struct tog_log_view_state *s = &view->state.log;
3213 s->matched_entry = NULL;
3214 s->search_entry = NULL;
3218 static const struct got_error *
3219 search_next_log_view(struct tog_view *view)
3221 const struct got_error *err = NULL;
3222 struct tog_log_view_state *s = &view->state.log;
3223 struct commit_queue_entry *entry;
3225 /* Display progress update in log view. */
3226 show_log_view(view);
3230 if (s->search_entry) {
3232 errcode = pthread_mutex_unlock(&tog_mutex);
3234 return got_error_set_errno(errcode,
3235 "pthread_mutex_unlock");
3236 ch = wgetch(view->window);
3237 errcode = pthread_mutex_lock(&tog_mutex);
3239 return got_error_set_errno(errcode,
3240 "pthread_mutex_lock");
3241 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3242 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3245 if (view->searching == TOG_SEARCH_FORWARD)
3246 entry = TAILQ_NEXT(s->search_entry, entry);
3248 entry = TAILQ_PREV(s->search_entry,
3249 commit_queue_head, entry);
3250 } else if (s->matched_entry) {
3252 * If the user has moved the cursor after we hit a match,
3253 * the position from where we should continue searching
3254 * might have changed.
3256 if (view->searching == TOG_SEARCH_FORWARD)
3257 entry = TAILQ_NEXT(s->selected_entry, entry);
3259 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3262 entry = s->selected_entry;
3268 if (entry == NULL) {
3269 if (s->thread_args.log_complete ||
3270 view->searching == TOG_SEARCH_BACKWARD) {
3271 view->search_next_done =
3272 (s->matched_entry == NULL ?
3273 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3274 s->search_entry = NULL;
3278 * Poke the log thread for more commits and return,
3279 * allowing the main loop to make progress. Search
3280 * will resume at s->search_entry once we come back.
3282 s->thread_args.commits_needed++;
3283 return trigger_log_thread(view, 0);
3286 err = match_commit(&have_match, entry->id, entry->commit,
3291 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3292 s->matched_entry = entry;
3296 s->search_entry = entry;
3297 if (view->searching == TOG_SEARCH_FORWARD)
3298 entry = TAILQ_NEXT(entry, entry);
3300 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3303 if (s->matched_entry) {
3304 int cur = s->selected_entry->idx;
3305 while (cur < s->matched_entry->idx) {
3306 err = input_log_view(NULL, view, KEY_DOWN);
3311 while (cur > s->matched_entry->idx) {
3312 err = input_log_view(NULL, view, KEY_UP);
3319 s->search_entry = NULL;
3324 static const struct got_error *
3325 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3326 struct got_repository *repo, const char *head_ref_name,
3327 const char *in_repo_path, int log_branches)
3329 const struct got_error *err = NULL;
3330 struct tog_log_view_state *s = &view->state.log;
3331 struct got_repository *thread_repo = NULL;
3332 struct got_commit_graph *thread_graph = NULL;
3335 if (in_repo_path != s->in_repo_path) {
3336 free(s->in_repo_path);
3337 s->in_repo_path = strdup(in_repo_path);
3338 if (s->in_repo_path == NULL)
3339 return got_error_from_errno("strdup");
3342 /* The commit queue only contains commits being displayed. */
3343 TAILQ_INIT(&s->real_commits.head);
3344 s->real_commits.ncommits = 0;
3345 s->commits = &s->real_commits;
3347 TAILQ_INIT(&s->limit_commits.head);
3349 s->limit_commits.ncommits = 0;
3352 if (head_ref_name) {
3353 s->head_ref_name = strdup(head_ref_name);
3354 if (s->head_ref_name == NULL) {
3355 err = got_error_from_errno("strdup");
3359 s->start_id = got_object_id_dup(start_id);
3360 if (s->start_id == NULL) {
3361 err = got_error_from_errno("got_object_id_dup");
3364 s->log_branches = log_branches;
3365 s->use_committer = 1;
3367 STAILQ_INIT(&s->colors);
3368 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3369 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3370 get_color_value("TOG_COLOR_COMMIT"));
3373 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3374 get_color_value("TOG_COLOR_AUTHOR"));
3376 free_colors(&s->colors);
3379 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3380 get_color_value("TOG_COLOR_DATE"));
3382 free_colors(&s->colors);
3387 view->show = show_log_view;
3388 view->input = input_log_view;
3389 view->resize = resize_log_view;
3390 view->close = close_log_view;
3391 view->search_start = search_start_log_view;
3392 view->search_next = search_next_log_view;
3394 if (s->thread_args.pack_fds == NULL) {
3395 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3399 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3400 s->thread_args.pack_fds);
3403 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3407 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3408 s->repo, NULL, NULL);
3412 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3414 err = got_error_set_errno(errcode, "pthread_cond_init");
3417 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3419 err = got_error_set_errno(errcode, "pthread_cond_init");
3423 s->thread_args.commits_needed = view->nlines;
3424 s->thread_args.graph = thread_graph;
3425 s->thread_args.real_commits = &s->real_commits;
3426 s->thread_args.limit_commits = &s->limit_commits;
3427 s->thread_args.in_repo_path = s->in_repo_path;
3428 s->thread_args.start_id = s->start_id;
3429 s->thread_args.repo = thread_repo;
3430 s->thread_args.log_complete = 0;
3431 s->thread_args.quit = &s->quit;
3432 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3433 s->thread_args.selected_entry = &s->selected_entry;
3434 s->thread_args.searching = &view->searching;
3435 s->thread_args.search_next_done = &view->search_next_done;
3436 s->thread_args.regex = &view->regex;
3437 s->thread_args.limiting = &s->limit_view;
3438 s->thread_args.limit_regex = &s->limit_regex;
3439 s->thread_args.limit_commits = &s->limit_commits;
3442 close_log_view(view);
3446 static const struct got_error *
3447 show_log_view(struct tog_view *view)
3449 const struct got_error *err;
3450 struct tog_log_view_state *s = &view->state.log;
3452 if (s->thread == NULL) {
3453 int errcode = pthread_create(&s->thread, NULL, log_thread,
3456 return got_error_set_errno(errcode, "pthread_create");
3457 if (s->thread_args.commits_needed > 0) {
3458 err = trigger_log_thread(view, 1);
3464 return draw_commits(view);
3468 log_move_cursor_up(struct tog_view *view, int page, int home)
3470 struct tog_log_view_state *s = &view->state.log;
3472 if (s->first_displayed_entry == NULL)
3474 if (s->selected_entry->idx == 0)
3477 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3479 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3481 if (!page && !home && s->selected > 0)
3484 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3490 static const struct got_error *
3491 log_move_cursor_down(struct tog_view *view, int page)
3493 struct tog_log_view_state *s = &view->state.log;
3494 const struct got_error *err = NULL;
3495 int eos = view->nlines - 2;
3497 if (s->first_displayed_entry == NULL)
3500 if (s->thread_args.log_complete &&
3501 s->selected_entry->idx >= s->commits->ncommits - 1)
3504 if (view_is_hsplit_top(view))
3505 --eos; /* border consumes the last line */
3508 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3511 err = log_scroll_down(view, 1);
3512 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3513 struct commit_queue_entry *entry;
3517 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3518 s->last_displayed_entry = entry;
3519 for (n = 0; n <= eos; n++) {
3522 s->first_displayed_entry = entry;
3523 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3526 s->selected = n - 1;
3528 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3529 s->thread_args.log_complete)
3530 s->selected += MIN(page,
3531 s->commits->ncommits - s->selected_entry->idx - 1);
3533 err = log_scroll_down(view, page);
3539 * We might necessarily overshoot in horizontal
3540 * splits; if so, select the last displayed commit.
3542 if (s->first_displayed_entry && s->last_displayed_entry) {
3543 s->selected = MIN(s->selected,
3544 s->last_displayed_entry->idx -
3545 s->first_displayed_entry->idx);
3550 if (s->thread_args.log_complete &&
3551 s->selected_entry->idx == s->commits->ncommits - 1)
3558 view_get_split(struct tog_view *view, int *y, int *x)
3563 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3564 if (view->child && view->child->resized_y)
3565 *y = view->child->resized_y;
3566 else if (view->resized_y)
3567 *y = view->resized_y;
3569 *y = view_split_begin_y(view->lines);
3570 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3571 if (view->child && view->child->resized_x)
3572 *x = view->child->resized_x;
3573 else if (view->resized_x)
3574 *x = view->resized_x;
3576 *x = view_split_begin_x(view->begin_x);
3580 /* Split view horizontally at y and offset view->state->selected line. */
3581 static const struct got_error *
3582 view_init_hsplit(struct tog_view *view, int y)
3584 const struct got_error *err = NULL;
3588 err = view_resize(view);
3592 err = offset_selection_down(view);
3597 static const struct got_error *
3598 log_goto_line(struct tog_view *view, int nlines)
3600 const struct got_error *err = NULL;
3601 struct tog_log_view_state *s = &view->state.log;
3602 int g, idx = s->selected_entry->idx;
3604 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3610 if (g >= s->first_displayed_entry->idx + 1 &&
3611 g <= s->last_displayed_entry->idx + 1 &&
3612 g - s->first_displayed_entry->idx - 1 < nlines) {
3613 s->selected = g - s->first_displayed_entry->idx - 1;
3619 err = log_move_cursor_down(view, g - idx - 1);
3620 if (!err && g > s->selected_entry->idx + 1)
3621 err = log_move_cursor_down(view,
3622 g - s->first_displayed_entry->idx - 1);
3625 } else if (idx + 1 > g)
3626 log_move_cursor_up(view, idx - g + 1, 0);
3628 if (g < nlines && s->first_displayed_entry->idx == 0)
3629 s->selected = g - 1;
3636 static const struct got_error *
3637 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3639 const struct got_error *err = NULL;
3640 struct tog_log_view_state *s = &view->state.log;
3643 if (s->thread_args.load_all) {
3644 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3645 s->thread_args.load_all = 0;
3646 else if (s->thread_args.log_complete) {
3647 err = log_move_cursor_down(view, s->commits->ncommits);
3648 s->thread_args.load_all = 0;
3654 eos = nscroll = view->nlines - 1;
3655 if (view_is_hsplit_top(view))
3659 return log_goto_line(view, eos);
3663 err = limit_log_view(view);
3672 view->x = MAX(view->maxx - view->ncols / 2, 0);
3677 if (view->x + view->ncols / 2 < view->maxx)
3678 view->x += 2; /* move two columns right */
3684 view->x -= MIN(view->x, 2); /* move two columns back */
3693 log_move_cursor_up(view, 0, 0);
3697 log_move_cursor_up(view, 0, 1);
3707 log_move_cursor_up(view, nscroll, 0);
3714 err = log_move_cursor_down(view, 0);
3717 s->use_committer = !s->use_committer;
3721 /* We don't know yet how many commits, so we're forced to
3722 * traverse them all. */
3724 s->thread_args.load_all = 1;
3725 if (!s->thread_args.log_complete)
3726 return trigger_log_thread(view, 0);
3727 err = log_move_cursor_down(view, s->commits->ncommits);
3728 s->thread_args.load_all = 0;
3739 err = log_move_cursor_down(view, nscroll);
3742 if (s->selected > view->nlines - 2)
3743 s->selected = view->nlines - 2;
3744 if (s->selected > s->commits->ncommits - 1)
3745 s->selected = s->commits->ncommits - 1;
3747 if (s->commits->ncommits < view->nlines - 1 &&
3748 !s->thread_args.log_complete) {
3749 s->thread_args.commits_needed += (view->nlines - 1) -
3750 s->commits->ncommits;
3751 err = trigger_log_thread(view, 1);
3757 if (s->selected_entry == NULL)
3759 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3763 if (s->selected_entry == NULL)
3765 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3771 if (ch == KEY_BACKSPACE &&
3772 got_path_is_root_dir(s->in_repo_path))
3774 err = stop_log_thread(s);
3777 if (ch == KEY_BACKSPACE) {
3779 err = got_path_dirname(&parent_path, s->in_repo_path);
3782 free(s->in_repo_path);
3783 s->in_repo_path = parent_path;
3784 s->thread_args.in_repo_path = s->in_repo_path;
3785 } else if (ch == CTRL('l')) {
3786 struct got_object_id *start_id;
3787 err = got_repo_match_object_id(&start_id, NULL,
3788 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3789 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3791 if (s->head_ref_name == NULL ||
3792 err->code != GOT_ERR_NOT_REF)
3794 /* Try to cope with deleted references. */
3795 free(s->head_ref_name);
3796 s->head_ref_name = NULL;
3797 err = got_repo_match_object_id(&start_id,
3798 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3799 &tog_refs, s->repo);
3804 s->start_id = start_id;
3805 s->thread_args.start_id = s->start_id;
3807 s->log_branches = !s->log_branches;
3809 if (s->thread_args.pack_fds == NULL) {
3810 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3814 err = got_repo_open(&s->thread_args.repo,
3815 got_repo_get_path(s->repo), NULL,
3816 s->thread_args.pack_fds);
3820 err = tog_load_refs(s->repo, 0);
3823 err = got_commit_graph_open(&s->thread_args.graph,
3824 s->in_repo_path, !s->log_branches);
3827 err = got_commit_graph_iter_start(s->thread_args.graph,
3828 s->start_id, s->repo, NULL, NULL);
3831 free_commits(&s->real_commits);
3832 free_commits(&s->limit_commits);
3833 s->first_displayed_entry = NULL;
3834 s->last_displayed_entry = NULL;
3835 s->selected_entry = NULL;
3837 s->thread_args.log_complete = 0;
3839 s->thread_args.commits_needed = view->lines;
3840 s->matched_entry = NULL;
3841 s->search_entry = NULL;
3846 err = view_request_new(new_view, view, TOG_VIEW_REF);
3856 static const struct got_error *
3857 apply_unveil(const char *repo_path, const char *worktree_path)
3859 const struct got_error *error;
3862 if (unveil("gmon.out", "rwc") != 0)
3863 return got_error_from_errno2("unveil", "gmon.out");
3865 if (repo_path && unveil(repo_path, "r") != 0)
3866 return got_error_from_errno2("unveil", repo_path);
3868 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3869 return got_error_from_errno2("unveil", worktree_path);
3871 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3872 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3874 error = got_privsep_unveil_exec_helpers();
3878 if (unveil(NULL, NULL) != 0)
3879 return got_error_from_errno("unveil");
3888 * Override default signal handlers before starting ncurses.
3889 * This should prevent ncurses from installing its own
3890 * broken cleanup() signal handler.
3892 signal(SIGWINCH, tog_sigwinch);
3893 signal(SIGPIPE, tog_sigpipe);
3894 signal(SIGCONT, tog_sigcont);
3895 signal(SIGINT, tog_sigint);
3896 signal(SIGTERM, tog_sigterm);
3900 halfdelay(1); /* Do fast refresh while initial view is loading. */
3903 intrflush(stdscr, FALSE);
3904 keypad(stdscr, TRUE);
3906 if (getenv("TOG_COLORS") != NULL) {
3908 use_default_colors();
3912 static const struct got_error *
3913 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3914 struct got_repository *repo, struct got_worktree *worktree)
3916 const struct got_error *err = NULL;
3919 *in_repo_path = strdup("/");
3920 if (*in_repo_path == NULL)
3921 return got_error_from_errno("strdup");
3926 const char *prefix = got_worktree_get_path_prefix(worktree);
3929 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3932 if (asprintf(in_repo_path, "%s%s%s", prefix,
3933 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3935 err = got_error_from_errno("asprintf");
3936 *in_repo_path = NULL;
3940 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3945 static const struct got_error *
3946 cmd_log(int argc, char *argv[])
3948 const struct got_error *error;
3949 struct got_repository *repo = NULL;
3950 struct got_worktree *worktree = NULL;
3951 struct got_object_id *start_id = NULL;
3952 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3953 char *start_commit = NULL, *label = NULL;
3954 struct got_reference *ref = NULL;
3955 const char *head_ref_name = NULL;
3956 int ch, log_branches = 0;
3957 struct tog_view *view;
3958 int *pack_fds = NULL;
3960 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3966 start_commit = optarg;
3969 repo_path = realpath(optarg, NULL);
3970 if (repo_path == NULL)
3971 return got_error_from_errno2("realpath",
3986 error = got_repo_pack_fds_open(&pack_fds);
3990 if (repo_path == NULL) {
3991 cwd = getcwd(NULL, 0);
3993 return got_error_from_errno("getcwd");
3994 error = got_worktree_open(&worktree, cwd);
3995 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3999 strdup(got_worktree_get_repo_path(worktree));
4001 repo_path = strdup(cwd);
4002 if (repo_path == NULL) {
4003 error = got_error_from_errno("strdup");
4008 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4012 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4019 error = apply_unveil(got_repo_get_path(repo),
4020 worktree ? got_worktree_get_root_path(worktree) : NULL);
4024 /* already loaded by tog_log_with_path()? */
4025 if (TAILQ_EMPTY(&tog_refs)) {
4026 error = tog_load_refs(repo, 0);
4031 if (start_commit == NULL) {
4032 error = got_repo_match_object_id(&start_id, &label,
4033 worktree ? got_worktree_get_head_ref_name(worktree) :
4034 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4037 head_ref_name = label;
4039 error = got_ref_open(&ref, repo, start_commit, 0);
4041 head_ref_name = got_ref_get_name(ref);
4042 else if (error->code != GOT_ERR_NOT_REF)
4044 error = got_repo_match_object_id(&start_id, NULL,
4045 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4050 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4052 error = got_error_from_errno("view_open");
4055 error = open_log_view(view, start_id, repo, head_ref_name,
4056 in_repo_path, log_branches);
4060 /* Release work tree lock. */
4061 got_worktree_close(worktree);
4064 error = view_loop(view);
4074 const struct got_error *close_err = got_repo_close(repo);
4079 got_worktree_close(worktree);
4081 const struct got_error *pack_err =
4082 got_repo_pack_fds_close(pack_fds);
4094 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4095 "object1 object2\n", getprogname());
4100 match_line(const char *line, regex_t *regex, size_t nmatch,
4101 regmatch_t *regmatch)
4103 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4106 static struct tog_color *
4107 match_color(struct tog_colors *colors, const char *line)
4109 struct tog_color *tc = NULL;
4111 STAILQ_FOREACH(tc, colors, entry) {
4112 if (match_line(line, &tc->regex, 0, NULL))
4119 static const struct got_error *
4120 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4121 WINDOW *window, int skipcol, regmatch_t *regmatch)
4123 const struct got_error *err = NULL;
4125 wchar_t *wline = NULL;
4126 int rme, rms, n, width, scrollx;
4127 int width0 = 0, width1 = 0, width2 = 0;
4128 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4132 rms = regmatch->rm_so;
4133 rme = regmatch->rm_eo;
4135 err = expand_tab(&exstr, line);
4139 /* Split the line into 3 segments, according to match offsets. */
4140 seg0 = strndup(exstr, rms);
4142 err = got_error_from_errno("strndup");
4145 seg1 = strndup(exstr + rms, rme - rms);
4147 err = got_error_from_errno("strndup");
4150 seg2 = strdup(exstr + rme);
4152 err = got_error_from_errno("strndup");
4156 /* draw up to matched token if we haven't scrolled past it */
4157 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4161 n = MAX(width0 - skipcol, 0);
4164 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4165 wlimit, col_tab_align, 1);
4168 waddwstr(window, &wline[scrollx]);
4178 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4182 wlen = wcslen(wline);
4184 width = wcwidth(wline[i]);
4186 /* should not happen, tabs are expanded */
4187 err = got_error(GOT_ERR_RANGE);
4190 if (width0 + w + width > skipcol)
4195 /* draw (visible part of) matched token (if scrolled into it) */
4196 if (width1 - w > 0) {
4197 wattron(window, A_STANDOUT);
4198 waddwstr(window, &wline[i]);
4199 wattroff(window, A_STANDOUT);
4200 wlimit -= (width1 - w);
4201 *wtotal += (width1 - w);
4205 if (wlimit > 0) { /* draw rest of line */
4207 if (skipcol > width0 + width1) {
4208 err = format_line(&wline, &width2, &scrollx, seg2,
4209 skipcol - (width0 + width1), wlimit,
4213 waddwstr(window, &wline[scrollx]);
4215 err = format_line(&wline, &width2, NULL, seg2, 0,
4216 wlimit, col_tab_align, 1);
4219 waddwstr(window, wline);
4233 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4236 int *eof, *first, *selected;
4238 if (view->type == TOG_VIEW_DIFF) {
4239 struct tog_diff_view_state *s = &view->state.diff;
4241 first = &s->first_displayed_line;
4245 } else if (view->type == TOG_VIEW_HELP) {
4246 struct tog_help_view_state *s = &view->state.help;
4248 first = &s->first_displayed_line;
4252 } else if (view->type == TOG_VIEW_BLAME) {
4253 struct tog_blame_view_state *s = &view->state.blame;
4255 first = &s->first_displayed_line;
4256 selected = &s->selected_line;
4262 /* Center gline in the middle of the page like vi(1). */
4263 if (*lineno < view->gline - (view->nlines - 3) / 2)
4265 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4274 *selected = view->gline <= (view->nlines - 3) / 2 ?
4275 view->gline : (view->nlines - 3) / 2 + 1;
4281 static const struct got_error *
4282 draw_file(struct tog_view *view, const char *header)
4284 struct tog_diff_view_state *s = &view->state.diff;
4285 regmatch_t *regmatch = &view->regmatch;
4286 const struct got_error *err;
4289 size_t linesize = 0;
4293 int max_lines = view->nlines;
4294 int nlines = s->nlines;
4297 s->lineno = s->first_displayed_line - 1;
4298 line_offset = s->lines[s->first_displayed_line - 1].offset;
4299 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4300 return got_error_from_errno("fseek");
4302 werase(view->window);
4304 if (view->gline > s->nlines - 1)
4305 view->gline = s->nlines - 1;
4308 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4309 1 : view->gline - (view->nlines - 3) / 2 :
4310 s->lineno + s->selected_line;
4312 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4313 return got_error_from_errno("asprintf");
4314 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4320 if (view_needs_focus_indication(view))
4321 wstandout(view->window);
4322 waddwstr(view->window, wline);
4325 while (width++ < view->ncols)
4326 waddch(view->window, ' ');
4327 if (view_needs_focus_indication(view))
4328 wstandend(view->window);
4338 while (max_lines > 0 && nprinted < max_lines) {
4339 enum got_diff_line_type linetype;
4342 linelen = getline(&line, &linesize, s->f);
4343 if (linelen == -1) {
4349 return got_ferror(s->f, GOT_ERR_IO);
4352 if (++s->lineno < s->first_displayed_line)
4354 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4356 if (s->lineno == view->hiline)
4359 /* Set view->maxx based on full line length. */
4360 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4366 view->maxx = MAX(view->maxx, width);
4370 linetype = s->lines[s->lineno].type;
4371 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4372 linetype < GOT_DIFF_LINE_CONTEXT)
4373 attr |= COLOR_PAIR(linetype);
4375 wattron(view->window, attr);
4376 if (s->first_displayed_line + nprinted == s->matched_line &&
4377 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4378 err = add_matched_line(&width, line, view->ncols, 0,
4379 view->window, view->x, regmatch);
4386 err = format_line(&wline, &width, &skip, line,
4387 view->x, view->ncols, 0, view->x ? 1 : 0);
4392 waddwstr(view->window, &wline[skip]);
4396 if (s->lineno == view->hiline) {
4397 /* highlight full gline length */
4398 while (width++ < view->ncols)
4399 waddch(view->window, ' ');
4401 if (width <= view->ncols - 1)
4402 waddch(view->window, '\n');
4405 wattroff(view->window, attr);
4406 if (++nprinted == 1)
4407 s->first_displayed_line = s->lineno;
4411 s->last_displayed_line = s->first_displayed_line +
4414 s->last_displayed_line = s->first_displayed_line;
4419 while (nprinted < view->nlines) {
4420 waddch(view->window, '\n');
4424 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4430 wstandout(view->window);
4431 waddwstr(view->window, wline);
4434 wstandend(view->window);
4441 get_datestr(time_t *time, char *datebuf)
4443 struct tm mytm, *tm;
4446 tm = gmtime_r(time, &mytm);
4449 s = asctime_r(tm, datebuf);
4452 p = strchr(s, '\n');
4458 static const struct got_error *
4459 get_changed_paths(struct got_pathlist_head *paths,
4460 struct got_commit_object *commit, struct got_repository *repo)
4462 const struct got_error *err = NULL;
4463 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4464 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4465 struct got_object_qid *qid;
4467 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4469 struct got_commit_object *pcommit;
4470 err = got_object_open_as_commit(&pcommit, repo,
4475 tree_id1 = got_object_id_dup(
4476 got_object_commit_get_tree_id(pcommit));
4477 if (tree_id1 == NULL) {
4478 got_object_commit_close(pcommit);
4479 return got_error_from_errno("got_object_id_dup");
4481 got_object_commit_close(pcommit);
4486 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4491 tree_id2 = got_object_commit_get_tree_id(commit);
4492 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4496 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4497 got_diff_tree_collect_changed_paths, paths, 0);
4500 got_object_tree_close(tree1);
4502 got_object_tree_close(tree2);
4507 static const struct got_error *
4508 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4509 off_t off, uint8_t type)
4511 struct got_diff_line *p;
4513 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4515 return got_error_from_errno("reallocarray");
4517 (*lines)[*nlines].offset = off;
4518 (*lines)[*nlines].type = type;
4524 static const struct got_error *
4525 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4526 struct got_object_id *commit_id, struct got_reflist_head *refs,
4527 struct got_repository *repo, FILE *outfile)
4529 const struct got_error *err = NULL;
4530 char datebuf[26], *datestr;
4531 struct got_commit_object *commit;
4532 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4533 time_t committer_time;
4534 const char *author, *committer;
4535 char *refs_str = NULL;
4536 struct got_pathlist_head changed_paths;
4537 struct got_pathlist_entry *pe;
4541 TAILQ_INIT(&changed_paths);
4544 err = build_refs_str(&refs_str, refs, commit_id, repo);
4549 err = got_object_open_as_commit(&commit, repo, commit_id);
4553 err = got_object_id_str(&id_str, commit_id);
4555 err = got_error_from_errno("got_object_id_str");
4559 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4563 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4564 refs_str ? refs_str : "", refs_str ? ")" : "");
4566 err = got_error_from_errno("fprintf");
4570 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4574 n = fprintf(outfile, "from: %s\n",
4575 got_object_commit_get_author(commit));
4577 err = got_error_from_errno("fprintf");
4581 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4585 committer_time = got_object_commit_get_committer_time(commit);
4586 datestr = get_datestr(&committer_time, datebuf);
4588 n = fprintf(outfile, "date: %s UTC\n", datestr);
4590 err = got_error_from_errno("fprintf");
4594 err = add_line_metadata(lines, nlines, outoff,
4595 GOT_DIFF_LINE_DATE);
4599 author = got_object_commit_get_author(commit);
4600 committer = got_object_commit_get_committer(commit);
4601 if (strcmp(author, committer) != 0) {
4602 n = fprintf(outfile, "via: %s\n", committer);
4604 err = got_error_from_errno("fprintf");
4608 err = add_line_metadata(lines, nlines, outoff,
4609 GOT_DIFF_LINE_AUTHOR);
4613 if (got_object_commit_get_nparents(commit) > 1) {
4614 const struct got_object_id_queue *parent_ids;
4615 struct got_object_qid *qid;
4617 parent_ids = got_object_commit_get_parent_ids(commit);
4618 STAILQ_FOREACH(qid, parent_ids, entry) {
4619 err = got_object_id_str(&id_str, &qid->id);
4622 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4624 err = got_error_from_errno("fprintf");
4628 err = add_line_metadata(lines, nlines, outoff,
4629 GOT_DIFF_LINE_META);
4637 err = got_object_commit_get_logmsg(&logmsg, commit);
4641 while ((line = strsep(&s, "\n")) != NULL) {
4642 n = fprintf(outfile, "%s\n", line);
4644 err = got_error_from_errno("fprintf");
4648 err = add_line_metadata(lines, nlines, outoff,
4649 GOT_DIFF_LINE_LOGMSG);
4654 err = get_changed_paths(&changed_paths, commit, repo);
4657 TAILQ_FOREACH(pe, &changed_paths, entry) {
4658 struct got_diff_changed_path *cp = pe->data;
4659 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4661 err = got_error_from_errno("fprintf");
4665 err = add_line_metadata(lines, nlines, outoff,
4666 GOT_DIFF_LINE_CHANGES);
4669 free((char *)pe->path);
4673 fputc('\n', outfile);
4675 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4677 got_pathlist_free(&changed_paths);
4681 got_object_commit_close(commit);
4690 static const struct got_error *
4691 create_diff(struct tog_diff_view_state *s)
4693 const struct got_error *err = NULL;
4698 s->lines = malloc(sizeof(*s->lines));
4699 if (s->lines == NULL)
4700 return got_error_from_errno("malloc");
4705 err = got_error_from_errno("got_opentemp");
4708 if (s->f && fclose(s->f) == EOF) {
4709 err = got_error_from_errno("fclose");
4715 err = got_object_get_type(&obj_type, s->repo, s->id1);
4717 err = got_object_get_type(&obj_type, s->repo, s->id2);
4722 case GOT_OBJ_TYPE_BLOB:
4723 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4724 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4725 s->label1, s->label2, tog_diff_algo, s->diff_context,
4726 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4728 case GOT_OBJ_TYPE_TREE:
4729 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4730 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4731 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4732 s->force_text_diff, s->repo, s->f);
4734 case GOT_OBJ_TYPE_COMMIT: {
4735 const struct got_object_id_queue *parent_ids;
4736 struct got_object_qid *pid;
4737 struct got_commit_object *commit2;
4738 struct got_reflist_head *refs;
4740 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4743 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4744 /* Show commit info if we're diffing to a parent/root commit. */
4745 if (s->id1 == NULL) {
4746 err = write_commit_info(&s->lines, &s->nlines, s->id2,
4747 refs, s->repo, s->f);
4751 parent_ids = got_object_commit_get_parent_ids(commit2);
4752 STAILQ_FOREACH(pid, parent_ids, entry) {
4753 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4754 err = write_commit_info(&s->lines,
4755 &s->nlines, s->id2, refs, s->repo,
4763 got_object_commit_close(commit2);
4765 err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4766 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4767 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4768 s->force_text_diff, s->repo, s->f);
4772 err = got_error(GOT_ERR_OBJ_TYPE);
4776 if (s->f && fflush(s->f) != 0 && err == NULL)
4777 err = got_error_from_errno("fflush");
4782 diff_view_indicate_progress(struct tog_view *view)
4784 mvwaddstr(view->window, 0, 0, "diffing...");
4789 static const struct got_error *
4790 search_start_diff_view(struct tog_view *view)
4792 struct tog_diff_view_state *s = &view->state.diff;
4794 s->matched_line = 0;
4799 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4800 size_t *nlines, int **first, int **last, int **match, int **selected)
4802 struct tog_diff_view_state *s = &view->state.diff;
4805 *nlines = s->nlines;
4806 *line_offsets = NULL;
4807 *match = &s->matched_line;
4808 *first = &s->first_displayed_line;
4809 *last = &s->last_displayed_line;
4810 *selected = &s->selected_line;
4813 static const struct got_error *
4814 search_next_view_match(struct tog_view *view)
4816 const struct got_error *err = NULL;
4820 size_t linesize = 0;
4822 off_t *line_offsets;
4824 int *first, *last, *match, *selected;
4826 if (!view->search_setup)
4827 return got_error_msg(GOT_ERR_NOT_IMPL,
4828 "view search not supported");
4829 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4832 if (!view->searching) {
4833 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4838 if (view->searching == TOG_SEARCH_FORWARD)
4839 lineno = *match + 1;
4841 lineno = *match - 1;
4843 lineno = *first - 1 + *selected;
4848 if (lineno <= 0 || lineno > nlines) {
4850 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4854 if (view->searching == TOG_SEARCH_FORWARD)
4860 offset = view->type == TOG_VIEW_DIFF ?
4861 view->state.diff.lines[lineno - 1].offset :
4862 line_offsets[lineno - 1];
4863 if (fseeko(f, offset, SEEK_SET) != 0) {
4865 return got_error_from_errno("fseeko");
4867 linelen = getline(&line, &linesize, f);
4868 if (linelen != -1) {
4870 err = expand_tab(&exstr, line);
4873 if (match_line(exstr, &view->regex, 1,
4875 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4882 if (view->searching == TOG_SEARCH_FORWARD)
4897 static const struct got_error *
4898 close_diff_view(struct tog_view *view)
4900 const struct got_error *err = NULL;
4901 struct tog_diff_view_state *s = &view->state.diff;
4907 if (s->f && fclose(s->f) == EOF)
4908 err = got_error_from_errno("fclose");
4910 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4911 err = got_error_from_errno("fclose");
4913 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4914 err = got_error_from_errno("fclose");
4916 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4917 err = got_error_from_errno("close");
4919 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4920 err = got_error_from_errno("close");
4928 static const struct got_error *
4929 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4930 struct got_object_id *id2, const char *label1, const char *label2,
4931 int diff_context, int ignore_whitespace, int force_text_diff,
4932 struct tog_view *parent_view, struct got_repository *repo)
4934 const struct got_error *err;
4935 struct tog_diff_view_state *s = &view->state.diff;
4937 memset(s, 0, sizeof(*s));
4941 if (id1 != NULL && id2 != NULL) {
4943 err = got_object_get_type(&type1, repo, id1);
4946 err = got_object_get_type(&type2, repo, id2);
4951 return got_error(GOT_ERR_OBJ_TYPE);
4953 s->first_displayed_line = 1;
4954 s->last_displayed_line = view->nlines;
4955 s->selected_line = 1;
4963 s->id1 = got_object_id_dup(id1);
4965 return got_error_from_errno("got_object_id_dup");
4969 s->id2 = got_object_id_dup(id2);
4970 if (s->id2 == NULL) {
4971 err = got_error_from_errno("got_object_id_dup");
4975 s->f1 = got_opentemp();
4976 if (s->f1 == NULL) {
4977 err = got_error_from_errno("got_opentemp");
4981 s->f2 = got_opentemp();
4982 if (s->f2 == NULL) {
4983 err = got_error_from_errno("got_opentemp");
4987 s->fd1 = got_opentempfd();
4989 err = got_error_from_errno("got_opentempfd");
4993 s->fd2 = got_opentempfd();
4995 err = got_error_from_errno("got_opentempfd");
4999 s->first_displayed_line = 1;
5000 s->last_displayed_line = view->nlines;
5001 s->diff_context = diff_context;
5002 s->ignore_whitespace = ignore_whitespace;
5003 s->force_text_diff = force_text_diff;
5004 s->parent_view = parent_view;
5007 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5010 rc = init_pair(GOT_DIFF_LINE_MINUS,
5011 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5013 rc = init_pair(GOT_DIFF_LINE_PLUS,
5014 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5016 rc = init_pair(GOT_DIFF_LINE_HUNK,
5017 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5019 rc = init_pair(GOT_DIFF_LINE_META,
5020 get_color_value("TOG_COLOR_DIFF_META"), -1);
5022 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5023 get_color_value("TOG_COLOR_DIFF_META"), -1);
5025 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5026 get_color_value("TOG_COLOR_DIFF_META"), -1);
5028 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5029 get_color_value("TOG_COLOR_DIFF_META"), -1);
5031 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5032 get_color_value("TOG_COLOR_AUTHOR"), -1);
5034 rc = init_pair(GOT_DIFF_LINE_DATE,
5035 get_color_value("TOG_COLOR_DATE"), -1);
5037 err = got_error(GOT_ERR_RANGE);
5042 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5043 view_is_splitscreen(view))
5044 show_log_view(parent_view); /* draw border */
5045 diff_view_indicate_progress(view);
5047 err = create_diff(s);
5049 view->show = show_diff_view;
5050 view->input = input_diff_view;
5051 view->reset = reset_diff_view;
5052 view->close = close_diff_view;
5053 view->search_start = search_start_diff_view;
5054 view->search_setup = search_setup_diff_view;
5055 view->search_next = search_next_view_match;
5058 close_diff_view(view);
5062 static const struct got_error *
5063 show_diff_view(struct tog_view *view)
5065 const struct got_error *err;
5066 struct tog_diff_view_state *s = &view->state.diff;
5067 char *id_str1 = NULL, *id_str2, *header;
5068 const char *label1, *label2;
5071 err = got_object_id_str(&id_str1, s->id1);
5074 label1 = s->label1 ? s->label1 : id_str1;
5076 label1 = "/dev/null";
5078 err = got_object_id_str(&id_str2, s->id2);
5081 label2 = s->label2 ? s->label2 : id_str2;
5083 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5084 err = got_error_from_errno("asprintf");
5092 err = draw_file(view, header);
5097 static const struct got_error *
5098 set_selected_commit(struct tog_diff_view_state *s,
5099 struct commit_queue_entry *entry)
5101 const struct got_error *err;
5102 const struct got_object_id_queue *parent_ids;
5103 struct got_commit_object *selected_commit;
5104 struct got_object_qid *pid;
5107 s->id2 = got_object_id_dup(entry->id);
5109 return got_error_from_errno("got_object_id_dup");
5111 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5114 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5116 pid = STAILQ_FIRST(parent_ids);
5117 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5118 got_object_commit_close(selected_commit);
5122 static const struct got_error *
5123 reset_diff_view(struct tog_view *view)
5125 struct tog_diff_view_state *s = &view->state.diff;
5128 wclear(view->window);
5129 s->first_displayed_line = 1;
5130 s->last_displayed_line = view->nlines;
5131 s->matched_line = 0;
5132 diff_view_indicate_progress(view);
5133 return create_diff(s);
5137 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5141 i = start = s->first_displayed_line - 1;
5143 while (s->lines[i].type != type) {
5147 return; /* do nothing, requested type not in file */
5150 s->selected_line = 1;
5151 s->first_displayed_line = i;
5155 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5159 i = start = s->first_displayed_line + 1;
5161 while (s->lines[i].type != type) {
5162 if (i == s->nlines - 1)
5165 return; /* do nothing, requested type not in file */
5168 s->selected_line = 1;
5169 s->first_displayed_line = i;
5172 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5174 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5177 static const struct got_error *
5178 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5180 const struct got_error *err = NULL;
5181 struct tog_diff_view_state *s = &view->state.diff;
5182 struct tog_log_view_state *ls;
5183 struct commit_queue_entry *old_selected_entry;
5185 size_t linesize = 0;
5187 int i, nscroll = view->nlines - 1, up = 0;
5189 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5196 view->x = MAX(view->maxx - view->ncols / 3, 0);
5201 if (view->x + view->ncols / 3 < view->maxx)
5202 view->x += 2; /* move two columns right */
5208 view->x -= MIN(view->x, 2); /* move two columns back */
5215 s->force_text_diff = !s->force_text_diff;
5217 s->ignore_whitespace = !s->ignore_whitespace;
5218 err = reset_diff_view(view);
5222 s->first_displayed_line = 1;
5231 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5237 if (s->first_displayed_line > 1)
5238 s->first_displayed_line--;
5249 if (s->first_displayed_line == 1) {
5254 while (i++ < nscroll && s->first_displayed_line > 1)
5255 s->first_displayed_line--;
5261 s->first_displayed_line++;
5278 while (!s->eof && i++ < nscroll) {
5279 linelen = getline(&line, &linesize, s->f);
5280 s->first_displayed_line++;
5281 if (linelen == -1) {
5285 err = got_ferror(s->f, GOT_ERR_IO);
5292 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5295 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5298 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5301 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5304 if (s->diff_context > 0) {
5306 s->matched_line = 0;
5307 diff_view_indicate_progress(view);
5308 err = create_diff(s);
5309 if (s->first_displayed_line + view->nlines - 1 >
5311 s->first_displayed_line = 1;
5312 s->last_displayed_line = view->nlines;
5318 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5320 s->matched_line = 0;
5321 diff_view_indicate_progress(view);
5322 err = create_diff(s);
5334 if (s->parent_view == NULL) {
5338 s->parent_view->count = view->count;
5340 if (s->parent_view->type == TOG_VIEW_LOG) {
5341 ls = &s->parent_view->state.log;
5342 old_selected_entry = ls->selected_entry;
5344 err = input_log_view(NULL, s->parent_view,
5345 up ? KEY_UP : KEY_DOWN);
5348 view->count = s->parent_view->count;
5350 if (old_selected_entry == ls->selected_entry)
5353 err = set_selected_commit(s, ls->selected_entry);
5356 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5357 struct tog_blame_view_state *bs;
5358 struct got_object_id *id, *prev_id;
5360 bs = &s->parent_view->state.blame;
5361 prev_id = get_annotation_for_line(bs->blame.lines,
5362 bs->blame.nlines, bs->last_diffed_line);
5364 err = input_blame_view(&view, s->parent_view,
5365 up ? KEY_UP : KEY_DOWN);
5368 view->count = s->parent_view->count;
5370 if (prev_id == NULL)
5372 id = get_selected_commit_id(bs->blame.lines,
5373 bs->blame.nlines, bs->first_displayed_line,
5378 if (!got_object_id_cmp(prev_id, id))
5381 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5385 s->first_displayed_line = 1;
5386 s->last_displayed_line = view->nlines;
5387 s->matched_line = 0;
5390 diff_view_indicate_progress(view);
5391 err = create_diff(s);
5401 static const struct got_error *
5402 cmd_diff(int argc, char *argv[])
5404 const struct got_error *error = NULL;
5405 struct got_repository *repo = NULL;
5406 struct got_worktree *worktree = NULL;
5407 struct got_object_id *id1 = NULL, *id2 = NULL;
5408 char *repo_path = NULL, *cwd = NULL;
5409 char *id_str1 = NULL, *id_str2 = NULL;
5410 char *label1 = NULL, *label2 = NULL;
5411 int diff_context = 3, ignore_whitespace = 0;
5412 int ch, force_text_diff = 0;
5414 struct tog_view *view;
5415 int *pack_fds = NULL;
5417 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5420 force_text_diff = 1;
5423 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5426 errx(1, "number of context lines is %s: %s",
5430 repo_path = realpath(optarg, NULL);
5431 if (repo_path == NULL)
5432 return got_error_from_errno2("realpath",
5434 got_path_strip_trailing_slashes(repo_path);
5437 ignore_whitespace = 1;
5449 usage_diff(); /* TODO show local worktree changes */
5450 } else if (argc == 2) {
5456 error = got_repo_pack_fds_open(&pack_fds);
5460 if (repo_path == NULL) {
5461 cwd = getcwd(NULL, 0);
5463 return got_error_from_errno("getcwd");
5464 error = got_worktree_open(&worktree, cwd);
5465 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5469 strdup(got_worktree_get_repo_path(worktree));
5471 repo_path = strdup(cwd);
5472 if (repo_path == NULL) {
5473 error = got_error_from_errno("strdup");
5478 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5484 error = apply_unveil(got_repo_get_path(repo), NULL);
5488 error = tog_load_refs(repo, 0);
5492 error = got_repo_match_object_id(&id1, &label1, id_str1,
5493 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5497 error = got_repo_match_object_id(&id2, &label2, id_str2,
5498 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5502 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5504 error = got_error_from_errno("view_open");
5507 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5508 ignore_whitespace, force_text_diff, NULL, repo);
5511 error = view_loop(view);
5518 const struct got_error *close_err = got_repo_close(repo);
5523 got_worktree_close(worktree);
5525 const struct got_error *pack_err =
5526 got_repo_pack_fds_close(pack_fds);
5539 "usage: %s blame [-c commit] [-r repository-path] path\n",
5544 struct tog_blame_line {
5546 struct got_object_id *id;
5549 static const struct got_error *
5550 draw_blame(struct tog_view *view)
5552 struct tog_blame_view_state *s = &view->state.blame;
5553 struct tog_blame *blame = &s->blame;
5554 regmatch_t *regmatch = &view->regmatch;
5555 const struct got_error *err;
5556 int lineno = 0, nprinted = 0;
5558 size_t linesize = 0;
5562 struct tog_blame_line *blame_line;
5563 struct got_object_id *prev_id = NULL;
5565 struct tog_color *tc;
5567 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5572 werase(view->window);
5574 if (asprintf(&line, "commit %s", id_str) == -1) {
5575 err = got_error_from_errno("asprintf");
5580 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5585 if (view_needs_focus_indication(view))
5586 wstandout(view->window);
5587 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5589 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5590 waddwstr(view->window, wline);
5591 while (width++ < view->ncols)
5592 waddch(view->window, ' ');
5594 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5595 if (view_needs_focus_indication(view))
5596 wstandend(view->window);
5600 if (view->gline > blame->nlines)
5601 view->gline = blame->nlines;
5603 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5604 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5605 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5607 return got_error_from_errno("asprintf");
5610 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5615 waddwstr(view->window, wline);
5618 if (width < view->ncols - 1)
5619 waddch(view->window, '\n');
5623 while (nprinted < view->nlines - 2) {
5624 linelen = getline(&line, &linesize, blame->f);
5625 if (linelen == -1) {
5626 if (feof(blame->f)) {
5631 return got_ferror(blame->f, GOT_ERR_IO);
5633 if (++lineno < s->first_displayed_line)
5635 if (view->gline && !gotoline(view, &lineno, &nprinted))
5638 /* Set view->maxx based on full line length. */
5639 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5646 view->maxx = MAX(view->maxx, width);
5648 if (nprinted == s->selected_line - 1)
5649 wstandout(view->window);
5651 if (blame->nlines > 0) {
5652 blame_line = &blame->lines[lineno - 1];
5653 if (blame_line->annotated && prev_id &&
5654 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5655 !(nprinted == s->selected_line - 1)) {
5656 waddstr(view->window, " ");
5657 } else if (blame_line->annotated) {
5659 err = got_object_id_str(&id_str,
5665 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5667 wattr_on(view->window,
5668 COLOR_PAIR(tc->colorpair), NULL);
5669 wprintw(view->window, "%.8s", id_str);
5671 wattr_off(view->window,
5672 COLOR_PAIR(tc->colorpair), NULL);
5674 prev_id = blame_line->id;
5676 waddstr(view->window, "........");
5680 waddstr(view->window, "........");
5684 if (nprinted == s->selected_line - 1)
5685 wstandend(view->window);
5686 waddstr(view->window, " ");
5688 if (view->ncols <= 9) {
5690 } else if (s->first_displayed_line + nprinted ==
5692 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5693 err = add_matched_line(&width, line, view->ncols - 9, 9,
5694 view->window, view->x, regmatch);
5702 err = format_line(&wline, &width, &skip, line,
5703 view->x, view->ncols - 9, 9, 1);
5708 waddwstr(view->window, &wline[skip]);
5714 if (width <= view->ncols - 1)
5715 waddch(view->window, '\n');
5716 if (++nprinted == 1)
5717 s->first_displayed_line = lineno;
5720 s->last_displayed_line = lineno;
5727 static const struct got_error *
5728 blame_cb(void *arg, int nlines, int lineno,
5729 struct got_commit_object *commit, struct got_object_id *id)
5731 const struct got_error *err = NULL;
5732 struct tog_blame_cb_args *a = arg;
5733 struct tog_blame_line *line;
5736 if (nlines != a->nlines ||
5737 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5738 return got_error(GOT_ERR_RANGE);
5740 errcode = pthread_mutex_lock(&tog_mutex);
5742 return got_error_set_errno(errcode, "pthread_mutex_lock");
5744 if (*a->quit) { /* user has quit the blame view */
5745 err = got_error(GOT_ERR_ITER_COMPLETED);
5750 goto done; /* no change in this commit */
5752 line = &a->lines[lineno - 1];
5753 if (line->annotated)
5756 line->id = got_object_id_dup(id);
5757 if (line->id == NULL) {
5758 err = got_error_from_errno("got_object_id_dup");
5761 line->annotated = 1;
5763 errcode = pthread_mutex_unlock(&tog_mutex);
5765 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5770 blame_thread(void *arg)
5772 const struct got_error *err, *close_err;
5773 struct tog_blame_thread_args *ta = arg;
5774 struct tog_blame_cb_args *a = ta->cb_args;
5775 int errcode, fd1 = -1, fd2 = -1;
5776 FILE *f1 = NULL, *f2 = NULL;
5778 fd1 = got_opentempfd();
5780 return (void *)got_error_from_errno("got_opentempfd");
5782 fd2 = got_opentempfd();
5784 err = got_error_from_errno("got_opentempfd");
5788 f1 = got_opentemp();
5790 err = (void *)got_error_from_errno("got_opentemp");
5793 f2 = got_opentemp();
5795 err = (void *)got_error_from_errno("got_opentemp");
5799 err = block_signals_used_by_main_thread();
5803 err = got_blame(ta->path, a->commit_id, ta->repo,
5804 tog_diff_algo, blame_cb, ta->cb_args,
5805 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5806 if (err && err->code == GOT_ERR_CANCELLED)
5809 errcode = pthread_mutex_lock(&tog_mutex);
5811 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5815 close_err = got_repo_close(ta->repo);
5821 errcode = pthread_mutex_unlock(&tog_mutex);
5822 if (errcode && err == NULL)
5823 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5826 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5827 err = got_error_from_errno("close");
5828 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5829 err = got_error_from_errno("close");
5830 if (f1 && fclose(f1) == EOF && err == NULL)
5831 err = got_error_from_errno("fclose");
5832 if (f2 && fclose(f2) == EOF && err == NULL)
5833 err = got_error_from_errno("fclose");
5838 static struct got_object_id *
5839 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5840 int first_displayed_line, int selected_line)
5842 struct tog_blame_line *line;
5847 line = &lines[first_displayed_line - 1 + selected_line - 1];
5848 if (!line->annotated)
5854 static struct got_object_id *
5855 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5858 struct tog_blame_line *line;
5860 if (nlines <= 0 || lineno >= nlines)
5863 line = &lines[lineno - 1];
5864 if (!line->annotated)
5870 static const struct got_error *
5871 stop_blame(struct tog_blame *blame)
5873 const struct got_error *err = NULL;
5876 if (blame->thread) {
5878 errcode = pthread_mutex_unlock(&tog_mutex);
5880 return got_error_set_errno(errcode,
5881 "pthread_mutex_unlock");
5882 errcode = pthread_join(blame->thread, (void **)&err);
5884 return got_error_set_errno(errcode, "pthread_join");
5885 errcode = pthread_mutex_lock(&tog_mutex);
5887 return got_error_set_errno(errcode,
5888 "pthread_mutex_lock");
5889 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5891 blame->thread = NULL;
5893 if (blame->thread_args.repo) {
5894 const struct got_error *close_err;
5895 close_err = got_repo_close(blame->thread_args.repo);
5898 blame->thread_args.repo = NULL;
5901 if (fclose(blame->f) == EOF && err == NULL)
5902 err = got_error_from_errno("fclose");
5906 for (i = 0; i < blame->nlines; i++)
5907 free(blame->lines[i].id);
5909 blame->lines = NULL;
5911 free(blame->cb_args.commit_id);
5912 blame->cb_args.commit_id = NULL;
5913 if (blame->pack_fds) {
5914 const struct got_error *pack_err =
5915 got_repo_pack_fds_close(blame->pack_fds);
5918 blame->pack_fds = NULL;
5923 static const struct got_error *
5924 cancel_blame_view(void *arg)
5926 const struct got_error *err = NULL;
5930 errcode = pthread_mutex_lock(&tog_mutex);
5932 return got_error_set_errno(errcode,
5933 "pthread_mutex_unlock");
5936 err = got_error(GOT_ERR_CANCELLED);
5938 errcode = pthread_mutex_unlock(&tog_mutex);
5940 return got_error_set_errno(errcode,
5941 "pthread_mutex_lock");
5946 static const struct got_error *
5947 run_blame(struct tog_view *view)
5949 struct tog_blame_view_state *s = &view->state.blame;
5950 struct tog_blame *blame = &s->blame;
5951 const struct got_error *err = NULL;
5952 struct got_commit_object *commit = NULL;
5953 struct got_blob_object *blob = NULL;
5954 struct got_repository *thread_repo = NULL;
5955 struct got_object_id *obj_id = NULL;
5956 int obj_type, fd = -1;
5957 int *pack_fds = NULL;
5959 err = got_object_open_as_commit(&commit, s->repo,
5960 &s->blamed_commit->id);
5964 fd = got_opentempfd();
5966 err = got_error_from_errno("got_opentempfd");
5970 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5974 err = got_object_get_type(&obj_type, s->repo, obj_id);
5978 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5979 err = got_error(GOT_ERR_OBJ_TYPE);
5983 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5986 blame->f = got_opentemp();
5987 if (blame->f == NULL) {
5988 err = got_error_from_errno("got_opentemp");
5991 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5992 &blame->line_offsets, blame->f, blob);
5995 if (blame->nlines == 0) {
5996 s->blame_complete = 1;
6000 /* Don't include \n at EOF in the blame line count. */
6001 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6004 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6005 if (blame->lines == NULL) {
6006 err = got_error_from_errno("calloc");
6010 err = got_repo_pack_fds_open(&pack_fds);
6013 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6018 blame->pack_fds = pack_fds;
6019 blame->cb_args.view = view;
6020 blame->cb_args.lines = blame->lines;
6021 blame->cb_args.nlines = blame->nlines;
6022 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6023 if (blame->cb_args.commit_id == NULL) {
6024 err = got_error_from_errno("got_object_id_dup");
6027 blame->cb_args.quit = &s->done;
6029 blame->thread_args.path = s->path;
6030 blame->thread_args.repo = thread_repo;
6031 blame->thread_args.cb_args = &blame->cb_args;
6032 blame->thread_args.complete = &s->blame_complete;
6033 blame->thread_args.cancel_cb = cancel_blame_view;
6034 blame->thread_args.cancel_arg = &s->done;
6035 s->blame_complete = 0;
6037 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6038 s->first_displayed_line = 1;
6039 s->last_displayed_line = view->nlines;
6040 s->selected_line = 1;
6042 s->matched_line = 0;
6046 got_object_commit_close(commit);
6047 if (fd != -1 && close(fd) == -1 && err == NULL)
6048 err = got_error_from_errno("close");
6050 got_object_blob_close(blob);
6057 static const struct got_error *
6058 open_blame_view(struct tog_view *view, char *path,
6059 struct got_object_id *commit_id, struct got_repository *repo)
6061 const struct got_error *err = NULL;
6062 struct tog_blame_view_state *s = &view->state.blame;
6064 STAILQ_INIT(&s->blamed_commits);
6066 s->path = strdup(path);
6067 if (s->path == NULL)
6068 return got_error_from_errno("strdup");
6070 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6076 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6077 s->first_displayed_line = 1;
6078 s->last_displayed_line = view->nlines;
6079 s->selected_line = 1;
6080 s->blame_complete = 0;
6082 s->commit_id = commit_id;
6083 memset(&s->blame, 0, sizeof(s->blame));
6085 STAILQ_INIT(&s->colors);
6086 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6087 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6088 get_color_value("TOG_COLOR_COMMIT"));
6093 view->show = show_blame_view;
6094 view->input = input_blame_view;
6095 view->reset = reset_blame_view;
6096 view->close = close_blame_view;
6097 view->search_start = search_start_blame_view;
6098 view->search_setup = search_setup_blame_view;
6099 view->search_next = search_next_view_match;
6101 return run_blame(view);
6104 static const struct got_error *
6105 close_blame_view(struct tog_view *view)
6107 const struct got_error *err = NULL;
6108 struct tog_blame_view_state *s = &view->state.blame;
6110 if (s->blame.thread)
6111 err = stop_blame(&s->blame);
6113 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6114 struct got_object_qid *blamed_commit;
6115 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6116 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6117 got_object_qid_free(blamed_commit);
6121 free_colors(&s->colors);
6125 static const struct got_error *
6126 search_start_blame_view(struct tog_view *view)
6128 struct tog_blame_view_state *s = &view->state.blame;
6130 s->matched_line = 0;
6135 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6136 size_t *nlines, int **first, int **last, int **match, int **selected)
6138 struct tog_blame_view_state *s = &view->state.blame;
6141 *nlines = s->blame.nlines;
6142 *line_offsets = s->blame.line_offsets;
6143 *match = &s->matched_line;
6144 *first = &s->first_displayed_line;
6145 *last = &s->last_displayed_line;
6146 *selected = &s->selected_line;
6149 static const struct got_error *
6150 show_blame_view(struct tog_view *view)
6152 const struct got_error *err = NULL;
6153 struct tog_blame_view_state *s = &view->state.blame;
6156 if (s->blame.thread == NULL && !s->blame_complete) {
6157 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6158 &s->blame.thread_args);
6160 return got_error_set_errno(errcode, "pthread_create");
6162 halfdelay(1); /* fast refresh while annotating */
6165 if (s->blame_complete)
6166 halfdelay(10); /* disable fast refresh */
6168 err = draw_blame(view);
6174 static const struct got_error *
6175 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6176 struct got_repository *repo, struct got_object_id *id)
6178 struct tog_view *log_view;
6179 const struct got_error *err = NULL;
6183 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6184 if (log_view == NULL)
6185 return got_error_from_errno("view_open");
6187 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6189 view_close(log_view);
6191 *new_view = log_view;
6196 static const struct got_error *
6197 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6199 const struct got_error *err = NULL, *thread_err = NULL;
6200 struct tog_view *diff_view;
6201 struct tog_blame_view_state *s = &view->state.blame;
6202 int eos, nscroll, begin_y = 0, begin_x = 0;
6204 eos = nscroll = view->nlines - 2;
6205 if (view_is_hsplit_top(view))
6213 view->x = MAX(view->maxx - view->ncols / 3, 0);
6218 if (view->x + view->ncols / 3 < view->maxx)
6219 view->x += 2; /* move two columns right */
6225 view->x -= MIN(view->x, 2); /* move two columns back */
6234 s->selected_line = 1;
6235 s->first_displayed_line = 1;
6240 if (s->blame.nlines < eos) {
6241 s->selected_line = s->blame.nlines;
6242 s->first_displayed_line = 1;
6244 s->selected_line = eos;
6245 s->first_displayed_line = s->blame.nlines - (eos - 1);
6252 if (s->selected_line > 1)
6254 else if (s->selected_line == 1 &&
6255 s->first_displayed_line > 1)
6256 s->first_displayed_line--;
6267 if (s->first_displayed_line == 1) {
6268 if (view->count > 1)
6270 s->selected_line = MAX(1, s->selected_line - nscroll);
6274 if (s->first_displayed_line > nscroll)
6275 s->first_displayed_line -= nscroll;
6277 s->first_displayed_line = 1;
6282 if (s->selected_line < eos && s->first_displayed_line +
6283 s->selected_line <= s->blame.nlines)
6285 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6286 s->first_displayed_line++;
6292 struct got_object_id *id = NULL;
6295 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6296 s->first_displayed_line, s->selected_line);
6300 struct got_commit_object *commit, *pcommit;
6301 struct got_object_qid *pid;
6302 struct got_object_id *blob_id = NULL;
6304 err = got_object_open_as_commit(&commit,
6309 got_object_commit_get_parent_ids(commit));
6311 got_object_commit_close(commit);
6314 /* Check if path history ends here. */
6315 err = got_object_open_as_commit(&pcommit,
6319 err = got_object_id_by_path(&blob_id, s->repo,
6321 got_object_commit_close(pcommit);
6323 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6325 got_object_commit_close(commit);
6328 err = got_object_get_type(&obj_type, s->repo,
6331 /* Can't blame non-blob type objects. */
6332 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6333 got_object_commit_close(commit);
6336 err = got_object_qid_alloc(&s->blamed_commit,
6338 got_object_commit_close(commit);
6340 if (got_object_id_cmp(id,
6341 &s->blamed_commit->id) == 0)
6343 err = got_object_qid_alloc(&s->blamed_commit,
6349 thread_err = stop_blame(&s->blame);
6353 STAILQ_INSERT_HEAD(&s->blamed_commits,
6354 s->blamed_commit, entry);
6355 err = run_blame(view);
6361 struct got_object_qid *first;
6364 first = STAILQ_FIRST(&s->blamed_commits);
6365 if (!got_object_id_cmp(&first->id, s->commit_id))
6368 thread_err = stop_blame(&s->blame);
6372 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6373 got_object_qid_free(s->blamed_commit);
6375 STAILQ_FIRST(&s->blamed_commits);
6376 err = run_blame(view);
6383 s->id_to_log = get_selected_commit_id(s->blame.lines,
6384 s->blame.nlines, s->first_displayed_line, s->selected_line);
6386 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6390 struct got_object_id *id = NULL;
6391 struct got_object_qid *pid;
6392 struct got_commit_object *commit = NULL;
6395 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6396 s->first_displayed_line, s->selected_line);
6399 err = got_object_open_as_commit(&commit, s->repo, id);
6402 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6404 /* traversed from diff view, release diff resources */
6405 err = close_diff_view(*new_view);
6408 diff_view = *new_view;
6410 if (view_is_parent_view(view))
6411 view_get_split(view, &begin_y, &begin_x);
6413 diff_view = view_open(0, 0, begin_y, begin_x,
6415 if (diff_view == NULL) {
6416 got_object_commit_close(commit);
6417 err = got_error_from_errno("view_open");
6421 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6422 id, NULL, NULL, 3, 0, 0, view, s->repo);
6423 got_object_commit_close(commit);
6425 view_close(diff_view);
6428 s->last_diffed_line = s->first_displayed_line - 1 +
6431 break; /* still open from active diff view */
6432 if (view_is_parent_view(view) &&
6433 view->mode == TOG_VIEW_SPLIT_HRZN) {
6434 err = view_init_hsplit(view, begin_y);
6440 diff_view->focussed = 1;
6441 diff_view->mode = view->mode;
6442 diff_view->nlines = view->lines - begin_y;
6443 if (view_is_parent_view(view)) {
6444 view_transfer_size(diff_view, view);
6445 err = view_close_child(view);
6448 err = view_set_child(view, diff_view);
6451 view->focus_child = 1;
6453 *new_view = diff_view;
6466 if (s->last_displayed_line >= s->blame.nlines &&
6467 s->selected_line >= MIN(s->blame.nlines,
6468 view->nlines - 2)) {
6472 if (s->last_displayed_line >= s->blame.nlines &&
6473 s->selected_line < view->nlines - 2) {
6475 MIN(nscroll, s->last_displayed_line -
6476 s->first_displayed_line - s->selected_line + 1);
6478 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6479 s->first_displayed_line += nscroll;
6481 s->first_displayed_line =
6482 s->blame.nlines - (view->nlines - 3);
6485 if (s->selected_line > view->nlines - 2) {
6486 s->selected_line = MIN(s->blame.nlines,
6494 return thread_err ? thread_err : err;
6497 static const struct got_error *
6498 reset_blame_view(struct tog_view *view)
6500 const struct got_error *err;
6501 struct tog_blame_view_state *s = &view->state.blame;
6505 err = stop_blame(&s->blame);
6509 return run_blame(view);
6512 static const struct got_error *
6513 cmd_blame(int argc, char *argv[])
6515 const struct got_error *error;
6516 struct got_repository *repo = NULL;
6517 struct got_worktree *worktree = NULL;
6518 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6519 char *link_target = NULL;
6520 struct got_object_id *commit_id = NULL;
6521 struct got_commit_object *commit = NULL;
6522 char *commit_id_str = NULL;
6524 struct tog_view *view;
6525 int *pack_fds = NULL;
6527 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6530 commit_id_str = optarg;
6533 repo_path = realpath(optarg, NULL);
6534 if (repo_path == NULL)
6535 return got_error_from_errno2("realpath",
6550 error = got_repo_pack_fds_open(&pack_fds);
6554 if (repo_path == NULL) {
6555 cwd = getcwd(NULL, 0);
6557 return got_error_from_errno("getcwd");
6558 error = got_worktree_open(&worktree, cwd);
6559 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6563 strdup(got_worktree_get_repo_path(worktree));
6565 repo_path = strdup(cwd);
6566 if (repo_path == NULL) {
6567 error = got_error_from_errno("strdup");
6572 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6576 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6583 error = apply_unveil(got_repo_get_path(repo), NULL);
6587 error = tog_load_refs(repo, 0);
6591 if (commit_id_str == NULL) {
6592 struct got_reference *head_ref;
6593 error = got_ref_open(&head_ref, repo, worktree ?
6594 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6597 error = got_ref_resolve(&commit_id, repo, head_ref);
6598 got_ref_close(head_ref);
6600 error = got_repo_match_object_id(&commit_id, NULL,
6601 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6606 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6608 error = got_error_from_errno("view_open");
6612 error = got_object_open_as_commit(&commit, repo, commit_id);
6616 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6621 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6626 /* Release work tree lock. */
6627 got_worktree_close(worktree);
6630 error = view_loop(view);
6638 got_object_commit_close(commit);
6640 got_worktree_close(worktree);
6642 const struct got_error *close_err = got_repo_close(repo);
6647 const struct got_error *pack_err =
6648 got_repo_pack_fds_close(pack_fds);
6656 static const struct got_error *
6657 draw_tree_entries(struct tog_view *view, const char *parent_path)
6659 struct tog_tree_view_state *s = &view->state.tree;
6660 const struct got_error *err = NULL;
6661 struct got_tree_entry *te;
6664 struct tog_color *tc;
6665 int width, n, nentries, i = 1;
6666 int limit = view->nlines;
6669 if (view_is_hsplit_top(view))
6670 --limit; /* border */
6672 werase(view->window);
6677 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6681 if (view_needs_focus_indication(view))
6682 wstandout(view->window);
6683 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6685 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6686 waddwstr(view->window, wline);
6689 while (width++ < view->ncols)
6690 waddch(view->window, ' ');
6692 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6693 if (view_needs_focus_indication(view))
6694 wstandend(view->window);
6699 if (s->first_displayed_entry) {
6700 i += got_tree_entry_get_index(s->first_displayed_entry);
6701 if (s->tree != s->root)
6702 ++i; /* account for ".." entry */
6704 nentries = got_object_tree_get_nentries(s->tree);
6705 if (asprintf(&index, "[%d/%d] %s",
6706 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6707 return got_error_from_errno("asprintf");
6708 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6712 waddwstr(view->window, wline);
6715 if (width < view->ncols - 1)
6716 waddch(view->window, '\n');
6719 waddch(view->window, '\n');
6723 if (s->first_displayed_entry == NULL) {
6724 te = got_object_tree_get_first_entry(s->tree);
6725 if (s->selected == 0) {
6727 wstandout(view->window);
6728 s->selected_entry = NULL;
6730 waddstr(view->window, " ..\n"); /* parent directory */
6731 if (s->selected == 0 && view->focussed)
6732 wstandend(view->window);
6739 te = s->first_displayed_entry;
6742 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6743 char *line = NULL, *id_str = NULL, *link_target = NULL;
6744 const char *modestr = "";
6747 te = got_object_tree_get_entry(s->tree, i);
6748 mode = got_tree_entry_get_mode(te);
6751 err = got_object_id_str(&id_str,
6752 got_tree_entry_get_id(te));
6754 return got_error_from_errno(
6755 "got_object_id_str");
6757 if (got_object_tree_entry_is_submodule(te))
6759 else if (S_ISLNK(mode)) {
6762 err = got_tree_entry_get_symlink_target(&link_target,
6768 for (i = 0; i < strlen(link_target); i++) {
6769 if (!isprint((unsigned char)link_target[i]))
6770 link_target[i] = '?';
6774 else if (S_ISDIR(mode))
6776 else if (mode & S_IXUSR)
6778 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6779 got_tree_entry_get_name(te), modestr,
6780 link_target ? " -> ": "",
6781 link_target ? link_target : "") == -1) {
6784 return got_error_from_errno("asprintf");
6788 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6794 if (n == s->selected) {
6796 wstandout(view->window);
6797 s->selected_entry = te;
6799 tc = match_color(&s->colors, line);
6801 wattr_on(view->window,
6802 COLOR_PAIR(tc->colorpair), NULL);
6803 waddwstr(view->window, wline);
6805 wattr_off(view->window,
6806 COLOR_PAIR(tc->colorpair), NULL);
6807 if (width < view->ncols - 1)
6808 waddch(view->window, '\n');
6809 if (n == s->selected && view->focussed)
6810 wstandend(view->window);
6816 s->last_displayed_entry = te;
6825 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6827 struct got_tree_entry *te;
6828 int isroot = s->tree == s->root;
6831 if (s->first_displayed_entry == NULL)
6834 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6835 while (i++ < maxscroll) {
6838 s->first_displayed_entry = NULL;
6841 s->first_displayed_entry = te;
6842 te = got_tree_entry_get_prev(s->tree, te);
6846 static const struct got_error *
6847 tree_scroll_down(struct tog_view *view, int maxscroll)
6849 struct tog_tree_view_state *s = &view->state.tree;
6850 struct got_tree_entry *next, *last;
6853 if (s->first_displayed_entry)
6854 next = got_tree_entry_get_next(s->tree,
6855 s->first_displayed_entry);
6857 next = got_object_tree_get_first_entry(s->tree);
6859 last = s->last_displayed_entry;
6860 while (next && n++ < maxscroll) {
6862 s->last_displayed_entry = last;
6863 last = got_tree_entry_get_next(s->tree, last);
6865 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6866 s->first_displayed_entry = next;
6867 next = got_tree_entry_get_next(s->tree, next);
6874 static const struct got_error *
6875 tree_entry_path(char **path, struct tog_parent_trees *parents,
6876 struct got_tree_entry *te)
6878 const struct got_error *err = NULL;
6879 struct tog_parent_tree *pt;
6880 size_t len = 2; /* for leading slash and NUL */
6882 TAILQ_FOREACH(pt, parents, entry)
6883 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6886 len += strlen(got_tree_entry_get_name(te));
6888 *path = calloc(1, len);
6890 return got_error_from_errno("calloc");
6893 pt = TAILQ_LAST(parents, tog_parent_trees);
6895 const char *name = got_tree_entry_get_name(pt->selected_entry);
6896 if (strlcat(*path, name, len) >= len) {
6897 err = got_error(GOT_ERR_NO_SPACE);
6900 if (strlcat(*path, "/", len) >= len) {
6901 err = got_error(GOT_ERR_NO_SPACE);
6904 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6907 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6908 err = got_error(GOT_ERR_NO_SPACE);
6920 static const struct got_error *
6921 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6922 struct got_tree_entry *te, struct tog_parent_trees *parents,
6923 struct got_object_id *commit_id, struct got_repository *repo)
6925 const struct got_error *err = NULL;
6927 struct tog_view *blame_view;
6931 err = tree_entry_path(&path, parents, te);
6935 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6936 if (blame_view == NULL) {
6937 err = got_error_from_errno("view_open");
6941 err = open_blame_view(blame_view, path, commit_id, repo);
6943 if (err->code == GOT_ERR_CANCELLED)
6945 view_close(blame_view);
6947 *new_view = blame_view;
6953 static const struct got_error *
6954 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6955 struct tog_tree_view_state *s)
6957 struct tog_view *log_view;
6958 const struct got_error *err = NULL;
6963 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6964 if (log_view == NULL)
6965 return got_error_from_errno("view_open");
6967 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6971 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6974 view_close(log_view);
6976 *new_view = log_view;
6981 static const struct got_error *
6982 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6983 const char *head_ref_name, struct got_repository *repo)
6985 const struct got_error *err = NULL;
6986 char *commit_id_str = NULL;
6987 struct tog_tree_view_state *s = &view->state.tree;
6988 struct got_commit_object *commit = NULL;
6990 TAILQ_INIT(&s->parents);
6991 STAILQ_INIT(&s->colors);
6993 s->commit_id = got_object_id_dup(commit_id);
6994 if (s->commit_id == NULL)
6995 return got_error_from_errno("got_object_id_dup");
6997 err = got_object_open_as_commit(&commit, repo, commit_id);
7002 * The root is opened here and will be closed when the view is closed.
7003 * Any visited subtrees and their path-wise parents are opened and
7006 err = got_object_open_as_tree(&s->root, repo,
7007 got_object_commit_get_tree_id(commit));
7012 err = got_object_id_str(&commit_id_str, commit_id);
7016 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7017 err = got_error_from_errno("asprintf");
7021 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7022 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7023 if (head_ref_name) {
7024 s->head_ref_name = strdup(head_ref_name);
7025 if (s->head_ref_name == NULL) {
7026 err = got_error_from_errno("strdup");
7032 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7033 err = add_color(&s->colors, "\\$$",
7034 TOG_COLOR_TREE_SUBMODULE,
7035 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7038 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7039 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7042 err = add_color(&s->colors, "/$",
7043 TOG_COLOR_TREE_DIRECTORY,
7044 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7048 err = add_color(&s->colors, "\\*$",
7049 TOG_COLOR_TREE_EXECUTABLE,
7050 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7054 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7055 get_color_value("TOG_COLOR_COMMIT"));
7060 view->show = show_tree_view;
7061 view->input = input_tree_view;
7062 view->close = close_tree_view;
7063 view->search_start = search_start_tree_view;
7064 view->search_next = search_next_tree_view;
7066 free(commit_id_str);
7068 got_object_commit_close(commit);
7070 close_tree_view(view);
7074 static const struct got_error *
7075 close_tree_view(struct tog_view *view)
7077 struct tog_tree_view_state *s = &view->state.tree;
7079 free_colors(&s->colors);
7080 free(s->tree_label);
7081 s->tree_label = NULL;
7083 s->commit_id = NULL;
7084 free(s->head_ref_name);
7085 s->head_ref_name = NULL;
7086 while (!TAILQ_EMPTY(&s->parents)) {
7087 struct tog_parent_tree *parent;
7088 parent = TAILQ_FIRST(&s->parents);
7089 TAILQ_REMOVE(&s->parents, parent, entry);
7090 if (parent->tree != s->root)
7091 got_object_tree_close(parent->tree);
7095 if (s->tree != NULL && s->tree != s->root)
7096 got_object_tree_close(s->tree);
7098 got_object_tree_close(s->root);
7102 static const struct got_error *
7103 search_start_tree_view(struct tog_view *view)
7105 struct tog_tree_view_state *s = &view->state.tree;
7107 s->matched_entry = NULL;
7112 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7114 regmatch_t regmatch;
7116 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7120 static const struct got_error *
7121 search_next_tree_view(struct tog_view *view)
7123 struct tog_tree_view_state *s = &view->state.tree;
7124 struct got_tree_entry *te = NULL;
7126 if (!view->searching) {
7127 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7131 if (s->matched_entry) {
7132 if (view->searching == TOG_SEARCH_FORWARD) {
7133 if (s->selected_entry)
7134 te = got_tree_entry_get_next(s->tree,
7137 te = got_object_tree_get_first_entry(s->tree);
7139 if (s->selected_entry == NULL)
7140 te = got_object_tree_get_last_entry(s->tree);
7142 te = got_tree_entry_get_prev(s->tree,
7146 if (s->selected_entry)
7147 te = s->selected_entry;
7148 else if (view->searching == TOG_SEARCH_FORWARD)
7149 te = got_object_tree_get_first_entry(s->tree);
7151 te = got_object_tree_get_last_entry(s->tree);
7156 if (s->matched_entry == NULL) {
7157 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7160 if (view->searching == TOG_SEARCH_FORWARD)
7161 te = got_object_tree_get_first_entry(s->tree);
7163 te = got_object_tree_get_last_entry(s->tree);
7166 if (match_tree_entry(te, &view->regex)) {
7167 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7168 s->matched_entry = te;
7172 if (view->searching == TOG_SEARCH_FORWARD)
7173 te = got_tree_entry_get_next(s->tree, te);
7175 te = got_tree_entry_get_prev(s->tree, te);
7178 if (s->matched_entry) {
7179 s->first_displayed_entry = s->matched_entry;
7186 static const struct got_error *
7187 show_tree_view(struct tog_view *view)
7189 const struct got_error *err = NULL;
7190 struct tog_tree_view_state *s = &view->state.tree;
7193 err = tree_entry_path(&parent_path, &s->parents, NULL);
7197 err = draw_tree_entries(view, parent_path);
7204 static const struct got_error *
7205 tree_goto_line(struct tog_view *view, int nlines)
7207 const struct got_error *err = NULL;
7208 struct tog_tree_view_state *s = &view->state.tree;
7209 struct got_tree_entry **fte, **lte, **ste;
7210 int g, last, first = 1, i = 1;
7211 int root = s->tree == s->root;
7212 int off = root ? 1 : 2;
7219 else if (g > got_object_tree_get_nentries(s->tree))
7220 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7222 fte = &s->first_displayed_entry;
7223 lte = &s->last_displayed_entry;
7224 ste = &s->selected_entry;
7227 first = got_tree_entry_get_index(*fte);
7228 first += off; /* account for ".." */
7230 last = got_tree_entry_get_index(*lte);
7233 if (g >= first && g <= last && g - first < nlines) {
7234 s->selected = g - first;
7235 return NULL; /* gline is on the current page */
7239 i = got_tree_entry_get_index(*ste);
7244 err = tree_scroll_down(view, g - i);
7247 if (got_tree_entry_get_index(*lte) >=
7248 got_object_tree_get_nentries(s->tree) - 1 &&
7249 first + s->selected < g &&
7250 s->selected < s->ndisplayed - 1) {
7251 first = got_tree_entry_get_index(*fte);
7253 s->selected = g - first;
7256 tree_scroll_up(s, i - g);
7259 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7260 s->selected = g - 1;
7265 static const struct got_error *
7266 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7268 const struct got_error *err = NULL;
7269 struct tog_tree_view_state *s = &view->state.tree;
7270 struct got_tree_entry *te;
7271 int n, nscroll = view->nlines - 3;
7274 return tree_goto_line(view, nscroll);
7278 s->show_ids = !s->show_ids;
7283 if (!s->selected_entry)
7285 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7289 err = view_request_new(new_view, view, TOG_VIEW_REF);
7295 if (s->tree == s->root)
7296 s->first_displayed_entry =
7297 got_object_tree_get_first_entry(s->tree);
7299 s->first_displayed_entry = NULL;
7303 int eos = view->nlines - 3;
7305 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7309 te = got_object_tree_get_last_entry(s->tree);
7310 for (n = 0; n < eos; n++) {
7312 if (s->tree != s->root) {
7313 s->first_displayed_entry = NULL;
7318 s->first_displayed_entry = te;
7319 te = got_tree_entry_get_prev(s->tree, te);
7322 s->selected = n - 1;
7328 if (s->selected > 0) {
7332 tree_scroll_up(s, 1);
7333 if (s->selected_entry == NULL ||
7334 (s->tree == s->root && s->selected_entry ==
7335 got_object_tree_get_first_entry(s->tree)))
7345 if (s->tree == s->root) {
7346 if (got_object_tree_get_first_entry(s->tree) ==
7347 s->first_displayed_entry)
7348 s->selected -= MIN(s->selected, nscroll);
7350 if (s->first_displayed_entry == NULL)
7351 s->selected -= MIN(s->selected, nscroll);
7353 tree_scroll_up(s, MAX(0, nscroll));
7354 if (s->selected_entry == NULL ||
7355 (s->tree == s->root && s->selected_entry ==
7356 got_object_tree_get_first_entry(s->tree)))
7362 if (s->selected < s->ndisplayed - 1) {
7366 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7368 /* can't scroll any further */
7372 tree_scroll_down(view, 1);
7382 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7384 /* can't scroll any further; move cursor down */
7385 if (s->selected < s->ndisplayed - 1)
7386 s->selected += MIN(nscroll,
7387 s->ndisplayed - s->selected - 1);
7392 tree_scroll_down(view, nscroll);
7397 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7398 struct tog_parent_tree *parent;
7399 /* user selected '..' */
7400 if (s->tree == s->root) {
7404 parent = TAILQ_FIRST(&s->parents);
7405 TAILQ_REMOVE(&s->parents, parent,
7407 got_object_tree_close(s->tree);
7408 s->tree = parent->tree;
7409 s->first_displayed_entry =
7410 parent->first_displayed_entry;
7412 parent->selected_entry;
7413 s->selected = parent->selected;
7414 if (s->selected > view->nlines - 3) {
7415 err = offset_selection_down(view);
7420 } else if (S_ISDIR(got_tree_entry_get_mode(
7421 s->selected_entry))) {
7422 struct got_tree_object *subtree;
7424 err = got_object_open_as_tree(&subtree, s->repo,
7425 got_tree_entry_get_id(s->selected_entry));
7428 err = tree_view_visit_subtree(s, subtree);
7430 got_object_tree_close(subtree);
7433 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7434 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7437 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7438 s->selected = view->nlines - 4;
7454 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7459 static const struct got_error *
7460 cmd_tree(int argc, char *argv[])
7462 const struct got_error *error;
7463 struct got_repository *repo = NULL;
7464 struct got_worktree *worktree = NULL;
7465 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7466 struct got_object_id *commit_id = NULL;
7467 struct got_commit_object *commit = NULL;
7468 const char *commit_id_arg = NULL;
7470 struct got_reference *ref = NULL;
7471 const char *head_ref_name = NULL;
7473 struct tog_view *view;
7474 int *pack_fds = NULL;
7476 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7479 commit_id_arg = optarg;
7482 repo_path = realpath(optarg, NULL);
7483 if (repo_path == NULL)
7484 return got_error_from_errno2("realpath",
7499 error = got_repo_pack_fds_open(&pack_fds);
7503 if (repo_path == NULL) {
7504 cwd = getcwd(NULL, 0);
7506 return got_error_from_errno("getcwd");
7507 error = got_worktree_open(&worktree, cwd);
7508 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7512 strdup(got_worktree_get_repo_path(worktree));
7514 repo_path = strdup(cwd);
7515 if (repo_path == NULL) {
7516 error = got_error_from_errno("strdup");
7521 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7525 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7532 error = apply_unveil(got_repo_get_path(repo), NULL);
7536 error = tog_load_refs(repo, 0);
7540 if (commit_id_arg == NULL) {
7541 error = got_repo_match_object_id(&commit_id, &label,
7542 worktree ? got_worktree_get_head_ref_name(worktree) :
7543 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7546 head_ref_name = label;
7548 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7550 head_ref_name = got_ref_get_name(ref);
7551 else if (error->code != GOT_ERR_NOT_REF)
7553 error = got_repo_match_object_id(&commit_id, NULL,
7554 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7559 error = got_object_open_as_commit(&commit, repo, commit_id);
7563 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7565 error = got_error_from_errno("view_open");
7568 error = open_tree_view(view, commit_id, head_ref_name, repo);
7571 if (!got_path_is_root_dir(in_repo_path)) {
7572 error = tree_view_walk_path(&view->state.tree, commit,
7579 /* Release work tree lock. */
7580 got_worktree_close(worktree);
7583 error = view_loop(view);
7592 const struct got_error *close_err = got_repo_close(repo);
7597 const struct got_error *pack_err =
7598 got_repo_pack_fds_close(pack_fds);
7606 static const struct got_error *
7607 ref_view_load_refs(struct tog_ref_view_state *s)
7609 struct got_reflist_entry *sre;
7610 struct tog_reflist_entry *re;
7613 TAILQ_FOREACH(sre, &tog_refs, entry) {
7614 if (strncmp(got_ref_get_name(sre->ref),
7615 "refs/got/", 9) == 0 &&
7616 strncmp(got_ref_get_name(sre->ref),
7617 "refs/got/backup/", 16) != 0)
7620 re = malloc(sizeof(*re));
7622 return got_error_from_errno("malloc");
7624 re->ref = got_ref_dup(sre->ref);
7625 if (re->ref == NULL)
7626 return got_error_from_errno("got_ref_dup");
7627 re->idx = s->nrefs++;
7628 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7631 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7636 ref_view_free_refs(struct tog_ref_view_state *s)
7638 struct tog_reflist_entry *re;
7640 while (!TAILQ_EMPTY(&s->refs)) {
7641 re = TAILQ_FIRST(&s->refs);
7642 TAILQ_REMOVE(&s->refs, re, entry);
7643 got_ref_close(re->ref);
7648 static const struct got_error *
7649 open_ref_view(struct tog_view *view, struct got_repository *repo)
7651 const struct got_error *err = NULL;
7652 struct tog_ref_view_state *s = &view->state.ref;
7654 s->selected_entry = 0;
7657 TAILQ_INIT(&s->refs);
7658 STAILQ_INIT(&s->colors);
7660 err = ref_view_load_refs(s);
7664 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7665 err = add_color(&s->colors, "^refs/heads/",
7666 TOG_COLOR_REFS_HEADS,
7667 get_color_value("TOG_COLOR_REFS_HEADS"));
7671 err = add_color(&s->colors, "^refs/tags/",
7672 TOG_COLOR_REFS_TAGS,
7673 get_color_value("TOG_COLOR_REFS_TAGS"));
7677 err = add_color(&s->colors, "^refs/remotes/",
7678 TOG_COLOR_REFS_REMOTES,
7679 get_color_value("TOG_COLOR_REFS_REMOTES"));
7683 err = add_color(&s->colors, "^refs/got/backup/",
7684 TOG_COLOR_REFS_BACKUP,
7685 get_color_value("TOG_COLOR_REFS_BACKUP"));
7690 view->show = show_ref_view;
7691 view->input = input_ref_view;
7692 view->close = close_ref_view;
7693 view->search_start = search_start_ref_view;
7694 view->search_next = search_next_ref_view;
7697 free_colors(&s->colors);
7701 static const struct got_error *
7702 close_ref_view(struct tog_view *view)
7704 struct tog_ref_view_state *s = &view->state.ref;
7706 ref_view_free_refs(s);
7707 free_colors(&s->colors);
7712 static const struct got_error *
7713 resolve_reflist_entry(struct got_object_id **commit_id,
7714 struct tog_reflist_entry *re, struct got_repository *repo)
7716 const struct got_error *err = NULL;
7717 struct got_object_id *obj_id;
7718 struct got_tag_object *tag = NULL;
7723 err = got_ref_resolve(&obj_id, repo, re->ref);
7727 err = got_object_get_type(&obj_type, repo, obj_id);
7732 case GOT_OBJ_TYPE_COMMIT:
7733 *commit_id = obj_id;
7735 case GOT_OBJ_TYPE_TAG:
7736 err = got_object_open_as_tag(&tag, repo, obj_id);
7740 err = got_object_get_type(&obj_type, repo,
7741 got_object_tag_get_object_id(tag));
7744 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7745 err = got_error(GOT_ERR_OBJ_TYPE);
7748 *commit_id = got_object_id_dup(
7749 got_object_tag_get_object_id(tag));
7750 if (*commit_id == NULL) {
7751 err = got_error_from_errno("got_object_id_dup");
7756 err = got_error(GOT_ERR_OBJ_TYPE);
7762 got_object_tag_close(tag);
7770 static const struct got_error *
7771 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7772 struct tog_reflist_entry *re, struct got_repository *repo)
7774 struct tog_view *log_view;
7775 const struct got_error *err = NULL;
7776 struct got_object_id *commit_id = NULL;
7780 err = resolve_reflist_entry(&commit_id, re, repo);
7782 if (err->code != GOT_ERR_OBJ_TYPE)
7788 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7789 if (log_view == NULL) {
7790 err = got_error_from_errno("view_open");
7794 err = open_log_view(log_view, commit_id, repo,
7795 got_ref_get_name(re->ref), "", 0);
7798 view_close(log_view);
7800 *new_view = log_view;
7806 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7808 struct tog_reflist_entry *re;
7811 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7814 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7815 while (i++ < maxscroll) {
7818 s->first_displayed_entry = re;
7819 re = TAILQ_PREV(re, tog_reflist_head, entry);
7823 static const struct got_error *
7824 ref_scroll_down(struct tog_view *view, int maxscroll)
7826 struct tog_ref_view_state *s = &view->state.ref;
7827 struct tog_reflist_entry *next, *last;
7830 if (s->first_displayed_entry)
7831 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7833 next = TAILQ_FIRST(&s->refs);
7835 last = s->last_displayed_entry;
7836 while (next && n++ < maxscroll) {
7838 s->last_displayed_entry = last;
7839 last = TAILQ_NEXT(last, entry);
7841 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7842 s->first_displayed_entry = next;
7843 next = TAILQ_NEXT(next, entry);
7850 static const struct got_error *
7851 search_start_ref_view(struct tog_view *view)
7853 struct tog_ref_view_state *s = &view->state.ref;
7855 s->matched_entry = NULL;
7860 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7862 regmatch_t regmatch;
7864 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7868 static const struct got_error *
7869 search_next_ref_view(struct tog_view *view)
7871 struct tog_ref_view_state *s = &view->state.ref;
7872 struct tog_reflist_entry *re = NULL;
7874 if (!view->searching) {
7875 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7879 if (s->matched_entry) {
7880 if (view->searching == TOG_SEARCH_FORWARD) {
7881 if (s->selected_entry)
7882 re = TAILQ_NEXT(s->selected_entry, entry);
7884 re = TAILQ_PREV(s->selected_entry,
7885 tog_reflist_head, entry);
7887 if (s->selected_entry == NULL)
7888 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7890 re = TAILQ_PREV(s->selected_entry,
7891 tog_reflist_head, entry);
7894 if (s->selected_entry)
7895 re = s->selected_entry;
7896 else if (view->searching == TOG_SEARCH_FORWARD)
7897 re = TAILQ_FIRST(&s->refs);
7899 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7904 if (s->matched_entry == NULL) {
7905 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7908 if (view->searching == TOG_SEARCH_FORWARD)
7909 re = TAILQ_FIRST(&s->refs);
7911 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7914 if (match_reflist_entry(re, &view->regex)) {
7915 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7916 s->matched_entry = re;
7920 if (view->searching == TOG_SEARCH_FORWARD)
7921 re = TAILQ_NEXT(re, entry);
7923 re = TAILQ_PREV(re, tog_reflist_head, entry);
7926 if (s->matched_entry) {
7927 s->first_displayed_entry = s->matched_entry;
7934 static const struct got_error *
7935 show_ref_view(struct tog_view *view)
7937 const struct got_error *err = NULL;
7938 struct tog_ref_view_state *s = &view->state.ref;
7939 struct tog_reflist_entry *re;
7942 struct tog_color *tc;
7944 int limit = view->nlines;
7946 werase(view->window);
7949 if (view_is_hsplit_top(view))
7950 --limit; /* border */
7955 re = s->first_displayed_entry;
7957 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7959 return got_error_from_errno("asprintf");
7961 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7966 if (view_needs_focus_indication(view))
7967 wstandout(view->window);
7968 waddwstr(view->window, wline);
7969 while (width++ < view->ncols)
7970 waddch(view->window, ' ');
7971 if (view_needs_focus_indication(view))
7972 wstandend(view->window);
7981 while (re && limit > 0) {
7983 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7986 struct got_commit_object *ci;
7987 struct got_tag_object *tag;
7988 struct got_object_id *id;
7992 err = got_ref_resolve(&id, s->repo, re->ref);
7995 err = got_object_open_as_tag(&tag, s->repo, id);
7997 if (err->code != GOT_ERR_OBJ_TYPE) {
8001 err = got_object_open_as_commit(&ci, s->repo,
8007 t = got_object_commit_get_committer_time(ci);
8008 got_object_commit_close(ci);
8010 t = got_object_tag_get_tagger_time(tag);
8011 got_object_tag_close(tag);
8014 if (gmtime_r(&t, &tm) == NULL)
8015 return got_error_from_errno("gmtime_r");
8016 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8017 return got_error(GOT_ERR_NO_SPACE);
8019 if (got_ref_is_symbolic(re->ref)) {
8020 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8021 ymd : "", got_ref_get_name(re->ref),
8022 got_ref_get_symref_target(re->ref)) == -1)
8023 return got_error_from_errno("asprintf");
8024 } else if (s->show_ids) {
8025 struct got_object_id *id;
8027 err = got_ref_resolve(&id, s->repo, re->ref);
8030 err = got_object_id_str(&id_str, id);
8035 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8036 got_ref_get_name(re->ref), id_str) == -1) {
8037 err = got_error_from_errno("asprintf");
8044 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8045 got_ref_get_name(re->ref)) == -1)
8046 return got_error_from_errno("asprintf");
8048 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
8054 if (n == s->selected) {
8056 wstandout(view->window);
8057 s->selected_entry = re;
8059 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8061 wattr_on(view->window,
8062 COLOR_PAIR(tc->colorpair), NULL);
8063 waddwstr(view->window, wline);
8065 wattr_off(view->window,
8066 COLOR_PAIR(tc->colorpair), NULL);
8067 if (width < view->ncols - 1)
8068 waddch(view->window, '\n');
8069 if (n == s->selected && view->focussed)
8070 wstandend(view->window);
8076 s->last_displayed_entry = re;
8079 re = TAILQ_NEXT(re, entry);
8086 static const struct got_error *
8087 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8088 struct tog_reflist_entry *re, struct got_repository *repo)
8090 const struct got_error *err = NULL;
8091 struct got_object_id *commit_id = NULL;
8092 struct tog_view *tree_view;
8096 err = resolve_reflist_entry(&commit_id, re, repo);
8098 if (err->code != GOT_ERR_OBJ_TYPE)
8105 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8106 if (tree_view == NULL) {
8107 err = got_error_from_errno("view_open");
8111 err = open_tree_view(tree_view, commit_id,
8112 got_ref_get_name(re->ref), repo);
8116 *new_view = tree_view;
8122 static const struct got_error *
8123 ref_goto_line(struct tog_view *view, int nlines)
8125 const struct got_error *err = NULL;
8126 struct tog_ref_view_state *s = &view->state.ref;
8127 int g, idx = s->selected_entry->idx;
8134 else if (g > s->nrefs)
8137 if (g >= s->first_displayed_entry->idx + 1 &&
8138 g <= s->last_displayed_entry->idx + 1 &&
8139 g - s->first_displayed_entry->idx - 1 < nlines) {
8140 s->selected = g - s->first_displayed_entry->idx - 1;
8145 err = ref_scroll_down(view, g - idx - 1);
8148 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8149 s->first_displayed_entry->idx + s->selected < g &&
8150 s->selected < s->ndisplayed - 1)
8151 s->selected = g - s->first_displayed_entry->idx - 1;
8152 } else if (idx + 1 > g)
8153 ref_scroll_up(s, idx - g + 1);
8155 if (g < nlines && s->first_displayed_entry->idx == 0)
8156 s->selected = g - 1;
8162 static const struct got_error *
8163 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8165 const struct got_error *err = NULL;
8166 struct tog_ref_view_state *s = &view->state.ref;
8167 struct tog_reflist_entry *re;
8168 int n, nscroll = view->nlines - 1;
8171 return ref_goto_line(view, nscroll);
8175 s->show_ids = !s->show_ids;
8179 s->show_date = !s->show_date;
8183 s->sort_by_date = !s->sort_by_date;
8185 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8186 got_ref_cmp_by_commit_timestamp_descending :
8187 tog_ref_cmp_by_name, s->repo);
8190 got_reflist_object_id_map_free(tog_refs_idmap);
8191 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8192 &tog_refs, s->repo);
8195 ref_view_free_refs(s);
8196 err = ref_view_load_refs(s);
8201 if (!s->selected_entry)
8203 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8207 if (!s->selected_entry)
8209 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8215 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8219 int eos = view->nlines - 1;
8221 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8225 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8226 for (n = 0; n < eos; n++) {
8229 s->first_displayed_entry = re;
8230 re = TAILQ_PREV(re, tog_reflist_head, entry);
8233 s->selected = n - 1;
8239 if (s->selected > 0) {
8243 ref_scroll_up(s, 1);
8244 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8254 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8255 s->selected -= MIN(nscroll, s->selected);
8256 ref_scroll_up(s, MAX(0, nscroll));
8257 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8263 if (s->selected < s->ndisplayed - 1) {
8267 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8268 /* can't scroll any further */
8272 ref_scroll_down(view, 1);
8282 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8283 /* can't scroll any further; move cursor down */
8284 if (s->selected < s->ndisplayed - 1)
8285 s->selected += MIN(nscroll,
8286 s->ndisplayed - s->selected - 1);
8287 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8288 s->selected += s->ndisplayed - s->selected - 1;
8292 ref_scroll_down(view, nscroll);
8297 err = tog_load_refs(s->repo, s->sort_by_date);
8300 ref_view_free_refs(s);
8301 err = ref_view_load_refs(s);
8304 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8305 s->selected = view->nlines - 2;
8319 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8324 static const struct got_error *
8325 cmd_ref(int argc, char *argv[])
8327 const struct got_error *error;
8328 struct got_repository *repo = NULL;
8329 struct got_worktree *worktree = NULL;
8330 char *cwd = NULL, *repo_path = NULL;
8332 struct tog_view *view;
8333 int *pack_fds = NULL;
8335 while ((ch = getopt(argc, argv, "r:")) != -1) {
8338 repo_path = realpath(optarg, NULL);
8339 if (repo_path == NULL)
8340 return got_error_from_errno2("realpath",
8355 error = got_repo_pack_fds_open(&pack_fds);
8359 if (repo_path == NULL) {
8360 cwd = getcwd(NULL, 0);
8362 return got_error_from_errno("getcwd");
8363 error = got_worktree_open(&worktree, cwd);
8364 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8368 strdup(got_worktree_get_repo_path(worktree));
8370 repo_path = strdup(cwd);
8371 if (repo_path == NULL) {
8372 error = got_error_from_errno("strdup");
8377 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8383 error = apply_unveil(got_repo_get_path(repo), NULL);
8387 error = tog_load_refs(repo, 0);
8391 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8393 error = got_error_from_errno("view_open");
8397 error = open_ref_view(view, repo);
8402 /* Release work tree lock. */
8403 got_worktree_close(worktree);
8406 error = view_loop(view);
8411 const struct got_error *close_err = got_repo_close(repo);
8416 const struct got_error *pack_err =
8417 got_repo_pack_fds_close(pack_fds);
8425 static const struct got_error*
8426 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8435 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8439 if (mvwprintw(win, y, x, "%s", str) == ERR)
8440 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8447 static const struct got_error *
8448 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8452 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8454 free(*line_offsets);
8455 *line_offsets = NULL;
8456 return got_error_from_errno("reallocarray");
8460 (*line_offsets)[*nlines] = off;
8465 static const struct got_error *
8466 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8470 for (;n > 0; --n, ++km) {
8474 if (km->keys == NULL)
8477 t = t0 = strdup(km->keys);
8479 return got_error_from_errno("strdup");
8482 while ((k = strsep(&t, " ")) != NULL)
8483 len += strlen(k) > 1 ? 2 : 0;
8485 *ret = MAX(*ret, len);
8492 * Write keymap section headers, keys, and key info in km to f.
8493 * Save line offset to *off. If terminal has UTF8 encoding enabled,
8494 * wrap control and symbolic keys in guillemets, else use <>.
8496 static const struct got_error *
8497 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8502 static const char *u8_glyph[] = {
8503 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8504 "\xe2\x80\xba" /* U+203A (utf8 >) */
8507 int cs, s, first = 1;
8509 cs = got_locale_is_utf8();
8511 t = t0 = strdup(km->keys);
8513 return got_error_from_errno("strdup");
8515 len = strlen(km->keys);
8516 while ((k = strsep(&t, " ")) != NULL) {
8517 s = strlen(k) > 1; /* control or symbolic key */
8518 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8519 cs && s ? u8_glyph[0] : s ? "<" : "", k,
8520 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8523 return got_error_from_errno("fprintf");
8531 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8533 return got_error_from_errno("fprintf");
8539 static const struct got_error *
8540 format_help(struct tog_help_view_state *s)
8542 const struct got_error *err = NULL;
8544 int i, max, n, show = s->all;
8545 static const struct tog_key_map km[] = {
8546 #define KEYMAP_(info, type) { NULL, (info), type }
8547 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8553 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8558 err = max_key_str(&max, km, n);
8562 for (i = 0; i < n; ++i) {
8563 if (km[i].keys == NULL) {
8565 if (km[i].type == TOG_KEYMAP_GLOBAL ||
8566 km[i].type == s->type || s->all)
8570 err = format_help_line(&off, s->f, &km[i], max);
8573 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8580 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8584 static const struct got_error *
8585 create_help(struct tog_help_view_state *s)
8588 const struct got_error *err;
8590 free(s->line_offsets);
8591 s->line_offsets = NULL;
8596 return got_error_from_errno("got_opentemp");
8599 err = format_help(s);
8603 if (s->f && fflush(s->f) != 0)
8604 return got_error_from_errno("fflush");
8609 static const struct got_error *
8610 search_start_help_view(struct tog_view *view)
8612 view->state.help.matched_line = 0;
8617 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8618 size_t *nlines, int **first, int **last, int **match, int **selected)
8620 struct tog_help_view_state *s = &view->state.help;
8623 *nlines = s->nlines;
8624 *line_offsets = s->line_offsets;
8625 *match = &s->matched_line;
8626 *first = &s->first_displayed_line;
8627 *last = &s->last_displayed_line;
8628 *selected = &s->selected_line;
8631 static const struct got_error *
8632 show_help_view(struct tog_view *view)
8634 struct tog_help_view_state *s = &view->state.help;
8635 const struct got_error *err;
8636 regmatch_t *regmatch = &view->regmatch;
8641 int width, nprinted = 0, rc = 0;
8642 int eos = view->nlines;
8644 if (view_is_hsplit_top(view))
8645 --eos; /* account for border */
8649 werase(view->window);
8651 if (view->gline > s->nlines - 1)
8652 view->gline = s->nlines - 1;
8654 err = win_draw_center(view->window, 0, 0, view->ncols,
8655 view_needs_focus_indication(view),
8656 "tog help (press q to return to tog)");
8661 waddstr(view->window, "\n\n");
8667 while (eos > 0 && nprinted < eos) {
8670 linelen = getline(&line, &linesz, s->f);
8671 if (linelen == -1) {
8674 return got_ferror(s->f, GOT_ERR_IO);
8679 if (++s->lineno < s->first_displayed_line)
8681 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8683 if (s->lineno == view->hiline)
8686 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8692 view->maxx = MAX(view->maxx, width);
8697 wattron(view->window, attr);
8698 if (s->first_displayed_line + nprinted == s->matched_line &&
8699 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8700 err = add_matched_line(&width, line, view->ncols - 1, 0,
8701 view->window, view->x, regmatch);
8709 err = format_line(&wline, &width, &skip, line,
8710 view->x, view->ncols - 1, 0, view->x ? 1 : 0);
8715 rc = waddwstr(view->window, &wline[skip]);
8719 return got_error_msg(GOT_ERR_IO, "waddwstr");
8721 if (s->lineno == view->hiline) {
8722 while (width++ < view->ncols)
8723 waddch(view->window, ' ');
8725 if (width <= view->ncols)
8726 waddch(view->window, '\n');
8729 wattroff(view->window, attr);
8730 if (++nprinted == 1)
8731 s->first_displayed_line = s->lineno;
8735 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8737 s->last_displayed_line = s->first_displayed_line;
8742 rc = waddnstr(view->window,
8743 "See the tog(1) manual page for full documentation",
8746 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8748 wmove(view->window, view->nlines - 1, 0);
8749 wclrtoeol(view->window);
8750 wstandout(view->window);
8751 rc = waddnstr(view->window, "scroll down for more...",
8754 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8755 if (getcurx(view->window) < view->ncols - 6) {
8756 rc = wprintw(view->window, "[%.0f%%]",
8757 100.00 * s->last_displayed_line / s->nlines);
8759 return got_error_msg(GOT_ERR_IO, "wprintw");
8761 wstandend(view->window);
8767 static const struct got_error *
8768 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8770 struct tog_help_view_state *s = &view->state.help;
8771 const struct got_error *err = NULL;
8777 eos = nscroll = view->nlines;
8778 if (view_is_hsplit_top(view))
8781 s->lineno = s->first_displayed_line - 1 + s->selected_line;
8788 view->x = MAX(view->maxx - view->ncols / 3, 0);
8793 if (view->x + view->ncols / 3 < view->maxx)
8800 view->x -= MIN(view->x, 2);
8806 s->first_displayed_line = 1;
8814 s->first_displayed_line = (s->nlines - eos) + 3;
8819 if (s->first_displayed_line > 1)
8820 --s->first_displayed_line;
8831 if (s->first_displayed_line == 1) {
8835 while (--nscroll > 0 && s->first_displayed_line > 1)
8836 s->first_displayed_line--;
8842 ++s->first_displayed_line;
8858 while (!s->eof && --nscroll > 0) {
8859 linelen = getline(&line, &linesz, s->f);
8860 s->first_displayed_line++;
8861 if (linelen == -1) {
8865 err = got_ferror(s->f, GOT_ERR_IO);
8879 static const struct got_error *
8880 close_help_view(struct tog_view *view)
8882 struct tog_help_view_state *s = &view->state.help;
8884 free(s->line_offsets);
8885 s->line_offsets = NULL;
8886 if (fclose(s->f) == EOF)
8887 return got_error_from_errno("fclose");
8892 static const struct got_error *
8893 reset_help_view(struct tog_view *view)
8895 struct tog_help_view_state *s = &view->state.help;
8898 if (s->f && fclose(s->f) == EOF)
8899 return got_error_from_errno("fclose");
8901 wclear(view->window);
8905 s->first_displayed_line = 1;
8906 s->last_displayed_line = view->nlines;
8907 s->matched_line = 0;
8909 return create_help(s);
8912 static const struct got_error *
8913 open_help_view(struct tog_view *view, struct tog_view *parent)
8915 const struct got_error *err = NULL;
8916 struct tog_help_view_state *s = &view->state.help;
8918 s->type = (enum tog_keymap_type)parent->type;
8919 s->first_displayed_line = 1;
8920 s->last_displayed_line = view->nlines;
8921 s->selected_line = 1;
8923 view->show = show_help_view;
8924 view->input = input_help_view;
8925 view->reset = reset_help_view;
8926 view->close = close_help_view;
8927 view->search_start = search_start_help_view;
8928 view->search_setup = search_setup_help_view;
8929 view->search_next = search_next_view_match;
8931 err = create_help(s);
8935 static const struct got_error *
8936 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8937 enum tog_view_type request, int y, int x)
8939 const struct got_error *err = NULL;
8945 if (view->type == TOG_VIEW_LOG) {
8946 struct tog_log_view_state *s = &view->state.log;
8948 err = open_diff_view_for_commit(new_view, y, x,
8949 s->selected_entry->commit, s->selected_entry->id,
8952 return got_error_msg(GOT_ERR_NOT_IMPL,
8953 "parent/child view pair not supported");
8955 case TOG_VIEW_BLAME:
8956 if (view->type == TOG_VIEW_TREE) {
8957 struct tog_tree_view_state *s = &view->state.tree;
8959 err = blame_tree_entry(new_view, y, x,
8960 s->selected_entry, &s->parents, s->commit_id,
8963 return got_error_msg(GOT_ERR_NOT_IMPL,
8964 "parent/child view pair not supported");
8967 if (view->type == TOG_VIEW_BLAME)
8968 err = log_annotated_line(new_view, y, x,
8969 view->state.blame.repo, view->state.blame.id_to_log);
8970 else if (view->type == TOG_VIEW_TREE)
8971 err = log_selected_tree_entry(new_view, y, x,
8973 else if (view->type == TOG_VIEW_REF)
8974 err = log_ref_entry(new_view, y, x,
8975 view->state.ref.selected_entry,
8976 view->state.ref.repo);
8978 return got_error_msg(GOT_ERR_NOT_IMPL,
8979 "parent/child view pair not supported");
8982 if (view->type == TOG_VIEW_LOG)
8983 err = browse_commit_tree(new_view, y, x,
8984 view->state.log.selected_entry,
8985 view->state.log.in_repo_path,
8986 view->state.log.head_ref_name,
8987 view->state.log.repo);
8988 else if (view->type == TOG_VIEW_REF)
8989 err = browse_ref_tree(new_view, y, x,
8990 view->state.ref.selected_entry,
8991 view->state.ref.repo);
8993 return got_error_msg(GOT_ERR_NOT_IMPL,
8994 "parent/child view pair not supported");
8997 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8998 if (*new_view == NULL)
8999 return got_error_from_errno("view_open");
9000 if (view->type == TOG_VIEW_LOG)
9001 err = open_ref_view(*new_view, view->state.log.repo);
9002 else if (view->type == TOG_VIEW_TREE)
9003 err = open_ref_view(*new_view, view->state.tree.repo);
9005 err = got_error_msg(GOT_ERR_NOT_IMPL,
9006 "parent/child view pair not supported");
9008 view_close(*new_view);
9011 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9012 if (*new_view == NULL)
9013 return got_error_from_errno("view_open");
9014 err = open_help_view(*new_view, view);
9016 view_close(*new_view);
9019 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9026 * If view was scrolled down to move the selected line into view when opening a
9027 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9030 offset_selection_up(struct tog_view *view)
9032 switch (view->type) {
9033 case TOG_VIEW_BLAME: {
9034 struct tog_blame_view_state *s = &view->state.blame;
9035 if (s->first_displayed_line == 1) {
9036 s->selected_line = MAX(s->selected_line - view->offset,
9040 if (s->first_displayed_line > view->offset)
9041 s->first_displayed_line -= view->offset;
9043 s->first_displayed_line = 1;
9044 s->selected_line += view->offset;
9048 log_scroll_up(&view->state.log, view->offset);
9049 view->state.log.selected += view->offset;
9052 ref_scroll_up(&view->state.ref, view->offset);
9053 view->state.ref.selected += view->offset;
9056 tree_scroll_up(&view->state.tree, view->offset);
9057 view->state.tree.selected += view->offset;
9067 * If the selected line is in the section of screen covered by the bottom split,
9068 * scroll down offset lines to move it into view and index its new position.
9070 static const struct got_error *
9071 offset_selection_down(struct tog_view *view)
9073 const struct got_error *err = NULL;
9074 const struct got_error *(*scrolld)(struct tog_view *, int);
9075 int *selected = NULL;
9078 switch (view->type) {
9079 case TOG_VIEW_BLAME: {
9080 struct tog_blame_view_state *s = &view->state.blame;
9083 if (s->selected_line > view->nlines - header) {
9084 offset = abs(view->nlines - s->selected_line - header);
9085 s->first_displayed_line += offset;
9086 s->selected_line -= offset;
9087 view->offset = offset;
9091 case TOG_VIEW_LOG: {
9092 struct tog_log_view_state *s = &view->state.log;
9093 scrolld = &log_scroll_down;
9094 header = view_is_parent_view(view) ? 3 : 2;
9095 selected = &s->selected;
9098 case TOG_VIEW_REF: {
9099 struct tog_ref_view_state *s = &view->state.ref;
9100 scrolld = &ref_scroll_down;
9102 selected = &s->selected;
9105 case TOG_VIEW_TREE: {
9106 struct tog_tree_view_state *s = &view->state.tree;
9107 scrolld = &tree_scroll_down;
9109 selected = &s->selected;
9119 if (selected && *selected > view->nlines - header) {
9120 offset = abs(view->nlines - *selected - header);
9121 view->offset = offset;
9122 if (scrolld && offset) {
9123 err = scrolld(view, offset);
9124 *selected -= offset;
9132 list_commands(FILE *fp)
9136 fprintf(fp, "commands:");
9137 for (i = 0; i < nitems(tog_commands); i++) {
9138 const struct tog_cmd *cmd = &tog_commands[i];
9139 fprintf(fp, " %s", cmd->name);
9145 usage(int hflag, int status)
9147 FILE *fp = (status == 0) ? stdout : stderr;
9149 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9152 fprintf(fp, "lazy usage: %s path\n", getprogname());
9159 make_argv(int argc, ...)
9167 argv = calloc(argc, sizeof(char *));
9170 for (i = 0; i < argc; i++) {
9171 argv[i] = strdup(va_arg(ap, char *));
9172 if (argv[i] == NULL)
9181 * Try to convert 'tog path' into a 'tog log path' command.
9182 * The user could simply have mistyped the command rather than knowingly
9183 * provided a path. So check whether argv[0] can in fact be resolved
9184 * to a path in the HEAD commit and print a special error if not.
9185 * This hack is for mpi@ <3
9187 static const struct got_error *
9188 tog_log_with_path(int argc, char *argv[])
9190 const struct got_error *error = NULL, *close_err;
9191 const struct tog_cmd *cmd = NULL;
9192 struct got_repository *repo = NULL;
9193 struct got_worktree *worktree = NULL;
9194 struct got_object_id *commit_id = NULL, *id = NULL;
9195 struct got_commit_object *commit = NULL;
9196 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9197 char *commit_id_str = NULL, **cmd_argv = NULL;
9198 int *pack_fds = NULL;
9200 cwd = getcwd(NULL, 0);
9202 return got_error_from_errno("getcwd");
9204 error = got_repo_pack_fds_open(&pack_fds);
9208 error = got_worktree_open(&worktree, cwd);
9209 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9213 repo_path = strdup(got_worktree_get_repo_path(worktree));
9215 repo_path = strdup(cwd);
9216 if (repo_path == NULL) {
9217 error = got_error_from_errno("strdup");
9221 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9225 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9230 error = tog_load_refs(repo, 0);
9233 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9234 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9235 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9240 got_worktree_close(worktree);
9244 error = got_object_open_as_commit(&commit, repo, commit_id);
9248 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9250 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9252 fprintf(stderr, "%s: '%s' is no known command or path\n",
9253 getprogname(), argv[0]);
9258 error = got_object_id_str(&commit_id_str, commit_id);
9262 cmd = &tog_commands[0]; /* log */
9264 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9265 error = cmd->cmd_main(argc, cmd_argv);
9268 close_err = got_repo_close(repo);
9273 got_object_commit_close(commit);
9275 got_worktree_close(worktree);
9277 const struct got_error *pack_err =
9278 got_repo_pack_fds_close(pack_fds);
9283 free(commit_id_str);
9290 for (i = 0; i < argc; i++)
9299 main(int argc, char *argv[])
9301 const struct got_error *error = NULL;
9302 const struct tog_cmd *cmd = NULL;
9303 int ch, hflag = 0, Vflag = 0;
9304 char **cmd_argv = NULL;
9305 static const struct option longopts[] = {
9306 { "version", no_argument, NULL, 'V' },
9309 char *diff_algo_str = NULL;
9311 if (!isatty(STDIN_FILENO))
9312 errx(1, "standard input is not a tty");
9314 setlocale(LC_CTYPE, "");
9316 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9336 got_version_print_str();
9341 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9349 /* Build an argument vector which runs a default command. */
9350 cmd = &tog_commands[0];
9352 cmd_argv = make_argv(argc, cmd->name);
9356 /* Did the user specify a command? */
9357 for (i = 0; i < nitems(tog_commands); i++) {
9358 if (strncmp(tog_commands[i].name, argv[0],
9359 strlen(argv[0])) == 0) {
9360 cmd = &tog_commands[i];
9366 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9367 if (diff_algo_str) {
9368 if (strcasecmp(diff_algo_str, "patience") == 0)
9369 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9370 if (strcasecmp(diff_algo_str, "myers") == 0)
9371 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9377 /* No command specified; try log with a path */
9378 error = tog_log_with_path(argc, argv);
9383 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9390 for (i = 0; i < argc; i++)
9395 if (error && error->code != GOT_ERR_CANCELLED &&
9396 error->code != GOT_ERR_EOF &&
9397 error->code != GOT_ERR_PRIVSEP_EXIT &&
9398 error->code != GOT_ERR_PRIVSEP_PIPE &&
9399 !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9400 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);