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;
140 static const struct got_error *
141 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
142 struct got_reference* re2)
144 const char *name1 = got_ref_get_name(re1);
145 const char *name2 = got_ref_get_name(re2);
146 int isbackup1, isbackup2;
148 /* Sort backup refs towards the bottom of the list. */
149 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
150 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
151 if (!isbackup1 && isbackup2) {
154 } else if (isbackup1 && !isbackup2) {
159 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
163 static const struct got_error *
164 tog_load_refs(struct got_repository *repo, int sort_by_date)
166 const struct got_error *err;
168 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
169 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
174 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
181 if (tog_refs_idmap) {
182 got_reflist_object_id_map_free(tog_refs_idmap);
183 tog_refs_idmap = NULL;
185 got_ref_list_free(&tog_refs);
188 static const struct got_error *
189 add_color(struct tog_colors *colors, const char *pattern,
190 int idx, short color)
192 const struct got_error *err = NULL;
193 struct tog_color *tc;
196 if (idx < 1 || idx > COLOR_PAIRS - 1)
199 init_pair(idx, color, -1);
201 tc = calloc(1, sizeof(*tc));
203 return got_error_from_errno("calloc");
204 regerr = regcomp(&tc->regex, pattern,
205 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
207 static char regerr_msg[512];
208 static char err_msg[512];
209 regerror(regerr, &tc->regex, regerr_msg,
211 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
213 err = got_error_msg(GOT_ERR_REGEX, err_msg);
218 STAILQ_INSERT_HEAD(colors, tc, entry);
223 free_colors(struct tog_colors *colors)
225 struct tog_color *tc;
227 while (!STAILQ_EMPTY(colors)) {
228 tc = STAILQ_FIRST(colors);
229 STAILQ_REMOVE_HEAD(colors, entry);
235 static struct tog_color *
236 get_color(struct tog_colors *colors, int colorpair)
238 struct tog_color *tc = NULL;
240 STAILQ_FOREACH(tc, colors, entry) {
241 if (tc->colorpair == colorpair)
249 default_color_value(const char *envvar)
251 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
252 return COLOR_MAGENTA;
253 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
255 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
257 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
259 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
260 return COLOR_MAGENTA;
261 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
262 return COLOR_MAGENTA;
263 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
265 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
267 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
269 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
271 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
273 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
275 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
276 return COLOR_MAGENTA;
277 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
279 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
286 get_color_value(const char *envvar)
288 const char *val = getenv(envvar);
291 return default_color_value(envvar);
293 if (strcasecmp(val, "black") == 0)
295 if (strcasecmp(val, "red") == 0)
297 if (strcasecmp(val, "green") == 0)
299 if (strcasecmp(val, "yellow") == 0)
301 if (strcasecmp(val, "blue") == 0)
303 if (strcasecmp(val, "magenta") == 0)
304 return COLOR_MAGENTA;
305 if (strcasecmp(val, "cyan") == 0)
307 if (strcasecmp(val, "white") == 0)
309 if (strcasecmp(val, "default") == 0)
312 return default_color_value(envvar);
316 struct tog_diff_view_state {
317 struct got_object_id *id1, *id2;
318 const char *label1, *label2;
321 int first_displayed_line;
322 int last_displayed_line;
325 int ignore_whitespace;
327 struct got_repository *repo;
328 struct tog_colors colors;
334 /* passed from log view; may be NULL */
335 struct tog_view *log_view;
338 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
340 struct tog_log_thread_args {
341 pthread_cond_t need_commits;
342 pthread_cond_t commit_loaded;
345 struct got_commit_graph *graph;
346 struct commit_queue *commits;
347 const char *in_repo_path;
348 struct got_object_id *start_id;
349 struct got_repository *repo;
353 struct commit_queue_entry **first_displayed_entry;
354 struct commit_queue_entry **selected_entry;
356 int *search_next_done;
360 struct tog_log_view_state {
361 struct commit_queue commits;
362 struct commit_queue_entry *first_displayed_entry;
363 struct commit_queue_entry *last_displayed_entry;
364 struct commit_queue_entry *selected_entry;
369 struct got_repository *repo;
370 struct got_object_id *start_id;
373 struct tog_log_thread_args thread_args;
374 struct commit_queue_entry *matched_entry;
375 struct commit_queue_entry *search_entry;
376 struct tog_colors colors;
379 #define TOG_COLOR_DIFF_MINUS 1
380 #define TOG_COLOR_DIFF_PLUS 2
381 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
382 #define TOG_COLOR_DIFF_META 4
383 #define TOG_COLOR_TREE_SUBMODULE 5
384 #define TOG_COLOR_TREE_SYMLINK 6
385 #define TOG_COLOR_TREE_DIRECTORY 7
386 #define TOG_COLOR_TREE_EXECUTABLE 8
387 #define TOG_COLOR_COMMIT 9
388 #define TOG_COLOR_AUTHOR 10
389 #define TOG_COLOR_DATE 11
390 #define TOG_COLOR_REFS_HEADS 12
391 #define TOG_COLOR_REFS_TAGS 13
392 #define TOG_COLOR_REFS_REMOTES 14
393 #define TOG_COLOR_REFS_BACKUP 15
395 struct tog_blame_cb_args {
396 struct tog_blame_line *lines; /* one per line */
399 struct tog_view *view;
400 struct got_object_id *commit_id;
404 struct tog_blame_thread_args {
406 struct got_repository *repo;
407 struct tog_blame_cb_args *cb_args;
409 got_cancel_cb cancel_cb;
416 struct tog_blame_line *lines;
420 struct tog_blame_thread_args thread_args;
421 struct tog_blame_cb_args cb_args;
426 struct tog_blame_view_state {
427 int first_displayed_line;
428 int last_displayed_line;
433 struct got_object_id_queue blamed_commits;
434 struct got_object_qid *blamed_commit;
436 struct got_repository *repo;
437 struct got_object_id *commit_id;
438 struct tog_blame blame;
440 struct tog_colors colors;
443 struct tog_parent_tree {
444 TAILQ_ENTRY(tog_parent_tree) entry;
445 struct got_tree_object *tree;
446 struct got_tree_entry *first_displayed_entry;
447 struct got_tree_entry *selected_entry;
451 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
453 struct tog_tree_view_state {
455 struct got_object_id *commit_id;/* commit which this tree belongs to */
456 struct got_tree_object *root; /* the commit's root tree entry */
457 struct got_tree_object *tree; /* currently displayed (sub-)tree */
458 struct got_tree_entry *first_displayed_entry;
459 struct got_tree_entry *last_displayed_entry;
460 struct got_tree_entry *selected_entry;
461 int ndisplayed, selected, show_ids;
462 struct tog_parent_trees parents; /* parent trees of current sub-tree */
464 struct got_repository *repo;
465 struct got_tree_entry *matched_entry;
466 struct tog_colors colors;
469 struct tog_reflist_entry {
470 TAILQ_ENTRY(tog_reflist_entry) entry;
471 struct got_reference *ref;
475 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
477 struct tog_ref_view_state {
478 struct tog_reflist_head refs;
479 struct tog_reflist_entry *first_displayed_entry;
480 struct tog_reflist_entry *last_displayed_entry;
481 struct tog_reflist_entry *selected_entry;
482 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
483 struct got_repository *repo;
484 struct tog_reflist_entry *matched_entry;
485 struct tog_colors colors;
489 * We implement two types of views: parent views and child views.
491 * The 'Tab' key switches focus between a parent view and its child view.
492 * Child views are shown side-by-side to their parent view, provided
493 * there is enough screen estate.
495 * When a new view is opened from within a parent view, this new view
496 * becomes a child view of the parent view, replacing any existing child.
498 * When a new view is opened from within a child view, this new view
499 * becomes a parent view which will obscure the views below until the
500 * user quits the new parent view by typing 'q'.
502 * This list of views contains parent views only.
503 * Child views are only pointed to by their parent view.
505 TAILQ_HEAD(tog_view_list_head, tog_view);
508 TAILQ_ENTRY(tog_view) entry;
511 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
512 int maxx, x; /* max column and current start column */
513 int lines, cols; /* copies of LINES and COLS */
514 int nscrolled, offset; /* lines scrolled and hsplit line offset */
515 int ch, count; /* current keymap and count prefix */
516 int focussed; /* Only set on one parent or child view at a time. */
518 struct tog_view *parent;
519 struct tog_view *child;
522 * This flag is initially set on parent views when a new child view
523 * is created. It gets toggled when the 'Tab' key switches focus
524 * between parent and child.
525 * The flag indicates whether focus should be passed on to our child
526 * view if this parent view gets picked for focus after another parent
527 * view was closed. This prevents child views from losing focus in such
532 enum tog_view_mode mode;
533 /* type-specific state */
534 enum tog_view_type type;
536 struct tog_diff_view_state diff;
537 struct tog_log_view_state log;
538 struct tog_blame_view_state blame;
539 struct tog_tree_view_state tree;
540 struct tog_ref_view_state ref;
543 const struct got_error *(*show)(struct tog_view *);
544 const struct got_error *(*input)(struct tog_view **,
545 struct tog_view *, int);
546 const struct got_error *(*close)(struct tog_view *);
548 const struct got_error *(*search_start)(struct tog_view *);
549 const struct got_error *(*search_next)(struct tog_view *);
552 #define TOG_SEARCH_FORWARD 1
553 #define TOG_SEARCH_BACKWARD 2
554 int search_next_done;
555 #define TOG_SEARCH_HAVE_MORE 1
556 #define TOG_SEARCH_NO_MORE 2
557 #define TOG_SEARCH_HAVE_NONE 3
562 static const struct got_error *open_diff_view(struct tog_view *,
563 struct got_object_id *, struct got_object_id *,
564 const char *, const char *, int, int, int, struct tog_view *,
565 struct got_repository *);
566 static const struct got_error *show_diff_view(struct tog_view *);
567 static const struct got_error *input_diff_view(struct tog_view **,
568 struct tog_view *, int);
569 static const struct got_error* close_diff_view(struct tog_view *);
570 static const struct got_error *search_start_diff_view(struct tog_view *);
571 static const struct got_error *search_next_diff_view(struct tog_view *);
573 static const struct got_error *open_log_view(struct tog_view *,
574 struct got_object_id *, struct got_repository *,
575 const char *, const char *, int);
576 static const struct got_error * show_log_view(struct tog_view *);
577 static const struct got_error *input_log_view(struct tog_view **,
578 struct tog_view *, int);
579 static const struct got_error *close_log_view(struct tog_view *);
580 static const struct got_error *search_start_log_view(struct tog_view *);
581 static const struct got_error *search_next_log_view(struct tog_view *);
583 static const struct got_error *open_blame_view(struct tog_view *, char *,
584 struct got_object_id *, struct got_repository *);
585 static const struct got_error *show_blame_view(struct tog_view *);
586 static const struct got_error *input_blame_view(struct tog_view **,
587 struct tog_view *, int);
588 static const struct got_error *close_blame_view(struct tog_view *);
589 static const struct got_error *search_start_blame_view(struct tog_view *);
590 static const struct got_error *search_next_blame_view(struct tog_view *);
592 static const struct got_error *open_tree_view(struct tog_view *,
593 struct got_object_id *, const char *, struct got_repository *);
594 static const struct got_error *show_tree_view(struct tog_view *);
595 static const struct got_error *input_tree_view(struct tog_view **,
596 struct tog_view *, int);
597 static const struct got_error *close_tree_view(struct tog_view *);
598 static const struct got_error *search_start_tree_view(struct tog_view *);
599 static const struct got_error *search_next_tree_view(struct tog_view *);
601 static const struct got_error *open_ref_view(struct tog_view *,
602 struct got_repository *);
603 static const struct got_error *show_ref_view(struct tog_view *);
604 static const struct got_error *input_ref_view(struct tog_view **,
605 struct tog_view *, int);
606 static const struct got_error *close_ref_view(struct tog_view *);
607 static const struct got_error *search_start_ref_view(struct tog_view *);
608 static const struct got_error *search_next_ref_view(struct tog_view *);
610 static volatile sig_atomic_t tog_sigwinch_received;
611 static volatile sig_atomic_t tog_sigpipe_received;
612 static volatile sig_atomic_t tog_sigcont_received;
613 static volatile sig_atomic_t tog_sigint_received;
614 static volatile sig_atomic_t tog_sigterm_received;
617 tog_sigwinch(int signo)
619 tog_sigwinch_received = 1;
623 tog_sigpipe(int signo)
625 tog_sigpipe_received = 1;
629 tog_sigcont(int signo)
631 tog_sigcont_received = 1;
635 tog_sigint(int signo)
637 tog_sigint_received = 1;
641 tog_sigterm(int signo)
643 tog_sigterm_received = 1;
647 tog_fatal_signal_received(void)
649 return (tog_sigpipe_received ||
650 tog_sigint_received || tog_sigint_received);
654 static const struct got_error *
655 view_close(struct tog_view *view)
657 const struct got_error *err = NULL;
660 view_close(view->child);
664 err = view->close(view);
666 del_panel(view->panel);
668 delwin(view->window);
673 static struct tog_view *
674 view_open(int nlines, int ncols, int begin_y, int begin_x,
675 enum tog_view_type type)
677 struct tog_view *view = calloc(1, sizeof(*view));
685 view->nlines = nlines ? nlines : LINES - begin_y;
686 view->ncols = ncols ? ncols : COLS - begin_x;
687 view->begin_y = begin_y;
688 view->begin_x = begin_x;
689 view->window = newwin(nlines, ncols, begin_y, begin_x);
690 if (view->window == NULL) {
694 view->panel = new_panel(view->window);
695 if (view->panel == NULL ||
696 set_panel_userptr(view->panel, view) != OK) {
701 keypad(view->window, TRUE);
706 view_split_begin_x(int begin_x)
708 if (begin_x > 0 || COLS < 120)
710 return (COLS - MAX(COLS / 2, 80));
713 /* XXX Stub till we decide what to do. */
715 view_split_begin_y(int lines)
717 return lines * HSPLIT_SCALE;
720 static const struct got_error *view_resize(struct tog_view *);
722 static const struct got_error *
723 view_splitscreen(struct tog_view *view)
725 const struct got_error *err = NULL;
727 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
728 view->begin_y = view_split_begin_y(view->nlines);
731 view->begin_x = view_split_begin_x(0);
734 view->nlines = LINES - view->begin_y;
735 view->ncols = COLS - view->begin_x;
738 err = view_resize(view);
742 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
743 view->parent->nlines = view->begin_y;
745 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
746 return got_error_from_errno("mvwin");
751 static const struct got_error *
752 view_fullscreen(struct tog_view *view)
754 const struct got_error *err = NULL;
758 view->nlines = LINES;
762 err = view_resize(view);
766 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
767 return got_error_from_errno("mvwin");
773 view_is_parent_view(struct tog_view *view)
775 return view->parent == NULL;
779 view_is_splitscreen(struct tog_view *view)
781 return view->begin_x > 0 || view->begin_y > 0;
785 view_border(struct tog_view *view)
788 const struct tog_view *view_above;
791 return view_border(view->parent);
793 panel = panel_above(view->panel);
797 view_above = panel_userptr(panel);
798 if (view->mode == TOG_VIEW_SPLIT_HRZN)
799 mvwhline(view->window, view_above->begin_y - 1,
800 view->begin_x, got_locale_is_utf8() ?
801 ACS_HLINE : '-', view->ncols);
803 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
804 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
807 static const struct got_error *request_log_commits(struct tog_view *);
808 static const struct got_error *offset_selection_down(struct tog_view *);
809 static void offset_selection_up(struct tog_view *);
811 static const struct got_error *
812 view_resize(struct tog_view *view)
814 const struct got_error *err = NULL;
815 int dif, nlines, ncols;
817 dif = LINES - view->lines; /* line difference */
819 if (view->lines > LINES)
820 nlines = view->nlines - (view->lines - LINES);
822 nlines = view->nlines + (LINES - view->lines);
823 if (view->cols > COLS)
824 ncols = view->ncols - (view->cols - COLS);
826 ncols = view->ncols + (COLS - view->cols);
829 int hs = view->child->begin_y;
831 if (view->child->focussed)
832 view->child->begin_x = view_split_begin_x(view->begin_x);
833 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
834 view->child->begin_x == 0) {
837 view_fullscreen(view->child);
838 if (view->child->focussed)
839 show_panel(view->child->panel);
841 show_panel(view->panel);
843 ncols = view->child->begin_x;
845 view_splitscreen(view->child);
846 show_panel(view->child->panel);
849 * Request commits if terminal height was increased in a log
850 * view so we have enough commits loaded to populate the view.
852 if (view->type == TOG_VIEW_LOG && dif > 0) {
853 struct tog_log_view_state *ts = &view->state.log;
855 if (ts->commits.ncommits < ts->selected_entry->idx +
856 view->lines - ts->selected) {
857 view->nscrolled = ts->selected_entry->idx +
858 view->lines - ts->selected -
859 ts->commits.ncommits + dif;
860 err = request_log_commits(view);
867 * XXX This is ugly and needs to be moved into the above
868 * logic but "works" for now and my attempts at moving it
869 * break either 'tab' or 'F' key maps in horizontal splits.
872 err = view_splitscreen(view->child);
875 if (dif < 0) { /* top split decreased */
876 err = offset_selection_down(view);
883 show_panel(view->child->panel);
884 nlines = view->nlines;
886 } else if (view->parent == NULL)
889 if (wresize(view->window, nlines, ncols) == ERR)
890 return got_error_from_errno("wresize");
891 if (replace_panel(view->panel, view->window) == ERR)
892 return got_error_from_errno("replace_panel");
893 wclear(view->window);
895 view->nlines = nlines;
903 static const struct got_error *
904 view_close_child(struct tog_view *view)
906 const struct got_error *err = NULL;
908 if (view->child == NULL)
911 err = view_close(view->child);
916 static const struct got_error *
917 view_set_child(struct tog_view *view, struct tog_view *child)
920 child->parent = view;
922 return view_resize(view);
931 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
932 cols = 80; /* Default */
938 resize_term(lines, cols);
941 static const struct got_error *
942 view_search_start(struct tog_view *view)
944 const struct got_error *err = NULL;
945 struct tog_view *v = view;
949 if (view->search_started) {
950 regfree(&view->regex);
952 memset(&view->regmatch, 0, sizeof(view->regmatch));
954 view->search_started = 0;
956 if (view->nlines < 1)
959 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
960 view_is_splitscreen(view->child))
963 mvwaddstr(v->window, v->nlines - 1, 0, "/");
964 wclrtoeol(v->window);
968 ret = wgetnstr(v->window, pattern, sizeof(pattern));
975 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
976 err = view->search_start(view);
978 regfree(&view->regex);
981 view->search_started = 1;
982 view->searching = TOG_SEARCH_FORWARD;
983 view->search_next_done = 0;
984 view->search_next(view);
991 * Compute view->count from numeric user input. User has five-tenths of a
992 * second to follow each numeric keypress with another number to form count.
993 * Return first non-numeric input or ERR and assign total to view->count.
994 * XXX Should we add support for user-defined timeout?
997 get_compound_key(struct tog_view *view, int c)
999 struct tog_view *v = view;
1002 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
1003 view_is_splitscreen(view->child))
1005 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1009 halfdelay(5); /* block for half a second */
1010 wattron(v->window, A_BOLD);
1011 wmove(v->window, v->nlines - 1, 0);
1012 wclrtoeol(v->window);
1013 waddch(v->window, ':');
1016 x = getcurx(v->window);
1017 if (x != ERR && x < view->ncols) {
1018 waddch(v->window, c);
1019 wrefresh(v->window);
1023 * Don't overflow. Max valid request should be the greatest
1024 * between the longest and total lines; cap at 10 million.
1029 n = n * 10 + (c - '0');
1030 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1032 /* Massage excessive or inapplicable values at the input handler. */
1035 wattroff(v->window, A_BOLD);
1036 cbreak(); /* return to blocking */
1040 static const struct got_error *
1041 view_input(struct tog_view **new, int *done, struct tog_view *view,
1042 struct tog_view_list_head *views)
1044 const struct got_error *err = NULL;
1050 /* Clear "no matches" indicator. */
1051 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1052 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1053 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1057 if (view->searching && !view->search_next_done) {
1058 errcode = pthread_mutex_unlock(&tog_mutex);
1060 return got_error_set_errno(errcode,
1061 "pthread_mutex_unlock");
1063 errcode = pthread_mutex_lock(&tog_mutex);
1065 return got_error_set_errno(errcode,
1066 "pthread_mutex_lock");
1067 view->search_next(view);
1071 nodelay(stdscr, FALSE);
1072 /* Allow threads to make progress while we are waiting for input. */
1073 errcode = pthread_mutex_unlock(&tog_mutex);
1075 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1076 /* If we have an unfinished count, don't get a new key map. */
1078 if ((view->count && --view->count == 0) || !view->count) {
1079 ch = wgetch(view->window);
1080 if (ch >= '1' && ch <= '9')
1081 view->ch = ch = get_compound_key(view, ch);
1083 errcode = pthread_mutex_lock(&tog_mutex);
1085 return got_error_set_errno(errcode, "pthread_mutex_lock");
1086 nodelay(stdscr, TRUE);
1088 if (tog_sigwinch_received || tog_sigcont_received) {
1090 tog_sigwinch_received = 0;
1091 tog_sigcont_received = 0;
1092 TAILQ_FOREACH(v, views, entry) {
1093 err = view_resize(v);
1096 err = v->input(new, v, KEY_RESIZE);
1100 err = view_resize(v->child);
1103 err = v->child->input(new, v->child,
1116 view->child->focussed = 1;
1117 view->focus_child = 1;
1118 } else if (view->parent) {
1120 view->parent->focussed = 1;
1121 view->parent->focus_child = 0;
1122 if (!view_is_splitscreen(view)) {
1123 if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1124 view->parent->type == TOG_VIEW_LOG) {
1125 err = request_log_commits(view->parent);
1129 offset_selection_up(view->parent);
1130 err = view_fullscreen(view->parent);
1137 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1138 if (view->parent->type == TOG_VIEW_LOG) {
1139 /* might need more commits to fill fullscreen */
1140 err = request_log_commits(view->parent);
1144 offset_selection_up(view->parent);
1145 view->parent->mode = TOG_VIEW_SPLIT_NONE;
1147 err = view->input(new, view, ch);
1155 if (view_is_parent_view(view)) {
1156 if (view->child == NULL)
1158 if (view_is_splitscreen(view->child)) {
1160 view->child->focussed = 1;
1161 err = view_fullscreen(view->child);
1163 err = view_splitscreen(view->child);
1166 err = view->child->input(new, view->child,
1169 if (view_is_splitscreen(view)) {
1170 view->parent->focussed = 0;
1172 err = view_fullscreen(view);
1174 err = view_splitscreen(view);
1175 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1176 err = view_resize(view->parent);
1180 err = view->input(new, view, KEY_RESIZE);
1184 if (view->type == TOG_VIEW_LOG) {
1185 err = request_log_commits(view);
1190 err = offset_selection_down(view->parent);
1192 err = offset_selection_down(view);
1198 if (view->search_start)
1199 view_search_start(view);
1201 err = view->input(new, view, ch);
1205 if (view->search_started && view->search_next) {
1206 view->searching = (ch == 'n' ?
1207 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1208 view->search_next_done = 0;
1209 view->search_next(view);
1211 err = view->input(new, view, ch);
1214 err = view->input(new, view, ch);
1222 view_needs_focus_indication(struct tog_view *view)
1224 if (view_is_parent_view(view)) {
1225 if (view->child == NULL || view->child->focussed)
1227 if (!view_is_splitscreen(view->child))
1229 } else if (!view_is_splitscreen(view))
1232 return view->focussed;
1235 static const struct got_error *
1236 view_loop(struct tog_view *view)
1238 const struct got_error *err = NULL;
1239 struct tog_view_list_head views;
1240 struct tog_view *new_view;
1241 int fast_refresh = 10;
1242 int done = 0, errcode;
1244 errcode = pthread_mutex_lock(&tog_mutex);
1246 return got_error_set_errno(errcode, "pthread_mutex_lock");
1249 TAILQ_INSERT_HEAD(&views, view, entry);
1252 err = view->show(view);
1257 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1258 /* Refresh fast during initialization, then become slower. */
1259 if (fast_refresh && fast_refresh-- == 0)
1260 halfdelay(10); /* switch to once per second */
1262 err = view_input(&new_view, &done, view, &views);
1266 struct tog_view *v, *prev = NULL;
1268 if (view_is_parent_view(view))
1269 prev = TAILQ_PREV(view, tog_view_list_head,
1271 else if (view->parent)
1272 prev = view->parent;
1275 view->parent->child = NULL;
1276 view->parent->focus_child = 0;
1277 /* Restore fullscreen line height. */
1278 view->parent->nlines = view->parent->lines;
1279 err = view_resize(view->parent);
1283 TAILQ_REMOVE(&views, view, entry);
1285 err = view_close(view);
1290 TAILQ_FOREACH(v, &views, entry) {
1294 if (view == NULL && new_view == NULL) {
1295 /* No view has focus. Try to pick one. */
1298 else if (!TAILQ_EMPTY(&views)) {
1299 view = TAILQ_LAST(&views,
1300 tog_view_list_head);
1303 if (view->focus_child) {
1304 view->child->focussed = 1;
1312 struct tog_view *v, *t;
1313 /* Only allow one parent view per type. */
1314 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1315 if (v->type != new_view->type)
1317 TAILQ_REMOVE(&views, v, entry);
1318 err = view_close(v);
1323 TAILQ_INSERT_TAIL(&views, new_view, entry);
1327 if (view_is_parent_view(view)) {
1328 if (view->child && view->child->focussed)
1331 if (view->parent && view->parent->focussed)
1332 view = view->parent;
1334 show_panel(view->panel);
1335 if (view->child && view_is_splitscreen(view->child))
1336 show_panel(view->child->panel);
1337 if (view->parent && view_is_splitscreen(view)) {
1338 err = view->parent->show(view->parent);
1342 err = view->show(view);
1346 err = view->child->show(view->child);
1355 while (!TAILQ_EMPTY(&views)) {
1356 view = TAILQ_FIRST(&views);
1357 TAILQ_REMOVE(&views, view, entry);
1361 errcode = pthread_mutex_unlock(&tog_mutex);
1362 if (errcode && err == NULL)
1363 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1373 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1378 /* Create newly allocated wide-character string equivalent to a byte string. */
1379 static const struct got_error *
1380 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1383 const struct got_error *err = NULL;
1386 *wlen = mbstowcs(NULL, s, 0);
1387 if (*wlen == (size_t)-1) {
1389 if (errno != EILSEQ)
1390 return got_error_from_errno("mbstowcs");
1392 /* byte string invalid in current encoding; try to "fix" it */
1393 err = got_mbsavis(&vis, &vislen, s);
1396 *wlen = mbstowcs(NULL, vis, 0);
1397 if (*wlen == (size_t)-1) {
1398 err = got_error_from_errno("mbstowcs"); /* give up */
1403 *ws = calloc(*wlen + 1, sizeof(**ws));
1405 err = got_error_from_errno("calloc");
1409 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1410 err = got_error_from_errno("mbstowcs");
1421 static const struct got_error *
1422 expand_tab(char **ptr, const char *src)
1425 size_t len, n, idx = 0, sz = 0;
1428 n = len = strlen(src);
1429 dst = malloc(n + 1);
1431 return got_error_from_errno("malloc");
1433 while (idx < len && src[idx]) {
1434 const char c = src[idx];
1437 size_t nb = TABSIZE - sz % TABSIZE;
1440 p = realloc(dst, n + nb);
1443 return got_error_from_errno("realloc");
1448 memset(dst + sz, ' ', nb);
1451 dst[sz++] = src[idx];
1461 * Advance at most n columns from wline starting at offset off.
1462 * Return the index to the first character after the span operation.
1463 * Return the combined column width of all spanned wide character in
1467 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1469 int width, i, cols = 0;
1476 for (i = off; wline[i] != L'\0'; ++i) {
1477 if (wline[i] == L'\t')
1478 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1480 width = wcwidth(wline[i]);
1487 if (cols + width > n)
1497 * Format a line for display, ensuring that it won't overflow a width limit.
1498 * With scrolling, the width returned refers to the scrolled version of the
1499 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1501 static const struct got_error *
1502 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1503 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1505 const struct got_error *err = NULL;
1507 wchar_t *wline = NULL;
1516 err = expand_tab(&exstr, line);
1521 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1526 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1528 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1529 wline[wlen - 1] = L'\0';
1532 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1533 wline[wlen - 1] = L'\0';
1537 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1543 *scrollxp = scrollx;
1551 static const struct got_error*
1552 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1553 struct got_object_id *id, struct got_repository *repo)
1555 static const struct got_error *err = NULL;
1556 struct got_reflist_entry *re;
1562 TAILQ_FOREACH(re, refs, entry) {
1563 struct got_tag_object *tag = NULL;
1564 struct got_object_id *ref_id;
1567 name = got_ref_get_name(re->ref);
1568 if (strcmp(name, GOT_REF_HEAD) == 0)
1570 if (strncmp(name, "refs/", 5) == 0)
1572 if (strncmp(name, "got/", 4) == 0 &&
1573 strncmp(name, "got/backup/", 11) != 0)
1575 if (strncmp(name, "heads/", 6) == 0)
1577 if (strncmp(name, "remotes/", 8) == 0) {
1579 s = strstr(name, "/" GOT_REF_HEAD);
1580 if (s != NULL && s[strlen(s)] == '\0')
1583 err = got_ref_resolve(&ref_id, repo, re->ref);
1586 if (strncmp(name, "tags/", 5) == 0) {
1587 err = got_object_open_as_tag(&tag, repo, ref_id);
1589 if (err->code != GOT_ERR_OBJ_TYPE) {
1593 /* Ref points at something other than a tag. */
1598 cmp = got_object_id_cmp(tag ?
1599 got_object_tag_get_object_id(tag) : ref_id, id);
1602 got_object_tag_close(tag);
1606 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1607 s ? ", " : "", name) == -1) {
1608 err = got_error_from_errno("asprintf");
1619 static const struct got_error *
1620 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1625 smallerthan = strchr(author, '<');
1626 if (smallerthan && smallerthan[1] != '\0')
1627 author = smallerthan + 1;
1628 author[strcspn(author, "@>")] = '\0';
1629 return format_line(wauthor, author_width, NULL, author, 0, limit,
1633 static const struct got_error *
1634 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1635 struct got_object_id *id, const size_t date_display_cols,
1636 int author_display_cols)
1638 struct tog_log_view_state *s = &view->state.log;
1639 const struct got_error *err = NULL;
1640 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1641 char *logmsg0 = NULL, *logmsg = NULL;
1642 char *author = NULL;
1643 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1644 int author_width, logmsg_width;
1645 char *newline, *line = NULL;
1646 int col, limit, scrollx;
1647 const int avail = view->ncols;
1649 time_t committer_time;
1650 struct tog_color *tc;
1652 committer_time = got_object_commit_get_committer_time(commit);
1653 if (gmtime_r(&committer_time, &tm) == NULL)
1654 return got_error_from_errno("gmtime_r");
1655 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1656 return got_error(GOT_ERR_NO_SPACE);
1658 if (avail <= date_display_cols)
1659 limit = MIN(sizeof(datebuf) - 1, avail);
1661 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1662 tc = get_color(&s->colors, TOG_COLOR_DATE);
1664 wattr_on(view->window,
1665 COLOR_PAIR(tc->colorpair), NULL);
1666 waddnstr(view->window, datebuf, limit);
1668 wattr_off(view->window,
1669 COLOR_PAIR(tc->colorpair), NULL);
1676 err = got_object_id_str(&id_str, id);
1679 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1681 wattr_on(view->window,
1682 COLOR_PAIR(tc->colorpair), NULL);
1683 wprintw(view->window, "%.8s ", id_str);
1685 wattr_off(view->window,
1686 COLOR_PAIR(tc->colorpair), NULL);
1693 author = strdup(got_object_commit_get_author(commit));
1694 if (author == NULL) {
1695 err = got_error_from_errno("strdup");
1698 err = format_author(&wauthor, &author_width, author, avail - col, col);
1701 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1703 wattr_on(view->window,
1704 COLOR_PAIR(tc->colorpair), NULL);
1705 waddwstr(view->window, wauthor);
1707 wattr_off(view->window,
1708 COLOR_PAIR(tc->colorpair), NULL);
1709 col += author_width;
1710 while (col < avail && author_width < author_display_cols + 2) {
1711 waddch(view->window, ' ');
1718 err = got_object_commit_get_logmsg(&logmsg0, commit);
1722 while (*logmsg == '\n')
1724 newline = strchr(logmsg, '\n');
1727 limit = avail - col;
1728 if (view->child && view_is_splitscreen(view->child) && limit > 0)
1729 limit--; /* for the border */
1730 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1734 waddwstr(view->window, &wlogmsg[scrollx]);
1735 col += MAX(logmsg_width, 0);
1736 while (col < avail) {
1737 waddch(view->window, ' ');
1749 static struct commit_queue_entry *
1750 alloc_commit_queue_entry(struct got_commit_object *commit,
1751 struct got_object_id *id)
1753 struct commit_queue_entry *entry;
1755 entry = calloc(1, sizeof(*entry));
1760 entry->commit = commit;
1765 pop_commit(struct commit_queue *commits)
1767 struct commit_queue_entry *entry;
1769 entry = TAILQ_FIRST(&commits->head);
1770 TAILQ_REMOVE(&commits->head, entry, entry);
1771 got_object_commit_close(entry->commit);
1772 commits->ncommits--;
1773 /* Don't free entry->id! It is owned by the commit graph. */
1778 free_commits(struct commit_queue *commits)
1780 while (!TAILQ_EMPTY(&commits->head))
1781 pop_commit(commits);
1784 static const struct got_error *
1785 match_commit(int *have_match, struct got_object_id *id,
1786 struct got_commit_object *commit, regex_t *regex)
1788 const struct got_error *err = NULL;
1789 regmatch_t regmatch;
1790 char *id_str = NULL, *logmsg = NULL;
1794 err = got_object_id_str(&id_str, id);
1798 err = got_object_commit_get_logmsg(&logmsg, commit);
1802 if (regexec(regex, got_object_commit_get_author(commit), 1,
1803 ®match, 0) == 0 ||
1804 regexec(regex, got_object_commit_get_committer(commit), 1,
1805 ®match, 0) == 0 ||
1806 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1807 regexec(regex, logmsg, 1, ®match, 0) == 0)
1815 static const struct got_error *
1816 queue_commits(struct tog_log_thread_args *a)
1818 const struct got_error *err = NULL;
1821 * We keep all commits open throughout the lifetime of the log
1822 * view in order to avoid having to re-fetch commits from disk
1823 * while updating the display.
1826 struct got_object_id *id;
1827 struct got_commit_object *commit;
1828 struct commit_queue_entry *entry;
1831 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1833 if (err || id == NULL)
1836 err = got_object_open_as_commit(&commit, a->repo, id);
1839 entry = alloc_commit_queue_entry(commit, id);
1840 if (entry == NULL) {
1841 err = got_error_from_errno("alloc_commit_queue_entry");
1845 errcode = pthread_mutex_lock(&tog_mutex);
1847 err = got_error_set_errno(errcode,
1848 "pthread_mutex_lock");
1852 entry->idx = a->commits->ncommits;
1853 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1854 a->commits->ncommits++;
1856 if (*a->searching == TOG_SEARCH_FORWARD &&
1857 !*a->search_next_done) {
1859 err = match_commit(&have_match, id, commit, a->regex);
1863 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1866 errcode = pthread_mutex_unlock(&tog_mutex);
1867 if (errcode && err == NULL)
1868 err = got_error_set_errno(errcode,
1869 "pthread_mutex_unlock");
1872 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1878 select_commit(struct tog_log_view_state *s)
1880 struct commit_queue_entry *entry;
1883 entry = s->first_displayed_entry;
1885 if (ncommits == s->selected) {
1886 s->selected_entry = entry;
1889 entry = TAILQ_NEXT(entry, entry);
1894 static const struct got_error *
1895 draw_commits(struct tog_view *view)
1897 const struct got_error *err = NULL;
1898 struct tog_log_view_state *s = &view->state.log;
1899 struct commit_queue_entry *entry = s->selected_entry;
1900 const int limit = view->nlines;
1902 int ncommits, author_cols = 4;
1903 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1904 char *refs_str = NULL;
1906 struct tog_color *tc;
1907 static const size_t date_display_cols = 12;
1909 if (s->selected_entry &&
1910 !(view->searching && view->search_next_done == 0)) {
1911 struct got_reflist_head *refs;
1912 err = got_object_id_str(&id_str, s->selected_entry->id);
1915 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1916 s->selected_entry->id);
1918 err = build_refs_str(&refs_str, refs,
1919 s->selected_entry->id, s->repo);
1925 if (s->thread_args.commits_needed == 0)
1926 halfdelay(10); /* disable fast refresh */
1928 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1929 if (asprintf(&ncommits_str, " [%d/%d] %s",
1930 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1931 (view->searching && !view->search_next_done) ?
1932 "searching..." : "loading...") == -1) {
1933 err = got_error_from_errno("asprintf");
1937 const char *search_str = NULL;
1939 if (view->searching) {
1940 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1941 search_str = "no more matches";
1942 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1943 search_str = "no matches found";
1944 else if (!view->search_next_done)
1945 search_str = "searching...";
1948 if (asprintf(&ncommits_str, " [%d/%d] %s",
1949 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1950 search_str ? search_str :
1951 (refs_str ? refs_str : "")) == -1) {
1952 err = got_error_from_errno("asprintf");
1957 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1958 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1959 "........................................",
1960 s->in_repo_path, ncommits_str) == -1) {
1961 err = got_error_from_errno("asprintf");
1965 } else if (asprintf(&header, "commit %s%s",
1966 id_str ? id_str : "........................................",
1967 ncommits_str) == -1) {
1968 err = got_error_from_errno("asprintf");
1972 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1976 werase(view->window);
1978 if (view_needs_focus_indication(view))
1979 wstandout(view->window);
1980 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1982 wattr_on(view->window,
1983 COLOR_PAIR(tc->colorpair), NULL);
1984 waddwstr(view->window, wline);
1986 wattr_off(view->window,
1987 COLOR_PAIR(tc->colorpair), NULL);
1988 while (width < view->ncols) {
1989 waddch(view->window, ' ');
1992 if (view_needs_focus_indication(view))
1993 wstandend(view->window);
1998 /* Grow author column size if necessary, and set view->maxx. */
1999 entry = s->first_displayed_entry;
2003 char *author, *eol, *msg, *msg0;
2004 wchar_t *wauthor, *wmsg;
2006 if (ncommits >= limit - 1)
2008 author = strdup(got_object_commit_get_author(entry->commit));
2009 if (author == NULL) {
2010 err = got_error_from_errno("strdup");
2013 err = format_author(&wauthor, &width, author, COLS,
2015 if (author_cols < width)
2016 author_cols = width;
2019 err = got_object_commit_get_logmsg(&msg0, entry->commit);
2023 while (*msg == '\n')
2025 if ((eol = strchr(msg, '\n')))
2027 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2028 date_display_cols + author_cols, 0);
2031 view->maxx = MAX(view->maxx, width);
2035 entry = TAILQ_NEXT(entry, entry);
2038 entry = s->first_displayed_entry;
2039 s->last_displayed_entry = s->first_displayed_entry;
2042 if (ncommits >= limit - 1)
2044 if (ncommits == s->selected)
2045 wstandout(view->window);
2046 err = draw_commit(view, entry->commit, entry->id,
2047 date_display_cols, author_cols);
2048 if (ncommits == s->selected)
2049 wstandend(view->window);
2053 s->last_displayed_entry = entry;
2054 entry = TAILQ_NEXT(entry, entry);
2067 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2069 struct commit_queue_entry *entry;
2072 entry = TAILQ_FIRST(&s->commits.head);
2073 if (s->first_displayed_entry == entry)
2076 entry = s->first_displayed_entry;
2077 while (entry && nscrolled < maxscroll) {
2078 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2080 s->first_displayed_entry = entry;
2086 static const struct got_error *
2087 trigger_log_thread(struct tog_view *view, int wait)
2089 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2092 halfdelay(1); /* fast refresh while loading commits */
2094 while (ta->commits_needed > 0 || ta->load_all) {
2095 if (ta->log_complete)
2098 /* Wake the log thread. */
2099 errcode = pthread_cond_signal(&ta->need_commits);
2101 return got_error_set_errno(errcode,
2102 "pthread_cond_signal");
2105 * The mutex will be released while the view loop waits
2106 * in wgetch(), at which time the log thread will run.
2111 /* Display progress update in log view. */
2112 show_log_view(view);
2116 /* Wait right here while next commit is being loaded. */
2117 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2119 return got_error_set_errno(errcode,
2120 "pthread_cond_wait");
2122 /* Display progress update in log view. */
2123 show_log_view(view);
2131 static const struct got_error *
2132 request_log_commits(struct tog_view *view)
2134 struct tog_log_view_state *state = &view->state.log;
2135 const struct got_error *err = NULL;
2137 state->thread_args.commits_needed = view->nscrolled;
2138 err = trigger_log_thread(view, 1);
2139 view->nscrolled = 0;
2144 static const struct got_error *
2145 log_scroll_down(struct tog_view *view, int maxscroll)
2147 struct tog_log_view_state *s = &view->state.log;
2148 const struct got_error *err = NULL;
2149 struct commit_queue_entry *pentry;
2150 int nscrolled = 0, ncommits_needed;
2152 if (s->last_displayed_entry == NULL)
2155 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2156 if (s->commits.ncommits < ncommits_needed &&
2157 !s->thread_args.log_complete) {
2159 * Ask the log thread for required amount of commits.
2161 s->thread_args.commits_needed += maxscroll;
2162 err = trigger_log_thread(view, 1);
2168 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2169 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2172 s->last_displayed_entry = pentry ?
2173 pentry : s->last_displayed_entry;;
2175 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2178 s->first_displayed_entry = pentry;
2179 } while (++nscrolled < maxscroll);
2181 if (view->mode == TOG_VIEW_SPLIT_HRZN)
2182 view->nscrolled += nscrolled;
2184 view->nscrolled = 0;
2189 static const struct got_error *
2190 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2191 struct got_commit_object *commit, struct got_object_id *commit_id,
2192 struct tog_view *log_view, struct got_repository *repo)
2194 const struct got_error *err;
2195 struct got_object_qid *parent_id;
2196 struct tog_view *diff_view;
2198 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2199 if (diff_view == NULL)
2200 return got_error_from_errno("view_open");
2202 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2203 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2204 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2206 *new_view = diff_view;
2210 static const struct got_error *
2211 tree_view_visit_subtree(struct tog_tree_view_state *s,
2212 struct got_tree_object *subtree)
2214 struct tog_parent_tree *parent;
2216 parent = calloc(1, sizeof(*parent));
2218 return got_error_from_errno("calloc");
2220 parent->tree = s->tree;
2221 parent->first_displayed_entry = s->first_displayed_entry;
2222 parent->selected_entry = s->selected_entry;
2223 parent->selected = s->selected;
2224 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2227 s->first_displayed_entry = NULL;
2231 static const struct got_error *
2232 tree_view_walk_path(struct tog_tree_view_state *s,
2233 struct got_commit_object *commit, const char *path)
2235 const struct got_error *err = NULL;
2236 struct got_tree_object *tree = NULL;
2238 char *slash, *subpath = NULL;
2240 /* Walk the path and open corresponding tree objects. */
2243 struct got_tree_entry *te;
2244 struct got_object_id *tree_id;
2250 /* Ensure the correct subtree entry is selected. */
2251 slash = strchr(p, '/');
2253 te_name = strdup(p);
2255 te_name = strndup(p, slash - p);
2256 if (te_name == NULL) {
2257 err = got_error_from_errno("strndup");
2260 te = got_object_tree_find_entry(s->tree, te_name);
2262 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2267 s->first_displayed_entry = s->selected_entry = te;
2269 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2270 break; /* jump to this file's entry */
2272 slash = strchr(p, '/');
2274 subpath = strndup(path, slash - path);
2276 subpath = strdup(path);
2277 if (subpath == NULL) {
2278 err = got_error_from_errno("strdup");
2282 err = got_object_id_by_path(&tree_id, s->repo, commit,
2287 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2292 err = tree_view_visit_subtree(s, tree);
2294 got_object_tree_close(tree);
2308 static const struct got_error *
2309 browse_commit_tree(struct tog_view **new_view, int begin_x,
2310 struct commit_queue_entry *entry, const char *path,
2311 const char *head_ref_name, struct got_repository *repo)
2313 const struct got_error *err = NULL;
2314 struct tog_tree_view_state *s;
2315 struct tog_view *tree_view;
2317 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2318 if (tree_view == NULL)
2319 return got_error_from_errno("view_open");
2321 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2324 s = &tree_view->state.tree;
2326 *new_view = tree_view;
2328 if (got_path_is_root_dir(path))
2331 return tree_view_walk_path(s, entry->commit, path);
2334 static const struct got_error *
2335 block_signals_used_by_main_thread(void)
2340 if (sigemptyset(&sigset) == -1)
2341 return got_error_from_errno("sigemptyset");
2343 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2344 if (sigaddset(&sigset, SIGWINCH) == -1)
2345 return got_error_from_errno("sigaddset");
2346 if (sigaddset(&sigset, SIGCONT) == -1)
2347 return got_error_from_errno("sigaddset");
2348 if (sigaddset(&sigset, SIGINT) == -1)
2349 return got_error_from_errno("sigaddset");
2350 if (sigaddset(&sigset, SIGTERM) == -1)
2351 return got_error_from_errno("sigaddset");
2353 /* ncurses handles SIGTSTP */
2354 if (sigaddset(&sigset, SIGTSTP) == -1)
2355 return got_error_from_errno("sigaddset");
2357 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2359 return got_error_set_errno(errcode, "pthread_sigmask");
2365 log_thread(void *arg)
2367 const struct got_error *err = NULL;
2369 struct tog_log_thread_args *a = arg;
2372 err = block_signals_used_by_main_thread();
2376 while (!done && !err && !tog_fatal_signal_received()) {
2377 err = queue_commits(a);
2379 if (err->code != GOT_ERR_ITER_COMPLETED)
2383 } else if (a->commits_needed > 0 && !a->load_all)
2384 a->commits_needed--;
2386 errcode = pthread_mutex_lock(&tog_mutex);
2388 err = got_error_set_errno(errcode,
2389 "pthread_mutex_lock");
2391 } else if (*a->quit)
2393 else if (*a->first_displayed_entry == NULL) {
2394 *a->first_displayed_entry =
2395 TAILQ_FIRST(&a->commits->head);
2396 *a->selected_entry = *a->first_displayed_entry;
2399 errcode = pthread_cond_signal(&a->commit_loaded);
2401 err = got_error_set_errno(errcode,
2402 "pthread_cond_signal");
2403 pthread_mutex_unlock(&tog_mutex);
2408 a->commits_needed = 0;
2410 if (a->commits_needed == 0 && !a->load_all) {
2411 errcode = pthread_cond_wait(&a->need_commits,
2414 err = got_error_set_errno(errcode,
2415 "pthread_cond_wait");
2421 errcode = pthread_mutex_unlock(&tog_mutex);
2422 if (errcode && err == NULL)
2423 err = got_error_set_errno(errcode,
2424 "pthread_mutex_unlock");
2426 a->log_complete = 1;
2430 static const struct got_error *
2431 stop_log_thread(struct tog_log_view_state *s)
2433 const struct got_error *err = NULL;
2438 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2440 return got_error_set_errno(errcode,
2441 "pthread_cond_signal");
2442 errcode = pthread_mutex_unlock(&tog_mutex);
2444 return got_error_set_errno(errcode,
2445 "pthread_mutex_unlock");
2446 errcode = pthread_join(s->thread, (void **)&err);
2448 return got_error_set_errno(errcode, "pthread_join");
2449 errcode = pthread_mutex_lock(&tog_mutex);
2451 return got_error_set_errno(errcode,
2452 "pthread_mutex_lock");
2456 if (s->thread_args.repo) {
2457 err = got_repo_close(s->thread_args.repo);
2458 s->thread_args.repo = NULL;
2461 if (s->thread_args.pack_fds) {
2462 const struct got_error *pack_err =
2463 got_repo_pack_fds_close(s->thread_args.pack_fds);
2466 s->thread_args.pack_fds = NULL;
2469 if (s->thread_args.graph) {
2470 got_commit_graph_close(s->thread_args.graph);
2471 s->thread_args.graph = NULL;
2477 static const struct got_error *
2478 close_log_view(struct tog_view *view)
2480 const struct got_error *err = NULL;
2481 struct tog_log_view_state *s = &view->state.log;
2484 err = stop_log_thread(s);
2486 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2487 if (errcode && err == NULL)
2488 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2490 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2491 if (errcode && err == NULL)
2492 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2494 free_commits(&s->commits);
2495 free(s->in_repo_path);
2496 s->in_repo_path = NULL;
2499 free(s->head_ref_name);
2500 s->head_ref_name = NULL;
2504 static const struct got_error *
2505 search_start_log_view(struct tog_view *view)
2507 struct tog_log_view_state *s = &view->state.log;
2509 s->matched_entry = NULL;
2510 s->search_entry = NULL;
2514 static const struct got_error *
2515 search_next_log_view(struct tog_view *view)
2517 const struct got_error *err = NULL;
2518 struct tog_log_view_state *s = &view->state.log;
2519 struct commit_queue_entry *entry;
2521 /* Display progress update in log view. */
2522 show_log_view(view);
2526 if (s->search_entry) {
2528 errcode = pthread_mutex_unlock(&tog_mutex);
2530 return got_error_set_errno(errcode,
2531 "pthread_mutex_unlock");
2532 ch = wgetch(view->window);
2533 errcode = pthread_mutex_lock(&tog_mutex);
2535 return got_error_set_errno(errcode,
2536 "pthread_mutex_lock");
2537 if (ch == KEY_BACKSPACE) {
2538 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2541 if (view->searching == TOG_SEARCH_FORWARD)
2542 entry = TAILQ_NEXT(s->search_entry, entry);
2544 entry = TAILQ_PREV(s->search_entry,
2545 commit_queue_head, entry);
2546 } else if (s->matched_entry) {
2547 int matched_idx = s->matched_entry->idx;
2548 int selected_idx = s->selected_entry->idx;
2551 * If the user has moved the cursor after we hit a match,
2552 * the position from where we should continue searching
2553 * might have changed.
2555 if (view->searching == TOG_SEARCH_FORWARD) {
2556 if (matched_idx > selected_idx)
2557 entry = TAILQ_NEXT(s->selected_entry, entry);
2559 entry = TAILQ_NEXT(s->matched_entry, entry);
2561 if (matched_idx < selected_idx)
2562 entry = TAILQ_PREV(s->selected_entry,
2563 commit_queue_head, entry);
2565 entry = TAILQ_PREV(s->matched_entry,
2566 commit_queue_head, entry);
2569 entry = s->selected_entry;
2575 if (entry == NULL) {
2576 if (s->thread_args.log_complete ||
2577 view->searching == TOG_SEARCH_BACKWARD) {
2578 view->search_next_done =
2579 (s->matched_entry == NULL ?
2580 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2581 s->search_entry = NULL;
2585 * Poke the log thread for more commits and return,
2586 * allowing the main loop to make progress. Search
2587 * will resume at s->search_entry once we come back.
2589 s->thread_args.commits_needed++;
2590 return trigger_log_thread(view, 0);
2593 err = match_commit(&have_match, entry->id, entry->commit,
2598 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2599 s->matched_entry = entry;
2603 s->search_entry = entry;
2604 if (view->searching == TOG_SEARCH_FORWARD)
2605 entry = TAILQ_NEXT(entry, entry);
2607 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2610 if (s->matched_entry) {
2611 int cur = s->selected_entry->idx;
2612 while (cur < s->matched_entry->idx) {
2613 err = input_log_view(NULL, view, KEY_DOWN);
2618 while (cur > s->matched_entry->idx) {
2619 err = input_log_view(NULL, view, KEY_UP);
2626 s->search_entry = NULL;
2631 static const struct got_error *
2632 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2633 struct got_repository *repo, const char *head_ref_name,
2634 const char *in_repo_path, int log_branches)
2636 const struct got_error *err = NULL;
2637 struct tog_log_view_state *s = &view->state.log;
2638 struct got_repository *thread_repo = NULL;
2639 struct got_commit_graph *thread_graph = NULL;
2642 if (in_repo_path != s->in_repo_path) {
2643 free(s->in_repo_path);
2644 s->in_repo_path = strdup(in_repo_path);
2645 if (s->in_repo_path == NULL)
2646 return got_error_from_errno("strdup");
2649 /* The commit queue only contains commits being displayed. */
2650 TAILQ_INIT(&s->commits.head);
2651 s->commits.ncommits = 0;
2654 if (head_ref_name) {
2655 s->head_ref_name = strdup(head_ref_name);
2656 if (s->head_ref_name == NULL) {
2657 err = got_error_from_errno("strdup");
2661 s->start_id = got_object_id_dup(start_id);
2662 if (s->start_id == NULL) {
2663 err = got_error_from_errno("got_object_id_dup");
2666 s->log_branches = log_branches;
2668 STAILQ_INIT(&s->colors);
2669 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2670 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2671 get_color_value("TOG_COLOR_COMMIT"));
2674 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2675 get_color_value("TOG_COLOR_AUTHOR"));
2677 free_colors(&s->colors);
2680 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2681 get_color_value("TOG_COLOR_DATE"));
2683 free_colors(&s->colors);
2688 view->show = show_log_view;
2689 view->input = input_log_view;
2690 view->close = close_log_view;
2691 view->search_start = search_start_log_view;
2692 view->search_next = search_next_log_view;
2694 if (s->thread_args.pack_fds == NULL) {
2695 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2699 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2700 s->thread_args.pack_fds);
2703 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2707 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2708 s->repo, NULL, NULL);
2712 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2714 err = got_error_set_errno(errcode, "pthread_cond_init");
2717 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2719 err = got_error_set_errno(errcode, "pthread_cond_init");
2723 s->thread_args.commits_needed = view->nlines;
2724 s->thread_args.graph = thread_graph;
2725 s->thread_args.commits = &s->commits;
2726 s->thread_args.in_repo_path = s->in_repo_path;
2727 s->thread_args.start_id = s->start_id;
2728 s->thread_args.repo = thread_repo;
2729 s->thread_args.log_complete = 0;
2730 s->thread_args.quit = &s->quit;
2731 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2732 s->thread_args.selected_entry = &s->selected_entry;
2733 s->thread_args.searching = &view->searching;
2734 s->thread_args.search_next_done = &view->search_next_done;
2735 s->thread_args.regex = &view->regex;
2738 close_log_view(view);
2742 static const struct got_error *
2743 show_log_view(struct tog_view *view)
2745 const struct got_error *err;
2746 struct tog_log_view_state *s = &view->state.log;
2748 if (s->thread == NULL) {
2749 int errcode = pthread_create(&s->thread, NULL, log_thread,
2752 return got_error_set_errno(errcode, "pthread_create");
2753 if (s->thread_args.commits_needed > 0) {
2754 err = trigger_log_thread(view, 1);
2760 return draw_commits(view);
2764 log_move_cursor_up(struct tog_view *view, int page, int home)
2766 struct tog_log_view_state *s = &view->state.log;
2768 if (s->selected_entry->idx == 0)
2770 if (s->first_displayed_entry == NULL)
2773 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2775 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
2777 if (!page && !home && s->selected > 0)
2780 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
2786 static const struct got_error *
2787 log_move_cursor_down(struct tog_view *view, int page)
2789 struct tog_log_view_state *s = &view->state.log;
2790 struct commit_queue_entry *first;
2791 const struct got_error *err = NULL;
2793 first = s->first_displayed_entry;
2794 if (first == NULL) {
2799 if (s->thread_args.log_complete &&
2800 s->selected_entry->idx >= s->commits.ncommits - 1)
2804 int eos = view->nlines - 2;
2806 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
2807 view_is_splitscreen(view->child))
2808 --eos; /* border consumes the last line */
2809 if (s->selected < MIN(eos, s->commits.ncommits - 1))
2812 err = log_scroll_down(view, 1);
2813 } else if (s->thread_args.log_complete) {
2814 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
2815 s->selected += MIN(s->last_displayed_entry->idx -
2816 s->selected_entry->idx, page + 1);
2818 err = log_scroll_down(view, MIN(page,
2819 s->commits.ncommits - s->selected_entry->idx - 1));
2820 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
2822 err = log_scroll_down(view, page);
2825 if (first == s->first_displayed_entry && s->selected <
2826 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
2827 s->selected = MIN(s->commits.ncommits - 1, page);
2834 * We might necessarily overshoot in horizontal
2835 * splits; if so, select the last displayed commit.
2837 s->selected = MIN(s->selected,
2838 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
2842 if (s->thread_args.log_complete &&
2843 s->selected_entry->idx == s->commits.ncommits - 1)
2850 * Get splitscreen dimensions based on TOG_VIEW_SPLIT_MODE:
2851 * TOG_VIEW_SPLIT_VERT vertical split if COLS > 119 (default)
2852 * TOG_VIEW_SPLIT_HRZN horizontal split
2853 * Assign start column and line of the new split to *x and *y, respectively,
2854 * and assign view mode to view->mode.
2857 view_get_split(struct tog_view *view, int *y, int *x)
2861 mode = getenv("TOG_VIEW_SPLIT_MODE");
2863 if (!mode || mode[0] != 'h') {
2864 view->mode = TOG_VIEW_SPLIT_VERT;
2865 *x = view_split_begin_x(view->begin_x);
2866 } else if (mode && mode[0] == 'h') {
2867 view->mode = TOG_VIEW_SPLIT_HRZN;
2868 *y = view_split_begin_y(view->lines);
2872 /* Split view horizontally at y and offset view->state->selected line. */
2873 static const struct got_error *
2874 view_init_hsplit(struct tog_view *view, int y)
2876 const struct got_error *err = NULL;
2879 err = view_resize(view);
2883 err = offset_selection_down(view);
2888 static const struct got_error *
2889 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2891 const struct got_error *err = NULL;
2892 struct tog_log_view_state *s = &view->state.log;
2893 struct tog_view *diff_view = NULL, *tree_view = NULL;
2894 struct tog_view *ref_view = NULL;
2895 struct commit_queue_entry *entry;
2896 int begin_x = 0, begin_y = 0, n, nscroll = view->nlines - 1;
2898 if (s->thread_args.load_all) {
2899 if (ch == KEY_BACKSPACE)
2900 s->thread_args.load_all = 0;
2901 else if (s->thread_args.log_complete) {
2902 s->thread_args.load_all = 0;
2903 err = log_move_cursor_down(view, s->commits.ncommits);
2916 view->x = MAX(view->maxx - view->ncols / 2, 0);
2921 if (view->x + view->ncols / 2 < view->maxx)
2922 view->x += 2; /* move two columns right */
2928 view->x -= MIN(view->x, 2); /* move two columns back */
2937 log_move_cursor_up(view, 0, 0);
2941 log_move_cursor_up(view, 0, 1);
2951 log_move_cursor_up(view, nscroll, 0);
2958 err = log_move_cursor_down(view, 0);
2962 /* We don't know yet how many commits, so we're forced to
2963 * traverse them all. */
2965 if (!s->thread_args.log_complete) {
2966 s->thread_args.load_all = 1;
2967 return trigger_log_thread(view, 0);
2971 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2972 for (n = 0; n < view->nlines - 1; n++) {
2975 s->first_displayed_entry = entry;
2976 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2979 s->selected = n - 1;
2991 err = log_move_cursor_down(view, nscroll);
2994 if (s->selected > view->nlines - 2)
2995 s->selected = view->nlines - 2;
2996 if (s->selected > s->commits.ncommits - 1)
2997 s->selected = s->commits.ncommits - 1;
2999 if (s->commits.ncommits < view->nlines - 1 &&
3000 !s->thread_args.log_complete) {
3001 s->thread_args.commits_needed += (view->nlines - 1) -
3002 s->commits.ncommits;
3003 err = trigger_log_thread(view, 1);
3009 if (s->selected_entry == NULL)
3012 /* get dimensions--don't split till initialisation succeeds */
3013 if (view_is_parent_view(view))
3014 view_get_split(view, &begin_y, &begin_x);
3016 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3017 s->selected_entry->commit, s->selected_entry->id,
3022 if (view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3023 err = view_init_hsplit(view, begin_y);
3029 diff_view->focussed = 1;
3030 diff_view->mode = view->mode;
3031 diff_view->nlines = view->lines - begin_y;
3033 if (view_is_parent_view(view)) {
3034 err = view_close_child(view);
3037 err = view_set_child(view, diff_view);
3040 view->focus_child = 1;
3042 *new_view = diff_view;
3047 if (s->selected_entry == NULL)
3049 if (view_is_parent_view(view))
3050 begin_x = view_split_begin_x(view->begin_x);
3051 err = browse_commit_tree(&tree_view, begin_x,
3052 s->selected_entry, s->in_repo_path, s->head_ref_name,
3057 tree_view->focussed = 1;
3058 if (view_is_parent_view(view)) {
3059 err = view_close_child(view);
3062 err = view_set_child(view, tree_view);
3065 view->focus_child = 1;
3067 *new_view = tree_view;
3073 if (ch == KEY_BACKSPACE &&
3074 got_path_is_root_dir(s->in_repo_path))
3076 err = stop_log_thread(s);
3079 if (ch == KEY_BACKSPACE) {
3081 err = got_path_dirname(&parent_path, s->in_repo_path);
3084 free(s->in_repo_path);
3085 s->in_repo_path = parent_path;
3086 s->thread_args.in_repo_path = s->in_repo_path;
3087 } else if (ch == CTRL('l')) {
3088 struct got_object_id *start_id;
3089 err = got_repo_match_object_id(&start_id, NULL,
3090 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3091 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3095 s->start_id = start_id;
3096 s->thread_args.start_id = s->start_id;
3098 s->log_branches = !s->log_branches;
3100 if (s->thread_args.pack_fds == NULL) {
3101 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3105 err = got_repo_open(&s->thread_args.repo,
3106 got_repo_get_path(s->repo), NULL,
3107 s->thread_args.pack_fds);
3111 err = tog_load_refs(s->repo, 0);
3114 err = got_commit_graph_open(&s->thread_args.graph,
3115 s->in_repo_path, !s->log_branches);
3118 err = got_commit_graph_iter_start(s->thread_args.graph,
3119 s->start_id, s->repo, NULL, NULL);
3122 free_commits(&s->commits);
3123 s->first_displayed_entry = NULL;
3124 s->last_displayed_entry = NULL;
3125 s->selected_entry = NULL;
3127 s->thread_args.log_complete = 0;
3129 s->thread_args.commits_needed = view->lines;
3130 s->matched_entry = NULL;
3131 s->search_entry = NULL;
3135 if (view_is_parent_view(view))
3136 begin_x = view_split_begin_x(view->begin_x);
3137 ref_view = view_open(view->nlines, view->ncols,
3138 view->begin_y, begin_x, TOG_VIEW_REF);
3139 if (ref_view == NULL)
3140 return got_error_from_errno("view_open");
3141 err = open_ref_view(ref_view, s->repo);
3143 view_close(ref_view);
3147 ref_view->focussed = 1;
3148 if (view_is_parent_view(view)) {
3149 err = view_close_child(view);
3152 err = view_set_child(view, ref_view);
3155 view->focus_child = 1;
3157 *new_view = ref_view;
3167 static const struct got_error *
3168 apply_unveil(const char *repo_path, const char *worktree_path)
3170 const struct got_error *error;
3173 if (unveil("gmon.out", "rwc") != 0)
3174 return got_error_from_errno2("unveil", "gmon.out");
3176 if (repo_path && unveil(repo_path, "r") != 0)
3177 return got_error_from_errno2("unveil", repo_path);
3179 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3180 return got_error_from_errno2("unveil", worktree_path);
3182 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3183 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3185 error = got_privsep_unveil_exec_helpers();
3189 if (unveil(NULL, NULL) != 0)
3190 return got_error_from_errno("unveil");
3199 * Override default signal handlers before starting ncurses.
3200 * This should prevent ncurses from installing its own
3201 * broken cleanup() signal handler.
3203 signal(SIGWINCH, tog_sigwinch);
3204 signal(SIGPIPE, tog_sigpipe);
3205 signal(SIGCONT, tog_sigcont);
3206 signal(SIGINT, tog_sigint);
3207 signal(SIGTERM, tog_sigterm);
3211 halfdelay(1); /* Do fast refresh while initial view is loading. */
3214 intrflush(stdscr, FALSE);
3215 keypad(stdscr, TRUE);
3217 if (getenv("TOG_COLORS") != NULL) {
3219 use_default_colors();
3223 static const struct got_error *
3224 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3225 struct got_repository *repo, struct got_worktree *worktree)
3227 const struct got_error *err = NULL;
3230 *in_repo_path = strdup("/");
3231 if (*in_repo_path == NULL)
3232 return got_error_from_errno("strdup");
3237 const char *prefix = got_worktree_get_path_prefix(worktree);
3240 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3243 if (asprintf(in_repo_path, "%s%s%s", prefix,
3244 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3246 err = got_error_from_errno("asprintf");
3247 *in_repo_path = NULL;
3251 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3256 static const struct got_error *
3257 cmd_log(int argc, char *argv[])
3259 const struct got_error *error;
3260 struct got_repository *repo = NULL;
3261 struct got_worktree *worktree = NULL;
3262 struct got_object_id *start_id = NULL;
3263 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3264 char *start_commit = NULL, *label = NULL;
3265 struct got_reference *ref = NULL;
3266 const char *head_ref_name = NULL;
3267 int ch, log_branches = 0;
3268 struct tog_view *view;
3269 int *pack_fds = NULL;
3271 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3277 start_commit = optarg;
3280 repo_path = realpath(optarg, NULL);
3281 if (repo_path == NULL)
3282 return got_error_from_errno2("realpath",
3297 error = got_repo_pack_fds_open(&pack_fds);
3301 if (repo_path == NULL) {
3302 cwd = getcwd(NULL, 0);
3304 return got_error_from_errno("getcwd");
3305 error = got_worktree_open(&worktree, cwd);
3306 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3310 strdup(got_worktree_get_repo_path(worktree));
3312 repo_path = strdup(cwd);
3313 if (repo_path == NULL) {
3314 error = got_error_from_errno("strdup");
3319 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3323 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3330 error = apply_unveil(got_repo_get_path(repo),
3331 worktree ? got_worktree_get_root_path(worktree) : NULL);
3335 /* already loaded by tog_log_with_path()? */
3336 if (TAILQ_EMPTY(&tog_refs)) {
3337 error = tog_load_refs(repo, 0);
3342 if (start_commit == NULL) {
3343 error = got_repo_match_object_id(&start_id, &label,
3344 worktree ? got_worktree_get_head_ref_name(worktree) :
3345 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3348 head_ref_name = label;
3350 error = got_ref_open(&ref, repo, start_commit, 0);
3352 head_ref_name = got_ref_get_name(ref);
3353 else if (error->code != GOT_ERR_NOT_REF)
3355 error = got_repo_match_object_id(&start_id, NULL,
3356 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3361 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3363 error = got_error_from_errno("view_open");
3366 error = open_log_view(view, start_id, repo, head_ref_name,
3367 in_repo_path, log_branches);
3371 /* Release work tree lock. */
3372 got_worktree_close(worktree);
3375 error = view_loop(view);
3385 const struct got_error *close_err = got_repo_close(repo);
3390 got_worktree_close(worktree);
3392 const struct got_error *pack_err =
3393 got_repo_pack_fds_close(pack_fds);
3405 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3406 "[-w] object1 object2\n", getprogname());
3411 match_line(const char *line, regex_t *regex, size_t nmatch,
3412 regmatch_t *regmatch)
3414 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3417 static struct tog_color *
3418 match_color(struct tog_colors *colors, const char *line)
3420 struct tog_color *tc = NULL;
3422 STAILQ_FOREACH(tc, colors, entry) {
3423 if (match_line(line, &tc->regex, 0, NULL))
3430 static const struct got_error *
3431 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3432 WINDOW *window, int skipcol, regmatch_t *regmatch)
3434 const struct got_error *err = NULL;
3436 wchar_t *wline = NULL;
3437 int rme, rms, n, width, scrollx;
3438 int width0 = 0, width1 = 0, width2 = 0;
3439 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3443 rms = regmatch->rm_so;
3444 rme = regmatch->rm_eo;
3446 err = expand_tab(&exstr, line);
3450 /* Split the line into 3 segments, according to match offsets. */
3451 seg0 = strndup(exstr, rms);
3453 err = got_error_from_errno("strndup");
3456 seg1 = strndup(exstr + rms, rme - rms);
3458 err = got_error_from_errno("strndup");
3461 seg2 = strdup(exstr + rme);
3463 err = got_error_from_errno("strndup");
3467 /* draw up to matched token if we haven't scrolled past it */
3468 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3472 n = MAX(width0 - skipcol, 0);
3475 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3476 wlimit, col_tab_align, 1);
3479 waddwstr(window, &wline[scrollx]);
3489 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3493 wlen = wcslen(wline);
3495 width = wcwidth(wline[i]);
3497 /* should not happen, tabs are expanded */
3498 err = got_error(GOT_ERR_RANGE);
3501 if (width0 + w + width > skipcol)
3506 /* draw (visible part of) matched token (if scrolled into it) */
3507 if (width1 - w > 0) {
3508 wattron(window, A_STANDOUT);
3509 waddwstr(window, &wline[i]);
3510 wattroff(window, A_STANDOUT);
3511 wlimit -= (width1 - w);
3512 *wtotal += (width1 - w);
3516 if (wlimit > 0) { /* draw rest of line */
3518 if (skipcol > width0 + width1) {
3519 err = format_line(&wline, &width2, &scrollx, seg2,
3520 skipcol - (width0 + width1), wlimit,
3524 waddwstr(window, &wline[scrollx]);
3526 err = format_line(&wline, &width2, NULL, seg2, 0,
3527 wlimit, col_tab_align, 1);
3530 waddwstr(window, wline);
3543 static const struct got_error *
3544 draw_file(struct tog_view *view, const char *header)
3546 struct tog_diff_view_state *s = &view->state.diff;
3547 regmatch_t *regmatch = &view->regmatch;
3548 const struct got_error *err;
3551 size_t linesize = 0;
3553 struct tog_color *tc;
3556 int max_lines = view->nlines;
3557 int nlines = s->nlines;
3560 line_offset = s->line_offsets[s->first_displayed_line - 1];
3561 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3562 return got_error_from_errno("fseek");
3564 werase(view->window);
3567 if (asprintf(&line, "[%d/%d] %s",
3568 s->first_displayed_line - 1 + s->selected_line, nlines,
3570 return got_error_from_errno("asprintf");
3571 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3577 if (view_needs_focus_indication(view))
3578 wstandout(view->window);
3579 waddwstr(view->window, wline);
3582 if (view_needs_focus_indication(view))
3583 wstandend(view->window);
3584 if (width <= view->ncols - 1)
3585 waddch(view->window, '\n');
3595 while (max_lines > 0 && nprinted < max_lines) {
3596 linelen = getline(&line, &linesize, s->f);
3597 if (linelen == -1) {
3603 return got_ferror(s->f, GOT_ERR_IO);
3606 /* Set view->maxx based on full line length. */
3607 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3613 view->maxx = MAX(view->maxx, width);
3617 tc = match_color(&s->colors, line);
3619 wattr_on(view->window,
3620 COLOR_PAIR(tc->colorpair), NULL);
3621 if (s->first_displayed_line + nprinted == s->matched_line &&
3622 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3623 err = add_matched_line(&width, line, view->ncols, 0,
3624 view->window, view->x, regmatch);
3631 err = format_line(&wline, &width, &skip, line,
3632 view->x, view->ncols, 0, view->x ? 1 : 0);
3637 waddwstr(view->window, &wline[skip]);
3642 wattr_off(view->window,
3643 COLOR_PAIR(tc->colorpair), NULL);
3644 if (width <= view->ncols - 1)
3645 waddch(view->window, '\n');
3650 s->last_displayed_line = s->first_displayed_line +
3653 s->last_displayed_line = s->first_displayed_line;
3658 while (nprinted < view->nlines) {
3659 waddch(view->window, '\n');
3663 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3669 wstandout(view->window);
3670 waddwstr(view->window, wline);
3673 wstandend(view->window);
3680 get_datestr(time_t *time, char *datebuf)
3682 struct tm mytm, *tm;
3685 tm = gmtime_r(time, &mytm);
3688 s = asctime_r(tm, datebuf);
3691 p = strchr(s, '\n');
3697 static const struct got_error *
3698 get_changed_paths(struct got_pathlist_head *paths,
3699 struct got_commit_object *commit, struct got_repository *repo)
3701 const struct got_error *err = NULL;
3702 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3703 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3704 struct got_object_qid *qid;
3706 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3708 struct got_commit_object *pcommit;
3709 err = got_object_open_as_commit(&pcommit, repo,
3714 tree_id1 = got_object_id_dup(
3715 got_object_commit_get_tree_id(pcommit));
3716 if (tree_id1 == NULL) {
3717 got_object_commit_close(pcommit);
3718 return got_error_from_errno("got_object_id_dup");
3720 got_object_commit_close(pcommit);
3725 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3730 tree_id2 = got_object_commit_get_tree_id(commit);
3731 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3735 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3736 got_diff_tree_collect_changed_paths, paths, 0);
3739 got_object_tree_close(tree1);
3741 got_object_tree_close(tree2);
3746 static const struct got_error *
3747 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3751 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3753 return got_error_from_errno("reallocarray");
3755 (*line_offsets)[*nlines] = off;
3760 static const struct got_error *
3761 write_commit_info(off_t **line_offsets, size_t *nlines,
3762 struct got_object_id *commit_id, struct got_reflist_head *refs,
3763 struct got_repository *repo, FILE *outfile)
3765 const struct got_error *err = NULL;
3766 char datebuf[26], *datestr;
3767 struct got_commit_object *commit;
3768 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3769 time_t committer_time;
3770 const char *author, *committer;
3771 char *refs_str = NULL;
3772 struct got_pathlist_head changed_paths;
3773 struct got_pathlist_entry *pe;
3777 TAILQ_INIT(&changed_paths);
3780 err = build_refs_str(&refs_str, refs, commit_id, repo);
3785 err = got_object_open_as_commit(&commit, repo, commit_id);
3789 err = got_object_id_str(&id_str, commit_id);
3791 err = got_error_from_errno("got_object_id_str");
3795 err = add_line_offset(line_offsets, nlines, 0);
3799 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3800 refs_str ? refs_str : "", refs_str ? ")" : "");
3802 err = got_error_from_errno("fprintf");
3806 err = add_line_offset(line_offsets, nlines, outoff);
3810 n = fprintf(outfile, "from: %s\n",
3811 got_object_commit_get_author(commit));
3813 err = got_error_from_errno("fprintf");
3817 err = add_line_offset(line_offsets, nlines, outoff);
3821 committer_time = got_object_commit_get_committer_time(commit);
3822 datestr = get_datestr(&committer_time, datebuf);
3824 n = fprintf(outfile, "date: %s UTC\n", datestr);
3826 err = got_error_from_errno("fprintf");
3830 err = add_line_offset(line_offsets, nlines, outoff);
3834 author = got_object_commit_get_author(commit);
3835 committer = got_object_commit_get_committer(commit);
3836 if (strcmp(author, committer) != 0) {
3837 n = fprintf(outfile, "via: %s\n", committer);
3839 err = got_error_from_errno("fprintf");
3843 err = add_line_offset(line_offsets, nlines, outoff);
3847 if (got_object_commit_get_nparents(commit) > 1) {
3848 const struct got_object_id_queue *parent_ids;
3849 struct got_object_qid *qid;
3851 parent_ids = got_object_commit_get_parent_ids(commit);
3852 STAILQ_FOREACH(qid, parent_ids, entry) {
3853 err = got_object_id_str(&id_str, &qid->id);
3856 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3858 err = got_error_from_errno("fprintf");
3862 err = add_line_offset(line_offsets, nlines, outoff);
3870 err = got_object_commit_get_logmsg(&logmsg, commit);
3874 while ((line = strsep(&s, "\n")) != NULL) {
3875 n = fprintf(outfile, "%s\n", line);
3877 err = got_error_from_errno("fprintf");
3881 err = add_line_offset(line_offsets, nlines, outoff);
3886 err = get_changed_paths(&changed_paths, commit, repo);
3889 TAILQ_FOREACH(pe, &changed_paths, entry) {
3890 struct got_diff_changed_path *cp = pe->data;
3891 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3893 err = got_error_from_errno("fprintf");
3897 err = add_line_offset(line_offsets, nlines, outoff);
3900 free((char *)pe->path);
3904 fputc('\n', outfile);
3906 err = add_line_offset(line_offsets, nlines, outoff);
3908 got_pathlist_free(&changed_paths);
3912 got_object_commit_close(commit);
3914 free(*line_offsets);
3915 *line_offsets = NULL;
3921 static const struct got_error *
3922 create_diff(struct tog_diff_view_state *s)
3924 const struct got_error *err = NULL;
3928 free(s->line_offsets);
3929 s->line_offsets = malloc(sizeof(off_t));
3930 if (s->line_offsets == NULL)
3931 return got_error_from_errno("malloc");
3936 err = got_error_from_errno("got_opentemp");
3939 if (s->f && fclose(s->f) == EOF) {
3940 err = got_error_from_errno("fclose");
3946 err = got_object_get_type(&obj_type, s->repo, s->id1);
3948 err = got_object_get_type(&obj_type, s->repo, s->id2);
3953 case GOT_OBJ_TYPE_BLOB:
3954 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3955 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
3956 s->label1, s->label2, s->diff_context,
3957 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3959 case GOT_OBJ_TYPE_TREE:
3960 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3961 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
3962 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3965 case GOT_OBJ_TYPE_COMMIT: {
3966 const struct got_object_id_queue *parent_ids;
3967 struct got_object_qid *pid;
3968 struct got_commit_object *commit2;
3969 struct got_reflist_head *refs;
3971 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3974 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3975 /* Show commit info if we're diffing to a parent/root commit. */
3976 if (s->id1 == NULL) {
3977 err = write_commit_info(&s->line_offsets, &s->nlines,
3978 s->id2, refs, s->repo, s->f);
3982 parent_ids = got_object_commit_get_parent_ids(commit2);
3983 STAILQ_FOREACH(pid, parent_ids, entry) {
3984 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3985 err = write_commit_info(
3986 &s->line_offsets, &s->nlines,
3987 s->id2, refs, s->repo, s->f);
3994 got_object_commit_close(commit2);
3996 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3997 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
3998 s->diff_context, s->ignore_whitespace, s->force_text_diff,
4003 err = got_error(GOT_ERR_OBJ_TYPE);
4009 if (s->f && fflush(s->f) != 0 && err == NULL)
4010 err = got_error_from_errno("fflush");
4015 diff_view_indicate_progress(struct tog_view *view)
4017 mvwaddstr(view->window, 0, 0, "diffing...");
4022 static const struct got_error *
4023 search_start_diff_view(struct tog_view *view)
4025 struct tog_diff_view_state *s = &view->state.diff;
4027 s->matched_line = 0;
4031 static const struct got_error *
4032 search_next_diff_view(struct tog_view *view)
4034 struct tog_diff_view_state *s = &view->state.diff;
4035 const struct got_error *err = NULL;
4038 size_t linesize = 0;
4041 if (!view->searching) {
4042 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4046 if (s->matched_line) {
4047 if (view->searching == TOG_SEARCH_FORWARD)
4048 lineno = s->matched_line + 1;
4050 lineno = s->matched_line - 1;
4052 lineno = s->first_displayed_line;
4057 if (lineno <= 0 || lineno > s->nlines) {
4058 if (s->matched_line == 0) {
4059 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4063 if (view->searching == TOG_SEARCH_FORWARD)
4069 offset = s->line_offsets[lineno - 1];
4070 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4072 return got_error_from_errno("fseeko");
4074 linelen = getline(&line, &linesize, s->f);
4075 if (linelen != -1) {
4077 err = expand_tab(&exstr, line);
4080 if (match_line(exstr, &view->regex, 1,
4082 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4083 s->matched_line = lineno;
4089 if (view->searching == TOG_SEARCH_FORWARD)
4096 if (s->matched_line) {
4097 s->first_displayed_line = s->matched_line;
4098 s->selected_line = 1;
4104 static const struct got_error *
4105 close_diff_view(struct tog_view *view)
4107 const struct got_error *err = NULL;
4108 struct tog_diff_view_state *s = &view->state.diff;
4114 if (s->f && fclose(s->f) == EOF)
4115 err = got_error_from_errno("fclose");
4117 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4118 err = got_error_from_errno("fclose");
4120 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4121 err = got_error_from_errno("fclose");
4123 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4124 err = got_error_from_errno("close");
4126 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4127 err = got_error_from_errno("close");
4129 free_colors(&s->colors);
4130 free(s->line_offsets);
4131 s->line_offsets = NULL;
4136 static const struct got_error *
4137 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4138 struct got_object_id *id2, const char *label1, const char *label2,
4139 int diff_context, int ignore_whitespace, int force_text_diff,
4140 struct tog_view *log_view, struct got_repository *repo)
4142 const struct got_error *err;
4143 struct tog_diff_view_state *s = &view->state.diff;
4145 memset(s, 0, sizeof(*s));
4149 if (id1 != NULL && id2 != NULL) {
4151 err = got_object_get_type(&type1, repo, id1);
4154 err = got_object_get_type(&type2, repo, id2);
4159 return got_error(GOT_ERR_OBJ_TYPE);
4161 s->first_displayed_line = 1;
4162 s->last_displayed_line = view->nlines;
4163 s->selected_line = 1;
4171 s->id1 = got_object_id_dup(id1);
4173 return got_error_from_errno("got_object_id_dup");
4177 s->id2 = got_object_id_dup(id2);
4178 if (s->id2 == NULL) {
4179 err = got_error_from_errno("got_object_id_dup");
4183 s->f1 = got_opentemp();
4184 if (s->f1 == NULL) {
4185 err = got_error_from_errno("got_opentemp");
4189 s->f2 = got_opentemp();
4190 if (s->f2 == NULL) {
4191 err = got_error_from_errno("got_opentemp");
4195 s->fd1 = got_opentempfd();
4197 err = got_error_from_errno("got_opentempfd");
4201 s->fd2 = got_opentempfd();
4203 err = got_error_from_errno("got_opentempfd");
4207 s->first_displayed_line = 1;
4208 s->last_displayed_line = view->nlines;
4209 s->diff_context = diff_context;
4210 s->ignore_whitespace = ignore_whitespace;
4211 s->force_text_diff = force_text_diff;
4212 s->log_view = log_view;
4215 STAILQ_INIT(&s->colors);
4216 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4217 err = add_color(&s->colors,
4218 "^-", TOG_COLOR_DIFF_MINUS,
4219 get_color_value("TOG_COLOR_DIFF_MINUS"));
4222 err = add_color(&s->colors, "^\\+",
4223 TOG_COLOR_DIFF_PLUS,
4224 get_color_value("TOG_COLOR_DIFF_PLUS"));
4227 err = add_color(&s->colors,
4228 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4229 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4233 err = add_color(&s->colors,
4234 "^(commit [0-9a-f]|parent [0-9]|"
4235 "(blob|file|tree|commit) [-+] |"
4236 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4237 get_color_value("TOG_COLOR_DIFF_META"));
4241 err = add_color(&s->colors,
4242 "^(from|via): ", TOG_COLOR_AUTHOR,
4243 get_color_value("TOG_COLOR_AUTHOR"));
4247 err = add_color(&s->colors,
4248 "^date: ", TOG_COLOR_DATE,
4249 get_color_value("TOG_COLOR_DATE"));
4254 if (log_view && view_is_splitscreen(view))
4255 show_log_view(log_view); /* draw vborder */
4256 diff_view_indicate_progress(view);
4258 err = create_diff(s);
4260 view->show = show_diff_view;
4261 view->input = input_diff_view;
4262 view->close = close_diff_view;
4263 view->search_start = search_start_diff_view;
4264 view->search_next = search_next_diff_view;
4267 close_diff_view(view);
4271 static const struct got_error *
4272 show_diff_view(struct tog_view *view)
4274 const struct got_error *err;
4275 struct tog_diff_view_state *s = &view->state.diff;
4276 char *id_str1 = NULL, *id_str2, *header;
4277 const char *label1, *label2;
4280 err = got_object_id_str(&id_str1, s->id1);
4283 label1 = s->label1 ? : id_str1;
4285 label1 = "/dev/null";
4287 err = got_object_id_str(&id_str2, s->id2);
4290 label2 = s->label2 ? : id_str2;
4292 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4293 err = got_error_from_errno("asprintf");
4301 err = draw_file(view, header);
4306 static const struct got_error *
4307 set_selected_commit(struct tog_diff_view_state *s,
4308 struct commit_queue_entry *entry)
4310 const struct got_error *err;
4311 const struct got_object_id_queue *parent_ids;
4312 struct got_commit_object *selected_commit;
4313 struct got_object_qid *pid;
4316 s->id2 = got_object_id_dup(entry->id);
4318 return got_error_from_errno("got_object_id_dup");
4320 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4323 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4325 pid = STAILQ_FIRST(parent_ids);
4326 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4327 got_object_commit_close(selected_commit);
4331 static const struct got_error *
4332 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4334 const struct got_error *err = NULL;
4335 struct tog_diff_view_state *s = &view->state.diff;
4336 struct tog_log_view_state *ls;
4337 struct commit_queue_entry *old_selected_entry;
4339 size_t linesize = 0;
4341 int i, nscroll = view->nlines - 1;
4348 view->x = MAX(view->maxx - view->ncols / 3, 0);
4353 if (view->x + view->ncols / 3 < view->maxx)
4354 view->x += 2; /* move two columns right */
4360 view->x -= MIN(view->x, 2); /* move two columns back */
4367 s->force_text_diff = !s->force_text_diff;
4369 s->ignore_whitespace = !s->ignore_whitespace;
4370 wclear(view->window);
4371 s->first_displayed_line = 1;
4372 s->last_displayed_line = view->nlines;
4373 s->matched_line = 0;
4374 diff_view_indicate_progress(view);
4375 err = create_diff(s);
4380 s->first_displayed_line = 1;
4389 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4395 if (s->first_displayed_line > 1)
4396 s->first_displayed_line--;
4407 if (s->first_displayed_line == 1) {
4412 while (i++ < nscroll && s->first_displayed_line > 1)
4413 s->first_displayed_line--;
4419 s->first_displayed_line++;
4436 while (!s->eof && i++ < nscroll) {
4437 linelen = getline(&line, &linesize, s->f);
4438 s->first_displayed_line++;
4439 if (linelen == -1) {
4443 err = got_ferror(s->f, GOT_ERR_IO);
4450 if (s->diff_context > 0) {
4452 s->matched_line = 0;
4453 diff_view_indicate_progress(view);
4454 err = create_diff(s);
4455 if (s->first_displayed_line + view->nlines - 1 >
4457 s->first_displayed_line = 1;
4458 s->last_displayed_line = view->nlines;
4464 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4466 s->matched_line = 0;
4467 diff_view_indicate_progress(view);
4468 err = create_diff(s);
4474 if (s->log_view == NULL) {
4478 ls = &s->log_view->state.log;
4479 old_selected_entry = ls->selected_entry;
4481 /* view->count handled in input_log_view() */
4482 err = input_log_view(NULL, s->log_view, KEY_UP);
4486 if (old_selected_entry == ls->selected_entry)
4489 err = set_selected_commit(s, ls->selected_entry);
4493 s->first_displayed_line = 1;
4494 s->last_displayed_line = view->nlines;
4495 s->matched_line = 0;
4498 diff_view_indicate_progress(view);
4499 err = create_diff(s);
4503 if (s->log_view == NULL) {
4507 ls = &s->log_view->state.log;
4508 old_selected_entry = ls->selected_entry;
4510 /* view->count handled in input_log_view() */
4511 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4515 if (old_selected_entry == ls->selected_entry)
4518 err = set_selected_commit(s, ls->selected_entry);
4522 s->first_displayed_line = 1;
4523 s->last_displayed_line = view->nlines;
4524 s->matched_line = 0;
4527 diff_view_indicate_progress(view);
4528 err = create_diff(s);
4538 static const struct got_error *
4539 cmd_diff(int argc, char *argv[])
4541 const struct got_error *error = NULL;
4542 struct got_repository *repo = NULL;
4543 struct got_worktree *worktree = NULL;
4544 struct got_object_id *id1 = NULL, *id2 = NULL;
4545 char *repo_path = NULL, *cwd = NULL;
4546 char *id_str1 = NULL, *id_str2 = NULL;
4547 char *label1 = NULL, *label2 = NULL;
4548 int diff_context = 3, ignore_whitespace = 0;
4549 int ch, force_text_diff = 0;
4551 struct tog_view *view;
4552 int *pack_fds = NULL;
4554 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4557 force_text_diff = 1;
4560 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4563 errx(1, "number of context lines is %s: %s",
4567 repo_path = realpath(optarg, NULL);
4568 if (repo_path == NULL)
4569 return got_error_from_errno2("realpath",
4571 got_path_strip_trailing_slashes(repo_path);
4574 ignore_whitespace = 1;
4586 usage_diff(); /* TODO show local worktree changes */
4587 } else if (argc == 2) {
4593 error = got_repo_pack_fds_open(&pack_fds);
4597 if (repo_path == NULL) {
4598 cwd = getcwd(NULL, 0);
4600 return got_error_from_errno("getcwd");
4601 error = got_worktree_open(&worktree, cwd);
4602 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4606 strdup(got_worktree_get_repo_path(worktree));
4608 repo_path = strdup(cwd);
4609 if (repo_path == NULL) {
4610 error = got_error_from_errno("strdup");
4615 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4621 error = apply_unveil(got_repo_get_path(repo), NULL);
4625 error = tog_load_refs(repo, 0);
4629 error = got_repo_match_object_id(&id1, &label1, id_str1,
4630 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4634 error = got_repo_match_object_id(&id2, &label2, id_str2,
4635 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4639 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4641 error = got_error_from_errno("view_open");
4644 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4645 ignore_whitespace, force_text_diff, NULL, repo);
4648 error = view_loop(view);
4655 const struct got_error *close_err = got_repo_close(repo);
4660 got_worktree_close(worktree);
4662 const struct got_error *pack_err =
4663 got_repo_pack_fds_close(pack_fds);
4676 "usage: %s blame [-c commit] [-r repository-path] path\n",
4681 struct tog_blame_line {
4683 struct got_object_id *id;
4686 static const struct got_error *
4687 draw_blame(struct tog_view *view)
4689 struct tog_blame_view_state *s = &view->state.blame;
4690 struct tog_blame *blame = &s->blame;
4691 regmatch_t *regmatch = &view->regmatch;
4692 const struct got_error *err;
4693 int lineno = 0, nprinted = 0;
4695 size_t linesize = 0;
4699 struct tog_blame_line *blame_line;
4700 struct got_object_id *prev_id = NULL;
4702 struct tog_color *tc;
4704 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4709 werase(view->window);
4711 if (asprintf(&line, "commit %s", id_str) == -1) {
4712 err = got_error_from_errno("asprintf");
4717 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4722 if (view_needs_focus_indication(view))
4723 wstandout(view->window);
4724 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4726 wattr_on(view->window,
4727 COLOR_PAIR(tc->colorpair), NULL);
4728 waddwstr(view->window, wline);
4730 wattr_off(view->window,
4731 COLOR_PAIR(tc->colorpair), NULL);
4732 if (view_needs_focus_indication(view))
4733 wstandend(view->window);
4736 if (width < view->ncols - 1)
4737 waddch(view->window, '\n');
4739 if (asprintf(&line, "[%d/%d] %s%s",
4740 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4741 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4743 return got_error_from_errno("asprintf");
4746 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4751 waddwstr(view->window, wline);
4754 if (width < view->ncols - 1)
4755 waddch(view->window, '\n');
4759 while (nprinted < view->nlines - 2) {
4760 linelen = getline(&line, &linesize, blame->f);
4761 if (linelen == -1) {
4762 if (feof(blame->f)) {
4767 return got_ferror(blame->f, GOT_ERR_IO);
4769 if (++lineno < s->first_displayed_line)
4772 /* Set view->maxx based on full line length. */
4773 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4780 view->maxx = MAX(view->maxx, width);
4782 if (view->focussed && nprinted == s->selected_line - 1)
4783 wstandout(view->window);
4785 if (blame->nlines > 0) {
4786 blame_line = &blame->lines[lineno - 1];
4787 if (blame_line->annotated && prev_id &&
4788 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4790 nprinted == s->selected_line - 1)) {
4791 waddstr(view->window, " ");
4792 } else if (blame_line->annotated) {
4794 err = got_object_id_str(&id_str,
4800 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4802 wattr_on(view->window,
4803 COLOR_PAIR(tc->colorpair), NULL);
4804 wprintw(view->window, "%.8s", id_str);
4806 wattr_off(view->window,
4807 COLOR_PAIR(tc->colorpair), NULL);
4809 prev_id = blame_line->id;
4811 waddstr(view->window, "........");
4815 waddstr(view->window, "........");
4819 if (view->focussed && nprinted == s->selected_line - 1)
4820 wstandend(view->window);
4821 waddstr(view->window, " ");
4823 if (view->ncols <= 9) {
4825 } else if (s->first_displayed_line + nprinted ==
4827 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4828 err = add_matched_line(&width, line, view->ncols - 9, 9,
4829 view->window, view->x, regmatch);
4837 err = format_line(&wline, &width, &skip, line,
4838 view->x, view->ncols - 9, 9, 1);
4843 waddwstr(view->window, &wline[skip]);
4849 if (width <= view->ncols - 1)
4850 waddch(view->window, '\n');
4851 if (++nprinted == 1)
4852 s->first_displayed_line = lineno;
4855 s->last_displayed_line = lineno;
4862 static const struct got_error *
4863 blame_cb(void *arg, int nlines, int lineno,
4864 struct got_commit_object *commit, struct got_object_id *id)
4866 const struct got_error *err = NULL;
4867 struct tog_blame_cb_args *a = arg;
4868 struct tog_blame_line *line;
4871 if (nlines != a->nlines ||
4872 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4873 return got_error(GOT_ERR_RANGE);
4875 errcode = pthread_mutex_lock(&tog_mutex);
4877 return got_error_set_errno(errcode, "pthread_mutex_lock");
4879 if (*a->quit) { /* user has quit the blame view */
4880 err = got_error(GOT_ERR_ITER_COMPLETED);
4885 goto done; /* no change in this commit */
4887 line = &a->lines[lineno - 1];
4888 if (line->annotated)
4891 line->id = got_object_id_dup(id);
4892 if (line->id == NULL) {
4893 err = got_error_from_errno("got_object_id_dup");
4896 line->annotated = 1;
4898 errcode = pthread_mutex_unlock(&tog_mutex);
4900 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4905 blame_thread(void *arg)
4907 const struct got_error *err, *close_err;
4908 struct tog_blame_thread_args *ta = arg;
4909 struct tog_blame_cb_args *a = ta->cb_args;
4910 int errcode, fd1 = -1, fd2 = -1;
4911 FILE *f1 = NULL, *f2 = NULL;
4913 fd1 = got_opentempfd();
4915 return (void *)got_error_from_errno("got_opentempfd");
4917 fd2 = got_opentempfd();
4919 err = got_error_from_errno("got_opentempfd");
4923 f1 = got_opentemp();
4925 err = (void *)got_error_from_errno("got_opentemp");
4928 f2 = got_opentemp();
4930 err = (void *)got_error_from_errno("got_opentemp");
4934 err = block_signals_used_by_main_thread();
4938 err = got_blame(ta->path, a->commit_id, ta->repo,
4939 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1,
4941 if (err && err->code == GOT_ERR_CANCELLED)
4944 errcode = pthread_mutex_lock(&tog_mutex);
4946 err = got_error_set_errno(errcode, "pthread_mutex_lock");
4950 close_err = got_repo_close(ta->repo);
4956 errcode = pthread_mutex_unlock(&tog_mutex);
4957 if (errcode && err == NULL)
4958 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4961 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4962 err = got_error_from_errno("close");
4963 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4964 err = got_error_from_errno("close");
4965 if (f1 && fclose(f1) == EOF && err == NULL)
4966 err = got_error_from_errno("fclose");
4967 if (f2 && fclose(f2) == EOF && err == NULL)
4968 err = got_error_from_errno("fclose");
4973 static struct got_object_id *
4974 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4975 int first_displayed_line, int selected_line)
4977 struct tog_blame_line *line;
4982 line = &lines[first_displayed_line - 1 + selected_line - 1];
4983 if (!line->annotated)
4989 static const struct got_error *
4990 stop_blame(struct tog_blame *blame)
4992 const struct got_error *err = NULL;
4995 if (blame->thread) {
4997 errcode = pthread_mutex_unlock(&tog_mutex);
4999 return got_error_set_errno(errcode,
5000 "pthread_mutex_unlock");
5001 errcode = pthread_join(blame->thread, (void **)&err);
5003 return got_error_set_errno(errcode, "pthread_join");
5004 errcode = pthread_mutex_lock(&tog_mutex);
5006 return got_error_set_errno(errcode,
5007 "pthread_mutex_lock");
5008 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5010 blame->thread = NULL;
5012 if (blame->thread_args.repo) {
5013 const struct got_error *close_err;
5014 close_err = got_repo_close(blame->thread_args.repo);
5017 blame->thread_args.repo = NULL;
5020 if (fclose(blame->f) == EOF && err == NULL)
5021 err = got_error_from_errno("fclose");
5025 for (i = 0; i < blame->nlines; i++)
5026 free(blame->lines[i].id);
5028 blame->lines = NULL;
5030 free(blame->cb_args.commit_id);
5031 blame->cb_args.commit_id = NULL;
5032 if (blame->pack_fds) {
5033 const struct got_error *pack_err =
5034 got_repo_pack_fds_close(blame->pack_fds);
5037 blame->pack_fds = NULL;
5042 static const struct got_error *
5043 cancel_blame_view(void *arg)
5045 const struct got_error *err = NULL;
5049 errcode = pthread_mutex_lock(&tog_mutex);
5051 return got_error_set_errno(errcode,
5052 "pthread_mutex_unlock");
5055 err = got_error(GOT_ERR_CANCELLED);
5057 errcode = pthread_mutex_unlock(&tog_mutex);
5059 return got_error_set_errno(errcode,
5060 "pthread_mutex_lock");
5065 static const struct got_error *
5066 run_blame(struct tog_view *view)
5068 struct tog_blame_view_state *s = &view->state.blame;
5069 struct tog_blame *blame = &s->blame;
5070 const struct got_error *err = NULL;
5071 struct got_commit_object *commit = NULL;
5072 struct got_blob_object *blob = NULL;
5073 struct got_repository *thread_repo = NULL;
5074 struct got_object_id *obj_id = NULL;
5075 int obj_type, fd = -1;
5076 int *pack_fds = NULL;
5078 err = got_object_open_as_commit(&commit, s->repo,
5079 &s->blamed_commit->id);
5083 fd = got_opentempfd();
5085 err = got_error_from_errno("got_opentempfd");
5089 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5093 err = got_object_get_type(&obj_type, s->repo, obj_id);
5097 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5098 err = got_error(GOT_ERR_OBJ_TYPE);
5102 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5105 blame->f = got_opentemp();
5106 if (blame->f == NULL) {
5107 err = got_error_from_errno("got_opentemp");
5110 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5111 &blame->line_offsets, blame->f, blob);
5114 if (blame->nlines == 0) {
5115 s->blame_complete = 1;
5119 /* Don't include \n at EOF in the blame line count. */
5120 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5123 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5124 if (blame->lines == NULL) {
5125 err = got_error_from_errno("calloc");
5129 err = got_repo_pack_fds_open(&pack_fds);
5132 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5137 blame->pack_fds = pack_fds;
5138 blame->cb_args.view = view;
5139 blame->cb_args.lines = blame->lines;
5140 blame->cb_args.nlines = blame->nlines;
5141 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5142 if (blame->cb_args.commit_id == NULL) {
5143 err = got_error_from_errno("got_object_id_dup");
5146 blame->cb_args.quit = &s->done;
5148 blame->thread_args.path = s->path;
5149 blame->thread_args.repo = thread_repo;
5150 blame->thread_args.cb_args = &blame->cb_args;
5151 blame->thread_args.complete = &s->blame_complete;
5152 blame->thread_args.cancel_cb = cancel_blame_view;
5153 blame->thread_args.cancel_arg = &s->done;
5154 s->blame_complete = 0;
5156 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5157 s->first_displayed_line = 1;
5158 s->last_displayed_line = view->nlines;
5159 s->selected_line = 1;
5161 s->matched_line = 0;
5165 got_object_commit_close(commit);
5166 if (fd != -1 && close(fd) == -1 && err == NULL)
5167 err = got_error_from_errno("close");
5169 got_object_blob_close(blob);
5176 static const struct got_error *
5177 open_blame_view(struct tog_view *view, char *path,
5178 struct got_object_id *commit_id, struct got_repository *repo)
5180 const struct got_error *err = NULL;
5181 struct tog_blame_view_state *s = &view->state.blame;
5183 STAILQ_INIT(&s->blamed_commits);
5185 s->path = strdup(path);
5186 if (s->path == NULL)
5187 return got_error_from_errno("strdup");
5189 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5195 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5196 s->first_displayed_line = 1;
5197 s->last_displayed_line = view->nlines;
5198 s->selected_line = 1;
5199 s->blame_complete = 0;
5201 s->commit_id = commit_id;
5202 memset(&s->blame, 0, sizeof(s->blame));
5204 STAILQ_INIT(&s->colors);
5205 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5206 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5207 get_color_value("TOG_COLOR_COMMIT"));
5212 view->show = show_blame_view;
5213 view->input = input_blame_view;
5214 view->close = close_blame_view;
5215 view->search_start = search_start_blame_view;
5216 view->search_next = search_next_blame_view;
5218 return run_blame(view);
5221 static const struct got_error *
5222 close_blame_view(struct tog_view *view)
5224 const struct got_error *err = NULL;
5225 struct tog_blame_view_state *s = &view->state.blame;
5227 if (s->blame.thread)
5228 err = stop_blame(&s->blame);
5230 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5231 struct got_object_qid *blamed_commit;
5232 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5233 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5234 got_object_qid_free(blamed_commit);
5238 free_colors(&s->colors);
5242 static const struct got_error *
5243 search_start_blame_view(struct tog_view *view)
5245 struct tog_blame_view_state *s = &view->state.blame;
5247 s->matched_line = 0;
5251 static const struct got_error *
5252 search_next_blame_view(struct tog_view *view)
5254 struct tog_blame_view_state *s = &view->state.blame;
5255 const struct got_error *err = NULL;
5258 size_t linesize = 0;
5261 if (!view->searching) {
5262 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5266 if (s->matched_line) {
5267 if (view->searching == TOG_SEARCH_FORWARD)
5268 lineno = s->matched_line + 1;
5270 lineno = s->matched_line - 1;
5272 lineno = s->first_displayed_line - 1 + s->selected_line;
5277 if (lineno <= 0 || lineno > s->blame.nlines) {
5278 if (s->matched_line == 0) {
5279 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5283 if (view->searching == TOG_SEARCH_FORWARD)
5286 lineno = s->blame.nlines;
5289 offset = s->blame.line_offsets[lineno - 1];
5290 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5292 return got_error_from_errno("fseeko");
5294 linelen = getline(&line, &linesize, s->blame.f);
5295 if (linelen != -1) {
5297 err = expand_tab(&exstr, line);
5300 if (match_line(exstr, &view->regex, 1,
5302 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5303 s->matched_line = lineno;
5309 if (view->searching == TOG_SEARCH_FORWARD)
5316 if (s->matched_line) {
5317 s->first_displayed_line = s->matched_line;
5318 s->selected_line = 1;
5324 static const struct got_error *
5325 show_blame_view(struct tog_view *view)
5327 const struct got_error *err = NULL;
5328 struct tog_blame_view_state *s = &view->state.blame;
5331 if (s->blame.thread == NULL && !s->blame_complete) {
5332 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5333 &s->blame.thread_args);
5335 return got_error_set_errno(errcode, "pthread_create");
5337 halfdelay(1); /* fast refresh while annotating */
5340 if (s->blame_complete)
5341 halfdelay(10); /* disable fast refresh */
5343 err = draw_blame(view);
5349 static const struct got_error *
5350 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5352 const struct got_error *err = NULL, *thread_err = NULL;
5353 struct tog_view *diff_view;
5354 struct tog_blame_view_state *s = &view->state.blame;
5355 int eos, nscroll, begin_y = 0, begin_x = 0;
5357 eos = nscroll = view->nlines - 2;
5358 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5359 view_is_splitscreen(view->child))
5367 view->x = MAX(view->maxx - view->ncols / 3, 0);
5372 if (view->x + view->ncols / 3 < view->maxx)
5373 view->x += 2; /* move two columns right */
5379 view->x -= MIN(view->x, 2); /* move two columns back */
5388 s->selected_line = 1;
5389 s->first_displayed_line = 1;
5394 if (s->blame.nlines < eos) {
5395 s->selected_line = s->blame.nlines;
5396 s->first_displayed_line = 1;
5398 s->selected_line = eos;
5399 s->first_displayed_line = s->blame.nlines - (eos - 1);
5406 if (s->selected_line > 1)
5408 else if (s->selected_line == 1 &&
5409 s->first_displayed_line > 1)
5410 s->first_displayed_line--;
5421 if (s->first_displayed_line == 1) {
5422 if (view->count > 1)
5424 s->selected_line = MAX(1, s->selected_line - nscroll);
5428 if (s->first_displayed_line > nscroll)
5429 s->first_displayed_line -= nscroll;
5431 s->first_displayed_line = 1;
5436 if (s->selected_line < eos && s->first_displayed_line +
5437 s->selected_line <= s->blame.nlines)
5439 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5440 s->first_displayed_line++;
5446 struct got_object_id *id = NULL;
5449 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5450 s->first_displayed_line, s->selected_line);
5454 struct got_commit_object *commit, *pcommit;
5455 struct got_object_qid *pid;
5456 struct got_object_id *blob_id = NULL;
5458 err = got_object_open_as_commit(&commit,
5463 got_object_commit_get_parent_ids(commit));
5465 got_object_commit_close(commit);
5468 /* Check if path history ends here. */
5469 err = got_object_open_as_commit(&pcommit,
5473 err = got_object_id_by_path(&blob_id, s->repo,
5475 got_object_commit_close(pcommit);
5477 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5479 got_object_commit_close(commit);
5482 err = got_object_get_type(&obj_type, s->repo,
5485 /* Can't blame non-blob type objects. */
5486 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5487 got_object_commit_close(commit);
5490 err = got_object_qid_alloc(&s->blamed_commit,
5492 got_object_commit_close(commit);
5494 if (got_object_id_cmp(id,
5495 &s->blamed_commit->id) == 0)
5497 err = got_object_qid_alloc(&s->blamed_commit,
5503 thread_err = stop_blame(&s->blame);
5507 STAILQ_INSERT_HEAD(&s->blamed_commits,
5508 s->blamed_commit, entry);
5509 err = run_blame(view);
5515 struct got_object_qid *first;
5518 first = STAILQ_FIRST(&s->blamed_commits);
5519 if (!got_object_id_cmp(&first->id, s->commit_id))
5522 thread_err = stop_blame(&s->blame);
5526 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5527 got_object_qid_free(s->blamed_commit);
5529 STAILQ_FIRST(&s->blamed_commits);
5530 err = run_blame(view);
5537 struct got_object_id *id = NULL;
5538 struct got_object_qid *pid;
5539 struct got_commit_object *commit = NULL;
5542 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5543 s->first_displayed_line, s->selected_line);
5546 err = got_object_open_as_commit(&commit, s->repo, id);
5549 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5550 if (view_is_parent_view(view))
5551 view_get_split(view, &begin_y, &begin_x);
5553 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
5554 if (diff_view == NULL) {
5555 got_object_commit_close(commit);
5556 err = got_error_from_errno("view_open");
5559 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5560 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5561 got_object_commit_close(commit);
5563 view_close(diff_view);
5566 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
5567 err = view_init_hsplit(view, begin_y);
5573 diff_view->focussed = 1;
5574 diff_view->mode = view->mode;
5575 diff_view->nlines = view->lines - begin_y;
5576 if (view_is_parent_view(view)) {
5577 err = view_close_child(view);
5580 err = view_set_child(view, diff_view);
5583 view->focus_child = 1;
5585 *new_view = diff_view;
5598 if (s->last_displayed_line >= s->blame.nlines &&
5599 s->selected_line >= MIN(s->blame.nlines,
5600 view->nlines - 2)) {
5604 if (s->last_displayed_line >= s->blame.nlines &&
5605 s->selected_line < view->nlines - 2) {
5607 MIN(nscroll, s->last_displayed_line -
5608 s->first_displayed_line - s->selected_line + 1);
5610 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5611 s->first_displayed_line += nscroll;
5613 s->first_displayed_line =
5614 s->blame.nlines - (view->nlines - 3);
5617 if (s->selected_line > view->nlines - 2) {
5618 s->selected_line = MIN(s->blame.nlines,
5626 return thread_err ? thread_err : err;
5629 static const struct got_error *
5630 cmd_blame(int argc, char *argv[])
5632 const struct got_error *error;
5633 struct got_repository *repo = NULL;
5634 struct got_worktree *worktree = NULL;
5635 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5636 char *link_target = NULL;
5637 struct got_object_id *commit_id = NULL;
5638 struct got_commit_object *commit = NULL;
5639 char *commit_id_str = NULL;
5641 struct tog_view *view;
5642 int *pack_fds = NULL;
5644 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5647 commit_id_str = optarg;
5650 repo_path = realpath(optarg, NULL);
5651 if (repo_path == NULL)
5652 return got_error_from_errno2("realpath",
5667 error = got_repo_pack_fds_open(&pack_fds);
5671 if (repo_path == NULL) {
5672 cwd = getcwd(NULL, 0);
5674 return got_error_from_errno("getcwd");
5675 error = got_worktree_open(&worktree, cwd);
5676 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5680 strdup(got_worktree_get_repo_path(worktree));
5682 repo_path = strdup(cwd);
5683 if (repo_path == NULL) {
5684 error = got_error_from_errno("strdup");
5689 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5693 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5700 error = apply_unveil(got_repo_get_path(repo), NULL);
5704 error = tog_load_refs(repo, 0);
5708 if (commit_id_str == NULL) {
5709 struct got_reference *head_ref;
5710 error = got_ref_open(&head_ref, repo, worktree ?
5711 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5714 error = got_ref_resolve(&commit_id, repo, head_ref);
5715 got_ref_close(head_ref);
5717 error = got_repo_match_object_id(&commit_id, NULL,
5718 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5723 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5725 error = got_error_from_errno("view_open");
5729 error = got_object_open_as_commit(&commit, repo, commit_id);
5733 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5738 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5743 /* Release work tree lock. */
5744 got_worktree_close(worktree);
5747 error = view_loop(view);
5755 got_object_commit_close(commit);
5757 got_worktree_close(worktree);
5759 const struct got_error *close_err = got_repo_close(repo);
5764 const struct got_error *pack_err =
5765 got_repo_pack_fds_close(pack_fds);
5773 static const struct got_error *
5774 draw_tree_entries(struct tog_view *view, const char *parent_path)
5776 struct tog_tree_view_state *s = &view->state.tree;
5777 const struct got_error *err = NULL;
5778 struct got_tree_entry *te;
5780 struct tog_color *tc;
5781 int width, n, i, nentries;
5782 int limit = view->nlines;
5785 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5786 view_is_splitscreen(view->child))
5787 --limit; /* border */
5789 werase(view->window);
5794 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5798 if (view_needs_focus_indication(view))
5799 wstandout(view->window);
5800 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5802 wattr_on(view->window,
5803 COLOR_PAIR(tc->colorpair), NULL);
5804 waddwstr(view->window, wline);
5806 wattr_off(view->window,
5807 COLOR_PAIR(tc->colorpair), NULL);
5808 if (view_needs_focus_indication(view))
5809 wstandend(view->window);
5812 if (width < view->ncols - 1)
5813 waddch(view->window, '\n');
5816 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5820 waddwstr(view->window, wline);
5823 if (width < view->ncols - 1)
5824 waddch(view->window, '\n');
5827 waddch(view->window, '\n');
5831 if (s->first_displayed_entry == NULL) {
5832 te = got_object_tree_get_first_entry(s->tree);
5833 if (s->selected == 0) {
5835 wstandout(view->window);
5836 s->selected_entry = NULL;
5838 waddstr(view->window, " ..\n"); /* parent directory */
5839 if (s->selected == 0 && view->focussed)
5840 wstandend(view->window);
5847 te = s->first_displayed_entry;
5850 nentries = got_object_tree_get_nentries(s->tree);
5851 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5852 char *line = NULL, *id_str = NULL, *link_target = NULL;
5853 const char *modestr = "";
5856 te = got_object_tree_get_entry(s->tree, i);
5857 mode = got_tree_entry_get_mode(te);
5860 err = got_object_id_str(&id_str,
5861 got_tree_entry_get_id(te));
5863 return got_error_from_errno(
5864 "got_object_id_str");
5866 if (got_object_tree_entry_is_submodule(te))
5868 else if (S_ISLNK(mode)) {
5871 err = got_tree_entry_get_symlink_target(&link_target,
5877 for (i = 0; i < strlen(link_target); i++) {
5878 if (!isprint((unsigned char)link_target[i]))
5879 link_target[i] = '?';
5883 else if (S_ISDIR(mode))
5885 else if (mode & S_IXUSR)
5887 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5888 got_tree_entry_get_name(te), modestr,
5889 link_target ? " -> ": "",
5890 link_target ? link_target : "") == -1) {
5893 return got_error_from_errno("asprintf");
5897 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5903 if (n == s->selected) {
5905 wstandout(view->window);
5906 s->selected_entry = te;
5908 tc = match_color(&s->colors, line);
5910 wattr_on(view->window,
5911 COLOR_PAIR(tc->colorpair), NULL);
5912 waddwstr(view->window, wline);
5914 wattr_off(view->window,
5915 COLOR_PAIR(tc->colorpair), NULL);
5916 if (width < view->ncols - 1)
5917 waddch(view->window, '\n');
5918 if (n == s->selected && view->focussed)
5919 wstandend(view->window);
5925 s->last_displayed_entry = te;
5934 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5936 struct got_tree_entry *te;
5937 int isroot = s->tree == s->root;
5940 if (s->first_displayed_entry == NULL)
5943 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5944 while (i++ < maxscroll) {
5947 s->first_displayed_entry = NULL;
5950 s->first_displayed_entry = te;
5951 te = got_tree_entry_get_prev(s->tree, te);
5955 static const struct got_error *
5956 tree_scroll_down(struct tog_view *view, int maxscroll)
5958 struct tog_tree_view_state *s = &view->state.tree;
5959 struct got_tree_entry *next, *last;
5962 if (s->first_displayed_entry)
5963 next = got_tree_entry_get_next(s->tree,
5964 s->first_displayed_entry);
5966 next = got_object_tree_get_first_entry(s->tree);
5968 last = s->last_displayed_entry;
5969 while (next && n++ < maxscroll) {
5971 last = got_tree_entry_get_next(s->tree, last);
5972 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
5973 s->first_displayed_entry = next;
5974 next = got_tree_entry_get_next(s->tree, next);
5981 static const struct got_error *
5982 tree_entry_path(char **path, struct tog_parent_trees *parents,
5983 struct got_tree_entry *te)
5985 const struct got_error *err = NULL;
5986 struct tog_parent_tree *pt;
5987 size_t len = 2; /* for leading slash and NUL */
5989 TAILQ_FOREACH(pt, parents, entry)
5990 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5993 len += strlen(got_tree_entry_get_name(te));
5995 *path = calloc(1, len);
5997 return got_error_from_errno("calloc");
6000 pt = TAILQ_LAST(parents, tog_parent_trees);
6002 const char *name = got_tree_entry_get_name(pt->selected_entry);
6003 if (strlcat(*path, name, len) >= len) {
6004 err = got_error(GOT_ERR_NO_SPACE);
6007 if (strlcat(*path, "/", len) >= len) {
6008 err = got_error(GOT_ERR_NO_SPACE);
6011 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6014 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6015 err = got_error(GOT_ERR_NO_SPACE);
6027 static const struct got_error *
6028 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6029 struct got_tree_entry *te, struct tog_parent_trees *parents,
6030 struct got_object_id *commit_id, struct got_repository *repo)
6032 const struct got_error *err = NULL;
6034 struct tog_view *blame_view;
6038 err = tree_entry_path(&path, parents, te);
6042 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6043 if (blame_view == NULL) {
6044 err = got_error_from_errno("view_open");
6048 err = open_blame_view(blame_view, path, commit_id, repo);
6050 if (err->code == GOT_ERR_CANCELLED)
6052 view_close(blame_view);
6054 *new_view = blame_view;
6060 static const struct got_error *
6061 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
6062 struct tog_tree_view_state *s)
6064 struct tog_view *log_view;
6065 const struct got_error *err = NULL;
6070 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6071 if (log_view == NULL)
6072 return got_error_from_errno("view_open");
6074 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6078 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6081 view_close(log_view);
6083 *new_view = log_view;
6088 static const struct got_error *
6089 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6090 const char *head_ref_name, struct got_repository *repo)
6092 const struct got_error *err = NULL;
6093 char *commit_id_str = NULL;
6094 struct tog_tree_view_state *s = &view->state.tree;
6095 struct got_commit_object *commit = NULL;
6097 TAILQ_INIT(&s->parents);
6098 STAILQ_INIT(&s->colors);
6100 s->commit_id = got_object_id_dup(commit_id);
6101 if (s->commit_id == NULL)
6102 return got_error_from_errno("got_object_id_dup");
6104 err = got_object_open_as_commit(&commit, repo, commit_id);
6109 * The root is opened here and will be closed when the view is closed.
6110 * Any visited subtrees and their path-wise parents are opened and
6113 err = got_object_open_as_tree(&s->root, repo,
6114 got_object_commit_get_tree_id(commit));
6119 err = got_object_id_str(&commit_id_str, commit_id);
6123 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6124 err = got_error_from_errno("asprintf");
6128 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6129 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6130 if (head_ref_name) {
6131 s->head_ref_name = strdup(head_ref_name);
6132 if (s->head_ref_name == NULL) {
6133 err = got_error_from_errno("strdup");
6139 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6140 err = add_color(&s->colors, "\\$$",
6141 TOG_COLOR_TREE_SUBMODULE,
6142 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6145 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6146 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6149 err = add_color(&s->colors, "/$",
6150 TOG_COLOR_TREE_DIRECTORY,
6151 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6155 err = add_color(&s->colors, "\\*$",
6156 TOG_COLOR_TREE_EXECUTABLE,
6157 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6161 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6162 get_color_value("TOG_COLOR_COMMIT"));
6167 view->show = show_tree_view;
6168 view->input = input_tree_view;
6169 view->close = close_tree_view;
6170 view->search_start = search_start_tree_view;
6171 view->search_next = search_next_tree_view;
6173 free(commit_id_str);
6175 got_object_commit_close(commit);
6177 close_tree_view(view);
6181 static const struct got_error *
6182 close_tree_view(struct tog_view *view)
6184 struct tog_tree_view_state *s = &view->state.tree;
6186 free_colors(&s->colors);
6187 free(s->tree_label);
6188 s->tree_label = NULL;
6190 s->commit_id = NULL;
6191 free(s->head_ref_name);
6192 s->head_ref_name = NULL;
6193 while (!TAILQ_EMPTY(&s->parents)) {
6194 struct tog_parent_tree *parent;
6195 parent = TAILQ_FIRST(&s->parents);
6196 TAILQ_REMOVE(&s->parents, parent, entry);
6197 if (parent->tree != s->root)
6198 got_object_tree_close(parent->tree);
6202 if (s->tree != NULL && s->tree != s->root)
6203 got_object_tree_close(s->tree);
6205 got_object_tree_close(s->root);
6209 static const struct got_error *
6210 search_start_tree_view(struct tog_view *view)
6212 struct tog_tree_view_state *s = &view->state.tree;
6214 s->matched_entry = NULL;
6219 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6221 regmatch_t regmatch;
6223 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6227 static const struct got_error *
6228 search_next_tree_view(struct tog_view *view)
6230 struct tog_tree_view_state *s = &view->state.tree;
6231 struct got_tree_entry *te = NULL;
6233 if (!view->searching) {
6234 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6238 if (s->matched_entry) {
6239 if (view->searching == TOG_SEARCH_FORWARD) {
6240 if (s->selected_entry)
6241 te = got_tree_entry_get_next(s->tree,
6244 te = got_object_tree_get_first_entry(s->tree);
6246 if (s->selected_entry == NULL)
6247 te = got_object_tree_get_last_entry(s->tree);
6249 te = got_tree_entry_get_prev(s->tree,
6253 if (s->selected_entry)
6254 te = s->selected_entry;
6255 else if (view->searching == TOG_SEARCH_FORWARD)
6256 te = got_object_tree_get_first_entry(s->tree);
6258 te = got_object_tree_get_last_entry(s->tree);
6263 if (s->matched_entry == NULL) {
6264 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6267 if (view->searching == TOG_SEARCH_FORWARD)
6268 te = got_object_tree_get_first_entry(s->tree);
6270 te = got_object_tree_get_last_entry(s->tree);
6273 if (match_tree_entry(te, &view->regex)) {
6274 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6275 s->matched_entry = te;
6279 if (view->searching == TOG_SEARCH_FORWARD)
6280 te = got_tree_entry_get_next(s->tree, te);
6282 te = got_tree_entry_get_prev(s->tree, te);
6285 if (s->matched_entry) {
6286 s->first_displayed_entry = s->matched_entry;
6293 static const struct got_error *
6294 show_tree_view(struct tog_view *view)
6296 const struct got_error *err = NULL;
6297 struct tog_tree_view_state *s = &view->state.tree;
6300 err = tree_entry_path(&parent_path, &s->parents, NULL);
6304 err = draw_tree_entries(view, parent_path);
6311 static const struct got_error *
6312 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6314 const struct got_error *err = NULL;
6315 struct tog_tree_view_state *s = &view->state.tree;
6316 struct tog_view *log_view, *ref_view;
6317 struct got_tree_entry *te;
6318 int begin_x = 0, n, nscroll = view->nlines - 3;
6322 s->show_ids = !s->show_ids;
6327 if (!s->selected_entry)
6329 if (view_is_parent_view(view))
6330 begin_x = view_split_begin_x(view->begin_x);
6331 err = log_selected_tree_entry(&log_view, begin_x, s);
6333 log_view->focussed = 1;
6334 if (view_is_parent_view(view)) {
6335 err = view_close_child(view);
6338 err = view_set_child(view, log_view);
6341 view->focus_child = 1;
6343 *new_view = log_view;
6347 if (view_is_parent_view(view))
6348 begin_x = view_split_begin_x(view->begin_x);
6349 ref_view = view_open(view->nlines, view->ncols,
6350 view->begin_y, begin_x, TOG_VIEW_REF);
6351 if (ref_view == NULL)
6352 return got_error_from_errno("view_open");
6353 err = open_ref_view(ref_view, s->repo);
6355 view_close(ref_view);
6359 ref_view->focussed = 1;
6360 if (view_is_parent_view(view)) {
6361 err = view_close_child(view);
6364 err = view_set_child(view, ref_view);
6367 view->focus_child = 1;
6369 *new_view = ref_view;
6375 if (s->tree == s->root)
6376 s->first_displayed_entry =
6377 got_object_tree_get_first_entry(s->tree);
6379 s->first_displayed_entry = NULL;
6383 int eos = view->nlines - 3;
6385 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6389 te = got_object_tree_get_last_entry(s->tree);
6390 for (n = 0; n < eos; n++) {
6392 if(s->tree != s->root) {
6393 s->first_displayed_entry = NULL;
6398 s->first_displayed_entry = te;
6399 te = got_tree_entry_get_prev(s->tree, te);
6402 s->selected = n - 1;
6408 if (s->selected > 0) {
6412 tree_scroll_up(s, 1);
6413 if (s->selected_entry == NULL ||
6414 (s->tree == s->root && s->selected_entry ==
6415 got_object_tree_get_first_entry(s->tree)))
6425 if (s->tree == s->root) {
6426 if (got_object_tree_get_first_entry(s->tree) ==
6427 s->first_displayed_entry)
6428 s->selected -= MIN(s->selected, nscroll);
6430 if (s->first_displayed_entry == NULL)
6431 s->selected -= MIN(s->selected, nscroll);
6433 tree_scroll_up(s, MAX(0, nscroll));
6434 if (s->selected_entry == NULL ||
6435 (s->tree == s->root && s->selected_entry ==
6436 got_object_tree_get_first_entry(s->tree)))
6442 if (s->selected < s->ndisplayed - 1) {
6446 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6448 /* can't scroll any further */
6452 tree_scroll_down(view, 1);
6462 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6464 /* can't scroll any further; move cursor down */
6465 if (s->selected < s->ndisplayed - 1)
6466 s->selected += MIN(nscroll,
6467 s->ndisplayed - s->selected - 1);
6472 tree_scroll_down(view, nscroll);
6477 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6478 struct tog_parent_tree *parent;
6479 /* user selected '..' */
6480 if (s->tree == s->root) {
6484 parent = TAILQ_FIRST(&s->parents);
6485 TAILQ_REMOVE(&s->parents, parent,
6487 got_object_tree_close(s->tree);
6488 s->tree = parent->tree;
6489 s->first_displayed_entry =
6490 parent->first_displayed_entry;
6492 parent->selected_entry;
6493 s->selected = parent->selected;
6494 if (s->selected > view->nlines - 3) {
6495 err = offset_selection_down(view);
6500 } else if (S_ISDIR(got_tree_entry_get_mode(
6501 s->selected_entry))) {
6502 struct got_tree_object *subtree;
6504 err = got_object_open_as_tree(&subtree, s->repo,
6505 got_tree_entry_get_id(s->selected_entry));
6508 err = tree_view_visit_subtree(s, subtree);
6510 got_object_tree_close(subtree);
6513 } else if (S_ISREG(got_tree_entry_get_mode(
6514 s->selected_entry))) {
6515 struct tog_view *blame_view;
6516 int begin_x = 0, begin_y = 0;
6518 if (view_is_parent_view(view))
6519 view_get_split(view, &begin_y, &begin_x);
6521 err = blame_tree_entry(&blame_view, begin_y, begin_x,
6522 s->selected_entry, &s->parents,
6523 s->commit_id, s->repo);
6527 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
6528 err = view_init_hsplit(view, begin_y);
6535 blame_view->focussed = 1;
6536 blame_view->mode = view->mode;
6537 blame_view->nlines = view->lines - begin_y;
6538 if (view_is_parent_view(view)) {
6539 err = view_close_child(view);
6542 err = view_set_child(view, blame_view);
6545 view->focus_child = 1;
6547 *new_view = blame_view;
6551 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6552 s->selected = view->nlines - 4;
6568 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6573 static const struct got_error *
6574 cmd_tree(int argc, char *argv[])
6576 const struct got_error *error;
6577 struct got_repository *repo = NULL;
6578 struct got_worktree *worktree = NULL;
6579 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6580 struct got_object_id *commit_id = NULL;
6581 struct got_commit_object *commit = NULL;
6582 const char *commit_id_arg = NULL;
6584 struct got_reference *ref = NULL;
6585 const char *head_ref_name = NULL;
6587 struct tog_view *view;
6588 int *pack_fds = NULL;
6590 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6593 commit_id_arg = optarg;
6596 repo_path = realpath(optarg, NULL);
6597 if (repo_path == NULL)
6598 return got_error_from_errno2("realpath",
6613 error = got_repo_pack_fds_open(&pack_fds);
6617 if (repo_path == NULL) {
6618 cwd = getcwd(NULL, 0);
6620 return got_error_from_errno("getcwd");
6621 error = got_worktree_open(&worktree, cwd);
6622 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6626 strdup(got_worktree_get_repo_path(worktree));
6628 repo_path = strdup(cwd);
6629 if (repo_path == NULL) {
6630 error = got_error_from_errno("strdup");
6635 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6639 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6646 error = apply_unveil(got_repo_get_path(repo), NULL);
6650 error = tog_load_refs(repo, 0);
6654 if (commit_id_arg == NULL) {
6655 error = got_repo_match_object_id(&commit_id, &label,
6656 worktree ? got_worktree_get_head_ref_name(worktree) :
6657 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6660 head_ref_name = label;
6662 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6664 head_ref_name = got_ref_get_name(ref);
6665 else if (error->code != GOT_ERR_NOT_REF)
6667 error = got_repo_match_object_id(&commit_id, NULL,
6668 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6673 error = got_object_open_as_commit(&commit, repo, commit_id);
6677 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6679 error = got_error_from_errno("view_open");
6682 error = open_tree_view(view, commit_id, head_ref_name, repo);
6685 if (!got_path_is_root_dir(in_repo_path)) {
6686 error = tree_view_walk_path(&view->state.tree, commit,
6693 /* Release work tree lock. */
6694 got_worktree_close(worktree);
6697 error = view_loop(view);
6706 const struct got_error *close_err = got_repo_close(repo);
6711 const struct got_error *pack_err =
6712 got_repo_pack_fds_close(pack_fds);
6720 static const struct got_error *
6721 ref_view_load_refs(struct tog_ref_view_state *s)
6723 struct got_reflist_entry *sre;
6724 struct tog_reflist_entry *re;
6727 TAILQ_FOREACH(sre, &tog_refs, entry) {
6728 if (strncmp(got_ref_get_name(sre->ref),
6729 "refs/got/", 9) == 0 &&
6730 strncmp(got_ref_get_name(sre->ref),
6731 "refs/got/backup/", 16) != 0)
6734 re = malloc(sizeof(*re));
6736 return got_error_from_errno("malloc");
6738 re->ref = got_ref_dup(sre->ref);
6739 if (re->ref == NULL)
6740 return got_error_from_errno("got_ref_dup");
6741 re->idx = s->nrefs++;
6742 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6745 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6750 ref_view_free_refs(struct tog_ref_view_state *s)
6752 struct tog_reflist_entry *re;
6754 while (!TAILQ_EMPTY(&s->refs)) {
6755 re = TAILQ_FIRST(&s->refs);
6756 TAILQ_REMOVE(&s->refs, re, entry);
6757 got_ref_close(re->ref);
6762 static const struct got_error *
6763 open_ref_view(struct tog_view *view, struct got_repository *repo)
6765 const struct got_error *err = NULL;
6766 struct tog_ref_view_state *s = &view->state.ref;
6768 s->selected_entry = 0;
6771 TAILQ_INIT(&s->refs);
6772 STAILQ_INIT(&s->colors);
6774 err = ref_view_load_refs(s);
6778 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6779 err = add_color(&s->colors, "^refs/heads/",
6780 TOG_COLOR_REFS_HEADS,
6781 get_color_value("TOG_COLOR_REFS_HEADS"));
6785 err = add_color(&s->colors, "^refs/tags/",
6786 TOG_COLOR_REFS_TAGS,
6787 get_color_value("TOG_COLOR_REFS_TAGS"));
6791 err = add_color(&s->colors, "^refs/remotes/",
6792 TOG_COLOR_REFS_REMOTES,
6793 get_color_value("TOG_COLOR_REFS_REMOTES"));
6797 err = add_color(&s->colors, "^refs/got/backup/",
6798 TOG_COLOR_REFS_BACKUP,
6799 get_color_value("TOG_COLOR_REFS_BACKUP"));
6804 view->show = show_ref_view;
6805 view->input = input_ref_view;
6806 view->close = close_ref_view;
6807 view->search_start = search_start_ref_view;
6808 view->search_next = search_next_ref_view;
6811 free_colors(&s->colors);
6815 static const struct got_error *
6816 close_ref_view(struct tog_view *view)
6818 struct tog_ref_view_state *s = &view->state.ref;
6820 ref_view_free_refs(s);
6821 free_colors(&s->colors);
6826 static const struct got_error *
6827 resolve_reflist_entry(struct got_object_id **commit_id,
6828 struct tog_reflist_entry *re, struct got_repository *repo)
6830 const struct got_error *err = NULL;
6831 struct got_object_id *obj_id;
6832 struct got_tag_object *tag = NULL;
6837 err = got_ref_resolve(&obj_id, repo, re->ref);
6841 err = got_object_get_type(&obj_type, repo, obj_id);
6846 case GOT_OBJ_TYPE_COMMIT:
6847 *commit_id = obj_id;
6849 case GOT_OBJ_TYPE_TAG:
6850 err = got_object_open_as_tag(&tag, repo, obj_id);
6854 err = got_object_get_type(&obj_type, repo,
6855 got_object_tag_get_object_id(tag));
6858 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6859 err = got_error(GOT_ERR_OBJ_TYPE);
6862 *commit_id = got_object_id_dup(
6863 got_object_tag_get_object_id(tag));
6864 if (*commit_id == NULL) {
6865 err = got_error_from_errno("got_object_id_dup");
6870 err = got_error(GOT_ERR_OBJ_TYPE);
6876 got_object_tag_close(tag);
6884 static const struct got_error *
6885 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
6886 struct tog_reflist_entry *re, struct got_repository *repo)
6888 struct tog_view *log_view;
6889 const struct got_error *err = NULL;
6890 struct got_object_id *commit_id = NULL;
6894 err = resolve_reflist_entry(&commit_id, re, repo);
6896 if (err->code != GOT_ERR_OBJ_TYPE)
6902 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6903 if (log_view == NULL) {
6904 err = got_error_from_errno("view_open");
6908 err = open_log_view(log_view, commit_id, repo,
6909 got_ref_get_name(re->ref), "", 0);
6912 view_close(log_view);
6914 *new_view = log_view;
6920 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6922 struct tog_reflist_entry *re;
6925 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6928 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6929 while (i++ < maxscroll) {
6932 s->first_displayed_entry = re;
6933 re = TAILQ_PREV(re, tog_reflist_head, entry);
6937 static const struct got_error *
6938 ref_scroll_down(struct tog_view *view, int maxscroll)
6940 struct tog_ref_view_state *s = &view->state.ref;
6941 struct tog_reflist_entry *next, *last;
6944 if (s->first_displayed_entry)
6945 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6947 next = TAILQ_FIRST(&s->refs);
6949 last = s->last_displayed_entry;
6950 while (next && n++ < maxscroll) {
6952 last = TAILQ_NEXT(last, entry);
6953 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
6954 s->first_displayed_entry = next;
6955 next = TAILQ_NEXT(next, entry);
6962 static const struct got_error *
6963 search_start_ref_view(struct tog_view *view)
6965 struct tog_ref_view_state *s = &view->state.ref;
6967 s->matched_entry = NULL;
6972 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6974 regmatch_t regmatch;
6976 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6980 static const struct got_error *
6981 search_next_ref_view(struct tog_view *view)
6983 struct tog_ref_view_state *s = &view->state.ref;
6984 struct tog_reflist_entry *re = NULL;
6986 if (!view->searching) {
6987 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6991 if (s->matched_entry) {
6992 if (view->searching == TOG_SEARCH_FORWARD) {
6993 if (s->selected_entry)
6994 re = TAILQ_NEXT(s->selected_entry, entry);
6996 re = TAILQ_PREV(s->selected_entry,
6997 tog_reflist_head, entry);
6999 if (s->selected_entry == NULL)
7000 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7002 re = TAILQ_PREV(s->selected_entry,
7003 tog_reflist_head, entry);
7006 if (s->selected_entry)
7007 re = s->selected_entry;
7008 else if (view->searching == TOG_SEARCH_FORWARD)
7009 re = TAILQ_FIRST(&s->refs);
7011 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7016 if (s->matched_entry == NULL) {
7017 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7020 if (view->searching == TOG_SEARCH_FORWARD)
7021 re = TAILQ_FIRST(&s->refs);
7023 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7026 if (match_reflist_entry(re, &view->regex)) {
7027 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7028 s->matched_entry = re;
7032 if (view->searching == TOG_SEARCH_FORWARD)
7033 re = TAILQ_NEXT(re, entry);
7035 re = TAILQ_PREV(re, tog_reflist_head, entry);
7038 if (s->matched_entry) {
7039 s->first_displayed_entry = s->matched_entry;
7046 static const struct got_error *
7047 show_ref_view(struct tog_view *view)
7049 const struct got_error *err = NULL;
7050 struct tog_ref_view_state *s = &view->state.ref;
7051 struct tog_reflist_entry *re;
7054 struct tog_color *tc;
7056 int limit = view->nlines;
7058 werase(view->window);
7061 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
7062 view_is_splitscreen(view->child))
7063 --limit; /* border */
7068 re = s->first_displayed_entry;
7070 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7072 return got_error_from_errno("asprintf");
7074 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7079 if (view_needs_focus_indication(view))
7080 wstandout(view->window);
7081 waddwstr(view->window, wline);
7082 if (view_needs_focus_indication(view))
7083 wstandend(view->window);
7088 if (width < view->ncols - 1)
7089 waddch(view->window, '\n');
7094 while (re && limit > 0) {
7096 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7099 struct got_commit_object *ci;
7100 struct got_tag_object *tag;
7101 struct got_object_id *id;
7105 err = got_ref_resolve(&id, s->repo, re->ref);
7108 err = got_object_open_as_tag(&tag, s->repo, id);
7110 if (err->code != GOT_ERR_OBJ_TYPE) {
7114 err = got_object_open_as_commit(&ci, s->repo,
7120 t = got_object_commit_get_committer_time(ci);
7121 got_object_commit_close(ci);
7123 t = got_object_tag_get_tagger_time(tag);
7124 got_object_tag_close(tag);
7127 if (gmtime_r(&t, &tm) == NULL)
7128 return got_error_from_errno("gmtime_r");
7129 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7130 return got_error(GOT_ERR_NO_SPACE);
7132 if (got_ref_is_symbolic(re->ref)) {
7133 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7134 ymd : "", got_ref_get_name(re->ref),
7135 got_ref_get_symref_target(re->ref)) == -1)
7136 return got_error_from_errno("asprintf");
7137 } else if (s->show_ids) {
7138 struct got_object_id *id;
7140 err = got_ref_resolve(&id, s->repo, re->ref);
7143 err = got_object_id_str(&id_str, id);
7148 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7149 got_ref_get_name(re->ref), id_str) == -1) {
7150 err = got_error_from_errno("asprintf");
7157 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7158 got_ref_get_name(re->ref)) == -1)
7159 return got_error_from_errno("asprintf");
7161 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7167 if (n == s->selected) {
7169 wstandout(view->window);
7170 s->selected_entry = re;
7172 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7174 wattr_on(view->window,
7175 COLOR_PAIR(tc->colorpair), NULL);
7176 waddwstr(view->window, wline);
7178 wattr_off(view->window,
7179 COLOR_PAIR(tc->colorpair), NULL);
7180 if (width < view->ncols - 1)
7181 waddch(view->window, '\n');
7182 if (n == s->selected && view->focussed)
7183 wstandend(view->window);
7189 s->last_displayed_entry = re;
7192 re = TAILQ_NEXT(re, entry);
7199 static const struct got_error *
7200 browse_ref_tree(struct tog_view **new_view, int begin_x,
7201 struct tog_reflist_entry *re, struct got_repository *repo)
7203 const struct got_error *err = NULL;
7204 struct got_object_id *commit_id = NULL;
7205 struct tog_view *tree_view;
7209 err = resolve_reflist_entry(&commit_id, re, repo);
7211 if (err->code != GOT_ERR_OBJ_TYPE)
7218 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
7219 if (tree_view == NULL) {
7220 err = got_error_from_errno("view_open");
7224 err = open_tree_view(tree_view, commit_id,
7225 got_ref_get_name(re->ref), repo);
7229 *new_view = tree_view;
7234 static const struct got_error *
7235 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7237 const struct got_error *err = NULL;
7238 struct tog_ref_view_state *s = &view->state.ref;
7239 struct tog_view *log_view, *tree_view;
7240 struct tog_reflist_entry *re;
7241 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7245 s->show_ids = !s->show_ids;
7249 s->show_date = !s->show_date;
7253 s->sort_by_date = !s->sort_by_date;
7255 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7256 got_ref_cmp_by_commit_timestamp_descending :
7257 tog_ref_cmp_by_name, s->repo);
7260 got_reflist_object_id_map_free(tog_refs_idmap);
7261 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7262 &tog_refs, s->repo);
7265 ref_view_free_refs(s);
7266 err = ref_view_load_refs(s);
7271 if (!s->selected_entry)
7273 if (view_is_parent_view(view))
7274 view_get_split(view, &begin_y, &begin_x);
7276 err = log_ref_entry(&log_view, begin_y, begin_x,
7277 s->selected_entry, s->repo);
7281 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
7282 err = view_init_hsplit(view, begin_y);
7288 log_view->focussed = 1;
7289 log_view->mode = view->mode;
7290 log_view->nlines = view->lines - begin_y;
7291 if (view_is_parent_view(view)) {
7292 err = view_close_child(view);
7295 err = view_set_child(view, log_view);
7298 view->focus_child = 1;
7300 *new_view = log_view;
7304 if (!s->selected_entry)
7306 if (view_is_parent_view(view))
7307 begin_x = view_split_begin_x(view->begin_x);
7308 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
7310 if (err || tree_view == NULL)
7313 tree_view->focussed = 1;
7314 if (view_is_parent_view(view)) {
7315 err = view_close_child(view);
7318 err = view_set_child(view, tree_view);
7321 view->focus_child = 1;
7323 *new_view = tree_view;
7329 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7333 int eos = view->nlines - 1;
7335 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7339 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7340 for (n = 0; n < eos; n++) {
7343 s->first_displayed_entry = re;
7344 re = TAILQ_PREV(re, tog_reflist_head, entry);
7347 s->selected = n - 1;
7353 if (s->selected > 0) {
7357 ref_scroll_up(s, 1);
7358 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7368 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7369 s->selected -= MIN(nscroll, s->selected);
7370 ref_scroll_up(s, MAX(0, nscroll));
7371 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7377 if (s->selected < s->ndisplayed - 1) {
7381 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7382 /* can't scroll any further */
7386 ref_scroll_down(view, 1);
7396 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7397 /* can't scroll any further; move cursor down */
7398 if (s->selected < s->ndisplayed - 1)
7399 s->selected += MIN(nscroll,
7400 s->ndisplayed - s->selected - 1);
7401 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7402 s->selected += s->ndisplayed - s->selected - 1;
7406 ref_scroll_down(view, nscroll);
7411 err = tog_load_refs(s->repo, s->sort_by_date);
7414 ref_view_free_refs(s);
7415 err = ref_view_load_refs(s);
7418 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7419 s->selected = view->nlines - 2;
7433 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7438 static const struct got_error *
7439 cmd_ref(int argc, char *argv[])
7441 const struct got_error *error;
7442 struct got_repository *repo = NULL;
7443 struct got_worktree *worktree = NULL;
7444 char *cwd = NULL, *repo_path = NULL;
7446 struct tog_view *view;
7447 int *pack_fds = NULL;
7449 while ((ch = getopt(argc, argv, "r:")) != -1) {
7452 repo_path = realpath(optarg, NULL);
7453 if (repo_path == NULL)
7454 return got_error_from_errno2("realpath",
7469 error = got_repo_pack_fds_open(&pack_fds);
7473 if (repo_path == NULL) {
7474 cwd = getcwd(NULL, 0);
7476 return got_error_from_errno("getcwd");
7477 error = got_worktree_open(&worktree, cwd);
7478 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7482 strdup(got_worktree_get_repo_path(worktree));
7484 repo_path = strdup(cwd);
7485 if (repo_path == NULL) {
7486 error = got_error_from_errno("strdup");
7491 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7497 error = apply_unveil(got_repo_get_path(repo), NULL);
7501 error = tog_load_refs(repo, 0);
7505 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7507 error = got_error_from_errno("view_open");
7511 error = open_ref_view(view, repo);
7516 /* Release work tree lock. */
7517 got_worktree_close(worktree);
7520 error = view_loop(view);
7525 const struct got_error *close_err = got_repo_close(repo);
7530 const struct got_error *pack_err =
7531 got_repo_pack_fds_close(pack_fds);
7540 * If view was scrolled down to move the selected line into view when opening a
7541 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7544 offset_selection_up(struct tog_view *view)
7546 switch (view->type) {
7547 case TOG_VIEW_BLAME: {
7548 struct tog_blame_view_state *s = &view->state.blame;
7549 if (s->first_displayed_line == 1) {
7550 s->selected_line = MAX(s->selected_line - view->offset,
7554 if (s->first_displayed_line > view->offset)
7555 s->first_displayed_line -= view->offset;
7557 s->first_displayed_line = 1;
7558 s->selected_line += view->offset;
7562 log_scroll_up(&view->state.log, view->offset);
7563 view->state.log.selected += view->offset;
7566 ref_scroll_up(&view->state.ref, view->offset);
7567 view->state.ref.selected += view->offset;
7570 tree_scroll_up(&view->state.tree, view->offset);
7571 view->state.tree.selected += view->offset;
7581 * If the selected line is in the section of screen covered by the bottom split,
7582 * scroll down offset lines to move it into view and index its new position.
7584 static const struct got_error *
7585 offset_selection_down(struct tog_view *view)
7587 const struct got_error *err = NULL;
7588 const struct got_error *(*scrolld)(struct tog_view *, int);
7589 int *selected = NULL;
7592 switch (view->type) {
7593 case TOG_VIEW_BLAME: {
7594 struct tog_blame_view_state *s = &view->state.blame;
7597 if (s->selected_line > view->nlines - header) {
7598 offset = abs(view->nlines - s->selected_line - header);
7599 s->first_displayed_line += offset;
7600 s->selected_line -= offset;
7601 view->offset = offset;
7605 case TOG_VIEW_LOG: {
7606 struct tog_log_view_state *s = &view->state.log;
7607 scrolld = &log_scroll_down;
7609 selected = &s->selected;
7612 case TOG_VIEW_REF: {
7613 struct tog_ref_view_state *s = &view->state.ref;
7614 scrolld = &ref_scroll_down;
7616 selected = &s->selected;
7619 case TOG_VIEW_TREE: {
7620 struct tog_tree_view_state *s = &view->state.tree;
7621 scrolld = &tree_scroll_down;
7623 selected = &s->selected;
7633 if (selected && *selected > view->nlines - header) {
7634 offset = abs(view->nlines - *selected - header);
7635 view->offset = offset;
7636 if (scrolld && offset) {
7637 err = scrolld(view, offset);
7638 *selected -= offset;
7646 list_commands(FILE *fp)
7650 fprintf(fp, "commands:");
7651 for (i = 0; i < nitems(tog_commands); i++) {
7652 const struct tog_cmd *cmd = &tog_commands[i];
7653 fprintf(fp, " %s", cmd->name);
7659 usage(int hflag, int status)
7661 FILE *fp = (status == 0) ? stdout : stderr;
7663 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7666 fprintf(fp, "lazy usage: %s path\n", getprogname());
7673 make_argv(int argc, ...)
7681 argv = calloc(argc, sizeof(char *));
7684 for (i = 0; i < argc; i++) {
7685 argv[i] = strdup(va_arg(ap, char *));
7686 if (argv[i] == NULL)
7695 * Try to convert 'tog path' into a 'tog log path' command.
7696 * The user could simply have mistyped the command rather than knowingly
7697 * provided a path. So check whether argv[0] can in fact be resolved
7698 * to a path in the HEAD commit and print a special error if not.
7699 * This hack is for mpi@ <3
7701 static const struct got_error *
7702 tog_log_with_path(int argc, char *argv[])
7704 const struct got_error *error = NULL, *close_err;
7705 const struct tog_cmd *cmd = NULL;
7706 struct got_repository *repo = NULL;
7707 struct got_worktree *worktree = NULL;
7708 struct got_object_id *commit_id = NULL, *id = NULL;
7709 struct got_commit_object *commit = NULL;
7710 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7711 char *commit_id_str = NULL, **cmd_argv = NULL;
7712 int *pack_fds = NULL;
7714 cwd = getcwd(NULL, 0);
7716 return got_error_from_errno("getcwd");
7718 error = got_repo_pack_fds_open(&pack_fds);
7722 error = got_worktree_open(&worktree, cwd);
7723 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7727 repo_path = strdup(got_worktree_get_repo_path(worktree));
7729 repo_path = strdup(cwd);
7730 if (repo_path == NULL) {
7731 error = got_error_from_errno("strdup");
7735 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7739 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7744 error = tog_load_refs(repo, 0);
7747 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7748 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7749 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7754 got_worktree_close(worktree);
7758 error = got_object_open_as_commit(&commit, repo, commit_id);
7762 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7764 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7766 fprintf(stderr, "%s: '%s' is no known command or path\n",
7767 getprogname(), argv[0]);
7772 close_err = got_repo_close(repo);
7777 error = got_object_id_str(&commit_id_str, commit_id);
7781 cmd = &tog_commands[0]; /* log */
7783 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7784 error = cmd->cmd_main(argc, cmd_argv);
7787 close_err = got_repo_close(repo);
7792 got_object_commit_close(commit);
7794 got_worktree_close(worktree);
7796 const struct got_error *pack_err =
7797 got_repo_pack_fds_close(pack_fds);
7802 free(commit_id_str);
7809 for (i = 0; i < argc; i++)
7818 main(int argc, char *argv[])
7820 const struct got_error *error = NULL;
7821 const struct tog_cmd *cmd = NULL;
7822 int ch, hflag = 0, Vflag = 0;
7823 char **cmd_argv = NULL;
7824 static const struct option longopts[] = {
7825 { "version", no_argument, NULL, 'V' },
7829 setlocale(LC_CTYPE, "");
7831 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7851 got_version_print_str();
7856 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7864 /* Build an argument vector which runs a default command. */
7865 cmd = &tog_commands[0];
7867 cmd_argv = make_argv(argc, cmd->name);
7871 /* Did the user specify a command? */
7872 for (i = 0; i < nitems(tog_commands); i++) {
7873 if (strncmp(tog_commands[i].name, argv[0],
7874 strlen(argv[0])) == 0) {
7875 cmd = &tog_commands[i];
7884 /* No command specified; try log with a path */
7885 error = tog_log_with_path(argc, argv);
7890 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7897 for (i = 0; i < argc; i++)
7902 if (error && error->code != GOT_ERR_CANCELLED)
7903 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);