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 },
114 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
116 #define TOG_EOF_STRING "(END)"
118 struct commit_queue_entry {
119 TAILQ_ENTRY(commit_queue_entry) entry;
120 struct got_object_id *id;
121 struct got_commit_object *commit;
124 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
125 struct commit_queue {
127 struct commit_queue_head head;
131 STAILQ_ENTRY(tog_color) entry;
135 STAILQ_HEAD(tog_colors, tog_color);
137 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
138 static struct got_reflist_object_id_map *tog_refs_idmap;
139 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
141 static const struct got_error *
142 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
143 struct got_reference* re2)
145 const char *name1 = got_ref_get_name(re1);
146 const char *name2 = got_ref_get_name(re2);
147 int isbackup1, isbackup2;
149 /* Sort backup refs towards the bottom of the list. */
150 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
151 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
152 if (!isbackup1 && isbackup2) {
155 } else if (isbackup1 && !isbackup2) {
160 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
164 static const struct got_error *
165 tog_load_refs(struct got_repository *repo, int sort_by_date)
167 const struct got_error *err;
169 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
170 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
175 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
182 if (tog_refs_idmap) {
183 got_reflist_object_id_map_free(tog_refs_idmap);
184 tog_refs_idmap = NULL;
186 got_ref_list_free(&tog_refs);
189 static const struct got_error *
190 add_color(struct tog_colors *colors, const char *pattern,
191 int idx, short color)
193 const struct got_error *err = NULL;
194 struct tog_color *tc;
197 if (idx < 1 || idx > COLOR_PAIRS - 1)
200 init_pair(idx, color, -1);
202 tc = calloc(1, sizeof(*tc));
204 return got_error_from_errno("calloc");
205 regerr = regcomp(&tc->regex, pattern,
206 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
208 static char regerr_msg[512];
209 static char err_msg[512];
210 regerror(regerr, &tc->regex, regerr_msg,
212 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
214 err = got_error_msg(GOT_ERR_REGEX, err_msg);
219 STAILQ_INSERT_HEAD(colors, tc, entry);
224 free_colors(struct tog_colors *colors)
226 struct tog_color *tc;
228 while (!STAILQ_EMPTY(colors)) {
229 tc = STAILQ_FIRST(colors);
230 STAILQ_REMOVE_HEAD(colors, entry);
236 static struct tog_color *
237 get_color(struct tog_colors *colors, int colorpair)
239 struct tog_color *tc = NULL;
241 STAILQ_FOREACH(tc, colors, entry) {
242 if (tc->colorpair == colorpair)
250 default_color_value(const char *envvar)
252 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
253 return COLOR_MAGENTA;
254 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
256 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
258 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
260 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
261 return COLOR_MAGENTA;
262 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
263 return COLOR_MAGENTA;
264 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
266 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
268 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
270 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
272 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
274 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
276 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
277 return COLOR_MAGENTA;
278 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
280 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
287 get_color_value(const char *envvar)
289 const char *val = getenv(envvar);
292 return default_color_value(envvar);
294 if (strcasecmp(val, "black") == 0)
296 if (strcasecmp(val, "red") == 0)
298 if (strcasecmp(val, "green") == 0)
300 if (strcasecmp(val, "yellow") == 0)
302 if (strcasecmp(val, "blue") == 0)
304 if (strcasecmp(val, "magenta") == 0)
305 return COLOR_MAGENTA;
306 if (strcasecmp(val, "cyan") == 0)
308 if (strcasecmp(val, "white") == 0)
310 if (strcasecmp(val, "default") == 0)
313 return default_color_value(envvar);
316 struct tog_diff_view_state {
317 struct got_object_id *id1, *id2;
318 const char *label1, *label2;
322 int first_displayed_line;
323 int last_displayed_line;
326 int ignore_whitespace;
328 struct got_repository *repo;
329 struct got_diff_line *lines;
334 /* passed from log or blame view; may be NULL */
335 struct tog_view *parent_view;
338 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
339 static volatile sig_atomic_t tog_thread_error;
341 struct tog_log_thread_args {
342 pthread_cond_t need_commits;
343 pthread_cond_t commit_loaded;
346 struct got_commit_graph *graph;
347 struct commit_queue *real_commits;
348 const char *in_repo_path;
349 struct got_object_id *start_id;
350 struct got_repository *repo;
354 struct commit_queue_entry **first_displayed_entry;
355 struct commit_queue_entry **selected_entry;
357 int *search_next_done;
361 regex_t *limit_regex;
362 struct commit_queue *limit_commits;
365 struct tog_log_view_state {
366 struct commit_queue *commits;
367 struct commit_queue_entry *first_displayed_entry;
368 struct commit_queue_entry *last_displayed_entry;
369 struct commit_queue_entry *selected_entry;
370 struct commit_queue real_commits;
375 struct got_repository *repo;
376 struct got_object_id *start_id;
379 struct tog_log_thread_args thread_args;
380 struct commit_queue_entry *matched_entry;
381 struct commit_queue_entry *search_entry;
382 struct tog_colors colors;
386 struct commit_queue limit_commits;
389 #define TOG_COLOR_DIFF_MINUS 1
390 #define TOG_COLOR_DIFF_PLUS 2
391 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
392 #define TOG_COLOR_DIFF_META 4
393 #define TOG_COLOR_TREE_SUBMODULE 5
394 #define TOG_COLOR_TREE_SYMLINK 6
395 #define TOG_COLOR_TREE_DIRECTORY 7
396 #define TOG_COLOR_TREE_EXECUTABLE 8
397 #define TOG_COLOR_COMMIT 9
398 #define TOG_COLOR_AUTHOR 10
399 #define TOG_COLOR_DATE 11
400 #define TOG_COLOR_REFS_HEADS 12
401 #define TOG_COLOR_REFS_TAGS 13
402 #define TOG_COLOR_REFS_REMOTES 14
403 #define TOG_COLOR_REFS_BACKUP 15
405 struct tog_blame_cb_args {
406 struct tog_blame_line *lines; /* one per line */
409 struct tog_view *view;
410 struct got_object_id *commit_id;
414 struct tog_blame_thread_args {
416 struct got_repository *repo;
417 struct tog_blame_cb_args *cb_args;
419 got_cancel_cb cancel_cb;
426 struct tog_blame_line *lines;
430 struct tog_blame_thread_args thread_args;
431 struct tog_blame_cb_args cb_args;
436 struct tog_blame_view_state {
437 int first_displayed_line;
438 int last_displayed_line;
440 int last_diffed_line;
444 struct got_object_id_queue blamed_commits;
445 struct got_object_qid *blamed_commit;
447 struct got_repository *repo;
448 struct got_object_id *commit_id;
449 struct got_object_id *id_to_log;
450 struct tog_blame blame;
452 struct tog_colors colors;
455 struct tog_parent_tree {
456 TAILQ_ENTRY(tog_parent_tree) entry;
457 struct got_tree_object *tree;
458 struct got_tree_entry *first_displayed_entry;
459 struct got_tree_entry *selected_entry;
463 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
465 struct tog_tree_view_state {
467 struct got_object_id *commit_id;/* commit which this tree belongs to */
468 struct got_tree_object *root; /* the commit's root tree entry */
469 struct got_tree_object *tree; /* currently displayed (sub-)tree */
470 struct got_tree_entry *first_displayed_entry;
471 struct got_tree_entry *last_displayed_entry;
472 struct got_tree_entry *selected_entry;
473 int ndisplayed, selected, show_ids;
474 struct tog_parent_trees parents; /* parent trees of current sub-tree */
476 struct got_repository *repo;
477 struct got_tree_entry *matched_entry;
478 struct tog_colors colors;
481 struct tog_reflist_entry {
482 TAILQ_ENTRY(tog_reflist_entry) entry;
483 struct got_reference *ref;
487 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
489 struct tog_ref_view_state {
490 struct tog_reflist_head refs;
491 struct tog_reflist_entry *first_displayed_entry;
492 struct tog_reflist_entry *last_displayed_entry;
493 struct tog_reflist_entry *selected_entry;
494 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
495 struct got_repository *repo;
496 struct tog_reflist_entry *matched_entry;
497 struct tog_colors colors;
501 * We implement two types of views: parent views and child views.
503 * The 'Tab' key switches focus between a parent view and its child view.
504 * Child views are shown side-by-side to their parent view, provided
505 * there is enough screen estate.
507 * When a new view is opened from within a parent view, this new view
508 * becomes a child view of the parent view, replacing any existing child.
510 * When a new view is opened from within a child view, this new view
511 * becomes a parent view which will obscure the views below until the
512 * user quits the new parent view by typing 'q'.
514 * This list of views contains parent views only.
515 * Child views are only pointed to by their parent view.
517 TAILQ_HEAD(tog_view_list_head, tog_view);
520 TAILQ_ENTRY(tog_view) entry;
523 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
524 int resized_y, resized_x; /* begin_y/x based on user resizing */
525 int maxx, x; /* max column and current start column */
526 int lines, cols; /* copies of LINES and COLS */
527 int nscrolled, offset; /* lines scrolled and hsplit line offset */
528 int gline, hiline; /* navigate to and highlight this nG line */
529 int ch, count; /* current keymap and count prefix */
530 int resized; /* set when in a resize event */
531 int focussed; /* Only set on one parent or child view at a time. */
533 struct tog_view *parent;
534 struct tog_view *child;
537 * This flag is initially set on parent views when a new child view
538 * is created. It gets toggled when the 'Tab' key switches focus
539 * between parent and child.
540 * The flag indicates whether focus should be passed on to our child
541 * view if this parent view gets picked for focus after another parent
542 * view was closed. This prevents child views from losing focus in such
547 enum tog_view_mode mode;
548 /* type-specific state */
549 enum tog_view_type type;
551 struct tog_diff_view_state diff;
552 struct tog_log_view_state log;
553 struct tog_blame_view_state blame;
554 struct tog_tree_view_state tree;
555 struct tog_ref_view_state ref;
558 const struct got_error *(*show)(struct tog_view *);
559 const struct got_error *(*input)(struct tog_view **,
560 struct tog_view *, int);
561 const struct got_error *(*reset)(struct tog_view *);
562 const struct got_error *(*resize)(struct tog_view *, int);
563 const struct got_error *(*close)(struct tog_view *);
565 const struct got_error *(*search_start)(struct tog_view *);
566 const struct got_error *(*search_next)(struct tog_view *);
569 #define TOG_SEARCH_FORWARD 1
570 #define TOG_SEARCH_BACKWARD 2
571 int search_next_done;
572 #define TOG_SEARCH_HAVE_MORE 1
573 #define TOG_SEARCH_NO_MORE 2
574 #define TOG_SEARCH_HAVE_NONE 3
579 static const struct got_error *open_diff_view(struct tog_view *,
580 struct got_object_id *, struct got_object_id *,
581 const char *, const char *, int, int, int, struct tog_view *,
582 struct got_repository *);
583 static const struct got_error *show_diff_view(struct tog_view *);
584 static const struct got_error *input_diff_view(struct tog_view **,
585 struct tog_view *, int);
586 static const struct got_error *reset_diff_view(struct tog_view *);
587 static const struct got_error* close_diff_view(struct tog_view *);
588 static const struct got_error *search_start_diff_view(struct tog_view *);
589 static const struct got_error *search_next_diff_view(struct tog_view *);
591 static const struct got_error *open_log_view(struct tog_view *,
592 struct got_object_id *, struct got_repository *,
593 const char *, const char *, int);
594 static const struct got_error * show_log_view(struct tog_view *);
595 static const struct got_error *input_log_view(struct tog_view **,
596 struct tog_view *, int);
597 static const struct got_error *resize_log_view(struct tog_view *, int);
598 static const struct got_error *close_log_view(struct tog_view *);
599 static const struct got_error *search_start_log_view(struct tog_view *);
600 static const struct got_error *search_next_log_view(struct tog_view *);
602 static const struct got_error *open_blame_view(struct tog_view *, char *,
603 struct got_object_id *, struct got_repository *);
604 static const struct got_error *show_blame_view(struct tog_view *);
605 static const struct got_error *input_blame_view(struct tog_view **,
606 struct tog_view *, int);
607 static const struct got_error *reset_blame_view(struct tog_view *);
608 static const struct got_error *close_blame_view(struct tog_view *);
609 static const struct got_error *search_start_blame_view(struct tog_view *);
610 static const struct got_error *search_next_blame_view(struct tog_view *);
612 static const struct got_error *open_tree_view(struct tog_view *,
613 struct got_object_id *, const char *, struct got_repository *);
614 static const struct got_error *show_tree_view(struct tog_view *);
615 static const struct got_error *input_tree_view(struct tog_view **,
616 struct tog_view *, int);
617 static const struct got_error *close_tree_view(struct tog_view *);
618 static const struct got_error *search_start_tree_view(struct tog_view *);
619 static const struct got_error *search_next_tree_view(struct tog_view *);
621 static const struct got_error *open_ref_view(struct tog_view *,
622 struct got_repository *);
623 static const struct got_error *show_ref_view(struct tog_view *);
624 static const struct got_error *input_ref_view(struct tog_view **,
625 struct tog_view *, int);
626 static const struct got_error *close_ref_view(struct tog_view *);
627 static const struct got_error *search_start_ref_view(struct tog_view *);
628 static const struct got_error *search_next_ref_view(struct tog_view *);
630 static volatile sig_atomic_t tog_sigwinch_received;
631 static volatile sig_atomic_t tog_sigpipe_received;
632 static volatile sig_atomic_t tog_sigcont_received;
633 static volatile sig_atomic_t tog_sigint_received;
634 static volatile sig_atomic_t tog_sigterm_received;
637 tog_sigwinch(int signo)
639 tog_sigwinch_received = 1;
643 tog_sigpipe(int signo)
645 tog_sigpipe_received = 1;
649 tog_sigcont(int signo)
651 tog_sigcont_received = 1;
655 tog_sigint(int signo)
657 tog_sigint_received = 1;
661 tog_sigterm(int signo)
663 tog_sigterm_received = 1;
667 tog_fatal_signal_received(void)
669 return (tog_sigpipe_received ||
670 tog_sigint_received || tog_sigint_received);
673 static const struct got_error *
674 view_close(struct tog_view *view)
676 const struct got_error *err = NULL, *child_err = NULL;
679 child_err = view_close(view->child);
683 err = view->close(view);
685 del_panel(view->panel);
687 delwin(view->window);
689 return err ? err : child_err;
692 static struct tog_view *
693 view_open(int nlines, int ncols, int begin_y, int begin_x,
694 enum tog_view_type type)
696 struct tog_view *view = calloc(1, sizeof(*view));
704 view->nlines = nlines ? nlines : LINES - begin_y;
705 view->ncols = ncols ? ncols : COLS - begin_x;
706 view->begin_y = begin_y;
707 view->begin_x = begin_x;
708 view->window = newwin(nlines, ncols, begin_y, begin_x);
709 if (view->window == NULL) {
713 view->panel = new_panel(view->window);
714 if (view->panel == NULL ||
715 set_panel_userptr(view->panel, view) != OK) {
720 keypad(view->window, TRUE);
725 view_split_begin_x(int begin_x)
727 if (begin_x > 0 || COLS < 120)
729 return (COLS - MAX(COLS / 2, 80));
732 /* XXX Stub till we decide what to do. */
734 view_split_begin_y(int lines)
736 return lines * HSPLIT_SCALE;
739 static const struct got_error *view_resize(struct tog_view *);
741 static const struct got_error *
742 view_splitscreen(struct tog_view *view)
744 const struct got_error *err = NULL;
746 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
747 if (view->resized_y && view->resized_y < view->lines)
748 view->begin_y = view->resized_y;
750 view->begin_y = view_split_begin_y(view->nlines);
752 } else if (!view->resized) {
753 if (view->resized_x && view->resized_x < view->cols - 1 &&
755 view->begin_x = view->resized_x;
757 view->begin_x = view_split_begin_x(0);
760 view->nlines = LINES - view->begin_y;
761 view->ncols = COLS - view->begin_x;
764 err = view_resize(view);
768 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
769 view->parent->nlines = view->begin_y;
771 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
772 return got_error_from_errno("mvwin");
777 static const struct got_error *
778 view_fullscreen(struct tog_view *view)
780 const struct got_error *err = NULL;
783 view->begin_y = view->resized ? view->begin_y : 0;
784 view->nlines = view->resized ? view->nlines : LINES;
788 err = view_resize(view);
792 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
793 return got_error_from_errno("mvwin");
799 view_is_parent_view(struct tog_view *view)
801 return view->parent == NULL;
805 view_is_splitscreen(struct tog_view *view)
807 return view->begin_x > 0 || view->begin_y > 0;
811 view_is_fullscreen(struct tog_view *view)
813 return view->nlines == LINES && view->ncols == COLS;
817 view_is_hsplit_top(struct tog_view *view)
819 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
820 view_is_splitscreen(view->child);
824 view_border(struct tog_view *view)
827 const struct tog_view *view_above;
830 return view_border(view->parent);
832 panel = panel_above(view->panel);
836 view_above = panel_userptr(panel);
837 if (view->mode == TOG_VIEW_SPLIT_HRZN)
838 mvwhline(view->window, view_above->begin_y - 1,
839 view->begin_x, got_locale_is_utf8() ?
840 ACS_HLINE : '-', view->ncols);
842 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
843 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
846 static const struct got_error *view_init_hsplit(struct tog_view *, int);
847 static const struct got_error *request_log_commits(struct tog_view *);
848 static const struct got_error *offset_selection_down(struct tog_view *);
849 static void offset_selection_up(struct tog_view *);
850 static void view_get_split(struct tog_view *, int *, int *);
852 static const struct got_error *
853 view_resize(struct tog_view *view)
855 const struct got_error *err = NULL;
856 int dif, nlines, ncols;
858 dif = LINES - view->lines; /* line difference */
860 if (view->lines > LINES)
861 nlines = view->nlines - (view->lines - LINES);
863 nlines = view->nlines + (LINES - view->lines);
864 if (view->cols > COLS)
865 ncols = view->ncols - (view->cols - COLS);
867 ncols = view->ncols + (COLS - view->cols);
870 int hs = view->child->begin_y;
872 if (!view_is_fullscreen(view))
873 view->child->begin_x = view_split_begin_x(view->begin_x);
874 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
875 view->child->begin_x == 0) {
878 view_fullscreen(view->child);
879 if (view->child->focussed)
880 show_panel(view->child->panel);
882 show_panel(view->panel);
884 ncols = view->child->begin_x;
886 view_splitscreen(view->child);
887 show_panel(view->child->panel);
890 * XXX This is ugly and needs to be moved into the above
891 * logic but "works" for now and my attempts at moving it
892 * break either 'tab' or 'F' key maps in horizontal splits.
895 err = view_splitscreen(view->child);
898 if (dif < 0) { /* top split decreased */
899 err = offset_selection_down(view);
906 show_panel(view->child->panel);
907 nlines = view->nlines;
909 } else if (view->parent == NULL)
912 if (view->resize && dif > 0) {
913 err = view->resize(view, dif);
918 if (wresize(view->window, nlines, ncols) == ERR)
919 return got_error_from_errno("wresize");
920 if (replace_panel(view->panel, view->window) == ERR)
921 return got_error_from_errno("replace_panel");
922 wclear(view->window);
924 view->nlines = nlines;
932 static const struct got_error *
933 resize_log_view(struct tog_view *view, int increase)
935 struct tog_log_view_state *s = &view->state.log;
936 const struct got_error *err = NULL;
939 if (s->selected_entry)
940 n = s->selected_entry->idx + view->lines - s->selected;
943 * Request commits to account for the increased
944 * height so we have enough to populate the view.
946 if (s->commits->ncommits < n) {
947 view->nscrolled = n - s->commits->ncommits + increase + 1;
948 err = request_log_commits(view);
955 view_adjust_offset(struct tog_view *view, int n)
960 if (view->parent && view->parent->offset) {
961 if (view->parent->offset + n >= 0)
962 view->parent->offset += n;
964 view->parent->offset = 0;
965 } else if (view->offset) {
966 if (view->offset - n >= 0)
973 static const struct got_error *
974 view_resize_split(struct tog_view *view, int resize)
976 const struct got_error *err = NULL;
977 struct tog_view *v = NULL;
984 if (!v->child || !view_is_splitscreen(v->child))
987 v->resized = v->child->resized = resize; /* lock for resize event */
989 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
990 if (v->child->resized_y)
991 v->child->begin_y = v->child->resized_y;
993 v->child->begin_y -= resize;
995 v->child->begin_y += resize;
996 if (v->child->begin_y < 3) {
998 v->child->begin_y = 3;
999 } else if (v->child->begin_y > LINES - 1) {
1001 v->child->begin_y = LINES - 1;
1004 v->child->ncols = COLS;
1005 view_adjust_offset(view, resize);
1006 err = view_init_hsplit(v, v->child->begin_y);
1009 v->child->resized_y = v->child->begin_y;
1011 if (v->child->resized_x)
1012 v->child->begin_x = v->child->resized_x;
1014 v->child->begin_x -= resize;
1016 v->child->begin_x += resize;
1017 if (v->child->begin_x < 11) {
1019 v->child->begin_x = 11;
1020 } else if (v->child->begin_x > COLS - 1) {
1022 v->child->begin_x = COLS - 1;
1024 v->child->resized_x = v->child->begin_x;
1027 v->child->mode = v->mode;
1028 v->child->nlines = v->lines - v->child->begin_y;
1029 v->child->ncols = v->cols - v->child->begin_x;
1032 err = view_fullscreen(v);
1035 err = view_splitscreen(v->child);
1039 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1040 err = offset_selection_down(v->child);
1046 err = v->resize(v, 0);
1047 else if (v->child->resize)
1048 err = v->child->resize(v->child, 0);
1050 v->resized = v->child->resized = 0;
1056 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1058 struct tog_view *v = src->child ? src->child : src;
1060 dst->resized_x = v->resized_x;
1061 dst->resized_y = v->resized_y;
1064 static const struct got_error *
1065 view_close_child(struct tog_view *view)
1067 const struct got_error *err = NULL;
1069 if (view->child == NULL)
1072 err = view_close(view->child);
1077 static const struct got_error *
1078 view_set_child(struct tog_view *view, struct tog_view *child)
1080 const struct got_error *err = NULL;
1082 view->child = child;
1083 child->parent = view;
1085 err = view_resize(view);
1089 if (view->child->resized_x || view->child->resized_y)
1090 err = view_resize_split(view, 0);
1095 static const struct got_error *view_dispatch_request(struct tog_view **,
1096 struct tog_view *, enum tog_view_type, int, int);
1098 static const struct got_error *
1099 view_request_new(struct tog_view **requested, struct tog_view *view,
1100 enum tog_view_type request)
1102 struct tog_view *new_view = NULL;
1103 const struct got_error *err;
1108 if (view_is_parent_view(view))
1109 view_get_split(view, &y, &x);
1111 err = view_dispatch_request(&new_view, view, request, y, x);
1115 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN) {
1116 err = view_init_hsplit(view, y);
1122 new_view->focussed = 1;
1123 new_view->mode = view->mode;
1124 new_view->nlines = view->lines - y;
1126 if (view_is_parent_view(view)) {
1127 view_transfer_size(new_view, view);
1128 err = view_close_child(view);
1131 err = view_set_child(view, new_view);
1134 view->focus_child = 1;
1136 *requested = new_view;
1142 tog_resizeterm(void)
1145 struct winsize size;
1147 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1148 cols = 80; /* Default */
1152 lines = size.ws_row;
1154 resize_term(lines, cols);
1157 static const struct got_error *
1158 view_search_start(struct tog_view *view)
1160 const struct got_error *err = NULL;
1161 struct tog_view *v = view;
1165 if (view->search_started) {
1166 regfree(&view->regex);
1167 view->searching = 0;
1168 memset(&view->regmatch, 0, sizeof(view->regmatch));
1170 view->search_started = 0;
1172 if (view->nlines < 1)
1175 if (view_is_hsplit_top(view))
1178 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1179 wclrtoeol(v->window);
1181 nodelay(view->window, FALSE); /* block for search term input */
1184 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1185 wrefresh(v->window);
1188 nodelay(view->window, TRUE);
1192 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1193 err = view->search_start(view);
1195 regfree(&view->regex);
1198 view->search_started = 1;
1199 view->searching = TOG_SEARCH_FORWARD;
1200 view->search_next_done = 0;
1201 view->search_next(view);
1207 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1208 static const struct got_error *
1209 switch_split(struct tog_view *view)
1211 const struct got_error *err = NULL;
1212 struct tog_view *v = NULL;
1219 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1220 v->mode = TOG_VIEW_SPLIT_VERT;
1222 v->mode = TOG_VIEW_SPLIT_HRZN;
1226 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1227 v->mode = TOG_VIEW_SPLIT_NONE;
1229 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1230 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1231 v->child->begin_y = v->child->resized_y;
1232 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1233 v->child->begin_x = v->child->resized_x;
1236 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1238 v->child->ncols = COLS;
1239 v->child->nscrolled = LINES - v->child->nlines;
1241 err = view_init_hsplit(v, v->child->begin_y);
1245 v->child->mode = v->mode;
1246 v->child->nlines = v->lines - v->child->begin_y;
1249 err = view_fullscreen(v);
1252 err = view_splitscreen(v->child);
1256 if (v->mode == TOG_VIEW_SPLIT_NONE)
1257 v->mode = TOG_VIEW_SPLIT_VERT;
1258 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1259 err = offset_selection_down(v);
1262 err = offset_selection_down(v->child);
1266 offset_selection_up(v);
1267 offset_selection_up(v->child);
1270 err = v->resize(v, 0);
1271 else if (v->child->resize)
1272 err = v->child->resize(v->child, 0);
1278 * Compute view->count from numeric input. Assign total to view->count and
1279 * return first non-numeric key entered.
1282 get_compound_key(struct tog_view *view, int c)
1284 struct tog_view *v = view;
1287 if (view_is_hsplit_top(view))
1289 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1293 cbreak(); /* block for input */
1294 nodelay(view->window, FALSE);
1295 wmove(v->window, v->nlines - 1, 0);
1296 wclrtoeol(v->window);
1297 waddch(v->window, ':');
1300 x = getcurx(v->window);
1301 if (x != ERR && x < view->ncols) {
1302 waddch(v->window, c);
1303 wrefresh(v->window);
1307 * Don't overflow. Max valid request should be the greatest
1308 * between the longest and total lines; cap at 10 million.
1313 n = n * 10 + (c - '0');
1314 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1316 if (c == 'G' || c == 'g') { /* nG key map */
1317 view->gline = view->hiline = n;
1322 /* Massage excessive or inapplicable values at the input handler. */
1328 static const struct got_error *
1329 view_input(struct tog_view **new, int *done, struct tog_view *view,
1330 struct tog_view_list_head *views)
1332 const struct got_error *err = NULL;
1338 /* Clear "no matches" indicator. */
1339 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1340 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1341 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1345 if (view->searching && !view->search_next_done) {
1346 errcode = pthread_mutex_unlock(&tog_mutex);
1348 return got_error_set_errno(errcode,
1349 "pthread_mutex_unlock");
1351 errcode = pthread_mutex_lock(&tog_mutex);
1353 return got_error_set_errno(errcode,
1354 "pthread_mutex_lock");
1355 view->search_next(view);
1359 /* Allow threads to make progress while we are waiting for input. */
1360 errcode = pthread_mutex_unlock(&tog_mutex);
1362 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1363 /* If we have an unfinished count, let C-g or backspace abort. */
1364 if (view->count && --view->count) {
1366 nodelay(view->window, TRUE);
1367 ch = wgetch(view->window);
1368 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1373 ch = wgetch(view->window);
1374 if (ch >= '1' && ch <= '9')
1375 view->ch = ch = get_compound_key(view, ch);
1377 if (view->hiline && ch != ERR && ch != 0)
1378 view->hiline = 0; /* key pressed, clear line highlight */
1379 nodelay(view->window, TRUE);
1380 errcode = pthread_mutex_lock(&tog_mutex);
1382 return got_error_set_errno(errcode, "pthread_mutex_lock");
1384 if (tog_sigwinch_received || tog_sigcont_received) {
1386 tog_sigwinch_received = 0;
1387 tog_sigcont_received = 0;
1388 TAILQ_FOREACH(v, views, entry) {
1389 err = view_resize(v);
1392 err = v->input(new, v, KEY_RESIZE);
1396 err = view_resize(v->child);
1399 err = v->child->input(new, v->child,
1403 if (v->child->resized_x || v->child->resized_y) {
1404 err = view_resize_split(v, 0);
1417 view->child->focussed = 1;
1418 view->focus_child = 1;
1419 } else if (view->parent) {
1421 view->parent->focussed = 1;
1422 view->parent->focus_child = 0;
1423 if (!view_is_splitscreen(view)) {
1424 if (view->parent->resize) {
1425 err = view->parent->resize(view->parent,
1430 offset_selection_up(view->parent);
1431 err = view_fullscreen(view->parent);
1438 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1439 if (view->parent->resize) {
1440 /* might need more commits to fill fullscreen */
1441 err = view->parent->resize(view->parent, 0);
1445 offset_selection_up(view->parent);
1447 err = view->input(new, view, ch);
1455 if (view_is_parent_view(view)) {
1456 if (view->child == NULL)
1458 if (view_is_splitscreen(view->child)) {
1460 view->child->focussed = 1;
1461 err = view_fullscreen(view->child);
1463 err = view_splitscreen(view->child);
1465 err = view_resize_split(view, 0);
1469 err = view->child->input(new, view->child,
1472 if (view_is_splitscreen(view)) {
1473 view->parent->focussed = 0;
1475 err = view_fullscreen(view);
1477 err = view_splitscreen(view);
1478 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1479 err = view_resize(view->parent);
1481 err = view_resize_split(view, 0);
1485 err = view->input(new, view, KEY_RESIZE);
1490 err = view->resize(view, 0);
1495 err = offset_selection_down(view->parent);
1497 err = offset_selection_down(view);
1501 err = switch_split(view);
1504 err = view_resize_split(view, -1);
1507 err = view_resize_split(view, 1);
1513 if (view->search_start)
1514 view_search_start(view);
1516 err = view->input(new, view, ch);
1520 if (view->search_started && view->search_next) {
1521 view->searching = (ch == 'n' ?
1522 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1523 view->search_next_done = 0;
1524 view->search_next(view);
1526 err = view->input(new, view, ch);
1529 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1530 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1532 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1533 TAILQ_FOREACH(v, views, entry) {
1539 if (v->child && v->child->reset) {
1540 err = v->child->reset(v->child);
1547 err = view->input(new, view, ch);
1555 view_needs_focus_indication(struct tog_view *view)
1557 if (view_is_parent_view(view)) {
1558 if (view->child == NULL || view->child->focussed)
1560 if (!view_is_splitscreen(view->child))
1562 } else if (!view_is_splitscreen(view))
1565 return view->focussed;
1568 static const struct got_error *
1569 view_loop(struct tog_view *view)
1571 const struct got_error *err = NULL;
1572 struct tog_view_list_head views;
1573 struct tog_view *new_view;
1575 int fast_refresh = 10;
1576 int done = 0, errcode;
1578 mode = getenv("TOG_VIEW_SPLIT_MODE");
1579 if (!mode || !(*mode == 'h' || *mode == 'H'))
1580 view->mode = TOG_VIEW_SPLIT_VERT;
1582 view->mode = TOG_VIEW_SPLIT_HRZN;
1584 errcode = pthread_mutex_lock(&tog_mutex);
1586 return got_error_set_errno(errcode, "pthread_mutex_lock");
1589 TAILQ_INSERT_HEAD(&views, view, entry);
1592 err = view->show(view);
1597 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1598 !tog_fatal_signal_received()) {
1599 /* Refresh fast during initialization, then become slower. */
1600 if (fast_refresh && fast_refresh-- == 0)
1601 halfdelay(10); /* switch to once per second */
1603 err = view_input(&new_view, &done, view, &views);
1607 struct tog_view *v, *prev = NULL;
1609 if (view_is_parent_view(view))
1610 prev = TAILQ_PREV(view, tog_view_list_head,
1612 else if (view->parent)
1613 prev = view->parent;
1616 view->parent->child = NULL;
1617 view->parent->focus_child = 0;
1618 /* Restore fullscreen line height. */
1619 view->parent->nlines = view->parent->lines;
1620 err = view_resize(view->parent);
1623 /* Make resized splits persist. */
1624 view_transfer_size(view->parent, view);
1626 TAILQ_REMOVE(&views, view, entry);
1628 err = view_close(view);
1633 TAILQ_FOREACH(v, &views, entry) {
1637 if (view == NULL && new_view == NULL) {
1638 /* No view has focus. Try to pick one. */
1641 else if (!TAILQ_EMPTY(&views)) {
1642 view = TAILQ_LAST(&views,
1643 tog_view_list_head);
1646 if (view->focus_child) {
1647 view->child->focussed = 1;
1655 struct tog_view *v, *t;
1656 /* Only allow one parent view per type. */
1657 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1658 if (v->type != new_view->type)
1660 TAILQ_REMOVE(&views, v, entry);
1661 err = view_close(v);
1666 TAILQ_INSERT_TAIL(&views, new_view, entry);
1670 if (view_is_parent_view(view)) {
1671 if (view->child && view->child->focussed)
1674 if (view->parent && view->parent->focussed)
1675 view = view->parent;
1677 show_panel(view->panel);
1678 if (view->child && view_is_splitscreen(view->child))
1679 show_panel(view->child->panel);
1680 if (view->parent && view_is_splitscreen(view)) {
1681 err = view->parent->show(view->parent);
1685 err = view->show(view);
1689 err = view->child->show(view->child);
1698 while (!TAILQ_EMPTY(&views)) {
1699 const struct got_error *close_err;
1700 view = TAILQ_FIRST(&views);
1701 TAILQ_REMOVE(&views, view, entry);
1702 close_err = view_close(view);
1703 if (close_err && err == NULL)
1707 errcode = pthread_mutex_unlock(&tog_mutex);
1708 if (errcode && err == NULL)
1709 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1719 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1724 /* Create newly allocated wide-character string equivalent to a byte string. */
1725 static const struct got_error *
1726 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1729 const struct got_error *err = NULL;
1732 *wlen = mbstowcs(NULL, s, 0);
1733 if (*wlen == (size_t)-1) {
1735 if (errno != EILSEQ)
1736 return got_error_from_errno("mbstowcs");
1738 /* byte string invalid in current encoding; try to "fix" it */
1739 err = got_mbsavis(&vis, &vislen, s);
1742 *wlen = mbstowcs(NULL, vis, 0);
1743 if (*wlen == (size_t)-1) {
1744 err = got_error_from_errno("mbstowcs"); /* give up */
1749 *ws = calloc(*wlen + 1, sizeof(**ws));
1751 err = got_error_from_errno("calloc");
1755 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1756 err = got_error_from_errno("mbstowcs");
1767 static const struct got_error *
1768 expand_tab(char **ptr, const char *src)
1771 size_t len, n, idx = 0, sz = 0;
1774 n = len = strlen(src);
1775 dst = malloc(n + 1);
1777 return got_error_from_errno("malloc");
1779 while (idx < len && src[idx]) {
1780 const char c = src[idx];
1783 size_t nb = TABSIZE - sz % TABSIZE;
1786 p = realloc(dst, n + nb);
1789 return got_error_from_errno("realloc");
1794 memset(dst + sz, ' ', nb);
1797 dst[sz++] = src[idx];
1807 * Advance at most n columns from wline starting at offset off.
1808 * Return the index to the first character after the span operation.
1809 * Return the combined column width of all spanned wide character in
1813 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1815 int width, i, cols = 0;
1822 for (i = off; wline[i] != L'\0'; ++i) {
1823 if (wline[i] == L'\t')
1824 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1826 width = wcwidth(wline[i]);
1833 if (cols + width > n)
1843 * Format a line for display, ensuring that it won't overflow a width limit.
1844 * With scrolling, the width returned refers to the scrolled version of the
1845 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1847 static const struct got_error *
1848 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1849 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1851 const struct got_error *err = NULL;
1853 wchar_t *wline = NULL;
1862 err = expand_tab(&exstr, line);
1867 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1872 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1874 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1875 wline[wlen - 1] = L'\0';
1878 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1879 wline[wlen - 1] = L'\0';
1883 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1889 *scrollxp = scrollx;
1897 static const struct got_error*
1898 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1899 struct got_object_id *id, struct got_repository *repo)
1901 static const struct got_error *err = NULL;
1902 struct got_reflist_entry *re;
1908 TAILQ_FOREACH(re, refs, entry) {
1909 struct got_tag_object *tag = NULL;
1910 struct got_object_id *ref_id;
1913 name = got_ref_get_name(re->ref);
1914 if (strcmp(name, GOT_REF_HEAD) == 0)
1916 if (strncmp(name, "refs/", 5) == 0)
1918 if (strncmp(name, "got/", 4) == 0 &&
1919 strncmp(name, "got/backup/", 11) != 0)
1921 if (strncmp(name, "heads/", 6) == 0)
1923 if (strncmp(name, "remotes/", 8) == 0) {
1925 s = strstr(name, "/" GOT_REF_HEAD);
1926 if (s != NULL && s[strlen(s)] == '\0')
1929 err = got_ref_resolve(&ref_id, repo, re->ref);
1932 if (strncmp(name, "tags/", 5) == 0) {
1933 err = got_object_open_as_tag(&tag, repo, ref_id);
1935 if (err->code != GOT_ERR_OBJ_TYPE) {
1939 /* Ref points at something other than a tag. */
1944 cmp = got_object_id_cmp(tag ?
1945 got_object_tag_get_object_id(tag) : ref_id, id);
1948 got_object_tag_close(tag);
1952 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1953 s ? ", " : "", name) == -1) {
1954 err = got_error_from_errno("asprintf");
1965 static const struct got_error *
1966 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1971 smallerthan = strchr(author, '<');
1972 if (smallerthan && smallerthan[1] != '\0')
1973 author = smallerthan + 1;
1974 author[strcspn(author, "@>")] = '\0';
1975 return format_line(wauthor, author_width, NULL, author, 0, limit,
1979 static const struct got_error *
1980 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1981 struct got_object_id *id, const size_t date_display_cols,
1982 int author_display_cols)
1984 struct tog_log_view_state *s = &view->state.log;
1985 const struct got_error *err = NULL;
1986 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1987 char *logmsg0 = NULL, *logmsg = NULL;
1988 char *author = NULL;
1989 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1990 int author_width, logmsg_width;
1991 char *newline, *line = NULL;
1992 int col, limit, scrollx;
1993 const int avail = view->ncols;
1995 time_t committer_time;
1996 struct tog_color *tc;
1998 committer_time = got_object_commit_get_committer_time(commit);
1999 if (gmtime_r(&committer_time, &tm) == NULL)
2000 return got_error_from_errno("gmtime_r");
2001 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2002 return got_error(GOT_ERR_NO_SPACE);
2004 if (avail <= date_display_cols)
2005 limit = MIN(sizeof(datebuf) - 1, avail);
2007 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2008 tc = get_color(&s->colors, TOG_COLOR_DATE);
2010 wattr_on(view->window,
2011 COLOR_PAIR(tc->colorpair), NULL);
2012 waddnstr(view->window, datebuf, limit);
2014 wattr_off(view->window,
2015 COLOR_PAIR(tc->colorpair), NULL);
2022 err = got_object_id_str(&id_str, id);
2025 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2027 wattr_on(view->window,
2028 COLOR_PAIR(tc->colorpair), NULL);
2029 wprintw(view->window, "%.8s ", id_str);
2031 wattr_off(view->window,
2032 COLOR_PAIR(tc->colorpair), NULL);
2039 if (s->use_committer)
2040 author = strdup(got_object_commit_get_committer(commit));
2042 author = strdup(got_object_commit_get_author(commit));
2043 if (author == NULL) {
2044 err = got_error_from_errno("strdup");
2047 err = format_author(&wauthor, &author_width, author, avail - col, col);
2050 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2052 wattr_on(view->window,
2053 COLOR_PAIR(tc->colorpair), NULL);
2054 waddwstr(view->window, wauthor);
2055 col += author_width;
2056 while (col < avail && author_width < author_display_cols + 2) {
2057 waddch(view->window, ' ');
2062 wattr_off(view->window,
2063 COLOR_PAIR(tc->colorpair), NULL);
2067 err = got_object_commit_get_logmsg(&logmsg0, commit);
2071 while (*logmsg == '\n')
2073 newline = strchr(logmsg, '\n');
2076 limit = avail - col;
2077 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2078 limit--; /* for the border */
2079 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2083 waddwstr(view->window, &wlogmsg[scrollx]);
2084 col += MAX(logmsg_width, 0);
2085 while (col < avail) {
2086 waddch(view->window, ' ');
2098 static struct commit_queue_entry *
2099 alloc_commit_queue_entry(struct got_commit_object *commit,
2100 struct got_object_id *id)
2102 struct commit_queue_entry *entry;
2103 struct got_object_id *dup;
2105 entry = calloc(1, sizeof(*entry));
2109 dup = got_object_id_dup(id);
2116 entry->commit = commit;
2121 pop_commit(struct commit_queue *commits)
2123 struct commit_queue_entry *entry;
2125 entry = TAILQ_FIRST(&commits->head);
2126 TAILQ_REMOVE(&commits->head, entry, entry);
2127 got_object_commit_close(entry->commit);
2128 commits->ncommits--;
2134 free_commits(struct commit_queue *commits)
2136 while (!TAILQ_EMPTY(&commits->head))
2137 pop_commit(commits);
2140 static const struct got_error *
2141 match_commit(int *have_match, struct got_object_id *id,
2142 struct got_commit_object *commit, regex_t *regex)
2144 const struct got_error *err = NULL;
2145 regmatch_t regmatch;
2146 char *id_str = NULL, *logmsg = NULL;
2150 err = got_object_id_str(&id_str, id);
2154 err = got_object_commit_get_logmsg(&logmsg, commit);
2158 if (regexec(regex, got_object_commit_get_author(commit), 1,
2159 ®match, 0) == 0 ||
2160 regexec(regex, got_object_commit_get_committer(commit), 1,
2161 ®match, 0) == 0 ||
2162 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2163 regexec(regex, logmsg, 1, ®match, 0) == 0)
2171 static const struct got_error *
2172 queue_commits(struct tog_log_thread_args *a)
2174 const struct got_error *err = NULL;
2177 * We keep all commits open throughout the lifetime of the log
2178 * view in order to avoid having to re-fetch commits from disk
2179 * while updating the display.
2182 struct got_object_id id;
2183 struct got_commit_object *commit;
2184 struct commit_queue_entry *entry;
2185 int limit_match = 0;
2188 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2193 err = got_object_open_as_commit(&commit, a->repo, &id);
2196 entry = alloc_commit_queue_entry(commit, &id);
2197 if (entry == NULL) {
2198 err = got_error_from_errno("alloc_commit_queue_entry");
2202 errcode = pthread_mutex_lock(&tog_mutex);
2204 err = got_error_set_errno(errcode,
2205 "pthread_mutex_lock");
2209 entry->idx = a->real_commits->ncommits;
2210 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2211 a->real_commits->ncommits++;
2214 err = match_commit(&limit_match, &id, commit,
2220 struct commit_queue_entry *matched;
2222 matched = alloc_commit_queue_entry(
2223 entry->commit, entry->id);
2224 if (matched == NULL) {
2225 err = got_error_from_errno(
2226 "alloc_commit_queue_entry");
2230 err = got_object_commit_dup(&matched->commit,
2235 matched->idx = a->limit_commits->ncommits;
2236 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2238 a->limit_commits->ncommits++;
2242 * This is how we signal log_thread() that we
2243 * have found a match, and that it should be
2244 * counted as a new entry for the view.
2246 a->limit_match = limit_match;
2249 if (*a->searching == TOG_SEARCH_FORWARD &&
2250 !*a->search_next_done) {
2252 err = match_commit(&have_match, &id, commit, a->regex);
2257 if (limit_match && have_match)
2258 *a->search_next_done =
2259 TOG_SEARCH_HAVE_MORE;
2260 } else if (have_match)
2261 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2264 errcode = pthread_mutex_unlock(&tog_mutex);
2265 if (errcode && err == NULL)
2266 err = got_error_set_errno(errcode,
2267 "pthread_mutex_unlock");
2270 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2276 select_commit(struct tog_log_view_state *s)
2278 struct commit_queue_entry *entry;
2281 entry = s->first_displayed_entry;
2283 if (ncommits == s->selected) {
2284 s->selected_entry = entry;
2287 entry = TAILQ_NEXT(entry, entry);
2292 static const struct got_error *
2293 draw_commits(struct tog_view *view)
2295 const struct got_error *err = NULL;
2296 struct tog_log_view_state *s = &view->state.log;
2297 struct commit_queue_entry *entry = s->selected_entry;
2298 int limit = view->nlines;
2300 int ncommits, author_cols = 4;
2301 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2302 char *refs_str = NULL;
2304 struct tog_color *tc;
2305 static const size_t date_display_cols = 12;
2307 if (view_is_hsplit_top(view))
2308 --limit; /* account for border */
2310 if (s->selected_entry &&
2311 !(view->searching && view->search_next_done == 0)) {
2312 struct got_reflist_head *refs;
2313 err = got_object_id_str(&id_str, s->selected_entry->id);
2316 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2317 s->selected_entry->id);
2319 err = build_refs_str(&refs_str, refs,
2320 s->selected_entry->id, s->repo);
2326 if (s->thread_args.commits_needed == 0)
2327 halfdelay(10); /* disable fast refresh */
2329 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2330 if (asprintf(&ncommits_str, " [%d/%d] %s",
2331 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2332 (view->searching && !view->search_next_done) ?
2333 "searching..." : "loading...") == -1) {
2334 err = got_error_from_errno("asprintf");
2338 const char *search_str = NULL;
2339 const char *limit_str = NULL;
2341 if (view->searching) {
2342 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2343 search_str = "no more matches";
2344 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2345 search_str = "no matches found";
2346 else if (!view->search_next_done)
2347 search_str = "searching...";
2350 if (s->limit_view && s->commits->ncommits == 0)
2351 limit_str = "no matches found";
2353 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2354 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2355 search_str ? search_str : (refs_str ? refs_str : ""),
2356 limit_str ? limit_str : "") == -1) {
2357 err = got_error_from_errno("asprintf");
2362 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2363 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2364 "........................................",
2365 s->in_repo_path, ncommits_str) == -1) {
2366 err = got_error_from_errno("asprintf");
2370 } else if (asprintf(&header, "commit %s%s",
2371 id_str ? id_str : "........................................",
2372 ncommits_str) == -1) {
2373 err = got_error_from_errno("asprintf");
2377 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2381 werase(view->window);
2383 if (view_needs_focus_indication(view))
2384 wstandout(view->window);
2385 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2387 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2388 waddwstr(view->window, wline);
2389 while (width < view->ncols) {
2390 waddch(view->window, ' ');
2394 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2395 if (view_needs_focus_indication(view))
2396 wstandend(view->window);
2401 /* Grow author column size if necessary, and set view->maxx. */
2402 entry = s->first_displayed_entry;
2406 struct got_commit_object *c = entry->commit;
2407 char *author, *eol, *msg, *msg0;
2408 wchar_t *wauthor, *wmsg;
2410 if (ncommits >= limit - 1)
2412 if (s->use_committer)
2413 author = strdup(got_object_commit_get_committer(c));
2415 author = strdup(got_object_commit_get_author(c));
2416 if (author == NULL) {
2417 err = got_error_from_errno("strdup");
2420 err = format_author(&wauthor, &width, author, COLS,
2422 if (author_cols < width)
2423 author_cols = width;
2428 err = got_object_commit_get_logmsg(&msg0, c);
2432 while (*msg == '\n')
2434 if ((eol = strchr(msg, '\n')))
2436 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2437 date_display_cols + author_cols, 0);
2440 view->maxx = MAX(view->maxx, width);
2444 entry = TAILQ_NEXT(entry, entry);
2447 entry = s->first_displayed_entry;
2448 s->last_displayed_entry = s->first_displayed_entry;
2451 if (ncommits >= limit - 1)
2453 if (ncommits == s->selected)
2454 wstandout(view->window);
2455 err = draw_commit(view, entry->commit, entry->id,
2456 date_display_cols, author_cols);
2457 if (ncommits == s->selected)
2458 wstandend(view->window);
2462 s->last_displayed_entry = entry;
2463 entry = TAILQ_NEXT(entry, entry);
2476 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2478 struct commit_queue_entry *entry;
2481 entry = TAILQ_FIRST(&s->commits->head);
2482 if (s->first_displayed_entry == entry)
2485 entry = s->first_displayed_entry;
2486 while (entry && nscrolled < maxscroll) {
2487 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2489 s->first_displayed_entry = entry;
2495 static const struct got_error *
2496 trigger_log_thread(struct tog_view *view, int wait)
2498 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2501 halfdelay(1); /* fast refresh while loading commits */
2503 while (!ta->log_complete && !tog_thread_error &&
2504 (ta->commits_needed > 0 || ta->load_all)) {
2505 /* Wake the log thread. */
2506 errcode = pthread_cond_signal(&ta->need_commits);
2508 return got_error_set_errno(errcode,
2509 "pthread_cond_signal");
2512 * The mutex will be released while the view loop waits
2513 * in wgetch(), at which time the log thread will run.
2518 /* Display progress update in log view. */
2519 show_log_view(view);
2523 /* Wait right here while next commit is being loaded. */
2524 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2526 return got_error_set_errno(errcode,
2527 "pthread_cond_wait");
2529 /* Display progress update in log view. */
2530 show_log_view(view);
2538 static const struct got_error *
2539 request_log_commits(struct tog_view *view)
2541 struct tog_log_view_state *state = &view->state.log;
2542 const struct got_error *err = NULL;
2544 if (state->thread_args.log_complete)
2547 state->thread_args.commits_needed += view->nscrolled;
2548 err = trigger_log_thread(view, 1);
2549 view->nscrolled = 0;
2554 static const struct got_error *
2555 log_scroll_down(struct tog_view *view, int maxscroll)
2557 struct tog_log_view_state *s = &view->state.log;
2558 const struct got_error *err = NULL;
2559 struct commit_queue_entry *pentry;
2560 int nscrolled = 0, ncommits_needed;
2562 if (s->last_displayed_entry == NULL)
2565 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2566 if (s->commits->ncommits < ncommits_needed &&
2567 !s->thread_args.log_complete) {
2569 * Ask the log thread for required amount of commits.
2571 s->thread_args.commits_needed +=
2572 ncommits_needed - s->commits->ncommits;
2573 err = trigger_log_thread(view, 1);
2579 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2580 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2583 s->last_displayed_entry = pentry ?
2584 pentry : s->last_displayed_entry;;
2586 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2589 s->first_displayed_entry = pentry;
2590 } while (++nscrolled < maxscroll);
2592 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2593 view->nscrolled += nscrolled;
2595 view->nscrolled = 0;
2600 static const struct got_error *
2601 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2602 struct got_commit_object *commit, struct got_object_id *commit_id,
2603 struct tog_view *log_view, struct got_repository *repo)
2605 const struct got_error *err;
2606 struct got_object_qid *parent_id;
2607 struct tog_view *diff_view;
2609 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2610 if (diff_view == NULL)
2611 return got_error_from_errno("view_open");
2613 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2614 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2615 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2617 *new_view = diff_view;
2621 static const struct got_error *
2622 tree_view_visit_subtree(struct tog_tree_view_state *s,
2623 struct got_tree_object *subtree)
2625 struct tog_parent_tree *parent;
2627 parent = calloc(1, sizeof(*parent));
2629 return got_error_from_errno("calloc");
2631 parent->tree = s->tree;
2632 parent->first_displayed_entry = s->first_displayed_entry;
2633 parent->selected_entry = s->selected_entry;
2634 parent->selected = s->selected;
2635 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2638 s->first_displayed_entry = NULL;
2642 static const struct got_error *
2643 tree_view_walk_path(struct tog_tree_view_state *s,
2644 struct got_commit_object *commit, const char *path)
2646 const struct got_error *err = NULL;
2647 struct got_tree_object *tree = NULL;
2649 char *slash, *subpath = NULL;
2651 /* Walk the path and open corresponding tree objects. */
2654 struct got_tree_entry *te;
2655 struct got_object_id *tree_id;
2661 /* Ensure the correct subtree entry is selected. */
2662 slash = strchr(p, '/');
2664 te_name = strdup(p);
2666 te_name = strndup(p, slash - p);
2667 if (te_name == NULL) {
2668 err = got_error_from_errno("strndup");
2671 te = got_object_tree_find_entry(s->tree, te_name);
2673 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2678 s->first_displayed_entry = s->selected_entry = te;
2680 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2681 break; /* jump to this file's entry */
2683 slash = strchr(p, '/');
2685 subpath = strndup(path, slash - path);
2687 subpath = strdup(path);
2688 if (subpath == NULL) {
2689 err = got_error_from_errno("strdup");
2693 err = got_object_id_by_path(&tree_id, s->repo, commit,
2698 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2703 err = tree_view_visit_subtree(s, tree);
2705 got_object_tree_close(tree);
2719 static const struct got_error *
2720 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2721 struct commit_queue_entry *entry, const char *path,
2722 const char *head_ref_name, struct got_repository *repo)
2724 const struct got_error *err = NULL;
2725 struct tog_tree_view_state *s;
2726 struct tog_view *tree_view;
2728 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2729 if (tree_view == NULL)
2730 return got_error_from_errno("view_open");
2732 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2735 s = &tree_view->state.tree;
2737 *new_view = tree_view;
2739 if (got_path_is_root_dir(path))
2742 return tree_view_walk_path(s, entry->commit, path);
2745 static const struct got_error *
2746 block_signals_used_by_main_thread(void)
2751 if (sigemptyset(&sigset) == -1)
2752 return got_error_from_errno("sigemptyset");
2754 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2755 if (sigaddset(&sigset, SIGWINCH) == -1)
2756 return got_error_from_errno("sigaddset");
2757 if (sigaddset(&sigset, SIGCONT) == -1)
2758 return got_error_from_errno("sigaddset");
2759 if (sigaddset(&sigset, SIGINT) == -1)
2760 return got_error_from_errno("sigaddset");
2761 if (sigaddset(&sigset, SIGTERM) == -1)
2762 return got_error_from_errno("sigaddset");
2764 /* ncurses handles SIGTSTP */
2765 if (sigaddset(&sigset, SIGTSTP) == -1)
2766 return got_error_from_errno("sigaddset");
2768 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2770 return got_error_set_errno(errcode, "pthread_sigmask");
2776 log_thread(void *arg)
2778 const struct got_error *err = NULL;
2780 struct tog_log_thread_args *a = arg;
2784 * Sync startup with main thread such that we begin our
2785 * work once view_input() has released the mutex.
2787 errcode = pthread_mutex_lock(&tog_mutex);
2789 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2793 err = block_signals_used_by_main_thread();
2795 pthread_mutex_unlock(&tog_mutex);
2799 while (!done && !err && !tog_fatal_signal_received()) {
2800 errcode = pthread_mutex_unlock(&tog_mutex);
2802 err = got_error_set_errno(errcode,
2803 "pthread_mutex_unlock");
2806 err = queue_commits(a);
2808 if (err->code != GOT_ERR_ITER_COMPLETED)
2812 } else if (a->commits_needed > 0 && !a->load_all) {
2815 a->commits_needed--;
2817 a->commits_needed--;
2820 errcode = pthread_mutex_lock(&tog_mutex);
2822 err = got_error_set_errno(errcode,
2823 "pthread_mutex_lock");
2825 } else if (*a->quit)
2827 else if (*a->limiting && *a->first_displayed_entry == NULL) {
2828 *a->first_displayed_entry =
2829 TAILQ_FIRST(&a->limit_commits->head);
2830 *a->selected_entry = *a->first_displayed_entry;
2831 } else if (*a->first_displayed_entry == NULL) {
2832 *a->first_displayed_entry =
2833 TAILQ_FIRST(&a->real_commits->head);
2834 *a->selected_entry = *a->first_displayed_entry;
2837 errcode = pthread_cond_signal(&a->commit_loaded);
2839 err = got_error_set_errno(errcode,
2840 "pthread_cond_signal");
2841 pthread_mutex_unlock(&tog_mutex);
2846 a->commits_needed = 0;
2848 if (a->commits_needed == 0 && !a->load_all) {
2849 errcode = pthread_cond_wait(&a->need_commits,
2852 err = got_error_set_errno(errcode,
2853 "pthread_cond_wait");
2854 pthread_mutex_unlock(&tog_mutex);
2862 a->log_complete = 1;
2863 errcode = pthread_mutex_unlock(&tog_mutex);
2865 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2868 tog_thread_error = 1;
2869 pthread_cond_signal(&a->commit_loaded);
2874 static const struct got_error *
2875 stop_log_thread(struct tog_log_view_state *s)
2877 const struct got_error *err = NULL, *thread_err = NULL;
2882 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2884 return got_error_set_errno(errcode,
2885 "pthread_cond_signal");
2886 errcode = pthread_mutex_unlock(&tog_mutex);
2888 return got_error_set_errno(errcode,
2889 "pthread_mutex_unlock");
2890 errcode = pthread_join(s->thread, (void **)&thread_err);
2892 return got_error_set_errno(errcode, "pthread_join");
2893 errcode = pthread_mutex_lock(&tog_mutex);
2895 return got_error_set_errno(errcode,
2896 "pthread_mutex_lock");
2900 if (s->thread_args.repo) {
2901 err = got_repo_close(s->thread_args.repo);
2902 s->thread_args.repo = NULL;
2905 if (s->thread_args.pack_fds) {
2906 const struct got_error *pack_err =
2907 got_repo_pack_fds_close(s->thread_args.pack_fds);
2910 s->thread_args.pack_fds = NULL;
2913 if (s->thread_args.graph) {
2914 got_commit_graph_close(s->thread_args.graph);
2915 s->thread_args.graph = NULL;
2918 return err ? err : thread_err;
2921 static const struct got_error *
2922 close_log_view(struct tog_view *view)
2924 const struct got_error *err = NULL;
2925 struct tog_log_view_state *s = &view->state.log;
2928 err = stop_log_thread(s);
2930 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2931 if (errcode && err == NULL)
2932 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2934 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2935 if (errcode && err == NULL)
2936 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2938 free_commits(&s->limit_commits);
2939 free_commits(&s->real_commits);
2940 free(s->in_repo_path);
2941 s->in_repo_path = NULL;
2944 free(s->head_ref_name);
2945 s->head_ref_name = NULL;
2950 * We use two queues to implement the limit feature: first consists of
2951 * commits matching the current limit_regex; second is the real queue
2952 * of all known commits (real_commits). When the user starts limiting,
2953 * we swap queues such that all movement and displaying functionality
2954 * works with very slight change.
2956 static const struct got_error *
2957 limit_log_view(struct tog_view *view)
2959 struct tog_log_view_state *s = &view->state.log;
2960 struct commit_queue_entry *entry;
2961 struct tog_view *v = view;
2962 const struct got_error *err = NULL;
2966 if (view_is_hsplit_top(view))
2968 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
2971 /* Get the pattern */
2972 wmove(v->window, v->nlines - 1, 0);
2973 wclrtoeol(v->window);
2974 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
2975 nodelay(v->window, FALSE);
2978 ret = wgetnstr(v->window, pattern, sizeof(pattern));
2981 nodelay(v->window, TRUE);
2985 if (*pattern == '\0') {
2987 * Safety measure for the situation where the user
2988 * resets limit without previously limiting anything.
2994 * User could have pressed Ctrl+L, which refreshed the
2995 * commit queues, it means we can't save previously
2996 * (before limit took place) displayed entries,
2997 * because they would point to already free'ed memory,
2998 * so we are forced to always select first entry of
3001 s->commits = &s->real_commits;
3002 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3003 s->selected_entry = s->first_displayed_entry;
3010 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3015 /* Clear the screen while loading limit view */
3016 s->first_displayed_entry = NULL;
3017 s->last_displayed_entry = NULL;
3018 s->selected_entry = NULL;
3019 s->commits = &s->limit_commits;
3021 /* Prepare limit queue for new search */
3022 free_commits(&s->limit_commits);
3023 s->limit_commits.ncommits = 0;
3025 /* First process commits, which are in queue already */
3026 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3029 err = match_commit(&have_match, entry->id,
3030 entry->commit, &s->limit_regex);
3035 struct commit_queue_entry *matched;
3037 matched = alloc_commit_queue_entry(entry->commit,
3039 if (matched == NULL) {
3040 err = got_error_from_errno(
3041 "alloc_commit_queue_entry");
3045 err = got_object_commit_dup(&matched->commit,
3052 matched->idx = s->limit_commits.ncommits;
3053 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3055 s->limit_commits.ncommits++;
3059 /* Second process all the commits, until we fill the screen */
3060 if (s->limit_commits.ncommits < view->nlines - 1 &&
3061 !s->thread_args.log_complete) {
3062 s->thread_args.commits_needed +=
3063 view->nlines - s->limit_commits.ncommits - 1;
3064 err = trigger_log_thread(view, 1);
3069 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3070 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3076 static const struct got_error *
3077 search_start_log_view(struct tog_view *view)
3079 struct tog_log_view_state *s = &view->state.log;
3081 s->matched_entry = NULL;
3082 s->search_entry = NULL;
3086 static const struct got_error *
3087 search_next_log_view(struct tog_view *view)
3089 const struct got_error *err = NULL;
3090 struct tog_log_view_state *s = &view->state.log;
3091 struct commit_queue_entry *entry;
3093 /* Display progress update in log view. */
3094 show_log_view(view);
3098 if (s->search_entry) {
3100 errcode = pthread_mutex_unlock(&tog_mutex);
3102 return got_error_set_errno(errcode,
3103 "pthread_mutex_unlock");
3104 ch = wgetch(view->window);
3105 errcode = pthread_mutex_lock(&tog_mutex);
3107 return got_error_set_errno(errcode,
3108 "pthread_mutex_lock");
3109 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3110 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3113 if (view->searching == TOG_SEARCH_FORWARD)
3114 entry = TAILQ_NEXT(s->search_entry, entry);
3116 entry = TAILQ_PREV(s->search_entry,
3117 commit_queue_head, entry);
3118 } else if (s->matched_entry) {
3119 int matched_idx = s->matched_entry->idx;
3120 int selected_idx = s->selected_entry->idx;
3123 * If the user has moved the cursor after we hit a match,
3124 * the position from where we should continue searching
3125 * might have changed.
3127 if (view->searching == TOG_SEARCH_FORWARD) {
3128 if (matched_idx > selected_idx)
3129 entry = TAILQ_NEXT(s->selected_entry, entry);
3131 entry = TAILQ_NEXT(s->matched_entry, entry);
3133 if (matched_idx < selected_idx)
3134 entry = TAILQ_PREV(s->selected_entry,
3135 commit_queue_head, entry);
3137 entry = TAILQ_PREV(s->matched_entry,
3138 commit_queue_head, entry);
3141 entry = s->selected_entry;
3147 if (entry == NULL) {
3148 if (s->thread_args.log_complete ||
3149 view->searching == TOG_SEARCH_BACKWARD) {
3150 view->search_next_done =
3151 (s->matched_entry == NULL ?
3152 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3153 s->search_entry = NULL;
3157 * Poke the log thread for more commits and return,
3158 * allowing the main loop to make progress. Search
3159 * will resume at s->search_entry once we come back.
3161 s->thread_args.commits_needed++;
3162 return trigger_log_thread(view, 0);
3165 err = match_commit(&have_match, entry->id, entry->commit,
3170 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3171 s->matched_entry = entry;
3175 s->search_entry = entry;
3176 if (view->searching == TOG_SEARCH_FORWARD)
3177 entry = TAILQ_NEXT(entry, entry);
3179 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3182 if (s->matched_entry) {
3183 int cur = s->selected_entry->idx;
3184 while (cur < s->matched_entry->idx) {
3185 err = input_log_view(NULL, view, KEY_DOWN);
3190 while (cur > s->matched_entry->idx) {
3191 err = input_log_view(NULL, view, KEY_UP);
3198 s->search_entry = NULL;
3203 static const struct got_error *
3204 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3205 struct got_repository *repo, const char *head_ref_name,
3206 const char *in_repo_path, int log_branches)
3208 const struct got_error *err = NULL;
3209 struct tog_log_view_state *s = &view->state.log;
3210 struct got_repository *thread_repo = NULL;
3211 struct got_commit_graph *thread_graph = NULL;
3214 if (in_repo_path != s->in_repo_path) {
3215 free(s->in_repo_path);
3216 s->in_repo_path = strdup(in_repo_path);
3217 if (s->in_repo_path == NULL)
3218 return got_error_from_errno("strdup");
3221 /* The commit queue only contains commits being displayed. */
3222 TAILQ_INIT(&s->real_commits.head);
3223 s->real_commits.ncommits = 0;
3224 s->commits = &s->real_commits;
3226 TAILQ_INIT(&s->limit_commits.head);
3228 s->limit_commits.ncommits = 0;
3231 if (head_ref_name) {
3232 s->head_ref_name = strdup(head_ref_name);
3233 if (s->head_ref_name == NULL) {
3234 err = got_error_from_errno("strdup");
3238 s->start_id = got_object_id_dup(start_id);
3239 if (s->start_id == NULL) {
3240 err = got_error_from_errno("got_object_id_dup");
3243 s->log_branches = log_branches;
3245 STAILQ_INIT(&s->colors);
3246 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3247 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3248 get_color_value("TOG_COLOR_COMMIT"));
3251 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3252 get_color_value("TOG_COLOR_AUTHOR"));
3254 free_colors(&s->colors);
3257 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3258 get_color_value("TOG_COLOR_DATE"));
3260 free_colors(&s->colors);
3265 view->show = show_log_view;
3266 view->input = input_log_view;
3267 view->resize = resize_log_view;
3268 view->close = close_log_view;
3269 view->search_start = search_start_log_view;
3270 view->search_next = search_next_log_view;
3272 if (s->thread_args.pack_fds == NULL) {
3273 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3277 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3278 s->thread_args.pack_fds);
3281 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3285 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3286 s->repo, NULL, NULL);
3290 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3292 err = got_error_set_errno(errcode, "pthread_cond_init");
3295 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3297 err = got_error_set_errno(errcode, "pthread_cond_init");
3301 s->thread_args.commits_needed = view->nlines;
3302 s->thread_args.graph = thread_graph;
3303 s->thread_args.real_commits = &s->real_commits;
3304 s->thread_args.limit_commits = &s->limit_commits;
3305 s->thread_args.in_repo_path = s->in_repo_path;
3306 s->thread_args.start_id = s->start_id;
3307 s->thread_args.repo = thread_repo;
3308 s->thread_args.log_complete = 0;
3309 s->thread_args.quit = &s->quit;
3310 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3311 s->thread_args.selected_entry = &s->selected_entry;
3312 s->thread_args.searching = &view->searching;
3313 s->thread_args.search_next_done = &view->search_next_done;
3314 s->thread_args.regex = &view->regex;
3315 s->thread_args.limiting = &s->limit_view;
3316 s->thread_args.limit_regex = &s->limit_regex;
3317 s->thread_args.limit_commits = &s->limit_commits;
3320 close_log_view(view);
3324 static const struct got_error *
3325 show_log_view(struct tog_view *view)
3327 const struct got_error *err;
3328 struct tog_log_view_state *s = &view->state.log;
3330 if (s->thread == NULL) {
3331 int errcode = pthread_create(&s->thread, NULL, log_thread,
3334 return got_error_set_errno(errcode, "pthread_create");
3335 if (s->thread_args.commits_needed > 0) {
3336 err = trigger_log_thread(view, 1);
3342 return draw_commits(view);
3346 log_move_cursor_up(struct tog_view *view, int page, int home)
3348 struct tog_log_view_state *s = &view->state.log;
3350 if (s->first_displayed_entry == NULL)
3352 if (s->selected_entry->idx == 0)
3355 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3357 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3359 if (!page && !home && s->selected > 0)
3362 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3368 static const struct got_error *
3369 log_move_cursor_down(struct tog_view *view, int page)
3371 struct tog_log_view_state *s = &view->state.log;
3372 const struct got_error *err = NULL;
3373 int eos = view->nlines - 2;
3375 if (s->first_displayed_entry == NULL)
3378 if (s->thread_args.log_complete &&
3379 s->selected_entry->idx >= s->commits->ncommits - 1)
3382 if (view_is_hsplit_top(view))
3383 --eos; /* border consumes the last line */
3386 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3389 err = log_scroll_down(view, 1);
3390 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3391 struct commit_queue_entry *entry;
3395 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3396 s->last_displayed_entry = entry;
3397 for (n = 0; n <= eos; n++) {
3400 s->first_displayed_entry = entry;
3401 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3404 s->selected = n - 1;
3406 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3407 s->thread_args.log_complete)
3408 s->selected += MIN(page,
3409 s->commits->ncommits - s->selected_entry->idx - 1);
3411 err = log_scroll_down(view, page);
3417 * We might necessarily overshoot in horizontal
3418 * splits; if so, select the last displayed commit.
3420 if (s->first_displayed_entry && s->last_displayed_entry) {
3421 s->selected = MIN(s->selected,
3422 s->last_displayed_entry->idx -
3423 s->first_displayed_entry->idx);
3428 if (s->thread_args.log_complete &&
3429 s->selected_entry->idx == s->commits->ncommits - 1)
3436 view_get_split(struct tog_view *view, int *y, int *x)
3441 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3442 if (view->child && view->child->resized_y)
3443 *y = view->child->resized_y;
3444 else if (view->resized_y)
3445 *y = view->resized_y;
3447 *y = view_split_begin_y(view->lines);
3448 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3449 if (view->child && view->child->resized_x)
3450 *x = view->child->resized_x;
3451 else if (view->resized_x)
3452 *x = view->resized_x;
3454 *x = view_split_begin_x(view->begin_x);
3458 /* Split view horizontally at y and offset view->state->selected line. */
3459 static const struct got_error *
3460 view_init_hsplit(struct tog_view *view, int y)
3462 const struct got_error *err = NULL;
3466 err = view_resize(view);
3470 err = offset_selection_down(view);
3475 static const struct got_error *
3476 log_goto_line(struct tog_view *view, int nlines)
3478 const struct got_error *err = NULL;
3479 struct tog_log_view_state *s = &view->state.log;
3480 int g, idx = s->selected_entry->idx;
3482 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3488 if (g >= s->first_displayed_entry->idx + 1 &&
3489 g <= s->last_displayed_entry->idx + 1 &&
3490 g - s->first_displayed_entry->idx - 1 < nlines) {
3491 s->selected = g - s->first_displayed_entry->idx - 1;
3497 err = log_move_cursor_down(view, g - idx - 1);
3498 if (!err && g > s->selected_entry->idx + 1)
3499 err = log_move_cursor_down(view,
3500 g - s->first_displayed_entry->idx - 1);
3503 } else if (idx + 1 > g)
3504 log_move_cursor_up(view, idx - g + 1, 0);
3506 if (g < nlines && s->first_displayed_entry->idx == 0)
3507 s->selected = g - 1;
3514 static const struct got_error *
3515 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3517 const struct got_error *err = NULL;
3518 struct tog_log_view_state *s = &view->state.log;
3521 if (s->thread_args.load_all) {
3522 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3523 s->thread_args.load_all = 0;
3524 else if (s->thread_args.log_complete) {
3525 err = log_move_cursor_down(view, s->commits->ncommits);
3526 s->thread_args.load_all = 0;
3532 eos = nscroll = view->nlines - 1;
3533 if (view_is_hsplit_top(view))
3537 return log_goto_line(view, eos);
3541 err = limit_log_view(view);
3550 view->x = MAX(view->maxx - view->ncols / 2, 0);
3555 if (view->x + view->ncols / 2 < view->maxx)
3556 view->x += 2; /* move two columns right */
3562 view->x -= MIN(view->x, 2); /* move two columns back */
3571 log_move_cursor_up(view, 0, 0);
3575 log_move_cursor_up(view, 0, 1);
3585 log_move_cursor_up(view, nscroll, 0);
3592 err = log_move_cursor_down(view, 0);
3595 s->use_committer = !s->use_committer;
3599 /* We don't know yet how many commits, so we're forced to
3600 * traverse them all. */
3602 s->thread_args.load_all = 1;
3603 if (!s->thread_args.log_complete)
3604 return trigger_log_thread(view, 0);
3605 err = log_move_cursor_down(view, s->commits->ncommits);
3606 s->thread_args.load_all = 0;
3617 err = log_move_cursor_down(view, nscroll);
3620 if (s->selected > view->nlines - 2)
3621 s->selected = view->nlines - 2;
3622 if (s->selected > s->commits->ncommits - 1)
3623 s->selected = s->commits->ncommits - 1;
3625 if (s->commits->ncommits < view->nlines - 1 &&
3626 !s->thread_args.log_complete) {
3627 s->thread_args.commits_needed += (view->nlines - 1) -
3628 s->commits->ncommits;
3629 err = trigger_log_thread(view, 1);
3635 if (s->selected_entry == NULL)
3637 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3641 if (s->selected_entry == NULL)
3643 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3649 if (ch == KEY_BACKSPACE &&
3650 got_path_is_root_dir(s->in_repo_path))
3652 err = stop_log_thread(s);
3655 if (ch == KEY_BACKSPACE) {
3657 err = got_path_dirname(&parent_path, s->in_repo_path);
3660 free(s->in_repo_path);
3661 s->in_repo_path = parent_path;
3662 s->thread_args.in_repo_path = s->in_repo_path;
3663 } else if (ch == CTRL('l')) {
3664 struct got_object_id *start_id;
3665 err = got_repo_match_object_id(&start_id, NULL,
3666 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3667 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3671 s->start_id = start_id;
3672 s->thread_args.start_id = s->start_id;
3674 s->log_branches = !s->log_branches;
3676 if (s->thread_args.pack_fds == NULL) {
3677 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3681 err = got_repo_open(&s->thread_args.repo,
3682 got_repo_get_path(s->repo), NULL,
3683 s->thread_args.pack_fds);
3687 err = tog_load_refs(s->repo, 0);
3690 err = got_commit_graph_open(&s->thread_args.graph,
3691 s->in_repo_path, !s->log_branches);
3694 err = got_commit_graph_iter_start(s->thread_args.graph,
3695 s->start_id, s->repo, NULL, NULL);
3698 free_commits(&s->real_commits);
3699 free_commits(&s->limit_commits);
3700 s->first_displayed_entry = NULL;
3701 s->last_displayed_entry = NULL;
3702 s->selected_entry = NULL;
3704 s->thread_args.log_complete = 0;
3706 s->thread_args.commits_needed = view->lines;
3707 s->matched_entry = NULL;
3708 s->search_entry = NULL;
3713 err = view_request_new(new_view, view, TOG_VIEW_REF);
3723 static const struct got_error *
3724 apply_unveil(const char *repo_path, const char *worktree_path)
3726 const struct got_error *error;
3729 if (unveil("gmon.out", "rwc") != 0)
3730 return got_error_from_errno2("unveil", "gmon.out");
3732 if (repo_path && unveil(repo_path, "r") != 0)
3733 return got_error_from_errno2("unveil", repo_path);
3735 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3736 return got_error_from_errno2("unveil", worktree_path);
3738 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3739 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3741 error = got_privsep_unveil_exec_helpers();
3745 if (unveil(NULL, NULL) != 0)
3746 return got_error_from_errno("unveil");
3755 * Override default signal handlers before starting ncurses.
3756 * This should prevent ncurses from installing its own
3757 * broken cleanup() signal handler.
3759 signal(SIGWINCH, tog_sigwinch);
3760 signal(SIGPIPE, tog_sigpipe);
3761 signal(SIGCONT, tog_sigcont);
3762 signal(SIGINT, tog_sigint);
3763 signal(SIGTERM, tog_sigterm);
3767 halfdelay(1); /* Do fast refresh while initial view is loading. */
3770 intrflush(stdscr, FALSE);
3771 keypad(stdscr, TRUE);
3773 if (getenv("TOG_COLORS") != NULL) {
3775 use_default_colors();
3779 static const struct got_error *
3780 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3781 struct got_repository *repo, struct got_worktree *worktree)
3783 const struct got_error *err = NULL;
3786 *in_repo_path = strdup("/");
3787 if (*in_repo_path == NULL)
3788 return got_error_from_errno("strdup");
3793 const char *prefix = got_worktree_get_path_prefix(worktree);
3796 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3799 if (asprintf(in_repo_path, "%s%s%s", prefix,
3800 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3802 err = got_error_from_errno("asprintf");
3803 *in_repo_path = NULL;
3807 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3812 static const struct got_error *
3813 cmd_log(int argc, char *argv[])
3815 const struct got_error *error;
3816 struct got_repository *repo = NULL;
3817 struct got_worktree *worktree = NULL;
3818 struct got_object_id *start_id = NULL;
3819 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3820 char *start_commit = NULL, *label = NULL;
3821 struct got_reference *ref = NULL;
3822 const char *head_ref_name = NULL;
3823 int ch, log_branches = 0;
3824 struct tog_view *view;
3825 int *pack_fds = NULL;
3827 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3833 start_commit = optarg;
3836 repo_path = realpath(optarg, NULL);
3837 if (repo_path == NULL)
3838 return got_error_from_errno2("realpath",
3853 error = got_repo_pack_fds_open(&pack_fds);
3857 if (repo_path == NULL) {
3858 cwd = getcwd(NULL, 0);
3860 return got_error_from_errno("getcwd");
3861 error = got_worktree_open(&worktree, cwd);
3862 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3866 strdup(got_worktree_get_repo_path(worktree));
3868 repo_path = strdup(cwd);
3869 if (repo_path == NULL) {
3870 error = got_error_from_errno("strdup");
3875 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3879 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3886 error = apply_unveil(got_repo_get_path(repo),
3887 worktree ? got_worktree_get_root_path(worktree) : NULL);
3891 /* already loaded by tog_log_with_path()? */
3892 if (TAILQ_EMPTY(&tog_refs)) {
3893 error = tog_load_refs(repo, 0);
3898 if (start_commit == NULL) {
3899 error = got_repo_match_object_id(&start_id, &label,
3900 worktree ? got_worktree_get_head_ref_name(worktree) :
3901 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3904 head_ref_name = label;
3906 error = got_ref_open(&ref, repo, start_commit, 0);
3908 head_ref_name = got_ref_get_name(ref);
3909 else if (error->code != GOT_ERR_NOT_REF)
3911 error = got_repo_match_object_id(&start_id, NULL,
3912 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3917 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3919 error = got_error_from_errno("view_open");
3922 error = open_log_view(view, start_id, repo, head_ref_name,
3923 in_repo_path, log_branches);
3927 /* Release work tree lock. */
3928 got_worktree_close(worktree);
3931 error = view_loop(view);
3941 const struct got_error *close_err = got_repo_close(repo);
3946 got_worktree_close(worktree);
3948 const struct got_error *pack_err =
3949 got_repo_pack_fds_close(pack_fds);
3961 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
3962 "object1 object2\n", getprogname());
3967 match_line(const char *line, regex_t *regex, size_t nmatch,
3968 regmatch_t *regmatch)
3970 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3973 static struct tog_color *
3974 match_color(struct tog_colors *colors, const char *line)
3976 struct tog_color *tc = NULL;
3978 STAILQ_FOREACH(tc, colors, entry) {
3979 if (match_line(line, &tc->regex, 0, NULL))
3986 static const struct got_error *
3987 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3988 WINDOW *window, int skipcol, regmatch_t *regmatch)
3990 const struct got_error *err = NULL;
3992 wchar_t *wline = NULL;
3993 int rme, rms, n, width, scrollx;
3994 int width0 = 0, width1 = 0, width2 = 0;
3995 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3999 rms = regmatch->rm_so;
4000 rme = regmatch->rm_eo;
4002 err = expand_tab(&exstr, line);
4006 /* Split the line into 3 segments, according to match offsets. */
4007 seg0 = strndup(exstr, rms);
4009 err = got_error_from_errno("strndup");
4012 seg1 = strndup(exstr + rms, rme - rms);
4014 err = got_error_from_errno("strndup");
4017 seg2 = strdup(exstr + rme);
4019 err = got_error_from_errno("strndup");
4023 /* draw up to matched token if we haven't scrolled past it */
4024 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4028 n = MAX(width0 - skipcol, 0);
4031 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4032 wlimit, col_tab_align, 1);
4035 waddwstr(window, &wline[scrollx]);
4045 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4049 wlen = wcslen(wline);
4051 width = wcwidth(wline[i]);
4053 /* should not happen, tabs are expanded */
4054 err = got_error(GOT_ERR_RANGE);
4057 if (width0 + w + width > skipcol)
4062 /* draw (visible part of) matched token (if scrolled into it) */
4063 if (width1 - w > 0) {
4064 wattron(window, A_STANDOUT);
4065 waddwstr(window, &wline[i]);
4066 wattroff(window, A_STANDOUT);
4067 wlimit -= (width1 - w);
4068 *wtotal += (width1 - w);
4072 if (wlimit > 0) { /* draw rest of line */
4074 if (skipcol > width0 + width1) {
4075 err = format_line(&wline, &width2, &scrollx, seg2,
4076 skipcol - (width0 + width1), wlimit,
4080 waddwstr(window, &wline[scrollx]);
4082 err = format_line(&wline, &width2, NULL, seg2, 0,
4083 wlimit, col_tab_align, 1);
4086 waddwstr(window, wline);
4100 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4103 int *eof, *first, *selected;
4105 if (view->type == TOG_VIEW_DIFF) {
4106 struct tog_diff_view_state *s = &view->state.diff;
4108 first = &s->first_displayed_line;
4112 } else if (view->type == TOG_VIEW_BLAME) {
4113 struct tog_blame_view_state *s = &view->state.blame;
4115 first = &s->first_displayed_line;
4116 selected = &s->selected_line;
4122 /* Center gline in the middle of the page like vi(1). */
4123 if (*lineno < view->gline - (view->nlines - 3) / 2)
4125 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4134 *selected = view->gline <= (view->nlines - 3) / 2 ?
4135 view->gline : (view->nlines - 3) / 2 + 1;
4141 static const struct got_error *
4142 draw_file(struct tog_view *view, const char *header)
4144 struct tog_diff_view_state *s = &view->state.diff;
4145 regmatch_t *regmatch = &view->regmatch;
4146 const struct got_error *err;
4149 size_t linesize = 0;
4153 int max_lines = view->nlines;
4154 int nlines = s->nlines;
4157 s->lineno = s->first_displayed_line - 1;
4158 line_offset = s->lines[s->first_displayed_line - 1].offset;
4159 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4160 return got_error_from_errno("fseek");
4162 werase(view->window);
4164 if (view->gline > s->nlines - 1)
4165 view->gline = s->nlines - 1;
4168 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4169 1 : view->gline - (view->nlines - 3) / 2 :
4170 s->lineno + s->selected_line;
4172 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4173 return got_error_from_errno("asprintf");
4174 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4180 if (view_needs_focus_indication(view))
4181 wstandout(view->window);
4182 waddwstr(view->window, wline);
4185 while (width++ < view->ncols)
4186 waddch(view->window, ' ');
4187 if (view_needs_focus_indication(view))
4188 wstandend(view->window);
4198 while (max_lines > 0 && nprinted < max_lines) {
4199 enum got_diff_line_type linetype;
4202 linelen = getline(&line, &linesize, s->f);
4203 if (linelen == -1) {
4209 return got_ferror(s->f, GOT_ERR_IO);
4212 if (++s->lineno < s->first_displayed_line)
4214 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4216 if (s->lineno == view->hiline)
4219 /* Set view->maxx based on full line length. */
4220 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4226 view->maxx = MAX(view->maxx, width);
4230 linetype = s->lines[s->lineno].type;
4231 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4232 linetype < GOT_DIFF_LINE_CONTEXT)
4233 attr |= COLOR_PAIR(linetype);
4235 wattron(view->window, attr);
4236 if (s->first_displayed_line + nprinted == s->matched_line &&
4237 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4238 err = add_matched_line(&width, line, view->ncols, 0,
4239 view->window, view->x, regmatch);
4246 err = format_line(&wline, &width, &skip, line,
4247 view->x, view->ncols, 0, view->x ? 1 : 0);
4252 waddwstr(view->window, &wline[skip]);
4256 if (s->lineno == view->hiline) {
4257 /* highlight full gline length */
4258 while (width++ < view->ncols)
4259 waddch(view->window, ' ');
4261 if (width <= view->ncols - 1)
4262 waddch(view->window, '\n');
4265 wattroff(view->window, attr);
4266 if (++nprinted == 1)
4267 s->first_displayed_line = s->lineno;
4271 s->last_displayed_line = s->first_displayed_line +
4274 s->last_displayed_line = s->first_displayed_line;
4279 while (nprinted < view->nlines) {
4280 waddch(view->window, '\n');
4284 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4290 wstandout(view->window);
4291 waddwstr(view->window, wline);
4294 wstandend(view->window);
4301 get_datestr(time_t *time, char *datebuf)
4303 struct tm mytm, *tm;
4306 tm = gmtime_r(time, &mytm);
4309 s = asctime_r(tm, datebuf);
4312 p = strchr(s, '\n');
4318 static const struct got_error *
4319 get_changed_paths(struct got_pathlist_head *paths,
4320 struct got_commit_object *commit, struct got_repository *repo)
4322 const struct got_error *err = NULL;
4323 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4324 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4325 struct got_object_qid *qid;
4327 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4329 struct got_commit_object *pcommit;
4330 err = got_object_open_as_commit(&pcommit, repo,
4335 tree_id1 = got_object_id_dup(
4336 got_object_commit_get_tree_id(pcommit));
4337 if (tree_id1 == NULL) {
4338 got_object_commit_close(pcommit);
4339 return got_error_from_errno("got_object_id_dup");
4341 got_object_commit_close(pcommit);
4346 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4351 tree_id2 = got_object_commit_get_tree_id(commit);
4352 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4356 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4357 got_diff_tree_collect_changed_paths, paths, 0);
4360 got_object_tree_close(tree1);
4362 got_object_tree_close(tree2);
4367 static const struct got_error *
4368 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4369 off_t off, uint8_t type)
4371 struct got_diff_line *p;
4373 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4375 return got_error_from_errno("reallocarray");
4377 (*lines)[*nlines].offset = off;
4378 (*lines)[*nlines].type = type;
4384 static const struct got_error *
4385 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4386 struct got_object_id *commit_id, struct got_reflist_head *refs,
4387 struct got_repository *repo, FILE *outfile)
4389 const struct got_error *err = NULL;
4390 char datebuf[26], *datestr;
4391 struct got_commit_object *commit;
4392 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4393 time_t committer_time;
4394 const char *author, *committer;
4395 char *refs_str = NULL;
4396 struct got_pathlist_head changed_paths;
4397 struct got_pathlist_entry *pe;
4401 TAILQ_INIT(&changed_paths);
4404 err = build_refs_str(&refs_str, refs, commit_id, repo);
4409 err = got_object_open_as_commit(&commit, repo, commit_id);
4413 err = got_object_id_str(&id_str, commit_id);
4415 err = got_error_from_errno("got_object_id_str");
4419 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4423 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4424 refs_str ? refs_str : "", refs_str ? ")" : "");
4426 err = got_error_from_errno("fprintf");
4430 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4434 n = fprintf(outfile, "from: %s\n",
4435 got_object_commit_get_author(commit));
4437 err = got_error_from_errno("fprintf");
4441 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4445 committer_time = got_object_commit_get_committer_time(commit);
4446 datestr = get_datestr(&committer_time, datebuf);
4448 n = fprintf(outfile, "date: %s UTC\n", datestr);
4450 err = got_error_from_errno("fprintf");
4454 err = add_line_metadata(lines, nlines, outoff,
4455 GOT_DIFF_LINE_DATE);
4459 author = got_object_commit_get_author(commit);
4460 committer = got_object_commit_get_committer(commit);
4461 if (strcmp(author, committer) != 0) {
4462 n = fprintf(outfile, "via: %s\n", committer);
4464 err = got_error_from_errno("fprintf");
4468 err = add_line_metadata(lines, nlines, outoff,
4469 GOT_DIFF_LINE_AUTHOR);
4473 if (got_object_commit_get_nparents(commit) > 1) {
4474 const struct got_object_id_queue *parent_ids;
4475 struct got_object_qid *qid;
4477 parent_ids = got_object_commit_get_parent_ids(commit);
4478 STAILQ_FOREACH(qid, parent_ids, entry) {
4479 err = got_object_id_str(&id_str, &qid->id);
4482 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4484 err = got_error_from_errno("fprintf");
4488 err = add_line_metadata(lines, nlines, outoff,
4489 GOT_DIFF_LINE_META);
4497 err = got_object_commit_get_logmsg(&logmsg, commit);
4501 while ((line = strsep(&s, "\n")) != NULL) {
4502 n = fprintf(outfile, "%s\n", line);
4504 err = got_error_from_errno("fprintf");
4508 err = add_line_metadata(lines, nlines, outoff,
4509 GOT_DIFF_LINE_LOGMSG);
4514 err = get_changed_paths(&changed_paths, commit, repo);
4517 TAILQ_FOREACH(pe, &changed_paths, entry) {
4518 struct got_diff_changed_path *cp = pe->data;
4519 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4521 err = got_error_from_errno("fprintf");
4525 err = add_line_metadata(lines, nlines, outoff,
4526 GOT_DIFF_LINE_CHANGES);
4529 free((char *)pe->path);
4533 fputc('\n', outfile);
4535 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4537 got_pathlist_free(&changed_paths);
4541 got_object_commit_close(commit);
4550 static const struct got_error *
4551 create_diff(struct tog_diff_view_state *s)
4553 const struct got_error *err = NULL;
4558 s->lines = malloc(sizeof(*s->lines));
4559 if (s->lines == NULL)
4560 return got_error_from_errno("malloc");
4565 err = got_error_from_errno("got_opentemp");
4568 if (s->f && fclose(s->f) == EOF) {
4569 err = got_error_from_errno("fclose");
4575 err = got_object_get_type(&obj_type, s->repo, s->id1);
4577 err = got_object_get_type(&obj_type, s->repo, s->id2);
4582 case GOT_OBJ_TYPE_BLOB:
4583 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4584 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4585 s->label1, s->label2, tog_diff_algo, s->diff_context,
4586 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4588 case GOT_OBJ_TYPE_TREE:
4589 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4590 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4591 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4592 s->force_text_diff, s->repo, s->f);
4594 case GOT_OBJ_TYPE_COMMIT: {
4595 const struct got_object_id_queue *parent_ids;
4596 struct got_object_qid *pid;
4597 struct got_commit_object *commit2;
4598 struct got_reflist_head *refs;
4600 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4603 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4604 /* Show commit info if we're diffing to a parent/root commit. */
4605 if (s->id1 == NULL) {
4606 err = write_commit_info(&s->lines, &s->nlines, s->id2,
4607 refs, s->repo, s->f);
4611 parent_ids = got_object_commit_get_parent_ids(commit2);
4612 STAILQ_FOREACH(pid, parent_ids, entry) {
4613 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4614 err = write_commit_info(&s->lines,
4615 &s->nlines, s->id2, refs, s->repo,
4623 got_object_commit_close(commit2);
4625 err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4626 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4627 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4628 s->force_text_diff, s->repo, s->f);
4632 err = got_error(GOT_ERR_OBJ_TYPE);
4636 if (s->f && fflush(s->f) != 0 && err == NULL)
4637 err = got_error_from_errno("fflush");
4642 diff_view_indicate_progress(struct tog_view *view)
4644 mvwaddstr(view->window, 0, 0, "diffing...");
4649 static const struct got_error *
4650 search_start_diff_view(struct tog_view *view)
4652 struct tog_diff_view_state *s = &view->state.diff;
4654 s->matched_line = 0;
4658 static const struct got_error *
4659 search_next_diff_view(struct tog_view *view)
4661 struct tog_diff_view_state *s = &view->state.diff;
4662 const struct got_error *err = NULL;
4665 size_t linesize = 0;
4668 if (!view->searching) {
4669 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4673 if (s->matched_line) {
4674 if (view->searching == TOG_SEARCH_FORWARD)
4675 lineno = s->matched_line + 1;
4677 lineno = s->matched_line - 1;
4679 lineno = s->first_displayed_line;
4684 if (lineno <= 0 || lineno > s->nlines) {
4685 if (s->matched_line == 0) {
4686 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4690 if (view->searching == TOG_SEARCH_FORWARD)
4696 offset = s->lines[lineno - 1].offset;
4697 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4699 return got_error_from_errno("fseeko");
4701 linelen = getline(&line, &linesize, s->f);
4702 if (linelen != -1) {
4704 err = expand_tab(&exstr, line);
4707 if (match_line(exstr, &view->regex, 1,
4709 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4710 s->matched_line = lineno;
4716 if (view->searching == TOG_SEARCH_FORWARD)
4723 if (s->matched_line) {
4724 s->first_displayed_line = s->matched_line;
4725 s->selected_line = 1;
4731 static const struct got_error *
4732 close_diff_view(struct tog_view *view)
4734 const struct got_error *err = NULL;
4735 struct tog_diff_view_state *s = &view->state.diff;
4741 if (s->f && fclose(s->f) == EOF)
4742 err = got_error_from_errno("fclose");
4744 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4745 err = got_error_from_errno("fclose");
4747 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4748 err = got_error_from_errno("fclose");
4750 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4751 err = got_error_from_errno("close");
4753 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4754 err = got_error_from_errno("close");
4762 static const struct got_error *
4763 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4764 struct got_object_id *id2, const char *label1, const char *label2,
4765 int diff_context, int ignore_whitespace, int force_text_diff,
4766 struct tog_view *parent_view, struct got_repository *repo)
4768 const struct got_error *err;
4769 struct tog_diff_view_state *s = &view->state.diff;
4771 memset(s, 0, sizeof(*s));
4775 if (id1 != NULL && id2 != NULL) {
4777 err = got_object_get_type(&type1, repo, id1);
4780 err = got_object_get_type(&type2, repo, id2);
4785 return got_error(GOT_ERR_OBJ_TYPE);
4787 s->first_displayed_line = 1;
4788 s->last_displayed_line = view->nlines;
4789 s->selected_line = 1;
4797 s->id1 = got_object_id_dup(id1);
4799 return got_error_from_errno("got_object_id_dup");
4803 s->id2 = got_object_id_dup(id2);
4804 if (s->id2 == NULL) {
4805 err = got_error_from_errno("got_object_id_dup");
4809 s->f1 = got_opentemp();
4810 if (s->f1 == NULL) {
4811 err = got_error_from_errno("got_opentemp");
4815 s->f2 = got_opentemp();
4816 if (s->f2 == NULL) {
4817 err = got_error_from_errno("got_opentemp");
4821 s->fd1 = got_opentempfd();
4823 err = got_error_from_errno("got_opentempfd");
4827 s->fd2 = got_opentempfd();
4829 err = got_error_from_errno("got_opentempfd");
4833 s->first_displayed_line = 1;
4834 s->last_displayed_line = view->nlines;
4835 s->diff_context = diff_context;
4836 s->ignore_whitespace = ignore_whitespace;
4837 s->force_text_diff = force_text_diff;
4838 s->parent_view = parent_view;
4841 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4844 rc = init_pair(GOT_DIFF_LINE_MINUS,
4845 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
4847 rc = init_pair(GOT_DIFF_LINE_PLUS,
4848 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
4850 rc = init_pair(GOT_DIFF_LINE_HUNK,
4851 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
4853 rc = init_pair(GOT_DIFF_LINE_META,
4854 get_color_value("TOG_COLOR_DIFF_META"), -1);
4856 rc = init_pair(GOT_DIFF_LINE_CHANGES,
4857 get_color_value("TOG_COLOR_DIFF_META"), -1);
4859 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
4860 get_color_value("TOG_COLOR_DIFF_META"), -1);
4862 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
4863 get_color_value("TOG_COLOR_DIFF_META"), -1);
4865 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
4866 get_color_value("TOG_COLOR_AUTHOR"), -1);
4868 rc = init_pair(GOT_DIFF_LINE_DATE,
4869 get_color_value("TOG_COLOR_DATE"), -1);
4871 err = got_error(GOT_ERR_RANGE);
4876 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4877 view_is_splitscreen(view))
4878 show_log_view(parent_view); /* draw border */
4879 diff_view_indicate_progress(view);
4881 err = create_diff(s);
4883 view->show = show_diff_view;
4884 view->input = input_diff_view;
4885 view->reset = reset_diff_view;
4886 view->close = close_diff_view;
4887 view->search_start = search_start_diff_view;
4888 view->search_next = search_next_diff_view;
4891 close_diff_view(view);
4895 static const struct got_error *
4896 show_diff_view(struct tog_view *view)
4898 const struct got_error *err;
4899 struct tog_diff_view_state *s = &view->state.diff;
4900 char *id_str1 = NULL, *id_str2, *header;
4901 const char *label1, *label2;
4904 err = got_object_id_str(&id_str1, s->id1);
4907 label1 = s->label1 ? s->label1 : id_str1;
4909 label1 = "/dev/null";
4911 err = got_object_id_str(&id_str2, s->id2);
4914 label2 = s->label2 ? s->label2 : id_str2;
4916 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4917 err = got_error_from_errno("asprintf");
4925 err = draw_file(view, header);
4930 static const struct got_error *
4931 set_selected_commit(struct tog_diff_view_state *s,
4932 struct commit_queue_entry *entry)
4934 const struct got_error *err;
4935 const struct got_object_id_queue *parent_ids;
4936 struct got_commit_object *selected_commit;
4937 struct got_object_qid *pid;
4940 s->id2 = got_object_id_dup(entry->id);
4942 return got_error_from_errno("got_object_id_dup");
4944 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4947 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4949 pid = STAILQ_FIRST(parent_ids);
4950 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4951 got_object_commit_close(selected_commit);
4955 static const struct got_error *
4956 reset_diff_view(struct tog_view *view)
4958 struct tog_diff_view_state *s = &view->state.diff;
4961 wclear(view->window);
4962 s->first_displayed_line = 1;
4963 s->last_displayed_line = view->nlines;
4964 s->matched_line = 0;
4965 diff_view_indicate_progress(view);
4966 return create_diff(s);
4970 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4974 i = start = s->first_displayed_line - 1;
4976 while (s->lines[i].type != type) {
4980 return; /* do nothing, requested type not in file */
4983 s->selected_line = 1;
4984 s->first_displayed_line = i;
4988 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4992 i = start = s->first_displayed_line + 1;
4994 while (s->lines[i].type != type) {
4995 if (i == s->nlines - 1)
4998 return; /* do nothing, requested type not in file */
5001 s->selected_line = 1;
5002 s->first_displayed_line = i;
5005 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5007 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5010 static const struct got_error *
5011 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5013 const struct got_error *err = NULL;
5014 struct tog_diff_view_state *s = &view->state.diff;
5015 struct tog_log_view_state *ls;
5016 struct commit_queue_entry *old_selected_entry;
5018 size_t linesize = 0;
5020 int i, nscroll = view->nlines - 1, up = 0;
5022 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5029 view->x = MAX(view->maxx - view->ncols / 3, 0);
5034 if (view->x + view->ncols / 3 < view->maxx)
5035 view->x += 2; /* move two columns right */
5041 view->x -= MIN(view->x, 2); /* move two columns back */
5048 s->force_text_diff = !s->force_text_diff;
5050 s->ignore_whitespace = !s->ignore_whitespace;
5051 err = reset_diff_view(view);
5055 s->first_displayed_line = 1;
5064 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5070 if (s->first_displayed_line > 1)
5071 s->first_displayed_line--;
5082 if (s->first_displayed_line == 1) {
5087 while (i++ < nscroll && s->first_displayed_line > 1)
5088 s->first_displayed_line--;
5094 s->first_displayed_line++;
5111 while (!s->eof && i++ < nscroll) {
5112 linelen = getline(&line, &linesize, s->f);
5113 s->first_displayed_line++;
5114 if (linelen == -1) {
5118 err = got_ferror(s->f, GOT_ERR_IO);
5125 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5128 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5131 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5134 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5137 if (s->diff_context > 0) {
5139 s->matched_line = 0;
5140 diff_view_indicate_progress(view);
5141 err = create_diff(s);
5142 if (s->first_displayed_line + view->nlines - 1 >
5144 s->first_displayed_line = 1;
5145 s->last_displayed_line = view->nlines;
5151 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5153 s->matched_line = 0;
5154 diff_view_indicate_progress(view);
5155 err = create_diff(s);
5167 if (s->parent_view == NULL) {
5171 s->parent_view->count = view->count;
5173 if (s->parent_view->type == TOG_VIEW_LOG) {
5174 ls = &s->parent_view->state.log;
5175 old_selected_entry = ls->selected_entry;
5177 err = input_log_view(NULL, s->parent_view,
5178 up ? KEY_UP : KEY_DOWN);
5181 view->count = s->parent_view->count;
5183 if (old_selected_entry == ls->selected_entry)
5186 err = set_selected_commit(s, ls->selected_entry);
5189 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5190 struct tog_blame_view_state *bs;
5191 struct got_object_id *id, *prev_id;
5193 bs = &s->parent_view->state.blame;
5194 prev_id = get_annotation_for_line(bs->blame.lines,
5195 bs->blame.nlines, bs->last_diffed_line);
5197 err = input_blame_view(&view, s->parent_view,
5198 up ? KEY_UP : KEY_DOWN);
5201 view->count = s->parent_view->count;
5203 if (prev_id == NULL)
5205 id = get_selected_commit_id(bs->blame.lines,
5206 bs->blame.nlines, bs->first_displayed_line,
5211 if (!got_object_id_cmp(prev_id, id))
5214 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5218 s->first_displayed_line = 1;
5219 s->last_displayed_line = view->nlines;
5220 s->matched_line = 0;
5223 diff_view_indicate_progress(view);
5224 err = create_diff(s);
5234 static const struct got_error *
5235 cmd_diff(int argc, char *argv[])
5237 const struct got_error *error = NULL;
5238 struct got_repository *repo = NULL;
5239 struct got_worktree *worktree = NULL;
5240 struct got_object_id *id1 = NULL, *id2 = NULL;
5241 char *repo_path = NULL, *cwd = NULL;
5242 char *id_str1 = NULL, *id_str2 = NULL;
5243 char *label1 = NULL, *label2 = NULL;
5244 int diff_context = 3, ignore_whitespace = 0;
5245 int ch, force_text_diff = 0;
5247 struct tog_view *view;
5248 int *pack_fds = NULL;
5250 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5253 force_text_diff = 1;
5256 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5259 errx(1, "number of context lines is %s: %s",
5263 repo_path = realpath(optarg, NULL);
5264 if (repo_path == NULL)
5265 return got_error_from_errno2("realpath",
5267 got_path_strip_trailing_slashes(repo_path);
5270 ignore_whitespace = 1;
5282 usage_diff(); /* TODO show local worktree changes */
5283 } else if (argc == 2) {
5289 error = got_repo_pack_fds_open(&pack_fds);
5293 if (repo_path == NULL) {
5294 cwd = getcwd(NULL, 0);
5296 return got_error_from_errno("getcwd");
5297 error = got_worktree_open(&worktree, cwd);
5298 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5302 strdup(got_worktree_get_repo_path(worktree));
5304 repo_path = strdup(cwd);
5305 if (repo_path == NULL) {
5306 error = got_error_from_errno("strdup");
5311 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5317 error = apply_unveil(got_repo_get_path(repo), NULL);
5321 error = tog_load_refs(repo, 0);
5325 error = got_repo_match_object_id(&id1, &label1, id_str1,
5326 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5330 error = got_repo_match_object_id(&id2, &label2, id_str2,
5331 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5335 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5337 error = got_error_from_errno("view_open");
5340 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5341 ignore_whitespace, force_text_diff, NULL, repo);
5344 error = view_loop(view);
5351 const struct got_error *close_err = got_repo_close(repo);
5356 got_worktree_close(worktree);
5358 const struct got_error *pack_err =
5359 got_repo_pack_fds_close(pack_fds);
5372 "usage: %s blame [-c commit] [-r repository-path] path\n",
5377 struct tog_blame_line {
5379 struct got_object_id *id;
5382 static const struct got_error *
5383 draw_blame(struct tog_view *view)
5385 struct tog_blame_view_state *s = &view->state.blame;
5386 struct tog_blame *blame = &s->blame;
5387 regmatch_t *regmatch = &view->regmatch;
5388 const struct got_error *err;
5389 int lineno = 0, nprinted = 0;
5391 size_t linesize = 0;
5395 struct tog_blame_line *blame_line;
5396 struct got_object_id *prev_id = NULL;
5398 struct tog_color *tc;
5400 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5405 werase(view->window);
5407 if (asprintf(&line, "commit %s", id_str) == -1) {
5408 err = got_error_from_errno("asprintf");
5413 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5418 if (view_needs_focus_indication(view))
5419 wstandout(view->window);
5420 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5422 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5423 waddwstr(view->window, wline);
5424 while (width++ < view->ncols)
5425 waddch(view->window, ' ');
5427 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5428 if (view_needs_focus_indication(view))
5429 wstandend(view->window);
5433 if (view->gline > blame->nlines)
5434 view->gline = blame->nlines;
5436 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5437 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5438 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5440 return got_error_from_errno("asprintf");
5443 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5448 waddwstr(view->window, wline);
5451 if (width < view->ncols - 1)
5452 waddch(view->window, '\n');
5456 while (nprinted < view->nlines - 2) {
5457 linelen = getline(&line, &linesize, blame->f);
5458 if (linelen == -1) {
5459 if (feof(blame->f)) {
5464 return got_ferror(blame->f, GOT_ERR_IO);
5466 if (++lineno < s->first_displayed_line)
5468 if (view->gline && !gotoline(view, &lineno, &nprinted))
5471 /* Set view->maxx based on full line length. */
5472 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5479 view->maxx = MAX(view->maxx, width);
5481 if (nprinted == s->selected_line - 1)
5482 wstandout(view->window);
5484 if (blame->nlines > 0) {
5485 blame_line = &blame->lines[lineno - 1];
5486 if (blame_line->annotated && prev_id &&
5487 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5488 !(nprinted == s->selected_line - 1)) {
5489 waddstr(view->window, " ");
5490 } else if (blame_line->annotated) {
5492 err = got_object_id_str(&id_str,
5498 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5500 wattr_on(view->window,
5501 COLOR_PAIR(tc->colorpair), NULL);
5502 wprintw(view->window, "%.8s", id_str);
5504 wattr_off(view->window,
5505 COLOR_PAIR(tc->colorpair), NULL);
5507 prev_id = blame_line->id;
5509 waddstr(view->window, "........");
5513 waddstr(view->window, "........");
5517 if (nprinted == s->selected_line - 1)
5518 wstandend(view->window);
5519 waddstr(view->window, " ");
5521 if (view->ncols <= 9) {
5523 } else if (s->first_displayed_line + nprinted ==
5525 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5526 err = add_matched_line(&width, line, view->ncols - 9, 9,
5527 view->window, view->x, regmatch);
5535 err = format_line(&wline, &width, &skip, line,
5536 view->x, view->ncols - 9, 9, 1);
5541 waddwstr(view->window, &wline[skip]);
5547 if (width <= view->ncols - 1)
5548 waddch(view->window, '\n');
5549 if (++nprinted == 1)
5550 s->first_displayed_line = lineno;
5553 s->last_displayed_line = lineno;
5560 static const struct got_error *
5561 blame_cb(void *arg, int nlines, int lineno,
5562 struct got_commit_object *commit, struct got_object_id *id)
5564 const struct got_error *err = NULL;
5565 struct tog_blame_cb_args *a = arg;
5566 struct tog_blame_line *line;
5569 if (nlines != a->nlines ||
5570 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5571 return got_error(GOT_ERR_RANGE);
5573 errcode = pthread_mutex_lock(&tog_mutex);
5575 return got_error_set_errno(errcode, "pthread_mutex_lock");
5577 if (*a->quit) { /* user has quit the blame view */
5578 err = got_error(GOT_ERR_ITER_COMPLETED);
5583 goto done; /* no change in this commit */
5585 line = &a->lines[lineno - 1];
5586 if (line->annotated)
5589 line->id = got_object_id_dup(id);
5590 if (line->id == NULL) {
5591 err = got_error_from_errno("got_object_id_dup");
5594 line->annotated = 1;
5596 errcode = pthread_mutex_unlock(&tog_mutex);
5598 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5603 blame_thread(void *arg)
5605 const struct got_error *err, *close_err;
5606 struct tog_blame_thread_args *ta = arg;
5607 struct tog_blame_cb_args *a = ta->cb_args;
5608 int errcode, fd1 = -1, fd2 = -1;
5609 FILE *f1 = NULL, *f2 = NULL;
5611 fd1 = got_opentempfd();
5613 return (void *)got_error_from_errno("got_opentempfd");
5615 fd2 = got_opentempfd();
5617 err = got_error_from_errno("got_opentempfd");
5621 f1 = got_opentemp();
5623 err = (void *)got_error_from_errno("got_opentemp");
5626 f2 = got_opentemp();
5628 err = (void *)got_error_from_errno("got_opentemp");
5632 err = block_signals_used_by_main_thread();
5636 err = got_blame(ta->path, a->commit_id, ta->repo,
5637 tog_diff_algo, blame_cb, ta->cb_args,
5638 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5639 if (err && err->code == GOT_ERR_CANCELLED)
5642 errcode = pthread_mutex_lock(&tog_mutex);
5644 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5648 close_err = got_repo_close(ta->repo);
5654 errcode = pthread_mutex_unlock(&tog_mutex);
5655 if (errcode && err == NULL)
5656 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5659 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5660 err = got_error_from_errno("close");
5661 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5662 err = got_error_from_errno("close");
5663 if (f1 && fclose(f1) == EOF && err == NULL)
5664 err = got_error_from_errno("fclose");
5665 if (f2 && fclose(f2) == EOF && err == NULL)
5666 err = got_error_from_errno("fclose");
5671 static struct got_object_id *
5672 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5673 int first_displayed_line, int selected_line)
5675 struct tog_blame_line *line;
5680 line = &lines[first_displayed_line - 1 + selected_line - 1];
5681 if (!line->annotated)
5687 static struct got_object_id *
5688 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5691 struct tog_blame_line *line;
5693 if (nlines <= 0 || lineno >= nlines)
5696 line = &lines[lineno - 1];
5697 if (!line->annotated)
5703 static const struct got_error *
5704 stop_blame(struct tog_blame *blame)
5706 const struct got_error *err = NULL;
5709 if (blame->thread) {
5711 errcode = pthread_mutex_unlock(&tog_mutex);
5713 return got_error_set_errno(errcode,
5714 "pthread_mutex_unlock");
5715 errcode = pthread_join(blame->thread, (void **)&err);
5717 return got_error_set_errno(errcode, "pthread_join");
5718 errcode = pthread_mutex_lock(&tog_mutex);
5720 return got_error_set_errno(errcode,
5721 "pthread_mutex_lock");
5722 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5724 blame->thread = NULL;
5726 if (blame->thread_args.repo) {
5727 const struct got_error *close_err;
5728 close_err = got_repo_close(blame->thread_args.repo);
5731 blame->thread_args.repo = NULL;
5734 if (fclose(blame->f) == EOF && err == NULL)
5735 err = got_error_from_errno("fclose");
5739 for (i = 0; i < blame->nlines; i++)
5740 free(blame->lines[i].id);
5742 blame->lines = NULL;
5744 free(blame->cb_args.commit_id);
5745 blame->cb_args.commit_id = NULL;
5746 if (blame->pack_fds) {
5747 const struct got_error *pack_err =
5748 got_repo_pack_fds_close(blame->pack_fds);
5751 blame->pack_fds = NULL;
5756 static const struct got_error *
5757 cancel_blame_view(void *arg)
5759 const struct got_error *err = NULL;
5763 errcode = pthread_mutex_lock(&tog_mutex);
5765 return got_error_set_errno(errcode,
5766 "pthread_mutex_unlock");
5769 err = got_error(GOT_ERR_CANCELLED);
5771 errcode = pthread_mutex_unlock(&tog_mutex);
5773 return got_error_set_errno(errcode,
5774 "pthread_mutex_lock");
5779 static const struct got_error *
5780 run_blame(struct tog_view *view)
5782 struct tog_blame_view_state *s = &view->state.blame;
5783 struct tog_blame *blame = &s->blame;
5784 const struct got_error *err = NULL;
5785 struct got_commit_object *commit = NULL;
5786 struct got_blob_object *blob = NULL;
5787 struct got_repository *thread_repo = NULL;
5788 struct got_object_id *obj_id = NULL;
5789 int obj_type, fd = -1;
5790 int *pack_fds = NULL;
5792 err = got_object_open_as_commit(&commit, s->repo,
5793 &s->blamed_commit->id);
5797 fd = got_opentempfd();
5799 err = got_error_from_errno("got_opentempfd");
5803 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5807 err = got_object_get_type(&obj_type, s->repo, obj_id);
5811 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5812 err = got_error(GOT_ERR_OBJ_TYPE);
5816 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5819 blame->f = got_opentemp();
5820 if (blame->f == NULL) {
5821 err = got_error_from_errno("got_opentemp");
5824 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5825 &blame->line_offsets, blame->f, blob);
5828 if (blame->nlines == 0) {
5829 s->blame_complete = 1;
5833 /* Don't include \n at EOF in the blame line count. */
5834 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5837 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5838 if (blame->lines == NULL) {
5839 err = got_error_from_errno("calloc");
5843 err = got_repo_pack_fds_open(&pack_fds);
5846 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5851 blame->pack_fds = pack_fds;
5852 blame->cb_args.view = view;
5853 blame->cb_args.lines = blame->lines;
5854 blame->cb_args.nlines = blame->nlines;
5855 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5856 if (blame->cb_args.commit_id == NULL) {
5857 err = got_error_from_errno("got_object_id_dup");
5860 blame->cb_args.quit = &s->done;
5862 blame->thread_args.path = s->path;
5863 blame->thread_args.repo = thread_repo;
5864 blame->thread_args.cb_args = &blame->cb_args;
5865 blame->thread_args.complete = &s->blame_complete;
5866 blame->thread_args.cancel_cb = cancel_blame_view;
5867 blame->thread_args.cancel_arg = &s->done;
5868 s->blame_complete = 0;
5870 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5871 s->first_displayed_line = 1;
5872 s->last_displayed_line = view->nlines;
5873 s->selected_line = 1;
5875 s->matched_line = 0;
5879 got_object_commit_close(commit);
5880 if (fd != -1 && close(fd) == -1 && err == NULL)
5881 err = got_error_from_errno("close");
5883 got_object_blob_close(blob);
5890 static const struct got_error *
5891 open_blame_view(struct tog_view *view, char *path,
5892 struct got_object_id *commit_id, struct got_repository *repo)
5894 const struct got_error *err = NULL;
5895 struct tog_blame_view_state *s = &view->state.blame;
5897 STAILQ_INIT(&s->blamed_commits);
5899 s->path = strdup(path);
5900 if (s->path == NULL)
5901 return got_error_from_errno("strdup");
5903 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5909 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5910 s->first_displayed_line = 1;
5911 s->last_displayed_line = view->nlines;
5912 s->selected_line = 1;
5913 s->blame_complete = 0;
5915 s->commit_id = commit_id;
5916 memset(&s->blame, 0, sizeof(s->blame));
5918 STAILQ_INIT(&s->colors);
5919 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5920 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5921 get_color_value("TOG_COLOR_COMMIT"));
5926 view->show = show_blame_view;
5927 view->input = input_blame_view;
5928 view->reset = reset_blame_view;
5929 view->close = close_blame_view;
5930 view->search_start = search_start_blame_view;
5931 view->search_next = search_next_blame_view;
5933 return run_blame(view);
5936 static const struct got_error *
5937 close_blame_view(struct tog_view *view)
5939 const struct got_error *err = NULL;
5940 struct tog_blame_view_state *s = &view->state.blame;
5942 if (s->blame.thread)
5943 err = stop_blame(&s->blame);
5945 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5946 struct got_object_qid *blamed_commit;
5947 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5948 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5949 got_object_qid_free(blamed_commit);
5953 free_colors(&s->colors);
5957 static const struct got_error *
5958 search_start_blame_view(struct tog_view *view)
5960 struct tog_blame_view_state *s = &view->state.blame;
5962 s->matched_line = 0;
5966 static const struct got_error *
5967 search_next_blame_view(struct tog_view *view)
5969 struct tog_blame_view_state *s = &view->state.blame;
5970 const struct got_error *err = NULL;
5973 size_t linesize = 0;
5976 if (!view->searching) {
5977 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5981 if (s->matched_line) {
5982 if (view->searching == TOG_SEARCH_FORWARD)
5983 lineno = s->matched_line + 1;
5985 lineno = s->matched_line - 1;
5987 lineno = s->first_displayed_line - 1 + s->selected_line;
5992 if (lineno <= 0 || lineno > s->blame.nlines) {
5993 if (s->matched_line == 0) {
5994 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5998 if (view->searching == TOG_SEARCH_FORWARD)
6001 lineno = s->blame.nlines;
6004 offset = s->blame.line_offsets[lineno - 1];
6005 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
6007 return got_error_from_errno("fseeko");
6009 linelen = getline(&line, &linesize, s->blame.f);
6010 if (linelen != -1) {
6012 err = expand_tab(&exstr, line);
6015 if (match_line(exstr, &view->regex, 1,
6017 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6018 s->matched_line = lineno;
6024 if (view->searching == TOG_SEARCH_FORWARD)
6031 if (s->matched_line) {
6032 s->first_displayed_line = s->matched_line;
6033 s->selected_line = 1;
6039 static const struct got_error *
6040 show_blame_view(struct tog_view *view)
6042 const struct got_error *err = NULL;
6043 struct tog_blame_view_state *s = &view->state.blame;
6046 if (s->blame.thread == NULL && !s->blame_complete) {
6047 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6048 &s->blame.thread_args);
6050 return got_error_set_errno(errcode, "pthread_create");
6052 halfdelay(1); /* fast refresh while annotating */
6055 if (s->blame_complete)
6056 halfdelay(10); /* disable fast refresh */
6058 err = draw_blame(view);
6064 static const struct got_error *
6065 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6066 struct got_repository *repo, struct got_object_id *id)
6068 struct tog_view *log_view;
6069 const struct got_error *err = NULL;
6073 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6074 if (log_view == NULL)
6075 return got_error_from_errno("view_open");
6077 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6079 view_close(log_view);
6081 *new_view = log_view;
6086 static const struct got_error *
6087 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6089 const struct got_error *err = NULL, *thread_err = NULL;
6090 struct tog_view *diff_view;
6091 struct tog_blame_view_state *s = &view->state.blame;
6092 int eos, nscroll, begin_y = 0, begin_x = 0;
6094 eos = nscroll = view->nlines - 2;
6095 if (view_is_hsplit_top(view))
6103 view->x = MAX(view->maxx - view->ncols / 3, 0);
6108 if (view->x + view->ncols / 3 < view->maxx)
6109 view->x += 2; /* move two columns right */
6115 view->x -= MIN(view->x, 2); /* move two columns back */
6124 s->selected_line = 1;
6125 s->first_displayed_line = 1;
6130 if (s->blame.nlines < eos) {
6131 s->selected_line = s->blame.nlines;
6132 s->first_displayed_line = 1;
6134 s->selected_line = eos;
6135 s->first_displayed_line = s->blame.nlines - (eos - 1);
6142 if (s->selected_line > 1)
6144 else if (s->selected_line == 1 &&
6145 s->first_displayed_line > 1)
6146 s->first_displayed_line--;
6157 if (s->first_displayed_line == 1) {
6158 if (view->count > 1)
6160 s->selected_line = MAX(1, s->selected_line - nscroll);
6164 if (s->first_displayed_line > nscroll)
6165 s->first_displayed_line -= nscroll;
6167 s->first_displayed_line = 1;
6172 if (s->selected_line < eos && s->first_displayed_line +
6173 s->selected_line <= s->blame.nlines)
6175 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6176 s->first_displayed_line++;
6182 struct got_object_id *id = NULL;
6185 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6186 s->first_displayed_line, s->selected_line);
6190 struct got_commit_object *commit, *pcommit;
6191 struct got_object_qid *pid;
6192 struct got_object_id *blob_id = NULL;
6194 err = got_object_open_as_commit(&commit,
6199 got_object_commit_get_parent_ids(commit));
6201 got_object_commit_close(commit);
6204 /* Check if path history ends here. */
6205 err = got_object_open_as_commit(&pcommit,
6209 err = got_object_id_by_path(&blob_id, s->repo,
6211 got_object_commit_close(pcommit);
6213 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6215 got_object_commit_close(commit);
6218 err = got_object_get_type(&obj_type, s->repo,
6221 /* Can't blame non-blob type objects. */
6222 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6223 got_object_commit_close(commit);
6226 err = got_object_qid_alloc(&s->blamed_commit,
6228 got_object_commit_close(commit);
6230 if (got_object_id_cmp(id,
6231 &s->blamed_commit->id) == 0)
6233 err = got_object_qid_alloc(&s->blamed_commit,
6239 thread_err = stop_blame(&s->blame);
6243 STAILQ_INSERT_HEAD(&s->blamed_commits,
6244 s->blamed_commit, entry);
6245 err = run_blame(view);
6251 struct got_object_qid *first;
6254 first = STAILQ_FIRST(&s->blamed_commits);
6255 if (!got_object_id_cmp(&first->id, s->commit_id))
6258 thread_err = stop_blame(&s->blame);
6262 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6263 got_object_qid_free(s->blamed_commit);
6265 STAILQ_FIRST(&s->blamed_commits);
6266 err = run_blame(view);
6273 s->id_to_log = get_selected_commit_id(s->blame.lines,
6274 s->blame.nlines, s->first_displayed_line, s->selected_line);
6276 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6280 struct got_object_id *id = NULL;
6281 struct got_object_qid *pid;
6282 struct got_commit_object *commit = NULL;
6285 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6286 s->first_displayed_line, s->selected_line);
6289 err = got_object_open_as_commit(&commit, s->repo, id);
6292 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6294 /* traversed from diff view, release diff resources */
6295 err = close_diff_view(*new_view);
6298 diff_view = *new_view;
6300 if (view_is_parent_view(view))
6301 view_get_split(view, &begin_y, &begin_x);
6303 diff_view = view_open(0, 0, begin_y, begin_x,
6305 if (diff_view == NULL) {
6306 got_object_commit_close(commit);
6307 err = got_error_from_errno("view_open");
6311 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6312 id, NULL, NULL, 3, 0, 0, view, s->repo);
6313 got_object_commit_close(commit);
6315 view_close(diff_view);
6318 s->last_diffed_line = s->first_displayed_line - 1 +
6321 break; /* still open from active diff view */
6322 if (view_is_parent_view(view) &&
6323 view->mode == TOG_VIEW_SPLIT_HRZN) {
6324 err = view_init_hsplit(view, begin_y);
6330 diff_view->focussed = 1;
6331 diff_view->mode = view->mode;
6332 diff_view->nlines = view->lines - begin_y;
6333 if (view_is_parent_view(view)) {
6334 view_transfer_size(diff_view, view);
6335 err = view_close_child(view);
6338 err = view_set_child(view, diff_view);
6341 view->focus_child = 1;
6343 *new_view = diff_view;
6356 if (s->last_displayed_line >= s->blame.nlines &&
6357 s->selected_line >= MIN(s->blame.nlines,
6358 view->nlines - 2)) {
6362 if (s->last_displayed_line >= s->blame.nlines &&
6363 s->selected_line < view->nlines - 2) {
6365 MIN(nscroll, s->last_displayed_line -
6366 s->first_displayed_line - s->selected_line + 1);
6368 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6369 s->first_displayed_line += nscroll;
6371 s->first_displayed_line =
6372 s->blame.nlines - (view->nlines - 3);
6375 if (s->selected_line > view->nlines - 2) {
6376 s->selected_line = MIN(s->blame.nlines,
6384 return thread_err ? thread_err : err;
6387 static const struct got_error *
6388 reset_blame_view(struct tog_view *view)
6390 const struct got_error *err;
6391 struct tog_blame_view_state *s = &view->state.blame;
6395 err = stop_blame(&s->blame);
6399 return run_blame(view);
6402 static const struct got_error *
6403 cmd_blame(int argc, char *argv[])
6405 const struct got_error *error;
6406 struct got_repository *repo = NULL;
6407 struct got_worktree *worktree = NULL;
6408 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6409 char *link_target = NULL;
6410 struct got_object_id *commit_id = NULL;
6411 struct got_commit_object *commit = NULL;
6412 char *commit_id_str = NULL;
6414 struct tog_view *view;
6415 int *pack_fds = NULL;
6417 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6420 commit_id_str = optarg;
6423 repo_path = realpath(optarg, NULL);
6424 if (repo_path == NULL)
6425 return got_error_from_errno2("realpath",
6440 error = got_repo_pack_fds_open(&pack_fds);
6444 if (repo_path == NULL) {
6445 cwd = getcwd(NULL, 0);
6447 return got_error_from_errno("getcwd");
6448 error = got_worktree_open(&worktree, cwd);
6449 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6453 strdup(got_worktree_get_repo_path(worktree));
6455 repo_path = strdup(cwd);
6456 if (repo_path == NULL) {
6457 error = got_error_from_errno("strdup");
6462 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6466 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6473 error = apply_unveil(got_repo_get_path(repo), NULL);
6477 error = tog_load_refs(repo, 0);
6481 if (commit_id_str == NULL) {
6482 struct got_reference *head_ref;
6483 error = got_ref_open(&head_ref, repo, worktree ?
6484 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6487 error = got_ref_resolve(&commit_id, repo, head_ref);
6488 got_ref_close(head_ref);
6490 error = got_repo_match_object_id(&commit_id, NULL,
6491 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6496 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6498 error = got_error_from_errno("view_open");
6502 error = got_object_open_as_commit(&commit, repo, commit_id);
6506 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6511 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6516 /* Release work tree lock. */
6517 got_worktree_close(worktree);
6520 error = view_loop(view);
6528 got_object_commit_close(commit);
6530 got_worktree_close(worktree);
6532 const struct got_error *close_err = got_repo_close(repo);
6537 const struct got_error *pack_err =
6538 got_repo_pack_fds_close(pack_fds);
6546 static const struct got_error *
6547 draw_tree_entries(struct tog_view *view, const char *parent_path)
6549 struct tog_tree_view_state *s = &view->state.tree;
6550 const struct got_error *err = NULL;
6551 struct got_tree_entry *te;
6554 struct tog_color *tc;
6555 int width, n, nentries, i = 1;
6556 int limit = view->nlines;
6559 if (view_is_hsplit_top(view))
6560 --limit; /* border */
6562 werase(view->window);
6567 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6571 if (view_needs_focus_indication(view))
6572 wstandout(view->window);
6573 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6575 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6576 waddwstr(view->window, wline);
6579 while (width++ < view->ncols)
6580 waddch(view->window, ' ');
6582 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6583 if (view_needs_focus_indication(view))
6584 wstandend(view->window);
6589 if (s->first_displayed_entry) {
6590 i += got_tree_entry_get_index(s->first_displayed_entry);
6591 if (s->tree != s->root)
6592 ++i; /* account for ".." entry */
6594 nentries = got_object_tree_get_nentries(s->tree);
6595 if (asprintf(&index, "[%d/%d] %s",
6596 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6597 return got_error_from_errno("asprintf");
6598 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6602 waddwstr(view->window, wline);
6605 if (width < view->ncols - 1)
6606 waddch(view->window, '\n');
6609 waddch(view->window, '\n');
6613 if (s->first_displayed_entry == NULL) {
6614 te = got_object_tree_get_first_entry(s->tree);
6615 if (s->selected == 0) {
6617 wstandout(view->window);
6618 s->selected_entry = NULL;
6620 waddstr(view->window, " ..\n"); /* parent directory */
6621 if (s->selected == 0 && view->focussed)
6622 wstandend(view->window);
6629 te = s->first_displayed_entry;
6632 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6633 char *line = NULL, *id_str = NULL, *link_target = NULL;
6634 const char *modestr = "";
6637 te = got_object_tree_get_entry(s->tree, i);
6638 mode = got_tree_entry_get_mode(te);
6641 err = got_object_id_str(&id_str,
6642 got_tree_entry_get_id(te));
6644 return got_error_from_errno(
6645 "got_object_id_str");
6647 if (got_object_tree_entry_is_submodule(te))
6649 else if (S_ISLNK(mode)) {
6652 err = got_tree_entry_get_symlink_target(&link_target,
6658 for (i = 0; i < strlen(link_target); i++) {
6659 if (!isprint((unsigned char)link_target[i]))
6660 link_target[i] = '?';
6664 else if (S_ISDIR(mode))
6666 else if (mode & S_IXUSR)
6668 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6669 got_tree_entry_get_name(te), modestr,
6670 link_target ? " -> ": "",
6671 link_target ? link_target : "") == -1) {
6674 return got_error_from_errno("asprintf");
6678 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6684 if (n == s->selected) {
6686 wstandout(view->window);
6687 s->selected_entry = te;
6689 tc = match_color(&s->colors, line);
6691 wattr_on(view->window,
6692 COLOR_PAIR(tc->colorpair), NULL);
6693 waddwstr(view->window, wline);
6695 wattr_off(view->window,
6696 COLOR_PAIR(tc->colorpair), NULL);
6697 if (width < view->ncols - 1)
6698 waddch(view->window, '\n');
6699 if (n == s->selected && view->focussed)
6700 wstandend(view->window);
6706 s->last_displayed_entry = te;
6715 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6717 struct got_tree_entry *te;
6718 int isroot = s->tree == s->root;
6721 if (s->first_displayed_entry == NULL)
6724 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6725 while (i++ < maxscroll) {
6728 s->first_displayed_entry = NULL;
6731 s->first_displayed_entry = te;
6732 te = got_tree_entry_get_prev(s->tree, te);
6736 static const struct got_error *
6737 tree_scroll_down(struct tog_view *view, int maxscroll)
6739 struct tog_tree_view_state *s = &view->state.tree;
6740 struct got_tree_entry *next, *last;
6743 if (s->first_displayed_entry)
6744 next = got_tree_entry_get_next(s->tree,
6745 s->first_displayed_entry);
6747 next = got_object_tree_get_first_entry(s->tree);
6749 last = s->last_displayed_entry;
6750 while (next && n++ < maxscroll) {
6752 s->last_displayed_entry = last;
6753 last = got_tree_entry_get_next(s->tree, last);
6755 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6756 s->first_displayed_entry = next;
6757 next = got_tree_entry_get_next(s->tree, next);
6764 static const struct got_error *
6765 tree_entry_path(char **path, struct tog_parent_trees *parents,
6766 struct got_tree_entry *te)
6768 const struct got_error *err = NULL;
6769 struct tog_parent_tree *pt;
6770 size_t len = 2; /* for leading slash and NUL */
6772 TAILQ_FOREACH(pt, parents, entry)
6773 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6776 len += strlen(got_tree_entry_get_name(te));
6778 *path = calloc(1, len);
6780 return got_error_from_errno("calloc");
6783 pt = TAILQ_LAST(parents, tog_parent_trees);
6785 const char *name = got_tree_entry_get_name(pt->selected_entry);
6786 if (strlcat(*path, name, len) >= len) {
6787 err = got_error(GOT_ERR_NO_SPACE);
6790 if (strlcat(*path, "/", len) >= len) {
6791 err = got_error(GOT_ERR_NO_SPACE);
6794 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6797 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6798 err = got_error(GOT_ERR_NO_SPACE);
6810 static const struct got_error *
6811 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6812 struct got_tree_entry *te, struct tog_parent_trees *parents,
6813 struct got_object_id *commit_id, struct got_repository *repo)
6815 const struct got_error *err = NULL;
6817 struct tog_view *blame_view;
6821 err = tree_entry_path(&path, parents, te);
6825 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6826 if (blame_view == NULL) {
6827 err = got_error_from_errno("view_open");
6831 err = open_blame_view(blame_view, path, commit_id, repo);
6833 if (err->code == GOT_ERR_CANCELLED)
6835 view_close(blame_view);
6837 *new_view = blame_view;
6843 static const struct got_error *
6844 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6845 struct tog_tree_view_state *s)
6847 struct tog_view *log_view;
6848 const struct got_error *err = NULL;
6853 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6854 if (log_view == NULL)
6855 return got_error_from_errno("view_open");
6857 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6861 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6864 view_close(log_view);
6866 *new_view = log_view;
6871 static const struct got_error *
6872 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6873 const char *head_ref_name, struct got_repository *repo)
6875 const struct got_error *err = NULL;
6876 char *commit_id_str = NULL;
6877 struct tog_tree_view_state *s = &view->state.tree;
6878 struct got_commit_object *commit = NULL;
6880 TAILQ_INIT(&s->parents);
6881 STAILQ_INIT(&s->colors);
6883 s->commit_id = got_object_id_dup(commit_id);
6884 if (s->commit_id == NULL)
6885 return got_error_from_errno("got_object_id_dup");
6887 err = got_object_open_as_commit(&commit, repo, commit_id);
6892 * The root is opened here and will be closed when the view is closed.
6893 * Any visited subtrees and their path-wise parents are opened and
6896 err = got_object_open_as_tree(&s->root, repo,
6897 got_object_commit_get_tree_id(commit));
6902 err = got_object_id_str(&commit_id_str, commit_id);
6906 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6907 err = got_error_from_errno("asprintf");
6911 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6912 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6913 if (head_ref_name) {
6914 s->head_ref_name = strdup(head_ref_name);
6915 if (s->head_ref_name == NULL) {
6916 err = got_error_from_errno("strdup");
6922 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6923 err = add_color(&s->colors, "\\$$",
6924 TOG_COLOR_TREE_SUBMODULE,
6925 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6928 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6929 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6932 err = add_color(&s->colors, "/$",
6933 TOG_COLOR_TREE_DIRECTORY,
6934 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6938 err = add_color(&s->colors, "\\*$",
6939 TOG_COLOR_TREE_EXECUTABLE,
6940 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6944 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6945 get_color_value("TOG_COLOR_COMMIT"));
6950 view->show = show_tree_view;
6951 view->input = input_tree_view;
6952 view->close = close_tree_view;
6953 view->search_start = search_start_tree_view;
6954 view->search_next = search_next_tree_view;
6956 free(commit_id_str);
6958 got_object_commit_close(commit);
6960 close_tree_view(view);
6964 static const struct got_error *
6965 close_tree_view(struct tog_view *view)
6967 struct tog_tree_view_state *s = &view->state.tree;
6969 free_colors(&s->colors);
6970 free(s->tree_label);
6971 s->tree_label = NULL;
6973 s->commit_id = NULL;
6974 free(s->head_ref_name);
6975 s->head_ref_name = NULL;
6976 while (!TAILQ_EMPTY(&s->parents)) {
6977 struct tog_parent_tree *parent;
6978 parent = TAILQ_FIRST(&s->parents);
6979 TAILQ_REMOVE(&s->parents, parent, entry);
6980 if (parent->tree != s->root)
6981 got_object_tree_close(parent->tree);
6985 if (s->tree != NULL && s->tree != s->root)
6986 got_object_tree_close(s->tree);
6988 got_object_tree_close(s->root);
6992 static const struct got_error *
6993 search_start_tree_view(struct tog_view *view)
6995 struct tog_tree_view_state *s = &view->state.tree;
6997 s->matched_entry = NULL;
7002 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7004 regmatch_t regmatch;
7006 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7010 static const struct got_error *
7011 search_next_tree_view(struct tog_view *view)
7013 struct tog_tree_view_state *s = &view->state.tree;
7014 struct got_tree_entry *te = NULL;
7016 if (!view->searching) {
7017 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7021 if (s->matched_entry) {
7022 if (view->searching == TOG_SEARCH_FORWARD) {
7023 if (s->selected_entry)
7024 te = got_tree_entry_get_next(s->tree,
7027 te = got_object_tree_get_first_entry(s->tree);
7029 if (s->selected_entry == NULL)
7030 te = got_object_tree_get_last_entry(s->tree);
7032 te = got_tree_entry_get_prev(s->tree,
7036 if (s->selected_entry)
7037 te = s->selected_entry;
7038 else if (view->searching == TOG_SEARCH_FORWARD)
7039 te = got_object_tree_get_first_entry(s->tree);
7041 te = got_object_tree_get_last_entry(s->tree);
7046 if (s->matched_entry == NULL) {
7047 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7050 if (view->searching == TOG_SEARCH_FORWARD)
7051 te = got_object_tree_get_first_entry(s->tree);
7053 te = got_object_tree_get_last_entry(s->tree);
7056 if (match_tree_entry(te, &view->regex)) {
7057 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7058 s->matched_entry = te;
7062 if (view->searching == TOG_SEARCH_FORWARD)
7063 te = got_tree_entry_get_next(s->tree, te);
7065 te = got_tree_entry_get_prev(s->tree, te);
7068 if (s->matched_entry) {
7069 s->first_displayed_entry = s->matched_entry;
7076 static const struct got_error *
7077 show_tree_view(struct tog_view *view)
7079 const struct got_error *err = NULL;
7080 struct tog_tree_view_state *s = &view->state.tree;
7083 err = tree_entry_path(&parent_path, &s->parents, NULL);
7087 err = draw_tree_entries(view, parent_path);
7094 static const struct got_error *
7095 tree_goto_line(struct tog_view *view, int nlines)
7097 const struct got_error *err = NULL;
7098 struct tog_tree_view_state *s = &view->state.tree;
7099 struct got_tree_entry **fte, **lte, **ste;
7100 int g, last, first = 1, i = 1;
7101 int root = s->tree == s->root;
7102 int off = root ? 1 : 2;
7109 else if (g > got_object_tree_get_nentries(s->tree))
7110 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7112 fte = &s->first_displayed_entry;
7113 lte = &s->last_displayed_entry;
7114 ste = &s->selected_entry;
7117 first = got_tree_entry_get_index(*fte);
7118 first += off; /* account for ".." */
7120 last = got_tree_entry_get_index(*lte);
7123 if (g >= first && g <= last && g - first < nlines) {
7124 s->selected = g - first;
7125 return NULL; /* gline is on the current page */
7129 i = got_tree_entry_get_index(*ste);
7134 err = tree_scroll_down(view, g - i);
7137 if (got_tree_entry_get_index(*lte) >=
7138 got_object_tree_get_nentries(s->tree) - 1 &&
7139 first + s->selected < g &&
7140 s->selected < s->ndisplayed - 1) {
7141 first = got_tree_entry_get_index(*fte);
7143 s->selected = g - first;
7146 tree_scroll_up(s, i - g);
7149 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7150 s->selected = g - 1;
7155 static const struct got_error *
7156 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7158 const struct got_error *err = NULL;
7159 struct tog_tree_view_state *s = &view->state.tree;
7160 struct got_tree_entry *te;
7161 int n, nscroll = view->nlines - 3;
7164 return tree_goto_line(view, nscroll);
7168 s->show_ids = !s->show_ids;
7173 if (!s->selected_entry)
7175 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7179 err = view_request_new(new_view, view, TOG_VIEW_REF);
7185 if (s->tree == s->root)
7186 s->first_displayed_entry =
7187 got_object_tree_get_first_entry(s->tree);
7189 s->first_displayed_entry = NULL;
7193 int eos = view->nlines - 3;
7195 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7199 te = got_object_tree_get_last_entry(s->tree);
7200 for (n = 0; n < eos; n++) {
7202 if (s->tree != s->root) {
7203 s->first_displayed_entry = NULL;
7208 s->first_displayed_entry = te;
7209 te = got_tree_entry_get_prev(s->tree, te);
7212 s->selected = n - 1;
7218 if (s->selected > 0) {
7222 tree_scroll_up(s, 1);
7223 if (s->selected_entry == NULL ||
7224 (s->tree == s->root && s->selected_entry ==
7225 got_object_tree_get_first_entry(s->tree)))
7235 if (s->tree == s->root) {
7236 if (got_object_tree_get_first_entry(s->tree) ==
7237 s->first_displayed_entry)
7238 s->selected -= MIN(s->selected, nscroll);
7240 if (s->first_displayed_entry == NULL)
7241 s->selected -= MIN(s->selected, nscroll);
7243 tree_scroll_up(s, MAX(0, nscroll));
7244 if (s->selected_entry == NULL ||
7245 (s->tree == s->root && s->selected_entry ==
7246 got_object_tree_get_first_entry(s->tree)))
7252 if (s->selected < s->ndisplayed - 1) {
7256 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7258 /* can't scroll any further */
7262 tree_scroll_down(view, 1);
7272 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7274 /* can't scroll any further; move cursor down */
7275 if (s->selected < s->ndisplayed - 1)
7276 s->selected += MIN(nscroll,
7277 s->ndisplayed - s->selected - 1);
7282 tree_scroll_down(view, nscroll);
7287 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7288 struct tog_parent_tree *parent;
7289 /* user selected '..' */
7290 if (s->tree == s->root) {
7294 parent = TAILQ_FIRST(&s->parents);
7295 TAILQ_REMOVE(&s->parents, parent,
7297 got_object_tree_close(s->tree);
7298 s->tree = parent->tree;
7299 s->first_displayed_entry =
7300 parent->first_displayed_entry;
7302 parent->selected_entry;
7303 s->selected = parent->selected;
7304 if (s->selected > view->nlines - 3) {
7305 err = offset_selection_down(view);
7310 } else if (S_ISDIR(got_tree_entry_get_mode(
7311 s->selected_entry))) {
7312 struct got_tree_object *subtree;
7314 err = got_object_open_as_tree(&subtree, s->repo,
7315 got_tree_entry_get_id(s->selected_entry));
7318 err = tree_view_visit_subtree(s, subtree);
7320 got_object_tree_close(subtree);
7323 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7324 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7327 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7328 s->selected = view->nlines - 4;
7344 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7349 static const struct got_error *
7350 cmd_tree(int argc, char *argv[])
7352 const struct got_error *error;
7353 struct got_repository *repo = NULL;
7354 struct got_worktree *worktree = NULL;
7355 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7356 struct got_object_id *commit_id = NULL;
7357 struct got_commit_object *commit = NULL;
7358 const char *commit_id_arg = NULL;
7360 struct got_reference *ref = NULL;
7361 const char *head_ref_name = NULL;
7363 struct tog_view *view;
7364 int *pack_fds = NULL;
7366 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7369 commit_id_arg = optarg;
7372 repo_path = realpath(optarg, NULL);
7373 if (repo_path == NULL)
7374 return got_error_from_errno2("realpath",
7389 error = got_repo_pack_fds_open(&pack_fds);
7393 if (repo_path == NULL) {
7394 cwd = getcwd(NULL, 0);
7396 return got_error_from_errno("getcwd");
7397 error = got_worktree_open(&worktree, cwd);
7398 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7402 strdup(got_worktree_get_repo_path(worktree));
7404 repo_path = strdup(cwd);
7405 if (repo_path == NULL) {
7406 error = got_error_from_errno("strdup");
7411 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7415 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7422 error = apply_unveil(got_repo_get_path(repo), NULL);
7426 error = tog_load_refs(repo, 0);
7430 if (commit_id_arg == NULL) {
7431 error = got_repo_match_object_id(&commit_id, &label,
7432 worktree ? got_worktree_get_head_ref_name(worktree) :
7433 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7436 head_ref_name = label;
7438 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7440 head_ref_name = got_ref_get_name(ref);
7441 else if (error->code != GOT_ERR_NOT_REF)
7443 error = got_repo_match_object_id(&commit_id, NULL,
7444 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7449 error = got_object_open_as_commit(&commit, repo, commit_id);
7453 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7455 error = got_error_from_errno("view_open");
7458 error = open_tree_view(view, commit_id, head_ref_name, repo);
7461 if (!got_path_is_root_dir(in_repo_path)) {
7462 error = tree_view_walk_path(&view->state.tree, commit,
7469 /* Release work tree lock. */
7470 got_worktree_close(worktree);
7473 error = view_loop(view);
7482 const struct got_error *close_err = got_repo_close(repo);
7487 const struct got_error *pack_err =
7488 got_repo_pack_fds_close(pack_fds);
7496 static const struct got_error *
7497 ref_view_load_refs(struct tog_ref_view_state *s)
7499 struct got_reflist_entry *sre;
7500 struct tog_reflist_entry *re;
7503 TAILQ_FOREACH(sre, &tog_refs, entry) {
7504 if (strncmp(got_ref_get_name(sre->ref),
7505 "refs/got/", 9) == 0 &&
7506 strncmp(got_ref_get_name(sre->ref),
7507 "refs/got/backup/", 16) != 0)
7510 re = malloc(sizeof(*re));
7512 return got_error_from_errno("malloc");
7514 re->ref = got_ref_dup(sre->ref);
7515 if (re->ref == NULL)
7516 return got_error_from_errno("got_ref_dup");
7517 re->idx = s->nrefs++;
7518 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7521 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7526 ref_view_free_refs(struct tog_ref_view_state *s)
7528 struct tog_reflist_entry *re;
7530 while (!TAILQ_EMPTY(&s->refs)) {
7531 re = TAILQ_FIRST(&s->refs);
7532 TAILQ_REMOVE(&s->refs, re, entry);
7533 got_ref_close(re->ref);
7538 static const struct got_error *
7539 open_ref_view(struct tog_view *view, struct got_repository *repo)
7541 const struct got_error *err = NULL;
7542 struct tog_ref_view_state *s = &view->state.ref;
7544 s->selected_entry = 0;
7547 TAILQ_INIT(&s->refs);
7548 STAILQ_INIT(&s->colors);
7550 err = ref_view_load_refs(s);
7554 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7555 err = add_color(&s->colors, "^refs/heads/",
7556 TOG_COLOR_REFS_HEADS,
7557 get_color_value("TOG_COLOR_REFS_HEADS"));
7561 err = add_color(&s->colors, "^refs/tags/",
7562 TOG_COLOR_REFS_TAGS,
7563 get_color_value("TOG_COLOR_REFS_TAGS"));
7567 err = add_color(&s->colors, "^refs/remotes/",
7568 TOG_COLOR_REFS_REMOTES,
7569 get_color_value("TOG_COLOR_REFS_REMOTES"));
7573 err = add_color(&s->colors, "^refs/got/backup/",
7574 TOG_COLOR_REFS_BACKUP,
7575 get_color_value("TOG_COLOR_REFS_BACKUP"));
7580 view->show = show_ref_view;
7581 view->input = input_ref_view;
7582 view->close = close_ref_view;
7583 view->search_start = search_start_ref_view;
7584 view->search_next = search_next_ref_view;
7587 free_colors(&s->colors);
7591 static const struct got_error *
7592 close_ref_view(struct tog_view *view)
7594 struct tog_ref_view_state *s = &view->state.ref;
7596 ref_view_free_refs(s);
7597 free_colors(&s->colors);
7602 static const struct got_error *
7603 resolve_reflist_entry(struct got_object_id **commit_id,
7604 struct tog_reflist_entry *re, struct got_repository *repo)
7606 const struct got_error *err = NULL;
7607 struct got_object_id *obj_id;
7608 struct got_tag_object *tag = NULL;
7613 err = got_ref_resolve(&obj_id, repo, re->ref);
7617 err = got_object_get_type(&obj_type, repo, obj_id);
7622 case GOT_OBJ_TYPE_COMMIT:
7623 *commit_id = obj_id;
7625 case GOT_OBJ_TYPE_TAG:
7626 err = got_object_open_as_tag(&tag, repo, obj_id);
7630 err = got_object_get_type(&obj_type, repo,
7631 got_object_tag_get_object_id(tag));
7634 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7635 err = got_error(GOT_ERR_OBJ_TYPE);
7638 *commit_id = got_object_id_dup(
7639 got_object_tag_get_object_id(tag));
7640 if (*commit_id == NULL) {
7641 err = got_error_from_errno("got_object_id_dup");
7646 err = got_error(GOT_ERR_OBJ_TYPE);
7652 got_object_tag_close(tag);
7660 static const struct got_error *
7661 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7662 struct tog_reflist_entry *re, struct got_repository *repo)
7664 struct tog_view *log_view;
7665 const struct got_error *err = NULL;
7666 struct got_object_id *commit_id = NULL;
7670 err = resolve_reflist_entry(&commit_id, re, repo);
7672 if (err->code != GOT_ERR_OBJ_TYPE)
7678 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7679 if (log_view == NULL) {
7680 err = got_error_from_errno("view_open");
7684 err = open_log_view(log_view, commit_id, repo,
7685 got_ref_get_name(re->ref), "", 0);
7688 view_close(log_view);
7690 *new_view = log_view;
7696 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7698 struct tog_reflist_entry *re;
7701 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7704 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7705 while (i++ < maxscroll) {
7708 s->first_displayed_entry = re;
7709 re = TAILQ_PREV(re, tog_reflist_head, entry);
7713 static const struct got_error *
7714 ref_scroll_down(struct tog_view *view, int maxscroll)
7716 struct tog_ref_view_state *s = &view->state.ref;
7717 struct tog_reflist_entry *next, *last;
7720 if (s->first_displayed_entry)
7721 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7723 next = TAILQ_FIRST(&s->refs);
7725 last = s->last_displayed_entry;
7726 while (next && n++ < maxscroll) {
7728 s->last_displayed_entry = last;
7729 last = TAILQ_NEXT(last, entry);
7731 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7732 s->first_displayed_entry = next;
7733 next = TAILQ_NEXT(next, entry);
7740 static const struct got_error *
7741 search_start_ref_view(struct tog_view *view)
7743 struct tog_ref_view_state *s = &view->state.ref;
7745 s->matched_entry = NULL;
7750 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7752 regmatch_t regmatch;
7754 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7758 static const struct got_error *
7759 search_next_ref_view(struct tog_view *view)
7761 struct tog_ref_view_state *s = &view->state.ref;
7762 struct tog_reflist_entry *re = NULL;
7764 if (!view->searching) {
7765 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7769 if (s->matched_entry) {
7770 if (view->searching == TOG_SEARCH_FORWARD) {
7771 if (s->selected_entry)
7772 re = TAILQ_NEXT(s->selected_entry, entry);
7774 re = TAILQ_PREV(s->selected_entry,
7775 tog_reflist_head, entry);
7777 if (s->selected_entry == NULL)
7778 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7780 re = TAILQ_PREV(s->selected_entry,
7781 tog_reflist_head, entry);
7784 if (s->selected_entry)
7785 re = s->selected_entry;
7786 else if (view->searching == TOG_SEARCH_FORWARD)
7787 re = TAILQ_FIRST(&s->refs);
7789 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7794 if (s->matched_entry == NULL) {
7795 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7798 if (view->searching == TOG_SEARCH_FORWARD)
7799 re = TAILQ_FIRST(&s->refs);
7801 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7804 if (match_reflist_entry(re, &view->regex)) {
7805 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7806 s->matched_entry = re;
7810 if (view->searching == TOG_SEARCH_FORWARD)
7811 re = TAILQ_NEXT(re, entry);
7813 re = TAILQ_PREV(re, tog_reflist_head, entry);
7816 if (s->matched_entry) {
7817 s->first_displayed_entry = s->matched_entry;
7824 static const struct got_error *
7825 show_ref_view(struct tog_view *view)
7827 const struct got_error *err = NULL;
7828 struct tog_ref_view_state *s = &view->state.ref;
7829 struct tog_reflist_entry *re;
7832 struct tog_color *tc;
7834 int limit = view->nlines;
7836 werase(view->window);
7839 if (view_is_hsplit_top(view))
7840 --limit; /* border */
7845 re = s->first_displayed_entry;
7847 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7849 return got_error_from_errno("asprintf");
7851 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7856 if (view_needs_focus_indication(view))
7857 wstandout(view->window);
7858 waddwstr(view->window, wline);
7859 while (width++ < view->ncols)
7860 waddch(view->window, ' ');
7861 if (view_needs_focus_indication(view))
7862 wstandend(view->window);
7871 while (re && limit > 0) {
7873 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7876 struct got_commit_object *ci;
7877 struct got_tag_object *tag;
7878 struct got_object_id *id;
7882 err = got_ref_resolve(&id, s->repo, re->ref);
7885 err = got_object_open_as_tag(&tag, s->repo, id);
7887 if (err->code != GOT_ERR_OBJ_TYPE) {
7891 err = got_object_open_as_commit(&ci, s->repo,
7897 t = got_object_commit_get_committer_time(ci);
7898 got_object_commit_close(ci);
7900 t = got_object_tag_get_tagger_time(tag);
7901 got_object_tag_close(tag);
7904 if (gmtime_r(&t, &tm) == NULL)
7905 return got_error_from_errno("gmtime_r");
7906 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7907 return got_error(GOT_ERR_NO_SPACE);
7909 if (got_ref_is_symbolic(re->ref)) {
7910 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7911 ymd : "", got_ref_get_name(re->ref),
7912 got_ref_get_symref_target(re->ref)) == -1)
7913 return got_error_from_errno("asprintf");
7914 } else if (s->show_ids) {
7915 struct got_object_id *id;
7917 err = got_ref_resolve(&id, s->repo, re->ref);
7920 err = got_object_id_str(&id_str, id);
7925 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7926 got_ref_get_name(re->ref), id_str) == -1) {
7927 err = got_error_from_errno("asprintf");
7934 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7935 got_ref_get_name(re->ref)) == -1)
7936 return got_error_from_errno("asprintf");
7938 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7944 if (n == s->selected) {
7946 wstandout(view->window);
7947 s->selected_entry = re;
7949 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7951 wattr_on(view->window,
7952 COLOR_PAIR(tc->colorpair), NULL);
7953 waddwstr(view->window, wline);
7955 wattr_off(view->window,
7956 COLOR_PAIR(tc->colorpair), NULL);
7957 if (width < view->ncols - 1)
7958 waddch(view->window, '\n');
7959 if (n == s->selected && view->focussed)
7960 wstandend(view->window);
7966 s->last_displayed_entry = re;
7969 re = TAILQ_NEXT(re, entry);
7976 static const struct got_error *
7977 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7978 struct tog_reflist_entry *re, struct got_repository *repo)
7980 const struct got_error *err = NULL;
7981 struct got_object_id *commit_id = NULL;
7982 struct tog_view *tree_view;
7986 err = resolve_reflist_entry(&commit_id, re, repo);
7988 if (err->code != GOT_ERR_OBJ_TYPE)
7995 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7996 if (tree_view == NULL) {
7997 err = got_error_from_errno("view_open");
8001 err = open_tree_view(tree_view, commit_id,
8002 got_ref_get_name(re->ref), repo);
8006 *new_view = tree_view;
8012 static const struct got_error *
8013 ref_goto_line(struct tog_view *view, int nlines)
8015 const struct got_error *err = NULL;
8016 struct tog_ref_view_state *s = &view->state.ref;
8017 int g, idx = s->selected_entry->idx;
8024 else if (g > s->nrefs)
8027 if (g >= s->first_displayed_entry->idx + 1 &&
8028 g <= s->last_displayed_entry->idx + 1 &&
8029 g - s->first_displayed_entry->idx - 1 < nlines) {
8030 s->selected = g - s->first_displayed_entry->idx - 1;
8035 err = ref_scroll_down(view, g - idx - 1);
8038 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8039 s->first_displayed_entry->idx + s->selected < g &&
8040 s->selected < s->ndisplayed - 1)
8041 s->selected = g - s->first_displayed_entry->idx - 1;
8042 } else if (idx + 1 > g)
8043 ref_scroll_up(s, idx - g + 1);
8045 if (g < nlines && s->first_displayed_entry->idx == 0)
8046 s->selected = g - 1;
8052 static const struct got_error *
8053 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8055 const struct got_error *err = NULL;
8056 struct tog_ref_view_state *s = &view->state.ref;
8057 struct tog_reflist_entry *re;
8058 int n, nscroll = view->nlines - 1;
8061 return ref_goto_line(view, nscroll);
8065 s->show_ids = !s->show_ids;
8069 s->show_date = !s->show_date;
8073 s->sort_by_date = !s->sort_by_date;
8075 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8076 got_ref_cmp_by_commit_timestamp_descending :
8077 tog_ref_cmp_by_name, s->repo);
8080 got_reflist_object_id_map_free(tog_refs_idmap);
8081 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8082 &tog_refs, s->repo);
8085 ref_view_free_refs(s);
8086 err = ref_view_load_refs(s);
8091 if (!s->selected_entry)
8093 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8097 if (!s->selected_entry)
8099 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8105 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8109 int eos = view->nlines - 1;
8111 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8115 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8116 for (n = 0; n < eos; n++) {
8119 s->first_displayed_entry = re;
8120 re = TAILQ_PREV(re, tog_reflist_head, entry);
8123 s->selected = n - 1;
8129 if (s->selected > 0) {
8133 ref_scroll_up(s, 1);
8134 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8144 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8145 s->selected -= MIN(nscroll, s->selected);
8146 ref_scroll_up(s, MAX(0, nscroll));
8147 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8153 if (s->selected < s->ndisplayed - 1) {
8157 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8158 /* can't scroll any further */
8162 ref_scroll_down(view, 1);
8172 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8173 /* can't scroll any further; move cursor down */
8174 if (s->selected < s->ndisplayed - 1)
8175 s->selected += MIN(nscroll,
8176 s->ndisplayed - s->selected - 1);
8177 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8178 s->selected += s->ndisplayed - s->selected - 1;
8182 ref_scroll_down(view, nscroll);
8187 err = tog_load_refs(s->repo, s->sort_by_date);
8190 ref_view_free_refs(s);
8191 err = ref_view_load_refs(s);
8194 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8195 s->selected = view->nlines - 2;
8209 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8214 static const struct got_error *
8215 cmd_ref(int argc, char *argv[])
8217 const struct got_error *error;
8218 struct got_repository *repo = NULL;
8219 struct got_worktree *worktree = NULL;
8220 char *cwd = NULL, *repo_path = NULL;
8222 struct tog_view *view;
8223 int *pack_fds = NULL;
8225 while ((ch = getopt(argc, argv, "r:")) != -1) {
8228 repo_path = realpath(optarg, NULL);
8229 if (repo_path == NULL)
8230 return got_error_from_errno2("realpath",
8245 error = got_repo_pack_fds_open(&pack_fds);
8249 if (repo_path == NULL) {
8250 cwd = getcwd(NULL, 0);
8252 return got_error_from_errno("getcwd");
8253 error = got_worktree_open(&worktree, cwd);
8254 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8258 strdup(got_worktree_get_repo_path(worktree));
8260 repo_path = strdup(cwd);
8261 if (repo_path == NULL) {
8262 error = got_error_from_errno("strdup");
8267 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8273 error = apply_unveil(got_repo_get_path(repo), NULL);
8277 error = tog_load_refs(repo, 0);
8281 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8283 error = got_error_from_errno("view_open");
8287 error = open_ref_view(view, repo);
8292 /* Release work tree lock. */
8293 got_worktree_close(worktree);
8296 error = view_loop(view);
8301 const struct got_error *close_err = got_repo_close(repo);
8306 const struct got_error *pack_err =
8307 got_repo_pack_fds_close(pack_fds);
8315 static const struct got_error *
8316 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8317 enum tog_view_type request, int y, int x)
8319 const struct got_error *err = NULL;
8325 if (view->type == TOG_VIEW_LOG) {
8326 struct tog_log_view_state *s = &view->state.log;
8328 err = open_diff_view_for_commit(new_view, y, x,
8329 s->selected_entry->commit, s->selected_entry->id,
8332 return got_error_msg(GOT_ERR_NOT_IMPL,
8333 "parent/child view pair not supported");
8335 case TOG_VIEW_BLAME:
8336 if (view->type == TOG_VIEW_TREE) {
8337 struct tog_tree_view_state *s = &view->state.tree;
8339 err = blame_tree_entry(new_view, y, x,
8340 s->selected_entry, &s->parents, s->commit_id,
8343 return got_error_msg(GOT_ERR_NOT_IMPL,
8344 "parent/child view pair not supported");
8347 if (view->type == TOG_VIEW_BLAME)
8348 err = log_annotated_line(new_view, y, x,
8349 view->state.blame.repo, view->state.blame.id_to_log);
8350 else if (view->type == TOG_VIEW_TREE)
8351 err = log_selected_tree_entry(new_view, y, x,
8353 else if (view->type == TOG_VIEW_REF)
8354 err = log_ref_entry(new_view, y, x,
8355 view->state.ref.selected_entry,
8356 view->state.ref.repo);
8358 return got_error_msg(GOT_ERR_NOT_IMPL,
8359 "parent/child view pair not supported");
8362 if (view->type == TOG_VIEW_LOG)
8363 err = browse_commit_tree(new_view, y, x,
8364 view->state.log.selected_entry,
8365 view->state.log.in_repo_path,
8366 view->state.log.head_ref_name,
8367 view->state.log.repo);
8368 else if (view->type == TOG_VIEW_REF)
8369 err = browse_ref_tree(new_view, y, x,
8370 view->state.ref.selected_entry,
8371 view->state.ref.repo);
8373 return got_error_msg(GOT_ERR_NOT_IMPL,
8374 "parent/child view pair not supported");
8377 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8378 if (*new_view == NULL)
8379 return got_error_from_errno("view_open");
8380 if (view->type == TOG_VIEW_LOG)
8381 err = open_ref_view(*new_view, view->state.log.repo);
8382 else if (view->type == TOG_VIEW_TREE)
8383 err = open_ref_view(*new_view, view->state.tree.repo);
8385 err = got_error_msg(GOT_ERR_NOT_IMPL,
8386 "parent/child view pair not supported");
8388 view_close(*new_view);
8391 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
8398 * If view was scrolled down to move the selected line into view when opening a
8399 * horizontal split, scroll back up when closing the split/toggling fullscreen.
8402 offset_selection_up(struct tog_view *view)
8404 switch (view->type) {
8405 case TOG_VIEW_BLAME: {
8406 struct tog_blame_view_state *s = &view->state.blame;
8407 if (s->first_displayed_line == 1) {
8408 s->selected_line = MAX(s->selected_line - view->offset,
8412 if (s->first_displayed_line > view->offset)
8413 s->first_displayed_line -= view->offset;
8415 s->first_displayed_line = 1;
8416 s->selected_line += view->offset;
8420 log_scroll_up(&view->state.log, view->offset);
8421 view->state.log.selected += view->offset;
8424 ref_scroll_up(&view->state.ref, view->offset);
8425 view->state.ref.selected += view->offset;
8428 tree_scroll_up(&view->state.tree, view->offset);
8429 view->state.tree.selected += view->offset;
8439 * If the selected line is in the section of screen covered by the bottom split,
8440 * scroll down offset lines to move it into view and index its new position.
8442 static const struct got_error *
8443 offset_selection_down(struct tog_view *view)
8445 const struct got_error *err = NULL;
8446 const struct got_error *(*scrolld)(struct tog_view *, int);
8447 int *selected = NULL;
8450 switch (view->type) {
8451 case TOG_VIEW_BLAME: {
8452 struct tog_blame_view_state *s = &view->state.blame;
8455 if (s->selected_line > view->nlines - header) {
8456 offset = abs(view->nlines - s->selected_line - header);
8457 s->first_displayed_line += offset;
8458 s->selected_line -= offset;
8459 view->offset = offset;
8463 case TOG_VIEW_LOG: {
8464 struct tog_log_view_state *s = &view->state.log;
8465 scrolld = &log_scroll_down;
8466 header = view_is_parent_view(view) ? 3 : 2;
8467 selected = &s->selected;
8470 case TOG_VIEW_REF: {
8471 struct tog_ref_view_state *s = &view->state.ref;
8472 scrolld = &ref_scroll_down;
8474 selected = &s->selected;
8477 case TOG_VIEW_TREE: {
8478 struct tog_tree_view_state *s = &view->state.tree;
8479 scrolld = &tree_scroll_down;
8481 selected = &s->selected;
8491 if (selected && *selected > view->nlines - header) {
8492 offset = abs(view->nlines - *selected - header);
8493 view->offset = offset;
8494 if (scrolld && offset) {
8495 err = scrolld(view, offset);
8496 *selected -= offset;
8504 list_commands(FILE *fp)
8508 fprintf(fp, "commands:");
8509 for (i = 0; i < nitems(tog_commands); i++) {
8510 const struct tog_cmd *cmd = &tog_commands[i];
8511 fprintf(fp, " %s", cmd->name);
8517 usage(int hflag, int status)
8519 FILE *fp = (status == 0) ? stdout : stderr;
8521 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8524 fprintf(fp, "lazy usage: %s path\n", getprogname());
8531 make_argv(int argc, ...)
8539 argv = calloc(argc, sizeof(char *));
8542 for (i = 0; i < argc; i++) {
8543 argv[i] = strdup(va_arg(ap, char *));
8544 if (argv[i] == NULL)
8553 * Try to convert 'tog path' into a 'tog log path' command.
8554 * The user could simply have mistyped the command rather than knowingly
8555 * provided a path. So check whether argv[0] can in fact be resolved
8556 * to a path in the HEAD commit and print a special error if not.
8557 * This hack is for mpi@ <3
8559 static const struct got_error *
8560 tog_log_with_path(int argc, char *argv[])
8562 const struct got_error *error = NULL, *close_err;
8563 const struct tog_cmd *cmd = NULL;
8564 struct got_repository *repo = NULL;
8565 struct got_worktree *worktree = NULL;
8566 struct got_object_id *commit_id = NULL, *id = NULL;
8567 struct got_commit_object *commit = NULL;
8568 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8569 char *commit_id_str = NULL, **cmd_argv = NULL;
8570 int *pack_fds = NULL;
8572 cwd = getcwd(NULL, 0);
8574 return got_error_from_errno("getcwd");
8576 error = got_repo_pack_fds_open(&pack_fds);
8580 error = got_worktree_open(&worktree, cwd);
8581 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8585 repo_path = strdup(got_worktree_get_repo_path(worktree));
8587 repo_path = strdup(cwd);
8588 if (repo_path == NULL) {
8589 error = got_error_from_errno("strdup");
8593 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8597 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8602 error = tog_load_refs(repo, 0);
8605 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8606 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8607 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8612 got_worktree_close(worktree);
8616 error = got_object_open_as_commit(&commit, repo, commit_id);
8620 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8622 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8624 fprintf(stderr, "%s: '%s' is no known command or path\n",
8625 getprogname(), argv[0]);
8630 error = got_object_id_str(&commit_id_str, commit_id);
8634 cmd = &tog_commands[0]; /* log */
8636 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8637 error = cmd->cmd_main(argc, cmd_argv);
8640 close_err = got_repo_close(repo);
8645 got_object_commit_close(commit);
8647 got_worktree_close(worktree);
8649 const struct got_error *pack_err =
8650 got_repo_pack_fds_close(pack_fds);
8655 free(commit_id_str);
8662 for (i = 0; i < argc; i++)
8671 main(int argc, char *argv[])
8673 const struct got_error *error = NULL;
8674 const struct tog_cmd *cmd = NULL;
8675 int ch, hflag = 0, Vflag = 0;
8676 char **cmd_argv = NULL;
8677 static const struct option longopts[] = {
8678 { "version", no_argument, NULL, 'V' },
8681 char *diff_algo_str = NULL;
8683 if (!isatty(STDIN_FILENO))
8684 errx(1, "standard input is not a tty");
8686 setlocale(LC_CTYPE, "");
8688 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8708 got_version_print_str();
8713 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8721 /* Build an argument vector which runs a default command. */
8722 cmd = &tog_commands[0];
8724 cmd_argv = make_argv(argc, cmd->name);
8728 /* Did the user specify a command? */
8729 for (i = 0; i < nitems(tog_commands); i++) {
8730 if (strncmp(tog_commands[i].name, argv[0],
8731 strlen(argv[0])) == 0) {
8732 cmd = &tog_commands[i];
8738 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8739 if (diff_algo_str) {
8740 if (strcasecmp(diff_algo_str, "patience") == 0)
8741 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8742 if (strcasecmp(diff_algo_str, "myers") == 0)
8743 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8749 /* No command specified; try log with a path */
8750 error = tog_log_with_path(argc, argv);
8755 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8762 for (i = 0; i < argc; i++)
8767 if (error && error->code != GOT_ERR_CANCELLED)
8768 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);