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 #if defined(__FreeBSD__) || defined(__APPLE__)
24 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
45 #include "got_compat.h"
47 #include "got_version.h"
48 #include "got_error.h"
49 #include "got_object.h"
50 #include "got_reference.h"
51 #include "got_repository.h"
53 #include "got_opentemp.h"
55 #include "got_cancel.h"
56 #include "got_commit_graph.h"
57 #include "got_blame.h"
58 #include "got_privsep.h"
60 #include "got_worktree.h"
63 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
67 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
71 #define CTRL(x) ((x) & 0x1f)
75 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
80 const struct got_error *(*cmd_main)(int, char *[]);
81 void (*cmd_usage)(void);
84 __dead static void usage(int, int);
85 __dead static void usage_log(void);
86 __dead static void usage_diff(void);
87 __dead static void usage_blame(void);
88 __dead static void usage_tree(void);
89 __dead static void usage_ref(void);
91 static const struct got_error* cmd_log(int, char *[]);
92 static const struct got_error* cmd_diff(int, char *[]);
93 static const struct got_error* cmd_blame(int, char *[]);
94 static const struct got_error* cmd_tree(int, char *[]);
95 static const struct got_error* cmd_ref(int, char *[]);
97 static const struct tog_cmd tog_commands[] = {
98 { "log", cmd_log, usage_log },
99 { "diff", cmd_diff, usage_diff },
100 { "blame", cmd_blame, usage_blame },
101 { "tree", cmd_tree, usage_tree },
102 { "ref", cmd_ref, usage_ref },
119 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
121 #define TOG_EOF_STRING "(END)"
123 struct commit_queue_entry {
124 TAILQ_ENTRY(commit_queue_entry) entry;
125 struct got_object_id *id;
126 struct got_commit_object *commit;
129 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
130 struct commit_queue {
132 struct commit_queue_head head;
136 STAILQ_ENTRY(tog_color) entry;
140 STAILQ_HEAD(tog_colors, tog_color);
142 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
143 static struct got_reflist_object_id_map *tog_refs_idmap;
144 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
146 static const struct got_error *
147 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
148 struct got_reference* re2)
150 const char *name1 = got_ref_get_name(re1);
151 const char *name2 = got_ref_get_name(re2);
152 int isbackup1, isbackup2;
154 /* Sort backup refs towards the bottom of the list. */
155 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
156 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
157 if (!isbackup1 && isbackup2) {
160 } else if (isbackup1 && !isbackup2) {
165 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
169 static const struct got_error *
170 tog_load_refs(struct got_repository *repo, int sort_by_date)
172 const struct got_error *err;
174 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
175 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
180 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
187 if (tog_refs_idmap) {
188 got_reflist_object_id_map_free(tog_refs_idmap);
189 tog_refs_idmap = NULL;
191 got_ref_list_free(&tog_refs);
194 static const struct got_error *
195 add_color(struct tog_colors *colors, const char *pattern,
196 int idx, short color)
198 const struct got_error *err = NULL;
199 struct tog_color *tc;
202 if (idx < 1 || idx > COLOR_PAIRS - 1)
205 init_pair(idx, color, -1);
207 tc = calloc(1, sizeof(*tc));
209 return got_error_from_errno("calloc");
210 regerr = regcomp(&tc->regex, pattern,
211 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
213 static char regerr_msg[512];
214 static char err_msg[512];
215 regerror(regerr, &tc->regex, regerr_msg,
217 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
219 err = got_error_msg(GOT_ERR_REGEX, err_msg);
224 STAILQ_INSERT_HEAD(colors, tc, entry);
229 free_colors(struct tog_colors *colors)
231 struct tog_color *tc;
233 while (!STAILQ_EMPTY(colors)) {
234 tc = STAILQ_FIRST(colors);
235 STAILQ_REMOVE_HEAD(colors, entry);
241 static struct tog_color *
242 get_color(struct tog_colors *colors, int colorpair)
244 struct tog_color *tc = NULL;
246 STAILQ_FOREACH(tc, colors, entry) {
247 if (tc->colorpair == colorpair)
255 default_color_value(const char *envvar)
257 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
258 return COLOR_MAGENTA;
259 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
261 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
263 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
265 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
266 return COLOR_MAGENTA;
267 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
268 return COLOR_MAGENTA;
269 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
271 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
273 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
275 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
277 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
279 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
281 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
282 return COLOR_MAGENTA;
283 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
285 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
292 get_color_value(const char *envvar)
294 const char *val = getenv(envvar);
297 return default_color_value(envvar);
299 if (strcasecmp(val, "black") == 0)
301 if (strcasecmp(val, "red") == 0)
303 if (strcasecmp(val, "green") == 0)
305 if (strcasecmp(val, "yellow") == 0)
307 if (strcasecmp(val, "blue") == 0)
309 if (strcasecmp(val, "magenta") == 0)
310 return COLOR_MAGENTA;
311 if (strcasecmp(val, "cyan") == 0)
313 if (strcasecmp(val, "white") == 0)
315 if (strcasecmp(val, "default") == 0)
318 return default_color_value(envvar);
322 struct tog_diff_view_state {
323 struct got_object_id *id1, *id2;
324 const char *label1, *label2;
327 int first_displayed_line;
328 int last_displayed_line;
331 int ignore_whitespace;
333 struct got_repository *repo;
334 struct tog_colors colors;
340 /* passed from log or blame view; may be NULL */
341 struct tog_view *parent_view;
344 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
345 static volatile sig_atomic_t tog_thread_error;
347 struct tog_log_thread_args {
348 pthread_cond_t need_commits;
349 pthread_cond_t commit_loaded;
352 struct got_commit_graph *graph;
353 struct commit_queue *commits;
354 const char *in_repo_path;
355 struct got_object_id *start_id;
356 struct got_repository *repo;
360 struct commit_queue_entry **first_displayed_entry;
361 struct commit_queue_entry **selected_entry;
363 int *search_next_done;
367 struct tog_log_view_state {
368 struct commit_queue commits;
369 struct commit_queue_entry *first_displayed_entry;
370 struct commit_queue_entry *last_displayed_entry;
371 struct commit_queue_entry *selected_entry;
376 struct got_repository *repo;
377 struct got_object_id *start_id;
380 struct tog_log_thread_args thread_args;
381 struct commit_queue_entry *matched_entry;
382 struct commit_queue_entry *search_entry;
383 struct tog_colors colors;
387 #define TOG_COLOR_DIFF_MINUS 1
388 #define TOG_COLOR_DIFF_PLUS 2
389 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
390 #define TOG_COLOR_DIFF_META 4
391 #define TOG_COLOR_TREE_SUBMODULE 5
392 #define TOG_COLOR_TREE_SYMLINK 6
393 #define TOG_COLOR_TREE_DIRECTORY 7
394 #define TOG_COLOR_TREE_EXECUTABLE 8
395 #define TOG_COLOR_COMMIT 9
396 #define TOG_COLOR_AUTHOR 10
397 #define TOG_COLOR_DATE 11
398 #define TOG_COLOR_REFS_HEADS 12
399 #define TOG_COLOR_REFS_TAGS 13
400 #define TOG_COLOR_REFS_REMOTES 14
401 #define TOG_COLOR_REFS_BACKUP 15
403 struct tog_blame_cb_args {
404 struct tog_blame_line *lines; /* one per line */
407 struct tog_view *view;
408 struct got_object_id *commit_id;
412 struct tog_blame_thread_args {
414 struct got_repository *repo;
415 struct tog_blame_cb_args *cb_args;
417 got_cancel_cb cancel_cb;
424 struct tog_blame_line *lines;
428 struct tog_blame_thread_args thread_args;
429 struct tog_blame_cb_args cb_args;
434 struct tog_blame_view_state {
435 int first_displayed_line;
436 int last_displayed_line;
438 int last_diffed_line;
442 struct got_object_id_queue blamed_commits;
443 struct got_object_qid *blamed_commit;
445 struct got_repository *repo;
446 struct got_object_id *commit_id;
447 struct tog_blame blame;
449 struct tog_colors colors;
452 struct tog_parent_tree {
453 TAILQ_ENTRY(tog_parent_tree) entry;
454 struct got_tree_object *tree;
455 struct got_tree_entry *first_displayed_entry;
456 struct got_tree_entry *selected_entry;
460 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
462 struct tog_tree_view_state {
464 struct got_object_id *commit_id;/* commit which this tree belongs to */
465 struct got_tree_object *root; /* the commit's root tree entry */
466 struct got_tree_object *tree; /* currently displayed (sub-)tree */
467 struct got_tree_entry *first_displayed_entry;
468 struct got_tree_entry *last_displayed_entry;
469 struct got_tree_entry *selected_entry;
470 int ndisplayed, selected, show_ids;
471 struct tog_parent_trees parents; /* parent trees of current sub-tree */
473 struct got_repository *repo;
474 struct got_tree_entry *matched_entry;
475 struct tog_colors colors;
478 struct tog_reflist_entry {
479 TAILQ_ENTRY(tog_reflist_entry) entry;
480 struct got_reference *ref;
484 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
486 struct tog_ref_view_state {
487 struct tog_reflist_head refs;
488 struct tog_reflist_entry *first_displayed_entry;
489 struct tog_reflist_entry *last_displayed_entry;
490 struct tog_reflist_entry *selected_entry;
491 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
492 struct got_repository *repo;
493 struct tog_reflist_entry *matched_entry;
494 struct tog_colors colors;
498 * We implement two types of views: parent views and child views.
500 * The 'Tab' key switches focus between a parent view and its child view.
501 * Child views are shown side-by-side to their parent view, provided
502 * there is enough screen estate.
504 * When a new view is opened from within a parent view, this new view
505 * becomes a child view of the parent view, replacing any existing child.
507 * When a new view is opened from within a child view, this new view
508 * becomes a parent view which will obscure the views below until the
509 * user quits the new parent view by typing 'q'.
511 * This list of views contains parent views only.
512 * Child views are only pointed to by their parent view.
514 TAILQ_HEAD(tog_view_list_head, tog_view);
517 TAILQ_ENTRY(tog_view) entry;
520 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
521 int resized_y, resized_x; /* begin_y/x based on user resizing */
522 int maxx, x; /* max column and current start column */
523 int lines, cols; /* copies of LINES and COLS */
524 int nscrolled, offset; /* lines scrolled and hsplit line offset */
525 int ch, count; /* current keymap and count prefix */
526 int resized; /* set when in a resize event */
527 int focussed; /* Only set on one parent or child view at a time. */
529 struct tog_view *parent;
530 struct tog_view *child;
533 * This flag is initially set on parent views when a new child view
534 * is created. It gets toggled when the 'Tab' key switches focus
535 * between parent and child.
536 * The flag indicates whether focus should be passed on to our child
537 * view if this parent view gets picked for focus after another parent
538 * view was closed. This prevents child views from losing focus in such
543 enum tog_view_mode mode;
544 /* type-specific state */
545 enum tog_view_type type;
547 struct tog_diff_view_state diff;
548 struct tog_log_view_state log;
549 struct tog_blame_view_state blame;
550 struct tog_tree_view_state tree;
551 struct tog_ref_view_state ref;
554 const struct got_error *(*show)(struct tog_view *);
555 const struct got_error *(*input)(struct tog_view **,
556 struct tog_view *, int);
557 const struct got_error *(*reset)(struct tog_view *);
558 const struct got_error *(*resize)(struct tog_view *, int);
559 const struct got_error *(*close)(struct tog_view *);
561 const struct got_error *(*search_start)(struct tog_view *);
562 const struct got_error *(*search_next)(struct tog_view *);
565 #define TOG_SEARCH_FORWARD 1
566 #define TOG_SEARCH_BACKWARD 2
567 int search_next_done;
568 #define TOG_SEARCH_HAVE_MORE 1
569 #define TOG_SEARCH_NO_MORE 2
570 #define TOG_SEARCH_HAVE_NONE 3
575 static const struct got_error *open_diff_view(struct tog_view *,
576 struct got_object_id *, struct got_object_id *,
577 const char *, const char *, int, int, int, struct tog_view *,
578 struct got_repository *);
579 static const struct got_error *show_diff_view(struct tog_view *);
580 static const struct got_error *input_diff_view(struct tog_view **,
581 struct tog_view *, int);
582 static const struct got_error *reset_diff_view(struct tog_view *);
583 static const struct got_error* close_diff_view(struct tog_view *);
584 static const struct got_error *search_start_diff_view(struct tog_view *);
585 static const struct got_error *search_next_diff_view(struct tog_view *);
587 static const struct got_error *open_log_view(struct tog_view *,
588 struct got_object_id *, struct got_repository *,
589 const char *, const char *, int);
590 static const struct got_error * show_log_view(struct tog_view *);
591 static const struct got_error *input_log_view(struct tog_view **,
592 struct tog_view *, int);
593 static const struct got_error *resize_log_view(struct tog_view *, int);
594 static const struct got_error *close_log_view(struct tog_view *);
595 static const struct got_error *search_start_log_view(struct tog_view *);
596 static const struct got_error *search_next_log_view(struct tog_view *);
598 static const struct got_error *open_blame_view(struct tog_view *, char *,
599 struct got_object_id *, struct got_repository *);
600 static const struct got_error *show_blame_view(struct tog_view *);
601 static const struct got_error *input_blame_view(struct tog_view **,
602 struct tog_view *, int);
603 static const struct got_error *reset_blame_view(struct tog_view *);
604 static const struct got_error *close_blame_view(struct tog_view *);
605 static const struct got_error *search_start_blame_view(struct tog_view *);
606 static const struct got_error *search_next_blame_view(struct tog_view *);
608 static const struct got_error *open_tree_view(struct tog_view *,
609 struct got_object_id *, const char *, struct got_repository *);
610 static const struct got_error *show_tree_view(struct tog_view *);
611 static const struct got_error *input_tree_view(struct tog_view **,
612 struct tog_view *, int);
613 static const struct got_error *close_tree_view(struct tog_view *);
614 static const struct got_error *search_start_tree_view(struct tog_view *);
615 static const struct got_error *search_next_tree_view(struct tog_view *);
617 static const struct got_error *open_ref_view(struct tog_view *,
618 struct got_repository *);
619 static const struct got_error *show_ref_view(struct tog_view *);
620 static const struct got_error *input_ref_view(struct tog_view **,
621 struct tog_view *, int);
622 static const struct got_error *close_ref_view(struct tog_view *);
623 static const struct got_error *search_start_ref_view(struct tog_view *);
624 static const struct got_error *search_next_ref_view(struct tog_view *);
626 static volatile sig_atomic_t tog_sigwinch_received;
627 static volatile sig_atomic_t tog_sigpipe_received;
628 static volatile sig_atomic_t tog_sigcont_received;
629 static volatile sig_atomic_t tog_sigint_received;
630 static volatile sig_atomic_t tog_sigterm_received;
633 tog_sigwinch(int signo)
635 tog_sigwinch_received = 1;
639 tog_sigpipe(int signo)
641 tog_sigpipe_received = 1;
645 tog_sigcont(int signo)
647 tog_sigcont_received = 1;
651 tog_sigint(int signo)
653 tog_sigint_received = 1;
657 tog_sigterm(int signo)
659 tog_sigterm_received = 1;
663 tog_fatal_signal_received(void)
665 return (tog_sigpipe_received ||
666 tog_sigint_received || tog_sigint_received);
669 static const struct got_error *
670 view_close(struct tog_view *view)
672 const struct got_error *err = NULL, *child_err = NULL;
675 child_err = view_close(view->child);
679 err = view->close(view);
681 del_panel(view->panel);
683 delwin(view->window);
685 return err ? err : child_err;
688 static struct tog_view *
689 view_open(int nlines, int ncols, int begin_y, int begin_x,
690 enum tog_view_type type)
692 struct tog_view *view = calloc(1, sizeof(*view));
700 view->nlines = nlines ? nlines : LINES - begin_y;
701 view->ncols = ncols ? ncols : COLS - begin_x;
702 view->begin_y = begin_y;
703 view->begin_x = begin_x;
704 view->window = newwin(nlines, ncols, begin_y, begin_x);
705 if (view->window == NULL) {
709 view->panel = new_panel(view->window);
710 if (view->panel == NULL ||
711 set_panel_userptr(view->panel, view) != OK) {
716 keypad(view->window, TRUE);
721 view_split_begin_x(int begin_x)
723 if (begin_x > 0 || COLS < 120)
725 return (COLS - MAX(COLS / 2, 80));
728 /* XXX Stub till we decide what to do. */
730 view_split_begin_y(int lines)
732 return lines * HSPLIT_SCALE;
735 static const struct got_error *view_resize(struct tog_view *);
737 static const struct got_error *
738 view_splitscreen(struct tog_view *view)
740 const struct got_error *err = NULL;
742 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
743 if (view->resized_y && view->resized_y < view->lines)
744 view->begin_y = view->resized_y;
746 view->begin_y = view_split_begin_y(view->nlines);
748 } else if (!view->resized) {
749 if (view->resized_x && view->resized_x < view->cols - 1 &&
751 view->begin_x = view->resized_x;
753 view->begin_x = view_split_begin_x(0);
756 view->nlines = LINES - view->begin_y;
757 view->ncols = COLS - view->begin_x;
760 err = view_resize(view);
764 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
765 view->parent->nlines = view->begin_y;
767 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
768 return got_error_from_errno("mvwin");
773 static const struct got_error *
774 view_fullscreen(struct tog_view *view)
776 const struct got_error *err = NULL;
779 view->begin_y = view->resized ? view->begin_y : 0;
780 view->nlines = view->resized ? view->nlines : LINES;
784 err = view_resize(view);
788 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
789 return got_error_from_errno("mvwin");
795 view_is_parent_view(struct tog_view *view)
797 return view->parent == NULL;
801 view_is_splitscreen(struct tog_view *view)
803 return view->begin_x > 0 || view->begin_y > 0;
807 view_is_fullscreen(struct tog_view *view)
809 return view->nlines == LINES && view->ncols == COLS;
813 view_is_hsplit_top(struct tog_view *view)
815 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
816 view_is_splitscreen(view->child);
820 view_border(struct tog_view *view)
823 const struct tog_view *view_above;
826 return view_border(view->parent);
828 panel = panel_above(view->panel);
832 view_above = panel_userptr(panel);
833 if (view->mode == TOG_VIEW_SPLIT_HRZN)
834 mvwhline(view->window, view_above->begin_y - 1,
835 view->begin_x, got_locale_is_utf8() ?
836 ACS_HLINE : '-', view->ncols);
838 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
839 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
842 static const struct got_error *view_init_hsplit(struct tog_view *, int);
843 static const struct got_error *request_log_commits(struct tog_view *);
844 static const struct got_error *offset_selection_down(struct tog_view *);
845 static void offset_selection_up(struct tog_view *);
846 static void view_get_split(struct tog_view *, int *, int *);
848 static const struct got_error *
849 view_resize(struct tog_view *view)
851 const struct got_error *err = NULL;
852 int dif, nlines, ncols;
854 dif = LINES - view->lines; /* line difference */
856 if (view->lines > LINES)
857 nlines = view->nlines - (view->lines - LINES);
859 nlines = view->nlines + (LINES - view->lines);
860 if (view->cols > COLS)
861 ncols = view->ncols - (view->cols - COLS);
863 ncols = view->ncols + (COLS - view->cols);
866 int hs = view->child->begin_y;
868 if (!view_is_fullscreen(view))
869 view->child->begin_x = view_split_begin_x(view->begin_x);
870 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
871 view->child->begin_x == 0) {
874 view_fullscreen(view->child);
875 if (view->child->focussed)
876 show_panel(view->child->panel);
878 show_panel(view->panel);
880 ncols = view->child->begin_x;
882 view_splitscreen(view->child);
883 show_panel(view->child->panel);
886 * XXX This is ugly and needs to be moved into the above
887 * logic but "works" for now and my attempts at moving it
888 * break either 'tab' or 'F' key maps in horizontal splits.
891 err = view_splitscreen(view->child);
894 if (dif < 0) { /* top split decreased */
895 err = offset_selection_down(view);
902 show_panel(view->child->panel);
903 nlines = view->nlines;
905 } else if (view->parent == NULL)
908 if (view->resize && dif > 0) {
909 err = view->resize(view, dif);
914 if (wresize(view->window, nlines, ncols) == ERR)
915 return got_error_from_errno("wresize");
916 if (replace_panel(view->panel, view->window) == ERR)
917 return got_error_from_errno("replace_panel");
918 wclear(view->window);
920 view->nlines = nlines;
928 static const struct got_error *
929 resize_log_view(struct tog_view *view, int increase)
931 struct tog_log_view_state *s = &view->state.log;
932 const struct got_error *err = NULL;
933 int n = s->selected_entry->idx + view->lines - s->selected;
936 * Request commits to account for the increased
937 * height so we have enough to populate the view.
939 if (s->commits.ncommits < n) {
940 view->nscrolled = n - s->commits.ncommits + increase + 1;
941 err = request_log_commits(view);
948 view_adjust_offset(struct tog_view *view, int n)
953 if (view->parent && view->parent->offset) {
954 if (view->parent->offset + n >= 0)
955 view->parent->offset += n;
957 view->parent->offset = 0;
958 } else if (view->offset) {
959 if (view->offset - n >= 0)
966 static const struct got_error *
967 view_resize_split(struct tog_view *view, int resize)
969 const struct got_error *err = NULL;
970 struct tog_view *v = NULL;
977 if (!v->child || !view_is_splitscreen(v->child))
980 v->resized = v->child->resized = resize; /* lock for resize event */
982 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
983 int y = v->child->begin_y;
985 if (v->child->resized_y)
986 v->child->begin_y = v->child->resized_y;
988 v->child->begin_y -= resize;
990 v->child->begin_y += resize;
991 if (v->child->begin_y < 3) {
993 v->child->begin_y = 3;
994 } else if (v->child->begin_y > LINES - 1) {
996 v->child->begin_y = LINES - 1;
999 v->child->ncols = COLS;
1000 view_adjust_offset(view, resize);
1001 err = view_init_hsplit(v, v->child->begin_y);
1004 v->child->resized_y = v->child->begin_y;
1005 if (y > v->child->begin_y && v->child->type == TOG_VIEW_LOG)
1006 v->child->nscrolled = y - v->child->begin_y;
1007 else if (y < v->child->begin_y && v->type == TOG_VIEW_LOG)
1008 v->nscrolled = v->child->begin_y - y;
1010 if (v->child->resized_x)
1011 v->child->begin_x = v->child->resized_x;
1013 v->child->begin_x -= resize;
1015 v->child->begin_x += resize;
1016 if (v->child->begin_x < 11) {
1018 v->child->begin_x = 11;
1019 } else if (v->child->begin_x > COLS - 1) {
1021 v->child->begin_x = COLS - 1;
1023 v->child->resized_x = v->child->begin_x;
1026 v->child->mode = v->mode;
1027 v->child->nlines = v->lines - v->child->begin_y;
1028 v->child->ncols = v->cols - v->child->begin_x;
1031 err = view_fullscreen(v);
1034 err = view_splitscreen(v->child);
1038 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1039 err = offset_selection_down(v->child);
1045 err = request_log_commits(v);
1046 else if (v->child->nscrolled)
1047 err = request_log_commits(v->child);
1049 v->resized = v->child->resized = 0;
1055 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1057 struct tog_view *v = src->child ? src->child : src;
1059 dst->resized_x = v->resized_x;
1060 dst->resized_y = v->resized_y;
1063 static const struct got_error *
1064 view_close_child(struct tog_view *view)
1066 const struct got_error *err = NULL;
1068 if (view->child == NULL)
1071 err = view_close(view->child);
1076 static const struct got_error *
1077 view_set_child(struct tog_view *view, struct tog_view *child)
1079 const struct got_error *err = NULL;
1081 view->child = child;
1082 child->parent = view;
1084 err = view_resize(view);
1088 if (view->child->resized_x || view->child->resized_y)
1089 err = view_resize_split(view, 0);
1095 tog_resizeterm(void)
1098 struct winsize size;
1100 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1101 cols = 80; /* Default */
1105 lines = size.ws_row;
1107 resize_term(lines, cols);
1110 static const struct got_error *
1111 view_search_start(struct tog_view *view)
1113 const struct got_error *err = NULL;
1114 struct tog_view *v = view;
1118 if (view->search_started) {
1119 regfree(&view->regex);
1120 view->searching = 0;
1121 memset(&view->regmatch, 0, sizeof(view->regmatch));
1123 view->search_started = 0;
1125 if (view->nlines < 1)
1128 if (view_is_hsplit_top(view))
1131 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1132 wclrtoeol(v->window);
1134 nodelay(view->window, FALSE); /* block for search term input */
1137 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1138 wrefresh(v->window);
1141 nodelay(view->window, TRUE);
1145 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1146 err = view->search_start(view);
1148 regfree(&view->regex);
1151 view->search_started = 1;
1152 view->searching = TOG_SEARCH_FORWARD;
1153 view->search_next_done = 0;
1154 view->search_next(view);
1160 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1161 static const struct got_error *
1162 switch_split(struct tog_view *view)
1164 const struct got_error *err = NULL;
1165 struct tog_view *v = NULL;
1172 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1173 v->mode = TOG_VIEW_SPLIT_VERT;
1175 v->mode = TOG_VIEW_SPLIT_HRZN;
1179 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1180 v->mode = TOG_VIEW_SPLIT_NONE;
1182 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1183 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1184 v->child->begin_y = v->child->resized_y;
1185 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1186 v->child->begin_x = v->child->resized_x;
1189 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1191 v->child->ncols = COLS;
1192 v->child->nscrolled = LINES - v->child->nlines;
1194 err = view_init_hsplit(v, v->child->begin_y);
1198 v->child->mode = v->mode;
1199 v->child->nlines = v->lines - v->child->begin_y;
1202 err = view_fullscreen(v);
1205 err = view_splitscreen(v->child);
1209 if (v->mode == TOG_VIEW_SPLIT_NONE)
1210 v->mode = TOG_VIEW_SPLIT_VERT;
1211 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1212 err = offset_selection_down(v);
1213 err = offset_selection_down(v->child);
1215 offset_selection_up(v);
1216 offset_selection_up(v->child);
1218 if (v->type == TOG_VIEW_LOG && v->nscrolled)
1219 err = request_log_commits(v);
1220 else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1221 err = request_log_commits(v->child);
1227 * Compute view->count from numeric input. Assign total to view->count and
1228 * return first non-numeric key entered.
1231 get_compound_key(struct tog_view *view, int c)
1233 struct tog_view *v = view;
1236 if (view_is_hsplit_top(view))
1238 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1242 cbreak(); /* block for input */
1243 wmove(v->window, v->nlines - 1, 0);
1244 wclrtoeol(v->window);
1245 waddch(v->window, ':');
1248 x = getcurx(v->window);
1249 if (x != ERR && x < view->ncols) {
1250 waddch(v->window, c);
1251 wrefresh(v->window);
1255 * Don't overflow. Max valid request should be the greatest
1256 * between the longest and total lines; cap at 10 million.
1261 n = n * 10 + (c - '0');
1262 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1264 /* Massage excessive or inapplicable values at the input handler. */
1270 static const struct got_error *
1271 view_input(struct tog_view **new, int *done, struct tog_view *view,
1272 struct tog_view_list_head *views)
1274 const struct got_error *err = NULL;
1280 /* Clear "no matches" indicator. */
1281 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1282 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1283 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1287 if (view->searching && !view->search_next_done) {
1288 errcode = pthread_mutex_unlock(&tog_mutex);
1290 return got_error_set_errno(errcode,
1291 "pthread_mutex_unlock");
1293 errcode = pthread_mutex_lock(&tog_mutex);
1295 return got_error_set_errno(errcode,
1296 "pthread_mutex_lock");
1297 view->search_next(view);
1301 nodelay(view->window, FALSE);
1302 /* Allow threads to make progress while we are waiting for input. */
1303 errcode = pthread_mutex_unlock(&tog_mutex);
1305 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1306 /* If we have an unfinished count, let C-g or backspace abort. */
1307 if (view->count && --view->count) {
1309 nodelay(view->window, TRUE);
1310 ch = wgetch(view->window);
1311 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1316 ch = wgetch(view->window);
1317 if (ch >= '1' && ch <= '9')
1318 view->ch = ch = get_compound_key(view, ch);
1320 errcode = pthread_mutex_lock(&tog_mutex);
1322 return got_error_set_errno(errcode, "pthread_mutex_lock");
1323 nodelay(view->window, TRUE);
1325 if (tog_sigwinch_received || tog_sigcont_received) {
1327 tog_sigwinch_received = 0;
1328 tog_sigcont_received = 0;
1329 TAILQ_FOREACH(v, views, entry) {
1330 err = view_resize(v);
1333 err = v->input(new, v, KEY_RESIZE);
1337 err = view_resize(v->child);
1340 err = v->child->input(new, v->child,
1344 if (v->child->resized_x || v->child->resized_y) {
1345 err = view_resize_split(v, 0);
1358 view->child->focussed = 1;
1359 view->focus_child = 1;
1360 } else if (view->parent) {
1362 view->parent->focussed = 1;
1363 view->parent->focus_child = 0;
1364 if (!view_is_splitscreen(view)) {
1365 if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1366 view->parent->type == TOG_VIEW_LOG) {
1367 err = request_log_commits(view->parent);
1371 offset_selection_up(view->parent);
1372 err = view_fullscreen(view->parent);
1379 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1380 if (view->parent->type == TOG_VIEW_LOG) {
1381 /* might need more commits to fill fullscreen */
1382 err = request_log_commits(view->parent);
1386 offset_selection_up(view->parent);
1388 err = view->input(new, view, ch);
1396 if (view_is_parent_view(view)) {
1397 if (view->child == NULL)
1399 if (view_is_splitscreen(view->child)) {
1401 view->child->focussed = 1;
1402 err = view_fullscreen(view->child);
1404 err = view_splitscreen(view->child);
1406 err = view_resize_split(view, 0);
1410 err = view->child->input(new, view->child,
1413 if (view_is_splitscreen(view)) {
1414 view->parent->focussed = 0;
1416 err = view_fullscreen(view);
1418 err = view_splitscreen(view);
1419 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1420 err = view_resize(view->parent);
1422 err = view_resize_split(view, 0);
1426 err = view->input(new, view, KEY_RESIZE);
1430 if (view->type == TOG_VIEW_LOG) {
1431 err = request_log_commits(view);
1436 err = offset_selection_down(view->parent);
1438 err = offset_selection_down(view);
1442 err = switch_split(view);
1445 err = view_resize_split(view, -1);
1448 err = view_resize_split(view, 1);
1454 if (view->search_start)
1455 view_search_start(view);
1457 err = view->input(new, view, ch);
1461 if (view->search_started && view->search_next) {
1462 view->searching = (ch == 'n' ?
1463 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1464 view->search_next_done = 0;
1465 view->search_next(view);
1467 err = view->input(new, view, ch);
1470 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1471 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1473 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1474 TAILQ_FOREACH(v, views, entry) {
1480 if (v->child && v->child->reset) {
1481 err = v->child->reset(v->child);
1488 err = view->input(new, view, ch);
1496 view_needs_focus_indication(struct tog_view *view)
1498 if (view_is_parent_view(view)) {
1499 if (view->child == NULL || view->child->focussed)
1501 if (!view_is_splitscreen(view->child))
1503 } else if (!view_is_splitscreen(view))
1506 return view->focussed;
1509 static const struct got_error *
1510 view_loop(struct tog_view *view)
1512 const struct got_error *err = NULL;
1513 struct tog_view_list_head views;
1514 struct tog_view *new_view;
1516 int fast_refresh = 10;
1517 int done = 0, errcode;
1519 mode = getenv("TOG_VIEW_SPLIT_MODE");
1520 if (!mode || !(*mode == 'h' || *mode == 'H'))
1521 view->mode = TOG_VIEW_SPLIT_VERT;
1523 view->mode = TOG_VIEW_SPLIT_HRZN;
1525 errcode = pthread_mutex_lock(&tog_mutex);
1527 return got_error_set_errno(errcode, "pthread_mutex_lock");
1530 TAILQ_INSERT_HEAD(&views, view, entry);
1533 err = view->show(view);
1538 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1539 !tog_fatal_signal_received()) {
1540 /* Refresh fast during initialization, then become slower. */
1541 if (fast_refresh && fast_refresh-- == 0)
1542 halfdelay(10); /* switch to once per second */
1544 err = view_input(&new_view, &done, view, &views);
1548 struct tog_view *v, *prev = NULL;
1550 if (view_is_parent_view(view))
1551 prev = TAILQ_PREV(view, tog_view_list_head,
1553 else if (view->parent)
1554 prev = view->parent;
1557 view->parent->child = NULL;
1558 view->parent->focus_child = 0;
1559 /* Restore fullscreen line height. */
1560 view->parent->nlines = view->parent->lines;
1561 err = view_resize(view->parent);
1564 /* Make resized splits persist. */
1565 view_transfer_size(view->parent, view);
1567 TAILQ_REMOVE(&views, view, entry);
1569 err = view_close(view);
1574 TAILQ_FOREACH(v, &views, entry) {
1578 if (view == NULL && new_view == NULL) {
1579 /* No view has focus. Try to pick one. */
1582 else if (!TAILQ_EMPTY(&views)) {
1583 view = TAILQ_LAST(&views,
1584 tog_view_list_head);
1587 if (view->focus_child) {
1588 view->child->focussed = 1;
1596 struct tog_view *v, *t;
1597 /* Only allow one parent view per type. */
1598 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1599 if (v->type != new_view->type)
1601 TAILQ_REMOVE(&views, v, entry);
1602 err = view_close(v);
1607 TAILQ_INSERT_TAIL(&views, new_view, entry);
1611 if (view_is_parent_view(view)) {
1612 if (view->child && view->child->focussed)
1615 if (view->parent && view->parent->focussed)
1616 view = view->parent;
1618 show_panel(view->panel);
1619 if (view->child && view_is_splitscreen(view->child))
1620 show_panel(view->child->panel);
1621 if (view->parent && view_is_splitscreen(view)) {
1622 err = view->parent->show(view->parent);
1626 err = view->show(view);
1630 err = view->child->show(view->child);
1639 while (!TAILQ_EMPTY(&views)) {
1640 const struct got_error *close_err;
1641 view = TAILQ_FIRST(&views);
1642 TAILQ_REMOVE(&views, view, entry);
1643 close_err = view_close(view);
1644 if (close_err && err == NULL)
1648 errcode = pthread_mutex_unlock(&tog_mutex);
1649 if (errcode && err == NULL)
1650 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1660 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1665 /* Create newly allocated wide-character string equivalent to a byte string. */
1666 static const struct got_error *
1667 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1670 const struct got_error *err = NULL;
1673 *wlen = mbstowcs(NULL, s, 0);
1674 if (*wlen == (size_t)-1) {
1676 if (errno != EILSEQ)
1677 return got_error_from_errno("mbstowcs");
1679 /* byte string invalid in current encoding; try to "fix" it */
1680 err = got_mbsavis(&vis, &vislen, s);
1683 *wlen = mbstowcs(NULL, vis, 0);
1684 if (*wlen == (size_t)-1) {
1685 err = got_error_from_errno("mbstowcs"); /* give up */
1690 *ws = calloc(*wlen + 1, sizeof(**ws));
1692 err = got_error_from_errno("calloc");
1696 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1697 err = got_error_from_errno("mbstowcs");
1708 static const struct got_error *
1709 expand_tab(char **ptr, const char *src)
1712 size_t len, n, idx = 0, sz = 0;
1715 n = len = strlen(src);
1716 dst = malloc(n + 1);
1718 return got_error_from_errno("malloc");
1720 while (idx < len && src[idx]) {
1721 const char c = src[idx];
1724 size_t nb = TABSIZE - sz % TABSIZE;
1727 p = realloc(dst, n + nb);
1730 return got_error_from_errno("realloc");
1735 memset(dst + sz, ' ', nb);
1738 dst[sz++] = src[idx];
1748 * Advance at most n columns from wline starting at offset off.
1749 * Return the index to the first character after the span operation.
1750 * Return the combined column width of all spanned wide character in
1754 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1756 int width, i, cols = 0;
1763 for (i = off; wline[i] != L'\0'; ++i) {
1764 if (wline[i] == L'\t')
1765 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1767 width = wcwidth(wline[i]);
1774 if (cols + width > n)
1784 * Format a line for display, ensuring that it won't overflow a width limit.
1785 * With scrolling, the width returned refers to the scrolled version of the
1786 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1788 static const struct got_error *
1789 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1790 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1792 const struct got_error *err = NULL;
1794 wchar_t *wline = NULL;
1803 err = expand_tab(&exstr, line);
1808 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1813 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1815 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1816 wline[wlen - 1] = L'\0';
1819 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1820 wline[wlen - 1] = L'\0';
1824 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1830 *scrollxp = scrollx;
1838 static const struct got_error*
1839 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1840 struct got_object_id *id, struct got_repository *repo)
1842 static const struct got_error *err = NULL;
1843 struct got_reflist_entry *re;
1849 TAILQ_FOREACH(re, refs, entry) {
1850 struct got_tag_object *tag = NULL;
1851 struct got_object_id *ref_id;
1854 name = got_ref_get_name(re->ref);
1855 if (strcmp(name, GOT_REF_HEAD) == 0)
1857 if (strncmp(name, "refs/", 5) == 0)
1859 if (strncmp(name, "got/", 4) == 0 &&
1860 strncmp(name, "got/backup/", 11) != 0)
1862 if (strncmp(name, "heads/", 6) == 0)
1864 if (strncmp(name, "remotes/", 8) == 0) {
1866 s = strstr(name, "/" GOT_REF_HEAD);
1867 if (s != NULL && s[strlen(s)] == '\0')
1870 err = got_ref_resolve(&ref_id, repo, re->ref);
1873 if (strncmp(name, "tags/", 5) == 0) {
1874 err = got_object_open_as_tag(&tag, repo, ref_id);
1876 if (err->code != GOT_ERR_OBJ_TYPE) {
1880 /* Ref points at something other than a tag. */
1885 cmp = got_object_id_cmp(tag ?
1886 got_object_tag_get_object_id(tag) : ref_id, id);
1889 got_object_tag_close(tag);
1893 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1894 s ? ", " : "", name) == -1) {
1895 err = got_error_from_errno("asprintf");
1906 static const struct got_error *
1907 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1912 smallerthan = strchr(author, '<');
1913 if (smallerthan && smallerthan[1] != '\0')
1914 author = smallerthan + 1;
1915 author[strcspn(author, "@>")] = '\0';
1916 return format_line(wauthor, author_width, NULL, author, 0, limit,
1920 static const struct got_error *
1921 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1922 struct got_object_id *id, const size_t date_display_cols,
1923 int author_display_cols)
1925 struct tog_log_view_state *s = &view->state.log;
1926 const struct got_error *err = NULL;
1927 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1928 char *logmsg0 = NULL, *logmsg = NULL;
1929 char *author = NULL;
1930 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1931 int author_width, logmsg_width;
1932 char *newline, *line = NULL;
1933 int col, limit, scrollx;
1934 const int avail = view->ncols;
1936 time_t committer_time;
1937 struct tog_color *tc;
1939 committer_time = got_object_commit_get_committer_time(commit);
1940 if (gmtime_r(&committer_time, &tm) == NULL)
1941 return got_error_from_errno("gmtime_r");
1942 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1943 return got_error(GOT_ERR_NO_SPACE);
1945 if (avail <= date_display_cols)
1946 limit = MIN(sizeof(datebuf) - 1, avail);
1948 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1949 tc = get_color(&s->colors, TOG_COLOR_DATE);
1951 wattr_on(view->window,
1952 COLOR_PAIR(tc->colorpair), NULL);
1953 waddnstr(view->window, datebuf, limit);
1955 wattr_off(view->window,
1956 COLOR_PAIR(tc->colorpair), NULL);
1963 err = got_object_id_str(&id_str, id);
1966 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1968 wattr_on(view->window,
1969 COLOR_PAIR(tc->colorpair), NULL);
1970 wprintw(view->window, "%.8s ", id_str);
1972 wattr_off(view->window,
1973 COLOR_PAIR(tc->colorpair), NULL);
1980 if (s->use_committer)
1981 author = strdup(got_object_commit_get_committer(commit));
1983 author = strdup(got_object_commit_get_author(commit));
1984 if (author == NULL) {
1985 err = got_error_from_errno("strdup");
1988 err = format_author(&wauthor, &author_width, author, avail - col, col);
1991 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1993 wattr_on(view->window,
1994 COLOR_PAIR(tc->colorpair), NULL);
1995 waddwstr(view->window, wauthor);
1997 wattr_off(view->window,
1998 COLOR_PAIR(tc->colorpair), NULL);
1999 col += author_width;
2000 while (col < avail && author_width < author_display_cols + 2) {
2001 waddch(view->window, ' ');
2008 err = got_object_commit_get_logmsg(&logmsg0, commit);
2012 while (*logmsg == '\n')
2014 newline = strchr(logmsg, '\n');
2017 limit = avail - col;
2018 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2019 limit--; /* for the border */
2020 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2024 waddwstr(view->window, &wlogmsg[scrollx]);
2025 col += MAX(logmsg_width, 0);
2026 while (col < avail) {
2027 waddch(view->window, ' ');
2039 static struct commit_queue_entry *
2040 alloc_commit_queue_entry(struct got_commit_object *commit,
2041 struct got_object_id *id)
2043 struct commit_queue_entry *entry;
2045 entry = calloc(1, sizeof(*entry));
2050 entry->commit = commit;
2055 pop_commit(struct commit_queue *commits)
2057 struct commit_queue_entry *entry;
2059 entry = TAILQ_FIRST(&commits->head);
2060 TAILQ_REMOVE(&commits->head, entry, entry);
2061 got_object_commit_close(entry->commit);
2062 commits->ncommits--;
2063 /* Don't free entry->id! It is owned by the commit graph. */
2068 free_commits(struct commit_queue *commits)
2070 while (!TAILQ_EMPTY(&commits->head))
2071 pop_commit(commits);
2074 static const struct got_error *
2075 match_commit(int *have_match, struct got_object_id *id,
2076 struct got_commit_object *commit, regex_t *regex)
2078 const struct got_error *err = NULL;
2079 regmatch_t regmatch;
2080 char *id_str = NULL, *logmsg = NULL;
2084 err = got_object_id_str(&id_str, id);
2088 err = got_object_commit_get_logmsg(&logmsg, commit);
2092 if (regexec(regex, got_object_commit_get_author(commit), 1,
2093 ®match, 0) == 0 ||
2094 regexec(regex, got_object_commit_get_committer(commit), 1,
2095 ®match, 0) == 0 ||
2096 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2097 regexec(regex, logmsg, 1, ®match, 0) == 0)
2105 static const struct got_error *
2106 queue_commits(struct tog_log_thread_args *a)
2108 const struct got_error *err = NULL;
2111 * We keep all commits open throughout the lifetime of the log
2112 * view in order to avoid having to re-fetch commits from disk
2113 * while updating the display.
2116 struct got_object_id *id;
2117 struct got_commit_object *commit;
2118 struct commit_queue_entry *entry;
2121 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2123 if (err || id == NULL)
2126 err = got_object_open_as_commit(&commit, a->repo, id);
2129 entry = alloc_commit_queue_entry(commit, id);
2130 if (entry == NULL) {
2131 err = got_error_from_errno("alloc_commit_queue_entry");
2135 errcode = pthread_mutex_lock(&tog_mutex);
2137 err = got_error_set_errno(errcode,
2138 "pthread_mutex_lock");
2142 entry->idx = a->commits->ncommits;
2143 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2144 a->commits->ncommits++;
2146 if (*a->searching == TOG_SEARCH_FORWARD &&
2147 !*a->search_next_done) {
2149 err = match_commit(&have_match, id, commit, a->regex);
2153 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2156 errcode = pthread_mutex_unlock(&tog_mutex);
2157 if (errcode && err == NULL)
2158 err = got_error_set_errno(errcode,
2159 "pthread_mutex_unlock");
2162 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2168 select_commit(struct tog_log_view_state *s)
2170 struct commit_queue_entry *entry;
2173 entry = s->first_displayed_entry;
2175 if (ncommits == s->selected) {
2176 s->selected_entry = entry;
2179 entry = TAILQ_NEXT(entry, entry);
2184 static const struct got_error *
2185 draw_commits(struct tog_view *view)
2187 const struct got_error *err = NULL;
2188 struct tog_log_view_state *s = &view->state.log;
2189 struct commit_queue_entry *entry = s->selected_entry;
2190 const int limit = view->nlines;
2192 int ncommits, author_cols = 4;
2193 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2194 char *refs_str = NULL;
2196 struct tog_color *tc;
2197 static const size_t date_display_cols = 12;
2199 if (s->selected_entry &&
2200 !(view->searching && view->search_next_done == 0)) {
2201 struct got_reflist_head *refs;
2202 err = got_object_id_str(&id_str, s->selected_entry->id);
2205 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2206 s->selected_entry->id);
2208 err = build_refs_str(&refs_str, refs,
2209 s->selected_entry->id, s->repo);
2215 if (s->thread_args.commits_needed == 0)
2216 halfdelay(10); /* disable fast refresh */
2218 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2219 if (asprintf(&ncommits_str, " [%d/%d] %s",
2220 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2221 (view->searching && !view->search_next_done) ?
2222 "searching..." : "loading...") == -1) {
2223 err = got_error_from_errno("asprintf");
2227 const char *search_str = NULL;
2229 if (view->searching) {
2230 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2231 search_str = "no more matches";
2232 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2233 search_str = "no matches found";
2234 else if (!view->search_next_done)
2235 search_str = "searching...";
2238 if (asprintf(&ncommits_str, " [%d/%d] %s",
2239 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2240 search_str ? search_str :
2241 (refs_str ? refs_str : "")) == -1) {
2242 err = got_error_from_errno("asprintf");
2247 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2248 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2249 "........................................",
2250 s->in_repo_path, ncommits_str) == -1) {
2251 err = got_error_from_errno("asprintf");
2255 } else if (asprintf(&header, "commit %s%s",
2256 id_str ? id_str : "........................................",
2257 ncommits_str) == -1) {
2258 err = got_error_from_errno("asprintf");
2262 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2266 werase(view->window);
2268 if (view_needs_focus_indication(view))
2269 wstandout(view->window);
2270 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2272 wattr_on(view->window,
2273 COLOR_PAIR(tc->colorpair), NULL);
2274 waddwstr(view->window, wline);
2276 wattr_off(view->window,
2277 COLOR_PAIR(tc->colorpair), NULL);
2278 while (width < view->ncols) {
2279 waddch(view->window, ' ');
2282 if (view_needs_focus_indication(view))
2283 wstandend(view->window);
2288 /* Grow author column size if necessary, and set view->maxx. */
2289 entry = s->first_displayed_entry;
2293 struct got_commit_object *c = entry->commit;
2294 char *author, *eol, *msg, *msg0;
2295 wchar_t *wauthor, *wmsg;
2297 if (ncommits >= limit - 1)
2299 if (s->use_committer)
2300 author = strdup(got_object_commit_get_committer(c));
2302 author = strdup(got_object_commit_get_author(c));
2303 if (author == NULL) {
2304 err = got_error_from_errno("strdup");
2307 err = format_author(&wauthor, &width, author, COLS,
2309 if (author_cols < width)
2310 author_cols = width;
2313 err = got_object_commit_get_logmsg(&msg0, c);
2317 while (*msg == '\n')
2319 if ((eol = strchr(msg, '\n')))
2321 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2322 date_display_cols + author_cols, 0);
2325 view->maxx = MAX(view->maxx, width);
2329 entry = TAILQ_NEXT(entry, entry);
2332 entry = s->first_displayed_entry;
2333 s->last_displayed_entry = s->first_displayed_entry;
2336 if (ncommits >= limit - 1)
2338 if (ncommits == s->selected)
2339 wstandout(view->window);
2340 err = draw_commit(view, entry->commit, entry->id,
2341 date_display_cols, author_cols);
2342 if (ncommits == s->selected)
2343 wstandend(view->window);
2347 s->last_displayed_entry = entry;
2348 entry = TAILQ_NEXT(entry, entry);
2361 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2363 struct commit_queue_entry *entry;
2366 entry = TAILQ_FIRST(&s->commits.head);
2367 if (s->first_displayed_entry == entry)
2370 entry = s->first_displayed_entry;
2371 while (entry && nscrolled < maxscroll) {
2372 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2374 s->first_displayed_entry = entry;
2380 static const struct got_error *
2381 trigger_log_thread(struct tog_view *view, int wait)
2383 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2386 halfdelay(1); /* fast refresh while loading commits */
2388 while (!ta->log_complete && !tog_thread_error &&
2389 (ta->commits_needed > 0 || ta->load_all)) {
2390 /* Wake the log thread. */
2391 errcode = pthread_cond_signal(&ta->need_commits);
2393 return got_error_set_errno(errcode,
2394 "pthread_cond_signal");
2397 * The mutex will be released while the view loop waits
2398 * in wgetch(), at which time the log thread will run.
2403 /* Display progress update in log view. */
2404 show_log_view(view);
2408 /* Wait right here while next commit is being loaded. */
2409 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2411 return got_error_set_errno(errcode,
2412 "pthread_cond_wait");
2414 /* Display progress update in log view. */
2415 show_log_view(view);
2423 static const struct got_error *
2424 request_log_commits(struct tog_view *view)
2426 struct tog_log_view_state *state = &view->state.log;
2427 const struct got_error *err = NULL;
2429 if (state->thread_args.log_complete)
2432 state->thread_args.commits_needed += view->nscrolled;
2433 err = trigger_log_thread(view, 1);
2434 view->nscrolled = 0;
2439 static const struct got_error *
2440 log_scroll_down(struct tog_view *view, int maxscroll)
2442 struct tog_log_view_state *s = &view->state.log;
2443 const struct got_error *err = NULL;
2444 struct commit_queue_entry *pentry;
2445 int nscrolled = 0, ncommits_needed;
2447 if (s->last_displayed_entry == NULL)
2450 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2451 if (s->commits.ncommits < ncommits_needed &&
2452 !s->thread_args.log_complete) {
2454 * Ask the log thread for required amount of commits.
2456 s->thread_args.commits_needed += maxscroll;
2457 err = trigger_log_thread(view, 1);
2463 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2464 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2467 s->last_displayed_entry = pentry ?
2468 pentry : s->last_displayed_entry;;
2470 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2473 s->first_displayed_entry = pentry;
2474 } while (++nscrolled < maxscroll);
2476 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2477 view->nscrolled += nscrolled;
2479 view->nscrolled = 0;
2484 static const struct got_error *
2485 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2486 struct got_commit_object *commit, struct got_object_id *commit_id,
2487 struct tog_view *log_view, struct got_repository *repo)
2489 const struct got_error *err;
2490 struct got_object_qid *parent_id;
2491 struct tog_view *diff_view;
2493 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2494 if (diff_view == NULL)
2495 return got_error_from_errno("view_open");
2497 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2498 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2499 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2501 *new_view = diff_view;
2505 static const struct got_error *
2506 tree_view_visit_subtree(struct tog_tree_view_state *s,
2507 struct got_tree_object *subtree)
2509 struct tog_parent_tree *parent;
2511 parent = calloc(1, sizeof(*parent));
2513 return got_error_from_errno("calloc");
2515 parent->tree = s->tree;
2516 parent->first_displayed_entry = s->first_displayed_entry;
2517 parent->selected_entry = s->selected_entry;
2518 parent->selected = s->selected;
2519 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2522 s->first_displayed_entry = NULL;
2526 static const struct got_error *
2527 tree_view_walk_path(struct tog_tree_view_state *s,
2528 struct got_commit_object *commit, const char *path)
2530 const struct got_error *err = NULL;
2531 struct got_tree_object *tree = NULL;
2533 char *slash, *subpath = NULL;
2535 /* Walk the path and open corresponding tree objects. */
2538 struct got_tree_entry *te;
2539 struct got_object_id *tree_id;
2545 /* Ensure the correct subtree entry is selected. */
2546 slash = strchr(p, '/');
2548 te_name = strdup(p);
2550 te_name = strndup(p, slash - p);
2551 if (te_name == NULL) {
2552 err = got_error_from_errno("strndup");
2555 te = got_object_tree_find_entry(s->tree, te_name);
2557 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2562 s->first_displayed_entry = s->selected_entry = te;
2564 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2565 break; /* jump to this file's entry */
2567 slash = strchr(p, '/');
2569 subpath = strndup(path, slash - path);
2571 subpath = strdup(path);
2572 if (subpath == NULL) {
2573 err = got_error_from_errno("strdup");
2577 err = got_object_id_by_path(&tree_id, s->repo, commit,
2582 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2587 err = tree_view_visit_subtree(s, tree);
2589 got_object_tree_close(tree);
2603 static const struct got_error *
2604 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2605 struct commit_queue_entry *entry, const char *path,
2606 const char *head_ref_name, struct got_repository *repo)
2608 const struct got_error *err = NULL;
2609 struct tog_tree_view_state *s;
2610 struct tog_view *tree_view;
2612 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2613 if (tree_view == NULL)
2614 return got_error_from_errno("view_open");
2616 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2619 s = &tree_view->state.tree;
2621 *new_view = tree_view;
2623 if (got_path_is_root_dir(path))
2626 return tree_view_walk_path(s, entry->commit, path);
2629 static const struct got_error *
2630 block_signals_used_by_main_thread(void)
2635 if (sigemptyset(&sigset) == -1)
2636 return got_error_from_errno("sigemptyset");
2638 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2639 if (sigaddset(&sigset, SIGWINCH) == -1)
2640 return got_error_from_errno("sigaddset");
2641 if (sigaddset(&sigset, SIGCONT) == -1)
2642 return got_error_from_errno("sigaddset");
2643 if (sigaddset(&sigset, SIGINT) == -1)
2644 return got_error_from_errno("sigaddset");
2645 if (sigaddset(&sigset, SIGTERM) == -1)
2646 return got_error_from_errno("sigaddset");
2648 /* ncurses handles SIGTSTP */
2649 if (sigaddset(&sigset, SIGTSTP) == -1)
2650 return got_error_from_errno("sigaddset");
2652 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2654 return got_error_set_errno(errcode, "pthread_sigmask");
2660 log_thread(void *arg)
2662 const struct got_error *err = NULL;
2664 struct tog_log_thread_args *a = arg;
2668 * Sync startup with main thread such that we begin our
2669 * work once view_input() has released the mutex.
2671 errcode = pthread_mutex_lock(&tog_mutex);
2673 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2677 err = block_signals_used_by_main_thread();
2679 pthread_mutex_unlock(&tog_mutex);
2683 while (!done && !err && !tog_fatal_signal_received()) {
2684 errcode = pthread_mutex_unlock(&tog_mutex);
2686 err = got_error_set_errno(errcode,
2687 "pthread_mutex_unlock");
2690 err = queue_commits(a);
2692 if (err->code != GOT_ERR_ITER_COMPLETED)
2696 } else if (a->commits_needed > 0 && !a->load_all)
2697 a->commits_needed--;
2699 errcode = pthread_mutex_lock(&tog_mutex);
2701 err = got_error_set_errno(errcode,
2702 "pthread_mutex_lock");
2704 } else if (*a->quit)
2706 else if (*a->first_displayed_entry == NULL) {
2707 *a->first_displayed_entry =
2708 TAILQ_FIRST(&a->commits->head);
2709 *a->selected_entry = *a->first_displayed_entry;
2712 errcode = pthread_cond_signal(&a->commit_loaded);
2714 err = got_error_set_errno(errcode,
2715 "pthread_cond_signal");
2716 pthread_mutex_unlock(&tog_mutex);
2721 a->commits_needed = 0;
2723 if (a->commits_needed == 0 && !a->load_all) {
2724 errcode = pthread_cond_wait(&a->need_commits,
2727 err = got_error_set_errno(errcode,
2728 "pthread_cond_wait");
2729 pthread_mutex_unlock(&tog_mutex);
2737 a->log_complete = 1;
2738 errcode = pthread_mutex_unlock(&tog_mutex);
2740 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2743 tog_thread_error = 1;
2744 pthread_cond_signal(&a->commit_loaded);
2749 static const struct got_error *
2750 stop_log_thread(struct tog_log_view_state *s)
2752 const struct got_error *err = NULL, *thread_err = NULL;
2757 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2759 return got_error_set_errno(errcode,
2760 "pthread_cond_signal");
2761 errcode = pthread_mutex_unlock(&tog_mutex);
2763 return got_error_set_errno(errcode,
2764 "pthread_mutex_unlock");
2765 errcode = pthread_join(s->thread, (void **)&thread_err);
2767 return got_error_set_errno(errcode, "pthread_join");
2768 errcode = pthread_mutex_lock(&tog_mutex);
2770 return got_error_set_errno(errcode,
2771 "pthread_mutex_lock");
2772 s->thread = 0; //NULL;
2775 if (s->thread_args.repo) {
2776 err = got_repo_close(s->thread_args.repo);
2777 s->thread_args.repo = NULL;
2780 if (s->thread_args.pack_fds) {
2781 const struct got_error *pack_err =
2782 got_repo_pack_fds_close(s->thread_args.pack_fds);
2785 s->thread_args.pack_fds = NULL;
2788 if (s->thread_args.graph) {
2789 got_commit_graph_close(s->thread_args.graph);
2790 s->thread_args.graph = NULL;
2793 return err ? err : thread_err;
2796 static const struct got_error *
2797 close_log_view(struct tog_view *view)
2799 const struct got_error *err = NULL;
2800 struct tog_log_view_state *s = &view->state.log;
2803 err = stop_log_thread(s);
2805 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2806 if (errcode && err == NULL)
2807 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2809 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2810 if (errcode && err == NULL)
2811 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2813 free_commits(&s->commits);
2814 free(s->in_repo_path);
2815 s->in_repo_path = NULL;
2818 free(s->head_ref_name);
2819 s->head_ref_name = NULL;
2823 static const struct got_error *
2824 search_start_log_view(struct tog_view *view)
2826 struct tog_log_view_state *s = &view->state.log;
2828 s->matched_entry = NULL;
2829 s->search_entry = NULL;
2833 static const struct got_error *
2834 search_next_log_view(struct tog_view *view)
2836 const struct got_error *err = NULL;
2837 struct tog_log_view_state *s = &view->state.log;
2838 struct commit_queue_entry *entry;
2840 /* Display progress update in log view. */
2841 show_log_view(view);
2845 if (s->search_entry) {
2847 errcode = pthread_mutex_unlock(&tog_mutex);
2849 return got_error_set_errno(errcode,
2850 "pthread_mutex_unlock");
2851 ch = wgetch(view->window);
2852 errcode = pthread_mutex_lock(&tog_mutex);
2854 return got_error_set_errno(errcode,
2855 "pthread_mutex_lock");
2856 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2857 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2860 if (view->searching == TOG_SEARCH_FORWARD)
2861 entry = TAILQ_NEXT(s->search_entry, entry);
2863 entry = TAILQ_PREV(s->search_entry,
2864 commit_queue_head, entry);
2865 } else if (s->matched_entry) {
2866 int matched_idx = s->matched_entry->idx;
2867 int selected_idx = s->selected_entry->idx;
2870 * If the user has moved the cursor after we hit a match,
2871 * the position from where we should continue searching
2872 * might have changed.
2874 if (view->searching == TOG_SEARCH_FORWARD) {
2875 if (matched_idx > selected_idx)
2876 entry = TAILQ_NEXT(s->selected_entry, entry);
2878 entry = TAILQ_NEXT(s->matched_entry, entry);
2880 if (matched_idx < selected_idx)
2881 entry = TAILQ_PREV(s->selected_entry,
2882 commit_queue_head, entry);
2884 entry = TAILQ_PREV(s->matched_entry,
2885 commit_queue_head, entry);
2888 entry = s->selected_entry;
2894 if (entry == NULL) {
2895 if (s->thread_args.log_complete ||
2896 view->searching == TOG_SEARCH_BACKWARD) {
2897 view->search_next_done =
2898 (s->matched_entry == NULL ?
2899 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2900 s->search_entry = NULL;
2904 * Poke the log thread for more commits and return,
2905 * allowing the main loop to make progress. Search
2906 * will resume at s->search_entry once we come back.
2908 s->thread_args.commits_needed++;
2909 return trigger_log_thread(view, 0);
2912 err = match_commit(&have_match, entry->id, entry->commit,
2917 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2918 s->matched_entry = entry;
2922 s->search_entry = entry;
2923 if (view->searching == TOG_SEARCH_FORWARD)
2924 entry = TAILQ_NEXT(entry, entry);
2926 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2929 if (s->matched_entry) {
2930 int cur = s->selected_entry->idx;
2931 while (cur < s->matched_entry->idx) {
2932 err = input_log_view(NULL, view, KEY_DOWN);
2937 while (cur > s->matched_entry->idx) {
2938 err = input_log_view(NULL, view, KEY_UP);
2945 s->search_entry = NULL;
2950 static const struct got_error *
2951 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2952 struct got_repository *repo, const char *head_ref_name,
2953 const char *in_repo_path, int log_branches)
2955 const struct got_error *err = NULL;
2956 struct tog_log_view_state *s = &view->state.log;
2957 struct got_repository *thread_repo = NULL;
2958 struct got_commit_graph *thread_graph = NULL;
2961 if (in_repo_path != s->in_repo_path) {
2962 free(s->in_repo_path);
2963 s->in_repo_path = strdup(in_repo_path);
2964 if (s->in_repo_path == NULL)
2965 return got_error_from_errno("strdup");
2968 /* The commit queue only contains commits being displayed. */
2969 TAILQ_INIT(&s->commits.head);
2970 s->commits.ncommits = 0;
2973 if (head_ref_name) {
2974 s->head_ref_name = strdup(head_ref_name);
2975 if (s->head_ref_name == NULL) {
2976 err = got_error_from_errno("strdup");
2980 s->start_id = got_object_id_dup(start_id);
2981 if (s->start_id == NULL) {
2982 err = got_error_from_errno("got_object_id_dup");
2985 s->log_branches = log_branches;
2987 STAILQ_INIT(&s->colors);
2988 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2989 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2990 get_color_value("TOG_COLOR_COMMIT"));
2993 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2994 get_color_value("TOG_COLOR_AUTHOR"));
2996 free_colors(&s->colors);
2999 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3000 get_color_value("TOG_COLOR_DATE"));
3002 free_colors(&s->colors);
3007 view->show = show_log_view;
3008 view->input = input_log_view;
3009 view->resize = resize_log_view;
3010 view->close = close_log_view;
3011 view->search_start = search_start_log_view;
3012 view->search_next = search_next_log_view;
3014 if (s->thread_args.pack_fds == NULL) {
3015 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3019 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3020 s->thread_args.pack_fds);
3023 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3027 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3028 s->repo, NULL, NULL);
3032 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3034 err = got_error_set_errno(errcode, "pthread_cond_init");
3037 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3039 err = got_error_set_errno(errcode, "pthread_cond_init");
3043 s->thread_args.commits_needed = view->nlines;
3044 s->thread_args.graph = thread_graph;
3045 s->thread_args.commits = &s->commits;
3046 s->thread_args.in_repo_path = s->in_repo_path;
3047 s->thread_args.start_id = s->start_id;
3048 s->thread_args.repo = thread_repo;
3049 s->thread_args.log_complete = 0;
3050 s->thread_args.quit = &s->quit;
3051 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3052 s->thread_args.selected_entry = &s->selected_entry;
3053 s->thread_args.searching = &view->searching;
3054 s->thread_args.search_next_done = &view->search_next_done;
3055 s->thread_args.regex = &view->regex;
3058 close_log_view(view);
3062 static const struct got_error *
3063 show_log_view(struct tog_view *view)
3065 const struct got_error *err;
3066 struct tog_log_view_state *s = &view->state.log;
3068 if (s->thread == 0) { //NULL) {
3069 int errcode = pthread_create(&s->thread, NULL, log_thread,
3072 return got_error_set_errno(errcode, "pthread_create");
3073 if (s->thread_args.commits_needed > 0) {
3074 err = trigger_log_thread(view, 1);
3080 return draw_commits(view);
3084 log_move_cursor_up(struct tog_view *view, int page, int home)
3086 struct tog_log_view_state *s = &view->state.log;
3088 if (s->selected_entry->idx == 0)
3090 if (s->first_displayed_entry == NULL)
3093 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3095 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3097 if (!page && !home && s->selected > 0)
3100 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3106 static const struct got_error *
3107 log_move_cursor_down(struct tog_view *view, int page)
3109 struct tog_log_view_state *s = &view->state.log;
3110 struct commit_queue_entry *first;
3111 const struct got_error *err = NULL;
3113 first = s->first_displayed_entry;
3114 if (first == NULL) {
3119 if (s->thread_args.log_complete &&
3120 s->selected_entry->idx >= s->commits.ncommits - 1)
3124 int eos = view->nlines - 2;
3126 if (view_is_hsplit_top(view))
3127 --eos; /* border consumes the last line */
3128 if (s->selected < MIN(eos, s->commits.ncommits - 1))
3131 err = log_scroll_down(view, 1);
3132 } else if (s->thread_args.load_all) {
3133 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3134 s->selected += MIN(s->last_displayed_entry->idx -
3135 s->selected_entry->idx, page + 1);
3137 err = log_scroll_down(view, MIN(page,
3138 s->commits.ncommits - s->selected_entry->idx - 1));
3139 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3141 err = log_scroll_down(view, page);
3144 if (first == s->first_displayed_entry && s->selected <
3145 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3146 s->selected = MIN(s->commits.ncommits - 1, page);
3153 * We might necessarily overshoot in horizontal
3154 * splits; if so, select the last displayed commit.
3156 s->selected = MIN(s->selected,
3157 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3161 if (s->thread_args.log_complete &&
3162 s->selected_entry->idx == s->commits.ncommits - 1)
3169 view_get_split(struct tog_view *view, int *y, int *x)
3174 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3175 if (view->child && view->child->resized_y)
3176 *y = view->child->resized_y;
3177 else if (view->resized_y)
3178 *y = view->resized_y;
3180 *y = view_split_begin_y(view->lines);
3181 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3182 if (view->child && view->child->resized_x)
3183 *x = view->child->resized_x;
3184 else if (view->resized_x)
3185 *x = view->resized_x;
3187 *x = view_split_begin_x(view->begin_x);
3191 /* Split view horizontally at y and offset view->state->selected line. */
3192 static const struct got_error *
3193 view_init_hsplit(struct tog_view *view, int y)
3195 const struct got_error *err = NULL;
3199 err = view_resize(view);
3203 err = offset_selection_down(view);
3208 static const struct got_error *
3209 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3211 const struct got_error *err = NULL;
3212 struct tog_log_view_state *s = &view->state.log;
3213 struct tog_view *diff_view = NULL, *tree_view = NULL;
3214 struct tog_view *ref_view = NULL;
3215 struct commit_queue_entry *entry;
3216 int begin_x = 0, begin_y = 0, eos, n, nscroll;
3218 if (s->thread_args.load_all) {
3219 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3220 s->thread_args.load_all = 0;
3221 else if (s->thread_args.log_complete) {
3222 err = log_move_cursor_down(view, s->commits.ncommits);
3223 s->thread_args.load_all = 0;
3228 eos = nscroll = view->nlines - 1;
3229 if (view_is_hsplit_top(view))
3240 view->x = MAX(view->maxx - view->ncols / 2, 0);
3245 if (view->x + view->ncols / 2 < view->maxx)
3246 view->x += 2; /* move two columns right */
3252 view->x -= MIN(view->x, 2); /* move two columns back */
3261 log_move_cursor_up(view, 0, 0);
3265 log_move_cursor_up(view, 0, 1);
3275 log_move_cursor_up(view, nscroll, 0);
3282 err = log_move_cursor_down(view, 0);
3285 s->use_committer = !s->use_committer;
3289 /* We don't know yet how many commits, so we're forced to
3290 * traverse them all. */
3292 if (!s->thread_args.log_complete) {
3293 s->thread_args.load_all = 1;
3294 return trigger_log_thread(view, 0);
3298 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3299 for (n = 0; n < eos; n++) {
3302 s->first_displayed_entry = entry;
3303 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3306 s->selected = n - 1;
3318 err = log_move_cursor_down(view, nscroll);
3321 if (s->selected > view->nlines - 2)
3322 s->selected = view->nlines - 2;
3323 if (s->selected > s->commits.ncommits - 1)
3324 s->selected = s->commits.ncommits - 1;
3326 if (s->commits.ncommits < view->nlines - 1 &&
3327 !s->thread_args.log_complete) {
3328 s->thread_args.commits_needed += (view->nlines - 1) -
3329 s->commits.ncommits;
3330 err = trigger_log_thread(view, 1);
3336 if (s->selected_entry == NULL)
3339 /* get dimensions--don't split till initialisation succeeds */
3340 if (view_is_parent_view(view))
3341 view_get_split(view, &begin_y, &begin_x);
3343 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3344 s->selected_entry->commit, s->selected_entry->id,
3349 if (view_is_parent_view(view) &&
3350 view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3351 err = view_init_hsplit(view, begin_y);
3357 diff_view->focussed = 1;
3358 diff_view->mode = view->mode;
3359 diff_view->nlines = view->lines - begin_y;
3361 if (view_is_parent_view(view)) {
3362 view_transfer_size(diff_view, view);
3363 err = view_close_child(view);
3366 err = view_set_child(view, diff_view);
3369 view->focus_child = 1;
3371 *new_view = diff_view;
3375 if (s->selected_entry == NULL)
3377 if (view_is_parent_view(view))
3378 view_get_split(view, &begin_y, &begin_x);
3379 err = browse_commit_tree(&tree_view, begin_y, begin_x,
3380 s->selected_entry, s->in_repo_path, s->head_ref_name,
3384 if (view_is_parent_view(view) &&
3385 view->mode == TOG_VIEW_SPLIT_HRZN) {
3386 err = view_init_hsplit(view, begin_y);
3391 tree_view->focussed = 1;
3392 tree_view->mode = view->mode;
3393 tree_view->nlines = view->lines - begin_y;
3394 if (view_is_parent_view(view)) {
3395 view_transfer_size(tree_view, view);
3396 err = view_close_child(view);
3399 err = view_set_child(view, tree_view);
3402 view->focus_child = 1;
3404 *new_view = tree_view;
3410 if (ch == KEY_BACKSPACE &&
3411 got_path_is_root_dir(s->in_repo_path))
3413 err = stop_log_thread(s);
3416 if (ch == KEY_BACKSPACE) {
3418 err = got_path_dirname(&parent_path, s->in_repo_path);
3421 free(s->in_repo_path);
3422 s->in_repo_path = parent_path;
3423 s->thread_args.in_repo_path = s->in_repo_path;
3424 } else if (ch == CTRL('l')) {
3425 struct got_object_id *start_id;
3426 err = got_repo_match_object_id(&start_id, NULL,
3427 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3428 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3432 s->start_id = start_id;
3433 s->thread_args.start_id = s->start_id;
3435 s->log_branches = !s->log_branches;
3437 if (s->thread_args.pack_fds == NULL) {
3438 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3442 err = got_repo_open(&s->thread_args.repo,
3443 got_repo_get_path(s->repo), NULL,
3444 s->thread_args.pack_fds);
3448 err = tog_load_refs(s->repo, 0);
3451 err = got_commit_graph_open(&s->thread_args.graph,
3452 s->in_repo_path, !s->log_branches);
3455 err = got_commit_graph_iter_start(s->thread_args.graph,
3456 s->start_id, s->repo, NULL, NULL);
3459 free_commits(&s->commits);
3460 s->first_displayed_entry = NULL;
3461 s->last_displayed_entry = NULL;
3462 s->selected_entry = NULL;
3464 s->thread_args.log_complete = 0;
3466 s->thread_args.commits_needed = view->lines;
3467 s->matched_entry = NULL;
3468 s->search_entry = NULL;
3472 if (view_is_parent_view(view))
3473 view_get_split(view, &begin_y, &begin_x);
3474 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3475 if (ref_view == NULL)
3476 return got_error_from_errno("view_open");
3477 err = open_ref_view(ref_view, s->repo);
3479 view_close(ref_view);
3482 if (view_is_parent_view(view) &&
3483 view->mode == TOG_VIEW_SPLIT_HRZN) {
3484 err = view_init_hsplit(view, begin_y);
3489 ref_view->focussed = 1;
3490 ref_view->mode = view->mode;
3491 ref_view->nlines = view->lines - begin_y;
3492 if (view_is_parent_view(view)) {
3493 view_transfer_size(ref_view, view);
3494 err = view_close_child(view);
3497 err = view_set_child(view, ref_view);
3500 view->focus_child = 1;
3502 *new_view = ref_view;
3512 static const struct got_error *
3513 apply_unveil(const char *repo_path, const char *worktree_path)
3515 const struct got_error *error;
3518 if (unveil("gmon.out", "rwc") != 0)
3519 return got_error_from_errno2("unveil", "gmon.out");
3521 if (repo_path && unveil(repo_path, "r") != 0)
3522 return got_error_from_errno2("unveil", repo_path);
3524 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3525 return got_error_from_errno2("unveil", worktree_path);
3527 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3528 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3530 error = got_privsep_unveil_exec_helpers();
3534 if (unveil(NULL, NULL) != 0)
3535 return got_error_from_errno("unveil");
3544 * Override default signal handlers before starting ncurses.
3545 * This should prevent ncurses from installing its own
3546 * broken cleanup() signal handler.
3548 signal(SIGWINCH, tog_sigwinch);
3549 signal(SIGPIPE, tog_sigpipe);
3550 signal(SIGCONT, tog_sigcont);
3551 signal(SIGINT, tog_sigint);
3552 signal(SIGTERM, tog_sigterm);
3556 halfdelay(1); /* Do fast refresh while initial view is loading. */
3559 intrflush(stdscr, FALSE);
3560 keypad(stdscr, TRUE);
3562 if (getenv("TOG_COLORS") != NULL) {
3564 use_default_colors();
3568 static const struct got_error *
3569 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3570 struct got_repository *repo, struct got_worktree *worktree)
3572 const struct got_error *err = NULL;
3575 *in_repo_path = strdup("/");
3576 if (*in_repo_path == NULL)
3577 return got_error_from_errno("strdup");
3582 const char *prefix = got_worktree_get_path_prefix(worktree);
3585 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3588 if (asprintf(in_repo_path, "%s%s%s", prefix,
3589 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3591 err = got_error_from_errno("asprintf");
3592 *in_repo_path = NULL;
3596 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3601 static const struct got_error *
3602 cmd_log(int argc, char *argv[])
3604 const struct got_error *error;
3605 struct got_repository *repo = NULL;
3606 struct got_worktree *worktree = NULL;
3607 struct got_object_id *start_id = NULL;
3608 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3609 char *start_commit = NULL, *label = NULL;
3610 struct got_reference *ref = NULL;
3611 const char *head_ref_name = NULL;
3612 int ch, log_branches = 0;
3613 struct tog_view *view;
3614 int *pack_fds = NULL;
3616 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3622 start_commit = optarg;
3625 repo_path = realpath(optarg, NULL);
3626 if (repo_path == NULL)
3627 return got_error_from_errno2("realpath",
3642 error = got_repo_pack_fds_open(&pack_fds);
3646 if (repo_path == NULL) {
3647 cwd = getcwd(NULL, 0);
3649 return got_error_from_errno("getcwd");
3650 error = got_worktree_open(&worktree, cwd);
3651 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3655 strdup(got_worktree_get_repo_path(worktree));
3657 repo_path = strdup(cwd);
3658 if (repo_path == NULL) {
3659 error = got_error_from_errno("strdup");
3664 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3668 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3675 error = apply_unveil(got_repo_get_path(repo),
3676 worktree ? got_worktree_get_root_path(worktree) : NULL);
3680 /* already loaded by tog_log_with_path()? */
3681 if (TAILQ_EMPTY(&tog_refs)) {
3682 error = tog_load_refs(repo, 0);
3687 if (start_commit == NULL) {
3688 error = got_repo_match_object_id(&start_id, &label,
3689 worktree ? got_worktree_get_head_ref_name(worktree) :
3690 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3693 head_ref_name = label;
3695 error = got_ref_open(&ref, repo, start_commit, 0);
3697 head_ref_name = got_ref_get_name(ref);
3698 else if (error->code != GOT_ERR_NOT_REF)
3700 error = got_repo_match_object_id(&start_id, NULL,
3701 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3706 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3708 error = got_error_from_errno("view_open");
3711 error = open_log_view(view, start_id, repo, head_ref_name,
3712 in_repo_path, log_branches);
3716 /* Release work tree lock. */
3717 got_worktree_close(worktree);
3720 error = view_loop(view);
3730 const struct got_error *close_err = got_repo_close(repo);
3735 got_worktree_close(worktree);
3737 const struct got_error *pack_err =
3738 got_repo_pack_fds_close(pack_fds);
3750 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3751 "[-w] object1 object2\n", getprogname());
3756 match_line(const char *line, regex_t *regex, size_t nmatch,
3757 regmatch_t *regmatch)
3759 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3762 static struct tog_color *
3763 match_color(struct tog_colors *colors, const char *line)
3765 struct tog_color *tc = NULL;
3767 STAILQ_FOREACH(tc, colors, entry) {
3768 if (match_line(line, &tc->regex, 0, NULL))
3775 static const struct got_error *
3776 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3777 WINDOW *window, int skipcol, regmatch_t *regmatch)
3779 const struct got_error *err = NULL;
3781 wchar_t *wline = NULL;
3782 int rme, rms, n, width, scrollx;
3783 int width0 = 0, width1 = 0, width2 = 0;
3784 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3788 rms = regmatch->rm_so;
3789 rme = regmatch->rm_eo;
3791 err = expand_tab(&exstr, line);
3795 /* Split the line into 3 segments, according to match offsets. */
3796 seg0 = strndup(exstr, rms);
3798 err = got_error_from_errno("strndup");
3801 seg1 = strndup(exstr + rms, rme - rms);
3803 err = got_error_from_errno("strndup");
3806 seg2 = strdup(exstr + rme);
3808 err = got_error_from_errno("strndup");
3812 /* draw up to matched token if we haven't scrolled past it */
3813 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3817 n = MAX(width0 - skipcol, 0);
3820 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3821 wlimit, col_tab_align, 1);
3824 waddwstr(window, &wline[scrollx]);
3834 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3838 wlen = wcslen(wline);
3840 width = wcwidth(wline[i]);
3842 /* should not happen, tabs are expanded */
3843 err = got_error(GOT_ERR_RANGE);
3846 if (width0 + w + width > skipcol)
3851 /* draw (visible part of) matched token (if scrolled into it) */
3852 if (width1 - w > 0) {
3853 wattron(window, A_STANDOUT);
3854 waddwstr(window, &wline[i]);
3855 wattroff(window, A_STANDOUT);
3856 wlimit -= (width1 - w);
3857 *wtotal += (width1 - w);
3861 if (wlimit > 0) { /* draw rest of line */
3863 if (skipcol > width0 + width1) {
3864 err = format_line(&wline, &width2, &scrollx, seg2,
3865 skipcol - (width0 + width1), wlimit,
3869 waddwstr(window, &wline[scrollx]);
3871 err = format_line(&wline, &width2, NULL, seg2, 0,
3872 wlimit, col_tab_align, 1);
3875 waddwstr(window, wline);
3888 static const struct got_error *
3889 draw_file(struct tog_view *view, const char *header)
3891 struct tog_diff_view_state *s = &view->state.diff;
3892 regmatch_t *regmatch = &view->regmatch;
3893 const struct got_error *err;
3896 size_t linesize = 0;
3898 struct tog_color *tc;
3901 int max_lines = view->nlines;
3902 int nlines = s->nlines;
3905 line_offset = s->line_offsets[s->first_displayed_line - 1];
3906 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3907 return got_error_from_errno("fseek");
3909 werase(view->window);
3912 if (asprintf(&line, "[%d/%d] %s",
3913 s->first_displayed_line - 1 + s->selected_line, nlines,
3915 return got_error_from_errno("asprintf");
3916 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3922 if (view_needs_focus_indication(view))
3923 wstandout(view->window);
3924 waddwstr(view->window, wline);
3927 if (view_needs_focus_indication(view))
3928 wstandend(view->window);
3929 if (width <= view->ncols - 1)
3930 waddch(view->window, '\n');
3940 while (max_lines > 0 && nprinted < max_lines) {
3941 linelen = getline(&line, &linesize, s->f);
3942 if (linelen == -1) {
3948 return got_ferror(s->f, GOT_ERR_IO);
3951 /* Set view->maxx based on full line length. */
3952 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3958 view->maxx = MAX(view->maxx, width);
3962 tc = match_color(&s->colors, line);
3964 wattr_on(view->window,
3965 COLOR_PAIR(tc->colorpair), NULL);
3966 if (s->first_displayed_line + nprinted == s->matched_line &&
3967 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3968 err = add_matched_line(&width, line, view->ncols, 0,
3969 view->window, view->x, regmatch);
3976 err = format_line(&wline, &width, &skip, line,
3977 view->x, view->ncols, 0, view->x ? 1 : 0);
3982 waddwstr(view->window, &wline[skip]);
3987 wattr_off(view->window,
3988 COLOR_PAIR(tc->colorpair), NULL);
3989 if (width <= view->ncols - 1)
3990 waddch(view->window, '\n');
3995 s->last_displayed_line = s->first_displayed_line +
3998 s->last_displayed_line = s->first_displayed_line;
4003 while (nprinted < view->nlines) {
4004 waddch(view->window, '\n');
4008 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4014 wstandout(view->window);
4015 waddwstr(view->window, wline);
4018 wstandend(view->window);
4025 get_datestr(time_t *time, char *datebuf)
4027 struct tm mytm, *tm;
4030 tm = gmtime_r(time, &mytm);
4033 s = asctime_r(tm, datebuf);
4036 p = strchr(s, '\n');
4042 static const struct got_error *
4043 get_changed_paths(struct got_pathlist_head *paths,
4044 struct got_commit_object *commit, struct got_repository *repo)
4046 const struct got_error *err = NULL;
4047 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4048 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4049 struct got_object_qid *qid;
4051 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4053 struct got_commit_object *pcommit;
4054 err = got_object_open_as_commit(&pcommit, repo,
4059 tree_id1 = got_object_id_dup(
4060 got_object_commit_get_tree_id(pcommit));
4061 if (tree_id1 == NULL) {
4062 got_object_commit_close(pcommit);
4063 return got_error_from_errno("got_object_id_dup");
4065 got_object_commit_close(pcommit);
4070 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4075 tree_id2 = got_object_commit_get_tree_id(commit);
4076 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4080 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4081 got_diff_tree_collect_changed_paths, paths, 0);
4084 got_object_tree_close(tree1);
4086 got_object_tree_close(tree2);
4091 static const struct got_error *
4092 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4096 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4098 return got_error_from_errno("reallocarray");
4100 (*line_offsets)[*nlines] = off;
4105 static const struct got_error *
4106 write_commit_info(off_t **line_offsets, size_t *nlines,
4107 struct got_object_id *commit_id, struct got_reflist_head *refs,
4108 struct got_repository *repo, FILE *outfile)
4110 const struct got_error *err = NULL;
4111 char datebuf[26], *datestr;
4112 struct got_commit_object *commit;
4113 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4114 time_t committer_time;
4115 const char *author, *committer;
4116 char *refs_str = NULL;
4117 struct got_pathlist_head changed_paths;
4118 struct got_pathlist_entry *pe;
4122 TAILQ_INIT(&changed_paths);
4125 err = build_refs_str(&refs_str, refs, commit_id, repo);
4130 err = got_object_open_as_commit(&commit, repo, commit_id);
4134 err = got_object_id_str(&id_str, commit_id);
4136 err = got_error_from_errno("got_object_id_str");
4140 err = add_line_offset(line_offsets, nlines, 0);
4144 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4145 refs_str ? refs_str : "", refs_str ? ")" : "");
4147 err = got_error_from_errno("fprintf");
4151 err = add_line_offset(line_offsets, nlines, outoff);
4155 n = fprintf(outfile, "from: %s\n",
4156 got_object_commit_get_author(commit));
4158 err = got_error_from_errno("fprintf");
4162 err = add_line_offset(line_offsets, nlines, outoff);
4166 committer_time = got_object_commit_get_committer_time(commit);
4167 datestr = get_datestr(&committer_time, datebuf);
4169 n = fprintf(outfile, "date: %s UTC\n", datestr);
4171 err = got_error_from_errno("fprintf");
4175 err = add_line_offset(line_offsets, nlines, outoff);
4179 author = got_object_commit_get_author(commit);
4180 committer = got_object_commit_get_committer(commit);
4181 if (strcmp(author, committer) != 0) {
4182 n = fprintf(outfile, "via: %s\n", committer);
4184 err = got_error_from_errno("fprintf");
4188 err = add_line_offset(line_offsets, nlines, outoff);
4192 if (got_object_commit_get_nparents(commit) > 1) {
4193 const struct got_object_id_queue *parent_ids;
4194 struct got_object_qid *qid;
4196 parent_ids = got_object_commit_get_parent_ids(commit);
4197 STAILQ_FOREACH(qid, parent_ids, entry) {
4198 err = got_object_id_str(&id_str, &qid->id);
4201 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4203 err = got_error_from_errno("fprintf");
4207 err = add_line_offset(line_offsets, nlines, outoff);
4215 err = got_object_commit_get_logmsg(&logmsg, commit);
4219 while ((line = strsep(&s, "\n")) != NULL) {
4220 n = fprintf(outfile, "%s\n", line);
4222 err = got_error_from_errno("fprintf");
4226 err = add_line_offset(line_offsets, nlines, outoff);
4231 err = get_changed_paths(&changed_paths, commit, repo);
4234 TAILQ_FOREACH(pe, &changed_paths, entry) {
4235 struct got_diff_changed_path *cp = pe->data;
4236 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4238 err = got_error_from_errno("fprintf");
4242 err = add_line_offset(line_offsets, nlines, outoff);
4245 free((char *)pe->path);
4249 fputc('\n', outfile);
4251 err = add_line_offset(line_offsets, nlines, outoff);
4253 got_pathlist_free(&changed_paths);
4257 got_object_commit_close(commit);
4259 free(*line_offsets);
4260 *line_offsets = NULL;
4266 static const struct got_error *
4267 create_diff(struct tog_diff_view_state *s)
4269 const struct got_error *err = NULL;
4273 free(s->line_offsets);
4274 s->line_offsets = malloc(sizeof(off_t));
4275 if (s->line_offsets == NULL)
4276 return got_error_from_errno("malloc");
4281 err = got_error_from_errno("got_opentemp");
4284 if (s->f && fclose(s->f) == EOF) {
4285 err = got_error_from_errno("fclose");
4291 err = got_object_get_type(&obj_type, s->repo, s->id1);
4293 err = got_object_get_type(&obj_type, s->repo, s->id2);
4298 case GOT_OBJ_TYPE_BLOB:
4299 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4300 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4301 s->label1, s->label2, tog_diff_algo, s->diff_context,
4302 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4304 case GOT_OBJ_TYPE_TREE:
4305 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4306 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4307 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4308 s->force_text_diff, s->repo, s->f);
4310 case GOT_OBJ_TYPE_COMMIT: {
4311 const struct got_object_id_queue *parent_ids;
4312 struct got_object_qid *pid;
4313 struct got_commit_object *commit2;
4314 struct got_reflist_head *refs;
4316 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4319 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4320 /* Show commit info if we're diffing to a parent/root commit. */
4321 if (s->id1 == NULL) {
4322 err = write_commit_info(&s->line_offsets, &s->nlines,
4323 s->id2, refs, s->repo, s->f);
4327 parent_ids = got_object_commit_get_parent_ids(commit2);
4328 STAILQ_FOREACH(pid, parent_ids, entry) {
4329 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4330 err = write_commit_info(
4331 &s->line_offsets, &s->nlines,
4332 s->id2, refs, s->repo, s->f);
4339 got_object_commit_close(commit2);
4341 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4342 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4343 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4344 s->force_text_diff, s->repo, s->f);
4348 err = got_error(GOT_ERR_OBJ_TYPE);
4354 if (s->f && fflush(s->f) != 0 && err == NULL)
4355 err = got_error_from_errno("fflush");
4360 diff_view_indicate_progress(struct tog_view *view)
4362 mvwaddstr(view->window, 0, 0, "diffing...");
4367 static const struct got_error *
4368 search_start_diff_view(struct tog_view *view)
4370 struct tog_diff_view_state *s = &view->state.diff;
4372 s->matched_line = 0;
4376 static const struct got_error *
4377 search_next_diff_view(struct tog_view *view)
4379 struct tog_diff_view_state *s = &view->state.diff;
4380 const struct got_error *err = NULL;
4383 size_t linesize = 0;
4386 if (!view->searching) {
4387 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4391 if (s->matched_line) {
4392 if (view->searching == TOG_SEARCH_FORWARD)
4393 lineno = s->matched_line + 1;
4395 lineno = s->matched_line - 1;
4397 lineno = s->first_displayed_line;
4402 if (lineno <= 0 || lineno > s->nlines) {
4403 if (s->matched_line == 0) {
4404 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4408 if (view->searching == TOG_SEARCH_FORWARD)
4414 offset = s->line_offsets[lineno - 1];
4415 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4417 return got_error_from_errno("fseeko");
4419 linelen = getline(&line, &linesize, s->f);
4420 if (linelen != -1) {
4422 err = expand_tab(&exstr, line);
4425 if (match_line(exstr, &view->regex, 1,
4427 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4428 s->matched_line = lineno;
4434 if (view->searching == TOG_SEARCH_FORWARD)
4441 if (s->matched_line) {
4442 s->first_displayed_line = s->matched_line;
4443 s->selected_line = 1;
4449 static const struct got_error *
4450 close_diff_view(struct tog_view *view)
4452 const struct got_error *err = NULL;
4453 struct tog_diff_view_state *s = &view->state.diff;
4459 if (s->f && fclose(s->f) == EOF)
4460 err = got_error_from_errno("fclose");
4462 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4463 err = got_error_from_errno("fclose");
4465 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4466 err = got_error_from_errno("fclose");
4468 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4469 err = got_error_from_errno("close");
4471 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4472 err = got_error_from_errno("close");
4474 free_colors(&s->colors);
4475 free(s->line_offsets);
4476 s->line_offsets = NULL;
4481 static const struct got_error *
4482 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4483 struct got_object_id *id2, const char *label1, const char *label2,
4484 int diff_context, int ignore_whitespace, int force_text_diff,
4485 struct tog_view *parent_view, struct got_repository *repo)
4487 const struct got_error *err;
4488 struct tog_diff_view_state *s = &view->state.diff;
4490 memset(s, 0, sizeof(*s));
4494 if (id1 != NULL && id2 != NULL) {
4496 err = got_object_get_type(&type1, repo, id1);
4499 err = got_object_get_type(&type2, repo, id2);
4504 return got_error(GOT_ERR_OBJ_TYPE);
4506 s->first_displayed_line = 1;
4507 s->last_displayed_line = view->nlines;
4508 s->selected_line = 1;
4516 s->id1 = got_object_id_dup(id1);
4518 return got_error_from_errno("got_object_id_dup");
4522 s->id2 = got_object_id_dup(id2);
4523 if (s->id2 == NULL) {
4524 err = got_error_from_errno("got_object_id_dup");
4528 s->f1 = got_opentemp();
4529 if (s->f1 == NULL) {
4530 err = got_error_from_errno("got_opentemp");
4534 s->f2 = got_opentemp();
4535 if (s->f2 == NULL) {
4536 err = got_error_from_errno("got_opentemp");
4540 s->fd1 = got_opentempfd();
4542 err = got_error_from_errno("got_opentempfd");
4546 s->fd2 = got_opentempfd();
4548 err = got_error_from_errno("got_opentempfd");
4552 s->first_displayed_line = 1;
4553 s->last_displayed_line = view->nlines;
4554 s->diff_context = diff_context;
4555 s->ignore_whitespace = ignore_whitespace;
4556 s->force_text_diff = force_text_diff;
4557 s->parent_view = parent_view;
4560 STAILQ_INIT(&s->colors);
4561 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4562 err = add_color(&s->colors,
4563 "^-", TOG_COLOR_DIFF_MINUS,
4564 get_color_value("TOG_COLOR_DIFF_MINUS"));
4567 err = add_color(&s->colors, "^\\+",
4568 TOG_COLOR_DIFF_PLUS,
4569 get_color_value("TOG_COLOR_DIFF_PLUS"));
4572 err = add_color(&s->colors,
4573 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4574 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4578 err = add_color(&s->colors,
4579 "^(commit [0-9a-f]|parent [0-9]|"
4580 "(blob|file|tree|commit) [-+] |"
4581 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4582 get_color_value("TOG_COLOR_DIFF_META"));
4586 err = add_color(&s->colors,
4587 "^(from|via): ", TOG_COLOR_AUTHOR,
4588 get_color_value("TOG_COLOR_AUTHOR"));
4592 err = add_color(&s->colors,
4593 "^date: ", TOG_COLOR_DATE,
4594 get_color_value("TOG_COLOR_DATE"));
4599 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4600 view_is_splitscreen(view))
4601 show_log_view(parent_view); /* draw border */
4602 diff_view_indicate_progress(view);
4604 err = create_diff(s);
4606 view->show = show_diff_view;
4607 view->input = input_diff_view;
4608 view->reset = reset_diff_view;
4609 view->close = close_diff_view;
4610 view->search_start = search_start_diff_view;
4611 view->search_next = search_next_diff_view;
4614 close_diff_view(view);
4618 static const struct got_error *
4619 show_diff_view(struct tog_view *view)
4621 const struct got_error *err;
4622 struct tog_diff_view_state *s = &view->state.diff;
4623 char *id_str1 = NULL, *id_str2, *header;
4624 const char *label1, *label2;
4627 err = got_object_id_str(&id_str1, s->id1);
4630 label1 = s->label1 ? : id_str1;
4632 label1 = "/dev/null";
4634 err = got_object_id_str(&id_str2, s->id2);
4637 label2 = s->label2 ? : id_str2;
4639 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4640 err = got_error_from_errno("asprintf");
4648 err = draw_file(view, header);
4653 static const struct got_error *
4654 set_selected_commit(struct tog_diff_view_state *s,
4655 struct commit_queue_entry *entry)
4657 const struct got_error *err;
4658 const struct got_object_id_queue *parent_ids;
4659 struct got_commit_object *selected_commit;
4660 struct got_object_qid *pid;
4663 s->id2 = got_object_id_dup(entry->id);
4665 return got_error_from_errno("got_object_id_dup");
4667 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4670 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4672 pid = STAILQ_FIRST(parent_ids);
4673 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4674 got_object_commit_close(selected_commit);
4678 static const struct got_error *
4679 reset_diff_view(struct tog_view *view)
4681 struct tog_diff_view_state *s = &view->state.diff;
4684 wclear(view->window);
4685 s->first_displayed_line = 1;
4686 s->last_displayed_line = view->nlines;
4687 s->matched_line = 0;
4688 diff_view_indicate_progress(view);
4689 return create_diff(s);
4692 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4694 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4697 static const struct got_error *
4698 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4700 const struct got_error *err = NULL;
4701 struct tog_diff_view_state *s = &view->state.diff;
4702 struct tog_log_view_state *ls;
4703 struct commit_queue_entry *old_selected_entry;
4705 size_t linesize = 0;
4707 int i, nscroll = view->nlines - 1, up = 0;
4714 view->x = MAX(view->maxx - view->ncols / 3, 0);
4719 if (view->x + view->ncols / 3 < view->maxx)
4720 view->x += 2; /* move two columns right */
4726 view->x -= MIN(view->x, 2); /* move two columns back */
4733 s->force_text_diff = !s->force_text_diff;
4735 s->ignore_whitespace = !s->ignore_whitespace;
4736 err = reset_diff_view(view);
4740 s->first_displayed_line = 1;
4749 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4755 if (s->first_displayed_line > 1)
4756 s->first_displayed_line--;
4767 if (s->first_displayed_line == 1) {
4772 while (i++ < nscroll && s->first_displayed_line > 1)
4773 s->first_displayed_line--;
4779 s->first_displayed_line++;
4796 while (!s->eof && i++ < nscroll) {
4797 linelen = getline(&line, &linesize, s->f);
4798 s->first_displayed_line++;
4799 if (linelen == -1) {
4803 err = got_ferror(s->f, GOT_ERR_IO);
4810 if (s->diff_context > 0) {
4812 s->matched_line = 0;
4813 diff_view_indicate_progress(view);
4814 err = create_diff(s);
4815 if (s->first_displayed_line + view->nlines - 1 >
4817 s->first_displayed_line = 1;
4818 s->last_displayed_line = view->nlines;
4824 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4826 s->matched_line = 0;
4827 diff_view_indicate_progress(view);
4828 err = create_diff(s);
4838 if (s->parent_view == NULL) {
4842 s->parent_view->count = view->count;
4844 if (s->parent_view->type == TOG_VIEW_LOG) {
4845 ls = &s->parent_view->state.log;
4846 old_selected_entry = ls->selected_entry;
4848 err = input_log_view(NULL, s->parent_view,
4849 up ? KEY_UP : KEY_DOWN);
4852 view->count = s->parent_view->count;
4854 if (old_selected_entry == ls->selected_entry)
4857 err = set_selected_commit(s, ls->selected_entry);
4860 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4861 struct tog_blame_view_state *bs;
4862 struct got_object_id *id, *prev_id;
4864 bs = &s->parent_view->state.blame;
4865 prev_id = get_annotation_for_line(bs->blame.lines,
4866 bs->blame.nlines, bs->last_diffed_line);
4868 err = input_blame_view(&view, s->parent_view,
4869 up ? KEY_UP : KEY_DOWN);
4872 view->count = s->parent_view->count;
4874 if (prev_id == NULL)
4876 id = get_selected_commit_id(bs->blame.lines,
4877 bs->blame.nlines, bs->first_displayed_line,
4882 if (!got_object_id_cmp(prev_id, id))
4885 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4889 s->first_displayed_line = 1;
4890 s->last_displayed_line = view->nlines;
4891 s->matched_line = 0;
4894 diff_view_indicate_progress(view);
4895 err = create_diff(s);
4905 static const struct got_error *
4906 cmd_diff(int argc, char *argv[])
4908 const struct got_error *error = NULL;
4909 struct got_repository *repo = NULL;
4910 struct got_worktree *worktree = NULL;
4911 struct got_object_id *id1 = NULL, *id2 = NULL;
4912 char *repo_path = NULL, *cwd = NULL;
4913 char *id_str1 = NULL, *id_str2 = NULL;
4914 char *label1 = NULL, *label2 = NULL;
4915 int diff_context = 3, ignore_whitespace = 0;
4916 int ch, force_text_diff = 0;
4918 struct tog_view *view;
4919 int *pack_fds = NULL;
4921 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4924 force_text_diff = 1;
4927 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4930 errx(1, "number of context lines is %s: %s",
4934 repo_path = realpath(optarg, NULL);
4935 if (repo_path == NULL)
4936 return got_error_from_errno2("realpath",
4938 got_path_strip_trailing_slashes(repo_path);
4941 ignore_whitespace = 1;
4953 usage_diff(); /* TODO show local worktree changes */
4954 } else if (argc == 2) {
4960 error = got_repo_pack_fds_open(&pack_fds);
4964 if (repo_path == NULL) {
4965 cwd = getcwd(NULL, 0);
4967 return got_error_from_errno("getcwd");
4968 error = got_worktree_open(&worktree, cwd);
4969 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4973 strdup(got_worktree_get_repo_path(worktree));
4975 repo_path = strdup(cwd);
4976 if (repo_path == NULL) {
4977 error = got_error_from_errno("strdup");
4982 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4988 error = apply_unveil(got_repo_get_path(repo), NULL);
4992 error = tog_load_refs(repo, 0);
4996 error = got_repo_match_object_id(&id1, &label1, id_str1,
4997 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5001 error = got_repo_match_object_id(&id2, &label2, id_str2,
5002 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5006 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5008 error = got_error_from_errno("view_open");
5011 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5012 ignore_whitespace, force_text_diff, NULL, repo);
5015 error = view_loop(view);
5022 const struct got_error *close_err = got_repo_close(repo);
5027 got_worktree_close(worktree);
5029 const struct got_error *pack_err =
5030 got_repo_pack_fds_close(pack_fds);
5043 "usage: %s blame [-c commit] [-r repository-path] path\n",
5048 struct tog_blame_line {
5050 struct got_object_id *id;
5053 static const struct got_error *
5054 draw_blame(struct tog_view *view)
5056 struct tog_blame_view_state *s = &view->state.blame;
5057 struct tog_blame *blame = &s->blame;
5058 regmatch_t *regmatch = &view->regmatch;
5059 const struct got_error *err;
5060 int lineno = 0, nprinted = 0;
5062 size_t linesize = 0;
5066 struct tog_blame_line *blame_line;
5067 struct got_object_id *prev_id = NULL;
5069 struct tog_color *tc;
5071 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5076 werase(view->window);
5078 if (asprintf(&line, "commit %s", id_str) == -1) {
5079 err = got_error_from_errno("asprintf");
5084 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5089 if (view_needs_focus_indication(view))
5090 wstandout(view->window);
5091 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5093 wattr_on(view->window,
5094 COLOR_PAIR(tc->colorpair), NULL);
5095 waddwstr(view->window, wline);
5097 wattr_off(view->window,
5098 COLOR_PAIR(tc->colorpair), NULL);
5099 if (view_needs_focus_indication(view))
5100 wstandend(view->window);
5103 if (width < view->ncols - 1)
5104 waddch(view->window, '\n');
5106 if (asprintf(&line, "[%d/%d] %s%s",
5107 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5108 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5110 return got_error_from_errno("asprintf");
5113 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5118 waddwstr(view->window, wline);
5121 if (width < view->ncols - 1)
5122 waddch(view->window, '\n');
5126 while (nprinted < view->nlines - 2) {
5127 linelen = getline(&line, &linesize, blame->f);
5128 if (linelen == -1) {
5129 if (feof(blame->f)) {
5134 return got_ferror(blame->f, GOT_ERR_IO);
5136 if (++lineno < s->first_displayed_line)
5139 /* Set view->maxx based on full line length. */
5140 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5147 view->maxx = MAX(view->maxx, width);
5149 if (nprinted == s->selected_line - 1)
5150 wstandout(view->window);
5152 if (blame->nlines > 0) {
5153 blame_line = &blame->lines[lineno - 1];
5154 if (blame_line->annotated && prev_id &&
5155 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5156 !(nprinted == s->selected_line - 1)) {
5157 waddstr(view->window, " ");
5158 } else if (blame_line->annotated) {
5160 err = got_object_id_str(&id_str,
5166 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5168 wattr_on(view->window,
5169 COLOR_PAIR(tc->colorpair), NULL);
5170 wprintw(view->window, "%.8s", id_str);
5172 wattr_off(view->window,
5173 COLOR_PAIR(tc->colorpair), NULL);
5175 prev_id = blame_line->id;
5177 waddstr(view->window, "........");
5181 waddstr(view->window, "........");
5185 if (nprinted == s->selected_line - 1)
5186 wstandend(view->window);
5187 waddstr(view->window, " ");
5189 if (view->ncols <= 9) {
5191 } else if (s->first_displayed_line + nprinted ==
5193 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5194 err = add_matched_line(&width, line, view->ncols - 9, 9,
5195 view->window, view->x, regmatch);
5203 err = format_line(&wline, &width, &skip, line,
5204 view->x, view->ncols - 9, 9, 1);
5209 waddwstr(view->window, &wline[skip]);
5215 if (width <= view->ncols - 1)
5216 waddch(view->window, '\n');
5217 if (++nprinted == 1)
5218 s->first_displayed_line = lineno;
5221 s->last_displayed_line = lineno;
5228 static const struct got_error *
5229 blame_cb(void *arg, int nlines, int lineno,
5230 struct got_commit_object *commit, struct got_object_id *id)
5232 const struct got_error *err = NULL;
5233 struct tog_blame_cb_args *a = arg;
5234 struct tog_blame_line *line;
5237 if (nlines != a->nlines ||
5238 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5239 return got_error(GOT_ERR_RANGE);
5241 errcode = pthread_mutex_lock(&tog_mutex);
5243 return got_error_set_errno(errcode, "pthread_mutex_lock");
5245 if (*a->quit) { /* user has quit the blame view */
5246 err = got_error(GOT_ERR_ITER_COMPLETED);
5251 goto done; /* no change in this commit */
5253 line = &a->lines[lineno - 1];
5254 if (line->annotated)
5257 line->id = got_object_id_dup(id);
5258 if (line->id == NULL) {
5259 err = got_error_from_errno("got_object_id_dup");
5262 line->annotated = 1;
5264 errcode = pthread_mutex_unlock(&tog_mutex);
5266 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5271 blame_thread(void *arg)
5273 const struct got_error *err, *close_err;
5274 struct tog_blame_thread_args *ta = arg;
5275 struct tog_blame_cb_args *a = ta->cb_args;
5276 int errcode, fd1 = -1, fd2 = -1;
5277 FILE *f1 = NULL, *f2 = NULL;
5279 fd1 = got_opentempfd();
5281 return (void *)got_error_from_errno("got_opentempfd");
5283 fd2 = got_opentempfd();
5285 err = got_error_from_errno("got_opentempfd");
5289 f1 = got_opentemp();
5291 err = (void *)got_error_from_errno("got_opentemp");
5294 f2 = got_opentemp();
5296 err = (void *)got_error_from_errno("got_opentemp");
5300 err = block_signals_used_by_main_thread();
5304 err = got_blame(ta->path, a->commit_id, ta->repo,
5305 tog_diff_algo, blame_cb, ta->cb_args,
5306 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5307 if (err && err->code == GOT_ERR_CANCELLED)
5310 errcode = pthread_mutex_lock(&tog_mutex);
5312 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5316 close_err = got_repo_close(ta->repo);
5322 errcode = pthread_mutex_unlock(&tog_mutex);
5323 if (errcode && err == NULL)
5324 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5327 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5328 err = got_error_from_errno("close");
5329 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5330 err = got_error_from_errno("close");
5331 if (f1 && fclose(f1) == EOF && err == NULL)
5332 err = got_error_from_errno("fclose");
5333 if (f2 && fclose(f2) == EOF && err == NULL)
5334 err = got_error_from_errno("fclose");
5339 static struct got_object_id *
5340 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5341 int first_displayed_line, int selected_line)
5343 struct tog_blame_line *line;
5348 line = &lines[first_displayed_line - 1 + selected_line - 1];
5349 if (!line->annotated)
5355 static struct got_object_id *
5356 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5359 struct tog_blame_line *line;
5361 if (nlines <= 0 || lineno >= nlines)
5364 line = &lines[lineno - 1];
5365 if (!line->annotated)
5371 static const struct got_error *
5372 stop_blame(struct tog_blame *blame)
5374 const struct got_error *err = NULL;
5377 if (blame->thread) {
5379 errcode = pthread_mutex_unlock(&tog_mutex);
5381 return got_error_set_errno(errcode,
5382 "pthread_mutex_unlock");
5383 errcode = pthread_join(blame->thread, (void **)&err);
5385 return got_error_set_errno(errcode, "pthread_join");
5386 errcode = pthread_mutex_lock(&tog_mutex);
5388 return got_error_set_errno(errcode,
5389 "pthread_mutex_lock");
5390 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5392 blame->thread = 0; //NULL;
5394 if (blame->thread_args.repo) {
5395 const struct got_error *close_err;
5396 close_err = got_repo_close(blame->thread_args.repo);
5399 blame->thread_args.repo = NULL;
5402 if (fclose(blame->f) == EOF && err == NULL)
5403 err = got_error_from_errno("fclose");
5407 for (i = 0; i < blame->nlines; i++)
5408 free(blame->lines[i].id);
5410 blame->lines = NULL;
5412 free(blame->cb_args.commit_id);
5413 blame->cb_args.commit_id = NULL;
5414 if (blame->pack_fds) {
5415 const struct got_error *pack_err =
5416 got_repo_pack_fds_close(blame->pack_fds);
5419 blame->pack_fds = NULL;
5424 static const struct got_error *
5425 cancel_blame_view(void *arg)
5427 const struct got_error *err = NULL;
5431 errcode = pthread_mutex_lock(&tog_mutex);
5433 return got_error_set_errno(errcode,
5434 "pthread_mutex_unlock");
5437 err = got_error(GOT_ERR_CANCELLED);
5439 errcode = pthread_mutex_unlock(&tog_mutex);
5441 return got_error_set_errno(errcode,
5442 "pthread_mutex_lock");
5447 static const struct got_error *
5448 run_blame(struct tog_view *view)
5450 struct tog_blame_view_state *s = &view->state.blame;
5451 struct tog_blame *blame = &s->blame;
5452 const struct got_error *err = NULL;
5453 struct got_commit_object *commit = NULL;
5454 struct got_blob_object *blob = NULL;
5455 struct got_repository *thread_repo = NULL;
5456 struct got_object_id *obj_id = NULL;
5457 int obj_type, fd = -1;
5458 int *pack_fds = NULL;
5460 err = got_object_open_as_commit(&commit, s->repo,
5461 &s->blamed_commit->id);
5465 fd = got_opentempfd();
5467 err = got_error_from_errno("got_opentempfd");
5471 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5475 err = got_object_get_type(&obj_type, s->repo, obj_id);
5479 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5480 err = got_error(GOT_ERR_OBJ_TYPE);
5484 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5487 blame->f = got_opentemp();
5488 if (blame->f == NULL) {
5489 err = got_error_from_errno("got_opentemp");
5492 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5493 &blame->line_offsets, blame->f, blob);
5496 if (blame->nlines == 0) {
5497 s->blame_complete = 1;
5501 /* Don't include \n at EOF in the blame line count. */
5502 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5505 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5506 if (blame->lines == NULL) {
5507 err = got_error_from_errno("calloc");
5511 err = got_repo_pack_fds_open(&pack_fds);
5514 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5519 blame->pack_fds = pack_fds;
5520 blame->cb_args.view = view;
5521 blame->cb_args.lines = blame->lines;
5522 blame->cb_args.nlines = blame->nlines;
5523 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5524 if (blame->cb_args.commit_id == NULL) {
5525 err = got_error_from_errno("got_object_id_dup");
5528 blame->cb_args.quit = &s->done;
5530 blame->thread_args.path = s->path;
5531 blame->thread_args.repo = thread_repo;
5532 blame->thread_args.cb_args = &blame->cb_args;
5533 blame->thread_args.complete = &s->blame_complete;
5534 blame->thread_args.cancel_cb = cancel_blame_view;
5535 blame->thread_args.cancel_arg = &s->done;
5536 s->blame_complete = 0;
5538 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5539 s->first_displayed_line = 1;
5540 s->last_displayed_line = view->nlines;
5541 s->selected_line = 1;
5543 s->matched_line = 0;
5547 got_object_commit_close(commit);
5548 if (fd != -1 && close(fd) == -1 && err == NULL)
5549 err = got_error_from_errno("close");
5551 got_object_blob_close(blob);
5558 static const struct got_error *
5559 open_blame_view(struct tog_view *view, char *path,
5560 struct got_object_id *commit_id, struct got_repository *repo)
5562 const struct got_error *err = NULL;
5563 struct tog_blame_view_state *s = &view->state.blame;
5565 STAILQ_INIT(&s->blamed_commits);
5567 s->path = strdup(path);
5568 if (s->path == NULL)
5569 return got_error_from_errno("strdup");
5571 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5577 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5578 s->first_displayed_line = 1;
5579 s->last_displayed_line = view->nlines;
5580 s->selected_line = 1;
5581 s->blame_complete = 0;
5583 s->commit_id = commit_id;
5584 memset(&s->blame, 0, sizeof(s->blame));
5586 STAILQ_INIT(&s->colors);
5587 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5588 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5589 get_color_value("TOG_COLOR_COMMIT"));
5594 view->show = show_blame_view;
5595 view->input = input_blame_view;
5596 view->reset = reset_blame_view;
5597 view->close = close_blame_view;
5598 view->search_start = search_start_blame_view;
5599 view->search_next = search_next_blame_view;
5601 return run_blame(view);
5604 static const struct got_error *
5605 close_blame_view(struct tog_view *view)
5607 const struct got_error *err = NULL;
5608 struct tog_blame_view_state *s = &view->state.blame;
5610 if (s->blame.thread)
5611 err = stop_blame(&s->blame);
5613 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5614 struct got_object_qid *blamed_commit;
5615 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5616 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5617 got_object_qid_free(blamed_commit);
5621 free_colors(&s->colors);
5625 static const struct got_error *
5626 search_start_blame_view(struct tog_view *view)
5628 struct tog_blame_view_state *s = &view->state.blame;
5630 s->matched_line = 0;
5634 static const struct got_error *
5635 search_next_blame_view(struct tog_view *view)
5637 struct tog_blame_view_state *s = &view->state.blame;
5638 const struct got_error *err = NULL;
5641 size_t linesize = 0;
5644 if (!view->searching) {
5645 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5649 if (s->matched_line) {
5650 if (view->searching == TOG_SEARCH_FORWARD)
5651 lineno = s->matched_line + 1;
5653 lineno = s->matched_line - 1;
5655 lineno = s->first_displayed_line - 1 + s->selected_line;
5660 if (lineno <= 0 || lineno > s->blame.nlines) {
5661 if (s->matched_line == 0) {
5662 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5666 if (view->searching == TOG_SEARCH_FORWARD)
5669 lineno = s->blame.nlines;
5672 offset = s->blame.line_offsets[lineno - 1];
5673 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5675 return got_error_from_errno("fseeko");
5677 linelen = getline(&line, &linesize, s->blame.f);
5678 if (linelen != -1) {
5680 err = expand_tab(&exstr, line);
5683 if (match_line(exstr, &view->regex, 1,
5685 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5686 s->matched_line = lineno;
5692 if (view->searching == TOG_SEARCH_FORWARD)
5699 if (s->matched_line) {
5700 s->first_displayed_line = s->matched_line;
5701 s->selected_line = 1;
5707 static const struct got_error *
5708 show_blame_view(struct tog_view *view)
5710 const struct got_error *err = NULL;
5711 struct tog_blame_view_state *s = &view->state.blame;
5714 if (s->blame.thread == 0 && !s->blame_complete) {
5715 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5716 &s->blame.thread_args);
5718 return got_error_set_errno(errcode, "pthread_create");
5720 halfdelay(1); /* fast refresh while annotating */
5723 if (s->blame_complete)
5724 halfdelay(10); /* disable fast refresh */
5726 err = draw_blame(view);
5732 static const struct got_error *
5733 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5735 const struct got_error *err = NULL, *thread_err = NULL;
5736 struct tog_view *diff_view;
5737 struct tog_blame_view_state *s = &view->state.blame;
5738 int eos, nscroll, begin_y = 0, begin_x = 0;
5740 eos = nscroll = view->nlines - 2;
5741 if (view_is_hsplit_top(view))
5749 view->x = MAX(view->maxx - view->ncols / 3, 0);
5754 if (view->x + view->ncols / 3 < view->maxx)
5755 view->x += 2; /* move two columns right */
5761 view->x -= MIN(view->x, 2); /* move two columns back */
5770 s->selected_line = 1;
5771 s->first_displayed_line = 1;
5776 if (s->blame.nlines < eos) {
5777 s->selected_line = s->blame.nlines;
5778 s->first_displayed_line = 1;
5780 s->selected_line = eos;
5781 s->first_displayed_line = s->blame.nlines - (eos - 1);
5788 if (s->selected_line > 1)
5790 else if (s->selected_line == 1 &&
5791 s->first_displayed_line > 1)
5792 s->first_displayed_line--;
5803 if (s->first_displayed_line == 1) {
5804 if (view->count > 1)
5806 s->selected_line = MAX(1, s->selected_line - nscroll);
5810 if (s->first_displayed_line > nscroll)
5811 s->first_displayed_line -= nscroll;
5813 s->first_displayed_line = 1;
5818 if (s->selected_line < eos && s->first_displayed_line +
5819 s->selected_line <= s->blame.nlines)
5821 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5822 s->first_displayed_line++;
5828 struct got_object_id *id = NULL;
5831 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5832 s->first_displayed_line, s->selected_line);
5836 struct got_commit_object *commit, *pcommit;
5837 struct got_object_qid *pid;
5838 struct got_object_id *blob_id = NULL;
5840 err = got_object_open_as_commit(&commit,
5845 got_object_commit_get_parent_ids(commit));
5847 got_object_commit_close(commit);
5850 /* Check if path history ends here. */
5851 err = got_object_open_as_commit(&pcommit,
5855 err = got_object_id_by_path(&blob_id, s->repo,
5857 got_object_commit_close(pcommit);
5859 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5861 got_object_commit_close(commit);
5864 err = got_object_get_type(&obj_type, s->repo,
5867 /* Can't blame non-blob type objects. */
5868 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5869 got_object_commit_close(commit);
5872 err = got_object_qid_alloc(&s->blamed_commit,
5874 got_object_commit_close(commit);
5876 if (got_object_id_cmp(id,
5877 &s->blamed_commit->id) == 0)
5879 err = got_object_qid_alloc(&s->blamed_commit,
5885 thread_err = stop_blame(&s->blame);
5889 STAILQ_INSERT_HEAD(&s->blamed_commits,
5890 s->blamed_commit, entry);
5891 err = run_blame(view);
5897 struct got_object_qid *first;
5900 first = STAILQ_FIRST(&s->blamed_commits);
5901 if (!got_object_id_cmp(&first->id, s->commit_id))
5904 thread_err = stop_blame(&s->blame);
5908 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5909 got_object_qid_free(s->blamed_commit);
5911 STAILQ_FIRST(&s->blamed_commits);
5912 err = run_blame(view);
5919 struct got_object_id *id = NULL;
5920 struct got_object_qid *pid;
5921 struct got_commit_object *commit = NULL;
5924 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5925 s->first_displayed_line, s->selected_line);
5928 err = got_object_open_as_commit(&commit, s->repo, id);
5931 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5933 /* traversed from diff view, release diff resources */
5934 err = close_diff_view(*new_view);
5937 diff_view = *new_view;
5939 if (view_is_parent_view(view))
5940 view_get_split(view, &begin_y, &begin_x);
5942 diff_view = view_open(0, 0, begin_y, begin_x,
5944 if (diff_view == NULL) {
5945 got_object_commit_close(commit);
5946 err = got_error_from_errno("view_open");
5950 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5951 id, NULL, NULL, 3, 0, 0, view, s->repo);
5952 got_object_commit_close(commit);
5954 view_close(diff_view);
5957 s->last_diffed_line = s->first_displayed_line - 1 +
5960 break; /* still open from active diff view */
5961 if (view_is_parent_view(view) &&
5962 view->mode == TOG_VIEW_SPLIT_HRZN) {
5963 err = view_init_hsplit(view, begin_y);
5969 diff_view->focussed = 1;
5970 diff_view->mode = view->mode;
5971 diff_view->nlines = view->lines - begin_y;
5972 if (view_is_parent_view(view)) {
5973 view_transfer_size(diff_view, view);
5974 err = view_close_child(view);
5977 err = view_set_child(view, diff_view);
5980 view->focus_child = 1;
5982 *new_view = diff_view;
5995 if (s->last_displayed_line >= s->blame.nlines &&
5996 s->selected_line >= MIN(s->blame.nlines,
5997 view->nlines - 2)) {
6001 if (s->last_displayed_line >= s->blame.nlines &&
6002 s->selected_line < view->nlines - 2) {
6004 MIN(nscroll, s->last_displayed_line -
6005 s->first_displayed_line - s->selected_line + 1);
6007 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6008 s->first_displayed_line += nscroll;
6010 s->first_displayed_line =
6011 s->blame.nlines - (view->nlines - 3);
6014 if (s->selected_line > view->nlines - 2) {
6015 s->selected_line = MIN(s->blame.nlines,
6023 return thread_err ? thread_err : err;
6026 static const struct got_error *
6027 reset_blame_view(struct tog_view *view)
6029 const struct got_error *err;
6030 struct tog_blame_view_state *s = &view->state.blame;
6034 err = stop_blame(&s->blame);
6038 return run_blame(view);
6041 static const struct got_error *
6042 cmd_blame(int argc, char *argv[])
6044 const struct got_error *error;
6045 struct got_repository *repo = NULL;
6046 struct got_worktree *worktree = NULL;
6047 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6048 char *link_target = NULL;
6049 struct got_object_id *commit_id = NULL;
6050 struct got_commit_object *commit = NULL;
6051 char *commit_id_str = NULL;
6053 struct tog_view *view;
6054 int *pack_fds = NULL;
6056 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6059 commit_id_str = optarg;
6062 repo_path = realpath(optarg, NULL);
6063 if (repo_path == NULL)
6064 return got_error_from_errno2("realpath",
6079 error = got_repo_pack_fds_open(&pack_fds);
6083 if (repo_path == NULL) {
6084 cwd = getcwd(NULL, 0);
6086 return got_error_from_errno("getcwd");
6087 error = got_worktree_open(&worktree, cwd);
6088 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6092 strdup(got_worktree_get_repo_path(worktree));
6094 repo_path = strdup(cwd);
6095 if (repo_path == NULL) {
6096 error = got_error_from_errno("strdup");
6101 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6105 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6112 error = apply_unveil(got_repo_get_path(repo), NULL);
6116 error = tog_load_refs(repo, 0);
6120 if (commit_id_str == NULL) {
6121 struct got_reference *head_ref;
6122 error = got_ref_open(&head_ref, repo, worktree ?
6123 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6126 error = got_ref_resolve(&commit_id, repo, head_ref);
6127 got_ref_close(head_ref);
6129 error = got_repo_match_object_id(&commit_id, NULL,
6130 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6135 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6137 error = got_error_from_errno("view_open");
6141 error = got_object_open_as_commit(&commit, repo, commit_id);
6145 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6150 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6155 /* Release work tree lock. */
6156 got_worktree_close(worktree);
6159 error = view_loop(view);
6167 got_object_commit_close(commit);
6169 got_worktree_close(worktree);
6171 const struct got_error *close_err = got_repo_close(repo);
6176 const struct got_error *pack_err =
6177 got_repo_pack_fds_close(pack_fds);
6185 static const struct got_error *
6186 draw_tree_entries(struct tog_view *view, const char *parent_path)
6188 struct tog_tree_view_state *s = &view->state.tree;
6189 const struct got_error *err = NULL;
6190 struct got_tree_entry *te;
6192 struct tog_color *tc;
6193 int width, n, i, nentries;
6194 int limit = view->nlines;
6197 if (view_is_hsplit_top(view))
6198 --limit; /* border */
6200 werase(view->window);
6205 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6209 if (view_needs_focus_indication(view))
6210 wstandout(view->window);
6211 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6213 wattr_on(view->window,
6214 COLOR_PAIR(tc->colorpair), NULL);
6215 waddwstr(view->window, wline);
6217 wattr_off(view->window,
6218 COLOR_PAIR(tc->colorpair), NULL);
6219 if (view_needs_focus_indication(view))
6220 wstandend(view->window);
6223 if (width < view->ncols - 1)
6224 waddch(view->window, '\n');
6227 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6231 waddwstr(view->window, wline);
6234 if (width < view->ncols - 1)
6235 waddch(view->window, '\n');
6238 waddch(view->window, '\n');
6242 if (s->first_displayed_entry == NULL) {
6243 te = got_object_tree_get_first_entry(s->tree);
6244 if (s->selected == 0) {
6246 wstandout(view->window);
6247 s->selected_entry = NULL;
6249 waddstr(view->window, " ..\n"); /* parent directory */
6250 if (s->selected == 0 && view->focussed)
6251 wstandend(view->window);
6258 te = s->first_displayed_entry;
6261 nentries = got_object_tree_get_nentries(s->tree);
6262 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6263 char *line = NULL, *id_str = NULL, *link_target = NULL;
6264 const char *modestr = "";
6267 te = got_object_tree_get_entry(s->tree, i);
6268 mode = got_tree_entry_get_mode(te);
6271 err = got_object_id_str(&id_str,
6272 got_tree_entry_get_id(te));
6274 return got_error_from_errno(
6275 "got_object_id_str");
6277 if (got_object_tree_entry_is_submodule(te))
6279 else if (S_ISLNK(mode)) {
6282 err = got_tree_entry_get_symlink_target(&link_target,
6288 for (i = 0; i < strlen(link_target); i++) {
6289 if (!isprint((unsigned char)link_target[i]))
6290 link_target[i] = '?';
6294 else if (S_ISDIR(mode))
6296 else if (mode & S_IXUSR)
6298 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6299 got_tree_entry_get_name(te), modestr,
6300 link_target ? " -> ": "",
6301 link_target ? link_target : "") == -1) {
6304 return got_error_from_errno("asprintf");
6308 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6314 if (n == s->selected) {
6316 wstandout(view->window);
6317 s->selected_entry = te;
6319 tc = match_color(&s->colors, line);
6321 wattr_on(view->window,
6322 COLOR_PAIR(tc->colorpair), NULL);
6323 waddwstr(view->window, wline);
6325 wattr_off(view->window,
6326 COLOR_PAIR(tc->colorpair), NULL);
6327 if (width < view->ncols - 1)
6328 waddch(view->window, '\n');
6329 if (n == s->selected && view->focussed)
6330 wstandend(view->window);
6336 s->last_displayed_entry = te;
6345 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6347 struct got_tree_entry *te;
6348 int isroot = s->tree == s->root;
6351 if (s->first_displayed_entry == NULL)
6354 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6355 while (i++ < maxscroll) {
6358 s->first_displayed_entry = NULL;
6361 s->first_displayed_entry = te;
6362 te = got_tree_entry_get_prev(s->tree, te);
6366 static const struct got_error *
6367 tree_scroll_down(struct tog_view *view, int maxscroll)
6369 struct tog_tree_view_state *s = &view->state.tree;
6370 struct got_tree_entry *next, *last;
6373 if (s->first_displayed_entry)
6374 next = got_tree_entry_get_next(s->tree,
6375 s->first_displayed_entry);
6377 next = got_object_tree_get_first_entry(s->tree);
6379 last = s->last_displayed_entry;
6380 while (next && n++ < maxscroll) {
6382 last = got_tree_entry_get_next(s->tree, last);
6383 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6384 s->first_displayed_entry = next;
6385 next = got_tree_entry_get_next(s->tree, next);
6392 static const struct got_error *
6393 tree_entry_path(char **path, struct tog_parent_trees *parents,
6394 struct got_tree_entry *te)
6396 const struct got_error *err = NULL;
6397 struct tog_parent_tree *pt;
6398 size_t len = 2; /* for leading slash and NUL */
6400 TAILQ_FOREACH(pt, parents, entry)
6401 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6404 len += strlen(got_tree_entry_get_name(te));
6406 *path = calloc(1, len);
6408 return got_error_from_errno("calloc");
6411 pt = TAILQ_LAST(parents, tog_parent_trees);
6413 const char *name = got_tree_entry_get_name(pt->selected_entry);
6414 if (strlcat(*path, name, len) >= len) {
6415 err = got_error(GOT_ERR_NO_SPACE);
6418 if (strlcat(*path, "/", len) >= len) {
6419 err = got_error(GOT_ERR_NO_SPACE);
6422 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6425 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6426 err = got_error(GOT_ERR_NO_SPACE);
6438 static const struct got_error *
6439 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6440 struct got_tree_entry *te, struct tog_parent_trees *parents,
6441 struct got_object_id *commit_id, struct got_repository *repo)
6443 const struct got_error *err = NULL;
6445 struct tog_view *blame_view;
6449 err = tree_entry_path(&path, parents, te);
6453 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6454 if (blame_view == NULL) {
6455 err = got_error_from_errno("view_open");
6459 err = open_blame_view(blame_view, path, commit_id, repo);
6461 if (err->code == GOT_ERR_CANCELLED)
6463 view_close(blame_view);
6465 *new_view = blame_view;
6471 static const struct got_error *
6472 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6473 struct tog_tree_view_state *s)
6475 struct tog_view *log_view;
6476 const struct got_error *err = NULL;
6481 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6482 if (log_view == NULL)
6483 return got_error_from_errno("view_open");
6485 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6489 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6492 view_close(log_view);
6494 *new_view = log_view;
6499 static const struct got_error *
6500 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6501 const char *head_ref_name, struct got_repository *repo)
6503 const struct got_error *err = NULL;
6504 char *commit_id_str = NULL;
6505 struct tog_tree_view_state *s = &view->state.tree;
6506 struct got_commit_object *commit = NULL;
6508 TAILQ_INIT(&s->parents);
6509 STAILQ_INIT(&s->colors);
6511 s->commit_id = got_object_id_dup(commit_id);
6512 if (s->commit_id == NULL)
6513 return got_error_from_errno("got_object_id_dup");
6515 err = got_object_open_as_commit(&commit, repo, commit_id);
6520 * The root is opened here and will be closed when the view is closed.
6521 * Any visited subtrees and their path-wise parents are opened and
6524 err = got_object_open_as_tree(&s->root, repo,
6525 got_object_commit_get_tree_id(commit));
6530 err = got_object_id_str(&commit_id_str, commit_id);
6534 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6535 err = got_error_from_errno("asprintf");
6539 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6540 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6541 if (head_ref_name) {
6542 s->head_ref_name = strdup(head_ref_name);
6543 if (s->head_ref_name == NULL) {
6544 err = got_error_from_errno("strdup");
6550 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6551 err = add_color(&s->colors, "\\$$",
6552 TOG_COLOR_TREE_SUBMODULE,
6553 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6556 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6557 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6560 err = add_color(&s->colors, "/$",
6561 TOG_COLOR_TREE_DIRECTORY,
6562 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6566 err = add_color(&s->colors, "\\*$",
6567 TOG_COLOR_TREE_EXECUTABLE,
6568 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6572 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6573 get_color_value("TOG_COLOR_COMMIT"));
6578 view->show = show_tree_view;
6579 view->input = input_tree_view;
6580 view->close = close_tree_view;
6581 view->search_start = search_start_tree_view;
6582 view->search_next = search_next_tree_view;
6584 free(commit_id_str);
6586 got_object_commit_close(commit);
6588 close_tree_view(view);
6592 static const struct got_error *
6593 close_tree_view(struct tog_view *view)
6595 struct tog_tree_view_state *s = &view->state.tree;
6597 free_colors(&s->colors);
6598 free(s->tree_label);
6599 s->tree_label = NULL;
6601 s->commit_id = NULL;
6602 free(s->head_ref_name);
6603 s->head_ref_name = NULL;
6604 while (!TAILQ_EMPTY(&s->parents)) {
6605 struct tog_parent_tree *parent;
6606 parent = TAILQ_FIRST(&s->parents);
6607 TAILQ_REMOVE(&s->parents, parent, entry);
6608 if (parent->tree != s->root)
6609 got_object_tree_close(parent->tree);
6613 if (s->tree != NULL && s->tree != s->root)
6614 got_object_tree_close(s->tree);
6616 got_object_tree_close(s->root);
6620 static const struct got_error *
6621 search_start_tree_view(struct tog_view *view)
6623 struct tog_tree_view_state *s = &view->state.tree;
6625 s->matched_entry = NULL;
6630 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6632 regmatch_t regmatch;
6634 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6638 static const struct got_error *
6639 search_next_tree_view(struct tog_view *view)
6641 struct tog_tree_view_state *s = &view->state.tree;
6642 struct got_tree_entry *te = NULL;
6644 if (!view->searching) {
6645 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6649 if (s->matched_entry) {
6650 if (view->searching == TOG_SEARCH_FORWARD) {
6651 if (s->selected_entry)
6652 te = got_tree_entry_get_next(s->tree,
6655 te = got_object_tree_get_first_entry(s->tree);
6657 if (s->selected_entry == NULL)
6658 te = got_object_tree_get_last_entry(s->tree);
6660 te = got_tree_entry_get_prev(s->tree,
6664 if (s->selected_entry)
6665 te = s->selected_entry;
6666 else if (view->searching == TOG_SEARCH_FORWARD)
6667 te = got_object_tree_get_first_entry(s->tree);
6669 te = got_object_tree_get_last_entry(s->tree);
6674 if (s->matched_entry == NULL) {
6675 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6678 if (view->searching == TOG_SEARCH_FORWARD)
6679 te = got_object_tree_get_first_entry(s->tree);
6681 te = got_object_tree_get_last_entry(s->tree);
6684 if (match_tree_entry(te, &view->regex)) {
6685 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6686 s->matched_entry = te;
6690 if (view->searching == TOG_SEARCH_FORWARD)
6691 te = got_tree_entry_get_next(s->tree, te);
6693 te = got_tree_entry_get_prev(s->tree, te);
6696 if (s->matched_entry) {
6697 s->first_displayed_entry = s->matched_entry;
6704 static const struct got_error *
6705 show_tree_view(struct tog_view *view)
6707 const struct got_error *err = NULL;
6708 struct tog_tree_view_state *s = &view->state.tree;
6711 err = tree_entry_path(&parent_path, &s->parents, NULL);
6715 err = draw_tree_entries(view, parent_path);
6722 static const struct got_error *
6723 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6725 const struct got_error *err = NULL;
6726 struct tog_tree_view_state *s = &view->state.tree;
6727 struct tog_view *log_view, *ref_view;
6728 struct got_tree_entry *te;
6729 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6733 s->show_ids = !s->show_ids;
6738 if (!s->selected_entry)
6740 if (view_is_parent_view(view))
6741 view_get_split(view, &begin_y, &begin_x);
6742 err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6743 if (view_is_parent_view(view) &&
6744 view->mode == TOG_VIEW_SPLIT_HRZN) {
6745 err = view_init_hsplit(view, begin_y);
6750 log_view->focussed = 1;
6751 log_view->mode = view->mode;
6752 log_view->nlines = view->lines - begin_y;
6753 if (view_is_parent_view(view)) {
6754 view_transfer_size(log_view, view);
6755 err = view_close_child(view);
6758 err = view_set_child(view, log_view);
6761 view->focus_child = 1;
6763 *new_view = log_view;
6767 if (view_is_parent_view(view))
6768 view_get_split(view, &begin_y, &begin_x);
6769 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6770 if (ref_view == NULL)
6771 return got_error_from_errno("view_open");
6772 err = open_ref_view(ref_view, s->repo);
6774 view_close(ref_view);
6777 if (view_is_parent_view(view) &&
6778 view->mode == TOG_VIEW_SPLIT_HRZN) {
6779 err = view_init_hsplit(view, begin_y);
6784 ref_view->focussed = 1;
6785 ref_view->mode = view->mode;
6786 ref_view->nlines = view->lines - begin_y;
6787 if (view_is_parent_view(view)) {
6788 view_transfer_size(ref_view, view);
6789 err = view_close_child(view);
6792 err = view_set_child(view, ref_view);
6795 view->focus_child = 1;
6797 *new_view = ref_view;
6803 if (s->tree == s->root)
6804 s->first_displayed_entry =
6805 got_object_tree_get_first_entry(s->tree);
6807 s->first_displayed_entry = NULL;
6811 int eos = view->nlines - 3;
6813 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6817 te = got_object_tree_get_last_entry(s->tree);
6818 for (n = 0; n < eos; n++) {
6820 if (s->tree != s->root) {
6821 s->first_displayed_entry = NULL;
6826 s->first_displayed_entry = te;
6827 te = got_tree_entry_get_prev(s->tree, te);
6830 s->selected = n - 1;
6836 if (s->selected > 0) {
6840 tree_scroll_up(s, 1);
6841 if (s->selected_entry == NULL ||
6842 (s->tree == s->root && s->selected_entry ==
6843 got_object_tree_get_first_entry(s->tree)))
6853 if (s->tree == s->root) {
6854 if (got_object_tree_get_first_entry(s->tree) ==
6855 s->first_displayed_entry)
6856 s->selected -= MIN(s->selected, nscroll);
6858 if (s->first_displayed_entry == NULL)
6859 s->selected -= MIN(s->selected, nscroll);
6861 tree_scroll_up(s, MAX(0, nscroll));
6862 if (s->selected_entry == NULL ||
6863 (s->tree == s->root && s->selected_entry ==
6864 got_object_tree_get_first_entry(s->tree)))
6870 if (s->selected < s->ndisplayed - 1) {
6874 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6876 /* can't scroll any further */
6880 tree_scroll_down(view, 1);
6890 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6892 /* can't scroll any further; move cursor down */
6893 if (s->selected < s->ndisplayed - 1)
6894 s->selected += MIN(nscroll,
6895 s->ndisplayed - s->selected - 1);
6900 tree_scroll_down(view, nscroll);
6905 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6906 struct tog_parent_tree *parent;
6907 /* user selected '..' */
6908 if (s->tree == s->root) {
6912 parent = TAILQ_FIRST(&s->parents);
6913 TAILQ_REMOVE(&s->parents, parent,
6915 got_object_tree_close(s->tree);
6916 s->tree = parent->tree;
6917 s->first_displayed_entry =
6918 parent->first_displayed_entry;
6920 parent->selected_entry;
6921 s->selected = parent->selected;
6922 if (s->selected > view->nlines - 3) {
6923 err = offset_selection_down(view);
6928 } else if (S_ISDIR(got_tree_entry_get_mode(
6929 s->selected_entry))) {
6930 struct got_tree_object *subtree;
6932 err = got_object_open_as_tree(&subtree, s->repo,
6933 got_tree_entry_get_id(s->selected_entry));
6936 err = tree_view_visit_subtree(s, subtree);
6938 got_object_tree_close(subtree);
6941 } else if (S_ISREG(got_tree_entry_get_mode(
6942 s->selected_entry))) {
6943 struct tog_view *blame_view;
6944 int begin_x = 0, begin_y = 0;
6946 if (view_is_parent_view(view))
6947 view_get_split(view, &begin_y, &begin_x);
6949 err = blame_tree_entry(&blame_view, begin_y, begin_x,
6950 s->selected_entry, &s->parents,
6951 s->commit_id, s->repo);
6955 if (view_is_parent_view(view) &&
6956 view->mode == TOG_VIEW_SPLIT_HRZN) {
6957 err = view_init_hsplit(view, begin_y);
6964 blame_view->focussed = 1;
6965 blame_view->mode = view->mode;
6966 blame_view->nlines = view->lines - begin_y;
6967 if (view_is_parent_view(view)) {
6968 view_transfer_size(blame_view, view);
6969 err = view_close_child(view);
6972 err = view_set_child(view, blame_view);
6975 view->focus_child = 1;
6977 *new_view = blame_view;
6981 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6982 s->selected = view->nlines - 4;
6998 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7003 static const struct got_error *
7004 cmd_tree(int argc, char *argv[])
7006 const struct got_error *error;
7007 struct got_repository *repo = NULL;
7008 struct got_worktree *worktree = NULL;
7009 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7010 struct got_object_id *commit_id = NULL;
7011 struct got_commit_object *commit = NULL;
7012 const char *commit_id_arg = NULL;
7014 struct got_reference *ref = NULL;
7015 const char *head_ref_name = NULL;
7017 struct tog_view *view;
7018 int *pack_fds = NULL;
7020 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7023 commit_id_arg = optarg;
7026 repo_path = realpath(optarg, NULL);
7027 if (repo_path == NULL)
7028 return got_error_from_errno2("realpath",
7043 error = got_repo_pack_fds_open(&pack_fds);
7047 if (repo_path == NULL) {
7048 cwd = getcwd(NULL, 0);
7050 return got_error_from_errno("getcwd");
7051 error = got_worktree_open(&worktree, cwd);
7052 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7056 strdup(got_worktree_get_repo_path(worktree));
7058 repo_path = strdup(cwd);
7059 if (repo_path == NULL) {
7060 error = got_error_from_errno("strdup");
7065 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7069 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7076 error = apply_unveil(got_repo_get_path(repo), NULL);
7080 error = tog_load_refs(repo, 0);
7084 if (commit_id_arg == NULL) {
7085 error = got_repo_match_object_id(&commit_id, &label,
7086 worktree ? got_worktree_get_head_ref_name(worktree) :
7087 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7090 head_ref_name = label;
7092 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7094 head_ref_name = got_ref_get_name(ref);
7095 else if (error->code != GOT_ERR_NOT_REF)
7097 error = got_repo_match_object_id(&commit_id, NULL,
7098 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7103 error = got_object_open_as_commit(&commit, repo, commit_id);
7107 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7109 error = got_error_from_errno("view_open");
7112 error = open_tree_view(view, commit_id, head_ref_name, repo);
7115 if (!got_path_is_root_dir(in_repo_path)) {
7116 error = tree_view_walk_path(&view->state.tree, commit,
7123 /* Release work tree lock. */
7124 got_worktree_close(worktree);
7127 error = view_loop(view);
7136 const struct got_error *close_err = got_repo_close(repo);
7141 const struct got_error *pack_err =
7142 got_repo_pack_fds_close(pack_fds);
7150 static const struct got_error *
7151 ref_view_load_refs(struct tog_ref_view_state *s)
7153 struct got_reflist_entry *sre;
7154 struct tog_reflist_entry *re;
7157 TAILQ_FOREACH(sre, &tog_refs, entry) {
7158 if (strncmp(got_ref_get_name(sre->ref),
7159 "refs/got/", 9) == 0 &&
7160 strncmp(got_ref_get_name(sre->ref),
7161 "refs/got/backup/", 16) != 0)
7164 re = malloc(sizeof(*re));
7166 return got_error_from_errno("malloc");
7168 re->ref = got_ref_dup(sre->ref);
7169 if (re->ref == NULL)
7170 return got_error_from_errno("got_ref_dup");
7171 re->idx = s->nrefs++;
7172 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7175 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7180 ref_view_free_refs(struct tog_ref_view_state *s)
7182 struct tog_reflist_entry *re;
7184 while (!TAILQ_EMPTY(&s->refs)) {
7185 re = TAILQ_FIRST(&s->refs);
7186 TAILQ_REMOVE(&s->refs, re, entry);
7187 got_ref_close(re->ref);
7192 static const struct got_error *
7193 open_ref_view(struct tog_view *view, struct got_repository *repo)
7195 const struct got_error *err = NULL;
7196 struct tog_ref_view_state *s = &view->state.ref;
7198 s->selected_entry = 0;
7201 TAILQ_INIT(&s->refs);
7202 STAILQ_INIT(&s->colors);
7204 err = ref_view_load_refs(s);
7208 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7209 err = add_color(&s->colors, "^refs/heads/",
7210 TOG_COLOR_REFS_HEADS,
7211 get_color_value("TOG_COLOR_REFS_HEADS"));
7215 err = add_color(&s->colors, "^refs/tags/",
7216 TOG_COLOR_REFS_TAGS,
7217 get_color_value("TOG_COLOR_REFS_TAGS"));
7221 err = add_color(&s->colors, "^refs/remotes/",
7222 TOG_COLOR_REFS_REMOTES,
7223 get_color_value("TOG_COLOR_REFS_REMOTES"));
7227 err = add_color(&s->colors, "^refs/got/backup/",
7228 TOG_COLOR_REFS_BACKUP,
7229 get_color_value("TOG_COLOR_REFS_BACKUP"));
7234 view->show = show_ref_view;
7235 view->input = input_ref_view;
7236 view->close = close_ref_view;
7237 view->search_start = search_start_ref_view;
7238 view->search_next = search_next_ref_view;
7241 free_colors(&s->colors);
7245 static const struct got_error *
7246 close_ref_view(struct tog_view *view)
7248 struct tog_ref_view_state *s = &view->state.ref;
7250 ref_view_free_refs(s);
7251 free_colors(&s->colors);
7256 static const struct got_error *
7257 resolve_reflist_entry(struct got_object_id **commit_id,
7258 struct tog_reflist_entry *re, struct got_repository *repo)
7260 const struct got_error *err = NULL;
7261 struct got_object_id *obj_id;
7262 struct got_tag_object *tag = NULL;
7267 err = got_ref_resolve(&obj_id, repo, re->ref);
7271 err = got_object_get_type(&obj_type, repo, obj_id);
7276 case GOT_OBJ_TYPE_COMMIT:
7277 *commit_id = obj_id;
7279 case GOT_OBJ_TYPE_TAG:
7280 err = got_object_open_as_tag(&tag, repo, obj_id);
7284 err = got_object_get_type(&obj_type, repo,
7285 got_object_tag_get_object_id(tag));
7288 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7289 err = got_error(GOT_ERR_OBJ_TYPE);
7292 *commit_id = got_object_id_dup(
7293 got_object_tag_get_object_id(tag));
7294 if (*commit_id == NULL) {
7295 err = got_error_from_errno("got_object_id_dup");
7300 err = got_error(GOT_ERR_OBJ_TYPE);
7306 got_object_tag_close(tag);
7314 static const struct got_error *
7315 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7316 struct tog_reflist_entry *re, struct got_repository *repo)
7318 struct tog_view *log_view;
7319 const struct got_error *err = NULL;
7320 struct got_object_id *commit_id = NULL;
7324 err = resolve_reflist_entry(&commit_id, re, repo);
7326 if (err->code != GOT_ERR_OBJ_TYPE)
7332 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7333 if (log_view == NULL) {
7334 err = got_error_from_errno("view_open");
7338 err = open_log_view(log_view, commit_id, repo,
7339 got_ref_get_name(re->ref), "", 0);
7342 view_close(log_view);
7344 *new_view = log_view;
7350 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7352 struct tog_reflist_entry *re;
7355 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7358 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7359 while (i++ < maxscroll) {
7362 s->first_displayed_entry = re;
7363 re = TAILQ_PREV(re, tog_reflist_head, entry);
7367 static const struct got_error *
7368 ref_scroll_down(struct tog_view *view, int maxscroll)
7370 struct tog_ref_view_state *s = &view->state.ref;
7371 struct tog_reflist_entry *next, *last;
7374 if (s->first_displayed_entry)
7375 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7377 next = TAILQ_FIRST(&s->refs);
7379 last = s->last_displayed_entry;
7380 while (next && n++ < maxscroll) {
7382 last = TAILQ_NEXT(last, entry);
7383 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7384 s->first_displayed_entry = next;
7385 next = TAILQ_NEXT(next, entry);
7392 static const struct got_error *
7393 search_start_ref_view(struct tog_view *view)
7395 struct tog_ref_view_state *s = &view->state.ref;
7397 s->matched_entry = NULL;
7402 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7404 regmatch_t regmatch;
7406 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7410 static const struct got_error *
7411 search_next_ref_view(struct tog_view *view)
7413 struct tog_ref_view_state *s = &view->state.ref;
7414 struct tog_reflist_entry *re = NULL;
7416 if (!view->searching) {
7417 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7421 if (s->matched_entry) {
7422 if (view->searching == TOG_SEARCH_FORWARD) {
7423 if (s->selected_entry)
7424 re = TAILQ_NEXT(s->selected_entry, entry);
7426 re = TAILQ_PREV(s->selected_entry,
7427 tog_reflist_head, entry);
7429 if (s->selected_entry == NULL)
7430 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7432 re = TAILQ_PREV(s->selected_entry,
7433 tog_reflist_head, entry);
7436 if (s->selected_entry)
7437 re = s->selected_entry;
7438 else if (view->searching == TOG_SEARCH_FORWARD)
7439 re = TAILQ_FIRST(&s->refs);
7441 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7446 if (s->matched_entry == NULL) {
7447 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7450 if (view->searching == TOG_SEARCH_FORWARD)
7451 re = TAILQ_FIRST(&s->refs);
7453 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7456 if (match_reflist_entry(re, &view->regex)) {
7457 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7458 s->matched_entry = re;
7462 if (view->searching == TOG_SEARCH_FORWARD)
7463 re = TAILQ_NEXT(re, entry);
7465 re = TAILQ_PREV(re, tog_reflist_head, entry);
7468 if (s->matched_entry) {
7469 s->first_displayed_entry = s->matched_entry;
7476 static const struct got_error *
7477 show_ref_view(struct tog_view *view)
7479 const struct got_error *err = NULL;
7480 struct tog_ref_view_state *s = &view->state.ref;
7481 struct tog_reflist_entry *re;
7484 struct tog_color *tc;
7486 int limit = view->nlines;
7488 werase(view->window);
7491 if (view_is_hsplit_top(view))
7492 --limit; /* border */
7497 re = s->first_displayed_entry;
7499 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7501 return got_error_from_errno("asprintf");
7503 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7508 if (view_needs_focus_indication(view))
7509 wstandout(view->window);
7510 waddwstr(view->window, wline);
7511 if (view_needs_focus_indication(view))
7512 wstandend(view->window);
7517 if (width < view->ncols - 1)
7518 waddch(view->window, '\n');
7523 while (re && limit > 0) {
7525 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7528 struct got_commit_object *ci;
7529 struct got_tag_object *tag;
7530 struct got_object_id *id;
7534 err = got_ref_resolve(&id, s->repo, re->ref);
7537 err = got_object_open_as_tag(&tag, s->repo, id);
7539 if (err->code != GOT_ERR_OBJ_TYPE) {
7543 err = got_object_open_as_commit(&ci, s->repo,
7549 t = got_object_commit_get_committer_time(ci);
7550 got_object_commit_close(ci);
7552 t = got_object_tag_get_tagger_time(tag);
7553 got_object_tag_close(tag);
7556 if (gmtime_r(&t, &tm) == NULL)
7557 return got_error_from_errno("gmtime_r");
7558 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7559 return got_error(GOT_ERR_NO_SPACE);
7561 if (got_ref_is_symbolic(re->ref)) {
7562 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7563 ymd : "", got_ref_get_name(re->ref),
7564 got_ref_get_symref_target(re->ref)) == -1)
7565 return got_error_from_errno("asprintf");
7566 } else if (s->show_ids) {
7567 struct got_object_id *id;
7569 err = got_ref_resolve(&id, s->repo, re->ref);
7572 err = got_object_id_str(&id_str, id);
7577 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7578 got_ref_get_name(re->ref), id_str) == -1) {
7579 err = got_error_from_errno("asprintf");
7586 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7587 got_ref_get_name(re->ref)) == -1)
7588 return got_error_from_errno("asprintf");
7590 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7596 if (n == s->selected) {
7598 wstandout(view->window);
7599 s->selected_entry = re;
7601 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7603 wattr_on(view->window,
7604 COLOR_PAIR(tc->colorpair), NULL);
7605 waddwstr(view->window, wline);
7607 wattr_off(view->window,
7608 COLOR_PAIR(tc->colorpair), NULL);
7609 if (width < view->ncols - 1)
7610 waddch(view->window, '\n');
7611 if (n == s->selected && view->focussed)
7612 wstandend(view->window);
7618 s->last_displayed_entry = re;
7621 re = TAILQ_NEXT(re, entry);
7628 static const struct got_error *
7629 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7630 struct tog_reflist_entry *re, struct got_repository *repo)
7632 const struct got_error *err = NULL;
7633 struct got_object_id *commit_id = NULL;
7634 struct tog_view *tree_view;
7638 err = resolve_reflist_entry(&commit_id, re, repo);
7640 if (err->code != GOT_ERR_OBJ_TYPE)
7647 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7648 if (tree_view == NULL) {
7649 err = got_error_from_errno("view_open");
7653 err = open_tree_view(tree_view, commit_id,
7654 got_ref_get_name(re->ref), repo);
7658 *new_view = tree_view;
7663 static const struct got_error *
7664 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7666 const struct got_error *err = NULL;
7667 struct tog_ref_view_state *s = &view->state.ref;
7668 struct tog_view *log_view, *tree_view;
7669 struct tog_reflist_entry *re;
7670 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7674 s->show_ids = !s->show_ids;
7678 s->show_date = !s->show_date;
7682 s->sort_by_date = !s->sort_by_date;
7684 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7685 got_ref_cmp_by_commit_timestamp_descending :
7686 tog_ref_cmp_by_name, s->repo);
7689 got_reflist_object_id_map_free(tog_refs_idmap);
7690 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7691 &tog_refs, s->repo);
7694 ref_view_free_refs(s);
7695 err = ref_view_load_refs(s);
7700 if (!s->selected_entry)
7702 if (view_is_parent_view(view))
7703 view_get_split(view, &begin_y, &begin_x);
7705 err = log_ref_entry(&log_view, begin_y, begin_x,
7706 s->selected_entry, s->repo);
7710 if (view_is_parent_view(view) &&
7711 view->mode == TOG_VIEW_SPLIT_HRZN) {
7712 err = view_init_hsplit(view, begin_y);
7718 log_view->focussed = 1;
7719 log_view->mode = view->mode;
7720 log_view->nlines = view->lines - begin_y;
7721 if (view_is_parent_view(view)) {
7722 view_transfer_size(log_view, view);
7723 err = view_close_child(view);
7726 err = view_set_child(view, log_view);
7729 view->focus_child = 1;
7731 *new_view = log_view;
7735 if (!s->selected_entry)
7737 if (view_is_parent_view(view))
7738 view_get_split(view, &begin_y, &begin_x);
7739 err = browse_ref_tree(&tree_view, begin_y, begin_x,
7740 s->selected_entry, s->repo);
7741 if (err || tree_view == NULL)
7743 if (view_is_parent_view(view) &&
7744 view->mode == TOG_VIEW_SPLIT_HRZN) {
7745 err = view_init_hsplit(view, begin_y);
7750 tree_view->focussed = 1;
7751 tree_view->mode = view->mode;
7752 tree_view->nlines = view->lines - begin_y;
7753 if (view_is_parent_view(view)) {
7754 view_transfer_size(tree_view, view);
7755 err = view_close_child(view);
7758 err = view_set_child(view, tree_view);
7761 view->focus_child = 1;
7763 *new_view = tree_view;
7769 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7773 int eos = view->nlines - 1;
7775 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7779 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7780 for (n = 0; n < eos; n++) {
7783 s->first_displayed_entry = re;
7784 re = TAILQ_PREV(re, tog_reflist_head, entry);
7787 s->selected = n - 1;
7793 if (s->selected > 0) {
7797 ref_scroll_up(s, 1);
7798 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7808 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7809 s->selected -= MIN(nscroll, s->selected);
7810 ref_scroll_up(s, MAX(0, nscroll));
7811 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7817 if (s->selected < s->ndisplayed - 1) {
7821 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7822 /* can't scroll any further */
7826 ref_scroll_down(view, 1);
7836 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7837 /* can't scroll any further; move cursor down */
7838 if (s->selected < s->ndisplayed - 1)
7839 s->selected += MIN(nscroll,
7840 s->ndisplayed - s->selected - 1);
7841 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7842 s->selected += s->ndisplayed - s->selected - 1;
7846 ref_scroll_down(view, nscroll);
7851 err = tog_load_refs(s->repo, s->sort_by_date);
7854 ref_view_free_refs(s);
7855 err = ref_view_load_refs(s);
7858 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7859 s->selected = view->nlines - 2;
7873 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7878 static const struct got_error *
7879 cmd_ref(int argc, char *argv[])
7881 const struct got_error *error;
7882 struct got_repository *repo = NULL;
7883 struct got_worktree *worktree = NULL;
7884 char *cwd = NULL, *repo_path = NULL;
7886 struct tog_view *view;
7887 int *pack_fds = NULL;
7889 while ((ch = getopt(argc, argv, "r:")) != -1) {
7892 repo_path = realpath(optarg, NULL);
7893 if (repo_path == NULL)
7894 return got_error_from_errno2("realpath",
7909 error = got_repo_pack_fds_open(&pack_fds);
7913 if (repo_path == NULL) {
7914 cwd = getcwd(NULL, 0);
7916 return got_error_from_errno("getcwd");
7917 error = got_worktree_open(&worktree, cwd);
7918 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7922 strdup(got_worktree_get_repo_path(worktree));
7924 repo_path = strdup(cwd);
7925 if (repo_path == NULL) {
7926 error = got_error_from_errno("strdup");
7931 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7937 error = apply_unveil(got_repo_get_path(repo), NULL);
7941 error = tog_load_refs(repo, 0);
7945 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7947 error = got_error_from_errno("view_open");
7951 error = open_ref_view(view, repo);
7956 /* Release work tree lock. */
7957 got_worktree_close(worktree);
7960 error = view_loop(view);
7965 const struct got_error *close_err = got_repo_close(repo);
7970 const struct got_error *pack_err =
7971 got_repo_pack_fds_close(pack_fds);
7980 * If view was scrolled down to move the selected line into view when opening a
7981 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7984 offset_selection_up(struct tog_view *view)
7986 switch (view->type) {
7987 case TOG_VIEW_BLAME: {
7988 struct tog_blame_view_state *s = &view->state.blame;
7989 if (s->first_displayed_line == 1) {
7990 s->selected_line = MAX(s->selected_line - view->offset,
7994 if (s->first_displayed_line > view->offset)
7995 s->first_displayed_line -= view->offset;
7997 s->first_displayed_line = 1;
7998 s->selected_line += view->offset;
8002 log_scroll_up(&view->state.log, view->offset);
8003 view->state.log.selected += view->offset;
8006 ref_scroll_up(&view->state.ref, view->offset);
8007 view->state.ref.selected += view->offset;
8010 tree_scroll_up(&view->state.tree, view->offset);
8011 view->state.tree.selected += view->offset;
8021 * If the selected line is in the section of screen covered by the bottom split,
8022 * scroll down offset lines to move it into view and index its new position.
8024 static const struct got_error *
8025 offset_selection_down(struct tog_view *view)
8027 const struct got_error *err = NULL;
8028 const struct got_error *(*scrolld)(struct tog_view *, int);
8029 int *selected = NULL;
8032 switch (view->type) {
8033 case TOG_VIEW_BLAME: {
8034 struct tog_blame_view_state *s = &view->state.blame;
8037 if (s->selected_line > view->nlines - header) {
8038 offset = abs(view->nlines - s->selected_line - header);
8039 s->first_displayed_line += offset;
8040 s->selected_line -= offset;
8041 view->offset = offset;
8045 case TOG_VIEW_LOG: {
8046 struct tog_log_view_state *s = &view->state.log;
8047 scrolld = &log_scroll_down;
8048 header = view_is_parent_view(view) ? 3 : 2;
8049 selected = &s->selected;
8052 case TOG_VIEW_REF: {
8053 struct tog_ref_view_state *s = &view->state.ref;
8054 scrolld = &ref_scroll_down;
8056 selected = &s->selected;
8059 case TOG_VIEW_TREE: {
8060 struct tog_tree_view_state *s = &view->state.tree;
8061 scrolld = &tree_scroll_down;
8063 selected = &s->selected;
8073 if (selected && *selected > view->nlines - header) {
8074 offset = abs(view->nlines - *selected - header);
8075 view->offset = offset;
8076 if (scrolld && offset) {
8077 err = scrolld(view, offset);
8078 *selected -= offset;
8086 list_commands(FILE *fp)
8090 fprintf(fp, "commands:");
8091 for (i = 0; i < nitems(tog_commands); i++) {
8092 const struct tog_cmd *cmd = &tog_commands[i];
8093 fprintf(fp, " %s", cmd->name);
8099 usage(int hflag, int status)
8101 FILE *fp = (status == 0) ? stdout : stderr;
8103 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8106 fprintf(fp, "lazy usage: %s path\n", getprogname());
8113 make_argv(int argc, ...)
8121 argv = calloc(argc, sizeof(char *));
8124 for (i = 0; i < argc; i++) {
8125 argv[i] = strdup(va_arg(ap, char *));
8126 if (argv[i] == NULL)
8135 * Try to convert 'tog path' into a 'tog log path' command.
8136 * The user could simply have mistyped the command rather than knowingly
8137 * provided a path. So check whether argv[0] can in fact be resolved
8138 * to a path in the HEAD commit and print a special error if not.
8139 * This hack is for mpi@ <3
8141 static const struct got_error *
8142 tog_log_with_path(int argc, char *argv[])
8144 const struct got_error *error = NULL, *close_err;
8145 const struct tog_cmd *cmd = NULL;
8146 struct got_repository *repo = NULL;
8147 struct got_worktree *worktree = NULL;
8148 struct got_object_id *commit_id = NULL, *id = NULL;
8149 struct got_commit_object *commit = NULL;
8150 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8151 char *commit_id_str = NULL, **cmd_argv = NULL;
8152 int *pack_fds = NULL;
8154 cwd = getcwd(NULL, 0);
8156 return got_error_from_errno("getcwd");
8158 error = got_repo_pack_fds_open(&pack_fds);
8162 error = got_worktree_open(&worktree, cwd);
8163 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8167 repo_path = strdup(got_worktree_get_repo_path(worktree));
8169 repo_path = strdup(cwd);
8170 if (repo_path == NULL) {
8171 error = got_error_from_errno("strdup");
8175 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8179 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8184 error = tog_load_refs(repo, 0);
8187 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8188 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8189 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8194 got_worktree_close(worktree);
8198 error = got_object_open_as_commit(&commit, repo, commit_id);
8202 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8204 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8206 fprintf(stderr, "%s: '%s' is no known command or path\n",
8207 getprogname(), argv[0]);
8212 close_err = got_repo_close(repo);
8217 error = got_object_id_str(&commit_id_str, commit_id);
8221 cmd = &tog_commands[0]; /* log */
8223 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8224 error = cmd->cmd_main(argc, cmd_argv);
8227 close_err = got_repo_close(repo);
8232 got_object_commit_close(commit);
8234 got_worktree_close(worktree);
8236 const struct got_error *pack_err =
8237 got_repo_pack_fds_close(pack_fds);
8242 free(commit_id_str);
8249 for (i = 0; i < argc; i++)
8258 main(int argc, char *argv[])
8260 const struct got_error *error = NULL;
8261 const struct tog_cmd *cmd = NULL;
8262 int ch, hflag = 0, Vflag = 0;
8263 char **cmd_argv = NULL;
8264 static const struct option longopts[] = {
8265 { "version", no_argument, NULL, 'V' },
8268 char *diff_algo_str = NULL;
8270 setlocale(LC_CTYPE, "");
8272 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8292 got_version_print_str();
8297 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8305 /* Build an argument vector which runs a default command. */
8306 cmd = &tog_commands[0];
8308 cmd_argv = make_argv(argc, cmd->name);
8312 /* Did the user specify a command? */
8313 for (i = 0; i < nitems(tog_commands); i++) {
8314 if (strncmp(tog_commands[i].name, argv[0],
8315 strlen(argv[0])) == 0) {
8316 cmd = &tog_commands[i];
8322 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8323 if (diff_algo_str) {
8324 if (strcasecmp(diff_algo_str, "patience") == 0)
8325 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8326 if (strcasecmp(diff_algo_str, "myers") == 0)
8327 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8333 /* No command specified; try log with a path */
8334 error = tog_log_with_path(argc, argv);
8339 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8346 for (i = 0; i < argc; i++)
8351 if (error && error->code != GOT_ERR_CANCELLED)
8352 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);