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, fd = -1;
4912 fd = got_opentempfd();
4914 return (void *)got_error_from_errno("got_opentempfd");
4916 err = block_signals_used_by_main_thread();
4920 err = got_blame(ta->path, a->commit_id, ta->repo,
4921 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg, fd);
4922 if (err && err->code == GOT_ERR_CANCELLED)
4925 errcode = pthread_mutex_lock(&tog_mutex);
4927 return (void *)got_error_set_errno(errcode,
4928 "pthread_mutex_lock");
4930 close_err = got_repo_close(ta->repo);
4936 errcode = pthread_mutex_unlock(&tog_mutex);
4937 if (errcode && err == NULL)
4938 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4940 if (fd != -1 && close(fd) == -1 && err == NULL)
4941 err = got_error_from_errno("close");
4946 static struct got_object_id *
4947 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4948 int first_displayed_line, int selected_line)
4950 struct tog_blame_line *line;
4955 line = &lines[first_displayed_line - 1 + selected_line - 1];
4956 if (!line->annotated)
4962 static const struct got_error *
4963 stop_blame(struct tog_blame *blame)
4965 const struct got_error *err = NULL;
4968 if (blame->thread) {
4970 errcode = pthread_mutex_unlock(&tog_mutex);
4972 return got_error_set_errno(errcode,
4973 "pthread_mutex_unlock");
4974 errcode = pthread_join(blame->thread, (void **)&err);
4976 return got_error_set_errno(errcode, "pthread_join");
4977 errcode = pthread_mutex_lock(&tog_mutex);
4979 return got_error_set_errno(errcode,
4980 "pthread_mutex_lock");
4981 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4983 blame->thread = NULL;
4985 if (blame->thread_args.repo) {
4986 const struct got_error *close_err;
4987 close_err = got_repo_close(blame->thread_args.repo);
4990 blame->thread_args.repo = NULL;
4993 if (fclose(blame->f) == EOF && err == NULL)
4994 err = got_error_from_errno("fclose");
4998 for (i = 0; i < blame->nlines; i++)
4999 free(blame->lines[i].id);
5001 blame->lines = NULL;
5003 free(blame->cb_args.commit_id);
5004 blame->cb_args.commit_id = NULL;
5005 if (blame->pack_fds) {
5006 const struct got_error *pack_err =
5007 got_repo_pack_fds_close(blame->pack_fds);
5010 blame->pack_fds = NULL;
5015 static const struct got_error *
5016 cancel_blame_view(void *arg)
5018 const struct got_error *err = NULL;
5022 errcode = pthread_mutex_lock(&tog_mutex);
5024 return got_error_set_errno(errcode,
5025 "pthread_mutex_unlock");
5028 err = got_error(GOT_ERR_CANCELLED);
5030 errcode = pthread_mutex_unlock(&tog_mutex);
5032 return got_error_set_errno(errcode,
5033 "pthread_mutex_lock");
5038 static const struct got_error *
5039 run_blame(struct tog_view *view)
5041 struct tog_blame_view_state *s = &view->state.blame;
5042 struct tog_blame *blame = &s->blame;
5043 const struct got_error *err = NULL;
5044 struct got_commit_object *commit = NULL;
5045 struct got_blob_object *blob = NULL;
5046 struct got_repository *thread_repo = NULL;
5047 struct got_object_id *obj_id = NULL;
5048 int obj_type, fd = -1;
5049 int *pack_fds = NULL;
5051 err = got_object_open_as_commit(&commit, s->repo,
5052 &s->blamed_commit->id);
5056 fd = got_opentempfd();
5058 err = got_error_from_errno("got_opentempfd");
5062 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5066 err = got_object_get_type(&obj_type, s->repo, obj_id);
5070 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5071 err = got_error(GOT_ERR_OBJ_TYPE);
5075 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5078 blame->f = got_opentemp();
5079 if (blame->f == NULL) {
5080 err = got_error_from_errno("got_opentemp");
5083 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5084 &blame->line_offsets, blame->f, blob);
5087 if (blame->nlines == 0) {
5088 s->blame_complete = 1;
5092 /* Don't include \n at EOF in the blame line count. */
5093 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5096 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5097 if (blame->lines == NULL) {
5098 err = got_error_from_errno("calloc");
5102 err = got_repo_pack_fds_open(&pack_fds);
5105 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5110 blame->pack_fds = pack_fds;
5111 blame->cb_args.view = view;
5112 blame->cb_args.lines = blame->lines;
5113 blame->cb_args.nlines = blame->nlines;
5114 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5115 if (blame->cb_args.commit_id == NULL) {
5116 err = got_error_from_errno("got_object_id_dup");
5119 blame->cb_args.quit = &s->done;
5121 blame->thread_args.path = s->path;
5122 blame->thread_args.repo = thread_repo;
5123 blame->thread_args.cb_args = &blame->cb_args;
5124 blame->thread_args.complete = &s->blame_complete;
5125 blame->thread_args.cancel_cb = cancel_blame_view;
5126 blame->thread_args.cancel_arg = &s->done;
5127 s->blame_complete = 0;
5129 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5130 s->first_displayed_line = 1;
5131 s->last_displayed_line = view->nlines;
5132 s->selected_line = 1;
5134 s->matched_line = 0;
5138 got_object_commit_close(commit);
5139 if (fd != -1 && close(fd) == -1 && err == NULL)
5140 err = got_error_from_errno("close");
5142 got_object_blob_close(blob);
5149 static const struct got_error *
5150 open_blame_view(struct tog_view *view, char *path,
5151 struct got_object_id *commit_id, struct got_repository *repo)
5153 const struct got_error *err = NULL;
5154 struct tog_blame_view_state *s = &view->state.blame;
5156 STAILQ_INIT(&s->blamed_commits);
5158 s->path = strdup(path);
5159 if (s->path == NULL)
5160 return got_error_from_errno("strdup");
5162 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5168 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5169 s->first_displayed_line = 1;
5170 s->last_displayed_line = view->nlines;
5171 s->selected_line = 1;
5172 s->blame_complete = 0;
5174 s->commit_id = commit_id;
5175 memset(&s->blame, 0, sizeof(s->blame));
5177 STAILQ_INIT(&s->colors);
5178 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5179 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5180 get_color_value("TOG_COLOR_COMMIT"));
5185 view->show = show_blame_view;
5186 view->input = input_blame_view;
5187 view->close = close_blame_view;
5188 view->search_start = search_start_blame_view;
5189 view->search_next = search_next_blame_view;
5191 return run_blame(view);
5194 static const struct got_error *
5195 close_blame_view(struct tog_view *view)
5197 const struct got_error *err = NULL;
5198 struct tog_blame_view_state *s = &view->state.blame;
5200 if (s->blame.thread)
5201 err = stop_blame(&s->blame);
5203 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5204 struct got_object_qid *blamed_commit;
5205 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5206 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5207 got_object_qid_free(blamed_commit);
5211 free_colors(&s->colors);
5215 static const struct got_error *
5216 search_start_blame_view(struct tog_view *view)
5218 struct tog_blame_view_state *s = &view->state.blame;
5220 s->matched_line = 0;
5224 static const struct got_error *
5225 search_next_blame_view(struct tog_view *view)
5227 struct tog_blame_view_state *s = &view->state.blame;
5228 const struct got_error *err = NULL;
5231 size_t linesize = 0;
5234 if (!view->searching) {
5235 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5239 if (s->matched_line) {
5240 if (view->searching == TOG_SEARCH_FORWARD)
5241 lineno = s->matched_line + 1;
5243 lineno = s->matched_line - 1;
5245 lineno = s->first_displayed_line - 1 + s->selected_line;
5250 if (lineno <= 0 || lineno > s->blame.nlines) {
5251 if (s->matched_line == 0) {
5252 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5256 if (view->searching == TOG_SEARCH_FORWARD)
5259 lineno = s->blame.nlines;
5262 offset = s->blame.line_offsets[lineno - 1];
5263 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5265 return got_error_from_errno("fseeko");
5267 linelen = getline(&line, &linesize, s->blame.f);
5268 if (linelen != -1) {
5270 err = expand_tab(&exstr, line);
5273 if (match_line(exstr, &view->regex, 1,
5275 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5276 s->matched_line = lineno;
5282 if (view->searching == TOG_SEARCH_FORWARD)
5289 if (s->matched_line) {
5290 s->first_displayed_line = s->matched_line;
5291 s->selected_line = 1;
5297 static const struct got_error *
5298 show_blame_view(struct tog_view *view)
5300 const struct got_error *err = NULL;
5301 struct tog_blame_view_state *s = &view->state.blame;
5304 if (s->blame.thread == NULL && !s->blame_complete) {
5305 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5306 &s->blame.thread_args);
5308 return got_error_set_errno(errcode, "pthread_create");
5310 halfdelay(1); /* fast refresh while annotating */
5313 if (s->blame_complete)
5314 halfdelay(10); /* disable fast refresh */
5316 err = draw_blame(view);
5322 static const struct got_error *
5323 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5325 const struct got_error *err = NULL, *thread_err = NULL;
5326 struct tog_view *diff_view;
5327 struct tog_blame_view_state *s = &view->state.blame;
5328 int eos, nscroll, begin_y = 0, begin_x = 0;
5330 eos = nscroll = view->nlines - 2;
5331 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5332 view_is_splitscreen(view->child))
5340 view->x = MAX(view->maxx - view->ncols / 3, 0);
5345 if (view->x + view->ncols / 3 < view->maxx)
5346 view->x += 2; /* move two columns right */
5352 view->x -= MIN(view->x, 2); /* move two columns back */
5361 s->selected_line = 1;
5362 s->first_displayed_line = 1;
5367 if (s->blame.nlines < eos) {
5368 s->selected_line = s->blame.nlines;
5369 s->first_displayed_line = 1;
5371 s->selected_line = eos;
5372 s->first_displayed_line = s->blame.nlines - (eos - 1);
5379 if (s->selected_line > 1)
5381 else if (s->selected_line == 1 &&
5382 s->first_displayed_line > 1)
5383 s->first_displayed_line--;
5394 if (s->first_displayed_line == 1) {
5395 if (view->count > 1)
5397 s->selected_line = MAX(1, s->selected_line - nscroll);
5401 if (s->first_displayed_line > nscroll)
5402 s->first_displayed_line -= nscroll;
5404 s->first_displayed_line = 1;
5409 if (s->selected_line < eos && s->first_displayed_line +
5410 s->selected_line <= s->blame.nlines)
5412 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5413 s->first_displayed_line++;
5419 struct got_object_id *id = NULL;
5422 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5423 s->first_displayed_line, s->selected_line);
5427 struct got_commit_object *commit, *pcommit;
5428 struct got_object_qid *pid;
5429 struct got_object_id *blob_id = NULL;
5431 err = got_object_open_as_commit(&commit,
5436 got_object_commit_get_parent_ids(commit));
5438 got_object_commit_close(commit);
5441 /* Check if path history ends here. */
5442 err = got_object_open_as_commit(&pcommit,
5446 err = got_object_id_by_path(&blob_id, s->repo,
5448 got_object_commit_close(pcommit);
5450 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5452 got_object_commit_close(commit);
5455 err = got_object_get_type(&obj_type, s->repo,
5458 /* Can't blame non-blob type objects. */
5459 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5460 got_object_commit_close(commit);
5463 err = got_object_qid_alloc(&s->blamed_commit,
5465 got_object_commit_close(commit);
5467 if (got_object_id_cmp(id,
5468 &s->blamed_commit->id) == 0)
5470 err = got_object_qid_alloc(&s->blamed_commit,
5476 thread_err = stop_blame(&s->blame);
5480 STAILQ_INSERT_HEAD(&s->blamed_commits,
5481 s->blamed_commit, entry);
5482 err = run_blame(view);
5488 struct got_object_qid *first;
5491 first = STAILQ_FIRST(&s->blamed_commits);
5492 if (!got_object_id_cmp(&first->id, s->commit_id))
5495 thread_err = stop_blame(&s->blame);
5499 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5500 got_object_qid_free(s->blamed_commit);
5502 STAILQ_FIRST(&s->blamed_commits);
5503 err = run_blame(view);
5510 struct got_object_id *id = NULL;
5511 struct got_object_qid *pid;
5512 struct got_commit_object *commit = NULL;
5515 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5516 s->first_displayed_line, s->selected_line);
5519 err = got_object_open_as_commit(&commit, s->repo, id);
5522 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5523 if (view_is_parent_view(view))
5524 view_get_split(view, &begin_y, &begin_x);
5526 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
5527 if (diff_view == NULL) {
5528 got_object_commit_close(commit);
5529 err = got_error_from_errno("view_open");
5532 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5533 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5534 got_object_commit_close(commit);
5536 view_close(diff_view);
5539 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
5540 err = view_init_hsplit(view, begin_y);
5546 diff_view->focussed = 1;
5547 diff_view->mode = view->mode;
5548 diff_view->nlines = view->lines - begin_y;
5549 if (view_is_parent_view(view)) {
5550 err = view_close_child(view);
5553 err = view_set_child(view, diff_view);
5556 view->focus_child = 1;
5558 *new_view = diff_view;
5571 if (s->last_displayed_line >= s->blame.nlines &&
5572 s->selected_line >= MIN(s->blame.nlines,
5573 view->nlines - 2)) {
5577 if (s->last_displayed_line >= s->blame.nlines &&
5578 s->selected_line < view->nlines - 2) {
5580 MIN(nscroll, s->last_displayed_line -
5581 s->first_displayed_line - s->selected_line + 1);
5583 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5584 s->first_displayed_line += nscroll;
5586 s->first_displayed_line =
5587 s->blame.nlines - (view->nlines - 3);
5590 if (s->selected_line > view->nlines - 2) {
5591 s->selected_line = MIN(s->blame.nlines,
5599 return thread_err ? thread_err : err;
5602 static const struct got_error *
5603 cmd_blame(int argc, char *argv[])
5605 const struct got_error *error;
5606 struct got_repository *repo = NULL;
5607 struct got_worktree *worktree = NULL;
5608 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5609 char *link_target = NULL;
5610 struct got_object_id *commit_id = NULL;
5611 struct got_commit_object *commit = NULL;
5612 char *commit_id_str = NULL;
5614 struct tog_view *view;
5615 int *pack_fds = NULL;
5617 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5620 commit_id_str = optarg;
5623 repo_path = realpath(optarg, NULL);
5624 if (repo_path == NULL)
5625 return got_error_from_errno2("realpath",
5640 error = got_repo_pack_fds_open(&pack_fds);
5644 if (repo_path == NULL) {
5645 cwd = getcwd(NULL, 0);
5647 return got_error_from_errno("getcwd");
5648 error = got_worktree_open(&worktree, cwd);
5649 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5653 strdup(got_worktree_get_repo_path(worktree));
5655 repo_path = strdup(cwd);
5656 if (repo_path == NULL) {
5657 error = got_error_from_errno("strdup");
5662 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5666 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5673 error = apply_unveil(got_repo_get_path(repo), NULL);
5677 error = tog_load_refs(repo, 0);
5681 if (commit_id_str == NULL) {
5682 struct got_reference *head_ref;
5683 error = got_ref_open(&head_ref, repo, worktree ?
5684 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5687 error = got_ref_resolve(&commit_id, repo, head_ref);
5688 got_ref_close(head_ref);
5690 error = got_repo_match_object_id(&commit_id, NULL,
5691 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5696 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5698 error = got_error_from_errno("view_open");
5702 error = got_object_open_as_commit(&commit, repo, commit_id);
5706 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5711 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5716 /* Release work tree lock. */
5717 got_worktree_close(worktree);
5720 error = view_loop(view);
5728 got_object_commit_close(commit);
5730 got_worktree_close(worktree);
5732 const struct got_error *close_err = got_repo_close(repo);
5737 const struct got_error *pack_err =
5738 got_repo_pack_fds_close(pack_fds);
5746 static const struct got_error *
5747 draw_tree_entries(struct tog_view *view, const char *parent_path)
5749 struct tog_tree_view_state *s = &view->state.tree;
5750 const struct got_error *err = NULL;
5751 struct got_tree_entry *te;
5753 struct tog_color *tc;
5754 int width, n, i, nentries;
5755 int limit = view->nlines;
5758 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5759 view_is_splitscreen(view->child))
5760 --limit; /* border */
5762 werase(view->window);
5767 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5771 if (view_needs_focus_indication(view))
5772 wstandout(view->window);
5773 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5775 wattr_on(view->window,
5776 COLOR_PAIR(tc->colorpair), NULL);
5777 waddwstr(view->window, wline);
5779 wattr_off(view->window,
5780 COLOR_PAIR(tc->colorpair), NULL);
5781 if (view_needs_focus_indication(view))
5782 wstandend(view->window);
5785 if (width < view->ncols - 1)
5786 waddch(view->window, '\n');
5789 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5793 waddwstr(view->window, wline);
5796 if (width < view->ncols - 1)
5797 waddch(view->window, '\n');
5800 waddch(view->window, '\n');
5804 if (s->first_displayed_entry == NULL) {
5805 te = got_object_tree_get_first_entry(s->tree);
5806 if (s->selected == 0) {
5808 wstandout(view->window);
5809 s->selected_entry = NULL;
5811 waddstr(view->window, " ..\n"); /* parent directory */
5812 if (s->selected == 0 && view->focussed)
5813 wstandend(view->window);
5820 te = s->first_displayed_entry;
5823 nentries = got_object_tree_get_nentries(s->tree);
5824 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5825 char *line = NULL, *id_str = NULL, *link_target = NULL;
5826 const char *modestr = "";
5829 te = got_object_tree_get_entry(s->tree, i);
5830 mode = got_tree_entry_get_mode(te);
5833 err = got_object_id_str(&id_str,
5834 got_tree_entry_get_id(te));
5836 return got_error_from_errno(
5837 "got_object_id_str");
5839 if (got_object_tree_entry_is_submodule(te))
5841 else if (S_ISLNK(mode)) {
5844 err = got_tree_entry_get_symlink_target(&link_target,
5850 for (i = 0; i < strlen(link_target); i++) {
5851 if (!isprint((unsigned char)link_target[i]))
5852 link_target[i] = '?';
5856 else if (S_ISDIR(mode))
5858 else if (mode & S_IXUSR)
5860 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5861 got_tree_entry_get_name(te), modestr,
5862 link_target ? " -> ": "",
5863 link_target ? link_target : "") == -1) {
5866 return got_error_from_errno("asprintf");
5870 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5876 if (n == s->selected) {
5878 wstandout(view->window);
5879 s->selected_entry = te;
5881 tc = match_color(&s->colors, line);
5883 wattr_on(view->window,
5884 COLOR_PAIR(tc->colorpair), NULL);
5885 waddwstr(view->window, wline);
5887 wattr_off(view->window,
5888 COLOR_PAIR(tc->colorpair), NULL);
5889 if (width < view->ncols - 1)
5890 waddch(view->window, '\n');
5891 if (n == s->selected && view->focussed)
5892 wstandend(view->window);
5898 s->last_displayed_entry = te;
5907 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5909 struct got_tree_entry *te;
5910 int isroot = s->tree == s->root;
5913 if (s->first_displayed_entry == NULL)
5916 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5917 while (i++ < maxscroll) {
5920 s->first_displayed_entry = NULL;
5923 s->first_displayed_entry = te;
5924 te = got_tree_entry_get_prev(s->tree, te);
5928 static const struct got_error *
5929 tree_scroll_down(struct tog_view *view, int maxscroll)
5931 struct tog_tree_view_state *s = &view->state.tree;
5932 struct got_tree_entry *next, *last;
5935 if (s->first_displayed_entry)
5936 next = got_tree_entry_get_next(s->tree,
5937 s->first_displayed_entry);
5939 next = got_object_tree_get_first_entry(s->tree);
5941 last = s->last_displayed_entry;
5942 while (next && n++ < maxscroll) {
5944 last = got_tree_entry_get_next(s->tree, last);
5945 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
5946 s->first_displayed_entry = next;
5947 next = got_tree_entry_get_next(s->tree, next);
5954 static const struct got_error *
5955 tree_entry_path(char **path, struct tog_parent_trees *parents,
5956 struct got_tree_entry *te)
5958 const struct got_error *err = NULL;
5959 struct tog_parent_tree *pt;
5960 size_t len = 2; /* for leading slash and NUL */
5962 TAILQ_FOREACH(pt, parents, entry)
5963 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5966 len += strlen(got_tree_entry_get_name(te));
5968 *path = calloc(1, len);
5970 return got_error_from_errno("calloc");
5973 pt = TAILQ_LAST(parents, tog_parent_trees);
5975 const char *name = got_tree_entry_get_name(pt->selected_entry);
5976 if (strlcat(*path, name, len) >= len) {
5977 err = got_error(GOT_ERR_NO_SPACE);
5980 if (strlcat(*path, "/", len) >= len) {
5981 err = got_error(GOT_ERR_NO_SPACE);
5984 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5987 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5988 err = got_error(GOT_ERR_NO_SPACE);
6000 static const struct got_error *
6001 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6002 struct got_tree_entry *te, struct tog_parent_trees *parents,
6003 struct got_object_id *commit_id, struct got_repository *repo)
6005 const struct got_error *err = NULL;
6007 struct tog_view *blame_view;
6011 err = tree_entry_path(&path, parents, te);
6015 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6016 if (blame_view == NULL) {
6017 err = got_error_from_errno("view_open");
6021 err = open_blame_view(blame_view, path, commit_id, repo);
6023 if (err->code == GOT_ERR_CANCELLED)
6025 view_close(blame_view);
6027 *new_view = blame_view;
6033 static const struct got_error *
6034 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
6035 struct tog_tree_view_state *s)
6037 struct tog_view *log_view;
6038 const struct got_error *err = NULL;
6043 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6044 if (log_view == NULL)
6045 return got_error_from_errno("view_open");
6047 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6051 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6054 view_close(log_view);
6056 *new_view = log_view;
6061 static const struct got_error *
6062 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6063 const char *head_ref_name, struct got_repository *repo)
6065 const struct got_error *err = NULL;
6066 char *commit_id_str = NULL;
6067 struct tog_tree_view_state *s = &view->state.tree;
6068 struct got_commit_object *commit = NULL;
6070 TAILQ_INIT(&s->parents);
6071 STAILQ_INIT(&s->colors);
6073 s->commit_id = got_object_id_dup(commit_id);
6074 if (s->commit_id == NULL)
6075 return got_error_from_errno("got_object_id_dup");
6077 err = got_object_open_as_commit(&commit, repo, commit_id);
6082 * The root is opened here and will be closed when the view is closed.
6083 * Any visited subtrees and their path-wise parents are opened and
6086 err = got_object_open_as_tree(&s->root, repo,
6087 got_object_commit_get_tree_id(commit));
6092 err = got_object_id_str(&commit_id_str, commit_id);
6096 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6097 err = got_error_from_errno("asprintf");
6101 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6102 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6103 if (head_ref_name) {
6104 s->head_ref_name = strdup(head_ref_name);
6105 if (s->head_ref_name == NULL) {
6106 err = got_error_from_errno("strdup");
6112 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6113 err = add_color(&s->colors, "\\$$",
6114 TOG_COLOR_TREE_SUBMODULE,
6115 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6118 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6119 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6122 err = add_color(&s->colors, "/$",
6123 TOG_COLOR_TREE_DIRECTORY,
6124 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6128 err = add_color(&s->colors, "\\*$",
6129 TOG_COLOR_TREE_EXECUTABLE,
6130 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6134 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6135 get_color_value("TOG_COLOR_COMMIT"));
6140 view->show = show_tree_view;
6141 view->input = input_tree_view;
6142 view->close = close_tree_view;
6143 view->search_start = search_start_tree_view;
6144 view->search_next = search_next_tree_view;
6146 free(commit_id_str);
6148 got_object_commit_close(commit);
6150 close_tree_view(view);
6154 static const struct got_error *
6155 close_tree_view(struct tog_view *view)
6157 struct tog_tree_view_state *s = &view->state.tree;
6159 free_colors(&s->colors);
6160 free(s->tree_label);
6161 s->tree_label = NULL;
6163 s->commit_id = NULL;
6164 free(s->head_ref_name);
6165 s->head_ref_name = NULL;
6166 while (!TAILQ_EMPTY(&s->parents)) {
6167 struct tog_parent_tree *parent;
6168 parent = TAILQ_FIRST(&s->parents);
6169 TAILQ_REMOVE(&s->parents, parent, entry);
6170 if (parent->tree != s->root)
6171 got_object_tree_close(parent->tree);
6175 if (s->tree != NULL && s->tree != s->root)
6176 got_object_tree_close(s->tree);
6178 got_object_tree_close(s->root);
6182 static const struct got_error *
6183 search_start_tree_view(struct tog_view *view)
6185 struct tog_tree_view_state *s = &view->state.tree;
6187 s->matched_entry = NULL;
6192 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6194 regmatch_t regmatch;
6196 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6200 static const struct got_error *
6201 search_next_tree_view(struct tog_view *view)
6203 struct tog_tree_view_state *s = &view->state.tree;
6204 struct got_tree_entry *te = NULL;
6206 if (!view->searching) {
6207 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6211 if (s->matched_entry) {
6212 if (view->searching == TOG_SEARCH_FORWARD) {
6213 if (s->selected_entry)
6214 te = got_tree_entry_get_next(s->tree,
6217 te = got_object_tree_get_first_entry(s->tree);
6219 if (s->selected_entry == NULL)
6220 te = got_object_tree_get_last_entry(s->tree);
6222 te = got_tree_entry_get_prev(s->tree,
6226 if (s->selected_entry)
6227 te = s->selected_entry;
6228 else if (view->searching == TOG_SEARCH_FORWARD)
6229 te = got_object_tree_get_first_entry(s->tree);
6231 te = got_object_tree_get_last_entry(s->tree);
6236 if (s->matched_entry == NULL) {
6237 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6240 if (view->searching == TOG_SEARCH_FORWARD)
6241 te = got_object_tree_get_first_entry(s->tree);
6243 te = got_object_tree_get_last_entry(s->tree);
6246 if (match_tree_entry(te, &view->regex)) {
6247 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6248 s->matched_entry = te;
6252 if (view->searching == TOG_SEARCH_FORWARD)
6253 te = got_tree_entry_get_next(s->tree, te);
6255 te = got_tree_entry_get_prev(s->tree, te);
6258 if (s->matched_entry) {
6259 s->first_displayed_entry = s->matched_entry;
6266 static const struct got_error *
6267 show_tree_view(struct tog_view *view)
6269 const struct got_error *err = NULL;
6270 struct tog_tree_view_state *s = &view->state.tree;
6273 err = tree_entry_path(&parent_path, &s->parents, NULL);
6277 err = draw_tree_entries(view, parent_path);
6284 static const struct got_error *
6285 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6287 const struct got_error *err = NULL;
6288 struct tog_tree_view_state *s = &view->state.tree;
6289 struct tog_view *log_view, *ref_view;
6290 struct got_tree_entry *te;
6291 int begin_x = 0, n, nscroll = view->nlines - 3;
6295 s->show_ids = !s->show_ids;
6300 if (!s->selected_entry)
6302 if (view_is_parent_view(view))
6303 begin_x = view_split_begin_x(view->begin_x);
6304 err = log_selected_tree_entry(&log_view, begin_x, s);
6306 log_view->focussed = 1;
6307 if (view_is_parent_view(view)) {
6308 err = view_close_child(view);
6311 err = view_set_child(view, log_view);
6314 view->focus_child = 1;
6316 *new_view = log_view;
6320 if (view_is_parent_view(view))
6321 begin_x = view_split_begin_x(view->begin_x);
6322 ref_view = view_open(view->nlines, view->ncols,
6323 view->begin_y, begin_x, TOG_VIEW_REF);
6324 if (ref_view == NULL)
6325 return got_error_from_errno("view_open");
6326 err = open_ref_view(ref_view, s->repo);
6328 view_close(ref_view);
6332 ref_view->focussed = 1;
6333 if (view_is_parent_view(view)) {
6334 err = view_close_child(view);
6337 err = view_set_child(view, ref_view);
6340 view->focus_child = 1;
6342 *new_view = ref_view;
6348 if (s->tree == s->root)
6349 s->first_displayed_entry =
6350 got_object_tree_get_first_entry(s->tree);
6352 s->first_displayed_entry = NULL;
6356 int eos = view->nlines - 3;
6358 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6362 te = got_object_tree_get_last_entry(s->tree);
6363 for (n = 0; n < eos; n++) {
6365 if(s->tree != s->root) {
6366 s->first_displayed_entry = NULL;
6371 s->first_displayed_entry = te;
6372 te = got_tree_entry_get_prev(s->tree, te);
6375 s->selected = n - 1;
6381 if (s->selected > 0) {
6385 tree_scroll_up(s, 1);
6386 if (s->selected_entry == NULL ||
6387 (s->tree == s->root && s->selected_entry ==
6388 got_object_tree_get_first_entry(s->tree)))
6398 if (s->tree == s->root) {
6399 if (got_object_tree_get_first_entry(s->tree) ==
6400 s->first_displayed_entry)
6401 s->selected -= MIN(s->selected, nscroll);
6403 if (s->first_displayed_entry == NULL)
6404 s->selected -= MIN(s->selected, nscroll);
6406 tree_scroll_up(s, MAX(0, nscroll));
6407 if (s->selected_entry == NULL ||
6408 (s->tree == s->root && s->selected_entry ==
6409 got_object_tree_get_first_entry(s->tree)))
6415 if (s->selected < s->ndisplayed - 1) {
6419 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6421 /* can't scroll any further */
6425 tree_scroll_down(view, 1);
6435 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6437 /* can't scroll any further; move cursor down */
6438 if (s->selected < s->ndisplayed - 1)
6439 s->selected += MIN(nscroll,
6440 s->ndisplayed - s->selected - 1);
6445 tree_scroll_down(view, nscroll);
6450 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6451 struct tog_parent_tree *parent;
6452 /* user selected '..' */
6453 if (s->tree == s->root) {
6457 parent = TAILQ_FIRST(&s->parents);
6458 TAILQ_REMOVE(&s->parents, parent,
6460 got_object_tree_close(s->tree);
6461 s->tree = parent->tree;
6462 s->first_displayed_entry =
6463 parent->first_displayed_entry;
6465 parent->selected_entry;
6466 s->selected = parent->selected;
6467 if (s->selected > view->nlines - 3) {
6468 err = offset_selection_down(view);
6473 } else if (S_ISDIR(got_tree_entry_get_mode(
6474 s->selected_entry))) {
6475 struct got_tree_object *subtree;
6477 err = got_object_open_as_tree(&subtree, s->repo,
6478 got_tree_entry_get_id(s->selected_entry));
6481 err = tree_view_visit_subtree(s, subtree);
6483 got_object_tree_close(subtree);
6486 } else if (S_ISREG(got_tree_entry_get_mode(
6487 s->selected_entry))) {
6488 struct tog_view *blame_view;
6489 int begin_x = 0, begin_y = 0;
6491 if (view_is_parent_view(view))
6492 view_get_split(view, &begin_y, &begin_x);
6494 err = blame_tree_entry(&blame_view, begin_y, begin_x,
6495 s->selected_entry, &s->parents,
6496 s->commit_id, s->repo);
6500 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
6501 err = view_init_hsplit(view, begin_y);
6508 blame_view->focussed = 1;
6509 blame_view->mode = view->mode;
6510 blame_view->nlines = view->lines - begin_y;
6511 if (view_is_parent_view(view)) {
6512 err = view_close_child(view);
6515 err = view_set_child(view, blame_view);
6518 view->focus_child = 1;
6520 *new_view = blame_view;
6524 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6525 s->selected = view->nlines - 4;
6541 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6546 static const struct got_error *
6547 cmd_tree(int argc, char *argv[])
6549 const struct got_error *error;
6550 struct got_repository *repo = NULL;
6551 struct got_worktree *worktree = NULL;
6552 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6553 struct got_object_id *commit_id = NULL;
6554 struct got_commit_object *commit = NULL;
6555 const char *commit_id_arg = NULL;
6557 struct got_reference *ref = NULL;
6558 const char *head_ref_name = NULL;
6560 struct tog_view *view;
6561 int *pack_fds = NULL;
6563 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6566 commit_id_arg = optarg;
6569 repo_path = realpath(optarg, NULL);
6570 if (repo_path == NULL)
6571 return got_error_from_errno2("realpath",
6586 error = got_repo_pack_fds_open(&pack_fds);
6590 if (repo_path == NULL) {
6591 cwd = getcwd(NULL, 0);
6593 return got_error_from_errno("getcwd");
6594 error = got_worktree_open(&worktree, cwd);
6595 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6599 strdup(got_worktree_get_repo_path(worktree));
6601 repo_path = strdup(cwd);
6602 if (repo_path == NULL) {
6603 error = got_error_from_errno("strdup");
6608 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6612 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6619 error = apply_unveil(got_repo_get_path(repo), NULL);
6623 error = tog_load_refs(repo, 0);
6627 if (commit_id_arg == NULL) {
6628 error = got_repo_match_object_id(&commit_id, &label,
6629 worktree ? got_worktree_get_head_ref_name(worktree) :
6630 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6633 head_ref_name = label;
6635 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6637 head_ref_name = got_ref_get_name(ref);
6638 else if (error->code != GOT_ERR_NOT_REF)
6640 error = got_repo_match_object_id(&commit_id, NULL,
6641 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6646 error = got_object_open_as_commit(&commit, repo, commit_id);
6650 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6652 error = got_error_from_errno("view_open");
6655 error = open_tree_view(view, commit_id, head_ref_name, repo);
6658 if (!got_path_is_root_dir(in_repo_path)) {
6659 error = tree_view_walk_path(&view->state.tree, commit,
6666 /* Release work tree lock. */
6667 got_worktree_close(worktree);
6670 error = view_loop(view);
6679 const struct got_error *close_err = got_repo_close(repo);
6684 const struct got_error *pack_err =
6685 got_repo_pack_fds_close(pack_fds);
6693 static const struct got_error *
6694 ref_view_load_refs(struct tog_ref_view_state *s)
6696 struct got_reflist_entry *sre;
6697 struct tog_reflist_entry *re;
6700 TAILQ_FOREACH(sre, &tog_refs, entry) {
6701 if (strncmp(got_ref_get_name(sre->ref),
6702 "refs/got/", 9) == 0 &&
6703 strncmp(got_ref_get_name(sre->ref),
6704 "refs/got/backup/", 16) != 0)
6707 re = malloc(sizeof(*re));
6709 return got_error_from_errno("malloc");
6711 re->ref = got_ref_dup(sre->ref);
6712 if (re->ref == NULL)
6713 return got_error_from_errno("got_ref_dup");
6714 re->idx = s->nrefs++;
6715 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6718 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6723 ref_view_free_refs(struct tog_ref_view_state *s)
6725 struct tog_reflist_entry *re;
6727 while (!TAILQ_EMPTY(&s->refs)) {
6728 re = TAILQ_FIRST(&s->refs);
6729 TAILQ_REMOVE(&s->refs, re, entry);
6730 got_ref_close(re->ref);
6735 static const struct got_error *
6736 open_ref_view(struct tog_view *view, struct got_repository *repo)
6738 const struct got_error *err = NULL;
6739 struct tog_ref_view_state *s = &view->state.ref;
6741 s->selected_entry = 0;
6744 TAILQ_INIT(&s->refs);
6745 STAILQ_INIT(&s->colors);
6747 err = ref_view_load_refs(s);
6751 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6752 err = add_color(&s->colors, "^refs/heads/",
6753 TOG_COLOR_REFS_HEADS,
6754 get_color_value("TOG_COLOR_REFS_HEADS"));
6758 err = add_color(&s->colors, "^refs/tags/",
6759 TOG_COLOR_REFS_TAGS,
6760 get_color_value("TOG_COLOR_REFS_TAGS"));
6764 err = add_color(&s->colors, "^refs/remotes/",
6765 TOG_COLOR_REFS_REMOTES,
6766 get_color_value("TOG_COLOR_REFS_REMOTES"));
6770 err = add_color(&s->colors, "^refs/got/backup/",
6771 TOG_COLOR_REFS_BACKUP,
6772 get_color_value("TOG_COLOR_REFS_BACKUP"));
6777 view->show = show_ref_view;
6778 view->input = input_ref_view;
6779 view->close = close_ref_view;
6780 view->search_start = search_start_ref_view;
6781 view->search_next = search_next_ref_view;
6784 free_colors(&s->colors);
6788 static const struct got_error *
6789 close_ref_view(struct tog_view *view)
6791 struct tog_ref_view_state *s = &view->state.ref;
6793 ref_view_free_refs(s);
6794 free_colors(&s->colors);
6799 static const struct got_error *
6800 resolve_reflist_entry(struct got_object_id **commit_id,
6801 struct tog_reflist_entry *re, struct got_repository *repo)
6803 const struct got_error *err = NULL;
6804 struct got_object_id *obj_id;
6805 struct got_tag_object *tag = NULL;
6810 err = got_ref_resolve(&obj_id, repo, re->ref);
6814 err = got_object_get_type(&obj_type, repo, obj_id);
6819 case GOT_OBJ_TYPE_COMMIT:
6820 *commit_id = obj_id;
6822 case GOT_OBJ_TYPE_TAG:
6823 err = got_object_open_as_tag(&tag, repo, obj_id);
6827 err = got_object_get_type(&obj_type, repo,
6828 got_object_tag_get_object_id(tag));
6831 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6832 err = got_error(GOT_ERR_OBJ_TYPE);
6835 *commit_id = got_object_id_dup(
6836 got_object_tag_get_object_id(tag));
6837 if (*commit_id == NULL) {
6838 err = got_error_from_errno("got_object_id_dup");
6843 err = got_error(GOT_ERR_OBJ_TYPE);
6849 got_object_tag_close(tag);
6857 static const struct got_error *
6858 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
6859 struct tog_reflist_entry *re, struct got_repository *repo)
6861 struct tog_view *log_view;
6862 const struct got_error *err = NULL;
6863 struct got_object_id *commit_id = NULL;
6867 err = resolve_reflist_entry(&commit_id, re, repo);
6869 if (err->code != GOT_ERR_OBJ_TYPE)
6875 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6876 if (log_view == NULL) {
6877 err = got_error_from_errno("view_open");
6881 err = open_log_view(log_view, commit_id, repo,
6882 got_ref_get_name(re->ref), "", 0);
6885 view_close(log_view);
6887 *new_view = log_view;
6893 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6895 struct tog_reflist_entry *re;
6898 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6901 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6902 while (i++ < maxscroll) {
6905 s->first_displayed_entry = re;
6906 re = TAILQ_PREV(re, tog_reflist_head, entry);
6910 static const struct got_error *
6911 ref_scroll_down(struct tog_view *view, int maxscroll)
6913 struct tog_ref_view_state *s = &view->state.ref;
6914 struct tog_reflist_entry *next, *last;
6917 if (s->first_displayed_entry)
6918 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6920 next = TAILQ_FIRST(&s->refs);
6922 last = s->last_displayed_entry;
6923 while (next && n++ < maxscroll) {
6925 last = TAILQ_NEXT(last, entry);
6926 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
6927 s->first_displayed_entry = next;
6928 next = TAILQ_NEXT(next, entry);
6935 static const struct got_error *
6936 search_start_ref_view(struct tog_view *view)
6938 struct tog_ref_view_state *s = &view->state.ref;
6940 s->matched_entry = NULL;
6945 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6947 regmatch_t regmatch;
6949 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6953 static const struct got_error *
6954 search_next_ref_view(struct tog_view *view)
6956 struct tog_ref_view_state *s = &view->state.ref;
6957 struct tog_reflist_entry *re = NULL;
6959 if (!view->searching) {
6960 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6964 if (s->matched_entry) {
6965 if (view->searching == TOG_SEARCH_FORWARD) {
6966 if (s->selected_entry)
6967 re = TAILQ_NEXT(s->selected_entry, entry);
6969 re = TAILQ_PREV(s->selected_entry,
6970 tog_reflist_head, entry);
6972 if (s->selected_entry == NULL)
6973 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6975 re = TAILQ_PREV(s->selected_entry,
6976 tog_reflist_head, entry);
6979 if (s->selected_entry)
6980 re = s->selected_entry;
6981 else if (view->searching == TOG_SEARCH_FORWARD)
6982 re = TAILQ_FIRST(&s->refs);
6984 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6989 if (s->matched_entry == NULL) {
6990 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6993 if (view->searching == TOG_SEARCH_FORWARD)
6994 re = TAILQ_FIRST(&s->refs);
6996 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6999 if (match_reflist_entry(re, &view->regex)) {
7000 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7001 s->matched_entry = re;
7005 if (view->searching == TOG_SEARCH_FORWARD)
7006 re = TAILQ_NEXT(re, entry);
7008 re = TAILQ_PREV(re, tog_reflist_head, entry);
7011 if (s->matched_entry) {
7012 s->first_displayed_entry = s->matched_entry;
7019 static const struct got_error *
7020 show_ref_view(struct tog_view *view)
7022 const struct got_error *err = NULL;
7023 struct tog_ref_view_state *s = &view->state.ref;
7024 struct tog_reflist_entry *re;
7027 struct tog_color *tc;
7029 int limit = view->nlines;
7031 werase(view->window);
7034 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
7035 view_is_splitscreen(view->child))
7036 --limit; /* border */
7041 re = s->first_displayed_entry;
7043 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7045 return got_error_from_errno("asprintf");
7047 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7052 if (view_needs_focus_indication(view))
7053 wstandout(view->window);
7054 waddwstr(view->window, wline);
7055 if (view_needs_focus_indication(view))
7056 wstandend(view->window);
7061 if (width < view->ncols - 1)
7062 waddch(view->window, '\n');
7067 while (re && limit > 0) {
7069 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7072 struct got_commit_object *ci;
7073 struct got_tag_object *tag;
7074 struct got_object_id *id;
7078 err = got_ref_resolve(&id, s->repo, re->ref);
7081 err = got_object_open_as_tag(&tag, s->repo, id);
7083 if (err->code != GOT_ERR_OBJ_TYPE) {
7087 err = got_object_open_as_commit(&ci, s->repo,
7093 t = got_object_commit_get_committer_time(ci);
7094 got_object_commit_close(ci);
7096 t = got_object_tag_get_tagger_time(tag);
7097 got_object_tag_close(tag);
7100 if (gmtime_r(&t, &tm) == NULL)
7101 return got_error_from_errno("gmtime_r");
7102 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7103 return got_error(GOT_ERR_NO_SPACE);
7105 if (got_ref_is_symbolic(re->ref)) {
7106 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7107 ymd : "", got_ref_get_name(re->ref),
7108 got_ref_get_symref_target(re->ref)) == -1)
7109 return got_error_from_errno("asprintf");
7110 } else if (s->show_ids) {
7111 struct got_object_id *id;
7113 err = got_ref_resolve(&id, s->repo, re->ref);
7116 err = got_object_id_str(&id_str, id);
7121 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7122 got_ref_get_name(re->ref), id_str) == -1) {
7123 err = got_error_from_errno("asprintf");
7130 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7131 got_ref_get_name(re->ref)) == -1)
7132 return got_error_from_errno("asprintf");
7134 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7140 if (n == s->selected) {
7142 wstandout(view->window);
7143 s->selected_entry = re;
7145 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7147 wattr_on(view->window,
7148 COLOR_PAIR(tc->colorpair), NULL);
7149 waddwstr(view->window, wline);
7151 wattr_off(view->window,
7152 COLOR_PAIR(tc->colorpair), NULL);
7153 if (width < view->ncols - 1)
7154 waddch(view->window, '\n');
7155 if (n == s->selected && view->focussed)
7156 wstandend(view->window);
7162 s->last_displayed_entry = re;
7165 re = TAILQ_NEXT(re, entry);
7172 static const struct got_error *
7173 browse_ref_tree(struct tog_view **new_view, int begin_x,
7174 struct tog_reflist_entry *re, struct got_repository *repo)
7176 const struct got_error *err = NULL;
7177 struct got_object_id *commit_id = NULL;
7178 struct tog_view *tree_view;
7182 err = resolve_reflist_entry(&commit_id, re, repo);
7184 if (err->code != GOT_ERR_OBJ_TYPE)
7191 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
7192 if (tree_view == NULL) {
7193 err = got_error_from_errno("view_open");
7197 err = open_tree_view(tree_view, commit_id,
7198 got_ref_get_name(re->ref), repo);
7202 *new_view = tree_view;
7207 static const struct got_error *
7208 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7210 const struct got_error *err = NULL;
7211 struct tog_ref_view_state *s = &view->state.ref;
7212 struct tog_view *log_view, *tree_view;
7213 struct tog_reflist_entry *re;
7214 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7218 s->show_ids = !s->show_ids;
7222 s->show_date = !s->show_date;
7226 s->sort_by_date = !s->sort_by_date;
7228 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7229 got_ref_cmp_by_commit_timestamp_descending :
7230 tog_ref_cmp_by_name, s->repo);
7233 got_reflist_object_id_map_free(tog_refs_idmap);
7234 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7235 &tog_refs, s->repo);
7238 ref_view_free_refs(s);
7239 err = ref_view_load_refs(s);
7244 if (!s->selected_entry)
7246 if (view_is_parent_view(view))
7247 view_get_split(view, &begin_y, &begin_x);
7249 err = log_ref_entry(&log_view, begin_y, begin_x,
7250 s->selected_entry, s->repo);
7254 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
7255 err = view_init_hsplit(view, begin_y);
7261 log_view->focussed = 1;
7262 log_view->mode = view->mode;
7263 log_view->nlines = view->lines - begin_y;
7264 if (view_is_parent_view(view)) {
7265 err = view_close_child(view);
7268 err = view_set_child(view, log_view);
7271 view->focus_child = 1;
7273 *new_view = log_view;
7277 if (!s->selected_entry)
7279 if (view_is_parent_view(view))
7280 begin_x = view_split_begin_x(view->begin_x);
7281 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
7283 if (err || tree_view == NULL)
7286 tree_view->focussed = 1;
7287 if (view_is_parent_view(view)) {
7288 err = view_close_child(view);
7291 err = view_set_child(view, tree_view);
7294 view->focus_child = 1;
7296 *new_view = tree_view;
7302 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7306 int eos = view->nlines - 1;
7308 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7312 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7313 for (n = 0; n < eos; n++) {
7316 s->first_displayed_entry = re;
7317 re = TAILQ_PREV(re, tog_reflist_head, entry);
7320 s->selected = n - 1;
7326 if (s->selected > 0) {
7330 ref_scroll_up(s, 1);
7331 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7341 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7342 s->selected -= MIN(nscroll, s->selected);
7343 ref_scroll_up(s, MAX(0, nscroll));
7344 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7350 if (s->selected < s->ndisplayed - 1) {
7354 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7355 /* can't scroll any further */
7359 ref_scroll_down(view, 1);
7369 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7370 /* can't scroll any further; move cursor down */
7371 if (s->selected < s->ndisplayed - 1)
7372 s->selected += MIN(nscroll,
7373 s->ndisplayed - s->selected - 1);
7374 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7375 s->selected += s->ndisplayed - s->selected - 1;
7379 ref_scroll_down(view, nscroll);
7384 err = tog_load_refs(s->repo, s->sort_by_date);
7387 ref_view_free_refs(s);
7388 err = ref_view_load_refs(s);
7391 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7392 s->selected = view->nlines - 2;
7406 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7411 static const struct got_error *
7412 cmd_ref(int argc, char *argv[])
7414 const struct got_error *error;
7415 struct got_repository *repo = NULL;
7416 struct got_worktree *worktree = NULL;
7417 char *cwd = NULL, *repo_path = NULL;
7419 struct tog_view *view;
7420 int *pack_fds = NULL;
7422 while ((ch = getopt(argc, argv, "r:")) != -1) {
7425 repo_path = realpath(optarg, NULL);
7426 if (repo_path == NULL)
7427 return got_error_from_errno2("realpath",
7442 error = got_repo_pack_fds_open(&pack_fds);
7446 if (repo_path == NULL) {
7447 cwd = getcwd(NULL, 0);
7449 return got_error_from_errno("getcwd");
7450 error = got_worktree_open(&worktree, cwd);
7451 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7455 strdup(got_worktree_get_repo_path(worktree));
7457 repo_path = strdup(cwd);
7458 if (repo_path == NULL) {
7459 error = got_error_from_errno("strdup");
7464 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7470 error = apply_unveil(got_repo_get_path(repo), NULL);
7474 error = tog_load_refs(repo, 0);
7478 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7480 error = got_error_from_errno("view_open");
7484 error = open_ref_view(view, repo);
7489 /* Release work tree lock. */
7490 got_worktree_close(worktree);
7493 error = view_loop(view);
7498 const struct got_error *close_err = got_repo_close(repo);
7503 const struct got_error *pack_err =
7504 got_repo_pack_fds_close(pack_fds);
7513 * If view was scrolled down to move the selected line into view when opening a
7514 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7517 offset_selection_up(struct tog_view *view)
7519 switch (view->type) {
7520 case TOG_VIEW_BLAME: {
7521 struct tog_blame_view_state *s = &view->state.blame;
7522 if (s->first_displayed_line == 1) {
7523 s->selected_line = MAX(s->selected_line - view->offset,
7527 if (s->first_displayed_line > view->offset)
7528 s->first_displayed_line -= view->offset;
7530 s->first_displayed_line = 1;
7531 s->selected_line += view->offset;
7535 log_scroll_up(&view->state.log, view->offset);
7536 view->state.log.selected += view->offset;
7539 ref_scroll_up(&view->state.ref, view->offset);
7540 view->state.ref.selected += view->offset;
7543 tree_scroll_up(&view->state.tree, view->offset);
7544 view->state.tree.selected += view->offset;
7554 * If the selected line is in the section of screen covered by the bottom split,
7555 * scroll down offset lines to move it into view and index its new position.
7557 static const struct got_error *
7558 offset_selection_down(struct tog_view *view)
7560 const struct got_error *err = NULL;
7561 const struct got_error *(*scrolld)(struct tog_view *, int);
7562 int *selected = NULL;
7565 switch (view->type) {
7566 case TOG_VIEW_BLAME: {
7567 struct tog_blame_view_state *s = &view->state.blame;
7570 if (s->selected_line > view->nlines - header) {
7571 offset = abs(view->nlines - s->selected_line - header);
7572 s->first_displayed_line += offset;
7573 s->selected_line -= offset;
7574 view->offset = offset;
7578 case TOG_VIEW_LOG: {
7579 struct tog_log_view_state *s = &view->state.log;
7580 scrolld = &log_scroll_down;
7582 selected = &s->selected;
7585 case TOG_VIEW_REF: {
7586 struct tog_ref_view_state *s = &view->state.ref;
7587 scrolld = &ref_scroll_down;
7589 selected = &s->selected;
7592 case TOG_VIEW_TREE: {
7593 struct tog_tree_view_state *s = &view->state.tree;
7594 scrolld = &tree_scroll_down;
7596 selected = &s->selected;
7606 if (selected && *selected > view->nlines - header) {
7607 offset = abs(view->nlines - *selected - header);
7608 view->offset = offset;
7609 if (scrolld && offset) {
7610 err = scrolld(view, offset);
7611 *selected -= offset;
7619 list_commands(FILE *fp)
7623 fprintf(fp, "commands:");
7624 for (i = 0; i < nitems(tog_commands); i++) {
7625 const struct tog_cmd *cmd = &tog_commands[i];
7626 fprintf(fp, " %s", cmd->name);
7632 usage(int hflag, int status)
7634 FILE *fp = (status == 0) ? stdout : stderr;
7636 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7639 fprintf(fp, "lazy usage: %s path\n", getprogname());
7646 make_argv(int argc, ...)
7654 argv = calloc(argc, sizeof(char *));
7657 for (i = 0; i < argc; i++) {
7658 argv[i] = strdup(va_arg(ap, char *));
7659 if (argv[i] == NULL)
7668 * Try to convert 'tog path' into a 'tog log path' command.
7669 * The user could simply have mistyped the command rather than knowingly
7670 * provided a path. So check whether argv[0] can in fact be resolved
7671 * to a path in the HEAD commit and print a special error if not.
7672 * This hack is for mpi@ <3
7674 static const struct got_error *
7675 tog_log_with_path(int argc, char *argv[])
7677 const struct got_error *error = NULL, *close_err;
7678 const struct tog_cmd *cmd = NULL;
7679 struct got_repository *repo = NULL;
7680 struct got_worktree *worktree = NULL;
7681 struct got_object_id *commit_id = NULL, *id = NULL;
7682 struct got_commit_object *commit = NULL;
7683 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7684 char *commit_id_str = NULL, **cmd_argv = NULL;
7685 int *pack_fds = NULL;
7687 cwd = getcwd(NULL, 0);
7689 return got_error_from_errno("getcwd");
7691 error = got_repo_pack_fds_open(&pack_fds);
7695 error = got_worktree_open(&worktree, cwd);
7696 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7700 repo_path = strdup(got_worktree_get_repo_path(worktree));
7702 repo_path = strdup(cwd);
7703 if (repo_path == NULL) {
7704 error = got_error_from_errno("strdup");
7708 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7712 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7717 error = tog_load_refs(repo, 0);
7720 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7721 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7722 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7727 got_worktree_close(worktree);
7731 error = got_object_open_as_commit(&commit, repo, commit_id);
7735 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7737 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7739 fprintf(stderr, "%s: '%s' is no known command or path\n",
7740 getprogname(), argv[0]);
7745 close_err = got_repo_close(repo);
7750 error = got_object_id_str(&commit_id_str, commit_id);
7754 cmd = &tog_commands[0]; /* log */
7756 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7757 error = cmd->cmd_main(argc, cmd_argv);
7760 close_err = got_repo_close(repo);
7765 got_object_commit_close(commit);
7767 got_worktree_close(worktree);
7769 const struct got_error *pack_err =
7770 got_repo_pack_fds_close(pack_fds);
7775 free(commit_id_str);
7782 for (i = 0; i < argc; i++)
7791 main(int argc, char *argv[])
7793 const struct got_error *error = NULL;
7794 const struct tog_cmd *cmd = NULL;
7795 int ch, hflag = 0, Vflag = 0;
7796 char **cmd_argv = NULL;
7797 static const struct option longopts[] = {
7798 { "version", no_argument, NULL, 'V' },
7802 setlocale(LC_CTYPE, "");
7804 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7824 got_version_print_str();
7829 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7837 /* Build an argument vector which runs a default command. */
7838 cmd = &tog_commands[0];
7840 cmd_argv = make_argv(argc, cmd->name);
7844 /* Did the user specify a command? */
7845 for (i = 0; i < nitems(tog_commands); i++) {
7846 if (strncmp(tog_commands[i].name, argv[0],
7847 strlen(argv[0])) == 0) {
7848 cmd = &tog_commands[i];
7857 /* No command specified; try log with a path */
7858 error = tog_log_with_path(argc, argv);
7863 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7870 for (i = 0; i < argc; i++)
7875 if (error && error->code != GOT_ERR_CANCELLED)
7876 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);