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) {
3120 * If the user has moved the cursor after we hit a match,
3121 * the position from where we should continue searching
3122 * might have changed.
3124 if (view->searching == TOG_SEARCH_FORWARD)
3125 entry = TAILQ_NEXT(s->selected_entry, entry);
3127 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3130 entry = s->selected_entry;
3136 if (entry == NULL) {
3137 if (s->thread_args.log_complete ||
3138 view->searching == TOG_SEARCH_BACKWARD) {
3139 view->search_next_done =
3140 (s->matched_entry == NULL ?
3141 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3142 s->search_entry = NULL;
3146 * Poke the log thread for more commits and return,
3147 * allowing the main loop to make progress. Search
3148 * will resume at s->search_entry once we come back.
3150 s->thread_args.commits_needed++;
3151 return trigger_log_thread(view, 0);
3154 err = match_commit(&have_match, entry->id, entry->commit,
3159 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3160 s->matched_entry = entry;
3164 s->search_entry = entry;
3165 if (view->searching == TOG_SEARCH_FORWARD)
3166 entry = TAILQ_NEXT(entry, entry);
3168 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3171 if (s->matched_entry) {
3172 int cur = s->selected_entry->idx;
3173 while (cur < s->matched_entry->idx) {
3174 err = input_log_view(NULL, view, KEY_DOWN);
3179 while (cur > s->matched_entry->idx) {
3180 err = input_log_view(NULL, view, KEY_UP);
3187 s->search_entry = NULL;
3192 static const struct got_error *
3193 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3194 struct got_repository *repo, const char *head_ref_name,
3195 const char *in_repo_path, int log_branches)
3197 const struct got_error *err = NULL;
3198 struct tog_log_view_state *s = &view->state.log;
3199 struct got_repository *thread_repo = NULL;
3200 struct got_commit_graph *thread_graph = NULL;
3203 if (in_repo_path != s->in_repo_path) {
3204 free(s->in_repo_path);
3205 s->in_repo_path = strdup(in_repo_path);
3206 if (s->in_repo_path == NULL)
3207 return got_error_from_errno("strdup");
3210 /* The commit queue only contains commits being displayed. */
3211 TAILQ_INIT(&s->real_commits.head);
3212 s->real_commits.ncommits = 0;
3213 s->commits = &s->real_commits;
3215 TAILQ_INIT(&s->limit_commits.head);
3217 s->limit_commits.ncommits = 0;
3220 if (head_ref_name) {
3221 s->head_ref_name = strdup(head_ref_name);
3222 if (s->head_ref_name == NULL) {
3223 err = got_error_from_errno("strdup");
3227 s->start_id = got_object_id_dup(start_id);
3228 if (s->start_id == NULL) {
3229 err = got_error_from_errno("got_object_id_dup");
3232 s->log_branches = log_branches;
3234 STAILQ_INIT(&s->colors);
3235 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3236 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3237 get_color_value("TOG_COLOR_COMMIT"));
3240 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3241 get_color_value("TOG_COLOR_AUTHOR"));
3243 free_colors(&s->colors);
3246 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3247 get_color_value("TOG_COLOR_DATE"));
3249 free_colors(&s->colors);
3254 view->show = show_log_view;
3255 view->input = input_log_view;
3256 view->resize = resize_log_view;
3257 view->close = close_log_view;
3258 view->search_start = search_start_log_view;
3259 view->search_next = search_next_log_view;
3261 if (s->thread_args.pack_fds == NULL) {
3262 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3266 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3267 s->thread_args.pack_fds);
3270 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3274 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3275 s->repo, NULL, NULL);
3279 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3281 err = got_error_set_errno(errcode, "pthread_cond_init");
3284 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3286 err = got_error_set_errno(errcode, "pthread_cond_init");
3290 s->thread_args.commits_needed = view->nlines;
3291 s->thread_args.graph = thread_graph;
3292 s->thread_args.real_commits = &s->real_commits;
3293 s->thread_args.limit_commits = &s->limit_commits;
3294 s->thread_args.in_repo_path = s->in_repo_path;
3295 s->thread_args.start_id = s->start_id;
3296 s->thread_args.repo = thread_repo;
3297 s->thread_args.log_complete = 0;
3298 s->thread_args.quit = &s->quit;
3299 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3300 s->thread_args.selected_entry = &s->selected_entry;
3301 s->thread_args.searching = &view->searching;
3302 s->thread_args.search_next_done = &view->search_next_done;
3303 s->thread_args.regex = &view->regex;
3304 s->thread_args.limiting = &s->limit_view;
3305 s->thread_args.limit_regex = &s->limit_regex;
3306 s->thread_args.limit_commits = &s->limit_commits;
3309 close_log_view(view);
3313 static const struct got_error *
3314 show_log_view(struct tog_view *view)
3316 const struct got_error *err;
3317 struct tog_log_view_state *s = &view->state.log;
3319 if (s->thread == NULL) {
3320 int errcode = pthread_create(&s->thread, NULL, log_thread,
3323 return got_error_set_errno(errcode, "pthread_create");
3324 if (s->thread_args.commits_needed > 0) {
3325 err = trigger_log_thread(view, 1);
3331 return draw_commits(view);
3335 log_move_cursor_up(struct tog_view *view, int page, int home)
3337 struct tog_log_view_state *s = &view->state.log;
3339 if (s->first_displayed_entry == NULL)
3341 if (s->selected_entry->idx == 0)
3344 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3346 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3348 if (!page && !home && s->selected > 0)
3351 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3357 static const struct got_error *
3358 log_move_cursor_down(struct tog_view *view, int page)
3360 struct tog_log_view_state *s = &view->state.log;
3361 const struct got_error *err = NULL;
3362 int eos = view->nlines - 2;
3364 if (s->first_displayed_entry == NULL)
3367 if (s->thread_args.log_complete &&
3368 s->selected_entry->idx >= s->commits->ncommits - 1)
3371 if (view_is_hsplit_top(view))
3372 --eos; /* border consumes the last line */
3375 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3378 err = log_scroll_down(view, 1);
3379 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3380 struct commit_queue_entry *entry;
3384 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3385 s->last_displayed_entry = entry;
3386 for (n = 0; n <= eos; n++) {
3389 s->first_displayed_entry = entry;
3390 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3393 s->selected = n - 1;
3395 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3396 s->thread_args.log_complete)
3397 s->selected += MIN(page,
3398 s->commits->ncommits - s->selected_entry->idx - 1);
3400 err = log_scroll_down(view, page);
3406 * We might necessarily overshoot in horizontal
3407 * splits; if so, select the last displayed commit.
3409 if (s->first_displayed_entry && s->last_displayed_entry) {
3410 s->selected = MIN(s->selected,
3411 s->last_displayed_entry->idx -
3412 s->first_displayed_entry->idx);
3417 if (s->thread_args.log_complete &&
3418 s->selected_entry->idx == s->commits->ncommits - 1)
3425 view_get_split(struct tog_view *view, int *y, int *x)
3430 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3431 if (view->child && view->child->resized_y)
3432 *y = view->child->resized_y;
3433 else if (view->resized_y)
3434 *y = view->resized_y;
3436 *y = view_split_begin_y(view->lines);
3437 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3438 if (view->child && view->child->resized_x)
3439 *x = view->child->resized_x;
3440 else if (view->resized_x)
3441 *x = view->resized_x;
3443 *x = view_split_begin_x(view->begin_x);
3447 /* Split view horizontally at y and offset view->state->selected line. */
3448 static const struct got_error *
3449 view_init_hsplit(struct tog_view *view, int y)
3451 const struct got_error *err = NULL;
3455 err = view_resize(view);
3459 err = offset_selection_down(view);
3464 static const struct got_error *
3465 log_goto_line(struct tog_view *view, int nlines)
3467 const struct got_error *err = NULL;
3468 struct tog_log_view_state *s = &view->state.log;
3469 int g, idx = s->selected_entry->idx;
3471 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3477 if (g >= s->first_displayed_entry->idx + 1 &&
3478 g <= s->last_displayed_entry->idx + 1 &&
3479 g - s->first_displayed_entry->idx - 1 < nlines) {
3480 s->selected = g - s->first_displayed_entry->idx - 1;
3486 err = log_move_cursor_down(view, g - idx - 1);
3487 if (!err && g > s->selected_entry->idx + 1)
3488 err = log_move_cursor_down(view,
3489 g - s->first_displayed_entry->idx - 1);
3492 } else if (idx + 1 > g)
3493 log_move_cursor_up(view, idx - g + 1, 0);
3495 if (g < nlines && s->first_displayed_entry->idx == 0)
3496 s->selected = g - 1;
3503 static const struct got_error *
3504 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3506 const struct got_error *err = NULL;
3507 struct tog_log_view_state *s = &view->state.log;
3510 if (s->thread_args.load_all) {
3511 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3512 s->thread_args.load_all = 0;
3513 else if (s->thread_args.log_complete) {
3514 err = log_move_cursor_down(view, s->commits->ncommits);
3515 s->thread_args.load_all = 0;
3521 eos = nscroll = view->nlines - 1;
3522 if (view_is_hsplit_top(view))
3526 return log_goto_line(view, eos);
3530 err = limit_log_view(view);
3539 view->x = MAX(view->maxx - view->ncols / 2, 0);
3544 if (view->x + view->ncols / 2 < view->maxx)
3545 view->x += 2; /* move two columns right */
3551 view->x -= MIN(view->x, 2); /* move two columns back */
3560 log_move_cursor_up(view, 0, 0);
3564 log_move_cursor_up(view, 0, 1);
3574 log_move_cursor_up(view, nscroll, 0);
3581 err = log_move_cursor_down(view, 0);
3584 s->use_committer = !s->use_committer;
3588 /* We don't know yet how many commits, so we're forced to
3589 * traverse them all. */
3591 s->thread_args.load_all = 1;
3592 if (!s->thread_args.log_complete)
3593 return trigger_log_thread(view, 0);
3594 err = log_move_cursor_down(view, s->commits->ncommits);
3595 s->thread_args.load_all = 0;
3606 err = log_move_cursor_down(view, nscroll);
3609 if (s->selected > view->nlines - 2)
3610 s->selected = view->nlines - 2;
3611 if (s->selected > s->commits->ncommits - 1)
3612 s->selected = s->commits->ncommits - 1;
3614 if (s->commits->ncommits < view->nlines - 1 &&
3615 !s->thread_args.log_complete) {
3616 s->thread_args.commits_needed += (view->nlines - 1) -
3617 s->commits->ncommits;
3618 err = trigger_log_thread(view, 1);
3624 if (s->selected_entry == NULL)
3626 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3630 if (s->selected_entry == NULL)
3632 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3638 if (ch == KEY_BACKSPACE &&
3639 got_path_is_root_dir(s->in_repo_path))
3641 err = stop_log_thread(s);
3644 if (ch == KEY_BACKSPACE) {
3646 err = got_path_dirname(&parent_path, s->in_repo_path);
3649 free(s->in_repo_path);
3650 s->in_repo_path = parent_path;
3651 s->thread_args.in_repo_path = s->in_repo_path;
3652 } else if (ch == CTRL('l')) {
3653 struct got_object_id *start_id;
3654 err = got_repo_match_object_id(&start_id, NULL,
3655 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3656 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3660 s->start_id = start_id;
3661 s->thread_args.start_id = s->start_id;
3663 s->log_branches = !s->log_branches;
3665 if (s->thread_args.pack_fds == NULL) {
3666 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3670 err = got_repo_open(&s->thread_args.repo,
3671 got_repo_get_path(s->repo), NULL,
3672 s->thread_args.pack_fds);
3676 err = tog_load_refs(s->repo, 0);
3679 err = got_commit_graph_open(&s->thread_args.graph,
3680 s->in_repo_path, !s->log_branches);
3683 err = got_commit_graph_iter_start(s->thread_args.graph,
3684 s->start_id, s->repo, NULL, NULL);
3687 free_commits(&s->real_commits);
3688 free_commits(&s->limit_commits);
3689 s->first_displayed_entry = NULL;
3690 s->last_displayed_entry = NULL;
3691 s->selected_entry = NULL;
3693 s->thread_args.log_complete = 0;
3695 s->thread_args.commits_needed = view->lines;
3696 s->matched_entry = NULL;
3697 s->search_entry = NULL;
3702 err = view_request_new(new_view, view, TOG_VIEW_REF);
3712 static const struct got_error *
3713 apply_unveil(const char *repo_path, const char *worktree_path)
3715 const struct got_error *error;
3718 if (unveil("gmon.out", "rwc") != 0)
3719 return got_error_from_errno2("unveil", "gmon.out");
3721 if (repo_path && unveil(repo_path, "r") != 0)
3722 return got_error_from_errno2("unveil", repo_path);
3724 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3725 return got_error_from_errno2("unveil", worktree_path);
3727 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3728 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3730 error = got_privsep_unveil_exec_helpers();
3734 if (unveil(NULL, NULL) != 0)
3735 return got_error_from_errno("unveil");
3744 * Override default signal handlers before starting ncurses.
3745 * This should prevent ncurses from installing its own
3746 * broken cleanup() signal handler.
3748 signal(SIGWINCH, tog_sigwinch);
3749 signal(SIGPIPE, tog_sigpipe);
3750 signal(SIGCONT, tog_sigcont);
3751 signal(SIGINT, tog_sigint);
3752 signal(SIGTERM, tog_sigterm);
3756 halfdelay(1); /* Do fast refresh while initial view is loading. */
3759 intrflush(stdscr, FALSE);
3760 keypad(stdscr, TRUE);
3762 if (getenv("TOG_COLORS") != NULL) {
3764 use_default_colors();
3768 static const struct got_error *
3769 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3770 struct got_repository *repo, struct got_worktree *worktree)
3772 const struct got_error *err = NULL;
3775 *in_repo_path = strdup("/");
3776 if (*in_repo_path == NULL)
3777 return got_error_from_errno("strdup");
3782 const char *prefix = got_worktree_get_path_prefix(worktree);
3785 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3788 if (asprintf(in_repo_path, "%s%s%s", prefix,
3789 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3791 err = got_error_from_errno("asprintf");
3792 *in_repo_path = NULL;
3796 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3801 static const struct got_error *
3802 cmd_log(int argc, char *argv[])
3804 const struct got_error *error;
3805 struct got_repository *repo = NULL;
3806 struct got_worktree *worktree = NULL;
3807 struct got_object_id *start_id = NULL;
3808 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3809 char *start_commit = NULL, *label = NULL;
3810 struct got_reference *ref = NULL;
3811 const char *head_ref_name = NULL;
3812 int ch, log_branches = 0;
3813 struct tog_view *view;
3814 int *pack_fds = NULL;
3816 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3822 start_commit = optarg;
3825 repo_path = realpath(optarg, NULL);
3826 if (repo_path == NULL)
3827 return got_error_from_errno2("realpath",
3842 error = got_repo_pack_fds_open(&pack_fds);
3846 if (repo_path == NULL) {
3847 cwd = getcwd(NULL, 0);
3849 return got_error_from_errno("getcwd");
3850 error = got_worktree_open(&worktree, cwd);
3851 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3855 strdup(got_worktree_get_repo_path(worktree));
3857 repo_path = strdup(cwd);
3858 if (repo_path == NULL) {
3859 error = got_error_from_errno("strdup");
3864 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3868 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3875 error = apply_unveil(got_repo_get_path(repo),
3876 worktree ? got_worktree_get_root_path(worktree) : NULL);
3880 /* already loaded by tog_log_with_path()? */
3881 if (TAILQ_EMPTY(&tog_refs)) {
3882 error = tog_load_refs(repo, 0);
3887 if (start_commit == NULL) {
3888 error = got_repo_match_object_id(&start_id, &label,
3889 worktree ? got_worktree_get_head_ref_name(worktree) :
3890 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3893 head_ref_name = label;
3895 error = got_ref_open(&ref, repo, start_commit, 0);
3897 head_ref_name = got_ref_get_name(ref);
3898 else if (error->code != GOT_ERR_NOT_REF)
3900 error = got_repo_match_object_id(&start_id, NULL,
3901 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3906 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3908 error = got_error_from_errno("view_open");
3911 error = open_log_view(view, start_id, repo, head_ref_name,
3912 in_repo_path, log_branches);
3916 /* Release work tree lock. */
3917 got_worktree_close(worktree);
3920 error = view_loop(view);
3930 const struct got_error *close_err = got_repo_close(repo);
3935 got_worktree_close(worktree);
3937 const struct got_error *pack_err =
3938 got_repo_pack_fds_close(pack_fds);
3950 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
3951 "object1 object2\n", getprogname());
3956 match_line(const char *line, regex_t *regex, size_t nmatch,
3957 regmatch_t *regmatch)
3959 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3962 static struct tog_color *
3963 match_color(struct tog_colors *colors, const char *line)
3965 struct tog_color *tc = NULL;
3967 STAILQ_FOREACH(tc, colors, entry) {
3968 if (match_line(line, &tc->regex, 0, NULL))
3975 static const struct got_error *
3976 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3977 WINDOW *window, int skipcol, regmatch_t *regmatch)
3979 const struct got_error *err = NULL;
3981 wchar_t *wline = NULL;
3982 int rme, rms, n, width, scrollx;
3983 int width0 = 0, width1 = 0, width2 = 0;
3984 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3988 rms = regmatch->rm_so;
3989 rme = regmatch->rm_eo;
3991 err = expand_tab(&exstr, line);
3995 /* Split the line into 3 segments, according to match offsets. */
3996 seg0 = strndup(exstr, rms);
3998 err = got_error_from_errno("strndup");
4001 seg1 = strndup(exstr + rms, rme - rms);
4003 err = got_error_from_errno("strndup");
4006 seg2 = strdup(exstr + rme);
4008 err = got_error_from_errno("strndup");
4012 /* draw up to matched token if we haven't scrolled past it */
4013 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4017 n = MAX(width0 - skipcol, 0);
4020 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4021 wlimit, col_tab_align, 1);
4024 waddwstr(window, &wline[scrollx]);
4034 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4038 wlen = wcslen(wline);
4040 width = wcwidth(wline[i]);
4042 /* should not happen, tabs are expanded */
4043 err = got_error(GOT_ERR_RANGE);
4046 if (width0 + w + width > skipcol)
4051 /* draw (visible part of) matched token (if scrolled into it) */
4052 if (width1 - w > 0) {
4053 wattron(window, A_STANDOUT);
4054 waddwstr(window, &wline[i]);
4055 wattroff(window, A_STANDOUT);
4056 wlimit -= (width1 - w);
4057 *wtotal += (width1 - w);
4061 if (wlimit > 0) { /* draw rest of line */
4063 if (skipcol > width0 + width1) {
4064 err = format_line(&wline, &width2, &scrollx, seg2,
4065 skipcol - (width0 + width1), wlimit,
4069 waddwstr(window, &wline[scrollx]);
4071 err = format_line(&wline, &width2, NULL, seg2, 0,
4072 wlimit, col_tab_align, 1);
4075 waddwstr(window, wline);
4089 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4092 int *eof, *first, *selected;
4094 if (view->type == TOG_VIEW_DIFF) {
4095 struct tog_diff_view_state *s = &view->state.diff;
4097 first = &s->first_displayed_line;
4101 } else if (view->type == TOG_VIEW_BLAME) {
4102 struct tog_blame_view_state *s = &view->state.blame;
4104 first = &s->first_displayed_line;
4105 selected = &s->selected_line;
4111 /* Center gline in the middle of the page like vi(1). */
4112 if (*lineno < view->gline - (view->nlines - 3) / 2)
4114 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4123 *selected = view->gline <= (view->nlines - 3) / 2 ?
4124 view->gline : (view->nlines - 3) / 2 + 1;
4130 static const struct got_error *
4131 draw_file(struct tog_view *view, const char *header)
4133 struct tog_diff_view_state *s = &view->state.diff;
4134 regmatch_t *regmatch = &view->regmatch;
4135 const struct got_error *err;
4138 size_t linesize = 0;
4142 int max_lines = view->nlines;
4143 int nlines = s->nlines;
4146 s->lineno = s->first_displayed_line - 1;
4147 line_offset = s->lines[s->first_displayed_line - 1].offset;
4148 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4149 return got_error_from_errno("fseek");
4151 werase(view->window);
4153 if (view->gline > s->nlines - 1)
4154 view->gline = s->nlines - 1;
4157 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4158 1 : view->gline - (view->nlines - 3) / 2 :
4159 s->lineno + s->selected_line;
4161 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4162 return got_error_from_errno("asprintf");
4163 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4169 if (view_needs_focus_indication(view))
4170 wstandout(view->window);
4171 waddwstr(view->window, wline);
4174 while (width++ < view->ncols)
4175 waddch(view->window, ' ');
4176 if (view_needs_focus_indication(view))
4177 wstandend(view->window);
4187 while (max_lines > 0 && nprinted < max_lines) {
4188 enum got_diff_line_type linetype;
4191 linelen = getline(&line, &linesize, s->f);
4192 if (linelen == -1) {
4198 return got_ferror(s->f, GOT_ERR_IO);
4201 if (++s->lineno < s->first_displayed_line)
4203 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4205 if (s->lineno == view->hiline)
4208 /* Set view->maxx based on full line length. */
4209 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4215 view->maxx = MAX(view->maxx, width);
4219 linetype = s->lines[s->lineno].type;
4220 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4221 linetype < GOT_DIFF_LINE_CONTEXT)
4222 attr |= COLOR_PAIR(linetype);
4224 wattron(view->window, attr);
4225 if (s->first_displayed_line + nprinted == s->matched_line &&
4226 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4227 err = add_matched_line(&width, line, view->ncols, 0,
4228 view->window, view->x, regmatch);
4235 err = format_line(&wline, &width, &skip, line,
4236 view->x, view->ncols, 0, view->x ? 1 : 0);
4241 waddwstr(view->window, &wline[skip]);
4245 if (s->lineno == view->hiline) {
4246 /* highlight full gline length */
4247 while (width++ < view->ncols)
4248 waddch(view->window, ' ');
4250 if (width <= view->ncols - 1)
4251 waddch(view->window, '\n');
4254 wattroff(view->window, attr);
4255 if (++nprinted == 1)
4256 s->first_displayed_line = s->lineno;
4260 s->last_displayed_line = s->first_displayed_line +
4263 s->last_displayed_line = s->first_displayed_line;
4268 while (nprinted < view->nlines) {
4269 waddch(view->window, '\n');
4273 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4279 wstandout(view->window);
4280 waddwstr(view->window, wline);
4283 wstandend(view->window);
4290 get_datestr(time_t *time, char *datebuf)
4292 struct tm mytm, *tm;
4295 tm = gmtime_r(time, &mytm);
4298 s = asctime_r(tm, datebuf);
4301 p = strchr(s, '\n');
4307 static const struct got_error *
4308 get_changed_paths(struct got_pathlist_head *paths,
4309 struct got_commit_object *commit, struct got_repository *repo)
4311 const struct got_error *err = NULL;
4312 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4313 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4314 struct got_object_qid *qid;
4316 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4318 struct got_commit_object *pcommit;
4319 err = got_object_open_as_commit(&pcommit, repo,
4324 tree_id1 = got_object_id_dup(
4325 got_object_commit_get_tree_id(pcommit));
4326 if (tree_id1 == NULL) {
4327 got_object_commit_close(pcommit);
4328 return got_error_from_errno("got_object_id_dup");
4330 got_object_commit_close(pcommit);
4335 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4340 tree_id2 = got_object_commit_get_tree_id(commit);
4341 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4345 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4346 got_diff_tree_collect_changed_paths, paths, 0);
4349 got_object_tree_close(tree1);
4351 got_object_tree_close(tree2);
4356 static const struct got_error *
4357 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4358 off_t off, uint8_t type)
4360 struct got_diff_line *p;
4362 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4364 return got_error_from_errno("reallocarray");
4366 (*lines)[*nlines].offset = off;
4367 (*lines)[*nlines].type = type;
4373 static const struct got_error *
4374 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4375 struct got_object_id *commit_id, struct got_reflist_head *refs,
4376 struct got_repository *repo, FILE *outfile)
4378 const struct got_error *err = NULL;
4379 char datebuf[26], *datestr;
4380 struct got_commit_object *commit;
4381 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4382 time_t committer_time;
4383 const char *author, *committer;
4384 char *refs_str = NULL;
4385 struct got_pathlist_head changed_paths;
4386 struct got_pathlist_entry *pe;
4390 TAILQ_INIT(&changed_paths);
4393 err = build_refs_str(&refs_str, refs, commit_id, repo);
4398 err = got_object_open_as_commit(&commit, repo, commit_id);
4402 err = got_object_id_str(&id_str, commit_id);
4404 err = got_error_from_errno("got_object_id_str");
4408 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4412 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4413 refs_str ? refs_str : "", refs_str ? ")" : "");
4415 err = got_error_from_errno("fprintf");
4419 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4423 n = fprintf(outfile, "from: %s\n",
4424 got_object_commit_get_author(commit));
4426 err = got_error_from_errno("fprintf");
4430 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4434 committer_time = got_object_commit_get_committer_time(commit);
4435 datestr = get_datestr(&committer_time, datebuf);
4437 n = fprintf(outfile, "date: %s UTC\n", datestr);
4439 err = got_error_from_errno("fprintf");
4443 err = add_line_metadata(lines, nlines, outoff,
4444 GOT_DIFF_LINE_DATE);
4448 author = got_object_commit_get_author(commit);
4449 committer = got_object_commit_get_committer(commit);
4450 if (strcmp(author, committer) != 0) {
4451 n = fprintf(outfile, "via: %s\n", committer);
4453 err = got_error_from_errno("fprintf");
4457 err = add_line_metadata(lines, nlines, outoff,
4458 GOT_DIFF_LINE_AUTHOR);
4462 if (got_object_commit_get_nparents(commit) > 1) {
4463 const struct got_object_id_queue *parent_ids;
4464 struct got_object_qid *qid;
4466 parent_ids = got_object_commit_get_parent_ids(commit);
4467 STAILQ_FOREACH(qid, parent_ids, entry) {
4468 err = got_object_id_str(&id_str, &qid->id);
4471 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4473 err = got_error_from_errno("fprintf");
4477 err = add_line_metadata(lines, nlines, outoff,
4478 GOT_DIFF_LINE_META);
4486 err = got_object_commit_get_logmsg(&logmsg, commit);
4490 while ((line = strsep(&s, "\n")) != NULL) {
4491 n = fprintf(outfile, "%s\n", line);
4493 err = got_error_from_errno("fprintf");
4497 err = add_line_metadata(lines, nlines, outoff,
4498 GOT_DIFF_LINE_LOGMSG);
4503 err = get_changed_paths(&changed_paths, commit, repo);
4506 TAILQ_FOREACH(pe, &changed_paths, entry) {
4507 struct got_diff_changed_path *cp = pe->data;
4508 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4510 err = got_error_from_errno("fprintf");
4514 err = add_line_metadata(lines, nlines, outoff,
4515 GOT_DIFF_LINE_CHANGES);
4518 free((char *)pe->path);
4522 fputc('\n', outfile);
4524 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4526 got_pathlist_free(&changed_paths);
4530 got_object_commit_close(commit);
4539 static const struct got_error *
4540 create_diff(struct tog_diff_view_state *s)
4542 const struct got_error *err = NULL;
4547 s->lines = malloc(sizeof(*s->lines));
4548 if (s->lines == NULL)
4549 return got_error_from_errno("malloc");
4554 err = got_error_from_errno("got_opentemp");
4557 if (s->f && fclose(s->f) == EOF) {
4558 err = got_error_from_errno("fclose");
4564 err = got_object_get_type(&obj_type, s->repo, s->id1);
4566 err = got_object_get_type(&obj_type, s->repo, s->id2);
4571 case GOT_OBJ_TYPE_BLOB:
4572 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4573 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4574 s->label1, s->label2, tog_diff_algo, s->diff_context,
4575 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4577 case GOT_OBJ_TYPE_TREE:
4578 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4579 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4580 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4581 s->force_text_diff, s->repo, s->f);
4583 case GOT_OBJ_TYPE_COMMIT: {
4584 const struct got_object_id_queue *parent_ids;
4585 struct got_object_qid *pid;
4586 struct got_commit_object *commit2;
4587 struct got_reflist_head *refs;
4589 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4592 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4593 /* Show commit info if we're diffing to a parent/root commit. */
4594 if (s->id1 == NULL) {
4595 err = write_commit_info(&s->lines, &s->nlines, s->id2,
4596 refs, s->repo, s->f);
4600 parent_ids = got_object_commit_get_parent_ids(commit2);
4601 STAILQ_FOREACH(pid, parent_ids, entry) {
4602 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4603 err = write_commit_info(&s->lines,
4604 &s->nlines, s->id2, refs, s->repo,
4612 got_object_commit_close(commit2);
4614 err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4615 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4616 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4617 s->force_text_diff, s->repo, s->f);
4621 err = got_error(GOT_ERR_OBJ_TYPE);
4625 if (s->f && fflush(s->f) != 0 && err == NULL)
4626 err = got_error_from_errno("fflush");
4631 diff_view_indicate_progress(struct tog_view *view)
4633 mvwaddstr(view->window, 0, 0, "diffing...");
4638 static const struct got_error *
4639 search_start_diff_view(struct tog_view *view)
4641 struct tog_diff_view_state *s = &view->state.diff;
4643 s->matched_line = 0;
4647 static const struct got_error *
4648 search_next_diff_view(struct tog_view *view)
4650 struct tog_diff_view_state *s = &view->state.diff;
4651 const struct got_error *err = NULL;
4654 size_t linesize = 0;
4657 if (!view->searching) {
4658 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4662 if (s->matched_line) {
4663 if (view->searching == TOG_SEARCH_FORWARD)
4664 lineno = s->matched_line + 1;
4666 lineno = s->matched_line - 1;
4668 lineno = s->first_displayed_line;
4673 if (lineno <= 0 || lineno > s->nlines) {
4674 if (s->matched_line == 0) {
4675 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4679 if (view->searching == TOG_SEARCH_FORWARD)
4685 offset = s->lines[lineno - 1].offset;
4686 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4688 return got_error_from_errno("fseeko");
4690 linelen = getline(&line, &linesize, s->f);
4691 if (linelen != -1) {
4693 err = expand_tab(&exstr, line);
4696 if (match_line(exstr, &view->regex, 1,
4698 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4699 s->matched_line = lineno;
4705 if (view->searching == TOG_SEARCH_FORWARD)
4712 if (s->matched_line) {
4713 s->first_displayed_line = s->matched_line;
4714 s->selected_line = 1;
4720 static const struct got_error *
4721 close_diff_view(struct tog_view *view)
4723 const struct got_error *err = NULL;
4724 struct tog_diff_view_state *s = &view->state.diff;
4730 if (s->f && fclose(s->f) == EOF)
4731 err = got_error_from_errno("fclose");
4733 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4734 err = got_error_from_errno("fclose");
4736 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4737 err = got_error_from_errno("fclose");
4739 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4740 err = got_error_from_errno("close");
4742 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4743 err = got_error_from_errno("close");
4751 static const struct got_error *
4752 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4753 struct got_object_id *id2, const char *label1, const char *label2,
4754 int diff_context, int ignore_whitespace, int force_text_diff,
4755 struct tog_view *parent_view, struct got_repository *repo)
4757 const struct got_error *err;
4758 struct tog_diff_view_state *s = &view->state.diff;
4760 memset(s, 0, sizeof(*s));
4764 if (id1 != NULL && id2 != NULL) {
4766 err = got_object_get_type(&type1, repo, id1);
4769 err = got_object_get_type(&type2, repo, id2);
4774 return got_error(GOT_ERR_OBJ_TYPE);
4776 s->first_displayed_line = 1;
4777 s->last_displayed_line = view->nlines;
4778 s->selected_line = 1;
4786 s->id1 = got_object_id_dup(id1);
4788 return got_error_from_errno("got_object_id_dup");
4792 s->id2 = got_object_id_dup(id2);
4793 if (s->id2 == NULL) {
4794 err = got_error_from_errno("got_object_id_dup");
4798 s->f1 = got_opentemp();
4799 if (s->f1 == NULL) {
4800 err = got_error_from_errno("got_opentemp");
4804 s->f2 = got_opentemp();
4805 if (s->f2 == NULL) {
4806 err = got_error_from_errno("got_opentemp");
4810 s->fd1 = got_opentempfd();
4812 err = got_error_from_errno("got_opentempfd");
4816 s->fd2 = got_opentempfd();
4818 err = got_error_from_errno("got_opentempfd");
4822 s->first_displayed_line = 1;
4823 s->last_displayed_line = view->nlines;
4824 s->diff_context = diff_context;
4825 s->ignore_whitespace = ignore_whitespace;
4826 s->force_text_diff = force_text_diff;
4827 s->parent_view = parent_view;
4830 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4833 rc = init_pair(GOT_DIFF_LINE_MINUS,
4834 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
4836 rc = init_pair(GOT_DIFF_LINE_PLUS,
4837 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
4839 rc = init_pair(GOT_DIFF_LINE_HUNK,
4840 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
4842 rc = init_pair(GOT_DIFF_LINE_META,
4843 get_color_value("TOG_COLOR_DIFF_META"), -1);
4845 rc = init_pair(GOT_DIFF_LINE_CHANGES,
4846 get_color_value("TOG_COLOR_DIFF_META"), -1);
4848 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
4849 get_color_value("TOG_COLOR_DIFF_META"), -1);
4851 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
4852 get_color_value("TOG_COLOR_DIFF_META"), -1);
4854 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
4855 get_color_value("TOG_COLOR_AUTHOR"), -1);
4857 rc = init_pair(GOT_DIFF_LINE_DATE,
4858 get_color_value("TOG_COLOR_DATE"), -1);
4860 err = got_error(GOT_ERR_RANGE);
4865 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4866 view_is_splitscreen(view))
4867 show_log_view(parent_view); /* draw border */
4868 diff_view_indicate_progress(view);
4870 err = create_diff(s);
4872 view->show = show_diff_view;
4873 view->input = input_diff_view;
4874 view->reset = reset_diff_view;
4875 view->close = close_diff_view;
4876 view->search_start = search_start_diff_view;
4877 view->search_next = search_next_diff_view;
4880 close_diff_view(view);
4884 static const struct got_error *
4885 show_diff_view(struct tog_view *view)
4887 const struct got_error *err;
4888 struct tog_diff_view_state *s = &view->state.diff;
4889 char *id_str1 = NULL, *id_str2, *header;
4890 const char *label1, *label2;
4893 err = got_object_id_str(&id_str1, s->id1);
4896 label1 = s->label1 ? s->label1 : id_str1;
4898 label1 = "/dev/null";
4900 err = got_object_id_str(&id_str2, s->id2);
4903 label2 = s->label2 ? s->label2 : id_str2;
4905 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4906 err = got_error_from_errno("asprintf");
4914 err = draw_file(view, header);
4919 static const struct got_error *
4920 set_selected_commit(struct tog_diff_view_state *s,
4921 struct commit_queue_entry *entry)
4923 const struct got_error *err;
4924 const struct got_object_id_queue *parent_ids;
4925 struct got_commit_object *selected_commit;
4926 struct got_object_qid *pid;
4929 s->id2 = got_object_id_dup(entry->id);
4931 return got_error_from_errno("got_object_id_dup");
4933 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4936 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4938 pid = STAILQ_FIRST(parent_ids);
4939 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4940 got_object_commit_close(selected_commit);
4944 static const struct got_error *
4945 reset_diff_view(struct tog_view *view)
4947 struct tog_diff_view_state *s = &view->state.diff;
4950 wclear(view->window);
4951 s->first_displayed_line = 1;
4952 s->last_displayed_line = view->nlines;
4953 s->matched_line = 0;
4954 diff_view_indicate_progress(view);
4955 return create_diff(s);
4959 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4963 i = start = s->first_displayed_line - 1;
4965 while (s->lines[i].type != type) {
4969 return; /* do nothing, requested type not in file */
4972 s->selected_line = 1;
4973 s->first_displayed_line = i;
4977 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4981 i = start = s->first_displayed_line + 1;
4983 while (s->lines[i].type != type) {
4984 if (i == s->nlines - 1)
4987 return; /* do nothing, requested type not in file */
4990 s->selected_line = 1;
4991 s->first_displayed_line = i;
4994 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4996 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4999 static const struct got_error *
5000 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5002 const struct got_error *err = NULL;
5003 struct tog_diff_view_state *s = &view->state.diff;
5004 struct tog_log_view_state *ls;
5005 struct commit_queue_entry *old_selected_entry;
5007 size_t linesize = 0;
5009 int i, nscroll = view->nlines - 1, up = 0;
5011 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5018 view->x = MAX(view->maxx - view->ncols / 3, 0);
5023 if (view->x + view->ncols / 3 < view->maxx)
5024 view->x += 2; /* move two columns right */
5030 view->x -= MIN(view->x, 2); /* move two columns back */
5037 s->force_text_diff = !s->force_text_diff;
5039 s->ignore_whitespace = !s->ignore_whitespace;
5040 err = reset_diff_view(view);
5044 s->first_displayed_line = 1;
5053 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5059 if (s->first_displayed_line > 1)
5060 s->first_displayed_line--;
5071 if (s->first_displayed_line == 1) {
5076 while (i++ < nscroll && s->first_displayed_line > 1)
5077 s->first_displayed_line--;
5083 s->first_displayed_line++;
5100 while (!s->eof && i++ < nscroll) {
5101 linelen = getline(&line, &linesize, s->f);
5102 s->first_displayed_line++;
5103 if (linelen == -1) {
5107 err = got_ferror(s->f, GOT_ERR_IO);
5114 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5117 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5120 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5123 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5126 if (s->diff_context > 0) {
5128 s->matched_line = 0;
5129 diff_view_indicate_progress(view);
5130 err = create_diff(s);
5131 if (s->first_displayed_line + view->nlines - 1 >
5133 s->first_displayed_line = 1;
5134 s->last_displayed_line = view->nlines;
5140 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5142 s->matched_line = 0;
5143 diff_view_indicate_progress(view);
5144 err = create_diff(s);
5156 if (s->parent_view == NULL) {
5160 s->parent_view->count = view->count;
5162 if (s->parent_view->type == TOG_VIEW_LOG) {
5163 ls = &s->parent_view->state.log;
5164 old_selected_entry = ls->selected_entry;
5166 err = input_log_view(NULL, s->parent_view,
5167 up ? KEY_UP : KEY_DOWN);
5170 view->count = s->parent_view->count;
5172 if (old_selected_entry == ls->selected_entry)
5175 err = set_selected_commit(s, ls->selected_entry);
5178 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5179 struct tog_blame_view_state *bs;
5180 struct got_object_id *id, *prev_id;
5182 bs = &s->parent_view->state.blame;
5183 prev_id = get_annotation_for_line(bs->blame.lines,
5184 bs->blame.nlines, bs->last_diffed_line);
5186 err = input_blame_view(&view, s->parent_view,
5187 up ? KEY_UP : KEY_DOWN);
5190 view->count = s->parent_view->count;
5192 if (prev_id == NULL)
5194 id = get_selected_commit_id(bs->blame.lines,
5195 bs->blame.nlines, bs->first_displayed_line,
5200 if (!got_object_id_cmp(prev_id, id))
5203 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5207 s->first_displayed_line = 1;
5208 s->last_displayed_line = view->nlines;
5209 s->matched_line = 0;
5212 diff_view_indicate_progress(view);
5213 err = create_diff(s);
5223 static const struct got_error *
5224 cmd_diff(int argc, char *argv[])
5226 const struct got_error *error = NULL;
5227 struct got_repository *repo = NULL;
5228 struct got_worktree *worktree = NULL;
5229 struct got_object_id *id1 = NULL, *id2 = NULL;
5230 char *repo_path = NULL, *cwd = NULL;
5231 char *id_str1 = NULL, *id_str2 = NULL;
5232 char *label1 = NULL, *label2 = NULL;
5233 int diff_context = 3, ignore_whitespace = 0;
5234 int ch, force_text_diff = 0;
5236 struct tog_view *view;
5237 int *pack_fds = NULL;
5239 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5242 force_text_diff = 1;
5245 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5248 errx(1, "number of context lines is %s: %s",
5252 repo_path = realpath(optarg, NULL);
5253 if (repo_path == NULL)
5254 return got_error_from_errno2("realpath",
5256 got_path_strip_trailing_slashes(repo_path);
5259 ignore_whitespace = 1;
5271 usage_diff(); /* TODO show local worktree changes */
5272 } else if (argc == 2) {
5278 error = got_repo_pack_fds_open(&pack_fds);
5282 if (repo_path == NULL) {
5283 cwd = getcwd(NULL, 0);
5285 return got_error_from_errno("getcwd");
5286 error = got_worktree_open(&worktree, cwd);
5287 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5291 strdup(got_worktree_get_repo_path(worktree));
5293 repo_path = strdup(cwd);
5294 if (repo_path == NULL) {
5295 error = got_error_from_errno("strdup");
5300 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5306 error = apply_unveil(got_repo_get_path(repo), NULL);
5310 error = tog_load_refs(repo, 0);
5314 error = got_repo_match_object_id(&id1, &label1, id_str1,
5315 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5319 error = got_repo_match_object_id(&id2, &label2, id_str2,
5320 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5324 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5326 error = got_error_from_errno("view_open");
5329 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5330 ignore_whitespace, force_text_diff, NULL, repo);
5333 error = view_loop(view);
5340 const struct got_error *close_err = got_repo_close(repo);
5345 got_worktree_close(worktree);
5347 const struct got_error *pack_err =
5348 got_repo_pack_fds_close(pack_fds);
5361 "usage: %s blame [-c commit] [-r repository-path] path\n",
5366 struct tog_blame_line {
5368 struct got_object_id *id;
5371 static const struct got_error *
5372 draw_blame(struct tog_view *view)
5374 struct tog_blame_view_state *s = &view->state.blame;
5375 struct tog_blame *blame = &s->blame;
5376 regmatch_t *regmatch = &view->regmatch;
5377 const struct got_error *err;
5378 int lineno = 0, nprinted = 0;
5380 size_t linesize = 0;
5384 struct tog_blame_line *blame_line;
5385 struct got_object_id *prev_id = NULL;
5387 struct tog_color *tc;
5389 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5394 werase(view->window);
5396 if (asprintf(&line, "commit %s", id_str) == -1) {
5397 err = got_error_from_errno("asprintf");
5402 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5407 if (view_needs_focus_indication(view))
5408 wstandout(view->window);
5409 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5411 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5412 waddwstr(view->window, wline);
5413 while (width++ < view->ncols)
5414 waddch(view->window, ' ');
5416 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5417 if (view_needs_focus_indication(view))
5418 wstandend(view->window);
5422 if (view->gline > blame->nlines)
5423 view->gline = blame->nlines;
5425 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5426 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5427 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5429 return got_error_from_errno("asprintf");
5432 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5437 waddwstr(view->window, wline);
5440 if (width < view->ncols - 1)
5441 waddch(view->window, '\n');
5445 while (nprinted < view->nlines - 2) {
5446 linelen = getline(&line, &linesize, blame->f);
5447 if (linelen == -1) {
5448 if (feof(blame->f)) {
5453 return got_ferror(blame->f, GOT_ERR_IO);
5455 if (++lineno < s->first_displayed_line)
5457 if (view->gline && !gotoline(view, &lineno, &nprinted))
5460 /* Set view->maxx based on full line length. */
5461 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5468 view->maxx = MAX(view->maxx, width);
5470 if (nprinted == s->selected_line - 1)
5471 wstandout(view->window);
5473 if (blame->nlines > 0) {
5474 blame_line = &blame->lines[lineno - 1];
5475 if (blame_line->annotated && prev_id &&
5476 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5477 !(nprinted == s->selected_line - 1)) {
5478 waddstr(view->window, " ");
5479 } else if (blame_line->annotated) {
5481 err = got_object_id_str(&id_str,
5487 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5489 wattr_on(view->window,
5490 COLOR_PAIR(tc->colorpair), NULL);
5491 wprintw(view->window, "%.8s", id_str);
5493 wattr_off(view->window,
5494 COLOR_PAIR(tc->colorpair), NULL);
5496 prev_id = blame_line->id;
5498 waddstr(view->window, "........");
5502 waddstr(view->window, "........");
5506 if (nprinted == s->selected_line - 1)
5507 wstandend(view->window);
5508 waddstr(view->window, " ");
5510 if (view->ncols <= 9) {
5512 } else if (s->first_displayed_line + nprinted ==
5514 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5515 err = add_matched_line(&width, line, view->ncols - 9, 9,
5516 view->window, view->x, regmatch);
5524 err = format_line(&wline, &width, &skip, line,
5525 view->x, view->ncols - 9, 9, 1);
5530 waddwstr(view->window, &wline[skip]);
5536 if (width <= view->ncols - 1)
5537 waddch(view->window, '\n');
5538 if (++nprinted == 1)
5539 s->first_displayed_line = lineno;
5542 s->last_displayed_line = lineno;
5549 static const struct got_error *
5550 blame_cb(void *arg, int nlines, int lineno,
5551 struct got_commit_object *commit, struct got_object_id *id)
5553 const struct got_error *err = NULL;
5554 struct tog_blame_cb_args *a = arg;
5555 struct tog_blame_line *line;
5558 if (nlines != a->nlines ||
5559 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5560 return got_error(GOT_ERR_RANGE);
5562 errcode = pthread_mutex_lock(&tog_mutex);
5564 return got_error_set_errno(errcode, "pthread_mutex_lock");
5566 if (*a->quit) { /* user has quit the blame view */
5567 err = got_error(GOT_ERR_ITER_COMPLETED);
5572 goto done; /* no change in this commit */
5574 line = &a->lines[lineno - 1];
5575 if (line->annotated)
5578 line->id = got_object_id_dup(id);
5579 if (line->id == NULL) {
5580 err = got_error_from_errno("got_object_id_dup");
5583 line->annotated = 1;
5585 errcode = pthread_mutex_unlock(&tog_mutex);
5587 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5592 blame_thread(void *arg)
5594 const struct got_error *err, *close_err;
5595 struct tog_blame_thread_args *ta = arg;
5596 struct tog_blame_cb_args *a = ta->cb_args;
5597 int errcode, fd1 = -1, fd2 = -1;
5598 FILE *f1 = NULL, *f2 = NULL;
5600 fd1 = got_opentempfd();
5602 return (void *)got_error_from_errno("got_opentempfd");
5604 fd2 = got_opentempfd();
5606 err = got_error_from_errno("got_opentempfd");
5610 f1 = got_opentemp();
5612 err = (void *)got_error_from_errno("got_opentemp");
5615 f2 = got_opentemp();
5617 err = (void *)got_error_from_errno("got_opentemp");
5621 err = block_signals_used_by_main_thread();
5625 err = got_blame(ta->path, a->commit_id, ta->repo,
5626 tog_diff_algo, blame_cb, ta->cb_args,
5627 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5628 if (err && err->code == GOT_ERR_CANCELLED)
5631 errcode = pthread_mutex_lock(&tog_mutex);
5633 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5637 close_err = got_repo_close(ta->repo);
5643 errcode = pthread_mutex_unlock(&tog_mutex);
5644 if (errcode && err == NULL)
5645 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5648 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5649 err = got_error_from_errno("close");
5650 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5651 err = got_error_from_errno("close");
5652 if (f1 && fclose(f1) == EOF && err == NULL)
5653 err = got_error_from_errno("fclose");
5654 if (f2 && fclose(f2) == EOF && err == NULL)
5655 err = got_error_from_errno("fclose");
5660 static struct got_object_id *
5661 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5662 int first_displayed_line, int selected_line)
5664 struct tog_blame_line *line;
5669 line = &lines[first_displayed_line - 1 + selected_line - 1];
5670 if (!line->annotated)
5676 static struct got_object_id *
5677 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5680 struct tog_blame_line *line;
5682 if (nlines <= 0 || lineno >= nlines)
5685 line = &lines[lineno - 1];
5686 if (!line->annotated)
5692 static const struct got_error *
5693 stop_blame(struct tog_blame *blame)
5695 const struct got_error *err = NULL;
5698 if (blame->thread) {
5700 errcode = pthread_mutex_unlock(&tog_mutex);
5702 return got_error_set_errno(errcode,
5703 "pthread_mutex_unlock");
5704 errcode = pthread_join(blame->thread, (void **)&err);
5706 return got_error_set_errno(errcode, "pthread_join");
5707 errcode = pthread_mutex_lock(&tog_mutex);
5709 return got_error_set_errno(errcode,
5710 "pthread_mutex_lock");
5711 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5713 blame->thread = NULL;
5715 if (blame->thread_args.repo) {
5716 const struct got_error *close_err;
5717 close_err = got_repo_close(blame->thread_args.repo);
5720 blame->thread_args.repo = NULL;
5723 if (fclose(blame->f) == EOF && err == NULL)
5724 err = got_error_from_errno("fclose");
5728 for (i = 0; i < blame->nlines; i++)
5729 free(blame->lines[i].id);
5731 blame->lines = NULL;
5733 free(blame->cb_args.commit_id);
5734 blame->cb_args.commit_id = NULL;
5735 if (blame->pack_fds) {
5736 const struct got_error *pack_err =
5737 got_repo_pack_fds_close(blame->pack_fds);
5740 blame->pack_fds = NULL;
5745 static const struct got_error *
5746 cancel_blame_view(void *arg)
5748 const struct got_error *err = NULL;
5752 errcode = pthread_mutex_lock(&tog_mutex);
5754 return got_error_set_errno(errcode,
5755 "pthread_mutex_unlock");
5758 err = got_error(GOT_ERR_CANCELLED);
5760 errcode = pthread_mutex_unlock(&tog_mutex);
5762 return got_error_set_errno(errcode,
5763 "pthread_mutex_lock");
5768 static const struct got_error *
5769 run_blame(struct tog_view *view)
5771 struct tog_blame_view_state *s = &view->state.blame;
5772 struct tog_blame *blame = &s->blame;
5773 const struct got_error *err = NULL;
5774 struct got_commit_object *commit = NULL;
5775 struct got_blob_object *blob = NULL;
5776 struct got_repository *thread_repo = NULL;
5777 struct got_object_id *obj_id = NULL;
5778 int obj_type, fd = -1;
5779 int *pack_fds = NULL;
5781 err = got_object_open_as_commit(&commit, s->repo,
5782 &s->blamed_commit->id);
5786 fd = got_opentempfd();
5788 err = got_error_from_errno("got_opentempfd");
5792 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5796 err = got_object_get_type(&obj_type, s->repo, obj_id);
5800 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5801 err = got_error(GOT_ERR_OBJ_TYPE);
5805 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5808 blame->f = got_opentemp();
5809 if (blame->f == NULL) {
5810 err = got_error_from_errno("got_opentemp");
5813 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5814 &blame->line_offsets, blame->f, blob);
5817 if (blame->nlines == 0) {
5818 s->blame_complete = 1;
5822 /* Don't include \n at EOF in the blame line count. */
5823 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5826 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5827 if (blame->lines == NULL) {
5828 err = got_error_from_errno("calloc");
5832 err = got_repo_pack_fds_open(&pack_fds);
5835 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5840 blame->pack_fds = pack_fds;
5841 blame->cb_args.view = view;
5842 blame->cb_args.lines = blame->lines;
5843 blame->cb_args.nlines = blame->nlines;
5844 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5845 if (blame->cb_args.commit_id == NULL) {
5846 err = got_error_from_errno("got_object_id_dup");
5849 blame->cb_args.quit = &s->done;
5851 blame->thread_args.path = s->path;
5852 blame->thread_args.repo = thread_repo;
5853 blame->thread_args.cb_args = &blame->cb_args;
5854 blame->thread_args.complete = &s->blame_complete;
5855 blame->thread_args.cancel_cb = cancel_blame_view;
5856 blame->thread_args.cancel_arg = &s->done;
5857 s->blame_complete = 0;
5859 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5860 s->first_displayed_line = 1;
5861 s->last_displayed_line = view->nlines;
5862 s->selected_line = 1;
5864 s->matched_line = 0;
5868 got_object_commit_close(commit);
5869 if (fd != -1 && close(fd) == -1 && err == NULL)
5870 err = got_error_from_errno("close");
5872 got_object_blob_close(blob);
5879 static const struct got_error *
5880 open_blame_view(struct tog_view *view, char *path,
5881 struct got_object_id *commit_id, struct got_repository *repo)
5883 const struct got_error *err = NULL;
5884 struct tog_blame_view_state *s = &view->state.blame;
5886 STAILQ_INIT(&s->blamed_commits);
5888 s->path = strdup(path);
5889 if (s->path == NULL)
5890 return got_error_from_errno("strdup");
5892 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5898 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5899 s->first_displayed_line = 1;
5900 s->last_displayed_line = view->nlines;
5901 s->selected_line = 1;
5902 s->blame_complete = 0;
5904 s->commit_id = commit_id;
5905 memset(&s->blame, 0, sizeof(s->blame));
5907 STAILQ_INIT(&s->colors);
5908 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5909 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5910 get_color_value("TOG_COLOR_COMMIT"));
5915 view->show = show_blame_view;
5916 view->input = input_blame_view;
5917 view->reset = reset_blame_view;
5918 view->close = close_blame_view;
5919 view->search_start = search_start_blame_view;
5920 view->search_next = search_next_blame_view;
5922 return run_blame(view);
5925 static const struct got_error *
5926 close_blame_view(struct tog_view *view)
5928 const struct got_error *err = NULL;
5929 struct tog_blame_view_state *s = &view->state.blame;
5931 if (s->blame.thread)
5932 err = stop_blame(&s->blame);
5934 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5935 struct got_object_qid *blamed_commit;
5936 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5937 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5938 got_object_qid_free(blamed_commit);
5942 free_colors(&s->colors);
5946 static const struct got_error *
5947 search_start_blame_view(struct tog_view *view)
5949 struct tog_blame_view_state *s = &view->state.blame;
5951 s->matched_line = 0;
5955 static const struct got_error *
5956 search_next_blame_view(struct tog_view *view)
5958 struct tog_blame_view_state *s = &view->state.blame;
5959 const struct got_error *err = NULL;
5962 size_t linesize = 0;
5965 if (!view->searching) {
5966 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5970 if (s->matched_line) {
5971 if (view->searching == TOG_SEARCH_FORWARD)
5972 lineno = s->matched_line + 1;
5974 lineno = s->matched_line - 1;
5976 lineno = s->first_displayed_line - 1 + s->selected_line;
5981 if (lineno <= 0 || lineno > s->blame.nlines) {
5982 if (s->matched_line == 0) {
5983 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5987 if (view->searching == TOG_SEARCH_FORWARD)
5990 lineno = s->blame.nlines;
5993 offset = s->blame.line_offsets[lineno - 1];
5994 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5996 return got_error_from_errno("fseeko");
5998 linelen = getline(&line, &linesize, s->blame.f);
5999 if (linelen != -1) {
6001 err = expand_tab(&exstr, line);
6004 if (match_line(exstr, &view->regex, 1,
6006 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6007 s->matched_line = lineno;
6013 if (view->searching == TOG_SEARCH_FORWARD)
6020 if (s->matched_line) {
6021 s->first_displayed_line = s->matched_line;
6022 s->selected_line = 1;
6028 static const struct got_error *
6029 show_blame_view(struct tog_view *view)
6031 const struct got_error *err = NULL;
6032 struct tog_blame_view_state *s = &view->state.blame;
6035 if (s->blame.thread == NULL && !s->blame_complete) {
6036 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6037 &s->blame.thread_args);
6039 return got_error_set_errno(errcode, "pthread_create");
6041 halfdelay(1); /* fast refresh while annotating */
6044 if (s->blame_complete)
6045 halfdelay(10); /* disable fast refresh */
6047 err = draw_blame(view);
6053 static const struct got_error *
6054 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6055 struct got_repository *repo, struct got_object_id *id)
6057 struct tog_view *log_view;
6058 const struct got_error *err = NULL;
6062 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6063 if (log_view == NULL)
6064 return got_error_from_errno("view_open");
6066 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6068 view_close(log_view);
6070 *new_view = log_view;
6075 static const struct got_error *
6076 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6078 const struct got_error *err = NULL, *thread_err = NULL;
6079 struct tog_view *diff_view;
6080 struct tog_blame_view_state *s = &view->state.blame;
6081 int eos, nscroll, begin_y = 0, begin_x = 0;
6083 eos = nscroll = view->nlines - 2;
6084 if (view_is_hsplit_top(view))
6092 view->x = MAX(view->maxx - view->ncols / 3, 0);
6097 if (view->x + view->ncols / 3 < view->maxx)
6098 view->x += 2; /* move two columns right */
6104 view->x -= MIN(view->x, 2); /* move two columns back */
6113 s->selected_line = 1;
6114 s->first_displayed_line = 1;
6119 if (s->blame.nlines < eos) {
6120 s->selected_line = s->blame.nlines;
6121 s->first_displayed_line = 1;
6123 s->selected_line = eos;
6124 s->first_displayed_line = s->blame.nlines - (eos - 1);
6131 if (s->selected_line > 1)
6133 else if (s->selected_line == 1 &&
6134 s->first_displayed_line > 1)
6135 s->first_displayed_line--;
6146 if (s->first_displayed_line == 1) {
6147 if (view->count > 1)
6149 s->selected_line = MAX(1, s->selected_line - nscroll);
6153 if (s->first_displayed_line > nscroll)
6154 s->first_displayed_line -= nscroll;
6156 s->first_displayed_line = 1;
6161 if (s->selected_line < eos && s->first_displayed_line +
6162 s->selected_line <= s->blame.nlines)
6164 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6165 s->first_displayed_line++;
6171 struct got_object_id *id = NULL;
6174 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6175 s->first_displayed_line, s->selected_line);
6179 struct got_commit_object *commit, *pcommit;
6180 struct got_object_qid *pid;
6181 struct got_object_id *blob_id = NULL;
6183 err = got_object_open_as_commit(&commit,
6188 got_object_commit_get_parent_ids(commit));
6190 got_object_commit_close(commit);
6193 /* Check if path history ends here. */
6194 err = got_object_open_as_commit(&pcommit,
6198 err = got_object_id_by_path(&blob_id, s->repo,
6200 got_object_commit_close(pcommit);
6202 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6204 got_object_commit_close(commit);
6207 err = got_object_get_type(&obj_type, s->repo,
6210 /* Can't blame non-blob type objects. */
6211 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6212 got_object_commit_close(commit);
6215 err = got_object_qid_alloc(&s->blamed_commit,
6217 got_object_commit_close(commit);
6219 if (got_object_id_cmp(id,
6220 &s->blamed_commit->id) == 0)
6222 err = got_object_qid_alloc(&s->blamed_commit,
6228 thread_err = stop_blame(&s->blame);
6232 STAILQ_INSERT_HEAD(&s->blamed_commits,
6233 s->blamed_commit, entry);
6234 err = run_blame(view);
6240 struct got_object_qid *first;
6243 first = STAILQ_FIRST(&s->blamed_commits);
6244 if (!got_object_id_cmp(&first->id, s->commit_id))
6247 thread_err = stop_blame(&s->blame);
6251 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6252 got_object_qid_free(s->blamed_commit);
6254 STAILQ_FIRST(&s->blamed_commits);
6255 err = run_blame(view);
6262 s->id_to_log = get_selected_commit_id(s->blame.lines,
6263 s->blame.nlines, s->first_displayed_line, s->selected_line);
6265 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6269 struct got_object_id *id = NULL;
6270 struct got_object_qid *pid;
6271 struct got_commit_object *commit = NULL;
6274 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6275 s->first_displayed_line, s->selected_line);
6278 err = got_object_open_as_commit(&commit, s->repo, id);
6281 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6283 /* traversed from diff view, release diff resources */
6284 err = close_diff_view(*new_view);
6287 diff_view = *new_view;
6289 if (view_is_parent_view(view))
6290 view_get_split(view, &begin_y, &begin_x);
6292 diff_view = view_open(0, 0, begin_y, begin_x,
6294 if (diff_view == NULL) {
6295 got_object_commit_close(commit);
6296 err = got_error_from_errno("view_open");
6300 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6301 id, NULL, NULL, 3, 0, 0, view, s->repo);
6302 got_object_commit_close(commit);
6304 view_close(diff_view);
6307 s->last_diffed_line = s->first_displayed_line - 1 +
6310 break; /* still open from active diff view */
6311 if (view_is_parent_view(view) &&
6312 view->mode == TOG_VIEW_SPLIT_HRZN) {
6313 err = view_init_hsplit(view, begin_y);
6319 diff_view->focussed = 1;
6320 diff_view->mode = view->mode;
6321 diff_view->nlines = view->lines - begin_y;
6322 if (view_is_parent_view(view)) {
6323 view_transfer_size(diff_view, view);
6324 err = view_close_child(view);
6327 err = view_set_child(view, diff_view);
6330 view->focus_child = 1;
6332 *new_view = diff_view;
6345 if (s->last_displayed_line >= s->blame.nlines &&
6346 s->selected_line >= MIN(s->blame.nlines,
6347 view->nlines - 2)) {
6351 if (s->last_displayed_line >= s->blame.nlines &&
6352 s->selected_line < view->nlines - 2) {
6354 MIN(nscroll, s->last_displayed_line -
6355 s->first_displayed_line - s->selected_line + 1);
6357 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6358 s->first_displayed_line += nscroll;
6360 s->first_displayed_line =
6361 s->blame.nlines - (view->nlines - 3);
6364 if (s->selected_line > view->nlines - 2) {
6365 s->selected_line = MIN(s->blame.nlines,
6373 return thread_err ? thread_err : err;
6376 static const struct got_error *
6377 reset_blame_view(struct tog_view *view)
6379 const struct got_error *err;
6380 struct tog_blame_view_state *s = &view->state.blame;
6384 err = stop_blame(&s->blame);
6388 return run_blame(view);
6391 static const struct got_error *
6392 cmd_blame(int argc, char *argv[])
6394 const struct got_error *error;
6395 struct got_repository *repo = NULL;
6396 struct got_worktree *worktree = NULL;
6397 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6398 char *link_target = NULL;
6399 struct got_object_id *commit_id = NULL;
6400 struct got_commit_object *commit = NULL;
6401 char *commit_id_str = NULL;
6403 struct tog_view *view;
6404 int *pack_fds = NULL;
6406 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6409 commit_id_str = optarg;
6412 repo_path = realpath(optarg, NULL);
6413 if (repo_path == NULL)
6414 return got_error_from_errno2("realpath",
6429 error = got_repo_pack_fds_open(&pack_fds);
6433 if (repo_path == NULL) {
6434 cwd = getcwd(NULL, 0);
6436 return got_error_from_errno("getcwd");
6437 error = got_worktree_open(&worktree, cwd);
6438 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6442 strdup(got_worktree_get_repo_path(worktree));
6444 repo_path = strdup(cwd);
6445 if (repo_path == NULL) {
6446 error = got_error_from_errno("strdup");
6451 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6455 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6462 error = apply_unveil(got_repo_get_path(repo), NULL);
6466 error = tog_load_refs(repo, 0);
6470 if (commit_id_str == NULL) {
6471 struct got_reference *head_ref;
6472 error = got_ref_open(&head_ref, repo, worktree ?
6473 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6476 error = got_ref_resolve(&commit_id, repo, head_ref);
6477 got_ref_close(head_ref);
6479 error = got_repo_match_object_id(&commit_id, NULL,
6480 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6485 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6487 error = got_error_from_errno("view_open");
6491 error = got_object_open_as_commit(&commit, repo, commit_id);
6495 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6500 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6505 /* Release work tree lock. */
6506 got_worktree_close(worktree);
6509 error = view_loop(view);
6517 got_object_commit_close(commit);
6519 got_worktree_close(worktree);
6521 const struct got_error *close_err = got_repo_close(repo);
6526 const struct got_error *pack_err =
6527 got_repo_pack_fds_close(pack_fds);
6535 static const struct got_error *
6536 draw_tree_entries(struct tog_view *view, const char *parent_path)
6538 struct tog_tree_view_state *s = &view->state.tree;
6539 const struct got_error *err = NULL;
6540 struct got_tree_entry *te;
6543 struct tog_color *tc;
6544 int width, n, nentries, i = 1;
6545 int limit = view->nlines;
6548 if (view_is_hsplit_top(view))
6549 --limit; /* border */
6551 werase(view->window);
6556 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6560 if (view_needs_focus_indication(view))
6561 wstandout(view->window);
6562 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6564 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6565 waddwstr(view->window, wline);
6568 while (width++ < view->ncols)
6569 waddch(view->window, ' ');
6571 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6572 if (view_needs_focus_indication(view))
6573 wstandend(view->window);
6578 if (s->first_displayed_entry) {
6579 i += got_tree_entry_get_index(s->first_displayed_entry);
6580 if (s->tree != s->root)
6581 ++i; /* account for ".." entry */
6583 nentries = got_object_tree_get_nentries(s->tree);
6584 if (asprintf(&index, "[%d/%d] %s",
6585 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6586 return got_error_from_errno("asprintf");
6587 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6591 waddwstr(view->window, wline);
6594 if (width < view->ncols - 1)
6595 waddch(view->window, '\n');
6598 waddch(view->window, '\n');
6602 if (s->first_displayed_entry == NULL) {
6603 te = got_object_tree_get_first_entry(s->tree);
6604 if (s->selected == 0) {
6606 wstandout(view->window);
6607 s->selected_entry = NULL;
6609 waddstr(view->window, " ..\n"); /* parent directory */
6610 if (s->selected == 0 && view->focussed)
6611 wstandend(view->window);
6618 te = s->first_displayed_entry;
6621 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6622 char *line = NULL, *id_str = NULL, *link_target = NULL;
6623 const char *modestr = "";
6626 te = got_object_tree_get_entry(s->tree, i);
6627 mode = got_tree_entry_get_mode(te);
6630 err = got_object_id_str(&id_str,
6631 got_tree_entry_get_id(te));
6633 return got_error_from_errno(
6634 "got_object_id_str");
6636 if (got_object_tree_entry_is_submodule(te))
6638 else if (S_ISLNK(mode)) {
6641 err = got_tree_entry_get_symlink_target(&link_target,
6647 for (i = 0; i < strlen(link_target); i++) {
6648 if (!isprint((unsigned char)link_target[i]))
6649 link_target[i] = '?';
6653 else if (S_ISDIR(mode))
6655 else if (mode & S_IXUSR)
6657 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6658 got_tree_entry_get_name(te), modestr,
6659 link_target ? " -> ": "",
6660 link_target ? link_target : "") == -1) {
6663 return got_error_from_errno("asprintf");
6667 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6673 if (n == s->selected) {
6675 wstandout(view->window);
6676 s->selected_entry = te;
6678 tc = match_color(&s->colors, line);
6680 wattr_on(view->window,
6681 COLOR_PAIR(tc->colorpair), NULL);
6682 waddwstr(view->window, wline);
6684 wattr_off(view->window,
6685 COLOR_PAIR(tc->colorpair), NULL);
6686 if (width < view->ncols - 1)
6687 waddch(view->window, '\n');
6688 if (n == s->selected && view->focussed)
6689 wstandend(view->window);
6695 s->last_displayed_entry = te;
6704 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6706 struct got_tree_entry *te;
6707 int isroot = s->tree == s->root;
6710 if (s->first_displayed_entry == NULL)
6713 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6714 while (i++ < maxscroll) {
6717 s->first_displayed_entry = NULL;
6720 s->first_displayed_entry = te;
6721 te = got_tree_entry_get_prev(s->tree, te);
6725 static const struct got_error *
6726 tree_scroll_down(struct tog_view *view, int maxscroll)
6728 struct tog_tree_view_state *s = &view->state.tree;
6729 struct got_tree_entry *next, *last;
6732 if (s->first_displayed_entry)
6733 next = got_tree_entry_get_next(s->tree,
6734 s->first_displayed_entry);
6736 next = got_object_tree_get_first_entry(s->tree);
6738 last = s->last_displayed_entry;
6739 while (next && n++ < maxscroll) {
6741 s->last_displayed_entry = last;
6742 last = got_tree_entry_get_next(s->tree, last);
6744 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6745 s->first_displayed_entry = next;
6746 next = got_tree_entry_get_next(s->tree, next);
6753 static const struct got_error *
6754 tree_entry_path(char **path, struct tog_parent_trees *parents,
6755 struct got_tree_entry *te)
6757 const struct got_error *err = NULL;
6758 struct tog_parent_tree *pt;
6759 size_t len = 2; /* for leading slash and NUL */
6761 TAILQ_FOREACH(pt, parents, entry)
6762 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6765 len += strlen(got_tree_entry_get_name(te));
6767 *path = calloc(1, len);
6769 return got_error_from_errno("calloc");
6772 pt = TAILQ_LAST(parents, tog_parent_trees);
6774 const char *name = got_tree_entry_get_name(pt->selected_entry);
6775 if (strlcat(*path, name, len) >= len) {
6776 err = got_error(GOT_ERR_NO_SPACE);
6779 if (strlcat(*path, "/", len) >= len) {
6780 err = got_error(GOT_ERR_NO_SPACE);
6783 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6786 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6787 err = got_error(GOT_ERR_NO_SPACE);
6799 static const struct got_error *
6800 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6801 struct got_tree_entry *te, struct tog_parent_trees *parents,
6802 struct got_object_id *commit_id, struct got_repository *repo)
6804 const struct got_error *err = NULL;
6806 struct tog_view *blame_view;
6810 err = tree_entry_path(&path, parents, te);
6814 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6815 if (blame_view == NULL) {
6816 err = got_error_from_errno("view_open");
6820 err = open_blame_view(blame_view, path, commit_id, repo);
6822 if (err->code == GOT_ERR_CANCELLED)
6824 view_close(blame_view);
6826 *new_view = blame_view;
6832 static const struct got_error *
6833 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6834 struct tog_tree_view_state *s)
6836 struct tog_view *log_view;
6837 const struct got_error *err = NULL;
6842 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6843 if (log_view == NULL)
6844 return got_error_from_errno("view_open");
6846 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6850 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6853 view_close(log_view);
6855 *new_view = log_view;
6860 static const struct got_error *
6861 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6862 const char *head_ref_name, struct got_repository *repo)
6864 const struct got_error *err = NULL;
6865 char *commit_id_str = NULL;
6866 struct tog_tree_view_state *s = &view->state.tree;
6867 struct got_commit_object *commit = NULL;
6869 TAILQ_INIT(&s->parents);
6870 STAILQ_INIT(&s->colors);
6872 s->commit_id = got_object_id_dup(commit_id);
6873 if (s->commit_id == NULL)
6874 return got_error_from_errno("got_object_id_dup");
6876 err = got_object_open_as_commit(&commit, repo, commit_id);
6881 * The root is opened here and will be closed when the view is closed.
6882 * Any visited subtrees and their path-wise parents are opened and
6885 err = got_object_open_as_tree(&s->root, repo,
6886 got_object_commit_get_tree_id(commit));
6891 err = got_object_id_str(&commit_id_str, commit_id);
6895 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6896 err = got_error_from_errno("asprintf");
6900 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6901 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6902 if (head_ref_name) {
6903 s->head_ref_name = strdup(head_ref_name);
6904 if (s->head_ref_name == NULL) {
6905 err = got_error_from_errno("strdup");
6911 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6912 err = add_color(&s->colors, "\\$$",
6913 TOG_COLOR_TREE_SUBMODULE,
6914 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6917 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6918 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6921 err = add_color(&s->colors, "/$",
6922 TOG_COLOR_TREE_DIRECTORY,
6923 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6927 err = add_color(&s->colors, "\\*$",
6928 TOG_COLOR_TREE_EXECUTABLE,
6929 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6933 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6934 get_color_value("TOG_COLOR_COMMIT"));
6939 view->show = show_tree_view;
6940 view->input = input_tree_view;
6941 view->close = close_tree_view;
6942 view->search_start = search_start_tree_view;
6943 view->search_next = search_next_tree_view;
6945 free(commit_id_str);
6947 got_object_commit_close(commit);
6949 close_tree_view(view);
6953 static const struct got_error *
6954 close_tree_view(struct tog_view *view)
6956 struct tog_tree_view_state *s = &view->state.tree;
6958 free_colors(&s->colors);
6959 free(s->tree_label);
6960 s->tree_label = NULL;
6962 s->commit_id = NULL;
6963 free(s->head_ref_name);
6964 s->head_ref_name = NULL;
6965 while (!TAILQ_EMPTY(&s->parents)) {
6966 struct tog_parent_tree *parent;
6967 parent = TAILQ_FIRST(&s->parents);
6968 TAILQ_REMOVE(&s->parents, parent, entry);
6969 if (parent->tree != s->root)
6970 got_object_tree_close(parent->tree);
6974 if (s->tree != NULL && s->tree != s->root)
6975 got_object_tree_close(s->tree);
6977 got_object_tree_close(s->root);
6981 static const struct got_error *
6982 search_start_tree_view(struct tog_view *view)
6984 struct tog_tree_view_state *s = &view->state.tree;
6986 s->matched_entry = NULL;
6991 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6993 regmatch_t regmatch;
6995 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6999 static const struct got_error *
7000 search_next_tree_view(struct tog_view *view)
7002 struct tog_tree_view_state *s = &view->state.tree;
7003 struct got_tree_entry *te = NULL;
7005 if (!view->searching) {
7006 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7010 if (s->matched_entry) {
7011 if (view->searching == TOG_SEARCH_FORWARD) {
7012 if (s->selected_entry)
7013 te = got_tree_entry_get_next(s->tree,
7016 te = got_object_tree_get_first_entry(s->tree);
7018 if (s->selected_entry == NULL)
7019 te = got_object_tree_get_last_entry(s->tree);
7021 te = got_tree_entry_get_prev(s->tree,
7025 if (s->selected_entry)
7026 te = s->selected_entry;
7027 else if (view->searching == TOG_SEARCH_FORWARD)
7028 te = got_object_tree_get_first_entry(s->tree);
7030 te = got_object_tree_get_last_entry(s->tree);
7035 if (s->matched_entry == NULL) {
7036 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7039 if (view->searching == TOG_SEARCH_FORWARD)
7040 te = got_object_tree_get_first_entry(s->tree);
7042 te = got_object_tree_get_last_entry(s->tree);
7045 if (match_tree_entry(te, &view->regex)) {
7046 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7047 s->matched_entry = te;
7051 if (view->searching == TOG_SEARCH_FORWARD)
7052 te = got_tree_entry_get_next(s->tree, te);
7054 te = got_tree_entry_get_prev(s->tree, te);
7057 if (s->matched_entry) {
7058 s->first_displayed_entry = s->matched_entry;
7065 static const struct got_error *
7066 show_tree_view(struct tog_view *view)
7068 const struct got_error *err = NULL;
7069 struct tog_tree_view_state *s = &view->state.tree;
7072 err = tree_entry_path(&parent_path, &s->parents, NULL);
7076 err = draw_tree_entries(view, parent_path);
7083 static const struct got_error *
7084 tree_goto_line(struct tog_view *view, int nlines)
7086 const struct got_error *err = NULL;
7087 struct tog_tree_view_state *s = &view->state.tree;
7088 struct got_tree_entry **fte, **lte, **ste;
7089 int g, last, first = 1, i = 1;
7090 int root = s->tree == s->root;
7091 int off = root ? 1 : 2;
7098 else if (g > got_object_tree_get_nentries(s->tree))
7099 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7101 fte = &s->first_displayed_entry;
7102 lte = &s->last_displayed_entry;
7103 ste = &s->selected_entry;
7106 first = got_tree_entry_get_index(*fte);
7107 first += off; /* account for ".." */
7109 last = got_tree_entry_get_index(*lte);
7112 if (g >= first && g <= last && g - first < nlines) {
7113 s->selected = g - first;
7114 return NULL; /* gline is on the current page */
7118 i = got_tree_entry_get_index(*ste);
7123 err = tree_scroll_down(view, g - i);
7126 if (got_tree_entry_get_index(*lte) >=
7127 got_object_tree_get_nentries(s->tree) - 1 &&
7128 first + s->selected < g &&
7129 s->selected < s->ndisplayed - 1) {
7130 first = got_tree_entry_get_index(*fte);
7132 s->selected = g - first;
7135 tree_scroll_up(s, i - g);
7138 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7139 s->selected = g - 1;
7144 static const struct got_error *
7145 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7147 const struct got_error *err = NULL;
7148 struct tog_tree_view_state *s = &view->state.tree;
7149 struct got_tree_entry *te;
7150 int n, nscroll = view->nlines - 3;
7153 return tree_goto_line(view, nscroll);
7157 s->show_ids = !s->show_ids;
7162 if (!s->selected_entry)
7164 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7168 err = view_request_new(new_view, view, TOG_VIEW_REF);
7174 if (s->tree == s->root)
7175 s->first_displayed_entry =
7176 got_object_tree_get_first_entry(s->tree);
7178 s->first_displayed_entry = NULL;
7182 int eos = view->nlines - 3;
7184 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7188 te = got_object_tree_get_last_entry(s->tree);
7189 for (n = 0; n < eos; n++) {
7191 if (s->tree != s->root) {
7192 s->first_displayed_entry = NULL;
7197 s->first_displayed_entry = te;
7198 te = got_tree_entry_get_prev(s->tree, te);
7201 s->selected = n - 1;
7207 if (s->selected > 0) {
7211 tree_scroll_up(s, 1);
7212 if (s->selected_entry == NULL ||
7213 (s->tree == s->root && s->selected_entry ==
7214 got_object_tree_get_first_entry(s->tree)))
7224 if (s->tree == s->root) {
7225 if (got_object_tree_get_first_entry(s->tree) ==
7226 s->first_displayed_entry)
7227 s->selected -= MIN(s->selected, nscroll);
7229 if (s->first_displayed_entry == NULL)
7230 s->selected -= MIN(s->selected, nscroll);
7232 tree_scroll_up(s, MAX(0, nscroll));
7233 if (s->selected_entry == NULL ||
7234 (s->tree == s->root && s->selected_entry ==
7235 got_object_tree_get_first_entry(s->tree)))
7241 if (s->selected < s->ndisplayed - 1) {
7245 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7247 /* can't scroll any further */
7251 tree_scroll_down(view, 1);
7261 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7263 /* can't scroll any further; move cursor down */
7264 if (s->selected < s->ndisplayed - 1)
7265 s->selected += MIN(nscroll,
7266 s->ndisplayed - s->selected - 1);
7271 tree_scroll_down(view, nscroll);
7276 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7277 struct tog_parent_tree *parent;
7278 /* user selected '..' */
7279 if (s->tree == s->root) {
7283 parent = TAILQ_FIRST(&s->parents);
7284 TAILQ_REMOVE(&s->parents, parent,
7286 got_object_tree_close(s->tree);
7287 s->tree = parent->tree;
7288 s->first_displayed_entry =
7289 parent->first_displayed_entry;
7291 parent->selected_entry;
7292 s->selected = parent->selected;
7293 if (s->selected > view->nlines - 3) {
7294 err = offset_selection_down(view);
7299 } else if (S_ISDIR(got_tree_entry_get_mode(
7300 s->selected_entry))) {
7301 struct got_tree_object *subtree;
7303 err = got_object_open_as_tree(&subtree, s->repo,
7304 got_tree_entry_get_id(s->selected_entry));
7307 err = tree_view_visit_subtree(s, subtree);
7309 got_object_tree_close(subtree);
7312 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7313 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7316 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7317 s->selected = view->nlines - 4;
7333 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7338 static const struct got_error *
7339 cmd_tree(int argc, char *argv[])
7341 const struct got_error *error;
7342 struct got_repository *repo = NULL;
7343 struct got_worktree *worktree = NULL;
7344 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7345 struct got_object_id *commit_id = NULL;
7346 struct got_commit_object *commit = NULL;
7347 const char *commit_id_arg = NULL;
7349 struct got_reference *ref = NULL;
7350 const char *head_ref_name = NULL;
7352 struct tog_view *view;
7353 int *pack_fds = NULL;
7355 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7358 commit_id_arg = optarg;
7361 repo_path = realpath(optarg, NULL);
7362 if (repo_path == NULL)
7363 return got_error_from_errno2("realpath",
7378 error = got_repo_pack_fds_open(&pack_fds);
7382 if (repo_path == NULL) {
7383 cwd = getcwd(NULL, 0);
7385 return got_error_from_errno("getcwd");
7386 error = got_worktree_open(&worktree, cwd);
7387 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7391 strdup(got_worktree_get_repo_path(worktree));
7393 repo_path = strdup(cwd);
7394 if (repo_path == NULL) {
7395 error = got_error_from_errno("strdup");
7400 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7404 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7411 error = apply_unveil(got_repo_get_path(repo), NULL);
7415 error = tog_load_refs(repo, 0);
7419 if (commit_id_arg == NULL) {
7420 error = got_repo_match_object_id(&commit_id, &label,
7421 worktree ? got_worktree_get_head_ref_name(worktree) :
7422 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7425 head_ref_name = label;
7427 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7429 head_ref_name = got_ref_get_name(ref);
7430 else if (error->code != GOT_ERR_NOT_REF)
7432 error = got_repo_match_object_id(&commit_id, NULL,
7433 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7438 error = got_object_open_as_commit(&commit, repo, commit_id);
7442 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7444 error = got_error_from_errno("view_open");
7447 error = open_tree_view(view, commit_id, head_ref_name, repo);
7450 if (!got_path_is_root_dir(in_repo_path)) {
7451 error = tree_view_walk_path(&view->state.tree, commit,
7458 /* Release work tree lock. */
7459 got_worktree_close(worktree);
7462 error = view_loop(view);
7471 const struct got_error *close_err = got_repo_close(repo);
7476 const struct got_error *pack_err =
7477 got_repo_pack_fds_close(pack_fds);
7485 static const struct got_error *
7486 ref_view_load_refs(struct tog_ref_view_state *s)
7488 struct got_reflist_entry *sre;
7489 struct tog_reflist_entry *re;
7492 TAILQ_FOREACH(sre, &tog_refs, entry) {
7493 if (strncmp(got_ref_get_name(sre->ref),
7494 "refs/got/", 9) == 0 &&
7495 strncmp(got_ref_get_name(sre->ref),
7496 "refs/got/backup/", 16) != 0)
7499 re = malloc(sizeof(*re));
7501 return got_error_from_errno("malloc");
7503 re->ref = got_ref_dup(sre->ref);
7504 if (re->ref == NULL)
7505 return got_error_from_errno("got_ref_dup");
7506 re->idx = s->nrefs++;
7507 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7510 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7515 ref_view_free_refs(struct tog_ref_view_state *s)
7517 struct tog_reflist_entry *re;
7519 while (!TAILQ_EMPTY(&s->refs)) {
7520 re = TAILQ_FIRST(&s->refs);
7521 TAILQ_REMOVE(&s->refs, re, entry);
7522 got_ref_close(re->ref);
7527 static const struct got_error *
7528 open_ref_view(struct tog_view *view, struct got_repository *repo)
7530 const struct got_error *err = NULL;
7531 struct tog_ref_view_state *s = &view->state.ref;
7533 s->selected_entry = 0;
7536 TAILQ_INIT(&s->refs);
7537 STAILQ_INIT(&s->colors);
7539 err = ref_view_load_refs(s);
7543 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7544 err = add_color(&s->colors, "^refs/heads/",
7545 TOG_COLOR_REFS_HEADS,
7546 get_color_value("TOG_COLOR_REFS_HEADS"));
7550 err = add_color(&s->colors, "^refs/tags/",
7551 TOG_COLOR_REFS_TAGS,
7552 get_color_value("TOG_COLOR_REFS_TAGS"));
7556 err = add_color(&s->colors, "^refs/remotes/",
7557 TOG_COLOR_REFS_REMOTES,
7558 get_color_value("TOG_COLOR_REFS_REMOTES"));
7562 err = add_color(&s->colors, "^refs/got/backup/",
7563 TOG_COLOR_REFS_BACKUP,
7564 get_color_value("TOG_COLOR_REFS_BACKUP"));
7569 view->show = show_ref_view;
7570 view->input = input_ref_view;
7571 view->close = close_ref_view;
7572 view->search_start = search_start_ref_view;
7573 view->search_next = search_next_ref_view;
7576 free_colors(&s->colors);
7580 static const struct got_error *
7581 close_ref_view(struct tog_view *view)
7583 struct tog_ref_view_state *s = &view->state.ref;
7585 ref_view_free_refs(s);
7586 free_colors(&s->colors);
7591 static const struct got_error *
7592 resolve_reflist_entry(struct got_object_id **commit_id,
7593 struct tog_reflist_entry *re, struct got_repository *repo)
7595 const struct got_error *err = NULL;
7596 struct got_object_id *obj_id;
7597 struct got_tag_object *tag = NULL;
7602 err = got_ref_resolve(&obj_id, repo, re->ref);
7606 err = got_object_get_type(&obj_type, repo, obj_id);
7611 case GOT_OBJ_TYPE_COMMIT:
7612 *commit_id = obj_id;
7614 case GOT_OBJ_TYPE_TAG:
7615 err = got_object_open_as_tag(&tag, repo, obj_id);
7619 err = got_object_get_type(&obj_type, repo,
7620 got_object_tag_get_object_id(tag));
7623 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7624 err = got_error(GOT_ERR_OBJ_TYPE);
7627 *commit_id = got_object_id_dup(
7628 got_object_tag_get_object_id(tag));
7629 if (*commit_id == NULL) {
7630 err = got_error_from_errno("got_object_id_dup");
7635 err = got_error(GOT_ERR_OBJ_TYPE);
7641 got_object_tag_close(tag);
7649 static const struct got_error *
7650 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7651 struct tog_reflist_entry *re, struct got_repository *repo)
7653 struct tog_view *log_view;
7654 const struct got_error *err = NULL;
7655 struct got_object_id *commit_id = NULL;
7659 err = resolve_reflist_entry(&commit_id, re, repo);
7661 if (err->code != GOT_ERR_OBJ_TYPE)
7667 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7668 if (log_view == NULL) {
7669 err = got_error_from_errno("view_open");
7673 err = open_log_view(log_view, commit_id, repo,
7674 got_ref_get_name(re->ref), "", 0);
7677 view_close(log_view);
7679 *new_view = log_view;
7685 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7687 struct tog_reflist_entry *re;
7690 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7693 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7694 while (i++ < maxscroll) {
7697 s->first_displayed_entry = re;
7698 re = TAILQ_PREV(re, tog_reflist_head, entry);
7702 static const struct got_error *
7703 ref_scroll_down(struct tog_view *view, int maxscroll)
7705 struct tog_ref_view_state *s = &view->state.ref;
7706 struct tog_reflist_entry *next, *last;
7709 if (s->first_displayed_entry)
7710 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7712 next = TAILQ_FIRST(&s->refs);
7714 last = s->last_displayed_entry;
7715 while (next && n++ < maxscroll) {
7717 s->last_displayed_entry = last;
7718 last = TAILQ_NEXT(last, entry);
7720 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7721 s->first_displayed_entry = next;
7722 next = TAILQ_NEXT(next, entry);
7729 static const struct got_error *
7730 search_start_ref_view(struct tog_view *view)
7732 struct tog_ref_view_state *s = &view->state.ref;
7734 s->matched_entry = NULL;
7739 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7741 regmatch_t regmatch;
7743 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7747 static const struct got_error *
7748 search_next_ref_view(struct tog_view *view)
7750 struct tog_ref_view_state *s = &view->state.ref;
7751 struct tog_reflist_entry *re = NULL;
7753 if (!view->searching) {
7754 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7758 if (s->matched_entry) {
7759 if (view->searching == TOG_SEARCH_FORWARD) {
7760 if (s->selected_entry)
7761 re = TAILQ_NEXT(s->selected_entry, entry);
7763 re = TAILQ_PREV(s->selected_entry,
7764 tog_reflist_head, entry);
7766 if (s->selected_entry == NULL)
7767 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7769 re = TAILQ_PREV(s->selected_entry,
7770 tog_reflist_head, entry);
7773 if (s->selected_entry)
7774 re = s->selected_entry;
7775 else if (view->searching == TOG_SEARCH_FORWARD)
7776 re = TAILQ_FIRST(&s->refs);
7778 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7783 if (s->matched_entry == NULL) {
7784 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7787 if (view->searching == TOG_SEARCH_FORWARD)
7788 re = TAILQ_FIRST(&s->refs);
7790 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7793 if (match_reflist_entry(re, &view->regex)) {
7794 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7795 s->matched_entry = re;
7799 if (view->searching == TOG_SEARCH_FORWARD)
7800 re = TAILQ_NEXT(re, entry);
7802 re = TAILQ_PREV(re, tog_reflist_head, entry);
7805 if (s->matched_entry) {
7806 s->first_displayed_entry = s->matched_entry;
7813 static const struct got_error *
7814 show_ref_view(struct tog_view *view)
7816 const struct got_error *err = NULL;
7817 struct tog_ref_view_state *s = &view->state.ref;
7818 struct tog_reflist_entry *re;
7821 struct tog_color *tc;
7823 int limit = view->nlines;
7825 werase(view->window);
7828 if (view_is_hsplit_top(view))
7829 --limit; /* border */
7834 re = s->first_displayed_entry;
7836 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7838 return got_error_from_errno("asprintf");
7840 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7845 if (view_needs_focus_indication(view))
7846 wstandout(view->window);
7847 waddwstr(view->window, wline);
7848 while (width++ < view->ncols)
7849 waddch(view->window, ' ');
7850 if (view_needs_focus_indication(view))
7851 wstandend(view->window);
7860 while (re && limit > 0) {
7862 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7865 struct got_commit_object *ci;
7866 struct got_tag_object *tag;
7867 struct got_object_id *id;
7871 err = got_ref_resolve(&id, s->repo, re->ref);
7874 err = got_object_open_as_tag(&tag, s->repo, id);
7876 if (err->code != GOT_ERR_OBJ_TYPE) {
7880 err = got_object_open_as_commit(&ci, s->repo,
7886 t = got_object_commit_get_committer_time(ci);
7887 got_object_commit_close(ci);
7889 t = got_object_tag_get_tagger_time(tag);
7890 got_object_tag_close(tag);
7893 if (gmtime_r(&t, &tm) == NULL)
7894 return got_error_from_errno("gmtime_r");
7895 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7896 return got_error(GOT_ERR_NO_SPACE);
7898 if (got_ref_is_symbolic(re->ref)) {
7899 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7900 ymd : "", got_ref_get_name(re->ref),
7901 got_ref_get_symref_target(re->ref)) == -1)
7902 return got_error_from_errno("asprintf");
7903 } else if (s->show_ids) {
7904 struct got_object_id *id;
7906 err = got_ref_resolve(&id, s->repo, re->ref);
7909 err = got_object_id_str(&id_str, id);
7914 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7915 got_ref_get_name(re->ref), id_str) == -1) {
7916 err = got_error_from_errno("asprintf");
7923 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7924 got_ref_get_name(re->ref)) == -1)
7925 return got_error_from_errno("asprintf");
7927 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7933 if (n == s->selected) {
7935 wstandout(view->window);
7936 s->selected_entry = re;
7938 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7940 wattr_on(view->window,
7941 COLOR_PAIR(tc->colorpair), NULL);
7942 waddwstr(view->window, wline);
7944 wattr_off(view->window,
7945 COLOR_PAIR(tc->colorpair), NULL);
7946 if (width < view->ncols - 1)
7947 waddch(view->window, '\n');
7948 if (n == s->selected && view->focussed)
7949 wstandend(view->window);
7955 s->last_displayed_entry = re;
7958 re = TAILQ_NEXT(re, entry);
7965 static const struct got_error *
7966 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7967 struct tog_reflist_entry *re, struct got_repository *repo)
7969 const struct got_error *err = NULL;
7970 struct got_object_id *commit_id = NULL;
7971 struct tog_view *tree_view;
7975 err = resolve_reflist_entry(&commit_id, re, repo);
7977 if (err->code != GOT_ERR_OBJ_TYPE)
7984 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7985 if (tree_view == NULL) {
7986 err = got_error_from_errno("view_open");
7990 err = open_tree_view(tree_view, commit_id,
7991 got_ref_get_name(re->ref), repo);
7995 *new_view = tree_view;
8001 static const struct got_error *
8002 ref_goto_line(struct tog_view *view, int nlines)
8004 const struct got_error *err = NULL;
8005 struct tog_ref_view_state *s = &view->state.ref;
8006 int g, idx = s->selected_entry->idx;
8013 else if (g > s->nrefs)
8016 if (g >= s->first_displayed_entry->idx + 1 &&
8017 g <= s->last_displayed_entry->idx + 1 &&
8018 g - s->first_displayed_entry->idx - 1 < nlines) {
8019 s->selected = g - s->first_displayed_entry->idx - 1;
8024 err = ref_scroll_down(view, g - idx - 1);
8027 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8028 s->first_displayed_entry->idx + s->selected < g &&
8029 s->selected < s->ndisplayed - 1)
8030 s->selected = g - s->first_displayed_entry->idx - 1;
8031 } else if (idx + 1 > g)
8032 ref_scroll_up(s, idx - g + 1);
8034 if (g < nlines && s->first_displayed_entry->idx == 0)
8035 s->selected = g - 1;
8041 static const struct got_error *
8042 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8044 const struct got_error *err = NULL;
8045 struct tog_ref_view_state *s = &view->state.ref;
8046 struct tog_reflist_entry *re;
8047 int n, nscroll = view->nlines - 1;
8050 return ref_goto_line(view, nscroll);
8054 s->show_ids = !s->show_ids;
8058 s->show_date = !s->show_date;
8062 s->sort_by_date = !s->sort_by_date;
8064 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8065 got_ref_cmp_by_commit_timestamp_descending :
8066 tog_ref_cmp_by_name, s->repo);
8069 got_reflist_object_id_map_free(tog_refs_idmap);
8070 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8071 &tog_refs, s->repo);
8074 ref_view_free_refs(s);
8075 err = ref_view_load_refs(s);
8080 if (!s->selected_entry)
8082 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8086 if (!s->selected_entry)
8088 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8094 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8098 int eos = view->nlines - 1;
8100 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8104 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8105 for (n = 0; n < eos; n++) {
8108 s->first_displayed_entry = re;
8109 re = TAILQ_PREV(re, tog_reflist_head, entry);
8112 s->selected = n - 1;
8118 if (s->selected > 0) {
8122 ref_scroll_up(s, 1);
8123 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8133 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8134 s->selected -= MIN(nscroll, s->selected);
8135 ref_scroll_up(s, MAX(0, nscroll));
8136 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8142 if (s->selected < s->ndisplayed - 1) {
8146 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8147 /* can't scroll any further */
8151 ref_scroll_down(view, 1);
8161 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8162 /* can't scroll any further; move cursor down */
8163 if (s->selected < s->ndisplayed - 1)
8164 s->selected += MIN(nscroll,
8165 s->ndisplayed - s->selected - 1);
8166 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8167 s->selected += s->ndisplayed - s->selected - 1;
8171 ref_scroll_down(view, nscroll);
8176 err = tog_load_refs(s->repo, s->sort_by_date);
8179 ref_view_free_refs(s);
8180 err = ref_view_load_refs(s);
8183 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8184 s->selected = view->nlines - 2;
8198 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8203 static const struct got_error *
8204 cmd_ref(int argc, char *argv[])
8206 const struct got_error *error;
8207 struct got_repository *repo = NULL;
8208 struct got_worktree *worktree = NULL;
8209 char *cwd = NULL, *repo_path = NULL;
8211 struct tog_view *view;
8212 int *pack_fds = NULL;
8214 while ((ch = getopt(argc, argv, "r:")) != -1) {
8217 repo_path = realpath(optarg, NULL);
8218 if (repo_path == NULL)
8219 return got_error_from_errno2("realpath",
8234 error = got_repo_pack_fds_open(&pack_fds);
8238 if (repo_path == NULL) {
8239 cwd = getcwd(NULL, 0);
8241 return got_error_from_errno("getcwd");
8242 error = got_worktree_open(&worktree, cwd);
8243 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8247 strdup(got_worktree_get_repo_path(worktree));
8249 repo_path = strdup(cwd);
8250 if (repo_path == NULL) {
8251 error = got_error_from_errno("strdup");
8256 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8262 error = apply_unveil(got_repo_get_path(repo), NULL);
8266 error = tog_load_refs(repo, 0);
8270 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8272 error = got_error_from_errno("view_open");
8276 error = open_ref_view(view, repo);
8281 /* Release work tree lock. */
8282 got_worktree_close(worktree);
8285 error = view_loop(view);
8290 const struct got_error *close_err = got_repo_close(repo);
8295 const struct got_error *pack_err =
8296 got_repo_pack_fds_close(pack_fds);
8304 static const struct got_error *
8305 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8306 enum tog_view_type request, int y, int x)
8308 const struct got_error *err = NULL;
8314 if (view->type == TOG_VIEW_LOG) {
8315 struct tog_log_view_state *s = &view->state.log;
8317 err = open_diff_view_for_commit(new_view, y, x,
8318 s->selected_entry->commit, s->selected_entry->id,
8321 return got_error_msg(GOT_ERR_NOT_IMPL,
8322 "parent/child view pair not supported");
8324 case TOG_VIEW_BLAME:
8325 if (view->type == TOG_VIEW_TREE) {
8326 struct tog_tree_view_state *s = &view->state.tree;
8328 err = blame_tree_entry(new_view, y, x,
8329 s->selected_entry, &s->parents, s->commit_id,
8332 return got_error_msg(GOT_ERR_NOT_IMPL,
8333 "parent/child view pair not supported");
8336 if (view->type == TOG_VIEW_BLAME)
8337 err = log_annotated_line(new_view, y, x,
8338 view->state.blame.repo, view->state.blame.id_to_log);
8339 else if (view->type == TOG_VIEW_TREE)
8340 err = log_selected_tree_entry(new_view, y, x,
8342 else if (view->type == TOG_VIEW_REF)
8343 err = log_ref_entry(new_view, y, x,
8344 view->state.ref.selected_entry,
8345 view->state.ref.repo);
8347 return got_error_msg(GOT_ERR_NOT_IMPL,
8348 "parent/child view pair not supported");
8351 if (view->type == TOG_VIEW_LOG)
8352 err = browse_commit_tree(new_view, y, x,
8353 view->state.log.selected_entry,
8354 view->state.log.in_repo_path,
8355 view->state.log.head_ref_name,
8356 view->state.log.repo);
8357 else if (view->type == TOG_VIEW_REF)
8358 err = browse_ref_tree(new_view, y, x,
8359 view->state.ref.selected_entry,
8360 view->state.ref.repo);
8362 return got_error_msg(GOT_ERR_NOT_IMPL,
8363 "parent/child view pair not supported");
8366 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8367 if (*new_view == NULL)
8368 return got_error_from_errno("view_open");
8369 if (view->type == TOG_VIEW_LOG)
8370 err = open_ref_view(*new_view, view->state.log.repo);
8371 else if (view->type == TOG_VIEW_TREE)
8372 err = open_ref_view(*new_view, view->state.tree.repo);
8374 err = got_error_msg(GOT_ERR_NOT_IMPL,
8375 "parent/child view pair not supported");
8377 view_close(*new_view);
8380 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
8387 * If view was scrolled down to move the selected line into view when opening a
8388 * horizontal split, scroll back up when closing the split/toggling fullscreen.
8391 offset_selection_up(struct tog_view *view)
8393 switch (view->type) {
8394 case TOG_VIEW_BLAME: {
8395 struct tog_blame_view_state *s = &view->state.blame;
8396 if (s->first_displayed_line == 1) {
8397 s->selected_line = MAX(s->selected_line - view->offset,
8401 if (s->first_displayed_line > view->offset)
8402 s->first_displayed_line -= view->offset;
8404 s->first_displayed_line = 1;
8405 s->selected_line += view->offset;
8409 log_scroll_up(&view->state.log, view->offset);
8410 view->state.log.selected += view->offset;
8413 ref_scroll_up(&view->state.ref, view->offset);
8414 view->state.ref.selected += view->offset;
8417 tree_scroll_up(&view->state.tree, view->offset);
8418 view->state.tree.selected += view->offset;
8428 * If the selected line is in the section of screen covered by the bottom split,
8429 * scroll down offset lines to move it into view and index its new position.
8431 static const struct got_error *
8432 offset_selection_down(struct tog_view *view)
8434 const struct got_error *err = NULL;
8435 const struct got_error *(*scrolld)(struct tog_view *, int);
8436 int *selected = NULL;
8439 switch (view->type) {
8440 case TOG_VIEW_BLAME: {
8441 struct tog_blame_view_state *s = &view->state.blame;
8444 if (s->selected_line > view->nlines - header) {
8445 offset = abs(view->nlines - s->selected_line - header);
8446 s->first_displayed_line += offset;
8447 s->selected_line -= offset;
8448 view->offset = offset;
8452 case TOG_VIEW_LOG: {
8453 struct tog_log_view_state *s = &view->state.log;
8454 scrolld = &log_scroll_down;
8455 header = view_is_parent_view(view) ? 3 : 2;
8456 selected = &s->selected;
8459 case TOG_VIEW_REF: {
8460 struct tog_ref_view_state *s = &view->state.ref;
8461 scrolld = &ref_scroll_down;
8463 selected = &s->selected;
8466 case TOG_VIEW_TREE: {
8467 struct tog_tree_view_state *s = &view->state.tree;
8468 scrolld = &tree_scroll_down;
8470 selected = &s->selected;
8480 if (selected && *selected > view->nlines - header) {
8481 offset = abs(view->nlines - *selected - header);
8482 view->offset = offset;
8483 if (scrolld && offset) {
8484 err = scrolld(view, offset);
8485 *selected -= offset;
8493 list_commands(FILE *fp)
8497 fprintf(fp, "commands:");
8498 for (i = 0; i < nitems(tog_commands); i++) {
8499 const struct tog_cmd *cmd = &tog_commands[i];
8500 fprintf(fp, " %s", cmd->name);
8506 usage(int hflag, int status)
8508 FILE *fp = (status == 0) ? stdout : stderr;
8510 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8513 fprintf(fp, "lazy usage: %s path\n", getprogname());
8520 make_argv(int argc, ...)
8528 argv = calloc(argc, sizeof(char *));
8531 for (i = 0; i < argc; i++) {
8532 argv[i] = strdup(va_arg(ap, char *));
8533 if (argv[i] == NULL)
8542 * Try to convert 'tog path' into a 'tog log path' command.
8543 * The user could simply have mistyped the command rather than knowingly
8544 * provided a path. So check whether argv[0] can in fact be resolved
8545 * to a path in the HEAD commit and print a special error if not.
8546 * This hack is for mpi@ <3
8548 static const struct got_error *
8549 tog_log_with_path(int argc, char *argv[])
8551 const struct got_error *error = NULL, *close_err;
8552 const struct tog_cmd *cmd = NULL;
8553 struct got_repository *repo = NULL;
8554 struct got_worktree *worktree = NULL;
8555 struct got_object_id *commit_id = NULL, *id = NULL;
8556 struct got_commit_object *commit = NULL;
8557 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8558 char *commit_id_str = NULL, **cmd_argv = NULL;
8559 int *pack_fds = NULL;
8561 cwd = getcwd(NULL, 0);
8563 return got_error_from_errno("getcwd");
8565 error = got_repo_pack_fds_open(&pack_fds);
8569 error = got_worktree_open(&worktree, cwd);
8570 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8574 repo_path = strdup(got_worktree_get_repo_path(worktree));
8576 repo_path = strdup(cwd);
8577 if (repo_path == NULL) {
8578 error = got_error_from_errno("strdup");
8582 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8586 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8591 error = tog_load_refs(repo, 0);
8594 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8595 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8596 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8601 got_worktree_close(worktree);
8605 error = got_object_open_as_commit(&commit, repo, commit_id);
8609 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8611 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8613 fprintf(stderr, "%s: '%s' is no known command or path\n",
8614 getprogname(), argv[0]);
8619 error = got_object_id_str(&commit_id_str, commit_id);
8623 cmd = &tog_commands[0]; /* log */
8625 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8626 error = cmd->cmd_main(argc, cmd_argv);
8629 close_err = got_repo_close(repo);
8634 got_object_commit_close(commit);
8636 got_worktree_close(worktree);
8638 const struct got_error *pack_err =
8639 got_repo_pack_fds_close(pack_fds);
8644 free(commit_id_str);
8651 for (i = 0; i < argc; i++)
8660 main(int argc, char *argv[])
8662 const struct got_error *error = NULL;
8663 const struct tog_cmd *cmd = NULL;
8664 int ch, hflag = 0, Vflag = 0;
8665 char **cmd_argv = NULL;
8666 static const struct option longopts[] = {
8667 { "version", no_argument, NULL, 'V' },
8670 char *diff_algo_str = NULL;
8672 if (!isatty(STDIN_FILENO))
8673 errx(1, "standard input is not a tty");
8675 setlocale(LC_CTYPE, "");
8677 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8697 got_version_print_str();
8702 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8710 /* Build an argument vector which runs a default command. */
8711 cmd = &tog_commands[0];
8713 cmd_argv = make_argv(argc, cmd->name);
8717 /* Did the user specify a command? */
8718 for (i = 0; i < nitems(tog_commands); i++) {
8719 if (strncmp(tog_commands[i].name, argv[0],
8720 strlen(argv[0])) == 0) {
8721 cmd = &tog_commands[i];
8727 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8728 if (diff_algo_str) {
8729 if (strcasecmp(diff_algo_str, "patience") == 0)
8730 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8731 if (strcasecmp(diff_algo_str, "myers") == 0)
8732 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8738 /* No command specified; try log with a path */
8739 error = tog_log_with_path(argc, argv);
8744 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8751 for (i = 0; i < argc; i++)
8756 if (error && error->code != GOT_ERR_CANCELLED)
8757 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);