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))
1308 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1309 wclrtoeol(v->window);
1311 nodelay(view->window, FALSE); /* block for search term input */
1314 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1315 wrefresh(v->window);
1318 nodelay(view->window, TRUE);
1322 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1323 err = view->search_start(view);
1325 regfree(&view->regex);
1328 view->search_started = 1;
1329 view->searching = TOG_SEARCH_FORWARD;
1330 view->search_next_done = 0;
1331 view->search_next(view);
1337 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1338 static const struct got_error *
1339 switch_split(struct tog_view *view)
1341 const struct got_error *err = NULL;
1342 struct tog_view *v = NULL;
1349 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1350 v->mode = TOG_VIEW_SPLIT_VERT;
1352 v->mode = TOG_VIEW_SPLIT_HRZN;
1356 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1357 v->mode = TOG_VIEW_SPLIT_NONE;
1359 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1360 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1361 v->child->begin_y = v->child->resized_y;
1362 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1363 v->child->begin_x = v->child->resized_x;
1366 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1368 v->child->ncols = COLS;
1369 v->child->nscrolled = LINES - v->child->nlines;
1371 err = view_init_hsplit(v, v->child->begin_y);
1375 v->child->mode = v->mode;
1376 v->child->nlines = v->lines - v->child->begin_y;
1379 err = view_fullscreen(v);
1382 err = view_splitscreen(v->child);
1386 if (v->mode == TOG_VIEW_SPLIT_NONE)
1387 v->mode = TOG_VIEW_SPLIT_VERT;
1388 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1389 err = offset_selection_down(v);
1392 err = offset_selection_down(v->child);
1396 offset_selection_up(v);
1397 offset_selection_up(v->child);
1400 err = v->resize(v, 0);
1401 else if (v->child->resize)
1402 err = v->child->resize(v->child, 0);
1408 * Compute view->count from numeric input. Assign total to view->count and
1409 * return first non-numeric key entered.
1412 get_compound_key(struct tog_view *view, int c)
1414 struct tog_view *v = view;
1417 if (view_is_hsplit_top(view))
1419 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1423 cbreak(); /* block for input */
1424 nodelay(view->window, FALSE);
1425 wmove(v->window, v->nlines - 1, 0);
1426 wclrtoeol(v->window);
1427 waddch(v->window, ':');
1430 x = getcurx(v->window);
1431 if (x != ERR && x < view->ncols) {
1432 waddch(v->window, c);
1433 wrefresh(v->window);
1437 * Don't overflow. Max valid request should be the greatest
1438 * between the longest and total lines; cap at 10 million.
1443 n = n * 10 + (c - '0');
1444 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1446 if (c == 'G' || c == 'g') { /* nG key map */
1447 view->gline = view->hiline = n;
1452 /* Massage excessive or inapplicable values at the input handler. */
1458 static const struct got_error *
1459 view_input(struct tog_view **new, int *done, struct tog_view *view,
1460 struct tog_view_list_head *views)
1462 const struct got_error *err = NULL;
1468 /* Clear "no matches" indicator. */
1469 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1470 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1471 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1475 if (view->searching && !view->search_next_done) {
1476 errcode = pthread_mutex_unlock(&tog_mutex);
1478 return got_error_set_errno(errcode,
1479 "pthread_mutex_unlock");
1481 errcode = pthread_mutex_lock(&tog_mutex);
1483 return got_error_set_errno(errcode,
1484 "pthread_mutex_lock");
1485 view->search_next(view);
1489 /* Allow threads to make progress while we are waiting for input. */
1490 errcode = pthread_mutex_unlock(&tog_mutex);
1492 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1493 /* If we have an unfinished count, let C-g or backspace abort. */
1494 if (view->count && --view->count) {
1496 nodelay(view->window, TRUE);
1497 ch = wgetch(view->window);
1498 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1503 ch = wgetch(view->window);
1504 if (ch >= '1' && ch <= '9')
1505 view->ch = ch = get_compound_key(view, ch);
1507 if (view->hiline && ch != ERR && ch != 0)
1508 view->hiline = 0; /* key pressed, clear line highlight */
1509 nodelay(view->window, TRUE);
1510 errcode = pthread_mutex_lock(&tog_mutex);
1512 return got_error_set_errno(errcode, "pthread_mutex_lock");
1514 if (tog_sigwinch_received || tog_sigcont_received) {
1516 tog_sigwinch_received = 0;
1517 tog_sigcont_received = 0;
1518 TAILQ_FOREACH(v, views, entry) {
1519 err = view_resize(v);
1522 err = v->input(new, v, KEY_RESIZE);
1526 err = view_resize(v->child);
1529 err = v->child->input(new, v->child,
1533 if (v->child->resized_x || v->child->resized_y) {
1534 err = view_resize_split(v, 0);
1546 if (view->type == TOG_VIEW_HELP)
1547 err = view->reset(view);
1549 err = view_request_new(new, view, TOG_VIEW_HELP);
1555 view->child->focussed = 1;
1556 view->focus_child = 1;
1557 } else if (view->parent) {
1559 view->parent->focussed = 1;
1560 view->parent->focus_child = 0;
1561 if (!view_is_splitscreen(view)) {
1562 if (view->parent->resize) {
1563 err = view->parent->resize(view->parent,
1568 offset_selection_up(view->parent);
1569 err = view_fullscreen(view->parent);
1576 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1577 if (view->parent->resize) {
1578 /* might need more commits to fill fullscreen */
1579 err = view->parent->resize(view->parent, 0);
1583 offset_selection_up(view->parent);
1585 err = view->input(new, view, ch);
1593 if (view_is_parent_view(view)) {
1594 if (view->child == NULL)
1596 if (view_is_splitscreen(view->child)) {
1598 view->child->focussed = 1;
1599 err = view_fullscreen(view->child);
1601 err = view_splitscreen(view->child);
1603 err = view_resize_split(view, 0);
1607 err = view->child->input(new, view->child,
1610 if (view_is_splitscreen(view)) {
1611 view->parent->focussed = 0;
1613 err = view_fullscreen(view);
1615 err = view_splitscreen(view);
1616 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1617 err = view_resize(view->parent);
1619 err = view_resize_split(view, 0);
1623 err = view->input(new, view, KEY_RESIZE);
1628 err = view->resize(view, 0);
1633 err = offset_selection_down(view->parent);
1635 err = offset_selection_down(view);
1639 err = switch_split(view);
1642 err = view_resize_split(view, -1);
1645 err = view_resize_split(view, 1);
1651 if (view->search_start)
1652 view_search_start(view);
1654 err = view->input(new, view, ch);
1658 if (view->search_started && view->search_next) {
1659 view->searching = (ch == 'n' ?
1660 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1661 view->search_next_done = 0;
1662 view->search_next(view);
1664 err = view->input(new, view, ch);
1667 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1668 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1670 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1671 TAILQ_FOREACH(v, views, entry) {
1677 if (v->child && v->child->reset) {
1678 err = v->child->reset(v->child);
1685 err = view->input(new, view, ch);
1693 view_needs_focus_indication(struct tog_view *view)
1695 if (view_is_parent_view(view)) {
1696 if (view->child == NULL || view->child->focussed)
1698 if (!view_is_splitscreen(view->child))
1700 } else if (!view_is_splitscreen(view))
1703 return view->focussed;
1706 static const struct got_error *
1707 view_loop(struct tog_view *view)
1709 const struct got_error *err = NULL;
1710 struct tog_view_list_head views;
1711 struct tog_view *new_view;
1713 int fast_refresh = 10;
1714 int done = 0, errcode;
1716 mode = getenv("TOG_VIEW_SPLIT_MODE");
1717 if (!mode || !(*mode == 'h' || *mode == 'H'))
1718 view->mode = TOG_VIEW_SPLIT_VERT;
1720 view->mode = TOG_VIEW_SPLIT_HRZN;
1722 errcode = pthread_mutex_lock(&tog_mutex);
1724 return got_error_set_errno(errcode, "pthread_mutex_lock");
1727 TAILQ_INSERT_HEAD(&views, view, entry);
1730 err = view->show(view);
1735 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1736 !tog_fatal_signal_received()) {
1737 /* Refresh fast during initialization, then become slower. */
1738 if (fast_refresh && fast_refresh-- == 0)
1739 halfdelay(10); /* switch to once per second */
1741 err = view_input(&new_view, &done, view, &views);
1745 struct tog_view *v, *prev = NULL;
1747 if (view_is_parent_view(view))
1748 prev = TAILQ_PREV(view, tog_view_list_head,
1750 else if (view->parent)
1751 prev = view->parent;
1754 view->parent->child = NULL;
1755 view->parent->focus_child = 0;
1756 /* Restore fullscreen line height. */
1757 view->parent->nlines = view->parent->lines;
1758 err = view_resize(view->parent);
1761 /* Make resized splits persist. */
1762 view_transfer_size(view->parent, view);
1764 TAILQ_REMOVE(&views, view, entry);
1766 err = view_close(view);
1771 TAILQ_FOREACH(v, &views, entry) {
1775 if (view == NULL && new_view == NULL) {
1776 /* No view has focus. Try to pick one. */
1779 else if (!TAILQ_EMPTY(&views)) {
1780 view = TAILQ_LAST(&views,
1781 tog_view_list_head);
1784 if (view->focus_child) {
1785 view->child->focussed = 1;
1793 struct tog_view *v, *t;
1794 /* Only allow one parent view per type. */
1795 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1796 if (v->type != new_view->type)
1798 TAILQ_REMOVE(&views, v, entry);
1799 err = view_close(v);
1804 TAILQ_INSERT_TAIL(&views, new_view, entry);
1808 if (view_is_parent_view(view)) {
1809 if (view->child && view->child->focussed)
1812 if (view->parent && view->parent->focussed)
1813 view = view->parent;
1815 show_panel(view->panel);
1816 if (view->child && view_is_splitscreen(view->child))
1817 show_panel(view->child->panel);
1818 if (view->parent && view_is_splitscreen(view)) {
1819 err = view->parent->show(view->parent);
1823 err = view->show(view);
1827 err = view->child->show(view->child);
1836 while (!TAILQ_EMPTY(&views)) {
1837 const struct got_error *close_err;
1838 view = TAILQ_FIRST(&views);
1839 TAILQ_REMOVE(&views, view, entry);
1840 close_err = view_close(view);
1841 if (close_err && err == NULL)
1845 errcode = pthread_mutex_unlock(&tog_mutex);
1846 if (errcode && err == NULL)
1847 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1857 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1862 /* Create newly allocated wide-character string equivalent to a byte string. */
1863 static const struct got_error *
1864 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1867 const struct got_error *err = NULL;
1870 *wlen = mbstowcs(NULL, s, 0);
1871 if (*wlen == (size_t)-1) {
1873 if (errno != EILSEQ)
1874 return got_error_from_errno("mbstowcs");
1876 /* byte string invalid in current encoding; try to "fix" it */
1877 err = got_mbsavis(&vis, &vislen, s);
1880 *wlen = mbstowcs(NULL, vis, 0);
1881 if (*wlen == (size_t)-1) {
1882 err = got_error_from_errno("mbstowcs"); /* give up */
1887 *ws = calloc(*wlen + 1, sizeof(**ws));
1889 err = got_error_from_errno("calloc");
1893 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1894 err = got_error_from_errno("mbstowcs");
1905 static const struct got_error *
1906 expand_tab(char **ptr, const char *src)
1909 size_t len, n, idx = 0, sz = 0;
1912 n = len = strlen(src);
1913 dst = malloc(n + 1);
1915 return got_error_from_errno("malloc");
1917 while (idx < len && src[idx]) {
1918 const char c = src[idx];
1921 size_t nb = TABSIZE - sz % TABSIZE;
1924 p = realloc(dst, n + nb);
1927 return got_error_from_errno("realloc");
1932 memset(dst + sz, ' ', nb);
1935 dst[sz++] = src[idx];
1945 * Advance at most n columns from wline starting at offset off.
1946 * Return the index to the first character after the span operation.
1947 * Return the combined column width of all spanned wide character in
1951 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1953 int width, i, cols = 0;
1960 for (i = off; wline[i] != L'\0'; ++i) {
1961 if (wline[i] == L'\t')
1962 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1964 width = wcwidth(wline[i]);
1971 if (cols + width > n)
1981 * Format a line for display, ensuring that it won't overflow a width limit.
1982 * With scrolling, the width returned refers to the scrolled version of the
1983 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1985 static const struct got_error *
1986 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1987 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1989 const struct got_error *err = NULL;
1991 wchar_t *wline = NULL;
2000 err = expand_tab(&exstr, line);
2005 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2010 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2012 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2013 wline[wlen - 1] = L'\0';
2016 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2017 wline[wlen - 1] = L'\0';
2021 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2027 *scrollxp = scrollx;
2035 static const struct got_error*
2036 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2037 struct got_object_id *id, struct got_repository *repo)
2039 static const struct got_error *err = NULL;
2040 struct got_reflist_entry *re;
2046 TAILQ_FOREACH(re, refs, entry) {
2047 struct got_tag_object *tag = NULL;
2048 struct got_object_id *ref_id;
2051 name = got_ref_get_name(re->ref);
2052 if (strcmp(name, GOT_REF_HEAD) == 0)
2054 if (strncmp(name, "refs/", 5) == 0)
2056 if (strncmp(name, "got/", 4) == 0 &&
2057 strncmp(name, "got/backup/", 11) != 0)
2059 if (strncmp(name, "heads/", 6) == 0)
2061 if (strncmp(name, "remotes/", 8) == 0) {
2063 s = strstr(name, "/" GOT_REF_HEAD);
2064 if (s != NULL && s[strlen(s)] == '\0')
2067 err = got_ref_resolve(&ref_id, repo, re->ref);
2070 if (strncmp(name, "tags/", 5) == 0) {
2071 err = got_object_open_as_tag(&tag, repo, ref_id);
2073 if (err->code != GOT_ERR_OBJ_TYPE) {
2077 /* Ref points at something other than a tag. */
2082 cmp = got_object_id_cmp(tag ?
2083 got_object_tag_get_object_id(tag) : ref_id, id);
2086 got_object_tag_close(tag);
2090 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2091 s ? ", " : "", name) == -1) {
2092 err = got_error_from_errno("asprintf");
2103 static const struct got_error *
2104 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2109 smallerthan = strchr(author, '<');
2110 if (smallerthan && smallerthan[1] != '\0')
2111 author = smallerthan + 1;
2112 author[strcspn(author, "@>")] = '\0';
2113 return format_line(wauthor, author_width, NULL, author, 0, limit,
2117 static const struct got_error *
2118 draw_commit(struct tog_view *view, struct got_commit_object *commit,
2119 struct got_object_id *id, const size_t date_display_cols,
2120 int author_display_cols)
2122 struct tog_log_view_state *s = &view->state.log;
2123 const struct got_error *err = NULL;
2124 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2125 char *logmsg0 = NULL, *logmsg = NULL;
2126 char *author = NULL;
2127 wchar_t *wlogmsg = NULL, *wauthor = NULL;
2128 int author_width, logmsg_width;
2129 char *newline, *line = NULL;
2130 int col, limit, scrollx;
2131 const int avail = view->ncols;
2133 time_t committer_time;
2134 struct tog_color *tc;
2136 committer_time = got_object_commit_get_committer_time(commit);
2137 if (gmtime_r(&committer_time, &tm) == NULL)
2138 return got_error_from_errno("gmtime_r");
2139 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2140 return got_error(GOT_ERR_NO_SPACE);
2142 if (avail <= date_display_cols)
2143 limit = MIN(sizeof(datebuf) - 1, avail);
2145 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2146 tc = get_color(&s->colors, TOG_COLOR_DATE);
2148 wattr_on(view->window,
2149 COLOR_PAIR(tc->colorpair), NULL);
2150 waddnstr(view->window, datebuf, limit);
2152 wattr_off(view->window,
2153 COLOR_PAIR(tc->colorpair), NULL);
2160 err = got_object_id_str(&id_str, id);
2163 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2165 wattr_on(view->window,
2166 COLOR_PAIR(tc->colorpair), NULL);
2167 wprintw(view->window, "%.8s ", id_str);
2169 wattr_off(view->window,
2170 COLOR_PAIR(tc->colorpair), NULL);
2177 if (s->use_committer)
2178 author = strdup(got_object_commit_get_committer(commit));
2180 author = strdup(got_object_commit_get_author(commit));
2181 if (author == NULL) {
2182 err = got_error_from_errno("strdup");
2185 err = format_author(&wauthor, &author_width, author, avail - col, col);
2188 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2190 wattr_on(view->window,
2191 COLOR_PAIR(tc->colorpair), NULL);
2192 waddwstr(view->window, wauthor);
2193 col += author_width;
2194 while (col < avail && author_width < author_display_cols + 2) {
2195 waddch(view->window, ' ');
2200 wattr_off(view->window,
2201 COLOR_PAIR(tc->colorpair), NULL);
2205 err = got_object_commit_get_logmsg(&logmsg0, commit);
2209 while (*logmsg == '\n')
2211 newline = strchr(logmsg, '\n');
2214 limit = avail - col;
2215 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2216 limit--; /* for the border */
2217 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2221 waddwstr(view->window, &wlogmsg[scrollx]);
2222 col += MAX(logmsg_width, 0);
2223 while (col < avail) {
2224 waddch(view->window, ' ');
2236 static struct commit_queue_entry *
2237 alloc_commit_queue_entry(struct got_commit_object *commit,
2238 struct got_object_id *id)
2240 struct commit_queue_entry *entry;
2241 struct got_object_id *dup;
2243 entry = calloc(1, sizeof(*entry));
2247 dup = got_object_id_dup(id);
2254 entry->commit = commit;
2259 pop_commit(struct commit_queue *commits)
2261 struct commit_queue_entry *entry;
2263 entry = TAILQ_FIRST(&commits->head);
2264 TAILQ_REMOVE(&commits->head, entry, entry);
2265 got_object_commit_close(entry->commit);
2266 commits->ncommits--;
2272 free_commits(struct commit_queue *commits)
2274 while (!TAILQ_EMPTY(&commits->head))
2275 pop_commit(commits);
2278 static const struct got_error *
2279 match_commit(int *have_match, struct got_object_id *id,
2280 struct got_commit_object *commit, regex_t *regex)
2282 const struct got_error *err = NULL;
2283 regmatch_t regmatch;
2284 char *id_str = NULL, *logmsg = NULL;
2288 err = got_object_id_str(&id_str, id);
2292 err = got_object_commit_get_logmsg(&logmsg, commit);
2296 if (regexec(regex, got_object_commit_get_author(commit), 1,
2297 ®match, 0) == 0 ||
2298 regexec(regex, got_object_commit_get_committer(commit), 1,
2299 ®match, 0) == 0 ||
2300 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2301 regexec(regex, logmsg, 1, ®match, 0) == 0)
2309 static const struct got_error *
2310 queue_commits(struct tog_log_thread_args *a)
2312 const struct got_error *err = NULL;
2315 * We keep all commits open throughout the lifetime of the log
2316 * view in order to avoid having to re-fetch commits from disk
2317 * while updating the display.
2320 struct got_object_id id;
2321 struct got_commit_object *commit;
2322 struct commit_queue_entry *entry;
2323 int limit_match = 0;
2326 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2331 err = got_object_open_as_commit(&commit, a->repo, &id);
2334 entry = alloc_commit_queue_entry(commit, &id);
2335 if (entry == NULL) {
2336 err = got_error_from_errno("alloc_commit_queue_entry");
2340 errcode = pthread_mutex_lock(&tog_mutex);
2342 err = got_error_set_errno(errcode,
2343 "pthread_mutex_lock");
2347 entry->idx = a->real_commits->ncommits;
2348 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2349 a->real_commits->ncommits++;
2352 err = match_commit(&limit_match, &id, commit,
2358 struct commit_queue_entry *matched;
2360 matched = alloc_commit_queue_entry(
2361 entry->commit, entry->id);
2362 if (matched == NULL) {
2363 err = got_error_from_errno(
2364 "alloc_commit_queue_entry");
2367 matched->commit = entry->commit;
2368 got_object_commit_retain(entry->commit);
2370 matched->idx = a->limit_commits->ncommits;
2371 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2373 a->limit_commits->ncommits++;
2377 * This is how we signal log_thread() that we
2378 * have found a match, and that it should be
2379 * counted as a new entry for the view.
2381 a->limit_match = limit_match;
2384 if (*a->searching == TOG_SEARCH_FORWARD &&
2385 !*a->search_next_done) {
2387 err = match_commit(&have_match, &id, commit, a->regex);
2392 if (limit_match && have_match)
2393 *a->search_next_done =
2394 TOG_SEARCH_HAVE_MORE;
2395 } else if (have_match)
2396 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2399 errcode = pthread_mutex_unlock(&tog_mutex);
2400 if (errcode && err == NULL)
2401 err = got_error_set_errno(errcode,
2402 "pthread_mutex_unlock");
2405 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2411 select_commit(struct tog_log_view_state *s)
2413 struct commit_queue_entry *entry;
2416 entry = s->first_displayed_entry;
2418 if (ncommits == s->selected) {
2419 s->selected_entry = entry;
2422 entry = TAILQ_NEXT(entry, entry);
2427 static const struct got_error *
2428 draw_commits(struct tog_view *view)
2430 const struct got_error *err = NULL;
2431 struct tog_log_view_state *s = &view->state.log;
2432 struct commit_queue_entry *entry = s->selected_entry;
2433 int limit = view->nlines;
2435 int ncommits, author_cols = 4;
2436 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2437 char *refs_str = NULL;
2439 struct tog_color *tc;
2440 static const size_t date_display_cols = 12;
2442 if (view_is_hsplit_top(view))
2443 --limit; /* account for border */
2445 if (s->selected_entry &&
2446 !(view->searching && view->search_next_done == 0)) {
2447 struct got_reflist_head *refs;
2448 err = got_object_id_str(&id_str, s->selected_entry->id);
2451 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2452 s->selected_entry->id);
2454 err = build_refs_str(&refs_str, refs,
2455 s->selected_entry->id, s->repo);
2461 if (s->thread_args.commits_needed == 0)
2462 halfdelay(10); /* disable fast refresh */
2464 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2465 if (asprintf(&ncommits_str, " [%d/%d] %s",
2466 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2467 (view->searching && !view->search_next_done) ?
2468 "searching..." : "loading...") == -1) {
2469 err = got_error_from_errno("asprintf");
2473 const char *search_str = NULL;
2474 const char *limit_str = NULL;
2476 if (view->searching) {
2477 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2478 search_str = "no more matches";
2479 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2480 search_str = "no matches found";
2481 else if (!view->search_next_done)
2482 search_str = "searching...";
2485 if (s->limit_view && s->commits->ncommits == 0)
2486 limit_str = "no matches found";
2488 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2489 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2490 search_str ? search_str : (refs_str ? refs_str : ""),
2491 limit_str ? limit_str : "") == -1) {
2492 err = got_error_from_errno("asprintf");
2497 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2498 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2499 "........................................",
2500 s->in_repo_path, ncommits_str) == -1) {
2501 err = got_error_from_errno("asprintf");
2505 } else if (asprintf(&header, "commit %s%s",
2506 id_str ? id_str : "........................................",
2507 ncommits_str) == -1) {
2508 err = got_error_from_errno("asprintf");
2512 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2516 werase(view->window);
2518 if (view_needs_focus_indication(view))
2519 wstandout(view->window);
2520 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2522 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2523 waddwstr(view->window, wline);
2524 while (width < view->ncols) {
2525 waddch(view->window, ' ');
2529 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2530 if (view_needs_focus_indication(view))
2531 wstandend(view->window);
2536 /* Grow author column size if necessary, and set view->maxx. */
2537 entry = s->first_displayed_entry;
2541 struct got_commit_object *c = entry->commit;
2542 char *author, *eol, *msg, *msg0;
2543 wchar_t *wauthor, *wmsg;
2545 if (ncommits >= limit - 1)
2547 if (s->use_committer)
2548 author = strdup(got_object_commit_get_committer(c));
2550 author = strdup(got_object_commit_get_author(c));
2551 if (author == NULL) {
2552 err = got_error_from_errno("strdup");
2555 err = format_author(&wauthor, &width, author, COLS,
2557 if (author_cols < width)
2558 author_cols = width;
2563 err = got_object_commit_get_logmsg(&msg0, c);
2567 while (*msg == '\n')
2569 if ((eol = strchr(msg, '\n')))
2571 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2572 date_display_cols + author_cols, 0);
2575 view->maxx = MAX(view->maxx, width);
2579 entry = TAILQ_NEXT(entry, entry);
2582 entry = s->first_displayed_entry;
2583 s->last_displayed_entry = s->first_displayed_entry;
2586 if (ncommits >= limit - 1)
2588 if (ncommits == s->selected)
2589 wstandout(view->window);
2590 err = draw_commit(view, entry->commit, entry->id,
2591 date_display_cols, author_cols);
2592 if (ncommits == s->selected)
2593 wstandend(view->window);
2597 s->last_displayed_entry = entry;
2598 entry = TAILQ_NEXT(entry, entry);
2611 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2613 struct commit_queue_entry *entry;
2616 entry = TAILQ_FIRST(&s->commits->head);
2617 if (s->first_displayed_entry == entry)
2620 entry = s->first_displayed_entry;
2621 while (entry && nscrolled < maxscroll) {
2622 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2624 s->first_displayed_entry = entry;
2630 static const struct got_error *
2631 trigger_log_thread(struct tog_view *view, int wait)
2633 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2636 halfdelay(1); /* fast refresh while loading commits */
2638 while (!ta->log_complete && !tog_thread_error &&
2639 (ta->commits_needed > 0 || ta->load_all)) {
2640 /* Wake the log thread. */
2641 errcode = pthread_cond_signal(&ta->need_commits);
2643 return got_error_set_errno(errcode,
2644 "pthread_cond_signal");
2647 * The mutex will be released while the view loop waits
2648 * in wgetch(), at which time the log thread will run.
2653 /* Display progress update in log view. */
2654 show_log_view(view);
2658 /* Wait right here while next commit is being loaded. */
2659 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2661 return got_error_set_errno(errcode,
2662 "pthread_cond_wait");
2664 /* Display progress update in log view. */
2665 show_log_view(view);
2673 static const struct got_error *
2674 request_log_commits(struct tog_view *view)
2676 struct tog_log_view_state *state = &view->state.log;
2677 const struct got_error *err = NULL;
2679 if (state->thread_args.log_complete)
2682 state->thread_args.commits_needed += view->nscrolled;
2683 err = trigger_log_thread(view, 1);
2684 view->nscrolled = 0;
2689 static const struct got_error *
2690 log_scroll_down(struct tog_view *view, int maxscroll)
2692 struct tog_log_view_state *s = &view->state.log;
2693 const struct got_error *err = NULL;
2694 struct commit_queue_entry *pentry;
2695 int nscrolled = 0, ncommits_needed;
2697 if (s->last_displayed_entry == NULL)
2700 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2701 if (s->commits->ncommits < ncommits_needed &&
2702 !s->thread_args.log_complete) {
2704 * Ask the log thread for required amount of commits.
2706 s->thread_args.commits_needed +=
2707 ncommits_needed - s->commits->ncommits;
2708 err = trigger_log_thread(view, 1);
2714 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2715 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2718 s->last_displayed_entry = pentry ?
2719 pentry : s->last_displayed_entry;;
2721 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2724 s->first_displayed_entry = pentry;
2725 } while (++nscrolled < maxscroll);
2727 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2728 view->nscrolled += nscrolled;
2730 view->nscrolled = 0;
2735 static const struct got_error *
2736 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2737 struct got_commit_object *commit, struct got_object_id *commit_id,
2738 struct tog_view *log_view, struct got_repository *repo)
2740 const struct got_error *err;
2741 struct got_object_qid *parent_id;
2742 struct tog_view *diff_view;
2744 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2745 if (diff_view == NULL)
2746 return got_error_from_errno("view_open");
2748 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2749 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2750 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2752 *new_view = diff_view;
2756 static const struct got_error *
2757 tree_view_visit_subtree(struct tog_tree_view_state *s,
2758 struct got_tree_object *subtree)
2760 struct tog_parent_tree *parent;
2762 parent = calloc(1, sizeof(*parent));
2764 return got_error_from_errno("calloc");
2766 parent->tree = s->tree;
2767 parent->first_displayed_entry = s->first_displayed_entry;
2768 parent->selected_entry = s->selected_entry;
2769 parent->selected = s->selected;
2770 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2773 s->first_displayed_entry = NULL;
2777 static const struct got_error *
2778 tree_view_walk_path(struct tog_tree_view_state *s,
2779 struct got_commit_object *commit, const char *path)
2781 const struct got_error *err = NULL;
2782 struct got_tree_object *tree = NULL;
2784 char *slash, *subpath = NULL;
2786 /* Walk the path and open corresponding tree objects. */
2789 struct got_tree_entry *te;
2790 struct got_object_id *tree_id;
2796 /* Ensure the correct subtree entry is selected. */
2797 slash = strchr(p, '/');
2799 te_name = strdup(p);
2801 te_name = strndup(p, slash - p);
2802 if (te_name == NULL) {
2803 err = got_error_from_errno("strndup");
2806 te = got_object_tree_find_entry(s->tree, te_name);
2808 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2813 s->first_displayed_entry = s->selected_entry = te;
2815 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2816 break; /* jump to this file's entry */
2818 slash = strchr(p, '/');
2820 subpath = strndup(path, slash - path);
2822 subpath = strdup(path);
2823 if (subpath == NULL) {
2824 err = got_error_from_errno("strdup");
2828 err = got_object_id_by_path(&tree_id, s->repo, commit,
2833 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2838 err = tree_view_visit_subtree(s, tree);
2840 got_object_tree_close(tree);
2854 static const struct got_error *
2855 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2856 struct commit_queue_entry *entry, const char *path,
2857 const char *head_ref_name, struct got_repository *repo)
2859 const struct got_error *err = NULL;
2860 struct tog_tree_view_state *s;
2861 struct tog_view *tree_view;
2863 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2864 if (tree_view == NULL)
2865 return got_error_from_errno("view_open");
2867 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2870 s = &tree_view->state.tree;
2872 *new_view = tree_view;
2874 if (got_path_is_root_dir(path))
2877 return tree_view_walk_path(s, entry->commit, path);
2880 static const struct got_error *
2881 block_signals_used_by_main_thread(void)
2886 if (sigemptyset(&sigset) == -1)
2887 return got_error_from_errno("sigemptyset");
2889 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2890 if (sigaddset(&sigset, SIGWINCH) == -1)
2891 return got_error_from_errno("sigaddset");
2892 if (sigaddset(&sigset, SIGCONT) == -1)
2893 return got_error_from_errno("sigaddset");
2894 if (sigaddset(&sigset, SIGINT) == -1)
2895 return got_error_from_errno("sigaddset");
2896 if (sigaddset(&sigset, SIGTERM) == -1)
2897 return got_error_from_errno("sigaddset");
2899 /* ncurses handles SIGTSTP */
2900 if (sigaddset(&sigset, SIGTSTP) == -1)
2901 return got_error_from_errno("sigaddset");
2903 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2905 return got_error_set_errno(errcode, "pthread_sigmask");
2911 log_thread(void *arg)
2913 const struct got_error *err = NULL;
2915 struct tog_log_thread_args *a = arg;
2919 * Sync startup with main thread such that we begin our
2920 * work once view_input() has released the mutex.
2922 errcode = pthread_mutex_lock(&tog_mutex);
2924 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2928 err = block_signals_used_by_main_thread();
2930 pthread_mutex_unlock(&tog_mutex);
2934 while (!done && !err && !tog_fatal_signal_received()) {
2935 errcode = pthread_mutex_unlock(&tog_mutex);
2937 err = got_error_set_errno(errcode,
2938 "pthread_mutex_unlock");
2941 err = queue_commits(a);
2943 if (err->code != GOT_ERR_ITER_COMPLETED)
2947 } else if (a->commits_needed > 0 && !a->load_all) {
2950 a->commits_needed--;
2952 a->commits_needed--;
2955 errcode = pthread_mutex_lock(&tog_mutex);
2957 err = got_error_set_errno(errcode,
2958 "pthread_mutex_lock");
2960 } else if (*a->quit)
2962 else if (*a->limiting && *a->first_displayed_entry == NULL) {
2963 *a->first_displayed_entry =
2964 TAILQ_FIRST(&a->limit_commits->head);
2965 *a->selected_entry = *a->first_displayed_entry;
2966 } else if (*a->first_displayed_entry == NULL) {
2967 *a->first_displayed_entry =
2968 TAILQ_FIRST(&a->real_commits->head);
2969 *a->selected_entry = *a->first_displayed_entry;
2972 errcode = pthread_cond_signal(&a->commit_loaded);
2974 err = got_error_set_errno(errcode,
2975 "pthread_cond_signal");
2976 pthread_mutex_unlock(&tog_mutex);
2981 a->commits_needed = 0;
2983 if (a->commits_needed == 0 && !a->load_all) {
2984 errcode = pthread_cond_wait(&a->need_commits,
2987 err = got_error_set_errno(errcode,
2988 "pthread_cond_wait");
2989 pthread_mutex_unlock(&tog_mutex);
2997 a->log_complete = 1;
2998 errcode = pthread_mutex_unlock(&tog_mutex);
3000 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3003 tog_thread_error = 1;
3004 pthread_cond_signal(&a->commit_loaded);
3009 static const struct got_error *
3010 stop_log_thread(struct tog_log_view_state *s)
3012 const struct got_error *err = NULL, *thread_err = NULL;
3017 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3019 return got_error_set_errno(errcode,
3020 "pthread_cond_signal");
3021 errcode = pthread_mutex_unlock(&tog_mutex);
3023 return got_error_set_errno(errcode,
3024 "pthread_mutex_unlock");
3025 errcode = pthread_join(s->thread, (void **)&thread_err);
3027 return got_error_set_errno(errcode, "pthread_join");
3028 errcode = pthread_mutex_lock(&tog_mutex);
3030 return got_error_set_errno(errcode,
3031 "pthread_mutex_lock");
3035 if (s->thread_args.repo) {
3036 err = got_repo_close(s->thread_args.repo);
3037 s->thread_args.repo = NULL;
3040 if (s->thread_args.pack_fds) {
3041 const struct got_error *pack_err =
3042 got_repo_pack_fds_close(s->thread_args.pack_fds);
3045 s->thread_args.pack_fds = NULL;
3048 if (s->thread_args.graph) {
3049 got_commit_graph_close(s->thread_args.graph);
3050 s->thread_args.graph = NULL;
3053 return err ? err : thread_err;
3056 static const struct got_error *
3057 close_log_view(struct tog_view *view)
3059 const struct got_error *err = NULL;
3060 struct tog_log_view_state *s = &view->state.log;
3063 err = stop_log_thread(s);
3065 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3066 if (errcode && err == NULL)
3067 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3069 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3070 if (errcode && err == NULL)
3071 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3073 free_commits(&s->limit_commits);
3074 free_commits(&s->real_commits);
3075 free(s->in_repo_path);
3076 s->in_repo_path = NULL;
3079 free(s->head_ref_name);
3080 s->head_ref_name = NULL;
3085 * We use two queues to implement the limit feature: first consists of
3086 * commits matching the current limit_regex; second is the real queue
3087 * of all known commits (real_commits). When the user starts limiting,
3088 * we swap queues such that all movement and displaying functionality
3089 * works with very slight change.
3091 static const struct got_error *
3092 limit_log_view(struct tog_view *view)
3094 struct tog_log_view_state *s = &view->state.log;
3095 struct commit_queue_entry *entry;
3096 struct tog_view *v = view;
3097 const struct got_error *err = NULL;
3101 if (view_is_hsplit_top(view))
3103 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3106 /* Get the pattern */
3107 wmove(v->window, v->nlines - 1, 0);
3108 wclrtoeol(v->window);
3109 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3110 nodelay(v->window, FALSE);
3113 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3116 nodelay(v->window, TRUE);
3120 if (*pattern == '\0') {
3122 * Safety measure for the situation where the user
3123 * resets limit without previously limiting anything.
3129 * User could have pressed Ctrl+L, which refreshed the
3130 * commit queues, it means we can't save previously
3131 * (before limit took place) displayed entries,
3132 * because they would point to already free'ed memory,
3133 * so we are forced to always select first entry of
3136 s->commits = &s->real_commits;
3137 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3138 s->selected_entry = s->first_displayed_entry;
3145 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3150 /* Clear the screen while loading limit view */
3151 s->first_displayed_entry = NULL;
3152 s->last_displayed_entry = NULL;
3153 s->selected_entry = NULL;
3154 s->commits = &s->limit_commits;
3156 /* Prepare limit queue for new search */
3157 free_commits(&s->limit_commits);
3158 s->limit_commits.ncommits = 0;
3160 /* First process commits, which are in queue already */
3161 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3164 err = match_commit(&have_match, entry->id,
3165 entry->commit, &s->limit_regex);
3170 struct commit_queue_entry *matched;
3172 matched = alloc_commit_queue_entry(entry->commit,
3174 if (matched == NULL) {
3175 err = got_error_from_errno(
3176 "alloc_commit_queue_entry");
3179 matched->commit = entry->commit;
3180 got_object_commit_retain(entry->commit);
3182 matched->idx = s->limit_commits.ncommits;
3183 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3185 s->limit_commits.ncommits++;
3189 /* Second process all the commits, until we fill the screen */
3190 if (s->limit_commits.ncommits < view->nlines - 1 &&
3191 !s->thread_args.log_complete) {
3192 s->thread_args.commits_needed +=
3193 view->nlines - s->limit_commits.ncommits - 1;
3194 err = trigger_log_thread(view, 1);
3199 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3200 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3206 static const struct got_error *
3207 search_start_log_view(struct tog_view *view)
3209 struct tog_log_view_state *s = &view->state.log;
3211 s->matched_entry = NULL;
3212 s->search_entry = NULL;
3216 static const struct got_error *
3217 search_next_log_view(struct tog_view *view)
3219 const struct got_error *err = NULL;
3220 struct tog_log_view_state *s = &view->state.log;
3221 struct commit_queue_entry *entry;
3223 /* Display progress update in log view. */
3224 show_log_view(view);
3228 if (s->search_entry) {
3230 errcode = pthread_mutex_unlock(&tog_mutex);
3232 return got_error_set_errno(errcode,
3233 "pthread_mutex_unlock");
3234 ch = wgetch(view->window);
3235 errcode = pthread_mutex_lock(&tog_mutex);
3237 return got_error_set_errno(errcode,
3238 "pthread_mutex_lock");
3239 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3240 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3243 if (view->searching == TOG_SEARCH_FORWARD)
3244 entry = TAILQ_NEXT(s->search_entry, entry);
3246 entry = TAILQ_PREV(s->search_entry,
3247 commit_queue_head, entry);
3248 } else if (s->matched_entry) {
3250 * If the user has moved the cursor after we hit a match,
3251 * the position from where we should continue searching
3252 * might have changed.
3254 if (view->searching == TOG_SEARCH_FORWARD)
3255 entry = TAILQ_NEXT(s->selected_entry, entry);
3257 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3260 entry = s->selected_entry;
3266 if (entry == NULL) {
3267 if (s->thread_args.log_complete ||
3268 view->searching == TOG_SEARCH_BACKWARD) {
3269 view->search_next_done =
3270 (s->matched_entry == NULL ?
3271 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3272 s->search_entry = NULL;
3276 * Poke the log thread for more commits and return,
3277 * allowing the main loop to make progress. Search
3278 * will resume at s->search_entry once we come back.
3280 s->thread_args.commits_needed++;
3281 return trigger_log_thread(view, 0);
3284 err = match_commit(&have_match, entry->id, entry->commit,
3289 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3290 s->matched_entry = entry;
3294 s->search_entry = entry;
3295 if (view->searching == TOG_SEARCH_FORWARD)
3296 entry = TAILQ_NEXT(entry, entry);
3298 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3301 if (s->matched_entry) {
3302 int cur = s->selected_entry->idx;
3303 while (cur < s->matched_entry->idx) {
3304 err = input_log_view(NULL, view, KEY_DOWN);
3309 while (cur > s->matched_entry->idx) {
3310 err = input_log_view(NULL, view, KEY_UP);
3317 s->search_entry = NULL;
3322 static const struct got_error *
3323 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3324 struct got_repository *repo, const char *head_ref_name,
3325 const char *in_repo_path, int log_branches)
3327 const struct got_error *err = NULL;
3328 struct tog_log_view_state *s = &view->state.log;
3329 struct got_repository *thread_repo = NULL;
3330 struct got_commit_graph *thread_graph = NULL;
3333 if (in_repo_path != s->in_repo_path) {
3334 free(s->in_repo_path);
3335 s->in_repo_path = strdup(in_repo_path);
3336 if (s->in_repo_path == NULL)
3337 return got_error_from_errno("strdup");
3340 /* The commit queue only contains commits being displayed. */
3341 TAILQ_INIT(&s->real_commits.head);
3342 s->real_commits.ncommits = 0;
3343 s->commits = &s->real_commits;
3345 TAILQ_INIT(&s->limit_commits.head);
3347 s->limit_commits.ncommits = 0;
3350 if (head_ref_name) {
3351 s->head_ref_name = strdup(head_ref_name);
3352 if (s->head_ref_name == NULL) {
3353 err = got_error_from_errno("strdup");
3357 s->start_id = got_object_id_dup(start_id);
3358 if (s->start_id == NULL) {
3359 err = got_error_from_errno("got_object_id_dup");
3362 s->log_branches = log_branches;
3364 STAILQ_INIT(&s->colors);
3365 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3366 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3367 get_color_value("TOG_COLOR_COMMIT"));
3370 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3371 get_color_value("TOG_COLOR_AUTHOR"));
3373 free_colors(&s->colors);
3376 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3377 get_color_value("TOG_COLOR_DATE"));
3379 free_colors(&s->colors);
3384 view->show = show_log_view;
3385 view->input = input_log_view;
3386 view->resize = resize_log_view;
3387 view->close = close_log_view;
3388 view->search_start = search_start_log_view;
3389 view->search_next = search_next_log_view;
3391 if (s->thread_args.pack_fds == NULL) {
3392 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3396 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3397 s->thread_args.pack_fds);
3400 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3404 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3405 s->repo, NULL, NULL);
3409 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3411 err = got_error_set_errno(errcode, "pthread_cond_init");
3414 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3416 err = got_error_set_errno(errcode, "pthread_cond_init");
3420 s->thread_args.commits_needed = view->nlines;
3421 s->thread_args.graph = thread_graph;
3422 s->thread_args.real_commits = &s->real_commits;
3423 s->thread_args.limit_commits = &s->limit_commits;
3424 s->thread_args.in_repo_path = s->in_repo_path;
3425 s->thread_args.start_id = s->start_id;
3426 s->thread_args.repo = thread_repo;
3427 s->thread_args.log_complete = 0;
3428 s->thread_args.quit = &s->quit;
3429 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3430 s->thread_args.selected_entry = &s->selected_entry;
3431 s->thread_args.searching = &view->searching;
3432 s->thread_args.search_next_done = &view->search_next_done;
3433 s->thread_args.regex = &view->regex;
3434 s->thread_args.limiting = &s->limit_view;
3435 s->thread_args.limit_regex = &s->limit_regex;
3436 s->thread_args.limit_commits = &s->limit_commits;
3439 close_log_view(view);
3443 static const struct got_error *
3444 show_log_view(struct tog_view *view)
3446 const struct got_error *err;
3447 struct tog_log_view_state *s = &view->state.log;
3449 if (s->thread == NULL) {
3450 int errcode = pthread_create(&s->thread, NULL, log_thread,
3453 return got_error_set_errno(errcode, "pthread_create");
3454 if (s->thread_args.commits_needed > 0) {
3455 err = trigger_log_thread(view, 1);
3461 return draw_commits(view);
3465 log_move_cursor_up(struct tog_view *view, int page, int home)
3467 struct tog_log_view_state *s = &view->state.log;
3469 if (s->first_displayed_entry == NULL)
3471 if (s->selected_entry->idx == 0)
3474 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3476 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3478 if (!page && !home && s->selected > 0)
3481 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3487 static const struct got_error *
3488 log_move_cursor_down(struct tog_view *view, int page)
3490 struct tog_log_view_state *s = &view->state.log;
3491 const struct got_error *err = NULL;
3492 int eos = view->nlines - 2;
3494 if (s->first_displayed_entry == NULL)
3497 if (s->thread_args.log_complete &&
3498 s->selected_entry->idx >= s->commits->ncommits - 1)
3501 if (view_is_hsplit_top(view))
3502 --eos; /* border consumes the last line */
3505 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3508 err = log_scroll_down(view, 1);
3509 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3510 struct commit_queue_entry *entry;
3514 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3515 s->last_displayed_entry = entry;
3516 for (n = 0; n <= eos; n++) {
3519 s->first_displayed_entry = entry;
3520 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3523 s->selected = n - 1;
3525 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3526 s->thread_args.log_complete)
3527 s->selected += MIN(page,
3528 s->commits->ncommits - s->selected_entry->idx - 1);
3530 err = log_scroll_down(view, page);
3536 * We might necessarily overshoot in horizontal
3537 * splits; if so, select the last displayed commit.
3539 if (s->first_displayed_entry && s->last_displayed_entry) {
3540 s->selected = MIN(s->selected,
3541 s->last_displayed_entry->idx -
3542 s->first_displayed_entry->idx);
3547 if (s->thread_args.log_complete &&
3548 s->selected_entry->idx == s->commits->ncommits - 1)
3555 view_get_split(struct tog_view *view, int *y, int *x)
3560 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3561 if (view->child && view->child->resized_y)
3562 *y = view->child->resized_y;
3563 else if (view->resized_y)
3564 *y = view->resized_y;
3566 *y = view_split_begin_y(view->lines);
3567 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3568 if (view->child && view->child->resized_x)
3569 *x = view->child->resized_x;
3570 else if (view->resized_x)
3571 *x = view->resized_x;
3573 *x = view_split_begin_x(view->begin_x);
3577 /* Split view horizontally at y and offset view->state->selected line. */
3578 static const struct got_error *
3579 view_init_hsplit(struct tog_view *view, int y)
3581 const struct got_error *err = NULL;
3585 err = view_resize(view);
3589 err = offset_selection_down(view);
3594 static const struct got_error *
3595 log_goto_line(struct tog_view *view, int nlines)
3597 const struct got_error *err = NULL;
3598 struct tog_log_view_state *s = &view->state.log;
3599 int g, idx = s->selected_entry->idx;
3601 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3607 if (g >= s->first_displayed_entry->idx + 1 &&
3608 g <= s->last_displayed_entry->idx + 1 &&
3609 g - s->first_displayed_entry->idx - 1 < nlines) {
3610 s->selected = g - s->first_displayed_entry->idx - 1;
3616 err = log_move_cursor_down(view, g - idx - 1);
3617 if (!err && g > s->selected_entry->idx + 1)
3618 err = log_move_cursor_down(view,
3619 g - s->first_displayed_entry->idx - 1);
3622 } else if (idx + 1 > g)
3623 log_move_cursor_up(view, idx - g + 1, 0);
3625 if (g < nlines && s->first_displayed_entry->idx == 0)
3626 s->selected = g - 1;
3633 static const struct got_error *
3634 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3636 const struct got_error *err = NULL;
3637 struct tog_log_view_state *s = &view->state.log;
3640 if (s->thread_args.load_all) {
3641 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3642 s->thread_args.load_all = 0;
3643 else if (s->thread_args.log_complete) {
3644 err = log_move_cursor_down(view, s->commits->ncommits);
3645 s->thread_args.load_all = 0;
3651 eos = nscroll = view->nlines - 1;
3652 if (view_is_hsplit_top(view))
3656 return log_goto_line(view, eos);
3660 err = limit_log_view(view);
3669 view->x = MAX(view->maxx - view->ncols / 2, 0);
3674 if (view->x + view->ncols / 2 < view->maxx)
3675 view->x += 2; /* move two columns right */
3681 view->x -= MIN(view->x, 2); /* move two columns back */
3690 log_move_cursor_up(view, 0, 0);
3694 log_move_cursor_up(view, 0, 1);
3704 log_move_cursor_up(view, nscroll, 0);
3711 err = log_move_cursor_down(view, 0);
3714 s->use_committer = !s->use_committer;
3718 /* We don't know yet how many commits, so we're forced to
3719 * traverse them all. */
3721 s->thread_args.load_all = 1;
3722 if (!s->thread_args.log_complete)
3723 return trigger_log_thread(view, 0);
3724 err = log_move_cursor_down(view, s->commits->ncommits);
3725 s->thread_args.load_all = 0;
3736 err = log_move_cursor_down(view, nscroll);
3739 if (s->selected > view->nlines - 2)
3740 s->selected = view->nlines - 2;
3741 if (s->selected > s->commits->ncommits - 1)
3742 s->selected = s->commits->ncommits - 1;
3744 if (s->commits->ncommits < view->nlines - 1 &&
3745 !s->thread_args.log_complete) {
3746 s->thread_args.commits_needed += (view->nlines - 1) -
3747 s->commits->ncommits;
3748 err = trigger_log_thread(view, 1);
3754 if (s->selected_entry == NULL)
3756 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3760 if (s->selected_entry == NULL)
3762 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3768 if (ch == KEY_BACKSPACE &&
3769 got_path_is_root_dir(s->in_repo_path))
3771 err = stop_log_thread(s);
3774 if (ch == KEY_BACKSPACE) {
3776 err = got_path_dirname(&parent_path, s->in_repo_path);
3779 free(s->in_repo_path);
3780 s->in_repo_path = parent_path;
3781 s->thread_args.in_repo_path = s->in_repo_path;
3782 } else if (ch == CTRL('l')) {
3783 struct got_object_id *start_id;
3784 err = got_repo_match_object_id(&start_id, NULL,
3785 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3786 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3788 if (s->head_ref_name == NULL ||
3789 err->code != GOT_ERR_NOT_REF)
3791 /* Try to cope with deleted references. */
3792 free(s->head_ref_name);
3793 s->head_ref_name = NULL;
3794 err = got_repo_match_object_id(&start_id,
3795 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3796 &tog_refs, s->repo);
3801 s->start_id = start_id;
3802 s->thread_args.start_id = s->start_id;
3804 s->log_branches = !s->log_branches;
3806 if (s->thread_args.pack_fds == NULL) {
3807 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3811 err = got_repo_open(&s->thread_args.repo,
3812 got_repo_get_path(s->repo), NULL,
3813 s->thread_args.pack_fds);
3817 err = tog_load_refs(s->repo, 0);
3820 err = got_commit_graph_open(&s->thread_args.graph,
3821 s->in_repo_path, !s->log_branches);
3824 err = got_commit_graph_iter_start(s->thread_args.graph,
3825 s->start_id, s->repo, NULL, NULL);
3828 free_commits(&s->real_commits);
3829 free_commits(&s->limit_commits);
3830 s->first_displayed_entry = NULL;
3831 s->last_displayed_entry = NULL;
3832 s->selected_entry = NULL;
3834 s->thread_args.log_complete = 0;
3836 s->thread_args.commits_needed = view->lines;
3837 s->matched_entry = NULL;
3838 s->search_entry = NULL;
3843 err = view_request_new(new_view, view, TOG_VIEW_REF);
3853 static const struct got_error *
3854 apply_unveil(const char *repo_path, const char *worktree_path)
3856 const struct got_error *error;
3859 if (unveil("gmon.out", "rwc") != 0)
3860 return got_error_from_errno2("unveil", "gmon.out");
3862 if (repo_path && unveil(repo_path, "r") != 0)
3863 return got_error_from_errno2("unveil", repo_path);
3865 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3866 return got_error_from_errno2("unveil", worktree_path);
3868 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3869 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3871 error = got_privsep_unveil_exec_helpers();
3875 if (unveil(NULL, NULL) != 0)
3876 return got_error_from_errno("unveil");
3885 * Override default signal handlers before starting ncurses.
3886 * This should prevent ncurses from installing its own
3887 * broken cleanup() signal handler.
3889 signal(SIGWINCH, tog_sigwinch);
3890 signal(SIGPIPE, tog_sigpipe);
3891 signal(SIGCONT, tog_sigcont);
3892 signal(SIGINT, tog_sigint);
3893 signal(SIGTERM, tog_sigterm);
3897 halfdelay(1); /* Do fast refresh while initial view is loading. */
3900 intrflush(stdscr, FALSE);
3901 keypad(stdscr, TRUE);
3903 if (getenv("TOG_COLORS") != NULL) {
3905 use_default_colors();
3909 static const struct got_error *
3910 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3911 struct got_repository *repo, struct got_worktree *worktree)
3913 const struct got_error *err = NULL;
3916 *in_repo_path = strdup("/");
3917 if (*in_repo_path == NULL)
3918 return got_error_from_errno("strdup");
3923 const char *prefix = got_worktree_get_path_prefix(worktree);
3926 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3929 if (asprintf(in_repo_path, "%s%s%s", prefix,
3930 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3932 err = got_error_from_errno("asprintf");
3933 *in_repo_path = NULL;
3937 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3942 static const struct got_error *
3943 cmd_log(int argc, char *argv[])
3945 const struct got_error *error;
3946 struct got_repository *repo = NULL;
3947 struct got_worktree *worktree = NULL;
3948 struct got_object_id *start_id = NULL;
3949 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3950 char *start_commit = NULL, *label = NULL;
3951 struct got_reference *ref = NULL;
3952 const char *head_ref_name = NULL;
3953 int ch, log_branches = 0;
3954 struct tog_view *view;
3955 int *pack_fds = NULL;
3957 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3963 start_commit = optarg;
3966 repo_path = realpath(optarg, NULL);
3967 if (repo_path == NULL)
3968 return got_error_from_errno2("realpath",
3983 error = got_repo_pack_fds_open(&pack_fds);
3987 if (repo_path == NULL) {
3988 cwd = getcwd(NULL, 0);
3990 return got_error_from_errno("getcwd");
3991 error = got_worktree_open(&worktree, cwd);
3992 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3996 strdup(got_worktree_get_repo_path(worktree));
3998 repo_path = strdup(cwd);
3999 if (repo_path == NULL) {
4000 error = got_error_from_errno("strdup");
4005 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4009 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4016 error = apply_unveil(got_repo_get_path(repo),
4017 worktree ? got_worktree_get_root_path(worktree) : NULL);
4021 /* already loaded by tog_log_with_path()? */
4022 if (TAILQ_EMPTY(&tog_refs)) {
4023 error = tog_load_refs(repo, 0);
4028 if (start_commit == NULL) {
4029 error = got_repo_match_object_id(&start_id, &label,
4030 worktree ? got_worktree_get_head_ref_name(worktree) :
4031 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4034 head_ref_name = label;
4036 error = got_ref_open(&ref, repo, start_commit, 0);
4038 head_ref_name = got_ref_get_name(ref);
4039 else if (error->code != GOT_ERR_NOT_REF)
4041 error = got_repo_match_object_id(&start_id, NULL,
4042 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4047 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4049 error = got_error_from_errno("view_open");
4052 error = open_log_view(view, start_id, repo, head_ref_name,
4053 in_repo_path, log_branches);
4057 /* Release work tree lock. */
4058 got_worktree_close(worktree);
4061 error = view_loop(view);
4071 const struct got_error *close_err = got_repo_close(repo);
4076 got_worktree_close(worktree);
4078 const struct got_error *pack_err =
4079 got_repo_pack_fds_close(pack_fds);
4091 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4092 "object1 object2\n", getprogname());
4097 match_line(const char *line, regex_t *regex, size_t nmatch,
4098 regmatch_t *regmatch)
4100 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4103 static struct tog_color *
4104 match_color(struct tog_colors *colors, const char *line)
4106 struct tog_color *tc = NULL;
4108 STAILQ_FOREACH(tc, colors, entry) {
4109 if (match_line(line, &tc->regex, 0, NULL))
4116 static const struct got_error *
4117 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4118 WINDOW *window, int skipcol, regmatch_t *regmatch)
4120 const struct got_error *err = NULL;
4122 wchar_t *wline = NULL;
4123 int rme, rms, n, width, scrollx;
4124 int width0 = 0, width1 = 0, width2 = 0;
4125 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4129 rms = regmatch->rm_so;
4130 rme = regmatch->rm_eo;
4132 err = expand_tab(&exstr, line);
4136 /* Split the line into 3 segments, according to match offsets. */
4137 seg0 = strndup(exstr, rms);
4139 err = got_error_from_errno("strndup");
4142 seg1 = strndup(exstr + rms, rme - rms);
4144 err = got_error_from_errno("strndup");
4147 seg2 = strdup(exstr + rme);
4149 err = got_error_from_errno("strndup");
4153 /* draw up to matched token if we haven't scrolled past it */
4154 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4158 n = MAX(width0 - skipcol, 0);
4161 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4162 wlimit, col_tab_align, 1);
4165 waddwstr(window, &wline[scrollx]);
4175 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4179 wlen = wcslen(wline);
4181 width = wcwidth(wline[i]);
4183 /* should not happen, tabs are expanded */
4184 err = got_error(GOT_ERR_RANGE);
4187 if (width0 + w + width > skipcol)
4192 /* draw (visible part of) matched token (if scrolled into it) */
4193 if (width1 - w > 0) {
4194 wattron(window, A_STANDOUT);
4195 waddwstr(window, &wline[i]);
4196 wattroff(window, A_STANDOUT);
4197 wlimit -= (width1 - w);
4198 *wtotal += (width1 - w);
4202 if (wlimit > 0) { /* draw rest of line */
4204 if (skipcol > width0 + width1) {
4205 err = format_line(&wline, &width2, &scrollx, seg2,
4206 skipcol - (width0 + width1), wlimit,
4210 waddwstr(window, &wline[scrollx]);
4212 err = format_line(&wline, &width2, NULL, seg2, 0,
4213 wlimit, col_tab_align, 1);
4216 waddwstr(window, wline);
4230 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4233 int *eof, *first, *selected;
4235 if (view->type == TOG_VIEW_DIFF) {
4236 struct tog_diff_view_state *s = &view->state.diff;
4238 first = &s->first_displayed_line;
4242 } else if (view->type == TOG_VIEW_HELP) {
4243 struct tog_help_view_state *s = &view->state.help;
4245 first = &s->first_displayed_line;
4249 } else if (view->type == TOG_VIEW_BLAME) {
4250 struct tog_blame_view_state *s = &view->state.blame;
4252 first = &s->first_displayed_line;
4253 selected = &s->selected_line;
4259 /* Center gline in the middle of the page like vi(1). */
4260 if (*lineno < view->gline - (view->nlines - 3) / 2)
4262 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4271 *selected = view->gline <= (view->nlines - 3) / 2 ?
4272 view->gline : (view->nlines - 3) / 2 + 1;
4278 static const struct got_error *
4279 draw_file(struct tog_view *view, const char *header)
4281 struct tog_diff_view_state *s = &view->state.diff;
4282 regmatch_t *regmatch = &view->regmatch;
4283 const struct got_error *err;
4286 size_t linesize = 0;
4290 int max_lines = view->nlines;
4291 int nlines = s->nlines;
4294 s->lineno = s->first_displayed_line - 1;
4295 line_offset = s->lines[s->first_displayed_line - 1].offset;
4296 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4297 return got_error_from_errno("fseek");
4299 werase(view->window);
4301 if (view->gline > s->nlines - 1)
4302 view->gline = s->nlines - 1;
4305 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4306 1 : view->gline - (view->nlines - 3) / 2 :
4307 s->lineno + s->selected_line;
4309 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4310 return got_error_from_errno("asprintf");
4311 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4317 if (view_needs_focus_indication(view))
4318 wstandout(view->window);
4319 waddwstr(view->window, wline);
4322 while (width++ < view->ncols)
4323 waddch(view->window, ' ');
4324 if (view_needs_focus_indication(view))
4325 wstandend(view->window);
4335 while (max_lines > 0 && nprinted < max_lines) {
4336 enum got_diff_line_type linetype;
4339 linelen = getline(&line, &linesize, s->f);
4340 if (linelen == -1) {
4346 return got_ferror(s->f, GOT_ERR_IO);
4349 if (++s->lineno < s->first_displayed_line)
4351 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4353 if (s->lineno == view->hiline)
4356 /* Set view->maxx based on full line length. */
4357 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4363 view->maxx = MAX(view->maxx, width);
4367 linetype = s->lines[s->lineno].type;
4368 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4369 linetype < GOT_DIFF_LINE_CONTEXT)
4370 attr |= COLOR_PAIR(linetype);
4372 wattron(view->window, attr);
4373 if (s->first_displayed_line + nprinted == s->matched_line &&
4374 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4375 err = add_matched_line(&width, line, view->ncols, 0,
4376 view->window, view->x, regmatch);
4383 err = format_line(&wline, &width, &skip, line,
4384 view->x, view->ncols, 0, view->x ? 1 : 0);
4389 waddwstr(view->window, &wline[skip]);
4393 if (s->lineno == view->hiline) {
4394 /* highlight full gline length */
4395 while (width++ < view->ncols)
4396 waddch(view->window, ' ');
4398 if (width <= view->ncols - 1)
4399 waddch(view->window, '\n');
4402 wattroff(view->window, attr);
4403 if (++nprinted == 1)
4404 s->first_displayed_line = s->lineno;
4408 s->last_displayed_line = s->first_displayed_line +
4411 s->last_displayed_line = s->first_displayed_line;
4416 while (nprinted < view->nlines) {
4417 waddch(view->window, '\n');
4421 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4427 wstandout(view->window);
4428 waddwstr(view->window, wline);
4431 wstandend(view->window);
4438 get_datestr(time_t *time, char *datebuf)
4440 struct tm mytm, *tm;
4443 tm = gmtime_r(time, &mytm);
4446 s = asctime_r(tm, datebuf);
4449 p = strchr(s, '\n');
4455 static const struct got_error *
4456 get_changed_paths(struct got_pathlist_head *paths,
4457 struct got_commit_object *commit, struct got_repository *repo)
4459 const struct got_error *err = NULL;
4460 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4461 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4462 struct got_object_qid *qid;
4464 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4466 struct got_commit_object *pcommit;
4467 err = got_object_open_as_commit(&pcommit, repo,
4472 tree_id1 = got_object_id_dup(
4473 got_object_commit_get_tree_id(pcommit));
4474 if (tree_id1 == NULL) {
4475 got_object_commit_close(pcommit);
4476 return got_error_from_errno("got_object_id_dup");
4478 got_object_commit_close(pcommit);
4483 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4488 tree_id2 = got_object_commit_get_tree_id(commit);
4489 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4493 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4494 got_diff_tree_collect_changed_paths, paths, 0);
4497 got_object_tree_close(tree1);
4499 got_object_tree_close(tree2);
4504 static const struct got_error *
4505 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4506 off_t off, uint8_t type)
4508 struct got_diff_line *p;
4510 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4512 return got_error_from_errno("reallocarray");
4514 (*lines)[*nlines].offset = off;
4515 (*lines)[*nlines].type = type;
4521 static const struct got_error *
4522 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4523 struct got_object_id *commit_id, struct got_reflist_head *refs,
4524 struct got_repository *repo, FILE *outfile)
4526 const struct got_error *err = NULL;
4527 char datebuf[26], *datestr;
4528 struct got_commit_object *commit;
4529 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4530 time_t committer_time;
4531 const char *author, *committer;
4532 char *refs_str = NULL;
4533 struct got_pathlist_head changed_paths;
4534 struct got_pathlist_entry *pe;
4538 TAILQ_INIT(&changed_paths);
4541 err = build_refs_str(&refs_str, refs, commit_id, repo);
4546 err = got_object_open_as_commit(&commit, repo, commit_id);
4550 err = got_object_id_str(&id_str, commit_id);
4552 err = got_error_from_errno("got_object_id_str");
4556 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4560 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4561 refs_str ? refs_str : "", refs_str ? ")" : "");
4563 err = got_error_from_errno("fprintf");
4567 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4571 n = fprintf(outfile, "from: %s\n",
4572 got_object_commit_get_author(commit));
4574 err = got_error_from_errno("fprintf");
4578 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4582 committer_time = got_object_commit_get_committer_time(commit);
4583 datestr = get_datestr(&committer_time, datebuf);
4585 n = fprintf(outfile, "date: %s UTC\n", datestr);
4587 err = got_error_from_errno("fprintf");
4591 err = add_line_metadata(lines, nlines, outoff,
4592 GOT_DIFF_LINE_DATE);
4596 author = got_object_commit_get_author(commit);
4597 committer = got_object_commit_get_committer(commit);
4598 if (strcmp(author, committer) != 0) {
4599 n = fprintf(outfile, "via: %s\n", committer);
4601 err = got_error_from_errno("fprintf");
4605 err = add_line_metadata(lines, nlines, outoff,
4606 GOT_DIFF_LINE_AUTHOR);
4610 if (got_object_commit_get_nparents(commit) > 1) {
4611 const struct got_object_id_queue *parent_ids;
4612 struct got_object_qid *qid;
4614 parent_ids = got_object_commit_get_parent_ids(commit);
4615 STAILQ_FOREACH(qid, parent_ids, entry) {
4616 err = got_object_id_str(&id_str, &qid->id);
4619 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4621 err = got_error_from_errno("fprintf");
4625 err = add_line_metadata(lines, nlines, outoff,
4626 GOT_DIFF_LINE_META);
4634 err = got_object_commit_get_logmsg(&logmsg, commit);
4638 while ((line = strsep(&s, "\n")) != NULL) {
4639 n = fprintf(outfile, "%s\n", line);
4641 err = got_error_from_errno("fprintf");
4645 err = add_line_metadata(lines, nlines, outoff,
4646 GOT_DIFF_LINE_LOGMSG);
4651 err = get_changed_paths(&changed_paths, commit, repo);
4654 TAILQ_FOREACH(pe, &changed_paths, entry) {
4655 struct got_diff_changed_path *cp = pe->data;
4656 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4658 err = got_error_from_errno("fprintf");
4662 err = add_line_metadata(lines, nlines, outoff,
4663 GOT_DIFF_LINE_CHANGES);
4666 free((char *)pe->path);
4670 fputc('\n', outfile);
4672 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4674 got_pathlist_free(&changed_paths);
4678 got_object_commit_close(commit);
4687 static const struct got_error *
4688 create_diff(struct tog_diff_view_state *s)
4690 const struct got_error *err = NULL;
4695 s->lines = malloc(sizeof(*s->lines));
4696 if (s->lines == NULL)
4697 return got_error_from_errno("malloc");
4702 err = got_error_from_errno("got_opentemp");
4705 if (s->f && fclose(s->f) == EOF) {
4706 err = got_error_from_errno("fclose");
4712 err = got_object_get_type(&obj_type, s->repo, s->id1);
4714 err = got_object_get_type(&obj_type, s->repo, s->id2);
4719 case GOT_OBJ_TYPE_BLOB:
4720 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4721 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4722 s->label1, s->label2, tog_diff_algo, s->diff_context,
4723 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4725 case GOT_OBJ_TYPE_TREE:
4726 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4727 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4728 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4729 s->force_text_diff, s->repo, s->f);
4731 case GOT_OBJ_TYPE_COMMIT: {
4732 const struct got_object_id_queue *parent_ids;
4733 struct got_object_qid *pid;
4734 struct got_commit_object *commit2;
4735 struct got_reflist_head *refs;
4737 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4740 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4741 /* Show commit info if we're diffing to a parent/root commit. */
4742 if (s->id1 == NULL) {
4743 err = write_commit_info(&s->lines, &s->nlines, s->id2,
4744 refs, s->repo, s->f);
4748 parent_ids = got_object_commit_get_parent_ids(commit2);
4749 STAILQ_FOREACH(pid, parent_ids, entry) {
4750 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4751 err = write_commit_info(&s->lines,
4752 &s->nlines, s->id2, refs, s->repo,
4760 got_object_commit_close(commit2);
4762 err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4763 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4764 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4765 s->force_text_diff, s->repo, s->f);
4769 err = got_error(GOT_ERR_OBJ_TYPE);
4773 if (s->f && fflush(s->f) != 0 && err == NULL)
4774 err = got_error_from_errno("fflush");
4779 diff_view_indicate_progress(struct tog_view *view)
4781 mvwaddstr(view->window, 0, 0, "diffing...");
4786 static const struct got_error *
4787 search_start_diff_view(struct tog_view *view)
4789 struct tog_diff_view_state *s = &view->state.diff;
4791 s->matched_line = 0;
4796 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4797 size_t *nlines, int **first, int **last, int **match, int **selected)
4799 struct tog_diff_view_state *s = &view->state.diff;
4802 *nlines = s->nlines;
4803 *line_offsets = NULL;
4804 *match = &s->matched_line;
4805 *first = &s->first_displayed_line;
4806 *last = &s->last_displayed_line;
4807 *selected = &s->selected_line;
4810 static const struct got_error *
4811 search_next_view_match(struct tog_view *view)
4813 const struct got_error *err = NULL;
4817 size_t linesize = 0;
4819 off_t *line_offsets;
4821 int *first, *last, *match, *selected;
4823 if (!view->search_setup)
4824 return got_error_msg(GOT_ERR_NOT_IMPL,
4825 "view search not supported");
4826 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4829 if (!view->searching) {
4830 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4835 if (view->searching == TOG_SEARCH_FORWARD)
4836 lineno = *match + 1;
4838 lineno = *match - 1;
4840 lineno = *first - 1 + *selected;
4845 if (lineno <= 0 || lineno > nlines) {
4847 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4851 if (view->searching == TOG_SEARCH_FORWARD)
4857 offset = view->type == TOG_VIEW_DIFF ?
4858 view->state.diff.lines[lineno - 1].offset :
4859 line_offsets[lineno - 1];
4860 if (fseeko(f, offset, SEEK_SET) != 0) {
4862 return got_error_from_errno("fseeko");
4864 linelen = getline(&line, &linesize, f);
4865 if (linelen != -1) {
4867 err = expand_tab(&exstr, line);
4870 if (match_line(exstr, &view->regex, 1,
4872 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4879 if (view->searching == TOG_SEARCH_FORWARD)
4894 static const struct got_error *
4895 close_diff_view(struct tog_view *view)
4897 const struct got_error *err = NULL;
4898 struct tog_diff_view_state *s = &view->state.diff;
4904 if (s->f && fclose(s->f) == EOF)
4905 err = got_error_from_errno("fclose");
4907 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4908 err = got_error_from_errno("fclose");
4910 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4911 err = got_error_from_errno("fclose");
4913 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4914 err = got_error_from_errno("close");
4916 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4917 err = got_error_from_errno("close");
4925 static const struct got_error *
4926 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4927 struct got_object_id *id2, const char *label1, const char *label2,
4928 int diff_context, int ignore_whitespace, int force_text_diff,
4929 struct tog_view *parent_view, struct got_repository *repo)
4931 const struct got_error *err;
4932 struct tog_diff_view_state *s = &view->state.diff;
4934 memset(s, 0, sizeof(*s));
4938 if (id1 != NULL && id2 != NULL) {
4940 err = got_object_get_type(&type1, repo, id1);
4943 err = got_object_get_type(&type2, repo, id2);
4948 return got_error(GOT_ERR_OBJ_TYPE);
4950 s->first_displayed_line = 1;
4951 s->last_displayed_line = view->nlines;
4952 s->selected_line = 1;
4960 s->id1 = got_object_id_dup(id1);
4962 return got_error_from_errno("got_object_id_dup");
4966 s->id2 = got_object_id_dup(id2);
4967 if (s->id2 == NULL) {
4968 err = got_error_from_errno("got_object_id_dup");
4972 s->f1 = got_opentemp();
4973 if (s->f1 == NULL) {
4974 err = got_error_from_errno("got_opentemp");
4978 s->f2 = got_opentemp();
4979 if (s->f2 == NULL) {
4980 err = got_error_from_errno("got_opentemp");
4984 s->fd1 = got_opentempfd();
4986 err = got_error_from_errno("got_opentempfd");
4990 s->fd2 = got_opentempfd();
4992 err = got_error_from_errno("got_opentempfd");
4996 s->first_displayed_line = 1;
4997 s->last_displayed_line = view->nlines;
4998 s->diff_context = diff_context;
4999 s->ignore_whitespace = ignore_whitespace;
5000 s->force_text_diff = force_text_diff;
5001 s->parent_view = parent_view;
5004 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5007 rc = init_pair(GOT_DIFF_LINE_MINUS,
5008 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5010 rc = init_pair(GOT_DIFF_LINE_PLUS,
5011 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5013 rc = init_pair(GOT_DIFF_LINE_HUNK,
5014 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5016 rc = init_pair(GOT_DIFF_LINE_META,
5017 get_color_value("TOG_COLOR_DIFF_META"), -1);
5019 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5020 get_color_value("TOG_COLOR_DIFF_META"), -1);
5022 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5023 get_color_value("TOG_COLOR_DIFF_META"), -1);
5025 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5026 get_color_value("TOG_COLOR_DIFF_META"), -1);
5028 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5029 get_color_value("TOG_COLOR_AUTHOR"), -1);
5031 rc = init_pair(GOT_DIFF_LINE_DATE,
5032 get_color_value("TOG_COLOR_DATE"), -1);
5034 err = got_error(GOT_ERR_RANGE);
5039 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5040 view_is_splitscreen(view))
5041 show_log_view(parent_view); /* draw border */
5042 diff_view_indicate_progress(view);
5044 err = create_diff(s);
5046 view->show = show_diff_view;
5047 view->input = input_diff_view;
5048 view->reset = reset_diff_view;
5049 view->close = close_diff_view;
5050 view->search_start = search_start_diff_view;
5051 view->search_setup = search_setup_diff_view;
5052 view->search_next = search_next_view_match;
5055 close_diff_view(view);
5059 static const struct got_error *
5060 show_diff_view(struct tog_view *view)
5062 const struct got_error *err;
5063 struct tog_diff_view_state *s = &view->state.diff;
5064 char *id_str1 = NULL, *id_str2, *header;
5065 const char *label1, *label2;
5068 err = got_object_id_str(&id_str1, s->id1);
5071 label1 = s->label1 ? s->label1 : id_str1;
5073 label1 = "/dev/null";
5075 err = got_object_id_str(&id_str2, s->id2);
5078 label2 = s->label2 ? s->label2 : id_str2;
5080 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5081 err = got_error_from_errno("asprintf");
5089 err = draw_file(view, header);
5094 static const struct got_error *
5095 set_selected_commit(struct tog_diff_view_state *s,
5096 struct commit_queue_entry *entry)
5098 const struct got_error *err;
5099 const struct got_object_id_queue *parent_ids;
5100 struct got_commit_object *selected_commit;
5101 struct got_object_qid *pid;
5104 s->id2 = got_object_id_dup(entry->id);
5106 return got_error_from_errno("got_object_id_dup");
5108 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5111 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5113 pid = STAILQ_FIRST(parent_ids);
5114 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5115 got_object_commit_close(selected_commit);
5119 static const struct got_error *
5120 reset_diff_view(struct tog_view *view)
5122 struct tog_diff_view_state *s = &view->state.diff;
5125 wclear(view->window);
5126 s->first_displayed_line = 1;
5127 s->last_displayed_line = view->nlines;
5128 s->matched_line = 0;
5129 diff_view_indicate_progress(view);
5130 return create_diff(s);
5134 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5138 i = start = s->first_displayed_line - 1;
5140 while (s->lines[i].type != type) {
5144 return; /* do nothing, requested type not in file */
5147 s->selected_line = 1;
5148 s->first_displayed_line = i;
5152 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5156 i = start = s->first_displayed_line + 1;
5158 while (s->lines[i].type != type) {
5159 if (i == s->nlines - 1)
5162 return; /* do nothing, requested type not in file */
5165 s->selected_line = 1;
5166 s->first_displayed_line = i;
5169 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5171 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5174 static const struct got_error *
5175 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5177 const struct got_error *err = NULL;
5178 struct tog_diff_view_state *s = &view->state.diff;
5179 struct tog_log_view_state *ls;
5180 struct commit_queue_entry *old_selected_entry;
5182 size_t linesize = 0;
5184 int i, nscroll = view->nlines - 1, up = 0;
5186 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5193 view->x = MAX(view->maxx - view->ncols / 3, 0);
5198 if (view->x + view->ncols / 3 < view->maxx)
5199 view->x += 2; /* move two columns right */
5205 view->x -= MIN(view->x, 2); /* move two columns back */
5212 s->force_text_diff = !s->force_text_diff;
5214 s->ignore_whitespace = !s->ignore_whitespace;
5215 err = reset_diff_view(view);
5219 s->first_displayed_line = 1;
5228 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5234 if (s->first_displayed_line > 1)
5235 s->first_displayed_line--;
5246 if (s->first_displayed_line == 1) {
5251 while (i++ < nscroll && s->first_displayed_line > 1)
5252 s->first_displayed_line--;
5258 s->first_displayed_line++;
5275 while (!s->eof && i++ < nscroll) {
5276 linelen = getline(&line, &linesize, s->f);
5277 s->first_displayed_line++;
5278 if (linelen == -1) {
5282 err = got_ferror(s->f, GOT_ERR_IO);
5289 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5292 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5295 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5298 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5301 if (s->diff_context > 0) {
5303 s->matched_line = 0;
5304 diff_view_indicate_progress(view);
5305 err = create_diff(s);
5306 if (s->first_displayed_line + view->nlines - 1 >
5308 s->first_displayed_line = 1;
5309 s->last_displayed_line = view->nlines;
5315 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5317 s->matched_line = 0;
5318 diff_view_indicate_progress(view);
5319 err = create_diff(s);
5331 if (s->parent_view == NULL) {
5335 s->parent_view->count = view->count;
5337 if (s->parent_view->type == TOG_VIEW_LOG) {
5338 ls = &s->parent_view->state.log;
5339 old_selected_entry = ls->selected_entry;
5341 err = input_log_view(NULL, s->parent_view,
5342 up ? KEY_UP : KEY_DOWN);
5345 view->count = s->parent_view->count;
5347 if (old_selected_entry == ls->selected_entry)
5350 err = set_selected_commit(s, ls->selected_entry);
5353 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5354 struct tog_blame_view_state *bs;
5355 struct got_object_id *id, *prev_id;
5357 bs = &s->parent_view->state.blame;
5358 prev_id = get_annotation_for_line(bs->blame.lines,
5359 bs->blame.nlines, bs->last_diffed_line);
5361 err = input_blame_view(&view, s->parent_view,
5362 up ? KEY_UP : KEY_DOWN);
5365 view->count = s->parent_view->count;
5367 if (prev_id == NULL)
5369 id = get_selected_commit_id(bs->blame.lines,
5370 bs->blame.nlines, bs->first_displayed_line,
5375 if (!got_object_id_cmp(prev_id, id))
5378 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5382 s->first_displayed_line = 1;
5383 s->last_displayed_line = view->nlines;
5384 s->matched_line = 0;
5387 diff_view_indicate_progress(view);
5388 err = create_diff(s);
5398 static const struct got_error *
5399 cmd_diff(int argc, char *argv[])
5401 const struct got_error *error = NULL;
5402 struct got_repository *repo = NULL;
5403 struct got_worktree *worktree = NULL;
5404 struct got_object_id *id1 = NULL, *id2 = NULL;
5405 char *repo_path = NULL, *cwd = NULL;
5406 char *id_str1 = NULL, *id_str2 = NULL;
5407 char *label1 = NULL, *label2 = NULL;
5408 int diff_context = 3, ignore_whitespace = 0;
5409 int ch, force_text_diff = 0;
5411 struct tog_view *view;
5412 int *pack_fds = NULL;
5414 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5417 force_text_diff = 1;
5420 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5423 errx(1, "number of context lines is %s: %s",
5427 repo_path = realpath(optarg, NULL);
5428 if (repo_path == NULL)
5429 return got_error_from_errno2("realpath",
5431 got_path_strip_trailing_slashes(repo_path);
5434 ignore_whitespace = 1;
5446 usage_diff(); /* TODO show local worktree changes */
5447 } else if (argc == 2) {
5453 error = got_repo_pack_fds_open(&pack_fds);
5457 if (repo_path == NULL) {
5458 cwd = getcwd(NULL, 0);
5460 return got_error_from_errno("getcwd");
5461 error = got_worktree_open(&worktree, cwd);
5462 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5466 strdup(got_worktree_get_repo_path(worktree));
5468 repo_path = strdup(cwd);
5469 if (repo_path == NULL) {
5470 error = got_error_from_errno("strdup");
5475 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5481 error = apply_unveil(got_repo_get_path(repo), NULL);
5485 error = tog_load_refs(repo, 0);
5489 error = got_repo_match_object_id(&id1, &label1, id_str1,
5490 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5494 error = got_repo_match_object_id(&id2, &label2, id_str2,
5495 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5499 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5501 error = got_error_from_errno("view_open");
5504 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5505 ignore_whitespace, force_text_diff, NULL, repo);
5508 error = view_loop(view);
5515 const struct got_error *close_err = got_repo_close(repo);
5520 got_worktree_close(worktree);
5522 const struct got_error *pack_err =
5523 got_repo_pack_fds_close(pack_fds);
5536 "usage: %s blame [-c commit] [-r repository-path] path\n",
5541 struct tog_blame_line {
5543 struct got_object_id *id;
5546 static const struct got_error *
5547 draw_blame(struct tog_view *view)
5549 struct tog_blame_view_state *s = &view->state.blame;
5550 struct tog_blame *blame = &s->blame;
5551 regmatch_t *regmatch = &view->regmatch;
5552 const struct got_error *err;
5553 int lineno = 0, nprinted = 0;
5555 size_t linesize = 0;
5559 struct tog_blame_line *blame_line;
5560 struct got_object_id *prev_id = NULL;
5562 struct tog_color *tc;
5564 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5569 werase(view->window);
5571 if (asprintf(&line, "commit %s", id_str) == -1) {
5572 err = got_error_from_errno("asprintf");
5577 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5582 if (view_needs_focus_indication(view))
5583 wstandout(view->window);
5584 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5586 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5587 waddwstr(view->window, wline);
5588 while (width++ < view->ncols)
5589 waddch(view->window, ' ');
5591 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5592 if (view_needs_focus_indication(view))
5593 wstandend(view->window);
5597 if (view->gline > blame->nlines)
5598 view->gline = blame->nlines;
5600 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5601 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5602 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5604 return got_error_from_errno("asprintf");
5607 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5612 waddwstr(view->window, wline);
5615 if (width < view->ncols - 1)
5616 waddch(view->window, '\n');
5620 while (nprinted < view->nlines - 2) {
5621 linelen = getline(&line, &linesize, blame->f);
5622 if (linelen == -1) {
5623 if (feof(blame->f)) {
5628 return got_ferror(blame->f, GOT_ERR_IO);
5630 if (++lineno < s->first_displayed_line)
5632 if (view->gline && !gotoline(view, &lineno, &nprinted))
5635 /* Set view->maxx based on full line length. */
5636 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5643 view->maxx = MAX(view->maxx, width);
5645 if (nprinted == s->selected_line - 1)
5646 wstandout(view->window);
5648 if (blame->nlines > 0) {
5649 blame_line = &blame->lines[lineno - 1];
5650 if (blame_line->annotated && prev_id &&
5651 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5652 !(nprinted == s->selected_line - 1)) {
5653 waddstr(view->window, " ");
5654 } else if (blame_line->annotated) {
5656 err = got_object_id_str(&id_str,
5662 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5664 wattr_on(view->window,
5665 COLOR_PAIR(tc->colorpair), NULL);
5666 wprintw(view->window, "%.8s", id_str);
5668 wattr_off(view->window,
5669 COLOR_PAIR(tc->colorpair), NULL);
5671 prev_id = blame_line->id;
5673 waddstr(view->window, "........");
5677 waddstr(view->window, "........");
5681 if (nprinted == s->selected_line - 1)
5682 wstandend(view->window);
5683 waddstr(view->window, " ");
5685 if (view->ncols <= 9) {
5687 } else if (s->first_displayed_line + nprinted ==
5689 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5690 err = add_matched_line(&width, line, view->ncols - 9, 9,
5691 view->window, view->x, regmatch);
5699 err = format_line(&wline, &width, &skip, line,
5700 view->x, view->ncols - 9, 9, 1);
5705 waddwstr(view->window, &wline[skip]);
5711 if (width <= view->ncols - 1)
5712 waddch(view->window, '\n');
5713 if (++nprinted == 1)
5714 s->first_displayed_line = lineno;
5717 s->last_displayed_line = lineno;
5724 static const struct got_error *
5725 blame_cb(void *arg, int nlines, int lineno,
5726 struct got_commit_object *commit, struct got_object_id *id)
5728 const struct got_error *err = NULL;
5729 struct tog_blame_cb_args *a = arg;
5730 struct tog_blame_line *line;
5733 if (nlines != a->nlines ||
5734 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5735 return got_error(GOT_ERR_RANGE);
5737 errcode = pthread_mutex_lock(&tog_mutex);
5739 return got_error_set_errno(errcode, "pthread_mutex_lock");
5741 if (*a->quit) { /* user has quit the blame view */
5742 err = got_error(GOT_ERR_ITER_COMPLETED);
5747 goto done; /* no change in this commit */
5749 line = &a->lines[lineno - 1];
5750 if (line->annotated)
5753 line->id = got_object_id_dup(id);
5754 if (line->id == NULL) {
5755 err = got_error_from_errno("got_object_id_dup");
5758 line->annotated = 1;
5760 errcode = pthread_mutex_unlock(&tog_mutex);
5762 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5767 blame_thread(void *arg)
5769 const struct got_error *err, *close_err;
5770 struct tog_blame_thread_args *ta = arg;
5771 struct tog_blame_cb_args *a = ta->cb_args;
5772 int errcode, fd1 = -1, fd2 = -1;
5773 FILE *f1 = NULL, *f2 = NULL;
5775 fd1 = got_opentempfd();
5777 return (void *)got_error_from_errno("got_opentempfd");
5779 fd2 = got_opentempfd();
5781 err = got_error_from_errno("got_opentempfd");
5785 f1 = got_opentemp();
5787 err = (void *)got_error_from_errno("got_opentemp");
5790 f2 = got_opentemp();
5792 err = (void *)got_error_from_errno("got_opentemp");
5796 err = block_signals_used_by_main_thread();
5800 err = got_blame(ta->path, a->commit_id, ta->repo,
5801 tog_diff_algo, blame_cb, ta->cb_args,
5802 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5803 if (err && err->code == GOT_ERR_CANCELLED)
5806 errcode = pthread_mutex_lock(&tog_mutex);
5808 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5812 close_err = got_repo_close(ta->repo);
5818 errcode = pthread_mutex_unlock(&tog_mutex);
5819 if (errcode && err == NULL)
5820 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5823 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5824 err = got_error_from_errno("close");
5825 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5826 err = got_error_from_errno("close");
5827 if (f1 && fclose(f1) == EOF && err == NULL)
5828 err = got_error_from_errno("fclose");
5829 if (f2 && fclose(f2) == EOF && err == NULL)
5830 err = got_error_from_errno("fclose");
5835 static struct got_object_id *
5836 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5837 int first_displayed_line, int selected_line)
5839 struct tog_blame_line *line;
5844 line = &lines[first_displayed_line - 1 + selected_line - 1];
5845 if (!line->annotated)
5851 static struct got_object_id *
5852 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5855 struct tog_blame_line *line;
5857 if (nlines <= 0 || lineno >= nlines)
5860 line = &lines[lineno - 1];
5861 if (!line->annotated)
5867 static const struct got_error *
5868 stop_blame(struct tog_blame *blame)
5870 const struct got_error *err = NULL;
5873 if (blame->thread) {
5875 errcode = pthread_mutex_unlock(&tog_mutex);
5877 return got_error_set_errno(errcode,
5878 "pthread_mutex_unlock");
5879 errcode = pthread_join(blame->thread, (void **)&err);
5881 return got_error_set_errno(errcode, "pthread_join");
5882 errcode = pthread_mutex_lock(&tog_mutex);
5884 return got_error_set_errno(errcode,
5885 "pthread_mutex_lock");
5886 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5888 blame->thread = NULL;
5890 if (blame->thread_args.repo) {
5891 const struct got_error *close_err;
5892 close_err = got_repo_close(blame->thread_args.repo);
5895 blame->thread_args.repo = NULL;
5898 if (fclose(blame->f) == EOF && err == NULL)
5899 err = got_error_from_errno("fclose");
5903 for (i = 0; i < blame->nlines; i++)
5904 free(blame->lines[i].id);
5906 blame->lines = NULL;
5908 free(blame->cb_args.commit_id);
5909 blame->cb_args.commit_id = NULL;
5910 if (blame->pack_fds) {
5911 const struct got_error *pack_err =
5912 got_repo_pack_fds_close(blame->pack_fds);
5915 blame->pack_fds = NULL;
5920 static const struct got_error *
5921 cancel_blame_view(void *arg)
5923 const struct got_error *err = NULL;
5927 errcode = pthread_mutex_lock(&tog_mutex);
5929 return got_error_set_errno(errcode,
5930 "pthread_mutex_unlock");
5933 err = got_error(GOT_ERR_CANCELLED);
5935 errcode = pthread_mutex_unlock(&tog_mutex);
5937 return got_error_set_errno(errcode,
5938 "pthread_mutex_lock");
5943 static const struct got_error *
5944 run_blame(struct tog_view *view)
5946 struct tog_blame_view_state *s = &view->state.blame;
5947 struct tog_blame *blame = &s->blame;
5948 const struct got_error *err = NULL;
5949 struct got_commit_object *commit = NULL;
5950 struct got_blob_object *blob = NULL;
5951 struct got_repository *thread_repo = NULL;
5952 struct got_object_id *obj_id = NULL;
5953 int obj_type, fd = -1;
5954 int *pack_fds = NULL;
5956 err = got_object_open_as_commit(&commit, s->repo,
5957 &s->blamed_commit->id);
5961 fd = got_opentempfd();
5963 err = got_error_from_errno("got_opentempfd");
5967 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5971 err = got_object_get_type(&obj_type, s->repo, obj_id);
5975 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5976 err = got_error(GOT_ERR_OBJ_TYPE);
5980 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5983 blame->f = got_opentemp();
5984 if (blame->f == NULL) {
5985 err = got_error_from_errno("got_opentemp");
5988 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5989 &blame->line_offsets, blame->f, blob);
5992 if (blame->nlines == 0) {
5993 s->blame_complete = 1;
5997 /* Don't include \n at EOF in the blame line count. */
5998 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6001 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6002 if (blame->lines == NULL) {
6003 err = got_error_from_errno("calloc");
6007 err = got_repo_pack_fds_open(&pack_fds);
6010 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6015 blame->pack_fds = pack_fds;
6016 blame->cb_args.view = view;
6017 blame->cb_args.lines = blame->lines;
6018 blame->cb_args.nlines = blame->nlines;
6019 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6020 if (blame->cb_args.commit_id == NULL) {
6021 err = got_error_from_errno("got_object_id_dup");
6024 blame->cb_args.quit = &s->done;
6026 blame->thread_args.path = s->path;
6027 blame->thread_args.repo = thread_repo;
6028 blame->thread_args.cb_args = &blame->cb_args;
6029 blame->thread_args.complete = &s->blame_complete;
6030 blame->thread_args.cancel_cb = cancel_blame_view;
6031 blame->thread_args.cancel_arg = &s->done;
6032 s->blame_complete = 0;
6034 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6035 s->first_displayed_line = 1;
6036 s->last_displayed_line = view->nlines;
6037 s->selected_line = 1;
6039 s->matched_line = 0;
6043 got_object_commit_close(commit);
6044 if (fd != -1 && close(fd) == -1 && err == NULL)
6045 err = got_error_from_errno("close");
6047 got_object_blob_close(blob);
6054 static const struct got_error *
6055 open_blame_view(struct tog_view *view, char *path,
6056 struct got_object_id *commit_id, struct got_repository *repo)
6058 const struct got_error *err = NULL;
6059 struct tog_blame_view_state *s = &view->state.blame;
6061 STAILQ_INIT(&s->blamed_commits);
6063 s->path = strdup(path);
6064 if (s->path == NULL)
6065 return got_error_from_errno("strdup");
6067 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6073 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6074 s->first_displayed_line = 1;
6075 s->last_displayed_line = view->nlines;
6076 s->selected_line = 1;
6077 s->blame_complete = 0;
6079 s->commit_id = commit_id;
6080 memset(&s->blame, 0, sizeof(s->blame));
6082 STAILQ_INIT(&s->colors);
6083 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6084 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6085 get_color_value("TOG_COLOR_COMMIT"));
6090 view->show = show_blame_view;
6091 view->input = input_blame_view;
6092 view->reset = reset_blame_view;
6093 view->close = close_blame_view;
6094 view->search_start = search_start_blame_view;
6095 view->search_setup = search_setup_blame_view;
6096 view->search_next = search_next_view_match;
6098 return run_blame(view);
6101 static const struct got_error *
6102 close_blame_view(struct tog_view *view)
6104 const struct got_error *err = NULL;
6105 struct tog_blame_view_state *s = &view->state.blame;
6107 if (s->blame.thread)
6108 err = stop_blame(&s->blame);
6110 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6111 struct got_object_qid *blamed_commit;
6112 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6113 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6114 got_object_qid_free(blamed_commit);
6118 free_colors(&s->colors);
6122 static const struct got_error *
6123 search_start_blame_view(struct tog_view *view)
6125 struct tog_blame_view_state *s = &view->state.blame;
6127 s->matched_line = 0;
6132 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6133 size_t *nlines, int **first, int **last, int **match, int **selected)
6135 struct tog_blame_view_state *s = &view->state.blame;
6138 *nlines = s->blame.nlines;
6139 *line_offsets = s->blame.line_offsets;
6140 *match = &s->matched_line;
6141 *first = &s->first_displayed_line;
6142 *last = &s->last_displayed_line;
6143 *selected = &s->selected_line;
6146 static const struct got_error *
6147 show_blame_view(struct tog_view *view)
6149 const struct got_error *err = NULL;
6150 struct tog_blame_view_state *s = &view->state.blame;
6153 if (s->blame.thread == NULL && !s->blame_complete) {
6154 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6155 &s->blame.thread_args);
6157 return got_error_set_errno(errcode, "pthread_create");
6159 halfdelay(1); /* fast refresh while annotating */
6162 if (s->blame_complete)
6163 halfdelay(10); /* disable fast refresh */
6165 err = draw_blame(view);
6171 static const struct got_error *
6172 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6173 struct got_repository *repo, struct got_object_id *id)
6175 struct tog_view *log_view;
6176 const struct got_error *err = NULL;
6180 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6181 if (log_view == NULL)
6182 return got_error_from_errno("view_open");
6184 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6186 view_close(log_view);
6188 *new_view = log_view;
6193 static const struct got_error *
6194 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6196 const struct got_error *err = NULL, *thread_err = NULL;
6197 struct tog_view *diff_view;
6198 struct tog_blame_view_state *s = &view->state.blame;
6199 int eos, nscroll, begin_y = 0, begin_x = 0;
6201 eos = nscroll = view->nlines - 2;
6202 if (view_is_hsplit_top(view))
6210 view->x = MAX(view->maxx - view->ncols / 3, 0);
6215 if (view->x + view->ncols / 3 < view->maxx)
6216 view->x += 2; /* move two columns right */
6222 view->x -= MIN(view->x, 2); /* move two columns back */
6231 s->selected_line = 1;
6232 s->first_displayed_line = 1;
6237 if (s->blame.nlines < eos) {
6238 s->selected_line = s->blame.nlines;
6239 s->first_displayed_line = 1;
6241 s->selected_line = eos;
6242 s->first_displayed_line = s->blame.nlines - (eos - 1);
6249 if (s->selected_line > 1)
6251 else if (s->selected_line == 1 &&
6252 s->first_displayed_line > 1)
6253 s->first_displayed_line--;
6264 if (s->first_displayed_line == 1) {
6265 if (view->count > 1)
6267 s->selected_line = MAX(1, s->selected_line - nscroll);
6271 if (s->first_displayed_line > nscroll)
6272 s->first_displayed_line -= nscroll;
6274 s->first_displayed_line = 1;
6279 if (s->selected_line < eos && s->first_displayed_line +
6280 s->selected_line <= s->blame.nlines)
6282 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6283 s->first_displayed_line++;
6289 struct got_object_id *id = NULL;
6292 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6293 s->first_displayed_line, s->selected_line);
6297 struct got_commit_object *commit, *pcommit;
6298 struct got_object_qid *pid;
6299 struct got_object_id *blob_id = NULL;
6301 err = got_object_open_as_commit(&commit,
6306 got_object_commit_get_parent_ids(commit));
6308 got_object_commit_close(commit);
6311 /* Check if path history ends here. */
6312 err = got_object_open_as_commit(&pcommit,
6316 err = got_object_id_by_path(&blob_id, s->repo,
6318 got_object_commit_close(pcommit);
6320 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6322 got_object_commit_close(commit);
6325 err = got_object_get_type(&obj_type, s->repo,
6328 /* Can't blame non-blob type objects. */
6329 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6330 got_object_commit_close(commit);
6333 err = got_object_qid_alloc(&s->blamed_commit,
6335 got_object_commit_close(commit);
6337 if (got_object_id_cmp(id,
6338 &s->blamed_commit->id) == 0)
6340 err = got_object_qid_alloc(&s->blamed_commit,
6346 thread_err = stop_blame(&s->blame);
6350 STAILQ_INSERT_HEAD(&s->blamed_commits,
6351 s->blamed_commit, entry);
6352 err = run_blame(view);
6358 struct got_object_qid *first;
6361 first = STAILQ_FIRST(&s->blamed_commits);
6362 if (!got_object_id_cmp(&first->id, s->commit_id))
6365 thread_err = stop_blame(&s->blame);
6369 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6370 got_object_qid_free(s->blamed_commit);
6372 STAILQ_FIRST(&s->blamed_commits);
6373 err = run_blame(view);
6380 s->id_to_log = get_selected_commit_id(s->blame.lines,
6381 s->blame.nlines, s->first_displayed_line, s->selected_line);
6383 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6387 struct got_object_id *id = NULL;
6388 struct got_object_qid *pid;
6389 struct got_commit_object *commit = NULL;
6392 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6393 s->first_displayed_line, s->selected_line);
6396 err = got_object_open_as_commit(&commit, s->repo, id);
6399 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6401 /* traversed from diff view, release diff resources */
6402 err = close_diff_view(*new_view);
6405 diff_view = *new_view;
6407 if (view_is_parent_view(view))
6408 view_get_split(view, &begin_y, &begin_x);
6410 diff_view = view_open(0, 0, begin_y, begin_x,
6412 if (diff_view == NULL) {
6413 got_object_commit_close(commit);
6414 err = got_error_from_errno("view_open");
6418 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6419 id, NULL, NULL, 3, 0, 0, view, s->repo);
6420 got_object_commit_close(commit);
6422 view_close(diff_view);
6425 s->last_diffed_line = s->first_displayed_line - 1 +
6428 break; /* still open from active diff view */
6429 if (view_is_parent_view(view) &&
6430 view->mode == TOG_VIEW_SPLIT_HRZN) {
6431 err = view_init_hsplit(view, begin_y);
6437 diff_view->focussed = 1;
6438 diff_view->mode = view->mode;
6439 diff_view->nlines = view->lines - begin_y;
6440 if (view_is_parent_view(view)) {
6441 view_transfer_size(diff_view, view);
6442 err = view_close_child(view);
6445 err = view_set_child(view, diff_view);
6448 view->focus_child = 1;
6450 *new_view = diff_view;
6463 if (s->last_displayed_line >= s->blame.nlines &&
6464 s->selected_line >= MIN(s->blame.nlines,
6465 view->nlines - 2)) {
6469 if (s->last_displayed_line >= s->blame.nlines &&
6470 s->selected_line < view->nlines - 2) {
6472 MIN(nscroll, s->last_displayed_line -
6473 s->first_displayed_line - s->selected_line + 1);
6475 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6476 s->first_displayed_line += nscroll;
6478 s->first_displayed_line =
6479 s->blame.nlines - (view->nlines - 3);
6482 if (s->selected_line > view->nlines - 2) {
6483 s->selected_line = MIN(s->blame.nlines,
6491 return thread_err ? thread_err : err;
6494 static const struct got_error *
6495 reset_blame_view(struct tog_view *view)
6497 const struct got_error *err;
6498 struct tog_blame_view_state *s = &view->state.blame;
6502 err = stop_blame(&s->blame);
6506 return run_blame(view);
6509 static const struct got_error *
6510 cmd_blame(int argc, char *argv[])
6512 const struct got_error *error;
6513 struct got_repository *repo = NULL;
6514 struct got_worktree *worktree = NULL;
6515 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6516 char *link_target = NULL;
6517 struct got_object_id *commit_id = NULL;
6518 struct got_commit_object *commit = NULL;
6519 char *commit_id_str = NULL;
6521 struct tog_view *view;
6522 int *pack_fds = NULL;
6524 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6527 commit_id_str = optarg;
6530 repo_path = realpath(optarg, NULL);
6531 if (repo_path == NULL)
6532 return got_error_from_errno2("realpath",
6547 error = got_repo_pack_fds_open(&pack_fds);
6551 if (repo_path == NULL) {
6552 cwd = getcwd(NULL, 0);
6554 return got_error_from_errno("getcwd");
6555 error = got_worktree_open(&worktree, cwd);
6556 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6560 strdup(got_worktree_get_repo_path(worktree));
6562 repo_path = strdup(cwd);
6563 if (repo_path == NULL) {
6564 error = got_error_from_errno("strdup");
6569 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6573 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6580 error = apply_unveil(got_repo_get_path(repo), NULL);
6584 error = tog_load_refs(repo, 0);
6588 if (commit_id_str == NULL) {
6589 struct got_reference *head_ref;
6590 error = got_ref_open(&head_ref, repo, worktree ?
6591 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6594 error = got_ref_resolve(&commit_id, repo, head_ref);
6595 got_ref_close(head_ref);
6597 error = got_repo_match_object_id(&commit_id, NULL,
6598 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6603 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6605 error = got_error_from_errno("view_open");
6609 error = got_object_open_as_commit(&commit, repo, commit_id);
6613 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6618 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6623 /* Release work tree lock. */
6624 got_worktree_close(worktree);
6627 error = view_loop(view);
6635 got_object_commit_close(commit);
6637 got_worktree_close(worktree);
6639 const struct got_error *close_err = got_repo_close(repo);
6644 const struct got_error *pack_err =
6645 got_repo_pack_fds_close(pack_fds);
6653 static const struct got_error *
6654 draw_tree_entries(struct tog_view *view, const char *parent_path)
6656 struct tog_tree_view_state *s = &view->state.tree;
6657 const struct got_error *err = NULL;
6658 struct got_tree_entry *te;
6661 struct tog_color *tc;
6662 int width, n, nentries, i = 1;
6663 int limit = view->nlines;
6666 if (view_is_hsplit_top(view))
6667 --limit; /* border */
6669 werase(view->window);
6674 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6678 if (view_needs_focus_indication(view))
6679 wstandout(view->window);
6680 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6682 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6683 waddwstr(view->window, wline);
6686 while (width++ < view->ncols)
6687 waddch(view->window, ' ');
6689 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6690 if (view_needs_focus_indication(view))
6691 wstandend(view->window);
6696 if (s->first_displayed_entry) {
6697 i += got_tree_entry_get_index(s->first_displayed_entry);
6698 if (s->tree != s->root)
6699 ++i; /* account for ".." entry */
6701 nentries = got_object_tree_get_nentries(s->tree);
6702 if (asprintf(&index, "[%d/%d] %s",
6703 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6704 return got_error_from_errno("asprintf");
6705 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6709 waddwstr(view->window, wline);
6712 if (width < view->ncols - 1)
6713 waddch(view->window, '\n');
6716 waddch(view->window, '\n');
6720 if (s->first_displayed_entry == NULL) {
6721 te = got_object_tree_get_first_entry(s->tree);
6722 if (s->selected == 0) {
6724 wstandout(view->window);
6725 s->selected_entry = NULL;
6727 waddstr(view->window, " ..\n"); /* parent directory */
6728 if (s->selected == 0 && view->focussed)
6729 wstandend(view->window);
6736 te = s->first_displayed_entry;
6739 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6740 char *line = NULL, *id_str = NULL, *link_target = NULL;
6741 const char *modestr = "";
6744 te = got_object_tree_get_entry(s->tree, i);
6745 mode = got_tree_entry_get_mode(te);
6748 err = got_object_id_str(&id_str,
6749 got_tree_entry_get_id(te));
6751 return got_error_from_errno(
6752 "got_object_id_str");
6754 if (got_object_tree_entry_is_submodule(te))
6756 else if (S_ISLNK(mode)) {
6759 err = got_tree_entry_get_symlink_target(&link_target,
6765 for (i = 0; i < strlen(link_target); i++) {
6766 if (!isprint((unsigned char)link_target[i]))
6767 link_target[i] = '?';
6771 else if (S_ISDIR(mode))
6773 else if (mode & S_IXUSR)
6775 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6776 got_tree_entry_get_name(te), modestr,
6777 link_target ? " -> ": "",
6778 link_target ? link_target : "") == -1) {
6781 return got_error_from_errno("asprintf");
6785 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6791 if (n == s->selected) {
6793 wstandout(view->window);
6794 s->selected_entry = te;
6796 tc = match_color(&s->colors, line);
6798 wattr_on(view->window,
6799 COLOR_PAIR(tc->colorpair), NULL);
6800 waddwstr(view->window, wline);
6802 wattr_off(view->window,
6803 COLOR_PAIR(tc->colorpair), NULL);
6804 if (width < view->ncols - 1)
6805 waddch(view->window, '\n');
6806 if (n == s->selected && view->focussed)
6807 wstandend(view->window);
6813 s->last_displayed_entry = te;
6822 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6824 struct got_tree_entry *te;
6825 int isroot = s->tree == s->root;
6828 if (s->first_displayed_entry == NULL)
6831 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6832 while (i++ < maxscroll) {
6835 s->first_displayed_entry = NULL;
6838 s->first_displayed_entry = te;
6839 te = got_tree_entry_get_prev(s->tree, te);
6843 static const struct got_error *
6844 tree_scroll_down(struct tog_view *view, int maxscroll)
6846 struct tog_tree_view_state *s = &view->state.tree;
6847 struct got_tree_entry *next, *last;
6850 if (s->first_displayed_entry)
6851 next = got_tree_entry_get_next(s->tree,
6852 s->first_displayed_entry);
6854 next = got_object_tree_get_first_entry(s->tree);
6856 last = s->last_displayed_entry;
6857 while (next && n++ < maxscroll) {
6859 s->last_displayed_entry = last;
6860 last = got_tree_entry_get_next(s->tree, last);
6862 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6863 s->first_displayed_entry = next;
6864 next = got_tree_entry_get_next(s->tree, next);
6871 static const struct got_error *
6872 tree_entry_path(char **path, struct tog_parent_trees *parents,
6873 struct got_tree_entry *te)
6875 const struct got_error *err = NULL;
6876 struct tog_parent_tree *pt;
6877 size_t len = 2; /* for leading slash and NUL */
6879 TAILQ_FOREACH(pt, parents, entry)
6880 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6883 len += strlen(got_tree_entry_get_name(te));
6885 *path = calloc(1, len);
6887 return got_error_from_errno("calloc");
6890 pt = TAILQ_LAST(parents, tog_parent_trees);
6892 const char *name = got_tree_entry_get_name(pt->selected_entry);
6893 if (strlcat(*path, name, len) >= len) {
6894 err = got_error(GOT_ERR_NO_SPACE);
6897 if (strlcat(*path, "/", len) >= len) {
6898 err = got_error(GOT_ERR_NO_SPACE);
6901 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6904 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6905 err = got_error(GOT_ERR_NO_SPACE);
6917 static const struct got_error *
6918 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6919 struct got_tree_entry *te, struct tog_parent_trees *parents,
6920 struct got_object_id *commit_id, struct got_repository *repo)
6922 const struct got_error *err = NULL;
6924 struct tog_view *blame_view;
6928 err = tree_entry_path(&path, parents, te);
6932 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6933 if (blame_view == NULL) {
6934 err = got_error_from_errno("view_open");
6938 err = open_blame_view(blame_view, path, commit_id, repo);
6940 if (err->code == GOT_ERR_CANCELLED)
6942 view_close(blame_view);
6944 *new_view = blame_view;
6950 static const struct got_error *
6951 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6952 struct tog_tree_view_state *s)
6954 struct tog_view *log_view;
6955 const struct got_error *err = NULL;
6960 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6961 if (log_view == NULL)
6962 return got_error_from_errno("view_open");
6964 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6968 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6971 view_close(log_view);
6973 *new_view = log_view;
6978 static const struct got_error *
6979 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6980 const char *head_ref_name, struct got_repository *repo)
6982 const struct got_error *err = NULL;
6983 char *commit_id_str = NULL;
6984 struct tog_tree_view_state *s = &view->state.tree;
6985 struct got_commit_object *commit = NULL;
6987 TAILQ_INIT(&s->parents);
6988 STAILQ_INIT(&s->colors);
6990 s->commit_id = got_object_id_dup(commit_id);
6991 if (s->commit_id == NULL)
6992 return got_error_from_errno("got_object_id_dup");
6994 err = got_object_open_as_commit(&commit, repo, commit_id);
6999 * The root is opened here and will be closed when the view is closed.
7000 * Any visited subtrees and their path-wise parents are opened and
7003 err = got_object_open_as_tree(&s->root, repo,
7004 got_object_commit_get_tree_id(commit));
7009 err = got_object_id_str(&commit_id_str, commit_id);
7013 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7014 err = got_error_from_errno("asprintf");
7018 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7019 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7020 if (head_ref_name) {
7021 s->head_ref_name = strdup(head_ref_name);
7022 if (s->head_ref_name == NULL) {
7023 err = got_error_from_errno("strdup");
7029 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7030 err = add_color(&s->colors, "\\$$",
7031 TOG_COLOR_TREE_SUBMODULE,
7032 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7035 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7036 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7039 err = add_color(&s->colors, "/$",
7040 TOG_COLOR_TREE_DIRECTORY,
7041 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7045 err = add_color(&s->colors, "\\*$",
7046 TOG_COLOR_TREE_EXECUTABLE,
7047 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7051 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7052 get_color_value("TOG_COLOR_COMMIT"));
7057 view->show = show_tree_view;
7058 view->input = input_tree_view;
7059 view->close = close_tree_view;
7060 view->search_start = search_start_tree_view;
7061 view->search_next = search_next_tree_view;
7063 free(commit_id_str);
7065 got_object_commit_close(commit);
7067 close_tree_view(view);
7071 static const struct got_error *
7072 close_tree_view(struct tog_view *view)
7074 struct tog_tree_view_state *s = &view->state.tree;
7076 free_colors(&s->colors);
7077 free(s->tree_label);
7078 s->tree_label = NULL;
7080 s->commit_id = NULL;
7081 free(s->head_ref_name);
7082 s->head_ref_name = NULL;
7083 while (!TAILQ_EMPTY(&s->parents)) {
7084 struct tog_parent_tree *parent;
7085 parent = TAILQ_FIRST(&s->parents);
7086 TAILQ_REMOVE(&s->parents, parent, entry);
7087 if (parent->tree != s->root)
7088 got_object_tree_close(parent->tree);
7092 if (s->tree != NULL && s->tree != s->root)
7093 got_object_tree_close(s->tree);
7095 got_object_tree_close(s->root);
7099 static const struct got_error *
7100 search_start_tree_view(struct tog_view *view)
7102 struct tog_tree_view_state *s = &view->state.tree;
7104 s->matched_entry = NULL;
7109 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7111 regmatch_t regmatch;
7113 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7117 static const struct got_error *
7118 search_next_tree_view(struct tog_view *view)
7120 struct tog_tree_view_state *s = &view->state.tree;
7121 struct got_tree_entry *te = NULL;
7123 if (!view->searching) {
7124 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7128 if (s->matched_entry) {
7129 if (view->searching == TOG_SEARCH_FORWARD) {
7130 if (s->selected_entry)
7131 te = got_tree_entry_get_next(s->tree,
7134 te = got_object_tree_get_first_entry(s->tree);
7136 if (s->selected_entry == NULL)
7137 te = got_object_tree_get_last_entry(s->tree);
7139 te = got_tree_entry_get_prev(s->tree,
7143 if (s->selected_entry)
7144 te = s->selected_entry;
7145 else if (view->searching == TOG_SEARCH_FORWARD)
7146 te = got_object_tree_get_first_entry(s->tree);
7148 te = got_object_tree_get_last_entry(s->tree);
7153 if (s->matched_entry == NULL) {
7154 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7157 if (view->searching == TOG_SEARCH_FORWARD)
7158 te = got_object_tree_get_first_entry(s->tree);
7160 te = got_object_tree_get_last_entry(s->tree);
7163 if (match_tree_entry(te, &view->regex)) {
7164 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7165 s->matched_entry = te;
7169 if (view->searching == TOG_SEARCH_FORWARD)
7170 te = got_tree_entry_get_next(s->tree, te);
7172 te = got_tree_entry_get_prev(s->tree, te);
7175 if (s->matched_entry) {
7176 s->first_displayed_entry = s->matched_entry;
7183 static const struct got_error *
7184 show_tree_view(struct tog_view *view)
7186 const struct got_error *err = NULL;
7187 struct tog_tree_view_state *s = &view->state.tree;
7190 err = tree_entry_path(&parent_path, &s->parents, NULL);
7194 err = draw_tree_entries(view, parent_path);
7201 static const struct got_error *
7202 tree_goto_line(struct tog_view *view, int nlines)
7204 const struct got_error *err = NULL;
7205 struct tog_tree_view_state *s = &view->state.tree;
7206 struct got_tree_entry **fte, **lte, **ste;
7207 int g, last, first = 1, i = 1;
7208 int root = s->tree == s->root;
7209 int off = root ? 1 : 2;
7216 else if (g > got_object_tree_get_nentries(s->tree))
7217 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7219 fte = &s->first_displayed_entry;
7220 lte = &s->last_displayed_entry;
7221 ste = &s->selected_entry;
7224 first = got_tree_entry_get_index(*fte);
7225 first += off; /* account for ".." */
7227 last = got_tree_entry_get_index(*lte);
7230 if (g >= first && g <= last && g - first < nlines) {
7231 s->selected = g - first;
7232 return NULL; /* gline is on the current page */
7236 i = got_tree_entry_get_index(*ste);
7241 err = tree_scroll_down(view, g - i);
7244 if (got_tree_entry_get_index(*lte) >=
7245 got_object_tree_get_nentries(s->tree) - 1 &&
7246 first + s->selected < g &&
7247 s->selected < s->ndisplayed - 1) {
7248 first = got_tree_entry_get_index(*fte);
7250 s->selected = g - first;
7253 tree_scroll_up(s, i - g);
7256 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7257 s->selected = g - 1;
7262 static const struct got_error *
7263 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7265 const struct got_error *err = NULL;
7266 struct tog_tree_view_state *s = &view->state.tree;
7267 struct got_tree_entry *te;
7268 int n, nscroll = view->nlines - 3;
7271 return tree_goto_line(view, nscroll);
7275 s->show_ids = !s->show_ids;
7280 if (!s->selected_entry)
7282 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7286 err = view_request_new(new_view, view, TOG_VIEW_REF);
7292 if (s->tree == s->root)
7293 s->first_displayed_entry =
7294 got_object_tree_get_first_entry(s->tree);
7296 s->first_displayed_entry = NULL;
7300 int eos = view->nlines - 3;
7302 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7306 te = got_object_tree_get_last_entry(s->tree);
7307 for (n = 0; n < eos; n++) {
7309 if (s->tree != s->root) {
7310 s->first_displayed_entry = NULL;
7315 s->first_displayed_entry = te;
7316 te = got_tree_entry_get_prev(s->tree, te);
7319 s->selected = n - 1;
7325 if (s->selected > 0) {
7329 tree_scroll_up(s, 1);
7330 if (s->selected_entry == NULL ||
7331 (s->tree == s->root && s->selected_entry ==
7332 got_object_tree_get_first_entry(s->tree)))
7342 if (s->tree == s->root) {
7343 if (got_object_tree_get_first_entry(s->tree) ==
7344 s->first_displayed_entry)
7345 s->selected -= MIN(s->selected, nscroll);
7347 if (s->first_displayed_entry == NULL)
7348 s->selected -= MIN(s->selected, nscroll);
7350 tree_scroll_up(s, MAX(0, nscroll));
7351 if (s->selected_entry == NULL ||
7352 (s->tree == s->root && s->selected_entry ==
7353 got_object_tree_get_first_entry(s->tree)))
7359 if (s->selected < s->ndisplayed - 1) {
7363 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7365 /* can't scroll any further */
7369 tree_scroll_down(view, 1);
7379 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7381 /* can't scroll any further; move cursor down */
7382 if (s->selected < s->ndisplayed - 1)
7383 s->selected += MIN(nscroll,
7384 s->ndisplayed - s->selected - 1);
7389 tree_scroll_down(view, nscroll);
7394 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7395 struct tog_parent_tree *parent;
7396 /* user selected '..' */
7397 if (s->tree == s->root) {
7401 parent = TAILQ_FIRST(&s->parents);
7402 TAILQ_REMOVE(&s->parents, parent,
7404 got_object_tree_close(s->tree);
7405 s->tree = parent->tree;
7406 s->first_displayed_entry =
7407 parent->first_displayed_entry;
7409 parent->selected_entry;
7410 s->selected = parent->selected;
7411 if (s->selected > view->nlines - 3) {
7412 err = offset_selection_down(view);
7417 } else if (S_ISDIR(got_tree_entry_get_mode(
7418 s->selected_entry))) {
7419 struct got_tree_object *subtree;
7421 err = got_object_open_as_tree(&subtree, s->repo,
7422 got_tree_entry_get_id(s->selected_entry));
7425 err = tree_view_visit_subtree(s, subtree);
7427 got_object_tree_close(subtree);
7430 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7431 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7434 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7435 s->selected = view->nlines - 4;
7451 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7456 static const struct got_error *
7457 cmd_tree(int argc, char *argv[])
7459 const struct got_error *error;
7460 struct got_repository *repo = NULL;
7461 struct got_worktree *worktree = NULL;
7462 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7463 struct got_object_id *commit_id = NULL;
7464 struct got_commit_object *commit = NULL;
7465 const char *commit_id_arg = NULL;
7467 struct got_reference *ref = NULL;
7468 const char *head_ref_name = NULL;
7470 struct tog_view *view;
7471 int *pack_fds = NULL;
7473 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7476 commit_id_arg = optarg;
7479 repo_path = realpath(optarg, NULL);
7480 if (repo_path == NULL)
7481 return got_error_from_errno2("realpath",
7496 error = got_repo_pack_fds_open(&pack_fds);
7500 if (repo_path == NULL) {
7501 cwd = getcwd(NULL, 0);
7503 return got_error_from_errno("getcwd");
7504 error = got_worktree_open(&worktree, cwd);
7505 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7509 strdup(got_worktree_get_repo_path(worktree));
7511 repo_path = strdup(cwd);
7512 if (repo_path == NULL) {
7513 error = got_error_from_errno("strdup");
7518 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7522 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7529 error = apply_unveil(got_repo_get_path(repo), NULL);
7533 error = tog_load_refs(repo, 0);
7537 if (commit_id_arg == NULL) {
7538 error = got_repo_match_object_id(&commit_id, &label,
7539 worktree ? got_worktree_get_head_ref_name(worktree) :
7540 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7543 head_ref_name = label;
7545 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7547 head_ref_name = got_ref_get_name(ref);
7548 else if (error->code != GOT_ERR_NOT_REF)
7550 error = got_repo_match_object_id(&commit_id, NULL,
7551 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7556 error = got_object_open_as_commit(&commit, repo, commit_id);
7560 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7562 error = got_error_from_errno("view_open");
7565 error = open_tree_view(view, commit_id, head_ref_name, repo);
7568 if (!got_path_is_root_dir(in_repo_path)) {
7569 error = tree_view_walk_path(&view->state.tree, commit,
7576 /* Release work tree lock. */
7577 got_worktree_close(worktree);
7580 error = view_loop(view);
7589 const struct got_error *close_err = got_repo_close(repo);
7594 const struct got_error *pack_err =
7595 got_repo_pack_fds_close(pack_fds);
7603 static const struct got_error *
7604 ref_view_load_refs(struct tog_ref_view_state *s)
7606 struct got_reflist_entry *sre;
7607 struct tog_reflist_entry *re;
7610 TAILQ_FOREACH(sre, &tog_refs, entry) {
7611 if (strncmp(got_ref_get_name(sre->ref),
7612 "refs/got/", 9) == 0 &&
7613 strncmp(got_ref_get_name(sre->ref),
7614 "refs/got/backup/", 16) != 0)
7617 re = malloc(sizeof(*re));
7619 return got_error_from_errno("malloc");
7621 re->ref = got_ref_dup(sre->ref);
7622 if (re->ref == NULL)
7623 return got_error_from_errno("got_ref_dup");
7624 re->idx = s->nrefs++;
7625 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7628 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7633 ref_view_free_refs(struct tog_ref_view_state *s)
7635 struct tog_reflist_entry *re;
7637 while (!TAILQ_EMPTY(&s->refs)) {
7638 re = TAILQ_FIRST(&s->refs);
7639 TAILQ_REMOVE(&s->refs, re, entry);
7640 got_ref_close(re->ref);
7645 static const struct got_error *
7646 open_ref_view(struct tog_view *view, struct got_repository *repo)
7648 const struct got_error *err = NULL;
7649 struct tog_ref_view_state *s = &view->state.ref;
7651 s->selected_entry = 0;
7654 TAILQ_INIT(&s->refs);
7655 STAILQ_INIT(&s->colors);
7657 err = ref_view_load_refs(s);
7661 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7662 err = add_color(&s->colors, "^refs/heads/",
7663 TOG_COLOR_REFS_HEADS,
7664 get_color_value("TOG_COLOR_REFS_HEADS"));
7668 err = add_color(&s->colors, "^refs/tags/",
7669 TOG_COLOR_REFS_TAGS,
7670 get_color_value("TOG_COLOR_REFS_TAGS"));
7674 err = add_color(&s->colors, "^refs/remotes/",
7675 TOG_COLOR_REFS_REMOTES,
7676 get_color_value("TOG_COLOR_REFS_REMOTES"));
7680 err = add_color(&s->colors, "^refs/got/backup/",
7681 TOG_COLOR_REFS_BACKUP,
7682 get_color_value("TOG_COLOR_REFS_BACKUP"));
7687 view->show = show_ref_view;
7688 view->input = input_ref_view;
7689 view->close = close_ref_view;
7690 view->search_start = search_start_ref_view;
7691 view->search_next = search_next_ref_view;
7694 free_colors(&s->colors);
7698 static const struct got_error *
7699 close_ref_view(struct tog_view *view)
7701 struct tog_ref_view_state *s = &view->state.ref;
7703 ref_view_free_refs(s);
7704 free_colors(&s->colors);
7709 static const struct got_error *
7710 resolve_reflist_entry(struct got_object_id **commit_id,
7711 struct tog_reflist_entry *re, struct got_repository *repo)
7713 const struct got_error *err = NULL;
7714 struct got_object_id *obj_id;
7715 struct got_tag_object *tag = NULL;
7720 err = got_ref_resolve(&obj_id, repo, re->ref);
7724 err = got_object_get_type(&obj_type, repo, obj_id);
7729 case GOT_OBJ_TYPE_COMMIT:
7730 *commit_id = obj_id;
7732 case GOT_OBJ_TYPE_TAG:
7733 err = got_object_open_as_tag(&tag, repo, obj_id);
7737 err = got_object_get_type(&obj_type, repo,
7738 got_object_tag_get_object_id(tag));
7741 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7742 err = got_error(GOT_ERR_OBJ_TYPE);
7745 *commit_id = got_object_id_dup(
7746 got_object_tag_get_object_id(tag));
7747 if (*commit_id == NULL) {
7748 err = got_error_from_errno("got_object_id_dup");
7753 err = got_error(GOT_ERR_OBJ_TYPE);
7759 got_object_tag_close(tag);
7767 static const struct got_error *
7768 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7769 struct tog_reflist_entry *re, struct got_repository *repo)
7771 struct tog_view *log_view;
7772 const struct got_error *err = NULL;
7773 struct got_object_id *commit_id = NULL;
7777 err = resolve_reflist_entry(&commit_id, re, repo);
7779 if (err->code != GOT_ERR_OBJ_TYPE)
7785 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7786 if (log_view == NULL) {
7787 err = got_error_from_errno("view_open");
7791 err = open_log_view(log_view, commit_id, repo,
7792 got_ref_get_name(re->ref), "", 0);
7795 view_close(log_view);
7797 *new_view = log_view;
7803 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7805 struct tog_reflist_entry *re;
7808 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7811 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7812 while (i++ < maxscroll) {
7815 s->first_displayed_entry = re;
7816 re = TAILQ_PREV(re, tog_reflist_head, entry);
7820 static const struct got_error *
7821 ref_scroll_down(struct tog_view *view, int maxscroll)
7823 struct tog_ref_view_state *s = &view->state.ref;
7824 struct tog_reflist_entry *next, *last;
7827 if (s->first_displayed_entry)
7828 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7830 next = TAILQ_FIRST(&s->refs);
7832 last = s->last_displayed_entry;
7833 while (next && n++ < maxscroll) {
7835 s->last_displayed_entry = last;
7836 last = TAILQ_NEXT(last, entry);
7838 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7839 s->first_displayed_entry = next;
7840 next = TAILQ_NEXT(next, entry);
7847 static const struct got_error *
7848 search_start_ref_view(struct tog_view *view)
7850 struct tog_ref_view_state *s = &view->state.ref;
7852 s->matched_entry = NULL;
7857 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7859 regmatch_t regmatch;
7861 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7865 static const struct got_error *
7866 search_next_ref_view(struct tog_view *view)
7868 struct tog_ref_view_state *s = &view->state.ref;
7869 struct tog_reflist_entry *re = NULL;
7871 if (!view->searching) {
7872 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7876 if (s->matched_entry) {
7877 if (view->searching == TOG_SEARCH_FORWARD) {
7878 if (s->selected_entry)
7879 re = TAILQ_NEXT(s->selected_entry, entry);
7881 re = TAILQ_PREV(s->selected_entry,
7882 tog_reflist_head, entry);
7884 if (s->selected_entry == NULL)
7885 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7887 re = TAILQ_PREV(s->selected_entry,
7888 tog_reflist_head, entry);
7891 if (s->selected_entry)
7892 re = s->selected_entry;
7893 else if (view->searching == TOG_SEARCH_FORWARD)
7894 re = TAILQ_FIRST(&s->refs);
7896 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7901 if (s->matched_entry == NULL) {
7902 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7905 if (view->searching == TOG_SEARCH_FORWARD)
7906 re = TAILQ_FIRST(&s->refs);
7908 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7911 if (match_reflist_entry(re, &view->regex)) {
7912 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7913 s->matched_entry = re;
7917 if (view->searching == TOG_SEARCH_FORWARD)
7918 re = TAILQ_NEXT(re, entry);
7920 re = TAILQ_PREV(re, tog_reflist_head, entry);
7923 if (s->matched_entry) {
7924 s->first_displayed_entry = s->matched_entry;
7931 static const struct got_error *
7932 show_ref_view(struct tog_view *view)
7934 const struct got_error *err = NULL;
7935 struct tog_ref_view_state *s = &view->state.ref;
7936 struct tog_reflist_entry *re;
7939 struct tog_color *tc;
7941 int limit = view->nlines;
7943 werase(view->window);
7946 if (view_is_hsplit_top(view))
7947 --limit; /* border */
7952 re = s->first_displayed_entry;
7954 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7956 return got_error_from_errno("asprintf");
7958 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7963 if (view_needs_focus_indication(view))
7964 wstandout(view->window);
7965 waddwstr(view->window, wline);
7966 while (width++ < view->ncols)
7967 waddch(view->window, ' ');
7968 if (view_needs_focus_indication(view))
7969 wstandend(view->window);
7978 while (re && limit > 0) {
7980 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7983 struct got_commit_object *ci;
7984 struct got_tag_object *tag;
7985 struct got_object_id *id;
7989 err = got_ref_resolve(&id, s->repo, re->ref);
7992 err = got_object_open_as_tag(&tag, s->repo, id);
7994 if (err->code != GOT_ERR_OBJ_TYPE) {
7998 err = got_object_open_as_commit(&ci, s->repo,
8004 t = got_object_commit_get_committer_time(ci);
8005 got_object_commit_close(ci);
8007 t = got_object_tag_get_tagger_time(tag);
8008 got_object_tag_close(tag);
8011 if (gmtime_r(&t, &tm) == NULL)
8012 return got_error_from_errno("gmtime_r");
8013 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8014 return got_error(GOT_ERR_NO_SPACE);
8016 if (got_ref_is_symbolic(re->ref)) {
8017 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8018 ymd : "", got_ref_get_name(re->ref),
8019 got_ref_get_symref_target(re->ref)) == -1)
8020 return got_error_from_errno("asprintf");
8021 } else if (s->show_ids) {
8022 struct got_object_id *id;
8024 err = got_ref_resolve(&id, s->repo, re->ref);
8027 err = got_object_id_str(&id_str, id);
8032 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8033 got_ref_get_name(re->ref), id_str) == -1) {
8034 err = got_error_from_errno("asprintf");
8041 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8042 got_ref_get_name(re->ref)) == -1)
8043 return got_error_from_errno("asprintf");
8045 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
8051 if (n == s->selected) {
8053 wstandout(view->window);
8054 s->selected_entry = re;
8056 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8058 wattr_on(view->window,
8059 COLOR_PAIR(tc->colorpair), NULL);
8060 waddwstr(view->window, wline);
8062 wattr_off(view->window,
8063 COLOR_PAIR(tc->colorpair), NULL);
8064 if (width < view->ncols - 1)
8065 waddch(view->window, '\n');
8066 if (n == s->selected && view->focussed)
8067 wstandend(view->window);
8073 s->last_displayed_entry = re;
8076 re = TAILQ_NEXT(re, entry);
8083 static const struct got_error *
8084 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8085 struct tog_reflist_entry *re, struct got_repository *repo)
8087 const struct got_error *err = NULL;
8088 struct got_object_id *commit_id = NULL;
8089 struct tog_view *tree_view;
8093 err = resolve_reflist_entry(&commit_id, re, repo);
8095 if (err->code != GOT_ERR_OBJ_TYPE)
8102 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8103 if (tree_view == NULL) {
8104 err = got_error_from_errno("view_open");
8108 err = open_tree_view(tree_view, commit_id,
8109 got_ref_get_name(re->ref), repo);
8113 *new_view = tree_view;
8119 static const struct got_error *
8120 ref_goto_line(struct tog_view *view, int nlines)
8122 const struct got_error *err = NULL;
8123 struct tog_ref_view_state *s = &view->state.ref;
8124 int g, idx = s->selected_entry->idx;
8131 else if (g > s->nrefs)
8134 if (g >= s->first_displayed_entry->idx + 1 &&
8135 g <= s->last_displayed_entry->idx + 1 &&
8136 g - s->first_displayed_entry->idx - 1 < nlines) {
8137 s->selected = g - s->first_displayed_entry->idx - 1;
8142 err = ref_scroll_down(view, g - idx - 1);
8145 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8146 s->first_displayed_entry->idx + s->selected < g &&
8147 s->selected < s->ndisplayed - 1)
8148 s->selected = g - s->first_displayed_entry->idx - 1;
8149 } else if (idx + 1 > g)
8150 ref_scroll_up(s, idx - g + 1);
8152 if (g < nlines && s->first_displayed_entry->idx == 0)
8153 s->selected = g - 1;
8159 static const struct got_error *
8160 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8162 const struct got_error *err = NULL;
8163 struct tog_ref_view_state *s = &view->state.ref;
8164 struct tog_reflist_entry *re;
8165 int n, nscroll = view->nlines - 1;
8168 return ref_goto_line(view, nscroll);
8172 s->show_ids = !s->show_ids;
8176 s->show_date = !s->show_date;
8180 s->sort_by_date = !s->sort_by_date;
8182 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8183 got_ref_cmp_by_commit_timestamp_descending :
8184 tog_ref_cmp_by_name, s->repo);
8187 got_reflist_object_id_map_free(tog_refs_idmap);
8188 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8189 &tog_refs, s->repo);
8192 ref_view_free_refs(s);
8193 err = ref_view_load_refs(s);
8198 if (!s->selected_entry)
8200 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8204 if (!s->selected_entry)
8206 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8212 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8216 int eos = view->nlines - 1;
8218 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8222 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8223 for (n = 0; n < eos; n++) {
8226 s->first_displayed_entry = re;
8227 re = TAILQ_PREV(re, tog_reflist_head, entry);
8230 s->selected = n - 1;
8236 if (s->selected > 0) {
8240 ref_scroll_up(s, 1);
8241 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8251 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8252 s->selected -= MIN(nscroll, s->selected);
8253 ref_scroll_up(s, MAX(0, nscroll));
8254 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8260 if (s->selected < s->ndisplayed - 1) {
8264 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8265 /* can't scroll any further */
8269 ref_scroll_down(view, 1);
8279 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8280 /* can't scroll any further; move cursor down */
8281 if (s->selected < s->ndisplayed - 1)
8282 s->selected += MIN(nscroll,
8283 s->ndisplayed - s->selected - 1);
8284 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8285 s->selected += s->ndisplayed - s->selected - 1;
8289 ref_scroll_down(view, nscroll);
8294 err = tog_load_refs(s->repo, s->sort_by_date);
8297 ref_view_free_refs(s);
8298 err = ref_view_load_refs(s);
8301 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8302 s->selected = view->nlines - 2;
8316 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8321 static const struct got_error *
8322 cmd_ref(int argc, char *argv[])
8324 const struct got_error *error;
8325 struct got_repository *repo = NULL;
8326 struct got_worktree *worktree = NULL;
8327 char *cwd = NULL, *repo_path = NULL;
8329 struct tog_view *view;
8330 int *pack_fds = NULL;
8332 while ((ch = getopt(argc, argv, "r:")) != -1) {
8335 repo_path = realpath(optarg, NULL);
8336 if (repo_path == NULL)
8337 return got_error_from_errno2("realpath",
8352 error = got_repo_pack_fds_open(&pack_fds);
8356 if (repo_path == NULL) {
8357 cwd = getcwd(NULL, 0);
8359 return got_error_from_errno("getcwd");
8360 error = got_worktree_open(&worktree, cwd);
8361 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8365 strdup(got_worktree_get_repo_path(worktree));
8367 repo_path = strdup(cwd);
8368 if (repo_path == NULL) {
8369 error = got_error_from_errno("strdup");
8374 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8380 error = apply_unveil(got_repo_get_path(repo), NULL);
8384 error = tog_load_refs(repo, 0);
8388 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8390 error = got_error_from_errno("view_open");
8394 error = open_ref_view(view, repo);
8399 /* Release work tree lock. */
8400 got_worktree_close(worktree);
8403 error = view_loop(view);
8408 const struct got_error *close_err = got_repo_close(repo);
8413 const struct got_error *pack_err =
8414 got_repo_pack_fds_close(pack_fds);
8422 static const struct got_error*
8423 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8432 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8436 if (mvwprintw(win, y, x, "%s", str) == ERR)
8437 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8444 static const struct got_error *
8445 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8449 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8451 free(*line_offsets);
8452 *line_offsets = NULL;
8453 return got_error_from_errno("reallocarray");
8457 (*line_offsets)[*nlines] = off;
8462 static const struct got_error *
8463 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8467 for (;n > 0; --n, ++km) {
8471 if (km->keys == NULL)
8474 t = t0 = strdup(km->keys);
8476 return got_error_from_errno("strdup");
8479 while ((k = strsep(&t, " ")) != NULL)
8480 len += strlen(k) > 1 ? 2 : 0;
8482 *ret = MAX(*ret, len);
8489 * Write keymap section headers, keys, and key info in km to f.
8490 * Save line offset to *off. If terminal has UTF8 encoding enabled,
8491 * wrap control and symbolic keys in guillemets, else use <>.
8493 static const struct got_error *
8494 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8499 static const char *u8_glyph[] = {
8500 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8501 "\xe2\x80\xba" /* U+203A (utf8 >) */
8504 int cs, s, first = 1;
8506 cs = got_locale_is_utf8();
8508 t = t0 = strdup(km->keys);
8510 return got_error_from_errno("strdup");
8512 len = strlen(km->keys);
8513 while ((k = strsep(&t, " ")) != NULL) {
8514 s = strlen(k) > 1; /* control or symbolic key */
8515 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8516 cs && s ? u8_glyph[0] : s ? "<" : "", k,
8517 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8520 return got_error_from_errno("fprintf");
8528 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8530 return got_error_from_errno("fprintf");
8536 static const struct got_error *
8537 format_help(struct tog_help_view_state *s)
8539 const struct got_error *err = NULL;
8541 int i, max, n, show = s->all;
8542 static const struct tog_key_map km[] = {
8543 #define KEYMAP_(info, type) { NULL, (info), type }
8544 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8550 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8555 err = max_key_str(&max, km, n);
8559 for (i = 0; i < n; ++i) {
8560 if (km[i].keys == NULL) {
8562 if (km[i].type == TOG_KEYMAP_GLOBAL ||
8563 km[i].type == s->type || s->all)
8567 err = format_help_line(&off, s->f, &km[i], max);
8570 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8577 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8581 static const struct got_error *
8582 create_help(struct tog_help_view_state *s)
8585 const struct got_error *err;
8587 free(s->line_offsets);
8588 s->line_offsets = NULL;
8593 return got_error_from_errno("got_opentemp");
8596 err = format_help(s);
8600 if (s->f && fflush(s->f) != 0)
8601 return got_error_from_errno("fflush");
8606 static const struct got_error *
8607 search_start_help_view(struct tog_view *view)
8609 view->state.help.matched_line = 0;
8614 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8615 size_t *nlines, int **first, int **last, int **match, int **selected)
8617 struct tog_help_view_state *s = &view->state.help;
8620 *nlines = s->nlines;
8621 *line_offsets = s->line_offsets;
8622 *match = &s->matched_line;
8623 *first = &s->first_displayed_line;
8624 *last = &s->last_displayed_line;
8625 *selected = &s->selected_line;
8628 static const struct got_error *
8629 show_help_view(struct tog_view *view)
8631 struct tog_help_view_state *s = &view->state.help;
8632 const struct got_error *err;
8633 regmatch_t *regmatch = &view->regmatch;
8638 int width, nprinted = 0, rc = 0;
8639 int eos = view->nlines;
8641 if (view_is_hsplit_top(view))
8642 --eos; /* account for border */
8646 werase(view->window);
8648 if (view->gline > s->nlines - 1)
8649 view->gline = s->nlines - 1;
8651 err = win_draw_center(view->window, 0, 0, view->ncols,
8652 view_needs_focus_indication(view),
8653 "tog help (press q to return to tog)");
8658 waddstr(view->window, "\n\n");
8664 while (eos > 0 && nprinted < eos) {
8667 linelen = getline(&line, &linesz, s->f);
8668 if (linelen == -1) {
8671 return got_ferror(s->f, GOT_ERR_IO);
8676 if (++s->lineno < s->first_displayed_line)
8678 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8680 if (s->lineno == view->hiline)
8683 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8689 view->maxx = MAX(view->maxx, width);
8694 wattron(view->window, attr);
8695 if (s->first_displayed_line + nprinted == s->matched_line &&
8696 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8697 err = add_matched_line(&width, line, view->ncols - 1, 0,
8698 view->window, view->x, regmatch);
8706 err = format_line(&wline, &width, &skip, line,
8707 view->x, view->ncols - 1, 0, view->x ? 1 : 0);
8712 rc = waddwstr(view->window, &wline[skip]);
8716 return got_error_msg(GOT_ERR_IO, "waddwstr");
8718 if (s->lineno == view->hiline) {
8719 while (width++ < view->ncols)
8720 waddch(view->window, ' ');
8722 if (width <= view->ncols)
8723 waddch(view->window, '\n');
8726 wattroff(view->window, attr);
8727 if (++nprinted == 1)
8728 s->first_displayed_line = s->lineno;
8732 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8734 s->last_displayed_line = s->first_displayed_line;
8739 rc = waddnstr(view->window,
8740 "See the tog(1) manual page for full documentation",
8743 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8745 wmove(view->window, view->nlines - 1, 0);
8746 wclrtoeol(view->window);
8747 wstandout(view->window);
8748 rc = waddnstr(view->window, "scroll down for more...",
8751 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8752 if (getcurx(view->window) < view->ncols - 6) {
8753 rc = wprintw(view->window, "[%.0f%%]",
8754 100.00 * s->last_displayed_line / s->nlines);
8756 return got_error_msg(GOT_ERR_IO, "wprintw");
8758 wstandend(view->window);
8764 static const struct got_error *
8765 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8767 struct tog_help_view_state *s = &view->state.help;
8768 const struct got_error *err = NULL;
8774 eos = nscroll = view->nlines;
8775 if (view_is_hsplit_top(view))
8778 s->lineno = s->first_displayed_line - 1 + s->selected_line;
8785 view->x = MAX(view->maxx - view->ncols / 3, 0);
8790 if (view->x + view->ncols / 3 < view->maxx)
8797 view->x -= MIN(view->x, 2);
8803 s->first_displayed_line = 1;
8811 s->first_displayed_line = (s->nlines - eos) + 3;
8816 if (s->first_displayed_line > 1)
8817 --s->first_displayed_line;
8828 if (s->first_displayed_line == 1) {
8832 while (--nscroll > 0 && s->first_displayed_line > 1)
8833 s->first_displayed_line--;
8839 ++s->first_displayed_line;
8855 while (!s->eof && --nscroll > 0) {
8856 linelen = getline(&line, &linesz, s->f);
8857 s->first_displayed_line++;
8858 if (linelen == -1) {
8862 err = got_ferror(s->f, GOT_ERR_IO);
8876 static const struct got_error *
8877 close_help_view(struct tog_view *view)
8879 struct tog_help_view_state *s = &view->state.help;
8881 free(s->line_offsets);
8882 s->line_offsets = NULL;
8883 if (fclose(s->f) == EOF)
8884 return got_error_from_errno("fclose");
8889 static const struct got_error *
8890 reset_help_view(struct tog_view *view)
8892 struct tog_help_view_state *s = &view->state.help;
8895 if (s->f && fclose(s->f) == EOF)
8896 return got_error_from_errno("fclose");
8898 wclear(view->window);
8902 s->first_displayed_line = 1;
8903 s->last_displayed_line = view->nlines;
8904 s->matched_line = 0;
8906 return create_help(s);
8909 static const struct got_error *
8910 open_help_view(struct tog_view *view, struct tog_view *parent)
8912 const struct got_error *err = NULL;
8913 struct tog_help_view_state *s = &view->state.help;
8915 s->type = (enum tog_keymap_type)parent->type;
8916 s->first_displayed_line = 1;
8917 s->last_displayed_line = view->nlines;
8918 s->selected_line = 1;
8920 view->show = show_help_view;
8921 view->input = input_help_view;
8922 view->reset = reset_help_view;
8923 view->close = close_help_view;
8924 view->search_start = search_start_help_view;
8925 view->search_setup = search_setup_help_view;
8926 view->search_next = search_next_view_match;
8928 err = create_help(s);
8932 static const struct got_error *
8933 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8934 enum tog_view_type request, int y, int x)
8936 const struct got_error *err = NULL;
8942 if (view->type == TOG_VIEW_LOG) {
8943 struct tog_log_view_state *s = &view->state.log;
8945 err = open_diff_view_for_commit(new_view, y, x,
8946 s->selected_entry->commit, s->selected_entry->id,
8949 return got_error_msg(GOT_ERR_NOT_IMPL,
8950 "parent/child view pair not supported");
8952 case TOG_VIEW_BLAME:
8953 if (view->type == TOG_VIEW_TREE) {
8954 struct tog_tree_view_state *s = &view->state.tree;
8956 err = blame_tree_entry(new_view, y, x,
8957 s->selected_entry, &s->parents, s->commit_id,
8960 return got_error_msg(GOT_ERR_NOT_IMPL,
8961 "parent/child view pair not supported");
8964 if (view->type == TOG_VIEW_BLAME)
8965 err = log_annotated_line(new_view, y, x,
8966 view->state.blame.repo, view->state.blame.id_to_log);
8967 else if (view->type == TOG_VIEW_TREE)
8968 err = log_selected_tree_entry(new_view, y, x,
8970 else if (view->type == TOG_VIEW_REF)
8971 err = log_ref_entry(new_view, y, x,
8972 view->state.ref.selected_entry,
8973 view->state.ref.repo);
8975 return got_error_msg(GOT_ERR_NOT_IMPL,
8976 "parent/child view pair not supported");
8979 if (view->type == TOG_VIEW_LOG)
8980 err = browse_commit_tree(new_view, y, x,
8981 view->state.log.selected_entry,
8982 view->state.log.in_repo_path,
8983 view->state.log.head_ref_name,
8984 view->state.log.repo);
8985 else if (view->type == TOG_VIEW_REF)
8986 err = browse_ref_tree(new_view, y, x,
8987 view->state.ref.selected_entry,
8988 view->state.ref.repo);
8990 return got_error_msg(GOT_ERR_NOT_IMPL,
8991 "parent/child view pair not supported");
8994 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8995 if (*new_view == NULL)
8996 return got_error_from_errno("view_open");
8997 if (view->type == TOG_VIEW_LOG)
8998 err = open_ref_view(*new_view, view->state.log.repo);
8999 else if (view->type == TOG_VIEW_TREE)
9000 err = open_ref_view(*new_view, view->state.tree.repo);
9002 err = got_error_msg(GOT_ERR_NOT_IMPL,
9003 "parent/child view pair not supported");
9005 view_close(*new_view);
9008 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9009 if (*new_view == NULL)
9010 return got_error_from_errno("view_open");
9011 err = open_help_view(*new_view, view);
9013 view_close(*new_view);
9016 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9023 * If view was scrolled down to move the selected line into view when opening a
9024 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9027 offset_selection_up(struct tog_view *view)
9029 switch (view->type) {
9030 case TOG_VIEW_BLAME: {
9031 struct tog_blame_view_state *s = &view->state.blame;
9032 if (s->first_displayed_line == 1) {
9033 s->selected_line = MAX(s->selected_line - view->offset,
9037 if (s->first_displayed_line > view->offset)
9038 s->first_displayed_line -= view->offset;
9040 s->first_displayed_line = 1;
9041 s->selected_line += view->offset;
9045 log_scroll_up(&view->state.log, view->offset);
9046 view->state.log.selected += view->offset;
9049 ref_scroll_up(&view->state.ref, view->offset);
9050 view->state.ref.selected += view->offset;
9053 tree_scroll_up(&view->state.tree, view->offset);
9054 view->state.tree.selected += view->offset;
9064 * If the selected line is in the section of screen covered by the bottom split,
9065 * scroll down offset lines to move it into view and index its new position.
9067 static const struct got_error *
9068 offset_selection_down(struct tog_view *view)
9070 const struct got_error *err = NULL;
9071 const struct got_error *(*scrolld)(struct tog_view *, int);
9072 int *selected = NULL;
9075 switch (view->type) {
9076 case TOG_VIEW_BLAME: {
9077 struct tog_blame_view_state *s = &view->state.blame;
9080 if (s->selected_line > view->nlines - header) {
9081 offset = abs(view->nlines - s->selected_line - header);
9082 s->first_displayed_line += offset;
9083 s->selected_line -= offset;
9084 view->offset = offset;
9088 case TOG_VIEW_LOG: {
9089 struct tog_log_view_state *s = &view->state.log;
9090 scrolld = &log_scroll_down;
9091 header = view_is_parent_view(view) ? 3 : 2;
9092 selected = &s->selected;
9095 case TOG_VIEW_REF: {
9096 struct tog_ref_view_state *s = &view->state.ref;
9097 scrolld = &ref_scroll_down;
9099 selected = &s->selected;
9102 case TOG_VIEW_TREE: {
9103 struct tog_tree_view_state *s = &view->state.tree;
9104 scrolld = &tree_scroll_down;
9106 selected = &s->selected;
9116 if (selected && *selected > view->nlines - header) {
9117 offset = abs(view->nlines - *selected - header);
9118 view->offset = offset;
9119 if (scrolld && offset) {
9120 err = scrolld(view, offset);
9121 *selected -= offset;
9129 list_commands(FILE *fp)
9133 fprintf(fp, "commands:");
9134 for (i = 0; i < nitems(tog_commands); i++) {
9135 const struct tog_cmd *cmd = &tog_commands[i];
9136 fprintf(fp, " %s", cmd->name);
9142 usage(int hflag, int status)
9144 FILE *fp = (status == 0) ? stdout : stderr;
9146 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9149 fprintf(fp, "lazy usage: %s path\n", getprogname());
9156 make_argv(int argc, ...)
9164 argv = calloc(argc, sizeof(char *));
9167 for (i = 0; i < argc; i++) {
9168 argv[i] = strdup(va_arg(ap, char *));
9169 if (argv[i] == NULL)
9178 * Try to convert 'tog path' into a 'tog log path' command.
9179 * The user could simply have mistyped the command rather than knowingly
9180 * provided a path. So check whether argv[0] can in fact be resolved
9181 * to a path in the HEAD commit and print a special error if not.
9182 * This hack is for mpi@ <3
9184 static const struct got_error *
9185 tog_log_with_path(int argc, char *argv[])
9187 const struct got_error *error = NULL, *close_err;
9188 const struct tog_cmd *cmd = NULL;
9189 struct got_repository *repo = NULL;
9190 struct got_worktree *worktree = NULL;
9191 struct got_object_id *commit_id = NULL, *id = NULL;
9192 struct got_commit_object *commit = NULL;
9193 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9194 char *commit_id_str = NULL, **cmd_argv = NULL;
9195 int *pack_fds = NULL;
9197 cwd = getcwd(NULL, 0);
9199 return got_error_from_errno("getcwd");
9201 error = got_repo_pack_fds_open(&pack_fds);
9205 error = got_worktree_open(&worktree, cwd);
9206 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9210 repo_path = strdup(got_worktree_get_repo_path(worktree));
9212 repo_path = strdup(cwd);
9213 if (repo_path == NULL) {
9214 error = got_error_from_errno("strdup");
9218 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9222 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9227 error = tog_load_refs(repo, 0);
9230 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9231 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9232 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9237 got_worktree_close(worktree);
9241 error = got_object_open_as_commit(&commit, repo, commit_id);
9245 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9247 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9249 fprintf(stderr, "%s: '%s' is no known command or path\n",
9250 getprogname(), argv[0]);
9255 error = got_object_id_str(&commit_id_str, commit_id);
9259 cmd = &tog_commands[0]; /* log */
9261 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9262 error = cmd->cmd_main(argc, cmd_argv);
9265 close_err = got_repo_close(repo);
9270 got_object_commit_close(commit);
9272 got_worktree_close(worktree);
9274 const struct got_error *pack_err =
9275 got_repo_pack_fds_close(pack_fds);
9280 free(commit_id_str);
9287 for (i = 0; i < argc; i++)
9296 main(int argc, char *argv[])
9298 const struct got_error *error = NULL;
9299 const struct tog_cmd *cmd = NULL;
9300 int ch, hflag = 0, Vflag = 0;
9301 char **cmd_argv = NULL;
9302 static const struct option longopts[] = {
9303 { "version", no_argument, NULL, 'V' },
9306 char *diff_algo_str = NULL;
9308 if (!isatty(STDIN_FILENO))
9309 errx(1, "standard input is not a tty");
9311 setlocale(LC_CTYPE, "");
9313 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9333 got_version_print_str();
9338 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9346 /* Build an argument vector which runs a default command. */
9347 cmd = &tog_commands[0];
9349 cmd_argv = make_argv(argc, cmd->name);
9353 /* Did the user specify a command? */
9354 for (i = 0; i < nitems(tog_commands); i++) {
9355 if (strncmp(tog_commands[i].name, argv[0],
9356 strlen(argv[0])) == 0) {
9357 cmd = &tog_commands[i];
9363 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9364 if (diff_algo_str) {
9365 if (strcasecmp(diff_algo_str, "patience") == 0)
9366 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9367 if (strcasecmp(diff_algo_str, "myers") == 0)
9368 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9374 /* No command specified; try log with a path */
9375 error = tog_log_with_path(argc, argv);
9380 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9387 for (i = 0; i < argc; i++)
9392 if (error && error->code != GOT_ERR_CANCELLED &&
9393 error->code != GOT_ERR_EOF &&
9394 error->code != GOT_ERR_PRIVSEP_EXIT &&
9395 error->code != GOT_ERR_PRIVSEP_PIPE &&
9396 !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9397 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);