2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_opentemp.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
57 #include "got_worktree.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 #define CTRL(x) ((x) & 0x1f)
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static const struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
114 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
116 #define TOG_EOF_STRING "(END)"
118 struct commit_queue_entry {
119 TAILQ_ENTRY(commit_queue_entry) entry;
120 struct got_object_id *id;
121 struct got_commit_object *commit;
124 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
125 struct commit_queue {
127 struct commit_queue_head head;
131 STAILQ_ENTRY(tog_color) entry;
135 STAILQ_HEAD(tog_colors, tog_color);
137 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
138 static struct got_reflist_object_id_map *tog_refs_idmap;
139 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
141 static const struct got_error *
142 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
143 struct got_reference* re2)
145 const char *name1 = got_ref_get_name(re1);
146 const char *name2 = got_ref_get_name(re2);
147 int isbackup1, isbackup2;
149 /* Sort backup refs towards the bottom of the list. */
150 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
151 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
152 if (!isbackup1 && isbackup2) {
155 } else if (isbackup1 && !isbackup2) {
160 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
164 static const struct got_error *
165 tog_load_refs(struct got_repository *repo, int sort_by_date)
167 const struct got_error *err;
169 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
170 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
175 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
182 if (tog_refs_idmap) {
183 got_reflist_object_id_map_free(tog_refs_idmap);
184 tog_refs_idmap = NULL;
186 got_ref_list_free(&tog_refs);
189 static const struct got_error *
190 add_color(struct tog_colors *colors, const char *pattern,
191 int idx, short color)
193 const struct got_error *err = NULL;
194 struct tog_color *tc;
197 if (idx < 1 || idx > COLOR_PAIRS - 1)
200 init_pair(idx, color, -1);
202 tc = calloc(1, sizeof(*tc));
204 return got_error_from_errno("calloc");
205 regerr = regcomp(&tc->regex, pattern,
206 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
208 static char regerr_msg[512];
209 static char err_msg[512];
210 regerror(regerr, &tc->regex, regerr_msg,
212 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
214 err = got_error_msg(GOT_ERR_REGEX, err_msg);
219 STAILQ_INSERT_HEAD(colors, tc, entry);
224 free_colors(struct tog_colors *colors)
226 struct tog_color *tc;
228 while (!STAILQ_EMPTY(colors)) {
229 tc = STAILQ_FIRST(colors);
230 STAILQ_REMOVE_HEAD(colors, entry);
236 static struct tog_color *
237 get_color(struct tog_colors *colors, int colorpair)
239 struct tog_color *tc = NULL;
241 STAILQ_FOREACH(tc, colors, entry) {
242 if (tc->colorpair == colorpair)
250 default_color_value(const char *envvar)
252 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
253 return COLOR_MAGENTA;
254 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
256 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
258 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
260 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
261 return COLOR_MAGENTA;
262 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
263 return COLOR_MAGENTA;
264 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
266 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
268 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
270 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
272 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
274 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
276 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
277 return COLOR_MAGENTA;
278 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
280 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
287 get_color_value(const char *envvar)
289 const char *val = getenv(envvar);
292 return default_color_value(envvar);
294 if (strcasecmp(val, "black") == 0)
296 if (strcasecmp(val, "red") == 0)
298 if (strcasecmp(val, "green") == 0)
300 if (strcasecmp(val, "yellow") == 0)
302 if (strcasecmp(val, "blue") == 0)
304 if (strcasecmp(val, "magenta") == 0)
305 return COLOR_MAGENTA;
306 if (strcasecmp(val, "cyan") == 0)
308 if (strcasecmp(val, "white") == 0)
310 if (strcasecmp(val, "default") == 0)
313 return default_color_value(envvar);
317 struct tog_diff_view_state {
318 struct got_object_id *id1, *id2;
319 const char *label1, *label2;
322 int first_displayed_line;
323 int last_displayed_line;
326 int ignore_whitespace;
328 struct got_repository *repo;
329 struct tog_colors colors;
335 /* passed from log or blame view; may be NULL */
336 struct tog_view *parent_view;
339 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
340 static volatile sig_atomic_t tog_thread_error;
342 struct tog_log_thread_args {
343 pthread_cond_t need_commits;
344 pthread_cond_t commit_loaded;
347 struct got_commit_graph *graph;
348 struct commit_queue *commits;
349 const char *in_repo_path;
350 struct got_object_id *start_id;
351 struct got_repository *repo;
355 struct commit_queue_entry **first_displayed_entry;
356 struct commit_queue_entry **selected_entry;
358 int *search_next_done;
362 struct tog_log_view_state {
363 struct commit_queue commits;
364 struct commit_queue_entry *first_displayed_entry;
365 struct commit_queue_entry *last_displayed_entry;
366 struct commit_queue_entry *selected_entry;
371 struct got_repository *repo;
372 struct got_object_id *start_id;
375 struct tog_log_thread_args thread_args;
376 struct commit_queue_entry *matched_entry;
377 struct commit_queue_entry *search_entry;
378 struct tog_colors colors;
382 #define TOG_COLOR_DIFF_MINUS 1
383 #define TOG_COLOR_DIFF_PLUS 2
384 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
385 #define TOG_COLOR_DIFF_META 4
386 #define TOG_COLOR_TREE_SUBMODULE 5
387 #define TOG_COLOR_TREE_SYMLINK 6
388 #define TOG_COLOR_TREE_DIRECTORY 7
389 #define TOG_COLOR_TREE_EXECUTABLE 8
390 #define TOG_COLOR_COMMIT 9
391 #define TOG_COLOR_AUTHOR 10
392 #define TOG_COLOR_DATE 11
393 #define TOG_COLOR_REFS_HEADS 12
394 #define TOG_COLOR_REFS_TAGS 13
395 #define TOG_COLOR_REFS_REMOTES 14
396 #define TOG_COLOR_REFS_BACKUP 15
398 struct tog_blame_cb_args {
399 struct tog_blame_line *lines; /* one per line */
402 struct tog_view *view;
403 struct got_object_id *commit_id;
407 struct tog_blame_thread_args {
409 struct got_repository *repo;
410 struct tog_blame_cb_args *cb_args;
412 got_cancel_cb cancel_cb;
419 struct tog_blame_line *lines;
423 struct tog_blame_thread_args thread_args;
424 struct tog_blame_cb_args cb_args;
429 struct tog_blame_view_state {
430 int first_displayed_line;
431 int last_displayed_line;
433 int last_diffed_line;
437 struct got_object_id_queue blamed_commits;
438 struct got_object_qid *blamed_commit;
440 struct got_repository *repo;
441 struct got_object_id *commit_id;
442 struct tog_blame blame;
444 struct tog_colors colors;
447 struct tog_parent_tree {
448 TAILQ_ENTRY(tog_parent_tree) entry;
449 struct got_tree_object *tree;
450 struct got_tree_entry *first_displayed_entry;
451 struct got_tree_entry *selected_entry;
455 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
457 struct tog_tree_view_state {
459 struct got_object_id *commit_id;/* commit which this tree belongs to */
460 struct got_tree_object *root; /* the commit's root tree entry */
461 struct got_tree_object *tree; /* currently displayed (sub-)tree */
462 struct got_tree_entry *first_displayed_entry;
463 struct got_tree_entry *last_displayed_entry;
464 struct got_tree_entry *selected_entry;
465 int ndisplayed, selected, show_ids;
466 struct tog_parent_trees parents; /* parent trees of current sub-tree */
468 struct got_repository *repo;
469 struct got_tree_entry *matched_entry;
470 struct tog_colors colors;
473 struct tog_reflist_entry {
474 TAILQ_ENTRY(tog_reflist_entry) entry;
475 struct got_reference *ref;
479 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
481 struct tog_ref_view_state {
482 struct tog_reflist_head refs;
483 struct tog_reflist_entry *first_displayed_entry;
484 struct tog_reflist_entry *last_displayed_entry;
485 struct tog_reflist_entry *selected_entry;
486 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
487 struct got_repository *repo;
488 struct tog_reflist_entry *matched_entry;
489 struct tog_colors colors;
493 * We implement two types of views: parent views and child views.
495 * The 'Tab' key switches focus between a parent view and its child view.
496 * Child views are shown side-by-side to their parent view, provided
497 * there is enough screen estate.
499 * When a new view is opened from within a parent view, this new view
500 * becomes a child view of the parent view, replacing any existing child.
502 * When a new view is opened from within a child view, this new view
503 * becomes a parent view which will obscure the views below until the
504 * user quits the new parent view by typing 'q'.
506 * This list of views contains parent views only.
507 * Child views are only pointed to by their parent view.
509 TAILQ_HEAD(tog_view_list_head, tog_view);
512 TAILQ_ENTRY(tog_view) entry;
515 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
516 int resized_y, resized_x; /* begin_y/x based on user resizing */
517 int maxx, x; /* max column and current start column */
518 int lines, cols; /* copies of LINES and COLS */
519 int nscrolled, offset; /* lines scrolled and hsplit line offset */
520 int ch, count; /* current keymap and count prefix */
521 int resized; /* set when in a resize event */
522 int focussed; /* Only set on one parent or child view at a time. */
524 struct tog_view *parent;
525 struct tog_view *child;
528 * This flag is initially set on parent views when a new child view
529 * is created. It gets toggled when the 'Tab' key switches focus
530 * between parent and child.
531 * The flag indicates whether focus should be passed on to our child
532 * view if this parent view gets picked for focus after another parent
533 * view was closed. This prevents child views from losing focus in such
538 enum tog_view_mode mode;
539 /* type-specific state */
540 enum tog_view_type type;
542 struct tog_diff_view_state diff;
543 struct tog_log_view_state log;
544 struct tog_blame_view_state blame;
545 struct tog_tree_view_state tree;
546 struct tog_ref_view_state ref;
549 const struct got_error *(*show)(struct tog_view *);
550 const struct got_error *(*input)(struct tog_view **,
551 struct tog_view *, int);
552 const struct got_error *(*reset)(struct tog_view *);
553 const struct got_error *(*resize)(struct tog_view *, int);
554 const struct got_error *(*close)(struct tog_view *);
556 const struct got_error *(*search_start)(struct tog_view *);
557 const struct got_error *(*search_next)(struct tog_view *);
560 #define TOG_SEARCH_FORWARD 1
561 #define TOG_SEARCH_BACKWARD 2
562 int search_next_done;
563 #define TOG_SEARCH_HAVE_MORE 1
564 #define TOG_SEARCH_NO_MORE 2
565 #define TOG_SEARCH_HAVE_NONE 3
570 static const struct got_error *open_diff_view(struct tog_view *,
571 struct got_object_id *, struct got_object_id *,
572 const char *, const char *, int, int, int, struct tog_view *,
573 struct got_repository *);
574 static const struct got_error *show_diff_view(struct tog_view *);
575 static const struct got_error *input_diff_view(struct tog_view **,
576 struct tog_view *, int);
577 static const struct got_error *reset_diff_view(struct tog_view *);
578 static const struct got_error* close_diff_view(struct tog_view *);
579 static const struct got_error *search_start_diff_view(struct tog_view *);
580 static const struct got_error *search_next_diff_view(struct tog_view *);
582 static const struct got_error *open_log_view(struct tog_view *,
583 struct got_object_id *, struct got_repository *,
584 const char *, const char *, int);
585 static const struct got_error * show_log_view(struct tog_view *);
586 static const struct got_error *input_log_view(struct tog_view **,
587 struct tog_view *, int);
588 static const struct got_error *resize_log_view(struct tog_view *, int);
589 static const struct got_error *close_log_view(struct tog_view *);
590 static const struct got_error *search_start_log_view(struct tog_view *);
591 static const struct got_error *search_next_log_view(struct tog_view *);
593 static const struct got_error *open_blame_view(struct tog_view *, char *,
594 struct got_object_id *, struct got_repository *);
595 static const struct got_error *show_blame_view(struct tog_view *);
596 static const struct got_error *input_blame_view(struct tog_view **,
597 struct tog_view *, int);
598 static const struct got_error *reset_blame_view(struct tog_view *);
599 static const struct got_error *close_blame_view(struct tog_view *);
600 static const struct got_error *search_start_blame_view(struct tog_view *);
601 static const struct got_error *search_next_blame_view(struct tog_view *);
603 static const struct got_error *open_tree_view(struct tog_view *,
604 struct got_object_id *, const char *, struct got_repository *);
605 static const struct got_error *show_tree_view(struct tog_view *);
606 static const struct got_error *input_tree_view(struct tog_view **,
607 struct tog_view *, int);
608 static const struct got_error *close_tree_view(struct tog_view *);
609 static const struct got_error *search_start_tree_view(struct tog_view *);
610 static const struct got_error *search_next_tree_view(struct tog_view *);
612 static const struct got_error *open_ref_view(struct tog_view *,
613 struct got_repository *);
614 static const struct got_error *show_ref_view(struct tog_view *);
615 static const struct got_error *input_ref_view(struct tog_view **,
616 struct tog_view *, int);
617 static const struct got_error *close_ref_view(struct tog_view *);
618 static const struct got_error *search_start_ref_view(struct tog_view *);
619 static const struct got_error *search_next_ref_view(struct tog_view *);
621 static volatile sig_atomic_t tog_sigwinch_received;
622 static volatile sig_atomic_t tog_sigpipe_received;
623 static volatile sig_atomic_t tog_sigcont_received;
624 static volatile sig_atomic_t tog_sigint_received;
625 static volatile sig_atomic_t tog_sigterm_received;
628 tog_sigwinch(int signo)
630 tog_sigwinch_received = 1;
634 tog_sigpipe(int signo)
636 tog_sigpipe_received = 1;
640 tog_sigcont(int signo)
642 tog_sigcont_received = 1;
646 tog_sigint(int signo)
648 tog_sigint_received = 1;
652 tog_sigterm(int signo)
654 tog_sigterm_received = 1;
658 tog_fatal_signal_received(void)
660 return (tog_sigpipe_received ||
661 tog_sigint_received || tog_sigint_received);
664 static const struct got_error *
665 view_close(struct tog_view *view)
667 const struct got_error *err = NULL, *child_err = NULL;
670 child_err = view_close(view->child);
674 err = view->close(view);
676 del_panel(view->panel);
678 delwin(view->window);
680 return err ? err : child_err;
683 static struct tog_view *
684 view_open(int nlines, int ncols, int begin_y, int begin_x,
685 enum tog_view_type type)
687 struct tog_view *view = calloc(1, sizeof(*view));
695 view->nlines = nlines ? nlines : LINES - begin_y;
696 view->ncols = ncols ? ncols : COLS - begin_x;
697 view->begin_y = begin_y;
698 view->begin_x = begin_x;
699 view->window = newwin(nlines, ncols, begin_y, begin_x);
700 if (view->window == NULL) {
704 view->panel = new_panel(view->window);
705 if (view->panel == NULL ||
706 set_panel_userptr(view->panel, view) != OK) {
711 keypad(view->window, TRUE);
716 view_split_begin_x(int begin_x)
718 if (begin_x > 0 || COLS < 120)
720 return (COLS - MAX(COLS / 2, 80));
723 /* XXX Stub till we decide what to do. */
725 view_split_begin_y(int lines)
727 return lines * HSPLIT_SCALE;
730 static const struct got_error *view_resize(struct tog_view *);
732 static const struct got_error *
733 view_splitscreen(struct tog_view *view)
735 const struct got_error *err = NULL;
737 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
738 if (view->resized_y && view->resized_y < view->lines)
739 view->begin_y = view->resized_y;
741 view->begin_y = view_split_begin_y(view->nlines);
743 } else if (!view->resized) {
744 if (view->resized_x && view->resized_x < view->cols - 1 &&
746 view->begin_x = view->resized_x;
748 view->begin_x = view_split_begin_x(0);
751 view->nlines = LINES - view->begin_y;
752 view->ncols = COLS - view->begin_x;
755 err = view_resize(view);
759 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
760 view->parent->nlines = view->begin_y;
762 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
763 return got_error_from_errno("mvwin");
768 static const struct got_error *
769 view_fullscreen(struct tog_view *view)
771 const struct got_error *err = NULL;
774 view->begin_y = view->resized ? view->begin_y : 0;
775 view->nlines = view->resized ? view->nlines : LINES;
779 err = view_resize(view);
783 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
784 return got_error_from_errno("mvwin");
790 view_is_parent_view(struct tog_view *view)
792 return view->parent == NULL;
796 view_is_splitscreen(struct tog_view *view)
798 return view->begin_x > 0 || view->begin_y > 0;
802 view_is_fullscreen(struct tog_view *view)
804 return view->nlines == LINES && view->ncols == COLS;
808 view_is_hsplit_top(struct tog_view *view)
810 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
811 view_is_splitscreen(view->child);
815 view_border(struct tog_view *view)
818 const struct tog_view *view_above;
821 return view_border(view->parent);
823 panel = panel_above(view->panel);
827 view_above = panel_userptr(panel);
828 if (view->mode == TOG_VIEW_SPLIT_HRZN)
829 mvwhline(view->window, view_above->begin_y - 1,
830 view->begin_x, got_locale_is_utf8() ?
831 ACS_HLINE : '-', view->ncols);
833 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
834 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
837 static const struct got_error *view_init_hsplit(struct tog_view *, int);
838 static const struct got_error *request_log_commits(struct tog_view *);
839 static const struct got_error *offset_selection_down(struct tog_view *);
840 static void offset_selection_up(struct tog_view *);
841 static void view_get_split(struct tog_view *, int *, int *);
843 static const struct got_error *
844 view_resize(struct tog_view *view)
846 const struct got_error *err = NULL;
847 int dif, nlines, ncols;
849 dif = LINES - view->lines; /* line difference */
851 if (view->lines > LINES)
852 nlines = view->nlines - (view->lines - LINES);
854 nlines = view->nlines + (LINES - view->lines);
855 if (view->cols > COLS)
856 ncols = view->ncols - (view->cols - COLS);
858 ncols = view->ncols + (COLS - view->cols);
861 int hs = view->child->begin_y;
863 if (!view_is_fullscreen(view))
864 view->child->begin_x = view_split_begin_x(view->begin_x);
865 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
866 view->child->begin_x == 0) {
869 view_fullscreen(view->child);
870 if (view->child->focussed)
871 show_panel(view->child->panel);
873 show_panel(view->panel);
875 ncols = view->child->begin_x;
877 view_splitscreen(view->child);
878 show_panel(view->child->panel);
881 * XXX This is ugly and needs to be moved into the above
882 * logic but "works" for now and my attempts at moving it
883 * break either 'tab' or 'F' key maps in horizontal splits.
886 err = view_splitscreen(view->child);
889 if (dif < 0) { /* top split decreased */
890 err = offset_selection_down(view);
897 show_panel(view->child->panel);
898 nlines = view->nlines;
900 } else if (view->parent == NULL)
903 if (view->resize && dif > 0) {
904 err = view->resize(view, dif);
909 if (wresize(view->window, nlines, ncols) == ERR)
910 return got_error_from_errno("wresize");
911 if (replace_panel(view->panel, view->window) == ERR)
912 return got_error_from_errno("replace_panel");
913 wclear(view->window);
915 view->nlines = nlines;
923 static const struct got_error *
924 resize_log_view(struct tog_view *view, int increase)
926 struct tog_log_view_state *s = &view->state.log;
927 const struct got_error *err = NULL;
928 int n = s->selected_entry->idx + view->lines - s->selected;
931 * Request commits to account for the increased
932 * height so we have enough to populate the view.
934 if (s->commits.ncommits < n) {
935 view->nscrolled = n - s->commits.ncommits + increase + 1;
936 err = request_log_commits(view);
943 view_adjust_offset(struct tog_view *view, int n)
948 if (view->parent && view->parent->offset) {
949 if (view->parent->offset + n >= 0)
950 view->parent->offset += n;
952 view->parent->offset = 0;
953 } else if (view->offset) {
954 if (view->offset - n >= 0)
961 static const struct got_error *
962 view_resize_split(struct tog_view *view, int resize)
964 const struct got_error *err = NULL;
965 struct tog_view *v = NULL;
972 if (!v->child || !view_is_splitscreen(v->child))
975 v->resized = v->child->resized = resize; /* lock for resize event */
977 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
978 int y = v->child->begin_y;
980 if (v->child->resized_y)
981 v->child->begin_y = v->child->resized_y;
983 v->child->begin_y -= resize;
985 v->child->begin_y += resize;
986 if (v->child->begin_y < 3) {
988 v->child->begin_y = 3;
989 } else if (v->child->begin_y > LINES - 1) {
991 v->child->begin_y = LINES - 1;
994 v->child->ncols = COLS;
995 view_adjust_offset(view, resize);
996 err = view_init_hsplit(v, v->child->begin_y);
999 v->child->resized_y = v->child->begin_y;
1000 if (y > v->child->begin_y && v->child->type == TOG_VIEW_LOG)
1001 v->child->nscrolled = y - v->child->begin_y;
1002 else if (y < v->child->begin_y && v->type == TOG_VIEW_LOG)
1003 v->nscrolled = v->child->begin_y - y;
1005 if (v->child->resized_x)
1006 v->child->begin_x = v->child->resized_x;
1008 v->child->begin_x -= resize;
1010 v->child->begin_x += resize;
1011 if (v->child->begin_x < 11) {
1013 v->child->begin_x = 11;
1014 } else if (v->child->begin_x > COLS - 1) {
1016 v->child->begin_x = COLS - 1;
1018 v->child->resized_x = v->child->begin_x;
1021 v->child->mode = v->mode;
1022 v->child->nlines = v->lines - v->child->begin_y;
1023 v->child->ncols = v->cols - v->child->begin_x;
1026 err = view_fullscreen(v);
1029 err = view_splitscreen(v->child);
1033 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1034 err = offset_selection_down(v->child);
1040 err = request_log_commits(v);
1041 else if (v->child->nscrolled)
1042 err = request_log_commits(v->child);
1044 v->resized = v->child->resized = 0;
1050 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1052 struct tog_view *v = src->child ? src->child : src;
1054 dst->resized_x = v->resized_x;
1055 dst->resized_y = v->resized_y;
1058 static const struct got_error *
1059 view_close_child(struct tog_view *view)
1061 const struct got_error *err = NULL;
1063 if (view->child == NULL)
1066 err = view_close(view->child);
1071 static const struct got_error *
1072 view_set_child(struct tog_view *view, struct tog_view *child)
1074 const struct got_error *err = NULL;
1076 view->child = child;
1077 child->parent = view;
1079 err = view_resize(view);
1083 if (view->child->resized_x || view->child->resized_y)
1084 err = view_resize_split(view, 0);
1090 tog_resizeterm(void)
1093 struct winsize size;
1095 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1096 cols = 80; /* Default */
1100 lines = size.ws_row;
1102 resize_term(lines, cols);
1105 static const struct got_error *
1106 view_search_start(struct tog_view *view)
1108 const struct got_error *err = NULL;
1109 struct tog_view *v = view;
1113 if (view->search_started) {
1114 regfree(&view->regex);
1115 view->searching = 0;
1116 memset(&view->regmatch, 0, sizeof(view->regmatch));
1118 view->search_started = 0;
1120 if (view->nlines < 1)
1123 if (view_is_hsplit_top(view))
1126 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1127 wclrtoeol(v->window);
1129 nodelay(view->window, FALSE); /* block for search term input */
1132 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1133 wrefresh(v->window);
1136 nodelay(view->window, TRUE);
1140 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1141 err = view->search_start(view);
1143 regfree(&view->regex);
1146 view->search_started = 1;
1147 view->searching = TOG_SEARCH_FORWARD;
1148 view->search_next_done = 0;
1149 view->search_next(view);
1155 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1156 static const struct got_error *
1157 switch_split(struct tog_view *view)
1159 const struct got_error *err = NULL;
1160 struct tog_view *v = NULL;
1167 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1168 v->mode = TOG_VIEW_SPLIT_VERT;
1170 v->mode = TOG_VIEW_SPLIT_HRZN;
1174 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1175 v->mode = TOG_VIEW_SPLIT_NONE;
1177 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1178 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1179 v->child->begin_y = v->child->resized_y;
1180 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1181 v->child->begin_x = v->child->resized_x;
1184 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1186 v->child->ncols = COLS;
1187 v->child->nscrolled = LINES - v->child->nlines;
1189 err = view_init_hsplit(v, v->child->begin_y);
1193 v->child->mode = v->mode;
1194 v->child->nlines = v->lines - v->child->begin_y;
1197 err = view_fullscreen(v);
1200 err = view_splitscreen(v->child);
1204 if (v->mode == TOG_VIEW_SPLIT_NONE)
1205 v->mode = TOG_VIEW_SPLIT_VERT;
1206 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1207 err = offset_selection_down(v);
1208 err = offset_selection_down(v->child);
1210 offset_selection_up(v);
1211 offset_selection_up(v->child);
1213 if (v->type == TOG_VIEW_LOG && v->nscrolled)
1214 err = request_log_commits(v);
1215 else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1216 err = request_log_commits(v->child);
1222 * Compute view->count from numeric input. Assign total to view->count and
1223 * return first non-numeric key entered.
1226 get_compound_key(struct tog_view *view, int c)
1228 struct tog_view *v = view;
1231 if (view_is_hsplit_top(view))
1233 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1237 cbreak(); /* block for input */
1238 wmove(v->window, v->nlines - 1, 0);
1239 wclrtoeol(v->window);
1240 waddch(v->window, ':');
1243 x = getcurx(v->window);
1244 if (x != ERR && x < view->ncols) {
1245 waddch(v->window, c);
1246 wrefresh(v->window);
1250 * Don't overflow. Max valid request should be the greatest
1251 * between the longest and total lines; cap at 10 million.
1256 n = n * 10 + (c - '0');
1257 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1259 /* Massage excessive or inapplicable values at the input handler. */
1265 static const struct got_error *
1266 view_input(struct tog_view **new, int *done, struct tog_view *view,
1267 struct tog_view_list_head *views)
1269 const struct got_error *err = NULL;
1275 /* Clear "no matches" indicator. */
1276 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1277 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1278 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1282 if (view->searching && !view->search_next_done) {
1283 errcode = pthread_mutex_unlock(&tog_mutex);
1285 return got_error_set_errno(errcode,
1286 "pthread_mutex_unlock");
1288 errcode = pthread_mutex_lock(&tog_mutex);
1290 return got_error_set_errno(errcode,
1291 "pthread_mutex_lock");
1292 view->search_next(view);
1296 nodelay(view->window, FALSE);
1297 /* Allow threads to make progress while we are waiting for input. */
1298 errcode = pthread_mutex_unlock(&tog_mutex);
1300 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1301 /* If we have an unfinished count, let C-g or backspace abort. */
1302 if (view->count && --view->count) {
1304 nodelay(view->window, TRUE);
1305 ch = wgetch(view->window);
1306 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1311 ch = wgetch(view->window);
1312 if (ch >= '1' && ch <= '9')
1313 view->ch = ch = get_compound_key(view, ch);
1315 errcode = pthread_mutex_lock(&tog_mutex);
1317 return got_error_set_errno(errcode, "pthread_mutex_lock");
1318 nodelay(view->window, TRUE);
1320 if (tog_sigwinch_received || tog_sigcont_received) {
1322 tog_sigwinch_received = 0;
1323 tog_sigcont_received = 0;
1324 TAILQ_FOREACH(v, views, entry) {
1325 err = view_resize(v);
1328 err = v->input(new, v, KEY_RESIZE);
1332 err = view_resize(v->child);
1335 err = v->child->input(new, v->child,
1339 if (v->child->resized_x || v->child->resized_y) {
1340 err = view_resize_split(v, 0);
1353 view->child->focussed = 1;
1354 view->focus_child = 1;
1355 } else if (view->parent) {
1357 view->parent->focussed = 1;
1358 view->parent->focus_child = 0;
1359 if (!view_is_splitscreen(view)) {
1360 if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1361 view->parent->type == TOG_VIEW_LOG) {
1362 err = request_log_commits(view->parent);
1366 offset_selection_up(view->parent);
1367 err = view_fullscreen(view->parent);
1374 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1375 if (view->parent->type == TOG_VIEW_LOG) {
1376 /* might need more commits to fill fullscreen */
1377 err = request_log_commits(view->parent);
1381 offset_selection_up(view->parent);
1383 err = view->input(new, view, ch);
1391 if (view_is_parent_view(view)) {
1392 if (view->child == NULL)
1394 if (view_is_splitscreen(view->child)) {
1396 view->child->focussed = 1;
1397 err = view_fullscreen(view->child);
1399 err = view_splitscreen(view->child);
1401 err = view_resize_split(view, 0);
1405 err = view->child->input(new, view->child,
1408 if (view_is_splitscreen(view)) {
1409 view->parent->focussed = 0;
1411 err = view_fullscreen(view);
1413 err = view_splitscreen(view);
1414 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1415 err = view_resize(view->parent);
1417 err = view_resize_split(view, 0);
1421 err = view->input(new, view, KEY_RESIZE);
1425 if (view->type == TOG_VIEW_LOG) {
1426 err = request_log_commits(view);
1431 err = offset_selection_down(view->parent);
1433 err = offset_selection_down(view);
1437 err = switch_split(view);
1440 err = view_resize_split(view, -1);
1443 err = view_resize_split(view, 1);
1449 if (view->search_start)
1450 view_search_start(view);
1452 err = view->input(new, view, ch);
1456 if (view->search_started && view->search_next) {
1457 view->searching = (ch == 'n' ?
1458 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1459 view->search_next_done = 0;
1460 view->search_next(view);
1462 err = view->input(new, view, ch);
1465 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1466 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1468 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1469 TAILQ_FOREACH(v, views, entry) {
1475 if (v->child && v->child->reset) {
1476 err = v->child->reset(v->child);
1483 err = view->input(new, view, ch);
1491 view_needs_focus_indication(struct tog_view *view)
1493 if (view_is_parent_view(view)) {
1494 if (view->child == NULL || view->child->focussed)
1496 if (!view_is_splitscreen(view->child))
1498 } else if (!view_is_splitscreen(view))
1501 return view->focussed;
1504 static const struct got_error *
1505 view_loop(struct tog_view *view)
1507 const struct got_error *err = NULL;
1508 struct tog_view_list_head views;
1509 struct tog_view *new_view;
1511 int fast_refresh = 10;
1512 int done = 0, errcode;
1514 mode = getenv("TOG_VIEW_SPLIT_MODE");
1515 if (!mode || !(*mode == 'h' || *mode == 'H'))
1516 view->mode = TOG_VIEW_SPLIT_VERT;
1518 view->mode = TOG_VIEW_SPLIT_HRZN;
1520 errcode = pthread_mutex_lock(&tog_mutex);
1522 return got_error_set_errno(errcode, "pthread_mutex_lock");
1525 TAILQ_INSERT_HEAD(&views, view, entry);
1528 err = view->show(view);
1533 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1534 !tog_fatal_signal_received()) {
1535 /* Refresh fast during initialization, then become slower. */
1536 if (fast_refresh && fast_refresh-- == 0)
1537 halfdelay(10); /* switch to once per second */
1539 err = view_input(&new_view, &done, view, &views);
1543 struct tog_view *v, *prev = NULL;
1545 if (view_is_parent_view(view))
1546 prev = TAILQ_PREV(view, tog_view_list_head,
1548 else if (view->parent)
1549 prev = view->parent;
1552 view->parent->child = NULL;
1553 view->parent->focus_child = 0;
1554 /* Restore fullscreen line height. */
1555 view->parent->nlines = view->parent->lines;
1556 err = view_resize(view->parent);
1559 /* Make resized splits persist. */
1560 view_transfer_size(view->parent, view);
1562 TAILQ_REMOVE(&views, view, entry);
1564 err = view_close(view);
1569 TAILQ_FOREACH(v, &views, entry) {
1573 if (view == NULL && new_view == NULL) {
1574 /* No view has focus. Try to pick one. */
1577 else if (!TAILQ_EMPTY(&views)) {
1578 view = TAILQ_LAST(&views,
1579 tog_view_list_head);
1582 if (view->focus_child) {
1583 view->child->focussed = 1;
1591 struct tog_view *v, *t;
1592 /* Only allow one parent view per type. */
1593 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1594 if (v->type != new_view->type)
1596 TAILQ_REMOVE(&views, v, entry);
1597 err = view_close(v);
1602 TAILQ_INSERT_TAIL(&views, new_view, entry);
1606 if (view_is_parent_view(view)) {
1607 if (view->child && view->child->focussed)
1610 if (view->parent && view->parent->focussed)
1611 view = view->parent;
1613 show_panel(view->panel);
1614 if (view->child && view_is_splitscreen(view->child))
1615 show_panel(view->child->panel);
1616 if (view->parent && view_is_splitscreen(view)) {
1617 err = view->parent->show(view->parent);
1621 err = view->show(view);
1625 err = view->child->show(view->child);
1634 while (!TAILQ_EMPTY(&views)) {
1635 const struct got_error *close_err;
1636 view = TAILQ_FIRST(&views);
1637 TAILQ_REMOVE(&views, view, entry);
1638 close_err = view_close(view);
1639 if (close_err && err == NULL)
1643 errcode = pthread_mutex_unlock(&tog_mutex);
1644 if (errcode && err == NULL)
1645 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1655 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1660 /* Create newly allocated wide-character string equivalent to a byte string. */
1661 static const struct got_error *
1662 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1665 const struct got_error *err = NULL;
1668 *wlen = mbstowcs(NULL, s, 0);
1669 if (*wlen == (size_t)-1) {
1671 if (errno != EILSEQ)
1672 return got_error_from_errno("mbstowcs");
1674 /* byte string invalid in current encoding; try to "fix" it */
1675 err = got_mbsavis(&vis, &vislen, s);
1678 *wlen = mbstowcs(NULL, vis, 0);
1679 if (*wlen == (size_t)-1) {
1680 err = got_error_from_errno("mbstowcs"); /* give up */
1685 *ws = calloc(*wlen + 1, sizeof(**ws));
1687 err = got_error_from_errno("calloc");
1691 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1692 err = got_error_from_errno("mbstowcs");
1703 static const struct got_error *
1704 expand_tab(char **ptr, const char *src)
1707 size_t len, n, idx = 0, sz = 0;
1710 n = len = strlen(src);
1711 dst = malloc(n + 1);
1713 return got_error_from_errno("malloc");
1715 while (idx < len && src[idx]) {
1716 const char c = src[idx];
1719 size_t nb = TABSIZE - sz % TABSIZE;
1722 p = realloc(dst, n + nb);
1725 return got_error_from_errno("realloc");
1730 memset(dst + sz, ' ', nb);
1733 dst[sz++] = src[idx];
1743 * Advance at most n columns from wline starting at offset off.
1744 * Return the index to the first character after the span operation.
1745 * Return the combined column width of all spanned wide character in
1749 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1751 int width, i, cols = 0;
1758 for (i = off; wline[i] != L'\0'; ++i) {
1759 if (wline[i] == L'\t')
1760 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1762 width = wcwidth(wline[i]);
1769 if (cols + width > n)
1779 * Format a line for display, ensuring that it won't overflow a width limit.
1780 * With scrolling, the width returned refers to the scrolled version of the
1781 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1783 static const struct got_error *
1784 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1785 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1787 const struct got_error *err = NULL;
1789 wchar_t *wline = NULL;
1798 err = expand_tab(&exstr, line);
1803 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1808 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1810 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1811 wline[wlen - 1] = L'\0';
1814 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1815 wline[wlen - 1] = L'\0';
1819 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1825 *scrollxp = scrollx;
1833 static const struct got_error*
1834 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1835 struct got_object_id *id, struct got_repository *repo)
1837 static const struct got_error *err = NULL;
1838 struct got_reflist_entry *re;
1844 TAILQ_FOREACH(re, refs, entry) {
1845 struct got_tag_object *tag = NULL;
1846 struct got_object_id *ref_id;
1849 name = got_ref_get_name(re->ref);
1850 if (strcmp(name, GOT_REF_HEAD) == 0)
1852 if (strncmp(name, "refs/", 5) == 0)
1854 if (strncmp(name, "got/", 4) == 0 &&
1855 strncmp(name, "got/backup/", 11) != 0)
1857 if (strncmp(name, "heads/", 6) == 0)
1859 if (strncmp(name, "remotes/", 8) == 0) {
1861 s = strstr(name, "/" GOT_REF_HEAD);
1862 if (s != NULL && s[strlen(s)] == '\0')
1865 err = got_ref_resolve(&ref_id, repo, re->ref);
1868 if (strncmp(name, "tags/", 5) == 0) {
1869 err = got_object_open_as_tag(&tag, repo, ref_id);
1871 if (err->code != GOT_ERR_OBJ_TYPE) {
1875 /* Ref points at something other than a tag. */
1880 cmp = got_object_id_cmp(tag ?
1881 got_object_tag_get_object_id(tag) : ref_id, id);
1884 got_object_tag_close(tag);
1888 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1889 s ? ", " : "", name) == -1) {
1890 err = got_error_from_errno("asprintf");
1901 static const struct got_error *
1902 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1907 smallerthan = strchr(author, '<');
1908 if (smallerthan && smallerthan[1] != '\0')
1909 author = smallerthan + 1;
1910 author[strcspn(author, "@>")] = '\0';
1911 return format_line(wauthor, author_width, NULL, author, 0, limit,
1915 static const struct got_error *
1916 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1917 struct got_object_id *id, const size_t date_display_cols,
1918 int author_display_cols)
1920 struct tog_log_view_state *s = &view->state.log;
1921 const struct got_error *err = NULL;
1922 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1923 char *logmsg0 = NULL, *logmsg = NULL;
1924 char *author = NULL;
1925 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1926 int author_width, logmsg_width;
1927 char *newline, *line = NULL;
1928 int col, limit, scrollx;
1929 const int avail = view->ncols;
1931 time_t committer_time;
1932 struct tog_color *tc;
1934 committer_time = got_object_commit_get_committer_time(commit);
1935 if (gmtime_r(&committer_time, &tm) == NULL)
1936 return got_error_from_errno("gmtime_r");
1937 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1938 return got_error(GOT_ERR_NO_SPACE);
1940 if (avail <= date_display_cols)
1941 limit = MIN(sizeof(datebuf) - 1, avail);
1943 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1944 tc = get_color(&s->colors, TOG_COLOR_DATE);
1946 wattr_on(view->window,
1947 COLOR_PAIR(tc->colorpair), NULL);
1948 waddnstr(view->window, datebuf, limit);
1950 wattr_off(view->window,
1951 COLOR_PAIR(tc->colorpair), NULL);
1958 err = got_object_id_str(&id_str, id);
1961 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1963 wattr_on(view->window,
1964 COLOR_PAIR(tc->colorpair), NULL);
1965 wprintw(view->window, "%.8s ", id_str);
1967 wattr_off(view->window,
1968 COLOR_PAIR(tc->colorpair), NULL);
1975 if (s->use_committer)
1976 author = strdup(got_object_commit_get_committer(commit));
1978 author = strdup(got_object_commit_get_author(commit));
1979 if (author == NULL) {
1980 err = got_error_from_errno("strdup");
1983 err = format_author(&wauthor, &author_width, author, avail - col, col);
1986 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1988 wattr_on(view->window,
1989 COLOR_PAIR(tc->colorpair), NULL);
1990 waddwstr(view->window, wauthor);
1992 wattr_off(view->window,
1993 COLOR_PAIR(tc->colorpair), NULL);
1994 col += author_width;
1995 while (col < avail && author_width < author_display_cols + 2) {
1996 waddch(view->window, ' ');
2003 err = got_object_commit_get_logmsg(&logmsg0, commit);
2007 while (*logmsg == '\n')
2009 newline = strchr(logmsg, '\n');
2012 limit = avail - col;
2013 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2014 limit--; /* for the border */
2015 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2019 waddwstr(view->window, &wlogmsg[scrollx]);
2020 col += MAX(logmsg_width, 0);
2021 while (col < avail) {
2022 waddch(view->window, ' ');
2034 static struct commit_queue_entry *
2035 alloc_commit_queue_entry(struct got_commit_object *commit,
2036 struct got_object_id *id)
2038 struct commit_queue_entry *entry;
2040 entry = calloc(1, sizeof(*entry));
2045 entry->commit = commit;
2050 pop_commit(struct commit_queue *commits)
2052 struct commit_queue_entry *entry;
2054 entry = TAILQ_FIRST(&commits->head);
2055 TAILQ_REMOVE(&commits->head, entry, entry);
2056 got_object_commit_close(entry->commit);
2057 commits->ncommits--;
2058 /* Don't free entry->id! It is owned by the commit graph. */
2063 free_commits(struct commit_queue *commits)
2065 while (!TAILQ_EMPTY(&commits->head))
2066 pop_commit(commits);
2069 static const struct got_error *
2070 match_commit(int *have_match, struct got_object_id *id,
2071 struct got_commit_object *commit, regex_t *regex)
2073 const struct got_error *err = NULL;
2074 regmatch_t regmatch;
2075 char *id_str = NULL, *logmsg = NULL;
2079 err = got_object_id_str(&id_str, id);
2083 err = got_object_commit_get_logmsg(&logmsg, commit);
2087 if (regexec(regex, got_object_commit_get_author(commit), 1,
2088 ®match, 0) == 0 ||
2089 regexec(regex, got_object_commit_get_committer(commit), 1,
2090 ®match, 0) == 0 ||
2091 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2092 regexec(regex, logmsg, 1, ®match, 0) == 0)
2100 static const struct got_error *
2101 queue_commits(struct tog_log_thread_args *a)
2103 const struct got_error *err = NULL;
2106 * We keep all commits open throughout the lifetime of the log
2107 * view in order to avoid having to re-fetch commits from disk
2108 * while updating the display.
2111 struct got_object_id *id;
2112 struct got_commit_object *commit;
2113 struct commit_queue_entry *entry;
2116 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2118 if (err || id == NULL)
2121 err = got_object_open_as_commit(&commit, a->repo, id);
2124 entry = alloc_commit_queue_entry(commit, id);
2125 if (entry == NULL) {
2126 err = got_error_from_errno("alloc_commit_queue_entry");
2130 errcode = pthread_mutex_lock(&tog_mutex);
2132 err = got_error_set_errno(errcode,
2133 "pthread_mutex_lock");
2137 entry->idx = a->commits->ncommits;
2138 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2139 a->commits->ncommits++;
2141 if (*a->searching == TOG_SEARCH_FORWARD &&
2142 !*a->search_next_done) {
2144 err = match_commit(&have_match, id, commit, a->regex);
2148 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2151 errcode = pthread_mutex_unlock(&tog_mutex);
2152 if (errcode && err == NULL)
2153 err = got_error_set_errno(errcode,
2154 "pthread_mutex_unlock");
2157 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2163 select_commit(struct tog_log_view_state *s)
2165 struct commit_queue_entry *entry;
2168 entry = s->first_displayed_entry;
2170 if (ncommits == s->selected) {
2171 s->selected_entry = entry;
2174 entry = TAILQ_NEXT(entry, entry);
2179 static const struct got_error *
2180 draw_commits(struct tog_view *view)
2182 const struct got_error *err = NULL;
2183 struct tog_log_view_state *s = &view->state.log;
2184 struct commit_queue_entry *entry = s->selected_entry;
2185 const int limit = view->nlines;
2187 int ncommits, author_cols = 4;
2188 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2189 char *refs_str = NULL;
2191 struct tog_color *tc;
2192 static const size_t date_display_cols = 12;
2194 if (s->selected_entry &&
2195 !(view->searching && view->search_next_done == 0)) {
2196 struct got_reflist_head *refs;
2197 err = got_object_id_str(&id_str, s->selected_entry->id);
2200 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2201 s->selected_entry->id);
2203 err = build_refs_str(&refs_str, refs,
2204 s->selected_entry->id, s->repo);
2210 if (s->thread_args.commits_needed == 0)
2211 halfdelay(10); /* disable fast refresh */
2213 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2214 if (asprintf(&ncommits_str, " [%d/%d] %s",
2215 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2216 (view->searching && !view->search_next_done) ?
2217 "searching..." : "loading...") == -1) {
2218 err = got_error_from_errno("asprintf");
2222 const char *search_str = NULL;
2224 if (view->searching) {
2225 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2226 search_str = "no more matches";
2227 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2228 search_str = "no matches found";
2229 else if (!view->search_next_done)
2230 search_str = "searching...";
2233 if (asprintf(&ncommits_str, " [%d/%d] %s",
2234 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2235 search_str ? search_str :
2236 (refs_str ? refs_str : "")) == -1) {
2237 err = got_error_from_errno("asprintf");
2242 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2243 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2244 "........................................",
2245 s->in_repo_path, ncommits_str) == -1) {
2246 err = got_error_from_errno("asprintf");
2250 } else if (asprintf(&header, "commit %s%s",
2251 id_str ? id_str : "........................................",
2252 ncommits_str) == -1) {
2253 err = got_error_from_errno("asprintf");
2257 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2261 werase(view->window);
2263 if (view_needs_focus_indication(view))
2264 wstandout(view->window);
2265 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2267 wattr_on(view->window,
2268 COLOR_PAIR(tc->colorpair), NULL);
2269 waddwstr(view->window, wline);
2271 wattr_off(view->window,
2272 COLOR_PAIR(tc->colorpair), NULL);
2273 while (width < view->ncols) {
2274 waddch(view->window, ' ');
2277 if (view_needs_focus_indication(view))
2278 wstandend(view->window);
2283 /* Grow author column size if necessary, and set view->maxx. */
2284 entry = s->first_displayed_entry;
2288 struct got_commit_object *c = entry->commit;
2289 char *author, *eol, *msg, *msg0;
2290 wchar_t *wauthor, *wmsg;
2292 if (ncommits >= limit - 1)
2294 if (s->use_committer)
2295 author = strdup(got_object_commit_get_committer(c));
2297 author = strdup(got_object_commit_get_author(c));
2298 if (author == NULL) {
2299 err = got_error_from_errno("strdup");
2302 err = format_author(&wauthor, &width, author, COLS,
2304 if (author_cols < width)
2305 author_cols = width;
2308 err = got_object_commit_get_logmsg(&msg0, c);
2312 while (*msg == '\n')
2314 if ((eol = strchr(msg, '\n')))
2316 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2317 date_display_cols + author_cols, 0);
2320 view->maxx = MAX(view->maxx, width);
2324 entry = TAILQ_NEXT(entry, entry);
2327 entry = s->first_displayed_entry;
2328 s->last_displayed_entry = s->first_displayed_entry;
2331 if (ncommits >= limit - 1)
2333 if (ncommits == s->selected)
2334 wstandout(view->window);
2335 err = draw_commit(view, entry->commit, entry->id,
2336 date_display_cols, author_cols);
2337 if (ncommits == s->selected)
2338 wstandend(view->window);
2342 s->last_displayed_entry = entry;
2343 entry = TAILQ_NEXT(entry, entry);
2356 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2358 struct commit_queue_entry *entry;
2361 entry = TAILQ_FIRST(&s->commits.head);
2362 if (s->first_displayed_entry == entry)
2365 entry = s->first_displayed_entry;
2366 while (entry && nscrolled < maxscroll) {
2367 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2369 s->first_displayed_entry = entry;
2375 static const struct got_error *
2376 trigger_log_thread(struct tog_view *view, int wait)
2378 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2381 halfdelay(1); /* fast refresh while loading commits */
2383 while (!ta->log_complete && !tog_thread_error &&
2384 (ta->commits_needed > 0 || ta->load_all)) {
2385 /* Wake the log thread. */
2386 errcode = pthread_cond_signal(&ta->need_commits);
2388 return got_error_set_errno(errcode,
2389 "pthread_cond_signal");
2392 * The mutex will be released while the view loop waits
2393 * in wgetch(), at which time the log thread will run.
2398 /* Display progress update in log view. */
2399 show_log_view(view);
2403 /* Wait right here while next commit is being loaded. */
2404 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2406 return got_error_set_errno(errcode,
2407 "pthread_cond_wait");
2409 /* Display progress update in log view. */
2410 show_log_view(view);
2418 static const struct got_error *
2419 request_log_commits(struct tog_view *view)
2421 struct tog_log_view_state *state = &view->state.log;
2422 const struct got_error *err = NULL;
2424 if (state->thread_args.log_complete)
2427 state->thread_args.commits_needed += view->nscrolled;
2428 err = trigger_log_thread(view, 1);
2429 view->nscrolled = 0;
2434 static const struct got_error *
2435 log_scroll_down(struct tog_view *view, int maxscroll)
2437 struct tog_log_view_state *s = &view->state.log;
2438 const struct got_error *err = NULL;
2439 struct commit_queue_entry *pentry;
2440 int nscrolled = 0, ncommits_needed;
2442 if (s->last_displayed_entry == NULL)
2445 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2446 if (s->commits.ncommits < ncommits_needed &&
2447 !s->thread_args.log_complete) {
2449 * Ask the log thread for required amount of commits.
2451 s->thread_args.commits_needed += maxscroll;
2452 err = trigger_log_thread(view, 1);
2458 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2459 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2462 s->last_displayed_entry = pentry ?
2463 pentry : s->last_displayed_entry;;
2465 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2468 s->first_displayed_entry = pentry;
2469 } while (++nscrolled < maxscroll);
2471 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2472 view->nscrolled += nscrolled;
2474 view->nscrolled = 0;
2479 static const struct got_error *
2480 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2481 struct got_commit_object *commit, struct got_object_id *commit_id,
2482 struct tog_view *log_view, struct got_repository *repo)
2484 const struct got_error *err;
2485 struct got_object_qid *parent_id;
2486 struct tog_view *diff_view;
2488 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2489 if (diff_view == NULL)
2490 return got_error_from_errno("view_open");
2492 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2493 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2494 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2496 *new_view = diff_view;
2500 static const struct got_error *
2501 tree_view_visit_subtree(struct tog_tree_view_state *s,
2502 struct got_tree_object *subtree)
2504 struct tog_parent_tree *parent;
2506 parent = calloc(1, sizeof(*parent));
2508 return got_error_from_errno("calloc");
2510 parent->tree = s->tree;
2511 parent->first_displayed_entry = s->first_displayed_entry;
2512 parent->selected_entry = s->selected_entry;
2513 parent->selected = s->selected;
2514 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2517 s->first_displayed_entry = NULL;
2521 static const struct got_error *
2522 tree_view_walk_path(struct tog_tree_view_state *s,
2523 struct got_commit_object *commit, const char *path)
2525 const struct got_error *err = NULL;
2526 struct got_tree_object *tree = NULL;
2528 char *slash, *subpath = NULL;
2530 /* Walk the path and open corresponding tree objects. */
2533 struct got_tree_entry *te;
2534 struct got_object_id *tree_id;
2540 /* Ensure the correct subtree entry is selected. */
2541 slash = strchr(p, '/');
2543 te_name = strdup(p);
2545 te_name = strndup(p, slash - p);
2546 if (te_name == NULL) {
2547 err = got_error_from_errno("strndup");
2550 te = got_object_tree_find_entry(s->tree, te_name);
2552 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2557 s->first_displayed_entry = s->selected_entry = te;
2559 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2560 break; /* jump to this file's entry */
2562 slash = strchr(p, '/');
2564 subpath = strndup(path, slash - path);
2566 subpath = strdup(path);
2567 if (subpath == NULL) {
2568 err = got_error_from_errno("strdup");
2572 err = got_object_id_by_path(&tree_id, s->repo, commit,
2577 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2582 err = tree_view_visit_subtree(s, tree);
2584 got_object_tree_close(tree);
2598 static const struct got_error *
2599 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2600 struct commit_queue_entry *entry, const char *path,
2601 const char *head_ref_name, struct got_repository *repo)
2603 const struct got_error *err = NULL;
2604 struct tog_tree_view_state *s;
2605 struct tog_view *tree_view;
2607 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2608 if (tree_view == NULL)
2609 return got_error_from_errno("view_open");
2611 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2614 s = &tree_view->state.tree;
2616 *new_view = tree_view;
2618 if (got_path_is_root_dir(path))
2621 return tree_view_walk_path(s, entry->commit, path);
2624 static const struct got_error *
2625 block_signals_used_by_main_thread(void)
2630 if (sigemptyset(&sigset) == -1)
2631 return got_error_from_errno("sigemptyset");
2633 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2634 if (sigaddset(&sigset, SIGWINCH) == -1)
2635 return got_error_from_errno("sigaddset");
2636 if (sigaddset(&sigset, SIGCONT) == -1)
2637 return got_error_from_errno("sigaddset");
2638 if (sigaddset(&sigset, SIGINT) == -1)
2639 return got_error_from_errno("sigaddset");
2640 if (sigaddset(&sigset, SIGTERM) == -1)
2641 return got_error_from_errno("sigaddset");
2643 /* ncurses handles SIGTSTP */
2644 if (sigaddset(&sigset, SIGTSTP) == -1)
2645 return got_error_from_errno("sigaddset");
2647 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2649 return got_error_set_errno(errcode, "pthread_sigmask");
2655 log_thread(void *arg)
2657 const struct got_error *err = NULL;
2659 struct tog_log_thread_args *a = arg;
2663 * Sync startup with main thread such that we begin our
2664 * work once view_input() has released the mutex.
2666 errcode = pthread_mutex_lock(&tog_mutex);
2668 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2672 err = block_signals_used_by_main_thread();
2674 pthread_mutex_unlock(&tog_mutex);
2678 while (!done && !err && !tog_fatal_signal_received()) {
2679 errcode = pthread_mutex_unlock(&tog_mutex);
2681 err = got_error_set_errno(errcode,
2682 "pthread_mutex_unlock");
2685 err = queue_commits(a);
2687 if (err->code != GOT_ERR_ITER_COMPLETED)
2691 } else if (a->commits_needed > 0 && !a->load_all)
2692 a->commits_needed--;
2694 errcode = pthread_mutex_lock(&tog_mutex);
2696 err = got_error_set_errno(errcode,
2697 "pthread_mutex_lock");
2699 } else if (*a->quit)
2701 else if (*a->first_displayed_entry == NULL) {
2702 *a->first_displayed_entry =
2703 TAILQ_FIRST(&a->commits->head);
2704 *a->selected_entry = *a->first_displayed_entry;
2707 errcode = pthread_cond_signal(&a->commit_loaded);
2709 err = got_error_set_errno(errcode,
2710 "pthread_cond_signal");
2711 pthread_mutex_unlock(&tog_mutex);
2716 a->commits_needed = 0;
2718 if (a->commits_needed == 0 && !a->load_all) {
2719 errcode = pthread_cond_wait(&a->need_commits,
2722 err = got_error_set_errno(errcode,
2723 "pthread_cond_wait");
2724 pthread_mutex_unlock(&tog_mutex);
2732 a->log_complete = 1;
2733 errcode = pthread_mutex_unlock(&tog_mutex);
2735 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2738 tog_thread_error = 1;
2739 pthread_cond_signal(&a->commit_loaded);
2744 static const struct got_error *
2745 stop_log_thread(struct tog_log_view_state *s)
2747 const struct got_error *err = NULL, *thread_err = NULL;
2752 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2754 return got_error_set_errno(errcode,
2755 "pthread_cond_signal");
2756 errcode = pthread_mutex_unlock(&tog_mutex);
2758 return got_error_set_errno(errcode,
2759 "pthread_mutex_unlock");
2760 errcode = pthread_join(s->thread, (void **)&thread_err);
2762 return got_error_set_errno(errcode, "pthread_join");
2763 errcode = pthread_mutex_lock(&tog_mutex);
2765 return got_error_set_errno(errcode,
2766 "pthread_mutex_lock");
2770 if (s->thread_args.repo) {
2771 err = got_repo_close(s->thread_args.repo);
2772 s->thread_args.repo = NULL;
2775 if (s->thread_args.pack_fds) {
2776 const struct got_error *pack_err =
2777 got_repo_pack_fds_close(s->thread_args.pack_fds);
2780 s->thread_args.pack_fds = NULL;
2783 if (s->thread_args.graph) {
2784 got_commit_graph_close(s->thread_args.graph);
2785 s->thread_args.graph = NULL;
2788 return err ? err : thread_err;
2791 static const struct got_error *
2792 close_log_view(struct tog_view *view)
2794 const struct got_error *err = NULL;
2795 struct tog_log_view_state *s = &view->state.log;
2798 err = stop_log_thread(s);
2800 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2801 if (errcode && err == NULL)
2802 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2804 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2805 if (errcode && err == NULL)
2806 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2808 free_commits(&s->commits);
2809 free(s->in_repo_path);
2810 s->in_repo_path = NULL;
2813 free(s->head_ref_name);
2814 s->head_ref_name = NULL;
2818 static const struct got_error *
2819 search_start_log_view(struct tog_view *view)
2821 struct tog_log_view_state *s = &view->state.log;
2823 s->matched_entry = NULL;
2824 s->search_entry = NULL;
2828 static const struct got_error *
2829 search_next_log_view(struct tog_view *view)
2831 const struct got_error *err = NULL;
2832 struct tog_log_view_state *s = &view->state.log;
2833 struct commit_queue_entry *entry;
2835 /* Display progress update in log view. */
2836 show_log_view(view);
2840 if (s->search_entry) {
2842 errcode = pthread_mutex_unlock(&tog_mutex);
2844 return got_error_set_errno(errcode,
2845 "pthread_mutex_unlock");
2846 ch = wgetch(view->window);
2847 errcode = pthread_mutex_lock(&tog_mutex);
2849 return got_error_set_errno(errcode,
2850 "pthread_mutex_lock");
2851 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2852 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2855 if (view->searching == TOG_SEARCH_FORWARD)
2856 entry = TAILQ_NEXT(s->search_entry, entry);
2858 entry = TAILQ_PREV(s->search_entry,
2859 commit_queue_head, entry);
2860 } else if (s->matched_entry) {
2861 int matched_idx = s->matched_entry->idx;
2862 int selected_idx = s->selected_entry->idx;
2865 * If the user has moved the cursor after we hit a match,
2866 * the position from where we should continue searching
2867 * might have changed.
2869 if (view->searching == TOG_SEARCH_FORWARD) {
2870 if (matched_idx > selected_idx)
2871 entry = TAILQ_NEXT(s->selected_entry, entry);
2873 entry = TAILQ_NEXT(s->matched_entry, entry);
2875 if (matched_idx < selected_idx)
2876 entry = TAILQ_PREV(s->selected_entry,
2877 commit_queue_head, entry);
2879 entry = TAILQ_PREV(s->matched_entry,
2880 commit_queue_head, entry);
2883 entry = s->selected_entry;
2889 if (entry == NULL) {
2890 if (s->thread_args.log_complete ||
2891 view->searching == TOG_SEARCH_BACKWARD) {
2892 view->search_next_done =
2893 (s->matched_entry == NULL ?
2894 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2895 s->search_entry = NULL;
2899 * Poke the log thread for more commits and return,
2900 * allowing the main loop to make progress. Search
2901 * will resume at s->search_entry once we come back.
2903 s->thread_args.commits_needed++;
2904 return trigger_log_thread(view, 0);
2907 err = match_commit(&have_match, entry->id, entry->commit,
2912 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2913 s->matched_entry = entry;
2917 s->search_entry = entry;
2918 if (view->searching == TOG_SEARCH_FORWARD)
2919 entry = TAILQ_NEXT(entry, entry);
2921 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2924 if (s->matched_entry) {
2925 int cur = s->selected_entry->idx;
2926 while (cur < s->matched_entry->idx) {
2927 err = input_log_view(NULL, view, KEY_DOWN);
2932 while (cur > s->matched_entry->idx) {
2933 err = input_log_view(NULL, view, KEY_UP);
2940 s->search_entry = NULL;
2945 static const struct got_error *
2946 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2947 struct got_repository *repo, const char *head_ref_name,
2948 const char *in_repo_path, int log_branches)
2950 const struct got_error *err = NULL;
2951 struct tog_log_view_state *s = &view->state.log;
2952 struct got_repository *thread_repo = NULL;
2953 struct got_commit_graph *thread_graph = NULL;
2956 if (in_repo_path != s->in_repo_path) {
2957 free(s->in_repo_path);
2958 s->in_repo_path = strdup(in_repo_path);
2959 if (s->in_repo_path == NULL)
2960 return got_error_from_errno("strdup");
2963 /* The commit queue only contains commits being displayed. */
2964 TAILQ_INIT(&s->commits.head);
2965 s->commits.ncommits = 0;
2968 if (head_ref_name) {
2969 s->head_ref_name = strdup(head_ref_name);
2970 if (s->head_ref_name == NULL) {
2971 err = got_error_from_errno("strdup");
2975 s->start_id = got_object_id_dup(start_id);
2976 if (s->start_id == NULL) {
2977 err = got_error_from_errno("got_object_id_dup");
2980 s->log_branches = log_branches;
2982 STAILQ_INIT(&s->colors);
2983 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2984 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2985 get_color_value("TOG_COLOR_COMMIT"));
2988 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2989 get_color_value("TOG_COLOR_AUTHOR"));
2991 free_colors(&s->colors);
2994 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2995 get_color_value("TOG_COLOR_DATE"));
2997 free_colors(&s->colors);
3002 view->show = show_log_view;
3003 view->input = input_log_view;
3004 view->resize = resize_log_view;
3005 view->close = close_log_view;
3006 view->search_start = search_start_log_view;
3007 view->search_next = search_next_log_view;
3009 if (s->thread_args.pack_fds == NULL) {
3010 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3014 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3015 s->thread_args.pack_fds);
3018 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3022 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3023 s->repo, NULL, NULL);
3027 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3029 err = got_error_set_errno(errcode, "pthread_cond_init");
3032 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3034 err = got_error_set_errno(errcode, "pthread_cond_init");
3038 s->thread_args.commits_needed = view->nlines;
3039 s->thread_args.graph = thread_graph;
3040 s->thread_args.commits = &s->commits;
3041 s->thread_args.in_repo_path = s->in_repo_path;
3042 s->thread_args.start_id = s->start_id;
3043 s->thread_args.repo = thread_repo;
3044 s->thread_args.log_complete = 0;
3045 s->thread_args.quit = &s->quit;
3046 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3047 s->thread_args.selected_entry = &s->selected_entry;
3048 s->thread_args.searching = &view->searching;
3049 s->thread_args.search_next_done = &view->search_next_done;
3050 s->thread_args.regex = &view->regex;
3053 close_log_view(view);
3057 static const struct got_error *
3058 show_log_view(struct tog_view *view)
3060 const struct got_error *err;
3061 struct tog_log_view_state *s = &view->state.log;
3063 if (s->thread == NULL) {
3064 int errcode = pthread_create(&s->thread, NULL, log_thread,
3067 return got_error_set_errno(errcode, "pthread_create");
3068 if (s->thread_args.commits_needed > 0) {
3069 err = trigger_log_thread(view, 1);
3075 return draw_commits(view);
3079 log_move_cursor_up(struct tog_view *view, int page, int home)
3081 struct tog_log_view_state *s = &view->state.log;
3083 if (s->selected_entry->idx == 0)
3085 if (s->first_displayed_entry == NULL)
3088 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3090 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3092 if (!page && !home && s->selected > 0)
3095 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3101 static const struct got_error *
3102 log_move_cursor_down(struct tog_view *view, int page)
3104 struct tog_log_view_state *s = &view->state.log;
3105 struct commit_queue_entry *first;
3106 const struct got_error *err = NULL;
3108 first = s->first_displayed_entry;
3109 if (first == NULL) {
3114 if (s->thread_args.log_complete &&
3115 s->selected_entry->idx >= s->commits.ncommits - 1)
3119 int eos = view->nlines - 2;
3121 if (view_is_hsplit_top(view))
3122 --eos; /* border consumes the last line */
3123 if (s->selected < MIN(eos, s->commits.ncommits - 1))
3126 err = log_scroll_down(view, 1);
3127 } else if (s->thread_args.load_all) {
3128 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3129 s->selected += MIN(s->last_displayed_entry->idx -
3130 s->selected_entry->idx, page + 1);
3132 err = log_scroll_down(view, MIN(page,
3133 s->commits.ncommits - s->selected_entry->idx - 1));
3134 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3136 err = log_scroll_down(view, page);
3139 if (first == s->first_displayed_entry && s->selected <
3140 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3141 s->selected = MIN(s->commits.ncommits - 1, page);
3148 * We might necessarily overshoot in horizontal
3149 * splits; if so, select the last displayed commit.
3151 s->selected = MIN(s->selected,
3152 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3156 if (s->thread_args.log_complete &&
3157 s->selected_entry->idx == s->commits.ncommits - 1)
3164 view_get_split(struct tog_view *view, int *y, int *x)
3169 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3170 if (view->child && view->child->resized_y)
3171 *y = view->child->resized_y;
3172 else if (view->resized_y)
3173 *y = view->resized_y;
3175 *y = view_split_begin_y(view->lines);
3176 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3177 if (view->child && view->child->resized_x)
3178 *x = view->child->resized_x;
3179 else if (view->resized_x)
3180 *x = view->resized_x;
3182 *x = view_split_begin_x(view->begin_x);
3186 /* Split view horizontally at y and offset view->state->selected line. */
3187 static const struct got_error *
3188 view_init_hsplit(struct tog_view *view, int y)
3190 const struct got_error *err = NULL;
3194 err = view_resize(view);
3198 err = offset_selection_down(view);
3203 static const struct got_error *
3204 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3206 const struct got_error *err = NULL;
3207 struct tog_log_view_state *s = &view->state.log;
3208 struct tog_view *diff_view = NULL, *tree_view = NULL;
3209 struct tog_view *ref_view = NULL;
3210 struct commit_queue_entry *entry;
3211 int begin_x = 0, begin_y = 0, eos, n, nscroll;
3213 if (s->thread_args.load_all) {
3214 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3215 s->thread_args.load_all = 0;
3216 else if (s->thread_args.log_complete) {
3217 err = log_move_cursor_down(view, s->commits.ncommits);
3218 s->thread_args.load_all = 0;
3223 eos = nscroll = view->nlines - 1;
3224 if (view_is_hsplit_top(view))
3235 view->x = MAX(view->maxx - view->ncols / 2, 0);
3240 if (view->x + view->ncols / 2 < view->maxx)
3241 view->x += 2; /* move two columns right */
3247 view->x -= MIN(view->x, 2); /* move two columns back */
3256 log_move_cursor_up(view, 0, 0);
3260 log_move_cursor_up(view, 0, 1);
3270 log_move_cursor_up(view, nscroll, 0);
3277 err = log_move_cursor_down(view, 0);
3280 s->use_committer = !s->use_committer;
3284 /* We don't know yet how many commits, so we're forced to
3285 * traverse them all. */
3287 if (!s->thread_args.log_complete) {
3288 s->thread_args.load_all = 1;
3289 return trigger_log_thread(view, 0);
3293 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3294 for (n = 0; n < eos; n++) {
3297 s->first_displayed_entry = entry;
3298 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3301 s->selected = n - 1;
3313 err = log_move_cursor_down(view, nscroll);
3316 if (s->selected > view->nlines - 2)
3317 s->selected = view->nlines - 2;
3318 if (s->selected > s->commits.ncommits - 1)
3319 s->selected = s->commits.ncommits - 1;
3321 if (s->commits.ncommits < view->nlines - 1 &&
3322 !s->thread_args.log_complete) {
3323 s->thread_args.commits_needed += (view->nlines - 1) -
3324 s->commits.ncommits;
3325 err = trigger_log_thread(view, 1);
3331 if (s->selected_entry == NULL)
3334 /* get dimensions--don't split till initialisation succeeds */
3335 if (view_is_parent_view(view))
3336 view_get_split(view, &begin_y, &begin_x);
3338 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3339 s->selected_entry->commit, s->selected_entry->id,
3344 if (view_is_parent_view(view) &&
3345 view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3346 err = view_init_hsplit(view, begin_y);
3352 diff_view->focussed = 1;
3353 diff_view->mode = view->mode;
3354 diff_view->nlines = view->lines - begin_y;
3356 if (view_is_parent_view(view)) {
3357 view_transfer_size(diff_view, view);
3358 err = view_close_child(view);
3361 err = view_set_child(view, diff_view);
3364 view->focus_child = 1;
3366 *new_view = diff_view;
3370 if (s->selected_entry == NULL)
3372 if (view_is_parent_view(view))
3373 view_get_split(view, &begin_y, &begin_x);
3374 err = browse_commit_tree(&tree_view, begin_y, begin_x,
3375 s->selected_entry, s->in_repo_path, s->head_ref_name,
3379 if (view_is_parent_view(view) &&
3380 view->mode == TOG_VIEW_SPLIT_HRZN) {
3381 err = view_init_hsplit(view, begin_y);
3386 tree_view->focussed = 1;
3387 tree_view->mode = view->mode;
3388 tree_view->nlines = view->lines - begin_y;
3389 if (view_is_parent_view(view)) {
3390 view_transfer_size(tree_view, view);
3391 err = view_close_child(view);
3394 err = view_set_child(view, tree_view);
3397 view->focus_child = 1;
3399 *new_view = tree_view;
3405 if (ch == KEY_BACKSPACE &&
3406 got_path_is_root_dir(s->in_repo_path))
3408 err = stop_log_thread(s);
3411 if (ch == KEY_BACKSPACE) {
3413 err = got_path_dirname(&parent_path, s->in_repo_path);
3416 free(s->in_repo_path);
3417 s->in_repo_path = parent_path;
3418 s->thread_args.in_repo_path = s->in_repo_path;
3419 } else if (ch == CTRL('l')) {
3420 struct got_object_id *start_id;
3421 err = got_repo_match_object_id(&start_id, NULL,
3422 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3423 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3427 s->start_id = start_id;
3428 s->thread_args.start_id = s->start_id;
3430 s->log_branches = !s->log_branches;
3432 if (s->thread_args.pack_fds == NULL) {
3433 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3437 err = got_repo_open(&s->thread_args.repo,
3438 got_repo_get_path(s->repo), NULL,
3439 s->thread_args.pack_fds);
3443 err = tog_load_refs(s->repo, 0);
3446 err = got_commit_graph_open(&s->thread_args.graph,
3447 s->in_repo_path, !s->log_branches);
3450 err = got_commit_graph_iter_start(s->thread_args.graph,
3451 s->start_id, s->repo, NULL, NULL);
3454 free_commits(&s->commits);
3455 s->first_displayed_entry = NULL;
3456 s->last_displayed_entry = NULL;
3457 s->selected_entry = NULL;
3459 s->thread_args.log_complete = 0;
3461 s->thread_args.commits_needed = view->lines;
3462 s->matched_entry = NULL;
3463 s->search_entry = NULL;
3467 if (view_is_parent_view(view))
3468 view_get_split(view, &begin_y, &begin_x);
3469 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3470 if (ref_view == NULL)
3471 return got_error_from_errno("view_open");
3472 err = open_ref_view(ref_view, s->repo);
3474 view_close(ref_view);
3477 if (view_is_parent_view(view) &&
3478 view->mode == TOG_VIEW_SPLIT_HRZN) {
3479 err = view_init_hsplit(view, begin_y);
3484 ref_view->focussed = 1;
3485 ref_view->mode = view->mode;
3486 ref_view->nlines = view->lines - begin_y;
3487 if (view_is_parent_view(view)) {
3488 view_transfer_size(ref_view, view);
3489 err = view_close_child(view);
3492 err = view_set_child(view, ref_view);
3495 view->focus_child = 1;
3497 *new_view = ref_view;
3507 static const struct got_error *
3508 apply_unveil(const char *repo_path, const char *worktree_path)
3510 const struct got_error *error;
3513 if (unveil("gmon.out", "rwc") != 0)
3514 return got_error_from_errno2("unveil", "gmon.out");
3516 if (repo_path && unveil(repo_path, "r") != 0)
3517 return got_error_from_errno2("unveil", repo_path);
3519 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3520 return got_error_from_errno2("unveil", worktree_path);
3522 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3523 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3525 error = got_privsep_unveil_exec_helpers();
3529 if (unveil(NULL, NULL) != 0)
3530 return got_error_from_errno("unveil");
3539 * Override default signal handlers before starting ncurses.
3540 * This should prevent ncurses from installing its own
3541 * broken cleanup() signal handler.
3543 signal(SIGWINCH, tog_sigwinch);
3544 signal(SIGPIPE, tog_sigpipe);
3545 signal(SIGCONT, tog_sigcont);
3546 signal(SIGINT, tog_sigint);
3547 signal(SIGTERM, tog_sigterm);
3551 halfdelay(1); /* Do fast refresh while initial view is loading. */
3554 intrflush(stdscr, FALSE);
3555 keypad(stdscr, TRUE);
3557 if (getenv("TOG_COLORS") != NULL) {
3559 use_default_colors();
3563 static const struct got_error *
3564 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3565 struct got_repository *repo, struct got_worktree *worktree)
3567 const struct got_error *err = NULL;
3570 *in_repo_path = strdup("/");
3571 if (*in_repo_path == NULL)
3572 return got_error_from_errno("strdup");
3577 const char *prefix = got_worktree_get_path_prefix(worktree);
3580 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3583 if (asprintf(in_repo_path, "%s%s%s", prefix,
3584 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3586 err = got_error_from_errno("asprintf");
3587 *in_repo_path = NULL;
3591 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3596 static const struct got_error *
3597 cmd_log(int argc, char *argv[])
3599 const struct got_error *error;
3600 struct got_repository *repo = NULL;
3601 struct got_worktree *worktree = NULL;
3602 struct got_object_id *start_id = NULL;
3603 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3604 char *start_commit = NULL, *label = NULL;
3605 struct got_reference *ref = NULL;
3606 const char *head_ref_name = NULL;
3607 int ch, log_branches = 0;
3608 struct tog_view *view;
3609 int *pack_fds = NULL;
3611 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3617 start_commit = optarg;
3620 repo_path = realpath(optarg, NULL);
3621 if (repo_path == NULL)
3622 return got_error_from_errno2("realpath",
3637 error = got_repo_pack_fds_open(&pack_fds);
3641 if (repo_path == NULL) {
3642 cwd = getcwd(NULL, 0);
3644 return got_error_from_errno("getcwd");
3645 error = got_worktree_open(&worktree, cwd);
3646 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3650 strdup(got_worktree_get_repo_path(worktree));
3652 repo_path = strdup(cwd);
3653 if (repo_path == NULL) {
3654 error = got_error_from_errno("strdup");
3659 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3663 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3670 error = apply_unveil(got_repo_get_path(repo),
3671 worktree ? got_worktree_get_root_path(worktree) : NULL);
3675 /* already loaded by tog_log_with_path()? */
3676 if (TAILQ_EMPTY(&tog_refs)) {
3677 error = tog_load_refs(repo, 0);
3682 if (start_commit == NULL) {
3683 error = got_repo_match_object_id(&start_id, &label,
3684 worktree ? got_worktree_get_head_ref_name(worktree) :
3685 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3688 head_ref_name = label;
3690 error = got_ref_open(&ref, repo, start_commit, 0);
3692 head_ref_name = got_ref_get_name(ref);
3693 else if (error->code != GOT_ERR_NOT_REF)
3695 error = got_repo_match_object_id(&start_id, NULL,
3696 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3701 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3703 error = got_error_from_errno("view_open");
3706 error = open_log_view(view, start_id, repo, head_ref_name,
3707 in_repo_path, log_branches);
3711 /* Release work tree lock. */
3712 got_worktree_close(worktree);
3715 error = view_loop(view);
3725 const struct got_error *close_err = got_repo_close(repo);
3730 got_worktree_close(worktree);
3732 const struct got_error *pack_err =
3733 got_repo_pack_fds_close(pack_fds);
3745 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3746 "[-w] object1 object2\n", getprogname());
3751 match_line(const char *line, regex_t *regex, size_t nmatch,
3752 regmatch_t *regmatch)
3754 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3757 static struct tog_color *
3758 match_color(struct tog_colors *colors, const char *line)
3760 struct tog_color *tc = NULL;
3762 STAILQ_FOREACH(tc, colors, entry) {
3763 if (match_line(line, &tc->regex, 0, NULL))
3770 static const struct got_error *
3771 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3772 WINDOW *window, int skipcol, regmatch_t *regmatch)
3774 const struct got_error *err = NULL;
3776 wchar_t *wline = NULL;
3777 int rme, rms, n, width, scrollx;
3778 int width0 = 0, width1 = 0, width2 = 0;
3779 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3783 rms = regmatch->rm_so;
3784 rme = regmatch->rm_eo;
3786 err = expand_tab(&exstr, line);
3790 /* Split the line into 3 segments, according to match offsets. */
3791 seg0 = strndup(exstr, rms);
3793 err = got_error_from_errno("strndup");
3796 seg1 = strndup(exstr + rms, rme - rms);
3798 err = got_error_from_errno("strndup");
3801 seg2 = strdup(exstr + rme);
3803 err = got_error_from_errno("strndup");
3807 /* draw up to matched token if we haven't scrolled past it */
3808 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3812 n = MAX(width0 - skipcol, 0);
3815 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3816 wlimit, col_tab_align, 1);
3819 waddwstr(window, &wline[scrollx]);
3829 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3833 wlen = wcslen(wline);
3835 width = wcwidth(wline[i]);
3837 /* should not happen, tabs are expanded */
3838 err = got_error(GOT_ERR_RANGE);
3841 if (width0 + w + width > skipcol)
3846 /* draw (visible part of) matched token (if scrolled into it) */
3847 if (width1 - w > 0) {
3848 wattron(window, A_STANDOUT);
3849 waddwstr(window, &wline[i]);
3850 wattroff(window, A_STANDOUT);
3851 wlimit -= (width1 - w);
3852 *wtotal += (width1 - w);
3856 if (wlimit > 0) { /* draw rest of line */
3858 if (skipcol > width0 + width1) {
3859 err = format_line(&wline, &width2, &scrollx, seg2,
3860 skipcol - (width0 + width1), wlimit,
3864 waddwstr(window, &wline[scrollx]);
3866 err = format_line(&wline, &width2, NULL, seg2, 0,
3867 wlimit, col_tab_align, 1);
3870 waddwstr(window, wline);
3883 static const struct got_error *
3884 draw_file(struct tog_view *view, const char *header)
3886 struct tog_diff_view_state *s = &view->state.diff;
3887 regmatch_t *regmatch = &view->regmatch;
3888 const struct got_error *err;
3891 size_t linesize = 0;
3893 struct tog_color *tc;
3896 int max_lines = view->nlines;
3897 int nlines = s->nlines;
3900 line_offset = s->line_offsets[s->first_displayed_line - 1];
3901 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3902 return got_error_from_errno("fseek");
3904 werase(view->window);
3907 if (asprintf(&line, "[%d/%d] %s",
3908 s->first_displayed_line - 1 + s->selected_line, nlines,
3910 return got_error_from_errno("asprintf");
3911 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3917 if (view_needs_focus_indication(view))
3918 wstandout(view->window);
3919 waddwstr(view->window, wline);
3922 if (view_needs_focus_indication(view))
3923 wstandend(view->window);
3924 if (width <= view->ncols - 1)
3925 waddch(view->window, '\n');
3935 while (max_lines > 0 && nprinted < max_lines) {
3936 linelen = getline(&line, &linesize, s->f);
3937 if (linelen == -1) {
3943 return got_ferror(s->f, GOT_ERR_IO);
3946 /* Set view->maxx based on full line length. */
3947 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3953 view->maxx = MAX(view->maxx, width);
3957 tc = match_color(&s->colors, line);
3959 wattr_on(view->window,
3960 COLOR_PAIR(tc->colorpair), NULL);
3961 if (s->first_displayed_line + nprinted == s->matched_line &&
3962 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3963 err = add_matched_line(&width, line, view->ncols, 0,
3964 view->window, view->x, regmatch);
3971 err = format_line(&wline, &width, &skip, line,
3972 view->x, view->ncols, 0, view->x ? 1 : 0);
3977 waddwstr(view->window, &wline[skip]);
3982 wattr_off(view->window,
3983 COLOR_PAIR(tc->colorpair), NULL);
3984 if (width <= view->ncols - 1)
3985 waddch(view->window, '\n');
3990 s->last_displayed_line = s->first_displayed_line +
3993 s->last_displayed_line = s->first_displayed_line;
3998 while (nprinted < view->nlines) {
3999 waddch(view->window, '\n');
4003 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4009 wstandout(view->window);
4010 waddwstr(view->window, wline);
4013 wstandend(view->window);
4020 get_datestr(time_t *time, char *datebuf)
4022 struct tm mytm, *tm;
4025 tm = gmtime_r(time, &mytm);
4028 s = asctime_r(tm, datebuf);
4031 p = strchr(s, '\n');
4037 static const struct got_error *
4038 get_changed_paths(struct got_pathlist_head *paths,
4039 struct got_commit_object *commit, struct got_repository *repo)
4041 const struct got_error *err = NULL;
4042 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4043 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4044 struct got_object_qid *qid;
4046 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4048 struct got_commit_object *pcommit;
4049 err = got_object_open_as_commit(&pcommit, repo,
4054 tree_id1 = got_object_id_dup(
4055 got_object_commit_get_tree_id(pcommit));
4056 if (tree_id1 == NULL) {
4057 got_object_commit_close(pcommit);
4058 return got_error_from_errno("got_object_id_dup");
4060 got_object_commit_close(pcommit);
4065 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4070 tree_id2 = got_object_commit_get_tree_id(commit);
4071 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4075 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4076 got_diff_tree_collect_changed_paths, paths, 0);
4079 got_object_tree_close(tree1);
4081 got_object_tree_close(tree2);
4086 static const struct got_error *
4087 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4091 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4093 return got_error_from_errno("reallocarray");
4095 (*line_offsets)[*nlines] = off;
4100 static const struct got_error *
4101 write_commit_info(off_t **line_offsets, size_t *nlines,
4102 struct got_object_id *commit_id, struct got_reflist_head *refs,
4103 struct got_repository *repo, FILE *outfile)
4105 const struct got_error *err = NULL;
4106 char datebuf[26], *datestr;
4107 struct got_commit_object *commit;
4108 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4109 time_t committer_time;
4110 const char *author, *committer;
4111 char *refs_str = NULL;
4112 struct got_pathlist_head changed_paths;
4113 struct got_pathlist_entry *pe;
4117 TAILQ_INIT(&changed_paths);
4120 err = build_refs_str(&refs_str, refs, commit_id, repo);
4125 err = got_object_open_as_commit(&commit, repo, commit_id);
4129 err = got_object_id_str(&id_str, commit_id);
4131 err = got_error_from_errno("got_object_id_str");
4135 err = add_line_offset(line_offsets, nlines, 0);
4139 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4140 refs_str ? refs_str : "", refs_str ? ")" : "");
4142 err = got_error_from_errno("fprintf");
4146 err = add_line_offset(line_offsets, nlines, outoff);
4150 n = fprintf(outfile, "from: %s\n",
4151 got_object_commit_get_author(commit));
4153 err = got_error_from_errno("fprintf");
4157 err = add_line_offset(line_offsets, nlines, outoff);
4161 committer_time = got_object_commit_get_committer_time(commit);
4162 datestr = get_datestr(&committer_time, datebuf);
4164 n = fprintf(outfile, "date: %s UTC\n", datestr);
4166 err = got_error_from_errno("fprintf");
4170 err = add_line_offset(line_offsets, nlines, outoff);
4174 author = got_object_commit_get_author(commit);
4175 committer = got_object_commit_get_committer(commit);
4176 if (strcmp(author, committer) != 0) {
4177 n = fprintf(outfile, "via: %s\n", committer);
4179 err = got_error_from_errno("fprintf");
4183 err = add_line_offset(line_offsets, nlines, outoff);
4187 if (got_object_commit_get_nparents(commit) > 1) {
4188 const struct got_object_id_queue *parent_ids;
4189 struct got_object_qid *qid;
4191 parent_ids = got_object_commit_get_parent_ids(commit);
4192 STAILQ_FOREACH(qid, parent_ids, entry) {
4193 err = got_object_id_str(&id_str, &qid->id);
4196 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4198 err = got_error_from_errno("fprintf");
4202 err = add_line_offset(line_offsets, nlines, outoff);
4210 err = got_object_commit_get_logmsg(&logmsg, commit);
4214 while ((line = strsep(&s, "\n")) != NULL) {
4215 n = fprintf(outfile, "%s\n", line);
4217 err = got_error_from_errno("fprintf");
4221 err = add_line_offset(line_offsets, nlines, outoff);
4226 err = get_changed_paths(&changed_paths, commit, repo);
4229 TAILQ_FOREACH(pe, &changed_paths, entry) {
4230 struct got_diff_changed_path *cp = pe->data;
4231 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4233 err = got_error_from_errno("fprintf");
4237 err = add_line_offset(line_offsets, nlines, outoff);
4240 free((char *)pe->path);
4244 fputc('\n', outfile);
4246 err = add_line_offset(line_offsets, nlines, outoff);
4248 got_pathlist_free(&changed_paths);
4252 got_object_commit_close(commit);
4254 free(*line_offsets);
4255 *line_offsets = NULL;
4261 static const struct got_error *
4262 create_diff(struct tog_diff_view_state *s)
4264 const struct got_error *err = NULL;
4268 free(s->line_offsets);
4269 s->line_offsets = malloc(sizeof(off_t));
4270 if (s->line_offsets == NULL)
4271 return got_error_from_errno("malloc");
4276 err = got_error_from_errno("got_opentemp");
4279 if (s->f && fclose(s->f) == EOF) {
4280 err = got_error_from_errno("fclose");
4286 err = got_object_get_type(&obj_type, s->repo, s->id1);
4288 err = got_object_get_type(&obj_type, s->repo, s->id2);
4293 case GOT_OBJ_TYPE_BLOB:
4294 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4295 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4296 s->label1, s->label2, tog_diff_algo, s->diff_context,
4297 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4299 case GOT_OBJ_TYPE_TREE:
4300 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4301 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4302 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4303 s->force_text_diff, s->repo, s->f);
4305 case GOT_OBJ_TYPE_COMMIT: {
4306 const struct got_object_id_queue *parent_ids;
4307 struct got_object_qid *pid;
4308 struct got_commit_object *commit2;
4309 struct got_reflist_head *refs;
4311 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4314 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4315 /* Show commit info if we're diffing to a parent/root commit. */
4316 if (s->id1 == NULL) {
4317 err = write_commit_info(&s->line_offsets, &s->nlines,
4318 s->id2, refs, s->repo, s->f);
4322 parent_ids = got_object_commit_get_parent_ids(commit2);
4323 STAILQ_FOREACH(pid, parent_ids, entry) {
4324 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4325 err = write_commit_info(
4326 &s->line_offsets, &s->nlines,
4327 s->id2, refs, s->repo, s->f);
4334 got_object_commit_close(commit2);
4336 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4337 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4338 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4339 s->force_text_diff, s->repo, s->f);
4343 err = got_error(GOT_ERR_OBJ_TYPE);
4349 if (s->f && fflush(s->f) != 0 && err == NULL)
4350 err = got_error_from_errno("fflush");
4355 diff_view_indicate_progress(struct tog_view *view)
4357 mvwaddstr(view->window, 0, 0, "diffing...");
4362 static const struct got_error *
4363 search_start_diff_view(struct tog_view *view)
4365 struct tog_diff_view_state *s = &view->state.diff;
4367 s->matched_line = 0;
4371 static const struct got_error *
4372 search_next_diff_view(struct tog_view *view)
4374 struct tog_diff_view_state *s = &view->state.diff;
4375 const struct got_error *err = NULL;
4378 size_t linesize = 0;
4381 if (!view->searching) {
4382 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4386 if (s->matched_line) {
4387 if (view->searching == TOG_SEARCH_FORWARD)
4388 lineno = s->matched_line + 1;
4390 lineno = s->matched_line - 1;
4392 lineno = s->first_displayed_line;
4397 if (lineno <= 0 || lineno > s->nlines) {
4398 if (s->matched_line == 0) {
4399 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4403 if (view->searching == TOG_SEARCH_FORWARD)
4409 offset = s->line_offsets[lineno - 1];
4410 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4412 return got_error_from_errno("fseeko");
4414 linelen = getline(&line, &linesize, s->f);
4415 if (linelen != -1) {
4417 err = expand_tab(&exstr, line);
4420 if (match_line(exstr, &view->regex, 1,
4422 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4423 s->matched_line = lineno;
4429 if (view->searching == TOG_SEARCH_FORWARD)
4436 if (s->matched_line) {
4437 s->first_displayed_line = s->matched_line;
4438 s->selected_line = 1;
4444 static const struct got_error *
4445 close_diff_view(struct tog_view *view)
4447 const struct got_error *err = NULL;
4448 struct tog_diff_view_state *s = &view->state.diff;
4454 if (s->f && fclose(s->f) == EOF)
4455 err = got_error_from_errno("fclose");
4457 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4458 err = got_error_from_errno("fclose");
4460 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4461 err = got_error_from_errno("fclose");
4463 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4464 err = got_error_from_errno("close");
4466 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4467 err = got_error_from_errno("close");
4469 free_colors(&s->colors);
4470 free(s->line_offsets);
4471 s->line_offsets = NULL;
4476 static const struct got_error *
4477 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4478 struct got_object_id *id2, const char *label1, const char *label2,
4479 int diff_context, int ignore_whitespace, int force_text_diff,
4480 struct tog_view *parent_view, struct got_repository *repo)
4482 const struct got_error *err;
4483 struct tog_diff_view_state *s = &view->state.diff;
4485 memset(s, 0, sizeof(*s));
4489 if (id1 != NULL && id2 != NULL) {
4491 err = got_object_get_type(&type1, repo, id1);
4494 err = got_object_get_type(&type2, repo, id2);
4499 return got_error(GOT_ERR_OBJ_TYPE);
4501 s->first_displayed_line = 1;
4502 s->last_displayed_line = view->nlines;
4503 s->selected_line = 1;
4511 s->id1 = got_object_id_dup(id1);
4513 return got_error_from_errno("got_object_id_dup");
4517 s->id2 = got_object_id_dup(id2);
4518 if (s->id2 == NULL) {
4519 err = got_error_from_errno("got_object_id_dup");
4523 s->f1 = got_opentemp();
4524 if (s->f1 == NULL) {
4525 err = got_error_from_errno("got_opentemp");
4529 s->f2 = got_opentemp();
4530 if (s->f2 == NULL) {
4531 err = got_error_from_errno("got_opentemp");
4535 s->fd1 = got_opentempfd();
4537 err = got_error_from_errno("got_opentempfd");
4541 s->fd2 = got_opentempfd();
4543 err = got_error_from_errno("got_opentempfd");
4547 s->first_displayed_line = 1;
4548 s->last_displayed_line = view->nlines;
4549 s->diff_context = diff_context;
4550 s->ignore_whitespace = ignore_whitespace;
4551 s->force_text_diff = force_text_diff;
4552 s->parent_view = parent_view;
4555 STAILQ_INIT(&s->colors);
4556 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4557 err = add_color(&s->colors,
4558 "^-", TOG_COLOR_DIFF_MINUS,
4559 get_color_value("TOG_COLOR_DIFF_MINUS"));
4562 err = add_color(&s->colors, "^\\+",
4563 TOG_COLOR_DIFF_PLUS,
4564 get_color_value("TOG_COLOR_DIFF_PLUS"));
4567 err = add_color(&s->colors,
4568 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4569 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4573 err = add_color(&s->colors,
4574 "^(commit [0-9a-f]|parent [0-9]|"
4575 "(blob|file|tree|commit) [-+] |"
4576 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4577 get_color_value("TOG_COLOR_DIFF_META"));
4581 err = add_color(&s->colors,
4582 "^(from|via): ", TOG_COLOR_AUTHOR,
4583 get_color_value("TOG_COLOR_AUTHOR"));
4587 err = add_color(&s->colors,
4588 "^date: ", TOG_COLOR_DATE,
4589 get_color_value("TOG_COLOR_DATE"));
4594 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4595 view_is_splitscreen(view))
4596 show_log_view(parent_view); /* draw border */
4597 diff_view_indicate_progress(view);
4599 err = create_diff(s);
4601 view->show = show_diff_view;
4602 view->input = input_diff_view;
4603 view->reset = reset_diff_view;
4604 view->close = close_diff_view;
4605 view->search_start = search_start_diff_view;
4606 view->search_next = search_next_diff_view;
4609 close_diff_view(view);
4613 static const struct got_error *
4614 show_diff_view(struct tog_view *view)
4616 const struct got_error *err;
4617 struct tog_diff_view_state *s = &view->state.diff;
4618 char *id_str1 = NULL, *id_str2, *header;
4619 const char *label1, *label2;
4622 err = got_object_id_str(&id_str1, s->id1);
4625 label1 = s->label1 ? : id_str1;
4627 label1 = "/dev/null";
4629 err = got_object_id_str(&id_str2, s->id2);
4632 label2 = s->label2 ? : id_str2;
4634 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4635 err = got_error_from_errno("asprintf");
4643 err = draw_file(view, header);
4648 static const struct got_error *
4649 set_selected_commit(struct tog_diff_view_state *s,
4650 struct commit_queue_entry *entry)
4652 const struct got_error *err;
4653 const struct got_object_id_queue *parent_ids;
4654 struct got_commit_object *selected_commit;
4655 struct got_object_qid *pid;
4658 s->id2 = got_object_id_dup(entry->id);
4660 return got_error_from_errno("got_object_id_dup");
4662 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4665 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4667 pid = STAILQ_FIRST(parent_ids);
4668 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4669 got_object_commit_close(selected_commit);
4673 static const struct got_error *
4674 reset_diff_view(struct tog_view *view)
4676 struct tog_diff_view_state *s = &view->state.diff;
4679 wclear(view->window);
4680 s->first_displayed_line = 1;
4681 s->last_displayed_line = view->nlines;
4682 s->matched_line = 0;
4683 diff_view_indicate_progress(view);
4684 return create_diff(s);
4687 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4689 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4692 static const struct got_error *
4693 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4695 const struct got_error *err = NULL;
4696 struct tog_diff_view_state *s = &view->state.diff;
4697 struct tog_log_view_state *ls;
4698 struct commit_queue_entry *old_selected_entry;
4700 size_t linesize = 0;
4702 int i, nscroll = view->nlines - 1, up = 0;
4709 view->x = MAX(view->maxx - view->ncols / 3, 0);
4714 if (view->x + view->ncols / 3 < view->maxx)
4715 view->x += 2; /* move two columns right */
4721 view->x -= MIN(view->x, 2); /* move two columns back */
4728 s->force_text_diff = !s->force_text_diff;
4730 s->ignore_whitespace = !s->ignore_whitespace;
4731 err = reset_diff_view(view);
4735 s->first_displayed_line = 1;
4744 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4750 if (s->first_displayed_line > 1)
4751 s->first_displayed_line--;
4762 if (s->first_displayed_line == 1) {
4767 while (i++ < nscroll && s->first_displayed_line > 1)
4768 s->first_displayed_line--;
4774 s->first_displayed_line++;
4791 while (!s->eof && i++ < nscroll) {
4792 linelen = getline(&line, &linesize, s->f);
4793 s->first_displayed_line++;
4794 if (linelen == -1) {
4798 err = got_ferror(s->f, GOT_ERR_IO);
4805 if (s->diff_context > 0) {
4807 s->matched_line = 0;
4808 diff_view_indicate_progress(view);
4809 err = create_diff(s);
4810 if (s->first_displayed_line + view->nlines - 1 >
4812 s->first_displayed_line = 1;
4813 s->last_displayed_line = view->nlines;
4819 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4821 s->matched_line = 0;
4822 diff_view_indicate_progress(view);
4823 err = create_diff(s);
4833 if (s->parent_view == NULL) {
4837 s->parent_view->count = view->count;
4839 if (s->parent_view->type == TOG_VIEW_LOG) {
4840 ls = &s->parent_view->state.log;
4841 old_selected_entry = ls->selected_entry;
4843 err = input_log_view(NULL, s->parent_view,
4844 up ? KEY_UP : KEY_DOWN);
4847 view->count = s->parent_view->count;
4849 if (old_selected_entry == ls->selected_entry)
4852 err = set_selected_commit(s, ls->selected_entry);
4855 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4856 struct tog_blame_view_state *bs;
4857 struct got_object_id *id, *prev_id;
4859 bs = &s->parent_view->state.blame;
4860 prev_id = get_annotation_for_line(bs->blame.lines,
4861 bs->blame.nlines, bs->last_diffed_line);
4863 err = input_blame_view(&view, s->parent_view,
4864 up ? KEY_UP : KEY_DOWN);
4867 view->count = s->parent_view->count;
4869 if (prev_id == NULL)
4871 id = get_selected_commit_id(bs->blame.lines,
4872 bs->blame.nlines, bs->first_displayed_line,
4877 if (!got_object_id_cmp(prev_id, id))
4880 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4884 s->first_displayed_line = 1;
4885 s->last_displayed_line = view->nlines;
4886 s->matched_line = 0;
4889 diff_view_indicate_progress(view);
4890 err = create_diff(s);
4900 static const struct got_error *
4901 cmd_diff(int argc, char *argv[])
4903 const struct got_error *error = NULL;
4904 struct got_repository *repo = NULL;
4905 struct got_worktree *worktree = NULL;
4906 struct got_object_id *id1 = NULL, *id2 = NULL;
4907 char *repo_path = NULL, *cwd = NULL;
4908 char *id_str1 = NULL, *id_str2 = NULL;
4909 char *label1 = NULL, *label2 = NULL;
4910 int diff_context = 3, ignore_whitespace = 0;
4911 int ch, force_text_diff = 0;
4913 struct tog_view *view;
4914 int *pack_fds = NULL;
4916 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4919 force_text_diff = 1;
4922 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4925 errx(1, "number of context lines is %s: %s",
4929 repo_path = realpath(optarg, NULL);
4930 if (repo_path == NULL)
4931 return got_error_from_errno2("realpath",
4933 got_path_strip_trailing_slashes(repo_path);
4936 ignore_whitespace = 1;
4948 usage_diff(); /* TODO show local worktree changes */
4949 } else if (argc == 2) {
4955 error = got_repo_pack_fds_open(&pack_fds);
4959 if (repo_path == NULL) {
4960 cwd = getcwd(NULL, 0);
4962 return got_error_from_errno("getcwd");
4963 error = got_worktree_open(&worktree, cwd);
4964 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4968 strdup(got_worktree_get_repo_path(worktree));
4970 repo_path = strdup(cwd);
4971 if (repo_path == NULL) {
4972 error = got_error_from_errno("strdup");
4977 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4983 error = apply_unveil(got_repo_get_path(repo), NULL);
4987 error = tog_load_refs(repo, 0);
4991 error = got_repo_match_object_id(&id1, &label1, id_str1,
4992 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4996 error = got_repo_match_object_id(&id2, &label2, id_str2,
4997 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5001 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5003 error = got_error_from_errno("view_open");
5006 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5007 ignore_whitespace, force_text_diff, NULL, repo);
5010 error = view_loop(view);
5017 const struct got_error *close_err = got_repo_close(repo);
5022 got_worktree_close(worktree);
5024 const struct got_error *pack_err =
5025 got_repo_pack_fds_close(pack_fds);
5038 "usage: %s blame [-c commit] [-r repository-path] path\n",
5043 struct tog_blame_line {
5045 struct got_object_id *id;
5048 static const struct got_error *
5049 draw_blame(struct tog_view *view)
5051 struct tog_blame_view_state *s = &view->state.blame;
5052 struct tog_blame *blame = &s->blame;
5053 regmatch_t *regmatch = &view->regmatch;
5054 const struct got_error *err;
5055 int lineno = 0, nprinted = 0;
5057 size_t linesize = 0;
5061 struct tog_blame_line *blame_line;
5062 struct got_object_id *prev_id = NULL;
5064 struct tog_color *tc;
5066 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5071 werase(view->window);
5073 if (asprintf(&line, "commit %s", id_str) == -1) {
5074 err = got_error_from_errno("asprintf");
5079 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5084 if (view_needs_focus_indication(view))
5085 wstandout(view->window);
5086 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5088 wattr_on(view->window,
5089 COLOR_PAIR(tc->colorpair), NULL);
5090 waddwstr(view->window, wline);
5092 wattr_off(view->window,
5093 COLOR_PAIR(tc->colorpair), NULL);
5094 if (view_needs_focus_indication(view))
5095 wstandend(view->window);
5098 if (width < view->ncols - 1)
5099 waddch(view->window, '\n');
5101 if (asprintf(&line, "[%d/%d] %s%s",
5102 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5103 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5105 return got_error_from_errno("asprintf");
5108 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5113 waddwstr(view->window, wline);
5116 if (width < view->ncols - 1)
5117 waddch(view->window, '\n');
5121 while (nprinted < view->nlines - 2) {
5122 linelen = getline(&line, &linesize, blame->f);
5123 if (linelen == -1) {
5124 if (feof(blame->f)) {
5129 return got_ferror(blame->f, GOT_ERR_IO);
5131 if (++lineno < s->first_displayed_line)
5134 /* Set view->maxx based on full line length. */
5135 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5142 view->maxx = MAX(view->maxx, width);
5144 if (nprinted == s->selected_line - 1)
5145 wstandout(view->window);
5147 if (blame->nlines > 0) {
5148 blame_line = &blame->lines[lineno - 1];
5149 if (blame_line->annotated && prev_id &&
5150 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5151 !(nprinted == s->selected_line - 1)) {
5152 waddstr(view->window, " ");
5153 } else if (blame_line->annotated) {
5155 err = got_object_id_str(&id_str,
5161 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5163 wattr_on(view->window,
5164 COLOR_PAIR(tc->colorpair), NULL);
5165 wprintw(view->window, "%.8s", id_str);
5167 wattr_off(view->window,
5168 COLOR_PAIR(tc->colorpair), NULL);
5170 prev_id = blame_line->id;
5172 waddstr(view->window, "........");
5176 waddstr(view->window, "........");
5180 if (nprinted == s->selected_line - 1)
5181 wstandend(view->window);
5182 waddstr(view->window, " ");
5184 if (view->ncols <= 9) {
5186 } else if (s->first_displayed_line + nprinted ==
5188 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5189 err = add_matched_line(&width, line, view->ncols - 9, 9,
5190 view->window, view->x, regmatch);
5198 err = format_line(&wline, &width, &skip, line,
5199 view->x, view->ncols - 9, 9, 1);
5204 waddwstr(view->window, &wline[skip]);
5210 if (width <= view->ncols - 1)
5211 waddch(view->window, '\n');
5212 if (++nprinted == 1)
5213 s->first_displayed_line = lineno;
5216 s->last_displayed_line = lineno;
5223 static const struct got_error *
5224 blame_cb(void *arg, int nlines, int lineno,
5225 struct got_commit_object *commit, struct got_object_id *id)
5227 const struct got_error *err = NULL;
5228 struct tog_blame_cb_args *a = arg;
5229 struct tog_blame_line *line;
5232 if (nlines != a->nlines ||
5233 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5234 return got_error(GOT_ERR_RANGE);
5236 errcode = pthread_mutex_lock(&tog_mutex);
5238 return got_error_set_errno(errcode, "pthread_mutex_lock");
5240 if (*a->quit) { /* user has quit the blame view */
5241 err = got_error(GOT_ERR_ITER_COMPLETED);
5246 goto done; /* no change in this commit */
5248 line = &a->lines[lineno - 1];
5249 if (line->annotated)
5252 line->id = got_object_id_dup(id);
5253 if (line->id == NULL) {
5254 err = got_error_from_errno("got_object_id_dup");
5257 line->annotated = 1;
5259 errcode = pthread_mutex_unlock(&tog_mutex);
5261 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5266 blame_thread(void *arg)
5268 const struct got_error *err, *close_err;
5269 struct tog_blame_thread_args *ta = arg;
5270 struct tog_blame_cb_args *a = ta->cb_args;
5271 int errcode, fd1 = -1, fd2 = -1;
5272 FILE *f1 = NULL, *f2 = NULL;
5274 fd1 = got_opentempfd();
5276 return (void *)got_error_from_errno("got_opentempfd");
5278 fd2 = got_opentempfd();
5280 err = got_error_from_errno("got_opentempfd");
5284 f1 = got_opentemp();
5286 err = (void *)got_error_from_errno("got_opentemp");
5289 f2 = got_opentemp();
5291 err = (void *)got_error_from_errno("got_opentemp");
5295 err = block_signals_used_by_main_thread();
5299 err = got_blame(ta->path, a->commit_id, ta->repo,
5300 tog_diff_algo, blame_cb, ta->cb_args,
5301 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5302 if (err && err->code == GOT_ERR_CANCELLED)
5305 errcode = pthread_mutex_lock(&tog_mutex);
5307 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5311 close_err = got_repo_close(ta->repo);
5317 errcode = pthread_mutex_unlock(&tog_mutex);
5318 if (errcode && err == NULL)
5319 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5322 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5323 err = got_error_from_errno("close");
5324 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5325 err = got_error_from_errno("close");
5326 if (f1 && fclose(f1) == EOF && err == NULL)
5327 err = got_error_from_errno("fclose");
5328 if (f2 && fclose(f2) == EOF && err == NULL)
5329 err = got_error_from_errno("fclose");
5334 static struct got_object_id *
5335 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5336 int first_displayed_line, int selected_line)
5338 struct tog_blame_line *line;
5343 line = &lines[first_displayed_line - 1 + selected_line - 1];
5344 if (!line->annotated)
5350 static struct got_object_id *
5351 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5354 struct tog_blame_line *line;
5356 if (nlines <= 0 || lineno >= nlines)
5359 line = &lines[lineno - 1];
5360 if (!line->annotated)
5366 static const struct got_error *
5367 stop_blame(struct tog_blame *blame)
5369 const struct got_error *err = NULL;
5372 if (blame->thread) {
5374 errcode = pthread_mutex_unlock(&tog_mutex);
5376 return got_error_set_errno(errcode,
5377 "pthread_mutex_unlock");
5378 errcode = pthread_join(blame->thread, (void **)&err);
5380 return got_error_set_errno(errcode, "pthread_join");
5381 errcode = pthread_mutex_lock(&tog_mutex);
5383 return got_error_set_errno(errcode,
5384 "pthread_mutex_lock");
5385 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5387 blame->thread = NULL;
5389 if (blame->thread_args.repo) {
5390 const struct got_error *close_err;
5391 close_err = got_repo_close(blame->thread_args.repo);
5394 blame->thread_args.repo = NULL;
5397 if (fclose(blame->f) == EOF && err == NULL)
5398 err = got_error_from_errno("fclose");
5402 for (i = 0; i < blame->nlines; i++)
5403 free(blame->lines[i].id);
5405 blame->lines = NULL;
5407 free(blame->cb_args.commit_id);
5408 blame->cb_args.commit_id = NULL;
5409 if (blame->pack_fds) {
5410 const struct got_error *pack_err =
5411 got_repo_pack_fds_close(blame->pack_fds);
5414 blame->pack_fds = NULL;
5419 static const struct got_error *
5420 cancel_blame_view(void *arg)
5422 const struct got_error *err = NULL;
5426 errcode = pthread_mutex_lock(&tog_mutex);
5428 return got_error_set_errno(errcode,
5429 "pthread_mutex_unlock");
5432 err = got_error(GOT_ERR_CANCELLED);
5434 errcode = pthread_mutex_unlock(&tog_mutex);
5436 return got_error_set_errno(errcode,
5437 "pthread_mutex_lock");
5442 static const struct got_error *
5443 run_blame(struct tog_view *view)
5445 struct tog_blame_view_state *s = &view->state.blame;
5446 struct tog_blame *blame = &s->blame;
5447 const struct got_error *err = NULL;
5448 struct got_commit_object *commit = NULL;
5449 struct got_blob_object *blob = NULL;
5450 struct got_repository *thread_repo = NULL;
5451 struct got_object_id *obj_id = NULL;
5452 int obj_type, fd = -1;
5453 int *pack_fds = NULL;
5455 err = got_object_open_as_commit(&commit, s->repo,
5456 &s->blamed_commit->id);
5460 fd = got_opentempfd();
5462 err = got_error_from_errno("got_opentempfd");
5466 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5470 err = got_object_get_type(&obj_type, s->repo, obj_id);
5474 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5475 err = got_error(GOT_ERR_OBJ_TYPE);
5479 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5482 blame->f = got_opentemp();
5483 if (blame->f == NULL) {
5484 err = got_error_from_errno("got_opentemp");
5487 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5488 &blame->line_offsets, blame->f, blob);
5491 if (blame->nlines == 0) {
5492 s->blame_complete = 1;
5496 /* Don't include \n at EOF in the blame line count. */
5497 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5500 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5501 if (blame->lines == NULL) {
5502 err = got_error_from_errno("calloc");
5506 err = got_repo_pack_fds_open(&pack_fds);
5509 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5514 blame->pack_fds = pack_fds;
5515 blame->cb_args.view = view;
5516 blame->cb_args.lines = blame->lines;
5517 blame->cb_args.nlines = blame->nlines;
5518 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5519 if (blame->cb_args.commit_id == NULL) {
5520 err = got_error_from_errno("got_object_id_dup");
5523 blame->cb_args.quit = &s->done;
5525 blame->thread_args.path = s->path;
5526 blame->thread_args.repo = thread_repo;
5527 blame->thread_args.cb_args = &blame->cb_args;
5528 blame->thread_args.complete = &s->blame_complete;
5529 blame->thread_args.cancel_cb = cancel_blame_view;
5530 blame->thread_args.cancel_arg = &s->done;
5531 s->blame_complete = 0;
5533 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5534 s->first_displayed_line = 1;
5535 s->last_displayed_line = view->nlines;
5536 s->selected_line = 1;
5538 s->matched_line = 0;
5542 got_object_commit_close(commit);
5543 if (fd != -1 && close(fd) == -1 && err == NULL)
5544 err = got_error_from_errno("close");
5546 got_object_blob_close(blob);
5553 static const struct got_error *
5554 open_blame_view(struct tog_view *view, char *path,
5555 struct got_object_id *commit_id, struct got_repository *repo)
5557 const struct got_error *err = NULL;
5558 struct tog_blame_view_state *s = &view->state.blame;
5560 STAILQ_INIT(&s->blamed_commits);
5562 s->path = strdup(path);
5563 if (s->path == NULL)
5564 return got_error_from_errno("strdup");
5566 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5572 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5573 s->first_displayed_line = 1;
5574 s->last_displayed_line = view->nlines;
5575 s->selected_line = 1;
5576 s->blame_complete = 0;
5578 s->commit_id = commit_id;
5579 memset(&s->blame, 0, sizeof(s->blame));
5581 STAILQ_INIT(&s->colors);
5582 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5583 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5584 get_color_value("TOG_COLOR_COMMIT"));
5589 view->show = show_blame_view;
5590 view->input = input_blame_view;
5591 view->reset = reset_blame_view;
5592 view->close = close_blame_view;
5593 view->search_start = search_start_blame_view;
5594 view->search_next = search_next_blame_view;
5596 return run_blame(view);
5599 static const struct got_error *
5600 close_blame_view(struct tog_view *view)
5602 const struct got_error *err = NULL;
5603 struct tog_blame_view_state *s = &view->state.blame;
5605 if (s->blame.thread)
5606 err = stop_blame(&s->blame);
5608 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5609 struct got_object_qid *blamed_commit;
5610 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5611 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5612 got_object_qid_free(blamed_commit);
5616 free_colors(&s->colors);
5620 static const struct got_error *
5621 search_start_blame_view(struct tog_view *view)
5623 struct tog_blame_view_state *s = &view->state.blame;
5625 s->matched_line = 0;
5629 static const struct got_error *
5630 search_next_blame_view(struct tog_view *view)
5632 struct tog_blame_view_state *s = &view->state.blame;
5633 const struct got_error *err = NULL;
5636 size_t linesize = 0;
5639 if (!view->searching) {
5640 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5644 if (s->matched_line) {
5645 if (view->searching == TOG_SEARCH_FORWARD)
5646 lineno = s->matched_line + 1;
5648 lineno = s->matched_line - 1;
5650 lineno = s->first_displayed_line - 1 + s->selected_line;
5655 if (lineno <= 0 || lineno > s->blame.nlines) {
5656 if (s->matched_line == 0) {
5657 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5661 if (view->searching == TOG_SEARCH_FORWARD)
5664 lineno = s->blame.nlines;
5667 offset = s->blame.line_offsets[lineno - 1];
5668 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5670 return got_error_from_errno("fseeko");
5672 linelen = getline(&line, &linesize, s->blame.f);
5673 if (linelen != -1) {
5675 err = expand_tab(&exstr, line);
5678 if (match_line(exstr, &view->regex, 1,
5680 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5681 s->matched_line = lineno;
5687 if (view->searching == TOG_SEARCH_FORWARD)
5694 if (s->matched_line) {
5695 s->first_displayed_line = s->matched_line;
5696 s->selected_line = 1;
5702 static const struct got_error *
5703 show_blame_view(struct tog_view *view)
5705 const struct got_error *err = NULL;
5706 struct tog_blame_view_state *s = &view->state.blame;
5709 if (s->blame.thread == NULL && !s->blame_complete) {
5710 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5711 &s->blame.thread_args);
5713 return got_error_set_errno(errcode, "pthread_create");
5715 halfdelay(1); /* fast refresh while annotating */
5718 if (s->blame_complete)
5719 halfdelay(10); /* disable fast refresh */
5721 err = draw_blame(view);
5727 static const struct got_error *
5728 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5730 const struct got_error *err = NULL, *thread_err = NULL;
5731 struct tog_view *diff_view;
5732 struct tog_blame_view_state *s = &view->state.blame;
5733 int eos, nscroll, begin_y = 0, begin_x = 0;
5735 eos = nscroll = view->nlines - 2;
5736 if (view_is_hsplit_top(view))
5744 view->x = MAX(view->maxx - view->ncols / 3, 0);
5749 if (view->x + view->ncols / 3 < view->maxx)
5750 view->x += 2; /* move two columns right */
5756 view->x -= MIN(view->x, 2); /* move two columns back */
5765 s->selected_line = 1;
5766 s->first_displayed_line = 1;
5771 if (s->blame.nlines < eos) {
5772 s->selected_line = s->blame.nlines;
5773 s->first_displayed_line = 1;
5775 s->selected_line = eos;
5776 s->first_displayed_line = s->blame.nlines - (eos - 1);
5783 if (s->selected_line > 1)
5785 else if (s->selected_line == 1 &&
5786 s->first_displayed_line > 1)
5787 s->first_displayed_line--;
5798 if (s->first_displayed_line == 1) {
5799 if (view->count > 1)
5801 s->selected_line = MAX(1, s->selected_line - nscroll);
5805 if (s->first_displayed_line > nscroll)
5806 s->first_displayed_line -= nscroll;
5808 s->first_displayed_line = 1;
5813 if (s->selected_line < eos && s->first_displayed_line +
5814 s->selected_line <= s->blame.nlines)
5816 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5817 s->first_displayed_line++;
5823 struct got_object_id *id = NULL;
5826 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5827 s->first_displayed_line, s->selected_line);
5831 struct got_commit_object *commit, *pcommit;
5832 struct got_object_qid *pid;
5833 struct got_object_id *blob_id = NULL;
5835 err = got_object_open_as_commit(&commit,
5840 got_object_commit_get_parent_ids(commit));
5842 got_object_commit_close(commit);
5845 /* Check if path history ends here. */
5846 err = got_object_open_as_commit(&pcommit,
5850 err = got_object_id_by_path(&blob_id, s->repo,
5852 got_object_commit_close(pcommit);
5854 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5856 got_object_commit_close(commit);
5859 err = got_object_get_type(&obj_type, s->repo,
5862 /* Can't blame non-blob type objects. */
5863 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5864 got_object_commit_close(commit);
5867 err = got_object_qid_alloc(&s->blamed_commit,
5869 got_object_commit_close(commit);
5871 if (got_object_id_cmp(id,
5872 &s->blamed_commit->id) == 0)
5874 err = got_object_qid_alloc(&s->blamed_commit,
5880 thread_err = stop_blame(&s->blame);
5884 STAILQ_INSERT_HEAD(&s->blamed_commits,
5885 s->blamed_commit, entry);
5886 err = run_blame(view);
5892 struct got_object_qid *first;
5895 first = STAILQ_FIRST(&s->blamed_commits);
5896 if (!got_object_id_cmp(&first->id, s->commit_id))
5899 thread_err = stop_blame(&s->blame);
5903 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5904 got_object_qid_free(s->blamed_commit);
5906 STAILQ_FIRST(&s->blamed_commits);
5907 err = run_blame(view);
5914 struct got_object_id *id = NULL;
5915 struct got_object_qid *pid;
5916 struct got_commit_object *commit = NULL;
5919 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5920 s->first_displayed_line, s->selected_line);
5923 err = got_object_open_as_commit(&commit, s->repo, id);
5926 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5928 /* traversed from diff view, release diff resources */
5929 err = close_diff_view(*new_view);
5932 diff_view = *new_view;
5934 if (view_is_parent_view(view))
5935 view_get_split(view, &begin_y, &begin_x);
5937 diff_view = view_open(0, 0, begin_y, begin_x,
5939 if (diff_view == NULL) {
5940 got_object_commit_close(commit);
5941 err = got_error_from_errno("view_open");
5945 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5946 id, NULL, NULL, 3, 0, 0, view, s->repo);
5947 got_object_commit_close(commit);
5949 view_close(diff_view);
5952 s->last_diffed_line = s->first_displayed_line - 1 +
5955 break; /* still open from active diff view */
5956 if (view_is_parent_view(view) &&
5957 view->mode == TOG_VIEW_SPLIT_HRZN) {
5958 err = view_init_hsplit(view, begin_y);
5964 diff_view->focussed = 1;
5965 diff_view->mode = view->mode;
5966 diff_view->nlines = view->lines - begin_y;
5967 if (view_is_parent_view(view)) {
5968 view_transfer_size(diff_view, view);
5969 err = view_close_child(view);
5972 err = view_set_child(view, diff_view);
5975 view->focus_child = 1;
5977 *new_view = diff_view;
5990 if (s->last_displayed_line >= s->blame.nlines &&
5991 s->selected_line >= MIN(s->blame.nlines,
5992 view->nlines - 2)) {
5996 if (s->last_displayed_line >= s->blame.nlines &&
5997 s->selected_line < view->nlines - 2) {
5999 MIN(nscroll, s->last_displayed_line -
6000 s->first_displayed_line - s->selected_line + 1);
6002 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6003 s->first_displayed_line += nscroll;
6005 s->first_displayed_line =
6006 s->blame.nlines - (view->nlines - 3);
6009 if (s->selected_line > view->nlines - 2) {
6010 s->selected_line = MIN(s->blame.nlines,
6018 return thread_err ? thread_err : err;
6021 static const struct got_error *
6022 reset_blame_view(struct tog_view *view)
6024 const struct got_error *err;
6025 struct tog_blame_view_state *s = &view->state.blame;
6029 err = stop_blame(&s->blame);
6033 return run_blame(view);
6036 static const struct got_error *
6037 cmd_blame(int argc, char *argv[])
6039 const struct got_error *error;
6040 struct got_repository *repo = NULL;
6041 struct got_worktree *worktree = NULL;
6042 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6043 char *link_target = NULL;
6044 struct got_object_id *commit_id = NULL;
6045 struct got_commit_object *commit = NULL;
6046 char *commit_id_str = NULL;
6048 struct tog_view *view;
6049 int *pack_fds = NULL;
6051 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6054 commit_id_str = optarg;
6057 repo_path = realpath(optarg, NULL);
6058 if (repo_path == NULL)
6059 return got_error_from_errno2("realpath",
6074 error = got_repo_pack_fds_open(&pack_fds);
6078 if (repo_path == NULL) {
6079 cwd = getcwd(NULL, 0);
6081 return got_error_from_errno("getcwd");
6082 error = got_worktree_open(&worktree, cwd);
6083 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6087 strdup(got_worktree_get_repo_path(worktree));
6089 repo_path = strdup(cwd);
6090 if (repo_path == NULL) {
6091 error = got_error_from_errno("strdup");
6096 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6100 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6107 error = apply_unveil(got_repo_get_path(repo), NULL);
6111 error = tog_load_refs(repo, 0);
6115 if (commit_id_str == NULL) {
6116 struct got_reference *head_ref;
6117 error = got_ref_open(&head_ref, repo, worktree ?
6118 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6121 error = got_ref_resolve(&commit_id, repo, head_ref);
6122 got_ref_close(head_ref);
6124 error = got_repo_match_object_id(&commit_id, NULL,
6125 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6130 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6132 error = got_error_from_errno("view_open");
6136 error = got_object_open_as_commit(&commit, repo, commit_id);
6140 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6145 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6150 /* Release work tree lock. */
6151 got_worktree_close(worktree);
6154 error = view_loop(view);
6162 got_object_commit_close(commit);
6164 got_worktree_close(worktree);
6166 const struct got_error *close_err = got_repo_close(repo);
6171 const struct got_error *pack_err =
6172 got_repo_pack_fds_close(pack_fds);
6180 static const struct got_error *
6181 draw_tree_entries(struct tog_view *view, const char *parent_path)
6183 struct tog_tree_view_state *s = &view->state.tree;
6184 const struct got_error *err = NULL;
6185 struct got_tree_entry *te;
6187 struct tog_color *tc;
6188 int width, n, i, nentries;
6189 int limit = view->nlines;
6192 if (view_is_hsplit_top(view))
6193 --limit; /* border */
6195 werase(view->window);
6200 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6204 if (view_needs_focus_indication(view))
6205 wstandout(view->window);
6206 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6208 wattr_on(view->window,
6209 COLOR_PAIR(tc->colorpair), NULL);
6210 waddwstr(view->window, wline);
6212 wattr_off(view->window,
6213 COLOR_PAIR(tc->colorpair), NULL);
6214 if (view_needs_focus_indication(view))
6215 wstandend(view->window);
6218 if (width < view->ncols - 1)
6219 waddch(view->window, '\n');
6222 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6226 waddwstr(view->window, wline);
6229 if (width < view->ncols - 1)
6230 waddch(view->window, '\n');
6233 waddch(view->window, '\n');
6237 if (s->first_displayed_entry == NULL) {
6238 te = got_object_tree_get_first_entry(s->tree);
6239 if (s->selected == 0) {
6241 wstandout(view->window);
6242 s->selected_entry = NULL;
6244 waddstr(view->window, " ..\n"); /* parent directory */
6245 if (s->selected == 0 && view->focussed)
6246 wstandend(view->window);
6253 te = s->first_displayed_entry;
6256 nentries = got_object_tree_get_nentries(s->tree);
6257 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6258 char *line = NULL, *id_str = NULL, *link_target = NULL;
6259 const char *modestr = "";
6262 te = got_object_tree_get_entry(s->tree, i);
6263 mode = got_tree_entry_get_mode(te);
6266 err = got_object_id_str(&id_str,
6267 got_tree_entry_get_id(te));
6269 return got_error_from_errno(
6270 "got_object_id_str");
6272 if (got_object_tree_entry_is_submodule(te))
6274 else if (S_ISLNK(mode)) {
6277 err = got_tree_entry_get_symlink_target(&link_target,
6283 for (i = 0; i < strlen(link_target); i++) {
6284 if (!isprint((unsigned char)link_target[i]))
6285 link_target[i] = '?';
6289 else if (S_ISDIR(mode))
6291 else if (mode & S_IXUSR)
6293 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6294 got_tree_entry_get_name(te), modestr,
6295 link_target ? " -> ": "",
6296 link_target ? link_target : "") == -1) {
6299 return got_error_from_errno("asprintf");
6303 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6309 if (n == s->selected) {
6311 wstandout(view->window);
6312 s->selected_entry = te;
6314 tc = match_color(&s->colors, line);
6316 wattr_on(view->window,
6317 COLOR_PAIR(tc->colorpair), NULL);
6318 waddwstr(view->window, wline);
6320 wattr_off(view->window,
6321 COLOR_PAIR(tc->colorpair), NULL);
6322 if (width < view->ncols - 1)
6323 waddch(view->window, '\n');
6324 if (n == s->selected && view->focussed)
6325 wstandend(view->window);
6331 s->last_displayed_entry = te;
6340 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6342 struct got_tree_entry *te;
6343 int isroot = s->tree == s->root;
6346 if (s->first_displayed_entry == NULL)
6349 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6350 while (i++ < maxscroll) {
6353 s->first_displayed_entry = NULL;
6356 s->first_displayed_entry = te;
6357 te = got_tree_entry_get_prev(s->tree, te);
6361 static const struct got_error *
6362 tree_scroll_down(struct tog_view *view, int maxscroll)
6364 struct tog_tree_view_state *s = &view->state.tree;
6365 struct got_tree_entry *next, *last;
6368 if (s->first_displayed_entry)
6369 next = got_tree_entry_get_next(s->tree,
6370 s->first_displayed_entry);
6372 next = got_object_tree_get_first_entry(s->tree);
6374 last = s->last_displayed_entry;
6375 while (next && n++ < maxscroll) {
6377 last = got_tree_entry_get_next(s->tree, last);
6378 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6379 s->first_displayed_entry = next;
6380 next = got_tree_entry_get_next(s->tree, next);
6387 static const struct got_error *
6388 tree_entry_path(char **path, struct tog_parent_trees *parents,
6389 struct got_tree_entry *te)
6391 const struct got_error *err = NULL;
6392 struct tog_parent_tree *pt;
6393 size_t len = 2; /* for leading slash and NUL */
6395 TAILQ_FOREACH(pt, parents, entry)
6396 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6399 len += strlen(got_tree_entry_get_name(te));
6401 *path = calloc(1, len);
6403 return got_error_from_errno("calloc");
6406 pt = TAILQ_LAST(parents, tog_parent_trees);
6408 const char *name = got_tree_entry_get_name(pt->selected_entry);
6409 if (strlcat(*path, name, len) >= len) {
6410 err = got_error(GOT_ERR_NO_SPACE);
6413 if (strlcat(*path, "/", len) >= len) {
6414 err = got_error(GOT_ERR_NO_SPACE);
6417 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6420 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6421 err = got_error(GOT_ERR_NO_SPACE);
6433 static const struct got_error *
6434 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6435 struct got_tree_entry *te, struct tog_parent_trees *parents,
6436 struct got_object_id *commit_id, struct got_repository *repo)
6438 const struct got_error *err = NULL;
6440 struct tog_view *blame_view;
6444 err = tree_entry_path(&path, parents, te);
6448 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6449 if (blame_view == NULL) {
6450 err = got_error_from_errno("view_open");
6454 err = open_blame_view(blame_view, path, commit_id, repo);
6456 if (err->code == GOT_ERR_CANCELLED)
6458 view_close(blame_view);
6460 *new_view = blame_view;
6466 static const struct got_error *
6467 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6468 struct tog_tree_view_state *s)
6470 struct tog_view *log_view;
6471 const struct got_error *err = NULL;
6476 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6477 if (log_view == NULL)
6478 return got_error_from_errno("view_open");
6480 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6484 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6487 view_close(log_view);
6489 *new_view = log_view;
6494 static const struct got_error *
6495 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6496 const char *head_ref_name, struct got_repository *repo)
6498 const struct got_error *err = NULL;
6499 char *commit_id_str = NULL;
6500 struct tog_tree_view_state *s = &view->state.tree;
6501 struct got_commit_object *commit = NULL;
6503 TAILQ_INIT(&s->parents);
6504 STAILQ_INIT(&s->colors);
6506 s->commit_id = got_object_id_dup(commit_id);
6507 if (s->commit_id == NULL)
6508 return got_error_from_errno("got_object_id_dup");
6510 err = got_object_open_as_commit(&commit, repo, commit_id);
6515 * The root is opened here and will be closed when the view is closed.
6516 * Any visited subtrees and their path-wise parents are opened and
6519 err = got_object_open_as_tree(&s->root, repo,
6520 got_object_commit_get_tree_id(commit));
6525 err = got_object_id_str(&commit_id_str, commit_id);
6529 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6530 err = got_error_from_errno("asprintf");
6534 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6535 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6536 if (head_ref_name) {
6537 s->head_ref_name = strdup(head_ref_name);
6538 if (s->head_ref_name == NULL) {
6539 err = got_error_from_errno("strdup");
6545 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6546 err = add_color(&s->colors, "\\$$",
6547 TOG_COLOR_TREE_SUBMODULE,
6548 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6551 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6552 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6555 err = add_color(&s->colors, "/$",
6556 TOG_COLOR_TREE_DIRECTORY,
6557 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6561 err = add_color(&s->colors, "\\*$",
6562 TOG_COLOR_TREE_EXECUTABLE,
6563 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6567 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6568 get_color_value("TOG_COLOR_COMMIT"));
6573 view->show = show_tree_view;
6574 view->input = input_tree_view;
6575 view->close = close_tree_view;
6576 view->search_start = search_start_tree_view;
6577 view->search_next = search_next_tree_view;
6579 free(commit_id_str);
6581 got_object_commit_close(commit);
6583 close_tree_view(view);
6587 static const struct got_error *
6588 close_tree_view(struct tog_view *view)
6590 struct tog_tree_view_state *s = &view->state.tree;
6592 free_colors(&s->colors);
6593 free(s->tree_label);
6594 s->tree_label = NULL;
6596 s->commit_id = NULL;
6597 free(s->head_ref_name);
6598 s->head_ref_name = NULL;
6599 while (!TAILQ_EMPTY(&s->parents)) {
6600 struct tog_parent_tree *parent;
6601 parent = TAILQ_FIRST(&s->parents);
6602 TAILQ_REMOVE(&s->parents, parent, entry);
6603 if (parent->tree != s->root)
6604 got_object_tree_close(parent->tree);
6608 if (s->tree != NULL && s->tree != s->root)
6609 got_object_tree_close(s->tree);
6611 got_object_tree_close(s->root);
6615 static const struct got_error *
6616 search_start_tree_view(struct tog_view *view)
6618 struct tog_tree_view_state *s = &view->state.tree;
6620 s->matched_entry = NULL;
6625 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6627 regmatch_t regmatch;
6629 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6633 static const struct got_error *
6634 search_next_tree_view(struct tog_view *view)
6636 struct tog_tree_view_state *s = &view->state.tree;
6637 struct got_tree_entry *te = NULL;
6639 if (!view->searching) {
6640 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6644 if (s->matched_entry) {
6645 if (view->searching == TOG_SEARCH_FORWARD) {
6646 if (s->selected_entry)
6647 te = got_tree_entry_get_next(s->tree,
6650 te = got_object_tree_get_first_entry(s->tree);
6652 if (s->selected_entry == NULL)
6653 te = got_object_tree_get_last_entry(s->tree);
6655 te = got_tree_entry_get_prev(s->tree,
6659 if (s->selected_entry)
6660 te = s->selected_entry;
6661 else if (view->searching == TOG_SEARCH_FORWARD)
6662 te = got_object_tree_get_first_entry(s->tree);
6664 te = got_object_tree_get_last_entry(s->tree);
6669 if (s->matched_entry == NULL) {
6670 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6673 if (view->searching == TOG_SEARCH_FORWARD)
6674 te = got_object_tree_get_first_entry(s->tree);
6676 te = got_object_tree_get_last_entry(s->tree);
6679 if (match_tree_entry(te, &view->regex)) {
6680 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6681 s->matched_entry = te;
6685 if (view->searching == TOG_SEARCH_FORWARD)
6686 te = got_tree_entry_get_next(s->tree, te);
6688 te = got_tree_entry_get_prev(s->tree, te);
6691 if (s->matched_entry) {
6692 s->first_displayed_entry = s->matched_entry;
6699 static const struct got_error *
6700 show_tree_view(struct tog_view *view)
6702 const struct got_error *err = NULL;
6703 struct tog_tree_view_state *s = &view->state.tree;
6706 err = tree_entry_path(&parent_path, &s->parents, NULL);
6710 err = draw_tree_entries(view, parent_path);
6717 static const struct got_error *
6718 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6720 const struct got_error *err = NULL;
6721 struct tog_tree_view_state *s = &view->state.tree;
6722 struct tog_view *log_view, *ref_view;
6723 struct got_tree_entry *te;
6724 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6728 s->show_ids = !s->show_ids;
6733 if (!s->selected_entry)
6735 if (view_is_parent_view(view))
6736 view_get_split(view, &begin_y, &begin_x);
6737 err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6738 if (view_is_parent_view(view) &&
6739 view->mode == TOG_VIEW_SPLIT_HRZN) {
6740 err = view_init_hsplit(view, begin_y);
6745 log_view->focussed = 1;
6746 log_view->mode = view->mode;
6747 log_view->nlines = view->lines - begin_y;
6748 if (view_is_parent_view(view)) {
6749 view_transfer_size(log_view, view);
6750 err = view_close_child(view);
6753 err = view_set_child(view, log_view);
6756 view->focus_child = 1;
6758 *new_view = log_view;
6762 if (view_is_parent_view(view))
6763 view_get_split(view, &begin_y, &begin_x);
6764 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6765 if (ref_view == NULL)
6766 return got_error_from_errno("view_open");
6767 err = open_ref_view(ref_view, s->repo);
6769 view_close(ref_view);
6772 if (view_is_parent_view(view) &&
6773 view->mode == TOG_VIEW_SPLIT_HRZN) {
6774 err = view_init_hsplit(view, begin_y);
6779 ref_view->focussed = 1;
6780 ref_view->mode = view->mode;
6781 ref_view->nlines = view->lines - begin_y;
6782 if (view_is_parent_view(view)) {
6783 view_transfer_size(ref_view, view);
6784 err = view_close_child(view);
6787 err = view_set_child(view, ref_view);
6790 view->focus_child = 1;
6792 *new_view = ref_view;
6798 if (s->tree == s->root)
6799 s->first_displayed_entry =
6800 got_object_tree_get_first_entry(s->tree);
6802 s->first_displayed_entry = NULL;
6806 int eos = view->nlines - 3;
6808 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6812 te = got_object_tree_get_last_entry(s->tree);
6813 for (n = 0; n < eos; n++) {
6815 if (s->tree != s->root) {
6816 s->first_displayed_entry = NULL;
6821 s->first_displayed_entry = te;
6822 te = got_tree_entry_get_prev(s->tree, te);
6825 s->selected = n - 1;
6831 if (s->selected > 0) {
6835 tree_scroll_up(s, 1);
6836 if (s->selected_entry == NULL ||
6837 (s->tree == s->root && s->selected_entry ==
6838 got_object_tree_get_first_entry(s->tree)))
6848 if (s->tree == s->root) {
6849 if (got_object_tree_get_first_entry(s->tree) ==
6850 s->first_displayed_entry)
6851 s->selected -= MIN(s->selected, nscroll);
6853 if (s->first_displayed_entry == NULL)
6854 s->selected -= MIN(s->selected, nscroll);
6856 tree_scroll_up(s, MAX(0, nscroll));
6857 if (s->selected_entry == NULL ||
6858 (s->tree == s->root && s->selected_entry ==
6859 got_object_tree_get_first_entry(s->tree)))
6865 if (s->selected < s->ndisplayed - 1) {
6869 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6871 /* can't scroll any further */
6875 tree_scroll_down(view, 1);
6885 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6887 /* can't scroll any further; move cursor down */
6888 if (s->selected < s->ndisplayed - 1)
6889 s->selected += MIN(nscroll,
6890 s->ndisplayed - s->selected - 1);
6895 tree_scroll_down(view, nscroll);
6900 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6901 struct tog_parent_tree *parent;
6902 /* user selected '..' */
6903 if (s->tree == s->root) {
6907 parent = TAILQ_FIRST(&s->parents);
6908 TAILQ_REMOVE(&s->parents, parent,
6910 got_object_tree_close(s->tree);
6911 s->tree = parent->tree;
6912 s->first_displayed_entry =
6913 parent->first_displayed_entry;
6915 parent->selected_entry;
6916 s->selected = parent->selected;
6917 if (s->selected > view->nlines - 3) {
6918 err = offset_selection_down(view);
6923 } else if (S_ISDIR(got_tree_entry_get_mode(
6924 s->selected_entry))) {
6925 struct got_tree_object *subtree;
6927 err = got_object_open_as_tree(&subtree, s->repo,
6928 got_tree_entry_get_id(s->selected_entry));
6931 err = tree_view_visit_subtree(s, subtree);
6933 got_object_tree_close(subtree);
6936 } else if (S_ISREG(got_tree_entry_get_mode(
6937 s->selected_entry))) {
6938 struct tog_view *blame_view;
6939 int begin_x = 0, begin_y = 0;
6941 if (view_is_parent_view(view))
6942 view_get_split(view, &begin_y, &begin_x);
6944 err = blame_tree_entry(&blame_view, begin_y, begin_x,
6945 s->selected_entry, &s->parents,
6946 s->commit_id, s->repo);
6950 if (view_is_parent_view(view) &&
6951 view->mode == TOG_VIEW_SPLIT_HRZN) {
6952 err = view_init_hsplit(view, begin_y);
6959 blame_view->focussed = 1;
6960 blame_view->mode = view->mode;
6961 blame_view->nlines = view->lines - begin_y;
6962 if (view_is_parent_view(view)) {
6963 view_transfer_size(blame_view, view);
6964 err = view_close_child(view);
6967 err = view_set_child(view, blame_view);
6970 view->focus_child = 1;
6972 *new_view = blame_view;
6976 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6977 s->selected = view->nlines - 4;
6993 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6998 static const struct got_error *
6999 cmd_tree(int argc, char *argv[])
7001 const struct got_error *error;
7002 struct got_repository *repo = NULL;
7003 struct got_worktree *worktree = NULL;
7004 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7005 struct got_object_id *commit_id = NULL;
7006 struct got_commit_object *commit = NULL;
7007 const char *commit_id_arg = NULL;
7009 struct got_reference *ref = NULL;
7010 const char *head_ref_name = NULL;
7012 struct tog_view *view;
7013 int *pack_fds = NULL;
7015 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7018 commit_id_arg = optarg;
7021 repo_path = realpath(optarg, NULL);
7022 if (repo_path == NULL)
7023 return got_error_from_errno2("realpath",
7038 error = got_repo_pack_fds_open(&pack_fds);
7042 if (repo_path == NULL) {
7043 cwd = getcwd(NULL, 0);
7045 return got_error_from_errno("getcwd");
7046 error = got_worktree_open(&worktree, cwd);
7047 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7051 strdup(got_worktree_get_repo_path(worktree));
7053 repo_path = strdup(cwd);
7054 if (repo_path == NULL) {
7055 error = got_error_from_errno("strdup");
7060 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7064 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7071 error = apply_unveil(got_repo_get_path(repo), NULL);
7075 error = tog_load_refs(repo, 0);
7079 if (commit_id_arg == NULL) {
7080 error = got_repo_match_object_id(&commit_id, &label,
7081 worktree ? got_worktree_get_head_ref_name(worktree) :
7082 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7085 head_ref_name = label;
7087 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7089 head_ref_name = got_ref_get_name(ref);
7090 else if (error->code != GOT_ERR_NOT_REF)
7092 error = got_repo_match_object_id(&commit_id, NULL,
7093 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7098 error = got_object_open_as_commit(&commit, repo, commit_id);
7102 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7104 error = got_error_from_errno("view_open");
7107 error = open_tree_view(view, commit_id, head_ref_name, repo);
7110 if (!got_path_is_root_dir(in_repo_path)) {
7111 error = tree_view_walk_path(&view->state.tree, commit,
7118 /* Release work tree lock. */
7119 got_worktree_close(worktree);
7122 error = view_loop(view);
7131 const struct got_error *close_err = got_repo_close(repo);
7136 const struct got_error *pack_err =
7137 got_repo_pack_fds_close(pack_fds);
7145 static const struct got_error *
7146 ref_view_load_refs(struct tog_ref_view_state *s)
7148 struct got_reflist_entry *sre;
7149 struct tog_reflist_entry *re;
7152 TAILQ_FOREACH(sre, &tog_refs, entry) {
7153 if (strncmp(got_ref_get_name(sre->ref),
7154 "refs/got/", 9) == 0 &&
7155 strncmp(got_ref_get_name(sre->ref),
7156 "refs/got/backup/", 16) != 0)
7159 re = malloc(sizeof(*re));
7161 return got_error_from_errno("malloc");
7163 re->ref = got_ref_dup(sre->ref);
7164 if (re->ref == NULL)
7165 return got_error_from_errno("got_ref_dup");
7166 re->idx = s->nrefs++;
7167 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7170 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7175 ref_view_free_refs(struct tog_ref_view_state *s)
7177 struct tog_reflist_entry *re;
7179 while (!TAILQ_EMPTY(&s->refs)) {
7180 re = TAILQ_FIRST(&s->refs);
7181 TAILQ_REMOVE(&s->refs, re, entry);
7182 got_ref_close(re->ref);
7187 static const struct got_error *
7188 open_ref_view(struct tog_view *view, struct got_repository *repo)
7190 const struct got_error *err = NULL;
7191 struct tog_ref_view_state *s = &view->state.ref;
7193 s->selected_entry = 0;
7196 TAILQ_INIT(&s->refs);
7197 STAILQ_INIT(&s->colors);
7199 err = ref_view_load_refs(s);
7203 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7204 err = add_color(&s->colors, "^refs/heads/",
7205 TOG_COLOR_REFS_HEADS,
7206 get_color_value("TOG_COLOR_REFS_HEADS"));
7210 err = add_color(&s->colors, "^refs/tags/",
7211 TOG_COLOR_REFS_TAGS,
7212 get_color_value("TOG_COLOR_REFS_TAGS"));
7216 err = add_color(&s->colors, "^refs/remotes/",
7217 TOG_COLOR_REFS_REMOTES,
7218 get_color_value("TOG_COLOR_REFS_REMOTES"));
7222 err = add_color(&s->colors, "^refs/got/backup/",
7223 TOG_COLOR_REFS_BACKUP,
7224 get_color_value("TOG_COLOR_REFS_BACKUP"));
7229 view->show = show_ref_view;
7230 view->input = input_ref_view;
7231 view->close = close_ref_view;
7232 view->search_start = search_start_ref_view;
7233 view->search_next = search_next_ref_view;
7236 free_colors(&s->colors);
7240 static const struct got_error *
7241 close_ref_view(struct tog_view *view)
7243 struct tog_ref_view_state *s = &view->state.ref;
7245 ref_view_free_refs(s);
7246 free_colors(&s->colors);
7251 static const struct got_error *
7252 resolve_reflist_entry(struct got_object_id **commit_id,
7253 struct tog_reflist_entry *re, struct got_repository *repo)
7255 const struct got_error *err = NULL;
7256 struct got_object_id *obj_id;
7257 struct got_tag_object *tag = NULL;
7262 err = got_ref_resolve(&obj_id, repo, re->ref);
7266 err = got_object_get_type(&obj_type, repo, obj_id);
7271 case GOT_OBJ_TYPE_COMMIT:
7272 *commit_id = obj_id;
7274 case GOT_OBJ_TYPE_TAG:
7275 err = got_object_open_as_tag(&tag, repo, obj_id);
7279 err = got_object_get_type(&obj_type, repo,
7280 got_object_tag_get_object_id(tag));
7283 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7284 err = got_error(GOT_ERR_OBJ_TYPE);
7287 *commit_id = got_object_id_dup(
7288 got_object_tag_get_object_id(tag));
7289 if (*commit_id == NULL) {
7290 err = got_error_from_errno("got_object_id_dup");
7295 err = got_error(GOT_ERR_OBJ_TYPE);
7301 got_object_tag_close(tag);
7309 static const struct got_error *
7310 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7311 struct tog_reflist_entry *re, struct got_repository *repo)
7313 struct tog_view *log_view;
7314 const struct got_error *err = NULL;
7315 struct got_object_id *commit_id = NULL;
7319 err = resolve_reflist_entry(&commit_id, re, repo);
7321 if (err->code != GOT_ERR_OBJ_TYPE)
7327 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7328 if (log_view == NULL) {
7329 err = got_error_from_errno("view_open");
7333 err = open_log_view(log_view, commit_id, repo,
7334 got_ref_get_name(re->ref), "", 0);
7337 view_close(log_view);
7339 *new_view = log_view;
7345 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7347 struct tog_reflist_entry *re;
7350 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7353 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7354 while (i++ < maxscroll) {
7357 s->first_displayed_entry = re;
7358 re = TAILQ_PREV(re, tog_reflist_head, entry);
7362 static const struct got_error *
7363 ref_scroll_down(struct tog_view *view, int maxscroll)
7365 struct tog_ref_view_state *s = &view->state.ref;
7366 struct tog_reflist_entry *next, *last;
7369 if (s->first_displayed_entry)
7370 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7372 next = TAILQ_FIRST(&s->refs);
7374 last = s->last_displayed_entry;
7375 while (next && n++ < maxscroll) {
7377 last = TAILQ_NEXT(last, entry);
7378 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7379 s->first_displayed_entry = next;
7380 next = TAILQ_NEXT(next, entry);
7387 static const struct got_error *
7388 search_start_ref_view(struct tog_view *view)
7390 struct tog_ref_view_state *s = &view->state.ref;
7392 s->matched_entry = NULL;
7397 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7399 regmatch_t regmatch;
7401 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7405 static const struct got_error *
7406 search_next_ref_view(struct tog_view *view)
7408 struct tog_ref_view_state *s = &view->state.ref;
7409 struct tog_reflist_entry *re = NULL;
7411 if (!view->searching) {
7412 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7416 if (s->matched_entry) {
7417 if (view->searching == TOG_SEARCH_FORWARD) {
7418 if (s->selected_entry)
7419 re = TAILQ_NEXT(s->selected_entry, entry);
7421 re = TAILQ_PREV(s->selected_entry,
7422 tog_reflist_head, entry);
7424 if (s->selected_entry == NULL)
7425 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7427 re = TAILQ_PREV(s->selected_entry,
7428 tog_reflist_head, entry);
7431 if (s->selected_entry)
7432 re = s->selected_entry;
7433 else if (view->searching == TOG_SEARCH_FORWARD)
7434 re = TAILQ_FIRST(&s->refs);
7436 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7441 if (s->matched_entry == NULL) {
7442 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7445 if (view->searching == TOG_SEARCH_FORWARD)
7446 re = TAILQ_FIRST(&s->refs);
7448 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7451 if (match_reflist_entry(re, &view->regex)) {
7452 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7453 s->matched_entry = re;
7457 if (view->searching == TOG_SEARCH_FORWARD)
7458 re = TAILQ_NEXT(re, entry);
7460 re = TAILQ_PREV(re, tog_reflist_head, entry);
7463 if (s->matched_entry) {
7464 s->first_displayed_entry = s->matched_entry;
7471 static const struct got_error *
7472 show_ref_view(struct tog_view *view)
7474 const struct got_error *err = NULL;
7475 struct tog_ref_view_state *s = &view->state.ref;
7476 struct tog_reflist_entry *re;
7479 struct tog_color *tc;
7481 int limit = view->nlines;
7483 werase(view->window);
7486 if (view_is_hsplit_top(view))
7487 --limit; /* border */
7492 re = s->first_displayed_entry;
7494 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7496 return got_error_from_errno("asprintf");
7498 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7503 if (view_needs_focus_indication(view))
7504 wstandout(view->window);
7505 waddwstr(view->window, wline);
7506 if (view_needs_focus_indication(view))
7507 wstandend(view->window);
7512 if (width < view->ncols - 1)
7513 waddch(view->window, '\n');
7518 while (re && limit > 0) {
7520 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7523 struct got_commit_object *ci;
7524 struct got_tag_object *tag;
7525 struct got_object_id *id;
7529 err = got_ref_resolve(&id, s->repo, re->ref);
7532 err = got_object_open_as_tag(&tag, s->repo, id);
7534 if (err->code != GOT_ERR_OBJ_TYPE) {
7538 err = got_object_open_as_commit(&ci, s->repo,
7544 t = got_object_commit_get_committer_time(ci);
7545 got_object_commit_close(ci);
7547 t = got_object_tag_get_tagger_time(tag);
7548 got_object_tag_close(tag);
7551 if (gmtime_r(&t, &tm) == NULL)
7552 return got_error_from_errno("gmtime_r");
7553 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7554 return got_error(GOT_ERR_NO_SPACE);
7556 if (got_ref_is_symbolic(re->ref)) {
7557 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7558 ymd : "", got_ref_get_name(re->ref),
7559 got_ref_get_symref_target(re->ref)) == -1)
7560 return got_error_from_errno("asprintf");
7561 } else if (s->show_ids) {
7562 struct got_object_id *id;
7564 err = got_ref_resolve(&id, s->repo, re->ref);
7567 err = got_object_id_str(&id_str, id);
7572 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7573 got_ref_get_name(re->ref), id_str) == -1) {
7574 err = got_error_from_errno("asprintf");
7581 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7582 got_ref_get_name(re->ref)) == -1)
7583 return got_error_from_errno("asprintf");
7585 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7591 if (n == s->selected) {
7593 wstandout(view->window);
7594 s->selected_entry = re;
7596 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7598 wattr_on(view->window,
7599 COLOR_PAIR(tc->colorpair), NULL);
7600 waddwstr(view->window, wline);
7602 wattr_off(view->window,
7603 COLOR_PAIR(tc->colorpair), NULL);
7604 if (width < view->ncols - 1)
7605 waddch(view->window, '\n');
7606 if (n == s->selected && view->focussed)
7607 wstandend(view->window);
7613 s->last_displayed_entry = re;
7616 re = TAILQ_NEXT(re, entry);
7623 static const struct got_error *
7624 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7625 struct tog_reflist_entry *re, struct got_repository *repo)
7627 const struct got_error *err = NULL;
7628 struct got_object_id *commit_id = NULL;
7629 struct tog_view *tree_view;
7633 err = resolve_reflist_entry(&commit_id, re, repo);
7635 if (err->code != GOT_ERR_OBJ_TYPE)
7642 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7643 if (tree_view == NULL) {
7644 err = got_error_from_errno("view_open");
7648 err = open_tree_view(tree_view, commit_id,
7649 got_ref_get_name(re->ref), repo);
7653 *new_view = tree_view;
7658 static const struct got_error *
7659 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7661 const struct got_error *err = NULL;
7662 struct tog_ref_view_state *s = &view->state.ref;
7663 struct tog_view *log_view, *tree_view;
7664 struct tog_reflist_entry *re;
7665 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7669 s->show_ids = !s->show_ids;
7673 s->show_date = !s->show_date;
7677 s->sort_by_date = !s->sort_by_date;
7679 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7680 got_ref_cmp_by_commit_timestamp_descending :
7681 tog_ref_cmp_by_name, s->repo);
7684 got_reflist_object_id_map_free(tog_refs_idmap);
7685 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7686 &tog_refs, s->repo);
7689 ref_view_free_refs(s);
7690 err = ref_view_load_refs(s);
7695 if (!s->selected_entry)
7697 if (view_is_parent_view(view))
7698 view_get_split(view, &begin_y, &begin_x);
7700 err = log_ref_entry(&log_view, begin_y, begin_x,
7701 s->selected_entry, s->repo);
7705 if (view_is_parent_view(view) &&
7706 view->mode == TOG_VIEW_SPLIT_HRZN) {
7707 err = view_init_hsplit(view, begin_y);
7713 log_view->focussed = 1;
7714 log_view->mode = view->mode;
7715 log_view->nlines = view->lines - begin_y;
7716 if (view_is_parent_view(view)) {
7717 view_transfer_size(log_view, view);
7718 err = view_close_child(view);
7721 err = view_set_child(view, log_view);
7724 view->focus_child = 1;
7726 *new_view = log_view;
7730 if (!s->selected_entry)
7732 if (view_is_parent_view(view))
7733 view_get_split(view, &begin_y, &begin_x);
7734 err = browse_ref_tree(&tree_view, begin_y, begin_x,
7735 s->selected_entry, s->repo);
7736 if (err || tree_view == NULL)
7738 if (view_is_parent_view(view) &&
7739 view->mode == TOG_VIEW_SPLIT_HRZN) {
7740 err = view_init_hsplit(view, begin_y);
7745 tree_view->focussed = 1;
7746 tree_view->mode = view->mode;
7747 tree_view->nlines = view->lines - begin_y;
7748 if (view_is_parent_view(view)) {
7749 view_transfer_size(tree_view, view);
7750 err = view_close_child(view);
7753 err = view_set_child(view, tree_view);
7756 view->focus_child = 1;
7758 *new_view = tree_view;
7764 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7768 int eos = view->nlines - 1;
7770 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7774 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7775 for (n = 0; n < eos; n++) {
7778 s->first_displayed_entry = re;
7779 re = TAILQ_PREV(re, tog_reflist_head, entry);
7782 s->selected = n - 1;
7788 if (s->selected > 0) {
7792 ref_scroll_up(s, 1);
7793 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7803 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7804 s->selected -= MIN(nscroll, s->selected);
7805 ref_scroll_up(s, MAX(0, nscroll));
7806 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7812 if (s->selected < s->ndisplayed - 1) {
7816 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7817 /* can't scroll any further */
7821 ref_scroll_down(view, 1);
7831 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7832 /* can't scroll any further; move cursor down */
7833 if (s->selected < s->ndisplayed - 1)
7834 s->selected += MIN(nscroll,
7835 s->ndisplayed - s->selected - 1);
7836 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7837 s->selected += s->ndisplayed - s->selected - 1;
7841 ref_scroll_down(view, nscroll);
7846 err = tog_load_refs(s->repo, s->sort_by_date);
7849 ref_view_free_refs(s);
7850 err = ref_view_load_refs(s);
7853 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7854 s->selected = view->nlines - 2;
7868 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7873 static const struct got_error *
7874 cmd_ref(int argc, char *argv[])
7876 const struct got_error *error;
7877 struct got_repository *repo = NULL;
7878 struct got_worktree *worktree = NULL;
7879 char *cwd = NULL, *repo_path = NULL;
7881 struct tog_view *view;
7882 int *pack_fds = NULL;
7884 while ((ch = getopt(argc, argv, "r:")) != -1) {
7887 repo_path = realpath(optarg, NULL);
7888 if (repo_path == NULL)
7889 return got_error_from_errno2("realpath",
7904 error = got_repo_pack_fds_open(&pack_fds);
7908 if (repo_path == NULL) {
7909 cwd = getcwd(NULL, 0);
7911 return got_error_from_errno("getcwd");
7912 error = got_worktree_open(&worktree, cwd);
7913 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7917 strdup(got_worktree_get_repo_path(worktree));
7919 repo_path = strdup(cwd);
7920 if (repo_path == NULL) {
7921 error = got_error_from_errno("strdup");
7926 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7932 error = apply_unveil(got_repo_get_path(repo), NULL);
7936 error = tog_load_refs(repo, 0);
7940 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7942 error = got_error_from_errno("view_open");
7946 error = open_ref_view(view, repo);
7951 /* Release work tree lock. */
7952 got_worktree_close(worktree);
7955 error = view_loop(view);
7960 const struct got_error *close_err = got_repo_close(repo);
7965 const struct got_error *pack_err =
7966 got_repo_pack_fds_close(pack_fds);
7975 * If view was scrolled down to move the selected line into view when opening a
7976 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7979 offset_selection_up(struct tog_view *view)
7981 switch (view->type) {
7982 case TOG_VIEW_BLAME: {
7983 struct tog_blame_view_state *s = &view->state.blame;
7984 if (s->first_displayed_line == 1) {
7985 s->selected_line = MAX(s->selected_line - view->offset,
7989 if (s->first_displayed_line > view->offset)
7990 s->first_displayed_line -= view->offset;
7992 s->first_displayed_line = 1;
7993 s->selected_line += view->offset;
7997 log_scroll_up(&view->state.log, view->offset);
7998 view->state.log.selected += view->offset;
8001 ref_scroll_up(&view->state.ref, view->offset);
8002 view->state.ref.selected += view->offset;
8005 tree_scroll_up(&view->state.tree, view->offset);
8006 view->state.tree.selected += view->offset;
8016 * If the selected line is in the section of screen covered by the bottom split,
8017 * scroll down offset lines to move it into view and index its new position.
8019 static const struct got_error *
8020 offset_selection_down(struct tog_view *view)
8022 const struct got_error *err = NULL;
8023 const struct got_error *(*scrolld)(struct tog_view *, int);
8024 int *selected = NULL;
8027 switch (view->type) {
8028 case TOG_VIEW_BLAME: {
8029 struct tog_blame_view_state *s = &view->state.blame;
8032 if (s->selected_line > view->nlines - header) {
8033 offset = abs(view->nlines - s->selected_line - header);
8034 s->first_displayed_line += offset;
8035 s->selected_line -= offset;
8036 view->offset = offset;
8040 case TOG_VIEW_LOG: {
8041 struct tog_log_view_state *s = &view->state.log;
8042 scrolld = &log_scroll_down;
8043 header = view_is_parent_view(view) ? 3 : 2;
8044 selected = &s->selected;
8047 case TOG_VIEW_REF: {
8048 struct tog_ref_view_state *s = &view->state.ref;
8049 scrolld = &ref_scroll_down;
8051 selected = &s->selected;
8054 case TOG_VIEW_TREE: {
8055 struct tog_tree_view_state *s = &view->state.tree;
8056 scrolld = &tree_scroll_down;
8058 selected = &s->selected;
8068 if (selected && *selected > view->nlines - header) {
8069 offset = abs(view->nlines - *selected - header);
8070 view->offset = offset;
8071 if (scrolld && offset) {
8072 err = scrolld(view, offset);
8073 *selected -= offset;
8081 list_commands(FILE *fp)
8085 fprintf(fp, "commands:");
8086 for (i = 0; i < nitems(tog_commands); i++) {
8087 const struct tog_cmd *cmd = &tog_commands[i];
8088 fprintf(fp, " %s", cmd->name);
8094 usage(int hflag, int status)
8096 FILE *fp = (status == 0) ? stdout : stderr;
8098 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8101 fprintf(fp, "lazy usage: %s path\n", getprogname());
8108 make_argv(int argc, ...)
8116 argv = calloc(argc, sizeof(char *));
8119 for (i = 0; i < argc; i++) {
8120 argv[i] = strdup(va_arg(ap, char *));
8121 if (argv[i] == NULL)
8130 * Try to convert 'tog path' into a 'tog log path' command.
8131 * The user could simply have mistyped the command rather than knowingly
8132 * provided a path. So check whether argv[0] can in fact be resolved
8133 * to a path in the HEAD commit and print a special error if not.
8134 * This hack is for mpi@ <3
8136 static const struct got_error *
8137 tog_log_with_path(int argc, char *argv[])
8139 const struct got_error *error = NULL, *close_err;
8140 const struct tog_cmd *cmd = NULL;
8141 struct got_repository *repo = NULL;
8142 struct got_worktree *worktree = NULL;
8143 struct got_object_id *commit_id = NULL, *id = NULL;
8144 struct got_commit_object *commit = NULL;
8145 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8146 char *commit_id_str = NULL, **cmd_argv = NULL;
8147 int *pack_fds = NULL;
8149 cwd = getcwd(NULL, 0);
8151 return got_error_from_errno("getcwd");
8153 error = got_repo_pack_fds_open(&pack_fds);
8157 error = got_worktree_open(&worktree, cwd);
8158 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8162 repo_path = strdup(got_worktree_get_repo_path(worktree));
8164 repo_path = strdup(cwd);
8165 if (repo_path == NULL) {
8166 error = got_error_from_errno("strdup");
8170 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8174 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8179 error = tog_load_refs(repo, 0);
8182 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8183 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8184 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8189 got_worktree_close(worktree);
8193 error = got_object_open_as_commit(&commit, repo, commit_id);
8197 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8199 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8201 fprintf(stderr, "%s: '%s' is no known command or path\n",
8202 getprogname(), argv[0]);
8207 close_err = got_repo_close(repo);
8212 error = got_object_id_str(&commit_id_str, commit_id);
8216 cmd = &tog_commands[0]; /* log */
8218 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8219 error = cmd->cmd_main(argc, cmd_argv);
8222 close_err = got_repo_close(repo);
8227 got_object_commit_close(commit);
8229 got_worktree_close(worktree);
8231 const struct got_error *pack_err =
8232 got_repo_pack_fds_close(pack_fds);
8237 free(commit_id_str);
8244 for (i = 0; i < argc; i++)
8253 main(int argc, char *argv[])
8255 const struct got_error *error = NULL;
8256 const struct tog_cmd *cmd = NULL;
8257 int ch, hflag = 0, Vflag = 0;
8258 char **cmd_argv = NULL;
8259 static const struct option longopts[] = {
8260 { "version", no_argument, NULL, 'V' },
8263 char *diff_algo_str = NULL;
8265 setlocale(LC_CTYPE, "");
8267 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8287 got_version_print_str();
8292 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8300 /* Build an argument vector which runs a default command. */
8301 cmd = &tog_commands[0];
8303 cmd_argv = make_argv(argc, cmd->name);
8307 /* Did the user specify a command? */
8308 for (i = 0; i < nitems(tog_commands); i++) {
8309 if (strncmp(tog_commands[i].name, argv[0],
8310 strlen(argv[0])) == 0) {
8311 cmd = &tog_commands[i];
8317 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8318 if (diff_algo_str) {
8319 if (strcasecmp(diff_algo_str, "patience") == 0)
8320 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8321 if (strcasecmp(diff_algo_str, "myers") == 0)
8322 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8328 /* No command specified; try log with a path */
8329 error = tog_log_with_path(argc, argv);
8334 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8341 for (i = 0; i < argc; i++)
8346 if (error && error->code != GOT_ERR_CANCELLED)
8347 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);